mirror of
https://github.com/reactos/reactos.git
synced 2025-06-14 02:38:31 +00:00
[SETUPLIB] Split OpenAndMapFile function and Doxygen-ize it
This commit is contained in:
parent
e8e770fd14
commit
68c2a28973
5 changed files with 140 additions and 48 deletions
|
@ -345,9 +345,9 @@ Quit:
|
||||||
Status = OpenAndMapFile(NULL,
|
Status = OpenAndMapFile(NULL,
|
||||||
UnattendInfPath,
|
UnattendInfPath,
|
||||||
&UnattendFileHandle,
|
&UnattendFileHandle,
|
||||||
|
&FileSize,
|
||||||
&SectionHandle,
|
&SectionHandle,
|
||||||
&ViewBase,
|
&ViewBase,
|
||||||
&FileSize,
|
|
||||||
FALSE);
|
FALSE);
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
|
|
|
@ -190,7 +190,7 @@ FindBootStore( // By handle
|
||||||
#if 0
|
#if 0
|
||||||
/* Check whether the loader configuration file exists */
|
/* Check whether the loader configuration file exists */
|
||||||
Status = OpenAndMapFile(PartitionDirectoryHandle, NtosBootLoaders[Type].LoaderConfigurationFile,
|
Status = OpenAndMapFile(PartitionDirectoryHandle, NtosBootLoaders[Type].LoaderConfigurationFile,
|
||||||
&FileHandle, &SectionHandle, &ViewBase, &FileSize, FALSE);
|
&FileHandle, &FileSize, &SectionHandle, &ViewBase, FALSE);
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
/* The loader does not exist, continue with another one */
|
/* The loader does not exist, continue with another one */
|
||||||
|
@ -343,9 +343,9 @@ OpenIniBootLoaderStore(
|
||||||
Status = OpenAndMapFile(PartitionDirectoryHandle,
|
Status = OpenAndMapFile(PartitionDirectoryHandle,
|
||||||
NtosBootLoaders[Type].LoaderConfigurationFile,
|
NtosBootLoaders[Type].LoaderConfigurationFile,
|
||||||
&BootStore->FileHandle,
|
&BootStore->FileHandle,
|
||||||
|
&BootStore->FileSize,
|
||||||
&BootStore->SectionHandle,
|
&BootStore->SectionHandle,
|
||||||
&BootStore->ViewBase,
|
&BootStore->ViewBase,
|
||||||
&BootStore->FileSize,
|
|
||||||
TRUE);
|
TRUE);
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
|
|
|
@ -854,26 +854,52 @@ Quit:
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief
|
||||||
|
* Opens and maps a file in memory.
|
||||||
|
*
|
||||||
|
* @param[in] RootDirectory
|
||||||
|
* @param[in] PathNameToFile
|
||||||
|
* Path to the file, either in absolute form, or relative to the opened
|
||||||
|
* root directory given by the RootDirectory handle.
|
||||||
|
*
|
||||||
|
* @param[out] FileHandle
|
||||||
|
* An optional pointer to a variable receiving a handle to the opened file.
|
||||||
|
* If NULL, the underlying file handle is closed.
|
||||||
|
*
|
||||||
|
* @param[out] FileSize
|
||||||
|
* An optional pointer to a variable receiving the size of the opened file.
|
||||||
|
*
|
||||||
|
* @param[out] SectionHandle
|
||||||
|
* A pointer to a variable receiving a handle to a section mapping the file.
|
||||||
|
*
|
||||||
|
* @param[out] BaseAddress
|
||||||
|
* A pointer to a variable receiving the address where the file is mapped.
|
||||||
|
*
|
||||||
|
* @param[in] ReadWriteAccess
|
||||||
|
* A boolean variable specifying whether to map the file for read and write
|
||||||
|
* access (TRUE), or read-only access (FALSE).
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* STATUS_SUCCESS in case of success, or a status code in case of error.
|
||||||
|
**/
|
||||||
NTSTATUS
|
NTSTATUS
|
||||||
OpenAndMapFile(
|
OpenAndMapFile(
|
||||||
IN HANDLE RootDirectory OPTIONAL,
|
_In_opt_ HANDLE RootDirectory,
|
||||||
IN PCWSTR PathNameToFile,
|
_In_ PCWSTR PathNameToFile,
|
||||||
OUT PHANDLE FileHandle, // IN OUT PHANDLE OPTIONAL
|
_Out_opt_ PHANDLE FileHandle,
|
||||||
OUT PHANDLE SectionHandle,
|
_Out_opt_ PULONG FileSize,
|
||||||
OUT PVOID* BaseAddress,
|
_Out_ PHANDLE SectionHandle,
|
||||||
OUT PULONG FileSize OPTIONAL,
|
_Out_ PVOID* BaseAddress,
|
||||||
IN BOOLEAN ReadWriteAccess)
|
_In_ BOOLEAN ReadWriteAccess)
|
||||||
{
|
{
|
||||||
NTSTATUS Status;
|
NTSTATUS Status;
|
||||||
UNICODE_STRING FileName;
|
UNICODE_STRING FileName;
|
||||||
OBJECT_ATTRIBUTES ObjectAttributes;
|
OBJECT_ATTRIBUTES ObjectAttributes;
|
||||||
IO_STATUS_BLOCK IoStatusBlock;
|
IO_STATUS_BLOCK IoStatusBlock;
|
||||||
ULONG SectionPageProtection;
|
HANDLE LocalFileHandle;
|
||||||
SIZE_T ViewSize;
|
|
||||||
PVOID ViewBase;
|
|
||||||
|
|
||||||
/* Open the file */
|
/* Open the file */
|
||||||
|
|
||||||
RtlInitUnicodeString(&FileName, PathNameToFile);
|
RtlInitUnicodeString(&FileName, PathNameToFile);
|
||||||
InitializeObjectAttributes(&ObjectAttributes,
|
InitializeObjectAttributes(&ObjectAttributes,
|
||||||
&FileName,
|
&FileName,
|
||||||
|
@ -881,10 +907,8 @@ OpenAndMapFile(
|
||||||
RootDirectory,
|
RootDirectory,
|
||||||
NULL);
|
NULL);
|
||||||
|
|
||||||
*FileHandle = NULL;
|
if (FileHandle) *FileHandle = NULL;
|
||||||
*SectionHandle = NULL;
|
Status = NtOpenFile(&LocalFileHandle,
|
||||||
|
|
||||||
Status = NtOpenFile(FileHandle,
|
|
||||||
FILE_GENERIC_READ | // Contains SYNCHRONIZE
|
FILE_GENERIC_READ | // Contains SYNCHRONIZE
|
||||||
(ReadWriteAccess ? FILE_GENERIC_WRITE : 0),
|
(ReadWriteAccess ? FILE_GENERIC_WRITE : 0),
|
||||||
&ObjectAttributes,
|
&ObjectAttributes,
|
||||||
|
@ -893,7 +917,7 @@ OpenAndMapFile(
|
||||||
FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE);
|
FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE);
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
DPRINT1("Failed to open file '%wZ', Status 0x%08lx\n", &FileName, Status);
|
DPRINT1("Failed to open file '%wZ' (Status 0x%08lx)\n", &FileName, Status);
|
||||||
return Status;
|
return Status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -901,32 +925,82 @@ OpenAndMapFile(
|
||||||
{
|
{
|
||||||
/* Query the file size */
|
/* Query the file size */
|
||||||
FILE_STANDARD_INFORMATION FileInfo;
|
FILE_STANDARD_INFORMATION FileInfo;
|
||||||
Status = NtQueryInformationFile(*FileHandle,
|
Status = NtQueryInformationFile(LocalFileHandle,
|
||||||
&IoStatusBlock,
|
&IoStatusBlock,
|
||||||
&FileInfo,
|
&FileInfo,
|
||||||
sizeof(FileInfo),
|
sizeof(FileInfo),
|
||||||
FileStandardInformation);
|
FileStandardInformation);
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
DPRINT("NtQueryInformationFile() failed (Status %lx)\n", Status);
|
DPRINT("NtQueryInformationFile() failed (Status 0x%08lx)\n", Status);
|
||||||
NtClose(*FileHandle);
|
goto Quit;
|
||||||
*FileHandle = NULL;
|
|
||||||
return Status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (FileInfo.EndOfFile.HighPart != 0)
|
if (FileInfo.EndOfFile.HighPart != 0)
|
||||||
DPRINT1("WARNING!! The file '%wZ' is too large!\n", &FileName);
|
DPRINT1("WARNING!! The file '%wZ' is too large!\n", &FileName);
|
||||||
|
|
||||||
*FileSize = FileInfo.EndOfFile.LowPart;
|
*FileSize = FileInfo.EndOfFile.LowPart;
|
||||||
|
|
||||||
DPRINT("File size: %lu\n", *FileSize);
|
DPRINT("File size: %lu\n", *FileSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Map the file in memory */
|
/* Map the whole file into memory */
|
||||||
|
Status = MapFile(LocalFileHandle,
|
||||||
|
SectionHandle,
|
||||||
|
BaseAddress,
|
||||||
|
ReadWriteAccess);
|
||||||
|
if (!NT_SUCCESS(Status))
|
||||||
|
{
|
||||||
|
DPRINT1("Failed to map file '%wZ' (Status 0x%08lx)\n", &FileName, Status);
|
||||||
|
goto Quit;
|
||||||
|
}
|
||||||
|
|
||||||
|
Quit:
|
||||||
|
/* If we succeeded, return the opened file handle if needed.
|
||||||
|
* If we failed or the caller does not need the handle, close it now. */
|
||||||
|
if (NT_SUCCESS(Status) && FileHandle)
|
||||||
|
*FileHandle = LocalFileHandle;
|
||||||
|
else
|
||||||
|
NtClose(LocalFileHandle);
|
||||||
|
|
||||||
|
return Status;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief
|
||||||
|
* Maps an opened file in memory.
|
||||||
|
*
|
||||||
|
* @param[in] FileHandle
|
||||||
|
* A handle to an opened file to map.
|
||||||
|
*
|
||||||
|
* @param[out] SectionHandle
|
||||||
|
* A pointer to a variable receiving a handle to a section mapping the file.
|
||||||
|
*
|
||||||
|
* @param[out] BaseAddress
|
||||||
|
* A pointer to a variable receiving the address where the file is mapped.
|
||||||
|
*
|
||||||
|
* @param[in] ReadWriteAccess
|
||||||
|
* A boolean variable specifying whether to map the file for read and write
|
||||||
|
* access (TRUE), or read-only access (FALSE).
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* STATUS_SUCCESS in case of success, or a status code in case of error.
|
||||||
|
**/
|
||||||
|
NTSTATUS
|
||||||
|
MapFile(
|
||||||
|
_In_ HANDLE FileHandle,
|
||||||
|
_Out_ PHANDLE SectionHandle,
|
||||||
|
_Out_ PVOID* BaseAddress,
|
||||||
|
_In_ BOOLEAN ReadWriteAccess)
|
||||||
|
{
|
||||||
|
NTSTATUS Status;
|
||||||
|
ULONG SectionPageProtection;
|
||||||
|
SIZE_T ViewSize;
|
||||||
|
PVOID ViewBase;
|
||||||
|
|
||||||
SectionPageProtection = (ReadWriteAccess ? PAGE_READWRITE : PAGE_READONLY);
|
SectionPageProtection = (ReadWriteAccess ? PAGE_READWRITE : PAGE_READONLY);
|
||||||
|
|
||||||
/* Create the section */
|
/* Create the section */
|
||||||
|
*SectionHandle = NULL;
|
||||||
Status = NtCreateSection(SectionHandle,
|
Status = NtCreateSection(SectionHandle,
|
||||||
STANDARD_RIGHTS_REQUIRED | SECTION_QUERY |
|
STANDARD_RIGHTS_REQUIRED | SECTION_QUERY |
|
||||||
SECTION_MAP_READ |
|
SECTION_MAP_READ |
|
||||||
|
@ -935,12 +1009,11 @@ OpenAndMapFile(
|
||||||
NULL,
|
NULL,
|
||||||
SectionPageProtection,
|
SectionPageProtection,
|
||||||
SEC_COMMIT /* | SEC_IMAGE (_NO_EXECUTE) */,
|
SEC_COMMIT /* | SEC_IMAGE (_NO_EXECUTE) */,
|
||||||
*FileHandle);
|
FileHandle);
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
DPRINT1("Failed to create a memory section for file '%wZ', Status 0x%08lx\n", &FileName, Status);
|
DPRINT1("Failed to create a memory section for file 0x%p (Status 0x%08lx)\n",
|
||||||
NtClose(*FileHandle);
|
FileHandle, Status);
|
||||||
*FileHandle = NULL;
|
|
||||||
return Status;
|
return Status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -958,11 +1031,10 @@ OpenAndMapFile(
|
||||||
SectionPageProtection);
|
SectionPageProtection);
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
DPRINT1("Failed to map a view for file '%wZ', Status 0x%08lx\n", &FileName, Status);
|
DPRINT1("Failed to map a view for file 0x%p (Status 0x%08lx)\n",
|
||||||
|
FileHandle, Status);
|
||||||
NtClose(*SectionHandle);
|
NtClose(*SectionHandle);
|
||||||
*SectionHandle = NULL;
|
*SectionHandle = NULL;
|
||||||
NtClose(*FileHandle);
|
|
||||||
*FileHandle = NULL;
|
|
||||||
return Status;
|
return Status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -970,10 +1042,23 @@ OpenAndMapFile(
|
||||||
return STATUS_SUCCESS;
|
return STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief
|
||||||
|
* Unmaps a mapped file by section.
|
||||||
|
*
|
||||||
|
* @param[in] SectionHandle
|
||||||
|
* The handle to the section mapping the file.
|
||||||
|
*
|
||||||
|
* @param[in] BaseAddress
|
||||||
|
* The base address where the file is mapped.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* TRUE if the file was successfully unmapped; FALSE if an error occurred.
|
||||||
|
**/
|
||||||
BOOLEAN
|
BOOLEAN
|
||||||
UnMapFile(
|
UnMapFile(
|
||||||
IN HANDLE SectionHandle,
|
_In_ HANDLE SectionHandle,
|
||||||
IN PVOID BaseAddress)
|
_In_ PVOID BaseAddress)
|
||||||
{
|
{
|
||||||
NTSTATUS Status;
|
NTSTATUS Status;
|
||||||
BOOLEAN Success = TRUE;
|
BOOLEAN Success = TRUE;
|
||||||
|
@ -981,14 +1066,14 @@ UnMapFile(
|
||||||
Status = NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
|
Status = NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
DPRINT1("UnMapFile: NtUnmapViewOfSection(0x%p) failed with Status 0x%08lx\n",
|
DPRINT1("NtUnmapViewOfSection(0x%p) failed (Status 0x%08lx)\n",
|
||||||
BaseAddress, Status);
|
BaseAddress, Status);
|
||||||
Success = FALSE;
|
Success = FALSE;
|
||||||
}
|
}
|
||||||
Status = NtClose(SectionHandle);
|
Status = NtClose(SectionHandle);
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
DPRINT1("UnMapFile: NtClose(0x%p) failed with Status 0x%08lx\n",
|
DPRINT1("NtClose(0x%p) failed (Status 0x%08lx)\n",
|
||||||
SectionHandle, Status);
|
SectionHandle, Status);
|
||||||
Success = FALSE;
|
Success = FALSE;
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,18 +92,25 @@ NtPathToDiskPartComponents(
|
||||||
|
|
||||||
NTSTATUS
|
NTSTATUS
|
||||||
OpenAndMapFile(
|
OpenAndMapFile(
|
||||||
IN HANDLE RootDirectory OPTIONAL,
|
_In_opt_ HANDLE RootDirectory,
|
||||||
IN PCWSTR PathNameToFile,
|
_In_ PCWSTR PathNameToFile,
|
||||||
OUT PHANDLE FileHandle, // IN OUT PHANDLE OPTIONAL
|
_Out_opt_ PHANDLE FileHandle,
|
||||||
OUT PHANDLE SectionHandle,
|
_Out_opt_ PULONG FileSize,
|
||||||
OUT PVOID* BaseAddress,
|
_Out_ PHANDLE SectionHandle,
|
||||||
OUT PULONG FileSize OPTIONAL,
|
_Out_ PVOID* BaseAddress,
|
||||||
IN BOOLEAN ReadWriteAccess);
|
_In_ BOOLEAN ReadWriteAccess);
|
||||||
|
|
||||||
|
NTSTATUS
|
||||||
|
MapFile(
|
||||||
|
_In_ HANDLE FileHandle,
|
||||||
|
_Out_ PHANDLE SectionHandle,
|
||||||
|
_Out_ PVOID* BaseAddress,
|
||||||
|
_In_ BOOLEAN ReadWriteAccess);
|
||||||
|
|
||||||
BOOLEAN
|
BOOLEAN
|
||||||
UnMapFile(
|
UnMapFile(
|
||||||
IN HANDLE SectionHandle,
|
_In_ HANDLE SectionHandle,
|
||||||
IN PVOID BaseAddress);
|
_In_ PVOID BaseAddress);
|
||||||
|
|
||||||
#define UnMapAndCloseFile(FileHandle, SectionHandle, BaseAddress) \
|
#define UnMapAndCloseFile(FileHandle, SectionHandle, BaseAddress) \
|
||||||
do { \
|
do { \
|
||||||
|
|
|
@ -263,8 +263,8 @@ CheckForValidPEAndVendor(
|
||||||
VendorName->Length = 0;
|
VendorName->Length = 0;
|
||||||
|
|
||||||
Status = OpenAndMapFile(RootDirectory, PathNameToFile,
|
Status = OpenAndMapFile(RootDirectory, PathNameToFile,
|
||||||
&FileHandle, &SectionHandle, &ViewBase,
|
&FileHandle, NULL,
|
||||||
NULL, FALSE);
|
&SectionHandle, &ViewBase, FALSE);
|
||||||
if (!NT_SUCCESS(Status))
|
if (!NT_SUCCESS(Status))
|
||||||
{
|
{
|
||||||
DPRINT1("Failed to open and map file '%S', Status 0x%08lx\n", PathNameToFile, Status);
|
DPRINT1("Failed to open and map file '%S', Status 0x%08lx\n", PathNameToFile, Status);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue