-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpsynclib.c
1511 lines (1393 loc) · 48.1 KB
/
psynclib.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
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2013 Anton Titov.
* Copyright (c) 2013 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 "plibs.h"
#include "pcompat.h"
#include "psynclib.h"
#include "pcallbacks.h"
#include "pstatus.h"
#include "pdiff.h"
#include "pssl.h"
#include "ptimer.h"
#include "pupload.h"
#include "pdownload.h"
#include "pfolder.h"
#include "psettings.h"
#include "psyncer.h"
#include "ptasks.h"
#include "papi.h"
#include "pnetlibs.h"
#include "pscanner.h"
#include "plocalscan.h"
#include "plist.h"
#include "pp2p.h"
#include "plocalnotify.h"
#include "pcache.h"
#include "pfileops.h"
#include "pcloudcrypto.h"
#include "ppagecache.h"
#include "ppassword.h"
#include "pnotifications.h"
#include "pmemlock.h"
#include <string.h>
#include <ctype.h>
#include <stddef.h>
typedef struct {
psync_list list;
char str[];
} string_list;
static psync_malloc_t psync_real_malloc=malloc;
static psync_realloc_t psync_real_realloc=realloc;
static psync_free_t psync_real_free=free;
const char *psync_database=NULL;
static int psync_libstate=0;
static pthread_mutex_t psync_libstate_mutex=PTHREAD_MUTEX_INITIALIZER;
#define return_error(err) do {psync_error=err; return -1;} while (0)
#define return_isyncid(err) do {psync_error=err; return PSYNC_INVALID_SYNCID;} while (0)
PSYNC_NOINLINE void *psync_emergency_malloc(size_t size){
void *ret;
debug(D_WARNING, "could not allocate %lu bytes", (unsigned long)size);
psync_try_free_memory();
ret=psync_real_malloc(size);
if (likely(ret))
#if IS_DEBUG
return memset(ret, 0xfa, size);
#else
return ret;
#endif
else{
debug(D_CRITICAL, "could not allocate %lu bytes even after freeing some memory, aborting", (unsigned long)size);
abort();
return NULL;
}
}
void *psync_malloc(size_t size){
void *ret;
ret=psync_real_malloc(size);
if (likely(ret))
#if IS_DEBUG
return memset(ret, 0xfa, size);
#else
return ret;
#endif
else
return psync_emergency_malloc(size);
}
PSYNC_NOINLINE void *psync_emergency_realloc(void *ptr, size_t size){
void *ret;
debug(D_WARNING, "could not reallocate %lu bytes", (unsigned long)size);
psync_try_free_memory();
ret=psync_real_realloc(ptr, size);
if (likely(ret))
return ret;
else{
debug(D_CRITICAL, "could not reallocate %lu bytes even after freeing some memory, aborting", (unsigned long)size);
abort();
return NULL;
}
}
void *psync_realloc(void *ptr, size_t size){
void *ret;
ret=psync_real_realloc(ptr, size);
if (likely(ret))
return ret;
else
return psync_emergency_realloc(ptr, size);
}
void psync_free(void *ptr){
psync_real_free(ptr);
}
uint32_t psync_get_last_error(){
return psync_error;
}
void psync_set_database_path(const char *databasepath){
psync_database=psync_strdup(databasepath);
}
void psync_set_alloc(psync_malloc_t malloc_call, psync_realloc_t realloc_call, psync_free_t free_call){
psync_real_malloc=malloc_call;
psync_real_realloc=realloc_call;
psync_real_free=free_call;
}
void psync_set_software_string(const char *str){
debug(D_NOTICE, "setting software name to %s", str);
psync_set_software_name(str);
}
static void psync_stop_crypto_on_sleep(){
if (psync_setting_get_bool(_PS(sleepstopcrypto)) && psync_crypto_isstarted()){
psync_cloud_crypto_stop();
debug(D_NOTICE, "stopped crypto due to sleep");
}
}
int psync_init(){
psync_thread_name="main app thread";
debug(D_NOTICE, "initializing");
if (IS_DEBUG){
pthread_mutex_lock(&psync_libstate_mutex);
if (psync_libstate!=0){
pthread_mutex_unlock(&psync_libstate_mutex);
debug(D_BUG, "you are not supposed to call psync_init for a second time");
return 0;
}
}
psync_locked_init();
psync_cache_init();
psync_compat_init();
if (!psync_database){
psync_database=psync_get_default_database_path();
if (unlikely_log(!psync_database)){
if (IS_DEBUG)
pthread_mutex_unlock(&psync_libstate_mutex);
return_error(PERROR_NO_HOMEDIR);
}
}
if (psync_sql_connect(psync_database)){
if (IS_DEBUG)
pthread_mutex_unlock(&psync_libstate_mutex);
return_error(PERROR_DATABASE_OPEN);
}
psync_timer_init();
psync_sql_statement("UPDATE task SET inprogress=0 WHERE inprogress=1");
if (unlikely_log(psync_ssl_init())){
if (IS_DEBUG)
pthread_mutex_unlock(&psync_libstate_mutex);
return_error(PERROR_SSL_INIT_FAILED);
}
psync_libs_init();
psync_settings_init();
psync_status_init();
psync_timer_sleep_handler(psync_stop_crypto_on_sleep);
if (IS_DEBUG){
psync_libstate=1;
pthread_mutex_unlock(&psync_libstate_mutex);
}
return 0;
}
void psync_start_sync(pstatus_change_callback_t status_callback, pevent_callback_t event_callback){
debug(D_NOTICE, "starting sync");
if (IS_DEBUG){
pthread_mutex_lock(&psync_libstate_mutex);
if (psync_libstate==0){
pthread_mutex_unlock(&psync_libstate_mutex);
debug(D_BUG, "you are calling psync_start_sync before psync_init");
return;
}
else if (psync_libstate==2){
pthread_mutex_unlock(&psync_libstate_mutex);
debug(D_BUG, "you are calling psync_start_sync for a second time");
return;
}
else
psync_libstate=2;
pthread_mutex_unlock(&psync_libstate_mutex);
}
if (status_callback)
psync_set_status_callback(status_callback);
if (event_callback)
psync_set_event_callback(event_callback);
psync_syncer_init();
psync_diff_init();
psync_upload_init();
psync_download_init();
psync_netlibs_init();
psync_localscan_init();
psync_p2p_init();
if (psync_setting_get_bool(_PS(autostartfs)))
psync_fs_start();
}
void psync_set_notification_callback(pnotification_callback_t notification_callback, const char *thumbsize){
psync_notifications_set_callback(notification_callback, thumbsize);
}
psync_notification_list_t *psync_get_notifications(){
return psync_notifications_get();
}
uint32_t psync_download_state(){
return 0;
}
void psync_destroy(){
psync_do_run=0;
psync_fs_stop();
psync_terminate_status_waiters();
psync_send_status_update();
psync_timer_wake();
psync_timer_notify_exception();
psync_sql_sync();
psync_milisleep(20);
psync_sql_lock();
psync_cache_clean_all();
psync_sql_close();
}
void psync_get_status(pstatus_t *status){
psync_callbacks_get_status(status);
}
char *psync_get_username(){
return psync_sql_cellstr("SELECT value FROM setting WHERE id='username'");
}
static void clear_db(int save){
psync_sql_statement("DELETE FROM setting WHERE id IN ('pass', 'auth')");
psync_setting_set_bool(_PS(saveauth), save);
}
void psync_set_user_pass(const char *username, const char *password, int save){
clear_db(save);
if (save){
psync_set_string_value("user", username);
psync_set_string_value("pass", password);
}
else{
pthread_mutex_lock(&psync_my_auth_mutex);
psync_free(psync_my_user);
psync_my_user=psync_strdup(username);
psync_free(psync_my_pass);
psync_my_pass=psync_strdup(password);
pthread_mutex_unlock(&psync_my_auth_mutex);
}
psync_set_status(PSTATUS_TYPE_AUTH, PSTATUS_AUTH_PROVIDED);
}
void psync_set_pass(const char *password, int save){
clear_db(save);
if (save)
psync_set_string_value("pass", password);
else{
pthread_mutex_lock(&psync_my_auth_mutex);
psync_free(psync_my_pass);
psync_my_pass=psync_strdup(password);
pthread_mutex_unlock(&psync_my_auth_mutex);
}
psync_set_status(PSTATUS_TYPE_AUTH, PSTATUS_AUTH_PROVIDED);
}
void psync_set_auth(const char *auth, int save){
clear_db(save);
if (save)
psync_set_string_value("auth", auth);
else
psync_strlcpy(psync_my_auth, auth, sizeof(psync_my_auth));
psync_set_status(PSTATUS_TYPE_AUTH, PSTATUS_AUTH_PROVIDED);
}
#define run_command(cmd, params, err) do_run_command_res(cmd, strlen(cmd), params, sizeof(params)/sizeof(binparam), err)
static int do_run_command_res(const char *cmd, size_t cmdlen, const binparam *params, size_t paramscnt, char **err){
psync_socket *api;
binresult *res;
uint64_t result;
int tries;
tries=0;
while (1){
api=psync_apipool_get();
if (unlikely(!api))
goto neterr;
res=do_send_command(api, cmd, cmdlen, params, paramscnt, -1, 1);
if (likely(res)){
psync_apipool_release(api);
break;
}
else{
psync_apipool_release_bad(api);
if (++tries>=5)
goto neterr;
}
}
result=psync_find_result(res, "result", PARAM_NUM)->num;
if (result){
debug(D_WARNING, "command %s returned code %u", cmd, (unsigned)result);
if (err)
*err=psync_strdup(psync_find_result(res, "error", PARAM_STR)->str);
psync_process_api_error(result);
}
psync_free(res);
return (int)result;
neterr:
if (err)
*err=psync_strdup("Could not connect to the server.");
return -1;
}
int psync_mark_notificaitons_read(uint32_t notificationid){
binparam params[]={P_STR("auth", psync_my_auth), P_NUM("notificationid", notificationid)};
return run_command("readnotifications", params, NULL)?-1:0;
}
static void psync_invalidate_auth(const char *auth){
binparam params[]={P_STR("auth", auth)};
run_command("logout", params, NULL);
}
void psync_logout2(uint32_t auth_status, int doinvauth){
debug(D_NOTICE, "logout");
psync_sql_statement("DELETE FROM setting WHERE id IN ('pass', 'auth', 'saveauth')");
if (doinvauth)
psync_invalidate_auth(psync_my_auth);
memset(psync_my_auth, 0, sizeof(psync_my_auth));
psync_cloud_crypto_stop();
pthread_mutex_lock(&psync_my_auth_mutex);
psync_free(psync_my_pass);
psync_my_pass=NULL;
pthread_mutex_unlock(&psync_my_auth_mutex);
psync_set_status(PSTATUS_TYPE_ONLINE, PSTATUS_ONLINE_CONNECTING);
psync_set_status(PSTATUS_TYPE_AUTH, auth_status);
psync_fs_pause_until_login();
psync_stop_all_download();
psync_stop_all_upload();
psync_cache_clean_all();
psync_restart_localscan();
psync_timer_notify_exception();
if (psync_fs_need_per_folder_refresh())
psync_fs_refresh_folder(0);
}
void psync_logout(){
psync_logout2(PSTATUS_AUTH_REQUIRED, 1);
}
void psync_unlink(){
int ret;
debug(D_NOTICE, "unlink");
psync_diff_lock();
psync_stop_all_download();
psync_stop_all_upload();
psync_status_recalc_to_download();
psync_status_recalc_to_upload();
psync_invalidate_auth(psync_my_auth);
psync_cloud_crypto_stop();
psync_milisleep(20);
psync_stop_localscan();
psync_sql_checkpoint_lock();
psync_set_status(PSTATUS_TYPE_ONLINE, PSTATUS_ONLINE_CONNECTING);
psync_set_status(PSTATUS_TYPE_AUTH, PSTATUS_AUTH_REQUIRED);
psync_set_status(PSTATUS_TYPE_RUN, PSTATUS_RUN_STOP);
psync_timer_notify_exception();
psync_sql_lock();
debug(D_NOTICE, "clearing database, locked");
psync_cache_clean_all();
ret=psync_sql_close();
psync_file_delete(psync_database);
if (ret){
debug(D_ERROR, "failed to close database, exiting");
exit(1);
}
psync_pagecache_clean_cache();
psync_sql_connect(psync_database);
/*
psync_sql_res *res;
psync_variant_row row;
char *sql;
const char *str;
size_t len;
psync_list list;
string_list *le;
psync_list_init(&list);
res=psync_sql_query("SELECT name FROM sqlite_master WHERE type='index'");
while ((row=psync_sql_fetch_row(res))){
str=psync_get_lstring(row[0], &len);
le=(string_list *)psync_malloc(offsetof(string_list, str)+len+1);
memcpy(le->str, str, len+1);
psync_list_add_tail(&list, &le->list);
}
psync_sql_free_result(res);
psync_list_for_each_element(le, &list, string_list, list){
sql=psync_strcat("DROP INDEX ", le->str, NULL);
psync_sql_statement(sql);
psync_free(sql);
}
psync_list_for_each_element_call(&list, string_list, list, psync_free);
psync_list_init(&list);
res=psync_sql_query("SELECT name FROM sqlite_master WHERE type='table'");
while ((row=psync_sql_fetch_row(res))){
str=psync_get_lstring(row[0], &len);
le=(string_list *)psync_malloc(offsetof(string_list, str)+len+1);
memcpy(le->str, str, len+1);
psync_list_add_tail(&list, &le->list);
}
psync_sql_free_result(res);
psync_list_for_each_element(le, &list, string_list, list){
sql=psync_strcat("DROP TABLE ", le->str, NULL);
psync_sql_statement(sql);
psync_free(sql);
}
psync_list_for_each_element_call(&list, string_list, list, psync_free);
psync_sql_statement("VACUUM");
*/
pthread_mutex_lock(&psync_my_auth_mutex);
memset(psync_my_auth, 0, sizeof(psync_my_auth));
psync_my_user=NULL;
psync_my_pass=NULL;
psync_my_userid=0;
pthread_mutex_unlock(&psync_my_auth_mutex);
debug(D_NOTICE, "clearing database, finished");
psync_fs_pause_until_login();
psync_fs_clean_tasks();
psync_sql_unlock();
psync_sql_checkpoint_unlock();
psync_settings_reset();
psync_cache_clean_all();
psync_notifications_clean();
psync_diff_unlock();
psync_set_status(PSTATUS_TYPE_ONLINE, PSTATUS_ONLINE_CONNECTING);
psync_set_status(PSTATUS_TYPE_ACCFULL, PSTATUS_ACCFULL_QUOTAOK);
psync_set_status(PSTATUS_TYPE_AUTH, PSTATUS_AUTH_REQUIRED);
psync_set_status(PSTATUS_TYPE_RUN, PSTATUS_RUN_RUN);
psync_resume_localscan();
if (psync_fs_need_per_folder_refresh())
psync_fs_refresh_folder(0);
}
psync_syncid_t psync_add_sync_by_path(const char *localpath, const char *remotepath, psync_synctype_t synctype){
psync_folderid_t folderid=psync_get_folderid_by_path(remotepath);
if (likely_log(folderid!=PSYNC_INVALID_FOLDERID))
return psync_add_sync_by_folderid(localpath, folderid, synctype);
else
return PSYNC_INVALID_SYNCID;
}
psync_syncid_t psync_add_sync_by_folderid(const char *localpath, psync_folderid_t folderid, psync_synctype_t synctype){
psync_sql_res *res;
char *syncmp;
psync_uint_row row;
psync_str_row srow;
uint64_t perms;
psync_stat_t st;
psync_syncid_t ret;
int unsigned md;
if (unlikely_log(synctype<PSYNC_SYNCTYPE_MIN || synctype>PSYNC_SYNCTYPE_MAX))
return_isyncid(PERROR_INVALID_SYNCTYPE);
if (unlikely_log(psync_stat(localpath, &st)) || unlikely_log(!psync_stat_isfolder(&st)))
return_isyncid(PERROR_LOCAL_FOLDER_NOT_FOUND);
if (synctype&PSYNC_DOWNLOAD_ONLY)
md=7;
else
md=5;
if (unlikely_log(!psync_stat_mode_ok(&st, md)))
return_isyncid(PERROR_LOCAL_FOLDER_ACC_DENIED);
syncmp=psync_fs_getmountpoint();
if (syncmp){
size_t len=strlen(syncmp);
if (!psync_filename_cmpn(syncmp, localpath, len) && (localpath[len]==0 || localpath[len]=='/' || localpath[len]=='\\')){
debug(D_NOTICE, "local path %s is on pCloudDrive mounted as %s, rejecting sync", localpath, syncmp);
psync_free(syncmp);
return_isyncid(PERROR_LOCAL_IS_ON_PDRIVE);
}
psync_free(syncmp);
}
res=psync_sql_query("SELECT localpath FROM syncfolder");
if (unlikely_log(!res))
return_isyncid(PERROR_DATABASE_ERROR);
while ((srow=psync_sql_fetch_rowstr(res)))
if (psync_str_is_prefix(srow[0], localpath)){
psync_sql_free_result(res);
return_isyncid(PERROR_PARENT_OR_SUBFOLDER_ALREADY_SYNCING);
}
else if (!psync_filename_cmp(srow[0], localpath)){
psync_sql_free_result(res);
return_isyncid(PERROR_FOLDER_ALREADY_SYNCING);
}
psync_sql_free_result(res);
if (folderid){
res=psync_sql_query("SELECT permissions FROM folder WHERE id=?");
if (unlikely_log(!res))
return_isyncid(PERROR_DATABASE_ERROR);
psync_sql_bind_uint(res, 1, folderid);
row=psync_sql_fetch_rowint(res);
if (unlikely_log(!row)){
psync_sql_free_result(res);
return_isyncid(PERROR_REMOTE_FOLDER_NOT_FOUND);
}
perms=row[0];
psync_sql_free_result(res);
}
else
perms=PSYNC_PERM_ALL;
if (unlikely_log((synctype&PSYNC_DOWNLOAD_ONLY && (perms&PSYNC_PERM_READ)!=PSYNC_PERM_READ) ||
(synctype&PSYNC_UPLOAD_ONLY && (perms&PSYNC_PERM_WRITE)!=PSYNC_PERM_WRITE)))
return_isyncid(PERROR_REMOTE_FOLDER_ACC_DENIED);
res=psync_sql_prep_statement("INSERT OR IGNORE INTO syncfolder (folderid, localpath, synctype, flags, inode, deviceid) VALUES (?, ?, ?, 0, ?, ?)");
if (unlikely_log(!res))
return_isyncid(PERROR_DATABASE_ERROR);
psync_sql_bind_uint(res, 1, folderid);
psync_sql_bind_string(res, 2, localpath);
psync_sql_bind_uint(res, 3, synctype);
psync_sql_bind_uint(res, 4, psync_stat_inode(&st));
psync_sql_bind_uint(res, 5, psync_stat_device(&st));
psync_sql_run(res);
if (likely_log(psync_sql_affected_rows()))
ret=psync_sql_insertid();
else
ret=PSYNC_INVALID_SYNCID;
psync_sql_free_result(res);
if (ret==PSYNC_INVALID_SYNCID)
return_isyncid(PERROR_FOLDER_ALREADY_SYNCING);
psync_sql_sync();
psync_syncer_new(ret);
return ret;
}
int psync_add_sync_by_path_delayed(const char *localpath, const char *remotepath, psync_synctype_t synctype){
psync_sql_res *res;
psync_stat_t st;
int unsigned md;
if (unlikely_log(synctype<PSYNC_SYNCTYPE_MIN || synctype>PSYNC_SYNCTYPE_MAX))
return_error(PERROR_INVALID_SYNCTYPE);
if (unlikely_log(psync_stat(localpath, &st)) || unlikely_log(!psync_stat_isfolder(&st)))
return_error(PERROR_LOCAL_FOLDER_NOT_FOUND);
if (synctype&PSYNC_DOWNLOAD_ONLY)
md=7;
else
md=5;
if (unlikely_log(!psync_stat_mode_ok(&st, md)))
return_error(PERROR_LOCAL_FOLDER_ACC_DENIED);
res=psync_sql_prep_statement("INSERT INTO syncfolderdelayed (localpath, remotepath, synctype) VALUES (?, ?, ?)");
psync_sql_bind_string(res, 1, localpath);
psync_sql_bind_string(res, 2, remotepath);
psync_sql_bind_uint(res, 3, synctype);
psync_sql_run_free(res);
psync_sql_sync();
if (psync_status_get(PSTATUS_TYPE_ONLINE)==PSTATUS_ONLINE_ONLINE)
psync_run_thread("check delayed syncs", psync_syncer_check_delayed_syncs);
return 0;
}
int psync_change_synctype(psync_syncid_t syncid, psync_synctype_t synctype){
psync_sql_res *res;
psync_variant_row row;
psync_uint_row urow;
psync_folderid_t folderid;
uint64_t perms;
psync_stat_t st;
int unsigned md;
psync_synctype_t oldsynctype;
if (unlikely_log(synctype<PSYNC_SYNCTYPE_MIN || synctype>PSYNC_SYNCTYPE_MAX))
return_isyncid(PERROR_INVALID_SYNCTYPE);
psync_sql_start_transaction();
res=psync_sql_query("SELECT folderid, localpath, synctype FROM syncfolder WHERE id=?");
psync_sql_bind_uint(res, 1, syncid);
row=psync_sql_fetch_row(res);
if (unlikely_log(!row)){
psync_sql_free_result(res);
psync_sql_rollback_transaction();
return_error(PERROR_INVALID_SYNCID);
}
folderid=psync_get_number(row[0]);
oldsynctype=psync_get_number(row[2]);
if (oldsynctype==synctype){
psync_sql_free_result(res);
psync_sql_rollback_transaction();
return 0;
}
if (unlikely_log(psync_stat(psync_get_string(row[1]), &st)) || unlikely_log(!psync_stat_isfolder(&st))){
psync_sql_free_result(res);
psync_sql_rollback_transaction();
return_isyncid(PERROR_LOCAL_FOLDER_NOT_FOUND);
}
psync_sql_free_result(res);
if (synctype&PSYNC_DOWNLOAD_ONLY)
md=7;
else
md=5;
if (unlikely_log(!psync_stat_mode_ok(&st, md))){
psync_sql_rollback_transaction();
return_isyncid(PERROR_LOCAL_FOLDER_ACC_DENIED);
}
if (folderid){
res=psync_sql_query("SELECT permissions FROM folder WHERE id=?");
if (unlikely_log(!res))
return_isyncid(PERROR_DATABASE_ERROR);
psync_sql_bind_uint(res, 1, folderid);
urow=psync_sql_fetch_rowint(res);
if (unlikely_log(!urow)){
psync_sql_free_result(res);
psync_sql_rollback_transaction();
return_isyncid(PERROR_REMOTE_FOLDER_NOT_FOUND);
}
perms=urow[0];
psync_sql_free_result(res);
}
else
perms=PSYNC_PERM_ALL;
if (unlikely_log((synctype&PSYNC_DOWNLOAD_ONLY && (perms&PSYNC_PERM_READ)!=PSYNC_PERM_READ) ||
(synctype&PSYNC_UPLOAD_ONLY && (perms&PSYNC_PERM_WRITE)!=PSYNC_PERM_WRITE))){
psync_sql_rollback_transaction();
return_isyncid(PERROR_REMOTE_FOLDER_ACC_DENIED);
}
res=psync_sql_prep_statement("UPDATE syncfolder SET synctype=?, flags=0 WHERE id=?");
psync_sql_bind_uint(res, 1, synctype);
psync_sql_bind_uint(res, 2, syncid);
psync_sql_run_free(res);
res=psync_sql_query("SELECT folderid FROM syncedfolder WHERE syncid=?");
psync_sql_bind_uint(res, 1, syncid);
while ((urow=psync_sql_fetch_rowint(res)))
psync_del_folder_from_downloadlist(urow[0]);
psync_sql_free_result(res);
res=psync_sql_prep_statement("DELETE FROM syncedfolder WHERE syncid=?");
psync_sql_bind_uint(res, 1, syncid);
psync_sql_run_free(res);
res=psync_sql_prep_statement("DELETE FROM localfile WHERE syncid=?");
psync_sql_bind_uint(res, 1, syncid);
psync_sql_run_free(res);
res=psync_sql_prep_statement("DELETE FROM localfolder WHERE syncid=?");
psync_sql_bind_uint(res, 1, syncid);
psync_sql_run_free(res);
psync_sql_commit_transaction();
psync_localnotify_del_sync(syncid);
psync_stop_sync_download(syncid);
psync_stop_sync_upload(syncid);
psync_sql_sync();
psync_syncer_new(syncid);
return 0;
}
static void psync_delete_local_recursive(psync_syncid_t syncid, psync_folderid_t localfolderid){
psync_sql_res *res;
psync_uint_row row;
res=psync_sql_query("SELECT id FROM localfolder WHERE localparentfolderid=? AND syncid=?");
psync_sql_bind_uint(res, 1, localfolderid);
psync_sql_bind_uint(res, 2, syncid);
while ((row=psync_sql_fetch_rowint(res)))
psync_delete_local_recursive(syncid, row[0]);
psync_sql_free_result(res);
res=psync_sql_prep_statement("DELETE FROM localfile WHERE localparentfolderid=? AND syncid=?");
psync_sql_bind_uint(res, 1, localfolderid);
psync_sql_bind_uint(res, 2, syncid);
psync_sql_run_free(res);
res=psync_sql_prep_statement("DELETE FROM syncedfolder WHERE localfolderid=? AND syncid=?");
psync_sql_bind_uint(res, 1, localfolderid);
psync_sql_bind_uint(res, 2, syncid);
psync_sql_run_free(res);
res=psync_sql_prep_statement("DELETE FROM localfolder WHERE id=? AND syncid=?");
psync_sql_bind_uint(res, 1, localfolderid);
psync_sql_bind_uint(res, 2, syncid);
psync_sql_run_free(res);
}
int psync_delete_sync(psync_syncid_t syncid){
psync_sql_res *res;
psync_sql_start_transaction();
/* this is slow and unneeded:
psync_uint_row row;
res=psync_sql_query("SELECT type, itemid, localitemid FROM task WHERE syncid=?");
psync_sql_bind_uint(res, 1, syncid);
while ((row=psync_sql_fetch_rowint(res)))
if (row[0]==PSYNC_DOWNLOAD_FILE)
psync_stop_file_download(row[1], syncid);
else if (row[0]==PSYNC_UPLOAD_FILE)
psync_delete_upload_tasks_for_file(row[2]);
psync_sql_free_result(res);
*/
psync_delete_local_recursive(syncid, 0);
res=psync_sql_prep_statement("DELETE FROM syncfolder WHERE id=?");
psync_sql_bind_uint(res, 1, syncid);
psync_sql_run_free(res);
if (psync_sql_commit_transaction())
return -1;
else{
psync_stop_sync_download(syncid);
psync_stop_sync_upload(syncid);
psync_localnotify_del_sync(syncid);
psync_restart_localscan();
psync_sql_sync();
return 0;
}
}
psync_folder_list_t *psync_get_sync_list(){
return psync_list_get_list();
}
psuggested_folders_t *psync_get_sync_suggestions(){
char *home;
psuggested_folders_t *ret;
home=psync_get_home_dir();
if (likely_log(home)){
ret=psync_scanner_scan_folder(home);
psync_free(home);
return ret;
}
else{
psync_error=PERROR_NO_HOMEDIR;
return NULL;
}
}
pfolder_list_t *psync_list_local_folder_by_path(const char *localpath, psync_listtype_t listtype){
return psync_list_local_folder(localpath, listtype);
}
pfolder_list_t *psync_list_remote_folder_by_path(const char *remotepath, psync_listtype_t listtype){
psync_folderid_t folderid=psync_get_folderid_by_path(remotepath);
if (folderid!=PSYNC_INVALID_FOLDERID)
return psync_list_remote_folder(folderid, listtype);
else
return NULL;
}
pfolder_list_t *psync_list_remote_folder_by_folderid(psync_folderid_t folderid, psync_listtype_t listtype){
return psync_list_remote_folder(folderid, listtype);
}
pentry_t *psync_stat_path(const char *remotepath){
return psync_folder_stat_path(remotepath);
}
int psync_is_name_to_ignore(const char *name){
const char *ign, *sc, *pt;
char *namelower;
unsigned char *lp;
size_t ilen, off, pl;
namelower=psync_strdup(name);
lp=(unsigned char *)namelower;
while (*lp){
*lp=tolower(*lp);
lp++;
}
ign=psync_setting_get_string(_PS(ignorepatterns));
ilen=strlen(ign);
off=0;
do {
sc=(const char *)memchr(ign+off, ';', ilen-off);
if (sc)
pl=sc-ign-off;
else
pl=ilen-off;
pt=ign+off;
off+=pl+1;
while (pl && isspace((unsigned char)*pt)){
pt++;
pl--;
}
while (pl && isspace((unsigned char)pt[pl-1]))
pl--;
if (psync_match_pattern(namelower, pt, pl)){
psync_free(namelower);
debug(D_NOTICE, "ignoring file/folder %s", name);
return 1;
}
} while (sc);
psync_free(namelower);
return 0;
}
static void psync_set_run_status(uint32_t status){
psync_set_status(PSTATUS_TYPE_RUN, status);
psync_set_uint_value("runstatus", status);
}
int psync_pause(){
psync_set_run_status(PSTATUS_RUN_PAUSE);
return 0;
}
int psync_stop(){
psync_set_run_status(PSTATUS_RUN_STOP);
psync_timer_notify_exception();
return 0;
}
int psync_resume(){
psync_set_run_status(PSTATUS_RUN_RUN);
return 0;
}
void psync_run_localscan(){
psync_wake_localscan();
}
#define run_command_get_res(cmd, params, err, res) do_run_command_get_res(cmd, strlen(cmd), params, sizeof(params)/sizeof(binparam), err, res)
static int do_run_command_get_res(const char *cmd, size_t cmdlen, const binparam *params, size_t paramscnt, char **err, binresult **pres){
psync_socket *api;
binresult *res;
uint64_t result;
api=psync_apipool_get();
if (unlikely(!api))
goto neterr;
res=do_send_command(api, cmd, cmdlen, params, paramscnt, -1, 1);
if (likely(res))
psync_apipool_release(api);
else{
psync_apipool_release_bad(api);
goto neterr;
}
result=psync_find_result(res, "result", PARAM_NUM)->num;
if (result){
debug(D_WARNING, "command %s returned code %u", cmd, (unsigned)result);
if (err)
*err=psync_strdup(psync_find_result(res, "error", PARAM_STR)->str);
psync_process_api_error(result);
}
if (result)
psync_free(res);
else
*pres=res;
return (int)result;
neterr:
if (err)
*err=psync_strdup("Could not connect to the server.");
return -1;
}
int psync_register(const char *email, const char *password, int termsaccepted, char **err){
binparam params[]={P_STR("mail", email), P_STR("password", password), P_STR("termsaccepted", termsaccepted?"yes":"0"), P_NUM("os", P_OS_ID)};
return run_command("register", params, err);
}
int psync_verify_email(char **err){
binparam params[]={P_STR("auth", psync_my_auth)};
return run_command("sendverificationemail", params, err);
}
int psync_lost_password(const char *email, char **err){
binparam params[]={P_STR("mail", email)};
return run_command("lostpassword", params, err);
}
int psync_change_password(const char *currentpass, const char *newpass, char **err){
binparam params[]={P_STR("auth", psync_my_auth), P_STR("oldpassword", currentpass), P_STR("newpassword", newpass)};
return run_command("changepassword", params, err);
}
int psync_create_remote_folder_by_path(const char *path, char **err){
binparam params[]={P_STR("auth", psync_my_auth), P_STR("path", path), P_STR("timeformat", "timestamp")};
binresult *res;
int ret;
ret=run_command_get_res("createfolder", params, err, &res);
if (ret)
return ret;
psync_ops_create_folder_in_db(psync_find_result(res, "metadata", PARAM_HASH));
psync_free(res);
return 0;
}
int psync_create_remote_folder(psync_folderid_t parentfolderid, const char *name, char **err){
binparam params[]={P_STR("auth", psync_my_auth), P_NUM("folderid", parentfolderid), P_STR("name", name), P_STR("timeformat", "timestamp")};
binresult *res;
int ret;
ret=run_command_get_res("createfolder", params, err, &res);
if (ret)
return ret;
psync_ops_create_folder_in_db(psync_find_result(res, "metadata", PARAM_HASH));
psync_free(res);
return 0;
}
const char *psync_get_auth_string(){
return psync_my_auth;
}
int psync_get_bool_setting(const char *settingname){
return psync_setting_get_bool(psync_setting_getid(settingname));
}
int psync_set_bool_setting(const char *settingname, int value){
return psync_setting_set_bool(psync_setting_getid(settingname), value);
}
int64_t psync_get_int_setting(const char *settingname){
return psync_setting_get_int(psync_setting_getid(settingname));
}
int psync_set_int_setting(const char *settingname, int64_t value){
return psync_setting_set_int(psync_setting_getid(settingname), value);
}
uint64_t psync_get_uint_setting(const char *settingname){
return psync_setting_get_uint(psync_setting_getid(settingname));
}
int psync_set_uint_setting(const char *settingname, uint64_t value){
return psync_setting_set_uint(psync_setting_getid(settingname), value);
}
const char *psync_get_string_setting(const char *settingname){
return psync_setting_get_string(psync_setting_getid(settingname));
}
int psync_set_string_setting(const char *settingname, const char *value){
return psync_setting_set_string(psync_setting_getid(settingname), value);
}
int psync_has_value(const char *valuename){
psync_sql_res *res;
psync_uint_row row;
int ret;
res=psync_sql_query_rdlock("SELECT COUNT(*) FROM setting WHERE id=?");
psync_sql_bind_string(res, 1, valuename);
row=psync_sql_fetch_rowint(res);
if (row)
ret=row[0];
else
ret=0;
psync_sql_free_result(res);
return ret;
}
int psync_get_bool_value(const char *valuename){
return !!psync_get_uint_value(valuename);
}
void psync_set_bool_value(const char *valuename, int value){
psync_set_uint_value(valuename, (uint64_t)(!!value));
}
int64_t psync_get_int_value(const char *valuename){
return (int64_t)psync_get_uint_value(valuename);
}
void psync_set_int_value(const char *valuename, int64_t value){
psync_set_uint_value(valuename, (uint64_t)value);
}
uint64_t psync_get_uint_value(const char *valuename){
psync_sql_res *res;
psync_uint_row row;
uint64_t ret;
res=psync_sql_query_rdlock("SELECT value FROM setting WHERE id=?");
psync_sql_bind_string(res, 1, valuename);
row=psync_sql_fetch_rowint(res);
if (row)
ret=row[0];
else
ret=0;
psync_sql_free_result(res);
return ret;
}