agentmux_srv/bootstrap/
shared_root.rs1use std::path::Path;
15
16pub(super) fn refusal(
20 data_home: &Path,
21 home: Option<&Path>,
22 home_override: bool,
23 shared_dir_set: bool,
24) -> Option<String> {
25 if data_home.as_os_str().is_empty() || home_override || shared_dir_set {
26 return None;
27 }
28 let home = home?;
29 if data_home.starts_with(home) {
30 return None;
31 }
32 Some(format!(
33 "the data folder {} is outside the AgentMux home {}, so the shared folder ({}) would be the real one. \
34 Start it on an isolated home (AGENTMUX_HOME_OVERRIDE=<folder>), \
35 or set AGENTMUX_SHARED_DIR to the shared folder to use on purpose.",
36 data_home.display(),
37 home.display(),
38 home.join("shared").display(),
39 ))
40}
41
42pub(super) fn guard_shared_folder(data_home: &Path, version: &str) {
45 refuse_unlisted_shared_root(data_home);
46 super::shared_guard::claim_at_boot(version);
47}
48
49pub(super) fn refuse_unlisted_shared_root(data_home: &Path) {
51 let set = |name| std::env::var_os(name).is_some_and(|v| !v.is_empty());
52 let home = crate::srv_info::agentmux_home();
53 if let Some(why) = refusal(
54 data_home,
55 home.as_deref(),
56 set("AGENTMUX_HOME_OVERRIDE"),
57 set("AGENTMUX_SHARED_DIR"),
58 ) {
59 tracing::error!("startup: {why}");
60 eprintln!("agentmux-srv: {why}");
61 std::process::exit(1);
62 }
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68 use std::path::PathBuf;
69
70 fn home() -> PathBuf {
71 PathBuf::from("/home/me/.agentmux")
72 }
73
74 #[test]
75 fn a_data_folder_outside_the_home_with_nothing_said_about_shared_refuses() {
76 let why = refusal(Path::new("/tmp/iso"), Some(&home()), false, false).expect("refuses");
77 assert!(
78 why.contains("/tmp/iso")
79 && why.contains("AGENTMUX_HOME_OVERRIDE")
80 && why.contains("AGENTMUX_SHARED_DIR"),
81 "{why}"
82 );
83 }
84
85 #[test]
86 fn a_home_override_or_an_explicit_shared_dir_says_where_shared_data_goes() {
87 assert_eq!(
88 refusal(Path::new("/tmp/iso"), Some(&home()), true, false),
89 None
90 );
91 assert_eq!(
92 refusal(Path::new("/tmp/iso"), Some(&home()), false, true),
93 None
94 );
95 }
96
97 #[test]
98 fn a_data_folder_inside_the_home_or_none_at_all_is_fine() {
99 assert_eq!(
100 refusal(
101 &home().join("channels/stable/versions/1/data"),
102 Some(&home()),
103 false,
104 false
105 ),
106 None
107 );
108 assert_eq!(refusal(Path::new(""), Some(&home()), false, false), None);
109 assert_eq!(refusal(Path::new("/tmp/iso"), None, false, false), None);
110 }
111
112 #[test]
113 fn a_folder_that_only_shares_the_homes_name_prefix_is_outside_it() {
114 assert!(refusal(
115 Path::new("/home/me/.agentmux-iso/data"),
116 Some(&home()),
117 false,
118 false
119 )
120 .is_some());
121 }
122}