|
| 1 | +//! The daemon runs from its own copy in `<home>\bin`, never from the app's |
| 2 | +//! folder, so the app's installer can replace its files while the daemon |
| 3 | +//! runs (spec 14). |
| 4 | +
|
| 5 | +use std::fs; |
| 6 | +use std::io; |
| 7 | +use std::path::{Path, PathBuf}; |
| 8 | + |
| 9 | +pub const EXE_NAME: &str = if cfg!(windows) { |
| 10 | + "botloftd.exe" |
| 11 | +} else { |
| 12 | + "botloftd" |
| 13 | +}; |
| 14 | +const STAGED: &str = "botloftd.new"; |
| 15 | +const OLD_SUFFIX: &str = ".old"; |
| 16 | + |
| 17 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 18 | +pub struct Installed { |
| 19 | + pub path: PathBuf, |
| 20 | + /// Whether a different binary was put in place. |
| 21 | + pub changed: bool, |
| 22 | +} |
| 23 | + |
| 24 | +/// Copies `source` to `<bin_dir>\botloftd.exe`, unless it is that file or |
| 25 | +/// has the same bytes. A running exe cannot be overwritten but can be |
| 26 | +/// renamed, so the old one moves aside and is deleted on a later install. |
| 27 | +pub fn install(source: &Path, bin_dir: &Path) -> io::Result<Installed> { |
| 28 | + fs::create_dir_all(bin_dir)?; |
| 29 | + remove_old(bin_dir); |
| 30 | + let target = bin_dir.join(EXE_NAME); |
| 31 | + let unchanged = Installed { |
| 32 | + path: target.clone(), |
| 33 | + changed: false, |
| 34 | + }; |
| 35 | + if is_same_file(source, &target) { |
| 36 | + return Ok(unchanged); |
| 37 | + } |
| 38 | + let bytes = fs::read(source)?; |
| 39 | + match fs::read(&target) { |
| 40 | + Ok(current) if current == bytes => return Ok(unchanged), |
| 41 | + Ok(_) => {} |
| 42 | + Err(err) if err.kind() == io::ErrorKind::NotFound => {} |
| 43 | + Err(err) => return Err(err), |
| 44 | + } |
| 45 | + let staged = bin_dir.join(STAGED); |
| 46 | + fs::write(&staged, &bytes)?; |
| 47 | + if target.exists() { |
| 48 | + fs::rename(&target, aside(bin_dir))?; |
| 49 | + } |
| 50 | + fs::rename(&staged, &target)?; |
| 51 | + Ok(Installed { |
| 52 | + path: target, |
| 53 | + changed: true, |
| 54 | + }) |
| 55 | +} |
| 56 | + |
| 57 | +/// Deletes the installed binary and what was moved aside, as far as |
| 58 | +/// nothing still runs them. |
| 59 | +pub fn remove(bin_dir: &Path) { |
| 60 | + let _ = fs::remove_file(bin_dir.join(EXE_NAME)); |
| 61 | + let _ = fs::remove_file(bin_dir.join(STAGED)); |
| 62 | + remove_old(bin_dir); |
| 63 | + let _ = fs::remove_dir(bin_dir); |
| 64 | +} |
| 65 | + |
| 66 | +fn is_same_file(a: &Path, b: &Path) -> bool { |
| 67 | + match (fs::canonicalize(a), fs::canonicalize(b)) { |
| 68 | + (Ok(a), Ok(b)) => a == b, |
| 69 | + _ => false, |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/// A free `botloftd.<n>.old` name. A previous one may still be running. |
| 74 | +fn aside(bin_dir: &Path) -> PathBuf { |
| 75 | + (1..) |
| 76 | + .map(|n| bin_dir.join(format!("botloftd.{n}{OLD_SUFFIX}"))) |
| 77 | + .find(|path| !path.exists()) |
| 78 | + .unwrap_or_else(|| bin_dir.join(format!("botloftd{OLD_SUFFIX}"))) |
| 79 | +} |
| 80 | + |
| 81 | +fn remove_old(bin_dir: &Path) { |
| 82 | + let Ok(entries) = fs::read_dir(bin_dir) else { |
| 83 | + return; |
| 84 | + }; |
| 85 | + for entry in entries.flatten() { |
| 86 | + if entry.file_name().to_string_lossy().ends_with(OLD_SUFFIX) { |
| 87 | + // Still running: it goes next time. |
| 88 | + let _ = fs::remove_file(entry.path()); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +#[cfg(test)] |
| 94 | +mod tests { |
| 95 | + use super::*; |
| 96 | + |
| 97 | + fn names(dir: &Path) -> Vec<String> { |
| 98 | + let mut names: Vec<String> = fs::read_dir(dir) |
| 99 | + .expect("read dir") |
| 100 | + .map(|entry| { |
| 101 | + entry |
| 102 | + .expect("entry") |
| 103 | + .file_name() |
| 104 | + .to_string_lossy() |
| 105 | + .into_owned() |
| 106 | + }) |
| 107 | + .collect(); |
| 108 | + names.sort(); |
| 109 | + names |
| 110 | + } |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn copies_once_and_replaces_only_a_different_binary() { |
| 114 | + let dir = tempfile::tempdir().expect("tempdir"); |
| 115 | + let source = dir.path().join("sidecar.exe"); |
| 116 | + let bin = dir.path().join("bin"); |
| 117 | + fs::write(&source, b"v1").expect("write"); |
| 118 | + |
| 119 | + let first = install(&source, &bin).expect("install"); |
| 120 | + assert!(first.changed); |
| 121 | + assert_eq!(fs::read(&first.path).expect("read"), b"v1"); |
| 122 | + assert!(!install(&source, &bin).expect("again").changed); |
| 123 | + assert!( |
| 124 | + !install(&first.path, &bin).expect("itself").changed, |
| 125 | + "running install from the installed copy changes nothing" |
| 126 | + ); |
| 127 | + |
| 128 | + fs::write(&source, b"v2").expect("write"); |
| 129 | + let second = install(&source, &bin).expect("update"); |
| 130 | + assert!(second.changed); |
| 131 | + assert_eq!(fs::read(&second.path).expect("read"), b"v2"); |
| 132 | + assert_eq!(names(&bin), ["botloftd.1.old".to_owned(), EXE_NAME.into()]); |
| 133 | + |
| 134 | + fs::write(&source, b"v3").expect("write"); |
| 135 | + install(&source, &bin).expect("update"); |
| 136 | + assert_eq!( |
| 137 | + names(&bin), |
| 138 | + ["botloftd.1.old".to_owned(), EXE_NAME.into()], |
| 139 | + "the v1 copy went away and v2 moved aside" |
| 140 | + ); |
| 141 | + |
| 142 | + remove(&bin); |
| 143 | + assert!(!bin.exists()); |
| 144 | + } |
| 145 | + |
| 146 | + /// A running exe is open with share-delete: it can be renamed, not |
| 147 | + /// written. |
| 148 | + #[cfg(windows)] |
| 149 | + #[test] |
| 150 | + fn replaces_a_binary_that_is_running() { |
| 151 | + use std::os::windows::fs::OpenOptionsExt as _; |
| 152 | + const FILE_SHARE_READ: u32 = 1; |
| 153 | + const FILE_SHARE_DELETE: u32 = 4; |
| 154 | + |
| 155 | + let dir = tempfile::tempdir().expect("tempdir"); |
| 156 | + let source = dir.path().join("sidecar.exe"); |
| 157 | + let bin = dir.path().join("bin"); |
| 158 | + fs::write(&source, b"v1").expect("write"); |
| 159 | + let installed = install(&source, &bin).expect("install"); |
| 160 | + let running = fs::OpenOptions::new() |
| 161 | + .read(true) |
| 162 | + .share_mode(FILE_SHARE_READ | FILE_SHARE_DELETE) |
| 163 | + .open(&installed.path) |
| 164 | + .expect("open"); |
| 165 | + assert!( |
| 166 | + fs::write(&installed.path, b"x").is_err(), |
| 167 | + "cannot be written" |
| 168 | + ); |
| 169 | + |
| 170 | + fs::write(&source, b"v2").expect("write"); |
| 171 | + let updated = install(&source, &bin).expect("update"); |
| 172 | + assert_eq!(fs::read(&updated.path).expect("read"), b"v2"); |
| 173 | + drop(running); |
| 174 | + } |
| 175 | +} |
0 commit comments