mirror of
https://github.com/sqlite/sqlite.git
synced 2025-07-30 19:03:16 +03:00
Better handling for databases with corrupt headers.
FossilOrigin-Name: 17f68d803685405d880025134c71bac01077962d677f2902a608952d0b9cdb5a
This commit is contained in:
@ -28,6 +28,7 @@ __declspec(dllexport)
|
||||
int sqlite3_dbdata_init(sqlite3*, char**, const sqlite3_api_routines*);
|
||||
|
||||
typedef unsigned int u32;
|
||||
typedef unsigned char u8;
|
||||
typedef sqlite3_int64 i64;
|
||||
|
||||
typedef struct RecoverTable RecoverTable;
|
||||
@ -240,11 +241,30 @@ struct sqlite3_recover {
|
||||
#define RECOVER_STATE_SCHEMA2 5
|
||||
#define RECOVER_STATE_DONE 6
|
||||
|
||||
|
||||
/*
|
||||
** Global variables used by this extension.
|
||||
*/
|
||||
typedef struct RecoverGlobal RecoverGlobal;
|
||||
struct RecoverGlobal {
|
||||
const sqlite3_io_methods *pMethods;
|
||||
};
|
||||
static RecoverGlobal recover_g;
|
||||
|
||||
/*
|
||||
** Use this static SQLite mutex to protect the globals during the
|
||||
** first call to sqlite3_recover_step().
|
||||
*/
|
||||
#define RECOVER_MUTEX_ID SQLITE_MUTEX_STATIC_APP2
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Default value for SQLITE_RECOVER_ROWIDS (sqlite3_recover.bRecoverRowid).
|
||||
*/
|
||||
#define RECOVER_ROWID_DEFAULT 1
|
||||
|
||||
|
||||
/*
|
||||
** Like strlen(). But handles NULL pointer arguments.
|
||||
*/
|
||||
@ -1840,6 +1860,247 @@ static void recoverFinalCleanup(sqlite3_recover *p){
|
||||
p->dbOut = 0;
|
||||
}
|
||||
|
||||
static int recoverVfsClose(sqlite3_file*);
|
||||
static int recoverVfsRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
|
||||
static int recoverVfsWrite(sqlite3_file*, const void*, int, sqlite3_int64);
|
||||
static int recoverVfsTruncate(sqlite3_file*, sqlite3_int64 size);
|
||||
static int recoverVfsSync(sqlite3_file*, int flags);
|
||||
static int recoverVfsFileSize(sqlite3_file*, sqlite3_int64 *pSize);
|
||||
static int recoverVfsLock(sqlite3_file*, int);
|
||||
static int recoverVfsUnlock(sqlite3_file*, int);
|
||||
static int recoverVfsCheckReservedLock(sqlite3_file*, int *pResOut);
|
||||
static int recoverVfsFileControl(sqlite3_file*, int op, void *pArg);
|
||||
static int recoverVfsSectorSize(sqlite3_file*);
|
||||
static int recoverVfsDeviceCharacteristics(sqlite3_file*);
|
||||
static int recoverVfsShmMap(sqlite3_file*, int, int, int, void volatile**);
|
||||
static int recoverVfsShmLock(sqlite3_file*, int offset, int n, int flags);
|
||||
static void recoverVfsShmBarrier(sqlite3_file*);
|
||||
static int recoverVfsShmUnmap(sqlite3_file*, int deleteFlag);
|
||||
|
||||
static sqlite3_io_methods recover_methods = {
|
||||
2, /* iVersion */
|
||||
recoverVfsClose,
|
||||
recoverVfsRead,
|
||||
recoverVfsWrite,
|
||||
recoverVfsTruncate,
|
||||
recoverVfsSync,
|
||||
recoverVfsFileSize,
|
||||
recoverVfsLock,
|
||||
recoverVfsUnlock,
|
||||
recoverVfsCheckReservedLock,
|
||||
recoverVfsFileControl,
|
||||
recoverVfsSectorSize,
|
||||
recoverVfsDeviceCharacteristics,
|
||||
recoverVfsShmMap,
|
||||
recoverVfsShmLock,
|
||||
recoverVfsShmBarrier,
|
||||
recoverVfsShmUnmap,
|
||||
0, 0
|
||||
};
|
||||
|
||||
static int recoverVfsClose(sqlite3_file *pFd){
|
||||
assert( pFd->pMethods!=&recover_methods );
|
||||
return pFd->pMethods->xClose(pFd);
|
||||
}
|
||||
|
||||
static u32 recoverGetU16(const u8 *a){
|
||||
return (((u32)a[0])<<8) + ((u32)a[1]);
|
||||
}
|
||||
static u32 recoverGetU32(const u8 *a){
|
||||
return (((u32)a[0])<<24) + (((u32)a[1])<<16) + (((u32)a[2])<<8) + ((u32)a[3]);
|
||||
}
|
||||
|
||||
static void recoverPutU16(u8 *a, u32 v){
|
||||
a[0] = (v>>8) & 0x00FF;
|
||||
a[1] = (v>>0) & 0x00FF;
|
||||
}
|
||||
static u32 recoverPutU32(u8 *a, u32 v){
|
||||
a[0] = (v>>24) & 0x00FF;
|
||||
a[1] = (v>>16) & 0x00FF;
|
||||
a[2] = (v>>8) & 0x00FF;
|
||||
a[3] = (v>>0) & 0x00FF;
|
||||
}
|
||||
|
||||
static int recoverVfsRead(sqlite3_file *pFd, void *aBuf, int nByte, i64 iOff){
|
||||
int rc = SQLITE_OK;
|
||||
if( pFd->pMethods==&recover_methods ){
|
||||
pFd->pMethods = recover_g.pMethods;
|
||||
rc = pFd->pMethods->xRead(pFd, aBuf, nByte, iOff);
|
||||
if( rc==SQLITE_OK && iOff==0 && nByte>=100 ){
|
||||
/* Ensure that the database has a valid header file. The only fields
|
||||
** that really matter to recovery are:
|
||||
**
|
||||
** + Database page size (16-bits at offset 16)
|
||||
** + Size of db in pages (32-bits at offset 28)
|
||||
** + Database encoding (32-bits at offset 56)
|
||||
**
|
||||
** Also preserved are:
|
||||
**
|
||||
** + first freelist page (32-bits at offset 32)
|
||||
** + size of freelist (32-bits at offset 36)
|
||||
**
|
||||
** We also try to preserve the auto-vacuum, incr-value, user-version
|
||||
** and application-id fields - all 32 bit quantities at offsets
|
||||
** 52, 60, 64 and 68. All other fields are set to known good values.
|
||||
*/
|
||||
const int aPreserve[] = {32, 36, 52, 60, 64, 68};
|
||||
u8 aHdr[100] = {
|
||||
0x53, 0x51, 0x4c, 0x69, 0x74, 0x65, 0x20, 0x66,
|
||||
0x6f, 0x72, 0x6d, 0x61, 0x74, 0x20, 0x33, 0x00,
|
||||
0xFF, 0xFF, 0x01, 0x01, 0x00, 0x40, 0x20, 0x20,
|
||||
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
|
||||
0x00, 0x00, 0x10, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x2e, 0x5b, 0x30
|
||||
};
|
||||
u8 *a = (u8*)aBuf;
|
||||
|
||||
u32 pgsz = recoverGetU16(&a[16]);
|
||||
u32 enc = recoverGetU32(&a[56]);
|
||||
u32 dbsz = 0;
|
||||
i64 dbFileSize = 0;
|
||||
int ii;
|
||||
|
||||
if( pgsz==0x01 ) pgsz = 65536;
|
||||
rc = pFd->pMethods->xFileSize(pFd, &dbFileSize);
|
||||
|
||||
dbsz = dbFileSize / pgsz;
|
||||
if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16BE && enc!=SQLITE_UTF16LE ){
|
||||
enc = SQLITE_UTF8;
|
||||
}
|
||||
|
||||
recoverPutU32(&aHdr[28], dbsz);
|
||||
recoverPutU32(&aHdr[56], enc);
|
||||
if( pgsz==65536 ) pgsz = 1;
|
||||
recoverPutU16(&aHdr[16], pgsz);
|
||||
for(ii=0; ii<sizeof(aPreserve)/sizeof(aPreserve[0]); ii++){
|
||||
memcpy(&aHdr[aPreserve[ii]], &a[aPreserve[ii]], 4);
|
||||
}
|
||||
memcpy(aBuf, aHdr, sizeof(aHdr));
|
||||
}
|
||||
pFd->pMethods = &recover_methods;
|
||||
}else{
|
||||
rc = pFd->pMethods->xRead(pFd, aBuf, nByte, iOff);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
#define RECOVER_VFS_WRAPPER(code) \
|
||||
int rc = SQLITE_OK; \
|
||||
if( pFd->pMethods==&recover_methods ){ \
|
||||
pFd->pMethods = recover_g.pMethods; \
|
||||
rc = code; \
|
||||
pFd->pMethods = &recover_methods; \
|
||||
}else{ \
|
||||
rc = code; \
|
||||
} \
|
||||
return rc;
|
||||
|
||||
static int recoverVfsWrite(
|
||||
sqlite3_file *pFd, const void *aBuf, int nByte, i64 iOff
|
||||
){
|
||||
RECOVER_VFS_WRAPPER (
|
||||
pFd->pMethods->xWrite(pFd, aBuf, nByte, iOff)
|
||||
);
|
||||
}
|
||||
static int recoverVfsTruncate(sqlite3_file *pFd, sqlite3_int64 size){
|
||||
RECOVER_VFS_WRAPPER (
|
||||
pFd->pMethods->xTruncate(pFd, size)
|
||||
);
|
||||
}
|
||||
|
||||
static int recoverVfsSync(sqlite3_file *pFd, int flags){
|
||||
RECOVER_VFS_WRAPPER (
|
||||
pFd->pMethods->xSync(pFd, flags)
|
||||
);
|
||||
}
|
||||
static int recoverVfsFileSize(sqlite3_file *pFd, sqlite3_int64 *pSize){
|
||||
RECOVER_VFS_WRAPPER (
|
||||
pFd->pMethods->xFileSize(pFd, pSize)
|
||||
);
|
||||
}
|
||||
static int recoverVfsLock(sqlite3_file *pFd, int eLock){
|
||||
RECOVER_VFS_WRAPPER (
|
||||
pFd->pMethods->xLock(pFd, eLock)
|
||||
);
|
||||
}
|
||||
static int recoverVfsUnlock(sqlite3_file *pFd, int eLock){
|
||||
RECOVER_VFS_WRAPPER (
|
||||
pFd->pMethods->xUnlock(pFd, eLock)
|
||||
);
|
||||
}
|
||||
static int recoverVfsCheckReservedLock(sqlite3_file *pFd, int *pResOut){
|
||||
RECOVER_VFS_WRAPPER (
|
||||
pFd->pMethods->xCheckReservedLock(pFd, pResOut)
|
||||
);
|
||||
}
|
||||
static int recoverVfsFileControl(sqlite3_file *pFd, int op, void *pArg){
|
||||
RECOVER_VFS_WRAPPER (
|
||||
pFd->pMethods->xFileControl(pFd, op, pArg)
|
||||
);
|
||||
}
|
||||
static int recoverVfsSectorSize(sqlite3_file *pFd){
|
||||
RECOVER_VFS_WRAPPER (
|
||||
pFd->pMethods->xSectorSize(pFd)
|
||||
);
|
||||
}
|
||||
static int recoverVfsDeviceCharacteristics(sqlite3_file *pFd){
|
||||
RECOVER_VFS_WRAPPER (
|
||||
pFd->pMethods->xDeviceCharacteristics(pFd)
|
||||
);
|
||||
}
|
||||
static int recoverVfsShmMap(
|
||||
sqlite3_file *pFd, int iPg, int pgsz, int bExtend, void volatile **pp
|
||||
){
|
||||
RECOVER_VFS_WRAPPER (
|
||||
pFd->pMethods->xShmMap(pFd, iPg, pgsz, bExtend, pp)
|
||||
);
|
||||
}
|
||||
static int recoverVfsShmLock(sqlite3_file *pFd, int offset, int n, int flags){
|
||||
RECOVER_VFS_WRAPPER (
|
||||
pFd->pMethods->xShmLock(pFd, offset, n, flags)
|
||||
);
|
||||
}
|
||||
static void recoverVfsShmBarrier(sqlite3_file *pFd){
|
||||
if( pFd->pMethods==&recover_methods ){
|
||||
pFd->pMethods = recover_g.pMethods;
|
||||
pFd->pMethods->xShmBarrier(pFd);
|
||||
pFd->pMethods = &recover_methods;
|
||||
}else{
|
||||
pFd->pMethods->xShmBarrier(pFd);
|
||||
}
|
||||
}
|
||||
static int recoverVfsShmUnmap(sqlite3_file *pFd, int deleteFlag){
|
||||
RECOVER_VFS_WRAPPER (
|
||||
pFd->pMethods->xShmUnmap(pFd, deleteFlag)
|
||||
);
|
||||
}
|
||||
|
||||
static void recoverInstallWrapper(sqlite3_recover *p){
|
||||
sqlite3_file *pFd = 0;
|
||||
assert( recover_g.pMethods==0 );
|
||||
sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_FILE_POINTER, (void*)&pFd);
|
||||
if( pFd ){
|
||||
recover_g.pMethods = pFd->pMethods;
|
||||
pFd->pMethods = &recover_methods;
|
||||
}
|
||||
}
|
||||
static void recoverUninstallWrapper(sqlite3_recover *p){
|
||||
if( recover_g.pMethods ){
|
||||
sqlite3_file *pFd = 0;
|
||||
sqlite3_file_control(p->dbIn, p->zDb,SQLITE_FCNTL_FILE_POINTER,(void*)&pFd);
|
||||
assert( pFd );
|
||||
pFd->pMethods = recover_g.pMethods;
|
||||
recover_g.pMethods = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void recoverStep(sqlite3_recover *p){
|
||||
assert( p && p->errCode==SQLITE_OK );
|
||||
switch( p->eState ){
|
||||
@ -1853,6 +2114,9 @@ static void recoverStep(sqlite3_recover *p){
|
||||
** user functions with the new handle. */
|
||||
recoverOpenOutput(p);
|
||||
|
||||
sqlite3_mutex_enter( sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_APP2) );
|
||||
recoverInstallWrapper(p);
|
||||
|
||||
/* Open transactions on both the input and output databases. */
|
||||
recoverExec(p, p->dbIn, "PRAGMA writable_schema = on");
|
||||
recoverExec(p, p->dbIn, "BEGIN");
|
||||
@ -1860,8 +2124,11 @@ static void recoverStep(sqlite3_recover *p){
|
||||
recoverExec(p, p->dbOut, "BEGIN");
|
||||
|
||||
recoverCacheSchema(p);
|
||||
recoverWriteSchema1(p);
|
||||
|
||||
recoverUninstallWrapper(p);
|
||||
sqlite3_mutex_leave( sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_APP2) );
|
||||
|
||||
recoverWriteSchema1(p);
|
||||
p->eState = RECOVER_STATE_WRITING;
|
||||
break;
|
||||
|
||||
|
12
manifest
12
manifest
@ -1,5 +1,5 @@
|
||||
C Modify\simplementation\sof\srecovery\sto\sbreak\srecovery\sof\sthe\slost_and_found\stable\sinto\smultiple\ssteps.
|
||||
D 2022-09-26T20:51:16.327
|
||||
C Better\shandling\sfor\sdatabases\swith\scorrupt\sheaders.
|
||||
D 2022-10-04T19:43:12.989
|
||||
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
|
||||
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
|
||||
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
|
||||
@ -398,7 +398,7 @@ F ext/recover/recoverold.test 46e9d99b595fac583d4c67f74d7d89c20a435c752ef6eeb3e9
|
||||
F ext/recover/recoverrowid.test 1694a1a5526d825f71279f3d02ab02a1ee4c5265de18858bf54cb8ec54487ac8
|
||||
F ext/recover/recoverslowidx.test 7e3889e50758c614b9b3e75187eeeea425c0ca8a2387441fc19ae749a96a7949
|
||||
F ext/recover/recoversql.test f9872ff2114e13ffd8ee31e1de06919f62b9b48bc080191b5bd076d10becb60f
|
||||
F ext/recover/sqlite3recover.c 5aad3f4e569e8637abdadfeb748ec1b99198d6d45bf983c6b168a527f5891b7f
|
||||
F ext/recover/sqlite3recover.c 9c4d742223c519ef7322c51cb8997ca1046ca1971c06b617d455ec0bf0a9b390
|
||||
F ext/recover/sqlite3recover.h f698ccc94bd4da38761035415ad08c4549a408491ff9fd5f52d34d2214f64e36
|
||||
F ext/recover/test_recover.c 61ec931e47abca6b2210f46239cafd9f3060741605e3d3c45a7c7a53f63dd957
|
||||
F ext/repair/README.md 92f5e8aae749a4dae14f02eea8e1bb42d4db2b6ce5e83dbcdd6b1446997e0c15
|
||||
@ -2014,8 +2014,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
|
||||
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
|
||||
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
|
||||
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
|
||||
P ad9dba9d1eae786575c7f31e34b342b6f5b26e719bbe27b61609cad8cfd0a505
|
||||
R d7188e5461609c695208ab621d6c4574
|
||||
P adedfd040bb2d1c2dcda0b91c6073f12953e45d9b860a8f217d990e861d66dfb
|
||||
R bababa544bff189a544b5800da5cb030
|
||||
U dan
|
||||
Z d49040b82cb2efa369225859bd08a957
|
||||
Z 620b6c10a3ee6d6290114869c2142ea0
|
||||
# Remove this line to create a well-formed Fossil manifest.
|
||||
|
@ -1 +1 @@
|
||||
adedfd040bb2d1c2dcda0b91c6073f12953e45d9b860a8f217d990e861d66dfb
|
||||
17f68d803685405d880025134c71bac01077962d677f2902a608952d0b9cdb5a
|
Reference in New Issue
Block a user