Hi,
Does FPC have some file watching solution? I need this only for linux. I need notifications:
- File/folder changed / removed / added in watched directory. I have idea how to do this in thread loop but maybe FPC has OS solution
Regards

Mailing lists / fpc-pascal / 2014-04
16 messages · 8 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Hi,
Does FPC have some file watching solution? I need this only for linux. I need notifications:
- File/folder changed / removed / added in watched directory. I have idea how to do this in thread loop but maybe FPC has OS solution
Regards
i'm interested in this, as well... i have an app in perl that i'm considering rewriting in FPC... in that app, i had to specifically grab the attributes of the files the app watches and compare those values to previously stored values...
in perl:
if ($check_file eq $alert_file) { # If we're testing the alert file and
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($check_file);
if ($size < $prev_alert_size) { # the filesize is less than last we checked,
&write_log (2,"Alert filename changed. Reopening $alert_file");
close (ALERT); # we need to close and
open (ALERT, "$alert_file"); # reopen it.
$prev_alert_size = $size;
} else {
$prev_alert_size = $size;
}
}hopefully there is something similar to perl's stat() routine...
Does FPC have some file watching solution? I need this only for linux. I need…
You can use the inotify* functions in linux unit. See the man pages or Wikipedia for how they work.
Henry
i'm interested in this, as well... i have an app in perl that i'm considering rewriting…
I believe Linux has dnotify, inotify and possibly File Alteration Monitor, but that these are version-dependant and extremely dependant on distro policies. Because of this uncertainty it would be necessary to load the support libraries dynamically.
See: http://www.freepascal.org/~michael/articles/dirwatch/dirwatch.pdf
2014-04-11 17:40 GMT-03:00 silvioprog :
With sources and demos: http://www.freepascal.org/~michael/articles/
I believe Linux has dnotify, inotify and possibly File Alteration Monitor, but that these…
On 11 Apr 2014 21:39, "Mark Morgan Lloyd" <
considering rewriting in FPC... in that app, i had to specifically grab the attributes of the files the app watches and compare those values to previously stored values...
I believe Linux has dnotify, inotify and possibly File Alteration
Monitor, but that these are version-dependant and extremely dependant on distro policies. Because of this uncertainty it would be necessary to load the support libraries dynamically.
inotify replaced dnotify many years ago (both are in kernel, so no libraries needed). I don't know what FAM is useful for.
Henry
Thanks to all for tips. They are very helpfull. I'll test it, wait for feedback :)
Does FPC have some file watching solution? I need this only for linux. I need…
Krzysztof wroteidea how to do this in thread loop but maybe FPC has OS solution
As the others have said, the OS solutions are very version/distro dependent - especially under Linux which changes underlying functionality every 6 months or so (so damn annoying).
A cross-platform solution is very easy to implement. It might not be as efficient as an OS specific solution, but works just as well. I've implemented such a solution in fpGUI's demo IDE application. It run in a separate thread. You can register what to watch - files or directories, and it fires an OnChange event when changes are detected. If memory serves me well, I also used a CRC check on files to detect if the file content changed (and date/time or file size wasn't).
Michael van Canneyt's article (URL quoted earlier) also covers the topic.
Regards,
- Graeme -
No, the linux solution isn't distro dependent, since it's in the kernel. inotify came in 2005-ish to deprecate dnotify, but even dnotify still exists (read the man page for fcntl). The kernel guys are pretty serious about not breaking userspace interfaces.
If you're using some other userspace daemon to do this (like FAM), then
you're at their mercy.Henry
I've just been running a kernel build and note that there are explicit dnotify and inotify options, so what's built in is very much at the discretion of the distro maintainers.
If you're using some other userspace daemon to do this (like FAM), then you're at their mercy.
I notice that Gamin claims to be a subset/derivative of FAM. I think that I'd be inclined to test for the existence of one of the daemons first, since if they were being used by other components of the system (e.g. a file manager) they'd probably cache.
Almost everything in the kernel is configurable at build time, obviously. I've never come across a kernel without inotify/dnotify, even for old embedded ARM CPUs I've worked with (I guess no-one wants to waste power scanning directories and fstat-ing all the time). Distros usually build pretty full featured kernels, so I'd be surprised to see a distro without it.
No, the linux solution isn't distro dependent, since it's in the kernel. inotify came in…
Henry Vermaak wroteNo, the linux solution isn't distro dependent, since it's in the kernel. inotify came in 2005-ish to deprecate dnotify, but even dnotify still
OK, but does FCL include a cross-platform API that covers all platforms?
When I looked at this before (1.5 years ago) I couldn't find anything, and I needed the file monitor functionality under several platforms (MacOSX, FreeBSD, all Linux distros, Windows and RPi). So by implementing a cross-platform solution in pure Object Pascal was just way easier, and it worked everywhere first time round.
If FCL doesn't contain such a cross-platform API for this, then maybe something could be implemented. Use "native" OS/Kernel API's where support exists, and use the pure Object Pascal implementation as a fallback.
Regards,
- Graeme -
You can use the inotify* functions in linux unit. See the man pages or Wikipedia for how…
Henry Vermaak wroteYou can use the inotify* functions in linux unit. See the man pages or Wikipedia for how they work.
inotify is nice because - like anything in Linux - it is just a file.
Of course it would be nice if the rtl would provide an arch independent encapsulation for this...
-Michael
OK, but does FCL include a cross-platform API that covers all platforms? When I looked at…
Not yet. It is on my todo list.
Michael.
inotify is nice because - like anything in Linux - it is just a file. Of course it would…
Like "most anything". In particular, named network devices such as eth0 aren't files, and it's not possible to use a filesystem API to determine their properties.