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

View File

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