/* 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. */ #ifndef REPERTORY_INCLUDE_UTILS_FILE_HPP_ #define REPERTORY_INCLUDE_UTILS_FILE_HPP_ #include "utils/config.hpp" #include "utils/path.hpp" #include "utils/string.hpp" #include "utils/types/file/i_directory.hpp" #include "utils/types/file/i_file.hpp" #include "utils/types/file/i_fs_item.hpp" namespace repertory::utils::file { [[nodiscard]] auto change_to_process_directory() -> bool; // INFO: has test [[nodiscard]] auto create_temp_name(std::string_view file_part) -> std::string; // INFO: has test [[nodiscard]] auto create_temp_name(std::wstring_view file_part) -> std::wstring; // INFO: has test [[nodiscard]] inline auto directory_exists_in_path(std::string_view path, std::string_view sub_directory) -> bool; // INFO: has test [[nodiscard]] inline auto directory_exists_in_path(std::wstring_view path, std::wstring_view sub_directory) -> bool; // INFO: has test [[nodiscard]] inline auto file_exists_in_path(std::string_view path, std::string_view file_name) -> bool; // INFO: has test [[nodiscard]] inline auto file_exists_in_path(std::wstring_view path, std::wstring_view file_name) -> bool; // INFO: has test [[nodiscard]] auto get_free_drive_space(std::string_view path) -> std::optional; // INFO: has test [[nodiscard]] auto get_free_drive_space(std::wstring_view path) -> std::optional; // INFO: has test [[nodiscard]] auto get_time(std::string_view path, time_type type) -> std::optional; // INFO: has test [[nodiscard]] auto get_time(std::wstring_view path, time_type type) -> std::optional; // INFO: has test [[nodiscard]] auto get_times(std::string_view path) -> std::optional; // INFO: has test [[nodiscard]] auto get_times(std::wstring_view path) -> std::optional; // INFO: has test [[nodiscard]] auto get_total_drive_space(std::string_view path) -> std::optional; // INFO: has test [[nodiscard]] auto get_total_drive_space(std::wstring_view path) -> std::optional; #if defined(PROJECT_ENABLE_LIBDSM) [[nodiscard]] auto smb_create_and_validate_relative_path(std::string_view smb_path, std::string_view rel_path) -> std::string; // INFO: has test [[nodiscard]] auto smb_create_relative_path(std::string_view smb_path) -> std::string; // INFO: has test [[nodiscard]] auto smb_create_search_path(std::string_view smb_path) -> std::string; // INFO: has test [[nodiscard]] auto smb_create_smb_path(std::string_view smb_path, std::string_view rel_path) -> std::string; [[nodiscard]] auto smb_get_parent_path(std::string_view smb_path) -> std::string; [[nodiscard]] auto smb_get_root_path(std::string_view smb_path) -> std::string; [[nodiscard]] auto smb_get_unc_path(std::string_view smb_path) -> std::string; [[nodiscard]] auto smb_get_uri_path(std::string_view smb_path) -> std::string; [[nodiscard]] auto smb_get_uri_path(std::string_view smb_path, std::string_view user, std::string_view password) -> std::string; // INFO: has test [[nodiscard]] auto smb_parent_is_same(std::string_view smb_path1, std::string_view smb_path2) -> bool; #endif // defined(PROJECT_ENABLE_LIBDSM) class file final : public i_file { public: // [[nodiscard]] static auto // attach_file(native_handle handle, // bool read_only = false) -> fs_file_t; // INFO: has test [[nodiscard]] static auto open_file(std::string_view path, bool read_only = false) -> fs_file_t; [[nodiscard]] static auto open_file(std::wstring_view path, bool read_only = false) -> fs_file_t { return open_file(utils::string::to_utf8(path), read_only); } // INFO: has test [[nodiscard]] static auto open_or_create_file(std::string_view path, bool read_only = false) -> fs_file_t; [[nodiscard]] static auto open_or_create_file(std::wstring_view path, bool read_only = false) -> fs_file_t { 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_) {} ~file() override { close(); } private: file_t file_; std::string path_; bool read_only_{false}; private: std::atomic_uint32_t read_buffer_size{65536U}; private: void open(); public: void close() override; [[nodiscard]] auto copy_to(std::string_view new_path, bool overwrite) const -> bool 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 is_symlink() const -> bool override; [[nodiscard]] auto move_to(std::string_view new_path) -> 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 remove() -> bool override; auto set_read_buffer_size(std::uint32_t size) -> std::uint32_t override { read_buffer_size = size; return read_buffer_size; } #if defined(PROJECT_ENABLE_LIBSODIUM) [[nodiscard]] auto sha256() -> std::optional; #endif // defined(PROJECT_ENABLE_LIBSODIUM) [[nodiscard]] auto size() const -> std::optional override; [[nodiscard]] auto truncate(std::size_t size) -> 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_; } return *this; } [[nodiscard]] operator bool() const override { return file_ != nullptr; } }; #if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) class enc_file final : public i_file { public: [[nodiscard]] static auto attach_file(fs_file_t file) -> fs_file_t; public: enc_file() noexcept = default; protected: enc_file(fs_file_t file); public: enc_file(const enc_file &) = delete; enc_file(enc_file &&move_file) noexcept : file_(std::move(move_file.file_)) {} ~enc_file() override { close(); } private: fs_file_t file_; public: void close() override; [[nodiscard]] auto copy_to(std::string_view new_path, bool overwrite) const -> bool 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 get_time(time_type type) const -> std::optional override { return file_->get_time(type); } [[nodiscard]] auto is_read_only() const -> bool override { return file_->is_read_only(); } [[nodiscard]] auto is_symlink() const -> bool override { return file_->is_symlink(); } [[nodiscard]] auto move_to(std::string_view new_path) -> 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 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::optional override; [[nodiscard]] auto truncate(std::size_t size) -> 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(*file_); } auto operator=(const file &) noexcept -> enc_file & = delete; auto operator=(enc_file &&move_file) noexcept -> enc_file & { if (&move_file != this) { file_ = std::move(move_file.file_); } return *this; } }; #endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) class thread_file final : public i_file { public: // [[nodiscard]] static auto // attach_file(native_handle handle, // bool read_only = false) -> fs_file_t; [[nodiscard]] static auto attach_file(fs_file_t file) -> fs_file_t; [[nodiscard]] static auto open_file(std::string_view path, bool read_only = false) -> fs_file_t; [[nodiscard]] static auto open_file(std::wstring_view path, bool read_only = false) -> fs_file_t { 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) -> fs_file_t; [[nodiscard]] static auto open_or_create_file(std::wstring_view path, bool read_only = false) -> fs_file_t { 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(fs_file_t 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: fs_file_t file_; public: void close() override; [[nodiscard]] auto copy_to(std::string_view new_path, bool overwrite) const -> bool 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 get_time(time_type type) const -> std::optional override { return file_->get_time(type); } [[nodiscard]] auto is_read_only() const -> bool override { return file_->is_read_only(); } [[nodiscard]] auto is_symlink() const -> bool override { return file_->is_symlink(); } [[nodiscard]] auto move_to(std::string_view new_path) -> 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 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::optional override; [[nodiscard]] auto truncate(std::size_t size) -> 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(*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; } }; class directory final : public i_directory { public: using directory_t = std::unique_ptr; directory() noexcept = default; directory(std::string_view path) : path_(utils::path::absolute(path)) {} directory(std::wstring_view path) : path_(utils::path::absolute(utils::string::to_utf8(path))) {} directory(const directory &) noexcept = delete; directory(directory &&move_dir) noexcept = default; ~directory() override = default; private: std::string path_; public: [[nodiscard]] auto copy_to(std::string_view new_path, bool overwrite) const -> bool override; [[nodiscard]] auto count(bool recursive = false) const -> std::uint64_t override; [[nodiscard]] auto create_directory(std::string_view path = "") const -> fs_directory_t override; [[nodiscard]] auto create_file(std::string_view file_name, bool read_only) const -> fs_file_t override; [[nodiscard]] auto exists() const -> bool override; [[nodiscard]] auto get_directory(std::string_view path) const -> fs_directory_t override; [[nodiscard]] auto get_directories() const -> std::vector override; [[nodiscard]] auto get_file(std::string_view path) const -> fs_file_t override; [[nodiscard]] auto get_files() const -> std::vector override; [[nodiscard]] auto get_items() const -> std::vector override; [[nodiscard]] auto get_path() const -> std::string override { return path_; } [[nodiscard]] auto is_symlink() const -> bool override; [[nodiscard]] auto move_to(std::string_view new_path) -> bool override; [[nodiscard]] auto remove() -> bool override; [[nodiscard]] auto remove_recursively() -> bool override; [[nodiscard]] auto size(bool recursive = false) const -> std::uint64_t override; public: auto operator=(const directory &) noexcept -> directory & = delete; auto operator=(directory &&move_dir) noexcept -> directory & = default; [[nodiscard]] operator bool() const override { return exists(); } }; #if defined(PROJECT_ENABLE_LIBDSM) #define SMB_MOD_RW2 \ (SMB_MOD_READ | SMB_MOD_WRITE | SMB_MOD_READ_EXT | SMB_MOD_WRITE_EXT | \ SMB_MOD_READ_ATTR | SMB_MOD_WRITE_ATTR | SMB_MOD_READ_CTL) class smb_file final : public i_file { public: smb_file() = default; smb_file(std::optional fd, std::string path, smb_session_t session, std::string_view share_name, smb_tid tid) : fd_(std::move(fd)), path_(std::move(path)), session_(std::move(session)), share_name_(share_name), tid_(tid) {} smb_file(const smb_file &) = delete; smb_file(smb_file &&f) noexcept : fd_(std::move(f.fd_)), path_(std::move(f.path_)), read_buffer_size(f.get_read_buffer_size()), read_only_(f.read_only_), session_(std::move(f.session_)), share_name_(std::move(f.share_name_)), tid_(f.tid_) {} ~smb_file() override { close(); } private: std::optional fd_; std::string path_; std::atomic_uint32_t read_buffer_size{65536U}; bool read_only_; smb_session_t session_; std::string share_name_; smb_tid tid_; public: void close() override; [[nodiscard]] auto copy_to(std::string_view new_path, bool overwrite) const -> bool override; [[nodiscard]] auto exists() const -> bool override; void flush() const override; [[nodiscard]] auto get_handle() const -> native_handle override { return INVALID_HANDLE_VALUE; } [[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]] static auto get_time(smb_session *session, smb_tid tid, std::string path, time_type type) -> std::optional; [[nodiscard]] auto get_time(time_type type) const -> std::optional override { return get_time(session_.get(), tid_, path_, type); } [[nodiscard]] auto get_unc_path() const -> std::string { return smb_get_unc_path(path_); } [[nodiscard]] auto get_uri_path() const -> std::string { return smb_get_uri_path(path_); } [[nodiscard]] auto get_uri_path(std::string_view user, std::string_view password) const -> std::string { return smb_get_uri_path(path_, user, password); } [[nodiscard]] auto is_read_only() const -> bool override { return read_only_; } [[nodiscard]] auto is_symlink() const -> bool override; [[nodiscard]] auto move_to(std::string_view new_path) -> bool override; [[nodiscard]] auto open(bool read_only) -> bool; [[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 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::optional override; [[nodiscard]] auto truncate(std::size_t size) -> 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 smb_file &) noexcept -> smb_file & = delete; auto operator=(smb_file &&move_file) noexcept -> smb_file & { if (this != &move_file) { fd_ = std::move(move_file.fd_); path_ = std::move(move_file.path_); read_buffer_size = move_file.get_read_buffer_size(); read_only_ = move_file.read_only_; session_ = std::move(move_file.session_); share_name_ = std::move(move_file.share_name_); tid_ = move_file.tid_; } return *this; } [[nodiscard]] operator bool() const override { return fd_.has_value(); } }; class smb_directory final : public i_directory { public: using smb_directory_t = std::unique_ptr; [[nodiscard]] static auto open(std::string_view host, std::string_view user, std::string_view password, std::string_view share_name) -> smb_directory_t; [[nodiscard]] static auto open(std::wstring_view host, std::wstring_view user, std::wstring_view password, std::wstring_view share_name) -> smb_directory_t; public: smb_directory() noexcept = default; smb_directory(const smb_directory &) noexcept = delete; smb_directory(smb_directory &&) noexcept = default; ~smb_directory() override = default; private: smb_directory(std::string path, smb_session_t session, std::string_view share_name, smb_tid tid) : path_(std::move(path)), session_(std::move(session)), share_name_(share_name), tid_(tid) {} private: std::string path_{}; smb_session_t session_{}; std::string share_name_{}; smb_tid tid_{}; public: [[nodiscard]] auto count(bool recursive = false) const -> std::uint64_t override; [[nodiscard]] auto copy_to(std::string_view new_path, bool overwrite) const -> bool override; [[nodiscard]] auto create_directory(std::string_view path = "") const -> fs_directory_t override; [[nodiscard]] auto create_file(std::string_view file_name, bool read_only) const -> fs_file_t override; [[nodiscard]] auto exists() const -> bool override; [[nodiscard]] auto get_directory(std::string_view path) const -> fs_directory_t override; [[nodiscard]] auto get_directories() const -> std::vector override; [[nodiscard]] auto get_file(std::string_view path) const -> fs_file_t override; [[nodiscard]] auto get_files() const -> std::vector override; [[nodiscard]] auto get_items() const -> std::vector override; [[nodiscard]] auto get_path() const -> std::string override { return path_; } [[nodiscard]] auto get_time(time_type type) const -> std::optional override { return smb_file::get_time(session_.get(), tid_, path_, type); } [[nodiscard]] auto get_unc_path() const -> std::string { return smb_get_unc_path(path_); } [[nodiscard]] auto get_uri_path() const -> std::string { return smb_get_uri_path(path_); } [[nodiscard]] auto get_uri_path(std::string_view user, std::string_view password) const -> std::string { return smb_get_uri_path(path_, user, password); } [[nodiscard]] auto is_symlink() const -> bool override; [[nodiscard]] auto move_to(std::string_view new_path) -> bool override; [[nodiscard]] auto remove() -> bool override; [[nodiscard]] auto remove_recursively() -> bool override; [[nodiscard]] auto size(bool recursive = false) const -> std::uint64_t override; public: auto operator=(const smb_directory &) noexcept -> smb_directory & = delete; auto operator=(smb_directory &&move_dir) noexcept -> smb_directory & = default; [[nodiscard]] operator bool() const override { return session_ != nullptr; } }; #endif // defined(PROJECT_ENABLE_LIBDSM) #if defined(PROJECT_ENABLE_JSON) #if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) // INFO: has test [[nodiscard]] auto read_json_file(std::string_view path, nlohmann::json &data, std::optional password = std::nullopt) -> bool; // INFO: has test [[nodiscard]] auto read_json_file( std::wstring_view path, nlohmann::json &data, std::optional password = std::nullopt) -> bool; // INFO: has test [[nodiscard]] auto write_json_file( std::string_view path, const nlohmann::json &data, std::optional password = std::nullopt) -> bool; // INFO: has test [[nodiscard]] auto write_json_file( std::wstring_view path, const nlohmann::json &data, std::optional password = std::nullopt) -> bool; #else // !defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) // INFO: has test [[nodiscard]] auto read_json_file(std::string_view path, nlohmann::json &data) -> bool; // INFO: has test [[nodiscard]] auto read_json_file(std::wstring_view path, nlohmann::json &data) -> bool; // INFO: has test [[nodiscard]] auto write_json_file(std::string_view path, const nlohmann::json &data) -> bool; // INFO: has test [[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) // INFO: has test template [[nodiscard]] inline auto directory_exists_in_path_t( std::basic_string_view path, std::basic_string_view sub_directory) -> bool { return directory(utils::path::combine(path, {sub_directory})).exists(); } // INFO: has test template [[nodiscard]] inline auto file_exists_in_path_t( std::basic_string_view path, std::basic_string_view file_name) -> bool { return file(utils::path::combine(path, {file_name})).exists(); } // INFO: has test inline auto directory_exists_in_path(std::string_view path, std::string_view sub_directory) -> bool { return directory_exists_in_path_t(path, sub_directory); } // INFO: has test inline auto directory_exists_in_path(std::wstring_view path, std::wstring_view sub_directory) -> bool { return directory_exists_in_path_t(path, sub_directory); } // INFO: has test inline auto file_exists_in_path(std::string_view path, std::string_view file_name) -> bool { return file_exists_in_path_t(path, file_name); } // INFO: has test inline auto file_exists_in_path(std::wstring_view path, std::wstring_view file_name) -> bool { return file_exists_in_path_t(path, file_name); } } // namespace repertory::utils::file #endif // REPERTORY_INCLUDE_UTILS_FILE_HPP_