func (fs *dsFileSys) RemoveAll(path string) error { paths := []string{} walkRemove := func(path string, f os.FileInfo, err error) error { if err != nil { // do nothing; don't break the walk aelog.Errorf(fs.Ctx(), "Error walking %v => %v", path, err) } else { if f != nil { // && f.IsDir() to constrain paths = append(paths, path) } } return nil } err := common.Walk(fs, path, walkRemove) if err != nil { aelog.Errorf(fs.Ctx(), "Error removing %v => %v", path, err) } // Walk crawls directories first, files second. // Intuitively removal in reverse order should always work. Or does it not? for i := 0; i < len(paths); i++ { iRev := len(paths) - 1 - i err := fs.Remove(paths[iRev]) if err != nil { aelog.Errorf(fs.Ctx(), "Error removing %v => %v", paths[iRev], err) return err } aelog.Infof(fs.Ctx(), "removed path %v", paths[iRev]) } return nil }
func WalkDirs(fs fsi.FileSystem) (*bytes.Buffer, string) { bb := new(bytes.Buffer) wpf(bb, "-------filewalk----\n\n") wnt := []int{16, 6, 3} wnt2 := []int{13, 3, 0} got := []int{} cntr := 0 walkFunc := func(path string, f os.FileInfo, err error) error { if err != nil { wpf(bb, "error on visiting %q => %v \n", path, err) if err == datastore.ErrNoSuchEntity || err == os.ErrNotExist { return nil // dont break the walk on this, it's just a stale directory } return err // this would break the walk on any error; notably dir-index entries, that have been deleted since. } if strings.HasSuffix(path, "_secretdir") { return common.SkipDir // do not delve deeper } if err == os.ErrInvalid { return err // calling off the walk } tp := "file" if f != nil { if f.IsDir() { tp = "dir " } } wpf(bb, "Visited: %s %s \n", tp, path) cntr++ // if cntr > 100 { // return fmt.Errorf("too many walk recursions%v", cntr) // } return nil } var err error cntr = 0 err = common.Walk(fs, rel, walkFunc) wpf(bb, "fs.Walk() returned %v\n\n", err) got = append(got, cntr) cntr = 0 err = common.Walk(fs, "ch1/ch2", walkFunc) wpf(bb, "fs.Walk() returned %v\n\n", err) got = append(got, cntr) cntr = 0 err = common.Walk(fs, "ch1/ch2/ch3", walkFunc) wpf(bb, "fs.Walk() returned %v\n\n", err) got = append(got, cntr) testRes := "" if spf("%+v", wnt) != spf("%+v", got) && spf("%+v", wnt2) != spf("%+v", got) { testRes = spf("WalkDir: wnt %v or %v - got %v", wnt, wnt2, got) } return bb, testRes }