inotify事件IN_MODIFY发生两次tftp放 [英] inotify event IN_MODIFY occurring twice for tftp put

查看:586
本文介绍了inotify事件IN_MODIFY发生两次tftp放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我测试文件修改时,程序工作正常。

 #echotest> /tftpboot/.TEST 

输出:
读取16个数据
IN_MODIFY

但是当我做tftp put时,会产生两个事件:

$ $ $ $ $ $ $ c $ t> put .TEST
在0.1秒内发送6个字节
tftp>

输出:
读取16个数据
IN_MODIFY
读取16个数据
IN_MODIFY

任何想法如何避免重复通知?

代码如下:

  #include< sys / inotify.h> 
#include< stdio.h>
#include< stdlib.h>
#include< errno.h>
#include< sys / types.h>
#include< iostream>

使用namespace std;

int main(int argc,const char * argv [])
{
int fd = inotify_init();
if(fd <0)
{
cout<< inotify_init failed \\\
;
返回1;
}
int wd = inotify_add_watch(fd,/tftpboot/.TEST,IN_MODIFY);
if(wd <0)
{
cout<< inotify_add_watch failed \\\
;
返回1;

while(true)
{
char buffer [sizeof(struct inotify_event)+ NAME_MAX + 1] = {0};
ssize_t长度;


{
length = read(fd,buffer,sizeof(struct inotify_event));
cout<< 阅读<<长度<< data \\\
;
} while(length <0);

if(length <0)
{
cout< 读取失败\\\
;
返回1;
}

struct inotify_event * event =(struct inotify_event *)buffer;
if(event-> mask& IN_ACCESS)
cout<< IN_ACCESS\\\
;
if(event-> mask& IN_CLOSE_WRITE)
cout<< IN_CLOSE_WRITE\\\
;
if(event-> mask& IN_CLOSE_NOWRITE)
cout<< IN_CLOSE_NOWRITE\\\
;
if(event-> mask& IN_MODIFY)
cout<< IN_MODIFY \\\
;
if(event-> mask& IN_OPEN)
cout<< IN_OPEN\\\
;
}

inotify_rm_watch(fd,wd);
close(fd);

返回0;


解决方案

尝试使用 IN_CLOSE_WRITE 而不是


Q IN_MODIFY IN_CLOSE_WRITE
IN_MODIFY事件是在文件内容更改(例如通过
write()系统调用)时发出的,而IN_CLOSE_WRITE发生在关闭更改的
文件。这意味着每个更改操作都会导致一个IN_MODIFY事件(在打开文件的操作过程中,
可能会出现多次),而
IN_CLOSE_WRITE只发出一次(关闭文件时)。

Q :使用 IN_MODIFY IN_CLOSE_WRITE 会更好吗?

它从
的情况到大小写。通常情况下,使用IN_CLOSE_WRITE
更为合适,因为如果发出的话,相应文件上的所有更改都安全地写入文件
。 IN_MODIFY事件并不意味着文件
的变化已经完成(数据可能会保留在
应用程序中的内存缓冲区中)。另一方面,许多日志和类似的文件必须使用IN_MODIFY监视
,在这种情况下,这些文件是
永久打开,因此不能发出IN_CLOSE_WRITE。


信息来源


I am using inotify to listen to modifications to a file.

When I test file modification, program is working fine.

# echo "test" > /tftpboot/.TEST

Output:
Read 16 data
IN_MODIFY

But when I do tftp put, two events are generated:

tftp>  put .TEST
Sent 6 bytes in 0.1 seconds
tftp>

Output:
Read 16 data
IN_MODIFY
Read 16 data
IN_MODIFY

Any idea how to avoid the duplicate notification?

Code is given below:

#include <sys/inotify.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <iostream>

using namespace std;

int main(int argc, const char *argv[])
{
    int fd = inotify_init();
    if (fd < 0)
    {
        cout << "inotify_init failed\n";
        return 1;
    }
    int wd = inotify_add_watch(fd, "/tftpboot/.TEST", IN_MODIFY);
    if (wd < 0)
    {
        cout << "inotify_add_watch failed\n";
        return 1;
    }
    while (true)
    {
        char buffer[sizeof(struct inotify_event) + NAME_MAX + 1] = {0};
        ssize_t length;

        do
        {
            length = read( fd, buffer, sizeof(struct inotify_event));
            cout << "Read " << length << " data\n";
        }while (length < 0);

        if (length < 0)
        {
            cout << "read failed\n";
            return 1;
        }

        struct inotify_event *event = ( struct inotify_event * ) buffer;
        if ( event->mask & IN_ACCESS )
            cout << "IN_ACCESS\n";
        if ( event->mask & IN_CLOSE_WRITE )
            cout << "IN_CLOSE_WRITE\n";
        if ( event->mask & IN_CLOSE_NOWRITE )
            cout << "IN_CLOSE_NOWRITE\n";
        if ( event->mask & IN_MODIFY )
            cout << "IN_MODIFY \n";
        if ( event->mask & IN_OPEN )
            cout << "IN_OPEN\n";
    }

    inotify_rm_watch( fd, wd );
    close (fd);

    return 0;
}

解决方案

try using IN_CLOSE_WRITE instead

Q: What is the difference between IN_MODIFY and IN_CLOSE_WRITE?

The IN_MODIFY event is emitted on a file content change (e.g. via the write() syscall) while IN_CLOSE_WRITE occurs on closing the changed file. It means each change operation causes one IN_MODIFY event (it may occur many times during manipulations with an open file) whereas IN_CLOSE_WRITE is emitted only once (on closing the file).

Q: Is it better to use IN_MODIFY or IN_CLOSE_WRITE?

It varies from case to case. Usually it is more suitable to use IN_CLOSE_WRITE because if emitted the all changes on the appropriate file are safely written inside the file. The IN_MODIFY event needn't mean that a file change is finished (data may remain in memory buffers in the application). On the other hand, many logs and similar files must be monitored using IN_MODIFY - in such cases where these files are permanently open and thus no IN_CLOSE_WRITE can be emitted.

source

这篇关于inotify事件IN_MODIFY发生两次tftp放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