openjd-snapshots 0.1.2

[Experimental] Job attachments snapshot library for content-addressed file tree operations. The v2023 on-disk manifest format is stable and used by AWS Deadline Cloud; the v2025 format is an experimental draft.
Documentation
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// Copyright by contributors to this project.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)

use crate::hash::HashAlgorithm;
use crate::manifest::{AbsSnapshot, DirEntry, FileEntry, Manifest, SymlinkPolicy};
use crate::path_util::normalize_path;
use crate::{SnapshotError, DEFAULT_FILE_CHUNK_SIZE};
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::time::UNIX_EPOCH;
use tracing::{debug, warn};
use walkdir::WalkDir;

#[derive(Debug, Clone)]
pub struct CollectOptions {
    pub optional_filenames: Vec<PathBuf>,
    pub symlink_policy: SymlinkPolicy,
    pub file_chunk_size_bytes: Option<i64>,
}

impl Default for CollectOptions {
    fn default() -> Self {
        Self {
            optional_filenames: vec![],
            symlink_policy: SymlinkPolicy::CollapseEscaping,
            file_chunk_size_bytes: None,
        }
    }
}

/// Metadata helper: get (size, mtime_micros, runnable) from std::fs::Metadata.
fn file_meta(meta: &std::fs::Metadata) -> crate::Result<(u64, u64, bool)> {
    let size = meta.len();
    let mtime = meta
        .modified()?
        .duration_since(UNIX_EPOCH)
        .map_err(|e| SnapshotError::Task(e.to_string()))?
        .as_micros() as u64;
    #[cfg(unix)]
    let runnable = {
        use std::os::unix::fs::PermissionsExt;
        meta.permissions().mode() & 0o111 != 0
    };
    #[cfg(not(unix))]
    let runnable = false;
    Ok((size, mtime, runnable))
}

/// Make a path absolute and normalize it. This is purely lexical
/// (`std::path::absolute` never touches the filesystem or follows symlinks).
fn abs_normalized(p: &Path) -> crate::Result<String> {
    let abs = std::path::absolute(p).map_err(|e| {
        SnapshotError::Io(std::io::Error::new(
            e.kind(),
            format!("{}: {}", p.display(), e),
        ))
    })?;
    Ok(normalize_path(&abs.to_string_lossy()))
}

/// Represents a deferred symlink found during directory walking.
struct DeferredSymlink {
    /// The absolute normalized path of the symlink itself.
    path: String,
    /// The filesystem path for read_link / metadata calls.
    fs_path: PathBuf,
}

