zlob 1.6.0-dev.4

SIMD optimized glob pattern matching library faster than glob crate
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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
use crate::error::ZlobError;
use crate::{ZlobFlags, ffi};
use bitflags::bitflags;
use std::ffi::{CStr, CString};
use std::marker::PhantomData;
use std::os::raw::{c_int, c_void};
use std::path::Path;

type Result<T> = std::result::Result<T, ZlobError>;

bitflags! {
    /// Options for a [`WalkBuilder`]. Combine with `|`.
    ///
    /// ```
    /// use zlob::walk::{WalkBuilder, WalkFlags};
    /// // raw traversal, sorted, directories suppressed
    /// let r = WalkBuilder::new(".")
    ///     .unwrap()
    ///     .options(WalkFlags::SORT | WalkFlags::NO_REPORT_DIRS)
    ///     .collect()
    ///     .unwrap();
    /// ```
    ///
    /// Bit values must match `ZLOB_WALK_*` in `include/zlob.h`.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub struct WalkFlags: u32 {
        /// Honor nested `.gitignore`/`.ignore` files; also skips `.git` dirs.
        const GITIGNORE       = 1 << 0;
        /// Skip hidden (dot) files and don't descend into dot-directories.
        const SKIP_HIDDEN     = 1 << 1;
        /// Follow symlinked directories (cycles are detected and broken).
        const FOLLOW_SYMLINKS = 1 << 2;
        /// Do **not** report directory entries (they are still traversed).
        const NO_REPORT_DIRS  = 1 << 3;
        /// Sort [`WalkBuilder::collect`] results by path.
        const SORT            = 1 << 4;
        /// Abort on the first directory error (default: skip unreadable dirs).
        const ABORT_ON_ERROR  = 1 << 5;
        /// With [`Self::GITIGNORE`]: still descend into `.git` directories.
        const KEEP_GIT_DIR    = 1 << 6;

        /// `ignore`-crate-style defaults: [`Self::GITIGNORE`] | [`Self::SKIP_HIDDEN`].
        const RECOMMENDED     = Self::GITIGNORE.bits() | Self::SKIP_HIDDEN.bits();
    }
}

bitflags! {
    /// Which metadata attributes the walker should fetch per entry. Combine with `|`.
    ///
    /// ```
    /// use zlob::walk::{WalkBuilder, WalkMetadata};
    /// let r = WalkBuilder::new(".")
    ///     .unwrap()
    ///     .metadata(WalkMetadata::SIZE | WalkMetadata::MTIME)
    ///     .collect()
    ///     .unwrap();
    /// ```
    ///
    /// Bit values must match `ZLOB_META_*` in `include/zlob.h`.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
    pub struct WalkMetadata: u32 {
        /// File size in bytes.
        const SIZE  = 1 << 0;
        /// Modification time.
        const MTIME = 1 << 1;
        /// Access time.
        const ATIME = 1 << 2;
        /// Status-change time.
        const CTIME = 1 << 3;
        /// Creation (birth) time — not available on all filesystems.
        const BTIME = 1 << 4;
        /// Inode number.
        const INODE = 1 << 5;
        /// Hard-link count.
        const NLINK = 1 << 6;
        /// Permission bits (mode & 0o7777).
        const MODE  = 1 << 7;
        /// Owner user id.
        const UID   = 1 << 8;
        /// Owner group id.
        const GID   = 1 << 9;

        /// Every supported attribute.
        const ALL   = 0x3FF;
    }
}

/// Flow control returned by [`WalkBuilder::run`] visitors.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WalkState {
    /// Keep walking.
    Continue,
    /// Don't descend into this directory (no effect for files).
    SkipDir,
    /// Stop the whole walk as soon as possible.
    Quit,
}

/// Entry kind.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WalkEntryKind {
    File,
    Dir,
    Symlink,
    Unknown,
}

/// Builder for a parallel directory walk. Settings diefault to [`WalkFlags::RECOMMENDED`]
#[derive(Debug, Clone)]
pub struct WalkBuilder {
    extra_ignore: Option<CString>,
    flags: WalkFlags,
    max_depth: u16,
    meta: WalkMetadata,
    pattern: Option<CString>,
    pattern_flags: i32,
    root: CString,
    threads: u16,
}

impl WalkBuilder {
    pub fn new(root: impl AsRef<Path>) -> Result<Self> {
        let bytes = path_to_bytes(root.as_ref());
        Ok(Self {
            root: CString::new(bytes).map_err(|_| ZlobError::InvalidInput)?,
            flags: WalkFlags::RECOMMENDED,
            meta: WalkMetadata::empty(),
            threads: 0,
            max_depth: 0,
            pattern_flags: 0,
            pattern: None,
            extra_ignore: None,
        })
    }

    /// Set the walk behavior flags, replacing any previously set flags
    /// (default: [`WalkFlags::RECOMMENDED`]).
    ///
    /// # Example
    ///
    /// ```no_run
    /// use zlob::walk::{WalkBuilder, WalkFlags};
    /// // raw traversal (walkdir-style): no gitignore, hidden shown
    /// let r = WalkBuilder::new(".")
    ///     .unwrap()
    ///     .options(WalkFlags::empty())
    ///     .collect()
    ///     .unwrap();
    /// ```
    pub fn options(&mut self, flags: WalkFlags) -> &mut Self {
        self.flags = flags;
        self
    }

    /// Metadata to fetch per entry (default: [`WalkMetadata::empty`]).
    pub fn metadata(&mut self, meta: WalkMetadata) -> &mut Self {
        self.meta = meta;
        self
    }

    /// White-list pattern for the included directories
    pub fn include(&mut self, pattern: impl AsRef<str>) -> Result<&mut Self> {
        self.pattern = Some(CString::new(pattern.as_ref()).map_err(|_| ZlobError::InvalidInput)?);
        self.pattern_flags = ZlobFlags::RECOMMENDED.bits();
        Ok(self)
    }

