This commit is contained in:
Scott E. Graves 2024-09-23 20:22:43 -05:00
parent dfb9d78448
commit c4326520cd
2 changed files with 75 additions and 50 deletions

View File

@ -223,11 +223,6 @@ E_SIMPLE1(orphaned_file_detected, warn, true,
std::string, source, src, E_STRING
);
E_SIMPLE2(orphaned_file_processed, warn, true,
std::string, source, src, E_STRING,
std::string, dest, dest, E_STRING
);
E_SIMPLE3(orphaned_file_processing_failed, error, true,
std::string, source, src, E_STRING,
std::string, dest, dest, E_STRING,

View File

@ -481,51 +481,64 @@ void base_provider::remove_deleted_files() {
}
for (auto &&item : removed_list) {
if (not item.directory) {
if (utils::file::file(item.source_path).exists()) {
const auto orphaned_directory =
utils::path::combine(config_.get_data_directory(), {"orphaned"});
if (utils::file::directory(orphaned_directory).create_directory()) {
const auto parts = utils::string::split(item.api_path, '/', false);
const auto orphaned_file = utils::path::combine(
orphaned_directory,
{utils::path::strip_to_file_name(item.source_path) + '_' +
parts[parts.size() - 1U]});
event_system::instance().raise<orphaned_file_detected>(
item.source_path);
if (utils::file::reset_modified_time(item.source_path) &&
utils::file::file(item.source_path)
.copy_to(orphaned_file, true)) {
event_system::instance().raise<orphaned_file_processed>(
item.source_path, orphaned_file);
} else {
event_system::instance().raise<orphaned_file_processing_failed>(
item.source_path, orphaned_file,
std::to_string(utils::get_last_error_code()));
}
} else {
utils::error::raise_error(
function_name, std::to_string(utils::get_last_error_code()),
"failed to create orphaned directory|sp|" + orphaned_directory);
continue;
}
}
if (fm_->evict_file(item.api_path)) {
db3_->remove_api_path(item.api_path);
event_system::instance().raise<file_removed_externally>(
item.api_path, item.source_path);
}
if (item.directory) {
continue;
}
if (not utils::file::file(item.source_path).exists()) {
continue;
}
const auto orphaned_directory =
utils::path::combine(config_.get_data_directory(), {"orphaned"});
if (not utils::file::directory(orphaned_directory).create_directory()) {
utils::error::raise_error(
function_name, std::to_string(utils::get_last_error_code()),
"failed to create orphaned directory|sp|" + orphaned_directory);
break;
}
const auto parts = utils::string::split(item.api_path, '/', false);
const auto orphaned_file = utils::path::combine(
orphaned_directory, {utils::path::strip_to_file_name(item.source_path) +
'_' + parts[parts.size() - 1U]});
event_system::instance().raise<orphaned_file_detected>(item.source_path);
if (not utils::file::reset_modified_time(item.source_path)) {
event_system::instance().raise<orphaned_file_processing_failed>(
item.source_path, orphaned_file,
"reset modified failed|" +
std::to_string(utils::get_last_error_code()));
continue;
}
if (not utils::file::file(item.source_path).copy_to(orphaned_file, true)) {
[[maybe_unused]] auto removed = utils::file::file{orphaned_file}.remove();
event_system::instance().raise<orphaned_file_processing_failed>(
item.source_path, orphaned_file,
"copy failed|" + std::to_string(utils::get_last_error_code()));
continue;
}
if (not fm_->evict_file(item.api_path)) {
event_system::instance().raise<orphaned_file_processing_failed>(
item.source_path, orphaned_file, "eviction failed");
continue;
}
db3_->remove_api_path(item.api_path);
event_system::instance().raise<file_removed_externally>(item.api_path,
item.source_path);
}
for (auto &&item : removed_list) {
if (item.directory) {
db3_->remove_api_path(item.api_path);
event_system::instance().raise<directory_removed_externally>(
item.api_path, item.source_path);
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);
}
auto source_list =
@ -534,12 +547,29 @@ void base_provider::remove_deleted_files() {
filesystem_item fsi{};
if (get_filesystem_item_from_source_path(source_file->get_path(), fsi) !=
api_error::item_not_found) {
event_system::instance().raise<orphaned_source_file_detected>(
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 (source_file->remove()) {
event_system::instance().raise<orphaned_source_file_removed>(
source_file->get_path());
if (source_file->remove()) {
event_system::instance().raise<orphaned_source_file_removed>(
source_file->get_path());
}
}
}
}