bashkit 0.1.21

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
//! Directory stack builtins - pushd, popd, dirs
//!
//! Stack stored in variables: _DIRSTACK_SIZE, _DIRSTACK_0, _DIRSTACK_1, etc.

use async_trait::async_trait;
use std::path::PathBuf;

use super::{Builtin, Context};
use crate::error::Result;
use crate::interpreter::ExecResult;

// Security decision: bound mutable `_DIRSTACK_SIZE` to avoid unbounded builtin loops.
const MAX_DIRSTACK_SIZE: usize = 4096;

fn get_stack_size(ctx: &Context<'_>) -> usize {
    ctx.variables
        .get("_DIRSTACK_SIZE")
        .and_then(|s| s.parse().ok())
        .map(|size: usize| size.min(MAX_DIRSTACK_SIZE))
        .unwrap_or(0)
}

fn get_stack_entry(ctx: &Context<'_>, idx: usize) -> Option<String> {
    ctx.variables.get(&format!("_DIRSTACK_{}", idx)).cloned()
}

fn set_stack_size(ctx: &mut Context<'_>, size: usize) {
    ctx.variables
        .insert("_DIRSTACK_SIZE".to_string(), size.to_string());
}

fn push_stack(ctx: &mut Context<'_>, dir: &str) {
    let size = get_stack_size(ctx);
    ctx.variables
        .insert(format!("_DIRSTACK_{}", size), dir.to_string());
    set_stack_size(ctx, size + 1);
}

fn pop_stack(ctx: &mut Context<'_>) -> Option<String> {
    let size = get_stack_size(ctx);
    if size == 0 {
        return None;
    }
    let entry = get_stack_entry(ctx, size - 1);
    ctx.variables.remove(&format!("_DIRSTACK_{}", size - 1));
    set_stack_size(ctx, size - 1);
    entry
}

fn normalize_path(base: &std::path::Path, target: &str) -> PathBuf {
    let path = if target.starts_with('/') {
        PathBuf::from(target)
    } else {
        base.join(target)
    };
    super::resolve_path(&PathBuf::from("/"), &path.to_string_lossy())
}

/// The pushd builtin - push directory onto stack and cd.
///
/// Usage: pushd [dir]
///
/// Without args, swaps top two directories.
/// With dir, pushes current dir onto stack and cd to dir.
pub struct Pushd;

#[async_trait]
impl Builtin for Pushd {
    async fn execute(&self, mut ctx: Context<'_>) -> Result<ExecResult> {
        if ctx.args.is_empty() {
            // Swap top two: current dir <-> top of stack
            let top = pop_stack(&mut ctx);
            match top {
                Some(dir) => {
                    let old_cwd = ctx.cwd.to_string_lossy().to_string();
                    let new_path = normalize_path(ctx.cwd, &dir);
                    if ctx.fs.exists(&new_path).await.unwrap_or(false) {
                        push_stack(&mut ctx, &old_cwd);
                        *ctx.cwd = new_path;
                        // Print stack
                        let output = format_stack(&ctx);
                        Ok(ExecResult::ok(format!("{}\n", output)))
                    } else {
                        // Restore stack
                        push_stack(&mut ctx, &dir);
                        Ok(ExecResult::err(
                            format!("pushd: {}: No such file or directory\n", dir),
                            1,
                        ))
                    }
                }
                None => Ok(ExecResult::err(
                    "pushd: no other directory\n".to_string(),
                    1,
                )),
            }
        } else {
            let target = &ctx.args[0].clone();
            let new_path = normalize_path(ctx.cwd, target);

            if ctx.fs.exists(&new_path).await.unwrap_or(false) {
                let meta = ctx.fs.stat(&new_path).await;
                if meta.map(|m| m.file_type.is_dir()).unwrap_or(false) {
                    let old_cwd = ctx.cwd.to_string_lossy().to_string();
                    push_stack(&mut ctx, &old_cwd);
                    *ctx.cwd = new_path;
                    let output = format_stack(&ctx);
                    Ok(ExecResult::ok(format!("{}\n", output)))
                } else {
                    Ok(ExecResult::err(
                        format!("pushd: {}: Not a directory\n", target),
                        1,
                    ))
                }
            } else {
                Ok(ExecResult::err(
                    format!("pushd: {}: No such file or directory\n", target),
                    1,
                ))
            }
        }
    }
}

/// The popd builtin - pop directory from stack and cd.
///
/// Usage: popd
///
/// Removes top directory from stack and cd to it.
pub struct Popd;

