-
Notifications
You must be signed in to change notification settings - Fork 2
/
bodyfile.go
338 lines (280 loc) · 8.07 KB
/
bodyfile.go
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package bodyfile
import (
"encoding/csv"
"fmt"
"io"
"sort"
"strconv"
"strings"
"time"
"github.com/Knetic/govaluate"
"golang.org/x/exp/slices"
)
// Timestamp lower limit: represents a -1 timestamp
var smallestTime time.Time = time.Unix(-1, 0).UTC()
// Entry represents one line of the bodyfile
type Entry struct {
// MD5|name|inode|mode_as_string|UID|GID|size|atime|mtime|ctime|crtime
MD5 string
Name string
Inode string
Mode string
UID int
GID int
Size int64
AccessTime time.Time
ModificationTime time.Time
ChangeTime time.Time
CreationTime time.Time
MatchingTimestamp int
}
// Reader is the reading object
type Reader struct {
csv *csv.Reader
expression *govaluate.EvaluableExpression
offset int
entries tsEntrySortable
Strict bool
}
// NewReader instantiates a Reader object
func NewReader(r io.Reader) *Reader {
csvReader := csv.NewReader(r)
csvReader.Comma = '|'
csvReader.Comment = '#'
csvReader.LazyQuotes = true
return &Reader{
csv: csvReader,
offset: -1,
}
}
// NewStrictReader instantiates a new Reader object with the Strict mode enabled
func NewStrictReader(r io.Reader) *Reader {
reader := NewReader(r)
reader.Strict = true
return reader
}
// AddFilter adds a date restriction to filter the bodyfile
func (r *Reader) AddFilter(filter string) (err error) {
r.expression, err = govaluate.NewEvaluableExpression(filter)
return err
}
func fieldsToEntry(fields []string) (*Entry, error) {
if len(fields) < 11 {
return nil, fmt.Errorf("Invalid bodyfile format, expected 11 fields, got %d", len(fields))
}
if len(fields) > 11 {
i := 1
for i < len(fields)-1 {
if strings.HasSuffix(fields[i], "\\") && !strings.HasSuffix(fields[i], "\\\\") {
fields[i] = fields[i] + fields[i+1]
fields = slices.Delete(fields, i+1, i+2)
} else {
fields[i-1] = fields[i-1] + fields[i]
fields = slices.Delete(fields, i, i+1)
break
}
i += 1
}
}
e := Entry{}
e.MD5 = fields[0]
e.Name = fields[1]
e.Inode = fields[2]
e.Mode = fields[3]
i, err := strconv.ParseInt(fields[4], 10, 64)
if err != nil {
return nil, fmt.Errorf("UID was not an integer: %s", err)
}
e.UID = int(i)
i, err = strconv.ParseInt(fields[5], 10, 64)
if err != nil {
return nil, fmt.Errorf("GID was not an integer: %s", err)
}
e.GID = int(i)
i, err = strconv.ParseInt(fields[6], 10, 64)
if err != nil {
//return nil, fmt.Errorf("Size was not an integer: %s", err)
i = 0
}
e.Size = i
i, err = strconv.ParseInt(fields[7], 10, 64)
if err != nil {
return nil, fmt.Errorf("AccessTime was not an integer: %s", err)
}
e.AccessTime = time.Unix(i, 0).UTC()
i, err = strconv.ParseInt(fields[8], 10, 64)
if err != nil {
return nil, fmt.Errorf("ModificationTime was not an integer: %s", err)
}
e.ModificationTime = time.Unix(i, 0).UTC()
i, err = strconv.ParseInt(fields[9], 10, 64)
if err != nil {
return nil, fmt.Errorf("ChangeTime was not an integer: %s", err)
}
e.ChangeTime = time.Unix(i, 0).UTC()
i, err = strconv.ParseInt(fields[10], 10, 64)
if err != nil {
return nil, fmt.Errorf("CreationTime was not an integer: %s", err)
}
e.CreationTime = time.Unix(i, 0).UTC()
return &e, nil
}
func entry2params(t time.Time, base map[string]string) govaluate.Parameters {
params := govaluate.MapParameters{}
for k, v := range base {
params[k] = v
}
params["hour"] = t.Hour()
params["min"] = t.Minute()
params["day"] = t.Day()
params["date"] = t.Unix()
params["weekday"] = t.Weekday().String()
params["h"] = t.Hour()
params["m"] = t.Minute()
params["D"] = t.Day()
params["d"] = t.Unix()
params["w"] = t.Weekday().String()
return params
}
// Match filters the events
func (r *Reader) Match(entry *Entry) (bool, error) {
ret := false
if r.expression == nil {
// no filter, everything matches
return true, nil
}
baseParams := map[string]string{
"path": entry.Name,
"p": entry.Name,
}
params := entry2params(entry.AccessTime, baseParams)
decision, err := r.expression.Eval(params)
if err != nil {
return false, fmt.Errorf("Could not evaluate expression: %s", err)
}
ret = ret || decision.(bool)
// fmt.Printf("AccessTime: %+v paramas=%+v\n", decision.(bool), params)
if decision.(bool) {
entry.MatchingTimestamp |= AccessTime
}
params = entry2params(entry.ModificationTime, baseParams)
decision, err = r.expression.Eval(params)
if err != nil {
return false, fmt.Errorf("Could not evaluate expression: %s", err)
}
// fmt.Printf("ModificationTime: %+v params=%+v\n", decision.(bool), params)
ret = ret || decision.(bool)
if decision.(bool) {
entry.MatchingTimestamp |= ModificationTime
}
params = entry2params(entry.CreationTime, baseParams)
decision, err = r.expression.Eval(params)
if err != nil {
return false, fmt.Errorf("Could not evaluate expression: %s", err)
}
// fmt.Printf("CreationTime: %+v params=%+v\n", decision.(bool), params)
ret = ret || decision.(bool)
if decision.(bool) {
entry.MatchingTimestamp |= CreationTime
}
params = entry2params(entry.ChangeTime, baseParams)
decision, err = r.expression.Eval(params)
if err != nil {
return false, fmt.Errorf("Could not evaluate expression: %s", err)
}
// fmt.Printf("ChangeTime: %+v params=%+v\n", decision.(bool), params)
ret = ret || decision.(bool)
if decision.(bool) {
entry.MatchingTimestamp |= ChangeTime
}
return ret, nil
}
// These constants are used as a bitmap in Entry.MatchingTimestamps, it is useful to know
// which timestamp matched the filter.
const (
AccessTime = 1 << iota
ModificationTime
ChangeTime
CreationTime
)
// TimeStampedEntry is a wrapper around Entry to add an outer timestamp used for the
// sorted array
type TimeStampedEntry struct {
Time time.Time
Entry *Entry
}
// tsEntrySortable is an interface to be used for sort.Sort()
type tsEntrySortable []TimeStampedEntry
func (e tsEntrySortable) Len() int {
return len(e)
}
func (e tsEntrySortable) Swap(i int, j int) {
e[i], e[j] = e[j], e[i]
}
func (e tsEntrySortable) Less(i int, j int) bool {
return !e[i].Time.After(e[j].Time)
}
// Slurp reads all the content of the file in memory, filter out the non-matching events
func (r *Reader) Slurp() (int, error) {
for {
e, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return 0, fmt.Errorf("Error while reading file: %s", err)
}
if e.AccessTime.After(smallestTime) && !r.Strict || (r.Strict && ((AccessTime & e.MatchingTimestamp) != 0)) {
r.entries = append(r.entries, TimeStampedEntry{e.AccessTime, e})
}
if e.ModificationTime != e.AccessTime {
if e.ModificationTime.After(smallestTime) && !r.Strict || (r.Strict && ((ModificationTime & e.MatchingTimestamp) != 0)) {
r.entries = append(r.entries, TimeStampedEntry{e.ModificationTime, e})
}
}
if e.ChangeTime != e.ModificationTime && e.ChangeTime != e.AccessTime {
if e.ChangeTime.After(smallestTime) && !r.Strict || (r.Strict && ((ChangeTime & e.MatchingTimestamp) != 0)) {
r.entries = append(r.entries, TimeStampedEntry{e.ChangeTime, e})
}
}
if e.CreationTime != e.ModificationTime && e.CreationTime != e.ChangeTime && e.CreationTime != e.AccessTime {
if e.CreationTime.After(smallestTime) && !r.Strict || (r.Strict && ((CreationTime & e.MatchingTimestamp) != 0)) {
r.entries = append(r.entries, TimeStampedEntry{e.CreationTime, e})
}
}
}
sort.Sort(r.entries)
r.offset = 0
return len(r.entries), nil
}
// Next returns the next sorted elements
func (r *Reader) Next() (*TimeStampedEntry, error) {
if r.offset < 0 {
return nil, fmt.Errorf("Not initialized, call Slurp() first")
}
if r.offset >= len(r.entries) {
return nil, io.EOF
}
r.offset++
return &r.entries[r.offset-1], nil
}
// Read consumes CSV objects, instantiates them to Entry objects and applies filter
func (r *Reader) Read() (*Entry, error) {
var entry *Entry
matched := false
for !matched {
fields, err := r.csv.Read()
if err != nil {
return nil, err
}
entry, err = fieldsToEntry(fields)
if err != nil {
return nil, err
}
matched, err = r.Match(entry)
if err != nil {
return nil, err
}
}
return entry, nil
}