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.

344 lines
10 KiB

  1. /******************************************************************************
  2. *
  3. * Copyright (c) 2000 Microsoft Corporation
  4. *
  5. * Module Name:
  6. * FileInfo.cxx
  7. *
  8. * Abstract:
  9. * Tool for getting file information
  10. *
  11. * Revision History:
  12. *
  13. * Weiyou Cui (weiyouc) 02-May-2001
  14. * - Created
  15. *
  16. *****************************************************************************/
  17. #include "srheader.hxx"
  18. //+---------------------------------------------------------------------------
  19. //
  20. // Function prototypes
  21. //
  22. //----------------------------------------------------------------------------
  23. HRESULT InfoPerFile(LPTSTR ptszLogFile, LPTSTR ptszFileName);
  24. //+---------------------------------------------------------------------------
  25. //
  26. // Some global structures and variables
  27. //
  28. //----------------------------------------------------------------------------
  29. struct LANGANDCODEPAGE {
  30. WORD wLanguage;
  31. WORD wCodePage;
  32. } *lpTranslate;
  33. LPCTSTR g_tszFileVersionList[] =
  34. {
  35. TEXT("\\system32\\drivers\\sr.sys"),
  36. TEXT("\\system32\\srclient.dll"),
  37. TEXT("\\system32\\srsvc.dll"),
  38. TEXT("\\system32\\srrstr.dll"),
  39. TEXT("\\system32\\restore\\filelist.xml"),
  40. TEXT("\\system32\\restore\\rstrui.exe"),
  41. TEXT("\\system32\\restore\\srframe.mmf"),
  42. TEXT("\\system32\\restore\\sr.mof"),
  43. };
  44. LPCTSTR g_tszVersionResource[] =
  45. {
  46. TEXT("Comments"),
  47. TEXT("CompanyName"),
  48. TEXT("FileDescription"),
  49. TEXT("FileVersion"),
  50. TEXT("InternalName"),
  51. TEXT("LegalCopyright"),
  52. TEXT("LegalTrademarks"),
  53. TEXT("OriginalFilename"),
  54. TEXT("ProductName"),
  55. TEXT("ProductVersion"),
  56. TEXT("PrivateBuild"),
  57. TEXT("SpecialBuild"),
  58. };
  59. extern LPCTSTR g_tszMonth[];
  60. extern LPCTSTR g_tszDay[];
  61. //+---------------------------------------------------------------------------
  62. //
  63. // Function: SRGetFileInfo
  64. //
  65. // Synopsis: This is the wrapper function for InfoPerFile, where I will assemble
  66. // the file path for each of the relevant files that we need to get
  67. // file statistics.
  68. //
  69. // Arguments: [ptszLogFile] -- log file name
  70. //
  71. // Returns: HRESULT
  72. //
  73. // History: 9/21/00 SHeffner Created
  74. //
  75. // 03-May-2001 Weiyouc ReWritten
  76. //
  77. //----------------------------------------------------------------------------
  78. HRESULT GetSRFileInfo(LPTSTR ptszLogFile)
  79. {
  80. HRESULT hr = S_OK;
  81. int iCount = 0;
  82. TCHAR tszFileName[MAX_PATH];
  83. DH_VDATEPTRIN(ptszLogFile, TCHAR);
  84. for (iCount = 0; iCount < ARRAYSIZE(g_tszFileVersionList); iCount++)
  85. {
  86. //
  87. // Assemble the path, since I just have the relative path from windir
  88. //
  89. _tcscpy(tszFileName, _tgetenv(TEXT("WINDIR")));
  90. _tcscat(tszFileName, g_tszFileVersionList[iCount]);
  91. //
  92. // Call function to do the work since we have full path
  93. //
  94. hr = InfoPerFile(ptszLogFile, tszFileName);
  95. DH_HRCHECK_ABORT(hr, TEXT("InfoPerFile"));
  96. }
  97. ErrReturn:
  98. return hr;
  99. }
  100. //+---------------------------------------------------------------------------
  101. //
  102. // Function: InfoPerFile
  103. //
  104. // Synopsis: This function takes the log file path, and the filename,
  105. // and then will put out the relevant information from the
  106. // file to be logged.
  107. //
  108. //
  109. // Arguments: [ptszLogFile] -- Log file name
  110. // [ptszFileName] -- File to be examined
  111. //
  112. // Returns: HRESULT
  113. //
  114. // History: 9/21/00 SHeffner Created
  115. //
  116. // 03-May-2001 WeiyouC ReWritten
  117. //
  118. //----------------------------------------------------------------------------
  119. HRESULT InfoPerFile(LPTSTR ptszLogFile, LPTSTR ptszFileName)
  120. {
  121. HRESULT hr = S_OK;
  122. HANDLE hFile = INVALID_HANDLE_VALUE;
  123. FILE* fpLog = NULL;
  124. void* pvBuf = NULL;
  125. UINT uLen = 0;;
  126. DWORD dwSize = 0;
  127. DWORD dwResult = 0;
  128. DWORD i = 0;;
  129. TCHAR szString[MAX_PATH];
  130. BY_HANDLE_FILE_INFORMATION FileInfo;
  131. SYSTEMTIME SysTime;
  132. VS_FIXEDFILEINFO FixedFileInfo;
  133. DH_VDATEPTRIN(ptszLogFile, TCHAR);
  134. DH_VDATEPTRIN(ptszFileName, TCHAR);
  135. //
  136. // Open up our log file, and log the file we are processing
  137. //
  138. fpLog = _tfopen(ptszLogFile, TEXT("a"));
  139. DH_ABORTIF(NULL == fpLog,
  140. E_FAIL,
  141. TEXT("a"));
  142. fprintf(fpLog, "\n%S\n", ptszFileName);
  143. //
  144. // Open up the file so that we can get the information from the handle
  145. // If we are unable to do this we will just log the generic not
  146. // able to find file.
  147. //
  148. hFile = CreateFile(ptszFileName,
  149. GENERIC_READ,
  150. FILE_SHARE_READ,
  151. NULL,
  152. OPEN_EXISTING,
  153. FILE_ATTRIBUTE_NORMAL,
  154. NULL);
  155. if (INVALID_HANDLE_VALUE != hFile)
  156. {
  157. if (FALSE != GetFileInformationByHandle(hFile, &FileInfo))
  158. {
  159. //
  160. // Get file creation time
  161. //
  162. FileTimeToSystemTime(&FileInfo.ftCreationTime, &SysTime);
  163. fprintf(fpLog,
  164. "\tCreation Date=%S %S %lu, %lu %lu:%lu:%lu\n",
  165. g_tszDay[SysTime.wDayOfWeek],
  166. g_tszMonth[SysTime.wMonth - 1],
  167. SysTime.wDay,
  168. SysTime.wYear,
  169. SysTime.wHour,
  170. SysTime.wMinute,
  171. SysTime.wSecond);
  172. //
  173. // Get file last access time
  174. //
  175. FileTimeToSystemTime(&FileInfo.ftLastAccessTime, &SysTime);
  176. fprintf(fpLog,
  177. "\tLast Access Date=%S %S %lu, %lu %lu:%lu:%lu\n",
  178. g_tszDay[SysTime.wDayOfWeek],
  179. g_tszMonth[SysTime.wMonth - 1],
  180. SysTime.wDay,
  181. SysTime.wYear,
  182. SysTime.wHour,
  183. SysTime.wMinute,
  184. SysTime.wSecond);
  185. //
  186. // Get file last write time
  187. //
  188. FileTimeToSystemTime(&FileInfo.ftLastWriteTime, &SysTime);
  189. fprintf(fpLog,
  190. "\tLast Write Date=%S %S %lu, %lu %lu:%lu:%lu\n",
  191. g_tszDay[SysTime.wDayOfWeek],
  192. g_tszMonth[SysTime.wMonth - 1],
  193. SysTime.wDay,
  194. SysTime.wYear,
  195. SysTime.wHour,
  196. SysTime.wMinute,
  197. SysTime.wSecond);
  198. //
  199. // Get file attributes
  200. //
  201. _tcscpy(szString, FileInfo.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE ? TEXT("ARCHIVE ") : TEXT(""));
  202. _tcscat(szString, FileInfo.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED ? TEXT("COMPRESSED ") : TEXT(""));
  203. _tcscat(szString, FileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? TEXT("DIRECTORY ") : TEXT(""));
  204. _tcscat(szString, FileInfo.dwFileAttributes & FILE_ATTRIBUTE_ENCRYPTED ? TEXT("ENCRYPTED ") : TEXT(""));
  205. _tcscat(szString, FileInfo.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN ? TEXT("HIDDEN ") : TEXT(""));
  206. _tcscat(szString, FileInfo.dwFileAttributes & FILE_ATTRIBUTE_NORMAL ? TEXT("NORMAL ") : TEXT(""));
  207. _tcscat(szString, FileInfo.dwFileAttributes & FILE_ATTRIBUTE_OFFLINE ? TEXT("OFFLINE ") : TEXT(""));
  208. _tcscat(szString, FileInfo.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? TEXT("READONLY ") : TEXT(""));
  209. _tcscat(szString, FileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT ? TEXT("REPARSE_POINT ") : TEXT(""));
  210. _tcscat(szString, FileInfo.dwFileAttributes & FILE_ATTRIBUTE_SPARSE_FILE ? TEXT("SPARSE_FILE ") : TEXT(""));
  211. _tcscat(szString, FileInfo.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM ? TEXT("SYSTEM ") : TEXT(""));
  212. _tcscat(szString, FileInfo.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY ? TEXT("TEMPORARY ") : TEXT(""));
  213. fprintf(fpLog, "\tAttributes=%S\n", szString);
  214. //
  215. // Get the VolumeSerialNumber, FileSize, and Number of Links
  216. //
  217. fprintf(fpLog,
  218. "\tVolumeSerialNumber=%lu\n",
  219. FileInfo.dwVolumeSerialNumber);
  220. fprintf(fpLog,
  221. "\tFileSize=%lu%lu\n",
  222. FileInfo.nFileSizeHigh,
  223. FileInfo.nFileSizeLow);
  224. fprintf(fpLog,
  225. "\tNumberOfLinks=%lu\n",
  226. FileInfo.nNumberOfLinks);
  227. dwSize = GetFileVersionInfoSize(ptszFileName, &dwResult);
  228. if (0 != dwSize)
  229. {
  230. pvBuf = malloc(dwSize);
  231. DH_ABORTIF(NULL == pvBuf,
  232. E_OUTOFMEMORY,
  233. TEXT("malloc"));
  234. GetFileVersionInfo(ptszFileName,
  235. dwResult,
  236. dwSize,
  237. (LPVOID) pvBuf);
  238. //
  239. // Read the list of languages and code pages.
  240. //
  241. VerQueryValue(pvBuf,
  242. TEXT("\\VarFileInfo\\Translation"),
  243. (LPVOID*)&lpTranslate,
  244. &uLen);
  245. //
  246. // Read the Version info for each language and code page.
  247. //
  248. for (i=0; i < (uLen / sizeof(struct LANGANDCODEPAGE)); i++ )
  249. {
  250. char* lpBuffer;
  251. DWORD dwBytes, dwCount = 0;
  252. fprintf(fpLog,
  253. "\tLanguage=%x%x\n",
  254. lpTranslate[i].wLanguage,
  255. lpTranslate[i].wCodePage);
  256. for (dwCount = 0; dwCount < ARRAYSIZE(g_tszVersionResource); dwCount++)
  257. {
  258. //
  259. // Generate the string, for getting the resource
  260. // based on the language, then retrieve this, and
  261. // then put it to the log file.
  262. //
  263. _stprintf(szString,
  264. TEXT("\\StringFileInfo\\%04x%04x\\%s"),
  265. lpTranslate[i].wLanguage,
  266. lpTranslate[i].wCodePage,
  267. g_tszVersionResource[dwCount]);
  268. VerQueryValue(pvBuf,
  269. szString,
  270. (LPVOID *) &lpBuffer,
  271. &uLen);
  272. if (0 != uLen)
  273. {
  274. fprintf(fpLog,
  275. "\t%S=%S\n",
  276. g_tszVersionResource[dwCount],
  277. lpBuffer);
  278. }
  279. } //While loop end, for each resource
  280. } //for loop end for each language
  281. } //If check for getting the fileversioninfosize
  282. } //if check for GetFileInformationByHandle on the file
  283. } //if check on can I open this file
  284. ErrReturn:
  285. if (INVALID_HANDLE_VALUE != hFile)
  286. {
  287. CloseHandle(hFile);
  288. hFile = INVALID_HANDLE_VALUE;
  289. }
  290. if (NULL != fpLog)
  291. {
  292. fclose(fpLog);
  293. }
  294. if (NULL != pvBuf)
  295. {
  296. free(pvBuf);
  297. }
  298. return hr;
  299. }