-
Notifications
You must be signed in to change notification settings - Fork 19
/
walk.go
70 lines (66 loc) · 1.39 KB
/
walk.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
package objectcache
import (
"os"
"path"
"sort"
)
func validatePath(fileName string) bool {
for _, char := range fileName {
if (char >= '0' && char <= '9') || (char >= 'a' && char <= 'f') {
continue
}
return false
}
return true
}
func addCacheEntry(fileName string, cache ObjectCache) (ObjectCache, error) {
hash, err := filenameToHash(fileName)
if err != nil {
return nil, err
}
return append(cache, hash), nil
}
func scanObjectCache(cacheDirectoryName string, subpath string,
cache ObjectCache) (ObjectCache, error) {
myPathName := path.Join(cacheDirectoryName, subpath)
file, err := os.Open(myPathName)
if err != nil {
return nil, err
}
names, err := file.Readdirnames(-1)
file.Close()
if err != nil {
return nil, err
}
sort.Strings(names)
for _, name := range names {
lastChar := name[len(name)-1]
if lastChar == '~' || lastChar == '^' {
continue
}
pathname := path.Join(myPathName, name)
if !validatePath(name) {
if err := os.RemoveAll(pathname); err != nil {
return nil, err
}
continue
}
fi, err := os.Lstat(pathname)
if err != nil {
continue
}
filename := path.Join(subpath, name)
if fi.IsDir() {
cache, err = scanObjectCache(cacheDirectoryName, filename, cache)
if err != nil {
return nil, err
}
} else {
cache, err = addCacheEntry(filename, cache)
if err != nil {
return nil, err
}
}
}
return cache, nil
}