f00-core 0.10.0

Core listing, sorting, and filtering for f00
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
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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
use std::fs;
use std::path::{Path, PathBuf};
use std::time::Instant;

use rayon::prelude::*;
// jwalk for parallel recursive walks.

use crate::entry::Entry;
use crate::error::{Error, Result};
use crate::filter::filter_entries;
use crate::ignore::{apply_ignore_set, load_ignore_set, IgnoreSet};
use crate::options::{CliSymlinkMode, ListOptions};
use crate::sort::sort_entries;

/// Phase timings for a single listing (milliseconds).
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ListTiming {
    /// Time spent in `readdir` / collecting directory entries.
    pub readdir_ms: u128,
    /// Time spent stating paths and building [`Entry`] values.
    pub stat_ms: u128,
    /// Time spent filtering and sorting.
    pub sort_ms: u128,
}

impl ListTiming {
    /// Sum another timing into this one (for multi-path runs).
    pub fn add_assign(&mut self, other: &ListTiming) {
        self.readdir_ms = self.readdir_ms.saturating_add(other.readdir_ms);
        self.stat_ms = self.stat_ms.saturating_add(other.stat_ms);
        self.sort_ms = self.sort_ms.saturating_add(other.sort_ms);
    }
}

/// A listing produced for one path argument (or recursive tree).
#[derive(Debug, Clone)]
pub struct Listing {
    /// The path that was requested.
    pub root: PathBuf,
    /// Whether `root` itself is a directory.
    pub root_is_dir: bool,
    /// Entries to display. For recursive mode, may include dir headers.
    pub entries: Vec<Entry>,
    /// Recoverable problems while listing (e.g. unreadable subdirectory).
    /// Surfaces as process exit code 1 when non-zero.
    pub minor_errors: usize,
    /// Optional phase timings when [`ListOptions::collect_timing`] is set.
    pub timing: Option<ListTiming>,
}

impl Listing {
    fn new(root: PathBuf, root_is_dir: bool, entries: Vec<Entry>) -> Self {
        Self {
            root,
            root_is_dir,
            entries,
            minor_errors: 0,
            timing: None,
        }
    }

    fn with_timing(mut self, timing: Option<ListTiming>) -> Self {
        self.timing = timing;
        self
    }
}

/// Decide whether to follow a CLI path argument that is a symlink.
fn follow_cli_path(path: &Path, opts: &ListOptions) -> bool {
    if opts.follow_links {
        return true;
    }
    match opts.cli_symlink {
        CliSymlinkMode::Never => false,
        CliSymlinkMode::Always => {
            // `-H`: follow command-line symlinks.
            fs::symlink_metadata(path)
                .map(|m| m.file_type().is_symlink())
                .unwrap_or(false)
        }
        CliSymlinkMode::DirOnly => {
            // Follow only when the symlink resolves to a directory.
            let is_link = fs::symlink_metadata(path)
                .map(|m| m.file_type().is_symlink())
                .unwrap_or(false);
            if !is_link {
                return false;
            }
            fs::metadata(path).map(|m| m.is_dir()).unwrap_or(false)
        }
    }
}

/// List a single path: if file, return that entry alone; if dir, list children.
///
/// With `-d` / `directory`, list the path itself even when it is a directory.
pub fn list_path(path: &Path, opts: &ListOptions) -> Result<Listing> {
    let path = if path.as_os_str().is_empty() {
        Path::new(".")
    } else {
        path
    };

    let follow = follow_cli_path(path, opts);

    let meta = if follow {
        fs::metadata(path)
    } else {
        fs::symlink_metadata(path)
    }
    .map_err(|source| {
        if source.kind() == std::io::ErrorKind::NotFound {
            Error::NotFound(path.to_path_buf())
        } else {
            Error::Metadata {
                path: path.to_path_buf(),
                source,
            }
        }
    })?;

    // `-d`: list the directory entry itself, not its contents.
    if opts.directory || !meta.is_dir() {
        let fill = meta_fill_from(opts);
        let entry = if follow {
            Entry::from_path_follow_with(path, 0, fill)?
        } else {
            Entry::from_path_and_meta_with(path, &meta, 0, fill)?
        };
        return Ok(Listing::new(path.to_path_buf(), meta.is_dir(), vec![entry]));
    }

    if opts.recursive {
        list_recursive(path, opts)
    } else {
        list_directory(path, opts)
    }
}

