1use super::{DirEntry, Filesystem};
6use async_trait::async_trait;
7use std::collections::BTreeMap;
8use std::io;
9use std::path::{Path, PathBuf};
10use std::sync::Arc;
11
12pub use kaish_types::backend::MountInfo;
16
17#[derive(Default)]
23pub struct VfsRouter {
24 mounts: BTreeMap<PathBuf, Arc<dyn Filesystem>>,
26}
27
28impl std::fmt::Debug for VfsRouter {
29 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30 f.debug_struct("VfsRouter")
31 .field("mounts", &self.mounts.keys().collect::<Vec<_>>())
32 .finish()
33 }
34}
35
36impl VfsRouter {
37 pub fn new() -> Self {
39 Self {
40 mounts: BTreeMap::new(),
41 }
42 }
43
44 pub fn mount(&mut self, path: impl Into<PathBuf>, fs: impl Filesystem + 'static) {
49 let path = Self::normalize_mount_path(path.into());
50 self.mounts.insert(path, Arc::new(fs));
51 }
52
53 pub fn mount_arc(&mut self, path: impl Into<PathBuf>, fs: Arc<dyn Filesystem>) {
55 let path = Self::normalize_mount_path(path.into());
56 self.mounts.insert(path, fs);
57 }
58
59 pub fn unmount(&mut self, path: impl AsRef<Path>) -> bool {
63 let path = Self::normalize_mount_path(path.as_ref().to_path_buf());
64 self.mounts.remove(&path).is_some()
65 }
66
67 pub fn list_mounts(&self) -> Vec<MountInfo> {
69 self.mounts
70 .iter()
71 .map(|(path, fs)| MountInfo {
72 path: path.clone(),
73 read_only: fs.read_only(),
74 resident_bytes: fs.resident_bytes(),
75 })
76 .collect()
77 }
78
79 fn normalize_mount_path(path: PathBuf) -> PathBuf {
81 let s = path.to_string_lossy();
82 let s = s.trim_end_matches('/');
83 if s.is_empty() {
84 PathBuf::from("/")
85 } else if !s.starts_with('/') {
86 PathBuf::from(format!("/{}", s))
87 } else {
88 PathBuf::from(s)
89 }
90 }
91
92 pub fn resolve_real_path(&self, path: &Path) -> Option<PathBuf> {
99 let (fs, relative) = self.find_mount(path).ok()?;
100 fs.real_path(&relative)
101 }
102
103 fn find_mount(&self, path: &Path) -> io::Result<(Arc<dyn Filesystem>, PathBuf)> {
107 let path_str = path.to_string_lossy();
108 let normalized = if path_str.starts_with('/') {
109 path.to_path_buf()
110 } else {
111 PathBuf::from(format!("/{}", path_str))
112 };
113
114 let mut best_match: Option<(&PathBuf, &Arc<dyn Filesystem>)> = None;
116
117 for (mount_path, fs) in &self.mounts {
118 let mount_str = mount_path.to_string_lossy();
119
120 let is_match = if mount_str == "/" {
122 true } else {
124 let normalized_str = normalized.to_string_lossy();
125 normalized_str == mount_str.as_ref()
126 || normalized_str.starts_with(&format!("{}/", mount_str))
127 };
128
129 if is_match {
130 let dominated = best_match
132 .as_ref()
133 .is_none_or(|(bp, _)| mount_path.as_os_str().len() > bp.as_os_str().len());
134 if dominated {
135 best_match = Some((mount_path, fs));
136 }
137 }
138 }
139
140 match best_match {
141 Some((mount_path, fs)) => {
142 let mount_str = mount_path.to_string_lossy();
144 let normalized_str = normalized.to_string_lossy();
145
146 let relative = if mount_str == "/" {
147 normalized_str.trim_start_matches('/').to_string()
148 } else {
149 normalized_str
150 .strip_prefix(mount_str.as_ref())
151 .unwrap_or("")
152 .trim_start_matches('/')
153 .to_string()
154 };
155
156 Ok((Arc::clone(fs), PathBuf::from(relative)))
157 }
158 None => Err(io::Error::new(
159 io::ErrorKind::NotFound,
160 format!("no mount point for path: {}", path.display()),
161 )),
162 }
163 }
164}
165
166#[async_trait]
167impl Filesystem for VfsRouter {
168 #[tracing::instrument(level = "trace", skip(self), fields(path = %path.display()))]
169 async fn read(&self, path: &Path) -> io::Result<Vec<u8>> {
170 let (fs, relative) = self.find_mount(path)?;
171 fs.read(&relative).await
172 }
173
174 #[tracing::instrument(level = "trace", skip(self, data), fields(path = %path.display(), size = data.len()))]
175 async fn write(&self, path: &Path, data: &[u8]) -> io::Result<()> {
176 let (fs, relative) = self.find_mount(path)?;
177 fs.write(&relative, data).await
178 }
179
180 #[tracing::instrument(level = "trace", skip(self), fields(path = %path.display()))]
181 async fn list(&self, path: &Path) -> io::Result<Vec<DirEntry>> {
182 let path_str = path.to_string_lossy();
184 if path_str.is_empty() || path_str == "/" {
185 return self.list_root().await;
186 }
187
188 let (fs, relative) = self.find_mount(path)?;
189 fs.list(&relative).await
190 }
191
192 #[tracing::instrument(level = "trace", skip(self), fields(path = %path.display()))]
193 async fn stat(&self, path: &Path) -> io::Result<DirEntry> {
194 let path_str = path.to_string_lossy();
196 if path_str.is_empty() || path_str == "/" {
197 return Ok(DirEntry::directory("/"));
198 }
199
200 let normalized = Self::normalize_mount_path(path.to_path_buf());
202 if self.mounts.contains_key(&normalized) {
203 let name = path
204 .file_name()
205 .map(|n| n.to_string_lossy().into_owned())
206 .unwrap_or_else(|| "/".to_string());
207 return Ok(DirEntry::directory(name));
208 }
209
210 let (fs, relative) = self.find_mount(path)?;
211 fs.stat(&relative).await
212 }
213
214 async fn read_link(&self, path: &Path) -> io::Result<PathBuf> {
215 let (fs, relative) = self.find_mount(path)?;
216 fs.read_link(&relative).await
217 }
218
219 async fn symlink(&self, target: &Path, link: &Path) -> io::Result<()> {
220 let (fs, relative) = self.find_mount(link)?;
221 fs.symlink(target, &relative).await
222 }
223
224 async fn lstat(&self, path: &Path) -> io::Result<DirEntry> {
225 let path_str = path.to_string_lossy();
227 if path_str.is_empty() || path_str == "/" {
228 return Ok(DirEntry::directory("/"));
229 }
230
231 let normalized = Self::normalize_mount_path(path.to_path_buf());
233 if self.mounts.contains_key(&normalized) {
234 let name = path
235 .file_name()
236 .map(|n| n.to_string_lossy().into_owned())
237 .unwrap_or_else(|| "/".to_string());
238 return Ok(DirEntry::directory(name));
239 }
240
241 let (fs, relative) = self.find_mount(path)?;
242 fs.lstat(&relative).await
243 }
244
245 async fn mkdir(&self, path: &Path) -> io::Result<()> {
246 let (fs, relative) = self.find_mount(path)?;
247 fs.mkdir(&relative).await
248 }
249
250 async fn set_mtime(&self, path: &Path, mtime: std::time::SystemTime) -> io::Result<()> {
251 let (fs, relative) = self.find_mount(path)?;
252 fs.set_mtime(&relative, mtime).await
253 }
254
255 async fn remove(&self, path: &Path) -> io::Result<()> {
256 let (fs, relative) = self.find_mount(path)?;
257 fs.remove(&relative).await
258 }
259
260 async fn rename(&self, from: &Path, to: &Path) -> io::Result<()> {
261 let (from_fs, from_relative) = self.find_mount(from)?;
262 let (to_fs, to_relative) = self.find_mount(to)?;
263
264 if !Arc::ptr_eq(&from_fs, &to_fs) {
266 return Err(io::Error::new(
267 io::ErrorKind::Unsupported,
268 "cannot rename across different mount points",
269 ));
270 }
271
272 from_fs.rename(&from_relative, &to_relative).await
273 }
274
275 fn read_only(&self) -> bool {
276 if self.mounts.is_empty() {
280 return false;
281 }
282 self.mounts.values().all(|fs| fs.read_only())
283 }
284}
285
286impl VfsRouter {
287 async fn list_root(&self) -> io::Result<Vec<DirEntry>> {
289 let mut entries = Vec::new();
290 let mut seen_names = std::collections::HashSet::new();
291
292 for mount_path in self.mounts.keys() {
293 let mount_str = mount_path.to_string_lossy();
294 if mount_str == "/" {
295 if let Some(fs) = self.mounts.get(mount_path)
297 && let Ok(root_entries) = fs.list(Path::new("")).await {
298 for entry in root_entries {
299 if seen_names.insert(entry.name.clone()) {
300 entries.push(entry);
301 }
302 }
303 }
304 } else {
305 let first_component = mount_str
307 .trim_start_matches('/')
308 .split('/')
309 .next()
310 .unwrap_or("");
311
312 if !first_component.is_empty() && seen_names.insert(first_component.to_string()) {
313 entries.push(DirEntry::directory(first_component));
314 }
315 }
316 }
317
318 entries.sort_by(|a, b| a.name.cmp(&b.name));
319 Ok(entries)
320 }
321}
322
323#[cfg(test)]
324mod tests {
325 use super::*;
326 use crate::vfs::MemoryFs;
327
328 #[tokio::test]
329 async fn test_basic_mount() {
330 let mut router = VfsRouter::new();
331 let scratch = MemoryFs::new();
332 scratch.write(Path::new("test.txt"), b"hello").await.unwrap();
333 router.mount("/scratch", scratch);
334
335 let data = router.read(Path::new("/scratch/test.txt")).await.unwrap();
336 assert_eq!(data, b"hello");
337 }
338
339 #[tokio::test]
340 async fn test_multiple_mounts() {
341 let mut router = VfsRouter::new();
342
343 let scratch = MemoryFs::new();
344 scratch.write(Path::new("a.txt"), b"scratch").await.unwrap();
345 router.mount("/scratch", scratch);
346
347 let data = MemoryFs::new();
348 data.write(Path::new("b.txt"), b"data").await.unwrap();
349 router.mount("/data", data);
350
351 assert_eq!(
352 router.read(Path::new("/scratch/a.txt")).await.unwrap(),
353 b"scratch"
354 );
355 assert_eq!(
356 router.read(Path::new("/data/b.txt")).await.unwrap(),
357 b"data"
358 );
359 }
360
361 #[tokio::test]
362 async fn test_nested_mount() {
363 let mut router = VfsRouter::new();
364
365 let outer = MemoryFs::new();
366 outer.write(Path::new("outer.txt"), b"outer").await.unwrap();
367 router.mount("/mnt", outer);
368
369 let inner = MemoryFs::new();
370 inner.write(Path::new("inner.txt"), b"inner").await.unwrap();
371 router.mount("/mnt/project", inner);
372
373 assert_eq!(
375 router.read(Path::new("/mnt/outer.txt")).await.unwrap(),
376 b"outer"
377 );
378
379 assert_eq!(
381 router.read(Path::new("/mnt/project/inner.txt")).await.unwrap(),
382 b"inner"
383 );
384 }
385
386 #[tokio::test]
387 async fn test_list_root() {
388 let mut router = VfsRouter::new();
389 router.mount("/scratch", MemoryFs::new());
390 router.mount("/mnt/a", MemoryFs::new());
391 router.mount("/mnt/b", MemoryFs::new());
392
393 let entries = router.list(Path::new("/")).await.unwrap();
394 let names: Vec<_> = entries.iter().map(|e| &e.name).collect();
395
396 assert!(names.contains(&&"scratch".to_string()));
397 assert!(names.contains(&&"mnt".to_string()));
398 }
399
400 #[tokio::test]
401 async fn test_unmount() {
402 let mut router = VfsRouter::new();
403
404 let fs = MemoryFs::new();
405 fs.write(Path::new("test.txt"), b"data").await.unwrap();
406 router.mount("/scratch", fs);
407
408 assert!(router.read(Path::new("/scratch/test.txt")).await.is_ok());
409
410 router.unmount("/scratch");
411
412 assert!(router.read(Path::new("/scratch/test.txt")).await.is_err());
413 }
414
415 #[tokio::test]
416 async fn test_list_mounts() {
417 let mut router = VfsRouter::new();
418 router.mount("/scratch", MemoryFs::new());
419 router.mount("/data", MemoryFs::new());
420
421 let mounts = router.list_mounts();
422 assert_eq!(mounts.len(), 2);
423
424 let paths: Vec<_> = mounts.iter().map(|m| &m.path).collect();
425 assert!(paths.contains(&&PathBuf::from("/scratch")));
426 assert!(paths.contains(&&PathBuf::from("/data")));
427 }
428
429 #[tokio::test]
430 async fn test_no_mount_error() {
431 let router = VfsRouter::new();
432 let result = router.read(Path::new("/nothing/here.txt")).await;
433 assert!(result.is_err());
434 assert_eq!(result.unwrap_err().kind(), io::ErrorKind::NotFound);
435 }
436
437 #[tokio::test]
438 async fn test_root_mount() {
439 let mut router = VfsRouter::new();
440
441 let root = MemoryFs::new();
442 root.write(Path::new("at-root.txt"), b"root file").await.unwrap();
443 router.mount("/", root);
444
445 let data = router.read(Path::new("/at-root.txt")).await.unwrap();
446 assert_eq!(data, b"root file");
447 }
448
449 #[tokio::test]
450 async fn test_write_through_router() {
451 let mut router = VfsRouter::new();
452 router.mount("/scratch", MemoryFs::new());
453
454 router
455 .write(Path::new("/scratch/new.txt"), b"created")
456 .await
457 .unwrap();
458
459 let data = router.read(Path::new("/scratch/new.txt")).await.unwrap();
460 assert_eq!(data, b"created");
461 }
462
463 #[tokio::test]
464 async fn test_stat_mount_point() {
465 let mut router = VfsRouter::new();
466 router.mount("/scratch", MemoryFs::new());
467
468 let entry = router.stat(Path::new("/scratch")).await.unwrap();
469 assert!(entry.is_dir());
470 }
471
472 #[tokio::test]
473 async fn test_stat_root() {
474 let router = VfsRouter::new();
475 let entry = router.stat(Path::new("/")).await.unwrap();
476 assert!(entry.is_dir());
477 }
478
479 #[tokio::test]
480 async fn test_rename_same_mount() {
481 let mut router = VfsRouter::new();
482 let mem = MemoryFs::new();
483 mem.write(Path::new("old.txt"), b"data").await.unwrap();
484 router.mount("/scratch", mem);
485
486 router.rename(Path::new("/scratch/old.txt"), Path::new("/scratch/new.txt")).await.unwrap();
487
488 let data = router.read(Path::new("/scratch/new.txt")).await.unwrap();
490 assert_eq!(data, b"data");
491
492 assert!(!router.exists(Path::new("/scratch/old.txt")).await);
494 }
495
496 #[tokio::test]
497 async fn test_rename_cross_mount_fails() {
498 let mut router = VfsRouter::new();
499 let mem1 = MemoryFs::new();
500 mem1.write(Path::new("file.txt"), b"data").await.unwrap();
501 router.mount("/mount1", mem1);
502 router.mount("/mount2", MemoryFs::new());
503
504 let result = router.rename(Path::new("/mount1/file.txt"), Path::new("/mount2/file.txt")).await;
505 assert!(result.is_err());
506 assert_eq!(result.unwrap_err().kind(), io::ErrorKind::Unsupported);
507 }
508
509 #[tokio::test]
510 async fn read_only_empty_router_returns_false() {
511 let router = VfsRouter::new();
512 assert!(!router.read_only());
513 }
514
515 #[cfg(feature = "localfs")]
516 #[tokio::test]
517 async fn read_only_all_read_only_mounts_returns_true() {
518 use crate::vfs::LocalFs;
519
520 let t1 = tempfile::tempdir().unwrap();
521 let t2 = tempfile::tempdir().unwrap();
522
523 let mut router = VfsRouter::new();
524 router.mount("/a", LocalFs::read_only(t1.path().to_path_buf()));
525 router.mount("/b", LocalFs::read_only(t2.path().to_path_buf()));
526
527 assert!(router.read_only());
528 }
529
530 #[cfg(feature = "localfs")]
531 #[tokio::test]
532 async fn read_only_mixed_mounts_returns_false() {
533 use crate::vfs::LocalFs;
534
535 let t1 = tempfile::tempdir().unwrap();
536
537 let mut router = VfsRouter::new();
538 router.mount("/ro", LocalFs::read_only(t1.path().to_path_buf()));
539 router.mount("/rw", MemoryFs::new());
540
541 assert!(!router.read_only());
542 }
543}