Gary Willoughby
Gary Willoughby

Reputation: 52608

Using inotify why is my watched file ignored?

I'm writing a file watcher in D on Linux using the inotify subsystem for event notification. I've written some code below and i'm nearly there but i have a problem that the watched file is switched to ignored and no more events are raised for it.

It looks like this guy is having a similar problem but there are no answers there: Inotify vim modification

Consider the following code:

/**
 * Imports.
 */
import core.sys.posix.unistd;
import core.sys.posix.sys.select;
import inotify;
import std.stdio;

/**
 * Main.
 */
void main(string[] arguments)
{
    immutable int EVENT_BUF_LEN = (1024 * (inotify_event.sizeof + 16));

    auto inotifyInstance = inotify_init();

    if (inotifyInstance >= 0)
    {
        string watchedFile = "/home/gary/Desktop/test.txt";
        char[EVENT_BUF_LEN] buffer;
        void* pointer;

        auto watchDescriptor = inotify_add_watch(inotifyInstance, cast(char*)watchedFile, IN_ALL_EVENTS);

        while (true)
        {
            fd_set rfds;
            FD_ZERO(&rfds);
            FD_SET(inotifyInstance, &rfds);

            timeval timeout;
            timeout.tv_sec  = 5;
            timeout.tv_usec = 0;

            if (select(FD_SETSIZE, &rfds, null, null, &timeout))
            {
                auto inotifyResult = read(inotifyInstance, buffer.ptr, buffer.length);

                for (pointer = buffer.ptr; pointer < buffer.ptr + inotifyResult; )
                {
                    inotify_event* event = cast(inotify_event*)pointer;

                    if (event.mask & IN_ACCESS)
                    {
                        writeln("IN_ACCESS");
                    }

                    if (event.mask & IN_MODIFY)
                    {
                        writeln("IN_MODIFY");
                    }

                    if (event.mask & IN_ATTRIB)
                    {
                        writeln("IN_ATTRIB");
                    }

                    if (event.mask & IN_CLOSE_WRITE)
                    {
                        writeln("IN_CLOSE_WRITE");
                    }

                    if (event.mask & IN_CLOSE_NOWRITE)
                    {
                        writeln("IN_CLOSE_NOWRITE");
                    }

                    if (event.mask & IN_OPEN)
                    {
                        writeln("IN_OPEN");
                    }

                    if (event.mask & IN_MOVED_FROM)
                    {
                        writeln("IN_MOVED_FROM");
                    }

                    if (event.mask & IN_MOVED_TO)
                    {
                        writeln("IN_MOVED_TO");
                    }

                    if (event.mask & IN_CREATE)
                    {
                        writeln("IN_CREATE");
                    }

                    if (event.mask & IN_DELETE)
                    {
                        writeln("IN_DELETE");
                    }

                    if (event.mask & IN_DELETE_SELF)
                    {
                        writeln("IN_DELETE_SELF");
                    }

                    if (event.mask & IN_MOVE_SELF)
                    {
                        writeln("IN_MOVE_SELF");
                    }

                    if (event.mask & IN_UMOUNT)
                    {
                        writeln("IN_UMOUNT");
                    }

                    if (event.mask & IN_Q_OVERFLOW)
                    {
                        writeln("IN_Q_OVERFLOW");
                    }

                    if (event.mask & IN_IGNORED)
                    {
                        writeln("IN_IGNORED");
                    }

                    if (event.mask & IN_CLOSE)
                    {
                        writeln("IN_CLOSE");
                    }

                    if (event.mask & IN_MOVE)
                    {
                        writeln("IN_MOVE");
                    }

                    if (event.mask & IN_ONLYDIR)
                    {
                        writeln("IN_ONLYDIR");
                    }

                    if (event.mask & IN_DONT_FOLLOW)
                    {
                        writeln("IN_DONT_FOLLOW");
                    }

                    if (event.mask & IN_EXCL_UNLINK)
                    {
                        writeln("IN_EXCL_UNLINK");
                    }

                    if (event.mask & IN_MASK_ADD)
                    {
                        writeln("IN_MASK_ADD");
                    }

                    writefln("wd: %s - mask: 0x%08x - cookie: %s - len: %s", event.wd, event.mask, event.cookie, event.len);

                    pointer += inotify_event.sizeof + event.len;
                }

            }
        }

        inotify_rm_watch(inotifyInstance, watchDescriptor);
        close(inotifyInstance);
    }
}

