Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any file changes that occurred before InotifyFileWatcher was started but after the preceding EOF would not be detected #1

Merged
merged 2 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

until I looked up the usage I thought that the bool indicated whether the file was modified or not.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's if the file was deleted or not

}

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
}