Save and delete files in an aindrive drive without overwriting another change
A token that may write opens a writable folder. A save sends the file whole through write_file with the revision it was read at, or "none" for create-only, so aindrive refuses it as a conflict when the file moved on; saves are capped at one 1 MiB transfer, which a table-driven encoder fits into one call's fuel. A save whose answer is lost, or that the drive's device timed out on, is reported with an unknown outcome. Deleting takes one file; folders are refused because delete_path is recursive and listings hide entries such as .git. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+77
-4
@@ -13,7 +13,13 @@ use std::rc::Rc;
|
||||
pub struct File {
|
||||
pub bytes: Vec<u8>,
|
||||
/// Written into replies verbatim, the way the device's float prints.
|
||||
pub mtime: &'static str,
|
||||
pub mtime: String,
|
||||
}
|
||||
|
||||
impl File {
|
||||
pub fn revision(&self) -> String {
|
||||
format!("m{}-s{}", self.mtime, self.bytes.len())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -27,6 +33,11 @@ pub struct Drive {
|
||||
pub status: Option<u16>,
|
||||
/// Replaces a file between a stat and the read that follows it.
|
||||
pub change_on_read: Option<File>,
|
||||
/// A tool that answers with this error text instead of acting.
|
||||
pub fail: Option<(&'static str, &'static str)>,
|
||||
/// A tool that acts, but whose reply is lost on the way back.
|
||||
pub lose_reply: Option<&'static str>,
|
||||
pub writes: usize,
|
||||
pub seen: Vec<Seen>,
|
||||
}
|
||||
|
||||
@@ -48,10 +59,10 @@ pub fn install(drive: Drive) -> Rc<RefCell<Drive>> {
|
||||
drive
|
||||
}
|
||||
|
||||
pub fn file(bytes: &[u8], mtime: &'static str) -> File {
|
||||
pub fn file(bytes: &[u8], mtime: &str) -> File {
|
||||
File {
|
||||
bytes: bytes.to_vec(),
|
||||
mtime,
|
||||
mtime: mtime.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,6 +117,11 @@ impl Exchange {
|
||||
Some(status) => (status, b"{\"error\":\"refused\"}".to_vec()),
|
||||
None => (200, event(&drive.answer(&call))),
|
||||
};
|
||||
if drive.lose_reply.is_some()
|
||||
&& drive.lose_reply == call["params"]["name"].as_str()
|
||||
{
|
||||
return Err(HostError::Unavailable);
|
||||
}
|
||||
*self.reply.borrow_mut() = Some(reply);
|
||||
Ok(Response {
|
||||
status,
|
||||
@@ -152,6 +168,57 @@ fn failure(text: &str) -> Value {
|
||||
}
|
||||
|
||||
impl Drive {
|
||||
/// As `write_file` does: compare the expected revision, then write.
|
||||
fn write(&mut self, path: &str, arguments: &Value) -> Value {
|
||||
let Ok(bytes) =
|
||||
STANDARD.decode(arguments["content"].as_str().unwrap_or("!"))
|
||||
else {
|
||||
return failure("[invalid_params] content (string) required");
|
||||
};
|
||||
let current = self.files.get(path).map(File::revision);
|
||||
let folder = current.is_none() && self.is_folder(path);
|
||||
let conflict = match arguments["base_revision"].as_str() {
|
||||
None => false,
|
||||
Some("none") => current.is_some() || folder,
|
||||
Some(expected) => current.as_deref() != Some(expected),
|
||||
};
|
||||
if conflict {
|
||||
let current = current.as_deref().unwrap_or("none");
|
||||
return failure(&format!(
|
||||
"[invalid_params] conflict: the file changed since that revision (current revision: {current})"
|
||||
));
|
||||
}
|
||||
if folder {
|
||||
return failure("[internal] EISDIR: illegal operation on a directory");
|
||||
}
|
||||
self.writes += 1;
|
||||
let written = file(&bytes, &format!("17918{:08}.5", self.writes));
|
||||
let revision = written.revision();
|
||||
self.files.insert(path.to_owned(), written);
|
||||
success(json!({
|
||||
"method": "write", "ok": true, "bytes": bytes.len(), "revision": revision,
|
||||
}))
|
||||
}
|
||||
|
||||
/// As `delete_path` does: a folder goes with everything in it.
|
||||
fn delete(&mut self, path: &str) -> Value {
|
||||
let kind = if self.files.remove(path).is_some() {
|
||||
"file"
|
||||
} else if !path.is_empty() && self.is_folder(path) {
|
||||
let prefix = format!("{path}/");
|
||||
self.files.retain(|file, _| !file.starts_with(&prefix));
|
||||
self
|
||||
.folders
|
||||
.retain(|folder| folder != path && !folder.starts_with(&prefix));
|
||||
"folder"
|
||||
} else {
|
||||
return failure(&format!("[not_found] no entry at {path}"));
|
||||
};
|
||||
success(
|
||||
json!({ "method": "delete", "ok": true, "path": path, "kind": kind }),
|
||||
)
|
||||
}
|
||||
|
||||
fn answer(&mut self, call: &Value) -> Value {
|
||||
if call["method"] == "tools/list" {
|
||||
let tools = self.tools.clone().unwrap_or_else(|| {
|
||||
@@ -169,7 +236,13 @@ impl Drive {
|
||||
}
|
||||
let arguments = &call["params"]["arguments"];
|
||||
let path = arguments["path"].as_str().unwrap_or_default().to_owned();
|
||||
match call["params"]["name"].as_str().unwrap_or_default() {
|
||||
let tool = call["params"]["name"].as_str().unwrap_or_default();
|
||||
if let Some((_, text)) = self.fail.filter(|(failing, _)| *failing == tool) {
|
||||
return failure(text);
|
||||
}
|
||||
match tool {
|
||||
"write_file" => self.write(&path, arguments),
|
||||
"delete_path" => self.delete(&path),
|
||||
"list_files" => match self.children(&path) {
|
||||
Some(entries) => success(json!({ "entries": entries })),
|
||||
None if self.files.contains_key(&path) => {
|
||||
|
||||
Reference in New Issue
Block a user