-
Notifications
You must be signed in to change notification settings - Fork 2
/
fsnoti.go
101 lines (82 loc) · 1.91 KB
/
fsnoti.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
package sync
import (
"log"
"os"
"path/filepath"
"github.com/fsnotify/fsnotify"
)
func DirWatchStart() {
go func() {
for {
select {
case event, ok := <-Watcher.Events:
if !ok {
continue
}
//event file
path := event.Name
// lock mutex by hash value of file path
// using hash value is to reduce the number of mutex
if event.Op&fsnotify.Remove == fsnotify.Remove || event.Op&fsnotify.Rename == fsnotify.Rename {
log.Println("quics-client : REMOVE event ", path)
go PleaseSync(path)
continue
}
info, err := os.Stat(path)
if err != nil {
log.Println("quics-client : ", err)
continue
}
// continue this case, when PS happened by MS
if event.Op&fsnotify.Create == fsnotify.Create && info.IsDir() { // IsDirectory
DirWatchAdd(path)
continue
}
if event.Op&fsnotify.Create == fsnotify.Create && !info.IsDir() { // IsFile
log.Println("quics-client : CREATE event ", path)
go PleaseSync(path)
continue
}
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("quics-client : WRITE event ", path)
go PleaseSync(path)
continue
}
case err, ok := <-Watcher.Errors:
if !ok {
continue
}
log.Println("quics-cleint : ", err)
}
}
}()
}
func DirWatchAdd(rootpath string) {
err := filepath.Walk(rootpath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
err := Watcher.Add(path)
if err != nil {
log.Println("quics-client : Watcher Adding unsuccessful ", err)
}
}
return nil
})
if err != nil {
log.Println("quics-client : ", err)
}
}
func DirWatchStop(rootpath string) {
filepath.Walk(rootpath, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
log.Println("path >> ", path)
Watcher.Remove(path)
}
return nil
})
}
func WatchStop() {
Watcher.Close()
}