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