-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
99 lines (83 loc) · 2.6 KB
/
list.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
package main
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
)
func listFiles(dir string, recursive bool) ([]string, error) {
to_report := []string{}
err := filepath.WalkDir(dir, func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
is_dir := info.IsDir()
if is_dir {
if recursive || dir == path {
return nil
}
}
rel, err := filepath.Rel(dir, path)
if err != nil {
return err
}
if !recursive && is_dir {
to_report = append(to_report, rel + "/")
return fs.SkipDir
} else {
to_report = append(to_report, rel)
return nil
}
})
if err != nil {
return nil, fmt.Errorf("failed to obtain a directory listing; %w", err)
}
return to_report, nil
}
func listMetadata(dir string, base_names []string) (map[string]fs.FileInfo, []string, error) {
curcontents := map[string]fs.FileInfo{}
curfailures := []string{}
curnames := map[string]bool{}
for _, n := range base_names {
curnames[n] = true
}
// Just skip any directories that we can't access, no need to check the error.
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
curfailures = append(curfailures, fmt.Sprintf("failed to walk %q; %v", path, err))
return nil
}
if d.IsDir() {
base := filepath.Base(path)
if strings.HasPrefix(base, ".") {
return fs.SkipDir
} else {
return nil
}
}
if _, ok := curnames[filepath.Base(path)]; !ok {
return nil
}
var info fs.FileInfo
if d.Type() & os.ModeSymlink == 0 {
info, err = d.Info()
} else {
// Resolve any symbolic links to files at this point. This is important
// as it ensures that we include the target file's modification time in
// our index, so that the index is updated when the target is changed
// (rather than when the link itself is changed).
info, err = os.Stat(path)
if err == nil && info.IsDir() {
return nil
}
}
if err != nil {
curfailures = append(curfailures, fmt.Sprintf("failed to stat %q; %v", path, err))
return nil
}
curcontents[path] = info
return nil
})
return curcontents, curfailures, err
}