...
Source file
src/os/path_windows.go
Documentation: os
1
2
3
4
5 package os
6
7 const (
8 PathSeparator = '\\'
9 PathListSeparator = ';'
10 )
11
12
13 func IsPathSeparator(c uint8) bool {
14
15 return c == '\\' || c == '/'
16 }
17
18
19
20 func basename(name string) string {
21
22 if len(name) == 2 && name[1] == ':' {
23 name = "."
24 } else if len(name) > 2 && name[1] == ':' {
25 name = name[2:]
26 }
27 i := len(name) - 1
28
29 for ; i > 0 && (name[i] == '/' || name[i] == '\\'); i-- {
30 name = name[:i]
31 }
32
33 for i--; i >= 0; i-- {
34 if name[i] == '/' || name[i] == '\\' {
35 name = name[i+1:]
36 break
37 }
38 }
39 return name
40 }
41
42 func isAbs(path string) (b bool) {
43 v := volumeName(path)
44 if v == "" {
45 return false
46 }
47 path = path[len(v):]
48 if path == "" {
49 return false
50 }
51 return IsPathSeparator(path[0])
52 }
53
54 func volumeName(path string) (v string) {
55 if len(path) < 2 {
56 return ""
57 }
58
59 c := path[0]
60 if path[1] == ':' &&
61 ('0' <= c && c <= '9' || 'a' <= c && c <= 'z' ||
62 'A' <= c && c <= 'Z') {
63 return path[:2]
64 }
65
66 if l := len(path); l >= 5 && IsPathSeparator(path[0]) && IsPathSeparator(path[1]) &&
67 !IsPathSeparator(path[2]) && path[2] != '.' {
68
69 for n := 3; n < l-1; n++ {
70
71 if IsPathSeparator(path[n]) {
72 n++
73
74 if !IsPathSeparator(path[n]) {
75 if path[n] == '.' {
76 break
77 }
78 for ; n < l; n++ {
79 if IsPathSeparator(path[n]) {
80 break
81 }
82 }
83 return path[:n]
84 }
85 break
86 }
87 }
88 }
89 return ""
90 }
91
92 func fromSlash(path string) string {
93
94 var pathbuf []byte
95 var lastSlash int
96 for i, b := range path {
97 if b == '/' {
98 if pathbuf == nil {
99 pathbuf = make([]byte, len(path))
100 }
101 copy(pathbuf[lastSlash:], path[lastSlash:i])
102 pathbuf[i] = '\\'
103 lastSlash = i + 1
104 }
105 }
106 if pathbuf == nil {
107 return path
108 }
109
110 copy(pathbuf[lastSlash:], path[lastSlash:])
111 return string(pathbuf)
112 }
113
114 func dirname(path string) string {
115 vol := volumeName(path)
116 i := len(path) - 1
117 for i >= len(vol) && !IsPathSeparator(path[i]) {
118 i--
119 }
120 dir := path[len(vol) : i+1]
121 last := len(dir) - 1
122 if last > 0 && IsPathSeparator(dir[last]) {
123 dir = dir[:last]
124 }
125 if dir == "" {
126 dir = "."
127 }
128 return vol + dir
129 }
130
131
132
133 var canUseLongPaths bool
134
135
136
137
138
139
140
141
142
143 func fixLongPath(path string) string {
144 if canUseLongPaths {
145 return path
146 }
147
148
149
150
151
152
153
154
155
156
157
158 if len(path) < 248 {
159
160
161 return path
162 }
163
164
165
166
167
168
169
170
171
172
173
174
175 if len(path) >= 2 && path[:2] == `\\` {
176
177 return path
178 }
179 if !isAbs(path) {
180
181 return path
182 }
183
184 const prefix = `\\?`
185
186 pathbuf := make([]byte, len(prefix)+len(path)+len(`\`))
187 copy(pathbuf, prefix)
188 n := len(path)
189 r, w := 0, len(prefix)
190 for r < n {
191 switch {
192 case IsPathSeparator(path[r]):
193
194 r++
195 case path[r] == '.' && (r+1 == n || IsPathSeparator(path[r+1])):
196
197 r++
198 case r+1 < n && path[r] == '.' && path[r+1] == '.' && (r+2 == n || IsPathSeparator(path[r+2])):
199
200 return path
201 default:
202 pathbuf[w] = '\\'
203 w++
204 for ; r < n && !IsPathSeparator(path[r]); r++ {
205 pathbuf[w] = path[r]
206 w++
207 }
208 }
209 }
210
211 if w == len(`\\?\c:`) {
212 pathbuf[w] = '\\'
213 w++
214 }
215 return string(pathbuf[:w])
216 }
217
View as plain text