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.

233 lines
4.6 KiB

  1. // RegDiffFile.cpp: implementation of the CRegDiffFile class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "RegDiffFile.h"
  5. #include "Registry.h"
  6. //////////////////////////////////////////////////////////////////////
  7. // Construction/Destruction
  8. //////////////////////////////////////////////////////////////////////
  9. CRegDiffFile::CRegDiffFile()
  10. : m_AddSection(1), m_DelSection(1)
  11. {
  12. }
  13. CRegDiffFile::~CRegDiffFile()
  14. {
  15. }
  16. void CRegDiffFile::WriteDataItem(enum SectionType t, CRegDataItemPtr r)
  17. {
  18. switch (t)
  19. {
  20. case SECTION_ADDREG: m_AddSection.AddElement(r); break;
  21. case SECTION_DELREG: m_DelSection.AddElement(r); break;
  22. }
  23. }
  24. #define DIFF_VERSION_STR TEXT("[RegDiff 1.0]")
  25. #define DIFF_ADD_STR TEXT("[AddReg]")
  26. #define DIFF_DEL_STR TEXT("[DelReg]")
  27. void CRegDiffFile::WriteStoredSectionsToFile()
  28. {
  29. WriteString(DIFF_VERSION_STR);
  30. WriteNewLine();
  31. WriteString(DIFF_ADD_STR);
  32. WriteNewLine();
  33. for (int i=0; i<m_AddSection.GetNumElementsStored(); i++)
  34. {
  35. (m_AddSection.Access()[i])->WriteToFile(*this);
  36. }
  37. WriteString(DIFF_DEL_STR);
  38. WriteNewLine();
  39. CStr DelKey=L"NO WAY", CurrentKey;
  40. for (i=0; i<m_DelSection.GetNumElementsStored(); i++)
  41. {
  42. CurrentKey = (m_DelSection.Access()[i])->m_KeyName;
  43. if (!CurrentKey.IsPrefix(DelKey))
  44. {
  45. (m_DelSection.Access()[i])->WriteToFile(*this);
  46. DelKey = CurrentKey;
  47. }
  48. }
  49. }
  50. //BUGBUG memory leaks here
  51. BOOL CRegDiffFile::ApplyToRegistry(LPCTSTR UndoFileName)
  52. {
  53. CRegDiffFile UndoFile;
  54. if (UndoFileName != NULL)
  55. {
  56. UndoFile.Init(UndoFileName);
  57. }
  58. LPCTSTR VersionStr = GetNextLine();
  59. if (_tcscmp(VersionStr, DIFF_VERSION_STR) != 0)
  60. {
  61. delete[] VersionStr;
  62. return FALSE;//error - version string does not match!
  63. }
  64. LPCTSTR AddStr = GetNextLine();
  65. if (_tcscmp(AddStr, DIFF_ADD_STR) != 0)
  66. {
  67. delete[] AddStr;
  68. return FALSE;//error - version string does not match!
  69. }
  70. //Currently in the Add Section
  71. while (PeekNextChar() != TEXT('['))
  72. {
  73. LPCTSTR KeyName = GetNextLine();
  74. CRegDataItemPtr pDataItem = GetNextDataItem();
  75. pDataItem->m_KeyName = KeyName;
  76. AddToRegistry(pDataItem, UndoFile);
  77. }
  78. LPCTSTR DelStr = GetNextLine();
  79. if (_tcscmp(DelStr, DIFF_DEL_STR) != 0)
  80. {
  81. delete[] DelStr;
  82. return FALSE;//error - version string does not match!
  83. }
  84. TCHAR c;
  85. //Currently in the Delete Section
  86. while (((c = PeekNextChar()) != EOF)
  87. && (c != WEOF))
  88. {
  89. LPCTSTR KeyName = GetNextLine();
  90. CRegDataItemPtr pDataItem = GetNextDataItem();
  91. pDataItem->m_KeyName = KeyName;
  92. DeleteFromRegistry(pDataItem, UndoFile);
  93. }
  94. UndoFile.WriteStoredSectionsToFile();
  95. delete[] VersionStr; delete[] AddStr; delete[] DelStr;
  96. return TRUE;
  97. }
  98. void CRegDiffFile::AddToRegistry(CRegDataItemPtr pDataItem, CRegDiffFile &UndoFile)
  99. {
  100. CRegistry reg;
  101. if (pDataItem->m_KeyName != NULL)
  102. {
  103. pDataItem->m_bIsEmpty = false;
  104. if (pDataItem->m_Name == NULL)
  105. {
  106. //Adding just a registry key
  107. if (reg.KeyExists(pDataItem))
  108. {
  109. //no action
  110. }
  111. else
  112. {
  113. //key doesn't exist, so we will delete it on undo
  114. UndoFile.WriteDataItem(SECTION_DELREG, pDataItem);
  115. reg.AddKey(pDataItem);
  116. }
  117. }
  118. else
  119. {
  120. //Adding a data item
  121. int code = reg.ValueExists(pDataItem);
  122. if (code == 2) //value name exists, and data,type are the same
  123. {
  124. //no action
  125. }
  126. else if (code == 1) //value name exists, but data or type are different
  127. {
  128. CRegDataItemPtr oldval = reg.GetValue(pDataItem);
  129. UndoFile.WriteDataItem(SECTION_ADDREG, oldval);
  130. reg.AddValue(pDataItem);
  131. }
  132. else //code ==0, value name doesn't exist
  133. {
  134. UndoFile.WriteDataItem(SECTION_DELREG, pDataItem);
  135. reg.AddValue(pDataItem);
  136. }
  137. }
  138. }
  139. }
  140. void CRegDiffFile::DeleteFromRegistry(CRegDataItemPtr pDataItem, CRegDiffFile &UndoFile)
  141. {
  142. CRegistry reg;
  143. if (pDataItem->m_KeyName != NULL)
  144. {
  145. if (pDataItem->m_Name == NULL)
  146. {
  147. //Deleting a registry key
  148. if (reg.KeyExists(pDataItem))
  149. {
  150. reg.SaveKey(pDataItem, UndoFile, SECTION_ADDREG); //better def needed
  151. reg.DeleteKey(pDataItem);
  152. }
  153. else
  154. {
  155. //no action
  156. }
  157. }
  158. else
  159. {
  160. //Deleting a data item
  161. if(reg.ValueExists(pDataItem))
  162. {
  163. CRegDataItemPtr oldval = reg.GetValue(pDataItem);
  164. UndoFile.WriteDataItem(SECTION_ADDREG, oldval);
  165. reg.DeleteValue(pDataItem);
  166. }
  167. else //code ==0, value name doesn't exist
  168. {
  169. //no action
  170. }
  171. }
  172. }
  173. }
  174. bool CRegDiffFile::NeedStorageOfValueData()
  175. {
  176. return true;
  177. }