From 6f7e752aa011b803a5c5f4f6637046142a857f18 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 11 Nov 2015 14:31:15 -0800 Subject: [PATCH] fs: Implement custom walk to handle sorting properly --- pkg/client/fs/fs-utils.go | 129 ++++++++++++++++++++++++++++++++++++++ pkg/client/fs/fs.go | 6 +- pkg/client/fs/fs_test.go | 8 ++- 3 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 pkg/client/fs/fs-utils.go diff --git a/pkg/client/fs/fs-utils.go b/pkg/client/fs/fs-utils.go new file mode 100644 index 00000000..0af48b7f --- /dev/null +++ b/pkg/client/fs/fs-utils.go @@ -0,0 +1,129 @@ +/* + * Minio Client (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this fs except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package fs + +import ( + "errors" + "os" + "path/filepath" + "sort" +) + +// readDirNames reads the directory named by dirname and returns +// a sorted list of directory entries. +func readDirNames(dirname string) ([]string, error) { + names, err := readDirUnsortedNames(dirname) + if err != nil { + return nil, err + } + sort.Strings(names) + return names, nil +} + +// getRealName - gets the proper filename for sorting purposes +// Readdir() filters out directory names without separators, add +// them back for proper sorting results. +func getRealName(info os.FileInfo) string { + if info.IsDir() { + // Make sure directory has its end separator. + return info.Name() + string(os.PathSeparator) + } + return info.Name() +} + +func readDirUnsortedNames(dirname string) ([]string, error) { + f, err := os.Open(dirname) + if err != nil { + return nil, err + } + nameInfos, err := f.Readdir(-1) + f.Close() + if err != nil { + return nil, err + } + var names []string + for _, nameInfo := range nameInfos { + names = append(names, getRealName(nameInfo)) + } + return names, nil +} + +// Walk walks the file tree rooted at root, calling walkFn for each file or +// directory in the tree, including root. +func Walk(root string, walkFn WalkFunc) error { + info, err := os.Lstat(root) + if err != nil { + return walkFn(root, nil, err) + } + return walk(root, info, walkFn) +} + +// WalkFunc is the type of the function called for each file or directory +// visited by Walk. The path argument contains the argument to Walk as a +// prefix; that is, if Walk is called with "dir", which is a directory +// containing the file "a", the walk function will be called with argument +// "dir/a". The info argument is the os.FileInfo for the named path. +type WalkFunc func(path string, info os.FileInfo, err error) error + +// ErrSkipDir is used as a return value from WalkFuncs to indicate that +// the directory named in the call is to be skipped. It is not returned +// as an error by any function. +var ErrSkipDir = errors.New("skip this directory") + +// ErrSkipFile is used as a return value from WalkFuncs to indicate that +// the file named in the call is to be skipped. It is not returned +// as an error by any function. +var ErrSkipFile = errors.New("skip this file") + +// walk recursively descends path, calling w. +func walk(path string, info os.FileInfo, walkFn WalkFunc) error { + err := walkFn(path, info, nil) + if err != nil { + if info.Mode().IsDir() && err == ErrSkipDir { + return nil + } + if info.Mode().IsRegular() && err == ErrSkipFile { + return nil + } + return err + } + if !info.IsDir() { + return nil + } + names, err := readDirNames(path) + if err != nil { + return walkFn(path, info, err) + } + for _, name := range names { + filename := filepath.Join(path, name) + fileInfo, err := os.Lstat(filename) + if err != nil { + if err := walkFn(filename, fileInfo, err); err != nil && err != ErrSkipDir && err != ErrSkipFile { + return err + } + } else { + err = walk(filename, fileInfo, walkFn) + if err != nil { + if err == ErrSkipDir || err == ErrSkipFile { + return nil + } + return err + } + } + } + return nil +} diff --git a/pkg/client/fs/fs.go b/pkg/client/fs/fs.go index 8d4941a4..4e25fb28 100644 --- a/pkg/client/fs/fs.go +++ b/pkg/client/fs/fs.go @@ -366,7 +366,7 @@ func (f *fsClient) listRecursiveInRoutine(contentCh chan client.ContentOnChannel if !strings.HasPrefix(fp, filePrefix) && !strings.HasPrefix(filePrefix, fp) { if fi.IsDir() { - return filepath.SkipDir + return ErrSkipDir } return nil } @@ -472,7 +472,7 @@ func (f *fsClient) listRecursiveInRoutine(contentCh chan client.ContentOnChannel return err } } - if fi.Mode().IsRegular() { + if fi.Mode().IsRegular() || fi.Mode().IsDir() { content := &client.Content{ URL: *client.NewURL(fp), Time: fi.ModTime(), @@ -502,7 +502,7 @@ func (f *fsClient) listRecursiveInRoutine(contentCh chan client.ContentOnChannel // filePrefix is kept for filtering incoming contents through WalkFunc. filePrefix = pathURL.Path } - err := filepath.Walk(dirName, visitFS) + err := Walk(dirName, visitFS) if err != nil { contentCh <- client.ContentOnChannel{ Content: nil, diff --git a/pkg/client/fs/fs_test.go b/pkg/client/fs/fs_test.go index c62a4ae8..64431a2a 100644 --- a/pkg/client/fs/fs_test.go +++ b/pkg/client/fs/fs_test.go @@ -117,15 +117,21 @@ func (s *MySuite) TestList(c *C) { } c.Assert(err, IsNil) - c.Assert(len(contents), Equals, 3) + c.Assert(len(contents), Equals, 5) var regularFiles int + var regularDirs int for _, content := range contents { if content.Type.IsRegular() { regularFiles++ continue } + if content.Type.IsDir() { + regularDirs++ + continue + } } + c.Assert(regularDirs, Equals, 2) c.Assert(regularFiles, Equals, 3) }