This commit is contained in:
2024-10-18 07:36:52 -05:00
parent ae0a921ba8
commit a0a5ca3390
3 changed files with 87 additions and 21 deletions

View File

@ -161,7 +161,7 @@ auto directory::create_directory(std::string_view path) const
std::string{abs_path} + '|' +
std::to_string(res));
}
#else // !defined(_WIN32)
#else // !defined(_WIN32)
auto ret{true};
auto paths =
utils::string::split(abs_path, utils::path::directory_seperator, false);
@ -377,10 +377,16 @@ auto directory::remove() -> bool {
return utils::retry_action([this]() -> bool {
try {
#if defined(_WIN32)
return not exists() || (::RemoveDirectoryA(path_.c_str()) != 0);
auto ret = not exists() || (::RemoveDirectoryA(path_.c_str()) != 0);
#else // !defined(_WIN32)
return not exists() || (rmdir(path_.c_str()) == 0);
auto ret = not exists() || (rmdir(path_.c_str()) == 0);
#endif // defined(_WIN32)
if (not ret) {
utils::error::handle_error(function_name,
"failed to remove directory|" + path_);
}
return ret;
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {

View File

@ -25,6 +25,7 @@
#include "utils/common.hpp"
#include "utils/error.hpp"
#include "utils/path.hpp"
#include <utils/config.hpp>
namespace {
[[nodiscard]] auto get_file_size(std::string_view path,
@ -397,15 +398,31 @@ auto file::sha256() -> std::optional<std::string> {
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
auto file::remove() -> bool {
REPERTORY_USES_FUNCTION_NAME();
close();
return utils::retry_action([this]() -> bool {
try {
#if defined(_WIN32)
return not exists() || (::DeleteFileA(path_.c_str()) != 0);
auto ret = not exists() || (::DeleteFileA(path_.c_str()) != 0);
#else // !defined(_WIN32)
std::error_code ec{};
return not exists() || std::filesystem::remove(path_, ec);
std::error_code ec{};
auto ret = not exists() || std::filesystem::remove(path_, ec);
#endif // defined(_WIN32)
if (not ret) {
utils::error::handle_error(function_name,
"failed to remove file|" + path_);
}
return ret;
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
utils::error::handle_exception(function_name);
}
return false;
});
}
@ -458,17 +475,18 @@ auto file::write(const unsigned char *data, std::size_t to_write,
std::size_t bytes_written{0U};
while (bytes_written != to_write) {
res = fwrite(reinterpret_cast<const char *>(&data[bytes_written]), 1U,
to_write - bytes_written, file_.get());
auto written =
fwrite(reinterpret_cast<const char *>(&data[bytes_written]), 1U,
to_write - bytes_written, file_.get());
if (not feof(file_.get()) && ferror(file_.get())) {
throw std::runtime_error("failed to write file bytes");
}
if (res == 0) {
if (written == 0U) {
break;
}
bytes_written += static_cast<std::size_t>(res);
bytes_written += res;
}
flush();