This commit is contained in:
2024-08-07 13:12:56 -05:00
parent b4999323da
commit eaae2fea77
6 changed files with 45 additions and 30 deletions

View File

@ -28,31 +28,42 @@
namespace repertory::utils::file {
auto file::open_file(std::filesystem::path path, bool read_only) -> file {
path = utils::path::absolute(path.string());
if (not is_file(path.string())) {
throw std::runtime_error("file not found: " + path.string());
}
if (not read_only) {
#if defined(_WIN32)
_chmod(path.string().c_str(), 0600U);
#else // !defined(_WIN32)
chmod(path.string().c_str(), 0600U);
#endif // defined(_WIN32)
}
#if defined(_WIN32)
auto *ptr =
_fsopen(path.string().c_str(), read_only ? "rb" : "rb+", _SH_DENYNO);
std::cout << errno << std::endl;
#else // !defined(_WIN32)
auto *ptr = fopen(path.string().c_str(), read_only ? "rb" : "rb+");
#endif // defined(_WIN32)
return file{
file_t{ptr},
path,
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
try {
path = utils::path::absolute(path.string());
if (not is_file(path.string())) {
throw std::runtime_error("file not found: " + path.string());
}
if (not read_only) {
#if defined(_WIN32)
_chmod(path.string().c_str(), 0600U);
#else // !defined(_WIN32)
chmod(path.string().c_str(), 0600U);
#endif // defined(_WIN32)
}
#if defined(_WIN32)
auto *ptr =
_fsopen(path.string().c_str(), read_only ? "rb" : "rb+", _SH_DENYNO);
#else // !defined(_WIN32)
auto *ptr = fopen(path.string().c_str(), read_only ? "rb" : "rb+");
#endif // defined(_WIN32)
return file{
file_t{ptr},
path,
};
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
utils::error::handle_exception(function_name);
}
return {};
}
auto file::open_or_create_file(std::filesystem::path path,