Leaked source code of windows server 2003
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.

571 lines
14 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1999-2001.
  5. //
  6. // File: Main.cxx
  7. //
  8. // Contents: This the implementation for the main module of srdiag.exe.
  9. //
  10. // Classes: n/a
  11. //
  12. // Functions: DiaplayHelp
  13. // ParseCmdLine
  14. // Main
  15. //
  16. // Coupling:
  17. //
  18. // Notes:
  19. //
  20. // History: 20-04-2001 weiyouc Created
  21. //
  22. //----------------------------------------------------------------------------
  23. //--------------------------------------------------------------------------
  24. // Headers
  25. //--------------------------------------------------------------------------
  26. #include "SrHeader.hxx"
  27. #ifdef ARRAYSIZE
  28. #undef ARRAYSIZE
  29. #endif
  30. #include "stdafx.h"
  31. #include "srdiag.h"
  32. //--------------------------------------------------------------------------
  33. // Local defines
  34. //--------------------------------------------------------------------------
  35. #define CAB_FILE_EXT ".cab"
  36. //--------------------------------------------------------------------------
  37. // Local function prototypes
  38. //--------------------------------------------------------------------------
  39. void DisplayHelp();
  40. HRESULT ParseCmdLine(BOOL* pfDisplayHelp,
  41. LPTSTR* pptszCabFileName,
  42. LPTSTR* pptszCabLoc);
  43. HRESULT GenerateCabFileName(LPTSTR* pptszCabFile);
  44. HRESULT OpenCabFile(MPC::Cabinet* pCab,
  45. LPTSTR ptszCabFileName,
  46. LPTSTR ptszCabLoc);
  47. HRESULT AppendFilesToCabFile(MPC::Cabinet * pCab);
  48. //--------------------------------------------------------------------------
  49. // Global Variables
  50. //--------------------------------------------------------------------------
  51. //
  52. // List of files that we will collect at %windir% directory.
  53. //
  54. LPCTSTR g_tszWindirFileCollection[] =
  55. {
  56. TEXT("\\system32\\restore\\machineguid.txt"),
  57. TEXT("\\system32\\restore\\filelist.xml"),
  58. TEXT("\\system32\\restore\\rstrlog.dat")
  59. };
  60. //
  61. // List of the files, that we will collect at the root of
  62. // %systemdrv%\System Volume Information\_Restore{GUID} directory.
  63. //
  64. LPCTSTR g_tszSysVolFileCollection[] =
  65. {
  66. TEXT("_filelst.cfg"),
  67. TEXT("drivetable.txt"),
  68. TEXT("_driver.cfg"),
  69. TEXT("fifo.log"),
  70. };
  71. //--------------------------------------------------------------------------
  72. // Functions
  73. //--------------------------------------------------------------------------
  74. //+---------------------------------------------------------------------------
  75. //
  76. // Function: DisplayHelp
  77. //
  78. // Synopsis: Displays a help page for the user
  79. //
  80. // Arguments: (none)
  81. //
  82. // Returns: void
  83. //
  84. // History: 20-04-2001 weiyouc Created
  85. //
  86. // Notes:
  87. //
  88. //----------------------------------------------------------------------------
  89. void DisplayHelp()
  90. {
  91. printf("\n");
  92. printf("Microsoft Windows XP \n");
  93. printf("Usage: SrDiag [/CabName:test.cab] [/CabLoc:\"c:\\temp\\\"] \n");
  94. printf(" /CabName is the full name of the cab file that you wish to use. \n"
  95. " If the cab file name is not specified, the system will \n"
  96. " automatically generate one in the following format: \n"
  97. " <machine_name>_mmddyy_hhss.cab \n");
  98. printf(" /CabLoc points to the location to store the cab. \n"
  99. " It must have a \\ on the end. \n"
  100. " The default location is the current directory. \n");
  101. }
  102. //+---------------------------------------------------------------------------
  103. //
  104. // Function: ParseCmdLine
  105. //
  106. // Synopsis: Sets Globals based on Cmd and Enviroment settings
  107. //
  108. // Arguments: (none)
  109. //
  110. // Returns: HRESULT
  111. //
  112. // History: 20-04-2001 weiyouc Created
  113. //
  114. // Notes:
  115. //
  116. //----------------------------------------------------------------------------
  117. HRESULT ParseCmdLine(BOOL* pfDisplayHelp,
  118. LPTSTR* pptszCabFileName,
  119. LPTSTR* pptszCabLoc)
  120. {
  121. HRESULT hr = S_OK;
  122. //
  123. // Do we want to generate full string for printed test cases?
  124. //
  125. *pfDisplayHelp = (GETPARAM_ISPRESENT("help") ||
  126. GETPARAM_ISPRESENT("?"));
  127. //
  128. // Do we specify the cab file name or not
  129. //
  130. GETPARAM_ABORTONERROR("CabName:tstr", *pptszCabFileName);
  131. //
  132. // If cab file location is
  133. //
  134. GETPARAM_ABORTONERROR("CabLoc:tstr", *pptszCabLoc);
  135. ErrReturn:
  136. return hr;
  137. } // ParseCmdLine
  138. //+---------------------------------------------------------------------------
  139. //
  140. // Function: main
  141. //
  142. // Synopsis: Entry point for srdiag.exe
  143. //
  144. // Arguments: [argc] -- Command Line Arg Count
  145. // [argv] -- Command Line Args
  146. //
  147. // Returns: VOID
  148. //
  149. // History: 20-04-2001 weiyouc Created
  150. //
  151. // Notes:
  152. //
  153. //----------------------------------------------------------------------------
  154. void __cdecl main (int argc, char *argv[])
  155. {
  156. HRESULT hr = S_OK;
  157. BOOL fDisplayHelp = FALSE;
  158. LPTSTR ptszCabFileName = NULL;
  159. LPTSTR ptszCabLoc = NULL;
  160. MPC::Cabinet Cab;
  161. hr = ParseCmdLine(&fDisplayHelp,
  162. &ptszCabFileName,
  163. &ptszCabLoc);
  164. DH_HRCHECK_ABORT(hr, TEXT("ParseCmdLine"));
  165. //
  166. // Do we need to display help
  167. //
  168. if (fDisplayHelp)
  169. {
  170. DisplayHelp();
  171. goto ErrReturn;
  172. }
  173. //
  174. // Make sure that we always start from a clean environment
  175. //
  176. hr = CleanupFiles();
  177. DH_HRCHECK_ABORT(hr, TEXT("CleanupFiles"));
  178. //
  179. // Open the cab file
  180. //
  181. hr = OpenCabFile(&Cab, ptszCabFileName, ptszCabLoc);
  182. DH_HRCHECK_ABORT(hr, TEXT("OpenCabFile"));
  183. //
  184. // Now append the files to the cab file
  185. //
  186. hr = AppendFilesToCabFile(&Cab);
  187. DH_HRCHECK_ABORT(hr, TEXT("AppendFilesToCabFile"));
  188. //
  189. // Now we create the cab file for real
  190. //
  191. hr = Cab.Compress();
  192. DH_HRCHECK_ABORT(hr, TEXT("Cabinet::Compress"));
  193. //
  194. // Clean up the files
  195. //
  196. hr = CleanupFiles();
  197. DH_HRCHECK_ABORT(hr, TEXT("CleanupFiles"));
  198. ErrReturn:
  199. CleanupTStr(&ptszCabFileName);
  200. CleanupTStr(&ptszCabLoc);
  201. return;
  202. }
  203. //+---------------------------------------------------------------------------
  204. //
  205. // Function: GenerateCabFileName
  206. //
  207. // Synopsis: Generate a cab file name = ComputerName + ddmmyy + hhmmss
  208. //
  209. // Arguments: [pptszCabFile] -- cab file name
  210. //
  211. // Returns: HRESULT
  212. //
  213. // History: 20-04-2001 weiyouc Created
  214. //
  215. // Notes:
  216. //
  217. //----------------------------------------------------------------------------
  218. HRESULT GenerateCabFileName(LPTSTR* pptszCabFile)
  219. {
  220. HRESULT hr = S_OK;
  221. time_t ltime = 0;
  222. tm* ptmNow = NULL;
  223. size_t stRet = 0;
  224. TCHAR tszTmTemp[MAX_PATH];
  225. TCHAR tszCabFile[MAX_PATH];
  226. DH_VDATEPTROUT(pptszCabFile, LPTSTR);
  227. //
  228. // Copy Computer Name to CabFileName
  229. //
  230. _tcscpy(tszCabFile, _tgetenv(TEXT("COMPUTERNAME")));
  231. DH_HRCHECK_ABORT(hr, TEXT("CopyString"));
  232. //
  233. // Append Undescore character to CabFileName
  234. //
  235. _tcscat(tszCabFile, TEXT("_"));
  236. //
  237. // Get System Time and Date
  238. //
  239. time(&ltime);
  240. ptmNow = localtime(&ltime);
  241. //
  242. // Convert time/date to mmddyyhhmmss format (24hr)
  243. //
  244. stRet = _tcsftime(tszTmTemp,
  245. MAX_PATH,
  246. TEXT("%m%d%y_%H%M%S"),
  247. ptmNow);
  248. DH_ABORTIF(0 == stRet,
  249. E_FAIL,
  250. TEXT("_tcsftime"));
  251. _tcscat(tszCabFile, tszTmTemp);
  252. //
  253. // Finally append on the extension and now we are set.
  254. //
  255. _tcscat(tszCabFile, TEXT(CAB_FILE_EXT));
  256. hr = CopyString(tszCabFile, pptszCabFile);
  257. DH_HRCHECK_ABORT(hr, TEXT("CopyString"));
  258. ErrReturn:
  259. return hr;
  260. }
  261. //+---------------------------------------------------------------------------
  262. //
  263. // Function: OpenCabFile
  264. //
  265. // Synopsis: Open the cab file
  266. //
  267. // Arguments: [pCab] -- pointer to the cab file
  268. // [ptszCabFileName] -- cab file name
  269. // [ptszCabLoc] -- cab file location
  270. //
  271. // Returns: HRESULT
  272. //
  273. // History: 20-04-2001 weiyouc Created
  274. //
  275. // Notes:
  276. //
  277. //----------------------------------------------------------------------------
  278. HRESULT OpenCabFile(MPC::Cabinet* pCab,
  279. LPTSTR ptszCabFileName,
  280. LPTSTR ptszCabLoc)
  281. {
  282. HRESULT hr = S_OK;
  283. LPTSTR ptszFullCabFileName = NULL;
  284. DH_VDATEPTRIN(pCab, MPC::Cabinet);
  285. //
  286. // If the user does not specify the cab file name,
  287. // we will generate one.
  288. //
  289. if (NULL == ptszCabFileName)
  290. {
  291. hr = GenerateCabFileName(&ptszCabFileName);
  292. DH_HRCHECK_ABORT(hr, TEXT("GenerateCabFileName"));
  293. }
  294. //
  295. // If the use specifies a location for the cab file,
  296. // we need to glue it together with the cab file name
  297. //
  298. if (NULL != ptszCabLoc)
  299. {
  300. hr = SrTstTStrCat(ptszCabLoc,
  301. ptszCabFileName,
  302. &ptszFullCabFileName);
  303. DH_HRCHECK_ABORT(hr, TEXT("SrTstStrCat"));
  304. }
  305. else
  306. {
  307. hr = CopyString(ptszCabFileName, &ptszFullCabFileName);
  308. DH_HRCHECK_ABORT(hr, TEXT("CopyString"));
  309. }
  310. //
  311. // Open the cabinet file
  312. //
  313. hr = pCab->put_CabinetFile(ptszFullCabFileName);
  314. DH_HRCHECK_ABORT(hr, TEXT("Cabinet::put_CabinetFile"));
  315. //
  316. // Also set a flag to ignore the missing file
  317. //
  318. hr = pCab->put_IgnoreMissingFiles(TRUE);
  319. DH_HRCHECK_ABORT(hr, TEXT("Cabinet::put_IgnoreMissingFiles"));
  320. ErrReturn:
  321. CleanupTStr(&ptszFullCabFileName);
  322. return hr;
  323. }
  324. //+---------------------------------------------------------------------------
  325. //
  326. // Function: OpenCabFile
  327. //
  328. // Synopsis: Append files to the cab file. However it has not written
  329. // anyting to the cab file yet
  330. //
  331. // Arguments: [pCab] -- pointer to the cab file
  332. //
  333. // Returns: HRESULT
  334. //
  335. // History: 20-04-2001 weiyouc Created
  336. //
  337. // Notes:
  338. //
  339. //----------------------------------------------------------------------------
  340. HRESULT AppendFilesToCabFile(MPC::Cabinet* pCab)
  341. {
  342. HRESULT hr = S_OK;
  343. int i = 0;
  344. LPTSTR ptszFileName = NULL;
  345. LPTSTR ptszDSOnSys = NULL;
  346. DH_VDATEPTRIN(pCab, MPC::Cabinet);
  347. //
  348. // Add SR related registery settings
  349. //
  350. hr = GetSRRegInfo(TEXT("SR-Reg.TXT"));
  351. DH_HRCHECK_ABORT(hr, TEXT("GetSRRegInfo"));
  352. hr = pCab->AddFile(TEXT("SR-Reg.TXT"));
  353. DH_HRCHECK_ABORT(hr, TEXT("MPC::Cabinet::AddFile()"));
  354. //
  355. // Add files based on WinDir relative root
  356. //
  357. for (i = 0; i < ARRAYSIZE(g_tszWindirFileCollection); i++)
  358. {
  359. hr = SrTstTStrCat(_tgetenv(TEXT("WINDIR")),
  360. g_tszWindirFileCollection[i],
  361. &ptszFileName);
  362. DH_HRCHECK_ABORT(hr, TEXT("SrTstTStrCat"));
  363. hr = pCab->AddFile(ptszFileName);
  364. DH_HRCHECK_ABORT(hr, TEXT("MPC::Cabinet::AddFile"));
  365. CleanupTStr(&ptszFileName);
  366. }
  367. //
  368. // Parse the restore log and add it to the cab
  369. //
  370. hr = SrTstTStrCat(_tgetenv(TEXT("WINDIR")),
  371. g_tszWindirFileCollection[2],
  372. &ptszFileName);
  373. DH_HRCHECK_ABORT(hr, TEXT("SrTstTStrCat"));
  374. hr = ParseRstrLog(ptszFileName, TEXT("SR-RstrLog.TXT"));
  375. DH_HRCHECK_ABORT(hr, TEXT("ParseRstrLog"));
  376. hr = pCab->AddFile(TEXT("SR-RstrLog.TXT"));
  377. DH_HRCHECK_ABORT(hr, TEXT("MPC::Cabinet::AddFile"));
  378. CleanupTStr(&ptszFileName);
  379. //
  380. // Get the restore directory on the system drive,
  381. // and then Add in critial files
  382. //
  383. hr = GetDSOnSysVol(&ptszDSOnSys);
  384. DH_HRCHECK_ABORT(hr, TEXT("GetDSOnSysVol"));
  385. //
  386. // Add files Bases on System Volume Information relative root
  387. //
  388. for (i = 0; i < ARRAYSIZE(g_tszSysVolFileCollection); i++)
  389. {
  390. hr = SrTstTStrCat(ptszDSOnSys,
  391. g_tszSysVolFileCollection[i],
  392. &ptszFileName);
  393. DH_HRCHECK_ABORT(hr, TEXT("SrTstTStrCat"));
  394. hr = pCab->AddFile(ptszFileName);
  395. DH_HRCHECK_ABORT(hr, TEXT("MPC::Cabinet::AddFile()"));
  396. CleanupTStr(&ptszFileName);
  397. }
  398. //
  399. // Get the Restore point enumeration, and then cab the file
  400. //
  401. hr = RPEnumDrives(pCab, TEXT("SR-RP.LOG"));
  402. DH_HRCHECK_ABORT(hr, TEXT("RPEnumDrives"));
  403. hr = pCab->AddFile(TEXT("SR-RP.LOG"));
  404. DH_HRCHECK_ABORT(hr, TEXT("MPC::Cabinet::AddFile()"));
  405. //
  406. // Get change logs
  407. //
  408. hr = GetChgLogOnDrives(TEXT("SR-ChgLog.LOG"));
  409. DH_HRCHECK_ABORT(hr, TEXT("GetChgLog"));
  410. hr = pCab->AddFile(TEXT("SR-ChgLog.LOG"));
  411. DH_HRCHECK_ABORT(hr, TEXT("MPC::Cabinet::AddFile()"));
  412. //
  413. // Get file versions
  414. //
  415. hr = GetSRFileInfo(TEXT("SR-FileList.LOG"));
  416. DH_HRCHECK_ABORT(hr, TEXT("GetSRFileInfo"));
  417. hr = pCab->AddFile(TEXT("SR-FileList.LOG"));
  418. DH_HRCHECK_ABORT(hr, TEXT("MPC::Cabinet::AddFile()"));
  419. //
  420. // Get SR related event logs
  421. //
  422. hr = GetSREvents(TEXT("SR-EventLogs.TXT"));
  423. DH_HRCHECK_ABORT(hr, TEXT("GetSREvents"));
  424. hr = pCab->AddFile(TEXT("SR-EventLogs.TXT"));
  425. DH_HRCHECK_ABORT(hr, TEXT("MPC::Cabinet::AddFile()"));
  426. ErrReturn:
  427. CleanupTStr(&ptszFileName);
  428. CleanupTStr(&ptszDSOnSys);
  429. return hr;
  430. }
  431. //+---------------------------------------------------------------------------
  432. //
  433. // Function: CleanupFiles
  434. //
  435. // Synopsis: Remove the temporary file used by srdiag
  436. //
  437. // Arguments: none
  438. //
  439. // Returns: HRESULT
  440. //
  441. // History: 20-04-2001 weiyouc Created
  442. //
  443. // Notes:
  444. //
  445. //----------------------------------------------------------------------------
  446. HRESULT CleanupFiles()
  447. {
  448. HRESULT hr = S_OK;
  449. DeleteFileW(L"SR-Reg.TXT");
  450. DeleteFileW(L"SR-RstrLog.TXT");
  451. DeleteFileW(L"SR-RP.LOG");
  452. DeleteFileW(L"SR-ChgLog.LOG");
  453. DeleteFileW(L"SR-FileList.LOG");
  454. DeleteFileW(L"SR-EventLogs.TXT");
  455. return hr;
  456. }