[FMIFS] Improve FormatEx and Chkdsk

- Remove the hack for missing IOCTL_MOUNTDEV_QUERY_DEVICE_NAME
- Handle DriveRoot without a trailing backslash
This commit is contained in:
Adam Słaboń 2024-03-03 00:27:49 +01:00 committed by Hermès BÉLUSCA - MAÏTO
parent 1d18b12ff7
commit 5c5cd20b9d
2 changed files with 54 additions and 36 deletions

View file

@ -8,6 +8,7 @@
*/ */
#include "precomp.h" #include "precomp.h"
#include <ntstrsafe.h>
#define NDEBUG #define NDEBUG
#include <debug.h> #include <debug.h>
@ -30,31 +31,38 @@ Chkdsk(
UNICODE_STRING usDriveRoot; UNICODE_STRING usDriveRoot;
NTSTATUS Status; NTSTATUS Status;
BOOLEAN Success = FALSE; BOOLEAN Success = FALSE;
WCHAR DriveName[MAX_PATH];
WCHAR VolumeName[MAX_PATH]; WCHAR VolumeName[MAX_PATH];
//CURDIR CurDir;
Provider = GetProvider(Format); Provider = GetProvider(Format);
if (!Provider) if (!Provider)
{ {
/* Unknown file system */ /* Unknown file system */
Callback(DONE, 0, &Success); goto Quit;
return;
} }
#if 1 if (!NT_SUCCESS(RtlStringCchCopyW(DriveName, ARRAYSIZE(DriveName), DriveRoot)))
DPRINT1("Warning: use GetVolumeNameForVolumeMountPointW() instead!\n"); goto Quit;
swprintf(VolumeName, L"\\??\\%c:", towupper(DriveRoot[0]));
RtlCreateUnicodeString(&usDriveRoot, VolumeName); if (DriveName[wcslen(DriveName) - 1] != L'\\')
/* Code disabled as long as our storage stack doesn't understand IOCTL_MOUNTDEV_QUERY_DEVICE_NAME */
#else
if (!GetVolumeNameForVolumeMountPointW(DriveRoot, VolumeName, RTL_NUMBER_OF(VolumeName)) ||
!RtlDosPathNameToNtPathName_U(VolumeName, &usDriveRoot, NULL, &CurDir))
{ {
/* Report an error */ /* Append the trailing backslash for GetVolumeNameForVolumeMountPointW */
Callback(DONE, 0, &Success); if (!NT_SUCCESS(RtlStringCchCatW(DriveName, ARRAYSIZE(DriveName), L"\\")))
return; goto Quit;
} }
#endif
if (!GetVolumeNameForVolumeMountPointW(DriveName, VolumeName, ARRAYSIZE(VolumeName)))
{
/* Couldn't get a volume GUID path, try checking using a parameter provided path */
DPRINT1("Couldn't get a volume GUID path for drive %S\n", DriveName);
wcscpy(VolumeName, DriveName);
}
if (!RtlDosPathNameToNtPathName_U(VolumeName, &usDriveRoot, NULL, NULL))
goto Quit;
/* Trim the trailing backslash since we will work with a device object */
usDriveRoot.Length -= sizeof(WCHAR);
DPRINT("Chkdsk() - %S\n", Format); DPRINT("Chkdsk() - %S\n", Format);
Status = STATUS_SUCCESS; Status = STATUS_SUCCESS;
@ -72,10 +80,11 @@ Chkdsk(
if (!Success) if (!Success)
DPRINT1("Chkdsk() failed with Status 0x%lx\n", Status); DPRINT1("Chkdsk() failed with Status 0x%lx\n", Status);
/* Report success */
Callback(DONE, 0, &Success);
RtlFreeUnicodeString(&usDriveRoot); RtlFreeUnicodeString(&usDriveRoot);
Quit:
/* Report result */
Callback(DONE, 0, &Success);
} }
/* EOF */ /* EOF */

View file

@ -9,6 +9,7 @@
*/ */
#include "precomp.h" #include "precomp.h"
#include <ntstrsafe.h>
#define NDEBUG #define NDEBUG
#include <debug.h> #include <debug.h>
@ -50,8 +51,8 @@ FormatEx(
BOOLEAN Success = FALSE; BOOLEAN Success = FALSE;
BOOLEAN BackwardCompatible = FALSE; // Default to latest FS versions. BOOLEAN BackwardCompatible = FALSE; // Default to latest FS versions.
MEDIA_TYPE MediaType; MEDIA_TYPE MediaType;
WCHAR DriveName[MAX_PATH];
WCHAR VolumeName[MAX_PATH]; WCHAR VolumeName[MAX_PATH];
//CURDIR CurDir;
// //
// TODO: Convert filesystem Format into ULIB format string. // TODO: Convert filesystem Format into ULIB format string.
@ -61,24 +62,31 @@ FormatEx(
if (!Provider) if (!Provider)
{ {
/* Unknown file system */ /* Unknown file system */
Callback(DONE, 0, &Success); goto Quit;
return;
} }
#if 1 if (!NT_SUCCESS(RtlStringCchCopyW(DriveName, ARRAYSIZE(DriveName), DriveRoot)))
DPRINT1("Warning: use GetVolumeNameForVolumeMountPointW() instead!\n"); goto Quit;
swprintf(VolumeName, L"\\??\\%c:", towupper(DriveRoot[0]));
RtlCreateUnicodeString(&usDriveRoot, VolumeName); if (DriveName[wcslen(DriveName) - 1] != L'\\')
/* Code disabled as long as our storage stack doesn't understand IOCTL_MOUNTDEV_QUERY_DEVICE_NAME */
#else
if (!GetVolumeNameForVolumeMountPointW(DriveRoot, VolumeName, RTL_NUMBER_OF(VolumeName)) ||
!RtlDosPathNameToNtPathName_U(VolumeName, &usDriveRoot, NULL, &CurDir))
{ {
/* Report an error */ /* Append the trailing backslash for GetVolumeNameForVolumeMountPointW */
Callback(DONE, 0, &Success); if (!NT_SUCCESS(RtlStringCchCatW(DriveName, ARRAYSIZE(DriveName), L"\\")))
return; goto Quit;
} }
#endif
if (!GetVolumeNameForVolumeMountPointW(DriveName, VolumeName, ARRAYSIZE(VolumeName)))
{
/* Couldn't get a volume GUID path, try formatting using a parameter provided path */
DPRINT1("Couldn't get a volume GUID path for drive %S\n", DriveName);
wcscpy(VolumeName, DriveName);
}
if (!RtlDosPathNameToNtPathName_U(VolumeName, &usDriveRoot, NULL, NULL))
goto Quit;
/* Trim the trailing backslash since we will work with a device object */
usDriveRoot.Length -= sizeof(WCHAR);
RtlInitUnicodeString(&usLabel, Label); RtlInitUnicodeString(&usLabel, Label);
@ -119,10 +127,11 @@ FormatEx(
if (!Success) if (!Success)
DPRINT1("Format() failed\n"); DPRINT1("Format() failed\n");
/* Report success */
Callback(DONE, 0, &Success);
RtlFreeUnicodeString(&usDriveRoot); RtlFreeUnicodeString(&usDriveRoot);
Quit:
/* Report result */
Callback(DONE, 0, &Success);
} }
/* EOF */ /* EOF */