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.

450 lines
8.4 KiB

  1. #include "stdinc.h"
  2. #include <fcntl.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <io.h>
  6. #include <stdio.h>
  7. int get_percentage(unsigned long a, unsigned long b);
  8. /*
  9. In a merge module, there can only be one CAB file, and its name must be 'MergeModule.CABinet'
  10. Every call to this function must fail if iCab!=1
  11. */
  12. #define CABINET_NUMBER 1
  13. /*
  14. * When a CAB file reaches this size, a new CAB will be created
  15. * automatically. This is useful for fitting CAB files onto disks.
  16. *
  17. * If you want to create just one huge CAB file with everything in
  18. * it, change this to a very very large number.
  19. */
  20. #define MEDIA_SIZE (LONG_MAX)
  21. /*
  22. * When a folder has this much compressed data inside it,
  23. * automatically flush the folder.
  24. *
  25. * Flushing the folder hurts compression a little bit, but
  26. * helps random access significantly.
  27. */
  28. #define FOLDER_THRESHOLD (LONG_MAX)
  29. /*
  30. * Compression type to use
  31. */
  32. #define COMPRESSION_TYPE tcompTYPE_MSZIP
  33. /*
  34. * Our internal state
  35. *
  36. * The FCI APIs allow us to pass back a state pointer of our own
  37. */
  38. typedef struct
  39. {
  40. ULONG total_compressed_size; /* total compressed size so far */
  41. ULONG total_uncompressed_size; /* total uncompressed size so far */
  42. } client_state;
  43. //
  44. // helper functions for FCI
  45. //
  46. /*
  47. * Memory allocation function
  48. */
  49. FNFCIALLOC(fci_mem_alloc)
  50. {
  51. return malloc(cb);
  52. }
  53. /*
  54. * Memory free function
  55. */
  56. FNFCIFREE(fci_mem_free)
  57. {
  58. free(memory);
  59. }
  60. /*
  61. * File i/o functions
  62. */
  63. FNFCIOPEN(fci_open)
  64. {
  65. int result;
  66. result = _open(pszFile, oflag, pmode);
  67. if (result == -1)
  68. *err = errno;
  69. return result;
  70. }
  71. FNFCIREAD(fci_read)
  72. {
  73. unsigned int result;
  74. result = (unsigned int)_read((int)hf, memory, cb);
  75. if (result != cb)
  76. *err = errno;
  77. return result;
  78. }
  79. FNFCIWRITE(fci_write)
  80. {
  81. unsigned int result;
  82. result = (unsigned int) _write((int)hf, memory, (INT)cb);
  83. if (result != cb)
  84. *err = errno;
  85. return result;
  86. }
  87. FNFCICLOSE(fci_close)
  88. {
  89. int result;
  90. result = _close((int)hf);
  91. if (result != 0)
  92. *err = errno;
  93. return result;
  94. }
  95. FNFCISEEK(fci_seek)
  96. {
  97. long result;
  98. result = _lseek((int)hf, dist, seektype);
  99. if (result == -1)
  100. *err = errno;
  101. return result;
  102. }
  103. FNFCIDELETE(fci_delete)
  104. {
  105. int result;
  106. result = remove(pszFile);
  107. if (result != 0)
  108. *err = errno;
  109. return result;
  110. }
  111. /*
  112. * File placed function called when a file has been committed
  113. * to a cabinet
  114. */
  115. FNFCIFILEPLACED(file_placed)
  116. {
  117. return 0;
  118. }
  119. /*
  120. * Function to obtain temporary files
  121. */
  122. FNFCIGETTEMPFILE(get_temp_file)
  123. {
  124. char *psz;
  125. psz = _tempnam("","xx"); // Get a name
  126. if ((psz != NULL) && (strlen(psz) < (unsigned)cbTempName)) {
  127. strcpy(pszTempName,psz); // Copy to caller's buffer
  128. free(psz); // Free temporary name buffer
  129. return TRUE; // Success
  130. }
  131. //** Failed
  132. if (psz) {
  133. free(psz);
  134. }
  135. return FALSE;
  136. }
  137. /*
  138. * Progress function
  139. */
  140. FNFCISTATUS(progress)
  141. {
  142. client_state *cs;
  143. cs = (client_state *) pv;
  144. if (typeStatus == statusFile)
  145. {
  146. /*
  147. cs->total_compressed_size += cb1;
  148. cs->total_uncompressed_size += cb2;
  149. */
  150. /*
  151. * Compressing a block into a folder
  152. *
  153. * cb2 = uncompressed size of block
  154. */
  155. }
  156. else if (typeStatus == statusFolder)
  157. {
  158. int percentage;
  159. /*
  160. * Adding a folder to a cabinet
  161. *
  162. * cb1 = amount of folder copied to cabinet so far
  163. * cb2 = total size of folder
  164. */
  165. percentage = get_percentage(cb1, cb2);
  166. }
  167. return 0;
  168. }
  169. FNFCIGETNEXTCABINET(get_next_cabinet)
  170. {
  171. if (pccab->iCab != CABINET_NUMBER)
  172. {
  173. return -1;
  174. }
  175. /*
  176. * Cabinet counter has been incremented already by FCI
  177. */
  178. /*
  179. * Store next cabinet name
  180. */
  181. WideCharToMultiByte(
  182. CP_ACP, 0, MERGEMODULE_CABINET_FILENAME, NUMBER_OF(MERGEMODULE_CABINET_FILENAME) -1 ,
  183. pccab->szCab, sizeof(pccab->szCab), NULL, NULL);
  184. /*
  185. * You could change the disk name here too, if you wanted
  186. */
  187. return TRUE;
  188. }
  189. FNFCIGETOPENINFO(get_open_info)
  190. {
  191. BY_HANDLE_FILE_INFORMATION finfo;
  192. FILETIME filetime;
  193. HANDLE handle = INVALID_HANDLE_VALUE;
  194. DWORD attrs;
  195. INT_PTR hf;
  196. /*
  197. * Need a Win32 type handle to get file date/time
  198. * using the Win32 APIs, even though the handle we
  199. * will be returning is of the type compatible with
  200. * _open
  201. */
  202. handle = CreateFileA(
  203. pszName,
  204. GENERIC_READ,
  205. FILE_SHARE_READ,
  206. NULL,
  207. OPEN_EXISTING,
  208. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
  209. NULL
  210. );
  211. if (handle == INVALID_HANDLE_VALUE)
  212. {
  213. return -1;
  214. }
  215. if (GetFileInformationByHandle(handle, &finfo) == FALSE)
  216. {
  217. CloseHandle(handle);
  218. return -1;
  219. }
  220. FileTimeToLocalFileTime(
  221. &finfo.ftLastWriteTime,
  222. &filetime
  223. );
  224. FileTimeToDosDateTime(
  225. &filetime,
  226. pdate,
  227. ptime
  228. );
  229. attrs = GetFileAttributesA(pszName);
  230. if (attrs == 0xFFFFFFFF)
  231. {
  232. /* failure */
  233. *pattribs = 0;
  234. }
  235. else
  236. {
  237. /*
  238. * Mask out all other bits except these four, since other
  239. * bits are used by the cabinet format to indicate a
  240. * special meaning.
  241. */
  242. *pattribs = (USHORT) (attrs & (_A_RDONLY | _A_SYSTEM | _A_HIDDEN | _A_ARCH));
  243. }
  244. CloseHandle(handle);
  245. /*
  246. * Return handle using _open
  247. */
  248. hf = _open( pszName, _O_RDONLY | _O_BINARY );
  249. if (hf == -1)
  250. return -1; // abort on error
  251. return hf;
  252. }
  253. void set_cab_parameters(PCCAB cab_parms)
  254. {
  255. memset(cab_parms, 0, sizeof(CCAB));
  256. cab_parms->cb = MEDIA_SIZE;
  257. cab_parms->cbFolderThresh = FOLDER_THRESHOLD;
  258. /*
  259. * Don't reserve space for any extensions
  260. */
  261. cab_parms->cbReserveCFHeader = 0;
  262. cab_parms->cbReserveCFFolder = 0;
  263. cab_parms->cbReserveCFData = 0;
  264. /*
  265. * We use this to create the cabinet name
  266. */
  267. cab_parms->iCab = CABINET_NUMBER;
  268. /*
  269. * If you want to use disk names, use this to
  270. * count disks
  271. */
  272. cab_parms->iDisk = 0;
  273. /*
  274. * Choose your own number
  275. */
  276. cab_parms->setID = 1965;
  277. /*
  278. * Only important if CABs are spanning multiple
  279. * disks, in which case you will want to use a
  280. * real disk name.
  281. *
  282. * Can be left as an empty string.
  283. */
  284. strcpy(cab_parms->szDisk, "win32.fusion.tools");
  285. /* where to store the created CAB files */
  286. CSmallStringBuffer buf;
  287. if (! buf.Win32Assign(g_MsmInfo.m_sbCabinet))
  288. {
  289. fprintf(stderr, "error happened in set_cab_parameters");
  290. goto Exit; // void function
  291. }
  292. if (!buf.Win32RemoveLastPathElement())
  293. {
  294. goto Exit;
  295. }
  296. if ( ! buf.Win32EnsureTrailingPathSeparator())
  297. {
  298. fprintf(stderr, "error happened in set_cab_parameters");
  299. goto Exit; // void function
  300. }
  301. WideCharToMultiByte(
  302. CP_ACP, 0, buf, buf.GetCchAsDWORD(),
  303. cab_parms->szCabPath, sizeof(cab_parms->szCabPath), NULL, NULL);
  304. /* store name of first CAB file */
  305. WideCharToMultiByte(
  306. CP_ACP, 0, MERGEMODULE_CABINET_FILENAME, NUMBER_OF(MERGEMODULE_CABINET_FILENAME) -1 ,
  307. cab_parms->szCab, sizeof(cab_parms->szCab), NULL, NULL);
  308. Exit:
  309. return;
  310. }
  311. int get_percentage(unsigned long a, unsigned long b)
  312. {
  313. while (a > 10000000)
  314. {
  315. a >>= 3;
  316. b >>= 3;
  317. }
  318. if (b == 0)
  319. return 0;
  320. return ((a*100)/b);
  321. }
  322. char *return_fci_error_string(int err)
  323. {
  324. switch (err)
  325. {
  326. case FCIERR_NONE:
  327. return "No error";
  328. case FCIERR_OPEN_SRC:
  329. return "Failure opening file to be stored in cabinet";
  330. case FCIERR_READ_SRC:
  331. return "Failure reading file to be stored in cabinet";
  332. case FCIERR_ALLOC_FAIL:
  333. return "Insufficient memory in FCI";
  334. case FCIERR_TEMP_FILE:
  335. return "Could not create a temporary file";
  336. case FCIERR_BAD_COMPR_TYPE:
  337. return "Unknown compression type";
  338. case FCIERR_CAB_FILE:
  339. return "Could not create cabinet file";
  340. case FCIERR_USER_ABORT:
  341. return "Client requested abort";
  342. case FCIERR_MCI_FAIL:
  343. return "Failure compressing data";
  344. default:
  345. return "Unknown error";
  346. }
  347. }