File operations under /actor/<id>/fs/ become messages from `fs` to <id> (fs_read, fs_write, fs_mkdir, fs_remove, fs_rename) with a 10 s timeout. vfs.rs wraps the wasi:filesystem implementation of wasmtime-wasi.
This commit is contained in:
@@ -39,6 +39,8 @@ fn main() {
|
||||
}
|
||||
|
||||
let mut open = HashMap::<String, std::fs::File>::new();
|
||||
// Files that this actor serves at /actor/<id>/fs/ (path -> text).
|
||||
let mut served = std::collections::BTreeMap::<String, String>::new();
|
||||
let stdin = io::stdin();
|
||||
let mut out = io::stdout().lock();
|
||||
let mut emit_shutdown = false;
|
||||
@@ -73,6 +75,40 @@ fn main() {
|
||||
break;
|
||||
}
|
||||
Some("echo") => json!({"type": "echo", "value": body["value"]}),
|
||||
Some("fs_read") => {
|
||||
let dir = if path == "/" { "/".to_string() } else { format!("{path}/") };
|
||||
let mut names: Vec<String> = served
|
||||
.keys()
|
||||
.filter_map(|k| k.strip_prefix(&dir))
|
||||
.map(|rest| match rest.split_once('/') {
|
||||
Some((first, _)) => format!("{first}/"),
|
||||
None => rest.to_string(),
|
||||
})
|
||||
.collect();
|
||||
names.dedup();
|
||||
if let Some(text) = served.get(&path) {
|
||||
json!({"type": "fs_file", "text": text})
|
||||
} else if path == "/" || !names.is_empty() {
|
||||
let entries: Vec<Value> = names
|
||||
.iter()
|
||||
.map(|n| match n.strip_suffix('/') {
|
||||
Some(d) => json!({"name": d, "type": "dir"}),
|
||||
None => json!({"name": n, "type": "file"}),
|
||||
})
|
||||
.collect();
|
||||
json!({"type": "fs_dir", "entries": entries})
|
||||
} else {
|
||||
json!({"type": "fs_error", "errno": "no-entry"})
|
||||
}
|
||||
}
|
||||
Some("fs_write") => {
|
||||
served.insert(path.clone(), text.to_string());
|
||||
json!({"type": "fs_done"})
|
||||
}
|
||||
Some("fs_remove") => match served.remove(&path) {
|
||||
Some(_) => json!({"type": "fs_done"}),
|
||||
None => json!({"type": "fs_error", "errno": "no-entry"}),
|
||||
},
|
||||
Some("publish") => {
|
||||
send(
|
||||
&mut out,
|
||||
|
||||
Reference in New Issue
Block a user