0
0
mirror of https://github.com/tursodatabase/libsql.git synced 2025-06-17 11:29:49 +00:00

pager,wal: first batch of hooks to integrate encryption

Based on https://gist.github.com/psarna/0dbd95424fb7121bb54bf5f961e1e117
from SQLite3MultipleCiphers, let's try and make libsql flexible enough
to not require patching the amalgamation files at all in order to integrate
with SQLite3Multiple ciphers.

For starters, their codec functions are instead declared as libsql_pager_codec
and libsql_pager_has_codec. The idea for future integration is that
if an appropriate compilation flag is on, we assume that the code will be compiled
with additional symbols that provide all the implementation we need, e.g.
libsql_pager_codec_impl() will just transiently call sqlite3mcPagerCodec().
This commit is contained in:
Piotr Sarna
2024-01-02 16:25:52 +01:00
parent 52bb6cc706
commit 379b227587
2 changed files with 26 additions and 3 deletions
libsql-sqlite3/src

@ -414,7 +414,6 @@ int sqlite3PagerTrace=1; /* True to enable tracing */
*/
#define MAX_SECTOR_SIZE 0x10000
/*
** An instance of the following structure is allocated for each active
** savepoint and statement transaction in the system. All such structures
@ -706,6 +705,26 @@ struct Pager {
#endif
};
/* libSQL extension: pager codec */
int libsql_pager_has_codec(struct Pager *_p) {
#ifdef LIBSQL_CUSTOM_PAGER_CODEC
return libsql_pager_has_codec_impl(_p);
#else
return 0;
#endif
}
void *libsql_pager_codec(libsql_pghdr *hdr) {
#ifdef LIBSQL_CUSTOM_PAGER_CODEC
return libsql_pager_codec_impl(hdr);
#else
return hdr->pData;
#endif
}
/* end of libSQL extension: pager codec */
/*
** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS
@ -819,6 +838,7 @@ static const unsigned char aJournalMagic[] = {
int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
if( pPager->fd->pMethods==0 ) return 0;
if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
if( libsql_pager_has_codec(pPager) != 0 ) return 0;
#ifndef SQLITE_OMIT_WAL
if( pagerUseWal(pPager) ){
u32 iRead = 0;
@ -1052,7 +1072,7 @@ static void setGetterMethod(Pager *pPager){
if( pPager->errCode ){
pPager->xGet = getPageError;
#if SQLITE_MAX_MMAP_SIZE>0
}else if( USEFETCH(pPager) ){
}else if( USEFETCH(pPager) && libsql_pager_has_codec(pPager) == 0 ){
pPager->xGet = getPageMMap;
#endif /* SQLITE_MAX_MMAP_SIZE>0 */
}else{

@ -252,6 +252,8 @@
#include "wal.h"
void *libsql_pager_codec(libsql_pghdr *p);
typedef libsql_pghdr PgHdr;
typedef sqlite3_wal Wal;
@ -3492,6 +3494,7 @@ static int walWriteOneFrame(
void *pData; /* Data actually written */
u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
pData = pPage->pData;
if( (pData = libsql_pager_codec(pPage))==0 ) return SQLITE_NOMEM_BKPT;
walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset);
if( rc ) return rc;
@ -3676,7 +3679,7 @@ static int walFrames(
if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
pWal->iReCksum = iWrite;
}
pData = p->pData;
if( (pData = libsql_pager_codec(p))==0 ) return SQLITE_NOMEM;
rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
if( rc ) return rc;
p->flags &= ~PGHDR_WAL_APPEND;