watching for new files: libevent vs. libev

Mikhail T. mi+thun at aldan.algebra.com
Thu Apr 4 19:04:35 CEST 2013


Hello!

By adding EV_ET to the flags, I can watch a directory for changes --
such as files being added or removed to it -- using libevent (at least,
on FreeBSD). Sample program attached.

Does libev provide a similar functionality? Some mailing list archives
suggest, edge-triggered events aren't supported by libev (for
portability?), but those pages are from 2010. Have there been
improvements since, perhaps?

Libevent did not have EV_ET until fairly recently too, but it is there
now... Please, advise. Thank you!

    -mi

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.schmorp.de/pipermail/libev/attachments/20130404/8c8eaebd/attachment.html>
-------------- next part --------------
#include <sys/types.h>

#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>

#include <event2/event.h>

static void
handler(evutil_socket_t fd, short type, void *_p)
{
	const char *p = _p;

	printf("%s (%d) triggered by event(s) %hd\n", p, (int)fd, type);
}

static void
errhandler(int err)
{
	fprintf(stderr, "Error %d (%s) occured\n", err, strerror(err));
}

int
main(int argc, char *argv[])
{
	int	 dd, i;
	DIR	*dp;

	struct event_base	*eb;
	struct event     	*e;

	dp = opendir(argv[argc - 1]);
	if (dp == NULL) {
		fprintf(stderr, "%s: %s: %s\n", argv[0], argv[argc - 1],
		    strerror(errno));
		exit(EX_NOINPUT);
	}
	dd = dirfd(dp);

	event_set_fatal_callback(errhandler);
	eb = event_base_new();
	e = event_new(eb, dd, EV_WRITE|EV_READ|EV_ET|EV_PERSIST, handler,
	    argv[argc - 1]);
	event_add(e, NULL);
	switch (i = event_base_dispatch(eb)) {
	case -1:
		fprintf(stderr, "event_base_dispatch failed\n");
		exit(EX_SOFTWARE);
	case 1:
		fprintf(stderr, "event_base_dispatch found no "
		    "events to care\n");
		exit(EX_SOFTWARE);
	case 0:
		break;
	default:
		fprintf(stderr, "event_base_dispatch returned "
		    "unexpected valued %d\n", i);
		exit(EX_SOFTWARE);
	}
	return 0;
}


More information about the libev mailing list