c++ - 如何使用 Inotify 和 libev 在文件夹中获取新添加的文件?

标签 c++ inotify libev

我的程序(在 C++ 中)使用 libev 事件循环。我需要在特定文件夹(比如 foo)上查看新文件。

我不能在 block 模式下使用 Inotify::WaitForEvents() 因为我不想阻塞我的 libev 事件循环。正如 inotify documentation 中的建议,我使用 Inotify::SetNonBlock(true) 使其成为非阻塞的。然后将 inotify 文件描述符传递给 libev EV_STAT 以进行监视(如 libev documentation 中所建议)。

当文件夹 foo 中有新文件时,确实会调用 EV_STAT 的 libev 回调。然而,当我使用 Inotify::WaitForEvents() 后跟 Inotify::GetEventCount() 时,我得到了零事件。

我怀疑 libev 已经使用了该事件并将其转换为 EV_STAT 事件。如果是这种情况,我如何才能获得这些新文件的名称?

我知道 EV_STAT 回调参数中有 inode 编号,但从 inode 编号获取文件名并非易事。所以如果我能得到文件名会更好。

有什么建议吗?


编辑

我写了一个小程序来重现这个问题。看来事件并没有丢失。相反,当调用 libev 回调时,inotify 事件还没有到来。当您复制到新文件中时,事件会重新出现。

重现问题的程序:

#include <ev++.h>
#include "inotify-cxx.h"
#include <iostream>

const char * path_to_watch = "/path/to/my/folder";

class ev_inotify_test
{
    InotifyWatch m_watch;
    Inotify m_notify;

    // for watching new files
    ev::stat m_folderWatcher;

public:
    ev_inotify_test() : m_watch(path_to_watch, IN_MOVED_TO | IN_CLOSE_WRITE),
        m_notify()
    {
    }

    void run()
    {
        try {
            start();
            // run the loop
            ev::get_default_loop().run(0);
        }
        catch (InotifyException & e) {
            std::cout << e.GetMessage() << std::endl;
        }
        catch (...) {
            std::cout << "got an unknown exception." << std::endl;
        }
    }

private:

    void start()
    {
        m_notify.SetNonBlock(true);
        m_notify.Add(m_watch);

        m_folderWatcher.set<ev_inotify_test, &ev_inotify_test::cb_stat>(this);
        m_folderWatcher.set(path_to_watch);

        m_folderWatcher.start();
    }


    void cb_stat(ev::stat &w, int revents)
    {
        std::cout << "cb_stat called" << std::endl;
        try {
            m_notify.WaitForEvents();

            size_t count = m_notify.GetEventCount();

            std::cout << "inotify got " << count << " event(s).\n";

            while (count > 0) {
                InotifyEvent event;
                bool got_event = m_notify.GetEvent(&event);

                std::cout << "inotify confirm got event" << std::endl;

                if (got_event) {
                    std::string filename = event.GetName();
                    std::cout << "test: inotify got file " << filename << std::endl;
                }

                --count;
            }
        }
        catch (InotifyException &e) {
            std::cout << "inotify exception occurred: " << e.GetMessage() << std::endl;
        }
        catch (...) {
            std::cout << "Unknown exception in inotify processing occurred!" << std::endl;
        }
    }

};


int main(int argc, char ** argv)
{
    ev_inotify_test().run();
}

当我复制一个小文件(比如 300 字节)时,该文件会立即被检测到。但是如果我复制一个更大的文件(比如 500 kB),在我复制另一个文件之前没有事件,然后我得到两个事件。

输出如下:

cb_stat called     # test_file_1 (300 bytes) is copied in
inotify got 1 event(s).
inotify confirm got event
test: inotify got file test_file_1
cb_stat called     # test_file_2 (500 KB) is copied in
inotify got 0 event(s). # no inotify event
cb_stat called     # test_file_3 (300 bytes) is copied in
inotify got 2 event(s).
inotify confirm got event
test: inotify got file test_file_2
inotify confirm got event
test: inotify got file test_file_3

最佳答案

我终于想通了问题:我应该用ev::io来观察inotify的文件描述符,而不是用ev::stat来观察文件夹。

在示例代码中,m_folderWatcher的定义应该是:

ev::io m_folderWatcher;

代替

ev::stat m_folderWatcher;

它应该被初始化为:

m_folderWatcher.set(m_notify.GetDescriptor(), ev::READ);

代替

m_folderWatcher.set(path_to_watch);

关于c++ - 如何使用 Inotify 和 libev 在文件夹中获取新添加的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38506507/

相关文章:

c++ - libev,为什么在 evloop 中 recv 事件的编号是 3?

c++ - `seekg()` 和 `seekp()` 是对字符还是字节进行操作?

c++ - 如果成员是模板类,则在初始化列表中初始化某个类的成员

c++ - 如何创建一个(f)16重复n次的数字?

c++ - 与平台无关的方式来监视文件系统事件

inotify inotify_event 事件->名称为空

libevent - libev 和 libevent 有什么区别?

c - libev ev_io_stop() 不关闭套接字

c++ - C/C++ 静态 Voodoo

linux - 获取有关 cgroup 进程更改的通知?