From: mistachkin Date: Mon, 10 Sep 2012 10:29:06 +0000 (+0000) Subject: Initial prototype of SQLITE_CONFIG_READONLY. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e4df1b81755f43d3f2c146aea63e2683ed599209;p=thirdparty%2Fsqlite.git Initial prototype of SQLITE_CONFIG_READONLY. FossilOrigin-Name: 7c3401657a0410dd121d49742db102c6e7494964 --- diff --git a/manifest b/manifest index cf4d0a240c..0f277a2bc3 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Avoid\susing\sthe\ssqlite3ErrStr\sfunction\sin\sthe\sTcl\spackage\sif\sUSE_SYSTEM_SQLITE\sis\sdefined. -D 2012-09-10T09:33:09.064 +C Initial\sprototype\sof\sSQLITE_CONFIG_READONLY. +D 2012-09-10T10:29:06.407 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in abd5c10d21d1395f140d9e50ea999df8fa4d6376 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -136,7 +136,7 @@ F src/expr.c 217840a107dcc1e5dbb57cea311daad04bedbb9a F src/fault.c 160a0c015b6c2629d3899ed2daf63d75754a32bb F src/fkey.c 9c77d842dc9961d92a06a65abb80c64ef1750296 F src/func.c 18dfedfb857e100b05755a1b12e88b389f957879 -F src/global.c 4cfdca5cb0edd33c4d021baec4ede958cb2c793b +F src/global.c 2f3b6eb7dd5dc94f328f5b40bcdca262d4218be4 F src/hash.c a4031441741932da9e7a65bee2b36b5d0e81c073 F src/hash.h 2894c932d84d9f892d4b4023a75e501f83050970 F src/hwtime.h d32741c8f4df852c7d959236615444e2b1063b08 @@ -145,7 +145,7 @@ F src/journal.c 552839e54d1bf76fb8f7abe51868b66acacf6a0e F src/legacy.c a199d7683d60cef73089e892409113e69c23a99f F src/lempar.c 0ee69fca0be54cd93939df98d2aca4ca46f44416 F src/loadext.c f20382fbaeec832438a1ba7797bee3d3c8a6d51d -F src/main.c 02255cf1da50956c5427c469abddb15bccc4ba09 +F src/main.c 9600da7dae7ede8999fdd8a59cb55f7abe6809ed F src/malloc.c fe085aa851b666b7c375c1ff957643dc20a04bf6 F src/mem0.c 6a55ebe57c46ca1a7d98da93aaa07f99f1059645 F src/mem1.c 437c7c4af964895d4650f29881df63535caaa1fa @@ -178,10 +178,10 @@ F src/resolve.c 9e28280ec98035f31900fdd1db01f86f68ca6c32 F src/rowset.c 64655f1a627c9c212d9ab497899e7424a34222e0 F src/select.c f843c872a97baa1594c2cc3d4c003409a7bd03af F src/shell.c 87953c5d9c73d9494db97d1607e2e2280418f261 -F src/sqlite.h.in 39ddd714b62c651bfb5646440c594ca7ff6c7453 +F src/sqlite.h.in 340c7c9e804335a46d42c0cf300b2ba04930c358 F src/sqlite3.rc fea433eb0a59f4c9393c8e6d76a6e2596b1fe0c0 F src/sqlite3ext.h 6904f4aadf976f95241311fbffb00823075d9477 -F src/sqliteInt.h 053e03a532beb909ead2df0721db67cdb4c48ae8 +F src/sqliteInt.h 7539de59f231581d153b3f3df47903c711554b52 F src/sqliteLimit.h 164b0e6749d31e0daa1a4589a169d31c0dec7b3d F src/status.c 35939e7e03abf1b7577ce311f48f682c40de3208 F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e @@ -210,7 +210,7 @@ F src/test_intarray.c d879bbf8e4ce085ab966d1f3c896a7c8b4f5fc99 F src/test_intarray.h 489edb9068bb926583445cb02589344961054207 F src/test_journal.c f5c0a05b7b3d5930db769b5ee6c3766dc2221a64 F src/test_loadext.c df586c27176e3c2cb2e099c78da67bf14379a56e -F src/test_malloc.c 7c8e2511d9d9661c2fcf91960ce0fb801bae8d0a +F src/test_malloc.c 2fdd098d25a101e228c6a7576d0ebdd87371f9ff F src/test_multiplex.c ac0fbc1748e5b86a41a1d7a84654fae0d53a881d F src/test_multiplex.h e99c571bc4968b7a9363b661481f3934bfead61d F src/test_mutex.c a6bd7b9cf6e19d989e31392b06ac8d189f0d573e @@ -632,7 +632,7 @@ F test/notify2.test 9503e51b9a272a5405c205ad61b7623d5a9ca489 F test/notify3.test a86259abbfb923aa27d30f0fc038c88e5251488a F test/notnull.test cc7c78340328e6112a13c3e311a9ab3127114347 F test/null.test a8b09b8ed87852742343b33441a9240022108993 -F test/openv2.test 0d3040974bf402e19b7df4b783e447289d7ab394 +F test/openv2.test d1dbebe1573a440dc4a4bd6b7e6125272a0ec56b F test/oserror.test 50417780d0e0d7cd23cf12a8277bb44024765df3 F test/pager1.test 2163c6ef119f497a71a84137c957c63763e640ab F test/pager2.test 745b911dde3d1f24ae0870bd433dfa83d7c658c1 @@ -1014,7 +1014,10 @@ F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381 F tool/win/sqlite.vsix 67d8a99aceb56384a81b3f30d6c71743146d2cc9 -P af870ca9827d2079a35dbb545660f670c7ca9512 -R 41116711b74a4531ffcdbedf61877a05 +P a716b9a3091072774d067c0a1258b0f4c39a4180 +R 29425f73d83a1bbd7b16d5c03bbdc177 +T *branch * configReadOnly +T *sym-configReadOnly * +T -sym-trunk * U mistachkin -Z 81d27ccc240f2f1ae0e0280e87c1196f +Z c66435427bfb7211d34f4620152d3df9 diff --git a/manifest.uuid b/manifest.uuid index d65f797478..d52bd36407 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -a716b9a3091072774d067c0a1258b0f4c39a4180 \ No newline at end of file +7c3401657a0410dd121d49742db102c6e7494964 \ No newline at end of file diff --git a/src/global.c b/src/global.c index 7de0668250..a319b0679f 100644 --- a/src/global.c +++ b/src/global.c @@ -142,6 +142,7 @@ SQLITE_WSD struct Sqlite3Config sqlite3Config = { 1, /* bCoreMutex */ SQLITE_THREADSAFE==1, /* bFullMutex */ SQLITE_USE_URI, /* bOpenUri */ + 0, /* bReadOnly */ 0x7ffffffe, /* mxStrlen */ 128, /* szLookaside */ 500, /* nLookaside */ diff --git a/src/main.c b/src/main.c index 16294a6199..7ac4b6f851 100644 --- a/src/main.c +++ b/src/main.c @@ -475,6 +475,11 @@ int sqlite3_config(int op, ...){ break; } + case SQLITE_CONFIG_READONLY: { + sqlite3GlobalConfig.bReadOnly = va_arg(ap, int); + break; + } + default: { rc = SQLITE_ERROR; break; @@ -2187,7 +2192,8 @@ static int openDatabase( const char *zFilename, /* Database filename UTF-8 encoded */ sqlite3 **ppDb, /* OUT: Returned database handle */ unsigned int flags, /* Operational flags */ - const char *zVfs /* Name of the VFS to use */ + const char *zVfs, /* Name of the VFS to use */ + int defaultFlags /* Zero if opening via sqlite3_open_v2 */ ){ sqlite3 *db; /* Store allocated handle here */ int rc; /* Return code */ @@ -2257,6 +2263,16 @@ static int openDatabase( SQLITE_OPEN_WAL ); + /* Check for global read-only mode */ + if( sqlite3GlobalConfig.bReadOnly ){ + if( defaultFlags ){ + flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); + flags |= SQLITE_OPEN_READONLY; + }else if( flags & SQLITE_OPEN_READWRITE ){ + return SQLITE_READONLY; + } + } + /* Allocate the sqlite data structure */ db = sqlite3MallocZero( sizeof(sqlite3) ); if( db==0 ) goto opendb_out; @@ -2447,7 +2463,7 @@ int sqlite3_open( sqlite3 **ppDb ){ return openDatabase(zFilename, ppDb, - SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0); + SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0, 1); } int sqlite3_open_v2( const char *filename, /* Database filename (UTF-8) */ @@ -2455,7 +2471,7 @@ int sqlite3_open_v2( int flags, /* Flags */ const char *zVfs /* Name of VFS module to use */ ){ - return openDatabase(filename, ppDb, (unsigned int)flags, zVfs); + return openDatabase(filename, ppDb, (unsigned int)flags, zVfs, 0); } #ifndef SQLITE_OMIT_UTF16 @@ -2482,7 +2498,7 @@ int sqlite3_open16( zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8); if( zFilename8 ){ rc = openDatabase(zFilename8, ppDb, - SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0); + SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0, 1); assert( *ppDb || rc==SQLITE_NOMEM ); if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){ ENC(*ppDb) = SQLITE_UTF16NATIVE; diff --git a/src/sqlite.h.in b/src/sqlite.h.in index e66dcc72b3..ef0aa92ee3 100644 --- a/src/sqlite.h.in +++ b/src/sqlite.h.in @@ -1562,6 +1562,19 @@ struct sqlite3_mem_methods { ** disabled. The default value may be changed by compiling with the ** [SQLITE_USE_URI] symbol defined. ** +** [[SQLITE_CONFIG_READONLY]]
SQLITE_CONFIG_READONLY +**
This option takes a single argument of type int. If non-zero, then +** read-only mode for opening databases is globally enabled. If the parameter +** is zero, then read-only mode for opening databases is globally disabled. If +** read-only mode for opening databases is globally enabled, all databases +** opened by [sqlite3_open()], [sqlite3_open16()], or specified as part of +** [ATTACH] commands will be opened in read-only mode. Additionally, all calls +** to [sqlite3_open_v2()] must have the [SQLITE_OPEN_READONLY] flag set in the +** third argument; otherwise, a [SQLITE_READONLY] error will be returned. If it +** is globally disabled, [sqlite3_open()], [sqlite3_open16()], +** [sqlite3_open_v2()], and [ATTACH] commands will function normally. By +** default, read-only mode is globally disabled. +** ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]] **
SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE **
These options are obsolete and should not be used by new code. @@ -1587,6 +1600,7 @@ struct sqlite3_mem_methods { #define SQLITE_CONFIG_URI 17 /* int */ #define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */ #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */ +#define SQLITE_CONFIG_READONLY 20 /* int */ /* ** CAPI3REF: Database Connection Configuration Options diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 09163bf69c..002927b10e 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -2462,6 +2462,7 @@ struct Sqlite3Config { int bCoreMutex; /* True to enable core mutexing */ int bFullMutex; /* True to enable full mutexing */ int bOpenUri; /* True to interpret filenames as URIs */ + int bReadOnly; /* True to force read-only mode */ int mxStrlen; /* Maximum string length */ int szLookaside; /* Default lookaside buffer size */ int nLookaside; /* Default lookaside buffer count */ diff --git a/src/test_malloc.c b/src/test_malloc.c index f52894d9e5..b2aa848b53 100644 --- a/src/test_malloc.c +++ b/src/test_malloc.c @@ -1197,6 +1197,34 @@ static int test_config_uri( return TCL_OK; } +/* +** Usage: sqlite3_config_readonly BOOLEAN +** +** Enables or disables global read-only mode using SQLITE_CONFIG_READONLY. +*/ +static int test_config_readonly( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + int rc; + int bReadOnly; + + if( objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "BOOL"); + return TCL_ERROR; + } + if( Tcl_GetBooleanFromObj(interp, objv[1], &bReadOnly) ){ + return TCL_ERROR; + } + + rc = sqlite3_config(SQLITE_CONFIG_READONLY, bReadOnly); + Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE); + + return TCL_OK; +} + /* ** Usage: sqlite3_dump_memsys3 FILENAME ** sqlite3_dump_memsys5 FILENAME @@ -1447,6 +1475,7 @@ int Sqlitetest_malloc_Init(Tcl_Interp *interp){ { "sqlite3_config_lookaside", test_config_lookaside ,0 }, { "sqlite3_config_error", test_config_error ,0 }, { "sqlite3_config_uri", test_config_uri ,0 }, + { "sqlite3_config_readonly", test_config_readonly ,0 }, { "sqlite3_db_config_lookaside",test_db_config_lookaside ,0 }, { "sqlite3_dump_memsys3", test_dump_memsys3 ,3 }, { "sqlite3_dump_memsys5", test_dump_memsys3 ,5 }, diff --git a/test/openv2.test b/test/openv2.test index 2a9e22d6b0..1918c700fb 100644 --- a/test/openv2.test +++ b/test/openv2.test @@ -52,4 +52,42 @@ do_test openv2-2.2 { } {1 {attempt to write a readonly database}} +# Attempt to open a database with SQLITE_OPEN_READWRITE when the +# SQLITE_CONFIG_READONLY flag is enabled. +# +db close +sqlite3_shutdown +sqlite3_config_readonly 1 +sqlite3_initialize +autoinstall_test_functions + +do_test openv2-3.1 { + list [catch {sqlite3 db :memory:} msg] $msg +} {1 {attempt to write a readonly database}} +catch {db close} +do_test openv2-3.2 { + list [catch {sqlite3 db test.db} msg] $msg +} {1 {attempt to write a readonly database}} +catch {db close} +do_test openv2-3.3 { + list [catch {sqlite3 db :memory: -readonly 1} msg] $msg +} {0 {}} +catch {db close} +do_test openv2-3.4 { + list [catch {sqlite3 db test.db -readonly 1} msg] $msg +} {0 {}} +catch {db close} +do_test openv2-3.5 { + list [catch {sqlite3 db :memory: -readonly 0} msg] $msg +} {1 {attempt to write a readonly database}} +catch {db close} +do_test openv2-3.6 { + list [catch {sqlite3 db test.db -readonly 0} msg] $msg +} {1 {attempt to write a readonly database}} +catch {db close} + +sqlite3_shutdown +sqlite3_config_readonly 0 +sqlite3_initialize +autoinstall_test_functions finish_test