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

Commit

Permalink
Use real path instead of archive path to access symlinks, fix #222 (#296
Browse files Browse the repository at this point in the history
)
  • Loading branch information
iotanbo authored Oct 28, 2021
1 parent d35d4ce commit e0c66cf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
3 changes: 3 additions & 0 deletions archiver.go
Original file line number Diff line number Diff line change
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.
SourcePath string
}

// Name returns fi.CustomName if not empty;
Expand Down
9 changes: 7 additions & 2 deletions tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ func (t *Tar) writeWalk(source, topLevelFolder, destination string) error {
FileInfo: FileInfo{
FileInfo: info,
CustomName: nameInArchive,
SourcePath: 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.SourcePath)
if err != nil {
return fmt.Errorf("%s: readlink: %v", f.Name(), err)
return fmt.Errorf("%s: readlink: %v", fi.SourcePath, err)
}
}

Expand Down
11 changes: 8 additions & 3 deletions zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ func (z *Zip) writeWalk(source, topLevelFolder, destination string) error {
FileInfo: FileInfo{
FileInfo: info,
CustomName: nameInArchive,
SourcePath: 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.SourcePath)
if err != nil {
return fmt.Errorf("%s: readlink: %v", f.Name(), err)
return fmt.Errorf("%s: readlink: %v", fi.SourcePath, 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.SourcePath, err)
}
return nil
}
Expand Down

0 comments on commit e0c66cf

Please sign in to comment.