agentmux_srv_core/backend/
workspace_copy.rs1use std::io::Read as _;
11use std::path::{Path, PathBuf};
12
13pub fn open_beneath(root: &Path, file: &Path) -> Result<std::fs::File, String> {
15 #[cfg(unix)]
16 return super::widget::beneath::file_beneath(root, file).map_err(|e| format!("{} can't be read from the workspace: {e}", file.display()));
17 #[cfg(not(unix))]
18 return super::widget::packages::open_within(file, Some(root));
19}
20
21pub fn copy_file(root: &Path, file: &Path, into: &Path, limit: u64) -> Result<PathBuf, String> {
24 let name = file.file_name().ok_or_else(|| format!("{} has no file name", file.display()))?;
25 let mut source = open_beneath(root, file)?;
26 let size = source.metadata().map_err(|e| e.to_string())?.len();
27 if size > limit {
28 return Err(format!("{} is {} MB, over the {} MB a snapshot can be", file.display(), size.div_ceil(1_000_000), limit / 1_000_000));
29 }
30 std::fs::create_dir_all(into).map_err(|e| format!("can't make the snapshot folder: {e}"))?;
31 let copy = into.join(name);
32 let mut out = std::fs::File::create(©).map_err(|e| format!("can't write the snapshot: {e}"))?;
33 let written = std::io::copy(&mut (&mut source).take(limit + 1), &mut out).map_err(|e| format!("can't copy {}: {e}", file.display()))?;
35 drop(out);
36 if written > limit {
37 let _ = std::fs::remove_file(©);
38 return Err(format!("{} grew past the {} MB a snapshot can be", file.display(), limit / 1_000_000));
39 }
40 let mut permissions = std::fs::metadata(©).map_err(|e| e.to_string())?.permissions();
41 permissions.set_readonly(true);
42 std::fs::set_permissions(©, permissions).map_err(|e| e.to_string())?;
43 Ok(copy)
44}
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 fn workspace() -> (tempfile::TempDir, PathBuf) {
51 let tmp = tempfile::tempdir().unwrap();
52 let ws = tmp.path().join("ws").canonicalize().unwrap_or_else(|_| {
53 std::fs::create_dir_all(tmp.path().join("ws")).unwrap();
54 tmp.path().join("ws").canonicalize().unwrap()
55 });
56 std::fs::create_dir_all(ws.join("src")).unwrap();
57 std::fs::write(ws.join("src/main.rs"), "fn main() {}").unwrap();
58 std::fs::write(tmp.path().join("secret.txt"), "outside").unwrap();
59 (tmp, ws)
60 }
61
62 #[test]
63 fn a_file_is_copied_read_only_under_its_own_name() {
64 let (tmp, ws) = workspace();
65 let into = tmp.path().join("snap");
66 let copy = copy_file(&ws, &ws.join("src/main.rs"), &into, 1000).unwrap();
67 assert_eq!(copy, into.join("main.rs"));
68 assert_eq!(std::fs::read_to_string(©).unwrap(), "fn main() {}");
69 assert!(std::fs::metadata(©).unwrap().permissions().readonly());
70 }
71
72 #[test]
73 fn a_file_over_the_limit_is_refused_and_leaves_nothing() {
74 let (tmp, ws) = workspace();
75 std::fs::write(ws.join("big.bin"), vec![0u8; 5000]).unwrap();
76 let into = tmp.path().join("snap");
77 let err = copy_file(&ws, &ws.join("big.bin"), &into, 1000).unwrap_err();
78 assert!(err.contains("a snapshot can be"), "{err}");
79 assert!(!into.join("big.bin").exists());
80 }
81
82 #[cfg(unix)]
83 #[test]
84 fn a_link_out_of_the_workspace_is_not_followed() {
85 let (tmp, ws) = workspace();
86 std::os::unix::fs::symlink(tmp.path().join("secret.txt"), ws.join("link.txt")).unwrap();
87 assert!(copy_file(&ws, &ws.join("link.txt"), &tmp.path().join("snap"), 1000).is_err());
88 std::os::unix::fs::symlink(tmp.path(), ws.join("up")).unwrap();
90 assert!(copy_file(&ws, &ws.join("up/secret.txt"), &tmp.path().join("snap"), 1000).is_err());
91 assert!(!tmp.path().join("snap").exists(), "nothing was made for a refused file");
92 }
93
94 #[test]
95 fn a_path_outside_the_workspace_is_refused() {
96 let (tmp, ws) = workspace();
97 assert!(copy_file(&ws, &tmp.path().join("secret.txt"), &tmp.path().join("snap"), 1000).is_err());
98 }
99}