Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2014-04

Watching for file change

16 messages · 8 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Krzysztof

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

waldo kitty
to Krzysztof
Krzysztof wrote 6 lines
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

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...

NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted.
Mark Morgan Lloyd
to waldo kitty: i'm interested in this, as well... i have an app in perl that i'm considering rewriting…
waldo kitty wrote 12 lines
On 4/11/2014 3:06 PM, Krzysztof wrote: > 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 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...

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.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Henry Vermaak
to Mark Morgan Lloyd: I believe Linux has dnotify, inotify and possibly File Alteration Monitor, but that these…

On 11 Apr 2014 21:39, "Mark Morgan Lloyd" <

Show quoted text 15 lines
wrote: waldo kitty wrote: > > On 4/11/2014 3:06 PM, Krzysztof wrote: >> >> 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 > > > 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...

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

Graeme Geldenhuys
to Krzysztof: Does FPC have some file watching solution? I need this only for linux. I need…
Krzysztof wrote
idea 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 -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/
Henry Vermaak
to Graeme Geldenhuys
Graeme Geldenhuys wrote 6 lines
On 2014-04-11 20:06, Krzysztof wrote: > idea 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).

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

Mark Morgan Lloyd
to Henry Vermaak
Henry Vermaak wrote 11 lines
On Sat, Apr 12, 2014 at 12:32:14AM +0100, Graeme Geldenhuys wrote: > On 2014-04-11 20:06, Krzysztof wrote: >> idea 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). 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.

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.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Henry Vermaak
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 16 lines
Henry Vermaak wrote: >On Sat, Apr 12, 2014 at 12:32:14AM +0100, Graeme Geldenhuys wrote: >>On 2014-04-11 20:06, Krzysztof wrote: >>>idea 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). > >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. 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.

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.

Graeme Geldenhuys
to Henry Vermaak: No, the linux solution isn't distro dependent, since it's in the kernel. inotify came in…
Henry Vermaak wrote
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

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 -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/
Mark Morgan Lloyd
to Michael Schnell: inotify is nice because - like anything in Linux - it is just a file. Of course it would…
Michael Schnell wrote 7 lines
On 04/11/2014 09:53 PM, Henry Vermaak wrote: > > You 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.

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.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]