From d9f4dcdb2569533d48cb825856c4f80250dcf487 Mon Sep 17 00:00:00 2001 From: Florin Dragos Date: Tue, 29 Sep 2015 16:51:16 +0300 Subject: [PATCH] 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 -}