From 47f1d2487bf70caa0db8de029ab95d7d39e7c501 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 15 Nov 2015 11:39:32 -0800 Subject: [PATCH] fs: Non-recursive send in sorted order, note will be slow for large directories --- pkg/client/fs/fs.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pkg/client/fs/fs.go b/pkg/client/fs/fs.go index 7e09e35b..dfe9a967 100644 --- a/pkg/client/fs/fs.go +++ b/pkg/client/fs/fs.go @@ -21,6 +21,7 @@ import ( "os" "path/filepath" "runtime" + "sort" "strings" "time" @@ -229,6 +230,12 @@ func (f *fsClient) List(recursive, incomplete bool) <-chan client.ContentOnChann return contentCh } +type byName []os.FileInfo + +func (b byName) Len() int { return len(b) } +func (b byName) Swap(i, j int) { b[i], b[j] = b[j], b[i] } +func (b byName) Less(i, j int) bool { return b[i].Name() < b[j].Name() } + func (f *fsClient) listInRoutine(contentCh chan client.ContentOnChannel) { defer close(contentCh) @@ -258,6 +265,8 @@ func (f *fsClient) listInRoutine(contentCh chan client.ContentOnChannel) { } return } + // NOTE: This will be slow for large directories. + sort.Sort(byName(files)) for _, fi := range files { file := filepath.Join(dir.Name(), fi.Name()) if fi.Mode()&os.ModeSymlink == os.ModeSymlink { @@ -328,6 +337,8 @@ func (f *fsClient) listInRoutine(contentCh chan client.ContentOnChannel) { } return } + // NOTE: This will be slow for large directories. + sort.Sort(byName(files)) for _, file := range files { fi := file if fi.Mode()&os.ModeSymlink == os.ModeSymlink {