Recent

Author Topic: Cross platform DirectoryWatcher  (Read 10952 times)

Wosi

  • New Member
  • *
  • Posts: 21
Cross platform DirectoryWatcher
« on: July 02, 2017, 07:30:21 pm »
I hope here is the right place for this:

I've made a cross platform DirectoryWatcher library. It's working on Windows, Mac and Linux.
Feel free to use it.
Please tell me if you find a bug.

https://github.com/Wosi/DirectoryWatcher

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4459
  • I like bugs.
Re: Cross platform DirectoryWatcher
« Reply #1 on: July 02, 2017, 08:28:19 pm »
I think this should be placed in FPC's libraries.
I remember such a cross-platform class has been requested and discussed earlier but it did not exist then.
Now it does. Cool!
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

hnb

  • Sr. Member
  • ****
  • Posts: 270
Re: Cross platform DirectoryWatcher
« Reply #2 on: July 02, 2017, 08:36:09 pm »
I think this should be placed in FPC's libraries.

Fully agree :). Anyway I see that DirectoryWatcher is licensed under MIT so AFAIK can't be added to FPC libraries.
Checkout NewPascal initiative and donate beer - ready to use tuned FPC compiler + Lazarus for mORMot project

best regards,
Maciej Izak

Wosi

  • New Member
  • *
  • Posts: 21
Re: Cross platform DirectoryWatcher
« Reply #3 on: July 02, 2017, 08:38:55 pm »
I think this should be placed in FPC's libraries.

Fully agree :). Anyway I see that DirectoryWatcher is licensed under MIT so AFAIK can't be added to FPC libraries.

Glad, you like it ;-)
What is the license you want? LGPL?
I'm not a lawyer. Is it possible to license the library under both licenses?

hnb

  • Sr. Member
  • ****
  • Posts: 270
Re: Cross platform DirectoryWatcher
« Reply #4 on: July 02, 2017, 08:48:04 pm »
What is the license you want? LGPL?
I'm not a lawyer. Is it possible to license the library under both licenses?

:) I am not lawyer too, but IMO the most perfect licensing model for 3rd library has mORMot:

https://synopse.info/files/html/Synopse%20mORMot%20Framework%20SAD%201.18.html#TITL_34

Any part of mORMot can be integrated with Lazarus or FPC at any time. FPC RTL/FCL is licensed under modified LGPL with static linking exception, see: http://wiki.freepascal.org/modified_LGPL
Checkout NewPascal initiative and donate beer - ready to use tuned FPC compiler + Lazarus for mORMot project

best regards,
Maciej Izak

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Cross platform DirectoryWatcher
« Reply #5 on: July 02, 2017, 08:53:39 pm »
Please tell me if you find a bug.
OnGetData is called without synchronization with the main thread. When used in GUI applications, this can cause problems.

Wosi

  • New Member
  • *
  • Posts: 21
Re: Cross platform DirectoryWatcher
« Reply #6 on: July 02, 2017, 09:10:07 pm »
https://synopse.info/files/html/Synopse%20mORMot%20Framework%20SAD%201.18.html#TITL_34

Any part of mORMot can be integrated with Lazarus or FPC at any time. FPC RTL/FCL is licensed under modified LGPL with static linking exception, see: http://wiki.freepascal.org/modified_LGPL

Thanks, I copied the license text from mORMot.

Please tell me if you find a bug.
OnGetData is called without synchronization with the main thread. When used in GUI applications, this can cause problems.

Works as intended. If synchronization is important then the user of the library has to take care of it inside the callback.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4459
  • I like bugs.
Re: Cross platform DirectoryWatcher
« Reply #7 on: July 03, 2017, 01:07:18 pm »
The nested method calls when constructing TDirectoryWatcherBuilder is a nice idea.
However I think the user should have an option to use a more traditional way of setting properties.
For example there should be:
Code: Pascal  [Select][+][-]
  1. property Recursive: Boolean read FRecursively write FRecursively;
  2. property OnDirectoryChanged: TDirectoryEvent read FCallBack write FCallBack;
Note the naming convention for the event property, they typically start with On...
The event type itself could be TDirectoryChangedEvent, and the event variable could be FOnDirectoryChanged instead of FCallBack.
The event handler in the example is poorly named. It should be DirectoryChanged or maybe FileChanged instead of OnFileEvent.

Is there a need to separate events for directories and files?

@hnb, you are an FPC developer, or at least a semi-developer due to your generics collection work.
Can you influence this DirectoryWatcher going into FPC libs?
We can also ask about it in fpc-dev list.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

hnb

  • Sr. Member
  • ****
  • Posts: 270
