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.

486 lines
13 KiB

  1. /******************************************************************************
  2. *
  3. * Copyright (c) 2000 Microsoft Corporation
  4. *
  5. * Module Name:
  6. * RPEnum.cxx
  7. *
  8. * Abstract:
  9. * Tool for enumerating the restore points - forward/reverse
  10. *
  11. * Revision History:
  12. *
  13. * Brijesh Krishnaswami (brijeshk) 04/13/2000
  14. * created
  15. *
  16. * Stephen Heffner (sheffner)
  17. * I just copied this source, and using the existing API's so that
  18. * srdiag will also be in sync as changes occur to the file
  19. * structure.
  20. *
  21. * Weiyou Cui (weiyouc) 02-May-2001
  22. * Rewritten
  23. *
  24. *****************************************************************************/
  25. //+---------------------------------------------------------------------------
  26. //
  27. // Common Includes
  28. //
  29. //----------------------------------------------------------------------------
  30. #include "SrHeader.hxx"
  31. #include <shellapi.h>
  32. #include <enumlogs.h>
  33. #ifdef ARRAYSIZE
  34. #undef ARRAYSIZE
  35. #endif
  36. #include "stdafx.h"
  37. #include "srdiag.h"
  38. //+---------------------------------------------------------------------------
  39. //
  40. // Function proto types
  41. //
  42. //----------------------------------------------------------------------------
  43. HRESULT GetSRGuid(LPTSTR* pptszSRGuid);
  44. HRESULT GetRPLogsOnVolume(MPC::Cabinet* pCab,
  45. LPTSTR ptszLogFile,
  46. LPTSTR ptszVolume);
  47. //+---------------------------------------------------------------------------
  48. //
  49. // Simple Array's to say how to print the Months, Days
  50. //
  51. //----------------------------------------------------------------------------
  52. LPCTSTR g_tszMonth[] =
  53. {
  54. TEXT("January"),
  55. TEXT("Feburary"),
  56. TEXT("March"),
  57. TEXT("April"),
  58. TEXT("May"),
  59. TEXT("June"),
  60. TEXT("July"),
  61. TEXT("August"),
  62. TEXT("September"),
  63. TEXT("October"),
  64. TEXT("November"),
  65. TEXT("December")
  66. };
  67. LPCTSTR g_tszDay[] =
  68. {
  69. TEXT("Sunday"),
  70. TEXT("Monday"),
  71. TEXT("Tuesday"),
  72. TEXT("Wednesday"),
  73. TEXT("Thursday"),
  74. TEXT("Friday"),
  75. TEXT("Saturday")
  76. };
  77. //+---------------------------------------------------------------------------
  78. //
  79. // Files to collect for each Restore Point, on all drives.
  80. //
  81. //----------------------------------------------------------------------------
  82. LPCTSTR g_tszRPFileList[] =
  83. {
  84. TEXT("restorepointsize"),
  85. TEXT("drivetable.txt"),
  86. TEXT("rp.log"),
  87. };
  88. //+---------------------------------------------------------------------------
  89. //
  90. // Types of restorepoints, based off of Brijesh's code
  91. //
  92. //----------------------------------------------------------------------------
  93. LPCTSTR g_tszRPDescrip[] =
  94. {
  95. TEXT("APPLICATION_INSTALL"),
  96. TEXT("APPLICATION_UNINSTALL"),
  97. TEXT("DESKTOP_SETTING"),
  98. TEXT("ACCESSIBILITY_SETTING"),
  99. TEXT("OE_SETTING"),
  100. TEXT("APPLICATION_RUN"),
  101. TEXT("RESTORE"),
  102. TEXT("CHECKPOINT"),
  103. TEXT("WINDOWS_SHUTDOWN"),
  104. TEXT("WINDOWS_BOOT"),
  105. TEXT("DEVICE_DRIVER_CHANGE"),
  106. TEXT("FIRSTRUN"),
  107. TEXT("MODIFY_SETTINGS"),
  108. TEXT("CANCELLED_OPERATION")
  109. };
  110. //+---------------------------------------------------------------------------
  111. //
  112. // Function: RPEnumDrives
  113. //
  114. // Synopsis: Via the FindFirstVolume, and FindNext get all of the valid
  115. // volumes on the system. I then transulate this volume, to the
  116. // actual path and then pass that information to GetRPLogs which
  117. // gets the restore point logs.
  118. //
  119. // Arguments: [pCab] -- pointer to the cabinet file
  120. // [ptszLogFile] -- The name of the log file
  121. //
  122. // Returns: HRESULT
  123. //
  124. // History: 02-May-2001
  125. //
  126. //
  127. //----------------------------------------------------------------------------
  128. HRESULT RPEnumDrives(MPC::Cabinet* pCab,
  129. LPTSTR ptszLogFile)
  130. {
  131. HRESULT hr = S_OK;
  132. DWORD dwSize = 0;
  133. HANDLE hVolume = INVALID_HANDLE_VALUE;
  134. BOOL fOK = FALSE;
  135. TCHAR tszVolume[MAX_PATH];
  136. TCHAR tszMount[MAX_PATH];
  137. DH_VDATEPTRIN(pCab, MPC::Cabinet);
  138. DH_VDATEPTRIN(ptszLogFile, TCHAR);
  139. hVolume = FindFirstVolume(tszVolume, MAX_PATH);
  140. if (INVALID_HANDLE_VALUE != hVolume)
  141. {
  142. do
  143. {
  144. dwSize = MAX_PATH;
  145. //
  146. // Check to make sure that this is a fixed volume, and then
  147. // get the change log, else skip.
  148. //
  149. if (DRIVE_FIXED == GetDriveType(tszVolume))
  150. {
  151. //
  152. // First get the Friendly name for the current Volume, and get log
  153. //
  154. fOK = GetVolumePathNamesForVolumeName(tszVolume,
  155. tszMount,
  156. MAX_PATH,
  157. &dwSize);
  158. DH_ABORTIF(!fOK,
  159. HRESULT_FROM_WIN32(GetLastError()),
  160. TEXT("GetVolumePathNamesForVolumeName"));
  161. hr = GetRPLogsOnVolume(pCab,
  162. ptszLogFile,
  163. tszMount);
  164. DH_HRCHECK_ABORT(hr, TEXT("GetRPLogsOnVolume"));
  165. }
  166. } while (FindNextVolume(hVolume, tszVolume, MAX_PATH));
  167. }
  168. ErrReturn:
  169. if (INVALID_HANDLE_VALUE != hVolume)
  170. {
  171. FindVolumeClose(hVolume);
  172. }
  173. return hr;
  174. }
  175. //+---------------------------------------------------------------------------
  176. //
  177. // Function: GetRPLogsOnVolume
  178. //
  179. // Synopsis: This will enumerate the restore points on the volume path
  180. // that is provided, writting this information out to the logfile
  181. // specified.
  182. //
  183. // Arguments: [pCab] -- Pointer to the cabinet object
  184. // [ptszLogFile] -- Log file name
  185. // [ptszVolume] -- Path to the Volume for the restore point API
  186. // to work.
  187. //
  188. // Returns: HRESULT
  189. //
  190. // History: 02-May-2001 weiyouc Created
  191. //
  192. //----------------------------------------------------------------------------
  193. HRESULT GetRPLogsOnVolume(MPC::Cabinet* pCab,
  194. LPTSTR ptszLogFile,
  195. LPTSTR ptszVolume)
  196. {
  197. HRESULT hr = S_OK;
  198. INT64 i64Size = 0;
  199. int iCount = 0;
  200. LPTSTR ptszSRGuid = NULL;
  201. FILE* fpLogFile = NULL;
  202. FILE* fpRPLog = NULL;
  203. FILETIME* pFileTime = NULL;
  204. RESTOREPOINTINFOW pRpinfo;
  205. SYSTEMTIME SysTime;
  206. TCHAR tszString[MAX_PATH];
  207. TCHAR tszSRFileName[MAX_PATH];
  208. TCHAR tszFileNameInCab[MAX_PATH];
  209. CRestorePoint RP;
  210. DH_VDATEPTRIN(pCab, MPC::Cabinet*);
  211. DH_VDATEPTRIN(ptszLogFile, TCHAR);
  212. DH_VDATEPTRIN(ptszVolume, TCHAR);
  213. //
  214. // Initialization of the RestorePointEnum obejct
  215. //
  216. CRestorePointEnum RPEnum(ptszVolume, TRUE, FALSE);
  217. //
  218. // Get restore GUID
  219. //
  220. hr = GetSRGuid(&ptszSRGuid);
  221. DH_HRCHECK_ABORT(hr, TEXT("GetSRGuid"));
  222. //
  223. // Open the log file for appending
  224. //
  225. fpLogFile = _tfopen(ptszLogFile, TEXT("a"));
  226. DH_ABORTIF(NULL == fpLogFile,
  227. E_FAIL,
  228. TEXT("_tfopen"));
  229. fprintf(fpLogFile, "\nProcessing Mount Point [%S]\n", ptszVolume);
  230. //
  231. // If we have a valid restore point, enumerate through all of them and
  232. // log the results.
  233. //
  234. if (ERROR_SUCCESS == RPEnum.FindFirstRestorePoint(RP))
  235. {
  236. do
  237. {
  238. //Get RestorePoint Size for the restore point log.
  239. _stprintf(tszString,
  240. TEXT("%sSystem Volume Information\\_restore%S\\%s\\restorepointsize"),
  241. ptszVolume,
  242. ptszSRGuid,
  243. RP.GetDir());
  244. fpRPLog = _tfopen(tszString, TEXT("r"));
  245. if (NULL != fpRPLog)
  246. {
  247. fread(&i64Size, sizeof(INT64), 1, fpRPLog);
  248. fclose(fpRPLog);
  249. }
  250. else
  251. {
  252. i64Size=0;
  253. }
  254. //
  255. // Get the time, and then convert it to localsystemtime,
  256. // and then pump out the rest of the DataStructures
  257. //
  258. pFileTime = RP.GetTime();
  259. FileTimeToSystemTime(pFileTime, &SysTime);
  260. //
  261. // format should be field=value, field=value, ...
  262. //
  263. fprintf(fpLogFile,
  264. "DirectoryName=%S, "
  265. "Size=%I64ld, "
  266. "Type=%ld[%S], "
  267. "RestorePointName=%S, "
  268. "RestorePointStatus=%S, "
  269. "Number=%ul, "
  270. "Date=%S %S %lu, %lu %lu:%lu:%lu\n",
  271. RP.GetDir(),
  272. i64Size,
  273. RP.GetType(),
  274. g_tszRPDescrip[RP.GetType()],
  275. RP.GetName(),
  276. RP.IsDefunct() ? TEXT("[Cancelled]") : TEXT("[VALID]"),
  277. RP.GetNum(),
  278. g_tszDay[SysTime.wDayOfWeek],
  279. g_tszMonth[SysTime.wMonth - 1],
  280. SysTime.wDay,
  281. SysTime.wYear,
  282. SysTime.wHour,
  283. SysTime.wMinute,
  284. SysTime.wSecond);
  285. //
  286. // Now append the files needed per restore point
  287. //
  288. for (iCount = 0; iCount < ARRAYSIZE(g_tszRPFileList); iCount++)
  289. {
  290. //
  291. // figure out which file we need to cab
  292. //
  293. _stprintf(tszSRFileName,
  294. TEXT("%sSystem Volume Information\\_restore%s\\%s\\%s"),
  295. ptszVolume,
  296. ptszSRGuid,
  297. RP.GetDir(),
  298. g_tszRPFileList[iCount]);
  299. //
  300. // generate a new name for this file in the cab
  301. //
  302. //
  303. // we also need to replace ":\" with "--"
  304. //
  305. _stprintf(tszFileNameInCab,
  306. TEXT("%s"),
  307. ptszVolume);
  308. _stprintf((tszFileNameInCab + 1),
  309. TEXT("--%s-%s"),
  310. RP.GetDir(),
  311. g_tszRPFileList[iCount]);
  312. hr = pCab->AddFile(tszSRFileName, tszFileNameInCab);
  313. DH_HRCHECK_ABORT(hr, TEXT("Cabinet::AddFile"));
  314. iCount++;
  315. }
  316. } while (ERROR_SUCCESS == RPEnum.FindNextRestorePoint(RP));
  317. RPEnum.FindClose();
  318. }
  319. else
  320. {
  321. fprintf(fpLogFile,
  322. "No restore points for Mount Point [%S]\n",
  323. ptszVolume);
  324. }
  325. ErrReturn:
  326. if (NULL != fpLogFile)
  327. {
  328. fclose(fpLogFile);
  329. }
  330. if (NULL != fpRPLog)
  331. {
  332. fclose(fpRPLog);
  333. }
  334. CleanupTStr(&ptszSRGuid);
  335. return hr;
  336. }
  337. //+---------------------------------------------------------------------------
  338. //
  339. // Function: GetSRGuid
  340. //
  341. // Synopsis: Get SR GUID.
  342. //
  343. // Arguments: [pptszSRGuid] -- The returned SR GUID
  344. //
  345. // Returns: HRESULT
  346. //
  347. // History: 02-May-2001
  348. //
  349. //
  350. //----------------------------------------------------------------------------
  351. HRESULT GetSRGuid(LPTSTR* pptszSRGuid)
  352. {
  353. HRESULT hr = S_OK;
  354. long lResult = 0;
  355. DWORD dwType = 0;
  356. DWORD dwLength = MAX_PATH +1;
  357. HKEY hKey = NULL;
  358. TCHAR tszGuidStr[MAX_PATH + 1];
  359. DH_VDATEPTROUT(pptszSRGuid, LPTSTR);
  360. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  361. SR_CONFIG_REG_KEY,
  362. 0,
  363. KEY_READ,
  364. &hKey);
  365. DH_ABORTIF(ERROR_SUCCESS != lResult,
  366. E_FAIL,
  367. TEXT("RegOpenKeyEx"));
  368. lResult = RegQueryValueEx(hKey,
  369. TEXT("MachineGuid"),
  370. NULL,
  371. &dwType,
  372. (unsigned char*) tszGuidStr,
  373. &dwLength);
  374. DH_ABORTIF(ERROR_SUCCESS != lResult,
  375. E_FAIL,
  376. TEXT("RegQueryValueEx"));
  377. hr = CopyString(tszGuidStr, pptszSRGuid);
  378. DH_HRCHECK_ABORT(hr, TEXT("CopyString"));
  379. ErrReturn:
  380. if (NULL != hKey)
  381. {
  382. RegCloseKey(hKey);
  383. }
  384. return hr;
  385. }
  386. //+---------------------------------------------------------------------------
  387. //
  388. // Function: GetDSOnSysVol
  389. //
  390. // Synopsis: Get the name of the data store on the system.
  391. //
  392. // Arguments: [pptszDsOnSys] -- data store name
  393. //
  394. // Returns: HRESULT
  395. //
  396. // History: 02-May-2001
  397. //
  398. //
  399. //----------------------------------------------------------------------------
  400. HRESULT GetDSOnSysVol(LPTSTR* pptszDsOnSys)
  401. {
  402. HRESULT hr = S_OK;
  403. LPTSTR ptszSRGuid = NULL;
  404. TCHAR tszDS[MAX_PATH + 1];
  405. DH_VDATEPTROUT(pptszDsOnSys, LPTSTR);
  406. hr = GetSRGuid(&ptszSRGuid);
  407. DH_HRCHECK_ABORT(hr, TEXT("GetSRGuid"));
  408. _stprintf(tszDS,
  409. TEXT("%s\\System Volume Information\\_Restore%s\\"),
  410. _tgetenv(TEXT("SYSTEMDRIVE")),
  411. ptszSRGuid);
  412. hr = CopyString(tszDS, pptszDsOnSys);
  413. DH_HRCHECK_ABORT(hr, TEXT("CopyString"));
  414. ErrReturn:
  415. CleanupTStr(&ptszSRGuid);
  416. return hr;
  417. }