我正在尝试使用 inotify 监视目录,并且正在注册所有事件。现在,我的项目需要跟踪对目录执行的任何 MOVE_SELF 操作,以便我应该能够检测到受监视目录移动到哪个新位置。为了实现这一点,我存储了受监视目录的打开文件描述符(int fd)的引用,当我得到 MOVE_SELF 时,我尝试使用以下方法获取新路径:
//code to store a reference of file-descrptor of the monitored sirectory
fd = open(watchPath.c_str(), O_RDONLY)
//code to learn the new location of the moved directory
char fdpath[4096];
char path[4096];
sprintf(fdpath, "/proc/self/fd/%d", fd);
ssize_t sz = readlink(fdpath, path, sizeof(path) - 1); //Path will contain the new location after the move happens
但是这样做的副作用是,如果我删除目录,我不会收到 DELETE_SELF 事件,因为我仍然持有一个打开的文件描述符。谁能建议我如何解决这个问题?
谢谢,-桑迪普