Is there an asynchronous way to find out if a file has changed?

I would like to track the file asynchronously for any changes. That is, I would like to have a callback (possibly from the kernel) in my program when the file was modified / deleted. A file is just a text file. I know that this can be done using the polling mechanism, but I'm looking for an event-based solution. I read about inotify, but it looks like it needs a fix to my kernel.

If the solution is POSIX compatible, it is even better.

+4
source share
3 answers

Inotify was merged with the Linux kernel back in 2005, so if you are not on a very old system, you should be able to use it out of the box.

I do not think there is a POSIX compatible solution for this. Mac OS X has FSEvents .

Also check out the man page for inotify.

EDIT:

I don’t know about your limitations and / or requirements, but there is GFileMonitor if you use Glib (C ++ binding is glibmm ) and QFileSystemWatcher you use Qt, They are probably more friendly to each other.

+7
source

SGI fam has been ported to several Unix. There is also a gamin .

+1
source

1)

write a device driver that creates a file called / dev / special _file.

symlink your text file to / dev / special file

intercept low-level read / write operations to modify a real text file called /path/to/text.txt, then generate a callback through signals or some type of interprocess exchange to any process you want.

2)

you have a way to open a text file and just sit and wait. use select () to determine when this file was modified, then follow the callback procedure.

0
source

All Articles