Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
73b09b8
Add new files for an extension to recover data from corrupted databases.
Aug 31, 2022
6c86783
Further work on making recovery extension compatible with the shell t…
Sep 1, 2022
f2f8a3a
Further work on making the recover extension compatible with the .rec…
Sep 3, 2022
be2e212
Take the freelist into account when recovering data that is not linke…
Sep 3, 2022
7302079
Add an option to assume the freelist is corrupt when recovering data.
Sep 3, 2022
a768b67
Add the SQLITE_RECOVER_ROWIDS option. To specify that rowid values th…
Sep 5, 2022
d5e3fe5
Add a mode to output SQL statements instead of populating a database …
Sep 5, 2022
108c51b
Fix a problem with script mode and lost-and-found tables.
Sep 5, 2022
497b3e6
Tests and a fix for the SQLITE_RECOVER_ROWIDS option.
Sep 6, 2022
abb2866
Ensure that the recover extension properly escapes CR and NL characte…
Sep 7, 2022
4a08e79
Add new test file recoverclobber.test.
Sep 8, 2022
3887ffe
Fix problems with recovering the sqlite_sequence table.
Sep 8, 2022
9a27d65
Update the shell to use the recover extension for the .recover command.
Sep 8, 2022
46d4398
Update comments in sqlite3recover.h.
Sep 8, 2022
7920162
Handle recovery of virtual tables by recovering each shadow table ind…
Sep 9, 2022
bc2e7fc
Further fixes to comments in sqlite3recover.h. Also rework some data …
Sep 9, 2022
6566091
Rework recover extension code for readability.
Sep 10, 2022
aede9b5
More tests for the recover module.
Sep 12, 2022
5ca0b38
Fix some problems with handling corrupt database in the recovery exte…
Sep 12, 2022
7985e05
Add tests. Deal issues surrounding aborting recovery from within the …
Sep 13, 2022
a3a6d63
Fix a broken error handling case in the recovery extension.
Sep 13, 2022
322967d
Add OOM tests for the recovery extension.
Sep 13, 2022
b8eaf9a
Fix recovery of utf-16 databases.
Sep 14, 2022
3c24342
Extra tests and fixes.
Sep 14, 2022
6fa0a11
Include recovery extension in configure/make builds of testfixture.
Sep 14, 2022
73390f0
Fix windows builds of recover extension.
Sep 15, 2022
bdbd097
Merge latest trunk changes into this branch.
Sep 23, 2022
80b1f6f
Fix various compiler warnings in new code on this branch.
Sep 23, 2022
9bbd91b
Internal changes to the recover extension towards a step() style inte…
Sep 23, 2022
aee4049
Update things to use sqlite3_recover_step() internally.
Sep 24, 2022
351563e
Add sqlite3_recover_step() to header file sqlite3recover.h. Update th…
Sep 24, 2022
8d8fc58
Add the SQLITE_RECOVER_SLOWINDEXES option, for specifying that indexe…
Sep 24, 2022
7e33f8f
Modify implementation of recovery to break recovery of the lost_and_f…
Sep 26, 2022
041cc06
Better handling for databases with corrupt headers.
Oct 4, 2022
461fa44
Merge latest trunk changes.
Oct 4, 2022
adc6860
Improve handling of corruption in the sqlite_schema table b-tree stru…
Oct 5, 2022
4df12ce
Add code to determine the database page-size by searching for well-fo…
Oct 6, 2022
88a0637
Ensure the page-size, auto-vacuum and other settings are copied into …
Oct 6, 2022
249efd0
Add tests for detecting page size of databases.
Oct 13, 2022
3fa10f8
Handle recovering databases with reserved bytes at the end of each page.
Oct 14, 2022
9dffd36
Merge latest trunk changes.
Oct 15, 2022
f0799fd
Add missing comments and fix other code issues.
Oct 15, 2022
8e62d37
Exclude slow test recovercorrupt.test from veryquick.test.
Oct 15, 2022
361fb98
Move file /ext/misc/dbdata.c to the /ext/recover/ directory.
Oct 19, 2022
5f72669
Merge latest trunk changes with this branch.
Oct 25, 2022
5ad03a4
Merge further changes from trunk, including fix to dbdata.c.
Oct 26, 2022
50632af
Remove an undefined left-shift operation from the recover extension.
Oct 26, 2022
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
6 changes: 6 additions & 0 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,9 @@ TESTSRC = \
$(TOP)/ext/fts3/fts3_term.c \
$(TOP)/ext/fts3/fts3_test.c \
$(TOP)/ext/session/test_session.c \
$(TOP)/ext/recover/sqlite3recover.c \
$(TOP)/ext/recover/dbdata.c \
$(TOP)/ext/recover/test_recover.c \
$(TOP)/ext/rbu/test_rbu.c