/// Collects directories and files into an absolute-path snapshot manifest.
///
/// Walks the given directories recursively and includes the specified filenames,
/// capturing metadata (size, mtime, permissions) but not computing hashes.
/// Symlinks are handled according to the given policy.
pub fn collect_abs_snapshot(
    directories: &[impl AsRef<Path>],
    filenames: &[impl AsRef<Path>],
    options: CollectOptions,
) -> crate::Result<AbsSnapshot> {
    let chunk_size = options
        .file_chunk_size_bytes
        .unwrap_or(DEFAULT_FILE_CHUNK_SIZE);
    let mut files: Vec<FileEntry> = Vec::new();
    let mut dirs: Vec<DirEntry> = Vec::new();
    let mut deferred_symlinks: Vec<DeferredSymlink> = Vec::new();
    let mut seen_paths: HashSet<String> = HashSet::new();

    // Walk directories
    for dir in directories {
        let dir = dir.as_ref();
        if !dir.exists() {
            return Err(SnapshotError::FileNotFound(dir.display().to_string()));
        }
        if !dir.is_dir() {
            return Err(SnapshotError::Validation(format!(
                "not a directory: {}",
                dir.display()
            )));
        }
        for entry in WalkDir::new(dir).follow_links(false) {
            let entry =
                entry.map_err(|e| SnapshotError::Io(std::io::Error::other(e.to_string())))?;
            let ft = entry.file_type();
            let path = entry.path();

            if ft.is_symlink() {
                let norm = abs_normalized(path)?;
                if seen_paths.insert(norm.clone()) {
                    deferred_symlinks.push(DeferredSymlink {
                        path: norm,
                        fs_path: path.to_path_buf(),
                    });
                }
            } else if ft.is_file() {
                let norm = abs_normalized(path)?;
                if seen_paths.insert(norm.clone()) {
                    let meta = std::fs::metadata(path)?;
                    let (size, mtime, runnable) = file_meta(&meta)?;
                    let mut entry = FileEntry::file(norm, size, mtime);
                    entry.runnable = runnable;
                    files.push(entry);
                }
            } else if ft.is_dir() {
                let norm = abs_normalized(path)?;
                if seen_paths.insert(norm.clone()) {
                    dirs.push(DirEntry::new(norm));
                }
            }
        }
    }

    // Process required filenames
    for filename in filenames {
        let filename = filename.as_ref();
        if !filename.exists() {
            // Check with symlink_metadata too - the file truly doesn't exist
            match std::fs::symlink_metadata(filename) {
                Ok(_) => {} // exists as a symlink
                Err(_) => {
                    return Err(SnapshotError::FileNotFound(filename.display().to_string()));
                }
            }
        }
        collect_single_file(
            filename,
            &mut files,
            &mut deferred_symlinks,
            &mut seen_paths,
        )?;
    }

    // Process optional filenames
    for filename in &options.optional_filenames {
        // Use symlink_metadata to detect even broken symlinks
        if std::fs::symlink_metadata(filename).is_err() {
            debug!(path = %filename.display(), "optional file not found, skipping");
            continue;
        }
        collect_single_file(
            filename,
            &mut files,
            &mut deferred_symlinks,
            &mut seen_paths,
        )?;
    }

    // Process deferred symlinks based on policy
    process_symlinks(
        deferred_symlinks,
        options.symlink_policy,
        &seen_paths,
        &mut files,
        &mut dirs,
    )?;

    let mut manifest = Manifest::new(HashAlgorithm::Xxh128, chunk_size)
        .with_files(files)
        .with_dirs(dirs);
    manifest.recompute_total_size();
    debug!(
        files = manifest.files.len(),
        dirs = manifest.dirs.len(),
        total_size = manifest.total_size,
        "collect complete"
    );
    Ok(manifest)
}

fn collect_single_file(
    path: &Path,
    files: &mut Vec<FileEntry>,
    deferred: &mut Vec<DeferredSymlink>,
    seen: &mut HashSet<String>,
) -> crate::Result<()> {
    let sym_meta = std::fs::symlink_metadata(path)?;
    if sym_meta.file_type().is_symlink() {
        let norm = abs_normalized(path)?;
        if seen.insert(norm.clone()) {
            deferred.push(DeferredSymlink {
                path: norm,
                fs_path: path.to_path_buf(),
            });
        }
    } else {
        let norm = abs_normalized(path)?;
        if seen.insert(norm.clone()) {
            let (size, mtime, runnable) = file_meta(&sym_meta)?;
            let mut entry = FileEntry::file(norm, size, mtime);
            entry.runnable = runnable;
            files.push(entry);
        }
    }
    Ok(())
}

