Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
Fix hard links (fixes #152)
Browse files Browse the repository at this point in the history
  • Loading branch information
mholt committed Mar 7, 2019
1 parent bfece90 commit 11f2ffb
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (t *Tar) addTopLevelFolder(sourceArchive, destination string) (string, erro
return destination, nil
}

func (t *Tar) untarNext(to string) error {
func (t *Tar) untarNext(destination string) error {
f, err := t.Read()
if err != nil {
return err // don't wrap error; calling loop must break on io.EOF
Expand All @@ -215,20 +215,17 @@ func (t *Tar) untarNext(to string) error {
if !ok {
return fmt.Errorf("expected header to be *tar.Header but was %T", f.Header)
}
return t.untarFile(f, filepath.Join(to, header.Name))
return t.untarFile(f, destination, header)
}

func (t *Tar) untarFile(f File, to string) error {
func (t *Tar) untarFile(f File, destination string, hdr *tar.Header) error {
to := filepath.Join(destination, hdr.Name)

// do not overwrite existing files, if configured
if !f.IsDir() && !t.OverwriteExisting && fileExists(to) {
return fmt.Errorf("file already exists: %s", to)
}

hdr, ok := f.Header.(*tar.Header)
if !ok {
return fmt.Errorf("expected header to be *tar.Header but was %T", f.Header)
}

switch hdr.Typeflag {
case tar.TypeDir:
return mkdir(to, f.Mode())
Expand All @@ -237,7 +234,7 @@ func (t *Tar) untarFile(f File, to string) error {
case tar.TypeSymlink:
return writeNewSymbolicLink(to, hdr.Linkname)
case tar.TypeLink:
return writeNewHardLink(to, filepath.Join(to, hdr.Linkname))
return writeNewHardLink(to, filepath.Join(destination, hdr.Linkname))
case tar.TypeXGlobalHeader:
return nil // ignore the pax global header from git-generated tarballs
default:
Expand Down Expand Up @@ -513,9 +510,9 @@ func (t *Tar) Extract(source, target, destination string) error {
if err != nil {
return fmt.Errorf("relativizing paths: %v", err)
}
joined := filepath.Join(destination, end)
th.Name = end

err = t.untarFile(f, joined)
err = t.untarFile(f, destination, th)
if err != nil {
return fmt.Errorf("extracting file %s: %v", th.Name, err)
}
Expand Down

0 comments on commit 11f2ffb

Please sign in to comment.