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() {
return walkFn(path, info, nil)
}
names, err := wd.readDirNames(path)
fis, err := wd.readDir(path)
err1 := walkFn(path, info, err)
// If err != nil, walk can't walk into this directory.
// 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
}
for _, name := range names {
filename := filepath.Join(path, name)
fileInfo, err := wd.Stat(filename)
for _, fi := range fis {
filename := filepath.Join(path, fi.Name())
err = wd.walk(filename, fi, walkFn)
if err != nil {
if err := walkFn(filename, fileInfo, err); err != nil && err != filepath.SkipDir {
if !fi.IsDir() || err != filepath.SkipDir {
return err
}
} else {
err = wd.walk(filename, fileInfo, walkFn)
if err != nil {
if !fileInfo.IsDir() || err != filepath.SkipDir {
return err
}
}
}
}
return nil
}
// readDirNames reads the directory named by dirname and returns
// readDir reads the directory and returns
// 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)
if err != nil {
return nil, err
}
var names []string
for _, i := range fs {
names = append(names, i.Name())
}
sort.Strings(names)
return names, nil
sort.Slice(fs, func(i, j int) bool {
return sort.StringsAreSorted([]string{fs[i].Name(), fs[j].Name()})
})
return fs, nil
}

View File

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