If i perform the following actions, i get the following results:

  1. touch ~/Desktop/test.txt

    IN_OPEN
    wd: 1 - mask: 0x00000020 - cookie: 0 - len: 0
    IN_IGNORED
    wd: 1 - mask: 0x00008000 - cookie: 0 - len: 0

  2. echo "Mary had a little lamb." >> ~/Desktop/test.txt

    IN_OPEN
    wd: 1 - mask: 0x00000020 - cookie: 0 - len: 0
    IN_IGNORED
    wd: 1 - mask: 0x00008000 - cookie: 0 - len: 0

  3. Open in Vim, modify and :wq

    IN_OPEN
    wd: 1 - mask: 0x00000020 - cookie: 0 - len: 0
    IN_IGNORED
    wd: 1 - mask: 0x00008000 - cookie: 0 - len: 0

Nothing seems to work here and the file is ignored after the open event is triggered.

If i alter the code and change the IN_ALL_EVENTS flag (which checks for all events) and use IN_MODIFY instead, i get the following:

  1. touch ~/Desktop/test.txt

    No result.

  2. echo "Mary had a little lamb." >> ~/Desktop/test.txt

    IN_MODIFY wd: 1 - mask: 0x00000002 - cookie: 0 - len: 0

  3. Open in Vim, modify and :wq

    IN_IGNORED wd: 1 - mask: 0x00008000 - cookie: 0 - len: 0

Only point 2 seems to work correctly.

Any ideas what i'm doing wrong here and any thoughts about changing the code to correctly get all the modification events for a file?

Upvotes: 4

Views: 7195

Answers (1)

Gary Willoughby
Gary Willoughby

Reputation: 52608

After some reading i've modified the code a little and seems to have solved the problems. Appending to the file raises a IN_MODIFY event which is caught nicely. Touch raises a IN_ATTRIB which we can treat as a modification. Vim (and other editors) raise IN_ATTRIB, IN_DELETE_SELF and IN_MOVE_SELF events before raising a final IN_IGNORED so i can handle these now i know. If i encounter a IN_IGNORED event, i re-initialize inotify.

/**
 * Imports.
 */
import core.sys.posix.unistd;
import core.sys.posix.sys.select;
import inotify;
import std.stdio;

/**
 * Main.
 */
void main(string[] arguments)
{
    immutable int  EVENT_BUFFER_LENGTH = (inotify_event.sizeof + 16) * 1024;
    immutable uint NOTIFICATION_FLAGS  = IN_MODIFY | IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_IGNORED;

    auto inotifyInstance = inotify_init();

    if (inotifyInstance >= 0)
    {
        string watchedFile = "/home/gary/Desktop/test.txt";
        char[EVENT_BUFFER_LENGTH] eventBuffer;
        void* eventPointer;

        auto watchDescriptor = inotify_add_watch(inotifyInstance, cast(char*)watchedFile, NOTIFICATION_FLAGS);

        while (true)
        {
            fd_set fileDescriptorSet;

            FD_ZERO(&fileDescriptorSet);
            FD_SET(inotifyInstance, &fileDescriptorSet);

            timeval timeout;
            timeout.tv_sec  = 5;
            timeout.tv_usec = 0;

            if (select(FD_SETSIZE, &fileDescriptorSet, null, null, &timeout))
            {
                auto bytesRead = read(inotifyInstance, eventBuffer.ptr, eventBuffer.length);

                for (eventPointer = eventBuffer.ptr; eventPointer < eventBuffer.ptr + bytesRead; null)
                {
                    inotify_event* event = cast(inotify_event*)eventPointer;

                    if (event.mask & IN_MODIFY || event.mask & IN_ATTRIB)
                    {
                        writeln("Modified");
                    }

                    if (event.mask & IN_DELETE_SELF || event.mask & IN_MOVE_SELF || event.mask & IN_IGNORED)
                    {
                        writeln("Modified");
                        inotify_rm_watch(inotifyInstance, watchDescriptor);
                        close(inotifyInstance);
                        inotifyInstance = inotify_init();
                        watchDescriptor = inotify_add_watch(inotifyInstance, cast(char*)watchedFile, NOTIFICATION_FLAGS);
                    }

                    eventPointer += inotify_event.sizeof + event.len;
                }
            }
        }

        inotify_rm_watch(inotifyInstance, watchDescriptor);
        close(inotifyInstance);
    }
}

Upvotes: 8

Related Questions