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.

269 lines
6.0 KiB

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