hyprstream_vfs/proxy.rs
1//! VFS proxy for bridging sync callers to async Namespace operations.
2//!
3//! **Security invariant:** The [`VfsOp`] enum deliberately excludes
4//! `mount`, `bind_mount`, and `unmount` operations. Scripts cannot
5//! modify the namespace through the proxy — they can only read, write,
6//! list, and ctl existing mount points.
7
8use crate::namespace::Namespace;
9use hyprstream_rpc::Subject;
10use std::sync::Arc;
11
12/// Request from sync caller to async VFS layer.
13pub enum VfsOp {
14 Cat { path: String },
15 Ls { path: String },
16 Echo { path: String, data: Vec<u8> },
17 Ctl { path: String, cmd: Vec<u8> },
18 MountPrefixes,
19}
20
21pub struct VfsRequest {
22 pub op: VfsOp,
23 pub reply: std::sync::mpsc::SyncSender<Result<Vec<u8>, String>>,
24}
25
26/// Spawn the async VFS proxy task. Returns a Sender for submitting requests.
27///
28/// The proxy task runs on the tokio runtime and processes VFS operations.
29/// MUST be called from within a tokio runtime context.
30///
31/// Each request is executed in its own spawned task so that a panic in any
32/// `Mount` method is caught as a `JoinError` instead of killing the proxy
33/// loop and bricking every shell connected to this namespace.
34pub fn spawn_vfs_proxy(
35 ns: Arc<Namespace>,
36 subject: Subject,
37) -> tokio::sync::mpsc::Sender<VfsRequest> {
38 let (tx, mut rx) = tokio::sync::mpsc::channel::<VfsRequest>(64);
39 tokio::spawn(async move {
40 while let Some(req) = rx.recv().await {
41 let ns = Arc::clone(&ns);
42 let subject = subject.clone();
43 // Spawn each request in its own task. If a Mount method panics,
44 // the JoinError is caught here instead of unwinding the proxy loop.
45 let handle = tokio::spawn(async move {
46 let result = match req.op {
47 VfsOp::Cat { ref path } => {
48 ns.cat(path, &subject).await.map_err(|e| e.to_string())
49 }
50 VfsOp::Ls { ref path } => ns
51 .ls(path, &subject)
52 .await
53 .map(|entries| {
54 entries
55 .iter()
56 .map(|e| {
57 if e.is_dir {
58 format!("{}/", e.name)
59 } else {
60 e.name.clone()
61 }
62 })
63 .collect::<Vec<_>>()
64 .join("\n")
65 .into_bytes()
66 })
67 .map_err(|e| e.to_string()),
68 VfsOp::Echo { ref path, ref data } => ns
69 .echo(path, data, &subject)
70 .await
71 .map(|_| Vec::new())
72 .map_err(|e| e.to_string()),
73 VfsOp::Ctl { ref path, ref cmd } => {
74 ns.ctl(path, cmd, &subject).await.map_err(|e| e.to_string())
75 }
76 VfsOp::MountPrefixes => {
77 let prefixes = ns.mount_prefixes();
78 Ok(prefixes.join("\n").into_bytes())
79 }
80 };
81 (result, req.reply)
82 });
83 // Await the per-request task: Ok means normal completion,
84 // Err(JoinError) means the task panicked.
85 match handle.await {
86 Ok((result, reply)) => {
87 let _ = reply.send(result);
88 }
89 Err(join_err) => {
90 // Task panicked — proxy loop survives.
91 // The reply sender was moved into the panicked task and is
92 // now dropped, so the caller's recv() returns RecvError
93 // which builtins already handle as "VFS proxy did not respond".
94 eprintln!("VFS proxy: request panicked: {join_err}");
95 }
96 }
97 }
98 });
99 tx
100}