From 3b5dcd478a9506f02c2ea740330ca641dc31ca24 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 26 Jan 2016 13:16:34 -0800 Subject: [PATCH] ls: Make recursive and non-recursive behave the same way Outputs for recursive today ``` $ mc ls -r /tmp/value/ [2016-01-25 12:27:01 PST] 52B test.go [2016-01-26 11:36:22 PST] 4.2KiB test.txt [2016-01-26 11:45:22 PST] 102B test/ [2016-01-26 11:45:22 PST] 4.2KiB test/README.md ``` and Outputs for non recursive today differ lexically. ``` $ mc ls /tmp/value/ [2016-01-26 11:45:22 PST] 102B test/ [2016-01-25 12:27:01 PST] 52B test.go [2016-01-26 11:36:22 PST] 4.2KiB test.txt ``` To fix this issue we need to capture the ending '/' as well part of the names while sorting the output. This patch addresses this problem. --- access-main.go | 4 +- api_handlers_test.go | 7 +- appveyor.yml | 3 + cat-main.go | 8 +- config.go | 6 +- cp-main.go | 10 +- diff-main.go | 4 +- error.go | 12 +- mb-main.go | 4 +- mirror-main.go | 6 +- notifier.go | 6 +- pkg/client/fs/fs-utils.go | 113 ++++++++++++------ pkg/client/fs/fs.go | 10 +- pkg/client/fs/{fs_unix.go => path-utils.go} | 2 +- .../{fs_windows.go => path-utils_windows.go} | 0 pkg/client/s3/s3_test.go | 7 +- scan-bar.go | 6 +- session-main.go | 4 +- session-v6.go | 13 +- session.go | 4 +- share-db-v1.go | 4 +- share.go | 4 +- update-main.go | 4 +- version_test.go | 4 +- 24 files changed, 142 insertions(+), 103 deletions(-) rename pkg/client/fs/{fs_unix.go => path-utils.go} (90%) rename pkg/client/fs/{fs_windows.go => path-utils_windows.go} (100%) diff --git a/access-main.go b/access-main.go index 425fb7d4..006516cb 100644 --- a/access-main.go +++ b/access-main.go @@ -96,8 +96,8 @@ func (s accessMessage) String() string { // JSON jsonified access message. func (s accessMessage) JSON() string { - accessJSONBytes, err := json.Marshal(s) - fatalIf(probe.NewError(err), "Unable to marshal into JSON.") + accessJSONBytes, e := json.Marshal(s) + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(accessJSONBytes) } diff --git a/api_handlers_test.go b/api_handlers_test.go index 8ea5e3bc..1cb4ff36 100644 --- a/api_handlers_test.go +++ b/api_handlers_test.go @@ -138,14 +138,13 @@ func (h objectAPIHandler) putHandler(w http.ResponseWriter, r *http.Request) { return } - length, err := strconv.Atoi(r.Header.Get("Content-Length")) - if err != nil { + length, e := strconv.Atoi(r.Header.Get("Content-Length")) + if e != nil { w.WriteHeader(http.StatusBadRequest) return } var buffer bytes.Buffer - _, err = io.CopyN(&buffer, r.Body, int64(length)) - if err != nil { + if _, e = io.CopyN(&buffer, r.Body, int64(length)); e != nil { w.WriteHeader(http.StatusInternalServerError) return } diff --git a/appveyor.yml b/appveyor.yml index 2595bbfb..17a271e1 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -20,6 +20,8 @@ install: - go get -u golang.org/x/tools/cmd/vet - go get -u github.com/fzipp/gocyclo - go get -u github.com/remyoudompheng/go-misc/deadcode + - go get -u github.com/alecthomas/gometalinter + - go get -u github.com/client9/misspell # to run your custom scripts instead of automatic MSBuild build_script: @@ -28,6 +30,7 @@ build_script: - gofmt -s -l pkg - golint . - golint github.com/minio/mc/pkg... + - gometalinter --disable-all --linter='misspell:misspell ./*.go:PATH:LINE:COL:MESSAGE' --enable=misspell ./... - gocyclo -over 40 pkg - deadcode - go test -race . diff --git a/cat-main.go b/cat-main.go index 137de275..f00a42d2 100644 --- a/cat-main.go +++ b/cat-main.go @@ -98,16 +98,16 @@ func catURL(sourceURL string) *probe.Error { // catOut reads from reader stream and writes to stdout. func catOut(r io.Reader) *probe.Error { // Read till EOF. - if _, err := io.Copy(os.Stdout, r); err != nil { - switch e := err.(type) { + if _, e := io.Copy(os.Stdout, r); e != nil { + switch e := e.(type) { case *os.PathError: if e.Err == syscall.EPIPE { // stdout closed by the user. Gracefully exit. return nil } - return probe.NewError(err) + return probe.NewError(e) default: - return probe.NewError(err) + return probe.NewError(e) } } return nil diff --git a/config.go b/config.go index 347de14e..fbd7813d 100644 --- a/config.go +++ b/config.go @@ -69,8 +69,8 @@ func createMcConfigDir() *probe.Error { if err != nil { return err.Trace() } - if err := os.MkdirAll(p, 0700); err != nil { - return probe.NewError(err) + if e := os.MkdirAll(p, 0700); e != nil { + return probe.NewError(e) } return nil } @@ -142,7 +142,7 @@ func isMcConfigExists() bool { if err != nil { return false } - if _, err := os.Stat(configFile); err != nil { + if _, e := os.Stat(configFile); e != nil { return false } return true diff --git a/cp-main.go b/cp-main.go index f2b3edb1..417e47ec 100644 --- a/cp-main.go +++ b/cp-main.go @@ -100,8 +100,8 @@ func (c copyMessage) String() string { // JSON jsonified copy message func (c copyMessage) JSON() string { c.Status = "success" - copyMessageBytes, err := json.Marshal(c) - fatalIf(probe.NewError(err), "Failed to marshal copy message.") + copyMessageBytes, e := json.Marshal(c) + fatalIf(probe.NewError(e), "Failed to marshal copy message.") return string(copyMessageBytes) } @@ -226,10 +226,10 @@ func doPrepareCopyURLs(session *sessionV6, trapCh <-chan bool) { break } - jsonData, err := json.Marshal(cpURLs) - if err != nil { + jsonData, e := json.Marshal(cpURLs) + if e != nil { session.Delete() - fatalIf(probe.NewError(err), "Unable to prepare URL for copying. Error in JSON marshaling.") + fatalIf(probe.NewError(e), "Unable to prepare URL for copying. Error in JSON marshaling.") } fmt.Fprintln(dataFP, string(jsonData)) if !globalQuiet && !globalJSON { diff --git a/diff-main.go b/diff-main.go index 0a768ee5..5bcdd05f 100644 --- a/diff-main.go +++ b/diff-main.go @@ -99,8 +99,8 @@ func (d diffMessage) String() string { // JSON jsonified diff message func (d diffMessage) JSON() string { d.Status = "success" - diffJSONBytes, err := json.Marshal(d) - fatalIf(probe.NewError(err), + diffJSONBytes, e := json.Marshal(d) + fatalIf(probe.NewError(e), "Unable to marshal diff message ‘"+d.FirstURL+"’, ‘"+d.SecondURL+"’ and ‘"+d.Diff+"’.") return string(diffJSONBytes) } diff --git a/error.go b/error.go index 1dce6c8e..a7e78cf5 100644 --- a/error.go +++ b/error.go @@ -57,15 +57,15 @@ func fatalIf(err *probe.Error, msg string) { if globalDebug { errorMsg.CallTrace = err.CallTrace } - json, err := json.Marshal(struct { + json, e := json.Marshal(struct { Status string `json:"status"` Error errorMessage `json:"error"` }{ Status: "error", Error: errorMsg, }) - if err != nil { - console.Fatalln(probe.NewError(err)) + if e != nil { + console.Fatalln(probe.NewError(e)) } console.Println(string(json)) console.Fatalln() @@ -94,15 +94,15 @@ func errorIf(err *probe.Error, msg string) { if globalDebug { errorMsg.CallTrace = err.CallTrace } - json, err := json.Marshal(struct { + json, e := json.Marshal(struct { Status string `json:"status"` Error errorMessage `json:"error"` }{ Status: "error", Error: errorMsg, }) - if err != nil { - console.Fatalln(probe.NewError(err)) + if e != nil { + console.Fatalln(probe.NewError(e)) } console.Println(string(json)) return diff --git a/mb-main.go b/mb-main.go index 498bc52d..bba036d5 100644 --- a/mb-main.go +++ b/mb-main.go @@ -83,8 +83,8 @@ func (s makeBucketMessage) String() string { // JSON jsonified make bucket message. func (s makeBucketMessage) JSON() string { - makeBucketJSONBytes, err := json.Marshal(s) - fatalIf(probe.NewError(err), "Unable to marshal into JSON.") + makeBucketJSONBytes, e := json.Marshal(s) + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(makeBucketJSONBytes) } diff --git a/mirror-main.go b/mirror-main.go index a3340778..dadaae4d 100644 --- a/mirror-main.go +++ b/mirror-main.go @@ -207,10 +207,10 @@ func doPrepareMirrorURLs(session *sessionV6, isForce bool, trapCh <-chan bool) { if sURLs.isEmpty() { break } - jsonData, err := json.Marshal(sURLs) - if err != nil { + jsonData, e := json.Marshal(sURLs) + if e != nil { session.Delete() - fatalIf(probe.NewError(err), "Unable to marshal URLs into JSON.") + fatalIf(probe.NewError(e), "Unable to marshal URLs into JSON.") } fmt.Fprintln(dataFP, string(jsonData)) if !globalQuiet && !globalJSON { diff --git a/notifier.go b/notifier.go index 59e1ba28..57806f60 100644 --- a/notifier.go +++ b/notifier.go @@ -50,9 +50,9 @@ func colorizeUpdateMessage(updateString string) (string, *probe.Error) { line2Rest := maxContentWidth - line2Length // Fetch terminal size to calculate optimal box size. - terminal, err := ts.GetSize() - if err != nil { - return "", probe.NewError(err) + terminal, e := ts.GetSize() + if e != nil { + return "", probe.NewError(e) } var message string diff --git a/pkg/client/fs/fs-utils.go b/pkg/client/fs/fs-utils.go index c56e7754..836a3dd4 100644 --- a/pkg/client/fs/fs-utils.go +++ b/pkg/client/fs/fs-utils.go @@ -23,12 +23,47 @@ import ( "sort" ) +// byDirName implements sort.Interface. +type byDirName []os.FileInfo + +func (f byDirName) Len() int { return len(f) } +func (f byDirName) Less(i, j int) bool { + // For directory add an ending separator fortrue lexical order. + if f[i].Mode().IsDir() { + return f[i].Name()+string(os.PathSeparator) < f[j].Name() + } + // For directory add an ending separator for true lexical order. + if f[j].Mode().IsDir() { + return f[i].Name() < f[j].Name()+string(os.PathSeparator) + } + return f[i].Name() < f[j].Name() +} +func (f byDirName) Swap(i, j int) { f[i], f[j] = f[j], f[i] } + +// readDir reads the directory named by dirname and returns +// a list of sorted directory entries. +func readDir(dirname string) ([]os.FileInfo, error) { + f, e := os.Open(dirname) + if e != nil { + return nil, e + } + list, e := f.Readdir(-1) + if e != nil { + return nil, e + } + if e = f.Close(); e != nil { + return nil, e + } + sort.Sort(byDirName(list)) + return list, nil +} + // 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 + names, e := readDirUnsortedNames(dirname) + if e != nil { + return nil, e } sort.Strings(names) return names, nil @@ -45,15 +80,19 @@ func getRealName(info os.FileInfo) string { return info.Name() } +// readDirUnsortedNames reads the directory named by dirname and +// return a unsorted list of directory entries. func readDirUnsortedNames(dirname string) ([]string, error) { - f, err := os.Open(dirname) - if err != nil { - return nil, err + f, e := os.Open(dirname) + if e != nil { + return nil, e } - nameInfos, err := f.Readdir(-1) - f.Close() - if err != nil { - return nil, err + nameInfos, e := f.Readdir(-1) + if e != nil { + return nil, e + } + if e = f.Close(); e != nil { + return nil, e } var names []string for _, nameInfo := range nameInfos { @@ -62,14 +101,14 @@ func readDirUnsortedNames(dirname string) ([]string, error) { return names, nil } -// Walk walks the file tree rooted at root, calling walkFn for each file or +// 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) +func walk(root string, walkFn walkFunc) error { + info, e := os.Lstat(root) + if e != nil { + return walkFn(root, nil, e) } - return walk(root, info, walkFn) + return walkInternal(root, info, walkFn) } // WalkFunc is the type of the function called for each file or directory @@ -77,51 +116,51 @@ func Walk(root string, walkFn WalkFunc) error { // 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 +type walkFunc func(path string, info os.FileInfo, e 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") +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") +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 { +// walkInternal recursively descends path, calling w. +func walkInternal(path string, info os.FileInfo, walkFn walkFunc) error { + e := walkFn(path, info, nil) + if e != nil { + if info.Mode().IsDir() && e == errSkipDir { return nil } - if info.Mode().IsRegular() && err == ErrSkipFile { + if info.Mode().IsRegular() && e == errSkipFile { return nil } - return err + return e } if !info.IsDir() { return nil } - names, err := readDirNames(path) - if err != nil { - return walkFn(path, info, err) + names, e := readDirNames(path) + if e != nil { + return walkFn(path, info, e) } 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 + fileInfo, e := os.Lstat(filename) + if e != nil { + if e = walkFn(filename, fileInfo, e); e != nil && e != errSkipDir && e != errSkipFile { + return e } } else { - err = walk(filename, fileInfo, walkFn) - if err != nil { - if err == ErrSkipDir || err == ErrSkipFile { + e = walkInternal(filename, fileInfo, walkFn) + if e != nil { + if e == errSkipDir || e == errSkipFile { return nil } - return err + return e } } } diff --git a/pkg/client/fs/fs.go b/pkg/client/fs/fs.go index 7b2caf37..1f1d03b2 100644 --- a/pkg/client/fs/fs.go +++ b/pkg/client/fs/fs.go @@ -280,7 +280,7 @@ func (f *fsClient) List(recursive, incomplete bool) <-chan *client.Content { // listPrefixes - list all files for any given prefix. func (f *fsClient) listPrefixes(prefix string, contentCh chan<- *client.Content, incomplete bool) { dirName := filepath.Dir(prefix) - files, e := ioutil.ReadDir(dirName) + files, e := readDir(dirName) if e != nil { err := f.toClientError(e, dirName) contentCh <- &client.Content{ @@ -425,7 +425,7 @@ func (f *fsClient) listInRoutine(contentCh chan<- *client.Content, incomplete bo // If we really see the directory. switch fst.Mode().IsDir() { case true: - files, e := ioutil.ReadDir(fpath) + files, e := readDir(fpath) if err != nil { contentCh <- &client.Content{Err: probe.NewError(e)} return @@ -555,7 +555,7 @@ func (f *fsClient) listRecursiveInRoutine(contentCh chan *client.Content, incomp !strings.HasPrefix(filePrefix, fp) { if e == nil { if fi.IsDir() { - return ErrSkipDir + return errSkipDir } return nil } @@ -702,8 +702,8 @@ func (f *fsClient) listRecursiveInRoutine(contentCh chan *client.Content, incomp // filePrefix is kept for filtering incoming contents through WalkFunc. filePrefix = pathURL.Path } - // Walks invokes our custom function. - e := Walk(dirName, visitFS) + // walks invokes our custom function. + e := walk(dirName, visitFS) if e != nil { contentCh <- &client.Content{ Err: probe.NewError(e), diff --git a/pkg/client/fs/fs_unix.go b/pkg/client/fs/path-utils.go similarity index 90% rename from pkg/client/fs/fs_unix.go rename to pkg/client/fs/path-utils.go index b4be9ea5..262acac7 100644 --- a/pkg/client/fs/fs_unix.go +++ b/pkg/client/fs/path-utils.go @@ -1,4 +1,4 @@ -// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris +// +build !windows /* * Minio Client (C) 2015 Minio, Inc. diff --git a/pkg/client/fs/fs_windows.go b/pkg/client/fs/path-utils_windows.go similarity index 100% rename from pkg/client/fs/fs_windows.go rename to pkg/client/fs/path-utils_windows.go diff --git a/pkg/client/s3/s3_test.go b/pkg/client/s3/s3_test.go index 978c6435..1d3fdbb5 100644 --- a/pkg/client/s3/s3_test.go +++ b/pkg/client/s3/s3_test.go @@ -91,14 +91,13 @@ func (h objectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch { case r.Method == "PUT": // Handler for PUT object request. - length, err := strconv.Atoi(r.Header.Get("Content-Length")) - if err != nil { + length, e := strconv.Atoi(r.Header.Get("Content-Length")) + if e != nil { w.WriteHeader(http.StatusBadRequest) return } var buffer bytes.Buffer - _, err = io.CopyN(&buffer, r.Body, int64(length)) - if err != nil { + if _, e = io.CopyN(&buffer, r.Body, int64(length)); e != nil { w.WriteHeader(http.StatusInternalServerError) return } diff --git a/scan-bar.go b/scan-bar.go index 2818fe08..10e150ce 100644 --- a/scan-bar.go +++ b/scan-bar.go @@ -47,9 +47,9 @@ func scanBarFactory() scanBarFunc { prevLineSize := 0 prevSource := "" fileCount := 0 - termSize, err := ts.GetSize() - if err != nil { - fatalIf(probe.NewError(err), "Unable to get terminal size. Please use --quiet option.") + termSize, e := ts.GetSize() + if e != nil { + fatalIf(probe.NewError(e), "Unable to get terminal size. Please use --quiet option.") } termWidth := termSize.Col() cursorCh := cursorAnimate() diff --git a/session-main.go b/session-main.go index 03c70927..be75e1d1 100644 --- a/session-main.go +++ b/session-main.go @@ -115,8 +115,8 @@ func (c clearSessionMessage) String() string { // JSON jsonified clear session message. func (c clearSessionMessage) JSON() string { - clearSessionJSONBytes, err := json.Marshal(c) - fatalIf(probe.NewError(err), "Unable to marshal into JSON.") + clearSessionJSONBytes, e := json.Marshal(c) + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(clearSessionJSONBytes) } diff --git a/session-v6.go b/session-v6.go index fa9537ac..62960b41 100644 --- a/session-v6.go +++ b/session-v6.go @@ -137,8 +137,8 @@ func loadSessionV6(sid string) (*sessionV6, *probe.Error) { return nil, err.Trace(sid) } - if _, err := os.Stat(sessionFile); err != nil { - return nil, probe.NewError(err) + if _, e := os.Stat(sessionFile); e != nil { + return nil, probe.NewError(e) } s := &sessionV6{} @@ -300,9 +300,8 @@ func (s *sessionV6) Delete() *probe.Error { // the file is closed already s.DataFP.Close() - err := os.Remove(name) - if err != nil { - return probe.NewError(err) + if e := os.Remove(name); e != nil { + return probe.NewError(e) } } @@ -311,8 +310,8 @@ func (s *sessionV6) Delete() *probe.Error { return err.Trace(s.SessionID) } - if err := os.Remove(sessionFile); err != nil { - return probe.NewError(err) + if e := os.Remove(sessionFile); e != nil { + return probe.NewError(e) } return nil diff --git a/session.go b/session.go index 3be11503..04bcac52 100644 --- a/session.go +++ b/session.go @@ -37,8 +37,8 @@ func createSessionDir() *probe.Error { return err.Trace() } - if err := os.MkdirAll(sessionDir, 0700); err != nil { - return probe.NewError(err) + if e := os.MkdirAll(sessionDir, 0700); e != nil { + return probe.NewError(e) } return nil } diff --git a/share-db-v1.go b/share-db-v1.go index 4ddf90be..bfcaa9de 100644 --- a/share-db-v1.go +++ b/share-db-v1.go @@ -89,8 +89,8 @@ func (s *shareDBV1) Load(filename string) *probe.Error { defer s.mutex.Unlock() // Check if the db file exist. - if _, err := os.Stat(filename); err != nil { - return probe.NewError(err) + if _, e := os.Stat(filename); e != nil { + return probe.NewError(e) } // Initialize and load using quick package. diff --git a/share.go b/share.go index 128f9d7e..079523b8 100644 --- a/share.go +++ b/share.go @@ -130,8 +130,8 @@ func isShareDirExists() bool { // Create config share dir. func createShareDir() *probe.Error { - if err := os.MkdirAll(mustGetShareDir(), 0700); err != nil { - return probe.NewError(err) + if e := os.MkdirAll(mustGetShareDir(), 0700); e != nil { + return probe.NewError(e) } return nil } diff --git a/update-main.go b/update-main.go index b106a943..13aa4c93 100644 --- a/update-main.go +++ b/update-main.go @@ -102,8 +102,8 @@ func (u updateMessage) String() string { // JSON jsonified update message. func (u updateMessage) JSON() string { u.Status = "success" - updateMessageJSONBytes, err := json.Marshal(u) - fatalIf(probe.NewError(err), "Unable to marshal into JSON.") + updateMessageJSONBytes, e := json.Marshal(u) + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(updateMessageJSONBytes) } diff --git a/version_test.go b/version_test.go index b79bf22c..f6d83504 100644 --- a/version_test.go +++ b/version_test.go @@ -24,6 +24,6 @@ import ( ) func (s *TestSuite) TestVersion(c *C) { - _, err := time.Parse(mcVersion, http.TimeFormat) - c.Assert(err, NotNil) + _, e := time.Parse(mcVersion, http.TimeFormat) + c.Assert(e, NotNil) }