-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpcallbacks.c
395 lines (368 loc) · 11.6 KB
/
pcallbacks.c
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
/* 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"
#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;
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 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, status->bytestodownload-status->bytesdownloaded);
}
else{
dw=fill_formatted_bytes(dw, speed);
dw=cat_const(dw, "/sec, ");
dw=fill_remaining(dw, status->filestodownload, 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, status->bytestoupload-status->bytesuploaded);
}
else{
up=fill_formatted_bytes(up, speed);
up=cat_const(up, "/sec, ");
up=fill_remaining(up, status->filestoupload, 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){
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){
static 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;
pthread_mutex_unlock(&statusmutex);
if (!psync_do_run)
break;
status_fill_formatted_str(&psync_status, downloadstr, 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){
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);
}