-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpupload.c
1332 lines (1280 loc) · 48.4 KB
/
pupload.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 "pupload.h"
#include "pstatus.h"
#include "ptimer.h"
#include "plibs.h"
#include "ptasks.h"
#include "pssl.h"
#include "psettings.h"
#include "pnetlibs.h"
#include "papi.h"
#include "pfolder.h"
#include "pcallbacks.h"
#include "pdiff.h"
#include "plist.h"
typedef struct {
psync_list list;
psync_fileid_t localfileid;
uint64_t filesize;
uint64_t uploaded;
uint64_t taskid;
psync_syncid_t syncid;
int stop;
unsigned char hash[PSYNC_HASH_DIGEST_HEXLEN];
} upload_list_t;
typedef struct {
upload_list_t upllist;
char filename[];
} upload_task_t;
static pthread_mutex_t upload_mutex=PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t upload_cond=PTHREAD_COND_INITIALIZER;
static uint32_t upload_wakes=0;
static psync_uint_t current_uploads_waiters=0;
static pthread_mutex_t current_uploads_mutex=PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t current_uploads_cond=PTHREAD_COND_INITIALIZER;
static psync_list uploads=PSYNC_LIST_STATIC_INIT(uploads);
static const uint32_t requiredstatuses[]={
PSTATUS_COMBINE(PSTATUS_TYPE_RUN, PSTATUS_RUN_RUN),
PSTATUS_COMBINE(PSTATUS_TYPE_ONLINE, PSTATUS_ONLINE_ONLINE),
PSTATUS_COMBINE(PSTATUS_TYPE_ACCFULL, PSTATUS_ACCFULL_QUOTAOK)
};
void psync_upload_inc_uploads(){
pthread_mutex_lock(&upload_mutex);
psync_status.filesuploading++;
pthread_mutex_unlock(&upload_mutex);
psync_send_status_update();
}
void psync_upload_dec_uploads(){
pthread_mutex_lock(&upload_mutex);
psync_status.filesuploading--;
if (psync_status.filesuploading==0 && current_uploads_waiters)
pthread_cond_broadcast(¤t_uploads_cond);
pthread_mutex_unlock(&upload_mutex);
psync_send_status_update();
}
void psync_upload_dec_uploads_cnt(uint32_t cnt){
pthread_mutex_lock(&upload_mutex);
psync_status.filesuploading-=cnt;
if (psync_status.filesuploading==0 && current_uploads_waiters)
pthread_cond_broadcast(¤t_uploads_cond);
pthread_mutex_unlock(&upload_mutex);
psync_send_status_update();
}
void psync_upload_add_bytes_uploaded(uint64_t bytes){
pthread_mutex_lock(&upload_mutex);
psync_status.bytesuploaded+=bytes;
pthread_mutex_unlock(&upload_mutex);
psync_send_status_update();
}
void psync_upload_sub_bytes_uploaded(uint64_t bytes){
pthread_mutex_lock(&upload_mutex);
psync_status.bytesuploaded-=bytes;
pthread_mutex_unlock(&upload_mutex);
psync_send_status_update();
}
static void task_wait_no_uploads(){
pthread_mutex_lock(¤t_uploads_mutex);
while (psync_status.filesuploading){
current_uploads_waiters++;
pthread_cond_wait(¤t_uploads_cond, ¤t_uploads_mutex);
current_uploads_waiters--;
}
pthread_mutex_unlock(¤t_uploads_mutex);
}
static int64_t do_run_command_res(const char *cmd, size_t cmdlen, const binparam *params, size_t paramscnt){
psync_socket *api;
binresult *res;
uint64_t result;
api=psync_apipool_get();
if (unlikely(!api))
return -1;
res=do_send_command(api, cmd, cmdlen, params, paramscnt, -1, 1);
if (likely(res))
psync_apipool_release(api);
else{
psync_apipool_release_bad(api);
return -1;
}
result=psync_find_result(res, "result", PARAM_NUM)->num;
psync_free(res);
if (unlikely(result)){
psync_process_api_error(result);
debug(D_WARNING, "command %s returned code %u", cmd, (unsigned)result);
}
return result;
}
static int do_run_command(const char *cmd, size_t cmdlen, const binparam *params, size_t paramscnt){
uint64_t result;
result=do_run_command_res(cmd, cmdlen, params, paramscnt);
if (unlikely(result)){
psync_process_api_error(result);
debug(D_WARNING, "command %s returned code %u", cmd, (unsigned)result);
return psync_handle_api_result(result)==PSYNC_NET_TEMPFAIL?-1:0;
}
else
return 0;
}
#define run_command(cmd, params) do_run_command(cmd, strlen(cmd), params, sizeof(params)/sizeof(binparam))
#define run_command_res(cmd, params) do_run_command_res(cmd, strlen(cmd), params, sizeof(params)/sizeof(binparam))
static int task_createfolder(psync_syncid_t syncid, psync_folderid_t localfolderid, const char *name){
psync_sql_res *res;
psync_uint_row row;
psync_folderid_t parentfolderid, folderid;
psync_socket *api;
binresult *bres;
char *nname;
uint64_t result;
int ret;
res=psync_sql_query_rdlock("SELECT s.folderid FROM localfolder l, syncedfolder s WHERE l.id=? AND l.syncid=? AND l.localparentfolderid=s.localfolderid AND s.syncid=?");
psync_sql_bind_uint(res, 1, localfolderid);
psync_sql_bind_uint(res, 2, syncid);
psync_sql_bind_uint(res, 3, syncid);
if (likely_log(row=psync_sql_fetch_rowint(res)))
parentfolderid=row[0];
else
parentfolderid=PSYNC_INVALID_FOLDERID;
psync_sql_free_result(res);
nname=psync_strnormalize_filename(name);
if (unlikely(parentfolderid==PSYNC_INVALID_FOLDERID)){
psync_free(nname);
return 0;
}
else{
binparam params[]={P_STR("auth", psync_my_auth), P_NUM("folderid", parentfolderid), P_STR("name", nname)};
api=psync_apipool_get();
if (unlikely(!api)){
psync_free(nname);
return -1;
}
psync_diff_lock();
bres=send_command(api, "createfolderifnotexists", params);
psync_free(nname);
if (likely(bres))
psync_apipool_release(api);
else{
psync_diff_unlock();
psync_apipool_release_bad(api);
return -1;
}
result=psync_find_result(bres, "result", PARAM_NUM)->num;
if (unlikely(result)){
psync_diff_unlock();
debug(D_WARNING, "command createfolderifnotexists returned code %u", (unsigned)result);
psync_process_api_error(result);
if (psync_handle_api_result(result)==PSYNC_NET_TEMPFAIL)
return -1;
else
return 0;
}
folderid=psync_find_result(psync_find_result(bres, "metadata", PARAM_HASH), "folderid", PARAM_NUM)->num;
psync_free(bres);
debug(D_NOTICE, "remote folder %lu %lu/%s created", (long unsigned)folderid, (long unsigned)parentfolderid, name);
psync_sql_start_transaction();
res=psync_sql_prep_statement("UPDATE localfolder SET folderid=? WHERE id=? AND syncid=?");
psync_sql_bind_uint(res, 1, folderid);
psync_sql_bind_uint(res, 2, localfolderid);
psync_sql_bind_uint(res, 3, syncid);
psync_sql_run_free(res);
res=psync_sql_prep_statement("UPDATE syncedfolder SET folderid=? WHERE localfolderid=? AND syncid=?");
psync_sql_bind_uint(res, 1, folderid);
psync_sql_bind_uint(res, 2, localfolderid);
psync_sql_bind_uint(res, 3, syncid);
psync_sql_run_free(res);
ret=psync_sql_commit_transaction();
psync_diff_unlock();
return ret;
}
}
static int task_renameremotefile(psync_fileid_t fileid, psync_folderid_t newparentfolderid, const char *newname){
binparam params[]={P_STR("auth", psync_my_auth), P_NUM("fileid", fileid), P_NUM("tofolderid", newparentfolderid), P_STR("toname", newname)};
int ret;
ret=run_command("renamefile", params);
if (likely(!ret))
debug(D_NOTICE, "remote fileid %lu moved/renamed to (%lu)/%s", (long unsigned)fileid, (long unsigned)newparentfolderid, newname);
return ret;
}
static int task_renamefile(psync_syncid_t syncid, psync_fileid_t localfileid, psync_folderid_t newlocalparentfolderid, const char *newname){
psync_sql_res *res;
psync_uint_row row;
psync_fileid_t fileid;
psync_folderid_t folderid;
char *nname;
int ret;
task_wait_no_uploads();
res=psync_sql_query_rdlock("SELECT fileid FROM localfile WHERE id=?");
psync_sql_bind_uint(res, 1, localfileid);
if ((row=psync_sql_fetch_rowint(res)))
fileid=row[0];
else
fileid=0;
psync_sql_free_result(res);
res=psync_sql_query_rdlock("SELECT folderid FROM syncedfolder WHERE syncid=? AND localfolderid=?");
psync_sql_bind_uint(res, 1, syncid);
psync_sql_bind_uint(res, 2, newlocalparentfolderid);
if ((row=psync_sql_fetch_rowint(res)))
folderid=row[0];
else
folderid=0;
psync_sql_free_result(res);
if (unlikely_log(!fileid) || unlikely_log(!folderid))
return 0;
else{
nname=psync_strnormalize_filename(newname);
ret=task_renameremotefile(fileid, folderid, nname);
psync_free(nname);
return ret;
}
}
static int task_renameremotefolder(psync_folderid_t folderid, psync_folderid_t newparentfolderid, const char *newname){
binparam params[]={P_STR("auth", psync_my_auth), P_NUM("folderid", folderid), P_NUM("tofolderid", newparentfolderid), P_STR("toname", newname)};
int ret;
ret=run_command("renamefolder", params);
if (likely(!ret))
debug(D_NOTICE, "remote folderid %lu moved/renamed to (%lu)/%s", (long unsigned)folderid, (long unsigned)newparentfolderid, newname);
return ret;
}
static int task_renamefolder(psync_syncid_t syncid, psync_fileid_t localfolderid, psync_folderid_t newlocalparentfolderid, const char *newname){
psync_sql_res *res;
psync_uint_row row;
psync_folderid_t folderid, parentfolderid;
char *nname;
int ret;
task_wait_no_uploads();
res=psync_sql_query_rdlock("SELECT folderid FROM syncedfolder WHERE syncid=? AND localfolderid=?");
psync_sql_bind_uint(res, 1, syncid);
psync_sql_bind_uint(res, 2, localfolderid);
if ((row=psync_sql_fetch_rowint(res)))
folderid=row[0];
else
folderid=0;
psync_sql_free_result(res);
res=psync_sql_query_rdlock("SELECT folderid FROM syncedfolder WHERE syncid=? AND localfolderid=?");
psync_sql_bind_uint(res, 1, syncid);
psync_sql_bind_uint(res, 2, newlocalparentfolderid);
if ((row=psync_sql_fetch_rowint(res)))
parentfolderid=row[0];
else
parentfolderid=0;
psync_sql_free_result(res);
if (unlikely_log(!folderid) || unlikely_log(!parentfolderid))
return 0;
else{
nname=psync_strnormalize_filename(newname);
ret=task_renameremotefolder(folderid, parentfolderid, nname);
psync_free(nname);
return ret;
}
}
static void set_local_file_remote_id(psync_fileid_t localfileid, psync_fileid_t fileid, uint64_t hash){
psync_sql_res *res;
res=psync_sql_prep_statement("UPDATE localfile SET fileid=?, hash=? WHERE id=?");
psync_sql_bind_uint(res, 1, fileid);
psync_sql_bind_uint(res, 2, hash);
psync_sql_bind_uint(res, 3, localfileid);
psync_sql_run_free(res);
}
static void set_local_file_conflicted(psync_fileid_t localfileid, psync_fileid_t fileid, uint64_t hash, const char *localpath, const char *newname,
uint64_t taskid){
psync_sql_res *res;
char *newpath;
debug(D_NOTICE, "conflict found, renaming %s to %s", localpath, newname);
psync_sql_start_transaction();
res=psync_sql_prep_statement("UPDATE localfile SET fileid=?, hash=?, name=? WHERE id=?");
psync_sql_bind_uint(res, 1, fileid);
psync_sql_bind_uint(res, 2, hash);
psync_sql_bind_string(res, 3, newname);
psync_sql_bind_uint(res, 4, localfileid);
psync_sql_run_free(res);
res=psync_sql_prep_statement("UPDATE task SET name=? WHERE id=?");
psync_sql_bind_string(res, 1, newname);
psync_sql_bind_uint(res, 2, taskid);
psync_sql_run_free(res);
newpath=psync_local_path_for_local_file(localfileid, NULL);
if (unlikely_log(psync_file_rename_overwrite(localpath, newpath)))
psync_sql_rollback_transaction();
else
psync_sql_commit_transaction();
psync_free(newpath);
}
static int copy_file(psync_fileid_t fileid, uint64_t hash, psync_folderid_t folderid, const char *name, psync_fileid_t localfileid){
binparam params[]={P_STR("auth", psync_my_auth), P_NUM("fileid", fileid), P_NUM("hash", hash), P_NUM("tofolderid", folderid), P_STR("toname", name)};
psync_socket *api;
binresult *res;
const binresult *meta;
uint64_t result;
api=psync_apipool_get();
if (unlikely(!api))
return -1;
psync_diff_lock();
res=send_command(api, "copyfile", params);
if (likely(res))
psync_apipool_release(api);
else{
psync_diff_unlock();
psync_apipool_release_bad(api);
return -1;
}
result=psync_find_result(res, "result", PARAM_NUM)->num;
if (unlikely(result)){
psync_diff_unlock();
psync_free(res);
debug(D_WARNING, "command copyfile returned code %u", (unsigned)result);
psync_process_api_error(result);
return 0;
}
meta=psync_find_result(res, "metadata", PARAM_HASH);
set_local_file_remote_id(localfileid, psync_find_result(meta, "fileid", PARAM_NUM)->num, psync_find_result(meta, "hash", PARAM_NUM)->num);
psync_diff_unlock();
psync_free(res);
return 1;
}
static int check_file_if_exists(const unsigned char *hashhex, uint64_t fsize, psync_folderid_t folderid, const char *name, psync_fileid_t localfileid){
psync_sql_res *res;
psync_uint_row row;
psync_fileid_t fileid;
uint64_t filesize, hash;
unsigned char shashhex[PSYNC_HASH_DIGEST_HEXLEN];
int ret;
res=psync_sql_query_rdlock("SELECT id, size FROM file WHERE parentfolderid=? AND name=?");
psync_sql_bind_uint(res, 1, folderid);
psync_sql_bind_string(res, 2, name);
row=psync_sql_fetch_rowint(res);
if (row && row[1]==fsize){
fileid=row[0];
psync_sql_free_result(res);
ret=psync_get_remote_file_checksum(fileid, shashhex, &filesize, &hash);
if (ret==PSYNC_NET_OK){
if (filesize==fsize && !memcmp(hashhex, shashhex, PSYNC_HASH_DIGEST_HEXLEN)){
debug(D_NOTICE, "file %lu/%s already exists and matches local checksum, not doing anything", (unsigned long)folderid, name);
set_local_file_remote_id(localfileid, fileid, hash);
return 1;
}
else
return 0;
}
else if (ret==PSYNC_NET_TEMPFAIL)
return -1;
else if (ret==PSYNC_NET_PERMFAIL)
return 0;
}
psync_sql_free_result(res);
return 0;
}
static int copy_file_if_exists(const unsigned char *hashhex, uint64_t fsize, psync_folderid_t folderid, const char *name, psync_fileid_t localfileid){
binparam params[]={P_STR("auth", psync_my_auth), P_NUM("size", fsize), P_LSTR(PSYNC_CHECKSUM, hashhex, PSYNC_HASH_DIGEST_HEXLEN)};
psync_socket *api;
binresult *res;
const binresult *metas, *meta;
uint64_t result;
int ret;
api=psync_apipool_get();
if (unlikely(!api))
return -1;
res=send_command(api, "getfilesbychecksum", params);
if (likely(res))
psync_apipool_release(api);
else{
psync_apipool_release_bad(api);
return -1;
}
result=psync_find_result(res, "result", PARAM_NUM)->num;
if (unlikely(result)){
psync_free(res);
debug(D_WARNING, "command getfilesbychecksum returned code %u", (unsigned)result);
psync_process_api_error(result);
return 0;
}
metas=psync_find_result(res, "metadata", PARAM_ARRAY);
if (!metas->length){
psync_free(res);
return 0;
}
meta=metas->array[0];
ret=copy_file(psync_find_result(meta, "fileid", PARAM_NUM)->num, psync_find_result(meta, "hash", PARAM_NUM)->num, folderid, name, localfileid);
if (ret==1)
debug(D_NOTICE, "file %lu/%s copied to %lu/%s instead of uploading due to matching checksum",
(long unsigned)psync_find_result(meta, "parentfolderid", PARAM_NUM)->num, psync_find_result(meta, "name", PARAM_STR)->str,
(long unsigned)folderid, name);
psync_free(res);
return ret;
}
static void wake_upload_when_ready(){
if (current_uploads_waiters && psync_status.filesuploading<PSYNC_MAX_PARALLEL_UPLOADS &&
psync_status.bytestouploadcurrent-psync_status.bytesuploaded<=PSYNC_START_NEW_UPLOADS_TRESHOLD)
pthread_cond_signal(¤t_uploads_cond);
}
static void add_bytes_uploaded(uint64_t bytes){
pthread_mutex_lock(¤t_uploads_mutex);
psync_status.bytesuploaded+=bytes;
wake_upload_when_ready();
pthread_mutex_unlock(¤t_uploads_mutex);
psync_send_status_update();
}
static int upload_file(const char *localpath, const unsigned char *hashhex, uint64_t fsize, psync_folderid_t folderid, const char *name,
psync_fileid_t localfileid, psync_syncid_t syncid, upload_list_t *upload, binparam pr){
binparam params[]={P_STR("auth", psync_my_auth), P_NUM("folderid", folderid), P_STR("filename", name), P_BOOL("nopartial", 1),
{pr.paramtype, pr.paramnamelen, pr.opts, pr.paramname, {pr.num}} /* specially for Visual Studio compiler */};
psync_socket *api;
void *buff;
binresult *res;
const binresult *meta;
const char *hashhexsrv;
psync_sql_res *sres;
uint64_t bw, result, fileid, rsize, hash;
size_t rd;
ssize_t rrd;
psync_file_t fd;
fd=psync_file_open(localpath, P_O_RDONLY, 0);
if (fd==INVALID_HANDLE_VALUE){
debug(D_WARNING, "could not open local file %s", localpath);
return -1;
}
api=psync_apipool_get();
if (unlikely(!api))
goto err0;
if (unlikely_log(!do_send_command(api, "uploadfile", strlen("uploadfile"), params, ARRAY_SIZE(params), fsize, 0)))
goto err1;
bw=0;
buff=psync_malloc(PSYNC_COPY_BUFFER_SIZE);
while (bw<fsize){
if (unlikely(upload->stop)){
debug(D_NOTICE, "upload of %s stopped", localpath);
goto err2;
}
psync_wait_statuses_array(requiredstatuses, ARRAY_SIZE(requiredstatuses));
if (fsize-bw>PSYNC_COPY_BUFFER_SIZE)
rd=PSYNC_COPY_BUFFER_SIZE;
else
rd=fsize-bw;
rrd=psync_file_read(fd, buff, rd);
if (unlikely_log(rrd<=0))
goto err2;
if (unlikely_log(psync_socket_writeall_upload(api, buff, rrd)!=rrd))
goto err2;
bw+=rrd;
if (bw==fsize && psync_file_read(fd, buff, 1)!=0){
debug(D_WARNING, "file %s has grown while uploading, retrying", localpath);
goto err2;
}
upload->uploaded+=rrd;
add_bytes_uploaded(rrd);
}
psync_free(buff);
psync_file_close(fd);
psync_set_default_sendbuf(api);
psync_diff_lock();
res=get_result(api);
if (likely(res))
psync_apipool_release(api);
else{
psync_apipool_release_bad(api);
goto err00;
}
result=psync_find_result(res, "result", PARAM_NUM)->num;
if (unlikely(result)){
psync_free(res);
debug(D_WARNING, "command uploadfile returned code %u", (unsigned)result);
psync_process_api_error(result);
if (psync_handle_api_result(result)==PSYNC_NET_TEMPFAIL)
goto err00;
else{
psync_diff_unlock();
return 0;
}
}
meta=psync_find_result(res, "metadata", PARAM_ARRAY)->array[0];
fileid=psync_find_result(meta, "fileid", PARAM_NUM)->num;
hash=psync_find_result(meta, "hash", PARAM_NUM)->num;
rsize=psync_find_result(meta, "size", PARAM_NUM)->num;
hashhexsrv=psync_find_result(psync_find_result(res, "checksums", PARAM_ARRAY)->array[0], PSYNC_CHECKSUM, PARAM_STR)->str;
psync_sql_start_transaction();
sres=psync_sql_prep_statement("REPLACE INTO hashchecksum (hash, size, checksum) VALUES (?, ?, ?)");
psync_sql_bind_uint(sres, 1, hash);
psync_sql_bind_uint(sres, 2, rsize);
psync_sql_bind_lstring(sres, 3, hashhexsrv, PSYNC_HASH_DIGEST_HEXLEN);
psync_sql_run_free(sres);
if (psync_check_result(meta, "conflicted", PARAM_BOOL)){
psync_sql_commit_transaction();
set_local_file_conflicted(localfileid, fileid, hash, localpath, psync_find_result(meta, "name", PARAM_STR)->str, upload->taskid);
}
else{
set_local_file_remote_id(localfileid, fileid, hash);
psync_sql_commit_transaction();
}
psync_diff_unlock();
psync_free(res);
if (unlikely_log(rsize!=fsize) || unlikely_log(memcmp(hashhexsrv, hashhex, PSYNC_HASH_DIGEST_HEXLEN)))
return -1;
debug(D_NOTICE, "file %s uploaded to %lu/%s", localpath, (long unsigned)folderid, name);
return 0;
err2:
psync_free(buff);
err1:
psync_apipool_release_bad(api);
err0:
psync_file_close(fd);
err00:
psync_diff_unlock();
return -1;
}
static int upload_range(psync_socket *api, psync_upload_range_list_t *r, upload_list_t *upload, psync_uploadid_t uploadid, psync_file_t fd){
binparam params[]={P_STR("auth", psync_my_auth), P_NUM("uploadoffset", r->uploadoffset), P_NUM("id", r->id), P_NUM("uploadid", uploadid)};
void *buff;
uint64_t bw;
size_t rd;
ssize_t rrd;
if (unlikely_log(psync_file_seek(fd, r->off, P_SEEK_SET)==-1) ||
unlikely_log(!do_send_command(api, "upload_write", strlen("upload_write"), params, ARRAY_SIZE(params), r->len, 0)))
return PSYNC_NET_TEMPFAIL;
bw=0;
buff=psync_malloc(PSYNC_COPY_BUFFER_SIZE);
while (bw<r->len){
if (unlikely(upload->stop)){
debug(D_NOTICE, "upload stopped");
goto err0;
}
psync_wait_statuses_array(requiredstatuses, ARRAY_SIZE(requiredstatuses));
if (r->len-bw>PSYNC_COPY_BUFFER_SIZE)
rd=PSYNC_COPY_BUFFER_SIZE;
else
rd=r->len-bw;
rrd=psync_file_read(fd, buff, rd);
if (unlikely_log(rrd<=0))
goto err0;
bw+=rrd;
if (unlikely_log(psync_socket_writeall_upload(api, buff, rrd)!=rrd))
goto err0;
upload->uploaded+=rrd;
add_bytes_uploaded(rrd);
}
psync_free(buff);
return PSYNC_NET_OK;
err0:
psync_free(buff);
return PSYNC_NET_TEMPFAIL;
}
static int upload_from_file(psync_socket *api, psync_upload_range_list_t *r, psync_uploadid_t uploadid, upload_list_t *upload){
binparam params[]={P_STR("auth", psync_my_auth), P_NUM("uploadoffset", r->uploadoffset), P_NUM("id", r->id), P_NUM("uploadid", uploadid),
P_NUM("fileid", r->file.fileid), P_NUM("hash", r->file.hash), P_NUM("offset", r->off), P_NUM("count", r->len)};
if (unlikely_log(!send_command_no_res(api, "upload_writefromfile", params)))
return PSYNC_NET_TEMPFAIL;
else{
upload->uploaded+=r->len;
add_bytes_uploaded(r->len);
return PSYNC_NET_OK;
}
}
static int upload_from_upload(psync_socket *api, psync_upload_range_list_t *r, psync_uploadid_t uploadid, upload_list_t *upload){
binparam params[]={P_STR("auth", psync_my_auth), P_NUM("uploadoffset", r->uploadoffset), P_NUM("id", r->id), P_NUM("uploadid", uploadid),
P_NUM("readuploadid", r->uploadid), P_NUM("offset", r->off), P_NUM("count", r->len)};
if (unlikely_log(!send_command_no_res(api, "upload_writefromupload", params)))
return PSYNC_NET_TEMPFAIL;
else{
upload->uploaded+=r->len;
add_bytes_uploaded(r->len);
return PSYNC_NET_OK;
}
}
static int upload_get_checksum(psync_socket *api, psync_uploadid_t uploadid, uint32_t id){
binparam params[]={P_STR("auth", psync_my_auth), P_NUM("uploadid", uploadid), P_NUM("id", id)};
if (unlikely_log(!send_command_no_res(api, "upload_info", params)))
return PSYNC_NET_TEMPFAIL;
else
return PSYNC_NET_OK;
}
static int upload_save(psync_socket *api, psync_fileid_t localfileid, const char *localpath, const unsigned char *hashhex, uint64_t size,
psync_uploadid_t uploadid, psync_folderid_t folderid, const char *name, uint64_t taskid, binparam pr){
binparam params[]={P_STR("auth", psync_my_auth), P_NUM("folderid", folderid), P_STR("name", name), P_NUM("uploadid", uploadid),
{pr.paramtype, pr.paramnamelen, pr.opts, pr.paramname, {pr.num}} /* specially for Visual Studio compiler */};
psync_sql_res *sres;
binresult *res;
const binresult *meta;
psync_fileid_t fileid;
uint64_t result, hash;
int ret;
psync_diff_lock();
res=send_command(api, "upload_save", params);
if (res){
result=psync_find_result(res, "result", PARAM_NUM)->num;
if (unlikely(result)){
debug(D_WARNING, "command upload_save returned code %u", (unsigned)result);
psync_process_api_error(result);
ret=psync_handle_api_result(result);
}
else{
meta=psync_find_result(res, "metadata", PARAM_HASH);
fileid=psync_find_result(meta, "fileid", PARAM_NUM)->num;
hash=psync_find_result(meta, "hash", PARAM_NUM)->num;
psync_sql_start_transaction();
sres=psync_sql_prep_statement("REPLACE INTO hashchecksum (hash, size, checksum) VALUES (?, ?, ?)");
psync_sql_bind_uint(sres, 1, hash);
psync_sql_bind_uint(sres, 2, size);
psync_sql_bind_lstring(sres, 3, (const char *)hashhex, PSYNC_HASH_DIGEST_HEXLEN);
psync_sql_run_free(sres);
if (psync_check_result(meta, "conflicted", PARAM_BOOL))
set_local_file_conflicted(localfileid, fileid, hash, localpath, psync_find_result(meta, "name", PARAM_STR)->str, taskid);
else
set_local_file_remote_id(localfileid, fileid, hash);
psync_sql_commit_transaction();
ret=PSYNC_NET_OK;
}
psync_free(res);
}
else
ret=PSYNC_NET_TEMPFAIL;
psync_diff_unlock();
return ret;
}
static int upload_big_file(const char *localpath, const unsigned char *hashhex, uint64_t fsize, psync_folderid_t folderid, const char *name,
psync_fileid_t localfileid, psync_syncid_t syncid, upload_list_t *upload, psync_uploadid_t uploadid, uint64_t uploadoffset, binparam pr){
psync_socket *api;
binresult *res;
psync_sql_res *sql;
psync_uint_row row;
psync_full_result_int *fr;
psync_upload_range_list_t *le, *le2;
psync_list rlist;
uint64_t result;
uint32_t rid, respwait, id;
psync_file_t fd;
int ret;
debug(D_NOTICE, "uploading file %s with repeating block inspection", localpath);
if (uploadoffset){
debug(D_NOTICE, "resuming from position %lu", (unsigned long)uploadoffset);
upload->uploaded+=uploadoffset;
add_bytes_uploaded(uploadoffset);
}
api=psync_apipool_get();
if (unlikely(!api))
return -1;
if (!uploadid){
binparam params[]={P_STR("auth", psync_my_auth), P_NUM("filesize", fsize)};
res=send_command(api, "upload_create", params);
if (!res)
goto err0;
result=psync_find_result(res, "result", PARAM_NUM)->num;
if (unlikely(result)){
psync_free(res);
psync_apipool_release(api);
psync_process_api_error(result);
debug(D_WARNING, "upload_create returned %lu", (unsigned long)result);
if (psync_handle_api_result(result)==PSYNC_NET_TEMPFAIL)
return -1;
else
return 0;
}
uploadid=psync_find_result(res, "uploadid", PARAM_NUM)->num;
psync_free(res);
sql=psync_sql_prep_statement("INSERT INTO localfileupload (localfileid, uploadid) VALUES (?, ?)");
psync_sql_bind_uint(sql, 1, localfileid);
psync_sql_bind_uint(sql, 2, uploadid);
psync_sql_run_free(sql);
}
psync_list_init(&rlist);
if (likely(uploadoffset<fsize)){
le=psync_new(psync_upload_range_list_t);
le->uploadoffset=uploadoffset;
le->off=uploadoffset;
le->len=fsize-uploadoffset;
le->type=PSYNC_URANGE_UPLOAD;
psync_list_add_tail(&rlist, &le->list);
}
fd=psync_file_open(localpath, P_O_RDONLY, 0);
if (unlikely(fd==INVALID_HANDLE_VALUE)){
debug(D_WARNING, "could not open local file %s", localpath);
psync_apipool_release(api);
psync_list_for_each_element_call(&rlist, psync_upload_range_list_t, list, psync_free);
return -1;
}
if (likely(uploadoffset<fsize)){
sql=psync_sql_query_rdlock("SELECT fileid, hash FROM localfile WHERE id=?");
psync_sql_bind_uint(sql, 1, localfileid);
if ((row=psync_sql_fetch_rowint(sql))){
uint64_t fileid, hash;
fileid=row[0];
hash=row[1];
psync_sql_free_result(sql);
if (fileid && psync_net_scan_file_for_blocks(api, &rlist, fileid, hash, fd)==PSYNC_NET_TEMPFAIL)
goto err1;
}
else
psync_sql_free_result(sql);
sql=psync_sql_query_rdlock("SELECT uploadid FROM localfileupload WHERE localfileid=? ORDER BY uploadid DESC LIMIT 5");
psync_sql_bind_uint(sql, 1, localfileid);
fr=psync_sql_fetchall_int(sql);
for (id=0; id<fr->rows; id++)
if (psync_get_result_cell(fr, id, 0)!=uploadid && psync_net_scan_upload_for_blocks(api, &rlist, psync_get_result_cell(fr, id, 0), fd)==PSYNC_NET_TEMPFAIL){
psync_free(fr);
goto err1;
}
psync_free(fr);
}
rid=0;
respwait=0;
le=psync_new(psync_upload_range_list_t);
le->type=PSYNC_URANGE_LAST;
psync_list_add_tail(&rlist, &le->list);
psync_list_for_each_element(le, &rlist, psync_upload_range_list_t, list){
if (upload->stop)
goto err1;
le->uploadoffset=uploadoffset;
le->id=++rid;
if (le->type==PSYNC_URANGE_LAST){
if (upload_get_checksum(api, uploadid, le->id))
goto err1;
else
respwait++;
}
while (respwait && (le->type==PSYNC_URANGE_LAST || psync_socket_pendingdata(api) || psync_select_in(&api->sock, 1, 0)!=SOCKET_ERROR)){
res=get_result(api);
if (unlikely_log(!res))
goto err1;
respwait--;
result=psync_find_result(res, "result", PARAM_NUM)->num;
if (unlikely(result)){
id=psync_find_result(res, "id", PARAM_NUM)->num;
psync_free(res);
psync_process_api_error(result);
if (unlikely_log(!id))
goto err1;
while (respwait){
res=get_result(api);
if (unlikely_log(!res))
goto err1;
respwait--;
psync_free(res);
}
psync_list_for_each_element(le2, &rlist, psync_upload_range_list_t, list)
if (le2->id==id){
if (le2->type==PSYNC_URANGE_LAST || le2->type==PSYNC_URANGE_UPLOAD){
debug(D_ERROR, "range of type %u failed with error %lu", (unsigned)le2->type, (unsigned long)result);
goto err1;
}
else{
debug(D_WARNING, "range of type %u failed with error %lu, restarting as upload range", (unsigned)le2->type, (unsigned long)result);
le2->type=PSYNC_URANGE_UPLOAD;
uploadoffset=le2->uploadoffset;
le2->off=le2->uploadoffset;
le=le2;
goto restart;
}
}
debug(D_BUG, "could not find id %u", (unsigned)id);
goto err1;
}
else if (le->type==PSYNC_URANGE_LAST && le->id==psync_find_result(res, "id", PARAM_NUM)->num){
if (unlikely(psync_find_result(res, "size", PARAM_NUM)->num!=fsize)){
debug(D_WARNING, "file size mismatch after upload, expected: %lu, got: %lu", (unsigned long)fsize,
(unsigned long)psync_find_result(res, "size", PARAM_NUM)->num);
psync_free(res);
goto err1;
}
else if (unlikely(memcmp(psync_find_result(res, PSYNC_CHECKSUM, PARAM_STR)->str, hashhex, PSYNC_HASH_DIGEST_HEXLEN))){
debug(D_WARNING, "hash mismatch after upload, expected: %."NTO_STR(PSYNC_HASH_DIGEST_HEXLEN)
"s, got: %."NTO_STR(PSYNC_HASH_DIGEST_HEXLEN)"s", hashhex,
psync_find_result(res, PSYNC_CHECKSUM, PARAM_STR)->str);
psync_free(res);
goto err1;
}
else
assert(respwait==0);
}
psync_free(res);
}
restart:
if (le->type==PSYNC_URANGE_UPLOAD){
debug(D_NOTICE, "uploading %lu bytes", (unsigned long)le->len);
ret=upload_range(api, le, upload, uploadid, fd);
}
else if (le->type==PSYNC_URANGE_COPY_FILE){
debug(D_NOTICE, "copying %lu bytes from fileid %lu hash %lu offset %lu", (unsigned long)le->len, (unsigned long)le->file.fileid,
(unsigned long)le->file.hash, (unsigned long)le->off);
ret=upload_from_file(api, le, uploadid, upload);
}
else if (le->type==PSYNC_URANGE_COPY_UPLOAD){
debug(D_NOTICE, "copying %lu bytes from uploadid %lu offset %lu", (unsigned long)le->len, (unsigned long)le->uploadid, (unsigned long)le->off);
ret=upload_from_upload(api, le, uploadid, upload);
}
else if (le->type==PSYNC_URANGE_LAST)
break;
else {
debug(D_BUG, "Invalid range type %u", (unsigned)le->type);
goto err1;
}
if (unlikely_log(ret!=PSYNC_NET_OK)){
if (ret==PSYNC_NET_TEMPFAIL)
goto err1;
else
goto errp;
}
respwait++;
uploadoffset+=le->len;
}
psync_list_for_each_element_call(&rlist, psync_upload_range_list_t, list, psync_free);
if (psync_file_size(fd)!=fsize){
debug(D_NOTICE, "file %s changed filesize while uploading, restarting task", localpath);
ret=PSYNC_NET_TEMPFAIL;
}
else
ret=PSYNC_NET_OK;
psync_file_close(fd);
if (ret==PSYNC_NET_OK)
ret=upload_save(api, localfileid, localpath, hashhex, fsize, uploadid, folderid, name, upload->taskid, pr);
if (ret==PSYNC_NET_TEMPFAIL){
psync_apipool_release_bad(api);
return -1;
}
else{
psync_apipool_release(api);
return 0;
}
err1:
psync_file_close(fd);
psync_list_for_each_element_call(&rlist, psync_upload_range_list_t, list, psync_free);
err0:
psync_apipool_release_bad(api);
return -1;
errp:
psync_file_close(fd);
psync_list_for_each_element_call(&rlist, psync_upload_range_list_t, list, psync_free);
psync_apipool_release_bad(api);
return 0;
}
static void delete_uploadid(psync_uploadid_t uploadid){
binparam params[]={P_STR("auth", psync_my_auth), P_NUM("uploadid", uploadid)};
psync_socket *api;
binresult *res;
api=psync_apipool_get();
if (unlikely(!api))
return;
res=send_command(api, "upload_delete", params);
if (res)
psync_apipool_release(api);
else
psync_apipool_release_bad(api);
psync_free(res);
}
static void delete_uploadids(psync_fileid_t localfileid){
psync_sql_res *res;
psync_full_result_int *rows;
uint32_t i;
res=psync_sql_query_rdlock("SELECT uploadid FROM localfileupload WHERE localfileid=?");
psync_sql_bind_uint(res, 1, localfileid);
rows=psync_sql_fetchall_int(res);
if (rows->rows){
for (i=0; i<rows->rows; i++)
delete_uploadid(psync_get_result_cell(rows, i, 0));
res=psync_sql_prep_statement("DELETE FROM localfileupload WHERE localfileid=?");
psync_sql_bind_uint(res, 1, localfileid);
psync_sql_run_free(res);
}
psync_free(rows);
}
static int task_uploadfile(psync_syncid_t syncid, psync_folderid_t localfileid, const char *name, upload_list_t *upload){
psync_sql_res *res;
psync_uint_row row;
char *localpath, *nname;
psync_file_lock_t *lock;
psync_folderid_t folderid;
psync_uploadid_t uploadid;
uint64_t fsize, ufsize;
psync_stat_t st;
unsigned char hashhex[PSYNC_HASH_DIGEST_HEXLEN], uhashhex[PSYNC_HASH_DIGEST_HEXLEN], phashhex[PSYNC_HASH_DIGEST_HEXLEN];
binparam pr;
int ret;
psync_wait_statuses_array(requiredstatuses, ARRAY_SIZE(requiredstatuses));
if (upload->stop)
return -1;
localpath=psync_local_path_for_local_file(localfileid, NULL);
if (unlikely(!localpath)){
debug(D_WARNING, "could not find local file %s (id %lu)", name, (unsigned long)localfileid);
return 0;
}
if (!psync_stat(localpath, &st) && psync_stat_mtime(&st)>=psync_timer_time()-PSYNC_UPLOAD_OLDER_THAN_SEC){
time_t ctime;
debug(D_NOTICE, "file %s is too new, waiting for upload", localpath);
ctime=psync_timer_time();
psync_apipool_prepare();
if (ctime<psync_stat_mtime(&st)-2)
debug(D_NOTICE, "file %s has a modification time in the future, skipping waits", localpath);
else{
ret=0;
do {
psync_milisleep((psync_stat_mtime(&st)+PSYNC_UPLOAD_OLDER_THAN_SEC-ctime)*1000+500);
ctime=psync_timer_time();
if (psync_stat(localpath, &st)){
debug(D_NOTICE, "can not stat %s anymore, failing for now", localpath);
psync_free(localpath);
return -1;
}
} while (psync_stat_mtime(&st)>=psync_timer_time()-PSYNC_UPLOAD_OLDER_THAN_SEC && ++ret<=10);
if (ret==10){
debug(D_NOTICE, "file %s kept changing %d times, skipping for now", localpath, ret);
psync_free(localpath);
return -1;
}
debug(D_NOTICE, "file %s got old enough", localpath);
}
}
lock=psync_lock_file(localpath);
if (!lock){
debug(D_NOTICE, "file %s is currently locked, skipping for now", localpath);
psync_free(localpath);
psync_milisleep(PSYNC_SLEEP_ON_LOCKED_FILE);
return -1;
}
res=psync_sql_query_rdlock("SELECT uploadid FROM localfileupload WHERE localfileid=? ORDER BY uploadid DESC LIMIT 1");
psync_sql_bind_uint(res, 1, localfileid);
if ((row=psync_sql_fetch_rowint(res)))
uploadid=row[0];
else
uploadid=0;
psync_sql_free_result(res);
ufsize=0;
if (uploadid){
ret=psync_get_upload_checksum(uploadid, uhashhex, &ufsize);
if (ret==PSYNC_NET_TEMPFAIL){