Skip to content

Commit 5e3114e

Browse files
committed
Support read-only mode.
1 parent db9dadc commit 5e3114e

File tree

1 file changed

+41
-36
lines changed

1 file changed

+41
-36
lines changed

ceshim.c

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ struct ceshim_info {
119119

120120
// bools
121121
u8 bPgMapDirty:1; // Curr page map needs to be persisted
122+
u8 bReadOnly:1; // True when db was open for read-only
122123
};
123124

124125
/*
@@ -381,6 +382,7 @@ static int ceshimSavePagemapData(ceshim_file *p){
381382
static int ceshimSaveMMTbl(ceshim_file *p){
382383
int rc;
383384
ceshim_info *pInfo = p->pInfo;
385+
assert( pInfo->bReadOnly==0 );
384386
ceshim_header *header = &pInfo->ceshimHeader;
385387
int memSz = header->mmTblMaxCnt*sizeof(CeshimMMTblEntry);
386388
CeshimMMTblEntry *buf = sqlite3_malloc(memSz);
@@ -520,7 +522,9 @@ static int ceshimWriteUncompressed(
520522
}
521523

522524
static int ceshimSaveHeader(ceshim_file *p){
523-
ceshim_header *header = &p->pInfo->ceshimHeader;
525+
ceshim_info *pInfo = p->pInfo;
526+
assert( pInfo->bReadOnly==0 );
527+
ceshim_header *header = &pInfo->ceshimHeader;
524528
u8 buf[CESHIM_DB_HEADER2_SZ];
525529
memcpy(buf, &header->schema, 1);
526530
put4byte(buf+1, header->currPgno);
@@ -834,45 +838,45 @@ static int ceshimClose(sqlite3_file *pFile){
834838
ceshim_printf(pInfo, "%s.xClose(%s)", pInfo->zVfsName, p->zFName);
835839

836840
if( p->pPager ){
837-
// save pager counts
838-
839-
int nPageFile = 0; /* Number of pages in the database file */
840-
sqlite3PagerPagecount(p->pPager, &nPageFile);
841-
assert( pInfo->lwrPageFile==nPageFile );
841+
if( !pInfo->bReadOnly ){
842+
int nPageFile = 0; /* Number of pages in the database file */
843+
sqlite3PagerPagecount(p->pPager, &nPageFile);
844+
assert( pInfo->lwrPageFile==nPageFile );
842845

843-
u8 buf[4];
844-
sqlite3Put4byte(buf, pInfo->lwrPageFile);
845-
rc = ceshimWriteUncompressed(p, 1, 28, buf, 4);
846-
rc = ceshimSaveHeader(p);
846+
u8 buf[4];
847+
sqlite3Put4byte(buf, pInfo->lwrPageFile);
848+
rc = ceshimWriteUncompressed(p, 1, 28, buf, 4);
849+
rc = ceshimSaveHeader(p);
847850

848-
if( (rc = ceshimSaveMMTbl(p))==SQLITE_OK ){
849-
for(int i=0; i<p->nTransactions; i++){
850-
if( (rc = sqlite3PagerCommitPhaseOne(p->pPager, NULL, 0))==SQLITE_OK ){
851-
sqlite3PagerCommitPhaseTwo(p->pPager);
851+
if( (rc = ceshimSaveMMTbl(p))==SQLITE_OK ){
852+
for(int i=0; i<p->nTransactions; i++){
853+
if( (rc = sqlite3PagerCommitPhaseOne(p->pPager, NULL, 0))==SQLITE_OK ){
854+
sqlite3PagerCommitPhaseTwo(p->pPager);
855+
}
852856
}
857+
p->nTransactions = 0;
853858
}
854-
p->nTransactions = 0;
855-
856-
if( rc==SQLITE_OK ){
857-
ceshimReleasePage1(p);
858-
if( (rc = sqlite3PagerClose(p->pPager))==SQLITE_OK ){
859-
p->pPager = NULL;
860-
if( pInfo->zUppJournalPath ){
861-
sqlite3_free(pInfo->zUppJournalPath);
862-
pInfo->zUppJournalPath = NULL;
863-
}
864-
if( pInfo->mmTbl ){
865-
sqlite3_free(pInfo->mmTbl);
866-
pInfo->mmTbl = NULL;
867-
}
868-
if( pInfo->pPgMap ){
869-
sqlite3_free(pInfo->pPgMap);
870-
pInfo->pPgMap = NULL;
871-
}
872-
if( pInfo->pBigEndianPgMap ){
873-
sqlite3_free(pInfo->pBigEndianPgMap);
874-
pInfo->pBigEndianPgMap = NULL;
875-
}
859+
}
860+
861+
if( rc==SQLITE_OK ){
862+
ceshimReleasePage1(p);
863+
if( (rc = sqlite3PagerClose(p->pPager))==SQLITE_OK ){
864+
p->pPager = NULL;
865+
if( pInfo->zUppJournalPath ){
866+
sqlite3_free(pInfo->zUppJournalPath);
867+
pInfo->zUppJournalPath = NULL;
868+
}
869+
if( pInfo->mmTbl ){
870+
sqlite3_free(pInfo->mmTbl);
871+
pInfo->mmTbl = NULL;
872+
}
873+
if( pInfo->pPgMap ){
874+
sqlite3_free(pInfo->pPgMap);
875+
pInfo->pPgMap = NULL;
876+
}
877+
if( pInfo->pBigEndianPgMap ){
878+
sqlite3_free(pInfo->pBigEndianPgMap);
879+
pInfo->pBigEndianPgMap = NULL;
876880
}
877881
}
878882
}
@@ -1317,6 +1321,7 @@ static int ceshimOpen(
13171321
}
13181322

13191323
// open file
1324+
if( flags & SQLITE_OPEN_READONLY ) pInfo->bReadOnly = 1;
13201325
rc = pRoot->xOpen(pRoot, zName, p->pReal, flags, pOutFlags);
13211326
ceshim_printf(pInfo, "%s.xOpen(%s,flags=0x%x)",pInfo->zVfsName, p->zFName, flags);
13221327

0 commit comments

Comments
 (0)