fn meta_fill_from(opts: &ListOptions) -> crate::entry::MetaFill {
    crate::entry::MetaFill {
        resolve_names: opts.resolve_owner_group,
        read_context: opts.read_selinux,
    }
}

/// Build an [`Entry`] from a readdir item.
fn entry_from_dir_entry(
    item: &fs::DirEntry,
    follow_links: bool,
    fill: crate::entry::MetaFill,
    prefer_statx: bool,
) -> Option<Entry> {
    let entry_path = item.path();
    if follow_links {
        return Entry::from_path_follow_with(&entry_path, 0, fill).ok();
    }
    #[cfg(target_os = "linux")]
    {
        if prefer_statx {
            if let Ok(e) = crate::linux_statx::entry_from_statx(&entry_path, 0, fill) {
                return Some(e);
            }
        }
    }
    #[cfg(not(target_os = "linux"))]
    {
        let _ = prefer_statx;
    }
    let meta = item
        .metadata()
        .or_else(|_| fs::symlink_metadata(&entry_path));
    match meta {
        Ok(m) => Entry::from_path_and_meta_with(&entry_path, &m, 0, fill).ok(),
        Err(_) => None,
    }
}

/// Stat directory children, optionally in parallel with rayon (or io_uring).
fn stat_dir_entries(dir_entries: &[fs::DirEntry], opts: &ListOptions) -> Vec<Entry> {
    let follow = opts.follow_links;
    let fill = meta_fill_from(opts);
    let prefer_statx = opts.linux_statx;
    let map_one = |item: &fs::DirEntry| entry_from_dir_entry(item, follow, fill, prefer_statx);

    // Linux + feature: batch statx via io_uring for large dirs (no follow).
    #[cfg(all(target_os = "linux", feature = "io-uring"))]
    {
        if opts.io_uring
            && !follow
            && dir_entries.len() >= crate::io_uring_stat::IO_URING_THRESHOLD
            && !fill.resolve_names
            && !fill.read_context
        {
            let paths: Vec<_> = dir_entries.iter().map(|d| d.path()).collect();
            if let Some(entries) = crate::io_uring_stat::entries_from_paths_uring(&paths, fill) {
                // Preserve "same count roughly" — if many failures, fall back.
                if entries.len() * 10 >= paths.len() * 8 {
                    return entries;
                }
            }
        }
    }

    if !opts.use_parallel_stat(dir_entries.len()) {
        return dir_entries.iter().filter_map(map_one).collect();
    }

    let collect = || dir_entries.par_iter().filter_map(map_one).collect();

    if opts.threads > 1 {
        match rayon::ThreadPoolBuilder::new()
            .num_threads(opts.threads)
            .build()
        {
            Ok(pool) => pool.install(collect),
            Err(_) => collect(),
        }
    } else {
        collect()
    }
}

