Skip to content

Commit b20b3e9

Browse files
committed
Allow single VFS instance to handle journal file redirects for multiple simultaneously opened database files from upper pager.
1 parent 75e1c51 commit b20b3e9

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

ceshim.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,25 @@ static const char *fileTail(const char *z){
226226
** and not have additional '-' in name. We'll just append "btree" to distinguish it from ours.
227227
** Note: everything after the '-' must be alphanumeric only. No punctuation allowed
228228
** or an assertion will be triggered in debug mode.
229+
**
230+
** If we always had a pointer to the associated ceshim_file when processing a journal filename then
231+
** we could associate the renamed journal filename with the corresponding database filename and
232+
** encapsulate the memory management. Since we cannot, we use bMustRelease to assist with memory management.
233+
**
234+
** pFile or bMustRelease must be NULL. Both cannot be NULL.
235+
** If bMustRelease is true upon return, the newly created string must be freed by the caller.
236+
** Use sqlite3_free to free memory.
229237
*/
230238
static char * ceshimMapPath(ceshim_file *pFile, const char *zName, bool *bMustRelease){
239+
// Only one of pFile or bMustRelease MUST be NULL.
240+
assert( !(pFile && bMustRelease) && (pFile || bMustRelease) );
241+
231242
static const char *zTail = "btree";
232243
if (bMustRelease) *bMustRelease = false;
233-
if( strstr(zName, "-journal")==0 ) return zName;
244+
if( strstr(zName, "-journal")==0 ){
245+
bMustRelease = false;
246+
return (char *)zName;
247+
}
234248
char *zUppJournalPath = pFile ? pFile->zUppJournalPath : NULL;
235249
if( zUppJournalPath == NULL ){
236250
zUppJournalPath = sqlite3_malloc((int)(strlen(zName)+strlen(zTail))+1);
@@ -359,7 +373,6 @@ static int ceshimReadUncompressed(
359373
}
360374

361375
static void ceshimReleasePage1(ceshim_file *p){
362-
ceshim_info *pInfo = p->pInfo;
363376
if( p->pPage1 ){
364377
sqlite3PagerUnref(p->pPage1->pDbPage);
365378
p->pPage1 = NULL;
@@ -372,7 +385,6 @@ static void ceshimReleasePage1(ceshim_file *p){
372385
** Its size depends on the page size of the lower pager.
373386
*/
374387
static int ceshimCreateMMTbl(ceshim_file *p, int *memSzOut){
375-
ceshim_info *pInfo = p->pInfo;
376388
u16 maxSz = p->pageSize - CESHIM_DB_HEADER_SIZE;
377389
u16 maxEntries = maxSz / sizeof(CeshimMMTblEntry);
378390

@@ -392,7 +404,6 @@ static int ceshimCreateMMTbl(ceshim_file *p, int *memSzOut){
392404

393405
static int ceshimSavePagemapData(ceshim_file *p){
394406
int rc = SQLITE_OK;
395-
ceshim_info *pInfo = p->pInfo;
396407
if( p->bPgMapDirty ){
397408
Pgno pgno = p->mmTbl[p->mmTblCurrIx].lwrPgno;
398409
rc = ceshimWriteUncompressed(p, pgno, 0, p->pBigEndianPgMap, p->pgMapSz);
@@ -403,7 +414,6 @@ static int ceshimSavePagemapData(ceshim_file *p){
403414

404415
static int ceshimSaveMMTbl(ceshim_file *p){
405416
int rc;
406-
ceshim_info *pInfo = p->pInfo;
407417
assert( p->bReadOnly==0 );
408418
ceshim_header *header = &p->ceshimHeader;
409419
int memSz = header->mmTblMaxCnt*sizeof(CeshimMMTblEntry);
@@ -422,7 +432,6 @@ static int ceshimSaveMMTbl(ceshim_file *p){
422432

423433
static int ceshimLoadPagemapData(ceshim_file *p, u16 ix){
424434
int rc;
425-
ceshim_info *pInfo = p->pInfo;
426435
ceshim_header *header = &p->ceshimHeader;
427436
assert( p->bPgMapDirty==0 );
428437
assert( ix != p->mmTblCurrIx ); // mmTblCurrIx initially large number to mean no entries yet
@@ -443,7 +452,6 @@ static int ceshimLoadPagemapData(ceshim_file *p, u16 ix){
443452
static int ceshimLoadMMTbl(ceshim_file *p){
444453
int rc;
445454
int memSz;
446-
ceshim_info *pInfo = p->pInfo;
447455
ceshim_header *header = &p->ceshimHeader;
448456
assert( p->mmTbl==NULL );
449457

@@ -465,7 +473,6 @@ static int ceshimLoadMMTbl(ceshim_file *p){
465473
}
466474

467475
static int ceshimPagerLock(ceshim_file *p){
468-
ceshim_info *pInfo = p->pInfo;
469476
int rc;
470477
assert( p->pPage1==0 );
471478
if( (rc = sqlite3PagerSharedLock(p->pPager))==SQLITE_OK ){
@@ -549,7 +556,6 @@ static int ceshimWriteUncompressed(
549556
}
550557

551558
static int ceshimSaveHeader(ceshim_file *p){
552-
ceshim_info *pInfo = p->pInfo;
553559
assert( p->bReadOnly==0 );
554560
ceshim_header *header = &p->ceshimHeader;
555561
u8 buf[CESHIM_DB_HEADER2_SZ];
@@ -599,7 +605,6 @@ static int ceshimNewDatabase(ceshim_file *pFile){
599605
CeshimMemPage *pP1;
600606
unsigned char *data;
601607
int rc;
602-
ceshim_info *pInfo = pFile->pInfo;
603608

604609
pP1 = pFile->pPage1;
605610
data = pP1->aData;
@@ -667,7 +672,6 @@ static int ceshimPageMapGet(
667672
CeshimCmpSize *outCmpSz,
668673
u16 *outIx
669674
){
670-
ceshim_info *pInfo = pFile->pInfo;
671675
ceshim_header *header = &pFile->ceshimHeader;
672676
if( outUppPgno ) *outUppPgno = (Pgno)(uSrcOfst/header->uppPgSz+1);
673677
int currPgMapNo = ceshimSwitchPageMap(pFile, uSrcOfst);
@@ -716,7 +720,6 @@ void ceshimAllocCmpPageSpace(
716720
CeshimCmpSize cmpSz, // Current compressed size of data for allocation
717721
u16 pgMapIx // Index of map entry to record allocation data
718722
){
719-
ceshim_info *pInfo = pFile->pInfo;
720723
ceshim_header *header = &pFile->ceshimHeader;
721724
CeshimCmpOfst ofst = header->currPageOfst;
722725
ceshim_map_entry *pMapEntry = &pFile->pPgMap[pgMapIx];
@@ -753,7 +756,6 @@ int ceshimAddPageEntry(
753756
Pgno *outLwrPgno
754757
){
755758
assert( (!outCmpOfst && !outLwrPgno) || (outCmpOfst && outLwrPgno) );
756-
ceshim_info *pInfo = pFile->pInfo;
757759
ceshim_header *header = &pFile->ceshimHeader;
758760

759761
// if no more room, start a new pagemap
@@ -1225,7 +1227,6 @@ static int ceshimCheckReservedLock(sqlite3_file *pFile, int *pResOut){
12251227

12261228
static int ceshimPragma(sqlite3_file *pFile, const char *op, const char *arg){
12271229
ceshim_file *p = (ceshim_file *)pFile;
1228-
ceshim_info *pInfo = p->pInfo;
12291230
int rc = SQLITE_OK;
12301231
if( strcmp(op, "page_size")==0 ){
12311232
p->ceshimHeader.uppPgSz = (u32)sqlite3Atoi(arg);
@@ -1581,7 +1582,7 @@ static int ceshimAccess(
15811582
ceshim_info *pInfo = (ceshim_info*)pVfs->pAppData;
15821583
sqlite3_vfs *pRoot = pInfo->pRootVfs;
15831584
bool bMustRelease;
1584-
const char *zPath = ceshimMapPath(NULL, _zPath, &bMustRelease);
1585+
char *zPath = ceshimMapPath(NULL, _zPath, &bMustRelease);
15851586
int rc = SQLITE_OK;
15861587

15871588
CESHIM_PRINTF(pInfo, "%s.xAccess(\"%s\",%d)", pInfo->zVfsName, zPath, flags);

0 commit comments

Comments
 (0)