Skip to content

Commit

Permalink
fix: actually skip directories when SkipDir is returned by handler
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed Jan 22, 2025
1 parent 1434bf6 commit 70768e7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
22 changes: 21 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,18 +554,38 @@ func (c *client) Walk(dir string, fn fs.WalkDirFunc) error {
if w == nil {
return errors.New("nil *fs.Walker")
}

var skippedDirs []string

// Pass the callback to each file found
for w.Step() {
if err := w.Err(); err != nil {
return err
}

var skip bool
for _, sd := range skippedDirs {
matched := strings.HasPrefix(w.Path(), sd)
if matched {
skip = true
break
}
}
if skip {
w.SkipDir()
continue
}

info := w.Stat()
if info == nil || info.IsDir() {
if info == nil {
continue
}

err := fn(w.Path(), fs.FileInfoToDirEntry(info), w.Err())
if err != nil {
if err == fs.SkipDir {
skippedDirs = append(skippedDirs, filepath.Join(dir, ""))

w.SkipDir()
} else {
return err
Expand Down
32 changes: 25 additions & 7 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,20 @@ func TestClient(t *testing.T) {
if err != nil {
return err
}
if strings.Contains(path, "upload") {
return fs.SkipDir
}
walkedFiles = append(walkedFiles, path)
return nil
})
require.NoError(t, err)
require.ElementsMatch(t, walkedFiles, []string{
"root.txt",
"bigdata/large.txt",
"outbox/Upper/names.txt",
".", "root.txt",
"bigdata", "bigdata/large.txt",
"outbox", "outbox/Upper", "outbox/Upper/names.txt",
"outbox/one.txt", "outbox/two.txt", "outbox/empty.txt",
"outbox/archive/empty2.txt", "outbox/archive/three.txt",
"outbox/with-empty/EMPTY1.txt", "outbox/with-empty/empty_file2.txt",
"outbox/archive", "outbox/archive/empty2.txt", "outbox/archive/three.txt",
"outbox/with-empty", "outbox/with-empty/EMPTY1.txt", "outbox/with-empty/empty_file2.txt",
"outbox/with-empty/data.txt", "outbox/with-empty/data2.txt",
})
})
Expand All @@ -224,14 +227,29 @@ func TestClient(t *testing.T) {
})
require.NoError(t, err)
require.ElementsMatch(t, walkedFiles, []string{
"/outbox/Upper/names.txt",
"/outbox",
"/outbox/Upper", "/outbox/Upper/names.txt",
"/outbox/one.txt", "/outbox/two.txt", "/outbox/empty.txt",
"/outbox/archive/empty2.txt", "/outbox/archive/three.txt",
"/outbox/archive", "/outbox/archive/empty2.txt", "/outbox/archive/three.txt",
"/outbox/with-empty",
"/outbox/with-empty/EMPTY1.txt", "/outbox/with-empty/empty_file2.txt",
"/outbox/with-empty/data.txt", "/outbox/with-empty/data2.txt",
})
})

t.Run("Walk SkipDir", func(t *testing.T) {
var walkedFiles []string
err = client.Walk("/outbox", func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
walkedFiles = append(walkedFiles, path)
return fs.SkipDir
})
require.NoError(t, err)
require.ElementsMatch(t, walkedFiles, []string{"/outbox"})
})

t.Run("Upload and Delete", func(t *testing.T) {
// upload file
fileName := fmt.Sprintf("/upload/%d.txt", time.Now().Unix())
Expand Down

0 comments on commit 70768e7

Please sign in to comment.