Skip to content

Commit

Permalink
os symlink use rel path
Browse files Browse the repository at this point in the history
  • Loading branch information
izouxv committed Jun 30, 2024
1 parent e211396 commit a269d78
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 6 additions & 1 deletion os.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package afero

import (
"os"
"path/filepath"
"time"
)

Expand Down Expand Up @@ -105,7 +106,11 @@ func (OsFs) LstatIfPossible(name string) (os.FileInfo, bool, error) {
}

func (OsFs) SymlinkIfPossible(oldname, newname string) error {
return os.Symlink(oldname, newname)
relpath, err := filepath.Rel(filepath.Dir(newname), oldname)
if err != nil {
return &os.LinkError{Op: "symlink", Old: oldname, New: newname, Err: err}
}
return os.Symlink(relpath, newname)
}

func (OsFs) ReadlinkIfPossible(name string) (string, error) {
Expand Down
3 changes: 2 additions & 1 deletion symlink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,15 @@ func TestReadlinkIfPossible(t *testing.T) {
}

testRead := func(r LinkReader, name string, output *string) {
_, err := r.ReadlinkIfPossible(name)
str, err := r.ReadlinkIfPossible(name)
if (err != nil) && (output == nil) {
t.Fatalf("Error reading link, expected success, got error: %v", err)
} else if (err == nil) && (output != nil) {
t.Fatalf("Error reading link, succeeded when expecting error: %v", *output)
} else if err != nil && err.Error() != *output && !strings.HasSuffix(err.Error(), *output) {
t.Fatalf("Error reading link, expected error '%v', instead received '%v'", *output, err)
}
t.Logf("str: %v", str)
}

notSupported := ErrNoReadlink.Error()
Expand Down

0 comments on commit a269d78

Please sign in to comment.