-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpfs.c
More file actions
3796 lines (3434 loc) · 113 KB
/
pfs.c
File metadata and controls
3796 lines (3434 loc) · 113 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
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-2016 Anton Titov.
* Copyright (c) 2013-2016 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.
*/
#define FUSE_USE_VERSION 26
#define _FILE_OFFSET_BITS 64
#include <pthread.h>
#include <fuse.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include "pfs.h"
#include "pfsxattr.h"
#include "pfsfolder.h"
#include "pcompat.h"
#include "plibs.h"
#include "psettings.h"
#include "pfsfolder.h"
#include "pcache.h"
#include "ppagecache.h"
#include "ptimer.h"
#include "pfstasks.h"
#include "pfsupload.h"
#include "pstatus.h"
#include "pssl.h"
#include "pfolder.h"
#include "pnetlibs.h"
#include "pcloudcrypto.h"
#include "pfscrypto.h"
#include "pfsstatic.h"
#include "pcallbacks.h"
#ifndef FUSE_STAT
#define FUSE_STAT stat
#endif
#ifndef HAS_FUSE_OFF_T
typedef off_t fuse_off_t;
#endif
#if defined(P_OS_POSIX)
#include <signal.h>
#endif
#if defined(P_OS_MACOSX)
#include <sys/mount.h>
#include <sys/mount.h>
#endif
#if defined(P_OS_LINUX)
#include <sys/mount.h>
#endif
#if IS_DEBUG
#define psync_fs_set_thread_name() do {psync_thread_name=__FUNCTION__;} while (0)
#else
#define psync_fs_set_thread_name() do {} while (0)
#endif
#define fh_to_openfile(x) ((psync_openfile_t *)((uintptr_t)x))
#define openfile_to_fh(x) ((uintptr_t)x)
#define FS_BLOCK_SIZE 4096
#define FS_MAX_WRITE 16*1024*1024
#if defined(P_OS_MACOSX)
#define FS_MAX_ACCEPTABLE_FILENAME_LEN 255
#define FUSE_HAS_SETCRTIME 1
#if defined(_DARWIN_FEATURE_64_BIT_INODE)
#define FUSE_STAT_HAS_BIRTHTIME
#endif
#endif
#if defined(P_OS_LINUX)
#define PSYNC_FS_ERR_CRYPTO_EXPIRED EROFS
#define PSYNC_FS_ERR_MOVE_ACROSS_CRYPTO EXDEV
#elif defined(P_OS_WINDOWS)
#define PSYNC_FS_ERR_CRYPTO_EXPIRED EACCES
#define PSYNC_FS_ERR_MOVE_ACROSS_CRYPTO EACCES
#else
#define PSYNC_FS_ERR_CRYPTO_EXPIRED EIO
#define PSYNC_FS_ERR_MOVE_ACROSS_CRYPTO EXDEV
#endif
static struct fuse_chan *psync_fuse_channel=NULL;
static struct fuse *psync_fuse=NULL;
static char *psync_current_mountpoint=NULL;
static psync_generic_callback_t psync_start_callback=NULL;
char *psync_fake_prefix=NULL;
size_t psync_fake_prefix_len=0;
int64_t psync_fake_fileid=INT64_MIN;
static pthread_mutex_t start_mutex=PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t start_cond=PTHREAD_COND_INITIALIZER;
static int started=0;
static int initonce=0;
static int waitingforlogin=0;
static uid_t myuid=0;
static gid_t mygid=0;
static psync_tree *openfiles=PSYNC_TREE_EMPTY;
static int psync_fs_ftruncate_of_locked(psync_openfile_t *of, fuse_off_t size);
static void delete_log_files(psync_openfile_t *of){
char fileidhex[sizeof(psync_fsfileid_t)*2+2];
const char *cachepath;
char *filename;
psync_fsfileid_t fileid;
cachepath=psync_setting_get_string(_PS(fscachepath));
fileid=-of->fileid;
psync_binhex(fileidhex, &fileid, sizeof(psync_fsfileid_t));
fileidhex[sizeof(psync_fsfileid_t)]='l';
fileidhex[sizeof(psync_fsfileid_t)+1]=0;
filename=psync_strcat(cachepath, PSYNC_DIRECTORY_SEPARATOR, fileidhex, NULL);
psync_file_delete(filename);
psync_free(filename);
fileidhex[sizeof(psync_fsfileid_t)]='f';
filename=psync_strcat(cachepath, PSYNC_DIRECTORY_SEPARATOR, fileidhex, NULL);
psync_file_delete(filename);
psync_free(filename);
}
int psync_fs_update_openfile(uint64_t taskid, uint64_t writeid, psync_fileid_t newfileid, uint64_t hash, uint64_t size, time_t ctime){
psync_sql_res *res;
psync_uint_row row;
psync_openfile_t *fl;
psync_tree *tr;
psync_fsfileid_t fileid;
int64_t d;
int ret;
fileid=-(psync_fsfileid_t)taskid;
psync_sql_lock();
tr=openfiles;
while (tr){
d=fileid-psync_tree_element(tr, psync_openfile_t, tree)->fileid;
if (d<0)
tr=tr->left;
else if (d>0)
tr=tr->right;
else{
fl=psync_tree_element(tr, psync_openfile_t, tree);
psync_fs_lock_file(fl);
if (fl->writeid==writeid){
if (fl->encrypted){
if (fl->logfile){
psync_file_close(fl->logfile);
fl->logfile=INVALID_HANDLE_VALUE;
}
delete_log_files(fl);
if (fl->authenticatedints){
psync_interval_tree_free(fl->authenticatedints);
fl->authenticatedints=NULL;
}
size=psync_fs_crypto_plain_size(size);
}
debug(D_NOTICE, "updating fileid %ld to %lu, hash %lu size %lu", (long)fileid, (unsigned long)newfileid, (unsigned long)hash, (unsigned long)size);
fl->fileid=newfileid;
fl->remotefileid=newfileid;
fl->hash=hash;
fl->modified=0;
fl->newfile=0;
fl->currentsize=size;
fl->initialsize=size;
fl->releasedforupload=0;
fl->origctime=ctime;
if (fl->datafile!=INVALID_HANDLE_VALUE){
psync_file_close(fl->datafile);
fl->datafile=INVALID_HANDLE_VALUE;
}
if (fl->indexfile!=INVALID_HANDLE_VALUE){
psync_file_close(fl->indexfile);
fl->indexfile=INVALID_HANDLE_VALUE;
}
psync_tree_del(&openfiles, &fl->tree);
tr=openfiles;
d=-1;
while (tr){
d=newfileid-psync_tree_element(tr, psync_openfile_t, tree)->fileid;
if (d<0){
if (tr->left)
tr=tr->left;
else
break;
}
else if (d>0){
if (tr->right)
tr=tr->right;
else
break;
}
else{
debug(D_BUG, "found already open file %lu, should not happen", (unsigned long)newfileid);
break;
}
}
if (d<0)
psync_tree_add_before(&openfiles, tr, &fl->tree);
else
psync_tree_add_after(&openfiles, tr, &fl->tree);
ret=0;
}
else{
debug(D_NOTICE, "writeid of fileid %ld (%s) differs %lu!=%lu", (long)fileid, fl->currentname, (unsigned long)fl->writeid, (unsigned long)writeid);
if (fl->newfile){
res=psync_sql_prep_statement("REPLACE INTO fstaskfileid (fstaskid, fileid) VALUES (?, ?)");
psync_sql_bind_uint(res, 1, taskid);
psync_sql_bind_uint(res, 2, newfileid);
psync_sql_run_free(res);
}
ret=-1;
}
pthread_mutex_unlock(&fl->mutex);
psync_sql_unlock();
return ret;
}
}
res=psync_sql_query("SELECT int1 FROM fstask WHERE id=?");
psync_sql_bind_uint(res, 1, taskid);
if ((row=psync_sql_fetch_rowint(res)) && row[0]==writeid)
ret=0;
else{
if (row)
debug(D_NOTICE, "writeid of fileid %ld differs %lu!=%lu", (long)fileid, (unsigned long)row[0], (unsigned long)writeid);
ret=-1;
}
psync_sql_free_result(res);
psync_sql_unlock();
return ret;
}
/*void psync_fs_uploading_openfile(uint64_t taskid){
psync_openfile_t *fl;
psync_tree *tr;
psync_fsfileid_t fileid;
int64_t d;
fileid=-taskid;
psync_sql_lock();
tr=openfiles;
while (tr){
d=fileid-psync_tree_element(tr, psync_openfile_t, tree)->fileid;
if (d<0)
tr=tr->left;
else if (d>0)
tr=tr->right;
else{
fl=psync_tree_element(tr, psync_openfile_t, tree);
psync_fs_lock_file(fl);
fl->uploading=1;
pthread_mutex_unlock(&fl->mutex);
break;
}
}
psync_sql_unlock();
}*/
int psync_fs_rename_openfile_locked(psync_fsfileid_t fileid, psync_fsfolderid_t folderid, const char *name){
psync_openfile_t *fl;
psync_tree *tr;
int64_t d;
tr=openfiles;
while (tr){
d=fileid-psync_tree_element(tr, psync_openfile_t, tree)->fileid;
if (d<0)
tr=tr->left;
else if (d>0)
tr=tr->right;
else{
fl=psync_tree_element(tr, psync_openfile_t, tree);
psync_fs_lock_file(fl);
if (fl->currentfolder->folderid!=folderid){
psync_fstask_release_folder_tasks_locked(fl->currentfolder);
fl->currentfolder=psync_fstask_get_or_create_folder_tasks_locked(folderid);
}
psync_free(fl->currentname);
fl->currentname=psync_strdup(name);
pthread_mutex_unlock(&fl->mutex);
return 1;
}
}
return 0;
}
void psync_fs_mark_openfile_deleted(uint64_t taskid){
psync_sql_res *res;
psync_openfile_t *fl;
psync_tree *tr;
int64_t d;
psync_fsfileid_t fileid;
fileid=-(psync_fsfileid_t)taskid;
psync_sql_lock();
tr=openfiles;
while (tr){
d=fileid-psync_tree_element(tr, psync_openfile_t, tree)->fileid;
if (d<0)
tr=tr->left;
else if (d>0)
tr=tr->right;
else{
fl=psync_tree_element(tr, psync_openfile_t, tree);
debug(D_NOTICE, "file being deleted %s is still open, marking as deleted", fl->currentname);
psync_fs_lock_file(fl);
fl->deleted=1;
pthread_mutex_unlock(&fl->mutex);
res=psync_sql_prep_statement("UPDATE fstask SET status=12 WHERE id=?");
psync_sql_bind_uint(res, 1, taskid);
psync_sql_run_free(res);
break;
}
}
psync_sql_unlock();
}
int64_t psync_fs_get_file_writeid(uint64_t taskid){
psync_openfile_t *fl;
psync_tree *tr;
psync_sql_res *res;
psync_uint_row row;
psync_fsfileid_t fileid;
int64_t d;
fileid=-(psync_fsfileid_t)taskid;
psync_sql_rdlock();
tr=openfiles;
while (tr){
d=fileid-psync_tree_element(tr, psync_openfile_t, tree)->fileid;
if (d<0)
tr=tr->left;
else if (d>0)
tr=tr->right;
else{
fl=psync_tree_element(tr, psync_openfile_t, tree);
psync_fs_lock_file(fl);
d=fl->writeid;
pthread_mutex_unlock(&fl->mutex);
psync_sql_rdunlock();
return d;
}
}
res=psync_sql_query_nolock("SELECT int1 FROM fstask WHERE id=?");
psync_sql_bind_uint(res, 1, taskid);
if ((row=psync_sql_fetch_rowint(res)))
d=row[0];
else
d=-1;
psync_sql_free_result(res);
psync_sql_rdunlock();
return d;
}
void psync_fs_update_openfile_fileid_locked(psync_openfile_t *of, psync_fsfileid_t fileid){
psync_tree *tr;
int64_t d;
assertw(of->fileid!=fileid);
psync_tree_del(&openfiles, &of->tree);
of->fileid=fileid;
tr=openfiles;
if (tr)
while (1){
d=fileid-psync_tree_element(tr, psync_openfile_t, tree)->fileid;
if (d<0){
if (tr->left)
tr=tr->left;
else{
tr->left=&of->tree;
break;
}
}
else{
assertw(d>0);
if (tr->right)
tr=tr->right;
else{
tr->right=&of->tree;
break;
}
}
}
else
openfiles=&of->tree;
psync_tree_added_at(&openfiles, tr, &of->tree);
}
#define folderid_to_inode(folderid) ((folderid)*3)
#define fileid_to_inode(fileid) ((fileid)*3+1)
#define taskid_to_inode(taskid) ((taskid)*3+2)
static void psync_row_to_folder_stat(psync_variant_row row, struct FUSE_STAT *stbuf){
psync_folderid_t folderid;
uint64_t mtime;
psync_fstask_folder_t *folder;
folderid=psync_get_number(row[0]);
mtime=psync_get_number(row[3]);
folder=psync_fstask_get_folder_tasks_rdlocked(folderid);
if (folder && folder->mtime)
mtime=folder->mtime;
memset(stbuf, 0, sizeof(struct FUSE_STAT));
stbuf->st_ino=folderid_to_inode(folderid);
#ifdef FUSE_STAT_HAS_BIRTHTIME
stbuf->st_birthtime=psync_get_number(row[2]);
#endif
stbuf->st_ctime=mtime;
stbuf->st_mtime=mtime;
stbuf->st_atime=mtime;
stbuf->st_mode=S_IFDIR | 0755;
stbuf->st_nlink=psync_get_number(row[4])+2;
stbuf->st_size=FS_BLOCK_SIZE;
#if defined(P_OS_POSIX)
stbuf->st_blocks=1;
stbuf->st_blksize=FS_BLOCK_SIZE;
#endif
stbuf->st_uid=myuid;
stbuf->st_gid=mygid;
}
static void psync_row_to_file_stat(psync_variant_row row, struct FUSE_STAT *stbuf, uint32_t flags){
uint64_t size;
size=psync_get_number(row[1]);
if (flags&PSYNC_FOLDER_FLAG_ENCRYPTED)
size=psync_fs_crypto_plain_size(size);
memset(stbuf, 0, sizeof(struct FUSE_STAT));
stbuf->st_ino=fileid_to_inode(psync_get_number(row[4]));
#ifdef FUSE_STAT_HAS_BIRTHTIME
stbuf->st_birthtime=psync_get_number(row[2]);
#endif
stbuf->st_ctime=psync_get_number(row[3]);
stbuf->st_mtime=stbuf->st_ctime;
stbuf->st_atime=stbuf->st_ctime;
stbuf->st_mode=S_IFREG | 0644;
stbuf->st_nlink=1;
stbuf->st_size=size;
#if defined(P_OS_POSIX)
stbuf->st_blocks=(size+511)/512;
stbuf->st_blksize=FS_BLOCK_SIZE;
#endif
stbuf->st_uid=myuid;
stbuf->st_gid=mygid;
}
static void psync_mkdir_to_folder_stat(psync_fstask_mkdir_t *mk, struct FUSE_STAT *stbuf){
uint64_t mtime;
psync_fstask_folder_t *folder;
folder=psync_fstask_get_folder_tasks_rdlocked(mk->folderid);
if (folder && folder->mtime)
mtime=folder->mtime;
else
mtime=mk->mtime;
memset(stbuf, 0, sizeof(struct FUSE_STAT));
if (mk->folderid>=0)
stbuf->st_ino=folderid_to_inode(mk->folderid);
else
stbuf->st_ino=taskid_to_inode(-mk->folderid);
#ifdef FUSE_STAT_HAS_BIRTHTIME
stbuf->st_birthtime=mk->ctime;
#endif
stbuf->st_ctime=mtime;
stbuf->st_mtime=mtime;
stbuf->st_atime=mtime;
stbuf->st_mode=S_IFDIR | 0755;
stbuf->st_nlink=mk->subdircnt+2;
stbuf->st_size=FS_BLOCK_SIZE;
#if defined(P_OS_POSIX)
stbuf->st_blocks=1;
stbuf->st_blksize=FS_BLOCK_SIZE;
#endif
stbuf->st_uid=myuid;
stbuf->st_gid=mygid;
}
static int psync_creat_db_to_file_stat(psync_fileid_t fileid, struct FUSE_STAT *stbuf, uint32_t flags){
psync_sql_res *res;
psync_variant_row row;
res=psync_sql_query_rdlock("SELECT name, size, ctime, mtime, id, parentfolderid FROM file WHERE id=?");
psync_sql_bind_uint(res, 1, fileid);
if ((row=psync_sql_fetch_row(res)))
psync_row_to_file_stat(row, stbuf, flags);
else
debug(D_NOTICE, "fileid %lu not found in database", (unsigned long)fileid);
psync_sql_free_result(res);
return row?0:-1;
}
static int psync_creat_stat_fake_file(struct FUSE_STAT *stbuf){
time_t ctime;
memset(stbuf, 0, sizeof(struct FUSE_STAT));
ctime=psync_timer_time();
#ifdef FUSE_STAT_HAS_BIRTHTIME
stbuf->st_birthtime=ctime;
#endif
stbuf->st_ctime=ctime;
stbuf->st_mtime=ctime;
stbuf->st_atime=ctime;
stbuf->st_mode=S_IFREG | 0644;
stbuf->st_nlink=1;
stbuf->st_size=0;
#if defined(P_OS_POSIX)
stbuf->st_blocks=0;
stbuf->st_blksize=FS_BLOCK_SIZE;
#endif
stbuf->st_uid=myuid;
stbuf->st_gid=mygid;
return 0;
}
static int fill_stat_from_open_file(psync_fsfileid_t fileid, struct FUSE_STAT *stbuf){
psync_openfile_t *fl;
psync_tree *tr;
psync_stat_t st;
int64_t d;
psync_sql_rdlock();
tr=openfiles;
while (tr){
d=fileid-psync_tree_element(tr, psync_openfile_t, tree)->fileid;
if (d<0)
tr=tr->left;
else if (d>0)
tr=tr->right;
else{
fl=psync_tree_element(tr, psync_openfile_t, tree);
psync_fs_lock_file(fl);
stbuf->st_size=fl->currentsize;
debug(D_NOTICE, "found open file with size %lu", (unsigned long)fl->currentsize);
if (!psync_fstat(fl->logfile, &st))
stbuf->st_mtime=psync_stat_mtime(&st);
pthread_mutex_unlock(&fl->mutex);
psync_sql_rdunlock();
return 1;
}
}
psync_sql_rdunlock();
return 0;
}
static int psync_creat_local_to_file_stat(psync_fstask_creat_t *cr, struct FUSE_STAT *stbuf, uint32_t folderflags){
psync_stat_t st;
psync_fsfileid_t fileid;
uint64_t size;
const char *cachepath;
char *filename;
psync_openfile_t *fl;
psync_tree *tr;
int64_t d;
// psync_file_t fd;
char fileidhex[sizeof(psync_fsfileid_t)*2+2];
int stret;
if (unlikely(psync_fs_need_per_folder_refresh_const() && cr->fileid<psync_fake_fileid))
return psync_creat_stat_fake_file(stbuf);
fl=NULL;
fileid=-cr->fileid;
psync_sql_rdlock();
tr=openfiles;
while (tr){
d=cr->fileid-psync_tree_element(tr, psync_openfile_t, tree)->fileid;
if (d<0)
tr=tr->left;
else if (d>0)
tr=tr->right;
else{
fl=psync_tree_element(tr, psync_openfile_t, tree);
psync_fs_lock_file(fl);
break;
}
}
psync_sql_rdunlock();
if (fl && fl->datafile!=INVALID_HANDLE_VALUE){
stret=psync_fstat(fl->datafile, &st);
pthread_mutex_unlock(&fl->mutex);
if (stret)
debug(D_NOTICE, "could not stat open file %ld", (long)cr->fileid);
else
debug(D_NOTICE, "got stat from open file %ld", (long)cr->fileid);
}
else{
if (fl)
pthread_mutex_unlock(&fl->mutex);
psync_binhex(fileidhex, &fileid, sizeof(psync_fsfileid_t));
fileidhex[sizeof(psync_fsfileid_t)]='d';
fileidhex[sizeof(psync_fsfileid_t)+1]=0;
cachepath=psync_setting_get_string(_PS(fscachepath));
filename=psync_strcat(cachepath, PSYNC_DIRECTORY_SEPARATOR, fileidhex, NULL);
stret=psync_stat(filename, &st);
if (stret)
debug(D_NOTICE, "could not stat file %s", filename);
psync_free(filename);
}
if (stret)
return -1;
/* if (cr->newfile)
osize=0;
else{
fileidhex[sizeof(psync_fsfileid_t)]='i';
filename=psync_strcat(cachepath, PSYNC_DIRECTORY_SEPARATOR, fileidhex, NULL);
fd=psync_file_open(filename, P_O_RDONLY, 0);
psync_free(filename);
if (fd==INVALID_HANDLE_VALUE)
return -EIO;
stret=psync_file_pread(fd, &osize, sizeof(osize), offsetof(index_header, copyfromoriginal));
psync_file_close(fd);
if (stret!=sizeof(osize))
return -EIO;
}*/
memset(stbuf, 0, sizeof(struct FUSE_STAT));
stbuf->st_ino=taskid_to_inode(fileid);
#ifdef FUSE_STAT_HAS_BIRTHTIME
stbuf->st_birthtime=psync_stat_birthtime(&st);
#endif
stbuf->st_mtime=psync_stat_mtime(&st);
stbuf->st_ctime=stbuf->st_mtime;
stbuf->st_atime=stbuf->st_mtime;
stbuf->st_mode=S_IFREG | 0644;
stbuf->st_nlink=1;
if (folderflags&PSYNC_FOLDER_FLAG_ENCRYPTED){
if (fill_stat_from_open_file(cr->fileid, stbuf))
size=stbuf->st_size;
else{
size=psync_fs_crypto_plain_size(psync_stat_size(&st));
stbuf->st_size=size;
}
}
else{
size=psync_stat_size(&st);
stbuf->st_size=size;
}
#if defined(P_OS_POSIX)
stbuf->st_blocks=(size+511)/512;
stbuf->st_blksize=FS_BLOCK_SIZE;
#endif
stbuf->st_uid=myuid;
stbuf->st_gid=mygid;
return 0;
}
static int psync_creat_static_to_file_stat(psync_fstask_creat_t *cr, struct FUSE_STAT *stbuf, uint32_t folderflags){
psync_fstask_local_creat_t *lc;
lc=psync_fstask_creat_get_local(cr);
memset(stbuf, 0, sizeof(struct FUSE_STAT));
stbuf->st_ino=cr->taskid;
#ifdef FUSE_STAT_HAS_BIRTHTIME
stbuf->st_birthtime=lc->ctime;
#endif
stbuf->st_ctime=lc->ctime;
stbuf->st_mtime=lc->ctime;
stbuf->st_atime=lc->ctime;
stbuf->st_mode=S_IFREG | 0644;
stbuf->st_nlink=1;
stbuf->st_size=lc->datalen;
#if defined(P_OS_POSIX)
stbuf->st_blocks=(lc->datalen+511)/512;
stbuf->st_blksize=FS_BLOCK_SIZE;
#endif
stbuf->st_uid=myuid;
stbuf->st_gid=mygid;
return 0;
}
static int psync_creat_to_file_stat(psync_fstask_creat_t *cr, struct FUSE_STAT *stbuf, uint32_t folderflags){
debug(D_NOTICE, "getting stat from creat for file %s fileid %ld taskid %lu", cr->name, (long)cr->fileid, (unsigned long)cr->taskid);
if (cr->fileid>0)
return psync_creat_db_to_file_stat(cr->fileid, stbuf, folderflags);
else if (cr->fileid<0)
return psync_creat_local_to_file_stat(cr, stbuf, folderflags);
else
return psync_creat_static_to_file_stat(cr, stbuf, folderflags);
}
int psync_fs_crypto_err_to_errno(int cryptoerr){
switch (cryptoerr){
case PSYNC_CRYPTO_NOT_STARTED: return EACCES;
case PSYNC_CRYPTO_RSA_ERROR: return EIO;
case PSYNC_CRYPTO_FOLDER_NOT_FOUND: return ENOENT;
case PSYNC_CRYPTO_FILE_NOT_FOUND: return ENOENT;
case PSYNC_CRYPTO_INVALID_KEY: return EIO;
case PSYNC_CRYPTO_CANT_CONNECT: return ENOTCONN;
case PSYNC_CRYPTO_FOLDER_NOT_ENCRYPTED: return EINVAL;
case PSYNC_CRYPTO_INTERNAL_ERROR: return EINVAL;
default: return EINVAL;
}
}
static int psync_fs_getrootattr(struct FUSE_STAT *stbuf){
psync_sql_res *res;
psync_variant_row row;
res=psync_sql_query_rdlock("SELECT 0, 0, IFNULL(s.value, 1414766136)*1, f.mtime, f.subdircnt FROM folder f LEFT JOIN setting s ON s.id='registered' WHERE f.id=0");
if ((row=psync_sql_fetch_row(res)))
psync_row_to_folder_stat(row, stbuf);
psync_sql_free_result(res);
return 0;
}
#define CHECK_LOGIN_LOCKED() do {\
if (unlikely(waitingforlogin)){\
psync_sql_unlock();\
debug(D_NOTICE, "returning EACCES for not logged in");\
return -EACCES;\
}\
} while (0)
#define CHECK_LOGIN_RDLOCKED() do {\
if (unlikely(waitingforlogin)){\
psync_sql_rdunlock();\
debug(D_NOTICE, "returning EACCES for not logged in");\
return -EACCES;\
}\
} while (0)
static int psync_fs_getattr(const char *path, struct FUSE_STAT *stbuf){
psync_sql_res *res;
psync_variant_row row;
psync_fspath_t *fpath;
psync_fstask_folder_t *folder;
psync_fstask_creat_t *cr;
int crr;
psync_fs_set_thread_name();
if (path[1]==0 && path[0]=='/')
return psync_fs_getrootattr(stbuf);
psync_sql_rdlock();
CHECK_LOGIN_RDLOCKED();
fpath=psync_fsfolder_resolve_path(path);
if (!fpath){
psync_sql_rdunlock();
crr=psync_fsfolder_crypto_error();
if (crr){
crr=-psync_fs_crypto_err_to_errno(crr);
debug(D_NOTICE, "got crypto error for %s, returning %d", path, crr);
return crr;
}
else{
debug(D_NOTICE, "could not find path component of %s, returning ENOENT", path);
return -ENOENT;
}
}
folder=psync_fstask_get_folder_tasks_rdlocked(fpath->folderid);
if (folder){
psync_fstask_mkdir_t *mk;
mk=psync_fstask_find_mkdir(folder, fpath->name, 0);
if (mk){
if (mk->flags&PSYNC_FOLDER_FLAG_INVISIBLE){
psync_sql_rdunlock();
psync_free(fpath);
return -ENOENT;
}
psync_mkdir_to_folder_stat(mk, stbuf);
psync_sql_rdunlock();
psync_free(fpath);
return 0;
}
}
if (!folder || !psync_fstask_find_rmdir(folder, fpath->name, 0)){
res=psync_sql_query_nolock("SELECT id, permissions, ctime, mtime, subdircnt FROM folder WHERE parentfolderid=? AND name=?");
psync_sql_bind_uint(res, 1, fpath->folderid);
psync_sql_bind_string(res, 2, fpath->name);
if ((row=psync_sql_fetch_row(res)))
psync_row_to_folder_stat(row, stbuf);
psync_sql_free_result(res);
if (row){
psync_sql_rdunlock();
psync_free(fpath);
return 0;
}
}
res=psync_sql_query_nolock("SELECT name, size, ctime, mtime, id FROM file WHERE parentfolderid=? AND name=?");
psync_sql_bind_uint(res, 1, fpath->folderid);
psync_sql_bind_string(res, 2, fpath->name);
if ((row=psync_sql_fetch_row(res)))
psync_row_to_file_stat(row, stbuf, fpath->flags);
psync_sql_free_result(res);
if (folder){
if (psync_fstask_find_unlink(folder, fpath->name, 0))
row=NULL;
if (!row && (cr=psync_fstask_find_creat(folder, fpath->name, 0)))
crr=psync_creat_to_file_stat(cr, stbuf, fpath->flags);
else
crr=-1;
}
else
crr=-1;
psync_sql_rdunlock();
psync_free(fpath);
if (row || !crr)
return 0;
debug(D_NOTICE, "returning ENOENT for %s", path);
return -ENOENT;
}
static int filler_decoded(psync_crypto_aes256_text_decoder_t dec, fuse_fill_dir_t filler, void *buf, const char *name, struct FUSE_STAT *st, fuse_off_t off){
if (dec){
char *namedec;
int ret;
namedec=psync_cloud_crypto_decode_filename(dec, name);
if (!namedec)
return 0;
ret=filler(buf, namedec, st, off);
psync_free(namedec);
return ret;
}
else
return filler(buf, name, st, off);
}
static int psync_fs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, fuse_off_t offset, struct fuse_file_info *fi){
psync_sql_res *res;
psync_variant_row row;
psync_fsfolderid_t folderid;
psync_fstask_folder_t *folder;
psync_tree *trel;
const char *name;
psync_crypto_aes256_text_decoder_t dec;
uint32_t flags;
size_t namelen;
struct FUSE_STAT st;
psync_fs_set_thread_name();
debug(D_NOTICE, "readdir [%s]", path);
psync_sql_rdlock();
CHECK_LOGIN_RDLOCKED();
folderid=psync_fsfolderid_by_path(path, &flags);
if (unlikely_log(folderid==PSYNC_INVALID_FSFOLDERID)){
psync_sql_rdunlock();
if (psync_fsfolder_crypto_error())
return PRINT_RETURN(-psync_fs_crypto_err_to_errno(psync_fsfolder_crypto_error()));
else
return -PRINT_RETURN_CONST(ENOENT);
}
if (flags&PSYNC_FOLDER_FLAG_ENCRYPTED){
dec=psync_cloud_crypto_get_folder_decoder(folderid);
if (psync_crypto_is_error(dec)){
psync_sql_rdunlock();
return PRINT_RETURN(-psync_fs_crypto_err_to_errno(psync_crypto_to_error(dec)));
}
}
else
dec=NULL;
filler(buf, ".", NULL, 0);
if (folderid!=0)
filler(buf, "..", NULL, 0);
folder=psync_fstask_get_folder_tasks_rdlocked(folderid);
if (folderid>=0){
res=psync_sql_query_nolock("SELECT id, permissions, ctime, mtime, subdircnt, name FROM folder WHERE parentfolderid=?");
psync_sql_bind_uint(res, 1, folderid);
while ((row=psync_sql_fetch_row(res))){
name=psync_get_lstring(row[5], &namelen);
#if defined(FS_MAX_ACCEPTABLE_FILENAME_LEN)
if (unlikely_log(namelen>FS_MAX_ACCEPTABLE_FILENAME_LEN))
continue;
#endif
if (!name || !name[0])
continue;
if (folder && (psync_fstask_find_rmdir(folder, name, 0) || psync_fstask_find_mkdir(folder, name, 0)))
continue;
psync_row_to_folder_stat(row, &st);
filler_decoded(dec, filler, buf, name, &st, 0);
}
psync_sql_free_result(res);
res=psync_sql_query_nolock("SELECT name, size, ctime, mtime, id FROM file WHERE parentfolderid=?");
psync_sql_bind_uint(res, 1, folderid);
while ((row=psync_sql_fetch_row(res))){
name=psync_get_lstring(row[0], &namelen);
#if defined(FS_MAX_ACCEPTABLE_FILENAME_LEN)
if (unlikely_log(namelen>FS_MAX_ACCEPTABLE_FILENAME_LEN))
continue;
#endif
if (!name || !name[0])
continue;
if (folder && psync_fstask_find_unlink(folder, name, 0))
continue;
psync_row_to_file_stat(row, &st, flags);
filler_decoded(dec, filler, buf, name, &st, 0);
}
psync_sql_free_result(res);
}
if (folder){
psync_tree_for_each(trel, folder->mkdirs){
#if defined(FS_MAX_ACCEPTABLE_FILENAME_LEN)
if (unlikely_log(strlen(psync_tree_element(trel, psync_fstask_mkdir_t, tree)->name)>FS_MAX_ACCEPTABLE_FILENAME_LEN))
continue;
#endif
if (psync_tree_element(trel, psync_fstask_mkdir_t, tree)->flags&PSYNC_FOLDER_FLAG_INVISIBLE)
continue;
psync_mkdir_to_folder_stat(psync_tree_element(trel, psync_fstask_mkdir_t, tree), &st);
filler_decoded(dec, filler, buf, psync_tree_element(trel, psync_fstask_mkdir_t, tree)->name, &st, 0);
}
psync_tree_for_each(trel, folder->creats){
#if defined(FS_MAX_ACCEPTABLE_FILENAME_LEN)
if (unlikely_log(strlen(psync_tree_element(trel, psync_fstask_creat_t, tree)->name)>FS_MAX_ACCEPTABLE_FILENAME_LEN))
continue;
#endif
if (!psync_creat_to_file_stat(psync_tree_element(trel, psync_fstask_creat_t, tree), &st, flags))
filler_decoded(dec, filler, buf, psync_tree_element(trel, psync_fstask_creat_t, tree)->name, &st, 0);
}
}
psync_sql_rdunlock();
if (dec)
psync_cloud_crypto_release_folder_decoder(folderid, dec);
return PRINT_RETURN(0);
}
static psync_openfile_t *psync_fs_create_file(psync_fsfileid_t fileid, psync_fsfileid_t remotefileid, uint64_t size, uint64_t hash, int lock,
uint32_t writeid, psync_fstask_folder_t *folder, const char *name,
psync_crypto_aes256_sector_encoder_decoder_t encoder){
psync_openfile_t *fl;
psync_tree *tr;
int64_t d;
psync_sql_lock();
tr=openfiles;
d=-1;
while (tr){
d=fileid-psync_tree_element(tr, psync_openfile_t, tree)->fileid;
if (d<0){
if (tr->left)
tr=tr->left;
else
break;
}
else if (d>0){
if (tr->right)
tr=tr->right;
else
break;
}
else{
fl=psync_tree_element(tr, psync_openfile_t, tree);
if (lock){
psync_fs_lock_file(fl);
psync_fs_inc_of_refcnt_locked(fl);
}
else
psync_fs_inc_of_refcnt(fl);
assertw(fl->currentfolder==folder);
assertw(!strcmp(fl->currentname, name));
psync_fstask_release_folder_tasks_locked(folder);
psync_sql_unlock();
if (encoder!=PSYNC_CRYPTO_INVALID_ENCODER && encoder!=PSYNC_CRYPTO_UNLOADED_SECTOR_ENCODER)