# Statically linked extensions
Expand Down Expand Up @@ -1108,6 +1111,9 @@ SHELL_SRC = \
$(TOP)/ext/expert/sqlite3expert.h \
$(TOP)/ext/misc/zipfile.c \
$(TOP)/ext/misc/memtrace.c \
$(TOP)/ext/recover/dbdata.c \
$(TOP)/ext/recover/sqlite3recover.c \
$(TOP)/ext/recover/sqlite3recover.h \
$(TOP)/src/test_windirent.c

shell.c: $(SHELL_SRC) $(TOP)/tool/mkshellc.tcl
Expand Down
6 changes: 6 additions & 0 deletions Makefile.msc
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,9 @@ TESTEXT = \
$(TOP)\ext\misc\unionvtab.c \
$(TOP)\ext\misc\wholenumber.c \
$(TOP)\ext\rtree\test_rtreedoc.c \
$(TOP)\ext\recover\sqlite3recover.c \
$(TOP)\ext\recover\test_recover.c \
$(TOP)\ext\recover\dbdata.c \
fts5.c

# If use of zlib is enabled, add the "zipfile.c" source file.
Expand Down Expand Up @@ -2228,6 +2231,9 @@ SHELL_SRC = \
$(TOP)\ext\expert\sqlite3expert.c \
$(TOP)\ext\expert\sqlite3expert.h \
$(TOP)\ext\misc\memtrace.c \
$(TOP)/ext/recover/dbdata.c \
$(TOP)/ext/recover/sqlite3recover.c \
$(TOP)/ext/recover/sqlite3recover.h \
$(TOP)\src\test_windirent.c

# If use of zlib is enabled, add the "zipfile.c" source file.
Expand Down
2 changes: 1 addition & 1 deletion ext/misc/cksumvfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
**
** sqlite3 *db;
** sqlite3_open(":memory:", &db);
** sqlite3_load_extention(db, "./cksumvfs");
** sqlite3_load_extension(db, "./cksumvfs");
** sqlite3_close(db);
**
** If this extension is compiled with -DSQLITE_CKSUMVFS_STATIC and
Expand Down
93 changes: 80 additions & 13 deletions ext/misc/dbdata.c → ext/recover/dbdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
#include "sqlite3ext.h"

typedef unsigned char u8;
typedef unsigned int u32;

