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.

562 lines
13 KiB

  1. /*
  2. **++
  3. **
  4. ** Copyright (c) 2000-2001 Microsoft Corporation
  5. **
  6. **
  7. ** Module Name:
  8. **
  9. ** metasnap.cpp
  10. **
  11. **
  12. ** Abstract:
  13. **
  14. ** Test program to obtain and display the Writer metadata.
  15. **
  16. **
  17. ** Author:
  18. **
  19. ** Michael C. Johnson [mikejohn] 21-Jul-2000
  20. **
  21. ** (Based on another test program BETEST by Brian Berkowitz)
  22. **
  23. **
  24. ** Revision History:
  25. **
  26. **--
  27. */
  28. /*
  29. ** Defines
  30. **
  31. **
  32. ** C4290: C++ Exception Specification ignored
  33. ** warning C4511: 'CVssCOMApplication' : copy constructor could not be generated
  34. ** warning C4127: conditional expression is constant
  35. */
  36. #pragma warning(disable:4290)
  37. #pragma warning(disable:4511)
  38. #pragma warning(disable:4127)
  39. /*
  40. ** Includes
  41. */
  42. #include <windows.h>
  43. #include <wtypes.h>
  44. #include <stddef.h>
  45. #include <stdlib.h>
  46. #include <stdio.h>
  47. #include <time.h>
  48. #include <vss.h>
  49. #include <vswriter.h>
  50. #include <vsbackup.h>
  51. #include <oleauto.h>
  52. #define ATLASSERT(_condition)
  53. #include <atlconv.h>
  54. #include <atlbase.h>
  55. extern CComModule _Module;
  56. #include <atlcom.h>
  57. inline void CHECK_SUCCESS (HRESULT hr)
  58. {
  59. if (hr != S_OK)
  60. {
  61. wprintf(L"operation failed with HRESULT =0x%08x\n", hr);
  62. DebugBreak();
  63. }
  64. }
  65. inline void CHECK_NOFAIL (HRESULT hr)
  66. {
  67. if (FAILED(hr))
  68. {
  69. wprintf(L"operation failed with HRESULT =0x%08x\n", hr);
  70. DebugBreak();
  71. }
  72. }
  73. BOOL AssertPrivilege( LPCWSTR privName )
  74. {
  75. HANDLE tokenHandle;
  76. TOKEN_PRIVILEGES newState;
  77. TOKEN_PRIVILEGES *pTokens = NULL;
  78. BOOL stat = FALSE;
  79. LUID value;
  80. DWORD error;
  81. DWORD cbTokens;
  82. if (OpenProcessToken (GetCurrentProcess(),
  83. TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,
  84. &tokenHandle))
  85. {
  86. if (LookupPrivilegeValue (NULL, privName, &value))
  87. {
  88. newState.PrivilegeCount = 1;
  89. newState.Privileges [0].Luid = value;
  90. newState.Privileges [0].Attributes = SE_PRIVILEGE_ENABLED_BY_DEFAULT|SE_PRIVILEGE_ENABLED;
  91. /*
  92. ** We will always call GetLastError below, so clear
  93. ** any prior error values on this thread.
  94. */
  95. SetLastError (ERROR_SUCCESS);
  96. stat = AdjustTokenPrivileges (tokenHandle,
  97. FALSE,
  98. &newState,
  99. (DWORD)0,
  100. NULL,
  101. NULL);
  102. /*
  103. ** Supposedly, AdjustTokenPriveleges always returns TRUE
  104. ** (even when it fails). So, call GetLastError to be
  105. ** extra sure everything's cool.
  106. */
  107. if ((error = GetLastError()) != ERROR_SUCCESS)
  108. {
  109. stat = FALSE;
  110. }
  111. if (!stat)
  112. {
  113. wprintf (L"AdjustTokenPrivileges for %s failed with %d",
  114. privName,
  115. error);
  116. }
  117. }
  118. GetTokenInformation (tokenHandle,
  119. TokenPrivileges,
  120. NULL,
  121. 0,
  122. &cbTokens);
  123. pTokens = (TOKEN_PRIVILEGES *) new BYTE[cbTokens];
  124. GetTokenInformation (tokenHandle,
  125. TokenPrivileges,
  126. pTokens,
  127. cbTokens,
  128. &cbTokens);
  129. delete pTokens;
  130. CloseHandle (tokenHandle);
  131. }
  132. return stat;
  133. }
  134. LPCWSTR GetStringFromUsageType (VSS_USAGE_TYPE eUsageType)
  135. {
  136. LPCWSTR pwszRetString = L"UNDEFINED";
  137. switch (eUsageType)
  138. {
  139. case VSS_UT_BOOTABLESYSTEMSTATE: pwszRetString = L"BootableSystemState"; break;
  140. case VSS_UT_SYSTEMSERVICE: pwszRetString = L"SystemService"; break;
  141. case VSS_UT_USERDATA: pwszRetString = L"UserData"; break;
  142. case VSS_UT_OTHER: pwszRetString = L"Other"; break;
  143. default:
  144. break;
  145. }
  146. return (pwszRetString);
  147. }
  148. LPCWSTR GetStringFromSourceType (VSS_SOURCE_TYPE eSourceType)
  149. {
  150. LPCWSTR pwszRetString = L"UNDEFINED";
  151. switch (eSourceType)
  152. {
  153. case VSS_ST_TRANSACTEDDB: pwszRetString = L"TransactionDb"; break;
  154. case VSS_ST_NONTRANSACTEDDB: pwszRetString = L"NonTransactionDb"; break;
  155. case VSS_ST_OTHER: pwszRetString = L"Other"; break;
  156. default:
  157. break;
  158. }
  159. return (pwszRetString);
  160. }
  161. LPCWSTR GetStringFromRestoreMethod (VSS_RESTOREMETHOD_ENUM eRestoreMethod)
  162. {
  163. LPCWSTR pwszRetString = L"UNDEFINED";
  164. switch (eRestoreMethod)
  165. {
  166. case VSS_RME_RESTORE_IF_NOT_THERE: pwszRetString = L"RestoreIfNotThere"; break;
  167. case VSS_RME_RESTORE_IF_CAN_REPLACE: pwszRetString = L"RestoreIfCanReplace"; break;
  168. case VSS_RME_STOP_RESTORE_START: pwszRetString = L"StopRestoreStart"; break;
  169. case VSS_RME_RESTORE_TO_ALTERNATE_LOCATION: pwszRetString = L"RestoreToAlternateLocation"; break;
  170. case VSS_RME_RESTORE_AT_REBOOT: pwszRetString = L"RestoreAtReboot"; break;
  171. case VSS_RME_CUSTOM: pwszRetString = L"Custom"; break;
  172. default:
  173. break;
  174. }
  175. return (pwszRetString);
  176. }
  177. LPCWSTR GetStringFromWriterRestoreMethod (VSS_WRITERRESTORE_ENUM eWriterRestoreMethod)
  178. {
  179. LPCWSTR pwszRetString = L"UNDEFINED";
  180. switch (eWriterRestoreMethod)
  181. {
  182. case VSS_WRE_NEVER: pwszRetString = L"RestoreNever"; break;
  183. case VSS_WRE_IF_REPLACE_FAILS: pwszRetString = L"RestoreIfReplaceFailsI"; break;
  184. case VSS_WRE_ALWAYS: pwszRetString = L"RestoreAlways"; break;
  185. default:
  186. break;
  187. }
  188. return (pwszRetString);
  189. }
  190. LPCWSTR GetStringFromComponentType (VSS_COMPONENT_TYPE eComponentType)
  191. {
  192. LPCWSTR pwszRetString = L"UNDEFINED";
  193. switch (eComponentType)
  194. {
  195. case VSS_CT_DATABASE: pwszRetString = L"Database"; break;
  196. case VSS_CT_FILEGROUP: pwszRetString = L"FileGroup"; break;
  197. default:
  198. break;
  199. }
  200. return (pwszRetString);
  201. }
  202. LPCWSTR GetStringFromFailureType (HRESULT hrStatus)
  203. {
  204. LPCWSTR pwszFailureType;
  205. switch (hrStatus)
  206. {
  207. case NOERROR: pwszFailureType = L""; break;
  208. case VSS_E_WRITERERROR_INCONSISTENTSNAPSHOT: pwszFailureType = L"InconsistentSnapshot"; break;
  209. case VSS_E_WRITERERROR_OUTOFRESOURCES: pwszFailureType = L"OutOfResources"; break;
  210. case VSS_E_WRITERERROR_TIMEOUT: pwszFailureType = L"Timeout"; break;
  211. case VSS_E_WRITERERROR_NONRETRYABLE: pwszFailureType = L"Non-Retryable"; break;
  212. case VSS_E_WRITERERROR_RETRYABLE: pwszFailureType = L"Retryable"; break;
  213. default: pwszFailureType = L"UNDEFINED"; break;
  214. }
  215. return (pwszFailureType);
  216. }
  217. void PrintFiledesc (IVssWMFiledesc *pFiledesc, LPCWSTR wszDescription)
  218. {
  219. CComBSTR bstrPath;
  220. CComBSTR bstrFilespec;
  221. CComBSTR bstrAlternate;
  222. bool bRecursive;
  223. CHECK_SUCCESS (pFiledesc->GetPath (&bstrPath));
  224. CHECK_SUCCESS (pFiledesc->GetFilespec (&bstrFilespec));
  225. CHECK_NOFAIL (pFiledesc->GetRecursive (&bRecursive));
  226. CHECK_NOFAIL (pFiledesc->GetAlternateLocation (&bstrAlternate));
  227. wprintf (L"%s\n Path = %s, Filespec = %s, Recursive = %s\n",
  228. wszDescription,
  229. bstrPath,
  230. bstrFilespec,
  231. bRecursive ? L"yes" : L"no");
  232. if (bstrAlternate && wcslen (bstrAlternate) > 0)
  233. {
  234. wprintf(L" Alternate Location = %s\n", bstrAlternate);
  235. }
  236. }
  237. extern "C" __cdecl wmain(int argc, WCHAR **argv)
  238. {
  239. HRESULT hr = NOERROR;
  240. bool bCoInitializeSucceeded = false;
  241. UNREFERENCED_PARAMETER (argc);
  242. UNREFERENCED_PARAMETER (argv);
  243. try
  244. {
  245. unsigned cWriters;
  246. CComBSTR bstrXML;
  247. CComBSTR bstrXMLOut;
  248. CComBSTR strSnapshotSetId = "12345678-1234-1234-1234-1234567890ab";
  249. CComPtr<IVssBackupComponents> pvbc;
  250. CComPtr<IVssAsync> pAsync;
  251. CHECK_SUCCESS (CoInitializeEx (NULL, COINIT_MULTITHREADED));
  252. bCoInitializeSucceeded = true;
  253. if (!AssertPrivilege (SE_BACKUP_NAME))
  254. {
  255. wprintf(L"AssertPrivilege returned error, rc:%d\n", GetLastError ());
  256. return 2;
  257. }
  258. CHECK_SUCCESS (CreateVssBackupComponents (&pvbc));
  259. CHECK_SUCCESS (pvbc->InitializeForBackup ());
  260. CHECK_SUCCESS (pvbc->SetBackupState (true, false, VSS_BT_FULL));
  261. CHECK_NOFAIL (pvbc->GatherWriterMetadata (&pAsync));
  262. CHECK_NOFAIL (pAsync->Wait ());
  263. CHECK_NOFAIL (pvbc->GetWriterMetadataCount (&cWriters));
  264. for (unsigned iWriter = 0; iWriter < cWriters; iWriter++)
  265. {
  266. CComPtr<IVssExamineWriterMetadata> pMetadata;
  267. VSS_ID idInstance;
  268. VSS_ID idInstanceT;
  269. VSS_ID idWriter;
  270. CComBSTR bstrWriterName;
  271. VSS_USAGE_TYPE usage;
  272. VSS_SOURCE_TYPE source;
  273. WCHAR *pwszInstanceId;
  274. WCHAR *pwszWriterId;
  275. unsigned cIncludeFiles, cExcludeFiles, cComponents;
  276. CComBSTR bstrPath;
  277. CComBSTR bstrFilespec;
  278. CComBSTR bstrAlternate;
  279. CComBSTR bstrDestination;
  280. CHECK_SUCCESS (pvbc->GetWriterMetadata(iWriter, &idInstance, &pMetadata));
  281. CHECK_SUCCESS (pMetadata->GetIdentity (&idInstanceT,
  282. &idWriter,
  283. &bstrWriterName,
  284. &usage,
  285. &source));
  286. wprintf (L"\n\n");
  287. if (memcmp (&idInstance, &idInstanceT, sizeof(VSS_ID)) != 0)
  288. {
  289. wprintf(L"Instance id mismatch\n");
  290. DebugBreak();
  291. }
  292. UuidToString (&idInstance, &pwszInstanceId);
  293. UuidToString (&idWriter, &pwszWriterId);
  294. wprintf (L"WriterName = %s\n\n"
  295. L" WriterId = %s\n"
  296. L" InstanceId = %s\n"
  297. L" UsageType = %d (%s)\n"
  298. L" SourceType = %d (%s)\n",
  299. bstrWriterName,
  300. pwszWriterId,
  301. pwszInstanceId,
  302. usage,
  303. GetStringFromUsageType (usage),
  304. source,
  305. GetStringFromSourceType (source));
  306. RpcStringFree (&pwszInstanceId);
  307. RpcStringFree (&pwszWriterId);
  308. CHECK_SUCCESS(pMetadata->GetFileCounts (&cIncludeFiles,
  309. &cExcludeFiles,
  310. &cComponents));
  311. for(unsigned i = 0; i < cIncludeFiles; i++)
  312. {
  313. CComPtr<IVssWMFiledesc> pFiledesc;
  314. CHECK_SUCCESS (pMetadata->GetIncludeFile (i, &pFiledesc));
  315. PrintFiledesc(pFiledesc, L"\n Include File");
  316. }
  317. for(i = 0; i < cExcludeFiles; i++)
  318. {
  319. CComPtr<IVssWMFiledesc> pFiledesc;
  320. CHECK_SUCCESS (pMetadata->GetExcludeFile (i, &pFiledesc));
  321. PrintFiledesc (pFiledesc, L"\n Exclude File");
  322. }
  323. for(unsigned iComponent = 0; iComponent < cComponents; iComponent++)
  324. {
  325. CComPtr<IVssWMComponent> pComponent;
  326. PVSSCOMPONENTINFO pInfo;
  327. CHECK_SUCCESS (pMetadata->GetComponent (iComponent, &pComponent));
  328. CHECK_SUCCESS (pComponent->GetComponentInfo (&pInfo));
  329. wprintf (L"\n"
  330. L" Component %d, type = %d (%s)\n"
  331. L" LogicalPath = %s\n"
  332. L" Name = %s\n"
  333. L" Caption = %s\n"
  334. L" Icon size = %u\n",
  335. iComponent,
  336. pInfo->type,
  337. GetStringFromComponentType (pInfo->type),
  338. pInfo->bstrLogicalPath,
  339. pInfo->bstrComponentName,
  340. pInfo->bstrCaption,
  341. pInfo->cbIcon );
  342. wprintf (L" RestoreMetadata = %s\n"
  343. L" NotifyOnBackupComplete = %s\n"
  344. L" Selectable = %s\n",
  345. pInfo->bRestoreMetadata ? L"yes" : L"no",
  346. pInfo->bNotifyOnBackupComplete ? L"yes" : L"no",
  347. pInfo->bSelectable ? L"yes" : L"no");
  348. if (pInfo->cFileCount > 0)
  349. {
  350. for(i = 0; i < pInfo->cFileCount; i++)
  351. {
  352. CComPtr<IVssWMFiledesc> pFiledesc;
  353. CHECK_SUCCESS (pComponent->GetFile (i, &pFiledesc));
  354. PrintFiledesc (pFiledesc, L" FileGroupFile");
  355. }
  356. }
  357. if (pInfo->cDatabases > 0)
  358. {
  359. for(i = 0; i < pInfo->cDatabases; i++)
  360. {
  361. CComPtr<IVssWMFiledesc> pFiledesc;
  362. CHECK_SUCCESS (pComponent->GetDatabaseFile (i, &pFiledesc));
  363. PrintFiledesc (pFiledesc, L" DatabaseFile");
  364. }
  365. }
  366. if (pInfo->cLogFiles > 0)
  367. {
  368. for(i = 0; i < pInfo->cLogFiles; i++)
  369. {
  370. CComPtr<IVssWMFiledesc> pFiledesc;
  371. CHECK_SUCCESS (pComponent->GetDatabaseLogFile (i, &pFiledesc));
  372. PrintFiledesc (pFiledesc, L" DatabaseLogFile");
  373. }
  374. }
  375. pComponent->FreeComponentInfo (pInfo);
  376. }
  377. VSS_RESTOREMETHOD_ENUM method;
  378. CComBSTR bstrUserProcedure;
  379. CComBSTR bstrService;
  380. VSS_WRITERRESTORE_ENUM writerRestore;
  381. unsigned cMappings;
  382. bool bRebootRequired;
  383. CHECK_NOFAIL (pMetadata->GetRestoreMethod (&method,
  384. &bstrService,
  385. &bstrUserProcedure,
  386. &writerRestore,
  387. &bRebootRequired,
  388. &cMappings));
  389. wprintf (L"\n"
  390. L" Restore method = %d (%s)\n"
  391. L" Service = %d\n"
  392. L" User Procedure = %s\n"
  393. L" WriterRestore = %d (%s)\n"
  394. L" RebootRequired = %s\n",
  395. method,
  396. GetStringFromRestoreMethod (method),
  397. bstrService,
  398. bstrUserProcedure,
  399. writerRestore,
  400. GetStringFromWriterRestoreMethod (writerRestore),
  401. bRebootRequired ? L"yes" : L"no");
  402. for(i = 0; i < cMappings; i++)
  403. {
  404. CComPtr<IVssWMFiledesc> pFiledesc;
  405. CHECK_SUCCESS (pMetadata->GetAlternateLocationMapping (i, &pFiledesc));
  406. PrintFiledesc (pFiledesc, L" AlternateMapping");
  407. }
  408. }
  409. CHECK_SUCCESS (pvbc->FreeWriterMetadata());
  410. }
  411. catch(...)
  412. {
  413. hr = E_UNEXPECTED;
  414. }
  415. if (FAILED(hr)) wprintf (L"Failed with 0x%08X.\n", hr);
  416. if (bCoInitializeSucceeded) CoUninitialize();
  417. return (0);
  418. }