Compare commits
8 Commits
28dc153822
...
31e20e9af0
Author | SHA1 | Date | |
---|---|---|---|
31e20e9af0 | |||
cfb7a74841 | |||
c8f2485ff0 | |||
62857e1372 | |||
ae3b592cf6 | |||
3365363d23 | |||
f480720665 | |||
d0a8f9df58 |
76
repertory/librepertory/include/db/i_meta_db.hpp
Normal file
76
repertory/librepertory/include/db/i_meta_db.hpp
Normal file
@ -0,0 +1,76 @@
|
||||
/*
|
||||
Copyright <2018-2024> <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 REPERTORY_INCLUDE_DB_I_META_DB_HPP_
|
||||
#define REPERTORY_INCLUDE_DB_I_META_DB_HPP_
|
||||
|
||||
#include "types/repertory.hpp"
|
||||
|
||||
namespace repertory {
|
||||
class i_meta_db {
|
||||
INTERFACE_SETUP(i_meta_db);
|
||||
|
||||
public:
|
||||
[[nodiscard]] virtual auto get_api_path(const std::string &source_path,
|
||||
std::string &api_path) const
|
||||
-> api_error = 0;
|
||||
|
||||
[[nodiscard]] virtual auto get_api_path_list() const
|
||||
-> std::vector<std::string> = 0;
|
||||
|
||||
[[nodiscard]] virtual auto get_item_meta(const std::string &api_path,
|
||||
api_meta_map &meta) const
|
||||
-> api_error = 0;
|
||||
|
||||
[[nodiscard]] virtual auto get_item_meta(const std::string &api_path,
|
||||
const std::string &key,
|
||||
std::string &value) const
|
||||
-> api_error = 0;
|
||||
|
||||
[[nodiscard]] virtual auto get_pinned_files() const
|
||||
-> std::vector<std::string> = 0;
|
||||
|
||||
[[nodiscard]] virtual auto get_total_item_count() const -> std::uint64_t = 0;
|
||||
|
||||
[[nodiscard]] virtual auto get_total_size() const -> std::uint64_t = 0;
|
||||
|
||||
virtual void remove_api_path(const std::string &api_path) = 0;
|
||||
|
||||
[[nodiscard]] virtual auto remove_item_meta(const std::string &api_path,
|
||||
const std::string &key)
|
||||
-> api_error = 0;
|
||||
|
||||
[[nodiscard]] virtual auto rename_item_meta(const std::string &from_api_path,
|
||||
const std::string &to_api_path)
|
||||
-> api_error = 0;
|
||||
|
||||
[[nodiscard]] virtual auto set_item_meta(const std::string &api_path,
|
||||
const std::string &key,
|
||||
const std::string &value)
|
||||
-> api_error = 0;
|
||||
|
||||
[[nodiscard]] virtual auto set_item_meta(const std::string &api_path,
|
||||
const api_meta_map &meta)
|
||||
-> api_error = 0;
|
||||
};
|
||||
} // namespace repertory
|
||||
|
||||
#endif // REPERTORY_INCLUDE_DB_I_META_DB_HPP_
|
33
repertory/librepertory/include/db/meta_db.hpp
Normal file
33
repertory/librepertory/include/db/meta_db.hpp
Normal file
@ -0,0 +1,33 @@
|
||||
/*
|
||||
Copyright <2018-2024> <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 REPERTORY_INCLUDE_DB_META_DB_HPP_
|
||||
#define REPERTORY_INCLUDE_DB_META_DB_HPP_
|
||||
|
||||
#include "db/i_meta_db.hpp"
|
||||
|
||||
namespace repertory {
|
||||
class app_config;
|
||||
|
||||
auto create_meta_db(const app_config &cfg) -> std::unique_ptr<i_meta_db>;
|
||||
} // namespace repertory
|
||||
|
||||
#endif // REPERTORY_INCLUDE_DB_META_DB_HPP_
|
@ -19,24 +19,25 @@
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
#ifndef REPERTORY_INCLUDE_PROVIDERS_META_DB_HPP_
|
||||
#define REPERTORY_INCLUDE_PROVIDERS_META_DB_HPP_
|
||||
#ifndef REPERTORY_INCLUDE_DB_SQLITE_META_DB_HPP_
|
||||
#define REPERTORY_INCLUDE_DB_SQLITE_META_DB_HPP_
|
||||
|
||||
#include "db/i_meta_db.hpp"
|
||||
#include "types/repertory.hpp"
|
||||
#include "utils/db/sqlite/db_common.hpp"
|
||||
|
||||
namespace repertory {
|
||||
class app_config;
|
||||
|
||||
class meta_db final {
|
||||
class sqlite_meta_db final : public i_meta_db {
|
||||
public:
|
||||
meta_db(const app_config &cfg);
|
||||
~meta_db();
|
||||
sqlite_meta_db(const app_config &cfg);
|
||||
~sqlite_meta_db() override;
|
||||
|
||||
meta_db(const meta_db &) = delete;
|
||||
meta_db(meta_db &&) = delete;
|
||||
auto operator=(const meta_db &) -> meta_db & = delete;
|
||||
auto operator=(meta_db &&) -> meta_db & = delete;
|
||||
sqlite_meta_db(const sqlite_meta_db &) = delete;
|
||||
sqlite_meta_db(sqlite_meta_db &&) = delete;
|
||||
auto operator=(const sqlite_meta_db &) -> sqlite_meta_db & = delete;
|
||||
auto operator=(sqlite_meta_db &&) -> sqlite_meta_db & = delete;
|
||||
|
||||
private:
|
||||
utils::db::sqlite::db3_t db_;
|
||||
@ -48,39 +49,47 @@ private:
|
||||
|
||||
public:
|
||||
[[nodiscard]] auto get_api_path(const std::string &source_path,
|
||||
std::string &api_path) -> api_error;
|
||||
std::string &api_path) const
|
||||
-> api_error override;
|
||||
|
||||
[[nodiscard]] auto get_api_path_list() -> std::vector<std::string>;
|
||||
[[nodiscard]] auto get_api_path_list() const
|
||||
-> std::vector<std::string> override;
|
||||
|
||||
[[nodiscard]] auto get_item_meta(const std::string &api_path,
|
||||
api_meta_map &meta) -> api_error;
|
||||
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;
|
||||
std::string &value) const
|
||||
-> api_error override;
|
||||
|
||||
[[nodiscard]] auto get_pinned_files() const -> std::vector<std::string>;
|
||||
[[nodiscard]] auto get_pinned_files() const
|
||||
-> std::vector<std::string> override;
|
||||
|
||||
[[nodiscard]] auto get_total_item_count() const -> std::uint64_t;
|
||||
[[nodiscard]] auto get_total_item_count() const -> std::uint64_t override;
|
||||
|
||||
[[nodiscard]] auto get_total_size() const -> std::uint64_t;
|
||||
[[nodiscard]] auto get_total_size() const -> std::uint64_t override;
|
||||
|
||||
void remove_api_path(const std::string &api_path);
|
||||
void remove_api_path(const std::string &api_path) override;
|
||||
|
||||
[[nodiscard]] auto remove_item_meta(const std::string &api_path,
|
||||
const std::string &key) -> api_error;
|
||||
const std::string &key)
|
||||
-> api_error override;
|
||||
|
||||
[[nodiscard]] auto rename_item_meta(const std::string &from_api_path,
|
||||
const std::string &to_api_path)
|
||||
-> api_error;
|
||||
-> api_error override;
|
||||
|
||||
[[nodiscard]] auto set_item_meta(const std::string &api_path,
|
||||
const std::string &key,
|
||||
const std::string &value) -> api_error;
|
||||
const std::string &value)
|
||||
-> api_error override;
|
||||
|
||||
[[nodiscard]] auto set_item_meta(const std::string &api_path,
|
||||
const api_meta_map &meta) -> api_error;
|
||||
const api_meta_map &meta)
|
||||
-> api_error override;
|
||||
};
|
||||
} // namespace repertory
|
||||
|
||||
#endif // REPERTORY_INCLUDE_PROVIDERS_META_DB_HPP_
|
||||
#endif // REPERTORY_INCLUDE_DB_SQLITE_META_DB_HPP_
|
@ -88,7 +88,7 @@ E_SIMPLE2(download_resume_removed, debug, true,
|
||||
std::string, dest_path, dest, E_FROM_STRING
|
||||
);
|
||||
|
||||
E_SIMPLE1(item_timeout, debug, true,
|
||||
E_SIMPLE1(item_timeout, trace, true,
|
||||
std::string, api_path, ap, E_FROM_STRING
|
||||
);
|
||||
// clang-format on
|
||||
|
@ -22,8 +22,8 @@
|
||||
#ifndef REPERTORY_INCLUDE_PROVIDERS_BASE_PROVIDER_HPP_
|
||||
#define REPERTORY_INCLUDE_PROVIDERS_BASE_PROVIDER_HPP_
|
||||
|
||||
#include "db/i_meta_db.hpp"
|
||||
#include "providers/i_provider.hpp"
|
||||
#include "providers/meta_db.hpp"
|
||||
#include "types/repertory.hpp"
|
||||
|
||||
namespace repertory {
|
||||
@ -32,6 +32,13 @@ class i_file_manager;
|
||||
class i_http_comm;
|
||||
|
||||
class base_provider : public i_provider {
|
||||
private:
|
||||
struct removed_item final {
|
||||
std::string api_path;
|
||||
bool directory{};
|
||||
std::string source_path;
|
||||
};
|
||||
|
||||
public:
|
||||
base_provider(app_config &config, i_http_comm &comm)
|
||||
: config_(config), comm_(comm) {}
|
||||
@ -42,24 +49,39 @@ private:
|
||||
|
||||
private:
|
||||
api_item_added_callback api_item_added_;
|
||||
std::unique_ptr<meta_db> db3_;
|
||||
std::unique_ptr<i_meta_db> db3_;
|
||||
i_file_manager *fm_{};
|
||||
|
||||
private:
|
||||
void remove_deleted_files(const stop_type &stop_requested);
|
||||
void add_all_items(const stop_type &stop_requested);
|
||||
|
||||
void get_removed_items(std::deque<removed_item> &directories,
|
||||
std::deque<removed_item> &files,
|
||||
const stop_type &stop_requested) const;
|
||||
|
||||
void process_removed_directories(std::deque<removed_item> &removed_list,
|
||||
const stop_type &stop_requested);
|
||||
|
||||
void process_removed_files(std::deque<removed_item> &removed_list,
|
||||
const stop_type &stop_requested);
|
||||
|
||||
void remove_deleted_items(const stop_type &stop_requested);
|
||||
|
||||
void remove_unmatched_source_files(const stop_type &stop_requested);
|
||||
|
||||
protected:
|
||||
[[nodiscard]] static auto
|
||||
create_api_file(std::string path, std::string key, std::uint64_t size,
|
||||
std::uint64_t file_time) -> api_file;
|
||||
[[nodiscard]] static auto create_api_file(std::string path, std::string key,
|
||||
std::uint64_t size,
|
||||
std::uint64_t file_time)
|
||||
-> api_file;
|
||||
|
||||
[[nodiscard]] static auto create_api_file(std::string path,
|
||||
std::uint64_t size,
|
||||
api_meta_map &meta) -> api_file;
|
||||
|
||||
[[nodiscard]] virtual auto
|
||||
create_directory_impl(const std::string &api_path,
|
||||
api_meta_map &meta) -> api_error = 0;
|
||||
[[nodiscard]] virtual auto create_directory_impl(const std::string &api_path,
|
||||
api_meta_map &meta)
|
||||
-> api_error = 0;
|
||||
|
||||
[[nodiscard]] virtual auto
|
||||
create_file_extra(const std::string & /* api_path */,
|
||||
@ -71,8 +93,8 @@ protected:
|
||||
return api_item_added_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto
|
||||
get_api_item_added() const -> const api_item_added_callback & {
|
||||
[[nodiscard]] auto get_api_item_added() const
|
||||
-> const api_item_added_callback & {
|
||||
return api_item_added_;
|
||||
}
|
||||
|
||||
@ -84,9 +106,9 @@ protected:
|
||||
return config_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto get_db() -> meta_db & { return *db3_; }
|
||||
[[nodiscard]] auto get_db() -> i_meta_db & { return *db3_; }
|
||||
|
||||
[[nodiscard]] auto get_db() const -> const meta_db & { return *db3_; }
|
||||
[[nodiscard]] auto get_db() const -> const i_meta_db & { return *db3_; }
|
||||
|
||||
[[nodiscard]] virtual auto
|
||||
get_directory_items_impl(const std::string &api_path,
|
||||
@ -98,20 +120,22 @@ protected:
|
||||
return fm_;
|
||||
}
|
||||
|
||||
[[nodiscard]] virtual auto
|
||||
remove_directory_impl(const std::string &api_path) -> api_error = 0;
|
||||
[[nodiscard]] virtual auto remove_directory_impl(const std::string &api_path)
|
||||
-> api_error = 0;
|
||||
|
||||
[[nodiscard]] virtual auto
|
||||
remove_file_impl(const std::string &api_path) -> api_error = 0;
|
||||
[[nodiscard]] virtual auto remove_file_impl(const std::string &api_path)
|
||||
-> api_error = 0;
|
||||
|
||||
[[nodiscard]] virtual auto
|
||||
upload_file_impl(const std::string &api_path, const std::string &source_path,
|
||||
stop_type &stop_requested) -> api_error = 0;
|
||||
[[nodiscard]] virtual auto upload_file_impl(const std::string &api_path,
|
||||
const std::string &source_path,
|
||||
stop_type &stop_requested)
|
||||
-> api_error = 0;
|
||||
|
||||
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_directory_clone_source_meta(const std::string &source_api_path,
|
||||
const std::string &api_path)
|
||||
-> api_error override;
|
||||
|
||||
[[nodiscard]] auto create_directory(const std::string &api_path,
|
||||
api_meta_map &meta) -> api_error override;
|
||||
@ -119,76 +143,82 @@ public:
|
||||
[[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_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_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 &f,
|
||||
filesystem_item &fsi) const
|
||||
-> api_error override;
|
||||
|
||||
[[nodiscard]] auto
|
||||
get_directory_items(const std::string &api_path,
|
||||
directory_item_list &list) const -> api_error override;
|
||||
get_filesystem_item_from_source_path(const std::string &source_path,
|
||||
filesystem_item &fsi) 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_item_meta(const std::string &api_path,
|
||||
api_meta_map &meta) 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_item_meta(const std::string &api_path,
|
||||
const std::string &key,
|
||||
std::string &value) const
|
||||
-> api_error override;
|
||||
|
||||
[[nodiscard]] auto get_filesystem_item_and_file(
|
||||
const std::string &api_path, api_file &f,
|
||||
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;
|
||||
[[nodiscard]] auto get_pinned_files() const
|
||||
-> std::vector<std::string> override;
|
||||
|
||||
[[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_file_writeable(const std::string &api_path) const -> bool override;
|
||||
[[nodiscard]] auto is_file_writeable(const std::string &api_path) const
|
||||
-> bool override;
|
||||
|
||||
[[nodiscard]] auto is_read_only() const -> bool override { return false; }
|
||||
|
||||
[[nodiscard]] auto
|
||||
remove_directory(const std::string &api_path) -> api_error override;
|
||||
[[nodiscard]] auto remove_directory(const std::string &api_path)
|
||||
-> api_error override;
|
||||
|
||||
[[nodiscard]] auto
|
||||
remove_file(const std::string &api_path) -> api_error override;
|
||||
[[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 remove_item_meta(const std::string &api_path,
|
||||
const std::string &key)
|
||||
-> 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 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 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 *mgr) -> bool override;
|
||||
|
||||
void stop() override;
|
||||
|
||||
[[nodiscard]] auto
|
||||
upload_file(const std::string &api_path, const std::string &source_path,
|
||||
stop_type &stop_requested) -> api_error override;
|
||||
[[nodiscard]] auto upload_file(const std::string &api_path,
|
||||
const std::string &source_path,
|
||||
stop_type &stop_requested)
|
||||
-> api_error override;
|
||||
};
|
||||
} // namespace repertory
|
||||
|
||||
|
@ -26,6 +26,7 @@
|
||||
|
||||
namespace repertory {
|
||||
class app_config;
|
||||
|
||||
class polling final {
|
||||
public:
|
||||
enum struct frequency {
|
||||
@ -59,12 +60,10 @@ public:
|
||||
|
||||
private:
|
||||
app_config *config_{nullptr};
|
||||
std::unique_ptr<std::thread> high_frequency_thread_;
|
||||
std::array<std::unique_ptr<std::thread>, 3U> frequency_threads_;
|
||||
std::unordered_map<std::string, polling_item> items_;
|
||||
std::unique_ptr<std::thread> low_frequency_thread_;
|
||||
std::mutex mutex_;
|
||||
std::condition_variable notify_;
|
||||
std::unique_ptr<std::thread> second_frequency_thread_;
|
||||
std::mutex start_stop_mutex_;
|
||||
stop_type stop_requested_{false};
|
||||
|
||||
|
31
repertory/librepertory/src/db/meta_db.cpp
Normal file
31
repertory/librepertory/src/db/meta_db.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
/*
|
||||
Copyright <2018-2024> <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.
|
||||
*/
|
||||
#include "db/meta_db.hpp"
|
||||
|
||||
#include "app_config.hpp"
|
||||
#include "db/sqlite_meta_db.hpp"
|
||||
|
||||
namespace repertory {
|
||||
auto create_meta_db(const app_config &cfg) -> std::unique_ptr<i_meta_db> {
|
||||
return std::make_unique<sqlite_meta_db>(cfg);
|
||||
}
|
||||
} // namespace repertory
|
@ -19,7 +19,7 @@
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
#include "providers/meta_db.hpp"
|
||||
#include "db/sqlite_meta_db.hpp"
|
||||
|
||||
#include "app_config.hpp"
|
||||
#include "utils/db/sqlite/db_common.hpp"
|
||||
@ -31,7 +31,7 @@
|
||||
#include "utils/string.hpp"
|
||||
|
||||
namespace repertory {
|
||||
meta_db::meta_db(const app_config &cfg) {
|
||||
sqlite_meta_db::sqlite_meta_db(const app_config &cfg) {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
const std::map<std::string, std::string> sql_create_tables{
|
||||
@ -55,10 +55,10 @@ meta_db::meta_db(const app_config &cfg) {
|
||||
sql_create_tables);
|
||||
}
|
||||
|
||||
meta_db::~meta_db() { db_.reset(); }
|
||||
sqlite_meta_db::~sqlite_meta_db() { db_.reset(); }
|
||||
|
||||
auto meta_db::get_api_path(const std::string &source_path,
|
||||
std::string &api_path) -> api_error {
|
||||
auto sqlite_meta_db::get_api_path(const std::string &source_path,
|
||||
std::string &api_path) const -> api_error {
|
||||
auto result = utils::db::sqlite::db_select{*db_, table_name}
|
||||
.column("api_path")
|
||||
.where("source_path")
|
||||
@ -76,7 +76,7 @@ auto meta_db::get_api_path(const std::string &source_path,
|
||||
return api_error::item_not_found;
|
||||
}
|
||||
|
||||
auto meta_db::get_api_path_list() -> std::vector<std::string> {
|
||||
auto sqlite_meta_db::get_api_path_list() const -> std::vector<std::string> {
|
||||
std::vector<std::string> ret{};
|
||||
|
||||
auto result =
|
||||
@ -91,8 +91,8 @@ auto meta_db::get_api_path_list() -> std::vector<std::string> {
|
||||
return ret;
|
||||
}
|
||||
|
||||
auto meta_db::get_item_meta(const std::string &api_path, api_meta_map &meta)
|
||||
-> api_error {
|
||||
auto sqlite_meta_db::get_item_meta(const std::string &api_path,
|
||||
api_meta_map &meta) const -> api_error {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
auto result = utils::db::sqlite::db_select{*db_, table_name}
|
||||
@ -131,8 +131,9 @@ auto meta_db::get_item_meta(const std::string &api_path, api_meta_map &meta)
|
||||
return api_error::error;
|
||||
}
|
||||
|
||||
auto meta_db::get_item_meta(const std::string &api_path, const std::string &key,
|
||||
std::string &value) const -> api_error {
|
||||
auto sqlite_meta_db::get_item_meta(const std::string &api_path,
|
||||
const std::string &key,
|
||||
std::string &value) const -> api_error {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
auto result = utils::db::sqlite::db_select{*db_, table_name}
|
||||
@ -176,7 +177,7 @@ auto meta_db::get_item_meta(const std::string &api_path, const std::string &key,
|
||||
return api_error::error;
|
||||
}
|
||||
|
||||
auto meta_db::get_pinned_files() const -> std::vector<std::string> {
|
||||
auto sqlite_meta_db::get_pinned_files() const -> std::vector<std::string> {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
std::vector<std::string> ret{};
|
||||
@ -200,7 +201,7 @@ auto meta_db::get_pinned_files() const -> std::vector<std::string> {
|
||||
return ret;
|
||||
}
|
||||
|
||||
auto meta_db::get_total_item_count() const -> std::uint64_t {
|
||||
auto sqlite_meta_db::get_total_item_count() const -> std::uint64_t {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
std::uint64_t ret{};
|
||||
@ -223,7 +224,7 @@ auto meta_db::get_total_item_count() const -> std::uint64_t {
|
||||
return ret;
|
||||
}
|
||||
|
||||
auto meta_db::get_total_size() const -> std::uint64_t {
|
||||
auto sqlite_meta_db::get_total_size() const -> std::uint64_t {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
try {
|
||||
@ -245,7 +246,7 @@ auto meta_db::get_total_size() const -> std::uint64_t {
|
||||
return 0U;
|
||||
}
|
||||
|
||||
void meta_db::remove_api_path(const std::string &api_path) {
|
||||
void sqlite_meta_db::remove_api_path(const std::string &api_path) {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
auto result = utils::db::sqlite::db_delete{*db_, table_name}
|
||||
@ -258,8 +259,8 @@ void meta_db::remove_api_path(const std::string &api_path) {
|
||||
}
|
||||
}
|
||||
|
||||
auto meta_db::remove_item_meta(const std::string &api_path,
|
||||
const std::string &key) -> api_error {
|
||||
auto sqlite_meta_db::remove_item_meta(const std::string &api_path,
|
||||
const std::string &key) -> api_error {
|
||||
api_meta_map meta{};
|
||||
auto res = get_item_meta(api_path, meta);
|
||||
if (res != api_error::success) {
|
||||
@ -270,8 +271,9 @@ auto meta_db::remove_item_meta(const std::string &api_path,
|
||||
return update_item_meta(api_path, meta);
|
||||
}
|
||||
|
||||
auto meta_db::rename_item_meta(const std::string &from_api_path,
|
||||
const std::string &to_api_path) -> api_error {
|
||||
auto sqlite_meta_db::rename_item_meta(const std::string &from_api_path,
|
||||
const std::string &to_api_path)
|
||||
-> api_error {
|
||||
api_meta_map meta{};
|
||||
auto res = get_item_meta(from_api_path, meta);
|
||||
if (res != api_error::success) {
|
||||
@ -282,13 +284,14 @@ auto meta_db::rename_item_meta(const std::string &from_api_path,
|
||||
return update_item_meta(to_api_path, meta);
|
||||
}
|
||||
|
||||
auto meta_db::set_item_meta(const std::string &api_path, const std::string &key,
|
||||
const std::string &value) -> api_error {
|
||||
auto sqlite_meta_db::set_item_meta(const std::string &api_path,
|
||||
const std::string &key,
|
||||
const std::string &value) -> api_error {
|
||||
return set_item_meta(api_path, {{key, value}});
|
||||
}
|
||||
|
||||
auto meta_db::set_item_meta(const std::string &api_path,
|
||||
const api_meta_map &meta) -> api_error {
|
||||
auto sqlite_meta_db::set_item_meta(const std::string &api_path,
|
||||
const api_meta_map &meta) -> api_error {
|
||||
api_meta_map existing_meta{};
|
||||
if (get_item_meta(api_path, existing_meta) != api_error::success) {
|
||||
// TODO handle error
|
||||
@ -301,8 +304,8 @@ auto meta_db::set_item_meta(const std::string &api_path,
|
||||
return update_item_meta(api_path, existing_meta);
|
||||
}
|
||||
|
||||
auto meta_db::update_item_meta(const std::string &api_path, api_meta_map meta)
|
||||
-> api_error {
|
||||
auto sqlite_meta_db::update_item_meta(const std::string &api_path,
|
||||
api_meta_map meta) -> api_error {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
auto directory = utils::string::to_bool(meta[META_DIRECTORY]);
|
@ -22,6 +22,7 @@
|
||||
#include "providers/base_provider.hpp"
|
||||
|
||||
#include "app_config.hpp"
|
||||
#include "db/meta_db.hpp"
|
||||
#include "events/event_system.hpp"
|
||||
#include "events/events.hpp"
|
||||
#include "file_manager/i_file_manager.hpp"
|
||||
@ -32,9 +33,23 @@
|
||||
#include "utils/time.hpp"
|
||||
|
||||
namespace repertory {
|
||||
void base_provider::add_all_items(const stop_type &stop_requested) {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
api_file_list list{};
|
||||
std::string marker;
|
||||
auto res{api_error::more_data};
|
||||
while (not stop_requested && res == api_error::more_data) {
|
||||
res = get_file_list(list, marker);
|
||||
if (res != api_error::success && res != api_error::more_data) {
|
||||
utils::error::raise_error(function_name, res, "failed to get file list");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto base_provider::create_api_file(std::string path, std::string key,
|
||||
std::uint64_t size,
|
||||
std::uint64_t file_time) -> api_file {
|
||||
std::uint64_t size, std::uint64_t file_time)
|
||||
-> api_file {
|
||||
api_file file{};
|
||||
file.api_path = utils::path::create_api_path(path);
|
||||
file.api_parent = utils::path::get_parent_api_path(file.api_path);
|
||||
@ -66,8 +81,8 @@ auto base_provider::create_api_file(std::string path, std::uint64_t size,
|
||||
}
|
||||
|
||||
auto base_provider::create_directory_clone_source_meta(
|
||||
const std::string &source_api_path,
|
||||
const std::string &api_path) -> api_error {
|
||||
const std::string &source_api_path, const std::string &api_path)
|
||||
-> api_error {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
bool exists{};
|
||||
@ -164,8 +179,8 @@ auto base_provider::create_directory(const std::string &api_path,
|
||||
return set_item_meta(api_path, meta);
|
||||
}
|
||||
|
||||
auto base_provider::create_file(const std::string &api_path,
|
||||
api_meta_map &meta) -> api_error {
|
||||
auto base_provider::create_file(const std::string &api_path, api_meta_map &meta)
|
||||
-> api_error {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
bool exists{};
|
||||
@ -222,8 +237,9 @@ auto base_provider::create_file(const std::string &api_path,
|
||||
return api_error::error;
|
||||
}
|
||||
|
||||
auto base_provider::get_api_path_from_source(
|
||||
const std::string &source_path, std::string &api_path) const -> api_error {
|
||||
auto base_provider::get_api_path_from_source(const std::string &source_path,
|
||||
std::string &api_path) const
|
||||
-> api_error {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
if (source_path.empty()) {
|
||||
@ -236,8 +252,9 @@ auto base_provider::get_api_path_from_source(
|
||||
return db3_->get_api_path(source_path, api_path);
|
||||
}
|
||||
|
||||
auto base_provider::get_directory_items(
|
||||
const std::string &api_path, directory_item_list &list) const -> api_error {
|
||||
auto base_provider::get_directory_items(const std::string &api_path,
|
||||
directory_item_list &list) const
|
||||
-> api_error {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
bool exists{};
|
||||
@ -301,9 +318,10 @@ auto base_provider::get_file_size(const std::string &api_path,
|
||||
return api_error::success;
|
||||
}
|
||||
|
||||
auto base_provider::get_filesystem_item(
|
||||
const std::string &api_path, bool directory,
|
||||
filesystem_item &fsi) const -> api_error {
|
||||
auto base_provider::get_filesystem_item(const std::string &api_path,
|
||||
bool directory,
|
||||
filesystem_item &fsi) const
|
||||
-> api_error {
|
||||
bool exists{};
|
||||
auto res = is_directory(api_path, exists);
|
||||
if (res != api_error::success) {
|
||||
@ -336,9 +354,10 @@ auto base_provider::get_filesystem_item(
|
||||
return api_error::success;
|
||||
}
|
||||
|
||||
auto base_provider::get_filesystem_item_and_file(
|
||||
const std::string &api_path, api_file &file,
|
||||
filesystem_item &fsi) const -> api_error {
|
||||
auto base_provider::get_filesystem_item_and_file(const std::string &api_path,
|
||||
api_file &file,
|
||||
filesystem_item &fsi) const
|
||||
-> api_error {
|
||||
auto res = get_file(api_path, file);
|
||||
if (res != api_error::success) {
|
||||
return res;
|
||||
@ -394,6 +413,45 @@ auto base_provider::get_pinned_files() const -> std::vector<std::string> {
|
||||
return db3_->get_pinned_files();
|
||||
}
|
||||
|
||||
void base_provider::get_removed_items(std::deque<removed_item> &directories,
|
||||
std::deque<removed_item> &files,
|
||||
const stop_type &stop_requested) const {
|
||||
for (auto &&api_path : db3_->get_api_path_list()) {
|
||||
if (stop_requested) {
|
||||
return;
|
||||
}
|
||||
|
||||
api_meta_map meta{};
|
||||
if (get_item_meta(api_path, meta) != api_error::success) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (utils::string::to_bool(meta[META_DIRECTORY])) {
|
||||
bool exists{};
|
||||
if (is_directory(api_path, exists) != api_error::success) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (not exists) {
|
||||
directories.emplace_back(removed_item{api_path, true, ""});
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
bool exists{};
|
||||
if (is_file(api_path, exists) != api_error::success) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (exists) {
|
||||
continue;
|
||||
}
|
||||
|
||||
files.emplace_back(removed_item{api_path, false, meta[META_SOURCE]});
|
||||
}
|
||||
}
|
||||
|
||||
auto base_provider::get_total_item_count() const -> std::uint64_t {
|
||||
return db3_->get_total_item_count();
|
||||
}
|
||||
@ -413,108 +471,29 @@ auto base_provider::is_file_writeable(const std::string &api_path) const
|
||||
return not exists;
|
||||
}
|
||||
|
||||
void base_provider::remove_deleted_files(const stop_type &stop_requested) {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
if (not is_read_only()) {
|
||||
auto source_list =
|
||||
utils::file::directory{config_.get_cache_directory()}.get_files();
|
||||
for (auto &&source_file : source_list) {
|
||||
if (stop_requested) {
|
||||
return;
|
||||
}
|
||||
|
||||
filesystem_item fsi{};
|
||||
if (get_filesystem_item_from_source_path(source_file->get_path(), fsi) !=
|
||||
api_error::item_not_found) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto reference_time =
|
||||
source_file->get_time(config_.get_eviction_uses_accessed_time()
|
||||
? utils::file::time_type::accessed
|
||||
: utils::file::time_type::modified);
|
||||
if (not reference_time.has_value()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto delay = (config_.get_eviction_delay_mins() * 60UL) *
|
||||
utils::time::NANOS_PER_SECOND;
|
||||
if ((reference_time.value() + static_cast<std::uint64_t>(delay)) >=
|
||||
utils::time::get_time_now()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
event_system::instance().raise<orphaned_source_file_detected>(
|
||||
source_file->get_path());
|
||||
if (not source_file->remove()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
event_system::instance().raise<orphaned_source_file_removed>(
|
||||
source_file->get_path());
|
||||
}
|
||||
}
|
||||
|
||||
struct removed_item final {
|
||||
std::string api_path;
|
||||
bool directory{};
|
||||
std::string source_path;
|
||||
};
|
||||
|
||||
{
|
||||
api_file_list list{};
|
||||
std::string marker;
|
||||
auto res{api_error::more_data};
|
||||
while (not stop_requested && res == api_error::more_data) {
|
||||
res = get_file_list(list, marker);
|
||||
if (res != api_error::success && res != api_error::more_data) {
|
||||
utils::error::raise_error(function_name, res,
|
||||
"failed to get file list");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<removed_item> removed_list{};
|
||||
for (auto &&api_path : db3_->get_api_path_list()) {
|
||||
void base_provider::process_removed_directories(
|
||||
std::deque<removed_item> &removed_list, const stop_type &stop_requested) {
|
||||
for (auto &&item : removed_list) {
|
||||
if (stop_requested) {
|
||||
return;
|
||||
}
|
||||
|
||||
api_meta_map meta{};
|
||||
if (get_item_meta(api_path, meta) != api_error::success) {
|
||||
if (not item.directory) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (utils::string::to_bool(meta[META_DIRECTORY])) {
|
||||
bool exists{};
|
||||
if (is_directory(api_path, exists) != api_error::success) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (not exists) {
|
||||
removed_list.emplace_back(removed_item{api_path, true, ""});
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
bool exists{};
|
||||
if (is_file(api_path, exists) != api_error::success) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (exists) {
|
||||
continue;
|
||||
}
|
||||
|
||||
removed_list.emplace_back(removed_item{api_path, false, meta[META_SOURCE]});
|
||||
db3_->remove_api_path(item.api_path);
|
||||
event_system::instance().raise<directory_removed_externally>(
|
||||
item.api_path, item.source_path);
|
||||
}
|
||||
}
|
||||
|
||||
const auto orphaned_directory =
|
||||
void base_provider::process_removed_files(
|
||||
std::deque<removed_item> &removed_list, const stop_type &stop_requested) {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
auto orphaned_directory =
|
||||
utils::path::combine(config_.get_data_directory(), {"orphaned"});
|
||||
|
||||
for (auto &&item : removed_list) {
|
||||
if (stop_requested) {
|
||||
return;
|
||||
@ -535,8 +514,8 @@ void base_provider::remove_deleted_files(const stop_type &stop_requested) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto parts = utils::string::split(item.api_path, '/', false);
|
||||
const auto orphaned_file = utils::path::combine(
|
||||
auto parts = utils::string::split(item.api_path, '/', false);
|
||||
auto orphaned_file = utils::path::combine(
|
||||
orphaned_directory, {utils::path::strip_to_file_name(item.source_path) +
|
||||
'_' + parts[parts.size() - 1U]});
|
||||
|
||||
@ -567,20 +546,32 @@ void base_provider::remove_deleted_files(const stop_type &stop_requested) {
|
||||
event_system::instance().raise<file_removed_externally>(item.api_path,
|
||||
item.source_path);
|
||||
}
|
||||
}
|
||||
|
||||
for (auto &&item : removed_list) {
|
||||
if (stop_requested) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (not item.directory) {
|
||||
continue;
|
||||
}
|
||||
|
||||
db3_->remove_api_path(item.api_path);
|
||||
event_system::instance().raise<directory_removed_externally>(
|
||||
item.api_path, item.source_path);
|
||||
void base_provider::remove_deleted_items(const stop_type &stop_requested) {
|
||||
add_all_items(stop_requested);
|
||||
if (stop_requested) {
|
||||
return;
|
||||
}
|
||||
|
||||
remove_unmatched_source_files(stop_requested);
|
||||
if (stop_requested) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::deque<removed_item> directories;
|
||||
std::deque<removed_item> files;
|
||||
get_removed_items(directories, files, stop_requested);
|
||||
if (stop_requested) {
|
||||
return;
|
||||
}
|
||||
|
||||
process_removed_files(files, stop_requested);
|
||||
if (stop_requested) {
|
||||
return;
|
||||
}
|
||||
|
||||
process_removed_directories(directories, stop_requested);
|
||||
}
|
||||
|
||||
auto base_provider::remove_file(const std::string &api_path) -> api_error {
|
||||
@ -665,6 +656,51 @@ auto base_provider::remove_item_meta(const std::string &api_path,
|
||||
return db3_->remove_item_meta(api_path, key);
|
||||
}
|
||||
|
||||
void base_provider::remove_unmatched_source_files(
|
||||
const stop_type &stop_requested) {
|
||||
if (is_read_only()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto source_list =
|
||||
utils::file::directory{config_.get_cache_directory()}.get_files();
|
||||
for (auto &&source_file : source_list) {
|
||||
if (stop_requested) {
|
||||
return;
|
||||
}
|
||||
|
||||
filesystem_item fsi{};
|
||||
if (get_filesystem_item_from_source_path(source_file->get_path(), fsi) !=
|
||||
api_error::item_not_found) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto reference_time =
|
||||
source_file->get_time(config_.get_eviction_uses_accessed_time()
|
||||
? utils::file::time_type::accessed
|
||||
: utils::file::time_type::modified);
|
||||
if (not reference_time.has_value()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto delay = (config_.get_eviction_delay_mins() * 60UL) *
|
||||
utils::time::NANOS_PER_SECOND;
|
||||
if ((reference_time.value() + static_cast<std::uint64_t>(delay)) >=
|
||||
utils::time::get_time_now()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
event_system::instance().raise<orphaned_source_file_detected>(
|
||||
source_file->get_path());
|
||||
if (not source_file->remove()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
event_system::instance().raise<orphaned_source_file_removed>(
|
||||
source_file->get_path());
|
||||
}
|
||||
}
|
||||
|
||||
auto base_provider::set_item_meta(const std::string &api_path,
|
||||
const std::string &key,
|
||||
const std::string &value) -> api_error {
|
||||
@ -681,7 +717,7 @@ auto base_provider::start(api_item_added_callback api_item_added,
|
||||
api_item_added_ = api_item_added;
|
||||
fm_ = mgr;
|
||||
|
||||
db3_ = std::make_unique<meta_db>(config_);
|
||||
db3_ = create_meta_db(config_);
|
||||
|
||||
api_meta_map meta{};
|
||||
if (get_item_meta("/", meta) == api_error::item_not_found) {
|
||||
@ -715,8 +751,9 @@ auto base_provider::start(api_item_added_callback api_item_added,
|
||||
polling::instance().set_callback({
|
||||
"check_deleted",
|
||||
polling::frequency::low,
|
||||
[this](auto &&stop_requested) { remove_deleted_files(stop_requested); },
|
||||
[this](auto &&stop_requested) { remove_deleted_items(stop_requested); },
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -29,24 +29,29 @@ polling polling::instance_;
|
||||
void polling::frequency_thread(
|
||||
std::function<std::uint32_t()> get_frequency_seconds, frequency freq) {
|
||||
while (not stop_requested_) {
|
||||
std::deque<std::future<void>> futures;
|
||||
unique_mutex_lock lock(mutex_);
|
||||
for (const auto &item : items_) {
|
||||
if (item.second.freq == freq) {
|
||||
futures.emplace_back(
|
||||
std::async(std::launch::async, [this, &freq, item]() -> void {
|
||||
if (config_->get_event_level() == event_level::trace ||
|
||||
freq != frequency::second) {
|
||||
event_system::instance().raise<polling_item_begin>(item.first);
|
||||
}
|
||||
item.second.action(stop_requested_);
|
||||
if (config_->get_event_level() == event_level::trace ||
|
||||
freq != frequency::second) {
|
||||
event_system::instance().raise<polling_item_end>(item.first);
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
auto futures = std::accumulate(
|
||||
items_.begin(), items_.end(), std::deque<std::future<void>>{},
|
||||
[this, &freq](auto &&futures, auto &&item) {
|
||||
if (item.second.freq != freq) {
|
||||
return futures;
|
||||
}
|
||||
|
||||
futures.emplace_back(
|
||||
std::async(std::launch::async, [this, &freq, item]() -> void {
|
||||
if (config_->get_event_level() == event_level::trace ||
|
||||
freq != frequency::second) {
|
||||
event_system::instance().raise<polling_item_begin>(
|
||||
item.first);
|
||||
}
|
||||
item.second.action(stop_requested_);
|
||||
if (config_->get_event_level() == event_level::trace ||
|
||||
freq != frequency::second) {
|
||||
event_system::instance().raise<polling_item_end>(item.first);
|
||||
}
|
||||
}));
|
||||
return futures;
|
||||
});
|
||||
lock.unlock();
|
||||
|
||||
while (not futures.empty()) {
|
||||
@ -75,52 +80,55 @@ void polling::set_callback(const polling_item &item) {
|
||||
|
||||
void polling::start(app_config *config) {
|
||||
mutex_lock lock(start_stop_mutex_);
|
||||
if (high_frequency_thread_) {
|
||||
if (frequency_threads_.at(0U)) {
|
||||
return;
|
||||
}
|
||||
|
||||
event_system::instance().raise<service_started>("polling");
|
||||
config_ = config;
|
||||
stop_requested_ = false;
|
||||
high_frequency_thread_ = std::make_unique<std::thread>([this]() -> void {
|
||||
this->frequency_thread(
|
||||
[this]() -> std::uint32_t {
|
||||
return config_->get_high_frequency_interval_secs();
|
||||
},
|
||||
frequency::high);
|
||||
});
|
||||
low_frequency_thread_ = std::make_unique<std::thread>([this]() -> void {
|
||||
this->frequency_thread(
|
||||
[this]() -> std::uint32_t {
|
||||
return config_->get_low_frequency_interval_secs();
|
||||
},
|
||||
frequency::low);
|
||||
});
|
||||
second_frequency_thread_ = std::make_unique<std::thread>([this]() -> void {
|
||||
this->frequency_thread([]() -> std::uint32_t { return 1U; },
|
||||
frequency::second);
|
||||
});
|
||||
|
||||
auto idx{0U};
|
||||
frequency_threads_.at(idx++) =
|
||||
std::make_unique<std::thread>([this]() -> void {
|
||||
this->frequency_thread(
|
||||
[this]() -> std::uint32_t {
|
||||
return config_->get_high_frequency_interval_secs();
|
||||
},
|
||||
frequency::high);
|
||||
});
|
||||
frequency_threads_.at(idx++) =
|
||||
std::make_unique<std::thread>([this]() -> void {
|
||||
this->frequency_thread(
|
||||
[this]() -> std::uint32_t {
|
||||
return config_->get_low_frequency_interval_secs();
|
||||
},
|
||||
frequency::low);
|
||||
});
|
||||
frequency_threads_.at(idx++) =
|
||||
std::make_unique<std::thread>([this]() -> void {
|
||||
this->frequency_thread([]() -> std::uint32_t { return 1U; },
|
||||
frequency::second);
|
||||
});
|
||||
}
|
||||
|
||||
void polling::stop() {
|
||||
mutex_lock lock(start_stop_mutex_);
|
||||
if (not high_frequency_thread_) {
|
||||
if (not frequency_threads_.at(0U)) {
|
||||
return;
|
||||
}
|
||||
|
||||
event_system::instance().raise<service_shutdown_begin>("polling");
|
||||
stop_requested_ = true;
|
||||
|
||||
unique_mutex_lock lock2(mutex_);
|
||||
unique_mutex_lock thread_lock(mutex_);
|
||||
notify_.notify_all();
|
||||
lock2.unlock();
|
||||
thread_lock.unlock();
|
||||
|
||||
high_frequency_thread_->join();
|
||||
low_frequency_thread_->join();
|
||||
second_frequency_thread_->join();
|
||||
high_frequency_thread_.reset();
|
||||
low_frequency_thread_.reset();
|
||||
second_frequency_thread_.reset();
|
||||
for (auto &&thread : frequency_threads_) {
|
||||
thread->join();
|
||||
thread.reset();
|
||||
}
|
||||
|
||||
event_system::instance().raise<service_shutdown_end>("polling");
|
||||
}
|
||||
|
@ -27,10 +27,11 @@
|
||||
|
||||
#include "app_config.hpp"
|
||||
#include "comm/curl/curl_comm.hpp"
|
||||
#include "db/i_meta_db.hpp"
|
||||
#include "db/meta_db.hpp"
|
||||
#include "drives/fuse/fuse_drive.hpp"
|
||||
#include "platform/platform.hpp"
|
||||
#include "providers/encrypt/encrypt_provider.hpp"
|
||||
#include "providers/meta_db.hpp"
|
||||
#include "providers/s3/s3_provider.hpp"
|
||||
#include "providers/sia/sia_provider.hpp"
|
||||
#include "types/repertory.hpp"
|
||||
@ -76,7 +77,7 @@ public:
|
||||
static provider_type current_provider;
|
||||
static std::vector<std::string> drive_args;
|
||||
static std::vector<std::string> drive_args2;
|
||||
static std::unique_ptr<meta_db> meta;
|
||||
static std::unique_ptr<i_meta_db> meta;
|
||||
static std::string mount_location;
|
||||
static std::string mount_location2;
|
||||
|
||||
@ -122,7 +123,7 @@ protected:
|
||||
});
|
||||
}
|
||||
|
||||
meta = std::make_unique<meta_db>(*config);
|
||||
meta = create_meta_db(*config);
|
||||
execute_mount(drive_args, mount_location);
|
||||
};
|
||||
|
||||
@ -164,7 +165,7 @@ protected:
|
||||
});
|
||||
}
|
||||
|
||||
meta = std::make_unique<meta_db>(*config);
|
||||
meta = create_meta_db(*config);
|
||||
execute_mount(drive_args, mount_location);
|
||||
};
|
||||
|
||||
@ -257,8 +258,8 @@ public:
|
||||
return file_path;
|
||||
}
|
||||
|
||||
static auto create_file_and_test(std::string &file_name,
|
||||
mode_t perms) -> std::string {
|
||||
static auto create_file_and_test(std::string &file_name, mode_t perms)
|
||||
-> std::string {
|
||||
file_name += std::to_string(++idx);
|
||||
auto file_path = utils::path::combine(mount_location, {file_name});
|
||||
|
||||
@ -276,7 +277,7 @@ public:
|
||||
EXPECT_TRUE(utils::file::file(file_path).exists());
|
||||
EXPECT_FALSE(utils::file::directory(file_path).exists());
|
||||
|
||||
struct stat64 unix_st {};
|
||||
struct stat64 unix_st{};
|
||||
EXPECT_EQ(0, stat64(file_path.c_str(), &unix_st));
|
||||
EXPECT_EQ(getgid(), unix_st.st_gid);
|
||||
EXPECT_EQ(getuid(), unix_st.st_uid);
|
||||
@ -288,8 +289,8 @@ public:
|
||||
return create_file_and_test(file_name, ACCESSPERMS);
|
||||
}
|
||||
|
||||
static auto create_directory_and_test(std::string &dir_name,
|
||||
mode_t perms) -> std::string {
|
||||
static auto create_directory_and_test(std::string &dir_name, mode_t perms)
|
||||
-> std::string {
|
||||
dir_name += std::to_string(++idx);
|
||||
|
||||
auto dir_path = utils::path::combine(mount_location, {dir_name});
|
||||
@ -298,7 +299,7 @@ public:
|
||||
EXPECT_TRUE(utils::file::directory(dir_path).exists());
|
||||
EXPECT_FALSE(utils::file::file(dir_path).exists());
|
||||
|
||||
struct stat64 unix_st {};
|
||||
struct stat64 unix_st{};
|
||||
EXPECT_EQ(0, stat64(dir_path.c_str(), &unix_st));
|
||||
EXPECT_EQ(getgid(), unix_st.st_gid);
|
||||
EXPECT_EQ(getuid(), unix_st.st_uid);
|
||||
@ -391,7 +392,7 @@ template <typename provider_t>
|
||||
std::vector<std::string> fuse_test<provider_t>::drive_args2;
|
||||
|
||||
template <typename provider_t>
|
||||
std::unique_ptr<meta_db> fuse_test<provider_t>::meta{};
|
||||
std::unique_ptr<i_meta_db> fuse_test<provider_t>::meta{};
|
||||
|
||||
template <typename provider_t>
|
||||
std::string fuse_test<provider_t>::mount_location;
|
||||
|
@ -31,7 +31,10 @@ if [ "${PROJECT_IS_MINGW}" == "1" ] && [ "${PROJECT_STATIC_LINK}" == "OFF" ]; th
|
||||
fi
|
||||
|
||||
if [ "${PROJECT_ENABLE_CURL}" == "ON" ]; then
|
||||
PROJECT_MINGW64_COPY_DEPENDENCIES+=(/mingw64/bin/libcurl*.dll)
|
||||
PROJECT_MINGW64_COPY_DEPENDENCIES+=(
|
||||
/mingw64/bin/libcurl*.dll
|
||||
/mingw64/bin/libnghttp2-*.dll
|
||||
)
|
||||
fi
|
||||
|
||||
if [ "${PROJECT_ENABLE_FLAC}" == "ON" ]; then
|
||||
|
Reference in New Issue
Block a user