-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
57 lines (48 loc) · 1.15 KB
/
manager.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
package diplodocus
import (
"github.com/fsnotify/fsnotify"
"sync"
)
// FileManager is an entry point for module, it manipulates with files.
type FileManager struct {
watcher *fsnotify.Watcher
responders *ResponderMap
files map[string]*file
mutex sync.Mutex
}
// NewFileManager creates file manager with specified fs watcher.
func NewFileManager(watcher *fsnotify.Watcher) *FileManager {
return &FileManager{
watcher: watcher,
responders: newResponderMap(),
files: map[string]*file{},
}
}
// Watch monitors and processes fs events before it encounters an error.
func (m *FileManager) Watch() error {
for {
select {
case ev := <-m.watcher.Events:
files := m.responders.GetMappings(ev.Name)
for _, file := range files {
file.OnEvent(ev)
}
case err := <-m.watcher.Errors:
return err
}
}
}
// GetFile returns File object for specified fs path.
func (m *FileManager) GetFile(path string) (*file, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
if f, ok := m.files[path]; ok {
return f, nil
}
f, err := newFile(path, m.responders)
if err != nil {
return nil, err
}
m.files[path] = f
return f, nil
}