Yet another. Init the gobject type system.
[beagle.git] / Util / Inotify.cs
blob78ce1229496b1a7ced0c60d298ec97fd16985588
1 //
2 // Inotify.cs
3 //
4 // Copyright (C) 2004 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 // WARNING: This is not portable to Win32
29 using System;
30 using System.Collections;
31 using System.IO;
32 using System.Runtime.InteropServices;
33 using System.Text;
34 using System.Text.RegularExpressions;
35 using System.Threading;
37 namespace Beagle.Util {
39 public class Inotify {
41 public delegate void InotifyCallback (Watch watch, string path, string subitem, string srcpath, EventType type);
43 public interface Watch {
44 void Unsubscribe ();
45 void ChangeSubscription (EventType new_mask);
47 /////////////////////////////////////////////////////////////////////////////////////
49 [Flags]
50 public enum EventType : uint {
51 Access = 0x00000001, // File was accessed
52 Modify = 0x00000002, // File was modified
53 Attrib = 0x00000004, // File changed attributes
54 CloseWrite = 0x00000008, // Writable file was closed
55 CloseNoWrite = 0x00000010, // Non-writable file was close
56 Open = 0x00000020, // File was opened
57 MovedFrom = 0x00000040, // File was moved from X
58 MovedTo = 0x00000080, // File was moved to Y
59 Create = 0x00000100, // Subfile was created
60 Delete = 0x00000200, // Subfile was deleted
61 DeleteSelf = 0x00000400, // Self was deleted
63 Unmount = 0x00002000, // Backing fs was unmounted
64 QueueOverflow = 0x00004000, // Event queue overflowed
65 Ignored = 0x00008000, // File is no longer being watched
67 IsDirectory = 0x40000000, // Event is against a directory
68 OneShot = 0x80000000, // Watch is one-shot
70 // For forward compatibility, define these explicitly
71 All = (EventType.Access | EventType.Modify | EventType.Attrib |
72 EventType.CloseWrite | EventType.CloseNoWrite | EventType.Open |
73 EventType.MovedFrom | EventType.MovedTo | EventType.Create |
74 EventType.Delete | EventType.DeleteSelf)
77 // Events that we want internally, even if the handlers do not
78 private static EventType base_mask = EventType.MovedFrom | EventType.MovedTo;
80 /////////////////////////////////////////////////////////////////////////////////////
82 private static Logger log;
84 public static Logger Log {
85 get { return log; }
88 /////////////////////////////////////////////////////////////////////////////////////
90 [StructLayout (LayoutKind.Sequential)]
91 private struct inotify_event {
92 public int wd;
93 public EventType mask;
94 public uint cookie;
95 public uint len;
98 [DllImport ("libbeagleglue")]
99 static extern int inotify_glue_init ();
101 [DllImport ("libbeagleglue")]
102 static extern int inotify_glue_watch (int fd, string filename, EventType mask);
104 [DllImport ("libbeagleglue")]
105 static extern int inotify_glue_ignore (int fd, int wd);
107 [DllImport ("libbeagleglue")]
108 static extern unsafe void inotify_snarf_events (int fd,
109 out int nr,
110 out IntPtr buffer);
112 [DllImport ("libbeagleglue")]
113 static extern void inotify_snarf_cancel ();
115 /////////////////////////////////////////////////////////////////////////////////////
117 public static bool Verbose = false;
118 private static int inotify_fd = -1;
120 static Inotify ()
122 log = Logger.Get ("Inotify");
124 if (Environment.GetEnvironmentVariable ("BEAGLE_DISABLE_INOTIFY") != null) {
125 Logger.Log.Debug ("BEAGLE_DISABLE_INOTIFY is set");
126 return;
129 if (Environment.GetEnvironmentVariable ("BEAGLE_INOTIFY_VERBOSE") != null)
130 Inotify.Verbose = true;
132 try {
133 inotify_fd = inotify_glue_init ();
134 } catch (EntryPointNotFoundException) {
135 Logger.Log.Info ("Inotify not available on system.");
136 return;
139 if (inotify_fd == -1)
140 Logger.Log.Warn ("Could not initialize inotify");
143 public static bool Enabled {
144 get { return inotify_fd >= 0; }
147 /////////////////////////////////////////////////////////////////////////////////////
149 #if ! ENABLE_INOTIFY
151 // Stubs for systems where inotify is unavailable
153 public static Watch Subscribe (string path, InotifyCallback callback, EventType mask)
155 return null;
158 public static void Start ()
160 return;
163 public static void Stop ()
165 return;
168 #else // ENABLE_INOTIFY
170 /////////////////////////////////////////////////////////////////////////////////////
171 private static ArrayList event_queue = new ArrayList ();
173 private class QueuedEvent {
174 public int Wd;
175 public EventType Type;
176 public string Filename;
177 public uint Cookie;
179 public bool Analyzed;
180 public bool Dispatched;
181 public DateTime HoldUntil;
182 public QueuedEvent PairedMove;
184 // Measured in milliseconds; 57ms is totally random
185 public const double DefaultHoldTime = 57;
187 public QueuedEvent ()
189 // Set a default HoldUntil time
190 HoldUntil = DateTime.Now.AddMilliseconds (DefaultHoldTime);
193 public void AddMilliseconds (double x)
195 HoldUntil = HoldUntil.AddMilliseconds (x);
198 public void PairWith (QueuedEvent other)
200 this.PairedMove = other;
201 other.PairedMove = this;
203 if (this.HoldUntil < other.HoldUntil)
204 this.HoldUntil = other.HoldUntil;
205 other.HoldUntil = this.HoldUntil;
209 /////////////////////////////////////////////////////////////////////////////////////
211 private class WatchInternal : Watch {
212 private InotifyCallback callback;
213 private EventType mask;
214 private WatchInfo watchinfo;
215 private bool is_subscribed;
217 public InotifyCallback Callback {
218 get { return callback; }
221 public EventType Mask {
222 get { return mask; }
223 set { mask = value; }
226 public WatchInternal (InotifyCallback callback, EventType mask, WatchInfo watchinfo)
228 this.callback = callback;
229 this.mask = mask;
230 this.watchinfo = watchinfo;
231 this.is_subscribed = true;
234 public void Unsubscribe ()
236 if (!this.is_subscribed)
237 return;
239 Inotify.Unsubscribe (watchinfo, this);
240 this.is_subscribed = false;
243 public void ChangeSubscription (EventType mask)
245 if (! this.is_subscribed)
246 return;
248 this.mask = mask;
249 CreateOrModifyWatch (this.watchinfo);
254 private class WatchInfo {
255 public int Wd = -1;
256 public string Path;
257 public bool IsDirectory;
258 public EventType Mask;
260 public EventType FilterMask;
261 public EventType FilterSeen;
263 public ArrayList Children;
264 public WatchInfo Parent;
266 public ArrayList Subscribers;
269 private static Hashtable watched_by_wd = new Hashtable ();
270 private static Hashtable watched_by_path = new Hashtable ();
271 private static WatchInfo last_watched = null;
273 private class PendingMove {
274 public WatchInfo Watch;
275 public string SrcName;
276 public DateTime Time;
277 public uint Cookie;
279 public PendingMove (WatchInfo watched, string srcname, DateTime time, uint cookie) {
280 Watch = watched;
281 SrcName = srcname;
282 Time = time;
283 Cookie = cookie;
287 public static int WatchCount {
288 get { return watched_by_wd.Count; }
291 public static bool IsWatching (string path)
293 path = Path.GetFullPath (path);
294 return watched_by_path.Contains (path);
297 // Filter WatchInfo items when we do the Lookup.
298 // We do the filtering here to avoid having to acquire
299 // the watched_by_wd lock yet again.
300 private static WatchInfo Lookup (int wd, EventType event_type)
302 lock (watched_by_wd) {
303 WatchInfo watched;
304 if (last_watched != null && last_watched.Wd == wd)
305 watched = last_watched;
306 else {
307 watched = watched_by_wd [wd] as WatchInfo;
308 if (watched != null)
309 last_watched = watched;
312 if (watched != null && (watched.FilterMask & event_type) != 0) {
313 watched.FilterSeen |= event_type;
314 watched = null;
317 return watched;
321 // The caller has to handle all locking itself
322 private static void Forget (WatchInfo watched)
324 if (last_watched == watched)
325 last_watched = null;
326 if (watched.Parent != null)
327 watched.Parent.Children.Remove (watched);
328 watched_by_wd.Remove (watched.Wd);
329 watched_by_path.Remove (watched.Path);
332 public static Watch Subscribe (string path, InotifyCallback callback, EventType mask, EventType initial_filter)
334 WatchInternal watch;
335 WatchInfo watched;
336 EventType mask_orig = mask;
338 if (!Path.IsPathRooted (path))
339 path = Path.GetFullPath (path);
341 bool is_directory = false;
342 if (Directory.Exists (path))
343 is_directory = true;
344 else if (! File.Exists (path))
345 throw new IOException (path);
347 lock (watched_by_wd) {
348 watched = watched_by_path [path] as WatchInfo;
350 if (watched == null) {
351 // We need an entirely new WatchInfo object
352 watched = new WatchInfo ();
353 watched.Path = path;
354 watched.IsDirectory = is_directory;
355 watched.Subscribers = new ArrayList ();
356 watched.Children = new ArrayList ();
357 DirectoryInfo dir = new DirectoryInfo (path);
358 if (dir.Parent != null)
359 watched.Parent = watched_by_path [dir.Parent.ToString ()] as WatchInfo;
360 if (watched.Parent != null)
361 watched.Parent.Children.Add (watched);
362 watched_by_path [watched.Path] = watched;
365 watched.FilterMask = initial_filter;
366 watched.FilterSeen = 0;
368 watch = new WatchInternal (callback, mask_orig, watched);
369 watched.Subscribers.Add (watch);
371 CreateOrModifyWatch (watched);
372 watched_by_wd [watched.Wd] = watched;
375 return watch;
378 public static Watch Subscribe (string path, InotifyCallback callback, EventType mask)
380 return Subscribe (path, callback, mask, 0);
383 public static EventType Filter (string path, EventType mask)
385 EventType seen = 0;
387 path = Path.GetFullPath (path);
389 lock (watched_by_wd) {
390 WatchInfo watched;
391 watched = watched_by_path [path] as WatchInfo;
393 seen = watched.FilterSeen;
394 watched.FilterMask = mask;
395 watched.FilterSeen = 0;
398 return seen;
401 private static void Unsubscribe (WatchInfo watched, WatchInternal watch)
403 watched.Subscribers.Remove (watch);
405 // Other subscribers might still be around
406 if (watched.Subscribers.Count > 0) {
407 // Minimize it
408 CreateOrModifyWatch (watched);
409 return;
412 int retval = inotify_glue_ignore (inotify_fd, watched.Wd);
413 if (retval < 0) {
414 string msg = String.Format ("Attempt to ignore {0} failed!", watched.Path);
415 throw new IOException (msg);
418 Forget (watched);
419 return;
422 // Ensure our watch exists, meets all the subscribers requirements,
423 // and isn't matching any other events that we don't care about.
424 private static void CreateOrModifyWatch (WatchInfo watched)
426 EventType new_mask = base_mask;
427 foreach (WatchInternal watch in watched.Subscribers)
428 new_mask |= watch.Mask;
430 if (watched.Wd >= 0 && watched.Mask == new_mask)
431 return;
433 // We rely on the behaviour that watching the same inode twice won't result
434 // in the wd value changing.
435 // (no need to worry about watched_by_wd being polluted with stale watches)
437 int wd = -1;
438 wd = inotify_glue_watch (inotify_fd, watched.Path, new_mask);
439 if (wd < 0) {
440 string msg = String.Format ("Attempt to watch {0} failed!", watched.Path);
441 throw new IOException (msg);
443 if (watched.Wd >= 0 && watched.Wd != wd) {
444 string msg = String.Format ("Watch handle changed unexpectedly!", watched.Path);
445 throw new IOException (msg);
448 watched.Wd = wd;
449 watched.Mask = new_mask;
452 /////////////////////////////////////////////////////////////////////////////////////
454 private static Thread snarf_thread = null;
455 private static bool running = false;
456 private static bool shutdown_requested = false;
458 public static void ShutdownRequested () {
459 lock (event_queue) {
460 shutdown_requested = true;
464 public static void Start ()
466 if (! Enabled)
467 return;
469 Logger.Log.Debug("Starting Inotify threads");
471 lock (event_queue) {
472 if (shutdown_requested || snarf_thread != null)
473 return;
475 running = true;
477 snarf_thread = ExceptionHandlingThread.Start (new ThreadStart (SnarfWorker));
478 ExceptionHandlingThread.Start (new ThreadStart (DispatchWorker));
482 public static void Stop ()
484 if (! Enabled)
485 return;
487 Log.Debug ("Stopping inotify threads");
489 lock (event_queue) {
490 shutdown_requested = true;
492 if (! running)
493 return;
495 running = false;
496 Monitor.Pulse (event_queue);
499 inotify_snarf_cancel ();
502 private static unsafe void SnarfWorker ()
504 Encoding filename_encoding = Encoding.UTF8;
505 int event_size = Marshal.SizeOf (typeof (inotify_event));
507 while (running) {
509 // We get much better performance if we wait a tiny bit
510 // between reads in order to let events build up.
511 // FIXME: We need to be smarter here to avoid queue overflows.
512 Thread.Sleep (15);
514 IntPtr buffer;
515 int nr;
517 // Will block while waiting for events, but with a 1s timeout.
518 inotify_snarf_events (inotify_fd,
519 out nr,
520 out buffer);
522 if (!running)
523 break;
525 if (nr == 0)
526 continue;
528 ArrayList new_events = new ArrayList ();
530 bool saw_overflow = false;
531 while (nr > 0) {
533 // Read the low-level event struct from the buffer.
534 inotify_event raw_event;
535 raw_event = (inotify_event) Marshal.PtrToStructure (buffer, typeof (inotify_event));
536 buffer = (IntPtr) ((long) buffer + event_size);
538 if ((raw_event.mask & EventType.QueueOverflow) != 0)
539 saw_overflow = true;
541 // Now we convert our low-level event struct into a nicer object.
542 QueuedEvent qe = new QueuedEvent ();
543 qe.Wd = raw_event.wd;
544 qe.Type = raw_event.mask;
545 qe.Cookie = raw_event.cookie;
547 // Extract the filename payload (if any) from the buffer.
548 byte [] filename_bytes = new byte[raw_event.len];
549 Marshal.Copy (buffer, filename_bytes, 0, (int) raw_event.len);
550 buffer = (IntPtr) ((long) buffer + raw_event.len);
551 int n_chars = 0;
552 while (n_chars < filename_bytes.Length && filename_bytes [n_chars] != 0)
553 ++n_chars;
554 qe.Filename = "";
555 if (n_chars > 0)
556 qe.Filename = filename_encoding.GetString (filename_bytes, 0, n_chars);
558 new_events.Add (qe);
559 nr -= event_size + (int) raw_event.len;
562 if (saw_overflow)
563 Logger.Log.Warn ("Inotify queue overflow!");
565 lock (event_queue) {
566 event_queue.AddRange (new_events);
567 Monitor.Pulse (event_queue);
573 // Update the watched_by_path hash and the path stored inside the watch
574 // in response to a move event.
575 private static void MoveWatch (WatchInfo watch, string name)
577 lock (watched_by_wd) {
579 watched_by_path.Remove (watch.Path);
580 watch.Path = name;
581 watched_by_path [watch.Path] = watch;
584 if (Verbose)
585 Console.WriteLine ("*** inotify: Moved Watch to {0}", watch.Path);
588 // A directory we are watching has moved. We need to fix up its path, and the path of
589 // all of its subdirectories, their subdirectories, and so on.
590 private static void HandleMove (string srcpath, string dstparent, string dstname)
592 string dstpath = Path.Combine (dstparent, dstname);
593 lock (watched_by_wd) {
595 WatchInfo start = watched_by_path [srcpath] as WatchInfo; // not the same as src!
596 if (start == null) {
597 Logger.Log.Warn ("Lookup failed for {0}", srcpath);
598 return;
601 // Queue our starting point, then walk its subdirectories, invoking MoveWatch() on
602 // each, repeating for their subdirectories. The relationship between start, child
603 // and dstpath is fickle and important.
604 Queue queue = new Queue();
605 queue.Enqueue (start);
606 do {
607 WatchInfo target = queue.Dequeue () as WatchInfo;
608 for (int i = 0; i < target.Children.Count; i++) {
609 WatchInfo child = target.Children[i] as WatchInfo;
610 Logger.Log.Debug ("Moving watch on {0} from {1} to {2}", child.Path, srcpath, dstpath);
611 string name = Path.Combine (dstpath, child.Path.Substring (srcpath.Length + 1));
612 MoveWatch (child, name);
613 queue.Enqueue (child);
615 } while (queue.Count > 0);
617 // Ultimately, fixup the original watch, too
618 MoveWatch (start, dstpath);
619 if (start.Parent != null)
620 start.Parent.Children.Remove (start);
621 start.Parent = watched_by_path [dstparent] as WatchInfo;
622 if (start.Parent != null)
623 start.Parent.Children.Add (start);
627 private static void SendEvent (WatchInfo watched, string filename, string srcpath, EventType mask)
629 // Does the watch care about this event?
630 if ((watched.Mask & mask) == 0)
631 return;
633 bool isDirectory = false;
634 if ((mask & EventType.IsDirectory) != 0)
635 isDirectory = true;
637 if (Verbose) {
638 Console.WriteLine ("*** inotify: {0} {1} {2} {3} {4} {5}",
639 mask, watched.Wd, watched.Path,
640 filename != "" ? filename : "\"\"",
641 isDirectory == true ? "(directory)" : "(file)",
642 srcpath != null ? "(from " + srcpath + ")" : "");
645 if (watched.Subscribers == null)
646 return;
648 foreach (WatchInternal watch in (IEnumerable) watched.Subscribers.Clone ())
649 try {
650 if (watch.Callback != null && (watch.Mask & mask) != 0)
651 watch.Callback (watch, watched.Path, filename, srcpath, mask);
652 } catch (Exception e) {
653 Logger.Log.Error ("Caught exception executing Inotify callbacks");
654 Logger.Log.Error (e);
658 ////////////////////////////////////////////////////////////////////////////////////////////////////
660 // Dispatch-time operations on the event queue
662 private static Hashtable pending_move_cookies = new Hashtable ();
664 // Clean up the queue, removing dispatched objects.
665 // We assume that the called holds the event_queue lock.
666 private static void CleanQueue_Unlocked ()
668 int first_undispatched = 0;
669 while (first_undispatched < event_queue.Count) {
670 QueuedEvent qe = event_queue [first_undispatched] as QueuedEvent;
671 if (! qe.Dispatched)
672 break;
674 if (qe.Cookie != 0)
675 pending_move_cookies.Remove (qe.Cookie);
677 ++first_undispatched;
680 if (first_undispatched > 0)
681 event_queue.RemoveRange (0, first_undispatched);
685 // Apply high-level processing to the queue. Pair moves,
686 // coalesce events, etc.
687 // We assume that the caller holds the event_queue lock.
688 private static void AnalyzeQueue_Unlocked ()
690 int first_unanalyzed = event_queue.Count;
691 while (first_unanalyzed > 0) {
692 --first_unanalyzed;
693 QueuedEvent qe = event_queue [first_unanalyzed] as QueuedEvent;
694 if (qe.Analyzed) {
695 ++first_unanalyzed;
696 break;
699 if (first_unanalyzed == event_queue.Count)
700 return;
702 // Walk across the unanalyzed events...
703 for (int i = first_unanalyzed; i < event_queue.Count; ++i) {
704 QueuedEvent qe = event_queue [i] as QueuedEvent;
706 // Pair off the MovedFrom and MovedTo events.
707 if (qe.Cookie != 0) {
708 if ((qe.Type & EventType.MovedFrom) != 0) {
709 pending_move_cookies [qe.Cookie] = qe;
710 // This increases the MovedFrom's HoldUntil time,
711 // giving us more time for the matching MovedTo to
712 // show up.
713 // (512 ms is totally arbitrary)
714 qe.AddMilliseconds (512);
715 } else if ((qe.Type & EventType.MovedTo) != 0) {
716 QueuedEvent paired_move = pending_move_cookies [qe.Cookie] as QueuedEvent;
717 if (paired_move != null) {
718 paired_move.Dispatched = true;
719 qe.PairedMove = paired_move;
724 qe.Analyzed = true;
728 private static void DispatchWorker ()
730 while (running) {
731 QueuedEvent next_event = null;
733 // Until we find something we want to dispatch, we will stay
734 // inside the following block of code.
735 lock (event_queue) {
737 while (running) {
738 CleanQueue_Unlocked ();
740 AnalyzeQueue_Unlocked ();
742 // Now look for an event to dispatch.
743 DateTime min_hold_until = DateTime.MaxValue;
744 DateTime now = DateTime.Now;
745 foreach (QueuedEvent qe in event_queue) {
746 if (qe.Dispatched)
747 continue;
748 if (qe.HoldUntil <= now) {
749 next_event = qe;
750 break;
752 if (qe.HoldUntil < min_hold_until)
753 min_hold_until = qe.HoldUntil;
756 // If we found an event, break out of this block
757 // and dispatch it.
758 if (next_event != null)
759 break;
761 // If we didn't find an event to dispatch, we can sleep
762 // (1) until the next hold-until time
763 // (2) until the lock pulses (which means something changed, so
764 // we need to check that we are still running, new events
765 // are on the queue, etc.)
766 // and then we go back up and try to find something to dispatch
767 // all over again.
768 if (min_hold_until == DateTime.MaxValue)
769 Monitor.Wait (event_queue);
770 else
771 Monitor.Wait (event_queue, min_hold_until - now);
775 // If "running" gets set to false, we might get a null next_event as the above
776 // loop terminates
777 if (next_event == null)
778 return;
780 // Now we have an event, so we release the event_queue lock and do
781 // the actual dispatch.
783 // Before we get any further, mark it
784 next_event.Dispatched = true;
786 WatchInfo watched;
787 watched = Lookup (next_event.Wd, next_event.Type);
788 if (watched == null)
789 continue;
791 string srcpath = null;
793 // If this event is a paired MoveTo, there is extra work to do.
794 if ((next_event.Type & EventType.MovedTo) != 0 && next_event.PairedMove != null) {
795 WatchInfo paired_watched;
796 paired_watched = Lookup (next_event.PairedMove.Wd, next_event.PairedMove.Type);
798 if (paired_watched != null) {
799 // Set the source path accordingly.
800 srcpath = Path.Combine (paired_watched.Path, next_event.PairedMove.Filename);
802 // Handle the internal rename of the directory.
803 if ((next_event.Type & EventType.IsDirectory) != 0)
804 HandleMove (srcpath, watched.Path, next_event.Filename);
808 SendEvent (watched, next_event.Filename, srcpath, next_event.Type);
810 // If a directory we are watching gets ignored, we need
811 // to remove it from the watchedByFoo hashes.
812 if ((next_event.Type & EventType.Ignored) != 0) {
813 lock (watched_by_wd)
814 Forget (watched);
819 /////////////////////////////////////////////////////////////////////////////////
821 #if INOTIFY_TEST
822 private static void Main (string [] args)
824 Queue to_watch = new Queue ();
825 bool recursive = false;
827 foreach (string arg in args) {
828 if (arg == "-r" || arg == "--recursive")
829 recursive = true;
830 else {
831 // Our hashes work without a trailing path delimiter
832 string path = arg.TrimEnd ('/');
833 to_watch.Enqueue (path);
837 while (to_watch.Count > 0) {
838 string path = (string) to_watch.Dequeue ();
840 Console.WriteLine ("Watching {0}", path);
841 Inotify.Subscribe (path, null, Inotify.EventType.All);
843 if (recursive) {
844 foreach (string subdir in DirectoryWalker.GetDirectories (path))
845 to_watch.Enqueue (subdir);
849 Inotify.Start ();
850 Inotify.Verbose = true;
852 while (Inotify.Enabled && Inotify.WatchCount > 0)
853 Thread.Sleep (1000);
855 if (Inotify.WatchCount == 0)
856 Console.WriteLine ("Nothing being watched.");
858 // Kill the event-reading thread so that we exit
859 Inotify.Stop ();
861 #endif
863 #endif // ENABLE_INOTIFY