feat(vfs): Walk, RemoveTree, DirSize, SameFile and Within helpers (2A.2)
One implementation for every FS. Walk and RemoveTree never follow symlinks, so deleting a link to a directory removes only the link (N-P0-3). DirSize returns the root error and counts unreadable subdirectories instead of reporting a silent 0 B (N-P1-14). SameFile compares inodes locally and RealPath on other volumes, so an alias directory is recognised (N-P0-6); Within resolves symlinks on both sides and accepts a not-yet-existing child. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
c3de5a06ef
commit
5132daa292
5 changed files with 517 additions and 0 deletions
268
internal/vfs/helpers_test.go
Normal file
268
internal/vfs/helpers_test.go
Normal file
|
|
@ -0,0 +1,268 @@
|
||||||
|
package vfs_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"vcom/internal/testutil/fstree"
|
||||||
|
"vcom/internal/vfs"
|
||||||
|
"vcom/internal/vfs/localfs"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ctx = context.Background()
|
||||||
|
|
||||||
|
func TestRemoveTreeSymlinkToDirKeepsTarget(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
fstree.Build(t, dir, fstree.Spec{
|
||||||
|
fstree.File("data/precious", "keep me"),
|
||||||
|
fstree.File("data/sub/also", "keep"),
|
||||||
|
fstree.Symlink("link", "data"),
|
||||||
|
fstree.File("tree/f", "x"),
|
||||||
|
fstree.Symlink("tree/inner", "../data"),
|
||||||
|
})
|
||||||
|
fsys := localfs.New()
|
||||||
|
|
||||||
|
// Root is a symlink to a directory: only the link goes.
|
||||||
|
if err := vfs.RemoveTree(ctx, fsys, filepath.Join(dir, "link"), nil); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// A tree containing a link to a directory: the link is unlinked, not walked.
|
||||||
|
if err := vfs.RemoveTree(ctx, fsys, filepath.Join(dir, "tree"), nil); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
fstree.Equal(t, fstree.FromSpec(fstree.Spec{
|
||||||
|
fstree.File("data/precious", "keep me"),
|
||||||
|
fstree.File("data/sub/also", "keep"),
|
||||||
|
}), fstree.Snapshot(t, dir), fstree.Opts{Links: true})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRemoveTreeCancel(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
var spec fstree.Spec
|
||||||
|
for i := range 20 {
|
||||||
|
spec = append(spec, fstree.File(fmt.Sprintf("big/f%02d", i), "x"))
|
||||||
|
}
|
||||||
|
fstree.Build(t, dir, spec)
|
||||||
|
c, cancel := context.WithCancel(ctx)
|
||||||
|
defer cancel()
|
||||||
|
removed := 0
|
||||||
|
err := vfs.RemoveTree(c, localfs.New(), filepath.Join(dir, "big"), func(n int) {
|
||||||
|
removed = n
|
||||||
|
if n == 3 {
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if !errors.Is(err, vfs.ErrCancelled) {
|
||||||
|
t.Fatalf("err = %v, want ErrCancelled", err)
|
||||||
|
}
|
||||||
|
if removed != 3 {
|
||||||
|
t.Fatalf("removed %d after cancel, want 3", removed)
|
||||||
|
}
|
||||||
|
left, _ := os.ReadDir(filepath.Join(dir, "big"))
|
||||||
|
if len(left) != 17 {
|
||||||
|
t.Fatalf("%d entries left, want 17", len(left))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRemoveTreeProgressAndNonEmpty(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
fstree.Build(t, dir, fstree.Spec{fstree.File("a/b/c", ""), fstree.File("a/d", "")})
|
||||||
|
var last int
|
||||||
|
if err := vfs.RemoveTree(ctx, localfs.New(), filepath.Join(dir, "a"), func(n int) { last = n }); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if last != 4 { // c, b, d, a
|
||||||
|
t.Fatalf("progress = %d, want 4", last)
|
||||||
|
}
|
||||||
|
if _, err := os.Lstat(filepath.Join(dir, "a")); !errors.Is(err, os.ErrNotExist) {
|
||||||
|
t.Fatal("tree still there")
|
||||||
|
}
|
||||||
|
if err := vfs.RemoveTree(ctx, localfs.New(), filepath.Join(dir, "missing"), nil); !errors.Is(err, vfs.ErrNotExist) {
|
||||||
|
t.Fatalf("missing root: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDirSizeRootError(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
_, err := vfs.DirSize(ctx, localfs.New(), filepath.Join(dir, "missing"), nil)
|
||||||
|
if !errors.Is(err, vfs.ErrNotExist) {
|
||||||
|
t.Fatalf("missing root: err = %v, want ErrNotExist (not a silent 0 B)", err)
|
||||||
|
}
|
||||||
|
if os.Geteuid() != 0 {
|
||||||
|
fstree.Build(t, dir, fstree.Spec{fstree.File("locked/f", "x")})
|
||||||
|
lock(t, filepath.Join(dir, "locked"))
|
||||||
|
if _, err := vfs.DirSize(ctx, localfs.New(), filepath.Join(dir, "locked"), nil); !errors.Is(err, vfs.ErrPermission) {
|
||||||
|
t.Fatalf("unreadable root: err = %v, want ErrPermission", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c, cancel := context.WithCancel(ctx)
|
||||||
|
cancel()
|
||||||
|
if _, err := vfs.DirSize(c, localfs.New(), dir, nil); !errors.Is(err, vfs.ErrCancelled) {
|
||||||
|
t.Fatalf("cancelled: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// lock makes dir unreadable (mode 0) until the test ends.
|
||||||
|
func lock(t *testing.T, dir string) {
|
||||||
|
t.Helper()
|
||||||
|
if err := os.Chmod(dir, 0); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { _ = os.Chmod(dir, 0o755) })
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDirSizeCountsUnreadable(t *testing.T) {
|
||||||
|
if os.Geteuid() == 0 {
|
||||||
|
t.Skip("root reads mode-0 directories")
|
||||||
|
}
|
||||||
|
dir := t.TempDir()
|
||||||
|
fstree.Build(t, dir, fstree.Spec{
|
||||||
|
fstree.File("a.bin", "").WithSize(100),
|
||||||
|
fstree.File("ok/b.bin", "").WithSize(20),
|
||||||
|
fstree.File("locked/hidden", "").WithSize(1000),
|
||||||
|
fstree.Symlink("ln", "a.bin"),
|
||||||
|
})
|
||||||
|
lock(t, filepath.Join(dir, "locked"))
|
||||||
|
var calls int
|
||||||
|
r, err := vfs.DirSize(ctx, localfs.New(), dir, func(vfs.SizeResult) { calls++ })
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
want := vfs.SizeResult{Bytes: 100 + 20 + int64(len("a.bin")), Files: 3, Dirs: 2, Unreadable: 1}
|
||||||
|
if r != want {
|
||||||
|
t.Fatalf("got %+v, want %+v", r, want)
|
||||||
|
}
|
||||||
|
if calls == 0 {
|
||||||
|
t.Fatal("no progress")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSameFileSymlinkAlias(t *testing.T) {
|
||||||
|
home := t.TempDir()
|
||||||
|
fstree.Build(t, home, fstree.Spec{
|
||||||
|
fstree.File("real/f.txt", "data"),
|
||||||
|
fstree.File("other/f.txt", "data"),
|
||||||
|
fstree.Symlink("alias", "real"),
|
||||||
|
})
|
||||||
|
fsys := localfs.New()
|
||||||
|
same, err := vfs.SameFile(ctx, fsys, filepath.Join(home, "real", "f.txt"), fsys, filepath.Join(home, "alias", "f.txt"))
|
||||||
|
if err != nil || !same {
|
||||||
|
t.Fatalf("~/real/f.txt vs ~/alias/f.txt: same=%v err=%v, want true (N-P0-6)", same, err)
|
||||||
|
}
|
||||||
|
same, err = vfs.SameFile(ctx, fsys, filepath.Join(home, "real"), fsys, filepath.Join(home, "alias"))
|
||||||
|
if err != nil || !same {
|
||||||
|
t.Fatalf("dir vs its alias: %v %v", same, err)
|
||||||
|
}
|
||||||
|
same, err = vfs.SameFile(ctx, fsys, filepath.Join(home, "real", "f.txt"), fsys, filepath.Join(home, "other", "f.txt"))
|
||||||
|
if err != nil || same {
|
||||||
|
t.Fatalf("equal content is not identity: %v %v", same, err)
|
||||||
|
}
|
||||||
|
same, err = vfs.SameFile(ctx, fsys, filepath.Join(home, "real", "f.txt"), fsys, filepath.Join(home, "nope"))
|
||||||
|
if err != nil || same {
|
||||||
|
t.Fatalf("missing: %v %v", same, err)
|
||||||
|
}
|
||||||
|
// Different volume is never the same, even with an identical path.
|
||||||
|
ro := otherVolume{vfs.ReadOnly(fsys)}
|
||||||
|
same, err = vfs.SameFile(ctx, fsys, filepath.Join(home, "real"), ro, filepath.Join(home, "real"))
|
||||||
|
if err != nil || same {
|
||||||
|
t.Fatalf("different volumes: %v %v", same, err)
|
||||||
|
}
|
||||||
|
// Same volume, non-local kind: RealPath comparison.
|
||||||
|
rp := realPathOnly{vfs.ReadOnly(fsys)}
|
||||||
|
same, err = vfs.SameFile(ctx, rp, filepath.Join(home, "real", "f.txt"), rp, filepath.Join(home, "alias", "f.txt"))
|
||||||
|
if err != nil || !same {
|
||||||
|
t.Fatalf("realpath comparison: %v %v", same, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type otherVolume struct{ vfs.FS }
|
||||||
|
|
||||||
|
func (otherVolume) Volume() vfs.VolumeID { return "other" }
|
||||||
|
|
||||||
|
type realPathOnly struct{ vfs.FS }
|
||||||
|
|
||||||
|
func (realPathOnly) Kind() vfs.Kind { return vfs.KindSFTP }
|
||||||
|
|
||||||
|
func TestWithinViaSymlink(t *testing.T) {
|
||||||
|
home := t.TempDir()
|
||||||
|
fstree.Build(t, home, fstree.Spec{
|
||||||
|
fstree.Dir("src/sub"),
|
||||||
|
fstree.Dir("srcsibling"),
|
||||||
|
fstree.Symlink("alias", "src"),
|
||||||
|
fstree.Symlink("sublink", "src/sub"),
|
||||||
|
})
|
||||||
|
fsys := localfs.New()
|
||||||
|
j := func(p ...string) string { return filepath.Join(append([]string{home}, p...)...) }
|
||||||
|
cases := []struct {
|
||||||
|
child, parent string
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{j("alias", "sub"), j("src"), true}, // through an alias of the parent
|
||||||
|
{j("sublink"), j("src"), true}, // a link pointing inside
|
||||||
|
{j("src"), j("alias"), true}, // equal after resolution
|
||||||
|
{j("alias", "sub", "new", "deeper"), j("src"), true}, // child does not exist yet
|
||||||
|
{j("srcsibling"), j("src"), false}, // prefix is not containment
|
||||||
|
{j("src"), j("src", "sub"), false},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
got, err := vfs.Within(ctx, fsys, c.child, c.parent)
|
||||||
|
if err != nil || got != c.want {
|
||||||
|
t.Errorf("Within(%s, %s) = %v, %v; want %v",
|
||||||
|
strings.TrimPrefix(c.child, home), strings.TrimPrefix(c.parent, home), got, err, c.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWalkDoesNotFollowLinks(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
fstree.Build(t, dir, fstree.Spec{
|
||||||
|
fstree.File("root/b", ""),
|
||||||
|
fstree.File("root/a/x", ""),
|
||||||
|
fstree.File("root/c/skipme", ""),
|
||||||
|
fstree.Symlink("root/loop", "."),
|
||||||
|
fstree.Symlink("root/out", "../outside"),
|
||||||
|
fstree.File("outside/secret", ""),
|
||||||
|
})
|
||||||
|
root := filepath.Join(dir, "root")
|
||||||
|
var got []string
|
||||||
|
err := vfs.Walk(ctx, localfs.New(), root, func(p string, fi vfs.FileInfo, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rel, _ := filepath.Rel(root, p)
|
||||||
|
kind := "f"
|
||||||
|
switch {
|
||||||
|
case fi.IsSymlink():
|
||||||
|
kind = "l"
|
||||||
|
case fi.IsDir():
|
||||||
|
kind = "d"
|
||||||
|
}
|
||||||
|
got = append(got, kind+":"+filepath.ToSlash(rel))
|
||||||
|
if fi.Name == "c" {
|
||||||
|
return vfs.SkipDir
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
want := "d:. d:a f:a/x f:b d:c l:loop l:out"
|
||||||
|
if strings.Join(got, " ") != want {
|
||||||
|
t.Fatalf("walk order:\n got %s\nwant %s", strings.Join(got, " "), want)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Missing root: fn gets the error.
|
||||||
|
var rootErr error
|
||||||
|
_ = vfs.Walk(ctx, localfs.New(), filepath.Join(dir, "missing"), func(_ string, _ vfs.FileInfo, err error) error {
|
||||||
|
rootErr = err
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if !errors.Is(rootErr, vfs.ErrNotExist) {
|
||||||
|
t.Fatalf("root error = %v", rootErr)
|
||||||
|
}
|
||||||
|
}
|
||||||
47
internal/vfs/remove.go
Normal file
47
internal/vfs/remove.go
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
package vfs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RemoveTree deletes p and, if p is a real directory, everything under it
|
||||||
|
// (post-order). The root is Lstat'ed: a symlink — even to a directory — is
|
||||||
|
// removed as a link, and links met inside are never followed (N-P0-3).
|
||||||
|
// progress, if non-nil, is called after every removed node with the running
|
||||||
|
// count. The first error stops the removal and is returned; ctx is checked
|
||||||
|
// before every node.
|
||||||
|
func RemoveTree(ctx context.Context, fsys FS, p string, progress func(removed int)) error {
|
||||||
|
fi, err := fsys.Lstat(ctx, p)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
n := 0
|
||||||
|
return removeTree(ctx, fsys, p, fi, &n, progress)
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeTree(ctx context.Context, fsys FS, p string, fi FileInfo, n *int, progress func(int)) error {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if fi.IsDir() {
|
||||||
|
ents, err := fsys.ReadDir(ctx, p)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("remove %s: %w", p, err)
|
||||||
|
}
|
||||||
|
ops := fsys.Paths()
|
||||||
|
for _, e := range ents {
|
||||||
|
if err := removeTree(ctx, fsys, ops.Join(p, e.Name), e, n, progress); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := fsys.Remove(ctx, p); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*n++
|
||||||
|
if progress != nil {
|
||||||
|
progress(*n)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
87
internal/vfs/same.go
Normal file
87
internal/vfs/same.go
Normal file
|
|
@ -0,0 +1,87 @@
|
||||||
|
package vfs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SameFile reports whether pa on a and pb on b are the same file, following
|
||||||
|
// symlinks. local+local compares device and inode (os.SameFile), which also
|
||||||
|
// catches hard links and bind mounts; other FSes on the same Volume compare
|
||||||
|
// RealPath; different volumes are never the same. A missing path is not an
|
||||||
|
// error: the answer is false.
|
||||||
|
func SameFile(ctx context.Context, a FS, pa string, b FS, pb string) (bool, error) {
|
||||||
|
if a.Volume() != b.Volume() {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
if a.Kind() == KindLocal && b.Kind() == KindLocal {
|
||||||
|
fa, err := os.Stat(pa)
|
||||||
|
if err != nil {
|
||||||
|
return false, notExistIsFalse(err)
|
||||||
|
}
|
||||||
|
fb, err := os.Stat(pb)
|
||||||
|
if err != nil {
|
||||||
|
return false, notExistIsFalse(err)
|
||||||
|
}
|
||||||
|
return os.SameFile(fa, fb), nil
|
||||||
|
}
|
||||||
|
ra, err := a.RealPath(ctx, pa)
|
||||||
|
if err != nil {
|
||||||
|
return false, notExistIsFalse(err)
|
||||||
|
}
|
||||||
|
rb, err := b.RealPath(ctx, pb)
|
||||||
|
if err != nil {
|
||||||
|
return false, notExistIsFalse(err)
|
||||||
|
}
|
||||||
|
return a.Paths().Clean(ra) == b.Paths().Clean(rb), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func notExistIsFalse(err error) error {
|
||||||
|
if errors.Is(err, ErrNotExist) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Within reports whether child is parent or lies inside it after resolving
|
||||||
|
// symlinks on both (e.g. "copy a directory into its own subdirectory through
|
||||||
|
// an alias"). child need not exist yet: its deepest existing ancestor is
|
||||||
|
// resolved and the rest appended. parent must exist.
|
||||||
|
func Within(ctx context.Context, fsys FS, child, parent string) (bool, error) {
|
||||||
|
rp, err := fsys.RealPath(ctx, parent)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
rc, err := realPathLenient(ctx, fsys, child)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return IsWithin(fsys.Paths(), rc, rp), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// realPathLenient resolves p, or — if p does not exist — its deepest existing
|
||||||
|
// ancestor joined with the missing tail.
|
||||||
|
func realPathLenient(ctx context.Context, fsys FS, p string) (string, error) {
|
||||||
|
ops := fsys.Paths()
|
||||||
|
p = ops.Clean(p)
|
||||||
|
var tail []string
|
||||||
|
for {
|
||||||
|
r, err := fsys.RealPath(ctx, p)
|
||||||
|
if err == nil {
|
||||||
|
for i := len(tail) - 1; i >= 0; i-- {
|
||||||
|
r = ops.Join(r, tail[i])
|
||||||
|
}
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
if !errors.Is(err, ErrNotExist) {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
parent := ops.Dir(p)
|
||||||
|
if parent == p {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
tail = append(tail, ops.Base(p))
|
||||||
|
p = parent
|
||||||
|
}
|
||||||
|
}
|
||||||
54
internal/vfs/size.go
Normal file
54
internal/vfs/size.go
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
package vfs
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
|
// SizeResult is the outcome of DirSize. Bytes sums the (lstat) sizes of all
|
||||||
|
// non-directory entries; Files counts them; Dirs counts directories below the
|
||||||
|
// root. Unreadable counts subdirectories whose listing failed — when it is
|
||||||
|
// non-zero the totals are a lower bound ("≥ X, N unreadable").
|
||||||
|
type SizeResult struct {
|
||||||
|
Bytes int64
|
||||||
|
Files int
|
||||||
|
Dirs int
|
||||||
|
Unreadable int
|
||||||
|
}
|
||||||
|
|
||||||
|
// DirSize sums the tree under p without following symlinks. An error on the
|
||||||
|
// root (missing, unreadable, cancelled) is returned; errors on nested
|
||||||
|
// directories are counted in Unreadable instead (N-P1-14). progress, if
|
||||||
|
// non-nil, receives the running totals after each directory.
|
||||||
|
func DirSize(ctx context.Context, fsys FS, p string, progress func(SizeResult)) (SizeResult, error) {
|
||||||
|
var r SizeResult
|
||||||
|
err := Walk(ctx, fsys, p, func(q string, fi FileInfo, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
if q == p {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
r.Unreadable++
|
||||||
|
return SkipDir
|
||||||
|
}
|
||||||
|
switch {
|
||||||
|
case fi.IsDir():
|
||||||
|
if q != p {
|
||||||
|
r.Dirs++
|
||||||
|
}
|
||||||
|
if progress != nil {
|
||||||
|
progress(r)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
r.Files++
|
||||||
|
r.Bytes += fi.Size
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return r, err
|
||||||
|
}
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return r, err
|
||||||
|
}
|
||||||
|
if progress != nil {
|
||||||
|
progress(r)
|
||||||
|
}
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
61
internal/vfs/walk.go
Normal file
61
internal/vfs/walk.go
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
package vfs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
// WalkFunc is called for every node Walk visits. err != nil means p could not
|
||||||
|
// be read: for the root it is the Lstat error (fi is zero); for a directory it
|
||||||
|
// is the ReadDir error, reported in a second call after the first one for that
|
||||||
|
// directory succeeded. Returning SkipDir skips a directory's contents (or, for
|
||||||
|
// a non-directory, the rest of its parent); SkipAll stops the walk with nil.
|
||||||
|
// Any other error stops the walk and is returned.
|
||||||
|
type WalkFunc func(p string, fi FileInfo, err error) error
|
||||||
|
|
||||||
|
// Walk visits root and everything under it through Lstat/ReadDir; symlinks are
|
||||||
|
// reported but never followed. A parent is visited before its children and
|
||||||
|
// children in name order. ctx is checked before every directory read.
|
||||||
|
func Walk(ctx context.Context, fsys FS, root string, fn WalkFunc) error {
|
||||||
|
fi, err := fsys.Lstat(ctx, root)
|
||||||
|
if err != nil {
|
||||||
|
err = fn(root, FileInfo{}, err)
|
||||||
|
} else {
|
||||||
|
err = walk(ctx, fsys, root, fi, fn)
|
||||||
|
}
|
||||||
|
if errors.Is(err, SkipDir) || errors.Is(err, SkipAll) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func walk(ctx context.Context, fsys FS, p string, fi FileInfo, fn WalkFunc) error {
|
||||||
|
if err := fn(p, fi, nil); err != nil || !fi.IsDir() {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ents, err := fsys.ReadDir(ctx, p)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, ErrCancelled) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return fn(p, fi, err)
|
||||||
|
}
|
||||||
|
sort.Slice(ents, func(i, j int) bool { return ents[i].Name < ents[j].Name })
|
||||||
|
ops := fsys.Paths()
|
||||||
|
for _, e := range ents {
|
||||||
|
if err := walk(ctx, fsys, ops.Join(p, e.Name), e, fn); err != nil {
|
||||||
|
if errors.Is(err, SkipDir) {
|
||||||
|
if e.IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return nil // SkipDir on a file: skip the rest of this directory
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue