rsplug-walker 0.2.4

High-performance async directory walker with adaptive concurrency
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
use crate::compiled_glob::CompiledGlob;
use std::fmt;
use std::io;
use std::path::PathBuf;
use tokio::sync::mpsc;

#[cfg(not(windows))]
#[path = "walker_unix.rs"]
mod backend;
#[cfg(windows)]
#[path = "walker_windows.rs"]
mod backend;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum EntryKind {
    File,
    Dir,
    Symlink,
    Other,
}

#[derive(Debug)]
pub struct WalkEvent {
    pub path: PathBuf,
    pub kind: EntryKind,
}

#[derive(Debug)]
pub enum WalkError {
    Io {
        path: PathBuf,
        source: io::Error,
    },
    Unsupported {
        feature: &'static str,
        path: PathBuf,
    },
}

impl fmt::Display for WalkError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            WalkError::Io { path, source } => {
                write!(f, "io error at {}: {source}", path.display())
            }
            WalkError::Unsupported { feature, path } => {
                write!(f, "unsupported feature `{feature}` at {}", path.display())
            }
        }
    }
}

impl std::error::Error for WalkError {}

pub type WalkMessage = Result<WalkEvent, WalkError>;

#[derive(Clone, Debug)]
pub struct WalkerOptions {
    pub channel_capacity: usize,
    pub files_only: bool,
}

impl Default for WalkerOptions {
    fn default() -> Self {
        Self {
            channel_capacity: 1024,
            files_only: false,
        }
    }
}

pub struct Walker;

impl Walker {
    pub fn spawn(compiled: CompiledGlob) -> mpsc::Receiver<WalkMessage> {
        Self::spawn_with_options(compiled, WalkerOptions::default())
    }

    pub fn spawn_many(
        globs: impl IntoIterator<Item = CompiledGlob>,
    ) -> mpsc::Receiver<WalkMessage> {
        Self::spawn_many_with_options(globs, WalkerOptions::default())
    }

    pub fn spawn_with_options(
        compiled: CompiledGlob,
        options: WalkerOptions,
    ) -> mpsc::Receiver<WalkMessage> {
        Self::spawn_many_with_options([compiled], options)
    }

    pub fn spawn_many_with_options(
        globs: impl IntoIterator<Item = CompiledGlob>,
        options: WalkerOptions,
    ) -> mpsc::Receiver<WalkMessage> {
        let merged = match CompiledGlob::merge_many(globs) {
            Ok(merged) => merged,
            Err(err) => {
                let (tx, rx) = mpsc::channel(options.channel_capacity.max(1));
                tokio::spawn(async move {
                    let _ = tx
                        .send(Err(WalkError::Io {
                            path: PathBuf::from("<spawn_many>"),
                            source: err,
                        }))
                        .await;
                });
                return rx;
            }
        };

        backend::spawn_single_with_options(merged, options)
    }
}

#[cfg(test)]
mod tests {
    #[cfg(all(unix, not(windows)))]
    use super::*;
    use crate::compiled_glob::CompiledGlob;
    #[cfg(all(unix, not(windows)))]
    use std::collections::BTreeSet;
    #[cfg(all(unix, not(windows)))]
    use std::fs;
    #[cfg(all(unix, not(windows)))]
    use std::path::PathBuf;
    #[cfg(all(unix, not(windows)))]
    use std::time::Duration;
    #[cfg(all(unix, not(windows)))]
    use std::time::{SystemTime, UNIX_EPOCH};

    #[cfg(all(unix, not(windows)))]
    fn test_root(name: &str) -> PathBuf {
        let stamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("clock should be valid")
            .as_nanos();
        std::env::temp_dir().join(format!("walker-{name}-{stamp}"))
    }

