RemoveAll improved

This commit is contained in:
mkvolkov 2022-08-20 21:51:57 +03:00
parent abb7f53bae
commit 03968e0515
2 changed files with 36 additions and 35 deletions

View File

@ -7,6 +7,7 @@ import (
"os"
"os/user"
"path/filepath"
"sort"
"strings"
"time"
@ -69,8 +70,9 @@ func (rcfs *RCFS) Create(name string) (afero.File, error) {
}
func (rcfs *RCFS) Mkdir(name string, perm os.FileMode) error {
// TODO
return nil
name = rcfs.AbsPath(name)
return rcfs.Fs.Mkdir(name, perm)
}
func (rcfs *RCFS) MkdirAll(name string, perm os.FileMode) error {
@ -109,43 +111,44 @@ func (rcfs *RCFS) Remove(name string) error {
func (rcfs *RCFS) RemoveAll(path string) error {
path = rcfs.AbsPath(path)
afero.Walk(rcfs, path, func(path string, info fs.FileInfo, err error) error {
fileList := make([]string, 0)
dirList := make([]string, 0)
e := afero.Walk(rcfs, path, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
dirList = append(dirList, path)
} else {
rcfs.Remove(path)
return nil
fileList = append(fileList, path)
}
return nil
})
for {
if ok, err := afero.IsEmpty(rcfs, path); err == nil {
if ok {
rcfs.Remove(path)
break
}
} else {
return err
if e != nil {
return e
}
afero.Walk(rcfs, path, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
for _, f := range fileList {
if e := rcfs.Remove(f); e != nil {
return e
}
}
if ok, err := afero.IsEmpty(rcfs, path); ok {
rcfs.Remove(path)
return nil
} else {
return err
}
sort.Slice(dirList, func(i, j int) bool {
return len(dirList[i]) > len(dirList[j])
})
for _, d := range dirList {
if e := rcfs.Remove(d); e != nil {
return e
}
}
rcfs.Remove(path)
return nil
}

View File

@ -8,14 +8,12 @@ import (
)
func main() {
RFS, _ := rclonefs.CreateRCFS("pcloud_mv1:/cfg/json")
RFS, _ := rclonefs.CreateRCFS("pcloud_mv1:/cfg")
f, e := RFS.Create("../obj3.json")
if e != nil {
fmt.Printf("Error: %v\n", e)
err := RFS.RemoveAll("yml")
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
defer f.Close()
}