-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpfolder.c
More file actions
972 lines (894 loc) · 28.9 KB
/
pfolder.c
File metadata and controls
972 lines (894 loc) · 28.9 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
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
/* 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 <stddef.h>
#include "pfolder.h"
#include "plibs.h"
#include "psettings.h"
#include "plist.h"
#include "pnetlibs.h"
#include "papi.h"
#include "pcloudcrypto.h"
#include "pdiff.h"
#include "pfsfolder.h"
#ifdef P_OS_WINDOWS
#include "Shlobj.h"
#endif
#define INITIAL_NAME_BUFF 2000
#define INITIAL_ENTRY_CNT 128
typedef struct {
pentry_t *entries;
char *namebuff;
size_t nameoff;
size_t namealloc;
uint32_t entriescnt;
uint32_t entriesalloc;
} folder_list;
typedef struct {
folder_list *folderlist;
psync_listtype_t listtype;
} flist_ltype;
typedef struct _string_list {
psync_list list;
char *str;
size_t len;
} string_list;
psync_folderid_t psync_get_folderid_by_path(const char *path){
psync_folderid_t cfolderid;
const char *sl;
psync_sql_res *res;
psync_uint_row row;
size_t len;
res=NULL;
if (*path!='/')
goto err;
cfolderid=0;
while (1){
while (*path=='/')
path++;
if (*path==0){
if (res)
psync_sql_free_result(res);
return cfolderid;
}
sl=strchr(path, '/');
if (sl)
len=sl-path;
else
len=strlen(path);
if (!res){
res=psync_sql_query_rdlock("SELECT id FROM folder WHERE parentfolderid=? AND name=?");
if (unlikely_log(!res)){
psync_error=PERROR_DATABASE_ERROR;
return PSYNC_INVALID_FOLDERID;
}
}
else
psync_sql_reset(res);
psync_sql_bind_uint(res, 1, cfolderid);
psync_sql_bind_lstring(res, 2, path, len);
row=psync_sql_fetch_rowint(res);
if (unlikely_log(!row))
goto err;
cfolderid=row[0];
path+=len;
}
err:
if (res)
psync_sql_free_result(res);
psync_error=PERROR_REMOTE_FOLDER_NOT_FOUND;
return PSYNC_INVALID_FOLDERID;
}
static psync_folderid_t wait_folder_id_in_db(psync_folderid_t folderid){
psync_sql_res *res;
psync_uint_row row;
int tries;
tries=0;
while (++tries<=20){
res=psync_sql_query_rdlock("SELECT id FROM folder WHERE id=?");
psync_sql_bind_uint(res, 1, folderid);
row=psync_sql_fetch_rowint(res);
psync_sql_free_result(res);
if (row)
return folderid;
psync_milisleep(50);
}
return PSYNC_INVALID_FOLDERID;
}
/**********************************************************************************************************/
psync_folderid_t psync_wait_folder_in_local_db(psync_folderid_t folderid) {
return wait_folder_id_in_db(folderid);
}
/**********************************************************************************************************/
psync_folderid_t psync_get_folderid_by_path_or_create(const char *path){
psync_folderid_t cfolderid;
const char *sl;
psync_sql_res *res;
psync_uint_row row;
size_t len;
res=NULL;
if (*path!='/')
goto err;
cfolderid=0;
while (1){
while (*path=='/')
path++;
if (*path==0){
if (res)
psync_sql_free_result(res);
return wait_folder_id_in_db(cfolderid);
}
sl=strchr(path, '/');
if (sl)
len=sl-path;
else
len=strlen(path);
if (!res){
res=psync_sql_query_rdlock("SELECT id FROM folder WHERE parentfolderid=? AND name=?");
if (unlikely_log(!res)){
psync_error=PERROR_DATABASE_ERROR;
return PSYNC_INVALID_FOLDERID;
}
}
else
psync_sql_reset(res);
psync_sql_bind_uint(res, 1, cfolderid);
psync_sql_bind_lstring(res, 2, path, len);
row=psync_sql_fetch_rowint(res);
if (row)
cfolderid=row[0];
else{
binparam params[]={P_STR("auth", psync_my_auth), P_NUM("folderid", cfolderid), P_LSTR("name", path, len)};
psync_socket *api;
binresult *bres;
uint64_t result;
api=psync_apipool_get();
if (unlikely(!api))
goto errnet;
bres=send_command(api, "createfolderifnotexists", params);
if (bres)
psync_apipool_release(api);
else
psync_apipool_release_bad(api);
if (unlikely(!bres))
goto errnet;
result=psync_find_result(bres, "result", PARAM_NUM)->num;
if (result==0){
cfolderid=psync_find_result(psync_find_result(bres, "metadata", PARAM_HASH), "folderid", PARAM_NUM)->num;
if (psync_find_result(bres, "created", PARAM_BOOL)->num)
psync_diff_wake();
psync_free(bres);
}
else{
psync_free(bres);
psync_process_api_error(result);
if (psync_handle_api_result(result)==PSYNC_NET_TEMPFAIL)
goto errnet;
else
goto err;
}
}
path+=len;
}
err:
if (res)
psync_sql_free_result(res);
psync_error=PERROR_REMOTE_FOLDER_NOT_FOUND;
return PSYNC_INVALID_FOLDERID;
errnet:
if (res)
psync_sql_free_result(res);
psync_error=PERROR_OFFLINE;
return PSYNC_INVALID_FOLDERID;
}
static void psync_free_string_list(psync_list *lst){
psync_list_for_each_element_call(lst, string_list, list, psync_free);
}
static string_list *str_to_list_element(const char *str, size_t len){
string_list *le;
le=(string_list *)psync_malloc(sizeof(string_list)+len+1);
le->str=(char *)(le+1);
le->len=len;
memcpy(le->str, str, len+1);
return le;
}
static int psync_add_path_to_list(psync_list *lst, psync_folderid_t folderid){
string_list *e;
psync_sql_res *res;
psync_variant_row row;
const char *str;
size_t len;
while (1){
if (folderid==0){
e=(string_list *)psync_malloc(sizeof(string_list));
e->str=(char *)e;
e->len=0;
psync_list_add_head(lst, &e->list);
return 0;
}
res=psync_sql_query_rdlock("SELECT parentfolderid, name FROM folder WHERE id=?");
psync_sql_bind_uint(res, 1, folderid);
row=psync_sql_fetch_row(res);
if (unlikely(!row))
break;
folderid=psync_get_number(row[0]);
str=psync_get_lstring(row[1], &len);
e=str_to_list_element(str, len);
psync_list_add_head(lst, &e->list);
psync_sql_free_result(res);
}
psync_sql_free_result(res);
debug(D_NOTICE, "folder %lu not found in database", (unsigned long)folderid);
return -1;
}
static string_list *str_list_decode(psync_folderid_t folderid, string_list *e){
psync_crypto_aes256_text_decoder_t dec;
char *fn;
dec=psync_cloud_crypto_get_folder_decoder(folderid);
if (psync_crypto_is_error(dec)){
psync_free(e);
debug(D_WARNING, "got error %d getting decoder for folderid %lu", psync_crypto_to_error(dec), (unsigned long)folderid);
return NULL;
}
fn=psync_cloud_crypto_decode_filename(dec, e->str);
psync_cloud_crypto_release_folder_decoder(folderid, dec);
psync_free(e);
if (unlikely_log(!fn))
return NULL;
e=str_to_list_element(fn, strlen(fn));
psync_free(fn);
return e;
}
static int psync_add_path_to_list_decode(psync_list *lst, psync_folderid_t folderid){
string_list *e, *c;
psync_sql_res *res;
psync_variant_row row;
const char *str;
psync_folderid_t cfolderid;
size_t len;
uint32_t flags;
e=NULL;
while (1){
if (folderid==0){
if (e)
psync_list_add_head(lst, &e->list);
e=(string_list *)psync_malloc(sizeof(string_list));
e->str=(char *)e;
e->len=0;
psync_list_add_head(lst, &e->list);
return 0;
}
res=psync_sql_query_rdlock("SELECT parentfolderid, name, flags FROM folder WHERE id=?");
psync_sql_bind_uint(res, 1, folderid);
row=psync_sql_fetch_row(res);
if (unlikely_log(!row))
break;
cfolderid=folderid;
flags=psync_get_number(row[2]);
folderid=psync_get_number(row[0]);
str=psync_get_lstring(row[1], &len);
c=str_to_list_element(str, len);
psync_sql_free_result(res);
if (e){
if (flags&PSYNC_FOLDER_FLAG_ENCRYPTED){
e=str_list_decode(cfolderid, e);
if (unlikely_log(!e))
goto err;
}
psync_list_add_head(lst, &e->list);
}
e=c;
}
psync_sql_free_result(res);
err:
debug(D_NOTICE, "folder %lu not found in database", (unsigned long)folderid);
return -1;
}
char *psync_join_string_list(const char *sep, psync_list *lst, size_t *retlen){
size_t slen, seplen, cnt;
string_list *e;
char *ret, *str;
slen=cnt=0;
psync_list_for_each_element(e, lst, string_list, list){
slen+=e->len;
cnt++;
}
if (unlikely(!cnt))
return psync_strdup("");
seplen=strlen(sep);
ret=str=psync_malloc(slen+cnt*seplen+1);
psync_list_for_each_element(e, lst, string_list, list){
memcpy(str, e->str, e->len);
str+=e->len;
memcpy(str, sep, seplen);
str+=seplen;
}
str-=seplen;
*str=0;
if (retlen)
*retlen=str-ret;
return ret;
}
char *psync_get_path_by_folderid(psync_folderid_t folderid, size_t *retlen){
psync_list folderlist;
char *ret;
int res;
psync_list_init(&folderlist);
psync_sql_rdlock();
res=psync_add_path_to_list(&folderlist, folderid);
psync_sql_rdunlock();
if (unlikely_log(res)){
psync_free_string_list(&folderlist);
return PSYNC_INVALID_PATH;
}
ret=psync_join_string_list("/", &folderlist, retlen);
psync_free_string_list(&folderlist);
if (!ret[0]){
psync_free(ret);
ret=psync_strdup("/");
if (retlen)
*retlen=1;
}
return ret;
}
char *psync_get_path_by_folderid_sep(psync_folderid_t folderid, const char *sep, size_t *retlen){
psync_list folderlist;
char *ret;
int res;
psync_list_init(&folderlist);
res=psync_add_path_to_list_decode(&folderlist, folderid);
if (unlikely_log(res)){
psync_free_string_list(&folderlist);
return PSYNC_INVALID_PATH;
}
ret=psync_join_string_list(sep, &folderlist, retlen);
psync_free_string_list(&folderlist);
if (!ret[0]){
psync_free(ret);
ret=psync_strdup(sep);
if (retlen)
*retlen=1;
}
return ret;
}
char *psync_get_path_by_fileid(psync_fileid_t fileid, size_t *retlen){
psync_list folderlist;
char *ret;
psync_sql_res *res;
psync_variant_row row;
string_list *e;
const char *str;
psync_folderid_t folderid;
size_t len;
psync_list_init(&folderlist);
psync_sql_rdlock();
res=psync_sql_query_rdlock("SELECT parentfolderid, name FROM file WHERE id=?");
psync_sql_bind_uint(res, 1, fileid);
row=psync_sql_fetch_row(res);
if (unlikely_log(!row)){
psync_sql_free_result(res);
psync_sql_rdunlock();
return PSYNC_INVALID_PATH;
}
folderid=psync_get_number(row[0]);
str=psync_get_lstring(row[1], &len);
e=str_to_list_element(str, len);
psync_list_add_head(&folderlist, &e->list);
psync_sql_free_result(res);
if (unlikely_log(psync_add_path_to_list(&folderlist, folderid))){
psync_sql_rdunlock();
psync_free_string_list(&folderlist);
return PSYNC_INVALID_PATH;
}
psync_sql_rdunlock();
ret=psync_join_string_list("/", &folderlist, retlen);
psync_free_string_list(&folderlist);
return ret;
}
static int psync_add_local_path_to_list_by_localfolderid(psync_list *lst, psync_folderid_t localfolderid, psync_syncid_t syncid){
string_list *e, *le;
psync_sql_res *res;
psync_variant_row row;
const char *str;
size_t len;
res=psync_sql_query_rdlock("SELECT localpath FROM syncfolder WHERE id=?");
psync_sql_bind_uint(res, 1, syncid);
row=psync_sql_fetch_row(res);
if (unlikely(!row)){
debug(D_ERROR, "could not find sync id %lu", (long unsigned)syncid);
psync_sql_free_result(res);
return -1;
}
str=psync_get_lstring(row[0], &len);
le=str_to_list_element(str, len);
psync_sql_free_result(res);
while (1){
if (localfolderid==0){
psync_list_add_head(lst, &le->list);
return 0;
}
res=psync_sql_query_rdlock("SELECT localparentfolderid, name FROM localfolder WHERE id=?");
psync_sql_bind_uint(res, 1, localfolderid);
row=psync_sql_fetch_row(res);
if (unlikely(!row))
break;
localfolderid=psync_get_number(row[0]);
str=psync_get_lstring(row[1], &len);
e=str_to_list_element(str, len);
psync_list_add_head(lst, &e->list);
psync_sql_free_result(res);
}
psync_sql_free_result(res);
psync_list_add_head(lst, &le->list);
debug(D_NOTICE, "local folder %lu not found in database", (unsigned long)localfolderid);
return -1;
}
char *psync_local_path_for_local_folder(psync_folderid_t localfolderid, psync_syncid_t syncid, size_t *retlen){
psync_list folderlist;
char *ret;
int res;
psync_list_init(&folderlist);
psync_sql_rdlock();
res=psync_add_local_path_to_list_by_localfolderid(&folderlist, localfolderid, syncid);
psync_sql_rdunlock();
if (unlikely_log(res)){
psync_free_string_list(&folderlist);
return PSYNC_INVALID_PATH;
}
ret=psync_join_string_list(PSYNC_DIRECTORY_SEPARATOR, &folderlist, retlen);
psync_free_string_list(&folderlist);
return ret;
}
char *psync_local_path_for_local_file(psync_fileid_t localfileid, size_t *retlen){
psync_list folderlist;
char *ret;
const char *str;
psync_sql_res *res;
psync_variant_row row;
string_list *e;
psync_folderid_t localfolderid;
size_t len;
psync_syncid_t syncid;
int rs;
psync_list_init(&folderlist);
psync_sql_rdlock();
res=psync_sql_query_nolock("SELECT localparentfolderid, syncid, name FROM localfile WHERE id=?");
psync_sql_bind_uint(res, 1, localfileid);
if (unlikely_log(!(row=psync_sql_fetch_row(res)))){
psync_sql_free_result(res);
psync_sql_rdunlock();
psync_free_string_list(&folderlist);
return PSYNC_INVALID_PATH;
}
localfolderid=psync_get_number(row[0]);
syncid=psync_get_number(row[1]);
str=psync_get_lstring(row[2], &len);
e=str_to_list_element(str, len);
psync_sql_free_result(res);
psync_list_add_head(&folderlist, &e->list);
rs=psync_add_local_path_to_list_by_localfolderid(&folderlist, localfolderid, syncid);
psync_sql_rdunlock();
if (unlikely_log(rs)){
psync_free_string_list(&folderlist);
return PSYNC_INVALID_PATH;
}
ret=psync_join_string_list(PSYNC_DIRECTORY_SEPARATOR, &folderlist, retlen);
psync_free_string_list(&folderlist);
return ret;
}
static folder_list *folder_list_init(){
folder_list *list;
list=(folder_list *)psync_malloc(sizeof(folder_list));
list->entries=(pentry_t *)psync_malloc(sizeof(pentry_t)*INITIAL_ENTRY_CNT);
list->namebuff=(char *)psync_malloc(INITIAL_NAME_BUFF);
list->nameoff=0;
list->namealloc=INITIAL_NAME_BUFF;
list->entriescnt=0;
list->entriesalloc=INITIAL_ENTRY_CNT;
return list;
}
static void folder_list_add(folder_list *list, pentry_t *entry){
if (list->entriescnt>=list->entriesalloc){
list->entriesalloc*=2;
list->entries=(pentry_t *)psync_realloc(list->entries, sizeof(pentry_t)*list->entriesalloc);
}
while (list->nameoff+entry->namelen>=list->namealloc){
list->namealloc*=2;
list->namebuff=(char *)psync_realloc(list->namebuff, list->namealloc);
}
memcpy(&list->entries[list->entriescnt++], entry, sizeof(pentry_t));
memcpy(list->namebuff+list->nameoff, entry->name, entry->namelen);
list->nameoff+=entry->namelen;
list->namebuff[list->nameoff++]=0;
}
static void folder_list_free(folder_list *list){
psync_free(list->entries);
psync_free(list->namebuff);
psync_free(list);
}
static pfolder_list_t *folder_list_finalize(folder_list *list){
pfolder_list_t *ret;
char *name;
uint32_t i;
//debug(D_NOTICE, "allocating %u bytes for folder list, %u of which for names", (unsigned)(offsetof(pfolder_list_t, entries)+sizeof(pentry_t)*list->entriescnt+list->nameoff), (unsigned)list->nameoff);
ret=(pfolder_list_t *)psync_malloc(offsetof(pfolder_list_t, entries)+sizeof(pentry_t)*list->entriescnt+list->nameoff);
name=((char *)ret)+offsetof(pfolder_list_t, entries)+sizeof(pentry_t)*list->entriescnt;
ret->entrycnt=list->entriescnt;
memcpy(ret->entries, list->entries, sizeof(pentry_t)*list->entriescnt);
memcpy(name, list->namebuff, list->nameoff);
for (i=0; i<list->entriescnt; i++){
ret->entries[i].name=name;
name+=list->entries[i].namelen+1;
}
folder_list_free(list);
return ret;
}
pfolder_list_t *psync_list_remote_folder(psync_folderid_t folderid, psync_listtype_t listtype){
folder_list *list;
psync_sql_res *res;
psync_variant_row row;
size_t namelen;
pentry_t entry;
uint64_t perms, flags;
list=folder_list_init();
char *tmp;
int parentencrypted=0;
if (listtype&PLIST_FOLDERS){
res=psync_sql_query_rdlock("SELECT flags FROM folder WHERE id=?");
psync_sql_bind_uint(res, 1, folderid);
if ((row=psync_sql_fetch_row(res))){
parentencrypted=(psync_get_number(row[0])&PSYNC_FOLDER_FLAG_ENCRYPTED)?1:0;
}
else{
debug(D_ERROR, "Can't find folder with id %"P_PRI_U64, folderid);
psync_sql_free_result(res);
return NULL;
}
psync_sql_free_result(res);
res=psync_sql_query_rdlock("SELECT id, permissions, name, userid, flags FROM folder WHERE parentfolderid=? ORDER BY name");
psync_sql_bind_uint(res, 1, folderid);
while ((row=psync_sql_fetch_row(res))){
entry.folder.folderid=psync_get_number(row[0]);
perms=psync_get_number(row[1]);
flags=psync_get_number(row[4]);
entry.folder.cansyncup=((perms&PSYNC_PERM_WRITE)==PSYNC_PERM_WRITE) &&
((flags&(PSYNC_FOLDER_FLAG_BACKUP_DEVICE_LIST|PSYNC_FOLDER_FLAG_BACKUP_DEVICE|PSYNC_FOLDER_FLAG_BACKUP_ROOT|PSYNC_FOLDER_FLAG_BACKUP))==0);
entry.folder.cansyncdown=((perms&PSYNC_PERM_READ)==PSYNC_PERM_READ) &&
((flags&(PSYNC_FOLDER_FLAG_BACKUP_DEVICE_LIST|PSYNC_FOLDER_FLAG_BACKUP_DEVICE|PSYNC_FOLDER_FLAG_BACKUP_ROOT|PSYNC_FOLDER_FLAG_BACKUP))==0);
entry.folder.canshare=(psync_my_userid==psync_get_number(row[3]));
entry.folder.isencrypted=(psync_get_number(row[4])&PSYNC_FOLDER_FLAG_ENCRYPTED)?1:0;
if (parentencrypted&&psync_crypto_isstarted()){
tmp=psync_get_lstring(row[2], &namelen);
entry.name=get_decname_for_folder(folderid, tmp, namelen);
if (!entry.name){
debug(D_BUG, "Can't decrypt folder name for folderid: %"P_PRI_U64", parent folfderid: %"P_PRI_U64", cryptoerr: %d, encrypted name: %s. Skippping ...", entry.folder.folderid, folderid, psync_fsfolder_crypto_error(), tmp);
continue;
}
entry.namelen=strlen(entry.name);
}
else{
entry.name=psync_get_lstring(row[2], &namelen);
entry.namelen=namelen;
}
entry.isfolder=1;
folder_list_add(list, &entry);
}
psync_sql_free_result(res);
}
if (listtype&PLIST_FILES){
res=psync_sql_query_rdlock("SELECT id, size, name FROM file WHERE parentfolderid=? ORDER BY name");
psync_sql_bind_uint(res, 1, folderid);
while ((row=psync_sql_fetch_row(res))){
entry.file.fileid=psync_get_number(row[0]);
entry.file.size=psync_get_number(row[1]);
entry.name=psync_get_lstring(row[2], &namelen);
entry.namelen=namelen;
entry.isfolder=0;
folder_list_add(list, &entry);
}
psync_sql_free_result(res);
}
return folder_list_finalize(list);
}
static void add_to_folderlist(void *ptr, psync_pstat *stat){
flist_ltype *ft=(flist_ltype *)ptr;
pentry_t entry;
int isfolder=psync_stat_isfolder(&stat->stat);
if (((ft->listtype&PLIST_FOLDERS) && isfolder) || ((ft->listtype&PLIST_FILES) && !isfolder)){
entry.name=stat->name;
entry.namelen=strlen(stat->name);
if (isfolder){
entry.isfolder=1;
entry.folder.folderid=psync_stat_inode(&stat->stat);
entry.folder.cansyncup=psync_stat_mode_ok(&stat->stat, 5);
entry.folder.cansyncdown=psync_stat_mode_ok(&stat->stat, 7);
entry.folder.isencrypted=0;
}
else{
entry.isfolder=0;
entry.file.fileid=psync_stat_inode(&stat->stat);
entry.file.size=psync_stat_size(&stat->stat);
}
folder_list_add(ft->folderlist, &entry);
}
}
pfolder_list_t *psync_list_local_folder(const char *path, psync_listtype_t listtype){
flist_ltype ft;
ft.folderlist=folder_list_init();
ft.listtype=listtype;
if (psync_list_dir(path, add_to_folderlist, &ft)){
folder_list_free(ft.folderlist);
return NULL;
}
else
return folder_list_finalize(ft.folderlist);
}
pentry_t *psync_folder_stat_path(const char *remotepath){
psync_folderid_t folderid;
psync_sql_res *res;
psync_uint_row row;
pentry_t *ret;
char *cremotepath;
size_t len, olen;
if (remotepath[0]!='/')
return NULL;
if (remotepath[1]==0){
ret=psync_new(pentry_t);
ret->name="/";
ret->namelen=1;
ret->isfolder=1;
ret->folder.folderid=0;
ret->folder.cansyncup=1;
ret->folder.cansyncdown=1;
ret->folder.canshare=0;
}
olen=len=strlen(remotepath);
while (remotepath[--len]!='/');
if (len==0)
folderid=0;
else{
cremotepath=psync_new_cnt(char, len+1);
memcpy(cremotepath, remotepath, len+1);
cremotepath[len]=0;
folderid=psync_get_folderid_by_path(cremotepath);
psync_free(cremotepath);
if (folderid==PSYNC_INVALID_FOLDERID)
return NULL;
}
len++;
olen-=len;
res=psync_sql_query_rdlock("SELECT id, permissions, userid, flags FROM folder WHERE parentfolderid=? AND name=?");
psync_sql_bind_uint(res, 1, folderid);
psync_sql_bind_lstring(res, 2, remotepath+len, olen);
if ((row=psync_sql_fetch_rowint(res))){
ret=(pentry_t *)psync_malloc(sizeof(pentry_t)+olen+1);
ret->folder.folderid=row[0];
ret->folder.cansyncup=((row[1]&PSYNC_PERM_WRITE)==PSYNC_PERM_WRITE);
ret->folder.cansyncdown=((row[1]&PSYNC_PERM_READ)==PSYNC_PERM_READ);
ret->folder.canshare=(psync_my_userid==row[2]);
ret->folder.isencrypted=(row[3]&PSYNC_FOLDER_FLAG_ENCRYPTED)?1:0;
ret->name=(char *)(ret+1);
ret->namelen=olen;
ret->isfolder=1;
memcpy(ret+1, remotepath+len, olen+1);
psync_sql_free_result(res);
return ret;
}
psync_sql_free_result(res);
res=psync_sql_query_rdlock("SELECT id, size FROM file WHERE parentfolderid=? AND name=?");
psync_sql_bind_uint(res, 1, folderid);
psync_sql_bind_lstring(res, 2, remotepath+len, olen);
if ((row=psync_sql_fetch_rowint(res))){
ret=(pentry_t *)psync_malloc(sizeof(pentry_t)+olen+1);
ret->file.fileid=row[0];
ret->file.size=row[1];
ret->name=(char *)(ret+1);
ret->namelen=olen;
ret->isfolder=0;
memcpy(ret+1, remotepath+len, olen+1);
psync_sql_free_result(res);
return ret;
}
psync_sql_free_result(res);
return NULL;
}
typedef struct {
char *localpath;
char *remotepath;
size_t locallen;
size_t remotelen;
psync_folderid_t folderid;
psync_syncid_t syncid;
psync_synctype_t synctype;
} psync_tmp_folder_t;
psync_folder_list_t *psync_list_get_list(const char *syncTypes){
psync_sql_res *res;
psync_variant_row row;
psync_tmp_folder_t *folders;
const char *cstr;
char *str;
psync_folder_list_t *ret;
size_t strlens, l;
psync_folderid_t folderid;
uint32_t alloced, lastfolder, i;
char sqlLocal[1024];
folders=NULL;
alloced=lastfolder=0;
strlens=0;
if (strlen(syncTypes) > 0) {
psync_slprintf(sqlLocal, 1024, "SELECT id, folderid, localpath, synctype FROM syncfolder WHERE folderid IS NOT NULL AND synctype IN (%s)", syncTypes);
}
else {
psync_slprintf(sqlLocal, 1024, "SELECT id, folderid, localpath, synctype FROM syncfolder WHERE folderid IS NOT NULL");
}
res = psync_sql_query_rdlock(sqlLocal);
while ((row=psync_sql_fetch_row(res))){
if (alloced==lastfolder){
alloced=(alloced+32)*2;
folders=(psync_tmp_folder_t *)psync_realloc(folders, sizeof(psync_tmp_folder_t)*alloced);
}
cstr=psync_get_lstring(row[2], &l);
l++;
str=(char *)psync_malloc(l);
memcpy(str, cstr, l);
strlens+=l;
folders[lastfolder].localpath=str;
folders[lastfolder].locallen=l;
folderid=psync_get_number(row[1]);
str=psync_get_path_by_folderid(folderid, &l);
if (unlikely(!str)){
str=psync_strdup("/Invalid/Path");
l=strlen(str);
}
l++;
strlens+=l;
folders[lastfolder].remotepath=str;
folders[lastfolder].remotelen=l;
folders[lastfolder].folderid=folderid;
folders[lastfolder].syncid=psync_get_number(row[0]);
folders[lastfolder].synctype=psync_get_number(row[3]);
lastfolder++;
}
psync_sql_free_result(res);
l=offsetof(psync_folder_list_t, folders)+sizeof(psync_folder_t)*lastfolder;
ret=(psync_folder_list_t *)psync_malloc(l+strlens);
str=((char *)ret)+l;
ret->foldercnt=lastfolder;
for (i=0; i<lastfolder; i++){
l=folders[i].locallen;
memcpy(str, folders[i].localpath, l);
psync_free(folders[i].localpath);
ret->folders[i].localpath=str;
l--;
while (l && str[l]!=PSYNC_DIRECTORY_SEPARATORC && str[l]!='/')
l--;
if ((str[l]==PSYNC_DIRECTORY_SEPARATORC || str[l]=='/') && str[l+1])
l++;
ret->folders[i].localname=str+l;
str+=folders[i].locallen;
l=folders[i].remotelen;
memcpy(str, folders[i].remotepath, l);
psync_free(folders[i].remotepath);
ret->folders[i].remotepath=str;
if (l)
l--;
while (l && str[l]!='/')
l--;
if (str[l]=='/')
l++;
ret->folders[i].remotename=str+l;
str+=folders[i].remotelen;
ret->folders[i].folderid=folders[i].folderid;
ret->folders[i].syncid=folders[i].syncid;
ret->folders[i].synctype=folders[i].synctype;
}
psync_free(folders);
return ret;
}
#ifdef P_OS_WINDOWS
void psync_fsfolder_refresh_path(char *folderpath){
LPITEMIDLIST pidl;
LPSHELLFOLDER pdesktopfolder;
OLECHAR olepath[MAX_PATH];
ULONG cheaten;
ULONG attributes;
HRESULT hr;
DWORD lasterror;
debug(D_NOTICE, "Trying to refresh the Windows Explorer windows with open path: %s", folderpath);
if (likely(SUCCEEDED(SHGetDesktopFolder(&pdesktopfolder)))){
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, folderpath, -1, olepath, MAX_PATH);
SetLastError(0);
hr=pdesktopfolder->lpVtbl->ParseDisplayName(pdesktopfolder, NULL, NULL, olepath, &cheaten, &pidl, &attributes);
lasterror=GetLastError();
if (unlikely(FAILED(hr))){
debug(D_NOTICE, "Failed to get directory ITEMIDLIST for path: %s, LastError: %d", folderpath, lasterror);
}
else{
// pidl now contains a pointer to an ITEMIDLIST.
// This ITEMIDLIST needs to be freed using the IMalloc allocator
// returned from SHGetMalloc() or by calling the CoTaskMemFree() function
SetLastError(0);
SHChangeNotify(SHCNE_UPDATEDIR, SHCNF_IDLIST|SHCNF_FLUSHNOWAIT, pidl, NULL); // Refresh all Windows Explorer windows with open folderpath
if ((lasterror=GetLastError()))
debug(D_NOTICE, "Failed to send SHCNE_UPDATEDIR event, last error: %d", lasterror);
else
debug(D_NOTICE, "Successufly sent SHCNE_UPDATEDIR event for %s", folderpath);
CoTaskMemFree(pidl);
}
pdesktopfolder->lpVtbl->Release(pdesktopfolder);
}
}
void psync_refresh_explorer_crypto_folder(){
const char* cfname="\\Crypto Folder\\";
char *cfolderpath=NULL;
cfolderpath=psync_fs_getmountpoint();
if (cfolderpath){
cfolderpath=(char*)realloc(cfolderpath, strlen(cfolderpath)+strlen(cfname)+1);
strcat(cfolderpath, cfname);
psync_fsfolder_refresh_path(cfolderpath);
psync_free(cfolderpath);
}
}
#endif
/**************************************************************************/
char* psync_get_path_from_str(char* fullPath) {
char* path;
int i = strlen(fullPath);
if (i < 2) {
return NULL;
}
while (i > 1) {
if (fullPath[i] == PSYNC_DIRECTORY_SEPARATORC) {
path = (char*)malloc((i + 2) * sizeof(char));
strncpy(path, fullPath, i + 1);
//strncpy_s(path, i + 1, fullPath, i + 1);
path[i + 1] = 0;
break;
}
i--;
}
return path;
}
char* psync_get_path_from_str_noslash(char* fullPath) {
char* path;
int i = strlen(fullPath);
if (i < 2) {
return NULL;
}
while (i > 1) {
if (fullPath[i] == PSYNC_DIRECTORY_SEPARATORC) {
path = (char*)malloc((i + 1) * sizeof(char));
strncpy(path, fullPath, i);
//strncpy_s(path, i + 1, fullPath, i + 1);
path[i] = 0;
break;
}
i--;
}
return path;
}
/**************************************************************************/