From e2261a1bb41f126f6de5f6052301f2b0749a0caf Mon Sep 17 00:00:00 2001 From: Drew Stinnett Date: Thu, 31 Aug 2023 02:09:51 -0400 Subject: [PATCH] fs: Skip files named . during walk (#384) This happens from tar files being created in their target directory, apparently. Avoid infinite walk. Fix #383. * Cleaning up directories containing dots * Cleaning up some debug bits * More descriptive tests * Update fs.go Updating per suggestion, looks great! Co-authored-by: Matt Holt * Update fs.go --------- Co-authored-by: Matt Holt --- fs.go | 3 +++ fs_test.go | 29 +++++++++++++++++++++++++++++ testdata/self-tar.tar | Bin 0 -> 6144 bytes 3 files changed, 32 insertions(+) create mode 100644 testdata/self-tar.tar diff --git a/fs.go b/fs.go index a0ed59bc..eee7e607 100644 --- a/fs.go +++ b/fs.go @@ -567,6 +567,9 @@ func (f ArchiveFS) ReadDir(name string) ([]fs.DirEntry, error) { ) handler := func(_ context.Context, file File) error { file.NameInArchive = strings.Trim(file.NameInArchive, "/") + if file.NameInArchive == "." { + return nil + } files = append(files, file) if file.NameInArchive == name && !file.IsDir() { foundFile = true diff --git a/fs_test.go b/fs_test.go index 6262b161..9180fbf3 100644 --- a/fs_test.go +++ b/fs_test.go @@ -8,6 +8,7 @@ import ( "io/fs" "log" "net/http" + "os" "path" "reflect" "sort" @@ -53,6 +54,34 @@ var ( unorderZip []byte ) +func TestSelfTar(t *testing.T) { + fn := "testdata/self-tar.tar" + fh, err := os.Open(fn) + if err != nil { + t.Fatalf("Could not load test tar: %v", fn) + } + fstat, err := os.Stat(fn) + if err != nil { + t.Fatalf("Could not stat test tar: %v", fn) + } + fsys := ArchiveFS{ + Stream: io.NewSectionReader(fh, 0, fstat.Size()), + Format: Tar{}, + } + var count int + err = fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error { + if count > 10 { + t.Error("walking test tar appears to be recursing in error") + return fmt.Errorf("recursing tar: %v", fn) + } + count++ + return nil + }) + if err != nil { + t.Fatal(err) + } +} + func ExampleArchiveFS_Stream() { fsys := ArchiveFS{ Stream: io.NewSectionReader(bytes.NewReader(testZIP), 0, int64(len(testZIP))), diff --git a/testdata/self-tar.tar b/testdata/self-tar.tar new file mode 100644 index 0000000000000000000000000000000000000000..15c2b1d995a92ec62523001fc2f5f645a9052d74 GIT binary patch literal 6144 zcmeHL%T59@6s;!4h-*#U^$R*}r!B*R#Yjvr!KgqCu5bcHNgN(SQGUTcaPP+W0WSPH zJAi?p8HT4u+T>>Xxc9a_?U^|lR7POc1!J7#9D?vE22V^`zpcBluG^3>mb3-61?CVF zBDeq$uXkrluL92tump!C z>;rq+sJ6DYu>GPfMsbV9{Ln3oc-FQ*5wiUiu>S`FqfuAH2(>n;6GDWzv2A1G4xg4 zmuVtWww+=QC5U0+I1D>9!ILm|dhF&i+0&~N)bo@I&`)$tyO*xp^)s}6=Qr!6+XPF& zf)2`EM+J>>JhALMXY$2E09o<;7V(