move to new build system

This commit is contained in:
2024-06-06 14:17:47 -05:00
parent 88d8bf63f5
commit aee68520b3
563 changed files with 4283 additions and 361439 deletions

View File

@ -0,0 +1,162 @@
// NOLINTBEGIN
#ifndef _MACARON_BASE64_H_
#define _MACARON_BASE64_H_
/**
* The MIT License (MIT)
* Copyright (c) 2016 tomykaira
*
* 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.
*/
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunknown-warning-option"
#endif
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#pragma GCC diagnostic ignored "-Wold-style-cast"
#pragma GCC diagnostic ignored "-Wuseless-cast"
#endif
#include <string>
#include <vector>
namespace macaron::Base64 {
static std::string Encode(const char *data, const size_t &len) {
static constexpr char sEncodingTable[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
size_t in_len = len;
std::string ret;
if (in_len > 0) {
size_t out_len = 4 * ((in_len + 2) / 3);
ret = std::string(out_len, '\0');
size_t i;
char *p = const_cast<char *>(ret.c_str());
for (i = 0; i < in_len - 2; i += 3) {
*p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
*p++ = sEncodingTable[((data[i] & 0x3) << 4) |
((int)(data[i + 1] & 0xF0) >> 4)];
*p++ = sEncodingTable[((data[i + 1] & 0xF) << 2) |
((int)(data[i + 2] & 0xC0) >> 6)];
*p++ = sEncodingTable[data[i + 2] & 0x3F];
}
if (i < in_len) {
*p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
if (i == (in_len - 1)) {
*p++ = sEncodingTable[((data[i] & 0x3) << 4)];
*p++ = '=';
} else {
*p++ = sEncodingTable[((data[i] & 0x3) << 4) |
((int)(data[i + 1] & 0xF0) >> 4)];
*p++ = sEncodingTable[((data[i + 1] & 0xF) << 2)];
}
*p++ = '=';
}
}
return ret;
}
[[maybe_unused]] static std::string Encode(const std::string &data) {
return Encode(&data[0], data.size());
}
[[maybe_unused]] static std::vector<char> Decode(const std::string &input) {
static constexpr unsigned char kDecodingTable[] = {
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, 52, 53, 54, 55, 56, 57,
58, 59, 60, 61, 64, 64, 64, 64, 64, 64, 64, 0, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 64, 64, 64, 64, 64, 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64};
std::vector<char> out;
if (not input.empty()) {
size_t in_len = input.size();
if (in_len % 4 != 0)
throw std::runtime_error("Input data size is not a multiple of 4");
size_t out_len = in_len / 4 * 3;
if (input[in_len - 1] == '=')
out_len--;
if (input[in_len - 2] == '=')
out_len--;
out.resize(out_len);
for (size_t i = 0, j = 0; i < in_len;) {
uint32_t a = input[i] == '='
? 0 & i++
: kDecodingTable[static_cast<int>(input[i++])];
uint32_t b = input[i] == '='
? 0 & i++
: kDecodingTable[static_cast<int>(input[i++])];
uint32_t c = input[i] == '='
? 0 & i++
: kDecodingTable[static_cast<int>(input[i++])];
uint32_t d = input[i] == '='
? 0 & i++
: kDecodingTable[static_cast<int>(input[i++])];
uint32_t triple =
(a << 3 * 6) + (b << 2 * 6) + (c << 1 * 6) + (d << 0 * 6);
if (j < out_len)
out[j++] = (triple >> 2 * 8) & 0xFF;
if (j < out_len)
out[j++] = (triple >> 1 * 8) & 0xFF;
if (j < out_len)
out[j++] = (triple >> 0 * 8) & 0xFF;
}
}
return out;
}
} // namespace macaron::Base64
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#endif /* _MACARON_BASE64_H_ */
// NOLINTEND

View File

@ -0,0 +1,49 @@
/*
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 INCLUDE_UTILS_ACTION_QUEUE_HPP_
#define INCLUDE_UTILS_ACTION_QUEUE_HPP_
#include "utils/single_thread_service_base.hpp"
namespace repertory::utils::action_queue {
class action_queue final : single_thread_service_base {
explicit action_queue(const std::string &id,
std::uint8_t max_concurrent_actions = 5u);
private:
std::string id_;
std::uint8_t max_concurrent_actions_;
private:
std::deque<std::function<void()>> queue_;
mutable std::mutex queue_mtx_;
std::condition_variable queue_notify_;
protected:
void service_function() override;
public:
void push(std::function<void()> action);
};
} // namespace repertory::utils::action_queue
#endif // INCLUDE_UTILS_ACTION_QUEUE_HPP_

View File

@ -0,0 +1,117 @@
/*
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 INCLUDE_UTILS_CLI_UTILS_HPP_
#define INCLUDE_UTILS_CLI_UTILS_HPP_
#include "types/repertory.hpp"
namespace repertory::utils::cli {
using option = std::array<std::string, 2>;
namespace options {
static const option check_version_option = {"-cv", "--check_version"};
static const option display_config_option = {"-dc", "--display_config"};
static const option data_directory_option = {"-dd", "--data_directory"};
static const option encrypt_option = {"-en", "--encrypt"};
static const option drive_information_option = {"-di", "--drive_information"};
#if defined(REPERTORY_ENABLE_S3)
static const option name_option = {"-na", "--name"};
static const option s3_option = {"-s3", "--s3"};
#endif // defined(REPERTORY_ENABLE_S3)
static const option generate_config_option = {"-gc", "--generate_config"};
static const option get_option = {"-get", "--get"};
static const option get_directory_items_option = {"-gdi",
"--get_directory_items"};
static const option get_pinned_files_option = {"-gpf", "--get_pinned_files"};
static const option help_option = {"-h", "--help"};
static const option hidden_option = {"-hidden", "--hidden"};
static const option open_files_option = {"-of", "--open_files"};
static const option pin_file_option = {"-pf", "--pin_file"};
static const option pinned_status_option = {"-ps", "--pinned_status"};
static const option password_option = {"-pw", "--password"};
static const option remote_mount_option = {"-rm", "--remote_mount"};
static const option set_option = {"-set", "--set"};
static const option status_option = {"-status", "--status"};
static const option unmount_option = {"-unmount", "--unmount"};
static const option unpin_file_option = {"-uf", "--unpin_file"};
static const option user_option = {"-us", "--user"};
static const option version_option = {"-V", "--version"};
static const std::vector<option> option_list = {
check_version_option,
display_config_option,
data_directory_option,
drive_information_option,
encrypt_option,
#if defined(REPERTORY_ENABLE_S3)
s3_option,
name_option,
#endif // defined(REPERTORY_ENABLE_S3)
generate_config_option,
get_option,
get_directory_items_option,
get_pinned_files_option,
help_option,
hidden_option,
open_files_option,
password_option,
pin_file_option,
pinned_status_option,
remote_mount_option,
set_option,
status_option,
unmount_option,
unpin_file_option,
user_option,
version_option,
};
} // namespace options
// Prototypes
void get_api_authentication_data(std::string &user, std::string &password,
std::uint16_t &port, const provider_type &prov,
const std::string &data_directory);
[[nodiscard]] auto get_provider_type_from_args(std::vector<const char *> args)
-> provider_type;
[[nodiscard]] auto has_option(std::vector<const char *> args,
const std::string &option_name) -> bool;
[[nodiscard]] auto has_option(std::vector<const char *> args, const option &opt)
-> bool;
[[nodiscard]] auto parse_option(std::vector<const char *> args,
const std::string &option_name,
std::uint8_t count) -> std::vector<std::string>;
[[nodiscard]] auto parse_string_option(std::vector<const char *> args,
const option &opt, std::string &value)
-> exit_code;
[[nodiscard]] auto parse_drive_options(std::vector<const char *> args,
provider_type &prov,
std::string &data_directory)
-> std::vector<std::string>;
} // namespace repertory::utils::cli
#endif // INCLUDE_UTILS_CLI_UTILS_HPP_

View File

@ -0,0 +1,45 @@
/*
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 INCLUDE_UTILS_COM_INIT_WRAPPER_HPP_
#define INCLUDE_UTILS_COM_INIT_WRAPPER_HPP_
#ifdef _WIN32
namespace repertory {
class com_init_wrapper {
public:
com_init_wrapper()
: uninit_(
SUCCEEDED(::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED))) {}
~com_init_wrapper() {
if (uninit_) {
::CoUninitialize();
}
}
private:
const BOOL uninit_;
};
} // namespace repertory
#endif // _WIN32
#endif // INCLUDE_UTILS_COM_INIT_WRAPPER_HPP_

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 INCLUDE_UTILS_ENCRYPTING_READER_HPP_
#define INCLUDE_UTILS_ENCRYPTING_READER_HPP_
#include "types/repertory.hpp"
#include "utils/file_utils.hpp"
namespace repertory::utils::encryption {
using key_type = std::array<unsigned char, 32U>;
class encrypting_reader final {
public:
encrypting_reader(const std::string &file_name,
const std::string &source_path, stop_type &stop_requested,
const std::string &token,
std::optional<std::string> relative_parent_path,
std::size_t error_return = 0U);
encrypting_reader(const std::string &encrypted_file_path,
const std::string &source_path, stop_type &stop_requested,
const std::string &token, std::size_t error_return = 0U);
encrypting_reader(
const std::string &encrypted_file_path, const std::string &source_path,
stop_type &stop_requested, const std::string &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();
public:
using iostream = std::basic_iostream<char, std::char_traits<char>>;
using streambuf = std::basic_streambuf<char, std::char_traits<char>>;
private:
const key_type key_;
stop_type &stop_requested_;
const size_t error_return_;
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_{};
native_file_ptr source_file_;
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(const std::string &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]] static constexpr auto get_header_size() -> std::size_t {
return header_size_;
}
[[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 // INCLUDE_UTILS_ENCRYPTING_READER_HPP_

View File

@ -0,0 +1,160 @@
/*
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 INCLUDE_UTILS_ENCRYPTION_HPP_
#define INCLUDE_UTILS_ENCRYPTION_HPP_
#include "types/repertory.hpp"
#include "utils/encrypting_reader.hpp"
namespace repertory::utils::encryption {
using reader_func = std::function<api_error(data_buffer &cypher_text,
std::uint64_t start_offset,
std::uint64_t end_offset)>;
// Prototypes
[[nodiscard]] auto decrypt_file_path(const std::string &encryption_token,
std::string &file_path) -> api_error;
[[nodiscard]] auto decrypt_file_name(const std::string &encryption_token,
std::string &file_name) -> api_error;
[[nodiscard]] auto generate_key(const std::string &encryption_token)
-> key_type;
[[nodiscard]] auto read_encrypted_range(const http_range &range,
const key_type &key, reader_func reader,
std::uint64_t total_size,
data_buffer &data) -> api_error;
// Implementations
template <typename result>
[[nodiscard]] inline auto decrypt_data(const key_type &key, const char *buffer,
std::size_t buffer_size, result &res)
-> bool {
const auto header_size =
static_cast<std::uint32_t>(encrypting_reader::get_header_size());
if (buffer_size > header_size) {
const std::uint32_t size =
boost::endian::native_to_big(static_cast<std::uint32_t>(buffer_size));
res.resize(buffer_size - header_size);
return crypto_aead_xchacha20poly1305_ietf_decrypt_detached(
reinterpret_cast<unsigned char *>(&res[0u]), nullptr,
reinterpret_cast<const unsigned char *>(&buffer[header_size]),
res.size(),
reinterpret_cast<const unsigned char *>(
&buffer[crypto_aead_xchacha20poly1305_IETF_NPUBBYTES]),
reinterpret_cast<const unsigned char *>(&size), sizeof(size),
reinterpret_cast<const unsigned char *>(buffer),
key.data()) == 0;
}
return false;
}
template <typename buffer, typename result>
[[nodiscard]] inline auto decrypt_data(const key_type &key, const buffer &buf,
result &res) -> bool {
return decrypt_data<result>(key, &buf[0u], buf.size(), res);
}
template <typename buffer, typename result>
[[nodiscard]] inline auto decrypt_data(const std::string &encryption_token,
const buffer &buf, result &res) -> bool {
return decrypt_data<buffer, result>(generate_key(encryption_token), buf, res);
}
template <typename result>
[[nodiscard]] inline auto decrypt_data(const std::string &encryption_token,
const char *buffer,
std::size_t buffer_size, result &res)
-> bool {
return decrypt_data<result>(generate_key(encryption_token), buffer,
buffer_size, res);
}
template <typename result>
inline void
encrypt_data(const std::array<unsigned char,
crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> &iv,
const key_type &key, const char *buffer, std::size_t buffer_size,
result &res) {
std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_ABYTES> mac{};
const auto header_size =
static_cast<std::uint32_t>(encrypting_reader::get_header_size());
const std::uint32_t size = boost::endian::native_to_big(
static_cast<std::uint32_t>(buffer_size + header_size));
res.resize(buffer_size + header_size);
unsigned long long mac_length{};
if (crypto_aead_xchacha20poly1305_ietf_encrypt_detached(
reinterpret_cast<unsigned char *>(&res[header_size]), mac.data(),
&mac_length, reinterpret_cast<const unsigned char *>(buffer),
buffer_size, reinterpret_cast<const unsigned char *>(&size),
sizeof(size), nullptr, iv.data(), key.data()) != 0) {
throw std::runtime_error("encryption failed");
}
std::memcpy(&res[0u], &iv[0u], iv.size());
std::memcpy(&res[iv.size()], &mac[0u], mac.size());
}
template <typename result>
inline void encrypt_data(const key_type &key, const char *buffer,
std::size_t buffer_size, result &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);
}
template <typename result>
inline void encrypt_data(const std::string &encryption_token,
const char *buffer, std::size_t buffer_size,
result &res) {
encrypt_data<result>(generate_key(encryption_token), buffer, buffer_size,
res);
}
template <typename buffer, typename result>
inline void encrypt_data(const std::string &encryption_token, const buffer &buf,
result &res) {
encrypt_data<result>(generate_key(encryption_token), &buf[0u], buf.size(),
res);
}
template <typename buffer, typename result>
inline void encrypt_data(const key_type &key, const buffer &buf, result &res) {
encrypt_data<result>(key, &buf[0u], buf.size(), res);
}
template <typename buffer, typename result>
inline void
encrypt_data(const std::array<unsigned char,
crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> &iv,
const key_type &key, const buffer &buf, result &res) {
encrypt_data<result>(iv, key, &buf[0u], buf.size(), res);
}
} // namespace repertory::utils::encryption
#endif // INCLUDE_UTILS_ENCRYPTION_HPP_

View File

@ -0,0 +1,84 @@
/*
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 INCLUDE_UTILS_ERROR_UTILS_HPP_
#define INCLUDE_UTILS_ERROR_UTILS_HPP_
#include "types/repertory.hpp"
namespace repertory::utils::error {
void raise_error(std::string function, std::string_view msg);
void raise_error(std::string function, const api_error &err,
std::string_view msg);
void raise_error(std::string function, const std::exception &exception,
std::string_view msg);
void raise_error(std::string function, std::int64_t err, std::string_view msg);
void raise_error(std::string function, const json &err, std::string_view msg);
void raise_error(std::string function, const api_error &err,
std::string_view file_path, std::string_view msg);
void raise_error(std::string function, std::int64_t err,
std::string_view file_path, std::string_view msg);
void raise_error(std::string function, const std::exception &exception,
std::string_view file_path, std::string_view msg);
void raise_api_path_error(std::string function, std::string_view api_path,
const api_error &err, std::string_view msg);
void raise_api_path_error(std::string function, std::string_view api_path,
const std::exception &exception,
std::string_view msg);
void raise_api_path_error(std::string function, std::string_view api_path,
std::int64_t err, std::string_view msg);
void raise_api_path_error(std::string function, std::string_view api_path,
const json &err, std::string_view msg);
void raise_api_path_error(std::string function, std::string_view api_path,
std::string_view source_path, const api_error &err,
std::string_view msg);
void raise_api_path_error(std::string function, std::string_view api_path,
std::string_view source_path, std::int64_t err,
std::string_view msg);
void raise_api_path_error(std::string function, std::string_view api_path,
std::string_view source_path,
const std::exception &exception,
std::string_view msg);
void raise_url_error(std::string function, std::string_view url, CURLcode err,
std::string_view msg);
void raise_url_error(std::string function, std::string_view url,
std::string_view source_path,
const std::exception &exception, std::string_view msg);
} // namespace repertory::utils::error
#endif // INCLUDE_UTILS_ERROR_UTILS_HPP_

View File

@ -0,0 +1,96 @@
/*
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 INCLUDE_UTILS_FILE_UTILS_HPP_
#define INCLUDE_UTILS_FILE_UTILS_HPP_
#include "types/repertory.hpp"
#include "utils/native_file.hpp"
namespace repertory::utils::file {
// Prototypes
[[nodiscard]] auto calculate_used_space(std::string path, bool recursive)
-> std::uint64_t;
void change_to_process_directory();
[[nodiscard]] auto copy_directory_recursively(std::string from_path,
std::string to_path) -> bool;
[[nodiscard]] auto copy_file(std::string from_path, std::string to_path)
-> bool;
[[nodiscard]] auto create_full_directory_path(std::string path) -> bool;
[[nodiscard]] auto delete_directory(std::string path, bool recursive = false)
-> bool;
[[nodiscard]] auto delete_directory_recursively(std::string path) -> bool;
[[nodiscard]] auto delete_file(std::string path) -> bool;
[[nodiscard]] auto generate_sha256(const std::string &file_path) -> std::string;
[[nodiscard]] auto get_accessed_time(const std::string &path,
std::uint64_t &accessed) -> bool;
[[nodiscard]] auto get_directory_files(std::string path, bool oldest_first,
bool recursive = false)
-> std::deque<std::string>;
[[nodiscard]] auto get_free_drive_space(const std::string &path)
-> std::uint64_t;
[[nodiscard]] auto get_total_drive_space(const std::string &path)
-> std::uint64_t;
[[nodiscard]] auto get_file_size(std::string path, std::uint64_t &file_size)
-> bool;
[[nodiscard]] auto get_modified_time(const std::string &path,
std::uint64_t &modified) -> bool;
[[nodiscard]] auto is_directory(const std::string &path) -> bool;
[[nodiscard]] auto is_file(const std::string &path) -> bool;
[[nodiscard]] auto is_modified_date_older_than(const std::string &path,
const std::chrono::hours &hours)
-> bool;
[[nodiscard]] auto move_file(std::string from, std::string to) -> bool;
[[nodiscard]] auto read_file_lines(const std::string &path)
-> std::vector<std::string>;
[[nodiscard]] auto read_json_file(const std::string &path, json &data) -> bool;
[[nodiscard]] auto reset_modified_time(const std::string &path) -> bool;
[[nodiscard]] auto retry_delete_directory(const std::string &dir) -> bool;
[[nodiscard]] auto retry_delete_file(const std::string &file) -> bool;
[[nodiscard]] auto write_json_file(const std::string &path, const json &j)
-> bool;
} // namespace repertory::utils::file
#endif // INCLUDE_UTILS_FILE_UTILS_HPP_

View File

@ -0,0 +1,115 @@
/*
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 INCLUDE_UTILS_NATIVEFILE_HPP_
#define INCLUDE_UTILS_NATIVEFILE_HPP_
#include "types/repertory.hpp"
namespace repertory {
class native_file final {
public:
native_file(const native_file &) = delete;
native_file(native_file &&) = delete;
auto operator=(const native_file &) -> native_file & = delete;
auto operator=(native_file &&) -> native_file & = delete;
using native_file_ptr = std::shared_ptr<native_file>;
public:
[[nodiscard]] static auto attach(native_handle handle) -> native_file_ptr {
return std::shared_ptr<native_file>(new native_file(handle));
}
[[nodiscard]] static auto clone(const native_file_ptr &ptr)
-> native_file_ptr;
[[nodiscard]] static auto create_or_open(const std::string &source_path,
bool read_only, native_file_ptr &ptr)
-> api_error;
[[nodiscard]] static auto create_or_open(const std::string &source_path,
native_file_ptr &ptr) -> api_error;
[[nodiscard]] static auto open(const std::string &source_path,
native_file_ptr &ptr) -> api_error;
[[nodiscard]] static auto open(const std::string &source_path, bool read_only,
native_file_ptr &ptr) -> api_error;
private:
explicit native_file(const native_handle &handle) : handle_(handle) {}
public:
~native_file();
private:
native_handle handle_;
private:
bool auto_close{false};
#ifdef _WIN32
std::recursive_mutex read_write_mutex_;
#endif
public:
[[nodiscard]] auto allocate(std::uint64_t file_size) -> bool;
void close();
[[nodiscard]] auto copy_from(const native_file_ptr &ptr) -> bool;
[[nodiscard]] auto copy_from(const std::string &path) -> bool;
void flush();
[[nodiscard]] auto get_file_size(std::uint64_t &file_size) -> bool;
[[nodiscard]] auto get_handle() -> native_handle;
#ifdef _WIN32
[[nodiscard]] auto read_bytes(char *buffer, std::size_t read_size,
std::uint64_t read_offset,
std::size_t &bytes_read) -> bool;
#else
[[nodiscard]] auto read_bytes(char *buffer, std::size_t read_size,
std::uint64_t read_offset,
std::size_t &bytes_read) -> bool;
#endif
void set_auto_close(bool b) { auto_close = b; }
[[nodiscard]] auto truncate(std::uint64_t file_size) -> bool;
#ifdef _WIN32
[[nodiscard]] auto write_bytes(const char *buffer, std::size_t write_size,
std::uint64_t write_offset,
std::size_t &bytes_written) -> bool;
#else
[[nodiscard]] auto write_bytes(const char *buffer, std::size_t write_size,
std::uint64_t write_offset,
std::size_t &bytes_written) -> bool;
#endif
};
using native_file_ptr = native_file::native_file_ptr;
} // namespace repertory
#endif // INCLUDE_UTILS_NATIVEFILE_HPP_

View File

@ -0,0 +1,67 @@
/*
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 INCLUDE_UTILS_PATH_UTILS_HPP_
#define INCLUDE_UTILS_PATH_UTILS_HPP_
namespace repertory::utils::path {
#ifdef _WIN32
static const std::string directory_seperator = "\\";
static const std::string not_directory_seperator = "/";
#else
static const std::string directory_seperator = "/";
static const std::string not_directory_seperator = "\\";
#endif
// Prototypes
[[nodiscard]] auto absolute(std::string path) -> std::string;
[[nodiscard]] auto combine(std::string path,
const std::vector<std::string> &paths)
-> std::string;
[[nodiscard]] auto create_api_path(std::string path) -> std::string;
[[nodiscard]] auto finalize(std::string path) -> std::string;
auto format_path(std::string &path, const std::string &sep,
const std::string &not_sep) -> std::string &;
[[nodiscard]] auto get_parent_api_path(const std::string &path) -> std::string;
#ifndef _WIN32
[[nodiscard]] auto get_parent_directory(std::string path) -> std::string;
#endif
[[nodiscard]] auto is_ads_file_path(const std::string &path) -> bool;
[[nodiscard]] auto is_trash_directory(std::string path) -> bool;
[[nodiscard]] auto remove_file_name(std::string path) -> std::string;
#ifndef _WIN32
[[nodiscard]] auto resolve(std::string path) -> std::string;
#endif
[[nodiscard]] auto strip_to_file_name(std::string path) -> std::string;
} // namespace repertory::utils::path
#endif // INCLUDE_UTILS_PATH_UTILS_HPP_

View File

@ -0,0 +1,86 @@
/*
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 INCLUDE_UTILS_POLLING_HPP_
#define INCLUDE_UTILS_POLLING_HPP_
#include "types/repertory.hpp"
namespace repertory {
class app_config;
class polling final {
public:
enum struct frequency {
high,
low,
second,
};
struct polling_item {
std::string name;
frequency freq;
std::function<void()> action;
};
public:
polling(const polling &) = delete;
polling(polling &&) = delete;
auto operator=(const polling &) -> polling & = delete;
auto operator=(polling &&) -> polling & = delete;
private:
polling() = default;
~polling() { stop(); }
private:
static polling instance_;
public:
static auto instance() -> polling & { return instance_; }
private:
app_config *config_ = nullptr;
std::unique_ptr<std::thread> high_frequency_thread_;
std::unordered_map<std::string, polling_item> items_;
std::unique_ptr<std::thread> low_frequency_thread_;
std::mutex mutex_;
std::condition_variable notify_;
std::unique_ptr<std::thread> second_frequency_thread_;
std::mutex start_stop_mutex_;
stop_type stop_requested_ = false;
private:
void frequency_thread(std::function<std::uint32_t()> get_frequency_seconds,
frequency freq);
public:
void remove_callback(const std::string &name);
void set_callback(const polling_item &item);
void start(app_config *config);
void stop();
};
} // namespace repertory
#endif // INCLUDE_UTILS_POLLING_HPP_

View File

@ -0,0 +1,68 @@
/*
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 INCLUDE_UTILS_SINGLE_THREAD_SERVICE_BASE_HPP_
#define INCLUDE_UTILS_SINGLE_THREAD_SERVICE_BASE_HPP_
#include "types/repertory.hpp"
namespace repertory {
class single_thread_service_base {
public:
explicit single_thread_service_base(std::string service_name)
: service_name_(std::move(service_name)) {}
virtual ~single_thread_service_base() { stop(); }
private:
const std::string service_name_;
mutable std::mutex mtx_;
mutable std::condition_variable notify_;
stop_type stop_requested_ = false;
std::unique_ptr<std::thread> thread_;
protected:
[[nodiscard]] auto get_mutex() const -> std::mutex & { return mtx_; }
[[nodiscard]] auto get_notify() const -> std::condition_variable & {
return notify_;
}
[[nodiscard]] auto get_stop_requested() const -> bool {
return stop_requested_;
}
void notify_all() const;
virtual void on_start() {}
virtual void on_stop() {}
virtual void service_function() = 0;
public:
void start();
void stop();
};
} // namespace repertory
#endif // INCLUDE_UTILS_SINGLE_THREAD_SERVICE_BASE_HPP_

View File

@ -0,0 +1,115 @@
/*
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 INCLUDE_UTILS_STRING_UTILS_HPP_
#define INCLUDE_UTILS_STRING_UTILS_HPP_
namespace repertory::utils::string {
// Prototypes
constexpr auto begins_with(std::string_view str, std::string_view val) -> bool {
return (str.find(val) == 0U);
}
constexpr auto contains(std::string_view str, std::string_view search) -> bool {
return (str.find(search) != std::string_view::npos);
}
[[nodiscard]] /* constexpr c++20 */ auto ends_with(std::string_view str,
std::string_view val)
-> bool;
[[nodiscard]] auto from_bool(bool val) -> std::string;
[[nodiscard]] auto from_dynamic_bitset(const boost::dynamic_bitset<> &bitset)
-> std::string;
[[nodiscard]] auto from_utf8(const std::string &str) -> std::wstring;
[[nodiscard]] /* constexpr c++20 */ auto is_numeric(std::string_view str)
-> bool;
[[nodiscard]] auto join(const std::vector<std::string> &arr, const char &delim)
-> std::string;
auto left_trim(std::string &str) -> std::string &;
auto left_trim(std::string &str, const char &trim_ch) -> std::string &;
auto replace(std::string &src, const char &character, const char &with)
-> std::string &;
auto replace(std::string &src, const std::string &find, const std::string &with,
size_t start_pos = 0) -> std::string &;
[[nodiscard]] auto replace_copy(std::string src, const char &character,
const char &with) -> std::string;
[[nodiscard]] auto replace_copy(std::string src, const std::string &find,
const std::string &with, size_t start_pos = 0)
-> std::string;
auto right_trim(std::string &str) -> std::string &;
auto right_trim(std::string &str, const char &trim_ch) -> std::string &;
[[nodiscard]] auto split(const std::string &str, const char &delim,
bool should_trim = true) -> std::vector<std::string>;
[[nodiscard]] auto to_bool(std::string val) -> bool;
[[nodiscard]] auto to_double(const std::string &str) -> double;
[[nodiscard]] auto to_dynamic_bitset(const std::string &val)
-> boost::dynamic_bitset<>;
[[nodiscard]] auto to_lower(std::string str) -> std::string;
[[nodiscard]] auto to_int32(const std::string &val) -> std::int32_t;
[[nodiscard]] auto to_int64(const std::string &val) -> std::int64_t;
[[nodiscard]] auto to_size_t(const std::string &val) -> std::size_t;
[[nodiscard]] auto to_uint8(const std::string &val) -> std::uint8_t;
[[nodiscard]] auto to_uint16(const std::string &val) -> std::uint16_t;
[[nodiscard]] auto to_uint32(const std::string &val) -> std::uint32_t;
[[nodiscard]] auto to_uint64(const std::string &val) -> std::uint64_t;
[[nodiscard]] auto to_upper(std::string str) -> std::string;
[[nodiscard]] auto to_utf8(std::string str) -> std::string;
[[nodiscard]] auto to_utf8(const std::wstring &str) -> std::string;
auto trim(std::string &str) -> std::string &;
auto trim(std::string &str, const char &trim_ch) -> std::string &;
[[nodiscard]] auto trim_copy(std::string str) -> std::string;
[[nodiscard]] auto trim_copy(std::string str, const char &trim_ch)
-> std::string;
} // namespace repertory::utils::string
#endif // INCLUDE_UTILS_STRING_UTILS_HPP_