#[async_trait]
impl Builtin for Popd {
    async fn execute(&self, mut ctx: Context<'_>) -> Result<ExecResult> {
        match pop_stack(&mut ctx) {
            Some(dir) => {
                let new_path = normalize_path(ctx.cwd, &dir);
                *ctx.cwd = new_path;
                let output = format_stack(&ctx);
                Ok(ExecResult::ok(format!("{}\n", output)))
            }
            None => Ok(ExecResult::err(
                "popd: directory stack empty\n".to_string(),
                1,
            )),
        }
    }
}

/// The dirs builtin - display directory stack.
///
/// Usage: dirs [-c] [-l] [-p] [-v]
///
/// -c: clear the stack
/// -l: long listing (no ~ substitution)
/// -p: one entry per line
/// -v: numbered one entry per line
pub struct Dirs;

#[async_trait]
impl Builtin for Dirs {
    async fn execute(&self, mut ctx: Context<'_>) -> Result<ExecResult> {
        let mut clear = false;
        let mut per_line = false;
        let mut verbose = false;

        for arg in ctx.args.iter() {
            match arg.as_str() {
                "-c" => clear = true,
                "-p" => per_line = true,
                "-v" => {
                    verbose = true;
                    per_line = true;
                }
                "-l" => {} // long listing (we don't do ~ substitution anyway)
                _ => {}
            }
        }

        if clear {
            let size = get_stack_size(&ctx);
            for i in 0..size {
                ctx.variables.remove(&format!("_DIRSTACK_{}", i));
            }
            set_stack_size(&mut ctx, 0);
            return Ok(ExecResult::ok(String::new()));
        }

        let cwd = ctx.cwd.to_string_lossy().to_string();
        let size = get_stack_size(&ctx);

        if verbose {
            let mut output = format!(" 0  {}\n", cwd);
            for i in (0..size).rev() {
                if let Some(dir) = get_stack_entry(&ctx, i) {
                    output.push_str(&format!(" {}  {}\n", size - i, dir));
                }
            }
            Ok(ExecResult::ok(output))
        } else if per_line {
            let mut output = format!("{}\n", cwd);
            for i in (0..size).rev() {
                if let Some(dir) = get_stack_entry(&ctx, i) {
                    output.push_str(&format!("{}\n", dir));
                }
            }
            Ok(ExecResult::ok(output))
        } else {
            let output = format_stack(&ctx);
            Ok(ExecResult::ok(format!("{}\n", output)))
        }
    }
}

