Lazarus

Programming => Operating Systems => Linux => Topic started by: cdbc on December 06, 2024, 04:49:06 pm

Title: Directory-age?!?
Post by: cdbc on December 06, 2024, 04:49:06 pm
Hi
In my 'PluginMgr', I'm trying to detect changes in the "plugin-directory", i.e.: if someone added a new plugin...
If my manager is running in mode 'ActiveOnly', it learns what plugins are actually used and loads only them, cutting the load-times to ~1/10
But I'd like it to automagically detect /new-arrivals/ and reset 'active-cache' and load all plugins to let the user use the new plugin ...and then start learning anew...
So I'm fiddling with code like this, to ascertain if the directory has been modified:
Code: Pascal  [Select][+][-]
  1. program gda;  /// (g)et (d)irectory (a)ge ///
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}
  7.   cthreads, baseunix,
  8.   {$ENDIF}
  9.   Classes,sysutils;
  10.  
  11. function DirAge(const FileName : rawbytestring; out aStat: stat): ptrint;
  12. var
  13.   Info : baseunix.stat;
  14.   SystemFileName: RawByteString;
  15. begin
  16.   SystemFileName:= ToSingleByteFileSystemEncodedFileName(FileName);
  17.  
  18.   if  fpstat(PAnsiChar(SystemFileName),Info) < 0 then
  19.     exit(-1)
  20.   else
  21.     Result:= Info.st_mtime; /// we're mostly interested in 'modified'
  22.   aStat:= Info; /// record copy, but can't use pointer, 'cause info is local and goes outta scope
  23. end;
  24.  
  25. var
  26.   da: ptrint;
  27.   dn: string;
  28.   st: baseunix.stat;
  29.  
  30. begin
  31.   writeln('>> (g)et (d)irectory (a)ge <<');
  32.   writeln('(i) usage: gda <directory-name>');
  33.   if ParamCount < 1 then halt(1);
  34.   dn:= ParamStr(1);
  35.   da:= DirAge(dn,st);
  36.   writeln('>- - - -[ Info about "',dn,'" ]- - - -<');
  37.   writeln('(i) Dir-Mod: ',da,', as DateTime: ',DateTimeToStr(FileDateToDateTime(da)));
  38.   writeln('(i) Dir-Cre: ',st.st_ctime,', as DateTime: ',DateTimeToStr(FileDateToDateTime(st.st_ctime)));
  39.   writeln('(i) Dir-Acc: ',st.st_atime,', as DateTime: ',DateTimeToStr(FileDateToDateTime(st.st_atime)));
  40.   writeln('________________________________________');
  41. end.  
'DirAge' is a modified version of 'FileAge' in the RTL and I'm wondering if there are some snags ahead that I can't foresee, since it's not in the RTL?!?
I'll be happy for any input...  :D
Regards Benny
edit: typo
Title: Re: Directory-age?!?
Post by: MarkMLl on December 06, 2024, 05:01:09 pm
You need to use FAM. The details will vary a bit depending on OS but see for example the attached.

MarkMLl
Title: Re: Directory-age?!?
Post by: cdbc on December 06, 2024, 05:12:02 pm
Thanks Mark, will have a LookSee  :)
Regards Benny
Title: Re: Directory-age?!?
Post by: MarkMLl on December 06, 2024, 05:30:32 pm
Look at the handful of functions near the end. That's from a program that listens for the output from a logic analyzer in various forms, and disassembles it. So I'm fairly confident that it gets tricky things like the precise format of paths right.

MarkMLl
Title: Re: Directory-age?!?
Post by: cdbc on December 06, 2024, 05:45:08 pm
Hi
Cool code Mark, thanks.
So, inotify is a kernel-thingy, where you can get a /watched/ handle to the object in question, got it.
I have to poll this handle regularly to get a heads up on said object.
...and I guess 'fpClose(handle);' when I'm done, to release inotify-resources.
Am I right in thinking polling -> Thread? It so happens, I've got this sleeping thread, that I can utilize whenever I want  :D It's actually used to load plugins on app-startup and I just kept it around just in case...

Am I understanding this correctly?!?  %)
Regards Benny
Title: Re: Directory-age?!?
Post by: MarkMLl on December 06, 2024, 06:08:16 pm
Yes, sounds about right.

MarkMLl
Title: Re: Directory-age?!?
Post by: cdbc on December 06, 2024, 06:17:07 pm
Hi
Thanks Mate  :)
Regards Benny
Title: Re: Directory-age?!?
Post by: MarkMLl on December 06, 2024, 06:40:04 pm
I find I've used that stuff in a couple more places, but not- interestingly- in the program I was tweaking a few weeks ago which reads the content of a DSO (digital 'scope) and can reload the backend dynamic library if it's recompiled.

Updated: I've grafted it into the DSO program so that it re-reads the directory containing the backends (each of which has the ISO date and time in its name) only when something's actually changed. The API doesn't, unfortunately, allow wildcards, but the more difficult part of the exercise was working out the structure of my original code (a reload can be triggered by a number of things, including HUP etc.).

MarkMLl
TinyPortal © 2005-2018