Code cleanup and readline for better CLI UX (#116)

* pcommands.h for command ids

* remove compiler compat junk

* transient and unnecessary includes resolved

* unnecessary goto

* function naming and whatnot

* use readline for better command line UX

* add readline to CI workflow
This commit is contained in:
Levi Neely 2025-03-09 09:14:12 +01:00 committed by GitHub
parent 85e9eb3e77
commit 8ad1fe6086
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
30 changed files with 380 additions and 421 deletions

View File

@ -12,6 +12,6 @@ jobs:
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- name: make - name: make
run: sudo apt-get update && sudo apt-get install -y libudev-dev libfuse-dev libsqlite3-dev zlib1g-dev libboost-dev libboost-system-dev libboost-program-options-dev libmbedtls-dev run: sudo apt-get update && sudo apt-get install -y libreadline-dev libudev-dev libfuse-dev libsqlite3-dev zlib1g-dev libboost-dev libboost-system-dev libboost-program-options-dev libmbedtls-dev
- name: make - name: make
run: make run: make

View File

@ -7,7 +7,7 @@ ifneq (,$(filter clang%,$(CC)))
CFLAGS += -Wthread-safety CFLAGS += -Wthread-safety
endif endif
CXXFLAGS = $(CFLAGS) CXXFLAGS = $(CFLAGS)
LIBLDFLAGS = $(COMMONFLAGS) -lpthread -ludev -lsqlite3 -lz -l:libmbedtls.so.14 -l:libmbedx509.so.1 -l:libmbedcrypto.so.7 LIBLDFLAGS = $(COMMONFLAGS) -lreadline -lpthread -ludev -lsqlite3 -lz -l:libmbedtls.so.14 -l:libmbedx509.so.1 -l:libmbedcrypto.so.7
EXECLDFLAGS = $(COMMONFLAGS) -lboost_program_options -lfuse EXECLDFLAGS = $(COMMONFLAGS) -lboost_program_options -lfuse
SCAN := 0 SCAN := 0

View File

@ -36,6 +36,8 @@
#include <sys/types.h> #include <sys/types.h>
#include <syslog.h> #include <syslog.h>
#include <unistd.h> #include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "control_tools.h" #include "control_tools.h"
@ -43,6 +45,7 @@
#include "pclsync/pshm.h" #include "pclsync/pshm.h"
#include "pclsync/pfoldersync.h" #include "pclsync/pfoldersync.h"
#include "pclsync/rpcclient.h" #include "pclsync/rpcclient.h"
#include "pclsync/pcommands.h"
#include "CLI11.hpp" #include "CLI11.hpp"
@ -52,15 +55,6 @@ namespace control_tools {
static const int STOP = 0; static const int STOP = 0;
enum command_ids_ {
STARTCRYPTO = 20,
STOPCRYPTO,
FINALIZE,
LISTSYNC,
ADDSYNC,
STOPSYNC
};
int list_sync_folders() { int list_sync_folders() {
int ret; int ret;
char *errm; char *errm;
@ -235,7 +229,7 @@ int finalize() {
return ret; return ret;
} }
void help() { static void help() {
std::cout << "Supported commands are:" << std::endl std::cout << "Supported commands are:" << std::endl
<< " help(?): Show this help message" << std::endl << " help(?): Show this help message" << std::endl
<< " crypto(c):" << std::endl << " crypto(c):" << std::endl
@ -249,6 +243,45 @@ void help() {
<< " quit(q): Exit this program" << std::endl; << " quit(q): Exit this program" << std::endl;
} }
static char* command_generator(const char* text, int state) {
static int list_index, len;
static const char* commands[] = {
"help", "?",
"crypto", "crypto start", "crypto stop",
"c", "c start", "c stop",
"sync", "sync ls", "sync add", "sync remove", "sync rm",
"s", "s ls", "s add", "s remove", "s rm",
"finalize", "f",
"quit", "q",
nullptr
};
// If this is a new word to complete, initialize
if (!state) {
list_index = 0;
len = strlen(text);
}
// Return the next name which partially matches
while (const char* command = commands[list_index++]) {
if (strncmp(command, text, len) == 0) {
return strdup(command); // Caller frees this with free()
}
}
return nullptr; // No more matches
}
static char** command_completion(const char* text, int start, int end) {
// If this is the start of line, complete commands
if (start == 0) {
return rl_completion_matches(text, command_generator);
}
// Otherwise, don't complete
return nullptr;
}
void process_commands() { void process_commands() {
CLI::App app{"pcloudcc-lneely"}; CLI::App app{"pcloudcc-lneely"};
app.fallthrough(); app.fallthrough();
@ -298,12 +331,19 @@ void process_commands() {
sync_remove_cmd->add_option("folderid", syncrm_fid, "Folder ID")->required(); sync_remove_cmd->add_option("folderid", syncrm_fid, "Folder ID")->required();
sync_remove_cmd->callback([&] { remove_sync_folder(syncrm_fid.c_str()); }); sync_remove_cmd->callback([&] { remove_sync_folder(syncrm_fid.c_str()); });
using_history();
// command loop // command loop
rl_attempted_completion_function = command_completion;
while (true) { while (true) {
std::cout << "pcloud> "; char* line_read = readline("pcloud> ");
std::string line; if (!line_read) break;
if (!std::getline(std::cin, line)) if (line_read[0]) {
break; add_history(line_read);
}
std::string line(line_read);
free(line_read);
try { try {
app.parse(line); app.parse(line);
} catch (const CLI::ParseError &e) { } catch (const CLI::ParseError &e) {

8
pclsync/pcommands.h Normal file
View File

@ -0,0 +1,8 @@
enum command_ids_ {
STARTCRYPTO = 20,
STOPCRYPTO,
FINALIZE,
LISTSYNC,
ADDSYNC,
STOPSYNC
};

View File

@ -32,132 +32,20 @@
#ifndef _PSYNC_COMPILER_H #ifndef _PSYNC_COMPILER_H
#define _PSYNC_COMPILER_H #define _PSYNC_COMPILER_H
#if defined(_MSC_VER)
#include <mmintrin.h>
#endif
#if !defined(__has_attribute)
#if defined(__GNUC__)
#define __has_attribute(x) 1
#else
#define __has_attribute(x) 0
#endif
#else
#if defined(__GNUC__) && !__has_attribute(malloc)
#undef __has_attribute
#define __has_attribute(x) 1
#endif
#endif
#ifndef __has_builtin
#if defined(__GNUC__)
#define __has_builtin(x) 1
#else
#define __has_builtin(x) 0
#endif
#endif
#if __has_builtin(__builtin_expect)
#define likely(expr) __builtin_expect(!!(expr), 1) #define likely(expr) __builtin_expect(!!(expr), 1)
#define unlikely(expr) __builtin_expect(!!(expr), 0) #define unlikely(expr) __builtin_expect(!!(expr), 0)
#else
#define likely(expr) (expr)
#define unlikely(expr) (expr)
#endif
#if __has_builtin(__builtin_prefetch)
#define psync_prefetch(expr) __builtin_prefetch(expr) #define psync_prefetch(expr) __builtin_prefetch(expr)
#elif defined(_MSC_VER)
#define psync_prefetch(expr) _mm_prefetch((char *)(expr), _MM_HINT_T0)
#else
#define psync_prefetch(expr) ((void)0)
#endif
#if defined(_MSC_VER)
#define PSYNC_THREAD __declspec(thread)
#define PSYNC_NOINLINE __declspec(noinline)
#else
#if __has_attribute(noinline)
#define PSYNC_NOINLINE __attribute__((noinline)) #define PSYNC_NOINLINE __attribute__((noinline))
#else
#define PSYNC_NOINLINE
#endif
#define PSYNC_THREAD __thread #define PSYNC_THREAD __thread
#endif
#if __has_attribute(malloc)
#define PSYNC_MALLOC __attribute__((malloc)) #define PSYNC_MALLOC __attribute__((malloc))
#else
#define PSYNC_MALLOC
#endif
#if __has_attribute(sentinel)
#define PSYNC_SENTINEL __attribute__((sentinel)) #define PSYNC_SENTINEL __attribute__((sentinel))
#else
#define PSYNC_SENTINEL
#endif
#if __has_attribute(pure)
#define PSYNC_PURE __attribute__((pure)) #define PSYNC_PURE __attribute__((pure))
#else
#define PSYNC_PURE
#endif
#if __has_attribute(const)
#define PSYNC_CONST __attribute__((const)) #define PSYNC_CONST __attribute__((const))
#else
#define PSYNC_CONST
#endif
#if __has_attribute(cold)
#define PSYNC_COLD __attribute__((cold)) #define PSYNC_COLD __attribute__((cold))
#else
#define PSYNC_COLD
#endif
#if __has_attribute(format)
#define PSYNC_FORMAT(a, b, c) __attribute__((format(a, b, c))) #define PSYNC_FORMAT(a, b, c) __attribute__((format(a, b, c)))
#else
#define PSYNC_FORMAT(a, b, c)
#endif
#if __has_attribute(nonnull)
#define PSYNC_NONNULL(...) __attribute__((nonnull(__VA_ARGS__))) #define PSYNC_NONNULL(...) __attribute__((nonnull(__VA_ARGS__)))
#else
#define PSYNC_NONNULL(...)
#endif
#if __has_attribute(packed)
#define PSYNC_PACKED_STRUCT struct __attribute__((packed)) #define PSYNC_PACKED_STRUCT struct __attribute__((packed))
#elif defined(_MSC_VER)
#define PSYNC_PACKED_STRUCT __declspec(align(1)) struct
#else
#define PSYNC_PACKED_STRUCT struct
#endif
#if _MSC_VER >= 1500 && _MSC_VER < 1600
#define inline __inline
#define restrict __restrict
#elif __GNUC__ >= 3
#define inline __inline
#define restrict __restrict
#elif __STDC_VERSION__ != 199901L
#define inline
#define restrict
#endif
#if defined(__clang__) || defined(_MSC_VER)
#define psync_alignof __alignof
#elif defined(__GNUC__)
#define psync_alignof __alignof__ #define psync_alignof __alignof__
#else
#define psync_alignof(t) \
offsetof( \
struct { \
char a; \
t b; \
}, \
b)
#endif
#endif #endif

File diff suppressed because it is too large Load Diff

View File

@ -32,10 +32,14 @@
#ifndef __PCRYPTOFOLDER_H #ifndef __PCRYPTOFOLDER_H
#define __PCRYPTOFOLDER_H #define __PCRYPTOFOLDER_H
#ifdef __cplusplus
extern "C" {
#endif
#include "papi.h" #include "papi.h"
#include "pcrypto.h" #include "pcrypto.h"
#include "pfoldersync.h"
#include "pfsfolder.h" #include "pfsfolder.h"
#include "psynclib.h"
#define PSYNC_CRYPTO_SYM_FLAG_ISDIR 1 #define PSYNC_CRYPTO_SYM_FLAG_ISDIR 1
@ -74,4 +78,8 @@ int pcryptofolder_reset();
int pcryptofolder_setup(const char *password, const char *hint); int pcryptofolder_setup(const char *password, const char *hint);
int pcryptofolder_unlock(const char *password); int pcryptofolder_unlock(const char *password);
#ifdef __cplusplus
}
#endif
#endif #endif

View File

@ -38,6 +38,7 @@
#include <mbedtls/ssl.h> #include <mbedtls/ssl.h>
#include <pthread.h> #include <pthread.h>
#include <stddef.h> #include <stddef.h>
#include <unistd.h>
#include "paccountevents.h" #include "paccountevents.h"
#include "papi.h" #include "papi.h"

View File

@ -31,6 +31,7 @@
#include <errno.h> #include <errno.h>
#include <pthread.h> #include <pthread.h>
#include <stddef.h> #include <stddef.h>
#include <sys/stat.h>
#include "pqevent.h" #include "pqevent.h"
#include "pdownload.h" #include "pdownload.h"
@ -48,6 +49,7 @@
#include "ptimer.h" #include "ptimer.h"
#include "pupload.h" #include "pupload.h"
#include "ppath.h" #include "ppath.h"
#include "pfile.h"
extern const unsigned char pfile_invalid_chars[]; extern const unsigned char pfile_invalid_chars[];

View File

@ -33,7 +33,7 @@
#define _PSYNC_DOWNLOAD_H #define _PSYNC_DOWNLOAD_H
#include "psettings.h" #include "psettings.h"
#include "psynclib.h" #include "pfoldersync.h"
typedef unsigned char psync_hex_hash[PSYNC_HASH_DIGEST_HEXLEN]; typedef unsigned char psync_hex_hash[PSYNC_HASH_DIGEST_HEXLEN];

View File

@ -42,6 +42,7 @@
#include "pnetlibs.h" #include "pnetlibs.h"
#include "psys.h" #include "psys.h"
#include "ppath.h" #include "ppath.h"
#include "pfile.h"
#include <stddef.h> #include <stddef.h>
#include <string.h> #include <string.h>

View File

@ -41,7 +41,7 @@
#include "pintervaltree.h" #include "pintervaltree.h"
#include "plibs.h" #include "plibs.h"
#include "psettings.h" #include "psettings.h"
#include "psynclib.h" #include "pfoldersync.h"
#include "ptimer.h" #include "ptimer.h"
#include "ptree.h" #include "ptree.h"

View File

@ -36,9 +36,9 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include "pfoldersync.h"
#include "pfsfolder.h" #include "pfsfolder.h"
#include "plibs.h" #include "plibs.h"
#include "psynclib.h"
#include "ptree.h" #include "ptree.h"
#define PSYNC_FS_TASK_MKDIR 1 #define PSYNC_FS_TASK_MKDIR 1

View File

@ -42,6 +42,7 @@
#include "pcache.h" #include "pcache.h"
#include "pdiff.h" #include "pdiff.h"
#include "pfile.h"
#include "pfileops.h" #include "pfileops.h"
#include "pfscrypto.h" #include "pfscrypto.h"
#include "pfstasks.h" #include "pfstasks.h"

View File

@ -32,7 +32,7 @@
#ifndef _PSYNC_FSUPLOAD_H #ifndef _PSYNC_FSUPLOAD_H
#define _PSYNC_FSUPLOAD_H #define _PSYNC_FSUPLOAD_H
#include "psynclib.h" #include <stdint.h>
void psync_fsupload_init(); void psync_fsupload_init();
void psync_fsupload_wake(); void psync_fsupload_wake();

View File

@ -32,8 +32,9 @@
#ifndef _PSYNC_FSXATTR_H #ifndef _PSYNC_FSXATTR_H
#define _PSYNC_FSXATTR_H #define _PSYNC_FSXATTR_H
#include "pfile.h" #include <stddef.h>
#include "psynclib.h"
#include "pfoldersync.h"
#define PFS_XATTR_IGN #define PFS_XATTR_IGN

View File

@ -42,6 +42,8 @@
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <sys/stat.h>
#include "pfile.h"
// required by psync_debug // required by psync_debug
extern PSYNC_THREAD const char *psync_thread_name; extern PSYNC_THREAD const char *psync_thread_name;

View File

@ -35,10 +35,12 @@
#include <pthread.h> #include <pthread.h>
#include "pcompiler.h" #include "pcompiler.h"
#include "pstatus.h"
#include "putil.h" #include "putil.h"
#include "psynclib.h" #include "psynclib.h" // need for macros...
#include <sqlite3.h> #include <sqlite3.h>
#include <stdint.h>
#include <string.h> #include <string.h>
#define D_NONE 0 #define D_NONE 0

View File

@ -32,7 +32,7 @@
#ifndef _PSYNC_LOCALNOTIFY_H #ifndef _PSYNC_LOCALNOTIFY_H
#define _PSYNC_LOCALNOTIFY_H #define _PSYNC_LOCALNOTIFY_H
#include "psynclib.h" #include "pfoldersync.h"
int psync_localnotify_init(); int psync_localnotify_init();
void psync_localnotify_add_sync(psync_syncid_t syncid); void psync_localnotify_add_sync(psync_syncid_t syncid);

View File

@ -32,7 +32,7 @@
#ifndef _PSYNC_LOCALSCAN_H #ifndef _PSYNC_LOCALSCAN_H
#define _PSYNC_LOCALSCAN_H #define _PSYNC_LOCALSCAN_H
#include "psynclib.h" #include "pfoldersync.h"
void psync_localscan_init(); void psync_localscan_init();
void psync_wake_localscan(); void psync_wake_localscan();

View File

@ -36,13 +36,6 @@
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/debug.h>
#include <mbedtls/entropy.h>
#include <mbedtls/pkcs5.h>
#include <mbedtls/sha1.h>
#include <mbedtls/ssl.h>
#include "papi.h" #include "papi.h"
#include "pcache.h" #include "pcache.h"
#include "pdevice.h" #include "pdevice.h"
@ -56,6 +49,7 @@
#include "ppath.h" #include "ppath.h"
#include "psys.h" #include "psys.h"
#include "ptree.h" #include "ptree.h"
#include "pfile.h"
// required by psync_send_debug // required by psync_send_debug
extern PSYNC_THREAD const char *psync_thread_name; extern PSYNC_THREAD const char *psync_thread_name;

View File

@ -33,9 +33,8 @@
#define _PSYNC_NETLIBS_H #define _PSYNC_NETLIBS_H
#include "papi.h" #include "papi.h"
#include "pfile.h"
#include "plist.h" #include "plist.h"
#include "psynclib.h" #include "pfoldersync.h"
#define senddebug(str, ...) \ #define senddebug(str, ...) \
do { \ do { \

View File

@ -33,10 +33,10 @@
#include "plibs.h" #include "plibs.h"
#include "pnetlibs.h" #include "pnetlibs.h"
#include "psettings.h" #include "psettings.h"
#include "ptimer.h"
#include "ptree.h" #include "ptree.h"
#include "ppath.h" #include "ppath.h"
#include "prun.h" #include "prun.h"
#include "pfile.h"
typedef struct { typedef struct {

View File

@ -49,6 +49,7 @@
#include "ppath.h" #include "ppath.h"
#include "psys.h" #include "psys.h"
#include "putil.h" #include "putil.h"
#include "pfile.h"
#include "pstatus.h" #include "pstatus.h"
#include "ptimer.h" #include "ptimer.h"

View File

@ -50,33 +50,27 @@
#define PSYNC_SHA1_DIGEST_LEN 20 #define PSYNC_SHA1_DIGEST_LEN 20
#define PSYNC_SHA1_DIGEST_HEXLEN 40 #define PSYNC_SHA1_DIGEST_HEXLEN 40
#define psync_sha1_ctx mbedtls_sha1_context #define psync_sha1_ctx mbedtls_sha1_context
#define psync_sha1(data, datalen, checksum) \ #define psync_sha1(data, datalen, checksum) mbedtls_sha1(data, datalen, checksum)
mbedtls_sha1(data, datalen, checksum)
#define psync_sha1_init(pctx) mbedtls_sha1_starts(pctx) #define psync_sha1_init(pctx) mbedtls_sha1_starts(pctx)
#define psync_sha1_update(pctx, data, datalen) \ #define psync_sha1_update(pctx, data, datalen) mbedtls_sha1_update(pctx, (const unsigned char *)data, datalen)
mbedtls_sha1_update(pctx, (const unsigned char *)data, datalen)
#define psync_sha1_final(checksum, pctx) mbedtls_sha1_finish(pctx, checksum) #define psync_sha1_final(checksum, pctx) mbedtls_sha1_finish(pctx, checksum)
#define PSYNC_SHA256_BLOCK_LEN 64 #define PSYNC_SHA256_BLOCK_LEN 64
#define PSYNC_SHA256_DIGEST_LEN 32 #define PSYNC_SHA256_DIGEST_LEN 32
#define PSYNC_SHA256_DIGEST_HEXLEN 64 #define PSYNC_SHA256_DIGEST_HEXLEN 64
#define psync_sha256_ctx mbedtls_sha256_context #define psync_sha256_ctx mbedtls_sha256_context
#define psync_sha256(data, datalen, checksum) \ #define psync_sha256(data, datalen, checksum) mbedtls_sha256(data, datalen, checksum, 0)
mbedtls_sha256(data, datalen, checksum, 0)
#define psync_sha256_init(pctx) mbedtls_sha256_starts(pctx, 0) #define psync_sha256_init(pctx) mbedtls_sha256_starts(pctx, 0)
#define psync_sha256_update(pctx, data, datalen) \ #define psync_sha256_update(pctx, data, datalen) mbedtls_sha256_update(pctx, (const unsigned char *)data, datalen)
mbedtls_sha256_update(pctx, (const unsigned char *)data, datalen)
#define psync_sha256_final(checksum, pctx) mbedtls_sha256_finish(pctx, checksum) #define psync_sha256_final(checksum, pctx) mbedtls_sha256_finish(pctx, checksum)
#define PSYNC_SHA512_BLOCK_LEN 128 #define PSYNC_SHA512_BLOCK_LEN 128
#define PSYNC_SHA512_DIGEST_LEN 64 #define PSYNC_SHA512_DIGEST_LEN 64
#define PSYNC_SHA512_DIGEST_HEXLEN 128 #define PSYNC_SHA512_DIGEST_HEXLEN 128
#define psync_sha512_ctx mbedtls_sha512_context #define psync_sha512_ctx mbedtls_sha512_context
#define psync_sha512(data, datalen, checksum) \ #define psync_sha512(data, datalen, checksum) mbedtls_sha512(data, datalen, checksum, 0)
mbedtls_sha512(data, datalen, checksum, 0)
#define psync_sha512_init(pctx) mbedtls_sha512_starts(pctx, 0) #define psync_sha512_init(pctx) mbedtls_sha512_starts(pctx, 0)
#define psync_sha512_update(pctx, data, datalen) \ #define psync_sha512_update(pctx, data, datalen) mbedtls_sha512_update(pctx, (const unsigned char *)data, datalen)
mbedtls_sha512_update(pctx, (const unsigned char *)data, datalen)
#define psync_sha512_final(checksum, pctx) mbedtls_sha512_finish(pctx, checksum) #define psync_sha512_final(checksum, pctx) mbedtls_sha512_finish(pctx, checksum)
typedef mbedtls_rsa_context *psync_rsa_t; typedef mbedtls_rsa_context *psync_rsa_t;

View File

@ -31,6 +31,10 @@
#ifndef _PSYNC_STATUS_H #ifndef _PSYNC_STATUS_H
#define _PSYNC_STATUS_H #define _PSYNC_STATUS_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h> #include <stdint.h>
#include "plist.h" #include "plist.h"
@ -155,4 +159,8 @@ void pstatus_get_cb(pstatus_t *status);
void pstatus_set_cb(pstatus_change_callback_t callback); void pstatus_set_cb(pstatus_change_callback_t callback);
void pstatus_send_status_update(); void pstatus_send_status_update();
#ifdef __cplusplus
}
#endif
#endif #endif

View File

@ -1055,3 +1055,30 @@ int ptask_download_needed_async(psync_fileid_t fileid,
task.request.cbext = cbext; task.request.cbext = cbext;
return task_send_async(&task, sizeof(task)); return task_send_async(&task, sizeof(task));
} }
void ptask_cfldr_save_fldrkey(void *ptr) {
insert_folder_key_task *t;
psync_sql_res *res;
t = (insert_folder_key_task *)ptr;
res = psync_sql_prep_statement(
"REPLACE INTO cryptofolderkey (folderid, enckey) VALUES (?, ?)");
psync_sql_bind_uint(res, 1, t->id);
psync_sql_bind_blob(res, 2, (const char *)t->key->data, t->key->datalen);
psync_sql_run_free(res);
psync_free(t->key);
psync_free(t);
}
void ptask_cfldr_save_filekey(void *ptr) {
insert_file_key_task *t;
psync_sql_res *res;
t = (insert_file_key_task *)ptr;
res = psync_sql_prep_statement(
"REPLACE INTO cryptofilekey (fileid, hash, enckey) VALUES (?, ?, ?)");
psync_sql_bind_uint(res, 1, t->id);
psync_sql_bind_uint(res, 2, t->hash);
psync_sql_bind_blob(res, 3, (const char *)t->key->data, t->key->datalen);
psync_sql_run_free(res);
psync_free(t->key);
psync_free(t);
}

View File

@ -32,15 +32,12 @@
#ifndef _PSYNC_TASKS_H #ifndef _PSYNC_TASKS_H
#define _PSYNC_TASKS_H #define _PSYNC_TASKS_H
#include "psynclib.h" #include "pfoldersync.h"
#include "pssl.h"
#define PSYNC_ASYNC_ERR_FLAG_PERM \ #define PSYNC_ASYNC_ERR_FLAG_PERM 0x01 // the error is permanent(ish) and there is no reason to retry
0x01 // the error is permanent(ish) and there is no reason to retry #define PSYNC_ASYNC_ERR_FLAG_RETRY_AS_IS 0x02 // same request may succeed in the future if retried as is
#define PSYNC_ASYNC_ERR_FLAG_RETRY_AS_IS \ #define PSYNC_ASYNC_ERR_FLAG_SUCCESS 0x04 // like no action performed because of no need - file already exists and so on
0x02 // same request may succeed in the future if retried as is
#define PSYNC_ASYNC_ERR_FLAG_SUCCESS \
0x04 // like no action performed because of no need - file already exists and
// so on
#define PSYNC_ASYNC_ERROR_NET 1 #define PSYNC_ASYNC_ERROR_NET 1
#define PSYNC_ASYNC_ERROR_FILE 2 #define PSYNC_ASYNC_ERROR_FILE 2
@ -119,6 +116,17 @@ typedef struct {
}; };
} psync_async_result_t; } psync_async_result_t;
typedef struct {
psync_encrypted_symmetric_key_t key;
psync_fileid_t id;
uint64_t hash;
} insert_file_key_task;
typedef struct {
psync_encrypted_symmetric_key_t key;
psync_folderid_t id;
} insert_folder_key_task;
typedef void (*psync_async_callback_t)(void *, psync_async_result_t *); typedef void (*psync_async_callback_t)(void *, psync_async_result_t *);
@ -154,4 +162,8 @@ void ptask_download_q(psync_syncid_t syncid, psync_fileid_t fileid, psync_folder
void ptask_upload(psync_syncid_t syncid, psync_fileid_t localfileid, const char *name); void ptask_upload(psync_syncid_t syncid, psync_fileid_t localfileid, const char *name);
void ptask_upload_q(psync_syncid_t syncid, psync_fileid_t localfileid, const char *name); void ptask_upload_q(psync_syncid_t syncid, psync_fileid_t localfileid, const char *name);
// crypto folder
void ptask_cfldr_save_fldrkey(void *ptr);
void ptask_cfldr_save_filekey(void *ptr);
#endif #endif

View File

@ -47,6 +47,7 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h>
char *ptools_get_mac_addr() { char *ptools_get_mac_addr() {
char buffer[128]; char buffer[128];

View File

@ -37,10 +37,12 @@
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include "pcryptofolder.h"
#include "prpc.h" #include "prpc.h"
#include "psynclib.h" #include "psynclib.h"
#include "pshm.h" #include "pshm.h"
#include "pdevice.h" #include "pdevice.h"
#include "pcommands.h"
#include "pclsync_lib.h" #include "pclsync_lib.h"
@ -199,28 +201,35 @@ void event_handler(psync_eventtype_t event, psync_eventdata_t eventdata) {
} }
static int lib_setup_cripto() { static int lib_setup_cripto() {
int ret = 0; const char *pwd = clib::pclsync_lib::get_lib().get_crypto_pass().c_str();
ret = psync_crypto_issetup();
if (ret) { if(pstatus_get(PSTATUS_TYPE_ONLINE) == PSTATUS_ONLINE_OFFLINE) {
ret = psync_crypto_start( std::cout << "Cannot unlock crypto folder, pcloudcc is offline" << std::endl;
clib::pclsync_lib::get_lib().get_crypto_pass().c_str()); return PSYNC_CRYPTO_CANT_CONNECT;
std::cout << "crypto is setup, login result=" << ret << std::endl;
} else {
std::cout << "crypto is not setup" << std::endl;
ret = psync_crypto_setup(
clib::pclsync_lib::get_lib().get_crypto_pass().c_str(), "no hint");
if (ret) {
std::cout << "crypto setup failed" << std::endl;
} else {
ret = psync_crypto_start(
clib::pclsync_lib::get_lib().get_crypto_pass().c_str());
std::cout << "crypto setup successful, start=" << ret << std::endl;
ret = psync_crypto_mkdir(0, "Crypto", NULL, NULL);
std::cout << "creating folder=" << ret << std::endl;
}
} }
if(!psync_crypto_issetup()) {
std::cout << "crypto is not setup, setting it up now..." << std::endl;
if(int ret = pcryptofolder_setup(pwd, "no hint") != PSYNC_CRYPTO_SETUP_SUCCESS) {
std::cout << "crypto setup failed, error code was " << ret << std::endl;
return ret;
}
if(int ret = pcryptofolder_mkdir(0, "Crypto", NULL, NULL) != PSYNC_CRYPTO_SUCCESS) {
std::cout << "failed to create crypto directory, error code was" << ret << std::endl;
return ret;
}
std::cout << "crypto folder was setup using the provided password, "
<< "you may want to change your password hint on the "
<< "pcloud website." << std::endl;
}
if(int ret = pcryptofolder_unlock(pwd) != PSYNC_CRYPTO_START_SUCCESS) {
std::cout << "Failed to unlock crypto folder: error code was " << ret << std::endl;
return ret;
}
clib::pclsync_lib::get_lib().crypto_on_ = true; clib::pclsync_lib::get_lib().crypto_on_ = true;
return ret; return 0;
} }
static const char *status2string(uint32_t status) { static const char *status2string(uint32_t status) {
@ -450,12 +459,12 @@ int clib::pclsync_lib::init() {
psync_free(username_old); psync_free(username_old);
} }
psync_overlay_register_callback(20, &clib::pclsync_lib::start_crypto); psync_overlay_register_callback(STARTCRYPTO, &clib::pclsync_lib::start_crypto);
psync_overlay_register_callback(21, &clib::pclsync_lib::stop_crypto); psync_overlay_register_callback(STOPCRYPTO, &clib::pclsync_lib::stop_crypto);
psync_overlay_register_callback(22, &clib::pclsync_lib::finalize); psync_overlay_register_callback(FINALIZE, &clib::pclsync_lib::finalize);
psync_overlay_register_callback(23, &clib::pclsync_lib::list_sync_folders); psync_overlay_register_callback(LISTSYNC, &clib::pclsync_lib::list_sync_folders);
psync_overlay_register_callback(24, &clib::pclsync_lib::add_sync_folder); psync_overlay_register_callback(ADDSYNC, &clib::pclsync_lib::add_sync_folder);
psync_overlay_register_callback(25, &clib::pclsync_lib::remove_sync_folder); psync_overlay_register_callback(STOPSYNC, &clib::pclsync_lib::remove_sync_folder);
return 0; return 0;
} }