Skip to content

Commit

Permalink
fs: Skip files named . during walk (#384)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
drewstinnett and mholt authored Aug 31, 2023
1 parent de8cf22 commit e2261a1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/fs"
"log"
"net/http"
"os"
"path"
"reflect"
"sort"
Expand Down Expand Up @@ -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))),
Expand Down
Binary file added testdata/self-tar.tar
Binary file not shown.

0 comments on commit e2261a1

Please sign in to comment.