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.

3216 lines
96 KiB

  1. //+--------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1992.
  5. //
  6. // File: expdf.cxx
  7. //
  8. // Contents: Exposed DocFile implementation
  9. //
  10. // History: 20-Jan-92 DrewB Created
  11. //
  12. //---------------------------------------------------------------
  13. #include <exphead.cxx>
  14. #pragma hdrstop
  15. #include <expdf.hxx>
  16. #include <expst.hxx>
  17. #include <expiter.hxx>
  18. #include <pbstream.hxx>
  19. #include <lock.hxx>
  20. #include <marshl.hxx>
  21. #include <logfile.hxx>
  22. #include <rpubdf.hxx>
  23. #include <expparam.hxx>
  24. #include <olepfn.hxx>
  25. #include <ole2sp.h>
  26. #if WIN32 == 300
  27. IMPLEMENT_UNWIND(CSafeAccess);
  28. IMPLEMENT_UNWIND(CSafeSem);
  29. #endif
  30. extern WCHAR const wcsContents[];
  31. //+--------------------------------------------------------------
  32. //
  33. // Member: CExposedDocFile::CExposedDocFile, public
  34. //
  35. // Synopsis: Constructor
  36. //
  37. // Arguments: [pdf] - Public DocFile
  38. // [pdfb] - DocFile basis
  39. // [ppc] - Context
  40. //
  41. // History: 20-Jan-92 DrewB Created
  42. //
  43. //---------------------------------------------------------------
  44. CExposedDocFile::CExposedDocFile(CPubDocFile *pdf,
  45. CDFBasis *pdfb,
  46. CPerContext *ppc)
  47. : CPropertySetStorage(MAPPED_STREAM_QI),
  48. _PropertyBagEx( DFlagsToMode( pdf->GetDFlags() ))
  49. {
  50. olDebugOut((DEB_ITRACE, "In CExposedDocFile::CExposedDocFile("
  51. "%p, %p, %p)\n", pdf, pdfb, ppc));
  52. _ppc = ppc;
  53. _pdf = P_TO_BP(CBasedPubDocFilePtr, pdf);
  54. _pdfb = P_TO_BP(CBasedDFBasisPtr, pdfb);
  55. _pdfb->vAddRef();
  56. _cReferences = 1;
  57. _sig = CEXPOSEDDOCFILE_SIG;
  58. #if WIN32 >= 300
  59. _pIAC = NULL;
  60. #endif
  61. // Initialize CPropertySetStorage and CPropertyBagEx
  62. CPropertySetStorage::Init( static_cast<IStorage*>(this), static_cast<IBlockingLock*>(this),
  63. FALSE ); // fControlLifetimes (=> Don't addref)
  64. _PropertyBagEx.Init( static_cast<IPropertySetStorage*>(this), // Not addref-ed
  65. static_cast<IBlockingLock*>(this) ); // Not addref-ed
  66. //
  67. // CoQueryReleaseObject needs to have the address of the exposed docfiles
  68. // query interface routine.
  69. //
  70. if (adwQueryInterfaceTable[QI_TABLE_CExposedDocFile] == 0)
  71. {
  72. adwQueryInterfaceTable[QI_TABLE_CExposedDocFile] =
  73. **(ULONG_PTR **)((IStorage *)this);
  74. }
  75. #ifdef COORD
  76. _ulLock = _cbSizeBase = _cbSizeOrig = 0;
  77. _sigMSF = 0;
  78. #endif
  79. olDebugOut((DEB_ITRACE, "Out CExposedDocFile::CExposedDocFile\n"));
  80. }
  81. SCODE CExposedDocFile::InitMarshal(DWORD dwAsyncFlags,
  82. IDocfileAsyncConnectionPoint *pdacp)
  83. {
  84. return _cpoint.InitMarshal(this, dwAsyncFlags, pdacp);
  85. }
  86. //+--------------------------------------------------------------
  87. //
  88. // Member: CExposedDocFile::~CExposedDocFile, public
  89. //
  90. // Synopsis: Destructor
  91. //
  92. // History: 23-Jan-92 DrewB Created
  93. //
  94. //---------------------------------------------------------------
  95. CExposedDocFile::~CExposedDocFile(void)
  96. {
  97. BOOL fClose = FALSE;
  98. olDebugOut((DEB_ITRACE, "In CExposedDocFile::~CExposedDocFile\n"));
  99. olAssert(_cReferences == 0);
  100. //In order to call into the tree, we need to take the mutex.
  101. //The mutex may get deleted in _ppc->Release(), so we can't
  102. //release it here. The mutex actually gets released in
  103. //CPerContext::Release() or in the CPerContext destructor.
  104. //If _ppc is NULL, we're partially constructed and don't need to
  105. //worry.
  106. SCODE sc;
  107. #if WIN32 >= 300
  108. if (_pIAC != NULL)
  109. {
  110. _pIAC->Release();
  111. _pIAC = NULL;
  112. }
  113. #endif
  114. #if !defined(MULTIHEAP)
  115. // TakeSem and ReleaseSem are moved to the Release Method
  116. // so that the deallocation for this object is protected
  117. if (_ppc)
  118. {
  119. sc = TakeSem();
  120. SetWriteAccess();
  121. olAssert(SUCCEEDED(sc));
  122. }
  123. #ifdef ASYNC
  124. IDocfileAsyncConnectionPoint *pdacp = _cpoint.GetMarshalPoint();
  125. #endif
  126. #endif //MULTIHEAP
  127. if (_pdf)
  128. {
  129. // If we're the last reference on a root object
  130. // we close the context because all children will become
  131. // reverted so it is no longer necessary
  132. if (_pdf->GetRefCount() == 1 && _pdf->IsRoot())
  133. fClose = TRUE;
  134. _pdf->CPubDocFile::vRelease();
  135. }
  136. if (_pdfb)
  137. _pdfb->CDFBasis::vRelease();
  138. #if !defined(MULTIHEAP)
  139. if (_ppc)
  140. {
  141. if (fClose)
  142. _ppc->Close();
  143. if (_ppc->Release() > 0)
  144. ReleaseSem(sc);
  145. }
  146. #ifdef ASYNC
  147. //Mutex has been released, so we can release the connection point
  148. // without fear of deadlock.
  149. if (pdacp != NULL)
  150. pdacp->Release();
  151. #endif
  152. #endif // MULTIHEAP
  153. _sig = CEXPOSEDDOCFILE_SIGDEL;
  154. olDebugOut((DEB_ITRACE, "Out CExposedDocFile::~CExposedDocFile\n"));
  155. }
  156. //+--------------------------------------------------------------
  157. //
  158. // Member: CExposedDocFile::Release, public
  159. //
  160. // Synopsis: Releases resources for a CExposedDocFile
  161. //
  162. // Returns: Appropriate status code
  163. //
  164. // History: 20-Jan-92 DrewB Created
  165. //
  166. //---------------------------------------------------------------
  167. STDMETHODIMP_(ULONG) CExposedDocFile::Release(void)
  168. {
  169. LONG lRet;
  170. olLog(("%p::In CExposedDocFile::Release()\n", this));
  171. olDebugOut((DEB_TRACE, "In CExposedDocFile::Release()\n"));
  172. if (FAILED(Validate()))
  173. return 0;
  174. olAssert(_cReferences > 0);
  175. lRet = InterlockedDecrement(&_cReferences);
  176. if (lRet == 0)
  177. {
  178. SCODE sc = S_OK;
  179. sc = _PropertyBagEx.Commit( STGC_DEFAULT );
  180. olAssert(SUCCEEDED(sc));
  181. sc = _PropertyBagEx.ShutDown();
  182. olAssert(SUCCEEDED(sc));
  183. #ifdef MULTIHEAP
  184. CSafeMultiHeap smh(_ppc);
  185. CPerContext *ppc = _ppc;
  186. if (_ppc)
  187. {
  188. sc = TakeSem();
  189. SetWriteAccess();
  190. olAssert(SUCCEEDED(sc));
  191. }
  192. #ifdef ASYNC
  193. IDocfileAsyncConnectionPoint *pdacp = _cpoint.GetMarshalPoint();
  194. #endif
  195. BOOL fClose = (_pdf) && (_pdf->GetRefCount()==1) && _pdf->IsRoot();
  196. #endif //MULTIHEAP
  197. delete this;
  198. #ifdef MULTIHEAP
  199. if (ppc)
  200. {
  201. if (fClose)
  202. ppc->Close();
  203. if (ppc->Release() == 0)
  204. g_smAllocator.Uninit();
  205. else
  206. if (SUCCEEDED(sc)) ppc->UntakeSem();
  207. }
  208. #ifdef ASYNC
  209. //Mutex has been released, so we can release the connection point
  210. // without fear of deadlock.
  211. if (pdacp != NULL)
  212. pdacp->Release();
  213. #endif
  214. #endif
  215. }
  216. else if (lRet < 0)
  217. lRet = 0;
  218. olDebugOut((DEB_TRACE, "Out CExposedDocFile::Release %p() => %lu\n",
  219. this, lRet));
  220. olLog(("%p::Out CExposedDocFile::Release(). ret == %lu\n", this, lRet));
  221. FreeLogFile();
  222. return (ULONG)lRet;
  223. }
  224. //+--------------------------------------------------------------
  225. //
  226. // Member: CExposedDocFile::CheckCopyTo, private
  227. //
  228. // Synopsis: Checks for CopyTo legality
  229. //
  230. // Returns: Appropriate status code
  231. //
  232. // History: 07-Jul-92 DrewB Created
  233. //
  234. //---------------------------------------------------------------
  235. inline SCODE CExposedDocFile::CheckCopyTo(void)
  236. {
  237. return _pdfb->GetCopyBase() != NULL &&
  238. _pdf->IsAtOrAbove(_pdfb->GetCopyBase()) ?
  239. STG_E_ACCESSDENIED : S_OK;
  240. }
  241. //+--------------------------------------------------------------
  242. //
  243. // Member: CExposedDocFile::ConvertInternalStream, private
  244. //
  245. // Synopsis: Converts an internal stream to a storage
  246. //
  247. // Arguments: [pwcsName] - Name
  248. // [pdfExp] - Destination docfile
  249. //
  250. // Returns: Appropriate status code
  251. //
  252. // History: 23-Jun-92 DrewB Created
  253. //
  254. //---------------------------------------------------------------
  255. static WCHAR const wcsIllegalName[] = {'\\', '\0'};
  256. SCODE CExposedDocFile::ConvertInternalStream(CExposedDocFile *pdfExp)
  257. {
  258. CPubStream *pstFrom, *pstTo;
  259. SCODE sc;
  260. CDfName const dfnIllegal(wcsIllegalName);
  261. CDfName const dfnContents(wcsContents);
  262. olDebugOut((DEB_ITRACE, "In CExposedDocFile::ConvertInternalStream(%p)\n",
  263. pdfExp));
  264. olChk(_pdf->GetStream(&dfnIllegal, DF_READWRITE | DF_DENYALL, &pstFrom));
  265. olChkTo(EH_pstFrom, pdfExp->GetPub()->CreateStream(&dfnContents,
  266. DF_WRITE | DF_DENYALL,
  267. &pstTo));
  268. olChkTo(EH_pstTo, CopySStreamToSStream(pstFrom->GetSt(),
  269. pstTo->GetSt()));
  270. olChkTo(EH_pstTo, _pdf->DestroyEntry(&dfnIllegal, FALSE));
  271. sc = S_OK;
  272. olDebugOut((DEB_ITRACE, "Out CExposedDocFile::ConvertInternalStream\n"));
  273. // Fall through
  274. EH_pstTo:
  275. pstTo->CPubStream::vRelease();
  276. EH_pstFrom:
  277. pstFrom->CPubStream::vRelease();
  278. EH_Err:
  279. return sc;
  280. }
  281. //+---------------------------------------------------------------------------
  282. //
  283. // Member: CExposedDocFile::CreateEntry, private
  284. //
  285. // Synopsis: Creates elements, used in CreateStream, CreateStorage.
  286. //
  287. // Arguments: [pdfn] - Name
  288. // [dwType] - Entry type
  289. // [grfMode] - Access mode
  290. // [ppv] - Object return
  291. //
  292. // Returns: Appropriate status code
  293. //
  294. // Modifies: [ppv]
  295. //
  296. // History: 18-Dec-92 DrewB Created
  297. //
  298. //----------------------------------------------------------------------------
  299. SCODE CExposedDocFile::CreateEntry(CDfName const *pdfn,
  300. DWORD dwType,
  301. DWORD grfMode,
  302. void **ppv)
  303. {
  304. SCODE sc;
  305. SEntryBuffer eb;
  306. BOOL fRenamed = FALSE;
  307. CPubStream *pst;
  308. CExposedStream *pstExp;
  309. CPubDocFile *pdf;
  310. CExposedDocFile *pdfExp;
  311. olDebugOut((DEB_ITRACE, "In CExposedDocFile::CreateEntry:%p("
  312. "%p, %lX, %lX, %p)\n", this, pdfn, dwType, grfMode, ppv));
  313. olChk(EnforceSingle(grfMode));
  314. // 3/11/93 - Demand scratch when opening/creating transacted
  315. if ((grfMode & STGM_TRANSACTED) == STGM_TRANSACTED)
  316. {
  317. olChk(_ppc->GetDirty()->InitScratch());
  318. }
  319. if (grfMode & (STGM_CREATE | STGM_CONVERT))
  320. {
  321. if (FAILED(sc = _pdf->IsEntry(pdfn, &eb)))
  322. {
  323. if (sc != STG_E_FILENOTFOUND)
  324. olErr(EH_Err, sc);
  325. }
  326. else if (eb.dwType == dwType && (grfMode & STGM_CREATE))
  327. olChk(_pdf->DestroyEntry(pdfn, FALSE));
  328. else if (eb.dwType == STGTY_STREAM && (grfMode & STGM_CONVERT) &&
  329. dwType == STGTY_STORAGE)
  330. {
  331. CDfName const dfnIllegal(wcsIllegalName);
  332. olChk(_pdf->RenameEntry(pdfn, &dfnIllegal));
  333. fRenamed = TRUE;
  334. }
  335. else
  336. olErr(EH_Err, STG_E_FILEALREADYEXISTS);
  337. }
  338. if (REAL_STGTY(dwType) == STGTY_STREAM)
  339. {
  340. olChk(_pdf->CreateStream(pdfn, ModeToDFlags(grfMode), &pst));
  341. olMemTo(EH_pst, pstExp = new (_pdfb->GetMalloc()) CExposedStream);
  342. olChkTo(EH_pstExp, pstExp->Init(pst, BP_TO_P(CDFBasis *, _pdfb),
  343. _ppc, NULL));
  344. _ppc->AddRef();
  345. #ifdef ASYNC
  346. if (_cpoint.IsInitialized())
  347. {
  348. olChkTo(EH_connSt, pstExp->InitConnection(&_cpoint));
  349. }
  350. #endif
  351. *ppv = pstExp;
  352. }
  353. else
  354. {
  355. olAssert(REAL_STGTY(dwType) == STGTY_STORAGE);
  356. olChk(_pdf->CreateDocFile(pdfn, ModeToDFlags(grfMode), &pdf));
  357. olMemTo(EH_pdf, pdfExp = new (_pdfb->GetMalloc())
  358. CExposedDocFile(pdf, BP_TO_P(CDFBasis *, _pdfb), _ppc));
  359. _ppc->AddRef();
  360. if (_cpoint.IsInitialized())
  361. {
  362. olChkTo(EH_pdfExpInit, pdfExp->InitConnection(&_cpoint));
  363. }
  364. // If we've renamed the original stream for conversion, convert
  365. if (fRenamed)
  366. {
  367. olChkTo(EH_pdfExpInit, ConvertInternalStream(pdfExp));
  368. sc = STG_S_CONVERTED;
  369. }
  370. *ppv = pdfExp;
  371. }
  372. olDebugOut((DEB_ITRACE, "Out CExposedDocFile::CreateEntry\n"));
  373. return sc;
  374. EH_connSt:
  375. pstExp->Release();
  376. goto EH_Del;
  377. EH_pstExp:
  378. delete pstExp;
  379. EH_pst:
  380. pst->CPubStream::vRelease();
  381. goto EH_Del;
  382. EH_pdfExpInit:
  383. pdfExp->Release();
  384. goto EH_Del;
  385. EH_pdf:
  386. pdf->CPubDocFile::vRelease();
  387. // Fall through
  388. EH_Del:
  389. olVerSucc(_pdf->DestroyEntry(pdfn, TRUE));
  390. EH_Err:
  391. return sc;
  392. }
  393. //+---------------------------------------------------------------------------
  394. //
  395. // Member: CExposedDocFile::OpenEntry, private
  396. //
  397. // Synopsis: Opens elements, used in OpenStream, OpenStorage.
  398. //
  399. // Arguments: [pdfn] - Name
  400. // [dwType] - Entry type
  401. // [grfMode] - Access mode
  402. // [ppv] - Object return
  403. //
  404. // Returns: Appropriate status code
  405. //
  406. // Modifies: [ppv]
  407. //
  408. // History: 18-Dec-92 DrewB Created
  409. //
  410. //----------------------------------------------------------------------------
  411. SCODE CExposedDocFile::OpenEntry(CDfName const *pdfn,
  412. DWORD dwType,
  413. DWORD grfMode,
  414. void **ppv)
  415. {
  416. SCODE sc;
  417. CPubDocFile *pdf;
  418. CExposedDocFile *pdfExp;
  419. CPubStream *pst;
  420. CExposedStream *pstExp;
  421. olDebugOut((DEB_ITRACE, "In CExposedDocFile::OpenEntry:%p("
  422. "%p, %lX, %lX, %p)\n", this, pdfn, dwType, grfMode, ppv));
  423. olChk(EnforceSingle(grfMode));
  424. // 3/11/93 - Demand scratch when opening/creating transacted
  425. if ((grfMode & STGM_TRANSACTED) == STGM_TRANSACTED)
  426. {
  427. olChk(_ppc->GetDirty()->InitScratch());
  428. }
  429. if (REAL_STGTY(dwType) == STGTY_STREAM)
  430. {
  431. olChk(_pdf->GetStream(pdfn, ModeToDFlags(grfMode), &pst));
  432. olMemTo(EH_pst, pstExp = new (_pdfb->GetMalloc()) CExposedStream);
  433. olChkTo(EH_pstExp, pstExp->Init(pst, BP_TO_P(CDFBasis *, _pdfb),
  434. _ppc, NULL));
  435. _ppc->AddRef();
  436. if (_cpoint.IsInitialized())
  437. {
  438. olChkTo(EH_connSt, pstExp->InitConnection(&_cpoint));
  439. }
  440. *ppv = pstExp;
  441. }
  442. else
  443. {
  444. olAssert(REAL_STGTY(dwType) == STGTY_STORAGE);
  445. olChk(_pdf->GetDocFile(pdfn, ModeToDFlags(grfMode), &pdf));
  446. olMemTo(EH_pdf, pdfExp = new (_pdfb->GetMalloc())
  447. CExposedDocFile(pdf, BP_TO_P(CDFBasis *, _pdfb), _ppc));
  448. _ppc->AddRef();
  449. if (_cpoint.IsInitialized())
  450. {
  451. olChkTo(EH_connDf, pdfExp->InitConnection(&_cpoint));
  452. }
  453. *ppv = pdfExp;
  454. }
  455. olDebugOut((DEB_ITRACE, "Out CExposedDocFile::OpenEntry\n"));
  456. return S_OK;
  457. EH_connSt:
  458. pstExp->Release();
  459. return sc;
  460. EH_pstExp:
  461. delete pstExp;
  462. // Fall through to clean up CPubStream
  463. EH_pst:
  464. pst->CPubStream::vRelease();
  465. return sc;
  466. EH_connDf:
  467. pdfExp->Release();
  468. return sc;
  469. EH_pdf:
  470. pdf->CPubDocFile::vRelease();
  471. // Fall through
  472. EH_Err:
  473. return sc;
  474. }
  475. //+--------------------------------------------------------------
  476. //
  477. // Member: CExposedDocFile::CreateStream, public
  478. //
  479. // Synopsis: Creates a stream
  480. //
  481. // Arguments: [pwcsName] - Name
  482. // [grfMode] - Permissions
  483. // [reserved1]
  484. // [reserved2]
  485. // [ppstm] - Stream return
  486. //
  487. // Returns: Appropriate status code
  488. //
  489. // Modifies: [ppstm]
  490. //
  491. // History: 20-Jan-92 DrewB Created
  492. //
  493. //---------------------------------------------------------------
  494. _OLESTDMETHODIMP CExposedDocFile::CreateStream(WCHAR const *pwcsName,
  495. DWORD grfMode,
  496. DWORD reserved1,
  497. DWORD reserved2,
  498. IStream **ppstm)
  499. {
  500. SCODE sc;
  501. SAFE_SEM;
  502. SAFE_ACCESS;
  503. SafeCExposedStream pestm;
  504. CDfName dfn;
  505. olDebugOut((DEB_TRACE, "In CExposedDocFile::CreateStream("
  506. "%ws, %lX, %lu, %lu, %p)\n", pwcsName, grfMode, reserved1,
  507. reserved2, ppstm));
  508. olLog(("%p::In CExposedDocFile::CreateStream(%ws, %lX, %lu, %lu, %p)\n",
  509. this, pwcsName, grfMode, reserved1, reserved2, ppstm));
  510. OL_VALIDATE(CreateStream(pwcsName,
  511. grfMode,
  512. reserved1,
  513. reserved2,
  514. ppstm));
  515. olChk(Validate());
  516. BEGIN_PENDING_LOOP;
  517. olChk(TakeSafeSem());
  518. olChk(CheckCopyTo());
  519. SafeWriteAccess();
  520. #ifdef DIRECTWRITERLOCK
  521. olChk(ValidateWriteAccess());
  522. #endif
  523. dfn.Set(pwcsName);
  524. sc = CreateEntry(&dfn, STGTY_STREAM, grfMode, (void **)&pestm);
  525. END_PENDING_LOOP;
  526. if (SUCCEEDED(sc))
  527. TRANSFER_INTERFACE(pestm, IStream, ppstm);
  528. olDebugOut((DEB_TRACE, "Out CExposedDocFile::CreateStream => %p\n",
  529. *ppstm));
  530. EH_Err:
  531. olLog(("%p::Out CExposedDocFile::CreateStream(). "
  532. "*ppstm == %p, ret == %lx\n", this, *ppstm, sc));
  533. return _OLERETURN(sc);
  534. }
  535. //+--------------------------------------------------------------
  536. //
  537. // Member: CExposedDocFile::OpenStream, public
  538. //
  539. // Synopsis: Opens an existing stream
  540. //
  541. // Arguments: [pwcsName] - Name
  542. // [reserved1]
  543. // [grfMode] - Permissions
  544. // [reserved2]
  545. // [ppstm] - Stream return
  546. //
  547. // Returns: Appropriate status code
  548. //
  549. // Modifies: [ppstm]
  550. //
  551. // History: 20-Jan-92 DrewB Created
  552. //
  553. //---------------------------------------------------------------
  554. _OLESTDMETHODIMP CExposedDocFile::OpenStream(WCHAR const *pwcsName,
  555. void *reserved1,
  556. DWORD grfMode,
  557. DWORD reserved2,
  558. IStream **ppstm)
  559. {
  560. SCODE sc;
  561. SAFE_SEM;
  562. SAFE_ACCESS;
  563. SafeCExposedStream pestm;
  564. CDfName dfn;
  565. olDebugOut((DEB_TRACE, "In CExposedDocFile::OpenStream("
  566. "%ws, %p, %lX, %lu, %p)\n", pwcsName, reserved1,
  567. grfMode, reserved2, ppstm));
  568. olLog(("%p::In CExposedDocFile::OpenStream(%ws, %lu %lX, %lu, %p)\n",
  569. this, pwcsName, reserved1, grfMode, reserved2, ppstm));
  570. OL_VALIDATE(OpenStream(pwcsName,
  571. reserved1,
  572. grfMode,
  573. reserved2,
  574. ppstm));
  575. olChk(Validate());
  576. BEGIN_PENDING_LOOP;
  577. olChk(TakeSafeSem());
  578. SafeReadAccess();
  579. dfn.Set(pwcsName);
  580. sc = OpenEntry(&dfn, STGTY_STREAM, grfMode, (void **)&pestm);
  581. END_PENDING_LOOP;
  582. if (SUCCEEDED(sc))
  583. TRANSFER_INTERFACE(pestm, IStream, ppstm);
  584. olDebugOut((DEB_TRACE, "Out CExposedDocFile::OpenStream => %p\n",
  585. *ppstm));
  586. EH_Err:
  587. olLog(("%p::Out CExposedDocFile::OpenStream(). "
  588. "*ppstm == %p, ret == %lx\n", this, *ppstm, sc));
  589. return _OLERETURN(sc);
  590. }
  591. //+--------------------------------------------------------------
  592. //
  593. // Member: CExposedDocFile::CreateStorage, public
  594. //
  595. // Synopsis: Creates an embedded DocFile
  596. //
  597. // Arguments: [pwcsName] - Name
  598. // [grfMode] - Permissions
  599. // [reserved1]
  600. // [reserved2]
  601. // [ppstg] - New DocFile return
  602. //
  603. // Returns: Appropriate status code
  604. //
  605. // Modifies: [ppstg]
  606. //
  607. // History: 20-Jan-92 DrewB Created
  608. //
  609. //---------------------------------------------------------------
  610. _OLESTDMETHODIMP CExposedDocFile::CreateStorage(
  611. WCHAR const *pwcsName,
  612. DWORD grfMode,
  613. DWORD reserved1,
  614. LPSTGSECURITY reserved2,
  615. IStorage **ppstg)
  616. {
  617. SCODE sc;
  618. SAFE_SEM;
  619. SAFE_ACCESS;
  620. SafeCExposedDocFile pedf;
  621. CDfName dfn;
  622. olLog(("%p::In CExposedDocFile::CreateStorage(%ws, %lX, %lu, %lu, %p)\n",
  623. this, pwcsName, grfMode, reserved1, reserved2, ppstg));
  624. olDebugOut((DEB_TRACE, "In CExposedDocFile::CreateStorage:%p("
  625. "%ws, %lX, %lu, %lu, %p)\n", this, pwcsName, grfMode,
  626. reserved1, reserved2, ppstg));
  627. OL_VALIDATE(CreateStorage(pwcsName,
  628. grfMode,
  629. reserved1,
  630. reserved2,
  631. ppstg));
  632. olChk(Validate());
  633. BEGIN_PENDING_LOOP;
  634. olChk(TakeSafeSem());
  635. olChk(CheckCopyTo());
  636. SafeWriteAccess();
  637. #ifdef DIRECTWRITERLOCK
  638. olChk(ValidateWriteAccess());
  639. #endif
  640. dfn.Set(pwcsName);
  641. sc = CreateEntry(&dfn, STGTY_STORAGE, grfMode, (void **)&pedf);
  642. END_PENDING_LOOP;
  643. if (SUCCEEDED(sc))
  644. TRANSFER_INTERFACE(pedf, IStorage, ppstg);
  645. olDebugOut((DEB_TRACE, "Out CExposedDocFile::CreateStorage => %p\n",
  646. *ppstg));
  647. EH_Err:
  648. olLog(("%p::Out CExposedDocFile::CreateStorage(). "
  649. "*ppstg == %p, ret == %lX\n", this, *ppstg, sc));
  650. return _OLERETURN(sc);
  651. }
  652. //+--------------------------------------------------------------
  653. //
  654. // Member: CExposedDocFile::OpenStorage, public
  655. //
  656. // Synopsis: Gets an existing embedded DocFile
  657. //
  658. // Arguments: [pwcsName] - Name
  659. // [pstgPriority] - Priority reopens
  660. // [grfMode] - Permissions
  661. // [snbExclude] - Priority reopens
  662. // [reserved]
  663. // [ppstg] - DocFile return
  664. //
  665. // Returns: Appropriate status code
  666. //
  667. // Modifies: [ppstg]
  668. //
  669. // History: 20-Jan-92 DrewB Created
  670. //
  671. //---------------------------------------------------------------
  672. _OLESTDMETHODIMP CExposedDocFile::OpenStorage(WCHAR const *pwcsName,
  673. IStorage *pstgPriority,
  674. DWORD grfMode,
  675. SNBW snbExclude,
  676. DWORD reserved,
  677. IStorage **ppstg)
  678. {
  679. SCODE sc;
  680. SAFE_SEM;
  681. SAFE_ACCESS;
  682. SafeCExposedDocFile pdfExp;
  683. CDfName dfn;
  684. olLog(("%p::In CExposedDocFile::OpenStorage(%ws, %p, %lX, %p, %lu, %p)\n",
  685. this, pwcsName, pstgPriority, grfMode, snbExclude, reserved,
  686. ppstg));
  687. olDebugOut((DEB_TRACE, "In CExposedDocFile::OpenStorage:%p("
  688. "%ws, %p, %lX, %p, %lu, %p)\n", this, pwcsName, pstgPriority,
  689. grfMode, snbExclude, reserved, ppstg));
  690. OL_VALIDATE(OpenStorage(pwcsName,
  691. pstgPriority,
  692. grfMode,
  693. snbExclude,
  694. reserved,
  695. ppstg));
  696. olChk(Validate());
  697. if (snbExclude != NULL)
  698. olErr(EH_Err, STG_E_INVALIDPARAMETER);
  699. BEGIN_PENDING_LOOP;
  700. olChk(TakeSafeSem());
  701. SafeReadAccess();
  702. dfn.Set(pwcsName);
  703. sc = OpenEntry(&dfn, STGTY_STORAGE, grfMode, (void **)&pdfExp);
  704. END_PENDING_LOOP;
  705. if (SUCCEEDED(sc))
  706. {
  707. TRANSFER_INTERFACE(pdfExp, IStorage, ppstg);
  708. }
  709. olDebugOut((DEB_TRACE, "Out CExposedDocFile::OpenStorage => %p\n",
  710. *ppstg));
  711. EH_Err:
  712. olLog(("%p::Out CExposedDocFile::OpenStorage(). "
  713. "*ppstg == %p, ret == %lX\n", this, *ppstg, sc));
  714. return _OLERETURN(sc);
  715. }
  716. //+---------------------------------------------------------------------------
  717. //
  718. // Member: CExposedDocFile::MakeCopyFlags, public
  719. //
  720. // Synopsis: Translates IID array into bit fields
  721. //
  722. // Arguments: [ciidExclude] - Count of IIDs
  723. // [rgiidExclude] - IIDs not to copy
  724. //
  725. // Returns: Appropriate status code
  726. //
  727. // History: 23-Dec-92 DrewB Created
  728. //
  729. //----------------------------------------------------------------------------
  730. DWORD CExposedDocFile::MakeCopyFlags(DWORD ciidExclude,
  731. IID const *rgiidExclude)
  732. {
  733. DWORD dwCopyFlags;
  734. olDebugOut((DEB_ITRACE, "In CExposedDocFile::MakeCopyFlags(%lu, %p)\n",
  735. ciidExclude, rgiidExclude));
  736. // Copy everything by default
  737. dwCopyFlags = COPY_ALL;
  738. for (; ciidExclude > 0; ciidExclude--, rgiidExclude++)
  739. if (IsEqualIID(*rgiidExclude, IID_IStorage))
  740. dwCopyFlags &= ~COPY_STORAGES;
  741. else if (IsEqualIID(*rgiidExclude, IID_IStream))
  742. dwCopyFlags &= ~COPY_STREAMS;
  743. olDebugOut((DEB_ITRACE, "Out CExposedDocFile::MakeCopyFlags\n"));
  744. return dwCopyFlags;
  745. }
  746. //+--------------------------------------------------------------
  747. //
  748. // Member: CExposedDocFile::CopyTo, public
  749. //
  750. // Synopsis: Makes a copy of a DocFile
  751. //
  752. // Arguments: [ciidExclude] - Length of rgiid array
  753. // [rgiidExclude] - Array of IIDs to exclude
  754. // [snbExclude] - Names to exclude
  755. // [pstgDest] - Parent of copy
  756. //
  757. // Returns: Appropriate status code
  758. //
  759. // History: 20-Jan-92 DrewB Created
  760. //
  761. //---------------------------------------------------------------
  762. _OLESTDMETHODIMP CExposedDocFile::CopyTo(DWORD ciidExclude,
  763. IID const *rgiidExclude,
  764. SNBW snbExclude,
  765. IStorage *pstgDest)
  766. {
  767. SCODE sc;
  768. #ifdef MULTIHEAP
  769. CSafeMultiHeap smh(_ppc);
  770. #endif
  771. SAFE_SEM;
  772. DWORD i;
  773. olLog(("%p::In CExposedDocFile::CopyTo(%lu, %p, %p, %p)\n",
  774. this, ciidExclude, rgiidExclude, snbExclude, pstgDest));
  775. olDebugOut((DEB_TRACE, "In CExposedDocFile::CopyTo(%lu, %p, %p, %p)\n",
  776. ciidExclude, rgiidExclude, snbExclude, pstgDest));
  777. OL_VALIDATE(CopyTo(ciidExclude,
  778. rgiidExclude,
  779. snbExclude,
  780. pstgDest));
  781. olChk(Validate());
  782. BEGIN_PENDING_LOOP;
  783. olChk(TakeSafeSem());
  784. olChk(_pdf->CheckReverted());
  785. olAssert(_pdfb->GetCopyBase() == NULL);
  786. _pdfb->SetCopyBase(BP_TO_P(CPubDocFile *, _pdf));
  787. // Flush all descendant property set buffers so that their
  788. // underlying Streams (which are about to be copied) are
  789. // up to date.
  790. SetWriteAccess();
  791. olChkTo(EH_Loop, _pdf->FlushBufferedData(0));
  792. ClearWriteAccess();
  793. // Perform the copy.
  794. sc = CopyDocFileToIStorage(_pdf->GetDF(), pstgDest, snbExclude,
  795. MakeCopyFlags(ciidExclude, rgiidExclude));
  796. EH_Loop:
  797. _pdfb->SetCopyBase(NULL);
  798. END_PENDING_LOOP;
  799. olDebugOut((DEB_TRACE, "Out CExposedDocFile::CopyTo\n"));
  800. EH_Err:
  801. _pdfb->SetCopyBase(NULL);
  802. olLog(("%p::Out ExposedDocFile::CopyTo(). ret == %lX\n", this, sc));
  803. return _OLERETURN(sc);
  804. }
  805. //+--------------------------------------------------------------
  806. //
  807. // Member: CExposedDocFile::Commit, public
  808. //
  809. // Synopsis: Commits transacted changes
  810. //
  811. // Arguments: [dwFlags] - DFC_*
  812. //
  813. // Returns: Appropriate status code
  814. //
  815. // History: 20-Jan-92 DrewB Created
  816. //
  817. //---------------------------------------------------------------
  818. STDMETHODIMP CExposedDocFile::Commit(DWORD dwFlags)
  819. {
  820. SCODE sc;
  821. SAFE_SEM;
  822. SAFE_ACCESS;
  823. olLog(("%p::In CExposedDocFile::Commit(%lX)\n",this, dwFlags));
  824. olDebugOut((DEB_TRACE, "In CExposedDocFile::Commit(%lX)\n", dwFlags));
  825. OL_VALIDATE(Commit(dwFlags));
  826. olChk(Validate());
  827. BEGIN_PENDING_LOOP;
  828. olChk( _PropertyBagEx.Commit( dwFlags ));
  829. olChk(TakeSafeSem());
  830. SafeWriteAccess();
  831. #ifdef DIRECTWRITERLOCK
  832. if (_pdf->GetTransactedDepth() <= 1) // topmost transacted level or direct
  833. {
  834. if (_pdfb->DirectWriterMode() && (*_ppc->GetRecursionCount()) == 0)
  835. olChk(STG_E_ACCESSDENIED);
  836. }
  837. #endif
  838. olChkTo(EH_Loop, _pdf->Commit(dwFlags));
  839. #if WIN32 >= 300
  840. if (SUCCEEDED(sc) && _pIAC != NULL)
  841. sc = _pIAC->CommitAccessRights(0);
  842. #endif
  843. EH_Loop:
  844. END_PENDING_LOOP;
  845. olDebugOut((DEB_TRACE, "Out CExposedDocFile::Commit\n"));
  846. EH_Err:
  847. olLog(("%p::Out CExposedDocFile::Commit(). ret == %lx\n",this, sc));
  848. return ResultFromScode(sc);
  849. }
  850. //+--------------------------------------------------------------
  851. //
  852. // Member: CExposedDocFile::Revert, public
  853. //
  854. // Synopsis: Reverts transacted changes
  855. //
  856. // Returns: Appropriate status code
  857. //
  858. // History: 20-Jan-92 DrewB Created
  859. //
  860. //---------------------------------------------------------------
  861. STDMETHODIMP CExposedDocFile::Revert(void)
  862. {
  863. SCODE sc;
  864. SAFE_SEM;
  865. SAFE_ACCESS;
  866. olLog(("%p::In CExposedDocFile::Revert()\n", this));
  867. olDebugOut((DEB_TRACE, "In CExposedDocFile::Revert\n"));
  868. OL_VALIDATE(Revert());
  869. olChk(Validate());
  870. BEGIN_PENDING_LOOP;
  871. olChk(TakeSafeSem());
  872. SafeWriteAccess();
  873. olChkTo(EH_Loop, _pdf->Revert());
  874. #if WIN32 >= 300
  875. if (SUCCEEDED(sc) && _pIAC != NULL)
  876. sc = _pIAC->RevertAccessRights();
  877. #endif
  878. EH_Loop:
  879. END_PENDING_LOOP;
  880. olDebugOut((DEB_TRACE, "Out CExposedDocFile::Revert\n"));
  881. EH_Err:
  882. olLog(("%p::Out CExposedDocFile::Revert(). ret == %lx\n", this, sc));
  883. return ResultFromScode(sc);
  884. }
  885. //+--------------------------------------------------------------
  886. //
  887. // Member: CExposedDocFile::EnumElements, public
  888. //
  889. // Synopsis: Starts an iterator
  890. //
  891. // Arguments: [reserved1]
  892. // [reserved2]
  893. // [reserved3]
  894. // [ppenm] - Enumerator return
  895. //
  896. // Returns: Appropriate status code
  897. //
  898. // Modifies: [ppenm]
  899. //
  900. // History: 20-Jan-92 DrewB Created
  901. //
  902. //---------------------------------------------------------------
  903. STDMETHODIMP CExposedDocFile::EnumElements(DWORD reserved1,
  904. void *reserved2,
  905. DWORD reserved3,
  906. IEnumSTATSTG **ppenm)
  907. {
  908. SCODE sc;
  909. SAFE_SEM;
  910. SAFE_ACCESS;
  911. SafeCExposedIterator pdiExp;
  912. CDfName dfnTmp;
  913. olLog(("%p::In CExposedDocFile::EnumElements(%lu, %p, %lu, %p)\n",
  914. this, reserved1, reserved2, reserved3, ppenm));
  915. olDebugOut((DEB_TRACE, "In CExposedDocFile::EnumElements(%p)\n",
  916. ppenm));
  917. OL_VALIDATE(EnumElements(reserved1,
  918. reserved2,
  919. reserved3,
  920. ppenm));
  921. olChk(Validate());
  922. //ASYNC Note: It doesn't appear that there's any way that this
  923. // function can fail with STG_E_PENDING, so we don't need a pending
  924. // loop here.
  925. olChk(TakeSafeSem());
  926. if (!P_READ(_pdf->GetDFlags()))
  927. olErr(EH_Err, STG_E_ACCESSDENIED);
  928. olChk(_pdf->CheckReverted());
  929. SafeReadAccess();
  930. pdiExp.Attach(new CExposedIterator(BP_TO_P(CPubDocFile *, _pdf),
  931. &dfnTmp,
  932. BP_TO_P(CDFBasis *, _pdfb),
  933. _ppc));
  934. olMem((CExposedIterator *)pdiExp);
  935. _ppc->AddRef();
  936. #ifdef ASYNC
  937. if (_cpoint.IsInitialized())
  938. {
  939. olChkTo(EH_Exp, pdiExp->InitConnection(&_cpoint));
  940. }
  941. #endif
  942. TRANSFER_INTERFACE(pdiExp, IEnumSTATSTG, ppenm);
  943. olDebugOut((DEB_TRACE, "Out CExposedDocFile::EnumElements => %p\n",
  944. *ppenm));
  945. EH_Err:
  946. olLog(("%p::Out CExposedDocFile::EnumElements(). ret == %lx\n",this, sc));
  947. return ResultFromScode(sc);
  948. EH_Exp:
  949. pdiExp->Release();
  950. return ResultFromScode(sc);
  951. }
  952. //+--------------------------------------------------------------
  953. //
  954. // Member: CExposedDocFile::DestroyElement, public
  955. //
  956. // Synopsis: Permanently deletes an element of a DocFile
  957. //
  958. // Arguments: [pwcsName] - Name of element
  959. //
  960. // Returns: Appropriate status code
  961. //
  962. // History: 20-Jan-92 DrewB Created
  963. //
  964. //---------------------------------------------------------------
  965. _OLESTDMETHODIMP CExposedDocFile::DestroyElement(WCHAR const *pwcsName)
  966. {
  967. SCODE sc;
  968. SAFE_SEM;
  969. SAFE_ACCESS;
  970. CDfName dfn;
  971. olLog(("%p::In CExposedDocFile::DestroyElement(%ws)\n", this, pwcsName));
  972. olDebugOut((DEB_TRACE, "In CExposedDocFile::DestroyElement(%ws)\n",
  973. pwcsName));
  974. OL_VALIDATE(DestroyElement(pwcsName));
  975. olChk(Validate());
  976. dfn.Set(pwcsName);
  977. BEGIN_PENDING_LOOP;
  978. olChk(TakeSafeSem());
  979. SafeWriteAccess();
  980. #ifdef DIRECTWRITERLOCK
  981. olChk(ValidateWriteAccess());
  982. #endif
  983. sc = _pdf->DestroyEntry(&dfn, FALSE);
  984. END_PENDING_LOOP;
  985. olDebugOut((DEB_TRACE, "Out CExposedDocFile::DestroyElement\n"));
  986. EH_Err:
  987. olLog(("%p::Out CExposedDocFile::DestroyElement(). ret == %lx\n",
  988. this, sc));
  989. return _OLERETURN(sc);
  990. }
  991. _OLESTDMETHODIMP CExposedDocFile::MoveElementTo(WCHAR const *pwcsName,
  992. IStorage *pstgParent,
  993. OLECHAR const *ptcsNewName,
  994. DWORD grfFlags)
  995. {
  996. SCODE sc;
  997. #ifdef MULTIHEAP
  998. CSafeMultiHeap smh(_ppc);
  999. #endif
  1000. olLog(("%p::In CExposedDocFile::MoveElementTo("
  1001. "%ws, %p, " OLEFMT ", %lu)\n",
  1002. this, pwcsName, pstgParent, ptcsNewName, grfFlags));
  1003. olDebugOut((DEB_TRACE, "In CExposedDocFile::MoveElementTo("
  1004. "%ws, %p, " OLEFMT ", %lu)\n",
  1005. pwcsName, pstgParent, ptcsNewName, grfFlags));
  1006. OL_VALIDATE(MoveElementTo(pwcsName,
  1007. pstgParent,
  1008. ptcsNewName,
  1009. grfFlags));
  1010. olChk(Validate());
  1011. #ifdef ASYNC
  1012. //ASYNC Note: We don't use the normal pending loop macros here because
  1013. // we have no safe sem and need to pass a NULL.
  1014. do
  1015. {
  1016. #endif
  1017. sc = MoveElementWorker(pwcsName,
  1018. pstgParent,
  1019. ptcsNewName,
  1020. grfFlags);
  1021. #ifdef ASYNC
  1022. if (!ISPENDINGERROR(sc))
  1023. {
  1024. break;
  1025. }
  1026. else
  1027. {
  1028. SCODE sc2;
  1029. sc2 = _cpoint.Notify(sc,
  1030. _ppc->GetBase(),
  1031. _ppc,
  1032. NULL);
  1033. if (sc2 != S_OK)
  1034. {
  1035. return ResultFromScode(sc2);
  1036. }
  1037. }
  1038. } while (TRUE);
  1039. #endif
  1040. EH_Err:
  1041. olLog(("%p::Out CExposedDocFile::MoveElementTo(). ret == %lx\n",
  1042. this, sc));
  1043. return _OLERETURN(sc);
  1044. }
  1045. //+--------------------------------------------------------------
  1046. //
  1047. // Member: CExposedDocFile::MoveElementToWorker, public
  1048. //
  1049. // Synopsis: Move an element of a DocFile to an IStorage
  1050. //
  1051. // Arguments: [pwcsName] - Current name
  1052. // [ptcsNewName] - New name
  1053. //
  1054. // Returns: Appropriate status code
  1055. //
  1056. // Algorithm: Open source as storage or stream (whatever works)
  1057. // Create appropriate destination
  1058. // Copy source to destination
  1059. // Set create time of destination equal to create time of source
  1060. // If appropriate, delete source
  1061. //
  1062. // History: 10-Nov-92 AlexT Created
  1063. //
  1064. //---------------------------------------------------------------
  1065. SCODE CExposedDocFile::MoveElementWorker(WCHAR const *pwcsName,
  1066. IStorage *pstgParent,
  1067. OLECHAR const *ptcsNewName,
  1068. DWORD grfFlags)
  1069. {
  1070. IUnknown *punksrc = NULL;
  1071. SCODE sc;
  1072. IUnknown *punkdst = NULL;
  1073. IStorage *pstgsrc;
  1074. STATSTG statstg;
  1075. BOOL fCreate = TRUE;
  1076. // Determine source type
  1077. sc = GetScode(OpenStorage(pwcsName, NULL,
  1078. STGM_DIRECT | STGM_READ | STGM_SHARE_EXCLUSIVE,
  1079. NULL, NULL, &pstgsrc));
  1080. if (SUCCEEDED(sc))
  1081. {
  1082. HRESULT hr;
  1083. // It's a storage
  1084. punksrc = pstgsrc;
  1085. IStorage *pstgdst;
  1086. olHChkTo(EH_UnkSrc, pstgsrc->Stat(&statstg, STATFLAG_NONAME));
  1087. hr = pstgParent->CreateStorage(ptcsNewName,
  1088. STGM_DIRECT | STGM_WRITE |
  1089. STGM_SHARE_EXCLUSIVE | STGM_FAILIFTHERE,
  1090. 0, 0, &pstgdst);
  1091. if (DfGetScode(hr) == STG_E_FILEALREADYEXISTS &&
  1092. grfFlags == STGMOVE_COPY)
  1093. {
  1094. fCreate = FALSE;
  1095. //If we're opening an existing thing for merging, we need
  1096. // read and write permissions so we can traverse the tree.
  1097. hr = pstgParent->OpenStorage(ptcsNewName, NULL,
  1098. STGM_DIRECT | STGM_READWRITE |
  1099. STGM_SHARE_EXCLUSIVE, NULL,
  1100. 0, &pstgdst);
  1101. }
  1102. olHChkTo(EH_UnkSrc, hr);
  1103. punkdst = pstgdst;
  1104. sc = DfGetScode(pstgsrc->CopyTo(0, NULL, NULL, pstgdst));
  1105. }
  1106. else if (sc == STG_E_FILENOTFOUND)
  1107. {
  1108. // Try opening it as a stream
  1109. HRESULT hr;
  1110. IStream *pstmsrc, *pstmdst;
  1111. olHChk(OpenStream(pwcsName, NULL,
  1112. STGM_DIRECT | STGM_READ | STGM_SHARE_EXCLUSIVE,
  1113. NULL, &pstmsrc));
  1114. // It's a stream
  1115. punksrc = pstmsrc;
  1116. olHChkTo(EH_UnkSrc, pstmsrc->Stat(&statstg, STATFLAG_NONAME));
  1117. hr = pstgParent->CreateStream(ptcsNewName,
  1118. STGM_DIRECT | STGM_WRITE |
  1119. STGM_SHARE_EXCLUSIVE |
  1120. STGM_FAILIFTHERE,
  1121. 0, 0, &pstmdst);
  1122. if (DfGetScode(hr) == STG_E_FILEALREADYEXISTS &&
  1123. grfFlags == STGMOVE_COPY)
  1124. {
  1125. ULARGE_INTEGER uli;
  1126. uli.QuadPart = 0;
  1127. fCreate = FALSE;
  1128. //Try to open it instead
  1129. //Note: We do this instead of doing a CreateStream with
  1130. // STGM_CREATE because CreateStream can open over an already
  1131. // open stream, which leads to problems when the destination
  1132. // and the source are the same.
  1133. olHChkTo(EH_UnkSrc, pstgParent->OpenStream(ptcsNewName,
  1134. 0,
  1135. STGM_DIRECT |
  1136. STGM_WRITE |
  1137. STGM_SHARE_EXCLUSIVE,
  1138. 0,
  1139. &pstmdst));
  1140. sc = pstmdst->SetSize(uli);
  1141. if (FAILED(sc))
  1142. {
  1143. pstmdst->Release();
  1144. olErr(EH_UnkSrc, sc);
  1145. }
  1146. }
  1147. else
  1148. olHChkTo(EH_UnkSrc, hr);
  1149. punkdst = pstmdst;
  1150. ULARGE_INTEGER cb;
  1151. ULISetLow (cb, 0xffffffff);
  1152. ULISetHigh(cb, 0xffffffff);
  1153. sc = DfGetScode(pstmsrc->CopyTo(pstmdst, cb, NULL, NULL));
  1154. }
  1155. else
  1156. olChk(sc);
  1157. punkdst->Release();
  1158. if (SUCCEEDED(sc))
  1159. {
  1160. // Make destination create time match source create time
  1161. // Note that we don't really care if this call succeeded.
  1162. pstgParent->SetElementTimes(ptcsNewName, &statstg.ctime,
  1163. NULL, NULL);
  1164. //OK to ignore failure from DestroyElement
  1165. if ((grfFlags & STGMOVE_COPY) == STGMOVE_MOVE)
  1166. DestroyElement(pwcsName);
  1167. }
  1168. else
  1169. {
  1170. // The copy/move failed, so get rid of the partial result.
  1171. //Only do a delete if the object was newly created.
  1172. if (fCreate)
  1173. pstgParent->DestroyElement(ptcsNewName);
  1174. }
  1175. olDebugOut((DEB_TRACE, "Out CExposedDocFile::MoveElementTo\n"));
  1176. // Fall through
  1177. EH_UnkSrc:
  1178. if (punksrc)
  1179. punksrc->Release();
  1180. EH_Err:
  1181. return sc;
  1182. }
  1183. //+--------------------------------------------------------------
  1184. //
  1185. // Member: CExposedDocFile::RenameElement, public
  1186. //
  1187. // Synopsis: Renames an element of a DocFile
  1188. //
  1189. // Arguments: [pwcsName] - Current name
  1190. // [pwcsNewName] - New name
  1191. //
  1192. // Returns: Appropriate status code
  1193. //
  1194. // History: 20-Jan-92 DrewB Created
  1195. //
  1196. //---------------------------------------------------------------
  1197. _OLESTDMETHODIMP CExposedDocFile::RenameElement(WCHAR const *pwcsName,
  1198. WCHAR const *pwcsNewName)
  1199. {
  1200. SCODE sc;
  1201. SAFE_SEM;
  1202. SAFE_ACCESS;
  1203. CDfName dfnOld, dfnNew;
  1204. olLog(("%p::In CExposedDocFile::RenameElement(%ws, %ws)\n",
  1205. this, pwcsName, pwcsNewName));
  1206. olDebugOut((DEB_TRACE, "In CExposedDocFile::RenameElement(%ws, %ws)\n",
  1207. pwcsName, pwcsNewName));
  1208. OL_VALIDATE(RenameElement(pwcsName,
  1209. pwcsNewName));
  1210. olChk(Validate());
  1211. dfnOld.Set(pwcsName);
  1212. dfnNew.Set(pwcsNewName);
  1213. BEGIN_PENDING_LOOP;
  1214. olChk(TakeSafeSem());
  1215. SafeWriteAccess();
  1216. #ifdef DIRECTWRITERLOCK
  1217. olChk(ValidateWriteAccess());
  1218. #endif
  1219. sc = _pdf->RenameEntry(&dfnOld, &dfnNew);
  1220. END_PENDING_LOOP;
  1221. olDebugOut((DEB_TRACE, "Out CExposedDocFile::RenameElement\n"));
  1222. EH_Err:
  1223. olLog(("%p::Out CExposedDocFile::RenameElement(). ret == %lx\n",
  1224. this, sc));
  1225. return _OLERETURN(sc);
  1226. }
  1227. //+--------------------------------------------------------------
  1228. //
  1229. // Member: CExposedDocFile::SetElementTimes, public
  1230. //
  1231. // Synopsis: Sets element time stamps
  1232. //
  1233. // Arguments: [pwcsName] - Name
  1234. // [pctime] - create time
  1235. // [patime] - access time
  1236. // [pmtime] - modify time
  1237. //
  1238. // Returns: Appropriate status code
  1239. //
  1240. // History: 05-Oct-92 AlexT Created
  1241. //
  1242. //---------------------------------------------------------------
  1243. _OLESTDMETHODIMP CExposedDocFile::SetElementTimes(WCHAR const *pwcsName,
  1244. FILETIME const *pctime,
  1245. FILETIME const *patime,
  1246. FILETIME const *pmtime)
  1247. {
  1248. SCODE sc;
  1249. SAFE_SEM;
  1250. SAFE_ACCESS;
  1251. CDfName dfn;
  1252. CDfName *pdfn = NULL;
  1253. FILETIME ctime, atime, mtime;
  1254. olLog(("%p::In CExposedDocFile::SetElementTimes(%ws, %p, %p, %p)\n",
  1255. this, pwcsName, pctime, patime, pmtime));
  1256. olDebugOut((DEB_TRACE, "In CExposedDocFile::SetElementTimes:%p("
  1257. "%ws, %p, %p, %p)\n", this, pwcsName, pctime, patime, pmtime));
  1258. OL_VALIDATE(SetElementTimes(pwcsName,
  1259. pctime,
  1260. patime,
  1261. pmtime));
  1262. olChk(Validate());
  1263. // Probe arguments and make local copies if necessary
  1264. if (pctime)
  1265. {
  1266. ctime = *pctime;
  1267. pctime = &ctime;
  1268. }
  1269. if (patime)
  1270. {
  1271. atime = *patime;
  1272. patime = &atime;
  1273. }
  1274. if (pmtime)
  1275. {
  1276. mtime = *pmtime;
  1277. pmtime = &mtime;
  1278. }
  1279. if (pwcsName != NULL)
  1280. {
  1281. dfn.Set(pwcsName);
  1282. pdfn = &dfn;
  1283. }
  1284. BEGIN_PENDING_LOOP;
  1285. olChk(TakeSafeSem());
  1286. SafeWriteAccess();
  1287. #ifdef DIRECTWRITERLOCK
  1288. olChk(ValidateWriteAccess());
  1289. #endif
  1290. sc = _pdf->SetElementTimes(pdfn, pctime, patime, pmtime);
  1291. END_PENDING_LOOP;
  1292. olDebugOut((DEB_TRACE, "Out CExposedDocFile::SetElementTimes\n"));
  1293. EH_Err:
  1294. olLog(("%p::Out CExposedDocFile::SetElementTimes(). ret == %lx\n",
  1295. this, sc));
  1296. return _OLERETURN(sc);
  1297. }
  1298. //+--------------------------------------------------------------
  1299. //
  1300. // Member: CExposedDocFile::SetClass, public
  1301. //
  1302. // Synopsis: Sets storage class
  1303. //
  1304. // Arguments: [clsid] - class id
  1305. //
  1306. // Returns: Appropriate status code
  1307. //
  1308. // History: 05-Oct-92 AlexT Created
  1309. //
  1310. //---------------------------------------------------------------
  1311. STDMETHODIMP CExposedDocFile::SetClass(REFCLSID rclsid)
  1312. {
  1313. SCODE sc;
  1314. SAFE_SEM;
  1315. SAFE_ACCESS;
  1316. CLSID clsid;
  1317. olLog(("%p::In CExposedDocFile::SetClass(?)\n", this));
  1318. olDebugOut((DEB_TRACE, "In CExposedDocFile::SetClass:%p(?)\n", this));
  1319. OL_VALIDATE(SetClass(rclsid));
  1320. olChk(Validate());
  1321. clsid = rclsid;
  1322. BEGIN_PENDING_LOOP;
  1323. olChk(TakeSafeSem());
  1324. SafeWriteAccess();
  1325. #ifdef DIRECTWRITERLOCK
  1326. olChk(ValidateWriteAccess());
  1327. #endif
  1328. sc = _pdf->SetClass(clsid);
  1329. END_PENDING_LOOP;
  1330. olDebugOut((DEB_TRACE, "Out CExposedDocFile::SetClass\n"));
  1331. EH_Err:
  1332. olLog(("%p::Out CExposedDocFile::SetClass(). ret == %lx\n",
  1333. this, sc));
  1334. return ResultFromScode(sc);
  1335. }
  1336. //+--------------------------------------------------------------
  1337. //
  1338. // Member: CExposedDocFile::SetStateBits, public
  1339. //
  1340. // Synopsis: Sets state bits
  1341. //
  1342. // Arguments: [grfStateBits] - state bits
  1343. // [grfMask] - state bits mask
  1344. //
  1345. // Returns: Appropriate status code
  1346. //
  1347. // History: 05-Oct-92 AlexT Created
  1348. //
  1349. //---------------------------------------------------------------
  1350. STDMETHODIMP CExposedDocFile::SetStateBits(DWORD grfStateBits, DWORD grfMask)
  1351. {
  1352. SCODE sc;
  1353. SAFE_SEM;
  1354. SAFE_ACCESS;
  1355. olLog(("%p::In CExposedDocFile::SetStateBits(%lu, %lu)\n", this,
  1356. grfStateBits, grfMask));
  1357. olDebugOut((DEB_TRACE, "In CExposedDocFile::SetStateBits:%p("
  1358. "%lu, %lu)\n", this, grfStateBits, grfMask));
  1359. OL_VALIDATE(SetStateBits(grfStateBits, grfMask));
  1360. olChk(Validate());
  1361. BEGIN_PENDING_LOOP;
  1362. olChk(TakeSafeSem());
  1363. SafeWriteAccess();
  1364. #ifdef DIRECTWRITERLOCK
  1365. olChk(ValidateWriteAccess());
  1366. #endif
  1367. sc = _pdf->SetStateBits(grfStateBits, grfMask);
  1368. END_PENDING_LOOP;
  1369. olDebugOut((DEB_TRACE, "Out CExposedDocFile::SetStateBits\n"));
  1370. EH_Err:
  1371. olLog(("%p::Out CExposedDocFile::SetStateBits(). ret == %lx\n",
  1372. this, sc));
  1373. return ResultFromScode(sc);
  1374. }
  1375. //+--------------------------------------------------------------
  1376. //
  1377. // Member: CExposedDocFile::Stat, public
  1378. //
  1379. // Synopsis: Fills in a buffer of information about this object
  1380. //
  1381. // Arguments: [pstatstg] - Buffer
  1382. //
  1383. // Returns: Appropriate status code
  1384. //
  1385. // Modifies: [pstatstg]
  1386. //
  1387. // History: 24-Mar-92 DrewB Created
  1388. //
  1389. //---------------------------------------------------------------
  1390. _OLESTDMETHODIMP CExposedDocFile::Stat(STATSTGW *pstatstg, DWORD grfStatFlag)
  1391. {
  1392. SAFE_SEM;
  1393. SAFE_ACCESS;
  1394. SCODE sc;
  1395. STATSTGW stat;
  1396. olLog(("%p::In CExposedDocFile::Stat(%p)\n", this, pstatstg));
  1397. olDebugOut((DEB_TRACE, "In CExposedDocFile::Stat(%p)\n", pstatstg));
  1398. OL_VALIDATE(Stat(pstatstg, grfStatFlag));
  1399. BEGIN_PENDING_LOOP;
  1400. olChk(TakeSafeSem());
  1401. SafeReadAccess();
  1402. sc = _pdf->Stat(&stat, grfStatFlag);
  1403. END_PENDING_LOOP;
  1404. if (SUCCEEDED(sc))
  1405. {
  1406. TRY
  1407. {
  1408. *pstatstg = stat;
  1409. pstatstg->type = STGTY_STORAGE;
  1410. ULISet32(pstatstg->cbSize, 0);
  1411. pstatstg->grfLocksSupported = 0;
  1412. pstatstg->STATSTG_dwStgFmt = 0;
  1413. }
  1414. CATCH(CException, e)
  1415. {
  1416. UNREFERENCED_PARM(e);
  1417. if (stat.pwcsName)
  1418. TaskMemFree(stat.pwcsName);
  1419. }
  1420. END_CATCH
  1421. }
  1422. olDebugOut((DEB_TRACE, "Out CExposedDocFile::Stat\n"));
  1423. EH_Err:
  1424. olLog(("%p::Out CExposedDocFile::Stat(). *pstatstg == %p ret == %lx\n",
  1425. this, *pstatstg, sc));
  1426. return _OLERETURN(sc);
  1427. }
  1428. //+--------------------------------------------------------------
  1429. //
  1430. // Member: CExposedDocFile::AddRef, public
  1431. //
  1432. // Synopsis: Increments the ref count
  1433. //
  1434. // Returns: Appropriate status code
  1435. //
  1436. // History: 16-Mar-92 DrewB Created
  1437. //
  1438. //---------------------------------------------------------------
  1439. STDMETHODIMP_(ULONG) CExposedDocFile::AddRef(void)
  1440. {
  1441. ULONG ulRet;
  1442. olLog(("%p::In CExposedDocFile::AddRef()\n", this));
  1443. olDebugOut((DEB_TRACE, "In CExposedDocFile::AddRef()\n"));
  1444. if (FAILED(Validate()))
  1445. return 0;
  1446. InterlockedIncrement(&_cReferences);
  1447. ulRet = _cReferences;
  1448. olDebugOut((DEB_TRACE, "Out CExposedDocFile::AddRef %p()=> %lu\n",
  1449. this, _cReferences));
  1450. olLog(("%p::Out CExposedDocFile::AddRef(). ret == %lu\n", this, ulRet));
  1451. return ulRet;
  1452. }
  1453. //+--------------------------------------------------------------
  1454. //
  1455. // Member: CExposedDocFile::QueryInterface, public
  1456. //
  1457. // Synopsis: Returns an object for the requested interface
  1458. //
  1459. // Arguments: [iid] - Interface ID
  1460. // [ppvObj] - Object return
  1461. //
  1462. // Returns: Appropriate status code
  1463. //
  1464. // Modifies: [ppvObj]
  1465. //
  1466. // History: 26-Mar-92 DrewB Created
  1467. //
  1468. //---------------------------------------------------------------
  1469. STDMETHODIMP CExposedDocFile::QueryInterface(REFIID iid, void **ppvObj)
  1470. {
  1471. SCODE sc;
  1472. #ifdef MULTIHEAP
  1473. CSafeMultiHeap smh(_ppc);
  1474. #endif
  1475. olLog(("%p::In CExposedDocFile::QueryInterface(?, %p)\n",
  1476. this, ppvObj));
  1477. olDebugOut((DEB_TRACE, "In CExposedDocFile::QueryInterface(?, %p)\n",
  1478. ppvObj));
  1479. OL_VALIDATE(QueryInterface(iid, ppvObj));
  1480. olChk(Validate());
  1481. olChk(_pdf->CheckReverted());
  1482. sc = S_OK;
  1483. if (IsEqualIID(iid, IID_IStorage) || IsEqualIID(iid, IID_IUnknown))
  1484. {
  1485. *ppvObj = (IStorage *)this;
  1486. CExposedDocFile::AddRef();
  1487. }
  1488. else if (IsEqualIID(iid, IID_IMarshal))
  1489. {
  1490. if (P_PRIORITY(_pdf->GetDFlags()) && _pdf->IsRoot())
  1491. olErr(EH_Err, E_NOINTERFACE);
  1492. //If the ILockBytes we'd need to marshal doesn't support IMarshal
  1493. // then we want to do standard marshalling on the storage, mostly
  1494. // to prevent deadlock problems but also because you'll get better
  1495. // performance. So check, then do the right thing.
  1496. IMarshal *pim;
  1497. ILockBytes *plkb;
  1498. plkb = _ppc->GetOriginal();
  1499. if (plkb == NULL)
  1500. {
  1501. plkb = _ppc->GetBase();
  1502. }
  1503. sc = plkb->QueryInterface(IID_IMarshal, (void **)&pim);
  1504. if (FAILED(sc))
  1505. {
  1506. olErr(EH_Err, E_NOINTERFACE);
  1507. }
  1508. pim->Release();
  1509. #ifdef MULTIHEAP
  1510. if (_ppc->GetHeapBase() == NULL)
  1511. olErr (EH_Err, E_NOINTERFACE);
  1512. #endif
  1513. *ppvObj = (IMarshal *)this;
  1514. CExposedDocFile::AddRef();
  1515. }
  1516. else if (IsEqualIID(iid, IID_IRootStorage))
  1517. {
  1518. #ifdef COORD
  1519. if ((!_pdf->IsRoot()) && (!_pdf->IsCoord()))
  1520. #else
  1521. if (!_pdf->IsRoot())
  1522. #endif
  1523. olErr(EH_Err, E_NOINTERFACE);
  1524. *ppvObj = (IRootStorage *)this;
  1525. CExposedDocFile::AddRef();
  1526. }
  1527. #ifdef NEWPROPS
  1528. else if (IsEqualIID(iid, IID_IPropertySetStorage))
  1529. {
  1530. *ppvObj = (IPropertySetStorage *)this;
  1531. CExposedDocFile::AddRef();
  1532. }
  1533. #endif
  1534. #ifdef ASYNC
  1535. else if (IsEqualIID(iid, IID_IConnectionPointContainer) &&
  1536. _cpoint.IsInitialized())
  1537. {
  1538. *ppvObj = (IConnectionPointContainer *)this;
  1539. CExposedDocFile::AddRef();
  1540. }
  1541. #endif
  1542. #if WIN32 >= 300
  1543. else if (_pdf->IsRoot() && IsEqualIID(iid, IID_IAccessControl))
  1544. {
  1545. ILockBytes *piLB = _pdf->GetBaseMS()->GetILB();
  1546. olAssert((piLB != NULL));
  1547. SCODE scode = S_OK;
  1548. if (_pIAC == NULL) // use existing _pIAC if available
  1549. scode = piLB->QueryInterface(IID_IAccessControl,(void **)&_pIAC);
  1550. if (SUCCEEDED(scode))
  1551. {
  1552. *ppvObj = (IAccessControl *)this;
  1553. CExposedDocFile::AddRef();
  1554. }
  1555. else sc = E_NOINTERFACE;
  1556. }
  1557. #endif
  1558. #ifdef DIRECTWRITERLOCK
  1559. else if (_pdf->IsRoot() && IsEqualIID(iid, IID_IDirectWriterLock) &&
  1560. _pdfb->DirectWriterMode())
  1561. {
  1562. *ppvObj = (IDirectWriterLock *) this;
  1563. CExposedDocFile::AddRef();
  1564. }
  1565. #endif // DIRECTWRITERLOCK
  1566. else if( IsEqualIID( iid, IID_IPropertyBagEx ))
  1567. {
  1568. *ppvObj = static_cast<IPropertyBagEx*>(&_PropertyBagEx);
  1569. CExposedDocFile::AddRef();
  1570. }
  1571. else if( IsEqualIID( iid, IID_IPropertyBag ))
  1572. {
  1573. *ppvObj = static_cast<IPropertyBag*>(&_PropertyBagEx);
  1574. CExposedDocFile::AddRef();
  1575. }
  1576. else
  1577. sc = E_NOINTERFACE;
  1578. olDebugOut((DEB_TRACE, "Out CExposedDocFile::QueryInterface => %p\n",
  1579. *ppvObj));
  1580. EH_Err:
  1581. olLog(("%p::Out CExposedDocFile::QueryInterface(). "
  1582. "*ppvObj == %p ret == %lx\n", this, *ppvObj, sc));
  1583. return ResultFromScode(sc);
  1584. }
  1585. //+--------------------------------------------------------------
  1586. //
  1587. // Method: CExposedDocFile::CopySStreamToIStream, private
  1588. //
  1589. // Synopsis: Copies a PSStream to an IStream
  1590. //
  1591. // Arguments: [psstFrom] - SStream
  1592. // [pstTo] - IStream
  1593. //
  1594. // Returns: Appropriate status code
  1595. //
  1596. // History: 07-May-92 DrewB Created
  1597. // 26-Jun-92 AlexT Moved to CExposedDocFile
  1598. // so we can call SetReadAccess
  1599. //
  1600. //---------------------------------------------------------------
  1601. SCODE CExposedDocFile::CopySStreamToIStream(PSStream *psstFrom,
  1602. IStream *pstTo)
  1603. {
  1604. BYTE *pbBuffer;
  1605. SCODE sc;
  1606. #ifdef LARGE_STREAMS
  1607. ULONGLONG cbPos;
  1608. ULONG cbRead, cbWritten;
  1609. #else
  1610. ULONG cbRead, cbWritten, cbPos, cbSizeLow;
  1611. #endif
  1612. ULONG ulBufferSize;
  1613. ULARGE_INTEGER cbSize;
  1614. cbSize.QuadPart = 0;
  1615. ulBufferSize = (_pdfb->GetOpenFlags() & DF_LARGE) ?
  1616. LARGESTREAMBUFFERSIZE : STREAMBUFFERSIZE;
  1617. // This is part of CopyTo and therefore we are allowed to
  1618. // fail with out-of-memory
  1619. olChk(GetBuffer(STREAMBUFFERSIZE, ulBufferSize, &pbBuffer, &ulBufferSize));
  1620. // Set destination size for contiguity
  1621. SetReadAccess();
  1622. #ifdef LARGE_STREAMS
  1623. psstFrom->GetSize(&cbSize.QuadPart);
  1624. #else
  1625. psstFrom->GetSize(&cbSize.LowPart);
  1626. cbSize.HighPart = 0;
  1627. #endif
  1628. ClearReadAccess();
  1629. // Don't need to SetReadAccess here because pstTo is an IStream
  1630. olHChk(pstTo->SetSize(cbSize));
  1631. // Copy between streams
  1632. cbPos = 0;
  1633. for (;;)
  1634. {
  1635. SetReadAccess();
  1636. olChk(psstFrom->ReadAt(cbPos, pbBuffer, ulBufferSize,
  1637. (ULONG STACKBASED *)&cbRead));
  1638. ClearReadAccess();
  1639. if (cbRead == 0) // EOF
  1640. break;
  1641. // Don't need to SetReadAccess here because pstTo is an IStream
  1642. olHChk(pstTo->Write(pbBuffer, cbRead, &cbWritten));
  1643. if (cbRead != cbWritten)
  1644. olErr(EH_Err, STG_E_WRITEFAULT);
  1645. cbPos += cbWritten;
  1646. }
  1647. sc = S_OK;
  1648. EH_Err:
  1649. DfMemFree(pbBuffer);
  1650. return sc;
  1651. }
  1652. //+--------------------------------------------------------------
  1653. //
  1654. // Method: CExposedDocFile::CopyDocFileToIStorage, private
  1655. //
  1656. // Synopsis: Copies a docfile's contents to an IStorage
  1657. //
  1658. // Arguments: [pdfFrom] - From
  1659. // [pstgTo] - To
  1660. // [snbExclude] - Names to not copy
  1661. // [dwCopyFlags] - Bitwise flags for types of objects to copy
  1662. //
  1663. // Returns: Appropriate status code
  1664. //
  1665. // History: 07-May-92 DrewB Created
  1666. // 26-Jun-92 AlexT Moved to CExposedDocFile
  1667. // so we can call SetReadAccess
  1668. //
  1669. //---------------------------------------------------------------
  1670. // Variables used by CopyDocFileToIStorage that we
  1671. // want to allocate dynamically rather than eating stack space
  1672. struct SCopyVars : public CLocalAlloc
  1673. {
  1674. PSStream *psstFrom;
  1675. IStream *pstTo;
  1676. PDocFile *pdfFromChild;
  1677. IStorage *pstgToChild;
  1678. DWORD grfStateBits;
  1679. CLSID clsid;
  1680. CDfName dfnKey;
  1681. SIterBuffer ib;
  1682. OLECHAR atcName[CWCSTORAGENAME];
  1683. };
  1684. SCODE CExposedDocFile::CopyDocFileToIStorage(PDocFile *pdfFrom,
  1685. IStorage *pstgTo,
  1686. SNBW snbExclude,
  1687. DWORD dwCopyFlags)
  1688. {
  1689. SCODE sc;
  1690. SCopyVars *pcv = NULL;
  1691. olDebugOut((DEB_ITRACE, "In CopyDocFileToIStorage:%p(%p, %p, %p, %lX)\n",
  1692. this, pdfFrom, pstgTo, snbExclude, dwCopyFlags));
  1693. // Allocate variables dynamically to conserve stack space since
  1694. // this is a recursive call
  1695. olMem(pcv = new SCopyVars);
  1696. SetReadAccess();
  1697. sc = pdfFrom->GetClass(&pcv->clsid);
  1698. ClearReadAccess();
  1699. olChk(sc);
  1700. // Assume STG_E_INVALIDFUNCTION means that the destination storage
  1701. // doesn't support class IDs
  1702. sc = GetScode(pstgTo->SetClass(pcv->clsid));
  1703. if (FAILED(sc) && sc != STG_E_INVALIDFUNCTION)
  1704. olErr(EH_Err, sc);
  1705. SetReadAccess();
  1706. sc = pdfFrom->GetStateBits(&pcv->grfStateBits);
  1707. ClearReadAccess();
  1708. olChk(sc);
  1709. sc = GetScode(pstgTo->SetStateBits(pcv->grfStateBits, 0xffffffff));
  1710. if (FAILED(sc) && sc != STG_E_INVALIDFUNCTION)
  1711. olErr(EH_Err, sc);
  1712. for (;;)
  1713. {
  1714. SetReadAccess();
  1715. sc = pdfFrom->FindGreaterEntry(&pcv->dfnKey, &pcv->ib, NULL);
  1716. ClearReadAccess();
  1717. if (sc == STG_E_NOMOREFILES)
  1718. break;
  1719. else if (FAILED(sc))
  1720. olErr(EH_pdfi, sc);
  1721. pcv->dfnKey.Set(&pcv->ib.dfnName);
  1722. if (snbExclude && NameInSNB(&pcv->ib.dfnName, snbExclude) == S_OK)
  1723. continue;
  1724. if ((pcv->ib.type == STGTY_STORAGE &&
  1725. (dwCopyFlags & COPY_STORAGES) == 0) ||
  1726. (pcv->ib.type == STGTY_STREAM &&
  1727. (dwCopyFlags & COPY_STREAMS) == 0))
  1728. continue;
  1729. switch(pcv->ib.type)
  1730. {
  1731. case STGTY_STORAGE:
  1732. // Embedded DocFile, create destination and recurse
  1733. SetReadAccess();
  1734. sc = pdfFrom->GetDocFile(&pcv->ib.dfnName, DF_READ,
  1735. pcv->ib.type, &pcv->pdfFromChild);
  1736. ClearReadAccess();
  1737. olChkTo(EH_pdfi, sc);
  1738. // Not optimally efficient, but reduces #ifdef's
  1739. StringCbCopyW (pcv->atcName, sizeof(pcv->atcName), (WCHAR *)pcv->ib.dfnName.GetBuffer());
  1740. // Don't need to SetReadAccess here because pstgTo is an IStorage.
  1741. #ifdef MULTIHEAP
  1742. {
  1743. CSafeMultiHeap smh(_ppc);
  1744. // if pstgTo is an IStorage proxy, then returned IStorage
  1745. // can be custom marshaled and allocator state is lost
  1746. #endif
  1747. sc = DfGetScode(pstgTo->CreateStorage(pcv->atcName, STGM_WRITE |
  1748. STGM_SHARE_EXCLUSIVE |
  1749. STGM_FAILIFTHERE,
  1750. 0, 0, &pcv->pstgToChild));
  1751. if (sc == STG_E_FILEALREADYEXISTS)
  1752. //We need read and write permissions so we can traverse
  1753. // the destination IStorage
  1754. olHChkTo(EH_Get, pstgTo->OpenStorage(pcv->atcName, NULL,
  1755. STGM_READWRITE |
  1756. STGM_SHARE_EXCLUSIVE,
  1757. NULL, 0,
  1758. &pcv->pstgToChild));
  1759. else if (FAILED(sc))
  1760. olErr(EH_Get, sc);
  1761. #ifdef MULTIHEAP
  1762. }
  1763. #endif
  1764. olChkTo(EH_Create,
  1765. CopyDocFileToIStorage(pcv->pdfFromChild, pcv->pstgToChild,
  1766. NULL, dwCopyFlags));
  1767. pcv->pdfFromChild->Release();
  1768. pcv->pstgToChild->Release();
  1769. break;
  1770. case STGTY_STREAM:
  1771. SetReadAccess();
  1772. sc = pdfFrom->GetStream(&pcv->ib.dfnName, DF_READ,
  1773. pcv->ib.type, &pcv->psstFrom);
  1774. ClearReadAccess();
  1775. olChkTo(EH_pdfi, sc);
  1776. // Not optimally efficient, but reduces #ifdef's
  1777. StringCbCopyW (pcv->atcName, sizeof(pcv->atcName), (WCHAR *)pcv->ib.dfnName.GetBuffer());
  1778. // Don't need to SetReadAccess here because pstgTo is an IStorage.
  1779. #ifdef MULTIHEAP
  1780. {
  1781. CSafeMultiHeap smh(_ppc);
  1782. // if pstgTo is an IStorage proxy, then returned IStream
  1783. // can be custom marshaled and allocator state is lost
  1784. #endif
  1785. olHChkTo(EH_Get,
  1786. pstgTo->CreateStream(pcv->atcName, STGM_WRITE |
  1787. STGM_SHARE_EXCLUSIVE |
  1788. STGM_CREATE,
  1789. 0, 0, &pcv->pstTo));
  1790. #ifdef MULTIHEAP
  1791. }
  1792. #endif
  1793. olChkTo(EH_Create,
  1794. CopySStreamToIStream(pcv->psstFrom, pcv->pstTo));
  1795. pcv->psstFrom->Release();
  1796. pcv->pstTo->Release();
  1797. break;
  1798. default:
  1799. olAssert(!aMsg("Unknown type in CopyDocFileToIStorage"));
  1800. break;
  1801. }
  1802. }
  1803. olDebugOut((DEB_ITRACE, "Out CopyDocFileToIStorage\n"));
  1804. sc = S_OK;
  1805. EH_pdfi:
  1806. EH_Err:
  1807. delete pcv;
  1808. return sc;
  1809. EH_Create:
  1810. if (pcv->ib.type == STGTY_STORAGE)
  1811. pcv->pstgToChild->Release();
  1812. else
  1813. pcv->pstTo->Release();
  1814. olVerSucc(pstgTo->DestroyElement(pcv->atcName));
  1815. EH_Get:
  1816. if (pcv->ib.type == STGTY_STORAGE)
  1817. pcv->pdfFromChild->Release();
  1818. else
  1819. pcv->psstFrom->Release();
  1820. goto EH_Err;
  1821. }
  1822. //+--------------------------------------------------------------
  1823. //
  1824. // Member: CExposedDocFile::Unmarshal, public
  1825. //
  1826. // Synopsis: Creates a duplicate DocFile from parts
  1827. //
  1828. // Arguments: [pstm] - Marshal stream
  1829. // [ppv] - Object return
  1830. // [mshlflags] - Marshal flags
  1831. //
  1832. // Returns: Appropriate status code
  1833. //
  1834. // Modifies: [ppv]
  1835. //
  1836. // History: 26-Feb-92 DrewB Created
  1837. //
  1838. //---------------------------------------------------------------
  1839. SCODE CExposedDocFile::Unmarshal(IStream *pstm,
  1840. void **ppv,
  1841. DWORD mshlflags)
  1842. {
  1843. SCODE sc;
  1844. CDfMutex mtx;
  1845. CPerContext *ppc;
  1846. CPubDocFile *pdf;
  1847. CGlobalContext *pgc;
  1848. CDFBasis *pdfb;
  1849. CExposedDocFile *pedf;
  1850. IStorage *pstgStd = NULL;
  1851. ULONG_PTR df;
  1852. #ifdef ASYNC
  1853. DWORD dwAsyncFlags;
  1854. IDocfileAsyncConnectionPoint *pdacp;
  1855. #endif
  1856. #ifdef POINTER_IDENTITY
  1857. CMarshalList *pml;
  1858. #endif
  1859. olDebugOut((DEB_ITRACE, "In CExposedDocFile::Unmarshal(%p, %p)\n",
  1860. pstm, ppv));
  1861. #ifdef MULTIHEAP
  1862. void *pvBaseOld;
  1863. void *pvBaseNew;
  1864. ContextId cntxid;
  1865. CPerContext pcSharedMemory (NULL); // bootstrap object
  1866. #endif
  1867. //First unmarshal the standard marshalled version
  1868. sc = CoUnmarshalInterface(pstm, IID_IStorage, (void**)&pstgStd);
  1869. if (FAILED(sc))
  1870. {
  1871. // assume that entire standard marshaling stream has been read
  1872. olAssert (pstgStd == NULL);
  1873. sc = S_OK;
  1874. }
  1875. #ifdef MULTIHEAP
  1876. sc = UnmarshalSharedMemory(pstm, mshlflags, &pcSharedMemory, &cntxid);
  1877. if (!SUCCEEDED(sc))
  1878. {
  1879. #ifdef POINTER_IDENTITY
  1880. UnmarshalPointer(pstm, (void **) &pedf);
  1881. #endif
  1882. UnmarshalPointer(pstm, (void **)&pdf);
  1883. UnmarshalPointer(pstm, (void **)&pdfb);
  1884. UnmarshalPointer(pstm, (void **)&df);
  1885. #ifdef ASYNC
  1886. ReleaseContext(pstm, TRUE, P_INDEPENDENT(df) != 0, mshlflags);
  1887. ReleaseConnection(pstm, mshlflags);
  1888. #else
  1889. ReleaseContext(pstm, P_INDEPENDENT(df), mshlflags);
  1890. #endif
  1891. olChkTo(EH_std, sc);
  1892. }
  1893. pvBaseOld = DFBASEPTR;
  1894. #endif
  1895. #ifdef POINTER_IDENTITY
  1896. olChkTo(EH_mem, UnmarshalPointer(pstm, (void **) &pedf));
  1897. #endif
  1898. olChkTo(EH_mem, UnmarshalPointer(pstm, (void **)&pdf));
  1899. olChkTo(EH_mem, CPubDocFile::Validate(pdf));
  1900. olChkTo(EH_pdf, UnmarshalPointer(pstm, (void **)&pdfb));
  1901. olChkTo(EH_pdfb, UnmarshalPointer(pstm, (void **) &df));
  1902. olChkTo(EH_pdfb, UnmarshalPointer(pstm, (void **)&pgc));
  1903. olChkTo(EH_pgc, ValidateBuffer(pgc, sizeof(CGlobalContext)));
  1904. //So far, nothing has called into the tree so we don't really need
  1905. // to be holding the tree mutex. The UnmarshalContext call does
  1906. // call into the tree, though, so we need to make sure this is
  1907. // threadsafe. We'll do this my getting the mutex name from the
  1908. // CGlobalContext, then creating a new CDfMutex object. While
  1909. // this is obviously not optimal, since it's possible we could
  1910. // reuse an existing CDfMutex, the reuse strategy isn't threadsafe
  1911. // since we can't do a lookup without the possibility of the thing
  1912. // we're looking for being released by another thread.
  1913. TCHAR atcMutexName[CONTEXT_MUTEX_NAME_LENGTH];
  1914. pgc->GetMutexName(atcMutexName);
  1915. olChkTo(EH_pgc, mtx.Init(atcMutexName));
  1916. olChkTo(EH_pgc, mtx.Take(INFINITE));
  1917. //At this point we're holding the mutex.
  1918. #ifdef MULTIHEAP
  1919. #ifdef ASYNC
  1920. olChkTo(EH_mtx, UnmarshalContext(pstm,
  1921. pgc,
  1922. &ppc,
  1923. mshlflags,
  1924. TRUE,
  1925. P_INDEPENDENT(pdf->GetDFlags()),
  1926. cntxid,
  1927. pdf->IsRoot()));
  1928. #else
  1929. olChkTo(EH_mtx, UnmarshalContext(pstm,
  1930. pgc,
  1931. &ppc,
  1932. mshlflags,
  1933. P_INDEPENDENT(pdf->GetDFlags()),
  1934. cntxid,
  1935. pdf->IsRoot()));
  1936. #endif
  1937. if ((pvBaseNew = DFBASEPTR) != pvBaseOld)
  1938. {
  1939. pdf = (CPubDocFile*) ((ULONG_PTR)pdf - (ULONG_PTR)pvBaseOld
  1940. + (ULONG_PTR)pvBaseNew);
  1941. pedf = (CExposedDocFile*) ((ULONG_PTR)pedf - (ULONG_PTR)pvBaseOld
  1942. + (ULONG_PTR)pvBaseNew);
  1943. pdfb = (CDFBasis*) ((ULONG_PTR)pdfb - (ULONG_PTR)pvBaseOld
  1944. + (ULONG_PTR)pvBaseNew);
  1945. }
  1946. #else
  1947. #ifdef ASYNC
  1948. olChkTo(EH_mtx, UnmarshalContext(pstm,
  1949. pgc,
  1950. &ppc,
  1951. mshlflags,
  1952. TRUE,
  1953. P_INDEPENDENT(pdf->GetDFlags()),
  1954. pdf->IsRoot()));
  1955. #else
  1956. olChkTo(EH_mtx, UnmarshalContext(pstm,
  1957. pgc,
  1958. &ppc,
  1959. mshlflags,
  1960. P_INDEPENDENT(pdf->GetDFlags()),
  1961. pdf->IsRoot()));
  1962. #endif //ASYNC
  1963. #endif
  1964. #ifdef ASYNC
  1965. olChkTo(EH_mtx, UnmarshalConnection(pstm,
  1966. &dwAsyncFlags,
  1967. &pdacp,
  1968. mshlflags));
  1969. #endif
  1970. // if we use up 1Gig of address space, use standard unmarshaling
  1971. if (gs_iSharedHeaps > (DOCFILE_SM_LIMIT / DOCFILE_SM_SIZE))
  1972. olErr (EH_ppc, STG_E_INSUFFICIENTMEMORY);
  1973. #ifdef POINTER_IDENTITY
  1974. olAssert (pedf != NULL);
  1975. pml = (CMarshalList *) pedf;
  1976. // Warning: these checks must remain valid across processes
  1977. if (SUCCEEDED(pedf->Validate()) && pedf->GetPub() == pdf)
  1978. {
  1979. pedf = (CExposedDocFile *) pml->FindMarshal(GetCurrentContextId());
  1980. }
  1981. else
  1982. {
  1983. pml = NULL;
  1984. pedf = NULL;
  1985. }
  1986. // exposed object is not found or has been deleted
  1987. if (pedf == NULL)
  1988. {
  1989. #endif
  1990. olMemTo(EH_ppc, pedf = new (pdfb->GetMalloc())
  1991. CExposedDocFile(pdf, pdfb, ppc));
  1992. olChkTo(EH_exp, pedf->InitMarshal(dwAsyncFlags, pdacp));
  1993. //InitMarshal adds a reference.
  1994. if (pdacp)
  1995. pdacp->Release();
  1996. #ifdef POINTER_IDENTITY
  1997. if (pml) pml->AddMarshal(pedf);
  1998. pdf->vAddRef(); // CExposedDocFile ctor does not AddRef
  1999. }
  2000. else
  2001. {
  2002. pdfb->SetAccess(ppc);
  2003. pedf->AddRef(); // reuse this object
  2004. ppc->Release(); // reuse percontext
  2005. }
  2006. #else
  2007. pdf->vAddRef();
  2008. #endif
  2009. *ppv = (void *)pedf;
  2010. #ifdef MULTIHEAP
  2011. if (pvBaseOld != pvBaseNew)
  2012. {
  2013. pcSharedMemory.SetThreadAllocatorState(NULL);
  2014. g_smAllocator.Uninit(); // delete the extra mapping
  2015. }
  2016. g_smAllocator.SetState(NULL, NULL, 0, NULL, NULL);
  2017. #endif
  2018. mtx.Release();
  2019. //We're returning the custom marshalled version, so we don't need
  2020. //the std marshalled one anymore.
  2021. if (pstgStd != NULL)
  2022. pstgStd->Release();
  2023. olDebugOut((DEB_ITRACE, "Out CExposedDocFile::Unmarshal => %p\n", *ppv));
  2024. return S_OK;
  2025. EH_exp:
  2026. pedf->Release();
  2027. goto EH_Err;
  2028. EH_ppc:
  2029. ppc->Release();
  2030. EH_mtx:
  2031. mtx.Release();
  2032. goto EH_Err;
  2033. EH_pgc:
  2034. CoReleaseMarshalData(pstm); // release the ILockBytes
  2035. CoReleaseMarshalData(pstm); // release the ILockBytes
  2036. if (P_INDEPENDENT(pdf->GetDFlags()))
  2037. CoReleaseMarshalData(pstm); // release the ILockBytes
  2038. #ifdef ASYNC
  2039. ReleaseConnection(pstm, mshlflags);
  2040. #endif
  2041. EH_pdfb:
  2042. EH_pdf:
  2043. EH_mem:
  2044. EH_Err:
  2045. #ifdef MULTIHEAP
  2046. pcSharedMemory.SetThreadAllocatorState(NULL);
  2047. g_smAllocator.Uninit(); // delete the file mapping in error case
  2048. g_smAllocator.SetState(NULL, NULL, 0, NULL, NULL);
  2049. #endif
  2050. EH_std:
  2051. if (pstgStd != NULL)
  2052. {
  2053. //We can return the standard marshalled version and still succeed.
  2054. *ppv = pstgStd;
  2055. return S_OK;
  2056. }
  2057. return sc;
  2058. }
  2059. //+--------------------------------------------------------------
  2060. //
  2061. // Member: CExposedDocFile::GetUnmarshalClass, public
  2062. //
  2063. // Synopsis: Returns the class ID
  2064. //
  2065. // Arguments: [riid] - IID of object
  2066. // [pv] - Unreferenced
  2067. // [dwDestContext] - Unreferenced
  2068. // [pvDestContext] - Unreferenced
  2069. // [mshlflags] - Unreferenced
  2070. // [pcid] - CLSID return
  2071. //
  2072. // Returns: Appropriate status code
  2073. //
  2074. // Modifies: [pcid]
  2075. //
  2076. // History: 04-May-92 DrewB Created
  2077. //
  2078. //---------------------------------------------------------------
  2079. STDMETHODIMP CExposedDocFile::GetUnmarshalClass(REFIID riid,
  2080. void *pv,
  2081. DWORD dwDestContext,
  2082. LPVOID pvDestContext,
  2083. DWORD mshlflags,
  2084. LPCLSID pcid)
  2085. {
  2086. SCODE sc;
  2087. #ifdef MULTIHEAP
  2088. CSafeMultiHeap smh(_ppc);
  2089. #endif
  2090. olLog(("%p::In CExposedDocFile::GetUnmarshalClass("
  2091. "riid, %p, %lu, %p, %lu, %p)\n",
  2092. this, pv, dwDestContext, pvDestContext, mshlflags, pcid));
  2093. olDebugOut((DEB_TRACE, "In CExposedDocFile::GetUnmarshalClass:%p("
  2094. "riid, %p, %lu, %p, %lu, %p)\n", this,
  2095. pv, dwDestContext, pvDestContext, mshlflags, pcid));
  2096. UNREFERENCED_PARM(pv);
  2097. UNREFERENCED_PARM(mshlflags);
  2098. olChk(ValidateOutBuffer(pcid, sizeof(CLSID)));
  2099. memset(pcid, 0, sizeof(CLSID));
  2100. olChk(ValidateIid(riid));
  2101. olChk(Validate());
  2102. olChk(_pdf->CheckReverted());
  2103. if ((dwDestContext != MSHCTX_LOCAL) && (dwDestContext != MSHCTX_INPROC))
  2104. {
  2105. IMarshal *pmsh;
  2106. if (SUCCEEDED(sc = CoGetStandardMarshal(riid, (IUnknown *)pv,
  2107. dwDestContext, pvDestContext,
  2108. mshlflags, &pmsh)))
  2109. {
  2110. sc = GetScode(pmsh->GetUnmarshalClass(riid, pv, dwDestContext,
  2111. pvDestContext, mshlflags,
  2112. pcid));
  2113. pmsh->Release();
  2114. }
  2115. }
  2116. else if (pvDestContext != NULL)
  2117. {
  2118. sc = STG_E_INVALIDPARAMETER;
  2119. }
  2120. else
  2121. {
  2122. olChk(VerifyIid(riid, IID_IStorage));
  2123. *pcid = CLSID_DfMarshal;
  2124. }
  2125. olDebugOut((DEB_TRACE, "Out CExposedDocFile::GetUnmarshalClass\n"));
  2126. EH_Err:
  2127. olLog(("%p::Out CExposedDocFile::GetUnmarshalClass(). ret = %lx\n",
  2128. this, sc));
  2129. return ResultFromScode(sc);
  2130. }
  2131. //+--------------------------------------------------------------
  2132. //
  2133. // Member: CExposedDocFile::GetMarshalSizeMax, public
  2134. //
  2135. // Synopsis: Returns the size needed for the marshal buffer
  2136. //
  2137. // Arguments: [riid] - IID of object being marshaled
  2138. // [pv] - Unreferenced
  2139. // [dwDestContext] - Unreferenced
  2140. // [pvDestContext] - Unreferenced
  2141. // [mshlflags] - Unreferenced
  2142. // [pcbSize] - Size return
  2143. //
  2144. // Returns: Appropriate status code
  2145. //
  2146. // Modifies: [pcbSize]
  2147. //
  2148. // History: 04-May-92 DrewB Created
  2149. //
  2150. //---------------------------------------------------------------
  2151. STDMETHODIMP CExposedDocFile::GetMarshalSizeMax(REFIID riid,
  2152. void *pv,
  2153. DWORD dwDestContext,
  2154. LPVOID pvDestContext,
  2155. DWORD mshlflags,
  2156. LPDWORD pcbSize)
  2157. {
  2158. SCODE sc;
  2159. #ifdef MULTIHEAP
  2160. CSafeMultiHeap smh(_ppc);
  2161. #endif
  2162. UNREFERENCED_PARM(pv);
  2163. olLog(("%p::In CExposedDocFile::GetMarshalSizeMax("
  2164. "riid, %p, %lu, %p, %lu, %p)\n",
  2165. this, pv, dwDestContext, pvDestContext, mshlflags, pcbSize));
  2166. olDebugOut((DEB_TRACE, "In CExposedDocFile::GetMarshalSizeMax:%p("
  2167. "riid, %p, %lu, %p, %lu, %p)\n", this,
  2168. pv, dwDestContext, pvDestContext, mshlflags, pcbSize));
  2169. olChk(Validate());
  2170. olChk(_pdf->CheckReverted());
  2171. if ((dwDestContext != MSHCTX_LOCAL) && (dwDestContext != MSHCTX_INPROC))
  2172. {
  2173. IMarshal *pmsh;
  2174. if (SUCCEEDED(sc = CoGetStandardMarshal(riid, (IUnknown *)pv,
  2175. dwDestContext, pvDestContext,
  2176. mshlflags, &pmsh)))
  2177. {
  2178. sc = GetScode(pmsh->GetMarshalSizeMax(riid, pv, dwDestContext,
  2179. pvDestContext, mshlflags,
  2180. pcbSize));
  2181. pmsh->Release();
  2182. }
  2183. }
  2184. else if (pvDestContext != NULL)
  2185. {
  2186. sc = STG_E_INVALIDPARAMETER;
  2187. }
  2188. else
  2189. {
  2190. sc = GetStdMarshalSize(riid,
  2191. IID_IStorage,
  2192. dwDestContext,
  2193. pvDestContext,
  2194. mshlflags, pcbSize,
  2195. sizeof(CPubDocFile *)+sizeof(CDFBasis *)+
  2196. sizeof(DFLAGS),
  2197. #ifdef ASYNC
  2198. &_cpoint,
  2199. TRUE,
  2200. #endif
  2201. _ppc, P_INDEPENDENT(_pdf->GetDFlags()));
  2202. DWORD cbSize = 0;
  2203. IMarshal *pmsh;
  2204. if (SUCCEEDED(sc = CoGetStandardMarshal(riid, (IUnknown *)pv,
  2205. dwDestContext, pvDestContext,
  2206. mshlflags, &pmsh)))
  2207. {
  2208. sc = GetScode(pmsh->GetMarshalSizeMax(riid, pv, dwDestContext,
  2209. pvDestContext, mshlflags,
  2210. &cbSize));
  2211. pmsh->Release();
  2212. *pcbSize += cbSize;
  2213. }
  2214. }
  2215. olDebugOut((DEB_TRACE, "Out CExposedDocFile::GetMarshalSizeMax\n"));
  2216. EH_Err:
  2217. olLog(("%p::Out CExposedDocFile::GetMarshalSizeMax()."
  2218. "*pcbSize == %lu, ret == %lx\n", this, *pcbSize, sc));
  2219. return ResultFromScode(sc);
  2220. }
  2221. //+--------------------------------------------------------------
  2222. //
  2223. // Member: CExposedDocFile::MarshalInterface, public
  2224. //
  2225. // Synopsis: Marshals a given object
  2226. //
  2227. // Arguments: [pstStm] - Stream to write marshal data into
  2228. // [riid] - Interface to marshal
  2229. // [pv] - Unreferenced
  2230. // [dwDestContext] - Unreferenced
  2231. // [pvDestContext] - Unreferenced
  2232. // [mshlflags] - Unreferenced
  2233. //
  2234. // Returns: Appropriate status code
  2235. //
  2236. // History: 04-May-92 DrewB Created
  2237. //
  2238. //---------------------------------------------------------------
  2239. STDMETHODIMP CExposedDocFile::MarshalInterface(IStream *pstStm,
  2240. REFIID riid,
  2241. void *pv,
  2242. DWORD dwDestContext,
  2243. LPVOID pvDestContext,
  2244. DWORD mshlflags)
  2245. {
  2246. SCODE sc;
  2247. #ifdef MULTIHEAP
  2248. CSafeMultiHeap smh(_ppc);
  2249. #endif
  2250. olLog(("%p::In CExposedDocFile::MarshalInterface("
  2251. "%p, riid, %p, %lu, %p, %lu). Context == %lX\n",
  2252. this, pstStm, pv, dwDestContext,
  2253. pvDestContext, mshlflags,(ULONG)GetCurrentContextId()));
  2254. olDebugOut((DEB_TRACE, "In CExposedDocFile::MarshalInterface:%p("
  2255. "%p, riid, %p, %lu, %p, %lu)\n", this, pstStm, pv,
  2256. dwDestContext, pvDestContext, mshlflags));
  2257. UNREFERENCED_PARM(pv);
  2258. olChk(Validate());
  2259. olChk(_pdf->CheckReverted());
  2260. if ((dwDestContext != MSHCTX_LOCAL) && (dwDestContext != MSHCTX_INPROC))
  2261. {
  2262. IMarshal *pmsh;
  2263. if (SUCCEEDED(sc = CoGetStandardMarshal(riid, (IUnknown *)pv,
  2264. dwDestContext, pvDestContext,
  2265. mshlflags, &pmsh)))
  2266. {
  2267. sc = GetScode(pmsh->MarshalInterface(pstStm, riid, pv,
  2268. dwDestContext, pvDestContext,
  2269. mshlflags));
  2270. pmsh->Release();
  2271. }
  2272. }
  2273. else if (pvDestContext != NULL)
  2274. {
  2275. sc = STG_E_INVALIDPARAMETER;
  2276. }
  2277. else
  2278. {
  2279. olChk(StartMarshal(pstStm, riid, IID_IStorage, mshlflags));
  2280. //Always standard marshal, in case we get an error during
  2281. //unmarshalling of the custom stuff.
  2282. {
  2283. IMarshal *pmsh;
  2284. if (SUCCEEDED(sc = CoGetStandardMarshal(riid, (IUnknown *)pv,
  2285. dwDestContext, pvDestContext,
  2286. mshlflags, &pmsh)))
  2287. {
  2288. sc = GetScode(pmsh->MarshalInterface(pstStm, riid, pv,
  2289. dwDestContext, pvDestContext,
  2290. mshlflags));
  2291. pmsh->Release();
  2292. }
  2293. olChk(sc);
  2294. }
  2295. #ifdef MULTIHEAP
  2296. olChk(MarshalSharedMemory(pstStm, _ppc));
  2297. #endif
  2298. #ifdef POINTER_IDENTITY
  2299. olChk(MarshalPointer(pstStm, (CExposedDocFile*) GetNextMarshal()));
  2300. #endif
  2301. olChk(MarshalPointer(pstStm, BP_TO_P(CPubDocFile *, _pdf)));
  2302. olChk(MarshalPointer(pstStm, BP_TO_P(CDFBasis *, _pdfb)));
  2303. olChk(MarshalPointer(pstStm, (void *) LongToPtr(_pdf->GetDFlags()) ));
  2304. #ifdef ASYNC
  2305. olChk(MarshalContext(pstStm,
  2306. _ppc,
  2307. dwDestContext,
  2308. pvDestContext,
  2309. mshlflags,
  2310. TRUE,
  2311. P_INDEPENDENT(_pdf->GetDFlags())));
  2312. sc = MarshalConnection(pstStm,
  2313. &_cpoint,
  2314. dwDestContext,
  2315. pvDestContext,
  2316. mshlflags);
  2317. #else
  2318. olChk(MarshalContext(pstStm,
  2319. _ppc,
  2320. dwDestContext,
  2321. pvDestContext,
  2322. mshlflags,
  2323. P_INDEPENDENT(_pdf->GetDFlags())));
  2324. #endif
  2325. }
  2326. olDebugOut((DEB_TRACE, "Out CExposedDocFile::MarshalInterface\n"));
  2327. EH_Err:
  2328. olLog(("%p::Out CExposedDocFile::MarshalInterface(). ret == %lx\n",
  2329. this, sc));
  2330. return ResultFromScode(sc);
  2331. }
  2332. //+--------------------------------------------------------------
  2333. //
  2334. // Member: CExposedDocFile::UnmarshalInterface, public
  2335. //
  2336. // Synopsis: Non-functional
  2337. //
  2338. // Arguments: [pstStm] -
  2339. // [riid] -
  2340. // [ppvObj] -
  2341. //
  2342. // Returns: Appropriate status code
  2343. //
  2344. // Modifies: [ppvObj]
  2345. //
  2346. // History: 04-May-92 DrewB Created
  2347. //
  2348. //---------------------------------------------------------------
  2349. STDMETHODIMP CExposedDocFile::UnmarshalInterface(IStream *pstStm,
  2350. REFIID riid,
  2351. void **ppvObj)
  2352. {
  2353. olLog(("%p::INVALID CALL TO CExposedDocFile::UnmarshalInterface()\n",
  2354. this));
  2355. return ResultFromScode(STG_E_INVALIDFUNCTION);
  2356. }
  2357. //+--------------------------------------------------------------
  2358. //
  2359. // Member: CExposedDocFile::StaticReleaseMarshalData, public static
  2360. //
  2361. // Synopsis: Releases any references held in marshal data
  2362. //
  2363. // Arguments: [pstStm] - Marshal data stream
  2364. //
  2365. // Returns: Appropriate status code
  2366. //
  2367. // History: 02-Feb-94 DrewB Created
  2368. //
  2369. // Notes: Assumes standard marshal header has already been read
  2370. //
  2371. //---------------------------------------------------------------
  2372. SCODE CExposedDocFile::StaticReleaseMarshalData(IStream *pstStm,
  2373. DWORD mshlflags)
  2374. {
  2375. SCODE sc;
  2376. CPubDocFile *pdf;
  2377. CDFBasis *pdfb;
  2378. ULONG_PTR df;
  2379. #ifdef POINTER_IDENTITY
  2380. CExposedDocFile *pedf;
  2381. #endif
  2382. olDebugOut((DEB_ITRACE, "In CExposedDocFile::StaticReleaseMarshalData:("
  2383. "%p, %lX)\n", pstStm, mshlflags));
  2384. //First unmarshal the standard marshalled version
  2385. olChk(CoReleaseMarshalData(pstStm));
  2386. // The final release of the exposed object may have shut down the
  2387. // shared memory heap, so do not access shared memory after this point
  2388. //Then do the rest of it.
  2389. #ifdef MULTIHEAP
  2390. olChk(SkipSharedMemory(pstStm, mshlflags));
  2391. #endif
  2392. #ifdef POINTER_IDENTITY
  2393. olChk(UnmarshalPointer(pstStm, (void **) &pedf));
  2394. #endif
  2395. olChk(UnmarshalPointer(pstStm, (void **)&pdf));
  2396. olChk(UnmarshalPointer(pstStm, (void **)&pdfb));
  2397. olChk(UnmarshalPointer(pstStm, (void **)&df));
  2398. #ifdef ASYNC
  2399. olChk(ReleaseContext(pstStm, TRUE,
  2400. P_INDEPENDENT(df) != 0,
  2401. mshlflags));
  2402. olChk(ReleaseConnection(pstStm, mshlflags));
  2403. #else
  2404. olChk(ReleaseContext(pstStm, P_INDEPENDENT(df), mshlflags));
  2405. #endif
  2406. #ifdef MULTIHEAP
  2407. g_smAllocator.SetState(NULL, NULL, 0, NULL, NULL);
  2408. #endif
  2409. olDebugOut((DEB_ITRACE,
  2410. "Out CExposedDocFile::StaticReleaseMarshalData\n"));
  2411. EH_Err:
  2412. return sc;
  2413. }
  2414. //+--------------------------------------------------------------
  2415. //
  2416. // Member: CExposedDocFile::ReleaseMarshalData, public
  2417. //
  2418. // Synopsis: Non-functional
  2419. //
  2420. // Arguments: [pstStm] -
  2421. //
  2422. // Returns: Appropriate status code
  2423. //
  2424. // History: 18-Sep-92 DrewB Created
  2425. //
  2426. //---------------------------------------------------------------
  2427. STDMETHODIMP CExposedDocFile::ReleaseMarshalData(IStream *pstStm)
  2428. {
  2429. SCODE sc;
  2430. DWORD mshlflags;
  2431. IID iid;
  2432. #ifdef MULTIHEAP
  2433. CSafeMultiHeap smh(_ppc);
  2434. #endif
  2435. olLog(("%p::In CExposedDocFile::ReleaseMarshalData(%p)\n", this, pstStm));
  2436. olDebugOut((DEB_TRACE, "In CExposedDocFile::ReleaseMarshalData:%p(%p)\n",
  2437. this, pstStm));
  2438. olChk(Validate());
  2439. olChk(_pdf->CheckReverted());
  2440. olChk(SkipStdMarshal(pstStm, &iid, &mshlflags));
  2441. olAssert(IsEqualIID(iid, IID_IStorage));
  2442. sc = StaticReleaseMarshalData(pstStm, mshlflags);
  2443. olDebugOut((DEB_TRACE, "Out CExposedDocFile::ReleaseMarshalData\n"));
  2444. EH_Err:
  2445. olLog(("%p::Out CExposedDocFile::ReleaseMarshalData(). ret == %lx\n",
  2446. this, sc));
  2447. return ResultFromScode(sc);
  2448. }
  2449. //+--------------------------------------------------------------
  2450. //
  2451. // Member: CExposedDocFile::DisconnectObject, public
  2452. //
  2453. // Synopsis: Non-functional
  2454. //
  2455. // Arguments: [dwRevserved] -
  2456. //
  2457. // Returns: Appropriate status code
  2458. //
  2459. // History: 18-Sep-92 DrewB Created
  2460. //
  2461. //---------------------------------------------------------------
  2462. STDMETHODIMP CExposedDocFile::DisconnectObject(DWORD dwReserved)
  2463. {
  2464. olLog(("%p::INVALID CALL TO CExposedDocFile::DisconnectObject()\n",
  2465. this));
  2466. return ResultFromScode(STG_E_INVALIDFUNCTION);
  2467. }
  2468. //+---------------------------------------------------------------------------
  2469. //
  2470. // Member: CExposedDocFile::SwitchToFile, public
  2471. //
  2472. // Synopsis: Switches the underlying file to another file
  2473. //
  2474. // Arguments: [ptcsFile] - New file name
  2475. //
  2476. // Returns: Appropriate status code
  2477. //
  2478. // History: 08-Jan-93 DrewB Created
  2479. //
  2480. //----------------------------------------------------------------------------
  2481. STDMETHODIMP CExposedDocFile::SwitchToFile(OLECHAR *ptcsFile)
  2482. {
  2483. ULONG ulOpenLock;
  2484. SCODE sc;
  2485. SAFE_SEM;
  2486. SAFE_ACCESS;
  2487. olLog(("%p::In CExposedDocFile::SwitchToFile(" OLEFMT ")\n",
  2488. this, ptcsFile));
  2489. olDebugOut((DEB_TRACE, "In CExposedDocFile::SwitchToFile:"
  2490. "%p(" OLEFMT ")\n",
  2491. this, ptcsFile));
  2492. olChk(ValidateNameW(ptcsFile, _MAX_PATH));
  2493. olChk(Validate());
  2494. olChk(TakeSafeSem());
  2495. olChk(_pdf->CheckReverted());
  2496. #ifdef COORD
  2497. olAssert(_pdf->IsRoot() || _pdf->IsCoord());
  2498. #else
  2499. olAssert(_pdf->IsRoot());
  2500. #endif
  2501. SafeReadAccess();
  2502. ulOpenLock = _ppc->GetOpenLock();
  2503. #ifdef COORD
  2504. sc = _pdf->GetRoot()->SwitchToFile(ptcsFile,
  2505. _ppc->GetOriginal(),
  2506. &ulOpenLock);
  2507. #else
  2508. sc = ((CRootPubDocFile *)(CPubDocFile*)_pdf)->SwitchToFile(ptcsFile,
  2509. _ppc->GetOriginal(),
  2510. &ulOpenLock);
  2511. #endif
  2512. _ppc->SetOpenLock(ulOpenLock);
  2513. olDebugOut((DEB_TRACE, "Out CExposedDocFile::SwitchToFile\n"));
  2514. EH_Err:
  2515. olLog(("%p::Out CExposedDocFile::SwitchToFile(). ret == %lx\n",
  2516. this, sc));
  2517. return ResultFromScode(sc);
  2518. }
  2519. #if WIN32 >= 300
  2520. // IAccessControl methods
  2521. STDMETHODIMP CExposedDocFile::GrantAccessRights(ULONG cCount,
  2522. ACCESS_REQUEST pAccessRequestList[])
  2523. {
  2524. olAssert((_pIAC != NULL));
  2525. return _pIAC->GrantAccessRights(cCount, pAccessRequestList);
  2526. }
  2527. STDMETHODIMP CExposedDocFile::SetAccessRights(ULONG cCount,
  2528. ACCESS_REQUEST pAccessRequestList[])
  2529. {
  2530. olAssert((_pIAC != NULL));
  2531. return _pIAC->SetAccessRights(cCount, pAccessRequestList);
  2532. }
  2533. STDMETHODIMP CExposedDocFile::ReplaceAllAccessRights(ULONG cCount,
  2534. ACCESS_REQUEST pAccessRequestList[])
  2535. {
  2536. olAssert((_pIAC != NULL));
  2537. return _pIAC->ReplaceAllAccessRights(cCount, pAccessRequestList);
  2538. }
  2539. STDMETHODIMP CExposedDocFile::DenyAccessRights(ULONG cCount,
  2540. ACCESS_REQUEST pAccessRequestList[])
  2541. {
  2542. olAssert((_pIAC != NULL));
  2543. return _pIAC->DenyAccessRights(cCount, pAccessRequestList);
  2544. }
  2545. STDMETHODIMP CExposedDocFile::RevokeExplicitAccessRights(ULONG cCount,
  2546. TRUSTEE pTrustee[])
  2547. {
  2548. olAssert((_pIAC != NULL));
  2549. return _pIAC->RevokeExplicitAccessRights(cCount, pTrustee);
  2550. }
  2551. STDMETHODIMP CExposedDocFile::IsAccessPermitted(TRUSTEE *pTrustee,
  2552. DWORD grfAccessPermissions)
  2553. {
  2554. olAssert((_pIAC != NULL));
  2555. return _pIAC->IsAccessPermitted(pTrustee, grfAccessPermissions);
  2556. }
  2557. STDMETHODIMP CExposedDocFile::GetEffectiveAccessRights(TRUSTEE *pTrustee,
  2558. DWORD *pgrfAccessPermissions )
  2559. {
  2560. olAssert((_pIAC != NULL));
  2561. return _pIAC->GetEffectiveAccessRights(pTrustee, pgrfAccessPermissions);
  2562. }
  2563. STDMETHODIMP CExposedDocFile::GetExplicitAccessRights(ULONG *pcCount,
  2564. PEXPLICIT_ACCESS *pExplicitAccessList)
  2565. {
  2566. olAssert((_pIAC != NULL));
  2567. return _pIAC->GetExplicitAccessRights(pcCount, pExplicitAccessList);
  2568. }
  2569. STDMETHODIMP CExposedDocFile::CommitAccessRights(DWORD grfCommitFlags)
  2570. {
  2571. olAssert((_pIAC != NULL));
  2572. return _pIAC->CommitAccessRights(grfCommitFlags);
  2573. }
  2574. STDMETHODIMP CExposedDocFile::RevertAccessRights()
  2575. {
  2576. olAssert((_pIAC != NULL));
  2577. return _pIAC->RevertAccessRights();
  2578. }
  2579. #endif // if WIN32 >= 300
  2580. #ifdef COORD
  2581. //+---------------------------------------------------------------------------
  2582. //
  2583. // Member: CExposedDocFile::CommitPhase1, public
  2584. //
  2585. // Synopsis: Do phase 1 of an exposed two phase commit
  2586. //
  2587. // Arguments: [grfCommitFlags] -- Commit flags
  2588. //
  2589. // Returns: Appropriate status code
  2590. //
  2591. // History: 08-Aug-95 PhilipLa Created
  2592. //
  2593. //----------------------------------------------------------------------------
  2594. SCODE CExposedDocFile::CommitPhase1(DWORD grfCommitFlags)
  2595. {
  2596. SCODE sc;
  2597. SAFE_SEM;
  2598. SAFE_ACCESS;
  2599. olChk(VerifyCommitFlags(grfCommitFlags));
  2600. olChk(Validate());
  2601. olChk(TakeSafeSem());
  2602. SafeWriteAccess();
  2603. #ifdef DIRECTWRITERLOCK
  2604. olChk(ValidateWriteAccess());
  2605. #endif
  2606. sc = _pdf->CommitPhase1(grfCommitFlags,
  2607. &_ulLock,
  2608. &_sigMSF,
  2609. &_cbSizeBase,
  2610. &_cbSizeOrig);
  2611. EH_Err:
  2612. return sc;
  2613. }
  2614. //+---------------------------------------------------------------------------
  2615. //
  2616. // Member: CExposedDocFile::CommitPhase2, public
  2617. //
  2618. // Synopsis: Do phase 2 of an exposed two phase commit
  2619. //
  2620. // Arguments: [grfCommitFlags] -- Commit flags
  2621. // [fCommit] -- TRUE if transaction is to commit, FALSE if abort
  2622. //
  2623. // Returns: Appropriate status code
  2624. //
  2625. // History: 08-Aug-95 PhilipLa Created
  2626. //
  2627. //----------------------------------------------------------------------------
  2628. SCODE CExposedDocFile::CommitPhase2(DWORD grfCommitFlags,
  2629. BOOL fCommit)
  2630. {
  2631. SCODE sc;
  2632. SAFE_SEM;
  2633. SAFE_ACCESS;
  2634. olChk(Validate());
  2635. olChk(TakeSafeSem());
  2636. SafeWriteAccess();
  2637. #ifdef DIRECTWRITERLOCK
  2638. olChk(ValidateWriteAccess());
  2639. #endif
  2640. sc = _pdf->CommitPhase2(grfCommitFlags,
  2641. fCommit,
  2642. _ulLock,
  2643. _sigMSF,
  2644. _cbSizeBase,
  2645. _cbSizeOrig);
  2646. _ulLock = _cbSizeBase = _cbSizeOrig = 0;
  2647. _sigMSF = 0;
  2648. EH_Err:
  2649. return sc;
  2650. }
  2651. #endif //COORD
  2652. #ifdef NEWPROPS
  2653. //+---------------------------------------------------------------------------
  2654. //
  2655. // Member: CExposedDocFile::Lock, IBlockingLock
  2656. //
  2657. // Synopsis: Acquires the semaphore associated with the docfile.
  2658. //
  2659. // Notes: This member is called by CPropertyStorage.
  2660. //
  2661. //----------------------------------------------------------------------------
  2662. STDMETHODIMP
  2663. CExposedDocFile::Lock(DWORD dwTimeout)
  2664. {
  2665. #ifdef MULTIHEAP
  2666. CSafeMultiHeap smh(_ppc);
  2667. #endif
  2668. TakeSem();
  2669. SetDifferentBasisAccess(_pdfb, _ppc);
  2670. return S_OK;
  2671. }
  2672. //+---------------------------------------------------------------------------
  2673. //
  2674. // Member: CExposedDocFile::Unlock, public IBlockingLock
  2675. //
  2676. // Synopsis: Releases the semaphore associated with the docfile.
  2677. //
  2678. // Notes: This member is called by CPropertyStorage.
  2679. //
  2680. //----------------------------------------------------------------------------
  2681. STDMETHODIMP
  2682. CExposedDocFile::Unlock(VOID)
  2683. {
  2684. #ifdef MULTIHEAP
  2685. CSafeMultiHeap smh(_ppc);
  2686. #endif
  2687. ClearBasisAccess(_pdfb);
  2688. ReleaseSem(S_OK);
  2689. return( S_OK );
  2690. }
  2691. #endif
  2692. #ifdef DIRECTWRITERLOCK
  2693. //+---------------------------------------------------------------------------
  2694. //
  2695. // Member: CExposedDocFile::WaitForWriteAccess, public IDirectWriterLock
  2696. //
  2697. // Synopsis: tries to obtain exclusive write access in direct mode
  2698. //
  2699. // Notes: Tree mutex must be taken when accessing recursion count
  2700. //
  2701. //----------------------------------------------------------------------------
  2702. STDMETHODIMP CExposedDocFile::WaitForWriteAccess (DWORD dwTimeout)
  2703. {
  2704. SAFE_SEM;
  2705. SAFE_ACCESS;
  2706. HRESULT hr = TakeSafeSem();
  2707. if (SUCCEEDED(hr) && *_ppc->GetRecursionCount() == 0)
  2708. {
  2709. SafeReadAccess();
  2710. hr = _pdfb->WaitForWriteAccess (dwTimeout, _ppc->GetGlobal());
  2711. }
  2712. if (SUCCEEDED(hr)) ++(*_ppc->GetRecursionCount());
  2713. return hr;
  2714. }
  2715. //+---------------------------------------------------------------------------
  2716. //
  2717. // Member: CExposedDocFile::ReleaseWriteAccess, public IDirectWriterLock
  2718. //
  2719. // Synopsis: releases exclusive write access from WaitForWriteAccess
  2720. //
  2721. // Notes: Tree mutex must be taken when accessing recursion count
  2722. //
  2723. //----------------------------------------------------------------------------
  2724. STDMETHODIMP CExposedDocFile::ReleaseWriteAccess ()
  2725. {
  2726. SAFE_SEM;
  2727. SAFE_ACCESS;
  2728. HRESULT hr = TakeSafeSem();
  2729. if (SUCCEEDED(hr) && *_ppc->GetRecursionCount() == 1)
  2730. {
  2731. SafeReadAccess();
  2732. hr = _pdf->Commit(STGC_DEFAULT); // Flush
  2733. if (SUCCEEDED(hr)) hr = _pdfb->ReleaseWriteAccess();
  2734. }
  2735. if (SUCCEEDED(hr)) --(*_ppc->GetRecursionCount());
  2736. return hr;
  2737. }
  2738. //+---------------------------------------------------------------------------
  2739. //
  2740. // Member: CExposedDocFile::HaveWriteAccess, public IDirectWriterLock
  2741. //
  2742. // Synopsis: returns S_OK if write lock is active, S_FALSE if not
  2743. //
  2744. // Notes:
  2745. //
  2746. //----------------------------------------------------------------------------
  2747. STDMETHODIMP CExposedDocFile::HaveWriteAccess ()
  2748. {
  2749. SAFE_SEM;
  2750. HRESULT hr = TakeSafeSem();
  2751. olAssert(_pdfb->DirectWriterMode());
  2752. if (SUCCEEDED(hr))
  2753. {
  2754. hr = (_pdfb->HaveWriteAccess()) ? S_OK : S_FALSE;
  2755. }
  2756. return hr;
  2757. }
  2758. //+--------------------------------------------------------------
  2759. //
  2760. // Member: CExposedDocFile::ValidateWriteAccess, public
  2761. //
  2762. // Synopsis: returns whether writer currently has write access
  2763. //
  2764. // Notes: tree mutex must be taken
  2765. //
  2766. // History: 30-Apr-96 HenryLee Created
  2767. //
  2768. //---------------------------------------------------------------
  2769. HRESULT CExposedDocFile::ValidateWriteAccess()
  2770. {
  2771. if (_pdf->GetTransactedDepth() >= 1)
  2772. return S_OK;
  2773. return (!_pdfb->DirectWriterMode() || (*_ppc->GetRecursionCount()) > 0) ?
  2774. S_OK : STG_E_ACCESSDENIED;
  2775. };
  2776. #endif // DIRECTWRITERLOCK