use bashkit::{BashBuilder, DirEntry, FileSystem, FileSystemExt, Metadata, Result, ShellFeatures};
use std::io::{Error as IoError, ErrorKind};
use std::path::{Path, PathBuf};
use std::sync::Arc;
type SystemTime = bashkit::time::SystemTime;
pub(crate) fn apply(builder: BashBuilder) -> BashBuilder {
builder
.fs(Arc::new(DisabledFs))
.rootfs(false)
.shell_features(ShellFeatures::none())
.builtin_filter(builtin_allowed)
}
pub(crate) fn builtin_allowed(name: &str) -> bool {
matches!(
name,
"echo"
| "true"
| "false"
| "exit"
| "break"
| "continue"
| "return"
| "test"
| "["
| "printf"
| "export"
| "read"
| "set"
| "unset"
| "shift"
| "local"
| ":"
| "readonly"
| "times"
| "eval"
| "grep"
| "sed"
| "awk"
| "head"
| "tail"
| "sort"
| "uniq"
| "cut"
| "tr"
| "wc"
| "nl"
| "paste"
| "column"
| "comm"
| "strings"
| "tac"
| "rev"
| "fold"
| "expand"
| "unexpand"
| "join"
| "split"
| "jq"
| "seq"
| "expr"
| "bc"
| "numfmt"
| "env"
| "printenv"
| "type"
| "which"
| "hash"
| "alias"
| "unalias"
| "trap"
| "caller"
| "mapfile"
| "readarray"
| "shopt"
| "clear"
| "envsubst"
| "assert"
| "log"
| "retry"
| "semver"
| "verify"
| "compgen"
| "csv"
| "help"
| "iconv"
| "json"
| "parallel"
| "template"
| "tomlq"
| "yq"
| "timeout"
| "xargs"
| "wait"
)
}
pub(crate) struct DisabledFs;
fn disabled_fs_error() -> bashkit::Error {
IoError::new(ErrorKind::PermissionDenied, "filesystem access disabled").into()
}
#[async_trait::async_trait]
impl FileSystemExt for DisabledFs {}
#[async_trait::async_trait]
impl FileSystem for DisabledFs {
async fn read_file(&self, _path: &Path) -> Result<Vec<u8>> {
Err(disabled_fs_error())
}
async fn write_file(&self, _path: &Path, _content: &[u8]) -> Result<()> {
Err(disabled_fs_error())
}
async fn append_file(&self, _path: &Path, _content: &[u8]) -> Result<()> {
Err(disabled_fs_error())
}
async fn mkdir(&self, _path: &Path, _recursive: bool) -> Result<()> {
Err(disabled_fs_error())
}
async fn remove(&self, _path: &Path, _recursive: bool) -> Result<()> {
Err(disabled_fs_error())
}
async fn stat(&self, _path: &Path) -> Result<Metadata> {
Err(disabled_fs_error())
}
async fn read_dir(&self, _path: &Path) -> Result<Vec<DirEntry>> {
Err(disabled_fs_error())
}
async fn exists(&self, _path: &Path) -> Result<bool> {
Ok(false)
}
async fn rename(&self, _from: &Path, _to: &Path) -> Result<()> {
Err(disabled_fs_error())
}
async fn copy(&self, _from: &Path, _to: &Path) -> Result<()> {
Err(disabled_fs_error())
}
async fn symlink(&self, _target: &Path, _link: &Path) -> Result<()> {
Err(disabled_fs_error())
}
async fn read_link(&self, _path: &Path) -> Result<PathBuf> {
Err(disabled_fs_error())
}
async fn chmod(&self, _path: &Path, _mode: u32) -> Result<()> {
Err(disabled_fs_error())
}
async fn set_modified_time(&self, _path: &Path, _time: SystemTime) -> Result<()> {
Err(disabled_fs_error())
}
}