[SETUPLIB] Merge DoesPathExist() and DoesFileExist() into (temporarily called) DoesPa...
authorHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Mon, 21 Aug 2017 19:14:33 +0000 (19:14 +0000)
committerHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Sat, 27 Oct 2018 16:13:39 +0000 (18:13 +0200)
And turn the previous two functions into macros.

svn path=/branches/setup_improvements/; revision=75635
svn path=/branches/setup_improvements/; revision=75652

base/setup/lib/filesup.c
base/setup/lib/filesup.h

index 03d1ad8..44a8698 100644 (file)
@@ -8,6 +8,7 @@
 /* INCLUDES *****************************************************************/
 
 #include "precomp.h"
+#include "filesup.h"
 
 #define NDEBUG
 #include <debug.h>
@@ -580,19 +581,17 @@ CombinePaths(
     return Status;
 }
 
-//
-// NOTE: It may be possible to merge both DoesPathExist and DoesFileExist...
-//
 BOOLEAN
-DoesPathExist(
+DoesPathExistEx(
     IN HANDLE RootDirectory OPTIONAL,
-    IN PCWSTR PathName)
+    IN PCWSTR PathName,
+    IN BOOLEAN IsDirectory)
 {
     NTSTATUS Status;
+    UNICODE_STRING Name;
     HANDLE FileHandle;
     OBJECT_ATTRIBUTES ObjectAttributes;
     IO_STATUS_BLOCK IoStatusBlock;
-    UNICODE_STRING Name;
 
     RtlInitUnicodeString(&Name, PathName);
 
@@ -603,48 +602,24 @@ DoesPathExist(
                                NULL);
 
     Status = NtOpenFile(&FileHandle,
-                        FILE_LIST_DIRECTORY | SYNCHRONIZE,
+                        IsDirectory ? (FILE_LIST_DIRECTORY | SYNCHRONIZE)
+                                    :  FILE_GENERIC_READ, // Contains SYNCHRONIZE
                         &ObjectAttributes,
                         &IoStatusBlock,
                         FILE_SHARE_READ | FILE_SHARE_WRITE,
-                        FILE_SYNCHRONOUS_IO_NONALERT | FILE_DIRECTORY_FILE);
-    if (NT_SUCCESS(Status))
-        NtClose(FileHandle);
-    else
-        DPRINT("Failed to open directory '%wZ', Status 0x%08lx\n", &Name, Status);
-
-    return NT_SUCCESS(Status);
-}
-
-BOOLEAN
-DoesFileExist(
-    IN HANDLE RootDirectory OPTIONAL,
-    IN PCWSTR PathNameToFile)
-{
-    NTSTATUS Status;
-    UNICODE_STRING FileName;
-    HANDLE FileHandle;
-    OBJECT_ATTRIBUTES ObjectAttributes;
-    IO_STATUS_BLOCK IoStatusBlock;
-
-    RtlInitUnicodeString(&FileName, PathNameToFile);
-
-    InitializeObjectAttributes(&ObjectAttributes,
-                               &FileName,
-                               OBJ_CASE_INSENSITIVE,
-                               RootDirectory,
-                               NULL);
-
-    Status = NtOpenFile(&FileHandle,
-                        FILE_GENERIC_READ, // Contains SYNCHRONIZE
-                        &ObjectAttributes,
-                        &IoStatusBlock,
-                        FILE_SHARE_READ | FILE_SHARE_WRITE,
-                        FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE);
+                        FILE_SYNCHRONOUS_IO_NONALERT |
+                            (IsDirectory ? FILE_DIRECTORY_FILE
+                                         : FILE_NON_DIRECTORY_FILE));
     if (NT_SUCCESS(Status))
+    {
         NtClose(FileHandle);
+    }
     else
-        DPRINT("Failed to open file '%wZ', Status 0x%08lx\n", &FileName, Status);
+    {
+        DPRINT("Failed to open %s '%wZ', Status 0x%08lx\n",
+               IsDirectory ? "directory" : "file",
+               &Name, Status);
+    }
 
     return NT_SUCCESS(Status);
 }
index 0b82069..a9fabf4 100644 (file)
@@ -61,14 +61,16 @@ CombinePaths(
     IN /* PCWSTR */ ...);
 
 BOOLEAN
-DoesPathExist(
+DoesPathExistEx(
     IN HANDLE RootDirectory OPTIONAL,
-    IN PCWSTR PathName);
+    IN PCWSTR PathName,
+    IN BOOLEAN IsDirectory);
 
-BOOLEAN
-DoesFileExist(
-    IN HANDLE RootDirectory OPTIONAL,
-    IN PCWSTR PathNameToFile);
+#define DoesPathExist(RootDirectory, PathName)  \
+    DoesPathExistEx((RootDirectory), (PathName), TRUE)
+
+#define DoesFileExist(RootDirectory, FileName)  \
+    DoesPathExistEx((RootDirectory), (FileName), FALSE)
 
 // FIXME: DEPRECATED! HACKish function that needs to be deprecated!
 BOOLEAN