bashkit 0.8.0

Awesomely fast virtual sandbox with bash and file system
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
//! find builtin - search for files.

#![allow(clippy::unwrap_used)]

use async_trait::async_trait;
use std::path::Path;

use super::glob_match;
use crate::builtins::{Builtin, Context, ExecutionPlan, SubCommand, resolve_path};
use crate::error::Result;
use crate::interpreter::{ControlFlow, ExecResult};

/// Options for find command
pub(super) struct FindOptions {
    pub(super) name_pattern: Option<String>,
    /// -path pattern: match against the full display path
    pub(super) path_pattern: Option<String>,
    pub(super) type_filter: Option<char>,
    pub(super) max_depth: Option<usize>,
    pub(super) min_depth: Option<usize>,
    pub(super) printf_format: Option<String>,
    /// -exec/-execdir command template (args before \; or +)
    pub(super) exec_args: Vec<String>,
    /// true if -exec uses + (batch mode), false for \; (per-file mode)
    pub(super) exec_batch: bool,
    /// Negate the -name predicate
    pub(super) negate_name: bool,
    /// Negate the -path predicate
    pub(super) negate_path: bool,
}

/// The find builtin - search for files.
///
/// Usage: find [PATH...] [-name PATTERN] [-type TYPE] [-maxdepth N] [-mindepth N] [-printf FMT] [-exec CMD {} \;]
///
/// Options:
///   -name PATTERN      Match filename against PATTERN (supports * and ?)
///   -type TYPE         Match file type: f (file), d (directory), l (link)
///   -maxdepth N        Descend at most N levels
///   -mindepth N        Do not apply tests at levels less than N
///   -print             Print matching paths (default)
///   -printf FMT        Print using format string (%f %p %P %s %m %M %y %d %T@)
///   -exec CMD {} \;    Execute CMD for each match ({} = path)
///   -exec CMD {} +     Execute CMD once with all matches
pub struct Find;

/// Parse find arguments into search paths and options.
/// Returns (paths, opts) or an error ExecResult.
#[allow(clippy::result_large_err)]
pub(super) fn parse_find_args(
    args: &[String],
) -> std::result::Result<(Vec<String>, FindOptions), ExecResult> {
    let mut paths: Vec<String> = Vec::new();
    let mut opts = FindOptions {
        name_pattern: None,
        path_pattern: None,
        type_filter: None,
        max_depth: None,
        min_depth: None,
        printf_format: None,
        exec_args: Vec::new(),
        exec_batch: false,
        negate_name: false,
        negate_path: false,
    };
    let mut negate_next = false;

    let mut i = 0;
    while i < args.len() {
        let arg = &args[i];
        match arg.as_str() {
            "-name" => {
                i += 1;
                if i >= args.len() {
                    return Err(ExecResult::err(
                        "find: missing argument to '-name'\n".to_string(),
                        1,
                    ));
                }
                opts.name_pattern = Some(args[i].clone());
                if negate_next {
                    opts.negate_name = true;
                    negate_next = false;
                }
            }
            "-path" => {
                i += 1;
                if i >= args.len() {
                    return Err(ExecResult::err(
                        "find: missing argument to '-path'\n".to_string(),
                        1,
                    ));
                }
                opts.path_pattern = Some(args[i].clone());
                if negate_next {
                    opts.negate_path = true;
                    negate_next = false;
                }
            }
            "-type" => {
                i += 1;
                if i >= args.len() {
                    return Err(ExecResult::err(
                        "find: missing argument to '-type'\n".to_string(),
                        1,
                    ));
                }
                let t = &args[i];
                match t.as_str() {
                    "f" | "d" | "l" => opts.type_filter = Some(t.chars().next().unwrap()),
                    _ => {
                        return Err(ExecResult::err(format!("find: unknown type '{}'\n", t), 1));
                    }
                }
            }
            "-maxdepth" => {
                i += 1;
                if i >= args.len() {
                    return Err(ExecResult::err(
                        "find: missing argument to '-maxdepth'\n".to_string(),
                        1,
                    ));
                }
                match args[i].parse::<usize>() {
                    Ok(n) => opts.max_depth = Some(n),
                    Err(_) => {
                        return Err(ExecResult::err(
                            format!("find: invalid maxdepth value '{}'\n", args[i]),
                            1,
                        ));
                    }
                }
            }
            "-mindepth" => {
                i += 1;
                if i >= args.len() {
                    return Err(ExecResult::err(
                        "find: missing argument to '-mindepth'\n".to_string(),
                        1,
                    ));
                }
                match args[i].parse::<usize>() {
                    Ok(n) => opts.min_depth = Some(n),
                    Err(_) => {
                        return Err(ExecResult::err(
                            format!("find: invalid mindepth value '{}'\n", args[i]),
                            1,
                        ));
                    }
                }
            }
            "-print" | "-print0" => {
                // Default action, ignore
            }
            "-printf" => {
                i += 1;
                if i >= args.len() {
                    return Err(ExecResult::err(
                        "find: missing argument to '-printf'\n".to_string(),
                        1,
                    ));
                }
                opts.printf_format = Some(args[i].clone());
            }
            "-exec" | "-execdir" => {
                i += 1;
                while i < args.len() {
                    let a = &args[i];
                    if a == ";" || a == "\\;" {
                        break;
                    }
                    if a == "+" {
                        opts.exec_batch = true;
                        break;
                    }
                    opts.exec_args.push(a.clone());
                    i += 1;
                }
            }
            "-not" | "!" => {
                negate_next = true;
            }
            s if s.starts_with('-') => {
                return Err(ExecResult::err(
                    format!("find: unknown predicate '{}'\n", s),
                    1,
                ));
            }
            _ => {
                paths.push(arg.clone());
            }
        }
        i += 1;
    }

    if paths.is_empty() {
        paths.push(".".to_string());
    }

    Ok((paths, opts))
}

pub(super) struct FindPlanData {
    pub(super) matched_paths: Vec<String>,
    pub(super) errors: String,
    pub(super) had_error: bool,
}

/// Collect matched paths and path/traversal errors for find execution planning.
async fn collect_find_plan_data(
    ctx: &Context<'_>,
    search_paths: &[String],
    opts: &FindOptions,
) -> Result<FindPlanData> {
    let mut matched: Vec<String> = Vec::new();
    let mut errors = String::new();
    let mut had_error = false;
    // Reuse find_recursive but with a temporary output buffer
    let temp_opts = FindOptions {
        name_pattern: opts.name_pattern.clone(),
        path_pattern: opts.path_pattern.clone(),
        type_filter: opts.type_filter,
        max_depth: opts.max_depth,
        min_depth: opts.min_depth,
        printf_format: None, // Don't format, just collect paths
        exec_args: Vec::new(),
        exec_batch: false,
        negate_name: opts.negate_name,
        negate_path: opts.negate_path,
    };
    let mut output = String::new();
    for path_str in search_paths {
        let path = resolve_path(ctx.cwd, path_str);
        if !ctx.fs.exists(&path).await.unwrap_or(false) {
            errors.push_str(&format!(
                "find: '{}': No such file or directory\n",
                path_str
            ));
            had_error = true;
            continue;
        }
        if let Err(e) = find_recursive(ctx, &path, path_str, &temp_opts, 0, &mut output).await {
            errors.push_str(&format!("find: '{}': {}\n", path_str, e));
            had_error = true;
        }
    }
    // Parse the output back into paths (each line is a path)
    for line in output.lines() {
        if !line.is_empty() {
            matched.push(line.to_string());
        }
    }
    Ok(FindPlanData {
        matched_paths: matched,
        errors,
        had_error,
    })
}

/// Build exec sub-commands from matched paths and exec_args template.
pub(super) fn build_find_exec_commands(
    exec_args: &[String],
    matched_paths: &[String],
    batch: bool,
) -> Vec<SubCommand> {
    if exec_args.is_empty() || matched_paths.is_empty() {
        return Vec::new();
    }

    if batch {
        // Batch mode: -exec cmd {} +
        // Replace {} with all paths at once
        let cmd_args: Vec<String> = exec_args
            .iter()
            .flat_map(|arg| {
                if arg == "{}" {
                    matched_paths.to_vec()
                } else {
                    vec![arg.clone()]
                }
            })
            .collect();

        if cmd_args.is_empty() {
            return Vec::new();
        }

        vec![SubCommand {
            name: cmd_args[0].clone(),
            args: cmd_args[1..].to_vec(),
            stdin: None,
        }]
    } else {
        // Per-file mode: -exec cmd {} \;
        matched_paths
            .iter()
            .map(|found_path| {
                let cmd_args: Vec<String> = exec_args
                    .iter()
                    .map(|arg| arg.replace("{}", found_path))
                    .collect();

                SubCommand {
                    name: cmd_args[0].clone(),
                    args: cmd_args[1..].to_vec(),
                    stdin: None,
                }
            })
            .collect()
    }
}

#[async_trait]
impl Builtin for Find {
    async fn execute(&self, ctx: Context<'_>) -> Result<ExecResult> {
        if let Some(r) = crate::builtins::check_help_version(
            ctx.args,
            "Usage: find [PATH...] [EXPRESSION]\nSearch for files in a directory hierarchy.\n\n  -name PATTERN\tmatch filename against PATTERN (supports * and ?)\n  -path PATTERN\tmatch full path against PATTERN\n  -type TYPE\tmatch file type: f (file), d (directory), l (link)\n  -maxdepth N\tdescend at most N levels\n  -mindepth N\tdo not apply tests at levels less than N\n  -print\t\tprint matching paths (default)\n  -printf FMT\tprint using format string (%f %p %P %s %m %M %y %d %T@)\n  -exec CMD {} \\;\texecute CMD for each match ({} = path)\n  -exec CMD {} +\texecute CMD once with all matches\n  -not, !\t\tnegate the next predicate\n      --help\tdisplay this help and exit\n      --version\toutput version information and exit\n",
            Some("find (bashkit) 0.1"),
        ) {
            return Ok(r);
        }

        let (search_paths, opts) = match parse_find_args(ctx.args) {
            Ok(v) => v,
            Err(e) => return Ok(e),
        };

        let mut output = String::new();
        let mut errors = String::new();
        let mut had_error = false;

        for path_str in &search_paths {
            let path = resolve_path(ctx.cwd, path_str);
            if !ctx.fs.exists(&path).await.unwrap_or(false) {
                errors.push_str(&format!(
                    "find: '{}': No such file or directory\n",
                    path_str
                ));
                had_error = true;
                continue;
            }

            if let Err(e) = find_recursive(&ctx, &path, path_str, &opts, 0, &mut output).await {
                errors.push_str(&format!("find: '{}': {}\n", path_str, e));
                had_error = true;
            }
        }

        Ok(ExecResult {
            stdout: output,
            stderr: errors,
            exit_code: if had_error { 1 } else { 0 },
            control_flow: ControlFlow::None,
            ..Default::default()
        })
    }

    async fn execution_plan(&self, ctx: &Context<'_>) -> Result<Option<ExecutionPlan>> {
        let (search_paths, opts) = match parse_find_args(ctx.args) {
            Ok(v) => v,
            Err(_) => return Ok(None), // Let execute() handle errors
        };

        // Only return a plan when -exec is present
        if opts.exec_args.is_empty() {
            return Ok(None);
        }

        // Collect matched paths plus any path/traversal errors.
        let plan_data = collect_find_plan_data(ctx, &search_paths, &opts).await?;
        if plan_data.matched_paths.is_empty() && !plan_data.had_error {
            return Ok(None);
        }

        let commands =
            build_find_exec_commands(&opts.exec_args, &plan_data.matched_paths, opts.exec_batch);
        if commands.is_empty() && !plan_data.had_error {
            return Ok(None);
        }

        if plan_data.had_error {
            return Ok(Some(ExecutionPlan::BatchWithStatus {
                commands,
                stderr_prefix: plan_data.errors,
                force_error_exit: true,
            }));
        }

        Ok(Some(ExecutionPlan::Batch { commands }))
    }
}

