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

问题描述



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

  

尝试使用 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

source

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

10-13 17:22