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.

235 lines
6.0 KiB

  1. // Patch.cpp: implementation of the CPatch class.
  2. //
  3. // Copyright (c) 1997-2002 Microsoft Corporation, All Rights Reserved
  4. //
  5. //////////////////////////////////////////////////////////////////////
  6. #include "precomp.h"
  7. #include "Patch.h"
  8. #include "ExtendString.h"
  9. #include "ExtendQuery.h"
  10. //////////////////////////////////////////////////////////////////////
  11. // Construction/Destruction
  12. //////////////////////////////////////////////////////////////////////
  13. CPatch::CPatch(CRequestObject *pObj, IWbemServices *pNamespace,
  14. IWbemContext *pCtx):CGenericClass(pObj, pNamespace, pCtx)
  15. {
  16. }
  17. CPatch::~CPatch()
  18. {
  19. }
  20. HRESULT CPatch::CreateObject(IWbemObjectSink *pHandler, ACTIONTYPE atAction)
  21. {
  22. HRESULT hr = WBEM_S_NO_ERROR;
  23. MSIHANDLE hView = NULL, hRecord = NULL;
  24. int i = -1;
  25. WCHAR wcBuf[BUFF_SIZE];
  26. WCHAR wcProductCode[39];
  27. DWORD dwBufSize;
  28. bool bMatch = false;
  29. UINT uiStatus;
  30. bool bGotID = false;
  31. WCHAR wcTestCode[39];
  32. bool bGotName = false;
  33. WCHAR wcName[BUFF_SIZE];
  34. //These will change from class to class
  35. bool bFile, bSequence, bProduct;
  36. //improve getobject performance by optimizing the query
  37. if(atAction != ACTIONTYPE_ENUM)
  38. {
  39. // we are doing GetObject so we need to be reinitialized
  40. hr = WBEM_E_NOT_FOUND;
  41. BSTR bstrCompare;
  42. int iPos = -1;
  43. bstrCompare = SysAllocString ( L"ProductCode" );
  44. if ( bstrCompare )
  45. {
  46. if(FindIn(m_pRequest->m_Property, bstrCompare, &iPos))
  47. {
  48. if ( ::SysStringLen ( m_pRequest->m_Value[iPos] ) == 38 )
  49. {
  50. //Get the product code we're looking for
  51. wcscpy(wcTestCode, m_pRequest->m_Value[iPos]);
  52. bGotID = true;
  53. }
  54. else
  55. {
  56. // we are not good to go, they have sent us longer string
  57. SysFreeString ( bstrCompare );
  58. throw hr;
  59. }
  60. }
  61. SysFreeString ( bstrCompare );
  62. }
  63. else
  64. {
  65. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  66. }
  67. iPos = -1;
  68. bstrCompare = SysAllocString ( L"File" );
  69. if ( bstrCompare )
  70. {
  71. if(FindIn(m_pRequest->m_Property, bstrCompare, &iPos))
  72. {
  73. if ( ::SysStringLen ( m_pRequest->m_Value[iPos] ) < BUFF_SIZE )
  74. {
  75. //Get the name we're looking for
  76. wcscpy(wcName, m_pRequest->m_Value[iPos]);
  77. bGotName = true;
  78. }
  79. else
  80. {
  81. // we are not good to go, they have sent us longer string
  82. SysFreeString ( bstrCompare );
  83. throw hr;
  84. }
  85. }
  86. SysFreeString ( bstrCompare );
  87. }
  88. else
  89. {
  90. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  91. }
  92. }
  93. Query wcQuery;
  94. wcQuery.Append ( 1, L"select distinct `File_`, `Sequence`, `PatchSize`, `Attributes` from Patch" );
  95. //optimize for GetObject
  96. if ( bGotName )
  97. {
  98. wcQuery.Append ( 3, L" where `File_`=\'", wcName, L"\'" );
  99. }
  100. LPWSTR Buffer = NULL;
  101. LPWSTR dynBuffer = NULL;
  102. DWORD dwDynBuffer = 0L;
  103. while(!bMatch && m_pRequest->Package(++i) && (hr != WBEM_E_CALL_CANCELLED))
  104. {
  105. // safe operation:
  106. // Package ( i ) returns NULL ( tested above ) or valid WCHAR [39]
  107. wcscpy(wcProductCode, m_pRequest->Package(i));
  108. if((atAction == ACTIONTYPE_ENUM) || (bGotID && (_wcsicmp(wcTestCode, wcProductCode) == 0))){
  109. //Open our database
  110. try
  111. {
  112. if ( GetView ( &hView, wcProductCode, wcQuery, L"Patch", TRUE, FALSE ) )
  113. {
  114. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  115. while(!bMatch && (uiStatus != ERROR_NO_MORE_ITEMS) && (hr != WBEM_E_CALL_CANCELLED)){
  116. CheckMSI(uiStatus);
  117. if(FAILED(hr = SpawnAnInstance(&m_pObj))) throw hr;
  118. //----------------------------------------------------
  119. dwBufSize = BUFF_SIZE;
  120. GetBufferToPut ( hRecord, 1, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  121. PutKeyProperty(m_pObj, pFile, Buffer, &bFile, m_pRequest);
  122. PutProperty(m_pObj, pCaption, Buffer);
  123. PutProperty(m_pObj, pDescription, Buffer);
  124. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  125. {
  126. dynBuffer [ 0 ] = 0;
  127. }
  128. PutKeyProperty(m_pObj, pSequence, g_fpMsiRecordGetInteger(hRecord, 2),
  129. &bSequence, m_pRequest);
  130. PutKeyProperty(m_pObj, pProductCode, wcProductCode, &bProduct, m_pRequest);
  131. //====================================================
  132. PutProperty(m_pObj, pPatchSize, g_fpMsiRecordGetInteger(hRecord, 3));
  133. PutProperty(m_pObj, pAttributes, g_fpMsiRecordGetInteger(hRecord, 4));
  134. //----------------------------------------------------
  135. if(bFile && bSequence && bProduct) bMatch = true;
  136. if((atAction != ACTIONTYPE_GET) || bMatch){
  137. hr = pHandler->Indicate(1, &m_pObj);
  138. }
  139. m_pObj->Release();
  140. m_pObj = NULL;
  141. g_fpMsiCloseHandle(hRecord);
  142. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  143. }
  144. }
  145. }
  146. catch(...)
  147. {
  148. if ( dynBuffer )
  149. {
  150. delete [] dynBuffer;
  151. dynBuffer = NULL;
  152. }
  153. if (hRecord)
  154. g_fpMsiCloseHandle(hRecord);
  155. if (hView)
  156. {
  157. g_fpMsiViewClose(hView);
  158. g_fpMsiCloseHandle(hView);
  159. }
  160. msidata.CloseDatabase ();
  161. if(m_pObj)
  162. {
  163. m_pObj->Release();
  164. m_pObj = NULL;
  165. }
  166. throw;
  167. }
  168. if (hRecord)
  169. g_fpMsiCloseHandle(hRecord);
  170. if (hView)
  171. {
  172. g_fpMsiViewClose(hView);
  173. g_fpMsiCloseHandle(hView);
  174. }
  175. msidata.CloseDatabase ();
  176. }
  177. }
  178. if ( dynBuffer )
  179. {
  180. delete [] dynBuffer;
  181. dynBuffer = NULL;
  182. }
  183. return hr;
  184. }