fse_dump 3.1.6

Dumps the fseventsd entries from a mac
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//! Unique path aggregation and counting
//!
//! This module provides structures to aggregate FSEvents records by path,
//! combining their flags and counting occurrences.

use jiff::Timestamp;

use crate::flags as f;

/// Aggregates counts and flags for a unique path
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct UniqueCounts {
    counts: u64,
    flags: u32,
    earliest_timestamp: Option<Timestamp>,
    latest_timestamp: Option<Timestamp>,
}

impl UniqueCounts {
    /// Updates the count and combines the flag bits
    ///
    /// # Arguments
    /// * `flag` - The flag bits to OR with existing flags
    /// * `file_timestamp` - The file modification timestamp to track earliest/latest
    #[inline]
    pub fn update(&mut self, flag: u32, file_timestamp: Option<Timestamp>) {
        self.counts += 1;
        self.flags |= flag;

        if let Some(ts) = file_timestamp {
            match (self.earliest_timestamp, self.latest_timestamp) {
                (None, None) => {
                    // First update - initialize both
                    self.earliest_timestamp = Some(ts);
                    self.latest_timestamp = Some(ts);
                }
                (Some(earliest), Some(latest)) => {
                    // Track min and max timestamps
                    if ts < earliest {
                        self.earliest_timestamp = Some(ts);
                    }
                    if ts > latest {
                        self.latest_timestamp = Some(ts);
                    }
                }
                _ => {
                    // This shouldn't happen, but handle it gracefully
                    self.earliest_timestamp = Some(ts);
                    self.latest_timestamp = Some(ts);
                }
            }
        }
    }

    /// Converts internal counts to output format with parsed flag strings
    ///
    /// # Arguments
    /// * `path` - The file path associated with these counts
    ///
    /// # Returns
    /// A `UniqueOut` ready for serialization
    #[inline]
    pub fn into_unique_out(self, path: String) -> UniqueOut {
        let flags = f::parse_bits(self.flags);
        UniqueOut {
            path,
            counts: self.counts,
            flags: flags.norm,
            #[cfg(feature = "alt_flags")]
            alt_flags: flags.alt,
            earliest_timestamp: self.earliest_timestamp,
            latest_timestamp: self.latest_timestamp,
        }
    }

    /// Converts internal counts to output format without timestamps (for CSV)
    ///
    /// # Arguments
    /// * `path` - The file path associated with these counts
    ///
    /// # Returns
    /// A `UniqueOut` ready for serialization without timestamp fields
    #[inline]
    pub fn into_unique_out_no_timestamps(self, path: String) -> UniqueOut {
        let flags = f::parse_bits(self.flags);
        UniqueOut {
            path,
            counts: self.counts,
            flags: flags.norm,
            #[cfg(feature = "alt_flags")]
            alt_flags: flags.alt,
            earliest_timestamp: None,
            latest_timestamp: None,
        }
    }
}

/// Output structure for unique path aggregation results
#[derive(Debug, Serialize)]
pub struct UniqueOut {
    pub path: String,
    pub counts: u64,
    pub flags: &'static str,
    #[cfg(feature = "alt_flags")]
    pub alt_flags: &'static str,
    #[serde(
        skip_serializing_if = "should_skip_timestamp",
        serialize_with = "serialize_optional_timestamp"
    )]
    pub earliest_timestamp: Option<Timestamp>,
    #[serde(
        skip_serializing_if = "should_skip_timestamp",
        serialize_with = "serialize_optional_timestamp"
    )]
    pub latest_timestamp: Option<Timestamp>,
}

/// Helper to determine if timestamp should be skipped during serialization
fn should_skip_timestamp(ts: &Option<Timestamp>) -> bool {
    ts.is_none()
}

