-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpath.go
657 lines (559 loc) · 18.7 KB
/
path.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
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
package pathlib
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
"github.com/spf13/afero"
)
// Path is an object that represents a path
type Path struct {
path string
fs afero.Fs
// DefaultFileMode is the mode that is used when creating new files in functions
// that do not accept os.FileMode as a parameter.
DefaultFileMode os.FileMode
// DefaultDirMode is the mode that will be used when creating new directories
DefaultDirMode os.FileMode
// Sep is the seperator used in path calculations. By default this is set to
// os.PathSeparator.
Sep string
}
type PathOpts func(p *Path)
func PathWithAfero(fs afero.Fs) PathOpts {
return func(p *Path) {
p.fs = fs
}
}
func PathWithSeperator(sep string) PathOpts {
return func(p *Path) {
p.Sep = sep
}
}
// NewPath returns a new OS path
func NewPath(path string, opts ...PathOpts) *Path {
p := &Path{
path: path,
fs: afero.NewOsFs(),
DefaultFileMode: DefaultFileMode,
DefaultDirMode: DefaultDirMode,
Sep: string(os.PathSeparator),
}
for _, opt := range opts {
opt(p)
}
return p
}
// NewPathAfero returns a Path object with the given Afero object
//
// Deprecated: Use the PathWithAfero option in Newpath instead.
func NewPathAfero(path string, fs afero.Fs) *Path {
return NewPath(path, PathWithAfero(fs))
}
// Glob returns all of the path objects matched by the given pattern
// inside of the afero filesystem.
func Glob(fs afero.Fs, pattern string) ([]*Path, error) {
matches, err := afero.Glob(fs, pattern)
if err != nil {
return nil, fmt.Errorf("failed to glob: %w", err)
}
pathMatches := []*Path{}
for _, match := range matches {
pathMatches = append(pathMatches, NewPathAfero(match, fs))
}
return pathMatches, nil
}
type namer interface {
Name() string
}
func getFsName(fs afero.Fs) string {
if name, ok := fs.(namer); ok {
return name.Name()
}
return ""
}
// Fs returns the internal afero.Fs object.
func (p *Path) Fs() afero.Fs {
return p.fs
}
func (p *Path) doesNotImplementErr(interfaceName string) error {
return doesNotImplementErr(interfaceName, p.Fs())
}
func doesNotImplementErr(interfaceName string, fs afero.Fs) error {
return fmt.Errorf("%w: Path's afero filesystem %s does not implement %s", ErrDoesNotImplement, getFsName(fs), interfaceName)
}
func (p *Path) lstatNotPossible() error {
return lstatNotPossible(p.Fs())
}
func lstatNotPossible(fs afero.Fs) error {
return fmt.Errorf("%w: Path's afero filesystem %s does not support lstat", ErrLstatNotPossible, getFsName(fs))
}
// *******************************
// * afero.Fs wrappers *
// *******************************
// Create creates a file if possible, returning the file and an error, if any happens.
func (p *Path) Create() (File, error) {
file, err := p.Fs().Create(p.String())
return File{file}, err
}
// Mkdir makes the current dir. If the parents don't exist, an error
// is returned.
func (p *Path) Mkdir() error {
return p.Fs().Mkdir(p.String(), p.DefaultDirMode)
}
// MkdirMode makes the current dir. If the parents don't exist, an error
// is returned.
func (p *Path) MkdirMode(perm os.FileMode) error {
return p.Fs().Mkdir(p.String(), perm)
}
// MkdirAll makes all of the directories up to, and including, the given path.
func (p *Path) MkdirAll() error {
return p.Fs().MkdirAll(p.String(), p.DefaultDirMode)
}
// MkdirAllMode makes all of the directories up to, and including, the given path.
func (p *Path) MkdirAllMode(perm os.FileMode) error {
return p.Fs().MkdirAll(p.String(), perm)
}
// Open opens a file for read-only, returning it or an error, if any happens.
func (p *Path) Open() (*File, error) {
handle, err := p.Fs().Open(p.String())
return &File{
File: handle,
}, err
}
// OpenFile opens a file using the given flags.
// See the list of flags at: https://golang.org/pkg/os/#pkg-constants
func (p *Path) OpenFile(flag int) (*File, error) {
handle, err := p.Fs().OpenFile(p.String(), flag, p.DefaultFileMode)
return &File{
File: handle,
}, err
}
// OpenFileMode opens a file using the given flags and the given mode.
// See the list of flags at: https://golang.org/pkg/os/#pkg-constants
func (p *Path) OpenFileMode(flag int, perm os.FileMode) (*File, error) {
handle, err := p.Fs().OpenFile(p.String(), flag, perm)
return &File{
File: handle,
}, err
}
// Remove removes a file, returning an error, if any
// happens.
func (p *Path) Remove() error {
return p.Fs().Remove(p.String())
}
// RemoveAll removes the given path and all of its children.
func (p *Path) RemoveAll() error {
return p.Fs().RemoveAll(p.String())
}
// RenameStr renames a file
func (p *Path) RenameStr(newname string) error {
if err := p.Fs().Rename(p.String(), newname); err != nil {
return err
}
// Rename succeeded. Set our path to the newname.
p.path = newname
return nil
}
// Rename renames a file
func (p *Path) Rename(target *Path) error {
return p.RenameStr(target.String())
}
// Stat returns the os.FileInfo of the given path
func (p *Path) Stat() (os.FileInfo, error) {
return p.Fs().Stat(p.String())
}
// Chmod changes the file mode of the given path
func (p *Path) Chmod(mode os.FileMode) error {
return p.Fs().Chmod(p.String(), mode)
}
// Chtimes changes the modification and access time of the given path.
func (p *Path) Chtimes(atime time.Time, mtime time.Time) error {
return p.Fs().Chtimes(p.String(), atime, mtime)
}
// ************************
// * afero.Afero wrappers *
// ************************
// DirExists returns whether or not the path represents a directory that exists
func (p *Path) DirExists() (bool, error) {
return afero.DirExists(p.Fs(), p.String())
}
// Exists returns whether the path exists
func (p *Path) Exists() (bool, error) {
return afero.Exists(p.Fs(), p.String())
}
// FileContainsAnyBytes returns whether or not the path contains
// any of the listed bytes.
func (p *Path) FileContainsAnyBytes(subslices [][]byte) (bool, error) {
return afero.FileContainsAnyBytes(p.Fs(), p.String(), subslices)
}
// FileContainsBytes returns whether or not the given file contains the bytes
func (p *Path) FileContainsBytes(subslice []byte) (bool, error) {
return afero.FileContainsBytes(p.Fs(), p.String(), subslice)
}
// IsDir checks if a given path is a directory.
func (p *Path) IsDir() (bool, error) {
return afero.IsDir(p.Fs(), p.String())
}
// IsDir returns whether or not the os.FileMode object represents a
// directory.
func IsDir(mode os.FileMode) bool {
return mode.IsDir()
}
// IsEmpty checks if a given file or directory is empty.
func (p *Path) IsEmpty() (bool, error) {
return afero.IsEmpty(p.Fs(), p.String())
}
// ReadDir reads the current path and returns a list of the corresponding
// Path objects. This function differs from os.Readdir in that it does
// not call Stat() on the files. Instead, it calls Readdirnames which
// is less expensive and does not force the caller to make expensive
// Stat calls.
func (p *Path) ReadDir() ([]*Path, error) {
paths := []*Path{}
handle, err := p.Open()
if err != nil {
return paths, err
}
children, err := handle.Readdirnames(-1)
if err != nil {
return paths, err
}
for _, child := range children {
paths = append(paths, p.Join(child))
}
return paths, err
}
// ReadFile reads the given path and returns the data. If the file doesn't exist
// or is a directory, an error is returned.
func (p *Path) ReadFile() ([]byte, error) {
return afero.ReadFile(p.Fs(), p.String())
}
// SafeWriteReader is the same as WriteReader but checks to see if file/directory already exists.
func (p *Path) SafeWriteReader(r io.Reader) error {
return afero.SafeWriteReader(p.Fs(), p.String(), r)
}
// WriteFileMode writes the given data to the path (if possible). If the file exists,
// the file is truncated. If the file is a directory, or the path doesn't exist,
// an error is returned.
func (p *Path) WriteFileMode(data []byte, perm os.FileMode) error {
return afero.WriteFile(p.Fs(), p.String(), data, perm)
}
// WriteFile writes the given data to the path (if possible). If the file exists,
// the file is truncated. If the file is a directory, or the path doesn't exist,
// an error is returned.
func (p *Path) WriteFile(data []byte) error {
return afero.WriteFile(p.Fs(), p.String(), data, p.DefaultFileMode)
}
// WriteReader takes a reader and writes the content
func (p *Path) WriteReader(r io.Reader) error {
return afero.WriteReader(p.Fs(), p.String(), r)
}
// *************************************
// * pathlib.Path-like implementations *
// *************************************
// Name returns the string representing the final path component
func (p *Path) Name() string {
return filepath.Base(p.path)
}
// Parent returns the Path object of the parent directory
func (p *Path) Parent() *Path {
return NewPathAfero(filepath.Dir(p.String()), p.Fs())
}
// Readlink returns the target path of a symlink.
//
// This will fail if the underlying afero filesystem does not implement
// afero.LinkReader.
func (p *Path) Readlink() (*Path, error) {
linkReader, ok := p.Fs().(afero.LinkReader)
if !ok {
return nil, p.doesNotImplementErr("afero.LinkReader")
}
resolvedPathStr, err := linkReader.ReadlinkIfPossible(p.path)
if err != nil {
return nil, err
}
return NewPathAfero(resolvedPathStr, p.fs), nil
}
func resolveIfSymlink(path *Path) (*Path, bool, error) {
isSymlink, err := path.IsSymlink()
if err != nil {
return path, isSymlink, err
}
if isSymlink {
resolvedPath, err := path.Readlink()
if err != nil {
// Return the path unchanged on errors
return path, isSymlink, err
}
return resolvedPath, isSymlink, nil
}
return path, isSymlink, nil
}
func resolveAllHelper(path *Path) (*Path, error) {
parts := path.Parts()
for i := 0; i < len(parts); i++ {
rightOfComponent := parts[i+1:]
upToComponent := parts[:i+1]
componentPath := NewPathAfero(strings.Join(upToComponent, path.Sep), path.Fs())
resolved, isSymlink, err := resolveIfSymlink(componentPath)
if err != nil {
return path, err
}
if isSymlink {
if resolved.IsAbsolute() {
return resolveAllHelper(resolved.Join(strings.Join(rightOfComponent, path.Sep)))
}
return resolveAllHelper(componentPath.Parent().JoinPath(resolved).Join(rightOfComponent...))
}
}
// If we get through the entire iteration above, that means no component was a symlink.
// Return the argument.
return path, nil
}
// ResolveAll canonicalizes the path by following every symlink in
// every component of the given path recursively. The behavior
// should be identical to the `readlink -f` command from POSIX OSs.
// This will fail if the underlying afero filesystem does not implement
// afero.LinkReader. The path will be returned unchanged on errors.
func (p *Path) ResolveAll() (*Path, error) {
return resolveAllHelper(p)
}
// Parts returns the individual components of a path
func (p *Path) Parts() []string {
parts := []string{}
if p.IsAbsolute() {
parts = append(parts, p.Sep)
}
normalizedPathStr := normalizePathString(p.String())
normalizedParts := normalizePathParts(strings.Split(normalizedPathStr, p.Sep))
return append(parts, normalizedParts...)
}
// IsAbsolute returns whether or not the path is an absolute path. This is
// determined by checking if the path starts with a slash.
func (p *Path) IsAbsolute() bool {
return strings.HasPrefix(p.path, "/")
}
// Join joins the current object's path with the given elements and returns
// the resulting Path object.
func (p *Path) Join(elems ...string) *Path {
paths := []string{p.path}
paths = append(paths, elems...)
return NewPathAfero(strings.Join(paths, p.Sep), p.Fs())
}
// JoinPath is the same as Join() except it accepts a path object
func (p *Path) JoinPath(path *Path) *Path {
return p.Join(path.Parts()...)
}
func normalizePathString(path string) string {
path = strings.TrimSpace(path)
path = strings.TrimPrefix(path, "./")
path = strings.TrimRight(path, " ")
if len(path) > 1 {
path = strings.TrimSuffix(path, "/")
}
return path
}
func normalizePathParts(path []string) []string {
// We might encounter cases where path represents a split of the path
// "///" etc. We will get a bunch of erroneous empty strings in such a split,
// so remove all of the trailing empty strings except for the first one (if any)
normalized := []string{}
for i := 0; i < len(path); i++ {
if path[i] != "" {
normalized = append(normalized, path[i])
}
}
return normalized
}
// RelativeTo computes a relative version of path to the other path. For instance,
// if the object is /path/to/foo.txt and you provide /path/ as the argment, the
// returned Path object will represent to/foo.txt.
func (p *Path) RelativeTo(other *Path) (*Path, error) {
thisPathNormalized := normalizePathString(p.String())
otherPathNormalized := normalizePathString(other.String())
thisParts := p.Parts()
otherParts := other.Parts()
var relativeBase int
for idx, part := range otherParts {
if idx >= len(thisParts) || thisParts[idx] != part {
return p, fmt.Errorf("%s does not start with %s: %w", thisPathNormalized, otherPathNormalized, ErrRelativeTo)
}
relativeBase = idx
}
relativePath := thisParts[relativeBase+1:]
if len(relativePath) == 0 || (len(relativePath) == 1 && relativePath[0] == "") {
relativePath = []string{"."}
}
return NewPathAfero(strings.Join(relativePath, "/"), p.Fs()), nil
}
// RelativeToStr computes a relative version of path to the other path. For instance,
// if the object is /path/to/foo.txt and you provide /path/ as the argment, the
// returned Path object will represent to/foo.txt.
func (p *Path) RelativeToStr(other string) (*Path, error) {
return p.RelativeTo(NewPathAfero(other, p.Fs()))
}
// Lstat lstat's the path if the underlying afero filesystem supports it. If
// the filesystem does not support afero.Lstater, or if the filesystem implements
// afero.Lstater but returns false for the "lstat called" return value.
//
// A nil os.FileInfo is returned on errors.
func (p *Path) Lstat() (os.FileInfo, error) {
lStater, ok := p.Fs().(afero.Lstater)
if !ok {
return nil, p.doesNotImplementErr("afero.Lstater")
}
stat, lstatCalled, err := lStater.LstatIfPossible(p.String())
if !lstatCalled && err == nil {
return nil, p.lstatNotPossible()
}
return stat, err
}
// SymlinkStr symlinks to the target location. This will fail if the underlying
// afero filesystem does not implement afero.Linker.
func (p *Path) SymlinkStr(target string) error {
return p.Symlink(NewPathAfero(target, p.Fs()))
}
// Symlink symlinks to the target location. This will fail if the underlying
// afero filesystem does not implement afero.Linker.
func (p *Path) Symlink(target *Path) error {
symlinker, ok := p.fs.(afero.Linker)
if !ok {
return p.doesNotImplementErr("afero.Linker")
}
return symlinker.SymlinkIfPossible(target.path, p.path)
}
// String returns the string representation of the path
func (p *Path) String() string {
return p.path
}
// IsFile returns true if the given path is a file.
func (p *Path) IsFile() (bool, error) {
fileInfo, err := p.Stat()
if err != nil {
return false, err
}
return IsFile(fileInfo.Mode()), nil
}
// IsFile returns whether or not the file described by the given
// os.FileMode is a regular file.
func IsFile(mode os.FileMode) bool {
return mode.IsRegular()
}
// IsSymlink returns true if the given path is a symlink.
// Fails if the filesystem doesn't implement afero.Lstater.
func (p *Path) IsSymlink() (bool, error) {
fileInfo, err := p.Lstat()
if err != nil {
return false, err
}
return IsSymlink(fileInfo.Mode()), nil
}
// IsSymlink returns true if the file described by the given
// os.FileMode describes a symlink.
func IsSymlink(mode os.FileMode) bool {
return mode&os.ModeSymlink != 0
}
// DeepEquals returns whether or not the path pointed to by other
// has the same resolved filepath as self.
func (p *Path) DeepEquals(other *Path) (bool, error) {
selfResolved, err := p.ResolveAll()
if err != nil {
return false, err
}
otherResolved, err := other.ResolveAll()
if err != nil {
return false, err
}
return selfResolved.Clean().Equals(otherResolved.Clean()), nil
}
// Equals returns whether or not the object's path is identical
// to other's, in a shallow sense. It simply checks for equivalence
// in the unresolved Paths() of each object.
func (p *Path) Equals(other *Path) bool {
return p.String() == other.String()
}
// GetLatest returns the file or directory that has the most recent mtime. Only
// works if this path is a directory and it exists. If the directory is empty,
// the returned Path object will be nil.
func (p *Path) GetLatest() (*Path, error) {
files, err := p.ReadDir()
if err != nil {
return nil, err
}
var greatestFileSeen *Path
for _, file := range files {
if greatestFileSeen == nil {
greatestFileSeen = p.Join(file.Name())
}
greatestMtime, err := greatestFileSeen.Mtime()
if err != nil {
return nil, err
}
thisMtime, err := file.Mtime()
// There is a possible race condition where the file is deleted after
// our call to ReadDir. We throw away the error if it isn't
// os.ErrNotExist
if err != nil && !os.IsNotExist(err) {
return nil, err
}
if thisMtime.After(greatestMtime) {
greatestFileSeen = p.Join(file.Name())
}
}
return greatestFileSeen, nil
}
// Glob returns all matches of pattern relative to this object's path.
func (p *Path) Glob(pattern string) ([]*Path, error) {
return Glob(p.Fs(), p.Join(pattern).String())
}
// Clean returns a new object that is a lexically-cleaned
// version of Path.
func (p *Path) Clean() *Path {
return NewPathAfero(filepath.Clean(p.String()), p.Fs())
}
// Mtime returns the modification time of the given path.
func (p *Path) Mtime() (time.Time, error) {
stat, err := p.Stat()
if err != nil {
return time.Time{}, err
}
return Mtime(stat)
}
// Copy copies the path to another path using io.Copy.
// Returned is the number of bytes copied and any error values.
// The destination file is truncated if it exists, and is created
// if it does not exist.
func (p *Path) Copy(other *Path) (int64, error) {
srcFile, err := p.Open()
if err != nil {
return 0, fmt.Errorf("opening source file: %w", err)
}
defer srcFile.Close()
dstFile, err := other.OpenFile(os.O_TRUNC | os.O_CREATE | os.O_WRONLY)
if err != nil {
return 0, fmt.Errorf("opening destination file: %w", err)
}
defer dstFile.Close()
return io.Copy(dstFile, srcFile)
}
// Mtime returns the mtime described in the given os.FileInfo object
func Mtime(fileInfo os.FileInfo) (time.Time, error) {
return fileInfo.ModTime(), nil
}
// Size returns the size of the object. Fails if the object doesn't exist.
func (p *Path) Size() (int64, error) {
stat, err := p.Stat()
if err != nil {
return 0, err
}
return Size(stat), nil
}
// Size returns the size described by the os.FileInfo. Before you say anything,
// yes... you could just do fileInfo.Size(). This is purely a convenience function
// to create API consistency.
func Size(fileInfo os.FileInfo) int64 {
return fileInfo.Size()
}