Skip to content

Commit 03b658f

Browse files
committed
review: decrease cyclomatic complexity
1 parent dfa826f commit 03b658f

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

pkg/fsutils/fsutils_windows.go

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,40 @@ package fsutils
44

55
import (
66
"errors"
7-
"os"
87
"path/filepath"
98
"syscall"
109
)
1110

11+
// This is a workaround for the behavior of [filepath.EvalSymlinks],
12+
// which fails with [syscall.ENOTDIR] if the specified path contains a junction on Windows.
13+
// Junctions can occur, for example, when a volume is mounted as a subdirectory inside another drive.
14+
// This can usually happen when using the Dev Drives feature and replacing existing directories.
15+
// See: https://github.com/golang/go/issues/40180
16+
//
17+
// Since [syscall.ENOTDIR] is only returned when calling [filepath.EvalSymlinks] on Windows
18+
// if part of the presented path is a junction and nothing before was a symlink,
19+
// we simply treat this as NOT symlink,
20+
// because a symlink over the junction makes no sense at all.
1221
func evalSymlinks(path string) (string, error) {
1322
resolved, err := filepath.EvalSymlinks(path)
1423
if err == nil {
1524
return resolved, nil
1625
}
1726

18-
// This is a workaround for the behavior of filepath.EvalSymlinks, which fails with
19-
// syscall.ENOTDIR if the specified path contains a junction on Windows. Junctions
20-
// can occur, for example, when a volume is mounted as a subdirectory inside another
21-
// drive. This can usually happen when using the Dev Drives feature and replacing
22-
// existing directories. See: https://github.com/golang/go/issues/40180
23-
//
24-
// Since syscall.ENOTDIR is only returned when calling filepath.EvalSymlinks on
25-
// Windows if part of the presented path is a junction and nothing before was a
26-
// symlink, we simply treat this as NOT symlink, because a symlink over the junction
27-
// makes no sense at all.
28-
if errors.Is(err, syscall.ENOTDIR) {
29-
if _, sErr := os.Stat(path); sErr == nil {
30-
// If exists, we make the path absolute, to be sure...
31-
if abs, aErr := filepath.Abs(path); aErr == nil {
32-
return abs, nil
33-
}
34-
}
27+
if !errors.Is(err, syscall.ENOTDIR) {
28+
return "", err
3529
}
36-
return "", err
30+
31+
_, err := os.Stat(path)
32+
if err != nil {
33+
return "", err
34+
}
35+
36+
// If exists, we make the path absolute, to be sure...
37+
abs, err := filepath.Abs(path)
38+
if err != nil {
39+
return "", err
40+
}
41+
42+
return abs, nil
3743
}

0 commit comments

Comments
 (0)