3

My program (in C++) uses libev event loop. And I need to watch on a specific folder (say foo) for new files.

I cannot use Inotify::WaitForEvents() in block mode because I do not want to block my libev event loop. As suggested in inotify documentation,I use Inotify::SetNonBlock(true) to make it non-block. The inotify file descriptor is then passed to libev EV_STAT to watch on (as suggested in libev documentation).

The libev callback for EV_STAT is indeed called when there are new files in the folder foo. However, when I use Inotify::WaitForEvents() followed by Inotify::GetEventCount(), I get zero event.

I suspect that libev already consumed the event and convert it to EV_STAT event. If this is the case, how can I get the names of those new files?

I knew there is inode number in EV_STAT callback parameters, but getting file name from inode number is not trivial. So it is better if I can get file name instead.

Any suggestions?


EDIT

I wrote a small program to reproduce this problem. It seems the events are not lost. Instead, inotify events do not come yet when libev callback is called. The event can re-appear when you copy in a new file.

The program to reproduce the issue:

#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();
}

When I copy in a tiny file (say 300 bytes), the file is detected immediately. But if I copy a bigger file (say 500 kB), there is no event until I copy another file in and then I get two events.

The output looks like:

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
Bo Tian
  • 277
  • 2
  • 6

1 Answers1

1

I finally figured out the problem: I should use ev::io to watch the file descriptor of inotify, instead of using ev::stat to watch the folder.

In the example code, the definition of m_folderWatcher should be:

ev::io m_folderWatcher;

instead of

ev::stat m_folderWatcher;

And it should be initialized as:

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

instead of

m_folderWatcher.set(path_to_watch);
Bo Tian
  • 277
  • 2
  • 6