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

Bugfix: Tar files created in their target directory shouldn't recurse infinitely #384

Merged
merged 5 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.