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.

1008 lines
31 KiB

  1. //
  2. // LOGFILE.C
  3. //
  4. #include "sigverif.h"
  5. //
  6. // We need to remember the previous logging state when we do toggling.
  7. //
  8. BOOL g_bPrevLoggingEnabled = FALSE;
  9. BOOL
  10. LogFile_OnInitDialog(
  11. HWND hwnd,
  12. HWND hwndFocus,
  13. LPARAM lParam
  14. )
  15. {
  16. TCHAR szBuffer[MAX_PATH];
  17. UNREFERENCED_PARAMETER(hwndFocus);
  18. UNREFERENCED_PARAMETER(lParam);
  19. if (g_App.hIcon) {
  20. SetWindowLongPtr(hwnd, GCLP_HICON, (LONG_PTR) g_App.hIcon);
  21. }
  22. g_App.hLogging = hwnd;
  23. g_bPrevLoggingEnabled = g_App.bLoggingEnabled;
  24. if (*g_App.szLogDir) {
  25. SetCurrentDirectory(g_App.szLogDir);
  26. } else {
  27. if (GetWindowsDirectory(szBuffer, cA(szBuffer))) {
  28. SetCurrentDirectory(szBuffer);
  29. }
  30. }
  31. SetDlgItemText(hwnd, IDC_LOGNAME, g_App.szLogFile);
  32. CheckDlgButton(hwnd, IDC_ENABLELOG, g_App.bLoggingEnabled ? BST_CHECKED : BST_UNCHECKED);
  33. EnableWindow(GetDlgItem(hwnd, IDC_VIEWLOG), g_App.bLoggingEnabled && EXIST(g_App.szLogFile));
  34. CheckRadioButton(hwnd, IDC_OVERWRITE, IDC_APPEND, g_App.bOverwrite ? IDC_OVERWRITE : IDC_APPEND);
  35. EnableWindow(GetDlgItem(hwnd, IDC_APPEND), g_App.bLoggingEnabled);
  36. EnableWindow(GetDlgItem(hwnd, IDC_OVERWRITE), g_App.bLoggingEnabled);
  37. EnableWindow(GetDlgItem(hwnd, IDC_LOGNAME), g_App.bLoggingEnabled);
  38. SetForegroundWindow(g_App.hDlg);
  39. SetForegroundWindow(hwnd);
  40. return TRUE;
  41. }
  42. void
  43. LogFile_UpdateDialog(
  44. HWND hwnd
  45. )
  46. {
  47. TCHAR szBuffer[MAX_PATH];
  48. if (GetDlgItemText(hwnd, IDC_LOGNAME, szBuffer, cA(szBuffer))) {
  49. EnableWindow(GetDlgItem(hwnd, IDC_VIEWLOG), g_App.bLoggingEnabled && EXIST(szBuffer));
  50. } else {
  51. EnableWindow(GetDlgItem(hwnd, IDC_VIEWLOG), FALSE);
  52. }
  53. EnableWindow(GetDlgItem(hwnd, IDC_APPEND), g_App.bLoggingEnabled);
  54. EnableWindow(GetDlgItem(hwnd, IDC_OVERWRITE), g_App.bLoggingEnabled);
  55. EnableWindow(GetDlgItem(hwnd, IDC_LOGNAME), g_App.bLoggingEnabled);
  56. }
  57. void
  58. LogFile_OnViewLog(
  59. HWND hwnd
  60. )
  61. {
  62. TCHAR szDirName[MAX_PATH];
  63. TCHAR szFileName[MAX_PATH];
  64. if (!GetWindowsDirectory(szDirName, cA(szDirName))) {
  65. szDirName[0] = TEXT('\0');
  66. }
  67. if (*g_App.szLogDir) {
  68. if (FAILED(StringCchCopy(szDirName, cA(szDirName), g_App.szLogDir))) {
  69. szDirName[0] = TEXT('\0');
  70. }
  71. } else {
  72. if (!GetWindowsDirectory(szDirName, cA(szDirName))) {
  73. szDirName[0] = TEXT('\0');
  74. }
  75. }
  76. if (!GetDlgItemText(hwnd, IDC_LOGNAME, szFileName, cA(szFileName))) {
  77. MyErrorBoxId(IDS_BADLOGNAME);
  78. return;
  79. }
  80. ShellExecute(hwnd, NULL, szFileName, NULL, szDirName, SW_SHOW);
  81. }
  82. BOOL
  83. LogFile_VerifyLogFile(
  84. HWND hwnd,
  85. LPTSTR lpFileName,
  86. ULONG FileNameCchSize,
  87. BOOL bNoisy
  88. )
  89. {
  90. TCHAR szFileName[MAX_PATH];
  91. HANDLE hFile;
  92. BOOL bRet;
  93. HWND hTemp;
  94. ZeroMemory(szFileName, sizeof(szFileName));
  95. bRet = GetDlgItemText(hwnd, IDC_LOGNAME, szFileName, cA(szFileName));
  96. if (bRet) {
  97. hFile = CreateFile( szFileName,
  98. GENERIC_READ | GENERIC_WRITE,
  99. FILE_SHARE_READ,
  100. NULL,
  101. OPEN_EXISTING,
  102. FILE_ATTRIBUTE_NORMAL,
  103. NULL);
  104. if (hFile != INVALID_HANDLE_VALUE) {
  105. CloseHandle(hFile);
  106. } else {
  107. hFile = CreateFile( szFileName,
  108. GENERIC_READ | GENERIC_WRITE,
  109. FILE_SHARE_READ,
  110. NULL,
  111. CREATE_NEW,
  112. FILE_ATTRIBUTE_NORMAL,
  113. NULL);
  114. if (hFile != INVALID_HANDLE_VALUE) {
  115. CloseHandle(hFile);
  116. DeleteFile(szFileName);
  117. } else {
  118. //
  119. // If we couldn't open an existing file and we couldn't create a new one, then we fail.
  120. //
  121. bRet = FALSE;
  122. }
  123. }
  124. }
  125. if (!bRet && bNoisy) {
  126. //
  127. // Since we don't want to lose focus, we are going to temporarily change g_App.hDlg. JasKey, I apologize.
  128. //
  129. hTemp = g_App.hDlg;
  130. g_App.hDlg = hwnd;
  131. MyErrorBoxId(IDS_BADLOGNAME);
  132. g_App.hDlg = hTemp;
  133. }
  134. //
  135. // If everything worked and the user wants the file name, copy it into lpFileName
  136. //
  137. if (bRet && lpFileName && *szFileName) {
  138. if (FAILED(StringCchCopy(lpFileName, FileNameCchSize, szFileName))) {
  139. //
  140. // If we failed to copy the entire string into the callers buffer,
  141. // so set the callers buffer to the empty string and set the return
  142. // value to FALSE.
  143. //
  144. if (FileNameCchSize >= 1) {
  145. lpFileName[0] = TEXT('\0');
  146. }
  147. bRet = FALSE;
  148. }
  149. }
  150. return bRet;
  151. }
  152. BOOL
  153. LogFile_OnOK(
  154. HWND hwnd
  155. )
  156. {
  157. HKEY hKey;
  158. LONG lRes;
  159. DWORD dwDisp, dwType, dwFlags, cbData;
  160. if (!LogFile_VerifyLogFile(hwnd, g_App.szLogFile, cA(g_App.szLogFile), FALSE)) {
  161. //
  162. // The log file could not be created.
  163. //
  164. return FALSE;
  165. }
  166. g_App.bOverwrite = IsDlgButtonChecked(hwnd, IDC_OVERWRITE);
  167. //
  168. // Look in the registry for any settings from the last SigVerif session
  169. //
  170. lRes = RegCreateKeyEx( HKEY_CURRENT_USER,
  171. SIGVERIF_KEY,
  172. 0,
  173. NULL,
  174. 0,
  175. KEY_ALL_ACCESS,
  176. NULL,
  177. &hKey,
  178. &dwDisp);
  179. if (lRes == ERROR_SUCCESS) {
  180. cbData = sizeof(DWORD);
  181. dwFlags = 0;
  182. if (g_App.bLoggingEnabled) {
  183. dwFlags = 0x1;
  184. }
  185. if (g_App.bOverwrite) {
  186. dwFlags |= 0x2;
  187. }
  188. dwType = REG_DWORD;
  189. lRes = RegSetValueEx( hKey,
  190. SIGVERIF_FLAGS,
  191. 0,
  192. dwType,
  193. (LPBYTE) &dwFlags,
  194. cbData);
  195. dwType = REG_SZ;
  196. cbData = MAX_PATH;
  197. lRes = RegSetValueEx( hKey,
  198. SIGVERIF_LOGNAME,
  199. 0,
  200. dwType,
  201. (LPBYTE) g_App.szLogFile,
  202. cbData);
  203. RegCloseKey(hKey);
  204. }
  205. return TRUE;
  206. }
  207. void
  208. LogFile_OnCommand(
  209. HWND hwnd,
  210. int id,
  211. HWND hwndCtl,
  212. UINT codeNotify
  213. )
  214. {
  215. UNREFERENCED_PARAMETER(hwndCtl);
  216. UNREFERENCED_PARAMETER(codeNotify);
  217. switch (id) {
  218. case IDC_VIEWLOG:
  219. LogFile_OnViewLog(hwnd);
  220. break;
  221. case IDC_ENABLELOG:
  222. g_App.bLoggingEnabled = !g_App.bLoggingEnabled;
  223. //
  224. // Fall through to update...
  225. //
  226. default:
  227. LogFile_UpdateDialog(hwnd);
  228. }
  229. }
  230. //
  231. // This function handles any notification messages for the Search page.
  232. //
  233. LRESULT
  234. LogFile_NotifyHandler(
  235. HWND hwnd,
  236. UINT uMsg,
  237. WPARAM wParam,
  238. LPARAM lParam
  239. )
  240. {
  241. NMHDR *lpnmhdr = (NMHDR *) lParam;
  242. LRESULT lResult;
  243. BOOL bRet;
  244. UNREFERENCED_PARAMETER(uMsg);
  245. UNREFERENCED_PARAMETER(wParam);
  246. switch (lpnmhdr->code) {
  247. case PSN_APPLY:
  248. if (LogFile_OnOK(hwnd)) {
  249. lResult = PSNRET_NOERROR;
  250. } else {
  251. lResult = PSNRET_INVALID_NOCHANGEPAGE;
  252. }
  253. SetWindowLongPtr(hwnd,
  254. DWLP_MSGRESULT,
  255. (LONG_PTR) lResult);
  256. return lResult;
  257. case PSN_KILLACTIVE:
  258. bRet = !LogFile_VerifyLogFile(hwnd, NULL, 0, TRUE);
  259. if (bRet) {
  260. SetForegroundWindow(g_App.hLogging);
  261. SetFocus(GetDlgItem(g_App.hLogging, IDC_LOGNAME));
  262. }
  263. SetWindowLongPtr(hwnd,
  264. DWLP_MSGRESULT,
  265. (LONG_PTR) bRet);
  266. return bRet;
  267. }
  268. return 0;
  269. }
  270. INT_PTR
  271. CALLBACK LogFile_DlgProc(
  272. HWND hwnd,
  273. UINT uMsg,
  274. WPARAM wParam,
  275. LPARAM lParam
  276. )
  277. {
  278. BOOL fProcessed = TRUE;
  279. switch (uMsg) {
  280. HANDLE_MSG(hwnd, WM_INITDIALOG, LogFile_OnInitDialog);
  281. HANDLE_MSG(hwnd, WM_COMMAND, LogFile_OnCommand);
  282. case WM_NOTIFY:
  283. return LogFile_NotifyHandler(hwnd, uMsg, wParam, lParam);
  284. case WM_HELP:
  285. SigVerif_Help(hwnd, uMsg, wParam, lParam, FALSE);
  286. break;
  287. case WM_CONTEXTMENU:
  288. SigVerif_Help(hwnd, uMsg, wParam, lParam, TRUE);
  289. break;
  290. default: fProcessed = FALSE;
  291. }
  292. return fProcessed;
  293. }
  294. BOOL
  295. PrintUnscannedFileListItems(
  296. HANDLE hFile
  297. )
  298. {
  299. DWORD Err = ERROR_SUCCESS;
  300. LPFILENODE lpFileNode;
  301. TCHAR szDirectory[MAX_PATH];
  302. TCHAR szBuffer[MAX_PATH * 2];
  303. TCHAR szBuffer2[MAX_PATH];
  304. DWORD dwBytesWritten;
  305. HRESULT hr;
  306. *szDirectory = 0;
  307. for (lpFileNode = g_App.lpFileList;lpFileNode;lpFileNode = lpFileNode->next) {
  308. //
  309. // Make sure we only log files that have NOT been scanned.
  310. //
  311. if (!lpFileNode->bScanned) {
  312. //
  313. // Write out the directory name
  314. //
  315. if (lstrcmp(szDirectory, lpFileNode->lpDirName)) {
  316. hr = StringCchCopy(szDirectory, cA(szDirectory), lpFileNode->lpDirName);
  317. if (SUCCEEDED(hr) || (hr == STRSAFE_E_INSUFFICIENT_BUFFER)) {
  318. MyLoadString(szBuffer2, cA(szBuffer2), IDS_DIR);
  319. hr = StringCchPrintf(szBuffer, cA(szBuffer), szBuffer2, szDirectory);
  320. if (SUCCEEDED(hr) || (hr == STRSAFE_E_INSUFFICIENT_BUFFER)) {
  321. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  322. Err = GetLastError();
  323. }
  324. }
  325. }
  326. }
  327. MyLoadString(szBuffer2, cA(szBuffer2), IDS_STRING);
  328. hr = StringCchPrintf(szBuffer, cA(szBuffer), szBuffer2, lpFileNode->lpFileName);
  329. if (SUCCEEDED(hr) || (hr == STRSAFE_E_INSUFFICIENT_BUFFER)) {
  330. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  331. Err = GetLastError();
  332. }
  333. }
  334. //
  335. // Print out the reason that the file was not scanned.
  336. //
  337. if (lpFileNode->LastError != ERROR_SUCCESS) {
  338. //
  339. // We will special case the error ERROR_FILE_NOT_FOUND and display
  340. // the text "The file is not installed." in the log file instead of
  341. // the default ERROR_FILE_NOT_FOUND text "The system cannot find the
  342. // file specified."
  343. //
  344. if (lpFileNode->LastError == ERROR_FILE_NOT_FOUND) {
  345. MyLoadString(szBuffer, cA(szBuffer), IDS_FILENOTINSTALLED);
  346. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  347. Err = GetLastError();
  348. }
  349. } else {
  350. LPVOID lpLastError = NULL;
  351. if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  352. FORMAT_MESSAGE_FROM_SYSTEM |
  353. FORMAT_MESSAGE_IGNORE_INSERTS,
  354. NULL,
  355. lpFileNode->LastError,
  356. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  357. (LPTSTR)&lpLastError,
  358. 0,
  359. NULL) != 0) {
  360. if (lpLastError) {
  361. if (!WriteFile(hFile, (LPTSTR)lpLastError, lstrlen((LPTSTR)lpLastError) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  362. Err = GetLastError();
  363. }
  364. LocalFree(lpLastError);
  365. }
  366. }
  367. }
  368. }
  369. }
  370. }
  371. MyLoadString(szBuffer, cA(szBuffer), IDS_LINEFEED);
  372. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  373. Err = GetLastError();
  374. }
  375. SetLastError(Err);
  376. return (Err == ERROR_SUCCESS);
  377. }
  378. BOOL
  379. PrintFileListItems(
  380. HANDLE hFile
  381. )
  382. {
  383. DWORD Err = ERROR_SUCCESS;
  384. LPFILENODE lpFileNode;
  385. TCHAR szDirectory[MAX_PATH];
  386. TCHAR szBuffer[MAX_PATH * 2];
  387. TCHAR szBuffer2[MAX_PATH];
  388. TCHAR szBuffer3[MAX_PATH];
  389. DWORD dwBytesWritten;
  390. LPTSTR lpString;
  391. int iRet;
  392. BOOL bMirroredApp;
  393. HRESULT hr;
  394. bMirroredApp = (GetWindowLong(g_App.hDlg, GWL_EXSTYLE) & WS_EX_LAYOUTRTL);
  395. *szDirectory = 0;
  396. for (lpFileNode = g_App.lpFileList;lpFileNode;lpFileNode = lpFileNode->next) {
  397. //
  398. // Make sure we only log files that have actually been scanned.
  399. //
  400. if (lpFileNode->bScanned) {
  401. if (lstrcmp(szDirectory, lpFileNode->lpDirName)) {
  402. hr = StringCchCopy(szDirectory, cA(szDirectory), lpFileNode->lpDirName);
  403. if (SUCCEEDED(hr) || (hr == STRSAFE_E_INSUFFICIENT_BUFFER)) {
  404. MyLoadString(szBuffer2, cA(szBuffer2), IDS_DIR);
  405. hr = StringCchPrintf(szBuffer, cA(szBuffer), szBuffer2, szDirectory);
  406. if (SUCCEEDED(hr) || (hr == STRSAFE_E_INSUFFICIENT_BUFFER)) {
  407. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  408. Err = GetLastError();
  409. }
  410. }
  411. }
  412. }
  413. MyLoadString(szBuffer2, cA(szBuffer2), IDS_STRING);
  414. hr = StringCchPrintf(szBuffer, cA(szBuffer), szBuffer2, lpFileNode->lpFileName);
  415. if (SUCCEEDED(hr) || (hr == STRSAFE_E_INSUFFICIENT_BUFFER)) {
  416. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  417. Err = GetLastError();
  418. }
  419. }
  420. MyLoadString(szBuffer, cA(szBuffer), IDS_SPACES);
  421. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  422. Err = GetLastError();
  423. }
  424. //
  425. // Get the date format, so we are localizable...
  426. //
  427. MyLoadString(szBuffer2, cA(szBuffer2), IDS_UNKNOWN);
  428. iRet = GetDateFormat(LOCALE_SYSTEM_DEFAULT,
  429. bMirroredApp ?
  430. DATE_RTLREADING | DATE_SHORTDATE :
  431. DATE_SHORTDATE,
  432. &lpFileNode->LastModified,
  433. NULL,
  434. NULL,
  435. 0);
  436. if (iRet) {
  437. lpString = MALLOC((iRet + 1) * sizeof(TCHAR));
  438. if (lpString) {
  439. iRet = GetDateFormat(LOCALE_SYSTEM_DEFAULT,
  440. bMirroredApp ?
  441. DATE_RTLREADING | DATE_SHORTDATE :
  442. DATE_SHORTDATE,
  443. &lpFileNode->LastModified,
  444. NULL,
  445. lpString,
  446. iRet);
  447. if (iRet) {
  448. hr = StringCchCopy(szBuffer2, cA(szBuffer2), lpString);
  449. if (FAILED(hr) && (hr != STRSAFE_E_INSUFFICIENT_BUFFER)) {
  450. //
  451. // If we failed to copy the date into our buffer for
  452. // some reason other than insufficient buffer space,
  453. // then set the date to the empty string.
  454. //
  455. szBuffer2[0] = TEXT('\0');
  456. }
  457. }
  458. FREE(lpString);
  459. }
  460. }
  461. MyLoadString(szBuffer3, cA(szBuffer3), IDS_STRING2);
  462. hr = StringCchPrintf(szBuffer, cA(szBuffer), szBuffer3, szBuffer2);
  463. if (SUCCEEDED(hr) || (hr == STRSAFE_E_INSUFFICIENT_BUFFER)) {
  464. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  465. Err = GetLastError();
  466. }
  467. }
  468. MyLoadString(szBuffer, cA(szBuffer), IDS_SPACES);
  469. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  470. Err = GetLastError();
  471. }
  472. szBuffer3[0] = TEXT('\0');
  473. if (lpFileNode->lpVersion && *lpFileNode->lpVersion) {
  474. hr = StringCchCopy(szBuffer3, cA(szBuffer3), lpFileNode->lpVersion);
  475. if (FAILED(hr) && (hr != STRSAFE_E_INSUFFICIENT_BUFFER)) {
  476. szBuffer3[0] = TEXT('\0');
  477. }
  478. }
  479. if (szBuffer3[0] == TEXT('\0')) {
  480. //
  481. // We were unable to get the version of the file, or the
  482. // string copy routine failed for some reason, so just show
  483. // No version.
  484. //
  485. MyLoadString(szBuffer3, cA(szBuffer3), IDS_NOVERSION);
  486. }
  487. MyLoadString(szBuffer2, cA(szBuffer2), IDS_STRING);
  488. hr = StringCchPrintf(szBuffer, cA(szBuffer), szBuffer2, szBuffer3);
  489. if (SUCCEEDED(hr) || (hr == STRSAFE_E_INSUFFICIENT_BUFFER)) {
  490. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  491. Err = GetLastError();
  492. }
  493. }
  494. MyLoadString(szBuffer2, cA(szBuffer2), IDS_STRING);
  495. MyLoadString(szBuffer3, cA(szBuffer3), lpFileNode->bSigned ? IDS_SIGNED : IDS_NOTSIGNED);
  496. hr = StringCchPrintf(szBuffer, cA(szBuffer), szBuffer2, szBuffer3);
  497. if (SUCCEEDED(hr) || (hr == STRSAFE_E_INSUFFICIENT_BUFFER)) {
  498. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  499. Err = GetLastError();
  500. }
  501. }
  502. szBuffer3[0] = TEXT('\0');
  503. if (lpFileNode->lpCatalog) {
  504. hr = StringCchCopy(szBuffer3, cA(szBuffer3), lpFileNode->lpCatalog);
  505. if (FAILED(hr) && (hr != STRSAFE_E_INSUFFICIENT_BUFFER)) {
  506. szBuffer3[0] = TEXT('\0');
  507. }
  508. }
  509. if (szBuffer3[0] == TEXT('\0')) {
  510. //
  511. // We were unable to get the version of the file, or the
  512. // string copy routine failed for some reason, so just show
  513. // NA.
  514. //
  515. MyLoadString(szBuffer3, cA(szBuffer3), IDS_NA);
  516. }
  517. MyLoadString(szBuffer2, cA(szBuffer2), IDS_STRING);
  518. hr = StringCchPrintf(szBuffer, cA(szBuffer), szBuffer2, szBuffer3);
  519. if (SUCCEEDED(hr) || (hr == STRSAFE_E_INSUFFICIENT_BUFFER)) {
  520. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  521. Err = GetLastError();
  522. }
  523. }
  524. if (lpFileNode->lpSignedBy) {
  525. if (!WriteFile(hFile, lpFileNode->lpSignedBy, lstrlen(lpFileNode->lpSignedBy) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  526. Err = GetLastError();
  527. }
  528. }
  529. MyLoadString(szBuffer, cA(szBuffer), IDS_LINEFEED);
  530. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  531. Err = GetLastError();
  532. }
  533. }
  534. }
  535. SetLastError(Err);
  536. return (Err == GetLastError());
  537. }
  538. BOOL
  539. PrintFileList(void)
  540. {
  541. DWORD Err = ERROR_SUCCESS;
  542. HANDLE hFile;
  543. DWORD dwBytesWritten;
  544. TCHAR szBuffer[MAX_PATH*2];
  545. TCHAR szBuffer2[MAX_PATH];
  546. TCHAR szBuffer3[MAX_PATH];
  547. LPTSTR lpString = NULL;
  548. OSVERSIONINFO osinfo;
  549. SYSTEM_INFO sysinfo;
  550. int iRet;
  551. BOOL bMirroredApp;
  552. HRESULT hr;
  553. bMirroredApp = (GetWindowLong(g_App.hDlg, GWL_EXSTYLE) & WS_EX_LAYOUTRTL);
  554. //
  555. // Bail if logging is disabled or there's no file list
  556. //
  557. if (!g_App.bLoggingEnabled || !g_App.lpFileList) {
  558. SetLastError(ERROR_SUCCESS);
  559. return FALSE;
  560. }
  561. if (*g_App.szLogDir) {
  562. SetCurrentDirectory(g_App.szLogDir);
  563. } else {
  564. if (GetWindowsDirectory(szBuffer, cA(szBuffer))) {
  565. SetCurrentDirectory(szBuffer);
  566. }
  567. }
  568. hFile = CreateFile( g_App.szLogFile,
  569. GENERIC_READ | GENERIC_WRITE,
  570. FILE_SHARE_READ,
  571. NULL,
  572. OPEN_ALWAYS,
  573. FILE_ATTRIBUTE_NORMAL,
  574. NULL);
  575. if (hFile == INVALID_HANDLE_VALUE) {
  576. Err = GetLastError();
  577. MyErrorBoxId(IDS_CANTOPENLOGFILE);
  578. return FALSE;
  579. }
  580. //
  581. // If the overwrite flag is set, truncate the file.
  582. //
  583. if (g_App.bOverwrite) {
  584. SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
  585. SetEndOfFile(hFile);
  586. } else SetFilePointer(hFile, 0, NULL, FILE_END);
  587. #ifdef UNICODE
  588. //
  589. // If we are using UNICODE, then write the 0xFF and 0xFE bytes at the beginning of the file.
  590. //
  591. if (g_App.bOverwrite || (GetFileSize(hFile, NULL) == 0)) {
  592. szBuffer[0] = 0xFEFF;
  593. if (!WriteFile(hFile, szBuffer, sizeof(TCHAR), &dwBytesWritten, NULL)) {
  594. Err = GetLastError();
  595. }
  596. }
  597. #endif
  598. //
  599. // Write the header to the logfile.
  600. //
  601. MyLoadString(szBuffer, cA(szBuffer), IDS_LOGHEADER1);
  602. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  603. Err = GetLastError();
  604. }
  605. //
  606. // Get the date format, so we are localizable...
  607. //
  608. MyLoadString(szBuffer2, cA(szBuffer2), IDS_UNKNOWN);
  609. iRet = GetDateFormat(LOCALE_SYSTEM_DEFAULT,
  610. bMirroredApp ?
  611. DATE_RTLREADING | DATE_SHORTDATE :
  612. DATE_SHORTDATE,
  613. NULL,
  614. NULL,
  615. NULL,
  616. 0
  617. );
  618. if (iRet) {
  619. lpString = MALLOC((iRet + 1) * sizeof(TCHAR));
  620. if (lpString) {
  621. iRet = GetDateFormat(LOCALE_SYSTEM_DEFAULT,
  622. bMirroredApp ?
  623. DATE_RTLREADING | DATE_SHORTDATE :
  624. DATE_SHORTDATE,
  625. NULL,
  626. NULL,
  627. lpString,
  628. iRet
  629. );
  630. if (iRet) {
  631. hr = StringCchCopy(szBuffer2, cA(szBuffer2), lpString);
  632. if (FAILED(hr) && (hr != STRSAFE_E_INSUFFICIENT_BUFFER)) {
  633. //
  634. // If we failed to copy the date into our buffer for
  635. // some reason other than insufficient buffer space,
  636. // then set the date to the empty string.
  637. //
  638. szBuffer2[0] = TEXT('\0');
  639. }
  640. }
  641. FREE(lpString);
  642. }
  643. }
  644. //
  645. // Get the time format, so we are localizable...
  646. //
  647. iRet = GetTimeFormat(LOCALE_SYSTEM_DEFAULT,TIME_NOSECONDS,NULL,NULL,NULL,0);
  648. if (iRet) {
  649. lpString = MALLOC((iRet + 1) * sizeof(TCHAR));
  650. if (lpString) {
  651. iRet = GetTimeFormat(LOCALE_SYSTEM_DEFAULT,TIME_NOSECONDS,NULL,NULL,lpString,iRet);
  652. }
  653. }
  654. MyLoadString(szBuffer3, cA(szBuffer3), IDS_LOGHEADER2);
  655. if (lpString) {
  656. hr = StringCchPrintf(szBuffer, cA(szBuffer), szBuffer3, szBuffer2, lpString);
  657. FREE(lpString);
  658. } else {
  659. hr = StringCchPrintf(szBuffer, cA(szBuffer), szBuffer3, szBuffer2, szBuffer2);
  660. }
  661. if (SUCCEEDED(hr) || (hr == STRSAFE_E_INSUFFICIENT_BUFFER)) {
  662. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  663. Err = GetLastError();
  664. }
  665. }
  666. //
  667. // Get the OS Platform string for the log file.
  668. //
  669. MyLoadString(szBuffer, cA(szBuffer), IDS_OSPLATFORM);
  670. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  671. Err = GetLastError();
  672. }
  673. ZeroMemory(&osinfo, sizeof(OSVERSIONINFO));
  674. osinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  675. GetVersionEx(&osinfo);
  676. switch (osinfo.dwPlatformId) {
  677. case VER_PLATFORM_WIN32_NT:
  678. MyLoadString(szBuffer, cA(szBuffer), IDS_WINNT);
  679. break;
  680. default:
  681. MyLoadString(szBuffer, cA(szBuffer), IDS_UNKNOWN);
  682. break;
  683. }
  684. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  685. Err = GetLastError();
  686. }
  687. //
  688. // If this is NT, then get the processor architecture and log it
  689. //
  690. if (osinfo.dwPlatformId == VER_PLATFORM_WIN32_NT) {
  691. ZeroMemory(&sysinfo, sizeof(SYSTEM_INFO));
  692. GetSystemInfo(&sysinfo);
  693. //
  694. // Initialize szBuffer to zeroes in case of an unknown architecture
  695. //
  696. ZeroMemory(szBuffer, sizeof(szBuffer));
  697. switch (sysinfo.wProcessorArchitecture) {
  698. case PROCESSOR_ARCHITECTURE_INTEL:
  699. MyLoadString(szBuffer, cA(szBuffer), IDS_X86);
  700. break;
  701. case PROCESSOR_ARCHITECTURE_IA64:
  702. MyLoadString(szBuffer, cA(szBuffer), IDS_IA64);
  703. break;
  704. }
  705. if (*szBuffer) {
  706. //
  707. // Now write the processor type to the file
  708. //
  709. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  710. Err = GetLastError();
  711. }
  712. }
  713. }
  714. //
  715. // Get the OS Version, Build, and CSD information and log it.
  716. //
  717. MyLoadString(szBuffer2, cA(szBuffer2), IDS_OSVERSION);
  718. hr = StringCchPrintf(szBuffer, cA(szBuffer), szBuffer2, osinfo.dwMajorVersion, osinfo.dwMinorVersion, (osinfo.dwBuildNumber & 0xFFFF), osinfo.szCSDVersion);
  719. if (SUCCEEDED(hr) || (hr == STRSAFE_E_INSUFFICIENT_BUFFER)) {
  720. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  721. Err = GetLastError();
  722. }
  723. }
  724. //
  725. // Print out the total/signed/unsigned results right before the file list
  726. //
  727. MyLoadString(szBuffer2, cA(szBuffer2), IDS_TOTALS);
  728. hr = StringCchPrintf(szBuffer,
  729. cA(szBuffer),
  730. szBuffer2,
  731. g_App.dwFiles,
  732. g_App.dwSigned,
  733. g_App.dwUnsigned,
  734. g_App.dwFiles - g_App.dwSigned - g_App.dwUnsigned);
  735. if (SUCCEEDED(hr) || (hr == STRSAFE_E_INSUFFICIENT_BUFFER)) {
  736. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  737. Err = GetLastError();
  738. }
  739. }
  740. //
  741. // If we are doing a user-defined search, then log the parameters.
  742. //
  743. if (g_App.bUserScan) {
  744. //
  745. // Write the user-specified directory
  746. //
  747. MyLoadString(szBuffer2, cA(szBuffer2), IDS_LOGHEADER3);
  748. hr = StringCchPrintf(szBuffer, cA(szBuffer), szBuffer2, g_App.szScanPattern);
  749. if (SUCCEEDED(hr) || (hr == STRSAFE_E_INSUFFICIENT_BUFFER)) {
  750. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  751. Err = GetLastError();
  752. }
  753. }
  754. //
  755. // Write the user-specified search pattern
  756. //
  757. MyLoadString(szBuffer2, cA(szBuffer2), IDS_LOGHEADER4);
  758. hr = StringCchPrintf(szBuffer, cA(szBuffer), szBuffer2, g_App.szScanPath);
  759. if (SUCCEEDED(hr) || (hr == STRSAFE_E_INSUFFICIENT_BUFFER)) {
  760. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  761. Err = GetLastError();
  762. }
  763. }
  764. }
  765. //
  766. // Write the column headers to the log file
  767. //
  768. MyLoadString(szBuffer, cA(szBuffer), IDS_LOGHEADER5);
  769. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  770. Err = GetLastError();
  771. }
  772. MyLoadString(szBuffer, cA(szBuffer), IDS_LOGHEADER6);
  773. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  774. Err = GetLastError();
  775. }
  776. if (!PrintFileListItems(hFile)) {
  777. Err = GetLastError();
  778. }
  779. //
  780. // Write the unscanned file headers to the log file
  781. //
  782. if (g_App.dwFiles > (g_App.dwSigned + g_App.dwUnsigned)) {
  783. MyLoadString(szBuffer, cA(szBuffer), IDS_LOGHEADER7);
  784. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  785. Err = GetLastError();
  786. }
  787. MyLoadString(szBuffer, cA(szBuffer), IDS_LOGHEADER8);
  788. if (!WriteFile(hFile, szBuffer, lstrlen(szBuffer) * sizeof(TCHAR), &dwBytesWritten, NULL)) {
  789. Err = GetLastError();
  790. }
  791. if (!PrintUnscannedFileListItems(hFile)) {
  792. Err = GetLastError();
  793. }
  794. }
  795. CloseHandle(hFile);
  796. SetLastError(Err);
  797. return (Err == ERROR_SUCCESS);
  798. }