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.

498 lines
14 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 1997-1999 Microsoft Corporation
  4. // All rights reserved.
  5. //
  6. // File Name:
  7. // savescr.c
  8. //
  9. // Description:
  10. // This is the dialog proc for the Save Script page.
  11. //
  12. //----------------------------------------------------------------------------
  13. #include "pch.h"
  14. #include "resource.h"
  15. #define DEFAULT_SCRIPT_NAME _T("unattend.txt")
  16. #define DEFAULT_REMBOOT_NAME _T("remboot.sif")
  17. #define DEFAULT_SYSPREP_NAME _T("sysprep.inf")
  18. #define DEFAULT_SYSPREP_BAT_NAME _T("sysprep.bat")
  19. static TCHAR *StrWinntSifText;
  20. //----------------------------------------------------------------------------
  21. //
  22. // Function: AddEntryToRenameFile
  23. //
  24. // Purpose:
  25. //
  26. // Arguments:
  27. //
  28. // Returns: VOID
  29. //
  30. //----------------------------------------------------------------------------
  31. static VOID
  32. AddEntryToRenameFile( const TCHAR *pszDirPath,
  33. const TCHAR *pszShortName,
  34. const TCHAR *pszLongName )
  35. {
  36. TCHAR szQuotedLongName[MAX_PATH + 1];
  37. TCHAR szFullRenamePath[MAX_PATH + 1] = _T("");
  38. HRESULT hrPrintf;
  39. //
  40. // Quote the long file name
  41. hrPrintf=StringCchPrintf( szQuotedLongName, AS(szQuotedLongName),
  42. _T("\"%s\""),
  43. pszLongName );
  44. //
  45. // Note:ConcatenatePaths truncates to avoid buffer overrun
  46. if( ! ConcatenatePaths( szFullRenamePath,
  47. pszDirPath,
  48. _T("$$Rename.txt"),
  49. NULL ) )
  50. {
  51. return;
  52. }
  53. WritePrivateProfileString( pszDirPath,
  54. pszShortName,
  55. szQuotedLongName,
  56. szFullRenamePath );
  57. }
  58. //----------------------------------------------------------------------------
  59. //
  60. // Function: CreateRenameFiles
  61. //
  62. // Purpose: Walk all the dirs of the given path and lower. If there are any
  63. // long file names, put them in the $$Rename.txt file.
  64. //
  65. // Arguments: IN const TCHAR *pszDirPath - sub-dir to search for long filenames
  66. //
  67. // Returns: VOID
  68. //
  69. //----------------------------------------------------------------------------
  70. VOID
  71. CreateRenameFiles( IN const TCHAR *pszDirPath )
  72. {
  73. HANDLE FindHandle;
  74. WIN32_FIND_DATA FindData;
  75. TCHAR *pszShortName;
  76. TCHAR szLongPathAndFilename[MAX_PATH + 1];
  77. TCHAR szShortPathAndFilename[MAX_PATH + 1];
  78. TCHAR szAllFiles[MAX_PATH + 1] = _T("");
  79. TCHAR szNewPath[MAX_PATH + 1] = _T("");
  80. // Note: ConcatenatePaths truncates to avoid buffer overrun
  81. if( ! ConcatenatePaths( szAllFiles,
  82. pszDirPath,
  83. _T("*"),
  84. NULL ) )
  85. {
  86. return;
  87. }
  88. //
  89. // Look for * in this dir
  90. //
  91. // ISSUE-2002/02/27-stelo,swamip - Clean up the FindFirstFile Logic
  92. FindHandle = FindFirstFile( szAllFiles, &FindData );
  93. if( FindHandle == INVALID_HANDLE_VALUE ) {
  94. return;
  95. }
  96. do {
  97. //
  98. // skip over the . and .. entries
  99. //
  100. if( 0 == lstrcmp( FindData.cFileName, _T(".") ) ||
  101. 0 == lstrcmp( FindData.cFileName, _T("..") ) )
  102. {
  103. continue;
  104. }
  105. szLongPathAndFilename[0] = _T('\0');
  106. if( ! ConcatenatePaths( szLongPathAndFilename,
  107. pszDirPath,
  108. FindData.cFileName,
  109. NULL ) )
  110. {
  111. continue;
  112. }
  113. if( ! GetShortPathName( szLongPathAndFilename, szShortPathAndFilename, MAX_PATH ) )
  114. {
  115. ReportErrorId(NULL,
  116. MSGTYPE_ERR | MSGTYPE_WIN32,
  117. IDS_DONTSPECIFYSETTING);
  118. continue;
  119. }
  120. pszShortName = MyGetFullPath( szShortPathAndFilename );
  121. //
  122. // If the LFN and the short name are different, then add it to the
  123. // rename file
  124. //
  125. if( pszShortName && ( lstrcmpi( FindData.cFileName, pszShortName ) != 0 ) )
  126. {
  127. AddEntryToRenameFile( pszDirPath, pszShortName, FindData.cFileName );
  128. }
  129. //
  130. // If this is a dir entry, recurse.
  131. //
  132. if( FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
  133. {
  134. //
  135. // Build the full pathname, if >= MAX_PATH, skip it
  136. //
  137. // Note: ConcatenatePaths truncates to avoid buffer overrun
  138. if( ! ConcatenatePaths( szNewPath,
  139. pszDirPath,
  140. FindData.cFileName,
  141. NULL ) )
  142. {
  143. continue;
  144. }
  145. CreateRenameFiles( szNewPath );
  146. szNewPath[0] = _T('\0');
  147. }
  148. } while ( FindNextFile( FindHandle, &FindData ) );
  149. FindClose( FindHandle );
  150. }
  151. //----------------------------------------------------------------------------
  152. //
  153. // Function: OnSetActiveSaveScript
  154. //
  155. // Purpose: Init's the controls.
  156. //
  157. //----------------------------------------------------------------------------
  158. VOID
  159. OnSetActiveSaveScript(HWND hwnd)
  160. {
  161. //
  162. // If there is no script name at all. Put a default into the field
  163. // that includes the path to the current directory
  164. //
  165. if ( FixedGlobals.ScriptName[0] == _T('\0') ) {
  166. LPTSTR lpPath;
  167. LPTSTR lpFileName;
  168. if ( WizGlobals.iProductInstall == PRODUCT_REMOTEINSTALL ) {
  169. lpFileName = DEFAULT_REMBOOT_NAME;
  170. }
  171. else if( WizGlobals.iProductInstall == PRODUCT_SYSPREP ) {
  172. lpFileName = DEFAULT_SYSPREP_NAME;
  173. }
  174. else {
  175. lpFileName = DEFAULT_SCRIPT_NAME;
  176. }
  177. if( WizGlobals.bStandAloneScript ||
  178. ( ! WizGlobals.bStandAloneScript && WizGlobals.iProductInstall == PRODUCT_SYSPREP ) )
  179. {
  180. lpPath = FixedGlobals.szSavePath;
  181. }
  182. else
  183. {
  184. lpPath = WizGlobals.DistFolder;
  185. }
  186. // Note: ConcatenatePaths truncates to avoid buffer overrun
  187. ConcatenatePaths(FixedGlobals.ScriptName, lpPath, lpFileName, NULL);
  188. }
  189. //
  190. // Set the controls
  191. //
  192. SetDlgItemText(hwnd, IDT_SCRIPTNAME, FixedGlobals.ScriptName);
  193. PropSheet_SetWizButtons(GetParent(hwnd), PSWIZB_BACK | PSWIZB_NEXT);
  194. }
  195. //----------------------------------------------------------------------------
  196. //
  197. // Function: OnWizNext
  198. //
  199. // Purpose: Get user's input and validate
  200. //
  201. //----------------------------------------------------------------------------
  202. BOOL
  203. OnWizNextSaveScript(HWND hwnd)
  204. {
  205. BOOL bResult = TRUE;
  206. //
  207. // Retrieve the pathname
  208. //
  209. GetDlgItemText(hwnd, IDT_SCRIPTNAME, FixedGlobals.ScriptName, MAX_PATH);
  210. //
  211. // Can't be empty
  212. //
  213. if ( FixedGlobals.ScriptName[0] == _T('\0') ) {
  214. ReportErrorId(hwnd, MSGTYPE_ERR, IDS_ERR_ENTER_FILENAME);
  215. bResult = FALSE;
  216. }
  217. //
  218. // Lookin' good, go try save the file(s) now
  219. //
  220. else {
  221. TCHAR szFullPath[MAX_PATH] = NULLSTR;
  222. LPTSTR lpFind = NULL;
  223. //
  224. // ISSUE-2002/02/27-stelo,swamip - Investigate the call below. Seems to be not using the return value.
  225. //
  226. MyGetFullPath(FixedGlobals.ScriptName);
  227. // We should create the path before we try and save the script file
  228. //
  229. if (GetFullPathName(FixedGlobals.ScriptName, AS(szFullPath), szFullPath, &lpFind) && szFullPath[0] && lpFind)
  230. {
  231. // Chop off the file part of the buffer
  232. //
  233. *lpFind = NULLCHR;
  234. // If the path does not exist, attempt to create the path
  235. //
  236. if ( !DirectoryExists(szFullPath) )
  237. {
  238. //
  239. // ISSUE-2002/02/27-stelo,swamip - Need to chech the return value of CreatePath
  240. //
  241. CreatePath(szFullPath);
  242. }
  243. }
  244. if ( ! SaveAllSettings(hwnd) )
  245. {
  246. if (ERROR_CANCELLED == GetLastError())
  247. {
  248. PostMessage(GetParent(hwnd),
  249. PSM_SETCURSELID,
  250. (WPARAM) 0,
  251. (LPARAM) IDD_FINISH2);
  252. return TRUE;
  253. }
  254. else
  255. {
  256. bResult = FALSE;
  257. }
  258. }
  259. }
  260. //
  261. // If it is a sysprep, place a copy of the inf and batch files in the
  262. // %systemdrive%\sysprep dir (most likely c:\sysprep)
  263. //
  264. if( WizGlobals.iProductInstall == PRODUCT_SYSPREP ) {
  265. TCHAR szSysprepPath[MAX_PATH] = _T("");
  266. TCHAR szSysprepPathAndFileName[MAX_PATH] = _T("");
  267. ExpandEnvironmentStrings( _T("%SystemDrive%"),
  268. szSysprepPath,
  269. MAX_PATH );
  270. lstrcatn( szSysprepPath, _T("\\sysprep"), MAX_PATH );
  271. // Note: ConcatenatePaths truncates to avoid buffer overrun
  272. ConcatenatePaths( szSysprepPathAndFileName,
  273. szSysprepPath,
  274. DEFAULT_SYSPREP_NAME,
  275. NULL );
  276. CopyFile( FixedGlobals.ScriptName, szSysprepPathAndFileName, FALSE );
  277. szSysprepPathAndFileName[0] = _T('\0');
  278. // Note: ConcatenatePaths truncates to avoid buffer overrun
  279. ConcatenatePaths( szSysprepPathAndFileName,
  280. szSysprepPath,
  281. DEFAULT_SYSPREP_BAT_NAME,
  282. NULL );
  283. CopyFile( FixedGlobals.BatchFileName, szSysprepPathAndFileName, FALSE );
  284. }
  285. //
  286. // Add the $$Rename.txt files where necessary
  287. //
  288. if( ! WizGlobals.bStandAloneScript &&
  289. WizGlobals.OemFilesPath[0] != _T('\0') )
  290. {
  291. CreateRenameFiles( WizGlobals.OemFilesPath );
  292. }
  293. return ( bResult );
  294. }
  295. //----------------------------------------------------------------------------
  296. //
  297. // Function: OnBrowseSaveScript
  298. //
  299. // Purpose: Takes care of the Browse... button push
  300. //
  301. //----------------------------------------------------------------------------
  302. VOID OnBrowseSaveScript(HWND hwnd)
  303. {
  304. //
  305. // Let user browse for a filename, then update the display with
  306. // the filename.
  307. //
  308. GetAnswerFileName(hwnd, FixedGlobals.ScriptName, TRUE);
  309. SendDlgItemMessage(hwnd,
  310. IDT_SCRIPTNAME,
  311. WM_SETTEXT,
  312. (WPARAM) MAX_PATH,
  313. (LPARAM) FixedGlobals.ScriptName);
  314. }
  315. //----------------------------------------------------------------------------
  316. //
  317. // Function: DlgSaveScriptPage
  318. //
  319. // Purpose: This is the dialog procedure the save script page.
  320. //
  321. //----------------------------------------------------------------------------
  322. INT_PTR
  323. CALLBACK
  324. DlgSaveScriptPage(
  325. IN HWND hwnd,
  326. IN UINT uMsg,
  327. IN WPARAM wParam,
  328. IN LPARAM lParam)
  329. {
  330. BOOL bStatus = TRUE;
  331. switch (uMsg) {
  332. case WM_INITDIALOG:
  333. // Activate the dialog
  334. //
  335. OnSetActiveSaveScript(hwnd);
  336. //
  337. // ISSUE-2002/02/27-stelo,swamip - Never gets Freed
  338. //
  339. StrWinntSifText = MyLoadString( IDS_WINNTSIF_TEXT );
  340. break;
  341. case WM_COMMAND:
  342. switch ( LOWORD(wParam) ) {
  343. case IDC_BROWSE:
  344. if ( HIWORD(wParam) == BN_CLICKED )
  345. OnBrowseSaveScript(hwnd);
  346. break;
  347. case IDOK:
  348. if ( OnWizNextSaveScript(hwnd) )
  349. EndDialog(hwnd, TRUE);
  350. break;
  351. case IDCANCEL:
  352. EndDialog(hwnd, FALSE);
  353. break;
  354. default:
  355. bStatus = FALSE;
  356. break;
  357. }
  358. break;
  359. case WM_NOTIFY:
  360. {
  361. LPNMHDR pnmh = (LPNMHDR)lParam;
  362. // ISSUE-2002/02/27-stelo,swamip - should check for valid pointer (possible dereference)
  363. //
  364. switch( pnmh->code ) {
  365. case PSN_QUERYCANCEL:
  366. WIZ_CANCEL(hwnd);
  367. break;
  368. case PSN_SETACTIVE:
  369. OnSetActiveSaveScript(hwnd);
  370. break;
  371. case PSN_WIZBACK:
  372. bStatus = FALSE;
  373. break;
  374. case PSN_WIZNEXT:
  375. if ( !OnWizNextSaveScript(hwnd) )
  376. WIZ_FAIL(hwnd);
  377. else
  378. bStatus = FALSE;
  379. break;
  380. default:
  381. bStatus = FALSE;
  382. break;
  383. }
  384. }
  385. break;
  386. default:
  387. bStatus = FALSE;
  388. break;
  389. }
  390. return bStatus;
  391. }