Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
6.4 KiB

  1. #include <pch.cxx>
  2. #pragma hdrstop
  3. #include <ole2.h>
  4. #include "trkwks.hxx"
  5. #include "dltadmin.hxx"
  6. class CLogFileNotify : public PLogFileNotify
  7. {
  8. void OnHandlesMustClose()
  9. {
  10. return;
  11. }
  12. };
  13. BOOL
  14. EmptyLogFile( LONG iVol )
  15. {
  16. LogInfo loginfo, loginfoNew;
  17. CLogFile logfile;
  18. BOOL fSuccess = FALSE;
  19. TCHAR tszFile[ MAX_PATH + 1 ];
  20. CTrkWksConfiguration wksconfig;
  21. CLogFileNotify logfilenotify;
  22. __try
  23. {
  24. wksconfig.Initialize();
  25. memset( &loginfo, 0, sizeof(loginfo) );
  26. memset( &loginfoNew, 0, sizeof(loginfoNew) );
  27. logfile.Initialize( static_cast<const TCHAR*>(CVolumeDeviceName(iVol)),
  28. &wksconfig, &logfilenotify, VolChar(iVol) );
  29. logfile.ReadExtendedHeader( CLOG_LOGINFO_START, &loginfo, sizeof(loginfo) );
  30. loginfoNew.ilogStart = loginfoNew.ilogWrite = loginfoNew.ilogRead = 0;
  31. loginfoNew.ilogLast = loginfoNew.ilogEnd = loginfo.ilogEnd;
  32. loginfoNew.seqNext = 0;
  33. loginfoNew.seqLastRead = loginfoNew.seqNext - 1;
  34. logfile.WriteExtendedHeader( CLOG_LOGINFO_START, &loginfoNew, sizeof(loginfoNew) );
  35. logfile.SetShutdown( TRUE );
  36. logfile.InitializeLogEntries( 0, logfile.NumEntriesInFile() - 1 );
  37. _tprintf( TEXT(" Emptied log\n" ) );
  38. fSuccess = TRUE;
  39. }
  40. __except( EXCEPTION_EXECUTE_HANDLER )
  41. {
  42. logfile.UnInitialize();
  43. }
  44. return( fSuccess );
  45. }
  46. BOOL
  47. DeleteLogFile( LONG iVol )
  48. {
  49. NTSTATUS status;
  50. BOOL fSuccess = FALSE;
  51. TCHAR tszFile[ MAX_PATH + 1 ];
  52. __try // __except
  53. {
  54. status = SetVolId( iVol, CVolumeId() );
  55. if( !NT_SUCCESS(status) )
  56. {
  57. _tprintf( TEXT(" Couldn't delete vol ID (%08x)\n"), status );
  58. __leave;
  59. }
  60. _tcscpy( tszFile, static_cast<const TCHAR*>(CVolumeDeviceName(iVol)) );
  61. _tcscat( tszFile, s_tszLogFileName );
  62. for( int i = 0; i < 4; i ++ )
  63. {
  64. // Delete the file
  65. if(!DeleteFile( tszFile ))
  66. {
  67. LONG lLastError = GetLastError();
  68. if( ERROR_FILE_NOT_FOUND != lLastError
  69. &&
  70. ERROR_PATH_NOT_FOUND != lLastError )
  71. {
  72. _tprintf(TEXT(" Couldn't delete %s (%08x)\n"), tszFile, GetLastError());
  73. }
  74. }
  75. else
  76. _tprintf( TEXT(" Deleted %s\n"), tszFile );
  77. if( 1 & i ) // 1, 3
  78. {
  79. CDirectoryName dirname;
  80. dirname.SetFromFileName( tszFile );
  81. if(!RemoveDirectory( dirname ))
  82. {
  83. LONG lLastError = GetLastError();
  84. if( ERROR_FILE_NOT_FOUND != lLastError )
  85. {
  86. _tprintf(TEXT(" Couldn't delete %s (%lu)\n"),
  87. static_cast<const TCHAR*>(dirname), lLastError);
  88. }
  89. }
  90. else
  91. _tprintf( TEXT(" Deleted %s\n"), static_cast<const TCHAR*>(dirname) );
  92. _tcscpy( tszFile, static_cast<const TCHAR*>(CVolumeDeviceName(iVol)) );
  93. _tcscat( tszFile, s_tszOldLogFileName );
  94. }
  95. else
  96. _tcscat( tszFile, TEXT(".bak") );
  97. }
  98. fSuccess = TRUE;
  99. }
  100. __except( EXCEPTION_EXECUTE_HANDLER )
  101. {
  102. _tprintf( TEXT("Exception %08x in DeleteLogFileAndOids"), GetExceptionCode() );
  103. }
  104. return( fSuccess );
  105. }
  106. BOOL
  107. DltAdminCleanVol( ULONG cArgs, TCHAR * const rgptszArgs[], ULONG *pcEaten )
  108. {
  109. NTSTATUS status = 0;
  110. TCHAR* ptcTmp = NULL;
  111. LONG iVol, iVolChar = 0;
  112. BOOL fSuccess = TRUE;
  113. BOOL fEmptyLog = FALSE;
  114. *pcEaten = 0;
  115. if( 1 <= cArgs )
  116. {
  117. _tcsupr( rgptszArgs[0] );
  118. if( IsHelpArgument( rgptszArgs[0] ))
  119. {
  120. printf( "\nOption CleanVol\n"
  121. " Purpose: Clean all the IDs (object and volume) a volume\n"
  122. " Usage: -cleanvol [options] [drive letter]\n"
  123. " Options: -e Empty log rather than deleting it\n"
  124. " E.g.: -cleanvol -r D:\n"
  125. " -cleanvol\n"
  126. " Note: If no drive is specified, all drives will be cleaned\n" );
  127. *pcEaten = 1;
  128. return( TRUE );
  129. }
  130. if( TEXT('-') == rgptszArgs[0][0]
  131. ||
  132. TEXT('/') == rgptszArgs[0][0] )
  133. {
  134. (*pcEaten)++;
  135. switch( rgptszArgs[0][1] )
  136. {
  137. case 'E':
  138. fEmptyLog = TRUE;
  139. break;
  140. default:
  141. _tprintf( TEXT("Invalid option. Use -cleanvol -? for help\n") );
  142. return( TRUE );
  143. }
  144. iVolChar = 1;
  145. }
  146. else
  147. iVolChar = 0;
  148. }
  149. if( iVolChar < cArgs )
  150. {
  151. (*pcEaten)++;
  152. _tcsupr( rgptszArgs[iVolChar] );
  153. iVol = *rgptszArgs[iVolChar] - TEXT('A');
  154. }
  155. else
  156. iVol = 0;
  157. EnablePrivilege( SE_RESTORE_NAME );
  158. while( iVol < 26 )
  159. {
  160. if( IsLocalObjectVolume( iVol ))
  161. {
  162. LONG lLastError = 0;
  163. printf( "Cleaning volume %c:\n", iVol+TEXT('A') );
  164. if( fEmptyLog )
  165. fSuccess = EmptyLogFile( iVol );
  166. else
  167. fSuccess = DeleteLogFile( iVol );
  168. __try // except
  169. {
  170. CObjId objid;
  171. CDomainRelativeObjId droid;
  172. CObjIdEnumerator oie;
  173. ULONG cObjId = 0;
  174. if(oie.Initialize(CVolumeDeviceName(iVol)) == TRUE)
  175. {
  176. if(oie.FindFirst(&objid, &droid))
  177. {
  178. do
  179. {
  180. DelObjId( iVol, objid );
  181. cObjId++;
  182. } while(oie.FindNext(&objid, &droid));
  183. printf( " Deleted %d object ID%s\n",
  184. cObjId,
  185. 1 == cObjId ? "" : "s" );
  186. }
  187. }
  188. }
  189. __except( BreakOnDebuggableException() )
  190. {
  191. }
  192. } // if( IsLocalObjectVolume( iVol ))
  193. if( 1 <= cArgs ) break;
  194. iVol++;
  195. } // while( iVol < 26 )
  196. return( fSuccess );
  197. }