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.

485 lines
10 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. writer.cpp
  5. Abstract:
  6. main module of test writer
  7. Brian Berkowitz [brianb] 06/02/2000
  8. TBD:
  9. Revision History:
  10. Name Date Comments
  11. brianb 06/02/2000 Created
  12. --*/
  13. #include <stdafx.h>
  14. #include <vststmsgclient.hxx>
  15. #include <tstiniconfig.hxx>
  16. #include <vststprocess.hxx>
  17. #include <vss.h>
  18. #include <vswriter.h>
  19. #include <writer.h>
  20. void LogUnexpectedFailure(LPCWSTR wsz, ...);
  21. #define IID_PPV_ARG( Type, Expr ) IID_##Type, reinterpret_cast< void** >( static_cast< Type** >( Expr ) )
  22. #define SafeQI( Type, Expr ) QueryInterface( IID_PPV_ARG( Type, Expr ) )
  23. static BYTE x_rgbIcon[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  24. static unsigned x_cbIcon = 10;
  25. static VSS_ID s_WRITERID =
  26. {
  27. 0xc0577ae6, 0xd741, 0x452a,
  28. 0x8c, 0xba, 0x99, 0xd7, 0x44, 0x00, 0x8c, 0x04
  29. };
  30. static LPCWSTR s_WRITERNAME = L"Test Writer";
  31. HRESULT CVsWriterTest::RunTest
  32. (
  33. CVsTstINIConfig *pConfig,
  34. CVsTstClientMsg *pClient,
  35. CVsTstParams *pParams
  36. )
  37. {
  38. try
  39. {
  40. m_pConfig = pConfig;
  41. m_pParams = pParams;
  42. SetClientMsg(pClient);
  43. HANDLE hShutdownEvent;
  44. UINT lifetime;
  45. if (!m_pParams->GetTerminationEvent(&hShutdownEvent))
  46. {
  47. LogFailure("NoShutdownEvent");
  48. throw E_FAIL;
  49. }
  50. if (!m_pParams->GetLifetime(&lifetime) || lifetime > 30 * 24 * 3600)
  51. lifetime = INFINITE;
  52. else
  53. lifetime = lifetime * 1000;
  54. if (!Initialize())
  55. {
  56. LogFailure("CVsWriterTest::Initialize failed");
  57. throw E_FAIL;
  58. }
  59. HRESULT hr = Subscribe();
  60. ValidateResult(hr, "CVssWriter::Subscribe");
  61. DWORD dwResult = WaitForSingleObject(hShutdownEvent, lifetime);
  62. Unsubscribe();
  63. UNREFERENCED_PARAMETER( dwResult );
  64. }
  65. catch(HRESULT hr)
  66. {
  67. return hr;
  68. }
  69. catch(...)
  70. {
  71. LogUnexpectedException("CVsWriterTest::RunTest");
  72. return E_UNEXPECTED;
  73. }
  74. return S_OK;
  75. }
  76. extern "C" __cdecl wmain(int argc, WCHAR **argv)
  77. {
  78. bool bCoinitializeSucceeded = false;
  79. CVsWriterTest *pTest = NULL;
  80. try
  81. {
  82. HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  83. if (FAILED(hr))
  84. {
  85. LogUnexpectedFailure(L"CoInitializeEx failed. hr=0x%08lx", hr);
  86. throw E_UNEXPECTED;
  87. }
  88. bCoinitializeSucceeded = true;
  89. pTest = new CVsWriterTest;
  90. if (pTest == NULL)
  91. {
  92. LogUnexpectedFailure(L"Cannot create test writer");
  93. throw E_OUTOFMEMORY;
  94. }
  95. hr = CVsTstRunner::RunVsTest(argv, argc, pTest, false);
  96. if (FAILED(hr))
  97. {
  98. LogUnexpectedFailure(L"CVsTstRunner::RunVsTest failed. hr = 0x%08lx", hr);
  99. throw hr;
  100. }
  101. }
  102. catch(...)
  103. {
  104. }
  105. delete pTest;
  106. if (bCoinitializeSucceeded)
  107. CoUninitialize();
  108. return 0;
  109. }
  110. void PrintMessage(VSTST_MSG_HDR *phdr)
  111. {
  112. VSTST_ASSERT(phdr->type == VSTST_MT_TEXT || phdr->type == VSTST_MT_IMMEDIATETEXT);
  113. VSTST_TEXTMSG *pmsg = (VSTST_TEXTMSG *) phdr->rgb;
  114. printf("%d: %s", (UINT) phdr->sequence, pmsg->pch);
  115. }
  116. void LogUnexpectedFailure(LPCWSTR wsz, ...)
  117. {
  118. va_list args;
  119. va_start(args, wsz);
  120. VSTST_ASSERT(FALSE);
  121. wprintf(L"\n!!!UNEXPECTED FAILURE!!!\n");
  122. vwprintf(wsz, args);
  123. wprintf(L"\n");
  124. }
  125. bool CVsWriterTest::Initialize()
  126. {
  127. try
  128. {
  129. HRESULT hr = CVssWriter::Initialize
  130. (
  131. s_WRITERID,
  132. s_WRITERNAME,
  133. VSS_UT_USERDATA,
  134. VSS_ST_OTHER
  135. );
  136. ValidateResult(hr, "CVssWriter::Initialize");
  137. }
  138. catch(HRESULT)
  139. {
  140. return false;
  141. }
  142. catch(...)
  143. {
  144. LogUnexpectedException("CVsWriterTest::Initialize");
  145. return false;
  146. }
  147. return true;
  148. }
  149. bool STDMETHODCALLTYPE CVsWriterTest::OnIdentify(IN IVssCreateWriterMetadata *pMetadata)
  150. {
  151. try
  152. {
  153. HRESULT hr = pMetadata->AddIncludeFiles
  154. (
  155. L"%systemroot%\\config",
  156. L"mytestfiles.*",
  157. false,
  158. NULL
  159. );
  160. ValidateResult(hr, "IVssCreateWriterMetadata::AddIncludeFiles");
  161. hr = pMetadata->AddExcludeFiles
  162. (
  163. L"%systemroot%\\config",
  164. L"*.tmp",
  165. true
  166. );
  167. ValidateResult(hr, "IVssCreateWriterMetadata::AddExcludeFiles");
  168. hr = pMetadata->AddComponent
  169. (
  170. VSS_CT_DATABASE,
  171. L"\\mydatabases",
  172. L"db1",
  173. L"this is my main database",
  174. x_rgbIcon,
  175. x_cbIcon,
  176. true,
  177. true,
  178. true
  179. );
  180. ValidateResult(hr, "IVssCreateWriterMetadata::AddComponent");
  181. hr = pMetadata->AddDatabaseFiles
  182. (
  183. L"\\mydatabases",
  184. L"db1",
  185. L"e:\\databases",
  186. L"foo.db"
  187. );
  188. ValidateResult(hr, "IVssCreateWriterMetadata::AddDatabaseFiles");
  189. hr = pMetadata->AddDatabaseLogFiles
  190. (
  191. L"\\mydatabases",
  192. L"db1",
  193. L"e:\\logs",
  194. L"foo.log"
  195. );
  196. ValidateResult(hr, "IVssCreateWriterMetadata::AddDatabaseLogFiles");
  197. hr = pMetadata->SetRestoreMethod
  198. (
  199. VSS_RME_RESTORE_TO_ALTERNATE_LOCATION,
  200. NULL,
  201. NULL,
  202. VSS_WRE_ALWAYS,
  203. true
  204. );
  205. ValidateResult(hr, "IVssCreateWriterMetadata::SetRestoreMethod");
  206. hr = pMetadata->AddAlternateLocationMapping
  207. (
  208. L"c:\\databases",
  209. L"*.db",
  210. false,
  211. L"e:\\databases\\restore"
  212. );
  213. ValidateResult(hr, "IVssCreateWriterMetadata::AddAlternateLocationMapping");
  214. hr = pMetadata->AddAlternateLocationMapping
  215. (
  216. L"d:\\logs",
  217. L"*.log",
  218. false,
  219. L"e:\\databases\\restore"
  220. );
  221. ValidateResult(hr, "IVssCreateWriterMetadata::AddAlternateLocationMapping");
  222. }
  223. catch(HRESULT)
  224. {
  225. return false;
  226. }
  227. catch(...)
  228. {
  229. LogUnexpectedException("CVsWriterTest::OnIdentify");
  230. return false;
  231. }
  232. return true;
  233. }
  234. bool STDMETHODCALLTYPE CVsWriterTest::OnPrepareBackup(IN IVssWriterComponents *pWriterComponents)
  235. {
  236. try
  237. {
  238. unsigned cComponents;
  239. LPCWSTR wszBackupType;
  240. switch(GetBackupType())
  241. {
  242. default:
  243. wszBackupType = L"undefined";
  244. break;
  245. case VSS_BT_FULL:
  246. wszBackupType = L"full";
  247. break;
  248. case VSS_BT_INCREMENTAL:
  249. wszBackupType = L"incremental";
  250. break;
  251. case VSS_BT_DIFFERENTIAL:
  252. wszBackupType = L"differential";
  253. break;
  254. case VSS_BT_OTHER:
  255. wszBackupType = L"other";
  256. break;
  257. }
  258. if (!pWriterComponents)
  259. return true;
  260. HRESULT hr = pWriterComponents->GetComponentCount(&cComponents);
  261. ValidateResult(hr, "IVssWriterComponents::GetComponentCount");
  262. for(unsigned iComponent = 0; iComponent < cComponents; iComponent++)
  263. {
  264. CComPtr<IVssComponent> pComponent;
  265. VSS_COMPONENT_TYPE ct;
  266. CComBSTR bstrLogicalPath;
  267. CComBSTR bstrComponentName;
  268. hr = pWriterComponents->GetComponent(iComponent, &pComponent);
  269. ValidateResult(hr, "IVssWriterComponents::GetComponent");
  270. hr = pComponent->GetLogicalPath(&bstrLogicalPath);
  271. ValidateResult(hr, "IVssComponent::GetLogicalPath");
  272. hr = pComponent->GetComponentType(&ct);
  273. ValidateResult(hr, "IVssComponent::GetComponentType");
  274. hr = pComponent->GetComponentName(&bstrComponentName);
  275. ValidateResult(hr, "IVssComponent::GetComponentName");
  276. CComPtr<IXMLDOMNode> pNode;
  277. hr = pComponent->SetPrivateXMLMetadata(L"BACKUPINFO", &pNode);
  278. ValidateResult(hr, "IVssComponent::SetPrivateXMLMetadata");
  279. CComPtr<IXMLDOMElement> pElement;
  280. hr = pNode->SafeQI(IXMLDOMElement, &pElement);
  281. ValidateResult(hr, "IXMLDOMNode::QueryInterface");
  282. CComBSTR bstrAttributeName = L"backupTime";
  283. if (bstrAttributeName.Length() == 0)
  284. throw(E_OUTOFMEMORY);
  285. CComVariant varValue = (INT) time(NULL);
  286. // Set the attribute
  287. hr = pElement->setAttribute(bstrAttributeName, varValue);
  288. ValidateResult(hr, "IXMLDOMElement::setAttribute");
  289. }
  290. }
  291. catch(HRESULT)
  292. {
  293. return false;
  294. }
  295. catch(...)
  296. {
  297. LogUnexpectedException("CVsWriterTest::OnPrepareBackup");
  298. return false;
  299. }
  300. return true;
  301. }
  302. bool STDMETHODCALLTYPE CVsWriterTest::OnPrepareSnapshot()
  303. {
  304. Sleep(5000);
  305. return true;
  306. }
  307. bool STDMETHODCALLTYPE CVsWriterTest::OnFreeze()
  308. {
  309. Sleep(1000);
  310. return true;
  311. }
  312. bool STDMETHODCALLTYPE CVsWriterTest::OnThaw()
  313. {
  314. Sleep(1000);
  315. return true;
  316. }
  317. bool STDMETHODCALLTYPE CVsWriterTest::OnBackupComplete(IN IVssWriterComponents *pWriterComponents)
  318. {
  319. try
  320. {
  321. HRESULT hr;
  322. unsigned cComponents;
  323. hr = pWriterComponents->GetComponentCount(&cComponents);
  324. ValidateResult(hr, "IVssWriterComponents::GetComponentCount");
  325. for(unsigned iComponent = 0; iComponent < cComponents; iComponent++)
  326. {
  327. CComPtr<IVssComponent> pComponent;
  328. VSS_COMPONENT_TYPE ct;
  329. CComBSTR bstrLogicalPath;
  330. CComBSTR bstrComponentName;
  331. bool bBackupSucceeded;
  332. hr = pWriterComponents->GetComponent(iComponent, &pComponent);
  333. ValidateResult(hr, "IVssWriterComponents::GetComponent");
  334. hr = pComponent->GetLogicalPath(&bstrLogicalPath);
  335. ValidateResult(hr, "IVssComponent::GetLogicalPath");
  336. hr = pComponent->GetComponentType(&ct);
  337. ValidateResult(hr, "IVssComponent::GetComponentType");
  338. hr = pComponent->GetComponentName(&bstrComponentName);
  339. ValidateResult(hr, "IVssComponent::GetComponentName");
  340. hr = pComponent->GetBackupSucceeded(&bBackupSucceeded);
  341. ValidateResult(hr, "IVssComponent::GetBackupSucceeded");
  342. CComPtr<IXMLDOMNode> pNode;
  343. hr = pComponent->GetPrivateXMLData(L"BACKUPINFO", &pNode);
  344. ValidateResult(hr, "IVssComponent::GetPrivateXMLData");
  345. // create attribute map if one doesn't exist
  346. CComPtr<IXMLDOMNamedNodeMap>pAttributeMap;
  347. hr = pNode->get_attributes(&pAttributeMap);
  348. ValidateResult(hr, "IVssDOMNamedNodeMap::get_attributes");
  349. bool bFound = false;
  350. CComPtr<IXMLDOMNode> pNodeT = NULL;
  351. if (pAttributeMap != NULL)
  352. {
  353. // get attribute
  354. HRESULT hr = pAttributeMap->getNamedItem(L"backupTime", &pNodeT);
  355. if (SUCCEEDED(hr))
  356. bFound = true;
  357. if (bFound)
  358. {
  359. CComBSTR bstrAttrValue;
  360. hr = pNodeT->get_text(&bstrAttrValue);
  361. ValidateResult(hr, "IXMLDOMNode::get_text");
  362. }
  363. else
  364. {
  365. LogFailure("didn't find private backupTime attribute");
  366. }
  367. }
  368. }
  369. }
  370. catch(HRESULT)
  371. {
  372. return false;
  373. }
  374. catch(...)
  375. {
  376. LogUnexpectedException("CVsWriterTest::OnBackupComplete");
  377. return false;
  378. }
  379. return true;
  380. }
  381. bool STDMETHODCALLTYPE CVsWriterTest::OnRestore(IN IVssWriterComponents *pComponent)
  382. {
  383. UNREFERENCED_PARAMETER(pComponent);
  384. Sleep(10000);
  385. return true;
  386. }
  387. bool STDMETHODCALLTYPE CVsWriterTest::OnAbort()
  388. {
  389. return true;
  390. }