System.Data.SQLite
Check-in [1cb02b0a7b]
Not logged in

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

Overview
Comment:Pre 3.7.6 check-in for testing.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 1cb02b0a7bd24de9db576dbadd9ad634b1200b1d
User & Date: shaneh 2011-04-12 05:24:29
Context
2011-04-12
05:26
Added documentation for building. check-in: f8c5d3f07e user: shaneh tags: trunk
05:24
Pre 3.7.6 check-in for testing. check-in: 1cb02b0a7b user: shaneh tags: trunk
2011-04-10
01:50
Make sure all new build system metadata files are accessible in the IDE via the solutions. check-in: 191aeaf73e user: mistachkin tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to SQLite.Interop/src/core/sqlite3.c.

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.7.5.  By combining all the individual C code files into this 
     4         -** single large file, the entire code can be compiled as a one translation
            3  +** version 3.7.6.  By combining all the individual C code files into this 
            4  +** single large file, the entire code can be compiled as a single translation
     5      5   ** unit.  This allows many compilers to do optimizations that would not be
     6      6   ** possible if the files were compiled separately.  Performance improvements
     7      7   ** of 5% or more are commonly seen when SQLite is compiled as a single
     8      8   ** translation unit.
     9      9   **
    10     10   ** This file is all you need to compile SQLite.  To use SQLite in other
    11     11   ** programs, you need this file and the "sqlite3.h" header file that defines
................................................................................
   196    196   */
   197    197   #ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
   198    198   # define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT  1000
   199    199   #endif
   200    200   
   201    201   /*
   202    202   ** The maximum number of attached databases.  This must be between 0
   203         -** and 30.  The upper bound on 30 is because a 32-bit integer bitmap
          203  +** and 62.  The upper bound on 62 is because a 64-bit integer bitmap
   204    204   ** is used internally to track attached databases.
   205    205   */
   206    206   #ifndef SQLITE_MAX_ATTACHED
   207    207   # define SQLITE_MAX_ATTACHED 10
   208    208   #endif
   209    209   
   210    210   
................................................................................
   646    646   ** string contains the date and time of the check-in (UTC) and an SHA1
   647    647   ** hash of the entire source tree.
   648    648   **
   649    649   ** See also: [sqlite3_libversion()],
   650    650   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   651    651   ** [sqlite_version()] and [sqlite_source_id()].
   652    652   */
   653         -#define SQLITE_VERSION        "3.7.5"
   654         -#define SQLITE_VERSION_NUMBER 3007005
   655         -#define SQLITE_SOURCE_ID      "2011-01-28 17:03:50 ed759d5a9edb3bba5f48f243df47be29e3fe8cd7"
          653  +#define SQLITE_VERSION        "3.7.6"
          654  +#define SQLITE_VERSION_NUMBER 3007006
          655  +#define SQLITE_SOURCE_ID      "2011-04-11 18:35:09 51029d8430d2dbc782f161577d47e3dd11c4e4d7"
   656    656   
   657    657   /*
   658    658   ** CAPI3REF: Run-Time Library Version Numbers
   659    659   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   660    660   **
   661    661   ** These interfaces provide the same information as the [SQLITE_VERSION],
   662    662   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  1022   1022   #define SQLITE_OPEN_MASTER_JOURNAL   0x00004000  /* VFS only */
  1023   1023   #define SQLITE_OPEN_NOMUTEX          0x00008000  /* Ok for sqlite3_open_v2() */
  1024   1024   #define SQLITE_OPEN_FULLMUTEX        0x00010000  /* Ok for sqlite3_open_v2() */
  1025   1025   #define SQLITE_OPEN_SHAREDCACHE      0x00020000  /* Ok for sqlite3_open_v2() */
  1026   1026   #define SQLITE_OPEN_PRIVATECACHE     0x00040000  /* Ok for sqlite3_open_v2() */
  1027   1027   #define SQLITE_OPEN_WAL              0x00080000  /* VFS only */
  1028   1028   
         1029  +/* Reserved:                         0x00F00000 */
         1030  +
  1029   1031   /*
  1030   1032   ** CAPI3REF: Device Characteristics
  1031   1033   **
  1032   1034   ** The xDeviceCharacteristics method of the [sqlite3_io_methods]
  1033   1035   ** object returns an integer which is a vector of the these
  1034   1036   ** bit values expressing I/O characteristics of the mass storage
  1035   1037   ** device that holds the file that the [sqlite3_io_methods]
................................................................................
  1269   1271   ** ^(The [SQLITE_FCNTL_SYNC_OMITTED] opcode is generated internally by
  1270   1272   ** SQLite and sent to all VFSes in place of a call to the xSync method
  1271   1273   ** when the database connection has [PRAGMA synchronous] set to OFF.)^
  1272   1274   ** Some specialized VFSes need this signal in order to operate correctly
  1273   1275   ** when [PRAGMA synchronous | PRAGMA synchronous=OFF] is set, but most 
  1274   1276   ** VFSes do not need this signal and should silently ignore this opcode.
  1275   1277   ** Applications should not call [sqlite3_file_control()] with this
  1276         -** opcode as doing so may disrupt the operation of the specilized VFSes
         1278  +** opcode as doing so may disrupt the operation of the specialized VFSes
  1277   1279   ** that do require it.  
  1278   1280   */
  1279   1281   #define SQLITE_FCNTL_LOCKSTATE        1
  1280   1282   #define SQLITE_GET_LOCKPROXYFILE      2
  1281   1283   #define SQLITE_SET_LOCKPROXYFILE      3
  1282   1284   #define SQLITE_LAST_ERRNO             4
  1283   1285   #define SQLITE_FCNTL_SIZE_HINT        5
................................................................................
  1435   1437   ** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
  1436   1438   ** Day Number multipled by 86400000 (the number of milliseconds in 
  1437   1439   ** a 24-hour day).  
  1438   1440   ** ^SQLite will use the xCurrentTimeInt64() method to get the current
  1439   1441   ** date and time if that method is available (if iVersion is 2 or 
  1440   1442   ** greater and the function pointer is not NULL) and will fall back
  1441   1443   ** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
         1444  +**
         1445  +** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
         1446  +** are not used by the SQLite core.  These optional interfaces are provided
         1447  +** by some VFSes to facilitate testing of the VFS code. By overriding 
         1448  +** system calls with functions under its control, a test program can
         1449  +** simulate faults and error conditions that would otherwise be difficult
         1450  +** or impossible to induce.  The set of system calls that can be overridden
         1451  +** varies from one VFS to another, and from one version of the same VFS to the
         1452  +** next.  Applications that use these interfaces must be prepared for any
         1453  +** or all of these interfaces to be NULL or for their behavior to change
         1454  +** from one release to the next.  Applications must not attempt to access
         1455  +** any of these methods if the iVersion of the VFS is less than 3.
  1442   1456   */
  1443   1457   typedef struct sqlite3_vfs sqlite3_vfs;
         1458  +typedef void (*sqlite3_syscall_ptr)(void);
  1444   1459   struct sqlite3_vfs {
  1445         -  int iVersion;            /* Structure version number (currently 2) */
         1460  +  int iVersion;            /* Structure version number (currently 3) */
  1446   1461     int szOsFile;            /* Size of subclassed sqlite3_file */
  1447   1462     int mxPathname;          /* Maximum file pathname length */
  1448   1463     sqlite3_vfs *pNext;      /* Next registered VFS */
  1449   1464     const char *zName;       /* Name of this virtual file system */
  1450   1465     void *pAppData;          /* Pointer to application-specific data */
  1451   1466     int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
  1452   1467                  int flags, int *pOutFlags);
................................................................................
  1464   1479     /*
  1465   1480     ** The methods above are in version 1 of the sqlite_vfs object
  1466   1481     ** definition.  Those that follow are added in version 2 or later
  1467   1482     */
  1468   1483     int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
  1469   1484     /*
  1470   1485     ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
         1486  +  ** Those below are for version 3 and greater.
         1487  +  */
         1488  +  int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
         1489  +  sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
         1490  +  const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
         1491  +  /*
         1492  +  ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
  1471   1493     ** New fields may be appended in figure versions.  The iVersion
  1472   1494     ** value will increment whenever this happens. 
  1473   1495     */
  1474   1496   };
  1475   1497   
  1476   1498   /*
  1477   1499   ** CAPI3REF: Flags for the xAccess VFS method
................................................................................
  1648   1670   
  1649   1671   /*
  1650   1672   ** CAPI3REF: Configure database connections
  1651   1673   **
  1652   1674   ** The sqlite3_db_config() interface is used to make configuration
  1653   1675   ** changes to a [database connection].  The interface is similar to
  1654   1676   ** [sqlite3_config()] except that the changes apply to a single
  1655         -** [database connection] (specified in the first argument).  The
  1656         -** sqlite3_db_config() interface should only be used immediately after
  1657         -** the database connection is created using [sqlite3_open()],
  1658         -** [sqlite3_open16()], or [sqlite3_open_v2()].  
         1677  +** [database connection] (specified in the first argument).
  1659   1678   **
  1660   1679   ** The second argument to sqlite3_db_config(D,V,...)  is the
  1661         -** configuration verb - an integer code that indicates what
  1662         -** aspect of the [database connection] is being configured.
  1663         -** The only choice for this value is [SQLITE_DBCONFIG_LOOKASIDE].
  1664         -** New verbs are likely to be added in future releases of SQLite.
  1665         -** Additional arguments depend on the verb.
         1680  +** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code 
         1681  +** that indicates what aspect of the [database connection] is being configured.
         1682  +** Subsequent arguments vary depending on the configuration verb.
  1666   1683   **
  1667   1684   ** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
  1668   1685   ** the call is considered successful.
  1669   1686   */
  1670   1687   SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
  1671   1688   
  1672   1689   /*
................................................................................
  1836   1853   ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
  1837   1854   ** allocation statistics are disabled by default.
  1838   1855   ** </dd>
  1839   1856   **
  1840   1857   ** <dt>SQLITE_CONFIG_SCRATCH</dt>
  1841   1858   ** <dd> ^This option specifies a static memory buffer that SQLite can use for
  1842   1859   ** scratch memory.  There are three arguments:  A pointer an 8-byte
  1843         -** aligned memory buffer from which the scrach allocations will be
         1860  +** aligned memory buffer from which the scratch allocations will be
  1844   1861   ** drawn, the size of each scratch allocation (sz),
  1845   1862   ** and the maximum number of scratch allocations (N).  The sz
  1846   1863   ** argument must be a multiple of 16.
  1847   1864   ** The first argument must be a pointer to an 8-byte aligned buffer
  1848   1865   ** of at least sz*N bytes of memory.
  1849   1866   ** ^SQLite will use no more than two scratch buffers per thread.  So
  1850   1867   ** N should be set to twice the expected maximum number of threads.
................................................................................
  1883   1900   ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
  1884   1901   ** to using its default memory allocator (the system malloc() implementation),
  1885   1902   ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC].  ^If the
  1886   1903   ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
  1887   1904   ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
  1888   1905   ** allocator is engaged to handle all of SQLites memory allocation needs.
  1889   1906   ** The first pointer (the memory pointer) must be aligned to an 8-byte
  1890         -** boundary or subsequent behavior of SQLite will be undefined.</dd>
         1907  +** boundary or subsequent behavior of SQLite will be undefined.
         1908  +** The minimum allocation size is capped at 2^12. Reasonable values
         1909  +** for the minimum allocation size are 2^5 through 2^8.</dd>
  1891   1910   **
  1892   1911   ** <dt>SQLITE_CONFIG_MUTEX</dt>
  1893   1912   ** <dd> ^(This option takes a single argument which is a pointer to an
  1894   1913   ** instance of the [sqlite3_mutex_methods] structure.  The argument specifies
  1895   1914   ** alternative low-level mutex routines to be used in place
  1896   1915   ** the mutex routines built into SQLite.)^  ^SQLite makes a copy of the
  1897   1916   ** content of the [sqlite3_mutex_methods] structure before the call to
................................................................................
  1986   2005   ** is invoked.
  1987   2006   **
  1988   2007   ** <dl>
  1989   2008   ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
  1990   2009   ** <dd> ^This option takes three additional arguments that determine the 
  1991   2010   ** [lookaside memory allocator] configuration for the [database connection].
  1992   2011   ** ^The first argument (the third parameter to [sqlite3_db_config()] is a
  1993         -** pointer to an memory buffer to use for lookaside memory.
         2012  +** pointer to a memory buffer to use for lookaside memory.
  1994   2013   ** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
  1995   2014   ** may be NULL in which case SQLite will allocate the
  1996   2015   ** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
  1997   2016   ** size of each lookaside buffer slot.  ^The third argument is the number of
  1998   2017   ** slots.  The size of the buffer in the first argument must be greater than
  1999   2018   ** or equal to the product of the second and third arguments.  The buffer
  2000   2019   ** must be aligned to an 8-byte boundary.  ^If the second argument to
................................................................................
  2004   2023   ** connection is not currently using lookaside memory, or in other words
  2005   2024   ** when the "current value" returned by
  2006   2025   ** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
  2007   2026   ** Any attempt to change the lookaside memory configuration when lookaside
  2008   2027   ** memory is in use leaves the configuration unchanged and returns 
  2009   2028   ** [SQLITE_BUSY].)^</dd>
  2010   2029   **
         2030  +** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
         2031  +** <dd> ^This option is used to enable or disable the enforcement of
         2032  +** [foreign key constraints].  There should be two additional arguments.
         2033  +** The first argument is an integer which is 0 to disable FK enforcement,
         2034  +** positive to enable FK enforcement or negative to leave FK enforcement
         2035  +** unchanged.  The second parameter is a pointer to an integer into which
         2036  +** is written 0 or 1 to indicate whether FK enforcement is off or on
         2037  +** following this call.  The second parameter may be a NULL pointer, in
         2038  +** which case the FK enforcement setting is not reported back. </dd>
         2039  +**
         2040  +** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
         2041  +** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
         2042  +** There should be two additional arguments.
         2043  +** The first argument is an integer which is 0 to disable triggers,
         2044  +** positive to enable triggers or negative to leave the setting unchanged.
         2045  +** The second parameter is a pointer to an integer into which
         2046  +** is written 0 or 1 to indicate whether triggers are disabled or enabled
         2047  +** following this call.  The second parameter may be a NULL pointer, in
         2048  +** which case the trigger setting is not reported back. </dd>
         2049  +**
  2011   2050   ** </dl>
  2012   2051   */
  2013         -#define SQLITE_DBCONFIG_LOOKASIDE    1001  /* void* int int */
         2052  +#define SQLITE_DBCONFIG_LOOKASIDE       1001  /* void* int int */
         2053  +#define SQLITE_DBCONFIG_ENABLE_FKEY     1002  /* int int* */
         2054  +#define SQLITE_DBCONFIG_ENABLE_TRIGGER  1003  /* int int* */
  2014   2055   
  2015   2056   
  2016   2057   /*
  2017   2058   ** CAPI3REF: Enable Or Disable Extended Result Codes
  2018   2059   **
  2019   2060   ** ^The sqlite3_extended_result_codes() routine enables or disables the
  2020   2061   ** [extended result codes] feature of SQLite. ^The extended result
................................................................................
  2608   2649   ** method.
  2609   2650   */
  2610   2651   SQLITE_API void sqlite3_randomness(int N, void *P);
  2611   2652   
  2612   2653   /*
  2613   2654   ** CAPI3REF: Compile-Time Authorization Callbacks
  2614   2655   **
  2615         -** ^This routine registers a authorizer callback with a particular
         2656  +** ^This routine registers an authorizer callback with a particular
  2616   2657   ** [database connection], supplied in the first argument.
  2617   2658   ** ^The authorizer callback is invoked as SQL statements are being compiled
  2618   2659   ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
  2619   2660   ** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()].  ^At various
  2620   2661   ** points during the compilation process, as logic is being created
  2621   2662   ** to perform various actions, the authorizer callback is invoked to
  2622   2663   ** see if those actions are allowed.  ^The authorizer callback should
................................................................................
  3210   3251   ** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
  3211   3252   */
  3212   3253   SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
  3213   3254   
  3214   3255   /*
  3215   3256   ** CAPI3REF: Determine If An SQL Statement Writes The Database
  3216   3257   **
  3217         -** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if 
         3258  +** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
  3218   3259   ** and only if the [prepared statement] X makes no direct changes to
  3219   3260   ** the content of the database file.
  3220   3261   **
  3221   3262   ** Note that [application-defined SQL functions] or
  3222   3263   ** [virtual tables] might change the database indirectly as a side effect.  
  3223   3264   ** ^(For example, if an application defines a function "eval()" that 
  3224   3265   ** calls [sqlite3_exec()], then the following SQL statement would
................................................................................
  3254   3295   ** An sqlite3_value object may be either "protected" or "unprotected".
  3255   3296   ** Some interfaces require a protected sqlite3_value.  Other interfaces
  3256   3297   ** will accept either a protected or an unprotected sqlite3_value.
  3257   3298   ** Every interface that accepts sqlite3_value arguments specifies
  3258   3299   ** whether or not it requires a protected sqlite3_value.
  3259   3300   **
  3260   3301   ** The terms "protected" and "unprotected" refer to whether or not
  3261         -** a mutex is held.  A internal mutex is held for a protected
         3302  +** a mutex is held.  An internal mutex is held for a protected
  3262   3303   ** sqlite3_value object but no mutex is held for an unprotected
  3263   3304   ** sqlite3_value object.  If SQLite is compiled to be single-threaded
  3264   3305   ** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
  3265   3306   ** or if SQLite is run in one of reduced mutex modes 
  3266   3307   ** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
  3267   3308   ** then there is no distinction between protected and unprotected
  3268   3309   ** sqlite3_value objects and they can be used interchangeably.  However,
................................................................................
  3478   3519   ** interface returns a pointer to a zero-terminated UTF-8 string
  3479   3520   ** and sqlite3_column_name16() returns a pointer to a zero-terminated
  3480   3521   ** UTF-16 string.  ^The first parameter is the [prepared statement]
  3481   3522   ** that implements the [SELECT] statement. ^The second parameter is the
  3482   3523   ** column number.  ^The leftmost column is number 0.
  3483   3524   **
  3484   3525   ** ^The returned string pointer is valid until either the [prepared statement]
  3485         -** is destroyed by [sqlite3_finalize()] or until the next call to
         3526  +** is destroyed by [sqlite3_finalize()] or until the statement is automatically
         3527  +** reprepared by the first call to [sqlite3_step()] for a particular run
         3528  +** or until the next call to
  3486   3529   ** sqlite3_column_name() or sqlite3_column_name16() on the same column.
  3487   3530   **
  3488   3531   ** ^If sqlite3_malloc() fails during the processing of either routine
  3489   3532   ** (for example during a conversion from UTF-8 to UTF-16) then a
  3490   3533   ** NULL pointer is returned.
  3491   3534   **
  3492   3535   ** ^The name of a result column is the value of the "AS" clause for
................................................................................
  3504   3547   ** table column that is the origin of a particular result column in
  3505   3548   ** [SELECT] statement.
  3506   3549   ** ^The name of the database or table or column can be returned as
  3507   3550   ** either a UTF-8 or UTF-16 string.  ^The _database_ routines return
  3508   3551   ** the database name, the _table_ routines return the table name, and
  3509   3552   ** the origin_ routines return the column name.
  3510   3553   ** ^The returned string is valid until the [prepared statement] is destroyed
  3511         -** using [sqlite3_finalize()] or until the same information is requested
         3554  +** using [sqlite3_finalize()] or until the statement is automatically
         3555  +** reprepared by the first call to [sqlite3_step()] for a particular run
         3556  +** or until the same information is requested
  3512   3557   ** again in a different encoding.
  3513   3558   **
  3514   3559   ** ^The names returned are the original un-aliased names of the
  3515   3560   ** database, table, and column.
  3516   3561   **
  3517   3562   ** ^The first argument to these interfaces is a [prepared statement].
  3518   3563   ** ^These functions return information about the Nth result column returned by
................................................................................
  3936   3981   ** KEYWORDS: {application-defined SQL function}
  3937   3982   ** KEYWORDS: {application-defined SQL functions}
  3938   3983   **
  3939   3984   ** ^These functions (collectively known as "function creation routines")
  3940   3985   ** are used to add SQL functions or aggregates or to redefine the behavior
  3941   3986   ** of existing SQL functions or aggregates.  The only differences between
  3942   3987   ** these routines are the text encoding expected for
  3943         -** the the second parameter (the name of the function being created)
         3988  +** the second parameter (the name of the function being created)
  3944   3989   ** and the presence or absence of a destructor callback for
  3945   3990   ** the application data pointer.
  3946   3991   **
  3947   3992   ** ^The first parameter is the [database connection] to which the SQL
  3948   3993   ** function is to be added.  ^If an application uses more than one database
  3949   3994   ** connection then application-defined SQL functions must be added
  3950   3995   ** to each database connection separately.
................................................................................
  3981   4026   **
  3982   4027   ** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are
  3983   4028   ** pointers to C-language functions that implement the SQL function or
  3984   4029   ** aggregate. ^A scalar SQL function requires an implementation of the xFunc
  3985   4030   ** callback only; NULL pointers must be passed as the xStep and xFinal
  3986   4031   ** parameters. ^An aggregate SQL function requires an implementation of xStep
  3987   4032   ** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
  3988         -** SQL function or aggregate, pass NULL poiners for all three function
         4033  +** SQL function or aggregate, pass NULL pointers for all three function
  3989   4034   ** callbacks.
  3990   4035   **
  3991   4036   ** ^(If the ninth parameter to sqlite3_create_function_v2() is not NULL,
  3992   4037   ** then it is destructor for the application data pointer. 
  3993   4038   ** The destructor is invoked when the function is deleted, either by being
  3994   4039   ** overloaded or when the database connection closes.)^
  3995   4040   ** ^The destructor is also invoked if the call to
................................................................................
  4415   4460   ** ^The eTextRep argument determines the encoding of strings passed
  4416   4461   ** to the collating function callback, xCallback.
  4417   4462   ** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
  4418   4463   ** force strings to be UTF16 with native byte order.
  4419   4464   ** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
  4420   4465   ** on an even byte address.
  4421   4466   **
  4422         -** ^The fourth argument, pArg, is a application data pointer that is passed
         4467  +** ^The fourth argument, pArg, is an application data pointer that is passed
  4423   4468   ** through as the first argument to the collating function callback.
  4424   4469   **
  4425   4470   ** ^The fifth argument, xCallback, is a pointer to the collating function.
  4426   4471   ** ^Multiple collating functions can be registered using the same name but
  4427   4472   ** with different eTextRep parameters and SQLite will use whichever
  4428   4473   ** function requires the least amount of data transformation.
  4429   4474   ** ^If the xCallback argument is NULL then the collating function is
................................................................................
  4431   4476   ** that collation is no longer usable.
  4432   4477   **
  4433   4478   ** ^The collating function callback is invoked with a copy of the pArg 
  4434   4479   ** application data pointer and with two strings in the encoding specified
  4435   4480   ** by the eTextRep argument.  The collating function must return an
  4436   4481   ** integer that is negative, zero, or positive
  4437   4482   ** if the first string is less than, equal to, or greater than the second,
  4438         -** respectively.  A collating function must alway return the same answer
         4483  +** respectively.  A collating function must always return the same answer
  4439   4484   ** given the same inputs.  If two or more collating functions are registered
  4440   4485   ** to the same collation name (using different eTextRep values) then all
  4441   4486   ** must give an equivalent answer when invoked with equivalent strings.
  4442   4487   ** The collating function must obey the following properties for all
  4443   4488   ** strings A, B, and C:
  4444   4489   **
  4445   4490   ** <ol>
................................................................................
  4843   4888   ** if one or more of following conditions are true:
  4844   4889   **
  4845   4890   ** <ul>
  4846   4891   ** <li> The soft heap limit is set to zero.
  4847   4892   ** <li> Memory accounting is disabled using a combination of the
  4848   4893   **      [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
  4849   4894   **      the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
  4850         -** <li> An alternative page cache implementation is specifed using
         4895  +** <li> An alternative page cache implementation is specified using
  4851   4896   **      [sqlite3_config]([SQLITE_CONFIG_PCACHE],...).
  4852   4897   ** <li> The page cache allocates from its own memory pool supplied
  4853   4898   **      by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
  4854   4899   **      from the heap.
  4855   4900   ** </ul>)^
  4856   4901   **
  4857   4902   ** Beginning with SQLite version 3.7.3, the soft heap limit is enforced
................................................................................
  5064   5109   typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
  5065   5110   typedef struct sqlite3_module sqlite3_module;
  5066   5111   
  5067   5112   /*
  5068   5113   ** CAPI3REF: Virtual Table Object
  5069   5114   ** KEYWORDS: sqlite3_module {virtual table module}
  5070   5115   **
  5071         -** This structure, sometimes called a a "virtual table module", 
         5116  +** This structure, sometimes called a "virtual table module", 
  5072   5117   ** defines the implementation of a [virtual tables].  
  5073   5118   ** This structure consists mostly of methods for the module.
  5074   5119   **
  5075   5120   ** ^A virtual table module is created by filling in a persistent
  5076   5121   ** instance of this structure and passing a pointer to that instance
  5077   5122   ** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
  5078   5123   ** ^The registration remains valid until it is replaced by a different
................................................................................
  5376   5421   **
  5377   5422   ** ^(If the row that a BLOB handle points to is modified by an
  5378   5423   ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
  5379   5424   ** then the BLOB handle is marked as "expired".
  5380   5425   ** This is true if any column of the row is changed, even a column
  5381   5426   ** other than the one the BLOB handle is open on.)^
  5382   5427   ** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
  5383         -** a expired BLOB handle fail with an return code of [SQLITE_ABORT].
         5428  +** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
  5384   5429   ** ^(Changes written into a BLOB prior to the BLOB expiring are not
  5385   5430   ** rolled back by the expiration of the BLOB.  Such changes will eventually
  5386   5431   ** commit if the transaction continues to completion.)^
  5387   5432   **
  5388   5433   ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
  5389   5434   ** the opened blob.  ^The size of a blob may not be changed by this
  5390   5435   ** interface.  Use the [UPDATE] SQL command to change the size of a
................................................................................
  6078   6123   ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
  6079   6124   ** <dd>This parameter returns the number of lookaside memory slots currently
  6080   6125   ** checked out.</dd>)^
  6081   6126   **
  6082   6127   ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
  6083   6128   ** <dd>This parameter returns the number malloc attempts that were 
  6084   6129   ** satisfied using lookaside memory. Only the high-water value is meaningful;
  6085         -** the current value is always zero.
  6086         -** checked out.</dd>)^
         6130  +** the current value is always zero.)^
  6087   6131   **
  6088   6132   ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
  6089   6133   ** <dd>This parameter returns the number malloc attempts that might have
  6090   6134   ** been satisfied using lookaside memory but failed due to the amount of
  6091   6135   ** memory requested being larger than the lookaside slot size.
  6092   6136   ** Only the high-water value is meaningful;
  6093         -** the current value is always zero.
  6094         -** checked out.</dd>)^
         6137  +** the current value is always zero.)^
  6095   6138   **
  6096   6139   ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
  6097   6140   ** <dd>This parameter returns the number malloc attempts that might have
  6098   6141   ** been satisfied using lookaside memory but failed due to all lookaside
  6099   6142   ** memory already being in use.
  6100   6143   ** Only the high-water value is meaningful;
  6101         -** the current value is always zero.
  6102         -** checked out.</dd>)^
         6144  +** the current value is always zero.)^
  6103   6145   **
  6104   6146   ** ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
  6105   6147   ** <dd>This parameter returns the approximate number of of bytes of heap
  6106   6148   ** memory used by all pager caches associated with the database connection.)^
  6107   6149   ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
  6108   6150   **
  6109   6151   ** ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
................................................................................
  6794   6836   ** connection D.  ^If the database connection D is not in
  6795   6837   ** [WAL | write-ahead log mode] then this interface is a harmless no-op.
  6796   6838   **
  6797   6839   ** ^The [wal_checkpoint pragma] can be used to invoke this interface
  6798   6840   ** from SQL.  ^The [sqlite3_wal_autocheckpoint()] interface and the
  6799   6841   ** [wal_autocheckpoint pragma] can be used to cause this interface to be
  6800   6842   ** run whenever the WAL reaches a certain size threshold.
         6843  +**
         6844  +** See also: [sqlite3_wal_checkpoint_v2()]
  6801   6845   */
  6802   6846   SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
         6847  +
         6848  +/*
         6849  +** CAPI3REF: Checkpoint a database
         6850  +**
         6851  +** Run a checkpoint operation on WAL database zDb attached to database 
         6852  +** handle db. The specific operation is determined by the value of the 
         6853  +** eMode parameter:
         6854  +**
         6855  +** <dl>
         6856  +** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
         6857  +**   Checkpoint as many frames as possible without waiting for any database 
         6858  +**   readers or writers to finish. Sync the db file if all frames in the log
         6859  +**   are checkpointed. This mode is the same as calling 
         6860  +**   sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
         6861  +**
         6862  +** <dt>SQLITE_CHECKPOINT_FULL<dd>
         6863  +**   This mode blocks (calls the busy-handler callback) until there is no
         6864  +**   database writer and all readers are reading from the most recent database
         6865  +**   snapshot. It then checkpoints all frames in the log file and syncs the
         6866  +**   database file. This call blocks database writers while it is running,
         6867  +**   but not database readers.
         6868  +**
         6869  +** <dt>SQLITE_CHECKPOINT_RESTART<dd>
         6870  +**   This mode works the same way as SQLITE_CHECKPOINT_FULL, except after 
         6871  +**   checkpointing the log file it blocks (calls the busy-handler callback)
         6872  +**   until all readers are reading from the database file only. This ensures 
         6873  +**   that the next client to write to the database file restarts the log file 
         6874  +**   from the beginning. This call blocks database writers while it is running,
         6875  +**   but not database readers.
         6876  +** </dl>
         6877  +**
         6878  +** If pnLog is not NULL, then *pnLog is set to the total number of frames in
         6879  +** the log file before returning. If pnCkpt is not NULL, then *pnCkpt is set to
         6880  +** the total number of checkpointed frames (including any that were already
         6881  +** checkpointed when this function is called). *pnLog and *pnCkpt may be
         6882  +** populated even if sqlite3_wal_checkpoint_v2() returns other than SQLITE_OK.
         6883  +** If no values are available because of an error, they are both set to -1
         6884  +** before returning to communicate this to the caller.
         6885  +**
         6886  +** All calls obtain an exclusive "checkpoint" lock on the database file. If
         6887  +** any other process is running a checkpoint operation at the same time, the 
         6888  +** lock cannot be obtained and SQLITE_BUSY is returned. Even if there is a 
         6889  +** busy-handler configured, it will not be invoked in this case.
         6890  +**
         6891  +** The SQLITE_CHECKPOINT_FULL and RESTART modes also obtain the exclusive 
         6892  +** "writer" lock on the database file. If the writer lock cannot be obtained
         6893  +** immediately, and a busy-handler is configured, it is invoked and the writer
         6894  +** lock retried until either the busy-handler returns 0 or the lock is
         6895  +** successfully obtained. The busy-handler is also invoked while waiting for
         6896  +** database readers as described above. If the busy-handler returns 0 before
         6897  +** the writer lock is obtained or while waiting for database readers, the
         6898  +** checkpoint operation proceeds from that point in the same way as 
         6899  +** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible 
         6900  +** without blocking any further. SQLITE_BUSY is returned in this case.
         6901  +**
         6902  +** If parameter zDb is NULL or points to a zero length string, then the
         6903  +** specified operation is attempted on all WAL databases. In this case the
         6904  +** values written to output parameters *pnLog and *pnCkpt are undefined. If 
         6905  +** an SQLITE_BUSY error is encountered when processing one or more of the 
         6906  +** attached WAL databases, the operation is still attempted on any remaining 
         6907  +** attached databases and SQLITE_BUSY is returned to the caller. If any other 
         6908  +** error occurs while processing an attached database, processing is abandoned 
         6909  +** and the error code returned to the caller immediately. If no error 
         6910  +** (SQLITE_BUSY or otherwise) is encountered while processing the attached 
         6911  +** databases, SQLITE_OK is returned.
         6912  +**
         6913  +** If database zDb is the name of an attached database that is not in WAL
         6914  +** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. If
         6915  +** zDb is not NULL (or a zero length string) and is not the name of any
         6916  +** attached database, SQLITE_ERROR is returned to the caller.
         6917  +*/
         6918  +SQLITE_API int sqlite3_wal_checkpoint_v2(
         6919  +  sqlite3 *db,                    /* Database handle */
         6920  +  const char *zDb,                /* Name of attached database (or NULL) */
         6921  +  int eMode,                      /* SQLITE_CHECKPOINT_* value */
         6922  +  int *pnLog,                     /* OUT: Size of WAL log in frames */
         6923  +  int *pnCkpt                     /* OUT: Total number of frames checkpointed */
         6924  +);
         6925  +
         6926  +/*
         6927  +** CAPI3REF: Checkpoint operation parameters
         6928  +**
         6929  +** These constants can be used as the 3rd parameter to
         6930  +** [sqlite3_wal_checkpoint_v2()].  See the [sqlite3_wal_checkpoint_v2()]
         6931  +** documentation for additional information about the meaning and use of
         6932  +** each of these values.
         6933  +*/
         6934  +#define SQLITE_CHECKPOINT_PASSIVE 0
         6935  +#define SQLITE_CHECKPOINT_FULL    1
         6936  +#define SQLITE_CHECKPOINT_RESTART 2
         6937  +
  6803   6938   
  6804   6939   /*
  6805   6940   ** Undo the hack that converts floating point types to integer for
  6806   6941   ** builds on processors without floating point support.
  6807   6942   */
  6808   6943   #ifdef SQLITE_OMIT_FLOATING_POINT
  6809   6944   # undef double
................................................................................
  7515   7650   
  7516   7651   /*
  7517   7652   ** Forward declarations of structure
  7518   7653   */
  7519   7654   typedef struct Btree Btree;
  7520   7655   typedef struct BtCursor BtCursor;
  7521   7656   typedef struct BtShared BtShared;
  7522         -typedef struct BtreeMutexArray BtreeMutexArray;
  7523         -
  7524         -/*
  7525         -** This structure records all of the Btrees that need to hold
  7526         -** a mutex before we enter sqlite3VdbeExec().  The Btrees are
  7527         -** are placed in aBtree[] in order of aBtree[]->pBt.  That way,
  7528         -** we can always lock and unlock them all quickly.
  7529         -*/
  7530         -struct BtreeMutexArray {
  7531         -  int nMutex;
  7532         -  Btree *aBtree[SQLITE_MAX_ATTACHED+1];
  7533         -};
  7534   7657   
  7535   7658   
  7536   7659   SQLITE_PRIVATE int sqlite3BtreeOpen(
  7537   7660     const char *zFilename,   /* Name of database file to open */
  7538   7661     sqlite3 *db,             /* Associated database connection */
  7539   7662     Btree **ppBtree,         /* Return open Btree* here */
  7540   7663     int flags,               /* Flags */
................................................................................
  7563   7686   SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
  7564   7687   SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
  7565   7688   SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
  7566   7689   SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
  7567   7690   SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
  7568   7691   SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
  7569   7692   SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
  7570         -SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*);
         7693  +SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
  7571   7694   SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
  7572   7695   SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*);
  7573   7696   SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
  7574   7697   SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
  7575   7698   SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
  7576   7699   SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
  7577   7700   SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
................................................................................
  7683   7806   
  7684   7807   #ifdef SQLITE_TEST
  7685   7808   SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
  7686   7809   SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
  7687   7810   #endif
  7688   7811   
  7689   7812   #ifndef SQLITE_OMIT_WAL
  7690         -SQLITE_PRIVATE   int sqlite3BtreeCheckpoint(Btree*);
         7813  +SQLITE_PRIVATE   int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
  7691   7814   #endif
  7692   7815   
  7693   7816   /*
  7694   7817   ** If we are not using shared cache, then there is no need to
  7695   7818   ** use mutexes to access the BtShared structures.  So make the
  7696   7819   ** Enter and Leave procedures no-ops.
  7697   7820   */
................................................................................
  7700   7823   SQLITE_PRIVATE   void sqlite3BtreeEnterAll(sqlite3*);
  7701   7824   #else
  7702   7825   # define sqlite3BtreeEnter(X) 
  7703   7826   # define sqlite3BtreeEnterAll(X)
  7704   7827   #endif
  7705   7828   
  7706   7829   #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
         7830  +SQLITE_PRIVATE   int sqlite3BtreeSharable(Btree*);
  7707   7831   SQLITE_PRIVATE   void sqlite3BtreeLeave(Btree*);
  7708   7832   SQLITE_PRIVATE   void sqlite3BtreeEnterCursor(BtCursor*);
  7709   7833   SQLITE_PRIVATE   void sqlite3BtreeLeaveCursor(BtCursor*);
  7710   7834   SQLITE_PRIVATE   void sqlite3BtreeLeaveAll(sqlite3*);
  7711         -SQLITE_PRIVATE   void sqlite3BtreeMutexArrayEnter(BtreeMutexArray*);
  7712         -SQLITE_PRIVATE   void sqlite3BtreeMutexArrayLeave(BtreeMutexArray*);
  7713         -SQLITE_PRIVATE   void sqlite3BtreeMutexArrayInsert(BtreeMutexArray*, Btree*);
  7714   7835   #ifndef NDEBUG
  7715   7836     /* These routines are used inside assert() statements only. */
  7716   7837   SQLITE_PRIVATE   int sqlite3BtreeHoldsMutex(Btree*);
  7717   7838   SQLITE_PRIVATE   int sqlite3BtreeHoldsAllMutexes(sqlite3*);
         7839  +SQLITE_PRIVATE   int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
  7718   7840   #endif
  7719   7841   #else
  7720   7842   
         7843  +# define sqlite3BtreeSharable(X) 0
  7721   7844   # define sqlite3BtreeLeave(X)
  7722   7845   # define sqlite3BtreeEnterCursor(X)
  7723   7846   # define sqlite3BtreeLeaveCursor(X)
  7724   7847   # define sqlite3BtreeLeaveAll(X)
  7725         -# define sqlite3BtreeMutexArrayEnter(X)
  7726         -# define sqlite3BtreeMutexArrayLeave(X)
  7727         -# define sqlite3BtreeMutexArrayInsert(X,Y)
  7728   7848   
  7729   7849   # define sqlite3BtreeHoldsMutex(X) 1
  7730   7850   # define sqlite3BtreeHoldsAllMutexes(X) 1
         7851  +# define sqlite3SchemaMutexHeld(X,Y,Z) 1
  7731   7852   #endif
  7732   7853   
  7733   7854   
  7734   7855   #endif /* _BTREE_H_ */
  7735   7856   
  7736   7857   /************** End of btree.h ***********************************************/
  7737   7858   /************** Continuing where we left off in sqliteInt.h ******************/
................................................................................
  7842   7963   #define P4_DYNAMIC  (-1)  /* Pointer to a string obtained from sqliteMalloc() */
  7843   7964   #define P4_STATIC   (-2)  /* Pointer to a static string */
  7844   7965   #define P4_COLLSEQ  (-4)  /* P4 is a pointer to a CollSeq structure */
  7845   7966   #define P4_FUNCDEF  (-5)  /* P4 is a pointer to a FuncDef structure */
  7846   7967   #define P4_KEYINFO  (-6)  /* P4 is a pointer to a KeyInfo structure */
  7847   7968   #define P4_VDBEFUNC (-7)  /* P4 is a pointer to a VdbeFunc structure */
  7848   7969   #define P4_MEM      (-8)  /* P4 is a pointer to a Mem*    structure */
  7849         -#define P4_TRANSIENT (-9) /* P4 is a pointer to a transient string */
         7970  +#define P4_TRANSIENT  0   /* P4 is a pointer to a transient string */
  7850   7971   #define P4_VTAB     (-10) /* P4 is a pointer to an sqlite3_vtab structure */
  7851   7972   #define P4_MPRINTF  (-11) /* P4 is a string obtained from sqlite3_mprintf() */
  7852   7973   #define P4_REAL     (-12) /* P4 is a 64-bit floating point value */
  7853   7974   #define P4_INT64    (-13) /* P4 is a 64-bit signed integer */
  7854   7975   #define P4_INT32    (-14) /* P4 is a 32-bit signed integer */
  7855   7976   #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
  7856   7977   #define P4_SUBPROGRAM  (-18) /* P4 is a pointer to a SubProgram structure */
................................................................................
  8293   8414   SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager);
  8294   8415   SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
  8295   8416   SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
  8296   8417   SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
  8297   8418   SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
  8298   8419   SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
  8299   8420   
  8300         -SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager);
         8421  +SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
  8301   8422   SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
  8302   8423   SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
  8303   8424   SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
  8304   8425   SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager);
  8305   8426   
  8306   8427   /* Functions used to query pager state and configuration. */
  8307   8428   SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
................................................................................
  8877   8998     u8 inTrans;          /* 0: not writable.  1: Transaction.  2: Checkpoint */
  8878   8999     u8 safety_level;     /* How aggressive at syncing data to disk */
  8879   9000     Schema *pSchema;     /* Pointer to database schema (possibly shared) */
  8880   9001   };
  8881   9002   
  8882   9003   /*
  8883   9004   ** An instance of the following structure stores a database schema.
         9005  +**
         9006  +** Most Schema objects are associated with a Btree.  The exception is
         9007  +** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
         9008  +** In shared cache mode, a single Schema object can be shared by multiple
         9009  +** Btrees that refer to the same underlying BtShared object.
         9010  +** 
         9011  +** Schema objects are automatically deallocated when the last Btree that
         9012  +** references them is destroyed.   The TEMP Schema is manually freed by
         9013  +** sqlite3_close().
         9014  +*
         9015  +** A thread must be holding a mutex on the corresponding Btree in order
         9016  +** to access Schema content.  This implies that the thread must also be
         9017  +** holding a mutex on the sqlite3 connection pointer that owns the Btree.
         9018  +** For a TEMP Schema, on the connection mutex is required.
  8884   9019   */
  8885   9020   struct Schema {
  8886   9021     int schema_cookie;   /* Database schema version number for this file */
         9022  +  int iGeneration;     /* Generation counter.  Incremented with each change */
  8887   9023     Hash tblHash;        /* All tables indexed by name */
  8888   9024     Hash idxHash;        /* All (named) indices indexed by name */
  8889   9025     Hash trigHash;       /* All triggers indexed by name */
  8890   9026     Hash fkeyHash;       /* All foreign keys by referenced table name */
  8891   9027     Table *pSeqTab;      /* The sqlite_sequence table used by AUTOINCREMENT */
  8892   9028     u8 file_format;      /* Schema format version for this file */
  8893   9029     u8 enc;              /* Text encoding used by this database */
................................................................................
  9133   9269   #define SQLITE_RecoveryMode   0x00800000  /* Ignore schema errors */
  9134   9270   #define SQLITE_ReverseOrder   0x01000000  /* Reverse unordered SELECTs */
  9135   9271   #define SQLITE_RecTriggers    0x02000000  /* Enable recursive triggers */
  9136   9272   #define SQLITE_ForeignKeys    0x04000000  /* Enforce foreign key constraints  */
  9137   9273   #define SQLITE_AutoIndex      0x08000000  /* Enable automatic indexes */
  9138   9274   #define SQLITE_PreferBuiltin  0x10000000  /* Preference to built-in funcs */
  9139   9275   #define SQLITE_LoadExtension  0x20000000  /* Enable load_extension */
         9276  +#define SQLITE_EnableTrigger  0x40000000  /* True to enable triggers */
  9140   9277   
  9141   9278   /*
  9142   9279   ** Bits of the sqlite3.flags field that are used by the
  9143   9280   ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface.
  9144   9281   ** These must be the low-order bits of the flags field.
  9145   9282   */
  9146   9283   #define SQLITE_QueryFlattener 0x01        /* Disable query flattening */
................................................................................
  9391   9528   ** schema. This is because each database connection requires its own unique
  9392   9529   ** instance of the sqlite3_vtab* handle used to access the virtual table 
  9393   9530   ** implementation. sqlite3_vtab* handles can not be shared between 
  9394   9531   ** database connections, even when the rest of the in-memory database 
  9395   9532   ** schema is shared, as the implementation often stores the database
  9396   9533   ** connection handle passed to it via the xConnect() or xCreate() method
  9397   9534   ** during initialization internally. This database connection handle may
  9398         -** then used by the virtual table implementation to access real tables 
         9535  +** then be used by the virtual table implementation to access real tables 
  9399   9536   ** within the database. So that they appear as part of the callers 
  9400   9537   ** transaction, these accesses need to be made via the same database 
  9401   9538   ** connection as that used to execute SQL operations on the virtual table.
  9402   9539   **
  9403   9540   ** All VTable objects that correspond to a single table in a shared
  9404   9541   ** database schema are initially stored in a linked-list pointed to by
  9405   9542   ** the Table.pVTable member variable of the corresponding Table object.
................................................................................
  9669   9806     int nColumn;     /* Number of columns in the table used by this index */
  9670   9807     int *aiColumn;   /* Which columns are used by this index.  1st is 0 */
  9671   9808     unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
  9672   9809     Table *pTable;   /* The SQL table being indexed */
  9673   9810     int tnum;        /* Page containing root of this index in database file */
  9674   9811     u8 onError;      /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
  9675   9812     u8 autoIndex;    /* True if is automatically created (ex: by UNIQUE) */
         9813  +  u8 bUnordered;   /* Use this index for == or IN queries only */
  9676   9814     char *zColAff;   /* String defining the affinity of each column */
  9677   9815     Index *pNext;    /* The next index associated with the same table */
  9678   9816     Schema *pSchema; /* Schema containing this index */
  9679   9817     u8 *aSortOrder;  /* Array of size Index.nColumn. True==DESC, False==ASC */
  9680   9818     char **azColl;   /* Array of collation sequence names for index */
  9681   9819     IndexSample *aSample;    /* Array of SQLITE_INDEX_SAMPLES samples */
  9682   9820   };
................................................................................
  9832   9970   */
  9833   9971   struct Expr {
  9834   9972     u8 op;                 /* Operation performed by this node */
  9835   9973     char affinity;         /* The affinity of the column or 0 if not a column */
  9836   9974     u16 flags;             /* Various flags.  EP_* See below */
  9837   9975     union {
  9838   9976       char *zToken;          /* Token value. Zero terminated and dequoted */
  9839         -    int iValue;            /* Integer value if EP_IntValue */
         9977  +    int iValue;            /* Non-negative integer value if EP_IntValue */
  9840   9978     } u;
  9841   9979   
  9842   9980     /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
  9843   9981     ** space is allocated for the fields below this point. An attempt to
  9844   9982     ** access them will result in a segfault or malfunction. 
  9845   9983     *********************************************************************/
  9846   9984   
................................................................................
 10332  10470     Trigger *pTrigger;      /* Trigger this program was coded from */
 10333  10471     int orconf;             /* Default ON CONFLICT policy */
 10334  10472     SubProgram *pProgram;   /* Program implementing pTrigger/orconf */
 10335  10473     u32 aColmask[2];        /* Masks of old.*, new.* columns accessed */
 10336  10474     TriggerPrg *pNext;      /* Next entry in Parse.pTriggerPrg list */
 10337  10475   };
 10338  10476   
        10477  +/*
        10478  +** The yDbMask datatype for the bitmask of all attached databases.
        10479  +*/
        10480  +#if SQLITE_MAX_ATTACHED>30
        10481  +  typedef sqlite3_uint64 yDbMask;
        10482  +#else
        10483  +  typedef unsigned int yDbMask;
        10484  +#endif
        10485  +
 10339  10486   /*
 10340  10487   ** An SQL parser context.  A copy of this structure is passed through
 10341  10488   ** the parser and down into all the parser action routine in order to
 10342  10489   ** carry around information that is global to the entire parse.
 10343  10490   **
 10344  10491   ** The structure is divided into two parts.  When the parser and code
 10345  10492   ** generate call themselves recursively, the first part of the structure
................................................................................
 10380  10527       int iTable;           /* Table cursor number */
 10381  10528       int iColumn;          /* Table column number */
 10382  10529       u8 tempReg;           /* iReg is a temp register that needs to be freed */
 10383  10530       int iLevel;           /* Nesting level */
 10384  10531       int iReg;             /* Reg with value of this column. 0 means none. */
 10385  10532       int lru;              /* Least recently used entry has the smallest value */
 10386  10533     } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
 10387         -  u32 writeMask;       /* Start a write transaction on these databases */
 10388         -  u32 cookieMask;      /* Bitmask of schema verified databases */
        10534  +  yDbMask writeMask;   /* Start a write transaction on these databases */
        10535  +  yDbMask cookieMask;  /* Bitmask of schema verified databases */
 10389  10536     u8 isMultiWrite;     /* True if statement may affect/insert multiple rows */
 10390  10537     u8 mayAbort;         /* True if statement may throw an ABORT exception */
 10391  10538     int cookieGoto;      /* Address of OP_Goto to cookie verifier subroutine */
 10392  10539     int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
 10393  10540   #ifndef SQLITE_OMIT_SHARED_CACHE
 10394  10541     int nTableLock;        /* Number of locks in aTableLock */
 10395  10542     TableLock *aTableLock; /* Required table locks for shared-cache mode */
................................................................................
 10951  11098   SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
 10952  11099   SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
 10953  11100   SQLITE_PRIVATE void sqlite3PrngSaveState(void);
 10954  11101   SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
 10955  11102   SQLITE_PRIVATE void sqlite3PrngResetState(void);
 10956  11103   SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*);
 10957  11104   SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
        11105  +SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
 10958  11106   SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
 10959  11107   SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
 10960  11108   SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*);
 10961  11109   SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
 10962  11110   SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
 10963  11111   SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
 10964  11112   SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
................................................................................
 11112  11260   SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
 11113  11261   SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
 11114  11262   SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Expr*, CollSeq*);
 11115  11263   SQLITE_PRIVATE Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr*, Token*);
 11116  11264   SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
 11117  11265   SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
 11118  11266   SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
        11267  +SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
        11268  +SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
        11269  +SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
        11270  +SQLITE_PRIVATE int sqlite3AbsInt32(int);
 11119  11271   
 11120  11272   SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
 11121  11273   SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
 11122  11274   SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, 
 11123  11275                           void(*)(void*));
 11124  11276   SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
 11125  11277   SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
................................................................................
 11136  11288   SQLITE_PRIVATE const Token sqlite3IntTokens[];
 11137  11289   SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
 11138  11290   SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
 11139  11291   #ifndef SQLITE_OMIT_WSD
 11140  11292   SQLITE_PRIVATE int sqlite3PendingByte;
 11141  11293   #endif
 11142  11294   #endif
 11143         -SQLITE_PRIVATE void sqlite3RootPageMoved(Db*, int, int);
        11295  +SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
 11144  11296   SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
 11145  11297   SQLITE_PRIVATE void sqlite3AlterFunctions(void);
 11146  11298   SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
 11147  11299   SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
 11148  11300   SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
 11149  11301   SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
 11150  11302   SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
................................................................................
 11163  11315   SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
 11164  11316   SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
 11165  11317   SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
 11166  11318   SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
 11167  11319   SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
 11168  11320   SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
 11169  11321   SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse*, int, int);
 11170         -SQLITE_PRIVATE void sqlite3SchemaFree(void *);
        11322  +SQLITE_PRIVATE void sqlite3SchemaClear(void *);
 11171  11323   SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
 11172  11324   SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
 11173  11325   SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *, Index *);
 11174  11326   SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, 
 11175  11327     void (*)(sqlite3_context*,int,sqlite3_value **),
 11176  11328     void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
 11177  11329     FuncDestructor *pDestructor
................................................................................
 11250  11402   SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
 11251  11403   SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
 11252  11404   SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
 11253  11405   SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
 11254  11406   SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
 11255  11407   SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
 11256  11408   SQLITE_PRIVATE const char *sqlite3JournalModename(int);
 11257         -SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int);
        11409  +SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
 11258  11410   SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
 11259  11411   
 11260  11412   /* Declarations for functions in fkey.c. All of these are replaced by
 11261  11413   ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
 11262  11414   ** key functionality is available. If OMIT_TRIGGER is defined but
 11263  11415   ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
 11264  11416   ** this case foreign keys are parsed, but no other functionality is 
................................................................................
 12339  12491     u8 runOnlyOnce;         /* Automatically expire on reset */
 12340  12492     u8 minWriteFileFormat;  /* Minimum file format for writable database files */
 12341  12493     u8 inVtabMethod;        /* See comments above */
 12342  12494     u8 usesStmtJournal;     /* True if uses a statement journal */
 12343  12495     u8 readOnly;            /* True for read-only statements */
 12344  12496     u8 isPrepareV2;         /* True if prepared with prepare_v2() */
 12345  12497     int nChange;            /* Number of db changes made since last reset */
 12346         -  int btreeMask;          /* Bitmask of db->aDb[] entries referenced */
        12498  +  yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
        12499  +  yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
 12347  12500     int iStatement;         /* Statement number (or 0 if has not opened stmt) */
 12348  12501     int aCounter[3];        /* Counters used by sqlite3_stmt_status() */
 12349         -  BtreeMutexArray aMutex; /* An array of Btree used here and needing locks */
 12350  12502   #ifndef SQLITE_OMIT_TRACE
 12351  12503     i64 startTime;          /* Time when query started - used for profiling */
 12352  12504   #endif
 12353  12505     i64 nFkConstraint;      /* Number of imm. FK constraints this VM */
 12354  12506     i64 nStmtDefCons;       /* Number of def. constraints when stmt started */
 12355  12507     char *zSql;             /* Text of the SQL statement that generated this */
 12356  12508     void *pFree;            /* Free this when deleting the vdbe */
................................................................................
 12424  12576   SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
 12425  12577   SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
 12426  12578   SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
 12427  12579   SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
 12428  12580   SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
 12429  12581   SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
 12430  12582   SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
        12583  +
        12584  +#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
        12585  +SQLITE_PRIVATE   void sqlite3VdbeEnter(Vdbe*);
        12586  +SQLITE_PRIVATE   void sqlite3VdbeLeave(Vdbe*);
        12587  +#else
        12588  +# define sqlite3VdbeEnter(X)
        12589  +# define sqlite3VdbeLeave(X)
        12590  +#endif
 12431  12591   
 12432  12592   #ifdef SQLITE_DEBUG
 12433  12593   SQLITE_PRIVATE void sqlite3VdbeMemPrepareToChange(Vdbe*,Mem*);
 12434  12594   #endif
 12435  12595   
 12436  12596   #ifndef SQLITE_OMIT_FOREIGN_KEY
 12437  12597   SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
 12438  12598   #else
 12439  12599   # define sqlite3VdbeCheckFk(p,i) 0
 12440  12600   #endif
 12441  12601   
 12442         -#ifndef SQLITE_OMIT_SHARED_CACHE
 12443         -SQLITE_PRIVATE void sqlite3VdbeMutexArrayEnter(Vdbe *p);
 12444         -#else
 12445         -# define sqlite3VdbeMutexArrayEnter(p)
 12446         -#endif
 12447         -
 12448  12602   SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
 12449  12603   #ifdef SQLITE_DEBUG
 12450  12604   SQLITE_PRIVATE   void sqlite3VdbePrintSql(Vdbe*);
 12451  12605   SQLITE_PRIVATE   void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
 12452  12606   #endif
 12453  12607   SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
 12454  12608   
................................................................................
 12606  12760       ** to store the schema for all databases (main, temp, and any ATTACHed
 12607  12761       ** databases.  *pHighwater is set to zero.
 12608  12762       */
 12609  12763       case SQLITE_DBSTATUS_SCHEMA_USED: {
 12610  12764         int i;                      /* Used to iterate through schemas */
 12611  12765         int nByte = 0;              /* Used to accumulate return value */
 12612  12766   
        12767  +      sqlite3BtreeEnterAll(db);
 12613  12768         db->pnBytesFreed = &nByte;
 12614  12769         for(i=0; i<db->nDb; i++){
 12615  12770           Schema *pSchema = db->aDb[i].pSchema;
 12616  12771           if( ALWAYS(pSchema!=0) ){
 12617  12772             HashElem *p;
 12618  12773   
 12619  12774             nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
................................................................................
 12632  12787             }
 12633  12788             for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
 12634  12789               sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
 12635  12790             }
 12636  12791           }
 12637  12792         }
 12638  12793         db->pnBytesFreed = 0;
        12794  +      sqlite3BtreeLeaveAll(db);
 12639  12795   
 12640  12796         *pHighwater = 0;
 12641  12797         *pCurrent = nByte;
 12642  12798         break;
 12643  12799       }
 12644  12800   
 12645  12801       /*
................................................................................
 15743  15899   
 15744  15900     /*
 15745  15901     ** Space for tracking which blocks are checked out and the size
 15746  15902     ** of each block.  One byte per block.
 15747  15903     */
 15748  15904     u8 *aCtrl;
 15749  15905   
 15750         -} mem5 = { 0 };
        15906  +} mem5;
 15751  15907   
 15752  15908   /*
 15753  15909   ** Access the static variable through a macro for SQLITE_OMIT_WSD
 15754  15910   */
 15755  15911   #define mem5 GLOBAL(struct Mem5Global, mem5)
 15756  15912   
 15757  15913   /*
................................................................................
 16058  16214   **             memsys5Log(4) -> 2
 16059  16215   **             memsys5Log(5) -> 3
 16060  16216   **             memsys5Log(8) -> 3
 16061  16217   **             memsys5Log(9) -> 4
 16062  16218   */
 16063  16219   static int memsys5Log(int iValue){
 16064  16220     int iLog;
 16065         -  for(iLog=0; (1<<iLog)<iValue; iLog++);
        16221  +  for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
 16066  16222     return iLog;
 16067  16223   }
 16068  16224   
 16069  16225   /*
 16070  16226   ** Initialize the memory allocator.
 16071  16227   **
 16072  16228   ** This routine is not threadsafe.  The caller must be holding a mutex
................................................................................
 16089  16245     */
 16090  16246     assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
 16091  16247   
 16092  16248     nByte = sqlite3GlobalConfig.nHeap;
 16093  16249     zByte = (u8*)sqlite3GlobalConfig.pHeap;
 16094  16250     assert( zByte!=0 );  /* sqlite3_config() does not allow otherwise */
 16095  16251   
        16252  +  /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
 16096  16253     nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
 16097  16254     mem5.szAtom = (1<<nMinLog);
 16098  16255     while( (int)sizeof(Mem5Link)>mem5.szAtom ){
 16099  16256       mem5.szAtom = mem5.szAtom << 1;
 16100  16257     }
 16101  16258   
 16102  16259     mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
................................................................................
 16592  16749   /*
 16593  16750   ** The mutex object
 16594  16751   ** Each recursive mutex is an instance of the following structure.
 16595  16752   */
 16596  16753   struct sqlite3_mutex {
 16597  16754     HMTX mutex;       /* Mutex controlling the lock */
 16598  16755     int  id;          /* Mutex type */
 16599         -  int  nRef;        /* Number of references */
 16600         -  TID  owner;       /* Thread holding this mutex */
        16756  +#ifdef SQLITE_DEBUG
        16757  + int   trace;       /* True to trace changes */
        16758  +#endif
 16601  16759   };
 16602  16760   
 16603         -#define OS2_MUTEX_INITIALIZER   0,0,0,0
        16761  +#ifdef SQLITE_DEBUG
        16762  +#define SQLITE3_MUTEX_INITIALIZER { 0, 0, 0 }
        16763  +#else
        16764  +#define SQLITE3_MUTEX_INITIALIZER { 0, 0 }
        16765  +#endif
 16604  16766   
 16605  16767   /*
 16606  16768   ** Initialize and deinitialize the mutex subsystem.
 16607  16769   */
 16608  16770   static int os2MutexInit(void){ return SQLITE_OK; }
 16609  16771   static int os2MutexEnd(void){ return SQLITE_OK; }
 16610  16772   
................................................................................
 16612  16774   ** The sqlite3_mutex_alloc() routine allocates a new
 16613  16775   ** mutex and returns a pointer to it.  If it returns NULL
 16614  16776   ** that means that a mutex could not be allocated. 
 16615  16777   ** SQLite will unwind its stack and return an error.  The argument
 16616  16778   ** to sqlite3_mutex_alloc() is one of these integer constants:
 16617  16779   **
 16618  16780   ** <ul>
 16619         -** <li>  SQLITE_MUTEX_FAST               0
 16620         -** <li>  SQLITE_MUTEX_RECURSIVE          1
 16621         -** <li>  SQLITE_MUTEX_STATIC_MASTER      2
 16622         -** <li>  SQLITE_MUTEX_STATIC_MEM         3
 16623         -** <li>  SQLITE_MUTEX_STATIC_PRNG        4
        16781  +** <li>  SQLITE_MUTEX_FAST
        16782  +** <li>  SQLITE_MUTEX_RECURSIVE
        16783  +** <li>  SQLITE_MUTEX_STATIC_MASTER
        16784  +** <li>  SQLITE_MUTEX_STATIC_MEM
        16785  +** <li>  SQLITE_MUTEX_STATIC_MEM2
        16786  +** <li>  SQLITE_MUTEX_STATIC_PRNG
        16787  +** <li>  SQLITE_MUTEX_STATIC_LRU
        16788  +** <li>  SQLITE_MUTEX_STATIC_LRU2
 16624  16789   ** </ul>
 16625  16790   **
 16626  16791   ** The first two constants cause sqlite3_mutex_alloc() to create
 16627  16792   ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
 16628  16793   ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
 16629  16794   ** The mutex implementation does not need to make a distinction
 16630  16795   ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
 16631  16796   ** not want to.  But SQLite will only request a recursive mutex in
 16632  16797   ** cases where it really needs one.  If a faster non-recursive mutex
 16633  16798   ** implementation is available on the host platform, the mutex subsystem
 16634  16799   ** might return such a mutex in response to SQLITE_MUTEX_FAST.
 16635  16800   **
 16636  16801   ** The other allowed parameters to sqlite3_mutex_alloc() each return
 16637         -** a pointer to a static preexisting mutex.  Three static mutexes are
        16802  +** a pointer to a static preexisting mutex.  Six static mutexes are
 16638  16803   ** used by the current version of SQLite.  Future versions of SQLite
 16639  16804   ** may add additional static mutexes.  Static mutexes are for internal
 16640  16805   ** use by SQLite only.  Applications that use SQLite mutexes should
 16641  16806   ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
 16642  16807   ** SQLITE_MUTEX_RECURSIVE.
 16643  16808   **
 16644  16809   ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
................................................................................
 16660  16825             p = NULL;
 16661  16826           }
 16662  16827         }
 16663  16828         break;
 16664  16829       }
 16665  16830       default: {
 16666  16831         static volatile int isInit = 0;
 16667         -      static sqlite3_mutex staticMutexes[] = {
 16668         -        { OS2_MUTEX_INITIALIZER, },
 16669         -        { OS2_MUTEX_INITIALIZER, },
 16670         -        { OS2_MUTEX_INITIALIZER, },
 16671         -        { OS2_MUTEX_INITIALIZER, },
 16672         -        { OS2_MUTEX_INITIALIZER, },
 16673         -        { OS2_MUTEX_INITIALIZER, },
        16832  +      static sqlite3_mutex staticMutexes[6] = {
        16833  +        SQLITE3_MUTEX_INITIALIZER,
        16834  +        SQLITE3_MUTEX_INITIALIZER,
        16835  +        SQLITE3_MUTEX_INITIALIZER,
        16836  +        SQLITE3_MUTEX_INITIALIZER,
        16837  +        SQLITE3_MUTEX_INITIALIZER,
        16838  +        SQLITE3_MUTEX_INITIALIZER,
 16674  16839         };
 16675  16840         if ( !isInit ){
 16676  16841           APIRET rc;
 16677  16842           PTIB ptib;
 16678  16843           PPIB ppib;
 16679  16844           HMTX mutex;
 16680  16845           char name[32];
................................................................................
 16712  16877   
 16713  16878   
 16714  16879   /*
 16715  16880   ** This routine deallocates a previously allocated mutex.
 16716  16881   ** SQLite is careful to deallocate every mutex that it allocates.
 16717  16882   */
 16718  16883   static void os2MutexFree(sqlite3_mutex *p){
 16719         -  if( p==0 ) return;
 16720         -  assert( p->nRef==0 );
        16884  +#ifdef SQLITE_DEBUG
        16885  +  TID tid;
        16886  +  PID pid;
        16887  +  ULONG ulCount;
        16888  +  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
        16889  +  assert( ulCount==0 );
 16721  16890     assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
        16891  +#endif
 16722  16892     DosCloseMutexSem( p->mutex );
 16723  16893     sqlite3_free( p );
 16724  16894   }
 16725  16895   
 16726  16896   #ifdef SQLITE_DEBUG
 16727  16897   /*
 16728  16898   ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
................................................................................
 16729  16899   ** intended for use inside assert() statements.
 16730  16900   */
 16731  16901   static int os2MutexHeld(sqlite3_mutex *p){
 16732  16902     TID tid;
 16733  16903     PID pid;
 16734  16904     ULONG ulCount;
 16735  16905     PTIB ptib;
 16736         -  if( p!=0 ) {
 16737         -    DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
 16738         -  } else {
 16739         -    DosGetInfoBlocks(&ptib, NULL);
 16740         -    tid = ptib->tib_ptib2->tib2_ultid;
 16741         -  }
 16742         -  return p==0 || (p->nRef!=0 && p->owner==tid);
        16906  +  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
        16907  +  if( ulCount==0 || ( ulCount>1 && p->id!=SQLITE_MUTEX_RECURSIVE ) )
        16908  +    return 0;
        16909  +  DosGetInfoBlocks(&ptib, NULL);
        16910  +  return tid==ptib->tib_ptib2->tib2_ultid;
 16743  16911   }
 16744  16912   static int os2MutexNotheld(sqlite3_mutex *p){
 16745  16913     TID tid;
 16746  16914     PID pid;
 16747  16915     ULONG ulCount;
 16748  16916     PTIB ptib;
 16749         -  if( p!= 0 ) {
 16750         -    DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
 16751         -  } else {
 16752         -    DosGetInfoBlocks(&ptib, NULL);
 16753         -    tid = ptib->tib_ptib2->tib2_ultid;
 16754         -  }
 16755         -  return p==0 || p->nRef==0 || p->owner!=tid;
        16917  +  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
        16918  +  if( ulCount==0 )
        16919  +    return 1;
        16920  +  DosGetInfoBlocks(&ptib, NULL);
        16921  +  return tid!=ptib->tib_ptib2->tib2_ultid;
        16922  +}
        16923  +static void os2MutexTrace(sqlite3_mutex *p, char *pAction){
        16924  +  TID   tid;
        16925  +  PID   pid;
        16926  +  ULONG ulCount;
        16927  +  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
        16928  +  printf("%s mutex %p (%d) with nRef=%ld\n", pAction, (void*)p, p->trace, ulCount);
 16756  16929   }
 16757  16930   #endif
 16758  16931   
 16759  16932   /*
 16760  16933   ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
 16761  16934   ** to enter a mutex.  If another thread is already within the mutex,
 16762  16935   ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
................................................................................
 16764  16937   ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
 16765  16938   ** be entered multiple times by the same thread.  In such cases the,
 16766  16939   ** mutex must be exited an equal number of times before another thread
 16767  16940   ** can enter.  If the same thread tries to enter any other kind of mutex
 16768  16941   ** more than once, the behavior is undefined.
 16769  16942   */
 16770  16943   static void os2MutexEnter(sqlite3_mutex *p){
 16771         -  TID tid;
 16772         -  PID holder1;
 16773         -  ULONG holder2;
 16774         -  if( p==0 ) return;
 16775  16944     assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
 16776  16945     DosRequestMutexSem(p->mutex, SEM_INDEFINITE_WAIT);
 16777         -  DosQueryMutexSem(p->mutex, &holder1, &tid, &holder2);
 16778         -  p->owner = tid;
 16779         -  p->nRef++;
        16946  +#ifdef SQLITE_DEBUG
        16947  +  if( p->trace ) os2MutexTrace(p, "enter");
        16948  +#endif
 16780  16949   }
 16781  16950   static int os2MutexTry(sqlite3_mutex *p){
 16782         -  int rc;
 16783         -  TID tid;
 16784         -  PID holder1;
 16785         -  ULONG holder2;
 16786         -  if( p==0 ) return SQLITE_OK;
        16951  +  int rc = SQLITE_BUSY;
 16787  16952     assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
 16788         -  if( DosRequestMutexSem(p->mutex, SEM_IMMEDIATE_RETURN) == NO_ERROR) {
 16789         -    DosQueryMutexSem(p->mutex, &holder1, &tid, &holder2);
 16790         -    p->owner = tid;
 16791         -    p->nRef++;
        16953  +  if( DosRequestMutexSem(p->mutex, SEM_IMMEDIATE_RETURN) == NO_ERROR ) {
 16792  16954       rc = SQLITE_OK;
 16793         -  } else {
 16794         -    rc = SQLITE_BUSY;
        16955  +#ifdef SQLITE_DEBUG
        16956  +    if( p->trace ) os2MutexTrace(p, "try");
        16957  +#endif
 16795  16958     }
 16796         -
 16797  16959     return rc;
 16798  16960   }
 16799  16961   
 16800  16962   /*
 16801  16963   ** The sqlite3_mutex_leave() routine exits a mutex that was
 16802  16964   ** previously entered by the same thread.  The behavior
 16803  16965   ** is undefined if the mutex is not currently entered or
 16804  16966   ** is not currently allocated.  SQLite will never do either.
 16805  16967   */
 16806  16968   static void os2MutexLeave(sqlite3_mutex *p){
 16807         -  TID tid;
 16808         -  PID holder1;
 16809         -  ULONG holder2;
 16810         -  if( p==0 ) return;
 16811         -  assert( p->nRef>0 );
 16812         -  DosQueryMutexSem(p->mutex, &holder1, &tid, &holder2);
 16813         -  assert( p->owner==tid );
 16814         -  p->nRef--;
 16815         -  assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
        16969  +  assert( os2MutexHeld(p) );
 16816  16970     DosReleaseMutexSem(p->mutex);
        16971  +#ifdef SQLITE_DEBUG
        16972  +  if( p->trace ) os2MutexTrace(p, "leave");
        16973  +#endif
 16817  16974   }
 16818  16975   
 16819  16976   SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
 16820  16977     static const sqlite3_mutex_methods sMutex = {
 16821  16978       os2MutexInit,
 16822  16979       os2MutexEnd,
 16823  16980       os2MutexAlloc,
................................................................................
 16824  16981       os2MutexFree,
 16825  16982       os2MutexEnter,
 16826  16983       os2MutexTry,
 16827  16984       os2MutexLeave,
 16828  16985   #ifdef SQLITE_DEBUG
 16829  16986       os2MutexHeld,
 16830  16987       os2MutexNotheld
        16988  +#else
        16989  +    0,
        16990  +    0
 16831  16991   #endif
 16832  16992     };
 16833  16993   
 16834  16994     return &sMutex;
 16835  16995   }
 16836  16996   #endif /* SQLITE_MUTEX_OS2 */
 16837  16997   
................................................................................
 17467  17627       rc = SQLITE_OK;
 17468  17628     }
 17469  17629   #else
 17470  17630     UNUSED_PARAMETER(p);
 17471  17631   #endif
 17472  17632   #ifdef SQLITE_DEBUG
 17473  17633     if( rc==SQLITE_OK && p->trace ){
 17474         -    printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
        17634  +    printf("try mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
 17475  17635     }
 17476  17636   #endif
 17477  17637     return rc;
 17478  17638   }
 17479  17639   
 17480  17640   /*
 17481  17641   ** The sqlite3_mutex_leave() routine exits a mutex that was
................................................................................
 17924  18084         /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
 17925  18085         ScratchFreeslot *pSlot;
 17926  18086         pSlot = (ScratchFreeslot*)p;
 17927  18087         sqlite3_mutex_enter(mem0.mutex);
 17928  18088         pSlot->pNext = mem0.pScratchFree;
 17929  18089         mem0.pScratchFree = pSlot;
 17930  18090         mem0.nScratchFree++;
 17931         -      assert( mem0.nScratchFree<=sqlite3GlobalConfig.nScratch );
        18091  +      assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
 17932  18092         sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
 17933  18093         sqlite3_mutex_leave(mem0.mutex);
 17934  18094       }else{
 17935  18095         /* Release memory back to the heap */
 17936  18096         assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
 17937  18097         assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
 17938  18098         sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
................................................................................
 18698  18858               v = va_arg(ap,i64);
 18699  18859             }else if( flag_long ){
 18700  18860               v = va_arg(ap,long int);
 18701  18861             }else{
 18702  18862               v = va_arg(ap,int);
 18703  18863             }
 18704  18864             if( v<0 ){
 18705         -            longvalue = -v;
        18865  +            if( v==SMALLEST_INT64 ){
        18866  +              longvalue = ((u64)1)<<63;
        18867  +            }else{
        18868  +              longvalue = -v;
        18869  +            }
 18706  18870               prefix = '-';
 18707  18871             }else{
 18708  18872               longvalue = v;
 18709  18873               if( flag_plussign )        prefix = '+';
 18710  18874               else if( flag_blanksign )  prefix = ' ';
 18711  18875               else                       prefix = 0;
 18712  18876             }
................................................................................
 19637  19801           || (c&0xFFFFF800)==0xD800                          \
 19638  19802           || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
 19639  19803     }
 19640  19804   SQLITE_PRIVATE int sqlite3Utf8Read(
 19641  19805     const unsigned char *zIn,       /* First byte of UTF-8 character */
 19642  19806     const unsigned char **pzNext    /* Write first byte past UTF-8 char here */
 19643  19807   ){
 19644         -  int c;
        19808  +  unsigned int c;
 19645  19809   
 19646  19810     /* Same as READ_UTF8() above but without the zTerm parameter.
 19647  19811     ** For this routine, we assume the UTF8 string is always zero-terminated.
 19648  19812     */
 19649  19813     c = *(zIn++);
 19650  19814     if( c>=0xc0 ){
 19651  19815       c = sqlite3Utf8Trans1[c-0xc0];
................................................................................
 19880  20044   #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 19881  20045   /*
 19882  20046   ** Translate UTF-8 to UTF-8.
 19883  20047   **
 19884  20048   ** This has the effect of making sure that the string is well-formed
 19885  20049   ** UTF-8.  Miscoded characters are removed.
 19886  20050   **
 19887         -** The translation is done in-place (since it is impossible for the
 19888         -** correct UTF-8 encoding to be longer than a malformed encoding).
        20051  +** The translation is done in-place and aborted if the output
        20052  +** overruns the input.
 19889  20053   */
 19890  20054   SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
 19891  20055     unsigned char *zOut = zIn;
 19892  20056     unsigned char *zStart = zIn;
 19893  20057     u32 c;
 19894  20058   
 19895         -  while( zIn[0] ){
        20059  +  while( zIn[0] && zOut<=zIn ){
 19896  20060       c = sqlite3Utf8Read(zIn, (const u8**)&zIn);
 19897  20061       if( c!=0xfffd ){
 19898  20062         WRITE_UTF8(zOut, c);
 19899  20063       }
 19900  20064     }
 19901  20065     *zOut = 0;
 19902  20066     return (int)(zOut - zStart);
................................................................................
 20057  20221   #endif
 20058  20222   
 20059  20223   /*
 20060  20224   ** Routine needed to support the testcase() macro.
 20061  20225   */
 20062  20226   #ifdef SQLITE_COVERAGE_TEST
 20063  20227   SQLITE_PRIVATE void sqlite3Coverage(int x){
 20064         -  static int dummy = 0;
 20065         -  dummy += x;
        20228  +  static unsigned dummy = 0;
        20229  +  dummy += (unsigned)x;
 20066  20230   }
 20067  20231   #endif
 20068  20232   
 20069  20233   #ifndef SQLITE_OMIT_FLOATING_POINT
 20070  20234   /*
 20071  20235   ** Return true if the floating point value is Not a Number (NaN).
 20072  20236   **
................................................................................
 20472  20636       testcase( c==(+1) );
 20473  20637     }
 20474  20638     return c;
 20475  20639   }
 20476  20640   
 20477  20641   
 20478  20642   /*
 20479         -** Convert zNum to a 64-bit signed integer and write
 20480         -** the value of the integer into *pNum.
 20481         -** If zNum is exactly 9223372036854665808, return 2.
 20482         -** This is a special case as the context will determine
 20483         -** if it is too big (used as a negative).
 20484         -** If zNum is not an integer or is an integer that 
 20485         -** is too large to be expressed with 64 bits,
 20486         -** then return 1.  Otherwise return 0.
        20643  +** Convert zNum to a 64-bit signed integer.
        20644  +**
        20645  +** If the zNum value is representable as a 64-bit twos-complement 
        20646  +** integer, then write that value into *pNum and return 0.
        20647  +**
        20648  +** If zNum is exactly 9223372036854665808, return 2.  This special
        20649  +** case is broken out because while 9223372036854665808 cannot be a 
        20650  +** signed 64-bit integer, its negative -9223372036854665808 can be.
        20651  +**
        20652  +** If zNum is too big for a 64-bit integer and is not
        20653  +** 9223372036854665808 then return 1.
 20487  20654   **
 20488  20655   ** length is the number of bytes in the string (bytes, not characters).
 20489  20656   ** The string is not necessarily zero-terminated.  The encoding is
 20490  20657   ** given by enc.
 20491  20658   */
 20492  20659   SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
 20493  20660     int incr = (enc==SQLITE_UTF8?1:2);
 20494         -  i64 v = 0;
        20661  +  u64 u = 0;
 20495  20662     int neg = 0; /* assume positive */
 20496  20663     int i;
 20497  20664     int c = 0;
 20498  20665     const char *zStart;
 20499  20666     const char *zEnd = zNum + length;
 20500  20667     if( enc==SQLITE_UTF16BE ) zNum++;
 20501  20668     while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
 20502         -  if( zNum>=zEnd ) goto do_atoi_calc;
 20503         -  if( *zNum=='-' ){
 20504         -    neg = 1;
 20505         -    zNum+=incr;
 20506         -  }else if( *zNum=='+' ){
 20507         -    zNum+=incr;
        20669  +  if( zNum<zEnd ){
        20670  +    if( *zNum=='-' ){
        20671  +      neg = 1;
        20672  +      zNum+=incr;
        20673  +    }else if( *zNum=='+' ){
        20674  +      zNum+=incr;
        20675  +    }
 20508  20676     }
 20509         -do_atoi_calc:
 20510  20677     zStart = zNum;
 20511  20678     while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
 20512  20679     for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
 20513         -    v = v*10 + c - '0';
        20680  +    u = u*10 + c - '0';
 20514  20681     }
 20515         -  *pNum = neg ? -v : v;
        20682  +  if( u>LARGEST_INT64 ){
        20683  +    *pNum = SMALLEST_INT64;
        20684  +  }else if( neg ){
        20685  +    *pNum = -(i64)u;
        20686  +  }else{
        20687  +    *pNum = (i64)u;
        20688  +  }
 20516  20689     testcase( i==18 );
 20517  20690     testcase( i==19 );
 20518  20691     testcase( i==20 );
 20519  20692     if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr ){
 20520  20693       /* zNum is empty or contains non-numeric text or is longer
 20521  20694       ** than 19 digits (thus guaranteeing that it is too large) */
 20522  20695       return 1;
 20523  20696     }else if( i<19*incr ){
 20524  20697       /* Less than 19 digits, so we know that it fits in 64 bits */
        20698  +    assert( u<=LARGEST_INT64 );
 20525  20699       return 0;
 20526  20700     }else{
 20527         -    /* 19-digit numbers must be no larger than 9223372036854775807 if positive
 20528         -    ** or 9223372036854775808 if negative.  Note that 9223372036854665808
 20529         -    ** is 2^63. Return 1 if to large */
 20530         -    c=compare2pow63(zNum, incr);
 20531         -    if( c==0 && neg==0 ) return 2; /* too big, exactly 9223372036854665808 */
 20532         -    return c<neg ? 0 : 1;
        20701  +    /* zNum is a 19-digit numbers.  Compare it against 9223372036854775808. */
        20702  +    c = compare2pow63(zNum, incr);
        20703  +    if( c<0 ){
        20704  +      /* zNum is less than 9223372036854775808 so it fits */
        20705  +      assert( u<=LARGEST_INT64 );
        20706  +      return 0;
        20707  +    }else if( c>0 ){
        20708  +      /* zNum is greater than 9223372036854775808 so it overflows */
        20709  +      return 1;
        20710  +    }else{
        20711  +      /* zNum is exactly 9223372036854775808.  Fits if negative.  The
        20712  +      ** special case 2 overflow if positive */
        20713  +      assert( u-1==LARGEST_INT64 );
        20714  +      assert( (*pNum)==SMALLEST_INT64 );
        20715  +      return neg ? 0 : 2;
        20716  +    }
 20533  20717     }
 20534  20718   }
 20535  20719   
 20536  20720   /*
 20537  20721   ** If zNum represents an integer that will fit in 32-bits, then set
 20538  20722   ** *pValue to that integer and return true.  Otherwise return false.
 20539  20723   **
................................................................................
 21091  21275       testcase( sqlite3GlobalConfig.xLog!=0 );
 21092  21276       logBadConnection("invalid");
 21093  21277       return 0;
 21094  21278     }else{
 21095  21279       return 1;
 21096  21280     }
 21097  21281   }
        21282  +
        21283  +/*
        21284  +** Attempt to add, substract, or multiply the 64-bit signed value iB against
        21285  +** the other 64-bit signed integer at *pA and store the result in *pA.
        21286  +** Return 0 on success.  Or if the operation would have resulted in an
        21287  +** overflow, leave *pA unchanged and return 1.
        21288  +*/
        21289  +SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
        21290  +  i64 iA = *pA;
        21291  +  testcase( iA==0 ); testcase( iA==1 );
        21292  +  testcase( iB==-1 ); testcase( iB==0 );
        21293  +  if( iB>=0 ){
        21294  +    testcase( iA>0 && LARGEST_INT64 - iA == iB );
        21295  +    testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
        21296  +    if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
        21297  +    *pA += iB;
        21298  +  }else{
        21299  +    testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
        21300  +    testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
        21301  +    if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
        21302  +    *pA += iB;
        21303  +  }
        21304  +  return 0; 
        21305  +}
        21306  +SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
        21307  +  testcase( iB==SMALLEST_INT64+1 );
        21308  +  if( iB==SMALLEST_INT64 ){
        21309  +    testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
        21310  +    if( (*pA)>=0 ) return 1;
        21311  +    *pA -= iB;
        21312  +    return 0;
        21313  +  }else{
        21314  +    return sqlite3AddInt64(pA, -iB);
        21315  +  }
        21316  +}
        21317  +#define TWOPOWER32 (((i64)1)<<32)
        21318  +#define TWOPOWER31 (((i64)1)<<31)
        21319  +SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
        21320  +  i64 iA = *pA;
        21321  +  i64 iA1, iA0, iB1, iB0, r;
        21322  +
        21323  +  iA1 = iA/TWOPOWER32;
        21324  +  iA0 = iA % TWOPOWER32;
        21325  +  iB1 = iB/TWOPOWER32;
        21326  +  iB0 = iB % TWOPOWER32;
        21327  +  if( iA1*iB1 != 0 ) return 1;
        21328  +  assert( iA1*iB0==0 || iA0*iB1==0 );
        21329  +  r = iA1*iB0 + iA0*iB1;
        21330  +  testcase( r==(-TWOPOWER31)-1 );
        21331  +  testcase( r==(-TWOPOWER31) );
        21332  +  testcase( r==TWOPOWER31 );
        21333  +  testcase( r==TWOPOWER31-1 );
        21334  +  if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1;
        21335  +  r *= TWOPOWER32;
        21336  +  if( sqlite3AddInt64(&r, iA0*iB0) ) return 1;
        21337  +  *pA = r;
        21338  +  return 0;
        21339  +}
        21340  +
        21341  +/*
        21342  +** Compute the absolute value of a 32-bit signed integer, of possible.  Or 
        21343  +** if the integer has a value of -2147483648, return +2147483647
        21344  +*/
        21345  +SQLITE_PRIVATE int sqlite3AbsInt32(int x){
        21346  +  if( x>=0 ) return x;
        21347  +  if( x==(int)0x80000000 ) return 0x7fffffff;
        21348  +  return -x;
        21349  +}
 21098  21350   
 21099  21351   /************** End of util.c ************************************************/
 21100  21352   /************** Begin file hash.c ********************************************/
 21101  21353   /*
 21102  21354   ** 2001 September 22
 21103  21355   **
 21104  21356   ** The author disclaims copyright to this source code.  In place of
................................................................................
 21790  22042   #endif
 21791  22043   
 21792  22044   #endif /* !defined(_OS_COMMON_H_) */
 21793  22045   
 21794  22046   /************** End of os_common.h *******************************************/
 21795  22047   /************** Continuing where we left off in os_os2.c *********************/
 21796  22048   
        22049  +/* Forward references */
        22050  +typedef struct os2File os2File;         /* The file structure */
        22051  +typedef struct os2ShmNode os2ShmNode;   /* A shared descritive memory node */
        22052  +typedef struct os2ShmLink os2ShmLink;   /* A connection to shared-memory */
        22053  +
 21797  22054   /*
 21798  22055   ** The os2File structure is subclass of sqlite3_file specific for the OS/2
 21799  22056   ** protability layer.
 21800  22057   */
 21801         -typedef struct os2File os2File;
 21802  22058   struct os2File {
 21803  22059     const sqlite3_io_methods *pMethod;  /* Always the first entry */
 21804  22060     HFILE h;                  /* Handle for accessing the file */
 21805         -  char* pathToDel;          /* Name of file to delete on close, NULL if not */
 21806         -  unsigned char locktype;   /* Type of lock currently held on this file */
        22061  +  int flags;                /* Flags provided to os2Open() */
        22062  +  int locktype;             /* Type of lock currently held on this file */
        22063  +  int szChunk;              /* Chunk size configured by FCNTL_CHUNK_SIZE */
        22064  +  char *zFullPathCp;        /* Full path name of this file */
        22065  +  os2ShmLink *pShmLink;     /* Instance of shared memory on this file */
 21807  22066   };
 21808  22067   
 21809  22068   #define LOCK_TIMEOUT 10L /* the default locking timeout */
 21810  22069   
        22070  +/*
        22071  +** Missing from some versions of the OS/2 toolkit -
        22072  +** used to allocate from high memory if possible
        22073  +*/
        22074  +#ifndef OBJ_ANY
        22075  +# define OBJ_ANY 0x00000400
        22076  +#endif
        22077  +
 21811  22078   /*****************************************************************************
 21812  22079   ** The next group of routines implement the I/O methods specified
 21813  22080   ** by the sqlite3_io_methods object.
 21814  22081   ******************************************************************************/
 21815  22082   
 21816  22083   /*
 21817  22084   ** Close a file.
 21818  22085   */
 21819  22086   static int os2Close( sqlite3_file *id ){
 21820         -  APIRET rc = NO_ERROR;
 21821         -  os2File *pFile;
 21822         -  if( id && (pFile = (os2File*)id) != 0 ){
 21823         -    OSTRACE(( "CLOSE %d\n", pFile->h ));
 21824         -    rc = DosClose( pFile->h );
 21825         -    pFile->locktype = NO_LOCK;
 21826         -    if( pFile->pathToDel != NULL ){
 21827         -      rc = DosForceDelete( (PSZ)pFile->pathToDel );
 21828         -      free( pFile->pathToDel );
 21829         -      pFile->pathToDel = NULL;
 21830         -    }
 21831         -    id = 0;
 21832         -    OpenCounter( -1 );
 21833         -  }
        22087  +  APIRET rc;
        22088  +  os2File *pFile = (os2File*)id;
 21834  22089   
        22090  +  assert( id!=0 );
        22091  +  OSTRACE(( "CLOSE %d (%s)\n", pFile->h, pFile->zFullPathCp ));
        22092  +
        22093  +  rc = DosClose( pFile->h );
        22094  +
        22095  +  if( pFile->flags & SQLITE_OPEN_DELETEONCLOSE )
        22096  +    DosForceDelete( (PSZ)pFile->zFullPathCp );
        22097  +
        22098  +  free( pFile->zFullPathCp );
        22099  +  pFile->zFullPathCp = NULL;
        22100  +  pFile->locktype = NO_LOCK;
        22101  +  pFile->h = (HFILE)-1;
        22102  +  pFile->flags = 0;
        22103  +
        22104  +  OpenCounter( -1 );
 21835  22105     return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
 21836  22106   }
 21837  22107   
 21838  22108   /*
 21839  22109   ** Read data from a file into a buffer.  Return SQLITE_OK if all
 21840  22110   ** bytes were read successfully and SQLITE_IOERR if anything goes
 21841  22111   ** wrong.
................................................................................
 21900  22170     return ( rc != NO_ERROR || amt > (int)wrote ) ? SQLITE_FULL : SQLITE_OK;
 21901  22171   }
 21902  22172   
 21903  22173   /*
 21904  22174   ** Truncate an open file to a specified size
 21905  22175   */
 21906  22176   static int os2Truncate( sqlite3_file *id, i64 nByte ){
 21907         -  APIRET rc = NO_ERROR;
        22177  +  APIRET rc;
 21908  22178     os2File *pFile = (os2File*)id;
        22179  +  assert( id!=0 );
 21909  22180     OSTRACE(( "TRUNCATE %d %lld\n", pFile->h, nByte ));
 21910  22181     SimulateIOError( return SQLITE_IOERR_TRUNCATE );
        22182  +
        22183  +  /* If the user has configured a chunk-size for this file, truncate the
        22184  +  ** file so that it consists of an integer number of chunks (i.e. the
        22185  +  ** actual file size after the operation may be larger than the requested
        22186  +  ** size).
        22187  +  */
        22188  +  if( pFile->szChunk ){
        22189  +    nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
        22190  +  }
        22191  +  
 21911  22192     rc = DosSetFileSize( pFile->h, nByte );
 21912  22193     return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR_TRUNCATE;
 21913  22194   }
 21914  22195   
 21915  22196   #ifdef SQLITE_TEST
 21916  22197   /*
 21917  22198   ** Count the number of fullsyncs and normal syncs.  This is used to test
................................................................................
 22267  22548     switch( op ){
 22268  22549       case SQLITE_FCNTL_LOCKSTATE: {
 22269  22550         *(int*)pArg = ((os2File*)id)->locktype;
 22270  22551         OSTRACE(( "FCNTL_LOCKSTATE %d lock=%d\n",
 22271  22552                   ((os2File*)id)->h, ((os2File*)id)->locktype ));
 22272  22553         return SQLITE_OK;
 22273  22554       }
        22555  +    case SQLITE_FCNTL_CHUNK_SIZE: {
        22556  +      ((os2File*)id)->szChunk = *(int*)pArg;
        22557  +      return SQLITE_OK;
        22558  +    }
        22559  +    case SQLITE_FCNTL_SIZE_HINT: {
        22560  +      sqlite3_int64 sz = *(sqlite3_int64*)pArg;
        22561  +      SimulateIOErrorBenign(1);
        22562  +      os2Truncate(id, sz);
        22563  +      SimulateIOErrorBenign(0);
        22564  +      return SQLITE_OK;
        22565  +    }
        22566  +    case SQLITE_FCNTL_SYNC_OMITTED: {
        22567  +      return SQLITE_OK;
        22568  +    }
 22274  22569     }
 22275  22570     return SQLITE_NOTFOUND;
 22276  22571   }
 22277  22572   
 22278  22573   /*
 22279  22574   ** Return the sector size in bytes of the underlying block device for
 22280  22575   ** the specified file. This is almost always 512 bytes, but may be
................................................................................
 22282  22577   **
 22283  22578   ** SQLite code assumes this function cannot fail. It also assumes that
 22284  22579   ** if two files are created in the same file-system directory (i.e.
 22285  22580   ** a database and its journal file) that the sector size will be the
 22286  22581   ** same for both.
 22287  22582   */
 22288  22583   static int os2SectorSize(sqlite3_file *id){
        22584  +  UNUSED_PARAMETER(id);
 22289  22585     return SQLITE_DEFAULT_SECTOR_SIZE;
 22290  22586   }
 22291  22587   
 22292  22588   /*
 22293  22589   ** Return a vector of device characteristics.
 22294  22590   */
 22295  22591   static int os2DeviceCharacteristics(sqlite3_file *id){
 22296         -  return 0;
        22592  +  UNUSED_PARAMETER(id);
        22593  +  return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;
 22297  22594   }
 22298  22595   
 22299  22596   
 22300  22597   /*
 22301  22598   ** Character set conversion objects used by conversion routines.
 22302  22599   */
 22303  22600   static UconvObject ucUtf8 = NULL; /* convert between UTF-8 and UCS-2 */
................................................................................
 22375  22672       return out; /* if conversion fails, return the empty string */
 22376  22673   
 22377  22674     /* determine string for the conversion of UTF-8 which is CP1208 */
 22378  22675     UniStrFromUcs( ucUtf8, out, tempPath, CCHMAXPATH );
 22379  22676   
 22380  22677     return out;
 22381  22678   }
        22679  +
        22680  +
        22681  +#ifndef SQLITE_OMIT_WAL
        22682  +
        22683  +/*
        22684  +** Use main database file for interprocess locking. If un-defined
        22685  +** a separate file is created for this purpose. The file will be
        22686  +** used only to set file locks. There will be no data written to it.
        22687  +*/
        22688  +#define SQLITE_OS2_NO_WAL_LOCK_FILE     
        22689  +
        22690  +#if 0
        22691  +static void _ERR_TRACE( const char *fmt, ... ) {
        22692  +  va_list  ap;
        22693  +  va_start(ap, fmt);
        22694  +  vfprintf(stderr, fmt, ap);
        22695  +  fflush(stderr);
        22696  +}
        22697  +#define ERR_TRACE(rc, msg)        \
        22698  +        if( (rc) != SQLITE_OK ) _ERR_TRACE msg;
        22699  +#else
        22700  +#define ERR_TRACE(rc, msg)
        22701  +#endif
        22702  +
        22703  +/*
        22704  +** Helper functions to obtain and relinquish the global mutex. The
        22705  +** global mutex is used to protect os2ShmNodeList.
        22706  +**
        22707  +** Function os2ShmMutexHeld() is used to assert() that the global mutex 
        22708  +** is held when required. This function is only used as part of assert() 
        22709  +** statements. e.g.
        22710  +**
        22711  +**   os2ShmEnterMutex()
        22712  +**     assert( os2ShmMutexHeld() );
        22713  +**   os2ShmLeaveMutex()
        22714  +*/
        22715  +static void os2ShmEnterMutex(void){
        22716  +  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
        22717  +}
        22718  +static void os2ShmLeaveMutex(void){
        22719  +  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
        22720  +}
        22721  +#ifdef SQLITE_DEBUG
        22722  +static int os2ShmMutexHeld(void) {
        22723  +  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
        22724  +}
        22725  +int GetCurrentProcessId(void) {
        22726  +  PPIB pib;
        22727  +  DosGetInfoBlocks(NULL, &pib);
        22728  +  return (int)pib->pib_ulpid;
        22729  +}
        22730  +#endif
        22731  +
        22732  +/*
        22733  +** Object used to represent a the shared memory area for a single log file.
        22734  +** When multiple threads all reference the same log-summary, each thread has
        22735  +** its own os2File object, but they all point to a single instance of this 
        22736  +** object.  In other words, each log-summary is opened only once per process.
        22737  +**
        22738  +** os2ShmMutexHeld() must be true when creating or destroying
        22739  +** this object or while reading or writing the following fields:
        22740  +**
        22741  +**      nRef
        22742  +**      pNext 
        22743  +**
        22744  +** The following fields are read-only after the object is created:
        22745  +** 
        22746  +**      szRegion
        22747  +**      hLockFile
        22748  +**      shmBaseName
        22749  +**
        22750  +** Either os2ShmNode.mutex must be held or os2ShmNode.nRef==0 and
        22751  +** os2ShmMutexHeld() is true when reading or writing any other field
        22752  +** in this structure.
        22753  +**
        22754  +*/
        22755  +struct os2ShmNode {
        22756  +  sqlite3_mutex *mutex;      /* Mutex to access this object */
        22757  +  os2ShmNode *pNext;         /* Next in list of all os2ShmNode objects */
        22758  +
        22759  +  int szRegion;              /* Size of shared-memory regions */
        22760  +
        22761  +  int nRegion;               /* Size of array apRegion */
        22762  +  void **apRegion;           /* Array of pointers to shared-memory regions */
        22763  +
        22764  +  int nRef;                  /* Number of os2ShmLink objects pointing to this */
        22765  +  os2ShmLink *pFirst;        /* First os2ShmLink object pointing to this */
        22766  +
        22767  +  HFILE hLockFile;           /* File used for inter-process memory locking */
        22768  +  char shmBaseName[1];       /* Name of the memory object !!! must last !!! */
        22769  +};
        22770  +
        22771  +
        22772  +/*
        22773  +** Structure used internally by this VFS to record the state of an
        22774  +** open shared memory connection.
        22775  +**
        22776  +** The following fields are initialized when this object is created and
        22777  +** are read-only thereafter:
        22778  +**
        22779  +**    os2Shm.pShmNode
        22780  +**    os2Shm.id
        22781  +**
        22782  +** All other fields are read/write.  The os2Shm.pShmNode->mutex must be held
        22783  +** while accessing any read/write fields.
        22784  +*/
        22785  +struct os2ShmLink {
        22786  +  os2ShmNode *pShmNode;      /* The underlying os2ShmNode object */
        22787  +  os2ShmLink *pNext;         /* Next os2Shm with the same os2ShmNode */
        22788  +  u32 sharedMask;            /* Mask of shared locks held */
        22789  +  u32 exclMask;              /* Mask of exclusive locks held */
        22790  +#ifdef SQLITE_DEBUG
        22791  +  u8 id;                     /* Id of this connection with its os2ShmNode */
        22792  +#endif
        22793  +};
        22794  +
        22795  +
        22796  +/*
        22797  +** A global list of all os2ShmNode objects.
        22798  +**
        22799  +** The os2ShmMutexHeld() must be true while reading or writing this list.
        22800  +*/
        22801  +static os2ShmNode *os2ShmNodeList = NULL;
        22802  +
        22803  +/*
        22804  +** Constants used for locking
        22805  +*/
        22806  +#ifdef  SQLITE_OS2_NO_WAL_LOCK_FILE
        22807  +#define OS2_SHM_BASE   (PENDING_BYTE + 0x10000)         /* first lock byte */
        22808  +#else
        22809  +#define OS2_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
        22810  +#endif
        22811  +
        22812  +#define OS2_SHM_DMS    (OS2_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
        22813  +
        22814  +/*
        22815  +** Apply advisory locks for all n bytes beginning at ofst.
        22816  +*/
        22817  +#define _SHM_UNLCK  1   /* no lock */
        22818  +#define _SHM_RDLCK  2   /* shared lock, no wait */
        22819  +#define _SHM_WRLCK  3   /* exlusive lock, no wait */
        22820  +#define _SHM_WRLCK_WAIT 4 /* exclusive lock, wait */
        22821  +static int os2ShmSystemLock(
        22822  +  os2ShmNode *pNode,    /* Apply locks to this open shared-memory segment */
        22823  +  int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, _SHM_WRLCK or _SHM_WRLCK_WAIT */
        22824  +  int ofst,             /* Offset to first byte to be locked/unlocked */
        22825  +  int nByte             /* Number of bytes to lock or unlock */
        22826  +){
        22827  +  APIRET rc;
        22828  +  FILELOCK area;
        22829  +  ULONG mode, timeout;
        22830  +
        22831  +  /* Access to the os2ShmNode object is serialized by the caller */
        22832  +  assert( sqlite3_mutex_held(pNode->mutex) || pNode->nRef==0 );
        22833  +
        22834  +  mode = 1;     /* shared lock */
        22835  +  timeout = 0;  /* no wait */
        22836  +  area.lOffset = ofst;
        22837  +  area.lRange = nByte;
        22838  +
        22839  +  switch( lockType ) {
        22840  +    case _SHM_WRLCK_WAIT:
        22841  +      timeout = (ULONG)-1;      /* wait forever */
        22842  +    case _SHM_WRLCK:
        22843  +      mode = 0;                 /* exclusive lock */
        22844  +    case _SHM_RDLCK:
        22845  +      rc = DosSetFileLocks(pNode->hLockFile, 
        22846  +                           NULL, &area, timeout, mode);
        22847  +      break;
        22848  +    /* case _SHM_UNLCK: */
        22849  +    default:
        22850  +      rc = DosSetFileLocks(pNode->hLockFile, 
        22851  +                           &area, NULL, 0, 0);
        22852  +      break;
        22853  +  }
        22854  +                          
        22855  +  OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n", 
        22856  +           pNode->hLockFile,
        22857  +           rc==SQLITE_OK ? "ok" : "failed",
        22858  +           lockType==_SHM_UNLCK ? "Unlock" : "Lock",
        22859  +           rc));
        22860  +
        22861  +  ERR_TRACE(rc, ("os2ShmSystemLock: %d %s\n", rc, pNode->shmBaseName))
        22862  +
        22863  +  return ( rc == 0 ) ?  SQLITE_OK : SQLITE_BUSY;
        22864  +}
        22865  +
        22866  +/*
        22867  +** Find an os2ShmNode in global list or allocate a new one, if not found.
        22868  +**
        22869  +** This is not a VFS shared-memory method; it is a utility function called
        22870  +** by VFS shared-memory methods.
        22871  +*/
        22872  +static int os2OpenSharedMemory( os2File *fd, int szRegion ) {
        22873  +  os2ShmLink *pLink;
        22874  +  os2ShmNode *pNode;
        22875  +  int cbShmName, rc = SQLITE_OK;
        22876  +  char shmName[CCHMAXPATH + 30];
        22877  +#ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
        22878  +  ULONG action;
        22879  +#endif
        22880  +  
        22881  +  /* We need some additional space at the end to append the region number */
        22882  +  cbShmName = sprintf(shmName, "\\SHAREMEM\\%s", fd->zFullPathCp );
        22883  +  if( cbShmName >= CCHMAXPATH-8 )
        22884  +    return SQLITE_IOERR_SHMOPEN; 
        22885  +
        22886  +  /* Replace colon in file name to form a valid shared memory name */
        22887  +  shmName[10+1] = '!';
        22888  +
        22889  +  /* Allocate link object (we free it later in case of failure) */
        22890  +  pLink = sqlite3_malloc( sizeof(*pLink) );
        22891  +  if( !pLink )
        22892  +    return SQLITE_NOMEM;
        22893  +
        22894  +  /* Access node list */
        22895  +  os2ShmEnterMutex();
        22896  +
        22897  +  /* Find node by it's shared memory base name */
        22898  +  for( pNode = os2ShmNodeList; 
        22899  +       pNode && stricmp(shmName, pNode->shmBaseName) != 0; 
        22900  +       pNode = pNode->pNext )   ;
        22901  +
        22902  +  /* Not found: allocate a new node */
        22903  +  if( !pNode ) {
        22904  +    pNode = sqlite3_malloc( sizeof(*pNode) + cbShmName );
        22905  +    if( pNode ) {
        22906  +      memset(pNode, 0, sizeof(*pNode) );
        22907  +      pNode->szRegion = szRegion;
        22908  +      pNode->hLockFile = (HFILE)-1;      
        22909  +      strcpy(pNode->shmBaseName, shmName);
        22910  +
        22911  +#ifdef SQLITE_OS2_NO_WAL_LOCK_FILE
        22912  +      if( DosDupHandle(fd->h, &pNode->hLockFile) != 0 ) {
        22913  +#else
        22914  +      sprintf(shmName, "%s-lck", fd->zFullPathCp);
        22915  +      if( DosOpen((PSZ)shmName, &pNode->hLockFile, &action, 0, FILE_NORMAL, 
        22916  +                  OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
        22917  +                  OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE | 
        22918  +                  OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR,
        22919  +                  NULL) != 0 ) {
        22920  +#endif
        22921  +        sqlite3_free(pNode);  
        22922  +        rc = SQLITE_IOERR;
        22923  +      } else {
        22924  +        pNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
        22925  +        if( !pNode->mutex ) {
        22926  +          sqlite3_free(pNode);  
        22927  +          rc = SQLITE_NOMEM;
        22928  +        }
        22929  +      }   
        22930  +    } else {
        22931  +      rc = SQLITE_NOMEM;
        22932  +    }
        22933  +    
        22934  +    if( rc == SQLITE_OK ) {
        22935  +      pNode->pNext = os2ShmNodeList;
        22936  +      os2ShmNodeList = pNode;
        22937  +    } else {
        22938  +      pNode = NULL;
        22939  +    }
        22940  +  } else if( pNode->szRegion != szRegion ) {
        22941  +    rc = SQLITE_IOERR_SHMSIZE;
        22942  +    pNode = NULL;
        22943  +  }
        22944  +
        22945  +  if( pNode ) {
        22946  +    sqlite3_mutex_enter(pNode->mutex);
        22947  +
        22948  +    memset(pLink, 0, sizeof(*pLink));
        22949  +
        22950  +    pLink->pShmNode = pNode;
        22951  +    pLink->pNext = pNode->pFirst;
        22952  +    pNode->pFirst = pLink;
        22953  +    pNode->nRef++;
        22954  +
        22955  +    fd->pShmLink = pLink;
        22956  +
        22957  +    sqlite3_mutex_leave(pNode->mutex);
        22958  +    
        22959  +  } else {
        22960  +    /* Error occured. Free our link object. */
        22961  +    sqlite3_free(pLink);  
        22962  +  }
        22963  +
        22964  +  os2ShmLeaveMutex();
        22965  +
        22966  +  ERR_TRACE(rc, ("os2OpenSharedMemory: %d  %s\n", rc, fd->zFullPathCp))  
        22967  +  
        22968  +  return rc;
        22969  +}
        22970  +
        22971  +/*
        22972  +** Purge the os2ShmNodeList list of all entries with nRef==0.
        22973  +**
        22974  +** This is not a VFS shared-memory method; it is a utility function called
        22975  +** by VFS shared-memory methods.
        22976  +*/
        22977  +static void os2PurgeShmNodes( int deleteFlag ) {
        22978  +  os2ShmNode *pNode;
        22979  +  os2ShmNode **ppNode;
        22980  +
        22981  +  os2ShmEnterMutex();
        22982  +  
        22983  +  ppNode = &os2ShmNodeList;
        22984  +
        22985  +  while( *ppNode ) {
        22986  +    pNode = *ppNode;
        22987  +
        22988  +    if( pNode->nRef == 0 ) {
        22989  +      *ppNode = pNode->pNext;   
        22990  +     
        22991  +      if( pNode->apRegion ) {
        22992  +        /* Prevent other processes from resizing the shared memory */
        22993  +        os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
        22994  +
        22995  +        while( pNode->nRegion-- ) {
        22996  +#ifdef SQLITE_DEBUG
        22997  +          int rc = 
        22998  +#endif          
        22999  +          DosFreeMem(pNode->apRegion[pNode->nRegion]);
        23000  +
        23001  +          OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
        23002  +                  (int)GetCurrentProcessId(), pNode->nRegion,
        23003  +                  rc == 0 ? "ok" : "failed"));
        23004  +        }
        23005  +
        23006  +        /* Allow other processes to resize the shared memory */
        23007  +        os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
        23008  +
        23009  +        sqlite3_free(pNode->apRegion);
        23010  +      }  
        23011  +
        23012  +      DosClose(pNode->hLockFile);
        23013  +      
        23014  +#ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
        23015  +      if( deleteFlag ) {
        23016  +         char fileName[CCHMAXPATH];
        23017  +         /* Skip "\\SHAREMEM\\" */
        23018  +         sprintf(fileName, "%s-lck", pNode->shmBaseName + 10);
        23019  +         /* restore colon */
        23020  +         fileName[1] = ':';
        23021  +         
        23022  +         DosForceDelete(fileName); 
        23023  +      }
        23024  +#endif
        23025  +
        23026  +      sqlite3_mutex_free(pNode->mutex);
        23027  +
        23028  +      sqlite3_free(pNode);
        23029  +      
        23030  +    } else {
        23031  +      ppNode = &pNode->pNext;
        23032  +    }
        23033  +  } 
        23034  +
        23035  +  os2ShmLeaveMutex();
        23036  +}
        23037  +
        23038  +/*
        23039  +** This function is called to obtain a pointer to region iRegion of the
        23040  +** shared-memory associated with the database file id. Shared-memory regions
        23041  +** are numbered starting from zero. Each shared-memory region is szRegion
        23042  +** bytes in size.
        23043  +**
        23044  +** If an error occurs, an error code is returned and *pp is set to NULL.
        23045  +**
        23046  +** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
        23047  +** region has not been allocated (by any client, including one running in a
        23048  +** separate process), then *pp is set to NULL and SQLITE_OK returned. If
        23049  +** bExtend is non-zero and the requested shared-memory region has not yet
        23050  +** been allocated, it is allocated by this function.
        23051  +**
        23052  +** If the shared-memory region has already been allocated or is allocated by
        23053  +** this call as described above, then it is mapped into this processes
        23054  +** address space (if it is not already), *pp is set to point to the mapped
        23055  +** memory and SQLITE_OK returned.
        23056  +*/
        23057  +static int os2ShmMap(
        23058  +  sqlite3_file *id,               /* Handle open on database file */
        23059  +  int iRegion,                    /* Region to retrieve */
        23060  +  int szRegion,                   /* Size of regions */
        23061  +  int bExtend,                    /* True to extend block if necessary */
        23062  +  void volatile **pp              /* OUT: Mapped memory */
        23063  +){
        23064  +  PVOID pvTemp;
        23065  +  void **apRegion;
        23066  +  os2ShmNode *pNode;
        23067  +  int n, rc = SQLITE_OK;
        23068  +  char shmName[CCHMAXPATH];
        23069  +  os2File *pFile = (os2File*)id;
        23070  +  
        23071  +  *pp = NULL;
        23072  +
        23073  +  if( !pFile->pShmLink )
        23074  +    rc = os2OpenSharedMemory( pFile, szRegion );
        23075  +  
        23076  +  if( rc == SQLITE_OK ) {
        23077  +    pNode = pFile->pShmLink->pShmNode ;
        23078  +    
        23079  +    sqlite3_mutex_enter(pNode->mutex);
        23080  +    
        23081  +    assert( szRegion==pNode->szRegion );
        23082  +
        23083  +    /* Unmapped region ? */
        23084  +    if( iRegion >= pNode->nRegion ) {
        23085  +      /* Prevent other processes from resizing the shared memory */
        23086  +      os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
        23087  +
        23088  +      apRegion = sqlite3_realloc(
        23089  +        pNode->apRegion, (iRegion + 1) * sizeof(apRegion[0]));
        23090  +
        23091  +      if( apRegion ) {
        23092  +        pNode->apRegion = apRegion;
        23093  +
        23094  +        while( pNode->nRegion <= iRegion ) {
        23095  +          sprintf(shmName, "%s-%u", 
        23096  +                  pNode->shmBaseName, pNode->nRegion);
        23097  +
        23098  +          if( DosGetNamedSharedMem(&pvTemp, (PSZ)shmName, 
        23099  +                PAG_READ | PAG_WRITE) != NO_ERROR ) {
        23100  +            if( !bExtend )
        23101  +              break;
        23102  +
        23103  +            if( DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
        23104  +                  PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_ANY) != NO_ERROR && 
        23105  +                DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
        23106  +                  PAG_READ | PAG_WRITE | PAG_COMMIT) != NO_ERROR ) { 
        23107  +              rc = SQLITE_NOMEM;
        23108  +              break;
        23109  +            }
        23110  +          }
        23111  +
        23112  +          apRegion[pNode->nRegion++] = pvTemp;
        23113  +        }
        23114  +
        23115  +        /* zero out remaining entries */ 
        23116  +        for( n = pNode->nRegion; n <= iRegion; n++ )
        23117  +          pNode->apRegion[n] = NULL;
        23118  +
        23119  +        /* Return this region (maybe zero) */
        23120  +        *pp = pNode->apRegion[iRegion];
        23121  +      } else {
        23122  +        rc = SQLITE_NOMEM;
        23123  +      }
        23124  +
        23125  +      /* Allow other processes to resize the shared memory */
        23126  +      os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
        23127  +      
        23128  +    } else {
        23129  +      /* Region has been mapped previously */
        23130  +      *pp = pNode->apRegion[iRegion];
        23131  +    }
        23132  +
        23133  +    sqlite3_mutex_leave(pNode->mutex);
        23134  +  } 
        23135  +
        23136  +  ERR_TRACE(rc, ("os2ShmMap: %s iRgn = %d, szRgn = %d, bExt = %d : %d\n", 
        23137  +                 pFile->zFullPathCp, iRegion, szRegion, bExtend, rc))
        23138  +          
        23139  +  return rc;
        23140  +}
        23141  +
        23142  +/*
        23143  +** Close a connection to shared-memory.  Delete the underlying
        23144  +** storage if deleteFlag is true.
        23145  +**
        23146  +** If there is no shared memory associated with the connection then this
        23147  +** routine is a harmless no-op.
        23148  +*/
        23149  +static int os2ShmUnmap(
        23150  +  sqlite3_file *id,               /* The underlying database file */
        23151  +  int deleteFlag                  /* Delete shared-memory if true */
        23152  +){
        23153  +  os2File *pFile = (os2File*)id;
        23154  +  os2ShmLink *pLink = pFile->pShmLink;
        23155  +  
        23156  +  if( pLink ) {
        23157  +    int nRef = -1;
        23158  +    os2ShmLink **ppLink;
        23159  +    os2ShmNode *pNode = pLink->pShmNode;
        23160  +
        23161  +    sqlite3_mutex_enter(pNode->mutex);
        23162  +    
        23163  +    for( ppLink = &pNode->pFirst;
        23164  +         *ppLink && *ppLink != pLink;
        23165  +         ppLink = &(*ppLink)->pNext )   ;
        23166  +         
        23167  +    assert(*ppLink);
        23168  +
        23169  +    if( *ppLink ) {
        23170  +      *ppLink = pLink->pNext;
        23171  +      nRef = --pNode->nRef;
        23172  +    } else {
        23173  +      ERR_TRACE(1, ("os2ShmUnmap: link not found ! %s\n", 
        23174  +                    pNode->shmBaseName))
        23175  +    }
        23176  +    
        23177  +    pFile->pShmLink = NULL;
        23178  +    sqlite3_free(pLink);
        23179  +
        23180  +    sqlite3_mutex_leave(pNode->mutex);
        23181  +    
        23182  +    if( nRef == 0 )
        23183  +      os2PurgeShmNodes( deleteFlag );
        23184  +  }
        23185  +
        23186  +  return SQLITE_OK;
        23187  +}
        23188  +
        23189  +/*
        23190  +** Change the lock state for a shared-memory segment.
        23191  +**
        23192  +** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
        23193  +** different here than in posix.  In xShmLock(), one can go from unlocked
        23194  +** to shared and back or from unlocked to exclusive and back.  But one may
        23195  +** not go from shared to exclusive or from exclusive to shared.
        23196  +*/
        23197  +static int os2ShmLock(
        23198  +  sqlite3_file *id,          /* Database file holding the shared memory */
        23199  +  int ofst,                  /* First lock to acquire or release */
        23200  +  int n,                     /* Number of locks to acquire or release */
        23201  +  int flags                  /* What to do with the lock */
        23202  +){
        23203  +  u32 mask;                             /* Mask of locks to take or release */
        23204  +  int rc = SQLITE_OK;                   /* Result code */
        23205  +  os2File *pFile = (os2File*)id;
        23206  +  os2ShmLink *p = pFile->pShmLink;      /* The shared memory being locked */
        23207  +  os2ShmLink *pX;                       /* For looping over all siblings */
        23208  +  os2ShmNode *pShmNode = p->pShmNode;   /* Our node */
        23209  +  
        23210  +  assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
        23211  +  assert( n>=1 );
        23212  +  assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
        23213  +       || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
        23214  +       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
        23215  +       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
        23216  +  assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
        23217  +
        23218  +  mask = (u32)((1U<<(ofst+n)) - (1U<<ofst));
        23219  +  assert( n>1 || mask==(1<<ofst) );
        23220  +
        23221  +
        23222  +  sqlite3_mutex_enter(pShmNode->mutex);
        23223  +
        23224  +  if( flags & SQLITE_SHM_UNLOCK ){
        23225  +    u32 allMask = 0; /* Mask of locks held by siblings */
        23226  +
        23227  +    /* See if any siblings hold this same lock */
        23228  +    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
        23229  +      if( pX==p ) continue;
        23230  +      assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
        23231  +      allMask |= pX->sharedMask;
        23232  +    }
        23233  +
        23234  +    /* Unlock the system-level locks */
        23235  +    if( (mask & allMask)==0 ){
        23236  +      rc = os2ShmSystemLock(pShmNode, _SHM_UNLCK, ofst+OS2_SHM_BASE, n);
        23237  +    }else{
        23238  +      rc = SQLITE_OK;
        23239  +    }
        23240  +
        23241  +    /* Undo the local locks */
        23242  +    if( rc==SQLITE_OK ){
        23243  +      p->exclMask &= ~mask;
        23244  +      p->sharedMask &= ~mask;
        23245  +    } 
        23246  +  }else if( flags & SQLITE_SHM_SHARED ){
        23247  +    u32 allShared = 0;  /* Union of locks held by connections other than "p" */
        23248  +
        23249  +    /* Find out which shared locks are already held by sibling connections.
        23250  +    ** If any sibling already holds an exclusive lock, go ahead and return
        23251  +    ** SQLITE_BUSY.
        23252  +    */
        23253  +    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
        23254  +      if( (pX->exclMask & mask)!=0 ){
        23255  +        rc = SQLITE_BUSY;
        23256  +        break;
        23257  +      }
        23258  +      allShared |= pX->sharedMask;
        23259  +    }
        23260  +
        23261  +    /* Get shared locks at the system level, if necessary */
        23262  +    if( rc==SQLITE_OK ){
        23263  +      if( (allShared & mask)==0 ){
        23264  +        rc = os2ShmSystemLock(pShmNode, _SHM_RDLCK, ofst+OS2_SHM_BASE, n);
        23265  +      }else{
        23266  +        rc = SQLITE_OK;
        23267  +      }
        23268  +    }
        23269  +
        23270  +    /* Get the local shared locks */
        23271  +    if( rc==SQLITE_OK ){
        23272  +      p->sharedMask |= mask;
        23273  +    }
        23274  +  }else{
        23275  +    /* Make sure no sibling connections hold locks that will block this
        23276  +    ** lock.  If any do, return SQLITE_BUSY right away.
        23277  +    */
        23278  +    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
        23279  +      if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
        23280  +        rc = SQLITE_BUSY;
        23281  +        break;
        23282  +      }
        23283  +    }
        23284  +  
        23285  +    /* Get the exclusive locks at the system level.  Then if successful
        23286  +    ** also mark the local connection as being locked.
        23287  +    */
        23288  +    if( rc==SQLITE_OK ){
        23289  +      rc = os2ShmSystemLock(pShmNode, _SHM_WRLCK, ofst+OS2_SHM_BASE, n);
        23290  +      if( rc==SQLITE_OK ){
        23291  +        assert( (p->sharedMask & mask)==0 );
        23292  +        p->exclMask |= mask;
        23293  +      }
        23294  +    }
        23295  +  }
        23296  +
        23297  +  sqlite3_mutex_leave(pShmNode->mutex);
        23298  +  
        23299  +  OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
        23300  +           p->id, (int)GetCurrentProcessId(), p->sharedMask, p->exclMask,
        23301  +           rc ? "failed" : "ok"));
        23302  +
        23303  +  ERR_TRACE(rc, ("os2ShmLock: ofst = %d, n = %d, flags = 0x%x -> %d \n", 
        23304  +                 ofst, n, flags, rc))
        23305  +                  
        23306  +  return rc; 
        23307  +}
        23308  +
        23309  +/*
        23310  +** Implement a memory barrier or memory fence on shared memory.
        23311  +**
        23312  +** All loads and stores begun before the barrier must complete before
        23313  +** any load or store begun after the barrier.
        23314  +*/
        23315  +static void os2ShmBarrier(
        23316  +  sqlite3_file *id                /* Database file holding the shared memory */
        23317  +){
        23318  +  UNUSED_PARAMETER(id);
        23319  +  os2ShmEnterMutex();
        23320  +  os2ShmLeaveMutex();
        23321  +}
        23322  +
        23323  +#else
        23324  +# define os2ShmMap     0
        23325  +# define os2ShmLock    0
        23326  +# define os2ShmBarrier 0
        23327  +# define os2ShmUnmap   0
        23328  +#endif /* #ifndef SQLITE_OMIT_WAL */
        23329  +
 22382  23330   
 22383  23331   /*
 22384  23332   ** This vector defines all the methods that can operate on an
 22385  23333   ** sqlite3_file for os2.
 22386  23334   */
 22387  23335   static const sqlite3_io_methods os2IoMethod = {
 22388         -  1,                        /* iVersion */
 22389         -  os2Close,
 22390         -  os2Read,
 22391         -  os2Write,
 22392         -  os2Truncate,
 22393         -  os2Sync,
 22394         -  os2FileSize,
 22395         -  os2Lock,
 22396         -  os2Unlock,
 22397         -  os2CheckReservedLock,
 22398         -  os2FileControl,
 22399         -  os2SectorSize,
 22400         -  os2DeviceCharacteristics
        23336  +  2,                              /* iVersion */
        23337  +  os2Close,                       /* xClose */
        23338  +  os2Read,                        /* xRead */
        23339  +  os2Write,                       /* xWrite */
        23340  +  os2Truncate,                    /* xTruncate */
        23341  +  os2Sync,                        /* xSync */
        23342  +  os2FileSize,                    /* xFileSize */
        23343  +  os2Lock,                        /* xLock */
        23344  +  os2Unlock,                      /* xUnlock */
        23345  +  os2CheckReservedLock,           /* xCheckReservedLock */
        23346  +  os2FileControl,                 /* xFileControl */
        23347  +  os2SectorSize,                  /* xSectorSize */
        23348  +  os2DeviceCharacteristics,       /* xDeviceCharacteristics */
        23349  +  os2ShmMap,                      /* xShmMap */
        23350  +  os2ShmLock,                     /* xShmLock */
        23351  +  os2ShmBarrier,                  /* xShmBarrier */
        23352  +  os2ShmUnmap                     /* xShmUnmap */
 22401  23353   };
        23354  +
 22402  23355   
 22403  23356   /***************************************************************************
 22404  23357   ** Here ends the I/O methods that form the sqlite3_io_methods object.
 22405  23358   **
 22406  23359   ** The next block of code implements the VFS methods.
 22407  23360   ****************************************************************************/
 22408  23361   
 22409  23362   /*
 22410  23363   ** Create a temporary file name in zBuf.  zBuf must be big enough to
 22411  23364   ** hold at pVfs->mxPathname characters.
 22412  23365   */
 22413  23366   static int getTempname(int nBuf, char *zBuf ){
 22414         -  static const unsigned char zChars[] =
        23367  +  static const char zChars[] =
 22415  23368       "abcdefghijklmnopqrstuvwxyz"
 22416  23369       "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 22417  23370       "0123456789";
 22418  23371     int i, j;
 22419         -  char zTempPathBuf[3];
 22420         -  PSZ zTempPath = (PSZ)&zTempPathBuf;
 22421         -  if( sqlite3_temp_directory ){
 22422         -    zTempPath = sqlite3_temp_directory;
 22423         -  }else{
 22424         -    if( DosScanEnv( (PSZ)"TEMP", &zTempPath ) ){
 22425         -      if( DosScanEnv( (PSZ)"TMP", &zTempPath ) ){
 22426         -        if( DosScanEnv( (PSZ)"TMPDIR", &zTempPath ) ){
 22427         -           ULONG ulDriveNum = 0, ulDriveMap = 0;
 22428         -           DosQueryCurrentDisk( &ulDriveNum, &ulDriveMap );
 22429         -           sprintf( (char*)zTempPath, "%c:", (char)( 'A' + ulDriveNum - 1 ) );
 22430         -        }
 22431         -      }
 22432         -    }
        23372  +  PSZ zTempPathCp;      
        23373  +  char zTempPath[CCHMAXPATH];
        23374  +  ULONG ulDriveNum, ulDriveMap;
        23375  +  
        23376  +  /* It's odd to simulate an io-error here, but really this is just
        23377  +  ** using the io-error infrastructure to test that SQLite handles this
        23378  +  ** function failing. 
        23379  +  */
        23380  +  SimulateIOError( return SQLITE_IOERR );
        23381  +
        23382  +  if( sqlite3_temp_directory ) {
        23383  +    sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", sqlite3_temp_directory);
        23384  +  } else if( DosScanEnv( (PSZ)"TEMP",   &zTempPathCp ) == NO_ERROR ||
        23385  +             DosScanEnv( (PSZ)"TMP",    &zTempPathCp ) == NO_ERROR ||
        23386  +             DosScanEnv( (PSZ)"TMPDIR", &zTempPathCp ) == NO_ERROR ) {
        23387  +    char *zTempPathUTF = convertCpPathToUtf8( (char *)zTempPathCp );
        23388  +    sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", zTempPathUTF);
        23389  +    free( zTempPathUTF );
        23390  +  } else if( DosQueryCurrentDisk( &ulDriveNum, &ulDriveMap ) == NO_ERROR ) {
        23391  +    zTempPath[0] = (char)('A' + ulDriveNum - 1);
        23392  +    zTempPath[1] = ':'; 
        23393  +    zTempPath[2] = '\0'; 
        23394  +  } else {
        23395  +    zTempPath[0] = '\0'; 
 22433  23396     }
        23397  +  
 22434  23398     /* Strip off a trailing slashes or backslashes, otherwise we would get *
 22435  23399      * multiple (back)slashes which causes DosOpen() to fail.              *
 22436  23400      * Trailing spaces are not allowed, either.                            */
 22437  23401     j = sqlite3Strlen30(zTempPath);
 22438         -  while( j > 0 && ( zTempPath[j-1] == '\\' || zTempPath[j-1] == '/'
 22439         -                    || zTempPath[j-1] == ' ' ) ){
        23402  +  while( j > 0 && ( zTempPath[j-1] == '\\' || zTempPath[j-1] == '/' || 
        23403  +                    zTempPath[j-1] == ' ' ) ){
 22440  23404       j--;
 22441  23405     }
 22442  23406     zTempPath[j] = '\0';
 22443         -  if( !sqlite3_temp_directory ){
 22444         -    char *zTempPathUTF = convertCpPathToUtf8( zTempPath );
 22445         -    sqlite3_snprintf( nBuf-30, zBuf,
 22446         -                      "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPathUTF );
 22447         -    free( zTempPathUTF );
 22448         -  }else{
 22449         -    sqlite3_snprintf( nBuf-30, zBuf,
 22450         -                      "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath );
 22451         -  }
 22452         -  j = sqlite3Strlen30( zBuf );
        23407  +  
        23408  +  /* We use 20 bytes to randomize the name */
        23409  +  sqlite3_snprintf(nBuf-22, zBuf,
        23410  +                   "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
        23411  +  j = sqlite3Strlen30(zBuf);
 22453  23412     sqlite3_randomness( 20, &zBuf[j] );
 22454  23413     for( i = 0; i < 20; i++, j++ ){
 22455         -    zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
        23414  +    zBuf[j] = zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
 22456  23415     }
 22457  23416     zBuf[j] = 0;
        23417  +
 22458  23418     OSTRACE(( "TEMP FILENAME: %s\n", zBuf ));
 22459  23419     return SQLITE_OK;
 22460  23420   }
 22461  23421   
 22462  23422   
 22463  23423   /*
 22464  23424   ** Turn a relative pathname into a full pathname.  Write the full
................................................................................
 22470  23430     const char *zRelative,      /* Possibly relative input path */
 22471  23431     int nFull,                  /* Size of output buffer in bytes */
 22472  23432     char *zFull                 /* Output buffer */
 22473  23433   ){
 22474  23434     char *zRelativeCp = convertUtf8PathToCp( zRelative );
 22475  23435     char zFullCp[CCHMAXPATH] = "\0";
 22476  23436     char *zFullUTF;
 22477         -  APIRET rc = DosQueryPathInfo( zRelativeCp, FIL_QUERYFULLNAME, zFullCp,
 22478         -                                CCHMAXPATH );
        23437  +  APIRET rc = DosQueryPathInfo( (PSZ)zRelativeCp, FIL_QUERYFULLNAME, 
        23438  +                                zFullCp, CCHMAXPATH );
 22479  23439     free( zRelativeCp );
 22480  23440     zFullUTF = convertCpPathToUtf8( zFullCp );
 22481  23441     sqlite3_snprintf( nFull, zFull, zFullUTF );
 22482  23442     free( zFullUTF );
 22483  23443     return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
 22484  23444   }
 22485  23445   
 22486  23446   
 22487  23447   /*
 22488  23448   ** Open a file.
 22489  23449   */
 22490  23450   static int os2Open(
 22491  23451     sqlite3_vfs *pVfs,            /* Not used */
 22492         -  const char *zName,            /* Name of the file */
        23452  +  const char *zName,            /* Name of the file (UTF-8) */
 22493  23453     sqlite3_file *id,             /* Write the SQLite file handle here */
 22494  23454     int flags,                    /* Open mode flags */
 22495  23455     int *pOutFlags                /* Status return flags */
 22496  23456   ){
 22497  23457     HFILE h;
 22498         -  ULONG ulFileAttribute = FILE_NORMAL;
 22499  23458     ULONG ulOpenFlags = 0;
 22500  23459     ULONG ulOpenMode = 0;
        23460  +  ULONG ulAction = 0;
        23461  +  ULONG rc;
 22501  23462     os2File *pFile = (os2File*)id;
 22502         -  APIRET rc = NO_ERROR;
 22503         -  ULONG ulAction;
        23463  +  const char *zUtf8Name = zName;
 22504  23464     char *zNameCp;
 22505         -  char zTmpname[CCHMAXPATH+1];    /* Buffer to hold name of temp file */
        23465  +  char  zTmpname[CCHMAXPATH];
        23466  +
        23467  +  int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
        23468  +  int isCreate     = (flags & SQLITE_OPEN_CREATE);
        23469  +  int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
        23470  +#ifndef NDEBUG
        23471  +  int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
        23472  +  int isReadonly   = (flags & SQLITE_OPEN_READONLY);
        23473  +  int eType        = (flags & 0xFFFFFF00);
        23474  +  int isOpenJournal = (isCreate && (
        23475  +        eType==SQLITE_OPEN_MASTER_JOURNAL 
        23476  +     || eType==SQLITE_OPEN_MAIN_JOURNAL 
        23477  +     || eType==SQLITE_OPEN_WAL
        23478  +  ));
        23479  +#endif
        23480  +
        23481  +  UNUSED_PARAMETER(pVfs);
        23482  +  assert( id!=0 );
        23483  +
        23484  +  /* Check the following statements are true: 
        23485  +  **
        23486  +  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
        23487  +  **   (b) if CREATE is set, then READWRITE must also be set, and
        23488  +  **   (c) if EXCLUSIVE is set, then CREATE must also be set.
        23489  +  **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
        23490  +  */
        23491  +  assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
        23492  +  assert(isCreate==0 || isReadWrite);
        23493  +  assert(isExclusive==0 || isCreate);
        23494  +  assert(isDelete==0 || isCreate);
        23495  +
        23496  +  /* The main DB, main journal, WAL file and master journal are never 
        23497  +  ** automatically deleted. Nor are they ever temporary files.  */
        23498  +  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
        23499  +  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
        23500  +  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
        23501  +  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
        23502  +
        23503  +  /* Assert that the upper layer has set one of the "file-type" flags. */
        23504  +  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
        23505  +       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
        23506  +       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
        23507  +       || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
        23508  +  );
        23509  +
        23510  +  memset( pFile, 0, sizeof(*pFile) );
        23511  +  pFile->h = (HFILE)-1;
 22506  23512   
 22507  23513     /* If the second argument to this function is NULL, generate a 
 22508  23514     ** temporary file name to use 
 22509  23515     */
 22510         -  if( !zName ){
 22511         -    int rc = getTempname(CCHMAXPATH+1, zTmpname);
        23516  +  if( !zUtf8Name ){
        23517  +    assert(isDelete && !isOpenJournal);
        23518  +    rc = getTempname(CCHMAXPATH, zTmpname);
 22512  23519       if( rc!=SQLITE_OK ){
 22513  23520         return rc;
 22514  23521       }
 22515         -    zName = zTmpname;
        23522  +    zUtf8Name = zTmpname;
 22516  23523     }
 22517  23524   
 22518         -
 22519         -  memset( pFile, 0, sizeof(*pFile) );
 22520         -
 22521         -  OSTRACE(( "OPEN want %d\n", flags ));
 22522         -
 22523         -  if( flags & SQLITE_OPEN_READWRITE ){
        23525  +  if( isReadWrite ){
 22524  23526       ulOpenMode |= OPEN_ACCESS_READWRITE;
 22525         -    OSTRACE(( "OPEN read/write\n" ));
 22526         -  }else{
 22527         -    ulOpenMode |= OPEN_ACCESS_READONLY;
 22528         -    OSTRACE(( "OPEN read only\n" ));
 22529         -  }
 22530         -
 22531         -  if( flags & SQLITE_OPEN_CREATE ){
 22532         -    ulOpenFlags |= OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW;
 22533         -    OSTRACE(( "OPEN open new/create\n" ));
 22534         -  }else{
 22535         -    ulOpenFlags |= OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW;
 22536         -    OSTRACE(( "OPEN open existing\n" ));
 22537         -  }
 22538         -
 22539         -  if( flags & SQLITE_OPEN_MAIN_DB ){
 22540         -    ulOpenMode |= OPEN_SHARE_DENYNONE;
 22541         -    OSTRACE(( "OPEN share read/write\n" ));
 22542         -  }else{
 22543         -    ulOpenMode |= OPEN_SHARE_DENYWRITE;
 22544         -    OSTRACE(( "OPEN share read only\n" ));
 22545         -  }
 22546         -
 22547         -  if( flags & SQLITE_OPEN_DELETEONCLOSE ){
 22548         -    char pathUtf8[CCHMAXPATH];
 22549         -#ifdef NDEBUG /* when debugging we want to make sure it is deleted */
 22550         -    ulFileAttribute = FILE_HIDDEN;
 22551         -#endif
 22552         -    os2FullPathname( pVfs, zName, CCHMAXPATH, pathUtf8 );
 22553         -    pFile->pathToDel = convertUtf8PathToCp( pathUtf8 );
 22554         -    OSTRACE(( "OPEN hidden/delete on close file attributes\n" ));
 22555         -  }else{
 22556         -    pFile->pathToDel = NULL;
 22557         -    OSTRACE(( "OPEN normal file attribute\n" ));
 22558         -  }
 22559         -
 22560         -  /* always open in random access mode for possibly better speed */
 22561         -  ulOpenMode |= OPEN_FLAGS_RANDOM;
 22562         -  ulOpenMode |= OPEN_FLAGS_FAIL_ON_ERROR;
 22563         -  ulOpenMode |= OPEN_FLAGS_NOINHERIT;
 22564         -
 22565         -  zNameCp = convertUtf8PathToCp( zName );
        23527  +  }else{
        23528  +    ulOpenMode |= OPEN_ACCESS_READONLY;
        23529  +  }
        23530  +
        23531  +  /* Open in random access mode for possibly better speed.  Allow full
        23532  +  ** sharing because file locks will provide exclusive access when needed.
        23533  +  ** The handle should not be inherited by child processes and we don't 
        23534  +  ** want popups from the critical error handler.
        23535  +  */
        23536  +  ulOpenMode |= OPEN_FLAGS_RANDOM | OPEN_SHARE_DENYNONE | 
        23537  +                OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR;
        23538  +
        23539  +  /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is 
        23540  +  ** created. SQLite doesn't use it to indicate "exclusive access" 
        23541  +  ** as it is usually understood.
        23542  +  */
        23543  +  if( isExclusive ){
        23544  +    /* Creates a new file, only if it does not already exist. */
        23545  +    /* If the file exists, it fails. */
        23546  +    ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_FAIL_IF_EXISTS;
        23547  +  }else if( isCreate ){
        23548  +    /* Open existing file, or create if it doesn't exist */
        23549  +    ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
        23550  +  }else{
        23551  +    /* Opens a file, only if it exists. */
        23552  +    ulOpenFlags |= OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
        23553  +  }
        23554  +
        23555  +  zNameCp = convertUtf8PathToCp( zUtf8Name );
 22566  23556     rc = DosOpen( (PSZ)zNameCp,
 22567  23557                   &h,
 22568  23558                   &ulAction,
 22569  23559                   0L,
 22570         -                ulFileAttribute,
        23560  +                FILE_NORMAL,
 22571  23561                   ulOpenFlags,
 22572  23562                   ulOpenMode,
 22573  23563                   (PEAOP2)NULL );
 22574  23564     free( zNameCp );
        23565  +
 22575  23566     if( rc != NO_ERROR ){
 22576         -    OSTRACE(( "OPEN Invalid handle rc=%d: zName=%s, ulAction=%#lx, ulAttr=%#lx, ulFlags=%#lx, ulMode=%#lx\n",
 22577         -              rc, zName, ulAction, ulFileAttribute, ulOpenFlags, ulOpenMode ));
 22578         -    if( pFile->pathToDel )
 22579         -      free( pFile->pathToDel );
 22580         -    pFile->pathToDel = NULL;
 22581         -    if( flags & SQLITE_OPEN_READWRITE ){
 22582         -      OSTRACE(( "OPEN %d Invalid handle\n",
 22583         -                ((flags | SQLITE_OPEN_READONLY) & ~SQLITE_OPEN_READWRITE) ));
        23567  +    OSTRACE(( "OPEN Invalid handle rc=%d: zName=%s, ulAction=%#lx, ulFlags=%#lx, ulMode=%#lx\n",
        23568  +              rc, zUtf8Name, ulAction, ulOpenFlags, ulOpenMode ));
        23569  +
        23570  +    if( isReadWrite ){
 22584  23571         return os2Open( pVfs, zName, id,
 22585         -                      ((flags | SQLITE_OPEN_READONLY) & ~SQLITE_OPEN_READWRITE),
        23572  +                      ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
 22586  23573                         pOutFlags );
 22587  23574       }else{
 22588  23575         return SQLITE_CANTOPEN;
 22589  23576       }
 22590  23577     }
 22591  23578   
 22592  23579     if( pOutFlags ){
 22593         -    *pOutFlags = flags & SQLITE_OPEN_READWRITE ? SQLITE_OPEN_READWRITE : SQLITE_OPEN_READONLY;
        23580  +    *pOutFlags = isReadWrite ? SQLITE_OPEN_READWRITE : SQLITE_OPEN_READONLY;
 22594  23581     }
 22595  23582   
        23583  +  os2FullPathname( pVfs, zUtf8Name, sizeof( zTmpname ), zTmpname );
        23584  +  pFile->zFullPathCp = convertUtf8PathToCp( zTmpname );
 22596  23585     pFile->pMethod = &os2IoMethod;
        23586  +  pFile->flags = flags;
 22597  23587     pFile->h = h;
        23588  +
 22598  23589     OpenCounter(+1);
 22599  23590     OSTRACE(( "OPEN %d pOutFlags=%d\n", pFile->h, pOutFlags ));
 22600  23591     return SQLITE_OK;
 22601  23592   }
 22602  23593   
 22603  23594   /*
 22604  23595   ** Delete the named file.
 22605  23596   */
 22606  23597   static int os2Delete(
 22607  23598     sqlite3_vfs *pVfs,                     /* Not used on os2 */
 22608  23599     const char *zFilename,                 /* Name of file to delete */
 22609  23600     int syncDir                            /* Not used on os2 */
 22610  23601   ){
 22611         -  APIRET rc = NO_ERROR;
 22612         -  char *zFilenameCp = convertUtf8PathToCp( zFilename );
        23602  +  APIRET rc;
        23603  +  char *zFilenameCp;
 22613  23604     SimulateIOError( return SQLITE_IOERR_DELETE );
        23605  +  zFilenameCp = convertUtf8PathToCp( zFilename );
 22614  23606     rc = DosDelete( (PSZ)zFilenameCp );
 22615  23607     free( zFilenameCp );
 22616  23608     OSTRACE(( "DELETE \"%s\"\n", zFilename ));
 22617         -  return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR_DELETE;
        23609  +  return (rc == NO_ERROR ||
        23610  +          rc == ERROR_FILE_NOT_FOUND ||
        23611  +          rc == ERROR_PATH_NOT_FOUND ) ? SQLITE_OK : SQLITE_IOERR_DELETE;
 22618  23612   }
 22619  23613   
 22620  23614   /*
 22621  23615   ** Check the existance and status of a file.
 22622  23616   */
 22623  23617   static int os2Access(
 22624  23618     sqlite3_vfs *pVfs,        /* Not used on os2 */
 22625  23619     const char *zFilename,    /* Name of file to check */
 22626  23620     int flags,                /* Type of test to make on this file */
 22627  23621     int *pOut                 /* Write results here */
 22628  23622   ){
        23623  +  APIRET rc;
 22629  23624     FILESTATUS3 fsts3ConfigInfo;
 22630         -  APIRET rc = NO_ERROR;
 22631         -  char *zFilenameCp = convertUtf8PathToCp( zFilename );
        23625  +  char *zFilenameCp;
 22632  23626   
 22633         -  memset( &fsts3ConfigInfo, 0, sizeof(fsts3ConfigInfo) );
        23627  +  UNUSED_PARAMETER(pVfs);
        23628  +  SimulateIOError( return SQLITE_IOERR_ACCESS; );
        23629  +  
        23630  +  zFilenameCp = convertUtf8PathToCp( zFilename );
 22634  23631     rc = DosQueryPathInfo( (PSZ)zFilenameCp, FIL_STANDARD,
 22635  23632                            &fsts3ConfigInfo, sizeof(FILESTATUS3) );
 22636  23633     free( zFilenameCp );
 22637  23634     OSTRACE(( "ACCESS fsts3ConfigInfo.attrFile=%d flags=%d rc=%d\n",
 22638  23635               fsts3ConfigInfo.attrFile, flags, rc ));
        23636  +
 22639  23637     switch( flags ){
        23638  +    case SQLITE_ACCESS_EXISTS:
        23639  +      /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
        23640  +      ** as if it does not exist.
        23641  +      */
        23642  +      if( fsts3ConfigInfo.cbFile == 0 ) 
        23643  +        rc = ERROR_FILE_NOT_FOUND;
        23644  +      break;
 22640  23645       case SQLITE_ACCESS_READ:
 22641         -    case SQLITE_ACCESS_EXISTS:
 22642         -      rc = (rc == NO_ERROR);
 22643         -      OSTRACE(( "ACCESS %s access of read and exists  rc=%d\n", zFilename, rc));
 22644  23646         break;
 22645  23647       case SQLITE_ACCESS_READWRITE:
 22646         -      rc = (rc == NO_ERROR) && ( (fsts3ConfigInfo.attrFile & FILE_READONLY) == 0 );
 22647         -      OSTRACE(( "ACCESS %s access of read/write  rc=%d\n", zFilename, rc ));
        23648  +      if( fsts3ConfigInfo.attrFile & FILE_READONLY )
        23649  +        rc = ERROR_ACCESS_DENIED;
 22648  23650         break;
 22649  23651       default:
        23652  +      rc = ERROR_FILE_NOT_FOUND;
 22650  23653         assert( !"Invalid flags argument" );
 22651  23654     }
 22652         -  *pOut = rc;
        23655  +
        23656  +  *pOut = (rc == NO_ERROR);
        23657  +  OSTRACE(( "ACCESS %s flags %d: rc=%d\n", zFilename, flags, *pOut ));
        23658  +
 22653  23659     return SQLITE_OK;
 22654  23660   }
 22655  23661   
 22656  23662   
 22657  23663   #ifndef SQLITE_OMIT_LOAD_EXTENSION
 22658  23664   /*
 22659  23665   ** Interfaces for opening a shared library, finding entry points
................................................................................
 22660  23666   ** within the shared library, and closing the shared library.
 22661  23667   */
 22662  23668   /*
 22663  23669   ** Interfaces for opening a shared library, finding entry points
 22664  23670   ** within the shared library, and closing the shared library.
 22665  23671   */
 22666  23672   static void *os2DlOpen(sqlite3_vfs *pVfs, const char *zFilename){
 22667         -  UCHAR loadErr[256];
 22668  23673     HMODULE hmod;
 22669  23674     APIRET rc;
 22670  23675     char *zFilenameCp = convertUtf8PathToCp(zFilename);
 22671         -  rc = DosLoadModule((PSZ)loadErr, sizeof(loadErr), zFilenameCp, &hmod);
        23676  +  rc = DosLoadModule(NULL, 0, (PSZ)zFilenameCp, &hmod);
 22672  23677     free(zFilenameCp);
 22673  23678     return rc != NO_ERROR ? 0 : (void*)hmod;
 22674  23679   }
 22675  23680   /*
 22676  23681   ** A no-op since the error code is returned on the DosLoadModule call.
 22677  23682   ** os2Dlopen returns zero if DosLoadModule is not successful.
 22678  23683   */
 22679  23684   static void os2DlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
 22680  23685   /* no-op */
 22681  23686   }
 22682         -static void *os2DlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){
        23687  +static void (*os2DlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
 22683  23688     PFN pfn;
 22684  23689     APIRET rc;
 22685         -  rc = DosQueryProcAddr((HMODULE)pHandle, 0L, zSymbol, &pfn);
        23690  +  rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)zSymbol, &pfn);
 22686  23691     if( rc != NO_ERROR ){
 22687  23692       /* if the symbol itself was not found, search again for the same
 22688  23693        * symbol with an extra underscore, that might be needed depending
 22689  23694        * on the calling convention */
 22690  23695       char _zSymbol[256] = "_";
 22691         -    strncat(_zSymbol, zSymbol, 255);
 22692         -    rc = DosQueryProcAddr((HMODULE)pHandle, 0L, _zSymbol, &pfn);
        23696  +    strncat(_zSymbol, zSymbol, 254);
        23697  +    rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)_zSymbol, &pfn);
 22693  23698     }
 22694         -  return rc != NO_ERROR ? 0 : (void*)pfn;
        23699  +  return rc != NO_ERROR ? 0 : (void(*)(void))pfn;
 22695  23700   }
 22696  23701   static void os2DlClose(sqlite3_vfs *pVfs, void *pHandle){
 22697  23702     DosFreeModule((HMODULE)pHandle);
 22698  23703   }
 22699  23704   #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
 22700  23705     #define os2DlOpen 0
 22701  23706     #define os2DlError 0
................................................................................
 22709  23714   */
 22710  23715   static int os2Randomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf ){
 22711  23716     int n = 0;
 22712  23717   #if defined(SQLITE_TEST)
 22713  23718     n = nBuf;
 22714  23719     memset(zBuf, 0, nBuf);
 22715  23720   #else
 22716         -  int sizeofULong = sizeof(ULONG);
 22717         -  if( (int)sizeof(DATETIME) <= nBuf - n ){
 22718         -    DATETIME x;
 22719         -    DosGetDateTime(&x);
 22720         -    memcpy(&zBuf[n], &x, sizeof(x));
 22721         -    n += sizeof(x);
 22722         -  }
 22723         -
 22724         -  if( sizeofULong <= nBuf - n ){
 22725         -    PPIB ppib;
 22726         -    DosGetInfoBlocks(NULL, &ppib);
 22727         -    memcpy(&zBuf[n], &ppib->pib_ulpid, sizeofULong);
 22728         -    n += sizeofULong;
 22729         -  }
 22730         -
 22731         -  if( sizeofULong <= nBuf - n ){
 22732         -    PTIB ptib;
 22733         -    DosGetInfoBlocks(&ptib, NULL);
 22734         -    memcpy(&zBuf[n], &ptib->tib_ptib2->tib2_ultid, sizeofULong);
 22735         -    n += sizeofULong;
 22736         -  }
 22737         -
 22738         -  /* if we still haven't filled the buffer yet the following will */
 22739         -  /* grab everything once instead of making several calls for a single item */
 22740         -  if( sizeofULong <= nBuf - n ){
 22741         -    ULONG ulSysInfo[QSV_MAX];
 22742         -    DosQuerySysInfo(1L, QSV_MAX, ulSysInfo, sizeofULong * QSV_MAX);
 22743         -
 22744         -    memcpy(&zBuf[n], &ulSysInfo[QSV_MS_COUNT - 1], sizeofULong);
 22745         -    n += sizeofULong;
 22746         -
 22747         -    if( sizeofULong <= nBuf - n ){
 22748         -      memcpy(&zBuf[n], &ulSysInfo[QSV_TIMER_INTERVAL - 1], sizeofULong);
 22749         -      n += sizeofULong;
 22750         -    }
 22751         -    if( sizeofULong <= nBuf - n ){
 22752         -      memcpy(&zBuf[n], &ulSysInfo[QSV_TIME_LOW - 1], sizeofULong);
 22753         -      n += sizeofULong;
 22754         -    }
 22755         -    if( sizeofULong <= nBuf - n ){
 22756         -      memcpy(&zBuf[n], &ulSysInfo[QSV_TIME_HIGH - 1], sizeofULong);
 22757         -      n += sizeofULong;
 22758         -    }
 22759         -    if( sizeofULong <= nBuf - n ){
 22760         -      memcpy(&zBuf[n], &ulSysInfo[QSV_TOTAVAILMEM - 1], sizeofULong);
 22761         -      n += sizeofULong;
 22762         -    }
 22763         -  }
        23721  +  int i;                           
        23722  +  PPIB ppib;
        23723  +  PTIB ptib;
        23724  +  DATETIME dt; 
        23725  +  static unsigned c = 0;
        23726  +  /* Ordered by variation probability */
        23727  +  static ULONG svIdx[6] = { QSV_MS_COUNT, QSV_TIME_LOW,
        23728  +                            QSV_MAXPRMEM, QSV_MAXSHMEM,
        23729  +                            QSV_TOTAVAILMEM, QSV_TOTRESMEM };
        23730  +
        23731  +  /* 8 bytes; timezone and weekday don't increase the randomness much */
        23732  +  if( (int)sizeof(dt)-3 <= nBuf - n ){
        23733  +    c += 0x0100;
        23734  +    DosGetDateTime(&dt);
        23735  +    dt.year = (USHORT)((dt.year - 1900) | c);
        23736  +    memcpy(&zBuf[n], &dt, sizeof(dt)-3);
        23737  +    n += sizeof(dt)-3;
        23738  +  }
        23739  +
        23740  +  /* 4 bytes; PIDs and TIDs are 16 bit internally, so combine them */
        23741  +  if( (int)sizeof(ULONG) <= nBuf - n ){
        23742  +    DosGetInfoBlocks(&ptib, &ppib);
        23743  +    *(PULONG)&zBuf[n] = MAKELONG(ppib->pib_ulpid,
        23744  +                                 ptib->tib_ptib2->tib2_ultid);
        23745  +    n += sizeof(ULONG);
        23746  +  }
        23747  +
        23748  +  /* Up to 6 * 4 bytes; variables depend on the system state */
        23749  +  for( i = 0; i < 6 && (int)sizeof(ULONG) <= nBuf - n; i++ ){
        23750  +    DosQuerySysInfo(svIdx[i], svIdx[i], 
        23751  +                    (PULONG)&zBuf[n], sizeof(ULONG));
        23752  +    n += sizeof(ULONG);
        23753  +  } 
 22764  23754   #endif
 22765  23755   
 22766  23756     return n;
 22767  23757   }
 22768  23758   
 22769  23759   /*
 22770  23760   ** Sleep for a little while.  Return the amount of time slept.
................................................................................
 22784  23774   ** returned from sqlite3OsCurrentTime().  This is used for testing.
 22785  23775   */
 22786  23776   #ifdef SQLITE_TEST
 22787  23777   SQLITE_API int sqlite3_current_time = 0;
 22788  23778   #endif
 22789  23779   
 22790  23780   /*
 22791         -** Find the current time (in Universal Coordinated Time).  Write the
 22792         -** current time and date as a Julian Day number into *prNow and
 22793         -** return 0.  Return 1 if the time and date cannot be found.
        23781  +** Find the current time (in Universal Coordinated Time).  Write into *piNow
        23782  +** the current time and date as a Julian Day number times 86_400_000.  In
        23783  +** other words, write into *piNow the number of milliseconds since the Julian
        23784  +** epoch of noon in Greenwich on November 24, 4714 B.C according to the
        23785  +** proleptic Gregorian calendar.
        23786  +**
        23787  +** On success, return 0.  Return 1 if the time and date cannot be found.
 22794  23788   */
 22795         -int os2CurrentTime( sqlite3_vfs *pVfs, double *prNow ){
 22796         -  double now;
 22797         -  SHORT minute; /* needs to be able to cope with negative timezone offset */
 22798         -  USHORT second, hour,
 22799         -         day, month, year;
        23789  +static int os2CurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
        23790  +#ifdef SQLITE_TEST
        23791  +  static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
        23792  +#endif
        23793  +  int year, month, datepart, timepart;
        23794  + 
 22800  23795     DATETIME dt;
 22801  23796     DosGetDateTime( &dt );
 22802         -  second = (USHORT)dt.seconds;
 22803         -  minute = (SHORT)dt.minutes + dt.timezone;
 22804         -  hour = (USHORT)dt.hours;
 22805         -  day = (USHORT)dt.day;
 22806         -  month = (USHORT)dt.month;
 22807         -  year = (USHORT)dt.year;
        23797  +
        23798  +  year = dt.year;
        23799  +  month = dt.month;
 22808  23800   
 22809  23801     /* Calculations from http://www.astro.keele.ac.uk/~rno/Astronomy/hjd.html
 22810         -     http://www.astro.keele.ac.uk/~rno/Astronomy/hjd-0.1.c */
 22811         -  /* Calculate the Julian days */
 22812         -  now = day - 32076 +
        23802  +  ** http://www.astro.keele.ac.uk/~rno/Astronomy/hjd-0.1.c
        23803  +  ** Calculate the Julian days
        23804  +  */
        23805  +  datepart = (int)dt.day - 32076 +
 22813  23806       1461*(year + 4800 + (month - 14)/12)/4 +
 22814  23807       367*(month - 2 - (month - 14)/12*12)/12 -
 22815  23808       3*((year + 4900 + (month - 14)/12)/100)/4;
 22816  23809   
 22817         -  /* Add the fractional hours, mins and seconds */
 22818         -  now += (hour + 12.0)/24.0;
 22819         -  now += minute/1440.0;
 22820         -  now += second/86400.0;
 22821         -  *prNow = now;
        23810  +  /* Time in milliseconds, hours to noon added */
        23811  +  timepart = 12*3600*1000 + dt.hundredths*10 + dt.seconds*1000 +
        23812  +    ((int)dt.minutes + dt.timezone)*60*1000 + dt.hours*3600*1000;
        23813  +
        23814  +  *piNow = (sqlite3_int64)datepart*86400*1000 + timepart;
        23815  +   
 22822  23816   #ifdef SQLITE_TEST
 22823  23817     if( sqlite3_current_time ){
 22824         -    *prNow = sqlite3_current_time/86400.0 + 2440587.5;
        23818  +    *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
 22825  23819     }
 22826  23820   #endif
        23821  +
        23822  +  UNUSED_PARAMETER(pVfs);
 22827  23823     return 0;
 22828  23824   }
 22829  23825   
        23826  +/*
        23827  +** Find the current time (in Universal Coordinated Time).  Write the
        23828  +** current time and date as a Julian Day number into *prNow and
        23829  +** return 0.  Return 1 if the time and date cannot be found.
        23830  +*/
        23831  +static int os2CurrentTime( sqlite3_vfs *pVfs, double *prNow ){
        23832  +  int rc;
        23833  +  sqlite3_int64 i;
        23834  +  rc = os2CurrentTimeInt64(pVfs, &i);
        23835  +  if( !rc ){
        23836  +    *prNow = i/86400000.0;
        23837  +  }
        23838  +  return rc;
        23839  +}
        23840  +
        23841  +/*
        23842  +** The idea is that this function works like a combination of
        23843  +** GetLastError() and FormatMessage() on windows (or errno and
        23844  +** strerror_r() on unix). After an error is returned by an OS
        23845  +** function, SQLite calls this function with zBuf pointing to
        23846  +** a buffer of nBuf bytes. The OS layer should populate the
        23847  +** buffer with a nul-terminated UTF-8 encoded error message
        23848  +** describing the last IO error to have occurred within the calling
        23849  +** thread.
        23850  +**
        23851  +** If the error message is too large for the supplied buffer,
        23852  +** it should be truncated. The return value of xGetLastError
        23853  +** is zero if the error message fits in the buffer, or non-zero
        23854  +** otherwise (if the message was truncated). If non-zero is returned,
        23855  +** then it is not necessary to include the nul-terminator character
        23856  +** in the output buffer.
        23857  +**
        23858  +** Not supplying an error message will have no adverse effect
        23859  +** on SQLite. It is fine to have an implementation that never
        23860  +** returns an error message:
        23861  +**
        23862  +**   int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
        23863  +**     assert(zBuf[0]=='\0');
        23864  +**     return 0;
        23865  +**   }
        23866  +**
        23867  +** However if an error message is supplied, it will be incorporated
        23868  +** by sqlite into the error message available to the user using
        23869  +** sqlite3_errmsg(), possibly making IO errors easier to debug.
        23870  +*/
 22830  23871   static int os2GetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
        23872  +  assert(zBuf[0]=='\0');
 22831  23873     return 0;
 22832  23874   }
 22833  23875   
 22834  23876   /*
 22835  23877   ** Initialize and deinitialize the operating system interface.
 22836  23878   */
 22837  23879   SQLITE_API int sqlite3_os_init(void){
 22838  23880     static sqlite3_vfs os2Vfs = {
 22839         -    1,                 /* iVersion */
        23881  +    3,                 /* iVersion */
 22840  23882       sizeof(os2File),   /* szOsFile */
 22841  23883       CCHMAXPATH,        /* mxPathname */
 22842  23884       0,                 /* pNext */
 22843  23885       "os2",             /* zName */
 22844  23886       0,                 /* pAppData */
 22845  23887   
 22846  23888       os2Open,           /* xOpen */
................................................................................
 22851  23893       os2DlError,        /* xDlError */
 22852  23894       os2DlSym,          /* xDlSym */
 22853  23895       os2DlClose,        /* xDlClose */
 22854  23896       os2Randomness,     /* xRandomness */
 22855  23897       os2Sleep,          /* xSleep */
 22856  23898       os2CurrentTime,    /* xCurrentTime */
 22857  23899       os2GetLastError,   /* xGetLastError */
        23900  +    os2CurrentTimeInt64, /* xCurrentTimeInt64 */
        23901  +    0,                 /* xSetSystemCall */
        23902  +    0,                 /* xGetSystemCall */
        23903  +    0                  /* xNextSystemCall */
 22858  23904     };
 22859  23905     sqlite3_vfs_register(&os2Vfs, 1);
 22860  23906     initUconvObjects();
        23907  +/*  sqlite3OSTrace = 1; */
 22861  23908     return SQLITE_OK;
 22862  23909   }
 22863  23910   SQLITE_API int sqlite3_os_end(void){
 22864  23911     freeUconvObjects();
 22865  23912     return SQLITE_OK;
 22866  23913   }
 22867  23914   
................................................................................
 23070  24117   typedef struct unixFile unixFile;
 23071  24118   struct unixFile {
 23072  24119     sqlite3_io_methods const *pMethod;  /* Always the first entry */
 23073  24120     unixInodeInfo *pInode;              /* Info about locks on this inode */
 23074  24121     int h;                              /* The file descriptor */
 23075  24122     int dirfd;                          /* File descriptor for the directory */
 23076  24123     unsigned char eFileLock;            /* The type of lock held on this fd */
        24124  +  unsigned char ctrlFlags;            /* Behavioral bits.  UNIXFILE_* flags */
 23077  24125     int lastErrno;                      /* The unix errno from last I/O error */
 23078  24126     void *lockingContext;               /* Locking style specific state */
 23079  24127     UnixUnusedFd *pUnused;              /* Pre-allocated UnixUnusedFd */
 23080         -  int fileFlags;                      /* Miscellanous flags */
 23081  24128     const char *zPath;                  /* Name of the file */
 23082  24129     unixShm *pShm;                      /* Shared memory segment information */
 23083  24130     int szChunk;                        /* Configured by FCNTL_CHUNK_SIZE */
 23084  24131   #if SQLITE_ENABLE_LOCKING_STYLE
 23085  24132     int openFlags;                      /* The flags specified at open() */
 23086  24133   #endif
 23087  24134   #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
................................................................................
 23108  24155     ** it is larger than the struct CrashFile defined in test6.c.
 23109  24156     */
 23110  24157     char aPadding[32];
 23111  24158   #endif
 23112  24159   };
 23113  24160   
 23114  24161   /*
 23115         -** The following macros define bits in unixFile.fileFlags
        24162  +** Allowed values for the unixFile.ctrlFlags bitmask:
 23116  24163   */
 23117         -#define SQLITE_WHOLE_FILE_LOCKING  0x0001   /* Use whole-file locking */
        24164  +#define UNIXFILE_EXCL   0x01     /* Connections from one process only */
        24165  +#define UNIXFILE_RDONLY 0x02     /* Connection is read only */
 23118  24166   
 23119  24167   /*
 23120  24168   ** Include code that is common to all os_*.c files
 23121  24169   */
 23122  24170   /************** Include os_common.h in the middle of os_unix.c ***************/
 23123  24171   /************** Begin file os_common.h ***************************************/
 23124  24172   /*
................................................................................
 23339  24387   #ifndef O_NOFOLLOW
 23340  24388   # define O_NOFOLLOW 0
 23341  24389   #endif
 23342  24390   #ifndef O_BINARY
 23343  24391   # define O_BINARY 0
 23344  24392   #endif
 23345  24393   
 23346         -/*
 23347         -** The DJGPP compiler environment looks mostly like Unix, but it
 23348         -** lacks the fcntl() system call.  So redefine fcntl() to be something
 23349         -** that always succeeds.  This means that locking does not occur under
 23350         -** DJGPP.  But it is DOS - what did you expect?
 23351         -*/
 23352         -#ifdef __DJGPP__
 23353         -# define fcntl(A,B,C) 0
 23354         -#endif
 23355         -
 23356  24394   /*
 23357  24395   ** The threadid macro resolves to the thread-id or to 0.  Used for
 23358  24396   ** testing and debugging only.
 23359  24397   */
 23360  24398   #if SQLITE_THREADSAFE
 23361  24399   #define threadid pthread_self()
 23362  24400   #else
 23363  24401   #define threadid 0
 23364  24402   #endif
 23365  24403   
        24404  +/*
        24405  +** Many system calls are accessed through pointer-to-functions so that
        24406  +** they may be overridden at runtime to facilitate fault injection during
        24407  +** testing and sandboxing.  The following array holds the names and pointers
        24408  +** to all overrideable system calls.
        24409  +*/
        24410  +static struct unix_syscall {
        24411  +  const char *zName;            /* Name of the sytem call */
        24412  +  sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
        24413  +  sqlite3_syscall_ptr pDefault; /* Default value */
        24414  +} aSyscall[] = {
        24415  +  { "open",         (sqlite3_syscall_ptr)open,       0  },
        24416  +#define osOpen      ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
        24417  +
        24418  +  { "close",        (sqlite3_syscall_ptr)close,      0  },
        24419  +#define osClose     ((int(*)(int))aSyscall[1].pCurrent)
        24420  +
        24421  +  { "access",       (sqlite3_syscall_ptr)access,     0  },
        24422  +#define osAccess    ((int(*)(const char*,int))aSyscall[2].pCurrent)
        24423  +
        24424  +  { "getcwd",       (sqlite3_syscall_ptr)getcwd,     0  },
        24425  +#define osGetcwd    ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
        24426  +
        24427  +  { "stat",         (sqlite3_syscall_ptr)stat,       0  },
        24428  +#define osStat      ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
        24429  +
        24430  +/*
        24431  +** The DJGPP compiler environment looks mostly like Unix, but it
        24432  +** lacks the fcntl() system call.  So redefine fcntl() to be something
        24433  +** that always succeeds.  This means that locking does not occur under
        24434  +** DJGPP.  But it is DOS - what did you expect?
        24435  +*/
        24436  +#ifdef __DJGPP__
        24437  +  { "fstat",        0,                 0  },
        24438  +#define osFstat(a,b,c)    0
        24439  +#else     
        24440  +  { "fstat",        (sqlite3_syscall_ptr)fstat,      0  },
        24441  +#define osFstat     ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
        24442  +#endif
        24443  +
        24444  +  { "ftruncate",    (sqlite3_syscall_ptr)ftruncate,  0  },
        24445  +#define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
        24446  +
        24447  +  { "fcntl",        (sqlite3_syscall_ptr)fcntl,      0  },
        24448  +#define osFcntl     ((int(*)(int,int,...))aSyscall[7].pCurrent)
        24449  +
        24450  +  { "read",         (sqlite3_syscall_ptr)read,       0  },
        24451  +#define osRead      ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
        24452  +
        24453  +#if defined(USE_PREAD) || defined(SQLITE_ENABLE_LOCKING_STYLE)
        24454  +  { "pread",        (sqlite3_syscall_ptr)pread,      0  },
        24455  +#else
        24456  +  { "pread",        (sqlite3_syscall_ptr)0,          0  },
        24457  +#endif
        24458  +#define osPread     ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
        24459  +
        24460  +#if defined(USE_PREAD64)
        24461  +  { "pread64",      (sqlite3_syscall_ptr)pread64,    0  },
        24462  +#else
        24463  +  { "pread64",      (sqlite3_syscall_ptr)0,          0  },
        24464  +#endif
        24465  +#define osPread64   ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)
        24466  +
        24467  +  { "write",        (sqlite3_syscall_ptr)write,      0  },
        24468  +#define osWrite     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
        24469  +
        24470  +#if defined(USE_PREAD) || defined(SQLITE_ENABLE_LOCKING_STYLE)
        24471  +  { "pwrite",       (sqlite3_syscall_ptr)pwrite,     0  },
        24472  +#else
        24473  +  { "pwrite",       (sqlite3_syscall_ptr)0,          0  },
        24474  +#endif
        24475  +#define osPwrite    ((ssize_t(*)(int,const void*,size_t,off_t))\
        24476  +                    aSyscall[12].pCurrent)
        24477  +
        24478  +#if defined(USE_PREAD64)
        24479  +  { "pwrite64",     (sqlite3_syscall_ptr)pwrite64,   0  },
        24480  +#else
        24481  +  { "pwrite64",     (sqlite3_syscall_ptr)0,          0  },
        24482  +#endif
        24483  +#define osPwrite64  ((ssize_t(*)(int,const void*,size_t,off_t))\
        24484  +                    aSyscall[13].pCurrent)
        24485  +
        24486  +#if SQLITE_ENABLE_LOCKING_STYLE
        24487  +  { "fchmod",       (sqlite3_syscall_ptr)fchmod,     0  },
        24488  +#define osFchmod    ((int(*)(int,mode_t))aSyscall[14].pCurrent)
        24489  +#endif
        24490  +
        24491  +#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
        24492  +  { "fallocate",    (sqlite3_syscall_ptr)posix_fallocate,  0 },
        24493  +#else
        24494  +  { "fallocate",    (sqlite3_syscall_ptr)0,                0 },
        24495  +#endif
        24496  +#define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
        24497  +
        24498  +}; /* End of the overrideable system calls */
        24499  +
        24500  +/*
        24501  +** This is the xSetSystemCall() method of sqlite3_vfs for all of the
        24502  +** "unix" VFSes.  Return SQLITE_OK opon successfully updating the
        24503  +** system call pointer, or SQLITE_NOTFOUND if there is no configurable
        24504  +** system call named zName.
        24505  +*/
        24506  +static int unixSetSystemCall(
        24507  +  sqlite3_vfs *pNotUsed,        /* The VFS pointer.  Not used */
        24508  +  const char *zName,            /* Name of system call to override */
        24509  +  sqlite3_syscall_ptr pNewFunc  /* Pointer to new system call value */
        24510  +){
        24511  +  unsigned int i;
        24512  +  int rc = SQLITE_NOTFOUND;
        24513  +
        24514  +  UNUSED_PARAMETER(pNotUsed);
        24515  +  if( zName==0 ){
        24516  +    /* If no zName is given, restore all system calls to their default
        24517  +    ** settings and return NULL
        24518  +    */
        24519  +    rc = SQLITE_OK;
        24520  +    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
        24521  +      if( aSyscall[i].pDefault ){
        24522  +        aSyscall[i].pCurrent = aSyscall[i].pDefault;
        24523  +      }
        24524  +    }
        24525  +  }else{
        24526  +    /* If zName is specified, operate on only the one system call
        24527  +    ** specified.
        24528  +    */
        24529  +    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
        24530  +      if( strcmp(zName, aSyscall[i].zName)==0 ){
        24531  +        if( aSyscall[i].pDefault==0 ){
        24532  +          aSyscall[i].pDefault = aSyscall[i].pCurrent;
        24533  +        }
        24534  +        rc = SQLITE_OK;
        24535  +        if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
        24536  +        aSyscall[i].pCurrent = pNewFunc;
        24537  +        break;
        24538  +      }
        24539  +    }
        24540  +  }
        24541  +  return rc;
        24542  +}
        24543  +
        24544  +/*
        24545  +** Return the value of a system call.  Return NULL if zName is not a
        24546  +** recognized system call name.  NULL is also returned if the system call
        24547  +** is currently undefined.
        24548  +*/
        24549  +static sqlite3_syscall_ptr unixGetSystemCall(
        24550  +  sqlite3_vfs *pNotUsed,
        24551  +  const char *zName
        24552  +){
        24553  +  unsigned int i;
        24554  +
        24555  +  UNUSED_PARAMETER(pNotUsed);
        24556  +  for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
        24557  +    if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
        24558  +  }
        24559  +  return 0;
        24560  +}
        24561  +
        24562  +/*
        24563  +** Return the name of the first system call after zName.  If zName==NULL
        24564  +** then return the name of the first system call.  Return NULL if zName
        24565  +** is the last system call or if zName is not the name of a valid
        24566  +** system call.
        24567  +*/
        24568  +static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
        24569  +  int i = -1;
        24570  +
        24571  +  UNUSED_PARAMETER(p);
        24572  +  if( zName ){
        24573  +    for(i=0; i<ArraySize(aSyscall)-1; i++){
        24574  +      if( strcmp(zName, aSyscall[i].zName)==0 ) break;
        24575  +    }
        24576  +  }
        24577  +  for(i++; i<ArraySize(aSyscall); i++){
        24578  +    if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
        24579  +  }
        24580  +  return 0;
        24581  +}
        24582  +
        24583  +/*
        24584  +** Retry open() calls that fail due to EINTR
        24585  +*/
        24586  +static int robust_open(const char *z, int f, int m){
        24587  +  int rc;
        24588  +  do{ rc = osOpen(z,f,m); }while( rc<0 && errno==EINTR );
        24589  +  return rc;
        24590  +}
 23366  24591   
 23367  24592   /*
 23368  24593   ** Helper functions to obtain and relinquish the global mutex. The
 23369  24594   ** global mutex is used to protect the unixInodeInfo and
 23370  24595   ** vxworksFileId objects used by this file, all of which may be 
 23371  24596   ** shared by multiple threads.
 23372  24597   **
................................................................................
 23423  24648     int s;
 23424  24649     int savedErrno;
 23425  24650     if( op==F_GETLK ){
 23426  24651       zOpName = "GETLK";
 23427  24652     }else if( op==F_SETLK ){
 23428  24653       zOpName = "SETLK";
 23429  24654     }else{
 23430         -    s = fcntl(fd, op, p);
        24655  +    s = osFcntl(fd, op, p);
 23431  24656       sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
 23432  24657       return s;
 23433  24658     }
 23434  24659     if( p->l_type==F_RDLCK ){
 23435  24660       zType = "RDLCK";
 23436  24661     }else if( p->l_type==F_WRLCK ){
 23437  24662       zType = "WRLCK";
 23438  24663     }else if( p->l_type==F_UNLCK ){
 23439  24664       zType = "UNLCK";
 23440  24665     }else{
 23441  24666       assert( 0 );
 23442  24667     }
 23443  24668     assert( p->l_whence==SEEK_SET );
 23444         -  s = fcntl(fd, op, p);
        24669  +  s = osFcntl(fd, op, p);
 23445  24670     savedErrno = errno;
 23446  24671     sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
 23447  24672        threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
 23448  24673        (int)p->l_pid, s);
 23449  24674     if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
 23450  24675       struct flock l2;
 23451  24676       l2 = *p;
 23452         -    fcntl(fd, F_GETLK, &l2);
        24677  +    osFcntl(fd, F_GETLK, &l2);
 23453  24678       if( l2.l_type==F_RDLCK ){
 23454  24679         zType = "RDLCK";
 23455  24680       }else if( l2.l_type==F_WRLCK ){
 23456  24681         zType = "WRLCK";
 23457  24682       }else if( l2.l_type==F_UNLCK ){
 23458  24683         zType = "UNLCK";
 23459  24684       }else{
................................................................................
 23461  24686       }
 23462  24687       sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
 23463  24688          zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
 23464  24689     }
 23465  24690     errno = savedErrno;
 23466  24691     return s;
 23467  24692   }
 23468         -#define fcntl lockTrace
        24693  +#undef osFcntl
        24694  +#define osFcntl lockTrace
 23469  24695   #endif /* SQLITE_LOCK_TRACE */
 23470  24696   
 23471         -
        24697  +/*
        24698  +** Retry ftruncate() calls that fail due to EINTR
        24699  +*/
        24700  +static int robust_ftruncate(int h, sqlite3_int64 sz){
        24701  +  int rc;
        24702  +  do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
        24703  +  return rc;
        24704  +}
 23472  24705   
 23473  24706   /*
 23474  24707   ** This routine translates a standard POSIX errno code into something
 23475  24708   ** useful to the clients of the sqlite3 functions.  Specifically, it is
 23476  24709   ** intended to translate a variety of "try again" errors into SQLITE_BUSY
 23477  24710   ** and a variety of "please close the file descriptor NOW" errors into 
 23478  24711   ** SQLITE_IOERR
 23479  24712   ** 
 23480  24713   ** Errors during initialization of locks, or file system support for locks,
 23481  24714   ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
 23482  24715   */
 23483  24716   static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
 23484  24717     switch (posixError) {
        24718  +#if 0
        24719  +  /* At one point this code was not commented out. In theory, this branch
        24720  +  ** should never be hit, as this function should only be called after
        24721  +  ** a locking-related function (i.e. fcntl()) has returned non-zero with
        24722  +  ** the value of errno as the first argument. Since a system call has failed,
        24723  +  ** errno should be non-zero.
        24724  +  **
        24725  +  ** Despite this, if errno really is zero, we still don't want to return
        24726  +  ** SQLITE_OK. The system call failed, and *some* SQLite error should be
        24727  +  ** propagated back to the caller. Commenting this branch out means errno==0
        24728  +  ** will be handled by the "default:" case below.
        24729  +  */
 23485  24730     case 0: 
 23486  24731       return SQLITE_OK;
 23487         -    
        24732  +#endif
        24733  +
 23488  24734     case EAGAIN:
 23489  24735     case ETIMEDOUT:
 23490  24736     case EBUSY:
 23491  24737     case EINTR:
 23492  24738     case ENOLCK:  
 23493  24739       /* random NFS retry error, unless during file system support 
 23494  24740        * introspection, in which it actually means what it says */
................................................................................
 23502  24748   	(sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
 23503  24749         return SQLITE_BUSY;
 23504  24750       }
 23505  24751       /* else fall through */
 23506  24752     case EPERM: 
 23507  24753       return SQLITE_PERM;
 23508  24754       
        24755  +  /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
        24756  +  ** this module never makes such a call. And the code in SQLite itself 
        24757  +  ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons
        24758  +  ** this case is also commented out. If the system does set errno to EDEADLK,
        24759  +  ** the default SQLITE_IOERR_XXX code will be returned. */
        24760  +#if 0
 23509  24761     case EDEADLK:
 23510  24762       return SQLITE_IOERR_BLOCKED;
        24763  +#endif
 23511  24764       
 23512  24765   #if EOPNOTSUPP!=ENOTSUP
 23513  24766     case EOPNOTSUPP: 
 23514  24767       /* something went terribly awry, unless during file system support 
 23515  24768        * introspection, in which it actually means what it says */
 23516  24769   #endif
 23517  24770   #ifdef ENOTSUP
................................................................................
 23786  25039   ** A single inode can have multiple file descriptors, so each unixFile
 23787  25040   ** structure contains a pointer to an instance of this object and this
 23788  25041   ** object keeps a count of the number of unixFile pointing to it.
 23789  25042   */
 23790  25043   struct unixInodeInfo {
 23791  25044     struct unixFileId fileId;       /* The lookup key */
 23792  25045     int nShared;                    /* Number of SHARED locks held */
 23793         -  int eFileLock;                  /* One of SHARED_LOCK, RESERVED_LOCK etc. */
        25046  +  unsigned char eFileLock;        /* One of SHARED_LOCK, RESERVED_LOCK etc. */
        25047  +  unsigned char bProcessLock;     /* An exclusive process lock is held */
 23794  25048     int nRef;                       /* Number of pointers to this structure */
 23795  25049     unixShmNode *pShmNode;          /* Shared memory associated with this inode */
 23796  25050     int nLock;                      /* Number of outstanding file locks */
 23797  25051     UnixUnusedFd *pUnused;          /* Unused file descriptors to close */
 23798  25052     unixInodeInfo *pNext;           /* List of all unixInodeInfo objects */
 23799  25053     unixInodeInfo *pPrev;           /*    .... doubly linked */
 23800  25054   #if defined(SQLITE_ENABLE_LOCKING_STYLE)
................................................................................
 23808  25062   
 23809  25063   /*
 23810  25064   ** A lists of all unixInodeInfo objects.
 23811  25065   */
 23812  25066   static unixInodeInfo *inodeList = 0;
 23813  25067   
 23814  25068   /*
 23815         -** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
 23816         -** If all such file descriptors are closed without error, the list is
 23817         -** cleared and SQLITE_OK returned.
 23818         -**
 23819         -** Otherwise, if an error occurs, then successfully closed file descriptor
 23820         -** entries are removed from the list, and SQLITE_IOERR_CLOSE returned. 
 23821         -** not deleted and SQLITE_IOERR_CLOSE returned.
        25069  +**
        25070  +** This function - unixLogError_x(), is only ever called via the macro
        25071  +** unixLogError().
        25072  +**
        25073  +** It is invoked after an error occurs in an OS function and errno has been
        25074  +** set. It logs a message using sqlite3_log() containing the current value of
        25075  +** errno and, if possible, the human-readable equivalent from strerror() or
        25076  +** strerror_r().
        25077  +**
        25078  +** The first argument passed to the macro should be the error code that
        25079  +** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). 
        25080  +** The two subsequent arguments should be the name of the OS function that
        25081  +** failed (e.g. "unlink", "open") and the the associated file-system path,
        25082  +** if any.
        25083  +*/
        25084  +#define unixLogError(a,b,c)     unixLogErrorAtLine(a,b,c,__LINE__)
        25085  +static int unixLogErrorAtLine(
        25086  +  int errcode,                    /* SQLite error code */
        25087  +  const char *zFunc,              /* Name of OS function that failed */
        25088  +  const char *zPath,              /* File path associated with error */
        25089  +  int iLine                       /* Source line number where error occurred */
        25090  +){
        25091  +  char *zErr;                     /* Message from strerror() or equivalent */
        25092  +  int iErrno = errno;             /* Saved syscall error number */
        25093  +
        25094  +  /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
        25095  +  ** the strerror() function to obtain the human-readable error message
        25096  +  ** equivalent to errno. Otherwise, use strerror_r().
        25097  +  */ 
        25098  +#if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
        25099  +  char aErr[80];
        25100  +  memset(aErr, 0, sizeof(aErr));
        25101  +  zErr = aErr;
        25102  +
        25103  +  /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
        25104  +  ** assume that the system provides the the GNU version of strerror_r() that 
        25105  +  ** returns a pointer to a buffer containing the error message. That pointer 
        25106  +  ** may point to aErr[], or it may point to some static storage somewhere. 
        25107  +  ** Otherwise, assume that the system provides the POSIX version of 
        25108  +  ** strerror_r(), which always writes an error message into aErr[].
        25109  +  **
        25110  +  ** If the code incorrectly assumes that it is the POSIX version that is
        25111  +  ** available, the error message will often be an empty string. Not a
        25112  +  ** huge problem. Incorrectly concluding that the GNU version is available 
        25113  +  ** could lead to a segfault though.
        25114  +  */
        25115  +#if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
        25116  +  zErr = 
        25117  +# endif
        25118  +  strerror_r(iErrno, aErr, sizeof(aErr)-1);
        25119  +
        25120  +#elif SQLITE_THREADSAFE
        25121  +  /* This is a threadsafe build, but strerror_r() is not available. */
        25122  +  zErr = "";
        25123  +#else
        25124  +  /* Non-threadsafe build, use strerror(). */
        25125  +  zErr = strerror(iErrno);
        25126  +#endif
        25127  +
        25128  +  assert( errcode!=SQLITE_OK );
        25129  +  if( zPath==0 ) zPath = "";
        25130  +  sqlite3_log(errcode,
        25131  +      "os_unix.c:%d: (%d) %s(%s) - %s",
        25132  +      iLine, iErrno, zFunc, zPath, zErr
        25133  +  );
        25134  +
        25135  +  return errcode;
        25136  +}
        25137  +
        25138  +/*
        25139  +** Close a file descriptor.
        25140  +**
        25141  +** We assume that close() almost always works, since it is only in a
        25142  +** very sick application or on a very sick platform that it might fail.
        25143  +** If it does fail, simply leak the file descriptor, but do log the
        25144  +** error.
        25145  +**
        25146  +** Note that it is not safe to retry close() after EINTR since the
        25147  +** file descriptor might have already been reused by another thread.
        25148  +** So we don't even try to recover from an EINTR.  Just log the error
        25149  +** and move on.
        25150  +*/
        25151  +static void robust_close(unixFile *pFile, int h, int lineno){
        25152  +  if( osClose(h) ){
        25153  +    unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
        25154  +                       pFile ? pFile->zPath : 0, lineno);
        25155  +  }
        25156  +}
        25157  +
        25158  +/*
        25159  +** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
 23822  25160   */ 
 23823         -static int closePendingFds(unixFile *pFile){
 23824         -  int rc = SQLITE_OK;
        25161  +static void closePendingFds(unixFile *pFile){
 23825  25162     unixInodeInfo *pInode = pFile->pInode;
 23826         -  UnixUnusedFd *pError = 0;
 23827  25163     UnixUnusedFd *p;
 23828  25164     UnixUnusedFd *pNext;
 23829  25165     for(p=pInode->pUnused; p; p=pNext){
 23830  25166       pNext = p->pNext;
 23831         -    if( close(p->fd) ){
 23832         -      pFile->lastErrno = errno;
 23833         -      rc = SQLITE_IOERR_CLOSE;
 23834         -      p->pNext = pError;
 23835         -      pError = p;
 23836         -    }else{
 23837         -      sqlite3_free(p);
 23838         -    }
        25167  +    robust_close(pFile, p->fd, __LINE__);
        25168  +    sqlite3_free(p);
 23839  25169     }
 23840         -  pInode->pUnused = pError;
 23841         -  return rc;
        25170  +  pInode->pUnused = 0;
 23842  25171   }
 23843  25172   
 23844  25173   /*
 23845  25174   ** Release a unixInodeInfo structure previously allocated by findInodeInfo().
 23846  25175   **
 23847  25176   ** The mutex entered using the unixEnterMutex() function must be held
 23848  25177   ** when this function is called.
 23849  25178   */
 23850  25179   static void releaseInodeInfo(unixFile *pFile){
 23851  25180     unixInodeInfo *pInode = pFile->pInode;
 23852  25181     assert( unixMutexHeld() );
 23853         -  if( pInode ){
        25182  +  if( ALWAYS(pInode) ){
 23854  25183       pInode->nRef--;
 23855  25184       if( pInode->nRef==0 ){
 23856  25185         assert( pInode->pShmNode==0 );
 23857  25186         closePendingFds(pFile);
 23858  25187         if( pInode->pPrev ){
 23859  25188           assert( pInode->pPrev->pNext==pInode );
 23860  25189           pInode->pPrev->pNext = pInode->pNext;
................................................................................
 23893  25222   
 23894  25223     assert( unixMutexHeld() );
 23895  25224   
 23896  25225     /* Get low-level information about the file that we can used to
 23897  25226     ** create a unique name for the file.
 23898  25227     */
 23899  25228     fd = pFile->h;
 23900         -  rc = fstat(fd, &statbuf);
        25229  +  rc = osFstat(fd, &statbuf);
 23901  25230     if( rc!=0 ){
 23902  25231       pFile->lastErrno = errno;
 23903  25232   #ifdef EOVERFLOW
 23904  25233       if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
 23905  25234   #endif
 23906  25235       return SQLITE_IOERR;
 23907  25236     }
................................................................................
 23914  25243     ** prior to accessing the inode number.  The one byte written is
 23915  25244     ** an ASCII 'S' character which also happens to be the first byte
 23916  25245     ** in the header of every SQLite database.  In this way, if there
 23917  25246     ** is a race condition such that another thread has already populated
 23918  25247     ** the first page of the database, no damage is done.
 23919  25248     */
 23920  25249     if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
 23921         -    rc = write(fd, "S", 1);
        25250  +    do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
 23922  25251       if( rc!=1 ){
 23923  25252         pFile->lastErrno = errno;
 23924  25253         return SQLITE_IOERR;
 23925  25254       }
 23926         -    rc = fstat(fd, &statbuf);
        25255  +    rc = osFstat(fd, &statbuf);
 23927  25256       if( rc!=0 ){
 23928  25257         pFile->lastErrno = errno;
 23929  25258         return SQLITE_IOERR;
 23930  25259       }
 23931  25260     }
 23932  25261   #endif
 23933  25262   
................................................................................
 23982  25311     if( pFile->pInode->eFileLock>SHARED_LOCK ){
 23983  25312       reserved = 1;
 23984  25313     }
 23985  25314   
 23986  25315     /* Otherwise see if some other process holds it.
 23987  25316     */
 23988  25317   #ifndef __DJGPP__
 23989         -  if( !reserved ){
        25318  +  if( !reserved && !pFile->pInode->bProcessLock ){
 23990  25319       struct flock lock;
 23991  25320       lock.l_whence = SEEK_SET;
 23992  25321       lock.l_start = RESERVED_BYTE;
 23993  25322       lock.l_len = 1;
 23994  25323       lock.l_type = F_WRLCK;
 23995         -    if (-1 == fcntl(pFile->h, F_GETLK, &lock)) {
 23996         -      int tErrno = errno;
 23997         -      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
 23998         -      pFile->lastErrno = tErrno;
        25324  +    if( osFcntl(pFile->h, F_GETLK, &lock) ){
        25325  +      rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
        25326  +      pFile->lastErrno = errno;
 23999  25327       } else if( lock.l_type!=F_UNLCK ){
 24000  25328         reserved = 1;
 24001  25329       }
 24002  25330     }
 24003  25331   #endif
 24004  25332     
 24005  25333     unixLeaveMutex();
 24006  25334     OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
 24007  25335   
 24008  25336     *pResOut = reserved;
 24009  25337     return rc;
 24010  25338   }
        25339  +
        25340  +/*
        25341  +** Attempt to set a system-lock on the file pFile.  The lock is 
        25342  +** described by pLock.
        25343  +**
        25344  +** If the pFile was opened read/write from unix-excl, then the only lock
        25345  +** ever obtained is an exclusive lock, and it is obtained exactly once
        25346  +** the first time any lock is attempted.  All subsequent system locking
        25347  +** operations become no-ops.  Locking operations still happen internally,
        25348  +** in order to coordinate access between separate database connections
        25349  +** within this process, but all of that is handled in memory and the
        25350  +** operating system does not participate.
        25351  +**
        25352  +** This function is a pass-through to fcntl(F_SETLK) if pFile is using
        25353  +** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
        25354  +** and is read-only.
        25355  +**
        25356  +** Zero is returned if the call completes successfully, or -1 if a call
        25357  +** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
        25358  +*/
        25359  +static int unixFileLock(unixFile *pFile, struct flock *pLock){
        25360  +  int rc;
        25361  +  unixInodeInfo *pInode = pFile->pInode;
        25362  +  assert( unixMutexHeld() );
        25363  +  assert( pInode!=0 );
        25364  +  if( ((pFile->ctrlFlags & UNIXFILE_EXCL)!=0 || pInode->bProcessLock)
        25365  +   && ((pFile->ctrlFlags & UNIXFILE_RDONLY)==0)
        25366  +  ){
        25367  +    if( pInode->bProcessLock==0 ){
        25368  +      struct flock lock;
        25369  +      assert( pInode->nLock==0 );
        25370  +      lock.l_whence = SEEK_SET;
        25371  +      lock.l_start = SHARED_FIRST;
        25372  +      lock.l_len = SHARED_SIZE;
        25373  +      lock.l_type = F_WRLCK;
        25374  +      rc = osFcntl(pFile->h, F_SETLK, &lock);
        25375  +      if( rc<0 ) return rc;
        25376  +      pInode->bProcessLock = 1;
        25377  +      pInode->nLock++;
        25378  +    }else{
        25379  +      rc = 0;
        25380  +    }
        25381  +  }else{
        25382  +    rc = osFcntl(pFile->h, F_SETLK, pLock);
        25383  +  }
        25384  +  return rc;
        25385  +}
 24011  25386   
 24012  25387   /*
 24013  25388   ** Lock the file with the lock specified by parameter eFileLock - one
 24014  25389   ** of the following:
 24015  25390   **
 24016  25391   **     (1) SHARED_LOCK
 24017  25392   **     (2) RESERVED_LOCK
................................................................................
 24072  25447     ** locking a random byte from a range, concurrent SHARED locks may exist
 24073  25448     ** even if the locking primitive used is always a write-lock.
 24074  25449     */
 24075  25450     int rc = SQLITE_OK;
 24076  25451     unixFile *pFile = (unixFile*)id;
 24077  25452     unixInodeInfo *pInode = pFile->pInode;
 24078  25453     struct flock lock;
 24079         -  int s = 0;
 24080  25454     int tErrno = 0;
 24081  25455   
 24082  25456     assert( pFile );
 24083  25457     OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
 24084  25458         azFileLock(eFileLock), azFileLock(pFile->eFileLock),
 24085  25459         azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
 24086  25460   
................................................................................
 24141  25515     lock.l_len = 1L;
 24142  25516     lock.l_whence = SEEK_SET;
 24143  25517     if( eFileLock==SHARED_LOCK 
 24144  25518         || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
 24145  25519     ){
 24146  25520       lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
 24147  25521       lock.l_start = PENDING_BYTE;
 24148         -    s = fcntl(pFile->h, F_SETLK, &lock);
 24149         -    if( s==(-1) ){
        25522  +    if( unixFileLock(pFile, &lock) ){
 24150  25523         tErrno = errno;
 24151  25524         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
 24152         -      if( IS_LOCK_ERROR(rc) ){
        25525  +      if( rc!=SQLITE_BUSY ){
 24153  25526           pFile->lastErrno = tErrno;
 24154  25527         }
 24155  25528         goto end_lock;
 24156  25529       }
 24157  25530     }
 24158  25531   
 24159  25532   
 24160  25533     /* If control gets to this point, then actually go ahead and make
 24161  25534     ** operating system calls for the specified lock.
 24162  25535     */
 24163  25536     if( eFileLock==SHARED_LOCK ){
 24164  25537       assert( pInode->nShared==0 );
 24165  25538       assert( pInode->eFileLock==0 );
        25539  +    assert( rc==SQLITE_OK );
 24166  25540   
 24167  25541       /* Now get the read-lock */
 24168  25542       lock.l_start = SHARED_FIRST;
 24169  25543       lock.l_len = SHARED_SIZE;
 24170         -    if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){
        25544  +    if( unixFileLock(pFile, &lock) ){
 24171  25545         tErrno = errno;
        25546  +      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
 24172  25547       }
        25548  +
 24173  25549       /* Drop the temporary PENDING lock */
 24174  25550       lock.l_start = PENDING_BYTE;
 24175  25551       lock.l_len = 1L;
 24176  25552       lock.l_type = F_UNLCK;
 24177         -    if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
 24178         -      if( s != -1 ){
 24179         -        /* This could happen with a network mount */
 24180         -        tErrno = errno; 
 24181         -        rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); 
 24182         -        if( IS_LOCK_ERROR(rc) ){
 24183         -          pFile->lastErrno = tErrno;
 24184         -        }
 24185         -        goto end_lock;
 24186         -      }
        25553  +    if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){
        25554  +      /* This could happen with a network mount */
        25555  +      tErrno = errno;
        25556  +      rc = SQLITE_IOERR_UNLOCK; 
 24187  25557       }
 24188         -    if( s==(-1) ){
 24189         -      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
 24190         -      if( IS_LOCK_ERROR(rc) ){
        25558  +
        25559  +    if( rc ){
        25560  +      if( rc!=SQLITE_BUSY ){
 24191  25561           pFile->lastErrno = tErrno;
 24192  25562         }
        25563  +      goto end_lock;
 24193  25564       }else{
 24194  25565         pFile->eFileLock = SHARED_LOCK;
 24195  25566         pInode->nLock++;
 24196  25567         pInode->nShared = 1;
 24197  25568       }
 24198  25569     }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
 24199  25570       /* We are trying for an exclusive lock but another thread in this
................................................................................
 24202  25573     }else{
 24203  25574       /* The request was for a RESERVED or EXCLUSIVE lock.  It is
 24204  25575       ** assumed that there is a SHARED or greater lock on the file
 24205  25576       ** already.
 24206  25577       */
 24207  25578       assert( 0!=pFile->eFileLock );
 24208  25579       lock.l_type = F_WRLCK;
 24209         -    switch( eFileLock ){
 24210         -      case RESERVED_LOCK:
 24211         -        lock.l_start = RESERVED_BYTE;
 24212         -        break;
 24213         -      case EXCLUSIVE_LOCK:
 24214         -        lock.l_start = SHARED_FIRST;
 24215         -        lock.l_len = SHARED_SIZE;
 24216         -        break;
 24217         -      default:
 24218         -        assert(0);
        25580  +
        25581  +    assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
        25582  +    if( eFileLock==RESERVED_LOCK ){
        25583  +      lock.l_start = RESERVED_BYTE;
        25584  +      lock.l_len = 1L;
        25585  +    }else{
        25586  +      lock.l_start = SHARED_FIRST;
        25587  +      lock.l_len = SHARED_SIZE;
 24219  25588       }
 24220         -    s = fcntl(pFile->h, F_SETLK, &lock);
 24221         -    if( s==(-1) ){
        25589  +
        25590  +    if( unixFileLock(pFile, &lock) ){
 24222  25591         tErrno = errno;
 24223  25592         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
 24224         -      if( IS_LOCK_ERROR(rc) ){
        25593  +      if( rc!=SQLITE_BUSY ){
 24225  25594           pFile->lastErrno = tErrno;
 24226  25595         }
 24227  25596       }
 24228  25597     }
 24229  25598     
 24230  25599   
 24231  25600   #ifndef NDEBUG
................................................................................
 24282  25651   ** 
 24283  25652   ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
 24284  25653   ** the byte range is divided into 2 parts and the first part is unlocked then
 24285  25654   ** set to a read lock, then the other part is simply unlocked.  This works 
 24286  25655   ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to 
 24287  25656   ** remove the write lock on a region when a read lock is set.
 24288  25657   */
 24289         -static int _posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
        25658  +static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
 24290  25659     unixFile *pFile = (unixFile*)id;
 24291  25660     unixInodeInfo *pInode;
 24292  25661     struct flock lock;
 24293  25662     int rc = SQLITE_OK;
 24294  25663     int h;
 24295         -  int tErrno;                      /* Error code from system call errors */
 24296  25664   
 24297  25665     assert( pFile );
 24298  25666     OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
 24299  25667         pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
 24300  25668         getpid()));
 24301  25669   
 24302  25670     assert( eFileLock<=SHARED_LOCK );
................................................................................
 24336  25704       ** write lock until the rest is covered by a read lock:
 24337  25705       **  1:   [WWWWW]
 24338  25706       **  2:   [....W]
 24339  25707       **  3:   [RRRRW]
 24340  25708       **  4:   [RRRR.]
 24341  25709       */
 24342  25710       if( eFileLock==SHARED_LOCK ){
        25711  +
        25712  +#if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
        25713  +      (void)handleNFSUnlock;
        25714  +      assert( handleNFSUnlock==0 );
        25715  +#endif
        25716  +#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
 24343  25717         if( handleNFSUnlock ){
        25718  +        int tErrno;               /* Error code from system call errors */
 24344  25719           off_t divSize = SHARED_SIZE - 1;
 24345  25720           
 24346  25721           lock.l_type = F_UNLCK;
 24347  25722           lock.l_whence = SEEK_SET;
 24348  25723           lock.l_start = SHARED_FIRST;
 24349  25724           lock.l_len = divSize;
 24350         -        if( fcntl(h, F_SETLK, &lock)==(-1) ){
        25725  +        if( unixFileLock(pFile, &lock)==(-1) ){
 24351  25726             tErrno = errno;
 24352         -          rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
        25727  +          rc = SQLITE_IOERR_UNLOCK;
 24353  25728             if( IS_LOCK_ERROR(rc) ){
 24354  25729               pFile->lastErrno = tErrno;
 24355  25730             }
 24356  25731             goto end_unlock;
 24357  25732           }
 24358  25733           lock.l_type = F_RDLCK;
 24359  25734           lock.l_whence = SEEK_SET;
 24360  25735           lock.l_start = SHARED_FIRST;
 24361  25736           lock.l_len = divSize;
 24362         -        if( fcntl(h, F_SETLK, &lock)==(-1) ){
        25737  +        if( unixFileLock(pFile, &lock)==(-1) ){
 24363  25738             tErrno = errno;
 24364  25739             rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
 24365  25740             if( IS_LOCK_ERROR(rc) ){
 24366  25741               pFile->lastErrno = tErrno;
 24367  25742             }
 24368  25743             goto end_unlock;
 24369  25744           }
 24370  25745           lock.l_type = F_UNLCK;
 24371  25746           lock.l_whence = SEEK_SET;
 24372  25747           lock.l_start = SHARED_FIRST+divSize;
 24373  25748           lock.l_len = SHARED_SIZE-divSize;
 24374         -        if( fcntl(h, F_SETLK, &lock)==(-1) ){
        25749  +        if( unixFileLock(pFile, &lock)==(-1) ){
 24375  25750             tErrno = errno;
 24376         -          rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
        25751  +          rc = SQLITE_IOERR_UNLOCK;
 24377  25752             if( IS_LOCK_ERROR(rc) ){
 24378  25753               pFile->lastErrno = tErrno;
 24379  25754             }
 24380  25755             goto end_unlock;
 24381  25756           }
 24382         -      }else{
        25757  +      }else
        25758  +#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
        25759  +      {
 24383  25760           lock.l_type = F_RDLCK;
 24384  25761           lock.l_whence = SEEK_SET;
 24385  25762           lock.l_start = SHARED_FIRST;
 24386  25763           lock.l_len = SHARED_SIZE;
 24387         -        if( fcntl(h, F_SETLK, &lock)==(-1) ){
 24388         -          tErrno = errno;
 24389         -          rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
 24390         -          if( IS_LOCK_ERROR(rc) ){
 24391         -            pFile->lastErrno = tErrno;
 24392         -          }
        25764  +        if( unixFileLock(pFile, &lock) ){
        25765  +          /* In theory, the call to unixFileLock() cannot fail because another
        25766  +          ** process is holding an incompatible lock. If it does, this 
        25767  +          ** indicates that the other process is not following the locking
        25768  +          ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
        25769  +          ** SQLITE_BUSY would confuse the upper layer (in practice it causes 
        25770  +          ** an assert to fail). */ 
        25771  +          rc = SQLITE_IOERR_RDLOCK;
        25772  +          pFile->lastErrno = errno;
 24393  25773             goto end_unlock;
 24394  25774           }
 24395  25775         }
 24396  25776       }
 24397  25777       lock.l_type = F_UNLCK;
 24398  25778       lock.l_whence = SEEK_SET;
 24399  25779       lock.l_start = PENDING_BYTE;
 24400  25780       lock.l_len = 2L;  assert( PENDING_BYTE+1==RESERVED_BYTE );
 24401         -    if( fcntl(h, F_SETLK, &lock)!=(-1) ){
        25781  +    if( unixFileLock(pFile, &lock)==0 ){
 24402  25782         pInode->eFileLock = SHARED_LOCK;
 24403  25783       }else{
 24404         -      tErrno = errno;
 24405         -      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
 24406         -      if( IS_LOCK_ERROR(rc) ){
 24407         -        pFile->lastErrno = tErrno;
 24408         -      }
        25784  +      rc = SQLITE_IOERR_UNLOCK;
        25785  +      pFile->lastErrno = errno;
 24409  25786         goto end_unlock;
 24410  25787       }
 24411  25788     }
 24412  25789     if( eFileLock==NO_LOCK ){
 24413  25790       /* Decrement the shared lock counter.  Release the lock using an
 24414  25791       ** OS call only when all threads in this same process have released
 24415  25792       ** the lock.
................................................................................
 24418  25795       if( pInode->nShared==0 ){
 24419  25796         lock.l_type = F_UNLCK;
 24420  25797         lock.l_whence = SEEK_SET;
 24421  25798         lock.l_start = lock.l_len = 0L;
 24422  25799         SimulateIOErrorBenign(1);
 24423  25800         SimulateIOError( h=(-1) )
 24424  25801         SimulateIOErrorBenign(0);
 24425         -      if( fcntl(h, F_SETLK, &lock)!=(-1) ){
        25802  +      if( unixFileLock(pFile, &lock)==0 ){
 24426  25803           pInode->eFileLock = NO_LOCK;
 24427  25804         }else{
 24428         -        tErrno = errno;
 24429         -        rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
 24430         -        if( IS_LOCK_ERROR(rc) ){
 24431         -          pFile->lastErrno = tErrno;
 24432         -        }
        25805  +        rc = SQLITE_IOERR_UNLOCK;
        25806  +	pFile->lastErrno = errno;
 24433  25807           pInode->eFileLock = NO_LOCK;
 24434  25808           pFile->eFileLock = NO_LOCK;
 24435  25809         }
 24436  25810       }
 24437  25811   
 24438  25812       /* Decrement the count of locks against this same file.  When the
 24439  25813       ** count reaches zero, close any other file descriptors whose close
 24440  25814       ** was deferred because of outstanding locks.
 24441  25815       */
 24442  25816       pInode->nLock--;
 24443  25817       assert( pInode->nLock>=0 );
 24444  25818       if( pInode->nLock==0 ){
 24445         -      int rc2 = closePendingFds(pFile);
 24446         -      if( rc==SQLITE_OK ){
 24447         -        rc = rc2;
 24448         -      }
        25819  +      closePendingFds(pFile);
 24449  25820       }
 24450  25821     }
 24451  25822   	
 24452  25823   end_unlock:
 24453  25824     unixLeaveMutex();
 24454  25825     if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
 24455  25826     return rc;
................................................................................
 24459  25830   ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 24460  25831   ** must be either NO_LOCK or SHARED_LOCK.
 24461  25832   **
 24462  25833   ** If the locking level of the file descriptor is already at or below
 24463  25834   ** the requested locking level, this routine is a no-op.
 24464  25835   */
 24465  25836   static int unixUnlock(sqlite3_file *id, int eFileLock){
 24466         -  return _posixUnlock(id, eFileLock, 0);
        25837  +  return posixUnlock(id, eFileLock, 0);
 24467  25838   }
 24468  25839   
 24469  25840   /*
 24470  25841   ** This function performs the parts of the "close file" operation 
 24471  25842   ** common to all locking schemes. It closes the directory and file
 24472  25843   ** handles, if they are valid, and sets all fields of the unixFile
 24473  25844   ** structure to 0.
................................................................................
 24474  25845   **
 24475  25846   ** It is *not* necessary to hold the mutex when this routine is called,
 24476  25847   ** even on VxWorks.  A mutex will be acquired on VxWorks by the
 24477  25848   ** vxworksReleaseFileId() routine.
 24478  25849   */
 24479  25850   static int closeUnixFile(sqlite3_file *id){
 24480  25851     unixFile *pFile = (unixFile*)id;
 24481         -  if( pFile ){
 24482         -    if( pFile->dirfd>=0 ){
 24483         -      int err = close(pFile->dirfd);
 24484         -      if( err ){
 24485         -        pFile->lastErrno = errno;
 24486         -        return SQLITE_IOERR_DIR_CLOSE;
 24487         -      }else{
 24488         -        pFile->dirfd=-1;
 24489         -      }
 24490         -    }
 24491         -    if( pFile->h>=0 ){
 24492         -      int err = close(pFile->h);
 24493         -      if( err ){
 24494         -        pFile->lastErrno = errno;
 24495         -        return SQLITE_IOERR_CLOSE;
 24496         -      }
 24497         -    }
 24498         -#if OS_VXWORKS
 24499         -    if( pFile->pId ){
 24500         -      if( pFile->isDelete ){
 24501         -        unlink(pFile->pId->zCanonicalName);
 24502         -      }
 24503         -      vxworksReleaseFileId(pFile->pId);
 24504         -      pFile->pId = 0;
 24505         -    }
 24506         -#endif
 24507         -    OSTRACE(("CLOSE   %-3d\n", pFile->h));
 24508         -    OpenCounter(-1);
 24509         -    sqlite3_free(pFile->pUnused);
 24510         -    memset(pFile, 0, sizeof(unixFile));
 24511         -  }
        25852  +  if( pFile->dirfd>=0 ){
        25853  +    robust_close(pFile, pFile->dirfd, __LINE__);
        25854  +    pFile->dirfd=-1;
        25855  +  }
        25856  +  if( pFile->h>=0 ){
        25857  +    robust_close(pFile, pFile->h, __LINE__);
        25858  +    pFile->h = -1;
        25859  +  }
        25860  +#if OS_VXWORKS
        25861  +  if( pFile->pId ){
        25862  +    if( pFile->isDelete ){
        25863  +      unlink(pFile->pId->zCanonicalName);
        25864  +    }
        25865  +    vxworksReleaseFileId(pFile->pId);
        25866  +    pFile->pId = 0;
        25867  +  }
        25868  +#endif
        25869  +  OSTRACE(("CLOSE   %-3d\n", pFile->h));
        25870  +  OpenCounter(-1);
        25871  +  sqlite3_free(pFile->pUnused);
        25872  +  memset(pFile, 0, sizeof(unixFile));
 24512  25873     return SQLITE_OK;
 24513  25874   }
 24514  25875   
 24515  25876   /*
 24516  25877   ** Close a file.
 24517  25878   */
 24518  25879   static int unixClose(sqlite3_file *id){
 24519  25880     int rc = SQLITE_OK;
 24520         -  if( id ){
 24521         -    unixFile *pFile = (unixFile *)id;
 24522         -    unixUnlock(id, NO_LOCK);
 24523         -    unixEnterMutex();
 24524         -    if( pFile->pInode && pFile->pInode->nLock ){
 24525         -      /* If there are outstanding locks, do not actually close the file just
 24526         -      ** yet because that would clear those locks.  Instead, add the file
 24527         -      ** descriptor to pInode->pUnused list.  It will be automatically closed 
 24528         -      ** when the last lock is cleared.
 24529         -      */
 24530         -      setPendingFd(pFile);
 24531         -    }
 24532         -    releaseInodeInfo(pFile);
 24533         -    rc = closeUnixFile(id);
 24534         -    unixLeaveMutex();
        25881  +  unixFile *pFile = (unixFile *)id;
        25882  +  unixUnlock(id, NO_LOCK);
        25883  +  unixEnterMutex();
        25884  +
        25885  +  /* unixFile.pInode is always valid here. Otherwise, a different close
        25886  +  ** routine (e.g. nolockClose()) would be called instead.
        25887  +  */
        25888  +  assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
        25889  +  if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
        25890  +    /* If there are outstanding locks, do not actually close the file just
        25891  +    ** yet because that would clear those locks.  Instead, add the file
        25892  +    ** descriptor to pInode->pUnused list.  It will be automatically closed 
        25893  +    ** when the last lock is cleared.
        25894  +    */
        25895  +    setPendingFd(pFile);
 24535  25896     }
        25897  +  releaseInodeInfo(pFile);
        25898  +  rc = closeUnixFile(id);
        25899  +  unixLeaveMutex();
 24536  25900     return rc;
 24537  25901   }
 24538  25902   
 24539  25903   /************** End of the posix advisory lock implementation *****************
 24540  25904   ******************************************************************************/
 24541  25905   
 24542  25906   /******************************************************************************
................................................................................
 24631  25995     if( pFile->eFileLock>SHARED_LOCK ){
 24632  25996       /* Either this connection or some other connection in the same process
 24633  25997       ** holds a lock on the file.  No need to check further. */
 24634  25998       reserved = 1;
 24635  25999     }else{
 24636  26000       /* The lock is held if and only if the lockfile exists */
 24637  26001       const char *zLockFile = (const char*)pFile->lockingContext;
 24638         -    reserved = access(zLockFile, 0)==0;
        26002  +    reserved = osAccess(zLockFile, 0)==0;
 24639  26003     }
 24640  26004     OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
 24641  26005     *pResOut = reserved;
 24642  26006     return rc;
 24643  26007   }
 24644  26008   
 24645  26009   /*
................................................................................
 24685  26049       /* Always update the timestamp on the old file */
 24686  26050       utimes(zLockFile, NULL);
 24687  26051   #endif
 24688  26052       return SQLITE_OK;
 24689  26053     }
 24690  26054     
 24691  26055     /* grab an exclusive lock */
 24692         -  fd = open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600);
        26056  +  fd = robust_open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600);
 24693  26057     if( fd<0 ){
 24694  26058       /* failed to open/create the file, someone else may have stolen the lock */
 24695  26059       int tErrno = errno;
 24696  26060       if( EEXIST == tErrno ){
 24697  26061         rc = SQLITE_BUSY;
 24698  26062       } else {
 24699  26063         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
 24700  26064         if( IS_LOCK_ERROR(rc) ){
 24701  26065           pFile->lastErrno = tErrno;
 24702  26066         }
 24703  26067       }
 24704  26068       return rc;
 24705  26069     } 
 24706         -  if( close(fd) ){
 24707         -    pFile->lastErrno = errno;
 24708         -    rc = SQLITE_IOERR_CLOSE;
 24709         -  }
        26070  +  robust_close(pFile, fd, __LINE__);
 24710  26071     
 24711  26072     /* got it, set the type and return ok */
 24712  26073     pFile->eFileLock = eFileLock;
 24713  26074     return rc;
 24714  26075   }
 24715  26076   
 24716  26077   /*
................................................................................
 24746  26107     
 24747  26108     /* To fully unlock the database, delete the lock file */
 24748  26109     assert( eFileLock==NO_LOCK );
 24749  26110     if( unlink(zLockFile) ){
 24750  26111       int rc = 0;
 24751  26112       int tErrno = errno;
 24752  26113       if( ENOENT != tErrno ){
 24753         -      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
        26114  +      rc = SQLITE_IOERR_UNLOCK;
 24754  26115       }
 24755  26116       if( IS_LOCK_ERROR(rc) ){
 24756  26117         pFile->lastErrno = tErrno;
 24757  26118       }
 24758  26119       return rc; 
 24759  26120     }
 24760  26121     pFile->eFileLock = NO_LOCK;
................................................................................
 24790  26151   ** only a single process can be reading the database at a time.
 24791  26152   **
 24792  26153   ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if
 24793  26154   ** compiling for VXWORKS.
 24794  26155   */
 24795  26156   #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
 24796  26157   
        26158  +/*
        26159  +** Retry flock() calls that fail with EINTR
        26160  +*/
        26161  +#ifdef EINTR
        26162  +static int robust_flock(int fd, int op){
        26163  +  int rc;
        26164  +  do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR );
        26165  +  return rc;
        26166  +}
        26167  +#else
        26168  +# define robust_flock(a,b) flock(a,b)
        26169  +#endif
        26170  +     
        26171  +
 24797  26172   /*
 24798  26173   ** This routine checks if there is a RESERVED lock held on the specified
 24799  26174   ** file by this or any other process. If such a lock is held, set *pResOut
 24800  26175   ** to a non-zero value otherwise *pResOut is set to zero.  The return value
 24801  26176   ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
 24802  26177   */
 24803  26178   static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
................................................................................
 24813  26188     if( pFile->eFileLock>SHARED_LOCK ){
 24814  26189       reserved = 1;
 24815  26190     }
 24816  26191     
 24817  26192     /* Otherwise see if some other process holds it. */
 24818  26193     if( !reserved ){
 24819  26194       /* attempt to get the lock */
 24820         -    int lrc = flock(pFile->h, LOCK_EX | LOCK_NB);
        26195  +    int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
 24821  26196       if( !lrc ){
 24822  26197         /* got the lock, unlock it */
 24823         -      lrc = flock(pFile->h, LOCK_UN);
        26198  +      lrc = robust_flock(pFile->h, LOCK_UN);
 24824  26199         if ( lrc ) {
 24825  26200           int tErrno = errno;
 24826  26201           /* unlock failed with an error */
 24827         -        lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); 
        26202  +        lrc = SQLITE_IOERR_UNLOCK; 
 24828  26203           if( IS_LOCK_ERROR(lrc) ){
 24829  26204             pFile->lastErrno = tErrno;
 24830  26205             rc = lrc;
 24831  26206           }
 24832  26207         }
 24833  26208       } else {
 24834  26209         int tErrno = errno;
................................................................................
 24893  26268     if (pFile->eFileLock > NO_LOCK) {
 24894  26269       pFile->eFileLock = eFileLock;
 24895  26270       return SQLITE_OK;
 24896  26271     }
 24897  26272     
 24898  26273     /* grab an exclusive lock */
 24899  26274     
 24900         -  if (flock(pFile->h, LOCK_EX | LOCK_NB)) {
        26275  +  if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
 24901  26276       int tErrno = errno;
 24902  26277       /* didn't get, must be busy */
 24903  26278       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
 24904  26279       if( IS_LOCK_ERROR(rc) ){
 24905  26280         pFile->lastErrno = tErrno;
 24906  26281       }
 24907  26282     } else {
................................................................................
 24942  26317     /* shared can just be set because we always have an exclusive */
 24943  26318     if (eFileLock==SHARED_LOCK) {
 24944  26319       pFile->eFileLock = eFileLock;
 24945  26320       return SQLITE_OK;
 24946  26321     }
 24947  26322     
 24948  26323     /* no, really, unlock. */
 24949         -  int rc = flock(pFile->h, LOCK_UN);
 24950         -  if (rc) {
 24951         -    int r, tErrno = errno;
 24952         -    r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
 24953         -    if( IS_LOCK_ERROR(r) ){
 24954         -      pFile->lastErrno = tErrno;
 24955         -    }
        26324  +  if( robust_flock(pFile->h, LOCK_UN) ){
 24956  26325   #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
 24957         -    if( (r & SQLITE_IOERR) == SQLITE_IOERR ){
 24958         -      r = SQLITE_BUSY;
 24959         -    }
        26326  +    return SQLITE_OK;
 24960  26327   #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
 24961         -    
 24962         -    return r;
 24963         -  } else {
        26328  +    return SQLITE_IOERR_UNLOCK;
        26329  +  }else{
 24964  26330       pFile->eFileLock = NO_LOCK;
 24965  26331       return SQLITE_OK;
 24966  26332     }
 24967  26333   }
 24968  26334   
 24969  26335   /*
 24970  26336   ** Close a file.
................................................................................
 25580  26946           pFile->eFileLock = NO_LOCK;
 25581  26947         }
 25582  26948       }
 25583  26949       if( rc==SQLITE_OK ){
 25584  26950         pInode->nLock--;
 25585  26951         assert( pInode->nLock>=0 );
 25586  26952         if( pInode->nLock==0 ){
 25587         -        rc = closePendingFds(pFile);
        26953  +        closePendingFds(pFile);
 25588  26954         }
 25589  26955       }
 25590  26956     }
 25591  26957     
 25592  26958     unixLeaveMutex();
 25593  26959     if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
 25594  26960     return rc;
................................................................................
 25637  27003    ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
 25638  27004    ** must be either NO_LOCK or SHARED_LOCK.
 25639  27005    **
 25640  27006    ** If the locking level of the file descriptor is already at or below
 25641  27007    ** the requested locking level, this routine is a no-op.
 25642  27008    */
 25643  27009   static int nfsUnlock(sqlite3_file *id, int eFileLock){
 25644         -  return _posixUnlock(id, eFileLock, 1);
        27010  +  return posixUnlock(id, eFileLock, 1);
 25645  27011   }
 25646  27012   
 25647  27013   #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
 25648  27014   /*
 25649  27015   ** The code above is the NFS lock implementation.  The code is specific
 25650  27016   ** to MacOSX and does not work on other unix platforms.  No alternative
 25651  27017   ** is available.  
................................................................................
 25679  27045   static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
 25680  27046     int got;
 25681  27047   #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
 25682  27048     i64 newOffset;
 25683  27049   #endif
 25684  27050     TIMER_START;
 25685  27051   #if defined(USE_PREAD)
 25686         -  got = pread(id->h, pBuf, cnt, offset);
        27052  +  do{ got = osPread(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
 25687  27053     SimulateIOError( got = -1 );
 25688  27054   #elif defined(USE_PREAD64)
 25689         -  got = pread64(id->h, pBuf, cnt, offset);
        27055  +  do{ got = osPread64(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR);
 25690  27056     SimulateIOError( got = -1 );
 25691  27057   #else
 25692  27058     newOffset = lseek(id->h, offset, SEEK_SET);
 25693  27059     SimulateIOError( newOffset-- );
 25694  27060     if( newOffset!=offset ){
 25695  27061       if( newOffset == -1 ){
 25696  27062         ((unixFile*)id)->lastErrno = errno;
 25697  27063       }else{
 25698  27064         ((unixFile*)id)->lastErrno = 0;			
 25699  27065       }
 25700  27066       return -1;
 25701  27067     }
 25702         -  got = read(id->h, pBuf, cnt);
        27068  +  do{ got = osRead(id->h, pBuf, cnt); }while( got<0 && errno==EINTR );
 25703  27069   #endif
 25704  27070     TIMER_END;
 25705  27071     if( got<0 ){
 25706  27072       ((unixFile*)id)->lastErrno = errno;
 25707  27073     }
 25708  27074     OSTRACE(("READ    %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
 25709  27075     return got;
................................................................................
 25757  27123   static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
 25758  27124     int got;
 25759  27125   #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
 25760  27126     i64 newOffset;
 25761  27127   #endif
 25762  27128     TIMER_START;
 25763  27129   #if defined(USE_PREAD)
 25764         -  got = pwrite(id->h, pBuf, cnt, offset);
        27130  +  do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
 25765  27131   #elif defined(USE_PREAD64)
 25766         -  got = pwrite64(id->h, pBuf, cnt, offset);
        27132  +  do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
 25767  27133   #else
 25768  27134     newOffset = lseek(id->h, offset, SEEK_SET);
        27135  +  SimulateIOError( newOffset-- );
 25769  27136     if( newOffset!=offset ){
 25770  27137       if( newOffset == -1 ){
 25771  27138         ((unixFile*)id)->lastErrno = errno;
 25772  27139       }else{
 25773  27140         ((unixFile*)id)->lastErrno = 0;			
 25774  27141       }
 25775  27142       return -1;
 25776  27143     }
 25777         -  got = write(id->h, pBuf, cnt);
        27144  +  do{ got = osWrite(id->h, pBuf, cnt); }while( got<0 && errno==EINTR );
 25778  27145   #endif
 25779  27146     TIMER_END;
 25780  27147     if( got<0 ){
 25781  27148       ((unixFile*)id)->lastErrno = errno;
 25782  27149     }
 25783  27150   
 25784  27151     OSTRACE(("WRITE   %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
................................................................................
 25938  27305     /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
 25939  27306     ** no-op
 25940  27307     */
 25941  27308   #ifdef SQLITE_NO_SYNC
 25942  27309     rc = SQLITE_OK;
 25943  27310   #elif HAVE_FULLFSYNC
 25944  27311     if( fullSync ){
 25945         -    rc = fcntl(fd, F_FULLFSYNC, 0);
        27312  +    rc = osFcntl(fd, F_FULLFSYNC, 0);
 25946  27313     }else{
 25947  27314       rc = 1;
 25948  27315     }
 25949  27316     /* If the FULLFSYNC failed, fall back to attempting an fsync().
 25950  27317     ** It shouldn't be possible for fullfsync to fail on the local 
 25951  27318     ** file system (on OSX), so failure indicates that FULLFSYNC
 25952  27319     ** isn't supported for this file system. So, attempt an fsync 
................................................................................
 26010  27377   
 26011  27378     assert( pFile );
 26012  27379     OSTRACE(("SYNC    %-3d\n", pFile->h));
 26013  27380     rc = full_fsync(pFile->h, isFullsync, isDataOnly);
 26014  27381     SimulateIOError( rc=1 );
 26015  27382     if( rc ){
 26016  27383       pFile->lastErrno = errno;
 26017         -    return SQLITE_IOERR_FSYNC;
        27384  +    return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
 26018  27385     }
 26019  27386     if( pFile->dirfd>=0 ){
 26020         -    int err;
 26021  27387       OSTRACE(("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
 26022  27388               HAVE_FULLFSYNC, isFullsync));
 26023  27389   #ifndef SQLITE_DISABLE_DIRSYNC
 26024  27390       /* The directory sync is only attempted if full_fsync is
 26025  27391       ** turned off or unavailable.  If a full_fsync occurred above,
 26026  27392       ** then the directory sync is superfluous.
 26027  27393       */
................................................................................
 26032  27398          ** A failed directory sync is not a big deal.  So it seems
 26033  27399          ** better to ignore the error.  Ticket #1657
 26034  27400          */
 26035  27401          /* pFile->lastErrno = errno; */
 26036  27402          /* return SQLITE_IOERR; */
 26037  27403       }
 26038  27404   #endif
 26039         -    err = close(pFile->dirfd); /* Only need to sync once, so close the */
 26040         -    if( err==0 ){              /* directory when we are done */
 26041         -      pFile->dirfd = -1;
 26042         -    }else{
 26043         -      pFile->lastErrno = errno;
 26044         -      rc = SQLITE_IOERR_DIR_CLOSE;
 26045         -    }
        27405  +    /* Only need to sync once, so close the  directory when we are done */
        27406  +    robust_close(pFile, pFile->dirfd, __LINE__);
        27407  +    pFile->dirfd = -1;
 26046  27408     }
 26047  27409     return rc;
 26048  27410   }
 26049  27411   
 26050  27412   /*
 26051  27413   ** Truncate an open file to a specified size
 26052  27414   */
................................................................................
 26061  27423     ** actual file size after the operation may be larger than the requested
 26062  27424     ** size).
 26063  27425     */
 26064  27426     if( pFile->szChunk ){
 26065  27427       nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
 26066  27428     }
 26067  27429   
 26068         -  rc = ftruncate(pFile->h, (off_t)nByte);
        27430  +  rc = robust_ftruncate(pFile->h, (off_t)nByte);
 26069  27431     if( rc ){
 26070  27432       pFile->lastErrno = errno;
 26071         -    return SQLITE_IOERR_TRUNCATE;
        27433  +    return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
 26072  27434     }else{
 26073  27435   #ifndef NDEBUG
 26074  27436       /* If we are doing a normal write to a database file (as opposed to
 26075  27437       ** doing a hot-journal rollback or a write to some file other than a
 26076  27438       ** normal database file) and we truncate the file to zero length,
 26077  27439       ** that effectively updates the change counter.  This might happen
 26078  27440       ** when restoring a database using the backup API from a zero-length
................................................................................
 26090  27452   /*
 26091  27453   ** Determine the current size of a file in bytes
 26092  27454   */
 26093  27455   static int unixFileSize(sqlite3_file *id, i64 *pSize){
 26094  27456     int rc;
 26095  27457     struct stat buf;
 26096  27458     assert( id );
 26097         -  rc = fstat(((unixFile*)id)->h, &buf);
        27459  +  rc = osFstat(((unixFile*)id)->h, &buf);
 26098  27460     SimulateIOError( rc=1 );
 26099  27461     if( rc!=0 ){
 26100  27462       ((unixFile*)id)->lastErrno = errno;
 26101  27463       return SQLITE_IOERR_FSTAT;
 26102  27464     }
 26103  27465     *pSize = buf.st_size;
 26104  27466   
................................................................................
 26131  27493   ** SQLITE_FCNTL_SIZE_HINT operation is a no-op for Unix.
 26132  27494   */
 26133  27495   static int fcntlSizeHint(unixFile *pFile, i64 nByte){
 26134  27496     if( pFile->szChunk ){
 26135  27497       i64 nSize;                    /* Required file size */
 26136  27498       struct stat buf;              /* Used to hold return values of fstat() */
 26137  27499      
 26138         -    if( fstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
        27500  +    if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
 26139  27501   
 26140  27502       nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
 26141  27503       if( nSize>(i64)buf.st_size ){
        27504  +
 26142  27505   #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
 26143         -      if( posix_fallocate(pFile->h, buf.st_size, nSize-buf.st_size) ){
 26144         -        return SQLITE_IOERR_WRITE;
 26145         -      }
        27506  +      /* The code below is handling the return value of osFallocate() 
        27507  +      ** correctly. posix_fallocate() is defined to "returns zero on success, 
        27508  +      ** or an error number on  failure". See the manpage for details. */
        27509  +      int err;
        27510  +      do{
        27511  +        err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
        27512  +      }while( err==EINTR );
        27513  +      if( err ) return SQLITE_IOERR_WRITE;
 26146  27514   #else
 26147  27515         /* If the OS does not have posix_fallocate(), fake it. First use
 26148  27516         ** ftruncate() to set the file size, then write a single byte to
 26149  27517         ** the last byte in each block within the extended region. This
 26150  27518         ** is the same technique used by glibc to implement posix_fallocate()
 26151  27519         ** on systems that do not have a real fallocate() system call.
 26152  27520         */
 26153  27521         int nBlk = buf.st_blksize;  /* File-system block size */
 26154  27522         i64 iWrite;                 /* Next offset to write to */
 26155         -      int nWrite;                 /* Return value from seekAndWrite() */
 26156  27523   
 26157         -      if( ftruncate(pFile->h, nSize) ){
        27524  +      if( robust_ftruncate(pFile->h, nSize) ){
 26158  27525           pFile->lastErrno = errno;
 26159         -        return SQLITE_IOERR_TRUNCATE;
        27526  +        return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
 26160  27527         }
 26161  27528         iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1;
 26162         -      do {
 26163         -        nWrite = seekAndWrite(pFile, iWrite, "", 1);
        27529  +      while( iWrite<nSize ){
        27530  +        int nWrite = seekAndWrite(pFile, iWrite, "", 1);
        27531  +        if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
 26164  27532           iWrite += nBlk;
 26165         -      } while( nWrite==1 && iWrite<nSize );
 26166         -      if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
        27533  +      }
 26167  27534   #endif
 26168  27535       }
 26169  27536     }
 26170  27537   
 26171  27538     return SQLITE_OK;
 26172  27539   }
 26173  27540   
................................................................................
 26337  27704   
 26338  27705     /* Shared locks never span more than one byte */
 26339  27706     assert( n==1 || lockType!=F_RDLCK );
 26340  27707   
 26341  27708     /* Locks are within range */
 26342  27709     assert( n>=1 && n<SQLITE_SHM_NLOCK );
 26343  27710   
 26344         -  /* Initialize the locking parameters */
 26345         -  memset(&f, 0, sizeof(f));
 26346         -  f.l_type = lockType;
 26347         -  f.l_whence = SEEK_SET;
 26348         -  f.l_start = ofst;
 26349         -  f.l_len = n;
        27711  +  if( pShmNode->h>=0 ){
        27712  +    /* Initialize the locking parameters */
        27713  +    memset(&f, 0, sizeof(f));
        27714  +    f.l_type = lockType;
        27715  +    f.l_whence = SEEK_SET;
        27716  +    f.l_start = ofst;
        27717  +    f.l_len = n;
 26350  27718   
 26351         -  rc = fcntl(pShmNode->h, F_SETLK, &f);
 26352         -  rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
        27719  +    rc = osFcntl(pShmNode->h, F_SETLK, &f);
        27720  +    rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
        27721  +  }
 26353  27722   
 26354  27723     /* Update the global lock state and do debug tracing */
 26355  27724   #ifdef SQLITE_DEBUG
 26356  27725     { u16 mask;
 26357  27726     OSTRACE(("SHM-LOCK "));
 26358  27727     mask = (1<<(ofst+n)) - (1<<ofst);
 26359  27728     if( rc==SQLITE_OK ){
................................................................................
 26400  27769     unixShmNode *p = pFd->pInode->pShmNode;
 26401  27770     assert( unixMutexHeld() );
 26402  27771     if( p && p->nRef==0 ){
 26403  27772       int i;
 26404  27773       assert( p->pInode==pFd->pInode );
 26405  27774       if( p->mutex ) sqlite3_mutex_free(p->mutex);
 26406  27775       for(i=0; i<p->nRegion; i++){
 26407         -      munmap(p->apRegion[i], p->szRegion);
        27776  +      if( p->h>=0 ){
        27777  +        munmap(p->apRegion[i], p->szRegion);
        27778  +      }else{
        27779  +        sqlite3_free(p->apRegion[i]);
        27780  +      }
 26408  27781       }
 26409  27782       sqlite3_free(p->apRegion);
 26410         -    if( p->h>=0 ) close(p->h);
        27783  +    if( p->h>=0 ){
        27784  +      robust_close(pFd, p->h, __LINE__);
        27785  +      p->h = -1;
        27786  +    }
 26411  27787       p->pInode->pShmNode = 0;
 26412  27788       sqlite3_free(p);
 26413  27789     }
 26414  27790   }
 26415  27791   
 26416  27792   /*
 26417  27793   ** Open a shared-memory area associated with open database file pDbFd.  
................................................................................
 26437  27813   ** same database file at the same time, database corruption will likely
 26438  27814   ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
 26439  27815   ** "unsupported" and may go away in a future SQLite release.
 26440  27816   **
 26441  27817   ** When opening a new shared-memory file, if no other instances of that
 26442  27818   ** file are currently open, in this process or in other processes, then
 26443  27819   ** the file must be truncated to zero length or have its header cleared.
        27820  +**
        27821  +** If the original database file (pDbFd) is using the "unix-excl" VFS
        27822  +** that means that an exclusive lock is held on the database file and
        27823  +** that no other processes are able to read or write the database.  In
        27824  +** that case, we do not really need shared memory.  No shared memory
        27825  +** file is created.  The shared memory will be simulated with heap memory.
 26444  27826   */
 26445  27827   static int unixOpenSharedMemory(unixFile *pDbFd){
 26446  27828     struct unixShm *p = 0;          /* The connection to be opened */
 26447  27829     struct unixShmNode *pShmNode;   /* The underlying mmapped file */
 26448  27830     int rc;                         /* Result code */
 26449  27831     unixInodeInfo *pInode;          /* The inode of fd */
 26450  27832     char *zShmFilename;             /* Name of the file used for SHM */
................................................................................
 26466  27848       struct stat sStat;                 /* fstat() info for database file */
 26467  27849   
 26468  27850       /* Call fstat() to figure out the permissions on the database file. If
 26469  27851       ** a new *-shm file is created, an attempt will be made to create it
 26470  27852       ** with the same permissions. The actual permissions the file is created
 26471  27853       ** with are subject to the current umask setting.
 26472  27854       */
 26473         -    if( fstat(pDbFd->h, &sStat) ){
        27855  +    if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){
 26474  27856         rc = SQLITE_IOERR_FSTAT;
 26475  27857         goto shm_open_err;
 26476  27858       }
 26477  27859   
 26478  27860   #ifdef SQLITE_SHM_DIRECTORY
 26479  27861       nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 30;
 26480  27862   #else
................................................................................
 26499  27881       pShmNode->pInode = pDbFd->pInode;
 26500  27882       pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
 26501  27883       if( pShmNode->mutex==0 ){
 26502  27884         rc = SQLITE_NOMEM;
 26503  27885         goto shm_open_err;
 26504  27886       }
 26505  27887   
 26506         -    pShmNode->h = open(zShmFilename, O_RDWR|O_CREAT, (sStat.st_mode & 0777));
 26507         -    if( pShmNode->h<0 ){
 26508         -      rc = SQLITE_CANTOPEN_BKPT;
 26509         -      goto shm_open_err;
 26510         -    }
 26511         -
 26512         -    /* Check to see if another process is holding the dead-man switch.
 26513         -    ** If not, truncate the file to zero length. 
 26514         -    */
 26515         -    rc = SQLITE_OK;
 26516         -    if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
 26517         -      if( ftruncate(pShmNode->h, 0) ){
 26518         -        rc = SQLITE_IOERR_SHMOPEN;
 26519         -      }
 26520         -    }
 26521         -    if( rc==SQLITE_OK ){
 26522         -      rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1);
 26523         -    }
 26524         -    if( rc ) goto shm_open_err;
        27888  +    if( pInode->bProcessLock==0 ){
        27889  +      pShmNode->h = robust_open(zShmFilename, O_RDWR|O_CREAT,
        27890  +                               (sStat.st_mode & 0777));
        27891  +      if( pShmNode->h<0 ){
        27892  +        rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
        27893  +        goto shm_open_err;
        27894  +      }
        27895  +  
        27896  +      /* Check to see if another process is holding the dead-man switch.
        27897  +      ** If not, truncate the file to zero length. 
        27898  +      */
        27899  +      rc = SQLITE_OK;
        27900  +      if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
        27901  +        if( robust_ftruncate(pShmNode->h, 0) ){
        27902  +          rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
        27903  +        }
        27904  +      }
        27905  +      if( rc==SQLITE_OK ){
        27906  +        rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1);
        27907  +      }
        27908  +      if( rc ) goto shm_open_err;
        27909  +    }
 26525  27910     }
 26526  27911   
 26527  27912     /* Make the new connection a child of the unixShmNode */
 26528  27913     p->pShmNode = pShmNode;
 26529  27914   #ifdef SQLITE_DEBUG
 26530  27915     p->id = pShmNode->nextShmId++;
 26531  27916   #endif
................................................................................
 26591  27976       if( rc!=SQLITE_OK ) return rc;
 26592  27977     }
 26593  27978   
 26594  27979     p = pDbFd->pShm;
 26595  27980     pShmNode = p->pShmNode;
 26596  27981     sqlite3_mutex_enter(pShmNode->mutex);
 26597  27982     assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
        27983  +  assert( pShmNode->pInode==pDbFd->pInode );
        27984  +  assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
        27985  +  assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
 26598  27986   
 26599  27987     if( pShmNode->nRegion<=iRegion ){
 26600  27988       char **apNew;                      /* New apRegion[] array */
 26601  27989       int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
 26602  27990       struct stat sStat;                 /* Used by fstat() */
 26603  27991   
 26604  27992       pShmNode->szRegion = szRegion;
 26605  27993   
 26606         -    /* The requested region is not mapped into this processes address space.
 26607         -    ** Check to see if it has been allocated (i.e. if the wal-index file is
 26608         -    ** large enough to contain the requested region).
 26609         -    */
 26610         -    if( fstat(pShmNode->h, &sStat) ){
 26611         -      rc = SQLITE_IOERR_SHMSIZE;
 26612         -      goto shmpage_out;
 26613         -    }
 26614         -
 26615         -    if( sStat.st_size<nByte ){
 26616         -      /* The requested memory region does not exist. If bExtend is set to
 26617         -      ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
 26618         -      **
 26619         -      ** Alternatively, if bExtend is true, use ftruncate() to allocate
 26620         -      ** the requested memory region.
        27994  +    if( pShmNode->h>=0 ){
        27995  +      /* The requested region is not mapped into this processes address space.
        27996  +      ** Check to see if it has been allocated (i.e. if the wal-index file is
        27997  +      ** large enough to contain the requested region).
 26621  27998         */
 26622         -      if( !bExtend ) goto shmpage_out;
 26623         -      if( ftruncate(pShmNode->h, nByte) ){
        27999  +      if( osFstat(pShmNode->h, &sStat) ){
 26624  28000           rc = SQLITE_IOERR_SHMSIZE;
 26625  28001           goto shmpage_out;
 26626  28002         }
        28003  +  
        28004  +      if( sStat.st_size<nByte ){
        28005  +        /* The requested memory region does not exist. If bExtend is set to
        28006  +        ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
        28007  +        **
        28008  +        ** Alternatively, if bExtend is true, use ftruncate() to allocate
        28009  +        ** the requested memory region.
        28010  +        */
        28011  +        if( !bExtend ) goto shmpage_out;
        28012  +        if( robust_ftruncate(pShmNode->h, nByte) ){
        28013  +          rc = unixLogError(SQLITE_IOERR_SHMSIZE, "ftruncate",
        28014  +                            pShmNode->zFilename);
        28015  +          goto shmpage_out;
        28016  +        }
        28017  +      }
 26627  28018       }
 26628  28019   
 26629  28020       /* Map the requested memory region into this processes address space. */
 26630  28021       apNew = (char **)sqlite3_realloc(
 26631  28022           pShmNode->apRegion, (iRegion+1)*sizeof(char *)
 26632  28023       );
 26633  28024       if( !apNew ){
 26634  28025         rc = SQLITE_IOERR_NOMEM;
 26635  28026         goto shmpage_out;
 26636  28027       }
 26637  28028       pShmNode->apRegion = apNew;
 26638  28029       while(pShmNode->nRegion<=iRegion){
 26639         -      void *pMem = mmap(0, szRegion, PROT_READ|PROT_WRITE, 
 26640         -          MAP_SHARED, pShmNode->h, pShmNode->nRegion*szRegion
 26641         -      );
 26642         -      if( pMem==MAP_FAILED ){
 26643         -        rc = SQLITE_IOERR;
 26644         -        goto shmpage_out;
        28030  +      void *pMem;
        28031  +      if( pShmNode->h>=0 ){
        28032  +        pMem = mmap(0, szRegion, PROT_READ|PROT_WRITE, 
        28033  +            MAP_SHARED, pShmNode->h, pShmNode->nRegion*szRegion
        28034  +        );
        28035  +        if( pMem==MAP_FAILED ){
        28036  +          rc = SQLITE_IOERR;
        28037  +          goto shmpage_out;
        28038  +        }
        28039  +      }else{
        28040  +        pMem = sqlite3_malloc(szRegion);
        28041  +        if( pMem==0 ){
        28042  +          rc = SQLITE_NOMEM;
        28043  +          goto shmpage_out;
        28044  +        }
        28045  +        memset(pMem, 0, szRegion);
 26645  28046         }
 26646  28047         pShmNode->apRegion[pShmNode->nRegion] = pMem;
 26647  28048         pShmNode->nRegion++;
 26648  28049       }
 26649  28050     }
 26650  28051   
 26651  28052   shmpage_out:
................................................................................
 26684  28085     assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
 26685  28086     assert( n>=1 );
 26686  28087     assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
 26687  28088          || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
 26688  28089          || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
 26689  28090          || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
 26690  28091     assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
        28092  +  assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
        28093  +  assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
 26691  28094   
 26692  28095     mask = (1<<(ofst+n)) - (1<<ofst);
 26693  28096     assert( n>1 || mask==(1<<ofst) );
 26694  28097     sqlite3_mutex_enter(pShmNode->mutex);
 26695  28098     if( flags & SQLITE_SHM_UNLOCK ){
 26696  28099       u16 allMask = 0; /* Mask of locks held by siblings */
 26697  28100   
................................................................................
 26821  28224   
 26822  28225     /* If pShmNode->nRef has reached 0, then close the underlying
 26823  28226     ** shared-memory file, too */
 26824  28227     unixEnterMutex();
 26825  28228     assert( pShmNode->nRef>0 );
 26826  28229     pShmNode->nRef--;
 26827  28230     if( pShmNode->nRef==0 ){
 26828         -    if( deleteFlag ) unlink(pShmNode->zFilename);
        28231  +    if( deleteFlag && pShmNode->h>=0 ) unlink(pShmNode->zFilename);
 26829  28232       unixShmPurge(pDbFd);
 26830  28233     }
 26831  28234     unixLeaveMutex();
 26832  28235   
 26833  28236     return SQLITE_OK;
 26834  28237   }
 26835  28238   
................................................................................
 27062  28465     ** Test byte-range lock using fcntl(). If the call succeeds, 
 27063  28466     ** assume that the file-system supports POSIX style locks. 
 27064  28467     */
 27065  28468     lockInfo.l_len = 1;
 27066  28469     lockInfo.l_start = 0;
 27067  28470     lockInfo.l_whence = SEEK_SET;
 27068  28471     lockInfo.l_type = F_RDLCK;
 27069         -  if( fcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
        28472  +  if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
 27070  28473       if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
 27071  28474         return &nfsIoMethods;
 27072  28475       } else {
 27073  28476         return &posixIoMethods;
 27074  28477       }
 27075  28478     }else{
 27076  28479       return &dotlockIoMethods;
................................................................................
 27104  28507     /* Test if fcntl() is supported and use POSIX style locks.
 27105  28508     ** Otherwise fall back to the named semaphore method.
 27106  28509     */
 27107  28510     lockInfo.l_len = 1;
 27108  28511     lockInfo.l_start = 0;
 27109  28512     lockInfo.l_whence = SEEK_SET;
 27110  28513     lockInfo.l_type = F_RDLCK;
 27111         -  if( fcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
        28514  +  if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
 27112  28515       return &posixIoMethods;
 27113  28516     }else{
 27114  28517       return &semIoMethods;
 27115  28518     }
 27116  28519   }
 27117  28520   static const sqlite3_io_methods 
 27118  28521     *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
................................................................................
 27138  28541   static int fillInUnixFile(
 27139  28542     sqlite3_vfs *pVfs,      /* Pointer to vfs object */
 27140  28543     int h,                  /* Open file descriptor of file being opened */
 27141  28544     int dirfd,              /* Directory file descriptor */
 27142  28545     sqlite3_file *pId,      /* Write to the unixFile structure here */
 27143  28546     const char *zFilename,  /* Name of the file being opened */
 27144  28547     int noLock,             /* Omit locking if true */
 27145         -  int isDelete            /* Delete on close if true */
        28548  +  int isDelete,           /* Delete on close if true */
        28549  +  int isReadOnly          /* True if the file is opened read-only */
 27146  28550   ){
 27147  28551     const sqlite3_io_methods *pLockingStyle;
 27148  28552     unixFile *pNew = (unixFile *)pId;
 27149  28553     int rc = SQLITE_OK;
 27150  28554   
 27151  28555     assert( pNew->pInode==NULL );
 27152  28556   
................................................................................
 27165  28569   #else
 27166  28570     assert( zFilename==0 || zFilename[0]=='/' );
 27167  28571   #endif
 27168  28572   
 27169  28573     OSTRACE(("OPEN    %-3d %s\n", h, zFilename));
 27170  28574     pNew->h = h;
 27171  28575     pNew->dirfd = dirfd;
 27172         -  pNew->fileFlags = 0;
 27173  28576     pNew->zPath = zFilename;
        28577  +  if( memcmp(pVfs->zName,"unix-excl",10)==0 ){
        28578  +    pNew->ctrlFlags = UNIXFILE_EXCL;
        28579  +  }else{
        28580  +    pNew->ctrlFlags = 0;
        28581  +  }
        28582  +  if( isReadOnly ){
        28583  +    pNew->ctrlFlags |= UNIXFILE_RDONLY;
        28584  +  }
 27174  28585   
 27175  28586   #if OS_VXWORKS
 27176  28587     pNew->pId = vxworksFindFileId(zFilename);
 27177  28588     if( pNew->pId==0 ){
 27178  28589       noLock = 1;
 27179  28590       rc = SQLITE_NOMEM;
 27180  28591     }
................................................................................
 27214  28625         ** handle h - as it is guaranteed that no posix locks will be released
 27215  28626         ** by doing so.
 27216  28627         **
 27217  28628         ** If scenario (a) caused the error then things are not so safe. The
 27218  28629         ** implicit assumption here is that if fstat() fails, things are in
 27219  28630         ** such bad shape that dropping a lock or two doesn't matter much.
 27220  28631         */
 27221         -      close(h);
        28632  +      robust_close(pNew, h, __LINE__);
 27222  28633         h = -1;
 27223  28634       }
 27224  28635       unixLeaveMutex();
 27225  28636     }
 27226  28637   
 27227  28638   #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
 27228  28639     else if( pLockingStyle == &afpIoMethods ){
................................................................................
 27240  28651         pCtx->dbPath = zFilename;
 27241  28652         pCtx->reserved = 0;
 27242  28653         srandomdev();
 27243  28654         unixEnterMutex();
 27244  28655         rc = findInodeInfo(pNew, &pNew->pInode);
 27245  28656         if( rc!=SQLITE_OK ){
 27246  28657           sqlite3_free(pNew->lockingContext);
 27247         -        close(h);
        28658  +        robust_close(pNew, h, __LINE__);
 27248  28659           h = -1;
 27249  28660         }
 27250  28661         unixLeaveMutex();        
 27251  28662       }
 27252  28663     }
 27253  28664   #endif
 27254  28665   
................................................................................
 27291  28702       unixLeaveMutex();
 27292  28703     }
 27293  28704   #endif
 27294  28705     
 27295  28706     pNew->lastErrno = 0;
 27296  28707   #if OS_VXWORKS
 27297  28708     if( rc!=SQLITE_OK ){
 27298         -    if( h>=0 ) close(h);
        28709  +    if( h>=0 ) robust_close(pNew, h, __LINE__);
 27299  28710       h = -1;
 27300  28711       unlink(zFilename);
 27301  28712       isDelete = 0;
 27302  28713     }
 27303  28714     pNew->isDelete = isDelete;
 27304  28715   #endif
 27305  28716     if( rc!=SQLITE_OK ){
 27306         -    if( dirfd>=0 ) close(dirfd); /* silent leak if fail, already in error */
 27307         -    if( h>=0 ) close(h);
        28717  +    if( dirfd>=0 ) robust_close(pNew, dirfd, __LINE__);
        28718  +    if( h>=0 ) robust_close(pNew, h, __LINE__);
 27308  28719     }else{
 27309  28720       pNew->pMethod = pLockingStyle;
 27310  28721       OpenCounter(+1);
 27311  28722     }
 27312  28723     return rc;
 27313  28724   }
 27314  28725   
................................................................................
 27327  28738     int fd = -1;
 27328  28739     char zDirname[MAX_PATHNAME+1];
 27329  28740   
 27330  28741     sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
 27331  28742     for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
 27332  28743     if( ii>0 ){
 27333  28744       zDirname[ii] = '\0';
 27334         -    fd = open(zDirname, O_RDONLY|O_BINARY, 0);
        28745  +    fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
 27335  28746       if( fd>=0 ){
 27336  28747   #ifdef FD_CLOEXEC
 27337         -      fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
        28748  +      osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
 27338  28749   #endif
 27339  28750         OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
 27340  28751       }
 27341  28752     }
 27342  28753     *pFd = fd;
 27343         -  return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN_BKPT);
        28754  +  return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
 27344  28755   }
 27345  28756   
 27346  28757   /*
 27347  28758   ** Return the name of a directory in which to put temporary files.
 27348  28759   ** If no suitable temporary file directory can be found, return NULL.
 27349  28760   */
 27350  28761   static const char *unixTempFileDir(void){
................................................................................
 27360  28771     struct stat buf;
 27361  28772     const char *zDir = 0;
 27362  28773   
 27363  28774     azDirs[0] = sqlite3_temp_directory;
 27364  28775     if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
 27365  28776     for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
 27366  28777       if( zDir==0 ) continue;
 27367         -    if( stat(zDir, &buf) ) continue;
        28778  +    if( osStat(zDir, &buf) ) continue;
 27368  28779       if( !S_ISDIR(buf.st_mode) ) continue;
 27369         -    if( access(zDir, 07) ) continue;
        28780  +    if( osAccess(zDir, 07) ) continue;
 27370  28781       break;
 27371  28782     }
 27372  28783     return zDir;
 27373  28784   }
 27374  28785   
 27375  28786   /*
 27376  28787   ** Create a temporary file name in zBuf.  zBuf must be allocated
................................................................................
 27405  28816       sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
 27406  28817       j = (int)strlen(zBuf);
 27407  28818       sqlite3_randomness(15, &zBuf[j]);
 27408  28819       for(i=0; i<15; i++, j++){
 27409  28820         zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
 27410  28821       }
 27411  28822       zBuf[j] = 0;
 27412         -  }while( access(zBuf,0)==0 );
        28823  +  }while( osAccess(zBuf,0)==0 );
 27413  28824     return SQLITE_OK;
 27414  28825   }
 27415  28826   
 27416  28827   #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
 27417  28828   /*
 27418  28829   ** Routine to transform a unixFile into a proxy-locking unixFile.
 27419  28830   ** Implementation in the proxy-lock division, but used by unixOpen()
................................................................................
 27666  29077       mode_t openMode;              /* Permissions to create file with */
 27667  29078       rc = findCreateFileMode(zName, flags, &openMode);
 27668  29079       if( rc!=SQLITE_OK ){
 27669  29080         assert( !p->pUnused );
 27670  29081         assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
 27671  29082         return rc;
 27672  29083       }
 27673         -    fd = open(zName, openFlags, openMode);
        29084  +    fd = robust_open(zName, openFlags, openMode);
 27674  29085       OSTRACE(("OPENX   %-3d %s 0%o\n", fd, zName, openFlags));
 27675  29086       if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
 27676  29087         /* Failed to open the file for read/write access. Try read-only. */
 27677  29088         flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
 27678  29089         openFlags &= ~(O_RDWR|O_CREAT);
 27679  29090         flags |= SQLITE_OPEN_READONLY;
 27680  29091         openFlags |= O_RDONLY;
 27681         -      fd = open(zName, openFlags, openMode);
        29092  +      isReadonly = 1;
        29093  +      fd = robust_open(zName, openFlags, openMode);
 27682  29094       }
 27683  29095       if( fd<0 ){
 27684         -      rc = SQLITE_CANTOPEN_BKPT;
        29096  +      rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
 27685  29097         goto open_finished;
 27686  29098       }
 27687  29099     }
 27688  29100     assert( fd>=0 );
 27689  29101     if( pOutFlags ){
 27690  29102       *pOutFlags = flags;
 27691  29103     }
................................................................................
 27712  29124       rc = openDirectory(zPath, &dirfd);
 27713  29125       if( rc!=SQLITE_OK ){
 27714  29126         /* It is safe to close fd at this point, because it is guaranteed not
 27715  29127         ** to be open on a database file. If it were open on a database file,
 27716  29128         ** it would not be safe to close as this would release any locks held
 27717  29129         ** on the file by this process.  */
 27718  29130         assert( eType!=SQLITE_OPEN_MAIN_DB );
 27719         -      close(fd);             /* silently leak if fail, already in error */
        29131  +      robust_close(p, fd, __LINE__);
 27720  29132         goto open_finished;
 27721  29133       }
 27722  29134     }
 27723  29135   
 27724  29136   #ifdef FD_CLOEXEC
 27725         -  fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
        29137  +  osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
 27726  29138   #endif
 27727  29139   
 27728  29140     noLock = eType!=SQLITE_OPEN_MAIN_DB;
 27729  29141   
 27730  29142     
 27731  29143   #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
 27732  29144     struct statfs fsInfo;
 27733  29145     if( fstatfs(fd, &fsInfo) == -1 ){
 27734  29146       ((unixFile*)pFile)->lastErrno = errno;
 27735         -    if( dirfd>=0 ) close(dirfd); /* silently leak if fail, in error */
 27736         -    close(fd); /* silently leak if fail, in error */
        29147  +    if( dirfd>=0 ) robust_close(p, dirfd, __LINE__);
        29148  +    robust_close(p, fd, __LINE__);
 27737  29149       return SQLITE_IOERR_ACCESS;
 27738  29150     }
 27739  29151     if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
 27740  29152       ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
 27741  29153     }
 27742  29154   #endif
 27743  29155     
................................................................................
 27761  29173           ** on that file that are currently holding advisory locks on it,
 27762  29174           ** then the call to close() will cancel those locks. In practice,
 27763  29175           ** we're assuming that statfs() doesn't fail very often. At least
 27764  29176           ** not while other file descriptors opened by the same process on
 27765  29177           ** the same file are working.  */
 27766  29178           p->lastErrno = errno;
 27767  29179           if( dirfd>=0 ){
 27768         -          close(dirfd); /* silently leak if fail, in error */
        29180  +          robust_close(p, dirfd, __LINE__);
 27769  29181           }
 27770         -        close(fd); /* silently leak if fail, in error */
        29182  +        robust_close(p, fd, __LINE__);
 27771  29183           rc = SQLITE_IOERR_ACCESS;
 27772  29184           goto open_finished;
 27773  29185         }
 27774  29186         useProxy = !(fsInfo.f_flags&MNT_LOCAL);
 27775  29187       }
 27776  29188       if( useProxy ){
 27777         -      rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
        29189  +      rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock,
        29190  +                          isDelete, isReadonly);
 27778  29191         if( rc==SQLITE_OK ){
 27779  29192           rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
 27780  29193           if( rc!=SQLITE_OK ){
 27781  29194             /* Use unixClose to clean up the resources added in fillInUnixFile 
 27782  29195             ** and clear all the structure's references.  Specifically, 
 27783  29196             ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op 
 27784  29197             */
................................................................................
 27787  29200           }
 27788  29201         }
 27789  29202         goto open_finished;
 27790  29203       }
 27791  29204     }
 27792  29205   #endif
 27793  29206     
 27794         -  rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete);
        29207  +  rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock,
        29208  +                      isDelete, isReadonly);
 27795  29209   open_finished:
 27796  29210     if( rc!=SQLITE_OK ){
 27797  29211       sqlite3_free(p->pUnused);
 27798  29212     }
 27799  29213     return rc;
 27800  29214   }
 27801  29215   
................................................................................
 27809  29223     const char *zPath,        /* Name of file to be deleted */
 27810  29224     int dirSync               /* If true, fsync() directory after deleting file */
 27811  29225   ){
 27812  29226     int rc = SQLITE_OK;
 27813  29227     UNUSED_PARAMETER(NotUsed);
 27814  29228     SimulateIOError(return SQLITE_IOERR_DELETE);
 27815  29229     if( unlink(zPath)==(-1) && errno!=ENOENT ){
 27816         -    return SQLITE_IOERR_DELETE;
        29230  +    return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
 27817  29231     }
 27818  29232   #ifndef SQLITE_DISABLE_DIRSYNC
 27819  29233     if( dirSync ){
 27820  29234       int fd;
 27821  29235       rc = openDirectory(zPath, &fd);
 27822  29236       if( rc==SQLITE_OK ){
 27823  29237   #if OS_VXWORKS
 27824  29238         if( fsync(fd)==-1 )
 27825  29239   #else
 27826  29240         if( fsync(fd) )
 27827  29241   #endif
 27828  29242         {
 27829         -        rc = SQLITE_IOERR_DIR_FSYNC;
        29243  +        rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
 27830  29244         }
 27831         -      if( close(fd)&&!rc ){
 27832         -        rc = SQLITE_IOERR_DIR_CLOSE;
 27833         -      }
        29245  +      robust_close(0, fd, __LINE__);
 27834  29246       }
 27835  29247     }
 27836  29248   #endif
 27837  29249     return rc;
 27838  29250   }
 27839  29251   
 27840  29252   /*
................................................................................
 27866  29278       case SQLITE_ACCESS_READ:
 27867  29279         amode = R_OK;
 27868  29280         break;
 27869  29281   
 27870  29282       default:
 27871  29283         assert(!"Invalid flags argument");
 27872  29284     }
 27873         -  *pResOut = (access(zPath, amode)==0);
        29285  +  *pResOut = (osAccess(zPath, amode)==0);
 27874  29286     if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){
 27875  29287       struct stat buf;
 27876  29288       if( 0==stat(zPath, &buf) && buf.st_size==0 ){
 27877  29289         *pResOut = 0;
 27878  29290       }
 27879  29291     }
 27880  29292     return SQLITE_OK;
................................................................................
 27908  29320     UNUSED_PARAMETER(pVfs);
 27909  29321   
 27910  29322     zOut[nOut-1] = '\0';
 27911  29323     if( zPath[0]=='/' ){
 27912  29324       sqlite3_snprintf(nOut, zOut, "%s", zPath);
 27913  29325     }else{
 27914  29326       int nCwd;
 27915         -    if( getcwd(zOut, nOut-1)==0 ){
 27916         -      return SQLITE_CANTOPEN_BKPT;
        29327  +    if( osGetcwd(zOut, nOut-1)==0 ){
        29328  +      return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
 27917  29329       }
 27918  29330       nCwd = (int)strlen(zOut);
 27919  29331       sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
 27920  29332     }
 27921  29333     return SQLITE_OK;
 27922  29334   }
 27923  29335   
................................................................................
 28003  29415     ** that we always use the same random number sequence.  This makes the
 28004  29416     ** tests repeatable.
 28005  29417     */
 28006  29418     memset(zBuf, 0, nBuf);
 28007  29419   #if !defined(SQLITE_TEST)
 28008  29420     {
 28009  29421       int pid, fd;
 28010         -    fd = open("/dev/urandom", O_RDONLY);
        29422  +    fd = robust_open("/dev/urandom", O_RDONLY, 0);
 28011  29423       if( fd<0 ){
 28012  29424         time_t t;
 28013  29425         time(&t);
 28014  29426         memcpy(zBuf, &t, sizeof(t));
 28015  29427         pid = getpid();
 28016  29428         memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
 28017  29429         assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
 28018  29430         nBuf = sizeof(t) + sizeof(pid);
 28019  29431       }else{
 28020         -      nBuf = read(fd, zBuf, nBuf);
 28021         -      close(fd);
        29432  +      do{ nBuf = osRead(fd, zBuf, nBuf); }while( nBuf<0 && errno==EINTR );
        29433  +      robust_close(0, fd, __LINE__);
 28022  29434       }
 28023  29435     }
 28024  29436   #endif
 28025  29437     return nBuf;
 28026  29438   }
 28027  29439   
 28028  29440   
................................................................................
 28412  29824     }else{
 28413  29825       pUnused = sqlite3_malloc(sizeof(*pUnused));
 28414  29826       if( !pUnused ){
 28415  29827         return SQLITE_NOMEM;
 28416  29828       }
 28417  29829     }
 28418  29830     if( fd<0 ){
 28419         -    fd = open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
        29831  +    fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
 28420  29832       terrno = errno;
 28421  29833       if( fd<0 && errno==ENOENT && islockfile ){
 28422  29834         if( proxyCreateLockPath(path) == SQLITE_OK ){
 28423         -        fd = open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
        29835  +        fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
 28424  29836         }
 28425  29837       }
 28426  29838     }
 28427  29839     if( fd<0 ){
 28428  29840       openFlags = O_RDONLY;
 28429         -    fd = open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
        29841  +    fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
 28430  29842       terrno = errno;
 28431  29843     }
 28432  29844     if( fd<0 ){
 28433  29845       if( islockfile ){
 28434  29846         return SQLITE_BUSY;
 28435  29847       }
 28436  29848       switch (terrno) {
................................................................................
 28446  29858     pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew));
 28447  29859     if( pNew==NULL ){
 28448  29860       rc = SQLITE_NOMEM;
 28449  29861       goto end_create_proxy;
 28450  29862     }
 28451  29863     memset(pNew, 0, sizeof(unixFile));
 28452  29864     pNew->openFlags = openFlags;
        29865  +  memset(&dummyVfs, 0, sizeof(dummyVfs));
 28453  29866     dummyVfs.pAppData = (void*)&autolockIoFinder;
        29867  +  dummyVfs.zName = "dummy";
 28454  29868     pUnused->fd = fd;
 28455  29869     pUnused->flags = openFlags;
 28456  29870     pNew->pUnused = pUnused;
 28457  29871     
 28458         -  rc = fillInUnixFile(&dummyVfs, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0);
        29872  +  rc = fillInUnixFile(&dummyVfs, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0, 0);
 28459  29873     if( rc==SQLITE_OK ){
 28460  29874       *ppFile = pNew;
 28461  29875       return SQLITE_OK;
 28462  29876     }
 28463  29877   end_create_proxy:    
 28464         -  close(fd); /* silently leak fd if error, we're already in error */
        29878  +  robust_close(pNew, fd, __LINE__);
 28465  29879     sqlite3_free(pNew);
 28466  29880     sqlite3_free(pUnused);
 28467  29881     return rc;
 28468  29882   }
 28469  29883   
 28470  29884   #ifdef SQLITE_TEST
 28471  29885   /* simulate multiple hosts by creating unique hostid file paths */
................................................................................
 28536  29950     pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
 28537  29951     if( pathLen>MAXPATHLEN || pathLen<6 || 
 28538  29952        (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
 28539  29953       sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
 28540  29954       goto end_breaklock;
 28541  29955     }
 28542  29956     /* read the conch content */
 28543         -  readLen = pread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
        29957  +  readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
 28544  29958     if( readLen<PROXY_PATHINDEX ){
 28545  29959       sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
 28546  29960       goto end_breaklock;
 28547  29961     }
 28548  29962     /* write it out to the temporary break file */
 28549         -  fd = open(tPath, (O_RDWR|O_CREAT|O_EXCL), SQLITE_DEFAULT_FILE_PERMISSIONS);
        29963  +  fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL),
        29964  +                   SQLITE_DEFAULT_FILE_PERMISSIONS);
 28550  29965     if( fd<0 ){
 28551  29966       sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
 28552  29967       goto end_breaklock;
 28553  29968     }
 28554         -  if( pwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
        29969  +  if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
 28555  29970       sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
 28556  29971       goto end_breaklock;
 28557  29972     }
 28558  29973     if( rename(tPath, cPath) ){
 28559  29974       sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
 28560  29975       goto end_breaklock;
 28561  29976     }
 28562  29977     rc = 0;
 28563  29978     fprintf(stderr, "broke stale lock on %s\n", cPath);
 28564         -  close(conchFile->h);
        29979  +  robust_close(pFile, conchFile->h, __LINE__);
 28565  29980     conchFile->h = fd;
 28566  29981     conchFile->openFlags = O_RDWR | O_CREAT;
 28567  29982   
 28568  29983   end_breaklock:
 28569  29984     if( rc ){
 28570  29985       if( fd>=0 ){
 28571  29986         unlink(tPath);
 28572         -      close(fd);
        29987  +      robust_close(pFile, fd, __LINE__);
 28573  29988       }
 28574  29989       fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
 28575  29990     }
 28576  29991     return rc;
 28577  29992   }
 28578  29993   
 28579  29994   /* Take the requested lock on the conch file and break a stale lock if the 
................................................................................
 28593  30008         /* If the lock failed (busy):
 28594  30009          * 1st try: get the mod time of the conch, wait 0.5s and try again. 
 28595  30010          * 2nd try: fail if the mod time changed or host id is different, wait 
 28596  30011          *           10 sec and try again
 28597  30012          * 3rd try: break the lock unless the mod time has changed.
 28598  30013          */
 28599  30014         struct stat buf;
 28600         -      if( fstat(conchFile->h, &buf) ){
        30015  +      if( osFstat(conchFile->h, &buf) ){
 28601  30016           pFile->lastErrno = errno;
 28602  30017           return SQLITE_IOERR_LOCK;
 28603  30018         }
 28604  30019         
 28605  30020         if( nTries==1 ){
 28606  30021           conchModTime = buf.st_mtimespec;
 28607  30022           usleep(500000); /* wait 0.5 sec and try the lock again*/
................................................................................
 28612  30027         if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || 
 28613  30028            conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
 28614  30029           return SQLITE_BUSY;
 28615  30030         }
 28616  30031         
 28617  30032         if( nTries==2 ){  
 28618  30033           char tBuf[PROXY_MAXCONCHLEN];
 28619         -        int len = pread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
        30034  +        int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
 28620  30035           if( len<0 ){
 28621  30036             pFile->lastErrno = errno;
 28622  30037             return SQLITE_IOERR_LOCK;
 28623  30038           }
 28624  30039           if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
 28625  30040             /* don't break the lock if the host id doesn't match */
 28626  30041             if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
................................................................................
 28774  30189           memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
 28775  30190           if( pCtx->lockProxyPath!=NULL ){
 28776  30191             strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN);
 28777  30192           }else{
 28778  30193             strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
 28779  30194           }
 28780  30195           writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
 28781         -        ftruncate(conchFile->h, writeSize);
        30196  +        robust_ftruncate(conchFile->h, writeSize);
 28782  30197           rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
 28783  30198           fsync(conchFile->h);
 28784  30199           /* If we created a new conch file (not just updated the contents of a 
 28785  30200            ** valid conch file), try to match the permissions of the database 
 28786  30201            */
 28787  30202           if( rc==SQLITE_OK && createConch ){
 28788  30203             struct stat buf;
 28789         -          int err = fstat(pFile->h, &buf);
        30204  +          int err = osFstat(pFile->h, &buf);
 28790  30205             if( err==0 ){
 28791  30206               mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP |
 28792  30207                                           S_IROTH|S_IWOTH);
 28793  30208               /* try to match the database file R/W permissions, ignore failure */
 28794  30209   #ifndef SQLITE_PROXY_DEBUG
 28795         -            fchmod(conchFile->h, cmode);
        30210  +            osFchmod(conchFile->h, cmode);
 28796  30211   #else
 28797         -            if( fchmod(conchFile->h, cmode)!=0 ){
        30212  +            do{
        30213  +              rc = osFchmod(conchFile->h, cmode);
        30214  +            }while( rc==(-1) && errno==EINTR );
        30215  +            if( rc!=0 ){
 28798  30216                 int code = errno;
 28799  30217                 fprintf(stderr, "fchmod %o FAILED with %d %s\n",
 28800  30218                         cmode, code, strerror(code));
 28801  30219               } else {
 28802  30220                 fprintf(stderr, "fchmod %o SUCCEDED\n",cmode);
 28803  30221               }
 28804  30222             }else{
................................................................................
 28811  30229         }
 28812  30230         conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
 28813  30231         
 28814  30232       end_takeconch:
 28815  30233         OSTRACE(("TRANSPROXY: CLOSE  %d\n", pFile->h));
 28816  30234         if( rc==SQLITE_OK && pFile->openFlags ){
 28817  30235           if( pFile->h>=0 ){
 28818         -#ifdef STRICT_CLOSE_ERROR
 28819         -          if( close(pFile->h) ){
 28820         -            pFile->lastErrno = errno;
 28821         -            return SQLITE_IOERR_CLOSE;
 28822         -          }
 28823         -#else
 28824         -          close(pFile->h); /* silently leak fd if fail */
 28825         -#endif
        30236  +          robust_close(pFile, pFile->h, __LINE__);
 28826  30237           }
 28827  30238           pFile->h = -1;
 28828         -        int fd = open(pCtx->dbPath, pFile->openFlags,
        30239  +        int fd = robust_open(pCtx->dbPath, pFile->openFlags,
 28829  30240                         SQLITE_DEFAULT_FILE_PERMISSIONS);
 28830  30241           OSTRACE(("TRANSPROXY: OPEN  %d\n", fd));
 28831  30242           if( fd>=0 ){
 28832  30243             pFile->h = fd;
 28833  30244           }else{
 28834  30245             rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
 28835  30246              during locking */
................................................................................
 29047  30458         ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
 29048  30459         ** that openFlags will have only one of O_RDONLY or O_RDWR.
 29049  30460         */
 29050  30461         struct statfs fsInfo;
 29051  30462         struct stat conchInfo;
 29052  30463         int goLockless = 0;
 29053  30464   
 29054         -      if( stat(pCtx->conchFilePath, &conchInfo) == -1 ) {
        30465  +      if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
 29055  30466           int err = errno;
 29056  30467           if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
 29057  30468             goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
 29058  30469           }
 29059  30470         }
 29060  30471         if( goLockless ){
 29061  30472           pCtx->conchHeld = -1; /* read only FS/ lockless */
................................................................................
 29332  30743     ** Most finders simply return a pointer to a fixed sqlite3_io_methods
 29333  30744     ** object.  But the "autolockIoFinder" available on MacOSX does a little
 29334  30745     ** more than that; it looks at the filesystem type that hosts the 
 29335  30746     ** database file and tries to choose an locking method appropriate for
 29336  30747     ** that filesystem time.
 29337  30748     */
 29338  30749     #define UNIXVFS(VFSNAME, FINDER) {                        \
 29339         -    2,                    /* iVersion */                    \
        30750  +    3,                    /* iVersion */                    \
 29340  30751       sizeof(unixFile),     /* szOsFile */                    \
 29341  30752       MAX_PATHNAME,         /* mxPathname */                  \
 29342  30753       0,                    /* pNext */                       \
 29343  30754       VFSNAME,              /* zName */                       \
 29344  30755       (void*)&FINDER,       /* pAppData */                    \
 29345  30756       unixOpen,             /* xOpen */                       \
 29346  30757       unixDelete,           /* xDelete */                     \
................................................................................
 29351  30762       unixDlSym,            /* xDlSym */                      \
 29352  30763       unixDlClose,          /* xDlClose */                    \
 29353  30764       unixRandomness,       /* xRandomness */                 \
 29354  30765       unixSleep,            /* xSleep */                      \
 29355  30766       unixCurrentTime,      /* xCurrentTime */                \
 29356  30767       unixGetLastError,     /* xGetLastError */               \
 29357  30768       unixCurrentTimeInt64, /* xCurrentTimeInt64 */           \
        30769  +    unixSetSystemCall,    /* xSetSystemCall */              \
        30770  +    unixGetSystemCall,    /* xGetSystemCall */              \
        30771  +    unixNextSystemCall,   /* xNextSystemCall */             \
 29358  30772     }
 29359  30773   
 29360  30774     /*
 29361  30775     ** All default VFSes for unix are contained in the following array.
 29362  30776     **
 29363  30777     ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
 29364  30778     ** by the SQLite core when the VFS is registered.  So the following
................................................................................
 29368  30782   #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__))
 29369  30783       UNIXVFS("unix",          autolockIoFinder ),
 29370  30784   #else
 29371  30785       UNIXVFS("unix",          posixIoFinder ),
 29372  30786   #endif
 29373  30787       UNIXVFS("unix-none",     nolockIoFinder ),
 29374  30788       UNIXVFS("unix-dotfile",  dotlockIoFinder ),
        30789  +    UNIXVFS("unix-excl",     posixIoFinder ),
 29375  30790   #if OS_VXWORKS
 29376  30791       UNIXVFS("unix-namedsem", semIoFinder ),
 29377  30792   #endif
 29378  30793   #if SQLITE_ENABLE_LOCKING_STYLE
 29379  30794       UNIXVFS("unix-posix",    posixIoFinder ),
 29380  30795   #if !OS_VXWORKS
 29381  30796       UNIXVFS("unix-flock",    flockIoFinder ),
................................................................................
 32358  33773   
 32359  33774   
 32360  33775   /*
 32361  33776   ** Initialize and deinitialize the operating system interface.
 32362  33777   */
 32363  33778   SQLITE_API int sqlite3_os_init(void){
 32364  33779     static sqlite3_vfs winVfs = {
 32365         -    2,                   /* iVersion */
        33780  +    3,                   /* iVersion */
 32366  33781       sizeof(winFile),     /* szOsFile */
 32367  33782       MAX_PATH,            /* mxPathname */
 32368  33783       0,                   /* pNext */
 32369  33784       "win32",             /* zName */
 32370  33785       0,                   /* pAppData */
 32371  33786       winOpen,             /* xOpen */
 32372  33787       winDelete,           /* xDelete */
................................................................................
 32377  33792       winDlSym,            /* xDlSym */
 32378  33793       winDlClose,          /* xDlClose */
 32379  33794       winRandomness,       /* xRandomness */
 32380  33795       winSleep,            /* xSleep */
 32381  33796       winCurrentTime,      /* xCurrentTime */
 32382  33797       winGetLastError,     /* xGetLastError */
 32383  33798       winCurrentTimeInt64, /* xCurrentTimeInt64 */
        33799  +    0,                   /* xSetSystemCall */
        33800  +    0,                   /* xGetSystemCall */
        33801  +    0,                   /* xNextSystemCall */
 32384  33802     };
 32385  33803   
 32386  33804   #ifndef SQLITE_OMIT_WAL
 32387  33805     /* get memory map allocation granularity */
 32388  33806     memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
 32389  33807     GetSystemInfo(&winSysInfo);
 32390  33808     assert(winSysInfo.dwAllocationGranularity > 0);
................................................................................
 34831  36249   */
 34832  36250   
 34833  36251   #ifndef _WAL_H_
 34834  36252   #define _WAL_H_
 34835  36253   
 34836  36254   
 34837  36255   #ifdef SQLITE_OMIT_WAL
 34838         -# define sqlite3WalOpen(x,y,z)                 0
 34839         -# define sqlite3WalClose(w,x,y,z)              0
 34840         -# define sqlite3WalBeginReadTransaction(y,z)   0
        36256  +# define sqlite3WalOpen(x,y,z)                   0
        36257  +# define sqlite3WalClose(w,x,y,z)                0
        36258  +# define sqlite3WalBeginReadTransaction(y,z)     0
 34841  36259   # define sqlite3WalEndReadTransaction(z)
 34842         -# define sqlite3WalRead(v,w,x,y,z)             0
 34843         -# define sqlite3WalDbsize(y)                   0
 34844         -# define sqlite3WalBeginWriteTransaction(y)    0
 34845         -# define sqlite3WalEndWriteTransaction(x)      0
 34846         -# define sqlite3WalUndo(x,y,z)                 0
        36260  +# define sqlite3WalRead(v,w,x,y,z)               0
        36261  +# define sqlite3WalDbsize(y)                     0
        36262  +# define sqlite3WalBeginWriteTransaction(y)      0
        36263  +# define sqlite3WalEndWriteTransaction(x)        0
        36264  +# define sqlite3WalUndo(x,y,z)                   0
 34847  36265   # define sqlite3WalSavepoint(y,z)
 34848         -# define sqlite3WalSavepointUndo(y,z)          0
 34849         -# define sqlite3WalFrames(u,v,w,x,y,z)         0
 34850         -# define sqlite3WalCheckpoint(u,v,w,x)         0
 34851         -# define sqlite3WalCallback(z)                 0
 34852         -# define sqlite3WalExclusiveMode(y,z)          0
 34853         -# define sqlite3WalHeapMemory(z)               0
        36266  +# define sqlite3WalSavepointUndo(y,z)            0
        36267  +# define sqlite3WalFrames(u,v,w,x,y,z)           0
        36268  +# define sqlite3WalCheckpoint(r,s,t,u,v,w,x,y,z) 0
        36269  +# define sqlite3WalCallback(z)                   0
        36270  +# define sqlite3WalExclusiveMode(y,z)            0
        36271  +# define sqlite3WalHeapMemory(z)                 0
 34854  36272   #else
 34855  36273   
 34856  36274   #define WAL_SAVEPOINT_NDATA 4
 34857  36275   
 34858  36276   /* Connection to a write-ahead log (WAL) file. 
 34859  36277   ** There is one object of this type for each pager. 
 34860  36278   */
................................................................................
 34897  36315   
 34898  36316   /* Write a frame or frames to the log. */
 34899  36317   SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
 34900  36318   
 34901  36319   /* Copy pages from the log to the database file */ 
 34902  36320   SQLITE_PRIVATE int sqlite3WalCheckpoint(
 34903  36321     Wal *pWal,                      /* Write-ahead log connection */
        36322  +  int eMode,                      /* One of PASSIVE, FULL and RESTART */
        36323  +  int (*xBusy)(void*),            /* Function to call when busy */
        36324  +  void *pBusyArg,                 /* Context argument for xBusyHandler */
 34904  36325     int sync_flags,                 /* Flags to sync db file with (or 0) */
 34905  36326     int nBuf,                       /* Size of buffer nBuf */
 34906         -  u8 *zBuf                        /* Temporary buffer to use */
        36327  +  u8 *zBuf,                       /* Temporary buffer to use */
        36328  +  int *pnLog,                     /* OUT: Number of frames in WAL */
        36329  +  int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
 34907  36330   );
 34908  36331   
 34909  36332   /* Return the value to pass to a sqlite3_wal_hook callback, the
 34910  36333   ** number of frames in the WAL at the point of the last commit since
 34911  36334   ** sqlite3WalCallback() was called.  If no commits have occurred since
 34912  36335   ** the last call, then return 0.
 34913  36336   */
................................................................................
 37754  39177     PAGER_INCR(pPager->nRead);
 37755  39178     IOTRACE(("PGIN %p %d\n", pPager, pgno));
 37756  39179     PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
 37757  39180                  PAGERID(pPager), pgno, pager_pagehash(pPg)));
 37758  39181   
 37759  39182     return rc;
 37760  39183   }
        39184  +
        39185  +/*
        39186  +** Update the value of the change-counter at offsets 24 and 92 in
        39187  +** the header and the sqlite version number at offset 96.
        39188  +**
        39189  +** This is an unconditional update.  See also the pager_incr_changecounter()
        39190  +** routine which only updates the change-counter if the update is actually
        39191  +** needed, as determined by the pPager->changeCountDone state variable.
        39192  +*/
        39193  +static void pager_write_changecounter(PgHdr *pPg){
        39194  +  u32 change_counter;
        39195  +
        39196  +  /* Increment the value just read and write it back to byte 24. */
        39197  +  change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
        39198  +  put32bits(((char*)pPg->pData)+24, change_counter);
        39199  +
        39200  +  /* Also store the SQLite version number in bytes 96..99 and in
        39201  +  ** bytes 92..95 store the change counter for which the version number
        39202  +  ** is valid. */
        39203  +  put32bits(((char*)pPg->pData)+92, change_counter);
        39204  +  put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
        39205  +}
 37761  39206   
 37762  39207   #ifndef SQLITE_OMIT_WAL
 37763  39208   /*
 37764  39209   ** This function is invoked once for each page that has already been 
 37765  39210   ** written into the log file when a WAL transaction is rolled back.
 37766  39211   ** Parameter iPg is the page number of said page. The pCtx argument 
 37767  39212   ** is actually a pointer to the Pager structure.
................................................................................
 37825  39270       rc = pagerUndoCallback((void *)pPager, pList->pgno);
 37826  39271       pList = pNext;
 37827  39272     }
 37828  39273   
 37829  39274     return rc;
 37830  39275   }
 37831  39276   
 37832         -
 37833         -/*
 37834         -** Update the value of the change-counter at offsets 24 and 92 in
 37835         -** the header and the sqlite version number at offset 96.
 37836         -**
 37837         -** This is an unconditional update.  See also the pager_incr_changecounter()
 37838         -** routine which only updates the change-counter if the update is actually
 37839         -** needed, as determined by the pPager->changeCountDone state variable.
 37840         -*/
 37841         -static void pager_write_changecounter(PgHdr *pPg){
 37842         -  u32 change_counter;
 37843         -
 37844         -  /* Increment the value just read and write it back to byte 24. */
 37845         -  change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
 37846         -  put32bits(((char*)pPg->pData)+24, change_counter);
 37847         -
 37848         -  /* Also store the SQLite version number in bytes 96..99 and in
 37849         -  ** bytes 92..95 store the change counter for which the version number
 37850         -  ** is valid. */
 37851         -  put32bits(((char*)pPg->pData)+92, change_counter);
 37852         -  put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
 37853         -}
 37854         -
 37855  39277   /*
 37856  39278   ** This function is a wrapper around sqlite3WalFrames(). As well as logging
 37857  39279   ** the contents of the list of pages headed by pList (connected by pDirty),
 37858  39280   ** this function notifies any active backup processes that the pages have
 37859         -** changed.
        39281  +** changed. 
 37860  39282   **
 37861  39283   ** The list of pages passed into this routine is always sorted by page number.
 37862  39284   ** Hence, if page 1 appears anywhere on the list, it will be the first page.
 37863  39285   */ 
 37864  39286   static int pagerWalFrames(
 37865  39287     Pager *pPager,                  /* Pager object */
 37866  39288     PgHdr *pList,                   /* List of frames to log */
................................................................................
 37876  39298     assert( pPager->pWal );
 37877  39299   #ifdef SQLITE_DEBUG
 37878  39300     /* Verify that the page list is in accending order */
 37879  39301     for(p=pList; p && p->pDirty; p=p->pDirty){
 37880  39302       assert( p->pgno < p->pDirty->pgno );
 37881  39303     }
 37882  39304   #endif
        39305  +
        39306  +  if( isCommit ){
        39307  +    /* If a WAL transaction is being committed, there is no point in writing
        39308  +    ** any pages with page numbers greater than nTruncate into the WAL file.
        39309  +    ** They will never be read by any client. So remove them from the pDirty
        39310  +    ** list here. */
        39311  +    PgHdr *p;
        39312  +    PgHdr **ppNext = &pList;
        39313  +    for(p=pList; (*ppNext = p); p=p->pDirty){
        39314  +      if( p->pgno<=nTruncate ) ppNext = &p->pDirty;
        39315  +    }
        39316  +    assert( pList );
        39317  +  }
 37883  39318   
 37884  39319     if( pList->pgno==1 ) pager_write_changecounter(pList);
 37885  39320     rc = sqlite3WalFrames(pPager->pWal, 
 37886  39321         pPager->pageSize, pList, nTruncate, isCommit, syncFlags
 37887  39322     );
 37888  39323     if( rc==SQLITE_OK && pPager->pBackup ){
 37889  39324       PgHdr *p;
 37890  39325       for(p=pList; p; p=p->pDirty){
 37891  39326         sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
 37892  39327       }
 37893  39328     }
 37894  39329   
 37895  39330   #ifdef SQLITE_CHECK_PAGES
        39331  +  pList = sqlite3PcacheDirtyList(pPager->pPCache);
 37896  39332     for(p=pList; p; p=p->pDirty){
 37897  39333       pager_set_pagehash(p);
 37898  39334     }
 37899  39335   #endif
 37900  39336   
 37901  39337     return rc;
 37902  39338   }
................................................................................
 41504  42940   */
 41505  42941   SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
 41506  42942     return &pPager->pBackup;
 41507  42943   }
 41508  42944   
 41509  42945   #ifndef SQLITE_OMIT_WAL
 41510  42946   /*
 41511         -** This function is called when the user invokes "PRAGMA checkpoint".
        42947  +** This function is called when the user invokes "PRAGMA wal_checkpoint",
        42948  +** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
        42949  +** or wal_blocking_checkpoint() API functions.
        42950  +**
        42951  +** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
 41512  42952   */
 41513         -SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager){
        42953  +SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){
 41514  42954     int rc = SQLITE_OK;
 41515  42955     if( pPager->pWal ){
 41516         -    u8 *zBuf = (u8 *)pPager->pTmpSpace;
 41517         -    rc = sqlite3WalCheckpoint(pPager->pWal, pPager->ckptSyncFlags,
 41518         -                              pPager->pageSize, zBuf);
        42956  +    rc = sqlite3WalCheckpoint(pPager->pWal, eMode,
        42957  +        pPager->xBusyHandler, pPager->pBusyHandlerArg,
        42958  +        pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
        42959  +        pnLog, pnCkpt
        42960  +    );
 41519  42961     }
 41520  42962     return rc;
 41521  42963   }
 41522  42964   
 41523  42965   SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
 41524  42966     return sqlite3WalCallback(pPager->pWal);
 41525  42967   }
................................................................................
 41539  42981   */
 41540  42982   static int pagerExclusiveLock(Pager *pPager){
 41541  42983     int rc;                         /* Return code */
 41542  42984   
 41543  42985     assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
 41544  42986     rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
 41545  42987     if( rc!=SQLITE_OK ){
 41546         -    /* If the attempt to grab the pending lock failed, release the 
 41547         -    ** exclusive lock that may have been obtained instead.  */
        42988  +    /* If the attempt to grab the exclusive lock failed, release the 
        42989  +    ** pending lock that may have been obtained instead.  */
 41548  42990       pagerUnlockDb(pPager, SHARED_LOCK);
 41549  42991     }
 41550  42992   
 41551  42993     return rc;
 41552  42994   }
 41553  42995   
 41554  42996   /*
................................................................................
 43250  44692   
 43251  44693     if( rc!=SQLITE_OK ){
 43252  44694       walIteratorFree(p);
 43253  44695     }
 43254  44696     *pp = p;
 43255  44697     return rc;
 43256  44698   }
        44699  +
        44700  +/*
        44701  +** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
        44702  +** n. If the attempt fails and parameter xBusy is not NULL, then it is a
        44703  +** busy-handler function. Invoke it and retry the lock until either the
        44704  +** lock is successfully obtained or the busy-handler returns 0.
        44705  +*/
        44706  +static int walBusyLock(
        44707  +  Wal *pWal,                      /* WAL connection */
        44708  +  int (*xBusy)(void*),            /* Function to call when busy */
        44709  +  void *pBusyArg,                 /* Context argument for xBusyHandler */
        44710  +  int lockIdx,                    /* Offset of first byte to lock */
        44711  +  int n                           /* Number of bytes to lock */
        44712  +){
        44713  +  int rc;
        44714  +  do {
        44715  +    rc = walLockExclusive(pWal, lockIdx, n);
        44716  +  }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
        44717  +  return rc;
        44718  +}
        44719  +
        44720  +/*
        44721  +** The cache of the wal-index header must be valid to call this function.
        44722  +** Return the page-size in bytes used by the database.
        44723  +*/
        44724  +static int walPagesize(Wal *pWal){
        44725  +  return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
        44726  +}
 43257  44727   
 43258  44728   /*
 43259  44729   ** Copy as much content as we can from the WAL back into the database file
 43260  44730   ** in response to an sqlite3_wal_checkpoint() request or the equivalent.
 43261  44731   **
 43262  44732   ** The amount of information copies from WAL to database might be limited
 43263  44733   ** by active readers.  This routine will never overwrite a database page
................................................................................
 43284  44754   **
 43285  44755   ** The caller must be holding sufficient locks to ensure that no other
 43286  44756   ** checkpoint is running (in any other thread or process) at the same
 43287  44757   ** time.
 43288  44758   */
 43289  44759   static int walCheckpoint(
 43290  44760     Wal *pWal,                      /* Wal connection */
        44761  +  int eMode,                      /* One of PASSIVE, FULL or RESTART */
        44762  +  int (*xBusyCall)(void*),        /* Function to call when busy */
        44763  +  void *pBusyArg,                 /* Context argument for xBusyHandler */
 43291  44764     int sync_flags,                 /* Flags for OsSync() (or 0) */
 43292         -  int nBuf,                       /* Size of zBuf in bytes */
 43293  44765     u8 *zBuf                        /* Temporary buffer to use */
 43294  44766   ){
 43295  44767     int rc;                         /* Return code */
 43296  44768     int szPage;                     /* Database page-size */
 43297  44769     WalIterator *pIter = 0;         /* Wal iterator context */
 43298  44770     u32 iDbpage = 0;                /* Next database page to write */
 43299  44771     u32 iFrame = 0;                 /* Wal frame containing data for iDbpage */
 43300  44772     u32 mxSafeFrame;                /* Max frame that can be backfilled */
 43301  44773     u32 mxPage;                     /* Max database page to write */
 43302  44774     int i;                          /* Loop counter */
 43303  44775     volatile WalCkptInfo *pInfo;    /* The checkpoint status information */
        44776  +  int (*xBusy)(void*) = 0;        /* Function to call when waiting for locks */
 43304  44777   
 43305         -  szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
        44778  +  szPage = walPagesize(pWal);
 43306  44779     testcase( szPage<=32768 );
 43307  44780     testcase( szPage>=65536 );
 43308  44781     pInfo = walCkptInfo(pWal);
 43309  44782     if( pInfo->nBackfill>=pWal->hdr.mxFrame ) return SQLITE_OK;
 43310  44783   
 43311  44784     /* Allocate the iterator */
 43312  44785     rc = walIteratorInit(pWal, &pIter);
 43313  44786     if( rc!=SQLITE_OK ){
 43314  44787       return rc;
 43315  44788     }
 43316  44789     assert( pIter );
 43317  44790   
 43318         -  /*** TODO:  Move this test out to the caller.  Make it an assert() here ***/
 43319         -  if( szPage!=nBuf ){
 43320         -    rc = SQLITE_CORRUPT_BKPT;
 43321         -    goto walcheckpoint_out;
 43322         -  }
        44791  +  if( eMode!=SQLITE_CHECKPOINT_PASSIVE ) xBusy = xBusyCall;
 43323  44792   
 43324  44793     /* Compute in mxSafeFrame the index of the last frame of the WAL that is
 43325  44794     ** safe to write into the database.  Frames beyond mxSafeFrame might
 43326  44795     ** overwrite database pages that are in use by active readers and thus
 43327  44796     ** cannot be backfilled from the WAL.
 43328  44797     */
 43329  44798     mxSafeFrame = pWal->hdr.mxFrame;
 43330  44799     mxPage = pWal->hdr.nPage;
 43331  44800     for(i=1; i<WAL_NREADER; i++){
 43332  44801       u32 y = pInfo->aReadMark[i];
 43333         -    if( mxSafeFrame>=y ){
        44802  +    if( mxSafeFrame>y ){
 43334  44803         assert( y<=pWal->hdr.mxFrame );
 43335         -      rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
        44804  +      rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
 43336  44805         if( rc==SQLITE_OK ){
 43337  44806           pInfo->aReadMark[i] = READMARK_NOT_USED;
 43338  44807           walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
 43339  44808         }else if( rc==SQLITE_BUSY ){
 43340  44809           mxSafeFrame = y;
        44810  +        xBusy = 0;
 43341  44811         }else{
 43342  44812           goto walcheckpoint_out;
 43343  44813         }
 43344  44814       }
 43345  44815     }
 43346  44816   
 43347  44817     if( pInfo->nBackfill<mxSafeFrame
 43348         -   && (rc = walLockExclusive(pWal, WAL_READ_LOCK(0), 1))==SQLITE_OK
        44818  +   && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0), 1))==SQLITE_OK
 43349  44819     ){
 43350  44820       i64 nSize;                    /* Current size of database file */
 43351  44821       u32 nBackfill = pInfo->nBackfill;
 43352  44822   
 43353  44823       /* Sync the WAL to disk */
 43354  44824       if( sync_flags ){
 43355  44825         rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
................................................................................
 43394  44864         if( rc==SQLITE_OK ){
 43395  44865           pInfo->nBackfill = mxSafeFrame;
 43396  44866         }
 43397  44867       }
 43398  44868   
 43399  44869       /* Release the reader lock held while backfilling */
 43400  44870       walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
 43401         -  }else if( rc==SQLITE_BUSY ){
        44871  +  }
        44872  +
        44873  +  if( rc==SQLITE_BUSY ){
 43402  44874       /* Reset the return code so as not to report a checkpoint failure
 43403         -    ** just because active readers prevent any backfill.
 43404         -    */
        44875  +    ** just because there are active readers.  */
 43405  44876       rc = SQLITE_OK;
 43406  44877     }
        44878  +
        44879  +  /* If this is an SQLITE_CHECKPOINT_RESTART operation, and the entire wal
        44880  +  ** file has been copied into the database file, then block until all
        44881  +  ** readers have finished using the wal file. This ensures that the next
        44882  +  ** process to write to the database restarts the wal file.
        44883  +  */
        44884  +  if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
        44885  +    assert( pWal->writeLock );
        44886  +    if( pInfo->nBackfill<pWal->hdr.mxFrame ){
        44887  +      rc = SQLITE_BUSY;
        44888  +    }else if( eMode==SQLITE_CHECKPOINT_RESTART ){
        44889  +      assert( mxSafeFrame==pWal->hdr.mxFrame );
        44890  +      rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
        44891  +      if( rc==SQLITE_OK ){
        44892  +        walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
        44893  +      }
        44894  +    }
        44895  +  }
 43407  44896   
 43408  44897    walcheckpoint_out:
 43409  44898     walIteratorFree(pIter);
 43410  44899     return rc;
 43411  44900   }
 43412  44901   
 43413  44902   /*
................................................................................
 43432  44921       ** The EXCLUSIVE lock is not released before returning.
 43433  44922       */
 43434  44923       rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
 43435  44924       if( rc==SQLITE_OK ){
 43436  44925         if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
 43437  44926           pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
 43438  44927         }
 43439         -      rc = sqlite3WalCheckpoint(pWal, sync_flags, nBuf, zBuf);
        44928  +      rc = sqlite3WalCheckpoint(
        44929  +          pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
        44930  +      );
 43440  44931         if( rc==SQLITE_OK ){
 43441  44932           isDelete = 1;
 43442  44933         }
 43443  44934       }
 43444  44935   
 43445  44936       walIndexClose(pWal, isDelete);
 43446  44937       sqlite3OsClose(pWal->pWalFd);
................................................................................
 43644  45135     u32 mxReadMark;                 /* Largest aReadMark[] value */
 43645  45136     int mxI;                        /* Index of largest aReadMark[] value */
 43646  45137     int i;                          /* Loop counter */
 43647  45138     int rc = SQLITE_OK;             /* Return code  */
 43648  45139   
 43649  45140     assert( pWal->readLock<0 );     /* Not currently locked */
 43650  45141   
 43651         -  /* Take steps to avoid spinning forever if there is a protocol error. */
        45142  +  /* Take steps to avoid spinning forever if there is a protocol error.
        45143  +  **
        45144  +  ** Circumstances that cause a RETRY should only last for the briefest
        45145  +  ** instances of time.  No I/O or other system calls are done while the
        45146  +  ** locks are held, so the locks should not be held for very long. But 
        45147  +  ** if we are unlucky, another process that is holding a lock might get
        45148  +  ** paged out or take a page-fault that is time-consuming to resolve, 
        45149  +  ** during the few nanoseconds that it is holding the lock.  In that case,
        45150  +  ** it might take longer than normal for the lock to free.
        45151  +  **
        45152  +  ** After 5 RETRYs, we begin calling sqlite3OsSleep().  The first few
        45153  +  ** calls to sqlite3OsSleep() have a delay of 1 microsecond.  Really this
        45154  +  ** is more of a scheduler yield than an actual delay.  But on the 10th
        45155  +  ** an subsequent retries, the delays start becoming longer and longer, 
        45156  +  ** so that on the 100th (and last) RETRY we delay for 21 milliseconds.
        45157  +  ** The total delay time before giving up is less than 1 second.
        45158  +  */
 43652  45159     if( cnt>5 ){
 43653         -    if( cnt>100 ) return SQLITE_PROTOCOL;
 43654         -    sqlite3OsSleep(pWal->pVfs, 1);
        45160  +    int nDelay = 1;                      /* Pause time in microseconds */
        45161  +    if( cnt>100 ){
        45162  +      VVA_ONLY( pWal->lockError = 1; )
        45163  +      return SQLITE_PROTOCOL;
        45164  +    }
        45165  +    if( cnt>=10 ) nDelay = (cnt-9)*238;  /* Max delay 21ms. Total delay 996ms */
        45166  +    sqlite3OsSleep(pWal->pVfs, nDelay);
 43655  45167     }
 43656  45168   
 43657  45169     if( !useWal ){
 43658  45170       rc = walIndexReadHdr(pWal, pChanged);
 43659  45171       if( rc==SQLITE_BUSY ){
 43660  45172         /* If there is not a recovery running in another thread or process
 43661  45173         ** then convert BUSY errors to WAL_RETRY.  If recovery is known to
................................................................................
 43729  45241       u32 thisMark = pInfo->aReadMark[i];
 43730  45242       if( mxReadMark<=thisMark && thisMark<=pWal->hdr.mxFrame ){
 43731  45243         assert( thisMark!=READMARK_NOT_USED );
 43732  45244         mxReadMark = thisMark;
 43733  45245         mxI = i;
 43734  45246       }
 43735  45247     }
 43736         -  if( mxI==0 ){
 43737         -    /* If we get here, it means that all of the aReadMark[] entries between
 43738         -    ** 1 and WAL_NREADER-1 are zero.  Try to initialize aReadMark[1] to
 43739         -    ** be mxFrame, then retry.
 43740         -    */
 43741         -    rc = walLockExclusive(pWal, WAL_READ_LOCK(1), 1);
 43742         -    if( rc==SQLITE_OK ){
 43743         -      pInfo->aReadMark[1] = pWal->hdr.mxFrame;
 43744         -      walUnlockExclusive(pWal, WAL_READ_LOCK(1), 1);
 43745         -      rc = WAL_RETRY;
 43746         -    }else if( rc==SQLITE_BUSY ){
 43747         -      rc = WAL_RETRY;
 43748         -    }
 43749         -    return rc;
 43750         -  }else{
 43751         -    if( mxReadMark < pWal->hdr.mxFrame ){
        45248  +  /* There was once an "if" here. The extra "{" is to preserve indentation. */
        45249  +  {
        45250  +    if( mxReadMark < pWal->hdr.mxFrame || mxI==0 ){
 43752  45251         for(i=1; i<WAL_NREADER; i++){
 43753  45252           rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
 43754  45253           if( rc==SQLITE_OK ){
 43755  45254             mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame;
 43756  45255             mxI = i;
 43757  45256             walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
 43758  45257             break;
 43759  45258           }else if( rc!=SQLITE_BUSY ){
 43760  45259             return rc;
 43761  45260           }
 43762  45261         }
 43763  45262       }
        45263  +    if( mxI==0 ){
        45264  +      assert( rc==SQLITE_BUSY );
        45265  +      return WAL_RETRY;
        45266  +    }
 43764  45267   
 43765  45268       rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
 43766  45269       if( rc ){
 43767  45270         return rc==SQLITE_BUSY ? WAL_RETRY : rc;
 43768  45271       }
 43769  45272       /* Now that the read-lock has been obtained, check that neither the
 43770  45273       ** value in the aReadMark[] array or the contents of the wal-index
................................................................................
 43817  45320   SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
 43818  45321     int rc;                         /* Return code */
 43819  45322     int cnt = 0;                    /* Number of TryBeginRead attempts */
 43820  45323   
 43821  45324     do{
 43822  45325       rc = walTryBeginRead(pWal, pChanged, 0, ++cnt);
 43823  45326     }while( rc==WAL_RETRY );
        45327  +  testcase( (rc&0xff)==SQLITE_BUSY );
        45328  +  testcase( (rc&0xff)==SQLITE_IOERR );
        45329  +  testcase( rc==SQLITE_PROTOCOL );
        45330  +  testcase( rc==SQLITE_OK );
 43824  45331     return rc;
 43825  45332   }
 43826  45333   
 43827  45334   /*
 43828  45335   ** Finish with a read transaction.  All this does is release the
 43829  45336   ** read-lock.
 43830  45337   */
................................................................................
 44134  45641     int rc = SQLITE_OK;
 44135  45642     int cnt;
 44136  45643   
 44137  45644     if( pWal->readLock==0 ){
 44138  45645       volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
 44139  45646       assert( pInfo->nBackfill==pWal->hdr.mxFrame );
 44140  45647       if( pInfo->nBackfill>0 ){
        45648  +      u32 salt1;
        45649  +      sqlite3_randomness(4, &salt1);
 44141  45650         rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
 44142  45651         if( rc==SQLITE_OK ){
 44143  45652           /* If all readers are using WAL_READ_LOCK(0) (in other words if no
 44144  45653           ** readers are currently using the WAL), then the transactions
 44145  45654           ** frames will overwrite the start of the existing log. Update the
 44146  45655           ** wal-index header to reflect this.
 44147  45656           **
................................................................................
 44151  45660           ** to handle if this transaction is rolled back.
 44152  45661           */
 44153  45662           int i;                    /* Loop counter */
 44154  45663           u32 *aSalt = pWal->hdr.aSalt;       /* Big-endian salt values */
 44155  45664           pWal->nCkpt++;
 44156  45665           pWal->hdr.mxFrame = 0;
 44157  45666           sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
 44158         -        sqlite3_randomness(4, &aSalt[1]);
        45667  +        aSalt[1] = salt1;
 44159  45668           walIndexWriteHdr(pWal);
 44160  45669           pInfo->nBackfill = 0;
 44161  45670           for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
 44162  45671           assert( pInfo->aReadMark[0]==0 );
 44163  45672           walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
 44164  45673         }else if( rc!=SQLITE_BUSY ){
 44165  45674           return rc;
................................................................................
 44168  45677       walUnlockShared(pWal, WAL_READ_LOCK(0));
 44169  45678       pWal->readLock = -1;
 44170  45679       cnt = 0;
 44171  45680       do{
 44172  45681         int notUsed;
 44173  45682         rc = walTryBeginRead(pWal, &notUsed, 1, ++cnt);
 44174  45683       }while( rc==WAL_RETRY );
        45684  +    assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */
        45685  +    testcase( (rc&0xff)==SQLITE_IOERR );
        45686  +    testcase( rc==SQLITE_PROTOCOL );
        45687  +    testcase( rc==SQLITE_OK );
 44175  45688     }
 44176  45689     return rc;
 44177  45690   }
 44178  45691   
 44179  45692   /* 
 44180  45693   ** Write a set of frames to the log. The caller must hold the write-lock
 44181  45694   ** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
................................................................................
 44347  45860   
 44348  45861   /* 
 44349  45862   ** This routine is called to implement sqlite3_wal_checkpoint() and
 44350  45863   ** related interfaces.
 44351  45864   **
 44352  45865   ** Obtain a CHECKPOINT lock and then backfill as much information as
 44353  45866   ** we can from WAL into the database.
        45867  +**
        45868  +** If parameter xBusy is not NULL, it is a pointer to a busy-handler
        45869  +** callback. In this case this function runs a blocking checkpoint.
 44354  45870   */
 44355  45871   SQLITE_PRIVATE int sqlite3WalCheckpoint(
 44356  45872     Wal *pWal,                      /* Wal connection */
        45873  +  int eMode,                      /* PASSIVE, FULL or RESTART */
        45874  +  int (*xBusy)(void*),            /* Function to call when busy */
        45875  +  void *pBusyArg,                 /* Context argument for xBusyHandler */
 44357  45876     int sync_flags,                 /* Flags to sync db file with (or 0) */
 44358  45877     int nBuf,                       /* Size of temporary buffer */
 44359         -  u8 *zBuf                        /* Temporary buffer to use */
        45878  +  u8 *zBuf,                       /* Temporary buffer to use */
        45879  +  int *pnLog,                     /* OUT: Number of frames in WAL */
        45880  +  int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
 44360  45881   ){
 44361  45882     int rc;                         /* Return code */
 44362  45883     int isChanged = 0;              /* True if a new wal-index header is loaded */
        45884  +  int eMode2 = eMode;             /* Mode to pass to walCheckpoint() */
 44363  45885   
 44364  45886     assert( pWal->ckptLock==0 );
        45887  +  assert( pWal->writeLock==0 );
 44365  45888   
 44366  45889     WALTRACE(("WAL%p: checkpoint begins\n", pWal));
 44367  45890     rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
 44368  45891     if( rc ){
 44369  45892       /* Usually this is SQLITE_BUSY meaning that another thread or process
 44370  45893       ** is already running a checkpoint, or maybe a recovery.  But it might
 44371  45894       ** also be SQLITE_IOERR. */
 44372  45895       return rc;
 44373  45896     }
 44374  45897     pWal->ckptLock = 1;
        45898  +
        45899  +  /* If this is a blocking-checkpoint, then obtain the write-lock as well
        45900  +  ** to prevent any writers from running while the checkpoint is underway.
        45901  +  ** This has to be done before the call to walIndexReadHdr() below.
        45902  +  **
        45903  +  ** If the writer lock cannot be obtained, then a passive checkpoint is
        45904  +  ** run instead. Since the checkpointer is not holding the writer lock,
        45905  +  ** there is no point in blocking waiting for any readers. Assuming no 
        45906  +  ** other error occurs, this function will return SQLITE_BUSY to the caller.
        45907  +  */
        45908  +  if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
        45909  +    rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1);
        45910  +    if( rc==SQLITE_OK ){
        45911  +      pWal->writeLock = 1;
        45912  +    }else if( rc==SQLITE_BUSY ){
        45913  +      eMode2 = SQLITE_CHECKPOINT_PASSIVE;
        45914  +      rc = SQLITE_OK;
        45915  +    }
        45916  +  }
        45917  +
        45918  +  /* Read the wal-index header. */
        45919  +  if( rc==SQLITE_OK ){
        45920  +    rc = walIndexReadHdr(pWal, &isChanged);
        45921  +  }
 44375  45922   
 44376  45923     /* Copy data from the log to the database file. */
 44377         -  rc = walIndexReadHdr(pWal, &isChanged);
 44378  45924     if( rc==SQLITE_OK ){
 44379         -    rc = walCheckpoint(pWal, sync_flags, nBuf, zBuf);
        45925  +    if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
        45926  +      rc = SQLITE_CORRUPT_BKPT;
        45927  +    }else{
        45928  +      rc = walCheckpoint(pWal, eMode2, xBusy, pBusyArg, sync_flags, zBuf);
        45929  +    }
        45930  +
        45931  +    /* If no error occurred, set the output variables. */
        45932  +    if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
        45933  +      if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
        45934  +      if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
        45935  +    }
 44380  45936     }
        45937  +
 44381  45938     if( isChanged ){
 44382  45939       /* If a new wal-index header was loaded before the checkpoint was 
 44383  45940       ** performed, then the pager-cache associated with pWal is now
 44384  45941       ** out of date. So zero the cached wal-index header to ensure that
 44385  45942       ** next time the pager opens a snapshot on this database it knows that
 44386  45943       ** the cache needs to be reset.
 44387  45944       */
 44388  45945       memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
 44389  45946     }
 44390  45947   
 44391  45948     /* Release the locks. */
        45949  +  sqlite3WalEndWriteTransaction(pWal);
 44392  45950     walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
 44393  45951     pWal->ckptLock = 0;
 44394  45952     WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
 44395         -  return rc;
        45953  +  return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
 44396  45954   }
 44397  45955   
 44398  45956   /* Return the value to pass to a sqlite3_wal_hook callback, the
 44399  45957   ** number of frames in the WAL at the point of the last commit since
 44400  45958   ** sqlite3WalCallback() was called.  If no commits have occurred since
 44401  45959   ** the last call, then return 0.
 44402  45960   */
................................................................................
 44717  46275   **      *     zero or more pages numbers of leaves
 44718  46276   */
 44719  46277   
 44720  46278   
 44721  46279   /* The following value is the maximum cell size assuming a maximum page
 44722  46280   ** size give above.
 44723  46281   */
 44724         -#define MX_CELL_SIZE(pBt)  (pBt->pageSize-8)
        46282  +#define MX_CELL_SIZE(pBt)  ((int)(pBt->pageSize-8))
 44725  46283   
 44726  46284   /* The maximum number of cells on a single page of the database.  This
 44727  46285   ** assumes a minimum cell size of 6 bytes  (4 bytes for the cell itself
 44728  46286   ** plus 2 bytes for the index to the cell in the page header).  Such
 44729  46287   ** small cells will be rare, but they are possible.
 44730  46288   */
 44731  46289   #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
................................................................................
 44835  46393   ** points to the same BtShared object.  The database cache and the
 44836  46394   ** schema associated with the database file are all contained within
 44837  46395   ** the BtShared object.
 44838  46396   **
 44839  46397   ** All fields in this structure are accessed under sqlite3.mutex.
 44840  46398   ** The pBt pointer itself may not be changed while there exists cursors 
 44841  46399   ** in the referenced BtShared that point back to this Btree since those
 44842         -** cursors have to do go through this Btree to find their BtShared and
        46400  +** cursors have to go through this Btree to find their BtShared and
 44843  46401   ** they often do so without holding sqlite3.mutex.
 44844  46402   */
 44845  46403   struct Btree {
 44846  46404     sqlite3 *db;       /* The database connection holding this btree */
 44847  46405     BtShared *pBt;     /* Sharable content of this btree */
 44848  46406     u8 inTrans;        /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
 44849  46407     u8 sharable;       /* True if we can share pBt with another db */
................................................................................
 44925  46483     u16 minLeaf;          /* Minimum local payload in a LEAFDATA table */
 44926  46484     u32 pageSize;         /* Total number of bytes on a page */
 44927  46485     u32 usableSize;       /* Number of usable bytes on each page */
 44928  46486     int nTransaction;     /* Number of open transactions (read + write) */
 44929  46487     u32 nPage;            /* Number of pages in the database */
 44930  46488     void *pSchema;        /* Pointer to space allocated by sqlite3BtreeSchema() */
 44931  46489     void (*xFreeSchema)(void*);  /* Destructor for BtShared.pSchema */
 44932         -  sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */
        46490  +  sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
 44933  46491     Bitvec *pHasContent;  /* Set of pages moved to free-list this transaction */
 44934  46492   #ifndef SQLITE_OMIT_SHARED_CACHE
 44935  46493     int nRef;             /* Number of references to this structure */
 44936  46494     BtShared *pNext;      /* Next on a list of sharable BtShared structs */
 44937  46495     BtLock *pLock;        /* List of locks held on this shared-btree struct */
 44938  46496     Btree *pWriter;       /* Btree with currently open write transaction */
 44939  46497     u8 isExclusive;       /* True if pWriter has an EXCLUSIVE lock on the db */
................................................................................
 45166  46724   }
 45167  46725   
 45168  46726   /*
 45169  46727   ** Release the BtShared mutex associated with B-Tree handle p and
 45170  46728   ** clear the p->locked boolean.
 45171  46729   */
 45172  46730   static void unlockBtreeMutex(Btree *p){
        46731  +  BtShared *pBt = p->pBt;
 45173  46732     assert( p->locked==1 );
 45174         -  assert( sqlite3_mutex_held(p->pBt->mutex) );
        46733  +  assert( sqlite3_mutex_held(pBt->mutex) );
 45175  46734     assert( sqlite3_mutex_held(p->db->mutex) );
 45176         -  assert( p->db==p->pBt->db );
        46735  +  assert( p->db==pBt->db );
 45177  46736   
 45178         -  sqlite3_mutex_leave(p->pBt->mutex);
        46737  +  sqlite3_mutex_leave(pBt->mutex);
 45179  46738     p->locked = 0;
 45180  46739   }
 45181  46740   
 45182  46741   /*
 45183  46742   ** Enter a mutex on the given BTree object.
 45184  46743   **
 45185  46744   ** If the object is not sharable, then no mutex is ever required
................................................................................
 45312  46871   ** Enter the mutexes in accending order by BtShared pointer address
 45313  46872   ** to avoid the possibility of deadlock when two threads with
 45314  46873   ** two or more btrees in common both try to lock all their btrees
 45315  46874   ** at the same instant.
 45316  46875   */
 45317  46876   SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
 45318  46877     int i;
 45319         -  Btree *p, *pLater;
        46878  +  Btree *p;
 45320  46879     assert( sqlite3_mutex_held(db->mutex) );
 45321  46880     for(i=0; i<db->nDb; i++){
 45322  46881       p = db->aDb[i].pBt;
 45323         -    assert( !p || (p->locked==0 && p->sharable) || p->pBt->db==p->db );
 45324         -    if( p && p->sharable ){
 45325         -      p->wantToLock++;
 45326         -      if( !p->locked ){
 45327         -        assert( p->wantToLock==1 );
 45328         -        while( p->pPrev ) p = p->pPrev;
 45329         -        /* Reason for ALWAYS:  There must be at least on unlocked Btree in
 45330         -        ** the chain.  Otherwise the !p->locked test above would have failed */
 45331         -        while( p->locked && ALWAYS(p->pNext) ) p = p->pNext;
 45332         -        for(pLater = p->pNext; pLater; pLater=pLater->pNext){
 45333         -          if( pLater->locked ){
 45334         -            unlockBtreeMutex(pLater);
 45335         -          }
 45336         -        }
 45337         -        while( p ){
 45338         -          lockBtreeMutex(p);
 45339         -          p = p->pNext;
 45340         -        }
 45341         -      }
 45342         -    }
        46882  +    if( p ) sqlite3BtreeEnter(p);
 45343  46883     }
 45344  46884   }
 45345  46885   SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
 45346  46886     int i;
 45347  46887     Btree *p;
 45348  46888     assert( sqlite3_mutex_held(db->mutex) );
 45349  46889     for(i=0; i<db->nDb; i++){
 45350  46890       p = db->aDb[i].pBt;
 45351         -    if( p && p->sharable ){
 45352         -      assert( p->wantToLock>0 );
 45353         -      p->wantToLock--;
 45354         -      if( p->wantToLock==0 ){
 45355         -        unlockBtreeMutex(p);
 45356         -      }
 45357         -    }
        46891  +    if( p ) sqlite3BtreeLeave(p);
 45358  46892     }
 45359  46893   }
        46894  +
        46895  +/*
        46896  +** Return true if a particular Btree requires a lock.  Return FALSE if
        46897  +** no lock is ever required since it is not sharable.
        46898  +*/
        46899  +SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
        46900  +  return p->sharable;
        46901  +}
 45360  46902   
 45361  46903   #ifndef NDEBUG
 45362  46904   /*
 45363  46905   ** Return true if the current thread holds the database connection
 45364  46906   ** mutex and all required BtShared mutexes.
 45365  46907   **
 45366  46908   ** This routine is used inside assert() statements only.
................................................................................
 45378  46920         return 0;
 45379  46921       }
 45380  46922     }
 45381  46923     return 1;
 45382  46924   }
 45383  46925   #endif /* NDEBUG */
 45384  46926   
 45385         -/*
 45386         -** Add a new Btree pointer to a BtreeMutexArray. 
 45387         -** if the pointer can possibly be shared with
 45388         -** another database connection.
 45389         -**
 45390         -** The pointers are kept in sorted order by pBtree->pBt.  That
 45391         -** way when we go to enter all the mutexes, we can enter them
 45392         -** in order without every having to backup and retry and without
 45393         -** worrying about deadlock.
 45394         -**
 45395         -** The number of shared btrees will always be small (usually 0 or 1)
 45396         -** so an insertion sort is an adequate algorithm here.
 45397         -*/
 45398         -SQLITE_PRIVATE void sqlite3BtreeMutexArrayInsert(BtreeMutexArray *pArray, Btree *pBtree){
 45399         -  int i, j;
 45400         -  BtShared *pBt;
 45401         -  if( pBtree==0 || pBtree->sharable==0 ) return;
 45402         -#ifndef NDEBUG
 45403         -  {
 45404         -    for(i=0; i<pArray->nMutex; i++){
 45405         -      assert( pArray->aBtree[i]!=pBtree );
 45406         -    }
 45407         -  }
 45408         -#endif
 45409         -  assert( pArray->nMutex>=0 );
 45410         -  assert( pArray->nMutex<ArraySize(pArray->aBtree)-1 );
 45411         -  pBt = pBtree->pBt;
 45412         -  for(i=0; i<pArray->nMutex; i++){
 45413         -    assert( pArray->aBtree[i]!=pBtree );
 45414         -    if( pArray->aBtree[i]->pBt>pBt ){
 45415         -      for(j=pArray->nMutex; j>i; j--){
 45416         -        pArray->aBtree[j] = pArray->aBtree[j-1];
 45417         -      }
 45418         -      pArray->aBtree[i] = pBtree;
 45419         -      pArray->nMutex++;
 45420         -      return;
 45421         -    }
 45422         -  }
 45423         -  pArray->aBtree[pArray->nMutex++] = pBtree;
 45424         -}
 45425         -
 45426         -/*
 45427         -** Enter the mutex of every btree in the array.  This routine is
 45428         -** called at the beginning of sqlite3VdbeExec().  The mutexes are
 45429         -** exited at the end of the same function.
 45430         -*/
 45431         -SQLITE_PRIVATE void sqlite3BtreeMutexArrayEnter(BtreeMutexArray *pArray){
 45432         -  int i;
 45433         -  for(i=0; i<pArray->nMutex; i++){
 45434         -    Btree *p = pArray->aBtree[i];
 45435         -    /* Some basic sanity checking */
 45436         -    assert( i==0 || pArray->aBtree[i-1]->pBt<p->pBt );
 45437         -    assert( !p->locked || p->wantToLock>0 );
 45438         -
 45439         -    /* We should already hold a lock on the database connection */
 45440         -    assert( sqlite3_mutex_held(p->db->mutex) );
 45441         -
 45442         -    /* The Btree is sharable because only sharable Btrees are entered
 45443         -    ** into the array in the first place. */
 45444         -    assert( p->sharable );
 45445         -
 45446         -    p->wantToLock++;
 45447         -    if( !p->locked ){
 45448         -      lockBtreeMutex(p);
 45449         -    }
 45450         -  }
 45451         -}
 45452         -
 45453         -/*
 45454         -** Leave the mutex of every btree in the group.
 45455         -*/
 45456         -SQLITE_PRIVATE void sqlite3BtreeMutexArrayLeave(BtreeMutexArray *pArray){
 45457         -  int i;
 45458         -  for(i=0; i<pArray->nMutex; i++){
 45459         -    Btree *p = pArray->aBtree[i];
 45460         -    /* Some basic sanity checking */
 45461         -    assert( i==0 || pArray->aBtree[i-1]->pBt<p->pBt );
 45462         -    assert( p->locked );
 45463         -    assert( p->wantToLock>0 );
 45464         -
 45465         -    /* We should already hold a lock on the database connection */
 45466         -    assert( sqlite3_mutex_held(p->db->mutex) );
 45467         -
 45468         -    p->wantToLock--;
 45469         -    if( p->wantToLock==0 ){
 45470         -      unlockBtreeMutex(p);
 45471         -    }
 45472         -  }
 45473         -}
 45474         -
 45475         -#else
        46927  +#ifndef NDEBUG
        46928  +/*
        46929  +** Return true if the correct mutexes are held for accessing the
        46930  +** db->aDb[iDb].pSchema structure.  The mutexes required for schema
        46931  +** access are:
        46932  +**
        46933  +**   (1) The mutex on db
        46934  +**   (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
        46935  +**
        46936  +** If pSchema is not NULL, then iDb is computed from pSchema and
        46937  +** db using sqlite3SchemaToIndex().
        46938  +*/
        46939  +SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
        46940  +  Btree *p;
        46941  +  assert( db!=0 );
        46942  +  if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
        46943  +  assert( iDb>=0 && iDb<db->nDb );
        46944  +  if( !sqlite3_mutex_held(db->mutex) ) return 0;
        46945  +  if( iDb==1 ) return 1;
        46946  +  p = db->aDb[iDb].pBt;
        46947  +  assert( p!=0 );
        46948  +  return p->sharable==0 || p->locked==1;
        46949  +}
        46950  +#endif /* NDEBUG */
        46951  +
        46952  +#else /* SQLITE_THREADSAFE>0 above.  SQLITE_THREADSAFE==0 below */
        46953  +/*
        46954  +** The following are special cases for mutex enter routines for use
        46955  +** in single threaded applications that use shared cache.  Except for
        46956  +** these two routines, all mutex operations are no-ops in that case and
        46957  +** are null #defines in btree.h.
        46958  +**
        46959  +** If shared cache is disabled, then all btree mutex routines, including
        46960  +** the ones below, are no-ops and are null #defines in btree.h.
        46961  +*/
        46962  +
 45476  46963   SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
 45477  46964     p->pBt->db = p->db;
 45478  46965   }
 45479  46966   SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
 45480  46967     int i;
 45481  46968     for(i=0; i<db->nDb; i++){
 45482  46969       Btree *p = db->aDb[i].pBt;
................................................................................
 46715  48202     ** and the cell content area.  The btreeInitPage() call has already
 46716  48203     ** validated the freelist.  Given that the freelist is valid, there
 46717  48204     ** is no way that the allocation can extend off the end of the page.
 46718  48205     ** The assert() below verifies the previous sentence.
 46719  48206     */
 46720  48207     top -= nByte;
 46721  48208     put2byte(&data[hdr+5], top);
 46722         -  assert( top+nByte <= pPage->pBt->usableSize );
        48209  +  assert( top+nByte <= (int)pPage->pBt->usableSize );
 46723  48210     *pIdx = top;
 46724  48211     return SQLITE_OK;
 46725  48212   }
 46726  48213   
 46727  48214   /*
 46728  48215   ** Return a section of the pPage->aData to the freelist.
 46729  48216   ** The first byte of the new free block is pPage->aDisk[start]
................................................................................
 46736  48223     int addr, pbegin, hdr;
 46737  48224     int iLast;                        /* Largest possible freeblock offset */
 46738  48225     unsigned char *data = pPage->aData;
 46739  48226   
 46740  48227     assert( pPage->pBt!=0 );
 46741  48228     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 46742  48229     assert( start>=pPage->hdrOffset+6+pPage->childPtrSize );
 46743         -  assert( (start + size)<=pPage->pBt->usableSize );
        48230  +  assert( (start + size) <= (int)pPage->pBt->usableSize );
 46744  48231     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 46745  48232     assert( size>=0 );   /* Minimum cell size is 4 */
 46746  48233   
 46747  48234     if( pPage->pBt->secureDelete ){
 46748  48235       /* Overwrite deleted information with zeros when the secure_delete
 46749  48236       ** option is enabled */
 46750  48237       memset(&data[start], 0, size);
................................................................................
 46779  48266     pPage->nFree = pPage->nFree + (u16)size;
 46780  48267   
 46781  48268     /* Coalesce adjacent free blocks */
 46782  48269     addr = hdr + 1;
 46783  48270     while( (pbegin = get2byte(&data[addr]))>0 ){
 46784  48271       int pnext, psize, x;
 46785  48272       assert( pbegin>addr );
 46786         -    assert( pbegin<=pPage->pBt->usableSize-4 );
        48273  +    assert( pbegin <= (int)pPage->pBt->usableSize-4 );
 46787  48274       pnext = get2byte(&data[pbegin]);
 46788  48275       psize = get2byte(&data[pbegin+2]);
 46789  48276       if( pbegin + psize + 3 >= pnext && pnext>0 ){
 46790  48277         int frag = pnext - (pbegin+psize);
 46791  48278         if( (frag<0) || (frag>(int)data[hdr+7]) ){
 46792  48279           return SQLITE_CORRUPT_BKPT;
 46793  48280         }
................................................................................
 47614  49101     sqlite3BtreeEnter(p);
 47615  49102     assert( pBt && pBt->pPager );
 47616  49103     rc = sqlite3PagerNosync(pBt->pPager);
 47617  49104     sqlite3BtreeLeave(p);
 47618  49105     return rc;
 47619  49106   }
 47620  49107   
 47621         -#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
 47622  49108   /*
 47623  49109   ** Change the default pages size and the number of reserved bytes per page.
 47624  49110   ** Or, if the page size has already been fixed, return SQLITE_READONLY 
 47625  49111   ** without changing anything.
 47626  49112   **
 47627  49113   ** The page size must be a power of 2 between 512 and 65536.  If the page
 47628  49114   ** size supplied does not meet this constraint then the page size is not
................................................................................
 47669  49155   /*
 47670  49156   ** Return the currently defined page size
 47671  49157   */
 47672  49158   SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
 47673  49159     return p->pBt->pageSize;
 47674  49160   }
 47675  49161   
        49162  +#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
 47676  49163   /*
 47677  49164   ** Return the number of bytes of space at the end of every page that
 47678  49165   ** are intentually left unused.  This is the "reserved" space that is
 47679  49166   ** sometimes used by extensions.
 47680  49167   */
 47681  49168   SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree *p){
 47682  49169     int n;
................................................................................
 47868  49355         pBt->usableSize = usableSize;
 47869  49356         pBt->pageSize = pageSize;
 47870  49357         freeTempSpace(pBt);
 47871  49358         rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
 47872  49359                                      pageSize-usableSize);
 47873  49360         return rc;
 47874  49361       }
 47875         -    if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPageHeader>nPageFile ){
        49362  +    if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){
 47876  49363         rc = SQLITE_CORRUPT_BKPT;
 47877  49364         goto page1_init_failed;
 47878  49365       }
 47879  49366       if( usableSize<480 ){
 47880  49367         goto page1_init_failed;
 47881  49368       }
 47882  49369       pBt->pageSize = pageSize;
................................................................................
 48646  50133   ** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
 48647  50134   ** be invoked prior to calling this routine.  The sqlite3BtreeCommitPhaseOne()
 48648  50135   ** routine did all the work of writing information out to disk and flushing the
 48649  50136   ** contents so that they are written onto the disk platter.  All this
 48650  50137   ** routine has to do is delete or truncate or zero the header in the
 48651  50138   ** the rollback journal (which causes the transaction to commit) and
 48652  50139   ** drop locks.
        50140  +**
        50141  +** Normally, if an error occurs while the pager layer is attempting to 
        50142  +** finalize the underlying journal file, this function returns an error and
        50143  +** the upper layer will attempt a rollback. However, if the second argument
        50144  +** is non-zero then this b-tree transaction is part of a multi-file 
        50145  +** transaction. In this case, the transaction has already been committed 
        50146  +** (by deleting a master journal file) and the caller will ignore this 
        50147  +** functions return code. So, even if an error occurs in the pager layer,
        50148  +** reset the b-tree objects internal state to indicate that the write
        50149  +** transaction has been closed. This is quite safe, as the pager will have
        50150  +** transitioned to the error state.
 48653  50151   **
 48654  50152   ** This will release the write lock on the database file.  If there
 48655  50153   ** are no active cursors, it also releases the read lock.
 48656  50154   */
 48657         -SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p){
        50155  +SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
 48658  50156   
 48659  50157     if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
 48660  50158     sqlite3BtreeEnter(p);
 48661  50159     btreeIntegrity(p);
 48662  50160   
 48663  50161     /* If the handle has a write-transaction open, commit the shared-btrees 
 48664  50162     ** transaction and set the shared state to TRANS_READ.
................................................................................
 48665  50163     */
 48666  50164     if( p->inTrans==TRANS_WRITE ){
 48667  50165       int rc;
 48668  50166       BtShared *pBt = p->pBt;
 48669  50167       assert( pBt->inTransaction==TRANS_WRITE );
 48670  50168       assert( pBt->nTransaction>0 );
 48671  50169       rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
 48672         -    if( rc!=SQLITE_OK ){
        50170  +    if( rc!=SQLITE_OK && bCleanup==0 ){
 48673  50171         sqlite3BtreeLeave(p);
 48674  50172         return rc;
 48675  50173       }
 48676  50174       pBt->inTransaction = TRANS_READ;
 48677  50175     }
 48678  50176   
 48679  50177     btreeEndTransaction(p);
................................................................................
 48685  50183   ** Do both phases of a commit.
 48686  50184   */
 48687  50185   SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
 48688  50186     int rc;
 48689  50187     sqlite3BtreeEnter(p);
 48690  50188     rc = sqlite3BtreeCommitPhaseOne(p, 0);
 48691  50189     if( rc==SQLITE_OK ){
 48692         -    rc = sqlite3BtreeCommitPhaseTwo(p);
        50190  +    rc = sqlite3BtreeCommitPhaseTwo(p, 0);
 48693  50191     }
 48694  50192     sqlite3BtreeLeave(p);
 48695  50193     return rc;
 48696  50194   }
 48697  50195   
 48698  50196   #ifndef NDEBUG
 48699  50197   /*
................................................................................
 50295  51793           rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
 50296  51794         }
 50297  51795         if( rc ){
 50298  51796           pTrunk = 0;
 50299  51797           goto end_allocate_page;
 50300  51798         }
 50301  51799   
 50302         -      k = get4byte(&pTrunk->aData[4]);
        51800  +      k = get4byte(&pTrunk->aData[4]); /* # of leaves on this trunk page */
 50303  51801         if( k==0 && !searchList ){
 50304  51802           /* The trunk has no leaves and the list is not being searched. 
 50305  51803           ** So extract the trunk page itself and use it as the newly 
 50306  51804           ** allocated page */
 50307  51805           assert( pPrevTrunk==0 );
 50308  51806           rc = sqlite3PagerWrite(pTrunk->pDbPage);
 50309  51807           if( rc ){
................................................................................
 50380  51878           TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
 50381  51879   #endif
 50382  51880         }else if( k>0 ){
 50383  51881           /* Extract a leaf from the trunk */
 50384  51882           u32 closest;
 50385  51883           Pgno iPage;
 50386  51884           unsigned char *aData = pTrunk->aData;
 50387         -        rc = sqlite3PagerWrite(pTrunk->pDbPage);
 50388         -        if( rc ){
 50389         -          goto end_allocate_page;
 50390         -        }
 50391  51885           if( nearby>0 ){
 50392  51886             u32 i;
 50393  51887             int dist;
 50394  51888             closest = 0;
 50395         -          dist = get4byte(&aData[8]) - nearby;
 50396         -          if( dist<0 ) dist = -dist;
        51889  +          dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
 50397  51890             for(i=1; i<k; i++){
 50398         -            int d2 = get4byte(&aData[8+i*4]) - nearby;
 50399         -            if( d2<0 ) d2 = -d2;
        51891  +            int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
 50400  51892               if( d2<dist ){
 50401  51893                 closest = i;
 50402  51894                 dist = d2;
 50403  51895               }
 50404  51896             }
 50405  51897           }else{
 50406  51898             closest = 0;
................................................................................
 50415  51907           testcase( iPage==mxPage );
 50416  51908           if( !searchList || iPage==nearby ){
 50417  51909             int noContent;
 50418  51910             *pPgno = iPage;
 50419  51911             TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
 50420  51912                    ": %d more free pages\n",
 50421  51913                    *pPgno, closest+1, k, pTrunk->pgno, n-1));
        51914  +          rc = sqlite3PagerWrite(pTrunk->pDbPage);
        51915  +          if( rc ) goto end_allocate_page;
 50422  51916             if( closest<k-1 ){
 50423  51917               memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
 50424  51918             }
 50425  51919             put4byte(&aData[4], k-1);
 50426         -          assert( sqlite3PagerIswriteable(pTrunk->pDbPage) );
 50427  51920             noContent = !btreeGetHasContent(pBt, *pPgno);
 50428  51921             rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
 50429  51922             if( rc==SQLITE_OK ){
 50430  51923               rc = sqlite3PagerWrite((*ppPage)->pDbPage);
 50431  51924               if( rc!=SQLITE_OK ){
 50432  51925                 releasePage(*ppPage);
 50433  51926               }
................................................................................
 50488  51981         releasePage(*ppPage);
 50489  51982         return SQLITE_CORRUPT_BKPT;
 50490  51983       }
 50491  51984       (*ppPage)->isInit = 0;
 50492  51985     }else{
 50493  51986       *ppPage = 0;
 50494  51987     }
        51988  +  assert( rc!=SQLITE_OK || sqlite3PagerIswriteable((*ppPage)->pDbPage) );
 50495  51989     return rc;
 50496  51990   }
 50497  51991   
 50498  51992   /*
 50499  51993   ** This function is used to add page iPage to the database file free-list. 
 50500  51994   ** It is assumed that the page is not already a part of the free-list.
 50501  51995   **
................................................................................
 50977  52471       end = cellOffset + 2*pPage->nCell;
 50978  52472       ins = cellOffset + 2*i;
 50979  52473       rc = allocateSpace(pPage, sz, &idx);
 50980  52474       if( rc ){ *pRC = rc; return; }
 50981  52475       /* The allocateSpace() routine guarantees the following two properties
 50982  52476       ** if it returns success */
 50983  52477       assert( idx >= end+2 );
 50984         -    assert( idx+sz <= pPage->pBt->usableSize );
        52478  +    assert( idx+sz <= (int)pPage->pBt->usableSize );
 50985  52479       pPage->nCell++;
 50986  52480       pPage->nFree -= (u16)(2 + sz);
 50987  52481       memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
 50988  52482       if( iChild ){
 50989  52483         put4byte(&data[idx], iChild);
 50990  52484       }
 50991  52485       for(j=end, ptr=&data[j]; j>ins; j-=2, ptr-=2){
................................................................................
 51020  52514     int cellbody;     /* Address of next cell body */
 51021  52515     u8 * const data = pPage->aData;             /* Pointer to data for pPage */
 51022  52516     const int hdr = pPage->hdrOffset;           /* Offset of header on pPage */
 51023  52517     const int nUsable = pPage->pBt->usableSize; /* Usable size of page */
 51024  52518   
 51025  52519     assert( pPage->nOverflow==0 );
 51026  52520     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 51027         -  assert( nCell>=0 && nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921);
        52521  +  assert( nCell>=0 && nCell<=(int)MX_CELL(pPage->pBt)
        52522  +            && (int)MX_CELL(pPage->pBt)<=10921);
 51028  52523     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 51029  52524   
 51030  52525     /* Check that the page has just been zeroed by zeroPage() */
 51031  52526     assert( pPage->nCell==0 );
 51032  52527     assert( get2byteNotZero(&data[hdr+5])==nUsable );
 51033  52528   
 51034  52529     pCellptr = &data[pPage->cellOffset + nCell*2];
................................................................................
 51234  52729       int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
 51235  52730       int rc;
 51236  52731       int iData;
 51237  52732     
 51238  52733     
 51239  52734       assert( pFrom->isInit );
 51240  52735       assert( pFrom->nFree>=iToHdr );
 51241         -    assert( get2byte(&aFrom[iFromHdr+5])<=pBt->usableSize );
        52736  +    assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
 51242  52737     
 51243  52738       /* Copy the b-tree node content from page pFrom to page pTo. */
 51244  52739       iData = get2byte(&aFrom[iFromHdr+5]);
 51245  52740       memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
 51246  52741       memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
 51247  52742     
 51248  52743       /* Reinitialize page pTo so that the contents of the MemPage structure
................................................................................
 51501  52996         u16 sz = (u16)szNew[i];
 51502  52997         u8 *pTemp;
 51503  52998         assert( nCell<nMaxCells );
 51504  52999         szCell[nCell] = sz;
 51505  53000         pTemp = &aSpace1[iSpace1];
 51506  53001         iSpace1 += sz;
 51507  53002         assert( sz<=pBt->maxLocal+23 );
 51508         -      assert( iSpace1<=pBt->pageSize );
        53003  +      assert( iSpace1 <= (int)pBt->pageSize );
 51509  53004         memcpy(pTemp, apDiv[i], sz);
 51510  53005         apCell[nCell] = pTemp+leafCorrection;
 51511  53006         assert( leafCorrection==0 || leafCorrection==4 );
 51512  53007         szCell[nCell] = szCell[nCell] - leafCorrection;
 51513  53008         if( !pOld->leaf ){
 51514  53009           assert( leafCorrection==0 );
 51515  53010           assert( pOld->hdrOffset==0 );
................................................................................
 51666  53161       for(j=i+1; j<k; j++){
 51667  53162         if( apNew[j]->pgno<(unsigned)minV ){
 51668  53163           minI = j;
 51669  53164           minV = apNew[j]->pgno;
 51670  53165         }
 51671  53166       }
 51672  53167       if( minI>i ){
 51673         -      int t;
 51674  53168         MemPage *pT;
 51675         -      t = apNew[i]->pgno;
 51676  53169         pT = apNew[i];
 51677  53170         apNew[i] = apNew[minI];
 51678  53171         apNew[minI] = pT;
 51679  53172       }
 51680  53173     }
 51681  53174     TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
 51682  53175       apNew[0]->pgno, szNew[0],
................................................................................
 51747  53240           if( szCell[j]==4 ){
 51748  53241             assert(leafCorrection==4);
 51749  53242             sz = cellSizePtr(pParent, pCell);
 51750  53243           }
 51751  53244         }
 51752  53245         iOvflSpace += sz;
 51753  53246         assert( sz<=pBt->maxLocal+23 );
 51754         -      assert( iOvflSpace<=pBt->pageSize );
        53247  +      assert( iOvflSpace <= (int)pBt->pageSize );
 51755  53248         insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno, &rc);
 51756  53249         if( rc!=SQLITE_OK ) goto balance_cleanup;
 51757  53250         assert( sqlite3PagerIswriteable(pParent->pDbPage) );
 51758  53251   
 51759  53252         j++;
 51760  53253         nxDiv++;
 51761  53254       }
................................................................................
 52192  53685     assert( pPage->isInit );
 52193  53686     allocateTempSpace(pBt);
 52194  53687     newCell = pBt->pTmpSpace;
 52195  53688     if( newCell==0 ) return SQLITE_NOMEM;
 52196  53689     rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
 52197  53690     if( rc ) goto end_insert;
 52198  53691     assert( szNew==cellSizePtr(pPage, newCell) );
 52199         -  assert( szNew<=MX_CELL_SIZE(pBt) );
        53692  +  assert( szNew <= MX_CELL_SIZE(pBt) );
 52200  53693     idx = pCur->aiIdx[pCur->iPage];
 52201  53694     if( loc==0 ){
 52202  53695       u16 szOld;
 52203  53696       assert( idx<pPage->nCell );
 52204  53697       rc = sqlite3PagerWrite(pPage->pDbPage);
 52205  53698       if( rc ){
 52206  53699         goto end_insert;
................................................................................
 52332  53825       MemPage *pLeaf = pCur->apPage[pCur->iPage];
 52333  53826       int nCell;
 52334  53827       Pgno n = pCur->apPage[iCellDepth+1]->pgno;
 52335  53828       unsigned char *pTmp;
 52336  53829   
 52337  53830       pCell = findCell(pLeaf, pLeaf->nCell-1);
 52338  53831       nCell = cellSizePtr(pLeaf, pCell);
 52339         -    assert( MX_CELL_SIZE(pBt)>=nCell );
        53832  +    assert( MX_CELL_SIZE(pBt) >= nCell );
 52340  53833   
 52341  53834       allocateTempSpace(pBt);
 52342  53835       pTmp = pBt->pTmpSpace;
 52343  53836   
 52344  53837       rc = sqlite3PagerWrite(pLeaf->pDbPage);
 52345  53838       insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
 52346  53839       dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
................................................................................
 53419  54912   
 53420  54913   #ifndef SQLITE_OMIT_WAL
 53421  54914   /*
 53422  54915   ** Run a checkpoint on the Btree passed as the first argument.
 53423  54916   **
 53424  54917   ** Return SQLITE_LOCKED if this or any other connection has an open 
 53425  54918   ** transaction on the shared-cache the argument Btree is connected to.
        54919  +**
        54920  +** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
 53426  54921   */
 53427         -SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p){
        54922  +SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
 53428  54923     int rc = SQLITE_OK;
 53429  54924     if( p ){
 53430  54925       BtShared *pBt = p->pBt;
 53431  54926       sqlite3BtreeEnter(p);
 53432  54927       if( pBt->inTransaction!=TRANS_NONE ){
 53433  54928         rc = SQLITE_LOCKED;
 53434  54929       }else{
 53435         -      rc = sqlite3PagerCheckpoint(pBt->pPager);
        54930  +      rc = sqlite3PagerCheckpoint(pBt->pPager, eMode, pnLog, pnCkpt);
 53436  54931       }
 53437  54932       sqlite3BtreeLeave(p);
 53438  54933     }
 53439  54934     return rc;
 53440  54935   }
 53441  54936   #endif
 53442  54937   
................................................................................
 53468  54963   **
 53469  54964   ** If the nBytes parameter is 0 and the blob of memory has not yet been
 53470  54965   ** allocated, a null pointer is returned. If the blob has already been
 53471  54966   ** allocated, it is returned as normal.
 53472  54967   **
 53473  54968   ** Just before the shared-btree is closed, the function passed as the 
 53474  54969   ** xFree argument when the memory allocation was made is invoked on the 
 53475         -** blob of allocated memory. This function should not call sqlite3_free()
        54970  +** blob of allocated memory. The xFree function should not call sqlite3_free()
 53476  54971   ** on the memory, the btree layer does that.
 53477  54972   */
 53478  54973   SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
 53479  54974     BtShared *pBt = p->pBt;
 53480  54975     sqlite3BtreeEnter(p);
 53481  54976     if( !pBt->pSchema && nBytes ){
 53482  54977       pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
................................................................................
 53842  55337   */
 53843  55338   static int backupOnePage(sqlite3_backup *p, Pgno iSrcPg, const u8 *zSrcData){
 53844  55339     Pager * const pDestPager = sqlite3BtreePager(p->pDest);
 53845  55340     const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
 53846  55341     int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
 53847  55342     const int nCopy = MIN(nSrcPgsz, nDestPgsz);
 53848  55343     const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
        55344  +#ifdef SQLITE_HAS_CODEC
        55345  +  int nSrcReserve = sqlite3BtreeGetReserve(p->pSrc);
        55346  +  int nDestReserve = sqlite3BtreeGetReserve(p->pDest);
        55347  +#endif
 53849  55348   
 53850  55349     int rc = SQLITE_OK;
 53851  55350     i64 iOff;
 53852  55351   
 53853  55352     assert( p->bDestLocked );
 53854  55353     assert( !isFatalError(p->rc) );
 53855  55354     assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
................................................................................
 53860  55359     */
 53861  55360     if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
 53862  55361       rc = SQLITE_READONLY;
 53863  55362     }
 53864  55363   
 53865  55364   #ifdef SQLITE_HAS_CODEC
 53866  55365     /* Backup is not possible if the page size of the destination is changing
 53867         -  ** a a codec is in use.
        55366  +  ** and a codec is in use.
 53868  55367     */
 53869  55368     if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
 53870  55369       rc = SQLITE_READONLY;
 53871  55370     }
        55371  +
        55372  +  /* Backup is not possible if the number of bytes of reserve space differ
        55373  +  ** between source and destination.  If there is a difference, try to
        55374  +  ** fix the destination to agree with the source.  If that is not possible,
        55375  +  ** then the backup cannot proceed.
        55376  +  */
        55377  +  if( nSrcReserve!=nDestReserve ){
        55378  +    u32 newPgsz = nSrcPgsz;
        55379  +    rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
        55380  +    if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
        55381  +  }
 53872  55382   #endif
 53873  55383   
 53874  55384     /* This loop runs once for each destination page spanned by the source 
 53875  55385     ** page. For each iteration, variable iOff is set to the byte offset
 53876  55386     ** of the destination page.
 53877  55387     */
 53878  55388     for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
................................................................................
 54024  55534       */
 54025  55535       if( rc==SQLITE_DONE 
 54026  55536        && (rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1))==SQLITE_OK
 54027  55537       ){
 54028  55538         int nDestTruncate;
 54029  55539     
 54030  55540         if( p->pDestDb ){
 54031         -        sqlite3ResetInternalSchema(p->pDestDb, 0);
        55541  +        sqlite3ResetInternalSchema(p->pDestDb, -1);
 54032  55542         }
 54033  55543   
 54034  55544         /* Set nDestTruncate to the final number of pages in the destination
 54035  55545         ** database. The complication here is that the destination page
 54036  55546         ** size may be different to the source page size. 
 54037  55547         **
 54038  55548         ** If the source page size is smaller than the destination page size, 
................................................................................
 54111  55621           }
 54112  55622         }else{
 54113  55623           rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
 54114  55624         }
 54115  55625     
 54116  55626         /* Finish committing the transaction to the destination database. */
 54117  55627         if( SQLITE_OK==rc
 54118         -       && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest))
        55628  +       && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
 54119  55629         ){
 54120  55630           rc = SQLITE_DONE;
 54121  55631         }
 54122  55632       }
 54123  55633     
 54124  55634       /* If bCloseTrans is true, then this function opened a read transaction
 54125  55635       ** on the source database. Close the read transaction here. There is
 54126  55636       ** no need to check the return values of the btree methods here, as
 54127  55637       ** "committing" a read-only transaction cannot fail.
 54128  55638       */
 54129  55639       if( bCloseTrans ){
 54130  55640         TESTONLY( int rc2 );
 54131  55641         TESTONLY( rc2  = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
 54132         -      TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc);
        55642  +      TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
 54133  55643         assert( rc2==SQLITE_OK );
 54134  55644       }
 54135  55645     
 54136  55646       if( rc==SQLITE_IOERR_NOMEM ){
 54137  55647         rc = SQLITE_NOMEM;
 54138  55648       }
 54139  55649       p->rc = rc;
................................................................................
 54230  55740     for(p=pBackup; p; p=p->pNext){
 54231  55741       assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
 54232  55742       if( !isFatalError(p->rc) && iPage<p->iNext ){
 54233  55743         /* The backup process p has already copied page iPage. But now it
 54234  55744         ** has been modified by a transaction on the source pager. Copy
 54235  55745         ** the new data into the backup.
 54236  55746         */
 54237         -      int rc = backupOnePage(p, iPage, aData);
        55747  +      int rc;
        55748  +      assert( p->pDestDb );
        55749  +      sqlite3_mutex_enter(p->pDestDb->mutex);
        55750  +      rc = backupOnePage(p, iPage, aData);
        55751  +      sqlite3_mutex_leave(p->pDestDb->mutex);
 54238  55752         assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
 54239  55753         if( rc!=SQLITE_OK ){
 54240  55754           p->rc = rc;
 54241  55755         }
 54242  55756       }
 54243  55757     }
 54244  55758   }
................................................................................
 54673  56187     assert( EIGHT_BYTE_ALIGNMENT(pMem) );
 54674  56188     flags = pMem->flags;
 54675  56189     if( flags & MEM_Int ){
 54676  56190       return pMem->u.i;
 54677  56191     }else if( flags & MEM_Real ){
 54678  56192       return doubleToInt64(pMem->r);
 54679  56193     }else if( flags & (MEM_Str|MEM_Blob) ){
 54680         -    i64 value;
        56194  +    i64 value = 0;
 54681  56195       assert( pMem->z || pMem->n==0 );
 54682  56196       testcase( pMem->z==0 );
 54683  56197       sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
 54684  56198       return value;
 54685  56199     }else{
 54686  56200       return 0;
 54687  56201     }
................................................................................
 55383  56897       if( enc!=SQLITE_UTF8 ){
 55384  56898         sqlite3VdbeChangeEncoding(pVal, enc);
 55385  56899       }
 55386  56900     }else if( op==TK_UMINUS ) {
 55387  56901       /* This branch happens for multiple negative signs.  Ex: -(-5) */
 55388  56902       if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
 55389  56903         sqlite3VdbeMemNumerify(pVal);
 55390         -      pVal->u.i = -1 * pVal->u.i;
 55391         -      /* (double)-1 In case of SQLITE_OMIT_FLOATING_POINT... */
 55392         -      pVal->r = (double)-1 * pVal->r;
        56904  +      if( pVal->u.i==SMALLEST_INT64 ){
        56905  +        pVal->flags &= MEM_Int;
        56906  +        pVal->flags |= MEM_Real;
        56907  +        pVal->r = (double)LARGEST_INT64;
        56908  +      }else{
        56909  +        pVal->u.i = -pVal->u.i;
        56910  +      }
        56911  +      pVal->r = -pVal->r;
 55393  56912         sqlite3ValueApplyAffinity(pVal, affinity, enc);
 55394  56913       }
        56914  +  }else if( op==TK_NULL ){
        56915  +    pVal = sqlite3ValueNew(db);
        56916  +    if( pVal==0 ) goto no_mem;
 55395  56917     }
 55396  56918   #ifndef SQLITE_OMIT_BLOB_LITERAL
 55397  56919     else if( op==TK_BLOB ){
 55398  56920       int nVal;
 55399  56921       assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
 55400  56922       assert( pExpr->u.zToken[1]=='\'' );
 55401  56923       pVal = sqlite3ValueNew(db);
................................................................................
 55615  57137     pOp->p5 = 0;
 55616  57138     pOp->p1 = p1;
 55617  57139     pOp->p2 = p2;
 55618  57140     pOp->p3 = p3;
 55619  57141     pOp->p4.p = 0;
 55620  57142     pOp->p4type = P4_NOTUSED;
 55621  57143     p->expired = 0;
        57144  +  if( op==OP_ParseSchema ){
        57145  +    /* Any program that uses the OP_ParseSchema opcode needs to lock
        57146  +    ** all btrees. */
        57147  +    int j;
        57148  +    for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
        57149  +  }
 55622  57150   #ifdef SQLITE_DEBUG
 55623  57151     pOp->zComment = 0;
 55624  57152     if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
 55625  57153   #endif
 55626  57154   #ifdef VDBE_PROFILE
 55627  57155     pOp->cycles = 0;
 55628  57156     pOp->cnt = 0;
................................................................................
 55915  57443   ** returned program.
 55916  57444   */
 55917  57445   SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
 55918  57446     VdbeOp *aOp = p->aOp;
 55919  57447     assert( aOp && !p->db->mallocFailed );
 55920  57448   
 55921  57449     /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
 55922         -  assert( p->aMutex.nMutex==0 );
        57450  +  assert( p->btreeMask==0 );
 55923  57451   
 55924  57452     resolveP2Values(p, pnMaxArg);
 55925  57453     *pnOp = p->nOp;
 55926  57454     p->aOp = 0;
 55927  57455     return aOp;
 55928  57456   }
 55929  57457   
................................................................................
 56017  57545   }
 56018  57546   
 56019  57547   /*
 56020  57548   ** Change the P2 operand of instruction addr so that it points to
 56021  57549   ** the address of the next instruction to be coded.
 56022  57550   */
 56023  57551   SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
        57552  +  assert( addr>=0 );
 56024  57553     sqlite3VdbeChangeP2(p, addr, p->nOp);
 56025  57554   }
 56026  57555   
 56027  57556   
 56028  57557   /*
 56029  57558   ** If the input FuncDef structure is ephemeral, then free it.  If
 56030  57559   ** the FuncDef is not ephermal, then do nothing.
................................................................................
 56402  57931     return zP4;
 56403  57932   }
 56404  57933   #endif
 56405  57934   
 56406  57935   /*
 56407  57936   ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
 56408  57937   **
 56409         -** The prepared statement has to know in advance which Btree objects
 56410         -** will be used so that it can acquire mutexes on them all in sorted
 56411         -** order (via sqlite3VdbeMutexArrayEnter().  Mutexes are acquired
 56412         -** in order (and released in reverse order) to avoid deadlocks.
        57938  +** The prepared statements need to know in advance the complete set of
        57939  +** attached databases that they will be using.  A mask of these databases
        57940  +** is maintained in p->btreeMask and is used for locking and other purposes.
 56413  57941   */
 56414  57942   SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
 56415         -  int mask;
 56416         -  assert( i>=0 && i<p->db->nDb && i<sizeof(u32)*8 );
        57943  +  assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
 56417  57944     assert( i<(int)sizeof(p->btreeMask)*8 );
 56418         -  mask = ((u32)1)<<i;
 56419         -  if( (p->btreeMask & mask)==0 ){
 56420         -    p->btreeMask |= mask;
 56421         -    sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt);
 56422         -  }
 56423         -}
 56424         -
        57945  +  p->btreeMask |= ((yDbMask)1)<<i;
        57946  +  if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
        57947  +    p->lockMask |= ((yDbMask)1)<<i;
        57948  +  }
        57949  +}
        57950  +
        57951  +#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
        57952  +/*
        57953  +** If SQLite is compiled to support shared-cache mode and to be threadsafe,
        57954  +** this routine obtains the mutex associated with each BtShared structure
        57955  +** that may be accessed by the VM passed as an argument. In doing so it also
        57956  +** sets the BtShared.db member of each of the BtShared structures, ensuring
        57957  +** that the correct busy-handler callback is invoked if required.
        57958  +**
        57959  +** If SQLite is not threadsafe but does support shared-cache mode, then
        57960  +** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
        57961  +** of all of BtShared structures accessible via the database handle 
        57962  +** associated with the VM.
        57963  +**
        57964  +** If SQLite is not threadsafe and does not support shared-cache mode, this
        57965  +** function is a no-op.
        57966  +**
        57967  +** The p->btreeMask field is a bitmask of all btrees that the prepared 
        57968  +** statement p will ever use.  Let N be the number of bits in p->btreeMask
        57969  +** corresponding to btrees that use shared cache.  Then the runtime of
        57970  +** this routine is N*N.  But as N is rarely more than 1, this should not
        57971  +** be a problem.
        57972  +*/
        57973  +SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
        57974  +  int i;
        57975  +  yDbMask mask;
        57976  +  sqlite3 *db;
        57977  +  Db *aDb;
        57978  +  int nDb;
        57979  +  if( p->lockMask==0 ) return;  /* The common case */
        57980  +  db = p->db;
        57981  +  aDb = db->aDb;
        57982  +  nDb = db->nDb;
        57983  +  for(i=0, mask=1; i<nDb; i++, mask += mask){
        57984  +    if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
        57985  +      sqlite3BtreeEnter(aDb[i].pBt);
        57986  +    }
        57987  +  }
        57988  +}
        57989  +#endif
        57990  +
        57991  +#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
        57992  +/*
        57993  +** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
        57994  +*/
        57995  +SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
        57996  +  int i;
        57997  +  yDbMask mask;
        57998  +  sqlite3 *db;
        57999  +  Db *aDb;
        58000  +  int nDb;
        58001  +  if( p->lockMask==0 ) return;  /* The common case */
        58002  +  db = p->db;
        58003  +  aDb = db->aDb;
        58004  +  nDb = db->nDb;
        58005  +  for(i=0, mask=1; i<nDb; i++, mask += mask){
        58006  +    if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
        58007  +      sqlite3BtreeLeave(aDb[i].pBt);
        58008  +    }
        58009  +  }
        58010  +}
        58011  +#endif
 56425  58012   
 56426  58013   #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
 56427  58014   /*
 56428  58015   ** Print a single opcode.  This routine is used for debugging only.
 56429  58016   */
 56430  58017   SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
 56431  58018     char *zP4;
................................................................................
 56975  58562   ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory 
 56976  58563   ** cell array. This is necessary as the memory cell array may contain
 56977  58564   ** pointers to VdbeFrame objects, which may in turn contain pointers to
 56978  58565   ** open cursors.
 56979  58566   */
 56980  58567   static void closeAllCursors(Vdbe *p){
 56981  58568     if( p->pFrame ){
 56982         -    VdbeFrame *pFrame = p->pFrame;
        58569  +    VdbeFrame *pFrame;
 56983  58570       for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
 56984  58571       sqlite3VdbeFrameRestore(pFrame);
 56985  58572     }
 56986  58573     p->pFrame = 0;
 56987  58574     p->nFrame = 0;
 56988  58575   
 56989  58576     if( p->apCsr ){
................................................................................
 57161  58748       ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
 57162  58749       ** IO error while deleting or truncating a journal file. It is unlikely,
 57163  58750       ** but could happen. In this case abandon processing and return the error.
 57164  58751       */
 57165  58752       for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
 57166  58753         Btree *pBt = db->aDb[i].pBt;
 57167  58754         if( pBt ){
 57168         -        rc = sqlite3BtreeCommitPhaseTwo(pBt);
        58755  +        rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
 57169  58756         }
 57170  58757       }
 57171  58758       if( rc==SQLITE_OK ){
 57172  58759         sqlite3VtabCommit(db);
 57173  58760       }
 57174  58761     }
 57175  58762   
................................................................................
 57293  58880       ** may be lying around. Returning an error code won't help matters.
 57294  58881       */
 57295  58882       disable_simulated_io_errors();
 57296  58883       sqlite3BeginBenignMalloc();
 57297  58884       for(i=0; i<db->nDb; i++){ 
 57298  58885         Btree *pBt = db->aDb[i].pBt;
 57299  58886         if( pBt ){
 57300         -        sqlite3BtreeCommitPhaseTwo(pBt);
        58887  +        sqlite3BtreeCommitPhaseTwo(pBt, 1);
 57301  58888         }
 57302  58889       }
 57303  58890       sqlite3EndBenignMalloc();
 57304  58891       enable_simulated_io_errors();
 57305  58892   
 57306  58893       sqlite3VtabCommit(db);
 57307  58894     }
................................................................................
 57416  59003       if( eOp==SAVEPOINT_ROLLBACK ){
 57417  59004         db->nDeferredCons = p->nStmtDefCons;
 57418  59005       }
 57419  59006     }
 57420  59007     return rc;
 57421  59008   }
 57422  59009   
 57423         -/*
 57424         -** If SQLite is compiled to support shared-cache mode and to be threadsafe,
 57425         -** this routine obtains the mutex associated with each BtShared structure
 57426         -** that may be accessed by the VM passed as an argument. In doing so it
 57427         -** sets the BtShared.db member of each of the BtShared structures, ensuring
 57428         -** that the correct busy-handler callback is invoked if required.
 57429         -**
 57430         -** If SQLite is not threadsafe but does support shared-cache mode, then
 57431         -** sqlite3BtreeEnterAll() is invoked to set the BtShared.db variables
 57432         -** of all of BtShared structures accessible via the database handle 
 57433         -** associated with the VM. Of course only a subset of these structures
 57434         -** will be accessed by the VM, and we could use Vdbe.btreeMask to figure
 57435         -** that subset out, but there is no advantage to doing so.
 57436         -**
 57437         -** If SQLite is not threadsafe and does not support shared-cache mode, this
 57438         -** function is a no-op.
 57439         -*/
 57440         -#ifndef SQLITE_OMIT_SHARED_CACHE
 57441         -SQLITE_PRIVATE void sqlite3VdbeMutexArrayEnter(Vdbe *p){
 57442         -#if SQLITE_THREADSAFE
 57443         -  sqlite3BtreeMutexArrayEnter(&p->aMutex);
 57444         -#else
 57445         -  sqlite3BtreeEnterAll(p->db);
 57446         -#endif
 57447         -}
 57448         -#endif
 57449         -
 57450  59010   /*
 57451  59011   ** This function is called when a transaction opened by the database 
 57452  59012   ** handle associated with the VM passed as an argument is about to be 
 57453  59013   ** committed. If there are outstanding deferred foreign key constraint
 57454  59014   ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
 57455  59015   **
 57456  59016   ** If there are outstanding FK violations and this function returns 
................................................................................
 57515  59075     /* No commit or rollback needed if the program never started */
 57516  59076     if( p->pc>=0 ){
 57517  59077       int mrc;   /* Primary error code from p->rc */
 57518  59078       int eStatementOp = 0;
 57519  59079       int isSpecialError;            /* Set to true if a 'special' error */
 57520  59080   
 57521  59081       /* Lock all btrees used by the statement */
 57522         -    sqlite3VdbeMutexArrayEnter(p);
        59082  +    sqlite3VdbeEnter(p);
 57523  59083   
 57524  59084       /* Check for one of the special errors */
 57525  59085       mrc = p->rc & 0xff;
 57526  59086       assert( p->rc!=SQLITE_IOERR_BLOCKED );  /* This error no longer exists */
 57527  59087       isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
 57528  59088                        || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
 57529  59089       if( isSpecialError ){
................................................................................
 57566  59126       ** above has occurred. 
 57567  59127       */
 57568  59128       if( !sqlite3VtabInSync(db) 
 57569  59129        && db->autoCommit 
 57570  59130        && db->writeVdbeCnt==(p->readOnly==0) 
 57571  59131       ){
 57572  59132         if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
 57573         -        if( sqlite3VdbeCheckFk(p, 1) ){
 57574         -          sqlite3BtreeMutexArrayLeave(&p->aMutex);
 57575         -          return SQLITE_ERROR;
        59133  +        rc = sqlite3VdbeCheckFk(p, 1);
        59134  +        if( rc!=SQLITE_OK ){
        59135  +          if( NEVER(p->readOnly) ){
        59136  +            sqlite3VdbeLeave(p);
        59137  +            return SQLITE_ERROR;
        59138  +          }
        59139  +          rc = SQLITE_CONSTRAINT;
        59140  +        }else{ 
        59141  +          /* The auto-commit flag is true, the vdbe program was successful 
        59142  +          ** or hit an 'OR FAIL' constraint and there are no deferred foreign
        59143  +          ** key constraints to hold up the transaction. This means a commit 
        59144  +          ** is required. */
        59145  +          rc = vdbeCommit(db, p);
 57576  59146           }
 57577         -        /* The auto-commit flag is true, the vdbe program was successful 
 57578         -        ** or hit an 'OR FAIL' constraint and there are no deferred foreign
 57579         -        ** key constraints to hold up the transaction. This means a commit 
 57580         -        ** is required.  */
 57581         -        rc = vdbeCommit(db, p);
 57582         -        if( rc==SQLITE_BUSY ){
 57583         -          sqlite3BtreeMutexArrayLeave(&p->aMutex);
        59147  +        if( rc==SQLITE_BUSY && p->readOnly ){
        59148  +          sqlite3VdbeLeave(p);
 57584  59149             return SQLITE_BUSY;
 57585  59150           }else if( rc!=SQLITE_OK ){
 57586  59151             p->rc = rc;
 57587  59152             sqlite3RollbackAll(db);
 57588  59153           }else{
 57589  59154             db->nDeferredCons = 0;
 57590  59155             sqlite3CommitInternalChanges(db);
................................................................................
 57643  59208           sqlite3VdbeSetChanges(db, 0);
 57644  59209         }
 57645  59210         p->nChange = 0;
 57646  59211       }
 57647  59212     
 57648  59213       /* Rollback or commit any schema changes that occurred. */
 57649  59214       if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
 57650         -      sqlite3ResetInternalSchema(db, 0);
        59215  +      sqlite3ResetInternalSchema(db, -1);
 57651  59216         db->flags = (db->flags | SQLITE_InternChanges);
 57652  59217       }
 57653  59218   
 57654  59219       /* Release the locks */
 57655         -    sqlite3BtreeMutexArrayLeave(&p->aMutex);
        59220  +    sqlite3VdbeLeave(p);
 57656  59221     }
 57657  59222   
 57658  59223     /* We have successfully halted and closed the VM.  Record this fact. */
 57659  59224     if( p->pc>=0 ){
 57660  59225       db->activeVdbeCnt--;
 57661  59226       if( !p->readOnly ){
 57662  59227         db->writeVdbeCnt--;
................................................................................
 57674  59239     ** to invoke any required unlock-notify callbacks.
 57675  59240     */
 57676  59241     if( db->autoCommit ){
 57677  59242       sqlite3ConnectionUnlocked(db);
 57678  59243     }
 57679  59244   
 57680  59245     assert( db->activeVdbeCnt>0 || db->autoCommit==0 || db->nStatement==0 );
 57681         -  return SQLITE_OK;
        59246  +  return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
 57682  59247   }
 57683  59248   
 57684  59249   
 57685  59250   /*
 57686  59251   ** Each VDBE holds the result of the most recent sqlite3_step() call
 57687  59252   ** in p->rc.  This routine sets that result back to SQLITE_OK.
 57688  59253   */
................................................................................
 57950  59515       /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
 57951  59516   #   define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
 57952  59517       i64 i = pMem->u.i;
 57953  59518       u64 u;
 57954  59519       if( file_format>=4 && (i&1)==i ){
 57955  59520         return 8+(u32)i;
 57956  59521       }
 57957         -    u = i<0 ? -i : i;
        59522  +    if( i<0 ){
        59523  +      if( i<(-MAX_6BYTE) ) return 6;
        59524  +      /* Previous test prevents:  u = -(-9223372036854775808) */
        59525  +      u = -i;
        59526  +    }else{
        59527  +      u = i;
        59528  +    }
 57958  59529       if( u<=127 ) return 1;
 57959  59530       if( u<=32767 ) return 2;
 57960  59531       if( u<=8388607 ) return 3;
 57961  59532       if( u<=2147483647 ) return 4;
 57962  59533       if( u<=MAX_6BYTE ) return 5;
 57963  59534       return 6;
 57964  59535     }
................................................................................
 59300  60871   ** Check to see if column iCol of the given statement is valid.  If
 59301  60872   ** it is, return a pointer to the Mem for the value of that column.
 59302  60873   ** If iCol is not valid, return a pointer to a Mem which has a value
 59303  60874   ** of NULL.
 59304  60875   */
 59305  60876   static Mem *columnMem(sqlite3_stmt *pStmt, int i){
 59306  60877     Vdbe *pVm;
 59307         -  int vals;
 59308  60878     Mem *pOut;
 59309  60879   
 59310  60880     pVm = (Vdbe *)pStmt;
 59311  60881     if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
 59312  60882       sqlite3_mutex_enter(pVm->db->mutex);
 59313         -    vals = sqlite3_data_count(pStmt);
 59314  60883       pOut = &pVm->pResultSet[i];
 59315  60884     }else{
 59316  60885       /* If the value passed as the second argument is out of range, return
 59317  60886       ** a pointer to the following static Mem object which contains the
 59318  60887       ** value SQL NULL. Even though the Mem structure contains an element
 59319  60888       ** of type i64, on certain architecture (x86) with certain compiler
 59320  60889       ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
................................................................................
 59324  60893       ** this assert() from failing, when building with SQLITE_DEBUG defined
 59325  60894       ** using gcc, force nullMem to be 8-byte aligned using the magical
 59326  60895       ** __attribute__((aligned(8))) macro.  */
 59327  60896       static const Mem nullMem 
 59328  60897   #if defined(SQLITE_DEBUG) && defined(__GNUC__)
 59329  60898         __attribute__((aligned(8))) 
 59330  60899   #endif
 59331         -      = {0, "", (double)0, {0}, 0, MEM_Null, SQLITE_NULL, 0, 0, 0 };
        60900  +      = {0, "", (double)0, {0}, 0, MEM_Null, SQLITE_NULL, 0,
        60901  +#ifdef SQLITE_DEBUG
        60902  +         0, 0,  /* pScopyFrom, pFiller */
        60903  +#endif
        60904  +         0, 0 };
 59332  60905   
 59333  60906       if( pVm && ALWAYS(pVm->db) ){
 59334  60907         sqlite3_mutex_enter(pVm->db->mutex);
 59335  60908         sqlite3Error(pVm->db, SQLITE_RANGE, 0);
 59336  60909       }
 59337  60910       pOut = (Mem*)&nullMem;
 59338  60911     }
................................................................................
 60028  61601                         db->aLimit[SQLITE_LIMIT_LENGTH]);
 60029  61602     out.db = db;
 60030  61603     if( db->vdbeExecCnt>1 ){
 60031  61604       while( *zRawSql ){
 60032  61605         const char *zStart = zRawSql;
 60033  61606         while( *(zRawSql++)!='\n' && *zRawSql );
 60034  61607         sqlite3StrAccumAppend(&out, "-- ", 3);
 60035         -      sqlite3StrAccumAppend(&out, zStart, zRawSql-zStart);
        61608  +      sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
 60036  61609       }
 60037  61610     }else{
 60038  61611       while( zRawSql[0] ){
 60039  61612         n = findNextHostParameter(zRawSql, &nToken);
 60040  61613         assert( n>0 );
 60041  61614         sqlite3StrAccumAppend(&out, zRawSql, n);
 60042  61615         zRawSql += n;
................................................................................
 60738  62311     Vdbe *p                    /* The VDBE */
 60739  62312   ){
 60740  62313     int pc=0;                  /* The program counter */
 60741  62314     Op *aOp = p->aOp;          /* Copy of p->aOp */
 60742  62315     Op *pOp;                   /* Current operation */
 60743  62316     int rc = SQLITE_OK;        /* Value to return */
 60744  62317     sqlite3 *db = p->db;       /* The database */
 60745         -  u8 resetSchemaOnFault = 0; /* Reset schema after an error if true */
        62318  +  u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
 60746  62319     u8 encoding = ENC(db);     /* The database encoding */
 60747  62320   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
 60748  62321     int checkProgress;         /* True if progress callbacks are enabled */
 60749  62322     int nProgressOps = 0;      /* Opcodes executed since progress callback. */
 60750  62323   #endif
 60751  62324     Mem *aMem = p->aMem;       /* Copy of p->aMem */
 60752  62325     Mem *pIn1 = 0;             /* 1st input operand */
................................................................................
 60798  62371         int i;
 60799  62372         Mem *pArg;
 60800  62373         sqlite3_context ctx;
 60801  62374         sqlite3_value **apVal;
 60802  62375         int n;
 60803  62376       } ag;
 60804  62377       struct OP_ShiftRight_stack_vars {
 60805         -      i64 a;
 60806         -      i64 b;
        62378  +      i64 iA;
        62379  +      u64 uA;
        62380  +      i64 iB;
        62381  +      u8 op;
 60807  62382       } ah;
 60808  62383       struct OP_Ge_stack_vars {
 60809  62384         int res;            /* Result of the comparison of pIn1 against pIn3 */
 60810  62385         char affinity;      /* Affinity to use for comparison */
 60811  62386         u16 flags1;         /* Copy of initial value of pIn1->flags */
 60812  62387         u16 flags3;         /* Copy of initial value of pIn3->flags */
 60813  62388       } ai;
................................................................................
 60901  62476         int iCookie;
 60902  62477       } at;
 60903  62478       struct OP_SetCookie_stack_vars {
 60904  62479         Db *pDb;
 60905  62480       } au;
 60906  62481       struct OP_VerifyCookie_stack_vars {
 60907  62482         int iMeta;
        62483  +      int iGen;
 60908  62484         Btree *pBt;
 60909  62485       } av;
 60910  62486       struct OP_OpenWrite_stack_vars {
 60911  62487         int nField;
 60912  62488         KeyInfo *pKeyInfo;
 60913  62489         int p2;
 60914  62490         int iDb;
................................................................................
 61092  62668         Mem *pRec;
 61093  62669         sqlite3_context ctx;
 61094  62670         sqlite3_value **apVal;
 61095  62671       } cb;
 61096  62672       struct OP_AggFinal_stack_vars {
 61097  62673         Mem *pMem;
 61098  62674       } cc;
        62675  +    struct OP_Checkpoint_stack_vars {
        62676  +      int i;                          /* Loop counter */
        62677  +      int aRes[3];                    /* Results */
        62678  +      Mem *pMem;                      /* Write results here */
        62679  +    } cd;
 61099  62680       struct OP_JournalMode_stack_vars {
 61100  62681         Btree *pBt;                     /* Btree to change journal mode of */
 61101  62682         Pager *pPager;                  /* Pager associated with pBt */
 61102  62683         int eNew;                       /* New journal mode */
 61103  62684         int eOld;                       /* The old journal mode */
 61104  62685         const char *zFilename;          /* Name of database file for pPager */
 61105         -    } cd;
        62686  +    } ce;
 61106  62687       struct OP_IncrVacuum_stack_vars {
 61107  62688         Btree *pBt;
 61108         -    } ce;
        62689  +    } cf;
 61109  62690       struct OP_VBegin_stack_vars {
 61110  62691         VTable *pVTab;
 61111         -    } cf;
        62692  +    } cg;
 61112  62693       struct OP_VOpen_stack_vars {
 61113  62694         VdbeCursor *pCur;
 61114  62695         sqlite3_vtab_cursor *pVtabCursor;
 61115  62696         sqlite3_vtab *pVtab;
 61116  62697         sqlite3_module *pModule;
 61117         -    } cg;
        62698  +    } ch;
 61118  62699       struct OP_VFilter_stack_vars {
 61119  62700         int nArg;
 61120  62701         int iQuery;
 61121  62702         const sqlite3_module *pModule;
 61122  62703         Mem *pQuery;
 61123  62704         Mem *pArgc;
 61124  62705         sqlite3_vtab_cursor *pVtabCursor;
 61125  62706         sqlite3_vtab *pVtab;
 61126  62707         VdbeCursor *pCur;
 61127  62708         int res;
 61128  62709         int i;
 61129  62710         Mem **apArg;
 61130         -    } ch;
        62711  +    } ci;
 61131  62712       struct OP_VColumn_stack_vars {
 61132  62713         sqlite3_vtab *pVtab;
 61133  62714         const sqlite3_module *pModule;
 61134  62715         Mem *pDest;
 61135  62716         sqlite3_context sContext;
 61136         -    } ci;
        62717  +    } cj;
 61137  62718       struct OP_VNext_stack_vars {
 61138  62719         sqlite3_vtab *pVtab;
 61139  62720         const sqlite3_module *pModule;
 61140  62721         int res;
 61141  62722         VdbeCursor *pCur;
 61142         -    } cj;
        62723  +    } ck;
 61143  62724       struct OP_VRename_stack_vars {
 61144  62725         sqlite3_vtab *pVtab;
 61145  62726         Mem *pName;
 61146         -    } ck;
        62727  +    } cl;
 61147  62728       struct OP_VUpdate_stack_vars {
 61148  62729         sqlite3_vtab *pVtab;
 61149  62730         sqlite3_module *pModule;
 61150  62731         int nArg;
 61151  62732         int i;
 61152  62733         sqlite_int64 rowid;
 61153  62734         Mem **apArg;
 61154  62735         Mem *pX;
 61155         -    } cl;
        62736  +    } cm;
 61156  62737       struct OP_Trace_stack_vars {
 61157  62738         char *zTrace;
 61158         -    } cm;
        62739  +    } cn;
 61159  62740     } u;
 61160  62741     /* End automatically generated code
 61161  62742     ********************************************************************/
 61162  62743   
 61163  62744     assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
 61164         -  sqlite3VdbeMutexArrayEnter(p);
        62745  +  sqlite3VdbeEnter(p);
 61165  62746     if( p->rc==SQLITE_NOMEM ){
 61166  62747       /* This happens if a malloc() inside a call to sqlite3_column_text() or
 61167  62748       ** sqlite3_column_text16() failed.  */
 61168  62749       goto no_mem;
 61169  62750     }
 61170  62751     assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
 61171  62752     p->rc = SQLITE_OK;
................................................................................
 61844  63425     pOut = &aMem[pOp->p3];
 61845  63426     u.af.flags = pIn1->flags | pIn2->flags;
 61846  63427     if( (u.af.flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
 61847  63428     if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
 61848  63429       u.af.iA = pIn1->u.i;
 61849  63430       u.af.iB = pIn2->u.i;
 61850  63431       switch( pOp->opcode ){
 61851         -      case OP_Add:         u.af.iB += u.af.iA;       break;
 61852         -      case OP_Subtract:    u.af.iB -= u.af.iA;       break;
 61853         -      case OP_Multiply:    u.af.iB *= u.af.iA;       break;
        63432  +      case OP_Add:       if( sqlite3AddInt64(&u.af.iB,u.af.iA) ) goto fp_math;  break;
        63433  +      case OP_Subtract:  if( sqlite3SubInt64(&u.af.iB,u.af.iA) ) goto fp_math;  break;
        63434  +      case OP_Multiply:  if( sqlite3MulInt64(&u.af.iB,u.af.iA) ) goto fp_math;  break;
 61854  63435         case OP_Divide: {
 61855  63436           if( u.af.iA==0 ) goto arithmetic_result_is_null;
 61856         -        /* Dividing the largest possible negative 64-bit integer (1<<63) by
 61857         -        ** -1 returns an integer too large to store in a 64-bit data-type. On
 61858         -        ** some architectures, the value overflows to (1<<63). On others,
 61859         -        ** a SIGFPE is issued. The following statement normalizes this
 61860         -        ** behavior so that all architectures behave as if integer
 61861         -        ** overflow occurred.
 61862         -        */
 61863         -        if( u.af.iA==-1 && u.af.iB==SMALLEST_INT64 ) u.af.iA = 1;
        63437  +        if( u.af.iA==-1 && u.af.iB==SMALLEST_INT64 ) goto fp_math;
 61864  63438           u.af.iB /= u.af.iA;
 61865  63439           break;
 61866  63440         }
 61867  63441         default: {
 61868  63442           if( u.af.iA==0 ) goto arithmetic_result_is_null;
 61869  63443           if( u.af.iA==-1 ) u.af.iA = 1;
 61870  63444           u.af.iB %= u.af.iA;
 61871  63445           break;
 61872  63446         }
 61873  63447       }
 61874  63448       pOut->u.i = u.af.iB;
 61875  63449       MemSetTypeFlag(pOut, MEM_Int);
 61876  63450     }else{
        63451  +fp_math:
 61877  63452       u.af.rA = sqlite3VdbeRealValue(pIn1);
 61878  63453       u.af.rB = sqlite3VdbeRealValue(pIn2);
 61879  63454       switch( pOp->opcode ){
 61880  63455         case OP_Add:         u.af.rB += u.af.rA;       break;
 61881  63456         case OP_Subtract:    u.af.rB -= u.af.rA;       break;
 61882  63457         case OP_Multiply:    u.af.rB *= u.af.rA;       break;
 61883  63458         case OP_Divide: {
................................................................................
 62031  63606   
 62032  63607     /* Copy the result of the function into register P3 */
 62033  63608     sqlite3VdbeChangeEncoding(&u.ag.ctx.s, encoding);
 62034  63609     sqlite3VdbeMemMove(pOut, &u.ag.ctx.s);
 62035  63610     if( sqlite3VdbeMemTooBig(pOut) ){
 62036  63611       goto too_big;
 62037  63612     }
        63613  +
        63614  +#if 0
        63615  +  /* The app-defined function has done something that as caused this
        63616  +  ** statement to expire.  (Perhaps the function called sqlite3_exec()
        63617  +  ** with a CREATE TABLE statement.)
        63618  +  */
        63619  +  if( p->expired ) rc = SQLITE_ABORT;
        63620  +#endif
        63621  +
 62038  63622     REGISTER_TRACE(pOp->p3, pOut);
 62039  63623     UPDATE_MAX_BLOBSIZE(pOut);
 62040  63624     break;
 62041  63625   }
 62042  63626   
 62043  63627   /* Opcode: BitAnd P1 P2 P3 * *
 62044  63628   **
................................................................................
 62067  63651   ** If either input is NULL, the result is NULL.
 62068  63652   */
 62069  63653   case OP_BitAnd:                 /* same as TK_BITAND, in1, in2, out3 */
 62070  63654   case OP_BitOr:                  /* same as TK_BITOR, in1, in2, out3 */
 62071  63655   case OP_ShiftLeft:              /* same as TK_LSHIFT, in1, in2, out3 */
 62072  63656   case OP_ShiftRight: {           /* same as TK_RSHIFT, in1, in2, out3 */
 62073  63657   #if 0  /* local variables moved into u.ah */
 62074         -  i64 a;
 62075         -  i64 b;
        63658  +  i64 iA;
        63659  +  u64 uA;
        63660  +  i64 iB;
        63661  +  u8 op;
 62076  63662   #endif /* local variables moved into u.ah */
 62077  63663   
 62078  63664     pIn1 = &aMem[pOp->p1];
 62079  63665     pIn2 = &aMem[pOp->p2];
 62080  63666     pOut = &aMem[pOp->p3];
 62081  63667     if( (pIn1->flags | pIn2->flags) & MEM_Null ){
 62082  63668       sqlite3VdbeMemSetNull(pOut);
 62083  63669       break;
 62084  63670     }
 62085         -  u.ah.a = sqlite3VdbeIntValue(pIn2);
 62086         -  u.ah.b = sqlite3VdbeIntValue(pIn1);
 62087         -  switch( pOp->opcode ){
 62088         -    case OP_BitAnd:      u.ah.a &= u.ah.b;     break;
 62089         -    case OP_BitOr:       u.ah.a |= u.ah.b;     break;
 62090         -    case OP_ShiftLeft:   u.ah.a <<= u.ah.b;    break;
 62091         -    default:  assert( pOp->opcode==OP_ShiftRight );
 62092         -                         u.ah.a >>= u.ah.b;    break;
        63671  +  u.ah.iA = sqlite3VdbeIntValue(pIn2);
        63672  +  u.ah.iB = sqlite3VdbeIntValue(pIn1);
        63673  +  u.ah.op = pOp->opcode;
        63674  +  if( u.ah.op==OP_BitAnd ){
        63675  +    u.ah.iA &= u.ah.iB;
        63676  +  }else if( u.ah.op==OP_BitOr ){
        63677  +    u.ah.iA |= u.ah.iB;
        63678  +  }else if( u.ah.iB!=0 ){
        63679  +    assert( u.ah.op==OP_ShiftRight || u.ah.op==OP_ShiftLeft );
        63680  +
        63681  +    /* If shifting by a negative amount, shift in the other direction */
        63682  +    if( u.ah.iB<0 ){
        63683  +      assert( OP_ShiftRight==OP_ShiftLeft+1 );
        63684  +      u.ah.op = 2*OP_ShiftLeft + 1 - u.ah.op;
        63685  +      u.ah.iB = u.ah.iB>(-64) ? -u.ah.iB : 64;
        63686  +    }
        63687  +
        63688  +    if( u.ah.iB>=64 ){
        63689  +      u.ah.iA = (u.ah.iA>=0 || u.ah.op==OP_ShiftLeft) ? 0 : -1;
        63690  +    }else{
        63691  +      memcpy(&u.ah.uA, &u.ah.iA, sizeof(u.ah.uA));
        63692  +      if( u.ah.op==OP_ShiftLeft ){
        63693  +        u.ah.uA <<= u.ah.iB;
        63694  +      }else{
        63695  +        u.ah.uA >>= u.ah.iB;
        63696  +        /* Sign-extend on a right shift of a negative number */
        63697  +        if( u.ah.iA<0 ) u.ah.uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-u.ah.iB);
        63698  +      }
        63699  +      memcpy(&u.ah.iA, &u.ah.uA, sizeof(u.ah.iA));
        63700  +    }
 62093  63701     }
 62094         -  pOut->u.i = u.ah.a;
        63702  +  pOut->u.i = u.ah.iA;
 62095  63703     MemSetTypeFlag(pOut, MEM_Int);
 62096  63704     break;
 62097  63705   }
 62098  63706   
 62099  63707   /* Opcode: AddImm  P1 P2 * * *
 62100  63708   ** 
 62101  63709   ** Add the constant P2 to the value in register P1.
................................................................................
 63027  64635     ** Each type field is a varint representing the serial type of the
 63028  64636     ** corresponding data element (see sqlite3VdbeSerialType()). The
 63029  64637     ** hdr-size field is also a varint which is the offset from the beginning
 63030  64638     ** of the record to data0.
 63031  64639     */
 63032  64640     u.ao.nData = 0;         /* Number of bytes of data space */
 63033  64641     u.ao.nHdr = 0;          /* Number of bytes of header space */
 63034         -  u.ao.nByte = 0;         /* Data space required for this record */
 63035  64642     u.ao.nZero = 0;         /* Number of zero bytes at the end of the record */
 63036  64643     u.ao.nField = pOp->p1;
 63037  64644     u.ao.zAffinity = pOp->p4.z;
 63038  64645     assert( u.ao.nField>0 && pOp->p2>0 && pOp->p2+u.ao.nField<=p->nMem+1 );
 63039  64646     u.ao.pData0 = &aMem[u.ao.nField];
 63040  64647     u.ao.nField = pOp->p2;
 63041  64648     u.ao.pLast = &u.ao.pData0[u.ao.nField-1];
................................................................................
 63252  64859             rc = sqlite3BtreeSavepoint(db->aDb[u.aq.ii].pBt, u.aq.p1, u.aq.iSavepoint);
 63253  64860             if( rc!=SQLITE_OK ){
 63254  64861               goto abort_due_to_error;
 63255  64862             }
 63256  64863           }
 63257  64864           if( u.aq.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
 63258  64865             sqlite3ExpirePreparedStatements(db);
 63259         -          sqlite3ResetInternalSchema(db, 0);
        64866  +          sqlite3ResetInternalSchema(db, -1);
 63260  64867             db->flags = (db->flags | SQLITE_InternChanges);
 63261  64868           }
 63262  64869         }
 63263  64870   
 63264  64871         /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
 63265  64872         ** savepoints nested inside of the savepoint being operated on. */
 63266  64873         while( db->pSavepoint!=u.aq.pSavepoint ){
................................................................................
 63396  65003   */
 63397  65004   case OP_Transaction: {
 63398  65005   #if 0  /* local variables moved into u.as */
 63399  65006     Btree *pBt;
 63400  65007   #endif /* local variables moved into u.as */
 63401  65008   
 63402  65009     assert( pOp->p1>=0 && pOp->p1<db->nDb );
 63403         -  assert( (p->btreeMask & (1<<pOp->p1))!=0 );
        65010  +  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 63404  65011     u.as.pBt = db->aDb[pOp->p1].pBt;
 63405  65012   
 63406  65013     if( u.as.pBt ){
 63407  65014       rc = sqlite3BtreeBeginTrans(u.as.pBt, pOp->p2);
 63408  65015       if( rc==SQLITE_BUSY ){
 63409  65016         p->pc = pc;
 63410  65017         p->rc = rc = SQLITE_BUSY;
................................................................................
 63454  65061   #endif /* local variables moved into u.at */
 63455  65062   
 63456  65063     u.at.iDb = pOp->p1;
 63457  65064     u.at.iCookie = pOp->p3;
 63458  65065     assert( pOp->p3<SQLITE_N_BTREE_META );
 63459  65066     assert( u.at.iDb>=0 && u.at.iDb<db->nDb );
 63460  65067     assert( db->aDb[u.at.iDb].pBt!=0 );
 63461         -  assert( (p->btreeMask & (1<<u.at.iDb))!=0 );
        65068  +  assert( (p->btreeMask & (((yDbMask)1)<<u.at.iDb))!=0 );
 63462  65069   
 63463  65070     sqlite3BtreeGetMeta(db->aDb[u.at.iDb].pBt, u.at.iCookie, (u32 *)&u.at.iMeta);
 63464  65071     pOut->u.i = u.at.iMeta;
 63465  65072     break;
 63466  65073   }
 63467  65074   
 63468  65075   /* Opcode: SetCookie P1 P2 P3 * *
................................................................................
 63477  65084   */
 63478  65085   case OP_SetCookie: {       /* in3 */
 63479  65086   #if 0  /* local variables moved into u.au */
 63480  65087     Db *pDb;
 63481  65088   #endif /* local variables moved into u.au */
 63482  65089     assert( pOp->p2<SQLITE_N_BTREE_META );
 63483  65090     assert( pOp->p1>=0 && pOp->p1<db->nDb );
 63484         -  assert( (p->btreeMask & (1<<pOp->p1))!=0 );
        65091  +  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 63485  65092     u.au.pDb = &db->aDb[pOp->p1];
 63486  65093     assert( u.au.pDb->pBt!=0 );
        65094  +  assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
 63487  65095     pIn3 = &aMem[pOp->p3];
 63488  65096     sqlite3VdbeMemIntegerify(pIn3);
 63489  65097     /* See note about index shifting on OP_ReadCookie */
 63490  65098     rc = sqlite3BtreeUpdateMeta(u.au.pDb->pBt, pOp->p2, (int)pIn3->u.i);
 63491  65099     if( pOp->p2==BTREE_SCHEMA_VERSION ){
 63492  65100       /* When the schema cookie changes, record the new cookie internally */
 63493  65101       u.au.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
................................................................................
 63501  65109       ** schema is changed.  Ticket #1644 */
 63502  65110       sqlite3ExpirePreparedStatements(db);
 63503  65111       p->expired = 0;
 63504  65112     }
 63505  65113     break;
 63506  65114   }
 63507  65115   
 63508         -/* Opcode: VerifyCookie P1 P2 *
        65116  +/* Opcode: VerifyCookie P1 P2 P3 * *
 63509  65117   **
 63510  65118   ** Check the value of global database parameter number 0 (the
 63511         -** schema version) and make sure it is equal to P2.  
        65119  +** schema version) and make sure it is equal to P2 and that the
        65120  +** generation counter on the local schema parse equals P3.
        65121  +**
 63512  65122   ** P1 is the database number which is 0 for the main database file
 63513  65123   ** and 1 for the file holding temporary tables and some higher number
 63514  65124   ** for auxiliary databases.
 63515  65125   **
 63516  65126   ** The cookie changes its value whenever the database schema changes.
 63517  65127   ** This operation is used to detect when that the cookie has changed
 63518  65128   ** and that the current process needs to reread the schema.
................................................................................
 63520  65130   ** Either a transaction needs to have been started or an OP_Open needs
 63521  65131   ** to be executed (to establish a read lock) before this opcode is
 63522  65132   ** invoked.
 63523  65133   */
 63524  65134   case OP_VerifyCookie: {
 63525  65135   #if 0  /* local variables moved into u.av */
 63526  65136     int iMeta;
        65137  +  int iGen;
 63527  65138     Btree *pBt;
 63528  65139   #endif /* local variables moved into u.av */
        65140  +
 63529  65141     assert( pOp->p1>=0 && pOp->p1<db->nDb );
 63530         -  assert( (p->btreeMask & (1<<pOp->p1))!=0 );
        65142  +  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
        65143  +  assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
 63531  65144     u.av.pBt = db->aDb[pOp->p1].pBt;
 63532  65145     if( u.av.pBt ){
 63533  65146       sqlite3BtreeGetMeta(u.av.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.av.iMeta);
        65147  +    u.av.iGen = db->aDb[pOp->p1].pSchema->iGeneration;
 63534  65148     }else{
 63535         -    u.av.iMeta = 0;
        65149  +    u.av.iGen = u.av.iMeta = 0;
 63536  65150     }
 63537         -  if( u.av.iMeta!=pOp->p2 ){
        65151  +  if( u.av.iMeta!=pOp->p2 || u.av.iGen!=pOp->p3 ){
 63538  65152       sqlite3DbFree(db, p->zErrMsg);
 63539  65153       p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
 63540  65154       /* If the schema-cookie from the database file matches the cookie
 63541  65155       ** stored with the in-memory representation of the schema, do
 63542  65156       ** not reload the schema from the database file.
 63543  65157       **
 63544  65158       ** If virtual-tables are in use, this is not just an optimization.
................................................................................
 63550  65164       ** to be invalidated whenever sqlite3_step() is called from within
 63551  65165       ** a v-table method.
 63552  65166       */
 63553  65167       if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.av.iMeta ){
 63554  65168         sqlite3ResetInternalSchema(db, pOp->p1);
 63555  65169       }
 63556  65170   
 63557         -    sqlite3ExpirePreparedStatements(db);
        65171  +    p->expired = 1;
 63558  65172       rc = SQLITE_SCHEMA;
 63559  65173     }
 63560  65174     break;
 63561  65175   }
 63562  65176   
 63563  65177   /* Opcode: OpenRead P1 P2 P3 P4 P5
 63564  65178   **
................................................................................
 63628  65242     }
 63629  65243   
 63630  65244     u.aw.nField = 0;
 63631  65245     u.aw.pKeyInfo = 0;
 63632  65246     u.aw.p2 = pOp->p2;
 63633  65247     u.aw.iDb = pOp->p3;
 63634  65248     assert( u.aw.iDb>=0 && u.aw.iDb<db->nDb );
 63635         -  assert( (p->btreeMask & (1<<u.aw.iDb))!=0 );
        65249  +  assert( (p->btreeMask & (((yDbMask)1)<<u.aw.iDb))!=0 );
 63636  65250     u.aw.pDb = &db->aDb[u.aw.iDb];
 63637  65251     u.aw.pX = u.aw.pDb->pBt;
 63638  65252     assert( u.aw.pX!=0 );
 63639  65253     if( pOp->opcode==OP_OpenWrite ){
 63640  65254       u.aw.wrFlag = 1;
        65255  +    assert( sqlite3SchemaMutexHeld(db, u.aw.iDb, 0) );
 63641  65256       if( u.aw.pDb->pSchema->file_format < p->minWriteFileFormat ){
 63642  65257         p->minWriteFileFormat = u.aw.pDb->pSchema->file_format;
 63643  65258       }
 63644  65259     }else{
 63645  65260       u.aw.wrFlag = 0;
 63646  65261     }
 63647  65262     if( pOp->p5 ){
................................................................................
 64333  65948       **
 64334  65949       ** The second algorithm is to select a rowid at random and see if
 64335  65950       ** it already exists in the table.  If it does not exist, we have
 64336  65951       ** succeeded.  If the random rowid does exist, we select a new one
 64337  65952       ** and try again, up to 100 times.
 64338  65953       */
 64339  65954       assert( u.be.pC->isTable );
 64340         -    u.be.cnt = 0;
 64341  65955   
 64342  65956   #ifdef SQLITE_32BIT_ROWID
 64343  65957   #   define MAX_ROWID 0x7fffffff
 64344  65958   #else
 64345  65959       /* Some compilers complain about constants of the form 0x7fffffffffffffff.
 64346  65960       ** Others complain about 0x7ffffffffffffffffLL.  The following macro seems
 64347  65961       ** to provide the constant while making all compilers happy.
................................................................................
 65166  66780     pOut->flags = MEM_Null;
 65167  66781     if( u.br.iCnt>1 ){
 65168  66782       rc = SQLITE_LOCKED;
 65169  66783       p->errorAction = OE_Abort;
 65170  66784     }else{
 65171  66785       u.br.iDb = pOp->p3;
 65172  66786       assert( u.br.iCnt==1 );
 65173         -    assert( (p->btreeMask & (1<<u.br.iDb))!=0 );
        66787  +    assert( (p->btreeMask & (((yDbMask)1)<<u.br.iDb))!=0 );
 65174  66788       rc = sqlite3BtreeDropTable(db->aDb[u.br.iDb].pBt, pOp->p1, &u.br.iMoved);
 65175  66789       pOut->flags = MEM_Int;
 65176  66790       pOut->u.i = u.br.iMoved;
 65177  66791   #ifndef SQLITE_OMIT_AUTOVACUUM
 65178  66792       if( rc==SQLITE_OK && u.br.iMoved!=0 ){
 65179         -      sqlite3RootPageMoved(&db->aDb[u.br.iDb], u.br.iMoved, pOp->p1);
 65180         -      resetSchemaOnFault = 1;
        66793  +      sqlite3RootPageMoved(db, u.br.iDb, u.br.iMoved, pOp->p1);
        66794  +      /* All OP_Destroy operations occur on the same btree */
        66795  +      assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.br.iDb+1 );
        66796  +      resetSchemaOnFault = u.br.iDb+1;
 65181  66797       }
 65182  66798   #endif
 65183  66799     }
 65184  66800     break;
 65185  66801   }
 65186  66802   
 65187  66803   /* Opcode: Clear P1 P2 P3
................................................................................
 65204  66820   */
 65205  66821   case OP_Clear: {
 65206  66822   #if 0  /* local variables moved into u.bs */
 65207  66823     int nChange;
 65208  66824   #endif /* local variables moved into u.bs */
 65209  66825   
 65210  66826     u.bs.nChange = 0;
 65211         -  assert( (p->btreeMask & (1<<pOp->p2))!=0 );
        66827  +  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
 65212  66828     rc = sqlite3BtreeClearTable(
 65213  66829         db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bs.nChange : 0)
 65214  66830     );
 65215  66831     if( pOp->p3 ){
 65216  66832       p->nChange += u.bs.nChange;
 65217  66833       if( pOp->p3>0 ){
 65218  66834         assert( memIsValid(&aMem[pOp->p3]) );
................................................................................
 65251  66867     int pgno;
 65252  66868     int flags;
 65253  66869     Db *pDb;
 65254  66870   #endif /* local variables moved into u.bt */
 65255  66871   
 65256  66872     u.bt.pgno = 0;
 65257  66873     assert( pOp->p1>=0 && pOp->p1<db->nDb );
 65258         -  assert( (p->btreeMask & (1<<pOp->p1))!=0 );
        66874  +  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
 65259  66875     u.bt.pDb = &db->aDb[pOp->p1];
 65260  66876     assert( u.bt.pDb->pBt!=0 );
 65261  66877     if( pOp->opcode==OP_CreateTable ){
 65262  66878       /* u.bt.flags = BTREE_INTKEY; */
 65263  66879       u.bt.flags = BTREE_INTKEY;
 65264  66880     }else{
 65265  66881       u.bt.flags = BTREE_BLOBKEY;
 65266  66882     }
 65267  66883     rc = sqlite3BtreeCreateTable(u.bt.pDb->pBt, &u.bt.pgno, u.bt.flags);
 65268  66884     pOut->u.i = u.bt.pgno;
 65269  66885     break;
 65270  66886   }
 65271  66887   
 65272         -/* Opcode: ParseSchema P1 P2 * P4 *
        66888  +/* Opcode: ParseSchema P1 * * P4 *
 65273  66889   **
 65274  66890   ** Read and parse all entries from the SQLITE_MASTER table of database P1
 65275         -** that match the WHERE clause P4.  P2 is the "force" flag.   Always do
 65276         -** the parsing if P2 is true.  If P2 is false, then this routine is a
 65277         -** no-op if the schema is not currently loaded.  In other words, if P2
 65278         -** is false, the SQLITE_MASTER table is only parsed if the rest of the
 65279         -** schema is already loaded into the symbol table.
        66891  +** that match the WHERE clause P4. 
 65280  66892   **
 65281  66893   ** This opcode invokes the parser to create a new virtual machine,
 65282  66894   ** then runs the new virtual machine.  It is thus a re-entrant opcode.
 65283  66895   */
 65284  66896   case OP_ParseSchema: {
 65285  66897   #if 0  /* local variables moved into u.bu */
 65286  66898     int iDb;
 65287  66899     const char *zMaster;
 65288  66900     char *zSql;
 65289  66901     InitData initData;
 65290  66902   #endif /* local variables moved into u.bu */
        66903  +
        66904  +  /* Any prepared statement that invokes this opcode will hold mutexes
        66905  +  ** on every btree.  This is a prerequisite for invoking
        66906  +  ** sqlite3InitCallback().
        66907  +  */
        66908  +#ifdef SQLITE_DEBUG
        66909  +  for(u.bu.iDb=0; u.bu.iDb<db->nDb; u.bu.iDb++){
        66910  +    assert( u.bu.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.bu.iDb].pBt) );
        66911  +  }
        66912  +#endif
 65291  66913   
 65292  66914     u.bu.iDb = pOp->p1;
 65293  66915     assert( u.bu.iDb>=0 && u.bu.iDb<db->nDb );
 65294         -
 65295         -  /* If pOp->p2 is 0, then this opcode is being executed to read a
 65296         -  ** single row, for example the row corresponding to a new index
 65297         -  ** created by this VDBE, from the sqlite_master table. It only
 65298         -  ** does this if the corresponding in-memory schema is currently
 65299         -  ** loaded. Otherwise, the new index definition can be loaded along
 65300         -  ** with the rest of the schema when it is required.
 65301         -  **
 65302         -  ** Although the mutex on the BtShared object that corresponds to
 65303         -  ** database u.bu.iDb (the database containing the sqlite_master table
 65304         -  ** read by this instruction) is currently held, it is necessary to
 65305         -  ** obtain the mutexes on all attached databases before checking if
 65306         -  ** the schema of u.bu.iDb is loaded. This is because, at the start of
 65307         -  ** the sqlite3_exec() call below, SQLite will invoke
 65308         -  ** sqlite3BtreeEnterAll(). If all mutexes are not already held, the
 65309         -  ** u.bu.iDb mutex may be temporarily released to avoid deadlock. If
 65310         -  ** this happens, then some other thread may delete the in-memory
 65311         -  ** schema of database u.bu.iDb before the SQL statement runs. The schema
 65312         -  ** will not be reloaded becuase the db->init.busy flag is set. This
 65313         -  ** can result in a "no such table: sqlite_master" or "malformed
 65314         -  ** database schema" error being returned to the user.
 65315         -  */
 65316         -  assert( sqlite3BtreeHoldsMutex(db->aDb[u.bu.iDb].pBt) );
 65317         -  sqlite3BtreeEnterAll(db);
 65318         -  if( pOp->p2 || DbHasProperty(db, u.bu.iDb, DB_SchemaLoaded) ){
        66916  +  assert( DbHasProperty(db, u.bu.iDb, DB_SchemaLoaded) );
        66917  +  /* Used to be a conditional */ {
 65319  66918       u.bu.zMaster = SCHEMA_TABLE(u.bu.iDb);
 65320  66919       u.bu.initData.db = db;
 65321  66920       u.bu.initData.iDb = pOp->p1;
 65322  66921       u.bu.initData.pzErrMsg = &p->zErrMsg;
 65323  66922       u.bu.zSql = sqlite3MPrintf(db,
 65324  66923          "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
 65325  66924          db->aDb[u.bu.iDb].zName, u.bu.zMaster, pOp->p4.z);
................................................................................
 65332  66931         assert( !db->mallocFailed );
 65333  66932         rc = sqlite3_exec(db, u.bu.zSql, sqlite3InitCallback, &u.bu.initData, 0);
 65334  66933         if( rc==SQLITE_OK ) rc = u.bu.initData.rc;
 65335  66934         sqlite3DbFree(db, u.bu.zSql);
 65336  66935         db->init.busy = 0;
 65337  66936       }
 65338  66937     }
 65339         -  sqlite3BtreeLeaveAll(db);
 65340  66938     if( rc==SQLITE_NOMEM ){
 65341  66939       goto no_mem;
 65342  66940     }
 65343  66941     break;
 65344  66942   }
 65345  66943   
 65346  66944   #if !defined(SQLITE_OMIT_ANALYZE)
................................................................................
 65435  67033     assert( (u.bv.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
 65436  67034     pIn1 = &aMem[pOp->p1];
 65437  67035     for(u.bv.j=0; u.bv.j<u.bv.nRoot; u.bv.j++){
 65438  67036       u.bv.aRoot[u.bv.j] = (int)sqlite3VdbeIntValue(&pIn1[u.bv.j]);
 65439  67037     }
 65440  67038     u.bv.aRoot[u.bv.j] = 0;
 65441  67039     assert( pOp->p5<db->nDb );
 65442         -  assert( (p->btreeMask & (1<<pOp->p5))!=0 );
        67040  +  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
 65443  67041     u.bv.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.bv.aRoot, u.bv.nRoot,
 65444  67042                                    (int)u.bv.pnErr->u.i, &u.bv.nErr);
 65445  67043     sqlite3DbFree(db, u.bv.aRoot);
 65446  67044     u.bv.pnErr->u.i -= u.bv.nErr;
 65447  67045     sqlite3VdbeMemSetNull(pIn1);
 65448  67046     if( u.bv.nErr==0 ){
 65449  67047       assert( u.bv.z==0 );
................................................................................
 65875  67473       u.cb.ctx.pColl = pOp[-1].p4.pColl;
 65876  67474     }
 65877  67475     (u.cb.ctx.pFunc->xStep)(&u.cb.ctx, u.cb.n, u.cb.apVal); /* IMP: R-24505-23230 */
 65878  67476     if( u.cb.ctx.isError ){
 65879  67477       sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cb.ctx.s));
 65880  67478       rc = u.cb.ctx.isError;
 65881  67479     }
        67480  +
 65882  67481     sqlite3VdbeMemRelease(&u.cb.ctx.s);
        67482  +
 65883  67483     break;
 65884  67484   }
 65885  67485   
 65886  67486   /* Opcode: AggFinal P1 P2 * P4 *
 65887  67487   **
 65888  67488   ** Execute the finalizer function for an aggregate.  P1 is
 65889  67489   ** the memory location that is the accumulator for the aggregate.
................................................................................
 65911  67511     if( sqlite3VdbeMemTooBig(u.cc.pMem) ){
 65912  67512       goto too_big;
 65913  67513     }
 65914  67514     break;
 65915  67515   }
 65916  67516   
 65917  67517   #ifndef SQLITE_OMIT_WAL
 65918         -/* Opcode: Checkpoint P1 * * * *
        67518  +/* Opcode: Checkpoint P1 P2 P3 * *
 65919  67519   **
 65920  67520   ** Checkpoint database P1. This is a no-op if P1 is not currently in
 65921         -** WAL mode.
        67521  +** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
        67522  +** or RESTART.  Write 1 or 0 into mem[P3] if the checkpoint returns
        67523  +** SQLITE_BUSY or not, respectively.  Write the number of pages in the
        67524  +** WAL after the checkpoint into mem[P3+1] and the number of pages
        67525  +** in the WAL that have been checkpointed after the checkpoint
        67526  +** completes into mem[P3+2].  However on an error, mem[P3+1] and
        67527  +** mem[P3+2] are initialized to -1.
 65922  67528   */
 65923  67529   case OP_Checkpoint: {
 65924         -  rc = sqlite3Checkpoint(db, pOp->p1);
        67530  +#if 0  /* local variables moved into u.cd */
        67531  +  int i;                          /* Loop counter */
        67532  +  int aRes[3];                    /* Results */
        67533  +  Mem *pMem;                      /* Write results here */
        67534  +#endif /* local variables moved into u.cd */
        67535  +
        67536  +  u.cd.aRes[0] = 0;
        67537  +  u.cd.aRes[1] = u.cd.aRes[2] = -1;
        67538  +  assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
        67539  +       || pOp->p2==SQLITE_CHECKPOINT_FULL
        67540  +       || pOp->p2==SQLITE_CHECKPOINT_RESTART
        67541  +  );
        67542  +  rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.cd.aRes[1], &u.cd.aRes[2]);
        67543  +  if( rc==SQLITE_BUSY ){
        67544  +    rc = SQLITE_OK;
        67545  +    u.cd.aRes[0] = 1;
        67546  +  }
        67547  +  for(u.cd.i=0, u.cd.pMem = &aMem[pOp->p3]; u.cd.i<3; u.cd.i++, u.cd.pMem++){
        67548  +    sqlite3VdbeMemSetInt64(u.cd.pMem, (i64)u.cd.aRes[u.cd.i]);
        67549  +  }
 65925  67550     break;
 65926  67551   };  
 65927  67552   #endif
 65928  67553   
 65929  67554   #ifndef SQLITE_OMIT_PRAGMA
 65930  67555   /* Opcode: JournalMode P1 P2 P3 * P5
 65931  67556   **
................................................................................
 65935  67560   ** operation. No IO is required.
 65936  67561   **
 65937  67562   ** If changing into or out of WAL mode the procedure is more complicated.
 65938  67563   **
 65939  67564   ** Write a string containing the final journal-mode to register P2.
 65940  67565   */
 65941  67566   case OP_JournalMode: {    /* out2-prerelease */
 65942         -#if 0  /* local variables moved into u.cd */
        67567  +#if 0  /* local variables moved into u.ce */
 65943  67568     Btree *pBt;                     /* Btree to change journal mode of */
 65944  67569     Pager *pPager;                  /* Pager associated with pBt */
 65945  67570     int eNew;                       /* New journal mode */
 65946  67571     int eOld;                       /* The old journal mode */
 65947  67572     const char *zFilename;          /* Name of database file for pPager */
 65948         -#endif /* local variables moved into u.cd */
        67573  +#endif /* local variables moved into u.ce */
 65949  67574   
 65950         -  u.cd.eNew = pOp->p3;
 65951         -  assert( u.cd.eNew==PAGER_JOURNALMODE_DELETE
 65952         -       || u.cd.eNew==PAGER_JOURNALMODE_TRUNCATE
 65953         -       || u.cd.eNew==PAGER_JOURNALMODE_PERSIST
 65954         -       || u.cd.eNew==PAGER_JOURNALMODE_OFF
 65955         -       || u.cd.eNew==PAGER_JOURNALMODE_MEMORY
 65956         -       || u.cd.eNew==PAGER_JOURNALMODE_WAL
 65957         -       || u.cd.eNew==PAGER_JOURNALMODE_QUERY
        67575  +  u.ce.eNew = pOp->p3;
        67576  +  assert( u.ce.eNew==PAGER_JOURNALMODE_DELETE
        67577  +       || u.ce.eNew==PAGER_JOURNALMODE_TRUNCATE
        67578  +       || u.ce.eNew==PAGER_JOURNALMODE_PERSIST
        67579  +       || u.ce.eNew==PAGER_JOURNALMODE_OFF
        67580  +       || u.ce.eNew==PAGER_JOURNALMODE_MEMORY
        67581  +       || u.ce.eNew==PAGER_JOURNALMODE_WAL
        67582  +       || u.ce.eNew==PAGER_JOURNALMODE_QUERY
 65958  67583     );
 65959  67584     assert( pOp->p1>=0 && pOp->p1<db->nDb );
 65960  67585   
 65961         -  /* This opcode is used in two places: PRAGMA journal_mode and ATTACH.
 65962         -  ** In PRAGMA journal_mode, the sqlite3VdbeUsesBtree() routine is called
 65963         -  ** when the statment is prepared and so p->aMutex.nMutex>0.  All mutexes
 65964         -  ** are already acquired.  But when used in ATTACH, sqlite3VdbeUsesBtree()
 65965         -  ** is not called when the statement is prepared because it requires the
 65966         -  ** iDb index of the database as a parameter, and the database has not
 65967         -  ** yet been attached so that index is unavailable.  We have to wait
 65968         -  ** until runtime (now) to get the mutex on the newly attached database.
 65969         -  ** No other mutexes are required by the ATTACH command so this is safe
 65970         -  ** to do.
 65971         -  */
 65972         -  assert( (p->btreeMask & (1<<pOp->p1))!=0 || p->aMutex.nMutex==0 );
 65973         -  if( p->aMutex.nMutex==0 ){
 65974         -    /* This occurs right after ATTACH.  Get a mutex on the newly ATTACHed
 65975         -    ** database. */
 65976         -    sqlite3VdbeUsesBtree(p, pOp->p1);
 65977         -    sqlite3VdbeMutexArrayEnter(p);
 65978         -  }
 65979         -
 65980         -  u.cd.pBt = db->aDb[pOp->p1].pBt;
 65981         -  u.cd.pPager = sqlite3BtreePager(u.cd.pBt);
 65982         -  u.cd.eOld = sqlite3PagerGetJournalMode(u.cd.pPager);
 65983         -  if( u.cd.eNew==PAGER_JOURNALMODE_QUERY ) u.cd.eNew = u.cd.eOld;
 65984         -  if( !sqlite3PagerOkToChangeJournalMode(u.cd.pPager) ) u.cd.eNew = u.cd.eOld;
        67586  +  u.ce.pBt = db->aDb[pOp->p1].pBt;
        67587  +  u.ce.pPager = sqlite3BtreePager(u.ce.pBt);
        67588  +  u.ce.eOld = sqlite3PagerGetJournalMode(u.ce.pPager);
        67589  +  if( u.ce.eNew==PAGER_JOURNALMODE_QUERY ) u.ce.eNew = u.ce.eOld;
        67590  +  if( !sqlite3PagerOkToChangeJournalMode(u.ce.pPager) ) u.ce.eNew = u.ce.eOld;
 65985  67591   
 65986  67592   #ifndef SQLITE_OMIT_WAL
 65987         -  u.cd.zFilename = sqlite3PagerFilename(u.cd.pPager);
        67593  +  u.ce.zFilename = sqlite3PagerFilename(u.ce.pPager);
 65988  67594   
 65989  67595     /* Do not allow a transition to journal_mode=WAL for a database
 65990  67596     ** in temporary storage or if the VFS does not support shared memory
 65991  67597     */
 65992         -  if( u.cd.eNew==PAGER_JOURNALMODE_WAL
 65993         -   && (u.cd.zFilename[0]==0                         /* Temp file */
 65994         -       || !sqlite3PagerWalSupported(u.cd.pPager))   /* No shared-memory support */
        67598  +  if( u.ce.eNew==PAGER_JOURNALMODE_WAL
        67599  +   && (u.ce.zFilename[0]==0                         /* Temp file */
        67600  +       || !sqlite3PagerWalSupported(u.ce.pPager))   /* No shared-memory support */
 65995  67601     ){
 65996         -    u.cd.eNew = u.cd.eOld;
        67602  +    u.ce.eNew = u.ce.eOld;
 65997  67603     }
 65998  67604   
 65999         -  if( (u.cd.eNew!=u.cd.eOld)
 66000         -   && (u.cd.eOld==PAGER_JOURNALMODE_WAL || u.cd.eNew==PAGER_JOURNALMODE_WAL)
        67605  +  if( (u.ce.eNew!=u.ce.eOld)
        67606  +   && (u.ce.eOld==PAGER_JOURNALMODE_WAL || u.ce.eNew==PAGER_JOURNALMODE_WAL)
 66001  67607     ){
 66002  67608       if( !db->autoCommit || db->activeVdbeCnt>1 ){
 66003  67609         rc = SQLITE_ERROR;
 66004  67610         sqlite3SetString(&p->zErrMsg, db,
 66005  67611             "cannot change %s wal mode from within a transaction",
 66006         -          (u.cd.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
        67612  +          (u.ce.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
 66007  67613         );
 66008  67614         break;
 66009  67615       }else{
 66010  67616   
 66011         -      if( u.cd.eOld==PAGER_JOURNALMODE_WAL ){
        67617  +      if( u.ce.eOld==PAGER_JOURNALMODE_WAL ){
 66012  67618           /* If leaving WAL mode, close the log file. If successful, the call
 66013  67619           ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
 66014  67620           ** file. An EXCLUSIVE lock may still be held on the database file
 66015  67621           ** after a successful return.
 66016  67622           */
 66017         -        rc = sqlite3PagerCloseWal(u.cd.pPager);
        67623  +        rc = sqlite3PagerCloseWal(u.ce.pPager);
 66018  67624           if( rc==SQLITE_OK ){
 66019         -          sqlite3PagerSetJournalMode(u.cd.pPager, u.cd.eNew);
        67625  +          sqlite3PagerSetJournalMode(u.ce.pPager, u.ce.eNew);
 66020  67626           }
 66021         -      }else if( u.cd.eOld==PAGER_JOURNALMODE_MEMORY ){
        67627  +      }else if( u.ce.eOld==PAGER_JOURNALMODE_MEMORY ){
 66022  67628           /* Cannot transition directly from MEMORY to WAL.  Use mode OFF
 66023  67629           ** as an intermediate */
 66024         -        sqlite3PagerSetJournalMode(u.cd.pPager, PAGER_JOURNALMODE_OFF);
        67630  +        sqlite3PagerSetJournalMode(u.ce.pPager, PAGER_JOURNALMODE_OFF);
 66025  67631         }
 66026  67632   
 66027  67633         /* Open a transaction on the database file. Regardless of the journal
 66028  67634         ** mode, this transaction always uses a rollback journal.
 66029  67635         */
 66030         -      assert( sqlite3BtreeIsInTrans(u.cd.pBt)==0 );
        67636  +      assert( sqlite3BtreeIsInTrans(u.ce.pBt)==0 );
 66031  67637         if( rc==SQLITE_OK ){
 66032         -        rc = sqlite3BtreeSetVersion(u.cd.pBt, (u.cd.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
        67638  +        rc = sqlite3BtreeSetVersion(u.ce.pBt, (u.ce.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
 66033  67639         }
 66034  67640       }
 66035  67641     }
 66036  67642   #endif /* ifndef SQLITE_OMIT_WAL */
 66037  67643   
 66038  67644     if( rc ){
 66039         -    u.cd.eNew = u.cd.eOld;
        67645  +    u.ce.eNew = u.ce.eOld;
 66040  67646     }
 66041         -  u.cd.eNew = sqlite3PagerSetJournalMode(u.cd.pPager, u.cd.eNew);
        67647  +  u.ce.eNew = sqlite3PagerSetJournalMode(u.ce.pPager, u.ce.eNew);
 66042  67648   
 66043  67649     pOut = &aMem[pOp->p2];
 66044  67650     pOut->flags = MEM_Str|MEM_Static|MEM_Term;
 66045         -  pOut->z = (char *)sqlite3JournalModename(u.cd.eNew);
        67651  +  pOut->z = (char *)sqlite3JournalModename(u.ce.eNew);
 66046  67652     pOut->n = sqlite3Strlen30(pOut->z);
 66047  67653     pOut->enc = SQLITE_UTF8;
 66048  67654     sqlite3VdbeChangeEncoding(pOut, encoding);
 66049  67655     break;
 66050  67656   };
 66051  67657   #endif /* SQLITE_OMIT_PRAGMA */
 66052  67658   
................................................................................
 66067  67673   /* Opcode: IncrVacuum P1 P2 * * *
 66068  67674   **
 66069  67675   ** Perform a single step of the incremental vacuum procedure on
 66070  67676   ** the P1 database. If the vacuum has finished, jump to instruction
 66071  67677   ** P2. Otherwise, fall through to the next instruction.
 66072  67678   */
 66073  67679   case OP_IncrVacuum: {        /* jump */
 66074         -#if 0  /* local variables moved into u.ce */
        67680  +#if 0  /* local variables moved into u.cf */
 66075  67681     Btree *pBt;
 66076         -#endif /* local variables moved into u.ce */
        67682  +#endif /* local variables moved into u.cf */
 66077  67683   
 66078  67684     assert( pOp->p1>=0 && pOp->p1<db->nDb );
 66079         -  assert( (p->btreeMask & (1<<pOp->p1))!=0 );
 66080         -  u.ce.pBt = db->aDb[pOp->p1].pBt;
 66081         -  rc = sqlite3BtreeIncrVacuum(u.ce.pBt);
        67685  +  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
        67686  +  u.cf.pBt = db->aDb[pOp->p1].pBt;
        67687  +  rc = sqlite3BtreeIncrVacuum(u.cf.pBt);
 66082  67688     if( rc==SQLITE_DONE ){
 66083  67689       pc = pOp->p2 - 1;
 66084  67690       rc = SQLITE_OK;
 66085  67691     }
 66086  67692     break;
 66087  67693   }
 66088  67694   #endif
................................................................................
 66121  67727   ** used to generate an error message if the lock cannot be obtained.
 66122  67728   */
 66123  67729   case OP_TableLock: {
 66124  67730     u8 isWriteLock = (u8)pOp->p3;
 66125  67731     if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
 66126  67732       int p1 = pOp->p1; 
 66127  67733       assert( p1>=0 && p1<db->nDb );
 66128         -    assert( (p->btreeMask & (1<<p1))!=0 );
        67734  +    assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
 66129  67735       assert( isWriteLock==0 || isWriteLock==1 );
 66130  67736       rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
 66131  67737       if( (rc&0xFF)==SQLITE_LOCKED ){
 66132  67738         const char *z = pOp->p4.z;
 66133  67739         sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
 66134  67740       }
 66135  67741     }
................................................................................
 66144  67750   ** xBegin method for that table.
 66145  67751   **
 66146  67752   ** Also, whether or not P4 is set, check that this is not being called from
 66147  67753   ** within a callback to a virtual table xSync() method. If it is, the error
 66148  67754   ** code will be set to SQLITE_LOCKED.
 66149  67755   */
 66150  67756   case OP_VBegin: {
 66151         -#if 0  /* local variables moved into u.cf */
        67757  +#if 0  /* local variables moved into u.cg */
 66152  67758     VTable *pVTab;
 66153         -#endif /* local variables moved into u.cf */
 66154         -  u.cf.pVTab = pOp->p4.pVtab;
 66155         -  rc = sqlite3VtabBegin(db, u.cf.pVTab);
 66156         -  if( u.cf.pVTab ) importVtabErrMsg(p, u.cf.pVTab->pVtab);
        67759  +#endif /* local variables moved into u.cg */
        67760  +  u.cg.pVTab = pOp->p4.pVtab;
        67761  +  rc = sqlite3VtabBegin(db, u.cg.pVTab);
        67762  +  if( u.cg.pVTab ) importVtabErrMsg(p, u.cg.pVTab->pVtab);
 66157  67763     break;
 66158  67764   }
 66159  67765   #endif /* SQLITE_OMIT_VIRTUALTABLE */
 66160  67766   
 66161  67767   #ifndef SQLITE_OMIT_VIRTUALTABLE
 66162  67768   /* Opcode: VCreate P1 * * P4 *
 66163  67769   **
................................................................................
 66188  67794   /* Opcode: VOpen P1 * * P4 *
 66189  67795   **
 66190  67796   ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
 66191  67797   ** P1 is a cursor number.  This opcode opens a cursor to the virtual
 66192  67798   ** table and stores that cursor in P1.
 66193  67799   */
 66194  67800   case OP_VOpen: {
 66195         -#if 0  /* local variables moved into u.cg */
        67801  +#if 0  /* local variables moved into u.ch */
 66196  67802     VdbeCursor *pCur;
 66197  67803     sqlite3_vtab_cursor *pVtabCursor;
 66198  67804     sqlite3_vtab *pVtab;
 66199  67805     sqlite3_module *pModule;
 66200         -#endif /* local variables moved into u.cg */
        67806  +#endif /* local variables moved into u.ch */
 66201  67807   
 66202         -  u.cg.pCur = 0;
 66203         -  u.cg.pVtabCursor = 0;
 66204         -  u.cg.pVtab = pOp->p4.pVtab->pVtab;
 66205         -  u.cg.pModule = (sqlite3_module *)u.cg.pVtab->pModule;
 66206         -  assert(u.cg.pVtab && u.cg.pModule);
 66207         -  rc = u.cg.pModule->xOpen(u.cg.pVtab, &u.cg.pVtabCursor);
 66208         -  importVtabErrMsg(p, u.cg.pVtab);
        67808  +  u.ch.pCur = 0;
        67809  +  u.ch.pVtabCursor = 0;
        67810  +  u.ch.pVtab = pOp->p4.pVtab->pVtab;
        67811  +  u.ch.pModule = (sqlite3_module *)u.ch.pVtab->pModule;
        67812  +  assert(u.ch.pVtab && u.ch.pModule);
        67813  +  rc = u.ch.pModule->xOpen(u.ch.pVtab, &u.ch.pVtabCursor);
        67814  +  importVtabErrMsg(p, u.ch.pVtab);
 66209  67815     if( SQLITE_OK==rc ){
 66210  67816       /* Initialize sqlite3_vtab_cursor base class */
 66211         -    u.cg.pVtabCursor->pVtab = u.cg.pVtab;
        67817  +    u.ch.pVtabCursor->pVtab = u.ch.pVtab;
 66212  67818   
 66213  67819       /* Initialise vdbe cursor object */
 66214         -    u.cg.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
 66215         -    if( u.cg.pCur ){
 66216         -      u.cg.pCur->pVtabCursor = u.cg.pVtabCursor;
 66217         -      u.cg.pCur->pModule = u.cg.pVtabCursor->pVtab->pModule;
        67820  +    u.ch.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
        67821  +    if( u.ch.pCur ){
        67822  +      u.ch.pCur->pVtabCursor = u.ch.pVtabCursor;
        67823  +      u.ch.pCur->pModule = u.ch.pVtabCursor->pVtab->pModule;
 66218  67824       }else{
 66219  67825         db->mallocFailed = 1;
 66220         -      u.cg.pModule->xClose(u.cg.pVtabCursor);
        67826  +      u.ch.pModule->xClose(u.ch.pVtabCursor);
 66221  67827       }
 66222  67828     }
 66223  67829     break;
 66224  67830   }
 66225  67831   #endif /* SQLITE_OMIT_VIRTUALTABLE */
 66226  67832   
 66227  67833   #ifndef SQLITE_OMIT_VIRTUALTABLE
................................................................................
 66240  67846   ** xFilter method. Registers P3+2..P3+1+argc are the argc
 66241  67847   ** additional parameters which are passed to
 66242  67848   ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
 66243  67849   **
 66244  67850   ** A jump is made to P2 if the result set after filtering would be empty.
 66245  67851   */
 66246  67852   case OP_VFilter: {   /* jump */
 66247         -#if 0  /* local variables moved into u.ch */
        67853  +#if 0  /* local variables moved into u.ci */
 66248  67854     int nArg;
 66249  67855     int iQuery;
 66250  67856     const sqlite3_module *pModule;
 66251  67857     Mem *pQuery;
 66252  67858     Mem *pArgc;
 66253  67859     sqlite3_vtab_cursor *pVtabCursor;
 66254  67860     sqlite3_vtab *pVtab;
 66255  67861     VdbeCursor *pCur;
 66256  67862     int res;
 66257  67863     int i;
 66258  67864     Mem **apArg;
 66259         -#endif /* local variables moved into u.ch */
        67865  +#endif /* local variables moved into u.ci */
 66260  67866   
 66261         -  u.ch.pQuery = &aMem[pOp->p3];
 66262         -  u.ch.pArgc = &u.ch.pQuery[1];
 66263         -  u.ch.pCur = p->apCsr[pOp->p1];
 66264         -  assert( memIsValid(u.ch.pQuery) );
 66265         -  REGISTER_TRACE(pOp->p3, u.ch.pQuery);
 66266         -  assert( u.ch.pCur->pVtabCursor );
 66267         -  u.ch.pVtabCursor = u.ch.pCur->pVtabCursor;
 66268         -  u.ch.pVtab = u.ch.pVtabCursor->pVtab;
 66269         -  u.ch.pModule = u.ch.pVtab->pModule;
        67867  +  u.ci.pQuery = &aMem[pOp->p3];
        67868  +  u.ci.pArgc = &u.ci.pQuery[1];
        67869  +  u.ci.pCur = p->apCsr[pOp->p1];
        67870  +  assert( memIsValid(u.ci.pQuery) );
        67871  +  REGISTER_TRACE(pOp->p3, u.ci.pQuery);
        67872  +  assert( u.ci.pCur->pVtabCursor );
        67873  +  u.ci.pVtabCursor = u.ci.pCur->pVtabCursor;
        67874  +  u.ci.pVtab = u.ci.pVtabCursor->pVtab;
        67875  +  u.ci.pModule = u.ci.pVtab->pModule;
 66270  67876   
 66271  67877     /* Grab the index number and argc parameters */
 66272         -  assert( (u.ch.pQuery->flags&MEM_Int)!=0 && u.ch.pArgc->flags==MEM_Int );
 66273         -  u.ch.nArg = (int)u.ch.pArgc->u.i;
 66274         -  u.ch.iQuery = (int)u.ch.pQuery->u.i;
        67878  +  assert( (u.ci.pQuery->flags&MEM_Int)!=0 && u.ci.pArgc->flags==MEM_Int );
        67879  +  u.ci.nArg = (int)u.ci.pArgc->u.i;
        67880  +  u.ci.iQuery = (int)u.ci.pQuery->u.i;
 66275  67881   
 66276  67882     /* Invoke the xFilter method */
 66277  67883     {
 66278         -    u.ch.res = 0;
 66279         -    u.ch.apArg = p->apArg;
 66280         -    for(u.ch.i = 0; u.ch.i<u.ch.nArg; u.ch.i++){
 66281         -      u.ch.apArg[u.ch.i] = &u.ch.pArgc[u.ch.i+1];
 66282         -      sqlite3VdbeMemStoreType(u.ch.apArg[u.ch.i]);
        67884  +    u.ci.res = 0;
        67885  +    u.ci.apArg = p->apArg;
        67886  +    for(u.ci.i = 0; u.ci.i<u.ci.nArg; u.ci.i++){
        67887  +      u.ci.apArg[u.ci.i] = &u.ci.pArgc[u.ci.i+1];
        67888  +      sqlite3VdbeMemStoreType(u.ci.apArg[u.ci.i]);
 66283  67889       }
 66284  67890   
 66285  67891       p->inVtabMethod = 1;
 66286         -    rc = u.ch.pModule->xFilter(u.ch.pVtabCursor, u.ch.iQuery, pOp->p4.z, u.ch.nArg, u.ch.apArg);
        67892  +    rc = u.ci.pModule->xFilter(u.ci.pVtabCursor, u.ci.iQuery, pOp->p4.z, u.ci.nArg, u.ci.apArg);
 66287  67893       p->inVtabMethod = 0;
 66288         -    importVtabErrMsg(p, u.ch.pVtab);
        67894  +    importVtabErrMsg(p, u.ci.pVtab);
 66289  67895       if( rc==SQLITE_OK ){
 66290         -      u.ch.res = u.ch.pModule->xEof(u.ch.pVtabCursor);
        67896  +      u.ci.res = u.ci.pModule->xEof(u.ci.pVtabCursor);
 66291  67897       }
 66292  67898   
 66293         -    if( u.ch.res ){
        67899  +    if( u.ci.res ){
 66294  67900         pc = pOp->p2 - 1;
 66295  67901       }
 66296  67902     }
 66297         -  u.ch.pCur->nullRow = 0;
        67903  +  u.ci.pCur->nullRow = 0;
 66298  67904   
 66299  67905     break;
 66300  67906   }
 66301  67907   #endif /* SQLITE_OMIT_VIRTUALTABLE */
 66302  67908   
 66303  67909   #ifndef SQLITE_OMIT_VIRTUALTABLE
 66304  67910   /* Opcode: VColumn P1 P2 P3 * *
 66305  67911   **
 66306  67912   ** Store the value of the P2-th column of
 66307  67913   ** the row of the virtual-table that the 
 66308  67914   ** P1 cursor is pointing to into register P3.
 66309  67915   */
 66310  67916   case OP_VColumn: {
 66311         -#if 0  /* local variables moved into u.ci */
        67917  +#if 0  /* local variables moved into u.cj */
 66312  67918     sqlite3_vtab *pVtab;
 66313  67919     const sqlite3_module *pModule;
 66314  67920     Mem *pDest;
 66315  67921     sqlite3_context sContext;
 66316         -#endif /* local variables moved into u.ci */
        67922  +#endif /* local variables moved into u.cj */
 66317  67923   
 66318  67924     VdbeCursor *pCur = p->apCsr[pOp->p1];
 66319  67925     assert( pCur->pVtabCursor );
 66320  67926     assert( pOp->p3>0 && pOp->p3<=p->nMem );
 66321         -  u.ci.pDest = &aMem[pOp->p3];
 66322         -  memAboutToChange(p, u.ci.pDest);
        67927  +  u.cj.pDest = &aMem[pOp->p3];
        67928  +  memAboutToChange(p, u.cj.pDest);
 66323  67929     if( pCur->nullRow ){
 66324         -    sqlite3VdbeMemSetNull(u.ci.pDest);
        67930  +    sqlite3VdbeMemSetNull(u.cj.pDest);
 66325  67931       break;
 66326  67932     }
 66327         -  u.ci.pVtab = pCur->pVtabCursor->pVtab;
 66328         -  u.ci.pModule = u.ci.pVtab->pModule;
 66329         -  assert( u.ci.pModule->xColumn );
 66330         -  memset(&u.ci.sContext, 0, sizeof(u.ci.sContext));
        67933  +  u.cj.pVtab = pCur->pVtabCursor->pVtab;
        67934  +  u.cj.pModule = u.cj.pVtab->pModule;
        67935  +  assert( u.cj.pModule->xColumn );
        67936  +  memset(&u.cj.sContext, 0, sizeof(u.cj.sContext));
 66331  67937   
 66332  67938     /* The output cell may already have a buffer allocated. Move
 66333         -  ** the current contents to u.ci.sContext.s so in case the user-function
        67939  +  ** the current contents to u.cj.sContext.s so in case the user-function
 66334  67940     ** can use the already allocated buffer instead of allocating a
 66335  67941     ** new one.
 66336  67942     */
 66337         -  sqlite3VdbeMemMove(&u.ci.sContext.s, u.ci.pDest);
 66338         -  MemSetTypeFlag(&u.ci.sContext.s, MEM_Null);
        67943  +  sqlite3VdbeMemMove(&u.cj.sContext.s, u.cj.pDest);
        67944  +  MemSetTypeFlag(&u.cj.sContext.s, MEM_Null);
 66339  67945   
 66340         -  rc = u.ci.pModule->xColumn(pCur->pVtabCursor, &u.ci.sContext, pOp->p2);
 66341         -  importVtabErrMsg(p, u.ci.pVtab);
 66342         -  if( u.ci.sContext.isError ){
 66343         -    rc = u.ci.sContext.isError;
        67946  +  rc = u.cj.pModule->xColumn(pCur->pVtabCursor, &u.cj.sContext, pOp->p2);
        67947  +  importVtabErrMsg(p, u.cj.pVtab);
        67948  +  if( u.cj.sContext.isError ){
        67949  +    rc = u.cj.sContext.isError;
 66344  67950     }
 66345  67951   
 66346  67952     /* Copy the result of the function to the P3 register. We
 66347  67953     ** do this regardless of whether or not an error occurred to ensure any
 66348         -  ** dynamic allocation in u.ci.sContext.s (a Mem struct) is  released.
        67954  +  ** dynamic allocation in u.cj.sContext.s (a Mem struct) is  released.
 66349  67955     */
 66350         -  sqlite3VdbeChangeEncoding(&u.ci.sContext.s, encoding);
 66351         -  sqlite3VdbeMemMove(u.ci.pDest, &u.ci.sContext.s);
 66352         -  REGISTER_TRACE(pOp->p3, u.ci.pDest);
 66353         -  UPDATE_MAX_BLOBSIZE(u.ci.pDest);
        67956  +  sqlite3VdbeChangeEncoding(&u.cj.sContext.s, encoding);
        67957  +  sqlite3VdbeMemMove(u.cj.pDest, &u.cj.sContext.s);
        67958  +  REGISTER_TRACE(pOp->p3, u.cj.pDest);
        67959  +  UPDATE_MAX_BLOBSIZE(u.cj.pDest);
 66354  67960   
 66355         -  if( sqlite3VdbeMemTooBig(u.ci.pDest) ){
        67961  +  if( sqlite3VdbeMemTooBig(u.cj.pDest) ){
 66356  67962       goto too_big;
 66357  67963     }
 66358  67964     break;
 66359  67965   }
 66360  67966   #endif /* SQLITE_OMIT_VIRTUALTABLE */
 66361  67967   
 66362  67968   #ifndef SQLITE_OMIT_VIRTUALTABLE
................................................................................
 66363  67969   /* Opcode: VNext P1 P2 * * *
 66364  67970   **
 66365  67971   ** Advance virtual table P1 to the next row in its result set and
 66366  67972   ** jump to instruction P2.  Or, if the virtual table has reached
 66367  67973   ** the end of its result set, then fall through to the next instruction.
 66368  67974   */
 66369  67975   case OP_VNext: {   /* jump */
 66370         -#if 0  /* local variables moved into u.cj */
        67976  +#if 0  /* local variables moved into u.ck */
 66371  67977     sqlite3_vtab *pVtab;
 66372  67978     const sqlite3_module *pModule;
 66373  67979     int res;
 66374  67980     VdbeCursor *pCur;
 66375         -#endif /* local variables moved into u.cj */
        67981  +#endif /* local variables moved into u.ck */
 66376  67982   
 66377         -  u.cj.res = 0;
 66378         -  u.cj.pCur = p->apCsr[pOp->p1];
 66379         -  assert( u.cj.pCur->pVtabCursor );
 66380         -  if( u.cj.pCur->nullRow ){
        67983  +  u.ck.res = 0;
        67984  +  u.ck.pCur = p->apCsr[pOp->p1];
        67985  +  assert( u.ck.pCur->pVtabCursor );
        67986  +  if( u.ck.pCur->nullRow ){
 66381  67987       break;
 66382  67988     }
 66383         -  u.cj.pVtab = u.cj.pCur->pVtabCursor->pVtab;
 66384         -  u.cj.pModule = u.cj.pVtab->pModule;
 66385         -  assert( u.cj.pModule->xNext );
        67989  +  u.ck.pVtab = u.ck.pCur->pVtabCursor->pVtab;
        67990  +  u.ck.pModule = u.ck.pVtab->pModule;
        67991  +  assert( u.ck.pModule->xNext );
 66386  67992   
 66387  67993     /* Invoke the xNext() method of the module. There is no way for the
 66388  67994     ** underlying implementation to return an error if one occurs during
 66389  67995     ** xNext(). Instead, if an error occurs, true is returned (indicating that
 66390  67996     ** data is available) and the error code returned when xColumn or
 66391  67997     ** some other method is next invoked on the save virtual table cursor.
 66392  67998     */
 66393  67999     p->inVtabMethod = 1;
 66394         -  rc = u.cj.pModule->xNext(u.cj.pCur->pVtabCursor);
        68000  +  rc = u.ck.pModule->xNext(u.ck.pCur->pVtabCursor);
 66395  68001     p->inVtabMethod = 0;
 66396         -  importVtabErrMsg(p, u.cj.pVtab);
        68002  +  importVtabErrMsg(p, u.ck.pVtab);
 66397  68003     if( rc==SQLITE_OK ){
 66398         -    u.cj.res = u.cj.pModule->xEof(u.cj.pCur->pVtabCursor);
        68004  +    u.ck.res = u.ck.pModule->xEof(u.ck.pCur->pVtabCursor);
 66399  68005     }
 66400  68006   
 66401         -  if( !u.cj.res ){
        68007  +  if( !u.ck.res ){
 66402  68008       /* If there is data, jump to P2 */
 66403  68009       pc = pOp->p2 - 1;
 66404  68010     }
 66405  68011     break;
 66406  68012   }
 66407  68013   #endif /* SQLITE_OMIT_VIRTUALTABLE */
 66408  68014   
................................................................................
 66410  68016   /* Opcode: VRename P1 * * P4 *
 66411  68017   **
 66412  68018   ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
 66413  68019   ** This opcode invokes the corresponding xRename method. The value
 66414  68020   ** in register P1 is passed as the zName argument to the xRename method.
 66415  68021   */
 66416  68022   case OP_VRename: {
 66417         -#if 0  /* local variables moved into u.ck */
        68023  +#if 0  /* local variables moved into u.cl */
 66418  68024     sqlite3_vtab *pVtab;
 66419  68025     Mem *pName;
 66420         -#endif /* local variables moved into u.ck */
        68026  +#endif /* local variables moved into u.cl */
 66421  68027   
 66422         -  u.ck.pVtab = pOp->p4.pVtab->pVtab;
 66423         -  u.ck.pName = &aMem[pOp->p1];
 66424         -  assert( u.ck.pVtab->pModule->xRename );
 66425         -  assert( memIsValid(u.ck.pName) );
 66426         -  REGISTER_TRACE(pOp->p1, u.ck.pName);
 66427         -  assert( u.ck.pName->flags & MEM_Str );
 66428         -  rc = u.ck.pVtab->pModule->xRename(u.ck.pVtab, u.ck.pName->z);
 66429         -  importVtabErrMsg(p, u.ck.pVtab);
        68028  +  u.cl.pVtab = pOp->p4.pVtab->pVtab;
        68029  +  u.cl.pName = &aMem[pOp->p1];
        68030  +  assert( u.cl.pVtab->pModule->xRename );
        68031  +  assert( memIsValid(u.cl.pName) );
        68032  +  REGISTER_TRACE(pOp->p1, u.cl.pName);
        68033  +  assert( u.cl.pName->flags & MEM_Str );
        68034  +  rc = u.cl.pVtab->pModule->xRename(u.cl.pVtab, u.cl.pName->z);
        68035  +  importVtabErrMsg(p, u.cl.pVtab);
 66430  68036     p->expired = 0;
 66431  68037   
 66432  68038     break;
 66433  68039   }
 66434  68040   #endif
 66435  68041   
 66436  68042   #ifndef SQLITE_OMIT_VIRTUALTABLE
................................................................................
 66454  68060   ** a row to delete.
 66455  68061   **
 66456  68062   ** P1 is a boolean flag. If it is set to true and the xUpdate call
 66457  68063   ** is successful, then the value returned by sqlite3_last_insert_rowid() 
 66458  68064   ** is set to the value of the rowid for the row just inserted.
 66459  68065   */
 66460  68066   case OP_VUpdate: {
 66461         -#if 0  /* local variables moved into u.cl */
        68067  +#if 0  /* local variables moved into u.cm */
 66462  68068     sqlite3_vtab *pVtab;
 66463  68069     sqlite3_module *pModule;
 66464  68070     int nArg;
 66465  68071     int i;
 66466  68072     sqlite_int64 rowid;
 66467  68073     Mem **apArg;
 66468  68074     Mem *pX;
 66469         -#endif /* local variables moved into u.cl */
        68075  +#endif /* local variables moved into u.cm */
 66470  68076   
 66471         -  u.cl.pVtab = pOp->p4.pVtab->pVtab;
 66472         -  u.cl.pModule = (sqlite3_module *)u.cl.pVtab->pModule;
 66473         -  u.cl.nArg = pOp->p2;
        68077  +  u.cm.pVtab = pOp->p4.pVtab->pVtab;
        68078  +  u.cm.pModule = (sqlite3_module *)u.cm.pVtab->pModule;
        68079  +  u.cm.nArg = pOp->p2;
 66474  68080     assert( pOp->p4type==P4_VTAB );
 66475         -  if( ALWAYS(u.cl.pModule->xUpdate) ){
 66476         -    u.cl.apArg = p->apArg;
 66477         -    u.cl.pX = &aMem[pOp->p3];
 66478         -    for(u.cl.i=0; u.cl.i<u.cl.nArg; u.cl.i++){
 66479         -      assert( memIsValid(u.cl.pX) );
 66480         -      memAboutToChange(p, u.cl.pX);
 66481         -      sqlite3VdbeMemStoreType(u.cl.pX);
 66482         -      u.cl.apArg[u.cl.i] = u.cl.pX;
 66483         -      u.cl.pX++;
        68081  +  if( ALWAYS(u.cm.pModule->xUpdate) ){
        68082  +    u.cm.apArg = p->apArg;
        68083  +    u.cm.pX = &aMem[pOp->p3];
        68084  +    for(u.cm.i=0; u.cm.i<u.cm.nArg; u.cm.i++){
        68085  +      assert( memIsValid(u.cm.pX) );
        68086  +      memAboutToChange(p, u.cm.pX);
        68087  +      sqlite3VdbeMemStoreType(u.cm.pX);
        68088  +      u.cm.apArg[u.cm.i] = u.cm.pX;
        68089  +      u.cm.pX++;
 66484  68090       }
 66485         -    rc = u.cl.pModule->xUpdate(u.cl.pVtab, u.cl.nArg, u.cl.apArg, &u.cl.rowid);
 66486         -    importVtabErrMsg(p, u.cl.pVtab);
        68091  +    rc = u.cm.pModule->xUpdate(u.cm.pVtab, u.cm.nArg, u.cm.apArg, &u.cm.rowid);
        68092  +    importVtabErrMsg(p, u.cm.pVtab);
 66487  68093       if( rc==SQLITE_OK && pOp->p1 ){
 66488         -      assert( u.cl.nArg>1 && u.cl.apArg[0] && (u.cl.apArg[0]->flags&MEM_Null) );
 66489         -      db->lastRowid = u.cl.rowid;
        68094  +      assert( u.cm.nArg>1 && u.cm.apArg[0] && (u.cm.apArg[0]->flags&MEM_Null) );
        68095  +      db->lastRowid = u.cm.rowid;
 66490  68096       }
 66491  68097       p->nChange++;
 66492  68098     }
 66493  68099     break;
 66494  68100   }
 66495  68101   #endif /* SQLITE_OMIT_VIRTUALTABLE */
 66496  68102   
................................................................................
 66534  68140   #ifndef SQLITE_OMIT_TRACE
 66535  68141   /* Opcode: Trace * * * P4 *
 66536  68142   **
 66537  68143   ** If tracing is enabled (by the sqlite3_trace()) interface, then
 66538  68144   ** the UTF-8 string contained in P4 is emitted on the trace callback.
 66539  68145   */
 66540  68146   case OP_Trace: {
 66541         -#if 0  /* local variables moved into u.cm */
        68147  +#if 0  /* local variables moved into u.cn */
 66542  68148     char *zTrace;
 66543         -#endif /* local variables moved into u.cm */
        68149  +#endif /* local variables moved into u.cn */
 66544  68150   
 66545         -  u.cm.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
 66546         -  if( u.cm.zTrace ){
        68151  +  u.cn.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
        68152  +  if( u.cn.zTrace ){
 66547  68153       if( db->xTrace ){
 66548         -      char *z = sqlite3VdbeExpandSql(p, u.cm.zTrace);
        68154  +      char *z = sqlite3VdbeExpandSql(p, u.cn.zTrace);
 66549  68155         db->xTrace(db->pTraceArg, z);
 66550  68156         sqlite3DbFree(db, z);
 66551  68157       }
 66552  68158   #ifdef SQLITE_DEBUG
 66553  68159       if( (db->flags & SQLITE_SqlTrace)!=0 ){
 66554         -      sqlite3DebugPrintf("SQL-trace: %s\n", u.cm.zTrace);
        68160  +      sqlite3DebugPrintf("SQL-trace: %s\n", u.cn.zTrace);
 66555  68161       }
 66556  68162   #endif /* SQLITE_DEBUG */
 66557  68163     }
 66558  68164     break;
 66559  68165   }
 66560  68166   #endif
 66561  68167   
................................................................................
 66626  68232     p->rc = rc;
 66627  68233     testcase( sqlite3GlobalConfig.xLog!=0 );
 66628  68234     sqlite3_log(rc, "statement aborts at %d: [%s] %s", 
 66629  68235                      pc, p->zSql, p->zErrMsg);
 66630  68236     sqlite3VdbeHalt(p);
 66631  68237     if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
 66632  68238     rc = SQLITE_ERROR;
 66633         -  if( resetSchemaOnFault ) sqlite3ResetInternalSchema(db, 0);
        68239  +  if( resetSchemaOnFault>0 ){
        68240  +    sqlite3ResetInternalSchema(db, resetSchemaOnFault-1);
        68241  +  }
 66634  68242   
 66635  68243     /* This is the only way out of this procedure.  We have to
 66636  68244     ** release the mutexes on btrees that were acquired at the
 66637  68245     ** top. */
 66638  68246   vdbe_return:
 66639         -  sqlite3BtreeMutexArrayLeave(&p->aMutex);
        68247  +  sqlite3VdbeLeave(p);
 66640  68248     return rc;
 66641  68249   
 66642  68250     /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
 66643  68251     ** is encountered.
 66644  68252     */
 66645  68253   too_big:
 66646  68254     sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
................................................................................
 66941  68549         /* Configure the OP_Transaction */
 66942  68550         sqlite3VdbeChangeP1(v, 0, iDb);
 66943  68551         sqlite3VdbeChangeP2(v, 0, flags);
 66944  68552   
 66945  68553         /* Configure the OP_VerifyCookie */
 66946  68554         sqlite3VdbeChangeP1(v, 1, iDb);
 66947  68555         sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
        68556  +      sqlite3VdbeChangeP3(v, 1, pTab->pSchema->iGeneration);
 66948  68557   
 66949  68558         /* Make sure a mutex is held on the table to be accessed */
 66950  68559         sqlite3VdbeUsesBtree(v, iDb); 
 66951  68560   
 66952  68561         /* Configure the OP_TableLock instruction */
 66953  68562   #ifdef SQLITE_OMIT_SHARED_CACHE
 66954  68563         sqlite3VdbeChangeToNoop(v, 2, 1);
................................................................................
 69075  70684   /*
 69076  70685   ** Return the default collation sequence for the expression pExpr. If
 69077  70686   ** there is no default collation type, return 0.
 69078  70687   */
 69079  70688   SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
 69080  70689     CollSeq *pColl = 0;
 69081  70690     Expr *p = pExpr;
 69082         -  while( ALWAYS(p) ){
        70691  +  while( p ){
 69083  70692       int op;
 69084  70693       pColl = p->pColl;
 69085  70694       if( pColl ) break;
 69086  70695       op = p->op;
 69087  70696       if( p->pTab!=0 && (
 69088  70697           op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER || op==TK_TRIGGER
 69089  70698       )){
................................................................................
 69372  70981     int nExtra = 0;
 69373  70982     int iValue = 0;
 69374  70983   
 69375  70984     if( pToken ){
 69376  70985       if( op!=TK_INTEGER || pToken->z==0
 69377  70986             || sqlite3GetInt32(pToken->z, &iValue)==0 ){
 69378  70987         nExtra = pToken->n+1;
        70988  +      assert( iValue>=0 );
 69379  70989       }
 69380  70990     }
 69381  70991     pNew = sqlite3DbMallocZero(db, sizeof(Expr)+nExtra);
 69382  70992     if( pNew ){
 69383  70993       pNew->op = (u8)op;
 69384  70994       pNew->iAgg = -1;
 69385  70995       if( pToken ){
................................................................................
 69597  71207   }
 69598  71208   
 69599  71209   /*
 69600  71210   ** Recursively delete an expression tree.
 69601  71211   */
 69602  71212   SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
 69603  71213     if( p==0 ) return;
        71214  +  /* Sanity check: Assert that the IntValue is non-negative if it exists */
        71215  +  assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
 69604  71216     if( !ExprHasAnyProperty(p, EP_TokenOnly) ){
 69605  71217       sqlite3ExprDelete(db, p->pLeft);
 69606  71218       sqlite3ExprDelete(db, p->pRight);
 69607  71219       if( !ExprHasProperty(p, EP_Reduced) && (p->flags2 & EP2_MallocedToken)!=0 ){
 69608  71220         sqlite3DbFree(db, p->u.zToken);
 69609  71221       }
 69610  71222       if( ExprHasProperty(p, EP_xIsSelect) ){
................................................................................
 70181  71793   ** If the expression p codes a constant integer that is small enough
 70182  71794   ** to fit in a 32-bit integer, return 1 and put the value of the integer
 70183  71795   ** in *pValue.  If the expression is not an integer or if it is too big
 70184  71796   ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
 70185  71797   */
 70186  71798   SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){
 70187  71799     int rc = 0;
        71800  +
        71801  +  /* If an expression is an integer literal that fits in a signed 32-bit
        71802  +  ** integer, then the EP_IntValue flag will have already been set */
        71803  +  assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
        71804  +           || sqlite3GetInt32(p->u.zToken, &rc)==0 );
        71805  +
 70188  71806     if( p->flags & EP_IntValue ){
 70189  71807       *pValue = p->u.iValue;
 70190  71808       return 1;
 70191  71809     }
 70192  71810     switch( p->op ){
 70193         -    case TK_INTEGER: {
 70194         -      rc = sqlite3GetInt32(p->u.zToken, pValue);
 70195         -      assert( rc==0 );
 70196         -      break;
 70197         -    }
 70198  71811       case TK_UPLUS: {
 70199  71812         rc = sqlite3ExprIsInteger(p->pLeft, pValue);
 70200  71813         break;
 70201  71814       }
 70202  71815       case TK_UMINUS: {
 70203  71816         int v;
 70204  71817         if( sqlite3ExprIsInteger(p->pLeft, &v) ){
................................................................................
 70205  71818           *pValue = -v;
 70206  71819           rc = 1;
 70207  71820         }
 70208  71821         break;
 70209  71822       }
 70210  71823       default: break;
 70211  71824     }
 70212         -  if( rc ){
 70213         -    assert( ExprHasAnyProperty(p, EP_Reduced|EP_TokenOnly)
 70214         -               || (p->flags2 & EP2_MallocedToken)==0 );
 70215         -    p->op = TK_INTEGER;
 70216         -    p->flags |= EP_IntValue;
 70217         -    p->u.iValue = *pValue;
 70218         -  }
 70219  71825     return rc;
 70220  71826   }
 70221  71827   
 70222  71828   /*
 70223  71829   ** Return FALSE if there is no chance that the expression can be NULL.
 70224  71830   **
 70225  71831   ** If the expression might be NULL or if the expression is too complex
................................................................................
 70936  72542   **
 70937  72543   ** Expr.u.zToken is always UTF8 and zero-terminated.
 70938  72544   */
 70939  72545   static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
 70940  72546     Vdbe *v = pParse->pVdbe;
 70941  72547     if( pExpr->flags & EP_IntValue ){
 70942  72548       int i = pExpr->u.iValue;
        72549  +    assert( i>=0 );
 70943  72550       if( negFlag ) i = -i;
 70944  72551       sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
 70945  72552     }else{
 70946  72553       int c;
 70947  72554       i64 value;
 70948  72555       const char *z = pExpr->u.zToken;
 70949  72556       assert( z!=0 );
 70950  72557       c = sqlite3Atoi64(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
 70951  72558       if( c==0 || (c==2 && negFlag) ){
 70952  72559         char *zV;
 70953         -      if( negFlag ){ value = -value; }
        72560  +      if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
 70954  72561         zV = dup8bytes(v, (char*)&value);
 70955  72562         sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
 70956  72563       }else{
 70957  72564   #ifdef SQLITE_OMIT_FLOATING_POINT
 70958  72565         sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
 70959  72566   #else
 70960  72567         codeReal(v, z, negFlag, iMem);
................................................................................
 71330  72937   #endif
 71331  72938       case TK_VARIABLE: {
 71332  72939         assert( !ExprHasProperty(pExpr, EP_IntValue) );
 71333  72940         assert( pExpr->u.zToken!=0 );
 71334  72941         assert( pExpr->u.zToken[0]!=0 );
 71335  72942         sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
 71336  72943         if( pExpr->u.zToken[1]!=0 ){
 71337         -        sqlite3VdbeChangeP4(v, -1, pExpr->u.zToken, 0);
        72944  +        sqlite3VdbeChangeP4(v, -1, pExpr->u.zToken, P4_TRANSIENT);
 71338  72945         }
 71339  72946         break;
 71340  72947       }
 71341  72948       case TK_REGISTER: {
 71342  72949         inReg = pExpr->iTable;
 71343  72950         break;
 71344  72951       }
................................................................................
 72234  73841         break;
 72235  73842       }
 72236  73843       case TK_BETWEEN: {
 72237  73844         testcase( jumpIfNull==0 );
 72238  73845         exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
 72239  73846         break;
 72240  73847       }
        73848  +#ifndef SQLITE_OMIT_SUBQUERY
 72241  73849       case TK_IN: {
 72242  73850         int destIfFalse = sqlite3VdbeMakeLabel(v);
 72243  73851         int destIfNull = jumpIfNull ? dest : destIfFalse;
 72244  73852         sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
 72245  73853         sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
 72246  73854         sqlite3VdbeResolveLabel(v, destIfFalse);
 72247  73855         break;
 72248  73856       }
        73857  +#endif
 72249  73858       default: {
 72250  73859         r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
 72251  73860         sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
 72252  73861         testcase( regFree1==0 );
 72253  73862         testcase( jumpIfNull==0 );
 72254  73863         break;
 72255  73864       }
................................................................................
 72375  73984         break;
 72376  73985       }
 72377  73986       case TK_BETWEEN: {
 72378  73987         testcase( jumpIfNull==0 );
 72379  73988         exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull);
 72380  73989         break;
 72381  73990       }
        73991  +#ifndef SQLITE_OMIT_SUBQUERY
 72382  73992       case TK_IN: {
 72383  73993         if( jumpIfNull ){
 72384  73994           sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
 72385  73995         }else{
 72386  73996           int destIfNull = sqlite3VdbeMakeLabel(v);
 72387  73997           sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
 72388  73998           sqlite3VdbeResolveLabel(v, destIfNull);
 72389  73999         }
 72390  74000         break;
 72391  74001       }
        74002  +#endif
 72392  74003       default: {
 72393  74004         r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
 72394  74005         sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
 72395  74006         testcase( regFree1==0 );
 72396  74007         testcase( jumpIfNull==0 );
 72397  74008         break;
 72398  74009       }
................................................................................
 73107  74718     ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. 
 73108  74719     */
 73109  74720     if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
 73110  74721       sqlite3VdbeAddOp4(v, OP_ParseSchema, 1, 0, 0, zWhere, P4_DYNAMIC);
 73111  74722     }
 73112  74723   #endif
 73113  74724   }
        74725  +
        74726  +/*
        74727  +** Parameter zName is the name of a table that is about to be altered
        74728  +** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
        74729  +** If the table is a system table, this function leaves an error message
        74730  +** in pParse->zErr (system tables may not be altered) and returns non-zero.
        74731  +**
        74732  +** Or, if zName is not a system table, zero is returned.
        74733  +*/
        74734  +static int isSystemTable(Parse *pParse, const char *zName){
        74735  +  if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
        74736  +    sqlite3ErrorMsg(pParse, "table %s may not be altered", zName);
        74737  +    return 1;
        74738  +  }
        74739  +  return 0;
        74740  +}
 73114  74741   
 73115  74742   /*
 73116  74743   ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" 
 73117  74744   ** command. 
 73118  74745   */
 73119  74746   SQLITE_PRIVATE void sqlite3AlterRenameTable(
 73120  74747     Parse *pParse,            /* Parser context. */
................................................................................
 73158  74785           "there is already another table or index with this name: %s", zName);
 73159  74786       goto exit_rename_table;
 73160  74787     }
 73161  74788   
 73162  74789     /* Make sure it is not a system table being altered, or a reserved name
 73163  74790     ** that the table is being renamed to.
 73164  74791     */
 73165         -  if( sqlite3Strlen30(pTab->zName)>6 
 73166         -   && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7)
 73167         -  ){
 73168         -    sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
        74792  +  if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
 73169  74793       goto exit_rename_table;
 73170  74794     }
 73171         -  if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
 73172         -    goto exit_rename_table;
        74795  +  if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto
        74796  +    exit_rename_table;
 73173  74797     }
 73174  74798   
 73175  74799   #ifndef SQLITE_OMIT_VIEW
 73176  74800     if( pTab->pSelect ){
 73177  74801       sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
 73178  74802       goto exit_rename_table;
 73179  74803     }
................................................................................
 73497  75121   #endif
 73498  75122   
 73499  75123     /* Make sure this is not an attempt to ALTER a view. */
 73500  75124     if( pTab->pSelect ){
 73501  75125       sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
 73502  75126       goto exit_begin_add_column;
 73503  75127     }
        75128  +  if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
        75129  +    goto exit_begin_add_column;
        75130  +  }
 73504  75131   
 73505  75132     assert( pTab->addColOffset>0 );
 73506  75133     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 73507  75134   
 73508  75135     /* Put a copy of the Table struct in Parse.pNewTable for the
 73509  75136     ** sqlite3AddColumn() function and friends to modify.  But modify
 73510  75137     ** the name by adding an "sqlite_altertab_" prefix.  By adding this
................................................................................
 73584  75211   ** with the named table are deleted. If zWhere==0, then code is generated
 73585  75212   ** to delete all stat table entries.
 73586  75213   */
 73587  75214   static void openStatTable(
 73588  75215     Parse *pParse,          /* Parsing context */
 73589  75216     int iDb,                /* The database we are looking in */
 73590  75217     int iStatCur,           /* Open the sqlite_stat1 table on this cursor */
 73591         -  const char *zWhere      /* Delete entries associated with this table */
        75218  +  const char *zWhere,     /* Delete entries for this table or index */
        75219  +  const char *zWhereType  /* Either "tbl" or "idx" */
 73592  75220   ){
 73593  75221     static const struct {
 73594  75222       const char *zName;
 73595  75223       const char *zCols;
 73596  75224     } aTable[] = {
 73597  75225       { "sqlite_stat1", "tbl,idx,stat" },
 73598  75226   #ifdef SQLITE_ENABLE_STAT2
................................................................................
 73629  75257         /* The table already exists. If zWhere is not NULL, delete all entries 
 73630  75258         ** associated with the table zWhere. If zWhere is NULL, delete the
 73631  75259         ** entire contents of the table. */
 73632  75260         aRoot[i] = pStat->tnum;
 73633  75261         sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
 73634  75262         if( zWhere ){
 73635  75263           sqlite3NestedParse(pParse,
 73636         -           "DELETE FROM %Q.%s WHERE tbl=%Q", pDb->zName, zTab, zWhere
        75264  +           "DELETE FROM %Q.%s WHERE %s=%Q", pDb->zName, zTab, zWhereType, zWhere
 73637  75265           );
 73638  75266         }else{
 73639  75267           /* The sqlite_stat[12] table already exists.  Delete all rows. */
 73640  75268           sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
 73641  75269         }
 73642  75270       }
 73643  75271     }
................................................................................
 73653  75281   /*
 73654  75282   ** Generate code to do an analysis of all indices associated with
 73655  75283   ** a single table.
 73656  75284   */
 73657  75285   static void analyzeOneTable(
 73658  75286     Parse *pParse,   /* Parser context */
 73659  75287     Table *pTab,     /* Table whose indices are to be analyzed */
        75288  +  Index *pOnlyIdx, /* If not NULL, only analyze this one index */
 73660  75289     int iStatCur,    /* Index of VdbeCursor that writes the sqlite_stat1 table */
 73661  75290     int iMem         /* Available memory locations begin here */
 73662  75291   ){
 73663  75292     sqlite3 *db = pParse->db;    /* Database handle */
 73664  75293     Index *pIdx;                 /* An index to being analyzed */
 73665  75294     int iIdxCur;                 /* Cursor open on index being analyzed */
 73666  75295     Vdbe *v;                     /* The virtual machine being built up */
 73667  75296     int i;                       /* Loop counter */
 73668  75297     int topOfLoop;               /* The top of the loop */
 73669  75298     int endOfLoop;               /* The end of the loop */
 73670         -  int addr = 0;                /* The address of an instruction */
 73671         -  int jZeroRows = 0;           /* Jump from here if number of rows is zero */
        75299  +  int jZeroRows = -1;          /* Jump from here if number of rows is zero */
 73672  75300     int iDb;                     /* Index of database containing pTab */
 73673  75301     int regTabname = iMem++;     /* Register containing table name */
 73674  75302     int regIdxname = iMem++;     /* Register containing index name */
 73675  75303     int regSampleno = iMem++;    /* Register containing next sample number */
 73676  75304     int regCol = iMem++;         /* Content of a column analyzed table */
 73677  75305     int regRec = iMem++;         /* Register holding completed record */
 73678  75306     int regTemp = iMem++;        /* Temporary use register */
 73679  75307     int regRowid = iMem++;       /* Rowid for the inserted record */
 73680  75308   
 73681  75309   #ifdef SQLITE_ENABLE_STAT2
        75310  +  int addr = 0;                /* Instruction address */
 73682  75311     int regTemp2 = iMem++;       /* Temporary use register */
 73683  75312     int regSamplerecno = iMem++; /* Index of next sample to record */
 73684  75313     int regRecno = iMem++;       /* Current sample index */
 73685  75314     int regLast = iMem++;        /* Index of last sample to record */
 73686  75315     int regFirst = iMem++;       /* Index of first sample to record */
 73687  75316   #endif
 73688  75317   
................................................................................
 73697  75326     if( memcmp(pTab->zName, "sqlite_", 7)==0 ){
 73698  75327       /* Do not gather statistics on system tables */
 73699  75328       return;
 73700  75329     }
 73701  75330     assert( sqlite3BtreeHoldsAllMutexes(db) );
 73702  75331     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 73703  75332     assert( iDb>=0 );
        75333  +  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 73704  75334   #ifndef SQLITE_OMIT_AUTHORIZATION
 73705  75335     if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
 73706  75336         db->aDb[iDb].zName ) ){
 73707  75337       return;
 73708  75338     }
 73709  75339   #endif
 73710  75340   
 73711  75341     /* Establish a read-lock on the table at the shared-cache level. */
 73712  75342     sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
 73713  75343   
 73714  75344     iIdxCur = pParse->nTab++;
 73715  75345     sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
 73716  75346     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
 73717         -    int nCol = pIdx->nColumn;
 73718         -    KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
        75347  +    int nCol;
        75348  +    KeyInfo *pKey;
 73719  75349   
        75350  +    if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
        75351  +    nCol = pIdx->nColumn;
        75352  +    pKey = sqlite3IndexKeyinfo(pParse, pIdx);
 73720  75353       if( iMem+1+(nCol*2)>pParse->nMem ){
 73721  75354         pParse->nMem = iMem+1+(nCol*2);
 73722  75355       }
 73723  75356   
 73724  75357       /* Open a cursor to the index to be analyzed. */
 73725  75358       assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
 73726  75359       sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
................................................................................
 73869  75502       **        I = (K+D-1)/D
 73870  75503       **
 73871  75504       ** If K==0 then no entry is made into the sqlite_stat1 table.  
 73872  75505       ** If K>0 then it is always the case the D>0 so division by zero
 73873  75506       ** is never possible.
 73874  75507       */
 73875  75508       sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno);
 73876         -    if( jZeroRows==0 ){
        75509  +    if( jZeroRows<0 ){
 73877  75510         jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
 73878  75511       }
 73879  75512       for(i=0; i<nCol; i++){
 73880  75513         sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
 73881  75514         sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
 73882  75515         sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
 73883  75516         sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
................................................................................
 73895  75528     ** containing NULL as the index name and the row count as the content.
 73896  75529     */
 73897  75530     if( pTab->pIndex==0 ){
 73898  75531       sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
 73899  75532       VdbeComment((v, "%s", pTab->zName));
 73900  75533       sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno);
 73901  75534       sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
        75535  +    jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regSampleno);
 73902  75536     }else{
 73903         -    assert( jZeroRows>0 );
 73904         -    addr = sqlite3VdbeAddOp0(v, OP_Goto);
 73905  75537       sqlite3VdbeJumpHere(v, jZeroRows);
        75538  +    jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
 73906  75539     }
 73907  75540     sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
 73908  75541     sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
 73909  75542     sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
 73910  75543     sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
 73911  75544     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
 73912  75545     if( pParse->nMem<regRec ) pParse->nMem = regRec;
 73913         -  if( jZeroRows ){
 73914         -    sqlite3VdbeJumpHere(v, addr);
 73915         -  }
        75546  +  sqlite3VdbeJumpHere(v, jZeroRows);
 73916  75547   }
 73917  75548   
 73918  75549   /*
 73919  75550   ** Generate code that will cause the most recent index analysis to
 73920  75551   ** be loaded into internal hash tables where is can be used.
 73921  75552   */
 73922  75553   static void loadAnalysis(Parse *pParse, int iDb){
................................................................................
 73935  75566     HashElem *k;
 73936  75567     int iStatCur;
 73937  75568     int iMem;
 73938  75569   
 73939  75570     sqlite3BeginWriteOperation(pParse, 0, iDb);
 73940  75571     iStatCur = pParse->nTab;
 73941  75572     pParse->nTab += 2;
 73942         -  openStatTable(pParse, iDb, iStatCur, 0);
        75573  +  openStatTable(pParse, iDb, iStatCur, 0, 0);
 73943  75574     iMem = pParse->nMem+1;
        75575  +  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 73944  75576     for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
 73945  75577       Table *pTab = (Table*)sqliteHashData(k);
 73946         -    analyzeOneTable(pParse, pTab, iStatCur, iMem);
        75578  +    analyzeOneTable(pParse, pTab, 0, iStatCur, iMem);
 73947  75579     }
 73948  75580     loadAnalysis(pParse, iDb);
 73949  75581   }
 73950  75582   
 73951  75583   /*
 73952  75584   ** Generate code that will do an analysis of a single table in
 73953         -** a database.
        75585  +** a database.  If pOnlyIdx is not NULL then it is a single index
        75586  +** in pTab that should be analyzed.
 73954  75587   */
 73955         -static void analyzeTable(Parse *pParse, Table *pTab){
        75588  +static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
 73956  75589     int iDb;
 73957  75590     int iStatCur;
 73958  75591   
 73959  75592     assert( pTab!=0 );
 73960  75593     assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
 73961  75594     iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
 73962  75595     sqlite3BeginWriteOperation(pParse, 0, iDb);
 73963  75596     iStatCur = pParse->nTab;
 73964  75597     pParse->nTab += 2;
 73965         -  openStatTable(pParse, iDb, iStatCur, pTab->zName);
 73966         -  analyzeOneTable(pParse, pTab, iStatCur, pParse->nMem+1);
        75598  +  if( pOnlyIdx ){
        75599  +    openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
        75600  +  }else{
        75601  +    openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
        75602  +  }
        75603  +  analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur, pParse->nMem+1);
 73967  75604     loadAnalysis(pParse, iDb);
 73968  75605   }
 73969  75606   
 73970  75607   /*
 73971  75608   ** Generate code for the ANALYZE command.  The parser calls this routine
 73972  75609   ** when it recognizes an ANALYZE command.
 73973  75610   **
................................................................................
 73981  75618   */
 73982  75619   SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
 73983  75620     sqlite3 *db = pParse->db;
 73984  75621     int iDb;
 73985  75622     int i;
 73986  75623     char *z, *zDb;
 73987  75624     Table *pTab;
        75625  +  Index *pIdx;
 73988  75626     Token *pTableName;
 73989  75627   
 73990  75628     /* Read the database schema. If an error occurs, leave an error message
 73991  75629     ** and code in pParse and return NULL. */
 73992  75630     assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
 73993  75631     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 73994  75632       return;
................................................................................
 74005  75643       /* Form 2:  Analyze the database or table named */
 74006  75644       iDb = sqlite3FindDb(db, pName1);
 74007  75645       if( iDb>=0 ){
 74008  75646         analyzeDatabase(pParse, iDb);
 74009  75647       }else{
 74010  75648         z = sqlite3NameFromToken(db, pName1);
 74011  75649         if( z ){
 74012         -        pTab = sqlite3LocateTable(pParse, 0, z, 0);
        75650  +        if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){
        75651  +          analyzeTable(pParse, pIdx->pTable, pIdx);
        75652  +        }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){
        75653  +          analyzeTable(pParse, pTab, 0);
        75654  +        }
 74013  75655           sqlite3DbFree(db, z);
 74014         -        if( pTab ){
 74015         -          analyzeTable(pParse, pTab);
 74016         -        }
 74017  75656         }
 74018  75657       }
 74019  75658     }else{
 74020  75659       /* Form 3: Analyze the fully qualified table name */
 74021  75660       iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
 74022  75661       if( iDb>=0 ){
 74023  75662         zDb = db->aDb[iDb].zName;
 74024  75663         z = sqlite3NameFromToken(db, pTableName);
 74025  75664         if( z ){
 74026         -        pTab = sqlite3LocateTable(pParse, 0, z, zDb);
        75665  +        if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
        75666  +          analyzeTable(pParse, pIdx->pTable, pIdx);
        75667  +        }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
        75668  +          analyzeTable(pParse, pTab, 0);
        75669  +        }
 74027  75670           sqlite3DbFree(db, z);
 74028         -        if( pTab ){
 74029         -          analyzeTable(pParse, pTab);
 74030         -        }
 74031  75671         }
 74032  75672       }   
 74033  75673     }
 74034  75674   }
 74035  75675   
 74036  75676   /*
 74037  75677   ** Used to pass information from the analyzer reader through to the
................................................................................
 74085  75725         v = v*10 + c - '0';
 74086  75726         z++;
 74087  75727       }
 74088  75728       if( i==0 ) pTable->nRowEst = v;
 74089  75729       if( pIndex==0 ) break;
 74090  75730       pIndex->aiRowEst[i] = v;
 74091  75731       if( *z==' ' ) z++;
        75732  +    if( memcmp(z, "unordered", 10)==0 ){
        75733  +      pIndex->bUnordered = 1;
        75734  +      break;
        75735  +    }
 74092  75736     }
 74093  75737     return 0;
 74094  75738   }
 74095  75739   
 74096  75740   /*
 74097  75741   ** If the Index.aSample variable is not NULL, delete the aSample[] array
 74098  75742   ** and its contents.
................................................................................
 74139  75783     analysisInfo sInfo;
 74140  75784     HashElem *i;
 74141  75785     char *zSql;
 74142  75786     int rc;
 74143  75787   
 74144  75788     assert( iDb>=0 && iDb<db->nDb );
 74145  75789     assert( db->aDb[iDb].pBt!=0 );
 74146         -  assert( sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
 74147  75790   
 74148  75791     /* Clear any prior statistics */
        75792  +  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 74149  75793     for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
 74150  75794       Index *pIdx = sqliteHashData(i);
 74151  75795       sqlite3DefaultRowEst(pIdx);
 74152  75796       sqlite3DeleteIndexSamples(db, pIdx);
 74153  75797       pIdx->aSample = 0;
 74154  75798     }
 74155  75799   
................................................................................
 74430  76074           zKey = (char *)sqlite3_value_blob(argv[2]);
 74431  76075           rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
 74432  76076           break;
 74433  76077   
 74434  76078         case SQLITE_NULL:
 74435  76079           /* No key specified.  Use the key from the main database */
 74436  76080           sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
 74437         -        rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
        76081  +        if( nKey>0 || sqlite3BtreeGetReserve(db->aDb[0].pBt)>0 ){
        76082  +          rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
        76083  +        }
 74438  76084           break;
 74439  76085       }
 74440  76086     }
 74441  76087   #endif
 74442  76088   
 74443  76089     /* If the file was opened successfully, read the schema for the new database.
 74444  76090     ** If this fails, or if opening the file failed, then close the file and 
................................................................................
 74454  76100       int iDb = db->nDb - 1;
 74455  76101       assert( iDb>=2 );
 74456  76102       if( db->aDb[iDb].pBt ){
 74457  76103         sqlite3BtreeClose(db->aDb[iDb].pBt);
 74458  76104         db->aDb[iDb].pBt = 0;
 74459  76105         db->aDb[iDb].pSchema = 0;
 74460  76106       }
 74461         -    sqlite3ResetInternalSchema(db, 0);
        76107  +    sqlite3ResetInternalSchema(db, -1);
 74462  76108       db->nDb = iDb;
 74463  76109       if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
 74464  76110         db->mallocFailed = 1;
 74465  76111         sqlite3DbFree(db, zErrDyn);
 74466  76112         zErrDyn = sqlite3MPrintf(db, "out of memory");
 74467  76113       }else if( zErrDyn==0 ){
 74468  76114         zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
................................................................................
 74526  76172       sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
 74527  76173       goto detach_error;
 74528  76174     }
 74529  76175   
 74530  76176     sqlite3BtreeClose(pDb->pBt);
 74531  76177     pDb->pBt = 0;
 74532  76178     pDb->pSchema = 0;
 74533         -  sqlite3ResetInternalSchema(db, 0);
        76179  +  sqlite3ResetInternalSchema(db, -1);
 74534  76180     return;
 74535  76181   
 74536  76182   detach_error:
 74537  76183     sqlite3_result_error(context, zErr, -1);
 74538  76184   }
 74539  76185   
 74540  76186   /*
................................................................................
 74566  76212     ){
 74567  76213       pParse->nErr++;
 74568  76214       goto attach_end;
 74569  76215     }
 74570  76216   
 74571  76217   #ifndef SQLITE_OMIT_AUTHORIZATION
 74572  76218     if( pAuthArg ){
 74573         -    char *zAuthArg = pAuthArg->u.zToken;
 74574         -    if( NEVER(zAuthArg==0) ){
 74575         -      goto attach_end;
        76219  +    char *zAuthArg;
        76220  +    if( pAuthArg->op==TK_STRING ){
        76221  +      zAuthArg = pAuthArg->u.zToken;
        76222  +    }else{
        76223  +      zAuthArg = 0;
 74576  76224       }
 74577  76225       rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
 74578  76226       if(rc!=SQLITE_OK ){
 74579  76227         goto attach_end;
 74580  76228       }
 74581  76229     }
 74582  76230   #endif /* SQLITE_OMIT_AUTHORIZATION */
................................................................................
 75194  76842       /* The cookie mask contains one bit for each database file open.
 75195  76843       ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
 75196  76844       ** set for each database that is used.  Generate code to start a
 75197  76845       ** transaction on each used database and to verify the schema cookie
 75198  76846       ** on each used database.
 75199  76847       */
 75200  76848       if( pParse->cookieGoto>0 ){
 75201         -      u32 mask;
        76849  +      yDbMask mask;
 75202  76850         int iDb;
 75203  76851         sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
 75204  76852         for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
 75205  76853           if( (mask & pParse->cookieMask)==0 ) continue;
 75206  76854           sqlite3VdbeUsesBtree(v, iDb);
 75207  76855           sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
 75208  76856           if( db->init.busy==0 ){
 75209         -          sqlite3VdbeAddOp2(v,OP_VerifyCookie, iDb, pParse->cookieValue[iDb]);
        76857  +          assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
        76858  +          sqlite3VdbeAddOp3(v, OP_VerifyCookie,
        76859  +                            iDb, pParse->cookieValue[iDb],
        76860  +                            db->aDb[iDb].pSchema->iGeneration);
 75210  76861           }
 75211  76862         }
 75212  76863   #ifndef SQLITE_OMIT_VIRTUALTABLE
 75213  76864         {
 75214  76865           int i;
 75215  76866           for(i=0; i<pParse->nVtabLock; i++){
 75216  76867             char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
................................................................................
 75315  76966   */
 75316  76967   SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
 75317  76968     Table *p = 0;
 75318  76969     int i;
 75319  76970     int nName;
 75320  76971     assert( zName!=0 );
 75321  76972     nName = sqlite3Strlen30(zName);
        76973  +  /* All mutexes are required for schema access.  Make sure we hold them. */
        76974  +  assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
 75322  76975     for(i=OMIT_TEMPDB; i<db->nDb; i++){
 75323  76976       int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
 75324  76977       if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
        76978  +    assert( sqlite3SchemaMutexHeld(db, j, 0) );
 75325  76979       p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, nName);
 75326  76980       if( p ) break;
 75327  76981     }
 75328  76982     return p;
 75329  76983   }
 75330  76984   
 75331  76985   /*
................................................................................
 75377  77031   ** TEMP first, then MAIN, then any auxiliary databases added
 75378  77032   ** using the ATTACH command.
 75379  77033   */
 75380  77034   SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
 75381  77035     Index *p = 0;
 75382  77036     int i;
 75383  77037     int nName = sqlite3Strlen30(zName);
        77038  +  /* All mutexes are required for schema access.  Make sure we hold them. */
        77039  +  assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
 75384  77040     for(i=OMIT_TEMPDB; i<db->nDb; i++){
 75385  77041       int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
 75386  77042       Schema *pSchema = db->aDb[j].pSchema;
 75387  77043       assert( pSchema );
 75388  77044       if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
        77045  +    assert( sqlite3SchemaMutexHeld(db, j, 0) );
 75389  77046       p = sqlite3HashFind(&pSchema->idxHash, zName, nName);
 75390  77047       if( p ) break;
 75391  77048     }
 75392  77049     return p;
 75393  77050   }
 75394  77051   
 75395  77052   /*
................................................................................
 75408  77065   ** unlike that index from its Table then remove the index from
 75409  77066   ** the index hash table and free all memory structures associated
 75410  77067   ** with the index.
 75411  77068   */
 75412  77069   SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
 75413  77070     Index *pIndex;
 75414  77071     int len;
 75415         -  Hash *pHash = &db->aDb[iDb].pSchema->idxHash;
        77072  +  Hash *pHash;
 75416  77073   
        77074  +  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
        77075  +  pHash = &db->aDb[iDb].pSchema->idxHash;
 75417  77076     len = sqlite3Strlen30(zIdxName);
 75418  77077     pIndex = sqlite3HashInsert(pHash, zIdxName, len, 0);
 75419         -  if( pIndex ){
        77078  +  if( ALWAYS(pIndex) ){
 75420  77079       if( pIndex->pTable->pIndex==pIndex ){
 75421  77080         pIndex->pTable->pIndex = pIndex->pNext;
 75422  77081       }else{
 75423  77082         Index *p;
 75424  77083         /* Justification of ALWAYS();  The index must be on the list of
 75425  77084         ** indices. */
 75426  77085         p = pIndex->pTable->pIndex;
................................................................................
 75437  77096   /*
 75438  77097   ** Erase all schema information from the in-memory hash tables of
 75439  77098   ** a single database.  This routine is called to reclaim memory
 75440  77099   ** before the database closes.  It is also called during a rollback
 75441  77100   ** if there were schema changes during the transaction or if a
 75442  77101   ** schema-cookie mismatch occurs.
 75443  77102   **
 75444         -** If iDb==0 then reset the internal schema tables for all database
 75445         -** files.  If iDb>=1 then reset the internal schema for only the
        77103  +** If iDb<0 then reset the internal schema tables for all database
        77104  +** files.  If iDb>=0 then reset the internal schema for only the
 75446  77105   ** single file indicated.
 75447  77106   */
 75448  77107   SQLITE_PRIVATE void sqlite3ResetInternalSchema(sqlite3 *db, int iDb){
 75449  77108     int i, j;
 75450         -  assert( iDb>=0 && iDb<db->nDb );
        77109  +  assert( iDb<db->nDb );
 75451  77110   
 75452         -  if( iDb==0 ){
 75453         -    sqlite3BtreeEnterAll(db);
        77111  +  if( iDb>=0 ){
        77112  +    /* Case 1:  Reset the single schema identified by iDb */
        77113  +    Db *pDb = &db->aDb[iDb];
        77114  +    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
        77115  +    assert( pDb->pSchema!=0 );
        77116  +    sqlite3SchemaClear(pDb->pSchema);
        77117  +
        77118  +    /* If any database other than TEMP is reset, then also reset TEMP
        77119  +    ** since TEMP might be holding triggers that reference tables in the
        77120  +    ** other database.
        77121  +    */
        77122  +    if( iDb!=1 ){
        77123  +      pDb = &db->aDb[1];
        77124  +      assert( pDb->pSchema!=0 );
        77125  +      sqlite3SchemaClear(pDb->pSchema);
        77126  +    }
        77127  +    return;
 75454  77128     }
 75455         -  for(i=iDb; i<db->nDb; i++){
        77129  +  /* Case 2 (from here to the end): Reset all schemas for all attached
        77130  +  ** databases. */
        77131  +  assert( iDb<0 );
        77132  +  sqlite3BtreeEnterAll(db);
        77133  +  for(i=0; i<db->nDb; i++){
 75456  77134       Db *pDb = &db->aDb[i];
 75457  77135       if( pDb->pSchema ){
 75458         -      assert(i==1 || (pDb->pBt && sqlite3BtreeHoldsMutex(pDb->pBt)));
 75459         -      sqlite3SchemaFree(pDb->pSchema);
        77136  +      sqlite3SchemaClear(pDb->pSchema);
 75460  77137       }
 75461         -    if( iDb>0 ) return;
 75462  77138     }
 75463         -  assert( iDb==0 );
 75464  77139     db->flags &= ~SQLITE_InternChanges;
 75465  77140     sqlite3VtabUnlockList(db);
 75466  77141     sqlite3BtreeLeaveAll(db);
 75467  77142   
 75468  77143     /* If one or more of the auxiliary database files has been closed,
 75469  77144     ** then remove them from the auxiliary database list.  We take the
 75470  77145     ** opportunity to do this here since we have just deleted all of the
................................................................................
 75542  77217       pNext = pIndex->pNext;
 75543  77218       assert( pIndex->pSchema==pTable->pSchema );
 75544  77219       if( !db || db->pnBytesFreed==0 ){
 75545  77220         char *zName = pIndex->zName; 
 75546  77221         TESTONLY ( Index *pOld = ) sqlite3HashInsert(
 75547  77222   	  &pIndex->pSchema->idxHash, zName, sqlite3Strlen30(zName), 0
 75548  77223         );
        77224  +      assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
 75549  77225         assert( pOld==pIndex || pOld==0 );
 75550  77226       }
 75551  77227       freeIndex(db, pIndex);
 75552  77228     }
 75553  77229   
 75554  77230     /* Delete any foreign keys attached to this table. */
 75555  77231     sqlite3FkDelete(db, pTable);
................................................................................
 75576  77252   SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
 75577  77253     Table *p;
 75578  77254     Db *pDb;
 75579  77255   
 75580  77256     assert( db!=0 );
 75581  77257     assert( iDb>=0 && iDb<db->nDb );
 75582  77258     assert( zTabName );
        77259  +  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 75583  77260     testcase( zTabName[0]==0 );  /* Zero-length table names are allowed */
 75584  77261     pDb = &db->aDb[iDb];
 75585  77262     p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName,
 75586  77263                           sqlite3Strlen30(zTabName),0);
 75587  77264     sqlite3DeleteTable(db, p);
 75588  77265     db->flags |= SQLITE_InternChanges;
 75589  77266   }
................................................................................
 75830  77507       if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 75831  77508         goto begin_table_error;
 75832  77509       }
 75833  77510       pTable = sqlite3FindTable(db, zName, zDb);
 75834  77511       if( pTable ){
 75835  77512         if( !noErr ){
 75836  77513           sqlite3ErrorMsg(pParse, "table %T already exists", pName);
        77514  +      }else{
        77515  +        assert( !db->init.busy );
        77516  +        sqlite3CodeVerifySchema(pParse, iDb);
 75837  77517         }
 75838  77518         goto begin_table_error;
 75839  77519       }
 75840  77520       if( sqlite3FindIndex(db, zName, zDb)!=0 ){
 75841  77521         sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
 75842  77522         goto begin_table_error;
 75843  77523       }
................................................................................
 75860  77540   
 75861  77541     /* If this is the magic sqlite_sequence table used by autoincrement,
 75862  77542     ** then record a pointer to this table in the main database structure
 75863  77543     ** so that INSERT can find the table easily.
 75864  77544     */
 75865  77545   #ifndef SQLITE_OMIT_AUTOINCREMENT
 75866  77546     if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
        77547  +    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 75867  77548       pTable->pSchema->pSeqTab = pTable;
 75868  77549     }
 75869  77550   #endif
 75870  77551   
 75871  77552     /* Begin generating the code that will insert the table record into
 75872  77553     ** the SQLITE_MASTER table.  Note in particular that we must go ahead
 75873  77554     ** and allocate the record number for the table entry now.  Before any
................................................................................
 76320  78001   ** and the probability of hitting the same cookie value is only
 76321  78002   ** 1 chance in 2^32.  So we're safe enough.
 76322  78003   */
 76323  78004   SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
 76324  78005     int r1 = sqlite3GetTempReg(pParse);
 76325  78006     sqlite3 *db = pParse->db;
 76326  78007     Vdbe *v = pParse->pVdbe;
        78008  +  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 76327  78009     sqlite3VdbeAddOp2(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, r1);
 76328  78010     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, r1);
 76329  78011     sqlite3ReleaseTempReg(pParse, r1);
 76330  78012   }
 76331  78013   
 76332  78014   /*
 76333  78015   ** Measure the number of characters needed to output the given
................................................................................
 76427  78109       const char *zType;
 76428  78110   
 76429  78111       sqlite3_snprintf(n-k, &zStmt[k], zSep);
 76430  78112       k += sqlite3Strlen30(&zStmt[k]);
 76431  78113       zSep = zSep2;
 76432  78114       identPut(zStmt, &k, pCol->zName);
 76433  78115       assert( pCol->affinity-SQLITE_AFF_TEXT >= 0 );
 76434         -    assert( pCol->affinity-SQLITE_AFF_TEXT < sizeof(azType)/sizeof(azType[0]) );
        78116  +    assert( pCol->affinity-SQLITE_AFF_TEXT < ArraySize(azType) );
 76435  78117       testcase( pCol->affinity==SQLITE_AFF_TEXT );
 76436  78118       testcase( pCol->affinity==SQLITE_AFF_NONE );
 76437  78119       testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
 76438  78120       testcase( pCol->affinity==SQLITE_AFF_INTEGER );
 76439  78121       testcase( pCol->affinity==SQLITE_AFF_REAL );
 76440  78122       
 76441  78123       zType = azType[pCol->affinity - SQLITE_AFF_TEXT];
................................................................................
 76622  78304   
 76623  78305   #ifndef SQLITE_OMIT_AUTOINCREMENT
 76624  78306       /* Check to see if we need to create an sqlite_sequence table for
 76625  78307       ** keeping track of autoincrement keys.
 76626  78308       */
 76627  78309       if( p->tabFlags & TF_Autoincrement ){
 76628  78310         Db *pDb = &db->aDb[iDb];
        78311  +      assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 76629  78312         if( pDb->pSchema->pSeqTab==0 ){
 76630  78313           sqlite3NestedParse(pParse,
 76631  78314             "CREATE TABLE %Q.sqlite_sequence(name,seq)",
 76632  78315             pDb->zName
 76633  78316           );
 76634  78317         }
 76635  78318       }
................................................................................
 76642  78325   
 76643  78326   
 76644  78327     /* Add the table to the in-memory representation of the database.
 76645  78328     */
 76646  78329     if( db->init.busy ){
 76647  78330       Table *pOld;
 76648  78331       Schema *pSchema = p->pSchema;
        78332  +    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 76649  78333       pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName,
 76650  78334                                sqlite3Strlen30(p->zName),p);
 76651  78335       if( pOld ){
 76652  78336         assert( p==pOld );  /* Malloc must have failed inside HashInsert() */
 76653  78337         db->mallocFailed = 1;
 76654  78338         return;
 76655  78339       }
................................................................................
 76826  78510       if( pSelTab ){
 76827  78511         assert( pTable->aCol==0 );
 76828  78512         pTable->nCol = pSelTab->nCol;
 76829  78513         pTable->aCol = pSelTab->aCol;
 76830  78514         pSelTab->nCol = 0;
 76831  78515         pSelTab->aCol = 0;
 76832  78516         sqlite3DeleteTable(db, pSelTab);
        78517  +      assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
 76833  78518         pTable->pSchema->flags |= DB_UnresetViews;
 76834  78519       }else{
 76835  78520         pTable->nCol = 0;
 76836  78521         nErr++;
 76837  78522       }
 76838  78523       sqlite3SelectDelete(db, pSel);
 76839  78524     } else {
................................................................................
 76846  78531   
 76847  78532   #ifndef SQLITE_OMIT_VIEW
 76848  78533   /*
 76849  78534   ** Clear the column names from every VIEW in database idx.
 76850  78535   */
 76851  78536   static void sqliteViewResetAll(sqlite3 *db, int idx){
 76852  78537     HashElem *i;
        78538  +  assert( sqlite3SchemaMutexHeld(db, idx, 0) );
 76853  78539     if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
 76854  78540     for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
 76855  78541       Table *pTab = sqliteHashData(i);
 76856  78542       if( pTab->pSelect ){
 76857  78543         sqliteDeleteColumnNames(db, pTab);
 76858  78544         pTab->aCol = 0;
 76859  78545         pTab->nCol = 0;
................................................................................
 76879  78565   ** because the first match might be for one of the deleted indices
 76880  78566   ** or tables and not the table/index that is actually being moved.
 76881  78567   ** We must continue looping until all tables and indices with
 76882  78568   ** rootpage==iFrom have been converted to have a rootpage of iTo
 76883  78569   ** in order to be certain that we got the right one.
 76884  78570   */
 76885  78571   #ifndef SQLITE_OMIT_AUTOVACUUM
 76886         -SQLITE_PRIVATE void sqlite3RootPageMoved(Db *pDb, int iFrom, int iTo){
        78572  +SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){
 76887  78573     HashElem *pElem;
 76888  78574     Hash *pHash;
        78575  +  Db *pDb;
 76889  78576   
        78577  +  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
        78578  +  pDb = &db->aDb[iDb];
 76890  78579     pHash = &pDb->pSchema->tblHash;
 76891  78580     for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
 76892  78581       Table *pTab = sqliteHashData(pElem);
 76893  78582       if( pTab->tnum==iFrom ){
 76894  78583         pTab->tnum = iTo;
 76895  78584       }
 76896  78585     }
................................................................................
 77008  78697     assert( pName->nSrc==1 );
 77009  78698     if( noErr ) db->suppressErr++;
 77010  78699     pTab = sqlite3LocateTable(pParse, isView, 
 77011  78700                               pName->a[0].zName, pName->a[0].zDatabase);
 77012  78701     if( noErr ) db->suppressErr--;
 77013  78702   
 77014  78703     if( pTab==0 ){
        78704  +    if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
 77015  78705       goto exit_drop_table;
 77016  78706     }
 77017  78707     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 77018  78708     assert( iDb>=0 && iDb<db->nDb );
 77019  78709   
 77020  78710     /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
 77021  78711     ** it is initialized.
................................................................................
 77256  78946         z += n+1;
 77257  78947       }
 77258  78948     }
 77259  78949     pFKey->isDeferred = 0;
 77260  78950     pFKey->aAction[0] = (u8)(flags & 0xff);            /* ON DELETE action */
 77261  78951     pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff);    /* ON UPDATE action */
 77262  78952   
        78953  +  assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
 77263  78954     pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, 
 77264  78955         pFKey->zTo, sqlite3Strlen30(pFKey->zTo), (void *)pFKey
 77265  78956     );
 77266  78957     if( pNextTo==pFKey ){
 77267  78958       db->mallocFailed = 1;
 77268  78959       goto fk_end;
 77269  78960     }
................................................................................
 77525  79216           sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
 77526  79217           goto exit_create_index;
 77527  79218         }
 77528  79219       }
 77529  79220       if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){
 77530  79221         if( !ifNotExist ){
 77531  79222           sqlite3ErrorMsg(pParse, "index %s already exists", zName);
        79223  +      }else{
        79224  +        assert( !db->init.busy );
        79225  +        sqlite3CodeVerifySchema(pParse, iDb);
 77532  79226         }
 77533  79227         goto exit_create_index;
 77534  79228       }
 77535  79229     }else{
 77536  79230       int n;
 77537  79231       Index *pLoop;
 77538  79232       for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
................................................................................
 77611  79305     zExtra = (char *)(&pIndex->zName[nName+1]);
 77612  79306     memcpy(pIndex->zName, zName, nName+1);
 77613  79307     pIndex->pTable = pTab;
 77614  79308     pIndex->nColumn = pList->nExpr;
 77615  79309     pIndex->onError = (u8)onError;
 77616  79310     pIndex->autoIndex = (u8)(pName==0);
 77617  79311     pIndex->pSchema = db->aDb[iDb].pSchema;
        79312  +  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 77618  79313   
 77619  79314     /* Check to see if we should honor DESC requests on index columns
 77620  79315     */
 77621  79316     if( pDb->pSchema->file_format>=4 ){
 77622  79317       sortOrderMask = -1;   /* Honor DESC */
 77623  79318     }else{
 77624  79319       sortOrderMask = 0;    /* Ignore DESC */
................................................................................
 77740  79435     }
 77741  79436   
 77742  79437     /* Link the new Index structure to its table and to the other
 77743  79438     ** in-memory database structures. 
 77744  79439     */
 77745  79440     if( db->init.busy ){
 77746  79441       Index *p;
        79442  +    assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
 77747  79443       p = sqlite3HashInsert(&pIndex->pSchema->idxHash, 
 77748  79444                             pIndex->zName, sqlite3Strlen30(pIndex->zName),
 77749  79445                             pIndex);
 77750  79446       if( p ){
 77751  79447         assert( p==pIndex );  /* Malloc must have failed */
 77752  79448         db->mallocFailed = 1;
 77753  79449         goto exit_create_index;
................................................................................
 77916  79612     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
 77917  79613       goto exit_drop_index;
 77918  79614     }
 77919  79615     pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
 77920  79616     if( pIndex==0 ){
 77921  79617       if( !ifExists ){
 77922  79618         sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
        79619  +    }else{
        79620  +      sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
 77923  79621       }
 77924  79622       pParse->checkSchema = 1;
 77925  79623       goto exit_drop_index;
 77926  79624     }
 77927  79625     if( pIndex->autoIndex ){
 77928  79626       sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
 77929  79627         "or PRIMARY KEY constraint cannot be dropped", 0);
................................................................................
 78488  80186     if( pToplevel->cookieGoto==0 ){
 78489  80187       Vdbe *v = sqlite3GetVdbe(pToplevel);
 78490  80188       if( v==0 ) return;  /* This only happens if there was a prior error */
 78491  80189       pToplevel->cookieGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0)+1;
 78492  80190     }
 78493  80191     if( iDb>=0 ){
 78494  80192       sqlite3 *db = pToplevel->db;
 78495         -    int mask;
        80193  +    yDbMask mask;
 78496  80194   
 78497  80195       assert( iDb<db->nDb );
 78498  80196       assert( db->aDb[iDb].pBt!=0 || iDb==1 );
 78499  80197       assert( iDb<SQLITE_MAX_ATTACHED+2 );
 78500         -    mask = 1<<iDb;
        80198  +    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
        80199  +    mask = ((yDbMask)1)<<iDb;
 78501  80200       if( (pToplevel->cookieMask & mask)==0 ){
 78502  80201         pToplevel->cookieMask |= mask;
 78503  80202         pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
 78504  80203         if( !OMIT_TEMPDB && iDb==1 ){
 78505  80204           sqlite3OpenTempDatabase(pToplevel);
 78506  80205         }
 78507  80206       }
 78508  80207     }
 78509  80208   }
        80209  +
        80210  +/*
        80211  +** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each 
        80212  +** attached database. Otherwise, invoke it for the database named zDb only.
        80213  +*/
        80214  +SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
        80215  +  sqlite3 *db = pParse->db;
        80216  +  int i;
        80217  +  for(i=0; i<db->nDb; i++){
        80218  +    Db *pDb = &db->aDb[i];
        80219  +    if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zName)) ){
        80220  +      sqlite3CodeVerifySchema(pParse, i);
        80221  +    }
        80222  +  }
        80223  +}
 78510  80224   
 78511  80225   /*
 78512  80226   ** Generate VDBE code that prepares for doing an operation that
 78513  80227   ** might change the database.
 78514  80228   **
 78515  80229   ** This routine starts a new transaction if we are not already within
 78516  80230   ** a transaction.  If we are already within a transaction, then a checkpoint
................................................................................
 78520  80234   ** rollback the whole transaction.  For operations where all constraints
 78521  80235   ** can be checked before any changes are made to the database, it is never
 78522  80236   ** necessary to undo a write and the checkpoint should not be set.
 78523  80237   */
 78524  80238   SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
 78525  80239     Parse *pToplevel = sqlite3ParseToplevel(pParse);
 78526  80240     sqlite3CodeVerifySchema(pParse, iDb);
 78527         -  pToplevel->writeMask |= 1<<iDb;
        80241  +  pToplevel->writeMask |= ((yDbMask)1)<<iDb;
 78528  80242     pToplevel->isMultiWrite |= setStatement;
 78529  80243   }
 78530  80244   
 78531  80245   /*
 78532  80246   ** Indicate that the statement currently under construction might write
 78533  80247   ** more than one entry (example: deleting one row then inserting another,
 78534  80248   ** inserting multiple rows in a table, or inserting a row and index entries.)
................................................................................
 78620  80334   static void reindexDatabases(Parse *pParse, char const *zColl){
 78621  80335     Db *pDb;                    /* A single database */
 78622  80336     int iDb;                    /* The database index number */
 78623  80337     sqlite3 *db = pParse->db;   /* The database connection */
 78624  80338     HashElem *k;                /* For looping over tables in pDb */
 78625  80339     Table *pTab;                /* A table in the database */
 78626  80340   
        80341  +  assert( sqlite3BtreeHoldsAllMutexes(db) );  /* Needed for schema access */
 78627  80342     for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
 78628  80343       assert( pDb!=0 );
 78629  80344       for(k=sqliteHashFirst(&pDb->pSchema->tblHash);  k; k=sqliteHashNext(k)){
 78630  80345         pTab = (Table*)sqliteHashData(k);
 78631  80346         reindexTable(pParse, pTab, zColl);
 78632  80347       }
 78633  80348     }
................................................................................
 79138  80853     }
 79139  80854     return 0;
 79140  80855   }
 79141  80856   
 79142  80857   /*
 79143  80858   ** Free all resources held by the schema structure. The void* argument points
 79144  80859   ** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the 
 79145         -** pointer itself, it just cleans up subsiduary resources (i.e. the contents
        80860  +** pointer itself, it just cleans up subsidiary resources (i.e. the contents
 79146  80861   ** of the schema hash tables).
 79147  80862   **
 79148  80863   ** The Schema.cache_size variable is not cleared.
 79149  80864   */
 79150         -SQLITE_PRIVATE void sqlite3SchemaFree(void *p){
        80865  +SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
 79151  80866     Hash temp1;
 79152  80867     Hash temp2;
 79153  80868     HashElem *pElem;
 79154  80869     Schema *pSchema = (Schema *)p;
 79155  80870   
 79156  80871     temp1 = pSchema->tblHash;
 79157  80872     temp2 = pSchema->trigHash;
................................................................................
 79165  80880     for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
 79166  80881       Table *pTab = sqliteHashData(pElem);
 79167  80882       sqlite3DeleteTable(0, pTab);
 79168  80883     }
 79169  80884     sqlite3HashClear(&temp1);
 79170  80885     sqlite3HashClear(&pSchema->fkeyHash);
 79171  80886     pSchema->pSeqTab = 0;
 79172         -  pSchema->flags &= ~DB_SchemaLoaded;
        80887  +  if( pSchema->flags & DB_SchemaLoaded ){
        80888  +    pSchema->iGeneration++;
        80889  +    pSchema->flags &= ~DB_SchemaLoaded;
        80890  +  }
 79173  80891   }
 79174  80892   
 79175  80893   /*
 79176  80894   ** Find and return the schema associated with a BTree.  Create
 79177  80895   ** a new one if necessary.
 79178  80896   */
 79179  80897   SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
 79180  80898     Schema * p;
 79181  80899     if( pBt ){
 79182         -    p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaFree);
        80900  +    p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
 79183  80901     }else{
 79184  80902       p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
 79185  80903     }
 79186  80904     if( !p ){
 79187  80905       db->mallocFailed = 1;
 79188  80906     }else if ( 0==p->file_format ){
 79189  80907       sqlite3HashInit(&p->tblHash);
................................................................................
 79209  80927   **
 79210  80928   *************************************************************************
 79211  80929   ** This file contains C code routines that are called by the parser
 79212  80930   ** in order to generate code for DELETE FROM statements.
 79213  80931   */
 79214  80932   
 79215  80933   /*
 79216         -** Look up every table that is named in pSrc.  If any table is not found,
 79217         -** add an error message to pParse->zErrMsg and return NULL.  If all tables
 79218         -** are found, return a pointer to the last table.
        80934  +** While a SrcList can in general represent multiple tables and subqueries
        80935  +** (as in the FROM clause of a SELECT statement) in this case it contains
        80936  +** the name of a single table, as one might find in an INSERT, DELETE,
        80937  +** or UPDATE statement.  Look up that table in the symbol table and
        80938  +** return a pointer.  Set an error message and return NULL if the table 
        80939  +** name is not found or if any other error occurs.
        80940  +**
        80941  +** The following fields are initialized appropriate in pSrc:
        80942  +**
        80943  +**    pSrc->a[0].pTab       Pointer to the Table object
        80944  +**    pSrc->a[0].pIndex     Pointer to the INDEXED BY index, if there is one
        80945  +**
 79219  80946   */
 79220  80947   SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
 79221  80948     struct SrcList_item *pItem = pSrc->a;
 79222  80949     Table *pTab;
 79223  80950     assert( pItem && pSrc->nSrc==1 );
 79224  80951     pTab = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase);
 79225  80952     sqlite3DeleteTable(pParse->db, pItem->pTab);
................................................................................
 79730  81457     /* Delete the index and table entries. Skip this step if pTab is really
 79731  81458     ** a view (in which case the only effect of the DELETE statement is to
 79732  81459     ** fire the INSTEAD OF triggers).  */ 
 79733  81460     if( pTab->pSelect==0 ){
 79734  81461       sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, 0);
 79735  81462       sqlite3VdbeAddOp2(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0));
 79736  81463       if( count ){
 79737         -      sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC);
        81464  +      sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
 79738  81465       }
 79739  81466     }
 79740  81467   
 79741  81468     /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
 79742  81469     ** handle rows (possibly in other tables) that refer via a foreign key
 79743  81470     ** to the row just deleted. */ 
 79744  81471     sqlite3FkActions(pParse, pTab, 0, iOld);
................................................................................
 79821  81548       }else{
 79822  81549         sqlite3VdbeAddOp3(v, OP_Column, iCur, idx, regBase+j);
 79823  81550         sqlite3ColumnDefault(v, pTab, idx, -1);
 79824  81551       }
 79825  81552     }
 79826  81553     if( doMakeRec ){
 79827  81554       sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol+1, regOut);
 79828         -    sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), 0);
        81555  +    sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), P4_TRANSIENT);
 79829  81556     }
 79830  81557     sqlite3ReleaseTempRange(pParse, regBase, nCol+1);
 79831  81558     return regBase;
 79832  81559   }
 79833  81560   
 79834  81561   /************** End of delete.c **********************************************/
 79835  81562   /************** Begin file func.c ********************************************/
................................................................................
 81066  82793     p = sqlite3_aggregate_context(context, sizeof(*p));
 81067  82794     type = sqlite3_value_numeric_type(argv[0]);
 81068  82795     if( p && type!=SQLITE_NULL ){
 81069  82796       p->cnt++;
 81070  82797       if( type==SQLITE_INTEGER ){
 81071  82798         i64 v = sqlite3_value_int64(argv[0]);
 81072  82799         p->rSum += v;
 81073         -      if( (p->approx|p->overflow)==0 ){
 81074         -        i64 iNewSum = p->iSum + v;
 81075         -        int s1 = (int)(p->iSum >> (sizeof(i64)*8-1));
 81076         -        int s2 = (int)(v       >> (sizeof(i64)*8-1));
 81077         -        int s3 = (int)(iNewSum >> (sizeof(i64)*8-1));
 81078         -        p->overflow = ((s1&s2&~s3) | (~s1&~s2&s3))?1:0;
 81079         -        p->iSum = iNewSum;
        82800  +      if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
        82801  +        p->overflow = 1;
 81080  82802         }
 81081  82803       }else{
 81082  82804         p->rSum += sqlite3_value_double(argv[0]);
 81083  82805         p->approx = 1;
 81084  82806       }
 81085  82807     }
 81086  82808   }
................................................................................
 81823  83545             int iParent = pIdx->aiColumn[i]+1+regData;
 81824  83546             sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent);
 81825  83547           }
 81826  83548           sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
 81827  83549         }
 81828  83550     
 81829  83551         sqlite3VdbeAddOp3(v, OP_MakeRecord, regTemp, nCol, regRec);
 81830         -      sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), 0);
        83552  +      sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v,pIdx), P4_TRANSIENT);
 81831  83553         sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0);
 81832  83554     
 81833  83555         sqlite3ReleaseTempReg(pParse, regRec);
 81834  83556         sqlite3ReleaseTempRange(pParse, regTemp, nCol);
 81835  83557       }
 81836  83558     }
 81837  83559   
................................................................................
 82112  83834   SQLITE_PRIVATE void sqlite3FkCheck(
 82113  83835     Parse *pParse,                  /* Parse context */
 82114  83836     Table *pTab,                    /* Row is being deleted from this table */ 
 82115  83837     int regOld,                     /* Previous row data is stored here */
 82116  83838     int regNew                      /* New row data is stored here */
 82117  83839   ){
 82118  83840     sqlite3 *db = pParse->db;       /* Database handle */
 82119         -  Vdbe *v;                        /* VM to write code to */
 82120  83841     FKey *pFKey;                    /* Used to iterate through FKs */
 82121  83842     int iDb;                        /* Index of database containing pTab */
 82122  83843     const char *zDb;                /* Name of database containing pTab */
 82123  83844     int isIgnoreErrors = pParse->disableTriggers;
 82124  83845   
 82125  83846     /* Exactly one of regOld and regNew should be non-zero. */
 82126  83847     assert( (regOld==0)!=(regNew==0) );
 82127  83848   
 82128  83849     /* If foreign-keys are disabled, this function is a no-op. */
 82129  83850     if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
 82130  83851   
 82131         -  v = sqlite3GetVdbe(pParse);
 82132  83852     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
 82133  83853     zDb = db->aDb[iDb].zName;
 82134  83854   
 82135  83855     /* Loop through all the foreign key constraints for which pTab is the
 82136  83856     ** child table (the table that the foreign key definition is part of).  */
 82137  83857     for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
 82138  83858       Table *pTo;                   /* Parent table of foreign key pFKey */
................................................................................
 82581  84301   ** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
 82582  84302   ** hash table.
 82583  84303   */
 82584  84304   SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
 82585  84305     FKey *pFKey;                    /* Iterator variable */
 82586  84306     FKey *pNext;                    /* Copy of pFKey->pNextFrom */
 82587  84307   
        84308  +  assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
 82588  84309     for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
 82589  84310   
 82590  84311       /* Remove the FK from the fkeyHash hash table. */
 82591  84312       if( !db || db->pnBytesFreed==0 ){
 82592  84313         if( pFKey->pPrevTo ){
 82593  84314           pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
 82594  84315         }else{
................................................................................
 82740  84461         zColAff[i] = pTab->aCol[i].affinity;
 82741  84462       }
 82742  84463       zColAff[pTab->nCol] = '\0';
 82743  84464   
 82744  84465       pTab->zColAff = zColAff;
 82745  84466     }
 82746  84467   
 82747         -  sqlite3VdbeChangeP4(v, -1, pTab->zColAff, 0);
        84468  +  sqlite3VdbeChangeP4(v, -1, pTab->zColAff, P4_TRANSIENT);
 82748  84469   }
 82749  84470   
 82750  84471   /*
 82751  84472   ** Return non-zero if the table pTab in database iDb or any of its indices
 82752  84473   ** have been opened at any point in the VDBE program beginning at location
 82753  84474   ** iStartAddr throught the end of the program.  This is used to see if 
 82754  84475   ** a statement of the form  "INSERT INTO <iDb, pTab> SELECT ..." can 
................................................................................
 82854  84575     assert( pParse->pTriggerTab==0 );
 82855  84576     assert( pParse==sqlite3ParseToplevel(pParse) );
 82856  84577   
 82857  84578     assert( v );   /* We failed long ago if this is not so */
 82858  84579     for(p = pParse->pAinc; p; p = p->pNext){
 82859  84580       pDb = &db->aDb[p->iDb];
 82860  84581       memId = p->regCtr;
        84582  +    assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
 82861  84583       sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
 82862  84584       addr = sqlite3VdbeCurrentAddr(v);
 82863  84585       sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
 82864  84586       sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9);
 82865  84587       sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
 82866  84588       sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
 82867  84589       sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
................................................................................
 82904  84626     for(p = pParse->pAinc; p; p = p->pNext){
 82905  84627       Db *pDb = &db->aDb[p->iDb];
 82906  84628       int j1, j2, j3, j4, j5;
 82907  84629       int iRec;
 82908  84630       int memId = p->regCtr;
 82909  84631   
 82910  84632       iRec = sqlite3GetTempReg(pParse);
        84633  +    assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
 82911  84634       sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
 82912  84635       j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1);
 82913  84636       j2 = sqlite3VdbeAddOp0(v, OP_Rewind);
 82914  84637       j3 = sqlite3VdbeAddOp3(v, OP_Column, 0, 0, iRec);
 82915  84638       j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec);
 82916  84639       sqlite3VdbeAddOp2(v, OP_Next, 0, j3);
 82917  84640       sqlite3VdbeJumpHere(v, j2);
................................................................................
 83082  84805     /* Register allocations */
 83083  84806     int regFromSelect = 0;/* Base register for data coming from SELECT */
 83084  84807     int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
 83085  84808     int regRowCount = 0;  /* Memory cell used for the row counter */
 83086  84809     int regIns;           /* Block of regs holding rowid+data being inserted */
 83087  84810     int regRowid;         /* registers holding insert rowid */
 83088  84811     int regData;          /* register holding first column to insert */
 83089         -  int regRecord;        /* Holds the assemblied row record */
 83090  84812     int regEof = 0;       /* Register recording end of SELECT data */
 83091  84813     int *aRegIdx = 0;     /* One register allocated to each index */
 83092  84814   
 83093  84815   #ifndef SQLITE_OMIT_TRIGGER
 83094  84816     int isView;                 /* True if attempting to insert into a view */
 83095  84817     Trigger *pTrigger;          /* List of triggers on pTab, if required */
 83096  84818     int tmask;                  /* Mask of trigger times */
................................................................................
 83411  85133       addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
 83412  85134       addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof);
 83413  85135     }
 83414  85136   
 83415  85137     /* Allocate registers for holding the rowid of the new row,
 83416  85138     ** the content of the new row, and the assemblied row record.
 83417  85139     */
 83418         -  regRecord = ++pParse->nMem;
 83419  85140     regRowid = regIns = pParse->nMem+1;
 83420  85141     pParse->nMem += pTab->nCol + 1;
 83421  85142     if( IsVirtual(pTab) ){
 83422  85143       regRowid++;
 83423  85144       pParse->nMem++;
 83424  85145     }
 83425  85146     regData = regRowid+1;
................................................................................
 83805  85526           || onError==OE_Ignore || onError==OE_Replace );
 83806  85527       switch( onError ){
 83807  85528         case OE_Abort:
 83808  85529           sqlite3MayAbort(pParse);
 83809  85530         case OE_Rollback:
 83810  85531         case OE_Fail: {
 83811  85532           char *zMsg;
 83812         -        j1 = sqlite3VdbeAddOp3(v, OP_HaltIfNull,
        85533  +        sqlite3VdbeAddOp3(v, OP_HaltIfNull,
 83813  85534                                     SQLITE_CONSTRAINT, onError, regData+i);
 83814  85535           zMsg = sqlite3MPrintf(pParse->db, "%s.%s may not be NULL",
 83815  85536                                 pTab->zName, pTab->aCol[i].zName);
 83816  85537           sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC);
 83817  85538           break;
 83818  85539         }
 83819  85540         case OE_Ignore: {
................................................................................
 83945  85666           sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
 83946  85667         }else{
 83947  85668           sqlite3VdbeAddOp2(v, OP_SCopy, regData+idx, regIdx+i);
 83948  85669         }
 83949  85670       }
 83950  85671       sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
 83951  85672       sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn+1, aRegIdx[iCur]);
 83952         -    sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), 0);
        85673  +    sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), P4_TRANSIENT);
 83953  85674       sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn+1);
 83954  85675   
 83955  85676       /* Find out what action to take in case there is an indexing conflict */
 83956  85677       onError = pIdx->onError;
 83957  85678       if( onError==OE_None ){ 
 83958  85679         sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
 83959  85680         continue;  /* pIdx is not a UNIQUE index */
................................................................................
 84085  85806       pik_flags |= OPFLAG_APPEND;
 84086  85807     }
 84087  85808     if( useSeekResult ){
 84088  85809       pik_flags |= OPFLAG_USESEEKRESULT;
 84089  85810     }
 84090  85811     sqlite3VdbeAddOp3(v, OP_Insert, baseCur, regRec, regRowid);
 84091  85812     if( !pParse->nested ){
 84092         -    sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC);
        85813  +    sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
 84093  85814     }
 84094  85815     sqlite3VdbeChangeP5(v, pik_flags);
 84095  85816   }
 84096  85817   
 84097  85818   /*
 84098  85819   ** Generate code that will open cursors for a table and for all
 84099  85820   ** indices of that table.  The "baseCur" parameter is the cursor number used
................................................................................
 85088  86809   # define sqlite3_column_origin_name16   0
 85089  86810   #endif
 85090  86811   
 85091  86812   #ifdef SQLITE_OMIT_COMPLETE
 85092  86813   # define sqlite3_complete 0
 85093  86814   # define sqlite3_complete16 0
 85094  86815   #endif
        86816  +
        86817  +#ifdef SQLITE_OMIT_DECLTYPE
        86818  +# define sqlite3_column_decltype16      0
        86819  +# define sqlite3_column_decltype        0
        86820  +#endif
 85095  86821   
 85096  86822   #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
 85097  86823   # define sqlite3_progress_handler 0
 85098  86824   #endif
 85099  86825   
 85100  86826   #ifdef SQLITE_OMIT_VIRTUALTABLE
 85101  86827   # define sqlite3_create_module 0
................................................................................
 85780  87506       if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
 85781  87507         sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
 85782  87508           "from within a transaction");
 85783  87509         return SQLITE_ERROR;
 85784  87510       }
 85785  87511       sqlite3BtreeClose(db->aDb[1].pBt);
 85786  87512       db->aDb[1].pBt = 0;
 85787         -    sqlite3ResetInternalSchema(db, 0);
        87513  +    sqlite3ResetInternalSchema(db, -1);
 85788  87514     }
 85789  87515     return SQLITE_OK;
 85790  87516   }
 85791  87517   #endif /* SQLITE_PAGER_PRAGMAS */
 85792  87518   
 85793  87519   #ifndef SQLITE_OMIT_PAGER_PRAGMAS
 85794  87520   /*
................................................................................
 86049  87775         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
 86050  87776         pParse->nMem += 2;
 86051  87777         addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
 86052  87778         sqlite3VdbeChangeP1(v, addr, iDb);
 86053  87779         sqlite3VdbeChangeP1(v, addr+1, iDb);
 86054  87780         sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
 86055  87781       }else{
 86056         -      int size = sqlite3Atoi(zRight);
 86057         -      if( size<0 ) size = -size;
        87782  +      int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
 86058  87783         sqlite3BeginWriteOperation(pParse, 0, iDb);
 86059  87784         sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
 86060  87785         sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
        87786  +      assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 86061  87787         pDb->pSchema->cache_size = size;
 86062  87788         sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
 86063  87789       }
 86064  87790     }else
 86065  87791   
 86066  87792     /*
 86067  87793     **  PRAGMA [database.]page_size
................................................................................
 86356  88082     ** page cache size value.  It does not change the persistent
 86357  88083     ** cache size stored on the disk so the cache size will revert
 86358  88084     ** to its default value when the database is closed and reopened.
 86359  88085     ** N should be a positive integer.
 86360  88086     */
 86361  88087     if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
 86362  88088       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
        88089  +    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 86363  88090       if( !zRight ){
 86364  88091         returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
 86365  88092       }else{
 86366         -      int size = sqlite3Atoi(zRight);
 86367         -      if( size<0 ) size = -size;
        88093  +      int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
 86368  88094         pDb->pSchema->cache_size = size;
 86369  88095         sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
 86370  88096       }
 86371  88097     }else
 86372  88098   
 86373  88099     /*
 86374  88100     **   PRAGMA temp_store
................................................................................
 86777  88503         sqlite3VdbeJumpHere(v, addr);
 86778  88504   
 86779  88505         /* Do an integrity check of the B-Tree
 86780  88506         **
 86781  88507         ** Begin by filling registers 2, 3, ... with the root pages numbers
 86782  88508         ** for all tables and indices in the database.
 86783  88509         */
        88510  +      assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 86784  88511         pTbls = &db->aDb[i].pSchema->tblHash;
 86785  88512         for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
 86786  88513           Table *pTab = sqliteHashData(x);
 86787  88514           Index *pIdx;
 86788  88515           sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
 86789  88516           cnt++;
 86790  88517           for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
................................................................................
 86842  88569               { OP_Halt,        0,  0,  0},
 86843  88570             };
 86844  88571             r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0);
 86845  88572             jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, j+2, 0, r1, pIdx->nColumn+1);
 86846  88573             addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
 86847  88574             sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
 86848  88575             sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
 86849         -          sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_STATIC);
        88576  +          sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_TRANSIENT);
 86850  88577             sqlite3VdbeJumpHere(v, addr+9);
 86851  88578             sqlite3VdbeJumpHere(v, jmp2);
 86852  88579           }
 86853  88580           sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
 86854  88581           sqlite3VdbeJumpHere(v, loopTop);
 86855  88582           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
 86856  88583             static const VdbeOpList cntIdx[] = {
................................................................................
 86872  88599             sqlite3VdbeChangeP1(v, addr+1, j+2);
 86873  88600             sqlite3VdbeChangeP2(v, addr+1, addr+4);
 86874  88601             sqlite3VdbeChangeP1(v, addr+3, j+2);
 86875  88602             sqlite3VdbeChangeP2(v, addr+3, addr+2);
 86876  88603             sqlite3VdbeJumpHere(v, addr+4);
 86877  88604             sqlite3VdbeChangeP4(v, addr+6, 
 86878  88605                        "wrong # of entries in index ", P4_STATIC);
 86879         -          sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_STATIC);
        88606  +          sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_TRANSIENT);
 86880  88607           }
 86881  88608         } 
 86882  88609       }
 86883  88610       addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
 86884  88611       sqlite3VdbeChangeP2(v, addr, -mxErr);
 86885  88612       sqlite3VdbeJumpHere(v, addr+1);
 86886  88613       sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
................................................................................
 87051  88778         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
 87052  88779       }
 87053  88780     }else
 87054  88781   #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
 87055  88782   
 87056  88783   #ifndef SQLITE_OMIT_WAL
 87057  88784     /*
 87058         -  **   PRAGMA [database.]wal_checkpoint
        88785  +  **   PRAGMA [database.]wal_checkpoint = passive|full|restart
 87059  88786     **
 87060  88787     ** Checkpoint the database.
 87061  88788     */
 87062  88789     if( sqlite3StrICmp(zLeft, "wal_checkpoint")==0 ){
        88790  +    int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
        88791  +    int eMode = SQLITE_CHECKPOINT_PASSIVE;
        88792  +    if( zRight ){
        88793  +      if( sqlite3StrICmp(zRight, "full")==0 ){
        88794  +        eMode = SQLITE_CHECKPOINT_FULL;
        88795  +      }else if( sqlite3StrICmp(zRight, "restart")==0 ){
        88796  +        eMode = SQLITE_CHECKPOINT_RESTART;
        88797  +      }
        88798  +    }
 87063  88799       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
 87064         -    sqlite3VdbeAddOp3(v, OP_Checkpoint, pId2->z?iDb:SQLITE_MAX_ATTACHED, 0, 0);
        88800  +    sqlite3VdbeSetNumCols(v, 3);
        88801  +    pParse->nMem = 3;
        88802  +    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
        88803  +    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
        88804  +    sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
        88805  +
        88806  +    sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
        88807  +    sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
 87065  88808     }else
 87066  88809   
 87067  88810     /*
 87068  88811     **   PRAGMA wal_autocheckpoint
 87069  88812     **   PRAGMA wal_autocheckpoint = N
 87070  88813     **
 87071  88814     ** Configure a database connection to automatically checkpoint a database
................................................................................
 87208  88951       sqlite3SetString(pData->pzErrMsg, db,
 87209  88952         "malformed database schema (%s)", zObj);
 87210  88953       if( zExtra ){
 87211  88954         *pData->pzErrMsg = sqlite3MAppendf(db, *pData->pzErrMsg, 
 87212  88955                                    "%s - %s", *pData->pzErrMsg, zExtra);
 87213  88956       }
 87214  88957     }
 87215         -  pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT;
        88958  +  pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT;
 87216  88959   }
 87217  88960   
 87218  88961   /*
 87219  88962   ** This is the callback routine for the code that initializes the
 87220  88963   ** database.  See sqlite3Init() below for additional information.
 87221  88964   ** This routine is also called from the OP_ParseSchema opcode of the VDBE.
 87222  88965   **
................................................................................
 87315  89058     int size;
 87316  89059     Table *pTab;
 87317  89060     Db *pDb;
 87318  89061     char const *azArg[4];
 87319  89062     int meta[5];
 87320  89063     InitData initData;
 87321  89064     char const *zMasterSchema;
 87322         -  char const *zMasterName = SCHEMA_TABLE(iDb);
        89065  +  char const *zMasterName;
 87323  89066     int openedTransaction = 0;
 87324  89067   
 87325  89068     /*
 87326  89069     ** The master database table has a structure like this
 87327  89070     */
 87328  89071     static const char master_schema[] = 
 87329  89072        "CREATE TABLE sqlite_master(\n"
................................................................................
 87452  89195       }
 87453  89196     }else{
 87454  89197       DbSetProperty(db, iDb, DB_Empty);
 87455  89198     }
 87456  89199     pDb->pSchema->enc = ENC(db);
 87457  89200   
 87458  89201     if( pDb->pSchema->cache_size==0 ){
 87459         -    size = meta[BTREE_DEFAULT_CACHE_SIZE-1];
        89202  +    size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
 87460  89203       if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
 87461         -    if( size<0 ) size = -size;
 87462  89204       pDb->pSchema->cache_size = size;
 87463  89205       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
 87464  89206     }
 87465  89207   
 87466  89208     /*
 87467  89209     ** file_format==1    Version 3.0.0.
 87468  89210     ** file_format==2    Version 3.1.3.  // ALTER TABLE ADD COLUMN
................................................................................
 87513  89255       if( rc==SQLITE_OK ){
 87514  89256         sqlite3AnalysisLoad(db, iDb);
 87515  89257       }
 87516  89258   #endif
 87517  89259     }
 87518  89260     if( db->mallocFailed ){
 87519  89261       rc = SQLITE_NOMEM;
 87520         -    sqlite3ResetInternalSchema(db, 0);
        89262  +    sqlite3ResetInternalSchema(db, -1);
 87521  89263     }
 87522  89264     if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
 87523  89265       /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
 87524  89266       ** the schema loaded, even if errors occurred. In this situation the 
 87525  89267       ** current sqlite3_prepare() operation will fail, but the following one
 87526  89268       ** will attempt to compile the supplied statement against whatever subset
 87527  89269       ** of the schema was loaded before the error occurred. The primary
................................................................................
 87645  89387         openedTransaction = 1;
 87646  89388       }
 87647  89389   
 87648  89390       /* Read the schema cookie from the database. If it does not match the 
 87649  89391       ** value stored as part of the in-memory schema representation,
 87650  89392       ** set Parse.rc to SQLITE_SCHEMA. */
 87651  89393       sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
        89394  +    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
 87652  89395       if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
        89396  +      sqlite3ResetInternalSchema(db, iDb);
 87653  89397         pParse->rc = SQLITE_SCHEMA;
 87654  89398       }
 87655  89399   
 87656  89400       /* Close the transaction, if one was opened. */
 87657  89401       if( openedTransaction ){
 87658  89402         sqlite3BtreeCommit(pBt);
 87659  89403       }
................................................................................
<