    #[tokio::test]
    async fn descend_match_equivalence() {
        let one = CompiledGlob::new("a/**/**/b").expect("glob must parse");
        let two = CompiledGlob::new("a/**/b").expect("glob must parse");
        for path in ["a/b", "a/x/b", "a/x/y/b", "a/x/y/c", "x/a/b", "a/x/y/z"] {
            assert_eq!(one.r#match(path.as_ref()), two.r#match(path.as_ref()));
        }
    }

    #[tokio::test]
    #[cfg(all(unix, not(windows)))]
    async fn streams_results_before_full_walk() {
        let root = test_root("stream");
        fs::create_dir_all(root.join("d1/d2")).expect("create tree");
        fs::write(root.join("d1/a.txt"), b"a").expect("write file");
        fs::write(root.join("d1/d2/b.txt"), b"b").expect("write file");

        let pattern = format!("{}/**", root.display());
        let glob = CompiledGlob::new(&pattern).expect("glob must parse");
        let mut rx = Walker::spawn(glob);

        let first = tokio::time::timeout(Duration::from_secs(2), rx.recv())
            .await
            .expect("must receive quickly");
        assert!(first.is_some());

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

    #[tokio::test]
    #[cfg(all(unix, not(windows)))]
    async fn finds_expected_paths() {
        let root = test_root("paths");
        fs::create_dir_all(root.join("src/bin")).expect("create tree");
        fs::create_dir_all(root.join("docs")).expect("create tree");
        fs::write(root.join("src/main.rs"), b"fn main(){}").expect("write file");
        fs::write(root.join("src/bin/tool.rs"), b"fn main(){}").expect("write file");
        fs::write(root.join("docs/readme.md"), b"# hi").expect("write file");

        let pattern = format!("{}/**/*.rs", root.display());
        let glob = CompiledGlob::new(&pattern).expect("glob must parse");
        let mut rx = Walker::spawn(glob);

        let mut got = BTreeSet::new();
        while let Some(msg) = tokio::time::timeout(Duration::from_secs(2), rx.recv())
            .await
            .expect("channel should respond")
        {
            if let Ok(ev) = msg {
                got.insert(
                    ev.path
                        .strip_prefix(&root)
                        .expect("path under root")
                        .to_path_buf(),
                );
            }
        }

        let expected: BTreeSet<PathBuf> = ["src/main.rs", "src/bin/tool.rs"]
            .iter()
            .map(PathBuf::from)
            .collect();
        assert_eq!(got, expected);
        let _ = fs::remove_dir_all(&root);
    }

    #[tokio::test]
    #[cfg(all(unix, not(windows)))]
    async fn symlink_loop_does_not_hang() {
        use std::os::unix::fs::symlink;

        let root = test_root("loop");
        fs::create_dir_all(root.join("a/b")).expect("create tree");
        fs::write(root.join("a/b/file.txt"), b"1").expect("write file");
        symlink(root.join("a"), root.join("a/b/link_to_a")).expect("create symlink");

        let pattern = format!("{}/**", root.display());
        let glob = CompiledGlob::new(&pattern).expect("glob must parse");
        let mut rx = Walker::spawn(glob);

        let mut count = 0usize;
        while let Ok(Some(_)) = tokio::time::timeout(Duration::from_millis(250), rx.recv()).await {
            count += 1;
            if count > 64 {
                break;
            }
        }
        assert!(count > 0);
        assert!(count <= 64);

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

    #[tokio::test]
    #[cfg(all(unix, not(windows)))]
    async fn permission_denied_does_not_abort_descend_walk() {
        use std::os::unix::fs::PermissionsExt;

        let root = test_root("perm");
        fs::create_dir_all(root.join("ok")).expect("create tree");
        fs::create_dir_all(root.join("blocked")).expect("create tree");
        fs::write(root.join("ok/keep.rs"), b"fn main(){}").expect("write file");
        fs::set_permissions(root.join("blocked"), fs::Permissions::from_mode(0o0))
            .expect("chmod blocked");

        let pattern = format!("{}/**.rs", root.display());
        let glob = CompiledGlob::new(&pattern).expect("glob must parse");
        let mut rx = Walker::spawn(glob);

        let mut got_ok = false;
        while let Some(msg) = tokio::time::timeout(Duration::from_secs(3), rx.recv())
            .await
            .expect("channel should respond")
        {
            match msg {
                Ok(ev) => {
                    if ev.path == root.join("ok/keep.rs") {
                        got_ok = true;
                    }
                }
                Err(WalkError::Io { .. }) => {}
                Err(WalkError::Unsupported { .. }) => {}
            }
        }

        assert!(got_ok, "accessible matches should still be emitted");

        fs::set_permissions(root.join("blocked"), fs::Permissions::from_mode(0o755))
            .expect("restore perms");
        let _ = fs::remove_dir_all(&root);
    }

    #[tokio::test]
    #[cfg(all(unix, not(windows)))]
    async fn spawn_many_matches_union_of_patterns() {
        let root = test_root("many_union");
        fs::create_dir_all(root.join("src")).expect("create tree");
        fs::create_dir_all(root.join("docs")).expect("create tree");
        fs::write(root.join("src/main.rs"), b"fn main(){}").expect("write file");
        fs::write(root.join("docs/readme.md"), b"# hi").expect("write file");

        let g1 =
            CompiledGlob::new(&format!("{}/**/*.rs", root.display())).expect("glob must parse");
        let g2 =
            CompiledGlob::new(&format!("{}/**/*.md", root.display())).expect("glob must parse");
        let mut rx = Walker::spawn_many(vec![g1, g2]);

        let mut got = BTreeSet::new();
        while let Some(msg) = tokio::time::timeout(Duration::from_secs(2), rx.recv())
            .await
            .expect("channel should respond")
        {
            if let Ok(ev) = msg {
                got.insert(
                    ev.path
                        .strip_prefix(&root)
                        .expect("path under root")
                        .to_path_buf(),
                );
            }
        }

        let expected: BTreeSet<PathBuf> = ["src/main.rs", "docs/readme.md"]
            .iter()
            .map(PathBuf::from)
            .collect();
        assert_eq!(got, expected);
        let _ = fs::remove_dir_all(&root);
    }

    #[tokio::test]
    #[cfg(all(unix, not(windows)))]
    async fn spawn_many_single_equivalent_to_spawn() {
        let root = test_root("many_single");
        fs::create_dir_all(root.join("src/bin")).expect("create tree");
        fs::write(root.join("src/main.rs"), b"fn main(){}").expect("write file");
        fs::write(root.join("src/bin/tool.rs"), b"fn main(){}").expect("write file");

        let pattern = format!("{}/**/*.rs", root.display());
        let glob = CompiledGlob::new(&pattern).expect("glob must parse");

        let mut single_rx = Walker::spawn(CompiledGlob::new(&pattern).expect("glob must parse"));
        let mut many_rx = Walker::spawn_many(vec![glob]);

        let mut single = BTreeSet::new();
        while let Some(msg) = tokio::time::timeout(Duration::from_secs(2), single_rx.recv())
            .await
            .expect("channel should respond")
        {
            if let Ok(ev) = msg {
                single.insert(
                    ev.path
                        .strip_prefix(&root)
                        .expect("path under root")
                        .to_path_buf(),
                );
            }
        }

        let mut many = BTreeSet::new();
        while let Some(msg) = tokio::time::timeout(Duration::from_secs(2), many_rx.recv())
            .await
            .expect("channel should respond")
        {
            if let Ok(ev) = msg {
                many.insert(
                    ev.path
                        .strip_prefix(&root)
                        .expect("path under root")
                        .to_path_buf(),
                );
            }
        }

        assert_eq!(single, many);
        let _ = fs::remove_dir_all(&root);
    }

    #[tokio::test]
    #[cfg(all(unix, not(windows)))]
    async fn spawn_many_duplicate_patterns_no_duplicate_path() {
        let root = test_root("many_dup");
        fs::create_dir_all(root.join("src")).expect("create tree");
        fs::write(root.join("src/main.rs"), b"fn main(){}").expect("write file");

        let pattern = format!("{}/**/*.rs", root.display());
        let g1 = CompiledGlob::new(&pattern).expect("glob must parse");
        let g2 = CompiledGlob::new(&pattern).expect("glob must parse");
        let mut rx = Walker::spawn_many(vec![g1, g2]);

        let mut count = 0usize;
        while let Some(msg) = tokio::time::timeout(Duration::from_secs(2), rx.recv())
            .await
            .expect("channel should respond")
        {
            if let Ok(ev) = msg
                && ev.path.ends_with("src/main.rs")
            {
                count += 1;
            }
        }

        assert_eq!(count, 1);
        let _ = fs::remove_dir_all(&root);
    }

    #[tokio::test]
    #[cfg(all(unix, not(windows)))]
    async fn spawn_many_applies_last_match_wins_with_excludes() {
        let root = test_root("many_exclude");
        fs::create_dir_all(root.join("target")).expect("create tree");
        fs::write(root.join("target/keep.txt"), b"x").expect("write file");
        fs::write(root.join("target/ignore.txt"), b"x").expect("write file");

        let include =
            CompiledGlob::new(&format!("{}/**/*.txt", root.display())).expect("glob must parse");
        let exclude = CompiledGlob::new(&format!("!{}/**/ignore.txt", root.display()))
            .expect("glob must parse");
        let reinclude = CompiledGlob::new(&format!("{}/**/ignore.txt", root.display()))
            .expect("glob must parse");
        let mut rx = Walker::spawn_many(vec![include, exclude, reinclude]);

        let mut got = BTreeSet::new();
        while let Some(msg) = tokio::time::timeout(Duration::from_secs(2), rx.recv())
            .await
            .expect("channel should respond")
        {
            if let Ok(ev) = msg {
                got.insert(
                    ev.path
                        .strip_prefix(&root)
                        .expect("path under root")
                        .to_path_buf(),
                );
            }
        }

        let expected: BTreeSet<PathBuf> = ["target/keep.txt", "target/ignore.txt"]
            .iter()
            .map(PathBuf::from)
            .collect();
        assert_eq!(got, expected);
        let _ = fs::remove_dir_all(&root);
    }

    #[tokio::test]
    #[cfg(all(unix, not(windows)))]
    async fn files_only_does_not_emit_directories() {
        let root = test_root("files_only");
        fs::create_dir_all(root.join("src/bin")).expect("create tree");
        fs::write(root.join("src/main.rs"), b"fn main(){}").expect("write file");
        fs::write(root.join("src/bin/tool.rs"), b"fn main(){}").expect("write file");

        let pattern = format!("{}/**", root.display());
        let glob = CompiledGlob::new(&pattern).expect("glob must parse");
        let options = WalkerOptions {
            files_only: true,
            ..WalkerOptions::default()
        };
        let mut rx = Walker::spawn_with_options(glob, options);

        let mut got = BTreeSet::new();
        while let Some(msg) = tokio::time::timeout(Duration::from_secs(2), rx.recv())
            .await
            .expect("channel should respond")
        {
            if let Ok(ev) = msg {
                assert_eq!(ev.kind, EntryKind::File);
                got.insert(
                    ev.path
                        .strip_prefix(&root)
                        .expect("path under root")
                        .to_path_buf(),
                );
            }
        }

        let expected: BTreeSet<PathBuf> = ["src/main.rs", "src/bin/tool.rs"]
            .iter()
            .map(PathBuf::from)
            .collect();
        assert_eq!(got, expected);
        let _ = fs::remove_dir_all(&root);
    }

    #[tokio::test]
    #[cfg(all(unix, not(windows)))]
    async fn shard_capacity_does_not_drop_late_directories() {
        let root = test_root("shard_capacity");
        for idx in 0..10usize {
            let dir = root.join(format!("d{idx:02}"));
            fs::create_dir_all(&dir).expect("create dir");
            fs::write(dir.join("file.txt"), b"x").expect("write file");
        }

        let pattern = format!("{}/**/file.txt", root.display());
        let glob = CompiledGlob::new(&pattern).expect("glob must parse");
        let options = WalkerOptions {
            ..WalkerOptions::default()
        };
        let mut rx = Walker::spawn_with_options(glob, options);

        let mut got = BTreeSet::new();
        while let Some(msg) = tokio::time::timeout(Duration::from_secs(2), rx.recv())
            .await
            .expect("channel should respond")
        {
            if let Ok(ev) = msg {
                got.insert(
                    ev.path
                        .strip_prefix(&root)
                        .expect("path under root")
                        .to_path_buf(),
                );
            }
        }

        let expected: BTreeSet<PathBuf> = (0..10usize)
            .map(|idx| PathBuf::from(format!("d{idx:02}/file.txt")))
            .collect();
        assert_eq!(got, expected);
        let _ = fs::remove_dir_all(&root);
    }

    #[tokio::test]
    #[cfg(all(unix, not(windows)))]
    async fn dropping_receiver_terminates_run_promptly() {
        let root = test_root("drop_rx");
        fs::create_dir_all(root.join("d1/d2/d3")).expect("create tree");
        for idx in 0..200usize {
            fs::write(root.join(format!("d1/d2/d3/file-{idx}.txt")), b"x").expect("write file");
        }

        let glob = CompiledGlob::new(&format!("{}/**", root.display())).expect("glob must parse");
        for _ in 0..40usize {
            let rx = Walker::spawn(glob.clone());
            drop(rx);
        }

        // If background tasks fail to terminate, this timeout tends to trip.
        tokio::time::timeout(std::time::Duration::from_secs(2), async {
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        })
        .await
        .expect("drop should not stall runtime");

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

    #[tokio::test]
    #[cfg(all(unix, not(windows)))]
    async fn repeated_spawn_with_completion_is_stable() {
        let root = test_root("repeat_spawn");
        fs::create_dir_all(root.join("src/bin")).expect("create tree");
        fs::write(root.join("src/main.rs"), b"fn main(){}").expect("write file");
        fs::write(root.join("src/bin/tool.rs"), b"fn main(){}").expect("write file");
        let glob =
            CompiledGlob::new(&format!("{}/**/*.rs", root.display())).expect("glob must parse");

        for _ in 0..30usize {
            let mut rx = Walker::spawn(glob.clone());
            let mut files = 0usize;
            while let Some(msg) = tokio::time::timeout(Duration::from_secs(2), rx.recv())
                .await
                .expect("channel should respond")
            {
                if let Ok(ev) = msg
                    && ev.kind == EntryKind::File
                {
                    files += 1;
                }
            }
            assert!(files > 0, "at least one file event should be produced");
        }

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