/* Copyright <2018-2024> 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) -> file { path = utils::path::absolute(path.string()); if (not is_file(path.string())) { throw std::runtime_error("file not found: " + path.string()); } auto stream = std::fstream{ path, std::ios_base::binary | std::ios_base::in | std::ios_base::out, }; return { std::move(stream), path, }; } auto file::open_or_create_file(std::filesystem::path path) -> file { path = utils::path::absolute(path.string()); auto stream = std::fstream{ path.string().c_str(), std::ios_base::binary | std::ios_base::trunc | std::ios_base::in | std::ios_base::out, }; return { std::move(stream), path, }; } void file::close() { if (stream_.is_open()) { stream_.close(); } } auto file::move_to(std::filesystem::path new_path) -> bool { new_path = utils::path::absolute(new_path.string()); auto reopen{false}; if (stream_.is_open()) { reopen = true; close(); } std::filesystem::rename(path_, new_path, error_); if (not error_) { path_ = new_path; if (reopen) { *this = open_file(path_); } return true; } if (reopen) { *this = open_file(path_); } return false; } auto file::read_(unsigned char *data, std::size_t to_read, std::uint64_t offset, std::size_t *total_read) -> bool { static constexpr const std::string_view function_name{ static_cast(__FUNCTION__), }; if (total_read != nullptr) { (*total_read) = 0U; } try { stream_.seekg(static_cast(offset)); auto before = stream_.tellg(); if (before == -1) { throw std::runtime_error("failed to tellg() before read"); } stream_.read(reinterpret_cast(data), static_cast(to_read)); if (total_read != nullptr) { auto after = stream_.tellg(); if (after >= 0) { (*total_read) = static_cast(after - before); } else if (after == -1 && not stream_.eof()) { throw std::runtime_error("failed to tellg() after 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 { close(); return std::filesystem::remove(path_, error_); } auto file::truncate(std::size_t size) -> bool { auto reopen{false}; if (stream_.is_open()) { reopen = true; close(); } std::filesystem::resize_file(path_, size, error_); if (reopen) { *this = open_file(path_); } return not error_; } auto file::write_(const unsigned char *data, std::size_t to_write, std::size_t offset, std::size_t *total_written) -> bool { static constexpr const std::string_view function_name{ static_cast(__FUNCTION__), }; if (total_written != nullptr) { (*total_written) = 0U; } try { stream_.seekp(static_cast(offset)); auto before = stream_.tellp(); if (before == -1) { throw std::runtime_error("failed to tellp() before write"); } stream_.write(reinterpret_cast(data), static_cast(to_write)); auto after = stream_.tellp(); if (after == -1) { throw std::runtime_error("failed to tellp() after write"); } if (total_written != nullptr) { (*total_written) = static_cast(after - before); } return true; } catch (const std::exception &e) { utils::error::handle_exception(function_name, e); } catch (...) { utils::error::handle_exception(function_name); } return false; } auto get_file_size(std::string_view path, std::uint64_t &file_size) -> bool { auto abs_path = utils::path::absolute(path); file_size = 0U; #if defined(_WIN32) struct _stat64 st {}; if (_stat64(abs_path.c_str(), &st) != 0) { #else // !defined(_WIN32) struct stat st {}; if (stat(abs_path.c_str(), &st) != 0) { #endif // defined(_WIN32) return false; } if (st.st_size >= 0) { file_size = static_cast(st.st_size); } return (st.st_size >= 0); } auto get_file_size(std::wstring_view path, std::uint64_t &file_size) -> bool { return get_file_size(utils::string::to_utf8(path), file_size); } auto is_directory(std::string_view path) -> bool { auto abs_path = utils::path::absolute(path); #if defined(_WIN32) return ::PathIsDirectoryA(abs_path.c_str()) != 0; #else // !defined(_WIN32) struct stat st {}; return (stat(abs_path.c_str(), &st) == 0 && S_ISDIR(st.st_mode)); #endif // defined(_WIN32) } auto is_directory(std::wstring_view path) -> bool { return is_directory(utils::string::to_utf8(path)); } auto is_file(std::string_view path) -> bool { auto abs_path = utils::path::absolute(path); #if defined(_WIN32) return (::PathFileExistsA(abs_path.c_str()) && not ::PathIsDirectoryA(abs_path.c_str())); #else // !defined(_WIN32) struct stat st {}; return (stat(abs_path.c_str(), &st) == 0 && not S_ISDIR(st.st_mode)); #endif // defined(_WIN32) } auto is_file(std::wstring_view path) -> bool { return is_file(utils::string::to_utf8(path)); } #if defined(PROJECT_ENABLE_JSON) #if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) auto read_json_file(std::string_view path, nlohmann::json &data, std::optional password) -> bool { #else // !defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) auto read_json_file(std::string_view path, nlohmann::json &data) -> bool { #endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) static constexpr const std::string_view function_name{ static_cast(__FUNCTION__), }; try { auto abs_path = utils::path::absolute(path); if (not is_file(abs_path)) { return true; } std::ifstream file_stream{ abs_path.c_str(), std::ios_base::binary | std::ios::in, }; if (not file_stream.is_open()) { throw std::runtime_error("failed to open file: " + abs_path); } auto ret{true}; try { std::stringstream stream; stream << file_stream.rdbuf(); auto json_text = stream.str(); #if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) if (password.has_value()) { auto decrypted_data = utils::encryption::decrypt_data(json_text, *password); json_text = {decrypted_data.begin(), decrypted_data.end()}; } #endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) if (not json_text.empty()) { data = nlohmann::json::parse(json_text.c_str()); } } catch (const std::exception &e) { utils::error::handle_exception(function_name, e); ret = false; } catch (...) { utils::error::handle_exception(function_name); ret = false; } file_stream.close(); return ret; } catch (const std::exception &e) { utils::error::handle_exception(function_name, e); } catch (...) { utils::error::handle_exception(function_name); } return false; } #if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) auto write_json_file(std::string_view path, const nlohmann::json &data, std::optional password) -> bool { #else // !defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) auto write_json_file(std::string_view path, const nlohmann::json &data) -> bool { #endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) static constexpr const std::string_view function_name{ static_cast(__FUNCTION__), }; try { auto file = file::open_or_create_file(path); if (not file.truncate()) { throw std::runtime_error("failed to truncate file: " + file.get_error_code().message()); } #if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) if (password.has_value()) { return file.write(utils::encryption::encrypt_data(data.dump(), *password), 0U); } #endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) return file.write(data, 0U); } catch (const std::exception &e) { utils::error::handle_exception(function_name, e); } catch (...) { utils::error::handle_exception(function_name); } return false; } #if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) auto read_json_file(std::wstring_view path, nlohmann::json &data, std::optional password) -> bool { if (password.has_value()) { auto password_a = utils::string::to_utf8(*password); return read_json_file(utils::string::to_utf8(path), data, password_a); } return read_json_file(utils::string::to_utf8(path), data, std::nullopt); } auto write_json_file(std::wstring_view path, const nlohmann::json &data, std::optional password) -> bool { if (password.has_value()) { auto password_a = utils::string::to_utf8(*password); return write_json_file(utils::string::to_utf8(path), data, password_a); } return write_json_file(utils::string::to_utf8(path), data, std::nullopt); } #else // !defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) auto read_json_file(std::wstring_view path, nlohmann::json &data) -> bool { return read_json_file(utils::string::to_utf8(path), data); } auto write_json_file(std::wstring_view path, const nlohmann::json &data) -> bool { return write_json_file(utils::string::to_utf8(path), data); } #endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) #endif // defined(PROJECT_ENABLE_JSON) } // namespace repertory::utils::file