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

How to get new added files in a folder using Inotify along with libev?

本文关键字:新添加 添加 文件 获取 Inotify 何使用 libev 文件夹      更新时间:2023-10-16

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

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

当文件夹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字节)中复制时,会立即检测到该文件。但是,如果我复制一个更大的文件(比如500kB),在我复制另一个文件之前没有事件,然后我得到两个事件。

输出看起来像:

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);