Files
repertory/support/src/utils/file_file.cpp
2024-08-07 13:12:56 -05:00

365 lines
8.9 KiB
C++

/*
Copyright <2018-2024> <scott.e.graves@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "utils/file.hpp"
#include "utils/encryption.hpp"
#include "utils/error.hpp"
#include "utils/path.hpp"
#include "utils/string.hpp"
namespace repertory::utils::file {
auto file::open_file(std::filesystem::path path, bool read_only) -> file {
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,
bool read_only) -> file {
path = utils::path::absolute(path.string());
#if defined(_WIN32)
int old_mode{};
_umask_s(0600U, &old_mode);
#else // !defined(_WIN32)
auto old_mode = umask(0600U);
#endif // defined(_WIN32)
#if defined(_WIN32)
auto *ptr = _fsopen(path.string().c_str(), "ab+", _SH_DENYNO);
#else // !defined(_WIN32)
auto *ptr = fopen(path.string().c_str(), "ab+");
#endif // defined(_WIN32)
#if defined(_WIN32)
_umask_s(old_mode, nullptr);
#else // !defined(_WIN32)
umask(old_mode);
#endif // defined(_WIN32)
if (ptr != nullptr) {
fclose(ptr);
}
return open_file(path, read_only);
}
void file::close() {
#if defined(_WIN32)
recur_mutex_lock lock{mtx_};
#endif // defined(_WIN32)
file_.reset();
}
void file::flush() {
#if defined(_WIN32)
recur_mutex_lock lock{mtx_};
#endif // defined(_WIN32)
if (file_) {
fflush(file_.get());
}
}
auto file::get_handle() const -> native_handle {
if (file_) {
#if defined(_WIN32)
return reinterpret_cast<native_handle>(
_get_osfhandle(_fileno(file_.get())));
#else // !defined(_WIN32)
return fileno(file_.get());
#endif // defined(_WIN32)
}
return INVALID_HANDLE_VALUE;
}
auto file::move_to(std::filesystem::path new_path) -> bool {
#if defined(_WIN32)
recur_mutex_lock lock{mtx_};
#endif // defined(_WIN32)
new_path = utils::path::absolute(new_path.string());
auto reopen{false};
if (file_) {
reopen = true;
close();
}
std::error_code ec{};
std::filesystem::rename(path_, new_path, ec);
if (not ec) {
path_ = new_path;
if (reopen) {
*this = open_file(path_);
}
return true;
}
if (reopen) {
*this = open_file(path_);
}
return false;
}
auto file::read_all(data_buffer &data, std::uint64_t offset,
std::size_t *total_read) -> bool {
#if defined(_WIN32)
recur_mutex_lock lock{mtx_};
#endif // defined(_WIN32)
data_buffer buffer;
buffer.resize(65536U);
std::size_t current_read{};
while (read(reinterpret_cast<unsigned char *>(buffer.data()),
buffer.size() * sizeof(data_buffer::value_type), offset,
&current_read)) {
if (total_read != nullptr) {
*total_read += current_read;
}
if (current_read != 0U) {
offset += current_read;
data.insert(
data.end(), buffer.begin(),
std::next(buffer.begin(),
static_cast<std::int64_t>(
current_read / sizeof(data_buffer::value_type))));
continue;
}
return true;
}
return false;
}
auto file::read(data_buffer &data, std::uint64_t offset,
std::size_t *total_read) -> bool {
#if defined(_WIN32)
recur_mutex_lock lock{mtx_};
#endif // defined(_WIN32)
std::size_t bytes_read{};
auto ret =
read(reinterpret_cast<unsigned char *>(data.data()),
data.size() * sizeof(data_buffer::value_type), offset, &bytes_read);
data.resize(bytes_read / sizeof(data_buffer::value_type));
if (total_read != nullptr) {
(*total_read) = bytes_read;
}
return ret;
}
auto file::read(unsigned char *data, std::size_t to_read, std::uint64_t offset,
std::size_t *total_read) -> bool {
#if defined(_WIN32)
recur_mutex_lock lock{mtx_};
#endif // defined(_WIN32)
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
if (total_read != nullptr) {
(*total_read) = 0U;
}
try {
if (not file_) {
throw std::runtime_error("file is not open for reading");
}
auto res = fseeko(file_.get(), static_cast<std::int64_t>(offset), SEEK_SET);
if (res == -1) {
throw std::runtime_error("failed to seek before read");
}
auto bytes_read = fread(data, 1U, to_read, file_.get());
if (not feof(file_.get()) && ferror(file_.get())) {
throw std::runtime_error("failed to read file bytes");
}
if (total_read != nullptr) {
(*total_read) = bytes_read;
}
return true;
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
utils::error::handle_exception(function_name);
}
return false;
}
auto file::remove() -> bool {
#if defined(_WIN32)
recur_mutex_lock lock{mtx_};
#endif // defined(_WIN32)
close();
std::error_code ec{};
return std::filesystem::remove(path_, ec);
}
auto file::truncate(std::size_t size) -> bool {
#if defined(_WIN32)
recur_mutex_lock lock{mtx_};
#endif // defined(_WIN32)
auto reopen{false};
if (file_) {
reopen = true;
close();
}
std::error_code ec{};
std::filesystem::resize_file(path_, size, ec);
if (reopen) {
*this = open_file(path_);
}
return ec.value() == 0;
}
auto file::write_(const unsigned char *data, std::size_t to_write,
std::size_t offset, std::size_t *total_written) -> bool {
#if defined(_WIN32)
recur_mutex_lock lock{mtx_};
#endif // defined(_WIN32)
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
if (total_written != nullptr) {
(*total_written) = 0U;
}
try {
if (not file_) {
throw std::runtime_error("file is not open for writing");
}
auto res = fseeko(file_.get(), static_cast<std::int64_t>(offset), SEEK_SET);
if (res == -1) {
throw std::runtime_error("failed to seek before write");
}
auto bytes_written =
fwrite(reinterpret_cast<const char *>(data), 1U, to_write, file_.get());
if (not feof(file_.get()) && ferror(file_.get())) {
throw std::runtime_error("failed to read file bytes");
}
flush();
if (total_written != nullptr) {
(*total_written) = bytes_written;
}
return true;
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
utils::error::handle_exception(function_name);
}
return false;
}
auto file::size() const -> std::uint64_t {
#if defined(_WIN32)
recur_mutex_lock lock{mtx_};
#endif // defined(_WIN32)
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
try {
if (file_) {
if (fseeko(file_.get(), 0, SEEK_END) == -1) {
throw std::runtime_error("failed to seek");
}
auto size = ftello(file_.get());
if (size == -1) {
throw std::runtime_error("failed to get position");
}
return static_cast<std::uint64_t>(size);
}
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
utils::error::handle_exception(function_name);
}
return 0U;
}
} // namespace repertory::utils::file