    /// Override `ZlobFlags::RECOMMENDED` for the included pattern. Always call it after
    /// `WalkBuilder::include`. Make sure that not all the flags make sense in this case,
    /// e.g. there is no reason to set ZlobFlags::GITIGNORE if gitignore is enabled for walker.
    ///
    /// ```no_run
    /// use zlob::{ZlobFlags, walk::WalkBuilder};
    /// # fn main() -> Result<(), zlob::ZlobError> {
    /// let r = WalkBuilder::new(".")?
    ///     .include("src/*.!(c|cpp)")? // pattern using bash extglob syntax
    ///     .include_flags(ZlobFlags::EXTGLOB) // need to enable it
    ///     .collect()?;
    /// # Ok(()) }
    /// ```
    pub fn include_flags(&mut self, flags: ZlobFlags) -> &mut Self {
        self.pattern_flags = flags.bits();
        self
    }

    /// Extra ignore rules layered into the walker, using `.gitignore` syntax.
    /// Those extra ignore rules are going to be surfaced in the output IgnoreRules set.
    ///
    /// ```no_run
    /// use zlob::walk::WalkBuilder;
    /// # fn main() -> Result<(), zlob::ZlobError> {
    /// let _ = WalkBuilder::new(".")?
    ///     .extra_ignore(&["node_modules", "target", ".venv", "!**/README.md"])?
    ///     .collect()?;
    /// # Ok(()) }
    /// ```
    pub fn extra_ignore<S: AsRef<str>>(&mut self, patterns: &[S]) -> Result<&mut Self> {
        let mut joined = String::new();
        for p in patterns {
            joined.push_str(p.as_ref());
            joined.push('\n');
        }

        self.extra_ignore = Some(CString::new(joined).map_err(|_| ZlobError::InvalidInput)?);
        Ok(self)
    }

    /// Number of worker threads. `0` (default) = one per CPU; `1` = run on the calling thread.
    pub fn threads(&mut self, n: usize) -> &mut Self {
        self.threads = n.min(u16::MAX as usize) as u16;
        self
    }

    /// Maximum depth to descend/yield. Direct children of the root are at
    /// depth 1. `None` (default) = unlimited.
    pub fn max_depth(&mut self, depth: Option<usize>) -> &mut Self {
        self.max_depth = depth.unwrap_or(0).min(u16::MAX as usize) as u16;
        self
    }

    /// Validated root + options. The returned pattern/root pointers borrow
    /// from `self` and stay valid for the duration of the FFI call.
    fn checked_options(&self) -> Result<(&CStr, ffi::zlob_walk_options_t)> {
        let root = self.root.as_ref();
        let pattern = match self.pattern.as_ref() {
            Some(p) => p.as_ptr(),
            None => std::ptr::null(),
        };
        let extra_ignore = match self.extra_ignore.as_ref() {
            Some(p) => p.as_ptr(),
            None => std::ptr::null(),
        };

        Ok((
            root,
            ffi::zlob_walk_options_t {
                flags: self.flags.bits(),
                meta_mask: self.meta.bits(),
                threads: self.threads,
                max_depth: self.max_depth,
                errfunc: None,
                pattern,
                pattern_flags: self.pattern_flags as u32,
                extra_ignore,
            },
        ))
    }

    /// Walk the tree and materialize all entries in one call.
    ///
    /// This is the fastest way to consume the walker from Rust: workers
    /// accumulate into private buffers and then return combined results
    pub fn collect(&self) -> Result<WalkResults> {
        let (root, opts) = self.checked_options()?;
        let mut out = ffi::zlob_walk_result_t {
            entries: std::ptr::null_mut(),
            count: 0,
            _storage: std::ptr::null_mut(),
        };

        let rc = unsafe { ffi::zlob_walk_collect(root.as_ptr(), &opts, &mut out) };
        match rc {
            0 => Ok(WalkResults { raw: out }),
            _ => Err(rc_to_error(rc)),
        }
    }

