/ Check-in [0a694a0b]
Login

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

Overview
Comment:Change the supported URI options to "mode" and "cache".
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | uri
Files: files | file ages | folders
SHA1: 0a694a0b27e3ce251ce313cb5d19a7637c2fa309
User & Date: dan 2011-05-03 10:22:32
Context
2011-05-03
11:53
Allow only "localhost" and "" as authorities in URIs. Do not allow escapes (%HH) in the authority part of a URI. check-in: b8a0f1b5 user: dan tags: uri
10:22
Change the supported URI options to "mode" and "cache". check-in: 0a694a0b user: dan tags: uri
2011-05-02
17:41
Merge the latest trunk changes into uri branch. check-in: 7fdd0786 user: dan tags: uri
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/main.c.

1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
....
1860
1861
1862
1863
1864
1865
1866





1867
1868
1869
1870
1871
1872
1873
1874
....
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919

1920
1921
1922





1923
1924


1925




1926
1927







































1928
1929
1930
1931
1932
1933
1934
....
1938
1939
1940
1941
1942
1943
1944




1945
1946

1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
....
1969
1970
1971
1972
1973
1974
1975





















1976
1977
1978
1979
1980
1981
1982
....
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
  const char *zDefaultVfs,        /* VFS to use if no "vfs=xxx" query option */
  const char *zUri,               /* Nul-terminated URI to parse */
  int *pFlags,                    /* IN/OUT: SQLITE_OPEN_XXX flags */
  sqlite3_vfs **ppVfs,            /* OUT: VFS to use */ 
  char **pzFile,                  /* OUT: Filename component of URI */
  char **pzErrMsg                 /* OUT: Error message (if rc!=SQLITE_OK) */
){
  struct UriOption {
    const char *zOption;
    int mask;
  } aOpt [] = {
    { "vfs", 0 },
    { "readonly",     SQLITE_OPEN_READONLY },
    { "readwrite",    SQLITE_OPEN_READWRITE },
    { "create",       SQLITE_OPEN_CREATE },
    { "sharedcache",  SQLITE_OPEN_SHAREDCACHE },
    { "privatecache", SQLITE_OPEN_PRIVATECACHE }
  };

  int flags = *pFlags;
  const char *zVfs = zDefaultVfs;
  char *zFile;
  int nUri = sqlite3Strlen30(zUri);

  assert( *pzErrMsg==0 );

................................................................................
       && sqlite3Isxdigit(zUri[iIn]) 
       && sqlite3Isxdigit(zUri[iIn+1]) 
      ){
        int codepoint = (sqlite3HexToInt(zUri[iIn++]) << 4);
        codepoint += sqlite3HexToInt(zUri[iIn++]);

        assert( codepoint>=0 && codepoint<256 );





        if( codepoint==0 ){
          /* This branch is taken when "%00" appears within the URI. In this
          ** case we ignore all text in the remainder of the path, name or
          ** value currently being parsed. So ignore the current character
          ** and skip to the next "?", "=" or "&", as appropriate. */
          while( zUri[iIn] && zUri[iIn]!='#' 
              && (eState!=0 || zUri[iIn]!='?')
              && (eState!=1 || (zUri[iIn]!='=' && zUri[iIn]!='&'))
................................................................................
    if( eState==1 ) zFile[iOut++] = '\0';
    zFile[iOut++] = '\0';
    zFile[iOut++] = '\0';

    /* Check if there were any options specified that should be interpreted 
    ** here. Options that are interpreted here include "vfs" and those that
    ** correspond to flags that may be passed to the sqlite3_open_v2()
    ** method.  */
    zOpt = &zFile[sqlite3Strlen30(zFile)+1];
    while( zOpt[0] ){
      int nOpt = sqlite3Strlen30(zOpt);
      char *zVal = &zOpt[nOpt+1];
      int nVal = sqlite3Strlen30(zVal);
      int i;

      for(i=0; i<ArraySize(aOpt); i++){
        const char *z = aOpt[i].zOption;
        if( nOpt==sqlite3Strlen30(z) && 0==memcmp(zOpt, z, nOpt) ){
          int mask = aOpt[i].mask;
          if( mask==0 ){

            zVfs = zVal;
          }else{
            if( zVal[0]=='\0' || sqlite3GetBoolean(zVal) ){





              flags |= mask;
            }else{


              flags &= ~mask;




            }
          }







































        }
      }

      zOpt = &zVal[nVal+1];
    }

  }else{
................................................................................
    zFile[nUri] = '\0';
    zFile[nUri+1] = '\0';
  }

  *ppVfs = sqlite3_vfs_find(zVfs);
  if( *ppVfs==0 ){
    *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);




    sqlite3_free(zFile);
    return SQLITE_ERROR;

  }
  *pFlags = flags;
  *pzFile = zFile;
  return SQLITE_OK;
}


