-
Notifications
You must be signed in to change notification settings - Fork 9
/
local.go
158 lines (132 loc) · 3 KB
/
local.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
package local
// local.go implements a watcher for the local fileystem via fsnotify.
import (
"fmt"
"io/ioutil"
"sync"
"github.com/johnsiilver/golib/filewatcher"
"github.com/fsnotify/fsnotify"
log "github.com/golang/glog"
)
var dispatch *dispatcher
type dispatcher struct {
watcher *fsnotify.Watcher
routes map[string]chan struct{}
closer chan struct{}
count int
sync.Mutex
}
func newDispatcher() (*dispatcher, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
d := &dispatcher{watcher: watcher, closer: make(chan struct{}), routes: map[string]chan struct{}{}}
go d.listen()
return d, nil
}
func (d *dispatcher) listen() {
for {
select {
case event := <-d.watcher.Events:
d.Lock()
defer d.Unlock()
d.routes[event.Name]<-struct{}{}
case err := <-d.watcher.Errors:
log.Errorf("problem with filewatcher: %s", err)
case <-d.closer:
return
}
}
}
func (d *dispatcher) close() {
close(d.closer)
}
func (d *dispatcher) add(f string, route chan struct{}) error {
d.Lock()
defer d.Unlock()
if _, ok := d.routes[f]; ok {
return fmt.Errorf("file %q is already watched", f)
}
if err := d.watcher.Add(f); err != nil {
return err
}
d.count++
d.routes[f] = route
return nil
}
func (d *dispatcher) remove(f string) error {
d.Lock()
defer d.Unlock()
if _, ok := d.routes[f]; !ok {
return fmt.Errorf("file %q was not being watched", f)
}
if err := d.watcher.Remove(f); err != nil {
return err
}
d.count--
close(d.routes[f])
delete(d.routes, f)
return nil
}
// local implements watcher for the local file system.
type local struct {
file string
running bool
disUpdates chan struct{}
fileContent chan []byte
close, stopped chan struct{}
sync.Mutex
}
// Watch implements filewatcher.Watcher.Watch().
func (l *local) File(f string, i interface{}) (chan []byte, error) {
l.Lock()
defer l.Unlock()
if l.running {
return nil, fmt.Errorf("file watcher already running")
}
l.file = f
b, err := ioutil.ReadFile(f)
if err != nil {
return nil, fmt.Errorf("problem reading file %q: %s", f, err)
}
ch := make(chan []byte, 1)
dCh := make(chan struct{}, 1)
l.fileContent = ch
l.disUpdates = dCh
l.stopped = make(chan struct{})
ch <- b
if err := dispatch.add(f, l.disUpdates); err != nil {
return nil, err
}
go l.listen()
return ch, nil
}
func (l *local) listen() {
for _ = range l.disUpdates {
log.Infof("saw event")
b, err := ioutil.ReadFile(l.file)
if err != nil {
log.Error(err)
continue
}
l.fileContent <-b
}
close(l.stopped)
}
// Close implements filewatcher.Watcher.Close().
func (l *local) Close() {
l.Lock()
defer l.Unlock()
l.running = false
close(l.disUpdates)
<-l.stopped
}
func init() {
var err error
dispatch, err = newDispatcher()
if err != nil {
panic(err)
}
filewatcher.Register(filewatcher.Local, func()filewatcher.Watch{return &local{}})
}