Skip to content

Commit 0b58382

Browse files
committed
cevfs_build() fixes.
Add makeDefault param to cevfs_create_vfs(). Remove `static` so we can use reuse fileTail elsewhere. cevfs_example refactor.
1 parent 856ab0a commit 0b58382

File tree

3 files changed

+47
-30
lines changed

3 files changed

+47
-30
lines changed

cevfs/cevfs.c

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ static int cevfsLoadHeader(cevfs_file *p);
212212
** /home/drh/xyzzy.txt -> xyzzy.txt
213213
** xyzzy.txt -> xyzzy.txt
214214
*/
215-
static const char *fileTail(const char *z){
215+
const char *fileTail(const char *z){
216216
int i;
217217
if( z==0 ) return 0;
218218
i = (int)strlen(z)-1;
@@ -985,15 +985,19 @@ static int cevfsRead(
985985
if( bSuccess ){
986986
if( p->bCompressionEnabled ){
987987
bSuccess = p->vfsMethods.xUncompress(pInfo->pCtx, pUncBuf, &iDstAmt, pCmpBuf, (int)uCmpPgSz);
988-
if( !bSuccess ) return CEVFS_ERROR_DECOMPRESSION_FAILED;
988+
if( !bSuccess ){
989+
return CEVFS_ERROR_DECOMPRESSION_FAILED;
990+
}
989991
assert( iDstAmt==uppPgSz );
990992
}else{
991993
pUncBuf = pCmpBuf;
992994
}
993995
sqlite3_free(pCmpBuf);
994996
u16 uBufOfst = iOfst % uppPgSz;
995997
memcpy(zBuf, pUncBuf+uBufOfst, iAmt);
996-
} else rc = CEVFS_ERROR_DECRYPTION_FAILED;
998+
}else{
999+
rc = CEVFS_ERROR_DECRYPTION_FAILED;
1000+
}
9971001
sqlite3_free(pUncBuf);
9981002
}else rc = SQLITE_NOMEM;
9991003
sqlite3PagerUnref(pPage);
@@ -1090,7 +1094,9 @@ static int cevfsWrite(
10901094
sqlite3PagerUnref(pPage);
10911095
}
10921096
sqlite3_free(pIvEncBuf);
1093-
}else rc = CEVFS_ERROR_ENCRYPTION_FAILED;
1097+
}else{
1098+
rc = CEVFS_ERROR_ENCRYPTION_FAILED;
1099+
}
10941100
sqlite3_free(pCmpBuf);
10951101
}else rc = SQLITE_NOMEM;
10961102
}else rc = SQLITE_READONLY;
@@ -1674,7 +1680,8 @@ int cevfs_create_vfs(
16741680
char const *zName, // Name of the newly constructed VFS.
16751681
char const *zParent, // Name of the underlying VFS. NULL to use default.
16761682
void *pCtx, // Context pointer to be passed to CEVFS methods.
1677-
t_xAutoDetect xAutoDetect // Pointer to xAutoDetect custom supplied function.
1683+
t_xAutoDetect xAutoDetect, // Pointer to xAutoDetect custom supplied function.
1684+
int makeDefault
16781685
){
16791686
sqlite3_vfs *pNew;
16801687
sqlite3_vfs *pRoot;
@@ -1736,7 +1743,7 @@ int cevfs_create_vfs(
17361743
pInfo->xAutoDetect = xAutoDetect;
17371744

17381745
CEVFS_PRINTF(pInfo, "%s.enabled_for(\"%s\")\n", pInfo->zVfsName, pRoot->zName);
1739-
return sqlite3_vfs_register(pNew, 0);
1746+
return sqlite3_vfs_register(pNew, makeDefault);
17401747
}
17411748

17421749
int cevfs_destroy_vfs(const char *zName){
@@ -1750,26 +1757,33 @@ int cevfs_destroy_vfs(const char *zName){
17501757
}
17511758

17521759
int cevfs_build(
1753-
const char *srcDbPath,
1754-
const char *destUri,
1755-
void *pCtx, // Context pointer to be passed to CEVFS methods.
1756-
t_xAutoDetect xAutoDetect // Pointer to xAutoDetect custom supplied function.
1760+
const char *zSrcFilename,
1761+
const char *zDestFilename,
1762+
const char *vfsName,
1763+
void *pCtx,
1764+
t_xAutoDetect xAutoDetect
17571765
){
1758-
int rc = SQLITE_ERROR;
1766+
int rc = SQLITE_OK;
17591767
unsigned char zDbHeader[100];
1768+
sqlite3_vfs *pDestVfs = NULL;
1769+
1770+
// cevfs_create_vfs must be done early enough to avoid SQLITE_MISUSE error
1771+
rc = cevfs_create_vfs(vfsName, NULL, pCtx, xAutoDetect, 0);
1772+
if( rc==SQLITE_OK || rc==CEVFS_ERROR_VFS_ALREADY_EXISTS ){
1773+
pDestVfs = sqlite3_vfs_find(vfsName);
1774+
}
17601775

1761-
// _cevfs_register must be done early enough to avoid SQLITE_MISUSE error
1762-
if( (rc = cevfs_create_vfs("cevfs-build", NULL, pCtx, xAutoDetect))==SQLITE_OK ){
1763-
sqlite3_vfs *pVfs = sqlite3_vfs_find(NULL);
1764-
if( pVfs ){
1776+
if( pDestVfs ){
1777+
sqlite3_vfs *pSrcVfs = sqlite3_vfs_find(NULL);
1778+
if( pSrcVfs ){
17651779
Pager *pPager;
17661780
int vfsFlags = SQLITE_OPEN_READONLY | SQLITE_OPEN_MAIN_DB | SQLITE_OPEN_URI;
1767-
if( (rc = sqlite3PagerOpen(pVfs, &pPager, srcDbPath, EXTRA_SIZE, 0, vfsFlags, cevfsPageReinit))==SQLITE_OK ){
1781+
if( (rc = sqlite3PagerOpen(pSrcVfs, &pPager, zSrcFilename, EXTRA_SIZE, 0, vfsFlags, cevfsPageReinit))==SQLITE_OK ){
17681782
sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
17691783
if( (rc = sqlite3PagerReadFileheader(pPager,sizeof(zDbHeader),zDbHeader)) == SQLITE_OK ){
17701784
u32 pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
17711785
if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE // validate range
1772-
&& ((pageSize-1)&pageSize)==0 ){ // validate page size is a power of 2
1786+
&& ((pageSize-1)&pageSize)==0 ){ // validate page size is a power of 2
17731787
u8 nReserve = zDbHeader[20];
17741788
if( (rc = sqlite3PagerSetPagesize(pPager, &pageSize, nReserve)) == SQLITE_OK ){
17751789
u32 fileChangeCounter = sqlite3Get4byte(zDbHeader+24);
@@ -1779,7 +1793,7 @@ int cevfs_build(
17791793
// If we didn't get page count, figure it out from the file size
17801794
if( !(pageCount>0 && fileChangeCounter==versionValidForNumber) ){
17811795
struct stat st;
1782-
if( stat(srcDbPath, &st)==0 ){
1796+
if( stat(zSrcFilename, &st)==0 ){
17831797
off_t size = st.st_size;
17841798
pageCount = (u32)(size/pageSize);
17851799
}
@@ -1789,9 +1803,10 @@ int cevfs_build(
17891803
if( rc==SQLITE_OK && (rc = sqlite3PagerSharedLock(pPager))==SQLITE_OK ){
17901804
// get destination ready to receive data
17911805
sqlite3 *pDb;
1792-
if( (rc = sqlite3_open_v2(destUri, &pDb, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "cevfs-build"))==SQLITE_OK ){
1793-
sqlite3_vfs *pVfs = sqlite3_vfs_find("cevfs-build");
1794-
cevfs_info *pInfo = (cevfs_info *)pVfs->pAppData;
1806+
1807+
if( (rc = sqlite3_open_v2(zDestFilename, &pDb, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, vfsName))==SQLITE_OK ){
1808+
cevfs_info *pInfo = (cevfs_info *)pDestVfs->pAppData;
1809+
17951810
DbPage *pPage1 = NULL;
17961811
// import all pages
17971812
for(Pgno i=0; i<pageCount; i++){

cevfs/cevfs.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323

2424
struct CevfsMethods {
2525
void *pCtx;
26-
26+
2727
int (*xCompressBound)(void *pCtx, size_t nDataInSize);
2828
int (*xCompress) (void *pCtx, char *aDest, size_t *pnDataOutSize, char *aSrc, size_t nDataInSize);
2929
int (*xUncompress)(void *pCtx, char *aDest, size_t *pnDataOutSize, char *aSrc, size_t nDataInSize);
30-
30+
3131
int (*xEncrypt)(
3232
void *pCtx, // in: the context
3333
const void *pDataIn, // in: the unencrypted data
@@ -37,7 +37,7 @@ struct CevfsMethods {
3737
size_t *nDataSizeOut, // out: size of encrypted data
3838
void *sqlite3_malloc(int n) // in: pointer to the sqlite3_malloc function
3939
);
40-
40+
4141
int (*xDecrypt)(
4242
void *pCtx,
4343
const void *pDataIn,
@@ -56,7 +56,8 @@ int cevfs_create_vfs(
5656
char const *zName, // Name of the newly constructed VFS.
5757
char const *zParent, // Name of the underlying VFS. NULL to use default.
5858
void *pCtx, // Context pointer to be passed to CEVFS methods.
59-
t_xAutoDetect
59+
t_xAutoDetect, // xAutoDetect method to set up xMethods.
60+
int makeDefault // BOOL: Make this the default VFS? Typically false.
6061
);
6162

6263
int cevfs_set_vfs_key(const char *zName, const char *pExpr);
@@ -70,6 +71,7 @@ int cevfs_destroy_vfs(const char *zName);
7071
int cevfs_build(
7172
const char *zSrcFilename, // Source SQLite DB filename, including path. Can be a URI.
7273
const char *zDestFilename, // Destination SQLite DB filename, including path. Can be a URI.
74+
const char *vfsName, // This will be embedded into the header of the database file.
7375
void *pCtx, // Context pointer to be passed to CEVFS xMethods.
7476
t_xAutoDetect // xAutoDetect method to set up xMethods.
7577
);

cevfs_example/main.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,11 @@ int main(int argc, const char * argv[]) {
260260
{ 0x71, 0xD8, 0x6C, 0x32, 0x99, 0xCA, 0x29, 0x2A }
261261
};
262262

263-
ctx.pKey = keyBytes; // 32-bit encryption hex key
264-
ctx.nKeySz = 32; // key size in bytes
265-
ctx.nIvSz = kCCBlockSizeAES128; // size of IV in bytes
263+
ctx.pKey = keyBytes; // 32-bit encryption hex key
264+
ctx.nKeySz = kCCKeySizeAES256; // key size in bytes
265+
ctx.nIvSz = kCCBlockSizeAES128; // size of IV in bytes
266266

267-
if( (rc = cevfs_create_vfs(VFS_NAME, NULL, &ctx, cevfsAutoDetect))==SQLITE_OK ){
267+
if( (rc = cevfs_create_vfs(VFS_NAME, NULL, &ctx, cevfsAutoDetect, 0))==SQLITE_OK ){
268268
rc = SQLITE_OK;
269269

270270
#if SQL_TEST_WRITE
@@ -286,6 +286,6 @@ int main(int argc, const char * argv[]) {
286286
fputs("Done\n", stdout);
287287
}
288288
fclose(f);
289-
cevfs_destroy_vfs("cevfs");
289+
cevfs_destroy_vfs(VFS_NAME);
290290
return 0;
291291
}

0 commit comments

Comments
 (0)