Debian Codegraph @

symlink/fs_windows.go

raw symbols SCIP
1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE.BSD file.
4
5 // This code is a modified version of [path/filepath/symlink_windows.go]
6 // and [path/filepath/symlink.go] from the Go 1.4.2 standard library, and
7 // added in [docker@9b648df].
8 //
9 // [path/filepath/symlink_windows.go]: https://github.com/golang/go/blob/go1.4.2/src/path/filepath/symlink_windows.go
10 // [path/filepath/symlink.go]: https://github.com/golang/go/blob/go1.4.2/src/path/filepath/symlink.go
11 // [docker@9b648df]: https://github.com/moby/moby/commit/9b648dfac6453de5944ee4bb749115d85a253a05
12
13 package symlink
14
15 import (
16 "bytes"
17 "errors"
18 "os"
19 "path/filepath"
20 "strings"
21
22 "golang.org/x/sys/windows"
23 )
24
25 func toShort(path string) (string, error) {
26 p, err := windows.UTF16FromString(path)
27 if err != nil {
28 return "", err
29 }
30 b := p // GetShortPathName says we can reuse buffer
31 n, err := windows.GetShortPathName(&p[0], &b[0], uint32(len(b)))
32 if err != nil {
33 return "", err
34 }
35 if n > uint32(len(b)) {
36 b = make([]uint16, n)
37 if _, err = windows.GetShortPathName(&p[0], &b[0], uint32(len(b))); err != nil {
38 return "", err
39 }
40 }
41 return windows.UTF16ToString(b), nil
42 }
43
44 func toLong(path string) (string, error) {
45 p, err := windows.UTF16FromString(path)
46 if err != nil {
47 return "", err
48 }
49 b := p // GetLongPathName says we can reuse buffer
50 n, err := windows.GetLongPathName(&p[0], &b[0], uint32(len(b)))
51 if err != nil {
52 return "", err
53 }
54 if n > uint32(len(b)) {
55 b = make([]uint16, n)
56 n, err = windows.GetLongPathName(&p[0], &b[0], uint32(len(b)))
57 if err != nil {
58 return "", err
59 }
60 }
61 b = b[:n]
62 return windows.UTF16ToString(b), nil
63 }
64
65 func evalSymlinks(path string) (string, error) {
66 path, err := walkSymlinks(path)
67 if err != nil {
68 return "", err
69 }
70
71 p, err := toShort(path)
72 if err != nil {
73 return "", err
74 }
75 p, err = toLong(p)
76 if err != nil {
77 return "", err
78 }
79 // windows.GetLongPathName does not change the case of the drive letter,
80 // but the result of EvalSymlinks must be unique, so we have
81 // EvalSymlinks(`c:\a`) == EvalSymlinks(`C:\a`).
82 // Make drive letter upper case.
83 if len(p) >= 2 && p[1] == ':' && 'a' <= p[0] && p[0] <= 'z' {
84 p = string(p[0]+'A'-'a') + p[1:]
85 } else if len(p) >= 6 && p[5] == ':' && 'a' <= p[4] && p[4] <= 'z' {
86 p = p[:3] + string(p[4]+'A'-'a') + p[5:]
87 }
88 return filepath.Clean(p), nil
89 }
90
91 const (
92 utf8RuneSelf = 0x80
93 longPathPrefix = `\\?\`
94 )
95
96 func walkSymlinks(path string) (string, error) {
97 const maxIter = 255
98 originalPath := path
99 // consume path by taking each frontmost path element,
100 // expanding it if it's a symlink, and appending it to b
101 var b bytes.Buffer
102 for n := 0; path != ""; n++ {
103 if n > maxIter {
104 return "", errors.New("too many links in " + originalPath)
105 }
106
107 // A path beginning with `\\?\` represents the root, so automatically
108 // skip that part and begin processing the next segment.
109 if strings.HasPrefix(path, longPathPrefix) {
110 b.WriteString(longPathPrefix)
111 path = path[4:]
112 continue
113 }
114
115 // find next path component, p
116 i := -1
117 for j, c := range path {
118 if c < utf8RuneSelf && os.IsPathSeparator(uint8(c)) {
119 i = j
120 break
121 }
122 }
123 var p string
124 if i == -1 {
125 p, path = path, ""
126 } else {
127 p, path = path[:i], path[i+1:]
128 }
129
130 if p == "" {
131 if b.Len() == 0 {
132 // must be absolute path
133 b.WriteRune(filepath.Separator)
134 }
135 continue
136 }
137
138 // If this is the first segment after the long path prefix, accept the
139 // current segment as a volume root or UNC share and move on to the next.
140 if b.String() == longPathPrefix {
141 b.WriteString(p)
142 b.WriteRune(filepath.Separator)
143 continue
144 }
145
146 fi, err := os.Lstat(b.String() + p)
147 if err != nil {
148 return "", err
149 }
150 if fi.Mode()&os.ModeSymlink == 0 {
151 b.WriteString(p)
152 if path != "" || (b.Len() == 2 && len(p) == 2 && p[1] == ':') {
153 b.WriteRune(filepath.Separator)
154 }
155 continue
156 }
157
158 // it's a symlink, put it at the front of path
159 dest, err := os.Readlink(b.String() + p)
160 if err != nil {
161 return "", err
162 }
163 if isAbs(dest) {
164 b.Reset()
165 }
166 path = dest + string(filepath.Separator) + path
167 }
168 return filepath.Clean(b.String()), nil
169 }
170
171 func isDriveOrRoot(p string) bool {
172 if p == string(filepath.Separator) {
173 return true
174 }
175
176 length := len(p)
177 if length >= 2 {
178 if p[length-1] == ':' && (('a' <= p[length-2] && p[length-2] <= 'z') || ('A' <= p[length-2] && p[length-2] <= 'Z')) {
179 return true
180 }
181 }
182 return false
183 }
184
185 // isAbs is a platform-specific wrapper for filepath.IsAbs. On Windows,
186 // golang filepath.IsAbs does not consider a path \windows\system32 as absolute
187 // as it doesn't start with a drive-letter/colon combination. However, in
188 // docker we need to verify things such as WORKDIR /windows/system32 in
189 // a Dockerfile (which gets translated to \windows\system32 when being processed
190 // by the daemon. This SHOULD be treated as absolute from a docker processing
191 // perspective.
192 func isAbs(path string) bool {
193 if filepath.IsAbs(path) || strings.HasPrefix(path, string(os.PathSeparator)) {
194 return true
195 }
196 return false
197 }