Here is a first attempt. I'm sure there are much better ways to do this but this should at least get you going in the right direction.

Some of the key things to know are the undocumented (at least not documented on the web site) modules that are available. One of them contains the Linux inotify header. If you've downloaded the zip file from the website take a look at the src/druntime/src directory for these modules.

I took the example from http://stackoverflow.com/questions/4062806/inotify-how-to-use-it-linux and made some changes to get it to compile and run in D.

import core.sys.linux.sys.inotify;
import core.stdc.stdlib : malloc;
import core.sys.posix.unistd : read;

import std.stdio : writeln;
import std.string: toStringz;

enum PATH_MAX = 256;

void main()
{
    string filename="aaa";

    int inotfd = inotify_init();
int watch_desc = inotify_add_watch(inotfd, toStringz(filename), IN_MODIFY);

    size_t bufsiz = inotify_event.sizeof + PATH_MAX + 1;
    inotify_event* event = cast(inotify_event *) malloc(bufsiz);

    /* wait for an event to occur */
    read(inotfd, event, event.sizeof);

    /* process event struct here */
    writeln("Received inotify event.");
}

As far as monitoring a directory, you can do that with inotify the same way you would a file. See http://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/index.html for more a C example (Listing 1).

Reply via email to