Treat permission error as if file was deleted on windows

This commit is contained in:
Florin Dragos 2015-09-29 16:51:16 +03:00
parent 4b368d1590
commit d9f4dcdb25
3 changed files with 4 additions and 34 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/ActiveState/tail/util" "github.com/ActiveState/tail/util"
"gopkg.in/tomb.v1" "gopkg.in/tomb.v1"
"os" "os"
"runtime"
"time" "time"
) )
@ -51,8 +52,6 @@ func (fw *PollingFileWatcher) ChangeEvents(t *tomb.Tomb, origFi os.FileInfo) *Fi
go func() { go func() {
defer changes.Close() defer changes.Close()
var retry int = 0
prevSize := fw.Size prevSize := fw.Size
for { for {
select { select {
@ -64,16 +63,14 @@ func (fw *PollingFileWatcher) ChangeEvents(t *tomb.Tomb, origFi os.FileInfo) *Fi
time.Sleep(POLL_DURATION) time.Sleep(POLL_DURATION)
fi, err := os.Stat(fw.Filename) fi, err := os.Stat(fw.Filename)
if err != nil { 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). // File does not exist (has been deleted).
changes.NotifyDeleted() changes.NotifyDeleted()
return return
} }
if permissionErrorRetry(err, &retry) {
continue
}
// XXX: report this error back to the user // XXX: report this error back to the user
util.Fatal("Failed to stat file %v: %v", fw.Filename, err) util.Fatal("Failed to stat file %v: %v", fw.Filename, err)
} }

View File

@ -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
}

View File

@ -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
}