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.

274 lines
7.0 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995.
  5. //
  6. // File: FILELB.CXX
  7. //
  8. // Contents:
  9. //
  10. // Classes: Implements the file-based ILockBytes class.
  11. //
  12. // Functions:
  13. //
  14. // History: 12-13-95 JoeS (Joe Souza) Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #include <urlint.h>
  18. #include <urlmon.hxx>
  19. #include "clockbyt.hxx"
  20. #include "filelb.hxx"
  21. FileLockBytes::FileLockBytes(HANDLE filehandle) : _CRefs()
  22. {
  23. _hFileHandle = filehandle;
  24. }
  25. STDMETHODIMP_(ULONG) FileLockBytes::Release(void)
  26. {
  27. VDATETHIS(this);
  28. UrlMkDebugOut((DEB_ILOCKBYTES, "%p IN FileLockBytes::Release\n", this));
  29. UrlMkAssert((_CRefs > 0));
  30. LONG lRet = --_CRefs;
  31. if (_CRefs == 0)
  32. {
  33. if (_hFileHandle != INVALID_HANDLE_VALUE)
  34. CloseHandle(_hFileHandle);
  35. delete this;
  36. }
  37. UrlMkDebugOut((DEB_ILOCKBYTES, "%p OUT FileLockBytes::Release\n", this));
  38. return lRet;
  39. }
  40. HRESULT FileLockBytes::ReadAt(THIS_ ULARGE_INTEGER ulOffset, VOID HUGEP *pv,
  41. ULONG cb, ULONG FAR *pcbRead)
  42. {
  43. HRESULT hresult = E_FAIL;
  44. UrlMkDebugOut((DEB_ILOCKBYTES, "%p IN FileLockBytes::ReadAt\n", this));
  45. *pcbRead = 0;
  46. if (!cb)
  47. goto ReadAtExit;
  48. if (_hFileHandle == INVALID_HANDLE_VALUE)
  49. goto ReadAtExit;
  50. hresult = seekfile(ulOffset);
  51. if (hresult != NOERROR)
  52. goto ReadAtExit;
  53. if (!ReadFile(_hFileHandle, pv, cb, pcbRead, NULL))
  54. {
  55. if (GetLastError() == ERROR_LOCK_VIOLATION)
  56. hresult = STG_E_ACCESSDENIED;
  57. else
  58. hresult = E_FAIL;
  59. }
  60. ReadAtExit:
  61. UrlMkDebugOut((DEB_ILOCKBYTES, "%p OUT FileLockBytes::ReadAt\n", this));
  62. return(hresult);
  63. }
  64. HRESULT FileLockBytes::WriteAt(THIS_ ULARGE_INTEGER ulOffset, VOID const HUGEP *pv,
  65. ULONG cb, ULONG FAR *pcbWritten)
  66. {
  67. HRESULT hresult = E_FAIL;
  68. UrlMkDebugOut((DEB_ILOCKBYTES, "%p IN FileLockBytes::WriteAt\n", this));
  69. *pcbWritten = 0;
  70. if (!cb)
  71. goto WriteAtExit;
  72. if (_hFileHandle == INVALID_HANDLE_VALUE)
  73. goto WriteAtExit;
  74. hresult = seekfile(ulOffset);
  75. if (hresult != NOERROR)
  76. goto WriteAtExit;
  77. if (!WriteFile(_hFileHandle, pv, cb, pcbWritten, NULL))
  78. {
  79. if (GetLastError() == ERROR_LOCK_VIOLATION)
  80. hresult = STG_E_ACCESSDENIED;
  81. else
  82. hresult = E_FAIL;
  83. }
  84. WriteAtExit:
  85. UrlMkDebugOut((DEB_ILOCKBYTES, "%p OUT FileLockBytes::WriteAt\n", this));
  86. return(hresult);
  87. }
  88. HRESULT FileLockBytes::Flush()
  89. {
  90. HRESULT hresult = E_FAIL;
  91. UrlMkDebugOut((DEB_ILOCKBYTES, "%p IN FileLockBytes::Flush\n", this));
  92. if (_hFileHandle != INVALID_HANDLE_VALUE)
  93. {
  94. if (FlushFileBuffers(_hFileHandle))
  95. hresult = NOERROR;
  96. }
  97. UrlMkDebugOut((DEB_ILOCKBYTES, "%p OUT FileLockBytes::Flush\n", this));
  98. return(hresult);
  99. }
  100. HRESULT FileLockBytes::SetSize(THIS_ ULARGE_INTEGER cb)
  101. {
  102. HRESULT hresult = E_FAIL;
  103. UrlMkDebugOut((DEB_ILOCKBYTES, "%p IN FileLockBytes::SetSize\n", this));
  104. if (_hFileHandle == INVALID_HANDLE_VALUE)
  105. goto SetSizeExit;
  106. hresult = seekfile(cb);
  107. if (hresult != NOERROR)
  108. goto SetSizeExit;
  109. if (SetEndOfFile(_hFileHandle))
  110. hresult = NOERROR;
  111. SetSizeExit:
  112. UrlMkDebugOut((DEB_ILOCKBYTES, "%p OUT FileLockBytes::SetSize\n", this));
  113. return(hresult);
  114. }
  115. HRESULT FileLockBytes::LockRegion(THIS_ ULARGE_INTEGER libOffset,
  116. ULARGE_INTEGER cb, DWORD dwLockType)
  117. {
  118. HRESULT hresult = NOERROR;
  119. DWORD offslow, offshigh, lenlow, lenhigh;
  120. UrlMkDebugOut((DEB_ILOCKBYTES, "%p IN FileLockBytes::LockRegion\n", this));
  121. if (_hFileHandle == INVALID_HANDLE_VALUE)
  122. {
  123. hresult = E_FAIL;
  124. goto LockRegionExit;
  125. }
  126. offshigh = *(DWORD *)(&libOffset.QuadPart + sizeof(ULONG));
  127. offslow = *(DWORD *)(&libOffset.QuadPart);
  128. lenhigh = *(DWORD *)(&cb.QuadPart + sizeof(ULONG));
  129. lenlow = *(DWORD *)(&cb.QuadPart);
  130. if (!LockFile(_hFileHandle, offslow, offshigh, lenlow, lenhigh))
  131. {
  132. if (GetLastError() == ERROR_LOCK_FAILED)
  133. hresult = STG_E_LOCKVIOLATION;
  134. else
  135. hresult = E_FAIL;
  136. }
  137. LockRegionExit:
  138. UrlMkDebugOut((DEB_ILOCKBYTES, "%p OUT FileLockBytes::LockRegion\n", this));
  139. return(hresult);
  140. }
  141. HRESULT FileLockBytes::UnlockRegion(THIS_ ULARGE_INTEGER libOffset,
  142. ULARGE_INTEGER cb, DWORD dwLockType)
  143. {
  144. HRESULT hresult = NOERROR;
  145. DWORD offslow, offshigh, lenlow, lenhigh;
  146. UrlMkDebugOut((DEB_ILOCKBYTES, "%p IN FileLockBytes::UnlockRegion\n", this));
  147. if (_hFileHandle == INVALID_HANDLE_VALUE)
  148. {
  149. hresult = E_FAIL;
  150. goto UnlockRegionExit;
  151. }
  152. offshigh = *(DWORD *)(&libOffset.QuadPart + sizeof(ULONG));
  153. offslow = *(DWORD *)(&libOffset.QuadPart);
  154. lenhigh = *(DWORD *)(&cb.QuadPart + sizeof(ULONG));
  155. lenlow = *(DWORD *)(&cb.QuadPart);
  156. if (!UnlockFile(_hFileHandle, offslow, offshigh, lenlow, lenhigh))
  157. {
  158. if (GetLastError() == ERROR_LOCK_FAILED)
  159. hresult = STG_E_LOCKVIOLATION;
  160. else
  161. hresult = E_FAIL;
  162. }
  163. UnlockRegionExit:
  164. UrlMkDebugOut((DEB_ILOCKBYTES, "%p OUT FileLockBytes::UnlockRegion\n", this));
  165. return(hresult);
  166. }
  167. HRESULT FileLockBytes::Stat(THIS_ STATSTG FAR *pStatStg, DWORD grfStatFlag)
  168. {
  169. HRESULT hresult = E_FAIL;
  170. DWORD sizelow, sizehigh;
  171. UrlMkDebugOut((DEB_ILOCKBYTES, "%p IN FileLockBytes::Stat\n", this));
  172. if (!pStatStg)
  173. goto StatExit;
  174. if (_hFileHandle == INVALID_HANDLE_VALUE)
  175. goto StatExit;
  176. memset(pStatStg, 0, sizeof(STATSTG));
  177. pStatStg->pwcsName = NULL;
  178. pStatStg->type = STGTY_LOCKBYTES;
  179. sizelow = GetFileSize(_hFileHandle, &sizehigh);
  180. if (sizelow == -1 && GetLastError() != NOERROR)
  181. goto StatExit;
  182. *(DWORD *)(&pStatStg->cbSize.QuadPart + sizeof(ULONG)) = sizehigh;
  183. *(DWORD *)(&pStatStg->cbSize.QuadPart) = sizelow;
  184. if (GetFileTime(_hFileHandle, &pStatStg->ctime, &pStatStg->atime, &pStatStg->mtime))
  185. {
  186. pStatStg->grfMode = GENERIC_READ | GENERIC_WRITE;
  187. pStatStg->grfLocksSupported = LOCK_WRITE | LOCK_EXCLUSIVE | LOCK_ONLYONCE;
  188. pStatStg->clsid = IID_ILockBytes;
  189. pStatStg->grfStateBits = 0;
  190. hresult = NOERROR;
  191. }
  192. StatExit:
  193. UrlMkDebugOut((DEB_ILOCKBYTES, "%p OUT FileLockBytes::Stat\n", this));
  194. return(hresult);
  195. }
  196. HRESULT FileLockBytes::seekfile(ULARGE_INTEGER offset)
  197. {
  198. HRESULT hresult = NOERROR;
  199. DWORD offslow, offshigh;
  200. offshigh = *(DWORD *)(&offset.QuadPart + sizeof(ULONG));
  201. offslow = *(DWORD *)(&offset.QuadPart);
  202. offslow = SetFilePointer(_hFileHandle, offslow, (LONG *)&offshigh, FILE_BEGIN);
  203. if (offslow == -1 && GetLastError() != NO_ERROR)
  204. hresult = E_FAIL;
  205. return(hresult);
  206. }