1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
package fs
import (
"bufio"
"compress/gzip"
"context"
"errors"
"fmt"
"io"
"os"
"strings"
"time"
"github.com/mimecast/dtail/internal/io/dlog"
)
type readStatus int
const (
nothing readStatus = iota
abortReading readStatus = iota
continueReading readStatus = iota
defaultMaxLineLength = 1024 * 1024
)
// Used to tail and filter a local log file.
type readFile struct {
// Various statistics (e.g. regex hit percentage, transfer percentage).
stats
// Path of log file to tail.
filePath string
// Rooted target used for validated server-side re-opens.
validatedTarget *ValidatedReadTarget
// The glob identifier of the file.
globID string
// Channel to send a server message to the dtail client
serverMessages chan<- string
// Periodically retry reading file.
retry bool
// Can I skip messages when there are too many?
canSkipLines bool
// Seek to the EOF before processing file?
seekEOF bool
// Warned already about a long line.
warnedAboutLongLine bool
// Maximum line length before a line is split.
maxLineLength int
}
// String returns the string representation of the readFile
func (f readFile) String() string {
return fmt.Sprintf(
"readFile(filePath:%s,globID:%s,retry:%v,canSkipLines:%v,seekEOF:%v)",
f.filePath,
f.globID,
f.retry,
f.canSkipLines,
f.seekEOF)
}
// FilePath returns the full file path.
func (f readFile) FilePath() string {
return f.filePath
}
// Retry reading the file on error?
func (f readFile) Retry() bool {
return f.retry
}
func (f *readFile) lineLimit() int {
if f.maxLineLength <= 0 {
return defaultMaxLineLength
}
return f.maxLineLength
}
func (f *readFile) warnAboutLongLine(ctx context.Context) bool {
if f.warnedAboutLongLine {
return true
}
if f.serverMessages == nil {
f.warnedAboutLongLine = true
return true
}
select {
case f.serverMessages <- dlog.Common.Warn(f.filePath,
"Long log line, splitting into multiple lines") + "\n":
f.warnedAboutLongLine = true
return true
case <-ctx.Done():
return false
}
}
func (f *readFile) makeReader() (*bufio.Reader, *os.File, io.Closer, error) {
if f.filePath == "" && f.globID == "-" {
return f.makePipeReader()
}
return f.makeFileReader()
}
func (f *readFile) makeFileReader() (reader *bufio.Reader, fd *os.File, decompressor io.Closer, err error) {
if fd, err = f.openFile(); err != nil {
return
}
if f.seekEOF {
if _, err = fd.Seek(0, io.SeekEnd); err != nil {
return
}
}
reader, decompressor, err = f.makeCompressedFileReader(fd)
return
}
func (f *readFile) openFile() (*os.File, error) {
if f.validatedTarget != nil {
return f.validatedTarget.Open()
}
return os.Open(f.filePath)
}
func (f *readFile) makePipeReader() (*bufio.Reader, *os.File, io.Closer, error) {
return bufio.NewReader(os.Stdin), nil, nil, nil
}
func (f *readFile) periodicTruncateCheck(ctx context.Context, truncate chan<- struct{}) {
ticker := time.NewTicker(time.Second * 3)
defer ticker.Stop()
for {
select {
case <-ticker.C:
select {
case truncate <- struct{}{}:
case <-ctx.Done():
return
}
case <-ctx.Done():
return
}
}
}
func (f *readFile) makeCompressedFileReader(fd *os.File) (reader *bufio.Reader, decompressor io.Closer, err error) {
switch {
case strings.HasSuffix(f.FilePath(), ".gz"):
fallthrough
case strings.HasSuffix(f.FilePath(), ".gzip"):
dlog.Common.Info(f.FilePath(), "Detected gzip compression format")
var gzipReader *gzip.Reader
gzipReader, err = gzip.NewReader(fd)
if err != nil {
return
}
decompressor = gzipReader
reader = bufio.NewReader(gzipReader)
case strings.HasSuffix(f.FilePath(), ".zst"):
return f.makeZstdReader(fd)
default:
reader = bufio.NewReader(fd)
}
return
}
// Check wether log file is truncated. Returns nil if not.
func (f *readFile) truncated(fd *os.File) (bool, error) {
if fd == nil {
return false, nil
}
dlog.Common.Debug(f.filePath, "File truncation check")
// Can not seek currently open FD.
currentPosition, err := fd.Seek(0, io.SeekCurrent)
if err != nil {
return true, err
}
// Can not open file at original path.
pathFd, err := f.openFile()
if err != nil {
return true, err
}
defer pathFd.Close()
// Can not seek file at original path.
pathPosition, err := pathFd.Seek(0, io.SeekEnd)
if err != nil {
return true, err
}
if currentPosition > pathPosition {
return true, errors.New("File got truncated")
}
return false, nil
}
|