/ Check-in [7c340165]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Initial prototype of SQLITE_CONFIG_READONLY.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | configReadOnly
Files: files | file ages | folders
SHA1:7c3401657a0410dd121d49742db102c6e7494964
User & Date: mistachkin 2012-09-10 10:29:06
Context
2012-09-25
12:45
Merge updates from trunk. check-in: 4a470741 user: mistachkin tags: configReadOnly
2012-09-10
10:29
Initial prototype of SQLITE_CONFIG_READONLY. check-in: 7c340165 user: mistachkin tags: configReadOnly
09:33
Avoid using the sqlite3ErrStr function in the Tcl package if USE_SYSTEM_SQLITE is defined. check-in: a716b9a3 user: mistachkin tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

Changes to src/global.c.

   138    138   ** the SQLite library.
   139    139   */
   140    140   SQLITE_WSD struct Sqlite3Config sqlite3Config = {
   141    141      SQLITE_DEFAULT_MEMSTATUS,  /* bMemstat */
   142    142      1,                         /* bCoreMutex */
   143    143      SQLITE_THREADSAFE==1,      /* bFullMutex */
   144    144      SQLITE_USE_URI,            /* bOpenUri */
          145  +   0,                         /* bReadOnly */
   145    146      0x7ffffffe,                /* mxStrlen */
   146    147      128,                       /* szLookaside */
   147    148      500,                       /* nLookaside */
   148    149      {0,0,0,0,0,0,0,0},         /* m */
   149    150      {0,0,0,0,0,0,0,0,0},       /* mutex */
   150    151      {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
   151    152      (void*)0,                  /* pHeap */

Changes to src/main.c.

   470    470         break;
   471    471       }
   472    472   
   473    473       case SQLITE_CONFIG_URI: {
   474    474         sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
   475    475         break;
   476    476       }
          477  +
          478  +    case SQLITE_CONFIG_READONLY: {
          479  +      sqlite3GlobalConfig.bReadOnly = va_arg(ap, int);
          480  +      break;
          481  +    }
   477    482   
   478    483       default: {
   479    484         rc = SQLITE_ERROR;
   480    485         break;
   481    486       }
   482    487     }
   483    488     va_end(ap);
