Skip to content

Commit

Permalink
Merge pull request #26 from lugray/symlink_overwrite
Browse files Browse the repository at this point in the history
Allow overwriting symlinks
  • Loading branch information
cmaglie authored Aug 2, 2024
2 parents 07d6c33 + efb60af commit 778355b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func Zip(ctx context.Context, body io.Reader, location string, rename Renamer) e
type fs struct{}

func (f fs) Link(oldname, newname string) error {
_ = os.Remove(newname) // Ignore error. We don't care if the file doesn't exist.
return os.Link(oldname, newname)
}

Expand All @@ -124,6 +125,7 @@ func (f fs) MkdirAll(path string, perm os.FileMode) error {
}

func (f fs) Symlink(oldname, newname string) error {
_ = os.Remove(newname) // Ignore error. We don't care if the file doesn't exist.
return os.Symlink(oldname, newname)
}

Expand Down
42 changes: 42 additions & 0 deletions extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,48 @@ func TestExtract(t *testing.T) {
}
}

func TestExtractIdempotency(t *testing.T) {
for _, test := range ExtractCases {
dir, _ := os.MkdirTemp("", "")
dir = filepath.Join(dir, "test")
data, err := os.ReadFile(test.Archive)
if err != nil {
t.Fatal(err)
}

var extractFn func(context.Context, io.Reader, string, extract.Renamer) error
switch filepath.Ext(test.Archive) {
case ".bz2":
extractFn = extract.Bz2
case ".gz":
extractFn = extract.Gz
case ".zip":
extractFn = extract.Zip
case ".mistery":
extractFn = extract.Archive
default:
t.Fatal("unknown error")
}

buffer := bytes.NewBuffer(data)
if err = extractFn(context.Background(), buffer, dir, test.Renamer); err != nil {
t.Fatal(test.Name, ": Should not fail first extraction: "+err.Error())
}

buffer = bytes.NewBuffer(data)
if err = extractFn(context.Background(), buffer, dir, test.Renamer); err != nil {
t.Fatal(test.Name, ": Should not fail second extraction: "+err.Error())
}

testWalk(t, dir, test.Files)

err = os.RemoveAll(dir)
if err != nil {
t.Fatal(err)
}
}
}

func BenchmarkArchive(b *testing.B) {
dir, _ := os.MkdirTemp("", "")
data, _ := os.ReadFile("testdata/archive.tar.bz2")
Expand Down

0 comments on commit 778355b

Please sign in to comment.