    /// Stream entries through `visitor`, in parallel calling callback for every entry.
    /// Returns assembled Walk
    ///
    /// the callback should neer panic, panicking = UB
    pub fn run<F>(&self, visitor: F) -> Result<WalkerOutcomeRules>
    where
        F: Fn(WalkEntry<'_>) -> WalkState + Sync,
    {
        self.run_inner(self.threads, &move |e| visitor(e))
    }

    /// Stream entries through `visitor` on the calling thread only.
    /// This allows to pass non-Sync cb to the walker for example mutate reference
    ///
    /// Returns a [`WalkRulesHandle`] just like [`Self::run`].
    pub fn run_serial<F>(&self, mut visitor: F) -> Result<WalkerOutcomeRules>
    where
        F: FnMut(WalkEntry<'_>) -> WalkState,
    {
        struct SerialBridge<F> {
            ptr: *mut F,
        }

        unsafe impl<F> Sync for SerialBridge<F> {}

        let bridge = SerialBridge {
            ptr: &mut visitor as *mut F,
        };

        // Capture by shared reference so the closure captures the *whole*
        // SerialBridge (Sync via unsafe impl above)
        let bridge_ref = &bridge;
        self.run_inner(1, &move |e| {
            let f = unsafe { &mut *bridge_ref.ptr };
            f(e)
        })
    }

    fn run_inner<F>(&self, threads: u16, visitor: &F) -> Result<WalkerOutcomeRules>
    where
        F: Fn(WalkEntry<'_>) -> WalkState + Sync,
    {
        unsafe extern "C" fn zlob_callback<F>(
            entry: *const ffi::zlob_walk_entry_t,
            ctx: *mut c_void,
        ) -> c_int
        where
            F: Fn(WalkEntry<'_>) -> WalkState + Sync,
        {
            let visitor = unsafe { &*(ctx as *const F) };
            let entry = WalkEntry {
                raw: unsafe { &*entry },
                _marker: PhantomData,
            };

            match visitor(entry) {
                WalkState::Continue => 0,
                WalkState::SkipDir => 1,
                WalkState::Quit => 2,
            }
        }

        let (root, mut opts) = self.checked_options()?;
        opts.threads = threads;
        let mut raw_rules: *mut c_void = std::ptr::null_mut();
        let rc = unsafe {
            ffi::zlob_walk(
                root.as_ptr(),
                &opts,
                Some(zlob_callback::<F>),
                visitor as *const F as *mut c_void,
                &mut raw_rules,
            )
        };
        match rc {
            0 => Ok(WalkerOutcomeRules { raw: raw_rules }),
            _ => Err(rc_to_error(rc)),
        }
    }
}

fn rc_to_error(rc: c_int) -> ZlobError {
    match ZlobError::from_code(rc) {
        Err(e) => e,
        // The walker never returns NOMATCH-style codes; treat anything else
        // unexpected as an abort.
        Ok(_) => ZlobError::Aborted,
    }
}

#[cfg(unix)]
fn path_to_bytes(p: &Path) -> Vec<u8> {
    use std::os::unix::ffi::OsStrExt;
    p.as_os_str().as_bytes().to_vec()
}

#[cfg(not(unix))]
fn path_to_bytes(p: &Path) -> Vec<u8> {
    p.to_string_lossy().into_owned().into_bytes()
}

/// A reusable set of rules used by the walker to ignore files
#[derive(Clone, Copy)]
pub struct IgnoreRules<'a> {
    handle: *mut c_void,
    _marker: PhantomData<&'a ()>,
}

unsafe impl Send for IgnoreRules<'_> {}
unsafe impl Sync for IgnoreRules<'_> {}

impl IgnoreRules<'_> {
    /// Returns `true` if the provided path is ignored by the collected rule set
    /// Works at a `Path` so both absolute and relative paths are supported.
    ///
    /// Performs `lstat` internally, marks non-accessible/non-existent files as ignored
    ///
    /// `relative_path` has to be relative to the walker's provided base path
    pub fn is_ignored(&self, path: impl AsRef<Path>) -> bool {
        let Ok(cpath) = CString::new(path_to_bytes(path.as_ref())) else {
            return false; // an interior NUL can't match any real path
        };
        unsafe { ffi::zlob_ignore_rules_match_path(self.handle, cpath.as_ptr()) != 0 }
    }
}

/// Owned results of [`WalkBuilder::collect`]. Holds all paths and metadata in
/// a handful of contiguous allocations.
pub struct WalkResults {
    raw: ffi::zlob_walk_result_t,
}

// The underlying storage is plain heap memory with no thread affinity.
unsafe impl Send for WalkResults {}
unsafe impl Sync for WalkResults {}

impl WalkResults {
    pub fn len(&self) -> usize {
        self.raw.count
    }

    pub fn is_empty(&self) -> bool {
        self.raw.count == 0
    }

    fn raw_entries(&self) -> &[ffi::zlob_walk_entry_t] {
        if self.raw.count == 0 || self.raw.entries.is_null() {
            return &[];
        }
        unsafe { std::slice::from_raw_parts(self.raw.entries, self.raw.count) }
    }

    pub fn get(&self, index: usize) -> Option<WalkEntry<'_>> {
        self.raw_entries().get(index).map(|raw| WalkEntry {
            raw,
            _marker: PhantomData,
        })
    }

    pub fn iter(&self) -> impl ExactSizeIterator<Item = WalkEntry<'_>> {
        self.raw_entries().iter().map(|raw| WalkEntry {
            raw,
            _marker: PhantomData,
        })
    }

    /// Reusable ignore rules (`.gitignore` + `.ignore` + custom rules matcher) gathered during
    /// the walk, for testing arbitrary paths afterwards. Always `Some` after a
    /// successful [`WalkBuilder::collect`] (empty when `GITIGNORE` was off or the
    /// tree had no ignore files).
    pub fn ignore_rules(&self) -> Option<IgnoreRules<'_>> {
        let handle = unsafe { ffi::zlob_walk_result_ignore_rules(&self.raw) };
        if handle.is_null() {
            None
        } else {
            Some(IgnoreRules {
                handle,
                _marker: PhantomData,
            })
        }
    }
}

impl Drop for WalkResults {
    fn drop(&mut self) {
        unsafe { ffi::zlob_walk_result_free(&mut self.raw) };
    }
}

impl std::fmt::Debug for WalkResults {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WalkResults")
            .field("count", &self.raw.count)
            .finish()
    }
}

/// The rules that were assembled during a previous walker run
pub struct WalkerOutcomeRules {
    raw: *mut c_void,
}

unsafe impl Send for WalkerOutcomeRules {}
unsafe impl Sync for WalkerOutcomeRules {}

impl WalkerOutcomeRules {
    /// Borrow the rules for querying. `None` when the walk gathered nothing
    /// (e.g. `WalkFlags::GITIGNORE` was off and no `extra_ignore` was set).
    pub fn rules(&self) -> Option<IgnoreRules<'_>> {
        if self.raw.is_null() {
            None
        } else {
            Some(IgnoreRules {
                handle: self.raw,
                _marker: PhantomData,
            })
        }
    }
}

impl Drop for WalkerOutcomeRules {
    fn drop(&mut self) {
        if !self.raw.is_null() {
            unsafe { ffi::zlob_ignore_rules_free(self.raw) };
            self.raw = std::ptr::null_mut();
        }
    }
}

impl std::fmt::Debug for WalkerOutcomeRules {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WalkRulesHandle").finish_non_exhaustive()
    }
}

/// A single walked entry
#[derive(Clone, Copy)]
pub struct WalkEntry<'a> {
    raw: &'a ffi::zlob_walk_entry_t,
    _marker: PhantomData<&'a ()>,
}