fn find_recursive<'a>(
    ctx: &'a Context<'_>,
    path: &'a Path,
    display_path: &'a str,
    opts: &'a FindOptions,
    current_depth: usize,
    output: &'a mut String,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + Send + 'a>> {
    Box::pin(async move {
        // Check if this entry matches
        let metadata = ctx.fs.stat(path).await?;
        let entry_name = Path::new(display_path)
            .file_name()
            .map(|s| s.to_string_lossy().to_string())
            .unwrap_or_else(|| display_path.to_string());

        // Check type filter
        let type_matches = match opts.type_filter {
            Some('f') => metadata.file_type.is_file(),
            Some('d') => metadata.file_type.is_dir(),
            Some('l') => metadata.file_type.is_symlink(),
            _ => true,
        };

        // Check name pattern
        let name_matches = match &opts.name_pattern {
            Some(pattern) => {
                let m = glob_match(&entry_name, pattern);
                if opts.negate_name { !m } else { m }
            }
            None => true,
        };

        // Check path pattern
        let path_matches = match &opts.path_pattern {
            Some(pattern) => {
                let m = glob_match(display_path, pattern);
                if opts.negate_path { !m } else { m }
            }
            None => true,
        };

        // Check min depth before outputting
        let above_min_depth = match opts.min_depth {
            Some(min) => current_depth >= min,
            None => true,
        };

        // Output if matches (or if no filters, show everything)
        if type_matches && name_matches && path_matches && above_min_depth {
            if let Some(ref fmt) = opts.printf_format {
                output.push_str(&find_printf_format(fmt, display_path, &metadata));
            } else {
                output.push_str(display_path);
                output.push('\n');
            }
        }

        // Recurse into directories
        if metadata.file_type.is_dir() {
            // Check max depth
            if let Some(max) = opts.max_depth
                && current_depth >= max
            {
                return Ok(());
            }

            let entries = ctx.fs.read_dir(path).await?;
            let mut sorted_entries = entries;
            sorted_entries.sort_by(|a, b| a.name.cmp(&b.name));

            for entry in sorted_entries {
                let child_path = path.join(&entry.name);
                let child_display = if display_path == "." {
                    format!("./{}", entry.name)
                } else {
                    format!("{}/{}", display_path, entry.name)
                };

                find_recursive(
                    ctx,
                    &child_path,
                    &child_display,
                    opts,
                    current_depth + 1,
                    output,
                )
                .await?;
            }
        }

        Ok(())
    })
}

/// Format a path using find's -printf format string.
fn find_printf_format(fmt: &str, display_path: &str, metadata: &crate::fs::Metadata) -> String {
    let mut out = String::new();
    let chars: Vec<char> = fmt.chars().collect();
    let mut i = 0;
    while i < chars.len() {
        match chars[i] {
            '\\' => {
                i += 1;
                if i < chars.len() {
                    match chars[i] {
                        'n' => out.push('\n'),
                        't' => out.push('\t'),
                        '0' => out.push('\0'),
                        '\\' => out.push('\\'),
                        c => {
                            out.push('\\');
                            out.push(c);
                        }
                    }
                }
            }
            '%' => {
                i += 1;
                if i >= chars.len() {
                    out.push('%');
                    continue;
                }
                match chars[i] {
                    'f' => {
                        let name = std::path::Path::new(display_path)
                            .file_name()
                            .map(|s| s.to_string_lossy().to_string())
                            .unwrap_or_else(|| display_path.to_string());
                        out.push_str(&name);
                    }
                    'p' => out.push_str(display_path),
                    'P' => {
                        // In builtin context, display_path is already relative
                        let rel = display_path.strip_prefix("./").unwrap_or(display_path);
                        out.push_str(rel);
                    }
                    's' => out.push_str(&metadata.size.to_string()),
                    'm' => out.push_str(&format!("{:o}", metadata.mode & 0o7777)),
                    'M' => {
                        let type_ch = if metadata.file_type.is_dir() {
                            'd'
                        } else if metadata.file_type.is_symlink() {
                            'l'
                        } else {
                            '-'
                        };
                        out.push(type_ch);
                        for shift in [6, 3, 0] {
                            let bits = (metadata.mode >> shift) & 7;
                            out.push(if bits & 4 != 0 { 'r' } else { '-' });
                            out.push(if bits & 2 != 0 { 'w' } else { '-' });
                            out.push(if bits & 1 != 0 { 'x' } else { '-' });
                        }
                    }
                    'y' => {
                        let ch = if metadata.file_type.is_dir() {
                            'd'
                        } else if metadata.file_type.is_symlink() {
                            'l'
                        } else {
                            'f'
                        };
                        out.push(ch);
                    }
                    'd' => {
                        // Approximate depth from display_path
                        let base = display_path.strip_prefix("./").unwrap_or(display_path);
                        let depth = if base == "." || base.is_empty() {
                            0
                        } else {
                            base.matches('/').count() + 1
                        };
                        out.push_str(&depth.to_string());
                    }
                    'T' => {
                        i += 1;
                        if i < chars.len() && chars[i] == '@' {
                            let secs = metadata
                                .modified
                                .duration_since(std::time::UNIX_EPOCH)
                                .ok()
                                .map(|d| d.as_secs())
                                .unwrap_or(0);
                            out.push_str(&secs.to_string());
                        } else {
                            out.push_str("%T");
                            continue;
                        }
                    }
                    '%' => out.push('%'),
                    c => {
                        out.push('%');
                        out.push(c);
                    }
                }
            }
            c => out.push(c),
        }
        i += 1;
    }
    out
}