Can iNotify tell me where a monitored file is moved?

亡梦爱人 提交于 2019-12-21 20:58:58

问题


I want to monitor a file while it's moving in the system. Can iNotify tell me its new position whenever it's moves?


回答1:


If you're watching both the directory the file was moved from, and the directory the file was moved to, then you will receive an IN_MOVED_FROM event on the source directory and an IN_MOVED_TO event on the target directory, both with the same cookie. You can then use the name fields of the two events to find out where the file was moved to and from.

If you're only watching the source directory, or only the target directory, then you will only get one of the events, so you will only have half of the info. This is a limitation of inotify.




回答2:


You can grab a file descriptor to the file before the move and read the symlink at:

'/proc/self/fd/' + $fd

where $fd is your file descriptor, this file descriptor will point to your file. Note I have only tested this on ext4 and it works with LVM2, but does not work with OverlayFS. Also opening a file descriptor will block remove events from being fired for the file.

There may also be issues between linux kernel versions



来源:https://stackoverflow.com/questions/5000880/can-inotify-tell-me-where-a-monitored-file-is-moved

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!