/
fsnotify.go
100 lines (83 loc) · 2.81 KB
/
fsnotify.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
// Copyright 2017 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fsnotify
import (
"errors"
"fmt"
"strings"
"github.com/fsnotify/fsnotify"
"github.com/bazelbuild/bazel-watcher/ibazel/fswatcher/common"
)
// We have to declare our own partial interface in order to mock it out in test
// as the real struct varies from platform to platform
type fsNotifyWatcher interface {
Add(name string) error
Remove(name string) error
Close() error
Events() chan fsnotify.Event
}
type fsNotifyWatcherWrapper struct {
watcher *fsnotify.Watcher
}
func (w *fsNotifyWatcherWrapper) Add(name string) error { return w.watcher.Add(name) }
func (w *fsNotifyWatcherWrapper) Remove(name string) error { return w.watcher.Remove(name) }
func (w *fsNotifyWatcherWrapper) Close() error { return w.watcher.Close() }
func (w *fsNotifyWatcherWrapper) Events() chan fsnotify.Event { return w.watcher.Events }
type realFSNotifyWatcher struct {
watched map[string]struct{}
wrapper fsNotifyWatcher
}
var _ common.Watcher = &realFSNotifyWatcher{}
// UpdateAll implements ibazel/fswatcher/common.Watcher
func (w *realFSNotifyWatcher) UpdateAll(names []string) error {
var errs []string
prev_watched := w.watched
new_watched := make(map[string]struct{}, len(names))
for _, name := range names {
new_watched[name] = struct{}{}
_, ok := prev_watched[name]
if ok {
delete(w.watched, name)
} else {
err := w.wrapper.Add(name)
if err != nil {
errs = append(errs, fmt.Sprintf("Error watching file %q error: %v", name, err))
}
}
}
for name, _ := range prev_watched {
err := w.wrapper.Remove(name)
if err != nil {
errs = append(errs, fmt.Sprintf("Error unwatching file %q error: %v\n", name, err))
}
}
w.watched = new_watched
if len(errs) > 0 {
return errors.New(strings.Join(errs, "\n"))
}
return nil
}
// Close implements ibazel/fswatcher/common.Watcher
func (w *realFSNotifyWatcher) Close() error {
return w.wrapper.Close()
}
// Events implements ibazel/fswatcher/common.Watcher
func (w *realFSNotifyWatcher) Events() chan common.Event {
return w.wrapper.Events()
}
func NewWatcher() (common.Watcher, error) {
watcher, err := fsnotify.NewWatcher()
wrapper := &fsNotifyWatcherWrapper{watcher: watcher}
return &realFSNotifyWatcher{wrapper: wrapper}, err
}