tags:

views:

1602

answers:

7

The subject says it all - normally easy and cross platform way is to poll, intelligently. But every OS has some means to notify without polling. Is it possible in a reasonably cross platform way? (I only really care about Windows and Linux, but I use mac, so I thought posix may help?)

+1  A: 

I don't think POSIX itself has facilities for that. The closest to cross-platform I've seen is FAM, which seems to work for Linux, BSD, and Irix, but I'm not how easy it would be to port it to Windows and MacOS.

Chris Jester-Young
A: 

OK so it looks like I will be looking for solutions for each platform that I care about, alas.

Michael Neale
A: 

I believe OS X now has appropriate hooks/callbacks because they were needed for Spotlight indexing.

On linux you'll have the additional trouble that there are multiple file systems commonly used. If you need the functionality for only a limited amount of files/directories, I'd try about actively looking for modifications at regular intervals.

MattW.
+9  A: 

Linux users can use inotify

inotify is a Linux kernel subsystem that provides file system event notification.

Some goodies for Windows fellows:

aku
yeah I was able to make it work for that. Its a bit odd, with a blocking read() - but at least there is zero latency (rather, no polling !).
Michael Neale
+5  A: 

The Qt library has a QFileSystemWatcher class which provides cross platform notifications when a file changes. Even if you are not using Qt, because the source is available you could have a look at it as a sample for your own implementation. Qt has separate implementations for Windows, Linux and Mac.

David Dibben
A: 

There's File System Events API as of Leopard.

macbirdie
+1  A: 

libevent or libev seem to be what you want, though I haven't used them.

paleozogt