-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
129 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package hdfs | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
) | ||
|
||
// Walk walks the file tree rooted at root, calling walkFn for each file or | ||
// directory in the tree, including root. All errors that arise visiting files | ||
// and directories are filtered by walkFn. The files are walked in lexical | ||
// order, which makes the output deterministic but means that for very large | ||
// directories Walk can be inefficient. Walk does not follow symbolic links. | ||
func (c *Client) Walk(root string, walkFn filepath.WalkFunc) error { | ||
return c.walk(root, walkFn) | ||
} | ||
|
||
func (c *Client) walk(path string, walkFn filepath.WalkFunc) error { | ||
file, err := c.Open(path) | ||
var info os.FileInfo | ||
if file != nil { | ||
info = file.Stat() | ||
} | ||
|
||
err = walkFn(path, info, err) | ||
if err != nil { | ||
if info.IsDir() && err == filepath.SkipDir { | ||
return nil | ||
} | ||
|
||
return err | ||
} | ||
|
||
if info == nil || !info.IsDir() { | ||
return nil | ||
} | ||
|
||
names, err := file.Readdirnames(0) | ||
if err != nil { | ||
return walkFn(path, info, err) | ||
} | ||
|
||
sort.Strings(names) | ||
for _, name := range names { | ||
err = c.walk(filepath.Join(path, name), walkFn) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package hdfs | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWalk(t *testing.T) { | ||
c := getClient(t) | ||
|
||
c.Mkdir("/_test/walk", os.ModePerm) | ||
c.Mkdir("/_test/walk/dir", os.ModePerm) | ||
c.Mkdir("/_test/walk/dir/subdir", os.ModePerm) | ||
c.Create("/_test/walk/walkfile") | ||
c.Create("/_test/walk/dir/walkfile1") | ||
c.Create("/_test/walk/dir/walkfile2") | ||
c.Create("/_test/walk/dir/subdir/walkfile1") | ||
c.Create("/_test/walk/dir/subdir/walkfile2") | ||
|
||
paths := make([]string, 0, 8) | ||
|
||
err := c.Walk("/_test/walk/", walkFnTest(&paths)) | ||
assert.Nil(t, err, "unexpected error") | ||
|
||
expected := []string{ | ||
"/_test/walk/", | ||
"/_test/walk/dir", | ||
"/_test/walk/dir/subdir", | ||
"/_test/walk/dir/subdir/walkfile1", | ||
"/_test/walk/dir/subdir/walkfile2", | ||
"/_test/walk/dir/walkfile1", | ||
"/_test/walk/dir/walkfile2", | ||
"/_test/walk/walkfile"} | ||
|
||
assert.Equal(t, expected, paths, "discrepancy between expected and walked paths.") | ||
|
||
} | ||
|
||
func TestWalkError(t *testing.T) { | ||
c := getClient(t) | ||
errors := make([]error, 0, 1) | ||
c.Walk("/not_existing", walkErrorFn(&errors)) | ||
assert.Equal(t, 1, len(errors), "expected a single error") | ||
} | ||
|
||
func walkFnTest(encounteredPaths *[]string) filepath.WalkFunc { | ||
return func(path string, info os.FileInfo, err error) error { | ||
*encounteredPaths = append(*encounteredPaths, path) | ||
return nil | ||
} | ||
} | ||
|
||
func walkErrorFn(errors *[]error) filepath.WalkFunc { | ||
return func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
*errors = append(*errors, err) | ||
} | ||
return nil | ||
} | ||
} |