-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpcallbacks.c
More file actions
596 lines (517 loc) · 17.8 KB
/
pcallbacks.c
File metadata and controls
596 lines (517 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/* Copyright (c) 2013-2014 Anton Titov.
* Copyright (c) 2013-2014 pCloud Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of pCloud Ltd nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL pCloud Ltd BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <string.h>
#include "pcallbacks.h"
#include "pcompat.h"
#include "plibs.h"
#include "plist.h"
#include "pfolder.h"
#include "prunratelimit.h"
#define MAX_STATUS_STR_LEN 64
#define DONT_SHOW_TIME_IF_SEC_OVER (2*86400)
#define DONT_SHOW_TIME_IF_SPEED_BELOW (4*1024)
static pthread_mutex_t statusmutex=PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t statuscond=PTHREAD_COND_INITIALIZER;
static int statuschanges=0;
static int statusthreadrunning=0;
static pthread_mutex_t eventmutex=PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t eventcond=PTHREAD_COND_INITIALIZER;
static psync_list eventlist;
static int eventthreadrunning=0;
static pstatus_t status_old={ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
//Time when last diff change data ewvent was sent.
time_t plasr_diff_devent_t = -1;
#define PDIFF_DATA_EVENT_DELAY 5
typedef struct {
psync_list list;
psync_eventdata_t data;
psync_eventtype_t event;
int freedata;
} event_list_t;
static char *cat_lstr(char *src, const char *app, size_t len){
return (char *)memcpy(src, app, len)+len;
}
static char *cat_str(char *src, const char *app){
return cat_lstr(src, app, strlen(app));
}
PSYNC_NOINLINE static char *cat_uint32l(char *src, uint32_t num){
char str[16];
uint32_t rem;
int unsigned len;
len=0;
do{
rem=num%10;
str[sizeof(str)-++len]='0'+rem;
} while (num/=10);
memcpy(src, str+sizeof(str)-len, len);
return src+len;
}
static char *cat_uint32(char *src, uint32_t num){
if (num>=1000)
return cat_uint32l(src, num);
if (num<100){
if (num<10)
goto format10;
else
goto format100;
}
*src++='0'+num/100;
num%=100;
format100:
*src++='0'+num/10;
num%=10;
format10:
*src++='0'+num;
return src;
}
#define cat_const(src, app) cat_lstr(src, app, sizeof(app)-1)
static char *fill_formatted_bytes(char *str, uint64_t bytes){
const static char *sizes[]={"Bytes", "KB", "MB", "GB", "TB"};
uint32_t rem, sz;
rem=0;
sz=0;
while (bytes>=1024 && sz<ARRAY_SIZE(sizes)-1){
rem=bytes%1024;
bytes/=1024;
sz++;
}
str=cat_uint32(str, (uint32_t)bytes);
if (sz && bytes<100){
rem=rem*1000/1024;
assert(rem<=999);
*str++='.';
if (bytes<10){
rem/=10;
*str++='0'+rem/10;
*str++='0'+rem%10;
}
else
*str++='0'+rem/100;
}
return cat_str(str, sizes[sz]);
}
static char *fill_remaining(char *str, uint32_t files, uint64_t bytes){
str=cat_const(str, "Remaining: ");
str=cat_uint32(str, files);
str=cat_const(str, " files, ");
return fill_formatted_bytes(str, bytes);
}
static char *fill_formatted_time(char *str, uint64_t totalsec){
uint32_t d, h, m, s;
s=totalsec%60;
totalsec/=60;
m=totalsec%60;
totalsec/=60;
h=totalsec%24;
d=totalsec/24;
if (d){
str=cat_uint32(str, d);
*str++='d';
*str++=' ';
if (m>=30)
h++;
str=cat_uint32(str, h);
*str++='h';
}
else if (h){
str=cat_uint32(str, h);
*str++='h';
*str++=' ';
if (s>=30)
m++;
str=cat_uint32(str, m);
*str++='m';
}
else if (m){
str=cat_uint32(str, m);
*str++='m';
*str++=' ';
str=cat_uint32(str, s);
*str++='s';
}
else {
if (!s)
s=1;
str=cat_uint32(str, s);
*str++='s';
}
return str;
}
static uint64_t sub_no_underf(uint64_t a, uint64_t b){
if (likely(b<a))
return a-b;
else
return 0;
}
static void status_fill_formatted_str(pstatus_t *status, char *downloadstr, char *uploadstr){
char *up, *dw;
uint64_t remsec;
uint32_t speed;
dw=downloadstr;
up=uploadstr;
if (status->filestodownload){
speed=status->downloadspeed;
if (status->status==PSTATUS_PAUSED || status->status==PSTATUS_STOPPED || status->localisfull || speed==0){
if (status->status==PSTATUS_PAUSED)
dw=cat_const(dw, "Paused. ");
else if (status->status==PSTATUS_STOPPED)
dw=cat_const(dw, "Stopped. ");
else if (status->localisfull)
dw=cat_const(dw, "Disk full. ");
dw=fill_remaining(dw, status->filestodownload, sub_no_underf(status->bytestodownload, status->bytesdownloaded));
}
else{
dw=fill_formatted_bytes(dw, speed);
dw=cat_const(dw, "/sec, ");
dw=fill_remaining(dw, status->filestodownload, sub_no_underf(status->bytestodownload, status->bytesdownloaded));
remsec=(status->bytestodownload-status->bytesdownloaded)/speed;
if (remsec<DONT_SHOW_TIME_IF_SEC_OVER || speed>=DONT_SHOW_TIME_IF_SPEED_BELOW){
*dw++=' ';
dw=fill_formatted_time(dw, remsec);
}
}
}
else
dw=cat_const(dw, "Everything Downloaded");
if (status->filestoupload){
speed=status->uploadspeed;
if (status->status==PSTATUS_PAUSED || status->status==PSTATUS_STOPPED || status->remoteisfull || speed==0){
if (status->status==PSTATUS_PAUSED)
up=cat_const(up, "Paused. ");
else if (status->status==PSTATUS_STOPPED)
up=cat_const(up, "Stopped. ");
else if (status->remoteisfull)
up=cat_const(up, "Account full. ");
up=fill_remaining(up, status->filestoupload, sub_no_underf(status->bytestoupload, status->bytesuploaded));
}
else{
up=fill_formatted_bytes(up, speed);
up=cat_const(up, "/sec, ");
up=fill_remaining(up, status->filestoupload, sub_no_underf(status->bytestoupload, status->bytesuploaded));
remsec=(status->bytestoupload-status->bytesuploaded)/speed;
if (remsec<DONT_SHOW_TIME_IF_SEC_OVER || speed>=DONT_SHOW_TIME_IF_SPEED_BELOW){
*up++=' ';
up=fill_formatted_time(up, remsec);
}
}
}
else
up=cat_const(up, "Everything Uploaded");
assert(dw<downloadstr+MAX_STATUS_STR_LEN);
assert(up<uploadstr+MAX_STATUS_STR_LEN);
*dw=0;
*up=0;
status->downloadstr=downloadstr;
status->uploadstr=uploadstr;
}
void psync_callbacks_get_status(pstatus_t *status){
static char downloadstr[MAX_STATUS_STR_LEN], uploadstr[MAX_STATUS_STR_LEN];
memcpy(status, &psync_status, sizeof(pstatus_t));
status_fill_formatted_str(status, downloadstr, uploadstr);
}
static void status_change_thread(void *ptr){
char downloadstr[MAX_STATUS_STR_LEN], uploadstr[MAX_STATUS_STR_LEN];
pstatus_change_callback_t callback=(pstatus_change_callback_t)ptr;
while (1){
// Maximum 2 updates/sec
psync_milisleep(500);
pthread_mutex_lock(&statusmutex);
while (statuschanges<=0){
statuschanges=-1;
pthread_cond_wait(&statuscond, &statusmutex);
}
statuschanges=0;
if (((status_old.filestodownload > 0 ) && (psync_status.filestodownload == 0)) ||
((psync_status.filestodownload > 0 ) && (status_old.filestodownload == 0)) ||
((status_old.filestoupload > 0 ) && (psync_status.filestoupload == 0)) ||
((psync_status.filestoupload > 0 ) && (status_old.filestoupload == 0)) ||
((psync_status.localisfull != status_old.localisfull)) ||
((psync_status.remoteisfull != status_old.remoteisfull)) ||
((psync_status.status != status_old.status) && (
(psync_status.status == PSTATUS_STOPPED) ||
(psync_status.status == PSTATUS_PAUSED) ||
(psync_status.status == PSTATUS_OFFLINE) ||
(status_old.status == PSTATUS_STOPPED) ||
(status_old.status == PSTATUS_PAUSED) ||
(status_old.status == PSTATUS_OFFLINE) ) )
)
psync_run_ratelimited("rebuild icons", psync_rebuild_icons, 1, 1);
status_old = psync_status;
pthread_mutex_unlock(&statusmutex);
if (!psync_do_run)
break;
status_fill_formatted_str(&psync_status, downloadstr, uploadstr);
debug(D_NOTICE, "sending status update, dwlstr: [%s], uplstr: [%s]", psync_status.downloadstr, psync_status.uploadstr);
callback(&psync_status);
}
}
void psync_set_status_callback(pstatus_change_callback_t callback){
pthread_mutex_lock(&statusmutex);
statusthreadrunning=1;
pthread_mutex_unlock(&statusmutex);
psync_run_thread1("status change", status_change_thread, callback);
}
void psync_send_status_update(){
if (statusthreadrunning){
pthread_mutex_lock(&statusmutex);
if (++statuschanges==0){
statuschanges++;
pthread_cond_signal(&statuscond);
}
pthread_mutex_unlock(&statusmutex);
}
}
static void event_thread(void *ptr){
pevent_callback_t callback=(pevent_callback_t)ptr;
event_list_t *event;
while (1){
pthread_mutex_lock(&eventmutex);
while (psync_list_isempty(&eventlist))
pthread_cond_wait(&eventcond, &eventmutex);
event=psync_list_remove_head_element(&eventlist, event_list_t, list);
pthread_mutex_unlock(&eventmutex);
if (!psync_do_run)
break;
callback(event->event, event->data);
if (event->freedata)
psync_free(event->data.ptr);
psync_free(event);
}
}
void psync_set_event_callback(pevent_callback_t callback){
pthread_mutex_lock(&statusmutex);
eventthreadrunning=1;
pthread_mutex_unlock(&statusmutex);
psync_list_init(&eventlist);
psync_run_thread1("event", event_thread, callback);
}
void psync_send_event_by_id(psync_eventtype_t eventid, psync_syncid_t syncid, const char *localpath, psync_fileorfolderid_t remoteid){
if (eventthreadrunning){
char *remotepath;
if (eventid&PEVENT_TYPE_FOLDER)
remotepath=psync_get_path_by_folderid(remoteid, NULL);
else
remotepath=psync_get_path_by_fileid(remoteid, NULL);
if (unlikely_log(!remotepath))
return;
psync_send_event_by_path(eventid, syncid, localpath, remoteid, remotepath);
psync_free(remotepath);
}
}
void psync_send_event_by_path(psync_eventtype_t eventid, psync_syncid_t syncid, const char *localpath, psync_fileorfolderid_t remoteid, const char *remotepath){
if (eventthreadrunning){
event_list_t *event;
size_t llen, rlen, slen;
char *lcopy, *rcopy, *strct, *name;
llen=strlen(localpath)+1;
rlen=strlen(remotepath)+1;
if (eventid&PEVENT_TYPE_FOLDER)
slen=sizeof(psync_file_event_t);
else
slen=sizeof(psync_folder_event_t);
event=(event_list_t *)psync_malloc(sizeof(event_list_t)+slen+llen+rlen);
strct=(char *)(event+1);
lcopy=strct+slen;
rcopy=lcopy+llen;
memcpy(lcopy, localpath, llen);
memcpy(rcopy, remotepath, rlen);
name=strrchr(rcopy, '/')+1;
if (eventid&PEVENT_TYPE_FOLDER){
psync_folder_event_t *f=(psync_folder_event_t *)strct;
f->folderid=remoteid;
f->name=name;
f->localpath=lcopy;
f->remotepath=rcopy;
f->syncid=syncid;
event->data.folder=f;
}
else{
psync_file_event_t *f=(psync_file_event_t *)strct;
f->fileid=remoteid;
f->name=name;
f->localpath=lcopy;
f->remotepath=rcopy;
f->syncid=syncid;
event->data.file=f;
}
event->event=eventid;
event->freedata=0;
pthread_mutex_lock(&eventmutex);
psync_list_add_tail(&eventlist, &event->list);
pthread_cond_signal(&eventcond);
pthread_mutex_unlock(&eventmutex);
}
}
void psync_send_eventid(psync_eventtype_t eventid){
if (eventthreadrunning){
event_list_t *event;
event=psync_new(event_list_t);
event->data.ptr=NULL;
event->event=eventid;
event->freedata=0;
pthread_mutex_lock(&eventmutex);
psync_list_add_tail(&eventlist, &event->list);
pthread_cond_signal(&eventcond);
pthread_mutex_unlock(&eventmutex);
}
}
void psync_send_eventdata(psync_eventtype_t eventid, void *eventdata){
debug(D_NOTICE, "Send event data: eventid: [%u], eventthreadrunning: [%d]", eventid, eventthreadrunning);
if (eventthreadrunning){
event_list_t *event;
event=psync_new(event_list_t);
event->data.ptr=eventdata;
event->event=eventid;
event->freedata=1;
pthread_mutex_lock(&eventmutex);
psync_list_add_tail(&eventlist, &event->list);
pthread_cond_signal(&eventcond);
pthread_mutex_unlock(&eventmutex);
}
else
psync_free(eventdata);
}
//Data event methods.
/**********************************************************************************************/
data_event_callback data_event_fptr = NULL;
de_elem_list data_event_elem_list = {NULL, NULL};
static pthread_mutex_t data_event_mutex = PTHREAD_MUTEX_INITIALIZER;
/**********************************************************************************************/
void free_data_event(event_data_struct* elem) {
if (elem->str1) {
psync_free((void*)elem->str1);
}
if (elem->str2) {
psync_free((void*)elem->str2);
}
psync_free(elem);
}
/**********************************************************************************************/
void add_elem(event_data_struct* elem, de_elem_list* list) {
pthread_mutex_lock(&data_event_mutex);
if (list->first == NULL) {
list->first = elem;
}
if (list->last == NULL) {
list->last = elem;
}
else {
list->last->elem_next = elem;
}
list->last = elem;
pthread_mutex_unlock(&data_event_mutex);
}
/***********************************************************************/
event_data_struct* pop_elem(de_elem_list* list) {
event_data_struct* curr_elem;
if (list->first == NULL) {
return NULL;
}
pthread_mutex_lock(&data_event_mutex);
curr_elem = list->first;
if (list->first == list->last) {
list->first = NULL;
list->last = NULL;
}
else {
list->first = curr_elem->elem_next;
}
pthread_mutex_unlock(&data_event_mutex);
return curr_elem;
}
/**********************************************************************************************/
void data_event_thread(void* ptr) {
de_elem_list* event_list = (de_elem_list*)ptr;
event_data_struct* data;
char* path;
int i;
while (1) {
if ((plasr_diff_devent_t != -1) && ((psync_time() - plasr_diff_devent_t) >= PDIFF_DATA_EVENT_DELAY)) {
data_event_fptr(PEVENT_FS_ADD_OBJ, NULL, NULL, 0, 0);
plasr_diff_devent_t = -1;
}
else{
if (plasr_diff_devent_t != -1) {
debug(D_NOTICE, "Diff data event too early. Wait.");
}
}
for (i = 0; i < 100; i++) {
data = pop_elem(event_list);
if (data) {
debug(D_NOTICE, "Sending data event Event id: [%d] Str1: [%s], Str1: [%s], Uint1:[%"P_PRI_U64"] Uint2:[%"P_PRI_U64"]", data->eventid, data->str1, data->str2, data->uint1, data->uint2);
data_event_fptr(data->eventid, data->str1, data->str2, data->uint1, data->uint2);
free_data_event(data);
}
else {
break;
}
}
psync_milisleep(1000);
}
}
/**********************************************************************************************/
void psync_init_data_event(void* ptr) {
data_event_fptr = (data_event_callback)ptr;
psync_run_thread1("Data Event", data_event_thread, &data_event_elem_list);
}
/**********************************************************************************************/
void psync_send_data_event(int event_id, const char* str1, const char* str2, uint64_t uint1, uint64_t uint2) {
event_data_struct* data = psync_new(event_data_struct);
data->eventid = event_id;
if(str1)data->str1 = strdup(str1);
else data->str1 = strdup("null");
if (str1)data->str2 = strdup(str2);
else data->str2 = strdup("null");
data->uint1 = uint1;
data->uint2 = uint2;
debug(D_NOTICE, "Send data event: Event Id: [%d] Str1: [%s] Str2: [%s] Uint1: [%"P_PRI_U64"] Uint2: [%"P_PRI_U64"]", data->eventid, data->str1, data->str2, data->uint1, data->uint2);
if (data_event_fptr) {
add_elem(data, &data_event_elem_list);
}
else {
//debug(D_ERROR, "Data event callback function not set.");
}
}
/**********************************************************************************************/
void psync_timed_data_event() {
plasr_diff_devent_t = psync_time();
}
/**********************************************************************************************/
void psync_data_event_test(int eventid, char* str1, char* str2, uint64_t uint1, uint64_t uint2) {
debug(D_NOTICE, "Test Data event callback. eventid [%d]. String1: [%s], String2: [%s], uInt1: [%"P_PRI_U64"] uInt2: [%"P_PRI_U64"]", eventid, str1, str2, uint1, uint2);
}
/**********************************************************************************************/
void wait_auth_token_async(void* data) {
wait_token_cb_struct* local_data = data;
const wait_login_async_cb callback = (wait_login_async_cb)local_data->calb_ptr;
const int res = wait_auth_token(local_data->req_id);
debug(D_NOTICE, "wait_auth_token_async. Result:[%d]", res);
psync_free(local_data);
callback(res);
}
/**********************************************************************************************/
//Data event methods End.