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.

250 lines
6.5 KiB

  1. //--------------------------------------------------------------
  2. //
  3. // File: loadstate
  4. //
  5. // Contents: Load a machine state.
  6. //
  7. //---------------------------------------------------------------
  8. #include "loadhead.cxx"
  9. #pragma hdrstop
  10. #include <common.hxx>
  11. #include <objerror.h>
  12. #include <loadstate.hxx>
  13. #include <bothchar.hxx>
  14. //---------------------------------------------------------------
  15. // Constants.
  16. const char MIGRATEINF[] = "\\migration.inf";
  17. //---------------------------------------------------------------
  18. // Types.
  19. //---------------------------------------------------------------
  20. // Macros
  21. //---------------------------------------------------------------
  22. // Globals.
  23. DWORD SourceVersion = 0;
  24. TCHAR szLogFile[MAX_PATH+1];
  25. //---------------------------------------------------------------
  26. DWORD GetSourceVersion()
  27. {
  28. DWORD dwResult = ERROR_SUCCESS;
  29. BOOL fSuccess;
  30. INFCONTEXT ic;
  31. DWORD dwRealLen;
  32. TCHAR *ptsVar = NULL;
  33. // Find the section.
  34. fSuccess = SetupFindFirstLine( InputInf, SOURCE_SECTION, NULL, &ic );
  35. LOG_ASSERT_GLE( fSuccess, dwResult );
  36. // Find the version.
  37. do
  38. {
  39. // Get the correct length
  40. fSuccess = SetupGetStringField( &ic, 0, NULL, 0, &dwRealLen );
  41. LOG_ASSERT_GLE( fSuccess, dwResult );
  42. // Presumably we have the correct length now...
  43. ptsVar = (TCHAR *) malloc(dwRealLen * sizeof(TCHAR));
  44. LOG_ASSERT_EXPR( ptsVar != NULL, IDS_NOT_ENOUGH_MEMORY, dwResult,
  45. ERROR_NOT_ENOUGH_MEMORY );
  46. fSuccess = SetupGetStringField( &ic, 0, ptsVar, dwRealLen, NULL);
  47. LOG_ASSERT_EXPR(fSuccess, IDS_GETSTRINGFIELD_ERROR,
  48. dwResult, SPAPI_E_SECTION_NAME_TOO_LONG);
  49. // Save the value if it is the one we are looking for.
  50. if (_tcsicmp( ptsVar, VERSION ) == 0)
  51. {
  52. fSuccess = SetupGetIntField( &ic, 1, (int *) &SourceVersion );
  53. LOG_ASSERT_EXPR( fSuccess, IDS_INF_ERROR, dwResult, SPAPI_E_GENERAL_SYNTAX );
  54. break;
  55. }
  56. free( ptsVar );
  57. ptsVar = NULL;
  58. // Advance to the next line.
  59. fSuccess = SetupFindNextLine( &ic, &ic );
  60. } while( fSuccess);
  61. // If the version wasn't found, return an error.
  62. LOG_ASSERT_EXPR( SourceVersion != 0, IDS_INF_ERROR, dwResult, SPAPI_E_GENERAL_SYNTAX );
  63. cleanup:
  64. if (ptsVar != NULL)
  65. free( ptsVar );
  66. return dwResult;
  67. }
  68. /***************************************************************************
  69. main
  70. Load machine state from migration.inf.
  71. ***************************************************************************/
  72. int _cdecl main(int argc, char *argv[])
  73. {
  74. DWORD dwResult;
  75. TCHAR *ptsHiveName = NULL;
  76. DWORD dwLen;
  77. char *pszMigrate;
  78. DWORD dwReturnToDos = ERROR_SUCCESS;
  79. dwResult = CoInitialize(NULL);
  80. if (FAILED(dwResult))
  81. goto cleanup;
  82. dwResult = OpenFiles();
  83. if (dwResult != ERROR_SUCCESS) goto cleanup;
  84. // Parse the parameters.
  85. dwResult = ParseParams( argc, argv, FALSE, szLogFile );
  86. if (dwResult != ERROR_SUCCESS) goto cleanup;
  87. // Determine the operating system.
  88. if (FALSE == TestMode)
  89. {
  90. LOG_ASSERT_EXPR( (GetVersion() & 0xffff) == 5, IDS_OS5, dwResult,
  91. ERROR_BAD_ENVIRONMENT );
  92. }
  93. else if ((GetVersion() & 0xffff) != 5)
  94. Win32PrintfResource( LogFile, IDS_OS5_WARNING );
  95. // Append migration.inf.
  96. dwLen = strlen(MigrationPath) + sizeof(MIGRATEINF) + 2;
  97. pszMigrate = (char *) _alloca( dwLen );
  98. strcpy( pszMigrate, MigrationPath );
  99. strcat( pszMigrate, MIGRATEINF );
  100. dwResult = OpenInf( pszMigrate );
  101. if (dwResult != ERROR_SUCCESS) goto cleanup;
  102. // Read the source OS version.
  103. dwResult = GetSourceVersion();
  104. if (dwResult != ERROR_SUCCESS) goto cleanup;
  105. // Compute the temp directory.
  106. dwResult = ComputeTemp();
  107. if (dwResult != ERROR_SUCCESS) goto cleanup;
  108. // Copy the user registry.
  109. if (DebugOutput)
  110. Win32Printf(LogFile, "Loading User settings \r\n");
  111. dwResult = LoadUser( &DomainName, &UserName, &ptsHiveName );
  112. if (dwResult != ERROR_SUCCESS) goto cleanup;
  113. // Copy system settings.
  114. if (DebugOutput)
  115. Win32Printf(LogFile, "Loading System settings\r\n");
  116. dwResult = LoadSystem(argc, argv);
  117. if (dwResult != ERROR_SUCCESS) goto cleanup;
  118. // Create the user profile.
  119. if (DebugOutput)
  120. Win32Printf(LogFile, "Creating User Profile for %s, domain %s, hive %s\r\n", UserName, DomainName, ptsHiveName);
  121. dwResult = CreateUserProfileFromName( DomainName, UserName, ptsHiveName );
  122. if (dwResult != ERROR_SUCCESS) goto cleanup;
  123. // Copy files.
  124. if (DebugOutput)
  125. Win32Printf(LogFile, "Loading files\r\n");
  126. dwResult = LoadFiles();
  127. if (dwResult == ERROR_FILENAME_EXCED_RANGE)
  128. {
  129. dwReturnToDos = dwResult;
  130. }
  131. else if (dwResult != ERROR_SUCCESS)
  132. {
  133. goto cleanup;
  134. }
  135. // Process the extentions.
  136. if (DebugOutput)
  137. Win32Printf(LogFile, "Processing Extensions\r\n");
  138. dwResult = ProcessExtensions();
  139. if (dwResult != ERROR_SUCCESS) goto cleanup;
  140. //Fix registry entries with filenames in them
  141. if (DebugOutput)
  142. Win32Printf(LogFile, "Fixing special filenames\r\n");
  143. dwResult = FixSpecial();
  144. if (dwResult != ERROR_SUCCESS) goto cleanup;
  145. // Process the executable extentions.
  146. if (DebugOutput)
  147. Win32Printf(LogFile, "Processing Exec Extenstions\r\n");
  148. dwResult = ProcessExecExtensions();
  149. if (dwResult != ERROR_SUCCESS) goto cleanup;
  150. cleanup:
  151. // Clean up user stuff and ignore failures.
  152. if (DebugOutput)
  153. Win32Printf(LogFile, "Load complete. Cleaning up.\r\n");
  154. CleanupUser();
  155. // Close any open files and ignore failures.
  156. CloseFiles();
  157. // Erase the temp directory and ignore failures.
  158. EraseTemp();
  159. // Delete loadstate key in registry if success
  160. if (ERROR_SUCCESS == dwResult)
  161. {
  162. HKEY hKey;
  163. RegOpenKeyEx( HKEY_CURRENT_USER,
  164. TEXT("Software\\Microsoft\\Windows\\CurrentVersion"),
  165. 0, KEY_WRITE, &hKey );
  166. RegDeleteKey(hKey, TEXT("Loadstate"));
  167. RegCloseKey(hKey);
  168. }
  169. // Free strings.
  170. if (DomainName != NULL)
  171. free( DomainName );
  172. if (UserName != NULL)
  173. free( UserName );
  174. if (ptsHiveName != NULL )
  175. free( ptsHiveName );
  176. if (UserPath != NULL)
  177. free (UserPath);
  178. CoUninitialize();
  179. if (dwResult == ERROR_SUCCESS)
  180. {
  181. dwResult = dwReturnToDos;
  182. }
  183. // Print a message.
  184. if (Console != INVALID_HANDLE_VALUE)
  185. if (dwResult == ERROR_SUCCESS)
  186. Win32PrintfResource( Console, IDS_COMPLETE_OK );
  187. else
  188. Win32PrintfResource( Console, IDS_COMPLETE_ERROR, szLogFile );
  189. if (Verbose)
  190. {
  191. printf( "Returning 0x%x to DOS.\r\n", dwResult );
  192. }
  193. return dwResult;
  194. }