/// Non-recursive directory listing.
pub fn list_directory(path: &Path, opts: &ListOptions) -> Result<Listing> {
    let collect_timing = opts.collect_timing;
    let mut timing = ListTiming::default();

    let mut entries = Vec::new();

    let fill = meta_fill_from(opts);
    // Synthesize `.` and `..` sequentially (must not race with parallel stat).
    if opts.all {
        if let Ok(dot) = Entry::from_path_with(path, 0, fill) {
            let mut e = dot;
            e.name = ".".to_string();
            entries.push(e);
        }
        if let Some(parent) = path.parent().filter(|p| !p.as_os_str().is_empty()) {
            if let Ok(mut e) = Entry::from_path_with(parent, 0, fill) {
                e.name = "..".to_string();
                e.path = parent.to_path_buf();
                entries.push(e);
            }
        } else if let Ok(mut e) = Entry::from_path_with(path, 0, fill) {
            // path is like `.` or `/` — still emit `..` best-effort
            e.name = "..".to_string();
            entries.push(e);
        }
    }

    let t_readdir = if collect_timing {
        Some(Instant::now())
    } else {
        None
    };

    let read = fs::read_dir(path).map_err(|source| Error::ReadDir {
        path: path.to_path_buf(),
        source,
    })?;

    let mut dir_entries = Vec::new();
    for item in read {
        let item = item.map_err(|source| Error::ReadDir {
            path: path.to_path_buf(),
            source,
        })?;
        dir_entries.push(item);
    }

    if let Some(t0) = t_readdir {
        timing.readdir_ms = t0.elapsed().as_millis();
    }

    let t_stat = if collect_timing {
        Some(Instant::now())
    } else {
        None
    };

    let children = stat_dir_entries(&dir_entries, opts);
    entries.extend(children);

    if let Some(t0) = t_stat {
        timing.stat_ms = t0.elapsed().as_millis();
    }

    let t_sort = if collect_timing {
        Some(Instant::now())
    } else {
        None
    };

    filter_entries(&mut entries, opts);
    if opts.use_ignore_files {
        let set = load_ignore_set(path);
        apply_ignore_set(&mut entries, &set);
    }
    // Deterministic order: sort always runs after parallel collect.
    sort_entries(&mut entries, opts);

    if let Some(t0) = t_sort {
        timing.sort_ms = t0.elapsed().as_millis();
    }

    Ok(
        Listing::new(path.to_path_buf(), true, entries).with_timing(if collect_timing {
            Some(timing)
        } else {
            None
        }),
    )
}

/// Lightweight walk record (owned paths so we can parallelize metadata).
struct Walked {
    path: PathBuf,
    depth: usize,
    is_dir: bool,
}

