This commit is contained in:
Scott E. Graves 2023-12-13 15:21:21 -06:00
parent ff13633962
commit 7112fbee7e
2 changed files with 177 additions and 161 deletions

View File

@ -65,10 +65,17 @@ private:
static void create_item_meta(api_meta_map &meta, bool directory, static void create_item_meta(api_meta_map &meta, bool directory,
const api_file &file); const api_file &file);
auto do_directory_operation(
const std::string &api_path,
std::function<api_error(const encrypt_config &cfg,
const std::string &source_path)>
callback) const -> api_error;
auto auto
process_directory_entry(const std::filesystem::directory_entry &dir_entry, process_directory_entry(const std::filesystem::directory_entry &dir_entry,
const encrypt_config &cfg, const encrypt_config &cfg,
std::string &api_path) const -> bool; std::string &api_path) const -> bool;
void remove_deleted_files(); void remove_deleted_files();
public: public:

View File

@ -169,6 +169,45 @@ auto encrypt_provider::create_directory(const std::string &api_path,
return api_error::not_implemented; return api_error::not_implemented;
} }
auto encrypt_provider::do_directory_operation(
const std::string &api_path,
std::function<api_error(const encrypt_config &cfg,
const std::string &source_path)>
callback) const -> api_error {
bool exists{};
auto res = is_file(api_path, exists);
if (res != api_error::success) {
return res;
}
if (exists) {
return api_error::item_exists;
}
auto cfg = config_.get_encrypt_config();
std::string source_path{api_path};
if (api_path != "/") {
res =
utils::encryption::decrypt_file_path(cfg.encryption_token, source_path);
if (res != api_error::success) {
return res;
}
}
source_path =
utils::path::absolute(utils::path::combine(cfg.path, {source_path}));
if (source_path != cfg.path &&
not source_path.starts_with(cfg.path +
utils::path::directory_seperator)) {
return api_error::directory_not_found;
}
if (not utils::file::is_directory(source_path)) {
return api_error::directory_not_found;
}
return callback(cfg, source_path);
}
auto encrypt_provider::get_api_path_from_source(const std::string &source_path, auto encrypt_provider::get_api_path_from_source(const std::string &source_path,
std::string &api_path) const std::string &api_path) const
-> api_error { -> api_error {
@ -210,173 +249,143 @@ auto encrypt_provider::get_api_path_from_source(const std::string &source_path,
auto encrypt_provider::get_directory_item_count( auto encrypt_provider::get_directory_item_count(
const std::string &api_path) const -> std::uint64_t { const std::string &api_path) const -> std::uint64_t {
auto result = db::db_select{*db_, source_table} static const auto *function_name = __FUNCTION__;
.column("source_path")
.where("api_path")
.equals(api_path)
.go();
std::optional<db::db_select::row> row;
if (not(result.get_row(row) && row.has_value())) {
return 0U;
}
auto source_path = row->get_column("source_path").get_value<std::string>();
result = db::db_select{*db_, directory_table}
.column("api_path")
.where("source_path")
.equals(source_path)
.go();
if (not result.has_row()) {
return 0U;
}
const auto cfg = config_.get_encrypt_config();
std::uint64_t count{}; std::uint64_t count{};
try { auto res = do_directory_operation(
for ([[maybe_unused]] const auto &dir_entry : api_path,
std::filesystem::directory_iterator(source_path)) { [&api_path, &count](const encrypt_config & /* cfg */,
count++; const std::string &source_path) -> api_error {
} try {
for ([[maybe_unused]] const auto &dir_entry :
return count; std::filesystem::directory_iterator(source_path)) {
} catch (const std::exception &ex) { count++;
utils::error::raise_error(__FUNCTION__, ex, cfg.path, }
"failed to get directory item count"); } catch (const std::exception &ex) {
utils::error::raise_api_path_error(
function_name, api_path, source_path, ex,
"failed to get directory item count");
}
return api_error::success;
});
if (res != api_error::success) {
utils::error::raise_api_path_error(function_name, api_path, res,
"failed to get directory item count");
} }
return 0U; return count;
} }
auto encrypt_provider::get_directory_items(const std::string &api_path, auto encrypt_provider::get_directory_items(const std::string &api_path,
directory_item_list &list) const directory_item_list &list) const
-> api_error { -> api_error {
bool exists{}; static const auto *function_name = __FUNCTION__;
auto res = is_file(api_path, exists);
if (res != api_error::success) {
return res;
}
if (exists) {
return api_error::item_exists;
}
auto cfg = config_.get_encrypt_config(); return do_directory_operation(
std::string source_path{api_path}; api_path,
if (api_path != "/") { [this, &list](const encrypt_config &cfg,
res = const std::string &source_path) -> api_error {
utils::encryption::decrypt_file_path(cfg.encryption_token, source_path); try {
if (res != api_error::success) { for (const auto &dir_entry :
return res; std::filesystem::directory_iterator(source_path)) {
} try {
} std::string current_api_path{};
if (dir_entry.is_directory()) {
auto result = db::db_select{*db_, directory_table}
.column("api_path")
.where("source_path")
.equals(dir_entry.path().string())
.go();
std::optional<db::db_select::row> row;
if (result.get_row(row) && row.has_value()) {
current_api_path =
row->get_column("api_path").get_value<std::string>();
}
if (current_api_path.empty()) {
process_directory_entry(dir_entry, cfg, current_api_path);
source_path = result = db::db_select{*db_, directory_table}
utils::path::absolute(utils::path::combine(cfg.path, {source_path})); .column("api_path")
if (source_path != cfg.path && .where("source_path")
not source_path.starts_with(cfg.path + .equals(dir_entry.path().string())
utils::path::directory_seperator)) { .go();
return api_error::directory_not_found; row.reset();
} if (not(result.get_row(row) && row.has_value())) {
continue;
}
if (not utils::file::is_directory(source_path)) { current_api_path =
return api_error::directory_not_found; row->get_column("api_path").get_value<std::string>();
} }
} else {
std::string api_path_data{};
auto result = db::db_select{*db_, file_table}
.column("data")
.where("source_path")
.equals(dir_entry.path().string())
.go();
std::optional<db::db_select::row> row;
if (result.get_row(row) && row.has_value()) {
api_path_data =
row->get_column("data").get_value<std::string>();
}
try { if (api_path_data.empty()) {
for (const auto &dir_entry : if (not process_directory_entry(dir_entry, cfg,
std::filesystem::directory_iterator(source_path)) { current_api_path)) {
try { continue;
std::string current_api_path{}; }
if (dir_entry.is_directory()) { } else {
auto result = db::db_select{*db_, directory_table} current_api_path = json::parse(api_path_data)
.column("api_path") .at("api_path")
.where("source_path") .get<std::string>();
.equals(dir_entry.path().string()) }
.go(); }
std::optional<db::db_select::row> row;
if (result.get_row(row) && row.has_value()) {
current_api_path =
row->get_column("api_path").get_value<std::string>();
}
if (current_api_path.empty()) {
process_directory_entry(dir_entry, cfg, current_api_path);
result = db::db_select{*db_, directory_table} auto file =
.column("api_path") create_api_file(current_api_path, dir_entry.is_directory(),
.where("source_path") dir_entry.path().string());
.equals(dir_entry.path().string())
.go(); directory_item dir_item{};
row.reset(); dir_item.api_parent = file.api_parent;
if (not(result.get_row(row) && row.has_value())) { dir_item.api_path = file.api_path;
continue; dir_item.directory = dir_entry.is_directory();
dir_item.resolved = true;
dir_item.size = file.file_size;
create_item_meta(dir_item.meta, dir_item.directory, file);
list.emplace_back(std::move(dir_item));
} catch (const std::exception &ex) {
utils::error::raise_error(function_name, ex,
dir_entry.path().string(),
"failed to process directory item");
} }
current_api_path =
row->get_column("api_path").get_value<std::string>();
}
} else {
std::string api_path_data{};
auto result = db::db_select{*db_, file_table}
.column("data")
.where("source_path")
.equals(dir_entry.path().string())
.go();
std::optional<db::db_select::row> row;
if (result.get_row(row) && row.has_value()) {
api_path_data = row->get_column("data").get_value<std::string>();
}
if (api_path_data.empty()) {
if (not process_directory_entry(dir_entry, cfg, current_api_path)) {
continue;
}
} else {
current_api_path =
json::parse(api_path_data).at("api_path").get<std::string>();
} }
} catch (const std::exception &ex) {
utils::error::raise_error(function_name, ex, source_path,
"failed to get directory items");
return api_error::error;
} }
auto file = create_api_file(current_api_path, dir_entry.is_directory(), std::sort(list.begin(), list.end(),
dir_entry.path().string()); [](const auto &item1, const auto &item2) -> bool {
return (item1.directory && not item2.directory) ||
(not(item2.directory && not item1.directory) &&
(item1.api_path.compare(item2.api_path) < 0));
});
directory_item dir_item{}; list.insert(list.begin(), directory_item{
dir_item.api_parent = file.api_parent; "..",
dir_item.api_path = file.api_path; "",
dir_item.directory = dir_entry.is_directory(); true,
dir_item.resolved = true; });
dir_item.size = file.file_size; list.insert(list.begin(), directory_item{
create_item_meta(dir_item.meta, dir_item.directory, file); ".",
"",
list.emplace_back(std::move(dir_item)); true,
} catch (const std::exception &ex) { });
utils::error::raise_error(__FUNCTION__, ex, dir_entry.path().string(), return api_error::success;
"failed to process directory item"); });
}
}
} catch (const std::exception &ex) {
utils::error::raise_error(__FUNCTION__, ex, source_path,
"failed to get directory items");
return api_error::error;
}
std::sort(list.begin(), list.end(), [](const auto &a, const auto &b) -> bool {
return (a.directory && not b.directory) ||
(not(b.directory && not a.directory) &&
(a.api_path.compare(b.api_path) < 0));
});
list.insert(list.begin(), directory_item{
"..",
"",
true,
});
list.insert(list.begin(), directory_item{
".",
"",
true,
});
return api_error::success;
} }
auto encrypt_provider::get_file(const std::string &api_path, auto encrypt_provider::get_file(const std::string &api_path,
@ -857,12 +866,12 @@ auto encrypt_provider::read_file_bytes(const std::string &api_path,
const auto relative_path = const auto relative_path =
std::filesystem::path(source_path).lexically_relative(cfg.path); std::filesystem::path(source_path).lexically_relative(cfg.path);
auto ri = std::make_shared<reader_info>(); auto info = std::make_shared<reader_info>();
ri->reader = std::make_unique<utils::encryption::encrypting_reader>( info->reader = std::make_unique<utils::encryption::encrypting_reader>(
relative_path.filename().string(), source_path, stop_requested, relative_path.filename().string(), source_path, stop_requested,
cfg.encryption_token, relative_path.parent_path().string()); cfg.encryption_token, relative_path.parent_path().string());
reader_lookup_[source_path] = ri; reader_lookup_[source_path] = info;
iv_list = ri->reader->get_iv_list(); iv_list = info->reader->get_iv_list();
file_data["original_file_size"] = file_size; file_data["original_file_size"] = file_size;
file_data["iv_list"] = iv_list; file_data["iv_list"] = iv_list;
@ -882,11 +891,11 @@ auto encrypt_provider::read_file_bytes(const std::string &api_path,
std::array<unsigned char, std::array<unsigned char,
crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>>>(); crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>>>();
if (reader_lookup_.find(source_path) == reader_lookup_.end()) { if (reader_lookup_.find(source_path) == reader_lookup_.end()) {
auto ri = std::make_shared<reader_info>(); auto info = std::make_shared<reader_info>();
ri->reader = std::make_unique<utils::encryption::encrypting_reader>( info->reader = std::make_unique<utils::encryption::encrypting_reader>(
api_path, source_path, stop_requested, cfg.encryption_token, api_path, source_path, stop_requested, cfg.encryption_token,
std::move(iv_list)); std::move(iv_list));
reader_lookup_[source_path] = ri; reader_lookup_[source_path] = info;
} }
} }
@ -894,16 +903,16 @@ auto encrypt_provider::read_file_bytes(const std::string &api_path,
return api_error::success; return api_error::success;
} }
auto ri = reader_lookup_.at(source_path); auto info = reader_lookup_.at(source_path);
ri->last_access_time = std::chrono::system_clock::now(); info->last_access_time = std::chrono::system_clock::now();
reader_lookup_lock.unlock(); reader_lookup_lock.unlock();
mutex_lock reader_lock(ri->reader_mtx); mutex_lock reader_lock(info->reader_mtx);
ri->reader->set_read_position(offset); info->reader->set_read_position(offset);
data.resize(size); data.resize(size);
const auto res = ri->reader->reader_function(data.data(), 1u, data.size(), const auto res = info->reader->reader_function(data.data(), 1U, data.size(),
ri->reader.get()); info->reader.get());
if (res == 0) { if (res == 0) {
return api_error::os_error; return api_error::os_error;
} }