| 1 | // -------------------------------------------------------------------------- |
|---|
| 2 | // |
|---|
| 3 | // File |
|---|
| 4 | // Name: BackupStoreContext.h |
|---|
| 5 | // Purpose: Context for backup store server |
|---|
| 6 | // Created: 2003/08/20 |
|---|
| 7 | // |
|---|
| 8 | // -------------------------------------------------------------------------- |
|---|
| 9 | |
|---|
| 10 | #ifndef BACKUPCONTEXT__H |
|---|
| 11 | #define BACKUPCONTEXT__H |
|---|
| 12 | |
|---|
| 13 | #include <string> |
|---|
| 14 | #include <map> |
|---|
| 15 | #include <memory> |
|---|
| 16 | |
|---|
| 17 | #include "autogen_BackupProtocol.h" |
|---|
| 18 | #include "BackupStoreInfo.h" |
|---|
| 19 | #include "BackupStoreRefCountDatabase.h" |
|---|
| 20 | #include "NamedLock.h" |
|---|
| 21 | #include "Message.h" |
|---|
| 22 | #include "Utils.h" |
|---|
| 23 | |
|---|
| 24 | class BackupStoreDirectory; |
|---|
| 25 | class BackupStoreFilename; |
|---|
| 26 | class IOStream; |
|---|
| 27 | class BackupProtocolMessage; |
|---|
| 28 | class StreamableMemBlock; |
|---|
| 29 | |
|---|
| 30 | class HousekeepingInterface |
|---|
| 31 | { |
|---|
| 32 | public: |
|---|
| 33 | virtual ~HousekeepingInterface() { } |
|---|
| 34 | virtual void SendMessageToHousekeepingProcess(const void *Msg, int MsgLen) = 0; |
|---|
| 35 | }; |
|---|
| 36 | |
|---|
| 37 | // -------------------------------------------------------------------------- |
|---|
| 38 | // |
|---|
| 39 | // Class |
|---|
| 40 | // Name: BackupStoreContext |
|---|
| 41 | // Purpose: Context for backup store server |
|---|
| 42 | // Created: 2003/08/20 |
|---|
| 43 | // |
|---|
| 44 | // -------------------------------------------------------------------------- |
|---|
| 45 | class BackupStoreContext |
|---|
| 46 | { |
|---|
| 47 | public: |
|---|
| 48 | BackupStoreContext(int32_t ClientID, HousekeepingInterface &rDaemon, |
|---|
| 49 | const std::string& rConnectionDetails); |
|---|
| 50 | ~BackupStoreContext(); |
|---|
| 51 | private: |
|---|
| 52 | BackupStoreContext(const BackupStoreContext &rToCopy); |
|---|
| 53 | public: |
|---|
| 54 | |
|---|
| 55 | void ReceivedFinishCommand(); |
|---|
| 56 | void CleanUp(); |
|---|
| 57 | |
|---|
| 58 | int32_t GetClientID() {return mClientID;} |
|---|
| 59 | |
|---|
| 60 | enum |
|---|
| 61 | { |
|---|
| 62 | Phase_START = 0, |
|---|
| 63 | Phase_Version = 0, |
|---|
| 64 | Phase_Login = 1, |
|---|
| 65 | Phase_Commands = 2 |
|---|
| 66 | }; |
|---|
| 67 | |
|---|
| 68 | int GetPhase() const {return mProtocolPhase;} |
|---|
| 69 | void SetPhase(int NewPhase) {mProtocolPhase = NewPhase;} |
|---|
| 70 | |
|---|
| 71 | // Read only locking |
|---|
| 72 | bool SessionIsReadOnly() {return mReadOnly;} |
|---|
| 73 | bool AttemptToGetWriteLock(); |
|---|
| 74 | |
|---|
| 75 | void SetClientHasAccount(const std::string &rStoreRoot, int StoreDiscSet) {mClientHasAccount = true; mStoreRoot = rStoreRoot; mStoreDiscSet = StoreDiscSet;} |
|---|
| 76 | bool GetClientHasAccount() const {return mClientHasAccount;} |
|---|
| 77 | const std::string &GetStoreRoot() const {return mStoreRoot;} |
|---|
| 78 | int GetStoreDiscSet() const {return mStoreDiscSet;} |
|---|
| 79 | |
|---|
| 80 | // Store info |
|---|
| 81 | void LoadStoreInfo(); |
|---|
| 82 | void SaveStoreInfo(bool AllowDelay = true); |
|---|
| 83 | const BackupStoreInfo &GetBackupStoreInfo() const; |
|---|
| 84 | const std::string GetAccountName() |
|---|
| 85 | { |
|---|
| 86 | if(!mapStoreInfo.get()) |
|---|
| 87 | { |
|---|
| 88 | return "Unknown"; |
|---|
| 89 | } |
|---|
| 90 | return mapStoreInfo->GetAccountName(); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | // Client marker |
|---|
| 94 | int64_t GetClientStoreMarker(); |
|---|
| 95 | void SetClientStoreMarker(int64_t ClientStoreMarker); |
|---|
| 96 | |
|---|
| 97 | // Usage information |
|---|
| 98 | void GetStoreDiscUsageInfo(int64_t &rBlocksUsed, int64_t &rBlocksSoftLimit, int64_t &rBlocksHardLimit); |
|---|
| 99 | bool HardLimitExceeded(); |
|---|
| 100 | |
|---|
| 101 | // Reading directories |
|---|
| 102 | // -------------------------------------------------------------------------- |
|---|
| 103 | // |
|---|
| 104 | // Function |
|---|
| 105 | // Name: BackupStoreContext::GetDirectory(int64_t) |
|---|
| 106 | // Purpose: Return a reference to a directory. Valid only until the |
|---|
| 107 | // next time a function which affects directories is called. |
|---|
| 108 | // Mainly this funciton, and creation of files. |
|---|
| 109 | // Created: 2003/09/02 |
|---|
| 110 | // |
|---|
| 111 | // -------------------------------------------------------------------------- |
|---|
| 112 | const BackupStoreDirectory &GetDirectory(int64_t ObjectID) |
|---|
| 113 | { |
|---|
| 114 | // External callers aren't allowed to change it -- this function |
|---|
| 115 | // merely turns the the returned directory const. |
|---|
| 116 | return GetDirectoryInternal(ObjectID); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | // Manipulating files/directories |
|---|
| 120 | int64_t AddFile(IOStream &rFile, int64_t InDirectory, int64_t ModificationTime, int64_t AttributesHash, int64_t DiffFromFileID, const BackupStoreFilename &rFilename, bool MarkFileWithSameNameAsOldVersions); |
|---|
| 121 | int64_t AddDirectory(int64_t InDirectory, const BackupStoreFilename &rFilename, const StreamableMemBlock &Attributes, int64_t AttributesModTime, bool &rAlreadyExists); |
|---|
| 122 | void ChangeDirAttributes(int64_t Directory, const StreamableMemBlock &Attributes, int64_t AttributesModTime); |
|---|
| 123 | bool ChangeFileAttributes(const BackupStoreFilename &rFilename, int64_t InDirectory, const StreamableMemBlock &Attributes, int64_t AttributesHash, int64_t &rObjectIDOut); |
|---|
| 124 | bool DeleteFile(const BackupStoreFilename &rFilename, int64_t InDirectory, int64_t &rObjectIDOut); |
|---|
| 125 | bool UndeleteFile(int64_t ObjectID, int64_t InDirectory); |
|---|
| 126 | void DeleteDirectory(int64_t ObjectID, bool Undelete = false); |
|---|
| 127 | void MoveObject(int64_t ObjectID, int64_t MoveFromDirectory, int64_t MoveToDirectory, const BackupStoreFilename &rNewFilename, bool MoveAllWithSameName, bool AllowMoveOverDeletedObject); |
|---|
| 128 | |
|---|
| 129 | // Manipulating objects |
|---|
| 130 | enum |
|---|
| 131 | { |
|---|
| 132 | ObjectExists_Anything = 0, |
|---|
| 133 | ObjectExists_File = 1, |
|---|
| 134 | ObjectExists_Directory = 2 |
|---|
| 135 | }; |
|---|
| 136 | bool ObjectExists(int64_t ObjectID, int MustBe = ObjectExists_Anything); |
|---|
| 137 | std::auto_ptr<IOStream> OpenObject(int64_t ObjectID); |
|---|
| 138 | |
|---|
| 139 | // Info |
|---|
| 140 | int32_t GetClientID() const {return mClientID;} |
|---|
| 141 | const std::string& GetConnectionDetails() { return mConnectionDetails; } |
|---|
| 142 | |
|---|
| 143 | private: |
|---|
| 144 | void MakeObjectFilename(int64_t ObjectID, std::string &rOutput, bool EnsureDirectoryExists = false); |
|---|
| 145 | BackupStoreDirectory &GetDirectoryInternal(int64_t ObjectID); |
|---|
| 146 | void SaveDirectory(BackupStoreDirectory &rDir, int64_t ObjectID); |
|---|
| 147 | void RemoveDirectoryFromCache(int64_t ObjectID); |
|---|
| 148 | void DeleteDirectoryRecurse(int64_t ObjectID, int64_t &rBlocksDeletedOut, bool Undelete); |
|---|
| 149 | int64_t AllocateObjectID(); |
|---|
| 150 | |
|---|
| 151 | std::string mConnectionDetails; |
|---|
| 152 | int32_t mClientID; |
|---|
| 153 | HousekeepingInterface &mrDaemon; |
|---|
| 154 | int mProtocolPhase; |
|---|
| 155 | bool mClientHasAccount; |
|---|
| 156 | std::string mStoreRoot; // has final directory separator |
|---|
| 157 | int mStoreDiscSet; |
|---|
| 158 | bool mReadOnly; |
|---|
| 159 | NamedLock mWriteLock; |
|---|
| 160 | int mSaveStoreInfoDelay; // how many times to delay saving the store info |
|---|
| 161 | |
|---|
| 162 | // Store info |
|---|
| 163 | std::auto_ptr<BackupStoreInfo> mapStoreInfo; |
|---|
| 164 | |
|---|
| 165 | // Refcount database |
|---|
| 166 | std::auto_ptr<BackupStoreRefCountDatabase> mapRefCount; |
|---|
| 167 | |
|---|
| 168 | // Directory cache |
|---|
| 169 | std::map<int64_t, BackupStoreDirectory*> mDirectoryCache; |
|---|
| 170 | |
|---|
| 171 | public: |
|---|
| 172 | class TestHook |
|---|
| 173 | { |
|---|
| 174 | public: |
|---|
| 175 | virtual std::auto_ptr<BackupProtocolMessage> |
|---|
| 176 | StartCommand(const BackupProtocolMessage& rCommand) = 0; |
|---|
| 177 | virtual ~TestHook() { } |
|---|
| 178 | }; |
|---|
| 179 | void SetTestHook(TestHook& rTestHook) |
|---|
| 180 | { |
|---|
| 181 | mpTestHook = &rTestHook; |
|---|
| 182 | } |
|---|
| 183 | std::auto_ptr<BackupProtocolMessage> |
|---|
| 184 | StartCommandHook(const BackupProtocolMessage& rCommand) |
|---|
| 185 | { |
|---|
| 186 | if(mpTestHook) |
|---|
| 187 | { |
|---|
| 188 | return mpTestHook->StartCommand(rCommand); |
|---|
| 189 | } |
|---|
| 190 | return std::auto_ptr<BackupProtocolMessage>(); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | private: |
|---|
| 194 | TestHook* mpTestHook; |
|---|
| 195 | }; |
|---|
| 196 | |
|---|
| 197 | #endif // BACKUPCONTEXT__H |
|---|
| 198 | |
|---|