[SETUPLIB] Additions for filesup.c and inicache.c.
authorHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Wed, 31 May 2017 00:18:13 +0000 (00:18 +0000)
committerHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Sun, 3 Jun 2018 20:12:44 +0000 (22:12 +0200)
- In DoesFileExist(): Call NtOpenFile with FILE_GENERIC_READ instead of the more generic GENERIC_READ access right.
- OpenAndMapFile(): Add support for opening & mapping files with write access (to be used latter).

svn path=/branches/setup_improvements/; revision=74710

- Split IniCacheLoad() and IniCacheSave() into: themselves & IniCacheLoadByHandle() and IniCacheSaveByHandle(),
  respectively, so that we can load & save INI files if we already have an opened handle to them.

svn path=/branches/setup_improvements/; revision=74711

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

index 5a9ba00..0564877 100644 (file)
@@ -179,7 +179,7 @@ DoesFileExist(
                                NULL);
 
     Status = NtOpenFile(&FileHandle,
-                        GENERIC_READ | SYNCHRONIZE,
+                        FILE_GENERIC_READ, // Contains SYNCHRONIZE
                         &ObjectAttributes,
                         &IoStatusBlock,
                         FILE_SHARE_READ | FILE_SHARE_WRITE,
@@ -305,20 +305,24 @@ Quit:
 
 NTSTATUS
 OpenAndMapFile(
-    IN HANDLE RootDirectory OPTIONAL,
-    IN PCWSTR PathNameToFile,
+    IN  HANDLE RootDirectory OPTIONAL,
+    IN  PCWSTR PathNameToFile,
     OUT PHANDLE FileHandle,         // IN OUT PHANDLE OPTIONAL
     OUT PHANDLE SectionHandle,
     OUT PVOID* BaseAddress,
-    OUT PULONG FileSize OPTIONAL)
+    OUT PULONG FileSize OPTIONAL,
+    IN  BOOLEAN ReadWriteAccess)
 {
     NTSTATUS Status;
     UNICODE_STRING FileName;
     OBJECT_ATTRIBUTES ObjectAttributes;
     IO_STATUS_BLOCK IoStatusBlock;
+    ULONG SectionPageProtection;
     SIZE_T ViewSize;
     PVOID ViewBase;
 
+    /* Open the file */
+
     RtlInitUnicodeString(&FileName, PathNameToFile);
 
     InitializeObjectAttributes(&ObjectAttributes,
@@ -331,7 +335,8 @@ OpenAndMapFile(
     *SectionHandle = NULL;
 
     Status = NtOpenFile(FileHandle,
-                        GENERIC_READ | SYNCHRONIZE,
+                        FILE_GENERIC_READ | // Contains SYNCHRONIZE
+                            (ReadWriteAccess ? FILE_GENERIC_WRITE : 0),
                         &ObjectAttributes,
                         &IoStatusBlock,
                         FILE_SHARE_READ,
@@ -369,12 +374,16 @@ OpenAndMapFile(
 
     /* Map the file in memory */
 
+    SectionPageProtection = (ReadWriteAccess ? PAGE_READWRITE : PAGE_READONLY);
+
     /* Create the section */
     Status = NtCreateSection(SectionHandle,
-                             SECTION_MAP_READ,
+                             STANDARD_RIGHTS_REQUIRED | SECTION_QUERY |
+                             SECTION_MAP_READ |
+                                (ReadWriteAccess ? SECTION_MAP_WRITE : 0),
                              NULL,
                              NULL,
-                             PAGE_READONLY,
+                             SectionPageProtection,
                              SEC_COMMIT /* | SEC_IMAGE (_NO_EXECUTE) */,
                              *FileHandle);
     if (!NT_SUCCESS(Status))
@@ -396,7 +405,7 @@ OpenAndMapFile(
                                 &ViewSize,
                                 ViewShare,
                                 0,
-                                PAGE_READONLY);
+                                SectionPageProtection);
     if (!NT_SUCCESS(Status))
     {
         DPRINT1("Failed to map a view for file '%wZ', Status 0x%08lx\n", &FileName, Status);
index f944d96..abc1b1c 100644 (file)
@@ -68,12 +68,13 @@ NtPathToDiskPartComponents(
 
 NTSTATUS
 OpenAndMapFile(
-    IN HANDLE RootDirectory OPTIONAL,
-    IN PCWSTR PathNameToFile,
+    IN  HANDLE RootDirectory OPTIONAL,
+    IN  PCWSTR PathNameToFile,
     OUT PHANDLE FileHandle,         // IN OUT PHANDLE OPTIONAL
     OUT PHANDLE SectionHandle,
     OUT PVOID* BaseAddress,
-    OUT PULONG FileSize OPTIONAL);
+    OUT PULONG FileSize OPTIONAL,
+    IN  BOOLEAN ReadWriteAccess);
 
 BOOLEAN
 UnMapFile(
index 295d957..de0d6f6 100644 (file)
@@ -544,46 +544,20 @@ IniCacheLoadFromMemory(
 }
 
 NTSTATUS
-IniCacheLoad(
+IniCacheLoadByHandle(
     PINICACHE *Cache,
-    PWCHAR FileName,
+    HANDLE FileHandle,
     BOOLEAN String)
 {
-    UNICODE_STRING Name;
-    OBJECT_ATTRIBUTES ObjectAttributes;
-    FILE_STANDARD_INFORMATION FileInfo;
-    IO_STATUS_BLOCK IoStatusBlock;
-    HANDLE FileHandle;
     NTSTATUS Status;
+    IO_STATUS_BLOCK IoStatusBlock;
+    FILE_STANDARD_INFORMATION FileInfo;
     PCHAR FileBuffer;
     ULONG FileLength;
     LARGE_INTEGER FileOffset;
 
     *Cache = NULL;
 
-    /* Open ini file */
-    RtlInitUnicodeString(&Name, FileName);
-
-    InitializeObjectAttributes(&ObjectAttributes,
-                               &Name,
-                               0,
-                               NULL,
-                               NULL);
-
-    Status = NtOpenFile(&FileHandle,
-                        GENERIC_READ | SYNCHRONIZE,
-                        &ObjectAttributes,
-                        &IoStatusBlock,
-                        FILE_SHARE_READ,
-                        FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE);
-    if (!NT_SUCCESS(Status))
-    {
-        DPRINT("NtOpenFile() failed (Status %lx)\n", Status);
-        return Status;
-    }
-
-    DPRINT("NtOpenFile() successful\n");
-
     /* Query file size */
     Status = NtQueryInformationFile(FileHandle,
                                     &IoStatusBlock,
@@ -593,7 +567,6 @@ IniCacheLoad(
     if (!NT_SUCCESS(Status))
     {
         DPRINT("NtQueryInformationFile() failed (Status %lx)\n", Status);
-        NtClose(FileHandle);
         return Status;
     }
 
@@ -608,7 +581,6 @@ IniCacheLoad(
     if (FileBuffer == NULL)
     {
         DPRINT1("RtlAllocateHeap() failed\n");
-        NtClose(FileHandle);
         return STATUS_INSUFFICIENT_RESOURCES;
     }
 
@@ -627,8 +599,6 @@ IniCacheLoad(
     /* Append NULL-terminator */
     FileBuffer[FileLength] = 0;
 
-    NtClose(FileHandle);
-
     if (!NT_SUCCESS(Status))
     {
         DPRINT("NtReadFile() failed (Status %lx)\n", Status);
@@ -647,6 +617,50 @@ Quit:
     return Status;
 }
 
+NTSTATUS
+IniCacheLoad(
+    PINICACHE *Cache,
+    PWCHAR FileName,
+    BOOLEAN String)
+{
+    NTSTATUS Status;
+    UNICODE_STRING Name;
+    OBJECT_ATTRIBUTES ObjectAttributes;
+    IO_STATUS_BLOCK IoStatusBlock;
+    HANDLE FileHandle;
+
+    *Cache = NULL;
+
+    /* Open the INI file */
+    RtlInitUnicodeString(&Name, FileName);
+
+    InitializeObjectAttributes(&ObjectAttributes,
+                               &Name,
+                               OBJ_CASE_INSENSITIVE,
+                               NULL,
+                               NULL);
+
+    Status = NtOpenFile(&FileHandle,
+                        FILE_GENERIC_READ | SYNCHRONIZE,
+                        &ObjectAttributes,
+                        &IoStatusBlock,
+                        FILE_SHARE_READ,
+                        FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE);
+    if (!NT_SUCCESS(Status))
+    {
+        DPRINT("NtOpenFile() failed (Status %lx)\n", Status);
+        return Status;
+    }
+
+    DPRINT("NtOpenFile() successful\n");
+
+    Status = IniCacheLoadByHandle(Cache, FileHandle, String);
+
+    /* Close the INI file */
+    NtClose(FileHandle);
+    return Status;
+}
+
 
 VOID
 IniCacheDestroy(
@@ -931,24 +945,19 @@ IniCacheCreate(VOID)
 
 
 NTSTATUS
-IniCacheSave(
+IniCacheSaveByHandle(
     PINICACHE Cache,
-    PWCHAR FileName)
+    HANDLE FileHandle)
 {
-    UNICODE_STRING Name;
+    NTSTATUS Status;
     PINICACHESECTION Section;
     PINICACHEKEY Key;
     ULONG BufferSize;
     PCHAR Buffer;
     PCHAR Ptr;
     ULONG Len;
-    NTSTATUS Status;
-
-    OBJECT_ATTRIBUTES ObjectAttributes;
     IO_STATUS_BLOCK IoStatusBlock;
     LARGE_INTEGER Offset;
-    HANDLE FileHandle;
-
 
     /* Calculate required buffer size */
     BufferSize = 0;
@@ -1008,7 +1017,40 @@ IniCacheSave(
         }
     }
 
-    /* Create ini file */
+    /* Write to the INI file */
+    Offset.QuadPart = 0LL;
+    Status = NtWriteFile(FileHandle,
+                         NULL,
+                         NULL,
+                         NULL,
+                         &IoStatusBlock,
+                         Buffer,
+                         BufferSize,
+                         &Offset,
+                         NULL);
+    if (!NT_SUCCESS(Status))
+    {
+        DPRINT("NtWriteFile() failed (Status %lx)\n", Status);
+        RtlFreeHeap(ProcessHeap, 0, Buffer);
+        return Status;
+    }
+
+    RtlFreeHeap(ProcessHeap, 0, Buffer);
+    return STATUS_SUCCESS;
+}
+
+NTSTATUS
+IniCacheSave(
+    PINICACHE Cache,
+    PWCHAR FileName)
+{
+    NTSTATUS Status;
+    UNICODE_STRING Name;
+    OBJECT_ATTRIBUTES ObjectAttributes;
+    IO_STATUS_BLOCK IoStatusBlock;
+    HANDLE FileHandle;
+
+    /* Create the INI file */
     RtlInitUnicodeString(&Name, FileName);
 
     InitializeObjectAttributes(&ObjectAttributes,
@@ -1018,46 +1060,27 @@ IniCacheSave(
                                NULL);
 
     Status = NtCreateFile(&FileHandle,
-                          GENERIC_WRITE | SYNCHRONIZE,
+                          FILE_GENERIC_WRITE | SYNCHRONIZE,
                           &ObjectAttributes,
                           &IoStatusBlock,
                           NULL,
                           FILE_ATTRIBUTE_NORMAL,
                           0,
                           FILE_SUPERSEDE,
-                          FILE_SYNCHRONOUS_IO_NONALERT | FILE_SEQUENTIAL_ONLY,
+                          FILE_SYNCHRONOUS_IO_NONALERT | FILE_SEQUENTIAL_ONLY | FILE_NON_DIRECTORY_FILE,
                           NULL,
                           0);
     if (!NT_SUCCESS(Status))
     {
         DPRINT("NtCreateFile() failed (Status %lx)\n", Status);
-        RtlFreeHeap(ProcessHeap, 0, Buffer);
         return Status;
     }
 
-    Offset.QuadPart = 0LL;
-    Status = NtWriteFile(FileHandle,
-                         NULL,
-                         NULL,
-                         NULL,
-                         &IoStatusBlock,
-                         Buffer,
-                         BufferSize,
-                         &Offset,
-                         NULL);
-    if (!NT_SUCCESS(Status))
-    {
-      DPRINT("NtWriteFile() failed (Status %lx)\n", Status);
-      NtClose(FileHandle);
-      RtlFreeHeap(ProcessHeap, 0, Buffer);
-      return Status;
-    }
+    Status = IniCacheSaveByHandle(Cache, FileHandle);
 
+    /* Close the INI file */
     NtClose(FileHandle);
-
-    RtlFreeHeap(ProcessHeap, 0, Buffer);
-
-    return STATUS_SUCCESS;
+    return Status;
 }
 
 
index d9b7a3d..7d396d7 100644 (file)
@@ -60,6 +60,12 @@ IniCacheLoadFromMemory(
     ULONG FileLength,
     BOOLEAN String);
 
+NTSTATUS
+IniCacheLoadByHandle(
+    PINICACHE *Cache,
+    HANDLE FileHandle,
+    BOOLEAN String);
+
 NTSTATUS
 IniCacheLoad(
     PINICACHE *Cache,
@@ -109,6 +115,11 @@ IniCacheInsertKey(
 PINICACHE
 IniCacheCreate(VOID);
 
+NTSTATUS
+IniCacheSaveByHandle(
+    PINICACHE Cache,
+    HANDLE FileHandle);
+
 NTSTATUS
 IniCacheSave(
     PINICACHE Cache,