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.

382 lines
10 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. memdbren.c
  5. Abstract:
  6. Implements a stub tool that is designed to run with NT-side
  7. upgrade code.
  8. Author:
  9. <full name> (<alias>) <date>
  10. Revision History:
  11. <alias> <date> <comments>
  12. --*/
  13. #include "pch.h"
  14. VOID
  15. pFixUpMemDb2 (
  16. VOID
  17. );
  18. BOOL
  19. Init (
  20. VOID
  21. )
  22. {
  23. HINSTANCE hInstance;
  24. DWORD dwReason;
  25. PVOID lpReserved;
  26. //
  27. // Simulate DllMain
  28. //
  29. hInstance = GetModuleHandle (NULL);
  30. dwReason = DLL_PROCESS_ATTACH;
  31. lpReserved = NULL;
  32. //
  33. // Initialize DLL globals
  34. //
  35. if (!FirstInitRoutine (hInstance)) {
  36. return FALSE;
  37. }
  38. //
  39. // Initialize all libraries
  40. //
  41. if (!InitLibs (hInstance, dwReason, lpReserved)) {
  42. return FALSE;
  43. }
  44. //
  45. // Final initialization
  46. //
  47. if (!FinalInitRoutine ()) {
  48. return FALSE;
  49. }
  50. return TRUE;
  51. }
  52. VOID
  53. Terminate (
  54. VOID
  55. )
  56. {
  57. HINSTANCE hInstance;
  58. DWORD dwReason;
  59. PVOID lpReserved;
  60. //
  61. // Simulate DllMain
  62. //
  63. hInstance = GetModuleHandle (NULL);
  64. dwReason = DLL_PROCESS_DETACH;
  65. lpReserved = NULL;
  66. //
  67. // Call the cleanup routine that requires library APIs
  68. //
  69. FirstCleanupRoutine();
  70. //
  71. // Clean up all libraries
  72. //
  73. TerminateLibs (hInstance, dwReason, lpReserved);
  74. //
  75. // Do any remaining clean up
  76. //
  77. FinalCleanupRoutine();
  78. }
  79. INT
  80. __cdecl
  81. wmain (
  82. INT argc,
  83. WCHAR *argv[]
  84. )
  85. {
  86. TREE_ENUM te;
  87. DWORD Value, Flags;
  88. DWORD Offset;
  89. TCHAR Node[MEMDB_MAX];
  90. FILEOP_ENUM e;
  91. FILEOP_PROP_ENUM eOpProp;
  92. PTSTR NewDest;
  93. TCHAR node[MEMDB_MAX];
  94. if (!Init()) {
  95. wprintf (L"Unable to initialize!\n");
  96. return 255;
  97. }
  98. CopyFile (TEXT("c:\\public\\ntsetup.bak"), TEXT("c:\\public\\ntsetup.dat"), FALSE);
  99. MemDbLoad (TEXT("c:\\public\\ntsetup.dat"));
  100. MemDbSave (TEXT("c:\\public\\ntsetup.dat"));
  101. pFixUpMemDb2();
  102. if (EnumFirstPathInOperation (&e, OPERATION_SHELL_FOLDER)) {
  103. do {
  104. wprintf (L"Operation %u\n", e.Sequencer);
  105. if (EnumFirstFileOpProperty (&eOpProp, e.Sequencer, OPERATION_SHELL_FOLDER)) {
  106. do {
  107. wprintf (L" %s: %s\n", eOpProp.PropertyName, eOpProp.Property);
  108. } while (EnumNextFileOpProperty (&eOpProp));
  109. }
  110. } while (EnumNextPathInOperation (&e));
  111. }
  112. MemDbSave (TEXT("c:\\public\\ntsetup.new"));
  113. MemDbValidateDatabase();
  114. Terminate();
  115. return 0;
  116. }
  117. PCTSTR
  118. GetProfilePathForUser2 (
  119. PCTSTR User
  120. )
  121. {
  122. static TCHAR Path[MAX_TCHAR_PATH];
  123. wsprintf (Path, TEXT("c:\\Documents and Settings\\%s"), User);
  124. return Path;
  125. }
  126. VOID
  127. pFixUpDynamicPaths2 (
  128. PCTSTR Category
  129. )
  130. {
  131. MEMDB_ENUM e;
  132. TCHAR Pattern[MEMDB_MAX];
  133. PTSTR p;
  134. GROWBUFFER Roots = GROWBUF_INIT;
  135. MULTISZ_ENUM e2;
  136. TCHAR NewRoot[MEMDB_MAX];
  137. TCHAR AllProfilePath[MAX_TCHAR_PATH];
  138. PCTSTR ProfilePath;
  139. DWORD Size;
  140. PTSTR UserName;
  141. HKEY sfKey = NULL;
  142. PCTSTR sfPath = NULL;
  143. PTSTR NtLocation;
  144. PCTSTR tempExpand;
  145. BOOL regFolder;
  146. //
  147. // Collect all the roots that need to be renamed
  148. //
  149. StringCopy (Pattern, Category);
  150. p = AppendWack (Pattern);
  151. StringCopy (p, TEXT("*"));
  152. if (MemDbEnumFirstValue (&e, Pattern, MEMDB_THIS_LEVEL_ONLY, MEMDB_ALL_BUT_PROXY)) {
  153. do {
  154. if ((_tcsnextc (e.szName) == TEXT('>')) ||
  155. (_tcsnextc (e.szName) == TEXT('<'))
  156. ) {
  157. StringCopy (p, e.szName);
  158. MultiSzAppend (&Roots, Pattern);
  159. }
  160. } while (MemDbEnumNextValue (&e));
  161. }
  162. //
  163. // Now change each root
  164. //
  165. if (EnumFirstMultiSz (&e2, (PCTSTR) Roots.Buf)) {
  166. do {
  167. //
  168. // Compute NewRoot
  169. //
  170. StringCopy (NewRoot, e2.CurrentString);
  171. p = _tcschr (NewRoot, TEXT('<'));
  172. if (p) {
  173. UserName = _tcschr (p, TEXT('>'));
  174. MYASSERT (UserName);
  175. StringCopyAB (Pattern, _tcsinc (p), UserName);
  176. UserName = _tcsinc (UserName);
  177. regFolder = TRUE;
  178. if (StringIMatch (Pattern, TEXT("Profiles"))) {
  179. regFolder = FALSE;
  180. }
  181. if (StringIMatch (Pattern, TEXT("Common Profiles"))) {
  182. regFolder = FALSE;
  183. }
  184. if (StringIMatch (UserName, S_DOT_ALLUSERS)) {
  185. Size = MAX_TCHAR_PATH;
  186. if (regFolder) {
  187. if (!GetAllUsersProfileDirectory (AllProfilePath, &Size)) {
  188. DEBUGMSG ((DBG_WHOOPS, "Cannot get All Users profile path."));
  189. continue;
  190. }
  191. sfKey = OpenRegKeyStr (S_USHELL_FOLDERS_KEY_SYSTEM);
  192. } else {
  193. if (!GetProfilesDirectory (AllProfilePath, &Size)) {
  194. DEBUGMSG ((DBG_WHOOPS, "Cannot get All Users profile path."));
  195. continue;
  196. }
  197. }
  198. }
  199. else if (StringIMatch (UserName, S_DEFAULT_USER)) {
  200. Size = MAX_TCHAR_PATH;
  201. if (regFolder) {
  202. if (!GetDefaultUserProfileDirectory (AllProfilePath, &Size)) {
  203. DEBUGMSG ((DBG_WHOOPS, "Cannot get Default User profile path."));
  204. continue;
  205. }
  206. sfKey = OpenRegKey (HKEY_CURRENT_USER, S_USHELL_FOLDERS_KEY_USER);
  207. } else {
  208. if (!GetProfilesDirectory (AllProfilePath, &Size)) {
  209. DEBUGMSG ((DBG_WHOOPS, "Cannot get All Users profile path."));
  210. continue;
  211. }
  212. }
  213. }
  214. else {
  215. ProfilePath = GetProfilePathForUser2 (UserName);
  216. if (!ProfilePath) {
  217. DEBUGMSG ((DBG_WHOOPS, "Cannot get profile path for user:%s", UserName));
  218. continue;
  219. }
  220. StringCopy (AllProfilePath, ProfilePath);
  221. if (regFolder) {
  222. sfKey = OpenRegKey (HKEY_CURRENT_USER, S_USHELL_FOLDERS_KEY_USER);
  223. }
  224. }
  225. if (regFolder) {
  226. if (!sfKey) {
  227. DEBUGMSG ((DBG_ERROR, "Could not open Shell folders key."));
  228. continue;
  229. }
  230. sfPath = GetRegValueString (sfKey, Pattern);
  231. CloseRegKey (sfKey);
  232. if (!sfPath) {
  233. DEBUGMSG ((DBG_WHOOPS, "Could not get Shell Folder path for: %s", Pattern));
  234. continue;
  235. }
  236. tempExpand = StringSearchAndReplace (
  237. sfPath,
  238. S_USERPROFILE_ENV,
  239. AllProfilePath
  240. );
  241. if (!tempExpand) {
  242. tempExpand = DuplicatePathString (sfPath, 0);
  243. }
  244. } else {
  245. tempExpand = DuplicatePathString (AllProfilePath, 0);
  246. }
  247. NtLocation = ExpandEnvironmentText (tempExpand);
  248. StringCopy (p, NtLocation);
  249. MemDbMoveTree (e2.CurrentString, NewRoot);
  250. FreeText (NtLocation);
  251. FreePathString (tempExpand);
  252. if (regFolder) {
  253. MemFree (g_hHeap, 0, sfPath);
  254. }
  255. }
  256. else {
  257. p = _tcschr (NewRoot, TEXT('>'));
  258. MYASSERT (p);
  259. if (StringIMatch (_tcsinc (p), S_DOT_ALLUSERS)) {
  260. Size = MAX_TCHAR_PATH;
  261. if (!GetAllUsersProfileDirectory (AllProfilePath, &Size)) {
  262. DEBUGMSG ((DBG_WARNING, "Dynamic path for %s could not be resolved", e2.CurrentString));
  263. }
  264. else {
  265. StringCopy (p, AllProfilePath);
  266. MemDbMoveTree (e2.CurrentString, NewRoot);
  267. }
  268. } else if (StringIMatch (_tcsinc (p), S_DEFAULT_USER)) {
  269. Size = MAX_TCHAR_PATH;
  270. if (!GetDefaultUserProfileDirectory (AllProfilePath, &Size)) {
  271. DEBUGMSG ((DBG_WARNING, "Dynamic path for %s could not be resolved", e2.CurrentString));
  272. }
  273. else {
  274. StringCopy (p, AllProfilePath);
  275. MemDbMoveTree (e2.CurrentString, NewRoot);
  276. }
  277. } else {
  278. ProfilePath = GetProfilePathForUser2 (_tcsinc (p));
  279. if (ProfilePath) {
  280. StringCopy (p, ProfilePath);
  281. MemDbMoveTree (e2.CurrentString, NewRoot);
  282. }
  283. else {
  284. DEBUGMSG ((DBG_WARNING, "Dynamic path for %s could not be resolved", e2.CurrentString));
  285. }
  286. }
  287. }
  288. } while (EnumNextMultiSz (&e2));
  289. }
  290. FreeGrowBuffer (&Roots);
  291. }
  292. VOID
  293. pFixUpMemDb2 (
  294. VOID
  295. )
  296. {
  297. pFixUpDynamicPaths2 (MEMDB_CATEGORY_PATHROOT);
  298. pFixUpDynamicPaths2 (MEMDB_CATEGORY_DATA);
  299. pFixUpDynamicPaths2 (MEMDB_CATEGORY_USERFILEMOVE_DEST);
  300. pFixUpDynamicPaths2 (MEMDB_CATEGORY_SHELLFOLDERS_DEST);
  301. pFixUpDynamicPaths2 (MEMDB_CATEGORY_SHELLFOLDERS_SRC);
  302. pFixUpDynamicPaths2 (MEMDB_CATEGORY_LINKEDIT_TARGET);
  303. pFixUpDynamicPaths2 (MEMDB_CATEGORY_LINKEDIT_WORKDIR);
  304. pFixUpDynamicPaths2 (MEMDB_CATEGORY_LINKEDIT_ICONPATH);
  305. }