446 lines
14 KiB
C++
446 lines
14 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.
|
|
*/
|
|
#ifndef REPERTORY_INCLUDE_UTILS_FILE_HPP_
|
|
#define REPERTORY_INCLUDE_UTILS_FILE_HPP_
|
|
|
|
#include "utils/config.hpp"
|
|
|
|
#include "utils/path.hpp"
|
|
#include <memory>
|
|
|
|
namespace repertory::utils::file {
|
|
struct i_file {
|
|
virtual ~i_file() = default;
|
|
|
|
virtual void close() = 0;
|
|
|
|
[[nodiscard]] virtual auto exists() const -> bool = 0;
|
|
|
|
virtual void flush() const = 0;
|
|
|
|
[[nodiscard]] virtual auto get_handle() const -> native_handle = 0;
|
|
|
|
[[nodiscard]] virtual auto get_path() const -> std::string = 0;
|
|
|
|
[[nodiscard]] virtual auto get_read_buffer_size() const -> std::uint32_t = 0;
|
|
|
|
[[nodiscard]] virtual auto is_read_only() const -> bool = 0;
|
|
|
|
[[nodiscard]] virtual auto move_to(std::string_view new_path) -> bool = 0;
|
|
|
|
[[nodiscard]] virtual auto
|
|
read(data_buffer &data, std::uint64_t offset,
|
|
std::size_t *total_read = nullptr) -> bool = 0;
|
|
|
|
[[nodiscard]] virtual auto
|
|
read(unsigned char *data, std::size_t to_read, std::uint64_t offset,
|
|
std::size_t *total_read = nullptr) -> bool = 0;
|
|
|
|
[[nodiscard]] virtual auto
|
|
read_all(data_buffer &data, std::uint64_t offset,
|
|
std::size_t *total_read = nullptr) -> bool = 0;
|
|
|
|
[[nodiscard]] virtual auto remove() -> bool = 0;
|
|
|
|
virtual auto set_read_buffer_size(std::uint32_t size) -> std::uint32_t = 0;
|
|
|
|
[[nodiscard]] virtual auto size() const -> std::uint64_t = 0;
|
|
|
|
[[nodiscard]] virtual auto truncate() -> bool = 0;
|
|
|
|
[[nodiscard]] virtual auto truncate(std::size_t size) -> bool = 0;
|
|
|
|
[[nodiscard]] virtual auto
|
|
write(const data_buffer &data, std::uint64_t offset,
|
|
std::size_t *total_written = nullptr) -> bool = 0;
|
|
|
|
[[nodiscard]] virtual auto
|
|
write(const unsigned char *data, std::size_t to_write, std::size_t offset,
|
|
std::size_t *total_written = nullptr) -> bool = 0;
|
|
|
|
public:
|
|
[[nodiscard]] virtual operator bool() const = 0;
|
|
|
|
protected:
|
|
i_file() noexcept = default;
|
|
|
|
i_file(const i_file &) noexcept = delete;
|
|
|
|
i_file(i_file &&) noexcept = delete;
|
|
|
|
auto operator=(i_file &&) noexcept -> i_file & = delete;
|
|
|
|
auto operator=(const i_file &) noexcept -> i_file & = delete;
|
|
};
|
|
|
|
class file final : public i_file {
|
|
public:
|
|
[[nodiscard]] static auto
|
|
attach_file(native_handle handle,
|
|
bool read_only = false) -> std::unique_ptr<i_file>;
|
|
|
|
[[nodiscard]] static auto
|
|
open_file(std::string_view path,
|
|
bool read_only = false) -> std::unique_ptr<i_file>;
|
|
|
|
[[nodiscard]] static auto
|
|
open_file(std::wstring_view path,
|
|
bool read_only = false) -> std::unique_ptr<i_file> {
|
|
return open_file(utils::string::to_utf8(path), read_only);
|
|
}
|
|
|
|
[[nodiscard]] static auto
|
|
open_or_create_file(std::string_view path,
|
|
bool read_only = false) -> std::unique_ptr<i_file>;
|
|
|
|
[[nodiscard]] static auto
|
|
open_or_create_file(std::wstring_view path,
|
|
bool read_only = false) -> std::unique_ptr<i_file> {
|
|
return open_or_create_file(utils::string::to_utf8(path), read_only);
|
|
}
|
|
|
|
public:
|
|
file() noexcept = default;
|
|
|
|
file(std::string_view path)
|
|
: file_(nullptr), path_(utils::path::absolute(path)) {}
|
|
|
|
file(std::wstring_view path)
|
|
: file_(nullptr),
|
|
path_(utils::path::absolute(utils::string::to_utf8(path))) {}
|
|
|
|
private:
|
|
file(file_t file_ptr, std::string_view path, bool read_only)
|
|
: file_(std::move(file_ptr)), path_(path), read_only_(read_only) {}
|
|
|
|
public:
|
|
file(const file &) = delete;
|
|
|
|
file(file &&move_file) noexcept
|
|
: file_(std::move(move_file.file_)),
|
|
path_(std::move(move_file.path_)),
|
|
read_only_(move_file.read_only_)
|
|
#if defined(_WIN32)
|
|
,
|
|
mtx_(std::move(move_file.mtx_))
|
|
#endif // defined(_WIN32)
|
|
{
|
|
}
|
|
|
|
~file() override { close(); }
|
|
|
|
private:
|
|
file_t file_;
|
|
std::string path_;
|
|
bool read_only_{false};
|
|
#if defined(_WIN32)
|
|
mutable std::unique_ptr<std::recursive_mutex> mtx_{
|
|
new std::recursive_mutex(),
|
|
};
|
|
#endif // defined(_WIN32)
|
|
|
|
private:
|
|
std::atomic_uint32_t read_buffer_size{65536U};
|
|
|
|
private:
|
|
void open();
|
|
|
|
public:
|
|
void close() override;
|
|
|
|
[[nodiscard]] auto exists() const -> bool override;
|
|
|
|
void flush() const override;
|
|
|
|
[[nodiscard]] auto get_handle() const -> native_handle override;
|
|
|
|
[[nodiscard]] auto get_path() const -> std::string override { return path_; }
|
|
|
|
[[nodiscard]] auto get_read_buffer_size() const -> std::uint32_t override {
|
|
return read_buffer_size;
|
|
}
|
|
|
|
[[nodiscard]] auto is_read_only() const -> bool override {
|
|
return read_only_;
|
|
};
|
|
|
|
[[nodiscard]] auto move_to(std::string_view new_path) -> bool override;
|
|
|
|
[[nodiscard]] auto read(data_buffer &data, std::uint64_t offset,
|
|
std::size_t *total_read = nullptr) -> bool override;
|
|
|
|
[[nodiscard]] auto read(unsigned char *data, std::size_t to_read,
|
|
std::uint64_t offset,
|
|
std::size_t *total_read = nullptr) -> bool override;
|
|
|
|
[[nodiscard]] auto
|
|
read_all(data_buffer &data, std::uint64_t offset,
|
|
std::size_t *total_read = nullptr) -> bool override;
|
|
|
|
[[nodiscard]] auto remove() -> bool override;
|
|
|
|
auto set_read_buffer_size(std::uint32_t size) -> std::uint32_t override {
|
|
read_buffer_size = size;
|
|
return read_buffer_size;
|
|
}
|
|
|
|
[[nodiscard]] auto size() const -> std::uint64_t override;
|
|
|
|
[[nodiscard]] auto truncate() -> bool override { return truncate(0U); }
|
|
|
|
[[nodiscard]] auto truncate(std::size_t size) -> bool override;
|
|
|
|
[[nodiscard]] auto
|
|
write(const data_buffer &data, std::uint64_t offset,
|
|
std::size_t *total_written = nullptr) -> bool override;
|
|
|
|
[[nodiscard]] auto
|
|
write(const unsigned char *data, std::size_t to_write, std::size_t offset,
|
|
std::size_t *total_written = nullptr) -> bool override;
|
|
|
|
public:
|
|
auto operator=(const file &) noexcept -> file & = delete;
|
|
|
|
auto operator=(file &&move_file) noexcept -> file & {
|
|
if (&move_file != this) {
|
|
file_ = std::move(move_file.file_);
|
|
path_ = std::move(move_file.path_);
|
|
read_only_ = move_file.read_only_;
|
|
#if defined(_WIN32)
|
|
mtx_ = std::move(move_file.mtx_);
|
|
#endif // defined(_WIN32)
|
|
}
|
|
|
|
return *this;
|
|
}
|
|
|
|
[[nodiscard]] operator bool() const override { return file_ != nullptr; }
|
|
};
|
|
|
|
class thread_file final : public i_file {
|
|
public:
|
|
[[nodiscard]] static auto
|
|
attach_file(native_handle handle,
|
|
bool read_only = false) -> std::unique_ptr<i_file>;
|
|
|
|
[[nodiscard]] static auto
|
|
open_file(std::string_view path,
|
|
bool read_only = false) -> std::unique_ptr<i_file>;
|
|
|
|
[[nodiscard]] static auto
|
|
open_file(std::wstring_view path,
|
|
bool read_only = false) -> std::unique_ptr<i_file> {
|
|
return open_file(utils::string::to_utf8(path), read_only);
|
|
}
|
|
|
|
[[nodiscard]] static auto
|
|
open_or_create_file(std::string_view path,
|
|
bool read_only = false) -> std::unique_ptr<i_file>;
|
|
|
|
[[nodiscard]] static auto
|
|
open_or_create_file(std::wstring_view path,
|
|
bool read_only = false) -> std::unique_ptr<i_file> {
|
|
return open_or_create_file(utils::string::to_utf8(path), read_only);
|
|
}
|
|
|
|
public:
|
|
thread_file() noexcept = default;
|
|
|
|
thread_file(std::string_view path) : file_(new file(path)) {}
|
|
|
|
thread_file(std::wstring_view path)
|
|
: file_(new file(utils::string::to_utf8(path))) {}
|
|
|
|
protected:
|
|
thread_file(std::unique_ptr<i_file> file);
|
|
|
|
public:
|
|
thread_file(const thread_file &) = delete;
|
|
|
|
thread_file(thread_file &&move_file) noexcept
|
|
: file_(std::move(move_file.file_)) {}
|
|
|
|
~thread_file() override { close(); }
|
|
|
|
private:
|
|
std::unique_ptr<i_file> file_;
|
|
|
|
public:
|
|
void close() override;
|
|
|
|
[[nodiscard]] auto exists() const -> bool override { return file_->exists(); }
|
|
|
|
void flush() const override;
|
|
|
|
[[nodiscard]] auto get_handle() const -> native_handle override {
|
|
return file_->get_handle();
|
|
}
|
|
|
|
[[nodiscard]] auto get_path() const -> std::string override {
|
|
return file_->get_path();
|
|
}
|
|
|
|
[[nodiscard]] auto get_read_buffer_size() const -> std::uint32_t override {
|
|
return file_->get_read_buffer_size();
|
|
}
|
|
|
|
[[nodiscard]] auto is_read_only() const -> bool override {
|
|
return file_->is_read_only();
|
|
};
|
|
|
|
[[nodiscard]] auto move_to(std::string_view new_path) -> bool override;
|
|
|
|
[[nodiscard]] auto read(data_buffer &data, std::uint64_t offset,
|
|
std::size_t *total_read = nullptr) -> bool override;
|
|
|
|
[[nodiscard]] auto read(unsigned char *data, std::size_t to_read,
|
|
std::uint64_t offset,
|
|
std::size_t *total_read = nullptr) -> bool override;
|
|
|
|
[[nodiscard]] auto
|
|
read_all(data_buffer &data, std::uint64_t offset,
|
|
std::size_t *total_read = nullptr) -> bool override;
|
|
|
|
[[nodiscard]] auto remove() -> bool override;
|
|
|
|
auto set_read_buffer_size(std::uint32_t size) -> std::uint32_t override {
|
|
return file_->set_read_buffer_size(size);
|
|
}
|
|
|
|
[[nodiscard]] auto size() const -> std::uint64_t override;
|
|
|
|
[[nodiscard]] auto truncate() -> bool override { return truncate(0U); }
|
|
|
|
[[nodiscard]] auto truncate(std::size_t size) -> bool override;
|
|
|
|
[[nodiscard]] auto
|
|
write(const data_buffer &data, std::uint64_t offset,
|
|
std::size_t *total_written = nullptr) -> bool override;
|
|
|
|
[[nodiscard]] auto
|
|
write(const unsigned char *data, std::size_t to_write, std::size_t offset,
|
|
std::size_t *total_written = nullptr) -> bool override;
|
|
|
|
public:
|
|
[[nodiscard]] operator bool() const override {
|
|
return static_cast<bool>(*file_);
|
|
}
|
|
|
|
auto operator=(const file &) noexcept -> thread_file & = delete;
|
|
|
|
auto operator=(thread_file &&move_file) noexcept -> thread_file & {
|
|
if (&move_file != this) {
|
|
file_ = std::move(move_file.file_);
|
|
}
|
|
|
|
return *this;
|
|
}
|
|
};
|
|
|
|
[[nodiscard]] auto create_directories(std::string_view path) -> bool;
|
|
|
|
[[nodiscard]] auto create_directories(std::wstring_view path) -> bool;
|
|
|
|
[[nodiscard]] auto
|
|
directory_exists_in_path(std::string_view path,
|
|
std::string_view sub_directory) -> bool;
|
|
|
|
[[nodiscard]] auto
|
|
directory_exists_in_path(std::wstring_view path,
|
|
std::wstring_view sub_directory) -> bool;
|
|
|
|
[[nodiscard]] auto get_file_size(std::string_view path,
|
|
std::uint64_t &file_size) -> bool;
|
|
|
|
[[nodiscard]] auto get_file_size(std::wstring_view path,
|
|
std::uint64_t &file_size) -> bool;
|
|
|
|
[[nodiscard]] auto file_exists_in_path(std::string_view path,
|
|
std::string_view file_name) -> bool;
|
|
|
|
[[nodiscard]] auto file_exists_in_path(std::wstring_view path,
|
|
std::wstring_view file_name) -> bool;
|
|
|
|
[[nodiscard]] auto is_directory(std::string_view path) -> bool;
|
|
|
|
[[nodiscard]] auto is_directory(std::wstring_view path) -> bool;
|
|
|
|
[[nodiscard]] auto is_file(std::string_view path) -> bool;
|
|
|
|
[[nodiscard]] auto is_file(std::wstring_view path) -> bool;
|
|
|
|
[[nodiscard]] auto remove_directory(std::string_view path,
|
|
bool recursive = false) -> bool;
|
|
|
|
[[nodiscard]] auto remove_directory(std::wstring_view path,
|
|
bool recursive = false) -> bool;
|
|
|
|
#if defined(PROJECT_ENABLE_JSON)
|
|
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
|
[[nodiscard]] auto
|
|
read_json_file(std::string_view path, nlohmann::json &data,
|
|
std::optional<std::string_view> password = std::nullopt) -> bool;
|
|
|
|
[[nodiscard]] auto read_json_file(
|
|
std::wstring_view path, nlohmann::json &data,
|
|
std::optional<std::wstring_view> password = std::nullopt) -> bool;
|
|
|
|
[[nodiscard]] auto write_json_file(
|
|
std::string_view path, const nlohmann::json &data,
|
|
std::optional<std::string_view> password = std::nullopt) -> bool;
|
|
|
|
[[nodiscard]] auto write_json_file(
|
|
std::wstring_view path, const nlohmann::json &data,
|
|
std::optional<std::wstring_view> password = std::nullopt) -> bool;
|
|
#else // !defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
|
[[nodiscard]] auto read_json_file(std::string_view path,
|
|
nlohmann::json &data) -> bool;
|
|
|
|
[[nodiscard]] auto read_json_file(std::wstring_view path,
|
|
nlohmann::json &data) -> bool;
|
|
|
|
[[nodiscard]] auto write_json_file(std::string_view path,
|
|
const nlohmann::json &data) -> bool;
|
|
|
|
[[nodiscard]] auto write_json_file(std::wstring_view path,
|
|
const nlohmann::json &data) -> bool;
|
|
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
|
#endif // defined(PROJECT_ENABLE_JSON)
|
|
|
|
template <typename string_t>
|
|
inline auto directory_exists_in_path_t(
|
|
std::basic_string_view<typename string_t::value_type> path,
|
|
std::basic_string_view<typename string_t::value_type> sub_directory)
|
|
-> bool {
|
|
return is_directory(utils::path::combine(path, {sub_directory}));
|
|
}
|
|
|
|
template <typename string_t>
|
|
inline auto file_exists_in_path_t(
|
|
std::basic_string_view<typename string_t::value_type> path,
|
|
std::basic_string_view<typename string_t::value_type> file_name) -> bool {
|
|
return is_file(utils::path::combine(path, {file_name}));
|
|
}
|
|
} // namespace repertory::utils::file
|
|
|
|
#endif // REPERTORY_INCLUDE_UTILS_FILE_HPP_
|