Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileSystemWatcher under mono - watching subdirs

I have a problem. I have written a wrapper over FileSystemWatcher that detects changes in root folder and all of its subfolders. Nothing fancy:

FileSystemWatcher watcher = new FileSystemWatcher ();
watcher.Path = this.Root;
watcher.IncludeSubdirectories = true;
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.LastAccess | NotifyFilters.DirectoryName | NotifyFilters.FileName;
watcher.Changed += new FileSystemEventHandler (watcher_Changed);
watcher.Deleted += new FileSystemEventHandler (watcher_Deleted);
watcher.Created += new FileSystemEventHandler (watcher_Created);
watcher.Renamed += new RenamedEventHandler (watcher_Renamed);
watcher.EnableRaisingEvents = true;

While in .NET, under Windows, it works like a charm. But when I ported the code to mono and ran the code under OSX, it works properly only in the root folder.

Issues I have noticed by now:

  • Events are not raised for operations within folders already existing under root at the time the watcher starts

  • Paths I get via EventArgs.FullPath property are not correct (when I copy a file to path_to_root/some/more/subdirs/some.file, the path I get is just path_to_root/some.file).

The issue with unproper paths has been already reported one year ago (and looks like it was solved) but my mono comes from December last year (MonoDevelop says in References section it is version 4.0.0.0, it is all I can say about the distribution) and the bugs are still there... See: https://bugzilla.xamarin.com/show_bug.cgi?id=5747

Any ideas? I am really curious if there is a workaround not requiring writing own watcher that polls the file system repeatedly or starting separate watcher for every folder under root...

Thanks in advance!

like image 945
wojtuch Avatar asked May 13 '13 09:05

wojtuch


1 Answers

As far as I can tell, this simply does not work in Mono on OS X. I encountered it last week and could not find any bug report for it, so I reported it here: https://bugzilla.xamarin.com/show_bug.cgi?id=16259

As far as I can follow the implementation of KEventWatcher, it doesn't do anything to subscribe to subdirectories when the watcher is created. I think the only time it subscribes to subdirectories is when it detects them being added in PostEvent. Even if it did subscribe to all subdirectories on creation, it might not be a great solution. The underlying kevent mechanism would require an open file descriptor for every subdirectory, which could end up being an awful lot of file descriptors.

Mono does have other implementations of the FileSystemWatcher, but I believe the selection of implementation is baked into the Mono runtime when it's compiled. There is a slow and inefficient default watcher that works on all platforms by simply scanning the entire directory tree every second or so, but it is only selected if there isn't a platform-specific implementation available.

I'm afraid to say, it looks like your best bet are either of the workarounds you suggest - scanning for changes manually or creating a FileSystemWatcher for every directory.

like image 183
Weeble Avatar answered Sep 24 '22 16:09

Weeble