JKSV/source/tasks/fileoptions.cpp
2025-09-06 12:18:29 -04:00

73 lines
2.6 KiB
C++

#include "tasks/fileoptions.hpp"
#include "appstates/MessageState.hpp"
#include "error.hpp"
#include "fs/fs.hpp"
#include "fslib.hpp"
#include "strings/strings.hpp"
#include "stringutil.hpp"
void tasks::fileoptions::copy_source_to_destination(sys::ProgressTask *task, FileOptionState::TaskData taskData)
{
if (error::is_null(task)) { return; }
const fslib::Path &source = taskData->sourcePath;
const fslib::Path &dest = taskData->destPath;
const int64_t journalSpace = taskData->journalSize;
FileOptionState *spawningState = taskData->spawningState;
const bool sourceIsDir = fslib::directory_exists(source);
bool destError = false;
if (sourceIsDir) { destError = error::fslib(fslib::create_directories_recursively(dest)); }
else
{
const size_t subDest = dest.find_last_of('/');
if (subDest != dest.NOT_FOUND && subDest > 1)
{
fslib::Path subDestPath{dest.sub_path(subDest)};
destError = error::fslib(fslib::create_directories_recursively(subDestPath));
}
}
if (destError) { TASK_FINISH_RETURN(task); }
const bool needsCommit = journalSpace > 0;
if (sourceIsDir && needsCommit) { fs::copy_directory_commit(source, dest, journalSpace, task); }
else if (!sourceIsDir && needsCommit) { fs::copy_file_commit(source, dest, journalSpace, task); }
else if (sourceIsDir && !needsCommit) { fs::copy_directory(source, dest, task); }
else if (!sourceIsDir && !needsCommit) { fs::copy_file(source, dest, task); }
spawningState->update_destination();
task->complete();
}
void tasks::fileoptions::delete_target(sys::Task *task, FileOptionState::TaskData taskData)
{
if (error::is_null(task)) { return; }
// Gonna borrow this. No point in duplicating it.
const char *deletingFormat = strings::get_by_name(strings::names::IO_STATUSES, 3);
const fslib::Path &target = taskData->sourcePath;
const int64_t journalSpace = taskData->journalSize;
FileOptionState *spawningState = taskData->spawningState;
{
const char *filename = target.get_filename();
const std::string status = stringutil::get_formatted_string(deletingFormat, filename);
task->set_status(status);
}
const bool isDir = fslib::directory_exists(target);
bool needsCommit = journalSpace > 0;
if (isDir) { fslib::delete_directory_recursively(target); }
else { fslib::delete_file(target); }
if (needsCommit) { fslib::commit_data_to_file_system(target.get_device_name()); }
spawningState->update_source();
task->complete();
}