diff --git a/tail.go b/tail.go index 29cd4ce..d38e540 100644 --- a/tail.go +++ b/tail.go @@ -26,7 +26,9 @@ type Line struct { // Config is used to specify how a file must be tailed. type Config struct { - Location int64 // Tail from last N lines (tail -n) + Location int64 // For positive location, tail after first N + // lines; for negative, tail from last N lines; + // for zero, tail from end. If -1, do not seek. Follow bool // Continue looking for new lines (tail -f) ReOpen bool // Reopen recreated files (tail -F) MustExist bool // Fail early if the file does not exist @@ -139,9 +141,16 @@ func (tail *Tail) tailFileSync() { } // Seek to requested location on first open of the file. - if tail.Location >= 0 { - // Seek relative to file end - _, err := tail.file.Seek(tail.Location, 2) + if tail.Location != -1 { + var err error + switch { + case tail.Location <= 0: + // Seek relative to file end + _, err = tail.file.Seek(tail.Location, os.SEEK_END) + default: + // Seek from file beginning + _, err = tail.file.Seek(tail.Location, os.SEEK_SET) + } if err != nil { tail.Killf("Seek error on %s: %s", tail.Filename, err) return