This commit is contained in:
Scott E. Graves 2024-08-02 09:30:41 -05:00
parent 4fafc17b90
commit b399ff3291
20 changed files with 342 additions and 324 deletions

View File

@ -18,6 +18,7 @@ cppflags
cpphttplib
cpptrace
cppvsdbg
crypto_aead_xchacha20poly1305_ietf_npubbytes
cstdint
cxxflags
cxxstd

View File

@ -57,14 +57,13 @@ private:
bool use_s3_path_style_{false};
public:
[[nodiscard]] static auto construct_url(CURL *curl,
const std::string &relative_path,
const host_config &cfg)
-> std::string;
[[nodiscard]] static auto
construct_url(CURL *curl, const std::string &relative_path,
const host_config &cfg) -> std::string;
[[nodiscard]] static auto create_host_config(const s3_config &cfg,
bool use_s3_path_style)
-> host_config;
[[nodiscard]] static auto
create_host_config(const s3_config &cfg,
bool use_s3_path_style) -> host_config;
[[nodiscard]] static auto url_encode(CURL *curl, const std::string &data,
bool allow_slash) -> std::string;
@ -72,8 +71,8 @@ public:
template <typename request_type>
[[nodiscard]] static auto
make_encrypted_request(const host_config &cfg, const request_type &request,
long &response_code, stop_type &stop_requested)
-> bool {
long &response_code,
stop_type &stop_requested) -> bool {
response_code = 0;
if (not request.decryption_token.has_value() ||
@ -94,7 +93,7 @@ public:
utils::encryption::generate_key(request.decryption_token.value());
const auto result = utils::encryption::read_encrypted_range(
request.range.value(), key,
[&](std::vector<char> &ct, std::uint64_t start_offset,
[&](data_buffer &ct, std::uint64_t start_offset,
std::uint64_t end_offset) -> api_error {
auto encrypted_request = request;
encrypted_request.decryption_token = std::nullopt;
@ -212,30 +211,26 @@ public:
public:
void enable_s3_path_style(bool enable) override;
[[nodiscard]] auto make_request(const curl::requests::http_delete &del,
long &response_code,
stop_type &stop_requested) const
-> bool override;
[[nodiscard]] auto
make_request(const curl::requests::http_delete &del, long &response_code,
stop_type &stop_requested) const -> bool override;
[[nodiscard]] auto make_request(const curl::requests::http_get &get,
long &response_code,
stop_type &stop_requested) const
-> bool override;
[[nodiscard]] auto
make_request(const curl::requests::http_get &get, long &response_code,
stop_type &stop_requested) const -> bool override;
[[nodiscard]] auto make_request(const curl::requests::http_head &head,
long &response_code,
stop_type &stop_requested) const
-> bool override;
[[nodiscard]] auto
make_request(const curl::requests::http_head &head, long &response_code,
stop_type &stop_requested) const -> bool override;
[[nodiscard]] auto make_request(const curl::requests::http_post &post_file,
long &response_code,
stop_type &stop_requested) const
-> bool override;
[[nodiscard]] auto
make_request(const curl::requests::http_post &post_file, long &response_code,
stop_type &stop_requested) const -> bool override;
[[nodiscard]] auto make_request(const curl::requests::http_put_file &put_file,
long &response_code,
stop_type &stop_requested) const
-> bool override;
[[nodiscard]] auto
make_request(const curl::requests::http_put_file &put_file,
long &response_code,
stop_type &stop_requested) const -> bool override;
};
} // namespace repertory

View File

@ -41,8 +41,9 @@ inline const auto read_file_data = static_cast<read_callback>(
[](char *buffer, size_t size, size_t nitems, void *instream) -> size_t {
auto *read_info = reinterpret_cast<read_file_info *>(instream);
std::size_t bytes_read{};
auto ret = read_info->nf->read_bytes(buffer, size * nitems,
read_info->offset, bytes_read);
auto ret = read_info->nf->read_bytes(
reinterpret_cast<unsigned char *>(buffer), size * nitems,
read_info->offset, bytes_read);
if (ret) {
read_info->offset += bytes_read;
}
@ -73,9 +74,8 @@ struct http_request_base {
[[nodiscard]] virtual auto get_path() const -> std::string { return path; }
[[nodiscard]] virtual auto set_method(CURL *curl,
stop_type &stop_requested) const
-> bool = 0;
[[nodiscard]] virtual auto
set_method(CURL *curl, stop_type &stop_requested) const -> bool = 0;
};
} // namespace repertory::curl::requests

View File

@ -56,18 +56,18 @@ private:
std::size_t decode_offset_ = 0U;
public:
[[nodiscard]] static auto decode_json(packet &response, json &json_data)
-> int;
[[nodiscard]] static auto decode_json(packet &response,
json &json_data) -> int;
public:
void clear();
[[nodiscard]] auto current_pointer() -> char * {
[[nodiscard]] auto current_pointer() -> unsigned char * {
return (decode_offset_ < buffer_.size()) ? &buffer_[decode_offset_]
: nullptr;
}
[[nodiscard]] auto current_pointer() const -> const char * {
[[nodiscard]] auto current_pointer() const -> const unsigned char * {
return (decode_offset_ < buffer_.size()) ? &buffer_[decode_offset_]
: nullptr;
}
@ -110,19 +110,19 @@ public:
[[nodiscard]] auto decode(remote::file_info &val) -> error_type;
[[nodiscard]] auto decrypt(const std::string &token) -> error_type;
[[nodiscard]] auto decrypt(std::string_view token) -> error_type;
void encode(const void *buffer, std::size_t size, bool should_reserve = true);
void encode(const char *str) {
encode(std::string(str == nullptr ? "" : str));
encode(std::string{str == nullptr ? "" : str});
}
void encode(const std::string &str);
void encode(std::string_view str);
void encode(const wchar_t *str);
void encode(const std::wstring &str);
void encode(std::wstring_view str);
void encode(void *ptr) {
encode(static_cast<std::uint64_t>(reinterpret_cast<std::uintptr_t>(ptr)));
@ -161,9 +161,9 @@ public:
void encode_top(const void *buffer, std::size_t size,
bool should_reserve = true);
void encode_top(const std::string &str);
void encode_top(std::string_view str);
void encode_top(const std::wstring &str);
void encode_top(std::wstring_view str);
void encode_top(void *ptr) {
encode_top(
@ -200,7 +200,7 @@ public:
void encode_top(remote::file_info val);
void encrypt(const std::string &token);
void encrypt(std::string_view token);
[[nodiscard]] auto get_size() const -> std::uint32_t {
return static_cast<std::uint32_t>(buffer_.size());
@ -217,11 +217,12 @@ public:
auto operator=(packet &&pkt) noexcept -> packet &;
[[nodiscard]] auto operator[](std::size_t index) -> char & {
[[nodiscard]] auto operator[](std::size_t index) -> unsigned char & {
return buffer_[index];
}
[[nodiscard]] auto operator[](std::size_t index) const -> const char & {
[[nodiscard]] auto operator[](std::size_t index) const -> const
unsigned char & {
return buffer_.at(index);
}
};

View File

@ -1243,8 +1243,9 @@ public:
remote::file_handle handle{};
DECODE_OR_RETURN(request, handle);
ret = this->fuse_write(path.data(), buffer.data(), write_size,
write_offset, handle);
ret = this->fuse_write(
path.data(), reinterpret_cast<const char *>(buffer.data()),
write_size, write_offset, handle);
}
return ret;
}});
@ -1268,7 +1269,7 @@ public:
data_buffer buffer(write_size);
if ((ret = request->decode(buffer.data(), buffer.size())) == 0) {
buffer = macaron::Base64::Decode(
std::string(buffer.begin(), buffer.end()));
std::string{buffer.begin(), buffer.end()});
write_size = buffer.size();
remote::file_offset write_offset{};
@ -1277,8 +1278,9 @@ public:
remote::file_handle handle{};
DECODE_OR_RETURN(request, handle);
ret = this->fuse_write(path.data(), buffer.data(), write_size,
write_offset, handle);
ret = this->fuse_write(path.data(),
reinterpret_cast<char *>(buffer.data()),
write_size, write_offset, handle);
}
return ret;
}});

View File

@ -274,7 +274,7 @@ struct s3_config {
std::string bucket;
std::uint16_t cache_timeout_secs{60U};
std::string encryption_token;
std::string region = "any";
std::string region{"any"};
std::string secret_key;
std::uint32_t timeout_ms{60000U};
std::string url;
@ -282,7 +282,12 @@ struct s3_config {
bool use_region_in_url{false};
};
using data_buffer = std::vector<char>;
using data_buffer = std::vector<unsigned char>;
using key_type = std::array<unsigned char, 32U>;
using mutex_lock = std::lock_guard<std::mutex>;
using recur_mutex_lock = std::lock_guard<std::recursive_mutex>;
using unique_mutex_lock = std::unique_lock<std::mutex>;
using unique_recur_mutex_lock = std::unique_lock<std::recursive_mutex>;
using api_file_list = std::vector<api_file>;
using api_file_provider_callback = std::function<void(api_file &)>;
@ -292,11 +297,7 @@ using http_headers = std::unordered_map<std::string, std::string>;
using http_parameters = std::unordered_map<std::string, std::string>;
using http_ranges = std::vector<http_range>;
using meta_provider_callback = std::function<void(directory_item &)>;
using mutex_lock = std::lock_guard<std::mutex>;
using query_parameters = std::map<std::string, std::string>;
using recur_mutex_lock = std::lock_guard<std::recursive_mutex>;
using unique_mutex_lock = std::unique_lock<std::mutex>;
using unique_recur_mutex_lock = std::unique_lock<std::recursive_mutex>;
} // namespace repertory
#endif // INCLUDE_TYPES_REPERTORY_HPP_

View File

@ -38,43 +38,45 @@
#pragma GCC diagnostic ignored "-Wuseless-cast"
#endif
#include <array>
#include <string>
#include <vector>
namespace macaron::Base64 {
static std::string Encode(const char *data, const size_t &len) {
static constexpr char sEncodingTable[] = {
static std::string Encode(const unsigned char *data, std::size_t len) {
static constexpr std::array<unsigned char, 64U> 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', '+', '/'};
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',
};
size_t in_len = len;
auto in_len{len};
std::string ret;
if (in_len > 0) {
size_t out_len = 4 * ((in_len + 2) / 3);
std::size_t out_len{4U * ((in_len + 2U) / 3U)};
ret = std::string(out_len, '\0');
size_t i;
char *p = const_cast<char *>(ret.c_str());
std::size_t i;
auto *p = reinterpret_cast<unsigned char *>(ret.data());
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)];
for (i = 0U; i < in_len - 2U; i += 3U) {
*p++ = sEncodingTable[(data[i] >> 2U) & 0x3F];
*p++ = sEncodingTable[((data[i] & 0x3) << 4U) |
((int)(data[i + 1U] & 0xF0) >> 4U)];
*p++ = sEncodingTable[((data[i + 1] & 0xF) << 2) |
((int)(data[i + 2] & 0xC0) >> 6)];
*p++ = sEncodingTable[data[i + 2] & 0x3F];
((int)(data[i + 2U] & 0xC0) >> 6U)];
*p++ = sEncodingTable[data[i + 2U] & 0x3F];
}
if (i < in_len) {
*p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
if (i == (in_len - 1)) {
*p++ = sEncodingTable[((data[i] & 0x3) << 4)];
*p++ = sEncodingTable[(data[i] >> 2U) & 0x3F];
if (i == (in_len - 1U)) {
*p++ = sEncodingTable[((data[i] & 0x3) << 4U)];
*p++ = '=';
} else {
*p++ = sEncodingTable[((data[i] & 0x3) << 4) |
((int)(data[i + 1] & 0xF0) >> 4)];
*p++ = sEncodingTable[((data[i + 1] & 0xF) << 2)];
*p++ = sEncodingTable[((data[i] & 0x3) << 4U) |
((int)(data[i + 1U] & 0xF0) >> 4U)];
*p++ = sEncodingTable[((data[i + 1U] & 0xF) << 2U)];
}
*p++ = '=';
}
@ -83,12 +85,14 @@ static std::string Encode(const char *data, const size_t &len) {
return ret;
}
[[maybe_unused]] static std::string Encode(const std::string &data) {
return Encode(&data[0], data.size());
[[maybe_unused]] static std::string Encode(std::string_view data) {
return Encode(reinterpret_cast<const unsigned char *>(data.data()),
data.size());
}
[[maybe_unused]] static std::vector<char> Decode(const std::string &input) {
static constexpr unsigned char kDecodingTable[] = {
[[maybe_unused]] static std::vector<unsigned char>
Decode(std::string_view input) {
static constexpr std::array<unsigned char, 256> 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,
@ -103,45 +107,49 @@ static std::string Encode(const char *data, const size_t &len) {
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 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;
std::vector<unsigned char> out;
if (not input.empty()) {
size_t in_len = input.size();
if (in_len % 4 != 0)
auto in_len{input.size()};
if (in_len % 4U != 0U) {
throw std::runtime_error("Input data size is not a multiple of 4");
}
size_t out_len = in_len / 4 * 3;
if (input[in_len - 1] == '=')
std::size_t out_len{in_len / 4U * 3U};
if (input[in_len - 1U] == '=') {
out_len--;
if (input[in_len - 2] == '=')
}
if (input[in_len - 2U] == '=') {
out_len--;
}
out.resize(out_len);
for (size_t i = 0, j = 0; i < in_len;) {
uint32_t a = input[i] == '='
? 0 & i++
: kDecodingTable[static_cast<int>(input[i++])];
uint32_t b = input[i] == '='
? 0 & i++
: kDecodingTable[static_cast<int>(input[i++])];
uint32_t c = input[i] == '='
? 0 & i++
: kDecodingTable[static_cast<int>(input[i++])];
uint32_t d = input[i] == '='
? 0 & i++
: kDecodingTable[static_cast<int>(input[i++])];
for (std::size_t i = 0U, j = 0U; i < in_len;) {
std::uint32_t a = input[i] == '='
? 0U & i++
: kDecodingTable[static_cast<int>(input[i++])];
std::uint32_t b = input[i] == '='
? 0U & i++
: kDecodingTable[static_cast<int>(input[i++])];
std::uint32_t c = input[i] == '='
? 0U & i++
: kDecodingTable[static_cast<int>(input[i++])];
std::uint32_t d = input[i] == '='
? 0U & i++
: kDecodingTable[static_cast<int>(input[i++])];
uint32_t triple =
(a << 3 * 6) + (b << 2 * 6) + (c << 1 * 6) + (d << 0 * 6);
std::uint32_t triple =
(a << 3U * 6U) + (b << 2U * 6U) + (c << 1U * 6U) + (d << 0U * 6U);
if (j < out_len)
out[j++] = (triple >> 2 * 8) & 0xFF;
out[j++] = (triple >> 2U * 8U) & 0xFF;
if (j < out_len)
out[j++] = (triple >> 1 * 8) & 0xFF;
out[j++] = (triple >> 1U * 8U) & 0xFF;
if (j < out_len)
out[j++] = (triple >> 0 * 8) & 0xFF;
out[j++] = (triple >> 0U * 8U) & 0xFF;
}
}

View File

@ -26,23 +26,20 @@
#include "utils/file_utils.hpp"
namespace repertory::utils::encryption {
using key_type = std::array<unsigned char, 32U>;
class encrypting_reader final {
public:
encrypting_reader(const std::string &file_name,
const std::string &source_path, stop_type &stop_requested,
const std::string &token,
std::optional<std::string> relative_parent_path,
encrypting_reader(std::string_view file_name, std::string_view source_path,
stop_type &stop_requested, std::string_view token,
std::optional<std::string_view> relative_parent_path,
std::size_t error_return = 0U);
encrypting_reader(const std::string &encrypted_file_path,
const std::string &source_path, stop_type &stop_requested,
const std::string &token, std::size_t error_return = 0U);
encrypting_reader(std::string_view encrypted_file_path,
std::string_view source_path, stop_type &stop_requested,
std::string_view token, std::size_t error_return = 0U);
encrypting_reader(
const std::string &encrypted_file_path, const std::string &source_path,
stop_type &stop_requested, const std::string &token,
std::string_view encrypted_file_path, std::string_view source_path,
stop_type &stop_requested, std::string_view token,
std::vector<std::array<unsigned char,
crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>>
iv_list,
@ -61,9 +58,9 @@ public:
using streambuf = std::basic_streambuf<char, std::char_traits<char>>;
private:
const key_type key_;
key_type key_;
stop_type &stop_requested_;
const size_t error_return_;
size_t error_return_;
std::unordered_map<std::size_t, data_buffer> chunk_buffers_;
std::string encrypted_file_name_;
std::string encrypted_file_path_;
@ -85,16 +82,16 @@ private:
auto reader_function(char *buffer, size_t size, size_t nitems) -> size_t;
public:
[[nodiscard]] static auto calculate_decrypted_size(std::uint64_t total_size)
-> std::uint64_t;
[[nodiscard]] static auto
calculate_decrypted_size(std::uint64_t total_size) -> std::uint64_t;
[[nodiscard]] static auto
calculate_encrypted_size(const std::string &source_path) -> std::uint64_t;
calculate_encrypted_size(std::string_view source_path) -> std::uint64_t;
[[nodiscard]] auto create_iostream() const -> std::shared_ptr<iostream>;
[[nodiscard]] static constexpr auto get_encrypted_chunk_size()
-> std::size_t {
[[nodiscard]] static constexpr auto
get_encrypted_chunk_size() -> std::size_t {
return encrypted_chunk_size_;
}
@ -118,8 +115,9 @@ public:
return header_size_;
}
[[nodiscard]] auto get_iv_list() -> std::vector<
std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>> {
[[nodiscard]] auto get_iv_list()
-> std::vector<std::array<unsigned char,
crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>> {
return iv_list_;
}
@ -132,8 +130,8 @@ public:
}
[[nodiscard]] static auto reader_function(char *buffer, size_t size,
size_t nitems, void *instream)
-> size_t {
size_t nitems,
void *instream) -> size_t {
return reinterpret_cast<encrypting_reader *>(instream)->reader_function(
buffer, size, nitems);
}

View File

@ -30,25 +30,24 @@ using reader_func = std::function<api_error(data_buffer &cypher_text,
std::uint64_t start_offset,
std::uint64_t end_offset)>;
// Prototypes
[[nodiscard]] auto decrypt_file_path(const std::string &encryption_token,
[[nodiscard]] auto decrypt_file_path(std::string_view encryption_token,
std::string &file_path) -> api_error;
[[nodiscard]] auto decrypt_file_name(const std::string &encryption_token,
[[nodiscard]] auto decrypt_file_name(std::string_view encryption_token,
std::string &file_name) -> api_error;
[[nodiscard]] auto generate_key(const std::string &encryption_token)
-> key_type;
[[nodiscard]] auto read_encrypted_range(const http_range &range,
const key_type &key, reader_func reader,
std::uint64_t total_size,
data_buffer &data) -> api_error;
[[nodiscard]] auto generate_key(std::string_view encryption_token) -> key_type;
// Implementations
template <typename result>
[[nodiscard]] inline auto decrypt_data(const key_type &key, const char *buffer,
std::size_t buffer_size, result &res)
-> bool {
[[nodiscard]] inline auto
decrypt_data(const key_type &key, const unsigned 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) {
@ -56,14 +55,11 @@ template <typename result>
boost::endian::native_to_big(static_cast<std::uint32_t>(buffer_size));
res.resize(buffer_size - header_size);
return crypto_aead_xchacha20poly1305_ietf_decrypt_detached(
reinterpret_cast<unsigned char *>(&res[0u]), nullptr,
reinterpret_cast<const unsigned char *>(&buffer[header_size]),
res.size(),
reinterpret_cast<const unsigned char *>(
&buffer[crypto_aead_xchacha20poly1305_IETF_NPUBBYTES]),
reinterpret_cast<unsigned char *>(res.data()), nullptr,
&buffer[header_size], res.size(),
&buffer[crypto_aead_xchacha20poly1305_IETF_NPUBBYTES],
reinterpret_cast<const unsigned char *>(&size), sizeof(size),
reinterpret_cast<const unsigned char *>(buffer),
key.data()) == 0;
buffer, key.data()) == 0;
}
return false;
@ -72,20 +68,19 @@ template <typename result>
template <typename buffer, typename result>
[[nodiscard]] inline auto decrypt_data(const key_type &key, const buffer &buf,
result &res) -> bool {
return decrypt_data<result>(key, &buf[0u], buf.size(), res);
return decrypt_data<result>(key, buf.data(), buf.size(), res);
}
template <typename buffer, typename result>
[[nodiscard]] inline auto decrypt_data(const std::string &encryption_token,
[[nodiscard]] inline auto decrypt_data(std::string_view encryption_token,
const buffer &buf, result &res) -> bool {
return decrypt_data<buffer, result>(generate_key(encryption_token), buf, res);
}
template <typename result>
[[nodiscard]] inline auto decrypt_data(const std::string &encryption_token,
const char *buffer,
std::size_t buffer_size, result &res)
-> bool {
[[nodiscard]] inline auto
decrypt_data(std::string_view encryption_token, const unsigned char *buffer,
std::size_t buffer_size, result &res) -> bool {
return decrypt_data<result>(generate_key(encryption_token), buffer,
buffer_size, res);
}
@ -94,8 +89,8 @@ template <typename result>
inline void
encrypt_data(const std::array<unsigned char,
crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> &iv,
const key_type &key, const char *buffer, std::size_t buffer_size,
result &res) {
const key_type &key, const unsigned char *buffer,
std::size_t buffer_size, result &res) {
std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_ABYTES> mac{};
const auto header_size =
@ -109,18 +104,18 @@ encrypt_data(const std::array<unsigned char,
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) {
&mac_length, 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());
std::memcpy(res.data(), iv.data(), iv.size());
std::memcpy(&res[iv.size()], mac.data(), mac.size());
}
template <typename result>
inline void encrypt_data(const key_type &key, const char *buffer,
inline void encrypt_data(const key_type &key, const unsigned 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());
@ -129,23 +124,23 @@ inline void encrypt_data(const key_type &key, const char *buffer,
}
template <typename result>
inline void encrypt_data(const std::string &encryption_token,
const char *buffer, std::size_t buffer_size,
inline void encrypt_data(std::string_view encryption_token,
const unsigned char *buffer, std::size_t buffer_size,
result &res) {
encrypt_data<result>(generate_key(encryption_token), buffer, buffer_size,
res);
}
template <typename buffer, typename result>
inline void encrypt_data(const std::string &encryption_token, const buffer &buf,
inline void encrypt_data(std::string_view encryption_token, const buffer &buf,
result &res) {
encrypt_data<result>(generate_key(encryption_token), &buf[0u], buf.size(),
encrypt_data<result>(generate_key(encryption_token), buf.data(), buf.size(),
res);
}
template <typename buffer, typename result>
inline void encrypt_data(const key_type &key, const buffer &buf, result &res) {
encrypt_data<result>(key, &buf[0u], buf.size(), res);
encrypt_data<result>(key, buf.data(), buf.size(), res);
}
template <typename buffer, typename result>
@ -153,7 +148,7 @@ 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);
encrypt_data<result>(iv, key, buf.data(), buf.size(), res);
}
} // namespace repertory::utils::encryption

View File

@ -27,21 +27,21 @@
namespace repertory::utils::file {
// Prototypes
[[nodiscard]] auto calculate_used_space(std::string path, bool recursive)
-> std::uint64_t;
[[nodiscard]] auto calculate_used_space(std::string path,
bool recursive) -> std::uint64_t;
void change_to_process_directory();
[[nodiscard]] auto copy_directory_recursively(std::string from_path,
std::string to_path) -> bool;
[[nodiscard]] auto copy_file(std::string from_path, std::string to_path)
-> bool;
[[nodiscard]] auto copy_file(std::string from_path,
std::string to_path) -> bool;
[[nodiscard]] auto create_full_directory_path(std::string path) -> bool;
[[nodiscard]] auto delete_directory(std::string path, bool recursive = false)
-> bool;
[[nodiscard]] auto delete_directory(std::string path,
bool recursive = false) -> bool;
[[nodiscard]] auto delete_directory_recursively(std::string path) -> bool;
@ -52,18 +52,18 @@ void change_to_process_directory();
[[nodiscard]] auto get_accessed_time(const std::string &path,
std::uint64_t &accessed) -> bool;
[[nodiscard]] auto get_directory_files(std::string path, bool oldest_first,
bool recursive = false)
-> std::deque<std::string>;
[[nodiscard]] auto
get_directory_files(std::string path, bool oldest_first,
bool recursive = false) -> std::deque<std::string>;
[[nodiscard]] auto get_free_drive_space(const std::string &path)
-> std::uint64_t;
[[nodiscard]] auto
get_free_drive_space(const std::string &path) -> std::uint64_t;
[[nodiscard]] auto get_total_drive_space(const std::string &path)
-> std::uint64_t;
[[nodiscard]] auto
get_total_drive_space(const std::string &path) -> std::uint64_t;
[[nodiscard]] auto get_file_size(std::string path, std::uint64_t &file_size)
-> bool;
[[nodiscard]] auto get_file_size(std::string_view path,
std::uint64_t &file_size) -> bool;
[[nodiscard]] auto get_modified_time(const std::string &path,
std::uint64_t &modified) -> bool;
@ -72,14 +72,14 @@ void change_to_process_directory();
[[nodiscard]] auto is_file(const std::string &path) -> bool;
[[nodiscard]] auto is_modified_date_older_than(const std::string &path,
const std::chrono::hours &hours)
-> bool;
[[nodiscard]] auto
is_modified_date_older_than(const std::string &path,
const std::chrono::hours &hours) -> bool;
[[nodiscard]] auto move_file(std::string from, std::string to) -> bool;
[[nodiscard]] auto read_file_lines(const std::string &path)
-> std::vector<std::string>;
[[nodiscard]] auto
read_file_lines(const std::string &path) -> std::vector<std::string>;
[[nodiscard]] auto read_json_file(const std::string &path, json &data) -> bool;
@ -89,8 +89,8 @@ void change_to_process_directory();
[[nodiscard]] auto retry_delete_file(const std::string &file) -> bool;
[[nodiscard]] auto write_json_file(const std::string &path, const json &j)
-> bool;
[[nodiscard]] auto write_json_file(const std::string &path,
const json &j) -> bool;
} // namespace repertory::utils::file
#endif // INCLUDE_UTILS_FILE_UTILS_HPP_

View File

@ -42,17 +42,17 @@ public:
[[nodiscard]] static auto
clone(const native_file_ptr &ptr) -> native_file_ptr;
[[nodiscard]] static auto create_or_open(const std::string &source_path,
[[nodiscard]] static auto create_or_open(std::string_view source_path,
bool read_only,
native_file_ptr &ptr) -> api_error;
[[nodiscard]] static auto create_or_open(const std::string &source_path,
[[nodiscard]] static auto create_or_open(std::string_view source_path,
native_file_ptr &ptr) -> api_error;
[[nodiscard]] static auto open(const std::string &source_path,
[[nodiscard]] static auto open(std::string_view source_path,
native_file_ptr &ptr) -> api_error;
[[nodiscard]] static auto open(const std::string &source_path, bool read_only,
[[nodiscard]] static auto open(std::string_view source_path, bool read_only,
native_file_ptr &ptr) -> api_error;
private:
@ -86,11 +86,11 @@ public:
[[nodiscard]] auto get_handle() -> native_handle;
#if defined(_WIN32)
[[nodiscard]] auto read_bytes(char *buffer, std::size_t read_size,
[[nodiscard]] auto read_bytes(unsigned char *buffer, std::size_t read_size,
std::uint64_t read_offset,
std::size_t &bytes_read) -> bool;
#else
[[nodiscard]] auto read_bytes(char *buffer, std::size_t read_size,
[[nodiscard]] auto read_bytes(unsigned char *buffer, std::size_t read_size,
std::uint64_t read_offset,
std::size_t &bytes_read) -> bool;
#endif
@ -99,11 +99,13 @@ public:
[[nodiscard]] auto truncate(std::uint64_t file_size) -> bool;
#if defined(_WIN32)
[[nodiscard]] auto write_bytes(const char *buffer, std::size_t write_size,
[[nodiscard]] auto write_bytes(const unsigned char *buffer,
std::size_t write_size,
std::uint64_t write_offset,
std::size_t &bytes_written) -> bool;
#else
[[nodiscard]] auto write_bytes(const char *buffer, std::size_t write_size,
[[nodiscard]] auto write_bytes(const unsigned char *buffer,
std::size_t write_size,
std::uint64_t write_offset,
std::size_t &bytes_written) -> bool;
#endif

View File

@ -32,29 +32,28 @@ constexpr auto contains(std::string_view str, std::string_view search) -> bool {
return (str.find(search) != std::string_view::npos);
}
[[nodiscard]] /* constexpr c++20 */ auto ends_with(std::string_view str,
std::string_view val)
-> bool;
[[nodiscard]] /* constexpr c++20 */ auto
ends_with(std::string_view str, std::string_view val) -> bool;
[[nodiscard]] auto from_bool(bool val) -> std::string;
[[nodiscard]] auto from_dynamic_bitset(const boost::dynamic_bitset<> &bitset)
-> std::string;
[[nodiscard]] auto
from_dynamic_bitset(const boost::dynamic_bitset<> &bitset) -> std::string;
[[nodiscard]] auto from_utf8(const std::string &str) -> std::wstring;
[[nodiscard]] auto from_utf8(std::string_view str) -> std::wstring;
[[nodiscard]] /* constexpr c++20 */ auto is_numeric(std::string_view str)
-> bool;
[[nodiscard]] /* constexpr c++20 */ auto
is_numeric(std::string_view str) -> bool;
[[nodiscard]] auto join(const std::vector<std::string> &arr, const char &delim)
-> std::string;
[[nodiscard]] auto join(const std::vector<std::string> &arr,
const char &delim) -> std::string;
auto left_trim(std::string &str) -> std::string &;
auto left_trim(std::string &str, const char &trim_ch) -> std::string &;
auto replace(std::string &src, const char &character, const char &with)
-> std::string &;
auto replace(std::string &src, const char &character,
const char &with) -> std::string &;
auto replace(std::string &src, const std::string &find, const std::string &with,
size_t start_pos = 0) -> std::string &;
@ -63,8 +62,8 @@ auto replace(std::string &src, const std::string &find, const std::string &with,
const char &with) -> std::string;
[[nodiscard]] auto replace_copy(std::string src, const std::string &find,
const std::string &with, size_t start_pos = 0)
-> std::string;
const std::string &with,
size_t start_pos = 0) -> std::string;
auto right_trim(std::string &str) -> std::string &;
@ -77,8 +76,8 @@ auto right_trim(std::string &str, const char &trim_ch) -> std::string &;
[[nodiscard]] auto to_double(const std::string &str) -> double;
[[nodiscard]] auto to_dynamic_bitset(const std::string &val)
-> boost::dynamic_bitset<>;
[[nodiscard]] auto
to_dynamic_bitset(const std::string &val) -> boost::dynamic_bitset<>;
[[nodiscard]] auto to_lower(std::string str) -> std::string;
@ -98,9 +97,9 @@ auto right_trim(std::string &str, const char &trim_ch) -> std::string &;
[[nodiscard]] auto to_upper(std::string str) -> std::string;
[[nodiscard]] auto to_utf8(std::string str) -> std::string;
[[nodiscard]] auto to_utf8(std::string_view str) -> std::string;
[[nodiscard]] auto to_utf8(const std::wstring &str) -> std::string;
[[nodiscard]] auto to_utf8(std::wstring_view str) -> std::string;
auto trim(std::string &str) -> std::string &;
@ -108,8 +107,8 @@ auto trim(std::string &str, const char &trim_ch) -> std::string &;
[[nodiscard]] auto trim_copy(std::string str) -> std::string;
[[nodiscard]] auto trim_copy(std::string str, const char &trim_ch)
-> std::string;
[[nodiscard]] auto trim_copy(std::string str,
const char &trim_ch) -> std::string;
} // namespace repertory::utils::string
#endif // INCLUDE_UTILS_STRING_UTILS_HPP_

View File

@ -36,9 +36,9 @@ void packet::clear() {
}
auto packet::decode(std::string &data) -> packet::error_type {
const auto *str = &buffer_[decode_offset_];
const auto *str = reinterpret_cast<const char *>(&buffer_[decode_offset_]);
const auto length = strnlen(str, buffer_.size() - decode_offset_);
data = std::string(str, length);
data = std::string{str, length};
decode_offset_ += (length + 1);
return utils::from_api_error(api_error::success);
@ -236,7 +236,7 @@ auto packet::decode_json(packet &response, json &json_data) -> int {
return ret;
}
auto packet::decrypt(const std::string &token) -> packet::error_type {
auto packet::decrypt(std::string_view token) -> packet::error_type {
constexpr const auto *function_name = static_cast<const char *>(__FUNCTION__);
auto ret = utils::from_api_error(api_error::success);
@ -267,10 +267,10 @@ void packet::encode(const void *buffer, std::size_t size, bool should_reserve) {
}
}
void packet::encode(const std::string &str) {
const auto len = strnlen(str.c_str(), str.size());
void packet::encode(std::string_view str) {
const auto len = str.size();
buffer_.reserve(len + 1 + buffer_.size());
encode(str.c_str(), len, false);
encode(str.data(), len, false);
buffer_.emplace_back(0);
}
@ -278,7 +278,7 @@ void packet::encode(const wchar_t *str) {
encode(utils::string::to_utf8(str == nullptr ? L"" : str));
}
void packet::encode(const std::wstring &str) {
void packet::encode(std::wstring_view str) {
encode(utils::string::to_utf8(str));
}
@ -400,14 +400,14 @@ void packet::encode_top(const void *buffer, std::size_t size,
}
}
void packet::encode_top(const std::string &str) {
const auto len = strnlen(str.c_str(), str.size());
void packet::encode_top(std::string_view str) {
const auto len = str.size();
buffer_.reserve(len + 1U + buffer_.size());
encode_top(str.c_str(), len, false);
encode_top(str.data(), len, false);
buffer_.insert(buffer_.begin() + static_cast<std::int32_t>(len), 0);
}
void packet::encode_top(const std::wstring &str) {
void packet::encode_top(std::wstring_view str) {
encode_top(utils::string::to_utf8(str));
}
@ -518,7 +518,7 @@ void packet::encode_top(remote::file_info val) {
encode_top(&val, sizeof(val), true);
}
void packet::encrypt(const std::string &token) {
void packet::encrypt(std::string_view token) {
constexpr const auto *function_name = static_cast<const char *>(__FUNCTION__);
try {

View File

@ -1018,8 +1018,10 @@ auto fuse_drive::setxattr_impl(std::string api_path, const char *name,
}
}
return provider_.set_item_meta(api_path, attribute_name,
macaron::Base64::Encode(value, size));
return provider_.set_item_meta(
api_path, attribute_name,
macaron::Base64::Encode(reinterpret_cast<const unsigned char *>(value),
size));
}
#endif // HAS_SETXATTR

View File

@ -691,7 +691,9 @@ auto encrypt_provider::process_directory_entry(
part_idx++) {
data_buffer encrypted_data;
utils::encryption::encrypt_data(
cfg.encryption_token, encrypted_parts.at(part_idx).c_str(),
cfg.encryption_token,
reinterpret_cast<const unsigned char *>(
encrypted_parts.at(part_idx).c_str()),
strnlen(encrypted_parts.at(part_idx).c_str(),
encrypted_parts.at(part_idx).size()),
encrypted_data);
@ -917,8 +919,9 @@ auto encrypt_provider::read_file_bytes(const std::string &api_path,
info->reader->set_read_position(offset);
data.resize(size);
const auto res = info->reader->reader_function(data.data(), 1U, data.size(),
info->reader.get());
const auto res =
info->reader->reader_function(reinterpret_cast<char *>(data.data()), 1U,
data.size(), info->reader.get());
if (res == 0) {
return api_error::os_error;
}

View File

@ -33,8 +33,8 @@ class encrypting_streambuf final : public encrypting_reader::streambuf {
public:
encrypting_streambuf(const encrypting_streambuf &) = default;
encrypting_streambuf(encrypting_streambuf &&) = delete;
auto operator=(const encrypting_streambuf &)
-> encrypting_streambuf & = delete;
auto
operator=(const encrypting_streambuf &) -> encrypting_streambuf & = delete;
auto operator=(encrypting_streambuf &&) -> encrypting_streambuf & = delete;
explicit encrypting_streambuf(const encrypting_reader &reader)
@ -81,10 +81,9 @@ protected:
return encrypting_reader::streambuf::seekoff(off, dir, which);
}
auto seekpos(pos_type pos,
std::ios_base::openmode which = std::ios_base::out |
std::ios_base::in)
-> pos_type override {
auto seekpos(pos_type pos, std::ios_base::openmode which =
std::ios_base::out |
std::ios_base::in) -> pos_type override {
return seekoff(pos, std::ios_base::beg, which);
}
@ -164,29 +163,30 @@ const std::size_t encrypting_reader::encrypted_chunk_size_ =
data_chunk_size_ + header_size_;
encrypting_reader::encrypting_reader(
const std::string &file_name, const std::string &source_path,
stop_type &stop_requested, const std::string &token,
std::optional<std::string> relative_parent_path, std::size_t error_return)
std::string_view file_name, std::string_view source_path,
stop_type &stop_requested, std::string_view token,
std::optional<std::string_view> relative_parent_path,
std::size_t error_return)
: key_(utils::encryption::generate_key(token)),
stop_requested_(stop_requested),
error_return_(error_return) {
const auto res = native_file::create_or_open(source_path, true, source_file_);
if (res != api_error::success) {
throw std::runtime_error("file open failed|src|" + source_path + '|' +
throw std::runtime_error("file open failed|src|" +
std::string{source_path} + '|' +
api_error_to_string(res));
}
data_buffer result;
utils::encryption::encrypt_data(key_, file_name.c_str(),
strnlen(file_name.c_str(), file_name.size()),
result);
utils::encryption::encrypt_data(
key_, reinterpret_cast<const unsigned char *>(file_name.data()),
file_name.size(), result);
encrypted_file_name_ = utils::to_hex_string(result);
if (relative_parent_path.has_value()) {
for (const auto &part :
std::filesystem::path(relative_parent_path.value())) {
for (auto &&part : std::filesystem::path(relative_parent_path.value())) {
utils::encryption::encrypt_data(
key_, part.string().c_str(),
key_, reinterpret_cast<const unsigned char *>(part.string().c_str()),
strnlen(part.string().c_str(), part.string().size()), result);
encrypted_file_path_ += '/' + utils::to_hex_string(result);
}
@ -195,7 +195,8 @@ encrypting_reader::encrypting_reader(
std::uint64_t file_size{};
if (not utils::file::get_file_size(source_path, file_size)) {
throw std::runtime_error("get file size failed|src|" + source_path + '|' +
throw std::runtime_error("get file size failed|src|" +
std::string{source_path} + '|' +
std::to_string(utils::get_last_error_code()));
}
@ -214,17 +215,18 @@ encrypting_reader::encrypting_reader(
}
}
encrypting_reader::encrypting_reader(const std::string &encrypted_file_path,
const std::string &source_path,
encrypting_reader::encrypting_reader(std::string_view encrypted_file_path,
std::string_view source_path,
stop_type &stop_requested,
const std::string &token,
std::string_view token,
std::size_t error_return)
: key_(utils::encryption::generate_key(token)),
stop_requested_(stop_requested),
error_return_(error_return) {
const auto res = native_file::create_or_open(source_path, true, source_file_);
if (res != api_error::success) {
throw std::runtime_error("file open failed|src|" + source_path + '|' +
throw std::runtime_error("file open failed|src|" +
std::string{source_path} + '|' +
api_error_to_string(res));
}
@ -234,7 +236,8 @@ encrypting_reader::encrypting_reader(const std::string &encrypted_file_path,
std::uint64_t file_size{};
if (not utils::file::get_file_size(source_path, file_size)) {
throw std::runtime_error("get file size failed|src|" + source_path + '|' +
throw std::runtime_error("get file size failed|src|" +
std::string{source_path} + '|' +
std::to_string(utils::get_last_error_code()));
}
@ -254,8 +257,8 @@ encrypting_reader::encrypting_reader(const std::string &encrypted_file_path,
}
encrypting_reader::encrypting_reader(
const std::string &encrypted_file_path, const std::string &source_path,
stop_type &stop_requested, const std::string &token,
std::string_view encrypted_file_path, std::string_view source_path,
stop_type &stop_requested, std::string_view token,
std::vector<
std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>>
iv_list,
@ -265,7 +268,8 @@ encrypting_reader::encrypting_reader(
error_return_(error_return) {
const auto res = native_file::create_or_open(source_path, true, source_file_);
if (res != api_error::success) {
throw std::runtime_error("file open failed|src|" + source_path + '|' +
throw std::runtime_error("file open failed|src|" +
std::string{source_path} + '|' +
api_error_to_string(res));
}
@ -275,7 +279,8 @@ encrypting_reader::encrypting_reader(
std::uint64_t file_size{};
if (not utils::file::get_file_size(source_path, file_size)) {
throw std::runtime_error("get file size failed|src|" + source_path + '|' +
throw std::runtime_error("get file size failed|src|" +
std::string{source_path} + '|' +
std::to_string(utils::get_last_error_code()));
}
@ -319,11 +324,12 @@ auto encrypting_reader::calculate_decrypted_size(std::uint64_t total_size)
get_header_size());
}
auto encrypting_reader::calculate_encrypted_size(const std::string &source_path)
auto encrypting_reader::calculate_encrypted_size(std::string_view source_path)
-> std::uint64_t {
std::uint64_t file_size{};
if (not utils::file::get_file_size(source_path, file_size)) {
throw std::runtime_error("get file size failed|src|" + source_path + '|' +
throw std::runtime_error("get file size failed|src|" +
std::string{source_path} + '|' +
std::to_string(utils::get_last_error_code()));
}

View File

@ -28,7 +28,7 @@
#include "utils/utils.hpp"
namespace repertory::utils::encryption {
auto decrypt_file_path(const std::string &encryption_token,
auto decrypt_file_path(std::string_view encryption_token,
std::string &file_path) -> api_error {
std::string decrypted_file_path{};
for (const auto &part : std::filesystem::path(file_path)) {
@ -49,7 +49,7 @@ auto decrypt_file_path(const std::string &encryption_token,
return api_error::success;
}
auto decrypt_file_name(const std::string &encryption_token,
auto decrypt_file_name(std::string_view encryption_token,
std::string &file_name) -> api_error {
data_buffer buffer;
if (not utils::from_hex_string(file_name, buffer)) {
@ -65,7 +65,7 @@ auto decrypt_file_name(const std::string &encryption_token,
return api_error::success;
}
auto generate_key(const std::string &encryption_token) -> key_type {
auto generate_key(std::string_view encryption_token) -> key_type {
crypto_hash_sha256_state state{};
auto res = crypto_hash_sha256_init(&state);
if (res != 0) {
@ -73,8 +73,8 @@ auto generate_key(const std::string &encryption_token) -> key_type {
std::to_string(res));
}
res = crypto_hash_sha256_update(
&state, reinterpret_cast<const unsigned char *>(encryption_token.c_str()),
strnlen(encryption_token.c_str(), encryption_token.size()));
&state, reinterpret_cast<const unsigned char *>(encryption_token.data()),
encryption_token.size());
if (res != 0) {
throw std::runtime_error("failed to update sha256|" + std::to_string(res));
}

View File

@ -82,19 +82,19 @@ void change_to_process_directory() {
#if defined(_WIN32)
std::string file_name;
file_name.resize(MAX_PATH);
::GetModuleFileNameA(nullptr, &file_name[0u],
::GetModuleFileNameA(nullptr, &file_name[0U],
static_cast<DWORD>(file_name.size()));
std::string path = file_name.c_str();
::PathRemoveFileSpecA(&path[0u]);
::SetCurrentDirectoryA(&path[0u]);
::PathRemoveFileSpecA(&path[0U]);
::SetCurrentDirectoryA(&path[0U]);
#else
std::string path;
path.resize(PATH_MAX + 1);
#if defined(__APPLE__)
proc_pidpath(getpid(), &path[0u], path.size());
proc_pidpath(getpid(), &path[0U], path.size());
#else
readlink("/proc/self/exe", &path[0u], path.size());
readlink("/proc/self/exe", &path[0U], path.size());
#endif
path = utils::path::get_parent_directory(path);
chdir(path.c_str());
@ -174,9 +174,9 @@ auto create_full_directory_path(std::string path) -> bool {
#else
auto ret = true;
const auto paths = utils::string::split(utils::path::absolute(path),
utils::path::directory_seperator[0u]);
utils::path::directory_seperator[0U]);
std::string current_path;
for (std::size_t i = 0u; ret && (i < paths.size()); i++) {
for (std::size_t i = 0U; ret && (i < paths.size()); i++) {
if (paths[i].empty()) { // Skip root
current_path = utils::path::directory_seperator;
} else {
@ -282,8 +282,8 @@ auto generate_sha256(const std::string &file_path) -> std::string {
{
data_buffer buffer(1048576u);
std::uint64_t read_offset = 0u;
std::size_t bytes_read = 0u;
std::uint64_t read_offset = 0U;
std::size_t bytes_read = 0U;
while (
nf->read_bytes(buffer.data(), buffer.size(), read_offset, bytes_read)) {
if (not bytes_read) {
@ -461,7 +461,7 @@ auto get_accessed_time(const std::string &path,
auto get_modified_time(const std::string &path,
std::uint64_t &modified) -> bool {
auto ret = false;
modified = 0u;
modified = 0U;
#if defined(_WIN32)
struct _stat64 st {};
if (_stat64(path.c_str(), &st) != -1) {
@ -483,20 +483,20 @@ auto get_modified_time(const std::string &path,
return ret;
}
auto get_file_size(std::string path, std::uint64_t &file_size) -> bool {
file_size = 0u;
path = utils::path::absolute(path);
auto get_file_size(std::string_view path, std::uint64_t &file_size) -> bool {
file_size = 0U;
auto abs_path = utils::path::absolute(std::string{path});
#if defined(_WIN32)
struct _stat64 st {};
if (_stat64(path.c_str(), &st) != 0) {
if (_stat64(abs_path.c_str(), &st) != 0) {
#else
#if defined(__APPLE__)
struct stat st {};
if (stat(path.c_str(), &st) != 0) {
if (stat(abs_path.c_str(), &st) != 0) {
#else
struct stat64 st {};
if (stat64(path.c_str(), &st) != 0) {
if (stat64(abs_path.c_str(), &st) != 0) {
#endif
#endif
return false;
@ -643,7 +643,7 @@ auto reset_modified_time(const std::string &path) -> bool {
auto retry_delete_directory(const std::string &dir) -> bool {
auto deleted = false;
for (std::uint8_t i = 0u; not(deleted = delete_directory(dir)) && (i < 200u);
for (std::uint8_t i = 0U; not(deleted = delete_directory(dir)) && (i < 200U);
i++) {
std::this_thread::sleep_for(10ms);
}
@ -653,7 +653,7 @@ auto retry_delete_directory(const std::string &dir) -> bool {
auto retry_delete_file(const std::string &file) -> bool {
auto deleted = false;
for (std::uint8_t i = 0u; not(deleted = delete_file(file)) && (i < 200u);
for (std::uint8_t i = 0U; not(deleted = delete_file(file)) && (i < 200U);
i++) {
std::this_thread::sleep_for(10ms);
}
@ -671,9 +671,10 @@ auto write_json_file(const std::string &path, const json &j) -> bool {
native_file_ptr nf;
auto ret = (native_file::create_or_open(path, nf) == api_error::success);
if (ret) {
std::size_t bytes_written = 0u;
std::size_t bytes_written{0U};
ret = nf->truncate(0) &&
nf->write_bytes(&data[0u], data.size(), 0u, bytes_written);
nf->write_bytes(reinterpret_cast<const unsigned char *>(data.data()),
data.size(), 0U, bytes_written);
nf->close();
}

View File

@ -64,24 +64,24 @@ auto native_file::clone(const native_file_ptr &ptr) -> native_file_ptr {
return clone;
}
auto native_file::create_or_open(const std::string &source_path, bool read_only,
auto native_file::create_or_open(std::string_view source_path, bool read_only,
native_file_ptr &ptr) -> api_error {
#if defined(_WIN32)
auto handle =
read_only
? ::CreateFileA(source_path.c_str(), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, nullptr)
: ::CreateFileA(source_path.c_str(), GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, nullptr);
read_only ? ::CreateFileA(std::string{source_path}.c_str(), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, nullptr)
: ::CreateFileA(std::string{source_path}.c_str(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, nullptr);
#else
auto handle =
read_only
? ::open(source_path.c_str(), O_CREAT | O_RDONLY | O_CLOEXEC, 0600U)
: ::open(source_path.c_str(), O_CREAT | O_RDWR | O_CLOEXEC, 0600U);
auto handle = read_only ? ::open(std::string{source_path}.c_str(),
O_CREAT | O_RDONLY | O_CLOEXEC, 0600U)
: ::open(std::string{source_path}.c_str(),
O_CREAT | O_RDWR | O_CLOEXEC, 0600U);
if (not read_only) {
chmod(source_path.c_str(), 0600U);
chmod(std::string{source_path}.c_str(), 0600U);
}
#endif
ptr = native_file::attach(handle);
@ -89,32 +89,34 @@ auto native_file::create_or_open(const std::string &source_path, bool read_only,
: api_error::success);
}
auto native_file::create_or_open(const std::string &source_path,
auto native_file::create_or_open(std::string_view source_path,
native_file_ptr &ptr) -> api_error {
return create_or_open(source_path, false, ptr);
}
auto native_file::open(const std::string &source_path,
auto native_file::open(std::string_view source_path,
native_file_ptr &ptr) -> api_error {
return open(source_path, false, ptr);
}
auto native_file::open(const std::string &source_path, bool read_only,
auto native_file::open(std::string_view source_path, bool read_only,
native_file_ptr &ptr) -> api_error {
#if defined(_WIN32)
auto handle =
read_only
? ::CreateFileA(source_path.c_str(), GENERIC_READ,
? ::CreateFileA(std::string{source_path}.c_str(), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, nullptr)
: ::CreateFileA(source_path.c_str(), GENERIC_READ | GENERIC_WRITE,
: ::CreateFileA(std::string{source_path}.c_str(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, nullptr);
#else
auto handle = read_only ? ::open(source_path.c_str(), O_RDONLY | O_CLOEXEC)
: ::open(source_path.c_str(), O_RDWR | O_CLOEXEC);
auto handle =
read_only ? ::open(std::string{source_path}.c_str(), O_RDONLY | O_CLOEXEC)
: ::open(std::string{source_path}.c_str(), O_RDWR | O_CLOEXEC);
if (not read_only) {
chmod(source_path.c_str(), 0600U);
chmod(std::string{source_path}.c_str(), 0600U);
}
#endif
ptr = native_file::attach(handle);
@ -216,7 +218,7 @@ auto native_file::get_file_size(std::uint64_t &file_size) -> bool {
}
#if defined(_WIN32)
auto native_file::read_bytes(char *buffer, std::size_t read_size,
auto native_file::read_bytes(unsigned char *buffer, std::size_t read_size,
std::uint64_t read_offset,
std::size_t &bytes_read) -> bool {
recur_mutex_lock l(read_write_mutex_);
@ -243,7 +245,7 @@ auto native_file::read_bytes(char *buffer, std::size_t read_size,
return ret;
}
#else
auto native_file::read_bytes(char *buffer, std::size_t read_size,
auto native_file::read_bytes(unsigned char *buffer, std::size_t read_size,
std::uint64_t read_offset,
std::size_t &bytes_read) -> bool {
bytes_read = 0U;
@ -272,7 +274,8 @@ auto native_file::truncate(std::uint64_t file_size) -> bool {
}
#if defined(_WIN32)
auto native_file::write_bytes(const char *buffer, std::size_t write_size,
auto native_file::write_bytes(const unsigned char *buffer,
std::size_t write_size,
std::uint64_t write_offset,
std::size_t &bytes_written) -> bool {
recur_mutex_lock l(read_write_mutex_);
@ -295,7 +298,8 @@ auto native_file::write_bytes(const char *buffer, std::size_t write_size,
return ret;
}
#else
auto native_file::write_bytes(const char *buffer, std::size_t write_size,
auto native_file::write_bytes(const unsigned char *buffer,
std::size_t write_size,
std::uint64_t write_offset,
std::size_t &bytes_written) -> bool {
bytes_written = 0U;

View File

@ -22,8 +22,8 @@
#include "utils/string_utils.hpp"
namespace repertory::utils::string {
/* constexpr c++20 */ auto ends_with(std::string_view str, std::string_view val)
-> bool {
/* constexpr c++20 */ auto ends_with(std::string_view str,
std::string_view val) -> bool {
if (val.size() > str.size()) {
return false;
}
@ -41,11 +41,11 @@ auto from_dynamic_bitset(const boost::dynamic_bitset<> &bitset) -> std::string {
return stream.str();
}
auto from_utf8(const std::string &str) -> std::wstring {
auto from_utf8(std::string_view str) -> std::wstring {
return str.empty()
? L""
: std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t>()
.from_bytes(str);
.from_bytes(std::string{str});
}
/* constexpr c++20 */ auto is_numeric(std::string_view str) -> bool {
@ -72,8 +72,8 @@ auto from_utf8(const std::string &str) -> std::wstring {
}) == str.end();
}
auto join(const std::vector<std::string> &arr, const char &delim)
-> std::string {
auto join(const std::vector<std::string> &arr,
const char &delim) -> std::string {
if (arr.empty()) {
return "";
}
@ -92,8 +92,8 @@ auto left_trim(std::string &str, const char &trim_ch) -> std::string & {
return str;
}
auto replace(std::string &src, const char &character, const char &with)
-> std::string & {
auto replace(std::string &src, const char &character,
const char &with) -> std::string & {
std::replace(src.begin(), src.end(), character, with);
return src;
}
@ -109,8 +109,8 @@ auto replace(std::string &src, const std::string &find, const std::string &with,
return src;
}
auto replace_copy(std::string src, const char &character, const char &with)
-> std::string {
auto replace_copy(std::string src, const char &character,
const char &with) -> std::string {
std::replace(src.begin(), src.end(), character, with);
return src;
}
@ -129,8 +129,8 @@ auto right_trim(std::string &str, const char &trim_ch) -> std::string & {
return str;
}
auto split(const std::string &str, const char &delim, bool should_trim)
-> std::vector<std::string> {
auto split(const std::string &str, const char &delim,
bool should_trim) -> std::vector<std::string> {
std::vector<std::string> ret;
std::stringstream stream(str);
std::string item;
@ -203,13 +203,13 @@ auto to_upper(std::string str) -> std::string {
return str;
}
auto to_utf8(std::string str) -> std::string { return str; }
auto to_utf8(std::string_view str) -> std::string { return std::string{str}; }
auto to_utf8(const std::wstring &str) -> std::string {
auto to_utf8(std::wstring_view str) -> std::string {
return str.empty()
? ""
: std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t>()
.to_bytes(str);
.to_bytes(std::wstring{str});
}
auto trim(std::string &str) -> std::string & {