refactor
This commit is contained in:
@@ -160,21 +160,25 @@ void fuse_base::destroy_(void *ptr) {
|
||||
execute_void_callback(__FUNCTION__, [&]() { instance().destroy_impl(ptr); });
|
||||
}
|
||||
|
||||
void fuse_base::display_options([[maybe_unused]] int argc,
|
||||
[[maybe_unused]] char *argv[]) {
|
||||
void fuse_base::display_options(
|
||||
[[maybe_unused]] std::vector<const char *> args) {
|
||||
#if FUSE_USE_VERSION >= 30
|
||||
fuse_cmdline_help();
|
||||
#else
|
||||
struct fuse_operations fuse_ops {};
|
||||
fuse_main(argc, argv, &fuse_ops, nullptr);
|
||||
fuse_main(args.size(),
|
||||
reinterpret_cast<char **>(const_cast<char **>(args.data())),
|
||||
&fuse_ops, nullptr);
|
||||
#endif
|
||||
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
void fuse_base::display_version_information(int argc, char *argv[]) {
|
||||
void fuse_base::display_version_information(std::vector<const char *> args) {
|
||||
struct fuse_operations fuse_ops {};
|
||||
fuse_main(argc, argv, &fuse_ops, nullptr);
|
||||
fuse_main(args.size(),
|
||||
reinterpret_cast<char **>(const_cast<char **>(args.data())),
|
||||
&fuse_ops, nullptr);
|
||||
}
|
||||
|
||||
auto fuse_base::execute_callback(
|
||||
|
40
src/main.cpp
40
src/main.cpp
@@ -39,12 +39,13 @@ std::size_t PROVIDER_INDEX{0U};
|
||||
auto main(int argc, char **argv) -> int {
|
||||
repertory_init();
|
||||
|
||||
std::vector<const char *> args(argv, argv + argc);
|
||||
#ifdef REPERTORY_TESTING
|
||||
#ifdef _WIN32
|
||||
if (utils::cli::has_option(argc, argv, "--provider_index")) {
|
||||
if (utils::cli::has_option(args, "--provider_index")) {
|
||||
PROVIDER_INDEX = static_cast<std::size_t>(
|
||||
utils::string::to_uint64(
|
||||
utils::cli::parse_option(argc, argv, "--provider_index", 1U)[0U]) +
|
||||
utils::cli::parse_option(args, "--provider_index", 1U)[0U]) +
|
||||
1U);
|
||||
}
|
||||
#endif // _WIN32
|
||||
@@ -54,28 +55,25 @@ auto main(int argc, char **argv) -> int {
|
||||
delete_generated_files();
|
||||
#else
|
||||
if (argc == 1) {
|
||||
argc++;
|
||||
static std::string cmd(argv[0U]);
|
||||
static std::vector<const char *> v({cmd.data(), "-h"});
|
||||
argv = (char **)v.data();
|
||||
args.push_back("-h");
|
||||
}
|
||||
|
||||
auto prov = utils::cli::get_provider_type_from_args(argc, argv);
|
||||
auto prov = utils::cli::get_provider_type_from_args(args);
|
||||
|
||||
std::string data_directory;
|
||||
auto res = utils::cli::parse_string_option(
|
||||
argc, argv, utils::cli::options::data_directory_option, data_directory);
|
||||
args, utils::cli::options::data_directory_option, data_directory);
|
||||
|
||||
std::string password;
|
||||
res = (res == exit_code::success)
|
||||
? utils::cli::parse_string_option(
|
||||
argc, argv, utils::cli::options::password_option, password)
|
||||
args, utils::cli::options::password_option, password)
|
||||
: res;
|
||||
|
||||
std::string user;
|
||||
res = (res == exit_code::success)
|
||||
? utils::cli::parse_string_option(
|
||||
argc, argv, utils::cli::options::user_option, user)
|
||||
args, utils::cli::options::user_option, user)
|
||||
: res;
|
||||
|
||||
std::string remote_host;
|
||||
@@ -84,7 +82,7 @@ auto main(int argc, char **argv) -> int {
|
||||
if ((res == exit_code::success) && (prov == provider_type::remote)) {
|
||||
std::string data;
|
||||
if ((res = utils::cli::parse_string_option(
|
||||
argc, argv, utils::cli::options::remote_mount_option, data)) ==
|
||||
args, utils::cli::options::remote_mount_option, data)) ==
|
||||
exit_code::success) {
|
||||
const auto parts = utils::string::split(data, ':');
|
||||
if (parts.size() != 2) {
|
||||
@@ -102,7 +100,7 @@ auto main(int argc, char **argv) -> int {
|
||||
: data_directory,
|
||||
{utils::string::replace_copy(unique_id, ':', '_')});
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << (e.what() ? e.what() : "Unable to parse port")
|
||||
std::cerr << (e.what() == nullptr ? "Unable to parse port" : e.what())
|
||||
<< std::endl;
|
||||
res = exit_code::invalid_syntax;
|
||||
}
|
||||
@@ -113,7 +111,7 @@ auto main(int argc, char **argv) -> int {
|
||||
#ifdef REPERTORY_ENABLE_S3
|
||||
if ((res == exit_code::success) && (prov == provider_type::s3)) {
|
||||
std::string data;
|
||||
if ((res = utils::cli::parse_string_option(argc, argv,
|
||||
if ((res = utils::cli::parse_string_option(args,
|
||||
utils::cli::options::name_option,
|
||||
data)) == exit_code::success) {
|
||||
unique_id = utils::string::trim(data);
|
||||
@@ -133,11 +131,11 @@ auto main(int argc, char **argv) -> int {
|
||||
|
||||
int mount_result{};
|
||||
if (res == exit_code::success) {
|
||||
if (utils::cli::has_option(argc, argv, utils::cli::options::help_option)) {
|
||||
cli::actions::help<repertory_drive>(argc, argv);
|
||||
} else if (utils::cli::has_option(argc, argv,
|
||||
if (utils::cli::has_option(args, utils::cli::options::help_option)) {
|
||||
cli::actions::help<repertory_drive>(args);
|
||||
} else if (utils::cli::has_option(args,
|
||||
utils::cli::options::version_option)) {
|
||||
cli::actions::version<repertory_drive>(argc, argv);
|
||||
cli::actions::version<repertory_drive>(args);
|
||||
} else {
|
||||
res = exit_code::option_not_found;
|
||||
for (std::size_t idx = 0U;
|
||||
@@ -145,12 +143,12 @@ auto main(int argc, char **argv) -> int {
|
||||
(idx < utils::cli::options::option_list.size());
|
||||
idx++) {
|
||||
res = cli::actions::perform_action(
|
||||
utils::cli::options::option_list[idx], argc, argv, data_directory,
|
||||
prov, unique_id, user, password);
|
||||
utils::cli::options::option_list[idx], args, data_directory, prov,
|
||||
unique_id, user, password);
|
||||
}
|
||||
if (res == exit_code::option_not_found) {
|
||||
res = cli::actions::mount(argc, argv, data_directory, mount_result,
|
||||
prov, remote_host, remote_port, unique_id);
|
||||
res = cli::actions::mount(args, data_directory, mount_result, prov,
|
||||
remote_host, remote_port, unique_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -43,36 +43,33 @@ inline void create_comm(std::unique_ptr<intf_t> &comm, const config_t &config) {
|
||||
} // namespace
|
||||
|
||||
namespace repertory {
|
||||
auto create_provider(const provider_type &pt, app_config &config)
|
||||
auto create_provider(const provider_type &prov, app_config &config)
|
||||
-> std::unique_ptr<i_provider> {
|
||||
static std::mutex mutex;
|
||||
mutex_lock lock(mutex);
|
||||
|
||||
static std::unique_ptr<i_http_comm> comm;
|
||||
|
||||
switch (pt) {
|
||||
switch (prov) {
|
||||
case provider_type::sia: {
|
||||
create_comm<i_http_comm, curl_comm, host_config>(comm,
|
||||
config.get_host_config());
|
||||
return std::unique_ptr<i_provider>(
|
||||
dynamic_cast<i_provider *>(new sia_provider(config, *comm)));
|
||||
return std::make_unique<sia_provider>(config, *comm);
|
||||
}
|
||||
#if defined(REPERTORY_ENABLE_S3)
|
||||
case provider_type::s3: {
|
||||
create_comm<i_http_comm, curl_comm, s3_config>(comm,
|
||||
config.get_s3_config());
|
||||
return std::unique_ptr<i_provider>(
|
||||
dynamic_cast<i_provider *>(new s3_provider(config, *comm)));
|
||||
return std::make_unique<s3_provider>(config, *comm);
|
||||
}
|
||||
#endif // defined(REPERTORY_ENABLE_S3)
|
||||
case provider_type::encrypt: {
|
||||
return std::unique_ptr<i_provider>(
|
||||
dynamic_cast<i_provider *>(new encrypt_provider(config)));
|
||||
return std::make_unique<encrypt_provider>(config);
|
||||
}
|
||||
case provider_type::unknown:
|
||||
default:
|
||||
throw startup_exception("provider not supported: " +
|
||||
app_config::get_provider_display_name(pt));
|
||||
app_config::get_provider_display_name(prov));
|
||||
}
|
||||
}
|
||||
} // namespace repertory
|
||||
|
@@ -29,10 +29,10 @@
|
||||
|
||||
namespace repertory::utils::cli {
|
||||
void get_api_authentication_data(std::string &user, std::string &password,
|
||||
std::uint16_t &port, const provider_type &pt,
|
||||
std::uint16_t &port, const provider_type &prov,
|
||||
const std::string &data_directory) {
|
||||
const auto cfg_file_path = utils::path::combine(
|
||||
data_directory.empty() ? app_config::default_data_directory(pt)
|
||||
data_directory.empty() ? app_config::default_data_directory(prov)
|
||||
: data_directory,
|
||||
{"config.json"});
|
||||
|
||||
@@ -50,44 +50,45 @@ void get_api_authentication_data(std::string &user, std::string &password,
|
||||
}
|
||||
}
|
||||
|
||||
auto get_provider_type_from_args(int argc, char *argv[]) -> provider_type {
|
||||
[[nodiscard]] auto get_provider_type_from_args(std::vector<const char *> args)
|
||||
-> provider_type {
|
||||
#if defined(REPERTORY_ENABLE_S3)
|
||||
if (has_option(argc, argv, options::s3_option)) {
|
||||
if (has_option(args, options::s3_option)) {
|
||||
return provider_type::s3;
|
||||
}
|
||||
#endif // defined(REPERTORY_ENABLE_S3)
|
||||
if (has_option(argc, argv, options::remote_mount_option)) {
|
||||
if (has_option(args, options::remote_mount_option)) {
|
||||
return provider_type::remote;
|
||||
}
|
||||
if (has_option(argc, argv, options::encrypt_option)) {
|
||||
if (has_option(args, options::encrypt_option)) {
|
||||
return provider_type::encrypt;
|
||||
}
|
||||
|
||||
return provider_type::sia;
|
||||
}
|
||||
|
||||
auto has_option(int argc, char *argv[], const std::string &option_name)
|
||||
auto has_option(std::vector<const char *> args, const std::string &option_name)
|
||||
-> bool {
|
||||
auto ret = false;
|
||||
for (int i = 0; not ret && (i < argc); i++) {
|
||||
ret = (option_name == argv[i]);
|
||||
}
|
||||
return ret;
|
||||
return std::find_if(args.begin(), args.end(),
|
||||
[&option_name](const auto &value) -> bool {
|
||||
return option_name == value;
|
||||
}) != args.end();
|
||||
}
|
||||
|
||||
auto has_option(int argc, char *argv[], const option &opt) -> bool {
|
||||
return has_option(argc, argv, opt[0u]) || has_option(argc, argv, opt[1u]);
|
||||
auto has_option(std::vector<const char *> args, const option &opt) -> bool {
|
||||
return has_option(args, opt[0U]) || has_option(args, opt[1U]);
|
||||
}
|
||||
|
||||
auto parse_option(int argc, char *argv[], const std::string &option_name,
|
||||
std::uint8_t count) -> std::vector<std::string> {
|
||||
auto parse_option(std::vector<const char *> args,
|
||||
const std::string &option_name, std::uint8_t count)
|
||||
-> std::vector<std::string> {
|
||||
std::vector<std::string> ret;
|
||||
auto found = false;
|
||||
for (auto i = 0; not found && (i < argc); i++) {
|
||||
if ((found = (option_name == argv[i]))) {
|
||||
if ((++i + count) <= argc) {
|
||||
while (count--) {
|
||||
ret.emplace_back(argv[i++]);
|
||||
auto found{false};
|
||||
for (std::size_t i = 0U; not found && (i < args.size()); i++) {
|
||||
if ((found = (option_name == args.at(i)))) {
|
||||
if ((++i + count) <= args.size()) {
|
||||
while ((count--) != 0U) {
|
||||
ret.emplace_back(args.at(i++));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -96,90 +97,94 @@ auto parse_option(int argc, char *argv[], const std::string &option_name,
|
||||
return ret;
|
||||
}
|
||||
|
||||
auto parse_string_option(int argc, char **argv, const option &opt,
|
||||
auto parse_string_option(std::vector<const char *> args, const option &opt,
|
||||
std::string &value) -> exit_code {
|
||||
auto ret = exit_code::success;
|
||||
if (has_option(argc, argv, opt[0u]) || has_option(argc, argv, opt[1u])) {
|
||||
auto data = parse_option(argc, argv, opt[0u], 1u);
|
||||
exit_code ret{exit_code::success};
|
||||
if (has_option(args, opt.at(0U)) || has_option(args, opt.at(1U))) {
|
||||
auto data = parse_option(args, opt.at(0U), 1U);
|
||||
if (data.empty()) {
|
||||
data = parse_option(argc, argv, opt[1u], 1u);
|
||||
data = parse_option(args, opt.at(1U), 1U);
|
||||
if (data.empty()) {
|
||||
std::cerr << "Invalid syntax for '" << opt[0u] << "," << opt[1u] << '\''
|
||||
<< std::endl;
|
||||
std::cerr << "Invalid syntax for '" << opt.at(0U) << "," << opt.at(1U)
|
||||
<< '\'' << std::endl;
|
||||
ret = exit_code::invalid_syntax;
|
||||
}
|
||||
}
|
||||
|
||||
if (not data.empty()) {
|
||||
value = data[0u];
|
||||
value = data.at(0U);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
auto parse_drive_options(int argc, char **argv,
|
||||
[[maybe_unused]] provider_type &pt,
|
||||
auto parse_drive_options(std::vector<const char *> args,
|
||||
[[maybe_unused]] provider_type &prov,
|
||||
[[maybe_unused]] std::string &data_directory)
|
||||
-> std::vector<std::string> {
|
||||
// Strip out options from command line
|
||||
const auto &option_list = options::option_list;
|
||||
std::vector<std::string> drive_args;
|
||||
for (int i = 0; i < argc; i++) {
|
||||
const auto &a = argv[i];
|
||||
for (std::size_t i = 0U; i < args.size(); i++) {
|
||||
const auto &arg = args.at(i);
|
||||
if (std::find_if(option_list.begin(), option_list.end(),
|
||||
[&a](const auto &pair) -> bool {
|
||||
return ((pair[0] == a) || (pair[1] == a));
|
||||
[&arg](const auto &pair) -> bool {
|
||||
return ((pair[0] == arg) || (pair[1] == arg));
|
||||
}) == option_list.end()) {
|
||||
drive_args.emplace_back(argv[i]);
|
||||
} else if ((std::string(argv[i]) == options::remote_mount_option[0]) ||
|
||||
(std::string(argv[i]) == options::remote_mount_option[1])) {
|
||||
i++;
|
||||
} else if ((std::string(argv[i]) == options::data_directory_option[0]) ||
|
||||
(std::string(argv[i]) == options::data_directory_option[1])) {
|
||||
i++;
|
||||
drive_args.emplace_back(args.at(i));
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((std::string(args.at(i)) == options::remote_mount_option[0U]) ||
|
||||
(std::string(args.at(i)) == options::remote_mount_option[1U]) ||
|
||||
(std::string(args.at(i)) == options::data_directory_option[0U]) ||
|
||||
(std::string(args.at(i)) == options::data_directory_option[1U])
|
||||
#ifdef REPERTORY_ENABLE_S3
|
||||
else if ((std::string(argv[i]) == options::name_option[0]) ||
|
||||
(std::string(argv[i]) == options::name_option[1])) {
|
||||
i++;
|
||||
}
|
||||
|| (std::string(args.at(i)) == options::name_option[0U]) ||
|
||||
(std::string(args.at(i)) == options::name_option[1U])
|
||||
#endif // REPERTORY_ENABLE_S3
|
||||
) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
std::vector<std::string> fuse_flags_list;
|
||||
for (std::size_t i = 1; i < drive_args.size(); i++) {
|
||||
if (drive_args[i].find("-o") == 0) {
|
||||
std::string options = "";
|
||||
if (drive_args.at(i).find("-o") == 0) {
|
||||
std::string options;
|
||||
if (drive_args[i].size() == 2) {
|
||||
if ((i + 1) < drive_args.size()) {
|
||||
options = drive_args[++i];
|
||||
options = drive_args.at(++i);
|
||||
}
|
||||
} else {
|
||||
options = drive_args[i].substr(2);
|
||||
options = drive_args.at(i).substr(2);
|
||||
}
|
||||
|
||||
const auto fuse_option_list = utils::string::split(options, ',');
|
||||
for (const auto &fuse_option : fuse_option_list) {
|
||||
#if defined(REPERTORY_ENABLE_S3)
|
||||
if (fuse_option.find("s3") == 0) {
|
||||
pt = provider_type::s3;
|
||||
prov = provider_type::s3;
|
||||
continue;
|
||||
}
|
||||
#endif // defined(REPERTORY_ENABLE_S3)
|
||||
if ((fuse_option.find("dd") == 0) ||
|
||||
(fuse_option.find("data_directory") == 0)) {
|
||||
const auto data = utils::string::split(fuse_option, '=');
|
||||
if (data.size() == 2) {
|
||||
data_directory = data[1];
|
||||
if (data.size() == 2U) {
|
||||
data_directory = data.at(1);
|
||||
} else {
|
||||
std::cerr << "Invalid syntax for '-dd,--data_directory'"
|
||||
<< std::endl;
|
||||
exit(3);
|
||||
}
|
||||
} else {
|
||||
fuse_flags_list.push_back(fuse_option);
|
||||
continue;
|
||||
}
|
||||
|
||||
fuse_flags_list.push_back(fuse_option);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -194,10 +199,12 @@ auto parse_drive_options(int argc, char **argv,
|
||||
utils::remove_element_from(new_drive_args, new_drive_args[i]);
|
||||
i--;
|
||||
}
|
||||
} else {
|
||||
utils::remove_element_from(new_drive_args, new_drive_args[i]);
|
||||
i--;
|
||||
continue;
|
||||
}
|
||||
|
||||
utils::remove_element_from(new_drive_args, new_drive_args[i]);
|
||||
i--;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -144,11 +144,12 @@ auto is_process_elevated() -> bool {
|
||||
return ret;
|
||||
}
|
||||
|
||||
auto run_process_elevated(int argc, char *argv[]) -> int {
|
||||
auto run_process_elevated(std::vector<const char *> args) -> int {
|
||||
std::cout << "Elevating Process" << std::endl;
|
||||
std::string parameters = "-hidden";
|
||||
for (int i = 1; i < argc; i++) {
|
||||
parameters += (parameters.empty() ? argv[i] : " " + std::string(argv[i]));
|
||||
for (std::size_t i = 1U; i < args.size(); i++) {
|
||||
parameters +=
|
||||
(parameters.empty() ? args.at(i) : " " + std::string(args.at(i)));
|
||||
}
|
||||
|
||||
char full_path[MAX_PATH] = {0};
|
||||
|
Reference in New Issue
Block a user