wip: improved walk performances

This commit is contained in:
Adphi 2019-07-20 22:49:45 +02:00
parent ba43e1dcbb
commit 535664608a
2 changed files with 14 additions and 24 deletions

View File

@ -43,7 +43,7 @@ func (wd *webDav) walk(path string, info os.FileInfo, walkFn filepath.WalkFunc)
if !info.IsDir() { if !info.IsDir() {
return walkFn(path, info, nil) return walkFn(path, info, nil)
} }
names, err := wd.readDirNames(path) fis, err := wd.readDir(path)
err1 := walkFn(path, info, err) err1 := walkFn(path, info, err)
// If err != nil, walk can't walk into this directory. // If err != nil, walk can't walk into this directory.
// err1 != nil means walkFn want walk to skip this directory or stop walking. // err1 != nil means walkFn want walk to skip this directory or stop walking.
@ -56,36 +56,27 @@ func (wd *webDav) walk(path string, info os.FileInfo, walkFn filepath.WalkFunc)
return err1 return err1
} }
for _, name := range names { for _, fi := range fis {
filename := filepath.Join(path, name) filename := filepath.Join(path, fi.Name())
fileInfo, err := wd.Stat(filename) err = wd.walk(filename, fi, walkFn)
if err != nil { if err != nil {
if err := walkFn(filename, fileInfo, err); err != nil && err != filepath.SkipDir { if !fi.IsDir() || err != filepath.SkipDir {
return err return err
} }
} else {
err = wd.walk(filename, fileInfo, walkFn)
if err != nil {
if !fileInfo.IsDir() || err != filepath.SkipDir {
return err
}
}
} }
} }
return nil return nil
} }
// readDirNames reads the directory named by dirname and returns // readDir reads the directory and returns
// a sorted list of directory entries. // a sorted list of directory entries.
func (wd *webDav) readDirNames(dirname string) ([]string, error) { func (wd *webDav) readDir(dirname string) ([]os.FileInfo, error) {
fs, err := wd.ReadDir(dirname) fs, err := wd.ReadDir(dirname)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var names []string sort.Slice(fs, func(i, j int) bool {
for _, i := range fs { return sort.StringsAreSorted([]string{fs[i].Name(), fs[j].Name()})
names = append(names, i.Name()) })
} return fs, nil
sort.Strings(names)
return names, nil
} }

View File

@ -60,7 +60,7 @@ func testCreateFolder(t *testing.T){
func testStat(t *testing.T) { func testStat(t *testing.T) {
i, err := wd.Stat(dir) i, err := wd.Stat(dir)
require.NoError(t, err) require.NoError(t, err)
// TODO : there is a problem with fileinfo's Name : find a fix // TODO : there is a problem with fileinfo's Name for directories: find a fix
// assert.Equal(t, dir, i.Name()) // assert.Equal(t, dir, i.Name())
assert.True(t, i.IsDir()) assert.True(t, i.IsDir())
} }
@ -73,9 +73,8 @@ func testWalk(t *testing.T) {
if path == dir { if path == dir {
found = true found = true
} }
// TODO : there is a problem with fileinfo's Name : find a fix p := strings.Split(path, "/")
// p := strings.Split(path, "/") assert.Equal(t, p[len(p)-1], info.Name())
// assert.Equal(t, p[len(p)-1], info.Name())
return nil return nil
}) })
assert.NoError(t, err) assert.NoError(t, err)