................................................................................
  2183   2188   ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"  
  2184   2189   ** is UTF-8 encoded.
  2185   2190   */
  2186   2191   static int openDatabase(
  2187   2192     const char *zFilename, /* Database filename UTF-8 encoded */
  2188   2193     sqlite3 **ppDb,        /* OUT: Returned database handle */
  2189   2194     unsigned int flags,    /* Operational flags */
  2190         -  const char *zVfs       /* Name of the VFS to use */
         2195  +  const char *zVfs,      /* Name of the VFS to use */
         2196  +  int defaultFlags       /* Zero if opening via sqlite3_open_v2 */
  2191   2197   ){
  2192   2198     sqlite3 *db;                    /* Store allocated handle here */
  2193   2199     int rc;                         /* Return code */
  2194   2200     int isThreadsafe;               /* True for threadsafe connections */
  2195   2201     char *zOpen = 0;                /* Filename argument to pass to BtreeOpen() */
  2196   2202     char *zErrMsg = 0;              /* Error message from sqlite3ParseUri() */
  2197   2203   
................................................................................
  2252   2258                  SQLITE_OPEN_TEMP_JOURNAL | 
  2253   2259                  SQLITE_OPEN_SUBJOURNAL | 
  2254   2260                  SQLITE_OPEN_MASTER_JOURNAL |
  2255   2261                  SQLITE_OPEN_NOMUTEX |
  2256   2262                  SQLITE_OPEN_FULLMUTEX |
  2257   2263                  SQLITE_OPEN_WAL
  2258   2264                );
         2265  +
         2266  +  /* Check for global read-only mode */
         2267  +  if( sqlite3GlobalConfig.bReadOnly ){
         2268  +    if( defaultFlags ){
         2269  +      flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
         2270  +      flags |= SQLITE_OPEN_READONLY;
         2271  +    }else if( flags & SQLITE_OPEN_READWRITE ){
         2272  +      return SQLITE_READONLY;
         2273  +    }
         2274  +  }
  2259   2275   
  2260   2276     /* Allocate the sqlite data structure */
  2261   2277     db = sqlite3MallocZero( sizeof(sqlite3) );
  2262   2278     if( db==0 ) goto opendb_out;
  2263   2279     if( isThreadsafe ){
  2264   2280       db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
  2265   2281       if( db->mutex==0 ){
................................................................................
  2443   2459   ** Open a new database handle.
  2444   2460   */
  2445   2461   int sqlite3_open(
  2446   2462     const char *zFilename, 
  2447   2463     sqlite3 **ppDb 
  2448   2464   ){
  2449   2465     return openDatabase(zFilename, ppDb,
  2450         -                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
         2466  +                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0, 1);
  2451   2467   }
  2452   2468   int sqlite3_open_v2(
  2453   2469     const char *filename,   /* Database filename (UTF-8) */
  2454   2470     sqlite3 **ppDb,         /* OUT: SQLite db handle */
  2455   2471     int flags,              /* Flags */
  2456   2472     const char *zVfs        /* Name of VFS module to use */
  2457   2473   ){
  2458         -  return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
         2474  +  return openDatabase(filename, ppDb, (unsigned int)flags, zVfs, 0);
  2459   2475   }
  2460   2476   
  2461   2477   #ifndef SQLITE_OMIT_UTF16
  2462   2478   /*
  2463   2479   ** Open a new database handle.
  2464   2480   */
  2465   2481   int sqlite3_open16(
................................................................................
  2478   2494     if( rc ) return rc;
  2479   2495   #endif
  2480   2496     pVal = sqlite3ValueNew(0);
  2481   2497     sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
  2482   2498     zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
  2483   2499     if( zFilename8 ){
  2484   2500       rc = openDatabase(zFilename8, ppDb,
  2485         -                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
         2501  +                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0, 1);
  2486   2502       assert( *ppDb || rc==SQLITE_NOMEM );
  2487   2503       if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
  2488   2504         ENC(*ppDb) = SQLITE_UTF16NATIVE;
  2489   2505       }
  2490   2506     }else{
  2491   2507       rc = SQLITE_NOMEM;
  2492   2508     }

Changes to src/sqlite.h.in.

  1557   1557   ** specified as part of [ATTACH] commands are interpreted as URIs, regardless
  1558   1558   ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
  1559   1559   ** connection is opened. If it is globally disabled, filenames are
  1560   1560   ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
  1561   1561   ** database connection is opened. By default, URI handling is globally
  1562   1562   ** disabled. The default value may be changed by compiling with the
  1563   1563   ** [SQLITE_USE_URI] symbol defined.
         1564  +**
         1565  +** [[SQLITE_CONFIG_READONLY]] <dt>SQLITE_CONFIG_READONLY
         1566  +** <dd> This option takes a single argument of type int. If non-zero, then
         1567  +** read-only mode for opening databases is globally enabled. If the parameter
         1568  +** is zero, then read-only mode for opening databases is globally disabled. If
         1569  +** read-only mode for opening databases is globally enabled, all databases
         1570  +** opened by [sqlite3_open()], [sqlite3_open16()], or specified as part of
         1571  +** [ATTACH] commands will be opened in read-only mode. Additionally, all calls
         1572  +** to [sqlite3_open_v2()] must have the [SQLITE_OPEN_READONLY] flag set in the
         1573  +** third argument; otherwise, a [SQLITE_READONLY] error will be returned. If it
         1574  +** is globally disabled, [sqlite3_open()], [sqlite3_open16()],
         1575  +** [sqlite3_open_v2()], and [ATTACH] commands will function normally. By
         1576  +** default, read-only mode is globally disabled.
  1564   1577   **
  1565   1578   ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
  1566   1579   ** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
  1567   1580   ** <dd> These options are obsolete and should not be used by new code.
  1568   1581   ** They are retained for backwards compatibility but are now no-ops.
  1569   1582   ** </dl>
  1570   1583   */
................................................................................
  1583   1596   #define SQLITE_CONFIG_LOOKASIDE    13  /* int int */
  1584   1597   #define SQLITE_CONFIG_PCACHE       14  /* no-op */
  1585   1598   #define SQLITE_CONFIG_GETPCACHE    15  /* no-op */
  1586   1599   #define SQLITE_CONFIG_LOG          16  /* xFunc, void* */
  1587   1600   #define SQLITE_CONFIG_URI          17  /* int */
  1588   1601   #define SQLITE_CONFIG_PCACHE2      18  /* sqlite3_pcache_methods2* */
  1589   1602   #define SQLITE_CONFIG_GETPCACHE2   19  /* sqlite3_pcache_methods2* */
         1603  +#define SQLITE_CONFIG_READONLY     20  /* int */
  1590   1604   
  1591   1605   /*
  1592   1606   ** CAPI3REF: Database Connection Configuration Options
  1593   1607   **
  1594   1608   ** These constants are the available integer configuration options that
  1595   1609   ** can be passed as the second argument to the [sqlite3_db_config()] interface.
  1596   1610   **

Changes to src/sqliteInt.h.

  2458   2458   ** This structure also contains some state information.
  2459   2459   */
  2460   2460   struct Sqlite3Config {
  2461   2461     int bMemstat;                     /* True to enable memory status */
  2462   2462     int bCoreMutex;                   /* True to enable core mutexing */
  2463   2463     int bFullMutex;                   /* True to enable full mutexing */
  2464   2464     int bOpenUri;                     /* True to interpret filenames as URIs */
         2465  +  int bReadOnly;                    /* True to force read-only mode */
  2465   2466     int mxStrlen;                     /* Maximum string length */
  2466   2467     int szLookaside;                  /* Default lookaside buffer size */
  2467   2468     int nLookaside;                   /* Default lookaside buffer count */
  2468   2469     sqlite3_mem_methods m;            /* Low-level memory allocation interface */
  2469   2470     sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
  2470   2471     sqlite3_pcache_methods2 pcache2;  /* Low-level page-cache interface */
  2471   2472     void *pHeap;                      /* Heap storage space */

Changes to src/test_malloc.c.

  1192   1192     }
  1193   1193   
  1194   1194     rc = sqlite3_config(SQLITE_CONFIG_URI, bOpenUri);
  1195   1195     Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
  1196   1196   
  1197   1197     return TCL_OK;
  1198   1198   }
         1199  +
         1200  +/*
         1201  +** Usage:    sqlite3_config_readonly  BOOLEAN
         1202  +**
         1203  +** Enables or disables global read-only mode using SQLITE_CONFIG_READONLY.
         1204  +*/
         1205  +static int test_config_readonly(
         1206  +  void * clientData, 
         1207  +  Tcl_Interp *interp,
         1208  +  int objc,
         1209  +  Tcl_Obj *CONST objv[]
         1210  +){
         1211  +  int rc;
         1212  +  int bReadOnly;
         1213  +
         1214  +  if( objc!=2 ){
         1215  +    Tcl_WrongNumArgs(interp, 1, objv, "BOOL");
         1216  +    return TCL_ERROR;
         1217  +  }
         1218  +  if( Tcl_GetBooleanFromObj(interp, objv[1], &bReadOnly) ){
         1219  +    return TCL_ERROR;
         1220  +  }
         1221  +
         1222  +  rc = sqlite3_config(SQLITE_CONFIG_READONLY, bReadOnly);
         1223  +  Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
         1224  +
         1225  +  return TCL_OK;
         1226  +}
  1199   1227   
  1200   1228   /*
  1201   1229   ** Usage:    sqlite3_dump_memsys3  FILENAME
  1202   1230   **           sqlite3_dump_memsys5  FILENAME
  1203   1231   **
  1204   1232   ** Write a summary of unfreed memsys3 allocations to FILENAME.
  1205   1233   */
................................................................................
  1443   1471        { "sqlite3_db_status",          test_db_status                ,0 },
  1444   1472        { "install_malloc_faultsim",    test_install_malloc_faultsim  ,0 },
  1445   1473        { "sqlite3_config_heap",        test_config_heap              ,0 },
  1446   1474        { "sqlite3_config_memstatus",   test_config_memstatus         ,0 },
  1447   1475        { "sqlite3_config_lookaside",   test_config_lookaside         ,0 },
  1448   1476        { "sqlite3_config_error",       test_config_error             ,0 },
  1449   1477        { "sqlite3_config_uri",         test_config_uri               ,0 },
         1478  +     { "sqlite3_config_readonly",    test_config_readonly          ,0 },
  1450   1479        { "sqlite3_db_config_lookaside",test_db_config_lookaside      ,0 },
  1451   1480        { "sqlite3_dump_memsys3",       test_dump_memsys3             ,3 },
  1452   1481        { "sqlite3_dump_memsys5",       test_dump_memsys3             ,5 },
  1453   1482        { "sqlite3_install_memsys3",    test_install_memsys3          ,0 },
  1454   1483        { "sqlite3_memdebug_vfs_oom_test", test_vfs_oom_test          ,0 },
  1455   1484     };
  1456   1485     int i;

