niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·6c9705d·Ivan Molodetskikh·1 year ago·2085 commits
| 1 | //! File modification watcher. |
| 2 | |
| 3 | use std::path::{Path, PathBuf}; |
| 4 | use std::sync::atomic::{AtomicBool, Ordering}; |
| 5 | use std::sync::{mpsc, Arc}; |
| 6 | use std::thread; |
| 7 | use std::time::Duration; |
| 8 | |
| 9 | use smithay::reexports::calloop::channel::SyncSender; |
| 10 | |
| 11 | pub struct Watcher { |
| 12 | should_stop: Arc<AtomicBool>, |
| 13 | } |
| 14 | |
| 15 | impl Drop for Watcher { |
| 16 | fn drop(&mut self) { |
| 17 | self.should_stop.store(true, Ordering::SeqCst); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | impl Watcher { |
| 22 | pub fn new<T: Send + 'static>( |
| 23 | path: PathBuf, |
| 24 | process: impl FnMut(&Path) -> T + Send + 'static, |
| 25 | changed: SyncSender<T>, |
| 26 | ) -> Self { |
| 27 | Self::with_start_notification(path, process, changed, None) |
| 28 | } |
| 29 | |
| 30 | pub fn with_start_notification<T: Send + 'static>( |
| 31 | path: PathBuf, |
| 32 | mut process: impl FnMut(&Path) -> T + Send + 'static, |
| 33 | changed: SyncSender<T>, |
| 34 | started: Option<mpsc::SyncSender<()>>, |
| 35 | ) -> Self { |
| 36 | let should_stop = Arc::new(AtomicBool::new(false)); |
| 37 | |
| 38 | { |
| 39 | let should_stop = should_stop.clone(); |
| 40 | thread::Builder::new() |
| 41 | .name(format!("Filesystem Watcher for {}", path.to_string_lossy())) |
| 42 | .spawn(move || { |
| 43 | // this "should" be as simple as mtime, but it does not quite work in practice; |
| 44 | // it doesn't work if the config is a symlink, and its target changes but the |
| 45 | // new target and old target have identical mtimes. |
| 46 | // |
| 47 | // in practice, this does not occur on any systems other than nix. |
| 48 | // because, on nix practically everything is a symlink to /nix/store |
| 49 | // and due to reproducibility, /nix/store keeps no mtime (= 1970-01-01) |
| 50 | // so, symlink targets change frequently when mtime doesn't. |
| 51 | let mut last_props = path |
| 52 | .canonicalize() |
| 53 | .and_then(|canon| Ok((canon.metadata()?.modified()?, canon))) |
| 54 | .ok(); |
| 55 | |
| 56 | if let Some(started) = started { |
| 57 | let _ = started.send(()); |
| 58 | } |
| 59 | |
| 60 | loop { |
| 61 | thread::sleep(Duration::from_millis(500)); |
| 62 | |
| 63 | if should_stop.load(Ordering::SeqCst) { |
| 64 | break; |
| 65 | } |
| 66 | |
| 67 | if let Ok(new_props) = path |
| 68 | .canonicalize() |
| 69 | .and_then(|canon| Ok((canon.metadata()?.modified()?, canon))) |
| 70 | { |
| 71 | if last_props.as_ref() != Some(&new_props) { |
| 72 | trace!("file changed: {}", path.to_string_lossy()); |
| 73 | |
| 74 | let rv = process(&path); |
| 75 | |
| 76 | if let Err(err) = changed.send(rv) { |
| 77 | warn!("error sending change notification: {err:?}"); |
| 78 | break; |
| 79 | } |
| 80 | |
| 81 | last_props = Some(new_props); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | debug!("exiting watcher thread for {}", path.to_string_lossy()); |
| 87 | }) |
| 88 | .unwrap(); |
| 89 | } |
| 90 | |
| 91 | Self { should_stop } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | #[cfg(test)] |
| 96 | mod tests { |
| 97 | use std::error::Error; |
| 98 | use std::fs::File; |
| 99 | use std::io::Write; |
| 100 | use std::sync::atomic::AtomicU8; |
| 101 | |
| 102 | use calloop::channel::sync_channel; |
| 103 | use calloop::EventLoop; |
| 104 | use smithay::reexports::rustix::fs::{futimens, Timestamps}; |
| 105 | use smithay::reexports::rustix::time::Timespec; |
| 106 | use xshell::{cmd, Shell}; |
| 107 | |
| 108 | use super::*; |
| 109 | |
| 110 | fn check( |
| 111 | setup: impl FnOnce(&Shell) -> Result<(), Box<dyn Error>>, |
| 112 | change: impl FnOnce(&Shell) -> Result<(), Box<dyn Error>>, |
| 113 | ) { |
| 114 | let sh = Shell::new().unwrap(); |
| 115 | let temp_dir = sh.create_temp_dir().unwrap(); |
| 116 | sh.change_dir(temp_dir.path()); |
| 117 | // let dir = sh.create_dir("xshell").unwrap(); |
| 118 | // sh.change_dir(dir); |
| 119 | |
| 120 | let mut config_path = sh.current_dir(); |
| 121 | config_path.push("niri"); |
| 122 | config_path.push("config.kdl"); |
| 123 | |
| 124 | setup(&sh).unwrap(); |
| 125 | |
| 126 | let changed = AtomicU8::new(0); |
| 127 | |
| 128 | let mut event_loop = EventLoop::try_new().unwrap(); |
| 129 | let loop_handle = event_loop.handle(); |
| 130 | |
| 131 | let (tx, rx) = sync_channel(1); |
| 132 | let (started_tx, started_rx) = mpsc::sync_channel(1); |
| 133 | let _watcher = |
| 134 | Watcher::with_start_notification(config_path.clone(), |_| (), tx, Some(started_tx)); |
| 135 | loop_handle |
| 136 | .insert_source(rx, |_, _, _| { |
| 137 | changed.fetch_add(1, Ordering::SeqCst); |
| 138 | }) |
| 139 | .unwrap(); |
| 140 | started_rx.recv().unwrap(); |
| 141 | |
| 142 | // HACK: if we don't sleep, files might have the same mtime. |
| 143 | thread::sleep(Duration::from_millis(100)); |
| 144 | |
| 145 | change(&sh).unwrap(); |
| 146 | |
| 147 | event_loop |
| 148 | .dispatch(Duration::from_millis(750), &mut ()) |
| 149 | .unwrap(); |
| 150 | |
| 151 | assert_eq!(changed.load(Ordering::SeqCst), 1); |
| 152 | |
| 153 | // Verify that the watcher didn't break. |
| 154 | sh.write_file(&config_path, "c").unwrap(); |
| 155 | |
| 156 | event_loop |
| 157 | .dispatch(Duration::from_millis(750), &mut ()) |
| 158 | .unwrap(); |
| 159 | |
| 160 | assert_eq!(changed.load(Ordering::SeqCst), 2); |
| 161 | } |
| 162 | |
| 163 | #[test] |
| 164 | fn change_file() { |
| 165 | check( |
| 166 | |sh| { |
| 167 | sh.write_file("niri/config.kdl", "a")?; |
| 168 | Ok(()) |
| 169 | }, |
| 170 | |sh| { |
| 171 | sh.write_file("niri/config.kdl", "b")?; |
| 172 | Ok(()) |
| 173 | }, |
| 174 | ); |
| 175 | } |
| 176 | |
| 177 | #[test] |
| 178 | fn create_file() { |
| 179 | check( |
| 180 | |sh| { |
| 181 | sh.create_dir("niri")?; |
| 182 | Ok(()) |
| 183 | }, |
| 184 | |sh| { |
| 185 | sh.write_file("niri/config.kdl", "a")?; |
| 186 | Ok(()) |
| 187 | }, |
| 188 | ); |
| 189 | } |
| 190 | |
| 191 | #[test] |
| 192 | fn create_dir_and_file() { |
| 193 | check( |
| 194 | |_sh| Ok(()), |
| 195 | |sh| { |
| 196 | sh.write_file("niri/config.kdl", "a")?; |
| 197 | Ok(()) |
| 198 | }, |
| 199 | ); |
| 200 | } |
| 201 | |
| 202 | #[test] |
| 203 | fn change_linked_file() { |
| 204 | check( |
| 205 | |sh| { |
| 206 | sh.write_file("niri/config2.kdl", "a")?; |
| 207 | cmd!(sh, "ln -s config2.kdl niri/config.kdl").run()?; |
| 208 | Ok(()) |
| 209 | }, |
| 210 | |sh| { |
| 211 | sh.write_file("niri/config2.kdl", "b")?; |
| 212 | Ok(()) |
| 213 | }, |
| 214 | ); |
| 215 | } |
| 216 | |
| 217 | #[test] |
| 218 | fn change_file_in_linked_dir() { |
| 219 | check( |
| 220 | |sh| { |
| 221 | sh.write_file("niri2/config.kdl", "a")?; |
| 222 | cmd!(sh, "ln -s niri2 niri").run()?; |
| 223 | Ok(()) |
| 224 | }, |
| 225 | |sh| { |
| 226 | sh.write_file("niri2/config.kdl", "b")?; |
| 227 | Ok(()) |
| 228 | }, |
| 229 | ); |
| 230 | } |
| 231 | |
| 232 | #[test] |
| 233 | fn recreate_file() { |
| 234 | check( |
| 235 | |sh| { |
| 236 | sh.write_file("niri/config.kdl", "a")?; |
| 237 | Ok(()) |
| 238 | }, |
| 239 | |sh| { |
| 240 | sh.remove_path("niri/config.kdl")?; |
| 241 | sh.write_file("niri/config.kdl", "b")?; |
| 242 | Ok(()) |
| 243 | }, |
| 244 | ); |
| 245 | } |
| 246 | |
| 247 | #[test] |
| 248 | fn recreate_dir() { |
| 249 | check( |
| 250 | |sh| { |
| 251 | sh.write_file("niri/config.kdl", "a")?; |
| 252 | Ok(()) |
| 253 | }, |
| 254 | |sh| { |
| 255 | sh.remove_path("niri")?; |
| 256 | sh.write_file("niri/config.kdl", "b")?; |
| 257 | Ok(()) |
| 258 | }, |
| 259 | ); |
| 260 | } |
| 261 | |
| 262 | #[test] |
| 263 | fn swap_dir() { |
| 264 | check( |
| 265 | |sh| { |
| 266 | sh.write_file("niri/config.kdl", "a")?; |
| 267 | Ok(()) |
| 268 | }, |
| 269 | |sh| { |
| 270 | sh.write_file("niri2/config.kdl", "b")?; |
| 271 | sh.remove_path("niri")?; |
| 272 | cmd!(sh, "mv niri2 niri").run()?; |
| 273 | Ok(()) |
| 274 | }, |
| 275 | ); |
| 276 | } |
| 277 | |
| 278 | #[test] |
| 279 | fn swap_just_link() { |
| 280 | // NixOS setup: link path changes, mtime stays constant. |
| 281 | check( |
| 282 | |sh| { |
| 283 | let mut dir = sh.current_dir(); |
| 284 | dir.push("niri"); |
| 285 | sh.create_dir(&dir)?; |
| 286 | |
| 287 | let mut d2 = dir.clone(); |
| 288 | d2.push("config2.kdl"); |
| 289 | let mut c2 = File::create(d2).unwrap(); |
| 290 | write!(c2, "a")?; |
| 291 | c2.flush()?; |
| 292 | futimens( |
| 293 | &c2, |
| 294 | &Timestamps { |
| 295 | last_access: Timespec { |
| 296 | tv_sec: 0, |
| 297 | tv_nsec: 0, |
| 298 | }, |
| 299 | last_modification: Timespec { |
| 300 | tv_sec: 0, |
| 301 | tv_nsec: 0, |
| 302 | }, |
| 303 | }, |
| 304 | )?; |
| 305 | c2.sync_all()?; |
| 306 | drop(c2); |
| 307 | |
| 308 | let mut d3 = dir.clone(); |
| 309 | d3.push("config3.kdl"); |
| 310 | let mut c3 = File::create(d3).unwrap(); |
| 311 | write!(c3, "b")?; |
| 312 | c3.flush()?; |
| 313 | futimens( |
| 314 | &c3, |
| 315 | &Timestamps { |
| 316 | last_access: Timespec { |
| 317 | tv_sec: 0, |
| 318 | tv_nsec: 0, |
| 319 | }, |
| 320 | last_modification: Timespec { |
| 321 | tv_sec: 0, |
| 322 | tv_nsec: 0, |
| 323 | }, |
| 324 | }, |
| 325 | )?; |
| 326 | c3.sync_all()?; |
| 327 | drop(c3); |
| 328 | |
| 329 | cmd!(sh, "ln -s config2.kdl niri/config.kdl").run()?; |
| 330 | Ok(()) |
| 331 | }, |
| 332 | |sh| { |
| 333 | cmd!(sh, "unlink niri/config.kdl").run()?; |
| 334 | cmd!(sh, "ln -s config3.kdl niri/config.kdl").run()?; |
| 335 | Ok(()) |
| 336 | }, |
| 337 | ); |
| 338 | } |
| 339 | |
| 340 | #[test] |
| 341 | fn swap_dir_link() { |
| 342 | check( |
| 343 | |sh| { |
| 344 | sh.write_file("niri2/config.kdl", "a")?; |
| 345 | cmd!(sh, "ln -s niri2 niri").run()?; |
| 346 | Ok(()) |
| 347 | }, |
| 348 | |sh| { |
| 349 | sh.write_file("niri3/config.kdl", "b")?; |
| 350 | cmd!(sh, "unlink niri").run()?; |
| 351 | cmd!(sh, "ln -s niri3 niri").run()?; |
| 352 | Ok(()) |
| 353 | }, |
| 354 | ); |
| 355 | } |
| 356 | } |