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.

1674 lines
54 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation
  3. Module Name:
  4. inuse.cpp
  5. Abstract:
  6. This file can be used to replace the file which currently locked
  7. by the operating system.
  8. Authors:
  9. Choudary - Wipro Technologies, 07-Aug-2001
  10. Revision History:
  11. 07-Aug-2001 : Created by Wipro Technologies.
  12. --*/
  13. //common header files needed for this file
  14. #include "pch.h"
  15. #include "inuse.h"
  16. #include "resource.h"
  17. //Global variable
  18. DWORD g_dwRetVal = 0;
  19. VOID
  20. DisplayHelp ( VOID )
  21. /*++
  22. Routine Description:
  23. This function displays the help/usage for this utility.
  24. Arguments:
  25. None
  26. Return Value:
  27. None
  28. --*/
  29. {
  30. //sub-local variables
  31. WORD wCount = 0;
  32. // display the help/usage for this tool
  33. for ( wCount = IDS_INUSE_HLP_START; wCount <= IDS_INUSE_HLP_END ; wCount++ )
  34. {
  35. ShowMessage ( stdout, GetResString ( wCount ) );
  36. }
  37. return;
  38. }
  39. DWORD __cdecl
  40. wmain(
  41. IN DWORD argc,
  42. IN LPCWSTR argv[]
  43. )
  44. /*++
  45. Routine Description:
  46. This is the main entry for this utility. This function reads the input from
  47. console and calls the appropriate functions to achieve the functionality.
  48. Arguments:
  49. [IN] argc : Command line argument count
  50. [IN] argv : Command line argument
  51. Return Value:
  52. EXIT_FAILURE : On failure
  53. EXIT_SUCCESS : On success
  54. --*/
  55. {
  56. // Local variables
  57. BOOL bUsage = FALSE ;
  58. BOOL bConfirm = FALSE;
  59. DWORD dwRetVal = 0;
  60. LPWSTR wszReplace = NULL;
  61. LPWSTR wszDest = NULL;
  62. LPWSTR wszBuffer = NULL;
  63. LPWSTR wszFindStr = NULL;
  64. TARRAY arrValue = NULL ;
  65. DWORD dwDynCount = 0;
  66. LPWSTR wszTmpRFile = NULL;
  67. LPWSTR wszTmpDFile = NULL;
  68. LPWSTR wszTmpBuf1 = NULL;
  69. LPWSTR wszTmpBuf2 = NULL;
  70. LONG lRetVal = 0;
  71. const WCHAR szArr[] = L"\\\\";
  72. const WCHAR szTokens[] = L"/";
  73. DWORD dwLength = 0;
  74. HANDLE HndFile = 0;
  75. LPWSTR szSystemName = NULL;
  76. //create a dynamic array
  77. arrValue = CreateDynamicArray();
  78. //check if arrValue is empty
  79. if(arrValue == NULL )
  80. {
  81. // set the error with respect to the GetReason()
  82. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  83. SaveLastError();
  84. // Display an error message with respect to GetReason()
  85. ShowLastErrorEx( stderr, SLE_TYPE_ERROR | SLE_INTERNAL);
  86. return (EXIT_FAILURE);
  87. }
  88. TCMDPARSER2 cmdInuseOptions[MAX_INUSE_OPTIONS];
  89. BOOL bReturn = FALSE;
  90. // /run sub-options
  91. const WCHAR szInuseHelpOpt[] = L"?";
  92. const WCHAR szInuseConfirmOpt[] = L"y";
  93. // set all the fields to 0
  94. SecureZeroMemory( cmdInuseOptions, sizeof( TCMDPARSER2 ) * MAX_INUSE_OPTIONS );
  95. //
  96. // fill the commandline parser
  97. //
  98. // /? option
  99. StringCopyA( cmdInuseOptions[ OI_USAGE ].szSignature, "PARSER2\0", 8 );
  100. cmdInuseOptions[ OI_USAGE ].dwType = CP_TYPE_BOOLEAN;
  101. cmdInuseOptions[ OI_USAGE ].pwszOptions = szInuseHelpOpt;
  102. cmdInuseOptions[ OI_USAGE ].dwCount = 1;
  103. cmdInuseOptions[ OI_USAGE ].dwFlags = CP2_USAGE;
  104. cmdInuseOptions[ OI_USAGE ].pValue = &bUsage;
  105. // /default arguments
  106. StringCopyA( cmdInuseOptions[ OI_DEFAULT ].szSignature, "PARSER2\0", 8 );
  107. cmdInuseOptions[ OI_DEFAULT ].dwType = CP_TYPE_TEXT;
  108. cmdInuseOptions[ OI_DEFAULT ].pwszOptions = NULL;
  109. cmdInuseOptions[ OI_DEFAULT ].dwCount = 2;
  110. cmdInuseOptions[ OI_DEFAULT ].dwFlags = CP2_MODE_ARRAY|CP2_DEFAULT;
  111. cmdInuseOptions[ OI_DEFAULT ].pValue = &arrValue;
  112. // /y option
  113. StringCopyA( cmdInuseOptions[ OI_CONFIRM ].szSignature, "PARSER2\0", 8 );
  114. cmdInuseOptions[ OI_CONFIRM ].dwType = CP_TYPE_BOOLEAN;
  115. cmdInuseOptions[ OI_CONFIRM ].pwszOptions = szInuseConfirmOpt;
  116. cmdInuseOptions[ OI_CONFIRM ].dwCount = 1;
  117. cmdInuseOptions[ OI_CONFIRM ].dwFlags = 0;
  118. cmdInuseOptions[ OI_CONFIRM ].pValue = &bConfirm;
  119. //parse command line arguments
  120. bReturn = DoParseParam2( argc, argv, -1, SIZE_OF_ARRAY(cmdInuseOptions), cmdInuseOptions, 0);
  121. if( FALSE == bReturn) // Invalid commandline
  122. {
  123. //display an error message
  124. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  125. ReleaseGlobals();
  126. return EXIT_FAILURE;
  127. }
  128. // get the number of rows in a array
  129. dwDynCount = DynArrayGetCount(arrValue);
  130. // check for invalid syntax
  131. if( (argc == 1) ||
  132. ( ( TRUE == bUsage ) && ( argc > 2 ) ) ||
  133. ( ( FALSE == bUsage ) && ( dwDynCount < 2 ) ) ||
  134. ( ( TRUE == bConfirm ) && ( argc > 6 ) )
  135. )
  136. {
  137. //display an error message as specified syntax is invalid
  138. ShowMessage( stderr, GetResString(IDS_INVALID_SYNERROR ));
  139. DestroyDynamicArray(&arrValue);
  140. ReleaseGlobals();
  141. return EXIT_FAILURE;
  142. }
  143. // check whether /? is specified or not. If so, display the usage
  144. // for this utility.
  145. if ( TRUE == bUsage )
  146. {
  147. // display help/usage
  148. DisplayHelp();
  149. //release memory
  150. DestroyDynamicArray(&arrValue);
  151. ReleaseGlobals();
  152. return EXIT_SUCCESS;
  153. }
  154. // if count is 2 ..then get the values for Replacement and Destination
  155. // get the value for Replacement file
  156. wszReplace = (LPWSTR)DynArrayItemAsString( arrValue, 0 );
  157. // get the value for Destination file to be replaced
  158. wszDest = (LPWSTR)DynArrayItemAsString( arrValue, 1 );
  159. //check if replacement file is empty
  160. if ( 0 == StringLength (wszReplace, 0) )
  161. {
  162. // display an error message as .. empty value specified for Replacement file..
  163. ShowMessage ( stderr, GetResString ( IDS_SOURCE_NOT_NULL));
  164. DestroyDynamicArray(&arrValue);
  165. ReleaseGlobals();
  166. return EXIT_FAILURE;
  167. }
  168. //check if destination file is empty
  169. if ( 0 == StringLength (wszDest, 0))
  170. {
  171. // display an error message as .. empty value specified for Destination file..
  172. ShowMessage ( stderr, GetResString ( IDS_DEST_NOT_NULL));
  173. DestroyDynamicArray(&arrValue);
  174. ReleaseGlobals();
  175. return EXIT_FAILURE;
  176. }
  177. // check whether replacement file consists special characters i.e. '/'
  178. if( wcspbrk(wszReplace,szTokens) != NULL )
  179. {
  180. // display an error message as.. replacement file should not contain '/'
  181. ShowMessage ( stderr, GetResString ( IDS_INVALID_SOURCE ) );
  182. DestroyDynamicArray(&arrValue);
  183. ReleaseGlobals();
  184. return EXIT_FAILURE;
  185. }
  186. // check whether destination file consists special characters i.e. '/'
  187. if( wcspbrk(wszDest,szTokens) != NULL )
  188. {
  189. // display an error message as.. destination file should not contain '/'
  190. ShowMessage ( stderr, GetResString ( IDS_INVALID_DEST ) );
  191. DestroyDynamicArray(&arrValue);
  192. ReleaseGlobals();
  193. return EXIT_FAILURE;
  194. }
  195. // get the actual length of full path for replacement file
  196. dwLength = GetFullPathNameW( wszReplace, 0, NULL, &wszTmpBuf1);
  197. if ( dwLength == 0)
  198. {
  199. // display an error message with respect to GetLastError()
  200. //DisplayErrorMsg (GetLastError());
  201. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  202. DestroyDynamicArray(&arrValue);
  203. ReleaseGlobals();
  204. return EXIT_FAILURE;
  205. }
  206. // allocate memory with the actual length for a replacement file
  207. wszTmpRFile = (LPWSTR) AllocateMemory ((dwLength+20) * sizeof (WCHAR));
  208. if ( NULL == wszTmpRFile )
  209. {
  210. // display an error message with respect to GetLastError()
  211. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  212. DestroyDynamicArray(&arrValue);
  213. ReleaseGlobals();
  214. return EXIT_FAILURE;
  215. }
  216. // Get full path for a replacement file
  217. if ( GetFullPathNameW( wszReplace, GetBufferSize(wszTmpRFile)/sizeof(WCHAR), wszTmpRFile, &wszTmpBuf1) == 0)
  218. {
  219. // display an error message with respect to GetLastError()
  220. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  221. FreeMemory ((LPVOID*) &wszTmpRFile);
  222. DestroyDynamicArray(&arrValue);
  223. ReleaseGlobals();
  224. return EXIT_FAILURE;
  225. }
  226. // Get the actual length of full path for destination file
  227. dwLength = GetFullPathNameW( wszDest, 0, NULL, &wszTmpBuf2);
  228. if ( dwLength == 0)
  229. {
  230. // display an error message with respect to GetLastError()
  231. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  232. FreeMemory ((LPVOID*) &wszTmpRFile);
  233. DestroyDynamicArray(&arrValue);
  234. ReleaseGlobals();
  235. return EXIT_FAILURE;
  236. }
  237. // allocate memory with the actual length for a destination file
  238. wszTmpDFile = (LPWSTR) AllocateMemory ((dwLength+20) * sizeof (WCHAR));
  239. if ( NULL == wszTmpDFile )
  240. {
  241. // display an error message with respect to GetLastError()
  242. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  243. FreeMemory ((LPVOID*) &wszTmpRFile);
  244. DestroyDynamicArray(&arrValue);
  245. ReleaseGlobals();
  246. return EXIT_FAILURE;
  247. }
  248. // Get full path for destination file
  249. if ( GetFullPathNameW( wszDest, GetBufferSize(wszTmpDFile)/sizeof(WCHAR), wszTmpDFile, &wszTmpBuf2) == 0)
  250. {
  251. // display an error message with respect to GetLastError()
  252. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  253. DestroyDynamicArray(&arrValue);
  254. FreeMemory ((LPVOID*) &wszTmpRFile);
  255. FreeMemory ((LPVOID*) &wszTmpDFile);
  256. ReleaseGlobals();
  257. return EXIT_FAILURE;
  258. }
  259. // get the file attributes of replacement file
  260. dwRetVal = GetFileAttributes( wszReplace );
  261. // check if the GetFileAttributes() failed
  262. if ( INVALID_FILE_ATTRIBUTES == dwRetVal )
  263. {
  264. wszBuffer = (LPWSTR) AllocateMemory (GetBufferSize(wszTmpRFile) + MAX_RES_STRING );
  265. if ( NULL == wszBuffer )
  266. {
  267. // display an error message with respect to GetLastError()
  268. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  269. FreeMemory ((LPVOID*) &wszTmpRFile);
  270. FreeMemory ((LPVOID*) &wszTmpDFile);
  271. DestroyDynamicArray(&arrValue);
  272. ReleaseGlobals();
  273. return EXIT_FAILURE;
  274. }
  275. // format the message as .. replacement file not exists in the system.
  276. StringCchPrintf ( wszBuffer , GetBufferSize(wszBuffer)/sizeof(WCHAR),
  277. GetResString ( IDS_REPLACE_FILE_NOT_EXISTS), wszTmpRFile );
  278. // display the formatted message
  279. ShowMessage ( stderr, _X(wszBuffer) );
  280. DestroyDynamicArray(&arrValue);
  281. FreeMemory ((LPVOID*) &wszBuffer);
  282. FreeMemory ((LPVOID*) &wszTmpRFile);
  283. FreeMemory ((LPVOID*) &wszTmpDFile);
  284. ReleaseGlobals();
  285. return EXIT_FAILURE;
  286. }
  287. // check whether the source file is directory or not
  288. if ( dwRetVal & FILE_ATTRIBUTE_DIRECTORY )
  289. {
  290. wszBuffer = (LPWSTR) AllocateMemory (GetBufferSize(wszReplace) + MAX_RES_STRING );
  291. if ( NULL == wszBuffer )
  292. {
  293. // display an error message with respect to GetLastError()
  294. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  295. FreeMemory ((LPVOID*) &wszTmpRFile);
  296. FreeMemory ((LPVOID*) &wszTmpDFile);
  297. DestroyDynamicArray(&arrValue);
  298. ReleaseGlobals();
  299. return EXIT_FAILURE;
  300. }
  301. // format the message as .. replacement file is a directory.. not a file
  302. StringCchPrintf ( wszBuffer , GetBufferSize(wszBuffer)/sizeof(WCHAR),
  303. GetResString ( IDS_SOURCE_NOT_FILE), wszReplace );
  304. // display the formatted message
  305. ShowMessage ( stderr, _X(wszBuffer) );
  306. DestroyDynamicArray(&arrValue);
  307. FreeMemory ((LPVOID*) &wszBuffer);
  308. FreeMemory ((LPVOID*) &wszTmpRFile);
  309. FreeMemory ((LPVOID*) &wszTmpDFile);
  310. ReleaseGlobals();
  311. return EXIT_FAILURE;
  312. }
  313. // restrict the destination file with the UNC format
  314. lRetVal = StringCompare( wszDest, szArr , TRUE, 2 );
  315. if ( 0 == lRetVal )
  316. {
  317. // get the token till '\'
  318. wszFindStr = wcstok ( wszDest, BACK_SLASH);
  319. // check if failed
  320. if ( NULL != wszFindStr )
  321. {
  322. // get the token till '\'
  323. wszFindStr = wcstok ( wszDest, BACK_SLASH);
  324. // check if the specified is local or not
  325. if ( ( wszFindStr != NULL ) && ( IsLocalSystem ( wszFindStr ) == FALSE ) )
  326. {
  327. // display an error message as ..UNC format is not allowed for destinaton..
  328. ShowMessage ( stderr, GetResString (IDS_DEST_NOT_ALLOWED) );
  329. DestroyDynamicArray(&arrValue);
  330. FreeMemory ((LPVOID*) &wszTmpRFile);
  331. FreeMemory ((LPVOID*) &wszTmpDFile);
  332. ReleaseGlobals();
  333. return EXIT_FAILURE;
  334. }
  335. }
  336. else
  337. {
  338. // display an error message as ..UNC format is not allowed for destinaton..
  339. ShowMessage ( stderr, GetResString (IDS_DEST_NOT_ALLOWED) );
  340. DestroyDynamicArray(&arrValue);
  341. FreeMemory ((LPVOID*) &wszTmpRFile);
  342. FreeMemory ((LPVOID*) &wszTmpDFile);
  343. ReleaseGlobals();
  344. return EXIT_FAILURE;
  345. }
  346. }
  347. // get the file attributes of destination file
  348. dwRetVal = GetFileAttributes( wszDest );
  349. // check if the GetFileAttributes() failed
  350. if ( INVALID_FILE_ATTRIBUTES == dwRetVal )
  351. {
  352. wszBuffer = (LPWSTR) AllocateMemory (GetBufferSize(wszTmpDFile) + MAX_RES_STRING );
  353. if ( NULL == wszBuffer )
  354. {
  355. // display an error message with respect to GetLastError()
  356. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  357. FreeMemory ((LPVOID*) &wszTmpRFile);
  358. FreeMemory ((LPVOID*) &wszTmpDFile);
  359. DestroyDynamicArray(&arrValue);
  360. ReleaseGlobals();
  361. return EXIT_FAILURE;
  362. }
  363. // format the message as .. destination file is not exists in the system.
  364. StringCchPrintf ( wszBuffer , GetBufferSize(wszBuffer)/sizeof(WCHAR),
  365. GetResString (IDS_DEST_FILE_NOT_EXISTS), wszTmpDFile );
  366. // display the formatted message
  367. ShowMessage ( stderr, _X(wszBuffer) );
  368. DestroyDynamicArray(&arrValue);
  369. FreeMemory ((LPVOID*) &wszBuffer);
  370. FreeMemory ((LPVOID*) &wszTmpRFile);
  371. FreeMemory ((LPVOID*) &wszTmpDFile);
  372. ReleaseGlobals();
  373. return EXIT_FAILURE;
  374. }
  375. // check whether the destination file is exist or not
  376. if ( dwRetVal & FILE_ATTRIBUTE_DIRECTORY )
  377. {
  378. wszBuffer = (LPWSTR) AllocateMemory (GetBufferSize(wszDest) + MAX_RES_STRING );
  379. if ( NULL == wszBuffer )
  380. {
  381. // display an error message with respect to GetLastError()
  382. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  383. FreeMemory ((LPVOID*) &wszTmpRFile);
  384. FreeMemory ((LPVOID*) &wszTmpDFile);
  385. DestroyDynamicArray(&arrValue);
  386. ReleaseGlobals();
  387. return EXIT_FAILURE;
  388. }
  389. // format the message as .. destination is a directory .. not a file.
  390. StringCchPrintf ( wszBuffer , GetBufferSize(wszBuffer)/sizeof(WCHAR), GetResString ( IDS_DEST_NOT_FILE), wszDest );
  391. // display the formatted message
  392. ShowMessage ( stderr, _X(wszBuffer) );
  393. DestroyDynamicArray(&arrValue);
  394. FreeMemory ((LPVOID*) &wszBuffer);
  395. FreeMemory ((LPVOID*) &wszTmpRFile);
  396. FreeMemory ((LPVOID*) &wszTmpDFile);
  397. ReleaseGlobals();
  398. return EXIT_FAILURE;
  399. }
  400. // check whether replacement and destination files are same or not
  401. // if same, display an error message..
  402. if ( ( (StringLength (wszTmpRFile, 0) != 0) && (StringLength (wszTmpDFile, 0) != 0) ) &&
  403. (StringCompare (wszTmpRFile, wszTmpDFile, TRUE, 0) == 0) )
  404. {
  405. // display an error message as.. replacement and destination cannot be same..
  406. ShowMessage ( stderr, GetResString ( IDS_NOT_REPLACE));
  407. DestroyDynamicArray(&arrValue);
  408. FreeMemory ((LPVOID*) &wszTmpRFile);
  409. FreeMemory ((LPVOID*) &wszTmpDFile);
  410. ReleaseGlobals();
  411. return EXIT_FAILURE;
  412. }
  413. // check whether the replacement file accessible or not
  414. HndFile = CreateFile( wszReplace , 0, FILE_SHARE_READ , NULL,
  415. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
  416. // check if CreateFile() is failed
  417. if ( INVALID_HANDLE_VALUE == HndFile )
  418. {
  419. wszBuffer = (LPWSTR) AllocateMemory (GetBufferSize(wszReplace) + MAX_RES_STRING );
  420. if ( NULL == wszBuffer )
  421. {
  422. // display an error message with respect to GetLastError()
  423. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  424. FreeMemory ((LPVOID*) &wszTmpRFile);
  425. FreeMemory ((LPVOID*) &wszTmpDFile);
  426. DestroyDynamicArray(&arrValue);
  427. ReleaseGlobals();
  428. return EXIT_FAILURE;
  429. }
  430. // format the message as .. destination is a directory .. not a file.
  431. StringCchPrintf ( wszBuffer , GetBufferSize(wszBuffer)/sizeof(WCHAR), GetResString (IDS_REPLACE_ACCESS_DENIED), wszReplace );
  432. // display the formatted message
  433. ShowMessage ( stderr, _X(wszBuffer) );
  434. FreeMemory ((LPVOID*) &wszBuffer);
  435. DestroyDynamicArray(&arrValue);
  436. //ShowMessage ( stderr, GetResString (IDS_REPLACE_ACCESS_DENIED) );
  437. return EXIT_FAILURE;
  438. }
  439. //close the handle
  440. CloseHandle (HndFile);
  441. // check whether the destination file accessible or not
  442. HndFile = CreateFile( wszDest , 0, FILE_SHARE_READ , NULL,
  443. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
  444. // check if CreateFile() is failed
  445. if ( INVALID_HANDLE_VALUE == HndFile )
  446. {
  447. wszBuffer = (LPWSTR) AllocateMemory (GetBufferSize(wszDest) + MAX_RES_STRING );
  448. if ( NULL == wszBuffer )
  449. {
  450. // display an error message with respect to GetLastError()
  451. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  452. FreeMemory ((LPVOID*) &wszTmpRFile);
  453. FreeMemory ((LPVOID*) &wszTmpDFile);
  454. DestroyDynamicArray(&arrValue);
  455. ReleaseGlobals();
  456. return EXIT_FAILURE;
  457. }
  458. // format the message as .. destination is a directory .. not a file.
  459. StringCchPrintf ( wszBuffer , GetBufferSize(wszBuffer)/sizeof(WCHAR), GetResString (IDS_DEST_ACCESS_DENIED), wszDest );
  460. // display the formatted message
  461. ShowMessage ( stderr, _X(wszBuffer) );
  462. DestroyDynamicArray(&arrValue);
  463. FreeMemory ((LPVOID*) &wszBuffer);
  464. return EXIT_FAILURE;
  465. }
  466. //close the handle
  467. CloseHandle (HndFile);
  468. // Get the target system name of a source file
  469. lRetVal = StringCompare( wszReplace, szArr , TRUE, 2 );
  470. if ( 0 == lRetVal )
  471. {
  472. dwLength = StringLength (wszReplace, 0 );
  473. szSystemName = (LPWSTR) AllocateMemory ((dwLength+20) * sizeof(WCHAR));
  474. if ( NULL == szSystemName )
  475. {
  476. // display an error message with respect to GetLastError()
  477. //DisplayErrorMsg (GetLastError());
  478. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  479. FreeMemory ((LPVOID*) &wszTmpRFile);
  480. FreeMemory ((LPVOID*) &wszTmpDFile);
  481. DestroyDynamicArray(&arrValue);
  482. ReleaseGlobals();
  483. return EXIT_FAILURE;
  484. }
  485. StringCopy (szSystemName, wszReplace, GetBufferSize(szSystemName));
  486. // get the token till '\'
  487. wszFindStr = wcstok ( szSystemName, BACK_SLASH);
  488. // check if failed
  489. if ( NULL != wszFindStr )
  490. {
  491. wszFindStr = wcstok ( szSystemName, BACK_SLASH);
  492. // get the token till '\'
  493. StringCopy (szSystemName, wszFindStr, GetBufferSize(szSystemName));
  494. }
  495. }
  496. // move the contents of the replacement file into destination file.
  497. // but changes will not effect until reboot
  498. if ( FALSE == ReplaceFileInUse( wszReplace, wszDest , wszTmpRFile, wszTmpDFile, bConfirm, szSystemName) )
  499. {
  500. // check if invalid input entered while confirming the input (y/n)
  501. if ( g_dwRetVal != EXIT_ON_ERROR )
  502. {
  503. // Display an error message with respect to GetReason()
  504. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_INTERNAL );
  505. }
  506. DestroyDynamicArray(&arrValue);
  507. FreeMemory ((LPVOID*) &wszTmpRFile);
  508. FreeMemory ((LPVOID*) &wszTmpDFile);
  509. FreeMemory ((LPVOID*) &szSystemName);
  510. ReleaseGlobals();
  511. return EXIT_FAILURE;
  512. }
  513. if ( g_dwRetVal != EXIT_ON_CANCEL )
  514. {
  515. wszBuffer = (LPWSTR) AllocateMemory (GetBufferSize(wszTmpRFile) + GetBufferSize(wszTmpDFile) + 2 * MAX_RES_STRING );
  516. if ( NULL == wszBuffer )
  517. {
  518. // display an error message with respect to GetLastError()
  519. //DisplayErrorMsg (GetLastError());
  520. ShowLastErrorEx ( stderr, SLE_TYPE_ERROR | SLE_SYSTEM );
  521. FreeMemory ((LPVOID*) &wszTmpRFile);
  522. FreeMemory ((LPVOID*) &wszTmpDFile);
  523. FreeMemory ((LPVOID*) &szSystemName);
  524. DestroyDynamicArray(&arrValue);
  525. ReleaseGlobals();
  526. return EXIT_FAILURE;
  527. }
  528. // format the message as .. copy done successfully....
  529. StringCchPrintf ( wszBuffer , GetBufferSize(wszBuffer)/sizeof(WCHAR), GetResString (IDS_COPY_DONE),
  530. wszTmpRFile, wszTmpDFile );
  531. // display the formatted message
  532. ShowMessage ( stdout, _X(wszBuffer) );
  533. FreeMemory ((LPVOID*) &wszBuffer);
  534. }
  535. DestroyDynamicArray(&arrValue);
  536. FreeMemory ((LPVOID*) &wszTmpRFile);
  537. FreeMemory ((LPVOID*) &wszTmpDFile);
  538. FreeMemory ((LPVOID*) &szSystemName);
  539. ReleaseGlobals();
  540. return EXIT_SUCCESS;
  541. }
  542. BOOL
  543. ReplaceFileInUse(
  544. IN LPWSTR pwszSource,
  545. IN LPWSTR pwszDestination ,
  546. IN LPWSTR pwszSourceFullPath,
  547. IN LPWSTR pwszDestFullPath,
  548. IN BOOL bConfirm,
  549. IN LPWSTR szSysName
  550. )
  551. /*++
  552. Routine Description:
  553. This function moves the contents of replacement file into destination file
  554. Arguments:
  555. [IN] pwszSource : Replacement fiele
  556. [IN] pwszDestination : Destination file
  557. [IN] bConfirm : confirm input
  558. Return Value:
  559. FALSE : On failure
  560. TRUE : On success
  561. --*/
  562. {
  563. // local variables
  564. DWORD dwLength = 0;
  565. CHString strPath;
  566. CHString strFileName;
  567. LPWSTR wszTmpFileBuf = NULL;
  568. LPWSTR wszDestFileBuf = NULL;
  569. LPWSTR wszTmpFileName = NULL;
  570. WCHAR wszBuffer [MAX_RES_STRING];
  571. #ifdef _WIN64
  572. INT64 dwPos ;
  573. #else
  574. DWORD dwPos ;
  575. #endif
  576. // initialize the variable
  577. SecureZeroMemory ( wszBuffer, SIZE_OF_ARRAY(wszBuffer) );
  578. // display the destination file related information
  579. if ( EXIT_FAILURE == DisplayFileInfo ( pwszDestination, pwszDestFullPath, TRUE ) )
  580. {
  581. return FALSE;
  582. }
  583. // display the replacement file related information
  584. if ( EXIT_FAILURE == DisplayFileInfo ( pwszSource, pwszSourceFullPath, FALSE ) )
  585. {
  586. return FALSE;
  587. }
  588. // check whether to prompt for confirmation or not.
  589. if ( FALSE == bConfirm )
  590. {
  591. // to be added the fn
  592. if ( (EXIT_FAILURE == ConfirmInput ()) || (EXIT_ON_ERROR == g_dwRetVal) )
  593. {
  594. // could not get the handle so return failure
  595. return FALSE;
  596. }
  597. else if ( EXIT_ON_CANCEL == g_dwRetVal )
  598. {
  599. //operation has been cancelled.. so..return..
  600. return TRUE;
  601. }
  602. }
  603. // form the unique temp file name
  604. try
  605. {
  606. // sub-local variables
  607. DWORD dw = 0;
  608. LPWSTR pwsz = NULL;
  609. //
  610. // get the temporary file path location
  611. //
  612. // get the buffer to hold the temp. path information
  613. pwsz = strPath.GetBufferSetLength( MAX_PATH );
  614. // get the temp. path information
  615. dw = GetTempPath( MAX_PATH, pwsz );
  616. // check whether the buffer which we passed is sufficient or not
  617. if ( dw > MAX_PATH )
  618. {
  619. // the buffer we passed to the API is not sufficient -- need re-allocation
  620. // since the value in the dwLength variable is needed length -- just one more
  621. // call to the API function will suffice
  622. pwsz = strPath.GetBufferSetLength( dw + 2 ); // +2 is needed for NULL character
  623. // now get the temp. path once again
  624. dw = GetTempPath( dw, pwsz );
  625. }
  626. // check the result of the operation
  627. if ( dw == 0 )
  628. {
  629. // encountered problem
  630. SaveLastError();
  631. strPath.ReleaseBuffer();
  632. return FALSE;
  633. }
  634. // release the buffer
  635. pwsz = NULL;
  636. strPath.ReleaseBuffer();
  637. //
  638. // get the temporary file name
  639. //
  640. // get the buffer to hold the temp. path information
  641. pwsz = strFileName.GetBufferSetLength( MAX_PATH );
  642. // get the temp. file name
  643. dw = GetTempFileName( strPath, L"INUSE", 0, pwsz );
  644. // check the result
  645. if ( dw == 0 )
  646. {
  647. // encountered problem
  648. SaveLastError();
  649. strFileName.ReleaseBuffer();
  650. return FALSE;
  651. }
  652. // release the buffer
  653. pwsz = NULL;
  654. strFileName.ReleaseBuffer();
  655. }
  656. catch( ... )
  657. {
  658. SetLastError( (DWORD)E_OUTOFMEMORY );
  659. SaveLastError();
  660. return FALSE;
  661. }
  662. //1.Create a temp file in the destination directory and copy replacement file into a temp file.
  663. //2.Delete the destination file at the time of reboot and by using MoveFileEx api..
  664. //3.Copy temp file into destination file by using MoveFileEx api..So that temp file
  665. // would get deleted at the time of reboot.
  666. {
  667. StringCopy ( wszBuffer, strFileName, SIZE_OF_ARRAY(wszBuffer));
  668. //Get the temporary file name
  669. wszTmpFileName = StrRChr ( wszBuffer, NULL, L'\\' );
  670. if ( NULL == wszTmpFileName )
  671. {
  672. SetLastError( (DWORD)E_UNEXPECTED );
  673. SaveLastError();
  674. return FALSE;
  675. }
  676. // to be implemented
  677. wszTmpFileBuf = StrRChr ( pwszDestFullPath, NULL, L'\\' );
  678. if ( NULL == wszTmpFileBuf )
  679. {
  680. SetLastError( (DWORD)E_UNEXPECTED );
  681. SaveLastError();
  682. return FALSE;
  683. }
  684. // get the position
  685. dwPos = wszTmpFileBuf - pwszDestFullPath ;
  686. dwLength = StringLength ( pwszDestFullPath, 0 );
  687. // allocate memory with the actual length for a replacement file
  688. wszDestFileBuf = (LPWSTR) AllocateMemory ( dwLength + MAX_RES_STRING );
  689. if ( NULL == wszDestFileBuf )
  690. {
  691. // display an error message with respect to GetLastError()
  692. SetLastError( (DWORD)E_OUTOFMEMORY );
  693. SaveLastError();
  694. return FALSE;
  695. }
  696. //consider the destination file path as the file path for temporary file.
  697. StringCopy ( wszDestFileBuf, pwszDestFullPath, (DWORD) (dwPos + 1) );
  698. StringCchPrintf (wszDestFileBuf, GetBufferSize (wszDestFileBuf)/sizeof(WCHAR), L"%s%s", wszDestFileBuf, wszTmpFileName);
  699. // copy the source file as temporary file name
  700. if ( FALSE == CopyFile( pwszSource, wszDestFileBuf, FALSE ) )
  701. {
  702. if ( ERROR_ACCESS_DENIED == GetLastError () )
  703. {
  704. g_dwRetVal = EXIT_ON_ERROR;
  705. ShowMessage ( stderr, GetResString (IDS_DEST_DIR_DENIED) );
  706. }
  707. else
  708. {
  709. SaveLastError();
  710. }
  711. FreeMemory ((LPVOID*) &wszDestFileBuf);
  712. return FALSE;
  713. }
  714. //
  715. //copy ACLs of destination file into a temp file
  716. //
  717. PSID sidOwner = NULL;
  718. PSID sidGroup = NULL;
  719. PACL pOldDacl= NULL ;
  720. PACL pOldSacl = NULL ;
  721. PSECURITY_DESCRIPTOR pSecurityDescriptor = NULL;
  722. DWORD dwError = 0;
  723. BOOL bResult = FALSE;
  724. // enable seSecurityPrivilege
  725. if (!SetPrivilege (szSysName))
  726. {
  727. SaveLastError();
  728. FreeMemory ((LPVOID*) &wszDestFileBuf);
  729. return FALSE;
  730. }
  731. // get the DACLs of source file
  732. dwError = GetNamedSecurityInfo ( pwszSource, SE_FILE_OBJECT,
  733. DACL_SECURITY_INFORMATION |SACL_SECURITY_INFORMATION| GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
  734. &sidOwner,
  735. &sidGroup,
  736. &pOldDacl,
  737. &pOldSacl,
  738. &pSecurityDescriptor);
  739. //check for return value
  740. if (ERROR_SUCCESS != dwError )
  741. {
  742. SaveLastError();
  743. FreeMemory ((LPVOID*) &wszDestFileBuf);
  744. return FALSE;
  745. }
  746. // Set the DACLs of source file to a temp file
  747. bResult = SetFileSecurity(wszDestFileBuf,
  748. DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION| GROUP_SECURITY_INFORMATION |OWNER_SECURITY_INFORMATION ,
  749. pSecurityDescriptor);
  750. //check for return value
  751. if (FALSE == bResult )
  752. {
  753. // continue to replace the file at the time of reboot...
  754. }
  755. //release the security descriptor
  756. if ( NULL != pSecurityDescriptor)
  757. {
  758. LocalFree (&pSecurityDescriptor);
  759. }
  760. //
  761. // start replacing file
  762. //
  763. // now move this destination file -- means delete this file
  764. if ( FALSE == MoveFileEx( pwszDestination, NULL, MOVEFILE_DELAY_UNTIL_REBOOT ) )
  765. {
  766. // this will never occur since the operation result is not
  767. // known until the reboot happens
  768. SaveLastError();
  769. FreeMemory ((LPVOID*) &wszDestFileBuf);
  770. return FALSE;
  771. }
  772. // now move the temp. file as destination file
  773. if ( FALSE == MoveFileEx( wszDestFileBuf, pwszDestination, MOVEFILE_DELAY_UNTIL_REBOOT ) )
  774. {
  775. // this will never occur since the operation result is not
  776. // known until the reboot happens
  777. SetLastError( (DWORD)E_UNEXPECTED );
  778. SaveLastError();
  779. FreeMemory ((LPVOID*) &wszDestFileBuf);
  780. return FALSE;
  781. }
  782. // de-allocate the memory
  783. FreeMemory ((LPVOID*) &wszDestFileBuf);
  784. }
  785. // everything went well -- return success
  786. return TRUE;
  787. }
  788. DWORD
  789. DisplayFileInfo (
  790. IN LPWSTR pwszFileName,
  791. IN LPWSTR pwszFileFullPath,
  792. BOOL bFlag
  793. )
  794. /*++
  795. Routine Description:
  796. This function displays the information of replacement and destination files
  797. Arguments:
  798. [IN] pwszFileName : Replacement/Destination file name
  799. [IN] pwszFileFullPath : Replacement/Destination full path
  800. [IN] bFlag : TRUE for Destination file
  801. FALSE for Replacement file
  802. Return Value:
  803. EXIT_FAILURE : On failure
  804. EXIT_SUCCESS : On success
  805. --*/
  806. {
  807. // sub-local variables
  808. DWORD dw = 0;
  809. DWORD dwSize = 0;
  810. UINT uSize = 0;
  811. WCHAR wszData[MAX_RES_STRING] = NULL_STRING;
  812. WCHAR wszSubBlock[MAX_RES_STRING] = L"";
  813. WCHAR wszBuffer[MAX_RES_STRING] = L"";
  814. WCHAR wszDate[MAX_RES_STRING] = L"";
  815. WCHAR wszTime[MAX_RES_STRING] = L"";
  816. WCHAR wszSize[MAX_RES_STRING] = L"";
  817. LPWSTR lpBuffer = NULL;
  818. BOOL bVersion = TRUE;
  819. /////////////////////////////////////////////////////////////////////
  820. // Get the information of a file i.e. filename, version, created time,
  821. // Last modified time, last access time and size in bytes
  822. //////////////////////////////////////////////////////////////////////
  823. //
  824. // Get the version of a file
  825. //
  826. // get the size of file version
  827. dwSize = GetFileVersionInfoSize ( pwszFileFullPath, &dw );
  828. // get the file version file
  829. if ( 0 == dwSize )
  830. {
  831. bVersion = FALSE;
  832. }
  833. //retrieves version information for the file.
  834. else if ( FALSE == GetFileVersionInfo ( pwszFileFullPath , dw, dwSize, (LPVOID) wszData ) )
  835. {
  836. SaveLastError();
  837. return EXIT_FAILURE;
  838. }
  839. //retrieves version information from the file version-information resource
  840. if ( ( bVersion == TRUE ) &&
  841. (FALSE == VerQueryValue(wszData, STRING_NAME1, (LPVOID*)&lpTranslate, &uSize)))
  842. {
  843. SaveLastError();
  844. return EXIT_FAILURE;
  845. }
  846. if ( bFlag == TRUE)
  847. {
  848. // display the heading before displaying the file information
  849. ShowMessage ( stdout, GetResString (IDS_INUSE_HEADING) );
  850. // display the column "Destination File:"
  851. ShowMessage ( stdout, _X(GetResString (IDS_EXT_FILE_NAME)) );
  852. }
  853. else
  854. {
  855. // display the column "Replacement File:"
  856. ShowMessage ( stdout, _X(GetResString (IDS_REP_FILE_NAME)) );
  857. }
  858. //
  859. // Display the file name with full path
  860. //
  861. // display name of the file
  862. ShowMessage ( stdout, _X(pwszFileFullPath) );
  863. ShowMessage ( stdout, L"\n" );
  864. if ( TRUE == bVersion )
  865. {
  866. // format the message for sub-block i.e. value to retrieve
  867. StringCchPrintf( wszSubBlock, SIZE_OF_ARRAY(wszSubBlock), STRING_NAME2, lpTranslate[0].wLanguage,
  868. lpTranslate[0].wCodePage);
  869. }
  870. // Retrieve file version for language and code page
  871. if ( ( bVersion == TRUE ) &&
  872. ( FALSE == VerQueryValue(wszData, wszSubBlock, (LPVOID *) &lpBuffer, &uSize) ) )
  873. {
  874. SaveLastError();
  875. return EXIT_FAILURE;
  876. }
  877. //
  878. //Display the version information of a file
  879. //
  880. // if version infomation is not available
  881. if ( FALSE == bVersion )
  882. {
  883. // copy the string as version is "Not available"
  884. StringCopy ( wszBuffer, GetResString ( IDS_VER_NA), SIZE_OF_ARRAY(wszBuffer) );
  885. // display the column name as "Version:"
  886. ShowMessage ( stdout, _X(GetResString (IDS_FILE_VER)) );
  887. //display the version information of file
  888. ShowMessage ( stdout, _X(wszBuffer) );
  889. ShowMessage ( stdout, L"\n" );
  890. }
  891. else
  892. {
  893. // display the column name as "Version:"
  894. ShowMessage ( stdout, _X(GetResString (IDS_FILE_VER)) );
  895. ShowMessage ( stdout, _X(lpBuffer) );
  896. ShowMessage ( stdout, L"\n" );
  897. }
  898. // Get File info
  899. //SECURITY_ATTRIBUTES SecurityAttributes;
  900. HANDLE HndFile;
  901. FILETIME filetime = {0,0};
  902. BY_HANDLE_FILE_INFORMATION FileInformation ;
  903. SYSTEMTIME systemtime = {0,0,0,0,0,0,0,0};
  904. BOOL bLocaleChanged = FALSE;
  905. LCID lcid;
  906. int iBuffSize = 0;
  907. // opens an existing file
  908. HndFile = CreateFile( pwszFileName , 0, FILE_SHARE_READ , NULL,
  909. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
  910. // check if CreateFile() is failed
  911. if ( INVALID_HANDLE_VALUE == HndFile )
  912. {
  913. SaveLastError();
  914. return EXIT_FAILURE;
  915. }
  916. // retrieve file information of a file
  917. if (FALSE == GetFileInformationByHandle( HndFile , &FileInformation ))
  918. {
  919. // release handle
  920. if (FALSE == CloseHandle (HndFile))
  921. {
  922. SaveLastError();
  923. return EXIT_FAILURE;
  924. }
  925. SaveLastError();
  926. return EXIT_FAILURE;
  927. }
  928. // release handle
  929. if (FALSE == CloseHandle (HndFile))
  930. {
  931. SaveLastError();
  932. return EXIT_FAILURE;
  933. }
  934. ///
  935. // Get the information of a file creation time
  936. ///
  937. // convert file time to local file time
  938. if ( FALSE == FileTimeToLocalFileTime ( &FileInformation.ftCreationTime, &filetime ) )
  939. {
  940. SaveLastError();
  941. return EXIT_FAILURE;
  942. }
  943. // To get create time, convert local file time to system time
  944. if ( FALSE == FileTimeToSystemTime ( &filetime, &systemtime ) )
  945. {
  946. SaveLastError();
  947. return EXIT_FAILURE;
  948. }
  949. // verify whether console supports the current locale fully or not
  950. lcid = GetSupportedUserLocale( &bLocaleChanged );
  951. if ( 0 == lcid )
  952. {
  953. SaveLastError();
  954. return EXIT_FAILURE;
  955. }
  956. //Retrieve the Date format for a current locale
  957. iBuffSize = GetDateFormat( lcid, 0, &systemtime,
  958. (( bLocaleChanged == TRUE ) ? L"MM/dd/yyyy" : NULL), wszDate, SIZE_OF_ARRAY( wszDate ) );
  959. //check if GetDateFormat() is failed
  960. if( 0 == iBuffSize )
  961. {
  962. SaveLastError();
  963. return EXIT_FAILURE;
  964. }
  965. // retrieve the time format for a current locale
  966. iBuffSize = GetTimeFormat( lcid, 0, &systemtime,
  967. (( bLocaleChanged == TRUE ) ? L"HH:mm:ss" : NULL), wszTime, SIZE_OF_ARRAY( wszTime ) );
  968. if( 0 == iBuffSize )
  969. {
  970. SaveLastError();
  971. return EXIT_FAILURE;
  972. }
  973. // format the message as ... "time, date"..
  974. StringConcat ( wszTime, COMMA_STR, SIZE_OF_ARRAY(wszTime) );
  975. StringConcat ( wszTime , wszDate, SIZE_OF_ARRAY(wszTime) );
  976. // display the column name as "Created Time:"
  977. ShowMessage ( stdout, _X(GetResString (IDS_FILE_CRT_TIME)) );
  978. ShowMessage ( stdout, _X(wszTime) );
  979. ShowMessage ( stdout, L"\n" );
  980. //
  981. // Get the information of Last modified time
  982. //
  983. // get the Last modified time for a file
  984. if ( FALSE == FileTimeToLocalFileTime ( &FileInformation.ftLastWriteTime, &filetime ) )
  985. {
  986. SaveLastError();
  987. return EXIT_FAILURE;
  988. }
  989. // get the last access time for a file
  990. if ( FALSE == FileTimeToSystemTime ( &filetime , &systemtime ) )
  991. {
  992. SaveLastError();
  993. return EXIT_FAILURE;
  994. }
  995. // get date format for a current locale
  996. iBuffSize = GetDateFormat( lcid, 0, &systemtime,
  997. (( bLocaleChanged == TRUE ) ? L"MM/dd/yyyy" : NULL), wszDate, SIZE_OF_ARRAY( wszDate ) );
  998. // check if GetDateFormat() is failed
  999. if( 0 == iBuffSize )
  1000. {
  1001. SaveLastError();
  1002. return EXIT_FAILURE;
  1003. }
  1004. // check if GetTimeFormat() is failed
  1005. iBuffSize = GetTimeFormat( lcid, 0,
  1006. &systemtime, (( bLocaleChanged == TRUE ) ? L"HH:mm:ss" : NULL),
  1007. wszTime, SIZE_OF_ARRAY( wszTime ) );
  1008. // check if GetTimeFormat() is failed
  1009. if( 0 == iBuffSize )
  1010. {
  1011. SaveLastError();
  1012. return EXIT_FAILURE;
  1013. }
  1014. //format the message as .."time, date"
  1015. StringConcat ( wszTime, COMMA_STR, SIZE_OF_ARRAY(wszTime) );
  1016. StringConcat ( wszTime , wszDate, SIZE_OF_ARRAY(wszTime) );
  1017. // display the column name as "Last Modified Time:"
  1018. ShowMessage ( stdout, _X(GetResString (IDS_FILE_MOD_TIME)) );
  1019. ShowMessage ( stdout, _X(wszTime) );
  1020. // display the creation time of a file
  1021. ShowMessage ( stdout, L"\n" );
  1022. ///
  1023. // Get the information of Last Access Time
  1024. ///
  1025. // convert file time to local file time
  1026. if ( FALSE == FileTimeToLocalFileTime ( &FileInformation.ftLastAccessTime, &filetime ) )
  1027. {
  1028. SaveLastError();
  1029. return EXIT_FAILURE;
  1030. }
  1031. // To get last access time, convert local file time to system time for a file
  1032. if ( FALSE == FileTimeToSystemTime ( &filetime , &systemtime ) )
  1033. {
  1034. SaveLastError();
  1035. return EXIT_FAILURE;
  1036. }
  1037. // get date format for a specified locale
  1038. iBuffSize = GetDateFormat( lcid, 0, &systemtime,
  1039. (( bLocaleChanged == TRUE ) ? L"MM/dd/yyyy" : NULL), wszDate, SIZE_OF_ARRAY( wszDate ) );
  1040. // check if GetDateFormat is failed
  1041. if( 0 == iBuffSize )
  1042. {
  1043. SaveLastError();
  1044. return EXIT_FAILURE;
  1045. }
  1046. // get time format for a current locale
  1047. iBuffSize = GetTimeFormat( lcid, 0,
  1048. &systemtime, (( bLocaleChanged == TRUE ) ? L"HH:mm:ss" : NULL),
  1049. wszTime, SIZE_OF_ARRAY( wszTime ) );
  1050. // check if GetTimeFormat() is failed
  1051. if( 0 == iBuffSize )
  1052. {
  1053. SaveLastError();
  1054. return EXIT_FAILURE;
  1055. }
  1056. // format message as .. "time, date"
  1057. StringConcat ( wszTime, COMMA_STR, SIZE_OF_ARRAY(wszTime) );
  1058. StringConcat ( wszTime , wszDate, SIZE_OF_ARRAY(wszTime) );
  1059. // display the column name as "Last Access Time:"
  1060. ShowMessage ( stdout, _X(GetResString (IDS_FILE_ACS_TIME)) );
  1061. ShowMessage ( stdout, _X(wszTime) );
  1062. // display the creation time of a file
  1063. ShowMessage ( stdout, L"\n" );
  1064. ///
  1065. // Get the size of a file in bytes
  1066. ///
  1067. // sub-local variables
  1068. NUMBERFMT numberfmt;
  1069. WCHAR szGrouping[MAX_RES_STRING] = NULL_STRING;
  1070. WCHAR szDecimalSep[MAX_RES_STRING] = NULL_STRING;
  1071. WCHAR szThousandSep[MAX_RES_STRING] = NULL_STRING;
  1072. WCHAR szTemp[MAX_RES_STRING] = NULL_STRING;
  1073. LPWSTR szTemp1 = NULL;
  1074. LPWSTR pszStoppedString = NULL;
  1075. DWORD dwGrouping = 3;
  1076. //make the fractional digits and leading zeros to nothing
  1077. numberfmt.NumDigits = 0;
  1078. numberfmt.LeadingZero = 0;
  1079. //get the decimal seperate character
  1080. if( 0 == GetLocaleInfo( lcid, LOCALE_SDECIMAL, szDecimalSep, MAX_RES_STRING ) )
  1081. {
  1082. StringCopy(szDecimalSep, L",", SIZE_OF_ARRAY(szDecimalSep));
  1083. }
  1084. numberfmt.lpDecimalSep = szDecimalSep;
  1085. // retrieve info about locale
  1086. if(FALSE == GetLocaleInfo( lcid, LOCALE_STHOUSAND, szThousandSep, MAX_RES_STRING ) )
  1087. {
  1088. StringCopy(szThousandSep, L"," , SIZE_OF_ARRAY(szThousandSep));
  1089. }
  1090. numberfmt.lpThousandSep = szThousandSep;
  1091. // retrieve info about locale
  1092. if( GetLocaleInfo( lcid, LOCALE_SGROUPING, szGrouping, MAX_RES_STRING ) )
  1093. {
  1094. // get the token till ';'
  1095. szTemp1 = wcstok( szGrouping, L";");
  1096. if ( NULL == szTemp1 )
  1097. {
  1098. SaveLastError();
  1099. return EXIT_FAILURE;
  1100. }
  1101. do
  1102. {
  1103. StringConcat( szTemp, szTemp1, SIZE_OF_ARRAY(szTemp) );
  1104. // get the token till ';'
  1105. szTemp1 = wcstok( NULL, L";" );
  1106. }while( szTemp1 != NULL && StringCompare( szTemp1, L"0", TRUE, 0) != 0);
  1107. // get the numeric value
  1108. dwGrouping = wcstol( szTemp, &pszStoppedString, 10);
  1109. if ( (errno == ERANGE) ||
  1110. ((pszStoppedString != NULL) && (StringLength (pszStoppedString, 0) != 0 )))
  1111. {
  1112. SaveLastError();
  1113. return EXIT_FAILURE;
  1114. }
  1115. }
  1116. else
  1117. {
  1118. dwGrouping = 33; //set the default grouping
  1119. }
  1120. numberfmt.Grouping = (UINT)dwGrouping ;
  1121. numberfmt.NegativeOrder = 2;
  1122. // get the file size
  1123. StringCchPrintf (wszSize, SIZE_OF_ARRAY(wszSize), L"%d", FileInformation.nFileSizeLow );
  1124. // get number format for a current locale
  1125. iBuffSize = GetNumberFormat( lcid, 0,
  1126. wszSize, &numberfmt, wszBuffer, SIZE_OF_ARRAY( wszBuffer ) );
  1127. // check if GetNumberFormat() is failed
  1128. if( 0 == iBuffSize )
  1129. {
  1130. SaveLastError();
  1131. return EXIT_FAILURE;
  1132. }
  1133. // display the column name as "Size:"
  1134. ShowMessage ( stdout, _X( GetResString (IDS_FILE_SIZE)) );
  1135. // display the actual size in bytes for a file
  1136. ShowMessage ( stdout, _X(wszBuffer) );
  1137. ShowMessage ( stdout, GetResString (IDS_STR_BYTES) );
  1138. // display the blank lines
  1139. ShowMessage ( stdout, L"\n\n" );
  1140. // return 0
  1141. return EXIT_SUCCESS;
  1142. }
  1143. DWORD
  1144. ConfirmInput ( VOID )
  1145. /*++
  1146. Routine Description:
  1147. This function validates the input given by user.
  1148. Arguments:
  1149. None
  1150. Return Value:
  1151. EXIT_FAILURE : On failure
  1152. EXIT_SUCCESS : On success
  1153. --*/
  1154. {
  1155. // sub-local variables
  1156. DWORD dwCharsRead = 0;
  1157. DWORD dwPrevConsoleMode = 0;
  1158. HANDLE hInputConsole = NULL;
  1159. BOOL bIndirectionInput = FALSE;
  1160. WCHAR ch = L'\0';
  1161. WCHAR chTmp = L'\0';
  1162. DWORD dwCharsWritten = 0;
  1163. WCHAR szBuffer[MAX_RES_STRING];
  1164. WCHAR szBackup[MAX_RES_STRING];
  1165. WCHAR szTmpBuf[MAX_RES_STRING];
  1166. DWORD dwIndex = 0 ;
  1167. BOOL bNoBreak = TRUE;
  1168. SecureZeroMemory ( szBuffer, SIZE_OF_ARRAY(szBuffer));
  1169. SecureZeroMemory ( szTmpBuf, SIZE_OF_ARRAY(szTmpBuf));
  1170. SecureZeroMemory ( szBackup, SIZE_OF_ARRAY(szBackup));
  1171. // Get the handle for the standard input
  1172. hInputConsole = GetStdHandle( STD_INPUT_HANDLE );
  1173. if ( hInputConsole == INVALID_HANDLE_VALUE )
  1174. {
  1175. SaveLastError();
  1176. // could not get the handle so return failure
  1177. return EXIT_FAILURE;
  1178. }
  1179. MessageBeep(MB_ICONEXCLAMATION);
  1180. // display the message .. Do you want to continue? ...
  1181. ShowMessage ( stdout, GetResString ( IDS_INPUT_DATA ) );
  1182. // Check for the input redirect
  1183. if( ( hInputConsole != (HANDLE)0x0000000F ) &&
  1184. ( hInputConsole != (HANDLE)0x00000003 ) &&
  1185. ( hInputConsole != INVALID_HANDLE_VALUE ) )
  1186. {
  1187. bIndirectionInput = TRUE;
  1188. }
  1189. // if there is no redirection
  1190. if ( bIndirectionInput == FALSE )
  1191. {
  1192. // Get the current input mode of the input buffer
  1193. if ( FALSE == GetConsoleMode( hInputConsole, &dwPrevConsoleMode ))
  1194. {
  1195. SaveLastError();
  1196. // could not set the mode, return failure
  1197. return EXIT_FAILURE;
  1198. }
  1199. // Set the mode such that the control keys are processed by the system
  1200. if ( FALSE == SetConsoleMode( hInputConsole, ENABLE_PROCESSED_INPUT ) )
  1201. {
  1202. SaveLastError();
  1203. // could not set the mode, return failure
  1204. return EXIT_FAILURE;
  1205. }
  1206. }
  1207. // redirect the data into the console
  1208. if ( bIndirectionInput == TRUE )
  1209. {
  1210. do {
  1211. //read the contents of file
  1212. if ( ReadFile(hInputConsole, &chTmp, 1, &dwCharsRead, NULL) == FALSE )
  1213. {
  1214. SaveLastError();
  1215. // could not get the handle so return failure
  1216. return EXIT_FAILURE;
  1217. }
  1218. // check if number of characters read were zero.. or
  1219. // any carriage return pressed..
  1220. if ( dwCharsRead == 0 || chTmp == CARRIAGE_RETURN )
  1221. {
  1222. bNoBreak = FALSE;
  1223. // exit from the loop
  1224. break;
  1225. }
  1226. // write the contents to the console
  1227. if ( FALSE == WriteFile ( GetStdHandle( STD_OUTPUT_HANDLE ), &chTmp, 1, &dwCharsRead, NULL ) )
  1228. {
  1229. SaveLastError();
  1230. // could not get the handle so return failure
  1231. return EXIT_FAILURE;
  1232. }
  1233. // copy the character
  1234. ch = chTmp;
  1235. StringCchPrintf ( szBackup, SIZE_OF_ARRAY(szBackup), L"%c" , ch );
  1236. // increment the index
  1237. dwIndex++;
  1238. } while (TRUE == bNoBreak);
  1239. }
  1240. else
  1241. {
  1242. do {
  1243. // Get the Character and loop accordingly.
  1244. if ( ReadConsole( hInputConsole, &chTmp, 1, &dwCharsRead, NULL ) == FALSE )
  1245. {
  1246. SaveLastError();
  1247. // Set the original console settings
  1248. if ( FALSE == SetConsoleMode( hInputConsole, dwPrevConsoleMode ) )
  1249. {
  1250. SaveLastError();
  1251. }
  1252. // return failure
  1253. return EXIT_FAILURE;
  1254. }
  1255. // check if number of chars read were zero..if so, continue...
  1256. if ( dwCharsRead == 0 )
  1257. {
  1258. continue;
  1259. }
  1260. // check if any carriage return pressed...
  1261. if ( chTmp == CARRIAGE_RETURN )
  1262. {
  1263. bNoBreak = FALSE;
  1264. // exit from the loop
  1265. break;
  1266. }
  1267. ch = chTmp;
  1268. if ( ch != BACK_SPACE )
  1269. {
  1270. StringCchPrintf ( szTmpBuf, SIZE_OF_ARRAY(szTmpBuf), L"%c" , ch );
  1271. StringConcat ( szBackup, szTmpBuf , SIZE_OF_ARRAY(szBackup));
  1272. }
  1273. // Check id back space is hit
  1274. if ( ch == BACK_SPACE )
  1275. {
  1276. if ( dwIndex != 0 )
  1277. {
  1278. //
  1279. // Remove a asterix from the console
  1280. // move the cursor one character back
  1281. StringCchPrintf( szBuffer, SIZE_OF_ARRAY(szBuffer), L"%c" , BACK_SPACE );
  1282. if ( FALSE == WriteConsole( GetStdHandle( STD_OUTPUT_HANDLE ), szBuffer, 1,
  1283. &dwCharsWritten, NULL ) )
  1284. {
  1285. SaveLastError();
  1286. // return failure
  1287. return EXIT_FAILURE;
  1288. }
  1289. // replace the existing character with space
  1290. StringCchPrintf( szBuffer, SIZE_OF_ARRAY(szBuffer), L"%c" , BLANK_CHAR );
  1291. if ( FALSE == WriteConsole( GetStdHandle( STD_OUTPUT_HANDLE ), szBuffer, 1,
  1292. &dwCharsWritten, NULL ))
  1293. {
  1294. SaveLastError();
  1295. // return failure
  1296. return EXIT_FAILURE;
  1297. }
  1298. // now set the cursor at back position
  1299. StringCchPrintf( szBuffer, SIZE_OF_ARRAY(szBuffer), L"%c" , BACK_SPACE );
  1300. if ( FALSE == WriteConsole( GetStdHandle( STD_OUTPUT_HANDLE ), szBuffer, 1,
  1301. &dwCharsWritten, NULL ))
  1302. {
  1303. SaveLastError();
  1304. // return failure
  1305. return EXIT_FAILURE;
  1306. }
  1307. szBackup [StringLength(szBackup, 0) - 1] = L'\0';
  1308. // decrement the index
  1309. dwIndex--;
  1310. }
  1311. // process the next character
  1312. continue;
  1313. }
  1314. // write the contents onto console
  1315. if ( FALSE == WriteFile ( GetStdHandle( STD_OUTPUT_HANDLE ), &ch, 1, &dwCharsRead, NULL ) )
  1316. {
  1317. SaveLastError();
  1318. // return failure
  1319. return EXIT_FAILURE;
  1320. }
  1321. // increment the index value
  1322. dwIndex++;
  1323. } while (TRUE == bNoBreak);
  1324. }
  1325. ShowMessage(stdout, _T("\n") );
  1326. //StringCchPrintf( szBuffer, SIZE_OF_ARRAY(szBuffer), L"%c" , ch );
  1327. // check if 'Y' or 'y' is pressed
  1328. if ( ( dwIndex == 1 ) &&
  1329. ( StringCompare ( szBackup, GetResString (IDS_UPPER_YES), TRUE, 0 ) == 0 ) )
  1330. {
  1331. return EXIT_SUCCESS;
  1332. }
  1333. // check if 'N' or 'n' is pressed
  1334. else if ( ( dwIndex == 1 ) &&
  1335. ( StringCompare ( szBackup, GetResString(IDS_UPPER_NO), TRUE, 0 ) == 0 ) )
  1336. {
  1337. // display a message as .. operation has been cancelled...
  1338. ShowMessage ( stdout, GetResString (IDS_OPERATION_CANCELLED ) );
  1339. // Already displayed the INFO message as above...There is no need to display any
  1340. // success message now.. thats why assigning EXIT_ON_CALCEL flag to g_dwRetVal
  1341. g_dwRetVal = EXIT_ON_CANCEL;
  1342. return EXIT_SUCCESS;
  1343. }
  1344. else
  1345. {
  1346. // display an error message as .. wrong input specified...
  1347. ShowMessage(stderr, GetResString( IDS_WRONG_INPUT ));
  1348. // Already displayed the ERROR message as above...There is no need to display any
  1349. // success message now.. thats why assigning EXIT_ON_ERROR flag to g_dwRetVal
  1350. g_dwRetVal = EXIT_ON_ERROR;
  1351. return EXIT_FAILURE;
  1352. }
  1353. }
  1354. BOOL
  1355. SetPrivilege(
  1356. IN LPWSTR szSystemName
  1357. )
  1358. /*++
  1359. Routine Description:
  1360. This function enables seSecurityPrivilege.
  1361. Arguments:
  1362. [in] szSystemName: SystemName
  1363. Return Value:
  1364. FALSE : On failure
  1365. TRUE : On success
  1366. --*/
  1367. {
  1368. HANDLE hToken = NULL;
  1369. TOKEN_PRIVILEGES tp;
  1370. LUID luid;
  1371. // Open current process token
  1372. if( FALSE == OpenProcessToken ( GetCurrentProcess(),
  1373. TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES ,
  1374. &hToken )){
  1375. //return WIN32 error code
  1376. SaveLastError() ;
  1377. return FALSE;
  1378. }
  1379. if ( !LookupPrivilegeValue(
  1380. szSystemName , // lookup privilege on system
  1381. SECURITY_PRIV_NAME, // privilege to lookup
  1382. &luid ) )
  1383. { // receives LUID of privilege
  1384. SaveLastError();
  1385. return FALSE;
  1386. }
  1387. tp.PrivilegeCount = 1;
  1388. tp.Privileges[0].Luid = luid;
  1389. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  1390. // Enable the SeSecurityPrivilege
  1391. AdjustTokenPrivileges(
  1392. hToken,
  1393. FALSE,
  1394. &tp,
  1395. sizeof(TOKEN_PRIVILEGES),
  1396. (PTOKEN_PRIVILEGES) NULL,
  1397. (PDWORD) NULL);
  1398. // Call GetLastError to determine whether the function succeeded.
  1399. if (GetLastError() != ERROR_SUCCESS) {
  1400. SaveLastError();
  1401. return FALSE;
  1402. }
  1403. return TRUE;
  1404. }