From d9f4dcdb2569533d48cb825856c4f80250dcf487 Mon Sep 17 00:00:00 2001 From: Florin Dragos Date: Tue, 29 Sep 2015 16:51:16 +0300 Subject: [PATCH 1/2] Treat permission error as if file was deleted on windows --- watch/polling.go | 11 ++++------- watch/polling_posix.go | 9 --------- watch/polling_windows.go | 18 ------------------ 3 files changed, 4 insertions(+), 34 deletions(-) delete mode 100644 watch/polling_posix.go delete mode 100644 watch/polling_windows.go diff --git a/watch/polling.go b/watch/polling.go index 10a17eb..1747138 100644 --- a/watch/polling.go +++ b/watch/polling.go @@ -6,6 +6,7 @@ import ( "github.com/ActiveState/tail/util" "gopkg.in/tomb.v1" "os" + "runtime" "time" ) @@ -51,8 +52,6 @@ func (fw *PollingFileWatcher) ChangeEvents(t *tomb.Tomb, origFi os.FileInfo) *Fi go func() { defer changes.Close() - var retry int = 0 - prevSize := fw.Size for { select { @@ -64,16 +63,14 @@ func (fw *PollingFileWatcher) ChangeEvents(t *tomb.Tomb, origFi os.FileInfo) *Fi time.Sleep(POLL_DURATION) fi, err := os.Stat(fw.Filename) if err != nil { - if os.IsNotExist(err) { + // Windows cannot delete a file if a handle is still open (tail keeps one open) + // so it gives access denied to anything trying to read it until all handles are released. + if os.IsNotExist(err) || (runtime.GOOS == "windows" && os.IsPermission(err)) { // File does not exist (has been deleted). changes.NotifyDeleted() return } - if permissionErrorRetry(err, &retry) { - continue - } - // XXX: report this error back to the user util.Fatal("Failed to stat file %v: %v", fw.Filename, err) } diff --git a/watch/polling_posix.go b/watch/polling_posix.go deleted file mode 100644 index 1a75546..0000000 --- a/watch/polling_posix.go +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright (c) 2013 ActiveState Software Inc. All rights reserved. -// +build linux darwin freebsd - -package watch - -func permissionErrorRetry(err error, retry *int) bool { - // No need for this on linux, don't retry - return false -} diff --git a/watch/polling_windows.go b/watch/polling_windows.go deleted file mode 100644 index 9ffd23d..0000000 --- a/watch/polling_windows.go +++ /dev/null @@ -1,18 +0,0 @@ -// +build windows - -package watch - -import ( - "os" -) - -const permissionDeniedRetryCount int = 5 - -func permissionErrorRetry(err error, retry *int) bool { - if os.IsPermission(err) && *retry < permissionDeniedRetryCount { - // While pooling a file that does not exist yet, but will be created by another process we can get Permission Denied - (*retry)++ - return true - } - return false -} From 2403cad5eb4cd1184a9f468062e14d329ab034e1 Mon Sep 17 00:00:00 2001 From: Florin Dragos Date: Wed, 30 Sep 2015 17:48:17 +0300 Subject: [PATCH 2/2] Fix imports --- Dockerfile | 12 ++++++------ Makefile | 2 +- README.md | 8 ++++---- cmd/gotail/gotail.go | 2 +- tail.go | 6 +++--- tail_test.go | 2 +- tail_windows.go | 2 +- watch/inotify.go | 2 +- watch/polling.go | 4 ++-- 9 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Dockerfile b/Dockerfile index 15a8866..cd297b9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,17 +1,17 @@ FROM golang -RUN mkdir -p $GOPATH/src/github.com/ActiveState/tail/ -ADD . $GOPATH/src/github.com/ActiveState/tail/ +RUN mkdir -p $GOPATH/src/github.com/hpcloud/tail/ +ADD . $GOPATH/src/github.com/hpcloud/tail/ # expecting to fetch dependencies successfully. -RUN go get -v github.com/ActiveState/tail +RUN go get -v github.com/hpcloud/tail # expecting to run the test successfully. -RUN go test -v github.com/ActiveState/tail +RUN go test -v github.com/hpcloud/tail # expecting to install successfully -RUN go install -v github.com/ActiveState/tail -RUN go install -v github.com/ActiveState/tail/cmd/gotail +RUN go install -v github.com/hpcloud/tail +RUN go install -v github.com/hpcloud/tail/cmd/gotail RUN $GOPATH/bin/gotail -h || true diff --git a/Makefile b/Makefile index 6d5cea7..9ac927d 100644 --- a/Makefile +++ b/Makefile @@ -8,4 +8,4 @@ fmt: # Run the test in an isolated environment. fulltest: - docker build -t activestate/tail . + docker build -t hpcloud/tail . diff --git a/README.md b/README.md index 9a7142e..9d93774 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![Build Status](https://travis-ci.org/ActiveState/tail.svg)](https://travis-ci.org/ActiveState/tail) +[![Build Status](https://travis-ci.org/hpcloud/tail.svg)](https://travis-ci.org/ActiveState/tail) # Go package for tail-ing files @@ -11,7 +11,7 @@ for line := range t.Lines { } ``` -See [API documentation](http://godoc.org/github.com/ActiveState/tail). +See [API documentation](http://godoc.org/github.com/hpcloud/tail). ## Log rotation @@ -20,8 +20,8 @@ designed to work with log rotation tools. ## Installing - go get github.com/ActiveState/tail/... + go get github.com/hpcloud/tail/... ## Windows support -This package [needs assistance](https://github.com/ActiveState/tail/labels/Windows) for full Windows support. +This package [needs assistance](https://github.com/hpcloud/tail/labels/Windows) for full Windows support. diff --git a/cmd/gotail/gotail.go b/cmd/gotail/gotail.go index 12c61d3..c63b582 100644 --- a/cmd/gotail/gotail.go +++ b/cmd/gotail/gotail.go @@ -5,7 +5,7 @@ package main import ( "flag" "fmt" - "github.com/ActiveState/tail" + "github.com/hpcloud/tail" "os" ) diff --git a/tail.go b/tail.go index 93d40b7..a7a1a5b 100644 --- a/tail.go +++ b/tail.go @@ -12,9 +12,9 @@ import ( "strings" "time" - "github.com/ActiveState/tail/ratelimiter" - "github.com/ActiveState/tail/util" - "github.com/ActiveState/tail/watch" + "github.com/hpcloud/tail/ratelimiter" + "github.com/hpcloud/tail/util" + "github.com/hpcloud/tail/watch" "gopkg.in/tomb.v1" ) diff --git a/tail_test.go b/tail_test.go index 6095f4f..b2f22a5 100644 --- a/tail_test.go +++ b/tail_test.go @@ -14,7 +14,7 @@ import ( "time" "./watch" - "github.com/ActiveState/tail/ratelimiter" + "github.com/hpcloud/tail/ratelimiter" ) func init() { diff --git a/tail_windows.go b/tail_windows.go index 5a26372..ef2cfca 100644 --- a/tail_windows.go +++ b/tail_windows.go @@ -3,7 +3,7 @@ package tail import ( - "github.com/ActiveState/tail/winfile" + "github.com/hpcloud/tail/winfile" "os" ) diff --git a/watch/inotify.go b/watch/inotify.go index f263b56..ee660af 100644 --- a/watch/inotify.go +++ b/watch/inotify.go @@ -7,7 +7,7 @@ import ( "os" "path/filepath" - "github.com/ActiveState/tail/util" + "github.com/hpcloud/tail/util" "gopkg.in/fsnotify.v0" "gopkg.in/tomb.v1" ) diff --git a/watch/polling.go b/watch/polling.go index 1747138..1bf50f8 100644 --- a/watch/polling.go +++ b/watch/polling.go @@ -3,7 +3,7 @@ package watch import ( - "github.com/ActiveState/tail/util" + "github.com/hpcloud/tail/util" "gopkg.in/tomb.v1" "os" "runtime" @@ -63,7 +63,7 @@ func (fw *PollingFileWatcher) ChangeEvents(t *tomb.Tomb, origFi os.FileInfo) *Fi time.Sleep(POLL_DURATION) fi, err := os.Stat(fw.Filename) if err != nil { - // Windows cannot delete a file if a handle is still open (tail keeps one open) + // Windows cannot delete a file if a handle is still open (tail keeps one open) // so it gives access denied to anything trying to read it until all handles are released. if os.IsNotExist(err) || (runtime.GOOS == "windows" && os.IsPermission(err)) { // File does not exist (has been deleted).