[SETUPLIB][USETUP] Refactor the DoesFileExist() function so that it now looks closer to DoesPathExist() and use it almost everywhere.

- Adjust also its callers, adjust OpenAndMapFile() parameters.
- Related to that, simplify IsValidNTOSInstallation() parameters & introduce a IsValidNTOSInstallation_UStr()
  that does the same, but takes a UNICODE_STRING instead.
- Simplify CheckForValidPEAndVendor().

Now only exactly 5 calls use the "old" 'DoesFileExist' syntax, using a temporarily auxiliary function "DoesFileExist_2"...

svn path=/branches/setup_improvements/; revision=74641
This commit is contained in:
Hermès Bélusca-Maïto 2017-05-24 16:37:49 +00:00
parent d27ef70aab
commit b53b7b11e3
No known key found for this signature in database
GPG Key ID: 3B2539C65E7B93D0
5 changed files with 110 additions and 119 deletions

View File

@ -154,7 +154,7 @@ DoesPathExist(
if (NT_SUCCESS(Status))
NtClose(FileHandle);
else
DPRINT1("Failed to open directory %wZ, Status 0x%08lx\n", &Name, Status);
DPRINT1("Failed to open directory '%wZ', Status 0x%08lx\n", &Name, Status);
return NT_SUCCESS(Status);
}
@ -162,21 +162,18 @@ DoesPathExist(
BOOLEAN
DoesFileExist(
IN HANDLE RootDirectory OPTIONAL,
IN PCWSTR PathName OPTIONAL,
IN PCWSTR FileName)
IN PCWSTR PathNameToFile)
{
NTSTATUS Status;
UNICODE_STRING FileName;
HANDLE FileHandle;
OBJECT_ATTRIBUTES ObjectAttributes;
IO_STATUS_BLOCK IoStatusBlock;
UNICODE_STRING Name;
WCHAR FullName[MAX_PATH];
CombinePaths(FullName, ARRAYSIZE(FullName), 2, PathName, FileName);
RtlInitUnicodeString(&Name, FullName);
RtlInitUnicodeString(&FileName, PathNameToFile);
InitializeObjectAttributes(&ObjectAttributes,
&Name,
&FileName,
OBJ_CASE_INSENSITIVE,
RootDirectory,
NULL);
@ -190,11 +187,22 @@ DoesFileExist(
if (NT_SUCCESS(Status))
NtClose(FileHandle);
else
DPRINT1("Failed to open file %wZ, Status 0x%08lx\n", &Name, Status);
DPRINT1("Failed to open file '%wZ', Status 0x%08lx\n", &FileName, Status);
return NT_SUCCESS(Status);
}
// FIXME: DEPRECATED! HACKish function that needs to be deprecated!
BOOLEAN
DoesFileExist_2(
IN PCWSTR PathName OPTIONAL,
IN PCWSTR FileName)
{
WCHAR FullName[MAX_PATH];
CombinePaths(FullName, ARRAYSIZE(FullName), 2, PathName, FileName);
return DoesFileExist(NULL, FullName);
}
/*
* The format of NtPath should be:
* \Device\HarddiskXXX\PartitionYYY[\path] ,
@ -298,26 +306,23 @@ Quit:
NTSTATUS
OpenAndMapFile(
IN HANDLE RootDirectory OPTIONAL,
IN PCWSTR PathName OPTIONAL,
IN PCWSTR FileName, // OPTIONAL
IN PCWSTR PathNameToFile,
OUT PHANDLE FileHandle, // IN OUT PHANDLE OPTIONAL
OUT PHANDLE SectionHandle,
OUT PVOID* BaseAddress,
OUT PULONG FileSize OPTIONAL)
{
NTSTATUS Status;
UNICODE_STRING FileName;
OBJECT_ATTRIBUTES ObjectAttributes;
IO_STATUS_BLOCK IoStatusBlock;
SIZE_T ViewSize;
PVOID ViewBase;
UNICODE_STRING Name;
WCHAR FullName[MAX_PATH];
CombinePaths(FullName, ARRAYSIZE(FullName), 2, PathName, FileName);
RtlInitUnicodeString(&Name, FullName);
RtlInitUnicodeString(&FileName, PathNameToFile);
InitializeObjectAttributes(&ObjectAttributes,
&Name,
&FileName,
OBJ_CASE_INSENSITIVE,
RootDirectory,
NULL);
@ -333,7 +338,7 @@ OpenAndMapFile(
FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE);
if (!NT_SUCCESS(Status))
{
DPRINT1("Failed to open file '%wZ', Status 0x%08lx\n", &Name, Status);
DPRINT1("Failed to open file '%wZ', Status 0x%08lx\n", &FileName, Status);
return Status;
}
@ -355,7 +360,7 @@ OpenAndMapFile(
}
if (FileInfo.EndOfFile.HighPart != 0)
DPRINT1("WARNING!! The file '%wZ' is too large!\n", &Name);
DPRINT1("WARNING!! The file '%wZ' is too large!\n", &FileName);
*FileSize = FileInfo.EndOfFile.LowPart;
@ -374,7 +379,7 @@ OpenAndMapFile(
*FileHandle);
if (!NT_SUCCESS(Status))
{
DPRINT1("Failed to create a memory section for file '%wZ', Status 0x%08lx\n", &Name, Status);
DPRINT1("Failed to create a memory section for file '%wZ', Status 0x%08lx\n", &FileName, Status);
NtClose(*FileHandle);
*FileHandle = NULL;
return Status;
@ -394,7 +399,7 @@ OpenAndMapFile(
PAGE_READONLY);
if (!NT_SUCCESS(Status))
{
DPRINT1("Failed to map a view for file %wZ, Status 0x%08lx\n", &Name, Status);
DPRINT1("Failed to map a view for file '%wZ', Status 0x%08lx\n", &FileName, Status);
NtClose(*SectionHandle);
*SectionHandle = NULL;
NtClose(*FileHandle);

View File

@ -51,6 +51,11 @@ DoesPathExist(
BOOLEAN
DoesFileExist(
IN HANDLE RootDirectory OPTIONAL,
IN PCWSTR PathNameToFile);
// FIXME: DEPRECATED! HACKish function that needs to be deprecated!
BOOLEAN
DoesFileExist_2(
IN PCWSTR PathName OPTIONAL,
IN PCWSTR FileName);
@ -64,8 +69,7 @@ NtPathToDiskPartComponents(
NTSTATUS
OpenAndMapFile(
IN HANDLE RootDirectory OPTIONAL,
IN PCWSTR PathName OPTIONAL,
IN PCWSTR FileName, // OPTIONAL
IN PCWSTR PathNameToFile,
OUT PHANDLE FileHandle, // IN OUT PHANDLE OPTIONAL
OUT PHANDLE SectionHandle,
OUT PVOID* BaseAddress,

View File

@ -105,9 +105,12 @@ NTOS_BOOT_LOADER_FILES NtosBootLoaders[] =
static BOOLEAN
IsValidNTOSInstallation_UStr(
IN PUNICODE_STRING SystemRootPath);
/*static*/ BOOLEAN
IsValidNTOSInstallation(
IN HANDLE SystemRootDirectory OPTIONAL,
IN PCWSTR SystemRoot OPTIONAL);
IN PCWSTR SystemRoot);
static PNTOS_INSTALLATION
FindExistingNTOSInstall(
@ -142,9 +145,6 @@ FreeLdrEnumerateInstallations(
PWCHAR SectionName, KeyData;
UNICODE_STRING InstallName;
HANDLE SystemRootDirectory;
OBJECT_ATTRIBUTES ObjectAttributes;
IO_STATUS_BLOCK IoStatusBlock;
PNTOS_INSTALLATION NtOsInstall;
UNICODE_STRING SystemRootPath;
WCHAR SystemRoot[MAX_PATH];
@ -264,25 +264,7 @@ FreeLdrEnumerateInstallations(
/* Set SystemRootPath */
DPRINT1("FreeLdrEnumerateInstallations: SystemRootPath: '%wZ'\n", &SystemRootPath);
/* Open SystemRootPath */
InitializeObjectAttributes(&ObjectAttributes,
&SystemRootPath,
OBJ_CASE_INSENSITIVE,
NULL,
NULL);
Status = NtOpenFile(&SystemRootDirectory,
FILE_LIST_DIRECTORY | SYNCHRONIZE,
&ObjectAttributes,
&IoStatusBlock,
FILE_SHARE_READ | FILE_SHARE_WRITE,
FILE_SYNCHRONOUS_IO_NONALERT | FILE_DIRECTORY_FILE);
if (!NT_SUCCESS(Status))
{
DPRINT1("Failed to open SystemRoot '%wZ', Status 0x%08lx\n", &SystemRootPath, Status);
continue;
}
if (IsValidNTOSInstallation(SystemRootDirectory, NULL))
if (IsValidNTOSInstallation_UStr(&SystemRootPath))
{
ULONG DiskNumber = 0, PartitionNumber = 0;
PCWSTR PathComponent = NULL;
@ -322,8 +304,6 @@ FreeLdrEnumerateInstallations(
DiskNumber, PartitionNumber, PartEntry,
InstallNameW);
}
NtClose(SystemRootDirectory);
}
while (IniCacheFindNextValue(Iterator, &SectionName, &KeyData));
@ -349,9 +329,6 @@ NtLdrEnumerateInstallations(
PWCHAR SectionName, KeyData;
UNICODE_STRING InstallName;
HANDLE SystemRootDirectory;
OBJECT_ATTRIBUTES ObjectAttributes;
IO_STATUS_BLOCK IoStatusBlock;
PNTOS_INSTALLATION NtOsInstall;
UNICODE_STRING SystemRootPath;
WCHAR SystemRoot[MAX_PATH];
@ -439,25 +416,7 @@ NtLdrEnumerateInstallations(
/* Set SystemRootPath */
DPRINT1("NtLdrEnumerateInstallations: SystemRootPath: '%wZ'\n", &SystemRootPath);
/* Open SystemRootPath */
InitializeObjectAttributes(&ObjectAttributes,
&SystemRootPath,
OBJ_CASE_INSENSITIVE,
NULL,
NULL);
Status = NtOpenFile(&SystemRootDirectory,
FILE_LIST_DIRECTORY | SYNCHRONIZE,
&ObjectAttributes,
&IoStatusBlock,
FILE_SHARE_READ | FILE_SHARE_WRITE,
FILE_SYNCHRONOUS_IO_NONALERT | FILE_DIRECTORY_FILE);
if (!NT_SUCCESS(Status))
{
DPRINT1("Failed to open SystemRoot '%wZ', Status 0x%08lx\n", &SystemRootPath, Status);
continue;
}
if (IsValidNTOSInstallation(SystemRootDirectory, NULL))
if (IsValidNTOSInstallation_UStr(&SystemRootPath))
{
ULONG DiskNumber = 0, PartitionNumber = 0;
PCWSTR PathComponent = NULL;
@ -497,8 +456,6 @@ NtLdrEnumerateInstallations(
DiskNumber, PartitionNumber, PartEntry,
InstallNameW);
}
NtClose(SystemRootDirectory);
}
while (IniCacheFindNextValue(Iterator, &SectionName, &KeyData));
@ -542,8 +499,7 @@ PCWSTR FindSubStrI(PCWSTR str, PCWSTR strSearch)
static BOOLEAN
CheckForValidPEAndVendor(
IN HANDLE RootDirectory OPTIONAL,
IN PCWSTR PathName OPTIONAL,
IN PCWSTR FileName, // OPTIONAL
IN PCWSTR PathNameToFile,
OUT PUNICODE_STRING VendorName
)
{
@ -562,18 +518,18 @@ CheckForValidPEAndVendor(
*VendorName->Buffer = UNICODE_NULL;
VendorName->Length = 0;
Status = OpenAndMapFile(RootDirectory, PathName, FileName,
Status = OpenAndMapFile(RootDirectory, PathNameToFile,
&FileHandle, &SectionHandle, &ViewBase, NULL);
if (!NT_SUCCESS(Status))
{
DPRINT1("Failed to open and map file '%S', Status 0x%08lx\n", FileName, Status);
DPRINT1("Failed to open and map file '%S', Status 0x%08lx\n", PathNameToFile, Status);
return FALSE; // Status;
}
/* Make sure it's a valid PE file */
if (!RtlImageNtHeader(ViewBase))
{
DPRINT1("File '%S' does not seem to be a valid PE, bail out\n", FileName);
DPRINT1("File '%S' does not seem to be a valid PE, bail out\n", PathNameToFile);
Status = STATUS_INVALID_IMAGE_FORMAT;
goto UnmapFile;
}
@ -585,7 +541,7 @@ CheckForValidPEAndVendor(
Status = NtGetVersionResource((PVOID)((ULONG_PTR)ViewBase | 1), &VersionBuffer, NULL);
if (!NT_SUCCESS(Status))
{
DPRINT1("Failed to get version resource for file '%S', Status 0x%08lx\n", FileName, Status);
DPRINT1("Failed to get version resource for file '%S', Status 0x%08lx\n", PathNameToFile, Status);
goto UnmapFile;
}
@ -611,7 +567,7 @@ CheckForValidPEAndVendor(
if (NT_SUCCESS(Status) /*&& pvData*/)
{
/* BufLen includes the NULL terminator count */
DPRINT1("Found version vendor: \"%S\" for file '%S'\n", pvData, FileName);
DPRINT1("Found version vendor: \"%S\" for file '%S'\n", pvData, PathNameToFile);
StringCbCopyNW(VendorName->Buffer, VendorName->MaximumLength,
pvData, BufLen * sizeof(WCHAR));
@ -622,7 +578,7 @@ CheckForValidPEAndVendor(
}
if (!NT_SUCCESS(Status))
DPRINT1("No version vendor found for file '%S'\n", FileName);
DPRINT1("No version vendor found for file '%S'\n", PathNameToFile);
UnmapFile:
/* Finally, unmap and close the file */
@ -639,48 +595,30 @@ UnmapFile:
// - if it's broken or not (aka. needs for repair, or just upgrading).
//
static BOOLEAN
IsValidNTOSInstallation(
IN HANDLE SystemRootDirectory OPTIONAL,
IN PCWSTR SystemRoot OPTIONAL)
IsValidNTOSInstallationByHandle(
IN HANDLE SystemRootDirectory)
{
BOOLEAN Success = FALSE;
USHORT i;
UNICODE_STRING VendorName;
WCHAR PathBuffer[MAX_PATH];
/*
* Use either the 'SystemRootDirectory' handle or the 'SystemRoot' string,
* depending on what the user gave to us in entry.
*/
if (SystemRootDirectory)
SystemRoot = NULL;
// else SystemRootDirectory == NULL and SystemRoot is what it is.
/* If both the parameters are NULL we cannot do anything else more */
if (!SystemRootDirectory && !SystemRoot)
return FALSE;
// DoesPathExist(SystemRootDirectory, SystemRoot, L"System32\\"); etc...
WCHAR VendorNameBuffer[MAX_PATH];
/* Check for the existence of \SystemRoot\System32 */
StringCchPrintfW(PathBuffer, ARRAYSIZE(PathBuffer), L"%s%s", SystemRoot ? SystemRoot : L"", L"System32\\");
if (!DoesPathExist(SystemRootDirectory, PathBuffer))
if (!DoesPathExist(SystemRootDirectory, L"System32\\"))
{
// DPRINT1("Failed to open directory '%wZ', Status 0x%08lx\n", &FileName, Status);
return FALSE;
}
/* Check for the existence of \SystemRoot\System32\drivers */
StringCchPrintfW(PathBuffer, ARRAYSIZE(PathBuffer), L"%s%s", SystemRoot ? SystemRoot : L"", L"System32\\drivers\\");
if (!DoesPathExist(SystemRootDirectory, PathBuffer))
if (!DoesPathExist(SystemRootDirectory, L"System32\\drivers\\"))
{
// DPRINT1("Failed to open directory '%wZ', Status 0x%08lx\n", &FileName, Status);
return FALSE;
}
/* Check for the existence of \SystemRoot\System32\config */
StringCchPrintfW(PathBuffer, ARRAYSIZE(PathBuffer), L"%s%s", SystemRoot ? SystemRoot : L"", L"System32\\config\\");
if (!DoesPathExist(SystemRootDirectory, PathBuffer))
if (!DoesPathExist(SystemRootDirectory, L"System32\\config\\"))
{
// DPRINT1("Failed to open directory '%wZ', Status 0x%08lx\n", &FileName, Status);
return FALSE;
@ -691,22 +629,22 @@ IsValidNTOSInstallation(
* Check for the existence of SYSTEM and SOFTWARE hives in \SystemRoot\System32\config
* (but we don't check here whether they are actually valid).
*/
if (!DoesFileExist(SystemRootDirectory, SystemRoot, L"System32\\config\\SYSTEM"))
if (!DoesFileExist(SystemRootDirectory, L"System32\\config\\SYSTEM"))
{
// DPRINT1("Failed to open file '%wZ', Status 0x%08lx\n", &FileName, Status);
return FALSE;
}
if (!DoesFileExist(SystemRootDirectory, SystemRoot, L"System32\\config\\SOFTWARE"))
if (!DoesFileExist(SystemRootDirectory, L"System32\\config\\SOFTWARE"))
{
// DPRINT1("Failed to open file '%wZ', Status 0x%08lx\n", &FileName, Status);
return FALSE;
}
#endif
RtlInitEmptyUnicodeString(&VendorName, PathBuffer, sizeof(PathBuffer));
RtlInitEmptyUnicodeString(&VendorName, VendorNameBuffer, sizeof(VendorNameBuffer));
/* Check for the existence of \SystemRoot\System32\ntoskrnl.exe and retrieves its vendor name */
Success = CheckForValidPEAndVendor(SystemRootDirectory, SystemRoot, L"System32\\ntoskrnl.exe", &VendorName);
Success = CheckForValidPEAndVendor(SystemRootDirectory, L"System32\\ntoskrnl.exe", &VendorName);
if (!Success)
DPRINT1("Kernel file ntoskrnl.exe is either not a PE file, or does not have any vendor?\n");
@ -728,7 +666,7 @@ IsValidNTOSInstallation(
/* OPTIONAL: Check for the existence of \SystemRoot\System32\ntkrnlpa.exe */
/* Check for the existence of \SystemRoot\System32\ntdll.dll and retrieves its vendor name */
Success = CheckForValidPEAndVendor(SystemRootDirectory, SystemRoot, L"System32\\ntdll.dll", &VendorName);
Success = CheckForValidPEAndVendor(SystemRootDirectory, L"System32\\ntdll.dll", &VendorName);
if (!Success)
DPRINT1("User-mode file ntdll.dll is either not a PE file, or does not have any vendor?\n");
if (Success)
@ -747,6 +685,50 @@ IsValidNTOSInstallation(
return Success;
}
static BOOLEAN
IsValidNTOSInstallation_UStr(
IN PUNICODE_STRING SystemRootPath)
{
NTSTATUS Status;
OBJECT_ATTRIBUTES ObjectAttributes;
IO_STATUS_BLOCK IoStatusBlock;
HANDLE SystemRootDirectory;
BOOLEAN Success;
/* Open SystemRootPath */
InitializeObjectAttributes(&ObjectAttributes,
SystemRootPath,
OBJ_CASE_INSENSITIVE,
NULL,
NULL);
Status = NtOpenFile(&SystemRootDirectory,
FILE_LIST_DIRECTORY | SYNCHRONIZE,
&ObjectAttributes,
&IoStatusBlock,
FILE_SHARE_READ | FILE_SHARE_WRITE,
FILE_SYNCHRONOUS_IO_NONALERT | FILE_DIRECTORY_FILE);
if (!NT_SUCCESS(Status))
{
DPRINT1("Failed to open SystemRoot '%wZ', Status 0x%08lx\n", SystemRootPath, Status);
return FALSE;
}
Success = IsValidNTOSInstallationByHandle(SystemRootDirectory);
/* Done! */
NtClose(SystemRootDirectory);
return Success;
}
/*static*/ BOOLEAN
IsValidNTOSInstallation(
IN PCWSTR SystemRoot)
{
UNICODE_STRING SystemRootPath;
RtlInitUnicodeString(&SystemRootPath, SystemRoot);
return IsValidNTOSInstallationByHandle(&SystemRootPath);
}
static VOID
DumpNTOSInstalls(
IN PGENERIC_LIST List)
@ -934,7 +916,7 @@ FindNTOSInstallations(
for (i = 0; i < ARRAYSIZE(NtosBootLoaders); ++i)
{
/* Check whether the loader executable exists */
if (!DoesFileExist(PartitionHandle, NULL, NtosBootLoaders[i].LoaderExecutable))
if (!DoesFileExist(PartitionHandle, NtosBootLoaders[i].LoaderExecutable))
{
/* The loader does not exist, continue with another one */
DPRINT1("Loader executable '%S' does not exist, continue with another one...\n", NtosBootLoaders[i].LoaderExecutable);
@ -942,7 +924,7 @@ FindNTOSInstallations(
}
/* Check whether the loader configuration file exists */
Status = OpenAndMapFile(PartitionHandle, NULL, NtosBootLoaders[i].LoaderConfigurationFile,
Status = OpenAndMapFile(PartitionHandle, NtosBootLoaders[i].LoaderConfigurationFile,
&FileHandle, &SectionHandle, &ViewBase, &FileSize);
if (!NT_SUCCESS(Status))
{

View File

@ -2275,7 +2275,7 @@ InstallFatBootcodeToPartition(
/* Prepare for possibly copying 'freeldr.ini' */
CombinePaths(DstPath, ARRAYSIZE(DstPath), 2, SystemRootPath->Buffer, L"\\freeldr.ini");
DoesFreeLdrExist = DoesFileExist(NULL, NULL, DstPath);
DoesFreeLdrExist = DoesFileExist(NULL, DstPath);
if (DoesFreeLdrExist)
{
/* Update existing 'freeldr.ini' */
@ -2292,8 +2292,8 @@ InstallFatBootcodeToPartition(
/* Check for NT and other bootloaders */
// FIXME: Check for Vista+ bootloader!
if (DoesFileExist(NULL, SystemRootPath->Buffer, L"ntldr") == TRUE ||
DoesFileExist(NULL, SystemRootPath->Buffer, L"boot.ini") == TRUE)
if (DoesFileExist_2(SystemRootPath->Buffer, L"ntldr") == TRUE ||
DoesFileExist_2(SystemRootPath->Buffer, L"boot.ini") == TRUE)
{
/* Search root directory for 'ntldr' and 'boot.ini' */
DPRINT1("Found Microsoft Windows NT/2000/XP boot loader\n");
@ -2370,8 +2370,8 @@ InstallFatBootcodeToPartition(
PWCHAR BootSector;
PWCHAR BootSectorFileName;
if (DoesFileExist(NULL, SystemRootPath->Buffer, L"io.sys") == TRUE ||
DoesFileExist(NULL, SystemRootPath->Buffer, L"msdos.sys") == TRUE)
if (DoesFileExist_2(SystemRootPath->Buffer, L"io.sys") == TRUE ||
DoesFileExist_2(SystemRootPath->Buffer, L"msdos.sys") == TRUE)
{
/* Search for root directory for 'io.sys' and 'msdos.sys' */
DPRINT1("Found Microsoft DOS or Windows 9x boot loader\n");
@ -2385,7 +2385,7 @@ InstallFatBootcodeToPartition(
BootSectorFileName = L"\\bootsect.dos";
}
else
if (DoesFileExist(NULL, SystemRootPath->Buffer, L"kernel.sys") == TRUE)
if (DoesFileExist_2(SystemRootPath->Buffer, L"kernel.sys") == TRUE)
{
/* Search for root directory for 'kernel.sys' */
DPRINT1("Found FreeDOS boot loader\n");
@ -2517,7 +2517,7 @@ InstallExt2BootcodeToPartition(
/* Prepare for possibly copying 'freeldr.ini' */
CombinePaths(DstPath, ARRAYSIZE(DstPath), 2, SystemRootPath->Buffer, L"\\freeldr.ini");
DoesFreeLdrExist = DoesFileExist(NULL, NULL, DstPath);
DoesFreeLdrExist = DoesFileExist(NULL, DstPath);
if (DoesFreeLdrExist)
{
/* Update existing 'freeldr.ini' */

View File

@ -429,7 +429,7 @@ CheckUnattendedSetup(VOID)
CombinePaths(UnattendInfPath, ARRAYSIZE(UnattendInfPath), 2, SourcePath.Buffer, L"\\unattend.inf");
if (DoesFileExist(NULL, NULL, UnattendInfPath) == FALSE)
if (DoesFileExist(NULL, UnattendInfPath) == FALSE)
{
DPRINT("Does not exist: %S\n", UnattendInfPath);
return;