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.

572 lines
19 KiB

  1. VERSION 1.0 CLASS
  2. BEGIN
  3. MultiUse = -1 'True
  4. Persistable = 0 'NotPersistable
  5. DataBindingBehavior = 0 'vbNone
  6. DataSourceBehavior = 0 'vbNone
  7. MTSTransactionMode = 0 'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "FileImageCreator"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = True
  14. '===========================================================================================
  15. ' Compiland : FileImageCrerator.cls
  16. ' Author : Pierre Jacomet
  17. ' Version : 1.0
  18. '
  19. ' Description : Implements the Live Help File Image Creation Component
  20. '
  21. ' Called by : Any client that will deal with an HSC Live Help File Image
  22. '
  23. ' Environment data:
  24. ' Files that it uses (Specify if they are inherited in open state): NONE
  25. ' Parameters (Command Line) and usage mode {I,I/O,O}:
  26. ' N/A at this level
  27. '
  28. ' Parameters (inherited from environment) : NONE
  29. ' Public Variables created: NONE
  30. ' Environment Variables (Public or Module Level) modified:
  31. ' Com Object creates a couple of Shell Level Environment Variables which are needed
  32. ' by Source Safe Command Line interface.
  33. ' Environment Variables used in coupling with other routines: NONE
  34. ' Local variables : N/A at this level
  35. ' Problems detected :
  36. ' DCR Suggestions:
  37. ' - Make File Copies Incremental, even in those cases where things should be
  38. ' completely destroyed.
  39. ' - Incorporate Cancel Processing Logic
  40. '
  41. ' History:
  42. ' 2000-06-18 Initial Creation
  43. '===========================================================================================
  44. Option Explicit
  45. ' Instance Level Variables
  46. Private m_fso As Scripting.FileSystemObject ' Use for many file relaed operations
  47. Private m_strSSDB As String ' Source Safe DB - Will be set in Proc Environment for SS
  48. Private m_strSSUser As String ' Source Safe User - Will be set in Proc Env. for SS
  49. Private m_strSSProject As String ' Source Safe Project - Will be set in Proc Env for SS
  50. Private m_strLiveImageDir As String ' The HSC Live Hepl File Image Directory
  51. Private m_strWorkDir As String ' This is the HSC Working Directory
  52. Private m_bCancel As Boolean ' Not used yet
  53. Private m_WsShell As IWshShell ' Used to Shell and Wait for Sub-Processes
  54. Private m_bAdditiveImage As Boolean ' Says whether we are doing an incremental operation
  55. Private m_strRenamesFile As String ' The BAtch File Name for Renaming Files after Getting
  56. ' them from VSS
  57. Private m_strStatusMsg As String ' Records the Last Status Message sent by XRaiseEvent.
  58. Private m_dtStartTime As Date ' Records the Start Time for the Go Operation
  59. Private m_bExpandChmOnly ' Indicates whether we are either:
  60. ' False = creating the Live Help File + Expanding the CHMs
  61. ' True = Only Expanding the CHMs
  62. ' Needed to Set Shell Level Environment Variables
  63. Private Declare Function SetEnvironmentVariable Lib "kernel32" _
  64. Alias "SetEnvironmentVariableA" (ByVal lpName As String, ByVal lpValue As String) As Long
  65. ' Public Declares
  66. ' This event is used to motify Progress Status to clients that request it.
  67. Public Event GoStatus(strWhere As String, bCancel As Boolean)
  68. Function Init(Optional ByVal bAdditiveImage As Boolean = False) As Boolean
  69. Init = False
  70. Dim strErrMsg As String: strErrMsg = "": If (g_bOnErrorHandling) Then On Error GoTo Error_Handler
  71. GlobalInit
  72. Set m_fso = CreateObject("Scripting.FileSystemObject")
  73. m_strSSDB = "": m_strLiveImageDir = "": m_strWorkDir = "": m_strRenamesFile = ""
  74. m_bAdditiveImage = bAdditiveImage
  75. Set m_WsShell = CreateObject("Wscript.Shell")
  76. Init = True
  77. Common_Exit:
  78. Exit Function
  79. Error_Handler:
  80. g_XErr.SetInfo "FileImageCreator::Init", strErrMsg
  81. Err.Raise Err.Number
  82. End Function
  83. Function Go() As Boolean
  84. Go = False
  85. Dim strErrMsg As String: strErrMsg = "": If (g_bOnErrorHandling) Then On Error GoTo Error_Handler
  86. m_dtStartTime = Now
  87. If (m_bExpandChmOnly) Then
  88. expandchm m_strLiveImageDir, m_strWorkDir
  89. Else
  90. XRaiseEvent "Configuring SourceSafe Information"
  91. ' In this Section We connect to the SourceSafe depot and Get From
  92. ' There all the files that we need
  93. '
  94. ' The variables SSUSER / SSDIR are expected by Sourcesafe in the environment
  95. SetEnvironmentVariable "SSUSER", m_strSSUser
  96. SetEnvironmentVariable "SSDIR", m_strSSDB
  97. ' Now we will format the command for SS:
  98. '
  99. ' ss Get $/Whitler/ -I- -R
  100. '
  101. Dim strCmd As String
  102. strCmd = "SS Get " & m_strSSProject & " -I- -R"
  103. ' SourceSafe Gets stuff into the Current Directory.
  104. ' So we create a Temporary directory and switch to it.
  105. XRaiseEvent "Creating Temporary Directory"
  106. ' BUGBUG: We should use a function like MKTEMP to derive this name.
  107. Dim strTempDir As String: strTempDir = Environ$("TEMP") + "\__HSCHLI"
  108. If (m_fso.FolderExists(strTempDir)) Then
  109. m_fso.DeleteFolder strTempDir, Force:=True
  110. End If
  111. m_fso.CreateFolder (strTempDir)
  112. Dim strCurDir As String: strCurDir = CurDir$()
  113. ChDrive strTempDir: ChDir strTempDir
  114. ' Now we run the Sourcesafe Command.
  115. XRaiseEvent "Running SourceSafe Command"
  116. DoEvents
  117. m_WsShell.Run strCmd, True, True
  118. ResetAll2RW strTempDir
  119. ' Here I should apply the Rename Lists so that in case of an Additive Image
  120. ' we will copy over the right CHMs
  121. If (Len(m_strRenamesFile) <> 0) Then
  122. m_WsShell.Run "cmd /c " & m_strRenamesFile, True, True
  123. End If
  124. ' Now we copy All the contents of this Directory
  125. ' to the Live Help File Image Folder
  126. XRaiseEvent "Copying to Live Help File Image Directory"
  127. If (Not m_bAdditiveImage) Then
  128. m_fso.DeleteFolder m_strLiveImageDir, Force:=True
  129. End If
  130. m_fso.CopyFolder strTempDir, m_strLiveImageDir, OverWriteFiles:=True
  131. ' Now we need to expand ALL CHMs in the Live HelpFile Image into
  132. ' the Working Directory
  133. expandchm strTempDir, m_strWorkDir
  134. XRaiseEvent "Cleaning Up"
  135. ChDrive strCurDir: ChDir strCurDir
  136. m_fso.DeleteFolder strTempDir, Force:=True
  137. XRaiseEvent "Done"
  138. End If
  139. Common_Exit:
  140. Exit Function
  141. Error_Handler:
  142. g_XErr.SetInfo "FileImageCreator::Go", strErrMsg
  143. Err.Raise Err.Number, Description:=vbCr & "[Start Nested COM OBject Error Reporting" & vbCr & _
  144. g_XErr.Dump(False) & vbCr & _
  145. " End Nested COM Object Error Reporting]" & vbCr
  146. End Function
  147. 'Function ExpandImage(ByVal strHelpSourceFolder As String, ByVal strExpandedHelpFolder As String) As Boolean
  148. '
  149. ' ExpandImage = False
  150. ' Dim strErrMsg As String: strErrMsg = "": If (g_bOnErrorHandling) Then On Error GoTo Error_Handler
  151. '
  152. ' m_dtStartTime = Now
  153. '
  154. ' XMKDir strExpandedHelpFolder
  155. '
  156. ' ExpandImage = expandchm(strHelpSourceFolder, strExpandedHelpFolder)
  157. '
  158. ' XRaiseEvent "Done"
  159. '
  160. 'Common_Exit:
  161. '
  162. ' Exit Function
  163. '
  164. 'Error_Handler:
  165. ' g_XErr.SetInfo "FileImageCreator::ExpandImage", strErrMsg
  166. ' Err.Raise Err.Number, Description:=vbCr & "[Start Nested COM OBject Error Reporting" & vbCr & _
  167. ' g_XErr.Dump(False) & vbCr & _
  168. ' " End Nested COM Object Error Reporting]" & vbCr
  169. 'End Function
  170. Public Property Get SSDB() As String
  171. Dim strErrMsg As String: strErrMsg = "": If (g_bOnErrorHandling) Then On Error GoTo Error_Handler
  172. SSDB = m_strSSDB
  173. Common_Exit:
  174. Exit Property
  175. Error_Handler:
  176. g_XErr.SetInfo "FileImageCreator::Get SSDB", strErrMsg
  177. Err.Raise Err.Number
  178. End Property
  179. Public Property Let SSDB(ByVal strSSDB As String)
  180. Dim strErrMsg As String: strErrMsg = "": If (g_bOnErrorHandling) Then On Error GoTo Error_Handler
  181. If (Not m_fso.FolderExists(strSSDB)) Then
  182. Err.Raise HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "FileImageCreator::Let SSDB", _
  183. "I could not open " & strSSDB
  184. Else
  185. m_strSSDB = strSSDB
  186. End If
  187. Common_Exit:
  188. Exit Property
  189. Error_Handler:
  190. g_XErr.SetInfo "FileImageCreator::Let SSDB", strErrMsg
  191. Err.Raise Err.Number
  192. End Property
  193. Public Property Get SSUser() As String
  194. SSUser = m_strSSUser
  195. End Property
  196. Public Property Let SSUser(ByVal strSSUser As String)
  197. m_strSSUser = strSSUser
  198. End Property
  199. Public Property Get SSProject() As String
  200. SSProject = m_strSSProject
  201. End Property
  202. Public Property Let SSProject(ByVal strSSProject As String)
  203. m_strSSProject = strSSProject
  204. End Property
  205. Public Property Let LiveImageDir(ByVal strDir As String)
  206. Dim strErrMsg As String: strErrMsg = "": If (g_bOnErrorHandling) Then On Error GoTo Error_Handler
  207. XMKDir strDir
  208. m_strLiveImageDir = strDir
  209. Common_Exit:
  210. Exit Property
  211. Error_Handler:
  212. g_XErr.SetInfo "FileImageCreator::Let LiveImageDir", strErrMsg
  213. Err.Raise Err.Number
  214. End Property
  215. Public Property Get LiveImageDir() As String
  216. LiveImageDir = m_strLiveImageDir
  217. End Property
  218. Public Property Let WorkDir(ByVal strDir As String)
  219. Dim strErrMsg As String: strErrMsg = "": If (g_bOnErrorHandling) Then On Error GoTo Error_Handler
  220. XMKDir strDir
  221. m_strWorkDir = strDir
  222. Common_Exit:
  223. Exit Property
  224. Error_Handler:
  225. g_XErr.SetInfo "FileImageCreator::Let LiveImageDir", strErrMsg
  226. Err.Raise Err.Number
  227. End Property
  228. Public Property Get WorkDir() As String
  229. LiveImageDir = m_strWorkDir
  230. End Property
  231. Public Property Get RenamesFile() As String
  232. RenamesFile = m_strRenamesFile
  233. End Property
  234. Public Property Let RenamesFile(ByVal strFile As String)
  235. strFile = Trim$(strFile)
  236. If (Len(strFile) = 0) Then
  237. Err.Raise E_INVALIDARG, "HSCFileImage.FileImageCreator.Let RenamesFile", "You must supply a non empty file name for the Renames File.", ""
  238. End If
  239. m_strRenamesFile = strFile
  240. End Property
  241. ' ===================== Utility Functions =============================
  242. Private Function expandchm(ByVal strHelpDir As String, ByVal strChmDir As String) As Boolean
  243. expandchm = False
  244. Dim strErrMsg As String: strErrMsg = "": If (g_bOnErrorHandling) Then On Error GoTo Error_Handler
  245. strHelpDir = Trim$(strHelpDir)
  246. If (Len(strHelpDir) = 0) Then
  247. Err.Raise E_INVALIDARG, "HSCFileImage.ExpandChm", "You must supply a non empty file name for Chm Source Directory.", ""
  248. End If
  249. strChmDir = Trim$(strChmDir)
  250. If (Len(strChmDir) = 0) Then
  251. Err.Raise E_INVALIDARG, "HSCFileImage.ExpandChm", "You must supply a non empty file name for expanded Chm Destination Directory.", ""
  252. End If
  253. Static s_bCalcFreeSpace As Boolean
  254. ' Do we have enough Free Space around here??
  255. If (Not s_bCalcFreeSpace) Then
  256. Const MINIMUM_SPACE_FAT16 = 300
  257. Const MINIMUM_SPACE_FAT32 = 60
  258. Dim sngFreeSpace As Single, d As Scripting.Drive
  259. Set d = m_fso.GetDrive(m_fso.GetDriveName(strChmDir))
  260. sngFreeSpace = d.AvailableSpace
  261. If (d.FileSystem = "FAT32" Or d.FileSystem = "NTFS") Then
  262. If (sngFreeSpace < Mbytes2Bytes(MINIMUM_SPACE_FAT32)) Then
  263. sngFreeSpace = MINIMUM_SPACE_FAT32
  264. Err.Raise HRESULT_FROM_WIN32(ERROR_DISK_FULL)
  265. End If
  266. Else
  267. If (sngFreeSpace < Mbytes2Bytes(MINIMUM_SPACE_FAT16)) Then
  268. sngFreeSpace = MINIMUM_SPACE_FAT16
  269. Err.Raise HRESULT_FROM_WIN32(ERROR_DISK_FULL)
  270. End If
  271. End If
  272. s_bCalcFreeSpace = True
  273. End If
  274. Dim strLastStatus As String: strLastStatus = XRaiseEvent("Expanding CHMs")
  275. Dim oDir As Scripting.Folder
  276. Dim strHHCDir As String: strHHCDir = strChmDir + "\hhc"
  277. Dim strHHKDir As String: strHHKDir = strChmDir + "\hhk"
  278. ' First we delete all files in the Working Directory.
  279. If (Not m_bAdditiveImage) Then
  280. XRaiseEvent "Deleting previously Expanded CHMS"
  281. If (m_fso.FolderExists(strChmDir)) Then
  282. m_fso.DeleteFolder strChmDir, Force:=True
  283. End If
  284. End If
  285. XRaiseEvent "Expanding CHMs"
  286. ' Now we recreate the Work Folders
  287. ' I use XMKDIr instead of m_fso.CreateFolder, because XMKDir creates folders recursively
  288. If (Not m_fso.FolderExists(strChmDir)) Then XMKDir strChmDir
  289. If (Not m_fso.FolderExists(strHHCDir)) Then XMKDir strHHCDir
  290. If (Not m_fso.FolderExists(strHHKDir)) Then XMKDir strHHKDir
  291. Set oDir = m_fso.GetFolder(strHelpDir)
  292. Dim File As Scripting.File
  293. ' txtProgress.Visible = True
  294. Dim iFilecount As Integer, iX As Integer
  295. iFilecount = GetChmCount(oDir)
  296. For Each File In oDir.Files
  297. ' If m_BreakFlag Then Exit For
  298. ' File.Type
  299. If ((File.Attributes And Directory) = Directory) Then
  300. expandchm strHelpDir + "\" + File.Name, strChmDir + "\" + File.Name
  301. ElseIf FileExtension(File.Name) = "CHM" Then
  302. Dim strDirThisChm As String
  303. strDirThisChm = strChmDir + "\" + File.Name
  304. If (Not m_fso.FolderExists(strDirThisChm)) Then
  305. m_fso.CreateFolder (strDirThisChm)
  306. End If
  307. Dim strCmd As String
  308. strCmd = "hh -decompile " + strDirThisChm + " " + oDir.Path + "\" + File.Name
  309. iX = iX + 1
  310. XRaiseEvent "Expanding CHMS [" & iX & "/" & iFilecount & "] " & File.Name
  311. ' txtProgress.Text = "Decompiling [" & iX & "/" & iFilecount & "] " & File.Name
  312. ' Shell strCmd, vbHide
  313. m_WsShell.Run strCmd, True, True
  314. DoEvents
  315. CopyFiletoDir strDirThisChm, "HHC", strHHCDir
  316. CopyFiletoDir strDirThisChm, "HHK", strHHKDir
  317. End If
  318. Next
  319. ' txtProgress.Visible = False
  320. expandchm = True
  321. Common_Exit:
  322. XRaiseEvent strLastStatus
  323. Exit Function
  324. Error_Handler:
  325. g_XErr.SetInfo "FileImageCreator::expanchm", strErrMsg
  326. Err.Raise Err.Number
  327. End Function
  328. Private Function GetChmCount(ByVal oDir As Scripting.Folder) As Long
  329. GetChmCount = 0
  330. Dim strErrMsg As String: strErrMsg = "": If (g_bOnErrorHandling) Then On Error GoTo Error_Handler
  331. Dim File As Scripting.File
  332. For Each File In oDir.Files
  333. If ((File.Attributes And Directory) = Directory) Then
  334. GetChmCount = GetChmCount + GetChmCount(m_fso.GetFolder(File.Path))
  335. Else
  336. If FileExtension(File.Name) = "CHM" Then
  337. GetChmCount = GetChmCount + 1
  338. End If
  339. End If
  340. Next
  341. Common_Exit:
  342. Exit Function
  343. Error_Handler:
  344. GetChmCount = -1
  345. g_XErr.SetInfo "FileImageCreator::GetChmCount", strErrMsg
  346. Err.Raise Err.Number
  347. End Function
  348. Private Function Mbytes2Bytes(dblMbytesIn As Double) As Double
  349. Mbytes2Bytes = dblMbytesIn * 1024 * 1024
  350. End Function
  351. Private Sub CopyFiletoDir(ByVal strDirThisChm As String, ByVal strExt As String, ByVal strDir As String)
  352. Dim strErrMsg As String: strErrMsg = "": If (g_bOnErrorHandling) Then On Error GoTo Error_Handler
  353. Dim oDir As Scripting.Folder
  354. Set oDir = m_fso.GetFolder(strDirThisChm)
  355. Dim oFile As Scripting.File
  356. For Each oFile In oDir.Files
  357. If (FileExtension(oFile.Name) = strExt) Then
  358. FileCopy oFile.Path, strDir + "\" + oFile.Name
  359. End If
  360. Next
  361. Common_Exit:
  362. Exit Sub
  363. Error_Handler:
  364. g_XErr.SetInfo "FileImageCreator::CopyFiletoDir", strErrMsg
  365. Err.Raise Err.Number
  366. End Sub
  367. Function XRaiseEvent(ByVal strMsg As String) As String
  368. m_strStatusMsg = strMsg
  369. RaiseEvent GoStatus("[" & Format(Now - m_dtStartTime, "nn:ss") & "] " & strMsg, m_bCancel)
  370. DoEvents
  371. XRaiseEvent = m_strStatusMsg
  372. End Function
  373. ' This Subroutine simply creates a Chain of Sub-Directorys.
  374. Sub XMKDir(ByVal strPath As String)
  375. strPath = Trim$(strPath)
  376. If (Len(strPath) = 0) Then
  377. Err.Raise E_INVALIDARG, "XMKDir", "I Cannot Process an Empty FilePath", ""
  378. End If
  379. Dim fso As Scripting.FileSystemObject: Set fso = New Scripting.FileSystemObject
  380. If (fso.FolderExists(strPath)) Then GoTo Common_Exit
  381. Dim aStackNames() As String, iX As Integer: iX = 0
  382. Do While (Not fso.FolderExists(strPath))
  383. ReDim Preserve aStackNames(iX)
  384. aStackNames(iX) = Basename(strPath)
  385. strPath = Dirname(strPath)
  386. iX = iX + 1
  387. Loop
  388. If (strPath = "\") Then
  389. ' we received a request to create a UNC Server!!!
  390. ' or a UNC Share. In either case it is an invalid Argument
  391. Err.Raise E_INVALIDARG, "XMKDir", "Cannot Create a UNC Server or Share", ""
  392. End If
  393. For iX = iX - 1 To 0 Step -1
  394. strPath = strPath + "\" + aStackNames(iX)
  395. fso.CreateFolder strPath
  396. Next iX
  397. Common_Exit:
  398. Exit Sub
  399. End Sub
  400. Private Sub ResetAll2RW(ByVal strPath As String)
  401. Dim strErrMsg As String: strErrMsg = "": If (g_bOnErrorHandling) Then On Error GoTo Error_Handler
  402. Dim strLastStatus As String: strLastStatus = XRaiseEvent("Resetting All attributes to Read/Write")
  403. strPath = Trim$(strPath)
  404. If (Len(strPath) = 0) Then
  405. Err.Raise E_INVALIDARG, "ResetAll2RW", "I Cannot Process an Empty FilePath", ""
  406. End If
  407. Dim oDir As Scripting.Folder
  408. Set oDir = m_fso.GetFolder(strPath)
  409. Dim File As Scripting.File
  410. For Each File In oDir.Files
  411. If ((File.Attributes And Directory) = Directory) Then
  412. ResetAll2RW strPath + "\" + File.Name
  413. Else
  414. File.Attributes = (File.Attributes And (Not ReadOnly))
  415. End If
  416. Next
  417. Common_Exit:
  418. XRaiseEvent strLastStatus
  419. Exit Sub
  420. Error_Handler:
  421. g_XErr.SetInfo "FileImageCreator::ResetAll2RW", strErrMsg
  422. Err.Raise Err.Number
  423. End Sub
  424. Public Property Get ExpandChmOnly() As Boolean
  425. ExpandChmOnly = m_bExpandChmOnly
  426. End Property
  427. Public Property Let ExpandChmOnly(ByVal bExpandChmOnly As Boolean)
  428. m_bExpandChmOnly = bExpandChmOnly
  429. End Property