-
Notifications
You must be signed in to change notification settings - Fork 27
/
fswalk.go
182 lines (149 loc) · 4.24 KB
/
fswalk.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
package utils
import (
"fmt"
"os"
"path"
"path/filepath"
"sort"
"strings"
)
/*
* Example:
files, err = FilesFromRoot("./dir", func(dir string, name string, info os.FileInfo) bool {
return info.Mode()&0111 != 0
})
if err != nil {
fmt.Printf("FilesFromRoot: %v", err)
}
dirPaths = []string{}
for dirPath := range files {
dirPaths = append(dirPaths, dirPath)
}
sort.Strings(dirPaths)
for _, dirPath := range dirPaths {
fmt.Printf("%s\n", dirPath)
for file := range files[dirPath] {
fmt.Printf(" %s\n", file)
}
}
*/
// FilesFromRoot returns a map with path and array of files under it
func FilesFromRoot(root string, filterFn func(dir string, name string, info os.FileInfo) bool) (files map[string]map[string]string, err error) {
files = make(map[string]map[string]string)
symlinkedDirs, err := WalkSymlinks(root, "", files, filterFn)
if err != nil {
return nil, err
}
if len(symlinkedDirs) == 0 {
return files, nil
}
walkedSymlinks := map[string]string{}
// recurse list of symlinked directories
// limit depth to stop symlink loops
maxSymlinkDepth := 16
for {
maxSymlinkDepth--
if maxSymlinkDepth == 0 {
break
}
newSymlinkedDirs := map[string]string{}
for origPath, target := range symlinkedDirs {
symlinked, err := WalkSymlinks(target, origPath, files, filterFn)
if err != nil {
return nil, err
}
for k, v := range symlinked {
newSymlinkedDirs[k] = v
}
}
for k := range walkedSymlinks {
delete(newSymlinkedDirs, k)
}
if len(newSymlinkedDirs) == 0 {
break
}
symlinkedDirs = newSymlinkedDirs
}
return files, nil
}
func SymlinkInfo(path string, info os.FileInfo) (target string, isDir bool, err error) {
// return empty path if not a symlink
if info.Mode()&os.ModeSymlink == 0 {
return "", false, nil
}
// Eval symlink path and get stat of a target path
target, err = filepath.EvalSymlinks(path)
if err != nil {
return "", false, err
}
// is it file or dir?
targetInfo, err := os.Lstat(target)
if err != nil {
return "", false, err
}
return target, targetInfo.IsDir(), nil
}
// WalkSymlinks walks a directory, updates files map and returns symlinked directories
func WalkSymlinks(target string, linkName string, files map[string]map[string]string, filterFn func(dir string, name string, info os.FileInfo) bool) (symlinkedDirectories map[string]string, err error) {
symlinkedDirectories = map[string]string{}
err = filepath.Walk(target, func(foundPath string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("failure accessing a path '%s': %v\n", foundPath, err)
}
if info.IsDir() {
return nil
}
resPath := foundPath
if linkName != "" {
// replace target with linkName in foundPath
resPath = path.Join(linkName, strings.TrimPrefix(foundPath, target))
}
target, isDir, err := SymlinkInfo(foundPath, info)
if err != nil {
return err
}
if target != "" && isDir {
// symlink to directory -> save it for future listing
symlinkedDirectories[resPath] = target
return nil
}
// Walk found a file or symlink to file, just store it.
// FIXME symlink can have +x, but target file is not, so filterFn is not working properly
fDir := path.Dir(resPath)
fName := path.Base(resPath)
if filterFn == nil || filterFn(fDir, fName, info) {
if _, has := files[fDir]; !has {
files[fDir] = map[string]string{}
}
files[fDir][fName] = ""
}
return nil
})
if err != nil {
return nil, fmt.Errorf("walk symlinks dir %s: %v", target, err)
}
return symlinkedDirectories, nil
}
// FindExecutableFilesInPath returns a list of executable and a list of non-executable files in path
func FindExecutableFilesInPath(dir string) (executables []string, nonExecutables []string, err error) {
executables = make([]string, 0)
nonExecutables = make([]string, 0)
// Find only executable files
files, err := FilesFromRoot(dir, func(dir string, name string, info os.FileInfo) bool {
if info.Mode()&0111 != 0 {
return true
}
nonExecutables = append(nonExecutables, path.Join(dir, name))
return false
})
if err != nil {
return
}
for dirPath, filePaths := range files {
for file := range filePaths {
executables = append(executables, path.Join(dirPath, file))
}
}
sort.Strings(executables)
return
}