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

Commit

Permalink
Provide public overlay.h
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyklay committed Jul 25, 2021
1 parent 53eb6e5 commit a39ceeb
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 71 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.
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_ */
3 changes: 3 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
16 changes: 5 additions & 11 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 @@ -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
16 changes: 8 additions & 8 deletions src/pclcli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/pclcli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
13 changes: 7 additions & 6 deletions src/psync/poverlay-posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

#include "pcloudcc/psync/compat.h"
#include "pcloudcc/psync/overlay.h"

#if defined P_OS_POSIX
#include <sys/socket.h>
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand All @@ -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));
Expand Down
4 changes: 3 additions & 1 deletion src/psync/poverlay-windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <tchar.h>
#include <strsafe.h>

#include "pcloudcc/psync/overlay.h"

#define POVERLAY_BUFSIZE 600
#define MAX_SEM_COUNT 10
#define THREADCOUNT 12
Expand Down Expand Up @@ -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 */
Expand Down
32 changes: 18 additions & 14 deletions src/psync/poverlay.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

#include "pcloudcc/psync/compat.h"
#include "pcloudcc/psync/overlay.h"

#include "plibs.h"
#include "poverlay.h"
Expand All @@ -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);
}

Expand All @@ -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() {
Expand All @@ -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)) {
Expand All @@ -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) {
Expand All @@ -120,15 +124,15 @@ 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 */
}

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() {
Expand Down
30 changes: 3 additions & 27 deletions src/psync/poverlay.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,15 @@
#ifndef PCLOUD_PSYNC_POVERLAY_H_
#define PCLOUD_PSYNC_POVERLAY_H_

#include "pcloudcc/psync/overlay.h"

#ifdef __cplusplus
extern "C" {
#endif

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
Expand All @@ -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();
Expand All @@ -66,7 +42,7 @@ int psync_ovr_callbacks_running();
void init_overlay_callbacks();

#ifdef __cplusplus
}
} /* extern "C" */
#endif

#endif /* PCLOUD_PSYNC_POVERLAY_H_ */

0 comments on commit a39ceeb

Please sign in to comment.