-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwatcher.go
More file actions
45 lines (41 loc) · 875 Bytes
/
watcher.go
File metadata and controls
45 lines (41 loc) · 875 Bytes
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
package main
import (
"io/ioutil"
"path/filepath"
)
type dirWatcher struct {
folder string
files map[string]struct{}
}
func newDirWatcher(folder string) *dirWatcher {
if folder != "" {
return &dirWatcher{folder, make(map[string]struct{})}
}
return nil
}
func (w *dirWatcher) check() (added, removed []string, err error) {
files := make(map[string]struct{})
info, err := ioutil.ReadDir(w.folder)
if err != nil {
return
}
for _, fi := range info {
if fi.IsDir() {
continue
}
files[fi.Name()] = struct{}{}
}
for name := range w.files {
if _, present := files[name]; !present {
removed = append(removed, filepath.Join(w.folder, name))
delete(w.files, name)
}
}
for name := range files {
if _, present := w.files[name]; !present {
w.files[name] = struct{}{}
added = append(added, filepath.Join(w.folder, name))
}
}
return
}