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.

227 lines
3.9 KiB

  1. /*++
  2. FCACHE.H
  3. This file defines the interface for the file handle cache !
  4. --*/
  5. #ifndef _FCACHE_H_
  6. #define _FCACHE_H_
  7. #include "smartptr.h"
  8. #ifdef _USE_RWNH_
  9. #include "rwnew.h"
  10. #else
  11. #include "rw.h"
  12. #endif
  13. class CFileCacheKey {
  14. public:
  15. DWORD m_cbPathLength ;
  16. LPCSTR m_lpstrPath ;
  17. CFileCacheKey( LPCSTR lpstr, DWORD cb ) :
  18. m_lpstrPath( lpstr ), m_cbPathLength( cb ) {}
  19. } ;
  20. #define FILECACHE_MAX_PATH 768
  21. class CFileCacheObject : public CRefCount {
  22. private :
  23. char m_szPath[FILECACHE_MAX_PATH] ;
  24. CFileCacheKey m_key ;
  25. HANDLE m_hTokenOpeningUser ;
  26. HANDLE m_hFile ;
  27. BY_HANDLE_FILE_INFORMATION m_FileInfo ;
  28. PSECURITY_DESCRIPTOR m_pSecDesc ;
  29. DWORD m_cbDesc ;
  30. #ifndef _USE_RWNH_
  31. class CShareLock& m_Lock ;
  32. #else
  33. class CShareLockNH m_Lock ;
  34. #endif
  35. //
  36. // These constructors are private as we only want
  37. // to have one possible construction method in the public space !
  38. //
  39. CFileCacheObject() ;
  40. CFileCacheObject( CFileCacheObject& ) ;
  41. public :
  42. //
  43. // Create a CFileCacheObject object - we only save the path for
  44. // future reference !
  45. //
  46. CFileCacheObject(
  47. CFileCacheKey& key,
  48. class CFileCacheObjectConstructor& constructor
  49. ) ;
  50. //
  51. // Close our file handle and everything !
  52. //
  53. ~CFileCacheObject() ;
  54. //
  55. // This file actually attempt to open the file
  56. //
  57. BOOL
  58. Init(
  59. CFileCacheObjectConstructor& constructor
  60. ) ;
  61. CFileCacheKey&
  62. GetKey() {
  63. return m_key ;
  64. }
  65. int
  66. MatchKey(
  67. CFileCacheKey& key
  68. ) {
  69. return key.m_cbPathLength == m_key.m_cbPathLength &&
  70. memcmp( key.m_lpstrPath, m_key.m_lpstrPath, m_key.m_cbPathLength ) == 0 ;
  71. }
  72. void
  73. ExclusiveLock() {
  74. m_Lock.ExclusiveLock() ;
  75. }
  76. void
  77. ExclusiveUnlock() {
  78. m_Lock.ExclusiveUnlock() ;
  79. }
  80. void
  81. ShareLock() {
  82. m_Lock.ShareLock() ;
  83. }
  84. void
  85. ShareUnlock() {
  86. m_Lock.ShareUnlock() ;
  87. }
  88. BOOL
  89. AccessCheck(
  90. HANDLE hToken,
  91. BOOL fHoldTokens
  92. ) ;
  93. //
  94. // The following are the publicly available functions
  95. // for using the cached file handle data -
  96. //
  97. HANDLE
  98. GetFileHandle() {
  99. return m_hFile ;
  100. }
  101. //
  102. //
  103. //
  104. BOOL
  105. QuerySize( LPDWORD lpcbFileSizeLow,
  106. LPDWORD lpcbFileSizeHigh
  107. ) {
  108. *lpcbFileSizeLow = m_FileInfo.nFileSizeLow ;
  109. *lpcbFileSizeHigh = m_FileInfo.nFileSizeHigh ;
  110. return TRUE ;
  111. }
  112. } ;
  113. typedef CRefPtr< CFileCacheObject > PCACHEFILE ;
  114. class CFileCache {
  115. public :
  116. //
  117. // Destructor must be virtual as the actual File Cache will be
  118. // derived from this but accessed through the CFIleCache interface
  119. // only !
  120. //
  121. virtual ~CFileCache() {}
  122. //
  123. // If this returns true than we should have a valid file ready to go !
  124. //
  125. virtual BOOL
  126. CreateFile(
  127. LPCSTR lpstrName,
  128. DWORD cbTotalPath,
  129. HANDLE hOpeningUser,
  130. HANDLE& hFile,
  131. PCACHEFILE& pcacheFile,
  132. BOOL fCachingDesired
  133. ) = 0 ;
  134. //
  135. // This function is used by users who can use PreComputePathHash -
  136. // This allows some optimization as it reduces the cost of computing
  137. // hash values for file names significantly if the caller can
  138. // provide a portion of the hash value !!!
  139. //
  140. virtual BOOL
  141. CreateFileWithPrecomputedHash(
  142. LPCSTR lpstrName,
  143. DWORD cbTotalPath,
  144. DWORD cbPreComputePathLength,
  145. DWORD dwHashPrecompute,
  146. HANDLE hOpeningUser,
  147. HANDLE& hFile,
  148. PCACHEFILE& pcacheFile,
  149. BOOL fCachingDesired
  150. ) = 0 ;
  151. //
  152. // Close a file handle retrieved from the cache !
  153. //
  154. virtual BOOL
  155. CloseHandle(
  156. PCACHEFILE& pcacheFile
  157. ) = 0 ;
  158. //
  159. // Create an instance of a file cache !
  160. //
  161. static CFileCache*
  162. CreateFileCache(
  163. DWORD MaxHandles = 5000,
  164. BOOL fHoldTokens = TRUE
  165. ) ;
  166. //
  167. // This function is used to Compute a portion of the hash value for
  168. // a path that will be reused several times. This allows the caller
  169. // to speed up cache searches significantly, if they can compute this
  170. // value frequently !
  171. //
  172. virtual DWORD
  173. PreComputePathHash(
  174. LPCSTR lpstrPath,
  175. DWORD cbPath
  176. ) = 0 ;
  177. } ;
  178. BOOL
  179. FileCacheInit() ;
  180. BOOL
  181. FileCacheTerm() ;
  182. #endif // _FCACHE_H_