Address compiler warnings #10
Some checks failed
BlockStorage/repertory_osx/pipeline/head This commit looks good
BlockStorage/repertory_linux_builds/pipeline/head There was a failure building this commit

This commit is contained in:
Scott E. Graves 2023-10-30 13:31:52 -05:00
parent e7413fb741
commit 639d14452b
10 changed files with 1129 additions and 631 deletions

View File

@ -1,38 +1,8 @@
--- ---
Checks: 'clang-*,misc-unused-*,llvm-header-guard,llvm-include-order,modernize-*' Checks: '-*,clang-diagnostic-*,clang-analyzer-*,bugprone-*,concurrency-*,cppcoreguidelines-*,modernize-*,readability-*,-readability-redundant-access-specifiers,-readability-function-cognitive-complexity'
WarningsAsErrors: '' WarningsAsErrors: ''
HeaderFilterRegex: '' HeaderFilterRegex: ''
AnalyzeTemporaryDtors: false AnalyzeTemporaryDtors: false
FormatStyle: none FormatStyle: none
User: sgraves User: sgraves
CheckOptions:
- key: cert-dcl16-c.NewSuffixes
value: 'L;LL;LU;LLU'
- key: cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField
value: '0'
- key: cppcoreguidelines-explicit-virtual-functions.IgnoreDestructors
value: '1'
- key: cppcoreguidelines-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic
value: '1'
- key: google-readability-braces-around-statements.ShortStatementLines
value: '1'
- key: google-readability-function-size.StatementThreshold
value: '800'
- key: google-readability-namespace-comments.ShortNamespaceLines
value: '10'
- key: google-readability-namespace-comments.SpacesBeforeComments
value: '2'
- key: modernize-loop-convert.MaxCopySize
value: '16'
- key: modernize-loop-convert.MinConfidence
value: reasonable
- key: modernize-loop-convert.NamingStyle
value: CamelCase
- key: modernize-pass-by-value.IncludeStyle
value: llvm
- key: modernize-replace-auto-ptr.IncludeStyle
value: llvm
- key: modernize-use-nullptr.NullMacros
value: 'NULL'
... ...

File diff suppressed because it is too large Load Diff

View File

@ -82,7 +82,6 @@
#include <optional> #include <optional>
#include <random> #include <random>
#include <sstream> #include <sstream>
#include <sstream>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <thread> #include <thread>
@ -90,6 +89,14 @@
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include <sodium.h>
template <typename data_type>
[[nodiscard]] constexpr auto repertory_rand() -> data_type {
data_type ret{};
randombytes_buf(&ret, sizeof(ret));
return ret;
}
#include <boost/archive/text_iarchive.hpp> #include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_oarchive.hpp>
#include <boost/asio.hpp> #include <boost/asio.hpp>
@ -116,7 +123,6 @@
#endif #endif
#include <pugixml.hpp> #include <pugixml.hpp>
#include <sodium.h>
#define CPPHTTPLIB_TCP_NODELAY true #define CPPHTTPLIB_TCP_NODELAY true
#define CPPHTTPLIB_OPENSSL_SUPPORT #define CPPHTTPLIB_OPENSSL_SUPPORT
@ -136,12 +142,12 @@ using json = nlohmann::json;
#define REPERTORY_API_INVALID_HANDLE static_cast<std::uint64_t>(-1) #define REPERTORY_API_INVALID_HANDLE static_cast<std::uint64_t>(-1)
using native_handle = HANDLE; using native_handle = HANDLE;
#else #else
#define REPERTORY_INVALID_HANDLE -1 #define REPERTORY_INVALID_HANDLE (-1)
#define REPERTORY_API_INVALID_HANDLE REPERTORY_INVALID_HANDLE #define REPERTORY_API_INVALID_HANDLE REPERTORY_INVALID_HANDLE
using native_handle = int; using native_handle = int;
#endif #endif
#define NANOS_PER_SECOND 1000000000L constexpr const auto NANOS_PER_SECOND = 1000000000L;
#ifdef _WIN32 #ifdef _WIN32
#ifdef CreateDirectory #ifdef CreateDirectory
@ -339,6 +345,7 @@ using WCHAR = wchar_t;
#define STATUS_OBJECT_NAME_NOT_FOUND std::int32_t(0xC0000034L) #define STATUS_OBJECT_NAME_NOT_FOUND std::int32_t(0xC0000034L)
#define STATUS_OBJECT_PATH_INVALID std::int32_t(0xC0000039L) #define STATUS_OBJECT_PATH_INVALID std::int32_t(0xC0000039L)
#define STATUS_UNEXPECTED_IO_ERROR std::int32_t(0xC00000E9L) #define STATUS_UNEXPECTED_IO_ERROR std::int32_t(0xC00000E9L)
#define CONVERT_STATUS_NOT_IMPLEMENTED(e) \ #define CONVERT_STATUS_NOT_IMPLEMENTED(e) \
((std::int32_t(e) == STATUS_NOT_IMPLEMENTED) ? -ENOTSUP : e) ((std::int32_t(e) == STATUS_NOT_IMPLEMENTED) ? -ENOTSUP : e)