/// Basic recursive listing using parallel walk (jwalk) + parallel / io_uring metadata.
pub fn list_recursive(path: &Path, opts: &ListOptions) -> Result<Listing> {
    let collect_timing = opts.collect_timing;
    let mut timing = ListTiming::default();

    let mut minor_errors = 0usize;
    let max_depth = opts.max_depth.unwrap_or(usize::MAX);

    let root_ignore = if opts.use_ignore_files {
        Some(load_ignore_set(path))
    } else {
        None
    };

    let t_walk = if collect_timing {
        Some(Instant::now())
    } else {
        None
    };

    // Phase 1: parallel directory walk (jwalk). sort(true) keeps name order per dir.
    // We avoid metadata() during the walk so stat can be batched / parallel next.
    let mut walked: Vec<Walked> = Vec::new();
    // skip_hidden(false): f00 applies its own -a/-A/hide rules after the walk.
    let mut jwalker = jwalk::WalkDir::new(path)
        .follow_links(opts.follow_links)
        .skip_hidden(false)
        .sort(true);
    if max_depth != usize::MAX {
        jwalker = jwalker.max_depth(max_depth);
    }

    for item in jwalker {
        let item = match item {
            Ok(i) => i,
            Err(_) => {
                minor_errors += 1;
                continue;
            }
        };
        let depth = item.depth();
        if depth == 0 {
            continue; // root is not a listed entry
        }
        let is_dir = item.file_type().is_dir();
        walked.push(Walked {
            path: item.path(),
            depth,
            is_dir,
        });
    }

    // jwalk may not surface every unreadable-directory error on the iterator;
    // probe dirs so `-R` still yields exit code 1 (GNU ls-compatible minor error).
    for w in &walked {
        if w.is_dir {
            if let Err(err) = fs::read_dir(&w.path) {
                let kind = err.kind();
                if matches!(
                    kind,
                    std::io::ErrorKind::PermissionDenied
                        | std::io::ErrorKind::NotFound
                        | std::io::ErrorKind::Other
                ) {
                    minor_errors += 1;
                }
            }
        }
    }

    if let Some(t0) = t_walk {
        timing.readdir_ms = t0.elapsed().as_millis();
    }

    let fill = meta_fill_from(opts);
    let t_stat = if collect_timing {
        Some(Instant::now())
    } else {
        None
    };

    // Phase 2: metadata → Entry.
    // Prefer io_uring batch statx for large cheap listings (Linux + feature).
    let built: Vec<Option<Entry>> = {
        #[cfg(all(target_os = "linux", feature = "io-uring"))]
        {
            if opts.io_uring
                && !opts.follow_links
                && walked.len() >= crate::io_uring_stat::IO_URING_THRESHOLD
                && !fill.resolve_names
                && !fill.read_context
            {
                let paths: Vec<_> = walked.iter().map(|w| w.path.clone()).collect();
                if let Some(mut ents) = crate::io_uring_stat::entries_from_paths_uring(&paths, fill)
                {
                    if ents.len() * 10 >= paths.len() * 8 {
                        // Restore walk depths by path (uring builds depth=0).
                        use std::collections::HashMap;
                        let depth_by: HashMap<&Path, usize> =
                            walked.iter().map(|w| (w.path.as_path(), w.depth)).collect();
                        for e in &mut ents {
                            if let Some(&d) = depth_by.get(e.path.as_path()) {
                                e.depth = d;
                            }
                        }
                        // Re-order to walk order and filter.
                        let mut by_path: HashMap<PathBuf, Entry> =
                            ents.into_iter().map(|e| (e.path.clone(), e)).collect();
                        let ordered: Vec<Option<Entry>> = walked
                            .iter()
                            .map(|w| {
                                by_path
                                    .remove(&w.path)
                                    .filter(|e| crate::filter::should_show(e, opts))
                            })
                            .collect();
                        if let Some(t0) = t_stat {
                            timing.stat_ms = t0.elapsed().as_millis();
                        }
                        // Jump to phase 3 with ordered.
                        return finish_recursive(
                            path,
                            opts,
                            walked,
                            ordered,
                            root_ignore,
                            minor_errors,
                            timing,
                            collect_timing,
                        );
                    }
                }
            }
        }

        let use_parallel = opts.use_parallel_stat(walked.len().max(1));
        let map = |w: &Walked| {
            Entry::from_path_with(&w.path, w.depth, fill)
                .ok()
                .filter(|e| crate::filter::should_show(e, opts))
        };
        if use_parallel {
            if opts.threads > 1 {
                match rayon::ThreadPoolBuilder::new()
                    .num_threads(opts.threads)
                    .build()
                {
                    Ok(pool) => pool.install(|| walked.par_iter().map(map).collect()),
                    Err(_) => walked.par_iter().map(map).collect(),
                }
            } else {
                walked.par_iter().map(map).collect()
            }
        } else {
            walked.iter().map(map).collect()
        }
    };

    if let Some(t0) = t_stat {
        timing.stat_ms = t0.elapsed().as_millis();
    }

    finish_recursive(
        path,
        opts,
        walked,
        built,
        root_ignore,
        minor_errors,
        timing,
        collect_timing,
    )
}

