From 92099c4a3e4030d13a6165b0c605fad1a474b1fb Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Sun, 25 Jul 2021 09:27:41 +0300 Subject: [PATCH 1/4] Minor code cleanup --- include/pcloudcc/psync/compiler.h | 2 +- src/overlay_client/overlay_client.c | 2 +- src/pclcli.cpp | 32 ++++++++--------- src/pclcli.hpp | 6 ++-- src/psync/poverlay.c | 4 +-- src/psync/psynclib.c | 53 ++++++++++++++--------------- 6 files changed, 46 insertions(+), 53 deletions(-) diff --git a/include/pcloudcc/psync/compiler.h b/include/pcloudcc/psync/compiler.h index 11a5cf12..902fa156 100644 --- a/include/pcloudcc/psync/compiler.h +++ b/include/pcloudcc/psync/compiler.h @@ -57,7 +57,7 @@ # define PSYNC_NO_RETURN __declspec(noreturn) #else #if __has_attribute(noreturn) -#define PSYNC_NO_RETURN __attribute__((noreturn)) +# define PSYNC_NO_RETURN __attribute__((noreturn)) #else # define PSYNC_NO_RETURN #endif diff --git a/src/overlay_client/overlay_client.c b/src/overlay_client/overlay_client.c index e03e6693..18ca3ce2 100644 --- a/src/overlay_client/overlay_client.c +++ b/src/overlay_client/overlay_client.c @@ -152,7 +152,7 @@ int send_call(overlay_command_t cmd, const char *path, int *ret, char **out) { if (!socket_path) { log_error("%s: socket path is empty", cmd2str(cmd)); *out = (void *)strdup("Unable to determine a path for UNIX socket"); - *ret = - 1; + *ret = -1; return -1; } diff --git a/src/pclcli.cpp b/src/pclcli.cpp index f85234fd..34c19dee 100644 --- a/src/pclcli.cpp +++ b/src/pclcli.cpp @@ -41,7 +41,6 @@ clib::pclcli& clib::pclcli::get_lib() { static std::string exec(const char* cmd) { std::array buffer{}; std::string result; - size_t size; std::unique_ptr pipe(popen(cmd, "r"), pclose); if (!pipe) { @@ -55,22 +54,19 @@ static std::string exec(const char* cmd) { return result; } -char * clib::pclcli::get_token(){ +char * clib::pclcli::get_token() { return psync_get_token(); } -void clib::pclcli::get_pass_from_console() -{ +void clib::pclcli::get_pass_from_console() { do_get_pass_from_console(password_); } -void clib::pclcli::get_cryptopass_from_console() -{ +void clib::pclcli::get_cryptopass_from_console() { do_get_pass_from_console(crypto_pass_); } -void clib::pclcli::do_get_pass_from_console(std::string& password) -{ +void clib::pclcli::do_get_pass_from_console(std::string& password) { if (daemon_) { std::cout << "Not able to read password when started as daemon." << std::endl; exit(1); @@ -99,8 +95,8 @@ void clib::pclcli::do_get_pass_from_console(std::string& password) #endif } -void event_handler(psync_eventtype_t event, psync_eventdata_t eventdata){ - if (eventdownloadstr << "| Up: " << status->uploadstr <<", status is " << status2string(status->status) << std::endl; *clib::pclcli::get_lib().status_ = *status; - if (status->status==PSTATUS_LOGIN_REQUIRED){ + if (status->status==PSTATUS_LOGIN_REQUIRED) { if (clib::pclcli::get_lib().get_password().empty()) clib::pclcli::get_lib().get_pass_from_console(); psync_set_user_pass(clib::pclcli::get_lib().get_username().c_str(), clib::pclcli::get_lib().get_password().c_str(), (int) clib::pclcli::get_lib().save_pass_); std::cout << "logging in" << std::endl; } - else if (status->status==PSTATUS_BAD_LOGIN_DATA){ + else if (status->status==PSTATUS_BAD_LOGIN_DATA) { if (!clib::pclcli::get_lib().newuser_) { clib::pclcli::get_lib().get_pass_from_console(); psync_set_user_pass(clib::pclcli::get_lib().get_username().c_str(), clib::pclcli::get_lib().get_password().c_str(), (int) clib::pclcli::get_lib().save_pass_); } else { std::cout << "registering" << std::endl; - if (psync_register(clib::pclcli::get_lib().get_username().c_str(), clib::pclcli::get_lib().get_password().c_str(),1, nullptr)){ + if (psync_register(clib::pclcli::get_lib().get_username().c_str(), clib::pclcli::get_lib().get_password().c_str(),1, nullptr)) { std::cout << "both login and registration failed" << std::endl; exit(1); } @@ -204,8 +200,8 @@ static void status_change(pstatus_t* status) { } } - if (status->status==PSTATUS_READY || status->status==PSTATUS_UPLOADING || status->status==PSTATUS_DOWNLOADING || status->status==PSTATUS_DOWNLOADINGANDUPLOADING){ - if (!cryptocheck){ + if (status->status==PSTATUS_READY || status->status==PSTATUS_UPLOADING || status->status==PSTATUS_DOWNLOADING || status->status==PSTATUS_DOWNLOADINGANDUPLOADING) { + if (!cryptocheck) { cryptocheck=1; if (clib::pclcli::get_lib().setup_crypto_) { lib_setup_crypto(); @@ -272,8 +268,8 @@ int clib::pclcli::init() { psync_start_sync(status_change, event_handler); char * username_old = psync_get_username(); - if (username_old){ - if (username_ != username_old){ + if (username_old) { + if (username_ != username_old) { std::cout << "logged in with user " << username_old <<", not "<< username_ <<", unlinking"< Date: Sun, 25 Jul 2021 09:32:03 +0300 Subject: [PATCH 2/4] Fix callbacks signature --- src/pclcli.cpp | 8 ++++---- src/psync/poverlay.c | 5 +++-- src/psync/poverlay.h | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/pclcli.cpp b/src/pclcli.cpp index 34c19dee..0c708494 100644 --- a/src/pclcli.cpp +++ b/src/pclcli.cpp @@ -213,19 +213,19 @@ static void status_change(pstatus_t* status) { clib::pclcli::get_lib().status_callback_((int)status->status, status2string(status->status)); } -int clib::pclcli::start_crypto (const char *pass, void *rep) { +int clib::pclcli::start_crypto(const char *pass, void **rep) { get_lib().crypto_pass_ = pass; return lib_setup_crypto(); } -int clib::pclcli::stop_crypto (const char* path, void * rep) { +int clib::pclcli::stop_crypto(const char* path, void **rep) { get_lib().crypto_on_ = false; return psync_crypto_stop(); } -int clib::pclcli::list_sync_folders (const char *path, void *rep) { +int clib::pclcli::list_sync_folders(const char *path, void **rep) { psync_folder_list_t *folders = psync_get_sync_list(); - rep = psync_malloc(sizeof(folders)); + *rep = psync_malloc(sizeof(folders)); memcpy(rep, folders, sizeof(folders)); return 0; } diff --git a/src/psync/poverlay.c b/src/psync/poverlay.c index b0f753cc..0126c22e 100644 --- a/src/psync/poverlay.c +++ b/src/psync/poverlay.c @@ -106,12 +106,13 @@ void get_answer_to_request(message *request, message *response) { message *rep = NULL; if (callbacks[ind]) { - if ((ret = callbacks[ind](request->value, rep)) == 0) { + if ((ret = callbacks[ind](request->value, &rep)) == 0) { if (rep) { psync_free(response); response = rep; - } else + } else { response->type = 0; + } } else { response->type = ret; memcpy(response->value, "No.", 4); diff --git a/src/psync/poverlay.h b/src/psync/poverlay.h index bdb8d5ca..740d1dc2 100644 --- a/src/psync/poverlay.h +++ b/src/psync/poverlay.h @@ -28,7 +28,7 @@ typedef struct message_ { * * Callback to be registered to be called from file manager extension. */ -typedef int (*poverlay_callback)(const char *path, void *rep); +typedef int (*poverlay_callback)(const char *path, void **rep); /*! \brief The main overlay loop. * From a39ceebc1f6552a19c92c8d41e912b90993d5c98 Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Sun, 25 Jul 2021 11:34:01 +0300 Subject: [PATCH 3/4] Provide public overlay.h --- CHANGELOG.md | 6 +++- include/pcloudcc/psync/overlay.h | 52 +++++++++++++++++++++++++++++ src/main.cpp | 3 ++ src/overlay_client/overlay_client.c | 16 +++------ src/pclcli.cpp | 16 ++++----- src/pclcli.hpp | 6 ++-- src/psync/poverlay-posix.c | 13 ++++---- src/psync/poverlay-windows.c | 4 ++- src/psync/poverlay.c | 32 ++++++++++-------- src/psync/poverlay.h | 30 ++--------------- 10 files changed, 107 insertions(+), 71 deletions(-) create mode 100644 include/pcloudcc/psync/overlay.h diff --git a/CHANGELOG.md b/CHANGELOG.md index 46544a7e..56d05dee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -84,4 +84,8 @@ releases, in reverse chronological order. into `poverlay-posix.c`. * Moved `PSYNC_CRYPTO_*` defines from `psynclib.h` to `pcloudcrypto.h`. * Replace `boost::program_options` by more lightweight and specialized library `CLI11`. -* Rename `--commands_only` CLI option to `--commands-only`. +* Renamed `--commands_only` CLI option to `--commands-only`. +* Renamed `message` overlay data type to `overlay_message_t`. +* Renamed `get_answer_to_request()` to `psync_overlay_process_request()`. +* Renamed `psync_add_overlay_callback()` to `psync_overlay_add_callback()`. +* Split `poverlay.h` into public (`pcloudcc/psync/overlay.h`) and private part. diff --git a/include/pcloudcc/psync/overlay.h b/include/pcloudcc/psync/overlay.h new file mode 100644 index 00000000..3d634f72 --- /dev/null +++ b/include/pcloudcc/psync/overlay.h @@ -0,0 +1,52 @@ +/* + * This file is part of the pCloud Console Client. + * + * (c) 2021 Serghei Iakovlev + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +#ifndef PCLOUD_PSYNC_OVERLAY_H_ +#define PCLOUD_PSYNC_OVERLAY_H_ + +#include /* uint32_t, uint64_t */ + +#ifdef __cplusplus +extern "C" { +#endif + +/*! \brief A type definition for the overlay message. */ +typedef struct { + uint32_t type; + uint64_t length; + char value[]; +} overlay_message_t; + +/*! \brief The overlay callback. + * + * Callback to be registered to be called from file manager extension or + * commands mode (pcloudcc -k). Meant for psync_overlay_add_callback(). + */ +typedef int (*overlay_callback)(const char *path, void *rep); + +/*! \brief Register an overlay callback. + * + * Registers overlay callback that will be called when packet with \a id + * equals to the give one had arrived from extension. The \a id must be over + * or equal to 20 or -1 will be returned. Value of 0 returned on success. + * + * \warning These function are not thread-safe. Use them in single thread or + * synchronize. + */ +int psync_overlay_add_callback(int id, overlay_callback callback); + +/*! \brief Process a \a request. */ +void psync_overlay_process_request(overlay_message_t *request, + overlay_message_t *response); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* PCLOUD_PSYNC_OVERLAY_H_ */ diff --git a/src/main.cpp b/src/main.cpp index 68783104..14eeb522 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -102,6 +102,9 @@ class PcloudFormatter : public CLI::Formatter { } }; +/// \brief pcloudcc entrypoint. +/// +/// \return #EXIT_SUCCESS on success, #EXIT_FAILURE otherwise. int main(int argc, char** argv) { auto args = prepare_args(argc, argv); auto const description = diff --git a/src/overlay_client/overlay_client.c b/src/overlay_client/overlay_client.c index 18ca3ce2..512571d9 100644 --- a/src/overlay_client/overlay_client.c +++ b/src/overlay_client/overlay_client.c @@ -20,16 +20,10 @@ #include "pcloudcc/psync/compat.h" #include "pcloudcc/psync/stringcompat.h" #include "pcloudcc/psync/sockets.h" +#include "pcloudcc/psync/overlay.h" #include "overlay_client.h" #include "logger.h" -/* TODO: Duplicate. See: poverlay.h. Move to sockets.h? */ -typedef struct message_ { - uint32_t type; - uint64_t length; - char value[]; -} message; - static int logger_initialized = 0; /*! \brief Reads \a nbyte bytes from \a socketfd. @@ -107,12 +101,12 @@ int send_call(overlay_command_t cmd, const char *path, int *ret, char **out) { int fd; size_t path_size = strlen(path); - size_t mess_size = sizeof(message) + path_size + 1; + size_t mess_size = sizeof(overlay_message_t) + path_size + 1; size_t rc, bw = 0; char *curbuf = NULL; char *buf = NULL; char sendbuf[mess_size]; - message *rep = NULL; + overlay_message_t *rep = NULL; uint32_t bufflen = 0; uint64_t msg_type; @@ -179,7 +173,7 @@ int send_call(overlay_command_t cmd, const char *path, int *ret, char **out) { } #endif - message *mes = (message *)sendbuf; + overlay_message_t *mes = (overlay_message_t *)sendbuf; memset (mes, 0, mess_size); mes->type = cmd; strlcpy(mes->value, path, path_size + 1); @@ -214,7 +208,7 @@ int send_call(overlay_command_t cmd, const char *path, int *ret, char **out) { } buf = (char *)malloc(bufflen); - rep = ( message *)buf; + rep = (overlay_message_t *)buf; rep->length = bufflen; rep->type = msg_type; diff --git a/src/pclcli.cpp b/src/pclcli.cpp index 0c708494..d83bb139 100644 --- a/src/pclcli.cpp +++ b/src/pclcli.cpp @@ -23,8 +23,8 @@ #endif #include "pcloudcc/psync/deviceid.h" +#include "pcloudcc/psync/overlay.h" #include "psynclib.h" -#include "poverlay.h" #include "logger.h" #include "pcloudcc/version.hpp" @@ -213,19 +213,19 @@ static void status_change(pstatus_t* status) { clib::pclcli::get_lib().status_callback_((int)status->status, status2string(status->status)); } -int clib::pclcli::start_crypto(const char *pass, void **rep) { +int clib::pclcli::start_crypto(const char *pass, void *rep) { get_lib().crypto_pass_ = pass; return lib_setup_crypto(); } -int clib::pclcli::stop_crypto(const char* path, void **rep) { +int clib::pclcli::stop_crypto(const char* path, void *rep) { get_lib().crypto_on_ = false; return psync_crypto_stop(); } -int clib::pclcli::list_sync_folders(const char *path, void **rep) { +int clib::pclcli::list_sync_folders(const char *path, void *rep) { psync_folder_list_t *folders = psync_get_sync_list(); - *rep = psync_malloc(sizeof(folders)); + rep = psync_malloc(sizeof(folders)); memcpy(rep, folders, sizeof(folders)); return 0; } @@ -278,9 +278,9 @@ int clib::pclcli::init() { psync_free(username_old); } - psync_add_overlay_callback(20, &clib::pclcli::start_crypto); - psync_add_overlay_callback(21, &clib::pclcli::stop_crypto); - psync_add_overlay_callback(22, &clib::pclcli::list_sync_folders); + psync_overlay_add_callback(20, &clib::pclcli::start_crypto); + psync_overlay_add_callback(21, &clib::pclcli::stop_crypto); + psync_overlay_add_callback(22, &clib::pclcli::list_sync_folders); return 0; } diff --git a/src/pclcli.hpp b/src/pclcli.hpp index 91226c2d..ecc2cb1c 100644 --- a/src/pclcli.hpp +++ b/src/pclcli.hpp @@ -57,9 +57,9 @@ namespace console_client { // API calls int init(); - static int start_crypto(const char* pass, void **rep); - static int stop_crypto(const char* path, void **rep); - static int list_sync_folders(const char* path, void **rep); + static int start_crypto(const char* pass, void *rep); + static int stop_crypto(const char* path, void *rep); + static int list_sync_folders(const char* path, void *rep); // Singleton static pclcli& get_lib(); diff --git a/src/psync/poverlay-posix.c b/src/psync/poverlay-posix.c index 8b1bf7d0..4db7e8d8 100644 --- a/src/psync/poverlay-posix.c +++ b/src/psync/poverlay-posix.c @@ -9,6 +9,7 @@ */ #include "pcloudcc/psync/compat.h" +#include "pcloudcc/psync/overlay.h" #if defined P_OS_POSIX #include @@ -109,10 +110,10 @@ void overlay_main_loop() { void instance_thread(void *payload) { int *fd; char chbuf[POVERLAY_BUFSIZE]; - message *request = NULL; + overlay_message_t *request = NULL; char *curbuf = &chbuf[0]; size_t ret, br = 0; - message *response = (message *)psync_malloc(POVERLAY_BUFSIZE); + overlay_message_t *response = (overlay_message_t *)psync_malloc(POVERLAY_BUFSIZE); memset(response, 0, POVERLAY_BUFSIZE); memset(chbuf, 0, POVERLAY_BUFSIZE); @@ -124,7 +125,7 @@ void instance_thread(void *payload) { curbuf = curbuf + ret; log_trace("read %u bytes from socket", br); if (br > 12) { - request = (message *)chbuf; + request = (overlay_message_t *)chbuf; if (request->length == br) break; } @@ -139,11 +140,11 @@ void instance_thread(void *payload) { close(*fd); } - request = (message *)chbuf; + request = (overlay_message_t *)chbuf; if (request) { - log_trace("getting an answer to request..."); - get_answer_to_request(request, response); + psync_overlay_process_request(request, response); if (response) { + log_trace("got answer to request [%d]: %s", (int)response->type, response->value); ret = write(*fd, response, response->length); if (ret == -1) { log_error("failed to write to socket: %s", strerror(errno)); diff --git a/src/psync/poverlay-windows.c b/src/psync/poverlay-windows.c index c6828938..aa7b67bd 100644 --- a/src/psync/poverlay-windows.c +++ b/src/psync/poverlay-windows.c @@ -17,6 +17,8 @@ #include #include +#include "pcloudcc/psync/overlay.h" + #define POVERLAY_BUFSIZE 600 #define MAX_SEM_COUNT 10 #define THREADCOUNT 12 @@ -133,7 +135,7 @@ void instance_thread(void* payload) { message *request = (message *)chBuf; log_debug("bytes received %d buffer[%s]", cbBytesRead, chBuf); - get_answer_to_request(request, reply); + psync_overlay_process_request(request, reply); fSuccess = WriteFile( hPipe, /* handle to pipe */ reply, /* buffer to write from */ diff --git a/src/psync/poverlay.c b/src/psync/poverlay.c index 0126c22e..e4dfd0c1 100644 --- a/src/psync/poverlay.c +++ b/src/psync/poverlay.c @@ -9,6 +9,7 @@ */ #include "pcloudcc/psync/compat.h" +#include "pcloudcc/psync/overlay.h" #include "plibs.h" #include "poverlay.h" @@ -27,21 +28,23 @@ void overlay_main_loop(void) {} void instance_thread(void* payload) {} #endif /* P_OS_WINDOWS */ -poverlay_callback *callbacks; +overlay_callback *callbacks; static int callbacks_size = 15; static const int callbacks_lower_band = 20; -int psync_add_overlay_callback(int id, poverlay_callback callback) { - poverlay_callback *callbacks_old = callbacks; - int callbacks_size_old = callbacks_size; - if (id < callbacks_lower_band) +int psync_overlay_add_callback(int id, overlay_callback callback) { + if (id < callbacks_lower_band) { return -1; + } + + overlay_callback *callbacks_old = callbacks; + int callbacks_size_old = callbacks_size; if (id > (callbacks_lower_band + callbacks_size)) { callbacks_size = id - callbacks_lower_band + 1; init_overlay_callbacks(); memcpy(callbacks, callbacks_old, - callbacks_size_old * sizeof(poverlay_callback)); + callbacks_size_old * sizeof(overlay_callback)); psync_free(callbacks_old); } @@ -50,8 +53,8 @@ int psync_add_overlay_callback(int id, poverlay_callback callback) { } void init_overlay_callbacks() { - callbacks = (poverlay_callback *) psync_malloc(sizeof(poverlay_callback) * callbacks_size); - memset(callbacks, 0, sizeof(poverlay_callback) * callbacks_size); + callbacks = (overlay_callback *) psync_malloc(sizeof(overlay_callback) * callbacks_size); + memset(callbacks, 0, sizeof(overlay_callback) * callbacks_size); } void psync_stop_overlays() { @@ -70,13 +73,14 @@ void psync_start_overlay_callbacks() { callbacks_running = 1; } -void get_answer_to_request(message *request, message *response) { +void psync_overlay_process_request(overlay_message_t *request, + overlay_message_t *response) { psync_path_status_t stat = PSYNC_PATH_STATUS_NOT_OURS; memcpy(response->value, "Ok.", 4); - response->length = sizeof(message) + 4; + response->length = sizeof(overlay_message_t) + 4; int max_band; - if (request->type < 20 ) { + if (request->type < callbacks_lower_band) { if (overlays_running) stat = psync_path_status_get(request->value); switch (psync_path_status_get_status(stat)) { @@ -103,7 +107,7 @@ void get_answer_to_request(message *request, message *response) { if (psync_overlays_running() && (request->type < max_band)) { uint32_t ind = request->type - 20; int ret; - message *rep = NULL; + overlay_message_t *rep = NULL; if (callbacks[ind]) { if ((ret = callbacks[ind](request->value, &rep)) == 0) { @@ -120,7 +124,7 @@ void get_answer_to_request(message *request, message *response) { } else { response->type = 13; memcpy(response->value, "No callback with this id registered.", 37); - response->length = sizeof(message)+37; + response->length = sizeof(overlay_message_t) + 37; } return; /* exit */ @@ -128,7 +132,7 @@ void get_answer_to_request(message *request, message *response) { response->type = 13; memcpy(response->value, "Invalid type.", 14); - response->length = sizeof(message) + 14; + response->length = sizeof(overlay_message_t) + 14; } int psync_overlays_running() { diff --git a/src/psync/poverlay.h b/src/psync/poverlay.h index 740d1dc2..b5a9014f 100644 --- a/src/psync/poverlay.h +++ b/src/psync/poverlay.h @@ -11,6 +11,8 @@ #ifndef PCLOUD_PSYNC_POVERLAY_H_ #define PCLOUD_PSYNC_POVERLAY_H_ +#include "pcloudcc/psync/overlay.h" + #ifdef __cplusplus extern "C" { #endif @@ -18,18 +20,6 @@ extern "C" { extern int overlays_running; extern int callbacks_running; -typedef struct message_ { - uint32_t type; - uint64_t length; - char value[]; -} message; - -/*! \brief The file manager extension callback. - * - * Callback to be registered to be called from file manager extension. - */ -typedef int (*poverlay_callback)(const char *path, void **rep); - /*! \brief The main overlay loop. * * The main loop creates an instance of the named pipe on Windows, or a UNIX @@ -41,20 +31,6 @@ typedef int (*poverlay_callback)(const char *path, void **rep); void overlay_main_loop(); void instance_thread(void* payload); -void get_answer_to_request(message *request, message *replay); - -/*! \brief Register a file manager callback. - * - * Registers file manager extension callback that will be called when packet - * with id equals to the give one had arrived from extension. The id must be - * over or equal to 20 or -1 will be returned. There is a hard coded maximum - * of menu items on some OS-s so maximum of 15 ids are available. Value of -2 - * is returned when id grater then 35 and 0 returned on success. - * - * \warning These function are not thread-safe. Use them in single thread or - * synchronize. - */ -int psync_add_overlay_callback(int id, poverlay_callback callback); void psync_stop_overlays(); void psync_start_overlays(); @@ -66,7 +42,7 @@ int psync_ovr_callbacks_running(); void init_overlay_callbacks(); #ifdef __cplusplus -} +} /* extern "C" */ #endif #endif /* PCLOUD_PSYNC_POVERLAY_H_ */ From 3a4ee84e9d1963f6b2ea5e7cdacdec24a7fdcae4 Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Sun, 25 Jul 2021 11:46:01 +0300 Subject: [PATCH 4/4] Reformat main.cpp --- src/main.cpp | 100 +++++++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 47 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 14eeb522..c50f46de 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,16 +9,16 @@ #include #include + #include -#include "unistd.h" +#include "control_tools.hpp" +#include "pclcli.hpp" #include "pcloudcc/psync/version.h" - #include "pcloudcc/version.hpp" -#include "pclcli.hpp" -#include "control_tools.hpp" +#include "unistd.h" -static inline std::vector prepare_args(int argc, char** argv) { +static inline std::vector prepare_args(int argc, char **argv) { std::vector args; args.reserve(static_cast(argc - 1)); for (int i = argc - 1; i > 0; i--) { @@ -28,15 +28,22 @@ static inline std::vector prepare_args(int argc, char** argv) { return args; } +/// \brief This is the default Formatter for pcloudcc CLI. +/// +/// It pretty prints help output, and is broken into quite a few overridable +/// methods, to be highly customizable with minimal effort. class PcloudFormatter : public CLI::Formatter { public: + /// \brief Gets the usage line. + /// + /// \param app The pointer to a CLI::App instance + /// \param name The application name (e.g. "pcloudcc"). Currently not unused + /// \return A program (command) usage string. std::string make_usage(const CLI::App *app, const std::string /* name */) const override { - auto out = get_label("Usage") + ":\n"; out += " " + app->get_name(); - auto groups = app->get_groups(); // Print an OPTIONS badge if any options exist @@ -57,6 +64,10 @@ class PcloudFormatter : public CLI::Formatter { return out += "\n"; } + /// \brief Displays the description line. + /// + /// \param app The pointer to a CLI::App instance + /// \return Program/command description string. std::string make_description(const CLI::App *app) const override { std::string out; @@ -77,9 +88,15 @@ class PcloudFormatter : public CLI::Formatter { return out; } - std::string make_option_name(const CLI::Option *opt, bool is_positional) const override { + /// \brief Format the name part of an option. + /// + /// \param opt The pointer to a CLI::Option instance + /// \param is_positional Is this option positional? + /// \return Option name as a string. + std::string make_option_name(const CLI::Option *opt, + bool is_positional) const override { std::string name; - if(is_positional) + if (is_positional) name = opt->get_name(true, false); else name = opt->get_name(false, true); @@ -88,8 +105,8 @@ class PcloudFormatter : public CLI::Formatter { if (name[0] == '-' && name[1] == '-') { new_name = " " + name; } else { - for(char i : name) { - if(i != ',') + for (char i : name) { + if (i != ',') new_name += i; else { new_name += i; @@ -104,8 +121,8 @@ class PcloudFormatter : public CLI::Formatter { /// \brief pcloudcc entrypoint. /// -/// \return #EXIT_SUCCESS on success, #EXIT_FAILURE otherwise. -int main(int argc, char** argv) { +/// \return 0 on success, 1 otherwise. +int main(int argc, char **argv) { auto args = prepare_args(argc, argv); auto const description = std::string(PCLOUD_PACKAGE_NAME) + " " + std::string(PCLOUD_VERSION); @@ -125,12 +142,10 @@ int main(int argc, char** argv) { "Daemon already started pass only commands"); bool daemonize = false; - app.add_flag("--daemonize,-d", daemonize, - "Daemonize the process"); + app.add_flag("--daemonize,-d", daemonize, "Daemonize the process"); std::string username; - app.add_option("--username,-u", username, - "pCloud account name"); + app.add_option("--username,-u", username, "pCloud account name"); bool passwordsw = false; app.add_flag("--password,-p", passwordsw, "Ask pCloud account password"); @@ -155,13 +170,11 @@ int main(int argc, char** argv) { "Parent stays alive and processes commands"); bool savepassword = false; - app.add_flag("--savepassword,-s", commands, - "Save password in database"); + app.add_flag("--savepassword,-s", commands, "Save password in database"); - auto version = [](int /* count */){ - std::cout << PCLOUD_VERSION_FULL << " (" - << PSYNC_VERSION_FULL - << ") " << std::endl; + auto version = [](int /* count */) { + std::cout << PCLOUD_VERSION_FULL << " (" << PSYNC_VERSION_FULL << ") " + << std::endl; std::cout << "Copyright " << PCLOUD_COPYRIGHT << "." << std::endl; @@ -175,27 +188,22 @@ int main(int argc, char** argv) { << std::endl; exit(EXIT_SUCCESS); }; - app.add_flag_function( - "--version,-V", - version, - "Print client version information and quit"); + app.add_flag_function("--version,-V", version, + "Print client version information and quit"); - auto vernum = [](int /* count */){ + auto vernum = [](int /* count */) { std::cout << PCLOUD_VERSION_ID << std::endl; exit(EXIT_SUCCESS); }; - app.add_flag_function( - "--vernum", - vernum, - "Print the version of the client as integer and quit"); + app.add_flag_function("--vernum", vernum, + "Print the version of the client as integer and quit"); - auto dumpversion = [](int /* count */){ + auto dumpversion = [](int /* count */) { std::cout << PCLOUD_VERSION << std::endl; exit(EXIT_SUCCESS); }; app.add_flag_function( - "--dumpversion", - dumpversion, + "--dumpversion", dumpversion, "Print the version of the client and don't do anything else"); // Remove help flag because it shortcuts all processing @@ -234,13 +242,14 @@ int main(int argc, char** argv) { console_client::clibrary::pclcli::get_lib().set_crypto_pass(password); else { std::cout << "Crypto password: "; - console_client::clibrary::pclcli::get_lib().get_cryptopass_from_console(); + console_client::clibrary::pclcli::get_lib() + .get_cryptopass_from_console(); } } else - console_client::clibrary::pclcli::get_lib().setup_crypto_ = false; + console_client::clibrary::pclcli::get_lib().setup_crypto_ = false; if (!mountpoint.empty()) - console_client::clibrary::pclcli::get_lib().set_mount(mountpoint); + console_client::clibrary::pclcli::get_lib().set_mount(mountpoint); console_client::clibrary::pclcli::get_lib().newuser_ = newuser; console_client::clibrary::pclcli::get_lib().set_savepass(savepassword); @@ -251,21 +260,18 @@ int main(int argc, char** argv) { else { if (commands) std::cout << "The \"commands\" option was ignored because the " - << "client is not running in daemon mode" - << std::endl; - if (!console_client::clibrary::pclcli::get_lib().init()) - sleep(360000); + << "client is not running in daemon mode" << std::endl; + if (!console_client::clibrary::pclcli::get_lib().init()) sleep(360000); } - } catch (const CLI::Error& e) { + } catch (const CLI::Error &e) { auto ret = app.exit(e); return ret; - } catch(std::exception& e) { + } catch (std::exception &e) { std::cerr << "Error: " << e.what() << std::endl; return EXIT_FAILURE; - } catch(...) { + } catch (...) { std::cerr << "Unknown error. " - << "Please open a bug report: " - << PCLOUD_PACKAGE_URL + << "Please open a bug report: " << PCLOUD_PACKAGE_URL << std::endl; return EXIT_FAILURE; }