From 90586ec39b2a56c7377f1c59b11c8724f80aeb78 Mon Sep 17 00:00:00 2001 From: K Date: Mon, 24 Jul 2017 17:37:13 +0900 Subject: [PATCH 1/3] Decreased defers --- tail.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tail.go b/tail.go index c99cdaa2..856326a8 100644 --- a/tail.go +++ b/tail.go @@ -224,8 +224,10 @@ func (tail *Tail) readLine() (string, error) { } func (tail *Tail) tailFileSync() { - defer tail.Done() - defer tail.close() + defer func() { + tail.close() + tail.Done() + }() if !tail.MustExist { // deferred first open. From c153685dba77a96153aea654b23ee69c3382b941 Mon Sep 17 00:00:00 2001 From: K Date: Mon, 24 Jul 2017 17:47:07 +0900 Subject: [PATCH 2/3] Fixed a potential to never return the last line if it is not followed by a newline --- tail.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/tail.go b/tail.go index 856326a8..48b2e4f3 100644 --- a/tail.go +++ b/tail.go @@ -252,16 +252,12 @@ func (tail *Tail) tailFileSync() { tail.openReader() - var offset int64 - var err error - // Read line by line. for { // do not seek in named pipes if !tail.Pipe { // grab the position in case we need to back up in the event of a half-line - offset, err = tail.Tell() - if err != nil { + if _, err := tail.Tell(); err != nil { tail.Kill(err) return } @@ -297,10 +293,8 @@ func (tail *Tail) tailFileSync() { } if tail.Follow && line != "" { - // this has the potential to never return the last line if - // it's not followed by a newline; seems a fair trade here - err := tail.seekTo(SeekInfo{Offset: offset, Whence: 0}) - if err != nil { + tail.sendLine(line) + if err := tail.seekEnd(); err != nil { tail.Kill(err) return } From 5ad6447941c464f021ac1c2475ddb04a6e2caf47 Mon Sep 17 00:00:00 2001 From: K Date: Mon, 24 Jul 2017 20:34:59 +0900 Subject: [PATCH 3/3] Fixed test --- tail_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tail_test.go b/tail_test.go index 38d6b84b..5226c944 100644 --- a/tail_test.go +++ b/tail_test.go @@ -114,7 +114,7 @@ func TestStopAtEOF(t *testing.T) { func TestMaxLineSizeFollow(t *testing.T) { // As last file line does not end with newline, it will not be present in tail's output - maxLineSize(t, true, "hello\nworld\nfin\nhe", []string{"hel", "lo", "wor", "ld", "fin"}) + maxLineSize(t, true, "hello\nworld\nfin\nhe", []string{"hel", "lo", "wor", "ld", "fin", "he"}) } func TestMaxLineSizeNoFollow(t *testing.T) {