Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
7.2 KiB

  1. //
  2. // flatfile.h -- This file contains the class definations for:
  3. // CFlatFile
  4. //
  5. // Created:
  6. // Sep 3, 1996 -- Alex Wetmore (awetmore)
  7. // Changes:
  8. // May 7, 1998 -- Alex Wetmore (awetmore)
  9. // -- Modify for use in NNTP. Remove sorting features, file handle
  10. // cache, etc.
  11. // Oct 23,1998 -- Kangrong Yan ( kangyan )
  12. // -- Added integrity flag
  13. //
  14. #ifndef __FLATFILE_H__
  15. #define __FLATFILE_H__
  16. #include <windows.h>
  17. #include <stdio.h>
  18. #include <writebuf.h>
  19. //
  20. // size of the read buffer used for GetFirstRecord/GetNextRecord()
  21. //
  22. #define FF_BUFFER_SIZE 8192
  23. //
  24. // maximum record size
  25. //
  26. #define MAX_RECORD_SIZE 4096
  27. // do a automatic compaction if there are more then 10 deleted records and
  28. // the ratio of records to deleted records is less then 10
  29. #define FF_COMPACTION_MIN_DELETED_RECORDS 10
  30. #define FF_COMPACTION_MIN_TOTAL_RECORDS 100
  31. #define FF_COMPACTION_RATIO 10
  32. //
  33. // Integrity flag values
  34. //
  35. #define FF_FILE_GOOD 0x0000FFFF
  36. #define FF_FILE_BAD 0x00010000
  37. //
  38. // file extensions for flat files
  39. //
  40. #define NEW_FF_EXT ".tmp" // extension for new flatfile as its built
  41. #define BAK_FF_EXT ".bak" // extension for an old backup flatfile
  42. #define FF_IDX_EXT ".idx" // extension for an index file
  43. #pragma pack(push, flatfile)
  44. #pragma pack(1)
  45. //
  46. // the structure for the header of the file
  47. //
  48. typedef struct {
  49. DWORD dwSignature; // file signature
  50. DWORD dwFlags; // file flags
  51. } FLATFILE_HEADER;
  52. #define FF_FLAG_COMPACT 0x01
  53. #define FLATFILE_SIGNATURE (DWORD)'__fF'
  54. //
  55. // the record structure for the data file
  56. //
  57. // the size of it is RECORD_HEADER_SIZE + cData;
  58. //
  59. typedef struct {
  60. BOOL fDeleted; // is this deleted?
  61. DWORD cData; // length of the data
  62. BYTE pData[MAX_RECORD_SIZE]; // data
  63. } RECORD;
  64. typedef struct {
  65. BOOL fDeleted; // is this deleted?
  66. DWORD cData; // length of the data
  67. } RECORDHDR;
  68. #define RECORD_HEADER_SIZE sizeof(RECORDHDR)
  69. #pragma pack(pop, flatfile)
  70. //
  71. // A function of this type will be called whenever a record's offset
  72. // changes in the flatfile. It is used to keep the owner up to date
  73. // on record offsets, so that the owner can make quick Delete's
  74. //
  75. typedef void (*PFN_OFFSET_UPDATE)(void *pContext, BYTE *pData, DWORD cData, DWORD iNewOffset);
  76. class CFlatFile {
  77. public:
  78. friend class CFlatFileWriteBuf;
  79. CFlatFile(LPSTR szFilename,
  80. LPSTR szExtension,
  81. void *pContext,
  82. PFN_OFFSET_UPDATE pfnOffsetUpdate,
  83. DWORD dwSignature = FLATFILE_SIGNATURE,
  84. BOOL fClear = FALSE,
  85. DWORD dwFileFlags = 0);
  86. ~CFlatFile();
  87. // insert a new record into the file
  88. HRESULT InsertRecord(LPBYTE pData, DWORD cData, DWORD *piOffset = NULL, DWORD dwVer = 0);
  89. // delete a record from the file
  90. HRESULT DeleteRecord(DWORD iOffset);
  91. // compact out any deleted records in the file
  92. HRESULT Compact();
  93. // get the first record in the file
  94. HRESULT GetFirstRecord(LPBYTE pData,
  95. DWORD *cData,
  96. DWORD *piByteOffset = NULL,
  97. DWORD *pdwVer = NULL );
  98. // get the next record in the file
  99. HRESULT GetNextRecord(LPBYTE pData,
  100. DWORD *cData,
  101. DWORD *piByteOffset = NULL,
  102. DWORD *pdwVer = NULL);
  103. // delete everything in the file
  104. void DeleteAll();
  105. // Dirty the integrity flag
  106. HRESULT DirtyIntegrityFlag();
  107. // Set the integrity flag
  108. HRESULT SetIntegrityFlag();
  109. // Is the file in good integrity ?
  110. BOOL FileInGoodShape();
  111. // Enable the write buffer
  112. VOID EnableWriteBuffer( DWORD cbBuffer );
  113. // Check to see if the file has been opened
  114. BOOL IsFileOpened();
  115. private:
  116. //
  117. // open/close a file.
  118. //
  119. // because this uses cached file handles, the position of the file
  120. // should not be assumed
  121. //
  122. HRESULT OpenFile(LPSTR szFilename = NULL,
  123. DWORD dwOpenMode = OPEN_ALWAYS,
  124. DWORD dwFlags = 0);
  125. //
  126. // close the file handle
  127. //
  128. void CloseFile();
  129. //
  130. // set and get the file header
  131. //
  132. HRESULT SetFileHeader(FLATFILE_HEADER *pHeader);
  133. HRESULT GetFileHeader(FLATFILE_HEADER *pHeader);
  134. //
  135. // read the next chunk of the file into the temporary buffer
  136. // used by GetFirstRecord/GetNextRecord()
  137. //
  138. HRESULT ReadNextNBytes(LPBYTE pData, DWORD cData);
  139. HRESULT ReadNBytesFrom(LPBYTE pData, DWORD cData, DWORD iOffset, DWORD *pcDidRead = NULL);
  140. // iOffset can be set to infinite to append
  141. HRESULT WriteNBytesTo(LPBYTE pData,
  142. DWORD cData,
  143. DWORD *piOffset = NULL,
  144. DWORD iOffset = INFINITE,
  145. DWORD *pcDidWrite = NULL);
  146. HRESULT CFlatFile::WriteNBytesToInternal(
  147. LPBYTE pData,
  148. DWORD cData,
  149. DWORD *piOffset,
  150. DWORD iOffset,
  151. DWORD *pcDidWrite);
  152. HRESULT ReloadReadBuffer();
  153. //
  154. // the file handle for this file
  155. //
  156. HANDLE m_hFile;
  157. //
  158. // flags for creating the file with
  159. //
  160. DWORD m_dwFileFlags;
  161. //
  162. // filename for the flat file
  163. //
  164. char m_szFilename[FILENAME_MAX];
  165. char m_szBaseFilename[FILENAME_MAX];
  166. //
  167. // current read buffer
  168. //
  169. BYTE m_pBuffer[FF_BUFFER_SIZE];
  170. //
  171. // current offset inside the buffer
  172. //
  173. DWORD m_iBuffer;
  174. //
  175. // offset of the read buffer in the file
  176. //
  177. DWORD m_iFile;
  178. //
  179. // size of the read buffer (zero means its not valid)
  180. //
  181. DWORD m_cBuffer;
  182. //
  183. // clear the file on the next open
  184. //
  185. BOOL m_fClearOnOpen;
  186. //
  187. // the number of deleted records in the file. this is first
  188. // computed by FindFirst/FindNext and then kept updated by
  189. // DeleteRecord and DeleteRecordAtOffset
  190. //
  191. DWORD m_cDeletedRecords;
  192. //
  193. // the number of records in the file. this is first
  194. // computed by FindFirst/FindNext and then kept updated by
  195. // InsertRecord
  196. //
  197. DWORD m_cRecords;
  198. //
  199. // context passed into callback functions
  200. //
  201. void *m_pContext;
  202. //
  203. // Whether the file is open
  204. //
  205. BOOL m_fOpen;
  206. //
  207. // The write buffer
  208. //
  209. CFlatFileWriteBuf m_wbBuffer;
  210. //
  211. // function to call when an items offset changes in the flatfile
  212. //
  213. PFN_OFFSET_UPDATE m_pfnOffsetUpdate;
  214. //
  215. // signature for the file
  216. //
  217. DWORD m_dwSignature;
  218. };
  219. #define ret(__rc__) { /* TraceFunctLeave(); */ return(__rc__); }
  220. #define retEC(__ec__, __rc__) { SetLastError(__ec__); /* TraceFunctLeave(); */ return(__rc__); }
  221. #endif