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.

291 lines
7.3 KiB

  1. #include "StdAfx.h"
  2. #include "ADMTScript.h"
  3. #include "FixHierarchy.h"
  4. //---------------------------------------------------------------------------
  5. // Fix Objects in Hierarchy Class Implementation
  6. //---------------------------------------------------------------------------
  7. CFixObjectsInHierarchy::CFixObjectsInHierarchy() :
  8. m_bFixReplaced(false)
  9. {
  10. }
  11. CFixObjectsInHierarchy::~CFixObjectsInHierarchy()
  12. {
  13. }
  14. void CFixObjectsInHierarchy::FixObjects()
  15. {
  16. m_Migrated.RetrieveMigratedObjects();
  17. int nCount = m_Migrated.GetCount();
  18. if (nCount > 0)
  19. {
  20. m_TargetPath.SetContainerPaths(m_strSourceContainerPath, m_strTargetContainerPath);
  21. for (int nIndex = 0; nIndex < nCount; nIndex++)
  22. {
  23. int nStatus = m_Migrated.GetObjectStatus(nIndex);
  24. if ((nStatus & CMigrated::STATUS_CREATED) || (m_bFixReplaced && (nStatus & CMigrated::STATUS_REPLACED)))
  25. {
  26. _bstr_t strType = m_Migrated.GetObjectType(nIndex);
  27. if ((strType.length() > 0) && (_tcsicmp(strType, m_strObjectType) == 0))
  28. {
  29. if (m_TargetPath.NeedsToMove(m_Migrated.GetObjectSourcePath(nIndex), m_Migrated.GetObjectTargetPath(nIndex)))
  30. {
  31. m_Containers.InsertObject(m_TargetPath.GetTargetContainerPath(), nIndex, m_TargetPath.GetTargetObjectCurrentPath());
  32. }
  33. }
  34. }
  35. }
  36. for (CContainers::iterator itContainer = m_Containers.begin(); itContainer != m_Containers.end(); itContainer++)
  37. {
  38. try
  39. {
  40. CADsContainer cContainer((LPCTSTR)itContainer->first);
  41. CObjects& ovObjects = itContainer->second;
  42. for (CObjects::iterator itObject = ovObjects.begin(); itObject != ovObjects.end(); itObject++)
  43. {
  44. try
  45. {
  46. SObject& oObject = *itObject;
  47. CADs account(IADsPtr(cContainer.MoveHere(oObject.m_strPath, _bstr_t())));
  48. m_Migrated.UpdateObjectTargetPath(oObject.m_nIndex, account.GetADsPath());
  49. }
  50. catch (_com_error& ce)
  51. {
  52. _Module.Log(ErrW, IDS_E_FIX_HIERARCHY_MOVE_OBJECT, (LPCTSTR)itObject->m_strPath, (LPCTSTR)itContainer->first, ce.ErrorMessage());
  53. }
  54. catch (...)
  55. {
  56. _Module.Log(ErrW, IDS_E_FIX_HIERARCHY_MOVE_OBJECT, (LPCTSTR)itObject->m_strPath, (LPCTSTR)itContainer->first, _com_error(E_FAIL).ErrorMessage());
  57. }
  58. }
  59. }
  60. catch (_com_error& ce)
  61. {
  62. _Module.Log(ErrW, IDS_E_FIX_HIERARCHY_BIND_TO_CONTAINER, (LPCTSTR)itContainer->first, ce.ErrorMessage());
  63. }
  64. catch (...)
  65. {
  66. _Module.Log(ErrW, IDS_E_FIX_HIERARCHY_BIND_TO_CONTAINER, (LPCTSTR)itContainer->first, _com_error(E_FAIL).ErrorMessage());
  67. }
  68. }
  69. }
  70. }
  71. //---------------------------------------------------------------------------
  72. // Migrated Objects Class Implementation
  73. //---------------------------------------------------------------------------
  74. CFixObjectsInHierarchy::CMigrated::CMigrated() :
  75. m_lActionId(-1),
  76. m_spDB(__uuidof(IManageDB))
  77. {
  78. }
  79. CFixObjectsInHierarchy::CMigrated::~CMigrated()
  80. {
  81. }
  82. int CFixObjectsInHierarchy::CMigrated::GetCount()
  83. {
  84. return long(m_vsObjects.Get(_T("MigratedObjects")));
  85. }
  86. _bstr_t CFixObjectsInHierarchy::CMigrated::GetObjectKey(int nIndex)
  87. {
  88. _TCHAR szKey[64];
  89. _stprintf(szKey, _T("MigratedObjects.%d"), nIndex);
  90. return szKey;
  91. }
  92. _bstr_t CFixObjectsInHierarchy::CMigrated::GetObjectType(int nIndex)
  93. {
  94. return m_vsObjects.Get(_T("MigratedObjects.%d.Type"), nIndex);
  95. }
  96. int CFixObjectsInHierarchy::CMigrated::GetObjectStatus(int nIndex)
  97. {
  98. return long(m_vsObjects.Get(_T("MigratedObjects.%d.status"), nIndex));
  99. }
  100. _bstr_t CFixObjectsInHierarchy::CMigrated::GetObjectSourcePath(int nIndex)
  101. {
  102. return m_vsObjects.Get(_T("MigratedObjects.%d.SourceAdsPath"), nIndex);
  103. }
  104. _bstr_t CFixObjectsInHierarchy::CMigrated::GetObjectTargetPath(int nIndex)
  105. {
  106. return m_vsObjects.Get(_T("MigratedObjects.%d.TargetAdsPath"), nIndex);
  107. }
  108. void CFixObjectsInHierarchy::CMigrated::RetrieveMigratedObjects(int nActionId)
  109. {
  110. if (nActionId > 0)
  111. {
  112. m_lActionId = nActionId;
  113. }
  114. else
  115. {
  116. m_spDB->GetCurrentActionID(&m_lActionId);
  117. }
  118. IUnknownPtr spUnknown(m_vsObjects.GetInterface());
  119. IUnknown* punk = spUnknown;
  120. m_spDB->GetMigratedObjects(m_lActionId, &punk);
  121. }
  122. void CFixObjectsInHierarchy::CMigrated::UpdateObjectTargetPath(int nIndex, _bstr_t strPath)
  123. {
  124. IVarSetPtr spVarSet(__uuidof(VarSet));
  125. spVarSet->ImportSubTree(_bstr_t(), IVarSetPtr(m_vsObjects.GetInterface()->getReference(GetObjectKey(nIndex))));
  126. spVarSet->put(_T("TargetAdsPath"), strPath);
  127. m_spDB->SaveMigratedObject(m_lActionId, IUnknownPtr(spVarSet));
  128. }
  129. //---------------------------------------------------------------------------
  130. // Target Path Class Implementation
  131. //---------------------------------------------------------------------------
  132. CFixObjectsInHierarchy::CTargetPath::CTargetPath()
  133. {
  134. }
  135. CFixObjectsInHierarchy::CTargetPath::~CTargetPath()
  136. {
  137. }
  138. void CFixObjectsInHierarchy::CTargetPath::SetContainerPaths(_bstr_t strSourceContainerPath, _bstr_t strTargetContainerPath)
  139. {
  140. m_pnSourceContainerPath.Set(strSourceContainerPath, ADS_SETTYPE_FULL);
  141. m_pnTargetContainerPath.Set(strTargetContainerPath, ADS_SETTYPE_FULL);
  142. }
  143. bool CFixObjectsInHierarchy::CTargetPath::NeedsToMove(_bstr_t strSourceObjectPath, _bstr_t strTargetObjectPath)
  144. {
  145. bool bNeedsToMove = false;
  146. // if the source object exists within the source root container hierarchy...
  147. CADsPathName pn(strSourceObjectPath);
  148. long lCount = pn.GetNumElements() - m_pnSourceContainerPath.GetNumElements();
  149. while (lCount-- > 0)
  150. {
  151. pn.RemoveLeafElement();
  152. }
  153. if (IsMatch(pn.Retrieve(ADS_FORMAT_X500_DN), m_pnSourceContainerPath.Retrieve(ADS_FORMAT_X500_DN)))
  154. {
  155. m_pnTargetObjectOldPath.Set(strTargetObjectPath, ADS_SETTYPE_FULL);
  156. // construct expected target object path
  157. m_pnTargetObjectNewPath.Set(m_pnTargetContainerPath.Retrieve(ADS_FORMAT_X500), ADS_SETTYPE_FULL);
  158. pn.Set(strSourceObjectPath, ADS_SETTYPE_FULL);
  159. long lIndex = pn.GetNumElements() - m_pnSourceContainerPath.GetNumElements();
  160. while (--lIndex >= 0)
  161. {
  162. m_pnTargetObjectNewPath.AddLeafElement(pn.GetElement(lIndex));
  163. }
  164. // compare expected target path with current target path
  165. if (!IsMatch(m_pnTargetObjectNewPath.Retrieve(ADS_FORMAT_X500_DN), m_pnTargetObjectOldPath.Retrieve(ADS_FORMAT_X500_DN)))
  166. {
  167. m_pnTargetObjectNewPath.Set(m_pnTargetObjectOldPath.Retrieve(ADS_FORMAT_SERVER), ADS_SETTYPE_SERVER);
  168. bNeedsToMove = true;
  169. }
  170. }
  171. return bNeedsToMove;
  172. }
  173. _bstr_t CFixObjectsInHierarchy::CTargetPath::GetTargetContainerPath()
  174. {
  175. CADsPathName pn(m_pnTargetObjectNewPath.Retrieve(ADS_FORMAT_X500));
  176. pn.RemoveLeafElement();
  177. return pn.Retrieve(ADS_FORMAT_X500);
  178. }
  179. _bstr_t CFixObjectsInHierarchy::CTargetPath::GetTargetObjectCurrentPath()
  180. {
  181. return m_pnTargetObjectOldPath.Retrieve(ADS_FORMAT_X500_NO_SERVER);
  182. }
  183. bool CFixObjectsInHierarchy::CTargetPath::IsMatch(LPCTSTR pszA, LPCTSTR pszB)
  184. {
  185. bool bMatch = false;
  186. if (pszA && pszB)
  187. {
  188. bMatch = (_tcsicmp(pszA, pszB) == 0) ? true : false;
  189. }
  190. return bMatch;
  191. }
  192. //---------------------------------------------------------------------------
  193. // Containers Class Implementation
  194. //---------------------------------------------------------------------------
  195. void CFixObjectsInHierarchy::CContainers::InsertObject(_bstr_t strContainerPath, int nObjectIndex, _bstr_t strObjectPathOld)
  196. {
  197. iterator it = find(strContainerPath);
  198. if (it == end())
  199. {
  200. _Pairib pair = insert(value_type(strContainerPath, CObjects()));
  201. it = pair.first;
  202. }
  203. CObjects& ov = it->second;
  204. ov.push_back(SObject(nObjectIndex, strObjectPathOld));
  205. }