/// Custom serializer for `Option<Timestamp>` to produce ISO 8601 format
fn serialize_optional_timestamp<S>(
    timestamp: &Option<Timestamp>,
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    match timestamp {
        Some(ts) => serializer.serialize_str(&ts.to_string()),
        None => serializer.serialize_none(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Helper to create test timestamps from seconds since epoch
    fn ts(secs: i64) -> Option<Timestamp> {
        Timestamp::from_second(secs).ok()
    }

    #[test]
    fn test_unique_counts_default() {
        let uc = UniqueCounts::default();
        assert_eq!(uc.counts, 0);
        assert_eq!(uc.flags, 0);
        assert_eq!(uc.earliest_timestamp, None);
        assert_eq!(uc.latest_timestamp, None);
    }

    #[test]
    fn test_unique_counts_single_update() {
        let mut uc = UniqueCounts::default();
        uc.update(0x1000_0000, ts(100)); // Modified

        assert_eq!(uc.counts, 1);
        assert_eq!(uc.flags, 0x1000_0000);
        assert!(uc.earliest_timestamp.is_some());
        assert!(uc.latest_timestamp.is_some());
        assert_eq!(uc.earliest_timestamp, uc.latest_timestamp);
    }

    #[test]
    fn test_unique_counts_multiple_updates_same_flag() {
        let mut uc = UniqueCounts::default();
        uc.update(0x1000_0000, ts(100)); // Modified
        uc.update(0x1000_0000, ts(200)); // Modified
        uc.update(0x1000_0000, ts(150)); // Modified

        assert_eq!(uc.counts, 3, "Count should increment for each update");
        assert_eq!(uc.flags, 0x1000_0000, "Flags should remain the same");
        assert!(uc.earliest_timestamp.is_some());
        assert!(uc.latest_timestamp.is_some());
        assert!(
            uc.earliest_timestamp < uc.latest_timestamp,
            "Should track earliest < latest"
        );
    }

    #[test]
    fn test_unique_counts_multiple_updates_different_flags() {
        let mut uc = UniqueCounts::default();
        uc.update(0x1000_0000, ts(100)); // Modified
        uc.update(0x0800_0000, ts(200)); // Renamed
        uc.update(0x0100_0000, ts(150)); // Created

        assert_eq!(uc.counts, 3, "Count should be 3");
        assert_eq!(
            uc.flags,
            0x1000_0000 | 0x0800_0000 | 0x0100_0000,
            "Flags should be ORed together"
        );
        assert!(uc.earliest_timestamp.is_some());
        assert!(uc.latest_timestamp.is_some());
    }

    #[test]
    fn test_unique_counts_flag_accumulation() {
        let mut uc = UniqueCounts::default();

        // Update with different flags, simulating different operations on same path
        uc.update(0x0100_0000, ts(1000)); // Created
        assert_eq!(uc.flags, 0x0100_0000);

        uc.update(0x1000_0000, ts(2000)); // Modified
        assert_eq!(uc.flags, 0x0100_0000 | 0x1000_0000);

        uc.update(0x0800_0000, ts(1500)); // Renamed
        assert_eq!(uc.flags, 0x0100_0000 | 0x1000_0000 | 0x0800_0000);

        assert_eq!(uc.counts, 3);
        assert!(uc.earliest_timestamp.is_some());
        assert!(uc.latest_timestamp.is_some());
    }

    #[test]
    fn test_unique_counts_idempotent_flag_or() {
        let mut uc = UniqueCounts::default();

        uc.update(0x1000_0000, ts(100)); // Modified
        uc.update(0x1000_0000, ts(200)); // Modified again

        // Flag should only be set once (OR is idempotent)
        assert_eq!(uc.flags, 0x1000_0000);
        assert_eq!(uc.counts, 2, "But count should still be 2");
        assert!(uc.earliest_timestamp.is_some());
        assert!(uc.latest_timestamp.is_some());
    }

    #[test]
    fn test_into_unique_out() {
        let mut uc = UniqueCounts::default();
        uc.update(0x1000_0000, ts(1000)); // Modified
        uc.update(0x0100_0000, ts(2000)); // Created

        let out = uc.into_unique_out("/test/path.txt".to_string());

        assert_eq!(out.path, "/test/path.txt");
        assert_eq!(out.counts, 2);
        assert!(out.flags.contains("Modified"));
        assert!(out.flags.contains("Created"));
        assert!(out.earliest_timestamp.is_some());
        assert!(out.latest_timestamp.is_some());
    }

    #[test]
    fn test_into_unique_out_zero_counts() {
        let uc = UniqueCounts::default();
        let out = uc.into_unique_out("/empty/path".to_string());

        assert_eq!(out.path, "/empty/path");
        assert_eq!(out.counts, 0);
        assert_eq!(out.flags, "", "Zero flags should produce empty string");
        assert_eq!(out.earliest_timestamp, None);
        assert_eq!(out.latest_timestamp, None);
    }

    #[test]
    fn test_into_unique_out_single_flag() {
        let mut uc = UniqueCounts::default();
        uc.update(0x0200_0000, ts(500)); // Removed

        let out = uc.into_unique_out("/deleted/file".to_string());

        assert_eq!(out.counts, 1);
        assert_eq!(out.flags, "Removed");
        assert!(out.earliest_timestamp.is_some());
        assert!(out.latest_timestamp.is_some());
    }

    #[test]
    fn test_into_unique_out_all_flags() {
        let mut uc = UniqueCounts::default();

        // Add all possible flags
        uc.update(0x0000_0001, ts(1)); // FolderEvent
        uc.update(0x0000_0002, ts(2)); // Mount
        uc.update(0x0000_0004, ts(3)); // Unmount
        uc.update(0x0000_0020, ts(4)); // EndOfTransaction
        uc.update(0x0000_0800, ts(5)); // LastHardLinkRemoved
        uc.update(0x0000_1000, ts(6)); // HardLink
        uc.update(0x0000_4000, ts(7)); // SymbolicLink
        uc.update(0x0000_8000, ts(8)); // FileEvent
        uc.update(0x0001_0000, ts(9)); // PermissionChange
        uc.update(0x0002_0000, ts(10)); // ExtendedAttrModified
        uc.update(0x0004_0000, ts(11)); // ExtendedAttrRemoved
        uc.update(0x0010_0000, ts(12)); // DocumentRevisioning
        uc.update(0x0040_0000, ts(13)); // ItemCloned
        uc.update(0x0100_0000, ts(14)); // Created
        uc.update(0x0200_0000, ts(15)); // Removed
        uc.update(0x0400_0000, ts(16)); // InodeMetaMod
        uc.update(0x0800_0000, ts(17)); // Renamed
        uc.update(0x1000_0000, ts(18)); // Modified
        uc.update(0x2000_0000, ts(19)); // Exchange
        uc.update(0x4000_0000, ts(20)); // FinderInfoMod
        uc.update(0x8000_0000, ts(21)); // FolderCreated

        let out = uc.into_unique_out("/complex/path".to_string());

        assert_eq!(out.counts, 21);
        // Verify some key flags are present
        assert!(out.flags.contains("Created"));
        assert!(out.flags.contains("Modified"));
        assert!(out.flags.contains("Removed"));
        assert!(out.flags.contains("FileEvent"));
        assert!(out.earliest_timestamp.is_some());
        assert!(out.latest_timestamp.is_some());
    }

    #[test]
    fn test_unique_out_serialization() {
        let mut uc = UniqueCounts::default();
        uc.update(0x1000_0000, ts(100)); // Modified
        uc.update(0x1000_0000, ts(200)); // Modified again

        let out = uc.into_unique_out("/test.txt".to_string());
        let json = serde_json::to_string(&out).expect("Should serialize to JSON");

        assert!(json.contains("/test.txt"));
        assert!(json.contains("Modified"));
        assert!(json.contains("\"counts\":2"));
        // Timestamps will be ISO 8601 format, just check they exist
        assert!(json.contains("earliest_timestamp"));
        assert!(json.contains("latest_timestamp"));
    }

    #[test]
    fn test_unique_counts_equality() {
        let uc1 = UniqueCounts {
            counts: 5,
            flags: 0x1000_0000,
            earliest_timestamp: ts(100),
            latest_timestamp: ts(200),
        };
        let uc2 = UniqueCounts {
            counts: 5,
            flags: 0x1000_0000,
            earliest_timestamp: ts(100),
            latest_timestamp: ts(200),
        };
        let uc3 = UniqueCounts {
            counts: 3,
            flags: 0x1000_0000,
            earliest_timestamp: ts(100),
            latest_timestamp: ts(200),
        };

        assert_eq!(uc1, uc2, "Same counts and flags should be equal");
        assert_ne!(uc1, uc3, "Different counts should not be equal");
    }

    #[test]
    fn test_unique_counts_large_count() {
        let mut uc = UniqueCounts::default();

        // Simulate a frequently accessed file
        for i in 0..10000 {
            uc.update(0x1000_0000, ts(i)); // Modified
        }

        assert_eq!(uc.counts, 10000);
        assert_eq!(uc.flags, 0x1000_0000);
        assert!(uc.earliest_timestamp.is_some());
        assert!(uc.latest_timestamp.is_some());
    }

    #[test]
    fn test_unique_counts_update_with_zero() {
        let mut uc = UniqueCounts::default();
        uc.update(0, ts(42)); // No flags

        assert_eq!(uc.counts, 1, "Count should increment even with zero flags");
        assert_eq!(uc.flags, 0, "Flags should remain zero");
        assert!(uc.earliest_timestamp.is_some());
        assert!(uc.latest_timestamp.is_some());
    }

    #[test]
    fn test_path_special_characters() {
        let mut uc = UniqueCounts::default();
        uc.update(0x1000_0000, ts(999));

        let paths = vec![
            "/path with spaces/file.txt",
            "/path/with/üñíçödé/file.txt",
            "/path/with/emoji/😀.txt",
            "/path/with/quotes/\"file\".txt",
        ];

        for path in paths {
            let out = uc.into_unique_out(path.to_string());
            assert_eq!(out.path, path);
            assert!(out.earliest_timestamp.is_some());
            assert!(out.latest_timestamp.is_some());
        }
    }

    #[test]
    fn test_unique_counts_debug_format() {
        let uc = UniqueCounts {
            counts: 42,
            flags: 0x1000_0000,
            earliest_timestamp: ts(100),
            latest_timestamp: ts(200),
        };
        let debug_str = format!("{:?}", uc);

        assert!(debug_str.contains("42"));
        assert!(debug_str.contains("268435456") || debug_str.contains("1000_0000"));
    }

    #[cfg(feature = "alt_flags")]
    #[test]
    fn test_unique_out_with_alt_flags() {
        let mut uc = UniqueCounts::default();
        uc.update(0x0000_0001, ts(1)); // Different meaning in alt_flags

        let out = uc.into_unique_out("/test".to_string());

        // Should have both norm and alt_flags populated
        assert!(!out.flags.is_empty());
        assert!(!out.alt_flags.is_empty());
        assert!(out.earliest_timestamp.is_some());
        assert!(out.latest_timestamp.is_some());
    }

    #[test]
    fn test_timestamp_tracking_out_of_order() {
        let mut uc = UniqueCounts::default();

        // Updates arrive out of order
        uc.update(0x1000_0000, ts(500));
        uc.update(0x1000_0000, ts(100)); // Earlier timestamp
        uc.update(0x1000_0000, ts(1000)); // Later timestamp
        uc.update(0x1000_0000, ts(300));

        assert_eq!(uc.counts, 4);
        assert!(
            uc.earliest_timestamp < uc.latest_timestamp,
            "Should find minimum timestamp"
        );
    }

    #[test]
    fn test_timestamp_tracking_reverse_order() {
        let mut uc = UniqueCounts::default();

        // Updates arrive in reverse chronological order
        uc.update(0x1000_0000, ts(1000));
        uc.update(0x1000_0000, ts(900));
        uc.update(0x1000_0000, ts(800));
        uc.update(0x1000_0000, ts(700));

        assert_eq!(uc.counts, 4);
        assert!(uc.earliest_timestamp.is_some());
        assert!(uc.latest_timestamp.is_some());
        assert!(uc.earliest_timestamp < uc.latest_timestamp);
    }

    #[test]
    fn test_timestamp_with_same_values() {
        let mut uc = UniqueCounts::default();

        // All updates have the same timestamp (from same file)
        uc.update(0x1000_0000, ts(500));
        uc.update(0x0800_0000, ts(500));
        uc.update(0x0100_0000, ts(500));

        assert_eq!(uc.counts, 3);
        assert_eq!(uc.earliest_timestamp, uc.latest_timestamp);
    }
}