Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overwriting symlinks #26

Merged
merged 1 commit into from
Aug 2, 2024
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
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