notify-rs / notify

🔭 Cross-platform filesystem notification library for Rust.
https://docs.rs/notify
2.76k stars 222 forks source link

Watch on non-existing file #580

Closed tisonkun closed 6 months ago

tisonkun commented 8 months ago

My scenario is to watch a credential file; if not exist, bypass authentication; otherwise read the content to build the map.

I found that if the file exists and later deleted and recreated, all the loading can happen properly. But notify requires the file to exist initially:

    // https://github.com/thibaudgg/rb-fsevent/blob/master/ext/fsevent_watch/main.c
    fn append_path(&mut self, path: &Path, recursive_mode: RecursiveMode) -> Result<()> {
        if !path.exists() {
            return Err(Error::path_not_found().add_path(path.into()));
        }

Since we can anyway delete the file later it starts, I wonder if we can relax this condition (or at lease when RecursiveMode::NonRecursive) so that make it possible to watch on a maybe non-existing file.

cc @0xpr03

tisonkun commented 8 months ago

Seems it depends on the OS whether events can be emitted - https://github.com/fsnotify/fsnotify/issues/49

dfaust commented 6 months ago

I suggest you watch the parent folder. Does that work for you?

tisonkun commented 6 months ago

Yes. This is how I finally worked it out - https://github.com/GreptimeTeam/greptimedb/pull/3566/commits/408f8df934313f892b52abfdf55ec3a5ace7e6bb (among https://github.com/GreptimeTeam/greptimedb/pull/3566)