fn format_stack(ctx: &Context<'_>) -> String {
    let cwd = ctx.cwd.to_string_lossy().to_string();
    let size = get_stack_size(ctx);
    let mut parts = vec![cwd];
    for i in (0..size).rev() {
        if let Some(dir) = get_stack_entry(ctx, i) {
            parts.push(dir);
        }
    }
    parts.join(" ")
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use std::path::Path;
    use std::sync::Arc;

    use crate::fs::{FileSystem, InMemoryFs};

    async fn setup() -> (Arc<InMemoryFs>, PathBuf, HashMap<String, String>) {
        let fs = Arc::new(InMemoryFs::new());
        let cwd = PathBuf::from("/home/user");
        let variables = HashMap::new();
        fs.mkdir(&cwd, true).await.unwrap();
        fs.mkdir(Path::new("/tmp"), true).await.unwrap();
        fs.mkdir(Path::new("/var"), true).await.unwrap();
        (fs, cwd, variables)
    }

    // ==================== pushd ====================

    #[tokio::test]
    async fn pushd_to_directory() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();
        let args = vec!["/tmp".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Pushd.execute(ctx).await.unwrap();
        assert_eq!(result.exit_code, 0);
        assert_eq!(cwd, PathBuf::from("/tmp"));
        // Stack should have old cwd
        assert_eq!(variables.get("_DIRSTACK_0").unwrap(), "/home/user");
    }

    #[tokio::test]
    async fn pushd_nonexistent_dir() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();
        let args = vec!["/nonexistent".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Pushd.execute(ctx).await.unwrap();
        assert_eq!(result.exit_code, 1);
        assert!(result.stderr.contains("No such file or directory"));
        // cwd unchanged
        assert_eq!(cwd, PathBuf::from("/home/user"));
    }

    #[tokio::test]
    async fn pushd_file_not_dir() {
        let (fs, mut cwd, mut variables) = setup().await;
        fs.write_file(Path::new("/home/user/file.txt"), b"data")
            .await
            .unwrap();
        let env = HashMap::new();
        let args = vec!["file.txt".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Pushd.execute(ctx).await.unwrap();
        assert_eq!(result.exit_code, 1);
        assert!(result.stderr.contains("Not a directory"));
    }

    #[tokio::test]
    async fn pushd_no_args_empty_stack() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();
        let args: Vec<String> = vec![];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Pushd.execute(ctx).await.unwrap();
        assert_eq!(result.exit_code, 1);
        assert!(result.stderr.contains("no other directory"));
    }

    #[tokio::test]
    async fn pushd_no_args_swaps_top() {
        let (fs, mut cwd, mut variables) = setup().await;
        // Push /tmp first so stack has an entry
        let env = HashMap::new();
        let args = vec!["/tmp".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        Pushd.execute(ctx).await.unwrap();
        assert_eq!(cwd, PathBuf::from("/tmp"));

        // Now pushd with no args should swap
        let args: Vec<String> = vec![];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Pushd.execute(ctx).await.unwrap();
        assert_eq!(result.exit_code, 0);
        assert_eq!(cwd, PathBuf::from("/home/user"));
    }

    // ==================== popd ====================

    #[tokio::test]
    async fn popd_empty_stack() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();
        let args: Vec<String> = vec![];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Popd.execute(ctx).await.unwrap();
        assert_eq!(result.exit_code, 1);
        assert!(result.stderr.contains("directory stack empty"));
    }

    #[tokio::test]
    async fn popd_after_pushd() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();

        // pushd /tmp
        let args = vec!["/tmp".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        Pushd.execute(ctx).await.unwrap();
        assert_eq!(cwd, PathBuf::from("/tmp"));

        // popd
        let args: Vec<String> = vec![];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Popd.execute(ctx).await.unwrap();
        assert_eq!(result.exit_code, 0);
        assert_eq!(cwd, PathBuf::from("/home/user"));
    }

    #[tokio::test]
    async fn pushd_popd_multiple() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();

        // pushd /tmp
        let args = vec!["/tmp".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        Pushd.execute(ctx).await.unwrap();

        // pushd /var
        let args = vec!["/var".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        Pushd.execute(ctx).await.unwrap();
        assert_eq!(cwd, PathBuf::from("/var"));

        // popd -> /tmp
        let args: Vec<String> = vec![];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        Popd.execute(ctx).await.unwrap();
        assert_eq!(cwd, PathBuf::from("/tmp"));

        // popd -> /home/user
        let args: Vec<String> = vec![];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        Popd.execute(ctx).await.unwrap();
        assert_eq!(cwd, PathBuf::from("/home/user"));
    }

    // ==================== dirs ====================

    #[tokio::test]
    async fn dirs_empty_stack() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();
        let args: Vec<String> = vec![];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Dirs.execute(ctx).await.unwrap();
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("/home/user"));
    }

    #[tokio::test]
    async fn dirs_after_pushd() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();

        // pushd /tmp
        let args = vec!["/tmp".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        Pushd.execute(ctx).await.unwrap();

        // dirs
        let args: Vec<String> = vec![];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Dirs.execute(ctx).await.unwrap();
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.contains("/tmp"));
        assert!(result.stdout.contains("/home/user"));
    }

    #[tokio::test]
    async fn dirs_clear() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();

        // pushd /tmp
        let args = vec!["/tmp".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        Pushd.execute(ctx).await.unwrap();

        // dirs -c
        let args = vec!["-c".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Dirs.execute(ctx).await.unwrap();
        assert_eq!(result.exit_code, 0);
        assert_eq!(get_stack_size_from_vars(&variables), 0);
    }

    #[tokio::test]
    async fn dirs_per_line() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();

        // pushd /tmp
        let args = vec!["/tmp".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        Pushd.execute(ctx).await.unwrap();

        // dirs -p
        let args = vec!["-p".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Dirs.execute(ctx).await.unwrap();
        let lines: Vec<&str> = result.stdout.lines().collect();
        assert_eq!(lines.len(), 2);
    }

    #[tokio::test]
    async fn dirs_verbose() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();

        // pushd /tmp
        let args = vec!["/tmp".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        Pushd.execute(ctx).await.unwrap();

        // dirs -v
        let args = vec!["-v".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Dirs.execute(ctx).await.unwrap();
        // Verbose format has numbered entries
        assert!(result.stdout.contains(" 0  "));
        assert!(result.stdout.contains(" 1  "));
    }

    #[tokio::test]
    async fn dirs_limits_user_declared_size() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();
        variables.insert("_DIRSTACK_SIZE".to_string(), "999999999999".to_string());
        variables.insert("_DIRSTACK_0".to_string(), "/tmp".to_string());

        let args = vec!["-p".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Dirs.execute(ctx).await.unwrap();
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "/home/user\n/tmp\n");
    }

    fn get_stack_size_from_vars(vars: &HashMap<String, String>) -> usize {
        vars.get("_DIRSTACK_SIZE")
            .and_then(|s| s.parse().ok())
            .unwrap_or(0)
    }
}