Windows NT 4.0 source code leak
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.

230 lines
4.7 KiB

4 years ago
  1. /***************************************************************************\
  2. *
  3. * FSREAD.C
  4. *
  5. * Copyright (C) Microsoft Corporation 1990.
  6. * All Rights reserved.
  7. *
  8. *****************************************************************************
  9. *
  10. * Program Description: File System Manager functions for read and seek
  11. *
  12. *****************************************************************************
  13. *
  14. * Revision History: Created 03/12/90 by JohnSc
  15. *
  16. *
  17. *****************************************************************************
  18. *
  19. * Known Bugs: None
  20. *
  21. \***************************************************************************/
  22. #include "stdafx.h"
  23. #include "fspriv.h"
  24. #ifdef _DEBUG
  25. #undef THIS_FILE
  26. static char THIS_FILE[] = __FILE__;
  27. #endif
  28. /***************************************************************************\
  29. *
  30. * Function: FPlungeQfshr( qfshr )
  31. *
  32. * Purpose: Get back a qfshr->fid that was flushed
  33. *
  34. * ASSUMES
  35. *
  36. * args IN: qfshr - fid need not be valid
  37. *
  38. * PROMISES
  39. *
  40. * returns: fTruth of success
  41. *
  42. * args OUT: qfshr->fid is valid (or we return FALSE)
  43. *
  44. * globals OUT: rcFSError
  45. *
  46. \***************************************************************************/
  47. #include <io.h> // for _filelength
  48. BOOL STDCALL FPlungeQfshr(QFSHR qfshr)
  49. {
  50. if (qfshr->fid == HFILE_ERROR) {
  51. qfshr->fid = FidOpenFm((qfshr->fm),
  52. qfshr->fsh.bFlags & FS_OPEN_READ_ONLY ?
  53. OF_READ : OF_READWRITE);
  54. if (qfshr->fid == HFILE_ERROR) {
  55. SetFSErrorRc(RcGetIOError());
  56. return FALSE;
  57. }
  58. /*
  59. * Check size of file, then reset file pointer. Certain 0-length
  60. * files (eg con) give us no end of grief if we try to read from
  61. * them, and since a 0-length file could not possibly be a valid FS,
  62. * we reject the notion.
  63. */
  64. if (_filelength(qfshr->fid) < sizeof(FSH)) {
  65. SetFSErrorRc(RC_Invalid);
  66. return FALSE;
  67. }
  68. }
  69. SetFSErrorRc(RC_Success);
  70. return TRUE;
  71. }
  72. /***************************************************************************\
  73. *
  74. * Function: LcbReadHf()
  75. *
  76. * Purpose: read bytes from a file in a file system
  77. *
  78. * ASSUMES
  79. *
  80. * args IN: hf - file
  81. * lcb - number of bytes to read
  82. *
  83. * PROMISES
  84. *
  85. * returns: number of bytes actually read; -1 on error
  86. *
  87. * args OUT: qb - data read from file goes here (must be big enough)
  88. *
  89. * Notes: These are signed longs we're dealing with. This means
  90. * behaviour is different from read() when < 0.
  91. *
  92. \***************************************************************************/
  93. int STDCALL LcbReadHf(HF hf, LPVOID qb, int lcb)
  94. {
  95. int lcbTotalRead;
  96. FID fid;
  97. int lifOffset;
  98. ASSERT(hf);
  99. QRWFO qrwfo = (QRWFO) hf;
  100. SetFSErrorRc(RC_Success);
  101. if (lcb < 0) {
  102. SetFSErrorRc(RC_BadArg);
  103. return (int) -1;
  104. }
  105. if (qrwfo->lifCurrent + lcb > qrwfo->lcbFile) {
  106. lcb = qrwfo->lcbFile - qrwfo->lifCurrent;
  107. if (lcb <= 0) {
  108. return 0;
  109. }
  110. }
  111. // position file pointer for read
  112. if (qrwfo->bFlags & FS_DIRTY) {
  113. fid = USE_CTMPFILE;
  114. lifOffset = 0;
  115. }
  116. else {
  117. QFSHR qfshr = (QFSHR) qrwfo->hfs;
  118. if (!FPlungeQfshr(qfshr))
  119. return (int) -1;
  120. fid = qfshr->fid;
  121. lifOffset = qrwfo->lifBase;
  122. }
  123. if (fid == USE_CTMPFILE) {
  124. qrwfo->pTmpFile->seek(lifOffset + sizeof(FH) + qrwfo->lifCurrent,
  125. SEEK_SET);
  126. lcbTotalRead = qrwfo->pTmpFile->read(qb, lcb);
  127. }
  128. else {
  129. if (LSeekFid(fid, lifOffset + sizeof(FH) + qrwfo->lifCurrent, SEEK_SET)
  130. != lifOffset + (int) sizeof(FH) + qrwfo->lifCurrent) {
  131. ForceFSError();
  132. return HFILE_ERROR;
  133. }
  134. // read the data
  135. lcbTotalRead = LcbReadFid(fid, qb, lcb);
  136. SetFSErrorRc(RcGetIOError());
  137. }
  138. // update file pointer
  139. if (lcbTotalRead >= 0)
  140. qrwfo->lifCurrent += lcbTotalRead;
  141. return lcbTotalRead;
  142. }
  143. /***************************************************************************\
  144. *
  145. * Function: LSeekHf( hf, lOffset, wOrigin )
  146. *
  147. * Purpose: set current file pointer
  148. *
  149. * ASSUMES
  150. *
  151. * args IN: hf - file
  152. * lOffset - offset from origin
  153. * wOrigin - origin (SEEK_SET, SEEK_CUR, or SEEK_END)
  154. *
  155. * PROMISES
  156. *
  157. * returns: new position offset in bytes from beginning of file
  158. * if successful, or -1L if not
  159. *
  160. * state OUT: File pointer is set to new position unless error occurs,
  161. * in which case it stays where it was.
  162. *
  163. \***************************************************************************/
  164. int STDCALL LSeekHf(HF hf, int lOffset, WORD wOrigin)
  165. {
  166. int lif;
  167. ASSERT(hf);
  168. QRWFO qrwfo = (QRWFO) hf;
  169. switch (wOrigin) {
  170. case SEEK_SET:
  171. lif = lOffset;
  172. break;
  173. case SEEK_CUR:
  174. lif = qrwfo->lifCurrent + lOffset;
  175. break;
  176. case SEEK_END:
  177. lif = qrwfo->lcbFile + lOffset;
  178. break;
  179. default:
  180. lif = (int)-1;
  181. break;
  182. }
  183. if (lif >= 0) {
  184. // REVIEW: should we update qrwfo->pTmpFile.FilePtr ??
  185. qrwfo->lifCurrent = lif;
  186. SetFSErrorRc(RC_Success);
  187. }
  188. else {
  189. lif = (int) -1;
  190. SetFSErrorRc(RC_Invalid);
  191. }
  192. return lif;
  193. }