#[allow(clippy::too_many_arguments)]
fn finish_recursive(
    path: &Path,
    opts: &ListOptions,
    walked: Vec<Walked>,
    built: Vec<Option<Entry>>,
    root_ignore: Option<IgnoreSet>,
    minor_errors: usize,
    mut timing: ListTiming,
    collect_timing: bool,
) -> Result<Listing> {
    // Phase 3: sequential ignore filter + optional dir headers (stable walk order).
    let mut ignore_by_dir: Vec<(PathBuf, IgnoreSet)> = Vec::new();
    let mut entries: Vec<Entry> = Vec::with_capacity(built.len().saturating_add(16));

    if opts.emit_dir_headers {
        entries.push(Entry::dir_header(path, 0));
    }

    for (w, maybe) in walked.iter().zip(built) {
        let Some(e) = maybe else {
            continue;
        };
        if ignored_by_sets(&e, opts, root_ignore.as_ref(), &mut ignore_by_dir) {
            continue;
        }
        if opts.emit_dir_headers && w.is_dir {
            // Directory node as a listable entry, then a section header for its children.
            entries.push(e);
            entries.push(Entry::dir_header(&w.path, w.depth));
        } else {
            entries.push(e);
        }
    }

    let t_sort = if collect_timing {
        Some(Instant::now())
    } else {
        None
    };

    if opts.emit_dir_headers {
        sort_recursive_sections(&mut entries, opts);
    } else {
        // Tree path: walk order already name-sorted within each directory (WalkDir).
        // Re-sort siblings only if a non-name primary sort was requested.
        if !matches!(opts.sort_by, crate::options::SortBy::Name) || opts.reverse {
            sort_tree_preorder(&mut entries, opts);
        }
    }

    if let Some(t0) = t_sort {
        timing.sort_ms = t0.elapsed().as_millis();
    }

    let mut listing = Listing::new(path.to_path_buf(), true, entries)
        .with_timing(if collect_timing { Some(timing) } else { None });
    listing.minor_errors = minor_errors;
    Ok(listing)
}

/// Re-sort a preorder tree listing by re-grouping siblings after a non-name sort.
///
/// Keeps a valid preorder: each directory’s children stay contiguous under it.
fn sort_tree_preorder(entries: &mut [Entry], opts: &ListOptions) {
    if entries.is_empty() {
        return;
    }
    // Build groups by parent path; stable relative to walk.
    // Simple approach: sort by (parent, sort_key) while preserving depth structure
    // via full path component sort with custom comparator for the leaf name only.
    // For size/time sorts, sort siblings that share the same parent.
    let mut i = 0;
    while i < entries.len() {
        let depth = entries[i].depth;
        // Find run of direct children of the same parent starting at i… actually
        // at depth `depth`, a sibling run is contiguous until depth < depth.
        let parent = entries[i].path.parent().map(Path::to_path_buf);
        let mut j = i + 1;
        while j < entries.len() {
            if entries[j].depth < depth {
                break;
            }
            if entries[j].depth == depth {
                let p = entries[j].path.parent().map(Path::to_path_buf);
                if p != parent {
                    break;
                }
            }
            j += 1;
        }
        // Within [i, j), extract sibling indices at exactly `depth`.
        let sib: Vec<usize> = (i..j).filter(|&k| entries[k].depth == depth).collect();
        if sib.len() > 1 {
            // Sort sibling subtrees by moving whole subtree blocks.
            // Build blocks: each sibling at depth owns [start, next_sibling).
            let mut blocks: Vec<Vec<Entry>> = Vec::with_capacity(sib.len());
            for (bi, &start) in sib.iter().enumerate() {
                let end = sib.get(bi + 1).copied().unwrap_or(j);
                blocks.push(entries[start..end].to_vec());
            }
            // cmp_entry already applies reverse when set.
            blocks.sort_by(|a, b| crate::sort::cmp_entry(&a[0], &b[0], opts));
            let mut out = Vec::with_capacity(j - i);
            for b in blocks {
                out.extend(b);
            }
            entries[i..j].clone_from_slice(&out);
        }
        i = if j > i { j } else { i + 1 };
    }
}

/// Check root ignore set and the ignore file in the entry's parent directory.
fn ignored_by_sets(
    entry: &Entry,
    opts: &ListOptions,
    root_ignore: Option<&IgnoreSet>,
    cache: &mut Vec<(PathBuf, IgnoreSet)>,
) -> bool {
    if !opts.use_ignore_files {
        return false;
    }
    if let Some(root) = root_ignore {
        if root.ignores(entry) {
            return true;
        }
    }
    let Some(parent) = entry.path.parent() else {
        return false;
    };
    if let Some(root) = root_ignore {
        if parent == root.base {
            return false;
        }
    }
    if let Some((_, set)) = cache.iter().find(|(p, _)| p == parent) {
        return set.ignores(entry);
    }
    let set = load_ignore_set(parent);
    let ignored = set.ignores(entry);
    cache.push((parent.to_path_buf(), set));
    ignored
}

