diff options
Diffstat (limited to 'include/odhtdb/DatabaseStorage.hpp')
-rw-r--r-- | include/odhtdb/DatabaseStorage.hpp | 57 |
1 files changed, 46 insertions, 11 deletions
diff --git a/include/odhtdb/DatabaseStorage.hpp b/include/odhtdb/DatabaseStorage.hpp index ad4f70b..a2789f7 100644 --- a/include/odhtdb/DatabaseStorage.hpp +++ b/include/odhtdb/DatabaseStorage.hpp @@ -5,12 +5,14 @@ #include "DataView.hpp" #include "Signature.hpp" #include "Encryption.hpp" +#include "Group.hpp" #include <vector> #include <stdexcept> +#include <boost/filesystem/path.hpp> +#include <sibs/SafeDeserializer.hpp> namespace odhtdb { - class Group; class User; struct DatabaseStorageObject @@ -52,12 +54,30 @@ namespace odhtdb DatabaseStorageNotFound(const std::string &errMsg) : std::runtime_error(errMsg) {} }; + class DatabaseStorageCorrupt : public std::runtime_error + { + public: + DatabaseStorageCorrupt(const std::string &errMsg) : std::runtime_error(errMsg) {} + }; + using DatabaseStorageMap = MapHash<DatabaseStorageObjectList*>; using DatabaseStorageQuarantineMap = Signature::MapPublicKey<std::vector<DatabaseStorageQuarantineObject*>>; + const int PASSWORD_SALT_LEN = 16; + const int HASHED_PASSWORD_LEN = 32; + class DatabaseStorage { public: + struct UserData + { + User *user; + u8 hashedPassword[HASHED_PASSWORD_LEN]; // All bytes are zero if user is not local + }; + + // Throws DatabaseStorageCorrupt if storage is corrupted + DatabaseStorage(const boost::filesystem::path &storagePath); + // Throws DatabaseStorageAlreadyExists if data with hash already exists void createStorage(const Hash &hash, Group *creatorGroup, u64 timestamp, const u8 *data, usize dataSize); @@ -68,28 +88,43 @@ namespace odhtdb // Throws DatabaseStorageAlreadyExists if same data has been added before (hash of @data, in @dataHash) void addToQuarantine(const Hash &dataHash, const Signature::PublicKey &creatorPublicKey, u64 timestamp, const u8 *data, usize dataSize); - void addUser(User *user, const Hash &hash); + // Return false if group with id already exists, otherwise return true + bool addGroup(const Hash &nodeHash, Group *group); + + // Return false if user public key already exists, otherwise return true + bool addUser(const Hash &nodeHash, User *user); // Returns nullptr if no storage with provided hash exists const DatabaseStorageObjectList* getStorage(const Hash &hash) const; - // Returns nullptr if no node with the user exists - const Hash* getNodeByUserPublicKey(const Signature::PublicKey &userPublicKey) const; + // Returns nullptr if a group with id @groupId doesn't exist in node @nodeHash or if no node with id @nodeHash exists + Group* getGroupById(const Hash &nodeHash, uint8_t groupId[GROUP_ID_LENGTH]); - // Returns nullptr if no user with public key exists - const User* getUserByPublicKey(const Signature::PublicKey &userPublicKey) const; + // Returns nullptr if a user with public key @publicKey doesn't exist in node @nodeHash or if no node with id @nodeHash exists + User* getUserByPublicKey(const Hash &nodeHash, Signature::PublicKey &userPublicKey); - // Returns nullptr if a group with id @groupId doesn't exist - Group* getGroupById(uint8_t groupId[16]); + // Return users in node, or nullptr if no node with id @nodeHash exists + const Signature::MapPublicKey<UserData*>* getUsersData(const Hash &nodeHash) const; // Update storage state (remove quarantine objects if they are too old, etc) void update(); private: + void loadGroupsFromFile(); + void loadUsersFromFile(); + void loadDataFromFile(); + void loadMetadataFromFile(); + void loadStorageCreate(sibs::SafeDeserializer &deserializer); + void loadStorageAppend(sibs::SafeDeserializer &deserializer); + private: DatabaseStorageMap storageMap; DatabaseStorageQuarantineMap quarantineStorageMap; SetHash storedDataHash; // Prevent duplicate data from being added - Signature::MapPublicKey<Hash*> userPublicKeyNodeMap; - Signature::MapPublicKey<const User*> publicKeyUserMap; - DataViewMap<Group*> groupByIdMap; + MapHash<Signature::MapPublicKey<UserData*>*> nodePublicKeyUserDataMap; + MapHash<DataViewMap<Group*>*> nodeGroupByIdMap; + boost::filesystem::path groupsFilePath; + boost::filesystem::path usersFilePath; + boost::filesystem::path dataFilePath; + boost::filesystem::path metadataFilePath; + u8 passwordSalt[PASSWORD_SALT_LEN]; }; } |