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.

270 lines
7.0 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. recab.c
  5. Abstract:
  6. This module implements a program that determines the files in an INF that
  7. are newer than the a given file and writes these into a file.
  8. The input consists of a target inf (drvindex.inf), and a target file;
  9. the output consists of an inf file.
  10. Author:
  11. Andrew Ritz (andrewr) 2-Feb-1999
  12. Revision History:
  13. --*/
  14. #include <windows.h>
  15. #include <stdio.h>
  16. #include <setupapi.h>
  17. #define PULONGLONG PDWORDLONG
  18. #include <sputils.h>
  19. //
  20. // Define program result codes (returned from main()).
  21. //
  22. #define SUCCESS 0
  23. #define FAILURE 1
  24. //
  25. // Keep statistics...
  26. //
  27. INT ProcessedLines = 0;
  28. BOOL
  29. ParseArgs(
  30. IN int argc,
  31. IN char *argv[]
  32. )
  33. {
  34. return(argc == 5);
  35. }
  36. BOOL
  37. IsFileNewer(
  38. IN PCWSTR SourceName,
  39. IN PWIN32_FIND_DATAW TargetFileData
  40. )
  41. {
  42. WIN32_FIND_DATAW SourceFileData;
  43. HANDLE SourceHandle;
  44. LARGE_INTEGER SourceFileTime,TargetFileTime;
  45. SourceHandle = FindFirstFileW( SourceName, &SourceFileData );
  46. if (SourceHandle == INVALID_HANDLE_VALUE) {
  47. fprintf(stderr, TEXT("Error finding file %ws (%d)\n"), SourceName, GetLastError() );
  48. return(FALSE);
  49. }
  50. SourceFileTime.LowPart = SourceFileData.ftLastWriteTime.dwLowDateTime;
  51. SourceFileTime.HighPart = SourceFileData.ftLastWriteTime.dwHighDateTime;
  52. TargetFileTime.LowPart = TargetFileData->ftLastWriteTime.dwLowDateTime;
  53. TargetFileTime.HighPart = TargetFileData->ftLastWriteTime.dwHighDateTime;
  54. FindClose(SourceHandle);
  55. return (SourceFileTime.QuadPart > TargetFileTime.QuadPart) ;
  56. }
  57. BOOL
  58. DoSection(
  59. IN HINF hInputInf,
  60. IN PCWSTR InputSectionName,
  61. IN FILE *OutFile,
  62. IN PWIN32_FIND_DATAW TargetFileData
  63. )
  64. {
  65. DWORD SectionCount, i;
  66. INFCONTEXT InputContext;
  67. UCHAR line[4096];
  68. WCHAR SourceFileName[MAX_PATH];
  69. SectionCount = SetupGetLineCountW(hInputInf,InputSectionName);
  70. for (i = 0; i < SectionCount; i++) {
  71. if (SetupGetLineByIndexW(hInputInf, InputSectionName, i, &InputContext)) {
  72. if(SetupGetStringFieldW(&InputContext,0,SourceFileName,MAX_PATH,NULL)) {
  73. if (IsFileNewer(SourceFileName,TargetFileData)) {
  74. WideCharToMultiByte(
  75. CP_OEMCP,
  76. 0,
  77. SourceFileName,
  78. -1,
  79. line,
  80. sizeof(line),
  81. NULL,
  82. NULL
  83. );
  84. fprintf(OutFile, TEXT("%s\n"),line);
  85. } else if (GetLastError() != NO_ERROR) {
  86. fprintf(stderr, TEXT("IsFileNewer failed\n"));
  87. return(FALSE);
  88. }
  89. } else {
  90. fprintf(stderr, TEXT("SetupGetStringField failed, ec = %d\n"), GetLastError());
  91. return(FALSE);
  92. }
  93. } else {
  94. fprintf(stderr, TEXT("SetupGetLineByIndex failed, ec = %d\n"), GetLastError());
  95. return(FALSE);
  96. }
  97. ProcessedLines += 1;
  98. }
  99. return(TRUE);
  100. }
  101. BOOL
  102. DoIt(
  103. IN char *InfName,
  104. IN char *TargetFileNameA,
  105. IN char *cwd,
  106. IN FILE *OutFile
  107. )
  108. {
  109. PCWSTR infFilename;
  110. PCWSTR TargetFileName;
  111. HINF hInputinf;
  112. BOOL b;
  113. WCHAR sectionName[256];
  114. PWSTR p;
  115. INFCONTEXT InfContext;
  116. WIN32_FIND_DATAW TargetFileData;
  117. HANDLE TargetHandle;
  118. b = FALSE;
  119. infFilename = pSetupAnsiToUnicode(InfName);
  120. TargetFileName = pSetupAnsiToUnicode(TargetFileNameA);
  121. //
  122. // Only proceed if we've got a file to work with.
  123. //
  124. if( infFilename ) {
  125. hInputinf = SetupOpenInfFileW(infFilename,NULL,INF_STYLE_WIN4,NULL);
  126. if(hInputinf != INVALID_HANDLE_VALUE) {
  127. SetCurrentDirectory(cwd);
  128. TargetHandle = FindFirstFileW( TargetFileName, &TargetFileData );
  129. if ( TargetHandle == INVALID_HANDLE_VALUE ) {
  130. fprintf( stderr, TEXT(" couldn't findfirstfile %ws, %d\n "),TargetFileName,GetLastError());
  131. SetupCloseInfFile(hInputinf);
  132. return(FALSE);
  133. }
  134. fprintf(OutFile,"[Version]\n");
  135. fprintf(OutFile,"signature=\"$Windows NT$\"\n\n");
  136. fprintf(OutFile,"[Files]\n");
  137. p = wcsrchr( TargetFileName, L'\\' );
  138. if (!p) {
  139. p = (PWSTR)TargetFileName;
  140. } else {
  141. p++;
  142. }
  143. wcscpy(sectionName,p);
  144. p=wcsrchr(sectionName,L'.');
  145. if (p) {
  146. *p = 0;
  147. }
  148. b = DoSection( hInputinf,
  149. sectionName,
  150. OutFile,
  151. &TargetFileData );
  152. //
  153. // Print Statistics...
  154. //
  155. fprintf( stderr, " Total lines processed: %6d\n", ProcessedLines );
  156. //
  157. // Close up our inf handles.
  158. //
  159. SetupCloseInfFile(hInputinf);
  160. } else {
  161. fprintf(stderr,"Unable to open inf file %ws %d\n",infFilename, GetLastError());
  162. }
  163. } else {
  164. fprintf(stderr,"Unable to convert filename %s to Unicode %d\n",InfName, GetLastError());
  165. }
  166. pSetupFree(infFilename);
  167. return(b);
  168. }
  169. int
  170. __cdecl
  171. main(
  172. IN int argc,
  173. IN char *argv[]
  174. )
  175. {
  176. FILE *OutputFile;
  177. BOOL b;
  178. //
  179. // Assume failure.
  180. //
  181. b = FALSE;
  182. if(!pSetupInitializeUtils()) {
  183. return FAILURE;
  184. }
  185. if(ParseArgs(argc,argv)) {
  186. //
  187. // Open the output file.
  188. //
  189. OutputFile = fopen(argv[4],"wt");
  190. if(OutputFile) {
  191. fprintf(stdout,"%s: creating %s from %s and %s\n",argv[0],argv[4],argv[1],argv[2]);
  192. b = DoIt( argv[1],
  193. argv[2],
  194. argv[3],
  195. OutputFile
  196. );
  197. fclose(OutputFile);
  198. } else {
  199. fprintf(stderr,"%s: Unable to create output file %s\n",argv[0],argv[3]);
  200. }
  201. } else {
  202. fprintf( stderr,"generate file with newer dependencies. Usage:\n" );
  203. fprintf( stderr,"%s <inf file> <file> <directory> <output file>\n", argv[0] );
  204. fprintf( stderr,"\n" );
  205. fprintf( stderr," <inf file> - inf containing list of dependencies\n" );
  206. fprintf( stderr," <file> - contains the file to compare against.\n" );
  207. fprintf( stderr," <directory> - directory where the files live.\n" );
  208. fprintf( stderr," <output file> - output inf with newer dependencies\n" );
  209. fprintf( stderr,"\n" );
  210. fprintf( stderr,"\n" );
  211. }
  212. pSetupUninitializeUtils();
  213. return(b ? SUCCESS : FAILURE);
  214. }