Basic source control rule: please don't change things and move them
around at the same time. Unless your goal is to make sure noone reviews
your changes, of course.
Thanks.
-Thomas
On 2017-05-15 03:52, hbelusca(a)svn.reactos.org wrote:
Author: hbelusca
Date: Mon May 15 01:52:34 2017
New Revision: 74549
URL:
http://svn.reactos.org/svn/reactos?rev=74549&view=rev
Log:
[USETUP]: Add & modify some file utility functions that are going to be used in the
next commit.
- ConcatPaths that concatenates paths (or a path and a file name);
- OpenAndMapFile (resp. UnMapFile), whose purpose is to open a file and map it in memory
(resp. unmap it from memory).
- Add extra optional parameters to DoesPathExist and DoesFileExist: an optional
"RootDirectory" handle and, for DoesFileExist only, an optional PathName.
Modified:
branches/setup_improvements/base/setup/usetup/filesup.c
branches/setup_improvements/base/setup/usetup/filesup.h
Modified: branches/setup_improvements/base/setup/usetup/filesup.c
URL:
http://svn.reactos.org/svn/reactos/branches/setup_improvements/base/setup/u…
==============================================================================
--- branches/setup_improvements/base/setup/usetup/filesup.c [iso-8859-1] (original)
+++ branches/setup_improvements/base/setup/usetup/filesup.c [iso-8859-1] Mon May 15
01:52:34 2017
@@ -93,59 +93,6 @@
return Status;
}
-
-static
-BOOLEAN
-DoesPathExist(
- PWSTR PathName)
-{
- OBJECT_ATTRIBUTES ObjectAttributes;
- IO_STATUS_BLOCK IoStatusBlock;
- UNICODE_STRING Name;
- HANDLE FileHandle;
- NTSTATUS Status;
-
- RtlInitUnicodeString(&Name, PathName);
-
- InitializeObjectAttributes(&ObjectAttributes,
- &Name,
- OBJ_CASE_INSENSITIVE,
- NULL,
- NULL);
-
- Status = NtOpenFile(&FileHandle,
- GENERIC_READ | SYNCHRONIZE,
- &ObjectAttributes,
- &IoStatusBlock,
- 0,
- FILE_SYNCHRONOUS_IO_NONALERT);
- if (NT_SUCCESS(Status))
- NtClose(FileHandle);
-
- return NT_SUCCESS(Status);
-}
-
-
-BOOLEAN
-IsValidPath(
- PWCHAR InstallDir,
- ULONG Length)
-{
- UINT i;
-
- // TODO: Add check for 8.3 too.
-
- /* Check for whitespaces */
- for (i = 0; i < Length; i++)
- {
- if (isspace(InstallDir[i]))
- return FALSE;
- }
-
- return TRUE;
-}
-
-
NTSTATUS
SetupCreateDirectory(
PWCHAR PathName)
@@ -183,7 +130,7 @@
*Ptr = 0;
DPRINT("PathBuffer: %S\n", PathBuffer);
- if (!DoesPathExist(PathBuffer))
+ if (!DoesPathExist(NULL, PathBuffer))
{
DPRINT("Create: %S\n", PathBuffer);
Status = SetupCreateSingleDirectory(PathBuffer);
@@ -197,7 +144,7 @@
Ptr++;
}
- if (!DoesPathExist(PathBuffer))
+ if (!DoesPathExist(NULL, PathBuffer))
{
DPRINT("Create: %S\n", PathBuffer);
Status = SetupCreateSingleDirectory(PathBuffer);
@@ -212,7 +159,6 @@
return Status;
}
-
NTSTATUS
SetupCopyFile(
@@ -233,7 +179,6 @@
SIZE_T SourceSectionSize = 0;
LARGE_INTEGER ByteOffset;
-#ifdef __REACTOS__
RtlInitUnicodeString(&FileName,
SourceFileName);
@@ -254,20 +199,6 @@
DPRINT1("NtOpenFile failed: %x, %wZ\n", Status, &FileName);
goto done;
}
-#else
- FileHandleSource = CreateFileW(SourceFileName,
- GENERIC_READ,
- FILE_SHARE_READ,
- NULL,
- OPEN_EXISTING,
- 0,
- NULL);
- if (FileHandleSource == INVALID_HANDLE_VALUE)
- {
- Status = STATUS_UNSUCCESSFUL;
- goto done;
- }
-#endif
Status = NtQueryInformationFile(FileHandleSource,
&IoStatusBlock,
@@ -484,44 +415,254 @@
}
#endif
+
+BOOLEAN
+IsValidPath(
+ IN PWCHAR InstallDir,
+ IN ULONG Length)
+{
+ UINT i;
+
+ // TODO: Add check for 8.3 too.
+
+ /* Check for whitespaces */
+ for (i = 0; i < Length; i++)
+ {
+ if (isspace(InstallDir[i]))
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+HRESULT
+ConcatPaths(
+ IN OUT PWSTR PathElem1,
+ IN SIZE_T cchPathSize,
+ IN PCWSTR PathElem2 OPTIONAL)
+{
+ HRESULT hr;
+ SIZE_T cchPathLen;
+
+ if (!PathElem2)
+ return S_OK;
+ if (cchPathSize <= 1)
+ return S_OK;
+
+ cchPathLen = min(cchPathSize, wcslen(PathElem1));
+
+ if (PathElem2[0] != L'\\' && cchPathLen > 0 &&
PathElem1[cchPathLen-1] != L'\\')
+ {
+ /* PathElem2 does not start with '\' and PathElem1 does not end with
'\' */
+ hr = StringCchCatW(PathElem1, cchPathSize, L"\\");
+ if (FAILED(hr))
+ return hr;
+ }
+ else if (PathElem2[0] == L'\\' && cchPathLen > 0 &&
PathElem1[cchPathLen-1] == L'\\')
+ {
+ /* PathElem2 starts with '\' and PathElem1 ends with '\' */
+ while (*PathElem2 == L'\\')
+ ++PathElem2; // Skip any backslash
+ }
+ hr = StringCchCatW(PathElem1, cchPathSize, PathElem2);
+ return hr;
+}
+
+//
+// NOTE: It may be possible to merge both DoesPathExist and DoesFileExist...
+//
+BOOLEAN
+DoesPathExist(
+ IN HANDLE RootDirectory OPTIONAL,
+ IN PCWSTR PathName)
+{
+ NTSTATUS Status;
+ HANDLE FileHandle;
+ OBJECT_ATTRIBUTES ObjectAttributes;
+ IO_STATUS_BLOCK IoStatusBlock;
+ UNICODE_STRING Name;
+
+ RtlInitUnicodeString(&Name, PathName);
+
+ InitializeObjectAttributes(&ObjectAttributes,
+ &Name,
+ OBJ_CASE_INSENSITIVE,
+ RootDirectory,
+ NULL);
+
+ Status = NtOpenFile(&FileHandle,
+ FILE_LIST_DIRECTORY | SYNCHRONIZE,
+ &ObjectAttributes,
+ &IoStatusBlock,
+ FILE_SHARE_READ | FILE_SHARE_WRITE,
+ FILE_SYNCHRONOUS_IO_NONALERT | FILE_DIRECTORY_FILE);
+ if (NT_SUCCESS(Status))
+ NtClose(FileHandle);
+ else
+ DPRINT1("Failed to open directory %wZ, Status 0x%08lx\n", &Name,
Status);
+
+ return NT_SUCCESS(Status);
+}
+
BOOLEAN
DoesFileExist(
- PWSTR PathName,
- PWSTR FileName)
-{
+ IN HANDLE RootDirectory OPTIONAL,
+ IN PCWSTR PathName OPTIONAL,
+ IN PCWSTR FileName)
+{
+ NTSTATUS Status;
+ HANDLE FileHandle;
OBJECT_ATTRIBUTES ObjectAttributes;
IO_STATUS_BLOCK IoStatusBlock;
UNICODE_STRING Name;
WCHAR FullName[MAX_PATH];
- HANDLE FileHandle;
- NTSTATUS Status;
-
- wcscpy(FullName, PathName);
- if (FileName != NULL)
- {
- if (FileName[0] != L'\\')
- wcscat(FullName, L"\\");
- wcscat(FullName, FileName);
- }
+
+ if (PathName)
+ StringCchCopyW(FullName, ARRAYSIZE(FullName), PathName);
+ else
+ FullName[0] = UNICODE_NULL;
+
+ if (FileName)
+ ConcatPaths(FullName, ARRAYSIZE(FullName), FileName);
RtlInitUnicodeString(&Name, FullName);
InitializeObjectAttributes(&ObjectAttributes,
&Name,
OBJ_CASE_INSENSITIVE,
- NULL,
+ RootDirectory,
NULL);
Status = NtOpenFile(&FileHandle,
GENERIC_READ | SYNCHRONIZE,
&ObjectAttributes,
&IoStatusBlock,
- 0,
- FILE_SYNCHRONOUS_IO_NONALERT);
+ FILE_SHARE_READ | FILE_SHARE_WRITE,
+ FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE);
if (NT_SUCCESS(Status))
NtClose(FileHandle);
+ else
+ DPRINT1("Failed to open file %wZ, Status 0x%08lx\n", &Name,
Status);
return NT_SUCCESS(Status);
}
+NTSTATUS
+OpenAndMapFile(
+ IN HANDLE RootDirectory OPTIONAL,
+ IN PCWSTR PathName OPTIONAL,
+ IN PCWSTR FileName, // OPTIONAL
+ OUT PHANDLE FileHandle, // IN OUT PHANDLE OPTIONAL
+ OUT PHANDLE SectionHandle,
+ OUT PVOID* BaseAddress)
+{
+ NTSTATUS Status;
+ OBJECT_ATTRIBUTES ObjectAttributes;
+ IO_STATUS_BLOCK IoStatusBlock;
+ SIZE_T ViewSize;
+ PVOID ViewBase;
+ UNICODE_STRING Name;
+ WCHAR FullName[MAX_PATH];
+
+ if (PathName)
+ StringCchCopyW(FullName, ARRAYSIZE(FullName), PathName);
+ else
+ FullName[0] = UNICODE_NULL;
+
+ if (FileName)
+ ConcatPaths(FullName, ARRAYSIZE(FullName), FileName);
+
+ RtlInitUnicodeString(&Name, FullName);
+
+ InitializeObjectAttributes(&ObjectAttributes,
+ &Name,
+ OBJ_CASE_INSENSITIVE,
+ RootDirectory,
+ NULL);
+
+ *FileHandle = NULL;
+ *SectionHandle = NULL;
+
+ Status = NtOpenFile(FileHandle,
+ GENERIC_READ | SYNCHRONIZE,
+ &ObjectAttributes,
+ &IoStatusBlock,
+ FILE_SHARE_READ,
+ FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE);
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT1("Failed to open file %wZ, Status 0x%08lx\n", &Name,
Status);
+ return Status;
+ }
+
+ /* Map the file in memory */
+
+ /* Create the section */
+ Status = NtCreateSection(SectionHandle,
+ SECTION_MAP_READ,
+ NULL,
+ NULL,
+ PAGE_READONLY,
+ SEC_COMMIT /* | SEC_IMAGE (_NO_EXECUTE) */,
+ *FileHandle);
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT1("Failed to create a memory section for file %wZ, Status
0x%08lx\n", &Name, Status);
+ NtClose(*FileHandle);
+ *FileHandle = NULL;
+ return Status;
+ }
+
+ /* Map the section */
+ ViewSize = 0;
+ ViewBase = NULL;
+ Status = NtMapViewOfSection(*SectionHandle,
+ NtCurrentProcess(),
+ &ViewBase,
+ 0, 0,
+ NULL,
+ &ViewSize,
+ ViewShare,
+ 0,
+ PAGE_READONLY);
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT1("Failed to map a view for file %wZ, Status 0x%08lx\n",
&Name, Status);
+ NtClose(*SectionHandle);
+ *SectionHandle = NULL;
+ NtClose(*FileHandle);
+ *FileHandle = NULL;
+ return Status;
+ }
+
+ *BaseAddress = ViewBase;
+ return STATUS_SUCCESS;
+}
+
+BOOLEAN
+UnMapFile(
+ IN HANDLE SectionHandle,
+ IN PVOID BaseAddress)
+{
+ NTSTATUS Status;
+ BOOLEAN Success = TRUE;
+
+ Status = NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT1("UnMapFile: NtUnmapViewOfSection(0x%p) failed with Status
0x%08lx\n",
+ BaseAddress, Status);
+ Success = FALSE;
+ }
+ Status = NtClose(SectionHandle);
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT1("UnMapFile: NtClose(0x%p) failed with Status 0x%08lx\n",
+ SectionHandle, Status);
+ Success = FALSE;
+ }
+
+ return Success;
+}
+
/* EOF */
Modified: branches/setup_improvements/base/setup/usetup/filesup.h
URL:
http://svn.reactos.org/svn/reactos/branches/setup_improvements/base/setup/u…
==============================================================================
--- branches/setup_improvements/base/setup/usetup/filesup.h [iso-8859-1] (original)
+++ branches/setup_improvements/base/setup/usetup/filesup.h [iso-8859-1] Mon May 15
01:52:34 2017
@@ -41,14 +41,41 @@
PWCHAR SourceFileName,
PWCHAR DestinationFileName);
-BOOLEAN
-DoesFileExist(
- PWSTR PathName,
- PWSTR FileName);
BOOLEAN
IsValidPath(
- PWCHAR InstallDir,
- ULONG Length);
+ IN PWCHAR InstallDir,
+ IN ULONG Length);
+
+HRESULT
+ConcatPaths(
+ IN OUT PWSTR PathElem1,
+ IN SIZE_T cchPathSize,
+ IN PCWSTR PathElem2 OPTIONAL);
+
+BOOLEAN
+DoesPathExist(
+ IN HANDLE RootDirectory OPTIONAL,
+ IN PCWSTR PathName);
+
+BOOLEAN
+DoesFileExist(
+ IN HANDLE RootDirectory OPTIONAL,
+ IN PCWSTR PathName OPTIONAL,
+ IN PCWSTR FileName);
+
+NTSTATUS
+OpenAndMapFile(
+ IN HANDLE RootDirectory OPTIONAL,
+ IN PCWSTR PathName OPTIONAL,
+ IN PCWSTR FileName, // OPTIONAL
+ OUT PHANDLE FileHandle, // IN OUT PHANDLE OPTIONAL
+ OUT PHANDLE SectionHandle,
+ OUT PVOID* BaseAddress);
+
+BOOLEAN
+UnMapFile(
+ IN HANDLE SectionHandle,
+ IN PVOID BaseAddress);
/* EOF */