From 9599336b950965562a3c6b1e7fe397ad402ebc2c Mon Sep 17 00:00:00 2001 From: Colin Marc Date: Thu, 23 Oct 2014 15:58:00 +0200 Subject: [PATCH] fix some bugs in ls - flush files before the dir results - make sure we still filter cached readDir results --- cmd/hdfs/cache.go | 16 ++-------------- cmd/hdfs/ls.go | 9 +++++---- cmd/hdfs/paths.go | 15 ++++++++++----- 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/cmd/hdfs/cache.go b/cmd/hdfs/cache.go index 1d045ce..cd63dcb 100644 --- a/cmd/hdfs/cache.go +++ b/cmd/hdfs/cache.go @@ -50,7 +50,7 @@ func stat(client *hdfs.Client, fullPath string) (os.FileInfo, error) { return res, nil } -func readDir(client *hdfs.Client, dir string, glob string) ([]os.FileInfo, error) { +func readDir(client *hdfs.Client, dir string) ([]os.FileInfo, error) { if cachedRes, exists := readDirCache[dir]; exists { return cachedRes, nil } @@ -66,17 +66,5 @@ func readDir(client *hdfs.Client, dir string, glob string) ([]os.FileInfo, error statCache[childPath] = fi } - if glob != "" { - matched := make([]os.FileInfo, 0, len(res)) - for _, fi := range res { - match, _ := path.Match(glob, fi.Name()) - if match { - matched = append(matched, fi) - } - } - - return matched, nil - } else { - return res, nil - } + return res, nil } diff --git a/cmd/hdfs/ls.go b/cmd/hdfs/ls.go index 5d236dc..c1f9151 100644 --- a/cmd/hdfs/ls.go +++ b/cmd/hdfs/ls.go @@ -47,11 +47,12 @@ func ls(paths []string, long, all bool) { var tw *tabwriter.Writer if long { tw = defaultTabWriter() - defer tw.Flush() + printFiles(tw, files, true, all) + tw.Flush() + } else { + printFiles(nil, files, false, all) } - printFiles(tw, files, long, all) - for _, dir := range dirs { fmt.Printf("\n%s/:\n", dir) printDir(client, dir, long, all) @@ -60,7 +61,7 @@ func ls(paths []string, long, all bool) { } func printDir(client *hdfs.Client, dir string, long, all bool) { - files, err := readDir(client, dir, "") + files, err := readDir(client, dir) if err != nil { fatal(err) } diff --git a/cmd/hdfs/paths.go b/cmd/hdfs/paths.go index 9b7531b..507585f 100644 --- a/cmd/hdfs/paths.go +++ b/cmd/hdfs/paths.go @@ -65,12 +65,12 @@ func hasGlob(fragment string) bool { // expandGlobs recursively expands globs in a filepath. It assumes the paths // are already cleaned and normalize (ie, absolute). -func expandGlobs(client *hdfs.Client, p string) ([]string, error) { - if !hasGlob(p) { - return []string{p}, nil +func expandGlobs(client *hdfs.Client, globbedPath string) ([]string, error) { + if !hasGlob(globbedPath) { + return []string{globbedPath}, nil } - parts := strings.Split(p, "/")[1:] + parts := strings.Split(globbedPath, "/")[1:] res := make([]string, 0) splitAt := 0 for splitAt, _ = range parts { @@ -82,12 +82,17 @@ func expandGlobs(client *hdfs.Client, p string) ([]string, error) { base := "/" + path.Join(parts[:splitAt]...) glob := parts[splitAt] remainder := path.Join(parts[splitAt+1:]...) - list, err := readDir(client, base, glob) + list, err := readDir(client, base) if err != nil { return nil, err } for _, fi := range list { + match, _ := path.Match(glob, fi.Name()) + if !match { + continue + } + newPath := path.Join(base, fi.Name(), remainder) children, err := expandGlobs(client, newPath) if err != nil {