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.

197 lines
6.8 KiB

  1. // metatool.cpp : implementation file
  2. //
  3. // some common tools used for "smart" writing to the metabase
  4. #include "stdafx.h"
  5. #include <iadmw.h>
  6. #define _COMSTATIC
  7. #include <comprop.h>
  8. #include <idlg.h>
  9. #include "wrapmb.h"
  10. #include "metatool.h"
  11. //----------------------------------------------------------------
  12. // open the metabase with an option to create the directory if it doesn't
  13. // exist. It would be nice to move this into wrapmb, but that is too big
  14. // a change for now. Maybe we can do that later.
  15. BOOL OpenAndCreate( CWrapMetaBase* pmb, LPCTSTR pszTarget, DWORD perm, BOOL fCreate )
  16. {
  17. BOOL f;
  18. CString szTarget = pszTarget;
  19. // start by just trying to open it. easy easy.
  20. if ( pmb->Open(szTarget, perm) )
  21. return TRUE;
  22. // if requested, try to create the key if it doesn't exist
  23. if ( fCreate )
  24. {
  25. // find the nearest openable parent directory and open it
  26. CString szPartial;
  27. CString szBase = szTarget;
  28. do
  29. {
  30. szBase = szBase.Left( szBase.ReverseFind(_T('/')) );
  31. szPartial = szTarget.Right( szTarget.GetLength() - szBase.GetLength() - 1 );
  32. f = pmb->Open( szBase, METADATA_PERMISSION_WRITE | perm );
  33. } while (!f && !szBase.IsEmpty());
  34. // if all that failed, fail
  35. if ( !f ) return FALSE;
  36. // create the key that we really want
  37. f = pmb->AddObject( szPartial );
  38. pmb->Close();
  39. // if all that failed, fail
  40. if ( !f ) return FALSE;
  41. // try again
  42. if ( pmb->Open(szTarget, perm) )
  43. return TRUE;
  44. }
  45. // total washout
  46. return FALSE;
  47. }
  48. //----------------------------------------------------------------
  49. // starting at the root, check for values set on sub-keys that may need to be overridden
  50. // and propmt the user for what to do
  51. void CheckInheritence( LPCTSTR pszServer, LPCTSTR pszInheritRoot, DWORD idData )
  52. {
  53. CInheritanceDlg dlgInherit(
  54. idData,
  55. FROM_WRITE_PROPERTY,
  56. pszServer,
  57. pszInheritRoot
  58. );
  59. // if it worked, then run the dialog
  60. if ( !dlgInherit.IsEmpty() )
  61. dlgInherit.DoModal();
  62. }
  63. // notice that the dwords and generic blobs are handled seperately even though
  64. // we count route the dwords through the blob mechanisms. This is done for two
  65. // reasone. 1) Handling dwords is much more efficient than handling blobs.
  66. // and 2) Most of the values are dwords.
  67. //----------------------------------------------------------------
  68. // opens the metabase, writes out the value, then uses the inheritence
  69. // checking functionality from the iisui.dll to check for the inherited
  70. // properties and propt the user for what to do
  71. BOOL SetMetaDword(IMSAdminBase* pMB, LPCTSTR pszServer, LPCTSTR pszMetaRoot, LPCTSTR pszSub, DWORD idData, DWORD iType, DWORD dwValue, BOOL fCheckInheritence)
  72. {
  73. BOOL fAnswer = FALSE;
  74. BOOL fChanged = TRUE;
  75. DWORD dword;
  76. CWrapMetaBase mbWrap;
  77. if ( !mbWrap.FInit(pMB) ) return FALSE;
  78. // open the target
  79. if ( OpenAndCreate( &mbWrap, pszMetaRoot,
  80. METADATA_PERMISSION_WRITE | METADATA_PERMISSION_READ, TRUE ) )
  81. {
  82. // attempt to get the current value - no inheritence
  83. if ( mbWrap.GetDword(pszSub, idData, iType, &dword) )
  84. {
  85. // set the changed flag
  86. fChanged = (dwValue != dword);
  87. }
  88. // save it out, if it changed or is not there
  89. if ( fChanged )
  90. fAnswer = mbWrap.SetDword( pszSub, idData, iType, dwValue );
  91. // close the metabase
  92. mbWrap.Close();
  93. }
  94. else
  95. fChanged = FALSE;
  96. // set up and run the inheritence checking dialog
  97. if ( fCheckInheritence && fChanged )
  98. {
  99. CString szInheritRoot = pszMetaRoot;
  100. szInheritRoot += pszSub;
  101. CheckInheritence( pszServer, szInheritRoot, idData );
  102. }
  103. return fAnswer;
  104. }
  105. //----------------------------------------------------------------
  106. // assumes that the metabase is actually open to the parent of the one we are interested
  107. // and that the real target name is passed into szSub
  108. BOOL SetMetaData(IMSAdminBase* pMB, LPCTSTR pszServer, LPCTSTR pszMetaRoot, LPCTSTR pszSub, DWORD idData, DWORD iType, DWORD iDataType, PVOID pData, DWORD cbData, BOOL fCheckInheritence )
  109. {
  110. BOOL fAnswer = FALSE;
  111. BOOL fChanged = TRUE;
  112. DWORD cbTestData;
  113. CWrapMetaBase mbWrap;
  114. if ( !mbWrap.FInit(pMB) ) return FALSE;
  115. // open the target
  116. if ( OpenAndCreate( &mbWrap, pszMetaRoot,
  117. METADATA_PERMISSION_WRITE | METADATA_PERMISSION_READ, TRUE ) )
  118. {
  119. // attempt to get the current value - no inheritence
  120. PVOID pTestData = mbWrap.GetData( pszSub, idData, iType,
  121. iDataType, &cbTestData );
  122. if ( pTestData )
  123. {
  124. // set the changed flag
  125. if ( cbData == cbTestData )
  126. {
  127. fChanged = (memcmp(pData, pTestData, cbData) != 0);
  128. }
  129. mbWrap.FreeWrapData( pTestData );
  130. }
  131. // save it out, if it changed or is not there
  132. if ( fChanged )
  133. fAnswer = mbWrap.SetData( pszSub, idData, iType, iDataType, pData, cbData );
  134. // close the metabase
  135. mbWrap.Close();
  136. }
  137. else
  138. fChanged = FALSE;
  139. // set up and run the inheritence checking dialog
  140. if ( fCheckInheritence && fChanged )
  141. {
  142. CString szInheritRoot = pszMetaRoot;
  143. szInheritRoot += pszSub;
  144. CheckInheritence( pszServer, szInheritRoot, idData );
  145. }
  146. return fAnswer;
  147. }
  148. //----------------------------------------------------------------
  149. // assumes that the metabase is actually open to the parent of the one we are interested
  150. // and that the real target name is passed into szSub
  151. BOOL SetMetaString(IMSAdminBase* pMB, LPCTSTR pszServer, LPCTSTR pszMetaRoot, LPCTSTR pszSub, DWORD idData, DWORD iType, CString sz, BOOL fCheckInheritence)
  152. {
  153. return SetMetaData(pMB, pszServer, pszMetaRoot, pszSub, idData,
  154. iType, STRING_METADATA, (LPTSTR)(LPCTSTR)sz,
  155. (sz.GetLength()+1)*sizeof(TCHAR), fCheckInheritence );
  156. }
  157. //----------------------------------------------------------------
  158. // assumes that the metabase is actually open to the parent of the one we are interested
  159. // and that the real target name is passed into szSub
  160. // the cchmsz is the total count of characters in the multi string including the nulls
  161. BOOL SetMetaMultiSz(IMSAdminBase* pMB, LPCTSTR pszServer, LPCTSTR pszMetaRoot, LPCTSTR pszSub, DWORD idData, DWORD iType, PVOID pData, DWORD cchmsz, BOOL fCheckInheritence )
  162. {
  163. return SetMetaData(pMB, pszServer, pszMetaRoot, pszSub, idData,
  164. iType, MULTISZ_METADATA, pData, (cchmsz+1)*sizeof(TCHAR), fCheckInheritence );
  165. }