lcpfs 2026.1.102

LCP File System - A ZFS-inspired copy-on-write filesystem for Rust
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
// Copyright 2025 LunaOS Contributors
// SPDX-License-Identifier: Apache-2.0

//! Diff engine for comparing filesystem state between two points in time.
//!
//! This module computes the differences between two TXGs, identifying
//! created, modified, deleted, and renamed files.

use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;

use super::types::{ChangeType, DiffEntry, TimeError};
use super::walker::{HistoricalEntry, HistoricalTreeProvider, HistoricalTreeWalker, WalkOptions};

// ═══════════════════════════════════════════════════════════════════════════════
// DIFF OPTIONS
// ═══════════════════════════════════════════════════════════════════════════════

/// Options for diff computation.
#[derive(Debug, Clone)]
pub struct DiffOptions {
    /// Include content changes (expensive: compares checksums).
    pub include_content: bool,
    /// Include metadata-only changes.
    pub include_metadata: bool,
    /// Detect renames by matching checksums.
    pub detect_renames: bool,
    /// Maximum depth to recurse (-1 for unlimited).
    pub max_depth: i32,
    /// Filter by change types.
    pub change_types: Option<Vec<ChangeType>>,
    /// Maximum results to return.
    pub limit: Option<usize>,
}

impl Default for DiffOptions {
    fn default() -> Self {
        Self {
            include_content: true,
            include_metadata: true,
            detect_renames: true,
            max_depth: -1,
            change_types: None,
            limit: None,
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// DIFF ENGINE
// ═══════════════════════════════════════════════════════════════════════════════

/// Engine for computing diffs between filesystem states.
pub struct DiffEngine<'a, P: HistoricalTreeProvider> {
    provider: &'a P,
}

impl<'a, P: HistoricalTreeProvider> DiffEngine<'a, P> {
    /// Create a new diff engine.
    pub fn new(provider: &'a P) -> Self {
        Self { provider }
    }

    /// Compute diff between two TXGs for a given path.
    pub fn diff(
        &self,
        path: &str,
        from_txg: u64,
        to_txg: u64,
        options: &DiffOptions,
    ) -> Result<Vec<DiffEntry>, TimeError> {
        // Get entries at both TXGs
        let from_walker = HistoricalTreeWalker::new(self.provider, from_txg);
        let to_walker = HistoricalTreeWalker::new(self.provider, to_txg);

        let walk_options = WalkOptions {
            max_depth: options.max_depth,
            ..Default::default()
        };

        // Collect entries from both snapshots
        let from_entries = self.collect_entries(&from_walker, path, &walk_options)?;
        let to_entries = self.collect_entries(&to_walker, path, &walk_options)?;

        // Build maps by path
        let from_map: BTreeMap<String, HistoricalEntry> = from_entries
            .into_iter()
            .map(|e| (e.path.clone(), e))
            .collect();
        let to_map: BTreeMap<String, HistoricalEntry> = to_entries
            .into_iter()
            .map(|e| (e.path.clone(), e))
            .collect();

        let mut diffs = Vec::new();

        // Track checksums for rename detection
        let mut deleted_by_checksum: BTreeMap<[u64; 4], (String, HistoricalEntry)> =
            BTreeMap::new();

        // Find deleted and modified entries
        for (path, from_entry) in &from_map {
            if let Some(to_entry) = to_map.get(path) {
                // Path exists in both - check for modifications
                if let Some(change) = self.detect_change(from_entry, to_entry, options) {
                    diffs.push(change);
                }
            } else {
                // Deleted - store for potential rename detection
                if options.detect_renames && from_entry.checksum != [0; 4] {
                    deleted_by_checksum
                        .insert(from_entry.checksum, (path.clone(), from_entry.clone()));
                } else {
                    diffs.push(DiffEntry {
                        path: path.clone(),
                        change_type: ChangeType::Deleted,
                        old_size: Some(from_entry.size),
                        new_size: None,
                        old_checksum: Some(from_entry.checksum),
                        new_checksum: None,
                        old_mtime: Some(from_entry.mtime),
                        new_mtime: None,
                        txg: to_txg,
                    });
                }
            }
        }

        // Find created entries and detect renames
        for (path, to_entry) in &to_map {
            if !from_map.contains_key(path) {
                // Check for rename
                if options.detect_renames && to_entry.checksum != [0; 4] {
                    if let Some((old_path, old_entry)) =
                        deleted_by_checksum.remove(&to_entry.checksum)
                    {
                        // This is a rename
                        diffs.push(DiffEntry {
                            path: path.clone(),
                            change_type: ChangeType::Renamed { old_path },
                            old_size: Some(old_entry.size),
                            new_size: Some(to_entry.size),
                            old_checksum: Some(old_entry.checksum),
                            new_checksum: Some(to_entry.checksum),
                            old_mtime: Some(old_entry.mtime),
                            new_mtime: Some(to_entry.mtime),
                            txg: to_entry.txg,
                        });
                        continue;
                    }
                }

                // New file
                diffs.push(DiffEntry {
                    path: path.clone(),
                    change_type: ChangeType::Created,
                    old_size: None,
                    new_size: Some(to_entry.size),
                    old_checksum: None,
                    new_checksum: Some(to_entry.checksum),
                    old_mtime: None,
                    new_mtime: Some(to_entry.mtime),
                    txg: to_entry.txg,
                });
            }
        }

        // Remaining deleted entries (not matched to renames)
        for (_, (path, entry)) in deleted_by_checksum {
            diffs.push(DiffEntry {
                path,
                change_type: ChangeType::Deleted,
                old_size: Some(entry.size),
                new_size: None,
                old_checksum: Some(entry.checksum),
                new_checksum: None,
                old_mtime: Some(entry.mtime),
                new_mtime: None,
                txg: to_txg,
            });
        }

        // Apply change type filter
        if let Some(ref filter_types) = options.change_types {
            diffs.retain(|d| {
                filter_types.iter().any(|ct| {
                    matches!(
                        (&d.change_type, ct),
                        (ChangeType::Created, ChangeType::Created)
                            | (ChangeType::Modified, ChangeType::Modified)
                            | (ChangeType::Deleted, ChangeType::Deleted)
                            | (ChangeType::Renamed { .. }, ChangeType::Renamed { .. })
                            | (ChangeType::MetadataChanged, ChangeType::MetadataChanged)
                    )
                })
            });
        }

        // Sort by path for consistent output
        diffs.sort_by(|a, b| a.path.cmp(&b.path));

        // Apply limit
        if let Some(limit) = options.limit {
            diffs.truncate(limit);
        }

        Ok(diffs)
    }

    /// Collect all entries under a path.
    fn collect_entries(
        &self,
        walker: &HistoricalTreeWalker<P>,
        path: &str,
        options: &WalkOptions,
    ) -> Result<Vec<HistoricalEntry>, TimeError> {
        // First check if path exists
        if !walker.exists(path) {
            return Ok(Vec::new());
        }

        // Get the entry itself
        let entry = walker.lookup(path)?;

        if entry.is_dir() {
            // Walk directory tree
            let mut entries = walker.walk(path, options)?;
            entries.insert(0, entry);
            Ok(entries)
        } else {
            // Single file
            Ok(vec![entry])
        }
    }

    /// Detect the type of change between two versions of an entry.
    fn detect_change(
        &self,
        from: &HistoricalEntry,
        to: &HistoricalEntry,
        options: &DiffOptions,
    ) -> Option<DiffEntry> {
        // Check content change
        if options.include_content && from.checksum != to.checksum {
            return Some(DiffEntry {
                path: to.path.clone(),
                change_type: ChangeType::Modified,
                old_size: Some(from.size),
                new_size: Some(to.size),
                old_checksum: Some(from.checksum),
                new_checksum: Some(to.checksum),
                old_mtime: Some(from.mtime),
                new_mtime: Some(to.mtime),
                txg: to.txg,
            });
        }

        // Check metadata change
        if options.include_metadata {
            let metadata_changed = from.mode != to.mode
                || from.uid != to.uid
                || from.gid != to.gid
                || from.mtime != to.mtime;

            if metadata_changed {
                return Some(DiffEntry {
                    path: to.path.clone(),
                    change_type: ChangeType::MetadataChanged,
                    old_size: Some(from.size),
                    new_size: Some(to.size),
                    old_checksum: Some(from.checksum),
                    new_checksum: Some(to.checksum),
                    old_mtime: Some(from.mtime),
                    new_mtime: Some(to.mtime),
                    txg: to.txg,
                });
            }
        }

        None
    }

    /// Quick diff that only checks for existence changes (faster).
    pub fn quick_diff(
        &self,
        path: &str,
        from_txg: u64,
        to_txg: u64,
    ) -> Result<DiffSummary, TimeError> {
        let from_walker = HistoricalTreeWalker::new(self.provider, from_txg);
        let to_walker = HistoricalTreeWalker::new(self.provider, to_txg);

        let walk_options = WalkOptions::default();

        let from_entries = self.collect_entries(&from_walker, path, &walk_options)?;
        let to_entries = self.collect_entries(&to_walker, path, &walk_options)?;

        let from_paths: Vec<_> = from_entries.iter().map(|e| e.path.as_str()).collect();
        let to_paths: Vec<_> = to_entries.iter().map(|e| e.path.as_str()).collect();

        let mut created = 0;
        let mut deleted = 0;
        let mut modified = 0;

        for path in &to_paths {
            if !from_paths.contains(path) {
                created += 1;
            }
        }

        for path in &from_paths {
            if !to_paths.contains(path) {
                deleted += 1;
            }
        }

        // Count modified (paths that exist in both)
        for from_entry in &from_entries {
            if let Some(to_entry) = to_entries.iter().find(|e| e.path == from_entry.path) {
                if from_entry.checksum != to_entry.checksum {
                    modified += 1;
                }
            }
        }

        Ok(DiffSummary {
            created,
            modified,
            deleted,
            renamed: 0,
            metadata_changed: 0,
        })
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// DIFF SUMMARY
// ═══════════════════════════════════════════════════════════════════════════════

/// Summary statistics of a diff operation.
#[derive(Debug, Clone, Default)]
pub struct DiffSummary {
    /// Number of created files.
    pub created: usize,
    /// Number of modified files.
    pub modified: usize,
    /// Number of deleted files.
    pub deleted: usize,
    /// Number of renamed files.
    pub renamed: usize,
    /// Number of metadata-only changes.
    pub metadata_changed: usize,
}

impl DiffSummary {
    /// Total number of changes.
    pub fn total(&self) -> usize {
        self.created + self.modified + self.deleted + self.renamed + self.metadata_changed
    }

    /// Check if there are no changes.
    pub fn is_empty(&self) -> bool {
        self.total() == 0
    }

    /// Compute summary from a list of diff entries.
    pub fn from_entries(entries: &[DiffEntry]) -> Self {
        let mut summary = DiffSummary::default();

        for entry in entries {
            match &entry.change_type {
                ChangeType::Created => summary.created += 1,
                ChangeType::Modified => summary.modified += 1,
                ChangeType::Deleted => summary.deleted += 1,
                ChangeType::Renamed { .. } => summary.renamed += 1,
                ChangeType::MetadataChanged => summary.metadata_changed += 1,
            }
        }

        summary
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// INCREMENTAL DIFF ITERATOR
// ═══════════════════════════════════════════════════════════════════════════════

/// Iterator for computing diffs incrementally (memory-efficient for large trees).
pub struct IncrementalDiffIterator<'a, P: HistoricalTreeProvider> {
    engine: &'a DiffEngine<'a, P>,
    from_txg: u64,
    to_txg: u64,
    path: String,
    options: DiffOptions,
    current_diffs: Vec<DiffEntry>,
    position: usize,
    exhausted: bool,
}

impl<'a, P: HistoricalTreeProvider> IncrementalDiffIterator<'a, P> {
    /// Create a new incremental diff iterator.
    pub fn new(
        engine: &'a DiffEngine<'a, P>,
        path: &str,
        from_txg: u64,
        to_txg: u64,
        options: DiffOptions,
    ) -> Self {
        Self {
            engine,
            from_txg,
            to_txg,
            path: path.into(),
            options,
            current_diffs: Vec::new(),
            position: 0,
            exhausted: false,
        }
    }

    /// Load next batch of diffs.
    fn load_batch(&mut self) -> bool {
        if self.exhausted {
            return false;
        }

        // For now, load all at once (could be optimized for true incremental)
        match self
            .engine
            .diff(&self.path, self.from_txg, self.to_txg, &self.options)
        {
            Ok(diffs) => {
                self.current_diffs = diffs;
                self.position = 0;
                self.exhausted = true;
                !self.current_diffs.is_empty()
            }
            Err(_) => {
                self.exhausted = true;
                false
            }
        }
    }
}

impl<'a, P: HistoricalTreeProvider> Iterator for IncrementalDiffIterator<'a, P> {
    type Item = DiffEntry;

    fn next(&mut self) -> Option<Self::Item> {
        // Check if we need to load more diffs
        if self.position >= self.current_diffs.len() && !self.load_batch() {
            return None;
        }

        if self.position < self.current_diffs.len() {
            let entry = self.current_diffs[self.position].clone();
            self.position += 1;
            Some(entry)
        } else {
            None
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// TESTS
// ═══════════════════════════════════════════════════════════════════════════════

#[cfg(test)]
mod tests {
    use super::super::types::FileType;
    use super::super::walker::InMemoryTreeProvider;
    use super::*;

    fn create_entry(
        path: &str,
        name: &str,
        file_type: FileType,
        size: u64,
        txg: u64,
        checksum: [u64; 4],
    ) -> HistoricalEntry {
        HistoricalEntry {
            name: name.into(),
            path: path.into(),
            object_id: path.len() as u64,
            parent_id: 1,
            file_type,
            size,
            mode: 0o644,
            uid: 1000,
            gid: 1000,
            atime: txg * 1000,
            mtime: txg * 1000,
            ctime: txg * 1000,
            txg,
            checksum,
            nlinks: 1,
            blocks: size.div_ceil(512),
            generation: txg,
        }
    }

    fn create_test_provider() -> InMemoryTreeProvider {
        let mut provider = InMemoryTreeProvider::new();

        // TXG 100: Initial state with 2 files
        provider.add_entry(
            100,
            create_entry("/", "", FileType::Directory, 0, 100, [0; 4]),
        );
        provider.add_entry(
            100,
            create_entry("/data", "data", FileType::Directory, 0, 100, [0; 4]),
        );
        provider.add_entry(
            100,
            create_entry(
                "/data/file1.txt",
                "file1.txt",
                FileType::Regular,
                100,
                100,
                [1, 0, 0, 0],
            ),
        );
        provider.add_entry(
            100,
            create_entry(
                "/data/file2.txt",
                "file2.txt",
                FileType::Regular,
                200,
                100,
                [2, 0, 0, 0],
            ),
        );

        // TXG 200: Modified file1, added file3 (file2 unchanged)
        provider.add_entry(
            200,
            create_entry("/", "", FileType::Directory, 0, 200, [0; 4]),
        );
        provider.add_entry(
            200,
            create_entry("/data", "data", FileType::Directory, 0, 200, [0; 4]),
        );
        provider.add_entry(
            200,
            create_entry(
                "/data/file1.txt",
                "file1.txt",
                FileType::Regular,
                150,
                200,
                [10, 0, 0, 0], // Changed checksum
            ),
        );
        provider.add_entry(
            200,
            create_entry(
                "/data/file3.txt",
                "file3.txt",
                FileType::Regular,
                300,
                200,
                [3, 0, 0, 0],
            ),
        );

        provider
    }

    #[test]
    fn test_diff_basic() {
        let provider = create_test_provider();
        let engine = DiffEngine::new(&provider);

        let diffs = engine
            .diff("/data", 100, 200, &DiffOptions::default())
            .unwrap();

        // Should have: created file3, modified file1 (file2 unchanged)
        let created: Vec<_> = diffs
            .iter()
            .filter(|d| matches!(d.change_type, ChangeType::Created))
            .collect();
        assert_eq!(created.len(), 1);
        assert_eq!(created[0].path, "/data/file3.txt");

        let modified: Vec<_> = diffs
            .iter()
            .filter(|d| matches!(d.change_type, ChangeType::Modified))
            .collect();
        assert_eq!(modified.len(), 1);
        assert_eq!(modified[0].path, "/data/file1.txt");
    }

    #[test]
    fn test_diff_rename_detection() {
        // Note: In-memory provider doesn't support deletions, so we test
        // that adding a file with same checksum is detected as a creation
        // (rename detection requires deletion support).
        let mut provider = InMemoryTreeProvider::new();

        // TXG 100: one file
        provider.add_entry(
            100,
            create_entry("/", "", FileType::Directory, 0, 100, [0; 4]),
        );
        provider.add_entry(
            100,
            create_entry("/data", "data", FileType::Directory, 0, 100, [0; 4]),
        );
        provider.add_entry(
            100,
            create_entry(
                "/data/file.txt",
                "file.txt",
                FileType::Regular,
                100,
                100,
                [42, 42, 42, 42],
            ),
        );

        // TXG 200: add another file (same checksum, different name)
        provider.add_entry(
            200,
            create_entry("/", "", FileType::Directory, 0, 200, [0; 4]),
        );
        provider.add_entry(
            200,
            create_entry("/data", "data", FileType::Directory, 0, 200, [0; 4]),
        );
        provider.add_entry(
            200,
            create_entry(
                "/data/copy.txt",
                "copy.txt",
                FileType::Regular,
                100,
                200,
                [42, 42, 42, 42], // Same checksum = could be copy
            ),
        );

        let engine = DiffEngine::new(&provider);
        let options = DiffOptions {
            detect_renames: true,
            ..Default::default()
        };

        let diffs = engine.diff("/data", 100, 200, &options).unwrap();

        // Should see copy.txt as created (original still exists)
        let created: Vec<_> = diffs
            .iter()
            .filter(|d| matches!(d.change_type, ChangeType::Created))
            .collect();
        assert_eq!(created.len(), 1);
        assert_eq!(created[0].path, "/data/copy.txt");
    }

    #[test]
    fn test_diff_filter_change_types() {
        let provider = create_test_provider();
        let engine = DiffEngine::new(&provider);

        let options = DiffOptions {
            change_types: Some(vec![ChangeType::Created]),
            ..Default::default()
        };

        let diffs = engine.diff("/data", 100, 200, &options).unwrap();

        // Should only have created entries
        assert!(
            diffs
                .iter()
                .all(|d| matches!(d.change_type, ChangeType::Created))
        );
    }

    #[test]
    fn test_diff_limit() {
        let provider = create_test_provider();
        let engine = DiffEngine::new(&provider);

        let options = DiffOptions {
            limit: Some(1),
            ..Default::default()
        };

        let diffs = engine.diff("/data", 100, 200, &options).unwrap();
        assert_eq!(diffs.len(), 1);
    }

    #[test]
    fn test_quick_diff() {
        let provider = create_test_provider();
        let engine = DiffEngine::new(&provider);

        let summary = engine.quick_diff("/data", 100, 200).unwrap();

        // file3.txt created, file1.txt modified, file2.txt unchanged
        assert_eq!(summary.created, 1);
        assert_eq!(summary.modified, 1);
    }

    #[test]
    fn test_diff_summary() {
        let entries = vec![
            DiffEntry {
                path: "/a".into(),
                change_type: ChangeType::Created,
                old_size: None,
                new_size: Some(100),
                old_checksum: None,
                new_checksum: Some([1; 4]),
                old_mtime: None,
                new_mtime: Some(1000),
                txg: 200,
            },
            DiffEntry {
                path: "/b".into(),
                change_type: ChangeType::Modified,
                old_size: Some(50),
                new_size: Some(100),
                old_checksum: Some([1; 4]),
                new_checksum: Some([2; 4]),
                old_mtime: Some(500),
                new_mtime: Some(1000),
                txg: 200,
            },
            DiffEntry {
                path: "/c".into(),
                change_type: ChangeType::Deleted,
                old_size: Some(100),
                new_size: None,
                old_checksum: Some([3; 4]),
                new_checksum: None,
                old_mtime: Some(500),
                new_mtime: None,
                txg: 200,
            },
        ];

        let summary = DiffSummary::from_entries(&entries);
        assert_eq!(summary.created, 1);
        assert_eq!(summary.modified, 1);
        assert_eq!(summary.deleted, 1);
        assert_eq!(summary.total(), 3);
    }

    #[test]
    fn test_diff_no_changes() {
        let mut provider = InMemoryTreeProvider::new();

        // Same state at both TXGs
        provider.add_entry(
            100,
            create_entry("/", "", FileType::Directory, 0, 100, [0; 4]),
        );
        provider.add_entry(
            100,
            create_entry("/data", "data", FileType::Directory, 0, 100, [0; 4]),
        );
        provider.add_entry(
            100,
            create_entry(
                "/data/file.txt",
                "file.txt",
                FileType::Regular,
                100,
                100,
                [1; 4],
            ),
        );

        // TXG 200: same file, same content
        provider.add_entry(
            200,
            create_entry("/", "", FileType::Directory, 0, 200, [0; 4]),
        );
        provider.add_entry(
            200,
            create_entry("/data", "data", FileType::Directory, 0, 200, [0; 4]),
        );
        provider.add_entry(
            200,
            create_entry(
                "/data/file.txt",
                "file.txt",
                FileType::Regular,
                100,
                200,
                [1; 4], // Same checksum
            ),
        );

        let engine = DiffEngine::new(&provider);

        // Disable metadata checking since mtime will differ
        let options = DiffOptions {
            include_metadata: false,
            ..Default::default()
        };

        let diffs = engine.diff("/data", 100, 200, &options).unwrap();
        assert!(diffs.is_empty());
    }

    #[test]
    fn test_incremental_iterator() {
        let provider = create_test_provider();
        let engine = DiffEngine::new(&provider);

        let iter = IncrementalDiffIterator::new(&engine, "/data", 100, 200, DiffOptions::default());

        let diffs: Vec<_> = iter.collect();
        assert_eq!(diffs.len(), 3);
    }
}