Changes to test/openv2.test.

    48     48     db eval {SELECT * FROM sqlite_master}
    49     49   } {}
    50     50   do_test openv2-2.2 {
    51     51     catchsql {CREATE TABLE t1(x)}
    52     52   } {1 {attempt to write a readonly database}}
    53     53   
    54     54   
           55  +# Attempt to open a database with SQLITE_OPEN_READWRITE when the
           56  +# SQLITE_CONFIG_READONLY flag is enabled.
           57  +#
           58  +db close
           59  +sqlite3_shutdown
           60  +sqlite3_config_readonly 1
           61  +sqlite3_initialize
           62  +autoinstall_test_functions
           63  +
           64  +do_test openv2-3.1 {
           65  +  list [catch {sqlite3 db :memory:} msg] $msg
           66  +} {1 {attempt to write a readonly database}}
           67  +catch {db close}
           68  +do_test openv2-3.2 {
           69  +  list [catch {sqlite3 db test.db} msg] $msg
           70  +} {1 {attempt to write a readonly database}}
           71  +catch {db close}
           72  +do_test openv2-3.3 {
           73  +  list [catch {sqlite3 db :memory: -readonly 1} msg] $msg
           74  +} {0 {}}
           75  +catch {db close}
           76  +do_test openv2-3.4 {
           77  +  list [catch {sqlite3 db test.db -readonly 1} msg] $msg
           78  +} {0 {}}
           79  +catch {db close}
           80  +do_test openv2-3.5 {
           81  +  list [catch {sqlite3 db :memory: -readonly 0} msg] $msg
           82  +} {1 {attempt to write a readonly database}}
           83  +catch {db close}
           84  +do_test openv2-3.6 {
           85  +  list [catch {sqlite3 db test.db -readonly 0} msg] $msg
           86  +} {1 {attempt to write a readonly database}}
           87  +catch {db close}
           88  +
           89  +sqlite3_shutdown
           90  +sqlite3_config_readonly 0
           91  +sqlite3_initialize
           92  +autoinstall_test_functions
    55     93   finish_test