diff --git a/control_tools.cpp b/control_tools.cpp index 4bd7c93..e8bf717 100644 --- a/control_tools.cpp +++ b/control_tools.cpp @@ -59,6 +59,7 @@ static char* command_generator(const char* text, int state) { "c", "c start", "c stop", "sync", "sync ls", "sync add", "sync remove", "sync rm", "s", "s ls", "s add", "s remove", "s rm", + "pending", "p", "finalize", "f", "quit", "q", nullptr @@ -100,6 +101,7 @@ void setup_app(CLI::App *app) { << " list(ls): List sync folders" << std::endl << " add : Add sync folder" << std::endl << " remove(rm) : Remove sync folder" << std::endl + << " pending(p): Check for pending transfers" << std::endl << " finalize(f): Kill daemon and quit" << std::endl << " quit(q): Exit this program" << std::endl; }); @@ -113,6 +115,29 @@ void setup_app(CLI::App *app) { char *errm = NULL; size_t errm_size = 0; RpcClient *rpc = new RpcClient(); + + // Check for pending transfers first + if (rpc->Call(CHECKPENDING, "", &errm, &errm_size) == 0) { + uint32_t *pending = nullptr; + if (pshm_read((void**)&pending, nullptr) && pending) { + if (*pending > 0) { + std::cout << "Warning: " << *pending << " pending transfer(s) detected" << std::endl; + std::cout << "Stopping daemon will break pending transfers. Continue? (y/n): "; + std::string response; + std::getline(std::cin, response); + if (response != "y" && response != "Y") { + std::cout << "Finalize cancelled" << std::endl; + free(pending); + if (errm) { free(errm); } + delete rpc; + return; + } + } + free(pending); + } + } + if (errm) { free(errm); errm = NULL; } + rpc->Call(FINALIZE, "", &errm, &errm_size); delete rpc; std::cout << "Exiting ..." << std::endl; @@ -124,6 +149,17 @@ void setup_app(CLI::App *app) { exit(0); }); + // pending command + app->add_subcommand("pending", "Check for pending transfers")->alias("p")->callback([] { + char *errm = NULL; + size_t errm_size = 0; + RpcClient *rpc = new RpcClient(); + rpc->Call(CHECKPENDING, "", &errm, &errm_size); + delete rpc; + if (errm) { free(errm); } + return 0; + }); + // crypto start auto start_crypto_cmd = crypto_cmd->add_subcommand("start", "Start crypto"); static std::string start_crypto_pwd; diff --git a/pclsync/pcommands.h b/pclsync/pcommands.h index 9fca4c1..5e3a6e0 100644 --- a/pclsync/pcommands.h +++ b/pclsync/pcommands.h @@ -4,5 +4,6 @@ enum command_ids_ { FINALIZE, LISTSYNC, ADDSYNC, - STOPSYNC + STOPSYNC, + CHECKPENDING }; diff --git a/pclsync_lib.cpp b/pclsync_lib.cpp index b1c1600..3e563c9 100644 --- a/pclsync_lib.cpp +++ b/pclsync_lib.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -392,9 +393,37 @@ int clib::pclsync_lib::stop_crypto(const char *unused) { return 0; } +int clib::pclsync_lib::check_pending(const char *unused) { + (void)unused; + + pstatus_t status; + psync_get_status(&status); + + uint32_t pending = status.filestoupload + status.filestodownload; + + // Write result to shared memory for client + pshm_write(&pending, sizeof(uint32_t)); + + if (pending > 0) { + std::cout << "Warning: " << pending << " pending transfer(s) detected"; + if (status.filestoupload > 0 && status.filestodownload > 0) { + std::cout << " (" << status.filestoupload << " upload(s), " + << status.filestodownload << " download(s))"; + } else if (status.filestoupload > 0) { + std::cout << " (" << status.filestoupload << " upload(s))"; + } else { + std::cout << " (" << status.filestodownload << " download(s))"; + } + std::cout << std::endl; + return 1; + } + + std::cout << "No pending transfers detected" << std::endl; + return 0; +} + int clib::pclsync_lib::finalize(const char *unused) { (void)unused; - psync_destroy(); exit(0); } @@ -499,6 +528,7 @@ int clib::pclsync_lib::init() { prpc_register(STARTCRYPTO, &start_crypto); prpc_register(STOPCRYPTO, &stop_crypto); prpc_register(FINALIZE, &finalize); + prpc_register(CHECKPENDING, &check_pending); prpc_register(LISTSYNC, &list_sync_folders); prpc_register(ADDSYNC, &add_sync_folder); prpc_register(STOPSYNC, &remove_sync_folder); diff --git a/pclsync_lib.h b/pclsync_lib.h index ca1faf7..7181c3c 100644 --- a/pclsync_lib.h +++ b/pclsync_lib.h @@ -97,6 +97,7 @@ public: static int start_crypto(const char *pass); static int stop_crypto(const char *path); static int finalize(const char *path); + static int check_pending(const char *unused); static int list_sync_folders(const char *path); static int add_sync_folder(const char *path); static int remove_sync_folder(const char *path);