#include "DataTransfer.h" #include #include "ServerLogger.h" DataTransfer::DataTransfer(const std::string& tr) { temp_root = tr; } void DataTransfer::Init(const std::string& tr) { temp_root = tr; SCLOG_DEBUG("GDT-TempRoot: %s", temp_root.c_str()); } bool DataTransfer::ItemExist(const std::string& file) { return (files.find(file) != files.end()); } bool DataTransfer::InsertItem(const std::string& file) { if (ItemExist(file)) return false; files.insert({ file, true }); SCLOG_INFO("GDT: Item [%s] Inserted", file.c_str()); return true; } bool DataTransfer::RemoveItem(const std::string& file) { if (!ItemExist(file)) return false; files.erase(file); SCLOG_INFO("GDT: Item [%s] Removed", file.c_str()); return true; } bool DataTransfer::ItemValidate(const std::string& file) { if (!ItemExist(file)) return false; return files[file]; } void DataTransfer::DeactivateItem(const std::string& file) { if (!ItemExist(file)) return; files[file] = false; SCLOG_INFO("GDT: Item [%s] Deactivated", file.c_str()); } bool DataTransfer::CopyItemTo(const std::string& file, const std::string& dest_path) { if (!ItemExist(file)) return false; namespace fs = std::filesystem; std::error_code error; if(!fs::exists(dest_path)) { SCLOG_ERROR("GDT: Can't Copy File (Target Path [%s] Not Exist)", dest_path.c_str()); return false; } fs::path dest(dest_path); fs::path dest_file = dest / file; if(fs::copy_file(MakePath(file), dest_file, fs::copy_options::overwrite_existing, error)) { SCLOG_INFO("GDT: File Copied To [%s]", dest_file.c_str()); return true; } else { SCLOG_ERROR("Failed To Delete File [%s], Error: %s (%d)", dest_file.c_str(), error.message().c_str(), error.value()); return false; } } bool DataTransfer::CopyItemAS(const std::string& file, const std::string& dest) { if (!ItemExist(file)) return false; namespace fs = std::filesystem; std::error_code error; fs::path dest_file = dest; if(fs::copy_file(MakePath(file), dest_file, fs::copy_options::overwrite_existing, error)) { SCLOG_INFO("GDT: File Copied To [%s]", dest_file.c_str()); return true; } else { SCLOG_ERROR("Failed To Delete File [%s], Error: %s (%d)", dest_file.c_str(), error.message().c_str(), error.value()); return false; } } bool DataTransfer::RemoveAllCacheFiles() { SCLOG_INFO("Begin Cache Clear"); namespace fs = std::filesystem; bool result = true; std::error_code error; if (files.empty()) { SCLOG_INFO("No Cache File Found, Skip GDT Clear"); goto fs_scan; } for (const auto& [file, status] : files) { std::string filepath = MakePath(file); if (!fs::exists(filepath, error)) { SCLOG_WARNING("File [%s] Not Exist, Skip", filepath.c_str()); continue; } if (fs::remove(filepath, error)) SCLOG_INFO("File [%s] Deleted", filepath.c_str()); else { SCLOG_ERROR("Failed To Delete File [%s], Error: %s (%d)", filepath.c_str(), error.message().c_str(), error.value()); result = false; } } if (result) files.clear(); fs_scan: SCLOG_INFO("Cache Clear Process (GDT) Finished, Begin Filesystem Scan"); int dirs = 0, files = 0, dirs_deleted = 0, files_deleted = 0; for (const auto& entry : fs::directory_iterator(temp_root, error)) { if (entry.is_directory()) { dirs++; try { if (fs::remove_all(entry.path(), error)) { dirs_deleted++; SCLOG_WARNING("Found Directory [%s] In Cache Directory, Deleted", entry.path().string().c_str()); } else SCLOG_ERROR("Found Directory [%s] In Cache Directory, Failed To Delete. Error: %s (%d)", entry.path().string().c_str(), error.message().c_str(), error.value()); } catch (const std::exception& e) { SCLOG_ERROR("Found Directory [%s] In Cache Directory, Failed To Delete. Exception: %s", entry.path().string().c_str(), e.what()); } } else if (entry.is_regular_file()) { files++; if (fs::remove(entry.path(), error)) { files_deleted++; SCLOG_INFO("File [%s] Deleted", entry.path().string().c_str()); } else { SCLOG_ERROR("Failed To Delete File [%s], Error: %s (%d)", entry.path().string().c_str(), error.message().c_str(), error.value()); result = false; } } } SCLOG_INFO("Filesystem Scan Finished, %d/%d Dir(s) And %d/%d Files(s) Deleted", dirs_deleted, dirs, files_deleted, files); SCLOG_INFO("Cache Clear Finished"); return result; } std::string DataTransfer::MakePath(const std::string& file) { if (temp_root.empty() || (!ItemExist(file))) return ""; if (temp_root[temp_root.size() - 1] == '/') return temp_root + file; else return temp_root + "/" + file; } DataTransfer GlobalDataTransfer;