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.

3945 lines
104 KiB

  1. // ITSFS.CPP -- Implementation for the class CITFileSystem
  2. #include "StdAfx.h"
  3. //#include <stdio.h>
  4. const WCHAR *pwscSpaceNameListStream = L"::DataSpace/NameList";
  5. const WCHAR *pwcsSpaceNameStorage = L"::DataSpace/Storage/";
  6. const WCHAR *pwcsSpaceContentSuffix = L"/Content";
  7. const WCHAR *pwcsTransformListSuffix = L"/Transform/List";
  8. const WCHAR *pwcsSpanInfoSuffix = L"/SpanInfo";
  9. const WCHAR *pwcsTransformSubStorage = L"/Transform/";
  10. const WCHAR *pwcsControlDataSuffix = L"/ControlData";
  11. const WCHAR *pwcsInstanceSubStorage = L"/InstanceData/";
  12. const WCHAR *pwcsTransformStorage = L"::Transform/";
  13. const WCHAR *pwcsUncompressedSpace = L"Uncompressed";
  14. const WCHAR *pwcsLZXSpace = L"MSCompressed";
  15. // Creators:
  16. HRESULT __stdcall CITFileSystem::CreateITFileSystem
  17. (IUnknown *punkOuter, const WCHAR * pwcsName, DWORD grfMode,
  18. PITS_Control_Data pControlData,
  19. LCID lcid,
  20. IStorage ** ppstgOpen
  21. )
  22. {
  23. CSyncWith sw(g_csITFS);
  24. ILockBytes *pLKB = NULL;
  25. HRESULT hr = CFSLockBytes::Create(NULL, pwcsName,
  26. (grfMode & (~SHARE_MASK) & (~RW_ACCESS_MASK))
  27. | STGM_READWRITE
  28. | STGM_CREATE
  29. | STGM_SHARE_EXCLUSIVE,
  30. &pLKB
  31. );
  32. if (SUCCEEDED(hr))
  33. {
  34. hr = CreateITFSOnLockBytes(punkOuter, pLKB, grfMode, pControlData, lcid, ppstgOpen);
  35. pLKB->Release(); // if the Create on LockBytes succeeded, it AddRef'd pLKB.
  36. }
  37. return hr;
  38. }
  39. HRESULT __stdcall CITFileSystem::CreateITFSOnLockBytes
  40. (IUnknown *punkOuter, ILockBytes * pLKB, DWORD grfMode,
  41. PITS_Control_Data pControlData,
  42. LCID lcid,
  43. IStorage **ppstgOpen
  44. )
  45. {
  46. CSyncWith sw(g_csITFS);
  47. CITFileSystem *pITFS = New CITFileSystem(punkOuter);
  48. if (!pITFS)
  49. {
  50. pLKB->Release();
  51. return STG_E_INSUFFICIENTMEMORY;
  52. }
  53. IITFileSystem *pIITFileSystem = NULL;
  54. IStorageITEx *pStorage = NULL;
  55. pITFS->AddRef(); // Because of the reference counting tricks
  56. // within InitCreateOnLockBytes.
  57. HRESULT hr = pITFS->m_ImpITFileSystem.InitCreateOnLockBytes
  58. (pLKB, grfMode, pControlData, lcid);
  59. if (SUCCEEDED(hr))
  60. {
  61. pIITFileSystem = (IITFileSystem *) &pITFS->m_ImpITFileSystem;
  62. // If the OpenStorage call below succeeds, it will AddRef pITFS.
  63. hr = pIITFileSystem->OpenStorage(NULL, L"/", grfMode, &pStorage);
  64. pITFS->Release(); // To match the AddRef call above.
  65. }
  66. else delete pITFS;
  67. *ppstgOpen = pStorage;
  68. return hr;
  69. }
  70. IITFileSystem *CITFileSystem::CImpITFileSystem::FindFileSystem(const WCHAR *pwcsPath)
  71. {
  72. CSyncWith sw(g_csITFS);
  73. CImpITFileSystem *pITFS = (CImpITFileSystem *) g_pImpITFileSystemList;
  74. for (; pITFS; pITFS = (CImpITFileSystem *) pITFS->NextObject())
  75. if (!wcsicmp_0x0409(pwcsPath, pITFS->m_awszFileName))
  76. {
  77. pITFS->AddRef();
  78. return pITFS;
  79. }
  80. return NULL;
  81. }
  82. HRESULT __stdcall CITFileSystem::IsITFile(const WCHAR * pwcsName)
  83. {
  84. CSyncWith sw(g_csITFS);
  85. IITFileSystem *pITFS = CImpITFileSystem::FindFileSystem(pwcsName);
  86. if (pITFS)
  87. {
  88. pITFS->Release();
  89. return S_OK;
  90. }
  91. ILockBytes *pLKB = NULL;
  92. ITSFileHeader ITFH;
  93. HRESULT hr = CFSLockBytes::Open(NULL, pwcsName, STGM_READ | STGM_SHARE_DENY_NONE, &pLKB);
  94. if (SUCCEEDED(hr))
  95. hr = IsITLockBytes(pLKB);
  96. else hr = S_FALSE;
  97. if (pLKB)
  98. pLKB->Release();
  99. return hr;
  100. }
  101. HRESULT __stdcall CITFileSystem::IsITLockBytes(ILockBytes * pLKB)
  102. {
  103. CSyncWith sw(g_csITFS);
  104. ITSFileHeader ITFH;
  105. ULONG cbRead = 0;
  106. HRESULT hr = pLKB->ReadAt(CULINT(0).Uli(), &ITFH, sizeof(ITFH), &cbRead);
  107. if ( hr != S_OK
  108. || cbRead < sizeof(ITSFileHeaderV2)
  109. || ITFH.uMagic != MAGIC_ITS_FILE
  110. || ITFH.uFormatVersion < FirstReleasedVersion
  111. || ITFH.uFormatVersion > CurrentFileFormatVersion
  112. )
  113. hr = S_FALSE;
  114. else
  115. if ( ITFH.uFormatVersion == CurrentFileFormatVersion
  116. && cbRead == sizeof(ITSFileHeader)
  117. )
  118. hr = S_OK;
  119. else
  120. if (ITFH.uFormatVersion == FirstReleasedVersion)
  121. {
  122. ITFH.offPathMgrOrigin = 0;
  123. hr = S_OK;
  124. }
  125. else
  126. {
  127. RonM_ASSERT(FALSE); // If this assert fires, we have a old version
  128. // that isn't being handled.
  129. hr = S_FALSE;
  130. }
  131. return hr;
  132. }
  133. HRESULT __stdcall CITFileSystem::QueryFileStampAndLocale
  134. (const WCHAR *pwcsName, DWORD *pFileStamp, DWORD *pFileLocale)
  135. {
  136. CSyncWith sw(g_csITFS);
  137. IITFileSystem *pITFS = CImpITFileSystem::FindFileSystem(pwcsName);
  138. if (pITFS)
  139. {
  140. HRESULT hr = pITFS->QueryFileStampAndLocale(pFileStamp, pFileLocale);
  141. pITFS->Release();
  142. return hr;
  143. }
  144. ILockBytes *pLKB = NULL;
  145. ITSFileHeader ITFH;
  146. HRESULT hr = CFSLockBytes::Open(NULL, pwcsName, STGM_READ | STGM_SHARE_DENY_NONE, &pLKB);
  147. if (SUCCEEDED(hr))
  148. {
  149. hr = QueryLockByteStampAndLocale(pLKB, pFileStamp, pFileLocale);
  150. pLKB->Release();
  151. }
  152. return hr;
  153. }
  154. HRESULT __stdcall CITFileSystem::CImpITFileSystem::QueryFileStampAndLocale
  155. (DWORD *pFileStamp, DWORD *pFileLocale)
  156. {
  157. if (pFileStamp)
  158. *pFileStamp = m_itfsh.dwStamp;
  159. if (pFileLocale)
  160. *pFileLocale = m_itfsh.lcid;
  161. return S_OK;
  162. }
  163. HRESULT __stdcall CITFileSystem::CImpITFileSystem::CountWrites()
  164. {
  165. InterlockedIncrement((long *) &m_itfsh.dwStamp);
  166. return S_OK;
  167. }
  168. HRESULT __stdcall CITFileSystem::QueryLockByteStampAndLocale
  169. (ILockBytes * plkbyt, DWORD *pFileStamp, DWORD *pFileLocale)
  170. {
  171. CSyncWith sw(g_csITFS);
  172. ITSFileHeader ITFH;
  173. ULONG cbRead = 0;
  174. HRESULT hr = plkbyt->ReadAt(CULINT(0).Uli(), &ITFH, sizeof(ITFH), &cbRead);
  175. if (!SUCCEEDED(hr))
  176. return hr;
  177. if ( cbRead < sizeof(ITSFileHeaderV2)
  178. || ITFH.uMagic != MAGIC_ITS_FILE
  179. || ITFH.uFormatVersion < FirstReleasedVersion
  180. || ITFH.uFormatVersion > CurrentFileFormatVersion
  181. )
  182. return STG_E_INVALIDHEADER;
  183. else
  184. if ( ITFH.uFormatVersion == CurrentFileFormatVersion
  185. && cbRead == sizeof(ITSFileHeader)
  186. )
  187. {
  188. }
  189. else
  190. if (ITFH.uFormatVersion == FirstReleasedVersion)
  191. {
  192. }
  193. else
  194. {
  195. RonM_ASSERT(FALSE); // If this assert fires, we have a old version
  196. // that isn't being handled.
  197. return STG_E_INVALIDHEADER;
  198. }
  199. if (pFileStamp)
  200. *pFileStamp = ITFH.dwStamp;
  201. if (pFileLocale)
  202. *pFileLocale = ITFH.lcid;
  203. return S_OK;
  204. }
  205. HRESULT __stdcall CITFileSystem::OpenITFileSystem(IUnknown *punkOuter,
  206. const WCHAR * pwcsName,
  207. DWORD grfMode,
  208. IStorageITEx ** ppstgOpen
  209. )
  210. {
  211. HRESULT hr;
  212. IStorage *pStorage = NULL;
  213. CSyncWith sw(g_csITFS);
  214. IITFileSystem *pIITFS = CImpITFileSystem::FindFileSystem(pwcsName);
  215. if (pIITFS)
  216. {
  217. if (!pIITFS->IsCompacting())
  218. {
  219. hr = pIITFS->OpenStorage(punkOuter, L"/", grfMode, ppstgOpen);
  220. }
  221. else
  222. {
  223. hr = E_FAIL;
  224. }
  225. pIITFS->Release();
  226. return hr;
  227. }
  228. ILockBytes *pLKB = NULL;
  229. hr = CFSLockBytes::Open(NULL, pwcsName, grfMode, &pLKB);
  230. if (SUCCEEDED(hr))
  231. {
  232. hr = OpenITFSOnLockBytes(punkOuter, pLKB, grfMode, ppstgOpen);
  233. pLKB->Release(); // If OpenITFSOnLockBytes succeeds, it will AddRef pLKB.
  234. }
  235. return hr;
  236. }
  237. HRESULT __stdcall CITFileSystem::OpenITFSOnLockBytes
  238. (IUnknown *punkOuter, ILockBytes * plkbyt, DWORD grfMode,
  239. IStorageITEx ** ppstgOpen
  240. )
  241. {
  242. CSyncWith sw(g_csITFS);
  243. CITFileSystem *pITFS = New CITFileSystem(punkOuter);
  244. if (!pITFS) return STG_E_INSUFFICIENTMEMORY;
  245. IStorage *pStorage = NULL;
  246. pITFS->AddRef(); // Because of the reference counting tricks
  247. // within InitCreateOnLockBytes.
  248. HRESULT hr = pITFS->m_ImpITFileSystem.InitOpenOnLockBytes(plkbyt, grfMode);
  249. if (SUCCEEDED(hr))
  250. {
  251. // If the OpenStorage call below succeeds, it will AddRef pITFS.
  252. hr = pITFS->m_ImpITFileSystem.OpenStorage(NULL, L"/", grfMode, ppstgOpen);
  253. pITFS->Release(); // To match the AddRef above
  254. }
  255. else delete pITFS;
  256. return hr;
  257. }
  258. HRESULT __stdcall CITFileSystem::SetITFSTimes
  259. (WCHAR const * pwcsName, FILETIME const * pctime,
  260. FILETIME const * patime, FILETIME const * pmtime
  261. )
  262. {
  263. CSyncWith sw(g_csITFS);
  264. IITFileSystem *pITFS = CImpITFileSystem::FindFileSystem(pwcsName);
  265. if (pITFS)
  266. {
  267. HRESULT hr = pITFS->SetITFSTimes(pctime, patime, pmtime);
  268. pITFS->Release();
  269. return S_OK;
  270. }
  271. ILockBytes *pLKB = NULL;
  272. HRESULT hr = CFSLockBytes::Open(NULL, pwcsName, STGM_READ, &pLKB);
  273. if (SUCCEEDED(hr))
  274. if (S_OK == IsITLockBytes(pLKB))
  275. hr = ((CFSLockBytes::CImpILockBytes *)pLKB)->SetTimes(pctime, patime, pmtime);
  276. else hr = STG_E_FILENOTFOUND;
  277. if (pLKB)
  278. pLKB->Release();
  279. return hr;
  280. }
  281. HRESULT __stdcall CITFileSystem::DefaultControlData(PITS_Control_Data *ppControlData)
  282. {
  283. *ppControlData = NULL;
  284. PITSFS_Control_Data pCD = PITSFS_Control_Data(OLEHeap()->Alloc(sizeof(ITSFS_Control_Data)));
  285. if (!pCD) return STG_E_INSUFFICIENTMEMORY;
  286. pCD->cdwFollowing = 6;
  287. pCD->cdwITFS_Control = 5;
  288. pCD->dwMagic = MAGIC_ITSFS_CONTROL;
  289. pCD->dwVersion = 1;
  290. pCD->cbDirectoryBlock = 8192;
  291. pCD->cMinCacheEntries = 20;
  292. pCD->fFlags = fDefaultIsCompression;
  293. *ppControlData = PITS_Control_Data(pCD);
  294. return NO_ERROR;
  295. }
  296. // Constructor:
  297. CITFileSystem::CImpITFileSystem::CImpITFileSystem
  298. (CITFileSystem *pITFileSystem, IUnknown *punkOuter)
  299. : IITFileSystem(pITFileSystem, punkOuter)
  300. {
  301. ZeroMemory(&m_itfsh, sizeof(m_itfsh));
  302. m_itfsh.cbHeaderSize = sizeof(m_itfsh);
  303. m_itfsh.clsidFreeList = CLSID_NULL;
  304. m_itfsh.clsidPathMgr = CLSID_NULL;
  305. m_pLKBMedium = NULL;
  306. m_pPathManager = NULL;
  307. m_pSysPathManager = NULL;
  308. m_pFreeListManager = NULL;
  309. m_pTransformServices = NULL;
  310. m_pActiveStorageList = NULL;
  311. m_pActiveLockBytesList = NULL;
  312. m_fHeaderIsDirty = FALSE;
  313. m_fInitialed = FALSE;
  314. m_fReadOnly = FALSE;
  315. m_pwscDataSpaceNames = NULL;
  316. m_papTransformDescriptors = NULL;
  317. m_pStrmSpaceNames = NULL;
  318. m_cFSObjectRefs = 0;
  319. m_cwcFileName = 0;
  320. m_awszFileName[0] = 0;
  321. }
  322. // Destructor:
  323. CITFileSystem::CImpITFileSystem::~CImpITFileSystem(void)
  324. {
  325. // The code below is necessary to balance calls we've made to this->Release()
  326. // to hide circular references. The current set of circular references are:
  327. //
  328. // ** The free list Stream
  329. // ** The path manager LockBytes
  330. // ** The transform services object
  331. // ** Each storage whose name begins with ":"
  332. // ** Each stream whose name begins with ":"
  333. // ** Each active data space
  334. RonM_ASSERT(m_cFSObjectRefs != UINT(~0));
  335. if (!~m_cFSObjectRefs) return;
  336. #ifdef _DEBUG
  337. LONG cRefs =
  338. #endif // _DEBUG
  339. AddRef(); // To avoid recursive destruction when we eliminate the last
  340. // circular reference to this file system.
  341. RonM_ASSERT(cRefs == 1);
  342. for (; m_cFSObjectRefs--;) // Accounting for the hidden circular references.
  343. this->AddRef();
  344. // RonM_ASSERT(!m_pActiveStorageList);
  345. // RonM_ASSERT(!m_pActiveLockBytesList);
  346. DEBUGDEF(HRESULT hr)
  347. DEBUGDEF(ULONG c);
  348. if (m_papTransformDescriptors)
  349. {
  350. RonM_ASSERT(m_pStrmSpaceNames);
  351. RonM_ASSERT(m_pwscDataSpaceNames);
  352. UINT cSpaces = m_pwscDataSpaceNames[1];
  353. for (UINT iSpace = 0; iSpace < cSpaces; iSpace++)
  354. if (m_papTransformDescriptors[iSpace])
  355. DeactivateDataSpace(iSpace);
  356. delete [] m_papTransformDescriptors;
  357. }
  358. if (m_pwscDataSpaceNames)
  359. delete [] m_pwscDataSpaceNames;
  360. if (m_pStrmSpaceNames)
  361. m_pStrmSpaceNames->Release();
  362. if (m_fInitialed)
  363. {
  364. RonM_ASSERT(m_pLKBMedium);
  365. RonM_ASSERT(m_pPathManager);
  366. RonM_ASSERT(m_pFreeListManager);
  367. RonM_ASSERT(m_pTransformServices);
  368. #ifdef _DEBUG
  369. hr =
  370. #endif // _DEBUG
  371. m_pPathManager->FlushToLockBytes();
  372. RonM_ASSERT(SUCCEEDED(hr));
  373. #ifdef _DEBUG
  374. hr =
  375. #endif // _DEBUG
  376. FlushToLockBytes();
  377. RonM_ASSERT(SUCCEEDED(hr));
  378. }
  379. if (m_pTransformServices)
  380. m_pTransformServices->Release();
  381. if (m_pFreeListManager)
  382. {
  383. #ifdef _DEBUG
  384. c =
  385. #endif // _DEBUG
  386. m_pFreeListManager->Release();
  387. RonM_ASSERT(!c);
  388. }
  389. if (m_pPathManager)
  390. {
  391. #ifdef _DEBUG
  392. c =
  393. #endif // _DEBUG
  394. m_pPathManager->Release();
  395. RonM_ASSERT(!c);
  396. }
  397. if (m_pSysPathManager)
  398. {
  399. #ifdef _DEBUG
  400. c =
  401. #endif // _DEBUG
  402. m_pSysPathManager->Release();
  403. RonM_ASSERT(!c);
  404. }
  405. if (m_pLKBMedium)
  406. m_pLKBMedium->Release();
  407. }
  408. IStorageITEx *CITFileSystem::CImpITFileSystem::FindActiveStorage (const WCHAR *pwcsPath)
  409. {
  410. for (CImpITUnknown *pStg = m_pActiveStorageList;
  411. pStg;
  412. pStg = pStg->NextObject()
  413. )
  414. if (((IIT_IStorageITEx *)pStg)->IsNamed(pwcsPath))
  415. {
  416. pStg->AddRef();
  417. return (IStorageITEx *) pStg;
  418. }
  419. return NULL;
  420. }
  421. ILockBytes *CITFileSystem::CImpITFileSystem::FindActiveLockBytes(const WCHAR *pwcsPath)
  422. {
  423. return ::FindMatchingLockBytes(pwcsPath, (CImpITUnknown *) m_pActiveLockBytesList);
  424. }
  425. // Initialers:
  426. HRESULT __stdcall CITFileSystem::CImpITFileSystem::InitCreateOnLockBytes
  427. (ILockBytes * plkbyt, DWORD grfMode, PITS_Control_Data pControlData, LCID lcid)
  428. {
  429. // First we bind to the lockbyte object passed to us.
  430. // If this call fails, the containing create function will delete this
  431. // object instance, and that will cause the lockbyte object to be released.
  432. if (!plkbyt)
  433. return STG_E_INVALIDPOINTER;
  434. ITSFS_Control_Data *pITCD = (ITSFS_Control_Data *) pControlData;
  435. if (!pITCD)
  436. {
  437. // We use _alloca here because it allocates memory on the stack,
  438. // and those allocations automatically get cleaned up when we
  439. // exit this function. So no explicit deallocation is necessary.
  440. // That's important given that sometimes we have control data
  441. // passed in to us, and sometimes we create it dynamically.
  442. pITCD = PITSFS_Control_Data(_alloca(sizeof(ITSFS_Control_Data)));
  443. if (!pITCD)
  444. return STG_E_INSUFFICIENTMEMORY;
  445. pITCD->cdwFollowing = 6;
  446. pITCD->cdwITFS_Control = 5;
  447. pITCD->dwMagic = MAGIC_ITSFS_CONTROL;
  448. pITCD->dwVersion = ITSFS_CONTROL_VERSION;
  449. pITCD->cbDirectoryBlock = DEFAULT_DIR_BLOCK_SIZE;
  450. pITCD->cMinCacheEntries = DEFAULT_MIN_CACHE_ENTRIES;
  451. pITCD->fFlags = fDefaultIsCompression;
  452. }
  453. else
  454. if (pITCD->cdwFollowing < 6 || pITCD->cdwITFS_Control < 5
  455. || pITCD->dwMagic != MAGIC_ITSFS_CONTROL
  456. || pITCD->dwVersion != ITSFS_CONTROL_VERSION
  457. )
  458. return STG_E_INVALIDPARAMETER;
  459. m_pLKBMedium = plkbyt;
  460. m_pLKBMedium->AddRef();
  461. // We pick up the name of the file which contains this file system from
  462. // the LockBytes object.
  463. STATSTG statstg;
  464. ZeroMemory(&statstg, sizeof(statstg));
  465. HRESULT hr = plkbyt->Stat(&statstg, STATFLAG_DEFAULT);
  466. RonM_ASSERT(hr == S_OK);
  467. if (hr != S_OK)
  468. return hr;
  469. // Now we must set initial values for the header structure.
  470. // This structure will be stored at offset zero in m_pLKBMedium.
  471. m_itfsh.uMagic = MAGIC_ITS_FILE;
  472. m_itfsh.cbHeaderSize = sizeof(ITSFileHeader);
  473. m_itfsh.uFormatVersion = CurrentFileFormatVersion;
  474. m_itfsh.fFlags = pITCD->fFlags & fDefaultIsCompression;
  475. m_itfsh.dwStamp = m_StartingFileStamp = statstg.mtime.dwLowDateTime;
  476. m_itfsh.lcid = lcid;
  477. m_itfsh.offFreeListData = 0;
  478. m_itfsh.cbFreeListData = 0;
  479. m_itfsh.offPathMgrData = 0;
  480. m_itfsh.cbPathMgrData = 0;
  481. m_itfsh.offPathMgrOrigin = 0;
  482. m_cwcFileName = wcsLen(statstg.pwcsName);
  483. if (m_cwcFileName >= MAX_PATH) // Note that an empty name is valid.
  484. {
  485. OLEHeap()->Free(statstg.pwcsName);
  486. return STG_E_INVALIDNAME;
  487. }
  488. CopyMemory(m_awszFileName, statstg.pwcsName, (m_cwcFileName + 1) * sizeof(WCHAR));
  489. m_fHeaderIsDirty = TRUE;
  490. OLEHeap()->Free(statstg.pwcsName);
  491. // At this point we haven't created a Path Manager for this file system.
  492. // The Path Manager is the mechanism we used to keep track of the items
  493. // in the file system. However when we create the Path Manager, we must
  494. // record its location in the file system header instead. To keep the
  495. // program logic clean we're going to create a special variation on the
  496. // Path Manager interface called SysPathManager. The SysPathManager
  497. // object uses the file system header to keep track of two objects --
  498. // the Path Manager and the Free List Manager.
  499. hr = CSystemPathManager::Create(NULL, this, &m_pSysPathManager);
  500. RonM_ASSERT(SUCCEEDED(hr));
  501. if (!SUCCEEDED(hr))
  502. return hr;
  503. // SysPathManager works exactly like the PathManager object except
  504. // that it recognizes only the names L"F" and L"P", respectively,
  505. // to denote the Free List object and the Path Database object.
  506. // Now we must create a FreeList manager for this file system.
  507. hr = CFreeList::CreateFreeList(this, m_itfsh.cbHeaderSize, &m_pFreeListManager);
  508. RonM_ASSERT(SUCCEEDED(hr));
  509. if (!SUCCEEDED(hr))
  510. return hr;
  511. // Since we want to support multiple free list managers in the future,
  512. // we record the class id of the current free list manager in the
  513. // header for the file system. Then when we open this file system
  514. // sometime later, we can be assured of connecting it to the correct
  515. // free list manager implementation.
  516. hr = m_pFreeListManager->GetClassID(&m_itfsh.clsidFreeList);
  517. RonM_ASSERT(SUCCEEDED(hr));
  518. if (!SUCCEEDED(hr))
  519. return hr;
  520. ILockBytes *pLKB_PathDataBase = NULL;
  521. hr = OpenLockBytes(NULL, L"P", &pLKB_PathDataBase);
  522. RonM_ASSERT(SUCCEEDED(hr));
  523. if (!SUCCEEDED(hr))
  524. return hr;
  525. // The call to NewPathDatabase will either bind pLKB_PathDataBase to the new
  526. // Path Manager, or Release it if it fails.
  527. hr = CPathManager1::NewPathDatabase(NULL, pLKB_PathDataBase,
  528. pITCD->cbDirectoryBlock,
  529. pITCD->cMinCacheEntries,
  530. &m_pPathManager
  531. );
  532. RonM_ASSERT(SUCCEEDED(hr));
  533. if (!SUCCEEDED(hr))
  534. return hr;
  535. hr = m_pPathManager->GetClassID(&m_itfsh.clsidPathMgr);
  536. if (!SUCCEEDED(hr))
  537. return hr;
  538. m_fInitialed = TRUE;
  539. PathInfo PI;
  540. ZeroMemory(&PI, sizeof(PI));
  541. wcsCpy(PI.awszStreamPath, L"/");
  542. PI.cwcStreamPath = 1;
  543. PI.clsidStorage = CLSID_NULL;
  544. MarkActive(g_pImpITFileSystemList);
  545. hr = m_pPathManager->CreateEntry(&PI, NULL, FALSE);
  546. RonM_ASSERT(SUCCEEDED(hr));
  547. if (!SUCCEEDED(hr))
  548. return hr;
  549. wcsCpy(PI.awszStreamPath, pwcsSpaceNameStorage);
  550. wcsCat(PI.awszStreamPath, pwcsLZXSpace);
  551. wcsCat(PI.awszStreamPath, pwcsSpaceContentSuffix);
  552. ILockBytes *pLKBDefault = NULL;
  553. hr = CreateLockBytes(NULL, (const WCHAR *)PI.awszStreamPath, pwcsUncompressedSpace,
  554. FALSE, &pLKBDefault
  555. );
  556. if (!SUCCEEDED(hr))
  557. return hr;
  558. pLKBDefault->Release(); pLKBDefault = NULL;
  559. hr = CreateSpaceNameList();
  560. if (!SUCCEEDED(hr))
  561. return hr;
  562. hr = CreateDefaultDataSpace(pITCD);
  563. if (!SUCCEEDED(hr))
  564. return hr;
  565. hr = CTransformServices::Create(NULL, this, &m_pTransformServices);
  566. if (hr == S_OK)
  567. {
  568. Release(); // To compensate for the circular ref from FreeList Stream
  569. Release(); // To compensate for the circular ref from m_pTransformServices
  570. m_cFSObjectRefs += 2;
  571. // Note: The destructor for the file system will do m_cFSObjectRefs AddRef
  572. // calls to compensate for these Release calls. We do the release calls
  573. // here so that the file system won't be kept alive forever by its
  574. // circular references.
  575. }
  576. hr = ActivateDataSpace(1); // To flush out any errors in the control data
  577. // we've created for the default LZX data space.
  578. Container()->MoveInFrontOf(NULL);
  579. // BugBug: This put the current object list in clean order
  580. // for deallocation at process detach time. However
  581. // we'll need further adjustments as we add more
  582. // data spaces.
  583. return hr;
  584. }
  585. HRESULT CITFileSystem::CImpITFileSystem::CreateDefaultDataSpace(PITSFS_Control_Data pITCD)
  586. {
  587. XformControlData *pXFCD = NULL;
  588. if (pITCD->cdwFollowing > pITCD->cdwITFS_Control + 1)
  589. {
  590. // We have some trailing data. Let's assume that it's
  591. // control data for the LZX transform.
  592. pXFCD = PXformControlData(pITCD + 1);
  593. }
  594. else
  595. {
  596. // We don't have any LZX control data in *pITCD. So we
  597. // must get default control data from the transform
  598. // factory.
  599. XformControlData *pCD = NULL;
  600. ITransformFactory *pXFF = NULL;
  601. HRESULT hr = CLZX_TransformFactory::Create(NULL, IID_ITransformFactory, (void **)&pXFF);
  602. if (!SUCCEEDED(hr))
  603. return hr;
  604. hr = pXFF->DefaultControlData(&pCD);
  605. pXFF->Release();
  606. if (!SUCCEEDED(hr))
  607. return hr;
  608. // Now that we've got the default control data, we must
  609. // copy into a local stack buffer. The use of _alloca means
  610. // that we do not have to explicitly deallocate pXFCD. That's
  611. // important given that sometimes the control data will be
  612. // passed in as part of *pITCD and sometimes it will be
  613. // created dynamically.
  614. UINT cbData = sizeof(DWORD) * (1 + pCD->cdwControlData);
  615. pXFCD = PXformControlData(_alloca(cbData));
  616. if (!pXFCD)
  617. {
  618. OLEHeap()->Free(pCD);
  619. return STG_E_INSUFFICIENTMEMORY;
  620. }
  621. CopyMemory(pXFCD, pCD, cbData);
  622. OLEHeap()->Free(pCD);
  623. }
  624. WCHAR awcsPath[MAX_PATH];
  625. wcsCpy(awcsPath, pwcsSpaceNameStorage);
  626. wcsCat(awcsPath, pwcsLZXSpace);
  627. UINT cbPrefix = wcsLen(awcsPath);
  628. wcsCat(awcsPath, pwcsTransformListSuffix);
  629. IStream *pStrm = NULL;
  630. ULONG cbWritten = 0;
  631. WCHAR awcsClassID[CWC_GUID_STRING_BUFFER];
  632. UINT cbResult = StringFromGUID2(CLSID_LZX_Transform, awcsClassID, CWC_GUID_STRING_BUFFER);
  633. if (cbResult == 0)
  634. return STG_E_UNKNOWN;
  635. HRESULT hr = WriteToStream((const WCHAR *) awcsPath, awcsClassID, wcsLen(awcsClassID));
  636. if (hr == S_OK)
  637. {
  638. awcsPath[cbPrefix] = 0;
  639. wcsCat(awcsPath, pwcsSpanInfoSuffix);
  640. ULARGE_INTEGER uli;
  641. uli.LowPart = 0;
  642. uli.HighPart = 0;
  643. hr = WriteToStream((const WCHAR *) awcsPath, &uli, sizeof(uli));
  644. }
  645. if (hr == S_OK)
  646. {
  647. awcsPath[cbPrefix] = 0;
  648. wcsCat(awcsPath, pwcsControlDataSuffix);
  649. hr = WriteToStream((const WCHAR *) awcsPath, pXFCD,
  650. sizeof(XformControlData) + sizeof(DWORD) * pXFCD->cdwControlData
  651. );
  652. }
  653. return hr;
  654. }
  655. HRESULT CITFileSystem::CImpITFileSystem::WriteToStream
  656. (const WCHAR *pwcsStreamPath, PVOID pvData, ULONG cbData)
  657. {
  658. IStreamITEx *pStrm = NULL;
  659. HRESULT hr = CreateStream(NULL, pwcsStreamPath, STGM_READWRITE, &pStrm);
  660. if (hr != S_OK)
  661. return hr;
  662. ULONG cbWritten = 0;
  663. hr = pStrm->Write(pvData, cbData, &cbWritten);
  664. pStrm->Release(); pStrm = FALSE;
  665. if (hr == S_FALSE || cbWritten != cbData)
  666. hr = STG_E_WRITEFAULT;
  667. return hr;
  668. }
  669. HRESULT CITFileSystem::CImpITFileSystem::CreateSpaceNameList()
  670. {
  671. USHORT cwcUncompressedSpace = (USHORT) wcsLen(pwcsUncompressedSpace);
  672. USHORT cwcDefaultSpace = (USHORT) wcsLen(pwcsLZXSpace);
  673. RonM_ASSERT(sizeof(USHORT) == 2 && sizeof(WCHAR) == 2);
  674. USHORT cwcNameListSize = cwcUncompressedSpace + cwcDefaultSpace + 6;
  675. m_pwscDataSpaceNames = New WCHAR[cwcNameListSize];
  676. m_papTransformDescriptors = New PTransformDescriptor[2];
  677. if (m_papTransformDescriptors)
  678. {
  679. m_papTransformDescriptors[0] = NULL; // To make the destructor code
  680. m_papTransformDescriptors[1] = NULL; // operate correctly.
  681. }
  682. if (!m_pwscDataSpaceNames || !m_papTransformDescriptors)
  683. return STG_E_INSUFFICIENTMEMORY;
  684. /*
  685. The m_pwscDataSpaceNames array has a prefix consisting of:
  686. USHORT cwcTotal; -- Total length of *m_pwscDataSpaceNames
  687. USHORT cDataSpaces; -- Number of names in *m_pwscDataSpaceNames
  688. Then each name has this format:
  689. USHORT cwcDataSpaceName;
  690. WCHAR awcDataSpaceName[cwcDataSpaceName];
  691. WCHAR wcNULL == NULL;
  692. The position of a name in the sequence determines its ordinal value.
  693. */
  694. m_pwscDataSpaceNames[0] = cwcNameListSize;
  695. m_pwscDataSpaceNames[1] = 2;
  696. m_pwscDataSpaceNames[2] = cwcUncompressedSpace;
  697. m_pwscDataSpaceNames[3 + cwcUncompressedSpace] = 0;
  698. m_pwscDataSpaceNames[4 + cwcUncompressedSpace] = cwcDefaultSpace;
  699. m_pwscDataSpaceNames[5 + cwcUncompressedSpace + cwcDefaultSpace] = 0;
  700. CopyMemory(m_pwscDataSpaceNames + 3, pwcsUncompressedSpace, cwcUncompressedSpace * sizeof(WCHAR));
  701. CopyMemory(m_pwscDataSpaceNames + cwcUncompressedSpace + 5,
  702. pwcsLZXSpace, cwcDefaultSpace * sizeof(WCHAR)
  703. );
  704. HRESULT hr = CreateStream(NULL, pwscSpaceNameListStream, STGM_READWRITE, &m_pStrmSpaceNames);
  705. if (!SUCCEEDED(hr))
  706. return hr;
  707. return FlushSpaceNameList();
  708. }
  709. HRESULT CITFileSystem::CImpITFileSystem::FlushSpaceNameList()
  710. {
  711. ULONG cb = m_pwscDataSpaceNames[0] * sizeof(WCHAR);
  712. ULONG cbWritten = 0;
  713. HRESULT hr = m_pStrmSpaceNames->Write(m_pwscDataSpaceNames, cb, &cbWritten);
  714. if (!SUCCEEDED(hr))
  715. return hr;
  716. if (cbWritten != cb)
  717. return STG_E_WRITEFAULT;
  718. return NO_ERROR;
  719. }
  720. HRESULT CITFileSystem::CImpITFileSystem::OpenSpaceNameList()
  721. {
  722. HRESULT hr = OpenStream(NULL, pwscSpaceNameListStream, STGM_READWRITE, &m_pStrmSpaceNames);
  723. if (!SUCCEEDED(hr))
  724. return hr;
  725. USHORT cwc;
  726. ULONG cbRead;
  727. ULONG cb = sizeof(cwc);
  728. RonM_ASSERT(sizeof(USHORT) == sizeof(WCHAR));
  729. hr= m_pStrmSpaceNames->Read(&cwc, cb, &cbRead);
  730. if (!SUCCEEDED(hr))
  731. return hr;
  732. if (cbRead != cb)
  733. return STG_E_READFAULT;
  734. m_pwscDataSpaceNames = New WCHAR[cwc];
  735. m_pwscDataSpaceNames[0] = cwc;
  736. cb = sizeof(WCHAR) * (cwc - 1);
  737. hr = m_pStrmSpaceNames->Read(m_pwscDataSpaceNames + 1, cb, &cbRead);
  738. if (!SUCCEEDED(hr))
  739. return hr;
  740. if (cbRead != cb)
  741. return STG_E_READFAULT;
  742. m_papTransformDescriptors = New PTransformDescriptor[m_pwscDataSpaceNames[1]];
  743. if (!m_papTransformDescriptors)
  744. return STG_E_INSUFFICIENTMEMORY;
  745. ZeroMemory(m_papTransformDescriptors, sizeof(PTransformDescriptor) * m_pwscDataSpaceNames[1]);
  746. return NO_ERROR;
  747. }
  748. HRESULT CITFileSystem::CImpITFileSystem::FindSpaceName(const WCHAR *pwcsSpaceName)
  749. {
  750. RonM_ASSERT(m_pwscDataSpaceNames);
  751. DEBUGDEF(const WCHAR *pwcLimit = m_pwscDataSpaceNames + m_pwscDataSpaceNames[0])
  752. const WCHAR *pwcNext = m_pwscDataSpaceNames + 2;
  753. USHORT cSpaces = m_pwscDataSpaceNames[1];
  754. USHORT iSpace = 0;
  755. for (; iSpace < cSpaces; iSpace++)
  756. {
  757. RonM_ASSERT(pwcNext < pwcLimit);
  758. USHORT cwc = *pwcNext++;
  759. RonM_ASSERT(pwcNext < pwcLimit);
  760. if (!wcsicmp_0x0409(pwcNext, pwcsSpaceName))
  761. return iSpace;
  762. pwcNext += cwc + 1;
  763. RonM_ASSERT(pwcNext <= pwcLimit);
  764. }
  765. return STG_E_INVALIDNAME;
  766. }
  767. HRESULT CITFileSystem::CImpITFileSystem::AddSpaceName(const WCHAR *pwcsSpaceName)
  768. {
  769. if (!CStorage::ValidStreamName(pwcsSpaceName))
  770. return STG_E_INVALIDNAME;
  771. HRESULT hr = FindSpaceName(pwcsSpaceName);
  772. if (SUCCEEDED(hr))
  773. return STG_E_INVALIDNAME;
  774. UINT cwcNewSpace = wcsLen(pwcsSpaceName);
  775. USHORT cwcOld = m_pwscDataSpaceNames[0];
  776. WCHAR *pwcNext = m_pwscDataSpaceNames + 2;
  777. USHORT cSpaces = m_pwscDataSpaceNames[1];
  778. USHORT iSpace = 0;
  779. if (cSpaces == MAX_SPACES)
  780. return STG_E_INSUFFICIENTMEMORY;
  781. for (; iSpace < cSpaces; iSpace++)
  782. {
  783. USHORT cwc = *pwcNext++;
  784. if (!cwc)
  785. {
  786. WCHAR *pwcsNew = New WCHAR[cwcNewSpace + cwcOld];
  787. if (!pwcsNew)
  788. return STG_E_INSUFFICIENTMEMORY;
  789. pwcNext[-1] = (WCHAR)cwcNewSpace;
  790. UINT offset = UINT(pwcNext - m_pwscDataSpaceNames);
  791. CopyMemory(pwcsNew, m_pwscDataSpaceNames, offset * sizeof(WCHAR));
  792. CopyMemory(pwcsNew + offset, pwcsSpaceName, cwcNewSpace * sizeof(WCHAR));
  793. CopyMemory(pwcsNew + offset + cwcNewSpace, pwcNext,
  794. sizeof(WCHAR) * (cwcOld - offset)
  795. );
  796. delete [] m_pwscDataSpaceNames;
  797. m_pwscDataSpaceNames = pwcsNew;
  798. HRESULT hr = FlushSpaceNameList();
  799. if (!SUCCEEDED(hr))
  800. return hr; // BugBug: Should also delete the space name here
  801. RonM_ASSERT(m_papTransformDescriptors[iSpace] == NULL);
  802. return iSpace;
  803. }
  804. pwcNext += cwc + 1;
  805. }
  806. WCHAR *pwcsNew = New WCHAR[2 + cwcNewSpace + cwcOld];
  807. if (!pwcsNew)
  808. return STG_E_INSUFFICIENTMEMORY;
  809. TransformDescriptor **ppTXDNew = New PTransformDescriptor[cSpaces + 1];
  810. if (!ppTXDNew)
  811. {
  812. delete [] pwcsNew;
  813. return STG_E_INSUFFICIENTMEMORY;
  814. }
  815. CopyMemory(ppTXDNew, m_papTransformDescriptors, cSpaces * sizeof(PTransformDescriptor));
  816. m_papTransformDescriptors[cSpaces] = NULL;
  817. CopyMemory(pwcsNew, m_pwscDataSpaceNames, cwcOld * sizeof(WCHAR));
  818. CopyMemory(pwcsNew + cwcOld + 1, pwcsSpaceName, cwcNewSpace * sizeof(WCHAR));
  819. pwcsNew[cwcOld ] = (WCHAR)cwcNewSpace;
  820. pwcsNew[cwcOld + cwcNewSpace + 1] = 0;
  821. pwcsNew[1 ] = cSpaces + 1;
  822. delete m_papTransformDescriptors;
  823. m_papTransformDescriptors = ppTXDNew;
  824. delete [] m_pwscDataSpaceNames;
  825. m_pwscDataSpaceNames = pwcsNew;
  826. hr = FlushSpaceNameList();
  827. if (!SUCCEEDED(hr))
  828. return hr; // BugBug: Should also delete the space name here
  829. return cSpaces;
  830. }
  831. HRESULT CITFileSystem::CImpITFileSystem::DeleteSpaceName(const WCHAR *pwcsSpaceName)
  832. {
  833. HRESULT hr = FindSpaceName(pwcsSpaceName);
  834. if (!SUCCEEDED(hr))
  835. return hr;
  836. ULONG iSpace = hr;
  837. if (iSpace < 2)
  838. return STG_E_INVALIDNAME;
  839. RonM_ASSERT(hr < m_pwscDataSpaceNames[1]);
  840. TransformDescriptor *pTD = m_papTransformDescriptors[iSpace];
  841. RonM_ASSERT(!pTD || (pTD->pLockBytesChain == NULL && pTD->apTransformInstance[0] == NULL));
  842. if (pTD)
  843. delete [] PBYTE(pTD);
  844. pTD = NULL;
  845. m_papTransformDescriptors[iSpace] = NULL;
  846. USHORT cwcName = (USHORT) wcsLen(pwcsSpaceName);
  847. WCHAR *pwcLimit = m_pwscDataSpaceNames + m_pwscDataSpaceNames[0];
  848. WCHAR *pwcNext = m_pwscDataSpaceNames + 2;
  849. for (;hr--;)
  850. {
  851. USHORT cwc = *pwcNext++;
  852. pwcNext += cwc + 1;
  853. }
  854. *pwcNext++ = 0;
  855. WCHAR *pwcTrailing = pwcNext + cwcName;
  856. CopyMemory(pwcNext, pwcNext + cwcName, DWORD((pwcLimit - pwcTrailing) * sizeof(WCHAR)));
  857. return FlushSpaceNameList();
  858. }
  859. HRESULT __stdcall CITFileSystem::CImpITFileSystem::InitOpenOnLockBytes
  860. (ILockBytes * plkbyt, DWORD grfMode)
  861. {
  862. m_pLKBMedium = plkbyt;
  863. plkbyt->AddRef();
  864. // We pick up the name of the file which contains this file system from
  865. // the LockBytes object.
  866. STATSTG statstg;
  867. ZeroMemory(&statstg, sizeof(statstg));
  868. HRESULT hr = plkbyt->Stat(&statstg, STATFLAG_DEFAULT);
  869. RonM_ASSERT(hr == S_OK);
  870. if (hr != S_OK) return hr;
  871. m_cwcFileName = wcsLen(statstg.pwcsName);
  872. if (m_cwcFileName >= MAX_PATH) // Note that an empty name is valid.
  873. {
  874. OLEHeap()->Free(statstg.pwcsName);
  875. return STG_E_INVALIDNAME;
  876. }
  877. CopyMemory(m_awszFileName, statstg.pwcsName, (m_cwcFileName + 1) * sizeof(WCHAR));
  878. OLEHeap()->Free(statstg.pwcsName);
  879. m_fReadOnly = (statstg.grfMode & RW_ACCESS_MASK) == STGM_READ;
  880. ULONG cbRead = 0;
  881. hr = m_pLKBMedium->ReadAt(CULINT(0).Uli(), &m_itfsh, sizeof(m_itfsh), &cbRead);
  882. if (!SUCCEEDED(hr))
  883. return hr;
  884. if ( cbRead < sizeof(ITSFileHeaderV2)
  885. || m_itfsh.uMagic != MAGIC_ITS_FILE
  886. || m_itfsh.uFormatVersion < FirstReleasedVersion
  887. || m_itfsh.uFormatVersion > CurrentFileFormatVersion
  888. ||(m_itfsh.fFlags & ~VALID_OPEN_FLAGS)
  889. )
  890. return STG_E_INVALIDHEADER;
  891. else
  892. if ( m_itfsh.uFormatVersion == CurrentFileFormatVersion
  893. && cbRead == sizeof(ITSFileHeader)
  894. )
  895. {
  896. }
  897. else
  898. if (m_itfsh.uFormatVersion == FirstReleasedVersion)
  899. {
  900. m_itfsh.offPathMgrOrigin = 0;
  901. }
  902. else
  903. {
  904. RonM_ASSERT(FALSE); // If this assert fires, we have a old version
  905. // that isn't being handled.
  906. return STG_E_INVALIDHEADER;
  907. }
  908. m_StartingFileStamp = m_itfsh.dwStamp;
  909. hr = CSystemPathManager::Create(NULL, this, &m_pSysPathManager);
  910. RonM_ASSERT(SUCCEEDED(hr));
  911. if (!SUCCEEDED(hr))
  912. return hr;
  913. RonM_ASSERT(m_itfsh.clsidFreeList == CLSID_IFreeListManager_1);
  914. hr = CFreeList::AttachFreeList(this, &m_pFreeListManager);
  915. RonM_ASSERT(SUCCEEDED(hr));
  916. if (!SUCCEEDED(hr))
  917. return hr;
  918. ILockBytes *pLKB_PathDatabase = NULL;
  919. hr = OpenLockBytes(NULL, L"P", &pLKB_PathDatabase);
  920. RonM_ASSERT(SUCCEEDED(hr));
  921. if (!SUCCEEDED(hr))
  922. return hr;
  923. hr = CPathManager1::LoadPathDatabase(NULL, pLKB_PathDatabase, &m_pPathManager);
  924. RonM_ASSERT(SUCCEEDED(hr));
  925. if (hr == S_OK)
  926. {
  927. m_fInitialed = TRUE;
  928. MarkActive(g_pImpITFileSystemList);
  929. }
  930. hr = OpenSpaceNameList();
  931. if (!SUCCEEDED(hr))
  932. return hr;
  933. hr = CTransformServices::Create(NULL, this, &m_pTransformServices);
  934. if (hr == S_OK)
  935. {
  936. Release(); // To compensate for the circular ref from FreeList Stream
  937. Release(); // To compensate for the circular ref from m_pTransformServices
  938. m_cFSObjectRefs += 2;
  939. // Note: The destructor for the file system will do m_cFSObjectRefs AddRef
  940. // calls to compensate for these Release calls. We do the release calls
  941. // here so that the file system won't be kept alive forever by its
  942. // circular references.
  943. }
  944. Container()->MoveInFrontOf(((IITTransformServices *) m_pTransformServices)->Container());
  945. return hr;
  946. }
  947. HRESULT __stdcall CITFileSystem::CImpITFileSystem::SetITFSTimes
  948. (FILETIME const * pctime,
  949. FILETIME const * patime,
  950. FILETIME const * pmtime
  951. )
  952. {
  953. return ((CFSLockBytes::CImpILockBytes *) m_pLKBMedium)
  954. ->SetTimes(pctime, patime, pmtime);
  955. }
  956. // IUnknown methods:
  957. STDMETHODIMP_(ULONG) CITFileSystem::CImpITFileSystem::Release(void)
  958. {
  959. // The actual work for the Release function is done by
  960. // CImpITUnknown::Release() and ~CImpITFileSystem.
  961. //
  962. // We bracket that work as a critical section active file systems
  963. // are kept in a linked list. A release operation may remove
  964. // this file system from that list, and we need to guard against
  965. // having someone find a reference to this storage just before
  966. // we destroy it.
  967. CSyncWith sw(g_csITFS);
  968. ULONG ulCnt = CImpITUnknown::Release();
  969. return ulCnt;
  970. }
  971. // IITFileSystem methods:
  972. CITCriticalSection& CITFileSystem::CImpITFileSystem::CriticalSection()
  973. {
  974. return m_cs;
  975. }
  976. HRESULT __stdcall CITFileSystem::CImpITFileSystem::FlushToLockBytes()
  977. {
  978. // This routine copies the free list and the file header to the
  979. // lockbyte medium. It does not flush the path database. We expect
  980. // most calls to this interface will come from the Path Manager.
  981. RonM_ASSERT(m_fInitialed);
  982. RonM_ASSERT(m_pStrmSpaceNames);
  983. if (m_pFreeListManager->IsDirty() == S_OK)
  984. {
  985. HRESULT hr = m_pFreeListManager->RecordFreeList();
  986. if (!SUCCEEDED(hr)) return hr;
  987. }
  988. if (m_fHeaderIsDirty || m_itfsh.dwStamp != m_StartingFileStamp)
  989. {
  990. ULONG cbWritten = 0;
  991. HRESULT hr = S_FALSE;
  992. switch (m_itfsh.uFormatVersion)
  993. {
  994. case RelativeOffsetVersion:
  995. hr = m_pLKBMedium->WriteAt(CULINT(0).Uli(), &m_itfsh, sizeof(m_itfsh), &cbWritten);
  996. if (hr == S_OK && cbWritten != sizeof(m_itfsh))
  997. hr = STG_E_WRITEFAULT;
  998. break;
  999. case FirstReleasedVersion:
  1000. RonM_ASSERT(m_itfsh.offPathMgrOrigin == 0);
  1001. hr = m_pLKBMedium->WriteAt(CULINT(0).Uli(), &m_itfsh, sizeof(ITSFileHeaderV2), &cbWritten);
  1002. if (hr == S_OK && cbWritten != sizeof(ITSFileHeaderV2))
  1003. hr = STG_E_WRITEFAULT;
  1004. break;
  1005. default:
  1006. RonM_ASSERT(FALSE); // We have an old format version number that
  1007. // isn't being handled!
  1008. }
  1009. if (!SUCCEEDED(hr)) return hr;
  1010. m_fHeaderIsDirty = FALSE;
  1011. }
  1012. return NO_ERROR;
  1013. }
  1014. void CITFileSystem::CImpITFileSystem::CopyPath(PathInfo &PI, const WCHAR *pwcsPath)
  1015. {
  1016. PI.cwcStreamPath = wcsLen(pwcsPath);
  1017. RonM_ASSERT(PI.cwcStreamPath < MAX_PATH);
  1018. CopyMemory(PI.awszStreamPath, pwcsPath, sizeof(WCHAR) * (1 + PI.cwcStreamPath));
  1019. }
  1020. HRESULT __stdcall CITFileSystem::CImpITFileSystem::CreateStorage
  1021. (IUnknown *pUnkOuter, const WCHAR *pwcsPathPrefix,
  1022. DWORD grfMode, IStorageITEx **ppStg
  1023. )
  1024. {
  1025. CSyncWith sw(g_csITFS);
  1026. PathInfo PI, PIPrev;
  1027. CopyPath(PI, pwcsPathPrefix);
  1028. PI.uStateBits = 0;
  1029. PI.iLockedBytesSegment = 0;
  1030. PI.clsidStorage = CLSID_NULL;
  1031. PI.cUnrecordedChanges = 0;
  1032. RonM_ASSERT(PI.awszStreamPath[PI.cwcStreamPath - 1] == L'/');
  1033. HRESULT hr = m_pPathManager->CreateEntry(&PI, &PIPrev, FALSE);
  1034. if (!SUCCEEDED(hr))
  1035. return hr;
  1036. hr = CStorage::OpenStorage(pUnkOuter, this, &PI, grfMode, ppStg);
  1037. if (!SUCCEEDED(hr))
  1038. m_pPathManager->DeleteEntry(&PI);
  1039. else
  1040. if (pwcsPathPrefix[0] != L'/')
  1041. {
  1042. ((IIT_IStorageITEx *) *ppStg)->Container()->MarkSecondary();
  1043. if (m_cFSObjectRefs!= UINT(~0))
  1044. {
  1045. m_cFSObjectRefs++;
  1046. this->Release(); // To account for circular refs through ":" storages.
  1047. }
  1048. }
  1049. return hr;
  1050. }
  1051. HRESULT __stdcall CITFileSystem::CImpITFileSystem::OpenStorage
  1052. (IUnknown *pUnkOuter, const WCHAR *pwcsPathPrefix,
  1053. DWORD grfMode, IStorageITEx **ppstg
  1054. )
  1055. {
  1056. CSyncWith sw(g_csITFS);
  1057. IStorageITEx *pStorage = FindActiveStorage(pwcsPathPrefix);
  1058. if (pStorage)
  1059. {
  1060. *ppstg = pStorage;
  1061. return NO_ERROR;
  1062. }
  1063. PathInfo PI, PIPrev;
  1064. CopyPath(PI, pwcsPathPrefix);
  1065. RonM_ASSERT(PI.awszStreamPath[PI.cwcStreamPath - 1] == L'/');
  1066. HRESULT hr = m_pPathManager->FindEntry(&PI);
  1067. if (hr == S_OK)
  1068. hr = CStorage::OpenStorage(pUnkOuter, this, &PI, grfMode, ppstg);
  1069. else
  1070. {
  1071. // Some storages don't have explicit entries in the path database.
  1072. // So we'll look to see if anything has a prefix which matches
  1073. // pwcsPathPrefix.
  1074. HRESULT hr2 = NO_ERROR;
  1075. BOOL fStorage = FALSE;
  1076. IEnumSTATSTG *pEnum = NULL;
  1077. hr2 = m_pPathManager->EnumFromObject(NULL, pwcsPathPrefix, wcsLen(pwcsPathPrefix),
  1078. IID_IEnumSTATSTG, (PVOID *) &pEnum
  1079. );
  1080. if (hr2 == S_OK)
  1081. {
  1082. STATSTG statstg;
  1083. hr2 = pEnum->Next(1, &statstg, NULL);
  1084. if (hr2 == S_OK)
  1085. {
  1086. const WCHAR *pwcPrefix = pwcsPathPrefix;
  1087. const WCHAR *pwcEnum = statstg.pwcsName;
  1088. for (; ; )
  1089. {
  1090. WCHAR wcPrefix = WC_To_0x0409_Lower(*pwcPrefix++);
  1091. WCHAR wcEnum = WC_To_0x0409_Lower(*pwcEnum++);
  1092. if (wcPrefix == wcEnum && wcPrefix)
  1093. continue;
  1094. fStorage = wcPrefix == 0;
  1095. RonM_ASSERT(wcEnum || wcPrefix);
  1096. break;
  1097. }
  1098. OLEHeap()->Free(statstg.pwcsName);
  1099. }
  1100. pEnum->Release();
  1101. }
  1102. if (fStorage)
  1103. {
  1104. PI.clsidStorage = CLSID_NULL;
  1105. PI.uStateBits = 0;
  1106. // BugBug: We don't yet set iLockedBytesSegment for this storage.
  1107. // The problem is finding a prefix storage which has an
  1108. // entry in the path database.
  1109. hr = CStorage::OpenStorage(pUnkOuter, this, &PI, grfMode, ppstg);;
  1110. }
  1111. else hr = STG_E_FILENOTFOUND;
  1112. }
  1113. if (hr == S_OK && pwcsPathPrefix[0] != L'/')
  1114. {
  1115. ((IIT_IStorageITEx *) *ppstg)->Container()->MarkSecondary();
  1116. if (m_cFSObjectRefs!= UINT(~0))
  1117. {
  1118. m_cFSObjectRefs++;
  1119. this->Release(); // To account for circular references through ":" storages.
  1120. }
  1121. }
  1122. return hr;
  1123. }
  1124. HRESULT CITFileSystem::CImpITFileSystem::CreateTransformedLockBytes
  1125. (IUnknown *pUnkOuter, const WCHAR *pwcsPath,
  1126. const WCHAR *pwcsDataSpaceName,
  1127. BOOL fOverwrite, ILockBytes **ppLKB
  1128. )
  1129. {
  1130. RonM_ASSERT(pwcsPath[0] != L':');
  1131. HRESULT hr = FindSpaceName(pwcsDataSpaceName);
  1132. if (!SUCCEEDED(hr))
  1133. return hr;
  1134. ULONG iSpace = hr;
  1135. BOOL fNewActivation = (m_papTransformDescriptors[iSpace] == NULL);
  1136. if (fNewActivation)
  1137. {
  1138. hr = ActivateDataSpace(iSpace);
  1139. if (!SUCCEEDED(hr))
  1140. return hr;
  1141. }
  1142. TransformDescriptor *pTD = m_papTransformDescriptors[iSpace];
  1143. RonM_ASSERT(pTD);
  1144. ILockBytes *pLKB = CTransformedLockBytes::FindTransformedLockBytes(pwcsPath, pTD);
  1145. if (pLKB)
  1146. {
  1147. RonM_ASSERT(!fNewActivation);
  1148. pLKB->Release();
  1149. return STG_E_INUSE;
  1150. }
  1151. PathInfo PI, PIPrev;
  1152. CopyPath(PI, pwcsPath);
  1153. PI.ullcbOffset = 0;
  1154. PI.ullcbData = 0;
  1155. PI.uStateBits = 0;
  1156. PI.iLockedBytesSegment = iSpace;
  1157. PI.cUnrecordedChanges = 0;
  1158. RonM_ASSERT(PI.awszStreamPath[PI.cwcStreamPath - 1] != L'/');
  1159. PIPrev.cwcStreamPath = 0; // Setup to detect overwrite condition.
  1160. hr = m_pPathManager->CreateEntry(&PI, &PIPrev, fOverwrite);
  1161. if (SUCCEEDED(hr))
  1162. {
  1163. #if 0 // BugBug: Need to add a method handle this for transforms
  1164. if (PIPrev.cwcStreamPath) // Did we overwrite an existing file?
  1165. m_pFreeListManager->PutFreeSpace(PIPrev.ullcbOffset, PIPrev.ullcbData);
  1166. #endif // 0
  1167. hr = CTransformedLockBytes::Open(NULL, &PI, pTD, this, ppLKB);
  1168. }
  1169. return hr;
  1170. }
  1171. HRESULT CITFileSystem::CImpITFileSystem::ActivateDataSpace(ULONG iSpace)
  1172. {
  1173. RonM_ASSERT(iSpace == 1); // Since we have only the default space implemented
  1174. // BugBug: The code below only supports the default data space.
  1175. RonM_ASSERT(iSpace < m_pwscDataSpaceNames[1]);
  1176. RonM_ASSERT(m_papTransformDescriptors[iSpace] == NULL);
  1177. WCHAR awszPath[MAX_PATH];
  1178. wcsCpy(awszPath, pwcsSpaceNameStorage);
  1179. wcsCat(awszPath, pwcsLZXSpace);
  1180. UINT cwcPrefix = wcsLen(awszPath);
  1181. wcsCat(awszPath, pwcsSpanInfoSuffix);
  1182. IStreamITEx *pStrm = NULL;
  1183. HRESULT hr = OpenStream(NULL, (const WCHAR *) awszPath, STGM_READ, &pStrm);
  1184. if (!SUCCEEDED(hr))
  1185. return hr;
  1186. ULARGE_INTEGER ulicbSpan;
  1187. ULONG cbRead = 0;
  1188. hr = pStrm->Read(&ulicbSpan, sizeof(ulicbSpan), &cbRead);
  1189. pStrm->Release(); pStrm = NULL;
  1190. if (hr == S_FALSE || cbRead != sizeof(ulicbSpan))
  1191. hr = STG_E_READFAULT;
  1192. if (!SUCCEEDED(hr))
  1193. return hr;
  1194. awszPath[cwcPrefix] = 0;
  1195. wcsCat(awszPath, pwcsControlDataSuffix);
  1196. hr = OpenStream(NULL, (const WCHAR *) awszPath, STGM_READ, &pStrm);
  1197. if (!SUCCEEDED(hr))
  1198. return hr;
  1199. UINT cdwData = 0;
  1200. hr = pStrm->Read(&cdwData, sizeof(cdwData), &cbRead);
  1201. if (hr == S_FALSE || cbRead != sizeof(cdwData))
  1202. hr = STG_E_READFAULT;
  1203. if (!SUCCEEDED(hr))
  1204. return hr;
  1205. PXformControlData pXFCD = PXformControlData(_alloca(sizeof(DWORD) * (cdwData + 1)));
  1206. if (!pXFCD)
  1207. {
  1208. pStrm->Release();
  1209. return STG_E_INSUFFICIENTMEMORY;
  1210. }
  1211. pXFCD->cdwControlData = cdwData;
  1212. hr = pStrm->Read(&(pXFCD->adwControlData), cdwData * sizeof(DWORD), &cbRead);
  1213. pStrm->Release(); pStrm = NULL;
  1214. if (hr == S_FALSE || cbRead != cdwData * sizeof(DWORD))
  1215. hr = STG_E_READFAULT;
  1216. if (!SUCCEEDED(hr))
  1217. return hr;
  1218. awszPath[cwcPrefix] = 0;
  1219. wcsCat(awszPath, pwcsSpaceContentSuffix);
  1220. ILockBytes *pLKB = NULL;
  1221. hr = OpenLockBytes(NULL, (const WCHAR *) awszPath, &pLKB);
  1222. if (!SUCCEEDED(hr))
  1223. return hr;
  1224. ITransformInstance *pITxInst;
  1225. if (!SUCCEEDED(hr = CNull_TransformInstance::CreateFromILockBytes
  1226. (NULL, pLKB, &pITxInst)))
  1227. return hr;
  1228. ((IITTransformInstance *) pITxInst)->Container()->MarkSecondary();
  1229. ITransformFactory *pITxFactory;
  1230. hr = CLZX_TransformFactory::Create(NULL, IID_ITransformFactory, (void **)&pITxFactory);
  1231. if (SUCCEEDED(hr))
  1232. {
  1233. m_pTransformServices->AddRef(); // Because we're passing the Transform services
  1234. // object to the transform factory.
  1235. UINT cTransforms = 1; // Really should get this from transform list.
  1236. TransformDescriptor *pTD = TransformDescriptor::Create(iSpace, cTransforms);
  1237. if (pTD)
  1238. {
  1239. CLSID clsid = CLSID_LZX_Transform;
  1240. hr = pITxFactory->CreateTransformInstance
  1241. (pITxInst, // Container data span for transformed data
  1242. ulicbSpan, // Untransformed size of data
  1243. pXFCD, // Control data for this instance
  1244. &clsid, // Transform Class ID
  1245. pwcsLZXSpace, // Data space name for this instance
  1246. m_pTransformServices, // Utility routines
  1247. NULL, // Interface to get enciphering keys
  1248. &pTD->apTransformInstance[0] // Out: Instance transform interface
  1249. );
  1250. pITxFactory->Release();
  1251. if (SUCCEEDED(hr))
  1252. m_papTransformDescriptors[iSpace] = pTD;
  1253. else delete pTD;
  1254. }
  1255. else hr = STG_E_INSUFFICIENTMEMORY;
  1256. }
  1257. else pITxInst->Release();
  1258. return hr;
  1259. }
  1260. HRESULT CITFileSystem::CImpITFileSystem::DeactivateDataSpace(ULONG iSpace)
  1261. {
  1262. RonM_ASSERT(iSpace < m_pwscDataSpaceNames[1]);
  1263. RonM_ASSERT(m_papTransformDescriptors[iSpace]);
  1264. RonM_ASSERT(iSpace == 1); // While we have just the "MSCompressed" space.
  1265. TransformDescriptor *pTD = m_papTransformDescriptors[iSpace];
  1266. RonM_ASSERT(pTD->iSpace != 0);
  1267. RonM_ASSERT(pTD->pLockBytesChain == NULL);
  1268. if (IsWriteable() == S_OK)
  1269. {
  1270. UINT cTransforms = pTD->cTransformLayers;
  1271. ULARGE_INTEGER *pacbSpans = (ULARGE_INTEGER *) _alloca(cTransforms * sizeof(ULARGE_INTEGER));
  1272. if (!pacbSpans)
  1273. return STG_E_INSUFFICIENTMEMORY;
  1274. HRESULT hr = NO_ERROR;
  1275. for (UINT i = 0; i < cTransforms; i++)
  1276. {
  1277. hr = pTD->apTransformInstance[i]->SpaceSize(pacbSpans + i);
  1278. if (!SUCCEEDED(hr))
  1279. return hr;
  1280. }
  1281. WCHAR awcsPath[MAX_PATH];
  1282. wcsCpy(awcsPath, pwcsSpaceNameStorage);
  1283. wcsCat(awcsPath, pwcsLZXSpace);
  1284. wcsCat(awcsPath, pwcsSpanInfoSuffix);
  1285. IStreamITEx *pStrm = NULL;
  1286. hr = OpenStream(NULL, awcsPath, STGM_READWRITE, &pStrm);
  1287. if (!SUCCEEDED(hr))
  1288. return hr;
  1289. ULONG cbSpanSizes = cTransforms * sizeof(ULARGE_INTEGER);
  1290. ULONG cbWritten = 0;
  1291. hr = pStrm->Write(pacbSpans, cbSpanSizes, &cbWritten);
  1292. pStrm->Release(); pStrm = NULL;
  1293. if (hr == S_FALSE || cbWritten != cbSpanSizes)
  1294. hr = STG_E_WRITEFAULT;
  1295. if (!SUCCEEDED(hr))
  1296. return hr;
  1297. }
  1298. pTD->apTransformInstance[0]->Release();
  1299. delete pTD;
  1300. m_papTransformDescriptors[iSpace] = NULL;
  1301. return NO_ERROR;
  1302. }
  1303. HRESULT __stdcall CITFileSystem::CImpITFileSystem::CreateLockBytes
  1304. (IUnknown *pUnkOuter, const WCHAR *pwcsPath,
  1305. const WCHAR *pwcsDataSpaceName,
  1306. BOOL fOverwrite, ILockBytes **ppLKB
  1307. )
  1308. {
  1309. CSyncWith sw(m_cs);
  1310. if (wcsicmp_0x0409(pwcsDataSpaceName, pwcsUncompressedSpace))
  1311. {
  1312. RonM_ASSERT(pwcsPath[0] != L':');
  1313. HRESULT hr = CreateTransformedLockBytes(pUnkOuter, pwcsPath, pwcsDataSpaceName,
  1314. fOverwrite, ppLKB
  1315. );
  1316. return hr;
  1317. }
  1318. ILockBytes *pLKB = FindActiveLockBytes(pwcsPath);
  1319. if (pLKB)
  1320. {
  1321. pLKB->Release();
  1322. return STG_E_INUSE;
  1323. }
  1324. PathInfo PI, PIPrev;
  1325. CopyPath(PI, pwcsPath);
  1326. PI.ullcbOffset = 0;
  1327. PI.ullcbData = 0;
  1328. PI.uStateBits = 0;
  1329. PI.iLockedBytesSegment = 0;
  1330. PI.cUnrecordedChanges = 0;
  1331. RonM_ASSERT(PI.awszStreamPath[PI.cwcStreamPath - 1] != L'/');
  1332. PIPrev.cwcStreamPath = 0; // Setup to detect overwrite condition.
  1333. HRESULT hr = m_pPathManager->CreateEntry(&PI, &PIPrev, fOverwrite);
  1334. if (SUCCEEDED(hr))
  1335. {
  1336. if (PIPrev.cwcStreamPath) // Did we overwrite an existing file?
  1337. m_pFreeListManager->PutFreeSpace(PIPrev.ullcbOffset, PIPrev.ullcbData);
  1338. hr = CSegmentLockBytes::OpenSegment(pUnkOuter, this, m_pLKBMedium, &PI, ppLKB);
  1339. }
  1340. if (hr == S_OK && pwcsPath[0] != L'/')
  1341. {
  1342. ((IITLockBytes *) *ppLKB)->Container()->MarkSecondary();
  1343. if (m_cFSObjectRefs!= UINT(~0))
  1344. {
  1345. m_cFSObjectRefs++;
  1346. this->Release(); // To account for circular references through ":" streams
  1347. }
  1348. }
  1349. return hr;
  1350. }
  1351. HRESULT __stdcall CITFileSystem::CImpITFileSystem::OpenLockBytes
  1352. (IUnknown *pUnkOuter, const WCHAR *pwcsPath, ILockBytes **ppLKB)
  1353. {
  1354. CSyncWith sw(m_cs);
  1355. ILockBytes *pLKB = FindActiveLockBytes(pwcsPath);
  1356. if (pLKB)
  1357. {
  1358. *ppLKB = pLKB;
  1359. return NO_ERROR;
  1360. }
  1361. PathInfo PI;
  1362. CopyPath(PI, pwcsPath);
  1363. RonM_ASSERT(PI.awszStreamPath[PI.cwcStreamPath - 1] != L'/');
  1364. IITPathManager *pPathManager = PathMgr(&PI);
  1365. HRESULT hr = pPathManager->FindEntry(&PI);
  1366. if (hr == S_OK)
  1367. {
  1368. RonM_ASSERT(pwcsPath[0] != L':' || PI.iLockedBytesSegment == 0);
  1369. if (PI.iLockedBytesSegment != 0)
  1370. hr = OpenTransformedLockbytes(&PI, ppLKB);
  1371. else
  1372. {
  1373. if (PI.ullcbData.NonZero() && (pPathManager == m_pPathManager))
  1374. PI.ullcbOffset += m_itfsh.offPathMgrOrigin;
  1375. hr = CSegmentLockBytes::OpenSegment(pUnkOuter, this, m_pLKBMedium, &PI, ppLKB);
  1376. }
  1377. if (hr == S_OK && pwcsPath[0] != L'/')
  1378. {
  1379. ((IITLockBytes *) *ppLKB)->Container()->MarkSecondary();
  1380. if (m_cFSObjectRefs!= UINT(~0))
  1381. {
  1382. m_cFSObjectRefs++;
  1383. this->Release(); // To account for circular reference
  1384. // through "F", "P", or a stream whose
  1385. // path begins with ":".
  1386. }
  1387. }
  1388. }
  1389. else
  1390. if (hr == S_FALSE)
  1391. hr = STG_E_FILENOTFOUND;
  1392. return hr;
  1393. }
  1394. HRESULT CITFileSystem::CImpITFileSystem::OpenTransformedLockbytes
  1395. (PathInfo *pPI, ILockBytes **ppLKB)
  1396. {
  1397. RonM_ASSERT(pPI->cwcStreamPath > 0 && pPI->awszStreamPath[0] != L':');
  1398. UINT iSpace = pPI->iLockedBytesSegment;
  1399. RonM_ASSERT(iSpace < m_pwscDataSpaceNames[1]);
  1400. BOOL fNewActivation = !m_papTransformDescriptors[iSpace];
  1401. if (fNewActivation)
  1402. {
  1403. HRESULT hr = ActivateDataSpace(iSpace);
  1404. if (!SUCCEEDED(hr))
  1405. return hr;
  1406. }
  1407. TransformDescriptor *pTD = m_papTransformDescriptors[iSpace];
  1408. RonM_ASSERT(pTD);
  1409. ILockBytes *pLKB = CTransformedLockBytes::FindTransformedLockBytes(pPI->awszStreamPath, pTD);
  1410. if (pLKB)
  1411. {
  1412. RonM_ASSERT(!fNewActivation);
  1413. *ppLKB = pLKB;
  1414. return NO_ERROR;
  1415. }
  1416. return CTransformedLockBytes::Open(NULL, pPI, pTD, this, ppLKB);
  1417. }
  1418. HRESULT __stdcall CITFileSystem::CImpITFileSystem::CreateStream
  1419. (IUnknown *pUnkOuter, const WCHAR *pwcsPath,
  1420. DWORD grfMode, IStreamITEx **ppStrm
  1421. )
  1422. {
  1423. return CreateStream(pUnkOuter, pwcsPath,
  1424. (m_itfsh.fFlags & fDefaultIsCompression)
  1425. ? (pwcsPath[0] == L':'? pwcsUncompressedSpace : pwcsLZXSpace)
  1426. : pwcsUncompressedSpace,
  1427. grfMode, ppStrm
  1428. );
  1429. }
  1430. HRESULT __stdcall CITFileSystem::CImpITFileSystem::CreateStream
  1431. (IUnknown *pUnkOuter, const WCHAR * pwcsName, const WCHAR *pwcsDataSpaceName,
  1432. DWORD grfMode, IStreamITEx ** ppstm
  1433. )
  1434. {
  1435. CSyncWith sw(m_cs);
  1436. ILockBytes *pLKB;
  1437. HRESULT hr = CreateLockBytes(NULL, pwcsName, pwcsDataSpaceName,
  1438. !(grfMode & STGM_FAILIFTHERE), &pLKB
  1439. );
  1440. if (SUCCEEDED(hr))
  1441. {
  1442. hr = CStream::OpenStream(pUnkOuter, pLKB, grfMode, ppstm);
  1443. if (!SUCCEEDED(hr))
  1444. pLKB->Release();
  1445. else
  1446. if (((IITLockBytes *) pLKB)->Container()->IsSecondary())
  1447. ((IITStreamITEx *) *ppstm)->Container()->MarkSecondary();
  1448. }
  1449. return hr;
  1450. }
  1451. HRESULT __stdcall CITFileSystem::CImpITFileSystem::OpenStream
  1452. (IUnknown *pUnkOuter, const WCHAR *pwcsPath,
  1453. DWORD grfMode, IStreamITEx **ppStream
  1454. )
  1455. {
  1456. CSyncWith sw(m_cs);
  1457. ILockBytes *pLKB;
  1458. HRESULT hr = OpenLockBytes(NULL, pwcsPath, &pLKB);
  1459. if (SUCCEEDED(hr))
  1460. {
  1461. hr = CStream::OpenStream(pUnkOuter, pLKB, grfMode, ppStream);
  1462. if (!SUCCEEDED(hr))
  1463. pLKB->Release();
  1464. else
  1465. if (((IITLockBytes *) pLKB)->Container()->IsSecondary())
  1466. ((IITStreamITEx *) *ppStream)->Container()->MarkSecondary();
  1467. }
  1468. return hr;
  1469. }
  1470. HRESULT __stdcall CITFileSystem::CImpITFileSystem::ConnectStorage(CImpITUnknown *pStg)
  1471. {
  1472. CSyncWith sw(m_cs);
  1473. pStg->MarkActive(m_pActiveStorageList);
  1474. return NO_ERROR;
  1475. }
  1476. HRESULT __stdcall CITFileSystem::CImpITFileSystem::ConnectLockBytes(CImpITUnknown *pStg)
  1477. {
  1478. CSyncWith sw(m_cs);
  1479. pStg->MarkActive(m_pActiveLockBytesList);
  1480. return NO_ERROR;
  1481. }
  1482. HRESULT __stdcall CITFileSystem::CImpITFileSystem::LookForActivity
  1483. (WCHAR const *pwcsName, IEnumSTATSTG *pEnum)
  1484. {
  1485. HRESULT hr = S_OK;
  1486. BOOL fFinished = FALSE;
  1487. BOOL fInUse = FALSE;
  1488. // The strategy we use is to enumerate paths until we find one that doesn't
  1489. // have a prefix which matches pwcsName.
  1490. for (;;)
  1491. {
  1492. STATSTG statstg;
  1493. statstg.pwcsName = NULL;
  1494. HRESULT hr2 = pEnum->Next(1, &statstg, NULL);
  1495. if (hr2 != S_OK)
  1496. {
  1497. RonM_ASSERT(statstg.pwcsName == NULL);
  1498. if (hr2 != S_FALSE) hr = hr2;
  1499. break;
  1500. }
  1501. RonM_ASSERT(statstg.pwcsName);
  1502. // Now we'll compare the two paths to see if we still have a prefix match.
  1503. const WCHAR *pwcPrefix = pwcsName;
  1504. const WCHAR *pwcEnum = statstg.pwcsName;
  1505. for (; ; )
  1506. {
  1507. WCHAR wcPrefix = WC_To_0x0409_Lower(*pwcPrefix++);
  1508. WCHAR wcEnum = WC_To_0x0409_Lower(*pwcEnum++);
  1509. if (wcPrefix == wcEnum && wcPrefix)
  1510. continue;
  1511. // We've either found a mismatch or we've exhausted the
  1512. // prefix string.
  1513. if(wcPrefix || (wcEnum && wcEnum != L'/'))
  1514. {
  1515. fFinished = TRUE; // Not a storage prefix
  1516. break;
  1517. }
  1518. // Next we look to see if this path is currently being used.
  1519. // We can't delete storages or streams that are active.
  1520. if (statstg.pwcsName[lstrlenW(statstg.pwcsName)-1] == L'/')
  1521. {
  1522. IStorageITEx *pStorage = FindActiveStorage(statstg.pwcsName);
  1523. if (pStorage)
  1524. {
  1525. pStorage->Release();
  1526. fInUse = TRUE;
  1527. fFinished = TRUE;
  1528. }
  1529. }
  1530. else
  1531. {
  1532. // For lockbyte objects we need to check the
  1533. // chain of uncompressed lockbyte objects and
  1534. // then perhaps the chain for each active data space.
  1535. ILockBytes *pLKB = FindActiveLockBytes(statstg.pwcsName);
  1536. if (!pLKB)
  1537. {
  1538. // Not found as an uncompressed lockbyte.
  1539. // So we iterate through the data spaces.
  1540. // BugBug: Need to abstract the space count and hide the
  1541. // way that we store this information.
  1542. UINT cSpaces = m_pwscDataSpaceNames[1];
  1543. for (; cSpaces--; )
  1544. {
  1545. TransformDescriptor *pTD = m_papTransformDescriptors[cSpaces];
  1546. if (!pTD) continue;
  1547. pLKB = FindMatchingLockBytes
  1548. (statstg.pwcsName,
  1549. (CImpITUnknown *) pTD->pLockBytesChain
  1550. );
  1551. if (pLKB) break;
  1552. }
  1553. }
  1554. if (pLKB)
  1555. {
  1556. pLKB->Release();
  1557. fInUse = TRUE;
  1558. fFinished = TRUE;
  1559. }
  1560. }
  1561. break;
  1562. }
  1563. OLEHeap()->Free(statstg.pwcsName);
  1564. if (fFinished) break;
  1565. }
  1566. if (fInUse) hr = STG_E_INUSE;
  1567. return hr;
  1568. }
  1569. HRESULT __stdcall CITFileSystem::CImpITFileSystem::DeleteItem(WCHAR const *pwcsName)
  1570. {
  1571. // For a stream we remove one item.
  1572. // For a storage we'll remove the storage and everything it contains!
  1573. CSyncWith sw(m_cs); // To avoid race conditions...
  1574. IEnumSTATSTG *pEnum = NULL;
  1575. HRESULT hr = m_pPathManager->EnumFromObject(NULL, pwcsName, wcsLen(pwcsName),
  1576. IID_IEnumSTATSTG, (PVOID *) &pEnum
  1577. );
  1578. if (!SUCCEEDED(hr)) return hr;
  1579. // First we look to see if this item or one of its descendants are being used.
  1580. // We can't delete an item that's in use.
  1581. hr = LookForActivity(pwcsName, pEnum);
  1582. if (!SUCCEEDED(hr))
  1583. {
  1584. pEnum->Release();
  1585. return hr;
  1586. }
  1587. // The strategy we use is to enumerate paths until we find one that doesn't
  1588. // have a prefix which matches pwcsName.
  1589. pEnum->Reset();
  1590. BOOL fFinished = FALSE;
  1591. for (;;)
  1592. {
  1593. STATSTG statstg;
  1594. statstg.pwcsName = NULL;
  1595. HRESULT hr2 = pEnum->Next(1, &statstg, NULL);
  1596. if (hr2 != S_OK)
  1597. {
  1598. RonM_ASSERT(statstg.pwcsName == NULL);
  1599. if (hr2 != S_FALSE) hr = hr2;
  1600. break;
  1601. }
  1602. RonM_ASSERT(statstg.pwcsName);
  1603. // Now we'll compare the two paths to see if we still have a prefix match.
  1604. const WCHAR *pwcPrefix = pwcsName;
  1605. const WCHAR *pwcEnum = statstg.pwcsName;
  1606. for (; ; )
  1607. {
  1608. WCHAR wcPrefix = WC_To_0x0409_Lower(*pwcPrefix++);
  1609. WCHAR wcEnum = WC_To_0x0409_Lower(*pwcEnum++);
  1610. if (wcPrefix == wcEnum && wcPrefix)
  1611. continue;
  1612. // We've either found a mismatch or we've exhausted the
  1613. // prefix string.
  1614. if(wcPrefix || (wcEnum && wcEnum != L'/'))
  1615. {
  1616. fFinished = TRUE; // Not a storage prefix
  1617. break;
  1618. }
  1619. // At this point we know that we've got a prefix match and that the
  1620. // matching path is not being used by anybody.
  1621. // Now we can delete the corresponding path manager entry.
  1622. PathInfo PI;
  1623. CopyPath(PI, statstg.pwcsName);
  1624. hr = m_pPathManager->DeleteEntry(&PI);
  1625. if (!SUCCEEDED(hr))
  1626. {
  1627. fFinished = TRUE;
  1628. break;
  1629. }
  1630. // If this path was for a uncompressed stream, we need to tell the
  1631. // free list manager about the data space we've just released for reuse.
  1632. if (PI.awszStreamPath[PI.cwcStreamPath-1] != L'/')
  1633. if (PI.iLockedBytesSegment == 0)
  1634. {
  1635. hr = m_pFreeListManager->PutFreeSpace(PI.ullcbOffset, PI.ullcbData);
  1636. if (!SUCCEEDED(hr)) fFinished = TRUE;
  1637. }
  1638. // BugBug Need to make a similar call to the transformed space...
  1639. break;
  1640. }
  1641. OLEHeap()->Free(statstg.pwcsName);
  1642. if (fFinished) break;
  1643. }
  1644. pEnum->Release();
  1645. return hr;
  1646. }
  1647. HRESULT __stdcall CITFileSystem::CImpITFileSystem::RenameItem(WCHAR const *pwcsOldName, WCHAR const *pwcsNewName)
  1648. {
  1649. // For a stream we remove one item.
  1650. // For a storage we'll remove the storage and everything it contains!
  1651. CSyncWith sw(m_cs); // To avoid race conditions...
  1652. IEnumSTATSTG *pEnum = NULL;
  1653. HRESULT hr = m_pPathManager->EnumFromObject(NULL, pwcsOldName, wcsLen(pwcsOldName),
  1654. IID_IEnumSTATSTG, (PVOID *) &pEnum
  1655. );
  1656. if (!SUCCEEDED(hr)) return hr;
  1657. // First we look to see if this item or one of its descendants are being used.
  1658. // We can't delete an item that's in use.
  1659. hr = LookForActivity(pwcsOldName, pEnum);
  1660. if (!SUCCEEDED(hr))
  1661. {
  1662. pEnum->Release();
  1663. return hr;
  1664. }
  1665. // The strategy we use is to enumerate paths until we find one that doesn't
  1666. // have a prefix which matches pwcsName.
  1667. pEnum->Reset();
  1668. BOOL fFinished = FALSE;
  1669. UINT cwcOldName = lstrlenW(pwcsOldName);
  1670. UINT cwcNewName = lstrlenW(pwcsNewName);
  1671. WCHAR awcsBuffer[MAX_PATH];
  1672. for (;;)
  1673. {
  1674. STATSTG statstg;
  1675. statstg.pwcsName = NULL;
  1676. HRESULT hr2 = pEnum->Next(1, &statstg, NULL);
  1677. if (hr2 != S_OK)
  1678. {
  1679. RonM_ASSERT(statstg.pwcsName == NULL);
  1680. if (hr2 != S_FALSE) hr = hr2;
  1681. break;
  1682. }
  1683. RonM_ASSERT(statstg.pwcsName);
  1684. // Now we'll compare the two paths to see if we still have a prefix match.
  1685. const WCHAR *pwcPrefix = pwcsOldName;
  1686. const WCHAR *pwcEnum = statstg.pwcsName;
  1687. for (; ; )
  1688. {
  1689. WCHAR wcPrefix = WC_To_0x0409_Lower(*pwcPrefix++);
  1690. WCHAR wcEnum = WC_To_0x0409_Lower(*pwcEnum++);
  1691. if (wcPrefix == wcEnum && wcPrefix)
  1692. continue;
  1693. // We've either found a mismatch or we've exhausted the
  1694. // prefix string.
  1695. if(wcPrefix || (wcEnum && wcEnum != L'/'))
  1696. {
  1697. fFinished = TRUE; // Not a storage prefix
  1698. break;
  1699. }
  1700. // At this point we know that we've got a prefix match and that the
  1701. // matching path is not being used by anybody.
  1702. // Now we look to see if the new name would be too long.
  1703. PWCHAR pwcsSuffix = statstg.pwcsName + cwcOldName;
  1704. UINT cwcSuffix = lstrlenW(pwcsSuffix);
  1705. if (cwcSuffix + cwcNewName >= MAX_PATH)
  1706. {
  1707. hr = STG_E_INVALIDNAME;
  1708. fFinished = TRUE;
  1709. break;
  1710. }
  1711. // Now we construct the new name and look to see if it already exists.
  1712. // We don't allow rename operations to destroy any existing items.
  1713. wcsCpy(awcsBuffer, pwcsNewName);
  1714. wcsCat(awcsBuffer, pwcsSuffix);
  1715. PathInfo PI;
  1716. CopyPath(PI, awcsBuffer);
  1717. hr2 = m_pPathManager->FindEntry(&PI);
  1718. if (hr2 == S_OK)
  1719. {
  1720. hr = STG_E_FILEALREADYEXISTS;
  1721. fFinished = TRUE;
  1722. break;
  1723. }
  1724. CopyPath(PI, statstg.pwcsName);
  1725. hr = m_pPathManager->DeleteEntry(&PI);
  1726. if (!SUCCEEDED(hr))
  1727. {
  1728. fFinished = TRUE;
  1729. break;
  1730. }
  1731. CopyPath(PI, awcsBuffer);
  1732. hr = m_pPathManager->CreateEntry(&PI, NULL, FALSE);
  1733. if (!SUCCEEDED(hr))
  1734. {
  1735. // This is bad. We've removed the old entry, but we haven't been
  1736. // able to create the new entry. So we've lost a item. The best
  1737. // we can do in this situation is to reclaim the space occupied
  1738. // by the item.
  1739. if (PI.awszStreamPath[PI.cwcStreamPath-1] != L'/')
  1740. if (PI.iLockedBytesSegment == 0)
  1741. m_pFreeListManager->PutFreeSpace(PI.ullcbOffset, PI.ullcbData);
  1742. // BugBug Need to make a similar call to the transformed space...
  1743. fFinished = TRUE;
  1744. break;
  1745. }
  1746. break;
  1747. }
  1748. OLEHeap()->Free(statstg.pwcsName);
  1749. if (fFinished) break;
  1750. }
  1751. pEnum->Release();
  1752. return hr;
  1753. RonM_ASSERT(FALSE);
  1754. return E_NOTIMPL;
  1755. }
  1756. HRESULT __stdcall CITFileSystem::CImpITFileSystem::UpdatePathInfo(PathInfo *pPathInfo)
  1757. {
  1758. CSyncWith sw(m_cs);
  1759. RonM_ASSERT(pPathInfo->cwcStreamPath); // No empty paths allowed!
  1760. PathInfo pi = *pPathInfo;
  1761. if ( pi.awszStreamPath[pPathInfo->cwcStreamPath - 1] != L'/'
  1762. && pi.awszStreamPath[pPathInfo->cwcStreamPath - 1] != L'\\'
  1763. && pi.iLockedBytesSegment == 0 // Uncompressed dataspace.
  1764. && pi.ullcbData.NonZero()
  1765. )
  1766. {
  1767. // This is a stream in the L"Uncompressed" dataspace.
  1768. // So we've got to adjust for our offset origin.
  1769. pi.ullcbOffset -= m_itfsh.offPathMgrOrigin;
  1770. }
  1771. HRESULT hr = PathMgr(pPathInfo)->UpdateEntry(&pi);
  1772. if ( hr != S_OK
  1773. && pPathInfo->cwcStreamPath
  1774. && pPathInfo->awszStreamPath[pPathInfo->cwcStreamPath - 1] == L'/'
  1775. )
  1776. {
  1777. // Since some storage entries don't have explicit entries in
  1778. // the path database, we must be prepared to add them when
  1779. // get an update request for a storage path.
  1780. PathInfo PI;
  1781. hr = PathMgr(pPathInfo)->CreateEntry(pPathInfo, &PI, FALSE);
  1782. }
  1783. if (hr == S_OK)
  1784. pPathInfo->cUnrecordedChanges = 0;
  1785. return hr;
  1786. }
  1787. IITPathManager *CITFileSystem::CImpITFileSystem::PathMgr(PathInfo *pPathInfo)
  1788. {
  1789. return ( pPathInfo->cwcStreamPath == 1
  1790. && (pPathInfo->awszStreamPath[0] == L'F' || pPathInfo->awszStreamPath[0] == L'P')
  1791. )? m_pSysPathManager : m_pPathManager;
  1792. }
  1793. HRESULT __stdcall CITFileSystem::CImpITFileSystem::GetITFSTimes
  1794. (FILETIME *pctime, FILETIME *patime,
  1795. FILETIME *pmtime
  1796. )
  1797. {
  1798. STATSTG statstg;
  1799. HRESULT hr = m_pLKBMedium->Stat(&statstg, STATFLAG_NONAME);
  1800. if (!SUCCEEDED(hr)) return hr;
  1801. if (pctime)
  1802. {
  1803. pctime->dwLowDateTime = statstg.ctime.dwLowDateTime;
  1804. pctime->dwHighDateTime = statstg.ctime.dwHighDateTime;
  1805. }
  1806. if (patime)
  1807. {
  1808. patime->dwLowDateTime = statstg.atime.dwLowDateTime;
  1809. patime->dwHighDateTime = statstg.atime.dwHighDateTime;
  1810. }
  1811. if (pmtime)
  1812. {
  1813. pmtime->dwLowDateTime = statstg.mtime.dwLowDateTime;
  1814. pmtime->dwHighDateTime = statstg.mtime.dwHighDateTime;
  1815. }
  1816. return NO_ERROR;
  1817. }
  1818. HRESULT __stdcall CITFileSystem::CImpITFileSystem::ReallocEntry
  1819. (PathInfo *pPathInfo, CULINT ullcbNew, BOOL fCopyContent)
  1820. {
  1821. CSyncWith sw(m_cs);
  1822. CULINT ullBaseNew;
  1823. HRESULT hr = m_pFreeListManager->GetFreeSpace(&ullBaseNew, &ullcbNew);
  1824. if (SUCCEEDED(hr) & fCopyContent)
  1825. {
  1826. CULINT ullcb;
  1827. ullcb = pPathInfo->ullcbData;
  1828. if (ullcb > ullcbNew)
  1829. ullcb = ullcbNew;
  1830. hr = IITLockBytes::CopyLockBytes(m_pLKBMedium, pPathInfo->ullcbOffset,
  1831. pPathInfo->ullcbOffset + ullcb,
  1832. m_pLKBMedium, ullBaseNew
  1833. );
  1834. }
  1835. if (SUCCEEDED(hr))
  1836. {
  1837. hr = m_pFreeListManager->PutFreeSpace(pPathInfo->ullcbOffset, pPathInfo->ullcbData);
  1838. RonM_ASSERT(SUCCEEDED(hr));
  1839. pPathInfo->ullcbOffset = ullBaseNew;
  1840. pPathInfo->ullcbData = ullcbNew;
  1841. }
  1842. else
  1843. {
  1844. HRESULT hr = m_pFreeListManager->PutFreeSpace(ullBaseNew, ullcbNew);
  1845. RonM_ASSERT(SUCCEEDED(hr));
  1846. }
  1847. IITPathManager *pPathManager = PathMgr(pPathInfo);
  1848. if (SUCCEEDED(hr))
  1849. if (m_pSysPathManager == pPathManager)
  1850. m_pSysPathManager->UpdateEntry(pPathInfo);
  1851. else
  1852. if (++(pPathInfo->cUnrecordedChanges) > PENDING_CHANGE_LIMIT)
  1853. UpdatePathInfo(pPathInfo);
  1854. return hr;
  1855. }
  1856. HRESULT __stdcall CITFileSystem::CImpITFileSystem::ReallocInPlace
  1857. (PathInfo *pPathInfo, CULINT ullcbNew)
  1858. {
  1859. CSyncWith sw(m_cs);
  1860. HRESULT hr = NO_ERROR;
  1861. if (!(pPathInfo->ullcbData.NonZero())) // Currently empty?
  1862. {
  1863. if (ullcbNew.NonZero())
  1864. hr = ReallocEntry(pPathInfo, ullcbNew, FALSE);
  1865. return hr;
  1866. }
  1867. if (pPathInfo->ullcbData == ullcbNew)
  1868. return hr;
  1869. IITPathManager *pPathManager = PathMgr(pPathInfo);
  1870. if (pPathInfo->ullcbData > ullcbNew)
  1871. {
  1872. CULINT ullBaseTrailing, ullcbTrailing;
  1873. ullBaseTrailing = pPathInfo->ullcbOffset + ullcbNew;
  1874. ullcbTrailing = pPathInfo->ullcbData - ullcbNew;
  1875. // Shrinking always works...
  1876. hr = m_pFreeListManager->PutFreeSpace(ullBaseTrailing, ullcbTrailing);
  1877. RonM_ASSERT(SUCCEEDED(hr));
  1878. if (SUCCEEDED(hr))
  1879. {
  1880. pPathInfo->ullcbData = ullcbNew;
  1881. if (m_pSysPathManager == pPathManager)
  1882. m_pSysPathManager->UpdateEntry(pPathInfo);
  1883. else
  1884. if (++(pPathInfo->cUnrecordedChanges) > PENDING_CHANGE_LIMIT)
  1885. UpdatePathInfo(pPathInfo);
  1886. }
  1887. return hr;
  1888. }
  1889. ullcbNew -= pPathInfo->ullcbData;
  1890. hr = m_pFreeListManager->GetFreeSpaceAt
  1891. (pPathInfo->ullcbOffset + pPathInfo->ullcbData, &ullcbNew);
  1892. if (hr == S_OK)
  1893. {
  1894. pPathInfo->ullcbData += ullcbNew;
  1895. if (m_pSysPathManager == pPathManager)
  1896. m_pSysPathManager->UpdateEntry(pPathInfo);
  1897. else
  1898. if (++(pPathInfo->cUnrecordedChanges) > PENDING_CHANGE_LIMIT)
  1899. UpdatePathInfo(pPathInfo);
  1900. }
  1901. return hr;
  1902. }
  1903. HRESULT __stdcall CITFileSystem::CImpITFileSystem::EnumeratePaths
  1904. (WCHAR const *pwcsPathPrefix, IEnumSTATSTG **ppEnumStatStg)
  1905. {
  1906. CSyncWith sw(m_cs);
  1907. HRESULT hr = CEnumFSItems::NewFSEnumerator
  1908. (this, pwcsPathPrefix, wcsLen(pwcsPathPrefix), ppEnumStatStg);
  1909. return hr;
  1910. }
  1911. HRESULT __stdcall CITFileSystem::CImpITFileSystem::IsWriteable()
  1912. {
  1913. return m_fReadOnly? S_FALSE : S_OK;
  1914. }
  1915. HRESULT __stdcall CITFileSystem::CImpITFileSystem::FSObjectReleased()
  1916. {
  1917. if (m_cFSObjectRefs != UINT(-1))
  1918. {
  1919. RonM_ASSERT(m_cFSObjectRefs);
  1920. AddRef(); // Because this object is about to disappear.
  1921. m_cFSObjectRefs--;
  1922. }
  1923. return NO_ERROR;
  1924. }
  1925. HRESULT CITFileSystem::CImpITFileSystem::CSystemPathManager::Create
  1926. (IUnknown *punkOuter, CImpITFileSystem *pITFileSystem, IITPathManager **ppPathMgr)
  1927. {
  1928. CSystemPathManager *pSysPathMgr = New CSystemPathManager(punkOuter);
  1929. return FinishSetup(pSysPathMgr? pSysPathMgr->m_PathManager.Init(pITFileSystem)
  1930. : STG_E_INSUFFICIENTMEMORY,
  1931. pSysPathMgr, IID_PathManager, (PPVOID)ppPathMgr
  1932. );
  1933. }
  1934. CITFileSystem::CImpITFileSystem::CSystemPathManager::
  1935. CImpIPathManager::CImpIPathManager(CSystemPathManager *pBackObj, IUnknown *punkOuter)
  1936. : IITPathManager(pBackObj, punkOuter)
  1937. {
  1938. m_pIITFS = NULL;
  1939. }
  1940. HRESULT STDMETHODCALLTYPE CITFileSystem::CImpITFileSystem::CSystemPathManager::
  1941. CImpIPathManager::Init(CImpITFileSystem *pITFileSystem)
  1942. {
  1943. m_pIITFS = pITFileSystem;
  1944. return NO_ERROR;
  1945. }
  1946. // IPersist Method:
  1947. HRESULT STDMETHODCALLTYPE CITFileSystem::CImpITFileSystem::CSystemPathManager::
  1948. CImpIPathManager::GetClassID(CLSID __RPC_FAR *pClassID)
  1949. {
  1950. *pClassID = CLSID_SystemPathManager;
  1951. return NO_ERROR;
  1952. }
  1953. // IITPathManager interfaces:
  1954. HRESULT STDMETHODCALLTYPE CITFileSystem::CImpITFileSystem::CSystemPathManager::
  1955. CImpIPathManager::FlushToLockBytes()
  1956. {
  1957. RonM_ASSERT(FALSE); // To detect unexpected calls to this interface.
  1958. return E_NOTIMPL;
  1959. }
  1960. HRESULT STDMETHODCALLTYPE CITFileSystem::CImpITFileSystem::CSystemPathManager::
  1961. CImpIPathManager::FindEntry (PPathInfo pPI )
  1962. {
  1963. RonM_ASSERT(pPI->cwcStreamPath == 1);
  1964. pPI->uStateBits = 0;
  1965. pPI->iLockedBytesSegment = 0;
  1966. pPI->cUnrecordedChanges = 0;
  1967. switch(pPI->awszStreamPath[0])
  1968. {
  1969. case L'F':
  1970. pPI->ullcbOffset = m_pIITFS->m_itfsh.offFreeListData;
  1971. pPI->ullcbData = m_pIITFS->m_itfsh. cbFreeListData;
  1972. return NO_ERROR;
  1973. case L'P':
  1974. pPI->ullcbOffset = m_pIITFS->m_itfsh.offPathMgrData;
  1975. pPI->ullcbData = m_pIITFS->m_itfsh. cbPathMgrData;
  1976. return NO_ERROR;
  1977. default:
  1978. RonM_ASSERT(FALSE);
  1979. return STG_E_DOCFILECORRUPT;
  1980. }
  1981. }
  1982. HRESULT STDMETHODCALLTYPE CITFileSystem::CImpITFileSystem::CSystemPathManager::
  1983. CImpIPathManager::CreateEntry(PPathInfo pPINew,
  1984. PPathInfo pPIOld,
  1985. BOOL fReplace )
  1986. {
  1987. RonM_ASSERT(FALSE); // To detect unexpected calls to this interface.
  1988. return E_NOTIMPL;
  1989. }
  1990. HRESULT STDMETHODCALLTYPE CITFileSystem::CImpITFileSystem::CSystemPathManager::
  1991. CImpIPathManager::DeleteEntry(PPathInfo pPI )
  1992. {
  1993. RonM_ASSERT(FALSE); // To detect unexpected calls to this interface.
  1994. return E_NOTIMPL;
  1995. }
  1996. HRESULT STDMETHODCALLTYPE CITFileSystem::CImpITFileSystem::CSystemPathManager::
  1997. CImpIPathManager::UpdateEntry(PPathInfo pPI )
  1998. {
  1999. RonM_ASSERT(pPI->cwcStreamPath == 1);
  2000. RonM_ASSERT(pPI->iLockedBytesSegment == 0);
  2001. switch(pPI->awszStreamPath[0])
  2002. {
  2003. case L'F':
  2004. m_pIITFS->m_itfsh.offFreeListData = pPI->ullcbOffset;
  2005. m_pIITFS->m_itfsh. cbFreeListData = pPI->ullcbData;
  2006. pPI->cUnrecordedChanges = 0;
  2007. m_pIITFS->m_fHeaderIsDirty = TRUE;
  2008. return NO_ERROR;
  2009. case L'P':
  2010. m_pIITFS->m_itfsh.offPathMgrData = pPI->ullcbOffset;
  2011. m_pIITFS->m_itfsh. cbPathMgrData = pPI->ullcbData;
  2012. pPI->cUnrecordedChanges = 0;
  2013. m_pIITFS->m_fHeaderIsDirty = TRUE;
  2014. return NO_ERROR;
  2015. default:
  2016. RonM_ASSERT(FALSE);
  2017. return STG_E_DOCFILECORRUPT;
  2018. }
  2019. }
  2020. HRESULT STDMETHODCALLTYPE CITFileSystem::CImpITFileSystem::CSystemPathManager::
  2021. CImpIPathManager::EnumFromObject(IUnknown *punkOuter,
  2022. const WCHAR *pwszPrefix,
  2023. UINT cwcPrefix,
  2024. REFIID riid,
  2025. PVOID *ppv
  2026. )
  2027. {
  2028. RonM_ASSERT(FALSE); // To detect unexpected calls to this interface.
  2029. return E_NOTIMPL;
  2030. }
  2031. HRESULT STDMETHODCALLTYPE CITFileSystem::CImpITFileSystem::CSystemPathManager::
  2032. CImpIPathManager::GetPathDB(IStreamITEx *pTempPDBStrm, BOOL fCompact)
  2033. {
  2034. RonM_ASSERT(FALSE); // To detect unexpected calls to this interface.
  2035. return E_NOTIMPL;
  2036. }
  2037. HRESULT STDMETHODCALLTYPE CITFileSystem::CImpITFileSystem::CSystemPathManager::
  2038. CImpIPathManager::ForceClearDirty()
  2039. {
  2040. RonM_ASSERT(FALSE); // To detect unexpected calls to this interface.
  2041. return E_NOTIMPL;
  2042. }
  2043. CITFileSystem::CImpITFileSystem::CEnumFSItems::CImpIEnumSTATSTG::CImpIEnumSTATSTG
  2044. (CEnumFSItems *pBackObj, IUnknown *punkOuter)
  2045. : IITEnumSTATSTG(pBackObj, punkOuter)
  2046. {
  2047. m_pEnumPathMgr = NULL;
  2048. m_pITFS = NULL;
  2049. }
  2050. CITFileSystem::CImpITFileSystem::CEnumFSItems::CImpIEnumSTATSTG::~CImpIEnumSTATSTG()
  2051. {
  2052. if (m_pEnumPathMgr)
  2053. m_pEnumPathMgr->Release();
  2054. if (m_pITFS)
  2055. m_pITFS->Release();
  2056. }
  2057. HRESULT CITFileSystem::CImpITFileSystem::CEnumFSItems::NewFSEnumerator
  2058. (CImpITFileSystem *pITFS,
  2059. const WCHAR *pwszPathPrefix,
  2060. UINT cwcPathPrefix,
  2061. IEnumSTATSTG **ppEnumSTATSTG
  2062. )
  2063. {
  2064. CEnumFSItems *pEnumFS = New CEnumFSItems(NULL);
  2065. CSyncWith sw(pITFS->m_cs);
  2066. return FinishSetup(pEnumFS? pEnumFS->m_ImpEnumSTATSTG.InitFSEnumerator
  2067. (pITFS, pwszPathPrefix, cwcPathPrefix)
  2068. : STG_E_INSUFFICIENTMEMORY,
  2069. pEnumFS, IID_IEnumSTATSTG, (PPVOID) ppEnumSTATSTG
  2070. );
  2071. }
  2072. HRESULT CITFileSystem::CImpITFileSystem::CEnumFSItems::NewCloneOf
  2073. (CImpIEnumSTATSTG *pImpEnumFS,
  2074. IEnumSTATSTG **ppEnumSTATSTG
  2075. )
  2076. {
  2077. CEnumFSItems *pEnumFS = New CEnumFSItems(NULL);
  2078. CSyncWith sw(pImpEnumFS->m_pITFS->m_cs);
  2079. return FinishSetup(pEnumFS? pEnumFS->m_ImpEnumSTATSTG.InitNewCloneOf(pImpEnumFS)
  2080. : STG_E_INSUFFICIENTMEMORY,
  2081. pEnumFS, IID_IEnumSTATSTG, (PPVOID) ppEnumSTATSTG
  2082. );
  2083. }
  2084. HRESULT CITFileSystem::CImpITFileSystem::CEnumFSItems::CImpIEnumSTATSTG::InitNewCloneOf (CImpIEnumSTATSTG *pImpEnumFS)
  2085. {
  2086. m_pITFS = pImpEnumFS->m_pITFS;
  2087. m_pITFS->AddRef();
  2088. return pImpEnumFS->m_pEnumPathMgr->Clone(&m_pEnumPathMgr);
  2089. }
  2090. HRESULT CITFileSystem::CImpITFileSystem::CEnumFSItems::CImpIEnumSTATSTG::InitFSEnumerator
  2091. (CImpITFileSystem *pITFS, const WCHAR *pwszPathPrefix, UINT cwcPathPrefix)
  2092. {
  2093. m_pITFS = pITFS;
  2094. m_pITFS->AddRef();
  2095. HRESULT hr = m_pITFS->m_pPathManager->EnumFromObject
  2096. (NULL, pwszPathPrefix, cwcPathPrefix, IID_IEnumSTATSTG,
  2097. (PVOID *) &m_pEnumPathMgr
  2098. );
  2099. if (SUCCEEDED(hr))
  2100. ((IITEnumSTATSTG *) m_pEnumPathMgr)->Container()->MarkSecondary();
  2101. return hr;
  2102. }
  2103. HRESULT STDMETHODCALLTYPE CITFileSystem::CImpITFileSystem::CEnumFSItems::CImpIEnumSTATSTG::Next(
  2104. /* [in] */ ULONG celt,
  2105. /* [in] */ STATSTG __RPC_FAR *rgelt,
  2106. /* [out] */ ULONG __RPC_FAR *pceltFetched)
  2107. {
  2108. return m_pEnumPathMgr->Next(celt, rgelt, pceltFetched);
  2109. }
  2110. HRESULT STDMETHODCALLTYPE CITFileSystem::CImpITFileSystem::CEnumFSItems::CImpIEnumSTATSTG::Skip(
  2111. /* [in] */ ULONG celt)
  2112. {
  2113. return m_pEnumPathMgr->Skip(celt);
  2114. }
  2115. HRESULT STDMETHODCALLTYPE CITFileSystem::CImpITFileSystem::CEnumFSItems::CImpIEnumSTATSTG::Reset(
  2116. void)
  2117. {
  2118. return m_pEnumPathMgr->Reset();
  2119. }
  2120. HRESULT STDMETHODCALLTYPE CITFileSystem::CImpITFileSystem::CEnumFSItems::CImpIEnumSTATSTG::Clone(
  2121. /* [out] */ IEnumSTATSTG __RPC_FAR *__RPC_FAR *ppenum)
  2122. {
  2123. return CEnumFSItems::NewCloneOf(this, ppenum);
  2124. }
  2125. HRESULT CITFileSystem::CImpITFileSystem::DeactivateSpace(UINT iSpace)
  2126. {
  2127. return DeactivateDataSpace(iSpace);
  2128. }
  2129. HRESULT CITFileSystem::CImpITFileSystem::GetFirstRecord(SEntry *prec,
  2130. IStreamITEx *pRecTblStrm,
  2131. int cTblRecsInCache,
  2132. int cTblRecsTotal,
  2133. SEntry *pRecTblCache)
  2134. {
  2135. HRESULT hr = NO_ERROR;
  2136. RonM_ASSERT(cTblRecsTotal > 0);
  2137. if (cTblRecsTotal == cTblRecsInCache)
  2138. {
  2139. CopyMemory(prec, (LPVOID)&pRecTblCache[0], sizeof(SEntry));
  2140. }
  2141. else
  2142. {
  2143. LARGE_INTEGER li = CLINT(0).Li();
  2144. ULONG cbRead;
  2145. if (SUCCEEDED(hr = pRecTblStrm->Seek(li, STREAM_SEEK_SET, NULL)))
  2146. {
  2147. if (SUCCEEDED(hr = pRecTblStrm->Read(prec, sizeof(SEntry), &cbRead)))
  2148. {
  2149. RonM_ASSERT(cbRead == sizeof(SEntry));
  2150. }
  2151. }
  2152. }
  2153. return hr;
  2154. }
  2155. HRESULT CITFileSystem::CImpITFileSystem::GetNextRecord(ULONG ulCurRec, SEntry *prec,
  2156. IStreamITEx *pRecTblStrm,
  2157. int cTblRecsInCache,
  2158. int cTblRecsTotal,
  2159. SEntry *pRecTblCache)
  2160. {
  2161. RonM_ASSERT(cTblRecsTotal > 0);
  2162. HRESULT hr = NO_ERROR;
  2163. if (cTblRecsTotal == cTblRecsInCache)
  2164. {
  2165. if (ulCurRec == cTblRecsInCache)
  2166. return S_FALSE;
  2167. CopyMemory(prec, (LPVOID)&pRecTblCache[ulCurRec + 1], sizeof(SEntry));
  2168. }
  2169. else
  2170. {
  2171. ULONG cbRead;
  2172. if (SUCCEEDED(hr = pRecTblStrm->Read(prec, sizeof(SEntry), &cbRead)))
  2173. {
  2174. RonM_ASSERT(cbRead == sizeof(SEntry));
  2175. }
  2176. }
  2177. return S_OK;
  2178. }
  2179. HRESULT CITFileSystem::CImpITFileSystem::UpdatePathDB(IStreamITEx *pRecTblStrm, int cTblRecsInCache,
  2180. int cTblRecsTotal, SEntry *pRecTblCache,
  2181. CITSortRecords *pSort)
  2182. {
  2183. HRESULT hr = NO_ERROR;
  2184. if (cTblRecsTotal == 0)
  2185. return hr;
  2186. IITEnumSTATSTG *pEnumPathMgr = NULL;
  2187. ULONG ulCurEnumIndex;
  2188. ULONG celtFetched = 1;
  2189. UINT iDataSpace = FindSpaceName(pwcsUncompressedSpace);
  2190. PathInfo pathInfo;
  2191. if (SUCCEEDED(hr = m_pPathManager->EnumFromObject(NULL, L"//", 1,
  2192. IID_IEnumSTATSTG, (PVOID *) &pEnumPathMgr)))
  2193. {
  2194. hr = pEnumPathMgr->GetFirstEntryInSeq(&pathInfo);
  2195. ulCurEnumIndex = 0;
  2196. }
  2197. if (cTblRecsTotal == cTblRecsInCache)
  2198. {
  2199. for (int iRec = 0; SUCCEEDED(hr) && (iRec < cTblRecsInCache); iRec++)
  2200. {
  2201. SEntry *pe = pRecTblCache + iRec;
  2202. RonM_ASSERT(pe->ulEntryID > ulCurEnumIndex);
  2203. ULONG cEntriesToSkip = (pe->ulEntryID - ulCurEnumIndex);
  2204. for (int iEnum = 0; SUCCEEDED(hr) && (celtFetched > 0) && (iEnum < cEntriesToSkip); iEnum++)
  2205. {
  2206. hr = pEnumPathMgr->GetNextEntryInSeq(1, &pathInfo, &celtFetched);
  2207. ulCurEnumIndex += 1;
  2208. }//for
  2209. RonM_ASSERT(pe->ulEntryID == ulCurEnumIndex);
  2210. RonM_ASSERT (pathInfo.awszStreamPath[pathInfo.cwcStreamPath - 1] != L'/'
  2211. && pathInfo.awszStreamPath[pathInfo.cwcStreamPath - 1] != L'\\'
  2212. && pathInfo.iLockedBytesSegment == 0 // Uncompressed dataspace.
  2213. && pathInfo.ullcbData.NonZero());
  2214. RonM_ASSERT(pathInfo.ullcbOffset >= pe->ullcbOffset);
  2215. if (pathInfo.ullcbOffset != pe->ullcbOffset)
  2216. {
  2217. //wprintf(L"Name = %s, offset=%d newOffset =%d size= %d entryid= %d\n", pathInfo.awszStreamPath, (int)pathInfo.ullcbOffset.Ull(), (int)pe->ullcbOffset.Ull(), (int)pathInfo.ullcbData.Ull(), (int)pe->ulEntryID);
  2218. pathInfo.ullcbOffset = pe->ullcbOffset;
  2219. if (SUCCEEDED(hr = PathMgr(&pathInfo)->UpdateEntry(&pathInfo)))
  2220. pathInfo.cUnrecordedChanges = 0;
  2221. }
  2222. }// update all records
  2223. }
  2224. else
  2225. {
  2226. BOOL fEndLoop = FALSE;
  2227. int iCurBlk = -1;
  2228. ULONG cRecsToRead;
  2229. while (!fEndLoop && SUCCEEDED(hr))
  2230. {
  2231. if (SUCCEEDED(hr = pSort->ReadNextSortedBlk(&iCurBlk, (LPBYTE)pRecTblCache, &cRecsToRead, &fEndLoop)) && !fEndLoop)
  2232. {
  2233. for (int iRec = 0; SUCCEEDED(hr) && (iRec < cRecsToRead); iRec++)
  2234. {
  2235. SEntry *pe = pRecTblCache + iRec;
  2236. RonM_ASSERT(pe->ulEntryID > ulCurEnumIndex);
  2237. ULONG cEntriesToSkip = (pe->ulEntryID - ulCurEnumIndex);
  2238. for (int iEnum = 0; SUCCEEDED(hr) && (iEnum < cEntriesToSkip); iEnum++)
  2239. {
  2240. hr = pEnumPathMgr->GetNextEntryInSeq(1, &pathInfo, &celtFetched);
  2241. ulCurEnumIndex += 1;
  2242. }//for
  2243. RonM_ASSERT(pe->ulEntryID == ulCurEnumIndex);
  2244. RonM_ASSERT (pathInfo.awszStreamPath[pathInfo.cwcStreamPath - 1] != L'/'
  2245. && pathInfo.awszStreamPath[pathInfo.cwcStreamPath - 1] != L'\\'
  2246. && pathInfo.iLockedBytesSegment == 0 // Uncompressed dataspace.
  2247. && pathInfo.ullcbData.NonZero());
  2248. RonM_ASSERT(pathInfo.ullcbOffset >= pe->ullcbOffset);
  2249. if (pathInfo.ullcbOffset != pe->ullcbOffset)
  2250. {
  2251. //wprintf(L"Name = %s, offset=%d newOffset =%d size= %d entryid= %d\n", pathInfo.awszStreamPath, (int)pathInfo.ullcbOffset.Ull(), (int)pe->ullcbOffset.Ull(), (int)pathInfo.ullcbData.Ull(), (int)pe->ulEntryID);
  2252. pathInfo.ullcbOffset = pe->ullcbOffset;
  2253. if (SUCCEEDED(hr = PathMgr(&pathInfo)->UpdateEntry(&pathInfo)))
  2254. pathInfo.cUnrecordedChanges = 0;
  2255. }
  2256. }// update all records
  2257. }//read next file block in sorted sequence
  2258. }//while
  2259. }//else
  2260. if (pEnumPathMgr)
  2261. pEnumPathMgr->Release();
  2262. return hr;
  2263. }
  2264. HRESULT CITFileSystem::CImpITFileSystem::SortRecTable(ESortField eSField,
  2265. IStreamITEx *pRecTblStrm,
  2266. int cTblRecsInCache,
  2267. int cTblRecsTotal,
  2268. SEntry *pRecTblCache,
  2269. CITSortRecords **ppSort)
  2270. {
  2271. HRESULT hr = NO_ERROR;
  2272. if (*ppSort == NULL)
  2273. {
  2274. *ppSort = (CITSortRecords *)New CITSortRecords;
  2275. if (*ppSort == NULL)
  2276. return STG_E_INSUFFICIENTMEMORY;
  2277. }
  2278. (*ppSort)->Initialize(pRecTblStrm, eSField, cTblRecsTotal, MAX_TABLE_RECS_INCACHE, sizeof(SEntry));
  2279. if (*ppSort && cTblRecsTotal)
  2280. {
  2281. if (cTblRecsTotal == cTblRecsInCache)
  2282. {
  2283. (*ppSort)->QSort((LPBYTE)pRecTblCache, 0, cTblRecsInCache - 1, &CompareEntries);
  2284. VerifyData((LPBYTE)pRecTblCache, cTblRecsTotal, eSField, TRUE);
  2285. }
  2286. else
  2287. {
  2288. hr = (*ppSort)->FileSort(&CompareEntries);
  2289. }
  2290. }
  2291. return hr;
  2292. }
  2293. int CITFileSystem::CImpITFileSystem::CompareEntries(SEntry e1, SEntry e2, ESortField eSField)
  2294. {
  2295. if (eSField == SORT_BY_ENTRYID)
  2296. {
  2297. if (e1.ulEntryID < e2.ulEntryID)
  2298. return -1;
  2299. else if (e1.ulEntryID > e2.ulEntryID)
  2300. return 1;
  2301. else
  2302. return 0;
  2303. //return ((long)e1.ulEntryID - (long)e2.ulEntryID);
  2304. }
  2305. else
  2306. {
  2307. if (e1.ullcbOffset < e2.ullcbOffset)
  2308. return -1;
  2309. else if (e1.ullcbOffset > e2.ullcbOffset)
  2310. return 1;
  2311. else return 0;
  2312. }
  2313. }
  2314. HRESULT CITFileSystem::CImpITFileSystem::AppendToRecTbl(SEntry *prec,
  2315. IStreamITEx *pRecTblStrm,
  2316. int *pcTblRecsInCache,
  2317. int *pcTblRecsTotal,
  2318. SEntry *pRecTblCache)
  2319. {
  2320. HRESULT hr = NO_ERROR;
  2321. //printf("Entry[%d] offset= %d, entryid = %d, size = %d\n",
  2322. // prec->ulEntryID, (int)prec->ullcbOffset.Ull(),
  2323. // (int)prec->ulEntryID, (int)prec->ullcbData.Ull());
  2324. if (*pcTblRecsInCache >= MAX_TABLE_RECS_INCACHE)
  2325. {
  2326. ULONG cbWritten;
  2327. if (SUCCEEDED(hr = pRecTblStrm->Write((LPVOID)pRecTblCache, sizeof(SEntry)* (*pcTblRecsInCache), &cbWritten)))
  2328. {
  2329. RonM_ASSERT(cbWritten == sizeof(SEntry)*(*pcTblRecsInCache));
  2330. *pcTblRecsInCache = 0;
  2331. }
  2332. }
  2333. CopyMemory(&pRecTblCache[(*pcTblRecsInCache)++], prec, sizeof(SEntry));
  2334. (*pcTblRecsTotal)++;
  2335. return hr;
  2336. }
  2337. HRESULT CITFileSystem::CImpITFileSystem::InitRecTable(IStreamITEx **ppRecTblStrm,
  2338. int *pcTblRecsInCache,
  2339. int *pcTblRecsTotal,
  2340. SEntry **ppRecTblCache)
  2341. {
  2342. *pcTblRecsInCache = 0;
  2343. *pcTblRecsTotal = 0;
  2344. *ppRecTblCache = NULL;
  2345. *ppRecTblStrm = NULL;
  2346. *ppRecTblCache = (SEntry *) New BYTE[sizeof(SEntry) * MAX_TABLE_RECS_INCACHE];
  2347. if (*ppRecTblCache == NULL)
  2348. return STG_E_INSUFFICIENTMEMORY;
  2349. HRESULT hr = CreateTempStm(ppRecTblStrm);
  2350. return hr;
  2351. }
  2352. HRESULT CITFileSystem::CImpITFileSystem::CreateTempStm(IStreamITEx **ppRecTblStrm)
  2353. {
  2354. ILockBytes *pLKB = NULL;
  2355. *ppRecTblStrm = NULL;
  2356. HRESULT hr = CFSLockBytes::CreateTemp(NULL, &pLKB);
  2357. if (hr == S_OK)
  2358. {
  2359. hr = CStream::OpenStream(NULL,pLKB, STGM_READWRITE, (IStreamITEx **) ppRecTblStrm);
  2360. if (hr != S_OK)
  2361. pLKB->Release();
  2362. else
  2363. {
  2364. //(*ppRecTblStrm)->AddRef();
  2365. LARGE_INTEGER uli = CLINT(0).Li();
  2366. hr = (*ppRecTblStrm)->Seek(uli, STREAM_SEEK_SET, NULL);
  2367. }
  2368. }
  2369. return hr;
  2370. }
  2371. HRESULT CITFileSystem::CImpITFileSystem::BuildUpEntryTable(ULONG *pulRecNum,
  2372. IStreamITEx *pRecTblStrm,
  2373. int *pcTblRecsInCache,
  2374. int *pcTblRecsTotal,
  2375. SEntry *pRecTblCache,
  2376. BOOL *pfNeedFileSort)
  2377. {
  2378. //init out param
  2379. *pcTblRecsTotal = *pcTblRecsInCache = 0;
  2380. *pulRecNum = 0;
  2381. IITEnumSTATSTG *pEnumPathMgr;
  2382. HRESULT hr = NO_ERROR;
  2383. if (SUCCEEDED(hr = m_pPathManager->EnumFromObject(NULL, L"//", 1,
  2384. IID_IEnumSTATSTG, (PVOID *) &pEnumPathMgr)))
  2385. {
  2386. SEntry srec;
  2387. ULONG celtFetched = 1;
  2388. PathInfo pathInfo;
  2389. UINT iDataSpace = FindSpaceName(pwcsUncompressedSpace);
  2390. if (SUCCEEDED(hr = pEnumPathMgr->GetFirstEntryInSeq(&pathInfo)))
  2391. {
  2392. if (pathInfo.awszStreamPath[pathInfo.cwcStreamPath - 1] != L'/'
  2393. && pathInfo.awszStreamPath[pathInfo.cwcStreamPath - 1] != L'\\'
  2394. && pathInfo.iLockedBytesSegment == 0 // Uncompressed dataspace.
  2395. && pathInfo.ullcbData.NonZero()
  2396. )
  2397. {
  2398. srec.ullcbOffset = pathInfo.ullcbOffset;
  2399. srec.ullcbData = pathInfo.ullcbData ;
  2400. srec.ulEntryID = 0;
  2401. AppendToRecTbl(&srec, pRecTblStrm, pcTblRecsInCache,
  2402. pcTblRecsTotal, pRecTblCache);
  2403. }
  2404. (*pulRecNum) += 1;
  2405. while (SUCCEEDED(hr) && (celtFetched > 0))
  2406. {
  2407. if (SUCCEEDED(hr = pEnumPathMgr->GetNextEntryInSeq(1, &pathInfo, &celtFetched))
  2408. && (celtFetched > 0))
  2409. {
  2410. if (pathInfo.awszStreamPath[pathInfo.cwcStreamPath - 1] != L'/'
  2411. && pathInfo.awszStreamPath[pathInfo.cwcStreamPath - 1] != L'\\'
  2412. && pathInfo.iLockedBytesSegment == 0 // Uncompressed dataspace.
  2413. && pathInfo.ullcbData.NonZero())
  2414. {
  2415. srec.ullcbOffset = pathInfo.ullcbOffset;
  2416. srec.ullcbData = pathInfo.ullcbData ;
  2417. srec.ulEntryID = *pulRecNum;
  2418. AppendToRecTbl(&srec, pRecTblStrm, pcTblRecsInCache,
  2419. pcTblRecsTotal, pRecTblCache);
  2420. }
  2421. (*pulRecNum) += 1;
  2422. }
  2423. }
  2424. }
  2425. *pfNeedFileSort = (*pcTblRecsTotal > *pcTblRecsInCache);
  2426. pEnumPathMgr->Release();
  2427. }
  2428. return hr;
  2429. }
  2430. void CITFileSystem::CImpITFileSystem::SetCompaction(BOOL fSet)
  2431. {
  2432. if (fSet)
  2433. m_itfsh.fFlags |= Compacting;
  2434. else
  2435. m_itfsh.fFlags &= ~Compacting;
  2436. m_fHeaderIsDirty = TRUE;
  2437. }
  2438. HRESULT CITFileSystem::CImpITFileSystem::Compact(ECompactionLev iLev)
  2439. {
  2440. IStreamITEx *pRecTblStrm = NULL;
  2441. SEntry *pRecTblCache = NULL;
  2442. CITSortRecords *pSort = NULL;
  2443. int cTblRecsInCache;
  2444. int cTblRecsTotal;
  2445. BOOL fNeedFileSort;
  2446. ULONG cRecsToRead;
  2447. ULONG ulRecNum;
  2448. HRESULT hr;
  2449. if(SUCCEEDED(hr = InitRecTable(&pRecTblStrm, &cTblRecsInCache, &cTblRecsTotal, &pRecTblCache)))
  2450. {
  2451. if(SUCCEEDED(hr = BuildUpEntryTable(&ulRecNum, pRecTblStrm, &cTblRecsInCache,
  2452. &cTblRecsTotal, pRecTblCache, &fNeedFileSort)))
  2453. {
  2454. //write the remaining buffer to the stream
  2455. if (fNeedFileSort)
  2456. {
  2457. ULONG cbWritten;
  2458. if (SUCCEEDED(hr) && SUCCEEDED(hr = pRecTblStrm->Write((LPVOID)pRecTblCache,
  2459. sizeof(SEntry)*cTblRecsInCache, &cbWritten)))
  2460. {
  2461. RonM_ASSERT(cbWritten == (sizeof(SEntry) * cTblRecsInCache));
  2462. cTblRecsInCache = 0;
  2463. }
  2464. }
  2465. #if 1
  2466. //test code
  2467. if (!fNeedFileSort)
  2468. VerifyData((LPBYTE)pRecTblCache, cTblRecsTotal, SORT_BY_ENTRYID, TRUE);
  2469. else
  2470. {
  2471. int cNumBlks = cTblRecsTotal/MAX_TABLE_RECS_INCACHE;
  2472. int cEntriesInLastBlk;
  2473. if (cEntriesInLastBlk = cTblRecsTotal % MAX_TABLE_RECS_INCACHE)
  2474. {
  2475. cNumBlks ++;
  2476. }
  2477. ULONG cbToRead, cbRead;
  2478. LARGE_INTEGER uli = CLINT(0).Li();
  2479. hr = pRecTblStrm->Seek(uli, STREAM_SEEK_SET, NULL);
  2480. for (int iCurBlk = 0; (iCurBlk < cNumBlks) && SUCCEEDED(hr); iCurBlk++)
  2481. {
  2482. //read block in sorted sequence
  2483. if (iCurBlk == (cNumBlks -1))
  2484. cbToRead = cEntriesInLastBlk * sizeof(SEntry);
  2485. else
  2486. cbToRead = MAX_TABLE_RECS_INCACHE * sizeof(SEntry);
  2487. if (SUCCEEDED(hr = pRecTblStrm->Read((LPBYTE)pRecTblCache, cbToRead, &cbRead)))
  2488. {
  2489. RonM_ASSERT(cbRead == cbToRead);
  2490. VerifyData((LPBYTE)pRecTblCache, cbToRead/sizeof(SEntry), SORT_BY_ENTRYID, iCurBlk == 0);
  2491. }
  2492. }//for
  2493. }
  2494. //end test code
  2495. #endif
  2496. ESortField eSortType = SORT_BY_OFFSET;
  2497. if (cTblRecsTotal && SUCCEEDED(hr = SortRecTable(eSortType, pRecTblStrm,
  2498. cTblRecsInCache, cTblRecsTotal, pRecTblCache, &pSort)))
  2499. {
  2500. IStreamITEx *pTempDataStrm, *pTempPDBStrm;
  2501. if (SUCCEEDED(hr = CreateTempStm(&pTempDataStrm))
  2502. && SUCCEEDED(hr = CreateTempStm(&pTempPDBStrm)))
  2503. {
  2504. cRecsToRead = MAX_TABLE_RECS_INCACHE;
  2505. CULINT ullCompactedOffset = CULINT(0);
  2506. if (fNeedFileSort)
  2507. {
  2508. BOOL fEndLoop = FALSE;
  2509. int iCurBlk = -1;
  2510. while (!fEndLoop && SUCCEEDED(hr))
  2511. {
  2512. if (SUCCEEDED(hr = pSort->ReadNextSortedBlk(&iCurBlk, (LPBYTE)pRecTblCache, &cRecsToRead, &fEndLoop)))
  2513. {
  2514. //compact data pointed by read block and update block
  2515. if (!fEndLoop && SUCCEEDED(hr = CompactData((LPBYTE)pRecTblCache, cRecsToRead, &ullCompactedOffset, pTempDataStrm)))
  2516. {
  2517. VerifyData((LPBYTE)pRecTblCache, cRecsToRead, SORT_BY_OFFSET, TRUE);
  2518. hr = pSort->WriteBlk(iCurBlk, (LPBYTE)pRecTblCache, cRecsToRead);
  2519. }
  2520. }//read next file block in sorted sequence
  2521. }//while
  2522. }
  2523. else if (SUCCEEDED(hr))
  2524. {
  2525. //compact data pointed by path entries in cached block
  2526. hr = CompactData((LPBYTE)pRecTblCache, cTblRecsInCache, &ullCompactedOffset, pTempDataStrm);
  2527. VerifyData((LPBYTE)pRecTblCache, cTblRecsInCache, SORT_BY_OFFSET, TRUE);
  2528. }
  2529. if (SUCCEEDED(hr))
  2530. {
  2531. eSortType = SORT_BY_ENTRYID;
  2532. if (SUCCEEDED(hr = SortRecTable(eSortType, pRecTblStrm,
  2533. cTblRecsInCache, cTblRecsTotal, pRecTblCache, &pSort)))
  2534. {
  2535. if (SUCCEEDED(hr = UpdatePathDB(pRecTblStrm, cTblRecsInCache,
  2536. cTblRecsTotal, pRecTblCache, pSort)))
  2537. {
  2538. //commit all the updates to the disk and reload m_pPathManager
  2539. hr = m_pPathManager->FlushToLockBytes();
  2540. //get a copy of updated path DB with or without compaction
  2541. if (SUCCEEDED(hr) && SUCCEEDED(hr = GetPathDB(pTempPDBStrm, iLev == COMPACT_DATA_AND_PATH)))
  2542. {
  2543. //putting all pieces together
  2544. //updated header, empty free list, updated path DB and compacted data
  2545. hr = CompactFileSystem(pTempPDBStrm, pTempDataStrm);
  2546. }
  2547. }
  2548. }
  2549. }
  2550. pTempDataStrm->Release();
  2551. pTempPDBStrm->Release();
  2552. }//CreateTmpStrm
  2553. }//SortRecTable
  2554. }//BuildUpEntryTable
  2555. }//InitRecTable
  2556. if (pSort)
  2557. delete pSort;
  2558. if (pRecTblStrm)
  2559. pRecTblStrm->Release();
  2560. if (pRecTblCache)
  2561. delete pRecTblCache;
  2562. return hr;
  2563. }
  2564. HRESULT __stdcall CITFileSystem::Compact(const WCHAR * pwcsName, ECompactionLev iLev)
  2565. {
  2566. CSyncWith sw(g_csITFS);
  2567. HRESULT hr;
  2568. CImpITFileSystem *pITFS = (CImpITFileSystem *)CImpITFileSystem::FindFileSystem(pwcsName);
  2569. if(!pITFS)
  2570. {
  2571. ILockBytes *pLKB = NULL;
  2572. if (SUCCEEDED(hr = CFSLockBytes::Open(NULL, pwcsName,
  2573. STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
  2574. &pLKB
  2575. )))
  2576. {
  2577. CITFileSystem *pITFSout = New CITFileSystem(NULL);
  2578. if (!pITFSout)
  2579. {
  2580. return STG_E_INSUFFICIENTMEMORY;
  2581. }
  2582. pITFS = &(pITFSout->m_ImpITFileSystem);
  2583. pITFS->AddRef();
  2584. pITFS->AddRef();
  2585. //To match reference count trick in InitOpenOnLockBytes
  2586. pITFSout->AddRef();
  2587. hr = pITFS->InitOpenOnLockBytes(pLKB, STGM_READWRITE | STGM_SHARE_EXCLUSIVE);
  2588. pLKB->Release();
  2589. pITFSout->Release();
  2590. pITFS->SetCompaction(TRUE);
  2591. }
  2592. }
  2593. else
  2594. {
  2595. pITFS->Release();
  2596. return E_FAIL;
  2597. }
  2598. if (SUCCEEDED(hr))
  2599. {
  2600. hr = pITFS->Compact(iLev);
  2601. }
  2602. if (pITFS)
  2603. {
  2604. pITFS->SetCompaction(FALSE);
  2605. pITFS->Release();
  2606. pITFS->Release();
  2607. }
  2608. return hr;
  2609. }
  2610. /*
  2611. HRESULT CITFileSystem::CImpITFileSystem::SaveTransformedSpace(IStreamITEx **ppTempStrm, ULARGE_INTEGER *pcbSaved)
  2612. {
  2613. HRESULT hr = NO_ERROR;
  2614. WCHAR awszStreamPath[256];
  2615. //Copy tranformed space to a temporary stream
  2616. wcsCpy(awszStreamPath, pwcsSpaceNameStorage);
  2617. wcsCat(awszStreamPath, pwcsLZXSpace);
  2618. wcsCat(awszStreamPath, pwcsSpaceContentSuffix);
  2619. *pcbSaved = CULINT(0).Uli();
  2620. *ppTempStrm = NULL;
  2621. IStreamITEx *pXStream;
  2622. if (SUCCEEDED(hr = OpenStream(NULL, awszStreamPath, 0, &pXStream)))
  2623. {
  2624. STATSTG statstg;
  2625. if (SUCCEEDED(hr = pXStream->Stat(&statstg, STATFLAG_NONAME)))
  2626. {
  2627. *pcbSaved = statstg.cbSize;
  2628. if (SUCCEEDED(hr = CreateTempStm(ppTempStrm)))
  2629. {
  2630. ULARGE_INTEGER cbRead, cbWritten;
  2631. if (SUCCEEDED(hr = pXStream->CopyTo(*ppTempStrm, statstg.cbSize, &cbRead, &cbWritten)))
  2632. {
  2633. RonM_ASSERT(CULINT(cbRead) == CULINT(cbWritten));
  2634. }//CopyTo
  2635. }//CreateTempStm
  2636. }//Stat
  2637. pXStream->Release();
  2638. }//OpenStream
  2639. return hr;
  2640. }
  2641. */
  2642. HRESULT CITFileSystem::CImpITFileSystem::DumpStream(IStreamITEx *pTempStrm, LPSTR pFileName)
  2643. {
  2644. HRESULT hr = NO_ERROR;
  2645. #if 0 //test code
  2646. FILE *file = fopen(pFileName, "w" );
  2647. if (file != NULL)
  2648. {
  2649. RonM_ASSERT(pTempStrm != NULL);
  2650. HRESULT hr = NO_ERROR;
  2651. BYTE lpBuf[2048];
  2652. if (SUCCEEDED(hr = pTempStrm->Seek(CLINT(0).Li(), STREAM_SEEK_SET, 0)))
  2653. {
  2654. ULONG cbToRead = sizeof(lpBuf);
  2655. ULONG cbWritten;
  2656. ULONG cbRead = cbToRead;
  2657. while (SUCCEEDED(hr) && (cbRead == cbToRead))
  2658. {
  2659. if (SUCCEEDED(hr = pTempStrm->Read(lpBuf, cbToRead, &cbRead)))
  2660. {
  2661. cbWritten = fwrite(lpBuf, sizeof(BYTE), cbRead, file);
  2662. RonM_ASSERT(cbRead == cbWritten);
  2663. if (cbRead != cbWritten)
  2664. hr = E_FAIL;
  2665. }//ReadAt
  2666. }//while
  2667. }//seek
  2668. fclose(file);
  2669. }
  2670. else
  2671. hr = E_FAIL;
  2672. #endif //end test code
  2673. return hr;
  2674. }
  2675. HRESULT CITFileSystem::CImpITFileSystem::CopyStream(IStreamITEx *pTempStrm, CULINT *pullCompactedOffset)
  2676. {
  2677. RonM_ASSERT(pTempStrm != NULL);
  2678. HRESULT hr = NO_ERROR;
  2679. BYTE lpBuf[2048];
  2680. if (SUCCEEDED(hr = pTempStrm->Seek(CLINT(0).Li(), STREAM_SEEK_SET, 0)))
  2681. {
  2682. ULONG cbToRead = sizeof(lpBuf);
  2683. ULONG cbWritten;
  2684. ULONG cbRead = cbToRead;
  2685. while (SUCCEEDED(hr) && (cbRead == cbToRead))
  2686. {
  2687. if (SUCCEEDED(hr = pTempStrm->Read(lpBuf, cbToRead, &cbRead)))
  2688. {
  2689. if (SUCCEEDED(hr = m_pLKBMedium->WriteAt((*pullCompactedOffset).Uli(), lpBuf, cbRead, &cbWritten)))
  2690. {
  2691. RonM_ASSERT(cbRead == cbWritten);
  2692. *pullCompactedOffset += cbWritten;
  2693. }//WriteAt
  2694. }//ReadAt
  2695. }//while
  2696. }//seek
  2697. return hr;
  2698. }
  2699. HRESULT CITFileSystem::CImpITFileSystem:: CompactFileSystem(IStreamITEx *pTempPDBStrm, IStreamITEx *pTempDataStrm)
  2700. {
  2701. STATSTG statstg1, statstg2;
  2702. HRESULT hr;
  2703. if (SUCCEEDED(hr = pTempPDBStrm->Stat(&statstg1, STATFLAG_NONAME))
  2704. && SUCCEEDED(hr = pTempDataStrm->Stat(&statstg2, STATFLAG_NONAME)))
  2705. {
  2706. ULONG cbRead = 0;
  2707. ULARGE_INTEGER cbFreeListSize;
  2708. m_pFreeListManager->SetFreeListEmpty();
  2709. m_pFreeListManager->GetSizeMax(&cbFreeListSize);
  2710. //upgrade the version no.
  2711. m_itfsh.uFormatVersion = CurrentFileFormatVersion;
  2712. m_itfsh.cbHeaderSize = sizeof(m_itfsh);
  2713. m_itfsh.offFreeListData = m_itfsh.cbHeaderSize;
  2714. m_itfsh.cbFreeListData = cbFreeListSize;
  2715. m_itfsh.offPathMgrData = m_itfsh.offFreeListData + m_itfsh.cbFreeListData;
  2716. m_itfsh.cbPathMgrData = CULINT(statstg1.cbSize);
  2717. m_itfsh.offPathMgrOrigin = m_itfsh.offPathMgrData + m_itfsh.cbPathMgrData;
  2718. if (SUCCEEDED(hr = m_pLKBMedium->WriteAt(CULINT(0).Uli(), &m_itfsh, sizeof(m_itfsh), &cbRead)))
  2719. {
  2720. //write free list
  2721. ULARGE_INTEGER cbSpaceSize = (m_itfsh.cbHeaderSize + CULINT(cbFreeListSize)
  2722. + CULINT(statstg1.cbSize) + CULINT(statstg2.cbSize)).Uli();
  2723. m_pFreeListManager->SetSpaceSize(cbSpaceSize);
  2724. hr = m_pFreeListManager->RecordFreeList();
  2725. RonM_ASSERT(hr == S_OK);
  2726. if (SUCCEEDED(hr))
  2727. {
  2728. m_pPathManager->ForceClearDirty();
  2729. #if 0 //test code
  2730. hr = DumpStream(pTempPDBStrm, "c:\\test.PDB");
  2731. hr = DumpStream(pTempDataStrm, "c:\\test.DAT");
  2732. #endif//end test code
  2733. CULINT ullOffset = m_itfsh.offPathMgrData;
  2734. if (SUCCEEDED(hr = CopyStream(pTempPDBStrm, &ullOffset)))
  2735. {
  2736. RonM_ASSERT((ullOffset - m_itfsh.offPathMgrData) == CULINT(statstg1.cbSize));
  2737. ullOffset = m_itfsh.offPathMgrOrigin;
  2738. if (SUCCEEDED(hr = CopyStream(pTempDataStrm, &ullOffset)))
  2739. {
  2740. RonM_ASSERT((ullOffset - m_itfsh.offPathMgrOrigin) == CULINT(statstg2.cbSize));
  2741. hr = m_pLKBMedium->SetSize(cbSpaceSize);
  2742. }
  2743. }//copying temp path DB stream
  2744. }//copying free list
  2745. }//copying header
  2746. }//getting size of path database
  2747. return hr;
  2748. }
  2749. HRESULT CITFileSystem::CImpITFileSystem::GetPathDB(IStreamITEx *pTempPDBStrm, BOOL fCompact)
  2750. {
  2751. return m_pPathManager->GetPathDB(pTempPDBStrm, fCompact);
  2752. }
  2753. HRESULT CITFileSystem::CImpITFileSystem::ForceClearDirty()
  2754. {
  2755. return m_pPathManager->ForceClearDirty();
  2756. }
  2757. HRESULT CITFileSystem::CImpITFileSystem::CompactData(LPBYTE pRecTableCache, ULONG cEntries, CULINT *pullCompactedOffset, IStreamITEx *pTempDataStrm)
  2758. {
  2759. SEntry *pe;
  2760. BYTE lpBuf[4096];
  2761. HRESULT hr = NO_ERROR;
  2762. ULONG cEntriesCompacted = 0, iEntry = 0;
  2763. CULINT cbToReadTotal;
  2764. ULONG ceFollowing;
  2765. ULARGE_INTEGER uli;
  2766. SEntry *pe1, *pe2;
  2767. while (iEntry < cEntries)
  2768. {
  2769. //Finding all the consequtive compacted entries
  2770. BOOL fDone = FALSE;
  2771. ceFollowing = 0;
  2772. pe1 = (SEntry *)(pRecTableCache + iEntry * sizeof(SEntry));
  2773. cbToReadTotal = pe1->ullcbData;
  2774. while (!fDone)
  2775. {
  2776. pe2 = (SEntry *)(pRecTableCache + (iEntry + ceFollowing + 1) * sizeof(SEntry));
  2777. if (pe2->ullcbOffset == (pe1->ullcbOffset + pe1->ullcbData))
  2778. {
  2779. ceFollowing++;
  2780. cbToReadTotal += pe2->ullcbData;
  2781. pe1 = pe2;
  2782. }
  2783. else
  2784. fDone = TRUE;
  2785. }
  2786. pe = (SEntry *)(pRecTableCache + iEntry * sizeof(SEntry));
  2787. uli = (pe->ullcbOffset + m_itfsh.offPathMgrOrigin).Uli();
  2788. RonM_ASSERT(*pullCompactedOffset <= CULINT(uli));
  2789. ULONG cbToRead;
  2790. ULONG cbReadTotal = 0;
  2791. ULONG cbWritten;
  2792. ULONG cbRead;
  2793. while ((cbReadTotal != cbToReadTotal) && SUCCEEDED(hr))
  2794. {
  2795. cbToRead = (long)(cbToReadTotal - CULINT(cbReadTotal)).Ull();
  2796. if (cbToRead >= sizeof(lpBuf))
  2797. cbToRead = sizeof(lpBuf);
  2798. if (SUCCEEDED(hr = m_pLKBMedium->ReadAt(uli, lpBuf, cbToRead, &cbRead)))
  2799. {
  2800. RonM_ASSERT(cbRead == cbToRead);
  2801. if (SUCCEEDED(hr = pTempDataStrm->Write(lpBuf, cbRead, &cbWritten)))
  2802. {
  2803. RonM_ASSERT(cbRead == cbWritten);
  2804. cbReadTotal += cbToRead;
  2805. uli = (CULINT(uli) + cbWritten).Uli();
  2806. }//WriteAt
  2807. }//ReadAt
  2808. }//while
  2809. RonM_ASSERT(cbReadTotal == cbToReadTotal);
  2810. pe->ullcbOffset = *pullCompactedOffset;
  2811. memCpy(pRecTableCache + iEntry * sizeof(SEntry), pe, sizeof(SEntry));
  2812. *pullCompactedOffset += cbToReadTotal;
  2813. SEntry *pePrev = pe;
  2814. //modify the path info for following compacted entries
  2815. for (int i = iEntry + 1; i < (iEntry + ceFollowing + 1); i++)
  2816. {
  2817. pe = (SEntry *)(pRecTableCache + i * sizeof(SEntry));
  2818. pe->ullcbOffset = pePrev->ullcbOffset + pePrev->ullcbData;
  2819. memCpy(pRecTableCache + i * sizeof(SEntry), pe, sizeof(SEntry));
  2820. pePrev = pe;
  2821. }
  2822. iEntry += (ceFollowing + 1);
  2823. }//while
  2824. return hr;
  2825. }
  2826. void CITFileSystem::CImpITFileSystem::VerifyData(LPBYTE pRecTableCache, ULONG cEntries, ESortField eSType, BOOL fReset)
  2827. {
  2828. #ifdef _DEBUG
  2829. static int cEntry;
  2830. static CULINT ullLastEntryOffset = CULINT(0);
  2831. static UINT ulLastEntryID = 0;
  2832. if (fReset)
  2833. {
  2834. cEntry = 0;
  2835. ullLastEntryOffset = CULINT(0);
  2836. ulLastEntryID = 0;
  2837. }
  2838. //printf("Verifying original entries from file After Sort\n");
  2839. for (int iEntry = 0; iEntry < cEntries; iEntry++, cEntry++)
  2840. {
  2841. SEntry *pe = (SEntry *)(pRecTableCache + iEntry * sizeof(SEntry));
  2842. //printf("Entry[%d] offset= %d, entryid = %d, size = %d\n",
  2843. // cEntry, (int)pe->ullcbOffset.Ull(), (int)pe->ulEntryID, (int)pe->ullcbData.Ull());
  2844. //RonM_ASSERT(cEntry == pe->ulEntryID);
  2845. if (eSType == SORT_BY_OFFSET)
  2846. RonM_ASSERT(pe->ullcbOffset >= ullLastEntryOffset);
  2847. else
  2848. RonM_ASSERT(pe->ulEntryID >= ulLastEntryID);
  2849. ullLastEntryOffset = pe->ullcbOffset;
  2850. ulLastEntryID = pe->ulEntryID;
  2851. }
  2852. //printf("** End Verifying original entries **\n");
  2853. #endif
  2854. }
  2855. HRESULT STDMETHODCALLTYPE CITFileSystem::CImpITFileSystem::CEnumFSItems::CImpIEnumSTATSTG::GetNextEntryInSeq
  2856. (ULONG celt, PathInfo *rgelt, ULONG *pceltFetched)
  2857. {
  2858. return E_NOTIMPL;
  2859. }
  2860. HRESULT STDMETHODCALLTYPE CITFileSystem::CImpITFileSystem::CEnumFSItems::CImpIEnumSTATSTG::GetFirstEntryInSeq
  2861. (PathInfo *rgelt)
  2862. {
  2863. return E_NOTIMPL;
  2864. }