| 1 | // -------------------------------------------------------------------------- |
|---|
| 2 | // |
|---|
| 3 | // File |
|---|
| 4 | // Name: BackupStoreFileEncodeStream.h |
|---|
| 5 | // Purpose: Implement stream-based file encoding for the backup store |
|---|
| 6 | // Created: 12/1/04 |
|---|
| 7 | // |
|---|
| 8 | // -------------------------------------------------------------------------- |
|---|
| 9 | |
|---|
| 10 | #ifndef BACKUPSTOREFILEENCODESTREAM__H |
|---|
| 11 | #define BACKUPSTOREFILEENCODESTREAM__H |
|---|
| 12 | |
|---|
| 13 | #include <vector> |
|---|
| 14 | |
|---|
| 15 | #include "IOStream.h" |
|---|
| 16 | #include "BackupStoreFilename.h" |
|---|
| 17 | #include "CollectInBufferStream.h" |
|---|
| 18 | #include "MD5Digest.h" |
|---|
| 19 | #include "BackupStoreFile.h" |
|---|
| 20 | #include "ReadLoggingStream.h" |
|---|
| 21 | #include "RunStatusProvider.h" |
|---|
| 22 | |
|---|
| 23 | namespace BackupStoreFileCreation |
|---|
| 24 | { |
|---|
| 25 | // Diffing and creation of files share some implementation details. |
|---|
| 26 | typedef struct _BlocksAvailableEntry |
|---|
| 27 | { |
|---|
| 28 | struct _BlocksAvailableEntry *mpNextInHashList; |
|---|
| 29 | int32_t mSize; // size in clear |
|---|
| 30 | uint32_t mWeakChecksum; // weak, rolling checksum |
|---|
| 31 | uint8_t mStrongChecksum[MD5Digest::DigestLength]; // strong digest based checksum |
|---|
| 32 | } BlocksAvailableEntry; |
|---|
| 33 | |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | // -------------------------------------------------------------------------- |
|---|
| 38 | // |
|---|
| 39 | // Class |
|---|
| 40 | // Name: BackupStoreFileEncodeStream |
|---|
| 41 | // Purpose: Encode a file into a stream |
|---|
| 42 | // Created: 8/12/03 |
|---|
| 43 | // |
|---|
| 44 | // -------------------------------------------------------------------------- |
|---|
| 45 | class BackupStoreFileEncodeStream : public IOStream |
|---|
| 46 | { |
|---|
| 47 | public: |
|---|
| 48 | BackupStoreFileEncodeStream(); |
|---|
| 49 | ~BackupStoreFileEncodeStream(); |
|---|
| 50 | |
|---|
| 51 | typedef struct |
|---|
| 52 | { |
|---|
| 53 | int64_t mSpaceBefore; // amount of bytes which aren't taken out of blocks which go |
|---|
| 54 | int32_t mBlocks; // number of block to reuse, starting at this one |
|---|
| 55 | BackupStoreFileCreation::BlocksAvailableEntry *mpStartBlock; // may be null |
|---|
| 56 | } RecipeInstruction; |
|---|
| 57 | |
|---|
| 58 | class Recipe : public std::vector<RecipeInstruction> |
|---|
| 59 | { |
|---|
| 60 | // NOTE: This class is rather tied in with the implementation of diffing. |
|---|
| 61 | public: |
|---|
| 62 | Recipe(BackupStoreFileCreation::BlocksAvailableEntry *pBlockIndex, int64_t NumBlocksInIndex, |
|---|
| 63 | int64_t OtherFileID = 0); |
|---|
| 64 | ~Recipe(); |
|---|
| 65 | |
|---|
| 66 | int64_t GetOtherFileID() {return mOtherFileID;} |
|---|
| 67 | int64_t BlockPtrToIndex(BackupStoreFileCreation::BlocksAvailableEntry *pBlock) |
|---|
| 68 | { |
|---|
| 69 | return pBlock - mpBlockIndex; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | private: |
|---|
| 73 | BackupStoreFileCreation::BlocksAvailableEntry *mpBlockIndex; |
|---|
| 74 | int64_t mNumBlocksInIndex; |
|---|
| 75 | int64_t mOtherFileID; |
|---|
| 76 | }; |
|---|
| 77 | |
|---|
| 78 | void Setup(const char *Filename, Recipe *pRecipe, int64_t ContainerID, |
|---|
| 79 | const BackupStoreFilename &rStoreFilename, |
|---|
| 80 | int64_t *pModificationTime, |
|---|
| 81 | ReadLoggingStream::Logger* pLogger = NULL, |
|---|
| 82 | RunStatusProvider* pRunStatusProvider = NULL); |
|---|
| 83 | |
|---|
| 84 | virtual int Read(void *pBuffer, int NBytes, int Timeout); |
|---|
| 85 | virtual void Write(const void *pBuffer, int NBytes); |
|---|
| 86 | virtual bool StreamDataLeft(); |
|---|
| 87 | virtual bool StreamClosed(); |
|---|
| 88 | int64_t GetTotalBytesSent() { return mTotalBytesSent; } |
|---|
| 89 | |
|---|
| 90 | private: |
|---|
| 91 | enum |
|---|
| 92 | { |
|---|
| 93 | Status_Header = 0, |
|---|
| 94 | Status_Blocks = 1, |
|---|
| 95 | Status_BlockListing = 2, |
|---|
| 96 | Status_Finished = 3 |
|---|
| 97 | }; |
|---|
| 98 | |
|---|
| 99 | private: |
|---|
| 100 | void EncodeCurrentBlock(); |
|---|
| 101 | void CalculateBlockSizes(int64_t DataSize, int64_t &rNumBlocksOut, int32_t &rBlockSizeOut, int32_t &rLastBlockSizeOut); |
|---|
| 102 | void SkipPreviousBlocksInInstruction(); |
|---|
| 103 | void SetForInstruction(); |
|---|
| 104 | void StoreBlockIndexEntry(int64_t WncSizeOrBlkIndex, int32_t ClearSize, uint32_t WeakChecksum, uint8_t *pStrongChecksum); |
|---|
| 105 | |
|---|
| 106 | private: |
|---|
| 107 | Recipe *mpRecipe; |
|---|
| 108 | IOStream *mpFile; // source file |
|---|
| 109 | CollectInBufferStream mData; // buffer for header and index entries |
|---|
| 110 | IOStream *mpLogging; |
|---|
| 111 | RunStatusProvider* mpRunStatusProvider; |
|---|
| 112 | int mStatus; |
|---|
| 113 | bool mSendData; // true if there's file data to send (ie not a symlink) |
|---|
| 114 | int64_t mTotalBlocks; // Total number of blocks in the file |
|---|
| 115 | int64_t mAbsoluteBlockNumber; // The absolute block number currently being output |
|---|
| 116 | // Instruction number |
|---|
| 117 | int64_t mInstructionNumber; |
|---|
| 118 | // All the below are within the current instruction |
|---|
| 119 | int64_t mNumBlocks; // number of blocks. Last one will be a different size to the rest in most cases |
|---|
| 120 | int64_t mCurrentBlock; |
|---|
| 121 | int32_t mCurrentBlockEncodedSize; |
|---|
| 122 | int32_t mPositionInCurrentBlock; // for reading out |
|---|
| 123 | int32_t mBlockSize; // Basic block size of most of the blocks in the file |
|---|
| 124 | int32_t mLastBlockSize; // the size (unencoded) of the last block in the file |
|---|
| 125 | int64_t mTotalBytesSent; |
|---|
| 126 | // Buffers |
|---|
| 127 | uint8_t *mpRawBuffer; // buffer for raw data |
|---|
| 128 | BackupStoreFile::EncodingBuffer mEncodedBuffer; |
|---|
| 129 | // buffer for encoded data |
|---|
| 130 | int32_t mAllocatedBufferSize; // size of above two allocated blocks |
|---|
| 131 | uint64_t mEntryIVBase; // base for block entry IV |
|---|
| 132 | }; |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | #endif // BACKUPSTOREFILEENCODESTREAM__H |
|---|
| 137 | |
|---|