rust-bash 0.3.0

A sandboxed bash interpreter for AI Agents with a virtual filesystem
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
//! MountableFs — composite filesystem that delegates to different backends
//! based on longest-prefix mount point matching.
//!
//! Each mount point maps an absolute path to a `VirtualFs` backend. When an
//! operation arrives, MountableFs finds the longest mount prefix that matches
//! the path, strips the prefix, re-roots the remainder as absolute, and
//! delegates to that backend.
//!
//! Mounting at `"/"` provides a default fallback for all paths.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use crate::platform::SystemTime;

use parking_lot::RwLock;

use super::{DirEntry, Metadata, NodeType, VirtualFs};
use crate::error::VfsError;
use crate::interpreter::pattern::glob_match;

/// Result of resolving two paths to their respective mounts.
struct MountPair {
    src_fs: Arc<dyn VirtualFs>,
    src_rel: PathBuf,
    dst_fs: Arc<dyn VirtualFs>,
    dst_rel: PathBuf,
    same: bool,
}

/// A composite filesystem that delegates to mounted backends via longest-prefix
/// matching.
///
/// # Example
///
/// ```ignore
/// use rust_bash::{RustBashBuilder, InMemoryFs, MountableFs, OverlayFs};
/// use std::sync::Arc;
///
/// let mountable = MountableFs::new()
///     .mount("/", Arc::new(InMemoryFs::new()))
///     .mount("/project", Arc::new(OverlayFs::new("./myproject").unwrap()));
///
/// let mut shell = RustBashBuilder::new()
///     .fs(Arc::new(mountable))
///     .cwd("/")
///     .build()
///     .unwrap();
/// ```
pub struct MountableFs {
    mounts: Arc<RwLock<BTreeMap<PathBuf, Arc<dyn VirtualFs>>>>,
}

impl MountableFs {
    /// Create an empty MountableFs with no mount points.
    pub fn new() -> Self {
        Self {
            mounts: Arc::new(RwLock::new(BTreeMap::new())),
        }
    }

    /// Mount a filesystem backend at the given absolute path.
    ///
    /// Paths must be absolute. Mounting at `"/"` provides the default fallback.
    /// Later mounts at the same path replace earlier ones.
    pub fn mount(self, path: impl Into<PathBuf>, fs: Arc<dyn VirtualFs>) -> Self {
        let path = path.into();
        assert!(
            super::vfs_path_is_absolute(&path),
            "mount path must be absolute: {path:?}"
        );
        self.mounts.write().insert(path, fs);
        self
    }

    /// Find the mount that owns the given path.
    ///
    /// Returns the mount's filesystem and the path relative to the mount point,
    /// re-rooted as absolute (prepended with `/`).
    ///
    /// BTreeMap sorts lexicographically, so `/project/src` > `/project`.
    /// Iterating in reverse gives longest-prefix first.
    fn resolve_mount(&self, path: &Path) -> Result<(Arc<dyn VirtualFs>, PathBuf), VfsError> {
        let mounts = self.mounts.read();
        for (mount_point, fs) in mounts.iter().rev() {
            if path.starts_with(mount_point) {
                let relative = path.strip_prefix(mount_point).unwrap_or(Path::new(""));
                let resolved = if relative.as_os_str().is_empty() {
                    PathBuf::from("/")
                } else {
                    PathBuf::from("/").join(relative)
                };
                return Ok((Arc::clone(fs), resolved));
            }
        }
        Err(VfsError::NotFound(path.to_path_buf()))
    }