/*
** This routine does the work of opening a database on behalf of
** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"  
** is UTF-8 encoded.
................................................................................
  char *zErrMsg = 0;              /* Error message from sqlite3ParseUri() */

  *ppDb = 0;
#ifndef SQLITE_OMIT_AUTOINIT
  rc = sqlite3_initialize();
  if( rc ) return rc;
#endif






















  if( sqlite3GlobalConfig.bCoreMutex==0 ){
    isThreadsafe = 0;
  }else if( flags & SQLITE_OPEN_NOMUTEX ){
    isThreadsafe = 0;
  }else if( flags & SQLITE_OPEN_FULLMUTEX ){
    isThreadsafe = 1;
................................................................................
  /* Also add a UTF-8 case-insensitive collation sequence. */
  createCollation(db, "NOCASE", SQLITE_UTF8, SQLITE_COLL_NOCASE, 0,
                  nocaseCollatingFunc, 0);

  /* Parse the filename/URI argument. */
  rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
  if( rc!=SQLITE_OK ){
    sqlite3Error(db, rc, "%s", zErrMsg);
    sqlite3_free(zErrMsg);
    goto opendb_out;
  }

  /* Only allow sensible combinations of bits in the flags argument.  
  ** Throw an error if any non-sense combination is used.  If we
  ** do not block illegal combinations here, it could trigger
  ** assert() statements in deeper layers.  Sensible combinations
  ** are:
  **
  **  1:  SQLITE_OPEN_READONLY
  **  2:  SQLITE_OPEN_READWRITE
  **  6:  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
  */
  assert( SQLITE_OPEN_READONLY  == 0x01 );
  assert( SQLITE_OPEN_READWRITE == 0x02 );
  assert( SQLITE_OPEN_CREATE    == 0x04 );
  testcase( (1<<(flags&7))==0x02 ); /* READONLY */
  testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
  testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
  if( ((1<<(flags&7)) & 0x46)==0 ){
    rc = SQLITE_MISUSE;
    goto opendb_out;
  }

  /* Open the backend database driver */
  db->openFlags = flags;
  rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
                        flags | SQLITE_OPEN_MAIN_DB);
  if( rc!=SQLITE_OK ){
    if( rc==SQLITE_IOERR_NOMEM ){
      rc = SQLITE_NOMEM;







|
<
<
<
<
<
<
<
<
<
<
<







 







>
>
>
>
>
|







 







|





<

<
<
<
<
<
>
|
|
<
>
>
>
>
>
|
<
>
>
|
>
>
>
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>

<
>



|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|




<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







1799
1800
1801
1802
1803
1804
1805
1806











1807
1808
1809
1810
1811
1812
1813
....
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
....
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906

1907





1908
1909
1910

1911
1912
1913
1914
1915
1916

1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
....
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986

1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
....
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
....
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147





















2148
2149
2150
2151
2152
2153
2154
  const char *zDefaultVfs,        /* VFS to use if no "vfs=xxx" query option */
  const char *zUri,               /* Nul-terminated URI to parse */
  int *pFlags,                    /* IN/OUT: SQLITE_OPEN_XXX flags */
  sqlite3_vfs **ppVfs,            /* OUT: VFS to use */ 
  char **pzFile,                  /* OUT: Filename component of URI */
  char **pzErrMsg                 /* OUT: Error message (if rc!=SQLITE_OK) */
){
  int rc = SQLITE_OK;











  int flags = *pFlags;
  const char *zVfs = zDefaultVfs;
  char *zFile;
  int nUri = sqlite3Strlen30(zUri);

  assert( *pzErrMsg==0 );

................................................................................
       && sqlite3Isxdigit(zUri[iIn]) 
       && sqlite3Isxdigit(zUri[iIn+1]) 
      ){
        int codepoint = (sqlite3HexToInt(zUri[iIn++]) << 4);
        codepoint += sqlite3HexToInt(zUri[iIn++]);

        assert( codepoint>=0 && codepoint<256 );
        if( codepoint>=128 ){
          *pzErrMsg = sqlite3_mprintf("invalid uri escape: %.3s", &zUri[-3]);
          rc = SQLITE_ERROR;
          goto parse_uri_out;
        }
        else if( codepoint==0 ){
          /* This branch is taken when "%00" appears within the URI. In this
          ** case we ignore all text in the remainder of the path, name or
          ** value currently being parsed. So ignore the current character
          ** and skip to the next "?", "=" or "&", as appropriate. */
          while( zUri[iIn] && zUri[iIn]!='#' 
              && (eState!=0 || zUri[iIn]!='?')
              && (eState!=1 || (zUri[iIn]!='=' && zUri[iIn]!='&'))
................................................................................
    if( eState==1 ) zFile[iOut++] = '\0';
    zFile[iOut++] = '\0';
    zFile[iOut++] = '\0';

    /* Check if there were any options specified that should be interpreted 
    ** here. Options that are interpreted here include "vfs" and those that
    ** correspond to flags that may be passed to the sqlite3_open_v2()
    ** method. */
    zOpt = &zFile[sqlite3Strlen30(zFile)+1];
    while( zOpt[0] ){
      int nOpt = sqlite3Strlen30(zOpt);
      char *zVal = &zOpt[nOpt+1];
      int nVal = sqlite3Strlen30(zVal);







      if( nOpt==3 && sqlite3_strnicmp("vfs", zOpt, 3)==0 ){
        zVfs = zVal;
      }else{

        struct OpenMode {
          const char *z;
          int mode;
        } *aMode = 0;
        char *zModeType;
        int mask;

        int limit;

        if( nOpt==5 && sqlite3_strnicmp("cache", zOpt, 5)==0 ){
          static struct OpenMode aCacheMode[] = {
            { "shared",  SQLITE_OPEN_SHAREDCACHE },
            { "private", SQLITE_OPEN_PRIVATECACHE },
            { 0, 0 }
          };

          mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE;
          aMode = aCacheMode;
          limit = mask;
          zModeType = "cache";
        }
        if( nOpt==4 && sqlite3_strnicmp("mode", zOpt, 4)==0 ){
          static struct OpenMode aOpenMode[] = {
            { "ro",  SQLITE_OPEN_READONLY },
            { "rw",  SQLITE_OPEN_READWRITE }, 
            { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
            { 0, 0 }
          };

          mask = SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
          aMode = aOpenMode;
          limit = mask & flags;
          zModeType = "access";
        }

        if( aMode ){
          int i;
          int mode = 0;
          for(i=0; aMode[i].z; i++){
            const char *z = aMode[i].z;
            if( nVal==strlen(z) && 0==sqlite3_strnicmp(zVal, z, nVal) ){
              mode = aMode[i].mode;
              break;
            }
          }
          if( mode==0 ){
            *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
            rc = SQLITE_ERROR;
            goto parse_uri_out;
          }
          if( mode>limit ){
            rc = SQLITE_PERM;
            goto parse_uri_out;
          }
          flags = (flags & ~mask) | mode;
        }
      }

      zOpt = &zVal[nVal+1];
    }

  }else{
................................................................................
    zFile[nUri] = '\0';
    zFile[nUri+1] = '\0';
  }

  *ppVfs = sqlite3_vfs_find(zVfs);
  if( *ppVfs==0 ){
    *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
    rc = SQLITE_ERROR;
  }
 parse_uri_out:
  if( rc!=SQLITE_OK ){
    sqlite3_free(zFile);

    zFile = 0;
  }
  *pFlags = flags;
  *pzFile = zFile;
  return rc;
}


/*
** This routine does the work of opening a database on behalf of
** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"  
** is UTF-8 encoded.
................................................................................
  char *zErrMsg = 0;              /* Error message from sqlite3ParseUri() */

  *ppDb = 0;
#ifndef SQLITE_OMIT_AUTOINIT
  rc = sqlite3_initialize();
  if( rc ) return rc;
#endif

  /* Only allow sensible combinations of bits in the flags argument.  
  ** Throw an error if any non-sense combination is used.  If we
  ** do not block illegal combinations here, it could trigger
  ** assert() statements in deeper layers.  Sensible combinations
  ** are:
  **
  **  1:  SQLITE_OPEN_READONLY
  **  2:  SQLITE_OPEN_READWRITE
  **  6:  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
  */
  assert( SQLITE_OPEN_READONLY  == 0x01 );
  assert( SQLITE_OPEN_READWRITE == 0x02 );
  assert( SQLITE_OPEN_CREATE    == 0x04 );
  testcase( (1<<(flags&7))==0x02 ); /* READONLY */
  testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
  testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
  if( ((1<<(flags&7)) & 0x46)==0 ){
    rc = SQLITE_MISUSE;
    goto opendb_out;
  }

  if( sqlite3GlobalConfig.bCoreMutex==0 ){
    isThreadsafe = 0;
  }else if( flags & SQLITE_OPEN_NOMUTEX ){
    isThreadsafe = 0;
  }else if( flags & SQLITE_OPEN_FULLMUTEX ){
    isThreadsafe = 1;
................................................................................
  /* Also add a UTF-8 case-insensitive collation sequence. */
  createCollation(db, "NOCASE", SQLITE_UTF8, SQLITE_COLL_NOCASE, 0,
                  nocaseCollatingFunc, 0);

  /* Parse the filename/URI argument. */
  rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
  if( rc!=SQLITE_OK ){
    sqlite3Error(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
    sqlite3_free(zErrMsg);
    goto opendb_out;
  }






















  /* Open the backend database driver */
  db->openFlags = flags;
  rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
                        flags | SQLITE_OPEN_MAIN_DB);
  if( rc!=SQLITE_OK ){
    if( rc==SQLITE_IOERR_NOMEM ){
      rc = SQLITE_NOMEM;

Changes to test/uri.test.

113
114
115
116
117
118
119










120





121
122
123
124
125
126
127
128
129

130
131




132


133

















134









135
136



137
138
139







140
141
142
143
144
145
146
...
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
} {1 {no such vfs: nosuchvfs}}

#-------------------------------------------------------------------------
# Test some of the other options (other than "vfs").
#
# TODO: Fix this after the list of options is decided.
#










do_test 4.0 {





  sqlite3 db test.db
  db eval {CREATE TABLE t1(a, b)}
  db close
} {}
foreach {tn uri ro} {
  1    "file:test.db"                                 0
  2    "file:test.db?readonly=0"                      0
  3    "file:test.db?readonly=1&readwrite=0&create=0" 1
} {

  set RES(0) {0 {}}
  set RES(1) {1 {attempt to write a readonly database}}







  do_test 4.$tn {

















    sqlite3 db $uri









    catchsql { INSERT INTO t1 VALUES(1, 2) }
  } $RES($ro)



  db close
}








#-------------------------------------------------------------------------
# Test that things work if an ATTACHed database uses a different VFS than
# the main database. The important point is that for all operations 
# involving the ATTACHed database, the correct versions of the following
# VFS are used for all operations involving the attached database.
#
#     xOpen
................................................................................
  execsql {
    ATTACH 'file:test.db2?vfs=tvfs2' AS aux;
    PRAGMA main.journal_mode = PERSIST;
    PRAGMA aux.journal_mode = PERSIST;
    CREATE TABLE t1(a, b);
    CREATE TABLE aux.t2(a, b);
    PRAGMA main.journal_mode = WAL;
    PRAGMA aux.journal_mode = PERSIST;
    INSERT INTO t1 VALUES('x', 'y');
    INSERT INTO t2 VALUES('x', 'y');
  }
  lsort [array names ::T1]
} {test.db1 test.db1-journal test.db1-wal}

do_test 5.1.2 {







>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>

|

<
<
<
<
<
<
>
|
|
>
>
>
>

>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
|
<
>
>
>
|


>
>
>
>
>
>
>







 







|







113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138






139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
...
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
} {1 {no such vfs: nosuchvfs}}

#-------------------------------------------------------------------------
# Test some of the other options (other than "vfs").
#
# TODO: Fix this after the list of options is decided.
#
foreach {tn mode create_ok write_ok readonly_ok} {
  1    ro    0   0   1
  2    rw    0   1   0
  3    rwc   1   1   0
} {
  catch { db close }
  forcedelete test.db

  set A(1) {0 {}}
  set A(0) {1 {unable to open database file}}
  do_test 4.1.$tn.1 {
    list [catch {sqlite3 db "file:test.db?mode=$mode"} msg] $msg
  } $A($create_ok)

  catch { db close }
  forcedelete test.db
  sqlite3 db test.db
  db eval { CREATE TABLE t1(a, b) }
  db close







  set A(1) {0 {}}
  set A(0) {1 {attempt to write a readonly database}}
  do_test 4.1.$tn.2 {
    sqlite3 db "file:test.db?mode=$mode"
    catchsql { INSERT INTO t1 VALUES(1, 2) }
  } $A($write_ok)

  set A(1) {0 {}}
  set A(0) {1 {access permission denied}}
  do_test 4.1.$tn.3 {
    list [catch {sqlite3 db "file:test.db?mode=$mode" -readonly 1} msg] $msg
  } $A($readonly_ok)
}

set orig [sqlite3_enable_shared_cache]
foreach {tn options sc_default is_shared} {
  1    ""                1   1
  2    "cache=private"   1   0
  3    "cache=shared"    1   1
  4    ""                0   0
  5    "cache=private"   0   0
  6    "cache=shared"    0   1
} {
  catch { db close }
  forcedelete test.db

  sqlite3_enable_shared_cache 1
  sqlite3 db2 test.db
  db2 eval {CREATE TABLE t1(a, b)}

  sqlite3_enable_shared_cache $sc_default
  sqlite3 db "file:test.db?$options"
  db eval {SELECT * FROM t1}

  set A(1) {1 {database table is locked: t1}}
  set A(0) {0 {}}
  do_test 4.2.$tn {
    db2 eval {BEGIN; INSERT INTO t1 VALUES(1, 2);}

    catchsql { SELECT * FROM t1 }
  } $A($is_shared)

  db2 close
}

do_test 4.3.1 {
  list [catch {sqlite3 db "file:test.db?mode=rc"} msg] $msg
} {1 {no such access mode: rc}}
do_test 4.3.2 {
  list [catch {sqlite3 db "file:test.db?cache=public"} msg] $msg
} {1 {no such cache mode: public}}

#-------------------------------------------------------------------------
# Test that things work if an ATTACHed database uses a different VFS than
# the main database. The important point is that for all operations 
# involving the ATTACHed database, the correct versions of the following
# VFS are used for all operations involving the attached database.
#
#     xOpen
................................................................................
  execsql {
    ATTACH 'file:test.db2?vfs=tvfs2' AS aux;
    PRAGMA main.journal_mode = PERSIST;
    PRAGMA aux.journal_mode = PERSIST;
    CREATE TABLE t1(a, b);
    CREATE TABLE aux.t2(a, b);
    PRAGMA main.journal_mode = WAL;
    PRAGMA aux.journal_mode = WAL;
    INSERT INTO t1 VALUES('x', 'y');
    INSERT INTO t2 VALUES('x', 'y');
  }
  lsort [array names ::T1]
} {test.db1 test.db1-journal test.db1-wal}

do_test 5.1.2 {