Skip to content

Commit

Permalink
Merge pull request #37 from vinyl-linux/preserve_symlinks
Browse files Browse the repository at this point in the history
Ensure untar is aware of hard/symlinks
  • Loading branch information
jspc authored Mar 6, 2022
2 parents 97ea4e5 + 48287de commit 5cc8f95
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
13 changes: 13 additions & 0 deletions os.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@ func decompressLoop(tr *tar.Reader, dest string) (err error) {
}

f.Close()

case tar.TypeLink:
err = os.Link(header.Linkname, target)
if err != nil {
return
}

case tar.TypeSymlink:
err = os.Symlink(header.Linkname, target)
if err != nil {
return
}

}
}
}
Expand Down
39 changes: 39 additions & 0 deletions os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -49,6 +50,44 @@ func TestUntar(t *testing.T) {
}
}

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

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

defer os.RemoveAll("testdata/testdata")

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

sum, err := checksum(filepath.Join(dir, "testdata", "raw"))
if err != nil {
t.Fatalf("unexpected error: %+v", err)
}

if expect != sum {
t.Errorf("expected %q, received %q", expect, sum)
}
})

t.Run("hard links", func(t *testing.T) {
_, err := os.Stat(filepath.Join(dir, "testdata", "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"))
if err != nil {
t.Fatalf("unexpected error: %+v", err)
}
})
}

func TestExecute(t *testing.T) {
for _, test := range []struct {
name string
Expand Down
Binary file added testdata/complex.tar.bz2
Binary file not shown.

0 comments on commit 5cc8f95

Please sign in to comment.