Skip to content

Commit

Permalink
storage: local storage tolerates broken symlink when Walk (#51170) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Mar 25, 2024
1 parent fdbd093 commit cb2c200
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion br/pkg/storage/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ func (l *LocalStorage) WalkDir(_ context.Context, opt *WalkOption, fn func(strin
if !f.Mode().IsRegular() {
stat, err := os.Stat(filepath.Join(l.base, path))
if err != nil {
return errors.Trace(err)
// error may happen because of file deleted after walk started, or other errors
// like #49423. We just return 0 size and let the caller handle it in later
// logic.
log.Warn("failed to get file size", zap.String("path", path), zap.Error(err))
return fn(path, 0)
}
size = stat.Size()
}
Expand Down
20 changes: 20 additions & 0 deletions br/pkg/storage/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,23 @@ func TestLocalURI(t *testing.T) {
obtained := store.URI()
require.Equal(t, url, obtained)
}

func TestWalkBrokenSymLink(t *testing.T) {
ctx := context.Background()
dir := t.TempDir()
err := os.Symlink(filepath.Join(dir, "non-existing-file"), filepath.Join(dir, "file-that-should-be-ignored"))
require.NoError(t, err)

sb, err := ParseBackend("file://"+filepath.ToSlash(dir), nil)
require.NoError(t, err)
store, err := New(ctx, sb, nil)
require.NoError(t, err)

files := map[string]int64{}
err = store.WalkDir(ctx, nil, func(path string, size int64) error {
files[path] = size
return nil
})
require.NoError(t, err)
require.Equal(t, map[string]int64{"file-that-should-be-ignored": 0}, files)
}

0 comments on commit cb2c200

Please sign in to comment.