Skip to content

Commit 4834153

Browse files
committed
os synlink use related path
1 parent e211396 commit 4834153

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

os.go

Lines changed: 12 additions & 2 deletions
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,9 +106,18 @@ 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(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) {
112-
return os.Readlink(name)
117+
path, err := os.Readlink(name)
118+
if err != nil {
119+
return "", err
120+
}
121+
pathAbs := filepath.Clean(filepath.Join(name, path))
122+
return pathAbs, nil
113123
}

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)