fn sort_recursive_sections(entries: &mut [Entry], opts: &ListOptions) {
    let mut start = 0;
    while start < entries.len() {
        // Find next header after start
        if entries[start].is_dir_header {
            let section_start = start + 1;
            let mut end = section_start;
            while end < entries.len() && !entries[end].is_dir_header {
                end += 1;
            }
            sort_entries(&mut entries[section_start..end], opts);
            start = end;
        } else {
            // Leading non-header run
            let mut end = start;
            while end < entries.len() && !entries[end].is_dir_header {
                end += 1;
            }
            sort_entries(&mut entries[start..end], opts);
            start = end;
        }
    }
}

/// Outcome of listing one or more path arguments.
#[derive(Debug)]
pub struct ListOutcome {
    pub listings: Vec<Listing>,
    /// Errors for command-line path arguments that could not be listed.
    pub path_errors: Vec<Error>,
    /// Sum of recoverable errors inside successful listings (e.g. unreadable subdirs).
    pub minor_errors: usize,
}

impl ListOutcome {
    /// GNU-aligned exit status: 0 ok, 1 minor problems, 2 serious (bad path args).
    pub fn exit_code(&self) -> i32 {
        if !self.path_errors.is_empty() {
            2
        } else if self.minor_errors > 0 || self.listings.iter().any(|l| l.minor_errors > 0) {
            1
        } else {
            0
        }
    }

    /// Aggregate phase timings across successful listings.
    pub fn total_timing(&self) -> ListTiming {
        let mut total = ListTiming::default();
        for l in &self.listings {
            if let Some(ref t) = l.timing {
                total.add_assign(t);
            }
        }
        total
    }
}

/// List multiple paths, returning one Listing per successful path.
///
/// Failures on individual path arguments are collected (serious) rather than
/// aborting the whole run, matching GNU `ls` multi-argument behavior.
pub fn list_paths(paths: &[PathBuf], opts: &ListOptions) -> Result<Vec<Listing>> {
    let outcome = list_paths_with_errors(paths, opts);
    if let Some(err) = outcome.path_errors.into_iter().next() {
        // Preserve previous fail-fast API for callers that expect `Result`.
        return Err(err);
    }
    Ok(outcome.listings)
}

