Skip to content
This repository has been archived by the owner on Jun 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #63 from sergeyklay/fix/cleanup
Browse files Browse the repository at this point in the history
Provide public overlay.h
  • Loading branch information
sergeyklay authored Jul 25, 2021
2 parents 229286f + 3a4ee84 commit 1fedd97
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 166 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion include/pcloudcc/psync/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions include/pcloudcc/psync/overlay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* This file is part of the pCloud Console Client.
*
* (c) 2021 Serghei Iakovlev <[email protected]>
*
* 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 <stdint.h> /* 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_ */
101 changes: 55 additions & 46 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@

#include <iostream>
#include <vector>

#include <CLI/CLI.hpp>

#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<std::string> prepare_args(int argc, char** argv) {
static inline std::vector<std::string> prepare_args(int argc, char **argv) {
std::vector<std::string> args;
args.reserve(static_cast<size_t>(argc - 1));
for (int i = argc - 1; i > 0; i--) {
Expand All @@ -28,15 +28,22 @@ static inline std::vector<std::string> 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
Expand All @@ -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;

Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -102,7 +119,10 @@ class PcloudFormatter : public CLI::Formatter {
}
};

int main(int argc, char** argv) {
/// \brief pcloudcc entrypoint.
///
/// \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);
Expand All @@ -122,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");
Expand All @@ -152,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;

Expand All @@ -172,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
Expand Down Expand Up @@ -231,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);
Expand All @@ -248,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;
}
Expand Down
18 changes: 6 additions & 12 deletions src/overlay_client/overlay_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -152,7 +146,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;
}

Expand All @@ -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);
Expand Down Expand Up @@ -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;

Expand Down
Loading

0 comments on commit 1fedd97

Please sign in to comment.