Skip to content

Commit

Permalink
Ensure links and targets are in the same tree
Browse files Browse the repository at this point in the history
Without this we see weirdly vague errors like:

link etc/terminfo/v/vt200 /var/cache/vinyl/vin/packages/ncurses-terminfo-base/6.3.0/etc/terminfo/v/vt220: no such file or directory

which can be avoided by prefixing hard link names with the dest directory. Luckily for us, our cpio installation strategy for alpine APKs magically repoints hardlinks (at least; if I delete the cached package source, files are still readable somehow).
  • Loading branch information
jspc committed Mar 6, 2022
1 parent 5cc8f95 commit 72effc7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
13 changes: 11 additions & 2 deletions os.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,22 @@ 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:
err = os.Symlink(header.Linkname, target)
// see comment for the tar.TypeLink case above;
os.Remove(target)

err = os.Symlink(filepath.Join(dest, 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 72effc7

Please sign in to comment.