| 383 | } |
| 384 | |
| 385 | fn install_or_refresh_dispatcher(path: &Path, script: &str) -> io::Result<DispatcherInstall> { |
| 386 | let metadata = match fs::symlink_metadata(path) { |
| 387 | Ok(metadata) => Some(metadata), |
| 388 | Err(error) if error.kind() == ErrorKind::NotFound => None, |
| 389 | Err(error) => return Err(error), |
| 390 | }; |
| 391 | |
| 392 | match metadata { |
| 393 | None => { |
| 394 | write_new_executable(path, script.as_bytes())?; |
| 395 | Ok(DispatcherInstall::Installed) |
| 396 | } |
| 397 | Some(metadata) if metadata.file_type().is_symlink() || !metadata.file_type().is_file() => { |
| 398 | Ok(DispatcherInstall::Unmanaged) |
| 399 | } |
| 400 | Some(_) => { |
| 401 | let existing = fs::read(path)?; |
| 402 | if is_owned_dispatcher(&existing) || is_legacy_atomic_only(&existing) { |
| 403 | replace_owned_executable(path, script.as_bytes())?; |
| 404 | Ok(DispatcherInstall::Refreshed) |
| 405 | } else { |
| 406 | Ok(DispatcherInstall::Unmanaged) |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | pub(super) fn is_owned_dispatcher(content: &[u8]) -> bool { |
| 413 | let expected = format!("#!/bin/sh\n{DISPATCHER_MARKER}\n"); |