Re: Cross platform DirectoryWatcher
« Reply #8 on: July 03, 2017, 02:49:13 pm »
@hnb, you are an FPC developer, or at least a semi-developer due to your generics collection work.
I have no idea x)
Can you influence this DirectoryWatcher going into FPC libs?
We can also ask about it in fpc-dev list.
No need to ask on fpc-dev list. The library will be added to FCL (rather synchronized with not committed yet similar library).
Checkout NewPascal initiative and donate beer - ready to use tuned FPC compiler + Lazarus for mORMot project

best regards,
Maciej Izak

hnb

  • Sr. Member
  • ****
  • Posts: 270
Re: Cross platform DirectoryWatcher
« Reply #9 on: July 03, 2017, 02:49:51 pm »
@Wosi please check PM from me.
Checkout NewPascal initiative and donate beer - ready to use tuned FPC compiler + Lazarus for mORMot project

best regards,
Maciej Izak

edwinyzh

  • New Member
  • *
  • Posts: 43
Re: Cross platform DirectoryWatcher
« Reply #10 on: July 04, 2017, 05:53:43 am »
checked, worked as expected, and I like the fluent sytle API :)

promisetom

  • New Member
  • *
  • Posts: 11
Re: Cross platform DirectoryWatcher
« Reply #11 on: September 14, 2020, 04:30:52 am »
Has anyone created an app using DirectoryWatcher using Forms.
I would like to use it in a graphics based app, not a console.
I tested an issue version using formas and received a "SIGSEGV exception"
Tom

zamronypj

  • Full Member
  • ***
  • Posts: 133
    • Fano Framework, Free Pascal web application framework
Re: Cross platform DirectoryWatcher
« Reply #12 on: September 15, 2020, 05:00:30 am »
I think this should be placed in FPC's libraries.

Fully agree :). Anyway I see that DirectoryWatcher is licensed under MIT so AFAIK can't be added to FPC libraries.

According to  https://softwareengineering.stackexchange.com/questions/189633/what-sublicense-actually-means I believe you can sublicense MIT as LGPL but not vice versa as long as you give copyright license and license notice. https://tldrlegal.com/license/mit-license
Fano Framework, Free Pascal web application framework https://fanoframework.github.io
Apache module executes Pascal program like scripting language https://zamronypj.github.io/mod_pascal/
Github https://github.com/zamronypj

af0815

  • Hero Member
  • *****
  • Posts: 1289
Re: Cross platform DirectoryWatcher
« Reply #13 on: September 15, 2020, 05:50:25 am »
Has anyone created an app using DirectoryWatcher using Forms.
I would like to use it in a graphics based app, not a console.
I tested an issue version using formas and received a "SIGSEGV exception"
Tom
Have you used synchronize to make a correct thread change as described in post #6 from wosi. You have to use this in the callback. See also
https://wiki.freepascal.org/Multithreaded_Application_Tutorial
https://www.freepascal.org/~michael/articles/lazthread/lazthread.pdf

The reason is, the forms and other grafic stuff is not thread safe. So you have to use synchronize to bring the operation to the main thread, which can deal with the forms and the other graphical elements.

But it is a good idea to show the program or parts, so other people can see your problem. Because you see your code and the community sees nothing and can only stick in fog.
 
regards
Andreas

r.lukasiak

  • Full Member
  • ***
  • Posts: 138
Re: Cross platform DirectoryWatcher
« Reply #14 on: May 12, 2022, 05:32:41 am »
hi everyone.

I've just discovered DirectoryWatcher and trying  demo confirmed it's what I was looking for. The demo is pretty clear but the thing is that I'd need to use it with forms. I read the old answers and I understand I have to take care of synchronization etc,
Could someone  provide with an example of implementing DirectoryWatcher in forms?

thanks in advance!

-----------------------------------------------------------------------------------------
Edited on 13/05/2022:

Following https://github.com/Wosi/DirectoryWatcher "how to use":
Code: Pascal  [Select][+][-]
  1. var
  2.   Form1: TForm1;
  3.   FolderToWatch: String;
  4.   WatchSubFolders: Boolean = true; //I need to monitor subfolders, so I set it to 'true'
  5.   DirectoryWatcher: IDirectoryWatcher;    
  6.  
  7. procedure TForm1.SpeedButton1Click(Sender: TObject);
  8. begin
  9.   FolderToWatch := '/my_path';
  10.   WatchSubFolders:=true;
  11.  
  12.   DirectoryWatcher := TDirectoryWatcherBuilder
  13.                         .New
  14.                         .WatchDirectory(FolderToWatch)
  15.                         .Recursively(WatchSubFolders)
  16.                         .OnChangeTrigger(OnFileEvent)
  17.                         .Build;
  18.   DirectoryWatcher.Start;  
  19. end;

when building, it's saying that OnFileEvent needs more parameters. On the demo app, the problem doesn't occur.
Any clue?
« Last Edit: May 13, 2022, 11:20:58 pm by r.lukasiak »

 

TinyPortal © 2005-2018