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.

305 lines
9.5 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Abstract:
  4. @doc
  5. @module Writer.cpp | Implementation of Writer
  6. @end
  7. Author:
  8. Adi Oltean [aoltean] 08/18/1999
  9. TBD:
  10. Add comments.
  11. Revision History:
  12. Name Date Comments
  13. aoltean 08/18/1999 Created
  14. aoltean 09/22/1999 Making console output clearer
  15. --*/
  16. /////////////////////////////////////////////////////////////////////////////
  17. // Defines
  18. // C4290: C++ Exception Specification ignored
  19. #pragma warning(disable:4290)
  20. // warning C4511: 'CVssCOMApplication' : copy constructor could not be generated
  21. #pragma warning(disable:4511)
  22. // warning C4127: conditional expression is constant
  23. #pragma warning(disable:4127)
  24. /////////////////////////////////////////////////////////////////////////////
  25. // Includes
  26. #include <wtypes.h>
  27. #include <stddef.h>
  28. #include <oleauto.h>
  29. #include <comadmin.h>
  30. #include "vs_assert.hxx"
  31. // ATL
  32. #include <atlconv.h>
  33. #include <atlbase.h>
  34. CComModule _Module;
  35. #include <atlcom.h>
  36. #include "vs_inc.hxx"
  37. #include "vss.h"
  38. #include "comadmin.hxx"
  39. #include "vsevent.h"
  40. #include "writer.h"
  41. /////////////////////////////////////////////////////////////////////////////
  42. // Constants
  43. const CComBSTR g_bstrEventClassProgID = L"VssEvent.VssEvent.1";
  44. const CComBSTR g_bstrPublisherID = L"VSS Publisher"; // Publisher ID
  45. const CComBSTR g_bstrSubscriber1AppName = L"Writer 1"; // Subscriber 1 App Name
  46. const CComBSTR g_bstrEventClsIID = L"{2F7BF5AA-408A-4248-907A-2FD7D497A703}";
  47. const CComBSTR g_bstrResolveResourceMethodName = L"ResolveResource";
  48. const CComBSTR g_bstrPrepareForSnapshotMethodName = L"PrepareForSnapshot";
  49. const CComBSTR g_bstrFreezeMethodName = L"Freeze";
  50. const CComBSTR g_bstrThawMethodName = L"Thaw";
  51. const CComBSTR g_bstrMeltMethodName = L"Melt";
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CVssWriter
  54. STDMETHODIMP CVssWriter::PrepareForSnapshot(
  55. IN BSTR bstrSnapshotSetId,
  56. IN BSTR bstrVolumeNamesList,
  57. IN VSS_FLUSH_TYPE eFlushType,
  58. IN BSTR bstrFlushContext,
  59. IN IDispatch* pDepGraphCallback,
  60. IN IDispatch* pAsyncCallback
  61. )
  62. {
  63. CVssFunctionTracer ft( VSSDBG_VSSTEST, L"CVssWriter::PrepareForSnapshot" );
  64. wprintf(L"\nReceived Event: PrepareForSnapshot\nParameters:\n");
  65. wprintf(L"\tSnapshotSetID = %s\n", (LPWSTR)bstrSnapshotSetId);
  66. wprintf(L"\tVolumeNamesList = %s\n", (LPWSTR)bstrVolumeNamesList);
  67. wprintf(L"\tFlush Type = %d\n", eFlushType);
  68. wprintf(L"\tFlush Context = %s\n", (LPWSTR)bstrFlushContext);
  69. if (pAsyncCallback)
  70. {
  71. // Release the previous interface.
  72. // A smarter writer will associate one Async interface with one Snapshot Set ID.
  73. m_pAsync = NULL;
  74. // Get the new async interface
  75. ft.hr = pAsyncCallback->SafeQI(IVssAsync, &m_pAsync);
  76. if (ft.HrFailed())
  77. ft.Err( VSSDBG_VSSTEST, E_UNEXPECTED, L"Error calling QI 0x%08lx", ft.hr );
  78. BS_ASSERT(m_pAsync);
  79. }
  80. // Ask for cancel
  81. AskCancelDuringFreezeThaw(ft);
  82. return S_OK;
  83. UNREFERENCED_PARAMETER(pDepGraphCallback);
  84. }
  85. HRESULT CVssWriter::Freeze(
  86. IN BSTR bstrSnapshotSetId,
  87. IN INT nLevel
  88. )
  89. {
  90. CVssFunctionTracer ft( VSSDBG_VSSTEST, L"CVssWriter::Freeze" );
  91. wprintf(L"\nReceived Event: Freeze\nParameters:\n");
  92. wprintf(L"\tSnapshotSetID = %s\n", (LPWSTR)bstrSnapshotSetId);
  93. wprintf(L"\tLevel = %d\n", nLevel);
  94. // Ask for cancel
  95. AskCancelDuringFreezeThaw(ft);
  96. return S_OK;
  97. }
  98. HRESULT CVssWriter::Thaw(
  99. IN BSTR bstrSnapshotSetId
  100. )
  101. {
  102. CVssFunctionTracer ft( VSSDBG_VSSTEST, L"CVssWriter::Thaw" );
  103. wprintf(L"\nReceived Event: Thaw\nParameters:\n");
  104. wprintf(L"\tSnapshotSetId = %s\n", (LPWSTR)bstrSnapshotSetId);
  105. // Ask for cancel
  106. AskCancelDuringFreezeThaw(ft);
  107. // Release the Async interface
  108. m_pAsync = NULL;
  109. return S_OK;
  110. }
  111. IUnknown* GetSubscriptionObject(CVssFunctionTracer& ft)
  112. {
  113. IUnknown* pUnk;
  114. CComObject<CVssWriter>* pObj;
  115. ft.hr = CComObject<CVssWriter>::CreateInstance(&pObj);
  116. if (ft.HrFailed())
  117. ft.Throw(VSSDBG_VSSTEST, E_UNEXPECTED, L"Failure in creating the subscription object 0x%08lx", ft.hr);
  118. pUnk = pObj->GetUnknown();
  119. pUnk->AddRef();
  120. return pUnk;
  121. }
  122. /////////////////////////////////////////////////////////////////////////////
  123. // User interaction
  124. void CVssWriter::AskCancelDuringFreezeThaw(
  125. IN CVssFunctionTracer& ft
  126. )
  127. {
  128. try
  129. {
  130. if(m_pAsync == NULL)
  131. return;
  132. WCHAR wchCancelPlease = (QueryString(L"Cancel? [y/N] "))[0];
  133. if (towupper(wchCancelPlease) == L'Y')
  134. {
  135. CComBSTR strReason = QueryString(L"Reason: ");
  136. ft.hr = m_pAsync->Cancel();
  137. if (ft.HrFailed())
  138. ft.Err( VSSDBG_VSSTEST, E_UNEXPECTED,
  139. L"Error calling AddDependency 0x%08lx", ft.hr );
  140. ft.Msg(L"HRESULT = 0x%08lx", ft.hr );
  141. }
  142. }
  143. VSS_STANDARD_CATCH(ft)
  144. }
  145. /////////////////////////////////////////////////////////////////////////////
  146. // WinMain
  147. extern "C" int WINAPI _tWinMain(HINSTANCE /*hInstance*/,
  148. HINSTANCE /*hPrevInstance*/, LPTSTR /*lpCmdLine*/, int /*nShowCmd*/)
  149. {
  150. CVssFunctionTracer ft( VSSDBG_VSSTEST, L"_tWinMain" );
  151. int nRet = 0;
  152. try
  153. {
  154. // Initialize COM library
  155. ft.hr = CoInitialize(NULL);
  156. if (ft.HrFailed())
  157. ft.Throw(VSSDBG_VSSTEST, E_UNEXPECTED, L"Failure in initializing the COM library 0x%08lx", ft.hr);
  158. // Get the subscriber object
  159. IUnknown* pUnkSubscriber = GetSubscriptionObject(ft);
  160. // Initialize the catalog
  161. CVssCOMAdminCatalog catalog;
  162. ft.hr = catalog.Attach(g_bstrSubscriber1AppName);
  163. if (ft.HrFailed())
  164. ft.Throw(VSSDBG_VSSTEST, E_UNEXPECTED, L"Failure in initializing the catalog object 0x%08lx", ft.hr);
  165. // Get the list of applications
  166. CVssCOMCatalogCollection transSubsList(VSS_COM_TRANSIENT_SUBSCRIPTIONS);
  167. ft.hr = transSubsList.Attach(catalog);
  168. if (ft.HrFailed())
  169. ft.Throw(VSSDBG_VSSTEST, E_UNEXPECTED, L"Failure in initializing the subs collection object 0x%08lx", ft.hr);
  170. // Add our new transient subscription for PrepareForSnapshot
  171. CVssCOMTransientSubscription subscription;
  172. ft.hr = subscription.InsertInto(transSubsList);
  173. if (ft.HrFailed())
  174. ft.Throw(VSSDBG_VSSTEST, E_UNEXPECTED, L"Failure in creating a new sub object 0x%08lx", ft.hr);
  175. subscription.m_bstrName = g_bstrSubscriber1AppName;
  176. subscription.m_bstrPublisherID = g_bstrPublisherID;
  177. subscription.m_bstrInterfaceID = g_bstrEventClsIID;
  178. subscription.m_varSubscriberInterface = CComVariant(pUnkSubscriber);
  179. subscription.m_bstrMethodName = g_bstrResolveResourceMethodName;
  180. ft.hr = subscription.InsertInto(transSubsList);
  181. if (ft.HrFailed())
  182. ft.Throw(VSSDBG_VSSTEST, E_UNEXPECTED, L"Failure in creating a new sub object 0x%08lx", ft.hr);
  183. subscription.m_bstrName = g_bstrSubscriber1AppName;
  184. subscription.m_bstrPublisherID = g_bstrPublisherID;
  185. subscription.m_bstrInterfaceID = g_bstrEventClsIID;
  186. subscription.m_varSubscriberInterface = CComVariant(pUnkSubscriber);
  187. subscription.m_bstrMethodName = g_bstrPrepareForSnapshotMethodName;
  188. // Add our new transient subscription for Freeze
  189. ft.hr = subscription.InsertInto(transSubsList);
  190. if (ft.HrFailed())
  191. ft.Throw(VSSDBG_VSSTEST, E_UNEXPECTED, L"Failure in creating a new sub object 0x%08lx", ft.hr);
  192. subscription.m_bstrName = g_bstrSubscriber1AppName;
  193. subscription.m_bstrPublisherID = g_bstrPublisherID;
  194. subscription.m_bstrInterfaceID = g_bstrEventClsIID;
  195. subscription.m_varSubscriberInterface = CComVariant(pUnkSubscriber);
  196. subscription.m_bstrMethodName = g_bstrFreezeMethodName;
  197. // Add our new transient subscription for Thaw
  198. ft.hr = subscription.InsertInto(transSubsList);
  199. if (ft.HrFailed())
  200. ft.Throw(VSSDBG_VSSTEST, E_UNEXPECTED, L"Failure in creating a new sub object 0x%08lx", ft.hr);
  201. subscription.m_bstrName = g_bstrSubscriber1AppName;
  202. subscription.m_bstrPublisherID = g_bstrPublisherID;
  203. subscription.m_bstrInterfaceID = g_bstrEventClsIID;
  204. subscription.m_varSubscriberInterface = CComVariant(pUnkSubscriber);
  205. subscription.m_bstrMethodName = g_bstrThawMethodName;
  206. // Add our new transient subscription for Melt
  207. ft.hr = subscription.InsertInto(transSubsList);
  208. if (ft.HrFailed())
  209. ft.Throw(VSSDBG_VSSTEST, E_UNEXPECTED, L"Failure in creating a new sub object 0x%08lx", ft.hr);
  210. subscription.m_bstrName = g_bstrSubscriber1AppName;
  211. subscription.m_bstrPublisherID = g_bstrPublisherID;
  212. subscription.m_bstrInterfaceID = g_bstrEventClsIID;
  213. subscription.m_varSubscriberInterface = CComVariant(pUnkSubscriber);
  214. subscription.m_bstrMethodName = g_bstrMeltMethodName;
  215. // Save changes
  216. ft.hr = transSubsList.SaveChanges();
  217. if (ft.HrFailed())
  218. ft.Throw(VSSDBG_VSSTEST, E_UNEXPECTED, L"Failure in commiting changes. hr = 0x%08lx", ft.hr);
  219. // message loop - need for STA server
  220. MSG msg;
  221. while (GetMessage(&msg, 0, 0, 0))
  222. DispatchMessage(&msg);
  223. // Uninitialize COM library
  224. CoUninitialize();
  225. }
  226. VSS_STANDARD_CATCH(ft)
  227. return nRet;
  228. }