    /// Resolve mount for two paths (used by copy/rename/hardlink).
    fn resolve_two(&self, src: &Path, dst: &Path) -> Result<MountPair, VfsError> {
        let mounts = self.mounts.read();
        let resolve_one =
            |path: &Path| -> Result<(Arc<dyn VirtualFs>, PathBuf, PathBuf), VfsError> {
                for (mount_point, fs) in mounts.iter().rev() {
                    if path.starts_with(mount_point) {
                        let relative = path.strip_prefix(mount_point).unwrap_or(Path::new(""));
                        let resolved = if relative.as_os_str().is_empty() {
                            PathBuf::from("/")
                        } else {
                            PathBuf::from("/").join(relative)
                        };
                        return Ok((Arc::clone(fs), resolved, mount_point.clone()));
                    }
                }
                Err(VfsError::NotFound(path.to_path_buf()))
            };

        let (src_fs, src_rel, src_mount) = resolve_one(src)?;
        let (dst_fs, dst_rel, dst_mount) = resolve_one(dst)?;
        let same = src_mount == dst_mount;
        Ok(MountPair {
            src_fs,
            src_rel,
            dst_fs,
            dst_rel,
            same,
        })
    }

    /// Collect synthetic directory entries from mount points that are direct
    /// children of `dir_path`. For example, if mounts exist at `/project` and
    /// `/project/src`, listing `/` should include `project` and listing
    /// `/project` should include `src`.
    fn synthetic_mount_entries(&self, dir_path: &Path) -> Vec<DirEntry> {
        let mounts = self.mounts.read();
        let mut entries = Vec::new();
        let dir_str = dir_path.to_string_lossy();
        let prefix = if dir_str == "/" {
            "/".to_string()
        } else {
            format!("{}/", dir_str.trim_end_matches('/'))
        };

        for mount_point in mounts.keys() {
            // Skip the mount if it IS the directory itself.
            if mount_point == dir_path {
                continue;
            }
            let mp_str = mount_point.to_string_lossy();
            if let Some(rest) = mp_str.strip_prefix(&prefix)
                && !rest.is_empty()
            {
                // Take only the first path component (handles deep mounts
                // like /a/b/c when listing /a).
                let first_component = rest.split('/').next().unwrap();
                if !entries.iter().any(|e: &DirEntry| e.name == first_component) {
                    entries.push(DirEntry {
                        name: first_component.to_string(),
                        node_type: NodeType::Directory,
                    });
                }
            }
        }
        entries
    }

    /// Recursive glob walker that spans mount boundaries.
    fn glob_walk(
        &self,
        dir: &Path,
        components: &[&str],
        current_path: PathBuf,
        results: &mut Vec<PathBuf>,
        max: usize,
    ) {
        if results.len() >= max || components.is_empty() {
            if components.is_empty() {
                results.push(current_path);
            }
            return;
        }

        let pattern = components[0];
        let rest = &components[1..];

        // Get entries from the mounted fs (if any) merged with synthetic mount entries.
        let entries = self.merged_readdir_for_glob(dir);

        if pattern == "**" {
            // Zero directories — advance past **
            self.glob_walk(dir, rest, current_path.clone(), results, max);

            for entry in &entries {
                if results.len() >= max {
                    return;
                }
                if entry.name.starts_with('.') {
                    continue;
                }
                let child_path = current_path.join(&entry.name);
                let child_dir = dir.join(&entry.name);
                if entry.node_type == NodeType::Directory || entry.node_type == NodeType::Symlink {
                    self.glob_walk(&child_dir, components, child_path, results, max);
                }
            }
        } else {
            for entry in &entries {
                if results.len() >= max {
                    return;
                }
                if entry.name.starts_with('.') && !pattern.starts_with('.') {
                    continue;
                }
                if glob_match(pattern, &entry.name) {
                    let child_path = current_path.join(&entry.name);
                    let child_dir = dir.join(&entry.name);
                    if rest.is_empty() {
                        results.push(child_path);
                    } else if entry.node_type == NodeType::Directory
                        || entry.node_type == NodeType::Symlink
                    {
                        self.glob_walk(&child_dir, rest, child_path, results, max);
                    }
                }
            }
        }
    }

