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

decompressor_tgz: handle symlinks and links (hardlinks) #37

Closed
wants to merge 2 commits into from
Closed
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
20 changes: 20 additions & 0 deletions decompress_tgz.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ func (d *TarGzipDecompressor) Decompress(dst, src string, dir bool) error {
// Mark that we're done so future in single file mode errors
done = true

if (hdr.Typeflag == tar.TypeSymlink) {
// If the type is a symlink we re-write it and
// continue instead of attempting to copy the contents
if err := os.Symlink(hdr.Linkname, path); err != nil {
return fmt.Errorf("failed writing symbolic link: %s", err)
}

continue
}

if (hdr.Typeflag == tar.TypeLink) {
// If the type is a link ("hardlink") we re-write it and
// continue instead of attempting to copy the contents
if err := os.Link(hdr.Linkname, path); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you may need to accumulate the hard links and iterate over them after unpacking everything. If the hard link comes before the target lexically, this might not work. It's fine for symlinks since they are allowed to be broken. It would be worth it to try that in the test fixtures as well.

return fmt.Errorf("failed writing link: %s", err)
}

continue
}

// Open the file for writing
dstF, err := os.Create(path)
if err != nil {
Expand Down
16 changes: 16 additions & 0 deletions decompress_tgz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ func TestTarGzipDecompressor(t *testing.T) {
multiplePaths,
"",
},

{
"with-symlinks.tar.gz",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is still useful but we should definitely augment and check that the symlink actually got written as a symlink (instead of just contents being duplicated).

true,
false,
[]string{"bar", "baz", "foo"},
"",
},

{
"with-links.tar.gz",
true,
false,
[]string{"foo", "bar"},
"",
},
}

for i, tc := range cases {
Expand Down
Binary file added test-fixtures/decompress-tgz/with-links.tar.gz
Binary file not shown.
Binary file added test-fixtures/decompress-tgz/with-symlinks.tar.gz
Binary file not shown.