-
Notifications
You must be signed in to change notification settings - Fork 1
/
scan.go
273 lines (224 loc) · 6.39 KB
/
scan.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
package watch
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/windmilleng/fsnotify"
"github.com/windmilleng/wat/data"
"github.com/windmilleng/wat/data/db/dbint"
"github.com/windmilleng/wat/errors"
"github.com/windmilleng/wat/os/ospath"
)
var symlinkOutsideWorkspaceErr = fmt.Errorf("Symlink reaches outside workspace")
const defaultMaxFilesWatched = 1048576
type scanner struct {
root string
matcher *ospath.Matcher
// If a file is before the min mtime, we mark it ignored but still emit a trackedFile for it.
minMtime time.Time
watcher wmNotify
maxFiles int
}
func newScanner(root string, matcher *ospath.Matcher, watcher wmNotify, minMtime time.Time, max int) *scanner {
return &scanner{root: root, matcher: matcher, watcher: watcher, minMtime: minMtime, maxFiles: max}
}
func (s *scanner) Root() string {
return s.root
}
func (s *scanner) Matcher() *ospath.Matcher {
return s.matcher
}
// Returns
// string: The path relative to the watch root
// bool: We should track this if it's a file.
// bool: We should track this if it's a directory
func (s *scanner) relPath(path string) (string, bool, bool) {
relPath, err := filepath.Rel(s.root, path)
if err != nil {
return "", false, false
}
if relPath == "." {
return "", true, true
}
if filepath.IsAbs(relPath) || strings.HasPrefix(relPath, "..") {
return "", false, false
}
// Try to match this path
trackIfFile := s.matcher.Match(relPath)
// Check if we can match anything under this path.
child := s.matcher.Child(relPath)
trackIfDir := !child.Empty()
return relPath, trackIfFile, trackIfDir
}
type trackedFile struct {
absPath string
relPath string
mode os.FileMode
ignore bool
}
// Converts this into the common SnapshotFile format.
//
// If the file has been deleted since this struct was created,
// returns a os.IsNotExist error.
//
// If the file is a symlink to somewhere outside the workspace,
// returns a symlinkOutsideWorkspaceErr.
func (f *trackedFile) toSnapshotFile() (*dbint.SnapshotFile, error) {
var contents []byte
fileType := data.FileRegular
if f.mode.IsRegular() {
var err error
contents, err = ioutil.ReadFile(f.absPath)
if err != nil {
return nil, err
}
} else if isSymlink(f.mode) {
link, err := os.Readlink(f.absPath)
if err != nil {
return nil, err
}
if filepath.IsAbs(link) {
// For now, make all links relative when we convert them
// to a Windmill SnapshotFile. In the future, we might have some notion
// of an "absolute" link in the workspace.
link, err = filepath.EvalSymlinks(link)
if err != nil {
return nil, err
}
link, err = filepath.Rel(filepath.Dir(f.absPath), link)
if err != nil {
return nil, err
}
}
// Check to see if the link reaches outside the workspace.
relToWorkspace := filepath.Join(f.relPath, link)
if filepath.IsAbs(relToWorkspace) || strings.HasPrefix(relToWorkspace, ".") {
return nil, symlinkOutsideWorkspaceErr
}
contents = []byte(link)
fileType = data.FileSymlink
} else {
return nil, fmt.Errorf("toSnapshotFile: unexpected mode: %v", f.mode)
}
return &dbint.SnapshotFile{
Path: f.relPath,
Contents: data.NewBytesWithBacking(contents),
Executable: data.IsExecutableMode(f.mode),
Type: fileType,
}, nil
}
// helper for eventToUpdate
func handleStatError(relPath string, trackedIfFile bool, err error) (map[string]*trackedFile, error) {
if !os.IsNotExist(err) {
return nil, err
}
if !trackedIfFile {
return nil, nil
}
return map[string]*trackedFile{relPath: nil}, nil
}
func (s *scanner) eventToUpdate(event fsnotify.Event) (map[string]*trackedFile, error) {
path := event.Name
relPath, trackedIfFile, trackedIfDir := s.relPath(path)
if !trackedIfFile && !trackedIfDir {
return nil, nil
}
mode := os.FileMode(0)
st, err := os.Lstat(path)
if err != nil {
return handleStatError(relPath, trackedIfFile, err)
}
mode = st.Mode()
if isSymlink(mode) {
// Check to make sure the symlink is valid.
// Emacs creates deliberately broken symlinks as lock files.
_, err := os.Stat(path)
if err != nil {
return handleStatError(relPath, trackedIfFile, err)
}
}
if mode.IsDir() && event.Op&fsnotify.Create != 0 {
if !trackedIfDir {
return nil, nil
}
return s.scanNewDir(path)
}
if isScannableFileMode(mode) {
if !trackedIfFile {
return nil, nil
}
f := &trackedFile{absPath: path, relPath: relPath, mode: mode}
return map[string]*trackedFile{relPath: f}, nil
}
return nil, nil
}
// Walks a directory recursively and sets up fs watches.
// Returns all the paths we added.
func (s *scanner) scanNewDir(path string) (map[string]*trackedFile, error) {
files := make(map[string]*trackedFile)
err := filepath.Walk(path, func(innerPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
relPath, trackedIfFile, trackedIfDir := s.relPath(innerPath)
if !trackedIfFile && !trackedIfDir {
return nil
}
mode := info.Mode()
if isSymlink(mode) {
// Check to make sure the symlink is valid.
// Emacs creates deliberately broken symlinks as lock files.
_, err := os.Stat(innerPath)
if err != nil {
if !os.IsNotExist(err) {
return err
}
if trackedIfFile {
files[relPath] = nil
}
return nil
}
}
if mode.IsDir() && !trackedIfDir {
return filepath.SkipDir
} else if !mode.IsDir() && !trackedIfFile {
return nil
}
if s.watcher != nil && mode.IsDir() {
// Dirs capture file events directly in the directively, but
// not recursively in inner directories, so we need to watch each
// inner directory.
err = s.watcher.Add(innerPath)
if err != nil {
return errors.Propagatef(err, "scanNewDir")
}
}
if isScannableFileMode(mode) {
f := &trackedFile{absPath: innerPath, relPath: relPath, mode: mode}
// minMtime tracking is just an optimization, so we should only
// ignore file that are strictly before the minMtime.
if info.ModTime().Before(s.minMtime) {
f.ignore = true
}
files[relPath] = f
}
return nil
})
if err != nil {
return nil, err
}
lenFiles := len(files)
if lenFiles > s.maxFiles {
return nil, fmt.Errorf("trying to watch too many files. Max is %d but tried to watch %d.", s.maxFiles, lenFiles)
}
return files, nil
}
func isScannableFileMode(mode os.FileMode) bool {
return mode.IsRegular() || isSymlink(mode)
}
func isSymlink(mode os.FileMode) bool {
return (mode & os.ModeSymlink) == os.ModeSymlink
}