-
Notifications
You must be signed in to change notification settings - Fork 2
/
fs_windows.go
293 lines (262 loc) · 6.02 KB
/
fs_windows.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// The below code uses portions of the Go standard library.
// The full license can be found in fs.go.
//
// Copyright 2009 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 fs
import (
"io"
"os"
"path/filepath"
"strings"
"syscall"
"time"
"unicode"
)
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#maxpath
//
// When using an API to create a directory, the specified path cannot be so
// long that you cannot append an 8.3 file name (that is, the directory name
// cannot exceed MAX_PATH minus 12).
//
// MAX_PATH = 260
//
const MAX_PATH = 248 // 260 - 12
func absPath(path string) (string, error) {
if filepath.IsAbs(path) {
return filepath.Clean(path), nil
}
return filepath.Abs(filepath.Clean(path))
}
func winPath(path string) (string, error) {
if len(path) == 0 || (len(path) >= 2 && path[:2] == `\\`) {
return path, nil
}
p, err := absPath(path)
if err != nil {
return "", err
}
if len(p) >= MAX_PATH {
return `\\?\` + strings.TrimRightFunc(p, unicode.IsSpace), nil
}
return path, nil
}
func newPathError(op, path string, err error) error {
return &os.PathError{
Op: "fs: " + op,
Path: path,
Err: err,
}
}
func newLinkError(op, oldname, newname string, err error) error {
return &os.LinkError{
Op: "fs: " + op,
Old: oldname,
New: newname,
Err: err,
}
}
func chdir(dir string) error {
p, err := winPath(dir)
if err != nil {
return newPathError("chdir", dir, err)
}
return os.Chdir(p)
}
func chmod(name string, mode os.FileMode) error {
p, err := winPath(name)
if err != nil {
return newPathError("chmod", name, err)
}
return os.Chmod(p, mode)
}
func chown(name string, uid, gid int) error {
p, err := winPath(name)
if err != nil {
return newPathError("chown", name, err)
}
return os.Chown(p, uid, gid)
}
func chtimes(name string, atime time.Time, mtime time.Time) error {
p, err := winPath(name)
if err != nil {
return newPathError("chtimes", name, err)
}
return os.Chtimes(p, atime, mtime)
}
func lchown(name string, uid, gid int) error {
p, err := winPath(name)
if err != nil {
return newPathError("lchown", name, err)
}
return os.Lchown(p, uid, gid)
}
func link(oldname, newname string) error {
op, err := winPath(oldname)
if err != nil {
return newLinkError("link", oldname, newname, err)
}
np, err := winPath(newname)
if err != nil {
return newLinkError("link", oldname, newname, err)
}
return os.Link(op, np)
}
func mkdir(name string, perm os.FileMode) error {
p, err := winPath(name)
if err != nil {
return newPathError("mkdir", name, err)
}
return os.Mkdir(p, perm)
}
func mkdirall(path string, perm os.FileMode) error {
p, err := winPath(path)
if err != nil {
return err
}
return os.MkdirAll(p, perm)
}
func readlink(name string) (string, error) {
p, err := winPath(name)
if err != nil {
return "", newPathError("readlink", name, err)
}
return os.Readlink(p)
}
func remove(name string) error {
p, err := winPath(name)
if err != nil {
return newPathError("remove", name, err)
}
return os.Remove(p)
}
func removeall(name string) error {
path, err := winPath(name)
if err != nil {
return newPathError("remove", path, err)
}
// Simple case: if Remove works, we're done.
err = os.Remove(path)
if err == nil || os.IsNotExist(err) {
return nil
}
// Otherwise, is this a directory we need to recurse into?
dir, serr := Lstat(path)
if serr != nil {
if serr, ok := serr.(*os.PathError); ok && (os.IsNotExist(serr.Err) || serr.Err == syscall.ENOTDIR) {
return nil
}
return serr
}
if !dir.IsDir() {
// Not a directory; return the error from Remove.
return err
}
// Directory.
fd, err := Open(path)
if err != nil {
if os.IsNotExist(err) {
// Race. It was deleted between the Lstat and Open.
// Return nil per RemoveAll's docs.
return nil
}
return err
}
// Remove contents & return first error.
err = nil
for {
names, err1 := fd.Readdirnames(100)
for _, name := range names {
err1 := RemoveAll(path + string(os.PathSeparator) + name)
if err == nil {
err = err1
}
}
if err1 == io.EOF {
break
}
// If Readdirnames returned an error, use it.
if err == nil {
err = err1
}
if len(names) == 0 {
break
}
}
// Close directory, because windows won't remove opened directory.
fd.Close()
// Remove directory.
err1 := Remove(path)
if err1 == nil || os.IsNotExist(err1) {
return nil
}
if err == nil {
err = err1
}
return err
}
func rename(oldpath, newpath string) error {
op, err := winPath(oldpath)
if err != nil {
return newLinkError("rename", oldpath, newpath, err)
}
np, err := winPath(newpath)
if err != nil {
return newLinkError("rename", oldpath, newpath, err)
}
return os.Rename(op, np)
}
func symlink(oldname, newname string) error {
op, err := winPath(oldname)
if err != nil {
return newLinkError("symlink", oldname, newname, err)
}
np, err := winPath(newname)
if err != nil {
return newLinkError("symlink", oldname, newname, err)
}
return os.Symlink(op, np)
}
func create(name string) (*os.File, error) {
p, err := winPath(name)
if err != nil {
return nil, newPathError("create", name, err)
}
return os.Create(p)
}
func newfile(fd uintptr, name string) *os.File {
p, err := winPath(name)
if err != nil {
return os.NewFile(fd, name)
}
return os.NewFile(fd, p)
}
func open(name string) (*os.File, error) {
p, err := winPath(name)
if err != nil {
return nil, newPathError("open", name, err)
}
return os.Open(p)
}
func openfile(name string, flag int, perm os.FileMode) (*os.File, error) {
p, err := winPath(name)
if err != nil {
return nil, newPathError("openfile", name, err)
}
return os.OpenFile(p, flag, perm)
}
func lstat(name string) (os.FileInfo, error) {
p, err := winPath(name)
if err != nil {
return nil, newPathError("lstat", name, err)
}
return os.Lstat(p)
}
func stat(name string) (os.FileInfo, error) {
p, err := winPath(name)
if err != nil {
return nil, newPathError("stat", name, err)
}
return os.Stat(p)
}