| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | | use super::*; |
| 6 | |
|
| 7 | | #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] |
| 8 | | pub enum Health { |
| 9 | | Ok, |
| 10 | | Warn, |
| 11 | | Fail, |
| 12 | | } |
| 13 | |
|
| 14 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 15 | | pub struct Check { |
| 16 | | |
| 17 | | pub area: String, |
| 18 | | pub health: Health, |
| 19 | | pub message: String, |
| 20 | | |
| 21 | | pub hint: Option<String>, |
| 22 | | } |
| 23 | |
|
| 24 | | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 25 | | pub struct DoctorReport { |
| 26 | | pub checks: Vec<Check>, |
| 27 | | } |
| 28 | |
|
| 29 | | impl DoctorReport { |
| 30 | 1× | pub fn worst(&self) -> Health { |
| 31 | 1× | self.checks |
| 32 | 1× | .iter() |
| 33 | 1× | .map(|c| c.health) |
| 34 | 1× | .max() |
| 35 | 1× | .unwrap_or(Health::Ok) |
| 36 | 1× | } |
| 37 | |
|
| 38 | 31× | fn push( |
| 39 | 31× | &mut self, |
| 40 | 31× | area: impl Into<String>, |
| 41 | 31× | health: Health, |
| 42 | 31× | message: impl Into<String>, |
| 43 | 31× | hint: Option<String>, |
| 44 | 31× | ) { |
| 45 | 31× | self.checks.push(Check { |
| 46 | 31× | area: area.into(), |
| 47 | 31× | health, |
| 48 | 31× | message: message.into(), |
| 49 | 31× | hint, |
| 50 | 31× | }); |
| 51 | 31× | } |
| 52 | | } |
| 53 | |
|
| 54 | |
|
| 55 | | const STALE_LOCK_SECS: u64 = 60; |
| 56 | |
|
| 57 | | impl Repo { |
| 58 | | |
| 59 | 5× | pub fn doctor(&self, online: bool) -> DoctorReport { |
| 60 | 5× | let mut rep = DoctorReport::default(); |
| 61 | 5× | self.doctor_repository(&mut rep); |
| 62 | 5× | self.doctor_identity(&mut rep); |
| 63 | 5× | self.doctor_author(&mut rep); |
| 64 | 5× | self.doctor_portability(&mut rep); |
| 65 | 6× | for name in self.config.remotes.keys() { |
| 66 | 6× | self.doctor_remote(&mut rep, name, online); |
| 67 | 6× | } |
| 68 | 5× | if self.config.remotes.is_empty() && !self.bare { |
| 69 | 3× | rep.push("remotes", Health::Ok, "none configured", None); |
| 70 | 3× | } |
| 71 | 5× | rep |
| 72 | 5× | } |
| 73 | |
|
| 74 | 5× | fn doctor_repository(&self, rep: &mut DoctorReport) { |
| 75 | 5× | match self.head_op() { |
| 76 | 5× | Ok(_) => rep.push( |
| 77 | | "repository", |
| 78 | 5× | Health::Ok, |
| 79 | 5× | format!( |
| 80 | | "{} at {}", |
| 81 | 5× | if self.bare { |
| 82 | 0× | "bare repository" |
| 83 | | } else { |
| 84 | 5× | "repository" |
| 85 | | }, |
| 86 | 5× | self.root.display() |
| 87 | | ), |
| 88 | 5× | None, |
| 89 | | ), |
| 90 | 0× | Err(e) => rep.push( |
| 91 | | "repository", |
| 92 | 0× | Health::Fail, |
| 93 | 0× | format!("the operation log does not open: {e}"), |
| 94 | 0× | Some("run `oxy fsck` for details".into()), |
| 95 | | ), |
| 96 | | } |
| 97 | 5× | let mut locks = vec![self.oxy.join("ops").join("lock")]; |
| 98 | 5× | if let Ok(rd) = fs::read_dir(self.oxy.join("workspaces")) { |
| 99 | 5× | locks.extend(rd.flatten().map(|e| e.path().join("lock"))); |
| 100 | 0× | } |
| 101 | 10× | for lock in locks { |
| 102 | 10× | let age = fs::metadata(&lock) |
| 103 | 10× | .and_then(|m| m.modified()) |
| 104 | 10× | .ok() |
| 105 | 10× | .and_then(|t| t.elapsed().ok()); |
| 106 | 10× | if let Some(age) = age.filter(|a| a.as_secs() > STALE_LOCK_SECS) { |
| 107 | 0× | rep.push( |
| 108 | 0× | "repository", |
| 109 | 0× | Health::Warn, |
| 110 | 0× | format!( |
| 111 | 0× | "a lock left by a process that stopped {} s ago: {}", |
| 112 | 0× | age.as_secs(), |
| 113 | 0× | lock.display() |
| 114 | 0× | ), |
| 115 | 0× | Some( |
| 116 | 0× | "it is taken over automatically; delete it if no oxy command is running" |
| 117 | 0× | .into(), |
| 118 | 0× | ), |
| 119 | 0× | ); |
| 120 | 10× | } |
| 121 | | } |
| 122 | 5× | } |
| 123 | |
|
| 124 | 5× | fn doctor_identity(&self, rep: &mut DoctorReport) { |
| 125 | 5× | let Some(key) = self.config.author_key else { |
| 126 | 2× | rep.push( |
| 127 | | "identity", |
| 128 | 2× | Health::Warn, |
| 129 | | "no signing identity: pushes to a repository with a trust policy will be refused", |
| 130 | 2× | Some("create one with `oxy identity create`".into()), |
| 131 | | ); |
| 132 | 2× | return; |
| 133 | | }; |
| 134 | 3× | let id = match self.identity(&key) { |
| 135 | 2× | Ok(id) => id, |
| 136 | | |
| 137 | | |
| 138 | | Err(_) |
| 139 | 1× | if self |
| 140 | 1× | .keys_dir |
| 141 | 1× | .join(format!("{}.secret", key.to_hex())) |
| 142 | 1× | .is_file() => |
| 143 | | { |
| 144 | 1× | rep.push( |
| 145 | | "identity", |
| 146 | 1× | Health::Ok, |
| 147 | 1× | format!( |
| 148 | | "identity {} has its secret key; it is stored in the repository with your first signed change", |
| 149 | 1× | key.short(12) |
| 150 | | ), |
| 151 | 1× | None, |
| 152 | | ); |
| 153 | 1× | return; |
| 154 | | } |
| 155 | 0× | Err(e) => { |
| 156 | 0× | rep.push( |
| 157 | | "identity", |
| 158 | 0× | Health::Fail, |
| 159 | 0× | format!( |
| 160 | | "identity {} is configured but cannot be read: {e}", |
| 161 | 0× | key.short(12) |
| 162 | | ), |
| 163 | 0× | Some("restore it, or create a new one with `oxy identity create`".into()), |
| 164 | | ); |
| 165 | 0× | return; |
| 166 | | } |
| 167 | | }; |
| 168 | 2× | let secret = self.keys_dir.join(format!("{}.secret", key.to_hex())); |
| 169 | 2× | if !secret.is_file() { |
| 170 | 0× | rep.push( |
| 171 | | "identity", |
| 172 | 0× | Health::Fail, |
| 173 | 0× | format!("the secret key for {} is missing ({})", key.short(12), secret.display()), |
| 174 | 0× | Some("copy the key file back from a backup; without it nothing can be signed with this identity".into()), |
| 175 | | ); |
| 176 | 0× | return; |
| 177 | 2× | } |
| 178 | 2× | let now = Hlc::now().wall_ms; |
| 179 | 0× | match id.expires { |
| 180 | 0× | Some(t) if t <= now => rep.push( |
| 181 | | "identity", |
| 182 | 0× | Health::Fail, |
| 183 | 0× | format!("{} <{}> expired", id.name, id.email), |
| 184 | 0× | Some("renew it with `oxy identity renew`".into()), |
| 185 | | ), |
| 186 | 0× | Some(t) if t - now < 30 * 86_400_000 => rep.push( |
| 187 | | "identity", |
| 188 | 0× | Health::Warn, |
| 189 | 0× | format!( |
| 190 | | "{} <{}> expires in {} days", |
| 191 | | id.name, |
| 192 | | id.email, |
| 193 | 0× | (t - now) / 86_400_000 |
| 194 | | ), |
| 195 | 0× | Some("renew it with `oxy identity renew`".into()), |
| 196 | | ), |
| 197 | 2× | _ => rep.push( |
| 198 | | "identity", |
| 199 | 2× | Health::Ok, |
| 200 | 2× | format!("{} <{}> ({})", id.name, id.email, key.short(12)), |
| 201 | 2× | None, |
| 202 | | ), |
| 203 | | } |
| 204 | 5× | } |
| 205 | |
|
| 206 | | |
| 207 | | |
| 208 | 5× | fn doctor_portability(&self, rep: &mut DoctorReport) { |
| 209 | 5× | let Ok(heads) = self.bookmark_heads() else { |
| 210 | 0× | return; |
| 211 | | }; |
| 212 | 5× | let Ok(state) = self.load_state_history(&heads) else { |
| 213 | 0× | return; |
| 214 | | }; |
| 215 | 5× | let view = state.tree().view(); |
| 216 | 5× | let paths: Vec<&str> = view.entries.keys().map(String::as_str).collect(); |
| 217 | 5× | let mut problems: Vec<String> = |
| 218 | 5× | crate::portable::Platform::windows().problems(paths.iter().copied()); |
| 219 | 5× | problems.sort(); |
| 220 | 5× | problems.dedup(); |
| 221 | 5× | if problems.is_empty() { |
| 222 | 4× | rep.push( |
| 223 | | "portability", |
| 224 | 4× | Health::Ok, |
| 225 | 4× | format!( |
| 226 | | "every path of {} checks out on Linux, macOS and Windows", |
| 227 | | self.config.bookmark |
| 228 | | ), |
| 229 | 4× | None, |
| 230 | | ); |
| 231 | 4× | return; |
| 232 | 1× | } |
| 233 | 1× | let shown: Vec<&String> = problems.iter().take(5).collect(); |
| 234 | 1× | rep.push( |
| 235 | | "portability", |
| 236 | 1× | Health::Warn, |
| 237 | 1× | format!( |
| 238 | | "{} path problem{} on case-insensitive or Windows file systems: {}{}", |
| 239 | 1× | problems.len(), |
| 240 | 1× | if problems.len() == 1 { "" } else { "s" }, |
| 241 | 2× | shown.iter().map(|s| s.as_str()).collect::<Vec<_>>().join("; "), |
| 242 | 1× | if problems.len() > 5 { "; …" } else { "" } |
| 243 | | ), |
| 244 | 1× | Some("rename the paths so they differ by more than case and avoid Windows' reserved names and characters".into()), |
| 245 | | ); |
| 246 | 5× | } |
| 247 | |
|
| 248 | 5× | fn doctor_author(&self, rep: &mut DoctorReport) { |
| 249 | 5× | let defaulted = |
| 250 | 5× | self.config.author_email.ends_with("@localhost") || self.config.author_name.is_empty(); |
| 251 | 5× | if defaulted { |
| 252 | 2× | rep.push( |
| 253 | 2× | "author", |
| 254 | 2× | Health::Warn, |
| 255 | 2× | format!("changes are authored as {} <{}>, a default", self.config.author_name, self.config.author_email), |
| 256 | 2× | Some("set yours with `oxy config --user author.name NAME` and `oxy config --user author.email EMAIL`".into()), |
| 257 | 2× | ); |
| 258 | 3× | } else { |
| 259 | 3× | rep.push( |
| 260 | 3× | "author", |
| 261 | 3× | Health::Ok, |
| 262 | 3× | format!("{} <{}>", self.config.author_name, self.config.author_email), |
| 263 | 3× | None, |
| 264 | 3× | ); |
| 265 | 3× | } |
| 266 | 5× | } |
| 267 | |
|
| 268 | 6× | fn doctor_remote(&self, rep: &mut DoctorReport, name: &str, online: bool) { |
| 269 | 6× | let area = format!("remote {name}"); |
| 270 | 6× | let url = &self.config.remotes[name]; |
| 271 | 6× | let opts = self.remote_options(name); |
| 272 | 6× | let http = url.starts_with("http://") || url.starts_with("https://"); |
| 273 | 6× | let hint = |h: &str| Some(h.replace("<remote>", name)); |
| 274 | 6× | if url.starts_with("http://") && !is_loopback(url) { |
| 275 | 0× | rep.push( |
| 276 | 0× | &area, |
| 277 | 0× | Health::Warn, |
| 278 | 0× | format!("{url} is plain HTTP: the token travels unencrypted"), |
| 279 | 0× | hint("use the hub's https:// address"), |
| 280 | 0× | ); |
| 281 | 6× | } |
| 282 | 6× | if http && opts.token.is_none() { |
| 283 | 2× | rep.push( |
| 284 | 2× | &area, |
| 285 | 2× | Health::Warn, |
| 286 | 2× | "no token: fetching public repositories works, pushing will not", |
| 287 | 2× | hint("set one with `oxy config remote.<remote>.token TOKEN`"), |
| 288 | 2× | ); |
| 289 | 4× | } |
| 290 | 6× | if let Some(ca) = &opts.ca { |
| 291 | 2× | if !ca.is_file() { |
| 292 | 2× | rep.push(&area, Health::Fail, format!("the CA file {} does not exist", ca.display()), hint("download the hub's certificate from /.well-known/oxyhub-ca.pem and run `oxy config remote.<remote>.ca FILE`")); |
| 293 | 2× | return; |
| 294 | 0× | } |
| 295 | 4× | } |
| 296 | 4× | if !online { |
| 297 | 2× | rep.push(&area, Health::Ok, format!("{url} (not contacted)"), None); |
| 298 | 2× | return; |
| 299 | 2× | } |
| 300 | 2× | let reached = crate::protocol::open_remote(url, &opts).and_then(|r| r.repo_id()); |
| 301 | 0× | match reached { |
| 302 | 1× | Ok(theirs) if theirs == crate::protocol::UNBOUND_ID => rep.push( |
| 303 | 1× | &area, |
| 304 | 1× | Health::Ok, |
| 305 | 1× | format!("{url} is reachable and empty; the first push binds it"), |
| 306 | 1× | None, |
| 307 | | ), |
| 308 | 0× | Ok(theirs) if self.id().is_ok_and(|mine| mine == theirs) => rep.push( |
| 309 | 0× | &area, |
| 310 | 0× | Health::Ok, |
| 311 | 0× | format!("{url} is reachable and is this repository"), |
| 312 | 0× | None, |
| 313 | | ), |
| 314 | 0× | Ok(_) => rep.push( |
| 315 | 0× | &area, |
| 316 | 0× | Health::Fail, |
| 317 | 0× | format!("{url} is a different repository"), |
| 318 | 0× | hint("check the URL; each repository has its own id"), |
| 319 | | ), |
| 320 | 1× | Err(e) => { |
| 321 | 1× | let msg = e.to_string(); |
| 322 | 1× | let first = msg.lines().next().unwrap_or(&msg).to_string(); |
| 323 | 1× | rep.push( |
| 324 | 1× | &area, |
| 325 | 1× | Health::Fail, |
| 326 | 1× | first, |
| 327 | 1× | crate::protocol::hint_for(&msg).map(|h| h.replace("<remote>", name)), |
| 328 | | ); |
| 329 | | } |
| 330 | | } |
| 331 | 6× | } |
| 332 | | } |
| 333 | |
|
| 334 | 2× | fn is_loopback(url: &str) -> bool { |
| 335 | 2× | let host = url |
| 336 | 2× | .split("://") |
| 337 | 2× | .nth(1) |
| 338 | 2× | .unwrap_or("") |
| 339 | 2× | .split(['/', ':']) |
| 340 | 2× | .next() |
| 341 | 2× | .unwrap_or(""); |
| 342 | 2× | host == "localhost" || host == "127.0.0.1" || host == "[::1]" |
| 343 | 2× | } |