Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit e2261a1

Browse files
drewstinnettmholt
andauthored
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 <mholt@users.noreply.github.com> * Update fs.go --------- Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
1 parent de8cf22 commit e2261a1

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

fs.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,9 @@ func (f ArchiveFS) ReadDir(name string) ([]fs.DirEntry, error) {
567567
)
568568
handler := func(_ context.Context, file File) error {
569569
file.NameInArchive = strings.Trim(file.NameInArchive, "/")
570+
if file.NameInArchive == "." {
571+
return nil
572+
}
570573
files = append(files, file)
571574
if file.NameInArchive == name && !file.IsDir() {
572575
foundFile = true

fs_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io/fs"
99
"log"
1010
"net/http"
11+
"os"
1112
"path"
1213
"reflect"
1314
"sort"
@@ -53,6 +54,34 @@ var (
5354
unorderZip []byte
5455
)
5556

57+
func TestSelfTar(t *testing.T) {
58+
fn := "testdata/self-tar.tar"
59+
fh, err := os.Open(fn)
60+
if err != nil {
61+
t.Fatalf("Could not load test tar: %v", fn)
62+
}
63+
fstat, err := os.Stat(fn)
64+
if err != nil {
65+
t.Fatalf("Could not stat test tar: %v", fn)
66+
}
67+
fsys := ArchiveFS{
68+
Stream: io.NewSectionReader(fh, 0, fstat.Size()),
69+
Format: Tar{},
70+
}
71+
var count int
72+
err = fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
73+
if count > 10 {
74+
t.Error("walking test tar appears to be recursing in error")
75+
return fmt.Errorf("recursing tar: %v", fn)
76+
}
77+
count++
78+
return nil
79+
})
80+
if err != nil {
81+
t.Fatal(err)
82+
}
83+
}
84+
5685
func ExampleArchiveFS_Stream() {
5786
fsys := ArchiveFS{
5887
Stream: io.NewSectionReader(bytes.NewReader(testZIP), 0, int64(len(testZIP))),

testdata/self-tar.tar

6 KB
Binary file not shown.

0 commit comments

Comments
 (0)