From 8c3d737815e9756c9b93a61e2f9e7e2753a72a1d Mon Sep 17 00:00:00 2001 From: Levi Neely <141506390+lneely@users.noreply.github.com> Date: Tue, 25 Jun 2024 13:12:55 +0200 Subject: [PATCH] pCloudCC/ formatting --- Makefile | 6 ++ pCloudCC/control_tools.cpp | 213 +++++++++++++++++++++---------------- pCloudCC/pclsync_lib.cpp | 77 ++++++++++++-- pCloudCC/pclsync_lib.h | 112 +++++++++---------- pCloudCC/pclsync_lib_c.cpp | 4 +- pCloudCC/pclsync_lib_c.h | 2 +- 6 files changed, 255 insertions(+), 159 deletions(-) diff --git a/Makefile b/Makefile index 447493b..e8f240b 100644 --- a/Makefile +++ b/Makefile @@ -8,5 +8,11 @@ all: clean: make -C pCloudCC/lib/pclsync clean make -C pCloudCC/lib/mbedtls clean + rm -rf pCloudCC/lib/mbedtls/CMakeFiles + rm pCloudCC/lib/mbedtls/CMakeCache.txt + rm pCloudCC/lib/mbedtls/Makefile make -C pCloudCC clean + rm -rf pCloudCC/CMakeFiles + rm pCloudCC/CMakeCache.txt + rm pCloudCC/Makefile diff --git a/pCloudCC/control_tools.cpp b/pCloudCC/control_tools.cpp index 56e7c4a..97a887e 100644 --- a/pCloudCC/control_tools.cpp +++ b/pCloudCC/control_tools.cpp @@ -1,120 +1,147 @@ #include - #include #include -#include #include #include -#include #include #include -#include -#include #include #include "control_tools.h" #include "pclsync_lib.h" #include "overlay_client.h" -namespace control_tools{ +namespace cc = console_client; -static const int STOP = 0; +namespace control_tools { -enum command_ids_ { - STARTCRYPTO = 20, - STOPCRYPTO, - FINALIZE, - LISTSYNC, - ADDSYNC, - STOPSYNC -}; + static const int STOP = 0; + + enum command_ids_ { + STARTCRYPTO = 20, + STOPCRYPTO, + FINALIZE, + LISTSYNC, + ADDSYNC, + STOPSYNC + }; -int start_crypto(const char * pass) { - int ret; - char* errm; - if (SendCall(STARTCRYPTO, pass, &ret, &errm)) - std::cout << "Start Crypto failed. return is " << ret<< " and message is "<, stopcrypto, finalize, q, quit" << std::endl; - std::cout<< "> " ; - for (std::string line; std::getline(std::cin, line);) { - if (!line.compare("finalize")) { - finalize(); - break;} - else if (!line.compare("stopcrypto")) - stop_crypto(); - else if (!line.compare(0,11,"startcrypto",0,11) && (line.length() > 12)) - start_crypto(line.c_str() + 12); - else if (!line.compare("q") || !line.compare("quit")) - break; - - std::cout<< "> " ; + free(errm); + return ret; } -} + + int + stop_crypto(){ + int ret; + char* errm; -int daemonize(bool do_commands) { - pid_t pid, sid; + if (SendCall(STOPCRYPTO, "", &ret, &errm)) { + std::cout << "Stop Crypto failed. return is " << ret + << " and message is "< 0) { - std::cout << "Daemon process created. Process id is: " << pid << std::endl; - if (do_commands) { - process_commands(); - } - else - std::cout << "sudo kill -9 "<, " + << "stopcrypto, " + << "finalize, " + << "q, quit" << std::endl; + std::cout << "> " ; + + for (std::string line; std::getline(std::cin, line);) { + if (!line.compare("finalize")) { + finalize(); + break; + } else if (!line.compare("stopcrypto")) { + stop_crypto(); + } else if (!line.compare(0,11,"startcrypto",0,11) + && (line.length() > 12)) { + start_crypto(line.c_str() + 12); + } else if (!line.compare("q") || !line.compare("quit")) { + break; + } + std::cout<< "> " ; + } + } + + int + daemonize(bool do_commands) { + pid_t pid, sid; + + pid = fork(); + if (pid < 0) { + exit(EXIT_FAILURE); + } else if (pid > 0) { + std::cout << "Daemon process created. Process id is: " << pid << std::endl; + if (do_commands) { + process_commands(); + } else { + std::cout << "kill -9 "<< pid << std::endl + << " to stop it." << std::endl; + } + exit(EXIT_SUCCESS); + } + + /* Open any logs here */ + umask(0); + sid = setsid(); + if (sid < 0) { + exit(EXIT_FAILURE); + } + + if ((chdir("/")) < 0) { + exit(EXIT_FAILURE); + } + + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); + + if (cc::clibrary::pclsync_lib::get_lib().init()) { + exit(EXIT_FAILURE); + } + + while (1) { + sleep(10); + } } } - -} diff --git a/pCloudCC/pclsync_lib.cpp b/pCloudCC/pclsync_lib.cpp index 98d61e0..4d31493 100644 --- a/pCloudCC/pclsync_lib.cpp +++ b/pCloudCC/pclsync_lib.cpp @@ -43,7 +43,66 @@ namespace cc = console_client; namespace clib = cc::clibrary; -clib::pclsync_lib &clib::pclsync_lib::get_lib() { +const std::string& +clib::pclsync_lib::get_username() { + return username_; +} + +const std::string & +clib::pclsync_lib::get_password() { + return password_; +} + +const std::string & +clib::pclsync_lib::get_crypto_pass() { + return crypto_pass_; +}; + +const std::string & +clib::pclsync_lib::get_mount() { + return mount_; +} + +void +clib::pclsync_lib::set_username(const std::string &arg) { + username_ = arg; +} +void +clib::pclsync_lib::set_password(const std::string &arg) { + password_ = arg; +} +void +clib::pclsync_lib::set_crypto_pass(const std::string &arg) { + crypto_pass_ = arg; +}; +void +clib::pclsync_lib::set_mount(const std::string &arg) { + mount_ = arg; +} +void +clib::pclsync_lib::set_savepass(bool s) { + save_pass_ = s; +} +void +clib::pclsync_lib::setupsetup_crypto(bool p) { + setup_crypto_ = p; +} +void +clib::pclsync_lib::set_newuser(bool p) { + newuser_ = p; +} +void +clib::pclsync_lib::set_daemon(bool p) { + daemon_ = p; +} +void +clib::pclsync_lib::set_status_callback(status_callback_t p) { + status_callback_ = p; +} + + +clib::pclsync_lib & +clib::pclsync_lib::get_lib() { static clib::pclsync_lib g_lib; return g_lib; } @@ -67,17 +126,21 @@ exec(const char *cmd) { return result; } -char *clib::pclsync_lib::get_token() { return psync_get_token(); } +char * +clib::pclsync_lib::get_token() { return psync_get_token(); } -void clib::pclsync_lib::get_pass_from_console() { +void +clib::pclsync_lib::get_pass_from_console() { do_get_pass_from_console(password_); } -void clib::pclsync_lib::get_cryptopass_from_console() { +void +clib::pclsync_lib::get_cryptopass_from_console() { do_get_pass_from_console(crypto_pass_); } -void clib::pclsync_lib::do_get_pass_from_console(std::string &password) { +void +clib::pclsync_lib::do_get_pass_from_console(std::string &password) { if (daemon_) { std::cout << "Not able to read password when started as daemon." << std::endl; @@ -263,7 +326,7 @@ static void status_change(pstatus_t *status) { (int)status->status, status2string(status->status)); } -int clib::pclsync_lib::statrt_crypto(const char *pass, void *rep) { +int clib::pclsync_lib::start_crypto(const char *pass, void *rep) { std::cout << "calling startcrypto pass: " << pass << std::endl; get_lib().crypto_pass_ = pass; return lib_setup_cripto(); @@ -326,7 +389,7 @@ int clib::pclsync_lib::init() // std::string& username, std::string& password, psync_free(username_old); } - psync_add_overlay_callback(20, &clib::pclsync_lib::statrt_crypto); + psync_add_overlay_callback(20, &clib::pclsync_lib::start_crypto); psync_add_overlay_callback(21, &clib::pclsync_lib::stop_crypto); psync_add_overlay_callback(22, &clib::pclsync_lib::finalize); psync_add_overlay_callback(23, &clib::pclsync_lib::list_sync_folders); diff --git a/pCloudCC/pclsync_lib.h b/pCloudCC/pclsync_lib.h index 4db048f..743296c 100644 --- a/pCloudCC/pclsync_lib.h +++ b/pCloudCC/pclsync_lib.h @@ -36,70 +36,70 @@ struct pstatus_struct_; namespace console_client { -namespace clibrary { + namespace clibrary { -class pclsync_lib { -public: - ~pclsync_lib(); - pclsync_lib(); + class pclsync_lib { + public: + ~pclsync_lib(); + pclsync_lib(); - // Getters - const std::string &get_username() { return username_; } - const std::string &get_password() { return password_; } - const std::string &get_crypto_pass() { return crypto_pass_; }; - const std::string &get_mount() { return mount_; } + bool crypto_on_; + bool save_pass_; + bool setup_crypto_; + pstatus_struct_ *status_; + bool newuser_; + status_callback_t status_callback_; + bool was_init_; - // Setters - void set_username(const std::string &arg) { username_ = arg; } - void set_password(const std::string &arg) { password_ = arg; } - void set_crypto_pass(const std::string &arg) { crypto_pass_ = arg; }; - void set_mount(const std::string &arg) { mount_ = arg; } - void set_savepass(bool s) { save_pass_ = s; } - void setupsetup_crypto(bool p) { setup_crypto_ = p; } - void set_newuser(bool p) { newuser_ = p; } - void set_daemon(bool p) { daemon_ = p; } - void set_status_callback(status_callback_t p) { status_callback_ = p; } + // Getters + const std::string &get_username(); + const std::string &get_password(); + const std::string &get_crypto_pass(); + const std::string &get_mount(); - // Console - void get_pass_from_console(); - void get_cryptopass_from_console(); + // Setters + void set_username(const std::string &arg); + void set_password(const std::string &arg); + void set_crypto_pass(const std::string &arg); + void set_mount(const std::string &arg); + void set_savepass(bool s); + void setupsetup_crypto(bool p); + void set_newuser(bool p); + void set_daemon(bool p); + void set_status_callback(status_callback_t p); - // API calls - int init(); // std::string& username, std::string& password, std::string* - // crypto_pass, int setup_crypto = 1, int usesrypto_userpass = 0); - static int statrt_crypto(const char *pass, void *rep); - static int stop_crypto(const char *path, void *rep); - static int finalize(const char *path, void *rep); - static int list_sync_folders(const char *path, void *rep); - // Singelton - static pclsync_lib &get_lib(); - char *get_token(); - int logout(); - int unlink(); - int login(const char *user, const char *pass, int save); + // Singleton + static pclsync_lib &get_lib(); + + // Console + void get_pass_from_console(); + void get_cryptopass_from_console(); - bool crypto_on_; - bool save_pass_; - bool setup_crypto_; - pstatus_struct_ *status_; - bool newuser_; - status_callback_t status_callback_; - bool was_init_; + // API calls + int init(); + // std::string& username, std::string& password, std::string* + // crypto_pass, int setup_crypto = 1, int usesrypto_userpass = 0); + static int start_crypto(const char *pass, void *rep); + static int stop_crypto(const char *path, void *rep); + static int finalize(const char *path, void *rep); + static int list_sync_folders(const char *path, void *rep); -private: - std::string username_; - std::string password_; - std::string crypto_pass_; - std::string mount_; + char *get_token(); + int logout(); + int unlink(); + int login(const char *user, const char *pass, int save); - bool to_set_mount_; - bool daemon_; + private: + std::string username_; + std::string password_; + std::string crypto_pass_; + std::string mount_; -private: - void do_get_pass_from_console(std::string &password); -}; - -} // namespace clibrary -} // namespace console_client + bool to_set_mount_; + bool daemon_; + void do_get_pass_from_console(std::string &password); + }; + } +} #endif // PCLSYNC_LIB_H diff --git a/pCloudCC/pclsync_lib_c.cpp b/pCloudCC/pclsync_lib_c.cpp index 60054e4..d9586fb 100644 --- a/pCloudCC/pclsync_lib_c.cpp +++ b/pCloudCC/pclsync_lib_c.cpp @@ -15,8 +15,8 @@ extern "C" { else return 0; } - int statrt_crypto (const char* pass) { - return cc::pclsync_lib::statrt_crypto (pass, NULL); + int start_crypto (const char* pass) { + return cc::pclsync_lib::start_crypto (pass, NULL); } int stop_crypto () { return cc::pclsync_lib::stop_crypto (NULL, NULL); diff --git a/pCloudCC/pclsync_lib_c.h b/pCloudCC/pclsync_lib_c.h index b05196e..b8c0933 100644 --- a/pCloudCC/pclsync_lib_c.h +++ b/pCloudCC/pclsync_lib_c.h @@ -8,7 +8,7 @@ void psync_fs_pause_until_login(); typedef void (*status_callback_t)(int status, const char *stat_string); int init(); -int statrt_crypto(const char *pass); +int start_crypto(const char *pass); int stop_crypto(); int finalize(); char *get_token();