Source code of Windows XP (NT5)
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.

301 lines
9.2 KiB

  1. //+--------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1991 - 1996.
  5. //
  6. // File: docfile.c
  7. //
  8. // Contents: DocFile root functions (Stg* functions)
  9. //
  10. //---------------------------------------------------------------
  11. #include "exphead.cxx"
  12. #include "expst.hxx"
  13. #include "h/rexpdf.hxx"
  14. #include "h/docfile.hxx"
  15. #include "ascii.hxx"
  16. #include "logfile.hxx"
  17. #include "h/refilb.hxx"
  18. //+--------------------------------------------------------------
  19. //
  20. // Function: DfFromLB, private
  21. //
  22. // Synopsis: Starts a root Docfile on an ILockBytes
  23. //
  24. // Arguments: [plst] - LStream to start on
  25. // [df] - Permissions
  26. // [dwStartFlags] - Startup flags
  27. // [snbExclude] - Partial instantiation list
  28. // [ppdfExp] - DocFile return
  29. // [pcid] - Class ID return for opens
  30. //
  31. // Returns: Appropriate status code
  32. //
  33. // Modifies: [ppdfExp]
  34. // [pcid]
  35. //
  36. //---------------------------------------------------------------
  37. SCODE DfFromLB(ILockBytes *plst,
  38. DFLAGS df,
  39. DWORD dwStartFlags,
  40. SNBW snbExclude,
  41. CExposedDocFile **ppdfExp,
  42. CLSID *pcid)
  43. {
  44. SCODE sc, scConv;
  45. CRootExposedDocFile *prpdf;
  46. CDFBasis *pdfb=NULL;
  47. UNREFERENCED_PARM(pcid);
  48. olDebugOut((DEB_ITRACE, "In DfFromLB(%p, %X, %lX, %p, %p, %p)\n",
  49. plst, df, dwStartFlags, snbExclude, ppdfExp, pcid));
  50. // If we're not creating or converting, do a quick check
  51. // to make sure that the ILockBytes contains a storage
  52. if ((dwStartFlags & (RSF_CREATEFLAGS | RSF_CONVERT)) == 0)
  53. olHChk(StgIsStorageILockBytes(plst));
  54. // Make root
  55. olMem(pdfb = new CDFBasis);
  56. olMemTo(EH_pdfb, prpdf = new CRootExposedDocFile(pdfb));
  57. olChkTo(EH_ppcInit, scConv = prpdf->InitRoot(plst, dwStartFlags, df,
  58. snbExclude));
  59. *ppdfExp = prpdf;
  60. olDebugOut((DEB_ITRACE, "Out DfFromLB => %p\n", SAFE_DREF(ppdfExp)));
  61. return scConv;
  62. EH_ppcInit:
  63. prpdf->Release();
  64. EH_pdfb:
  65. delete pdfb;
  66. EH_Err:
  67. return sc;
  68. }
  69. //+--------------------------------------------------------------
  70. //
  71. // Function: StgCreateDocfile, public
  72. //
  73. // Synopsis: char version
  74. //
  75. //---------------------------------------------------------------
  76. STDAPI StgCreateDocfile(TCHAR const *pszName,
  77. DWORD grfMode,
  78. DWORD reserved,
  79. IStorage **ppstgOpen)
  80. {
  81. SCODE sc;
  82. CFileILB *pilb = NULL;
  83. int i;
  84. olDebugOut((DEB_ITRACE, "In StgCreateDocfile(%p, %lX, %lu, %p)\n",
  85. pszName, grfMode, reserved, ppstgOpen));
  86. olChk(ValidatePtrBuffer(ppstgOpen));
  87. *ppstgOpen = NULL;
  88. olChk(VerifyPerms(grfMode));
  89. if ((grfMode & STGM_RDWR) == STGM_READ ||
  90. (grfMode & (STGM_DELETEONRELEASE | STGM_CONVERT)) ==
  91. (STGM_DELETEONRELEASE | STGM_CONVERT))
  92. olErr(EH_Err, STG_E_INVALIDFLAG);
  93. pilb = new CFileILB(pszName, grfMode, FALSE);
  94. if (!pilb) olErr(EH_Err, STG_E_INSUFFICIENTMEMORY);
  95. olChk( pilb->Create(grfMode) );
  96. if ( (grfMode & (STGM_CREATE|STGM_CONVERT)) == STGM_FAILIFTHERE)
  97. grfMode |= STGM_CREATE; // no file exists, we will 'overwrite' the new
  98. // file
  99. grfMode &= (~STGM_DELETEONRELEASE); // remove the flag
  100. sc=GetScode(StgCreateDocfileOnILockBytes(pilb, grfMode, reserved,
  101. ppstgOpen));
  102. EH_Err:
  103. if (pilb)
  104. {
  105. if (FAILED(sc)) i=pilb->ReleaseOnError();
  106. else i=pilb->Release();
  107. olAssert(SUCCEEDED(sc) ? i==1 : i==0);
  108. }
  109. olDebugOut((DEB_ITRACE, "Out StgCreateDocfile: *ppstgOpen=%p ret=>%l\n",
  110. ppstgOpen?(*ppstgOpen):NULL, sc));
  111. return ResultFromScode(sc);
  112. }
  113. //+--------------------------------------------------------------
  114. //
  115. // Function: StgCreateDocfileOnILockBytes, public
  116. //
  117. // Synopsis: Creates a root Docfile on an lstream
  118. //
  119. // Arguments: [plkbyt] - LStream
  120. // [grfMode] - Permissions
  121. // [reserved] - Unused
  122. // [ppstgOpen] - Docfile return
  123. //
  124. // Returns: Appropriate status code
  125. //
  126. // Modifies: [ppstgOpen]
  127. //
  128. //---------------------------------------------------------------
  129. STDAPI StgCreateDocfileOnILockBytes(ILockBytes *plkbyt,
  130. DWORD grfMode,
  131. DWORD reserved,
  132. IStorage **ppstgOpen)
  133. {
  134. CExposedDocFile *pdfExp;
  135. SCODE sc;
  136. DFLAGS df;
  137. olLog(("--------::In StgCreateDocFileOnILockBytes(%p, %lX, %lu, %p)\n",
  138. plkbyt, grfMode, reserved, ppstgOpen));
  139. olDebugOut((DEB_ITRACE, "In StgCreateDocfileOnILockBytes("
  140. "%p, %lX, %lu, %p)\n",
  141. plkbyt, grfMode, reserved, ppstgOpen));
  142. TRY
  143. {
  144. olChk(ValidatePtrBuffer(ppstgOpen));
  145. *ppstgOpen = NULL;
  146. olChk(ValidateInterface(plkbyt, IID_ILockBytes));
  147. if (reserved != 0)
  148. olErr(EH_Err, STG_E_INVALIDPARAMETER);
  149. if ((grfMode & (STGM_CREATE | STGM_CONVERT)) == 0)
  150. olErr(EH_Err, STG_E_FILEALREADYEXISTS);
  151. olChk(VerifyPerms(grfMode));
  152. if (grfMode & STGM_DELETEONRELEASE)
  153. olErr(EH_Err, STG_E_INVALIDFUNCTION);
  154. df = ModeToDFlags(grfMode);
  155. if ((grfMode & (STGM_TRANSACTED | STGM_CONVERT)) ==
  156. (STGM_TRANSACTED | STGM_CONVERT))
  157. df |= DF_INDEPENDENT;
  158. olChkTo(EH_Truncate,
  159. sc = DfFromLB(plkbyt, df,
  160. RSF_CREATE |
  161. ((grfMode & STGM_CREATE) ? RSF_TRUNCATE : 0) |
  162. ((grfMode & STGM_CONVERT) ? RSF_CONVERT : 0),
  163. NULL, &pdfExp, NULL));
  164. *ppstgOpen = pdfExp;
  165. }
  166. CATCH(CException, e)
  167. {
  168. sc = e.GetErrorCode();
  169. }
  170. END_CATCH
  171. olDebugOut((DEB_ITRACE, "Out StgCreateDocfileOnILockBytes => %p\n",
  172. ppstgOpen?(*ppstgOpen):NULL));
  173. EH_Err:
  174. olLog(("--------::Out StgCreateDocFileOnILockBytes(). *ppstgOpen == %p, ret == %lx\n",
  175. *ppstgOpen, sc));
  176. FreeLogFile();
  177. return ResultFromScode(sc);
  178. EH_Truncate:
  179. if ((grfMode & STGM_CREATE) && (grfMode & STGM_TRANSACTED) == 0)
  180. {
  181. ULARGE_INTEGER ulSize;
  182. ULISet32(ulSize, 0);
  183. olHChk(plkbyt->SetSize(ulSize));
  184. }
  185. goto EH_Err;
  186. }
  187. //+--------------------------------------------------------------
  188. //
  189. // Function: DfOpenStorageOnILockBytes, public
  190. //
  191. // Synopsis: Instantiates a root Docfile from an LStream,
  192. // converting if necessary
  193. //
  194. // Arguments: [plkbyt] - Source LStream
  195. // [pstgPriority] - For priority reopens
  196. // [grfMode] - Permissions
  197. // [snbExclude] - For priority reopens
  198. // [reserved]
  199. // [ppstgOpen] - Docfile return
  200. // [pcid] - Class ID return
  201. //
  202. // Returns: Appropriate status code
  203. //
  204. // Modifies: [ppstgOpen]
  205. // [pcid]
  206. //
  207. //---------------------------------------------------------------
  208. HRESULT DfOpenStorageOnILockBytesW(ILockBytes *plkbyt,
  209. IStorage *pstgPriority,
  210. DWORD grfMode,
  211. SNBW snbExclude,
  212. DWORD reserved,
  213. IStorage **ppstgOpen,
  214. CLSID *pcid)
  215. {
  216. SCODE sc;
  217. CExposedDocFile *pdfExp;
  218. olLog(("--------::In DfOpenStorageOnILockBytes("
  219. "%p, %p, %lX, %p, %lu, %p, %p)\n",
  220. plkbyt, pstgPriority, grfMode, snbExclude, reserved, ppstgOpen,
  221. pcid));
  222. olDebugOut((DEB_ITRACE, "In DfOpenStorageOnILockBytes("
  223. "%p, %p, %lX, %p, %lu, %p, %p)\n", plkbyt, pstgPriority,
  224. grfMode, snbExclude, reserved, ppstgOpen, pcid));
  225. TRY
  226. {
  227. #ifdef _UNICODE // do checking if there is an ANSI layer
  228. olChk(ValidatePtrBuffer(ppstgOpen));
  229. *ppstgOpen = NULL;
  230. if (snbExclude) olChk(ValidateSNBW(snbExclude));
  231. #endif /!UNICODE
  232. olChk(ValidateInterface(plkbyt, IID_ILockBytes));
  233. if (pstgPriority)
  234. olChk(ValidateInterface(pstgPriority, IID_IStorage));
  235. olChk(VerifyPerms(grfMode));
  236. if (grfMode & STGM_DELETEONRELEASE)
  237. olErr(EH_Err, STG_E_INVALIDFUNCTION);
  238. if (snbExclude)
  239. {
  240. if ((grfMode & STGM_RDWR) != STGM_READWRITE)
  241. olErr(EH_Err, STG_E_ACCESSDENIED);
  242. }
  243. if (reserved != 0)
  244. olErr(EH_Err, STG_E_INVALIDPARAMETER);
  245. if (FAILED(DllIsMultiStream(plkbyt)))
  246. olErr(EH_Err, STG_E_FILEALREADYEXISTS); //file is not storage obj
  247. if (pstgPriority)
  248. olChk(pstgPriority->Release());
  249. olChk(DfFromLB(plkbyt, ModeToDFlags(grfMode), RSF_OPEN, snbExclude,
  250. &pdfExp, pcid));
  251. *ppstgOpen = pdfExp;
  252. }
  253. CATCH(CException, e)
  254. {
  255. sc = e.GetErrorCode();
  256. }
  257. END_CATCH
  258. olDebugOut((DEB_ITRACE, "Out DfOpenStorageOnILockBytes => %p\n",
  259. ppstgOpen?(*ppstgOpen):NULL));
  260. EH_Err:
  261. olLog(("--------::Out DfOpenStorageOnILockBytes(). *ppstgOpen == %p"
  262. ", ret == %lx\n", *ppstgOpen, sc));
  263. FreeLogFile();
  264. return sc;
  265. }