/// List paths, collecting per-argument errors instead of failing fast.
pub fn list_paths_with_errors(paths: &[PathBuf], opts: &ListOptions) -> ListOutcome {
    let owned: Vec<PathBuf>;
    let paths: &[PathBuf] = if paths.is_empty() {
        owned = vec![PathBuf::from(".")];
        &owned
    } else {
        paths
    };

    let mut listings = Vec::with_capacity(paths.len());
    let mut path_errors = Vec::new();
    let mut minor_errors = 0usize;

    for p in paths {
        match list_path(p, opts) {
            Ok(l) => {
                minor_errors += l.minor_errors;
                listings.push(l);
            }
            Err(e) => path_errors.push(e),
        }
    }

    ListOutcome {
        listings,
        path_errors,
        minor_errors,
    }
}

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

    fn temp_dir() -> PathBuf {
        use std::sync::atomic::{AtomicU64, Ordering};
        static N: AtomicU64 = AtomicU64::new(0);
        let base = std::env::temp_dir().join(format!(
            "f00-core-list-{}-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_nanos())
                .unwrap_or(0),
            N.fetch_add(1, Ordering::Relaxed)
        ));
        fs::create_dir_all(&base).unwrap();
        base
    }

    #[test]
    fn list_paths_with_errors_partial_success() {
        let dir = temp_dir();
        fs::write(dir.join("a.txt"), b"x").unwrap();
        let missing = dir.join("gone");
        let opts = ListOptions::default();
        let outcome = list_paths_with_errors(&[dir.clone(), missing], &opts);
        assert_eq!(outcome.listings.len(), 1);
        assert_eq!(outcome.path_errors.len(), 1);
        assert_eq!(outcome.exit_code(), 2);
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn list_paths_ok_exit_0() {
        let dir = temp_dir();
        fs::write(dir.join("a.txt"), b"x").unwrap();
        let opts = ListOptions::default();
        let outcome = list_paths_with_errors(std::slice::from_ref(&dir), &opts);
        assert!(outcome.path_errors.is_empty());
        assert_eq!(outcome.exit_code(), 0);
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn list_directory_honors_ignore_files() {
        let dir = temp_dir();
        fs::write(dir.join("keep.txt"), b"k").unwrap();
        fs::write(dir.join("skip.o"), b"o").unwrap();
        fs::write(dir.join(".gitignore"), "*.o\n").unwrap();

        let opts = ListOptions {
            use_ignore_files: true,
            ..Default::default()
        };
        let listing = list_directory(&dir, &opts).unwrap();
        let names: Vec<_> = listing.entries.iter().map(|e| e.name.as_str()).collect();
        assert!(names.contains(&"keep.txt"));
        assert!(!names.contains(&"skip.o"));

        let opts_off = ListOptions {
            use_ignore_files: false,
            ..Default::default()
        };
        let listing = list_directory(&dir, &opts_off).unwrap();
        let names: Vec<_> = listing.entries.iter().map(|e| e.name.as_str()).collect();
        assert!(names.contains(&"skip.o"));

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn parallel_list_same_names_as_sequential() {
        let dir = temp_dir();
        // Well above PARALLEL_STAT_THRESHOLD so parallel path is taken.
        let mut expected = Vec::new();
        for i in 0..64 {
            let name = format!("file_{i:03}.txt");
            fs::write(dir.join(&name), b"x").unwrap();
            expected.push(name);
        }
        fs::create_dir(dir.join("subdir")).unwrap();
        expected.push("subdir".into());
        expected.sort();

        let sequential = ListOptions {
            parallel: false,
            threads: 1,
            ..Default::default()
        };
        let seq = list_directory(&dir, &sequential).unwrap();
        let mut seq_names: Vec<_> = seq.entries.iter().map(|e| e.name.clone()).collect();
        seq_names.sort();
        assert_eq!(
            seq_names, expected,
            "listing must include every created entry"
        );

        // Skip rayon parallel path on FreeBSD CI VMs (SIGSEGV under qemu).
        if !cfg!(target_os = "freebsd") {
            let parallel = ListOptions {
                parallel: true,
                threads: 0,
                ..Default::default()
            };
            let par = list_directory(&dir, &parallel).unwrap();
            let mut par_names: Vec<_> = par.entries.iter().map(|e| e.name.clone()).collect();
            par_names.sort();
            assert_eq!(
                seq_names, par_names,
                "parallel and sequential must produce identical ordered names"
            );
        }

        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn collect_timing_fills_phases() {
        let dir = temp_dir();
        for i in 0..8 {
            fs::write(dir.join(format!("f{i}")), b"x").unwrap();
        }
        let opts = ListOptions {
            collect_timing: true,
            parallel: false,
            threads: 1,
            ..Default::default()
        };
        let listing = list_directory(&dir, &opts).unwrap();
        let t = listing.timing.expect("timing present");
        // Just ensure fields are populated (may be 0ms on very fast FS).
        let _ = (t.readdir_ms, t.stat_ms, t.sort_ms);
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn threads_one_forces_serial_path() {
        let dir = temp_dir();
        for i in 0..40 {
            fs::write(dir.join(format!("n{i}")), b"x").unwrap();
        }
        let opts = ListOptions {
            parallel: true,
            threads: 1,
            ..Default::default()
        };
        assert!(!opts.use_parallel_stat(40));
        let listing = list_directory(&dir, &opts).unwrap();
        assert_eq!(listing.entries.len(), 40);
        let _ = fs::remove_dir_all(&dir);
    }
}