Skip to content

Commit

Permalink
adapt non-Linux file tailers to MOVED_TO fix #21
Browse files Browse the repository at this point in the history
  • Loading branch information
fstab committed Nov 4, 2017
1 parent a3f369b commit f32d17d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
9 changes: 5 additions & 4 deletions tailer/fileTailer_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ func (events *eventList) Process(fileBefore *File, reader *bufferedLineReader, a
file = fileBefore
lines = []string{}
var truncated bool
for _, event := range events.events {
logger.Debug("File system watcher received %v.\n", event2string(events.watcher.dir, file, event))
logger.Debug("File system watcher received %v event(s):\n", len(events.events))
for i, event := range events.events {
logger.Debug("%v/%v: %v.\n", i+1, len(events.events), event2string(events.watcher.dir, file, event))
}

// Handle truncate events.
Expand Down Expand Up @@ -170,7 +171,7 @@ func (events *eventList) Process(fileBefore *File, reader *bufferedLineReader, a
}
}

// Handle move and delete events.
// Handle move and delete events (NOTE_RENAME on the file's fd means the file was moved away, like in inotify's IN_MOVED_FROM).
for _, event := range events.events {
if file != nil && event.Ident == uint64(file.Fd()) && (event.Fflags&syscall.NOTE_DELETE == syscall.NOTE_DELETE || event.Fflags&syscall.NOTE_RENAME == syscall.NOTE_RENAME) {
file.Close() // closing the fd will automatically remove event from kq.
Expand All @@ -179,7 +180,7 @@ func (events *eventList) Process(fileBefore *File, reader *bufferedLineReader, a
}
}

// Handle create events.
// Handle move_to and create events (NOTE_WRITE on the directory's fd means a file was created or moved, so this covers inotify's MOVED_TO).
for _, event := range events.events {
if file == nil && event.Ident == uint64(events.watcher.dir.Fd()) && event.Fflags&syscall.NOTE_WRITE == syscall.NOTE_WRITE {
file, err = open(abspath)
Expand Down
6 changes: 3 additions & 3 deletions tailer/fileTailer_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,14 @@ func (event *event) Process(fileBefore *File, reader *bufferedLineReader, abspat
lines = append(lines, freshLines...)
}

// MOVE or DELETE
// MOVED_FROM or DELETE
if file != nil && norm(event.Name) == norm(abspath) && (event.Mask&winfsnotify.FS_MOVED_FROM == winfsnotify.FS_MOVED_FROM || event.Mask&winfsnotify.FS_DELETE == winfsnotify.FS_DELETE) {
file = nil
reader.Clear()
}

// CREATE
if file == nil && norm(event.Name) == norm(abspath) && event.Mask&winfsnotify.FS_CREATE == winfsnotify.FS_CREATE {
// MOVED_TO or CREATE
if file == nil && norm(event.Name) == norm(abspath) && (event.Mask&winfsnotify.FS_MOVED_TO == winfsnotify.FS_MOVED_TO || event.Mask&winfsnotify.FS_CREATE == winfsnotify.FS_CREATE) {
file, err = open(abspath)
if err != nil {
return
Expand Down

0 comments on commit f32d17d

Please sign in to comment.