-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirs.go
75 lines (70 loc) · 2.08 KB
/
dirs.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
package fs
import (
"context"
"errors"
"os"
"os/user"
)
// HomeDir returns the home directory of the current user.
func HomeDir() File {
u, err := user.Current()
if err != nil {
return InvalidFile
}
return File(u.HomeDir)
}
// CurrentWorkingDir returns the current working directory of the process.
// In case of an erorr, Exists() of the result File will return false.
func CurrentWorkingDir() File {
cwd, _ := os.Getwd()
return File(cwd)
}
// listDirMaxImpl implements the ListDirMax method functionality by calling listDir.
// It returns the passed max number of files or an unlimited number if max is < 0.
// FileSystem implementations can use this function to implement ListDirMax,
// if an own, specialized implementation doesn't make sense.
func listDirMaxImpl(ctx context.Context, max int, listDir func(ctx context.Context, callback func(File) error) error) (files []File, err error) {
if ctx.Err() != nil {
return nil, ctx.Err()
}
if max == 0 {
return nil, nil
}
done := errors.New("done") // used as an internal flag, won't be returned
err = listDir(ctx, func(file File) error {
if max >= 0 && len(files) >= max {
return done
}
if files == nil {
// Reserve space for files
if max < 0 {
files = make([]File, 0, 32)
} else {
files = make([]File, 0, max)
}
}
files = append(files, file)
return nil
})
if err != nil && !errors.Is(err, done) {
return nil, err
}
return files, nil
}
// // ListDirRecursiveImpl can be used by FileSystem implementations to
// // implement FileSystem.ListDirRecursive if it doesn't have an internal
// // optimzed form of doing that.
// func ListDirRecursiveImpl(fs FileSystem, dirPath string, callback func(File) error, patterns []string) error {
// return fs.ListDir(dirPath, func(f File) error {
// if f.IsDir() {
// err := f.ListDirRecursive(callback, patterns...)
// // Don't mind files that have been deleted while iterating
// return RemoveErrDoesNotExist(err)
// }
// match, err := fs.MatchAnyPattern(f.Name(), patterns)
// if match {
// err = callback(f)
// }
// return err
// }, nil)
// }