2004-03-30 Gonzalo Paniagua Javier <gonzalo@ximian.com>
authorGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Tue, 30 Mar 2004 12:26:18 +0000 (12:26 -0000)
committerGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Tue, 30 Mar 2004 12:26:18 +0000 (12:26 -0000)
* FAMWatcher.cs: support monitoring subdirectories. FAM doesn't do that,
so we have to register the existing directories and add the new ones
that might be created.

* SearchPattern.cs: provide the pattern when it's wrong.

svn path=/trunk/mcs/; revision=24781

mcs/class/System/System.IO/ChangeLog
mcs/class/System/System.IO/FAMWatcher.cs
mcs/class/System/System.IO/SearchPattern.cs

index 4cbe5f7fcdd0c5aab173165b6bea46d90de3aaa4..e217672cd54b4b5759905dba8b826ea391628a77 100755 (executable)
@@ -1,3 +1,11 @@
+2004-03-30  Gonzalo Paniagua Javier <gonzalo@ximian.com>
+
+       * FAMWatcher.cs: support monitoring subdirectories. FAM doesn't do that,
+       so we have to register the existing directories and add the new ones
+       that might be created.
+
+       * SearchPattern.cs: provide the pattern when it's wrong.
+
 2004-03-25  Gonzalo Paniagua Javier <gonzalo@ximian.com>
 
        * SearchPattern.cs: small improvement for files with no wildcard.
index a85123fbdce1e2ecce33f1eda3de090b692946e3..2da293591390399aaca01d79448372953dc0e7fe 100644 (file)
@@ -44,6 +44,7 @@ namespace System.IO {
                public bool IncludeSubdirs;
                public bool Enabled;
                public FAMRequest Request;
+               public Hashtable SubDirs;
        }
 
        class FAMWatcher : IFileWatcher
@@ -73,8 +74,8 @@ namespace System.IO {
                                        return true;
                                }
 
-                               watches = new Hashtable ();
-                               requests = new Hashtable ();
+                               watches = Hashtable.Synchronized (new Hashtable ());
+                               requests = Hashtable.Synchronized (new Hashtable ());
                                if (FAMOpen (out conn) == -1) {
                                        failed = true;
                                        watcher = null;
@@ -106,6 +107,9 @@ namespace System.IO {
                                data.Directory = fsw.FullPath;
                                data.FileMask = fsw.Filter;
                                data.IncludeSubdirs = fsw.IncludeSubdirectories;
+                               if (data.IncludeSubdirs)
+                                       data.SubDirs = new Hashtable ();
+
                                data.Enabled = true;
                                lock (this) {
                                        StartMonitoringDirectory (data);
@@ -123,6 +127,22 @@ namespace System.IO {
                                throw new Win32Exception ();
 
                        data.Request = fr;
+                       if (!data.IncludeSubdirs)
+                               return;
+
+                       foreach (string directory in Directory.GetDirectories (data.Directory)) {
+                               FAMData fd = new FAMData ();
+                               fd.FSW = data.FSW;
+                               fd.Directory = directory;
+                               fd.FileMask = data.FSW.Filter;
+                               fd.IncludeSubdirs = true;
+                               fd.SubDirs = new Hashtable ();
+                               fd.Enabled = true;
+
+                               StartMonitoringDirectory (fd);
+                               data.SubDirs [directory] = fd;
+                               requests [fd.Request.ReqNum] = fd;
+                       }
                }
 
                public void StopDispatching (FileSystemWatcher fsw)
@@ -138,6 +158,14 @@ namespace System.IO {
                                requests.Remove (data.Request.ReqNum);
                                if (watches.Count == 0)
                                        stop = true;
+
+                               if (!data.IncludeSubdirs)
+                                       return;
+
+                               foreach (FAMData fd in data.SubDirs) {
+                                       StopMonitoringDirectory (fd);
+                                       requests.Remove (fd.Request.ReqNum);
+                               }
                        }
                }
 
@@ -221,24 +249,56 @@ namespace System.IO {
                                                fa = FileAction.Removed;
                                        else if (code == (int) FAMCodes.Created)
                                                fa = FileAction.Added;
-                                       
-                                       if (fa != 0) {
-                                               if (filename != data.Directory && !fsw.Pattern.IsMatch (filename))
-                                                       continue;
-
-                                               lock (fsw) {
-                                                       fsw.DispatchEvents (fa, filename, ref renamed);
-                                                       if (fsw.Waiting) {
-                                                               fsw.Waiting = false;
-                                                               System.Threading.Monitor.PulseAll (fsw);
+
+                                       if (fa == 0)
+                                               continue;
+
+                                       if (fsw.IncludeSubdirectories) {
+                                               string full = fsw.FullPath;
+                                               string datadir = data.Directory;
+                                               if (datadir != full) {
+                                                       string reldir = datadir.Substring (full.Length + 1);
+                                                       datadir = Path.Combine (datadir, filename);
+                                                       filename = Path.Combine (reldir, filename);
+                                               } else {
+                                                       datadir = Path.Combine (fsw.FullPath, filename);
+                                               }
+
+                                               if (fa == FileAction.Added && Directory.Exists (datadir)) {
+                                                       FAMData fd = new FAMData ();
+                                                       fd.FSW = fsw;
+                                                       fd.Directory = datadir;
+                                                       fd.FileMask = fsw.Filter;
+                                                       fd.IncludeSubdirs = true;
+                                                       fd.SubDirs = new Hashtable ();
+                                                       fd.Enabled = true;
+
+                                                       lock (instance) {
+                                                               StartMonitoringDirectory (fd);
+                                                       }
+
+                                                       lock (data) {
+                                                               data.SubDirs [datadir] = fd;
                                                        }
+
+                                                       requests [fd.Request.ReqNum] = fd;
                                                }
                                        }
 
+                                       if (filename != data.Directory && !fsw.Pattern.IsMatch (filename))
+                                               continue;
+
+                                       lock (fsw) {
+                                               fsw.DispatchEvents (fa, filename, ref renamed);
+                                               if (fsw.Waiting) {
+                                                       fsw.Waiting = false;
+                                                       System.Threading.Monitor.PulseAll (fsw);
+                                               }
+                                       }
                                } while (FAMPending (ref conn) > 0);
                        }
                }
-               
+
                [DllImport ("fam")]
                extern static int FAMOpen (out FAMConnection fc);
 
index d47d5155b111bf039298cf1b1c2348d2e64c97f1..adbb8ce4508410f6fbf45c71b6deafe5670327d3 100644 (file)
@@ -47,7 +47,7 @@ namespace System.IO {
                private void Compile (string pattern)
                {
                        if (pattern == null || pattern.IndexOfAny (InvalidChars) >= 0)
-                               throw new ArgumentException ("Invalid search pattern.");
+                               throw new ArgumentException ("Invalid search pattern: '" + pattern + "'");
 
                        if (pattern == "*") {   // common case
                                ops = new Op (OpCode.True);