impl<'a> WalkEntry<'a> {
    #[inline]
    pub fn path_bytes(&self) -> &'a [u8] {
        unsafe { std::slice::from_raw_parts(self.raw.path as *const u8, self.raw.path_len) }
    }

    /// Full path (walk root joined with the relative path).
    #[inline]
    pub fn path(&self) -> &'a Path {
        bytes_to_path(self.path_bytes())
    }

    /// Path relative to the walk root.
    #[inline]
    pub fn relative_path(&self) -> &'a Path {
        bytes_to_path(&self.path_bytes()[self.raw.relative_offset as usize..])
    }

    /// Raw bytes of the path relative to the walk root.
    #[inline]
    pub fn relative_path_bytes(&self) -> &'a [u8] {
        &self.path_bytes()[self.raw.relative_offset as usize..]
    }

    /// If the item is kind of `WalkEntryKind::File` returns it's filename, otherwise None
    #[inline]
    pub fn basename(&self) -> Option<&'a str> {
        if !self.is_file() {
            return None;
        }

        std::str::from_utf8(&self.path_bytes()[self.raw.basename_offset as usize..]).ok()
    }

    /// Byte offset where the basename begins inside [`Self::relative_path_bytes`].
    #[inline]
    pub fn basename_offset_in_relative(&self) -> u16 {
        (self
            .raw
            .basename_offset
            .saturating_sub(self.raw.relative_offset)) as u16
    }

    /// Depth below the root — direct children of the root are at depth 1.
    #[inline]
    pub fn depth(&self) -> usize {
        self.raw.depth as usize
    }

    #[inline]
    pub fn kind(&self) -> WalkEntryKind {
        match self.raw.kind {
            1 => WalkEntryKind::File,
            2 => WalkEntryKind::Dir,
            3 => WalkEntryKind::Symlink,
            _ => WalkEntryKind::Unknown,
        }
    }

    #[inline]
    pub fn is_file(&self) -> bool {
        self.raw.kind == 1
    }

    #[inline]
    pub fn is_dir(&self) -> bool {
        self.raw.kind == 2
    }

    #[inline]
    pub fn is_symlink(&self) -> bool {
        self.raw.kind == 3
    }

    fn meta(&self, bit: WalkMetadata) -> bool {
        self.raw.meta_valid & bit.bits() != 0
    }

    /// File size in bytes (requires [`WalkMetadata::SIZE`]).
    pub fn size(&self) -> Option<u64> {
        self.meta(WalkMetadata::SIZE).then_some(self.raw.size)
    }

    /// Modification time, nanoseconds since the Unix epoch.
    pub fn modified_ns(&self) -> Option<i64> {
        self.meta(WalkMetadata::MTIME).then_some(self.raw.mtime_ns)
    }

    /// Access time, nanoseconds since the Unix epoch.
    pub fn accessed_ns(&self) -> Option<i64> {
        self.meta(WalkMetadata::ATIME).then_some(self.raw.atime_ns)
    }

    /// Status-change time, nanoseconds since the Unix epoch.
    pub fn changed_ns(&self) -> Option<i64> {
        self.meta(WalkMetadata::CTIME).then_some(self.raw.ctime_ns)
    }

    /// Creation (birth) time, nanoseconds since the Unix epoch.
    pub fn created_ns(&self) -> Option<i64> {
        self.meta(WalkMetadata::BTIME).then_some(self.raw.btime_ns)
    }

    pub fn inode(&self) -> Option<u64> {
        self.meta(WalkMetadata::INODE).then_some(self.raw.inode)
    }

    pub fn nlink(&self) -> Option<u32> {
        self.meta(WalkMetadata::NLINK).then_some(self.raw.nlink)
    }

    /// Permission bits (mode & 0o7777); the file type is in [`Self::kind`].
    pub fn mode(&self) -> Option<u32> {
        self.meta(WalkMetadata::MODE).then_some(self.raw.mode)
    }

    pub fn uid(&self) -> Option<u32> {
        self.meta(WalkMetadata::UID).then_some(self.raw.uid)
    }

    pub fn gid(&self) -> Option<u32> {
        self.meta(WalkMetadata::GID).then_some(self.raw.gid)
    }
}

impl std::fmt::Debug for WalkEntry<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WalkEntry")
            .field("path", &self.path())
            .field("kind", &self.kind())
            .field("depth", &self.depth())
            .finish()
    }
}

#[cfg(unix)]
fn bytes_to_path(b: &[u8]) -> &Path {
    use std::os::unix::ffi::OsStrExt;
    Path::new(std::ffi::OsStr::from_bytes(b))
}

