Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions libsql-ffi/bundled/SQLite3MultipleCiphers/src/codecext.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
*/

/*
** Forward declaration for pager codec cache update function.
** This should be called after encryption is added, removed, or changed.
** Forward declaration for db codec check function.
** Used to update cached codec status after encryption changes.
*/
void libsql_pager_update_codec_cache(struct Pager *pPager);
int libsql_db_has_codec(sqlite3_vfs* pVfs, const char* zFilename);

/*
** "Special" version of function sqlite3BtreeSetPageSize
Expand Down Expand Up @@ -591,8 +591,8 @@ sqlite3_rekey_v2(sqlite3* db, const char* zDbName, const void* zKey, int nKey)
{
sqlite3mcSetIsEncrypted(codec, 0);
}
/* Update the pager's cached codec status after changing encryption */
libsql_pager_update_codec_cache(pPager);
/* Update both pager and database cached codec status after changing encryption */
db->aDb[dbIndex].hasCodec = pPager->hasCodec = libsql_db_has_codec(pPager->pVfs, pPager->zFilename);
}
else
{
Expand Down
68 changes: 45 additions & 23 deletions libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -16436,7 +16436,8 @@ SQLITE_PRIVATE int sqlite3PagerOpen(
int,
int,
int,
void(*)(DbPage*)
void(*)(DbPage*),
int /* hasCodec from connection-level cache */
);
SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3*);
SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
Expand Down Expand Up @@ -16625,7 +16626,8 @@ SQLITE_PRIVATE int sqlite3BtreeOpen(
sqlite3 *db, /* Associated database connection */
Btree **ppBtree, /* Return open Btree* here */
int flags, /* Flags */
int vfsFlags /* Flags passed through to VFS open */
int vfsFlags, /* Flags passed through to VFS open */
int hasCodec /* True if this database has encryption codec */
);

/* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
Expand Down Expand Up @@ -17993,6 +17995,7 @@ struct Db {
Btree *pBt; /* The B*Tree structure for this database file */
u8 safety_level; /* How aggressive at syncing data to disk */
u8 bSyncSet; /* True if "PRAGMA synchronous=N" has been run */
u8 hasCodec; /* True if this database has encryption codec */
Schema *pSchema; /* Pointer to database schema (possibly shared) */
};

