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.

566 lines
13 KiB

  1. #include "stdafx.hxx"
  2. #include "vs_inc.hxx"
  3. #include "vss.h"
  4. #include "vsevent.h"
  5. #include "vswriter.h"
  6. #include "vsbackup.h"
  7. inline void CHECK_SUCCESS(HRESULT hr)
  8. {
  9. if (hr != S_OK)
  10. {
  11. wprintf(L"operation failed with HRESULT=%08x\n", hr);
  12. DebugBreak();
  13. }
  14. }
  15. inline void CHECK_NOFAIL(HRESULT hr)
  16. {
  17. if (FAILED(hr))
  18. {
  19. wprintf(L"operation failed with HRESULT=%08x\n", hr);
  20. DebugBreak();
  21. }
  22. }
  23. void PrintFiledesc(IVssWMFiledesc *pFiledesc, LPCWSTR wszDescription)
  24. {
  25. CComBSTR bstrPath;
  26. CComBSTR bstrFilespec;
  27. CComBSTR bstrAlternate;
  28. CComBSTR bstrDestination;
  29. bool bRecursive;
  30. CHECK_SUCCESS(pFiledesc->GetPath(&bstrPath));
  31. CHECK_SUCCESS(pFiledesc->GetFilespec(&bstrFilespec));
  32. CHECK_NOFAIL(pFiledesc->GetRecursive(&bRecursive));
  33. CHECK_NOFAIL(pFiledesc->GetAlternateLocation(&bstrAlternate));
  34. CHECK_NOFAIL(pFiledesc->GetAlternateLocation(&bstrDestination));
  35. wprintf
  36. (
  37. L"%s\nPath=%s,Filespec=%s, Recursive=%s\n",
  38. wszDescription,
  39. bstrPath,
  40. bstrFilespec,
  41. bRecursive ? L"yes" : L"no"
  42. );
  43. if (bstrAlternate && wcslen(bstrAlternate) > 0)
  44. wprintf(L"Alternate Location = %s\n", bstrAlternate);
  45. if (bstrDestination && wcslen(bstrDestination) > 0)
  46. wprintf(L"Destination Location = %s\n", bstrDestination);
  47. }
  48. BSTR ProcessWMXML(BSTR bstrXML)
  49. {
  50. CVssFunctionTracer ft(VSSDBG_GEN, L"ProcessWMXML");
  51. CVssExamineWriterMetadata wm;
  52. CVssCreateWriterMetadata cwm;
  53. try
  54. {
  55. wm.Initialize(ft, bstrXML);
  56. }
  57. VSS_STANDARD_CATCH(ft);
  58. CHECK_SUCCESS(ft.hr);
  59. VSS_ID idInstance, idWriter;
  60. CComBSTR bstrWriterName;
  61. VSS_USAGE_TYPE usage;
  62. VSS_SOURCE_TYPE source;
  63. CHECK_SUCCESS(wm.GetIdentity(&idInstance, &idWriter, &bstrWriterName, &usage, &source));
  64. CHECK_SUCCESS
  65. (
  66. cwm.Initialize
  67. (
  68. idInstance,
  69. idWriter,
  70. bstrWriterName,
  71. usage,
  72. source
  73. )
  74. );
  75. WCHAR *pwszInstanceId;
  76. WCHAR *pwszWriterId;
  77. UuidToString(&idInstance, &pwszInstanceId);
  78. UuidToString(&idWriter, &pwszWriterId);
  79. wprintf
  80. (
  81. L"InstanceId=%s\nWriterId=%s\nWriterName=%s\nUsageType=%d\nSourceType=%d\n",
  82. pwszInstanceId,
  83. pwszWriterId,
  84. bstrWriterName,
  85. usage,
  86. source
  87. );
  88. RpcStringFree(&pwszInstanceId);
  89. RpcStringFree(&pwszWriterId);
  90. unsigned cIncludeFiles, cExcludeFiles, cComponents;
  91. CHECK_SUCCESS(wm.GetFileCounts(&cIncludeFiles, &cExcludeFiles, &cComponents));
  92. CComBSTR bstrPath;
  93. CComBSTR bstrFilespec;
  94. CComBSTR bstrAlternate;
  95. CComBSTR bstrDestination;
  96. bool bRecursive;
  97. for(unsigned i = 0; i < cIncludeFiles; i++)
  98. {
  99. CComPtr<IVssWMFiledesc> pFiledesc;
  100. CHECK_SUCCESS(wm.GetIncludeFile(i, &pFiledesc));
  101. CHECK_SUCCESS(pFiledesc->GetPath(&bstrPath));
  102. CHECK_SUCCESS(pFiledesc->GetFilespec(&bstrFilespec));
  103. CHECK_SUCCESS(pFiledesc->GetRecursive(&bRecursive));
  104. CHECK_SUCCESS(pFiledesc->GetAlternateLocation(&bstrAlternate));
  105. CHECK_SUCCESS
  106. (
  107. cwm.AddIncludeFiles
  108. (
  109. bstrPath,
  110. bstrFilespec,
  111. bRecursive,
  112. bstrAlternate
  113. )
  114. );
  115. PrintFiledesc(pFiledesc, L"Include File");
  116. }
  117. for(i = 0; i < cExcludeFiles; i++)
  118. {
  119. CComPtr<IVssWMFiledesc> pFiledesc;
  120. CHECK_SUCCESS(wm.GetExcludeFile(i, &pFiledesc));
  121. CHECK_SUCCESS(pFiledesc->GetPath(&bstrPath));
  122. CHECK_SUCCESS(pFiledesc->GetFilespec(&bstrFilespec));
  123. CHECK_SUCCESS(pFiledesc->GetRecursive(&bRecursive));
  124. CHECK_SUCCESS
  125. (
  126. cwm.AddExcludeFiles
  127. (
  128. bstrPath,
  129. bstrFilespec,
  130. bRecursive
  131. )
  132. );
  133. PrintFiledesc(pFiledesc, L"Exclude File");
  134. }
  135. for(unsigned iComponent = 0; iComponent < cComponents; iComponent++)
  136. {
  137. CComPtr<IVssWMComponent> pComponent;
  138. PVSSCOMPONENTINFO pInfo;
  139. CHECK_SUCCESS(wm.GetComponent(iComponent, &pComponent));
  140. CHECK_SUCCESS(pComponent->GetComponentInfo(&pInfo));
  141. wprintf
  142. (
  143. L"Component %d, type=%d\nLogicalPath=%s,Name=%s\nCaption=%s\n",
  144. i,
  145. pInfo->type,
  146. pInfo->bstrLogicalPath,
  147. pInfo->bstrComponentName,
  148. pInfo->bstrCaption
  149. );
  150. wprintf
  151. (
  152. L"RestoreMetadata=%s,NotifyOnBackupComplete=%s,Selectable=%s\n",
  153. pInfo->bRestoreMetadata ? L"yes" : L"no",
  154. pInfo->bNotifyOnBackupComplete ? L"yes" : L"no",
  155. pInfo->bSelectable ? L"yes" : L"no"
  156. );
  157. CHECK_SUCCESS
  158. (
  159. cwm.AddComponent
  160. (
  161. pInfo->type,
  162. pInfo->bstrLogicalPath,
  163. pInfo->bstrComponentName,
  164. pInfo->bstrCaption,
  165. pInfo->bstrIcon,
  166. pInfo->bRestoreMetadata,
  167. pInfo->bNotifyOnBackupComplete,
  168. pInfo->bSelectable
  169. )
  170. );
  171. if (pInfo->cFileCount > 0)
  172. {
  173. for(i = 0; i < pInfo->cFileCount; i++)
  174. {
  175. CComPtr<IVssWMFiledesc> pFiledesc;
  176. CHECK_SUCCESS(pComponent->GetFile(i, &pFiledesc));
  177. CHECK_SUCCESS(pFiledesc->GetPath(&bstrPath));
  178. CHECK_SUCCESS(pFiledesc->GetFilespec(&bstrFilespec));
  179. CHECK_SUCCESS(pFiledesc->GetRecursive(&bRecursive));
  180. CHECK_SUCCESS
  181. (
  182. cwm.AddFilesToFileGroup
  183. (
  184. pInfo->bstrLogicalPath,
  185. pInfo->bstrComponentName,
  186. bstrPath,
  187. bstrFilespec,
  188. bRecursive
  189. )
  190. );
  191. PrintFiledesc(pFiledesc, L"FileGroupFile");
  192. }
  193. }
  194. if (pInfo->cDatabases > 0)
  195. {
  196. for(i = 0; i < pInfo->cDatabases; i++)
  197. {
  198. CComPtr<IVssWMFiledesc> pFiledesc;
  199. CHECK_SUCCESS(pComponent->GetDatabaseFile(i, &pFiledesc));
  200. CHECK_SUCCESS(pFiledesc->GetPath(&bstrPath));
  201. CHECK_SUCCESS(pFiledesc->GetFilespec(&bstrFilespec));
  202. CHECK_SUCCESS
  203. (
  204. cwm.AddDatabaseFiles
  205. (
  206. pInfo->bstrLogicalPath,
  207. pInfo->bstrComponentName,
  208. bstrPath,
  209. bstrFilespec
  210. )
  211. );
  212. PrintFiledesc(pFiledesc, L"DatabaseFile");
  213. }
  214. }
  215. if (pInfo->cLogFiles > 0)
  216. {
  217. for(i = 0; i < pInfo->cLogFiles; i++)
  218. {
  219. CComPtr<IVssWMFiledesc> pFiledesc;
  220. CHECK_SUCCESS(pComponent->GetDatabaseLogFile(i, &pFiledesc));
  221. CHECK_SUCCESS(pFiledesc->GetPath(&bstrPath));
  222. CHECK_SUCCESS(pFiledesc->GetFilespec(&bstrFilespec));
  223. CHECK_SUCCESS
  224. (
  225. cwm.AddDatabaseLogFiles
  226. (
  227. pInfo->bstrLogicalPath,
  228. pInfo->bstrComponentName,
  229. bstrPath,
  230. bstrFilespec
  231. )
  232. );
  233. PrintFiledesc(pFiledesc, L"DatabaseLogFile");
  234. }
  235. }
  236. pComponent->FreeComponentInfo(pInfo);
  237. }
  238. VSS_RESTOREMETHOD_ENUM method;
  239. VSS_WRITERRESTORE_ENUM writerRestore;
  240. CComBSTR bstrUserProcedure;
  241. CComBSTR bstrService;
  242. unsigned cMappings;
  243. CHECK_SUCCESS
  244. (
  245. wm.GetRestoreMethod
  246. (
  247. &method,
  248. &bstrService,
  249. &bstrUserProcedure,
  250. &writerRestore,
  251. &cMappings
  252. )
  253. );
  254. CHECK_SUCCESS
  255. (
  256. cwm.SetRestoreMethod
  257. (
  258. method,
  259. bstrService,
  260. bstrUserProcedure,
  261. writerRestore
  262. )
  263. );
  264. wprintf
  265. (
  266. L"Restore method=%d\nService=%s\nUser Procedure=%s\nwriterRestore=%d\n",
  267. method,
  268. bstrService,
  269. bstrUserProcedure,
  270. writerRestore
  271. );
  272. for(i = 0; i < cMappings; i++)
  273. {
  274. CComPtr<IVssWMFiledesc> pFiledesc;
  275. CHECK_SUCCESS(wm.GetAlternateLocationMapping(i, &pFiledesc));
  276. CHECK_SUCCESS(pFiledesc->GetPath(&bstrPath));
  277. CHECK_SUCCESS(pFiledesc->GetFilespec(&bstrFilespec));
  278. CHECK_SUCCESS(pFiledesc->GetRecursive(&bRecursive));
  279. CHECK_SUCCESS(pFiledesc->GetAlternateLocation(&bstrDestination));
  280. CHECK_SUCCESS
  281. (
  282. cwm.AddAlternateLocationMapping
  283. (
  284. bstrPath,
  285. bstrFilespec,
  286. bRecursive,
  287. bstrDestination
  288. )
  289. );
  290. PrintFiledesc(pFiledesc, L"AlternateMapping");
  291. }
  292. CHECK_SUCCESS(cwm.SaveAsXML(&bstrXML));
  293. return bstrXML;
  294. }
  295. void SetBackupMetadata
  296. (
  297. IVssBackupComponents *pBackup,
  298. VSS_ID idInstance,
  299. VSS_ID idWriter,
  300. VSS_COMPONENT_TYPE ct,
  301. BSTR bstrLogicalPath,
  302. BSTR bstrComponentName,
  303. LPCWSTR wszMetadata
  304. )
  305. {
  306. unsigned cWriters;
  307. CHECK_SUCCESS(pBackup->GetWriterComponentsCount(&cWriters));
  308. for(unsigned iWriter = 0; iWriter < cWriters; iWriter++)
  309. {
  310. CComPtr<IVssWriterComponentsExt> pWriter;
  311. CHECK_SUCCESS(pBackup->GetWriterComponents(iWriter, &pWriter));
  312. VSS_ID idWriterT, idInstanceT;
  313. CHECK_SUCCESS(pWriter->GetWriterInfo(&idInstanceT, &idWriterT));
  314. if (memcmp(&idInstance, &idInstanceT, sizeof(GUID)) == 0 &&
  315. memcmp(&idWriter, &idWriterT, sizeof(GUID)) == 0)
  316. {
  317. unsigned cComponents;
  318. CHECK_SUCCESS(pWriter->GetComponentCount(&cComponents));
  319. for(unsigned iComponent = 0; iComponent < cComponents; iComponent++)
  320. {
  321. IVssComponent *pComponent;
  322. CHECK_SUCCESS(pWriter->GetComponent(iComponent, &pComponent));
  323. VSS_COMPONENT_TYPE ctT;
  324. CComBSTR bstrLogicalPathT;
  325. CComBSTR bstrComponentNameT;
  326. CHECK_NOFAIL(pComponent->GetLogicalPath(&bstrLogicalPathT));
  327. CHECK_SUCCESS(pComponent->GetComponentType(&ctT));
  328. CHECK_SUCCESS(pComponent->GetComponentName(&bstrComponentNameT));
  329. if (ct == ctT &&
  330. ((bstrLogicalPath == NULL && bstrLogicalPathT.Length() == 0) ||
  331. (bstrLogicalPath != NULL &&
  332. bstrLogicalPathT.Length() != 0 &&
  333. wcscmp(bstrLogicalPath, bstrLogicalPathT) == 0)) &&
  334. wcscmp(bstrComponentName, bstrComponentNameT) == 0)
  335. {
  336. CHECK_SUCCESS(pComponent->SetBackupMetadata(wszMetadata));
  337. break;
  338. }
  339. }
  340. }
  341. }
  342. }
  343. BSTR ProcessCMXML(BSTR bstrXML)
  344. {
  345. CComPtr<IVssBackupComponents> pvbc;
  346. CComPtr<IVssBackupComponents> pvbcCreated;
  347. CHECK_SUCCESS(CreateVssBackupComponents(&pvbc));
  348. CHECK_SUCCESS(CreateVssBackupComponents(&pvbcCreated));
  349. BS_ASSERT(pvbc);
  350. BS_ASSERT(pvbcCreated);
  351. CHECK_SUCCESS(pvbc->LoadFromXML(bstrXML));
  352. CHECK_SUCCESS(pvbcCreated->Initialize(true, true));
  353. unsigned cWriters;
  354. CHECK_SUCCESS(pvbc->GetWriterComponentsCount(&cWriters));
  355. for(unsigned iWriter = 0; iWriter < cWriters; iWriter++)
  356. {
  357. CComPtr<IVssWriterComponentsExt> pWriter;
  358. CHECK_SUCCESS(pvbc->GetWriterComponents(iWriter, &pWriter));
  359. VSS_ID idWriter, idInstance;
  360. CHECK_SUCCESS(pWriter->GetWriterInfo(&idInstance, &idWriter));
  361. unsigned cComponents;
  362. CHECK_SUCCESS(pWriter->GetComponentCount(&cComponents));
  363. for(unsigned iComponent = 0; iComponent < cComponents; iComponent++)
  364. {
  365. IVssComponent *pComponent;
  366. CHECK_SUCCESS(pWriter->GetComponent(iComponent, &pComponent));
  367. VSS_COMPONENT_TYPE ct;
  368. CComBSTR bstrLogicalPath;
  369. CComBSTR bstrComponentName;
  370. bool bBackupSucceeded;
  371. CHECK_NOFAIL(pComponent->GetLogicalPath(&bstrLogicalPath));
  372. CHECK_SUCCESS(pComponent->GetComponentType(&ct));
  373. CHECK_SUCCESS(pComponent->GetComponentName(&bstrComponentName));
  374. CHECK_SUCCESS(pComponent->GetBackupSucceeded(&bBackupSucceeded));
  375. CHECK_SUCCESS
  376. (
  377. pvbcCreated->AddComponent
  378. (
  379. idInstance,
  380. idWriter,
  381. ct,
  382. bstrLogicalPath,
  383. bstrComponentName
  384. )
  385. );
  386. CHECK_SUCCESS
  387. (
  388. pvbcCreated->SetBackupSucceeded
  389. (
  390. idInstance,
  391. idWriter,
  392. ct,
  393. bstrLogicalPath,
  394. bstrComponentName,
  395. bBackupSucceeded
  396. )
  397. );
  398. CComBSTR bstrMetadata;
  399. CHECK_SUCCESS(pComponent->GetBackupMetadata(&bstrMetadata));
  400. SetBackupMetadata
  401. (
  402. pvbcCreated,
  403. idInstance,
  404. idWriter,
  405. ct,
  406. bstrLogicalPath,
  407. bstrComponentName,
  408. bstrMetadata
  409. );
  410. unsigned cMappings;
  411. CHECK_SUCCESS(pComponent->GetAlternateLocationMappingCount(&cMappings));
  412. for(unsigned iMapping = 0; iMapping < cMappings; iMapping++)
  413. {
  414. CComPtr<IVssWMFiledesc> pFiledesc;
  415. CHECK_SUCCESS(pComponent->GetAlternateLocationMapping(iMapping, &pFiledesc));
  416. CComBSTR bstrPath;
  417. CComBSTR bstrFilespec;
  418. CComBSTR bstrDestination;
  419. bool bRecursive;
  420. CHECK_SUCCESS(pFiledesc->GetPath(&bstrPath));
  421. CHECK_SUCCESS(pFiledesc->GetFilespec(&bstrFilespec));
  422. CHECK_SUCCESS(pFiledesc->GetAlternateLocation(&bstrDestination));
  423. CHECK_SUCCESS(pFiledesc->GetRecursive(&bRecursive));
  424. CHECK_SUCCESS
  425. (
  426. pvbcCreated->AddAlternativeLocationMapping
  427. (
  428. idWriter,
  429. ct,
  430. bstrLogicalPath,
  431. bstrComponentName,
  432. bstrPath,
  433. bstrFilespec,
  434. bRecursive,
  435. bstrDestination
  436. )
  437. );
  438. }
  439. }
  440. }
  441. BSTR bstrRet;
  442. CHECK_SUCCESS(pvbcCreated->SaveAsXML(&bstrRet));
  443. return bstrRet;
  444. }
  445. extern "C" __cdecl wmain(int argc, WCHAR **argv)
  446. {
  447. CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  448. if (argc != 3)
  449. {
  450. wprintf(L"testxml writer-metadata-file component-file");
  451. exit(-1);
  452. }
  453. CVssFunctionTracer ft(VSSDBG_GEN, L"main");
  454. try
  455. {
  456. CXMLDocument doc;
  457. CComBSTR bstrXML;
  458. CComBSTR bstrXMLOut;
  459. if (!doc.LoadFromFile(ft, argv[1]))
  460. {
  461. wprintf(L"couldn't load xml document %s", argv[1]);
  462. exit(-1);
  463. }
  464. bstrXML = doc.SaveAsXML(ft);
  465. bstrXMLOut = ProcessWMXML(bstrXML);
  466. bstrXML = ProcessWMXML(bstrXMLOut);
  467. wprintf(L"\n\n%s\n\n%s\n", bstrXMLOut, bstrXML);
  468. if (wcscmp(bstrXML, bstrXMLOut) == 0)
  469. wprintf(L"\n\nSUCCESS\n");
  470. else
  471. wprintf(L"\n\nFAILURE\n");
  472. if (!doc.LoadFromFile(ft, argv[2]))
  473. {
  474. wprintf(L"couldn't load xml document %s", argv[1]);
  475. exit(-1);
  476. }
  477. bstrXML = doc.SaveAsXML(ft);
  478. bstrXMLOut = ProcessCMXML(bstrXML);
  479. bstrXML = ProcessCMXML(bstrXML);
  480. wprintf(L"\n\n%s\n\n%s\n", bstrXMLOut, bstrXML);
  481. if (wcscmp(bstrXML, bstrXMLOut) == 0)
  482. wprintf(L"\n\nSUCCESS\n");
  483. else
  484. wprintf(L"\n\nFAILURE\n");
  485. }
  486. VSS_STANDARD_CATCH(ft)
  487. if (FAILED(ft.hr))
  488. wprintf(L"Failed with %08x.\n", ft.hr);
  489. return(0);
  490. }