-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpfscrypto.c
1837 lines (1766 loc) · 66.4 KB
/
pfscrypto.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) 2014 Anton Titov.
* Copyright (c) 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 "plibs.h"
#include "pfscrypto.h"
#include "pcloudcrypto.h"
#include "ppagecache.h"
#include <ctype.h>
// this is only for debug, adds needless checks of tree for local files
#if IS_DEBUG
#define PSYNC_DO_LOCAL_FULL_TREE_CHECK 1
#endif
#define PSYNC_CRYPTO_LOG_DATA 1
#define PSYNC_CRYPTO_LOG_INT 2
#define PSYNC_CRYPTO_HASH_TREE_SHIFT 8
#define PSYNC_LOG_STATUS_INCOMPLETE 0
#define PSYNC_LOG_STATUS_FINALIZED 1
#define PSYNC_LOG_HASHID_FH256 0
typedef struct {
uint8_t type;
union {
uint8_t u8;
uint8_t finalized;
};
union {
uint16_t u16;
uint16_t length;
uint16_t longlengthhi;
};
union {
uint32_t u32;
uint32_t longlengthlo;
};
union {
uint64_t u64;
uint64_t offset;
uint64_t filesize;
};
} psync_crypto_log_header;
typedef struct {
uint32_t status;
uint32_t hashid;
uint64_t logsize;
uint64_t filesize;
unsigned char hash[PSYNC_FAST_HASH256_LEN];
uint32_t crc;
} psync_crypto_master_record;
typedef struct {
psync_crypto_log_header header;
unsigned char data[PSYNC_CRYPTO_SECTOR_SIZE];
} psync_crypto_log_data_record;
static const uint64_t max_level_size[PSYNC_CRYPTO_MAX_HASH_TREE_LEVEL+1]={
0x1000,
0x81000,
0x4081000,
0x204081000,
0x10204081000,
0x810204081000,
0x40810204081000
};
psync_crypto_sectorid_t psync_fs_crypto_data_sectorid_by_sectorid(psync_crypto_sectorid_t sectorid){
psync_crypto_sectorid_t sect;
sect=sectorid;
while (sectorid>=PSYNC_CRYPTO_HASH_TREE_SECTORS){
sectorid/=PSYNC_CRYPTO_HASH_TREE_SECTORS;
sect+=sectorid;
}
return sect;
}
static uint64_t psync_fs_crypto_data_offset_by_sectorid(psync_crypto_sectorid_t sectorid){
uint64_t off;
off=(uint64_t)sectorid*PSYNC_CRYPTO_SECTOR_SIZE;
while (sectorid>=PSYNC_CRYPTO_HASH_TREE_SECTORS){
sectorid/=PSYNC_CRYPTO_HASH_TREE_SECTORS;
off+=(uint64_t)sectorid*PSYNC_CRYPTO_SECTOR_SIZE;
}
return off;
}
/* works for all sectors of auth data except the last one of each level */
static uint64_t psync_fs_crypto_auth_offset(uint32_t level, uint32_t id){
uint64_t ret;
ret=max_level_size[level+1]*(id+1)-PSYNC_CRYPTO_SECTOR_SIZE;
while (id>=PSYNC_CRYPTO_HASH_TREE_SECTORS){
id/=PSYNC_CRYPTO_HASH_TREE_SECTORS;
ret+=id*PSYNC_CRYPTO_SECTOR_SIZE;
}
return ret;
}
void psync_fs_crypto_offsets_by_plainsize(uint64_t size, psync_crypto_offsets_t *offsets){
uint64_t off;
psync_crypto_sectorid_t lastsectorid;
uint32_t lastsectorsize, sz, level;
memset(offsets, 0, sizeof(psync_crypto_offsets_t));
offsets->plainsize=size;
offsets->needmasterauth=size>PSYNC_CRYPTO_SECTOR_SIZE;
if (!size)
return;
lastsectorid=size/PSYNC_CRYPTO_SECTOR_SIZE;
lastsectorsize=size%PSYNC_CRYPTO_SECTOR_SIZE;
if (lastsectorsize==0){
lastsectorid--;
lastsectorsize=PSYNC_CRYPTO_SECTOR_SIZE;
}
off=psync_fs_crypto_data_offset_by_sectorid(lastsectorid)+lastsectorsize;
level=0;
size=(size+PSYNC_CRYPTO_SECTOR_SIZE-1)/PSYNC_CRYPTO_SECTOR_SIZE;
do {
sz=size%PSYNC_CRYPTO_HASH_TREE_SECTORS;
size=(size+PSYNC_CRYPTO_HASH_TREE_SECTORS-1)/PSYNC_CRYPTO_HASH_TREE_SECTORS;
if (!sz)
sz=PSYNC_CRYPTO_HASH_TREE_SECTORS;
sz*=PSYNC_CRYPTO_AUTH_SIZE;
offsets->lastauthsectoroff[level]=off;
offsets->lastauthsectorlen[level]=sz;
off+=sz;
level++;
} while (size>1);
offsets->lastauthsectoroff[level]=off;
offsets->lastauthsectorlen[level]=PSYNC_CRYPTO_AUTH_SIZE;
offsets->masterauthoff=off;
offsets->treelevels=level;
}
static void psync_fs_crypto_offsets_by_cryptosize(uint64_t size, psync_crypto_offsets_t *offsets){
uint64_t off, cnt;
uint32_t level;
memset(offsets, 0, sizeof(psync_crypto_offsets_t));
if (size<=PSYNC_CRYPTO_AUTH_SIZE)
return;
offsets->needmasterauth=size>PSYNC_CRYPTO_SECTOR_SIZE+PSYNC_CRYPTO_AUTH_SIZE;
if (offsets->needmasterauth)
size-=PSYNC_CRYPTO_AUTH_SIZE;
offsets->masterauthoff=size;
for (level=1; level<=PSYNC_CRYPTO_MAX_HASH_TREE_LEVEL; level++)
if (size<=max_level_size[level])
break;
assert(level<=PSYNC_CRYPTO_MAX_HASH_TREE_LEVEL);
offsets->treelevels=level;
off=size;
offsets->lastauthsectoroff[level]=off;
offsets->lastauthsectorlen[level]=PSYNC_CRYPTO_AUTH_SIZE;
do {
level--;
cnt=((size+max_level_size[level]+PSYNC_CRYPTO_AUTH_SIZE-1)/(max_level_size[level]+PSYNC_CRYPTO_AUTH_SIZE));
size-=cnt*PSYNC_CRYPTO_AUTH_SIZE;
cnt%=PSYNC_CRYPTO_HASH_TREE_SECTORS;
if (!cnt)
cnt=PSYNC_CRYPTO_HASH_TREE_SECTORS;
off-=cnt*PSYNC_CRYPTO_AUTH_SIZE;
offsets->lastauthsectorlen[level]=cnt*PSYNC_CRYPTO_AUTH_SIZE;
offsets->lastauthsectoroff[level]=off;
} while (level);
offsets->plainsize=size;
}
int psync_fs_crypto_init_log(psync_openfile_t *of){
char buff[PSYNC_CRYPTO_SECTOR_SIZE];
ssize_t wrt;
assert(sizeof(psync_crypto_master_record)<=PSYNC_CRYPTO_SECTOR_SIZE);
memset(buff, 0xff, sizeof(buff));
wrt=psync_file_pwrite(of->logfile, buff, PSYNC_CRYPTO_SECTOR_SIZE, 0);
if (unlikely(wrt!=PSYNC_CRYPTO_SECTOR_SIZE)){
debug(D_ERROR, "write to log of %u bytes returned %d", (unsigned)PSYNC_CRYPTO_SECTOR_SIZE, (int)wrt);
return -EIO;
}
of->logoffset=PSYNC_CRYPTO_SECTOR_SIZE;
psync_fast_hash256_init(&of->loghashctx);
return 0;
}
static int psync_fs_crypto_read_newfile_full_sector_from_log(psync_openfile_t *of, char *buf, psync_sector_inlog_t *se){
unsigned char buff[PSYNC_CRYPTO_SECTOR_SIZE];
psync_crypto_log_header hdr;
ssize_t rd;
rd=psync_file_pread(of->logfile, &hdr, sizeof(hdr), se->logoffset);
if (unlikely(rd!=sizeof(hdr))){
debug(D_ERROR, "read from log of %u bytes returned %d", (unsigned)sizeof(hdr), (int)rd);
return -EIO;
}
if (hdr.type!=PSYNC_CRYPTO_LOG_DATA){
debug(D_ERROR, "bad log record type %u", (unsigned)hdr.type);
return -EIO;
}
assert(hdr.offset==(uint64_t)psync_fs_crypto_data_sectorid_by_sectorid(se->sectorid)*PSYNC_CRYPTO_SECTOR_SIZE);
assert(hdr.length<=PSYNC_CRYPTO_SECTOR_SIZE);
rd=psync_file_pread(of->logfile, buff, hdr.length, se->logoffset+sizeof(hdr));
if (unlikely(rd!=hdr.length)){
debug(D_ERROR, "read from log of %u bytes returned %d", (unsigned)hdr.length, (int)rd);
return -EIO;
}
if (unlikely_log(psync_crypto_aes256_decode_sector(of->encoder, buff, rd, (unsigned char *)buf, se->auth, se->sectorid)))
return -EIO;
else
return rd;
}
/*static psync_crypto_sectorid_t size_div_hash_tree_sectors(psync_crypto_sectorid_t sectorid){
psync_crypto_sectorid_t ret;
ret=sectorid/PSYNC_CRYPTO_HASH_TREE_SECTORS;
if (sectorid%PSYNC_CRYPTO_HASH_TREE_SECTORS==0 && ret)
ret--;
return ret;
}*/
static psync_crypto_sectorid_t get_last_sectorid_by_size(uint64_t size){
if (unlikely(size==0)){
debug(D_NOTICE, "called for 0 size");
return 0;
}
else
return (size-1)/PSYNC_CRYPTO_SECTOR_SIZE;
}
#if defined(PSYNC_DO_LOCAL_FULL_TREE_CHECK)
static int psync_fs_crypto_do_local_tree_check(psync_openfile_t *of, psync_crypto_sectorid_t sectorid, psync_crypto_offsets_t *offsets){
unsigned char buff[PSYNC_CRYPTO_SECTOR_SIZE];
psync_crypto_sector_auth_t auth;
uint64_t off;
ssize_t rd;
psync_crypto_sectorid_t sizesect;
uint32_t ssize, authoff, level;
if (!offsets->needmasterauth)
return 0;
sectorid/=PSYNC_CRYPTO_HASH_TREE_SECTORS;
sizesect=get_last_sectorid_by_size(offsets->plainsize)/PSYNC_CRYPTO_HASH_TREE_SECTORS;
if (sectorid==sizesect){
off=offsets->lastauthsectoroff[0];
ssize=offsets->lastauthsectorlen[0];
}
else{
off=psync_fs_crypto_auth_offset(0, sectorid);
ssize=PSYNC_CRYPTO_SECTOR_SIZE;
}
rd=psync_file_pread(of->datafile, buff, ssize, off);
if (unlikely_log(rd!=ssize))
return -1;
psync_crypto_sign_auth_sector(of->encoder, buff, ssize, auth);
level=0;
while (level<offsets->treelevels){
level++;
authoff=(sectorid%PSYNC_CRYPTO_HASH_TREE_SECTORS)*PSYNC_CRYPTO_AUTH_SIZE;
sectorid/=PSYNC_CRYPTO_HASH_TREE_SECTORS;
sizesect/=PSYNC_CRYPTO_HASH_TREE_SECTORS;
if (sectorid==sizesect){
off=offsets->lastauthsectoroff[level];
ssize=offsets->lastauthsectorlen[level];
}
else{
off=psync_fs_crypto_auth_offset(level, sectorid);
ssize=PSYNC_CRYPTO_SECTOR_SIZE;
}
rd=psync_file_pread(of->datafile, buff, ssize, off);
if (unlikely_log(rd!=ssize))
return -1;
if (unlikely(memcmp(buff+authoff, auth, sizeof(auth)))){
debug(D_WARNING, "verify failed on level %u, sectorid %u, off %lu, ssize %u", (unsigned)level, (unsigned)sectorid, (unsigned long)off, (unsigned)ssize);
return -1;
}
psync_crypto_sign_auth_sector(of->encoder, buff, ssize, auth);
}
return 0;
}
#endif
static int psync_fs_crypto_wait_no_extender_locked(psync_openfile_t *of){
int ret;
while (of->extender && !of->extender->ready){
debug(D_NOTICE, "waiting for extender to finish");
of->extender->waiters++;
pthread_cond_wait(&of->extender->cond, &of->mutex);
of->extender->waiters--;
}
if (of->extender){
ret=of->extender->error;
do {
pthread_mutex_unlock(&of->mutex);
pthread_mutex_lock(&of->mutex);
} while (of->extender);
debug(D_NOTICE, "waited for extender to finish");
return ret;
}
else
return 0;
}
static int psync_fs_crypto_wait_extender_after_locked(psync_openfile_t *of, uint64_t offset){
while (of->extender && !of->extender->ready && of->extender->extendedto<offset){
debug(D_NOTICE, "waiting for extending process to reach %lu, currently at %lu", (unsigned long)offset, (unsigned long)of->extender->extendedto);
of->extender->waiters++;
pthread_cond_wait(&of->extender->cond, &of->mutex);
of->extender->waiters--;
debug(D_NOTICE, "waited extending process to reach %lu", (unsigned long)of->extender->extendedto);
}
if (of->extender)
return of->extender->error;
else
return 0;
}
static int psync_fs_crypto_kill_extender_locked(psync_openfile_t *of){
if (of->extender){
debug(D_NOTICE, "killing extender of file %s", of->currentname);
of->extender->kill=1;
if (of->extender->error)
return of->extender->error;
}
return 0;
}
static int psync_fs_crypto_read_newfile_full_sector_from_datafile(psync_openfile_t *of, char *buf, psync_crypto_sectorid_t sectorid){
unsigned char buff[PSYNC_CRYPTO_SECTOR_SIZE];
psync_crypto_sector_auth_t auth;
psync_crypto_offsets_t offsets;
uint64_t off;
int64_t fs;
ssize_t rd;
uint32_t ssize;
// debug(D_NOTICE, "reading sector %u", sectorid);
fs=psync_file_size(of->datafile);
if (unlikely_log(fs==-1))
return -EIO;
psync_fs_crypto_offsets_by_cryptosize(fs, &offsets);
if ((uint64_t)sectorid*PSYNC_CRYPTO_SECTOR_SIZE>=offsets.plainsize)
return 0;
if ((uint64_t)sectorid*PSYNC_CRYPTO_SECTOR_SIZE+PSYNC_CRYPTO_SECTOR_SIZE>offsets.plainsize)
ssize=offsets.plainsize-(uint64_t)sectorid*PSYNC_CRYPTO_SECTOR_SIZE;
else
ssize=PSYNC_CRYPTO_SECTOR_SIZE;
off=psync_fs_crypto_data_offset_by_sectorid(sectorid);
// debug(D_NOTICE, "data offset=%lu", off);
rd=psync_file_pread(of->datafile, buff, ssize, off);
if (unlikely_log(rd!=ssize))
return -EIO;
if (sectorid/PSYNC_CRYPTO_HASH_TREE_SECTORS==get_last_sectorid_by_size(offsets.plainsize)/PSYNC_CRYPTO_HASH_TREE_SECTORS)
off=offsets.lastauthsectoroff[0];
else
off=psync_fs_crypto_auth_offset(0, sectorid/PSYNC_CRYPTO_HASH_TREE_SECTORS);
off+=(sectorid%PSYNC_CRYPTO_HASH_TREE_SECTORS)*PSYNC_CRYPTO_AUTH_SIZE;
// debug(D_NOTICE, "auth offset=%lu", off);
rd=psync_file_pread(of->datafile, auth, sizeof(auth), off);
if (unlikely_log(rd!=sizeof(auth)))
return -EIO;
if (unlikely_log(psync_crypto_aes256_decode_sector(of->encoder, buff, ssize, (unsigned char *)buf, auth, sectorid)))
return -EIO;
#if defined(PSYNC_DO_LOCAL_FULL_TREE_CHECK)
else if (psync_fs_crypto_do_local_tree_check(of, sectorid, &offsets))
return -EIO;
#endif
else
return ssize;
}
static int psync_fs_crypto_read_newfile_full_sector(psync_openfile_t *of, char *buf, psync_crypto_sectorid_t sectorid){
psync_crypto_sectorid_diff_t d;
psync_sector_inlog_t *tr;
tr=psync_tree_element(of->sectorsinlog, psync_sector_inlog_t, tree);
while (tr){
d=sectorid-tr->sectorid;
if (d<0)
tr=psync_tree_element(tr->tree.left, psync_sector_inlog_t, tree);
else if (d>0)
tr=psync_tree_element(tr->tree.right, psync_sector_inlog_t, tree);
else
return psync_fs_crypto_read_newfile_full_sector_from_log(of, buf, tr);
}
return psync_fs_crypto_read_newfile_full_sector_from_datafile(of, buf, sectorid);
}
static int psync_fs_crypto_read_newfile_partial_sector(psync_openfile_t *of, char *buf, psync_crypto_sectorid_t sectorid, size_t size, off_t offset){
char buff[PSYNC_CRYPTO_SECTOR_SIZE];
int rd;
assert(offset+size<=PSYNC_CRYPTO_SECTOR_SIZE);
rd=psync_fs_crypto_read_newfile_full_sector(of, buff, sectorid);
if (rd<=0)
return rd;
if (rd<=offset)
return 0;
rd-=offset;
if (rd>size)
rd=size;
memcpy(buf, buff+offset, rd);
return rd;
}
static int psync_fs_unlock_ret(psync_openfile_t *of, int ret){
pthread_mutex_unlock(&of->mutex);
return ret;
}
int psync_fs_crypto_read_newfile_locked(psync_openfile_t *of, char *buf, uint64_t size, uint64_t offset){
uint64_t off2, offdiff;
psync_crypto_sectorid_t sectorid;
int ret, rd;
assert(of->encrypted);
assert(of->encoder);
if (unlikely((size+offset+PSYNC_CRYPTO_SECTOR_SIZE-1)/PSYNC_CRYPTO_SECTOR_SIZE>PSYNC_CRYPTO_MAX_SECTORID))
return psync_fs_unlock_ret(of, -EINVAL);
ret=psync_fs_crypto_wait_extender_after_locked(of, offset+size);
if (unlikely_log(ret))
return psync_fs_unlock_ret(of, ret);
if (unlikely(!size || of->currentsize<=offset))
return psync_fs_unlock_ret(of, 0);
if (offset+size>of->currentsize)
size=of->currentsize-offset;
sectorid=offset/PSYNC_CRYPTO_SECTOR_SIZE;
off2=offset%PSYNC_CRYPTO_SECTOR_SIZE;
rd=0;
if (off2){
if (PSYNC_CRYPTO_SECTOR_SIZE-off2<size)
offdiff=PSYNC_CRYPTO_SECTOR_SIZE-off2;
else
offdiff=size;
ret=psync_fs_crypto_read_newfile_partial_sector(of, buf, sectorid, offdiff, off2);
offset+=offdiff;
buf+=offdiff;
rd+=offdiff;
size-=offdiff;
sectorid++;
if (ret!=offdiff)
return psync_fs_unlock_ret(of, ret);
}
while (size>=PSYNC_CRYPTO_SECTOR_SIZE){
ret=psync_fs_crypto_read_newfile_full_sector(of, buf, sectorid);
if (ret!=PSYNC_CRYPTO_SECTOR_SIZE){
if (ret<0)
return psync_fs_unlock_ret(of, ret);
else
return psync_fs_unlock_ret(of, ret+rd);
}
buf+=PSYNC_CRYPTO_SECTOR_SIZE;
offset+=PSYNC_CRYPTO_SECTOR_SIZE;
rd+=PSYNC_CRYPTO_SECTOR_SIZE;
size-=PSYNC_CRYPTO_SECTOR_SIZE;
sectorid++;
}
if (size){
ret=psync_fs_crypto_read_newfile_partial_sector(of, buf, sectorid, size, 0);
if (ret!=size){
if (ret<0)
return psync_fs_unlock_ret(of, ret);
else
return psync_fs_unlock_ret(of, ret+rd);
}
rd+=size;
}
pthread_mutex_unlock(&of->mutex);
return rd;
}
static void psync_fs_crypto_set_sector_log_offset(psync_openfile_t *of, psync_crypto_sectorid_t sectorid, uint32_t offset, unsigned char *auth){
psync_crypto_sectorid_diff_t d;
psync_sector_inlog_t *tr, *ntr;
psync_tree **pe;
tr=psync_tree_element(of->sectorsinlog, psync_sector_inlog_t, tree);
pe=&of->sectorsinlog;
while (tr){
d=sectorid-tr->sectorid;
if (d<0){
if (tr->tree.left)
tr=psync_tree_element(tr->tree.left, psync_sector_inlog_t, tree);
else{
pe=&tr->tree.left;
break;
}
}
else if (d>0){
if (tr->tree.right)
tr=psync_tree_element(tr->tree.right, psync_sector_inlog_t, tree);
else{
pe=&tr->tree.right;
break;
}
}
else{
tr->logoffset=offset;
memcpy(tr->auth, auth, PSYNC_CRYPTO_AUTH_SIZE);
return;
}
}
ntr=psync_new(psync_sector_inlog_t);
*pe=&ntr->tree;
ntr->sectorid=sectorid;
ntr->logoffset=offset;
memcpy(ntr->auth, auth, PSYNC_CRYPTO_AUTH_SIZE);
psync_tree_added_at(&of->sectorsinlog, &tr->tree, &ntr->tree);
}
static int psync_fs_crypto_switch_sectors(psync_openfile_t *of, psync_crypto_sectorid_t oldsectorid, psync_crypto_sectorid_t newsectorid,
psync_crypto_auth_sector_t *autharr, psync_crypto_offsets_t *offsets){
psync_crypto_log_header hdr;
psync_crypto_offsets_t ooffsets;
int64_t filesize, off;
psync_crypto_sectorid_t oldsecd, newsecd, sizesecd;
uint32_t level, oldsecn, sz;
ssize_t wrt;
if (oldsectorid!=PSYNC_CRYPTO_INVALID_SECTORID){
level=0;
oldsecd=oldsectorid/PSYNC_CRYPTO_HASH_TREE_SECTORS;
newsecd=newsectorid/PSYNC_CRYPTO_HASH_TREE_SECTORS;
sizesecd=get_last_sectorid_by_size(offsets->plainsize)/PSYNC_CRYPTO_HASH_TREE_SECTORS;
do{
oldsecn=oldsecd%PSYNC_CRYPTO_HASH_TREE_SECTORS;
memset(&hdr, 0, sizeof(hdr));
hdr.type=PSYNC_CRYPTO_LOG_DATA;
if (oldsecd==sizesecd){
hdr.offset=offsets->lastauthsectoroff[level];
sz=offsets->lastauthsectorlen[level];
}
else{
assert(oldsecd<sizesecd);
hdr.offset=psync_fs_crypto_auth_offset(level, oldsecd);
sz=PSYNC_CRYPTO_SECTOR_SIZE;
}
assert(hdr.offset+sz<=offsets->masterauthoff+PSYNC_CRYPTO_AUTH_SIZE);
// debug(D_NOTICE, "writing level %u signatures to offset %lu size %u", level, hdr.offset, sz);
hdr.length=sz;
psync_crypto_sign_auth_sector(of->encoder, (unsigned char *)&autharr[level], sz, autharr[level+1][oldsecn]);
wrt=psync_file_pwrite(of->logfile, &hdr, sizeof(hdr), of->logoffset);
if (unlikely(wrt!=sizeof(hdr))){
debug(D_ERROR, "write to log of %u bytes returned %d", (unsigned)sizeof(hdr), (int)wrt);
return -EIO;
}
psync_fast_hash256_update(&of->loghashctx, &hdr, sizeof(hdr));
wrt=psync_file_pwrite(of->logfile, &autharr[level], sz, of->logoffset+sizeof(hdr));
if (unlikely(wrt!=sz)){
debug(D_ERROR, "write to log of %u bytes returned %d", (unsigned)sz, (int)wrt);
return -EIO;
}
psync_fast_hash256_update(&of->loghashctx, &autharr[level], sz);
if (!of->newfile)
psync_interval_tree_add(&of->writeintervals, hdr.offset, hdr.offset+sz);
of->logoffset+=sz+sizeof(hdr);
oldsecd/=PSYNC_CRYPTO_HASH_TREE_SECTORS;
newsecd/=PSYNC_CRYPTO_HASH_TREE_SECTORS;
sizesecd/=PSYNC_CRYPTO_HASH_TREE_SECTORS;
level++;
} while (oldsecd!=newsecd && level<offsets->treelevels);
}
if (newsectorid!=PSYNC_CRYPTO_INVALID_SECTORID){
filesize=psync_file_size(of->datafile);
if (unlikely_log(filesize==-1))
return -EIO;
if (filesize>PSYNC_CRYPTO_AUTH_SIZE){
psync_fs_crypto_offsets_by_cryptosize(filesize, &ooffsets);
level=0;
oldsecd=oldsectorid/PSYNC_CRYPTO_HASH_TREE_SECTORS;
newsecd=newsectorid/PSYNC_CRYPTO_HASH_TREE_SECTORS;
sizesecd=get_last_sectorid_by_size(ooffsets.plainsize)/PSYNC_CRYPTO_HASH_TREE_SECTORS;
do{
if (newsecd<=sizesecd){
if (newsecd==sizesecd){
off=ooffsets.lastauthsectoroff[level];
sz=ooffsets.lastauthsectorlen[level];
}
else{
assert(newsecd<sizesecd);
off=psync_fs_crypto_auth_offset(level, newsecd);
sz=PSYNC_CRYPTO_SECTOR_SIZE;
}
wrt=psync_file_pread(of->datafile, &autharr[level], sz, off);
if (unlikely(wrt!=sz)){
debug(D_ERROR, "read from datafile of %u bytes returned %d at offset %u", (unsigned)sz, (int)wrt, (unsigned)off);
return -EIO;
}
}
oldsecd/=PSYNC_CRYPTO_HASH_TREE_SECTORS;
newsecd/=PSYNC_CRYPTO_HASH_TREE_SECTORS;
sizesecd/=PSYNC_CRYPTO_HASH_TREE_SECTORS;
level++;
} while (oldsecd!=newsecd && level<offsets->treelevels && (level<ooffsets.treelevels || (ooffsets.needmasterauth && level==ooffsets.treelevels)));
}
}
return 0;
}
static int psync_fs_crypto_write_master_auth(psync_openfile_t *of, psync_crypto_auth_sector_t *autharr, psync_crypto_offsets_t *offsets){
ssize_t wrt;
struct {
psync_crypto_log_header hdr;
psync_crypto_sector_auth_t auth;
} data;
assert(sizeof(data)==sizeof(psync_crypto_log_header)+PSYNC_CRYPTO_AUTH_SIZE);
assert(offsets->treelevels>0);
assert(offsets->lastauthsectorlen[offsets->treelevels-1]>0);
assert(offsets->lastauthsectorlen[offsets->treelevels-1]<=PSYNC_CRYPTO_SECTOR_SIZE);
psync_crypto_sign_auth_sector(of->encoder, (unsigned char *)&autharr[offsets->treelevels-1], offsets->lastauthsectorlen[offsets->treelevels-1], data.auth);
memset(&data.hdr, 0, sizeof(psync_crypto_log_header));
data.hdr.type=PSYNC_CRYPTO_LOG_DATA;
data.hdr.length=PSYNC_CRYPTO_AUTH_SIZE;
data.hdr.offset=offsets->masterauthoff;
wrt=psync_file_pwrite(of->logfile, &data, sizeof(data), of->logoffset);
if (unlikely(wrt!=sizeof(data))){
debug(D_ERROR, "write to log of %u bytes returned %d", (unsigned)sizeof(data), (int)wrt);
return -EIO;
}
psync_fast_hash256_update(&of->loghashctx, &data, sizeof(data));
if (!of->newfile)
psync_interval_tree_add(&of->writeintervals, offsets->masterauthoff, offsets->masterauthoff+PSYNC_CRYPTO_AUTH_SIZE);
of->logoffset+=sizeof(data);
debug(D_NOTICE, "wrote master auth to offset %lu", (unsigned long)offsets->masterauthoff);
return 0;
}
static int psync_fs_write_auth_tree_to_log(psync_openfile_t *of, psync_crypto_offsets_t *offsets){
psync_sector_inlog_t *sect;
psync_crypto_sectorid_t lastsect;
int ret;
psync_def_var_arr(authsect, psync_crypto_auth_sector_t, offsets->treelevels+1);
lastsect=PSYNC_CRYPTO_INVALID_SECTORID;
sect=psync_tree_element(psync_tree_get_first(of->sectorsinlog), psync_sector_inlog_t, tree);
while (sect){
if (lastsect/PSYNC_CRYPTO_HASH_TREE_SECTORS!=sect->sectorid/PSYNC_CRYPTO_HASH_TREE_SECTORS){
ret=psync_fs_crypto_switch_sectors(of, lastsect, sect->sectorid, authsect, offsets);
if (ret)
return PRINT_RETURN(ret);
}
memcpy(authsect[0][sect->sectorid%PSYNC_CRYPTO_HASH_TREE_SECTORS], sect->auth, PSYNC_CRYPTO_AUTH_SIZE);
lastsect=sect->sectorid;
sect=psync_tree_element(psync_tree_get_next(§->tree), psync_sector_inlog_t, tree);
}
ret=psync_fs_crypto_switch_sectors(of, lastsect, PSYNC_CRYPTO_INVALID_SECTORID, authsect, offsets);
if (ret)
return PRINT_RETURN(ret);
if (offsets->needmasterauth && (ret=psync_fs_crypto_write_master_auth(of, authsect, offsets)))
return PRINT_RETURN(ret);
debug(D_NOTICE, "wrote tree to log, lastsectorid=%u, currentsize=%lu", (unsigned)lastsect, (unsigned long)of->currentsize);
return 0;
}
PSYNC_NOINLINE static int psync_fs_crypto_check_log_hash(psync_file_t lfd, psync_crypto_master_record *mr){
char buff[PSYNC_CRYPTO_SECTOR_SIZE];
psync_fast_hash256_ctx ctx;
uint64_t off;
ssize_t rd;
unsigned char chash[PSYNC_FAST_HASH256_LEN];
if (unlikely(mr->hashid!=PSYNC_LOG_HASHID_FH256)){
debug(D_WARNING, "invalid hashid in log file %u", (unsigned)mr->hashid);
return -1;
}
psync_fast_hash256_init(&ctx);
off=PSYNC_CRYPTO_SECTOR_SIZE;
while ((rd=psync_file_pread(lfd, buff, PSYNC_CRYPTO_SECTOR_SIZE, off))!=0){
if (unlikely(rd==-1)){
debug(D_WARNING, "got error %d while reading from log", (int)psync_fs_err());
return -1;
}
psync_fast_hash256_update(&ctx, buff, rd);
off+=rd;
}
psync_fast_hash256_final(chash, &ctx);
if (memcmp(chash, mr->hash, PSYNC_FAST_HASH256_LEN)){
debug(D_WARNING, "calculated hash does not match");
#if IS_DEBUG
psync_binhex(buff, chash, PSYNC_FAST_HASH256_LEN);
buff[PSYNC_FAST_HASH256_LEN*2]=0;
debug(D_NOTICE, "calculated: %s", buff);
psync_binhex(buff, mr->hash, PSYNC_FAST_HASH256_LEN);
buff[PSYNC_FAST_HASH256_LEN*2]=0;
debug(D_NOTICE, "expected: %s", buff);
#endif
return -1;
}
else{
debug(D_NOTICE, "successfully checked log hash");
return 0;
}
}
static int psync_fs_crypto_process_log(psync_file_t lfd, psync_file_t dfd, psync_file_t ifd, int checkhash){
char buff[PSYNC_CRYPTO_SECTOR_SIZE];
psync_fs_index_record *records;
psync_crypto_master_record *mr;
psync_crypto_log_header hdr;
uint64_t off, size, ioff;
ssize_t rd;
uint32_t recid;
mr=(psync_crypto_master_record *)buff;
rd=psync_file_pread(lfd, mr, sizeof(psync_crypto_master_record), 0);
if (unlikely(rd!=sizeof(psync_crypto_master_record))){
debug(D_WARNING, "error reading from log file, expected to read %u got %d", (unsigned)sizeof(psync_crypto_master_record), (int)rd);
return -1;
}
if (unlikely(mr->status!=PSYNC_LOG_STATUS_FINALIZED)){
debug(D_WARNING, "got log file that is not finalized, skipping");
return -1;
}
if (unlikely(mr->logsize!=psync_file_size(lfd))){
debug(D_WARNING, "got log file that does not match in size, expected %u, got %d", (unsigned)mr->logsize, (int)psync_file_size(lfd));
return -1;
}
if (unlikely(mr->crc!=psync_crc32c(PSYNC_CRC_INITIAL, mr, offsetof(psync_crypto_master_record, crc)))){
debug(D_WARNING, "got log file with bad master record CRC, expected %u got %u", (unsigned)mr->crc,
(unsigned)psync_crc32c(PSYNC_CRC_INITIAL, mr, offsetof(psync_crypto_master_record, crc)));
return -1;
}
if (unlikely(checkhash && psync_fs_crypto_check_log_hash(lfd, mr))){
debug(D_WARNING, "log checksum failed, skipping replay");
return -1;
}
size=mr->filesize;
off=PSYNC_CRYPTO_SECTOR_SIZE;
if (unlikely_log(psync_file_seek(dfd, size, P_SEEK_SET)!=size || psync_file_truncate(dfd)))
return -1;
if (ifd!=INVALID_HANDLE_VALUE && unlikely_log(psync_file_seek(ifd, sizeof(psync_fs_index_header), P_SEEK_SET)!=sizeof(psync_fs_index_header) || psync_file_truncate(ifd)))
return -1;
records=(psync_fs_index_record *)buff;
recid=0;
ioff=0;
while ((rd=psync_file_pread(lfd, &hdr, sizeof(hdr), off))!=0){
if (unlikely_log(rd!=sizeof(hdr)))
return -1;
off+=sizeof(hdr);
if (hdr.type==PSYNC_CRYPTO_LOG_DATA){
assert(hdr.length<=sizeof(buff));
assert(recid==0);
if (unlikely(hdr.offset+hdr.length>size)){
debug(D_NOTICE, "got record past the current end of file, this should only happen if file was truncated down, skipping record");
off+=hdr.length;
continue;
}
rd=psync_file_pread(lfd, buff, hdr.length, off);
if (unlikely(rd!=hdr.length)){
debug(D_ERROR, "error reading from log file, expected to read %u got %d", (unsigned)hdr.length, (int)rd);
return -1;
}
off+=hdr.length;
rd=psync_file_pwrite(dfd, buff, hdr.length, hdr.offset);
if (unlikely(rd!=hdr.length)){
debug(D_ERROR, "error writing to data file, expected to write %u got %d", (unsigned)hdr.length, (int)rd);
return -1;
}
}
else if (hdr.type==PSYNC_CRYPTO_LOG_INT){
assert(ifd!=INVALID_HANDLE_VALUE);
records[recid].offset=hdr.offset;
records[recid].length=hdr.longlengthlo+((uint64_t)hdr.longlengthhi<<32);
if (++recid>=PSYNC_CRYPTO_SECTOR_SIZE/sizeof(psync_fs_index_record)){
rd=psync_file_pwrite(ifd, records, sizeof(psync_fs_index_record)*recid, sizeof(psync_fs_index_record)*ioff+sizeof(psync_fs_index_header));
if (rd!=sizeof(psync_fs_index_record)*recid){
debug(D_ERROR, "error writing to index file, expected to write %u got %d", (unsigned)(sizeof(psync_fs_index_record)*recid), (int)rd);
return -1;
}
ioff+=recid;
recid=0;
}
}
else{
debug(D_ERROR, "bad record type %u", (unsigned)hdr.type);
return -1;
}
}
if (recid){
rd=psync_file_pwrite(ifd, records, sizeof(psync_fs_index_record)*recid, sizeof(psync_fs_index_record)*ioff+sizeof(psync_fs_index_header));
if (rd!=sizeof(psync_fs_index_record)*recid){
debug(D_ERROR, "error writing to index file, expected to write %u got %d", (unsigned)(sizeof(psync_fs_index_record)*recid), (int)rd);
return -1;
}
}
if (unlikely_log(psync_file_seek(dfd, size, P_SEEK_SET)!=size || psync_file_truncate(dfd)))
return -1;
return 0;
}
static void wait_before_flush(psync_openfile_t *of, uint32_t millisec){
debug(D_NOTICE, "waiting up to %u milliseconds before flush of %s", (unsigned)millisec, of->currentname);
psync_milisleep(millisec);
}
/*static int psync_fs_flush_cache_dir(){
const char *path;
int ret;
path=psync_setting_get_string(_PS(fscachepath));
debug(D_NOTICE, "flushing directory %s", path);
ret=psync_folder_sync(path);
if (!ret)
debug(D_NOTICE, "flushed directory %s", path);
return ret;
}*/
static int psync_fs_crypto_mark_log_finalized(psync_openfile_t *of, uint64_t filesize){
psync_crypto_master_record mr;
int64_t lsize;
ssize_t wrt;
lsize=psync_file_size(of->logfile);
if (unlikely_log(lsize==-1))
return -EIO;
assert(lsize==of->loghashctx.length+PSYNC_CRYPTO_SECTOR_SIZE);
mr.status=PSYNC_LOG_STATUS_FINALIZED;
mr.hashid=PSYNC_LOG_HASHID_FH256;
mr.logsize=lsize;
mr.filesize=filesize;
psync_fast_hash256_final(&mr.hash, &of->loghashctx);
mr.crc=psync_crc32c(PSYNC_CRC_INITIAL, &mr, offsetof(psync_crypto_master_record, crc));
wrt=psync_file_pwrite(of->logfile, &mr, sizeof(mr), 0);
if (unlikely_log(wrt!=sizeof(mr)))
return -EIO;
else
return 0;
}
static int psync_fs_crypto_log_flush_and_process(psync_openfile_t *of, const char *filename, int dowaits, int locked){
psync_file_t fd;
fd=psync_file_open(filename, P_O_RDWR, 0);
if (unlikely_log(fd==INVALID_HANDLE_VALUE))
return -EIO;
if (dowaits && !locked)
wait_before_flush(of, 2000);
debug(D_NOTICE, "flushing log data %s", filename);
if (unlikely_log(psync_file_sync(fd)))
goto err_eio;
debug(D_NOTICE, "flushed log data %s", filename);
/* flushing directory does not seem to work on either Windows on Mac, disable for now, maybe look at SQLite code to see if they flush
if (unlikely_log(psync_fs_flush_cache_dir()))
goto err_eio;
*/
// assert(NULL=="break here to test log replay");
if (unlikely_log(psync_fs_crypto_process_log(fd, of->datafile, of->indexfile, 0)))
goto err_eio;
psync_file_close(fd);
if (dowaits && !locked)
wait_before_flush(of, 2000);
debug(D_NOTICE, "flushing data of %s", of->currentname);
if (unlikely_log(psync_file_sync(of->datafile)))
return -EIO;
debug(D_NOTICE, "flushed data of %s", of->currentname);
if (!of->newfile){
debug(D_NOTICE, "flushing index of %s", of->currentname);
if (unlikely_log(psync_file_sync(of->indexfile)))
return -EIO;
debug(D_NOTICE, "flushed index of %s", of->currentname);
}
return 0;
err_eio:
psync_file_close(fd);
return -EIO;
}
static int psync_fs_write_interval_tree_to_log(psync_openfile_t *of){
psync_crypto_log_header logs[4096/sizeof(psync_crypto_log_header)];
psync_interval_tree_t *itr;
uint64_t len;
ssize_t wrt;
uint32_t last;
last=0;
itr=psync_interval_tree_get_first(of->writeintervals);
while (itr){
if (last==ARRAY_SIZE(logs)){
wrt=psync_file_pwrite(of->logfile, logs, sizeof(logs), of->logoffset);
if (wrt!=sizeof(logs)){
debug(D_ERROR, "write to log of %u bytes returned %d", (unsigned)sizeof(logs), (int)wrt);
return -EIO;
}
psync_fast_hash256_update(&of->loghashctx, logs, sizeof(logs));
of->logoffset+=sizeof(logs);
last=0;
}
len=itr->to-itr->from;
assert((len>>48)==0);
logs[last].type=PSYNC_CRYPTO_LOG_INT;
logs[last].longlengthhi=len>>32;
logs[last].longlengthlo=len&0xffffffffU;
logs[last].offset=itr->from;
last++;
itr=psync_interval_tree_get_next(itr);
}
if (last){
last*=sizeof(psync_crypto_log_header);
wrt=psync_file_pwrite(of->logfile, logs, last, of->logoffset);
if (wrt!=last){
debug(D_ERROR, "write to log of %u bytes returned %d", (unsigned)(sizeof(psync_crypto_log_header)*last), (int)wrt);
return -EIO;
}
psync_fast_hash256_update(&of->loghashctx, logs, last);
of->logoffset+=last;
}
return 0;
}
static int psync_fs_crypto_do_finalize_log(psync_openfile_t *of, int fullsync){
psync_crypto_offsets_t offsets;
psync_fsfileid_t fileid;
const char *cachepath;
char *olog, *flog;
char fileidhex[sizeof(psync_fsfileid_t)*2+2];
int ret;
psync_fs_crypto_offsets_by_plainsize(of->currentsize, &offsets);
if (of->logoffset==PSYNC_CRYPTO_SECTOR_SIZE && offsets.masterauthoff+(offsets.needmasterauth?PSYNC_CRYPTO_AUTH_SIZE:0)==psync_file_size(of->datafile)){
debug(D_NOTICE, "skipping finalize of %s", of->currentname);
return 0;
}
debug(D_NOTICE, "finalizing log of %s size %lu", of->currentname, (unsigned long)of->currentsize);
psync_fs_crypto_offsets_by_plainsize(of->currentsize, &offsets);
ret=psync_fs_write_auth_tree_to_log(of, &offsets);
if (unlikely_log(ret))
return ret;
debug(D_NOTICE, "wrote checksums to log of %s", of->currentname);
if (!of->newfile){
debug(D_NOTICE, "writing interval tree to log of %s", of->currentname);
ret=psync_fs_write_interval_tree_to_log(of);
if (unlikely_log(ret))
return ret;
debug(D_NOTICE, "wrote interval tree to log of %s", of->currentname);
}
ret=psync_fs_crypto_mark_log_finalized(of, offsets.masterauthoff+(offsets.needmasterauth?PSYNC_CRYPTO_AUTH_SIZE:0));
if (unlikely_log(ret))
return ret;
psync_file_schedulesync(of->logfile);
ret=psync_file_close(of->logfile);
of->logfile=INVALID_HANDLE_VALUE;
if (unlikely_log(ret))
return -EIO;
debug(D_NOTICE, "finalized log of %s", of->currentname);
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;
olog=psync_strcat(cachepath, PSYNC_DIRECTORY_SEPARATOR, fileidhex, NULL);
fileidhex[sizeof(psync_fsfileid_t)]='f';
flog=psync_strcat(cachepath, PSYNC_DIRECTORY_SEPARATOR, fileidhex, NULL);
if (unlikely_log(psync_file_rename_overwrite(olog, flog)) ||
unlikely_log((of->logfile=psync_file_open(olog, P_O_RDWR, P_O_CREAT|P_O_TRUNC))==INVALID_HANDLE_VALUE) ||
unlikely_log(psync_fs_crypto_init_log(of))){
psync_free(olog);
psync_free(flog);
return -EIO;
}
psync_tree_for_each_element_call_safe(of->sectorsinlog, psync_sector_inlog_t, tree, psync_free);
of->sectorsinlog=PSYNC_TREE_EMPTY;
ret=psync_fs_crypto_log_flush_and_process(of, flog, 0, 1);
psync_file_delete(flog);
psync_free(olog);
psync_free(flog);
return PRINT_NEG_RETURN(ret);
}
static int psync_fs_crypto_finalize_log(psync_openfile_t *of, int fullsync){
int ret;
if (of->extender){
assert(of->currentsize==of->extender->extendto);
of->currentsize=of->extender->extendedto;
ret=psync_fs_crypto_do_finalize_log(of, fullsync);
of->currentsize=of->extender->extendto;
}
else
ret=psync_fs_crypto_do_finalize_log(of, fullsync);
return ret;
}
static void psync_fs_crypt_add_sector_to_interval_tree(psync_openfile_t *of, psync_crypto_sectorid_t sectorid, size_t size){
uint64_t offset;
offset=(uint64_t)psync_fs_crypto_data_sectorid_by_sectorid(sectorid)*PSYNC_CRYPTO_SECTOR_SIZE;
psync_interval_tree_add(&of->writeintervals, offset, offset+size);
}
PSYNC_NOINLINE static void psync_fs_crypto_reset_log_to_off(psync_openfile_t *of, uint32_t off){
int64_t sz;
sz=psync_file_size(of->logfile);
if (sz!=off)
debug(D_NOTICE, "need to reset log from size %d to %u", (int)sz, (unsigned)off);
if (sz==-1 || sz<off || (sz>off && (psync_file_seek(of->logfile, off, P_SEEK_SET)!=off || psync_file_truncate(of->logfile)))){
const char *cachepath;