Watching for file change

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|

Watching for file change

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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Watching for file change

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

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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Watching for file change

Henry Vermaak
In reply to this post by Krzysztof

On 11 Apr 2014 20:07, "Krzysztof" <[hidden email]> 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

You can use the inotify* functions in linux unit. See the man pages or Wikipedia for how they work.

Henry


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Watching for file change

Mark Morgan Lloyd-5
In reply to this post by wkitty42
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.

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Watching for file change

silvioprog

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Watching for file change

silvioprog


--
Silvio Clécio
My public projects - github.com/silvioprog

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Watching for file change

Henry Vermaak
In reply to this post by Mark Morgan Lloyd-5


On 11 Apr 2014 21:39, "Mark Morgan Lloyd" <[hidden email]> 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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Watching for file change

Krzysztof
Thanks to all for tips. They are very helpfull. I'll test it, wait for
feedback :)
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Watching for file change

Graeme Geldenhuys-6
In reply to this post by Krzysztof
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).

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/
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Watching for file change

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

If you're using some other userspace daemon to do this (like FAM), then
you're at their mercy.

Henry
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Watching for file change

Mark Morgan Lloyd-5
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.

> 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]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Watching for file change

Henry Vermaak
On Sat, Apr 12, 2014 at 08:08:20PM +0000, Mark Morgan Lloyd wrote:

> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Watching for file change

Graeme Geldenhuys-6
In reply to this post by Henry Vermaak
On 2014-04-12 20:16, 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/
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Watching for file change

Michael Schnell
In reply to this post by Henry Vermaak
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.

Of course it would be nice if the rtl  would provide an arch independent
encapsulation for this...

-Michael
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Watching for file change

Michael Van Canneyt
In reply to this post by Graeme Geldenhuys-6


On Sun, 13 Apr 2014, Graeme Geldenhuys wrote:

> On 2014-04-12 20:16, 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?

Not yet. It is on my todo list.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Watching for file change

Mark Morgan Lloyd-5
In reply to this post by Michael Schnell
Michael Schnell wrote:
> 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]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal