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

Bugfix issue#222 #296

Merged
merged 1 commit into from Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
SourcePath 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,
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
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