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

feat: fix file change(rename) line loss and duplication #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 14 additions & 6 deletions tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// Copyright (c) 2015 HPE Software Inc. All rights reserved.
// Copyright (c) 2013 ActiveState Software Inc. All rights reserved.

//nxadm/tail provides a Go library that emulates the features of the BSD `tail`
//program. The library comes with full support for truncation/move detection as
//it is designed to work with log rotation tools. The library works on all
//operating systems supported by Go, including POSIX systems like Linux and
//*BSD, and MS Windows. Go 1.9 is the oldest compiler release supported.
// nxadm/tail provides a Go library that emulates the features of the BSD `tail`
// program. The library comes with full support for truncation/move detection as
// it is designed to work with log rotation tools. The library works on all
// operating systems supported by Go, including POSIX systems like Linux and
// *BSD, and MS Windows. Go 1.9 is the oldest compiler release supported.
package tail

import (
Expand All @@ -21,10 +21,11 @@ import (
"sync"
"time"

"gopkg.in/tomb.v1"

"github.com/nxadm/tail/ratelimiter"
"github.com/nxadm/tail/util"
"github.com/nxadm/tail/watch"
"gopkg.in/tomb.v1"
)

var (
Expand Down Expand Up @@ -383,6 +384,13 @@ func (tail *Tail) waitForChanges() error {
}
}

// The write event should be handled first
select {
case <-tail.changes.Modified:
return nil
default:
}

select {
case <-tail.changes.Modified:
return nil
Expand Down
28 changes: 24 additions & 4 deletions watch/inotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@ import (
"fmt"
"os"
"path/filepath"
"syscall"

"github.com/nxadm/tail/util"

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

// InotifyFileWatcher uses inotify to monitor file changes.
type InotifyFileWatcher struct {
Filename string
Size int64
// inodeId record cur watch file id
inodeId uint64
}

func NewInotifyFileWatcher(filename string) *InotifyFileWatcher {
fw := &InotifyFileWatcher{filepath.Clean(filename), 0}
fw := &InotifyFileWatcher{filepath.Clean(filename), 0, 0}
return fw
}

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

changes := NewFileChanges()
fw.Size = pos
var stat syscall.Stat_t
// record file inodeId
err = syscall.Stat(fw.Filename, &stat)
if err == nil {
fw.inodeId = stat.Ino
}

go func() {

Expand Down Expand Up @@ -122,10 +131,21 @@ func (fw *InotifyFileWatcher) ChangeEvents(t *tomb.Tomb, pos int64) (*FileChange
}
fw.Size = fi.Size()

// No matter what, it is necessary to notify a write event.
changes.NotifyModified()

if prevSize > 0 && prevSize > fw.Size {
// file change; if file inodeId changed, notify delete event
if fw.inodeId > 0 && fi.Sys() != nil {
if statT, fok := fi.Sys().(*syscall.Stat_t); fok {
if statT.Ino != fw.inodeId {
RemoveWatch(fw.Filename)
changes.NotifyDeleted()
return
}
}
}
changes.NotifyTruncated()
} else {
changes.NotifyModified()
}
prevSize = fw.Size
}
Expand Down