Source code of Windows XP (NT5)
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.

671 lines
15 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name :
  4. mb.hxx
  5. Abstract:
  6. This module defines the USER-level wrapper class for access to the
  7. metabase
  8. Author:
  9. JohnL 09-Oct-1996
  10. Environment:
  11. Win32 - User Mode
  12. Project:
  13. Internet Server DLL
  14. Revision History:
  15. --*/
  16. #ifndef _MB_HXX_
  17. #define _MB_HXX_
  18. #if !defined( dllexp)
  19. #define dllexp __declspec( dllexport)
  20. #endif // !defined( dllexp)
  21. //
  22. // Default timeout
  23. //
  24. #define MB_TIMEOUT 5000
  25. //
  26. // IIS Service pointer
  27. //
  28. #define PINETSVC g_pInetSvc
  29. /************************************************************
  30. * Type Definitions
  31. ************************************************************/
  32. //
  33. // Simple wrapper class around the metabase APIs
  34. //
  35. // The Metabase Interface pointer is assumed to remain valid for the lifetime
  36. // of this object.
  37. //
  38. // The character counts for paths should include the terminating '\0'.
  39. //
  40. //
  41. class MB
  42. {
  43. public:
  44. MB( IMSAdminBase * pMBCom )
  45. : _pMBCom( pMBCom ),
  46. _hMB ( NULL )
  47. {
  48. }
  49. ~MB( VOID )
  50. {
  51. Close();
  52. _pMBCom = NULL;
  53. }
  54. inline
  55. BOOL Open( const TCHAR * pszPath,
  56. DWORD dwFlags = METADATA_PERMISSION_READ )
  57. {
  58. return Open( METADATA_MASTER_ROOT_HANDLE,
  59. pszPath,
  60. dwFlags );
  61. }
  62. inline
  63. BOOL Open( METADATA_HANDLE hOpenRoot,
  64. const TCHAR * pszPath,
  65. DWORD dwFlags = METADATA_PERMISSION_READ );
  66. /*
  67. inline
  68. BOOL GetAll( const TCHAR * pszPath,
  69. DWORD dwFlags,
  70. DWORD dwUserType,
  71. BUFFER * pBuff,
  72. DWORD * pcRecords,
  73. DWORD * pdwDataSetNumber );
  74. */
  75. inline
  76. BOOL GetDataSetNumber( const TCHAR * pszPath,
  77. DWORD * pdwDataSetNumber );
  78. inline
  79. BOOL EnumObjects( const TCHAR * pszPath,
  80. TCHAR * Name,
  81. DWORD Index )
  82. {
  83. HRESULT hRes = _pMBCom->EnumKeys( _hMB,pszPath, Name, Index );
  84. if ( SUCCEEDED( hRes ))
  85. {
  86. return TRUE;
  87. }
  88. SetLastError( HRESULTTOWIN32( hRes ));
  89. return FALSE;
  90. }
  91. inline
  92. BOOL AddObject( const TCHAR * pszPath )
  93. {
  94. HRESULT hRes = _pMBCom->AddKey( _hMB, pszPath );
  95. if ( SUCCEEDED( hRes ))
  96. {
  97. return TRUE;
  98. }
  99. SetLastError( HRESULTTOWIN32( hRes ));
  100. return FALSE;
  101. }
  102. //----------------------------------------------------------------- boydm
  103. inline
  104. BOOL RenameKey( const TCHAR* pszPath, const TCHAR* pszNewName )
  105. {
  106. HRESULT hRes = _pMBCom->RenameKey( _hMB, pszPath, pszNewName );
  107. if ( SUCCEEDED( hRes ))
  108. {
  109. return TRUE;
  110. }
  111. SetLastError( HRESULTTOWIN32( hRes ));
  112. return FALSE;
  113. }
  114. inline
  115. BOOL DeleteObject( const TCHAR * pszPath )
  116. {
  117. HRESULT hRes = _pMBCom->DeleteKey( _hMB, pszPath );
  118. if ( SUCCEEDED( hRes ))
  119. {
  120. return TRUE;
  121. }
  122. SetLastError( HRESULTTOWIN32( hRes ));
  123. return FALSE;
  124. }
  125. inline
  126. BOOL Save( VOID )
  127. {
  128. HRESULT hRes = _pMBCom->SaveData();
  129. if ( SUCCEEDED( hRes ))
  130. {
  131. return TRUE;
  132. }
  133. SetLastError( HRESULTTOWIN32( hRes ));
  134. return FALSE;
  135. }
  136. BOOL SetDword( const TCHAR * pszPath,
  137. DWORD dwPropID,
  138. DWORD dwUserType,
  139. DWORD dwValue,
  140. DWORD dwFlags = METADATA_INHERIT )
  141. {
  142. return SetData( pszPath,
  143. dwPropID,
  144. dwUserType,
  145. DWORD_METADATA,
  146. (PVOID) &dwValue,
  147. sizeof( DWORD ),
  148. dwFlags );
  149. }
  150. BOOL SetString( const TCHAR * pszPath,
  151. DWORD dwPropID,
  152. DWORD dwUserType,
  153. TCHAR * pszValue,
  154. DWORD dwFlags = METADATA_INHERIT )
  155. {
  156. #ifdef _UNICODE
  157. return SetData( pszPath,
  158. dwPropID,
  159. dwUserType,
  160. STRING_METADATA,
  161. pszValue,
  162. (wcslen(pszValue)+1) * 2, // byte count, not character count
  163. dwFlags );
  164. #else
  165. return SetData( pszPath,
  166. dwPropID,
  167. dwUserType,
  168. STRING_METADATA,
  169. pszValue,
  170. strlen(pszValue)+1, // string length ignored for inprocess clients
  171. dwFlags );
  172. #endif
  173. }
  174. BOOL GetDword( const TCHAR * pszPath,
  175. DWORD dwPropID,
  176. DWORD dwUserType,
  177. DWORD * pdwValue,
  178. DWORD dwFlags = METADATA_INHERIT )
  179. {
  180. DWORD cb = sizeof(DWORD);
  181. return GetData( pszPath,
  182. dwPropID,
  183. dwUserType,
  184. DWORD_METADATA,
  185. pdwValue,
  186. &cb,
  187. dwFlags );
  188. }
  189. BOOL GetString( const TCHAR * pszPath,
  190. DWORD dwPropID,
  191. DWORD dwUserType,
  192. TCHAR * pszValue,
  193. DWORD * pcbValue,
  194. DWORD dwFlags = METADATA_INHERIT )
  195. {
  196. return GetData( pszPath,
  197. dwPropID,
  198. dwUserType,
  199. STRING_METADATA,
  200. pszValue,
  201. pcbValue,
  202. dwFlags );
  203. }
  204. /*
  205. inline
  206. BOOL GetStr( const TCHAR * pszPath,
  207. DWORD dwPropID,
  208. DWORD dwUserType,
  209. STR * strValue,
  210. DWORD dwFlags = METADATA_INHERIT,
  211. const TCHAR * pszDefault = NULL );
  212. */
  213. inline
  214. BOOL SetData( const TCHAR * pszPath,
  215. DWORD dwPropID,
  216. DWORD dwUserType,
  217. DWORD dwDataType,
  218. VOID * pvData,
  219. DWORD cbData,
  220. DWORD dwFlags = METADATA_INHERIT );
  221. inline
  222. BOOL GetData( const TCHAR * pszPath,
  223. DWORD dwPropID,
  224. DWORD dwUserType,
  225. DWORD dwDataType,
  226. VOID * pvData,
  227. DWORD * cbData,
  228. DWORD dwFlags = METADATA_INHERIT );
  229. inline
  230. BOOL DeleteData(const TCHAR * pszPath,
  231. DWORD dwPropID,
  232. DWORD dwUserType,
  233. DWORD dwDataType )
  234. {
  235. HRESULT hRes = _pMBCom->DeleteData( _hMB, pszPath,
  236. dwPropID,
  237. dwDataType );
  238. if ( SUCCEEDED( hRes ))
  239. {
  240. return TRUE;
  241. }
  242. SetLastError( HRESULTTOWIN32( hRes ));
  243. return(FALSE);
  244. }
  245. BOOL Close( VOID )
  246. {
  247. if ( _hMB )
  248. {
  249. _pMBCom->CloseKey( _hMB );
  250. _hMB = NULL;
  251. }
  252. return TRUE;
  253. }
  254. METADATA_HANDLE QueryHandle( VOID ) const
  255. { return _hMB; }
  256. private:
  257. IMSAdminBase * _pMBCom;
  258. METADATA_HANDLE _hMB;
  259. };
  260. inline
  261. BOOL
  262. MB::Open(
  263. METADATA_HANDLE hOpenRoot,
  264. const TCHAR * pszPath,
  265. DWORD dwFlags
  266. )
  267. /*++
  268. Routine Description:
  269. Opens the metabase
  270. Arguments:
  271. hOpenRoot - Relative root or METADATA_MASTER_ROOT_HANDLE
  272. pszPath - Path to open
  273. dwFlags - Open flags
  274. Return:
  275. TRUE if success, FALSE on error, (call GetLastError())
  276. --*/
  277. {
  278. HRESULT hRes;
  279. hRes = _pMBCom->OpenKey( hOpenRoot, pszPath,
  280. dwFlags,
  281. MB_TIMEOUT,
  282. &_hMB );
  283. if ( SUCCEEDED( hRes ))
  284. {
  285. return TRUE;
  286. }
  287. SetLastError( HRESULTTOWIN32( hRes ) );
  288. return FALSE;
  289. }
  290. inline
  291. BOOL
  292. MB::SetData(
  293. const TCHAR * pszPath,
  294. DWORD dwPropID,
  295. DWORD dwUserType,
  296. DWORD dwDataType,
  297. VOID * pvData,
  298. DWORD cbData,
  299. DWORD dwFlags
  300. )
  301. /*++
  302. Routine Description:
  303. Sets a metadata property on an openned metabase
  304. Arguments:
  305. pszPath - Path to set data on
  306. dwPropID - Metabase property ID
  307. dwUserType - User type for this property
  308. dwDataType - Type of data being set (dword, string etc)
  309. pvData - Pointer to data
  310. cbData - Size of data
  311. dwFlags - Inheritance flags
  312. Return:
  313. TRUE if success, FALSE on error, (call GetLastError())
  314. --*/
  315. {
  316. METADATA_RECORD mdRecord;
  317. HRESULT hRes;
  318. mdRecord.dwMDIdentifier = dwPropID;
  319. mdRecord.dwMDAttributes = dwFlags;
  320. mdRecord.dwMDUserType = dwUserType;
  321. mdRecord.dwMDDataType = dwDataType;
  322. mdRecord.dwMDDataLen = cbData;
  323. mdRecord.pbMDData = (PBYTE) pvData;
  324. hRes = _pMBCom->SetData( _hMB, pszPath,
  325. &mdRecord );
  326. if ( SUCCEEDED( hRes ))
  327. {
  328. return TRUE;
  329. }
  330. SetLastError( HRESULTTOWIN32( hRes ) );
  331. return FALSE;
  332. }
  333. inline
  334. BOOL
  335. MB::GetData(
  336. const TCHAR * pszPath,
  337. DWORD dwPropID,
  338. DWORD dwUserType,
  339. DWORD dwDataType,
  340. VOID * pvData,
  341. DWORD * pcbData,
  342. DWORD dwFlags
  343. )
  344. /*++
  345. Routine Description:
  346. Retrieves a metadata property on an openned metabase
  347. Arguments:
  348. pszPath - Path to set data on
  349. dwPropID - Metabase property ID
  350. dwUserType - User type for this property
  351. dwDataType - Type of data being set (dword, string etc)
  352. pvData - Pointer to data
  353. pcbData - Size of pvData, receives size of object
  354. dwFlags - Inheritance flags
  355. Return:
  356. TRUE if success, FALSE on error, (call GetLastError())
  357. --*/
  358. {
  359. METADATA_RECORD mdRecord;
  360. HRESULT hRes;
  361. DWORD dwRequiredLen;
  362. mdRecord.dwMDIdentifier = dwPropID;
  363. mdRecord.dwMDAttributes = dwFlags;
  364. mdRecord.dwMDUserType = dwUserType;
  365. mdRecord.dwMDDataType = dwDataType;
  366. mdRecord.dwMDDataLen = *pcbData;
  367. mdRecord.pbMDData = (PBYTE) pvData;
  368. hRes = _pMBCom->GetData( _hMB, pszPath,
  369. &mdRecord,
  370. &dwRequiredLen );
  371. if ( SUCCEEDED( hRes ))
  372. {
  373. *pcbData = mdRecord.dwMDDataLen;
  374. return TRUE;
  375. }
  376. *pcbData = dwRequiredLen;
  377. SetLastError( HRESULTTOWIN32( hRes ) );
  378. return FALSE;
  379. }
  380. #ifdef O
  381. inline
  382. BOOL MB::GetAll(
  383. const TCHAR * pszPath,
  384. DWORD dwFlags,
  385. DWORD dwUserType,
  386. BUFFER * pBuff,
  387. DWORD * pcRecords,
  388. DWORD * pdwDataSetNumber
  389. )
  390. /*++
  391. Routine Description:
  392. Retrieves all the metabase properties on this path of the request type
  393. Arguments:
  394. pszPath - Path to set data on
  395. dwFlags - Inerhitance flags
  396. dwPropID - Metabase property ID
  397. dwUserType - User type for this property
  398. dwDataType - Type of data being set (dword, string etc)
  399. pvData - Pointer to data
  400. pcbData - Size of pvData, receives size of object
  401. dwFlags - Inheritance flags
  402. Return:
  403. TRUE if success, FALSE on error, (call GetLastError())
  404. --*/
  405. {
  406. DWORD RequiredSize;
  407. HRESULT hRes;
  408. TryAgain:
  409. hRes = _pMBCom->GetAllData( _hMB,
  410. (unsigned TCHAR *)pszPath,
  411. dwFlags,
  412. dwUserType,
  413. ALL_METADATA,
  414. pcRecords,
  415. pdwDataSetNumber,
  416. pBuff->QuerySize(),
  417. (PBYTE)pBuff->QueryPtr(),
  418. &RequiredSize
  419. );
  420. // See if we got it, and if we failed because of lack of buffer space
  421. // try again.
  422. if ( SUCCEEDED(hRes) )
  423. {
  424. return TRUE;
  425. }
  426. // Some sort of error, most likely not enough buffer space. Keep
  427. // trying until we get a non-fatal error.
  428. if (HRESULT_FACILITY(hRes) == FACILITY_WIN32 &&
  429. HRESULT_CODE(hRes) == ERROR_INSUFFICIENT_BUFFER) {
  430. // Not enough buffer space. RequiredSize contains the amount
  431. // the metabase thinks we need.
  432. if ( !pBuff->Resize(RequiredSize) ) {
  433. // Not enough memory to resize.
  434. return FALSE;
  435. }
  436. goto TryAgain;
  437. }
  438. return FALSE;
  439. }
  440. #endif
  441. inline
  442. BOOL MB::GetDataSetNumber(
  443. const TCHAR * pszPath,
  444. DWORD * pdwDataSetNumber
  445. )
  446. /*++
  447. Routine Description:
  448. Retrieves the data set number and size of the data from the
  449. metabase.
  450. Arguments:
  451. pszPath - Path to set data on
  452. pdwDataSetNumber - Where to return the data set number.
  453. Return:
  454. TRUE if success, FALSE on error, (call GetLastError())
  455. --*/
  456. {
  457. HRESULT hRes;
  458. //
  459. // We allow _hMB to be null (root handle) for this API (though technically
  460. // all the APIs allow the metabase handle to be null)
  461. //
  462. hRes = _pMBCom->GetDataSetNumber( _hMB, pszPath, pdwDataSetNumber );
  463. return SUCCEEDED(hRes);
  464. }
  465. #ifdef O
  466. inline
  467. BOOL
  468. MB::GetStr(
  469. const TCHAR * pszPath,
  470. DWORD dwPropID,
  471. DWORD dwUserType,
  472. STR * pstrValue,
  473. DWORD dwFlags,
  474. const TCHAR * pszDefault
  475. )
  476. /*++
  477. Routine Description:
  478. Retrieves the string from the metabase. If the value wasn't found and
  479. a default is supplied, then the default value is copied to the string.
  480. Arguments:
  481. pszPath - Path to get data on
  482. dwPropID - property id to retrieve
  483. dwUserType - User type for this property
  484. pstrValue - string that receives the value
  485. dwFlags - Metabase flags
  486. pszDefault - Default value to use if the string isn't found, NULL
  487. for no default value (i.e., will return an error).
  488. Return:
  489. TRUE if success, FALSE on error, (call GetLastError())
  490. --*/
  491. {
  492. DWORD cbSize = pstrValue->QuerySize();
  493. TryAgain:
  494. if ( !GetData( pszPath,
  495. dwPropID,
  496. dwUserType,
  497. STRING_METADATA,
  498. pstrValue->QueryStr(),
  499. &cbSize,
  500. dwFlags ))
  501. {
  502. if ( GetLastError() == MD_ERROR_DATA_NOT_FOUND )
  503. {
  504. if ( pszDefault != NULL )
  505. {
  506. return pstrValue->Copy( pszDefault );
  507. }
  508. return FALSE;
  509. }
  510. else if ( GetLastError() == ERROR_INSUFFICIENT_BUFFER &&
  511. pstrValue->Resize( cbSize ) )
  512. {
  513. goto TryAgain;
  514. }
  515. return FALSE;
  516. }
  517. pstrValue->SetLen( cbSize );
  518. return TRUE;
  519. }
  520. #endif
  521. #endif // _MB_HXX_