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.

555 lines
18 KiB

  1. // Microsoft OLE
  2. // Copyright (C) Microsoft Corporation, 1992 - 1996.
  3. // All rights reserved.
  4. //
  5. // File: stgwrap.cxx
  6. //
  7. // Contents: Wrap the StgOpen/Create apis
  8. // This is to permit nssfile and conversion testing
  9. // and to test the new Stg*Ex apis
  10. // using the same codebase as the docfile tests with
  11. // minimal changes to that codebase.
  12. //
  13. // Functions: StgInitStgFormatWrapper
  14. // mStgCreateDocfile
  15. // mStgOpenStorage
  16. //
  17. // Notes: -Only do this fancy stuff if we are not doing
  18. // vanilla docfile testing (ie _OLE_NSS_ is defd)
  19. // -Hook StgOpen/Create only if _OLE_NSS_ is defd
  20. // *and* _HOOK_STGAPI_ is defd also.
  21. //
  22. // NOTE: To turn on nss/cnv functionality you must add
  23. // -D_OLE_NSS_ -D_HOOK_STGAPI_ to you C_DEFINES
  24. // in daytona.mk
  25. //
  26. // History: SCousens 24-Feb-97 Created
  27. //--------------------------------------------------------------------------
  28. #include <dfheader.hxx>
  29. #pragma hdrstop
  30. // Debug object declaration
  31. DH_DECLARE;
  32. /* only do this fancy stuff if we are not doing vanilla docfile testing */
  33. #ifdef _OLE_NSS_
  34. // This is a global variable. We can do this because it controls
  35. // the state for this entire process. No process will be mixing
  36. // nssfile tests with conversion tests or any other combination.
  37. // Doing it this way gives instant access at various times in the
  38. // tests without rewriting anything.
  39. // Setting of this variable will be done by passing a parameter
  40. // on the commandline to start the process, and calling the init
  41. // function below.
  42. TSTTYPE g_uCreateType = TSTTYPE_DEFAULT; //DEFAULT, DOCFILE, NSSFILE
  43. TSTTYPE g_uOpenType = TSTTYPE_DEFAULT; //DEFAULT, DOCFILE, NSSFILE
  44. DWORD g_fRegistryBits= REG_OPEN_AS | REG_CREATE_AS | REG_CNSS_ENABLE;
  45. ULONG g_ulSectorSize = DEFAULT_SECTOR_SIZE;
  46. // DEFAULT_SECTOR_SIZE, LARGE_SECTOR_SIZE
  47. // internal functions
  48. BOOL StgCheckRegistry (void);
  49. BOOL StgCheckVolumeInformation (void);
  50. //********************************************************************
  51. // Function: StgInitStgFormatWrapper (multiple)
  52. //
  53. // Synopsis: Set the following global variables:
  54. // g_uCreateType - type of storage to create
  55. // g_uOpenType - type of storage to open
  56. //
  57. // 1. Check the cmdline, set open and create modes
  58. // (default, nssfile, docfile)
  59. // 2. Check the registry.
  60. // If NSS not set and want it, spew
  61. // 3. Check disk sub storage type.
  62. // If !NTFS and want it, spew
  63. // Return: TRUE if no issues
  64. // FALSE if nss set and not NTFS disk,
  65. //
  66. //********************************************************************
  67. BOOL StgInitStgFormatWrapper (int argc, char *argv[])
  68. {
  69. HRESULT hr = S_OK;
  70. BOOL fRet = TRUE;
  71. // check cmdline for open and create and sector size switches.
  72. CBaseCmdlineObj CCreateDF (OLESTR("CreateAs"),
  73. OLESTR("Create doc/nss/flat file"),
  74. OLESTR("default"));
  75. CBaseCmdlineObj COpenDF (OLESTR("OpenAs"),
  76. OLESTR("Open doc/nss/flat file"),
  77. OLESTR("default"));
  78. CBaseCmdlineObj CSectorSize (OLESTR("SectorSize"),
  79. OLESTR("Sector size"),
  80. OLESTR("default"));
  81. CBaseCmdlineObj *CArgList[] =
  82. {
  83. &CCreateDF,
  84. &COpenDF,
  85. &CSectorSize
  86. } ;
  87. CCmdline CCmdlineArgs(argc, argv);
  88. if (CMDLINE_NO_ERROR != CCmdlineArgs.QueryError())
  89. {
  90. hr = E_FAIL ;
  91. }
  92. if (S_OK == hr)
  93. {
  94. if (CMDLINE_NO_ERROR !=
  95. CCmdlineArgs.Parse(
  96. CArgList,
  97. ( sizeof(CArgList) / sizeof(CArgList[0]) ),
  98. FALSE))
  99. {
  100. hr = E_FAIL ;
  101. }
  102. }
  103. if (S_OK == hr)
  104. {
  105. // look for Create
  106. // default as docfile
  107. if (TRUE == CCreateDF.IsFound ())
  108. {
  109. if (NULL == _olestricmp (CCreateDF.GetValue (), OLESTR(SZ_NSSFILE)))
  110. {
  111. g_uCreateType = TSTTYPE_NSSFILE;
  112. }
  113. else if (NULL == _olestricmp (CCreateDF.GetValue (), OLESTR(SZ_DOCFILE)))
  114. {
  115. g_uCreateType = TSTTYPE_DOCFILE;
  116. }
  117. else if (NULL == _olestricmp (CCreateDF.GetValue (), OLESTR(SZ_FLATFILE)))
  118. {
  119. g_uCreateType = TSTTYPE_FLATFILE;
  120. g_uOpenType = TSTTYPE_FLATFILE;
  121. }
  122. }
  123. // look for Open
  124. // default as docfile
  125. if (TRUE == COpenDF.IsFound ())
  126. {
  127. if (NULL == _olestricmp (COpenDF.GetValue (), OLESTR(SZ_NSSFILE)))
  128. {
  129. g_uOpenType = TSTTYPE_NSSFILE;
  130. }
  131. else if (NULL == _olestricmp (COpenDF.GetValue (), OLESTR(SZ_DOCFILE)))
  132. {
  133. g_uOpenType = TSTTYPE_DOCFILE;
  134. }
  135. else if (NULL == _olestricmp (COpenDF.GetValue (), OLESTR(SZ_FLATFILE)))
  136. {
  137. g_uCreateType = TSTTYPE_FLATFILE;
  138. g_uOpenType = TSTTYPE_FLATFILE;
  139. }
  140. }
  141. // look for Sector size
  142. // default as SECTORTYPE_DEDAULT
  143. if (TRUE == CSectorSize.IsFound ())
  144. {
  145. if (NULL == _olestricmp (CSectorSize.GetValue (), OLESTR(SZ_DEFAULT)))
  146. {
  147. g_ulSectorSize = DEFAULT_SECTOR_SIZE;
  148. }
  149. else if (NULL == _olestricmp (CSectorSize.GetValue (), OLESTR(SZ_LARGE)))
  150. {
  151. g_ulSectorSize = LARGE_SECTOR_SIZE;
  152. }
  153. }
  154. }
  155. // Now check the registry for spewage reasons
  156. StgCheckRegistry ();
  157. // finally, is the disk sub-system compatible with NSS
  158. if (FALSE == StgCheckVolumeInformation ())
  159. {
  160. fRet = FALSE;
  161. }
  162. return fRet;
  163. }
  164. BOOL StgInitStgFormatWrapper (TCHAR *pCreateType, TCHAR *pOpenType)
  165. {
  166. BOOL fRet = TRUE;
  167. if (0 == _tcscmp(pCreateType, TSZ_NSSFILE))
  168. {
  169. g_uCreateType = TSTTYPE_NSSFILE;
  170. }
  171. else if (0 == _tcscmp(pCreateType, TSZ_DOCFILE))
  172. {
  173. g_uCreateType = TSTTYPE_DOCFILE;
  174. }
  175. else if (0 == _tcscmp(pCreateType, TSZ_FLATFILE))
  176. {
  177. g_uCreateType = TSTTYPE_FLATFILE;
  178. g_uOpenType = TSTTYPE_FLATFILE;
  179. }
  180. if (0 == _tcscmp(pOpenType, TSZ_NSSFILE))
  181. {
  182. g_uOpenType = TSTTYPE_NSSFILE;
  183. }
  184. else if (0 == _tcscmp(pOpenType, TSZ_DOCFILE))
  185. {
  186. g_uOpenType = TSTTYPE_DOCFILE;
  187. }
  188. else if (0 == _tcscmp(pOpenType, TSZ_FLATFILE))
  189. {
  190. g_uCreateType = TSTTYPE_FLATFILE;
  191. g_uOpenType = TSTTYPE_FLATFILE;
  192. }
  193. // Now check the registry, and override where necessary
  194. StgCheckRegistry ();
  195. // finally, is the disk sub-system compatible with NSS
  196. if (FALSE == StgCheckVolumeInformation ())
  197. {
  198. fRet = FALSE;
  199. }
  200. return fRet;
  201. }
  202. //********************************************************************
  203. // Function: StgCheckRegistryFor
  204. //
  205. // Synopsis: Check the registry to see if OLE will create nssfiles.
  206. // Adjust g_fRegistrySet to whether NSS regvalues are set.
  207. //
  208. // Return: TRUE if ok
  209. // FALSE if registry not set for NSS files
  210. //
  211. //********************************************************************
  212. BOOL StgCheckRegistry (void)
  213. {
  214. HKEY hKey;
  215. LONG lErr;
  216. TCHAR tszData[10];
  217. DWORD dwType, dwSize;
  218. LPCTSTR ptszRegKey = {TEXT("Software\\Microsoft\\OLE")};
  219. LPCTSTR ptszNssRegValue= {TEXT("EnableNtfsStructuredStorage")};
  220. LPCTSTR ptszCnssRegValue= {TEXT("EnableCNSS")};
  221. DH_FUNCENTRY (NULL, DH_LVL_DFLIB, TEXT("StgCheckRegistryForNSS"));
  222. // get whats in the registry
  223. lErr = RegOpenKeyEx (HKEY_LOCAL_MACHINE, ptszRegKey, 0, KEY_READ, &hKey);
  224. if (ERROR_SUCCESS == lErr)
  225. {
  226. dwSize = sizeof (tszData);
  227. lErr = RegQueryValueEx (hKey,
  228. ptszNssRegValue,
  229. 0,
  230. &dwType,
  231. (LPBYTE)tszData,
  232. &dwSize);
  233. if (ERROR_SUCCESS != lErr)
  234. {
  235. DH_TRACE ((DH_LVL_DFLIB, TEXT("RegQueryValueEx error; lErr=%#lx"), lErr));
  236. }
  237. else
  238. {
  239. // bit 2 /createas:
  240. if (TCHAR('Y') == tszData[0] || TCHAR('y') == tszData[0])
  241. {
  242. g_fRegistryBits |= REG_CREATE_AS;
  243. }
  244. else if (TCHAR('N') == tszData[0] || TCHAR('n') == tszData[0])
  245. {
  246. g_fRegistryBits &= ~REG_CREATE_AS;
  247. }
  248. // bit 1 /openas:
  249. if (TCHAR('Y') == tszData[1] || TCHAR('y') == tszData[1])
  250. {
  251. g_fRegistryBits |= REG_OPEN_AS;
  252. }
  253. else if (TCHAR('N') == tszData[1] || TCHAR('n') == tszData[1])
  254. {
  255. g_fRegistryBits &= ~REG_OPEN_AS;
  256. }
  257. }
  258. dwSize = sizeof (tszData);
  259. tszData[0] = tszData[1] = 0;
  260. lErr = RegQueryValueEx (hKey,
  261. ptszCnssRegValue,
  262. 0,
  263. &dwType,
  264. (LPBYTE)tszData,
  265. &dwSize);
  266. if (ERROR_SUCCESS != lErr)
  267. {
  268. DH_TRACE ((DH_LVL_DFLIB, TEXT("RegQueryValueEx error; lErr=%#lx"), lErr));
  269. }
  270. else
  271. {
  272. // bit 3 enable cnss
  273. if (TCHAR('Y') == tszData[0] || TCHAR('y') == tszData[0])
  274. {
  275. g_fRegistryBits |= REG_CNSS_ENABLE;
  276. }
  277. else if (TCHAR('N') == tszData[0] || TCHAR('n') == tszData[0])
  278. {
  279. g_fRegistryBits &= ~REG_CNSS_ENABLE;
  280. }
  281. }
  282. RegCloseKey (hKey);
  283. }
  284. else
  285. {
  286. DH_TRACE ((DH_LVL_DFLIB, TEXT("RegOpenKeyEx error; lErr=%#lx"), lErr));
  287. }
  288. return TRUE;
  289. }
  290. //********************************************************************
  291. // Function: StgCheckVolumeInformation
  292. //
  293. // Synopsis: If not doing docfiles, check the disk sub-system.
  294. // If not NTFS, spew
  295. //
  296. // Return: TRUE if ok
  297. // FALSE if doing nss, disk not ntfs
  298. //
  299. //********************************************************************
  300. BOOL StgCheckVolumeInformation (void)
  301. {
  302. LPTSTR pstrType = TSZ_DOCFILE;
  303. TCHAR pFileSystemNameBuffer[10];
  304. DWORD dwFileSystemFlags;
  305. BOOL fVolInfo = FALSE;
  306. BOOL fNTFS = FALSE;
  307. // if we are forcing an nssfile somewhere, or we
  308. // are going for default of nssfile
  309. if (TSTTYPE_NSSFILE == g_uOpenType || TSTTYPE_NSSFILE == g_uCreateType)
  310. {
  311. fVolInfo = GetVolumeInformation (NULL,
  312. NULL,
  313. 0,
  314. NULL,
  315. 0,
  316. &dwFileSystemFlags,
  317. pFileSystemNameBuffer,
  318. ARRAYSIZE (pFileSystemNameBuffer));
  319. // if we can detect the disk subsystem
  320. if (0 != fVolInfo)
  321. {
  322. // if not NTFS, 'fix' two flags
  323. if (0 == lstrcmp (pFileSystemNameBuffer, TEXT("NTFS")))
  324. {
  325. fNTFS = TRUE;
  326. }
  327. }
  328. if (FALSE == fNTFS)
  329. {
  330. DH_TRACE ((DH_LVL_ALWAYS,
  331. TEXT("WARNING: Disk subsystem not NTFS! NSS not possible!")));
  332. }
  333. }
  334. else
  335. {
  336. fNTFS = TRUE;
  337. }
  338. return fNTFS;
  339. }
  340. // hook the stgcreatedocfile stgopenstorage apis for debugging purposes -scousens
  341. #ifdef _HOOK_STGAPI_
  342. #undef StgCreateDocfile
  343. #undef StgOpenStorage
  344. //---------------------------------------------------------------
  345. // @doc
  346. // @func mStgCreateDocfile |
  347. // Wraps calls to StgCreateDocfile. This is a mechanism
  348. // to conditionally get current code to call the
  349. // StgCreateStorageEx API without changing the existing
  350. // codebase.
  351. //
  352. // @rdesc returns whatever the called function returned.
  353. //
  354. // @comm condition set in StgInitStgFormatWrap
  355. //
  356. // @comm The parameters that differ between the two APIs
  357. // are essentially ignored/defaulted.
  358. //---------------------------------------------------------------
  359. HRESULT mStgCreateDocfile(const OLECHAR FAR* pwcsName,
  360. DWORD grfMode,
  361. DWORD reserved,
  362. IStorage FAR * FAR *ppstgOpen)
  363. {
  364. HRESULT hr;
  365. // With 1795 changes to "dwReserved" Parameter to -> version number,
  366. // sector size (allowed is 512, 4096 only) and reserved parameter as
  367. // typedef struct tagSTGOPTIONS
  368. // {
  369. // USHORT usVersion; // Version 1
  370. // USHORT reserved; // must be 0 for padding
  371. // ULONG ulSectorSize; // docfile header sector size (512)
  372. // } STGOPTIONS;
  373. STGOPTIONS stgOptions;
  374. stgOptions.usVersion = 1;
  375. stgOptions.reserved = (USHORT)reserved; // Take from function arg
  376. stgOptions.ulSectorSize = g_ulSectorSize;
  377. DH_FUNCENTRY (NULL, DH_LVL_STGAPI, TEXT("mStgCreateDocfile:"));
  378. // If default, use old api
  379. if (TSTTYPE_DEFAULT == g_uCreateType)
  380. {
  381. hr = StgCreateDocfile (pwcsName,
  382. grfMode,
  383. reserved,
  384. ppstgOpen);
  385. DH_TRACE((DH_LVL_STGAPI, TEXT("StgCreateDocfile; mode=%#lx; hr=%#lx"), grfMode, hr));
  386. }
  387. // force docfile with StgCreateStorageEx (STGFMT_DOCFILE)
  388. else if (TSTTYPE_DOCFILE == g_uCreateType)
  389. {
  390. hr = StgCreateStorageEx (pwcsName,
  391. grfMode,
  392. STGFMT_DOCFILE, //force it to be a docfile
  393. 0,
  394. &stgOptions,
  395. (void*)reserved,
  396. IID_IStorage,
  397. (void**)ppstgOpen);
  398. DH_TRACE((DH_LVL_STGAPI, TEXT("StgCreateStorageEx (df); mode=%#lx; sectorsize=%#lx; hr=%#lx"), grfMode, g_ulSectorSize,hr));
  399. }
  400. // force flatfile with StgCreateStorageEx (STGFMT_FILE)
  401. else if (TSTTYPE_FLATFILE == g_uCreateType)
  402. {
  403. hr = StgCreateStorageEx (pwcsName,
  404. grfMode,
  405. STGFMT_FILE, //force it to be a flatfile
  406. 0,
  407. &stgOptions,
  408. (void*)reserved,
  409. IID_IStorage,
  410. (void**)ppstgOpen);
  411. DH_TRACE((DH_LVL_STGAPI, TEXT("StgCreateStorageEx (df); mode=%#lx; sectorsize=%#lx; hr=%#lx"), grfMode, g_ulSectorSize,hr));
  412. }
  413. // else try force nssfile with StgCreateStorageEx ()
  414. else
  415. {
  416. hr = StgCreateStorageEx (pwcsName,
  417. grfMode,
  418. STGFMT_GENERIC, //force it to be a nssfile (if possible)
  419. 0,
  420. &stgOptions,
  421. (void*)reserved,
  422. IID_IStorage,
  423. (void**)ppstgOpen);
  424. DH_TRACE((DH_LVL_STGAPI, TEXT("StgCreateStorageEx (df); mode=%#lx; hr=%#lx, sectorsize=%#lx"), grfMode, hr, g_ulSectorSize));
  425. }
  426. return (hr);
  427. }
  428. //---------------------------------------------------------------
  429. // @doc
  430. // @func mStgOpenStorage |
  431. // Wraps calls to StgOpenStorage. This is a mechanism
  432. // to conditionally get current code to call the
  433. // StgOpenStorageEx API without changing the existing
  434. // codebase.
  435. //
  436. // @rdesc returns whatever the called function returned.
  437. //
  438. // @comm condition set in StgInitStgFormatWrap
  439. //
  440. // @comm The parameters that differ between the two APIs
  441. // are essentially ignored/defaulted.
  442. //---------------------------------------------------------------
  443. HRESULT mStgOpenStorage (const OLECHAR FAR* pwcsName,
  444. IStorage FAR *pstgPriority,
  445. DWORD grfMode,
  446. SNB snbExclude,
  447. DWORD reserved,
  448. IStorage FAR * FAR *ppstgOpen)
  449. {
  450. HRESULT hr;
  451. DH_FUNCENTRY (NULL, DH_LVL_STGAPI, TEXT("mStgOpenStorage:"));
  452. // If default, or we have snbs or priority stgs, use old api
  453. if (TSTTYPE_DEFAULT == g_uOpenType ||
  454. NULL != pstgPriority ||
  455. NULL != snbExclude)
  456. {
  457. hr = StgOpenStorage (pwcsName,
  458. pstgPriority,
  459. grfMode,
  460. snbExclude,
  461. reserved,
  462. ppstgOpen);
  463. DH_TRACE((DH_LVL_STGAPI, TEXT("StgOpenStorage; mode=%#lx; hr=%#lx"), grfMode, hr));
  464. }
  465. // Force docfile with StgOpenStorageEx (STGFMT_DOCFILE)
  466. else if (TSTTYPE_DOCFILE == g_uOpenType)
  467. {
  468. hr = StgOpenStorageEx (pwcsName,
  469. grfMode,
  470. STGFMT_DOCFILE, //force it to be a docfile
  471. 0,
  472. NULL, // (void*)reserved -> STGOPTIONS*, BUGBUG
  473. (void*)reserved,
  474. IID_IStorage,
  475. (void**)ppstgOpen);
  476. DH_TRACE((DH_LVL_STGAPI, TEXT("StgOpenStorageEx (df); mode=%#lx; hr=%#lx"), grfMode, hr));
  477. }
  478. // Force flatfile with StgOpenStorageEx (STGFMT_FILE)
  479. else if (TSTTYPE_FLATFILE == g_uOpenType)
  480. {
  481. hr = StgOpenStorageEx (pwcsName,
  482. grfMode,
  483. STGFMT_FILE, //force it to be a flatfile
  484. 0,
  485. NULL, // (void*)reserved -> STGOPTIONS*, BUGBUG
  486. (void*)reserved,
  487. IID_IStorage,
  488. (void**)ppstgOpen);
  489. DH_TRACE((DH_LVL_STGAPI, TEXT("StgOpenStorageEx (df); mode=%#lx; hr=%#lx"), grfMode, hr));
  490. }
  491. // else try force nssfile with StgOpenStorageEx ()
  492. else
  493. {
  494. hr = StgOpenStorageEx (pwcsName,
  495. grfMode,
  496. STGFMT_GENERIC, //force it to be a nssfile (if possible)
  497. 0,
  498. NULL, // (void*)reserved -> STGOPTIONS*, BUGBUG
  499. (void*)reserved,
  500. IID_IStorage,
  501. (void**)ppstgOpen);
  502. DH_TRACE((DH_LVL_STGAPI, TEXT("StgOpenStorageEx(nss); mode=%#lx; hr=%#lx"), grfMode, hr));
  503. }
  504. return (hr);
  505. }
  506. #endif /* _HOOK_STGAPI_ */
  507. #endif /* _OLE_NSS_ */