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.

345 lines
11 KiB

  1. REM
  2. REM sxs patch tool for asms trees under build
  3. REM
  4. REM xiaoyuw @ 11/01/2001
  5. REM
  6. setlocal ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
  7. REM main function Begin {
  8. if "%1" == "" (
  9. set AsmRoot=%_NTPOSTBLD%
  10. ) else (
  11. set AsmRoot=%1
  12. )
  13. set IsThisMytest=0
  14. if /i "%2" equ "test" (
  15. set IsThisMytest=1
  16. )
  17. set myMove=move
  18. set NumOfNextNewAsmsCab=
  19. set AsmCabinetStore=
  20. call :PrepareBaseAssemblyCabinet
  21. set NewAsmsCabinet=asms%NumOfNextNewAsmsCab%.cab
  22. call :CreateNewCabinet
  23. :cleanup
  24. REM remove the directory for all old asms.cab
  25. if "!IsThisMytest!" == "0" (
  26. if not "%AsmCabinetStore%" == "" call ExecuteCmd.cmd "rmdir /s /q %%AsmCabinetStore%%"
  27. )
  28. REM
  29. REM remove the directory where we try to create the cabinet
  30. REM
  31. if not "%NewAssemblyCabinetDir%"=="" call ExecuteCmd.cmd "rmdir /s /q %%NewAssemblyCabinetDir%%"
  32. goto :Eof
  33. REM main function End }
  34. REM Sub Function PrepareBaseAssemblyCabinet Begin {
  35. REM Function:
  36. REM create the cabinet from this directory, each subdir would be a folder in the cabinet
  37. REM rename this cabinet as asms_n.cab
  38. REM move this cabinet into asms
  39. REM remove the directory
  40. :PrepareBaseAssemblyCabinet
  41. if not exist %AsmRoot%\asms.cab goto :Eof
  42. REM
  43. REM create a tempDir and extrace cab files from %AsmRoot% to it
  44. REM Directory name used in CABARC.cmd MUST has a backslash at the end
  45. REM
  46. set AsmCabinetStore=%TEMP%\%random%\
  47. if exist %AsmCabinetStore% (
  48. call ExecuteCmd.cmd "rmdir /s /q %%AsmCabinetStore%%"
  49. if "!errorlevel!"=="1" (
  50. call logmsg.cmd "Failed to delete for %%AsmCabinetStore%%"
  51. goto :Eof
  52. )
  53. )
  54. call ExecuteCmd.cmd "mkdir %%AsmCabinetStore%%"
  55. if "!errorlevel!"=="1" (
  56. call logmsg.cmd "Failed to create temporary directory %%AsmCabinetStore%% for cabinets"
  57. goto :EOF
  58. )
  59. REM
  60. REM extrace asms.cab which is the base cabinet for all
  61. REM
  62. call ExecuteCmd.cmd "cabarc.exe -p x %%AsmRoot%%\asms.cab %%AsmCabinetStore%%"
  63. if "!errorlevel!"=="1" (
  64. call errmsg.cmd "Failed to extract the assembly cabinet %%i"
  65. goto :EOF
  66. )
  67. REM
  68. REM there are other asms.cab avaiable and need extract them
  69. REM
  70. for /L %%i in (1,1,100) do (
  71. set CurrentAsmCabinet=%AsmRoot%\asms%%i.cab
  72. REM
  73. REM if no more asms cabinet, exit the loop
  74. REM
  75. if not exist !CurrentAsmCabinet! (
  76. set NumOfNextNewAsmsCab=%%i
  77. goto :Eof
  78. )
  79. REM
  80. REM extrace thd cabinet into the Cabinet temporary directory
  81. REM
  82. call ExecuteCmd.cmd "cabarc.exe -p x %%CurrentAsmCabinet%% %%AsmCabinetStore%%"
  83. if "!errorlevel!"=="1" (
  84. call errmsg.cmd "Failed to extract the assembly cabinet %%i"
  85. goto :EOF
  86. )
  87. call :ApplyPatchToAssemblyCabinet
  88. )
  89. goto :Eof
  90. REM sub function PrepareBaseAssemblyCabinet End }
  91. REM sub function start {
  92. :ApplyPatchToAssemblyCabinet
  93. REM
  94. REM the reason to put patch-apply inside of the loop is to guarrantee the sequence of patches
  95. REM could be applied
  96. REM
  97. REM if there exists patches, apply patch based on base
  98. REM
  99. set OneAssemblyPatchFile=
  100. for /F %%I in ('dir /s/b %AsmCabinetStore%\assembly.patch') do (
  101. set OneAssemblyPatchFile=%%I
  102. goto :FindPatchInAssembly
  103. )
  104. if not "!OneAssemblyPatchFile!" == "" (
  105. :FindPatchInAssembly
  106. for /F %%I in ('dir /s /b %%AsmCabinetStore%%assembly.patch') do (
  107. set OneAssemblyPatchFile=%%I
  108. for /F %%w in ('type %%OneAssemblyPatchFile%%') do set AssemblyIdentityAttribute=%%w
  109. set AssemblyIdentityAttribute=!AssemblyIdentityAttribute:"=\"!
  110. for /F %%w in ('sxs_GetAsmDir.exe -AsmIdToAsmDir %%AssemblyIdentityAttribute%%') do set BaseAssemblyDirectory=%%w
  111. set PatchedAssemblyDir=%%~dpI
  112. REM
  113. REM for each .patch in the directory, apply patch
  114. REM
  115. for /F %%w in ('dir /s /b %%PatchedAssemblyDir%%*.patch') do (
  116. if /i "%%~nxw" neq "assembly.patch" (
  117. REM
  118. REM get the real filename by remove the ending extension ".patch"
  119. REM
  120. set PatchFileName=%%w
  121. set DllFileName=%%~nxw
  122. set DllFileName=!DllFileName:.patch=!
  123. set BaseDllFileName=%AsmCabinetStore%!BaseAssemblyDirectory!\!DllFileName!
  124. set TargetDllFileName=%%~pdw!DllFileName!
  125. REM
  126. REM wpatch.exe PatchFile BaseFile TargetFile
  127. REM
  128. call ExecuteCmd.cmd "wpatch.exe %%PatchFileName%% %%BaseDllFileName%% %%TargetDllFileName%%"
  129. if "!errorlevel!"=="1" (
  130. call errmsg.cmd "Failed to apply patch %%w to %%i %%BaseDllFileName%%"
  131. goto :EOF
  132. )
  133. REM
  134. REM remove the xxx.dll.patch after apply it
  135. REM
  136. call ExecuteCmd.cmd "del %%w"
  137. )
  138. )
  139. REM
  140. REM remove assembly.patch
  141. REM
  142. call ExecuteCmd.cmd "del %%I"
  143. )
  144. )
  145. goto :Eof
  146. REM }
  147. REM sub Function CreateNewCabinet Begin {
  148. REM Function Description:
  149. REM create a directory
  150. REM for assemblies in asms(
  151. REM create a temporary directory to put files
  152. REM for each assembly(
  153. REM create x86_name_xxxx_hash directory under the cabinet directory
  154. REM locate its baseAssembly
  155. REM for all the files of this assembly (
  156. REM if it is a manifest, or a catalog,
  157. REM put it into the cabinet directory
  158. REM else if it is a brand new file (not exist in the BaseAssembly)
  159. REM put it into the cabinet directory
  160. REM else
  161. REM make a patch based on BaseAssemblyDll
  162. REM put the patch into the directory
  163. REM )
  164. REM )
  165. REM )
  166. :CreateNewCabinet
  167. set NewAssemblyCabinetDir=%TEMP%\%random%
  168. if exist %NewAssemblyCabinetDir% call ExecuteCmd.cmd "rmdir /s /q %%NewAssemblyCabinetDir%%"
  169. call ExecuteCmd.cmd "mkdir %%NewAssemblyCabinetDir%%"
  170. set NewAssemblyList=%NewAssemblyCabinetDir%\Assemblies.List
  171. @dir /s /b %AsmRoot%\asms\*.man > %NewAssemblyList%
  172. REM
  173. REM prepare cabarc.cmd line
  174. REM
  175. set CreateAsmCabinetCmd=cabarc.exe -p n %AsmRoot%\asms%NumOfNextNewAsmsCab%.cab
  176. set IsFirstFolderInCabinetCreated=0
  177. for /F %%f in (%NewAssemblyList%) do (
  178. set CurrentAssemblyPath=%%~dpf
  179. REM
  180. REM create one directory for an assembly
  181. REM
  182. for /F %%I in ('sxs_GetAsmDir.exe -manifestToAsmDir %%f') do set AssemblyFolder=%%I
  183. if "!AssemblyFolder!" == "" (
  184. call errmsg.cmd "AssemblyFolder generated is wrong\n";
  185. goto :Eof
  186. )
  187. if "!errorlevel!" == "1" (
  188. call errmsg.cmd "sxs_GetAsmDir.exe -manifest %%f failed!"
  189. goto :Eof
  190. )
  191. call ExecuteCmd.cmd "mkdir %%NewAssemblyCabinetDir%%\%%AssemblyFolder%%"
  192. if "!errorlevel!" == "1" (
  193. call errmsg.cmd "create directory %%NewAssemblyCabinetDir%%\%%AssemblyFolder%% failed"
  194. goto :Eof
  195. )
  196. call :GeneratePatchForOneAssembly
  197. if "!IsFirstFolderInCabinetCreated!"=="1" (
  198. set CreateAsmCabinetCmd=!CreateAsmCabinetCmd! +
  199. ) else (
  200. set IsFirstFolderInCabinetCreated=1
  201. )
  202. set CreateAsmCabinetCmd=!CreateAsmCabinetCmd! !AssemblyFolder!\*.*
  203. call logmsg.cmd "CreateAsmCabinetCmd is %%CreateAsmCabinetCmd%%"
  204. )
  205. REM
  206. REM time to create a cabinet
  207. REM
  208. pushd %NewAssemblyCabinetDir%\
  209. call ExecuteCmd.cmd "%%CreateAsmCabinetCmd%%"
  210. set error = !errorlevel!
  211. popd
  212. if "%error%"=="1" (
  213. call errmsg.cmd "Failed to create Cabinet for assemblies"
  214. goto :EOF
  215. )
  216. REM
  217. REM nuke %AsmRoot%asms and make it empty: we want to keep this dir in the build anyway
  218. REM
  219. call ExecuteCmd.Cmd "rmdir /s /q %%AsmRoot%%\asms"
  220. call ExecuteCmd.Cmd "mkdir %%AsmRoot%%\asms"
  221. goto :Eof
  222. REM sub Function CreateNewCabinet End}
  223. REM sub Function GeneratePatchForOneAssembly Start{
  224. :GeneratePatchForOneAssembly
  225. set BaseAssembly=
  226. REM
  227. REM find the possible Base Assembly from CabinetStore
  228. REM
  229. REM First Step: try to find base assembly with the same laguage ID
  230. REM
  231. if not "!AsmCabinetStore!" == "" (
  232. for /F "tokens=1-6 delims=_" %%I in ('echo %%AssemblyFolder%%') do set PossibleBaseAssembly=%%I_%%J_%%K_*_%%M_*
  233. for /F %%I in ('dir /b /ad /o-n %%AsmCabinetStore%%%%PossibleBaseAssembly%%') do (
  234. set BaseAssembly=%AsmCabinetStore%%%I
  235. goto :FoundBaseAssembly
  236. )
  237. REM
  238. REM Second Step: try to find base assembly with whatever laguage ID
  239. REM
  240. for /F "tokens=1-6 delims=_" %%I in ('echo %%AssemblyFolder%%') do set PossibleBaseAssmelby=%%I_%%J_%%K_*
  241. for /F %%I in ('dir /b /ad /o-n %%PossibleBaseAssembly%%') do (
  242. set BaseAssembly=%AsmCabinetStore%%%I
  243. goto :FoundBaseAssembly
  244. )
  245. )
  246. :FoundBaseAssembly
  247. REM
  248. REM for all files of this assembly, move it or its patch to the directory %%NewAssemblyCabinetDir%%\%%AssemblyFolder%%
  249. REM
  250. for /F %%I in ('dir /s /b %%CurrentAssemblyPath%%*.*') do (
  251. REM
  252. REM for .man and .cat file, move it
  253. REM
  254. if /i "%%~xI" equ ".man" (
  255. call ExecuteCmd.Cmd "%%myMove%% %%I %%NewAssemblyCabinetDir%%\%%AssemblyFolder%%"
  256. ) else (
  257. if /i "%%~xI" equ ".cat" (
  258. call ExecuteCmd.Cmd "%%myMove%% %%I %%NewAssemblyCabinetDir%%\%%AssemblyFolder%%"
  259. ) else (
  260. if "%BaseAssembly%" == "" (
  261. REM
  262. REM no BaseAssembly, just copy the file
  263. REM
  264. call ExecuteCmd.Cmd "%%myMove%% %%I %%NewAssemblyCabinetDir%%\%%AssemblyFolder%%"
  265. ) else (
  266. REM
  267. REM create patch on BaseAssembly files
  268. REM
  269. set BaseAssemblyFile=%BaseAssembly%\%%~nxI
  270. if exist %BaseAssemblyFile%. (
  271. REM
  272. REM create patch based on it
  273. REM
  274. set PatchFileName=%NewAssemblyCabinetDir%\%AssemblyFolder%\%%~nxI.patch
  275. call ExecuteCmd.cmd "mpatch.exe -NOCOMPARE -NOSYMS -NOPROGRESS -FAILBIGGER %%BaseAssemblyFile%% %%I %%PatchFileName%%"
  276. if "!errorlevel!"=="1" (
  277. call ExecuteCmd.Cmd "%%myMove%% %%I %%NewAssemblyCabinetDir%%\%%AssemblyFolder%%"
  278. ) else (
  279. REM
  280. REM generate assembly.patch
  281. REM
  282. if not exist %NewAssemblyCabinetDir%\%AssemblyFolder%\assembly.patch (
  283. for /F %%I in ('dir /s/b %%BaseAssembly%%\*.man*') do set BaseManifestFileName=%%I
  284. for /F %%I in ('sxs_GetAsmDir.exe -manifestToAsmID %%BaseManifestFileName%%') do echo %%I > %NewAssemblyCabinetDir%\%AssemblyFolder%\assembly.patch
  285. )
  286. )
  287. ) else (
  288. REM
  289. REM new file in assembly
  290. REM
  291. call ExecuteCmd.Cmd "%%myMove%% %%PatchFileName%% %%NewAssemblyCabinetDir%%\%%AssemblyFolder%%"
  292. )
  293. )
  294. )
  295. )
  296. )
  297. goto :Eof
  298. REM sub Function GeneratePatchForOneAssembly ends}
  299. goto :Eof