Skip to content

Commit

Permalink
Cleaning up directories containing dots
Browse files Browse the repository at this point in the history
  • Loading branch information
drewstinnett committed Aug 30, 2023
1 parent de8cf22 commit b35c539
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
13 changes: 13 additions & 0 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,8 @@ func (f ArchiveFS) ReadDir(name string) ([]fs.DirEntry, error) {

// always find all implicit directories
files = fillImplicit(files)
// Remove bogus "." entries that some tar files generate
files = removeSubdirDots(files)
// and return early for dot file
if name == "." {
return openReadDir(name, files), nil
Expand All @@ -612,6 +614,17 @@ func (f ArchiveFS) ReadDir(name string) ([]fs.DirEntry, error) {
return openReadDir(name, files), nil
}

// Certain tars have the current directory (.) listed as a file, remove that from the listing as it causes recurrsion issues
func removeSubdirDots(files []File) []File {
ret := make([]File, 0, len(files))
for _, item := range files {
if item.Name() != "." {
ret = append(ret, item)
}
}
return ret
}

// Sub returns an FS corresponding to the subtree rooted at dir.
func (f *ArchiveFS) Sub(dir string) (fs.FS, error) {
if !fs.ValidPath(dir) {
Expand Down
30 changes: 30 additions & 0 deletions fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package archiver
import (
"bytes"
_ "embed"
"errors"
"fmt"
"io"
"io/fs"
"log"
"net/http"
"os"
"path"
"reflect"
"sort"
Expand Down Expand Up @@ -53,6 +55,34 @@ var (
unorderZip []byte
)

func TestSelfTar(t *testing.T) {
fn := "testdata/self-tar.tar"
fh, err := os.Open(fn)
if err != nil {
t.Error("Could not load test tar")
}
fstat, err := os.Stat(fn)
if err != nil {
t.Error("Could not stat test tar")
}
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 errors.New("recursing tar")
}
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 b35c539

Please sign in to comment.