-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.go
More file actions
578 lines (530 loc) · 14.7 KB
/
Copy pathutils.go
File metadata and controls
578 lines (530 loc) · 14.7 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
* Copyright 2022 Mandelsoft. All rights reserved.
* This file is licensed under the Apache Software License, v. 2 except as noted
* otherwise in the LICENSE file
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package vfs
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path"
"sort"
"strings"
)
func filesys(fs ...FileSystem) FileSystem {
if len(fs) == 0 {
return nil
}
return fs[0]
}
// IsPathSeparator reports whether c is a directory separator character.
func IsPathSeparator(c uint8) bool {
return PathSeparatorChar == c
}
// Join joins any number of path elements into a single path, adding
// a Separator if necessary. Join never calls Clean on the result to
// assure the result denotes the same file as the input.
// Empty entries will be ignored.
// If a FileSystem is given, the file systems volume.
// handling is applied, otherwise the path argument
// is handled as a regular plain path
func Join(fs FileSystem, elems ...string) string {
for i := 0; i < len(elems); i++ {
if elems[i] == "" {
elems = append(elems[:i], elems[i+1:]...)
}
}
return Trim(fs, strings.Join(elems, PathSeparatorString))
}
// Clean returns the shortest path name equivalent to path
// by purely lexical processing. It applies the following rules
// iteratively until no further processing can be done:
//
// 1. Replace multiple path separators with a single one.
// 2. Eliminate each . path name element (the current directory).
// 3. Eliminate each inner .. path name element (the parent directory)
// along with the non-.. element that precedes it.
// 4. Eliminate .. elements that begin a rooted path:
// that is, replace "/.." by "/" at the beginning of a path.
//
// The returned path ends in a slash only if it is the root "/".
//
// If the result of this process is an empty string, Clean
// returns the string ".".
// If a FileSystem is given, the file systems volume.
// handling is applied, otherwise the path argument
// is handled as a regular plain path
func Clean(fs FileSystem, p string) string {
vol := ""
if fs != nil {
p = fs.Normalize(p)
vol = fs.VolumeName(p)
}
return vol + path.Clean(p[len(vol):])
}
// Dir returns the path's directory dropping the final element
// after removing trailing Separators, Dir does not call Clean on the path.
// If the path is empty, Dir returns "." or "/" for a rooted path.
// If the path consists entirely of Separators, Dir2 returns a single Separator.
// The returned path does not end in a Separator unless it is the root directory.
// This function is the counterpart of Base
// Base("a/b/")="b" and Dir("a/b/") = "a".
// If a FileSystem is given, the file systems volume.
// handling is applied, otherwise the path argument
// is handled as a regular plain path
func Dir(fs FileSystem, path string) string {
def := "."
vol := ""
if fs != nil {
vol, path = SplitVolume(fs, path)
}
i := len(path) - 1
for i > 0 && IsPathSeparator(path[i]) {
i--
}
for i >= 0 && !IsPathSeparator(path[i]) {
i--
}
for i > 0 && IsPathSeparator(path[i]) {
def = PathSeparatorString
i--
}
path = path[0 : i+1]
if path == "" {
path = def
}
return vol + path
}
// Base extracts the last path component.
// For the root path it returns the root name,
// For an empty path . is returned
// If a FileSystem is given, the file systems volume.
// handling is applied, otherwise the path argument
// is handled as a regular plain path
func Base(fs FileSystem, path string) string {
if fs != nil {
_, path = SplitVolume(fs, path)
}
i := len(path) - 1
for i > 0 && IsPathSeparator(path[i]) {
i--
}
j := i
for j >= 0 && !IsPathSeparator(path[j]) {
j--
}
path = path[j+1 : i+1]
if path == "" {
if j == 0 {
return PathSeparatorString
}
return "."
}
return path
}
// Trim eliminates trailing slashes from a path name.
// An empty path is unchanged.
// If a FileSystem is given, the file systems volume
// handling is applied, otherwise the path argument
// is handled as a regular plain path
func Trim(fs FileSystem, path string) string {
vol := ""
if fs != nil {
path = fs.Normalize(path)
vol = fs.VolumeName(path)
}
i := len(path) - 1
for i > len(vol) && IsPathSeparator(path[i]) {
i--
}
k := i + 1
path = path[:k]
for i >= len(vol) {
j := i
for j >= len(vol) && IsPathSeparator(path[j]) {
j--
}
if i != j {
if path[i+1:k] == "." {
if j < len(vol) && k == len(path) {
j++ // keep starting separator instead of trailing one, because this does not exist
}
i = k
}
path = path[:j+1] + path[i:]
i = j
k = i + 1
}
i--
}
if k < len(path) && path[len(vol):k] == "." {
path = path[:len(vol)] + path[k+1:]
}
return path
}
// IsAbs return true if the given path is an absolute one
// starting with a Separator or is quailified by a volume name.
func IsAbs(fs FileSystem, path string) bool {
if fs != nil {
_, path = SplitVolume(fs, path)
}
return len(path) > 0 && isSlash(path[0])
}
// IsRoot determines whether a given path is a root path.
// This might be the separator or the separator preceded by
// a volume name.
func IsRoot(fs FileSystem, path string) bool {
_, path = SplitVolume(fs, path)
return len(path) == 1 && isSlash(path[0])
}
func SplitVolume(fs FileSystem, path string) (string, string) {
path = fs.Normalize(path)
vol := fs.VolumeName(path)
return vol, path[len(vol):]
}
// Canonical returns the canonical absolute path of a file.
// If exist=false the denoted file must not exist, but
// then the part of the initial path referring to a not existing
// directory structure is lexically resolved (like Clean) and
// does not consider potential symbolic links that might occur
// if the file is finally created in the future.
func Canonical(fs FileSystem, path string, exist bool) (string, error) {
if !IsAbs(fs, path) {
wd, err := fs.Getwd()
if err != nil {
return "", err
}
path = Join(fs, wd, path)
}
return evalPath(fs, path, exist)
}
// EvalSymlinks resolves all symbolic links in a path
// and returns a path not containing any symbolic link
// anymore. It does not call Clean on a non-canonical path,
// so the result always denotes the same file than the original path.
// If the given path is a relative one, a
// relative one is returned as long as there is no
// absolute symbolic link. It may contain `..`, if it is above
// the current working directory
func EvalSymlinks(fs FileSystem, path string) (string, error) {
return evalPath(fs, path, false)
}
// Abs returns an absolute representation of path.
// If the path is not absolute it will be joined with the current
// working directory to turn it into an absolute path. The absolute
// path name for a given file is not guaranteed to be unique.
// Symbolic links in the given path will be resolved, but not in
// the current working directory, if used to make the path absolute.
// The denoted file may not exist.
// Abs never calls Clean on the result, so the resulting path
// will denote the same file as the argument.
func Abs(fs FileSystem, path string) (string, error) {
path, err := evalPath(fs, path, false)
if err != nil {
return "", err
}
if IsAbs(fs, path) {
return path, nil
}
p, err := fs.Getwd()
if err != nil {
return "", err
}
return Join(fs, p, path), nil
}
// Rel determines the relative path from a source folder
// to a target file.
func Rel(fs FileSystem, src, tgt string) (string, error) {
if ok, _ := Exists(fs, src); ok {
if ok, _ := IsDir(fs, src); !ok {
return "", ErrNotDir
}
}
s, err := Canonical(fs, src, false)
if err != nil {
return "", fmt.Errorf("%s: %w", src, err)
}
t, err := Canonical(fs, tgt, false)
if err != nil {
return "", fmt.Errorf("%s: %w", tgt, err)
}
vs, sseq := Components(fs, s)
vt, tseq := Components(fs, t)
if vs != vt {
return "", fmt.Errorf("different volumes")
}
if s == t {
return ".", nil
}
var is int
for is = 0; is < len(sseq); is++ {
if len(tseq) <= is || tseq[is] != sseq[is] {
break
}
}
for i := is; i < len(sseq); i++ {
sseq[i] = ".."
}
if is < len(tseq) {
return Join(fs, append(sseq[is:], tseq[is:]...)...), nil
}
return Join(fs, sseq[is:]...), nil
}
// Components splits a path into its volume part and a list
// of path components.
func Components(fs FileSystem, p string) (string, []string) {
var seq []string
var b string
v, p := SplitVolume(fs, p)
for p != "" && !IsRoot(fs, p) {
p, b = Split(fs, p)
seq = append(seq, b)
}
for i := 0; i < len(seq)/2; i++ {
seq[i], seq[len(seq)-i-1] = seq[len(seq)-i-1], seq[i]
}
return v, seq
}
// Split splits path immediately following the final Separator,
// separating it into a directory and file name component.
// If there is no Separator in path, Split returns an empty dir
// and file set to path. In contrast to filepath.Split the directory
// path does not end with a trailing Separator, so Split can
// subsequently called for the directory part, again.
func Split(fs FileSystem, path string) (dir, file string) {
path = fs.Normalize(path)
vol := fs.VolumeName(path)
i := len(path) - 1
for i >= len(vol) && !IsPathSeparator(path[i]) {
i--
}
j := i
for j > len(vol) && IsPathSeparator(path[j]) {
j--
}
return path[:j+1], path[i+1:]
}
// SplitPath splits a path into a volume and an array of the path segments
func SplitPath(fs FileSystem, path string) (string, []string, bool) {
vol, path := SplitVolume(fs, path)
rest := path
elems := []string{}
for rest != "" {
i := 0
for i < len(rest) && IsPathSeparator(rest[i]) {
i++
}
j := i
for j < len(rest) && !IsPathSeparator(rest[j]) {
j++
}
b := rest[i:j]
rest = rest[j:]
if b == "." || b == "" {
continue
}
elems = append(elems, b)
}
return vol, elems, strings.HasPrefix(path, PathSeparatorString)
}
func Exists_(err error) bool {
return err == nil || !IsNotExist(err)
}
// Exists checks if a file or directory exists.
func Exists(fs FileSystem, path string) (bool, error) {
_, err := fs.Stat(path)
if err == nil {
return true, nil
}
if IsNotExist(err) {
return false, nil
}
return false, err
}
// DirExists checks if a path exists and is a directory.
func DirExists(fs FileSystem, path string) (bool, error) {
fi, err := fs.Stat(path)
if err == nil && fi.IsDir() {
return true, nil
}
if IsNotExist(err) {
return false, nil
}
return false, err
}
// FileExists checks if a path exists and is a regular file.
func FileExists(fs FileSystem, path string) (bool, error) {
fi, err := fs.Stat(path)
if err == nil && fi.Mode()&os.ModeType == 0 {
return true, nil
}
if IsNotExist(err) {
return false, nil
}
return false, err
}
// IsDir checks if a given path is a directory.
func IsDir(fs FileSystem, path string) (bool, error) {
fi, err := fs.Stat(path)
if err != nil {
return false, err
}
return fi.IsDir(), nil
}
// IsFile checks if a given path is a file.
func IsFile(fs FileSystem, path string) (bool, error) {
fi, err := fs.Stat(path)
if err != nil {
return false, err
}
return fi.Mode()&os.ModeType == 0, nil
}
func CopyFile(srcfs FileSystem, src string, dstfs FileSystem, dst string) error {
fi, err := srcfs.Lstat(src)
if err != nil {
return err
}
if !fi.Mode().IsRegular() {
return errors.New("no regular file")
}
s, err := srcfs.Open(src)
if err != nil {
return err
}
defer s.Close()
d, err := dstfs.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, fi.Mode()&os.ModePerm)
if err != nil {
return err
}
defer d.Close()
_, err = io.Copy(d, s)
if err != nil {
return err
}
return dstfs.Chmod(dst, fi.Mode())
}
// CopyDir recursively copies a directory tree, attempting to preserve permissions.
// Source directory must exist, destination directory may exist.
// Symlinks are ignored and skipped.
func CopyDir(srcfs FileSystem, src string, dstfs FileSystem, dst string) error {
src = Trim(srcfs, src)
dst = Trim(dstfs, dst)
si, err := srcfs.Stat(src)
if err != nil {
return err
}
if !si.IsDir() {
return NewPathError("CopyDir", src, ErrNotDir)
}
di, err := dstfs.Stat(dst)
if err != nil && !os.IsNotExist(err) {
return err
}
if err == nil && !di.IsDir() {
return NewPathError("CopyDir", dst, ErrNotDir)
}
err = dstfs.MkdirAll(dst, si.Mode())
if err != nil {
return err
}
entries, err := ReadDir(srcfs, src)
if err != nil {
return err
}
for _, entry := range entries {
srcPath := Join(srcfs, src, entry.Name())
dstPath := Join(dstfs, dst, entry.Name())
if entry.IsDir() {
err = CopyDir(srcfs, srcPath, dstfs, dstPath)
} else {
// Skip symlinks.
if entry.Mode()&os.ModeSymlink != 0 {
var old string
old, err = srcfs.Readlink(srcPath)
if err == nil {
err = dstfs.Symlink(old, dstPath)
}
if err == nil {
err = os.Chmod(dst, entry.Mode())
}
} else {
err = CopyFile(srcfs, srcPath, dstfs, dstPath)
}
}
if err != nil {
return err
}
}
return nil
}
func Touch(fs FileSystem, path string, perm os.FileMode) error {
file, err := fs.OpenFile(path, os.O_CREATE, perm&os.ModePerm)
if err != nil {
return err
}
file.Close()
return nil
}
// ReadFile reads the file named by filename and returns the contents.
// A successful call returns err == nil, not err == EOF. Because ReadFile
// reads the whole file, it does not treat an EOF from Read as an error
// to be reported.
func ReadFile(fs FileSystem, path string) ([]byte, error) {
file, err := fs.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var buf bytes.Buffer
if _, err := io.Copy(&buf, file); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// WriteFile writes data to a file named by filename.
// If the file does not exist, WriteFile creates it with permissions perm
// (before umask); otherwise WriteFile truncates it before writing.
func WriteFile(fs FileSystem, filename string, data []byte, mode os.FileMode) error {
f, err := fs.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode)
if err != nil {
return err
}
n, err := f.Write(data)
if err == nil && n < len(data) {
err = io.ErrShortWrite
}
if err1 := f.Close(); err == nil {
err = err1
}
return err
}
// ReadDir reads the directory named by path and returns
// a list of directory entries sorted by filename.
func ReadDir(fs FileSystem, path string) ([]os.FileInfo, error) {
f, err := fs.Open(path)
if err != nil {
return nil, err
}
list, err := f.Readdir(-1)
f.Close()
if err != nil {
return nil, err
}
sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() })
return list, nil
}