hannobraun / inotify-rs

Idiomatic inotify wrapper for the Rust programming language
ISC License
254 stars 64 forks source link

Is it possible to get full path from Event? #192

Closed serzhiio closed 2 years ago

hannobraun commented 2 years ago

inotify-rs is just a simple layer on top of inotify, so it just returns whatever it gets from the Linux kernel. According to the inotify man page that's "the filename within the watched directory", so not the full path.

You should be able to get the full path yourself, by using the watch descriptor of the event to look up the path you watched and combine it with the file name you get from the event. Or maybe notify can help, as that's a higher-level and platform-independent library that uses inotify-rs internally. I don't know how it handles this case though.

I'm closing this issue, as I believe I've answered the question. But feel free to post another comment here, if there's anything else.

serzhiio commented 2 years ago

Many thanks, very useful response!

theguy147 commented 2 years ago

I had the same question and stumbled upon this issue and wanted to add the following for reference if somebody else sees this:

When watching a directory the event.name returns only the name of the file (or dir) without being relative to the watched directory. Therefore concatenating them on the base of the watch descriptor may lead to incorrect absolute paths. Example:

We are watching /tmp/ with inotify-rs and inside the subdirectory foo a file bar is created (the absolute path of the newly created file being /tmp/foo/bar). In this scenario event.name returns Some("bar") and concatenating this to the watched dir /tmp yields the path /tmp/bar. This is not the correct absolute filepath though.

I guess instead one needs to search the newly created file (event.name) recursively inside the watched dir to find the absolute path (which might not yield any result if the file does not exist anymore at that point in time).