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.

389 lines
12 KiB

  1. //-------------------------------------------------------------------------
  2. //
  3. // Microsoft OLE
  4. // Copyright (C) Microsoft Corporation, 1994 - 1995.
  5. //
  6. // File: init.cxx
  7. //
  8. // Contents: OLE storage base tests
  9. //
  10. // Functions: main
  11. //
  12. // History: 26-Feb-1997 SCousens Created.
  13. //
  14. //--------------------------------------------------------------------------
  15. #include <dfheader.hxx>
  16. #pragma hdrstop
  17. // Debug object declaration
  18. DH_DECLARE;
  19. // Private function
  20. HRESULT MakeVirtualDF (
  21. IN ChanceDF *pChanceDF,
  22. OUT VirtualDF **ppVirtualDF);
  23. //----------------------------------------------------------------------------
  24. //
  25. // FUNCTION: CreateTestDocfile [multiple]
  26. //
  27. // PARAMS: ppVirtualDF - bucket for pVirtualDF
  28. // pcdfd - CDFD for chancedf
  29. // pCmdLine - CommandLine (default)
  30. // pFileName - name of docfile (default)
  31. //
  32. // ulSeed - seed (to get name)
  33. //
  34. // SYNOPSIS: Create a test docfile with semantics defined in
  35. // given CDFD.
  36. // Look on CmdLine (either given or result of
  37. // GetCommandLine() call) to override values.
  38. //
  39. // RETURN: hr. S_OK or whatever failure was encountered.
  40. //
  41. // NOTES:
  42. //
  43. // HISTORY: 19-Mar-1997 SCousens Created.
  44. //
  45. //----------------------------------------------------------------------------
  46. HRESULT CreateTestDocfile (
  47. OUT VirtualDF **ppvdf,
  48. IN CDFD *pcdfd,
  49. IN LPTSTR pCmdLine,
  50. IN LPTSTR pFileName)
  51. {
  52. HRESULT hr = S_OK;
  53. ChanceDF *pChanceDF = NULL;
  54. LPTSTR pDocFileName = NULL;
  55. LPTSTR ptCommandLine= NULL;
  56. int argc = 0;
  57. char ** argv = NULL;
  58. DH_FUNCENTRY(&hr, DH_LVL_DFLIB, TEXT("CreateTestDocfile"));
  59. DH_VDATEPTRIN (pcdfd, CDFD);
  60. DH_VDATEPTROUT (ppvdf, VirtualDF *);
  61. *ppvdf = NULL;
  62. // Always create this.
  63. // Create the new ChanceDocFile tree that would consist of chance nodes.
  64. pChanceDF = new ChanceDF();
  65. if(NULL == pChanceDF)
  66. {
  67. hr = E_OUTOFMEMORY;
  68. }
  69. DH_HRCHECK_ABORT (hr, TEXT("new ChanceDF"));
  70. // initialize chancedf with our desired cdfd
  71. hr = pChanceDF->Init (pcdfd);
  72. DH_HRCHECK_ABORT (hr, TEXT("pChanceDF->Init"));
  73. // Create argc/argv from either given cmdline, or GetCommandLine
  74. ptCommandLine = (NULL == pCmdLine) ? GetCommandLine () : pCmdLine;
  75. if (NULL != ptCommandLine)
  76. {
  77. LPSTR paCommandLine = NULL;
  78. hr = TStringToAString (ptCommandLine, &paCommandLine);
  79. DH_HRCHECK_ABORT (hr, TEXT("TStringToAString"));
  80. hr = CmdlineToArgs (paCommandLine, &argc, &argv);
  81. DH_HRCHECK_ABORT (hr, TEXT("CmdlineToArgs"));
  82. delete []paCommandLine;
  83. }
  84. // Create ChanceDF, using filename and cmdline override
  85. hr = pChanceDF->CreateFromParams (argc, argv, pFileName);
  86. DH_HRCHECK_ABORT (hr, TEXT("pChanceDF->CreateFromParams"));
  87. //cleanup argc/argv
  88. if (NULL != argv)
  89. {
  90. for (int count=0; count<argc; count++)
  91. {
  92. delete []argv[count];
  93. }
  94. delete []argv;
  95. }
  96. // Make VirtualDF
  97. hr = MakeVirtualDF (pChanceDF, ppvdf);
  98. DH_HRCHECK_ABORT (hr, TEXT("MakeVirtualDF"));
  99. ErrReturn:
  100. // cleanup
  101. delete []pDocFileName;
  102. delete pChanceDF;
  103. return hr;
  104. }
  105. //----------------------------------------------------------------------------
  106. //
  107. // FUNCTION: CreateTestDocfile [multiple]
  108. //
  109. // PARAMS: ppVirtualDF - bucket for pVirtualDF
  110. // uType - predefinede type for docfile
  111. // ulSeed - seed
  112. // pCmdLine - CommandLine (default)
  113. // pFileName - name of docfile (default)
  114. //
  115. // SYNOPSIS: Create a test docfile with predefined semantics.
  116. // Create a CDFD and call CreateTestDocfile with
  117. // created CDFD
  118. //
  119. // RETURN: hr. S_OK or whatever failure was encountered.
  120. //
  121. // NOTES:
  122. //
  123. // HISTORY: 19-Mar-1997 SCousens Created.
  124. //
  125. //----------------------------------------------------------------------------
  126. HRESULT CreateTestDocfile (
  127. OUT VirtualDF **ppvdf,
  128. IN DWORD uType,
  129. IN ULONG ulSeed,
  130. IN LPTSTR pCmdLine,
  131. IN LPTSTR pFileName)
  132. {
  133. HRESULT hr = S_OK;
  134. CDFD cdfd;
  135. DH_FUNCENTRY(&hr, DH_LVL_DFLIB, TEXT("CreateTestDocfile"));
  136. DH_VDATEPTROUT (ppvdf, VirtualDF *);
  137. *ppvdf = NULL;
  138. // default modes
  139. cdfd.dwRootMode = cdfd.dwStgMode = cdfd.dwStmMode =
  140. STGM_READWRITE |
  141. STGM_DIRECT |
  142. STGM_SHARE_EXCLUSIVE;
  143. cdfd.ulSeed = ulSeed;
  144. //FIXIT: BUGBUG: we need to define all the types of docfiles needed
  145. switch (uType)
  146. {
  147. case DF_TINY:
  148. cdfd.cDepthMin = 0;
  149. cdfd.cDepthMax = 0;
  150. cdfd.cStgMin = 0;
  151. cdfd.cStgMax = 0;
  152. cdfd.cStmMin = 0;
  153. cdfd.cStmMax = 3;
  154. cdfd.cbStmMin = 0;
  155. cdfd.cbStmMax = 100;
  156. break;
  157. case DF_SMALL:
  158. cdfd.cDepthMin = 0;
  159. cdfd.cDepthMax = 1;
  160. cdfd.cStgMin = 0;
  161. cdfd.cStgMax = 1;
  162. cdfd.cStmMin = 0;
  163. cdfd.cStmMax = 5;
  164. cdfd.cbStmMin = 0;
  165. cdfd.cbStmMax = 4000;
  166. break;
  167. case DF_MEDIUM:
  168. cdfd.cDepthMin = 1;
  169. cdfd.cDepthMax = 3;
  170. cdfd.cStgMin = 1;
  171. cdfd.cStgMax = 4;
  172. cdfd.cStmMin = 1;
  173. cdfd.cStmMax = 6;
  174. cdfd.cbStmMin = 0;
  175. cdfd.cbStmMax = 10240;
  176. break;
  177. case DF_LARGE:
  178. cdfd.cDepthMin = 2;
  179. cdfd.cDepthMax = 5;
  180. cdfd.cStgMin = 2;
  181. cdfd.cStgMax = 10;
  182. cdfd.cStmMin = 0;
  183. cdfd.cStmMax = 8;
  184. cdfd.cbStmMin = 0;
  185. cdfd.cbStmMax = 20480;
  186. break;
  187. case DF_HUGE:
  188. cdfd.cDepthMin = 5;
  189. cdfd.cDepthMax = 10;
  190. cdfd.cStgMin = 5;
  191. cdfd.cStgMax = 30;
  192. cdfd.cStmMin = 0;
  193. cdfd.cStmMax = 10;
  194. cdfd.cbStmMin = 0;
  195. cdfd.cbStmMax = 40000;
  196. break;
  197. case DF_DIF:
  198. cdfd.cDepthMin = 5;
  199. cdfd.cDepthMax = 10;
  200. cdfd.cStgMin = 7;
  201. cdfd.cStgMax = 10;
  202. cdfd.cStmMin = 10;
  203. cdfd.cStmMax = 15;
  204. cdfd.cbStmMin = 100000;
  205. cdfd.cbStmMax = 150000;
  206. break;
  207. default:
  208. hr = E_FAIL;
  209. break;
  210. }
  211. DH_HRCHECK_ABORT (hr, TEXT("set CDFD"));
  212. hr = CreateTestDocfile (ppvdf,
  213. &cdfd,
  214. pCmdLine,
  215. pFileName);
  216. DH_HRCHECK (hr, TEXT("CreateTestDocfile"));
  217. ErrReturn:
  218. return hr;
  219. }
  220. //----------------------------------------------------------------------------
  221. // FUNCTION: MakeVirtualDF
  222. //
  223. // PARAMS: pChanceDF - ptr to pChanceDF
  224. // ppVirtualDF - bucket for pVirtualDF
  225. //
  226. // SYNOPSIS: This function should be called by
  227. // CreateTestDocfile
  228. // We need to create a storage file
  229. //
  230. // RETURN: hr. S_OK or whatever failure was encountered.
  231. //
  232. // HISTORY: 28-Feb-1997 SCousens Created.
  233. //----------------------------------------------------------------------------
  234. HRESULT MakeVirtualDF (
  235. IN ChanceDF *pChanceDF,
  236. OUT VirtualDF **ppVirtualDF)
  237. {
  238. HRESULT hr = S_OK;
  239. HRESULT hr2 = S_OK;
  240. VirtualCtrNode *pvcnRoot = NULL;
  241. // This is internal func. Shouldnt have to do this.
  242. DH_ASSERT (NULL != pChanceDF);
  243. DH_ASSERT (NULL != ppVirtualDF);
  244. DH_FUNCENTRY(&hr, DH_LVL_DFLIB, _TEXT("MakeVirtualDF"));
  245. DH_VDATEPTRIN (pChanceDF, ChanceDF);
  246. DH_VDATEPTROUT (ppVirtualDF, VirtualDF *);
  247. // Create the VirtualDocFile tree from the ChanceDocFile tree created in
  248. // the previous step. The VirtualDocFile tree consists of VirtualCtrNodes
  249. // and VirtualStmNodes.
  250. *ppVirtualDF = new VirtualDF();
  251. if (NULL == *ppVirtualDF)
  252. {
  253. hr = E_OUTOFMEMORY;
  254. }
  255. DH_HRCHECK_ABORT (hr, TEXT("new VirtualDF"));
  256. // Generate inmemory tree and docfile on disk
  257. hr = (*ppVirtualDF)->GenerateVirtualDF (pChanceDF, &pvcnRoot);
  258. DH_HRCHECK_ABORT (hr, TEXT("pVirtualDF->GenerateVirtualDF"));
  259. // Commit all stms and stgs in newly created storage file
  260. hr = ParseVirtualDFAndCommitAllOpenStgs (pvcnRoot,
  261. STGC_DEFAULT,
  262. NODE_INC_TOPSTG);
  263. DH_HRCHECK (hr, TEXT("ParseVirtualDFAndCommitAllOpenStgs"));
  264. ErrReturn:
  265. // close the file
  266. hr2 = ParseVirtualDFAndCloseOpenStgsStms (pvcnRoot, NODE_INC_TOPSTG);
  267. DH_HRCHECK (hr2, TEXT("ParseVirtualDFAndCloseOpenStgsStms"));
  268. hr = FirstError (hr, hr2);
  269. return hr;
  270. }
  271. //----------------------------------------------------------------------------
  272. //
  273. // FUNCTION: CleanupTestDocfile
  274. //
  275. // PARAMS: pVirtualDF - pVirtualDF to be deleted
  276. // fDeleteFile - Delete file?
  277. //
  278. // SYNOPSIS: Cleanup all items that were setup in CreateTestDocfile
  279. // - virtualdf
  280. // - delete docfile on disk (if there were no errors)
  281. //
  282. // RETURN: hr. S_OK or whatever failure was encountered.
  283. //
  284. // NOTES: Caller must NULLIFY their pVirtualDF pointer passed in
  285. // as this function deletes it.
  286. //
  287. // HISTORY: 28-Feb-1997 SCousens Created.
  288. //
  289. //----------------------------------------------------------------------------
  290. HRESULT CleanupTestDocfile (
  291. IN VirtualDF *pVirtualDF,
  292. IN HRESULT hrDeleteFile)
  293. {
  294. LPTSTR pFileName = NULL;
  295. HRESULT hr = S_OK;
  296. VirtualCtrNode *pvcnRoot;
  297. DH_FUNCENTRY (&hr, DH_LVL_DFLIB, TEXT("CleanupTestDocfile"));
  298. DH_VDATEPTRIN (pVirtualDF, VirtualDF);
  299. // Make sure everything in the docfile is closed
  300. pvcnRoot = pVirtualDF->GetVirtualDFRoot ();
  301. if (NULL != pvcnRoot)
  302. {
  303. hr = ParseVirtualDFAndCloseOpenStgsStms (pvcnRoot, NODE_INC_TOPSTG);
  304. DH_HRCHECK (hr, TEXT("ParseVirtualDFAndCloseOpenStgsStms"));
  305. }
  306. // Get file name
  307. if ((S_OK == hrDeleteFile || ALWAYS == hrDeleteFile) && NULL != pvcnRoot)
  308. {
  309. pFileName= new TCHAR[_tcslen (pVirtualDF->GetDocFileName ())+1];
  310. if (pFileName != NULL)
  311. {
  312. _tcscpy (pFileName, pVirtualDF->GetDocFileName ());
  313. }
  314. }
  315. // Delete Virtual docfile tree
  316. if (NULL != pVirtualDF)
  317. {
  318. hr = pVirtualDF->DeleteVirtualDocFileTree (pvcnRoot);
  319. DH_HRCHECK (hr, TEXT("pVirtualDF->DeleteVirtualFileDocTree"));
  320. delete pVirtualDF;
  321. pVirtualDF = NULL;
  322. }
  323. // Delete the docfile on disk
  324. if ((S_OK == hr) && (NULL != pFileName))
  325. {
  326. if (FALSE == DeleteFile(pFileName))
  327. {
  328. hr = HRESULT_FROM_WIN32 (GetLastError());
  329. DH_HRCHECK (hr, TEXT("DeleteFile"));
  330. }
  331. }
  332. // Delete the docfile name
  333. if (NULL != pFileName)
  334. {
  335. delete []pFileName;
  336. pFileName = NULL;
  337. }
  338. return hr;
  339. }