@@ -108,28 +108,10 @@ func remove(winfo *watchInfo) error {
108
108
delete (shared .done , winfo .fname )
109
109
close (done )
110
110
}
111
-
112
- fname := winfo .fname
113
- if winfo .isCreate () {
114
- // Watch for new files to be created in the parent directory.
115
- fname = filepath .Dir (fname )
116
- }
117
- shared .watchNums [fname ]--
118
- watchNum := shared .watchNums [fname ]
119
- if watchNum == 0 {
120
- delete (shared .watchNums , fname )
121
- }
122
111
shared .mux .Unlock ()
123
112
124
- // If we were the last ones to watch this file, unsubscribe from inotify.
125
- // This needs to happen after releasing the lock because fsnotify waits
126
- // synchronously for the kernel to acknowledge the removal of the watch
127
- // for this file, which causes us to deadlock if we still held the lock.
128
- if watchNum == 0 {
129
- return shared .watcher .Remove (fname )
130
- }
131
113
shared .remove <- winfo
132
- return nil
114
+ return <- shared . error
133
115
}
134
116
135
117
// Events returns a channel to which FileEvents corresponding to the input filename
@@ -166,47 +148,48 @@ func (shared *InotifyTracker) addWatch(winfo *watchInfo) error {
166
148
fname = filepath .Dir (fname )
167
149
}
168
150
151
+ var err error
169
152
// already in inotify watch
170
- if shared .watchNums [fname ] > 0 {
171
- shared .watchNums [fname ]++
172
- if winfo .isCreate () {
173
- shared .watchNums [winfo .fname ]++
174
- }
175
- return nil
176
- }
177
-
178
- err := shared .watcher .Add (fname )
179
- if err == nil {
180
- shared .watchNums [fname ]++
181
- if winfo .isCreate () {
182
- shared .watchNums [winfo .fname ]++
183
- }
153
+ if shared .watchNums [fname ] == 0 {
154
+ err = shared .watcher .Add (fname )
184
155
}
156
+ shared .watchNums [fname ]++
185
157
return err
186
158
}
187
159
188
160
// removeWatch calls fsnotify.RemoveWatch for the input filename and closes the
189
161
// corresponding events channel.
190
- func (shared * InotifyTracker ) removeWatch (winfo * watchInfo ) {
162
+ func (shared * InotifyTracker ) removeWatch (winfo * watchInfo ) error {
191
163
shared .mux .Lock ()
192
- defer shared .mux .Unlock ()
193
164
194
165
ch := shared .chans [winfo .fname ]
195
- if ch == nil {
196
- return
166
+ if ch != nil {
167
+ delete (shared .chans , winfo .fname )
168
+ close (ch )
197
169
}
198
170
199
- delete (shared .chans , winfo .fname )
200
- close (ch )
201
-
202
- if ! winfo .isCreate () {
203
- return
171
+ fname := winfo .fname
172
+ if winfo .isCreate () {
173
+ // Watch for new files to be created in the parent directory.
174
+ fname = filepath .Dir (fname )
175
+ }
176
+ shared .watchNums [fname ]--
177
+ watchNum := shared .watchNums [fname ]
178
+ if watchNum == 0 {
179
+ delete (shared .watchNums , fname )
204
180
}
181
+ shared .mux .Unlock ()
205
182
206
- shared .watchNums [winfo .fname ]--
207
- if shared .watchNums [winfo .fname ] == 0 {
208
- delete (shared .watchNums , winfo .fname )
183
+ var err error
184
+ // If we were the last ones to watch this file, unsubscribe from inotify.
185
+ // This needs to happen after releasing the lock because fsnotify waits
186
+ // synchronously for the kernel to acknowledge the removal of the watch
187
+ // for this file, which causes us to deadlock if we still held the lock.
188
+ if watchNum == 0 {
189
+ err = shared .watcher .Remove (fname )
209
190
}
191
+
192
+ return err
210
193
}
211
194
212
195
// sendEvent sends the input event to the appropriate Tail.
@@ -241,7 +224,7 @@ func (shared *InotifyTracker) run() {
241
224
shared .error <- shared .addWatch (winfo )
242
225
243
226
case winfo := <- shared .remove :
244
- shared .removeWatch (winfo )
227
+ shared .error <- shared . removeWatch (winfo )
245
228
246
229
case event , open := <- shared .watcher .Events :
247
230
if ! open {
0 commit comments