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
close #49423
  • Loading branch information
lance6716 authored Feb 20, 2024
1 parent 58e3735 commit 1fc92b3
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 @@ -130,7 +130,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 @@ -237,3 +237,23 @@ func TestLocalFileReadRange(t *testing.T) {
n, _ := r.Read(smallBuf)
require.Equal(t, "23", string(smallBuf[:n]))
}

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 1fc92b3

Please sign in to comment.