Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1821 from mozilla-services/logstreamer_deleted_empty
Browse files Browse the repository at this point in the history
Add safety check for cases where zero-length file is deleted and
  • Loading branch information
rafrombrc committed Dec 30, 2015
2 parents 90eea62 + abb60a4 commit 628f673
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
0.10.0 (2015-??-??)
===================

Bug Handling
------------

* Fixed issue where LogstreamerInput doesn't notice when a zero-length file is
deleted and replaced by a new zero-length file before any data is appended
(#1199).

0.10.0b2 (2015-11-20)
=====================
Expand Down
32 changes: 24 additions & 8 deletions logstreamer/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,17 @@ func (l *Logstream) NewerFileAvailable() (file string, ok bool) {
return "", false
}

priorWasEmpty := false
lastSize := currentInfo.Size()
newSize := fInfo.Size()
// 1. If our size is greater than the file at this filename, we're not the
// same file
if currentInfo.Size() > fInfo.Size() {
// same file.
if lastSize > newSize {
ok = true
} else if lastSize < newSize && lastSize == 0 {
// We have to double-check for cases where an empty file might have
// been deleted out from under us.
priorWasEmpty = true
ok = true
} else if l.FileHashMismatch() {
// Our file-hash didn't verify, not the same file
Expand All @@ -207,11 +215,10 @@ func (l *Logstream) NewerFileAvailable() (file string, ok bool) {
defer l.lfMutex.RUnlock()
if len(l.logfiles) > 0 {
file = l.logfiles[0].FileName
return
return file, ok
} else {
// Apparently no logfiles at all, retain this fd
ok = false
return
return file, false
}
}

Expand All @@ -227,15 +234,22 @@ func (l *Logstream) NewerFileAvailable() (file string, ok bool) {
if fileIndex == -1 {
// We couldn't find our filename in the list? Then there's nothing
// newer
return
return file, ok
}

if fileIndex+1 < len(l.logfiles) {
// There's a newer file!
return l.logfiles[fileIndex+1].FileName, true
}

return
// Filename didn't change, but currently opened fd is empty and file system
// file is bigger. No way to tell if the file system file is the same or if
// it was replaced by a new file before the content was appended, so we
// force the file to be repoened.
if priorWasEmpty {
return l.position.Filename, true
}
return file, ok
}

// FileHashMismatch uses the file path, seek location, and hash stored in the
Expand Down Expand Up @@ -264,7 +278,9 @@ func (l *Logstream) FileHashMismatch() bool {
// returned if the prior location cannot be located.
// If the logfile this location for has changed names, the position will be updated to
// reflect the move.
func (l *Logstream) LocatePriorLocation(checkFilename bool) (fd *os.File, reader io.Reader, err error) {
func (l *Logstream) LocatePriorLocation(checkFilename bool) (fd *os.File, reader io.Reader,
err error) {

var info os.FileInfo
l.lfMutex.RLock()
defer l.lfMutex.RUnlock()
Expand Down

0 comments on commit 628f673

Please sign in to comment.