Skip to content

Commit 68d1868

Browse files
committed
accommodate change requests:
* use filepath.EvalSymlinks * resolve only symlinks of the parent * update trashdir_test
1 parent 9bdedb9 commit 68d1868

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ require (
1717
github.com/spf13/pflag v1.0.5
1818
github.com/stretchr/testify v1.8.4
1919
github.com/umlx5h/go-runewidth v0.0.0-20240106112317-9bbbb3702d5f
20-
github.com/yookoala/realpath v1.0.0
2120
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
2221
golang.org/x/term v0.21.0
2322
)

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ github.com/umlx5h/go-runewidth v0.0.0-20240106112317-9bbbb3702d5f h1:T8MNFeOIelX
8989
github.com/umlx5h/go-runewidth v0.0.0-20240106112317-9bbbb3702d5f/go.mod h1:+aP7JKaGs4irGEvKbEMTjKb1uKLoRZKMrrUwdGzajsk=
9090
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
9191
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
92-
github.com/yookoala/realpath v1.0.0 h1:7OA9pj4FZd+oZDsyvXWQvjn5oBdcHRTV44PpdMSuImQ=
93-
github.com/yookoala/realpath v1.0.0/go.mod h1:gJJMA9wuX7AcqLy1+ffPatSCySA1FQ2S8Ya9AIoYBpE=
9492
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY=
9593
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI=
9694
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=

internal/xdg/trashdir.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
"github.com/moby/sys/mountinfo"
1616
"github.com/umlx5h/gtrash/internal/env"
17-
"github.com/yookoala/realpath"
1817
)
1918

2019
type trashDirType string
@@ -240,15 +239,15 @@ func getAllMountpoints() ([]string, error) {
240239
}
241240

242241
var mountinfo_Mounted = mountinfo.Mounted
243-
var realpath_Realpath = realpath.Realpath
242+
var EvalSymLinks = filepath.EvalSymlinks
244243

245244
// Obtain a mount point associated with a file.
246245
// Same as df <PATH>
247246
func getMountpoint(path string) (string, error) {
248247

249-
// iterate over the parents of the real (without symlinks) path until we find a mount point
248+
// iterate over the real (without symlinks) parents of path until we find a mount point
250249

251-
candidate, err := realpath_Realpath(path)
250+
candidate, err := EvalSymLinks(filepath.Dir(path))
252251
if err != nil {
253252
return "", err
254253
}
@@ -266,7 +265,12 @@ func getMountpoint(path string) (string, error) {
266265
return "", errors.New("mountpoint is '.'")
267266
}
268267

269-
if mounted, err := mountinfo_Mounted(candidate); err == nil && mounted {
268+
mounted, err := mountinfo_Mounted(candidate)
269+
if err != nil {
270+
return "", err
271+
}
272+
273+
if mounted {
270274
break
271275
}
272276

internal/xdg/trashdir_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,19 @@ func TestGetMountpoint(t *testing.T) {
2020
return slices.Contains(mounts, fpath), nil
2121
}
2222

23-
realpath_Realpath = func(path string) (string, error) {
23+
// not evaluating each component here, just the entire path
24+
symlinked := map[string]string{
25+
// file is a link
26+
"/foo/link.txt": "/foo/bar/target.txt",
27+
28+
// first component is a link
29+
"/link": "/foo/bar",
30+
}
31+
32+
EvalSymLinks = func(path string) (string, error) {
33+
if symlink, ok := symlinked[path]; ok {
34+
return symlink, nil
35+
}
2436
return path, nil
2537
}
2638

@@ -34,6 +46,9 @@ func TestGetMountpoint(t *testing.T) {
3446
{path: "/ffoo/bar/a.txt", want: "/ffoo/bar"},
3547
{path: "/aaa/bbb/ccc/ddd.txt", want: "/"},
3648
{path: "/", want: "/"},
49+
50+
{path: "/foo/link.txt", want: "/foo"},
51+
{path: "/link/a.txt", want: "/foo/bar"},
3752
}
3853

3954
t.Run("normal", func(t *testing.T) {

0 commit comments

Comments
 (0)