[AUTOCHK] Minor code formatting; reduce indentation level of some blocks; use RTL_NUMBER_OF() macro; normalize file header.

This commit is contained in:
Hermès Bélusca-Maïto 2018-07-29 15:05:22 +02:00
parent 8dbe62b294
commit 4225172506
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0

View file

@ -1,45 +1,43 @@
/* /*
* PROJECT: ReactOS Kernel * PROJECT: ReactOS AutoChk
* LICENSE: GPL - See COPYING in the top level directory * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
* FILE: base/system/autochk/autochk.c * PURPOSE: FileSystem checker in Native mode.
* PURPOSE: Filesystem checker * COPYRIGHT: Copyright 2002-2018 Eric Kohl
* PROGRAMMERS: Aleksey Bragin * Copyright 2006-2018 Aleksey Bragin
* Eric Kohl * Copyright 2006-2018 Hervé Poussineau
* Hervé Poussineau * Copyright 2008-2018 Pierre Schweitzer
* Pierre Schweitzer
*/ */
/* INCLUDES *****************************************************************/ /* INCLUDES *****************************************************************/
#include <stdio.h> #include <stdio.h>
#define WIN32_NO_STATUS #define WIN32_NO_STATUS
#include <windef.h> #include <windef.h>
#include <winbase.h> #include <winbase.h>
#include <ntddkbd.h> #include <ntddkbd.h>
#define NTOS_MODE_USER #define NTOS_MODE_USER
#include <ndk/exfuncs.h> #include <ndk/exfuncs.h>
#include <ndk/iofuncs.h> #include <ndk/iofuncs.h>
#include <ndk/obfuncs.h> #include <ndk/obfuncs.h>
#include <ndk/psfuncs.h> #include <ndk/psfuncs.h>
#include <ndk/rtlfuncs.h> #include <ndk/rtlfuncs.h>
#include <ndk/umfuncs.h>
#include <fmifs/fmifs.h> #include <fmifs/fmifs.h>
#include <fslib/vfatlib.h> #include <fslib/vfatlib.h>
#include <fslib/ext2lib.h>
#include <fslib/ntfslib.h> #include <fslib/ntfslib.h>
#include <fslib/cdfslib.h> #include <fslib/ext2lib.h>
#include <fslib/btrfslib.h> #include <fslib/btrfslib.h>
#include <fslib/ffslib.h>
#include <fslib/reiserfslib.h> #include <fslib/reiserfslib.h>
#include <fslib/ffslib.h>
#include <fslib/cdfslib.h>
#define NDEBUG #define NDEBUG
#include <debug.h> #include <debug.h>
/* DEFINES ******************************************************************/ /* DEFINES ******************************************************************/
#define FS_ATTRIBUTE_BUFFER_SIZE (MAX_PATH * sizeof(WCHAR) + sizeof(FILE_FS_ATTRIBUTE_INFORMATION))
typedef struct _FILESYSTEM_CHKDSK typedef struct _FILESYSTEM_CHKDSK
{ {
WCHAR Name[10]; WCHAR Name[10];
@ -63,6 +61,7 @@ FILESYSTEM_CHKDSK FileSystems[10] =
HANDLE KeyboardHandle; HANDLE KeyboardHandle;
/* FUNCTIONS ****************************************************************/ /* FUNCTIONS ****************************************************************/
// //
// FMIFS function // FMIFS function
// //
@ -209,7 +208,7 @@ GetFileSystem(
NTSTATUS Status; NTSTATUS Status;
IO_STATUS_BLOCK IoStatusBlock; IO_STATUS_BLOCK IoStatusBlock;
PFILE_FS_ATTRIBUTE_INFORMATION FileFsAttribute; PFILE_FS_ATTRIBUTE_INFORMATION FileFsAttribute;
UCHAR Buffer[FS_ATTRIBUTE_BUFFER_SIZE]; UCHAR Buffer[sizeof(FILE_FS_ATTRIBUTE_INFORMATION) + MAX_PATH * sizeof(WCHAR)];
FileFsAttribute = (PFILE_FS_ATTRIBUTE_INFORMATION)Buffer; FileFsAttribute = (PFILE_FS_ATTRIBUTE_INFORMATION)Buffer;
@ -220,30 +219,34 @@ GetFileSystem(
Status = NtQueryVolumeInformationFile(FileHandle, Status = NtQueryVolumeInformationFile(FileHandle,
&IoStatusBlock, &IoStatusBlock,
FileFsAttribute, FileFsAttribute,
FS_ATTRIBUTE_BUFFER_SIZE, sizeof(Buffer),
FileFsAttributeInformation); FileFsAttributeInformation);
NtClose(FileHandle); NtClose(FileHandle);
if (NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
DPRINT1("NtQueryVolumeInformationFile() failed, Status 0x%08lx\n", Status);
return Status;
}
if (FileSystemNameSize * sizeof(WCHAR) >= FileFsAttribute->FileSystemNameLength + sizeof(WCHAR)) if (FileSystemNameSize * sizeof(WCHAR) >= FileFsAttribute->FileSystemNameLength + sizeof(WCHAR))
{ {
CopyMemory(FileSystemName, RtlCopyMemory(FileSystemName,
FileFsAttribute->FileSystemName, FileFsAttribute->FileSystemName,
FileFsAttribute->FileSystemNameLength); FileFsAttribute->FileSystemNameLength);
FileSystemName[FileFsAttribute->FileSystemNameLength / sizeof(WCHAR)] = 0; FileSystemName[FileFsAttribute->FileSystemNameLength / sizeof(WCHAR)] = UNICODE_NULL;
} }
else else
{
return STATUS_BUFFER_TOO_SMALL; return STATUS_BUFFER_TOO_SMALL;
} }
else
return Status;
return STATUS_SUCCESS; return STATUS_SUCCESS;
} }
// This is based on SysInternal's ChkDsk app /* This is based on SysInternals' ChkDsk application */
static BOOLEAN NTAPI static BOOLEAN
NTAPI
ChkdskCallback( ChkdskCallback(
IN CALLBACKCOMMAND Command, IN CALLBACKCOMMAND Command,
IN ULONG Modifier, IN ULONG Modifier,
@ -348,6 +351,12 @@ CheckVolume(
NTSTATUS Status; NTSTATUS Status;
DWORD Count; DWORD Count;
swprintf(NtDrivePath, L"\\??\\");
wcscat(NtDrivePath, DrivePath);
NtDrivePath[wcslen(NtDrivePath)-1] = 0;
RtlInitUnicodeString(&DrivePathU, NtDrivePath);
DPRINT1("AUTOCHK: Checking %wZ\n", &DrivePathU);
PrintString(" Checking file system on %S\r\n", DrivePath); PrintString(" Checking file system on %S\r\n", DrivePath);
/* Get the file system */ /* Get the file system */
@ -356,27 +365,26 @@ CheckVolume(
ARRAYSIZE(FileSystem)); ARRAYSIZE(FileSystem));
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
DPRINT1("GetFileSystem() failed with status 0x%08lx\n", Status); DPRINT1("GetFileSystem() failed, Status 0x%08lx\n", Status);
PrintString(" Unable to detect file system of %S\r\n", DrivePath); PrintString(" Unable to detect file system of %S\r\n", DrivePath);
return Status; return Status;
} }
PrintString(" The file system type is %S.\r\n\r\n", FileSystem); PrintString(" The file system type is %S.\r\n\r\n", FileSystem);
/* Call provider */ /* Find a suitable file system provider */
for (Count = 0; Count < sizeof(FileSystems) / sizeof(FileSystems[0]); ++Count) for (Count = 0; Count < RTL_NUMBER_OF(FileSystems); ++Count)
{ {
if (wcscmp(FileSystem, FileSystems[Count].Name) != 0) if (wcscmp(FileSystem, FileSystems[Count].Name) == 0)
break;
}
if (Count >= RTL_NUMBER_OF(FileSystems))
{ {
continue; DPRINT1("File system not supported\n");
PrintString(" Unable to check the file system. %S is not supported.\r\n", FileSystem);
return STATUS_DLL_NOT_FOUND;
} }
swprintf(NtDrivePath, L"\\??\\");
wcscat(NtDrivePath, DrivePath);
NtDrivePath[wcslen(NtDrivePath)-1] = 0;
RtlInitUnicodeString(&DrivePathU, NtDrivePath);
DPRINT1("AUTOCHK: Checking %wZ\n", &DrivePathU);
/* First, check whether the volume is dirty */ /* First, check whether the volume is dirty */
Status = FileSystems[Count].ChkdskFunc(&DrivePathU, Status = FileSystems[Count].ChkdskFunc(&DrivePathU,
FALSE, // FixErrors FALSE, // FixErrors
@ -398,28 +406,19 @@ CheckVolume(
WaitStatus = WaitForKeyboard(TimeOut); WaitStatus = WaitForKeyboard(TimeOut);
if (WaitStatus == STATUS_TIMEOUT) if (WaitStatus == STATUS_TIMEOUT)
{ {
PrintString(" The system will now check the file system.\r\n\r\n");
Status = FileSystems[Count].ChkdskFunc(&DrivePathU, Status = FileSystems[Count].ChkdskFunc(&DrivePathU,
TRUE, // FixErrors TRUE, // FixErrors
TRUE, // Verbose TRUE, // Verbose
TRUE, // CheckOnlyIfDirty TRUE, // CheckOnlyIfDirty
FALSE, // ScanDrive FALSE, // ScanDrive
ChkdskCallback); ChkdskCallback);
PrintString(" The system will now check the file system.\r\n\r\n");
} }
else else
{ {
PrintString(" File system check has been skipped.\r\n"); PrintString(" File system check has been skipped.\r\n");
} }
} }
break;
}
if (Count == sizeof(FileSystems) / sizeof(FileSystems[0]))
{
DPRINT1("File system not supported\n");
PrintString(" Unable to check the file system. %S is not supported.\r\n", FileSystem);
return STATUS_DLL_NOT_FOUND;
}
return Status; return Status;
} }
@ -437,23 +436,16 @@ QueryTimeout(
RtlQueryRegistryValues(RTL_REGISTRY_CONTROL, L"Session Manager", QueryTable, NULL, NULL); RtlQueryRegistryValues(RTL_REGISTRY_CONTROL, L"Session Manager", QueryTable, NULL, NULL);
/* See: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/autochk */ /* See: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/autochk */
if (*TimeOut > 259200) *TimeOut = min(max(*TimeOut, 0), 259200);
{
*TimeOut = 259200;
}
else if (*TimeOut < 0)
{
*TimeOut = 0;
}
} }
/* Native image's entry point */ INT
int __cdecl
_cdecl _main(
_main(int argc, IN INT argc,
char *argv[], IN PCHAR argv[],
char *envp[], IN PCHAR envp[],
int DebugFlag) IN ULONG DebugFlag)
{ {
PROCESS_DEVICEMAP_INFORMATION DeviceMap; PROCESS_DEVICEMAP_INFORMATION DeviceMap;
ULONG i; ULONG i;
@ -475,7 +467,6 @@ _main(int argc,
/* FIXME: We should probably use here the mount manager to be /* FIXME: We should probably use here the mount manager to be
* able to check volumes which don't have a drive letter. * able to check volumes which don't have a drive letter.
*/ */
Status = NtQueryInformationProcess(NtCurrentProcess(), Status = NtQueryInformationProcess(NtCurrentProcess(),
ProcessDeviceMap, ProcessDeviceMap,
&DeviceMap.Query, &DeviceMap.Query,
@ -483,8 +474,7 @@ _main(int argc,
NULL); NULL);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
DPRINT1("NtQueryInformationProcess() failed with status 0x%08lx\n", DPRINT1("NtQueryInformationProcess() failed, Status 0x%08lx\n", Status);
Status);
return 1; return 1;
} }
@ -492,7 +482,7 @@ _main(int argc,
Status = OpenKeyboard(); Status = OpenKeyboard();
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
DPRINT1("OpenKeyboard() failed with status 0x%08lx\n", Status); DPRINT1("OpenKeyboard() failed, Status 0x%08lx\n", Status);
return 1; return 1;
} }