Skip to content

Commit

Permalink
Any file changes that occurred before InotifyFileWatcher was started …
Browse files Browse the repository at this point in the history
…but after the preceding EOF would not be detected (#1)
  • Loading branch information
theencee authored Aug 31, 2021
1 parent 9254734 commit 4070356
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
9 changes: 7 additions & 2 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ type Logger struct {

var LOGGER = &Logger{log.New(os.Stderr, "", log.LstdFlags)}

// fatal is like panic except it displays only the current goroutine's stack.
// Fatal is like panic except it displays only the current goroutine's stack.
func Fatal(format string, v ...interface{}) {
// https://github.com/nxadm/log/blob/master/log.go#L45
LOGGER.Output(2, fmt.Sprintf("FATAL -- "+format, v...)+"\n"+string(debug.Stack()))
os.Exit(1)
}

// partitionString partitions the string into chunks of given size,
// Error logs an error message with the current goroutine stack and returns. It doesn't quit.
func Error(format string, v ...interface{}) {
LOGGER.Output(2, fmt.Sprintf("ERROR -- "+format, v...)+"\n"+string(debug.Stack()))
}

// PartitionString partitions the string into chunks of given size,
// with the last chunk of variable size.
func PartitionString(s string, chunkSize int) []string {
if chunkSize <= 0 {
Expand Down
34 changes: 33 additions & 1 deletion watch/inotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/nxadm/tail/util"

"github.com/fsnotify/fsnotify"
"github.com/fsnotify/fsnotify"
"gopkg.in/tomb.v1"
)

Expand Down Expand Up @@ -79,6 +79,13 @@ func (fw *InotifyFileWatcher) ChangeEvents(t *tomb.Tomb, pos int64) (*FileChange

events := Events(fw.Filename)

// Check to see the file hasn't been modified already before the watcher started
fi, deleted := fw.checkAndNotifyIfModifiedInBetween(changes)
if deleted {
return
}
fw.Size = fi.Size()

for {
prevSize := fw.Size

Expand Down Expand Up @@ -134,3 +141,28 @@ func (fw *InotifyFileWatcher) ChangeEvents(t *tomb.Tomb, pos int64) (*FileChange

return changes, nil
}

func (fw *InotifyFileWatcher) checkAndNotifyIfModifiedInBetween(changes *FileChanges) (os.FileInfo, bool) {
fi, err := os.Stat(fw.Filename)
if err != nil {
if !os.IsNotExist(err) {
util.Error("Failed to stat file %v: %v", fw.Filename, err)
// Treat it as a deleted file as we cannot read it anyway.
}
_ = RemoveWatch(fw.Filename)
changes.NotifyDeleted()
return nil, true
}

if fw.Size > fi.Size() { // old file size was larger than now => truncated
changes.NotifyTruncated()
} else if fw.Size != fi.Size() {
changes.NotifyModified()
}
// there is a corner case of file that was truncated and replaced with exact same amount of bytes, which would
// result in no notification. However any subsequent writes will capture that
// If the file isn't expected to be written to often and those events cannot be missed, recommend using Polling watcher
// instead of inotify

return fi, false
}

0 comments on commit 4070356

Please sign in to comment.