[SETUPLIB] Additions for filesup.c and inicache.c.

- 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
This commit is contained in:
Hermès Bélusca-Maïto 2017-05-31 00:18:13 +00:00
parent 6b6163a5d9
commit 7f5428633b
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
4 changed files with 131 additions and 87 deletions

View 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);

View 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(

View 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,33 +1017,7 @@ IniCacheSave(
}
}
/* Create ini file */
RtlInitUnicodeString(&Name, FileName);
InitializeObjectAttributes(&ObjectAttributes,
&Name,
0,
NULL,
NULL);
Status = NtCreateFile(&FileHandle,
GENERIC_WRITE | SYNCHRONIZE,
&ObjectAttributes,
&IoStatusBlock,
NULL,
FILE_ATTRIBUTE_NORMAL,
0,
FILE_SUPERSEDE,
FILE_SYNCHRONOUS_IO_NONALERT | FILE_SEQUENTIAL_ONLY,
NULL,
0);
if (!NT_SUCCESS(Status))
{
DPRINT("NtCreateFile() failed (Status %lx)\n", Status);
RtlFreeHeap(ProcessHeap, 0, Buffer);
return Status;
}
/* Write to the INI file */
Offset.QuadPart = 0LL;
Status = NtWriteFile(FileHandle,
NULL,
@ -1047,19 +1030,59 @@ IniCacheSave(
NULL);
if (!NT_SUCCESS(Status))
{
DPRINT("NtWriteFile() failed (Status %lx)\n", Status);
NtClose(FileHandle);
RtlFreeHeap(ProcessHeap, 0, Buffer);
return Status;
DPRINT("NtWriteFile() failed (Status %lx)\n", Status);
RtlFreeHeap(ProcessHeap, 0, Buffer);
return Status;
}
NtClose(FileHandle);
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,
&Name,
0,
NULL,
NULL);
Status = NtCreateFile(&FileHandle,
FILE_GENERIC_WRITE | SYNCHRONIZE,
&ObjectAttributes,
&IoStatusBlock,
NULL,
FILE_ATTRIBUTE_NORMAL,
0,
FILE_SUPERSEDE,
FILE_SYNCHRONOUS_IO_NONALERT | FILE_SEQUENTIAL_ONLY | FILE_NON_DIRECTORY_FILE,
NULL,
0);
if (!NT_SUCCESS(Status))
{
DPRINT("NtCreateFile() failed (Status %lx)\n", Status);
return Status;
}
Status = IniCacheSaveByHandle(Cache, FileHandle);
/* Close the INI file */
NtClose(FileHandle);
return Status;
}
PINICACHESECTION
IniCacheAppendSection(

View 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,