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.

282 lines
7.0 KiB

  1. //
  2. // FILELIST.C
  3. //
  4. #include "sigverif.h"
  5. LPTSTR
  6. MyStrStr(
  7. LPTSTR lpString,
  8. LPTSTR lpSubString
  9. )
  10. {
  11. if (!lpString || !lpSubString) {
  12. return NULL;
  13. }
  14. return (StrStrI(lpString, lpSubString));
  15. }
  16. void
  17. InsertFileNodeIntoList(
  18. LPFILENODE lpFileNode
  19. )
  20. {
  21. LPFILENODE lpTempNode = g_App.lpFileList;
  22. LPFILENODE lpPrevNode = NULL;
  23. INT iRet;
  24. if (!lpFileNode) {
  25. return;
  26. }
  27. if (!g_App.lpFileList) {
  28. //
  29. // Initialize the global file lists
  30. //
  31. g_App.lpFileList = lpFileNode;
  32. g_App.lpFileLast = lpFileNode;
  33. } else {
  34. for(lpTempNode=g_App.lpFileList;lpTempNode;lpTempNode=lpTempNode->next) {
  35. //
  36. // Insert items sorted by directory and then filename
  37. //
  38. iRet = lstrcmp(lpTempNode->lpDirName, lpFileNode->lpDirName);
  39. if (iRet == 0) {
  40. //
  41. // If the directory names match, key off the filename
  42. //
  43. iRet = lstrcmp(lpTempNode->lpFileName, lpFileNode->lpFileName);
  44. }
  45. if (iRet >= 0) {
  46. if (!lpPrevNode) {
  47. //
  48. // Insert at the head of the list
  49. //
  50. lpFileNode->next = lpTempNode;
  51. g_App.lpFileList = lpFileNode;
  52. return;
  53. } else {
  54. //
  55. // Inserting between lpPrevNode and lpTempNode
  56. //
  57. lpFileNode->next = lpTempNode;
  58. lpPrevNode->next = lpFileNode;
  59. return;
  60. }
  61. }
  62. lpPrevNode = lpTempNode;
  63. }
  64. //
  65. // There were no matches, so insert this item at the end of the list
  66. //
  67. g_App.lpFileLast->next = lpFileNode;
  68. g_App.lpFileLast = lpFileNode;
  69. }
  70. }
  71. BOOL
  72. IsFileAlreadyInList(
  73. LPTSTR lpDirName,
  74. LPTSTR lpFileName
  75. )
  76. {
  77. LPFILENODE lpFileNode;
  78. CharLowerBuff(lpDirName, lstrlen(lpDirName));
  79. CharLowerBuff(lpFileName, lstrlen(lpFileName));
  80. for(lpFileNode=g_App.lpFileList;lpFileNode;lpFileNode=lpFileNode->next) {
  81. if (!lstrcmp(lpFileNode->lpFileName, lpFileName) && !lstrcmp(lpFileNode->lpDirName, lpDirName)) {
  82. return TRUE;
  83. }
  84. }
  85. return FALSE;
  86. }
  87. //
  88. // Free all the memory allocated in a single File Node.
  89. //
  90. void
  91. DestroyFileNode(
  92. LPFILENODE lpFileNode
  93. )
  94. {
  95. if (!lpFileNode) {
  96. return;
  97. }
  98. if (lpFileNode->lpFileName) {
  99. FREE(lpFileNode->lpFileName);
  100. }
  101. if (lpFileNode->lpDirName) {
  102. FREE(lpFileNode->lpDirName);
  103. }
  104. if (lpFileNode->lpVersion) {
  105. FREE(lpFileNode->lpVersion);
  106. }
  107. if (lpFileNode->lpCatalog) {
  108. FREE(lpFileNode->lpCatalog);
  109. }
  110. if (lpFileNode->lpSignedBy) {
  111. FREE(lpFileNode->lpSignedBy);
  112. }
  113. if (lpFileNode->lpTypeName) {
  114. FREE(lpFileNode->lpTypeName);
  115. }
  116. if (lpFileNode) {
  117. FREE(lpFileNode);
  118. lpFileNode = NULL;
  119. }
  120. }
  121. //
  122. // Free all the memory allocated in the g_App.lpFileList.
  123. //
  124. void
  125. DestroyFileList(
  126. BOOL bClear
  127. )
  128. {
  129. LPFILENODE lpFileNode;
  130. while(g_App.lpFileList) {
  131. lpFileNode = g_App.lpFileList->next;
  132. DestroyFileNode(g_App.lpFileList);
  133. g_App.lpFileList = lpFileNode;
  134. }
  135. g_App.lpFileLast = NULL;
  136. if (bClear) {
  137. g_App.dwFiles = 0;
  138. g_App.dwSigned = 0;
  139. g_App.dwUnsigned = 0;
  140. }
  141. }
  142. LPFILENODE
  143. CreateFileNode(
  144. LPTSTR lpDirectory,
  145. LPTSTR lpFileName
  146. )
  147. {
  148. DWORD Err = ERROR_SUCCESS;
  149. LPFILENODE lpFileNode;
  150. TCHAR szDirName[MAX_PATH];
  151. TCHAR szFullPathName[MAX_PATH];
  152. FILETIME ftLocalTime;
  153. WIN32_FILE_ATTRIBUTE_DATA faData;
  154. BOOL bRet;
  155. ULONG BufCbSize;
  156. lpFileNode = (LPFILENODE) MALLOC(sizeof(FILENODE));
  157. if (!lpFileNode) {
  158. Err = ERROR_NOT_ENOUGH_MEMORY;
  159. goto clean0;
  160. }
  161. BufCbSize = (lstrlen(lpFileName) + 1) * sizeof(TCHAR);
  162. lpFileNode->lpFileName = (LPTSTR)MALLOC(BufCbSize);
  163. if (!lpFileNode->lpFileName) {
  164. Err = ERROR_NOT_ENOUGH_MEMORY;
  165. goto clean0;
  166. }
  167. StringCbCopy(lpFileNode->lpFileName, BufCbSize, lpFileName);
  168. CharLowerBuff(lpFileNode->lpFileName, lstrlen(lpFileNode->lpFileName));
  169. if (lpDirectory) {
  170. BufCbSize = (lstrlen(lpDirectory) + 1) * sizeof(TCHAR);
  171. lpFileNode->lpDirName = (LPTSTR)MALLOC(BufCbSize);
  172. if (!lpFileNode->lpDirName) {
  173. Err = ERROR_NOT_ENOUGH_MEMORY;
  174. goto clean0;
  175. }
  176. StringCbCopy(lpFileNode->lpDirName, BufCbSize, lpDirectory);
  177. CharLowerBuff(lpFileNode->lpDirName, lstrlen(lpFileNode->lpDirName));
  178. } else {
  179. if (GetCurrentDirectory(cA(szDirName), szDirName) == 0) {
  180. Err = GetLastError();
  181. goto clean0;
  182. }
  183. CharLowerBuff(szDirName, lstrlen(szDirName));
  184. BufCbSize = (lstrlen(szDirName) + 1) * sizeof(TCHAR);
  185. lpFileNode->lpDirName = (LPTSTR)MALLOC(BufCbSize);
  186. if (!lpFileNode->lpDirName) {
  187. Err = ERROR_NOT_ENOUGH_MEMORY;
  188. goto clean0;
  189. }
  190. StringCbCopy(lpFileNode->lpDirName, BufCbSize, szDirName);
  191. CharLowerBuff(lpFileNode->lpDirName, lstrlen(lpFileNode->lpDirName));
  192. }
  193. //
  194. // Store away the last access time for logging purposes.
  195. //
  196. if (SUCCEEDED(StringCchCopy(szFullPathName, cA(szFullPathName), lpFileNode->lpDirName)) &&
  197. pSetupConcatenatePaths(szFullPathName, lpFileName, cA(szFullPathName), NULL)) {
  198. ZeroMemory(&faData, sizeof(WIN32_FILE_ATTRIBUTE_DATA));
  199. bRet = GetFileAttributesEx(szFullPathName, GetFileExInfoStandard, &faData);
  200. if (bRet) {
  201. FileTimeToLocalFileTime(&faData.ftLastWriteTime, &ftLocalTime);
  202. FileTimeToSystemTime(&ftLocalTime, &lpFileNode->LastModified);
  203. }
  204. }
  205. clean0:
  206. if (Err != ERROR_SUCCESS) {
  207. //
  208. // If we get here then we weren't able to allocate all of the memory needed
  209. // for this structure, so free up any memory we were able to allocate and
  210. // reutrn NULL.
  211. //
  212. if (lpFileNode) {
  213. if (lpFileNode->lpFileName) {
  214. FREE(lpFileNode->lpFileName);
  215. }
  216. if (lpFileNode->lpDirName) {
  217. FREE(lpFileNode->lpDirName);
  218. }
  219. FREE(lpFileNode);
  220. }
  221. lpFileNode = NULL;
  222. }
  223. SetLastError(Err);
  224. return lpFileNode;
  225. }