Skip to content

Commit 2a8002a

Browse files
authored
Merge pull request #97 from Pothulapati/inotify
Fix linting issues
2 parents fac88ab + a3c4558 commit 2a8002a

File tree

2 files changed

+72
-45
lines changed

2 files changed

+72
-45
lines changed

inotify/inotify_linux.go

+71-44
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"unsafe"
3737
)
3838

39+
// Event represents a notification
3940
type Event struct {
4041
Mask uint32 // Mask of events
4142
Cookie uint32 // Unique cookie associating related events (for rename(2))
@@ -47,6 +48,7 @@ type watch struct {
4748
flags uint32 // inotify flags of this watch (see inotify(7) for the list of valid flags)
4849
}
4950

51+
// Watcher represents an inotify instance
5052
type Watcher struct {
5153
mu sync.Mutex
5254
fd int // File descriptor (as returned by the inotify_init() syscall)
@@ -130,14 +132,14 @@ func (w *Watcher) AddWatch(path string, flags uint32) error {
130132

131133
// Watch adds path to the watched file set, watching all events.
132134
func (w *Watcher) Watch(path string) error {
133-
return w.AddWatch(path, IN_ALL_EVENTS)
135+
return w.AddWatch(path, InAllEvents)
134136
}
135137

136138
// RemoveWatch removes path from the watched file set.
137139
func (w *Watcher) RemoveWatch(path string) error {
138140
watch, ok := w.watches[path]
139141
if !ok {
140-
return errors.New(fmt.Sprintf("can't remove non-existent inotify watch for: %s", path))
142+
return fmt.Errorf("can't remove non-existent inotify watch for: %s", path)
141143
}
142144
success, errno := syscall.InotifyRmWatch(w.fd, watch.wd)
143145
if success == -1 {
@@ -186,7 +188,7 @@ func (w *Watcher) readEvents() {
186188
continue
187189
}
188190

189-
var offset uint32 = 0
191+
var offset uint32
190192
// We don't know how many events we just read into the buffer
191193
// While the offset points to at least one whole event...
192194
for offset <= uint32(n-syscall.SizeofInotifyEvent) {
@@ -223,7 +225,7 @@ func (w *Watcher) readEvents() {
223225
// String formats the event e in the form
224226
// "filename: 0xEventMask = IN_ACCESS|IN_ATTRIB_|..."
225227
func (e *Event) String() string {
226-
var events string = ""
228+
var events string
227229

228230
m := e.Mask
229231
for _, b := range eventBits {
@@ -249,58 +251,83 @@ const (
249251
// IN_NONBLOCK uint32 = syscall.IN_NONBLOCK
250252

251253
// Options for AddWatch
252-
IN_DONT_FOLLOW uint32 = syscall.IN_DONT_FOLLOW
253-
IN_ONESHOT uint32 = syscall.IN_ONESHOT
254-
IN_ONLYDIR uint32 = syscall.IN_ONLYDIR
254+
255+
// InDontFollow : Don't dereference pathname if it is a symbolic link
256+
InDontFollow uint32 = syscall.IN_DONT_FOLLOW
257+
// InOneshot : Monitor the filesystem object corresponding to pathname for one event, then remove from watch list
258+
InOneshot uint32 = syscall.IN_ONESHOT
259+
// InOnlydir : Watch pathname only if it is a directory
260+
InOnlydir uint32 = syscall.IN_ONLYDIR
255261

256262
// The "IN_MASK_ADD" option is not exported, as AddWatch
257263
// adds it automatically, if there is already a watch for the given path
258264
// IN_MASK_ADD uint32 = syscall.IN_MASK_ADD
259265

260266
// Events
261-
IN_ACCESS uint32 = syscall.IN_ACCESS
262-
IN_ALL_EVENTS uint32 = syscall.IN_ALL_EVENTS
263-
IN_ATTRIB uint32 = syscall.IN_ATTRIB
264-
IN_CLOSE uint32 = syscall.IN_CLOSE
265-
IN_CLOSE_NOWRITE uint32 = syscall.IN_CLOSE_NOWRITE
266-
IN_CLOSE_WRITE uint32 = syscall.IN_CLOSE_WRITE
267-
IN_CREATE uint32 = syscall.IN_CREATE
268-
IN_DELETE uint32 = syscall.IN_DELETE
269-
IN_DELETE_SELF uint32 = syscall.IN_DELETE_SELF
270-
IN_MODIFY uint32 = syscall.IN_MODIFY
271-
IN_MOVE uint32 = syscall.IN_MOVE
272-
IN_MOVED_FROM uint32 = syscall.IN_MOVED_FROM
273-
IN_MOVED_TO uint32 = syscall.IN_MOVED_TO
274-
IN_MOVE_SELF uint32 = syscall.IN_MOVE_SELF
275-
IN_OPEN uint32 = syscall.IN_OPEN
267+
268+
// InAccess : File was accessed
269+
InAccess uint32 = syscall.IN_ACCESS
270+
// InAllEvents : Bit mask for all notify events
271+
InAllEvents uint32 = syscall.IN_ALL_EVENTS
272+
// InAttrib : Metadata changed
273+
InAttrib uint32 = syscall.IN_ATTRIB
274+
// InClose : Equates to IN_CLOSE_WRITE | IN_CLOSE_NOWRITE
275+
InClose uint32 = syscall.IN_CLOSE
276+
// InCloseNowrite : File or directory not opened for writing was closed
277+
InCloseNowrite uint32 = syscall.IN_CLOSE_NOWRITE
278+
// InCloseWrite : File opened for writing was closed
279+
InCloseWrite uint32 = syscall.IN_CLOSE_WRITE
280+
// InCreate : File/directory created in watched directory
281+
InCreate uint32 = syscall.IN_CREATE
282+
// InDelete : File/directory deleted from watched directory
283+
InDelete uint32 = syscall.IN_DELETE
284+
// InDeleteSelf : Watched file/directory was itself deleted
285+
InDeleteSelf uint32 = syscall.IN_DELETE_SELF
286+
// InModify : File was modified
287+
InModify uint32 = syscall.IN_MODIFY
288+
// InMove : Equates to IN_MOVED_FROM | IN_MOVED_TO
289+
InMove uint32 = syscall.IN_MOVE
290+
// InMovedFrom : Generated for the directory containing the old filename when a file is renamed
291+
InMovedFrom uint32 = syscall.IN_MOVED_FROM
292+
// InMovedTo : Generated for the directory containing the new filename when a file is renamed
293+
InMovedTo uint32 = syscall.IN_MOVED_TO
294+
// InMoveSelf : Watched file/directory was itself moved
295+
InMoveSelf uint32 = syscall.IN_MOVE_SELF
296+
// InOpen : File or directory was opened
297+
InOpen uint32 = syscall.IN_OPEN
276298

277299
// Special events
278-
IN_ISDIR uint32 = syscall.IN_ISDIR
279-
IN_IGNORED uint32 = syscall.IN_IGNORED
280-
IN_Q_OVERFLOW uint32 = syscall.IN_Q_OVERFLOW
281-
IN_UNMOUNT uint32 = syscall.IN_UNMOUNT
300+
301+
// InIsdir : Subject of this event is a directory
302+
InIsdir uint32 = syscall.IN_ISDIR
303+
// InIgnored : Watch was removed explicitly or automatically
304+
InIgnored uint32 = syscall.IN_IGNORED
305+
// InQOverflow : Event queue overflowed
306+
InQOverflow uint32 = syscall.IN_Q_OVERFLOW
307+
// InUnmount : Filesystem containing watched object was unmounted
308+
InUnmount uint32 = syscall.IN_UNMOUNT
282309
)
283310

284311
var eventBits = []struct {
285312
Value uint32
286313
Name string
287314
}{
288-
{IN_ACCESS, "IN_ACCESS"},
289-
{IN_ATTRIB, "IN_ATTRIB"},
290-
{IN_CLOSE, "IN_CLOSE"},
291-
{IN_CLOSE_NOWRITE, "IN_CLOSE_NOWRITE"},
292-
{IN_CLOSE_WRITE, "IN_CLOSE_WRITE"},
293-
{IN_CREATE, "IN_CREATE"},
294-
{IN_DELETE, "IN_DELETE"},
295-
{IN_DELETE_SELF, "IN_DELETE_SELF"},
296-
{IN_MODIFY, "IN_MODIFY"},
297-
{IN_MOVE, "IN_MOVE"},
298-
{IN_MOVED_FROM, "IN_MOVED_FROM"},
299-
{IN_MOVED_TO, "IN_MOVED_TO"},
300-
{IN_MOVE_SELF, "IN_MOVE_SELF"},
301-
{IN_OPEN, "IN_OPEN"},
302-
{IN_ISDIR, "IN_ISDIR"},
303-
{IN_IGNORED, "IN_IGNORED"},
304-
{IN_Q_OVERFLOW, "IN_Q_OVERFLOW"},
305-
{IN_UNMOUNT, "IN_UNMOUNT"},
315+
{InAccess, "IN_ACCESS"},
316+
{InAttrib, "IN_ATTRIB"},
317+
{InClose, "IN_CLOSE"},
318+
{InCloseNowrite, "IN_CLOSE_NOWRITE"},
319+
{InCloseWrite, "IN_CLOSE_WRITE"},
320+
{InCreate, "IN_CREATE"},
321+
{InDelete, "IN_DELETE"},
322+
{InDeleteSelf, "IN_DELETE_SELF"},
323+
{InModify, "IN_MODIFY"},
324+
{InMove, "IN_MOVE"},
325+
{InMovedFrom, "IN_MOVED_FROM"},
326+
{InMovedTo, "IN_MOVED_TO"},
327+
{InMoveSelf, "IN_MOVE_SELF"},
328+
{InOpen, "IN_OPEN"},
329+
{InIsdir, "IN_ISDIR"},
330+
{InIgnored, "IN_IGNORED"},
331+
{InQOverflow, "IN_Q_OVERFLOW"},
332+
{InUnmount, "IN_UNMOUNT"},
306333
}

inotify/inotify_linux_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestInotifyEvents(t *testing.T) {
4444

4545
// Receive events on the event channel on a separate goroutine
4646
eventstream := watcher.Event
47-
var eventsReceived int32 = 0
47+
var eventsReceived int32
4848
done := make(chan bool)
4949
go func() {
5050
for event := range eventstream {

0 commit comments

Comments
 (0)