From d80e31ff73a95be73f252f7924e0b1d76d7a685e Mon Sep 17 00:00:00 2001 From: htt1997 Date: Tue, 22 Apr 2025 17:48:00 +0800 Subject: [PATCH 1/2] rekey support multiple process Signed-off-by: htt1997 --- ...08-Rekey-supports-multiple-processes.patch | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 patch/0008-Rekey-supports-multiple-processes.patch diff --git a/patch/0008-Rekey-supports-multiple-processes.patch b/patch/0008-Rekey-supports-multiple-processes.patch new file mode 100644 index 0000000..2d91c8b --- /dev/null +++ b/patch/0008-Rekey-supports-multiple-processes.patch @@ -0,0 +1,107 @@ +From 0f8f308a9a5884a5d5581008e9f7b018ea98b0fe Mon Sep 17 00:00:00 2001 +From: ht +Date: Tue, 22 Apr 2025 17:43:16 +0800 +Subject: [PATCH] Rekey supports multiple processes + +Signed-off-by: ht +Change-Id: I1892d85d1e34ed37e6659190e020839059b9822d +--- + src/sqlite3.c | 54 +++++++++++++++++++++++++++++++++++---------------- + 1 file changed, 37 insertions(+), 17 deletions(-) + +diff --git a/src/sqlite3.c b/src/sqlite3.c +index 4d96a1e..f70df81 100644 +--- a/src/sqlite3.c ++++ b/src/sqlite3.c +@@ -245765,7 +245765,11 @@ int sqlite3CodecAttach(sqlite3* db, int nDb, const void *pKey, int nKey){ + } + + sqlite3_mutex_leave(db->mutex); +- ++ rc = sqlite3BtreeBeginTrans(p, 0, 0); ++ if( rc!=SQLITE_OK ){ ++ return rc; ++ } ++ sqlite3BtreeCommit(p); + return SQLITE_OK; + } + +@@ -245803,6 +245807,21 @@ int sqlite3_key_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey){ + return sqlite3CodecAttach(db, iDb, pKey, nKey); + } + ++static inline u8 IsConnectionValidForCheck(Pager *pPager) ++{ ++#if SQLITE_OS_UNIX ++ unixFile *fd = (unixFile *)pPager->fd; ++ // unix and only one connection exist ++ if (fd == NULL || fd->pInode == NULL || pPager->pVfs == NULL || ++ sqlite3_stricmp(pPager->pVfs->zName, "unix") != 0 || fd->pInode->nRef != 1) { ++ return 0; ++ } ++ return 1; ++#else ++ return 0; ++#endif ++} ++ + int sqlite3_rekey(sqlite3 *db, const void *pKey, int nKey){ + return sqlite3_rekey_v2(db, "main", pKey, nKey); + } +@@ -245831,7 +245850,22 @@ int sqlite3_rekey_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey){ + return rc; + } + sqlite3_mutex_enter(db->mutex); +- (void)sqlite3BtreeBeginTrans(p, 1, 0); ++ rc = sqlite3BtreeBeginTrans(p, 1, 0); ++ if(rc != SQLITE_OK){ ++ sqlite3_mutex_leave(db->mutex); ++ return rc; ++ } ++ rc = unixShmSystemLock((unixFile *)pPager->fd, F_WRLCK, UNIX_SHM_DMS, 1); ++ if(rc != SQLITE_OK || !IsConnectionValidForCheck(pPager)){ ++ sqlite3_log(SQLITE_WARNING, "sqlite3_rekey_v2: error when lock errno = %d. IsConnectionValidForCheck", rc); ++ if(rc == SQLITE_OK) { ++ unixShmSystemLock((unixFile *)pPager->fd, F_RDLCK, UNIX_SHM_DMS, 1); ++ rc = SQLITE_BUSY; ++ } ++ (void)sqlite3BtreeRollback(p, SQLITE_ABORT_ROLLBACK, 0); ++ sqlite3_mutex_leave(db->mutex); ++ return rc; ++ } + sqlite3PagerPagecount(pPager, &pageCount); + // support hmac algo changed by using rekey operation + int oldHmacAlgo = ctx->writeCtx->codecConst.hmacAlgo; +@@ -245855,6 +245889,7 @@ int sqlite3_rekey_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey){ + } + } + } ++ unixShmSystemLock((unixFile *)pPager->fd, F_RDLCK, UNIX_SHM_DMS, 1); + if(rc == SQLITE_OK){ + (void)sqlite3BtreeCommit(p); + sqlite3CodecFreeKeyContext(ctx->readCtx); +@@ -247210,21 +247245,6 @@ CHK_RESTORE_OUT: + return rc; + } + +-static inline u8 IsConnectionValidForCheck(Pager *pPager) +-{ +-#if SQLITE_OS_UNIX +- unixFile *fd = (unixFile *)pPager->fd; +- // unix and only one connection exist +- if (fd == NULL || fd->pInode == NULL || pPager->pVfs == NULL || +- sqlite3_stricmp(pPager->pVfs->zName, "unix") != 0 || fd->pInode->nRef != 1) { +- return 0; +- } +- return 1; +-#else +- return 0; +-#endif +-} +- + static int MetaDwrOpenAndCheck(Btree *pBt) + { + Pager *pPager = pBt->pBt->pPager; +-- +2.17.1 + -- Gitee From 5e9a2196958ad302e111a2a8f9a1ec9dc0d9e58f Mon Sep 17 00:00:00 2001 From: htt1997 Date: Wed, 23 Apr 2025 16:28:55 +0800 Subject: [PATCH 2/2] Review and modify opinions Signed-off-by: htt1997 --- ...0007-rekey-support-multiple-process.patch} | 62 +++++++++++-------- ...on.patch => 0008-BugFix-CurrVersion.patch} | 0 2 files changed, 36 insertions(+), 26 deletions(-) rename patch/{0008-Rekey-supports-multiple-processes.patch => 0007-rekey-support-multiple-process.patch} (52%) rename patch/{0007-BugFix-CurrVersion.patch => 0008-BugFix-CurrVersion.patch} (100%) diff --git a/patch/0008-Rekey-supports-multiple-processes.patch b/patch/0007-rekey-support-multiple-process.patch similarity index 52% rename from patch/0008-Rekey-supports-multiple-processes.patch rename to patch/0007-rekey-support-multiple-process.patch index 2d91c8b..77a8888 100644 --- a/patch/0008-Rekey-supports-multiple-processes.patch +++ b/patch/0007-rekey-support-multiple-process.patch @@ -1,33 +1,34 @@ -From 0f8f308a9a5884a5d5581008e9f7b018ea98b0fe Mon Sep 17 00:00:00 2001 +From 2ec79c4228c372cd77e24032c0156de72634badc Mon Sep 17 00:00:00 2001 From: ht -Date: Tue, 22 Apr 2025 17:43:16 +0800 -Subject: [PATCH] Rekey supports multiple processes +Date: Wed, 23 Apr 2025 16:26:48 +0800 +Subject: [PATCH] rekey support multiple process Signed-off-by: ht -Change-Id: I1892d85d1e34ed37e6659190e020839059b9822d +Change-Id: I38686a4d05109a7b2a707c6abed54f784542f9da --- - src/sqlite3.c | 54 +++++++++++++++++++++++++++++++++++---------------- - 1 file changed, 37 insertions(+), 17 deletions(-) + src/sqlite3.c | 64 +++++++++++++++++++++++++++++++++++++-------------- + 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/src/sqlite3.c b/src/sqlite3.c -index 4d96a1e..f70df81 100644 +index 1063591..ac41310 100644 --- a/src/sqlite3.c +++ b/src/sqlite3.c -@@ -245765,7 +245765,11 @@ int sqlite3CodecAttach(sqlite3* db, int nDb, const void *pKey, int nKey){ +@@ -245762,7 +245762,12 @@ int sqlite3CodecAttach(sqlite3* db, int nDb, const void *pKey, int nKey){ } sqlite3_mutex_leave(db->mutex); - + rc = sqlite3BtreeBeginTrans(p, 0, 0); + if( rc!=SQLITE_OK ){ ++ sqlite3_log(SQLITE_WARNING, "sqlite3CodecAttach: error when begin trans. errno = %d", rc); + return rc; + } + sqlite3BtreeCommit(p); return SQLITE_OK; } -@@ -245803,6 +245807,21 @@ int sqlite3_key_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey){ - return sqlite3CodecAttach(db, iDb, pKey, nKey); +@@ -245804,6 +245809,21 @@ int sqlite3_rekey(sqlite3 *db, const void *pKey, int nKey){ + return sqlite3_rekey_v2(db, "main", pKey, nKey); } +static inline u8 IsConnectionValidForCheck(Pager *pPager) @@ -45,10 +46,10 @@ index 4d96a1e..f70df81 100644 +#endif +} + - int sqlite3_rekey(sqlite3 *db, const void *pKey, int nKey){ - return sqlite3_rekey_v2(db, "main", pKey, nKey); - } -@@ -245831,7 +245850,22 @@ int sqlite3_rekey_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey){ + int sqlite3_rekey_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey){ + if(db == NULL || pKey == NULL || nKey == 0){ + return SQLITE_ERROR; +@@ -245828,7 +245848,27 @@ int sqlite3_rekey_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey){ return rc; } sqlite3_mutex_enter(db->mutex); @@ -56,31 +57,40 @@ index 4d96a1e..f70df81 100644 + rc = sqlite3BtreeBeginTrans(p, 1, 0); + if(rc != SQLITE_OK){ + sqlite3_mutex_leave(db->mutex); ++ sqlite3_log(SQLITE_WARNING, "sqlite3_rekey_v2: error when begin trans. errno = %d.", rc); + return rc; + } -+ rc = unixShmSystemLock((unixFile *)pPager->fd, F_WRLCK, UNIX_SHM_DMS, 1); -+ if(rc != SQLITE_OK || !IsConnectionValidForCheck(pPager)){ -+ sqlite3_log(SQLITE_WARNING, "sqlite3_rekey_v2: error when lock errno = %d. IsConnectionValidForCheck", rc); -+ if(rc == SQLITE_OK) { -+ unixShmSystemLock((unixFile *)pPager->fd, F_RDLCK, UNIX_SHM_DMS, 1); -+ rc = SQLITE_BUSY; ++#ifndef SQLITE_OMIT_WAL ++ if(pPager->pWal != NULL){ ++ rc = unixShmSystemLock((unixFile *)pPager->fd, F_WRLCK, UNIX_SHM_DMS, 1); ++ if(rc != SQLITE_OK || !IsConnectionValidForCheck(pPager)){ ++ sqlite3_log(SQLITE_WARNING, "sqlite3_rekey_v2: error when lock. errno = %d.", rc); ++ if(rc == SQLITE_OK){ ++ unixShmSystemLock((unixFile *)pPager->fd, F_RDLCK, UNIX_SHM_DMS, 1); ++ rc = SQLITE_BUSY; ++ } ++ (void)sqlite3BtreeRollback(p, SQLITE_ABORT_ROLLBACK, 0); ++ sqlite3_mutex_leave(db->mutex); ++ return rc; + } -+ (void)sqlite3BtreeRollback(p, SQLITE_ABORT_ROLLBACK, 0); -+ sqlite3_mutex_leave(db->mutex); -+ return rc; + } ++#endif sqlite3PagerPagecount(pPager, &pageCount); // support hmac algo changed by using rekey operation int oldHmacAlgo = ctx->writeCtx->codecConst.hmacAlgo; -@@ -245855,6 +245889,7 @@ int sqlite3_rekey_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey){ +@@ -245852,6 +245892,11 @@ int sqlite3_rekey_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey){ } } } -+ unixShmSystemLock((unixFile *)pPager->fd, F_RDLCK, UNIX_SHM_DMS, 1); ++#ifndef SQLITE_OMIT_WAL ++ if(pPager->pWal != NULL){ ++ unixShmSystemLock((unixFile *)pPager->fd, F_RDLCK, UNIX_SHM_DMS, 1); ++ } ++#endif if(rc == SQLITE_OK){ (void)sqlite3BtreeCommit(p); sqlite3CodecFreeKeyContext(ctx->readCtx); -@@ -247210,21 +247245,6 @@ CHK_RESTORE_OUT: +@@ -247189,21 +247234,6 @@ CHK_RESTORE_OUT: return rc; } diff --git a/patch/0007-BugFix-CurrVersion.patch b/patch/0008-BugFix-CurrVersion.patch similarity index 100% rename from patch/0007-BugFix-CurrVersion.patch rename to patch/0008-BugFix-CurrVersion.patch -- Gitee