- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我一直在研究inotify调用,但是在读取接口(interface)的时候还是有点吃力。这些是我能找到的关于如何使用 read(2) 正确连接 inotify 的最相关资源:
他们都以相同的方式实现它,他们首先定义以下尺寸:
#define EVENT_SIZE ( sizeof (struct inotify_event) )
#define BUF_LEN ( 1024 * ( EVENT_SIZE + 16 )
然后他们以这种方式使用它们:
length = read( fd, buffer, BUF_LEN );
if ( length < 0 ) {
perror( "read" );
}
while ( i < length ) {
struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
/* some processing */
i += EVENT_SIZE + event->len;
}
现在,我们知道 name 是 struct inotify_event
的一部分,并且它的长度是可变的。那么,buffer 中的最后一个 inotify_event 不能被截断吗?
假设有1023个inotify_events,路径为16字节,一个路径为32字节。那么会发生什么?后期会不会被截断?或者内核会发现它不适合缓冲区并完全保留它吗?
最佳答案
根据 inotify(7) ,您可以使用 FIONREAD ioctl 找出有多少数据可供读取并相应地调整缓冲区大小。这里有一些(非常粗略的)代码可以完成这个:
unsigned int avail;
ioctl(inotify_fd, FIONREAD, &avail);
char buffer[avail];
read(fd, buffer, avail);
int offset = 0;
while (offset < avail) {
struct inotify_event *event = (inotify_event*)(buffer + offset);
// Insert logic here
my_process_inotify_event(event);
offset = offset + sizeof(inotify_event) + event->len;
}
inotify-tools为 inotify 提供更高级别的接口(interface)。您可以使用它来代替访问 inotify,或者您可以查看它如何实现 inotifytools_next_events安全可靠地读取所有可用事件。
针对您关于截断的问题,我认为如果给定的缓冲区对于所有事件来说都太小,内核将永远不会返回部分 inotify_event 或截断 inotify_event。 inotify(7) 联机帮助页中的以下段落表明了这一点:
The behavior when the buffer given to read(2) is too small to return information about the next event depends on the kernel version: in kernels before 2.6.21, read(2) returns 0; since kernel 2.6.21, read(2) fails with the error EINVAL.
来自 inotifytools.c 的以下评论也是如此:
// oh... no. this can't be happening. An incomplete event.
// Copy what we currently have into first element, call self to
// read remainder.
// oh, and they BETTER NOT overlap.
// Boy I hope this code works.
// But I think this can never happen due to how inotify is written.
关于c - 将读取与 inotify 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5211993/
如何解决以下问题?我正在使用 compass 。 Too many open files - Failed to initialize inotify: the user limit on th
我最终遇到了这样一种情况:一个目录正在被 inotify 监视,以触发一个不存在的进程。 我只是想阻止inotify监视目录,但是经过搜索和reading the man page我找不到如何做到这一
1.1 什么是实时同步:如何实现实时同步 要利用监控服务(inotify),监控同步数据服务器目录中信息的变化 发现目录中数据产生变化,就利用rsync服务推送到备份服务器上 1.2 实现实时
我正在尝试使用 inotify 递归地查看目录,这意味着,每当在此目录或其中一个子目录中创建目录或文件时,我都希望收到通知。程序开始时主目录为空。 我基本上是从这里复制代码进行测试:https://g
我正在编写一个基于源代码的监控文件程序:https://github.com/kvikas/file-monitor-service/blob/master/ 我的程序使用 boost::asio::
我是 Fedora 用户,目前正在使用 inotify-java 来实现目录轮询。我有以下代码片段: InotifyEventListener inel = new InotifyEventListe
我有一个inotify /内核问题。我正在使用“inotify” Python项目进行观察,但是,我的问题仍然是固有的关于inotify内核实现的核心。 Python inotify项目处理递归ino
我正在用 C 语言编写一个简单的线程来监视目录内文件的修改。该目录中有多个文件,其中一些文件经常更改。我想监视一些特定文件。 我仔细阅读了Linux手册,但没有找到答案谢谢问候帕特里夏 最佳答案 您无
我的程序使用 inotify用于监视文件的更改。我的代码如下: fd = inotify_init(); wd = inotify_add_watch(fd, "./test.txt"
我正在繁忙的目录上使用inotify(文件不断生成)。我希望捕获所有这些事件的 IN_CLOSE_WRITE 事件。 所以我尝试了这样的事情。 fd = inotify_init(); inotify
我尝试在线程内的 C++ 中使用 inotify但是 select 是阻塞的,所以当我的应用程序退出时我永远不能离开线程 我如何创建 inotify watch fd=inotify_init1(IN
我想监控一个文件夹。 每次弹出通知时,我都想运行系统命令行。使用系统命令时出现问题。每个新事件弹出 3 次,尽管它应该弹出一次。编辑:谢谢你重播。我发现了这个错误。系统执行了一个位于受监控文件夹内的文
我想监控某些目录中新文件的创建并阅读有关 inotify 的链接.我喜欢这个实现并使用了它。然而,在我的例子中,我想监控一个最多有 3 级子目录的目录。 我的想法是每次创建一个新目录时添加一个 wat
删除同一目录下的文件时,事件丢失。 inotify监控目录:./test删除命令:rm -rf ./test/* 文件数:20; 但是事件通知12,这是怎么回事? int fileDescripter
所以我使用 inotifywait 运行了这个脚本。一台服务器将图像放入位于/var/nfs/device_images 的主机服务器上的 NFS 文件夹中。 (工作文件夹上的 chmod 是 777
例如,假设您有一个以下列形式使用 inotifywait 的 shell 脚本 inotifywait -m -e create /home/user1/*/* 本质上,您是在要求它监控 user1
我在 Linux 上使用 inotify 来监视目录。我的目录有 50 个子目录,目录树有 20 层深。到目前为止,我能够监视目录及其所有子目录。我使用 C++ STL vector 来保存每个目录路
我的 Linux 服务器上有 inotify。我在网上查找了大量关于如何使用 inotify 的帖子,并找到了一个示例 C 代码,用于监视文件创建/删除的目录。它在本地目录和 nfs direcory
我正在寻找一个使用 inotify 的简单、简洁的示例gem 来检测目录的更改。 它缺少示例。 最佳答案 examples/watcher.rb 中有一个示例.该链接指向 aredridel 的 re
所以我有一个文件系统,它正在下载一些数据,将其存储在内存中,并仅将已完成的下载作为文件呈现给用户。但是,每次下载可能需要一些时间才能完成,因此我不希望用户必须等待所有文件完成下载。我执行此操作的方法是
我是一名优秀的程序员,十分优秀!