From 03968e0515c1b137cad6fb2c4200ec5d952a9e5b Mon Sep 17 00:00:00 2001 From: mkvolkov Date: Sat, 20 Aug 2022 21:51:57 +0300 Subject: [PATCH] RemoveAll improved --- rclonefs/fs.go | 61 ++++++++++++++++++++++++---------------------- rclonetest/main.go | 10 +++----- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/rclonefs/fs.go b/rclonefs/fs.go index 4fceb77..2662f79 100644 --- a/rclonefs/fs.go +++ b/rclonefs/fs.go @@ -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 { @@ -108,44 +110,45 @@ 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 - } - - afero.Walk(rcfs, path, func(path string, info fs.FileInfo, err error) error { - if err != nil { - return err - } - - if ok, err := afero.IsEmpty(rcfs, path); ok { - rcfs.Remove(path) - return nil - } else { - return err - } - }) + if e != nil { + return e } + for _, f := range fileList { + if e := rcfs.Remove(f); e != nil { + return e + } + } + + 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 } diff --git a/rclonetest/main.go b/rclonetest/main.go index 861477d..decca6f 100644 --- a/rclonetest/main.go +++ b/rclonetest/main.go @@ -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() }