#[cfg(not(unix))]
fn bytes_to_path(b: &[u8]) -> &Path {
    // The native walker produces WTF-8/UTF-8 encoded paths on Windows.
    Path::new(std::str::from_utf8(b).unwrap_or(""))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::sync::atomic::{AtomicUsize, Ordering};

    fn make_tree() -> tempfile::TempDir {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        fs::create_dir(root.join("src")).unwrap();
        fs::create_dir(root.join("target")).unwrap();
        fs::write(root.join(".gitignore"), "target/\n*.log\n").unwrap();
        fs::write(root.join("Cargo.toml"), "x").unwrap();
        fs::write(root.join("debug.log"), "").unwrap();
        fs::write(root.join("src/main.rs"), "fn main() {}").unwrap();
        fs::write(root.join("target/out.bin"), "bin").unwrap();
        dir
    }

    #[test]
    fn build_respects_gitignore() {
        let dir = make_tree();
        let results = WalkBuilder::new(dir.path())
            .unwrap()
            .options(WalkFlags::RECOMMENDED | WalkFlags::SORT)
            .threads(1)
            .collect()
            .unwrap();

        let names: Vec<String> = results
            .iter()
            .map(|e| e.relative_path().to_string_lossy().into_owned())
            .collect();

        // hidden(.gitignore) skipped, target/ and *.log ignored
        assert_eq!(names, vec!["Cargo.toml", "src", "src/main.rs"]);
    }

    #[test]
    fn build_plain_walkdir_mode() {
        let dir = make_tree();
        let results = WalkBuilder::new(dir.path())
            .unwrap()
            .options(WalkFlags::SORT)
            .threads(1)
            .collect()
            .unwrap();

        let names: Vec<String> = results
            .iter()
            .map(|e| e.relative_path().to_string_lossy().into_owned())
            .collect();
        assert_eq!(
            names,
            vec![
                ".gitignore",
                "Cargo.toml",
                "debug.log",
                "src",
                "src/main.rs",
                "target",
                "target/out.bin",
            ]
        );

        let main_rs = results
            .iter()
            .find(|e| e.basename() == Some("main.rs"))
            .unwrap();
        assert!(main_rs.is_file());
        assert_eq!(main_rs.depth(), 2);
        assert!(main_rs.path().ends_with("src/main.rs"));
        // no metadata requested
        assert_eq!(main_rs.size(), None);
    }

    #[test]
    fn metadata_size_and_mtime() {
        let dir = make_tree();
        let results = WalkBuilder::new(dir.path())
            .unwrap()
            .options(WalkFlags::empty())
            .threads(1)
            .metadata(WalkMetadata::SIZE | WalkMetadata::MTIME | WalkMetadata::INODE)
            .collect()
            .unwrap();

        let main_rs = results
            .iter()
            .find(|e| e.basename() == Some("main.rs"))
            .unwrap();
        assert_eq!(main_rs.size(), Some(12)); // "fn main() {}"
        assert!(main_rs.modified_ns().unwrap() > 0);
        assert!(main_rs.inode().unwrap() != 0);
    }

    #[test]
    fn parallel_run_visits_everything() {
        let dir = make_tree();
        let count = AtomicUsize::new(0);
        WalkBuilder::new(dir.path())
            .unwrap()
            .options(WalkFlags::empty())
            .run(|_entry| {
                count.fetch_add(1, Ordering::Relaxed);
                WalkState::Continue
            })
            .unwrap();
        assert_eq!(count.load(Ordering::Relaxed), 7);
    }

    #[test]
    fn run_returns_queryable_rules_handle() {
        // The streaming API now surfaces the retained ignore rules the same
        // way `collect()` does — same walk, same rule set.
        let dir = make_tree();
        let handle = WalkBuilder::new(dir.path())
            .unwrap()
            .run(|_| WalkState::Continue)
            .unwrap();
        let rules = handle.rules().expect("rules always present after a walk");
        // make_tree() creates target/ + debug.log, both gitignored. isIgnoredPath
        // lstats the on-disk paths.
        assert!(rules.is_ignored("target"));
        assert!(rules.is_ignored("debug.log"));
        assert!(!rules.is_ignored("Cargo.toml"));

        // Rules are usable after the walk returns — the handle owns storage
        // that lives until it drops (verified by the borrows above).
        drop(handle);
    }

    #[test]
    fn run_serial_accepts_non_sync_fnmut() {
        // The whole point of `run_serial`: no `Sync`/`Send` bound, so a plain
        // `FnMut` that mutates captured state without any synchronization
        // compiles and works. (`Vec`/`Rc` are `!Sync`; capturing them mutably
        // would be rejected by `run`.)
        use std::rc::Rc;
        let dir = make_tree();
        let mut names: Vec<String> = Vec::new();
        let not_sync: Rc<u8> = Rc::new(0); // a !Sync witness held across calls
        WalkBuilder::new(dir.path())
            .unwrap()
            .options(WalkFlags::empty())
            // Even with many threads configured, run_serial ignores it and
            // runs single-threaded, so this borrow is sound.
            .threads(8)
            .run_serial(|entry| {
                let _ = &not_sync;
                // basename() is file-only by design; this test wants the
                // leaf name of every entry (dirs included), so slice out of
                // the relative bytes directly.
                let rel = entry.relative_path_bytes();
                let leaf = &rel[entry.basename_offset_in_relative() as usize..];
                names.push(String::from_utf8_lossy(leaf).into_owned());
                WalkState::Continue
            })
            .unwrap();
        assert_eq!(names.len(), 7);
    }

    #[test]
    fn run_serial_skip_dir_and_quit() {
        let dir = make_tree();
        let mut count = 0usize;
        WalkBuilder::new(dir.path())
            .unwrap()
            .options(WalkFlags::empty())
            .run_serial(|entry| {
                count += 1;
                if entry.is_dir() {
                    WalkState::SkipDir
                } else {
                    WalkState::Continue
                }
            })
            .unwrap();
        // top level only: .gitignore, Cargo.toml, debug.log, src, target
        assert_eq!(count, 5);
    }

    #[test]
    fn run_skip_dir_and_quit() {
        let dir = make_tree();
        let count = AtomicUsize::new(0);
        WalkBuilder::new(dir.path())
            .unwrap()
            .options(WalkFlags::empty())
            .threads(1)
            .run(|entry| {
                count.fetch_add(1, Ordering::Relaxed);
                if entry.is_dir() {
                    WalkState::SkipDir
                } else {
                    WalkState::Continue
                }
            })
            .unwrap();
        // top level only: .gitignore, Cargo.toml, debug.log, src, target
        assert_eq!(count.load(Ordering::Relaxed), 5);

        let quit_count = AtomicUsize::new(0);
        WalkBuilder::new(dir.path())
            .unwrap()
            .threads(1)
            .run(|_| {
                quit_count.fetch_add(1, Ordering::Relaxed);
                WalkState::Quit
            })
            .unwrap();
        assert_eq!(quit_count.load(Ordering::Relaxed), 1);
    }

    #[test]
    fn max_depth_limits() {
        let dir = make_tree();
        let results = WalkBuilder::new(dir.path())
            .unwrap()
            .options(WalkFlags::empty())
            .threads(1)
            .max_depth(Some(1))
            .collect()
            .unwrap();
        assert_eq!(results.len(), 5);
        assert!(results.iter().all(|e| e.depth() == 1));
    }

    #[test]
    fn extra_ignore_un_ignores_via_negation() {
        // Project's .gitignore ignores every .rs file. The caller passes a
        // *negation* through extra_ignore to re-include `keep.rs` and the
        // entire `src/important/` subtree. This mirrors how a nested
        // .gitignore would override a shallower one.
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        fs::create_dir(root.join("src")).unwrap();
        fs::create_dir(root.join("src/important")).unwrap();
        fs::write(root.join(".gitignore"), "*.rs\n").unwrap();
        fs::write(root.join("keep.rs"), "fn k() {}").unwrap();
        fs::write(root.join("drop.rs"), "fn d() {}").unwrap();
        fs::write(root.join("src/main.rs"), "fn m() {}").unwrap();
        fs::write(root.join("src/lib.rs"), "fn l() {}").unwrap();
        fs::write(root.join("src/important/special.rs"), "fn s() {}").unwrap();
        fs::write(root.join("src/important/other.rs"), "fn o() {}").unwrap();

        let results = WalkBuilder::new(root)
            .unwrap()
            .options(WalkFlags::RECOMMENDED | WalkFlags::SORT)
            .threads(1)
            // - "!keep.rs" re-includes a single file at the root.
            // - "!src/important/**" re-includes a whole subtree.
            // - Both win because extra_ignore is checked before the project's
            //   "*.rs" rule (same precedence as a deeper nested .gitignore).
            .extra_ignore(&["!keep.rs", "!src/important/**"])
            .unwrap()
            .collect()
            .unwrap();

        let names: Vec<String> = results
            .iter()
            .map(|e| e.relative_path().to_string_lossy().into_owned())
            .collect();

        // Re-included by extra_ignore (negations win).
        assert!(
            names.contains(&"keep.rs".to_string()),
            "keep.rs should be re-included, got {names:?}"
        );
        assert!(
            names.iter().any(|n| n == "src/important/special.rs"),
            "src/important/special.rs should be re-included, got {names:?}"
        );
        assert!(
            names.iter().any(|n| n == "src/important/other.rs"),
            "src/important/other.rs should be re-included, got {names:?}"
        );

        // Dropped by the project's `*.rs` rule, NOT re-included.
        assert!(
            !names.contains(&"drop.rs".to_string()),
            "drop.rs should remain ignored, got {names:?}"
        );
        assert!(
            !names.iter().any(|n| n == "src/main.rs"),
            "src/main.rs should remain ignored, got {names:?}"
        );
        assert!(
            !names.iter().any(|n| n == "src/lib.rs"),
            "src/lib.rs should remain ignored, got {names:?}"
        );

        // The IgnoreRules surface mirrors what the walk applied — extra
        // patterns are checked there too, so external `is_ignored()` agrees.
        let rules = results.ignore_rules().expect("rules always present");
        assert!(!rules.is_ignored("keep.rs"));
        assert!(rules.is_ignored("drop.rs"));
        assert!(!rules.is_ignored("src/important/special.rs"));
        assert!(rules.is_ignored("src/main.rs"));
    }

    #[test]
    fn extra_ignore_adds_new_rules() {
        // No project .gitignore; extra_ignore alone supplies the ignore list.
        // Confirms many patterns batch into one matcher and surface in the
        // returned IgnoreRules.
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        fs::create_dir(root.join("node_modules")).unwrap();
        fs::create_dir(root.join("target")).unwrap();
        fs::write(root.join("node_modules/a.js"), "").unwrap();
        fs::write(root.join("target/out.o"), "").unwrap();
        fs::write(root.join("README.md"), "").unwrap();
        fs::write(root.join("main.rs"), "").unwrap();

        let results = WalkBuilder::new(root)
            .unwrap()
            .options(WalkFlags::empty()) // no gitignore-discovery, no hidden-skip
            .threads(1)
            .extra_ignore(&["node_modules", "/target"])
            .unwrap()
            .collect()
            .unwrap();

        let names: Vec<String> = results
            .iter()
            .map(|e| e.relative_path().to_string_lossy().into_owned())
            .collect();

        assert!(
            !names.iter().any(|n| n.starts_with("node_modules")),
            "node_modules subtree should be pruned, got {names:?}"
        );
        assert!(
            !names.iter().any(|n| n.starts_with("target")),
            "target/ subtree should be pruned at root, got {names:?}"
        );
        assert!(names.contains(&"README.md".to_string()));
        assert!(names.contains(&"main.rs".to_string()));

        let rules = results.ignore_rules().expect("rules always present");
        assert!(rules.is_ignored("node_modules/"));
        assert!(rules.is_ignored("target/"));
        assert!(!rules.is_ignored("main.rs"));
    }

    #[test]
    fn interior_nul_is_an_error_not_a_silent_default() {
        // A root with an interior NUL must fail at construction, not
        // silently walk the cwd.
        let err = WalkBuilder::new("bad\0root").unwrap_err();
        assert_eq!(err, ZlobError::InvalidInput);

        // A pattern with an interior NUL fails at `.include(...)` — same
        // shape as the root check, just on a different builder method.
        let dir = make_tree();
        let mut b = WalkBuilder::new(dir.path()).unwrap();
        let err = b.include("*.\0rs").unwrap_err();
        assert_eq!(err, ZlobError::InvalidInput);

        // Retrying with a valid pattern on the same builder works, since
        // the failed call didn't corrupt any state.
        let ok = WalkBuilder::new(dir.path())
            .unwrap()
            .include("**/*.rs")
            .unwrap()
            .collect();
        assert!(ok.is_ok());
    }

    #[test]
    fn empty_and_missing_roots() {
        let dir = tempfile::tempdir().unwrap();
        let results = WalkBuilder::new(dir.path()).unwrap().collect().unwrap();
        assert!(results.is_empty());

        // Missing roots are reported through the (unset) error callback and
        // produce an empty result unless abort_on_error is set.
        let missing = WalkBuilder::new("/definitely/not/a/real/path/zlob")
            .unwrap()
            .collect()
            .unwrap();
        assert!(missing.is_empty());

        let err = WalkBuilder::new("/definitely/not/a/real/path/zlob")
            .unwrap()
            .options(WalkFlags::RECOMMENDED | WalkFlags::ABORT_ON_ERROR)
            .collect();
        assert!(err.is_err());
    }

    #[test]
    fn is_ignored_accepts_windows_style_backslash_paths() {
        // The matcher normalizes '\\' → '/' on the way in so Windows callers
        // (who get native '\\'-separated paths from std::path) get the same
        // answers as Unix callers passing '/'.
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        fs::write(root.join(".gitignore"), "target/\n*.log\n").unwrap();
        fs::create_dir_all(root.join("target/foo")).unwrap();
        fs::write(root.join("target/foo/bar.rs"), "").unwrap();
        fs::create_dir_all(root.join("src/deeply/nested")).unwrap();
        fs::write(root.join("src/deeply/nested/scratch.log"), "").unwrap();
        fs::write(root.join("src/main.rs"), "").unwrap();

        let results = WalkBuilder::new(root)
            .unwrap()
            .threads(1)
            .collect()
            .unwrap();
        let rules = results.ignore_rules().expect("rules always present");

        // Same assertions as the ancestor-walk test but with '\\' separators.
        assert!(rules.is_ignored("target\\foo\\bar.rs"));
        assert!(rules.is_ignored("src\\deeply\\nested\\scratch.log"));
        assert!(!rules.is_ignored("src\\main.rs"));
        // Mixed separators — the leading '/' style should still work too.
        assert!(rules.is_ignored("target/foo\\bar.rs"));
    }

    #[test]
    fn is_ignored_walks_ancestors_for_unvisited_subtrees() {
        // The walker never enters `target/` because the root .gitignore prunes
        // it — so the by_dir HashMap has no node for `target/`, only for the
        // walk root. `is_ignored("target/foo/bar.rs")` still has to say yes,
        // by consulting the root's rule as the ancestor of that path.
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        fs::create_dir(root.join("src")).unwrap();
        fs::create_dir_all(root.join("target/foo")).unwrap();
        fs::create_dir_all(root.join("target/deeply/nested")).unwrap();
        fs::create_dir_all(root.join("src/deeply/nested")).unwrap();
        fs::write(root.join(".gitignore"), "target/\n*.log\n").unwrap();
        fs::write(root.join("src/main.rs"), "").unwrap();
        fs::write(root.join("src/lib.rs"), "").unwrap();
        fs::write(root.join("target/foo/bar.rs"), "").unwrap();
        fs::write(root.join("target/deeply/nested/thing.o"), "").unwrap();
        fs::write(root.join("src/deeply/nested/scratch.log"), "").unwrap();

        let results = WalkBuilder::new(root)
            .unwrap()
            .threads(1)
            .collect()
            .unwrap();
        let rules = results.ignore_rules().expect("rules always present");

        // Files under the pruned `target/` subtree — walker never opened the
        // dir, but the root's `target/` rule still catches every descendant.
        assert!(
            rules.is_ignored("target/foo/bar.rs"),
            "target/ at root should catch descendants even when target/ is pruned"
        );
        assert!(rules.is_ignored("target/deeply/nested/thing.o"));
        // File rule from the same root .gitignore, applied at arbitrary depth.
        assert!(rules.is_ignored("src/deeply/nested/scratch.log"));
        // Sanity: non-ignored path from a visited subtree.
        assert!(!rules.is_ignored("src/main.rs"));
        assert!(!rules.is_ignored("src/lib.rs"));
    }

    #[test]
    fn retained_ignore_rules_are_reusable() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        fs::create_dir(root.join("src")).unwrap();
        // Root: ignore *.log and build/; .ignore re-includes important.tmp,
        // while .gitignore ignores all *.tmp.
        fs::write(root.join(".gitignore"), "*.log\nbuild/\n*.tmp\n").unwrap();
        fs::write(root.join(".ignore"), "!important.tmp\n").unwrap();
        // Nested rule scoped to src/.
        fs::write(root.join("src/.gitignore"), "*.bak\n").unwrap();
        fs::write(root.join("src/main.rs"), "").unwrap();
        // Materialize the paths we query so lstat can classify them.
        fs::write(root.join("app.log"), "").unwrap();
        fs::create_dir(root.join("build")).unwrap();
        fs::write(root.join("scratch.tmp"), "").unwrap();
        fs::write(root.join("important.tmp"), "").unwrap();
        fs::write(root.join("src/old.bak"), "").unwrap();
        fs::write(root.join("old.bak"), "").unwrap();

        let results = WalkBuilder::new(root)
            .unwrap()
            .threads(1)
            .collect()
            .unwrap();

        let rules = results.ignore_rules().expect("rules always present");

        // Root-level rules — lstat now supplies directory-ness.
        assert!(rules.is_ignored("app.log"));
        // "build" exists as a directory on disk; the dir-only `build/` rule fires.
        assert!(rules.is_ignored("build"));
        assert!(rules.is_ignored("scratch.tmp"));
        // .ignore precedence re-includes important.tmp.
        assert!(!rules.is_ignored("important.tmp"));
        // Nested rule only inside src/.
        assert!(rules.is_ignored("src/old.bak"));
        // A `*.bak` file at the *root* is not covered by the src/-scoped rule.
        assert!(!rules.is_ignored("old.bak"));
        assert!(!rules.is_ignored("src/main.rs"));

        // Non-existent paths surface as ignored per the new contract.
        assert!(rules.is_ignored("this-does-not-exist"));

        // Absolute-path queries work too, provided they're inside the walk.
        let abs = root.join("app.log");
        assert!(rules.is_ignored(abs.to_str().unwrap()));
        // Absolute path outside the walk → ignored (not part of this walk).
        assert!(rules.is_ignored("/etc/passwd"));
    }

    #[test]
    fn ignore_rules_always_available() {
        // Rules are now always returned (no opt-in flag). With git_ignore on,
        // the tree's rules are queryable.
        let dir = make_tree();
        let root = dir.path();
        let results = WalkBuilder::new(root).unwrap().collect().unwrap();
        let rules = results.ignore_rules().expect("rules always present");
        // make_tree() creates target/ (dir) and debug.log (file), both
        // gitignored. Query the paths as they exist on disk.
        assert!(rules.is_ignored("target"));
        assert!(rules.is_ignored("debug.log"));
        assert!(!rules.is_ignored("Cargo.toml"));
    }

    #[cfg(unix)]
    #[test]
    fn permission_denied_surfaces_with_abort_on_error() {
        use std::os::unix::fs::PermissionsExt;

        let dir = tempfile::tempdir().unwrap();
        let locked = dir.path().join("locked");
        fs::create_dir(&locked).unwrap();
        fs::write(locked.join("inner.txt"), "x").unwrap();
        // Remove all access so opening/reading the directory fails.
        fs::set_permissions(&locked, fs::Permissions::from_mode(0o000)).unwrap();

        // Restore permissions on the way out so the tempdir can be cleaned up,
        // regardless of how the assertions go.
        struct Restore<'a>(&'a std::path::Path);
        impl Drop for Restore<'_> {
            fn drop(&mut self) {
                let _ = fs::set_permissions(self.0, fs::Permissions::from_mode(0o755));
            }
        }
        let _restore = Restore(&locked);

        // Skip when running as root, where mode 0o000 is not enforced.
        if std::fs::read_dir(&locked).is_ok() {
            return;
        }

        // Tolerated by default: the unreadable directory is skipped.
        let ok = WalkBuilder::new(dir.path())
            .unwrap()
            .options(WalkFlags::empty())
            .collect();
        assert!(ok.is_ok());

        // With abort_on_error the permission error surfaces as its own variant
        // rather than collapsing into Aborted.
        let err = WalkBuilder::new(dir.path())
            .unwrap()
            .options(WalkFlags::ABORT_ON_ERROR)
            .threads(1)
            .collect()
            .unwrap_err();
        assert_eq!(err, ZlobError::PermissionDenied);
    }

    // Guards against drift between the Rust bitflags and the C header. The
    // walk/meta constants come from bindgen (real build only).
    #[cfg(not(docsrs))]
    #[test]
    fn flag_values_match_c_header() {
        assert_eq!(WalkFlags::GITIGNORE.bits(), ffi::ZLOB_WALK_GITIGNORE as u32);
        assert_eq!(
            WalkFlags::SKIP_HIDDEN.bits(),
            ffi::ZLOB_WALK_SKIP_HIDDEN as u32
        );
        assert_eq!(
            WalkFlags::FOLLOW_SYMLINKS.bits(),
            ffi::ZLOB_WALK_FOLLOW_SYMLINKS as u32
        );
        assert_eq!(
            WalkFlags::NO_REPORT_DIRS.bits(),
            ffi::ZLOB_WALK_NO_REPORT_DIRS as u32
        );
        assert_eq!(WalkFlags::SORT.bits(), ffi::ZLOB_WALK_SORT as u32);
        assert_eq!(
            WalkFlags::ABORT_ON_ERROR.bits(),
            ffi::ZLOB_WALK_ABORT_ON_ERROR as u32
        );
        assert_eq!(
            WalkFlags::KEEP_GIT_DIR.bits(),
            ffi::ZLOB_WALK_KEEP_GIT_DIR as u32
        );
        assert_eq!(
            WalkFlags::RECOMMENDED,
            WalkFlags::GITIGNORE | WalkFlags::SKIP_HIDDEN
        );

        assert_eq!(WalkMetadata::SIZE.bits(), ffi::ZLOB_META_SIZE as u32);
        assert_eq!(WalkMetadata::MTIME.bits(), ffi::ZLOB_META_MTIME as u32);
        assert_eq!(WalkMetadata::ATIME.bits(), ffi::ZLOB_META_ATIME as u32);
        assert_eq!(WalkMetadata::CTIME.bits(), ffi::ZLOB_META_CTIME as u32);
        assert_eq!(WalkMetadata::BTIME.bits(), ffi::ZLOB_META_BTIME as u32);
        assert_eq!(WalkMetadata::INODE.bits(), ffi::ZLOB_META_INODE as u32);
        assert_eq!(WalkMetadata::NLINK.bits(), ffi::ZLOB_META_NLINK as u32);
        assert_eq!(WalkMetadata::MODE.bits(), ffi::ZLOB_META_MODE as u32);
        assert_eq!(WalkMetadata::UID.bits(), ffi::ZLOB_META_UID as u32);
        assert_eq!(WalkMetadata::GID.bits(), ffi::ZLOB_META_GID as u32);
        assert_eq!(WalkMetadata::ALL.bits(), ffi::ZLOB_META_ALL as u32);
    }
}

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

    #[test]
    fn glob_filters_and_prunes() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        fs::create_dir(root.join("src")).unwrap();
        fs::create_dir(root.join("docs")).unwrap();
        fs::write(root.join("src/main.rs"), "").unwrap();
        fs::write(root.join("src/lib.rs"), "").unwrap();
        fs::write(root.join("docs/guide.md"), "").unwrap();
        fs::write(root.join("top.rs"), "").unwrap();

        let results = WalkBuilder::new(root)
            .unwrap()
            .options(WalkFlags::SORT)
            .threads(1)
            .include("**/*.rs")
            .unwrap()
            .collect()
            .unwrap();

        let names: Vec<String> = results
            .iter()
            .map(|e| e.relative_path().to_string_lossy().into_owned())
            .collect();
        assert_eq!(names, vec!["src/lib.rs", "src/main.rs", "top.rs"]);

        // Brace pattern through the default flags.
        let braced = WalkBuilder::new(root)
            .unwrap()
            .options(WalkFlags::empty())
            .threads(1)
            .include("**/*.{md,rs}")
            .unwrap()
            .collect()
            .unwrap();
        assert_eq!(braced.len(), 4);

        // Anchored pattern narrows traversal to the src/ subtree.
        let scoped = WalkBuilder::new(root)
            .unwrap()
            .options(WalkFlags::empty())
            .threads(1)
            .include("src/**/*.rs")
            .unwrap()
            .collect()
            .unwrap();
        assert_eq!(scoped.len(), 2);
    }

    #[test]
    fn glob_composes_with_gitignore_and_parallel_run() {
        use std::sync::atomic::{AtomicUsize, Ordering};

        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        fs::create_dir(root.join("src")).unwrap();
        fs::create_dir(root.join("target")).unwrap();
        fs::write(root.join(".gitignore"), "target/\n").unwrap();
        fs::write(root.join("src/a.rs"), "").unwrap();
        fs::write(root.join("target/gen.rs"), "").unwrap();

        let count = AtomicUsize::new(0);
        WalkBuilder::new(root)
            .unwrap()
            .include("**/*.rs")
            .unwrap()
            .run(|entry| {
                assert!(entry.path().extension().is_some_and(|e| e == "rs"));
                count.fetch_add(1, Ordering::Relaxed);
                WalkState::Continue
            })
            .unwrap();
        // target/gen.rs is gitignored; only src/a.rs survives both filters.
        assert_eq!(count.load(Ordering::Relaxed), 1);
    }
}