| 1 | // -------------------------------------------------------------------------- |
|---|
| 2 | // |
|---|
| 3 | // File |
|---|
| 4 | // Name: BackupStoreFile.h |
|---|
| 5 | // Purpose: Utils for manipulating files |
|---|
| 6 | // Created: 2003/08/28 |
|---|
| 7 | // |
|---|
| 8 | // -------------------------------------------------------------------------- |
|---|
| 9 | |
|---|
| 10 | #ifndef BACKUPSTOREFILE__H |
|---|
| 11 | #define BACKUPSTOREFILE__H |
|---|
| 12 | |
|---|
| 13 | #include <cstdlib> |
|---|
| 14 | #include <memory> |
|---|
| 15 | #include <cstdlib> |
|---|
| 16 | |
|---|
| 17 | #include "BackupClientFileAttributes.h" |
|---|
| 18 | #include "BackupStoreFilename.h" |
|---|
| 19 | #include "IOStream.h" |
|---|
| 20 | #include "ReadLoggingStream.h" |
|---|
| 21 | |
|---|
| 22 | typedef struct |
|---|
| 23 | { |
|---|
| 24 | int64_t mBytesInEncodedFiles; |
|---|
| 25 | int64_t mBytesAlreadyOnServer; |
|---|
| 26 | int64_t mTotalFileStreamSize; |
|---|
| 27 | } BackupStoreFileStats; |
|---|
| 28 | |
|---|
| 29 | class RunStatusProvider; |
|---|
| 30 | |
|---|
| 31 | // Uncomment to disable backwards compatibility |
|---|
| 32 | //#define BOX_DISABLE_BACKWARDS_COMPATIBILITY_BACKUPSTOREFILE |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | // Output buffer to EncodeChunk and input data to DecodeChunk must |
|---|
| 36 | // have specific alignment, see function comments. |
|---|
| 37 | #define BACKUPSTOREFILE_CODING_BLOCKSIZE 16 |
|---|
| 38 | #define BACKUPSTOREFILE_CODING_OFFSET 15 |
|---|
| 39 | |
|---|
| 40 | // Have some memory allocation commands, note closing "Off" at end of file. |
|---|
| 41 | #include "MemLeakFindOn.h" |
|---|
| 42 | |
|---|
| 43 | // -------------------------------------------------------------------------- |
|---|
| 44 | // |
|---|
| 45 | // Class |
|---|
| 46 | // Name: DiffTimer |
|---|
| 47 | // Purpose: Interface for classes that can keep track of diffing time, |
|---|
| 48 | // and send SSL keepalive messages |
|---|
| 49 | // Created: 2006/01/19 |
|---|
| 50 | // |
|---|
| 51 | // -------------------------------------------------------------------------- |
|---|
| 52 | class DiffTimer |
|---|
| 53 | { |
|---|
| 54 | public: |
|---|
| 55 | DiffTimer(); |
|---|
| 56 | virtual ~DiffTimer(); |
|---|
| 57 | public: |
|---|
| 58 | virtual void DoKeepAlive() = 0; |
|---|
| 59 | virtual int GetMaximumDiffingTime() = 0; |
|---|
| 60 | virtual bool IsManaged() = 0; |
|---|
| 61 | }; |
|---|
| 62 | |
|---|
| 63 | // -------------------------------------------------------------------------- |
|---|
| 64 | // |
|---|
| 65 | // Class |
|---|
| 66 | // Name: BackupStoreFile |
|---|
| 67 | // Purpose: Class to hold together utils for manipulating files. |
|---|
| 68 | // Created: 2003/08/28 |
|---|
| 69 | // |
|---|
| 70 | // -------------------------------------------------------------------------- |
|---|
| 71 | class BackupStoreFile |
|---|
| 72 | { |
|---|
| 73 | public: |
|---|
| 74 | class DecodedStream : public IOStream |
|---|
| 75 | { |
|---|
| 76 | friend class BackupStoreFile; |
|---|
| 77 | private: |
|---|
| 78 | DecodedStream(IOStream &rEncodedFile, int Timeout); |
|---|
| 79 | DecodedStream(const DecodedStream &); // not allowed |
|---|
| 80 | DecodedStream &operator=(const DecodedStream &); // not allowed |
|---|
| 81 | public: |
|---|
| 82 | ~DecodedStream(); |
|---|
| 83 | |
|---|
| 84 | // Stream functions |
|---|
| 85 | virtual int Read(void *pBuffer, int NBytes, int Timeout); |
|---|
| 86 | virtual void Write(const void *pBuffer, int NBytes); |
|---|
| 87 | virtual bool StreamDataLeft(); |
|---|
| 88 | virtual bool StreamClosed(); |
|---|
| 89 | |
|---|
| 90 | // Accessor functions |
|---|
| 91 | const BackupClientFileAttributes &GetAttributes() {return mAttributes;} |
|---|
| 92 | const BackupStoreFilename &GetFilename() {return mFilename;} |
|---|
| 93 | int64_t GetNumBlocks() {return mNumBlocks;} // primarily for tests |
|---|
| 94 | |
|---|
| 95 | bool IsSymLink(); |
|---|
| 96 | |
|---|
| 97 | private: |
|---|
| 98 | void Setup(const BackupClientFileAttributes *pAlterativeAttr); |
|---|
| 99 | void ReadBlockIndex(bool MagicAlreadyRead); |
|---|
| 100 | |
|---|
| 101 | private: |
|---|
| 102 | IOStream &mrEncodedFile; |
|---|
| 103 | int mTimeout; |
|---|
| 104 | BackupClientFileAttributes mAttributes; |
|---|
| 105 | BackupStoreFilename mFilename; |
|---|
| 106 | int64_t mNumBlocks; |
|---|
| 107 | void *mpBlockIndex; |
|---|
| 108 | uint8_t *mpEncodedData; |
|---|
| 109 | uint8_t *mpClearData; |
|---|
| 110 | int mClearDataSize; |
|---|
| 111 | int mCurrentBlock; |
|---|
| 112 | int mCurrentBlockClearSize; |
|---|
| 113 | int mPositionInCurrentBlock; |
|---|
| 114 | uint64_t mEntryIVBase; |
|---|
| 115 | #ifndef BOX_DISABLE_BACKWARDS_COMPATIBILITY_BACKUPSTOREFILE |
|---|
| 116 | bool mIsOldVersion; |
|---|
| 117 | #endif |
|---|
| 118 | }; |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | // Main interface |
|---|
| 122 | static std::auto_ptr<IOStream> EncodeFile |
|---|
| 123 | ( |
|---|
| 124 | const char *Filename, |
|---|
| 125 | int64_t ContainerID, const BackupStoreFilename &rStoreFilename, |
|---|
| 126 | int64_t *pModificationTime = 0, |
|---|
| 127 | ReadLoggingStream::Logger* pLogger = NULL, |
|---|
| 128 | RunStatusProvider* pRunStatusProvider = NULL |
|---|
| 129 | ); |
|---|
| 130 | static std::auto_ptr<IOStream> EncodeFileDiff |
|---|
| 131 | ( |
|---|
| 132 | const char *Filename, int64_t ContainerID, |
|---|
| 133 | const BackupStoreFilename &rStoreFilename, |
|---|
| 134 | int64_t DiffFromObjectID, IOStream &rDiffFromBlockIndex, |
|---|
| 135 | int Timeout, |
|---|
| 136 | DiffTimer *pDiffTimer, |
|---|
| 137 | int64_t *pModificationTime = 0, |
|---|
| 138 | bool *pIsCompletelyDifferent = 0 |
|---|
| 139 | ); |
|---|
| 140 | static bool VerifyEncodedFileFormat(IOStream &rFile, int64_t *pDiffFromObjectIDOut = 0, int64_t *pContainerIDOut = 0); |
|---|
| 141 | static void CombineFile(IOStream &rDiff, IOStream &rDiff2, IOStream &rFrom, IOStream &rOut); |
|---|
| 142 | static void CombineDiffs(IOStream &rDiff1, IOStream &rDiff2, IOStream &rDiff2b, IOStream &rOut); |
|---|
| 143 | static void ReverseDiffFile(IOStream &rDiff, IOStream &rFrom, IOStream &rFrom2, IOStream &rOut, int64_t ObjectIDOfFrom, bool *pIsCompletelyDifferent = 0); |
|---|
| 144 | static void DecodeFile(IOStream &rEncodedFile, const char *DecodedFilename, int Timeout, const BackupClientFileAttributes *pAlterativeAttr = 0); |
|---|
| 145 | static std::auto_ptr<BackupStoreFile::DecodedStream> DecodeFileStream(IOStream &rEncodedFile, int Timeout, const BackupClientFileAttributes *pAlterativeAttr = 0); |
|---|
| 146 | static bool CompareFileContentsAgainstBlockIndex(const char *Filename, IOStream &rBlockIndex, int Timeout); |
|---|
| 147 | static std::auto_ptr<IOStream> CombineFileIndices(IOStream &rDiff, IOStream &rFrom, bool DiffIsIndexOnly = false, bool FromIsIndexOnly = false); |
|---|
| 148 | |
|---|
| 149 | // Stream manipulation |
|---|
| 150 | static std::auto_ptr<IOStream> ReorderFileToStreamOrder(IOStream *pStream, bool TakeOwnership); |
|---|
| 151 | static void MoveStreamPositionToBlockIndex(IOStream &rStream); |
|---|
| 152 | |
|---|
| 153 | // Crypto setup |
|---|
| 154 | static void SetBlowfishKeys(const void *pKey, int KeyLength, const void *pBlockEntryKey, int BlockEntryKeyLength); |
|---|
| 155 | #ifndef HAVE_OLD_SSL |
|---|
| 156 | static void SetAESKey(const void *pKey, int KeyLength); |
|---|
| 157 | #endif |
|---|
| 158 | |
|---|
| 159 | // Allocation of properly aligning chunks for decoding and encoding chunks |
|---|
| 160 | inline static void *CodingChunkAlloc(int Size) |
|---|
| 161 | { |
|---|
| 162 | uint8_t *a = (uint8_t*)malloc((Size) + (BACKUPSTOREFILE_CODING_BLOCKSIZE * 3)); |
|---|
| 163 | if(a == 0) return 0; |
|---|
| 164 | // Align to main block size |
|---|
| 165 | ASSERT(sizeof(uint64_t) >= sizeof(void*)); // make sure casting the right pointer size |
|---|
| 166 | uint8_t adjustment = BACKUPSTOREFILE_CODING_BLOCKSIZE |
|---|
| 167 | - (uint8_t)(((uint64_t)a) % BACKUPSTOREFILE_CODING_BLOCKSIZE); |
|---|
| 168 | uint8_t *b = (a + adjustment); |
|---|
| 169 | // Store adjustment |
|---|
| 170 | *b = adjustment; |
|---|
| 171 | // Return offset |
|---|
| 172 | return b + BACKUPSTOREFILE_CODING_OFFSET; |
|---|
| 173 | } |
|---|
| 174 | inline static void CodingChunkFree(void *Block) |
|---|
| 175 | { |
|---|
| 176 | // Check alignment is as expected |
|---|
| 177 | ASSERT(sizeof(uint64_t) >= sizeof(void*)); // make sure casting the right pointer size |
|---|
| 178 | ASSERT((uint8_t)(((uint64_t)Block) % BACKUPSTOREFILE_CODING_BLOCKSIZE) == BACKUPSTOREFILE_CODING_OFFSET); |
|---|
| 179 | uint8_t *a = (uint8_t*)Block; |
|---|
| 180 | a -= BACKUPSTOREFILE_CODING_OFFSET; |
|---|
| 181 | // Adjust downwards... |
|---|
| 182 | a -= *a; |
|---|
| 183 | free(a); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | static void DiffTimerExpired(); |
|---|
| 187 | |
|---|
| 188 | // Building blocks |
|---|
| 189 | class EncodingBuffer |
|---|
| 190 | { |
|---|
| 191 | public: |
|---|
| 192 | EncodingBuffer(); |
|---|
| 193 | ~EncodingBuffer(); |
|---|
| 194 | private: |
|---|
| 195 | // No copying |
|---|
| 196 | EncodingBuffer(const EncodingBuffer &); |
|---|
| 197 | EncodingBuffer &operator=(const EncodingBuffer &); |
|---|
| 198 | public: |
|---|
| 199 | void Allocate(int Size); |
|---|
| 200 | void Reallocate(int NewSize); |
|---|
| 201 | |
|---|
| 202 | uint8_t *mpBuffer; |
|---|
| 203 | int mBufferSize; |
|---|
| 204 | }; |
|---|
| 205 | static int MaxBlockSizeForChunkSize(int ChunkSize); |
|---|
| 206 | static int EncodeChunk(const void *Chunk, int ChunkSize, BackupStoreFile::EncodingBuffer &rOutput); |
|---|
| 207 | |
|---|
| 208 | // Caller should know how big the output size is, but also allocate a bit more memory to cover various |
|---|
| 209 | // overheads allowed for in checks |
|---|
| 210 | static inline int OutputBufferSizeForKnownOutputSize(int KnownChunkSize) |
|---|
| 211 | { |
|---|
| 212 | // Plenty big enough |
|---|
| 213 | return KnownChunkSize + 256; |
|---|
| 214 | } |
|---|
| 215 | static int DecodeChunk(const void *Encoded, int EncodedSize, void *Output, int OutputSize); |
|---|
| 216 | |
|---|
| 217 | // Statisitics, not designed to be completely reliable |
|---|
| 218 | static void ResetStats(); |
|---|
| 219 | static BackupStoreFileStats msStats; |
|---|
| 220 | |
|---|
| 221 | // For debug |
|---|
| 222 | #ifndef BOX_RELEASE_BUILD |
|---|
| 223 | static bool TraceDetailsOfDiffProcess; |
|---|
| 224 | #endif |
|---|
| 225 | |
|---|
| 226 | // For decoding encoded files |
|---|
| 227 | static void DumpFile(void *clibFileHandle, bool ToTrace, IOStream &rFile); |
|---|
| 228 | }; |
|---|
| 229 | |
|---|
| 230 | #include "MemLeakFindOff.h" |
|---|
| 231 | |
|---|
| 232 | #endif // BACKUPSTOREFILE__H |
|---|