-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbcvutil.c
3938 lines (3547 loc) · 107 KB
/
bcvutil.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
/*
** 2020-05-12
**
******************************************************************************
**
*/
#include "bcv_int.h"
#include "sqlite3.h"
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#ifndef __WIN32__
#include <unistd.h>
#endif
#include <openssl/md5.h>
#define BCV_MIMETYPE_HDR "Content-Type:application/octet-stream"
#define BCV_MIMETYPE_HDR_LC "content-type:application/octet-stream"
#define BCV_AWS_DATE_SZ 16
#define BCV_AWS_HASH_SZ (2*SHA256_DIGEST_LENGTH)
#define BCV_AWS_CONTENTHASH_HDR "x-amz-content-sha256:UNSIGNED-PAYLOAD"
/* Maximum value for SQLITE_BCVCONFIG_LOG */
#define BCV_MAX_NREQUEST 32
#define BCV_MANIFEST_SCHEMA \
"CREATE TABLE config( \n" \
" name TEXT PRIMARY KEY, \n" \
" value \n" \
"); \n" \
"CREATE TABLE database( \n" \
" dbid INTEGER PRIMARY KEY AUTOINCREMENT, \n" \
" name TEXT, \n" \
" version INTEGER, \n" \
" blocks INTEGER, \n" \
" parent INTEGER REFERENCES database (dbid) \n" \
"); \n" \
" \n" \
"CREATE TABLE block( \n" \
" dbid INTEGER, /* REFERENCES database(id) */ \n" \
" block_offset INTEGER, \n" \
" block_file BLOB, \n" \
" PRIMARY KEY(dbid, block_offset) \n" \
") WITHOUT ROWID; \n" \
" \n" \
"CREATE TABLE deleted(block_file BLOB, timestamp INTEGER); \n"
typedef int(*bcv_progress_cb)(void*, sqlite3_int64, sqlite3_int64);
typedef void(*bcv_log_cb)(void*, const char *);
struct sqlite3_bcv {
BcvContainer *pCont; /* Cloud module instance */
BcvDispatch *pDisp; /* Dispatcher to manage callbacks */
char *zErrmsg; /* Error message from most recent API call */
int errCode; /* Error code from most recent API call */
int nRequest; /* Max outstanding requests for up/download */
int bVerbose; /* True to use verbose curl handles */
int nLogLevel;
int nHttpTimeout; /* SQLITE_BCVCONFIG_HTTPTIMEOUT option */
void *pProgressCtx; /* First argument to pass to xProgress() */
bcv_progress_cb xProgress; /* Progress-handler callback */
int bTestNoKv; /* SQLITE_BCVCONFIG_TESTNOKV option */
bcv_log_cb xBcvLog;
void *pBcvLogCtx;
BcvLog *pBcvLogObj;
};
typedef struct sqlite3_bcv_job BcvDispatchJob;
typedef struct sqlite3_bcv_request BcvDispatchReq;
struct BcvDispatch {
CURLM *pMulti; /* The libcurl dispatcher ("multi-handle") */
int bVerbose; /* True to make libcurl verbose */
int nHttpTimeout;
int iNextRequestId; /* Next request-id (for logging only) */
int iDispatchId; /* Dispatch id (for logging only) */
char *zLogmsg; /* Logging caption for next http request */
const char *zClientId; /* Logging client name */
BcvDispatchJob *pJobList; /* List of current jobs */
void *pLogApp; /* First argument to xLog() */
void (*xLog)(void*,int,const char *zMsg); /* Log function to invoke */
BcvLog *pLog; /* Log object (may be NULL) */
};
struct sqlite3_bcv_job {
BcvDispatch *pDispatch;
int rc;
int eType; /* Type of job (BCV_DISPATCH_XXX value) */
int bLogEtag; /* True to log eTag of http replies */
char *zLogmsg; /* Logging caption */
const char *zClientId; /* Logging client name */
void *pCbApp; /* First argument for callback */
struct {
void (*xFetch)(void*, int rc, char *zETag, const u8*, int, const u8*, int);
void (*xPut)(void*, int rc, char *zETag);
void (*xCreate)(void*, int rc, char *zError);
void (*xDestroy)(void*, int rc, char *zError);
void (*xList)(void*, int rc, char *z);
void (*xDelete)(void*, int rc, char *zError);
} cb;
/* Result values */
char *zETag;
u8 *aResult;
int nResult;
u8 *aHdrs;
int nHdrs;
char *zPrefix; /* Prefix for List operations */
BcvContainer *pCont; /* Associated container object */
BcvDispatchReq *pPending; /* Requests yet to be issued */
BcvDispatchReq *pWaiting; /* Waiting request list */
BcvDispatchJob *pNext; /* Next job belonging to same dispatcher */
};
#define BCV_LOGLEVEL_DEBUG 1
/*
** Values that may be stored in BcvDispatchJob.eType variables.
*/
#define BCV_DISPATCH_FETCH 1
#define BCV_DISPATCH_LIST 2
#define BCV_DISPATCH_PUT 3
#define BCV_DISPATCH_CREATE 4
#define BCV_DISPATCH_DESTROY 5
#define BCV_DISPATCH_DELETE 6
struct sqlite3_bcv_request {
BcvDispatchJob *pJob; /* Job that owns this request */
int eMethod; /* SQLITE_BCV_METHOD_GET/PUT/DELETE */
int nRetry; /* # of times request has already failed */
i64 iRetryTime; /* Do not retry before this time */
char *zUri; /* URI */
char *zUriLog; /* URI value for logging */
struct curl_slist *pList; /* List of HTTP headers for request */
void *pApp; /* 3rd argument to xCallback */
void (*xCallback)(sqlite3_bcv_job*, sqlite3_bcv_request*, void*);
i64 iLogId; /* Logging id of request */
const u8 *aBody; /* Buffer of data to upload (METHOD_PUT) */
int nBody; /* Size of aBody[] in bytes */
int iBody; /* Current offset in aBody[] */
int rc;
CURLcode code;
char *zStatus; /* Most recent Status header from server */
BcvBuffer body; /* Body of reply */
BcvBuffer hdr; /* Headers */
CURL *pCurl; /* Curl easy-handle */
BcvDispatchReq *pNext; /* Next in pPending or pWaiting list */
};
int bcvStrcmp(const char *zLeft, const char *zRight){
if( zLeft==zRight ){
return 0;
}else if( zLeft==0 ){
return -1;
}else if( zRight==0 ){
return +1;
}
return strcmp(zLeft, zRight);
}
int bcvStrlen(const char *z){
return (z ? strlen(z) : 0);
}
void fatal_oom_error(void){
fprintf(stderr, "FATAL: out-of-memory error\n");
exit(-1);
}
/*
** Wrapper around sqlite3_mprintf() that calls fatal_oom_error() if an
** OOM occurs.
*/
char *bcvMprintf(const char *zFmt, ...){
char *zRet;
va_list ap;
va_start(ap, zFmt);
zRet = sqlite3_vmprintf(zFmt, ap);
va_end(ap);
if( zRet==0 ) fatal_oom_error();
return zRet;
}
char *bcvMprintfRc(int *pRc, const char *zFmt, ...){
char *zRet = 0;
va_list ap;
va_start(ap, zFmt);
if( *pRc==SQLITE_OK ){
zRet = sqlite3_vmprintf(zFmt, ap);
if( zRet==0 ) *pRc = SQLITE_NOMEM;
}
va_end(ap);
return zRet;
}
void *bcvMalloc(i64 nByte){
void *p = sqlite3_malloc64(nByte);
assert( nByte!=0 );
if( p==0 ) fatal_oom_error();
return p;
}
void *bcvRealloc(void *a, i64 nByte){
void *p = sqlite3_realloc64(a, nByte);
assert( nByte!=0 );
if( p==0 ) fatal_oom_error();
return p;
}
void *bcvMallocZero(i64 nByte){
void *p = bcvMalloc(nByte);
memset(p, 0, nByte);
return p;
}
void *bcvMallocRc(int *pRc, i64 nByte){
void *pRet = 0;
if( nByte>0 && *pRc==SQLITE_OK ){
pRet = sqlite3_malloc64(nByte);
if( pRet==0 ){
*pRc = SQLITE_NOMEM;
}else{
memset(pRet, 0, nByte);
}
}
return pRet;
}
static void *bcvReallocOrFreeRc(int *pRc, void *pIn, int nByte){
void *pRet = 0;
if( nByte>0 && *pRc==SQLITE_OK ){
pRet = sqlite3_realloc(pIn, nByte);
if( pRet==0 ){
sqlite3_free(pIn);
*pRc = SQLITE_NOMEM;
}
}else{
sqlite3_free(pIn);
}
return pRet;
}
char *bcvStrdupRc(int *pRc, const char *z){
char *zRet = 0;
if( z ){
int n = bcvStrlen(z);
zRet = bcvMallocRc(pRc, n+1);
if( zRet ){
memcpy(zRet, z, n+1);
}
}
return zRet;
}
void bcvBufferAppendRc(int *pRc, BcvBuffer *p, const void *a, int n){
if( n ){
int nByte = p->nAlloc;
if( p->nData+n>nByte ){
if( nByte==0 ){
nByte = n;
}else{
nByte = p->nAlloc;
while( nByte<p->nData+n ){ nByte = nByte*2; }
}
p->aData = bcvReallocOrFreeRc(pRc, p->aData, nByte);
}
if( p->aData ){
memcpy(&p->aData[p->nData], a, n);
p->nData += n;
p->nAlloc = nByte;
}
}
}
void bcvBufferZero(BcvBuffer *p){
sqlite3_free(p->aData);
memset(p, 0, sizeof(BcvBuffer));
}
/*
** Return a duplicate of the nIn byte buffer at aIn[]. It is the
** responsibility of the caller to eventually free the returned buffer
** by passing a pointer to it to sqlite3_free().
*/
u8 *bcvMemdup(int nIn, const u8 *aIn){
u8 *aRet = bcvMalloc(nIn+1);
memcpy(aRet, aIn, nIn);
aRet[nIn] = 0x00;
return aRet;
}
char *bcvStrdup(const char *zIn){
char *zNew = 0;
if( zIn ){
zNew = (char*)bcvMemdup(strlen(zIn), (const u8*)zIn);
}
return zNew;
}
i64 sqlite_timestamp(void){
i64 iRet = 0;
sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
pVfs->xCurrentTimeInt64(pVfs, &iRet);
return iRet;
}
int bcv_isdigit(char c){
return c>='0' && c<='9';
}
int bcv_isspace(char c){
return (c==' ' || c=='\n' || c=='\r' || c=='\t');
}
/*
** Primitives to read and write 32-bit big-endian integers.
*/
u32 bcvGetU16(const u8 *a){
return ((u32)a[0] << 8) + ((u32)a[1] << 0);
}
void bcvPutU32(u8 *a, u32 iVal){
a[0] = (iVal>>24) & 0xFF;
a[1] = (iVal>>16) & 0xFF;
a[2] = (iVal>> 8) & 0xFF;
a[3] = (iVal>> 0) & 0xFF;
}
u32 bcvGetU32(const u8 *a){
return ((u32)a[0] << 24) + ((u32)a[1] << 16)
+ ((u32)a[2] << 8) + ((u32)a[3] << 0);
}
void bcvPutU64(u8 *a, u64 iVal){
a[0] = (iVal>>56) & 0xFF;
a[1] = (iVal>>48) & 0xFF;
a[2] = (iVal>>40) & 0xFF;
a[3] = (iVal>>32) & 0xFF;
a[4] = (iVal>>24) & 0xFF;
a[5] = (iVal>>16) & 0xFF;
a[6] = (iVal>> 8) & 0xFF;
a[7] = (iVal>> 0) & 0xFF;
}
u64 bcvGetU64(const u8 *a){
return ((u64)a[0] << 56)
+ ((u64)a[1] << 48)
+ ((u64)a[2] << 40)
+ ((u64)a[3] << 32)
+ ((u64)a[4] << 24)
+ ((u64)a[5] << 16)
+ ((u64)a[6] << 8)
+ ((u64)a[7] << 0);
}
static void bcvApiErrorClear(sqlite3_bcv *p){
sqlite3_free(p->zErrmsg);
p->zErrmsg = 0;
p->errCode = SQLITE_OK;
}
static int bcvApiError(sqlite3_bcv *p, int rc, const char *zFmt, ...){
va_list ap;
va_start(ap, zFmt);
bcvApiErrorClear(p);
p->zErrmsg = sqlite3_vmprintf(zFmt, ap);
p->errCode = rc;
va_end(ap);
return rc;
}
/*
** Return the index of the hash bucket of pHash that may contain the block
** with binary name aBlk[].
*/
static int bcvMHashBucket(ManifestHash *pHash, u8 *aBlk){
return (bcvGetU32(&aBlk[4]) % pHash->nHash);
}
/*
** Add aBlk to the hash passed as the first argument.
*/
void bcvMHashAdd(ManifestHash *pHash, u8 *aBlk, int nBlk){
int iBucket = bcvMHashBucket(pHash, aBlk);
while( pHash->aHash[iBucket] ){
if( 0==memcmp(pHash->aHash[iBucket], aBlk, nBlk) ) return;
iBucket = (iBucket+1) % pHash->nHash;
}
pHash->aHash[iBucket] = aBlk;
}
/*
** Free hash table pHash.
*/
void bcvMHashFree(ManifestHash *pHash){
sqlite3_free(pHash);
}
/*
** Query the hash table for an entry with nBlk byte prefix aBlk.
*/
u8 *bcvMHashQuery(ManifestHash *pHash, u8 *aBlk, int nBlk){
u8 *aEntry;
int iBucket = bcvMHashBucket(pHash, aBlk);
while( (aEntry = pHash->aHash[iBucket]) ){
if( memcmp(aBlk, aEntry, nBlk)==0 ){
return aEntry;
}
iBucket = (iBucket+1) % pHash->nHash;
}
return 0;
}
/*
** Build a hash table containing all blocks from all databases except
** pExclude. Or, if pExclude is NULL, from all databases.
**
** If parameter bDeleteList is true, then the hash table also contains
** elements from the delete list.
*/
int bcvMHashBuild(
Manifest *pMan,
int bDeleteList,
ManifestDb *pExclude,
ManifestHash **ppHash
){
int rc = SQLITE_OK;
int nName = NAMEBYTES(pMan);
int nGCName = GCENTRYBYTES(pMan);
i64 nBlk = 0; /* Number of blocks in hash table */
i64 nHash = 0; /* Number of buckets in hash table */
i64 nAlloc; /* Bytes of space to allocate */
ManifestHash *pHash = 0;
int ii;
/* Count the number of blocks in all databases and on the delete list */
if( bDeleteList ){
nBlk = pMan->nDelBlk;
}
for(ii=0; ii<pMan->nDb; ii++){
ManifestDb *pDb = &pMan->aDb[ii];
ManifestDb *pPar = 0;
int iBlk;
if( pDb==pExclude ) continue;
if( pDb->iParentId!=0 ){
pPar = bcvManifestDbidToDb(pMan, pDb->iParentId);
if( pPar==pExclude ) pPar = 0;
}
for(iBlk=0; iBlk<pDb->nBlkOrig; iBlk++){
if( !pPar
|| iBlk>=pPar->nBlkOrig
|| memcmp(&pPar->aBlkOrig[iBlk*nName], &pDb->aBlkOrig[iBlk*nName], nName)
){
nBlk++;
}
}
}
/* Figure out how many hash buckets to use */
nHash = 128;
while( nHash<nBlk*8 ){
nHash = nHash*2;
}
/* Allocate and populate the hash table */
nAlloc = sizeof(ManifestHash) + nHash*sizeof(u8*);
pHash = (ManifestHash*)bcvMallocRc(&rc, nAlloc);
if( rc==SQLITE_OK ){
int iBlk;
pHash->nHash = nHash;
pHash->aHash = (u8**)&pHash[1];
for(ii=0; ii<pMan->nDb; ii++){
ManifestDb *pDb = &pMan->aDb[ii];
if( pDb==pExclude ) continue;
for(iBlk=0; iBlk<pDb->nBlkOrig; iBlk++){
u8 *pBlk = &pDb->aBlkOrig[iBlk*nName];
bcvMHashAdd(pHash, pBlk, nName);
}
}
if( bDeleteList ){
for(iBlk=0; iBlk<pMan->nDelBlk; iBlk++){
u8 *pBlk = &pMan->aDelBlk[iBlk*nGCName];
bcvMHashAdd(pHash, pBlk, nName);
}
}
}
*ppHash = pHash;
return rc;
}
ManifestDb *bcvManifestDbidToDb(Manifest *p, i64 iDbId){
if( p->nDb>0 ){
int i1 = 0;
int i2 = p->nDb-1;
while( i1<=i2 ){
int iTest = (i1+i2+1) / 2;
i64 iTestId = p->aDb[iTest].iDbId;
if( iTestId==iDbId ){
return &p->aDb[iTest];
}else if( iTestId>iDbId ){
i2 = iTest-1;
}else{
i1 = iTest+1;
}
}
#ifndef NDEBUG
{
int ii;
for(ii=0; ii<p->nDb; ii++) assert( p->aDb[ii].iDbId!=iDbId);
}
#endif
}
return 0;
}
/*
** MANIFEST FILE FORMAT VERSION 4
**
** The manifest file format is an ad-hoc binary design. It does not use
** XML or any other formatting convention. This is to keep it as compact
** as possible.
**
** All "integer" values mentioned below are unsigned values stored in
** big-endian format.
**
** Part 1: Manifest Header
**
** Manifest format version: 32-bit integer. (currently set to 3).
** Block size in bytes: 32-bit integer.
** Number of databases: 32-bit integer.
** Number of delete entries: 32-bit integer.
** Name-size in bytes: 32-bit integer. (between 12 and 32)
** Largest db id assigned: 32-bit integer.
** Then, for each database:
** Database id: 32-bit integer. (1 or greater)
** Parent id: 32-bit integer. (0 means no parent)
** Database version: 32-bit integer.
** Database offset: 32-bit integer.
** Number of blocks in db: 32-bit integer.
** Entries in block array: 32-bit integer.
** Database display name: 128 byte utf-8. (zero padded)
**
** Part 2: Delete Block list.
**
** An array of entries immediately following the last database header.
** Each entry consists of:
**
** Block id: name-size bytes.
** Timestamp: 64-bit integer.
**
** Timestamps indicate when a block was moved to the delete-list.
**
** Part 3: Database Block lists.
**
** There is a database block list for each database identified in the
** manifest header. Each block list begins at the offset identified in
** the manifest header and contains the number of entries also specified
** in the header.
**
** There are two formats for a block-list, depending on whether or not
** the parent id in the header is set to 0 or not. If it is set to 0, then
** the block-list contains tightly packed block-ids, one for each block
** in the database. Or, if the parent id is non-zero, each entry consists
** of:
**
** Block offset: 32-bit integer.
** Block id: name-size bytes.
**
*/
#define MANIFEST_HDR_SIZE (6*sizeof(u32))
#define MANIFEST_DBHDR_SIZE (6*sizeof(u32)+128)
/*
** Parse the manifest in buffer a[], size n bytes, and return a Manifest
** structure with ref-count set to 1 reflecting the results.
*/
int bcvManifestParse(
u8 *a, int n, /* Buffer to parse */
char *zETag, /* etag value for new manifest */
Manifest **ppOut, /* OUT: new parsed manifest object */
char **pzErr /* OUT: error message */
){
Manifest *pRet = 0;
int iDb;
int rc = SQLITE_OK;
/* Header fields */
u32 iVersion;
u32 nBlocksize;
u32 nDb;
u32 nDelete;
u32 nNamesize;
u32 iMaxDbId;
iVersion = bcvGetU32(&a[0]);
nBlocksize = bcvGetU32(&a[4]);
nDb = bcvGetU32(&a[8]);
nDelete = bcvGetU32(&a[12]);
nNamesize = bcvGetU32(&a[16]);
iMaxDbId = bcvGetU32(&a[20]);
if( iVersion!=BCV_MANIFEST_VERSION ){
*pzErr = bcvMprintf(
"bad manifest version - expected %d, found %d.",
BCV_MANIFEST_VERSION, iVersion
);
rc = SQLITE_ERROR;
}
/* Allocate Manifest object */
if( rc==SQLITE_OK ){
int nByte = sizeof(Manifest) + (nDb+1)*sizeof(ManifestDb);
pRet = (Manifest*)bcvMallocRc(&rc, nByte);
if( pRet ){
pRet->nRef = 1;
pRet->szBlk = nBlocksize;
pRet->nNamebytes = nNamesize;
pRet->iMaxDbId = iMaxDbId;
pRet->zETag = zETag;
pRet->pFree = a;
pRet->aDb = (ManifestDb*)&pRet[1];
pRet->aDelBlk = &a[MANIFEST_HDR_SIZE + MANIFEST_DBHDR_SIZE*nDb];
pRet->nDelBlk = nDelete;
}
}
for(iDb=0; rc==SQLITE_OK && iDb<nDb; iDb++){
ManifestDb *pDb = &pRet->aDb[iDb];
int iDbOff = MANIFEST_HDR_SIZE + MANIFEST_DBHDR_SIZE*iDb;
int iOff;
int nEntry;
int bDel = 0;
pDb->iDbId = bcvGetU32(&a[iDbOff+0]);
pDb->iParentId = bcvGetU32(&a[iDbOff+4]);
pDb->iVersion = bcvGetU32(&a[iDbOff+8]);
iOff = bcvGetU32(&a[iDbOff+12]);
pDb->nBlkOrig = bcvGetU32(&a[iDbOff+16]);
if( pDb->nBlkOrig & 0x80000000 ){
pDb->nBlkOrig &= 0x7FFFFFFF;
bDel = 1;
}
nEntry = bcvGetU32(&a[iDbOff+20]);
memcpy(pDb->zDName, &a[iDbOff+24], BCV_DBNAME_SIZE);
if( pDb->nBlkOrig ){
if( pDb->iParentId ){
pDb->nBlkOrigAlloc = pDb->nBlkOrig;
pDb->aBlkOrig = (u8*)bcvMallocRc(&rc, pDb->nBlkOrig*NAMEBYTES(pRet));
if( pDb->aBlkOrig ){
int ii;
ManifestDb *pPar = bcvManifestDbidToDb(pRet, pDb->iParentId);
if( pPar ){
int nCopy = MIN(pPar->nBlkOrig, pDb->nBlkOrig) * nNamesize;
memcpy(pDb->aBlkOrig, pPar->aBlkOrig, nCopy);
}
for(ii=0; ii<nEntry; ii++){
u32 iBlk = bcvGetU32(&a[iOff]);
memcpy(&pDb->aBlkOrig[nNamesize*iBlk], &a[iOff+4], nNamesize);
iOff += 4 + nNamesize;
}
}
assert( iDb!=nDb-1 || iOff==n );
}else{
pDb->aBlkOrig = &a[iOff];
}
}
if( bDel==0 ){
pDb->aBlkLocal = pDb->aBlkOrig;
pDb->nBlkLocal = pDb->nBlkOrig;
}
pRet->nDb++;
}
if( pRet==0 ){
sqlite3_free(a);
sqlite3_free(zETag);
}
if( rc!=SQLITE_OK ){
bcvManifestDeref(pRet);
pRet = 0;
}
*ppOut = pRet;
return rc;
}
int bcvManifestParseCopy(
const u8 *a, int n,
const char *zETag,
Manifest **ppOut,
char **pz
){
int rc = SQLITE_OK;
u8 *aCopy = bcvMallocRc(&rc, n);
char *zECopy = bcvStrdupRc(&rc, zETag);
if( rc!=SQLITE_OK ){
sqlite3_free(aCopy);
sqlite3_free(zECopy);
*ppOut = 0;
*pz = 0;
}else{
memcpy(aCopy, a, n);
rc = bcvManifestParse(aCopy, n, zECopy, ppOut, pz);
}
return rc;
}
u8 *bcvManifestCompose(Manifest *p, int *pnOut){
int rc = SQLITE_OK; /* Return Code */
int iDb;
u8 *aOut = 0;
int nOut = 0;
int nAlloc = 0;
/* Write the db header */
nAlloc = 1024*1024;
aOut = sqlite3_malloc(nAlloc);
if( aOut==0 ){
rc = SQLITE_NOMEM;
}else{
bcvPutU32(&aOut[0], 4);
bcvPutU32(&aOut[4], p->szBlk);
bcvPutU32(&aOut[8], p->nDb);
bcvPutU32(&aOut[12], p->nDelBlk);
bcvPutU32(&aOut[16], p->nNamebytes);
bcvPutU32(&aOut[20], p->iMaxDbId);
}
/* Write the delete block array */
if( rc==SQLITE_OK ){
int iOff = MANIFEST_HDR_SIZE + p->nDb * MANIFEST_DBHDR_SIZE;
int nReq = (p->nNamebytes + sizeof(u64))*p->nDelBlk;
nOut = iOff + nReq;
if( nOut>nAlloc ){
u8 *aNew = 0;
while( nOut>nAlloc ) nAlloc = nAlloc*2;
aNew = sqlite3_realloc(aOut, nAlloc);
if( aNew==0 ){
rc = SQLITE_NOMEM;
}else{
aOut = aNew;
}
}
if( rc==SQLITE_OK && nReq>0 ){
memcpy(&aOut[iOff], p->aDelBlk, nReq);
}
}
/* Write the db header and array of blocks for each database. */
for(iDb=0; rc==SQLITE_OK && iDb<p->nDb; iDb++){
int nEntry = 0;
int iHdrOff = MANIFEST_HDR_SIZE + iDb*MANIFEST_DBHDR_SIZE;
ManifestDb *pDb = &p->aDb[iDb];
ManifestDb *pPar = 0;
int nMax;
int iDbOff = nOut;
u32 nBlkOrig = pDb->nBlkOrig | (pDb->nBlkLocal==0 ? 0x80000000 : 0);
if( pDb->iParentId ){
pPar = bcvManifestDbidToDb(p, pDb->iParentId);
}
nMax = nOut + pDb->nBlkOrig * (NAMEBYTES(p) + (pPar ? 4 : 0));
if( nMax>nAlloc ){
u8 *aNew = 0;
while( nMax>nAlloc ) nAlloc = nAlloc*2;
aNew = sqlite3_realloc(aOut, nAlloc);
if( aNew==0 ){
rc = SQLITE_NOMEM;
break;
}else{
aOut = aNew;
}
}
if( pPar ){
int iBlk;
for(iBlk=0; iBlk<pDb->nBlkOrig; iBlk++){
int iBOff = iBlk*NAMEBYTES(p);
if( iBlk>=pPar->nBlkOrig
|| memcmp(&pDb->aBlkOrig[iBOff], &pPar->aBlkOrig[iBOff], NAMEBYTES(p))
){
bcvPutU32(&aOut[nOut], iBlk);
memcpy(&aOut[nOut+4], &pDb->aBlkOrig[iBOff], NAMEBYTES(p));
nEntry++;
nOut += 4 + NAMEBYTES(p);
}
}
}else{
int nByte = NAMEBYTES(p) * pDb->nBlkOrig;
memcpy(&aOut[nOut], pDb->aBlkOrig, nByte);
nOut += nByte;
}
bcvPutU32(&aOut[iHdrOff+0], pDb->iDbId);
bcvPutU32(&aOut[iHdrOff+4], (pPar ? pDb->iParentId : 0));
bcvPutU32(&aOut[iHdrOff+8], pDb->iVersion);
bcvPutU32(&aOut[iHdrOff+12], iDbOff);
bcvPutU32(&aOut[iHdrOff+16], nBlkOrig);
bcvPutU32(&aOut[iHdrOff+20], nEntry);
memset(&aOut[iHdrOff+24], 0, 128);
memcpy(&aOut[iHdrOff+24], pDb->zDName, strlen(pDb->zDName));
}
if( rc!=SQLITE_OK ){
sqlite3_free(aOut);
nOut = 0;
aOut = 0;
}
*pnOut = nOut;
return aOut;
}
void bcvManifestFree(Manifest *p){
if( p ){
int i;
for(i=0; i<p->nDb; i++){
ManifestDb *pDb = &p->aDb[i];
if( pDb->nBlkOrigAlloc ) sqlite3_free(pDb->aBlkOrig);
if( pDb->nBlkLocalAlloc ) sqlite3_free(pDb->aBlkLocal);
}
if( p->bDelFree ){
sqlite3_free(p->aDelBlk);
}
sqlite3_free(p->pFree);
sqlite3_free(p->zETag);
sqlite3_free(p);
}
}
void bcvManifestDeref(Manifest *p){
if( p ){
p->nRef--;
assert( p->nRef>=0 );
if( p->nRef==0 ){
bcvManifestFree(p);
}
}
}
Manifest *bcvManifestRef(Manifest *p){
p->nRef++;
return p;
}
/*
** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
** Otherwise, it reallocates the buffer used by (*pMan) so that
** (*pMan)->aDb is large enough for nExtra more entries. If successful,
** (*pMan) points to the new manifest object after this function returns.
** Or, if the reallocation fails, then (*pMan) is left unchanged and (*pRc)
** set to SQLITE_NOMEM.
*/
void bcvManifestExpand(int *pRc, Manifest **ppMan, int nExtra){
Manifest *pMan = *ppMan;
Manifest *pNew = 0;
int nByte = sizeof(Manifest) + (pMan->nDb * sizeof(ManifestDb));
assert( pMan->aDb==(ManifestDb*)&pMan[1] );
pNew = (Manifest*)bcvMallocRc(pRc, nByte + nExtra*sizeof(ManifestDb));
if( pNew ){
memcpy(pNew, pMan, nByte);
sqlite3_free(pMan);
*ppMan = pNew;
pNew->aDb = (ManifestDb*)&pNew[1];
}
}
/*
** Make a copy of the Manifest object passed as the only argument.
*/
int bcvManifestDup(Manifest *p, Manifest **ppNew){
Manifest *pNew = 0;
int nByte;
int rc = SQLITE_OK;
nByte = sizeof(Manifest) + (p->nDb+1) * sizeof(ManifestDb);
pNew = (Manifest*)bcvMallocRc(&rc, nByte);
if( pNew ){
int i;
pNew->nDb = p->nDb;
pNew->szBlk = p->szBlk;
pNew->nNamebytes = p->nNamebytes;
pNew->iMaxDbId = p->iMaxDbId;
pNew->aDb = (ManifestDb*)&pNew[1];
for(i=0; rc==SQLITE_OK && i<pNew->nDb; i++){
ManifestDb *pNewDb = &pNew->aDb[i];
ManifestDb *pOldDb = &p->aDb[i];
pNewDb->iDbId = pOldDb->iDbId;
pNewDb->iParentId = pOldDb->iParentId;
memcpy(pNewDb->zDName, pOldDb->zDName, BCV_DBNAME_SIZE);
assert( pOldDb->nBlkLocalAlloc>0
|| pOldDb->aBlkLocal==0
|| pOldDb->aBlkLocal==pOldDb->aBlkOrig
);
pNewDb->nBlkOrig = pOldDb->nBlkOrig;
if( pOldDb->nBlkOrig>0 ){
int nByte = pOldDb->nBlkOrig*NAMEBYTES(p);
pNewDb->aBlkOrig = (u8*)bcvMallocRc(&rc, nByte);
if( rc==SQLITE_OK ){
memcpy(pNewDb->aBlkOrig, pOldDb->aBlkOrig, nByte);
pNewDb->nBlkOrigAlloc = pNewDb->nBlkOrig = pOldDb->nBlkOrig;
}
}
if( pOldDb->nBlkLocalAlloc ){
int nByte = pOldDb->nBlkLocal*NAMEBYTES(p);
pNewDb->aBlkLocal = (u8*)bcvMallocRc(&rc, nByte);
if( rc==SQLITE_OK ){
memcpy(pNewDb->aBlkLocal, pOldDb->aBlkLocal, nByte);
pNewDb->nBlkLocal = pNewDb->nBlkLocalAlloc = pOldDb->nBlkLocal;
}
}else if( pOldDb->nBlkLocal>0 ){
pNewDb->nBlkLocal = pNewDb->nBlkOrig;
pNewDb->aBlkLocal = pNewDb->aBlkOrig;
}
pNewDb->iVersion = pOldDb->iVersion;
}
if( p->nDelBlk ){
nByte = p->nDelBlk * GCENTRYBYTES(p);
pNew->aDelBlk = (u8*)bcvMallocRc(&rc, nByte);
if( rc==SQLITE_OK ){
memcpy(pNew->aDelBlk, p->aDelBlk, nByte);
pNew->nDelBlk = p->nDelBlk;
pNew->bDelFree = 1;
}
}
pNew->zETag = bcvStrdupRc(&rc, p->zETag);
pNew->nRef = 1;
if( rc!=SQLITE_OK ){
bcvManifestFree(pNew);
pNew = 0;
}
}
*ppNew = pNew;
return rc;
}
typedef struct BcvFetchParsed BcvFetchParsed;
struct BcvFetchParsed {
BcvBuffer buf;
char *zETag;
int rc;
};
static void xFetchParsed(
void *pApp,
int rc,
char *zETag,
const u8 *aData,
int nData,
const u8 *aHdrs, int nHdrs
){
BcvFetchParsed *p = (BcvFetchParsed*)pApp;
if( rc!=SQLITE_OK ){
assert( aData==0 && nData==0 );
p->rc = rc;
p->zETag = bcvMprintf("download manifest failed (%d) - %s", rc, zETag);
}else if( nData>0 ){
bcvBufferAppendRc(&p->rc, &p->buf, aData, nData);
p->zETag = bcvStrdupRc(&p->rc, zETag);
}
}
static int bcvManifestFetchParsed(sqlite3_bcv *pBcv, Manifest **ppOut){
BcvContainer *p = pBcv->pCont;
int rc = SQLITE_OK;
BcvFetchParsed dl;
memset(&dl, 0, sizeof(dl));
rc = bcvDispatchFetch(pBcv->pDisp, p, BCV_MANIFEST_FILE,0,0,&dl,xFetchParsed);