fn process_symlinks(
    deferred: Vec<DeferredSymlink>,
    policy: SymlinkPolicy,
    collected_set: &HashSet<String>,
    files: &mut Vec<FileEntry>,
    dirs: &mut Vec<DirEntry>,
) -> crate::Result<()> {
    match policy {
        SymlinkPolicy::ExcludeAll => {
            debug!(count = deferred.len(), "excluding all symlinks per policy");
        }
        SymlinkPolicy::Preserve => {
            for sym in &deferred {
                let target = std::fs::read_link(&sym.fs_path)?;
                let abs_target = if target.is_absolute() {
                    normalize_path(&target.to_string_lossy())
                } else {
                    let parent = sym.fs_path.parent().unwrap_or(Path::new("/"));
                    normalize_path(&parent.join(&target).to_string_lossy())
                };
                files.push(FileEntry::symlink(&sym.path, abs_target));
            }
        }
        SymlinkPolicy::CollapseEscaping => {
            let mut visited = HashSet::new();
            for sym in &deferred {
                let target = std::fs::read_link(&sym.fs_path)?;
                let resolved = resolve_symlink_target(&sym.fs_path, &target)?;
                if is_escaping(&resolved, collected_set) {
                    // Collapse: replace symlink with target content
                    collapse_symlink(
                        &sym.path,
                        &sym.fs_path,
                        files,
                        dirs,
                        &mut visited,
                        None,
                        None,
                    )?;
                } else {
                    // Preserve as symlink with absolute target
                    files.push(FileEntry::symlink(&sym.path, &resolved));
                }
            }
        }
        SymlinkPolicy::CollapseAll => {
            let mut visited = HashSet::new();
            for sym in &deferred {
                collapse_symlink(
                    &sym.path,
                    &sym.fs_path,
                    files,
                    dirs,
                    &mut visited,
                    None,
                    None,
                )?;
            }
        }
        SymlinkPolicy::ExcludeEscaping => {
            for sym in &deferred {
                let target = std::fs::read_link(&sym.fs_path)?;
                let resolved = resolve_symlink_target(&sym.fs_path, &target)?;
                if !is_escaping(&resolved, collected_set) {
                    files.push(FileEntry::symlink(&sym.path, &resolved));
                }
            }
        }
        SymlinkPolicy::TransitiveIncludeTargets => {
            let mut queue: Vec<DeferredSymlink> = deferred;
            let mut all_seen: HashSet<String> = collected_set.clone();
            while let Some(sym) = queue.pop() {
                let target = std::fs::read_link(&sym.fs_path)?;
                let abs_target = resolve_symlink_target(&sym.fs_path, &target)?;
                files.push(FileEntry::symlink(&sym.path, &abs_target));

                // Add target to manifest if not already collected
                if all_seen.insert(abs_target.clone()) {
                    let target_path = PathBuf::from(&abs_target);
                    if let Ok(sym_meta) = std::fs::symlink_metadata(&target_path) {
                        if sym_meta.file_type().is_symlink() {
                            queue.push(DeferredSymlink {
                                path: abs_target,
                                fs_path: target_path,
                            });
                        } else if sym_meta.is_file() {
                            let (size, mtime, runnable) = file_meta(&sym_meta)?;
                            let mut entry = FileEntry::file(abs_target, size, mtime);
                            entry.runnable = runnable;
                            files.push(entry);
                        } else if sym_meta.is_dir() {
                            for entry in WalkDir::new(&target_path).follow_links(false) {
                                let entry = entry.map_err(|e| {
                                    SnapshotError::Io(std::io::Error::other(e.to_string()))
                                })?;
                                let ft = entry.file_type();
                                let ep = entry.path();
                                let norm = abs_normalized(ep)?;
                                if !all_seen.insert(norm.clone()) {
                                    continue;
                                }
                                if ft.is_symlink() {
                                    queue.push(DeferredSymlink {
                                        path: norm,
                                        fs_path: ep.to_path_buf(),
                                    });
                                } else if ft.is_file() {
                                    let meta = std::fs::metadata(ep)?;
                                    let (size, mtime, runnable) = file_meta(&meta)?;
                                    let mut entry = FileEntry::file(norm, size, mtime);
                                    entry.runnable = runnable;
                                    files.push(entry);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    Ok(())
}

fn resolve_symlink_target(symlink_path: &Path, target: &Path) -> crate::Result<String> {
    let abs = if target.is_absolute() {
        target.to_path_buf()
    } else {
        let parent = symlink_path.parent().unwrap_or(Path::new("/"));
        parent.join(target)
    };
    Ok(normalize_path(&abs.to_string_lossy()))
}

fn is_escaping(resolved_target: &str, collected_set: &HashSet<String>) -> bool {
    if collected_set.contains(resolved_target) {
        return false;
    }
    for p in collected_set {
        if resolved_target.starts_with(&format!("{p}/")) {
            return false;
        }
    }
    true
}

fn collapse_symlink(
    symlink_manifest_path: &str,
    symlink_fs_path: &Path,
    files: &mut Vec<FileEntry>,
    dirs: &mut Vec<DirEntry>,
    visited: &mut HashSet<String>,
    collapse_root: Option<&Path>,
    manifest_root: Option<&str>,
) -> crate::Result<()> {
    // Cycle detection
    let norm = normalize_path(&symlink_fs_path.to_string_lossy());
    if !visited.insert(norm) {
        warn!(path = %symlink_manifest_path, "symlink cycle detected, skipping");
        return Ok(()); // cycle detected, skip
    }

    // Follow the symlink to get target metadata
    let target_meta = match std::fs::metadata(symlink_fs_path) {
        Ok(m) => m,
        Err(e) => {
            warn!(path = %symlink_manifest_path, error = %e, "broken symlink, skipping");
            return Ok(());
        }
    };

    if target_meta.is_file() {
        let (size, mtime, runnable) = file_meta(&target_meta)?;
        let mut entry = FileEntry::file(symlink_manifest_path.to_string(), size, mtime);
        entry.runnable = runnable;
        files.push(entry);
    } else if target_meta.is_dir() {
        // Walk the target directory, mapping paths under the symlink's manifest path
        let real_target = std::fs::canonicalize(symlink_fs_path)?;
        let root = collapse_root.unwrap_or(&real_target);
        let mroot = manifest_root.unwrap_or(symlink_manifest_path);

        for entry in WalkDir::new(&real_target).follow_links(false) {
            let entry =
                entry.map_err(|e| SnapshotError::Io(std::io::Error::other(e.to_string())))?;
            let ft = entry.file_type();
            let ep = entry.path();

            // Compute the relative path from the real target
            let rel = ep.strip_prefix(&real_target).unwrap_or(ep);
            if rel.as_os_str().is_empty() {
                continue; // skip the root dir itself
            }
            let mapped = format!(
                "{}/{}",
                symlink_manifest_path,
                normalize_path(&rel.to_string_lossy())
            );

            if ft.is_symlink() {
                // Resolve the nested symlink's target
                let link_target = match std::fs::read_link(ep) {
                    Ok(t) => t,
                    Err(_) => continue,
                };
                let abs_target = if link_target.is_absolute() {
                    link_target
                } else {
                    ep.parent().unwrap_or(Path::new("/")).join(&link_target)
                };
                let resolved = match abs_target.canonicalize() {
                    Ok(p) => p,
                    Err(_) => continue, // broken symlink
                };

                if let Ok(rel_to_root) = resolved.strip_prefix(root) {
                    // Internal symlink: preserve with translated target
                    let translated = format!(
                        "{}/{}",
                        mroot,
                        normalize_path(&rel_to_root.to_string_lossy())
                    );
                    files.push(FileEntry::symlink(&mapped, &translated));
                } else if resolved.is_dir() {
                    // External directory symlink: recursively collapse
                    collapse_symlink(&mapped, ep, files, dirs, visited, Some(root), Some(mroot))?;
                } else if resolved.is_file() {
                    // External file symlink: collapse to file
                    if let Ok(meta) = std::fs::metadata(ep) {
                        let (size, mtime, runnable) = file_meta(&meta)?;
                        let mut fe = FileEntry::file(mapped, size, mtime);
                        fe.runnable = runnable;
                        files.push(fe);
                    }
                }
            } else if ft.is_file() {
                let meta = std::fs::metadata(ep)?;
                let (size, mtime, runnable) = file_meta(&meta)?;
                let mut entry = FileEntry::file(mapped, size, mtime);
                entry.runnable = runnable;
                files.push(entry);
            } else if ft.is_dir() {
                dirs.push(DirEntry::new(mapped));
            }
        }
    }
    Ok(())
}

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

    fn setup_dir_with_files(dir: &Path) {
        std::fs::create_dir_all(dir.join("sub")).unwrap();
        std::fs::write(dir.join("a.txt"), "hello").unwrap();
        std::fs::write(dir.join("sub/b.txt"), "world").unwrap();
    }

    #[test]
    fn collect_directory_with_files() {
        let tmp = TempDir::new().unwrap();
        setup_dir_with_files(tmp.path());

        let result = collect_abs_snapshot(
            &[tmp.path().to_path_buf()],
            &[] as &[PathBuf],
            CollectOptions::default(),
        )
        .unwrap();

        assert_eq!(result.files.len(), 2);
        assert!(result.files.iter().any(|f| f.path.ends_with("a.txt")));
        assert!(result.files.iter().any(|f| f.path.ends_with("sub/b.txt")));
    }

    #[test]
    fn file_sizes_and_mtimes_captured() {
        let tmp = TempDir::new().unwrap();
        std::fs::write(tmp.path().join("f.txt"), "12345").unwrap();

        let result = collect_abs_snapshot(
            &[tmp.path().to_path_buf()],
            &[] as &[PathBuf],
            CollectOptions::default(),
        )
        .unwrap();

        let f = &result.files[0];
        assert_eq!(f.size, Some(5));
        assert!(f.mtime.unwrap() > 0);
    }

    #[test]
    fn empty_directories_included() {
        let tmp = TempDir::new().unwrap();
        std::fs::create_dir_all(tmp.path().join("empty_dir")).unwrap();
        std::fs::write(tmp.path().join("f.txt"), "x").unwrap();

        let result = collect_abs_snapshot(
            &[tmp.path().to_path_buf()],
            &[] as &[PathBuf],
            CollectOptions::default(),
        )
        .unwrap();

        assert!(result.dirs.iter().any(|d| d.path.ends_with("empty_dir")));
    }

    #[test]
    fn all_directories_recorded() {
        let tmp = TempDir::new().unwrap();
        std::fs::create_dir_all(tmp.path().join("a/b")).unwrap();
        std::fs::write(tmp.path().join("a/b/file.txt"), "x").unwrap();

        let result = collect_abs_snapshot(
            &[tmp.path().to_path_buf()],
            &[] as &[PathBuf],
            CollectOptions::default(),
        )
        .unwrap();

        // Should have the root dir, a/, and a/b/ (3 dirs total)
        // Plus the file
        assert_eq!(result.files.len(), 1);
        assert!(
            result.dirs.len() >= 3,
            "expected at least 3 dirs, got {}: {:?}",
            result.dirs.len(),
            result.dirs.iter().map(|d| &d.path).collect::<Vec<_>>()
        );
        assert!(result.dirs.iter().any(|d| d.path.ends_with("/a")));
        assert!(result.dirs.iter().any(|d| d.path.ends_with("/a/b")));
    }

    #[test]
    fn required_filename_missing_errors() {
        let result = collect_abs_snapshot(
            &[] as &[PathBuf],
            &[PathBuf::from("/nonexistent/file.txt")],
            CollectOptions::default(),
        );
        assert!(result.is_err());
        match result.unwrap_err() {
            SnapshotError::FileNotFound(p) => assert!(p.contains("nonexistent")),
            e => panic!("expected FileNotFound, got: {e}"),
        }
    }

    #[test]
    fn optional_filename_missing_skipped() {
        let tmp = TempDir::new().unwrap();
        std::fs::write(tmp.path().join("exists.txt"), "ok").unwrap();

        let result = collect_abs_snapshot(
            &[] as &[PathBuf],
            &[tmp.path().join("exists.txt")],
            CollectOptions {
                optional_filenames: vec![tmp.path().join("nope.txt")],
                ..Default::default()
            },
        )
        .unwrap();

        assert_eq!(result.files.len(), 1);
        assert!(result.files[0].path.ends_with("exists.txt"));
    }

    #[test]
    fn all_paths_absolute_and_normalized() {
        let tmp = TempDir::new().unwrap();
        std::fs::write(tmp.path().join("f.txt"), "x").unwrap();

        let result = collect_abs_snapshot(
            &[tmp.path().to_path_buf()],
            &[] as &[PathBuf],
            CollectOptions::default(),
        )
        .unwrap();

        for f in &result.files {
            assert!(
                crate::path_util::is_absolute_path(&f.path),
                "not absolute: {}",
                f.path
            );
            assert!(!f.path.contains("/../"), "not normalized: {}", f.path);
        }
    }

    #[test]
    fn nested_directories_walked_recursively() {
        let tmp = TempDir::new().unwrap();
        std::fs::create_dir_all(tmp.path().join("a/b/c")).unwrap();
        std::fs::write(tmp.path().join("a/b/c/deep.txt"), "deep").unwrap();

        let result = collect_abs_snapshot(
            &[tmp.path().to_path_buf()],
            &[] as &[PathBuf],
            CollectOptions::default(),
        )
        .unwrap();

        assert_eq!(result.files.len(), 1);
        assert!(result.files[0].path.ends_with("a/b/c/deep.txt"));
    }

    #[cfg(unix)]
    #[test]
    fn symlinks_preserve_policy() {
        let tmp = TempDir::new().unwrap();
        std::fs::write(tmp.path().join("target.txt"), "data").unwrap();
        std::os::unix::fs::symlink(tmp.path().join("target.txt"), tmp.path().join("link.txt"))
            .unwrap();

        let result = collect_abs_snapshot(
            &[tmp.path().to_path_buf()],
            &[] as &[PathBuf],
            CollectOptions {
                symlink_policy: SymlinkPolicy::Preserve,
                ..Default::default()
            },
        )
        .unwrap();

        let symlink_entry = result
            .files
            .iter()
            .find(|f| f.path.ends_with("link.txt"))
            .unwrap();
        assert!(symlink_entry.symlink_target.is_some());
    }

    #[cfg(unix)]
    #[test]
    fn symlinks_exclude_all_policy() {
        let tmp = TempDir::new().unwrap();
        std::fs::write(tmp.path().join("target.txt"), "data").unwrap();
        std::os::unix::fs::symlink(tmp.path().join("target.txt"), tmp.path().join("link.txt"))
            .unwrap();

        let result = collect_abs_snapshot(
            &[tmp.path().to_path_buf()],
            &[] as &[PathBuf],
            CollectOptions {
                symlink_policy: SymlinkPolicy::ExcludeAll,
                ..Default::default()
            },
        )
        .unwrap();

        // Only the real file, no symlink
        assert_eq!(result.files.len(), 1);
        assert!(result.files[0].path.ends_with("target.txt"));
        assert!(result.files[0].symlink_target.is_none());
    }

    #[test]
    fn hashes_are_none() {
        let tmp = TempDir::new().unwrap();
        std::fs::write(tmp.path().join("f.txt"), "x").unwrap();

        let result = collect_abs_snapshot(
            &[tmp.path().to_path_buf()],
            &[] as &[PathBuf],
            CollectOptions::default(),
        )
        .unwrap();

        for f in &result.files {
            assert!(f.hash.is_none());
        }
    }

    #[cfg(unix)]
    #[test]
    fn collapse_escaping_dir_preserves_internal_symlinks() {
        let tmp = TempDir::new().unwrap();

        // Create target directory outside the root
        let outside = tmp.path().join("outside");
        std::fs::create_dir_all(outside.join("sub")).unwrap();
        std::fs::write(outside.join("file.txt"), "content").unwrap();
        // Internal symlink: points within the same target directory
        std::os::unix::fs::symlink(outside.join("file.txt"), outside.join("sub/internal_link"))
            .unwrap();

        // Create root with escaping dir symlink
        let root = tmp.path().join("root");
        std::fs::create_dir_all(&root).unwrap();
        std::os::unix::fs::symlink(&outside, root.join("link_dir")).unwrap();

        let m = collect_abs_snapshot(
            std::slice::from_ref(&root),
            &[] as &[PathBuf],
            CollectOptions {
                symlink_policy: SymlinkPolicy::CollapseEscaping,
                ..Default::default()
            },
        )
        .unwrap();

        // The internal symlink should be preserved with a translated target
        let internal = m
            .files
            .iter()
            .find(|f| f.path.ends_with("sub/internal_link"))
            .unwrap();
        assert!(
            internal.symlink_target.is_some(),
            "internal symlink should be preserved"
        );
        let target = internal.symlink_target.as_ref().unwrap();
        assert!(
            target.ends_with("link_dir/file.txt"),
            "target should be translated, got: {}",
            target
        );

        // The regular file should be collapsed normally
        let file = m
            .files
            .iter()
            .find(|f| f.path.ends_with("link_dir/file.txt"))
            .unwrap();
        assert!(file.symlink_target.is_none());
        assert_eq!(file.size, Some(7));
    }

    #[cfg(unix)]
    #[test]
    fn collapse_escaping_dir_collapses_external_symlinks() {
        let tmp = TempDir::new().unwrap();

        // Create an external file that a nested symlink will point to
        let far_away = tmp.path().join("far_away.txt");
        std::fs::write(&far_away, "far").unwrap();

        // Create target directory outside the root
        let outside = tmp.path().join("outside");
        std::fs::create_dir_all(&outside).unwrap();
        std::fs::write(outside.join("file.txt"), "content").unwrap();
        // External symlink: points outside the target directory
        std::os::unix::fs::symlink(&far_away, outside.join("external_link")).unwrap();

        // Create root with escaping dir symlink
        let root = tmp.path().join("root");
        std::fs::create_dir_all(&root).unwrap();
        std::os::unix::fs::symlink(&outside, root.join("link_dir")).unwrap();

        let m = collect_abs_snapshot(
            std::slice::from_ref(&root),
            &[] as &[PathBuf],
            CollectOptions {
                symlink_policy: SymlinkPolicy::CollapseEscaping,
                ..Default::default()
            },
        )
        .unwrap();

        // The external symlink should be collapsed to a regular file
        let external = m
            .files
            .iter()
            .find(|f| f.path.ends_with("external_link"))
            .unwrap();
        assert!(
            external.symlink_target.is_none(),
            "external symlink should be collapsed"
        );
        assert_eq!(external.size, Some(3)); // "far"
    }
}