forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fsnotification.go
54 lines (47 loc) · 1.33 KB
/
fsnotification.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
package fsnotification
import (
"fmt"
"os"
"path/filepath"
"github.com/golang/glog"
"github.com/fsnotify/fsnotify"
)
// AddRecursiveWatch handles adding watches recursively for the path provided
// and its subdirectories. If a non-directory is specified, this call is a no-op.
// Recursive logic from https://github.com/bronze1man/kmg/blob/master/fsnotify/Watcher.go
func AddRecursiveWatch(watcher *fsnotify.Watcher, path string) error {
file, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("error introspecting path %s: %v", path, err)
}
if !file.IsDir() {
return nil
}
folders, err := getSubFolders(path)
for _, v := range folders {
glog.V(5).Infof("adding watch on path %s", v)
err = watcher.Add(v)
if err != nil {
// "no space left on device" issues are usually resolved via
// $ sudo sysctl fs.inotify.max_user_watches=65536
return fmt.Errorf("error adding watcher for path %s: %v", v, err)
}
}
return nil
}
// getSubFolders recursively retrieves all subfolders of the specified path.
func getSubFolders(path string) (paths []string, err error) {
err = filepath.Walk(path, func(newPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
paths = append(paths, newPath)
}
return nil
})
return paths, err
}