From 9723b5e974964b57544b18a87c6dda1739d1f934 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Herm=C3=A8s=20B=C3=A9lusca-Ma=C3=AFto?= Date: Wed, 2 Mar 2016 22:05:19 +0000 Subject: [PATCH] [CHKDSK] - Integrate chkdsk into our build system. - Whitespace fixes. svn path=/trunk/; revision=70868 --- reactos/base/system/chkdsk/CMakeLists.txt | 4 +- reactos/base/system/chkdsk/chkdsk.c | 680 +++++++++++----------- reactos/base/system/chkdsk/chkdsk.rc | 8 +- 3 files changed, 344 insertions(+), 348 deletions(-) diff --git a/reactos/base/system/chkdsk/CMakeLists.txt b/reactos/base/system/chkdsk/CMakeLists.txt index 4e7221b1a6c..b57516f8643 100644 --- a/reactos/base/system/chkdsk/CMakeLists.txt +++ b/reactos/base/system/chkdsk/CMakeLists.txt @@ -1,7 +1,7 @@ -add_executable(chkdsk chkdsk.c chkdsk.rc) include_directories(${REACTOS_SOURCE_DIR}/include/reactos/libs/fmifs) + +add_executable(chkdsk chkdsk.c chkdsk.rc) set_module_type(chkdsk win32cui UNICODE) -target_link_libraries(chkdsk win32err) add_importlibs(chkdsk fmifs msvcrt kernel32 ntdll) add_cd_file(TARGET chkdsk DESTINATION reactos/system32 FOR all) diff --git a/reactos/base/system/chkdsk/chkdsk.c b/reactos/base/system/chkdsk/chkdsk.c index d037761a0de..2c6909fba29 100644 --- a/reactos/base/system/chkdsk/chkdsk.c +++ b/reactos/base/system/chkdsk/chkdsk.c @@ -3,8 +3,11 @@ // Chkdskx // // Copyright (c) 1998 Mark Russinovich -// Systems Internals -// http://www.sysinternals.com/ +// Systems Internals +// http://www.sysinternals.com/ +// +// Chkdsk clone that demonstrates the use of the FMIFS file system +// utility library. // // -------------------------------------------------------------------- // @@ -25,51 +28,71 @@ // // -------------------------------------------------------------------- // -// Chkdsk clone that demonstrates the use of the FMIFS file system -// utility library. -// // 1999 February (Emanuele Aliberti) -// Adapted for ReactOS and lcc-win32. +// Adapted for ReactOS and lcc-win32. // // 1999 April (Emanuele Aliberti) -// Adapted for ReactOS and egcs. +// Adapted for ReactOS and egcs. // // 2008 July (Aleksey Bragin) -// Cleanup, use ReactOS's fmifs.h +// Cleanup, use ReactOS's fmifs.h // //====================================================================== + +#include + +/* PSDK/NDK Headers */ #define WIN32_NO_STATUS -#define NTOS_MODE_USER #include -#include + +#define NTOS_MODE_USER #include #include -#define _UNICODE 1 -#include -#include "../config.h" -#include "../win32err.h" + +#define FMIFS_IMPORT_DLL // // Globals // -BOOL Error = FALSE; +BOOL Error = FALSE; // switches -BOOL FixErrors = FALSE; -BOOL SkipClean = FALSE; -BOOL ScanSectors = FALSE; -BOOL Verbose = FALSE; +BOOL FixErrors = FALSE; +BOOL SkipClean = FALSE; +BOOL ScanSectors = FALSE; +BOOL Verbose = FALSE; PWCHAR Drive = NULL; -WCHAR CurrentDirectory[1024]; +WCHAR CurrentDirectory[1024]; #ifndef FMIFS_IMPORT_DLL // // FMIFS function // -//PCHKDSK Chkdsk; +// PCHKDSK Chkdsk; #endif /* ndef FMIFS_IMPORT_DLL */ +//---------------------------------------------------------------------- +// +// PrintWin32Error +// +// Takes the win32 error code and prints the text version. +// +//---------------------------------------------------------------------- +static VOID PrintWin32Error(LPWSTR Message, DWORD ErrorCode) +{ + LPWSTR lpMsgBuf; + + FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, + NULL, ErrorCode, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPWSTR)&lpMsgBuf, 0, NULL); + + wprintf(L"%s: %s\n", Message, lpMsgBuf); + LocalFree(lpMsgBuf); +} + + //-------------------------------------------------------------------- // // CtrlCIntercept @@ -80,12 +103,12 @@ WCHAR CurrentDirectory[1024]; //-------------------------------------------------------------------- BOOL WINAPI -CtrlCIntercept( DWORD dwCtrlType ) +CtrlCIntercept(DWORD dwCtrlType) { - // - // Handle the event so that the default handler doesn't - // - return TRUE; + // + // Handle the event so that the default handler doesn't + // + return TRUE; } @@ -98,18 +121,15 @@ CtrlCIntercept( DWORD dwCtrlType ) // 19990216 EA Missing printf %s argument //---------------------------------------------------------------------- VOID -Usage( PWCHAR ProgramName ) +Usage(PWCHAR ProgramName) { - _tprintf( - L"\ -Usage: %s [drive:] [-F] [-V] [-R] [-C]\n\n\ - [drive:] Specifies the drive to check.\n\ - -F Fixes errors on the disk.\n\ - -V Displays the full path of every file on the disk.\n\ - -R Locates bad sectors and recovers readable information.\n\ - -C Checks the drive only if it is dirty.\n\n", - ProgramName - ); + wprintf(L"Usage: %s [drive:] [-F] [-V] [-R] [-C]\n\n" + L"[drive:] Specifies the drive to check.\n" + L"-F Fixes errors on the disk.\n" + L"-V Displays the full path of every file on the disk.\n" + L"-R Locates bad sectors and recovers readable information.\n" + L"-C Checks the drive only if it is dirty.\n\n", + ProgramName); } @@ -122,75 +142,76 @@ Usage: %s [drive:] [-F] [-V] [-R] [-C]\n\n\ //---------------------------------------------------------------------- int ParseCommandLine( - int argc, - WCHAR *argv [] - ) + int argc, + WCHAR *argv[] + ) { - int i; - BOOLEAN gotFix = FALSE; - BOOLEAN gotVerbose = FALSE; - BOOLEAN gotClean = FALSE; - /*BOOLEAN gotScan = FALSE;*/ - - - for ( i = 1; - (i < argc); - i++ - ) { - switch( argv[i][0] ) - { - case L'-': - case L'/': - - switch( argv[i][1] ) - { - case L'F': - case L'f': - - if( gotFix ) return i; - FixErrors = TRUE; - gotFix = TRUE; - break; - - case L'V': - case L'v': - - if( gotVerbose) return i; - Verbose = TRUE; - gotVerbose = TRUE; - break; - - case L'R': - case L'r': - - if( gotFix ) return i; - ScanSectors = TRUE; - gotFix = TRUE; - break; - - case L'C': - case L'c': - - if( gotClean ) return i; - SkipClean = TRUE; - gotClean = TRUE; - break; - - default: - return i; - } - break; - - default: - - if( Drive ) return i; - if( argv[i][1] != L':' ) return i; - - Drive = argv[i]; - break; - } - } - return 0; + int i; + BOOLEAN gotFix = FALSE; + BOOLEAN gotVerbose = FALSE; + BOOLEAN gotClean = FALSE; + // BOOLEAN gotScan = FALSE; + + for (i = 1; i < argc; i++) + { + switch (argv[i][0]) + { + case L'-': case L'/': + + switch (argv[i][1]) + { + case L'?': + Usage(argv[0]); + break; + + case L'F': case L'f': + { + if (gotFix) return i; + FixErrors = TRUE; + gotFix = TRUE; + break; + } + + case L'V': case L'v': + { + if (gotVerbose) return i; + Verbose = TRUE; + gotVerbose = TRUE; + break; + } + + case L'R': case L'r': + { + if (gotFix) return i; + ScanSectors = TRUE; + gotFix = TRUE; + break; + } + + case L'C': case L'c': + { + if (gotClean) return i; + SkipClean = TRUE; + gotClean = TRUE; + break; + } + + default: + return i; + } + break; + + default: + { + if (Drive) return i; + if (argv[i][1] != L':') return i; + + Drive = argv[i]; + break; + } + } + } + return 0; } @@ -205,97 +226,97 @@ ParseCommandLine( BOOLEAN WINAPI ChkdskCallback( - CALLBACKCOMMAND Command, - DWORD Modifier, - PVOID Argument - ) + CALLBACKCOMMAND Command, + DWORD Modifier, + PVOID Argument + ) { - PDWORD percent; - PBOOLEAN status; - PTEXTOUTPUT output; - - // - // We get other types of commands, - // but we don't have to pay attention to them - // - switch( Command ) - { - case UNKNOWN2: - wprintf(L"UNKNOWN2\r"); - break; - - case UNKNOWN3: - wprintf(L"UNKNOWN3\r"); - break; - - case UNKNOWN4: - wprintf(L"UNKNOWN4\r"); - break; - - case UNKNOWN5: - wprintf(L"UNKNOWN5\r"); - break; - - case FSNOTSUPPORTED: - wprintf(L"FSNOTSUPPORTED\r"); - break; - - case VOLUMEINUSE: - wprintf(L"VOLUMEINUSE\r"); - break; - - case UNKNOWN9: - wprintf(L"UNKNOWN9\r"); - break; - - case UNKNOWNA: - wprintf(L"UNKNOWNA\r"); - break; - - case UNKNOWNC: - wprintf(L"UNKNOWNC\r"); - break; - - case UNKNOWND: - wprintf(L"UNKNOWND\r"); - break; - - case INSUFFICIENTRIGHTS: - wprintf(L"INSUFFICIENTRIGHTS\r"); - break; - - case STRUCTUREPROGRESS: - wprintf(L"STRUCTUREPROGRESS\r"); - break; - - case DONEWITHSTRUCTURE: - wprintf(L"DONEWITHSTRUCTURE\r"); - break; - - case CLUSTERSIZETOOSMALL: - wprintf(L"CLUSTERSIZETOOSMALL\r"); - break; - - case PROGRESS: - percent = (PDWORD) Argument; - wprintf(L"%d percent completed.\r", *percent); - break; - - case OUTPUT: - output = (PTEXTOUTPUT) Argument; - fwprintf(stdout, L"%s", output->Output); - break; - - case DONE: - status = (PBOOLEAN) Argument; - if ( *status == TRUE ) - { - wprintf(L"Chkdsk was unable to complete successfully.\n\n"); - Error = TRUE; - } - break; - } - return TRUE; + PDWORD percent; + PBOOLEAN status; + PTEXTOUTPUT output; + + // + // We get other types of commands, + // but we don't have to pay attention to them + // + switch (Command) + { + case UNKNOWN2: + wprintf(L"UNKNOWN2\r"); + break; + + case UNKNOWN3: + wprintf(L"UNKNOWN3\n"); + break; + + case UNKNOWN4: + wprintf(L"UNKNOWN4\n"); + break; + + case UNKNOWN5: + wprintf(L"UNKNOWN5\n"); + break; + + case FSNOTSUPPORTED: + wprintf(L"FSNOTSUPPORTED\n"); + break; + + case VOLUMEINUSE: + wprintf(L"VOLUMEINUSE\n"); + break; + + case UNKNOWN9: + wprintf(L"UNKNOWN9\n"); + break; + + case UNKNOWNA: + wprintf(L"UNKNOWNA\n"); + break; + + case UNKNOWNC: + wprintf(L"UNKNOWNC\n"); + break; + + case UNKNOWND: + wprintf(L"UNKNOWND\n"); + break; + + case INSUFFICIENTRIGHTS: + wprintf(L"INSUFFICIENTRIGHTS\n"); + break; + + case STRUCTUREPROGRESS: + wprintf(L"STRUCTUREPROGRESS\n"); + break; + + case DONEWITHSTRUCTURE: + wprintf(L"DONEWITHSTRUCTURE\n"); + break; + + case CLUSTERSIZETOOSMALL: + wprintf(L"CLUSTERSIZETOOSMALL\n"); + break; + + case PROGRESS: + percent = (PDWORD)Argument; + wprintf(L"%d percent completed.\r", *percent); + break; + + case OUTPUT: + output = (PTEXTOUTPUT)Argument; + wprintf(L"%S", output->Output); + break; + + case DONE: + status = (PBOOLEAN)Argument; + if (*status == TRUE) + { + wprintf(L"Chkdsk was unable to complete successfully.\n\n"); + Error = TRUE; + } + break; + } + return TRUE; } #ifndef FMIFS_IMPORT_DLL @@ -311,17 +332,19 @@ ChkdskCallback( BOOLEAN LoadFMIFSEntryPoints(VOID) { - LoadLibraryW( L"fmifs.dll" ); - - if( !(Chkdsk = - (void *) GetProcAddress( - GetModuleHandleW(L"fmifs.dll"), - "Chkdsk" )) - ) - { - return FALSE; - } - return TRUE; + HMODULE hFmifs = LoadLibraryW(L"fmifs.dll"); + if (hFmifs == NULL) + return FALSE; + + Chkdsk = (PCHKDSK)GetProcAddress(hFmifs, "Chkdsk"); + + if (!Chkdsk) + { + FreeLibrary(hFmifs); + return FALSE; + } + + return TRUE; } #endif /* ndef FMIFS_IMPORT_DLL */ @@ -339,147 +362,120 @@ LoadFMIFSEntryPoints(VOID) // //---------------------------------------------------------------------- int -wmain( int argc, WCHAR *argv[] ) +wmain(int argc, WCHAR *argv[]) { - int badArg; - HANDLE volumeHandle; - WCHAR fileSystem [1024]; - WCHAR volumeName [1024]; - DWORD serialNumber; - DWORD flags, - maxComponent; - - wprintf( - L"\n\ + int badArg; + HANDLE volumeHandle; + WCHAR fileSystem[1024]; + WCHAR volumeName[1024]; + DWORD serialNumber; + DWORD flags, maxComponent; + + wprintf(L"\n\ Chkdskx v1.0.1 by Mark Russinovich\n\ Systems Internals - http://www.sysinternals.com/\n\ -ReactOS adaptation 1999 by Emanuele Aliberti\n\n" - ); +ReactOS adaptation 1999 by Emanuele Aliberti\n\n"); + #ifndef FMIFS_IMPORT_DLL - // - // Get function pointers - // - if( !LoadFMIFSEntryPoints()) - { - wprintf(L"Could not located FMIFS entry points.\n\n"); - return -1; - } + // + // Get function pointers + // + if (!LoadFMIFSEntryPoints()) + { + wprintf(L"Could not located FMIFS entry points.\n\n"); + return -1; + } #endif /* ndef FMIFS_IMPORT_DLL */ - // - // Parse command line - // - if( (badArg = ParseCommandLine( argc, argv ))) - { - wprintf( - L"Unknown argument: %s\n", - argv[badArg] - ); - - Usage(argv[0]); - return -1; - } - - // - // Get the drive's format - // - if( !Drive ) - { - if( !GetCurrentDirectoryW( - sizeof(CurrentDirectory), - CurrentDirectory - ) - ) { - - PrintWin32Error( - L"Could not get current directory", - GetLastError() - ); - return -1; - } - - } else { - - wcscpy( CurrentDirectory, Drive ); - } - CurrentDirectory[2] = L'\\'; - CurrentDirectory[3] = L'\0'; - Drive = CurrentDirectory; - - // - // Determine the drive's file system format, which we need to - // tell chkdsk - // - if( !GetVolumeInformationW( - Drive, - volumeName, - sizeof volumeName, - & serialNumber, - & maxComponent, - & flags, - fileSystem, - sizeof fileSystem - ) - ) { - PrintWin32Error( - L"Could not query volume", - GetLastError() - ); - return -1; - } - - // - // If they want to fix, we need to have access to the drive - // - if ( FixErrors ) - { - swprintf( - volumeName, - L"\\\\.\\%C:", - Drive[0] - ); - volumeHandle = CreateFileW( - volumeName, - GENERIC_WRITE, - 0, - NULL, - OPEN_EXISTING, - 0, - 0 - ); - if( volumeHandle == INVALID_HANDLE_VALUE ) - { - wprintf(L"Chdskx cannot run because the volume is in use by another process.\n\n"); - return -1; - } - CloseHandle( volumeHandle ); - - // - // Can't let the user break out of a chkdsk that can modify the drive - // - SetConsoleCtrlHandler( CtrlCIntercept, TRUE ); - } - - // - // Just do it - // - wprintf( - L"The type of file system is %s.\n", - fileSystem - ); - Chkdsk( - Drive, - fileSystem, - FixErrors, - Verbose, - SkipClean, - ScanSectors, - NULL, - NULL, - ChkdskCallback - ); - - if ( Error ) return -1; - return 0; + + // + // Parse command line + // + badArg = ParseCommandLine(argc, argv); + if (badArg) + { + wprintf(L"Unknown argument: %s\n", argv[badArg]); + Usage(argv[0]); + return -1; + } + + // + // Get the drive's format + // + if (!Drive) + { + if (!GetCurrentDirectoryW(ARRAYSIZE(CurrentDirectory), CurrentDirectory)) + { + PrintWin32Error(L"Could not get current directory", GetLastError()); + return -1; + } + } + else + { + wcscpy(CurrentDirectory, Drive); + } + CurrentDirectory[2] = L'\\'; + CurrentDirectory[3] = L'\0'; + Drive = CurrentDirectory; + + // + // Determine the drive's file system format, which we need to + // tell chkdsk + // + if (!GetVolumeInformationW(Drive, + volumeName, + ARRAYSIZE(volumeName), + &serialNumber, + &maxComponent, + &flags, + fileSystem, + ARRAYSIZE(fileSystem))) + { + PrintWin32Error(L"Could not query volume", GetLastError()); + return -1; + } + + // + // If they want to fix, we need to have access to the drive + // + if (FixErrors) + { + swprintf(volumeName, L"\\\\.\\%C:", Drive[0]); + volumeHandle = CreateFileW(volumeName, + GENERIC_WRITE, + 0, + NULL, + OPEN_EXISTING, + 0, + 0); + if (volumeHandle == INVALID_HANDLE_VALUE) + { + wprintf(L"Chdskx cannot run because the volume is in use by another process.\n\n"); + return -1; + } + CloseHandle(volumeHandle); + + // + // Can't let the user break out of a chkdsk that can modify the drive + // + SetConsoleCtrlHandler(CtrlCIntercept, TRUE); + } + + // + // Just do it + // + wprintf(L"The type of file system is %s.\n", fileSystem); + Chkdsk(Drive, + fileSystem, + FixErrors, + Verbose, + SkipClean, + ScanSectors, + NULL, + NULL, + ChkdskCallback); + + if (Error) return -1; + return 0; } /* EOF */ diff --git a/reactos/base/system/chkdsk/chkdsk.rc b/reactos/base/system/chkdsk/chkdsk.rc index d28775e6b83..17962ee7d24 100644 --- a/reactos/base/system/chkdsk/chkdsk.rc +++ b/reactos/base/system/chkdsk/chkdsk.rc @@ -1,6 +1,6 @@ -#define REACTOS_STR_FILE_DESCRIPTION "ReactOS Disk Checking Utility\0" -#define REACTOS_STR_INTERNAL_NAME "chkdsk\0" -#define REACTOS_STR_ORIGINAL_FILENAME "chkdsk.exe\0" -#define REACTOS_STR_ORIGINAL_COPYRIGHT "1998 Mark Russinovich\0" +#define REACTOS_STR_FILE_DESCRIPTION "ReactOS Disk Checking Utility" +#define REACTOS_STR_INTERNAL_NAME "chkdsk" +#define REACTOS_STR_ORIGINAL_FILENAME "chkdsk.exe" +#define REACTOS_STR_ORIGINAL_COPYRIGHT "1998 Mark Russinovich" #include -- 2.17.1