Skip to content

Commit 80204fb

Browse files
author
Ivan Stoev
committed
new
1 parent 78ebda4 commit 80204fb

File tree

14 files changed

+192
-166
lines changed

14 files changed

+192
-166
lines changed

pCloudCC/control_tools.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ int daemonize(bool do_commands) {
104104
close(STDOUT_FILENO);
105105
close(STDERR_FILENO);
106106

107-
if (console_client::clibrary::pclsync_lib::init())
107+
if (console_client::clibrary::pclsync_lib::get_lib().init())
108108
exit(EXIT_FAILURE);
109109
while (1) {
110110
sleep(10);

pCloudCC/lib/pclsync/poverlay.c

Lines changed: 9 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -55,57 +55,6 @@ poverlay_callback * callbacks;
5555
static int callbacks_size = 15;
5656
static const int calbacks_lower_band = 20;
5757

58-
/* disable cache, psync_path_status_get() has many layers of caching internally
59-
60-
#define CACHE_PREF "P_OVERLA_CACHE_PREFIX"
61-
#define CACHE_PREF_LEN 21
62-
63-
64-
typedef struct overlay_cache_{
65-
external_status stat;
66-
uint64_t timestamp;
67-
} overlay_cache_t;
68-
69-
static int get_item_from_cache(const char* key, external_status* stat){
70-
int ketlen = strlen(key);
71-
char* reckey = psync_malloc(ketlen + CACHE_PREF_LEN + 1);
72-
overlay_cache_t * rec = NULL;
73-
uint64_t now = 0;
74-
75-
memcpy(reckey, CACHE_PREF, CACHE_PREF_LEN);
76-
memcpy(reckey + CACHE_PREF_LEN , key, ketlen + 1);
77-
78-
if ((rec = (overlay_cache_t *) psync_cache_get(reckey))) {
79-
now = psync_millitime();
80-
if ((now - rec->timestamp) < 100) {
81-
*stat = rec->stat;
82-
psync_cache_add(reckey, rec, 1, psync_free, 1);
83-
psync_free(reckey);
84-
return 1;
85-
} else {
86-
psync_free(rec);
87-
}
88-
}
89-
psync_free(reckey);
90-
return 0;
91-
}
92-
93-
static void add_item_to_cache(const char* key, external_status* stat){
94-
int ketlen = strlen(key);
95-
char* reckey = psync_malloc(ketlen + CACHE_PREF_LEN + 1);
96-
overlay_cache_t * rec = psync_malloc(sizeof(overlay_cache_t));
97-
98-
strncpy(reckey, CACHE_PREF, CACHE_PREF_LEN);
99-
strncpy(reckey + CACHE_PREF_LEN , key, ketlen);
100-
101-
rec->stat = *stat;
102-
rec->timestamp = psync_millitime();
103-
psync_cache_add(reckey, rec, 1, psync_free, 1);
104-
psync_free(reckey);
105-
}
106-
107-
*/
108-
10958
int psync_add_overlay_callback(int id, poverlay_callback callback)
11059
{
11160
poverlay_callback * callbacks_old = callbacks;
@@ -148,29 +97,6 @@ void get_answer_to_request(message *request, message *replay)
14897
replay->length=sizeof(message)+4;
14998
//debug(D_NOTICE, "Client Request type [%u] len [%lu] string: [%s]", request->type, request->length, request->value);
15099
if (request->type < 20 ) {
151-
152-
/* if (overlays_running && !get_item_from_cache(request->value, &stat)) {
153-
stat = do_psync_external_status(request->value);
154-
}
155-
156-
if (stat == INSYNC) {
157-
replay->type = 10;
158-
}
159-
else if (stat == NOSYNC) {
160-
replay->type = 11;
161-
}
162-
else if (stat == INPROG) {
163-
164-
replay->type = 12;
165-
}
166-
else {
167-
replay->type = 13;
168-
memcpy(msg, "No.", 4);
169-
}
170-
replay->length = sizeof(message)+4;
171-
strncpy(replay->value, msg, 4);
172-
add_item_to_cache(request->value, &stat);
173-
*/
174100
if (overlays_running)
175101
stat=psync_path_status_get(request->value);
176102
switch (psync_path_status_get_status(stat)) {
@@ -189,11 +115,18 @@ void get_answer_to_request(message *request, message *replay)
189115
replay->type=13;
190116
memcpy(replay->value, "No.", 4);
191117
}
192-
} else if ((callbacks_running)&&(request->type < 36)) {
118+
} else if ((callbacks_running)&&(request->type < (calbacks_lower_band + callbacks_size))) {
193119
int ind = request->type - 20;
194120
int ret = 0;
121+
message *rep = NULL;
122+
195123
if (callbacks[ind]) {
196-
if ((ret = callbacks[ind](request->value)) == 0) {
124+
if ((ret = callbacks[ind](request->value, rep)) == 0) {
125+
if (rep) {
126+
psync_free(replay);
127+
replay = rep;
128+
}
129+
else
197130
replay->type = 0;
198131
} else {
199132
replay->type = ret;

pCloudCC/lib/pclsync/poverlay.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,5 @@ int psync_ovr_callbacks_running();
3333
void init_overlay_callbacks();
3434
int psync_add_overlay_callback(int id, poverlay_callback callback);
3535

36-
3736
#endif // POVERLAY_H
3837

pCloudCC/lib/pclsync/poverlay_lin.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void instance_thread(void* lpvParam)
116116
request = (message *)chbuf;
117117
if (request) {
118118
get_answer_to_request(request, reply);
119-
if (reply ) {
119+
if (reply) {
120120
rc = write(*cl,reply,reply->length);
121121
if (rc != reply->length)
122122
debug(D_ERROR,"Unix socket reply not sent.");
@@ -126,6 +126,7 @@ void instance_thread(void* lpvParam)
126126
if (cl) {
127127
close(*cl);
128128
}
129+
psync_free(reply);
129130
//debug(D_NOTICE, "InstanceThread exitting.\n");
130131
return;
131132
};

pCloudCC/lib/pclsync/poverlay_mac.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ void instance_thread(void* lpvParam)
133133
if (cl) {
134134
close(*cl);
135135
}
136+
psync_free(reply);
136137
//debug(D_NOTICE, "InstanceThread exitting.\n");
137138
return;
138139
};

pCloudCC/lib/pclsync/psynclib.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,3 +1865,10 @@ int psync_setlanguage(const char *language, char **err){
18651865
binparam params[]={P_STR("language", language)};
18661866
return run_command("setlanguage", params, err);
18671867
}
1868+
1869+
char * psync_get_token()
1870+
{
1871+
if (psync_my_auth[0])
1872+
return psync_strdup(psync_my_auth);
1873+
else return NULL;
1874+
}

pCloudCC/lib/pclsync/psynclib.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ void psync_get_folder_ownerid(psync_folderid_t folderid, psync_userid_t* /*OUT*/
11571157
/* Callback to be registered to be called from file manager extension.
11581158
*/
11591159

1160-
typedef int (*poverlay_callback)(const char* path);
1160+
typedef int (*poverlay_callback)(const char* path, void *rep);
11611161

11621162
/* Registers file manager extension callback that will be called when packet with id equals to the give one had arrived from extension.
11631163
* 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.
@@ -1181,6 +1181,8 @@ void psync_update_cryptostatus();
11811181
// Checks and creates new folder with write permissions on it and adds suffix to the name if necessary i.e. New Folder (1) etc..
11821182
psync_folderid_t psync_check_and_create_folder (const char * path);
11831183

1184+
char * psync_get_token();
1185+
11841186
#ifdef __cplusplus
11851187
}
11861188
#endif

pCloudCC/lib/poverlay_linux/overlay_client.c

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,17 @@ uint64_t length;
4343
char value[];
4444
} message;
4545

46-
46+
static void read_x_bytes(int socket, unsigned int x, void * buffer){
47+
int bytesRead = 0;
48+
int result;
49+
while (bytesRead < x) {
50+
result = read(socket, buffer + bytesRead, x - bytesRead);
51+
if (result < 1 ) {
52+
return;
53+
}
54+
bytesRead += result;
55+
}
56+
}
4757

4858
#if defined(P_OS_MACOS)
4959
uint32_t clport = 8989 ;
@@ -71,7 +81,7 @@ int QueryState( pCloud_FileState *state, char * path) {
7181
free (errm);
7282
return 0 ;
7383
}
74-
int SendCall( int id /*IN*/ ,const char * path /*IN*/ , int * ret /*OUT*/ , char ** errm /*OUT*/ ) {
84+
int SendCall( int id /*IN*/ ,const char * path /*IN*/ , int * ret /*OUT*/ , void * out /*OUT*/ ) {
7585
#if defined(P_OS_MACOS)
7686
struct sockaddr_in addr;
7787
#else
@@ -83,7 +93,8 @@ int SendCall( int id /*IN*/ ,const char * path /*IN*/ , int * ret /*OUT*/ , char
8393
int mess_size = sizeof ( message )+path_size + 1 ;
8494
int bytes_writen = 0 ;
8595
char *curbuf = NULL ;
86-
char buf[ POVERLAY_BUFSIZE ];
96+
char *buf = NULL;
97+
uint32_t bufflen=0;
8798
char sendbuf[mess_size];
8899
int bytes_read = 0 ;
89100
message *rep = NULL ;
@@ -111,8 +122,8 @@ int SendCall( int id /*IN*/ ,const char * path /*IN*/ , int * ret /*OUT*/ , char
111122
}
112123
#else
113124
if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0 )) == - 1 ) {
114-
if (errm)
115-
*errm = strndup( "Unable to create UNIX socket" , 27 );
125+
if (out)
126+
out = (void *)strndup( "Unable to create UNIX socket" , 27 );
116127
*ret = - 3 ;
117128
return - 3 ;
118129
}
@@ -121,8 +132,8 @@ int SendCall( int id /*IN*/ ,const char * path /*IN*/ , int * ret /*OUT*/ , char
121132
strncpy(addr.sun_path, clsoc, sizeof (addr.sun_path)- 1 );
122133

123134
if (connect(fd, ( struct sockaddr*)&addr,SUN_LEN(&addr)) == - 1 ) {
124-
if (errm)
125-
*errm = strndup( "Unable to connect to UNIX socket" , 32 );
135+
if (out)
136+
out = (void *)strndup( "Unable to connect to UNIX socket" , 32 );
126137
*ret = - 4 ;
127138
return - 4 ;
128139
}
@@ -140,28 +151,31 @@ int SendCall( int id /*IN*/ ,const char * path /*IN*/ , int * ret /*OUT*/ , char
140151
}
141152
debug ( D_NOTICE , "QueryState bytes send[%d]\n" , bytes_writen);
142153
if (bytes_writen != mes-> length ) {
143-
if (errm)
144-
*errm = strndup ( "Communication error" , 19 );
154+
if (out)
155+
out = strndup ( "Communication error" , 19 );
145156
close(fd);
146157
*ret = - 5 ;
147158
return - 5 ;
148159
}
149-
150-
curbuf = buf;
151-
while ( (rc= read (fd,curbuf,( POVERLAY_BUFSIZE - bytes_read))) > 0 ) {
152-
bytes_read += rc;
153-
curbuf = curbuf + rc;
154-
if (bytes_read > 12 ){
155-
rep = ( message *)buf;
156-
if (rep-> length == bytes_read)
157-
break ;
158-
}
160+
161+
162+
163+
read_x_bytes(fd, 4, &bufflen);
164+
if (bufflen <= 0)
165+
{
166+
debug ( D_NOTICE , "Message size could not be read![%d]\n" , bufflen);
167+
return -6;
159168
}
169+
buf = (char *)malloc(bufflen);
160170
rep = ( message *)buf;
171+
rep->length = bufflen;
172+
173+
read_x_bytes(fd, bufflen - 4, buf + 4);
174+
161175

162176
*ret = rep-> type ;
163-
if (errm)
164-
*errm = strndup (rep-> value , rep-> length - sizeof ( message ));
177+
if (out)
178+
out = strndup (rep-> value , rep-> length - sizeof ( message ));
165179

166180
close(fd);
167181

@@ -192,6 +206,9 @@ int main ( int arc, char **argv ){
192206
printf( "Call 21 returned %d msg %s \n" , j, errm);
193207
SendCall( 22 , argv[i], &j, &errm);
194208
printf( "Call 22 returned %d msg %s \n" , j, errm);
209+
210+
SendCall( 23 , argv[i], &j, &errm);
211+
printf( "Call 22 returned %d msg %s \n" , j, errm);
195212
}
196213
return 0 ;
197214
}

pCloudCC/lib/poverlay_linux/overlay_client.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
#ifndef OVERLAY_CLIENT_H
2828
#define OVERLAY_CLIENT_H
2929

30-
#define P_OS_MACOS
30+
//#define P_OS_MACOS
31+
#define PCLOUD_TESTING
3132
#ifdef __cplusplus
3233
extern "C" {
3334
#endif
@@ -42,7 +43,7 @@ typedef enum _pCloud_FileState
4243

4344
int QueryState(pCloud_FileState *state /*OUT*/, char* path /*IN*/);
4445

45-
int SendCall(int id /*IN*/, const char* path /*IN*/, int* ret /*OUT*/, char** errm /*OUT*/);
46+
int SendCall( int id /*IN*/ ,const char * path /*IN*/ , int * ret /*OUT*/ , void * out /*OUT*/ );
4647
#ifdef __cplusplus
4748
}
4849
#endif

pCloudCC/main.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ int main(int argc, char **argv) {
6262
std::cout << "Username option is required!!!" << "\n";
6363
return 1;
6464
}
65-
console_client::clibrary::pclsync_lib::get_lib().username_ = username;
65+
console_client::clibrary::pclsync_lib::get_lib().set_username(username);
6666

6767
if (passwordsw) {
6868
console_client::clibrary::pclsync_lib::get_lib().get_pass_from_console();
@@ -71,7 +71,7 @@ int main(int argc, char **argv) {
7171
if (crypto) {
7272
console_client::clibrary::pclsync_lib::get_lib().setup_crypto_ = true;
7373
if (vm.count("passascrypto"))
74-
console_client::clibrary::pclsync_lib::get_lib().crypto_pass_ = password;
74+
console_client::clibrary::pclsync_lib::get_lib().set_crypto_pass(password) ;
7575
else {
7676
std::cout << "Enter crypto password." << "\n";
7777
console_client::clibrary::pclsync_lib::get_lib().get_cryptopass_from_console();
@@ -80,11 +80,11 @@ int main(int argc, char **argv) {
8080
console_client::clibrary::pclsync_lib::get_lib().setup_crypto_ = false;
8181

8282
if (vm.count("mountpoint"))
83-
console_client::clibrary::pclsync_lib::get_lib().mount_ = vm["mountpoint"].as<std::string>();
83+
console_client::clibrary::pclsync_lib::get_lib().set_mount( vm["mountpoint"].as<std::string>());
8484

8585
console_client::clibrary::pclsync_lib::get_lib().newuser_ = newuser;
86-
console_client::clibrary::pclsync_lib::get_lib().save_pass_ = save_pass;
87-
console_client::clibrary::pclsync_lib::get_lib().daemon_ = daemon;
86+
console_client::clibrary::pclsync_lib::get_lib().set_savepass(save_pass);
87+
console_client::clibrary::pclsync_lib::get_lib().set_daemon(daemon);
8888
}
8989
catch(std::exception& e) {
9090
std::cerr << "error: " << e.what() << "\n";
@@ -100,7 +100,7 @@ int main(int argc, char **argv) {
100100
else {
101101
if (commands)
102102
std::cout << "Option commnads/o ignored." << "\n";
103-
if (!console_client::clibrary::pclsync_lib::init())
103+
if (!console_client::clibrary::pclsync_lib::get_lib().init())
104104
sleep(360000);
105105
}
106106

0 commit comments

Comments
 (0)