[corlib] Correctly recurse directories in Directory.EnumerateFiles if the first entry...
authorMarek Habersack <grendel@twistedcode.net>
Fri, 6 Apr 2012 17:35:38 +0000 (19:35 +0200)
committerMarek Habersack <grendel@twistedcode.net>
Fri, 6 Apr 2012 17:35:38 +0000 (19:35 +0200)
If the first entry in a directory happened to be another directory, the code wouldn't descend if
SearchOption.AllDirectoris was specified.

mcs/class/corlib/System.IO/Directory.cs

index f89218e2bc8aba9743140d565dfe69db4aff81ce..5730a45018c5f92ebae21c0f4477ce471a09d1eb 100644 (file)
@@ -547,19 +547,27 @@ namespace System.IO
                                throw MonoIO.GetException (Path.GetDirectoryName (Path.Combine (path, searchPattern)), (MonoIOError) error);
 
                        try {
+                               bool first = true;
                                if (((rattr & FileAttributes.ReparsePoint) == 0) && ((rattr & kind) != 0))
                                        yield return s;
                                
-                               while ((s = MonoIO.FindNext (handle, out rattr, out error)) != null){
+                               do {
+                                       if (((rattr & FileAttributes.Directory) != 0) && subdirs) {
+                                               foreach (string child in EnumerateKind (s, searchPattern, searchOption, kind))
+                                                       yield return child;
+                                       }
+
+                                       if (first) {
+                                               first = false;
+                                               continue;
+                                       }
+                                       
                                        if ((rattr & FileAttributes.ReparsePoint) != 0)
                                                continue;
+
                                        if ((rattr & kind) != 0)
                                                yield return s;
-                                       
-                                       if (((rattr & FileAttributes.Directory) != 0) && subdirs)
-                                               foreach (string child in EnumerateKind (s, searchPattern, searchOption, kind))
-                                                       yield return child;
-                               }
+                               } while ((s = MonoIO.FindNext (handle, out rattr, out error)) != null);
                        } finally {
                                MonoIO.FindClose (handle);
                        }