2.0.0-rc (#9)
Some checks failed
BlockStorage/repertory_osx/pipeline/head This commit looks good
BlockStorage/repertory_windows/pipeline/head This commit looks good
BlockStorage/repertory/pipeline/head There was a failure building this commit
BlockStorage/repertory_linux_builds/pipeline/head This commit looks good
BlockStorage/repertory_osx_builds/pipeline/head There was a failure building this commit

### Issues

* \#1 \[bug\] Unable to mount S3 due to 'item_not_found' exception
* \#2 Require bucket name for S3 mounts
* \#3 \[bug\] File size is not being updated in S3 mount
* \#4 Upgrade to libfuse-3.x.x
* \#5 Switch to renterd for Sia support
* \#6 Switch to cpp-httplib to further reduce dependencies
* \#7 Remove global_data and calculate used disk space per provider
* \#8 Switch to libcurl for S3 mount support

### Changes from v1.x.x

* Added read-only encrypt provider
  * Pass-through mount point that transparently encrypts source data using `XChaCha20-Poly1305`
* Added S3 encryption support via `XChaCha20-Poly1305`
* Added replay protection to remote mounts
* Added support base64 writes in remote FUSE
* Created static linked Linux binaries for `amd64` and `aarch64` using `musl-libc`
* Removed legacy Sia renter support
* Removed Skynet support
* Fixed multiple remote mount WinFSP API issues on \*NIX servers
* Implemented chunked read and write
  * Writes for non-cached files are performed in chunks of 8Mib
* Removed `repertory-ui` support
* Removed `FreeBSD` support
* Switched to `libsodium` over `CryptoPP`
* Switched to `XChaCha20-Poly1305` for remote mounts
* Updated `GoogleTest` to v1.14.0
* Updated `JSON for Modern C++` to v3.11.2
* Updated `OpenSSL` to v1.1.1w
* Updated `RocksDB` to v8.5.3
* Updated `WinFSP` to 2023
* Updated `boost` to v1.78.0
* Updated `cURL` to v8.3.0
* Updated `zlib` to v1.3
* Use `upload_manager` for all providers
  * Adds a delay to uploads to prevent excessive API calls
  * Supports re-upload after mount restart for incomplete uploads
  * NOTE: Uploads for all providers are full file (no resume support)
    * Multipart upload support is planned for S3

Reviewed-on: #9
This commit is contained in:
2023-10-29 06:55:59 +00:00
parent 3ff46723b8
commit f43c41f88a
839 changed files with 98214 additions and 92959 deletions

View File

@ -32,10 +32,11 @@ namespace macaron {
namespace 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', '+', '/'};
'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;
@ -47,8 +48,10 @@ static std::string Encode(const char *data, const size_t &len) {
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] & 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) {
@ -57,7 +60,8 @@ static std::string Encode(const char *data, const size_t &len) {
*p++ = sEncodingTable[((data[i] & 0x3) << 4)];
*p++ = '=';
} else {
*p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int)(data[i + 1] & 0xF0) >> 4)];
*p++ = sEncodingTable[((data[i] & 0x3) << 4) |
((int)(data[i + 1] & 0xF0) >> 4)];
*p++ = sEncodingTable[((data[i + 1] & 0xF) << 2)];
}
*p++ = '=';
@ -67,22 +71,27 @@ static std::string Encode(const char *data, const size_t &len) {
return ret;
}
static std::string Encode(const std::string &data) { return Encode(&data[0], data.size()); }
[[maybe_unused]] static std::string Encode(const std::string &data) {
return Encode(&data[0], data.size());
}
static std::vector<char> Decode(const std::string &input) {
[[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};
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()) {
@ -99,12 +108,21 @@ static std::vector<char> Decode(const std::string &input) {
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 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);
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;

View File

@ -0,0 +1,49 @@
/*
Copyright <2018-2023> <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

@ -1,57 +1,55 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
Copyright <2018-2023> <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
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 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.
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 "common.hpp"
#include "types/repertory.hpp"
namespace repertory::utils::cli {
typedef std::array<std::string, 2> option;
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"};
static const option export_option = {"-ex", "--export"};
static const option export_all_option = {"-ea", "--export_all"};
#if defined(REPERTORY_ENABLE_S3)
static const option s3_option = {"-s3", "--s3"};
static const option create_directory_option = {"-cd", "--create_directory"};
static const option list_objects_option = {"-lo", "--list_objects"};
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_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 import_option = {"-im", "--import"};
static const option import_json_option = {"-ij", "--import_json"};
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"};
#if defined(REPERTORY_ENABLE_SKYNET)
static const option skynet_option = {"-sk", "--skynet"};
static const option test_skynet_auth_option = {"-tsa", "--test_skynet_auth"};
#endif // defined(REPERTORY_ENABLE_SKYNET)
static const option remote_mount_option = {"-rm", "--remote_mount"};
static const option set_option = {"-set", "--set"};
static const option status_option = {"-status", "--status"};
@ -65,11 +63,12 @@ static const std::vector<option> option_list = {
display_config_option,
data_directory_option,
drive_information_option,
export_option,
export_all_option,
encrypt_option,
#if defined(REPERTORY_ENABLE_S3)
s3_option,
name_option,
create_directory_option,
list_objects_option,
#endif // defined(REPERTORY_ENABLE_S3)
generate_config_option,
get_option,
@ -77,16 +76,10 @@ static const std::vector<option> option_list = {
get_pinned_files_option,
help_option,
hidden_option,
import_option,
import_json_option,
open_files_option,
password_option,
pin_file_option,
pinned_status_option,
#if defined(REPERTORY_ENABLE_SKYNET)
skynet_option,
test_skynet_auth_option,
#endif // defined(REPERTORY_ENABLE_SKYNET)
remote_mount_option,
set_option,
status_option,
@ -98,22 +91,29 @@ static const std::vector<option> option_list = {
} // namespace options
// Prototypes
void get_api_authentication_data(std::string &user, std::string &password, std::uint16_t &port,
const provider_type &pt, const std::string &data_directory);
void get_api_authentication_data(std::string &user, std::string &password,
std::uint16_t &port, const provider_type &pt,
const std::string &data_directory);
provider_type get_provider_type_from_args(const int &argc, char *argv[]);
[[nodiscard]] auto get_provider_type_from_args(int argc, char *argv[])
-> provider_type;
bool has_option(const int &argc, char *argv[], const std::string &option_name);
[[nodiscard]] auto has_option(int argc, char *argv[],
const std::string &option_name) -> bool;
bool has_option(const int &argc, char *argv[], const option &opt);
[[nodiscard]] auto has_option(int argc, char *argv[], const option &opt)
-> bool;
std::vector<std::string> parse_option(const int &argc, char *argv[], const std::string &option_name,
std::uint8_t count);
[[nodiscard]] auto parse_option(int argc, char *argv[],
const std::string &option_name,
std::uint8_t count) -> std::vector<std::string>;
exit_code parse_string_option(const int &argc, char **argv, const option &opt, std::string &value);
[[nodiscard]] auto parse_string_option(int argc, char **argv, const option &opt,
std::string &value) -> exit_code;
std::vector<std::string> parse_drive_options(const int &argc, char **argv, provider_type &pt,
std::string &data_directory);
[[nodiscard]] auto parse_drive_options(int argc, char **argv, provider_type &pt,
std::string &data_directory)
-> std::vector<std::string>;
} // namespace repertory::utils::cli
#endif // INCLUDE_UTILS_CLI_UTILS_HPP_

View File

@ -1,31 +1,34 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
Copyright <2018-2023> <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
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 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.
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
#include "common.hpp"
namespace repertory {
class com_init_wrapper {
public:
com_init_wrapper() : uninit_(SUCCEEDED(::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED))) {}
com_init_wrapper()
: uninit_(
SUCCEEDED(::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED))) {}
~com_init_wrapper() {
if (uninit_) {

View File

@ -1,48 +1,67 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
Copyright <2018-2023> <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
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 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.
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 "common.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,
const bool &stop_requested, const std::string &token,
const size_t error_return = 0);
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::nullopt,
const size_t error_return = 0);
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,
const size_t error_return = 0);
encrypting_reader(const encrypting_reader &r);
~encrypting_reader();
public:
typedef std::basic_iostream<char, std::char_traits<char>> iostream;
typedef std::basic_streambuf<char, std::char_traits<char>> streambuf;
using iostream = std::basic_iostream<char, std::char_traits<char>>;
using streambuf = std::basic_streambuf<char, std::char_traits<char>>;
private:
const CryptoPP::SecByteBlock key_;
const bool &stop_requested_;
const key_type key_;
stop_type &stop_requested_;
const size_t error_return_;
std::unordered_map<std::size_t, std::vector<char>> chunk_buffers_;
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_ = 0u;
std::size_t last_data_chunk_size_ = 0u;
std::uint64_t read_offset_ = 0u;
@ -55,32 +74,63 @@ private:
static const std::size_t encrypted_chunk_size_;
private:
size_t reader_function(char *buffer, size_t size, size_t nitems);
auto reader_function(char *buffer, size_t size, size_t nitems) -> size_t;
public:
static std::uint64_t calculate_decrypted_size(const std::uint64_t &total_size);
[[nodiscard]] static auto calculate_decrypted_size(std::uint64_t total_size)
-> std::uint64_t;
std::shared_ptr<iostream> create_iostream() const;
[[nodiscard]] static auto
calculate_encrypted_size(const std::string &source_path) -> std::uint64_t;
static constexpr const std::size_t &get_encrypted_chunk_size() { return encrypted_chunk_size_; }
[[nodiscard]] auto create_iostream() const -> std::shared_ptr<iostream>;
static constexpr const std::size_t &get_data_chunk_size() { return data_chunk_size_; }
std::string get_encrypted_file_name() const { return encrypted_file_name_; }
std::size_t get_error_return() const { return error_return_; }
static constexpr const std::size_t &get_header_size() { return header_size_; }
const bool &get_stop_requested() const { return stop_requested_; }
std::uint64_t get_total_size() const { return total_size_; }
static size_t reader_function(char *buffer, size_t size, size_t nitems, void *instream) {
return reinterpret_cast<encrypting_reader *>(instream)->reader_function(buffer, size, nitems);
[[nodiscard]] static constexpr auto get_encrypted_chunk_size()
-> std::size_t {
return encrypted_chunk_size_;
}
void set_read_position(const std::uint64_t &position) { read_offset_ = position; }
[[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

View File

@ -1,122 +1,157 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
Copyright <2018-2023> <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
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 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.
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 "common.hpp"
#include "types/repertory.hpp"
#include "utils/encrypting_reader.hpp"
namespace repertory::utils::encryption {
// Prototypes
api_error decrypt_file_name(const std::string &encryption_token, std::string &file_name);
[[nodiscard]] auto decrypt_file_path(const std::string &encryption_token,
std::string &file_path) -> api_error;
CryptoPP::SecByteBlock generate_key(const std::string &encryption_token);
[[nodiscard]] auto decrypt_file_name(const std::string &encryption_token,
std::string &file_name) -> api_error;
api_error read_encrypted_range(
const http_range &range, const CryptoPP::SecByteBlock &key,
const std::function<api_error(std::vector<char> &ct, const std::uint64_t &start_offset,
const std::uint64_t &end_offset)> &reader,
const std::uint64_t &total_size, std::vector<char> &data);
[[nodiscard]] auto generate_key(const std::string &encryption_token)
-> key_type;
[[nodiscard]] auto read_encrypted_range(
const http_range &range, const key_type &key,
const std::function<api_error(data_buffer &ct, std::uint64_t start_offset,
std::uint64_t end_offset)> &reader,
std::uint64_t total_size, data_buffer &data) -> api_error;
// Implementations
template <typename result>
static bool decrypt_data(const CryptoPP::SecByteBlock &key, const char *buffer,
const std::size_t &buffer_size, result &res) {
[[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) {
CryptoPP::XChaCha20Poly1305::Decryption decryption;
CryptoPP::SecByteBlock iv(decryption.IVSize());
std::memcpy(&iv[0u], &buffer[0u], iv.size());
CryptoPP::SecByteBlock mac(decryption.DigestSize());
std::memcpy(&mac[0u], &buffer[iv.size()], mac.size());
decryption.SetKeyWithIV(key, key.size(), iv, iv.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 decryption.DecryptAndVerify(
reinterpret_cast<CryptoPP::byte *>(&res[0u]), &mac[0u], mac.size(), &iv[0u],
static_cast<int>(iv.size()), reinterpret_cast<const CryptoPP::byte *>(&size), sizeof(size),
reinterpret_cast<const CryptoPP::byte *>(&buffer[header_size]), res.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>
static bool decrypt_data(const CryptoPP::SecByteBlock &key, const buffer &buf, result &res) {
[[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>
static bool decrypt_data(const std::string &encryption_token, const buffer &buf, result &res) {
[[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>
static bool decrypt_data(const std::string &encryption_token, const char *buffer,
const std::size_t &buffer_size, result &res) {
return decrypt_data<result>(generate_key(encryption_token), buffer, buffer_size, res);
[[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>
static void encrypt_data(const CryptoPP::SecByteBlock &key, const char *buffer,
const std::size_t &buffer_size, result &res) {
CryptoPP::XChaCha20Poly1305::Encryption encryption;
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{};
CryptoPP::SecByteBlock iv(encryption.IVSize());
CryptoPP::OS_GenerateRandomBlock(false, iv, iv.size());
CryptoPP::SecByteBlock mac(encryption.DigestSize());
const std::uint32_t header_size =
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));
encryption.SetKeyWithIV(key, key.size(), iv, iv.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);
encryption.EncryptAndAuthenticate(reinterpret_cast<CryptoPP::byte *>(&res[header_size]), &mac[0u],
mac.size(), &iv[0u], static_cast<int>(iv.size()),
reinterpret_cast<const CryptoPP::byte *>(&size), sizeof(size),
reinterpret_cast<const CryptoPP::byte *>(buffer), buffer_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>
static void encrypt_data(const std::string &encryption_token, const char *buffer,
const std::size_t &buffer_size, result &res) {
return encrypt_data<result>(generate_key(encryption_token), buffer, buffer_size, res);
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>
static void encrypt_data(const std::string &encryption_token, const buffer &buf, result &res) {
return encrypt_data<result>(generate_key(encryption_token), &buf[0u], buf.size(), res);
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>
static void encrypt_data(const CryptoPP::SecByteBlock &key, const buffer &buf, result &res) {
return encrypt_data<result>(key, &buf[0u], buf.size(), res);
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

View File

@ -0,0 +1,84 @@
/*
Copyright <2018-2023> <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_view function, std::string_view msg);
void raise_error(std::string_view function, const api_error &e,
std::string_view msg);
void raise_error(std::string_view function, const std::exception &e,
std::string_view msg);
void raise_error(std::string_view function, std::int64_t e,
std::string_view msg);
void raise_error(std::string_view function, const json &e,
std::string_view msg);
void raise_error(std::string_view function, const api_error &e,
std::string_view file_path, std::string_view msg);
void raise_error(std::string_view function, std::int64_t e,
std::string_view file_path, std::string_view msg);
void raise_error(std::string_view function, const std::exception &e,
std::string_view file_path, std::string_view msg);
void raise_api_path_error(std::string_view function, std::string_view api_path,
const api_error &e, std::string_view msg);
void raise_api_path_error(std::string_view function, std::string_view api_path,
const std::exception &e, std::string_view msg);
void raise_api_path_error(std::string_view function, std::string_view api_path,
std::int64_t e, std::string_view msg);
void raise_api_path_error(std::string_view function, std::string_view api_path,
const json &e, std::string_view msg);
void raise_api_path_error(std::string_view function, std::string_view api_path,
std::string_view source_path, const api_error &e,
std::string_view msg);
void raise_api_path_error(std::string_view function, std::string_view api_path,
std::string_view source_path, std::int64_t e,
std::string_view msg);
void raise_api_path_error(std::string_view function, std::string_view api_path,
std::string_view source_path, const std::exception &e,
std::string_view msg);
void raise_url_error(std::string_view function, std::string_view url,
CURLcode e, std::string_view msg);
void raise_url_error(std::string_view function, std::string_view url,
std::string_view source_path, const std::exception &e,
std::string_view msg);
} // namespace repertory::utils::error
#endif // INCLUDE_UTILS_ERROR_UTILS_HPP_

View File

@ -1,106 +1,96 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
Copyright <2018-2023> <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
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 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.
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 "common.hpp"
#include "types/repertory.hpp"
#include "utils/native_file.hpp"
namespace repertory::utils::file {
// Prototypes
api_error assign_and_get_native_file(filesystem_item &fi, native_file_ptr &nf);
std::uint64_t calculate_used_space(std::string path, const bool &recursive);
[[nodiscard]] auto calculate_used_space(std::string path, bool recursive)
-> std::uint64_t;
void change_to_process_directory();
bool copy_directory_recursively(std::string from_path, std::string to_path);
[[nodiscard]] auto copy_directory_recursively(std::string from_path,
std::string to_path) -> bool;
bool copy_file(std::string from_path, std::string to_path);
[[nodiscard]] auto copy_file(std::string from_path, std::string to_path)
-> bool;
bool create_full_directory_path(std::string path);
[[nodiscard]] auto create_full_directory_path(std::string path) -> bool;
bool delete_directory(std::string path, const bool &recursive = false);
[[nodiscard]] auto delete_directory(std::string path, bool recursive = false)
-> bool;
bool delete_directory_recursively(std::string path);
[[nodiscard]] auto delete_directory_recursively(std::string path) -> bool;
bool delete_file(std::string path);
[[nodiscard]] auto delete_file(std::string path) -> bool;
std::string generate_sha256(const std::string &file_path);
[[nodiscard]] auto generate_sha256(const std::string &file_path) -> std::string;
std::uint64_t get_available_drive_space(const std::string &path);
[[nodiscard]] auto get_accessed_time(const std::string &path,
std::uint64_t &accessed) -> bool;
std::deque<std::string> get_directory_files(std::string path, const bool &oldest_first,
const bool &recursive = false);
[[nodiscard]] auto get_directory_files(std::string path, bool oldest_first,
bool recursive = false)
-> std::deque<std::string>;
bool get_accessed_time(const std::string &path, std::uint64_t &accessed);
[[nodiscard]] auto get_free_drive_space(const std::string &path)
-> std::uint64_t;
bool get_file_size(std::string path, std::uint64_t &file_size);
[[nodiscard]] auto get_total_drive_space(const std::string &path)
-> std::uint64_t;
bool get_modified_time(const std::string &path, std::uint64_t &modified);
[[nodiscard]] auto get_file_size(std::string path, std::uint64_t &file_size)
-> bool;
#ifdef _WIN32
static open_file_data get_read_write_open_flags() { return open_file_data{}; }
#else // _WIN32
static int get_read_write_open_flags() { return O_RDWR; }
#endif // _WIN32
[[nodiscard]] auto get_modified_time(const std::string &path,
std::uint64_t &modified) -> bool;
bool is_directory(const std::string &path);
[[nodiscard]] auto is_directory(const std::string &path) -> bool;
bool is_file(const std::string &path);
[[nodiscard]] auto is_file(const std::string &path) -> bool;
bool is_modified_date_older_than(const std::string &path, const std::chrono::hours &hours);
[[nodiscard]] auto is_modified_date_older_than(const std::string &path,
const std::chrono::hours &hours)
-> bool;
bool move_file(std::string from, std::string to);
[[nodiscard]] auto move_file(std::string from, std::string to) -> bool;
std::vector<std::string> read_file_lines(const std::string &path);
[[nodiscard]] auto read_file_lines(const std::string &path)
-> std::vector<std::string>;
api_error read_from_source(filesystem_item &fi, const std::size_t &read_size,
const std::uint64_t &read_offset, std::vector<char> &data);
[[nodiscard]] auto read_json_file(const std::string &path, json &data) -> bool;
template <typename t1, typename t2>
bool read_from_stream(t1 *input_file, t2 &buf, std::size_t to_read);
[[nodiscard]] auto reset_modified_time(const std::string &path) -> bool;
bool read_json_file(const std::string &path, json &data);
[[nodiscard]] auto retry_delete_directory(const std::string &dir) -> bool;
bool reset_modified_time(const std::string &path);
[[nodiscard]] auto retry_delete_file(const std::string &file) -> bool;
api_error truncate_source(filesystem_item &fi, const std::uint64_t &size);
bool write_json_file(const std::string &path, const json &j);
api_error write_to_source(filesystem_item &fi, const std::uint64_t &write_offset,
const std::vector<char> &data, std::size_t &bytes_written);
// template implementations
template <typename t1, typename t2>
bool read_from_stream(t1 *input_file, t2 &buf, std::size_t to_read) {
std::size_t offset = 0u;
do {
input_file->read((char *)&buf[offset], to_read);
to_read -= static_cast<std::size_t>(input_file->gcount());
offset += static_cast<std::size_t>(input_file->gcount());
} while (not input_file->fail() && (to_read > 0u));
return (to_read == 0u);
}
[[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

@ -1,68 +0,0 @@
/*
Copyright <2018-2022> <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_GLOBAL_DATA_HPP_
#define INCLUDE_UTILS_GLOBAL_DATA_HPP_
#include "common.hpp"
namespace repertory {
class global_data final {
public:
global_data(const global_data &) = delete;
global_data(global_data &&) = delete;
global_data &operator=(const global_data &) = delete;
global_data &operator=(global_data &&) = delete;
private:
global_data() : used_cache_space_(0u), used_drive_space_(0u) {}
~global_data() = default;
private:
static global_data instance_;
public:
static global_data &instance() { return instance_; }
private:
std::atomic<std::uint64_t> used_cache_space_;
std::atomic<std::uint64_t> used_drive_space_;
public:
void decrement_used_drive_space(const std::uint64_t &val) { used_drive_space_ -= val; }
std::uint64_t get_used_cache_space() const { return used_cache_space_; }
std::uint64_t get_used_drive_space() const { return used_drive_space_; }
void increment_used_drive_space(const std::uint64_t &val) { used_drive_space_ += val; }
void initialize_used_cache_space(const std::uint64_t &val) { used_cache_space_ = val; }
void initialize_used_drive_space(const std::uint64_t &val) { used_drive_space_ = val; }
void update_used_space(const std::uint64_t &file_size, const std::uint64_t &new_file_size,
const bool &cache_only);
};
} // namespace repertory
#endif // INCLUDE_UTILS_GLOBAL_DATA_HPP_

View File

@ -1,89 +1,111 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
Copyright <2018-2023> <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
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 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.
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 "common.hpp"
#include "types/repertory.hpp"
namespace repertory {
class native_file final {
public:
typedef std::shared_ptr<native_file> native_file_ptr;
using native_file_ptr = std::shared_ptr<native_file>;
public:
static native_file_ptr attach(OSHandle handle) {
return native_file_ptr(new native_file(handle));
[[nodiscard]] static auto attach(native_handle handle) -> native_file_ptr {
return std::shared_ptr<native_file>(new native_file(handle));
}
static native_file_ptr clone(const native_file_ptr &nativeFile);
[[nodiscard]] static auto clone(const native_file_ptr &nativeFile)
-> native_file_ptr;
static api_error create_or_open(const std::string &source_path, native_file_ptr &nf);
[[nodiscard]] static auto create_or_open(const std::string &source_path,
bool should_chmod,
native_file_ptr &nf) -> api_error;
static api_error open(const std::string &source_path, native_file_ptr &nf);
[[nodiscard]] static auto create_or_open(const std::string &source_path,
native_file_ptr &nf) -> api_error;
[[nodiscard]] static auto open(const std::string &source_path,
native_file_ptr &nf) -> api_error;
[[nodiscard]] static auto open(const std::string &source_path,
bool should_chmod, native_file_ptr &nf)
-> api_error;
private:
explicit native_file(const OSHandle &handle) : handle_(handle) {}
explicit native_file(const native_handle &handle) : handle_(handle) {}
public:
~native_file() = default;
~native_file();
private:
OSHandle handle_;
native_handle handle_;
private:
bool auto_close{false};
#ifdef _WIN32
std::recursive_mutex read_write_mutex_;
#endif
public:
bool allocate(const std::uint64_t &file_size);
[[nodiscard]] auto allocate(std::uint64_t file_size) -> bool;
void close();
bool copy_from(const native_file_ptr &source);
[[nodiscard]] auto copy_from(const native_file_ptr &source) -> bool;
bool copy_from(const std::string &path);
[[nodiscard]] auto copy_from(const std::string &path) -> bool;
void flush();
bool get_file_size(std::uint64_t &file_size);
[[nodiscard]] auto get_file_size(std::uint64_t &file_size) -> bool;
OSHandle get_handle() { return handle_; }
[[nodiscard]] auto get_handle() -> native_handle;
#ifdef _WIN32
bool read_bytes(char *buffer, const std::size_t &read_size, const std::uint64_t &read_offset,
std::size_t &bytes_read);
[[nodiscard]] auto read_bytes(char *buffer, std::size_t read_size,
std::uint64_t read_offset,
std::size_t &bytes_read) -> bool;
#else
bool read_bytes(char *buffer, const std::size_t &read_size, const std::uint64_t &read_offset,
std::size_t &bytes_read);
[[nodiscard]] auto read_bytes(char *buffer, std::size_t read_size,
std::uint64_t read_offset,
std::size_t &bytes_read) -> bool;
#endif
bool truncate(const std::uint64_t &file_size);
void set_auto_close(bool b) { auto_close = b; }
[[nodiscard]] auto truncate(std::uint64_t file_size) -> bool;
#ifdef _WIN32
bool write_bytes(const char *buffer, const std::size_t &write_size,
const std::uint64_t &write_offset, std::size_t &bytes_written);
[[nodiscard]] auto write_bytes(const char *buffer, std::size_t write_size,
std::uint64_t write_offset,
std::size_t &bytes_written) -> bool;
#else
bool write_bytes(const char *buffer, const std::size_t &write_size,
const std::uint64_t &write_offset, std::size_t &bytes_written);
[[nodiscard]] auto write_bytes(const char *buffer, std::size_t write_size,
std::uint64_t write_offset,
std::size_t &bytes_written) -> bool;
#endif
};
typedef native_file::native_file_ptr native_file_ptr;
using native_file_ptr = native_file::native_file_ptr;
} // namespace repertory
#endif // INCLUDE_UTILS_NATIVEFILE_HPP_

View File

@ -1,105 +0,0 @@
/*
Copyright <2018-2022> <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_OPTIONAL_HPP_
#define INCLUDE_UTILS_OPTIONAL_HPP_
#if IS_DEBIAN9_DISTRO || !HAS_STD_OPTIONAL
#include <memory>
namespace std {
class bad_optional_access : std::runtime_error {
public:
bad_optional_access() : std::runtime_error("bad optional access") {}
};
template <typename t> class optional {
public:
optional() = default;
explicit optional(const t &v) : val_(new t(v)) {}
explicit optional(t &&v) noexcept : val_(new t(std::move(v))) {}
optional(const optional &o) noexcept : val_(o.has_value() ? new t(o.val_) : nullptr) {}
optional(optional &&o) noexcept : val_(o.has_value() ? std::move(o.val_) : nullptr) {}
private:
std::unique_ptr<t> val_;
public:
bool has_value() const { return (val_ != nullptr); }
t &value() {
if (not has_value()) {
throw bad_optional_access();
}
return *val_;
}
const t &value() const {
if (not has_value()) {
throw bad_optional_access();
}
return *val_;
}
optional &operator=(const t &v) {
if (&v != val_.get()) {
val_.reset(new t(v));
}
return *this;
}
optional &operator=(const optional &v) {
if (&v != this) {
val_.reset(new t(v.has_value() ? v.value() : nullptr));
}
return *this;
}
optional &operator=(t &&v) noexcept {
if (&v != val_.get()) {
val_.reset(new t(std::move(v)));
}
return *this;
}
optional &operator=(optional &&v) noexcept {
if (&v != this) {
val_ = std::move(v);
}
return *this;
}
bool operator==(const optional &v) {
return (&v == this) || ((has_value() && v.has_value()) && (*val_ == *v.val_));
}
bool operator==(const t &v) { return has_value() && (v == *val_); }
bool operator!=(const optional &v) {
return (has_value() != v.has_value) || ((has_value() && v.has_value()) && (*val_ != *v.val_));
}
bool operator!=(const t &v) { return not has_value() || (v != *val_); }
};
} // namespace std
#endif // IS_DEBIAN9_DISTRO || !HAS_STD_OPTIONAL
#endif // INCLUDE_UTILS_OPTIONAL_HPP_

View File

@ -1,26 +1,27 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
Copyright <2018-2023> <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
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 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.
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_
#include "common.hpp"
namespace repertory::utils::path {
#ifdef _WIN32
static const std::string directory_seperator = "\\";
@ -31,31 +32,36 @@ static const std::string not_directory_seperator = "\\";
#endif
// Prototypes
std::string absolute(std::string path);
[[nodiscard]] auto absolute(std::string path) -> std::string;
std::string combine(std::string path, const std::vector<std::string> &paths);
[[nodiscard]] auto combine(std::string path,
const std::vector<std::string> &paths)
-> std::string;
std::string create_api_path(std::string path);
[[nodiscard]] auto create_api_path(std::string path) -> std::string;
std::string finalize(std::string path);
[[nodiscard]] auto finalize(std::string path) -> std::string;
std::string get_parent_api_path(const std::string &path);
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
std::string get_parent_directory(std::string path);
[[nodiscard]] auto get_parent_directory(std::string path) -> std::string;
#endif
bool is_ads_file_path(const std::string &path);
[[nodiscard]] auto is_ads_file_path(const std::string &path) -> bool;
bool is_trash_directory(std::string path);
[[nodiscard]] auto is_trash_directory(std::string path) -> bool;
std::string remove_file_name(std::string path);
[[nodiscard]] auto remove_file_name(std::string path) -> std::string;
#ifndef _WIN32
std::string resolve(std::string path);
[[nodiscard]] auto resolve(std::string path) -> std::string;
#endif
std::string strip_to_file_name(std::string path);
[[nodiscard]] auto strip_to_file_name(std::string path) -> std::string;
} // namespace repertory::utils::path
#endif // INCLUDE_UTILS_PATH_UTILS_HPP_

View File

@ -1,41 +1,50 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
Copyright <2018-2023> <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
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 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.
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 "common.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;
bool low_frequency;
frequency freq;
std::function<void()> action;
};
public:
polling(const polling &) = delete;
polling(polling &&) = delete;
polling &operator=(const polling &) = delete;
polling &operator=(polling &&) = delete;
auto operator=(const polling &) -> polling & = delete;
auto operator=(polling &&) -> polling & = delete;
private:
polling() = default;
@ -46,7 +55,7 @@ private:
static polling instance_;
public:
static polling &instance() { return instance_; }
static auto instance() -> polling & { return instance_; }
private:
app_config *config_ = nullptr;
@ -55,11 +64,13 @@ private:
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_;
bool stop_requested_ = false;
stop_type stop_requested_ = false;
private:
void frequency_thread(std::function<std::uint32_t()> get_frequency_seconds, bool low_frequency);
void frequency_thread(std::function<std::uint32_t()> get_frequency_seconds,
frequency freq);
public:
void remove_callback(const std::string &name);

View File

@ -1,36 +1,39 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
Copyright <2018-2023> <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
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 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.
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_ROCKSDB_UTILS_HPP_
#define INCLUDE_UTILS_ROCKSDB_UTILS_HPP_
#include "common.hpp"
namespace repertory {
class app_config;
namespace utils::db {
void create_rocksdb(const app_config &config, const std::string &name,
std::unique_ptr<rocksdb::DB> &db);
void create_rocksdb(const app_config &config, const std::string &name,
const std::vector<rocksdb::ColumnFamilyDescriptor> &families,
std::vector<rocksdb::ColumnFamilyHandle *> &handles,
std::unique_ptr<rocksdb::DB> &db);
void create_rocksdb(
const app_config &config, const std::string &name,
const std::vector<rocksdb::ColumnFamilyDescriptor> &families,
std::vector<rocksdb::ColumnFamilyHandle *> &handles,
std::unique_ptr<rocksdb::DB> &db);
} // namespace utils::db
} // namespace repertory

View File

@ -0,0 +1,68 @@
/*
Copyright <2018-2023> <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

@ -1,114 +1,118 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
Copyright <2018-2023> <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
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 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.
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_
#include <string>
#include <vector>
#include <boost/dynamic_bitset.hpp>
#include <string>
#include <string_view>
#include <vector>
namespace repertory::utils::string {
// Prototypes
bool begins_with(const std::string &str, const std::string &val);
constexpr auto begins_with(std::string_view str, std::string_view val) -> bool {
return (str.find(val) == 0u);
}
bool contains(const std::string &str, const std::string &search);
constexpr auto contains(std::string_view str, std::string_view search) -> bool {
return (str.find(search) != std::string_view::npos);
}
bool ends_with(const std::string &str, const std::string &val);
[[nodiscard]] /* constexpr c++20 */ auto ends_with(std::string_view str,
std::string_view val)
-> bool;
std::string from_bool(const bool &val);
[[nodiscard]] auto from_bool(bool val) -> std::string;
std::string from_double(const double &value);
[[nodiscard]] auto from_dynamic_bitset(const boost::dynamic_bitset<> &bitset)
-> std::string;
std::string from_dynamic_bitset(const boost::dynamic_bitset<> &bitset);
[[nodiscard]] auto from_utf8(const std::string &str) -> std::wstring;
std::string from_int32(const std::int32_t &val);
[[nodiscard]] /* constexpr c++20 */ auto is_numeric(std::string_view s) -> bool;
std::string from_int64(const std::int64_t &val);
[[nodiscard]] auto join(const std::vector<std::string> &arr, const char &delim)
-> std::string;
std::string from_uint8(const std::uint8_t &val);
auto left_trim(std::string &s) -> std::string &;
std::string from_uint16(const std::uint16_t &val);
auto left_trim(std::string &s, const char &c) -> std::string &;
std::string from_uint32(const std::uint32_t &val);
auto replace(std::string &src, const char &character, const char &with)
-> std::string &;
std::string from_uint64(const std::uint64_t &val);
auto replace(std::string &src, const std::string &find, const std::string &with,
size_t start_pos = 0) -> std::string &;
std::wstring from_utf8(const std::string &str);
[[nodiscard]] auto replace_copy(std::string src, const char &character,
const char &with) -> std::string;
bool is_numeric(const std::string &s);
[[nodiscard]] auto replace_copy(std::string src, const std::string &find,
const std::string &with, size_t start_pos = 0)
-> std::string;
std::string join(const std::vector<std::string> &arr, const char &delim);
auto right_trim(std::string &s) -> std::string &;
std::string &left_trim(std::string &s);
auto right_trim(std::string &s, const char &c) -> std::string &;
std::string &left_trim(std::string &s, const char &c);
[[nodiscard]] auto split(const std::string &str, const char &delim,
bool should_trim = true) -> std::vector<std::string>;
std::string &replace(std::string &src, const char &character, const char &with);
[[nodiscard]] auto to_bool(std::string val) -> bool;
std::string &replace(std::string &src, const std::string &find, const std::string &with,
size_t startPos = 0);
[[nodiscard]] auto to_double(const std::string &str) -> double;
std::string replace_copy(std::string src, const char &character, const char &with);
[[nodiscard]] auto to_dynamic_bitset(const std::string &val)
-> boost::dynamic_bitset<>;
std::string replace_copy(std::string src, const std::string &find, const std::string &with,
size_t startPos = 0);
[[nodiscard]] auto to_lower(std::string str) -> std::string;
std::string &right_trim(std::string &s);
[[nodiscard]] auto to_int32(const std::string &val) -> std::int32_t;
std::string &right_trim(std::string &s, const char &c);
[[nodiscard]] auto to_int64(const std::string &val) -> std::int64_t;
std::vector<std::string> split(const std::string &str, const char &delim,
const bool &should_trim = true);
[[nodiscard]] auto to_size_t(const std::string &val) -> std::size_t;
bool to_bool(std::string val);
[[nodiscard]] auto to_uint8(const std::string &val) -> std::uint8_t;
double to_double(const std::string &str);
[[nodiscard]] auto to_uint16(const std::string &val) -> std::uint16_t;
boost::dynamic_bitset<> to_dynamic_bitset(const std::string &val);
[[nodiscard]] auto to_uint32(const std::string &val) -> std::uint32_t;
std::string to_lower(std::string str);
[[nodiscard]] auto to_uint64(const std::string &val) -> std::uint64_t;
std::int32_t to_int32(const std::string &val);
[[nodiscard]] auto to_upper(std::string str) -> std::string;
std::int64_t to_int64(const std::string &val);
[[nodiscard]] auto to_utf8(std::string str) -> std::string;
std::uint8_t to_uint8(const std::string &val);
[[nodiscard]] auto to_utf8(const std::wstring &str) -> std::string;
std::uint16_t to_uint16(const std::string &val);
auto trim(std::string &str) -> std::string &;
std::uint32_t to_uint32(const std::string &val);
auto trim(std::string &str, const char &c) -> std::string &;
std::uint64_t to_uint64(const std::string &val);
[[nodiscard]] auto trim_copy(std::string str) -> std::string;
std::string to_upper(std::string str);
const std::string &to_utf8(const std::string &str);
std::string to_utf8(const std::wstring &str);
std::string &trim(std::string &str);
std::string &trim(std::string &str, const char &c);
std::string trim_copy(std::string str);
std::string trim_copy(std::string str, const char &c);
[[nodiscard]] auto trim_copy(std::string str, const char &c) -> std::string;
} // namespace repertory::utils::string
#endif // INCLUDE_UTILS_STRING_UTILS_HPP_

View File

@ -1,38 +1,39 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
Copyright <2018-2023> <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
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 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.
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_
#include "common.hpp"
namespace repertory {
class throttle final {
public:
throttle() : max_size_(10u) {}
explicit throttle(const std::size_t &max_size) : max_size_(max_size) {}
explicit throttle(std::size_t max_size) : max_size_(max_size) {}
public:
throttle(const throttle &) noexcept = delete;
throttle(throttle &&) noexcept = delete;
throttle &operator=(const throttle &) = delete;
throttle &operator=(throttle &&) = delete;
auto operator=(const throttle &) -> throttle & = delete;
auto operator=(throttle &&) -> throttle & = delete;
public:
~throttle() { shutdown(); }

View File

@ -1,33 +1,34 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
Copyright <2018-2023> <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
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 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.
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_
#include "common.hpp"
namespace repertory {
class timeout final {
public:
timeout(const timeout &) noexcept = delete;
timeout(timeout &&) noexcept = delete;
timeout &operator=(const timeout &) noexcept = delete;
timeout &operator=(timeout &&) noexcept = delete;
auto operator=(const timeout &) noexcept -> timeout & = delete;
auto operator=(timeout &&) noexcept -> timeout & = delete;
public:
timeout(std::function<void()> timeout_callback,

View File

@ -1,32 +1,34 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
Copyright <2018-2023> <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
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 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.
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 "common.hpp"
#include "types/remote.hpp"
#include "types/repertory.hpp"
namespace repertory::utils {
#if __linux__
static const std::array<std::string, 4u> attribute_namespaces = {
inline const std::array<std::string, 4u> attribute_namespaces = {
"security",
"system",
"trusted",
@ -35,33 +37,43 @@ static const std::array<std::string, 4u> attribute_namespaces = {
#endif
#if __APPLE__
template <typename t> std::uint64_t convert_to_uint64(const t *v);
template <typename t>
[[nodiscard]] auto convert_to_uint64(const t *v) -> std::uint64_t;
#else
std::uint64_t convert_to_uint64(const pthread_t &t);
[[nodiscard]] auto convert_to_uint64(const pthread_t &t) -> std::uint64_t;
#endif
int get_last_error_code();
[[nodiscard]] auto from_api_error(const api_error &e) -> int;
std::uint64_t get_thread_id();
[[nodiscard]] auto get_last_error_code() -> int;
bool is_uid_member_of_group(const uid_t &uid, const gid_t &gid);
[[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);
int translate_api_error(const api_error &e);
[[nodiscard]] auto to_api_error(int e) -> api_error;
std::int32_t unix_error_to_windows(const int &e);
[[nodiscard]] auto unix_error_to_windows(int e) -> std::int32_t;
UINT64 unix_time_to_windows_time(const remote::file_time &ts);
[[nodiscard]] auto unix_time_to_windows_time(const remote::file_time &ts)
-> UINT64;
void windows_create_to_unix(const UINT32 &create_options, const UINT32 &granted_access,
std::uint32_t &flags, remote::file_mode &mode);
void use_getpwuid(uid_t uid, std::function<void(struct passwd *pw)> fn);
remote::file_time windows_time_to_unix_time(const std::uint64_t &t);
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 t)
-> remote::file_time;
// template implementations
#if __APPLE__
template <typename t> std::uint64_t convert_to_uint64(const t *v) {
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

View File

@ -1,112 +1,138 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
Copyright <2018-2023> <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
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 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.
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 "common.hpp"
#include "types/remote.hpp"
#include "types/repertory.hpp"
#include "utils/unix/unix_utils.hpp"
#include "utils/windows/windows_utils.hpp"
namespace repertory::utils {
hastings api_currency_to_hastings(const api_currency &currency);
void calculate_allocation_size(bool directory, std::uint64_t file_size,
UINT64 allocation_size,
std::string &allocation_meta_size);
void calculate_allocation_size(const bool &directory, const 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;
std::size_t calculate_read_size(const uint64_t &total_size, const std::size_t &read_size,
const uint64_t &offset);
template <typename t>
[[nodiscard]] auto collection_excludes(t collection,
const typename t::value_type &v) -> bool;
template <typename t> bool collection_excludes(t collection, const typename t::value_type &v);
template <typename t>
[[nodiscard]] auto collection_includes(t collection,
const typename t::value_type &v) -> bool;
template <typename t> bool collection_includes(t collection, const typename t::value_type &v);
[[nodiscard]] auto compare_version_strings(std::string version1,
std::string version2) -> int;
int compare_version_strings(std::string version1, std::string version2);
[[nodiscard]] auto convert_api_date(const std::string &date) -> std::uint64_t;
std::uint64_t convert_api_date(const std::string &date);
[[nodiscard]] auto create_curl() -> CURL *;
CURL *create_curl();
[[nodiscard]] auto create_uuid_string() -> std::string;
std::string create_uuid_string();
[[nodiscard]] auto create_volume_label(const provider_type &pt) -> std::string;
std::string create_volume_label(const provider_type &pt);
template <typename t>
[[nodiscard]] auto divide_with_ceiling(const t &n, const t &d) -> t;
template <typename t> t divide_with_ceiling(const t &n, const t &d);
[[nodiscard]] auto download_type_from_string(std::string type,
const download_type &default_type)
-> download_type;
download_type download_type_from_string(std::string type, const download_type &default_type);
[[nodiscard]] auto download_type_to_string(const download_type &type)
-> std::string;
std::string download_type_to_string(const download_type &type);
template <typename t>
[[nodiscard]] auto from_hex_string(const std::string &str, t &v) -> bool;
template <typename t> bool from_hex_string(const std::string &str, t &v);
[[nodiscard]] auto generate_random_string(std::uint16_t length) -> std::string;
std::string generate_random_string(const std::uint16_t &length);
[[nodiscard]] auto get_attributes_from_meta(const api_meta_map &meta) -> DWORD;
std::string get_environment_variable(const std::string &variable);
[[nodiscard]] auto get_environment_variable(const std::string &variable)
-> std::string;
std::uint64_t get_file_time_now();
[[nodiscard]] auto get_file_time_now() -> std::uint64_t;
void get_local_time_now(struct tm &localTime);
bool get_next_available_port(std::uint16_t first_port, std::uint16_t &available_port);
[[nodiscard]] auto get_next_available_port(std::uint16_t first_port,
std::uint16_t &available_port)
-> bool;
std::uint64_t get_time_now();
[[nodiscard]] auto get_time_now() -> std::uint64_t;
api_currency hastings_string_to_api_currency(const std::string &amount);
template <typename t>
[[nodiscard]] auto random_between(const t &begin, const t &end) -> t;
// bool parse_url(const std::string &url, HostConfig &hc);
template <typename t>
void remove_element_from(t &v, const typename t::value_type &value);
template <typename t> t random_between(const t &begin, const t &end);
[[nodiscard]] auto reset_curl(CURL *curl_handle) -> CURL *;
template <typename t> void remove_element_from(t &v, const typename t::value_type &value);
[[nodiscard]] auto retryable_action(const std::function<bool()> &action)
-> bool;
CURL *reset_curl(CURL *curl_handle);
bool retryable_action(const std::function<bool()> &action);
void spin_wait_for_mutex(std::function<bool()> complete, std::condition_variable &cv,
std::mutex &mtx, const std::string &txt = "");
void spin_wait_for_mutex(bool &complete, std::condition_variable &cv, std::mutex &mtx,
void spin_wait_for_mutex(std::function<bool()> complete,
std::condition_variable &cv, std::mutex &mtx,
const std::string &txt = "");
template <typename t> std::string to_hex_string(const t &v);
void spin_wait_for_mutex(bool &complete, std::condition_variable &cv,
std::mutex &mtx, const std::string &txt = "");
template <typename t>
[[nodiscard]] auto to_hex_string(const t &v) -> std::string;
// template implementations
template <typename t> bool collection_excludes(t collection, const typename t::value_type &v) {
template <typename t>
[[nodiscard]] auto collection_excludes(t collection,
const typename t::value_type &v)
-> bool {
return std::find(collection.begin(), collection.end(), v) == collection.end();
}
template <typename t> bool collection_includes(t collection, const typename t::value_type &v) {
template <typename t>
[[nodiscard]] auto collection_includes(t collection,
const typename t::value_type &v)
-> bool {
return std::find(collection.begin(), collection.end(), v) != collection.end();
}
template <typename t> t divide_with_ceiling(const t &n, const t &d) {
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> bool from_hex_string(const std::string &str, t &v) {
template <typename t>
[[nodiscard]] auto from_hex_string(const std::string &str, t &v) -> bool {
v.clear();
if (not(str.length() % 2u)) {
for (std::size_t i = 0u; i < str.length(); i += 2u) {
v.emplace_back(
static_cast<typename t::value_type>(strtol(str.substr(i, 2u).c_str(), nullptr, 16)));
v.emplace_back(static_cast<typename t::value_type>(
strtol(str.substr(i, 2u).c_str(), nullptr, 16)));
}
return true;
}
@ -114,26 +140,30 @@ template <typename t> bool from_hex_string(const std::string &str, t &v) {
return false;
}
template <typename t> t random_between(const t &begin, const t &end) {
template <typename t>
[[nodiscard]] auto random_between(const t &begin, const t &end) -> t {
srand(static_cast<unsigned int>(get_time_now()));
return begin + rand() % ((end + 1) - begin);
}
template <typename t> void remove_element_from(t &v, const typename t::value_type &value) {
template <typename t>
void remove_element_from(t &v, const typename t::value_type &value) {
v.erase(std::remove(v.begin(), v.end(), value), v.end());
}
template <typename t> std::string to_hex_string(const t &v) {
std::string ret;
template <typename t>
[[nodiscard]] auto to_hex_string(const t &value) -> std::string {
std::string ret{};
for (const auto &v : v) {
char h[3] = {0};
std::array<char, 3> h{};
for (const auto &num : value) {
#ifdef _WIN32
sprintf_s(&h[0], sizeof(h), "%x", static_cast<std::uint8_t>(v));
sprintf_s(h.data(), h.size() - 1U, "%x", static_cast<std::uint8_t>(num));
#else
sprintf(&h[0], "%x", static_cast<std::uint8_t>(v));
sprintf(h.data(), "%x", static_cast<std::uint8_t>(num));
#endif
ret += ((strlen(h) == 1) ? std::string("0") + h : h);
ret += (strlen(h.data()) == 1) ? std::string("0") + h.data() : h.data();
}
return ret;

View File

@ -253,15 +253,18 @@ public:
/* regular method */
void import(const std::vector<void *> &bin) {
uuid_rc_t rc;
if ((rc = uuid_import(ctx, UUID_FMT_BIN, &bin[0], UUID_LEN_BIN)) != UUID_RC_OK)
if ((rc = uuid_import(ctx, UUID_FMT_BIN, &bin[0], UUID_LEN_BIN)) !=
UUID_RC_OK)
throw uuid_error_t(rc);
}
/* regular method */
void import(const std::string &str) {
uuid_rc_t rc;
if ((rc = uuid_import(ctx, UUID_FMT_STR, &str[0], UUID_LEN_STR)) != UUID_RC_OK)
if ((rc = uuid_import(ctx, UUID_FMT_SIV, &str[0], UUID_LEN_SIV)) != UUID_RC_OK)
if ((rc = uuid_import(ctx, UUID_FMT_STR, &str[0], UUID_LEN_STR)) !=
UUID_RC_OK)
if ((rc = uuid_import(ctx, UUID_FMT_SIV, &str[0], UUID_LEN_SIV)) !=
UUID_RC_OK)
throw uuid_error_t(rc);
}
@ -283,7 +286,8 @@ public:
std::string string() {
uuid_rc_t rc;
char *str = nullptr;
if ((rc = uuid_export(ctx, UUID_FMT_STR, (void **)&str, nullptr)) != UUID_RC_OK)
if ((rc = uuid_export(ctx, UUID_FMT_STR, (void **)&str, nullptr)) !=
UUID_RC_OK)
throw uuid_error_t(rc);
std::string data;
data.resize(UUID_LEN_STR + 1);
@ -296,7 +300,8 @@ public:
std::string integer() {
uuid_rc_t rc;
char *str = nullptr;
if ((rc = uuid_export(ctx, UUID_FMT_SIV, (void **)&str, nullptr)) != UUID_RC_OK)
if ((rc = uuid_export(ctx, UUID_FMT_SIV, (void **)&str, nullptr)) !=
UUID_RC_OK)
throw uuid_error_t(rc);
std::string data;
data.resize(UUID_LEN_SIV + 1);
@ -309,7 +314,8 @@ public:
std::string summary() {
uuid_rc_t rc;
char *txt = nullptr;
if ((rc = uuid_export(ctx, UUID_FMT_TXT, (void **)&txt, nullptr)) != UUID_RC_OK)
if ((rc = uuid_export(ctx, UUID_FMT_TXT, (void **)&txt, nullptr)) !=
UUID_RC_OK)
throw uuid_error_t(rc);
std::string data(txt, strlen(txt));
free(txt);

View File

@ -1,64 +1,74 @@
/*
Copyright <2018-2022> <scott.e.graves@protonmail.com>
Copyright <2018-2023> <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
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 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.
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 "common.hpp"
#include "types/remote.hpp"
#include "types/repertory.hpp"
namespace repertory::utils {
remote::file_time filetime_to_unix_time(const FILETIME &ft);
[[nodiscard]] auto filetime_to_unix_time(const FILETIME &ft)
-> remote::file_time;
DWORD get_last_error_code();
[[nodiscard]] auto get_last_error_code() -> DWORD;
const std::string &get_local_app_data_directory();
[[nodiscard]] auto get_local_app_data_directory() -> const std::string &;
std::uint64_t get_accessed_time_from_meta(const api_meta_map &meta);
[[nodiscard]] auto get_accessed_time_from_meta(const api_meta_map &meta)
-> std::uint64_t;
DWORD get_attributes_from_meta(const api_meta_map &meta);
[[nodiscard]] auto get_changed_time_from_meta(const api_meta_map &meta)
-> std::uint64_t;
std::uint64_t get_changed_time_from_meta(const api_meta_map &meta);
[[nodiscard]] auto get_creation_time_from_meta(const api_meta_map &meta)
-> std::uint64_t;
std::uint64_t get_creation_time_from_meta(const api_meta_map &meta);
[[nodiscard]] auto get_written_time_from_meta(const api_meta_map &meta)
-> std::uint64_t;
std::uint64_t get_written_time_from_meta(const api_meta_map &meta);
[[nodiscard]] auto get_thread_id() -> std::uint64_t;
std::uint64_t get_thread_id();
[[nodiscard]] auto is_process_elevated() -> bool;
bool is_process_elevated();
int run_process_elevated(int argc, char *argv[]);
[[nodiscard]] auto run_process_elevated(int argc, char *argv[]) -> int;
void set_last_error_code(DWORD errorCode);
NTSTATUS translate_api_error(const api_error &e);
[[nodiscard]] auto from_api_error(const api_error &e) -> NTSTATUS;
int unix_access_mask_to_windows(std::int32_t mask);
auto strptime(const char *s, const char *f, struct tm *tm) -> const char *;
int unix_open_flags_to_flags_and_perms(const remote::file_mode &mode, const remote::open_flags &flags,
std::int32_t &perms);
[[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);
remote::file_time time64_to_unix_time(const __time64_t &t);
[[nodiscard]] auto time64_to_unix_time(const __time64_t &t)
-> remote::file_time;
} // namespace repertory::utils
#endif // _WIN32