#endif
SQLITE_EXTENSION_INIT1
Expand Down Expand Up @@ -107,6 +108,7 @@ struct DbdataCursor {
int iField; /* Current field number */
u8 *pHdrPtr;
u8 *pPtr;
u32 enc; /* Text encoding */

sqlite3_int64 iIntkey; /* Integer key value */
};
Expand Down Expand Up @@ -299,14 +301,14 @@ static int dbdataClose(sqlite3_vtab_cursor *pCursor){
/*
** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
*/
static unsigned int get_uint16(unsigned char *a){
static u32 get_uint16(unsigned char *a){
return (a[0]<<8)|a[1];
}
static unsigned int get_uint32(unsigned char *a){
return ((unsigned int)a[0]<<24)
| ((unsigned int)a[1]<<16)
| ((unsigned int)a[2]<<8)
| ((unsigned int)a[3]);
static u32 get_uint32(unsigned char *a){
return ((u32)a[0]<<24)
| ((u32)a[1]<<16)
| ((u32)a[2]<<8)
| ((u32)a[3]);
}

/*
Expand All @@ -321,7 +323,7 @@ static unsigned int get_uint32(unsigned char *a){
*/
static int dbdataLoadPage(
DbdataCursor *pCsr, /* Cursor object */
unsigned int pgno, /* Page number of page to load */
u32 pgno, /* Page number of page to load */
u8 **ppPage, /* OUT: pointer to page buffer */
int *pnPage /* OUT: Size of (*ppPage) in bytes */
){
Expand Down Expand Up @@ -418,6 +420,7 @@ static int dbdataValueBytes(int eType){
*/
static void dbdataValue(
sqlite3_context *pCtx,
u32 enc,
int eType,
u8 *pData,
int nData
Expand Down Expand Up @@ -462,7 +465,17 @@ static void dbdataValue(
default: {
int n = ((eType-12) / 2);
if( eType % 2 ){
sqlite3_result_text(pCtx, (const char*)pData, n, SQLITE_TRANSIENT);
switch( enc ){
case SQLITE_UTF16BE:
sqlite3_result_text16be(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
break;
case SQLITE_UTF16LE:
sqlite3_result_text16le(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
break;
default:
sqlite3_result_text(pCtx, (char*)pData, n, SQLITE_TRANSIENT);
break;
}
}else{
sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
}
Expand Down Expand Up @@ -490,6 +503,7 @@ static int dbdataNext(sqlite3_vtab_cursor *pCursor){
rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
if( rc!=SQLITE_OK ) return rc;
if( pCsr->aPage ) break;
if( pCsr->bOnePage ) return SQLITE_OK;
pCsr->iPgno++;
}
pCsr->iCell = pTab->bPtr ? -2 : 0;
Expand Down Expand Up @@ -600,7 +614,7 @@ static int dbdataNext(sqlite3_vtab_cursor *pCursor){
/* Load content from overflow pages */
if( nPayload>nLocal ){
sqlite3_int64 nRem = nPayload - nLocal;
unsigned int pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
u32 pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
while( nRem>0 ){
u8 *aOvfl = 0;
int nOvfl = 0;
Expand Down Expand Up @@ -673,6 +687,18 @@ static int dbdataEof(sqlite3_vtab_cursor *pCursor){
return pCsr->aPage==0;
}

/*
** Return true if nul-terminated string zSchema ends in "()". Or false
** otherwise.
*/
static int dbdataIsFunction(const char *zSchema){
int n = strlen(zSchema);
if( n>2 && zSchema[n-2]=='(' && zSchema[n-1]==')' ){
return n-2;
}
return 0;
}

/*
** Determine the size in pages of database zSchema (where zSchema is
** "main", "temp" or the name of an attached database) and set
Expand All @@ -683,10 +709,16 @@ static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
char *zSql = 0;
int rc, rc2;
int nFunc = 0;
sqlite3_stmt *pStmt = 0;

zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
if( (nFunc = dbdataIsFunction(zSchema))>0 ){
zSql = sqlite3_mprintf("SELECT %.*s(0)", nFunc, zSchema);
}else{
zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
}
if( zSql==0 ) return SQLITE_NOMEM;

rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
sqlite3_free(zSql);
if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
Expand All @@ -697,6 +729,25 @@ static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
return rc;
}

/*
** Attempt to figure out the encoding of the database by retrieving page 1
** and inspecting the header field. If successful, set the pCsr->enc variable
** and return SQLITE_OK. Otherwise, return an SQLite error code.
*/
static int dbdataGetEncoding(DbdataCursor *pCsr){
int rc = SQLITE_OK;
int nPg1 = 0;
u8 *aPg1 = 0;
rc = dbdataLoadPage(pCsr, 1, &aPg1, &nPg1);
assert( rc!=SQLITE_OK || nPg1==0 || nPg1>=512 );
if( rc==SQLITE_OK && nPg1>0 ){
pCsr->enc = get_uint32(&aPg1[56]);
}
sqlite3_free(aPg1);
return rc;
}


/*
** xFilter method for sqlite_dbdata and sqlite_dbptr.
*/
Expand All @@ -719,14 +770,22 @@ static int dbdataFilter(
pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
pCsr->bOnePage = 1;
}else{
pCsr->nPage = dbdataDbsize(pCsr, zSchema);
rc = dbdataDbsize(pCsr, zSchema);
}

if( rc==SQLITE_OK ){
int nFunc = 0;
if( pTab->pStmt ){
pCsr->pStmt = pTab->pStmt;
pTab->pStmt = 0;
}else if( (nFunc = dbdataIsFunction(zSchema))>0 ){
char *zSql = sqlite3_mprintf("SELECT %.*s(?2)", nFunc, zSchema);
if( zSql==0 ){
rc = SQLITE_NOMEM;
}else{
rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
sqlite3_free(zSql);
}
}else{
rc = sqlite3_prepare_v2(pTab->db,
"SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
Expand All @@ -739,13 +798,20 @@ static int dbdataFilter(
}else{
pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
}

/* Try to determine the encoding of the db by inspecting the header
** field on page 1. */
if( rc==SQLITE_OK ){
rc = dbdataGetEncoding(pCsr);
}

if( rc==SQLITE_OK ){
rc = dbdataNext(pCursor);
}
return rc;
}

/*
/*
** Return a column for the sqlite_dbdata or sqlite_dbptr table.
*/
static int dbdataColumn(
Expand Down Expand Up @@ -793,7 +859,8 @@ static int dbdataColumn(
sqlite3_int64 iType;
dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
dbdataValue(
ctx, iType, pCsr->pPtr, &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
ctx, pCsr->enc, iType, pCsr->pPtr,
&pCsr->pRec[pCsr->nRec] - pCsr->pPtr
);
}
break;
Expand Down
Loading