View File

@ -23,7 +23,13 @@
#define INCLUDE_EVENTS_EVENT_HPP_ #define INCLUDE_EVENTS_EVENT_HPP_
namespace repertory { namespace repertory {
enum class event_level { error, warn, normal, debug, verbose }; enum class event_level {
error,
warn,
normal,
debug,
verbose,
};
auto event_level_from_string(std::string level) -> event_level; auto event_level_from_string(std::string level) -> event_level;
@ -37,10 +43,14 @@ protected:
: allow_async_(allow_async), ss_(ss.str()), j_(std::move(j)) {} : allow_async_(allow_async), ss_(ss.str()), j_(std::move(j)) {}
public: public:
event(const event &) = delete;
event(event &&) = delete;
auto operator=(const event &) -> event & = delete;
auto operator=(event &&) -> event & = delete;
virtual ~event() = default; virtual ~event() = default;
private: private:
const bool allow_async_; bool allow_async_;
protected: protected:
std::stringstream ss_; std::stringstream ss_;

View File

@ -22,8 +22,8 @@
#ifndef INCLUDE_TYPES_REMOTE_HPP_ #ifndef INCLUDE_TYPES_REMOTE_HPP_
#define INCLUDE_TYPES_REMOTE_HPP_ #define INCLUDE_TYPES_REMOTE_HPP_
#define PACKET_SERVICE_FUSE std::uint32_t(1) #define PACKET_SERVICE_FUSE 1U
#define PACKET_SERVICE_WINFSP std::uint32_t(2) #define PACKET_SERVICE_WINFSP 2U
#ifdef _WIN32 #ifdef _WIN32
#define PACKET_SERVICE_FLAGS PACKET_SERVICE_WINFSP #define PACKET_SERVICE_FLAGS PACKET_SERVICE_WINFSP
@ -44,48 +44,51 @@ using group_id = std::uint32_t;
using user_id = std::uint32_t; using user_id = std::uint32_t;
enum class open_flags : std::uint32_t { enum class open_flags : std::uint32_t {
read_only = 0u, read_only = 0U,
write_only = 1u, write_only = 1U,
read_write = 2u, read_write = 2U,
create = 4u, create = 4U,
excl = 8u, excl = 8U,
no_ctty = 16u, no_ctty = 16U,
truncate = 32u, truncate = 32U,
append = 64u, append = 64U,
non_blocking = 128u, non_blocking = 128U,
sync = 256u, sync = 256U,
async = 512u, async = 512U,
directory = 1024u, directory = 1024U,
no_follow = 2048u, no_follow = 2048U,
clo_exec = 4096u, clo_exec = 4096U,
direct = 8192u, direct = 8192U,
no_atime = 16384u, no_atime = 16384U,
path = 32768u, path = 32768U,
temp_file = 65536u, temp_file = 65536U,
dsync = 131072u, dsync = 131072U,
}; };
inline auto operator|(const open_flags &a, const open_flags &b) -> open_flags { inline auto operator|(const open_flags &flag_1, const open_flags &flag_2)
-> open_flags {
using t = std::underlying_type_t<open_flags>; using t = std::underlying_type_t<open_flags>;
return static_cast<open_flags>(static_cast<t>(a) | static_cast<t>(b)); return static_cast<open_flags>(static_cast<t>(flag_1) |
static_cast<t>(flag_2));
} }
#ifdef __GNUG__ #ifdef __GNUG__
__attribute__((unused)) __attribute__((unused))
#endif #endif
inline auto inline auto
operator|=(open_flags &a, const open_flags &b) -> open_flags & { operator|=(open_flags &flag_1, const open_flags &flag_2) -> open_flags & {
a = a | b; flag_1 = flag_1 | flag_2;
return a; return flag_1;
} }
#ifdef __GNUG__ #ifdef __GNUG__
__attribute__((unused)) __attribute__((unused))
#endif #endif
inline auto inline auto
operator&(const open_flags &a, const open_flags &b) -> open_flags { operator&(const open_flags &flag_1, const open_flags &flag_2) -> open_flags {
using t = std::underlying_type_t<open_flags>; using t = std::underlying_type_t<open_flags>;
return static_cast<open_flags>(static_cast<t>(a) & static_cast<t>(b)); return static_cast<open_flags>(static_cast<t>(flag_1) &
static_cast<t>(flag_2));
} }
#pragma pack(1) #pragma pack(1)

View File

@ -38,11 +38,13 @@ void calculate_allocation_size(bool directory, std::uint64_t file_size,
template <typename t> template <typename t>
[[nodiscard]] auto collection_excludes(t collection, [[nodiscard]] auto collection_excludes(t collection,
const typename t::value_type &v) -> bool; const typename t::value_type &val)
-> bool;
template <typename t> template <typename t>
[[nodiscard]] auto collection_includes(t collection, [[nodiscard]] auto collection_includes(t collection,
const typename t::value_type &v) -> bool; const typename t::value_type &val)
-> bool;
[[nodiscard]] auto compare_version_strings(std::string version1, [[nodiscard]] auto compare_version_strings(std::string version1,
std::string version2) -> int; std::string version2) -> int;
@ -53,7 +55,8 @@ template <typename t>
[[nodiscard]] auto create_uuid_string() -> std::string; [[nodiscard]] auto create_uuid_string() -> std::string;
[[nodiscard]] auto create_volume_label(const provider_type &pt) -> std::string; [[nodiscard]] auto create_volume_label(const provider_type &prov)
-> std::string;
template <typename t> template <typename t>
[[nodiscard]] auto divide_with_ceiling(const t &n, const t &d) -> t; [[nodiscard]] auto divide_with_ceiling(const t &n, const t &d) -> t;
@ -66,7 +69,7 @@ template <typename t>
-> std::string; -> std::string;
template <typename t> template <typename t>
[[nodiscard]] auto from_hex_string(const std::string &str, t &v) -> bool; [[nodiscard]] auto from_hex_string(const std::string &str, t &val) -> bool;
[[nodiscard]] auto generate_random_string(std::uint16_t length) -> std::string; [[nodiscard]] auto generate_random_string(std::uint16_t length) -> std::string;
@ -77,7 +80,7 @@ template <typename t>
[[nodiscard]] auto get_file_time_now() -> std::uint64_t; [[nodiscard]] auto get_file_time_now() -> std::uint64_t;
void get_local_time_now(struct tm &localTime); void get_local_time_now(struct tm &local_time);
[[nodiscard]] auto get_next_available_port(std::uint16_t first_port, [[nodiscard]] auto get_next_available_port(std::uint16_t first_port,
std::uint16_t &available_port) std::uint16_t &available_port)
@ -85,11 +88,12 @@ void get_local_time_now(struct tm &localTime);
[[nodiscard]] auto get_time_now() -> std::uint64_t; [[nodiscard]] auto get_time_now() -> std::uint64_t;
template <typename t> template <typename data_type>
[[nodiscard]] auto random_between(const t &begin, const t &end) -> t; [[nodiscard]] auto random_between(const data_type &begin, const data_type &end)
-> data_type;
template <typename t> template <typename t>
void remove_element_from(t &v, const typename t::value_type &value); void remove_element_from(t &collection, const typename t::value_type &val);
[[nodiscard]] auto reset_curl(CURL *curl_handle) -> CURL *; [[nodiscard]] auto reset_curl(CURL *curl_handle) -> CURL *;
@ -97,28 +101,30 @@ void remove_element_from(t &v, const typename t::value_type &value);
-> bool; -> bool;
void spin_wait_for_mutex(std::function<bool()> complete, void spin_wait_for_mutex(std::function<bool()> complete,
std::condition_variable &cv, std::mutex &mtx, std::condition_variable &cond, std::mutex &mtx,
const std::string &txt = ""); const std::string &text = "");
void spin_wait_for_mutex(bool &complete, std::condition_variable &cv, void spin_wait_for_mutex(bool &complete, std::condition_variable &cond,
std::mutex &mtx, const std::string &txt = ""); std::mutex &mtx, const std::string &text = "");
template <typename t> template <typename t>
[[nodiscard]] auto to_hex_string(const t &v) -> std::string; [[nodiscard]] auto to_hex_string(const t &val) -> std::string;
// template implementations // template implementations
template <typename t> template <typename t>
[[nodiscard]] auto collection_excludes(t collection, [[nodiscard]] auto collection_excludes(t collection,
const typename t::value_type &v) const typename t::value_type &val)
-> bool { -> bool {
return std::find(collection.begin(), collection.end(), v) == collection.end(); return std::find(collection.begin(), collection.end(), val) ==
collection.end();
} }
template <typename t> template <typename t>
[[nodiscard]] auto collection_includes(t collection, [[nodiscard]] auto collection_includes(t collection,
const typename t::value_type &v) const typename t::value_type &val)
-> bool { -> bool {
return std::find(collection.begin(), collection.end(), v) != collection.end(); return std::find(collection.begin(), collection.end(), val) !=
collection.end();
} }
template <typename t> template <typename t>
@ -127,12 +133,14 @@ template <typename t>
} }
template <typename t> template <typename t>
[[nodiscard]] auto from_hex_string(const std::string &str, t &v) -> bool { [[nodiscard]] auto from_hex_string(const std::string &str, t &val) -> bool {
v.clear(); static constexpr const auto base16 = 16;
if (not(str.length() % 2u)) {
for (std::size_t i = 0u; i < str.length(); i += 2u) { val.clear();
v.emplace_back(static_cast<typename t::value_type>( if (not(str.length() % 2U)) {
strtol(str.substr(i, 2u).c_str(), nullptr, 16))); for (std::size_t i = 0U; i < str.length(); i += 2U) {
val.emplace_back(static_cast<typename t::value_type>(
strtol(str.substr(i, 2U).c_str(), nullptr, base16)));
} }
return true; return true;
} }
@ -140,30 +148,32 @@ template <typename t>
return false; return false;
} }
template <typename t> template <typename data_type>
[[nodiscard]] auto random_between(const t &begin, const t &end) -> t { [[nodiscard]] auto random_between(const data_type &begin, const data_type &end)
srand(static_cast<unsigned int>(get_time_now())); -> data_type {
return begin + static_cast<t>(rand()) % ((end + 1) - begin); return begin + repertory_rand<data_type>() % ((end + 1) - begin);
} }
template <typename t> template <typename t>
void remove_element_from(t &v, const typename t::value_type &value) { void remove_element_from(t &collection, const typename t::value_type &value) {
v.erase(std::remove(v.begin(), v.end(), value), v.end()); collection.erase(std::remove(collection.begin(), collection.end(), value),
collection.end());
} }
template <typename t> template <typename t>
[[nodiscard]] auto to_hex_string(const t &value) -> std::string { [[nodiscard]] auto to_hex_string(const t &value) -> std::string {
std::string ret{}; std::string ret{};
std::array<char, 3> h{}; std::array<char, 3> tmp{};
for (const auto &num : value) { for (const auto &num : value) {
#ifdef _WIN32 #ifdef _WIN32
sprintf_s(h.data(), h.size() - 1U, "%x", static_cast<std::uint8_t>(num)); sprintf_s(h.data(), h.size() - 1U, "%x", static_cast<std::uint8_t>(num));
#else #else
sprintf(h.data(), "%x", static_cast<std::uint8_t>(num)); sprintf(tmp.data(), "%x", static_cast<std::uint8_t>(num));
#endif #endif
ret += (strlen(h.data()) == 1) ? std::string("0") + h.data() : h.data(); ret +=
(strlen(tmp.data()) == 1) ? std::string("0") + tmp.data() : tmp.data();
} }
return ret; return ret;

View File

@ -20,6 +20,7 @@
SOFTWARE. SOFTWARE.
*/ */
#include "types/repertory.hpp" #include "types/repertory.hpp"
#include "utils/utils.hpp"
namespace repertory { namespace repertory {
auto get_repertory_git_revision() -> const std::string & { auto get_repertory_git_revision() -> const std::string & {

View File

@ -601,11 +601,11 @@ auto remote_server::fuse_read(const char *path, char *buffer,
const remote::file_handle &handle) const remote::file_handle &handle)
-> packet::error_type { -> packet::error_type {
const auto file_path = construct_path(path); const auto file_path = construct_path(path);
auto &b = *reinterpret_cast<data_buffer *>(buffer); auto &data = *reinterpret_cast<data_buffer *>(buffer);
auto res = has_open_info(static_cast<native_handle>(handle), EBADF); auto res = has_open_info(static_cast<native_handle>(handle), EBADF);
if (res == 0) { if (res == 0) {
b.resize(read_size); data.resize(read_size);
res = pread64(static_cast<native_handle>(handle), &b[0], read_size, res = pread64(static_cast<native_handle>(handle), data.data(), read_size,
static_cast<off_t>(read_offset)); static_cast<off_t>(read_offset));
} }
@ -630,7 +630,7 @@ auto remote_server::fuse_rename(const char *from, const char *to)
auto remote_server::fuse_readdir(const char *path, auto remote_server::fuse_readdir(const char *path,
const remote::file_offset &offset, const remote::file_offset &offset,
const remote::file_handle &handle, const remote::file_handle &handle,
std::string &itemPath) -> packet::error_type { std::string &item_path) -> packet::error_type {
const auto file_path = construct_path(path); const auto file_path = construct_path(path);
auto res = 0; auto res = 0;
if (offset > std::numeric_limits<std::size_t>::max()) { if (offset > std::numeric_limits<std::size_t>::max()) {
@ -639,7 +639,7 @@ auto remote_server::fuse_readdir(const char *path,
} else { } else {
auto *iterator = reinterpret_cast<directory_iterator *>(handle); auto *iterator = reinterpret_cast<directory_iterator *>(handle);
if (iterator) { if (iterator) {
res = iterator->get(static_cast<std::size_t>(offset), itemPath); res = iterator->get(static_cast<std::size_t>(offset), item_path);
} else { } else {
res = -1; res = -1;
errno = EFAULT; errno = EFAULT;

View File

@ -26,160 +26,92 @@ namespace repertory::remote {
auto create_open_flags(std::uint32_t flags) -> open_flags { auto create_open_flags(std::uint32_t flags) -> open_flags {
open_flags ret{}; open_flags ret{};
{ {
const auto f = (flags & 3u); const auto val = (flags & 3U);
ret |= (f == 1u) ? open_flags::write_only ret |= (val == 1U) ? open_flags::write_only
: (f == 2u) ? open_flags::read_write : (val == 2U) ? open_flags::read_write
: open_flags::read_only; : open_flags::read_only;
}
if (flags & static_cast<std::uint32_t>(O_CREAT)) {
ret |= open_flags::create;
} }
if (flags & static_cast<std::uint32_t>(O_EXCL)) { const auto set_if_has_flag = [&flags, &ret](auto flag, open_flags o_flag) {
ret |= open_flags::excl; if ((flags & static_cast<std::uint32_t>(flag)) != 0U) {
} ret |= o_flag;
}
};
if (flags & static_cast<std::uint32_t>(O_NOCTTY)) { set_if_has_flag(O_APPEND, open_flags::append);
ret |= open_flags::no_ctty; set_if_has_flag(O_ASYNC, open_flags::async);
} set_if_has_flag(O_CLOEXEC, open_flags::clo_exec);
set_if_has_flag(O_CREAT, open_flags::create);
if (flags & static_cast<std::uint32_t>(O_TRUNC)) {
ret |= open_flags::truncate;
}
if (flags & static_cast<std::uint32_t>(O_APPEND)) {
ret |= open_flags::append;
}
if (flags & static_cast<std::uint32_t>(O_NONBLOCK)) {
ret |= open_flags::non_blocking;
}
if (flags & static_cast<std::uint32_t>(O_SYNC)) {
ret |= open_flags::sync;
}
if (flags & static_cast<std::uint32_t>(O_ASYNC)) {
ret |= open_flags::async;
}
if (flags & static_cast<std::uint32_t>(O_DIRECTORY)) {
ret |= open_flags::directory;
}
if (flags & static_cast<std::uint32_t>(O_NOFOLLOW)) {
ret |= open_flags::no_follow;
}
if (flags & static_cast<std::uint32_t>(O_CLOEXEC)) {
ret |= open_flags::clo_exec;
}
#ifdef O_DIRECT #ifdef O_DIRECT
if (flags & static_cast<std::uint32_t>(O_DIRECT)) { set_if_has_flag(O_DIRECT, open_flags::direct);
ret |= open_flags::direct;
}
#endif
#ifdef O_NOATIME
if (flags & static_cast<std::uint32_t>(O_NOATIME)) {
ret |= open_flags::no_atime;
}
#endif
#ifdef O_PATH
if (flags & static_cast<std::uint32_t>(O_PATH)) {
ret |= open_flags::path;
}
#endif
#ifdef O_TMPFILE
if (flags & static_cast<std::uint32_t>(O_TMPFILE)) {
ret |= open_flags::temp_file;
}
#endif #endif
set_if_has_flag(O_DIRECTORY, open_flags::directory);
#ifdef O_DSYNC #ifdef O_DSYNC
if (flags & static_cast<std::uint32_t>(O_DSYNC)) { set_if_has_flag(O_DSYNC, open_flags::dsync);
ret |= open_flags::dsync;
}
#endif #endif
set_if_has_flag(O_EXCL, open_flags::excl);
#ifdef O_NOATIME
set_if_has_flag(O_NOATIME, open_flags::no_atime);
#endif
set_if_has_flag(O_NOCTTY, open_flags::no_ctty);
set_if_has_flag(O_NOFOLLOW, open_flags::no_follow);
set_if_has_flag(O_NONBLOCK, open_flags::non_blocking);
#ifdef O_PATH
set_if_has_flag(O_PATH, open_flags::path);
#endif
set_if_has_flag(O_SYNC, open_flags::sync);
#ifdef O_TMPFILE
set_if_has_flag(O_TMPFILE, open_flags::temp_file);
#endif
set_if_has_flag(O_TRUNC, open_flags::truncate);
return ret; return ret;
} }
auto create_os_open_flags(const open_flags &flags) -> std::uint32_t { auto create_os_open_flags(const open_flags &flags) -> std::uint32_t {
std::uint32_t ret = 0u; std::uint32_t ret{};
if ((flags & open_flags::read_write) == open_flags::read_write) { const auto set_if_has_flag = [&flags, &ret](auto o_flag, auto flag) -> bool {
ret |= static_cast<std::uint32_t>(O_RDWR); if ((flags & o_flag) == o_flag) {
} else if ((flags & open_flags::write_only) == open_flags::write_only) { ret |= static_cast<std::uint32_t>(flag);
ret |= static_cast<std::uint32_t>(O_WRONLY); return true;
} else { }
ret |= static_cast<std::uint32_t>(O_RDONLY);
return false;
};
if (not set_if_has_flag(open_flags::read_write, O_RDWR)) {
if (not set_if_has_flag(open_flags::write_only, O_WRONLY)) {
ret |= static_cast<std::uint32_t>(O_RDONLY);
}
} }
if ((flags & open_flags::create) == open_flags::create) { set_if_has_flag(open_flags::append, O_APPEND);
ret |= static_cast<std::uint32_t>(O_CREAT); set_if_has_flag(open_flags::async, O_ASYNC);
} set_if_has_flag(open_flags::clo_exec, O_CLOEXEC);
set_if_has_flag(open_flags::create, O_CREAT);
if ((flags & open_flags::excl) == open_flags::excl) {
ret |= static_cast<std::uint32_t>(O_EXCL);
}
if ((flags & open_flags::no_ctty) == open_flags::no_ctty) {
ret |= static_cast<std::uint32_t>(O_NOCTTY);
}
if ((flags & open_flags::truncate) == open_flags::truncate) {
ret |= static_cast<std::uint32_t>(O_TRUNC);
}
if ((flags & open_flags::append) == open_flags::append) {
ret |= static_cast<std::uint32_t>(O_APPEND);
}
if ((flags & open_flags::non_blocking) == open_flags::non_blocking) {
ret |= static_cast<std::uint32_t>(O_NONBLOCK);
}
if ((flags & open_flags::sync) == open_flags::sync) {
ret |= static_cast<std::uint32_t>(O_SYNC);
}
if ((flags & open_flags::async) == open_flags::async) {
ret |= static_cast<std::uint32_t>(O_ASYNC);
}
if ((flags & open_flags::directory) == open_flags::directory) {
ret |= static_cast<std::uint32_t>(O_DIRECTORY);
}
if ((flags & open_flags::no_follow) == open_flags::no_follow) {
ret |= static_cast<std::uint32_t>(O_NOFOLLOW);
}
if ((flags & open_flags::clo_exec) == open_flags::clo_exec) {
ret |= static_cast<std::uint32_t>(O_CLOEXEC);
}
#ifdef O_DIRECT #ifdef O_DIRECT
if ((flags & open_flags::direct) == open_flags::direct) { set_if_has_flag(open_flags::direct, O_DIRECT);
ret |= static_cast<std::uint32_t>(O_DIRECT);
}
#endif
#ifdef O_NOATIME
if ((flags & open_flags::no_atime) == open_flags::no_atime) {
ret |= static_cast<std::uint32_t>(O_NOATIME);
}
#endif
#ifdef O_PATH
if ((flags & open_flags::path) == open_flags::path) {
ret |= static_cast<std::uint32_t>(O_PATH);
}
#endif
#ifdef O_TMPFILE
if ((flags & open_flags::temp_file) == open_flags::temp_file) {
ret |= static_cast<std::uint32_t>(O_TMPFILE);
}
#endif #endif
set_if_has_flag(open_flags::directory, O_DIRECTORY);
#ifdef O_DSYNC #ifdef O_DSYNC
if ((flags & open_flags::dsync) == open_flags::dsync) { set_if_has_flag(open_flags::dsync, O_DSYNC);
ret |= static_cast<std::uint32_t>(O_DSYNC);
}
#endif #endif
set_if_has_flag(open_flags::excl, O_EXCL);
#ifdef O_NOATIME
set_if_has_flag(open_flags::no_atime, O_NOATIME);
#endif
set_if_has_flag(open_flags::no_ctty, O_NOCTTY);
set_if_has_flag(open_flags::no_follow, O_NOFOLLOW);
set_if_has_flag(open_flags::non_blocking, O_NONBLOCK);
#ifdef O_PATH
set_if_has_flag(open_flags::path, O_PATH);
#endif
set_if_has_flag(open_flags::sync, O_SYNC);
#ifdef O_TMPFILE
set_if_has_flag(open_flags::temp_file, O_TMPFILE);
#endif
set_if_has_flag(open_flags::truncate, O_TRUNC);
return ret; return ret;
} }
#endif #endif

View File

@ -44,7 +44,7 @@ void calculate_allocation_size(bool directory, std::uint64_t file_size,
allocation_size = file_size; allocation_size = file_size;
} }
allocation_size = allocation_size =
((allocation_size == 0u) ? WINFSP_ALLOCATION_UNIT : allocation_size); ((allocation_size == 0U) ? WINFSP_ALLOCATION_UNIT : allocation_size);
allocation_size = allocation_size =
utils::divide_with_ceiling(allocation_size, WINFSP_ALLOCATION_UNIT) * utils::divide_with_ceiling(allocation_size, WINFSP_ALLOCATION_UNIT) *
WINFSP_ALLOCATION_UNIT; WINFSP_ALLOCATION_UNIT;
@ -55,18 +55,18 @@ auto calculate_read_size(const uint64_t &total_size, std::size_t read_size,
const uint64_t &offset) -> std::size_t { const uint64_t &offset) -> std::size_t {
return static_cast<std::size_t>( return static_cast<std::size_t>(
((offset + read_size) > total_size) ((offset + read_size) > total_size)
? ((offset < total_size) ? total_size - offset : 0u) ? ((offset < total_size) ? total_size - offset : 0U)
: read_size); : read_size);
} }
auto compare_version_strings(std::string version1, std::string version2) auto compare_version_strings(std::string version1, std::string version2)
-> int { -> int {
if (utils::string::contains(version1, "-")) { if (utils::string::contains(version1, "-")) {
version1 = utils::string::split(version1, '-')[0u]; version1 = utils::string::split(version1, '-')[0U];
} }
if (utils::string::contains(version2, "-")) { if (utils::string::contains(version2, "-")) {
version2 = utils::string::split(version2, '-')[0u]; version2 = utils::string::split(version2, '-')[0U];
} }
auto nums1 = utils::string::split(version1, '.'); auto nums1 = utils::string::split(version1, '.');
@ -80,11 +80,11 @@ auto compare_version_strings(std::string version1, std::string version2)
nums1.emplace_back("0"); nums1.emplace_back("0");
} }
for (std::size_t i = 0u; i < nums1.size(); i++) { for (std::size_t i = 0U; i < nums1.size(); i++) {
const auto int1 = utils::string::to_uint32(nums1[i]); const auto int1 = utils::string::to_uint32(nums1[i]);
const auto int2 = utils::string::to_uint32(nums2[i]); const auto int2 = utils::string::to_uint32(nums2[i]);
const auto res = std::memcmp(&int1, &int2, sizeof(int1)); const auto res = std::memcmp(&int1, &int2, sizeof(int1));
if (res) { if (res != 0) {
return res; return res;
} }
} }
@ -105,15 +105,15 @@ auto convert_api_date(const std::string &date) -> std::uint64_t {
#else #else
strptime(date_time.c_str(), "%Y-%m-%dT%T", &tm1); strptime(date_time.c_str(), "%Y-%m-%dT%T", &tm1);
#endif #endif
return nanos + (mktime(&tm1) * NANOS_PER_SECOND); return nanos + (static_cast<std::uint64_t>(mktime(&tm1)) * NANOS_PER_SECOND);
} }
auto create_curl() -> CURL * { auto create_curl() -> CURL * {
static std::recursive_mutex mtx; static std::recursive_mutex mtx;
unique_recur_mutex_lock l(mtx); unique_recur_mutex_lock lock(mtx);
curl_global_init(CURL_GLOBAL_DEFAULT); curl_global_init(CURL_GLOBAL_DEFAULT);
l.unlock(); lock.unlock();
return reset_curl(curl_easy_init()); return reset_curl(curl_easy_init());
} }
@ -132,9 +132,9 @@ auto create_uuid_string() -> std::string {
return ret; return ret;
#else #else
#if __linux__ #if __linux__
uuid id; uuid guid;
id.make(UUID_MAKE_V4); guid.make(UUID_MAKE_V4);
return id.string(); return guid.string();
#else #else
uuid_t guid; uuid_t guid;
uuid_generate_random(guid); uuid_generate_random(guid);
@ -148,8 +148,8 @@ auto create_uuid_string() -> std::string {
#endif #endif
} }
auto create_volume_label(const provider_type &pt) -> std::string { auto create_volume_label(const provider_type &prov) -> std::string {
return "repertory_" + app_config::get_provider_name(pt); return "repertory_" + app_config::get_provider_name(prov);
} }
auto download_type_from_string(std::string type, auto download_type_from_string(std::string type,
@ -158,9 +158,13 @@ auto download_type_from_string(std::string type,
type = utils::string::to_lower(utils::string::trim(type)); type = utils::string::to_lower(utils::string::trim(type));
if (type == "direct") { if (type == "direct") {
return download_type::direct; return download_type::direct;
} else if (type == "fallback") { }
if (type == "fallback") {
return download_type::fallback; return download_type::fallback;
} else if (type == "ring_buffer") { }
if (type == "ring_buffer") {
return download_type::ring_buffer; return download_type::ring_buffer;
} }
@ -192,20 +196,18 @@ auto filetime_to_unix_time(const FILETIME &ft) -> remote::file_time {
} }
void unix_time_to_filetime(const remote::file_time &ts, FILETIME &ft) { void unix_time_to_filetime(const remote::file_time &ts, FILETIME &ft) {
const auto winTime = (ts / 100ull) + 116444736000000000ull; const auto win_time = (ts / 100ULl) + 116444736000000000ull;
ft.dwHighDateTime = winTime >> 32u; ft.dwHighDateTime = win_time >> 32u;
ft.dwLowDateTime = winTime & 0xFFFFFFFF; ft.dwLowDateTime = win_time & 0xFFFFFFFF;
} }
#endif #endif
auto generate_random_string(std::uint16_t length) -> std::string { auto generate_random_string(std::uint16_t length) -> std::string {
srand(static_cast<unsigned int>(get_time_now()));
std::string ret; std::string ret;
ret.resize(length); ret.resize(length);
for (std::uint16_t i = 0u; i < length; i++) { for (std::uint16_t i = 0U; i < length; i++) {
do { do {
ret[i] = static_cast<char>(rand() % 74 + 48); ret[i] = static_cast<char>(repertory_rand<std::uint8_t>() % 74 + 48);
} while (((ret[i] >= 91) && (ret[i] <= 96)) || } while (((ret[i] >= 91) && (ret[i] <= 96)) ||
((ret[i] >= 58) && (ret[i] <= 64))); ((ret[i] >= 58) && (ret[i] <= 64)));
} }
@ -219,17 +221,21 @@ auto get_attributes_from_meta(const api_meta_map &meta) -> DWORD {
auto get_environment_variable(const std::string &variable) -> std::string { auto get_environment_variable(const std::string &variable) -> std::string {
#ifdef _WIN32 #ifdef _WIN32
std::string value; std::string val;
auto sz = ::GetEnvironmentVariable(&variable[0], nullptr, 0); auto sz = ::GetEnvironmentVariable(&variable[0], nullptr, 0);
if (sz > 0) { if (sz > 0) {
value.resize(sz); val.resize(sz);
::GetEnvironmentVariable(&variable[0], &value[0], sz); ::GetEnvironmentVariable(&variable[0], &val[0], sz);
} }
return value.c_str(); return value.c_str();
#else #else
const auto *v = getenv(variable.c_str()); static std::mutex mtx{};
return std::string(v ? v : ""); mutex_lock lock{mtx};
const auto *val = std::getenv(variable.c_str());
auto ret = std::string(val == nullptr ? "" : val);
return ret;
#endif #endif
} }
@ -253,8 +259,11 @@ void get_local_time_now(struct tm &local_time) {
#ifdef _WIN32 #ifdef _WIN32
localtime_s(&local_time, &now); localtime_s(&local_time, &now);
#else #else
static std::mutex mtx{};
mutex_lock lock{mtx};
const auto *tmp = std::localtime(&now); const auto *tmp = std::localtime(&now);
if (tmp) { if (tmp != nullptr) {
memcpy(&local_time, tmp, sizeof(local_time)); memcpy(&local_time, tmp, sizeof(local_time));
} }
#endif #endif
@ -264,18 +273,19 @@ auto get_next_available_port(std::uint16_t first_port,
std::uint16_t &available_port) -> bool { std::uint16_t &available_port) -> bool {
using namespace boost::asio; using namespace boost::asio;
using ip::tcp; using ip::tcp;
boost::system::error_code ec; boost::system::error_code error_code;
do { do {
io_service svc; io_service svc;
tcp::acceptor a(svc); tcp::acceptor acceptor(svc);
a.open(tcp::v4(), ec) || a.bind({tcp::v4(), first_port}, ec); acceptor.open(tcp::v4(), error_code) ||
} while (ec && (first_port++ < 65535u)); acceptor.bind({tcp::v4(), first_port}, error_code);
} while (error_code && (first_port++ < 65535U));
if (not ec) { if (not error_code) {
available_port = first_port; available_port = first_port;
} }
return not ec; return not error_code;
} }
auto get_time_now() -> std::uint64_t { auto get_time_now() -> std::uint64_t {
@ -305,41 +315,44 @@ auto reset_curl(CURL *curl_handle) -> CURL * {
} }
auto retryable_action(const std::function<bool()> &action) -> bool { auto retryable_action(const std::function<bool()> &action) -> bool {
static constexpr const auto retry_count = 20U;
auto succeeded = false; auto succeeded = false;
for (std::uint8_t i = 0u; not(succeeded = action()) && (i < 20u); i++) { for (std::uint8_t i = 0U; not(succeeded = action()) && (i < retry_count);
i++) {
std::this_thread::sleep_for(100ms); std::this_thread::sleep_for(100ms);
} }
return succeeded; return succeeded;
} }
void spin_wait_for_mutex(std::function<bool()> complete, void spin_wait_for_mutex(std::function<bool()> complete,
std::condition_variable &cv, std::mutex &mtx, std::condition_variable &cond, std::mutex &mtx,
const std::string &text) { const std::string &text) {
while (not complete()) { while (not complete()) {
unique_mutex_lock l(mtx); unique_mutex_lock lock(mtx);
if (not complete()) { if (not complete()) {
if (not text.empty()) { if (not text.empty()) {
/* event_system::instance().raise<DebugLog>(__FUNCTION__, /* event_system::instance().raise<DebugLog>(__FUNCTION__,
* "spin_wait_for_mutex", text); */ * "spin_wait_for_mutex", text); */
} }
cv.wait_for(l, 1s); cond.wait_for(lock, 1s);
} }
l.unlock(); lock.unlock();
} }
} }
void spin_wait_for_mutex(bool &complete, std::condition_variable &cv, void spin_wait_for_mutex(bool &complete, std::condition_variable &cond,
std::mutex &mtx, const std::string &text) { std::mutex &mtx, const std::string &text) {
while (not complete) { while (not complete) {
unique_mutex_lock l(mtx); unique_mutex_lock lock(mtx);
if (not complete) { if (not complete) {
if (not text.empty()) { if (not text.empty()) {
/* event_system::instance().raise<DebugLog>(__FUNCTION__, /* event_system::instance().raise<DebugLog>(__FUNCTION__,
* "spin_wait_for_mutex", text); */ * "spin_wait_for_mutex", text); */
} }
cv.wait_for(l, 1s); cond.wait_for(lock, 1s);
} }
l.unlock(); lock.unlock();
} }
} }
} // namespace repertory::utils } // namespace repertory::utils