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.

377 lines
9.3 KiB

  1. /******************************************************************************
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. Cabinet.cpp
  5. Abstract:
  6. This file contains the implementation of the CSAFCabinet class,
  7. which implements the data collection functionality.
  8. Revision History:
  9. Davide Massarenti (Dmassare) 08/25/99
  10. created
  11. ******************************************************************************/
  12. #include "stdafx.h"
  13. /////////////////////////////////////////////////////////////////////////////
  14. #define CHECK_MODIFY() __MPC_EXIT_IF_METHOD_FAILS(hr, CanModifyProperties())
  15. /////////////////////////////////////////////////////////////////////////////
  16. CSAFCabinet::CSAFCabinet()
  17. {
  18. __HCP_FUNC_ENTRY( "CSAFCabinet::CSAFCabinet" );
  19. // MPC::Impersonation m_imp;
  20. //
  21. // MPC::Cabinet m_cab;
  22. //
  23. m_hResult = 0; // HRESULT m_hResult;
  24. m_cbStatus = CB_NOTACTIVE; // CB_STATUS m_cbStatus;
  25. //
  26. // CComPtr<IDispatch> m_sink_onProgressFiles;
  27. // CComPtr<IDispatch> m_sink_onProgressBytes;
  28. // CComPtr<IDispatch> m_sink_onComplete;
  29. (void)m_cab.put_IgnoreMissingFiles( TRUE );
  30. (void)m_cab.put_UserData ( this );
  31. (void)m_cab.put_onProgress_Files ( fnCallback_Files );
  32. (void)m_cab.put_onProgress_Bytes ( fnCallback_Bytes );
  33. }
  34. void CSAFCabinet::FinalRelease()
  35. {
  36. __HCP_FUNC_ENTRY( "CSAFCabinet::FinalRelease" );
  37. (void)Abort();
  38. Thread_Wait();
  39. }
  40. /////////////////////////////////////////////////////////////////////////////
  41. /////////////////////////////////////////////////////////////////////////////
  42. HRESULT CSAFCabinet::Run()
  43. {
  44. __HCP_FUNC_ENTRY( "CSAFCabinet::Run" );
  45. HRESULT hr;
  46. BOOL res;
  47. MPC::SmartLock<_ThreadModel> lock( this );
  48. ::SetThreadPriority( ::GetCurrentThread(), THREAD_PRIORITY_LOWEST );
  49. __MPC_TRY_BEGIN();
  50. put_Status( CB_COMPRESSING );
  51. __MPC_EXIT_IF_METHOD_FAILS(hr, m_imp.Impersonate());
  52. lock = NULL;
  53. __MPC_EXIT_IF_METHOD_FAILS(hr, m_cab.Compress());
  54. lock = this;
  55. __MPC_EXIT_IF_METHOD_FAILS(hr, m_imp.RevertToSelf());
  56. put_Status( CB_COMPLETED );
  57. hr = S_OK;
  58. __HCP_FUNC_CLEANUP;
  59. __MPC_TRY_CATCHALL(hr);
  60. lock = this;
  61. m_hResult = hr;
  62. if(FAILED(hr))
  63. {
  64. put_Status( CB_FAILED );
  65. }
  66. //
  67. // Release the lock on current object, otherwise a deadlock could occur.
  68. //
  69. lock = NULL;
  70. Fire_onComplete( this, hr );
  71. Thread_Abort(); // To tell the MPC:Thread object to close the worker thread...
  72. __HCP_FUNC_EXIT(hr);
  73. }
  74. /////////////////////////////////////////////////////////////////////////////
  75. ////////////////
  76. // //
  77. // Properties //
  78. // //
  79. ////////////////
  80. STDMETHODIMP CSAFCabinet::put_IgnoreMissingFiles( /*[in]*/ VARIANT_BOOL fIgnoreMissingFiles )
  81. {
  82. __HCP_BEGIN_PROPERTY_PUT("CSAFCabinet::put_IgnoreMissingFiles",hr);
  83. CHECK_MODIFY();
  84. __MPC_EXIT_IF_METHOD_FAILS(hr, m_cab.put_IgnoreMissingFiles( fIgnoreMissingFiles == VARIANT_TRUE ));
  85. __HCP_END_PROPERTY(hr);
  86. }
  87. STDMETHODIMP CSAFCabinet::put_onProgressFiles( /*[in]*/ IDispatch* function )
  88. {
  89. __HCP_BEGIN_PROPERTY_PUT("CSAFCabinet::put_onProgressFiles",hr);
  90. CHECK_MODIFY();
  91. m_sink_onProgressFiles = function;
  92. __HCP_END_PROPERTY(hr);
  93. }
  94. STDMETHODIMP CSAFCabinet::put_onProgressBytes( /*[in]*/ IDispatch* function )
  95. {
  96. __HCP_BEGIN_PROPERTY_PUT("CSAFCabinet::put_onProgressBytes",hr);
  97. CHECK_MODIFY();
  98. m_sink_onProgressBytes = function;
  99. __HCP_END_PROPERTY(hr);
  100. }
  101. STDMETHODIMP CSAFCabinet::put_onComplete( /*[in]*/ IDispatch* function )
  102. {
  103. __HCP_BEGIN_PROPERTY_PUT("CSAFCabinet::put_onComplete",hr);
  104. CHECK_MODIFY();
  105. m_sink_onComplete = function;
  106. __HCP_END_PROPERTY(hr);
  107. }
  108. HRESULT CSAFCabinet::put_Status( /*[in]*/ CB_STATUS pVal ) // Inner method
  109. {
  110. __HCP_FUNC_ENTRY( "CSAFCabinet::put_Status" );
  111. HRESULT hr;
  112. MPC::SmartLock<_ThreadModel> lock( this );
  113. m_cbStatus = pVal;
  114. hr = S_OK;
  115. __HCP_FUNC_EXIT(hr);
  116. }
  117. STDMETHODIMP CSAFCabinet::get_Status( /*[out]*/ CB_STATUS *pVal )
  118. {
  119. __HCP_BEGIN_PROPERTY_GET2("CSAFCabinet::get_Status",hr,pVal,m_cbStatus);
  120. __HCP_END_PROPERTY(hr);
  121. }
  122. STDMETHODIMP CSAFCabinet::get_ErrorCode( /*[out]*/ long *pVal )
  123. {
  124. __HCP_BEGIN_PROPERTY_GET2("CSAFCabinet::get_ErrorCode",hr,pVal,m_hResult);
  125. __HCP_END_PROPERTY(hr);
  126. }
  127. /////////////////////////////////////////////////////////////////////////////
  128. /////////////////////////////////////////////////////////////////////////////
  129. STDMETHODIMP CSAFCabinet::AddFile( /*[in]*/ BSTR bstrFilePath ,
  130. /*[in]*/ VARIANT vFileName )
  131. {
  132. __HCP_FUNC_ENTRY( "CSAFCabinet::AddFile" );
  133. HRESULT hr;
  134. MPC::SmartLock<_ThreadModel> lock( this );
  135. LPCWSTR szFileName = (vFileName.vt == VT_BSTR ? SAFEBSTR( vFileName.bstrVal ) : L"");
  136. __MPC_PARAMCHECK_BEGIN(hr)
  137. __MPC_PARAMCHECK_STRING_NOT_EMPTY(bstrFilePath);
  138. __MPC_PARAMCHECK_END();
  139. CHECK_MODIFY();
  140. __MPC_EXIT_IF_METHOD_FAILS(hr, m_cab.AddFile( bstrFilePath, szFileName ));
  141. hr = S_OK;
  142. __HCP_FUNC_CLEANUP;
  143. __HCP_FUNC_EXIT(hr);
  144. }
  145. STDMETHODIMP CSAFCabinet::Compress( /*[in]*/ BSTR bstrCabinetFile )
  146. {
  147. __HCP_FUNC_ENTRY( "CSAFCabinet::Compress" );
  148. HRESULT hr;
  149. MPC::SmartLock<_ThreadModel> lock( this );
  150. __MPC_PARAMCHECK_BEGIN(hr)
  151. __MPC_PARAMCHECK_STRING_NOT_EMPTY(bstrCabinetFile);
  152. __MPC_PARAMCHECK_END();
  153. CHECK_MODIFY();
  154. __MPC_EXIT_IF_METHOD_FAILS(hr, m_cab.put_CabinetFile( bstrCabinetFile ));
  155. //
  156. // Release the lock on current object, otherwise a deadlock could occur.
  157. //
  158. lock = NULL;
  159. __MPC_EXIT_IF_METHOD_FAILS(hr, m_imp.Initialize());
  160. __MPC_EXIT_IF_METHOD_FAILS(hr, Thread_Start( this, Run, NULL ));
  161. hr = S_OK;
  162. __HCP_FUNC_CLEANUP;
  163. __HCP_FUNC_EXIT(hr);
  164. }
  165. STDMETHODIMP CSAFCabinet::Abort()
  166. {
  167. __HCP_FUNC_ENTRY( "CSAFCabinet::Abort" );
  168. Thread_Abort(); // To tell the MPC:Thread object to close the worker thread...
  169. __HCP_FUNC_EXIT(S_OK);
  170. }
  171. /////////////////////////////////////////////////////////////////////////////
  172. /////////////////////////////////////////////////////////////////////////////
  173. //////////////////////
  174. // //
  175. // Callback Methods //
  176. // //
  177. //////////////////////
  178. HRESULT CSAFCabinet::fnCallback_Files( MPC::Cabinet* cabinet, LPCWSTR szFile, ULONG lDone, ULONG lTotal, LPVOID user )
  179. {
  180. CSAFCabinet* pThis = (CSAFCabinet*)user;
  181. HRESULT hr;
  182. if(pThis->Thread_IsAborted())
  183. {
  184. hr = E_FAIL;
  185. }
  186. else
  187. {
  188. hr = pThis->Fire_onProgressFiles( pThis, CComBSTR( szFile ), lDone, lTotal );
  189. }
  190. return hr;
  191. }
  192. HRESULT CSAFCabinet::fnCallback_Bytes( MPC::Cabinet* cabinet, ULONG lDone, ULONG lTotal, LPVOID user )
  193. {
  194. CSAFCabinet* pThis = (CSAFCabinet*)user;
  195. HRESULT hr;
  196. if(pThis->Thread_IsAborted())
  197. {
  198. hr = E_FAIL;
  199. }
  200. else
  201. {
  202. hr = pThis->Fire_onProgressBytes( pThis, lDone, lTotal );
  203. }
  204. return hr;
  205. }
  206. /////////////////////////////////////////////////////////////////////////////
  207. /////////////////////////////////////////////////////////////////////////////
  208. //////////////////////////
  209. // //
  210. // Event Firing Methods //
  211. // //
  212. //////////////////////////
  213. HRESULT CSAFCabinet::Fire_onProgressFiles( ISAFCabinet* hcpcb, BSTR bstrFile, long lDone, long lTotal )
  214. {
  215. CComVariant pvars[4];
  216. pvars[3] = hcpcb;
  217. pvars[2] = bstrFile;
  218. pvars[1] = lDone;
  219. pvars[0] = lTotal;
  220. return FireAsync_Generic( DISPID_SAF_CBE__ONPROGRESSFILES, pvars, ARRAYSIZE( pvars ), m_sink_onProgressFiles );
  221. }
  222. HRESULT CSAFCabinet::Fire_onProgressBytes( ISAFCabinet* hcpcb, long lDone, long lTotal )
  223. {
  224. CComVariant pvars[3];
  225. pvars[2] = hcpcb;
  226. pvars[1] = lDone;
  227. pvars[0] = lTotal;
  228. return FireAsync_Generic( DISPID_SAF_CBE__ONPROGRESSBYTES, pvars, ARRAYSIZE( pvars ), m_sink_onProgressBytes );
  229. }
  230. HRESULT CSAFCabinet::Fire_onComplete( ISAFCabinet* hcpcb, HRESULT hrRes )
  231. {
  232. CComVariant pvars[2];
  233. pvars[1] = hcpcb;
  234. pvars[0] = hrRes;
  235. return FireAsync_Generic( DISPID_SAF_CBE__ONCOMPLETE, pvars, ARRAYSIZE( pvars ), m_sink_onComplete );
  236. }
  237. /////////////////////////////////////////////////////////////////////////////
  238. /////////////////////////////////////////////////////////////////////////////
  239. /////////////////////
  240. // //
  241. // Utility Methods //
  242. // //
  243. /////////////////////
  244. HRESULT CSAFCabinet::CanModifyProperties()
  245. {
  246. __HCP_FUNC_ENTRY( "CSAFCabinet::CanModifyProperties" );
  247. HRESULT hr = E_ACCESSDENIED;
  248. if(m_cbStatus != CB_COMPRESSING)
  249. {
  250. hr = S_OK;
  251. }
  252. __HCP_FUNC_EXIT(hr);
  253. }