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.

312 lines
8.1 KiB

  1. /******************************************************************
  2. DskCommonRoutines.CPP --
  3. Description: Common routines that are used by all the three
  4. classes of Disk Quota Provider
  5. Copyright (c) 2000-2001 Microsoft Corporation, All Rights Reserved
  6. ******************************************************************/
  7. #include "precomp.h"
  8. #include "DskQuotaCommon.h"
  9. /*****************************************************************************
  10. *
  11. * FUNCTION : DskCommonRoutines:: GetVolume
  12. *
  13. * DESCRIPTION : This method Parses the key and gets the Volume from
  14. * the Object Path. The input of this method could be
  15. * Logical Disk key which is on the form "D:" or could
  16. * receive volume Path which is in the form "D:\"
  17. *
  18. *****************************************************************************/
  19. HRESULT DskCommonRoutines::GetVolume (
  20. LPCWSTR a_Key,
  21. WCHAR &a_Drive
  22. )
  23. {
  24. HRESULT hRes = WBEM_E_INVALID_PARAMETER;
  25. CObjectPathParser t_PathParser;
  26. ParsedObjectPath *t_ObjPath = NULL;
  27. if ( t_PathParser.Parse( a_Key, &t_ObjPath ) == t_PathParser.NoError )
  28. {
  29. try
  30. {
  31. CHString t_KeyString = t_ObjPath->GetKeyString();
  32. // checking for the validity of the path
  33. if ( ( t_KeyString.GetLength() == 3 ) || (t_KeyString.GetLength() == 2 ) )
  34. {
  35. if ( (( t_KeyString.GetAt(0) >= L'A') && ( t_KeyString.GetAt(0) <= L'Z')) || (( t_KeyString.GetAt(0) >= L'a') && ( t_KeyString.GetAt(0) <= L'z') ) )
  36. {
  37. if ( t_KeyString.GetAt(1) == L':' )
  38. {
  39. if ( t_KeyString.GetLength() == 3 )
  40. {
  41. if ( t_KeyString.GetAt(2) == L'\\' )
  42. {
  43. hRes = WBEM_S_NO_ERROR;
  44. }
  45. }
  46. else
  47. {
  48. hRes = WBEM_S_NO_ERROR;
  49. }
  50. a_Drive = t_KeyString.GetAt(0);
  51. }
  52. }
  53. }
  54. }
  55. catch ( ... )
  56. {
  57. t_PathParser.Free ( t_ObjPath );
  58. throw;
  59. }
  60. t_PathParser.Free ( t_ObjPath );
  61. }
  62. return hRes;
  63. }
  64. /*****************************************************************************
  65. *
  66. * FUNCTION : DskCommonRoutines:: SearchLogicalDisk
  67. *
  68. * DESCRIPTION : This method Searches whether a given Logical disks exists
  69. * in the logicaldisks strings of the system
  70. *
  71. *****************************************************************************/
  72. HRESULT DskCommonRoutines::SearchLogicalDisk (
  73. WCHAR a_Drive,
  74. LPCWSTR a_DriveStrings
  75. )
  76. {
  77. int iLen = 0;
  78. LPCWSTR lpTempDriveString = a_DriveStrings;
  79. HRESULT hRes = WBEM_S_NO_ERROR;
  80. a_Drive = (WCHAR)toupper(a_Drive);
  81. while ( true )
  82. {
  83. iLen = lstrlen ( lpTempDriveString );
  84. if ( iLen == 0 )
  85. {
  86. hRes = WBEM_E_NOT_FOUND;
  87. break;
  88. }
  89. if ( lpTempDriveString [ 0 ] == a_Drive )
  90. break;
  91. lpTempDriveString = &lpTempDriveString [ iLen + 1 ];
  92. }
  93. return hRes;
  94. }
  95. /*****************************************************************************
  96. *
  97. * FUNCTION : DskCommonRoutines:: GetVolumeDrive
  98. *
  99. * DESCRIPTION : Gets the Volume Drive from the Given Path
  100. *
  101. *****************************************************************************/
  102. void DskCommonRoutines::GetVolumeDrive (
  103. LPCWSTR a_VolumePath,
  104. LPCWSTR a_DriveStrings,
  105. CHString &a_DriveName
  106. )
  107. {
  108. int iLen = 0;
  109. WCHAR w_Drive[ 4 ];
  110. LPCWSTR lpTempDriveString = a_DriveStrings;
  111. WCHAR t_TempVolumeName [ MAX_PATH + 1 ];
  112. while ( true )
  113. {
  114. iLen = lstrlen ( lpTempDriveString );
  115. if ( iLen == 0 )
  116. break;
  117. lstrcpy ( w_Drive, lpTempDriveString );
  118. BOOL bVol = GetVolumeNameForVolumeMountPoint(
  119. w_Drive,
  120. t_TempVolumeName,
  121. MAX_PATH
  122. );
  123. if ( lstrcmp ( t_TempVolumeName, a_VolumePath ) == 0 )
  124. {
  125. a_DriveName = w_Drive;
  126. break;
  127. }
  128. lpTempDriveString = &lpTempDriveString [ iLen + 1 ];
  129. }
  130. }
  131. /*****************************************************************************
  132. *
  133. * FUNCTION : DskCommonRoutines:: InitializeInterfacePointer
  134. *
  135. * DESCRIPTION : This method Initializes the DiskQuotaInterface pointer for a
  136. * given volume
  137. *
  138. *****************************************************************************/
  139. HRESULT DskCommonRoutines::InitializeInterfacePointer (
  140. IDiskQuotaControl* pIQuotaControl,
  141. LPCWSTR a_VolumeName
  142. )
  143. {
  144. HRESULT hRes = WBEM_S_NO_ERROR;
  145. BOOL bRetVal = TRUE;
  146. WCHAR w_VolumePathName [ MAX_PATH + 1 ];
  147. bRetVal = GetVolumePathName(
  148. a_VolumeName, // file path
  149. w_VolumePathName, // volume mount point
  150. MAX_PATH // Size of the Buffer
  151. );
  152. if ( bRetVal )
  153. {
  154. if ( FAILED ( pIQuotaControl->Initialize ( w_VolumePathName, TRUE ) ) )
  155. {
  156. hRes = WBEM_E_FAILED;
  157. }
  158. }
  159. return hRes;
  160. }
  161. /*****************************************************************************
  162. *
  163. * FUNCTION : DskCommonRoutines:: VolumeSupportsDiskQuota
  164. *
  165. * DESCRIPTION : This method checks if the volume supports Disk Quotas,
  166. *
  167. *****************************************************************************/
  168. HRESULT DskCommonRoutines::VolumeSupportsDiskQuota (
  169. LPCWSTR a_VolumeName,
  170. CHString &a_QuotaVolumeName
  171. )
  172. {
  173. // Get the name of the Volume Name Property
  174. LPWSTR t_VolumeNameBuffer = a_QuotaVolumeName.GetBuffer(MAX_PATH + 1);
  175. DWORD dwMaximumComponentLength = 0;
  176. DWORD dwFileSystemFlags = 0;
  177. HRESULT hRes = WBEM_S_NO_ERROR;
  178. BOOL bRetVal = GetVolumeInformation(
  179. a_VolumeName, // root directory
  180. t_VolumeNameBuffer, // volume name buffer
  181. MAX_PATH, // length of name buffer
  182. NULL, // volume serial number
  183. &dwMaximumComponentLength, // maximum file name length
  184. &dwFileSystemFlags, // file system options
  185. NULL, // file system name buffer
  186. 0 // length of file system name buffer
  187. );
  188. if ( ( bRetVal ) && ( ( dwFileSystemFlags & FILE_VOLUME_QUOTAS) == FILE_VOLUME_QUOTAS ))
  189. {
  190. a_QuotaVolumeName = t_VolumeNameBuffer;
  191. }
  192. else
  193. {
  194. hRes = WBEM_E_NOT_FOUND;
  195. }
  196. a_QuotaVolumeName.ReleaseBuffer();
  197. return hRes;
  198. }
  199. /*****************************************************************************
  200. *
  201. * FUNCTION : DskCommonRoutines:: MakeObjectPath
  202. *
  203. * DESCRIPTION : This method Adds a keyvalue for a given key property
  204. * into the Object Path and gives the Object Path
  205. *
  206. *****************************************************************************/
  207. void DskCommonRoutines::MakeObjectPath (
  208. LPWSTR &a_ObjPathString,
  209. LPWSTR a_ClassName,
  210. LPCWSTR a_AttributeName,
  211. LPCWSTR a_AttributeVal
  212. )
  213. {
  214. ParsedObjectPath t_ObjPath;
  215. variant_t t_Path(a_AttributeVal);
  216. t_ObjPath.SetClassName ( a_ClassName );
  217. t_ObjPath.AddKeyRef ( a_AttributeName, &t_Path );
  218. CObjectPathParser t_PathParser;
  219. if ( t_PathParser.Unparse( &t_ObjPath, &a_ObjPathString ) != t_PathParser.NoError )
  220. {
  221. a_ObjPathString = NULL;
  222. }
  223. }
  224. /*****************************************************************************
  225. *
  226. * FUNCTION : DskCommonRoutines:: AddToObjectPath
  227. *
  228. * DESCRIPTION : This method Adds a keyvalue for a given key property
  229. * into the existing Object Path.
  230. *
  231. *****************************************************************************/
  232. void DskCommonRoutines::AddToObjectPath (
  233. LPWSTR &a_ObjPathString,
  234. LPCWSTR a_AttributeName,
  235. LPCWSTR a_AttributeVal
  236. )
  237. {
  238. CObjectPathParser t_PathParser;
  239. ParsedObjectPath *t_ObjPath;
  240. if ( t_PathParser.Parse( a_ObjPathString, &t_ObjPath ) == t_PathParser.NoError )
  241. {
  242. try
  243. {
  244. variant_t t_Path(a_AttributeVal);
  245. t_ObjPath->AddKeyRef ( a_AttributeName, &t_Path );
  246. LPWSTR t_ObjPathString = NULL;
  247. delete [] a_ObjPathString;
  248. a_ObjPathString = NULL;
  249. if ( t_PathParser.Unparse( t_ObjPath, &t_ObjPathString ) != t_PathParser.NoError )
  250. {
  251. a_ObjPathString = NULL;
  252. }
  253. else
  254. {
  255. a_ObjPathString = t_ObjPathString;
  256. }
  257. }
  258. catch ( ... )
  259. {
  260. t_PathParser.Free (t_ObjPath);
  261. throw;
  262. }
  263. t_PathParser.Free (t_ObjPath);
  264. }
  265. }