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.

485 lines
9.7 KiB

  1. /* File: D:\WACKER\tdll\file_io.c (Created: 26-Jan-1994)
  2. *
  3. * Copyright 1994,1994 by Hilgraeve Inc. -- Monroe, MI
  4. * All rights reserved
  5. *
  6. * $Revision: 5 $
  7. * $Date: 3/15/02 12:19p $
  8. */
  9. #include <windows.h>
  10. #pragma hdrstop
  11. // #define DEBUGSTR 1
  12. #include "stdtyp.h"
  13. #include "mc.h"
  14. #include <tdll\assert.h>
  15. #include "file_io.h"
  16. #include "file_msc.h"
  17. /*
  18. * This stuff is a replacement for some sort of buffered file I/O.
  19. *
  20. * It is directly modeled after (read lifted from) the "stdio.h" stuff.
  21. */
  22. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  23. * FUNCTION:
  24. * _fio_fill_buf
  25. *
  26. * DESCRIPTION:
  27. * This is an "internal" function called by the "fio_getc" macro. It is a
  28. * replacement for the "_filbuf" function in "stdio".
  29. *
  30. * PARAMETERS:
  31. * pF -- a pointer to a file structure.
  32. *
  33. * RETURNS:
  34. * The next character available or an EOF.
  35. */
  36. int _fio_fill_buf(ST_IOBUF *pF)
  37. {
  38. DWORD dwSize;
  39. int iRet;
  40. assert(pF);
  41. assert(pF->_fio_magic == _FIO_MAGIC);
  42. if (pF->_fio_flag != 0)
  43. return EOF;
  44. if (pF->_fio_base == NULL)
  45. {
  46. pF->_fio_base = malloc(pF->_fio_bufsiz);
  47. if (pF->_fio_base == NULL)
  48. {
  49. pF->_fio_flag |= _FIO_IOERR;
  50. return EOF;
  51. }
  52. }
  53. pF->_fio_ptr = pF->_fio_base;
  54. dwSize = 0;
  55. DbgOutStr("fio_fill_buf reads %d bytes", pF->_fio_bufsiz, 0,0,0,0);
  56. iRet = ReadFile(pF->_fio_handle,
  57. pF->_fio_ptr,
  58. pF->_fio_bufsiz,
  59. &dwSize,
  60. NULL);
  61. DbgOutStr("...done\r\n", 0,0,0,0,0);
  62. if (dwSize == 0 || iRet == FALSE)
  63. {
  64. pF->_fio_flag |= _FIO_IOEOF;
  65. return EOF;
  66. }
  67. pF->_fio_cnt = dwSize;
  68. return (fio_getc(pF));
  69. }
  70. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  71. * FUNCTION:
  72. * _fio_flush_buf
  73. *
  74. * DESCRIPTION:
  75. * This is an "internal" function called by the "fio_putc" macro. It is a
  76. * replacement for the "_flsbuf" function in "stdio".
  77. *
  78. * PARAMETERS:
  79. * c -- the next character to be written out
  80. * pF -- a pointer to a file structure
  81. *
  82. * RETURNS:
  83. * The character buffered of an EOF.
  84. */
  85. int _fio_flush_buf(int c, ST_IOBUF *pF)
  86. {
  87. int size;
  88. DWORD dwFoo;
  89. assert(pF);
  90. assert(pF->_fio_magic == _FIO_MAGIC);
  91. if (pF->_fio_flag != 0)
  92. return EOF;
  93. if (pF->_fio_base == NULL)
  94. {
  95. pF->_fio_base = malloc(pF->_fio_bufsiz);
  96. if (pF->_fio_base == NULL)
  97. {
  98. pF->_fio_flag |= _FIO_IOERR;
  99. return EOF;
  100. }
  101. }
  102. else
  103. {
  104. /* We have been here before, dump the buffer */
  105. size = (int)(pF->_fio_ptr - pF->_fio_base);
  106. if (size > 0)
  107. {
  108. DbgOutStr("fio_putc writes %d bytes", size, 0,0,0,0);
  109. WriteFile(pF->_fio_handle,
  110. pF->_fio_base,
  111. size,
  112. &dwFoo,
  113. NULL);
  114. DbgOutStr("...done\r\n", 0,0,0,0,0);
  115. }
  116. }
  117. pF->_fio_ptr = pF->_fio_base;
  118. pF->_fio_cnt = pF->_fio_bufsiz;
  119. return fio_putc(c, pF);
  120. }
  121. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  122. * FUNCTION:
  123. * fio_open
  124. *
  125. * DESCRIPTION:
  126. * This function creates a file structure handle and initializes the handle.
  127. *
  128. * PARAMETERS:
  129. * fname -- a pointer to the file name
  130. * mode -- flags, see file_io.h
  131. *
  132. * RETURNS:
  133. * A pointer to an initialize structure or a NULL.
  134. */
  135. ST_IOBUF *fio_open(char *fname, int mode)
  136. {
  137. int nFileExists;
  138. DWORD dwMode;
  139. DWORD dwShare;
  140. DWORD dwCreate;
  141. ST_IOBUF *pF;
  142. nFileExists = GetFileSizeFromName(fname, NULL);
  143. dwMode = 0;
  144. if ((mode & FIO_READ) != 0)
  145. dwMode |= GENERIC_READ;
  146. if ((mode & FIO_WRITE) != 0)
  147. dwMode |= GENERIC_WRITE;
  148. if (dwMode == 0)
  149. return NULL;
  150. dwShare = FILE_SHARE_READ;
  151. if ((mode & FIO_WRITE) == 0)
  152. dwShare |= FILE_SHARE_WRITE;
  153. dwCreate = 0;
  154. if ((mode & FIO_CREATE) == 0)
  155. {
  156. /* Don't wack the file here */
  157. if (nFileExists)
  158. {
  159. dwCreate = OPEN_EXISTING;
  160. }
  161. else
  162. {
  163. dwCreate = CREATE_NEW;
  164. }
  165. }
  166. else
  167. {
  168. /* FIO_CREATE means always wack the file */
  169. if (nFileExists)
  170. {
  171. if ((mode & FIO_WRITE) == 0)
  172. {
  173. dwCreate = OPEN_EXISTING;
  174. }
  175. else
  176. {
  177. dwCreate = TRUNCATE_EXISTING;
  178. }
  179. }
  180. else
  181. {
  182. dwCreate = CREATE_NEW;
  183. }
  184. }
  185. pF = (ST_IOBUF *)malloc(sizeof(ST_IOBUF));
  186. if (pF != (ST_IOBUF *)0)
  187. {
  188. pF->_fio_magic = 0;
  189. pF->_fio_ptr = NULL;
  190. pF->_fio_cnt = 0;
  191. pF->_fio_base = NULL;
  192. pF->_fio_flag = 0;
  193. pF->_file = 0;
  194. pF->_fio_handle = 0;
  195. pF->_fio_mode = mode;
  196. pF->_fio_charbuf = 0;
  197. pF->_fio_bufsiz = _FIO_BSIZE;
  198. pF->_fio_tmpfname = NULL;
  199. /*
  200. * Try and open the file
  201. */
  202. pF->_fio_handle = CreateFile(fname,
  203. dwMode,
  204. dwShare,
  205. NULL,
  206. dwCreate,
  207. 0,
  208. NULL);
  209. if (pF->_fio_handle == INVALID_HANDLE_VALUE)
  210. {
  211. free(pF);
  212. pF = (ST_IOBUF *)0;
  213. }
  214. }
  215. if (pF)
  216. {
  217. if ((mode & FIO_APPEND) != 0)
  218. {
  219. SetFilePointer(pF->_fio_handle,
  220. 0,
  221. NULL,
  222. FILE_END);
  223. }
  224. }
  225. if (pF)
  226. {
  227. /* Mark as a valid structure */
  228. pF->_fio_magic = _FIO_MAGIC;
  229. }
  230. return pF;
  231. }
  232. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  233. * FUNCTION:
  234. * fio_close
  235. *
  236. * DESCRIPTION:
  237. * This function flushes whatever data needs to be flushed and closes stuff
  238. * up.
  239. *
  240. * PARAMETERS:
  241. * pF -- a pointer to a file structure
  242. *
  243. * RETURNS:
  244. * ZERO if everything is OK, otherwise an EOF.
  245. */
  246. int fio_close(ST_IOBUF *pF)
  247. {
  248. int size;
  249. DWORD dwFoo;
  250. assert(pF);
  251. assert(pF->_fio_magic == _FIO_MAGIC);
  252. if (pF)
  253. {
  254. /*
  255. * Make sure any data is written out
  256. */
  257. if ((pF->_fio_mode & FIO_WRITE) != 0)
  258. {
  259. if (pF->_fio_ptr != NULL)
  260. {
  261. size = (int)(pF->_fio_ptr - pF->_fio_base);
  262. if (size > 0)
  263. {
  264. DbgOutStr("fio_close writes %d bytes", size, 0,0,0,0);
  265. WriteFile(pF->_fio_handle,
  266. pF->_fio_base,
  267. size,
  268. &dwFoo,
  269. NULL);
  270. DbgOutStr("...done\r\n", 0,0,0,0,0);
  271. }
  272. }
  273. }
  274. CloseHandle(pF->_fio_handle);
  275. pF->_fio_handle = INVALID_HANDLE_VALUE;
  276. free(pF);
  277. pF = NULL;
  278. }
  279. return 0;
  280. }
  281. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  282. * FUNCTION:
  283. * fio_seek
  284. *
  285. * DESCRIPTION:
  286. * This function is a replacement for the fseek function.
  287. *
  288. * PARAMETERS:
  289. * pF -- a pointer to a file structure
  290. * position -- where to move the file pointer to
  291. * mode -- starting address of the move
  292. *
  293. * RETURNS:
  294. * ZERO if everything is OK, otherwise an EOF.
  295. */
  296. int fio_seek(ST_IOBUF *pF, size_t position, int mode)
  297. {
  298. DWORD dwMethod;
  299. int size;
  300. assert(pF);
  301. assert(pF->_fio_magic == _FIO_MAGIC);
  302. switch (mode)
  303. {
  304. default:
  305. return EOF;
  306. case FIO_SEEK_CUR:
  307. dwMethod = FILE_CURRENT;
  308. break;
  309. case FIO_SEEK_END:
  310. dwMethod = FILE_END;
  311. break;
  312. case FIO_SEEK_SET:
  313. dwMethod = FILE_BEGIN;
  314. break;
  315. }
  316. if (pF)
  317. {
  318. /*
  319. * Make sure any data is written out
  320. */
  321. if ((pF->_fio_mode & FIO_WRITE) != 0)
  322. {
  323. if (pF->_fio_ptr != NULL)
  324. {
  325. size = (int)(pF->_fio_ptr - pF->_fio_base);
  326. if (size > 0)
  327. {
  328. DbgOutStr("fio_seek writes %d bytes", size, 0,0,0,0);
  329. WriteFile(pF->_fio_handle,
  330. pF->_fio_base,
  331. size,
  332. NULL,
  333. NULL);
  334. DbgOutStr("...done\r\n", 0,0,0,0,0);
  335. }
  336. }
  337. }
  338. pF->_fio_cnt = 0;
  339. SetFilePointer(pF->_fio_handle,
  340. position,
  341. NULL,
  342. dwMethod);
  343. }
  344. return 0;
  345. }
  346. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  347. * FUNCTION:
  348. * fio_read
  349. *
  350. * DESCRIPTION:
  351. * This function is a replacement for the "fread" function in "stdio".
  352. *
  353. * PARAMETERS:
  354. * buffer -- address of the data to read
  355. * size -- the size of each item (object?) to read
  356. * count -- the number of items to read
  357. * pF -- a pointer to a file structure
  358. *
  359. * RETURNS:
  360. * The number of items read from the file, ZERO indicating EOF.
  361. */
  362. int fio_read(void *buffer, size_t size, size_t count, ST_IOBUF *pF)
  363. {
  364. DWORD dwSize;
  365. DWORD dwGot;
  366. int iRet;
  367. assert(pF);
  368. assert(pF->_fio_magic == _FIO_MAGIC);
  369. /* For now, don't allow intermix of buffered and non_buffered */
  370. assert(pF->_fio_base == NULL);
  371. if (pF)
  372. {
  373. dwSize = (DWORD)(size * count);
  374. dwGot = 0;
  375. DbgOutStr("fio_read reads %d bytes", dwSize, 0,0,0,0);
  376. iRet = ReadFile(pF->_fio_handle,
  377. buffer,
  378. dwSize,
  379. &dwGot,
  380. NULL);
  381. DbgOutStr("...done\r\n", 0,0,0,0,0);
  382. if (dwGot == 0 || iRet == FALSE)
  383. {
  384. pF->_fio_flag |= _FIO_IOEOF;
  385. }
  386. return dwGot / size;
  387. }
  388. return 0;
  389. }
  390. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  391. * FUNCTION:
  392. * fio_write
  393. *
  394. * DESCRIPTION:
  395. * This function is a replacement for the "fwrite" function in "stdio".
  396. *
  397. * PARAMETERS:
  398. * buffer -- address of the data to write
  399. * size -- the size of each item (object?) to write
  400. * count -- the number of items to write
  401. * pF -- a pointer to a file structure
  402. *
  403. * RETURNS:
  404. * The number of items written to the file.
  405. */
  406. int fio_write(void *buffer, size_t size, size_t count, ST_IOBUF *pF)
  407. {
  408. DWORD dwSize;
  409. DWORD dwPut;
  410. assert(pF);
  411. assert(pF->_fio_magic == _FIO_MAGIC);
  412. /* For now, don't allow intermix of buffered and non_buffered */
  413. assert(pF->_fio_base == NULL);
  414. if (pF)
  415. {
  416. dwSize = (DWORD)(size * count);
  417. dwPut = 0;
  418. DbgOutStr("fio_write writes %d bytes", dwSize, 0,0,0,0);
  419. WriteFile(pF->_fio_handle,
  420. buffer,
  421. dwSize,
  422. &dwPut,
  423. NULL);
  424. DbgOutStr("...done\r\n", 0,0,0,0,0);
  425. if (dwPut == 0)
  426. {
  427. pF->_fio_flag |= _FIO_IOEOF;
  428. }
  429. return dwPut / size;
  430. }
  431. return 0;
  432. }
  433. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  434. * FUNCTION:
  435. *
  436. * DESCRIPTION:
  437. *
  438. * PARAMETERS:
  439. *
  440. * RETURNS:
  441. */