Skip to content

Commit

Permalink
Merge pull request #38 from vinyl-linux/ensure_links
Browse files Browse the repository at this point in the history
Ensure links
  • Loading branch information
jspc authored Mar 6, 2022
2 parents 5cc8f95 + d3602cf commit fb1b586
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
15 changes: 14 additions & 1 deletion os.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,25 @@ func decompressLoop(tr *tar.Reader, dest string) (err error) {
f.Close()

case tar.TypeLink:
err = os.Link(header.Linkname, target)
// remove target if it exists.
//
// ignoring the error is fine here; if there's an error
// we'll see it when we try to link anyway /shrug
os.Remove(target)

err = os.Link(filepath.Join(dest, header.Linkname), target)
if err != nil {
return
}

case tar.TypeSymlink:
// see comment for the tar.TypeLink case above;
os.Remove(target)

// we don't need to worry about prefixing synlinks. Infact,
// we probably don't want that at all. symlinks can point to
// files that don't exist, and probably want to be more flexible
// for things like relative links anyway
err = os.Symlink(header.Linkname, target)
if err != nil {
return
Expand Down
15 changes: 8 additions & 7 deletions os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,20 @@ func TestUntar(t *testing.T) {
}

func TestUntar_WithLinks(t *testing.T) {
dir := "testdata"

err := untar("testdata/complex.tar.bz2", dir)
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("unexpected error: %+v", err)
}

defer os.RemoveAll("testdata/testdata")
err = untar("testdata/complex.tar.bz2", dir)
if err != nil {
t.Fatalf("unexpected error: %+v", err)
}

t.Run("checksum", func(t *testing.T) {
expect := "9fbefb949709fc7086ab4be43544d08406f0ededa0733f6683424e774a6cb799"

sum, err := checksum(filepath.Join(dir, "testdata", "raw"))
sum, err := checksum(filepath.Join(dir, "raw"))
if err != nil {
t.Fatalf("unexpected error: %+v", err)
}
Expand All @@ -74,14 +75,14 @@ func TestUntar_WithLinks(t *testing.T) {
})

t.Run("hard links", func(t *testing.T) {
_, err := os.Stat(filepath.Join(dir, "testdata", "hardlink"))
_, err := os.Stat(filepath.Join(dir, "hardlink"))
if err != nil {
t.Fatalf("unexpected error: %+v", err)
}
})

t.Run("symlinks", func(t *testing.T) {
_, err := os.Stat(filepath.Join(dir, "testdata", "symlink"))
_, err := os.Stat(filepath.Join(dir, "symlink"))
if err != nil {
t.Fatalf("unexpected error: %+v", err)
}
Expand Down
Binary file modified testdata/complex.tar.bz2
Binary file not shown.

0 comments on commit fb1b586

Please sign in to comment.