Skip to content

Commit a269d78

Browse files
committed
os symlink use rel path
1 parent e211396 commit a269d78

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

os.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package afero
1616

1717
import (
1818
"os"
19+
"path/filepath"
1920
"time"
2021
)
2122

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

107108
func (OsFs) SymlinkIfPossible(oldname, newname string) error {
108-
return os.Symlink(oldname, newname)
109+
relpath, err := filepath.Rel(filepath.Dir(newname), oldname)
110+
if err != nil {
111+
return &os.LinkError{Op: "symlink", Old: oldname, New: newname, Err: err}
112+
}
113+
return os.Symlink(relpath, newname)
109114
}
110115

111116
func (OsFs) ReadlinkIfPossible(name string) (string, error) {

symlink_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,15 @@ func TestReadlinkIfPossible(t *testing.T) {
132132
}
133133

134134
testRead := func(r LinkReader, name string, output *string) {
135-
_, err := r.ReadlinkIfPossible(name)
135+
str, err := r.ReadlinkIfPossible(name)
136136
if (err != nil) && (output == nil) {
137137
t.Fatalf("Error reading link, expected success, got error: %v", err)
138138
} else if (err == nil) && (output != nil) {
139139
t.Fatalf("Error reading link, succeeded when expecting error: %v", *output)
140140
} else if err != nil && err.Error() != *output && !strings.HasSuffix(err.Error(), *output) {
141141
t.Fatalf("Error reading link, expected error '%v', instead received '%v'", *output, err)
142142
}
143+
t.Logf("str: %v", str)
143144
}
144145

145146
notSupported := ErrNoReadlink.Error()

0 commit comments

Comments
 (0)