From bcc993b4949487908e96953af67799b9e3d9102e Mon Sep 17 00:00:00 2001 From: Brian Goff Date: Wed, 15 Jul 2020 10:40:21 -0700 Subject: [PATCH] Fix logfile to open all files with custom openFile This makes sure, on Windows, that all files are opened with FILE_SHARE_DELETE. On non-Windows this just calls the same `os.Open()`. Signed-off-by: Brian Goff --- daemon/logger/loggerutils/file_unix.go | 4 ++++ daemon/logger/loggerutils/file_windows.go | 4 ++++ daemon/logger/loggerutils/logfile.go | 12 ++++++------ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/daemon/logger/loggerutils/file_unix.go b/daemon/logger/loggerutils/file_unix.go index eb39d4b73d..e7b6095296 100644 --- a/daemon/logger/loggerutils/file_unix.go +++ b/daemon/logger/loggerutils/file_unix.go @@ -7,3 +7,7 @@ import "os" func openFile(name string, flag int, perm os.FileMode) (*os.File, error) { return os.OpenFile(name, flag, perm) } + +func open(name string) (*os.File, error) { + return os.Open(name) +} diff --git a/daemon/logger/loggerutils/file_windows.go b/daemon/logger/loggerutils/file_windows.go index 507da6874d..c42c08aa72 100644 --- a/daemon/logger/loggerutils/file_windows.go +++ b/daemon/logger/loggerutils/file_windows.go @@ -8,6 +8,10 @@ import ( "github.com/pkg/errors" ) +func open(name string) (*os.File, error) { + return openFile(name, os.O_RDONLY, 0) +} + func openFile(name string, flag int, perm os.FileMode) (*os.File, error) { if name == "" { return nil, &os.PathError{Op: "open", Path: name, Err: syscall.ENOENT} diff --git a/daemon/logger/loggerutils/logfile.go b/daemon/logger/loggerutils/logfile.go index c47964f3aa..b4212e7a81 100644 --- a/daemon/logger/loggerutils/logfile.go +++ b/daemon/logger/loggerutils/logfile.go @@ -262,7 +262,7 @@ func rotate(name string, maxFiles int, compress bool) error { } func compressFile(fileName string, lastTimestamp time.Time) (retErr error) { - file, err := os.Open(fileName) + file, err := open(fileName) if err != nil { if os.IsNotExist(err) { logrus.WithField("file", fileName).WithError(err).Debug("Could not open log file to compress") @@ -336,7 +336,7 @@ func (w *LogFile) Close() error { // TODO: Consider a different implementation which can effectively follow logs under frequent rotations. func (w *LogFile) ReadLogs(config logger.ReadConfig, watcher *logger.LogWatcher) { w.mu.RLock() - currentFile, err := os.Open(w.f.Name()) + currentFile, err := open(w.f.Name()) if err != nil { w.mu.RUnlock() watcher.Err <- err @@ -429,7 +429,7 @@ func (w *LogFile) openRotatedFiles(config logger.ReadConfig) (files []*os.File, }() for i := w.maxFiles; i > 1; i-- { - f, err := os.Open(fmt.Sprintf("%s.%d", w.f.Name(), i-1)) + f, err := open(fmt.Sprintf("%s.%d", w.f.Name(), i-1)) if err != nil { if !os.IsNotExist(err) { return nil, errors.Wrap(err, "error opening rotated log file") @@ -439,7 +439,7 @@ func (w *LogFile) openRotatedFiles(config logger.ReadConfig) (files []*os.File, decompressedFileName := fileName + tmpLogfileSuffix tmpFile, err := w.filesRefCounter.GetReference(decompressedFileName, func(refFileName string, exists bool) (*os.File, error) { if exists { - return os.Open(refFileName) + return open(refFileName) } return decompressfile(fileName, refFileName, config.Since) }) @@ -465,7 +465,7 @@ func (w *LogFile) openRotatedFiles(config logger.ReadConfig) (files []*os.File, } func decompressfile(fileName, destFileName string, since time.Time) (*os.File, error) { - cf, err := os.Open(fileName) + cf, err := open(fileName) if err != nil { return nil, errors.Wrap(err, "error opening file for decompression") } @@ -590,7 +590,7 @@ func followLogs(f *os.File, logWatcher *logger.LogWatcher, notifyRotate chan int // retry when the file doesn't exist for retries := 0; retries <= 5; retries++ { - f, err = os.Open(name) + f, err = open(name) if err == nil || !os.IsNotExist(err) { break }