我正在使用Inotify(通过inotify-rs包装器)在文件系统中观看大量文件(不是目录)。如何使用inotify-rs生成的WatchDescriptor结构将事件与生成它们的文件进行匹配?
以下的inotify-RS方法返回一个WatchDescriptor
结构:
add_watch<P>(&mut self, path: P, mask: WatchMask) -> io::Result<WatchDescriptor>
where
P: AsRef<Path>
WatchDescriptor
是NEWTYPE结构:pub struct WatchDescriptor(RawFd);
其中std::os::unix::io::RawFd
是类型c_int
/f32
对于inotify子系统返回inotify_event
结构每次手表被触发:
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 */
};
“监视描述符” int wd
由初始调用生成的表描述符匹配到add_watch()
反过来,inotify的-RS包装返回以下事件的结构:
pub struct Event<'a> {
pub wd : WatchDescriptor,
pub mask : EventMask,
pub cookie: u32,
pub name : &'a OsStr,
}
我试图用Event.wd
将事件与正在观看的文件列表中的文件进行匹配。 (.name
只返回一个文件名,如果一个目录手表上触发的事件)我试图使用HashMap
,因为结构WatchDescriptor
没有派生Hash
特质,所以我没有成功。我试图撬开箱子并自己执行,但是这打开了一个全新的蠕虫罐。
最简单的方法应该是使用Event.wd.0
获得c_int
/i32
但后来我不得不面对error: field '0' of struct 'inotify::WatchDescriptor' is private
有没有一种简单的方法来做到这一点,短的重写包装上,以行为方式我想要它,或者做一个公关并等待它被合并?
我已经考虑为每个正在观看的文件创建一个Inotify
,但是在可以观看数百个文件的情况下,这将非常昂贵。
鉴于'Event'具有'.name',你不能用它来映射回文件吗? – loganfsmyth
loganfsmyth:如果正在监视一个目录,'.name'只返回一个文件名。编辑的问题包括该信息。 – arsalan86