    /// Get directory entries for glob walking: real entries from the mount
    /// merged with synthetic mount-point entries.
    fn merged_readdir_for_glob(&self, dir: &Path) -> Vec<DirEntry> {
        let mut entries = match self.resolve_mount(dir) {
            Ok((fs, rel)) => fs.readdir(&rel).unwrap_or_default(),
            Err(_) => Vec::new(),
        };

        // Add synthetic entries for child mount points.
        let synthetics = self.synthetic_mount_entries(dir);
        let existing_names: std::collections::HashSet<String> =
            entries.iter().map(|e| e.name.clone()).collect();
        for s in synthetics {
            if !existing_names.contains(&s.name) {
                entries.push(s);
            }
        }
        entries
    }
}

impl Default for MountableFs {
    fn default() -> Self {
        Self::new()
    }
}

// ---------------------------------------------------------------------------
// VirtualFs implementation
// ---------------------------------------------------------------------------

impl VirtualFs for MountableFs {
    fn read_file(&self, path: &Path) -> Result<Vec<u8>, VfsError> {
        let (fs, rel) = self.resolve_mount(path)?;
        fs.read_file(&rel)
    }

    fn write_file(&self, path: &Path, content: &[u8]) -> Result<(), VfsError> {
        let (fs, rel) = self.resolve_mount(path)?;
        fs.write_file(&rel, content)
    }

    fn append_file(&self, path: &Path, content: &[u8]) -> Result<(), VfsError> {
        let (fs, rel) = self.resolve_mount(path)?;
        fs.append_file(&rel, content)
    }

    fn remove_file(&self, path: &Path) -> Result<(), VfsError> {
        let (fs, rel) = self.resolve_mount(path)?;
        fs.remove_file(&rel)
    }

    fn mkdir(&self, path: &Path) -> Result<(), VfsError> {
        let (fs, rel) = self.resolve_mount(path)?;
        fs.mkdir(&rel)
    }

    fn mkdir_p(&self, path: &Path) -> Result<(), VfsError> {
        let (fs, rel) = self.resolve_mount(path)?;
        fs.mkdir_p(&rel)
    }

    fn readdir(&self, path: &Path) -> Result<Vec<DirEntry>, VfsError> {
        // Track whether the underlying mount confirmed this directory exists.
        let (mut entries, mount_ok) = match self.resolve_mount(path) {
            Ok((fs, rel)) => match fs.readdir(&rel) {
                Ok(e) => (e, true),
                Err(_) => (Vec::new(), false),
            },
            Err(_) => (Vec::new(), false),
        };

        // Merge in synthetic entries from child mount points.
        let synthetics = self.synthetic_mount_entries(path);
        let existing_names: std::collections::HashSet<String> =
            entries.iter().map(|e| e.name.clone()).collect();
        for s in synthetics {
            if !existing_names.contains(&s.name) {
                entries.push(s);
            }
        }

        // Only return NotFound when the mount itself errored AND there are no
        // synthetic entries from child mounts. An empty directory that the
        // mount confirmed is legitimate.
        if !mount_ok && entries.is_empty() {
            return Err(VfsError::NotFound(path.to_path_buf()));
        }
        Ok(entries)
    }

    fn remove_dir(&self, path: &Path) -> Result<(), VfsError> {
        let (fs, rel) = self.resolve_mount(path)?;
        fs.remove_dir(&rel)
    }

    fn remove_dir_all(&self, path: &Path) -> Result<(), VfsError> {
        let (fs, rel) = self.resolve_mount(path)?;
        fs.remove_dir_all(&rel)
    }

    fn exists(&self, path: &Path) -> bool {
        // A path exists if its owning mount says so, OR if it is itself a
        // mount point (mount points are treated as existing directories).
        if let Ok((fs, rel)) = self.resolve_mount(path)
            && fs.exists(&rel)
        {
            return true;
        }
        // Check if this exact path is a mount point.
        let mounts = self.mounts.read();
        if mounts.contains_key(path) {
            return true;
        }
        // Check if any mount is a descendant (making this a synthetic parent).
        let prefix = if path == Path::new("/") {
            "/".to_string()
        } else {
            format!("{}/", path.to_string_lossy().trim_end_matches('/'))
        };
        mounts
            .keys()
            .any(|mp| mp.to_string_lossy().starts_with(&prefix))
    }

    fn stat(&self, path: &Path) -> Result<Metadata, VfsError> {
        // Try the owning mount first.
        if let Ok((fs, rel)) = self.resolve_mount(path)
            && let Ok(m) = fs.stat(&rel)
        {
            return Ok(m);
        }
        // If this path is a mount point or has child mounts, return synthetic
        // directory metadata.
        if self.is_mount_point_or_ancestor(path) {
            return Ok(Metadata {
                node_type: NodeType::Directory,
                size: 0,
                mode: 0o755,
                mtime: SystemTime::UNIX_EPOCH,
            });
        }
        Err(VfsError::NotFound(path.to_path_buf()))
    }

    fn lstat(&self, path: &Path) -> Result<Metadata, VfsError> {
        if let Ok((fs, rel)) = self.resolve_mount(path)
            && let Ok(m) = fs.lstat(&rel)
        {
            return Ok(m);
        }
        if self.is_mount_point_or_ancestor(path) {
            return Ok(Metadata {
                node_type: NodeType::Directory,
                size: 0,
                mode: 0o755,
                mtime: SystemTime::UNIX_EPOCH,
            });
        }
        Err(VfsError::NotFound(path.to_path_buf()))
    }

    fn chmod(&self, path: &Path, mode: u32) -> Result<(), VfsError> {
        let (fs, rel) = self.resolve_mount(path)?;
        fs.chmod(&rel, mode)
    }

    fn utimes(&self, path: &Path, mtime: SystemTime) -> Result<(), VfsError> {
        let (fs, rel) = self.resolve_mount(path)?;
        fs.utimes(&rel, mtime)
    }

    fn symlink(&self, target: &Path, link: &Path) -> Result<(), VfsError> {
        let (link_fs, link_rel) = self.resolve_mount(link)?;
        // If the target is absolute and resolves to the same mount as the link,
        // remap it into the mount's namespace so the underlying FS can follow it.
        let remapped_target = if target.is_absolute() {
            if let Ok((_, target_rel)) = self.resolve_mount(target) {
                // Find mount point for the link to compare
                let link_mount = self.mount_point_for(link);
                let target_mount = self.mount_point_for(target);
                if link_mount == target_mount {
                    target_rel
                } else {
                    target.to_path_buf()
                }
            } else {
                target.to_path_buf()
            }
        } else {
            target.to_path_buf()
        };
        link_fs.symlink(&remapped_target, &link_rel)
    }

    fn hardlink(&self, src: &Path, dst: &Path) -> Result<(), VfsError> {
        let pair = self.resolve_two(src, dst)?;
        if !pair.same {
            return Err(VfsError::IoError(
                "hard links across mount boundaries are not supported".to_string(),
            ));
        }
        pair.src_fs.hardlink(&pair.src_rel, &pair.dst_rel)
    }

    fn readlink(&self, path: &Path) -> Result<PathBuf, VfsError> {
        let (fs, rel) = self.resolve_mount(path)?;
        let target = fs.readlink(&rel)?;
        // If the target is absolute and the link lives at a non-root mount,
        // remap the target back to the global namespace.
        if target.is_absolute() {
            let mount_point = self.mount_point_for(path);
            if mount_point != Path::new("/") {
                let inner_rel = target.strip_prefix("/").unwrap_or(&target);
                if inner_rel.as_os_str().is_empty() {
                    return Ok(mount_point);
                }
                return Ok(mount_point.join(inner_rel));
            }
        }
        Ok(target)
    }

    fn canonicalize(&self, path: &Path) -> Result<PathBuf, VfsError> {
        let (fs, rel) = self.resolve_mount(path)?;
        let canonical_in_mount = fs.canonicalize(&rel)?;
        // Re-root back to global namespace: find what mount we used, prepend
        // the mount point.
        let mounts = self.mounts.read();
        for (mount_point, _) in mounts.iter().rev() {
            if path.starts_with(mount_point) {
                if mount_point == Path::new("/") {
                    return Ok(canonical_in_mount);
                }
                let inner_rel = canonical_in_mount
                    .strip_prefix("/")
                    .unwrap_or(&canonical_in_mount);
                if inner_rel.as_os_str().is_empty() {
                    return Ok(mount_point.clone());
                }
                return Ok(mount_point.join(inner_rel));
            }
        }
        Ok(canonical_in_mount)
    }

