new_build_system (#18)
All checks were successful
BlockStorage/repertory/pipeline/head This commit looks good

Reviewed-on: #18
This commit is contained in:
2024-09-06 15:05:48 +00:00
parent 9d3e4b8767
commit a7239558bd
191 changed files with 10683 additions and 10598 deletions

View File

@ -24,13 +24,19 @@
#include "utils/config.hpp"
#include "utils/base64.hpp"
#include "utils/collection.hpp"
#include "utils/com_init_wrapper.hpp"
#include "utils/common.hpp"
#include "utils/encrypting_reader.hpp"
#include "utils/encryption.hpp"
#include "utils/error.hpp"
#include "utils/file.hpp"
#include "utils/hash.hpp"
#include "utils/path.hpp"
#include "utils/string.hpp"
#include "utils/time.hpp"
#include "utils/unix.hpp"
#include "utils/windows.hpp"
#endif // REPERTORY_INCLUDE_UTILS_ALL_HPP_
#endif // REPERTORY_INCLUDE_UTILS_ALL_HPP_

View File

@ -24,6 +24,7 @@
#include "utils/config.hpp"
#include "utils/error.hpp"
#include "utils/string.hpp"
namespace repertory::utils::collection {
@ -69,24 +70,75 @@ inline auto includes(const col_t &collection,
collection.end();
}
template <typename val_t, typename string_t>
[[nodiscard]] inline auto
from_hex_string_t(std::basic_string_view<typename string_t::value_type> str,
val_t &val) -> bool {
template <typename val_t>
[[nodiscard]] inline auto from_hex_string_t(std::string_view str,
val_t &val) -> bool {
static constexpr const auto base16{16};
val.clear();
if (not(str.length() % 2U)) {
for (std::size_t i = 0U; i < str.length(); i += 2U) {
val.emplace_back(static_cast<typename val_t::value_type>(
std::strtol(string_t{str.substr(i, 2U)}.c_str(), nullptr, base16)));
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
try {
val.clear();
std::string fmt_val{str};
utils::string::trim(fmt_val);
if (fmt_val.empty()) {
return true;
}
fmt_val = utils::string::to_lower(fmt_val);
if (utils::string::begins_with(fmt_val, "0x")) {
fmt_val = fmt_val.substr(2U);
}
if (fmt_val.empty()) {
throw std::runtime_error("hex string is invalid|" + std::string{str});
}
if (fmt_val.length() % 2U) {
fmt_val = '0' + fmt_val;
}
auto iter = std::find_if_not(
fmt_val.begin(), fmt_val.end(), [](auto cur_char) -> bool {
auto check = static_cast<std::uint32_t>(cur_char);
return ((check >= 48U && check <= 57U) ||
(check >= 97U && check <= 102U));
});
if (iter != fmt_val.end()) {
auto invalid_idx{std::distance(fmt_val.begin(), iter)};
throw std::range_error(
"invalid character in hex string|" + std::to_string(invalid_idx) +
'|' + std::string(1U, str.at(invalid_idx)) + '|' + std::string{str});
}
val.resize(fmt_val.length() / 2U);
for (std::size_t idx = 0U; idx < fmt_val.length(); idx += 2U) {
val.at(idx / 2U) = static_cast<typename val_t::value_type>(
std::strtoul(fmt_val.substr(idx, 2U).c_str(), nullptr, base16));
}
return true;
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
utils::error::handle_exception(function_name);
}
val.clear();
return false;
}
template <typename val_t>
inline auto from_hex_string(std::string_view str, val_t &val) -> bool {
return from_hex_string_t<val_t>(str, val);
}
template <typename val_t>
inline auto from_hex_string(std::wstring_view str, val_t &val) -> bool {
return from_hex_string_t<val_t>(utils::string::to_utf8(str), val);
}
template <typename col_t>
inline auto remove_element(col_t &collection,
@ -96,16 +148,6 @@ inline auto remove_element(col_t &collection,
return collection;
}
template <typename val_t>
inline auto from_hex_string(std::string_view str, val_t &val) -> bool {
return from_hex_string_t<val_t, std::string>(str, val);
}
template <typename val_t>
inline auto from_hex_string(std::wstring_view str, val_t &val) -> bool {
return from_hex_string_t<val_t, std::wstring>(str, val);
}
template <typename col_t>
inline auto to_hex_string(const col_t &collection) -> std::string {
static_assert(sizeof(typename col_t::value_type) == 1U,

View File

@ -33,6 +33,8 @@ struct result final {
[[nodiscard]] operator bool() const { return ok; }
};
using retryable_action_t = std::function<bool()>;
[[nodiscard]] inline constexpr auto
calculate_read_size(std::uint64_t total_size, std::size_t read_size,
std::uint64_t offset) -> std::size_t {
@ -60,22 +62,20 @@ template <typename result_t, typename data_t>
[[nodiscard]] inline constexpr auto
divide_with_ceiling(result_t numerator, data_t denominator) -> result_t;
#if defined(PROJECT_ENABLE_LIBSODIUM)
template <typename data_t>
[[nodiscard]] inline auto generate_random() -> data_t;
template <typename data_t>
[[nodiscard]] inline auto generate_random(std::size_t size) -> data_t;
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
template <typename data_t>
[[nodiscard]] inline auto generate_random_between(data_t begin,
data_t end) -> data_t;
#if defined(PROJECT_ENABLE_LIBSODIUM)
[[nodiscard]] auto generate_random_string(std::size_t length) -> std::string;
[[nodiscard]] auto generate_random_wstring(std::size_t length) -> std::wstring;
#if defined(PROJECT_ENABLE_LIBSODIUM)
template <typename data_t>
[[nodiscard]] inline auto generate_secure_random() -> data_t;
template <typename data_t>
[[nodiscard]] inline auto generate_secure_random(std::size_t size) -> data_t;
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
[[nodiscard]] auto
@ -90,6 +90,11 @@ get_next_available_port(std::uint16_t first_port,
std::uint16_t &available_port) -> bool;
#endif // defined(PROJECT_ENABLE_BOOST)
[[nodiscard]] auto retry_action(retryable_action_t action,
std::size_t retry_count = 200U,
std::chrono::milliseconds retry_wait =
std::chrono::milliseconds(10)) -> bool;
template <typename result_t, typename data_t>
inline constexpr auto divide_with_ceiling(result_t numerator,
data_t denominator) -> result_t {
@ -101,26 +106,6 @@ inline constexpr auto divide_with_ceiling(result_t numerator,
: (numerator / denominator) + (numerator % denominator != 0);
}
#if defined(PROJECT_ENABLE_LIBSODIUM)
template <typename data_t> inline auto generate_random() -> data_t {
static_assert(!is_collection<std::decay_t<data_t>>::value,
"data_t is a vector or collection");
data_t ret{};
randombytes_buf(&ret, sizeof(ret));
return ret;
}
template <typename data_t>
inline auto generate_random(std::size_t size) -> data_t {
static_assert(is_collection<std::decay_t<data_t>>::value,
"data_t is not a vector or collection");
data_t ret;
ret.resize(size);
randombytes_buf(ret.data(), ret.size() * sizeof(typename data_t::value_type));
return ret;
}
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
template <typename data_t>
inline auto generate_random_between(data_t begin, data_t end) -> data_t {
static_assert(std::is_integral_v<std::remove_cv_t<data_t>>,
@ -129,10 +114,31 @@ inline auto generate_random_between(data_t begin, data_t end) -> data_t {
throw std::range_error("end must be greater than begin");
}
static std::mt19937 gen(std::random_device{}());
thread_local std::mt19937 gen(
static_cast<unsigned long>(std::time(nullptr) ^ std::random_device{}()));
std::uniform_int_distribution<data_t> dis(begin, end);
return dis(gen);
}
#if defined(PROJECT_ENABLE_LIBSODIUM)
template <typename data_t> inline auto generate_secure_random() -> data_t {
static_assert(!is_collection<std::decay_t<data_t>>::value,
"data_t is a vector or collection");
data_t ret{};
randombytes_buf(&ret, sizeof(ret));
return ret;
}
template <typename data_t>
inline auto generate_secure_random(std::size_t size) -> data_t {
static_assert(is_collection<std::decay_t<data_t>>::value,
"data_t is not a vector or collection");
data_t ret;
ret.resize(size);
randombytes_buf(ret.data(), ret.size() * sizeof(typename data_t::value_type));
return ret;
}
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
} // namespace repertory::utils
#endif // REPERTORY_INCLUDE_UTILS_COMMON_HPP_

View File

@ -22,6 +22,8 @@
#ifndef REPERTORY_INCLUDE_UTILS_CONFIG_HPP_
#define REPERTORY_INCLUDE_UTILS_CONFIG_HPP_
#define NOMINMAX
#if defined(_WIN32)
#define WINVER 0x0602
#define _WIN32_WINNT WINVER
@ -163,6 +165,50 @@ extern "C" {
#endif // defined(__cplusplus)
#endif // defined(PROJECT_ENABLE_FZF)
#if defined(PROJECT_ENABLE_LIBDSM)
#if defined(__cplusplus)
extern "C" {
#endif // defined(__cplusplus)
#include "bdsm/bdsm.h"
#if defined(__cplusplus)
}
struct netbios_ns_deleter final {
void operator()(netbios_ns *ns) {
if (ns != nullptr) {
netbios_ns_destroy(ns);
}
}
};
using netbios_ns_t = std::unique_ptr<netbios_ns, netbios_ns_deleter>;
inline const auto smb_session_deleter = [](smb_session *session) {
if (session != nullptr) {
smb_session_destroy(session);
}
};
using smb_session_t = std::shared_ptr<smb_session>;
struct smb_stat_deleter final {
void operator()(smb_stat st) {
if (st != nullptr) {
smb_stat_destroy(st);
}
}
};
using smb_stat_t = std::unique_ptr<smb_file, smb_stat_deleter>;
struct smb_stat_list_deleter final {
void operator()(smb_file *list) {
if (list != nullptr) {
smb_stat_list_destroy(list);
}
}
};
using smb_stat_list_t = std::unique_ptr<smb_file, smb_stat_list_deleter>;
#endif // defined(__cplusplus)
#endif // defined(PROJECT_ENABLE_LIBDSM)
#if defined(PROJECT_ENABLE_LIBEVENT)
#include "event2/buffer.h"
#include "event2/bufferevent.h"
@ -171,20 +217,68 @@ extern "C" {
#include "event2/util.h"
#endif // defined(PROJECT_ENABLE_LIBEVENT)
#if defined(PROJECT_ENABLE_LIBSODIUM)
#include "sodium.h"
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
#if defined(PROJECT_ENABLE_SDL)
#include "SDL.h"
#include "SDL_gamecontroller.h"
#include "SDL_joystick.h"
#endif // defined(PROJECT_ENABLE_SDL)
#if defined(PROJECT_ENABLE_LIBSODIUM)
#include "sodium.h"
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
#if defined(PROJECT_ENABLE_SQLITE)
#include "sqlite3.h"
#endif // defined(PROJECT_ENABLE_SQLITE)
#if defined(PROJECT_ENABLE_VLC)
#include <vlc/vlc.h>
#if defined(__cplusplus)
[[nodiscard]] inline auto get_libvlc_error_msg() -> std::string {
const auto *msg = libvlc_errmsg();
return msg == nullptr ? "none" : msg;
}
struct vlc_deleter final {
void operator()(libvlc_instance_t *inst) {
if (inst != nullptr) {
libvlc_release(inst);
}
}
};
using vlc_t = std::unique_ptr<libvlc_instance_t, vlc_deleter>;
struct vlc_media_deleter final {
void operator()(libvlc_media_t *media) {
if (media != nullptr) {
libvlc_media_release(media);
}
}
};
using vlc_media_t = std::unique_ptr<libvlc_media_t, vlc_media_deleter>;
struct vlc_media_list_deleter final {
void operator()(libvlc_media_list_t *media_list) {
if (media_list != nullptr) {
libvlc_media_list_release(media_list);
}
}
};
using vlc_media_list_t =
std::unique_ptr<libvlc_media_list_t, vlc_media_list_deleter>;
struct vlc_string_deleter final {
void operator()(char *str) {
if (str != nullptr) {
libvlc_free(str);
}
}
};
using vlc_string_t = std::unique_ptr<char, vlc_string_deleter>;
#endif // defined(__cplusplus)
#endif // defined(PROJECT_ENABLE_VLC)
#if !defined(fstat64)
#define fstat64 fstat
#endif // !defined(fstat64)
@ -231,6 +325,14 @@ extern "C" {
#include "boost/serialization/vector.hpp"
#endif // defined(PROJECT_ENABLE_BOOST)
#if defined(PROJECT_ENABLE_CLI11)
#if defined(PROJECT_IS_MINGW) && !defined(PROJECT_IS_MINGW_UNIX)
#include "CLI/CLI.hpp"
#else // !defined(PROJECT_IS_MINGW) || defined(PROJECT_IS_MINGW_UNIX)
#include "CLI11.hpp"
#endif // defined(PROJECT_IS_MINGW) && !defined(PROJECT_IS_MINGW_UNIX)
#endif // defined(PROJECT_ENABLE_CLI11)
#if defined(PROJECT_ENABLE_CPP_HTTPLIB)
#include "httplib.h"
#endif // defined(PROJECT_ENABLE_JSON)
@ -300,9 +402,26 @@ namespace repertory {
using data_buffer = std::vector<unsigned char>;
using mutex_lock = std::lock_guard<std::mutex>;
using recur_mutex_lock = std::lock_guard<std::recursive_mutex>;
using stop_type = std::atomic_bool;
using unique_mutex_lock = std::unique_lock<std::mutex>;
using unique_recur_mutex_lock = std::unique_lock<std::recursive_mutex>;
#if defined(_WIN32)
#if defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES)
inline constexpr const auto max_path_length = std::size_t{32767U};
#else // !defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES)
inline constexpr const auto max_path_length = std::size_t{MAX_PATH};
#endif // defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES)
using native_handle = HANDLE;
#else // !defined(_WIN32)
inline constexpr const auto max_path_length = std::size_t{PATH_MAX};
using native_handle = int;
#if !defined(INVALID_HANDLE_VALUE)
#define INVALID_HANDLE_VALUE (-1)
#endif // !defined(INVALID_HANDLE_VALUE)
#endif // defined(_WIN32)
template <class... Ts> struct overloaded : Ts... {
using Ts::operator()...;
};
@ -329,6 +448,17 @@ struct file_deleter final {
}
};
using file_t = std::unique_ptr<FILE, file_deleter>;
#if defined(PROJECT_ENABLE_CURL)
struct http_range final {
std::uint64_t begin{};
std::uint64_t end{};
};
using http_headers = std::unordered_map<std::string, std::string>;
using http_query_parameters = std::map<std::string, std::string>;
using http_ranges = std::vector<http_range>;
#endif // defined(PROJECT_ENABLE_CURL)
} // namespace repertory
#endif // defined(__cplusplus)

View File

@ -0,0 +1,145 @@
/*
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_ENCRYPTING_READER_HPP_
#define REPERTORY_INCLUDE_UTILS_ENCRYPTING_READER_HPP_
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
#include "utils/config.hpp"
#include "utils/hash.hpp"
#include "utils/types/file/i_file.hpp"
namespace repertory::utils::encryption {
class encrypting_reader final {
public:
encrypting_reader(std::string_view file_name, std::string_view source_path,
stop_type &stop_requested, std::string_view token,
std::optional<std::string> relative_parent_path,
std::size_t error_return = 0U);
encrypting_reader(std::string_view encrypted_file_path,
std::string_view source_path, stop_type &stop_requested,
std::string_view token, std::size_t error_return = 0U);
encrypting_reader(
std::string_view encrypted_file_path, std::string_view source_path,
stop_type &stop_requested, std::string_view token,
std::vector<std::array<unsigned char,
crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>>
iv_list,
std::size_t error_return = 0U);
encrypting_reader(const encrypting_reader &reader);
encrypting_reader(encrypting_reader &&) = delete;
auto operator=(const encrypting_reader &) -> encrypting_reader & = delete;
auto operator=(encrypting_reader &&) -> encrypting_reader & = delete;
~encrypting_reader() noexcept = default;
public:
using iostream = std::basic_iostream<char, std::char_traits<char>>;
using streambuf = std::basic_streambuf<char, std::char_traits<char>>;
private:
utils::encryption::hash_256_t key_;
stop_type &stop_requested_;
size_t error_return_;
std::unique_ptr<utils::file::i_file> source_file_;
private:
std::unordered_map<std::size_t, data_buffer> chunk_buffers_;
std::string encrypted_file_name_;
std::string encrypted_file_path_;
std::vector<
std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>>
iv_list_;
std::size_t last_data_chunk_{};
std::size_t last_data_chunk_size_{};
std::uint64_t read_offset_{};
std::uint64_t total_size_{};
private:
static const std::size_t header_size_;
static const std::size_t data_chunk_size_;
static const std::size_t encrypted_chunk_size_;
private:
auto reader_function(char *buffer, size_t size, size_t nitems) -> size_t;
public:
[[nodiscard]] static auto
calculate_decrypted_size(std::uint64_t total_size) -> std::uint64_t;
[[nodiscard]] static auto
calculate_encrypted_size(std::string_view source_path) -> std::uint64_t;
[[nodiscard]] auto create_iostream() const -> std::shared_ptr<iostream>;
[[nodiscard]] static constexpr auto
get_encrypted_chunk_size() -> std::size_t {
return encrypted_chunk_size_;
}
[[nodiscard]] static constexpr auto get_data_chunk_size() -> std::size_t {
return data_chunk_size_;
}
[[nodiscard]] auto get_encrypted_file_name() const -> std::string {
return encrypted_file_name_;
}
[[nodiscard]] auto get_encrypted_file_path() const -> std::string {
return encrypted_file_path_;
}
[[nodiscard]] auto get_error_return() const -> std::size_t {
return error_return_;
}
[[nodiscard]] auto get_iv_list()
-> std::vector<std::array<unsigned char,
crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>> {
return iv_list_;
}
[[nodiscard]] auto get_stop_requested() const -> bool {
return stop_requested_;
}
[[nodiscard]] auto get_total_size() const -> std::uint64_t {
return total_size_;
}
[[nodiscard]] static auto reader_function(char *buffer, size_t size,
size_t nitems,
void *instream) -> size_t {
return reinterpret_cast<encrypting_reader *>(instream)->reader_function(
buffer, size, nitems);
}
void set_read_position(std::uint64_t position) { read_offset_ = position; }
};
} // namespace repertory::utils::encryption
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
#endif // REPERTORY_INCLUDE_UTILS_ENCRYPTING_READER_HPP_

View File

@ -33,38 +33,30 @@ inline constexpr const std::uint32_t encryption_header_size{
crypto_aead_xchacha20poly1305_IETF_ABYTES,
};
template <typename hash_t>
[[nodiscard]] inline auto default_create_hash(std::string_view) -> hash_t;
template <typename hash_t>
[[nodiscard]] inline auto default_create_hash(std::wstring_view) -> hash_t;
template <typename hash_t>
inline auto generate_key(
std::string_view password,
std::optional<
std::function<hash_t(const unsigned char *data, std::size_t size)>>
hasher = std::nullopt) -> hash_t;
std::function<hash_t(const unsigned char *data, std::size_t size)> hasher =
default_create_hash<hash_t>()) -> hash_t;
template <typename hash_t>
inline auto generate_key(
std::wstring_view password,
std::optional<
std::function<hash_t(const unsigned char *data, std::size_t size)>>
hasher = std::nullopt) -> hash_t;
std::function<hash_t(const unsigned char *data, std::size_t size)> hasher =
default_create_hash<hash_t>()) -> hash_t;
#if defined(PROJECT_ENABLE_BOOST)
[[nodiscard]] auto decrypt_data(std::string_view password,
std::string_view data) -> data_buffer;
[[nodiscard]] auto decrypt_file_name(std::string_view encryption_token,
std::string &file_name) -> bool;
[[nodiscard]] auto encrypt_data(std::string_view password,
std::string_view data) -> data_buffer;
[[nodiscard]] auto decrypt_file_path(std::string_view encryption_token,
std::string &file_path) -> bool;
template <typename result, typename arr_t, std::size_t arr_size>
template <typename result_t, typename arr_t, std::size_t arr_size>
[[nodiscard]] inline auto decrypt_data(const std::array<arr_t, arr_size> &key,
const unsigned char *buffer,
std::size_t buffer_size,
result &res) -> bool {
result_t &res) -> bool {
if (buffer_size > encryption_header_size) {
const std::uint32_t size =
boost::endian::native_to_big(static_cast<std::uint32_t>(buffer_size));
@ -80,43 +72,42 @@ template <typename result, typename arr_t, std::size_t arr_size>
return false;
}
template <typename buffer, typename result, typename arr_t,
template <typename buffer_t, typename result_t, typename arr_t,
std::size_t arr_size>
[[nodiscard]] inline auto decrypt_data(const std::array<arr_t, arr_size> &key,
const buffer &buf, result &res) -> bool {
return decrypt_data<result>(
const buffer_t &buf,
result_t &res) -> bool {
return decrypt_data<result_t>(
key, reinterpret_cast<const unsigned char *>(buf.data()), buf.size(),
res);
}
template <typename buffer, typename result, typename hash_t = hash_256_t>
template <typename buffer_t, typename result_t, typename hash_t = hash_256_t>
[[nodiscard]] inline auto decrypt_data(
std::string_view password, const buffer &buf, result &res,
std::optional<
std::function<hash_t(const unsigned char *data, std::size_t size)>>
hasher = std::nullopt) -> bool {
return decrypt_data<buffer, result>(generate_key(password, hasher), buf, res);
std::string_view password, const buffer_t &buf, result_t &res,
std::function<hash_t(const unsigned char *data, std::size_t size)> hasher =
default_create_hash<hash_t>()) -> bool {
return decrypt_data<buffer_t, result_t>(generate_key(password, hasher), buf,
res);
}
template <typename result, typename hash_t = hash_256_t>
template <typename result_t, typename hash_t = hash_256_t>
[[nodiscard]] inline auto decrypt_data(
std::string_view password, const unsigned char *buffer,
std::size_t buffer_size, result &res,
std::optional<
std::function<hash_t(const unsigned char *data, std::size_t size)>>
hasher = std::nullopt) -> bool {
return decrypt_data<result>(generate_key(password, hasher), buffer,
buffer_size, res);
std::size_t buffer_size, result_t &res,
std::function<hash_t(const unsigned char *data, std::size_t size)> hasher =
default_create_hash<hash_t>()) -> bool {
return decrypt_data<result_t>(generate_key(password, hasher), buffer,
buffer_size, res);
}
template <typename result, typename arr_t, std::size_t arr_size>
template <typename result_t, typename arr_t, std::size_t arr_size>
inline void
encrypt_data(const std::array<unsigned char,
crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> &iv,
const std::array<arr_t, arr_size> &key,
const unsigned char *buffer, std::size_t buffer_size,
result &res) {
result_t &res) {
std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_ABYTES> mac{};
const std::uint32_t size = boost::endian::native_to_big(
@ -137,115 +128,86 @@ encrypt_data(const std::array<unsigned char,
std::memcpy(&res[iv.size()], mac.data(), mac.size());
}
template <typename result, typename s, std::size_t t>
inline void encrypt_data(const std::array<s, t> &key,
template <typename result_t, typename arr_t, std::size_t arr_size>
inline void encrypt_data(const std::array<arr_t, arr_size> &key,
const unsigned char *buffer, std::size_t buffer_size,
result &res) {
result_t &res) {
std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> iv{};
randombytes_buf(iv.data(), iv.size());
encrypt_data<result>(iv, key, buffer, buffer_size, res);
encrypt_data<result_t>(iv, key, buffer, buffer_size, res);
}
template <typename result, typename hash_t = hash_256_t>
template <typename result_t, typename hash_t = hash_256_t>
inline void encrypt_data(
std::string_view password, const unsigned char *buffer,
std::size_t buffer_size, result &res,
std::optional<
std::function<hash_t(const unsigned char *data, std::size_t size)>>
hasher = std::nullopt) {
encrypt_data<result>(generate_key(password, hasher), buffer, buffer_size,
res);
std::size_t buffer_size, result_t &res,
std::function<hash_t(const unsigned char *data, std::size_t size)> hasher =
default_create_hash<hash_t>()) {
encrypt_data<result_t>(generate_key(password, hasher), buffer, buffer_size,
res);
}
template <typename buffer, typename result, typename hash_t = hash_256_t>
template <typename buffer_t, typename result_t, typename hash_t = hash_256_t>
inline void encrypt_data(
std::string_view password, const buffer &buf, result &res,
std::optional<
std::function<hash_t(const unsigned char *data, std::size_t size)>>
hasher = std::nullopt) {
encrypt_data<result>(generate_key(password, hasher),
reinterpret_cast<const unsigned char *>(buf.data()),
buf.size(), res);
std::string_view password, const buffer_t &buf, result_t &res,
std::function<hash_t(const unsigned char *data, std::size_t size)> hasher =
default_create_hash<hash_t>()) {
encrypt_data<result_t>(generate_key(password, hasher),
reinterpret_cast<const unsigned char *>(buf.data()),
buf.size(), res);
}
template <typename buffer, typename result, typename s, std::size_t t>
inline void encrypt_data(const std::array<s, t> &key, const buffer &buf,
result &res) {
encrypt_data<result>(key, reinterpret_cast<const unsigned char *>(buf.data()),
buf.size(), res);
template <typename buffer_t, typename result_t, typename arr_t,
std::size_t arr_size>
inline void encrypt_data(const std::array<arr_t, arr_size> &key,
const buffer_t &buf, result_t &res) {
encrypt_data<result_t>(key,
reinterpret_cast<const unsigned char *>(buf.data()),
buf.size(), res);
}
template <typename buffer, typename result, typename s, std::size_t t>
template <typename buffer_t, typename result_t, typename arr_t,
std::size_t arr_size>
inline void
encrypt_data(const std::array<unsigned char,
crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> &iv,
const std::array<s, t> &key, const buffer &buf, result &res) {
encrypt_data<result>(iv, key,
reinterpret_cast<const unsigned char *>(buf.data()),
buf.size(), res);
const std::array<arr_t, arr_size> &key, const buffer_t &buf,
result_t &res) {
encrypt_data<result_t>(iv, key,
reinterpret_cast<const unsigned char *>(buf.data()),
buf.size(), res);
}
#if defined(PROJECT_ENABLE_CURL)
using reader_func_t =
std::function<bool(data_buffer &cypher_text, std::uint64_t start_offset,
std::uint64_t end_offset)>;
[[nodiscard]] auto
read_encrypted_range(const http_range &range,
const utils::encryption::hash_256_t &key,
reader_func_t reader_func, std::uint64_t total_size,
data_buffer &data) -> bool;
#endif // defined(PROJECT_ENABLE_CURL)
#endif // defined(PROJECT_ENABLE_BOOST)
template <>
inline auto
default_create_hash<hash_256_t>(std::string_view data) -> hash_256_t {
return create_hash_sha256(data);
}
template <>
inline auto
default_create_hash<hash_256_t>(std::wstring_view data) -> hash_256_t {
return create_hash_sha256(data);
}
template <>
inline auto
default_create_hash<hash_384_t>(std::string_view data) -> hash_384_t {
return create_hash_blake2b_384(data);
}
template <>
inline auto
default_create_hash<hash_384_t>(std::wstring_view data) -> hash_384_t {
return create_hash_blake2b_384(data);
}
template <>
inline auto
default_create_hash<hash_512_t>(std::string_view data) -> hash_512_t {
return create_hash_sha512(data);
}
template <>
inline auto
default_create_hash<hash_512_t>(std::wstring_view data) -> hash_512_t {
return create_hash_sha512(data);
}
template <typename hash_t>
inline auto generate_key(
std::string_view password,
std::optional<
std::function<hash_t(const unsigned char *data, std::size_t size)>>
hasher) -> hash_t {
return hasher.has_value() ? (*hasher)(reinterpret_cast<const unsigned char *>(
password.data()),
password.size())
: default_create_hash<hash_t>(password);
std::function<hash_t(const unsigned char *data, std::size_t size)> hasher)
-> hash_t {
return hasher(reinterpret_cast<const unsigned char *>(password.data()),
password.size());
}
template <typename hash_t>
inline auto generate_key(
std::wstring_view password,
std::optional<
std::function<hash_t(const unsigned char *data, std::size_t size)>>
hasher) -> hash_t {
return hasher.has_value()
? (*hasher)(
reinterpret_cast<const unsigned char *>(password.data()),
password.size() * sizeof(std::wstring_view::value_type))
: default_create_hash<hash_t>(password);
std::function<hash_t(const unsigned char *data, std::size_t size)> hasher)
-> hash_t {
return hasher(reinterpret_cast<const unsigned char *>(password.data()),
password.size() * sizeof(wchar_t));
}
} // namespace repertory::utils::encryption

View File

@ -42,11 +42,33 @@ protected:
i_exception_handler() = default;
};
struct iostream_exception_handler final : i_exception_handler {
void handle_exception(std::string_view function_name) const override {
std::cerr << function_name << "|exception|unknown" << std::endl;
}
void handle_exception(std::string_view function_name,
const std::exception &ex) const override {
std::cerr << function_name << "|exception|"
<< (ex.what() == nullptr ? "unknown" : ex.what()) << std::endl;
}
};
inline const iostream_exception_handler default_exception_handler{};
extern std::atomic<const i_exception_handler *> exception_handler;
#if defined(PROJECT_ENABLE_TESTING)
[[nodiscard]] inline auto
get_exception_handler() -> const i_exception_handler * {
return exception_handler;
}
#endif // defined(PROJECT_ENABLE_TESTING)
void handle_exception(std::string_view function_name);
void handle_exception(std::string_view function_name, const std::exception &ex);
void set_exception_handler(i_exception_handler *handler);
void set_exception_handler(const i_exception_handler *handler);
} // namespace repertory::utils::error
#endif // REPERTORY_INCLUDE_UTILS_ERROR_HPP_

View File

@ -24,128 +24,833 @@
#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 {
class file final {
[[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<std::uint64_t>;
// INFO: has test
[[nodiscard]] auto
get_free_drive_space(std::wstring_view path) -> std::optional<std::uint64_t>;
// INFO: has test
[[nodiscard]] auto get_time(std::string_view path,
time_type type) -> std::optional<std::uint64_t>;
// INFO: has test
[[nodiscard]] auto get_time(std::wstring_view path,
time_type type) -> std::optional<std::uint64_t>;
// INFO: has test
[[nodiscard]] auto
get_times(std::string_view path) -> std::optional<file_times>;
// INFO: has test
[[nodiscard]] auto
get_times(std::wstring_view path) -> std::optional<file_times>;
// INFO: has test
[[nodiscard]] auto
get_total_drive_space(std::string_view path) -> std::optional<std::uint64_t>;
// INFO: has test
[[nodiscard]] auto
get_total_drive_space(std::wstring_view path) -> std::optional<std::uint64_t>;
#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 open_file(std::filesystem::path path) -> file;
// [[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::filesystem::path path) -> file;
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);
}
protected:
file(std::fstream stream, std::filesystem::path path)
: path_(std::move(path)), stream_(std::move(stream)) {}
file() = default;
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 &&file_) noexcept = default;
~file() { close(); }
file(file &&move_file) noexcept
: file_(std::move(move_file.file_)),
path_(std::move(move_file.path_)),
read_only_(move_file.read_only_) {}
auto operator=(const file &) noexcept -> file & = delete;
auto operator=(file &&file_) noexcept -> file & = default;
~file() override { close(); }
private:
std::error_code error_{};
std::filesystem::path path_;
std::fstream stream_;
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();
void close() override;
[[nodiscard]] auto get_error_code() const -> std::error_code {
return error_;
[[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 get_path() const -> std::filesystem::path { return path_; }
[[nodiscard]] auto move_to(std::filesystem::path new_path) -> bool;
[[nodiscard]] auto read(data_buffer &data, std::uint64_t offset,
std::size_t *total_read = nullptr) -> bool {
return read_(reinterpret_cast<unsigned char *>(data.data()), data.size(),
offset, total_read);
[[nodiscard]] auto is_read_only() const -> bool override {
return read_only_;
}
[[nodiscard]] auto remove() -> bool;
[[nodiscard]] auto is_symlink() const -> bool override;
[[nodiscard]] auto truncate() -> bool { return truncate(0U); }
[[nodiscard]] auto move_to(std::string_view new_path) -> bool override;
[[nodiscard]] auto truncate(std::size_t size) -> 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 write(const data_buffer &data, std::uint64_t offset,
std::size_t *total_written = nullptr) -> bool {
return write_(reinterpret_cast<const unsigned char *>(data.data()),
data.size(), offset, total_written);
[[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_JSON)
[[nodiscard]] auto write_json(const nlohmann::json &data,
std::size_t *total_written = nullptr) -> bool {
auto str_data = data.dump();
return write_(reinterpret_cast<const unsigned char *>(str_data.c_str()),
str_data.size(), 0U, total_written);
#if defined(PROJECT_ENABLE_LIBSODIUM)
[[nodiscard]] auto sha256() -> std::optional<std::string>;
#endif // defined(PROJECT_ENABLE_LIBSODIUM)
[[nodiscard]] auto size() const -> std::optional<std::uint64_t> 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;
}
#endif // defined(PROJECT_ENABLE_JSON)
[[nodiscard]] operator bool() const { return stream_.is_open(); }
private:
[[nodiscard]] auto read_(unsigned char *data, std::size_t to_read,
std::uint64_t offset,
std::size_t *total_read) -> bool;
[[nodiscard]] auto write_(const unsigned char *data, std::size_t to_write,
std::size_t offset,
std::size_t *total_written) -> bool;
[[nodiscard]] operator bool() const override { return file_ != nullptr; }
};
[[nodiscard]] auto get_file_size(std::string_view path,
std::uint64_t &file_size) -> bool;
#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;
[[nodiscard]] auto get_file_size(std::wstring_view path,
std::uint64_t &file_size) -> bool;
public:
enc_file() noexcept = default;
[[nodiscard]] auto is_directory(std::string_view path) -> bool;
protected:
enc_file(fs_file_t file);
[[nodiscard]] auto is_directory(std::wstring_view path) -> bool;
public:
enc_file(const enc_file &) = delete;
[[nodiscard]] auto is_file(std::string_view path) -> bool;
enc_file(enc_file &&move_file) noexcept : file_(std::move(move_file.file_)) {}
[[nodiscard]] auto is_file(std::wstring_view path) -> bool;
~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<std::uint64_t> 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<std::uint64_t> 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<bool>(*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<std::uint64_t> 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<std::uint64_t> 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<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;
}
};
class directory final : public i_directory {
public:
using directory_t = std::unique_ptr<directory>;
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<fs_directory_t> override;
[[nodiscard]] auto
get_file(std::string_view path) const -> fs_file_t override;
[[nodiscard]] auto get_files() const -> std::vector<fs_file_t> override;
[[nodiscard]] auto get_items() const -> std::vector<fs_item_t> 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<smb_fd> 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<smb_fd> 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<std::uint64_t>;
[[nodiscard]] auto
get_time(time_type type) const -> std::optional<std::uint64_t> 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<std::uint64_t> 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<smb_directory>;
[[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<fs_directory_t> override;
[[nodiscard]] auto
get_file(std::string_view path) const -> fs_file_t override;
[[nodiscard]] auto get_files() const -> std::vector<fs_file_t> override;
[[nodiscard]] auto get_items() const -> std::vector<fs_item_t> override;
[[nodiscard]] auto get_path() const -> std::string override { return path_; }
[[nodiscard]] auto
get_time(time_type type) const -> std::optional<std::uint64_t> 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<std::string_view> password = std::nullopt) -> bool;
// INFO: has test
[[nodiscard]] auto read_json_file(
std::wstring_view path, nlohmann::json &data,
std::optional<std::wstring_view> password = std::nullopt) -> bool;
// INFO: has test
[[nodiscard]] auto write_json_file(
std::string_view path, const nlohmann::json &data,
std::optional<std::string_view> password = std::nullopt) -> bool;
// INFO: has test
[[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)
// 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 <typename string_t>
[[nodiscard]] 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 directory(utils::path::combine(path, {sub_directory})).exists();
}
// INFO: has test
template <typename string_t>
[[nodiscard]] 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 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<std::string>(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<std::wstring>(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<std::string>(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<std::wstring>(path, file_name);
}
} // namespace repertory::utils::file
#endif // REPERTORY_INCLUDE_UTILS_FILE_HPP_

View File

@ -35,219 +35,132 @@ using hash_512_t = std::array<unsigned char, 64U>;
[[nodiscard]] auto
create_hash_blake2b_256(std::wstring_view data) -> hash_256_t;
[[nodiscard]] auto
create_hash_blake2b_256(const data_buffer &data) -> hash_256_t;
[[nodiscard]] auto create_hash_blake2b_384(std::string_view data) -> hash_384_t;
[[nodiscard]] auto
create_hash_blake2b_384(std::wstring_view data) -> hash_384_t;
[[nodiscard]] auto create_hash_blake2b_512(std::string_view data) -> hash_512_t;
[[nodiscard]] auto
create_hash_blake2b_384(const data_buffer &data) -> hash_384_t;
[[nodiscard]] auto
create_hash_blake2b_512(std::wstring_view data) -> hash_512_t;
template <typename char_t, typename hash_t>
[[nodiscard]] auto
create_hash_blake2b_t(std::basic_string_view<char_t> data) -> hash_t;
create_hash_blake2b_512(const data_buffer &data) -> hash_512_t;
[[nodiscard]] auto create_hash_blake2b_512(std::string_view data) -> hash_512_t;
template <typename hash_t>
[[nodiscard]] auto create_hash_blake2b_t(const data_buffer &data) -> hash_t;
template <typename char_t, typename hash_t>
[[nodiscard]] auto create_hash_blake2b_t(
const std::vector<std::basic_string<char_t>> &data) -> hash_t;
template <typename arr_t, std::size_t arr_size>
[[nodiscard]] auto
create_hash_blake2b_t(const std::vector<std::array<arr_t, arr_size>> &data)
-> std::array<arr_t, arr_size>;
[[nodiscard]] auto create_hash_blake2b_t(const unsigned char *data,
std::size_t data_size) -> hash_t;
[[nodiscard]] auto create_hash_sha256(std::string_view data) -> hash_256_t;
[[nodiscard]] auto create_hash_sha256(std::wstring_view data) -> hash_256_t;
[[nodiscard]] auto create_hash_sha256(const data_buffer &data) -> hash_256_t;
[[nodiscard]] auto create_hash_sha256(const unsigned char *data,
std::size_t data_size) -> hash_256_t;
[[nodiscard]] auto create_hash_sha512(std::string_view data) -> hash_512_t;
[[nodiscard]] auto create_hash_sha512(std::wstring_view data) -> hash_512_t;
template <typename char_t>
[[nodiscard]] auto
create_hash_sha256_t(std::basic_string_view<char_t> data) -> hash_256_t;
[[nodiscard]] auto create_hash_sha512(const data_buffer &data) -> hash_512_t;
template <typename char_t>
[[nodiscard]] auto create_hash_sha512_t(std::basic_string_view<char_t> data)
-> repertory::utils::encryption::hash_512_t;
[[nodiscard]] auto create_hash_sha512(const unsigned char *data,
std::size_t data_size) -> hash_512_t;
template <typename hash_t>
auto create_hash_blake2b_t(const data_buffer &data) -> hash_t {
[[nodiscard]] inline auto default_create_hash() -> const
std::function<hash_t(const unsigned char *data, std::size_t size)> &;
template <typename hash_t>
auto create_hash_blake2b_t(const unsigned char *data,
std::size_t data_size) -> hash_t {
hash_t hash{};
crypto_generichash_blake2b_state state{};
auto res = crypto_generichash_blake2b_init(&state, nullptr, 0U, hash.size());
if (res != 0) {
throw std::runtime_error("failed to initialize blake2b|" +
throw std::runtime_error("failed to initialize blake2b-" +
std::to_string(hash.size() * 8U) + "|" +
std::to_string(res));
}
res = crypto_generichash_blake2b_update(
&state, reinterpret_cast<const unsigned char *>(data.data()),
data.size() * sizeof(data_buffer::value_type));
res = crypto_generichash_blake2b_update(&state, data, data_size);
if (res != 0) {
throw std::runtime_error("failed to update blake2b|" + std::to_string(res));
throw std::runtime_error("failed to update blake2b-" +
std::to_string(hash.size() * 8U) + "|" +
std::to_string(res));
}
res = crypto_generichash_blake2b_final(&state, hash.data(), hash.size());
if (res != 0) {
throw std::runtime_error("failed to finalize blake2b|" +
throw std::runtime_error("failed to finalize blake2b-" +
std::to_string(hash.size() * 8U) + "|" +
std::to_string(res));
}
return hash;
}
template <typename arr_t, std::size_t arr_size>
auto create_hash_blake2b_t(const std::vector<std::array<arr_t, arr_size>> &data)
-> std::array<arr_t, arr_size> {
using hash_t = std::array<arr_t, arr_size>;
hash_t hash{};
inline const std::function<hash_256_t(const unsigned char *data,
std::size_t size)>
blake2b_256_hasher =
[](const unsigned char *data, std::size_t data_size) -> hash_256_t {
return create_hash_blake2b_t<hash_256_t>(data, data_size);
};
crypto_generichash_blake2b_state state{};
auto res = crypto_generichash_blake2b_init(&state, nullptr, 0U, hash.size());
if (res != 0) {
throw std::runtime_error("failed to initialize blake2b|" +
std::to_string(res));
}
inline const std::function<hash_384_t(const unsigned char *data,
std::size_t size)>
blake2b_384_hasher =
[](const unsigned char *data, std::size_t data_size) -> hash_384_t {
return create_hash_blake2b_t<hash_384_t>(data, data_size);
};
for (const auto &item : data) {
res = crypto_generichash_blake2b_update(
&state, reinterpret_cast<const unsigned char *>(item.data()),
item.size());
if (res != 0) {
throw std::runtime_error("failed to update blake2b|" +
std::to_string(res));
}
}
inline const std::function<hash_512_t(const unsigned char *data,
std::size_t size)>
blake2b_512_hasher =
[](const unsigned char *data, std::size_t data_size) -> hash_512_t {
return create_hash_blake2b_t<hash_512_t>(data, data_size);
};
res = crypto_generichash_blake2b_final(&state, hash.data(), hash.size());
if (res != 0) {
throw std::runtime_error("failed to finalize blake2b|" +
std::to_string(res));
}
inline const std::function<hash_256_t(const unsigned char *data,
std::size_t size)>
sha256_hasher =
[](const unsigned char *data, std::size_t data_size) -> hash_256_t {
return create_hash_sha256(data, data_size);
};
return hash;
inline const std::function<hash_512_t(const unsigned char *data,
std::size_t size)>
sha512_hasher =
[](const unsigned char *data, std::size_t data_size) -> hash_512_t {
return create_hash_sha512(data, data_size);
};
template <>
[[nodiscard]] inline auto default_create_hash<hash_256_t>() -> const
std::function<hash_256_t(const unsigned char *data, std::size_t size)> & {
return blake2b_256_hasher;
}
template <typename char_t, typename hash_t>
auto create_hash_blake2b_t(const std::vector<std::basic_string<char_t>> &data)
-> hash_t {
hash_t hash{};
crypto_generichash_blake2b_state state{};
auto res = crypto_generichash_blake2b_init(&state, nullptr, 0U, hash.size());
if (res != 0) {
throw std::runtime_error("failed to initialize blake2b|" +
std::to_string(res));
}
for (const auto &item : data) {
res = crypto_generichash_blake2b_update(
&state, reinterpret_cast<const unsigned char *>(item.data()),
item.size() * sizeof(char_t));
if (res != 0) {
throw std::runtime_error("failed to update blake2b|" +
std::to_string(res));
}
}
res = crypto_generichash_blake2b_final(&state, hash.data(), hash.size());
if (res != 0) {
throw std::runtime_error("failed to finalize blake2b|" +
std::to_string(res));
}
return hash;
template <>
[[nodiscard]] inline auto default_create_hash<hash_384_t>() -> const
std::function<hash_384_t(const unsigned char *data, std::size_t size)> & {
return blake2b_384_hasher;
}
template <typename char_t, typename hash_t>
auto create_hash_blake2b_t(std::basic_string_view<char_t> data) -> hash_t {
hash_t hash{};
crypto_generichash_blake2b_state state{};
auto res = crypto_generichash_blake2b_init(&state, nullptr, 0U, hash.size());
if (res != 0) {
throw std::runtime_error("failed to initialize blake2b|" +
std::to_string(res));
}
res = crypto_generichash_blake2b_update(
&state, reinterpret_cast<const unsigned char *>(data.data()),
data.size() * sizeof(char_t));
if (res != 0) {
throw std::runtime_error("failed to update blake2b|" + std::to_string(res));
}
res = crypto_generichash_blake2b_final(&state, hash.data(), hash.size());
if (res != 0) {
throw std::runtime_error("failed to finalize blake2b|" +
std::to_string(res));
}
return hash;
}
template <typename char_t>
auto create_hash_sha256_t(std::basic_string_view<char_t> data)
-> repertory::utils::encryption::hash_256_t {
repertory::utils::encryption::hash_256_t hash{};
crypto_hash_sha256_state state{};
auto res = crypto_hash_sha256_init(&state);
if (res != 0) {
throw std::runtime_error("failed to initialize sha256|" +
std::to_string(res));
}
res = crypto_hash_sha256_update(
&state, reinterpret_cast<const unsigned char *>(data.data()),
data.size() * sizeof(char_t));
if (res != 0) {
throw std::runtime_error("failed to update sha256|" + std::to_string(res));
}
res = crypto_hash_sha256_final(&state, hash.data());
if (res != 0) {
throw std::runtime_error("failed to finalize sha256|" +
std::to_string(res));
}
return hash;
}
template <typename char_t>
auto create_hash_sha512_t(std::basic_string_view<char_t> data)
-> repertory::utils::encryption::hash_512_t {
repertory::utils::encryption::hash_512_t hash{};
crypto_hash_sha512_state state{};
auto res = crypto_hash_sha512_init(&state);
if (res != 0) {
throw std::runtime_error("failed to initialize sha512|" +
std::to_string(res));
}
res = crypto_hash_sha512_update(
&state, reinterpret_cast<const unsigned char *>(data.data()),
data.size() * sizeof(char_t));
if (res != 0) {
throw std::runtime_error("failed to update sha512|" + std::to_string(res));
}
res = crypto_hash_sha512_final(&state, hash.data());
if (res != 0) {
throw std::runtime_error("failed to finalize sha512|" +
std::to_string(res));
}
return hash;
template <>
[[nodiscard]] inline auto default_create_hash<hash_512_t>() -> const
std::function<hash_512_t(const unsigned char *data, std::size_t size)> & {
return blake2b_512_hasher;
}
} // namespace repertory::utils::encryption

View File

@ -30,8 +30,12 @@ inline constexpr const std::string_view backslash{"\\"};
inline constexpr const std::wstring_view backslash_w{L"\\"};
inline constexpr const std::string_view dot{"."};
inline constexpr const std::wstring_view dot_w{L"."};
inline constexpr const std::string_view dot_backslash{".\\"};
inline constexpr const std::wstring_view dot_backslash_w{L".\\"};
inline constexpr const std::string_view dot_slash{"./"};
inline constexpr const std::wstring_view dot_slash_w{L"./"};
inline constexpr const std::string_view long_notation{"\\\\?\\"};
inline constexpr const std::wstring_view long_notation_w{L"\\\\?\\"};
inline constexpr const std::string_view slash{"/"};
inline constexpr const std::wstring_view slash_w{L"/"};
#if defined(_WIN32)
@ -39,6 +43,8 @@ inline constexpr const std::string_view directory_seperator{backslash};
inline constexpr const std::wstring_view directory_seperator_w{backslash_w};
inline constexpr const std::string_view not_directory_seperator{slash};
inline constexpr const std::wstring_view not_directory_seperator_w{slash_w};
inline constexpr const std::string_view unc_notation{"\\\\"};
inline constexpr const std::wstring_view unc_notation_w{L"\\\\"};
#else // !defined(_WIN32)
inline constexpr const std::string_view directory_seperator{slash};
inline constexpr const std::wstring_view directory_seperator_w{slash_w};
@ -62,53 +68,6 @@ get_backslash<wchar_t>() -> std::basic_string_view<wchar_t> {
return backslash_w;
}
template <typename char_t>
[[nodiscard]] inline constexpr auto get_dot() -> std::basic_string_view<char_t>;
template <>
[[nodiscard]] inline constexpr auto
get_dot<char>() -> std::basic_string_view<char> {
return dot;
}
template <>
[[nodiscard]] inline constexpr auto
get_dot<wchar_t>() -> std::basic_string_view<wchar_t> {
return dot_w;
}
template <typename char_t>
[[nodiscard]] inline constexpr auto
get_dot_slash() -> std::basic_string_view<char_t>;
template <>
[[nodiscard]] inline constexpr auto
get_dot_slash<char>() -> std::basic_string_view<char> {
return dot_slash;
}
template <>
[[nodiscard]] inline constexpr auto
get_dot_slash<wchar_t>() -> std::basic_string_view<wchar_t> {
return dot_slash_w;
}
template <typename char_t>
[[nodiscard]] inline constexpr auto
get_slash() -> std::basic_string_view<char_t>;
template <>
[[nodiscard]] inline constexpr auto
get_slash<char>() -> std::basic_string_view<char> {
return slash;
}
template <>
[[nodiscard]] inline constexpr auto
get_slash<wchar_t>() -> std::basic_string_view<wchar_t> {
return slash_w;
}
template <typename char_t>
[[nodiscard]] inline constexpr auto
get_directory_seperator() -> std::basic_string_view<char_t>;
@ -141,37 +100,130 @@ get_not_directory_seperator<wchar_t>() -> std::basic_string_view<wchar_t> {
return not_directory_seperator_w;
}
template <typename char_t>
[[nodiscard]] inline constexpr auto get_dot() -> std::basic_string_view<char_t>;
template <>
[[nodiscard]] inline constexpr auto
get_dot<char>() -> std::basic_string_view<char> {
return dot;
}
template <>
[[nodiscard]] inline constexpr auto
get_dot<wchar_t>() -> std::basic_string_view<wchar_t> {
return dot_w;
}
template <typename char_t>
[[nodiscard]] inline constexpr auto
get_dot_backslash() -> std::basic_string_view<char_t>;
template <>
[[nodiscard]] inline constexpr auto
get_dot_backslash<char>() -> std::basic_string_view<char> {
return dot_backslash;
}
template <>
[[nodiscard]] inline constexpr auto
get_dot_backslash<wchar_t>() -> std::basic_string_view<wchar_t> {
return dot_backslash_w;
}
template <typename char_t>
[[nodiscard]] inline constexpr auto
get_dot_slash() -> std::basic_string_view<char_t>;
template <>
[[nodiscard]] inline constexpr auto
get_dot_slash<char>() -> std::basic_string_view<char> {
return dot_slash;
}
template <>
[[nodiscard]] inline constexpr auto
get_dot_slash<wchar_t>() -> std::basic_string_view<wchar_t> {
return dot_slash_w;
}
template <typename char_t>
[[nodiscard]] inline constexpr auto
get_long_notation() -> std::basic_string_view<char_t>;
template <>
[[nodiscard]] inline constexpr auto
get_long_notation<char>() -> std::basic_string_view<char> {
return long_notation;
}
template <>
[[nodiscard]] inline constexpr auto
get_long_notation<wchar_t>() -> std::basic_string_view<wchar_t> {
return long_notation_w;
}
template <typename char_t>
[[nodiscard]] inline constexpr auto
get_slash() -> std::basic_string_view<char_t>;
template <>
[[nodiscard]] inline constexpr auto
get_slash<char>() -> std::basic_string_view<char> {
return slash;
}
template <>
[[nodiscard]] inline constexpr auto
get_slash<wchar_t>() -> std::basic_string_view<wchar_t> {
return slash_w;
}
#if defined(_WIN32)
template <typename char_t>
[[nodiscard]] inline constexpr auto
get_unc_notation() -> std::basic_string_view<char_t>;
template <>
[[nodiscard]] inline constexpr auto
get_unc_notation<char>() -> std::basic_string_view<char> {
return unc_notation;
}
template <>
[[nodiscard]] inline constexpr auto
get_unc_notation<wchar_t>() -> std::basic_string_view<wchar_t> {
return unc_notation_w;
}
#endif // defined(_WIN32)
template <typename string_t>
[[nodiscard]] inline auto get_current_path() -> string_t;
[[nodiscard]] auto absolute(std::string_view path) -> std::string;
[[nodiscard]] auto absolute(std::wstring_view path) -> std::wstring;
[[nodiscard]] inline auto
combine(std::string path,
combine(std::string_view path,
const std::vector<std::string_view> &paths) -> std::string;
[[nodiscard]] inline auto
combine(std::wstring path,
combine(std::wstring_view path,
const std::vector<std::wstring_view> &paths) -> std::wstring;
[[nodiscard]] auto contains_trash_directory(std::string_view path) -> bool;
[[nodiscard]] auto contains_trash_directory(std::wstring_view path) -> bool;
[[nodiscard]] auto inline create_api_path(std::string_view path) -> std::string;
[[nodiscard]] auto inline create_api_path(std::wstring_view path)
-> std::wstring;
[[nodiscard]] inline auto
directory_exists_in_path(std::string_view path,
std::string_view sub_directory) -> bool;
[[nodiscard]] auto exists(std::string_view path) -> bool;
[[nodiscard]] inline auto
directory_exists_in_path(std::wstring_view path,
std::wstring_view sub_directory) -> bool;
[[nodiscard]] inline auto
file_exists_in_path(std::string_view path, std::string_view file_name) -> bool;
[[nodiscard]] inline auto
file_exists_in_path(std::wstring_view path,
std::wstring_view file_name) -> bool;
[[nodiscard]] auto exists(std::wstring_view path) -> bool;
[[nodiscard]] inline auto finalize(std::string_view path) -> std::string;
@ -196,22 +248,27 @@ get_parent_api_path(std::string_view path) -> std::string;
[[nodiscard]] inline auto
get_parent_api_path(std::wstring_view path) -> std::wstring;
[[nodiscard]] auto get_parent_directory(std::string_view path) -> std::string;
[[nodiscard]] auto get_parent_path(std::string_view path) -> std::string;
[[nodiscard]] auto get_parent_directory(std::wstring_view path) -> std::wstring;
[[nodiscard]] auto get_parent_path(std::wstring_view path) -> std::wstring;
[[nodiscard]] auto is_trash_directory(std::string_view path) -> bool;
[[nodiscard]] inline auto
get_parts(std::string_view path) -> std::vector<std::string>;
[[nodiscard]] auto is_trash_directory(std::wstring_view path) -> bool;
[[nodiscard]] inline auto
get_parts_w(std::wstring_view path) -> std::vector<std::wstring>;
[[nodiscard]] auto get_relative_path(std::string_view path,
std::string_view root_path) -> std::string;
[[nodiscard]] auto
get_relative_path(std::wstring_view path,
std::wstring_view root_path) -> std::wstring;
[[nodiscard]] auto make_file_uri(std::string_view path) -> std::string;
[[nodiscard]] auto make_file_uri(std::wstring_view path) -> std::wstring;
[[nodiscard]] auto remove_file_name(std::string_view path) -> std::string;
[[nodiscard]] auto remove_file_name(std::wstring_view path) -> std::wstring;
[[nodiscard]] auto strip_to_file_name(std::string path) -> std::string;
[[nodiscard]] auto strip_to_file_name(std::wstring path) -> std::wstring;
@ -222,31 +279,30 @@ get_parent_api_path(std::wstring_view path) -> std::wstring;
template <typename string_t>
[[nodiscard]] inline auto combine_t(
string_t path,
std::basic_string_view<typename string_t::value_type> path,
const std::vector<std::basic_string_view<typename string_t::value_type>>
&paths) -> string_t {
path = std::accumulate(
paths.begin(), paths.end(), path, [](auto next_path, auto &&path_part) {
if (next_path.empty()) {
return string_t{path_part};
}
auto dir_sep_t =
string_t{get_directory_seperator<typename string_t::value_type>()};
return absolute(
std::accumulate(paths.begin(), paths.end(),
std::basic_string<typename string_t::value_type>{path},
[&dir_sep_t](auto next_path, auto &&path_part) {
if (next_path.empty()) {
return string_t{path_part};
}
return next_path +
string_t{
get_directory_seperator<typename string_t::value_type>()} +
string_t{path_part};
});
return absolute(path);
return next_path + dir_sep_t + string_t{path_part};
}));
}
inline auto combine(std::string path,
inline auto combine(std::string_view path,
const std::vector<std::string_view> &paths) -> std::string {
return combine_t<std::string>(path, paths);
}
inline auto
combine(std::wstring path,
combine(std::wstring_view path,
const std::vector<std::wstring_view> &paths) -> std::wstring {
return combine_t<std::wstring>(path, paths);
}
@ -255,12 +311,20 @@ template <typename string_t>
[[nodiscard]] inline auto create_api_path_t(
std::basic_string_view<typename string_t::value_type> path) -> string_t {
auto backslash_t = get_backslash<typename string_t::value_type>();
auto dot_t = get_dot<typename string_t::value_type>();
auto dot_backslash_t = get_dot_backslash<typename string_t::value_type>();
auto dot_slash_t = get_dot_slash<typename string_t::value_type>();
auto dot_t = get_dot<typename string_t::value_type>();
auto slash_t = get_slash<typename string_t::value_type>();
#if defined(_WIN32)
auto long_notation_t = get_long_notation<typename string_t::value_type>();
if (utils::string::begins_with(path, long_notation_t)) {
path = path.substr(long_notation_t.size());
}
#endif // defined(_WIN32)
if (path.empty() || path == backslash_t || path == dot_t ||
path == dot_slash_t || path == slash_t) {
path == dot_slash_t || path == slash_t || path == dot_backslash_t) {
return string_t{slash_t};
}
@ -271,16 +335,12 @@ template <typename string_t>
}
#endif // defined(_WIN32)
format_path(api_path, slash_t, backslash_t);
while (utils::string::begins_with(api_path, dot_slash_t)) {
api_path = api_path.substr(dot_slash_t.size());
}
while (utils::string::begins_with(api_path, dot_t)) {
api_path = api_path.substr(dot_t.size());
}
format_path(api_path, slash_t, backslash_t);
if (api_path.at(0U) != slash_t.at(0U)) {
return string_t{slash_t} + api_path;
}
@ -296,50 +356,57 @@ inline auto create_api_path(std::wstring_view path) -> std::wstring {
return create_api_path_t<std::wstring>(path);
}
template <typename string_t>
[[nodiscard]] 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 std::filesystem::is_directory(
std::filesystem::path(path).append(sub_directory));
}
inline auto directory_exists_in_path(std::string_view path,
std::string_view sub_directory) -> bool {
return directory_exists_in_path_t<std::string>(path, sub_directory);
}
inline auto directory_exists_in_path(std::wstring_view path,
std::wstring_view sub_directory) -> bool {
return directory_exists_in_path_t<std::wstring>(path, sub_directory);
}
template <typename string_t>
[[nodiscard]] 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 std::filesystem::is_regular_file(
std::filesystem::path(path).append(file_name));
}
inline auto file_exists_in_path(std::string_view path,
std::string_view file_name) -> bool {
return file_exists_in_path_t<std::string>(path, file_name);
}
inline auto file_exists_in_path(std::wstring_view path,
std::wstring_view file_name) -> bool {
return file_exists_in_path_t<std::wstring>(path, file_name);
}
template <typename string_t>
[[nodiscard]] inline auto finalize_t(
std::basic_string_view<typename string_t::value_type> path) -> string_t {
string_t dir_sep_t{get_directory_seperator<typename string_t::value_type>()};
string_t fmt_path{path};
format_path(fmt_path,
get_directory_seperator<typename string_t::value_type>(),
if (fmt_path.empty()) {
return fmt_path;
}
format_path(fmt_path, dir_sep_t,
get_not_directory_seperator<typename string_t::value_type>());
#if defined(_WIN32)
auto unc_notation_t = get_unc_notation<typename string_t::value_type>();
if (utils::string::begins_with(fmt_path, unc_notation_t)) {
return fmt_path;
}
auto dot_t = get_dot<typename string_t::value_type>();
auto dot_sep_t = string_t{dot_t} + dir_sep_t;
if (fmt_path == dot_t || fmt_path == dot_sep_t) {
return get_current_path<string_t>();
}
if (fmt_path == dir_sep_t) {
#if defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES)
return get_current_path<string_t>().substr(0U, long_notation.size() + 2U);
#else // !defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES)
return get_current_path<string_t>().substr(0U, 2U);
#endif // defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES)
}
if (utils::string::begins_with(fmt_path, dir_sep_t)) {
#if defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES)
return get_current_path<string_t>().substr(0U, long_notation.size() + 2U) +
fmt_path;
#else // !defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES)
return get_current_path<string_t>().substr(0U, 2U) + fmt_path;
#endif // defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES)
}
if (utils::string::begins_with(fmt_path, dot_sep_t)) {
return get_current_path<string_t>() + dir_sep_t + fmt_path.substr(2U);
}
#if defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES)
return string_t{get_long_notation<typename string_t::value_type>()} +
fmt_path;
#endif // defined(PROJECT_ENABLE_WIN32_LONG_PATH_NAMES)
#endif // defined(_WIN32)
return fmt_path;
}
@ -359,6 +426,19 @@ format_path(string_t &path,
-> string_t & {
utils::string::replace(path, not_sep, sep);
#if defined(_WIN32)
auto is_unc{false};
auto long_notation_t = get_long_notation<typename string_t::value_type>();
auto unc_notation_t = get_unc_notation<typename string_t::value_type>();
if (utils::string::begins_with(path, long_notation_t)) {
path = path.substr(long_notation_t.size());
} else if (utils::string::begins_with(path, unc_notation_t)) {
path = path.substr(unc_notation_t.size());
utils::string::left_trim(path, sep.at(0U));
is_unc = true;
}
#endif // defined(_WIN32)
string_t double_sep(2U, sep.at(0U));
while (utils::string::contains(path, double_sep)) {
utils::string::replace(path, double_sep, sep);
@ -369,7 +449,9 @@ format_path(string_t &path,
}
#if defined(_WIN32)
if ((path.size() >= 2U) && (path.at(1U) == ':')) {
if (is_unc) {
path = string_t{unc_notation_t} + path;
} else if ((path.size() >= 2U) && (path.at(1U) == ':')) {
path[0U] = utils::string::to_lower(string_t(1U, path.at(0U))).at(0U);
}
#endif // defined(_WIN32)
@ -377,6 +459,24 @@ format_path(string_t &path,
return path;
}
template <>
[[nodiscard]] inline auto get_current_path<std::string>() -> std::string {
#if defined(_WIN32)
std::string path;
path.resize(repertory::max_path_length + 1);
::GetCurrentDirectoryA(static_cast<DWORD>(path.size()), path.data());
path = path.c_str();
return finalize(path);
#else // !defined(_WIN32)
return finalize(std::filesystem::current_path().string());
#endif // defined(_WIN32)
}
template <>
[[nodiscard]] inline auto get_current_path<std::wstring>() -> std::wstring {
return utils::string::from_utf8(get_current_path<std::string>());
}
template <typename string_t>
[[nodiscard]] inline auto get_parent_api_path_t(
std::basic_string_view<typename string_t::value_type> path) -> string_t {
@ -387,12 +487,12 @@ template <typename string_t>
return ret;
}
ret = path.substr(0, path.rfind('/') + 1);
ret = path.substr(0, path.rfind(slash_t) + 1);
if (ret == slash_t) {
return ret;
}
return utils::string::right_trim(ret, '/');
return utils::string::right_trim(ret, slash_t.at(0U));
}
inline auto get_parent_api_path(std::string_view path) -> std::string {
@ -402,6 +502,23 @@ inline auto get_parent_api_path(std::string_view path) -> std::string {
inline auto get_parent_api_path(std::wstring_view path) -> std::wstring {
return get_parent_api_path_t<std::wstring>(path);
}
template <typename string_t>
[[nodiscard]] inline auto
get_parts_t(std::basic_string_view<typename string_t::value_type> path)
-> std::vector<string_t> {
return utils::string::split(
path, get_directory_seperator<typename string_t::value_type>().at(0U),
false);
}
inline auto get_parts(std::string_view path) -> std::vector<std::string> {
return get_parts_t<std::string>(path);
}
inline auto get_parts_w(std::wstring_view path) -> std::vector<std::wstring> {
return get_parts_t<std::wstring>(path);
}
} // namespace repertory::utils::path
#endif // REPERTORY_INCLUDE_UTILS_PATH_HPP_

View File

@ -25,7 +25,9 @@
#include "utils/config.hpp"
namespace repertory::utils::time {
inline constexpr const auto NANOS_PER_SECOND = 1000000000L;
inline constexpr const auto NANOS_PER_SECOND{1000000000ULL};
inline constexpr const auto WIN32_TIME_CONVERSION{116444736000000000ULL};
inline constexpr const auto WIN32_TIME_NANOS_PER_TICK{100ULL};
#if defined(PROJECT_ENABLE_SPDLOG) || defined(PROJECT_ENABLE_FMT)
[[nodiscard]] inline auto convert_to_utc(time_t time) -> std::time_t {
@ -34,11 +36,6 @@ inline constexpr const auto NANOS_PER_SECOND = 1000000000L;
}
#endif // defined(PROJECT_ENABLE_SPDLOG) || defined(PROJECT_ENABLE_FMT)
#if defined(_WIN32)
[[nodiscard]] auto
filetime_to_unix_time(const FILETIME &file_time) -> std::uint64_t;
#endif // defined(_WIN32)
#if defined(PROJECT_ENABLE_SPDLOG) || defined(PROJECT_ENABLE_FMT)
[[nodiscard]] inline auto get_current_time_utc() -> std::time_t {
auto calendar_time = fmt::gmtime(std::time(nullptr));
@ -46,8 +43,6 @@ filetime_to_unix_time(const FILETIME &file_time) -> std::uint64_t;
}
#endif // defined(PROJECT_ENABLE_SPDLOG) || defined(PROJECT_ENABLE_FMT)
[[nodiscard]] auto get_file_time_now() -> std::uint64_t;
void get_local_time_now(struct tm &local_time);
[[nodiscard]] auto get_time_now() -> std::uint64_t;
@ -55,10 +50,20 @@ void get_local_time_now(struct tm &local_time);
#if defined(_WIN32)
auto strptime(const char *s, const char *f, struct tm *tm) -> const char *;
[[nodiscard]] auto time64_to_unix_time(const __time64_t &time) -> std::uint64_t;
[[nodiscard]] auto unix_time_to_filetime(std::uint64_t unix_time) -> FILETIME;
[[nodiscard]] auto
windows_file_time_to_unix_time(FILETIME win_time) -> std::uint64_t;
[[nodiscard]] auto
windows_time_t_to_unix_time(__time64_t win_time) -> std::uint64_t;
#endif // defined(_WIN32)
[[nodiscard]] auto
unix_time_to_windows_time(std::uint64_t unix_time) -> std::uint64_t;
[[nodiscard]] auto
windows_time_to_unix_time(std::uint64_t win_time) -> std::uint64_t;
} // namespace repertory::utils::time
#endif // REPERTORY_INCLUDE_UTILS_TIME_HPP_

View File

@ -0,0 +1,79 @@
/*
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_TYPES_FILE_I_DIRECTORY_HPP_
#define REPERTORY_INCLUDE_TYPES_FILE_I_DIRECTORY_HPP_
#include "utils/config.hpp"
#include "utils/types/file/i_file.hpp"
#include "utils/types/file/i_fs_item.hpp"
namespace repertory::utils::file {
struct i_directory : public i_fs_item {
using fs_directory_t = std::unique_ptr<i_directory>;
using fs_file_t = i_file::fs_file_t;
virtual ~i_directory() = default;
[[nodiscard]] virtual auto
count(bool recursive = false) const -> std::uint64_t = 0;
[[nodiscard]] virtual auto
create_directory(std::string_view path = "") const -> fs_directory_t = 0;
[[nodiscard]] virtual auto create_file(std::string_view file_name,
bool read_only) const -> fs_file_t = 0;
[[nodiscard]] auto is_directory_item() const -> bool override { return true; }
[[nodiscard]] virtual auto
get_directory(std::string_view path) const -> fs_directory_t = 0;
[[nodiscard]] virtual auto
get_directories() const -> std::vector<fs_directory_t> = 0;
[[nodiscard]] virtual auto
get_file(std::string_view path) const -> fs_file_t = 0;
[[nodiscard]] virtual auto get_files() const -> std::vector<fs_file_t> = 0;
[[nodiscard]] virtual auto get_items() const -> std::vector<fs_item_t> = 0;
[[nodiscard]] virtual auto remove_recursively() -> bool = 0;
[[nodiscard]] virtual auto
size(bool recursive = false) const -> std::uint64_t = 0;
protected:
i_directory() noexcept = default;
i_directory(const i_directory &) noexcept = default;
i_directory(i_directory &&) noexcept = default;
auto operator=(i_directory &&) noexcept -> i_directory & = default;
auto operator=(const i_directory &) noexcept -> i_directory & = default;
};
} // namespace repertory::utils::file
#endif // REPERTORY_INCLUDE_TYPES_FILE_I_DIRECTORY_HPP_

View File

@ -0,0 +1,93 @@
/*
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_TYPES_FILE_I_FILE_HPP_
#define REPERTORY_INCLUDE_TYPES_FILE_I_FILE_HPP_
#include "utils/config.hpp"
#include "utils/types/file/i_fs_item.hpp"
namespace repertory::utils::file {
struct i_file : public i_fs_item {
using fs_file_t = std::unique_ptr<i_file>;
virtual ~i_file() = default;
virtual void close() = 0;
virtual void flush() const = 0;
[[nodiscard]] virtual auto get_handle() const -> native_handle = 0;
[[nodiscard]] virtual auto get_read_buffer_size() const -> std::uint32_t = 0;
[[nodiscard]] auto is_directory_item() const -> bool override {
return false;
}
[[nodiscard]] virtual auto is_read_only() const -> bool = 0;
[[nodiscard]] virtual auto read(data_buffer &data, std::uint64_t offset,
std::size_t *total_read = nullptr) -> bool {
return read(data.data(), data.size(), offset, total_read);
}
[[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;
virtual auto set_read_buffer_size(std::uint32_t size) -> std::uint32_t = 0;
[[nodiscard]] virtual auto size() const -> std::optional<std::uint64_t> = 0;
[[nodiscard]] virtual auto truncate() -> bool { return truncate(0U); }
[[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 {
return write(data.data(), data.size(), offset, total_written);
}
[[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;
protected:
i_file() noexcept = default;
i_file(const i_file &) noexcept = default;
i_file(i_file &&) noexcept = default;
auto operator=(i_file &&) noexcept -> i_file & = default;
auto operator=(const i_file &) noexcept -> i_file & = default;
};
} // namespace repertory::utils::file
#endif // REPERTORY_INCLUDE_TYPES_FILE_I_FILE_HPP_

View File

@ -0,0 +1,111 @@
/*
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_TYPES_FILE_I_FS_ITEM_HPP_
#define REPERTORY_INCLUDE_TYPES_FILE_I_FS_ITEM_HPP_
#include "utils/config.hpp"
#include "utils/string.hpp"
namespace repertory::utils::file {
enum class time_type {
accessed,
created,
modified,
written,
};
struct file_times final {
std::uint64_t accessed{};
std::uint64_t created{};
std::uint64_t modified{};
std::uint64_t written{};
[[nodiscard]] auto get(time_type type) const -> std::uint64_t {
switch (type) {
case time_type::accessed:
return accessed;
case time_type::created:
return created;
case time_type::modified:
return modified;
case time_type::written:
return written;
}
throw std::runtime_error("type_type not supported");
}
};
struct i_fs_item {
using fs_item_t = std::unique_ptr<i_fs_item>;
virtual ~i_fs_item() = default;
[[nodiscard]] virtual auto copy_to(std::string_view to_path,
bool overwrite) const -> bool = 0;
[[nodiscard]] virtual auto copy_to(std::wstring_view new_path,
bool overwrite) -> bool {
return copy_to(utils::string::to_utf8(new_path), overwrite);
}
[[nodiscard]] virtual auto exists() const -> bool = 0;
[[nodiscard]] virtual auto get_path() const -> std::string = 0;
[[nodiscard]] virtual auto
get_time(time_type type) const -> std::optional<std::uint64_t>;
[[nodiscard]] virtual auto is_directory_item() const -> bool = 0;
[[nodiscard]] auto is_file_item() const -> bool {
return not is_directory_item();
}
[[nodiscard]] virtual auto is_symlink() const -> bool = 0;
[[nodiscard]] virtual auto move_to(std::string_view new_path) -> bool = 0;
[[nodiscard]] virtual auto move_to(std::wstring_view new_path) -> bool {
return move_to(utils::string::to_utf8(new_path));
}
[[nodiscard]] virtual auto remove() -> bool = 0;
public:
[[nodiscard]] virtual operator bool() const = 0;
protected:
i_fs_item() noexcept = default;
i_fs_item(const i_fs_item &) noexcept = default;
i_fs_item(i_fs_item &&) noexcept = default;
auto operator=(i_fs_item &&) noexcept -> i_fs_item & = default;
auto operator=(const i_fs_item &) noexcept -> i_fs_item & = default;
};
} // namespace repertory::utils::file
#endif // REPERTORY_INCLUDE_TYPES_FILE_I_FS_ITEM_HPP_