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.

284 lines
12 KiB

  1. ' Windows Installer utility to generate file cabinets from MSI database
  2. ' For use with Windows Scripting Host, CScript.exe or WScript.exe
  3. ' Copyright (c) 1999-2000, Microsoft Corporation
  4. ' Demonstrates the access to install engine and actions
  5. '
  6. Option Explicit
  7. ' FileSystemObject.CreateTextFile and FileSystemObject.OpenTextFile
  8. Const OpenAsASCII = 0
  9. Const OpenAsUnicode = -1
  10. ' FileSystemObject.CreateTextFile
  11. Const OverwriteIfExist = -1
  12. Const FailIfExist = 0
  13. ' FileSystemObject.OpenTextFile
  14. Const OpenAsDefault = -2
  15. Const CreateIfNotExist = -1
  16. Const FailIfNotExist = 0
  17. Const ForReading = 1
  18. Const ForWriting = 2
  19. Const ForAppending = 8
  20. Const msiOpenDatabaseModeReadOnly = 0
  21. Const msiOpenDatabaseModeTransact = 1
  22. Const msiViewModifyInsert = 1
  23. Const msiViewModifyUpdate = 2
  24. Const msiViewModifyAssign = 3
  25. Const msiViewModifyReplace = 4
  26. Const msiViewModifyDelete = 6
  27. Const msiUILevelNone = 2
  28. Const msiRunModeSourceShortNames = 9
  29. Const msidbFileAttributesNoncompressed = &h00002000
  30. Dim argCount:argCount = Wscript.Arguments.Count
  31. Dim iArg:iArg = 0
  32. If argCount > 0 Then If InStr(1, Wscript.Arguments(0), "?", vbTextCompare) > 0 Then argCount = 0
  33. If (argCount < 2) Then
  34. Wscript.Echo "Windows Installer utility to generate compressed file cabinets from MSI database" &_
  35. vbNewLine & " The 1st argument is the path to MSI database, at the source file root" &_
  36. vbNewLine & " The 2nd argument is the base name used for the generated files (DDF, INF, RPT)" &_
  37. vbNewLine & " The 3rd argument can optionally specify separate source location from the MSI" &_
  38. vbNewLine & " The following options may be specified at any point on the command line" &_
  39. vbNewLine & " /L to use LZX compression instead of MSZIP" &_
  40. vbNewLine & " /F to limit cabinet size to 1.44 MB floppy size rather than CD" &_
  41. vbNewLine & " /C to run compression, else only generates the .DDF file" &_
  42. vbNewLine & " /U to update the MSI database to reference the generated cabinet" &_
  43. vbNewLine & " /E to embed the cabinet file in the installer package as a stream" &_
  44. vbNewLine & " /S to sequence number file table, ordered by directories" &_
  45. vbNewLine & " /R to revert to non-cabinet install, removes cabinet if /E specified" &_
  46. vbNewLine & " Notes:" &_
  47. vbNewLine & " In order to generate a cabinet, MAKECAB.EXE must be on the PATH" &_
  48. vbNewLine & " base name used for files and cabinet stream is case-sensitive" &_
  49. vbNewLine & " If source type set to compressed, all files will be opened at the root" &_
  50. vbNewLine & " (The /R option removes the compressed bit - SummaryInfo property 15 & 2)" &_
  51. vbNewLine & " To replace an embedded cabinet, include the options: /R /C /U /E" &_
  52. vbNewLine & " Does not handle updating of Media table to handle multiple cabinets" &_
  53. vbNewLine &_
  54. vbNewLine & "Copyright (C) Microsoft Corporation, 1999-2000. All rights reserved."
  55. Wscript.Quit 1
  56. End If
  57. ' Get argument values, processing any option flags
  58. Dim compressType : compressType = "MSZIP"
  59. Dim cabSize : cabSize = "CDROM"
  60. Dim makeCab : makeCab = False
  61. Dim embedCab : embedCab = False
  62. Dim updateMsi : updateMsi = False
  63. Dim sequenceFile : sequenceFile = False
  64. Dim removeCab : removeCab = False
  65. Dim databasePath : databasePath = NextArgument
  66. Dim baseName : baseName = NextArgument
  67. Dim sourceFolder : sourceFolder = NextArgument
  68. If Not IsEmpty(NextArgument) Then Fail "More than 3 arguments supplied" ' process any trailing options
  69. If Len(baseName) < 1 Or Len(baseName) > 8 Then Fail "Base file name must be from 1 to 8 characters"
  70. If Not IsEmpty(sourceFolder) And Right(sourceFolder, 1) <> "\" Then sourceFolder = sourceFolder & "\"
  71. Dim cabFile : cabFile = baseName & ".CAB"
  72. Dim cabName : cabName = cabFile : If embedCab Then cabName = "#" & cabName
  73. ' Connect to Windows Installer object
  74. On Error Resume Next
  75. Dim installer : Set installer = Nothing
  76. Set installer = Wscript.CreateObject("WindowsInstaller.Installer") : CheckError
  77. ' Open database
  78. Dim database, openMode, view, record, updateMode, sumInfo, sequence, lastSequence
  79. If updateMsi Or sequenceFile Or removeCab Then openMode = msiOpenDatabaseModeTransact Else openMode = msiOpenDatabaseModeReadOnly
  80. Set database = installer.OpenDatabase(databasePath, openMode) : CheckError
  81. ' Remove existing cabinet(s) and revert to source tree install if options specified
  82. If removeCab Then
  83. Set view = database.OpenView("SELECT DiskId, LastSequence, Cabinet FROM Media ORDER BY DiskId") : CheckError
  84. view.Execute : CheckError
  85. updateMode = msiViewModifyUpdate
  86. Set record = view.Fetch : CheckError
  87. If Not record Is Nothing Then ' Media table not empty
  88. If Not record.IsNull(3) Then
  89. If record.StringData(3) <> cabName Then Wscript.Echo "Warning, cabinet name in media table, " & record.StringData(3) & " does not match " & cabName
  90. record.StringData(3) = Empty
  91. End If
  92. record.IntegerData(2) = 9999 ' in case of multiple cabinets, force all files from 1st media
  93. view.Modify msiViewModifyUpdate, record : CheckError
  94. Do
  95. Set record = view.Fetch : CheckError
  96. If record Is Nothing Then Exit Do
  97. view.Modify msiViewModifyDelete, record : CheckError 'remove other cabinet records
  98. Loop
  99. End If
  100. Set sumInfo = database.SummaryInformation(3) : CheckError
  101. sumInfo.Property(11) = Now
  102. sumInfo.Property(13) = Now
  103. sumInfo.Property(15) = sumInfo.Property(15) And Not 2
  104. sumInfo.Persist
  105. Set view = database.OpenView("SELECT `Name`,`Data` FROM _Streams WHERE `Name`= '" & cabFile & "'") : CheckError
  106. view.Execute : CheckError
  107. Set record = view.Fetch
  108. If record Is Nothing Then
  109. Wscript.Echo "Warning, cabinet stream not found in package: " & cabFile
  110. Else
  111. view.Modify msiViewModifyDelete, record : CheckError
  112. End If
  113. Set sumInfo = Nothing ' must release stream
  114. database.Commit : CheckError
  115. If Not updateMsi Then Wscript.Quit 0
  116. End If
  117. ' Create an install session and execute actions in order to perform directory resolution
  118. installer.UILevel = msiUILevelNone
  119. Dim session : Set session = installer.OpenPackage(database,1) : If Err <> 0 Then Fail "Database: " & databasePath & ". Invalid installer package format"
  120. Dim shortNames : shortNames = session.Mode(msiRunModeSourceShortNames) : CheckError
  121. If Not IsEmpty(sourceFolder) Then session.Property("OriginalDatabase") = sourceFolder : CheckError
  122. Dim stat : stat = session.DoAction("CostInitialize") : CheckError
  123. If stat <> 1 Then Fail "CostInitialize failed, returned " & stat
  124. ' Check for non-cabinet files to avoid sequence number collisions
  125. lastSequence = 0
  126. If sequenceFile Then
  127. Set view = database.OpenView("SELECT Sequence,Attributes FROM File") : CheckError
  128. view.Execute : CheckError
  129. Do
  130. Set record = view.Fetch : CheckError
  131. If record Is Nothing Then Exit Do
  132. sequence = record.IntegerData(1)
  133. If (record.IntegerData(2) And msidbFileAttributesNoncompressed) <> 0 And sequence > lastSequence Then lastSequence = sequence
  134. Loop
  135. End If
  136. ' Join File table to Component table in order to find directories
  137. Dim orderBy : If sequenceFile Then orderBy = "Directory_" Else orderBy = "Sequence"
  138. Set view = database.OpenView("SELECT File,FileName,Directory_,Sequence,File.Attributes FROM File,Component WHERE Component_=Component ORDER BY " & orderBy) : CheckError
  139. view.Execute : CheckError
  140. ' Create DDF file and write header properties
  141. Dim FileSys : Set FileSys = CreateObject("Scripting.FileSystemObject") : CheckError
  142. Dim outStream : Set outStream = FileSys.CreateTextFile(baseName & ".DDF", OverwriteIfExist, OpenAsASCII) : CheckError
  143. outStream.WriteLine "; Generated from " & databasePath & " on " & Now
  144. outStream.WriteLine ".Set CabinetNameTemplate=" & baseName & "*.CAB"
  145. outStream.WriteLine ".Set CabinetName1=" & cabFile
  146. outStream.WriteLine ".Set ReservePerCabinetSize=8"
  147. outStream.WriteLine ".Set MaxDiskSize=" & cabSize
  148. outStream.WriteLine ".Set CompressionType=" & compressType
  149. outStream.WriteLine ".Set InfFileLineFormat=(*disk#*) *file#*: *file* = *Size*"
  150. outStream.WriteLine ".Set InfFileName=" & baseName & ".INF"
  151. outStream.WriteLine ".Set RptFileName=" & baseName & ".RPT"
  152. outStream.WriteLine ".Set InfHeader="
  153. outStream.WriteLine ".Set InfFooter="
  154. outStream.WriteLine ".Set DiskDirectoryTemplate=."
  155. outStream.WriteLine ".Set Compress=ON"
  156. outStream.WriteLine ".Set Cabinet=ON"
  157. ' Fetch each file and request the source path, then verify the source path
  158. Dim fileKey, fileName, folder, sourcePath, delim, message, attributes
  159. Do
  160. Set record = view.Fetch : CheckError
  161. If record Is Nothing Then Exit Do
  162. fileKey = record.StringData(1)
  163. fileName = record.StringData(2)
  164. folder = record.StringData(3)
  165. sequence = record.IntegerData(4)
  166. attributes = record.IntegerData(5)
  167. If (attributes And msidbFileAttributesNoncompressed) = 0 Then
  168. If sequence <= lastSequence Then
  169. If Not sequenceFile Then Fail "Duplicate sequence numbers in File table, use /S option"
  170. sequence = lastSequence + 1
  171. record.IntegerData(4) = sequence
  172. view.Modify msiViewModifyUpdate, record
  173. End If
  174. lastSequence = sequence
  175. delim = InStr(1, fileName, "|", vbTextCompare)
  176. If delim <> 0 Then
  177. If shortNames Then fileName = Left(fileName, delim-1) Else fileName = Right(fileName, Len(fileName) - delim)
  178. End If
  179. sourcePath = session.SourcePath(folder) & fileName
  180. outStream.WriteLine """" & sourcePath & """" & " " & fileKey
  181. If installer.FileAttributes(sourcePath) = -1 Then message = message & vbNewLine & sourcePath
  182. End If
  183. Loop
  184. outStream.Close
  185. REM Wscript.Echo "SourceDir = " & session.Property("SourceDir")
  186. If Not IsEmpty(message) Then Fail "The following files were not available:" & message
  187. ' Generate compressed file cabinet
  188. If makeCab Then
  189. Dim WshShell : Set WshShell = Wscript.CreateObject("Wscript.Shell") : CheckError
  190. Dim cabStat : cabStat = WshShell.Run("MakeCab.exe /f " & baseName & ".DDF", 1, True) : CheckError
  191. If cabStat <> 0 Then Fail "MAKECAB.EXE failed, possibly could not find source files, or invalid DDF format"
  192. End If
  193. ' Update Media table and SummaryInformation if requested
  194. If updateMsi Then
  195. Set view = database.OpenView("SELECT DiskId, LastSequence, Cabinet FROM Media ORDER BY DiskId") : CheckError
  196. view.Execute : CheckError
  197. updateMode = msiViewModifyUpdate
  198. Set record = view.Fetch : CheckError
  199. If record Is Nothing Then ' Media table empty
  200. Set record = Installer.CreateRecord(3)
  201. record.IntegerData(1) = 1
  202. updateMode = msiViewModifyInsert
  203. End If
  204. record.IntegerData(2) = lastSequence
  205. record.StringData(3) = cabName
  206. view.Modify updateMode, record
  207. Set sumInfo = database.SummaryInformation(3) : CheckError
  208. sumInfo.Property(11) = Now
  209. sumInfo.Property(13) = Now
  210. sumInfo.Property(15) = (shortNames And 1) + 2
  211. sumInfo.Persist
  212. End If
  213. ' Embed cabinet if requested
  214. If embedCab Then
  215. Set view = database.OpenView("SELECT `Name`,`Data` FROM _Streams") : CheckError
  216. view.Execute : CheckError
  217. Set record = Installer.CreateRecord(2)
  218. record.StringData(1) = cabFile
  219. record.SetStream 2, cabFile : CheckError
  220. view.Modify msiViewModifyAssign, record : CheckError 'replace any existing stream of that name
  221. End If
  222. ' Commit database in case updates performed
  223. database.Commit : CheckError
  224. Wscript.Quit 0
  225. ' Extract argument value from command line, processing any option flags
  226. Function NextArgument
  227. Dim arg
  228. Do ' loop to pull in option flags until an argument value is found
  229. If iArg >= argCount Then Exit Function
  230. arg = Wscript.Arguments(iArg)
  231. iArg = iArg + 1
  232. If (AscW(arg) <> AscW("/")) And (AscW(arg) <> AscW("-")) Then Exit Do
  233. Select Case UCase(Right(arg, Len(arg)-1))
  234. Case "C" : makeCab = True
  235. Case "E" : embedCab = True
  236. Case "F" : cabSize = "1.44M"
  237. Case "L" : compressType = "LZX"
  238. Case "R" : removeCab = True
  239. Case "S" : sequenceFile = True
  240. Case "U" : updateMsi = True
  241. Case Else: Wscript.Echo "Invalid option flag:", arg : Wscript.Quit 1
  242. End Select
  243. Loop
  244. NextArgument = arg
  245. End Function
  246. Sub CheckError
  247. Dim message, errRec
  248. If Err = 0 Then Exit Sub
  249. message = Err.Source & " " & Hex(Err) & ": " & Err.Description
  250. If Not installer Is Nothing Then
  251. Set errRec = installer.LastErrorRecord
  252. If Not errRec Is Nothing Then message = message & vbNewLine & errRec.FormatText
  253. End If
  254. Fail message
  255. End Sub
  256. Sub Fail(message)
  257. Wscript.Echo message
  258. Wscript.Quit 2
  259. End Sub