Skip to content

Commit

Permalink
bugfix: handle absolute and relative paths as expected (#31)
Browse files Browse the repository at this point in the history
* chore: ignore .vscode folder

* feat: handle absolute and relative paths as expected

Previous behavior would take the absolute or relative path and
expand the directory structure onto the receiver. This is not what
one expects when for instance sending files using the absolute path
"portal send /home/zinokader/data/somefile.txt", which previously would
actually try to recreate the same directory structure "/home/zinokader..."
on the receiver end.

Now, we handle absolute paths by sending the last directory or file
(and its subfiles/subdirectories), this means "/home/zinokader/folder"
will just send and expand "folder" and all of its contents on the receiving end.
  • Loading branch information
ZinoKader committed Jan 18, 2023
1 parent 4de20b5 commit 130953d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
# misc
.DS_Store
dist/
.vscode
22 changes: 18 additions & 4 deletions internal/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,17 @@ func FilesTotalSize(files []*os.File) (int64, error) {
return size, nil
}

// addToTarArchive adds a file/folder to a tar archive. This function handles symlinks by replacing
// them with the files that they point to
// addToTarArchive adds a file/folder to a tar archive.
// Handles symlinks by replacing them with the files that they point to.
func addToTarArchive(tw *tar.Writer, file *os.File) error {
return filepath.Walk(file.Name(), func(path string, fi os.FileInfo, err error) error {
var absoluteBase string
absPath, err := filepath.Abs(file.Name())
if err != nil {
return err
}
absoluteBase = filepath.Dir(absPath)

return filepath.Walk(file.Name(), func(path string, fi os.FileInfo, err error) error {
if (fi.Mode() & os.ModeSymlink) == os.ModeSymlink {
// read path that the symlink is pointing to
var link string
Expand All @@ -165,7 +171,15 @@ func addToTarArchive(tw *tar.Writer, file *os.File) error {
if e != nil {
return err
}
header.Name = filepath.ToSlash(path)

// use absolute paths to handle both relative and absolute input paths identically
targetPath, err := filepath.Abs(path)
if err != nil {
return err
}
// remove the absolute root from the filename, leaving only the desired filename
header.Name = filepath.ToSlash(strings.TrimPrefix(targetPath, absoluteBase))
header.Name = strings.TrimPrefix(header.Name, string(os.PathSeparator))

if err := tw.WriteHeader(header); err != nil {
return err
Expand Down

0 comments on commit 130953d

Please sign in to comment.