[英]Use INotify to watch a file with multiple symlinks
So I setup some code to watch a config file for edits, which worked until I used VIM to edit the file, then I had to also watch the directory for renames and creations. 所以我设置了一些代码来观看编辑的配置文件,直到我使用VIM编辑文件,然后我还必须查看重命名和创建的目录。 Then I discovered that didn't catch renames higher in the path hierarchy.
然后我发现在路径层次结构中没有捕获更高的重命名。 Then I looked into symlinks...gaaahhhh!
然后我看了一下符号链接...... gaaahhhh!
First setup a made up example showing one (of many) tricky symlink scenarios: 首先设置一个示例,显示一个(很多)棘手的符号链接场景:
mkdir config1
touch config1/config
ln -s config1 machine1
mkdir config2
touch config2/config
ln -s config2 machine2
ln -s machine1 active
Now, given a filename like active/config that I want to watch, I can see how to get an inotify watch descriptor for: 现在,给定一个我想要观看的像active / config这样的文件名,我可以看到如何获取inotify监视描述符:
config1/ -> watch active/ follow symlinks (watches inode for config1)
active/ -> watch active/ dont follow symlinks (watches inode for active symlink
active/config -> watch active/config (watches inode for config1/config)
How do I add a watch on the machine1 symlink? 如何在machine1符号链接上添加手表? Do I need to find some way to manually walk each symlink adding watches for each along the way?
我是否需要找到一些方法来手动遍历每个符号链接为每个添加手表? How?
怎么样?
The purpose is to allow: 目的是允许:
mkdir config3
touch config3/config
ln -s -f -n config3 machine1
And have inotify warn that active/config has been redirected. 并且有inotify警告active / config已被重定向。 At the moment it looks like I'll have to add a watch for:
目前看起来我将不得不添加一个手表:
- target file inode
- every directory inode leading to the file (to detect moves/renames of directories)
- every symlink inode involved in reaching any of the above
There must be an easier way to just watch one file? 必须有一种更简单的方法来观看一个文件? Have I strayed from the path or is this really the way?
我是偏离了这条道路还是真的这样?
My answer is a straight "yes, you are doing it right". 我的答案是直接的“是的,你做得对”。
After carefully reading the inotify syscall manpage , I cannot see any way of short of watching every step of a (possibly-symlinked) path-to-a-file in order to detect any and all changes to the full path. 仔细阅读inotify系统调用联机帮助页后 ,我无法看到任何方法,无法查看(可能是符号链接的)路径到文件的每一步,以便检测完整路径的任何和所有更改。
This just seems to be the way inotify
works: it only looks at specific files or folders, and it does not do recursion on its own. 这似乎是
inotify
工作的方式:它只查看特定的文件或文件夹,它不会自己进行递归。 That, plus having to explicitly follow symlinks, seems to match your 3-step plan to the letter. 这一点,加上必须明确遵循符号链接,似乎与你的三步计划相符。
Selected quotes from the manpage: 从联机帮助页中选择的引号:
The following further bits can be specified in mask when calling inotify_add_watch(2): IN_DONT_FOLLOW (since Linux 2.6.15)
调用inotify_add_watch(2)时,可以在掩码中指定以下其他位:IN_DONT_FOLLOW(自Linux 2.6.15起)
Don't dereference pathname if it is a symbolic link.
如果路径名是符号链接,请不要取消引用路径名。
[...] [...]
Limitations and caveats
限制和警告
Inotify monitoring of directories is not recursive: to monitor subdirectories under a directory, additional watches must be created.
对目录的Inotify监视不是递归的:要监视目录下的子目录,必须创建其他监视。 This can take a significant amount time for large directory trees.
对于大型目录树,这可能需要相当长的时间。 [...]
[...]
This FAQ also lends support for your strategy re symlinks: 此常见问题解答还支持您的策略重新符号链接:
Q: What about the IN_ONLYDIR and IN_DONT_FOLLOW flags?
问:IN_ONLYDIR和IN_DONT_FOLLOW标志怎么样? IN_ONLYDIR ensures that the event occur only on a directory.
IN_ONLYDIR确保事件仅在目录上发生。 If you create such watch on a file it will not emit events.
如果在文件上创建此类监视,则不会发出事件。 IN_DONT_FOLLOW forbids following symbolic links (these ones will be monitored themselves and not the files they point to).
IN_DONT_FOLLOW禁止跟随符号链接(这些链接将被自己监视,而不是它们指向的文件)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.