diff options
| author | Paul Buetow <git@mx.buetow.org> | 2021-03-23 09:37:02 +0000 |
|---|---|---|
| committer | Paul Buetow <git@mx.buetow.org> | 2021-03-23 20:12:09 +0000 |
| commit | 2b47630c2f68794a95d5065a7989d489990f7a19 (patch) | |
| tree | 389166c157ebc7cec690a967b02255d9337c6988 /internal/io/fs/truncate.go | |
| parent | d6dd896805faa074960f17bd1e8c516420e27f0d (diff) | |
context aware grep with -max -after and -before not work
Diffstat (limited to 'internal/io/fs/truncate.go')
| -rw-r--r-- | internal/io/fs/truncate.go | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/internal/io/fs/truncate.go b/internal/io/fs/truncate.go new file mode 100644 index 0000000..a8d59ac --- /dev/null +++ b/internal/io/fs/truncate.go @@ -0,0 +1,61 @@ +package fs + +import ( + "context" + "errors" + "io" + "os" + "time" + + "github.com/mimecast/dtail/internal/io/logger" +) + +func (f readFile) truncateTimer(ctx context.Context) (checkTruncate chan struct{}) { + checkTruncate = make(chan struct{}) + + go func() { + for { + select { + case <-time.After(time.Second * 3): + select { + case checkTruncate <- struct{}{}: + case <-ctx.Done(): + } + case <-ctx.Done(): + return + } + } + }() + + return +} + +// Check wether log file is truncated. Returns nil if not. +func (f readFile) truncated(fd *os.File) (bool, error) { + logger.Debug(f.filePath, "File truncation check") + + // Can not seek currently open FD. + curPos, err := fd.Seek(0, os.SEEK_CUR) + if err != nil { + return true, err + } + + // Can not open file at original path. + pathFd, err := os.Open(f.filePath) + if err != nil { + return true, err + } + defer pathFd.Close() + + // Can not seek file at original path. + pathPos, err := pathFd.Seek(0, io.SeekEnd) + if err != nil { + return true, err + } + + if curPos > pathPos { + return true, errors.New("File got truncated") + } + + return false, nil +} |
