refactor s3 provider
This commit is contained in:
		| @@ -1,205 +0,0 @@ | ||||
| /* | ||||
|   Copyright <2018-2023> <scott.e.graves@protonmail.com> | ||||
|  | ||||
|   Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|   of this software and associated documentation files (the "Software"), to deal | ||||
|   in the Software without restriction, including without limitation the rights | ||||
|   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
|   copies of the Software, and to permit persons to whom the Software is | ||||
|   furnished to do so, subject to the following conditions: | ||||
|  | ||||
|   The above copyright notice and this permission notice shall be included in all | ||||
|   copies or substantial portions of the Software. | ||||
|  | ||||
|   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
|   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||
|   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
|   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
|   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
|   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||
|   SOFTWARE. | ||||
| */ | ||||
| #ifndef INCLUDE_PROVIDERS_BASE_PROVIDER_HPP_ | ||||
| #define INCLUDE_PROVIDERS_BASE_PROVIDER_HPP_ | ||||
|  | ||||
| #include "db/meta_db.hpp" | ||||
| #include "providers/i_provider.hpp" | ||||
|  | ||||
| namespace repertory { | ||||
| class app_config; | ||||
| class i_file_manager; | ||||
|  | ||||
| class base_provider : public i_provider { | ||||
| public: | ||||
|   explicit base_provider(app_config &config); | ||||
|  | ||||
|   ~base_provider() override = default; | ||||
|  | ||||
| private: | ||||
|   app_config &config_; | ||||
|   std::atomic<std::uint64_t> used_space_{0U}; | ||||
|  | ||||
| protected: | ||||
|   api_item_added_callback api_item_added_; | ||||
|   std::unique_ptr<meta_db> meta_db_; | ||||
|   mutable std::recursive_mutex notify_added_mutex_; | ||||
|   i_file_manager *fm_ = nullptr; | ||||
|   stop_type stop_requested_ = false; | ||||
|  | ||||
| protected: | ||||
|   void calculate_used_drive_space(bool add_missing); | ||||
|  | ||||
|   [[nodiscard]] virtual auto | ||||
|   check_file_exists(const std::string &api_path) const -> api_error = 0; | ||||
|  | ||||
|   void cleanup(); | ||||
|  | ||||
|   [[nodiscard]] auto get_config() -> app_config & { return config_; } | ||||
|  | ||||
|   [[nodiscard]] auto get_config() const -> app_config & { return config_; } | ||||
|  | ||||
|   [[nodiscard]] virtual auto | ||||
|   handle_rename_file(const std::string &from_api_path, | ||||
|                      const std::string &to_api_path, | ||||
|                      const std::string &source_path) -> api_error = 0; | ||||
|  | ||||
|   [[nodiscard]] auto notify_directory_added(const std::string &api_path, | ||||
|                                             const std::string &api_parent) const | ||||
|       -> api_error { | ||||
|     return const_cast<base_provider *>(this)->notify_directory_added( | ||||
|         api_path, api_parent); | ||||
|   } | ||||
|  | ||||
|   [[nodiscard]] virtual auto | ||||
|   notify_directory_added(const std::string &api_path, | ||||
|                          const std::string &api_parent) -> api_error; | ||||
|  | ||||
|   [[nodiscard]] auto notify_file_added(const std::string &api_path, | ||||
|                                        const std::string &api_parent, | ||||
|                                        std::uint64_t size) const -> api_error { | ||||
|     return const_cast<base_provider *>(this)->notify_file_added( | ||||
|         api_path, api_parent, size); | ||||
|   } | ||||
|  | ||||
|   [[nodiscard]] virtual auto notify_file_added(const std::string &api_path, | ||||
|                                                const std::string &api_parent, | ||||
|                                                std::uint64_t size) | ||||
|       -> api_error = 0; | ||||
|  | ||||
|   [[nodiscard]] virtual auto | ||||
|   populate_directory_items(const std::string &api_path, | ||||
|                            directory_item_list &list) const -> api_error = 0; | ||||
|  | ||||
|   [[nodiscard]] virtual auto populate_file(const std::string &api_path, | ||||
|                                            api_file &file) const | ||||
|       -> api_error = 0; | ||||
|  | ||||
|   auto processed_orphaned_file(const std::string &source_path, | ||||
|                                const std::string &api_path = "") const -> bool; | ||||
|  | ||||
|   void remove_deleted_files(); | ||||
|  | ||||
|   void remove_expired_orphaned_files(); | ||||
|  | ||||
|   void remove_unknown_source_files(); | ||||
|  | ||||
|   [[nodiscard]] auto remove_item_meta(const std::string &api_path) | ||||
|       -> api_error { | ||||
|     return meta_db_->remove_item_meta(api_path); | ||||
|   } | ||||
|  | ||||
|   void update_filesystem_item(bool directory, const api_error &error, | ||||
|                               const std::string &api_path, | ||||
|                               filesystem_item &fsi) const; | ||||
|  | ||||
| public: | ||||
|   [[nodiscard]] auto | ||||
|   create_directory_clone_source_meta(const std::string &source_api_path, | ||||
|                                      const std::string &api_path) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto create_file(const std::string &api_path, | ||||
|                                  api_meta_map &meta) -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_api_path_from_source(const std::string &source_path, | ||||
|                                               std::string &api_path) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_directory_items(const std::string &api_path, | ||||
|                                          directory_item_list &list) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_file(const std::string &api_path, api_file &file) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_file_size(const std::string &api_path, | ||||
|                                    std::uint64_t &file_size) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_filesystem_item(const std::string &api_path, | ||||
|                                          bool directory, | ||||
|                                          filesystem_item &fsi) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_filesystem_item_and_file(const std::string &api_path, | ||||
|                                                   api_file &file, | ||||
|                                                   filesystem_item &fsi) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto | ||||
|   get_filesystem_item_from_source_path(const std::string &source_path, | ||||
|                                        filesystem_item &fsi) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_item_meta(const std::string &api_path, | ||||
|                                    api_meta_map &meta) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_item_meta(const std::string &api_path, | ||||
|                                    const std::string &key, | ||||
|                                    std::string &value) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_pinned_files() const | ||||
|       -> std::vector<std::string> override { | ||||
|     return meta_db_->get_pinned_files(); | ||||
|   } | ||||
|  | ||||
|   [[nodiscard]] auto get_used_drive_space() const -> std::uint64_t override; | ||||
|  | ||||
|   [[nodiscard]] auto is_file_writeable(const std::string &) const | ||||
|       -> bool override { | ||||
|     return true; | ||||
|   } | ||||
|  | ||||
|   [[nodiscard]] auto remove_item_meta(const std::string &api_path, | ||||
|                                       const std::string &key) | ||||
|       -> api_error override { | ||||
|     return meta_db_->remove_item_meta(api_path, key); | ||||
|   } | ||||
|  | ||||
|   [[nodiscard]] auto rename_file(const std::string &from_api_path, | ||||
|                                  const std::string &to_api_path) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto set_item_meta(const std::string &api_path, | ||||
|                                    const std::string &key, | ||||
|                                    const std::string &value) | ||||
|       -> api_error override { | ||||
|     return meta_db_->set_item_meta(api_path, key, value); | ||||
|   } | ||||
|  | ||||
|   [[nodiscard]] auto set_item_meta(const std::string &api_path, | ||||
|                                    const api_meta_map &meta) | ||||
|       -> api_error override { | ||||
|     return meta_db_->set_item_meta(api_path, meta); | ||||
|   } | ||||
|  | ||||
|   [[nodiscard]] auto start(api_item_added_callback api_item_added, | ||||
|                            i_file_manager *fm) -> bool override; | ||||
|  | ||||
|   void stop() override; | ||||
| }; | ||||
| } // namespace repertory | ||||
|  | ||||
| #endif // INCLUDE_PROVIDERS_BASE_PROVIDER_HPP_ | ||||
| @@ -33,6 +33,12 @@ public: | ||||
|  | ||||
|   ~encrypt_provider() override = default; | ||||
|  | ||||
| public: | ||||
|   encrypt_provider(const encrypt_provider &) = delete; | ||||
|   encrypt_provider(encrypt_provider &&) = delete; | ||||
|   auto operator=(const encrypt_provider &) -> encrypt_provider & = delete; | ||||
|   auto operator=(encrypt_provider &&) -> encrypt_provider & = delete; | ||||
|  | ||||
| private: | ||||
|   struct reader_info final { | ||||
|     std::chrono::system_clock::time_point last_access_time = | ||||
| @@ -47,7 +53,7 @@ private: | ||||
|   rocksdb::ColumnFamilyHandle *dir_family_{}; | ||||
|   rocksdb::ColumnFamilyHandle *file_family_{}; | ||||
|   rocksdb::ColumnFamilyHandle *source_family_{}; | ||||
|   const std::string DB_NAME = "meta_db"; | ||||
|   std::string DB_NAME = "meta_db"; | ||||
|  | ||||
| private: | ||||
|   i_file_manager *fm_ = nullptr; | ||||
| @@ -56,7 +62,7 @@ private: | ||||
|   std::recursive_mutex reader_lookup_mtx_{}; | ||||
|  | ||||
| private: | ||||
|   static auto create_api_file(const std::string api_path, bool directory, | ||||
|   static auto create_api_file(const std::string &api_path, bool directory, | ||||
|                               const std::string &source_path) -> api_file; | ||||
|  | ||||
|   static void create_item_meta(api_meta_map &meta, bool directory, | ||||
| @@ -207,7 +213,6 @@ public: | ||||
|  | ||||
|   [[nodiscard]] auto upload_file(const std::string & /*api_path*/, | ||||
|                                  const std::string & /*source_path*/, | ||||
|                                  const std::string & /*encryption_token*/, | ||||
|                                  stop_type & /*stop_requested*/) | ||||
|       -> api_error override { | ||||
|     return api_error::not_implemented; | ||||
|   | ||||
| @@ -147,9 +147,9 @@ public: | ||||
|  | ||||
|   virtual void stop() = 0; | ||||
|  | ||||
|   [[nodiscard]] virtual auto | ||||
|   upload_file(const std::string &api_path, const std::string &source_path, | ||||
|               const std::string &encryption_token, stop_type &stop_requested) | ||||
|   [[nodiscard]] virtual auto upload_file(const std::string &api_path, | ||||
|                                          const std::string &source_path, | ||||
|                                          stop_type &stop_requested) | ||||
|       -> api_error = 0; | ||||
| }; | ||||
| } // namespace repertory | ||||
|   | ||||
| @@ -23,86 +23,129 @@ | ||||
| #define INCLUDE_PROVIDERS_S3_S3_PROVIDER_HPP_ | ||||
| #if defined(REPERTORY_ENABLE_S3) | ||||
|  | ||||
| #include "db/directory_db.hpp" | ||||
| #include "providers/base_provider.hpp" | ||||
| #include "providers/i_provider.hpp" | ||||
| #include "types/repertory.hpp" | ||||
|  | ||||
| namespace repertory { | ||||
| class app_config; | ||||
| class i_file_manager; | ||||
| class i_s3_comm; | ||||
| class i_http_comm; | ||||
| struct head_object_result; | ||||
|  | ||||
| class s3_provider final : public base_provider { | ||||
| class s3_provider final : public i_provider { | ||||
| public: | ||||
|   s3_provider(app_config &config, i_s3_comm &s3_comm); | ||||
|   s3_provider(app_config &config, i_http_comm &comm); | ||||
|  | ||||
|   ~s3_provider() override = default; | ||||
|  | ||||
| private: | ||||
|   i_s3_comm &s3_comm_; | ||||
| public: | ||||
|   s3_provider(const s3_provider &) = delete; | ||||
|   s3_provider(s3_provider &&) = delete; | ||||
|   auto operator=(const s3_provider &) -> s3_provider & = delete; | ||||
|   auto operator=(s3_provider &&) -> s3_provider & = delete; | ||||
|  | ||||
| private: | ||||
|   std::unique_ptr<directory_db> directory_db_; | ||||
|   std::unique_ptr<std::thread> background_thread_; | ||||
|   app_config &config_; | ||||
|   i_http_comm &comm_; | ||||
|  | ||||
| private: | ||||
|   void create_directories(); | ||||
|   api_item_added_callback api_item_added_; | ||||
|   std::unique_ptr<rocksdb::DB> db_; | ||||
|   std::string DB_NAME = "meta_db"; | ||||
|   i_file_manager *fm_{}; | ||||
|  | ||||
|   void create_parent_directories(const api_file &file, bool directory); | ||||
| private: | ||||
|   [[nodiscard]] auto add_if_not_found(api_file &file, | ||||
|                                       const std::string &object_name) const | ||||
|       -> api_error; | ||||
|  | ||||
|   void update_item_meta(directory_item &di) const; | ||||
|   [[nodiscard]] static auto create_api_file(const std::string &path, | ||||
|                                             const std::string &key, | ||||
|                                             std::uint64_t size) -> api_file; | ||||
|  | ||||
| protected: | ||||
|   [[nodiscard]] auto check_file_exists(const std::string &api_path) const | ||||
|       -> api_error override; | ||||
|   [[nodiscard]] auto create_path_directories(const std::string &api_path, | ||||
|                                              const std::string &key) const | ||||
|       -> api_error; | ||||
|  | ||||
|   [[nodiscard]] auto handle_rename_file(const std::string &from_api_path, | ||||
|                                         const std::string &to_api_path, | ||||
|                                         const std::string &source_path) | ||||
|       -> api_error override; | ||||
|   [[nodiscard]] auto decrypt_object_name(std::string &object_name) const | ||||
|       -> api_error; | ||||
|  | ||||
|   [[nodiscard]] auto notify_directory_added(const std::string &api_path, | ||||
|                                             const std::string &api_parent) | ||||
|       -> api_error override; | ||||
|   [[nodiscard]] auto | ||||
|   get_object_info(const std::string &api_path, bool &is_encrypted, | ||||
|                   std::string &object_name, head_object_result &result) const | ||||
|       -> api_error; | ||||
|  | ||||
|   [[nodiscard]] auto notify_file_added(const std::string &api_path, | ||||
|                                        const std::string &api_parent, | ||||
|                                        std::uint64_t size) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto populate_directory_items(const std::string &api_path, | ||||
|                                               directory_item_list &list) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto populate_file(const std::string &api_path, | ||||
|                                    api_file &file) const -> api_error override; | ||||
|   void remove_deleted_files(); | ||||
|  | ||||
| public: | ||||
| #ifdef REPERTORY_TESTING | ||||
|   void set_callback(api_item_added_callback cb) { api_item_added_ = cb; } | ||||
| #endif // REPERTORY_TESTING | ||||
|  | ||||
|   [[nodiscard]] auto create_directory(const std::string &api_path, | ||||
|                                       api_meta_map &meta) -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto | ||||
|   create_directory_clone_source_meta(const std::string &source_api_path, | ||||
|                                      const std::string &api_path) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto create_file(const std::string &api_path, | ||||
|                                  api_meta_map &meta) -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_api_path_from_source(const std::string &source_path, | ||||
|                                               std::string &api_path) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_directory_item_count(const std::string &api_path) const | ||||
|       -> std::uint64_t override; | ||||
|  | ||||
|   [[nodiscard]] auto get_directory_items(const std::string &api_path, | ||||
|                                          directory_item_list &list) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_file(const std::string &api_path, api_file &file) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_file_list(api_file_list &list) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_file_size(const std::string &api_path, | ||||
|                                    std::uint64_t &file_size) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_filesystem_item(const std::string &api_path, | ||||
|                                          bool directory, | ||||
|                                          filesystem_item &fsi) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_filesystem_item_and_file(const std::string &api_path, | ||||
|                                                   api_file &file, | ||||
|                                                   filesystem_item &fsi) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto | ||||
|   get_filesystem_item_from_source_path(const std::string &source_path, | ||||
|                                        filesystem_item &fsi) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_pinned_files() const | ||||
|       -> std::vector<std::string> override; | ||||
|  | ||||
|   [[nodiscard]] auto get_item_meta(const std::string &api_path, | ||||
|                                    api_meta_map &meta) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_item_meta(const std::string &api_path, | ||||
|                                    const std::string &key, | ||||
|                                    std::string &value) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto get_total_drive_space() const -> std::uint64_t override; | ||||
|  | ||||
|   [[nodiscard]] auto get_total_item_count() const -> std::uint64_t override; | ||||
|  | ||||
|   [[nodiscard]] auto get_provider_type() const -> provider_type override { | ||||
|     return provider_type::s3; | ||||
|   } | ||||
|  | ||||
|   [[nodiscard]] auto get_total_drive_space() const -> std::uint64_t override { | ||||
|     return std::numeric_limits<std::int64_t>::max() / std::int64_t(2); | ||||
|   } | ||||
|  | ||||
|   [[nodiscard]] auto get_total_item_count() const -> std::uint64_t override; | ||||
|   [[nodiscard]] auto get_used_drive_space() const -> std::uint64_t override; | ||||
|  | ||||
|   [[nodiscard]] auto is_direct_only() const -> bool override { return false; } | ||||
|  | ||||
| @@ -112,11 +155,14 @@ public: | ||||
|   [[nodiscard]] auto is_file(const std::string &api_path, bool &exists) const | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto is_file_writeable(const std::string &api_path) const | ||||
|       -> bool override; | ||||
|  | ||||
|   [[nodiscard]] auto is_online() const -> bool override; | ||||
|  | ||||
|   [[nodiscard]] auto is_rename_supported() const -> bool override { | ||||
|     return false; | ||||
|   } | ||||
|   }; | ||||
|  | ||||
|   [[nodiscard]] auto read_file_bytes(const std::string &api_path, | ||||
|                                      std::size_t size, std::uint64_t offset, | ||||
| @@ -130,14 +176,31 @@ public: | ||||
|   [[nodiscard]] auto remove_file(const std::string &api_path) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto remove_item_meta(const std::string &api_path, | ||||
|                                       const std::string &key) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto rename_file(const std::string &from_api_path, | ||||
|                                  const std::string &to_api_path) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto set_item_meta(const std::string &api_path, | ||||
|                                    const std::string &key, | ||||
|                                    const std::string &value) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto set_item_meta(const std::string &api_path, | ||||
|                                    const api_meta_map &meta) | ||||
|       -> api_error override; | ||||
|  | ||||
|   [[nodiscard]] auto start(api_item_added_callback api_item_added, | ||||
|                            i_file_manager *fm) -> bool override; | ||||
|  | ||||
|   void stop() override; | ||||
|  | ||||
|   [[nodiscard]] auto | ||||
|   upload_file(const std::string &api_path, const std::string &source_path, | ||||
|               const std::string &encryption_token, stop_type &stop_requested) | ||||
|   [[nodiscard]] auto upload_file(const std::string &api_path, | ||||
|                                  const std::string &source_path, | ||||
|                                  stop_type &stop_requested) | ||||
|       -> api_error override; | ||||
| }; | ||||
| } // namespace repertory | ||||
|   | ||||
| @@ -36,15 +36,21 @@ public: | ||||
|  | ||||
|   ~sia_provider() override = default; | ||||
|  | ||||
| public: | ||||
|   sia_provider(const sia_provider &) = delete; | ||||
|   sia_provider(sia_provider &&) = delete; | ||||
|   auto operator=(const sia_provider &) -> sia_provider & = delete; | ||||
|   auto operator=(sia_provider &&) -> sia_provider & = delete; | ||||
|  | ||||
| private: | ||||
|   app_config &config_; | ||||
|   i_http_comm &comm_; | ||||
|  | ||||
| private: | ||||
|   const std::string DB_NAME = "meta_db"; | ||||
|   api_item_added_callback api_item_added_; | ||||
|   std::unique_ptr<rocksdb::DB> db_; | ||||
|   i_file_manager *fm_ = nullptr; | ||||
|   std::string DB_NAME = "meta_db"; | ||||
|   i_file_manager *fm_{}; | ||||
|  | ||||
| private: | ||||
|   [[nodiscard]] static auto create_api_file(std::string path, | ||||
| @@ -57,7 +63,7 @@ private: | ||||
|   [[nodiscard]] auto get_object_info(const std::string &api_path, | ||||
|                                      json &object_info) const -> api_error; | ||||
|  | ||||
|   [[nodiscard]] auto get_object_list(const std::string api_path, | ||||
|   [[nodiscard]] auto get_object_list(const std::string &api_path, | ||||
|                                      nlohmann::json &object_list) const -> bool; | ||||
|  | ||||
|   void remove_deleted_files(); | ||||
| @@ -185,7 +191,6 @@ public: | ||||
|  | ||||
|   [[nodiscard]] auto upload_file(const std::string &api_path, | ||||
|                                  const std::string &source_path, | ||||
|                                  const std::string & /* encryption_token */, | ||||
|                                  stop_type &stop_requested) | ||||
|       -> api_error override; | ||||
| }; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user