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.

362 lines
9.3 KiB

  1. /******************************************************************
  2. VolumeQuotaSettings.CPP -- WMI provider class implementation
  3. Description: Implementation of the methods of an association class
  4. Between QuotaSettings and LogicalDisk
  5. Copyright (c) 2000-2001 Microsoft Corporation, All Rights Reserved
  6. ******************************************************************/
  7. #include "precomp.h"
  8. #include "VolumeQuotaSetting.h"
  9. CVolumeQuotaSetting MyCVolumeQuotaSetting (
  10. IDS_VolumeQuotaSetting ,
  11. NameSpace
  12. ) ;
  13. /*****************************************************************************
  14. *
  15. * FUNCTION : CVolumeQuotaSetting::CVolumeQuotaSetting
  16. *
  17. * DESCRIPTION : Constructor
  18. *
  19. * COMMENTS : Calls the Provider constructor.
  20. *
  21. *****************************************************************************/
  22. CVolumeQuotaSetting :: CVolumeQuotaSetting (
  23. LPCWSTR lpwszName,
  24. LPCWSTR lpwszNameSpace
  25. ) : Provider ( lpwszName , lpwszNameSpace )
  26. {
  27. }
  28. /*****************************************************************************
  29. *
  30. * FUNCTION : CVolumeQuotaSetting::~CVolumeQuotaSetting
  31. *
  32. * DESCRIPTION : Destructor
  33. *
  34. * COMMENTS :
  35. *
  36. *****************************************************************************/
  37. CVolumeQuotaSetting :: ~CVolumeQuotaSetting ()
  38. {
  39. }
  40. /*****************************************************************************
  41. *
  42. * FUNCTION : CVolumeQuotaSetting::EnumerateInstances
  43. *
  44. * DESCRIPTION : Returns all the instances of this class.
  45. *
  46. *****************************************************************************/
  47. HRESULT CVolumeQuotaSetting :: EnumerateInstances (
  48. MethodContext *pMethodContext,
  49. long lFlags
  50. )
  51. {
  52. HRESULT hRes = WBEM_S_NO_ERROR;
  53. hRes = EnumerateAllVolumeQuotas ( pMethodContext );
  54. return hRes;
  55. }
  56. /*****************************************************************************
  57. *
  58. * FUNCTION : CVolumeQuotaSetting::GetObject
  59. *
  60. * DESCRIPTION : Find a single instance based on the key properties for the
  61. * class.
  62. *
  63. *****************************************************************************/
  64. HRESULT CVolumeQuotaSetting :: GetObject (
  65. CInstance *pInstance,
  66. long lFlags ,
  67. CFrameworkQuery &Query
  68. )
  69. {
  70. HRESULT hRes = WBEM_S_NO_ERROR;
  71. // Not doing anything here, since the two properties which are in the object
  72. CHString t_Key1;
  73. CHString t_Key2;
  74. if ( pInstance->GetCHString ( IDS_Setting, t_Key1 ) == FALSE )
  75. hRes = WBEM_E_FAILED;
  76. if ( SUCCEEDED ( hRes ) )
  77. {
  78. if ( pInstance->GetCHString ( IDS_LogicalDisk, t_Key2 ) == FALSE )
  79. {
  80. hRes = WBEM_E_FAILED;
  81. }
  82. }
  83. if ( SUCCEEDED ( hRes ) )
  84. {
  85. // If the Drive is not as Logical Disks then GetVolume Method will return False;
  86. WCHAR w_Drive1;
  87. WCHAR w_Drive2;
  88. hRes = m_CommonRoutine.GetVolume ( t_Key1, w_Drive1 );
  89. if (SUCCEEDED ( hRes ) )
  90. {
  91. hRes = m_CommonRoutine.GetVolume ( t_Key2, w_Drive2 );
  92. if (SUCCEEDED ( hRes ) )
  93. {
  94. if ( w_Drive1 == w_Drive2 )
  95. {
  96. // verify this logical drives actually exists
  97. CHString t_DriveStrings1;
  98. CHString t_DriveStrings2;
  99. LPWSTR lpDriveStrings = t_DriveStrings1.GetBuffer ( MAX_PATH + 1 );
  100. DWORD dwDLength = GetLogicalDriveStrings ( MAX_PATH, lpDriveStrings );
  101. if ( dwDLength > MAX_PATH )
  102. {
  103. lpDriveStrings = t_DriveStrings2.GetBuffer ( dwDLength + 1 );
  104. dwDLength = GetLogicalDriveStrings ( dwDLength, lpDriveStrings );
  105. }
  106. hRes = m_CommonRoutine.SearchLogicalDisk ( w_Drive1, lpDriveStrings );
  107. }
  108. else
  109. {
  110. hRes = WBEM_E_INVALID_PARAMETER;
  111. }
  112. }
  113. }
  114. }
  115. return hRes;
  116. }
  117. /*****************************************************************************
  118. *
  119. * FUNCTION : CVolumeQuotaSetting::ExecQuery
  120. *
  121. * DESCRIPTION : You are passed a method context to use in the creation of
  122. * instances that satisfy the query, and a CFrameworkQuery
  123. * which describes the query. Create and populate all
  124. * instances which satisfy the query. You may return more
  125. * instances or more properties than are requested and WinMgmt
  126. * will post filter out any that do not apply.
  127. *
  128. *****************************************************************************/
  129. HRESULT CVolumeQuotaSetting :: ExecQuery (
  130. MethodContext *pMethodContext,
  131. CFrameworkQuery &Query,
  132. long lFlags
  133. )
  134. {
  135. // Queries involving only one Keyvalue VolumeObjectPath is implemented. Query involving the UserObjectPath is not
  136. // implemented. Since for this we will have to have to enumerate all the volumes until a user is found.
  137. HRESULT hRes = WBEM_S_NO_ERROR;
  138. CHStringArray t_Values;
  139. // Now a check for the LogicalDIsk attribute which if present in where clause the query optimization is supported
  140. hRes = Query.GetValuesForProp(
  141. IDS_Setting,
  142. t_Values
  143. );
  144. if ( SUCCEEDED ( hRes ) )
  145. {
  146. if ( t_Values.GetSize() == 0 )
  147. {
  148. hRes = Query.GetValuesForProp(
  149. IDS_LogicalDisk,
  150. t_Values
  151. );
  152. if ( SUCCEEDED ( hRes ) )
  153. {
  154. if ( t_Values.GetSize() == 0 )
  155. {
  156. //Let Winmgmt handle this, since anyway all the volumes will be enumerated.
  157. hRes = WBEM_E_PROVIDER_NOT_CAPABLE;
  158. }
  159. }
  160. }
  161. }
  162. if ( SUCCEEDED ( hRes ) )
  163. {
  164. int iSize = t_Values.GetSize ();
  165. // In this loop picking up one by one the VolumePath, getting the properties of those volumepath
  166. for ( int i = 0; i < iSize; i++ )
  167. {
  168. WCHAR w_Drive;
  169. hRes = m_CommonRoutine.GetVolume ( t_Values.GetAt(i), w_Drive );
  170. if ( SUCCEEDED ( hRes ) )
  171. {
  172. CHString t_VolumePath;
  173. CHString t_DeviceId;
  174. // In this loop I need to parse the object path
  175. t_VolumePath.Format ( L"%c%s", w_Drive, L":\\" );
  176. // Forming a Logical Disk Key Value
  177. t_DeviceId.Format( L"%c%c", w_Drive, _L(':') );
  178. hRes = PutNewInstance ( t_DeviceId.GetBuffer ( t_DeviceId.GetLength() + 1) ,
  179. t_VolumePath.GetBuffer( t_VolumePath.GetLength() + 1),
  180. pMethodContext );
  181. if ( FAILED (hRes) )
  182. {
  183. break;
  184. }
  185. }
  186. // otherwise continue with thenext drive
  187. }
  188. }
  189. return hRes;
  190. }
  191. /*****************************************************************************
  192. *
  193. * FUNCTION : CVolumeQuotaSetting::EnumerateAllVolumeQuotas
  194. *
  195. * DESCRIPTION : Enumerates all the volumes that supports disk Quotas
  196. *
  197. *****************************************************************************/
  198. HRESULT CVolumeQuotaSetting::EnumerateAllVolumeQuotas (
  199. MethodContext *pMethodContext
  200. )
  201. {
  202. HRESULT hRes = WBEM_S_NO_ERROR;
  203. // verify this logical drives actually exists
  204. CHString t_DriveStrings1;
  205. CHString t_DriveStrings2;
  206. LPWSTR lpDriveStrings = t_DriveStrings1.GetBuffer ( MAX_PATH + 1 );
  207. DWORD dwDLength = GetLogicalDriveStrings ( MAX_PATH, lpDriveStrings );
  208. if ( dwDLength > MAX_PATH )
  209. {
  210. lpDriveStrings = t_DriveStrings2.GetBuffer ( dwDLength + 1 );
  211. dwDLength = GetLogicalDriveStrings ( dwDLength, lpDriveStrings );
  212. }
  213. // Here for every drive, getting a volumePath for Win32_DiskVolume Class and DeviceId for Logical Disk Class
  214. LPWSTR lpTempDriveStrings;
  215. CHString t_VolumePath;
  216. CHString t_DeviceId;
  217. lpTempDriveStrings = lpDriveStrings;
  218. int iLen = lstrlen ( lpTempDriveStrings );
  219. while ( iLen > 0 )
  220. {
  221. t_VolumePath = lpTempDriveStrings;
  222. t_DeviceId = lpTempDriveStrings;
  223. lpTempDriveStrings = &lpTempDriveStrings [ iLen + 1];
  224. t_DeviceId.SetAt ( t_DeviceId.GetLength() - 1,L'\0' );
  225. iLen = lstrlen ( lpTempDriveStrings );
  226. hRes = PutNewInstance ( t_DeviceId.GetBuffer ( t_DeviceId.GetLength() + 1),
  227. t_VolumePath.GetBuffer ( t_VolumePath.GetLength() + 1),
  228. pMethodContext);
  229. if ( FAILED ( hRes ) )
  230. break;
  231. }
  232. return hRes;
  233. }
  234. /*****************************************************************************
  235. *
  236. * FUNCTION : CVolumeQuotaSetting::PutNewInstance
  237. *
  238. * DESCRIPTION : Sets the properties into a new instance
  239. *
  240. *****************************************************************************/
  241. HRESULT CVolumeQuotaSetting::PutNewInstance (
  242. LPWSTR a_DeviceId,
  243. LPWSTR a_VolumePath,
  244. MethodContext *pMethodContext
  245. )
  246. {
  247. HRESULT hRes = WBEM_S_NO_ERROR;
  248. LPWSTR lpTempVolumePath;
  249. LPWSTR lpTempDeviceID;
  250. m_CommonRoutine.MakeObjectPath ( lpTempVolumePath, IDS_DiskVolumeClass, IDS_VolumePath, a_VolumePath );
  251. m_CommonRoutine.MakeObjectPath ( lpTempDeviceID, IDS_LogicalDiskClass, IDS_DeviceID, a_DeviceId );
  252. if ( ( lpTempVolumePath != NULL ) && ( lpTempDeviceID != NULL ) )
  253. {
  254. try
  255. {
  256. CInstancePtr pInstance = CreateNewInstance ( pMethodContext ) ;
  257. if ( pInstance->SetCHString ( IDS_LogicalDisk, lpTempDeviceID ) )
  258. {
  259. if ( pInstance->SetCHString ( IDS_Setting, lpTempVolumePath ) )
  260. {
  261. hRes = pInstance->Commit ();
  262. }
  263. else
  264. {
  265. hRes = WBEM_E_FAILED;
  266. }
  267. }
  268. else
  269. {
  270. hRes = WBEM_E_FAILED;
  271. }
  272. }
  273. catch ( ... )
  274. {
  275. delete [] lpTempVolumePath;
  276. lpTempVolumePath = NULL;
  277. delete [] lpTempDeviceID;
  278. lpTempDeviceID = NULL;
  279. throw;
  280. }
  281. if ( lpTempVolumePath != NULL )
  282. {
  283. delete [] lpTempVolumePath;
  284. }
  285. if ( lpTempDeviceID != NULL )
  286. {
  287. delete [] lpTempDeviceID;
  288. }
  289. }
  290. return hRes;
  291. }