/
symlink.go
118 lines (111 loc) · 2.73 KB
/
symlink.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package filepath
import (
"errors"
"os"
"runtime"
)
// isRoot returns true if path is root of file system
// (`/` on unix and `/`, `\`, `c:\` or `c:/` on windows).
func isRoot(path string) bool {
if runtime.GOOS != "windows" {
return path == "/"
}
switch len(path) {
case 1:
return os.IsPathSeparator(path[0])
case 3:
return path[1] == ':' && os.IsPathSeparator(path[2])
}
return false
}
// isDriveLetter returns true if path is Windows drive letter (like "c:").
func isDriveLetter(path string) bool {
if runtime.GOOS != "windows" {
return false
}
return len(path) == 2 && path[1] == ':'
}
func walkLink(path string, linksWalked *int) (newpath string, islink bool, err error) {
if *linksWalked > 255 {
return "", false, errors.New("EvalSymlinks: too many links")
}
fi, err := os.Lstat(path)
if err != nil {
return "", false, err
}
if fi.Mode()&os.ModeSymlink == 0 {
return path, false, nil
}
newpath, err = os.Readlink(path)
if err != nil {
return "", false, err
}
*linksWalked++
return newpath, true, nil
}
func walkLinks(path string, linksWalked *int) (string, error) {
switch dir, file := Split(path); {
case dir == "":
newpath, _, err := walkLink(file, linksWalked)
return newpath, err
case file == "":
if isDriveLetter(dir) {
return dir, nil
}
if os.IsPathSeparator(dir[len(dir)-1]) {
if isRoot(dir) {
return dir, nil
}
return walkLinks(dir[:len(dir)-1], linksWalked)
}
newpath, _, err := walkLink(dir, linksWalked)
return newpath, err
default:
newdir, err := walkLinks(dir, linksWalked)
if err != nil {
return "", err
}
newpath, islink, err := walkLink(Join(newdir, file), linksWalked)
if err != nil {
return "", err
}
if !islink {
return newpath, nil
}
if IsAbs(newpath) || os.IsPathSeparator(newpath[0]) {
return newpath, nil
}
return Join(newdir, newpath), nil
}
}
func walkSymlinks(path string) (string, error) {
if path == "" {
return path, nil
}
var linksWalked int // to protect against cycles
for {
i := linksWalked
newpath, err := walkLinks(path, &linksWalked)
if err != nil {
return "", err
}
if runtime.GOOS == "windows" {
// walkLinks(".", ...) always returns "." on unix.
// But on windows it returns symlink target, if current
// directory is a symlink. Stop the walk, if symlink
// target is not absolute path, and return "."
// to the caller (just like unix does).
// Same for "C:.".
if path[volumeNameLen(path):] == "." && !IsAbs(newpath) {
return path, nil
}
}
if i == linksWalked {
return Clean(newpath), nil
}
path = newpath
}
}