如何使用Inotify和libev在文件夹中获取新添加的文件
How to get new added files in a folder using Inotify along with libev?
我的程序(在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);
- 将新元素添加到列表中,并返回对该元素的引用?
- 将新值添加到链表中
- QML ListView将新行添加到模型中的子向量
- 将新元素添加到动态数组C
- 推送功能(将新节点添加到列表顶部)C
- 如何防止 clang 格式向新行添加单个分号
- 删除前面元素并将新元素添加到最小堆 (std::p riority_queue)
- C++链表将新节点添加到列表的开头
- CMAKE将新项目添加到解决方案中
- OpenGL-将新数据添加到纹理中
- QTime为新对象添加秒
- 如何知道何时将新文件添加到 Windows 中的文件系统
- 将新节点添加到列表中并动态命名
- C++:尝试将新节点添加到链表会产生"线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x0)"错误
- Makefile.am:如何为新扩展添加规则
- 当我尝试将新节点添加到列表中时,我收到错误消息"no matching function call"
- 将新条目添加到地图列表中
- 将新节点添加到具有两个类的链表中
- 无法理解如何将新对象添加到 std::list<std::unique_ptr<classname>>
- 在数组的新位置添加一个新的类对象