Watch for canonical filename, not just mtime
Browse files at this commit18566e336699fbf933dbd5584e9228b04196e893Parent: df483371 changed file +18 -4
- src/watcher.rs +18 -4
src/watcher.rsmodified+18 -4
| @@ -27,7 +27,18 @@ impl Watcher { | |||
| 27 | thread::Builder::new() | 27 | thread::Builder::new() |
| 28 | .name(format!("Filesystem Watcher for {}", path.to_string_lossy())) | 28 | .name(format!("Filesystem Watcher for {}", path.to_string_lossy())) |
| 29 | .spawn(move || { | 29 | .spawn(move || { |
| 30 | - let mut last_mtime = path.metadata().and_then(|meta| meta.modified()).ok(); | 30 | + // this "should" be as simple as mtime, but it does not quite work in practice; |
| 31 | + // it doesn't work if the config is a symlink, and its target changes but the | ||
| 32 | + // new target and old target have identical mtimes. | ||
| 33 | + // | ||
| 34 | + // in practice, this does not occur on any systems other than nix. | ||
| 35 | + // because, on nix practically everything is a symlink to /nix/store | ||
| 36 | + // and due to reproducibility, /nix/store keeps no mtime (= 1970-01-01) | ||
| 37 | + // so, symlink targets change frequently when mtime doesn't. | ||
| 38 | + let mut last_props = path | ||
| 39 | + .canonicalize() | ||
| 40 | + .and_then(|canon| Ok((canon.metadata()?.modified()?, canon))) | ||
| 41 | + .ok(); | ||
| 31 | 42 | ||
| 32 | loop { | 43 | loop { |
| 33 | thread::sleep(Duration::from_millis(500)); | 44 | thread::sleep(Duration::from_millis(500)); |
| @@ -36,8 +47,11 @@ impl Watcher { | |||
| 36 | break; | 47 | break; |
| 37 | } | 48 | } |
| 38 | 49 | ||
| 39 | - if let Ok(mtime) = path.metadata().and_then(|meta| meta.modified()) { | 50 | + if let Ok(new_props) = path |
| 40 | - if last_mtime != Some(mtime) { | 51 | + .canonicalize() |
| 52 | + .and_then(|canon| Ok((canon.metadata()?.modified()?, canon))) | ||
| 53 | + { | ||
| 54 | + if last_props.as_ref() != Some(&new_props) { | ||
| 41 | trace!("file changed: {}", path.to_string_lossy()); | 55 | trace!("file changed: {}", path.to_string_lossy()); |
| 42 | 56 | ||
| 43 | if let Err(err) = changed.send(()) { | 57 | if let Err(err) = changed.send(()) { |
| @@ -45,7 +59,7 @@ impl Watcher { | |||
| 45 | break; | 59 | break; |
| 46 | } | 60 | } |
| 47 | 61 | ||
| 48 | - last_mtime = Some(mtime); | 62 | + last_props = Some(new_props); |
| 49 | } | 63 | } |
| 50 | } | 64 | } |
| 51 | } | 65 | } |