Fix unexpected exception when enumerating a completely empty drive root (#33684)
authorCarlos Sanchez Lopez <1175054+carlossanlop@users.noreply.github.com>
Wed, 18 Mar 2020 21:09:31 +0000 (14:09 -0700)
committerGitHub <noreply@github.com>
Wed, 18 Mar 2020 21:09:31 +0000 (14:09 -0700)
* Fix unexpected exception when enumerating a completely empty drive root.

* Address PR suggestions:
- Add NTStatus for file not found.
- Use it in the switch case. Since it's a rare case, add it right above 'default'.

* Remove hidden character causing build failure. It showed up after the file's encoding was changed to UTF8.

src/libraries/Common/src/Interop/Windows/NtDll/Interop.NtStatus.cs
src/libraries/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemEnumerator.Win32.cs

index 2d51c44..413efdf 100644 (file)
@@ -1,4 +1,4 @@
-// Licensed to the .NET Foundation under one or more agreements.
+// Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
@@ -7,15 +7,16 @@ internal static partial class Interop
     internal class StatusOptions
     {
         // Error codes from ntstatus.h
-        internal const uint STATUS_SUCCESS = 0x00000000;
-        internal const uint STATUS_SOME_NOT_MAPPED = 0x00000107;
-        internal const uint STATUS_NO_MORE_FILES = 0x80000006;
-        internal const uint STATUS_INVALID_PARAMETER = 0xC000000D;
-        internal const uint STATUS_NO_MEMORY = 0xC0000017;
-        internal const uint STATUS_OBJECT_NAME_NOT_FOUND = 0xC0000034;
-        internal const uint STATUS_NONE_MAPPED = 0xC0000073;
+        internal const uint STATUS_SUCCESS                = 0x00000000;
+        internal const uint STATUS_SOME_NOT_MAPPED        = 0x00000107;
+        internal const uint STATUS_NO_MORE_FILES          = 0x80000006;
+        internal const uint STATUS_INVALID_PARAMETER      = 0xC000000D;
+        internal const uint STATUS_FILE_NOT_FOUND         = 0xC000000F;
+        internal const uint STATUS_NO_MEMORY              = 0xC0000017;
+        internal const uint STATUS_ACCESS_DENIED          = 0xC0000022;
+        internal const uint STATUS_OBJECT_NAME_NOT_FOUND  = 0xC0000034;
+        internal const uint STATUS_ACCOUNT_RESTRICTION    = 0xC000006E;
+        internal const uint STATUS_NONE_MAPPED            = 0xC0000073;
         internal const uint STATUS_INSUFFICIENT_RESOURCES = 0xC000009A;
-        internal const uint STATUS_ACCESS_DENIED = 0xC0000022;
-        internal const uint STATUS_ACCOUNT_RESTRICTION = 0xc000006e;
     }
 }
index 2fa89c0..b0cf0b3 100644 (file)
@@ -42,6 +42,10 @@ namespace System.IO.Enumeration
                 case Interop.StatusOptions.STATUS_SUCCESS:
                     Debug.Assert(statusBlock.Information.ToInt64() != 0);
                     return true;
+                // FILE_NOT_FOUND can occur when there are NO files in a volume root (usually there are hidden system files).
+                case Interop.StatusOptions.STATUS_FILE_NOT_FOUND:
+                    DirectoryFinished();
+                    return false;
                 default:
                     int error = (int)Interop.NtDll.RtlNtStatusToDosError(status);