From 535664608a7221b8d167197bbee262584bd160c9 Mon Sep 17 00:00:00 2001 From: Adphi Date: Sat, 20 Jul 2019 22:49:45 +0200 Subject: [PATCH] wip: improved walk performances --- webdav.go | 31 +++++++++++-------------------- webdav_test.go | 7 +++---- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/webdav.go b/webdav.go index db82741..ee679a8 100644 --- a/webdav.go +++ b/webdav.go @@ -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 } diff --git a/webdav_test.go b/webdav_test.go index dacf152..429c987 100644 --- a/webdav_test.go +++ b/webdav_test.go @@ -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)