Skip to content

Commit

Permalink
Fix issue mholt#222
Browse files Browse the repository at this point in the history
  • Loading branch information
iotanbo committed Oct 19, 2021
1 parent d35d4ce commit 7824911
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
3 changes: 3 additions & 0 deletions archiver.go
Expand Up @@ -125,6 +125,9 @@ type File struct {
type FileInfo struct {
os.FileInfo
CustomName string
// Stores path to the source.
// Used when reading a symlink.
SrcPath string
}

// Name returns fi.CustomName if not empty;
Expand Down
9 changes: 7 additions & 2 deletions tar.go
Expand Up @@ -327,6 +327,7 @@ func (t *Tar) writeWalk(source, topLevelFolder, destination string) error {
FileInfo: FileInfo{
FileInfo: info,
CustomName: nameInArchive,
SrcPath: fpath,
},
ReadCloser: file,
})
Expand Down Expand Up @@ -372,10 +373,14 @@ func (t *Tar) Write(f File) error {

var linkTarget string
if isSymlink(f) {
fi, ok := f.FileInfo.(FileInfo)
if !ok {
return fmt.Errorf("failed to cast fs.FileInfo to archiver.FileInfo: %v", f)
}
var err error
linkTarget, err = os.Readlink(f.Name())
linkTarget, err = os.Readlink(fi.SrcPath)
if err != nil {
return fmt.Errorf("%s: readlink: %v", f.Name(), err)
return fmt.Errorf("%s: readlink: %v", fi.SrcPath, err)
}
}

Expand Down
11 changes: 8 additions & 3 deletions zip.go
Expand Up @@ -350,6 +350,7 @@ func (z *Zip) writeWalk(source, topLevelFolder, destination string) error {
FileInfo: FileInfo{
FileInfo: info,
CustomName: nameInArchive,
SrcPath: fpath,
},
ReadCloser: file,
})
Expand Down Expand Up @@ -431,14 +432,18 @@ func (z *Zip) writeFile(f File, writer io.Writer) error {
return nil // directories have no contents
}
if isSymlink(f) {
fi, ok := f.FileInfo.(FileInfo)
if !ok {
return fmt.Errorf("failed to cast fs.FileInfo to archiver.FileInfo: %v", f)
}
// file body for symlinks is the symlink target
linkTarget, err := os.Readlink(f.Name())
linkTarget, err := os.Readlink(fi.SrcPath)
if err != nil {
return fmt.Errorf("%s: readlink: %v", f.Name(), err)
return fmt.Errorf("%s: readlink: %v", fi.SrcPath, err)
}
_, err = writer.Write([]byte(filepath.ToSlash(linkTarget)))
if err != nil {
return fmt.Errorf("%s: writing symlink target: %v", f.Name(), err)
return fmt.Errorf("%s: writing symlink target: %v", fi.SrcPath, err)
}
return nil
}
Expand Down

0 comments on commit 7824911

Please sign in to comment.