    fn copy(&self, src: &Path, dst: &Path) -> Result<(), VfsError> {
        let pair = self.resolve_two(src, dst)?;
        if pair.same {
            pair.src_fs.copy(&pair.src_rel, &pair.dst_rel)
        } else {
            let content = pair.src_fs.read_file(&pair.src_rel)?;
            pair.dst_fs.write_file(&pair.dst_rel, &content)
        }
    }

    fn rename(&self, src: &Path, dst: &Path) -> Result<(), VfsError> {
        let pair = self.resolve_two(src, dst)?;
        if pair.same {
            pair.src_fs.rename(&pair.src_rel, &pair.dst_rel)
        } else {
            // Check if source is a directory — cross-mount directory rename
            // is not supported (would need recursive copy).
            if let Ok(m) = pair.src_fs.stat(&pair.src_rel)
                && m.node_type == NodeType::Directory
            {
                return Err(VfsError::IoError(
                    "rename of directories across mount boundaries is not supported".to_string(),
                ));
            }
            let content = pair.src_fs.read_file(&pair.src_rel)?;
            pair.dst_fs.write_file(&pair.dst_rel, &content)?;
            pair.src_fs.remove_file(&pair.src_rel)
        }
    }

    // TODO: MountableFs::glob does not yet honor GlobOptions (dotglob, nocaseglob, globstar).
    // Its glob_walk traversal needs refactoring to accept options.
    fn glob(&self, pattern: &str, cwd: &Path) -> Result<Vec<PathBuf>, VfsError> {
        let is_absolute = pattern.starts_with('/');
        let abs_pattern = if is_absolute {
            pattern.to_string()
        } else {
            let cwd_str = cwd.to_str().unwrap_or("/").trim_end_matches('/');
            format!("{cwd_str}/{pattern}")
        };

        let components: Vec<&str> = abs_pattern.split('/').filter(|s| !s.is_empty()).collect();
        let mut results = Vec::new();
        let max = 100_000;
        self.glob_walk(
            Path::new("/"),
            &components,
            PathBuf::from("/"),
            &mut results,
            max,
        );

        results.sort();
        results.dedup();

        if !is_absolute {
            results = results
                .into_iter()
                .filter_map(|p| p.strip_prefix(cwd).ok().map(|r| r.to_path_buf()))
                .collect();
        }

        Ok(results)
    }

    fn deep_clone(&self) -> Arc<dyn VirtualFs> {
        let mounts = self.mounts.read();
        let cloned_mounts: BTreeMap<PathBuf, Arc<dyn VirtualFs>> = mounts
            .iter()
            .map(|(path, fs)| (path.clone(), fs.deep_clone()))
            .collect();
        Arc::new(MountableFs {
            mounts: Arc::new(RwLock::new(cloned_mounts)),
        })
    }
}

// ---------------------------------------------------------------------------
// Private helpers
// ---------------------------------------------------------------------------

impl MountableFs {
    /// Returns true if `path` is a mount point or an ancestor of one.
    fn is_mount_point_or_ancestor(&self, path: &Path) -> bool {
        let mounts = self.mounts.read();
        if mounts.contains_key(path) {
            return true;
        }
        let prefix = if path == Path::new("/") {
            "/".to_string()
        } else {
            format!("{}/", path.to_string_lossy().trim_end_matches('/'))
        };
        mounts
            .keys()
            .any(|mp| mp.to_string_lossy().starts_with(&prefix))
    }

    /// Return the mount point that owns `path` (longest-prefix match).
    fn mount_point_for(&self, path: &Path) -> PathBuf {
        let mounts = self.mounts.read();
        for mount_point in mounts.keys().rev() {
            if path.starts_with(mount_point) {
                return mount_point.clone();
            }
        }
        PathBuf::from("/")
    }
}