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.

501 lines
13 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1996.
  5. //
  6. // File: ROSTMFIL.CXX
  7. //
  8. // Contents:
  9. //
  10. // Classes: Implements the CReadOnlyStreamFile class.
  11. //
  12. // Functions:
  13. //
  14. // History: 03-02-96 JoeS (Joe Souza) Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #include <urlint.h>
  18. #ifndef unix
  19. #include "..\trans\transact.hxx"
  20. #else
  21. #include "../trans/transact.hxx"
  22. #endif /* unix */
  23. #include "rostmdir.hxx"
  24. #include "rostmfil.hxx"
  25. //+---------------------------------------------------------------------------
  26. //
  27. // Method: CReadOnlyStreamFile::GetFileName
  28. //
  29. // Synopsis:
  30. //
  31. // Arguments: (none)
  32. //
  33. // Returns:
  34. //
  35. // History: 7-22-96 JohannP (Johann Posch) Created
  36. //
  37. // Notes:
  38. //
  39. //----------------------------------------------------------------------------
  40. LPWSTR CReadOnlyStreamFile::GetFileName()
  41. {
  42. DEBUG_ENTER((DBG_STORAGE,
  43. String,
  44. "CReadOnlyStreamFile::GetFileName",
  45. "this=%#x",
  46. this
  47. ));
  48. UrlMkDebugOut((DEB_ISTREAM, "%p IN CReadOnlyStreamFile::GetFileName\n", this));
  49. LPWSTR pwzDupname = NULL;
  50. if (_pszFileName)
  51. {
  52. pwzDupname = DupA2W(_pszFileName);
  53. }
  54. UrlMkDebugOut((DEB_ISTREAM, "%p OUT CReadOnlyStreamFile::GetFileName (Filename:%ws)\n", this, pwzDupname));
  55. DEBUG_LEAVE(pwzDupname);
  56. return pwzDupname;
  57. }
  58. HRESULT CReadOnlyStreamFile::Create(LPSTR pszFileName, CReadOnlyStreamFile **ppCRoStm, LPCWSTR pwzURL)
  59. {
  60. DEBUG_ENTER((DBG_STORAGE,
  61. Hresult,
  62. "CReadOnlyStreamFile::Create",
  63. "%.80q, %#x, %.80wq",
  64. pszFileName, ppCRoStm, pwzURL
  65. ));
  66. UrlMkDebugOut((DEB_ISTREAM, "%p IN CReadOnlyStreamFile::Create (pszFileName:%s)\n", NULL,pszFileName));
  67. HRESULT hr = NOERROR;
  68. *ppCRoStm = NULL;
  69. if (pszFileName == NULL)
  70. {
  71. hr = E_INVALIDARG;
  72. }
  73. else
  74. {
  75. LPSTR pszStr = new CHAR [strlen(pszFileName) + 1];
  76. if (pszStr == NULL)
  77. {
  78. hr = E_OUTOFMEMORY;
  79. }
  80. else
  81. {
  82. HANDLE handle = CreateFileA(pszFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
  83. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  84. if (handle == INVALID_HANDLE_VALUE)
  85. {
  86. delete pszStr;
  87. hr = E_FAIL;
  88. }
  89. else
  90. {
  91. LPWSTR pwzStr = NULL;
  92. if (pwzURL)
  93. {
  94. pwzStr = OLESTRDuplicate(pwzURL);
  95. }
  96. strcpy(pszStr, pszFileName);
  97. CReadOnlyStreamFile *pCRoStm = new CReadOnlyStreamFile(pszStr, handle, pwzStr);
  98. if (pCRoStm == NULL)
  99. {
  100. delete pszStr;
  101. }
  102. else
  103. {
  104. *ppCRoStm = pCRoStm;
  105. }
  106. }
  107. }
  108. }
  109. UrlMkDebugOut((DEB_ISTREAM, "%p OUT CReadOnlyStreamFile::Create (hr:%lx)\n", *ppCRoStm, hr));
  110. DEBUG_LEAVE(hr);
  111. return hr;
  112. }
  113. CReadOnlyStreamFile::CReadOnlyStreamFile(LPSTR pszFileName, HANDLE handle, LPWSTR pwzURL)
  114. : CReadOnlyStreamDirect(NULL, 0, TRUE)
  115. {
  116. DEBUG_ENTER((DBG_STORAGE,
  117. None,
  118. "CReadOnlyStreamFile::CReadOnlyStreamFile",
  119. "this=%#x, %.80q, %#x, %.80wq",
  120. this, pszFileName, handle, pwzURL
  121. ));
  122. UrlMkDebugOut((DEB_ISTREAM, "%p IN CReadOnlyStreamFile::CReadOnlyStreamFile (pszFileName:%s)\n", NULL,pszFileName));
  123. _hFileHandle = handle;
  124. _pszFileName = pszFileName;
  125. _fDataFullyAvailable = FALSE;
  126. _hWinInetLockHandle = NULL;
  127. _pwzURL = pwzURL;
  128. _fDeleteFile = FALSE;
  129. UrlMkDebugOut((DEB_ISTREAM, "%p OUT CReadOnlyStreamFile::CReadOnlyStreamFile \n", this));
  130. DEBUG_LEAVE(0);
  131. }
  132. CReadOnlyStreamFile::~CReadOnlyStreamFile(void)
  133. {
  134. DEBUG_ENTER((DBG_STORAGE,
  135. None,
  136. "CReadOnlyStreamFile::~CReadOnlyStreamFile",
  137. "this=%#x",
  138. this
  139. ));
  140. UrlMkDebugOut((DEB_ISTREAM, "%p IN CReadOnlyStreamFile::~CReadOnlyStreamFile (_pszFileName:%s)\n", this,_pszFileName));
  141. if (_hFileHandle != INVALID_HANDLE_VALUE)
  142. {
  143. CloseHandle(_hFileHandle);
  144. }
  145. if (_pwzURL)
  146. {
  147. if (_fDeleteFile)
  148. {
  149. DeleteUrlCacheEntryW(_pwzURL);
  150. }
  151. delete [] _pwzURL;
  152. }
  153. if (_hWinInetLockHandle)
  154. {
  155. InternetUnlockRequestFile(_hWinInetLockHandle);
  156. _hWinInetLockHandle = NULL;
  157. }
  158. if (_pszFileName)
  159. {
  160. delete _pszFileName;
  161. }
  162. UrlMkDebugOut((DEB_ISTREAM, "%p OUT CReadOnlyStreamFile::~CReadOnlyStreamFile\n", this));
  163. DEBUG_LEAVE(0);
  164. }
  165. HRESULT CReadOnlyStreamFile::Read(THIS_ VOID HUGEP *pv, ULONG cb, ULONG FAR *pcbRead)
  166. {
  167. DEBUG_ENTER((DBG_STORAGE,
  168. Hresult,
  169. "CReadOnlyStreamFile::IStream::Read",
  170. "this=%#x, %#x, %x, %#x",
  171. this, pv, cb, pcbRead
  172. ));
  173. HRESULT hresult = NOERROR;
  174. UrlMkDebugOut((DEB_ISTREAM, "%p IN CReadOnlyStreamFile::Read (cbBuffer:%lx)\n", this,cb));
  175. DWORD dwRead = 0;
  176. if (!ReadFile(_hFileHandle, pv, cb, &dwRead, NULL))
  177. {
  178. if (GetLastError() == ERROR_LOCK_VIOLATION)
  179. {
  180. hresult = STG_E_ACCESSDENIED;
  181. }
  182. else
  183. {
  184. hresult = E_FAIL;
  185. }
  186. if (pcbRead)
  187. {
  188. *pcbRead = 0;
  189. }
  190. }
  191. else
  192. {
  193. if (pcbRead)
  194. {
  195. *pcbRead = dwRead;
  196. }
  197. if (dwRead == 0)
  198. {
  199. hresult = (_fDataFullyAvailable) ? S_FALSE : E_PENDING;
  200. }
  201. }
  202. UrlMkDebugOut((DEB_ISTREAM, "%p OUT CReadOnlyStreamFile::Read (hr:%lx, cdRead:%lx)\n", this, hresult, dwRead));
  203. DEBUG_LEAVE(hresult);
  204. return(hresult);
  205. }
  206. HRESULT CReadOnlyStreamFile::Seek(THIS_ LARGE_INTEGER dlibMove, DWORD dwOrigin,
  207. ULARGE_INTEGER FAR *plibNewPosition)
  208. {
  209. DEBUG_ENTER((DBG_STORAGE,
  210. Hresult,
  211. "CReadOnlyStreamFile::IStream::Seek",
  212. "this=%#x, %#x, %x, %#x",
  213. this, dlibMove, dwOrigin, plibNewPosition
  214. ));
  215. HRESULT hresult = NOERROR;
  216. DWORD offslow, offshigh;
  217. UrlMkDebugOut((DEB_ISTREAM, "%p IN CReadOnlyStreamFile::Seek\n", this));
  218. offshigh = dlibMove.HighPart;
  219. offslow = dlibMove.LowPart;
  220. offslow = SetFilePointer(_hFileHandle, offslow, (LONG *)&offshigh, dwOrigin);
  221. if (offslow == -1 && GetLastError() != NO_ERROR)
  222. {
  223. hresult = E_FAIL;
  224. }
  225. else if (plibNewPosition)
  226. {
  227. plibNewPosition->HighPart = offshigh;
  228. plibNewPosition->LowPart = offslow;
  229. }
  230. UrlMkDebugOut((DEB_ISTREAM, "%p OUT CReadOnlyStreamFile::Seek\n", this));
  231. DEBUG_LEAVE(hresult);
  232. return(hresult);
  233. }
  234. HRESULT CReadOnlyStreamFile::CopyTo(THIS_ LPSTREAM pStm, ULARGE_INTEGER cb,
  235. ULARGE_INTEGER FAR *pcbRead, ULARGE_INTEGER FAR *pcbWritten)
  236. {
  237. DEBUG_ENTER((DBG_STORAGE,
  238. Hresult,
  239. "CReadOnlyStreamFile::IStream::CopyTo",
  240. "this=%#x, %#x, %x, %#x, %#x",
  241. this, pStm, cb, pcbRead, pcbWritten
  242. ));
  243. HRESULT hresult = STG_E_INSUFFICIENTMEMORY;
  244. LPVOID memptr = NULL;
  245. DWORD readcount, writecount;
  246. UrlMkDebugOut((DEB_ISTREAM, "%p IN CReadOnlyStreamFile::CopyTo\n", this));
  247. if (cb.HighPart || (pStm == NULL))
  248. {
  249. hresult = E_INVALIDARG;
  250. goto CopyToExit;
  251. }
  252. // do not need to copy to ourself
  253. if (pStm == this)
  254. {
  255. hresult = NOERROR;
  256. goto CopyToExit;
  257. }
  258. memptr = new BYTE [cb.LowPart];
  259. if (memptr)
  260. {
  261. if (!ReadFile(_hFileHandle, memptr, cb.LowPart, &readcount, NULL))
  262. {
  263. if (GetLastError() == ERROR_LOCK_VIOLATION)
  264. {
  265. hresult = STG_E_ACCESSDENIED;
  266. }
  267. else
  268. {
  269. hresult = E_FAIL;
  270. }
  271. goto CopyToExit;
  272. }
  273. if (pcbRead)
  274. {
  275. pcbRead->HighPart = 0;
  276. pcbRead->LowPart = readcount;
  277. }
  278. hresult = pStm->Write(memptr, readcount, &writecount);
  279. if (pcbWritten && !hresult)
  280. {
  281. pcbWritten->HighPart = 0;
  282. pcbWritten->LowPart = writecount;
  283. }
  284. }
  285. CopyToExit:
  286. if (memptr)
  287. {
  288. delete memptr;
  289. }
  290. UrlMkDebugOut((DEB_ISTREAM, "%p OUT CReadOnlyStreamFile::CopyTo\n", this));
  291. DEBUG_LEAVE(hresult);
  292. return(hresult);
  293. }
  294. HRESULT CReadOnlyStreamFile::LockRegion(THIS_ ULARGE_INTEGER libOffset,
  295. ULARGE_INTEGER cb, DWORD dwLockType)
  296. {
  297. DEBUG_ENTER((DBG_STORAGE,
  298. Hresult,
  299. "CReadOnlyStreamFile::IStream::LockRegion",
  300. "this=%#x, %#x, %x, %x",
  301. this, libOffset, cb, dwLockType
  302. ));
  303. UrlMkDebugOut((DEB_ISTREAM, "%p CReadOnlyStreamFile::LockRegion (NoOp)\n", this));
  304. DEBUG_LEAVE(E_NOTIMPL);
  305. return(E_NOTIMPL);
  306. }
  307. HRESULT CReadOnlyStreamFile::UnlockRegion(THIS_ ULARGE_INTEGER libOffset,
  308. ULARGE_INTEGER cb, DWORD dwLockType)
  309. {
  310. DEBUG_ENTER((DBG_STORAGE,
  311. Hresult,
  312. "CReadOnlyStreamFile::IStream::UnlockRegion",
  313. "this=%#x, %#x, %x, %x",
  314. this, libOffset, cb, dwLockType
  315. ));
  316. UrlMkDebugOut((DEB_ISTREAM, "%p CReadOnlyStreamFile::UnlockRegion (NoOp)\n", this));
  317. DEBUG_LEAVE(E_NOTIMPL);
  318. return(E_NOTIMPL);
  319. }
  320. HRESULT CReadOnlyStreamFile::Stat(THIS_ STATSTG FAR *pStatStg, DWORD grfStatFlag)
  321. {
  322. DEBUG_ENTER((DBG_STORAGE,
  323. Hresult,
  324. "CReadOnlyStreamFile::IStream::Stat",
  325. "this=%#x, %#x, %x",
  326. this, pStatStg, grfStatFlag
  327. ));
  328. HRESULT hresult = E_FAIL;
  329. DWORD sizelow, sizehigh;
  330. UrlMkDebugOut((DEB_ISTREAM, "%p IN CReadOnlyStreamFile::Stat\n", this));
  331. if (pStatStg)
  332. {
  333. memset(pStatStg, 0, sizeof(STATSTG));
  334. pStatStg->type = STGTY_STREAM;
  335. pStatStg->clsid = IID_IStream;
  336. sizelow = GetFileSize(_hFileHandle, &sizehigh);
  337. if (sizelow == -1 && GetLastError() != NOERROR)
  338. {
  339. goto StatExit;
  340. }
  341. pStatStg->cbSize.HighPart = sizehigh;
  342. pStatStg->cbSize.LowPart = sizelow;
  343. pStatStg->pwcsName = GetFileName();
  344. if (GetFileTime(_hFileHandle, &pStatStg->ctime, &pStatStg->atime, &pStatStg->mtime))
  345. {
  346. pStatStg->grfMode = GENERIC_READ;
  347. pStatStg->grfLocksSupported = 0;
  348. pStatStg->clsid = IID_IStream;
  349. pStatStg->grfStateBits = 0;
  350. hresult = NOERROR;
  351. }
  352. }
  353. StatExit:
  354. UrlMkDebugOut((DEB_ISTREAM, "%p OUT CReadOnlyStreamFile::Stat\n", this));
  355. DEBUG_LEAVE(hresult);
  356. return(hresult);
  357. }
  358. HRESULT CReadOnlyStreamFile::Clone(THIS_ LPSTREAM FAR *ppStm)
  359. {
  360. DEBUG_ENTER((DBG_STORAGE,
  361. Hresult,
  362. "CReadOnlyStreamFile::IStream::Clone",
  363. "this=%#x, %#x",
  364. this, ppStm
  365. ));
  366. HRESULT hr = NOERROR;
  367. CReadOnlyStreamFile *pCRoStm;
  368. UrlMkDebugOut((DEB_ISTREAM, "%p IN CReadOnlyStreamFile::Clone\n", this));
  369. hr = Create(_pszFileName, &pCRoStm, _pwzURL);
  370. if (hr == NOERROR)
  371. {
  372. *ppStm = pCRoStm;
  373. }
  374. else
  375. {
  376. *ppStm = NULL;
  377. }
  378. UrlMkDebugOut((DEB_ISTREAM, "%p OUT CReadOnlyStreamFile::Clone (hr:%lx)\n", this, hr));
  379. DEBUG_LEAVE(hr);
  380. return hr;
  381. }
  382. HRESULT CReadOnlyStreamFile::SetHandleForUnlock(THIS_ DWORD_PTR hWinInetLockHandle, DWORD_PTR dwReserved)
  383. {
  384. DEBUG_ENTER((DBG_STORAGE,
  385. Hresult,
  386. "CReadOnlyStreamFile::IWinInetFileStream::SetHandleForUnlock",
  387. "this=%#x, handle=%#x",
  388. this, hWinInetLockHandle
  389. ));
  390. HRESULT hr = E_FAIL;
  391. if (hWinInetLockHandle)
  392. {
  393. _hWinInetLockHandle = (HANDLE)hWinInetLockHandle;
  394. hr = S_OK;
  395. }
  396. DEBUG_LEAVE(hr);
  397. return hr;
  398. }
  399. HRESULT CReadOnlyStreamFile::SetDeleteFile(THIS_ DWORD_PTR dwReserved)
  400. {
  401. DEBUG_ENTER((DBG_STORAGE,
  402. Hresult,
  403. "CReadOnlyStreamFile::IWinInetFileStream::SetDeleteFile",
  404. "this=%#x, URL=%.80wq",
  405. this, _pwzURL
  406. ));
  407. _fDeleteFile = TRUE;
  408. DEBUG_LEAVE(S_OK);
  409. return S_OK;
  410. }