View File

@ -0,0 +1,59 @@
/*
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 INCLUDE_UTILS_THROTTLE_HPP_
#define INCLUDE_UTILS_THROTTLE_HPP_
namespace repertory {
class throttle final {
public:
throttle() : max_size_(10u) {}
explicit throttle(std::size_t max_size) : max_size_(max_size) {}
public:
throttle(const throttle &) noexcept = delete;
throttle(throttle &&) noexcept = delete;
auto operator=(const throttle &) -> throttle & = delete;
auto operator=(throttle &&) -> throttle & = delete;
public:
~throttle() { shutdown(); }
private:
const std::size_t max_size_;
std::size_t count_ = 0u;
bool shutdown_ = false;
std::mutex mutex_;
std::condition_variable notify_;
public:
void decrement();
void increment_or_wait();
void reset();
void shutdown();
};
} // namespace repertory
#endif // INCLUDE_UTILS_THROTTLE_HPP_

View File

@ -0,0 +1,50 @@
/*
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 INCLUDE_UTILS_TIMEOUT_HPP_
#define INCLUDE_UTILS_TIMEOUT_HPP_
namespace repertory {
class timeout final {
public:
timeout(const timeout &) noexcept = delete;
timeout(timeout &&) noexcept = delete;
auto operator=(const timeout &) noexcept -> timeout & = delete;
auto operator=(timeout &&) noexcept -> timeout & = delete;
public:
timeout(std::function<void()> timeout_callback,
const std::chrono::system_clock::duration &duration = 10s);
~timeout() { disable(); }
private:
bool timeout_killed_;
std::unique_ptr<std::thread> timeout_thread_;
std::mutex timeout_mutex_;
std::condition_variable timeout_notify_;
public:
void disable();
};
} // namespace repertory
#endif // INCLUDE_UTILS_TIMEOUT_HPP_

View File

@ -0,0 +1,83 @@
/*
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 INCLUDE_UTILS_UNIX_UNIX_UTILS_HPP_
#define INCLUDE_UTILS_UNIX_UNIX_UTILS_HPP_
#ifndef _WIN32
#include "types/remote.hpp"
#include "types/repertory.hpp"
namespace repertory::utils {
#if __linux__
inline const std::array<std::string, 4U> attribute_namespaces = {
"security",
"system",
"trusted",
"user",
};
#endif
#if __APPLE__
template <typename t>
[[nodiscard]] auto convert_to_uint64(const t *ptr) -> std::uint64_t;
#else
[[nodiscard]] auto convert_to_uint64(const pthread_t &thread) -> std::uint64_t;
#endif
[[nodiscard]] auto from_api_error(const api_error &err) -> int;
[[nodiscard]] auto get_last_error_code() -> int;
[[nodiscard]] auto get_thread_id() -> std::uint64_t;
[[nodiscard]] auto is_uid_member_of_group(const uid_t &uid, const gid_t &gid)
-> bool;
void set_last_error_code(int error_code);
[[nodiscard]] auto to_api_error(int err) -> api_error;
[[nodiscard]] auto unix_error_to_windows(int err) -> std::int32_t;
[[nodiscard]] auto unix_time_to_windows_time(const remote::file_time &file_time)
-> UINT64;
void use_getpwuid(uid_t uid, std::function<void(struct passwd *pass)> callback);
void windows_create_to_unix(const UINT32 &create_options,
const UINT32 &granted_access, std::uint32_t &flags,
remote::file_mode &mode);
[[nodiscard]] auto windows_time_to_unix_time(std::uint64_t win_time)
-> remote::file_time;
// template implementations
#if __APPLE__
template <typename t>
[[nodiscard]] auto convert_to_uint64(const t *v) -> std::uint64_t {
return static_cast<std::uint64_t>(reinterpret_cast<std::uintptr_t>(v));
}
#endif
} // namespace repertory::utils
#endif // !_WIN32
#endif // INCLUDE_UTILS_UNIX_UNIX_UTILS_HPP_

View File

@ -0,0 +1,181 @@
/*
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 INCLUDE_UTILS_UTILS_HPP_
#define INCLUDE_UTILS_UTILS_HPP_
#include "types/remote.hpp"
#include "types/repertory.hpp"
#include "utils/unix/unix_utils.hpp"
#include "utils/windows/windows_utils.hpp"
namespace repertory::utils {
void calculate_allocation_size(bool directory, std::uint64_t file_size,
UINT64 allocation_size,
std::string &allocation_meta_size);
[[nodiscard]] auto calculate_read_size(const uint64_t &total_size,
std::size_t read_size,
const uint64_t &offset) -> std::size_t;
template <typename t>
[[nodiscard]] auto collection_excludes(t collection,
const typename t::value_type &val)
-> bool;
template <typename t>
[[nodiscard]] auto collection_includes(t collection,
const typename t::value_type &val)
-> bool;
[[nodiscard]] auto compare_version_strings(std::string version1,
std::string version2) -> int;
[[nodiscard]] auto convert_api_date(const std::string &date) -> std::uint64_t;
[[nodiscard]] auto create_curl() -> CURL *;
[[nodiscard]] auto create_uuid_string() -> std::string;
[[nodiscard]] auto create_volume_label(const provider_type &prov)
-> std::string;
template <typename t>
[[nodiscard]] auto divide_with_ceiling(const t &n, const t &d) -> t;
[[nodiscard]] auto download_type_from_string(std::string type,
const download_type &default_type)
-> download_type;
[[nodiscard]] auto download_type_to_string(const download_type &type)
-> std::string;
template <typename t>
[[nodiscard]] auto from_hex_string(const std::string &str, t &val) -> bool;
[[nodiscard]] auto generate_random_string(std::uint16_t length) -> std::string;
[[nodiscard]] auto get_attributes_from_meta(const api_meta_map &meta) -> DWORD;
[[nodiscard]] auto get_environment_variable(const std::string &variable)
-> std::string;
[[nodiscard]] auto get_file_time_now() -> std::uint64_t;
void get_local_time_now(struct tm &local_time);
[[nodiscard]] auto get_next_available_port(std::uint16_t first_port,
std::uint16_t &available_port)
-> bool;
[[nodiscard]] auto get_time_now() -> std::uint64_t;
template <typename data_type>
[[nodiscard]] auto random_between(const data_type &begin, const data_type &end)
-> data_type;
template <typename t>
void remove_element_from(t &collection, const typename t::value_type &val);
[[nodiscard]] auto reset_curl(CURL *curl_handle) -> CURL *;
[[nodiscard]] auto retryable_action(const std::function<bool()> &action)
-> bool;
void spin_wait_for_mutex(std::function<bool()> complete,
std::condition_variable &cond, std::mutex &mtx,
const std::string &text = "");
void spin_wait_for_mutex(bool &complete, std::condition_variable &cond,
std::mutex &mtx, const std::string &text = "");
template <typename collection_t>
[[nodiscard]] auto to_hex_string(const collection_t &collection) -> std::string;
// template implementations
template <typename t>
[[nodiscard]] auto collection_excludes(t collection,
const typename t::value_type &val)
-> bool {
return std::find(collection.begin(), collection.end(), val) ==
collection.end();
}
template <typename t>
[[nodiscard]] auto collection_includes(t collection,
const typename t::value_type &val)
-> bool {
return std::find(collection.begin(), collection.end(), val) !=
collection.end();
}
template <typename t>
[[nodiscard]] auto divide_with_ceiling(const t &n, const t &d) -> t {
return n ? (n / d) + (n % d != 0) : 0;
}
template <typename t>
[[nodiscard]] auto from_hex_string(const std::string &str, 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 t::value_type>(
strtol(str.substr(i, 2U).c_str(), nullptr, base16)));
}
return true;
}
return false;
}
template <typename data_type>
[[nodiscard]] auto random_between(const data_type &begin, const data_type &end)
-> data_type {
return begin + repertory_rand<data_type>() % ((end + data_type{1}) - begin);
}
template <typename collection_t>
void remove_element_from(collection_t &collection,
const typename collection_t::value_type &value) {
collection.erase(std::remove(collection.begin(), collection.end(), value),
collection.end());
}
template <typename collection_t>
[[nodiscard]] auto to_hex_string(const collection_t &collection)
-> std::string {
static_assert(sizeof(typename collection_t::value_type) == 1U,
"value_type must be 1 byte in size");
static constexpr const auto mask = 0xFF;
std::stringstream stream;
for (const auto &val : collection) {
stream << std::setfill('0') << std::setw(2) << std::hex
<< (static_cast<std::uint32_t>(val) & mask);
}
return stream.str();
}
} // namespace repertory::utils
#endif // INCLUDE_UTILS_UTILS_HPP_

View File

@ -0,0 +1,75 @@
/*
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 INCLUDE_UTILS_WINDOWS_WINDOWS_UTILS_HPP_
#define INCLUDE_UTILS_WINDOWS_WINDOWS_UTILS_HPP_
#ifdef _WIN32
#include "types/remote.hpp"
#include "types/repertory.hpp"
namespace repertory::utils {
[[nodiscard]] auto filetime_to_unix_time(const FILETIME &ft)
-> remote::file_time;
[[nodiscard]] auto get_last_error_code() -> DWORD;
[[nodiscard]] auto get_local_app_data_directory() -> const std::string &;
[[nodiscard]] auto get_accessed_time_from_meta(const api_meta_map &meta)
-> std::uint64_t;
[[nodiscard]] auto get_changed_time_from_meta(const api_meta_map &meta)
-> std::uint64_t;
[[nodiscard]] auto get_creation_time_from_meta(const api_meta_map &meta)
-> std::uint64_t;
[[nodiscard]] auto get_written_time_from_meta(const api_meta_map &meta)
-> std::uint64_t;
[[nodiscard]] auto get_thread_id() -> std::uint64_t;
[[nodiscard]] auto is_process_elevated() -> bool;
[[nodiscard]] auto run_process_elevated(std::vector<const char *> args) -> int;
void set_last_error_code(DWORD errorCode);
[[nodiscard]] auto from_api_error(const api_error &e) -> NTSTATUS;
auto strptime(const char *s, const char *f, struct tm *tm) -> const char *;
[[nodiscard]] auto unix_access_mask_to_windows(std::int32_t mask) -> int;
[[nodiscard]] auto
unix_open_flags_to_flags_and_perms(const remote::file_mode &mode,
const remote::open_flags &flags,
std::int32_t &perms) -> int;
void unix_time_to_filetime(const remote::file_time &ts, FILETIME &ft);
[[nodiscard]] auto time64_to_unix_time(const __time64_t &t)
-> remote::file_time;
} // namespace repertory::utils
#endif // _WIN32
#endif // INCLUDE_UTILS_WINDOWS_WINDOWS_UTILS_HPP_