use async_trait::async_trait;
use clap::{CommandFactory, Parser};
use std::path::Path;
use std::time::SystemTime;
use crate::backend::WriteMode;
use crate::interpreter::ExecResult;
use crate::tools::{schema_from_clap, ExecContext, ToolCtx, GlobalFlags, Tool, ToolArgs, ToolSchema};
pub struct Touch;
#[derive(Parser, Debug)]
#[command(name = "touch", about = "Change file timestamps or create empty files")]
struct TouchArgs {
#[command(flatten)]
global: GlobalFlags,
paths: Vec<String>,
}
#[async_trait]
impl Tool for Touch {
fn name(&self) -> &str {
"touch"
}
fn schema(&self) -> ToolSchema {
schema_from_clap(
&TouchArgs::command(),
"touch",
"Change file timestamps or create empty files",
[
("Create empty file", "touch newfile.txt"),
("Update timestamp", "touch existing.txt"),
],
)
}
async fn execute(&self, args: ToolArgs, ctx: &mut dyn ToolCtx) -> ExecResult {
let Some(ctx) = ctx.as_any_mut().downcast_mut::<ExecContext>() else {
return ExecResult::failure(1, "internal error: kernel builtin requires ExecContext");
};
let parsed = match TouchArgs::try_parse_from(
std::iter::once("touch".to_string()).chain(args.to_argv()),
) {
Ok(p) => p,
Err(e) => return ExecResult::failure(2, format!("touch: {e}")),
};
parsed.global.apply(ctx);
if args.positional.is_empty() {
return ExecResult::failure(1, "touch: missing path argument");
}
let mut last_err: Option<String> = None;
for value in &args.positional {
let path_str = crate::interpreter::value_to_string(value);
let resolved = ctx.resolve_path(&path_str);
let path = Path::new(&resolved);
let result = if ctx.backend.exists(path).await {
ctx.backend.set_mtime(path, SystemTime::now()).await
} else {
ctx.backend.write(path, &[], WriteMode::CreateNew).await
};
if let Err(e) = result {
last_err = Some(format!("touch: {}: {}", path_str, e));
}
}
match last_err {
Some(msg) => ExecResult::failure(1, msg),
None => ExecResult::success(""),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ast::Value;
use crate::vfs::{Filesystem, MemoryFs, VfsRouter};
use std::sync::Arc;
async fn make_ctx() -> ExecContext {
let mut vfs = VfsRouter::new();
let mem = MemoryFs::new();
mem.write(Path::new("existing.txt"), b"content")
.await
.unwrap();
vfs.mount("/", mem);
ExecContext::new(Arc::new(vfs))
}
#[tokio::test]
async fn test_touch_create_new() {
let mut ctx = make_ctx().await;
let mut args = ToolArgs::new();
args.positional.push(Value::String("/newfile.txt".into()));
let result = Touch.execute(args, &mut ctx).await;
assert!(result.ok());
assert!(ctx.backend.exists(Path::new("/newfile.txt")).await);
let data = ctx
.backend
.read(Path::new("/newfile.txt"), None)
.await
.unwrap();
assert!(data.is_empty());
}
#[tokio::test]
async fn test_touch_existing() {
let mut ctx = make_ctx().await;
let mut args = ToolArgs::new();
args.positional.push(Value::String("/existing.txt".into()));
let result = Touch.execute(args, &mut ctx).await;
assert!(result.ok());
let data = ctx
.backend
.read(Path::new("/existing.txt"), None)
.await
.unwrap();
assert_eq!(data, b"content");
}
#[tokio::test]
async fn test_touch_missing_path() {
let mut ctx = make_ctx().await;
let result = Touch.execute(ToolArgs::new(), &mut ctx).await;
assert!(!result.ok());
}
#[tokio::test]
async fn test_touch_updates_memory_mtime() {
let mut ctx = make_ctx().await;
let before = ctx
.backend
.stat(Path::new("/existing.txt"))
.await
.unwrap()
.modified
.expect("MemoryFs records mtime");
let past = before - std::time::Duration::from_secs(3600);
ctx.backend
.set_mtime(Path::new("/existing.txt"), past)
.await
.expect("set_mtime via VFS");
let pinned = ctx
.backend
.stat(Path::new("/existing.txt"))
.await
.unwrap()
.modified
.unwrap();
assert_eq!(pinned, past, "set_mtime did not record the timestamp");
let mut args = ToolArgs::new();
args.positional.push(Value::String("/existing.txt".into()));
let result = Touch.execute(args, &mut ctx).await;
assert!(result.ok());
let after = ctx
.backend
.stat(Path::new("/existing.txt"))
.await
.unwrap()
.modified
.unwrap();
assert!(after > past, "touch did not advance the mtime");
}
#[cfg(feature = "localfs")]
#[tokio::test]
async fn test_touch_existing_readonly_rejects() {
use crate::vfs::LocalFs;
use std::sync::Arc;
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("ro.txt"), b"x").unwrap();
let mut vfs = VfsRouter::new();
vfs.mount("/", LocalFs::read_only(dir.path().to_path_buf()));
let mut ctx = ExecContext::new(Arc::new(vfs));
let mut args = ToolArgs::new();
args.positional.push(Value::String("/ro.txt".into()));
let result = Touch.execute(args, &mut ctx).await;
assert!(!result.ok(), "touch on read-only mount must fail");
}
}