1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

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 <cpuguy83@gmail.com>
This commit is contained in:
Brian Goff 2020-07-15 10:40:21 -07:00
parent 56ba96b6c1
commit bcc993b494
3 changed files with 14 additions and 6 deletions

View file

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

View file

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

View file

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