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.

507 lines
19 KiB

  1. // ITSFS.H -- Declaration of the CITFileSystem class
  2. #ifndef __ITSFS_H__
  3. #define __ITSFS_H__
  4. const UINT MAGIC_ITS_FILE = 'I' | ('T' << 8) | ('S' << 16) | ('F' << 24);
  5. const UINT FirstReleasedVersion = 2;
  6. const UINT RelativeOffsetVersion = 3;
  7. const UINT CurrentFileFormatVersion = 3;
  8. #define MAX_TABLE_RECS_INCACHE 1024
  9. typedef struct _ITSFileHeaderV2
  10. {
  11. UINT uMagic; // Should be set to MAGIC_ITS_FILE;
  12. UINT uFormatVersion; // Version number for file format
  13. UINT cbHeaderSize; // Size of this header in bytes.
  14. UINT fFlags; // State flag bits for the ITS file.
  15. DWORD dwStamp; // Content Version stamp; changes on every write.
  16. LCID lcid; // Locale Id for file.
  17. CLSID clsidFreeList; // Class id for the free list manager interface used in the file
  18. CLSID clsidPathMgr; // Class id for the path manager interface used in the file
  19. CULINT offFreeListData; // Offset in bytes to free list data
  20. CULINT cbFreeListData; // Size of free list data in bytes
  21. CULINT offPathMgrData; // Offset in bytes to Path Manager data
  22. CULINT cbPathMgrData; // Size of Path Manager data in bytes
  23. } ITSFileHeaderV2, *PITSFileHeaderV2;
  24. // In the ITSFileHeader structure each items is aligned on a natural boundary.
  25. // That is WCHARs are on 2-byte boundarides, UINTs are on 4-byte boundaries,
  26. // CULINTs are on 8-byte boundaries, and CLSIDs are on 16-byte boundaries.
  27. // Keeping them that way will avoid any alignment problems on RISC machines.
  28. typedef struct _ITSFileHeader
  29. {
  30. UINT uMagic; // Should be set to MAGIC_ITS_FILE;
  31. UINT uFormatVersion; // Version number for file format
  32. UINT cbHeaderSize; // Size of this header in bytes.
  33. UINT fFlags; // State flag bits for the ITS file.
  34. DWORD dwStamp; // Content Version stamp; changes on every write.
  35. LCID lcid; // Locale Id for file.
  36. CLSID clsidFreeList; // Class id for the free list manager interface used in the file
  37. CLSID clsidPathMgr; // Class id for the path manager interface used in the file
  38. CULINT offFreeListData; // Offset in bytes to free list data
  39. CULINT cbFreeListData; // Size of free list data in bytes
  40. CULINT offPathMgrData; // Offset in bytes to Path Manager data
  41. CULINT cbPathMgrData; // Size of Path Manager data in bytes
  42. CULINT offPathMgrOrigin;// Coordinate origin offset wrapped around path manager offsets
  43. } ITSFileHeader, *PITSFileHeader;
  44. const DWORD ITSFS_CONTROL_VERSION = 1;
  45. const DWORD DEFAULT_DIR_BLOCK_SIZE = 8192;
  46. const DWORD DEFAULT_MIN_CACHE_ENTRIES = 20;
  47. // Bit flag definitions for ITSFileHeader::fFlags
  48. enum { Compacting = 0x80, // file is being compacted
  49. VALID_OPEN_FLAGS = fDefaultIsCompression // valid flag set when opening a file.
  50. };
  51. class CITFileSystem : public CITUnknown
  52. {
  53. public:
  54. // Creators:
  55. static HRESULT __stdcall CreateITFileSystem(IUnknown *punkOuter, const WCHAR * pwcsName,
  56. DWORD grfMode,
  57. PITS_Control_Data pControlData,
  58. LCID lcid,
  59. IStorage ** ppstgOpen
  60. );
  61. static HRESULT __stdcall CreateITFSOnLockBytes(IUnknown *punkOuter, ILockBytes * pLKB,
  62. DWORD grfMode,
  63. PITS_Control_Data pControlData,
  64. LCID lcid,
  65. IStorage ** ppstgOpen
  66. );
  67. static HRESULT __stdcall QueryFileStampAndLocale
  68. (const WCHAR *pwcsName, DWORD *pFileStamp, DWORD *pFileLocale);
  69. static HRESULT __stdcall QueryLockByteStampAndLocale
  70. (ILockBytes * plkbyt, DWORD *pFileStamp, DWORD *pFileLocale);
  71. static HRESULT __stdcall IsITFile(const WCHAR * pwcsName);
  72. static HRESULT __stdcall IsITLockBytes(ILockBytes * plkbyt);
  73. static HRESULT __stdcall OpenITFileSystem(IUnknown *punkOuter, const WCHAR * pwcsName,
  74. DWORD grfMode,
  75. IStorageITEx ** ppstgOpen
  76. );
  77. static HRESULT __stdcall OpenITFSOnLockBytes(IUnknown *punkOuter, ILockBytes * pLKB,
  78. DWORD grfMode,
  79. IStorageITEx ** ppstgOpen
  80. );
  81. static HRESULT __stdcall SetITFSTimes(WCHAR const * pwcsName, FILETIME const * pctime,
  82. FILETIME const * patime, FILETIME const * pmtime
  83. );
  84. static HRESULT __stdcall DefaultControlData(PITS_Control_Data *ppControlData);
  85. static HRESULT __stdcall Compact(const WCHAR * pwcsName, ECompactionLev iLev);
  86. // Destructor:
  87. ~CITFileSystem();
  88. private:
  89. CITFileSystem(IUnknown *pUnkOuter);
  90. class CImpITFileSystem : public IITFileSystem
  91. {
  92. public:
  93. // Constructor:
  94. CImpITFileSystem(CITFileSystem *pITFileSystem, IUnknown *punkOuter);
  95. // Destructor:
  96. ~CImpITFileSystem(void);
  97. // Finding an active file system:
  98. static IITFileSystem *FindFileSystem(const WCHAR *pwcsPath);
  99. IStorageITEx *FindActiveStorage (const WCHAR *pwcsPath);
  100. ILockBytes *FindActiveLockBytes(const WCHAR *pwcsPath);
  101. // Initialers:
  102. HRESULT __stdcall InitCreateOnLockBytes(ILockBytes * pLKB, DWORD grfMode,
  103. PITS_Control_Data pControlData, LCID lcid
  104. );
  105. HRESULT __stdcall InitOpenOnLockBytes(ILockBytes * pLKB, DWORD grfMode);
  106. //garbage collection routines
  107. HRESULT GetFirstRecord(SEntry *prec,
  108. IStreamITEx *pRecTblStrm,
  109. int cTblRecsInCache,
  110. int cTblRecsTotal,
  111. SEntry *pRecTblCache);
  112. HRESULT GetNextRecord(ULONG ulCurRec,
  113. SEntry *prec,
  114. IStreamITEx *pRecTblStrm,
  115. int cTblRecsInCache,
  116. int cTblRecsTotal,
  117. SEntry *pRecTblCache);
  118. HRESULT SortRecTable(ESortField eSField,
  119. IStreamITEx *pRecTblStrm,
  120. int cTblRecsInCache,
  121. int cTblRecsTotal,
  122. SEntry *pRecTblCache,
  123. CITSortRecords **ppSort);
  124. HRESULT AppendToRecTbl(SEntry *prec,
  125. IStreamITEx *pRecTblStrm,
  126. int *pcTblRecsInCache,
  127. int *pcTblRecsTotal,
  128. SEntry *pRecTblCache);
  129. static int CompareEntries(SEntry e1, SEntry e2, ESortField eSField);
  130. HRESULT CreateTempStm(IStreamITEx **ppRecTblStrm);
  131. HRESULT InitRecTable( IStreamITEx **ppRecTblStrm,
  132. int *pcTblRecsInCache,
  133. int *pcTblRecsTotal,
  134. SEntry **ppRecTblCache);
  135. HRESULT BuildUpEntryTable(ULONG *pulRecNum,
  136. IStreamITEx *pRecTblStrm,
  137. int *pcTblRecsInCache,
  138. int *pcTblRecsTotal,
  139. SEntry *pRecTblCache,
  140. BOOL *pfNeedFileSort);
  141. HRESULT CompactData(LPBYTE pRecTableCache, ULONG cEntries, CULINT *pullCompactedOffset, IStreamITEx *pTempDataStrm);
  142. HRESULT UpdatePathDB(IStreamITEx *pRecTblStrm, int cTblRecsInCache,
  143. int cTblRecsTotal, SEntry *pRecTblCache, CITSortRecords *pSort);
  144. HRESULT GetPathDB(IStreamITEx *pTempPDBStrm, BOOL fCompact);
  145. HRESULT ForceClearDirty();
  146. void SetCompaction(BOOL fSet);
  147. HRESULT Compact(ECompactionLev iLev);
  148. HRESULT CompactFileSystem(IStreamITEx *pTempPDBStrm, IStreamITEx *pTempDataStrm);
  149. HRESULT CopyStream(IStreamITEx *pTempStrm, CULINT *pullCompactedOffset);
  150. void VerifyData(LPBYTE pRecTableCache, ULONG cEntries, ESortField eSType, BOOL fReset);
  151. HRESULT DumpStream(IStreamITEx *pTempStrm, LPSTR pFileName);
  152. // IUnknown methods:
  153. STDMETHODIMP_(ULONG) Release(void);
  154. // IITFileSystem methods:
  155. HRESULT __stdcall DeactivateSpace(UINT iSpace);
  156. CITCriticalSection& CriticalSection();
  157. HRESULT __stdcall FlushToLockBytes();
  158. HRESULT __stdcall CreateStorage (IUnknown *pUnkOuter, const WCHAR *pwcsPathPrefix,
  159. DWORD grfMode, IStorageITEx **ppStg);
  160. HRESULT __stdcall OpenStorage (IUnknown *pUnkOuter, const WCHAR *pwcsPath,
  161. DWORD grfMode, IStorageITEx **ppstg);
  162. HRESULT __stdcall CreateLockBytes(IUnknown *pUnkOuter, const WCHAR *pwcsPath,
  163. const WCHAR *pwcsDataSpaceName,
  164. BOOL fOverwrite, ILockBytes **ppLKB);
  165. HRESULT __stdcall OpenLockBytes(IUnknown *pUnkOuter, const WCHAR *pwcsPath,
  166. ILockBytes **ppLKB);
  167. HRESULT __stdcall CreateStream(IUnknown *pUnkOuter, const WCHAR *pwcsPath,
  168. DWORD grfMode, IStreamITEx **ppStrm);
  169. HRESULT __stdcall CreateStream
  170. (IUnknown *pUnkOuter, const WCHAR * pwcsName, const WCHAR *pwcsDataSpaceName,
  171. DWORD grfMode, IStreamITEx ** ppstm
  172. );
  173. HRESULT __stdcall OpenStream(IUnknown *pUnkOuter, const WCHAR *pwcsPath,
  174. DWORD grfMode, IStreamITEx **ppStream);
  175. HRESULT __stdcall ConnectStorage(CImpITUnknown *pStg);
  176. HRESULT __stdcall ConnectLockBytes(CImpITUnknown *pStg);
  177. HRESULT __stdcall DeleteItem(WCHAR const *pwcsName);
  178. HRESULT __stdcall RenameItem(WCHAR const *pwcsOldName, WCHAR const *pwcsNewName);
  179. HRESULT __stdcall UpdatePathInfo(PathInfo *pPathInfo);
  180. HRESULT __stdcall SetITFSTimes(FILETIME const * pctime,
  181. FILETIME const * patime,
  182. FILETIME const * pmtime
  183. );
  184. HRESULT __stdcall GetITFSTimes(FILETIME * pctime,
  185. FILETIME * patime,
  186. FILETIME * pmtime
  187. );
  188. HRESULT __stdcall ReallocEntry(PathInfo *pPathInfo, CULINT ullcbNew,
  189. BOOL fCopyContent
  190. );
  191. HRESULT __stdcall ReallocInPlace(PathInfo *pPathInfo, CULINT ullcbNew);
  192. HRESULT __stdcall EnumeratePaths(WCHAR const *pwcsPathPrefix,
  193. IEnumSTATSTG **ppEnumStatStg
  194. );
  195. HRESULT __stdcall IsWriteable();
  196. HRESULT __stdcall FSObjectReleased();
  197. BOOL __stdcall IsCompacting() { return (m_itfsh.fFlags & Compacting);};
  198. HRESULT __stdcall QueryFileStampAndLocale(DWORD *pFileStamp, DWORD *pFileLocale);
  199. HRESULT __stdcall CountWrites();
  200. private:
  201. class CSystemPathManager : public CITUnknown
  202. {
  203. public:
  204. static HRESULT Create(IUnknown *punkOuter, CImpITFileSystem *pITFS,
  205. IITPathManager **ppPathMgr
  206. );
  207. ~CSystemPathManager();
  208. class CImpIPathManager : public IITPathManager
  209. {
  210. public:
  211. CImpIPathManager(CSystemPathManager *pBackObj, IUnknown *punkOuter);
  212. ~CImpIPathManager();
  213. HRESULT STDMETHODCALLTYPE Init(CImpITFileSystem *pITFS);
  214. // IPersist Method:
  215. HRESULT STDMETHODCALLTYPE GetClassID(
  216. /* [out] */ CLSID __RPC_FAR *pClassID);
  217. // IITPathManager interfaces:
  218. HRESULT STDMETHODCALLTYPE FlushToLockBytes();
  219. HRESULT STDMETHODCALLTYPE FindEntry (PPathInfo pPI );
  220. HRESULT STDMETHODCALLTYPE CreateEntry(PPathInfo pPINew,
  221. PPathInfo pPIOld,
  222. BOOL fReplace );
  223. HRESULT STDMETHODCALLTYPE DeleteEntry(PPathInfo pPI );
  224. HRESULT STDMETHODCALLTYPE UpdateEntry(PPathInfo pPI );
  225. HRESULT STDMETHODCALLTYPE EnumFromObject(IUnknown *punkOuter,
  226. const WCHAR *pwszPrefix,
  227. UINT cwcPrefix,
  228. REFIID riid,
  229. PVOID *ppv
  230. );
  231. HRESULT STDMETHODCALLTYPE GetPathDB(IStreamITEx *pTempPDBStrm, BOOL fCompact);
  232. HRESULT STDMETHODCALLTYPE ForceClearDirty();
  233. private:
  234. CImpITFileSystem *m_pIITFS;
  235. };
  236. private:
  237. CSystemPathManager(IUnknown *pUnkOuter);
  238. CImpIPathManager m_PathManager;
  239. };
  240. friend CSystemPathManager::CImpIPathManager;
  241. class CEnumFSItems : public CITUnknown
  242. {
  243. public:
  244. static HRESULT NewFSEnumerator(CImpITFileSystem *pITFS,
  245. const WCHAR *pwszPathPrefix,
  246. UINT cwcPathPrefix,
  247. IEnumSTATSTG **ppEnumSTATSTG
  248. );
  249. ~CEnumFSItems();
  250. class CImpIEnumSTATSTG : public IITEnumSTATSTG
  251. {
  252. friend CEnumFSItems;
  253. public:
  254. CImpIEnumSTATSTG(CEnumFSItems *pBackObj, IUnknown *punkOuter);
  255. ~CImpIEnumSTATSTG();
  256. HRESULT InitFSEnumerator(CImpITFileSystem *pITFS,
  257. const WCHAR *pwszPathPrefix,
  258. UINT cwcPathPrefix
  259. );
  260. HRESULT InitNewCloneOf(CImpIEnumSTATSTG *pImpEnumFS);
  261. HRESULT STDMETHODCALLTYPE GetNextEntryInSeq(ULONG celt, PathInfo *rgelt, ULONG *pceltFetched);
  262. HRESULT STDMETHODCALLTYPE GetFirstEntryInSeq(PathInfo *rgelt);
  263. HRESULT STDMETHODCALLTYPE Next(
  264. /* [in] */ ULONG celt,
  265. /* [in] */ STATSTG __RPC_FAR *rgelt,
  266. /* [out] */ ULONG __RPC_FAR *pceltFetched);
  267. HRESULT STDMETHODCALLTYPE Skip(
  268. /* [in] */ ULONG celt);
  269. HRESULT STDMETHODCALLTYPE Reset( void);
  270. HRESULT STDMETHODCALLTYPE Clone(
  271. /* [out] */ IEnumSTATSTG __RPC_FAR *__RPC_FAR *ppenum);
  272. private:
  273. IEnumSTATSTG *m_pEnumPathMgr;
  274. CImpITFileSystem *m_pITFS;
  275. };
  276. static HRESULT NewCloneOf(CImpIEnumSTATSTG *pImpEnumFS,
  277. IEnumSTATSTG **ppEnumSTATSTG
  278. );
  279. private:
  280. CEnumFSItems(IUnknown *pUnkOuter);
  281. CImpIEnumSTATSTG m_ImpEnumSTATSTG;
  282. };
  283. friend CEnumFSItems;
  284. friend CEnumFSItems::CImpIEnumSTATSTG;
  285. enum { PENDING_CHANGE_LIMIT = 100 };
  286. void CopyPath(PathInfo &PI, const WCHAR *pwcsPath);
  287. IITPathManager *PathMgr(PathInfo *pPathInfo);
  288. HRESULT CreateSpaceNameList();
  289. HRESULT OpenSpaceNameList();
  290. HRESULT FlushSpaceNameList();
  291. HRESULT FindSpaceName(const WCHAR *pwcsSpaceName);
  292. HRESULT AddSpaceName(const WCHAR *pwcsSpaceName);
  293. HRESULT DeleteSpaceName(const WCHAR *pwcsSpaceName);
  294. HRESULT ActivateDataSpace(ULONG iSpace);
  295. HRESULT DeactivateDataSpace(ULONG iSpace);
  296. HRESULT CreateDefaultDataSpace(PITSFS_Control_Data pITCD);
  297. HRESULT WriteToStream(const WCHAR *pwcsStreamPath, PVOID pvData, ULONG cbData);
  298. HRESULT CreateTransformedLockBytes
  299. (IUnknown *pUnkOuter, const WCHAR *pwcsPath,
  300. const WCHAR *pwcsDataSpaceName,
  301. BOOL fOverwrite, ILockBytes **ppLKB
  302. );
  303. HRESULT OpenTransformedLockbytes(PathInfo *pPI, ILockBytes **ppLKB);
  304. HRESULT __stdcall LookForActivity(WCHAR const *pwcsName, IEnumSTATSTG *pEnum);
  305. enum { MAX_SPACES = 256 };
  306. ITSFileHeader m_itfsh;
  307. CITCriticalSection m_cs;
  308. UINT m_StartingFileStamp;
  309. ILockBytes *m_pLKBMedium;
  310. IITPathManager *m_pPathManager;
  311. IITPathManager *m_pSysPathManager;
  312. IITFreeList *m_pFreeListManager;
  313. ITransformServices *m_pTransformServices;
  314. CImpITUnknown *m_pActiveStorageList;
  315. CImpITUnknown *m_pActiveLockBytesList;
  316. BOOL m_fHeaderIsDirty;
  317. BOOL m_fInitialed;
  318. BOOL m_fReadOnly;
  319. BOOL m_fDefaultIsCompression;
  320. WCHAR *m_pwscDataSpaceNames;
  321. TransformDescriptor **m_papTransformDescriptors;
  322. IStreamITEx *m_pStrmSpaceNames;
  323. UINT m_cFSObjectRefs; // Count of circular refs through
  324. // ":" storages and streams.
  325. UINT m_cwcFileName;
  326. WCHAR m_awszFileName[MAX_PATH];
  327. };
  328. CImpITFileSystem m_ImpITFileSystem;
  329. };
  330. inline CITFileSystem::CITFileSystem(IUnknown *pUnkOuter)
  331. : m_ImpITFileSystem(this, pUnkOuter),
  332. CITUnknown(&IID_ITFileSystem, 1, &m_ImpITFileSystem)
  333. {
  334. }
  335. inline CITFileSystem::~CITFileSystem() { }
  336. inline CITFileSystem::CImpITFileSystem::CSystemPathManager::CSystemPathManager(IUnknown *pUnkOuter)
  337. : m_PathManager(this, pUnkOuter),
  338. CITUnknown(aIID_CPathManager, cInterfaces_CPathManager, &m_PathManager)
  339. {
  340. }
  341. inline CITFileSystem::CImpITFileSystem::CSystemPathManager::~CSystemPathManager(void)
  342. {
  343. }
  344. inline CITFileSystem::CImpITFileSystem::CSystemPathManager::CImpIPathManager::~CImpIPathManager()
  345. {
  346. }
  347. inline CITFileSystem::CImpITFileSystem::CEnumFSItems::CEnumFSItems(IUnknown *pUnkOuter)
  348. : m_ImpEnumSTATSTG(this, pUnkOuter),
  349. CITUnknown(&IID_IEnumSTATSTG, 1, &m_ImpEnumSTATSTG)
  350. {
  351. }
  352. inline CITFileSystem::CImpITFileSystem::CEnumFSItems::~CEnumFSItems(void)
  353. {
  354. }
  355. extern const WCHAR *pwscSpaceNameListStream;
  356. extern const WCHAR *pwcsSpaceNameStorage;
  357. extern const WCHAR *pwcsSpaceContentSuffix;
  358. extern const WCHAR *pwcsTransformListSuffix;
  359. extern const WCHAR *pwcsSpanInfoSuffix;
  360. extern const WCHAR *pwcsTransformSubStorage;
  361. extern const WCHAR *pwcsControlDataSuffix;
  362. extern const WCHAR *pwcsInstanceSubStorage;
  363. extern const WCHAR *pwcsTransformStorage;
  364. extern const WCHAR *pwcsUncompressedSpace;
  365. extern const WCHAR *pwcsLZXSpace;
  366. #endif // __ITSFS_H__