Expand Down Expand Up @@ -58232,27 +58235,18 @@ struct Pager {
/* libSQL extension: pager codec */

#ifdef LIBSQL_CUSTOM_PAGER_CODEC
int libsql_pager_has_codec_impl(struct Pager *_p);
int libsql_pager_codec_impl(libsql_pghdr *hdr, void **ret);
int libsql_db_has_codec(sqlite3_vfs* pVfs, const char* zFilename);
#endif

int libsql_pager_has_codec(struct Pager *_p) {
#ifdef LIBSQL_CUSTOM_PAGER_CODEC
return libsql_pager_has_codec_impl(_p);
return libsql_db_has_codec(_p->pVfs, _p->zFilename);
#else
return 0;
#endif
}

/*
** Update the cached codec status.
** This should be called after encryption is added, removed, or changed
** via sqlite3_rekey_v2() to ensure the cached hasCodec value is correct.
*/
void libsql_pager_update_codec_cache(struct Pager *pPager) {
pPager->hasCodec = libsql_pager_has_codec(pPager);
}

int libsql_pager_codec(libsql_pghdr *hdr, void **ret) {
if (!ret) {
return SQLITE_MISUSE_BKPT;
Expand Down Expand Up @@ -62295,7 +62289,8 @@ SQLITE_PRIVATE int sqlite3PagerOpen(
int nExtra, /* Extra bytes append to each in-memory page */
int flags, /* flags controlling this file */
int vfsFlags, /* flags passed through to sqlite3_vfs.xOpen() */
void (*xReinit)(DbPage*) /* Function to reinitialize pages */
void (*xReinit)(DbPage*),/* Function to reinitialize pages */
int dbHasCodec /* hasCodec from connection-level cache */
){
u8 *pPtr;
Pager *pPager = 0; /* Pager object to allocate and return */
Expand Down Expand Up @@ -62635,8 +62630,9 @@ SQLITE_PRIVATE int sqlite3PagerOpen(
/* pPager->xBusyHandler = 0; */
/* pPager->pBusyHandlerArg = 0; */
pPager->xReiniter = xReinit;
/* Cache the codec check result to avoid expensive VFS stack traversal on every page read */
pPager->hasCodec = libsql_pager_has_codec(pPager);
/* Use cached codec status from connection level to avoid expensive VFS stack traversal
** and file lookup on every pager initialization */
pPager->hasCodec = dbHasCodec;
setGetterMethod(pPager);
/* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
/* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
Expand Down Expand Up @@ -73757,7 +73753,8 @@ SQLITE_PRIVATE int sqlite3BtreeOpen(
sqlite3 *db, /* Associated database handle */
Btree **ppBtree, /* Pointer to new Btree object written here */
int flags, /* Options */
int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
int vfsFlags, /* Flags passed through to sqlite3_vfs.xOpen() */
int hasCodec /* True if this database has encryption codec */
){
BtShared *pBt = 0; /* Shared part of btree structure */
Btree *p; /* Handle to return */
Expand Down Expand Up @@ -73902,7 +73899,7 @@ SQLITE_PRIVATE int sqlite3BtreeOpen(
goto btree_open_out;
}
rc = sqlite3PagerOpen(pVfs, db->wal_manager ,&pBt->pPager, zFilename,
sizeof(MemPage), flags, vfsFlags, pageReinit);
sizeof(MemPage), flags, vfsFlags, pageReinit, hasCodec);
if( rc==SQLITE_OK ){
sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
Expand Down Expand Up @@ -98769,7 +98766,8 @@ case OP_OpenEphemeral: { /* ncycle */
pCx->isEphemeral = 1;
rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->ub.pBtx,
BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
vfsFlags);
vfsFlags,
0); /* Ephemeral tables are never encrypted */
if( rc==SQLITE_OK ){
rc = sqlite3BtreeBeginTrans(pCx->ub.pBtx, 1, 0);
if( rc==SQLITE_OK ){
Expand Down Expand Up @@ -121888,6 +121886,10 @@ int libsql_handle_extra_attach_params(sqlite3* db, const char* zName, const char
SQLITE_PRIVATE int sqlite3mcHandleAttachKey(sqlite3*, const char*, const char*, sqlite3_value*, char**);
#endif

#ifdef LIBSQL_CUSTOM_PAGER_CODEC
int libsql_db_has_codec(sqlite3_vfs* pVfs, const char* zFilename);
#endif

/*
** An SQL user-function registered to do the work of an ATTACH statement. The
** three arguments to the function come directly from an attach statement:
Expand Down Expand Up @@ -121940,7 +121942,8 @@ static void attachFunc(
Btree *pNewBt = 0;
pVfs = sqlite3_vfs_find("memdb");
if( pVfs==0 ) return;
rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNewBt, 0, SQLITE_OPEN_MAIN_DB);
rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNewBt, 0, SQLITE_OPEN_MAIN_DB,
0); /* Memdb databases are never encrypted */
if( rc==SQLITE_OK ){
Schema *pNewSchema = sqlite3SchemaGet(db, pNewBt);
if( pNewSchema ){
Expand Down Expand Up @@ -122009,7 +122012,13 @@ static void attachFunc(
}
assert( pVfs );
flags |= SQLITE_OPEN_MAIN_DB;
rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
/* Check encryption status for this database file */
#ifdef LIBSQL_CUSTOM_PAGER_CODEC
pNew->hasCodec = libsql_db_has_codec(pVfs, zPath);
#else
pNew->hasCodec = 0;
#endif
rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags, pNew->hasCodec);
db->nDb++;
pNew->zDbSName = sqlite3DbStrDup(db, zName);
}
Expand Down Expand Up @@ -128047,7 +128056,8 @@ SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
SQLITE_OPEN_DELETEONCLOSE |
SQLITE_OPEN_TEMP_DB;

rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
db->aDb[1].hasCodec = 0; /* Temp databases are never encrypted */
rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags, db->aDb[1].hasCodec);
if( rc!=SQLITE_OK ){
sqlite3ErrorMsg(pParse, "unable to open a temporary database "
"file for storing temporary tables");
Expand Down Expand Up @@ -184304,6 +184314,10 @@ static const char *uriParameter(const char *zFilename, const char *zParam){
int libsql_handle_extra_uri_params(sqlite3 *db, const char *zOpen);
#endif

#ifdef LIBSQL_CUSTOM_PAGER_CODEC
int libsql_db_has_codec(sqlite3_vfs* pVfs, const char* zFilename);
#endif

/*
** This routine does the work of opening a database on behalf of
** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
Expand Down Expand Up @@ -184559,9 +184573,17 @@ static int openDatabase(
}
#endif

/* Cache encryption status at database open time to avoid expensive
** VFS stack traversal and file lookup on every pager initialization */
#ifdef LIBSQL_CUSTOM_PAGER_CODEC
db->aDb[0].hasCodec = libsql_db_has_codec(db->pVfs, zOpen);
#else
db->aDb[0].hasCodec = 0;
#endif

/* Open the backend database driver */
rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
flags | SQLITE_OPEN_MAIN_DB);
flags | SQLITE_OPEN_MAIN_DB, db->aDb[0].hasCodec);
if( rc!=SQLITE_OK ){
if( rc==SQLITE_IOERR_NOMEM ){
rc = SQLITE_NOMEM_BKPT;
Expand Down
13 changes: 9 additions & 4 deletions libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3mc_vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1347,13 +1347,18 @@ sqlite3mcCheckVfs(const char* zVfs)
return rc;
}

int libsql_pager_has_codec_impl(struct Pager* pPager)
/*
** Check if a database file is encrypted at connection open time.
** This checks both the VFS stack and the specific file encryption status.
** The result is cached at the connection level to avoid expensive checks
** on every pager initialization.
*/
int libsql_db_has_codec(sqlite3_vfs* pVfs, const char* zFilename)
{
int hasCodec = 0;
sqlite3mc_vfs* pVfsMC = NULL;
sqlite3_vfs* pVfs = pPager->pVfs;

/* Check whether the VFS stack of the pager contains a Multiple Ciphers VFS */
/* Check whether the VFS stack contains a Multiple Ciphers VFS */
for (; pVfs; pVfs = pVfs->pNext)
{
if (pVfs && pVfs->xOpen == mcVfsOpen)
Expand All @@ -1367,7 +1372,7 @@ int libsql_pager_has_codec_impl(struct Pager* pPager)
/* Check whether codec is enabled for associated database file */
if (pVfsMC)
{
sqlite3mc_file* mcFile = mcFindDbMainFileName(pVfsMC, pPager->zFilename);
sqlite3mc_file* mcFile = mcFindDbMainFileName(pVfsMC, zFilename);
if (mcFile)
{
Codec* codec = mcFile->codec;
Expand Down
Loading
Loading