c - How do I use the WatchDescriptor struct generated by inotify-rs to match events to files that generated them? -


i'm using inotify (through inotify-rs wrapper) watch large number of files (not directories) across filesystem.

the following inotify-rs method returns watchdescriptor struct:

add_watch<p>(&mut self, path: p, mask: watchmask) -> io::result<watchdescriptor>    p: asref<path> 

watchdescriptor newtype struct: pub struct watchdescriptor(rawfd); std::os::unix::io::rawfd type c_int/f32

the inotify subsystem returns inotify_event struct each time watch triggered:

struct inotify_event {         int      wd;       /* watch descriptor */         uint32_t mask;     /* mask describing event */         uint32_t cookie;   /* unique cookie associating related                                 events (for rename(2)) */         uint32_t len;      /* size of name field */         char     name[];   /* optional null-terminated name */     }; 

the "watch descriptor" int wd matches watch descriptor generated initial call add_watch()

in turn, inotify-rs wrapper returns following event struct:

pub struct event<'a> {         pub wd    : watchdescriptor,     pub mask  : eventmask,     pub cookie: u32,     pub name  : &'a osstr, } 

i trying use event.wd match event file list of files being watched. (.name returns filename if event triggered on watch directory) have been unsuccessful @ trying use hashmap because struct watchdescriptor not derive hash trait. tried fork crate , implement myself, opened entirely new can of worms.

the simplest way should use event.wd.0 c_int/i32 have deal error: field '0' of struct 'inotify::watchdescriptor' private

is there simple way this, short of re-writing wrapper behave way want it, or pr , wait merged?

i have considered creating inotify each file being watched, in scenario hundreds of files watched, prohibitively expensive.

there substantial issues library:

  • the poor choice of type represent inotify watch descriptor. inotify watch descriptors not unix file descriptors, integral number, scoped per inotify file descriptor. not flaw itself, view red flag.
  • as noted above, inotify watch descriptor values scoped per parent inotify file descriptor. makes use of [derive(eq)] on watchdescriptor highly questionable. looks straight-up bug.
  • there nothing ensure inotify watches closed before closing inotify descriptor. there nothing ensure watch descriptors don't reused behind (they can reused after watch descriptor number wraps). issues won't bite immediately, but… imo, entire inotify-rs should have been declared unsafe. not add actual safety on top of inotify, sugar , pointless wrappers. not implement drop watch descriptor!
  • the library nothing highlight major gotchas of inotify: event queue overflows, in_ignored , hard links sharing same inode (and same inotify watch descriptors!)

i recommend read inotify documentation , write own wrapper if necessary. library won't save trouble.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -