Files
UNSWebServerCore_WindowsDLL/UNSWebServerCore/DataTransfer.cpp
T

180 lines
4.4 KiB
C++

#include "DataTransfer.h"
#include <filesystem>
#include "ServerLogger.h"
DataTransfer::DataTransfer(const std::string& tr)
{
temp_root = tr;
}
void DataTransfer::Init(const std::string& tr)
{
temp_root = tr;
SCLOGF_DEBUG("GDT-TempRoot: {}", temp_root);
}
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 });
SCLOGF_INFO("GDT: Item [{}] Inserted", file);
return true;
}
bool DataTransfer::RemoveItem(const std::string& file)
{
if (!ItemExist(file))
return false;
files.erase(file);
SCLOGF_INFO("GDT: Item [{}] Removed", file);
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;
SCLOGF_INFO("GDT: Item [{}] Deactivated", file);
}
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))
{
SCLOGF_ERROR("GDT: Can't Copy File (Target Path [{}] Not Exist)", dest_path);
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))
{
SCLOGF_INFO("GDT: File Copied To [{}]", dest_file);
return true;
}
else
{
SCLOGF_ERROR("Failed To Delete File [{}], Error: {} ({})", dest_file, error.message(), 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))
{
SCLOGF_INFO("GDT: File Copied To [{}]", dest_file);
return true;
}
else
{
SCLOGF_ERROR("Failed To Delete File [{}], Error: {} ({})", dest_file, error.message(), 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))
{
SCLOGF_WARNING("File [{}] Not Exist, Skip", filepath);
continue;
}
if (fs::remove(filepath, error))
SCLOGF_INFO("File [{}] Deleted", filepath);
else
{
SCLOGF_ERROR("Failed To Delete File [{}], Error: {} ({})", filepath, error.message(), 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++;
SCLOGF_WARNING("Found Directory [{}] In Cache Directory, Deleted", entry.path().string());
}
else
SCLOGF_ERROR("Found Directory [{}] In Cache Directory, Failed To Delete. Error: {} ({})", entry.path().string(), error.message(), error.value());
}
catch (const std::exception& e)
{
SCLOGF_ERROR("Found Directory [{}] In Cache Directory, Failed To Delete. Exception: {}", entry.path().string(), e.what());
}
}
else if (entry.is_regular_file())
{
files++;
if (fs::remove(entry.path(), error))
{
files_deleted++;
SCLOGF_INFO("File [{}] Deleted", entry.path().string());
}
else
{
SCLOGF_ERROR("Failed To Delete File [{}], Error: {} ({})", entry.path().string(), error.message(), error.value());
result = false;
}
}
}
SCLOGF_INFO("Filesystem Scan Finished, {}/{} Dir(s) And {}/{} 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;