Fossil

Diff
Login

Differences From Artifact [57b0d818]:

To Artifact [37b3ef49]:


     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.7.13.  By combining all the individual C code files into this 
            3  +** version 3.7.14.  By combining all the individual C code files into this 
     4      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
................................................................................
   385    385   
   386    386   /*
   387    387   ** Exactly one of the following macros must be defined in order to
   388    388   ** specify which memory allocation subsystem to use.
   389    389   **
   390    390   **     SQLITE_SYSTEM_MALLOC          // Use normal system malloc()
   391    391   **     SQLITE_WIN32_MALLOC           // Use Win32 native heap API
          392  +**     SQLITE_ZERO_MALLOC            // Use a stub allocator that always fails
   392    393   **     SQLITE_MEMDEBUG               // Debugging version of system malloc()
   393    394   **
   394    395   ** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
   395    396   ** assert() macro is enabled, each call into the Win32 native heap subsystem
   396    397   ** will cause HeapValidate to be called.  If heap validation should fail, an
   397    398   ** assertion will be triggered.
   398    399   **
   399    400   ** (Historical note:  There used to be several other options, but we've
   400    401   ** pared it down to just these three.)
   401    402   **
   402    403   ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
   403    404   ** the default.
   404    405   */
   405         -#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)>1
   406         -# error "At most one of the following compile-time configuration options\
   407         - is allows: SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG"
          406  +#if defined(SQLITE_SYSTEM_MALLOC) \
          407  +  + defined(SQLITE_WIN32_MALLOC) \
          408  +  + defined(SQLITE_ZERO_MALLOC) \
          409  +  + defined(SQLITE_MEMDEBUG)>1
          410  +# error "Two or more of the following compile-time configuration options\
          411  + are defined but at most one is allowed:\
          412  + SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
          413  + SQLITE_ZERO_MALLOC"
   408    414   #endif
   409         -#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)==0
          415  +#if defined(SQLITE_SYSTEM_MALLOC) \
          416  +  + defined(SQLITE_WIN32_MALLOC) \
          417  +  + defined(SQLITE_ZERO_MALLOC) \
          418  +  + defined(SQLITE_MEMDEBUG)==0
   410    419   # define SQLITE_SYSTEM_MALLOC 1
   411    420   #endif
   412    421   
   413    422   /*
   414    423   ** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
   415    424   ** sizes of memory allocations below this value where possible.
   416    425   */
................................................................................
   660    669   ** string contains the date and time of the check-in (UTC) and an SHA1
   661    670   ** hash of the entire source tree.
   662    671   **
   663    672   ** See also: [sqlite3_libversion()],
   664    673   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   665    674   ** [sqlite_version()] and [sqlite_source_id()].
   666    675   */
   667         -#define SQLITE_VERSION        "3.7.13"
   668         -#define SQLITE_VERSION_NUMBER 3007013
   669         -#define SQLITE_SOURCE_ID      "2012-06-07 07:24:04 506008f000ba4af0b35da023b8c52f7a3f5033bd"
          676  +#define SQLITE_VERSION        "3.7.14"
          677  +#define SQLITE_VERSION_NUMBER 3007014
          678  +#define SQLITE_SOURCE_ID      "2012-06-21 17:21:52 d5e6880279210ca63e2d5e7f6d009f30566f1242"
   670    679   
   671    680   /*
   672    681   ** CAPI3REF: Run-Time Library Version Numbers
   673    682   ** KEYWORDS: sqlite3_version, sqlite3_sourceid
   674    683   **
   675    684   ** These interfaces provide the same information as the [SQLITE_VERSION],
   676    685   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
   772    781   ** CAPI3REF: Database Connection Handle
   773    782   ** KEYWORDS: {database connection} {database connections}
   774    783   **
   775    784   ** Each open SQLite database is represented by a pointer to an instance of
   776    785   ** the opaque structure named "sqlite3".  It is useful to think of an sqlite3
   777    786   ** pointer as an object.  The [sqlite3_open()], [sqlite3_open16()], and
   778    787   ** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
   779         -** is its destructor.  There are many other interfaces (such as
          788  +** and [sqlite3_close_v2()] are its destructors.  There are many other
          789  +** interfaces (such as
   780    790   ** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
   781    791   ** [sqlite3_busy_timeout()] to name but three) that are methods on an
   782    792   ** sqlite3 object.
   783    793   */
   784    794   typedef struct sqlite3 sqlite3;
   785    795   
   786    796   /*
................................................................................
   819    829   #ifdef SQLITE_OMIT_FLOATING_POINT
   820    830   # define double sqlite3_int64
   821    831   #endif
   822    832   
   823    833   /*
   824    834   ** CAPI3REF: Closing A Database Connection
   825    835   **
   826         -** ^The sqlite3_close() routine is the destructor for the [sqlite3] object.
   827         -** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is
   828         -** successfully destroyed and all associated resources are deallocated.
          836  +** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
          837  +** for the [sqlite3] object.
          838  +** ^Calls to sqlite3_close() and sqlite3_close_v2() return SQLITE_OK if
          839  +** the [sqlite3] object is successfully destroyed and all associated
          840  +** resources are deallocated.
   829    841   **
   830         -** Applications must [sqlite3_finalize | finalize] all [prepared statements]
   831         -** and [sqlite3_blob_close | close] all [BLOB handles] associated with
   832         -** the [sqlite3] object prior to attempting to close the object.  ^If
          842  +** ^If the database connection is associated with unfinalized prepared
          843  +** statements or unfinished sqlite3_backup objects then sqlite3_close()
          844  +** will leave the database connection open and return [SQLITE_BUSY].
          845  +** ^If sqlite3_close_v2() is called with unfinalized prepared statements
          846  +** and unfinished sqlite3_backups, then the database connection becomes
          847  +** an unusable "zombie" which will automatically be deallocated when the
          848  +** last prepared statement is finalized or the last sqlite3_backup is
          849  +** finished.  The sqlite3_close_v2() interface is intended for use with
          850  +** host languages that are garbage collected, and where the order in which
          851  +** destructors are called is arbitrary.
          852  +**
          853  +** Applications should [sqlite3_finalize | finalize] all [prepared statements],
          854  +** [sqlite3_blob_close | close] all [BLOB handles], and 
          855  +** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
          856  +** with the [sqlite3] object prior to attempting to close the object.  ^If
   833    857   ** sqlite3_close() is called on a [database connection] that still has
   834         -** outstanding [prepared statements] or [BLOB handles], then it returns
   835         -** SQLITE_BUSY.
          858  +** outstanding [prepared statements], [BLOB handles], and/or
          859  +** [sqlite3_backup] objects then it returns SQLITE_OK but the deallocation
          860  +** of resources is deferred until all [prepared statements], [BLOB handles],
          861  +** and [sqlite3_backup] objects are also destroyed.
   836    862   **
   837         -** ^If [sqlite3_close()] is invoked while a transaction is open,
          863  +** ^If an [sqlite3] object is destroyed while a transaction is open,
   838    864   ** the transaction is automatically rolled back.
   839    865   **
   840         -** The C parameter to [sqlite3_close(C)] must be either a NULL
          866  +** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
          867  +** must be either a NULL
   841    868   ** pointer or an [sqlite3] object pointer obtained
   842    869   ** from [sqlite3_open()], [sqlite3_open16()], or
   843    870   ** [sqlite3_open_v2()], and not previously closed.
   844         -** ^Calling sqlite3_close() with a NULL pointer argument is a 
   845         -** harmless no-op.
          871  +** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
          872  +** argument is a harmless no-op.
   846    873   */
   847         -SQLITE_API int sqlite3_close(sqlite3 *);
          874  +SQLITE_API int sqlite3_close(sqlite3*);
          875  +SQLITE_API int sqlite3_close_v2(sqlite3*);
   848    876   
   849    877   /*
   850    878   ** The type for a callback function.
   851    879   ** This is legacy and deprecated.  It is included for historical
   852    880   ** compatibility and is not documented.
   853    881   */
   854    882   typedef int (*sqlite3_callback)(void*,int,char**, char**);
................................................................................
  5011   5039   
  5012   5040   /*
  5013   5041   ** CAPI3REF: Name Of The Folder Holding Database Files
  5014   5042   **
  5015   5043   ** ^(If this global variable is made to point to a string which is
  5016   5044   ** the name of a folder (a.k.a. directory), then all database files
  5017   5045   ** specified with a relative pathname and created or accessed by
  5018         -** SQLite when using a built-in [sqlite3_vfs | VFS] will be assumed
         5046  +** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
  5019   5047   ** to be relative to that directory.)^ ^If this variable is a NULL
  5020   5048   ** pointer, then SQLite assumes that all database files specified
  5021   5049   ** with a relative pathname are relative to the current directory
  5022         -** for the process.
         5050  +** for the process.  Only the windows VFS makes use of this global
         5051  +** variable; it is ignored by the unix VFS.
  5023   5052   **
  5024   5053   ** Changing the value of this variable while a database connection is
  5025   5054   ** open can result in a corrupt database.
  5026   5055   **
  5027   5056   ** It is not safe to read or modify this variable in more than one
  5028   5057   ** thread at a time.  It is not safe to read or modify this variable
  5029   5058   ** if a [database connection] is being used at the same time in a separate
................................................................................
  6046   6075   **
  6047   6076   ** The SQLite source code contains multiple implementations
  6048   6077   ** of these mutex routines.  An appropriate implementation
  6049   6078   ** is selected automatically at compile-time.  ^(The following
  6050   6079   ** implementations are available in the SQLite core:
  6051   6080   **
  6052   6081   ** <ul>
  6053         -** <li>   SQLITE_MUTEX_OS2
  6054   6082   ** <li>   SQLITE_MUTEX_PTHREADS
  6055   6083   ** <li>   SQLITE_MUTEX_W32
  6056   6084   ** <li>   SQLITE_MUTEX_NOOP
  6057   6085   ** </ul>)^
  6058   6086   **
  6059   6087   ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
  6060   6088   ** that does no real locking and is appropriate for use in
  6061         -** a single-threaded application.  ^The SQLITE_MUTEX_OS2,
  6062         -** SQLITE_MUTEX_PTHREADS, and SQLITE_MUTEX_W32 implementations
  6063         -** are appropriate for use on OS/2, Unix, and Windows.
         6089  +** a single-threaded application.  ^The SQLITE_MUTEX_PTHREADS and
         6090  +** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
         6091  +** and Windows.
  6064   6092   **
  6065   6093   ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
  6066   6094   ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
  6067   6095   ** implementation is included with the library. In this case the
  6068   6096   ** application must supply a custom mutex implementation using the
  6069   6097   ** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
  6070   6098   ** before calling sqlite3_initialize() or any other public sqlite3_
................................................................................
  9285   9313   */
  9286   9314   #ifndef _SQLITE_OS_H_
  9287   9315   #define _SQLITE_OS_H_
  9288   9316   
  9289   9317   /*
  9290   9318   ** Figure out if we are dealing with Unix, Windows, or some other
  9291   9319   ** operating system.  After the following block of preprocess macros,
  9292         -** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, SQLITE_OS_OS2, and SQLITE_OS_OTHER 
         9320  +** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, and SQLITE_OS_OTHER 
  9293   9321   ** will defined to either 1 or 0.  One of the four will be 1.  The other 
  9294   9322   ** three will be 0.
  9295   9323   */
  9296   9324   #if defined(SQLITE_OS_OTHER)
  9297   9325   # if SQLITE_OS_OTHER==1
  9298   9326   #   undef SQLITE_OS_UNIX
  9299   9327   #   define SQLITE_OS_UNIX 0
  9300   9328   #   undef SQLITE_OS_WIN
  9301   9329   #   define SQLITE_OS_WIN 0
  9302         -#   undef SQLITE_OS_OS2
  9303         -#   define SQLITE_OS_OS2 0
  9304   9330   # else
  9305   9331   #   undef SQLITE_OS_OTHER
  9306   9332   # endif
  9307   9333   #endif
  9308   9334   #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
  9309   9335   # define SQLITE_OS_OTHER 0
  9310   9336   # ifndef SQLITE_OS_WIN
  9311   9337   #   if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
  9312   9338   #     define SQLITE_OS_WIN 1
  9313   9339   #     define SQLITE_OS_UNIX 0
  9314         -#     define SQLITE_OS_OS2 0
  9315         -#   elif defined(__EMX__) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__)
  9316         -#     define SQLITE_OS_WIN 0
  9317         -#     define SQLITE_OS_UNIX 0
  9318         -#     define SQLITE_OS_OS2 1
  9319   9340   #   else
  9320   9341   #     define SQLITE_OS_WIN 0
  9321   9342   #     define SQLITE_OS_UNIX 1
  9322         -#     define SQLITE_OS_OS2 0
  9323   9343   #  endif
  9324   9344   # else
  9325   9345   #  define SQLITE_OS_UNIX 0
  9326         -#  define SQLITE_OS_OS2 0
  9327   9346   # endif
  9328   9347   #else
  9329   9348   # ifndef SQLITE_OS_WIN
  9330   9349   #  define SQLITE_OS_WIN 0
  9331   9350   # endif
  9332   9351   #endif
  9333   9352   
  9334   9353   #if SQLITE_OS_WIN
  9335   9354   # include <windows.h>
  9336   9355   #endif
  9337   9356   
  9338         -#if SQLITE_OS_OS2
  9339         -# if (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3) && defined(OS2_HIGH_MEMORY)
  9340         -#  include <os2safe.h> /* has to be included before os2.h for linking to work */
  9341         -# endif
  9342         -# define INCL_DOSDATETIME
  9343         -# define INCL_DOSFILEMGR
  9344         -# define INCL_DOSERRORS
  9345         -# define INCL_DOSMISC
  9346         -# define INCL_DOSPROCESS
  9347         -# define INCL_DOSMODULEMGR
  9348         -# define INCL_DOSSEMAPHORES
  9349         -# include <os2.h>
  9350         -# include <uconv.h>
  9351         -#endif
  9352         -
  9353   9357   /*
  9354   9358   ** Determine if we are dealing with Windows NT.
  9355   9359   **
  9356   9360   ** We ought to be able to determine if we are compiling for win98 or winNT
  9357   9361   ** using the _WIN32_WINNT macro as follows:
  9358   9362   **
  9359   9363   ** #if defined(_WIN32_WINNT)
................................................................................
  9615   9619   **                             mutual exclusion is provided.  But this
  9616   9620   **                             implementation can be overridden at
  9617   9621   **                             start-time.
  9618   9622   **
  9619   9623   **   SQLITE_MUTEX_PTHREADS     For multi-threaded applications on Unix.
  9620   9624   **
  9621   9625   **   SQLITE_MUTEX_W32          For multi-threaded applications on Win32.
  9622         -**
  9623         -**   SQLITE_MUTEX_OS2          For multi-threaded applications on OS/2.
  9624   9626   */
  9625   9627   #if !SQLITE_THREADSAFE
  9626   9628   # define SQLITE_MUTEX_OMIT
  9627   9629   #endif
  9628   9630   #if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
  9629   9631   #  if SQLITE_OS_UNIX
  9630   9632   #    define SQLITE_MUTEX_PTHREADS
  9631   9633   #  elif SQLITE_OS_WIN
  9632   9634   #    define SQLITE_MUTEX_W32
  9633         -#  elif SQLITE_OS_OS2
  9634         -#    define SQLITE_MUTEX_OS2
  9635   9635   #  else
  9636   9636   #    define SQLITE_MUTEX_NOOP
  9637   9637   #  endif
  9638   9638   #endif
  9639   9639   
  9640   9640   #ifdef SQLITE_MUTEX_OMIT
  9641   9641   /*
................................................................................
  9948   9948   ** than being distinct from one another.
  9949   9949   */
  9950   9950   #define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
  9951   9951   #define SQLITE_MAGIC_CLOSED   0x9f3c2d33  /* Database is closed */
  9952   9952   #define SQLITE_MAGIC_SICK     0x4b771290  /* Error and awaiting close */
  9953   9953   #define SQLITE_MAGIC_BUSY     0xf03b7906  /* Database currently in use */
  9954   9954   #define SQLITE_MAGIC_ERROR    0xb5357930  /* An SQLITE_MISUSE error occurred */
         9955  +#define SQLITE_MAGIC_ZOMBIE   0x64cffc7f  /* Close with last statement close */
  9955   9956   
  9956   9957   /*
  9957   9958   ** Each SQL function is defined by an instance of the following
  9958   9959   ** structure.  A pointer to this structure is stored in the sqlite.aFunc
  9959   9960   ** hash table.  When multiple functions have the same name, the hash table
  9960   9961   ** points to a linked list of these structures.
  9961   9962   */
................................................................................
 11809  11810   SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
 11810  11811   SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
 11811  11812   SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
 11812  11813   SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
 11813  11814   SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*);
 11814  11815   SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
 11815  11816   SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
        11817  +SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
 11816  11818   SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
 11817  11819   SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
 11818  11820   SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
 11819  11821   SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
 11820  11822   SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
 11821  11823   SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(Vdbe*, const Expr*, int, int);
 11822  11824   SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
................................................................................
 17714  17716   SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
 17715  17717     return sqlite3NoopMutex();
 17716  17718   }
 17717  17719   #endif /* defined(SQLITE_MUTEX_NOOP) */
 17718  17720   #endif /* !defined(SQLITE_MUTEX_OMIT) */
 17719  17721   
 17720  17722   /************** End of mutex_noop.c ******************************************/
 17721         -/************** Begin file mutex_os2.c ***************************************/
 17722         -/*
 17723         -** 2007 August 28
 17724         -**
 17725         -** The author disclaims copyright to this source code.  In place of
 17726         -** a legal notice, here is a blessing:
 17727         -**
 17728         -**    May you do good and not evil.
 17729         -**    May you find forgiveness for yourself and forgive others.
 17730         -**    May you share freely, never taking more than you give.
 17731         -**
 17732         -*************************************************************************
 17733         -** This file contains the C functions that implement mutexes for OS/2
 17734         -*/
 17735         -
 17736         -/*
 17737         -** The code in this file is only used if SQLITE_MUTEX_OS2 is defined.
 17738         -** See the mutex.h file for details.
 17739         -*/
 17740         -#ifdef SQLITE_MUTEX_OS2
 17741         -
 17742         -/********************** OS/2 Mutex Implementation **********************
 17743         -**
 17744         -** This implementation of mutexes is built using the OS/2 API.
 17745         -*/
 17746         -
 17747         -/*
 17748         -** The mutex object
 17749         -** Each recursive mutex is an instance of the following structure.
 17750         -*/
 17751         -struct sqlite3_mutex {
 17752         -  HMTX mutex;       /* Mutex controlling the lock */
 17753         -  int  id;          /* Mutex type */
 17754         -#ifdef SQLITE_DEBUG
 17755         - int   trace;       /* True to trace changes */
 17756         -#endif
 17757         -};
 17758         -
 17759         -#ifdef SQLITE_DEBUG
 17760         -#define SQLITE3_MUTEX_INITIALIZER { 0, 0, 0 }
 17761         -#else
 17762         -#define SQLITE3_MUTEX_INITIALIZER { 0, 0 }
 17763         -#endif
 17764         -
 17765         -/*
 17766         -** Initialize and deinitialize the mutex subsystem.
 17767         -*/
 17768         -static int os2MutexInit(void){ return SQLITE_OK; }
 17769         -static int os2MutexEnd(void){ return SQLITE_OK; }
 17770         -
 17771         -/*
 17772         -** The sqlite3_mutex_alloc() routine allocates a new
 17773         -** mutex and returns a pointer to it.  If it returns NULL
 17774         -** that means that a mutex could not be allocated. 
 17775         -** SQLite will unwind its stack and return an error.  The argument
 17776         -** to sqlite3_mutex_alloc() is one of these integer constants:
 17777         -**
 17778         -** <ul>
 17779         -** <li>  SQLITE_MUTEX_FAST
 17780         -** <li>  SQLITE_MUTEX_RECURSIVE
 17781         -** <li>  SQLITE_MUTEX_STATIC_MASTER
 17782         -** <li>  SQLITE_MUTEX_STATIC_MEM
 17783         -** <li>  SQLITE_MUTEX_STATIC_MEM2
 17784         -** <li>  SQLITE_MUTEX_STATIC_PRNG
 17785         -** <li>  SQLITE_MUTEX_STATIC_LRU
 17786         -** <li>  SQLITE_MUTEX_STATIC_LRU2
 17787         -** </ul>
 17788         -**
 17789         -** The first two constants cause sqlite3_mutex_alloc() to create
 17790         -** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
 17791         -** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
 17792         -** The mutex implementation does not need to make a distinction
 17793         -** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
 17794         -** not want to.  But SQLite will only request a recursive mutex in
 17795         -** cases where it really needs one.  If a faster non-recursive mutex
 17796         -** implementation is available on the host platform, the mutex subsystem
 17797         -** might return such a mutex in response to SQLITE_MUTEX_FAST.
 17798         -**
 17799         -** The other allowed parameters to sqlite3_mutex_alloc() each return
 17800         -** a pointer to a static preexisting mutex.  Six static mutexes are
 17801         -** used by the current version of SQLite.  Future versions of SQLite
 17802         -** may add additional static mutexes.  Static mutexes are for internal
 17803         -** use by SQLite only.  Applications that use SQLite mutexes should
 17804         -** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
 17805         -** SQLITE_MUTEX_RECURSIVE.
 17806         -**
 17807         -** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
 17808         -** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
 17809         -** returns a different mutex on every call.  But for the static
 17810         -** mutex types, the same mutex is returned on every call that has
 17811         -** the same type number.
 17812         -*/
 17813         -static sqlite3_mutex *os2MutexAlloc(int iType){
 17814         -  sqlite3_mutex *p = NULL;
 17815         -  switch( iType ){
 17816         -    case SQLITE_MUTEX_FAST:
 17817         -    case SQLITE_MUTEX_RECURSIVE: {
 17818         -      p = sqlite3MallocZero( sizeof(*p) );
 17819         -      if( p ){
 17820         -        p->id = iType;
 17821         -        if( DosCreateMutexSem( 0, &p->mutex, 0, FALSE ) != NO_ERROR ){
 17822         -          sqlite3_free( p );
 17823         -          p = NULL;
 17824         -        }
 17825         -      }
 17826         -      break;
 17827         -    }
 17828         -    default: {
 17829         -      static volatile int isInit = 0;
 17830         -      static sqlite3_mutex staticMutexes[6] = {
 17831         -        SQLITE3_MUTEX_INITIALIZER,
 17832         -        SQLITE3_MUTEX_INITIALIZER,
 17833         -        SQLITE3_MUTEX_INITIALIZER,
 17834         -        SQLITE3_MUTEX_INITIALIZER,
 17835         -        SQLITE3_MUTEX_INITIALIZER,
 17836         -        SQLITE3_MUTEX_INITIALIZER,
 17837         -      };
 17838         -      if ( !isInit ){
 17839         -        APIRET rc;
 17840         -        PTIB ptib;
 17841         -        PPIB ppib;
 17842         -        HMTX mutex;
 17843         -        char name[32];
 17844         -        DosGetInfoBlocks( &ptib, &ppib );
 17845         -        sqlite3_snprintf( sizeof(name), name, "\\SEM32\\SQLITE%04x",
 17846         -                          ppib->pib_ulpid );
 17847         -        while( !isInit ){
 17848         -          mutex = 0;
 17849         -          rc = DosCreateMutexSem( name, &mutex, 0, FALSE);
 17850         -          if( rc == NO_ERROR ){
 17851         -            unsigned int i;
 17852         -            if( !isInit ){
 17853         -              for( i = 0; i < sizeof(staticMutexes)/sizeof(staticMutexes[0]); i++ ){
 17854         -                DosCreateMutexSem( 0, &staticMutexes[i].mutex, 0, FALSE );
 17855         -              }
 17856         -              isInit = 1;
 17857         -            }
 17858         -            DosCloseMutexSem( mutex );
 17859         -          }else if( rc == ERROR_DUPLICATE_NAME ){
 17860         -            DosSleep( 1 );
 17861         -          }else{
 17862         -            return p;
 17863         -          }
 17864         -        }
 17865         -      }
 17866         -      assert( iType-2 >= 0 );
 17867         -      assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
 17868         -      p = &staticMutexes[iType-2];
 17869         -      p->id = iType;
 17870         -      break;
 17871         -    }
 17872         -  }
 17873         -  return p;
 17874         -}
 17875         -
 17876         -
 17877         -/*
 17878         -** This routine deallocates a previously allocated mutex.
 17879         -** SQLite is careful to deallocate every mutex that it allocates.
 17880         -*/
 17881         -static void os2MutexFree(sqlite3_mutex *p){
 17882         -#ifdef SQLITE_DEBUG
 17883         -  TID tid;
 17884         -  PID pid;
 17885         -  ULONG ulCount;
 17886         -  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
 17887         -  assert( ulCount==0 );
 17888         -  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
 17889         -#endif
 17890         -  DosCloseMutexSem( p->mutex );
 17891         -  sqlite3_free( p );
 17892         -}
 17893         -
 17894         -#ifdef SQLITE_DEBUG
 17895         -/*
 17896         -** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
 17897         -** intended for use inside assert() statements.
 17898         -*/
 17899         -static int os2MutexHeld(sqlite3_mutex *p){
 17900         -  TID tid;
 17901         -  PID pid;
 17902         -  ULONG ulCount;
 17903         -  PTIB ptib;
 17904         -  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
 17905         -  if( ulCount==0 || ( ulCount>1 && p->id!=SQLITE_MUTEX_RECURSIVE ) )
 17906         -    return 0;
 17907         -  DosGetInfoBlocks(&ptib, NULL);
 17908         -  return tid==ptib->tib_ptib2->tib2_ultid;
 17909         -}
 17910         -static int os2MutexNotheld(sqlite3_mutex *p){
 17911         -  TID tid;
 17912         -  PID pid;
 17913         -  ULONG ulCount;
 17914         -  PTIB ptib;
 17915         -  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
 17916         -  if( ulCount==0 )
 17917         -    return 1;
 17918         -  DosGetInfoBlocks(&ptib, NULL);
 17919         -  return tid!=ptib->tib_ptib2->tib2_ultid;
 17920         -}
 17921         -static void os2MutexTrace(sqlite3_mutex *p, char *pAction){
 17922         -  TID   tid;
 17923         -  PID   pid;
 17924         -  ULONG ulCount;
 17925         -  DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
 17926         -  printf("%s mutex %p (%d) with nRef=%ld\n", pAction, (void*)p, p->trace, ulCount);
 17927         -}
 17928         -#endif
 17929         -
 17930         -/*
 17931         -** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
 17932         -** to enter a mutex.  If another thread is already within the mutex,
 17933         -** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
 17934         -** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
 17935         -** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
 17936         -** be entered multiple times by the same thread.  In such cases the,
 17937         -** mutex must be exited an equal number of times before another thread
 17938         -** can enter.  If the same thread tries to enter any other kind of mutex
 17939         -** more than once, the behavior is undefined.
 17940         -*/
 17941         -static void os2MutexEnter(sqlite3_mutex *p){
 17942         -  assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
 17943         -  DosRequestMutexSem(p->mutex, SEM_INDEFINITE_WAIT);
 17944         -#ifdef SQLITE_DEBUG
 17945         -  if( p->trace ) os2MutexTrace(p, "enter");
 17946         -#endif
 17947         -}
 17948         -static int os2MutexTry(sqlite3_mutex *p){
 17949         -  int rc = SQLITE_BUSY;
 17950         -  assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
 17951         -  if( DosRequestMutexSem(p->mutex, SEM_IMMEDIATE_RETURN) == NO_ERROR ) {
 17952         -    rc = SQLITE_OK;
 17953         -#ifdef SQLITE_DEBUG
 17954         -    if( p->trace ) os2MutexTrace(p, "try");
 17955         -#endif
 17956         -  }
 17957         -  return rc;
 17958         -}
 17959         -
 17960         -/*
 17961         -** The sqlite3_mutex_leave() routine exits a mutex that was
 17962         -** previously entered by the same thread.  The behavior
 17963         -** is undefined if the mutex is not currently entered or
 17964         -** is not currently allocated.  SQLite will never do either.
 17965         -*/
 17966         -static void os2MutexLeave(sqlite3_mutex *p){
 17967         -  assert( os2MutexHeld(p) );
 17968         -  DosReleaseMutexSem(p->mutex);
 17969         -#ifdef SQLITE_DEBUG
 17970         -  if( p->trace ) os2MutexTrace(p, "leave");
 17971         -#endif
 17972         -}
 17973         -
 17974         -SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
 17975         -  static const sqlite3_mutex_methods sMutex = {
 17976         -    os2MutexInit,
 17977         -    os2MutexEnd,
 17978         -    os2MutexAlloc,
 17979         -    os2MutexFree,
 17980         -    os2MutexEnter,
 17981         -    os2MutexTry,
 17982         -    os2MutexLeave,
 17983         -#ifdef SQLITE_DEBUG
 17984         -    os2MutexHeld,
 17985         -    os2MutexNotheld
 17986         -#else
 17987         -    0,
 17988         -    0
 17989         -#endif
 17990         -  };
 17991         -
 17992         -  return &sMutex;
 17993         -}
 17994         -#endif /* SQLITE_MUTEX_OS2 */
 17995         -
 17996         -/************** End of mutex_os2.c *******************************************/
 17997  17723   /************** Begin file mutex_unix.c **************************************/
 17998  17724   /*
 17999  17725   ** 2007 August 28
 18000  17726   **
 18001  17727   ** The author disclaims copyright to this source code.  In place of
 18002  17728   ** a legal notice, here is a blessing:
 18003  17729   **
................................................................................
 18454  18180   /* As winMutexInit() and winMutexEnd() are called as part
 18455  18181   ** of the sqlite3_initialize and sqlite3_shutdown()
 18456  18182   ** processing, the "interlocked" magic is probably not
 18457  18183   ** strictly necessary.
 18458  18184   */
 18459  18185   static long winMutex_lock = 0;
 18460  18186   
 18461         -SQLITE_API extern void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
        18187  +SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
 18462  18188   
 18463  18189   static int winMutexInit(void){ 
 18464  18190     /* The first to increment to 1 does actual initialization */
 18465  18191     if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
 18466  18192       int i;
 18467  18193       for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
 18468  18194   #if SQLITE_OS_WINRT
................................................................................
 19597  19323   ** The counter *cnt is incremented each time.  After counter exceeds
 19598  19324   ** 16 (the number of significant digits in a 64-bit float) '0' is
 19599  19325   ** always returned.
 19600  19326   */
 19601  19327   static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
 19602  19328     int digit;
 19603  19329     LONGDOUBLE_TYPE d;
 19604         -  if( (*cnt)++ >= 16 ) return '0';
        19330  +  if( (*cnt)<=0 ) return '0';
        19331  +  (*cnt)--;
 19605  19332     digit = (int)*val;
 19606  19333     d = digit;
 19607  19334     digit += '0';
 19608  19335     *val = (*val - d)*10.0;
 19609  19336     return (char)digit;
 19610  19337   }
 19611  19338   #endif /* SQLITE_OMIT_FLOATING_POINT */
................................................................................
 19901  19628           exp = 0;
 19902  19629           if( sqlite3IsNaN((double)realvalue) ){
 19903  19630             bufpt = "NaN";
 19904  19631             length = 3;
 19905  19632             break;
 19906  19633           }
 19907  19634           if( realvalue>0.0 ){
 19908         -          while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
 19909         -          while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
 19910         -          while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
        19635  +          LONGDOUBLE_TYPE scale = 1.0;
        19636  +          while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
        19637  +          while( realvalue>=1e64*scale && exp<=350 ){ scale *= 1e64; exp+=64; }
        19638  +          while( realvalue>=1e8*scale && exp<=350 ){ scale *= 1e8; exp+=8; }
        19639  +          while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
        19640  +          realvalue /= scale;
 19911  19641             while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
 19912  19642             while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
 19913  19643             if( exp>350 ){
 19914  19644               if( prefix=='-' ){
 19915  19645                 bufpt = "-Inf";
 19916  19646               }else if( prefix=='+' ){
 19917  19647                 bufpt = "+Inf";
................................................................................
 19936  19666             if( exp<-4 || exp>precision ){
 19937  19667               xtype = etEXP;
 19938  19668             }else{
 19939  19669               precision = precision - exp;
 19940  19670               xtype = etFLOAT;
 19941  19671             }
 19942  19672           }else{
 19943         -          flag_rtz = 0;
        19673  +          flag_rtz = flag_altform2;
 19944  19674           }
 19945  19675           if( xtype==etEXP ){
 19946  19676             e2 = 0;
 19947  19677           }else{
 19948  19678             e2 = exp;
 19949  19679           }
 19950  19680           if( e2+precision+width > etBUFSIZE - 15 ){
................................................................................
 19951  19681             bufpt = zExtra = sqlite3Malloc( e2+precision+width+15 );
 19952  19682             if( bufpt==0 ){
 19953  19683               pAccum->mallocFailed = 1;
 19954  19684               return;
 19955  19685             }
 19956  19686           }
 19957  19687           zOut = bufpt;
 19958         -        nsd = 0;
        19688  +        nsd = 16 + flag_altform2*10;
 19959  19689           flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
 19960  19690           /* The sign in front of the number */
 19961  19691           if( prefix ){
 19962  19692             *(bufpt++) = prefix;
 19963  19693           }
 19964  19694           /* Digits prior to the decimal point */
 19965  19695           if( e2<0 ){
................................................................................
 21524  21254   
 21525  21255       /* adjust the sign of significand */
 21526  21256       s = sign<0 ? -s : s;
 21527  21257   
 21528  21258       /* if exponent, scale significand as appropriate
 21529  21259       ** and store in result. */
 21530  21260       if( e ){
 21531         -      double scale = 1.0;
        21261  +      LONGDOUBLE_TYPE scale = 1.0;
 21532  21262         /* attempt to handle extremely small/large numbers better */
 21533  21263         if( e>307 && e<342 ){
 21534  21264           while( e%308 ) { scale *= 1.0e+1; e -= 1; }
 21535  21265           if( esign<0 ){
 21536  21266             result = s / scale;
 21537  21267             result /= 1.0e+308;
 21538  21268           }else{
................................................................................
 22779  22509        /* 150 */ "Explain",
 22780  22510     };
 22781  22511     return azName[i];
 22782  22512   }
 22783  22513   #endif
 22784  22514   
 22785  22515   /************** End of opcodes.c *********************************************/
 22786         -/************** Begin file os_os2.c ******************************************/
 22787         -/*
 22788         -** 2006 Feb 14
 22789         -**
 22790         -** The author disclaims copyright to this source code.  In place of
 22791         -** a legal notice, here is a blessing:
 22792         -**
 22793         -**    May you do good and not evil.
 22794         -**    May you find forgiveness for yourself and forgive others.
 22795         -**    May you share freely, never taking more than you give.
 22796         -**
 22797         -******************************************************************************
 22798         -**
 22799         -** This file contains code that is specific to OS/2.
 22800         -*/
 22801         -
 22802         -
 22803         -#if SQLITE_OS_OS2
 22804         -
 22805         -/*
 22806         -** A Note About Memory Allocation:
 22807         -**
 22808         -** This driver uses malloc()/free() directly rather than going through
 22809         -** the SQLite-wrappers sqlite3_malloc()/sqlite3_free().  Those wrappers
 22810         -** are designed for use on embedded systems where memory is scarce and
 22811         -** malloc failures happen frequently.  OS/2 does not typically run on
 22812         -** embedded systems, and when it does the developers normally have bigger
 22813         -** problems to worry about than running out of memory.  So there is not
 22814         -** a compelling need to use the wrappers.
 22815         -**
 22816         -** But there is a good reason to not use the wrappers.  If we use the
 22817         -** wrappers then we will get simulated malloc() failures within this
 22818         -** driver.  And that causes all kinds of problems for our tests.  We
 22819         -** could enhance SQLite to deal with simulated malloc failures within
 22820         -** the OS driver, but the code to deal with those failure would not
 22821         -** be exercised on Linux (which does not need to malloc() in the driver)
 22822         -** and so we would have difficulty writing coverage tests for that
 22823         -** code.  Better to leave the code out, we think.
 22824         -**
 22825         -** The point of this discussion is as follows:  When creating a new
 22826         -** OS layer for an embedded system, if you use this file as an example,
 22827         -** avoid the use of malloc()/free().  Those routines work ok on OS/2
 22828         -** desktops but not so well in embedded systems.
 22829         -*/
 22830         -
 22831         -/*
 22832         -** Macros used to determine whether or not to use threads.
 22833         -*/
 22834         -#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE
 22835         -# define SQLITE_OS2_THREADS 1
 22836         -#endif
 22837         -
 22838         -/*
 22839         -** Include code that is common to all os_*.c files
 22840         -*/
 22841         -/************** Include os_common.h in the middle of os_os2.c ****************/
 22842         -/************** Begin file os_common.h ***************************************/
 22843         -/*
 22844         -** 2004 May 22
 22845         -**
 22846         -** The author disclaims copyright to this source code.  In place of
 22847         -** a legal notice, here is a blessing:
 22848         -**
 22849         -**    May you do good and not evil.
 22850         -**    May you find forgiveness for yourself and forgive others.
 22851         -**    May you share freely, never taking more than you give.
 22852         -**
 22853         -******************************************************************************
 22854         -**
 22855         -** This file contains macros and a little bit of code that is common to
 22856         -** all of the platform-specific files (os_*.c) and is #included into those
 22857         -** files.
 22858         -**
 22859         -** This file should be #included by the os_*.c files only.  It is not a
 22860         -** general purpose header file.
 22861         -*/
 22862         -#ifndef _OS_COMMON_H_
 22863         -#define _OS_COMMON_H_
 22864         -
 22865         -/*
 22866         -** At least two bugs have slipped in because we changed the MEMORY_DEBUG
 22867         -** macro to SQLITE_DEBUG and some older makefiles have not yet made the
 22868         -** switch.  The following code should catch this problem at compile-time.
 22869         -*/
 22870         -#ifdef MEMORY_DEBUG
 22871         -# error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
 22872         -#endif
 22873         -
 22874         -#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
 22875         -# ifndef SQLITE_DEBUG_OS_TRACE
 22876         -#   define SQLITE_DEBUG_OS_TRACE 0
 22877         -# endif
 22878         -  int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
 22879         -# define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
 22880         -#else
 22881         -# define OSTRACE(X)
 22882         -#endif
 22883         -
 22884         -/*
 22885         -** Macros for performance tracing.  Normally turned off.  Only works
 22886         -** on i486 hardware.
 22887         -*/
 22888         -#ifdef SQLITE_PERFORMANCE_TRACE
 22889         -
 22890         -/* 
 22891         -** hwtime.h contains inline assembler code for implementing 
 22892         -** high-performance timing routines.
 22893         -*/
 22894         -/************** Include hwtime.h in the middle of os_common.h ****************/
 22895         -/************** Begin file hwtime.h ******************************************/
 22896         -/*
 22897         -** 2008 May 27
 22898         -**
 22899         -** The author disclaims copyright to this source code.  In place of
 22900         -** a legal notice, here is a blessing:
 22901         -**
 22902         -**    May you do good and not evil.
 22903         -**    May you find forgiveness for yourself and forgive others.
 22904         -**    May you share freely, never taking more than you give.
 22905         -**
 22906         -******************************************************************************
 22907         -**
 22908         -** This file contains inline asm code for retrieving "high-performance"
 22909         -** counters for x86 class CPUs.
 22910         -*/
 22911         -#ifndef _HWTIME_H_
 22912         -#define _HWTIME_H_
 22913         -
 22914         -/*
 22915         -** The following routine only works on pentium-class (or newer) processors.
 22916         -** It uses the RDTSC opcode to read the cycle count value out of the
 22917         -** processor and returns that value.  This can be used for high-res
 22918         -** profiling.
 22919         -*/
 22920         -#if (defined(__GNUC__) || defined(_MSC_VER)) && \
 22921         -      (defined(i386) || defined(__i386__) || defined(_M_IX86))
 22922         -
 22923         -  #if defined(__GNUC__)
 22924         -
 22925         -  __inline__ sqlite_uint64 sqlite3Hwtime(void){
 22926         -     unsigned int lo, hi;
 22927         -     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
 22928         -     return (sqlite_uint64)hi << 32 | lo;
 22929         -  }
 22930         -
 22931         -  #elif defined(_MSC_VER)
 22932         -
 22933         -  __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
 22934         -     __asm {
 22935         -        rdtsc
 22936         -        ret       ; return value at EDX:EAX
 22937         -     }
 22938         -  }
 22939         -
 22940         -  #endif
 22941         -
 22942         -#elif (defined(__GNUC__) && defined(__x86_64__))
 22943         -
 22944         -  __inline__ sqlite_uint64 sqlite3Hwtime(void){
 22945         -      unsigned long val;
 22946         -      __asm__ __volatile__ ("rdtsc" : "=A" (val));
 22947         -      return val;
 22948         -  }
 22949         - 
 22950         -#elif (defined(__GNUC__) && defined(__ppc__))
 22951         -
 22952         -  __inline__ sqlite_uint64 sqlite3Hwtime(void){
 22953         -      unsigned long long retval;
 22954         -      unsigned long junk;
 22955         -      __asm__ __volatile__ ("\n\
 22956         -          1:      mftbu   %1\n\
 22957         -                  mftb    %L0\n\
 22958         -                  mftbu   %0\n\
 22959         -                  cmpw    %0,%1\n\
 22960         -                  bne     1b"
 22961         -                  : "=r" (retval), "=r" (junk));
 22962         -      return retval;
 22963         -  }
 22964         -
 22965         -#else
 22966         -
 22967         -  #error Need implementation of sqlite3Hwtime() for your platform.
 22968         -
 22969         -  /*
 22970         -  ** To compile without implementing sqlite3Hwtime() for your platform,
 22971         -  ** you can remove the above #error and use the following
 22972         -  ** stub function.  You will lose timing support for many
 22973         -  ** of the debugging and testing utilities, but it should at
 22974         -  ** least compile and run.
 22975         -  */
 22976         -SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
 22977         -
 22978         -#endif
 22979         -
 22980         -#endif /* !defined(_HWTIME_H_) */
 22981         -
 22982         -/************** End of hwtime.h **********************************************/
 22983         -/************** Continuing where we left off in os_common.h ******************/
 22984         -
 22985         -static sqlite_uint64 g_start;
 22986         -static sqlite_uint64 g_elapsed;
 22987         -#define TIMER_START       g_start=sqlite3Hwtime()
 22988         -#define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
 22989         -#define TIMER_ELAPSED     g_elapsed
 22990         -#else
 22991         -#define TIMER_START
 22992         -#define TIMER_END
 22993         -#define TIMER_ELAPSED     ((sqlite_uint64)0)
 22994         -#endif
 22995         -
 22996         -/*
 22997         -** If we compile with the SQLITE_TEST macro set, then the following block
 22998         -** of code will give us the ability to simulate a disk I/O error.  This
 22999         -** is used for testing the I/O recovery logic.
 23000         -*/
 23001         -#ifdef SQLITE_TEST
 23002         -SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
 23003         -SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
 23004         -SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
 23005         -SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
 23006         -SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
 23007         -SQLITE_API int sqlite3_diskfull_pending = 0;
 23008         -SQLITE_API int sqlite3_diskfull = 0;
 23009         -#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
 23010         -#define SimulateIOError(CODE)  \
 23011         -  if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
 23012         -       || sqlite3_io_error_pending-- == 1 )  \
 23013         -              { local_ioerr(); CODE; }
 23014         -static void local_ioerr(){
 23015         -  IOTRACE(("IOERR\n"));
 23016         -  sqlite3_io_error_hit++;
 23017         -  if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
 23018         -}
 23019         -#define SimulateDiskfullError(CODE) \
 23020         -   if( sqlite3_diskfull_pending ){ \
 23021         -     if( sqlite3_diskfull_pending == 1 ){ \
 23022         -       local_ioerr(); \
 23023         -       sqlite3_diskfull = 1; \
 23024         -       sqlite3_io_error_hit = 1; \
 23025         -       CODE; \
 23026         -     }else{ \
 23027         -       sqlite3_diskfull_pending--; \
 23028         -     } \
 23029         -   }
 23030         -#else
 23031         -#define SimulateIOErrorBenign(X)
 23032         -#define SimulateIOError(A)
 23033         -#define SimulateDiskfullError(A)
 23034         -#endif
 23035         -
 23036         -/*
 23037         -** When testing, keep a count of the number of open files.
 23038         -*/
 23039         -#ifdef SQLITE_TEST
 23040         -SQLITE_API int sqlite3_open_file_count = 0;
 23041         -#define OpenCounter(X)  sqlite3_open_file_count+=(X)
 23042         -#else
 23043         -#define OpenCounter(X)
 23044         -#endif
 23045         -
 23046         -#endif /* !defined(_OS_COMMON_H_) */
 23047         -
 23048         -/************** End of os_common.h *******************************************/
 23049         -/************** Continuing where we left off in os_os2.c *********************/
 23050         -
 23051         -/* Forward references */
 23052         -typedef struct os2File os2File;         /* The file structure */
 23053         -typedef struct os2ShmNode os2ShmNode;   /* A shared descritive memory node */
 23054         -typedef struct os2ShmLink os2ShmLink;   /* A connection to shared-memory */
 23055         -
 23056         -/*
 23057         -** The os2File structure is subclass of sqlite3_file specific for the OS/2
 23058         -** protability layer.
 23059         -*/
 23060         -struct os2File {
 23061         -  const sqlite3_io_methods *pMethod;  /* Always the first entry */
 23062         -  HFILE h;                  /* Handle for accessing the file */
 23063         -  int flags;                /* Flags provided to os2Open() */
 23064         -  int locktype;             /* Type of lock currently held on this file */
 23065         -  int szChunk;              /* Chunk size configured by FCNTL_CHUNK_SIZE */
 23066         -  char *zFullPathCp;        /* Full path name of this file */
 23067         -  os2ShmLink *pShmLink;     /* Instance of shared memory on this file */
 23068         -};
 23069         -
 23070         -#define LOCK_TIMEOUT 10L /* the default locking timeout */
 23071         -
 23072         -/*
 23073         -** Missing from some versions of the OS/2 toolkit -
 23074         -** used to allocate from high memory if possible
 23075         -*/
 23076         -#ifndef OBJ_ANY
 23077         -# define OBJ_ANY 0x00000400
 23078         -#endif
 23079         -
 23080         -/*****************************************************************************
 23081         -** The next group of routines implement the I/O methods specified
 23082         -** by the sqlite3_io_methods object.
 23083         -******************************************************************************/
 23084         -
 23085         -/*
 23086         -** Close a file.
 23087         -*/
 23088         -static int os2Close( sqlite3_file *id ){
 23089         -  APIRET rc;
 23090         -  os2File *pFile = (os2File*)id;
 23091         -
 23092         -  assert( id!=0 );
 23093         -  OSTRACE(( "CLOSE %d (%s)\n", pFile->h, pFile->zFullPathCp ));
 23094         -
 23095         -  rc = DosClose( pFile->h );
 23096         -
 23097         -  if( pFile->flags & SQLITE_OPEN_DELETEONCLOSE )
 23098         -    DosForceDelete( (PSZ)pFile->zFullPathCp );
 23099         -
 23100         -  free( pFile->zFullPathCp );
 23101         -  pFile->zFullPathCp = NULL;
 23102         -  pFile->locktype = NO_LOCK;
 23103         -  pFile->h = (HFILE)-1;
 23104         -  pFile->flags = 0;
 23105         -
 23106         -  OpenCounter( -1 );
 23107         -  return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
 23108         -}
 23109         -
 23110         -/*
 23111         -** Read data from a file into a buffer.  Return SQLITE_OK if all
 23112         -** bytes were read successfully and SQLITE_IOERR if anything goes
 23113         -** wrong.
 23114         -*/
 23115         -static int os2Read(
 23116         -  sqlite3_file *id,               /* File to read from */
 23117         -  void *pBuf,                     /* Write content into this buffer */
 23118         -  int amt,                        /* Number of bytes to read */
 23119         -  sqlite3_int64 offset            /* Begin reading at this offset */
 23120         -){
 23121         -  ULONG fileLocation = 0L;
 23122         -  ULONG got;
 23123         -  os2File *pFile = (os2File*)id;
 23124         -  assert( id!=0 );
 23125         -  SimulateIOError( return SQLITE_IOERR_READ );
 23126         -  OSTRACE(( "READ %d lock=%d\n", pFile->h, pFile->locktype ));
 23127         -  if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
 23128         -    return SQLITE_IOERR;
 23129         -  }
 23130         -  if( DosRead( pFile->h, pBuf, amt, &got ) != NO_ERROR ){
 23131         -    return SQLITE_IOERR_READ;
 23132         -  }
 23133         -  if( got == (ULONG)amt )
 23134         -    return SQLITE_OK;
 23135         -  else {
 23136         -    /* Unread portions of the input buffer must be zero-filled */
 23137         -    memset(&((char*)pBuf)[got], 0, amt-got);
 23138         -    return SQLITE_IOERR_SHORT_READ;
 23139         -  }
 23140         -}
 23141         -
 23142         -/*
 23143         -** Write data from a buffer into a file.  Return SQLITE_OK on success
 23144         -** or some other error code on failure.
 23145         -*/
 23146         -static int os2Write(
 23147         -  sqlite3_file *id,               /* File to write into */
 23148         -  const void *pBuf,               /* The bytes to be written */
 23149         -  int amt,                        /* Number of bytes to write */
 23150         -  sqlite3_int64 offset            /* Offset into the file to begin writing at */
 23151         -){
 23152         -  ULONG fileLocation = 0L;
 23153         -  APIRET rc = NO_ERROR;
 23154         -  ULONG wrote;
 23155         -  os2File *pFile = (os2File*)id;
 23156         -  assert( id!=0 );
 23157         -  SimulateIOError( return SQLITE_IOERR_WRITE );
 23158         -  SimulateDiskfullError( return SQLITE_FULL );
 23159         -  OSTRACE(( "WRITE %d lock=%d\n", pFile->h, pFile->locktype ));
 23160         -  if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
 23161         -    return SQLITE_IOERR;
 23162         -  }
 23163         -  assert( amt>0 );
 23164         -  while( amt > 0 &&
 23165         -         ( rc = DosWrite( pFile->h, (PVOID)pBuf, amt, &wrote ) ) == NO_ERROR &&
 23166         -         wrote > 0
 23167         -  ){
 23168         -    amt -= wrote;
 23169         -    pBuf = &((char*)pBuf)[wrote];
 23170         -  }
 23171         -
 23172         -  return ( rc != NO_ERROR || amt > (int)wrote ) ? SQLITE_FULL : SQLITE_OK;
 23173         -}
 23174         -
 23175         -/*
 23176         -** Truncate an open file to a specified size
 23177         -*/
 23178         -static int os2Truncate( sqlite3_file *id, i64 nByte ){
 23179         -  APIRET rc;
 23180         -  os2File *pFile = (os2File*)id;
 23181         -  assert( id!=0 );
 23182         -  OSTRACE(( "TRUNCATE %d %lld\n", pFile->h, nByte ));
 23183         -  SimulateIOError( return SQLITE_IOERR_TRUNCATE );
 23184         -
 23185         -  /* If the user has configured a chunk-size for this file, truncate the
 23186         -  ** file so that it consists of an integer number of chunks (i.e. the
 23187         -  ** actual file size after the operation may be larger than the requested
 23188         -  ** size).
 23189         -  */
 23190         -  if( pFile->szChunk ){
 23191         -    nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
 23192         -  }
 23193         -  
 23194         -  rc = DosSetFileSize( pFile->h, nByte );
 23195         -  return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR_TRUNCATE;
 23196         -}
 23197         -
 23198         -#ifdef SQLITE_TEST
 23199         -/*
 23200         -** Count the number of fullsyncs and normal syncs.  This is used to test
 23201         -** that syncs and fullsyncs are occuring at the right times.
 23202         -*/
 23203         -SQLITE_API int sqlite3_sync_count = 0;
 23204         -SQLITE_API int sqlite3_fullsync_count = 0;
 23205         -#endif
 23206         -
 23207         -/*
 23208         -** Make sure all writes to a particular file are committed to disk.
 23209         -*/
 23210         -static int os2Sync( sqlite3_file *id, int flags ){
 23211         -  os2File *pFile = (os2File*)id;
 23212         -  OSTRACE(( "SYNC %d lock=%d\n", pFile->h, pFile->locktype ));
 23213         -#ifdef SQLITE_TEST
 23214         -  if( flags & SQLITE_SYNC_FULL){
 23215         -    sqlite3_fullsync_count++;
 23216         -  }
 23217         -  sqlite3_sync_count++;
 23218         -#endif
 23219         -  /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
 23220         -  ** no-op
 23221         -  */
 23222         -#ifdef SQLITE_NO_SYNC
 23223         -  UNUSED_PARAMETER(pFile);
 23224         -  return SQLITE_OK;
 23225         -#else
 23226         -  return DosResetBuffer( pFile->h ) == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
 23227         -#endif
 23228         -}
 23229         -
 23230         -/*
 23231         -** Determine the current size of a file in bytes
 23232         -*/
 23233         -static int os2FileSize( sqlite3_file *id, sqlite3_int64 *pSize ){
 23234         -  APIRET rc = NO_ERROR;
 23235         -  FILESTATUS3 fsts3FileInfo;
 23236         -  memset(&fsts3FileInfo, 0, sizeof(fsts3FileInfo));
 23237         -  assert( id!=0 );
 23238         -  SimulateIOError( return SQLITE_IOERR_FSTAT );
 23239         -  rc = DosQueryFileInfo( ((os2File*)id)->h, FIL_STANDARD, &fsts3FileInfo, sizeof(FILESTATUS3) );
 23240         -  if( rc == NO_ERROR ){
 23241         -    *pSize = fsts3FileInfo.cbFile;
 23242         -    return SQLITE_OK;
 23243         -  }else{
 23244         -    return SQLITE_IOERR_FSTAT;
 23245         -  }
 23246         -}
 23247         -
 23248         -/*
 23249         -** Acquire a reader lock.
 23250         -*/
 23251         -static int getReadLock( os2File *pFile ){
 23252         -  FILELOCK  LockArea,
 23253         -            UnlockArea;
 23254         -  APIRET res;
 23255         -  memset(&LockArea, 0, sizeof(LockArea));
 23256         -  memset(&UnlockArea, 0, sizeof(UnlockArea));
 23257         -  LockArea.lOffset = SHARED_FIRST;
 23258         -  LockArea.lRange = SHARED_SIZE;
 23259         -  UnlockArea.lOffset = 0L;
 23260         -  UnlockArea.lRange = 0L;
 23261         -  res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
 23262         -  OSTRACE(( "GETREADLOCK %d res=%d\n", pFile->h, res ));
 23263         -  return res;
 23264         -}
 23265         -
 23266         -/*
 23267         -** Undo a readlock
 23268         -*/
 23269         -static int unlockReadLock( os2File *id ){
 23270         -  FILELOCK  LockArea,
 23271         -            UnlockArea;
 23272         -  APIRET res;
 23273         -  memset(&LockArea, 0, sizeof(LockArea));
 23274         -  memset(&UnlockArea, 0, sizeof(UnlockArea));
 23275         -  LockArea.lOffset = 0L;
 23276         -  LockArea.lRange = 0L;
 23277         -  UnlockArea.lOffset = SHARED_FIRST;
 23278         -  UnlockArea.lRange = SHARED_SIZE;
 23279         -  res = DosSetFileLocks( id->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
 23280         -  OSTRACE(( "UNLOCK-READLOCK file handle=%d res=%d?\n", id->h, res ));
 23281         -  return res;
 23282         -}
 23283         -
 23284         -/*
 23285         -** Lock the file with the lock specified by parameter locktype - one
 23286         -** of the following:
 23287         -**
 23288         -**     (1) SHARED_LOCK
 23289         -**     (2) RESERVED_LOCK
 23290         -**     (3) PENDING_LOCK
 23291         -**     (4) EXCLUSIVE_LOCK
 23292         -**
 23293         -** Sometimes when requesting one lock state, additional lock states
 23294         -** are inserted in between.  The locking might fail on one of the later
 23295         -** transitions leaving the lock state different from what it started but
 23296         -** still short of its goal.  The following chart shows the allowed
 23297         -** transitions and the inserted intermediate states:
 23298         -**
 23299         -**    UNLOCKED -> SHARED
 23300         -**    SHARED -> RESERVED
 23301         -**    SHARED -> (PENDING) -> EXCLUSIVE
 23302         -**    RESERVED -> (PENDING) -> EXCLUSIVE
 23303         -**    PENDING -> EXCLUSIVE
 23304         -**
 23305         -** This routine will only increase a lock.  The os2Unlock() routine
 23306         -** erases all locks at once and returns us immediately to locking level 0.
 23307         -** It is not possible to lower the locking level one step at a time.  You
 23308         -** must go straight to locking level 0.
 23309         -*/
 23310         -static int os2Lock( sqlite3_file *id, int locktype ){
 23311         -  int rc = SQLITE_OK;       /* Return code from subroutines */
 23312         -  APIRET res = NO_ERROR;    /* Result of an OS/2 lock call */
 23313         -  int newLocktype;       /* Set pFile->locktype to this value before exiting */
 23314         -  int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
 23315         -  FILELOCK  LockArea,
 23316         -            UnlockArea;
 23317         -  os2File *pFile = (os2File*)id;
 23318         -  memset(&LockArea, 0, sizeof(LockArea));
 23319         -  memset(&UnlockArea, 0, sizeof(UnlockArea));
 23320         -  assert( pFile!=0 );
 23321         -  OSTRACE(( "LOCK %d %d was %d\n", pFile->h, locktype, pFile->locktype ));
 23322         -
 23323         -  /* If there is already a lock of this type or more restrictive on the
 23324         -  ** os2File, do nothing. Don't use the end_lock: exit path, as
 23325         -  ** sqlite3_mutex_enter() hasn't been called yet.
 23326         -  */
 23327         -  if( pFile->locktype>=locktype ){
 23328         -    OSTRACE(( "LOCK %d %d ok (already held)\n", pFile->h, locktype ));
 23329         -    return SQLITE_OK;
 23330         -  }
 23331         -
 23332         -  /* Make sure the locking sequence is correct
 23333         -  */
 23334         -  assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
 23335         -  assert( locktype!=PENDING_LOCK );
 23336         -  assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
 23337         -
 23338         -  /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
 23339         -  ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
 23340         -  ** the PENDING_LOCK byte is temporary.
 23341         -  */
 23342         -  newLocktype = pFile->locktype;
 23343         -  if( pFile->locktype==NO_LOCK
 23344         -      || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK)
 23345         -  ){
 23346         -    LockArea.lOffset = PENDING_BYTE;
 23347         -    LockArea.lRange = 1L;
 23348         -    UnlockArea.lOffset = 0L;
 23349         -    UnlockArea.lRange = 0L;
 23350         -
 23351         -    /* wait longer than LOCK_TIMEOUT here not to have to try multiple times */
 23352         -    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 100L, 0L );
 23353         -    if( res == NO_ERROR ){
 23354         -      gotPendingLock = 1;
 23355         -      OSTRACE(( "LOCK %d pending lock boolean set.  res=%d\n", pFile->h, res ));
 23356         -    }
 23357         -  }
 23358         -
 23359         -  /* Acquire a shared lock
 23360         -  */
 23361         -  if( locktype==SHARED_LOCK && res == NO_ERROR ){
 23362         -    assert( pFile->locktype==NO_LOCK );
 23363         -    res = getReadLock(pFile);
 23364         -    if( res == NO_ERROR ){
 23365         -      newLocktype = SHARED_LOCK;
 23366         -    }
 23367         -    OSTRACE(( "LOCK %d acquire shared lock. res=%d\n", pFile->h, res ));
 23368         -  }
 23369         -
 23370         -  /* Acquire a RESERVED lock
 23371         -  */
 23372         -  if( locktype==RESERVED_LOCK && res == NO_ERROR ){
 23373         -    assert( pFile->locktype==SHARED_LOCK );
 23374         -    LockArea.lOffset = RESERVED_BYTE;
 23375         -    LockArea.lRange = 1L;
 23376         -    UnlockArea.lOffset = 0L;
 23377         -    UnlockArea.lRange = 0L;
 23378         -    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
 23379         -    if( res == NO_ERROR ){
 23380         -      newLocktype = RESERVED_LOCK;
 23381         -    }
 23382         -    OSTRACE(( "LOCK %d acquire reserved lock. res=%d\n", pFile->h, res ));
 23383         -  }
 23384         -
 23385         -  /* Acquire a PENDING lock
 23386         -  */
 23387         -  if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
 23388         -    newLocktype = PENDING_LOCK;
 23389         -    gotPendingLock = 0;
 23390         -    OSTRACE(( "LOCK %d acquire pending lock. pending lock boolean unset.\n",
 23391         -               pFile->h ));
 23392         -  }
 23393         -
 23394         -  /* Acquire an EXCLUSIVE lock
 23395         -  */
 23396         -  if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
 23397         -    assert( pFile->locktype>=SHARED_LOCK );
 23398         -    res = unlockReadLock(pFile);
 23399         -    OSTRACE(( "unreadlock = %d\n", res ));
 23400         -    LockArea.lOffset = SHARED_FIRST;
 23401         -    LockArea.lRange = SHARED_SIZE;
 23402         -    UnlockArea.lOffset = 0L;
 23403         -    UnlockArea.lRange = 0L;
 23404         -    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
 23405         -    if( res == NO_ERROR ){
 23406         -      newLocktype = EXCLUSIVE_LOCK;
 23407         -    }else{
 23408         -      OSTRACE(( "OS/2 error-code = %d\n", res ));
 23409         -      getReadLock(pFile);
 23410         -    }
 23411         -    OSTRACE(( "LOCK %d acquire exclusive lock.  res=%d\n", pFile->h, res ));
 23412         -  }
 23413         -
 23414         -  /* If we are holding a PENDING lock that ought to be released, then
 23415         -  ** release it now.
 23416         -  */
 23417         -  if( gotPendingLock && locktype==SHARED_LOCK ){
 23418         -    int r;
 23419         -    LockArea.lOffset = 0L;
 23420         -    LockArea.lRange = 0L;
 23421         -    UnlockArea.lOffset = PENDING_BYTE;
 23422         -    UnlockArea.lRange = 1L;
 23423         -    r = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
 23424         -    OSTRACE(( "LOCK %d unlocking pending/is shared. r=%d\n", pFile->h, r ));
 23425         -  }
 23426         -
 23427         -  /* Update the state of the lock has held in the file descriptor then
 23428         -  ** return the appropriate result code.
 23429         -  */
 23430         -  if( res == NO_ERROR ){
 23431         -    rc = SQLITE_OK;
 23432         -  }else{
 23433         -    OSTRACE(( "LOCK FAILED %d trying for %d but got %d\n", pFile->h,
 23434         -              locktype, newLocktype ));
 23435         -    rc = SQLITE_BUSY;
 23436         -  }
 23437         -  pFile->locktype = newLocktype;
 23438         -  OSTRACE(( "LOCK %d now %d\n", pFile->h, pFile->locktype ));
 23439         -  return rc;
 23440         -}
 23441         -
 23442         -/*
 23443         -** This routine checks if there is a RESERVED lock held on the specified
 23444         -** file by this or any other process. If such a lock is held, return
 23445         -** non-zero, otherwise zero.
 23446         -*/
 23447         -static int os2CheckReservedLock( sqlite3_file *id, int *pOut ){
 23448         -  int r = 0;
 23449         -  os2File *pFile = (os2File*)id;
 23450         -  assert( pFile!=0 );
 23451         -  if( pFile->locktype>=RESERVED_LOCK ){
 23452         -    r = 1;
 23453         -    OSTRACE(( "TEST WR-LOCK %d %d (local)\n", pFile->h, r ));
 23454         -  }else{
 23455         -    FILELOCK  LockArea,
 23456         -              UnlockArea;
 23457         -    APIRET rc = NO_ERROR;
 23458         -    memset(&LockArea, 0, sizeof(LockArea));
 23459         -    memset(&UnlockArea, 0, sizeof(UnlockArea));
 23460         -    LockArea.lOffset = RESERVED_BYTE;
 23461         -    LockArea.lRange = 1L;
 23462         -    UnlockArea.lOffset = 0L;
 23463         -    UnlockArea.lRange = 0L;
 23464         -    rc = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
 23465         -    OSTRACE(( "TEST WR-LOCK %d lock reserved byte rc=%d\n", pFile->h, rc ));
 23466         -    if( rc == NO_ERROR ){
 23467         -      APIRET rcu = NO_ERROR; /* return code for unlocking */
 23468         -      LockArea.lOffset = 0L;
 23469         -      LockArea.lRange = 0L;
 23470         -      UnlockArea.lOffset = RESERVED_BYTE;
 23471         -      UnlockArea.lRange = 1L;
 23472         -      rcu = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
 23473         -      OSTRACE(( "TEST WR-LOCK %d unlock reserved byte r=%d\n", pFile->h, rcu ));
 23474         -    }
 23475         -    r = !(rc == NO_ERROR);
 23476         -    OSTRACE(( "TEST WR-LOCK %d %d (remote)\n", pFile->h, r ));
 23477         -  }
 23478         -  *pOut = r;
 23479         -  return SQLITE_OK;
 23480         -}
 23481         -
 23482         -/*
 23483         -** Lower the locking level on file descriptor id to locktype.  locktype
 23484         -** must be either NO_LOCK or SHARED_LOCK.
 23485         -**
 23486         -** If the locking level of the file descriptor is already at or below
 23487         -** the requested locking level, this routine is a no-op.
 23488         -**
 23489         -** It is not possible for this routine to fail if the second argument
 23490         -** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
 23491         -** might return SQLITE_IOERR;
 23492         -*/
 23493         -static int os2Unlock( sqlite3_file *id, int locktype ){
 23494         -  int type;
 23495         -  os2File *pFile = (os2File*)id;
 23496         -  APIRET rc = SQLITE_OK;
 23497         -  APIRET res = NO_ERROR;
 23498         -  FILELOCK  LockArea,
 23499         -            UnlockArea;
 23500         -  memset(&LockArea, 0, sizeof(LockArea));
 23501         -  memset(&UnlockArea, 0, sizeof(UnlockArea));
 23502         -  assert( pFile!=0 );
 23503         -  assert( locktype<=SHARED_LOCK );
 23504         -  OSTRACE(( "UNLOCK %d to %d was %d\n", pFile->h, locktype, pFile->locktype ));
 23505         -  type = pFile->locktype;
 23506         -  if( type>=EXCLUSIVE_LOCK ){
 23507         -    LockArea.lOffset = 0L;
 23508         -    LockArea.lRange = 0L;
 23509         -    UnlockArea.lOffset = SHARED_FIRST;
 23510         -    UnlockArea.lRange = SHARED_SIZE;
 23511         -    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
 23512         -    OSTRACE(( "UNLOCK %d exclusive lock res=%d\n", pFile->h, res ));
 23513         -    if( locktype==SHARED_LOCK && getReadLock(pFile) != NO_ERROR ){
 23514         -      /* This should never happen.  We should always be able to
 23515         -      ** reacquire the read lock */
 23516         -      OSTRACE(( "UNLOCK %d to %d getReadLock() failed\n", pFile->h, locktype ));
 23517         -      rc = SQLITE_IOERR_UNLOCK;
 23518         -    }
 23519         -  }
 23520         -  if( type>=RESERVED_LOCK ){
 23521         -    LockArea.lOffset = 0L;
 23522         -    LockArea.lRange = 0L;
 23523         -    UnlockArea.lOffset = RESERVED_BYTE;
 23524         -    UnlockArea.lRange = 1L;
 23525         -    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
 23526         -    OSTRACE(( "UNLOCK %d reserved res=%d\n", pFile->h, res ));
 23527         -  }
 23528         -  if( locktype==NO_LOCK && type>=SHARED_LOCK ){
 23529         -    res = unlockReadLock(pFile);
 23530         -    OSTRACE(( "UNLOCK %d is %d want %d res=%d\n",
 23531         -              pFile->h, type, locktype, res ));
 23532         -  }
 23533         -  if( type>=PENDING_LOCK ){
 23534         -    LockArea.lOffset = 0L;
 23535         -    LockArea.lRange = 0L;
 23536         -    UnlockArea.lOffset = PENDING_BYTE;
 23537         -    UnlockArea.lRange = 1L;
 23538         -    res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
 23539         -    OSTRACE(( "UNLOCK %d pending res=%d\n", pFile->h, res ));
 23540         -  }
 23541         -  pFile->locktype = locktype;
 23542         -  OSTRACE(( "UNLOCK %d now %d\n", pFile->h, pFile->locktype ));
 23543         -  return rc;
 23544         -}
 23545         -
 23546         -/*
 23547         -** Control and query of the open file handle.
 23548         -*/
 23549         -static int os2FileControl(sqlite3_file *id, int op, void *pArg){
 23550         -  switch( op ){
 23551         -    case SQLITE_FCNTL_LOCKSTATE: {
 23552         -      *(int*)pArg = ((os2File*)id)->locktype;
 23553         -      OSTRACE(( "FCNTL_LOCKSTATE %d lock=%d\n",
 23554         -                ((os2File*)id)->h, ((os2File*)id)->locktype ));
 23555         -      return SQLITE_OK;
 23556         -    }
 23557         -    case SQLITE_FCNTL_CHUNK_SIZE: {
 23558         -      ((os2File*)id)->szChunk = *(int*)pArg;
 23559         -      return SQLITE_OK;
 23560         -    }
 23561         -    case SQLITE_FCNTL_SIZE_HINT: {
 23562         -      sqlite3_int64 sz = *(sqlite3_int64*)pArg;
 23563         -      SimulateIOErrorBenign(1);
 23564         -      os2Truncate(id, sz);
 23565         -      SimulateIOErrorBenign(0);
 23566         -      return SQLITE_OK;
 23567         -    }
 23568         -    case SQLITE_FCNTL_SYNC_OMITTED: {
 23569         -      return SQLITE_OK;
 23570         -    }
 23571         -  }
 23572         -  return SQLITE_NOTFOUND;
 23573         -}
 23574         -
 23575         -/*
 23576         -** Return the sector size in bytes of the underlying block device for
 23577         -** the specified file. This is almost always 512 bytes, but may be
 23578         -** larger for some devices.
 23579         -**
 23580         -** SQLite code assumes this function cannot fail. It also assumes that
 23581         -** if two files are created in the same file-system directory (i.e.
 23582         -** a database and its journal file) that the sector size will be the
 23583         -** same for both.
 23584         -*/
 23585         -static int os2SectorSize(sqlite3_file *id){
 23586         -  UNUSED_PARAMETER(id);
 23587         -  return SQLITE_DEFAULT_SECTOR_SIZE;
 23588         -}
 23589         -
 23590         -/*
 23591         -** Return a vector of device characteristics.
 23592         -*/
 23593         -static int os2DeviceCharacteristics(sqlite3_file *id){
 23594         -  UNUSED_PARAMETER(id);
 23595         -  return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;
 23596         -}
 23597         -
 23598         -
 23599         -/*
 23600         -** Character set conversion objects used by conversion routines.
 23601         -*/
 23602         -static UconvObject ucUtf8 = NULL; /* convert between UTF-8 and UCS-2 */
 23603         -static UconvObject uclCp = NULL;  /* convert between local codepage and UCS-2 */
 23604         -
 23605         -/*
 23606         -** Helper function to initialize the conversion objects from and to UTF-8.
 23607         -*/
 23608         -static void initUconvObjects( void ){
 23609         -  if( UniCreateUconvObject( UTF_8, &ucUtf8 ) != ULS_SUCCESS )
 23610         -    ucUtf8 = NULL;
 23611         -  if ( UniCreateUconvObject( (UniChar *)L"@path=yes", &uclCp ) != ULS_SUCCESS )
 23612         -    uclCp = NULL;
 23613         -}
 23614         -
 23615         -/*
 23616         -** Helper function to free the conversion objects from and to UTF-8.
 23617         -*/
 23618         -static void freeUconvObjects( void ){
 23619         -  if ( ucUtf8 )
 23620         -    UniFreeUconvObject( ucUtf8 );
 23621         -  if ( uclCp )
 23622         -    UniFreeUconvObject( uclCp );
 23623         -  ucUtf8 = NULL;
 23624         -  uclCp = NULL;
 23625         -}
 23626         -
 23627         -/*
 23628         -** Helper function to convert UTF-8 filenames to local OS/2 codepage.
 23629         -** The two-step process: first convert the incoming UTF-8 string
 23630         -** into UCS-2 and then from UCS-2 to the current codepage.
 23631         -** The returned char pointer has to be freed.
 23632         -*/
 23633         -static char *convertUtf8PathToCp( const char *in ){
 23634         -  UniChar tempPath[CCHMAXPATH];
 23635         -  char *out = (char *)calloc( CCHMAXPATH, 1 );
 23636         -
 23637         -  if( !out )
 23638         -    return NULL;
 23639         -
 23640         -  if( !ucUtf8 || !uclCp )
 23641         -    initUconvObjects();
 23642         -
 23643         -  /* determine string for the conversion of UTF-8 which is CP1208 */
 23644         -  if( UniStrToUcs( ucUtf8, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
 23645         -    return out; /* if conversion fails, return the empty string */
 23646         -
 23647         -  /* conversion for current codepage which can be used for paths */
 23648         -  UniStrFromUcs( uclCp, out, tempPath, CCHMAXPATH );
 23649         -
 23650         -  return out;
 23651         -}
 23652         -
 23653         -/*
 23654         -** Helper function to convert filenames from local codepage to UTF-8.
 23655         -** The two-step process: first convert the incoming codepage-specific
 23656         -** string into UCS-2 and then from UCS-2 to the codepage of UTF-8.
 23657         -** The returned char pointer has to be freed.
 23658         -**
 23659         -** This function is non-static to be able to use this in shell.c and
 23660         -** similar applications that take command line arguments.
 23661         -*/
 23662         -char *convertCpPathToUtf8( const char *in ){
 23663         -  UniChar tempPath[CCHMAXPATH];
 23664         -  char *out = (char *)calloc( CCHMAXPATH, 1 );
 23665         -
 23666         -  if( !out )
 23667         -    return NULL;
 23668         -
 23669         -  if( !ucUtf8 || !uclCp )
 23670         -    initUconvObjects();
 23671         -
 23672         -  /* conversion for current codepage which can be used for paths */
 23673         -  if( UniStrToUcs( uclCp, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
 23674         -    return out; /* if conversion fails, return the empty string */
 23675         -
 23676         -  /* determine string for the conversion of UTF-8 which is CP1208 */
 23677         -  UniStrFromUcs( ucUtf8, out, tempPath, CCHMAXPATH );
 23678         -
 23679         -  return out;
 23680         -}
 23681         -
 23682         -
 23683         -#ifndef SQLITE_OMIT_WAL
 23684         -
 23685         -/*
 23686         -** Use main database file for interprocess locking. If un-defined
 23687         -** a separate file is created for this purpose. The file will be
 23688         -** used only to set file locks. There will be no data written to it.
 23689         -*/
 23690         -#define SQLITE_OS2_NO_WAL_LOCK_FILE     
 23691         -
 23692         -#if 0
 23693         -static void _ERR_TRACE( const char *fmt, ... ) {
 23694         -  va_list  ap;
 23695         -  va_start(ap, fmt);
 23696         -  vfprintf(stderr, fmt, ap);
 23697         -  fflush(stderr);
 23698         -}
 23699         -#define ERR_TRACE(rc, msg)        \
 23700         -        if( (rc) != SQLITE_OK ) _ERR_TRACE msg;
 23701         -#else
 23702         -#define ERR_TRACE(rc, msg)
 23703         -#endif
 23704         -
 23705         -/*
 23706         -** Helper functions to obtain and relinquish the global mutex. The
 23707         -** global mutex is used to protect os2ShmNodeList.
 23708         -**
 23709         -** Function os2ShmMutexHeld() is used to assert() that the global mutex 
 23710         -** is held when required. This function is only used as part of assert() 
 23711         -** statements. e.g.
 23712         -**
 23713         -**   os2ShmEnterMutex()
 23714         -**     assert( os2ShmMutexHeld() );
 23715         -**   os2ShmLeaveMutex()
 23716         -*/
 23717         -static void os2ShmEnterMutex(void){
 23718         -  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 23719         -}
 23720         -static void os2ShmLeaveMutex(void){
 23721         -  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 23722         -}
 23723         -#ifdef SQLITE_DEBUG
 23724         -static int os2ShmMutexHeld(void) {
 23725         -  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
 23726         -}
 23727         -int GetCurrentProcessId(void) {
 23728         -  PPIB pib;
 23729         -  DosGetInfoBlocks(NULL, &pib);
 23730         -  return (int)pib->pib_ulpid;
 23731         -}
 23732         -#endif
 23733         -
 23734         -/*
 23735         -** Object used to represent a the shared memory area for a single log file.
 23736         -** When multiple threads all reference the same log-summary, each thread has
 23737         -** its own os2File object, but they all point to a single instance of this 
 23738         -** object.  In other words, each log-summary is opened only once per process.
 23739         -**
 23740         -** os2ShmMutexHeld() must be true when creating or destroying
 23741         -** this object or while reading or writing the following fields:
 23742         -**
 23743         -**      nRef
 23744         -**      pNext 
 23745         -**
 23746         -** The following fields are read-only after the object is created:
 23747         -** 
 23748         -**      szRegion
 23749         -**      hLockFile
 23750         -**      shmBaseName
 23751         -**
 23752         -** Either os2ShmNode.mutex must be held or os2ShmNode.nRef==0 and
 23753         -** os2ShmMutexHeld() is true when reading or writing any other field
 23754         -** in this structure.
 23755         -**
 23756         -*/
 23757         -struct os2ShmNode {
 23758         -  sqlite3_mutex *mutex;      /* Mutex to access this object */
 23759         -  os2ShmNode *pNext;         /* Next in list of all os2ShmNode objects */
 23760         -
 23761         -  int szRegion;              /* Size of shared-memory regions */
 23762         -
 23763         -  int nRegion;               /* Size of array apRegion */
 23764         -  void **apRegion;           /* Array of pointers to shared-memory regions */
 23765         -
 23766         -  int nRef;                  /* Number of os2ShmLink objects pointing to this */
 23767         -  os2ShmLink *pFirst;        /* First os2ShmLink object pointing to this */
 23768         -
 23769         -  HFILE hLockFile;           /* File used for inter-process memory locking */
 23770         -  char shmBaseName[1];       /* Name of the memory object !!! must last !!! */
 23771         -};
 23772         -
 23773         -
 23774         -/*
 23775         -** Structure used internally by this VFS to record the state of an
 23776         -** open shared memory connection.
 23777         -**
 23778         -** The following fields are initialized when this object is created and
 23779         -** are read-only thereafter:
 23780         -**
 23781         -**    os2Shm.pShmNode
 23782         -**    os2Shm.id
 23783         -**
 23784         -** All other fields are read/write.  The os2Shm.pShmNode->mutex must be held
 23785         -** while accessing any read/write fields.
 23786         -*/
 23787         -struct os2ShmLink {
 23788         -  os2ShmNode *pShmNode;      /* The underlying os2ShmNode object */
 23789         -  os2ShmLink *pNext;         /* Next os2Shm with the same os2ShmNode */
 23790         -  u32 sharedMask;            /* Mask of shared locks held */
 23791         -  u32 exclMask;              /* Mask of exclusive locks held */
 23792         -#ifdef SQLITE_DEBUG
 23793         -  u8 id;                     /* Id of this connection with its os2ShmNode */
 23794         -#endif
 23795         -};
 23796         -
 23797         -
 23798         -/*
 23799         -** A global list of all os2ShmNode objects.
 23800         -**
 23801         -** The os2ShmMutexHeld() must be true while reading or writing this list.
 23802         -*/
 23803         -static os2ShmNode *os2ShmNodeList = NULL;
 23804         -
 23805         -/*
 23806         -** Constants used for locking
 23807         -*/
 23808         -#ifdef  SQLITE_OS2_NO_WAL_LOCK_FILE
 23809         -#define OS2_SHM_BASE   (PENDING_BYTE + 0x10000)         /* first lock byte */
 23810         -#else
 23811         -#define OS2_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
 23812         -#endif
 23813         -
 23814         -#define OS2_SHM_DMS    (OS2_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
 23815         -
 23816         -/*
 23817         -** Apply advisory locks for all n bytes beginning at ofst.
 23818         -*/
 23819         -#define _SHM_UNLCK  1   /* no lock */
 23820         -#define _SHM_RDLCK  2   /* shared lock, no wait */
 23821         -#define _SHM_WRLCK  3   /* exlusive lock, no wait */
 23822         -#define _SHM_WRLCK_WAIT 4 /* exclusive lock, wait */
 23823         -static int os2ShmSystemLock(
 23824         -  os2ShmNode *pNode,    /* Apply locks to this open shared-memory segment */
 23825         -  int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, _SHM_WRLCK or _SHM_WRLCK_WAIT */
 23826         -  int ofst,             /* Offset to first byte to be locked/unlocked */
 23827         -  int nByte             /* Number of bytes to lock or unlock */
 23828         -){
 23829         -  APIRET rc;
 23830         -  FILELOCK area;
 23831         -  ULONG mode, timeout;
 23832         -
 23833         -  /* Access to the os2ShmNode object is serialized by the caller */
 23834         -  assert( sqlite3_mutex_held(pNode->mutex) || pNode->nRef==0 );
 23835         -
 23836         -  mode = 1;     /* shared lock */
 23837         -  timeout = 0;  /* no wait */
 23838         -  area.lOffset = ofst;
 23839         -  area.lRange = nByte;
 23840         -
 23841         -  switch( lockType ) {
 23842         -    case _SHM_WRLCK_WAIT:
 23843         -      timeout = (ULONG)-1;      /* wait forever */
 23844         -    case _SHM_WRLCK:
 23845         -      mode = 0;                 /* exclusive lock */
 23846         -    case _SHM_RDLCK:
 23847         -      rc = DosSetFileLocks(pNode->hLockFile, 
 23848         -                           NULL, &area, timeout, mode);
 23849         -      break;
 23850         -    /* case _SHM_UNLCK: */
 23851         -    default:
 23852         -      rc = DosSetFileLocks(pNode->hLockFile, 
 23853         -                           &area, NULL, 0, 0);
 23854         -      break;
 23855         -  }
 23856         -                          
 23857         -  OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n", 
 23858         -           pNode->hLockFile,
 23859         -           rc==SQLITE_OK ? "ok" : "failed",
 23860         -           lockType==_SHM_UNLCK ? "Unlock" : "Lock",
 23861         -           rc));
 23862         -
 23863         -  ERR_TRACE(rc, ("os2ShmSystemLock: %d %s\n", rc, pNode->shmBaseName))
 23864         -
 23865         -  return ( rc == 0 ) ?  SQLITE_OK : SQLITE_BUSY;
 23866         -}
 23867         -
 23868         -/*
 23869         -** Find an os2ShmNode in global list or allocate a new one, if not found.
 23870         -**
 23871         -** This is not a VFS shared-memory method; it is a utility function called
 23872         -** by VFS shared-memory methods.
 23873         -*/
 23874         -static int os2OpenSharedMemory( os2File *fd, int szRegion ) {
 23875         -  os2ShmLink *pLink;
 23876         -  os2ShmNode *pNode;
 23877         -  int cbShmName, rc = SQLITE_OK;
 23878         -  char shmName[CCHMAXPATH + 30];
 23879         -#ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
 23880         -  ULONG action;
 23881         -#endif
 23882         -  
 23883         -  /* We need some additional space at the end to append the region number */
 23884         -  cbShmName = sprintf(shmName, "\\SHAREMEM\\%s", fd->zFullPathCp );
 23885         -  if( cbShmName >= CCHMAXPATH-8 )
 23886         -    return SQLITE_IOERR_SHMOPEN; 
 23887         -
 23888         -  /* Replace colon in file name to form a valid shared memory name */
 23889         -  shmName[10+1] = '!';
 23890         -
 23891         -  /* Allocate link object (we free it later in case of failure) */
 23892         -  pLink = sqlite3_malloc( sizeof(*pLink) );
 23893         -  if( !pLink )
 23894         -    return SQLITE_NOMEM;
 23895         -
 23896         -  /* Access node list */
 23897         -  os2ShmEnterMutex();
 23898         -
 23899         -  /* Find node by it's shared memory base name */
 23900         -  for( pNode = os2ShmNodeList; 
 23901         -       pNode && stricmp(shmName, pNode->shmBaseName) != 0; 
 23902         -       pNode = pNode->pNext )   ;
 23903         -
 23904         -  /* Not found: allocate a new node */
 23905         -  if( !pNode ) {
 23906         -    pNode = sqlite3_malloc( sizeof(*pNode) + cbShmName );
 23907         -    if( pNode ) {
 23908         -      memset(pNode, 0, sizeof(*pNode) );
 23909         -      pNode->szRegion = szRegion;
 23910         -      pNode->hLockFile = (HFILE)-1;      
 23911         -      strcpy(pNode->shmBaseName, shmName);
 23912         -
 23913         -#ifdef SQLITE_OS2_NO_WAL_LOCK_FILE
 23914         -      if( DosDupHandle(fd->h, &pNode->hLockFile) != 0 ) {
 23915         -#else
 23916         -      sprintf(shmName, "%s-lck", fd->zFullPathCp);
 23917         -      if( DosOpen((PSZ)shmName, &pNode->hLockFile, &action, 0, FILE_NORMAL, 
 23918         -                  OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
 23919         -                  OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE | 
 23920         -                  OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR,
 23921         -                  NULL) != 0 ) {
 23922         -#endif
 23923         -        sqlite3_free(pNode);  
 23924         -        rc = SQLITE_IOERR;
 23925         -      } else {
 23926         -        pNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
 23927         -        if( !pNode->mutex ) {
 23928         -          sqlite3_free(pNode);  
 23929         -          rc = SQLITE_NOMEM;
 23930         -        }
 23931         -      }   
 23932         -    } else {
 23933         -      rc = SQLITE_NOMEM;
 23934         -    }
 23935         -    
 23936         -    if( rc == SQLITE_OK ) {
 23937         -      pNode->pNext = os2ShmNodeList;
 23938         -      os2ShmNodeList = pNode;
 23939         -    } else {
 23940         -      pNode = NULL;
 23941         -    }
 23942         -  } else if( pNode->szRegion != szRegion ) {
 23943         -    rc = SQLITE_IOERR_SHMSIZE;
 23944         -    pNode = NULL;
 23945         -  }
 23946         -
 23947         -  if( pNode ) {
 23948         -    sqlite3_mutex_enter(pNode->mutex);
 23949         -
 23950         -    memset(pLink, 0, sizeof(*pLink));
 23951         -
 23952         -    pLink->pShmNode = pNode;
 23953         -    pLink->pNext = pNode->pFirst;
 23954         -    pNode->pFirst = pLink;
 23955         -    pNode->nRef++;
 23956         -
 23957         -    fd->pShmLink = pLink;
 23958         -
 23959         -    sqlite3_mutex_leave(pNode->mutex);
 23960         -    
 23961         -  } else {
 23962         -    /* Error occured. Free our link object. */
 23963         -    sqlite3_free(pLink);  
 23964         -  }
 23965         -
 23966         -  os2ShmLeaveMutex();
 23967         -
 23968         -  ERR_TRACE(rc, ("os2OpenSharedMemory: %d  %s\n", rc, fd->zFullPathCp))  
 23969         -  
 23970         -  return rc;
 23971         -}
 23972         -
 23973         -/*
 23974         -** Purge the os2ShmNodeList list of all entries with nRef==0.
 23975         -**
 23976         -** This is not a VFS shared-memory method; it is a utility function called
 23977         -** by VFS shared-memory methods.
 23978         -*/
 23979         -static void os2PurgeShmNodes( int deleteFlag ) {
 23980         -  os2ShmNode *pNode;
 23981         -  os2ShmNode **ppNode;
 23982         -
 23983         -  os2ShmEnterMutex();
 23984         -  
 23985         -  ppNode = &os2ShmNodeList;
 23986         -
 23987         -  while( *ppNode ) {
 23988         -    pNode = *ppNode;
 23989         -
 23990         -    if( pNode->nRef == 0 ) {
 23991         -      *ppNode = pNode->pNext;   
 23992         -     
 23993         -      if( pNode->apRegion ) {
 23994         -        /* Prevent other processes from resizing the shared memory */
 23995         -        os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
 23996         -
 23997         -        while( pNode->nRegion-- ) {
 23998         -#ifdef SQLITE_DEBUG
 23999         -          int rc = 
 24000         -#endif          
 24001         -          DosFreeMem(pNode->apRegion[pNode->nRegion]);
 24002         -
 24003         -          OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
 24004         -                  (int)GetCurrentProcessId(), pNode->nRegion,
 24005         -                  rc == 0 ? "ok" : "failed"));
 24006         -        }
 24007         -
 24008         -        /* Allow other processes to resize the shared memory */
 24009         -        os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
 24010         -
 24011         -        sqlite3_free(pNode->apRegion);
 24012         -      }  
 24013         -
 24014         -      DosClose(pNode->hLockFile);
 24015         -      
 24016         -#ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
 24017         -      if( deleteFlag ) {
 24018         -         char fileName[CCHMAXPATH];
 24019         -         /* Skip "\\SHAREMEM\\" */
 24020         -         sprintf(fileName, "%s-lck", pNode->shmBaseName + 10);
 24021         -         /* restore colon */
 24022         -         fileName[1] = ':';
 24023         -         
 24024         -         DosForceDelete(fileName); 
 24025         -      }
 24026         -#endif
 24027         -
 24028         -      sqlite3_mutex_free(pNode->mutex);
 24029         -
 24030         -      sqlite3_free(pNode);
 24031         -      
 24032         -    } else {
 24033         -      ppNode = &pNode->pNext;
 24034         -    }
 24035         -  } 
 24036         -
 24037         -  os2ShmLeaveMutex();
 24038         -}
 24039         -
 24040         -/*
 24041         -** This function is called to obtain a pointer to region iRegion of the
 24042         -** shared-memory associated with the database file id. Shared-memory regions
 24043         -** are numbered starting from zero. Each shared-memory region is szRegion
 24044         -** bytes in size.
 24045         -**
 24046         -** If an error occurs, an error code is returned and *pp is set to NULL.
 24047         -**
 24048         -** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
 24049         -** region has not been allocated (by any client, including one running in a
 24050         -** separate process), then *pp is set to NULL and SQLITE_OK returned. If
 24051         -** bExtend is non-zero and the requested shared-memory region has not yet
 24052         -** been allocated, it is allocated by this function.
 24053         -**
 24054         -** If the shared-memory region has already been allocated or is allocated by
 24055         -** this call as described above, then it is mapped into this processes
 24056         -** address space (if it is not already), *pp is set to point to the mapped
 24057         -** memory and SQLITE_OK returned.
 24058         -*/
 24059         -static int os2ShmMap(
 24060         -  sqlite3_file *id,               /* Handle open on database file */
 24061         -  int iRegion,                    /* Region to retrieve */
 24062         -  int szRegion,                   /* Size of regions */
 24063         -  int bExtend,                    /* True to extend block if necessary */
 24064         -  void volatile **pp              /* OUT: Mapped memory */
 24065         -){
 24066         -  PVOID pvTemp;
 24067         -  void **apRegion;
 24068         -  os2ShmNode *pNode;
 24069         -  int n, rc = SQLITE_OK;
 24070         -  char shmName[CCHMAXPATH];
 24071         -  os2File *pFile = (os2File*)id;
 24072         -  
 24073         -  *pp = NULL;
 24074         -
 24075         -  if( !pFile->pShmLink )
 24076         -    rc = os2OpenSharedMemory( pFile, szRegion );
 24077         -  
 24078         -  if( rc == SQLITE_OK ) {
 24079         -    pNode = pFile->pShmLink->pShmNode ;
 24080         -    
 24081         -    sqlite3_mutex_enter(pNode->mutex);
 24082         -    
 24083         -    assert( szRegion==pNode->szRegion );
 24084         -
 24085         -    /* Unmapped region ? */
 24086         -    if( iRegion >= pNode->nRegion ) {
 24087         -      /* Prevent other processes from resizing the shared memory */
 24088         -      os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
 24089         -
 24090         -      apRegion = sqlite3_realloc(
 24091         -        pNode->apRegion, (iRegion + 1) * sizeof(apRegion[0]));
 24092         -
 24093         -      if( apRegion ) {
 24094         -        pNode->apRegion = apRegion;
 24095         -
 24096         -        while( pNode->nRegion <= iRegion ) {
 24097         -          sprintf(shmName, "%s-%u", 
 24098         -                  pNode->shmBaseName, pNode->nRegion);
 24099         -
 24100         -          if( DosGetNamedSharedMem(&pvTemp, (PSZ)shmName, 
 24101         -                PAG_READ | PAG_WRITE) != NO_ERROR ) {
 24102         -            if( !bExtend )
 24103         -              break;
 24104         -
 24105         -            if( DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
 24106         -                  PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_ANY) != NO_ERROR && 
 24107         -                DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
 24108         -                  PAG_READ | PAG_WRITE | PAG_COMMIT) != NO_ERROR ) { 
 24109         -              rc = SQLITE_NOMEM;
 24110         -              break;
 24111         -            }
 24112         -          }
 24113         -
 24114         -          apRegion[pNode->nRegion++] = pvTemp;
 24115         -        }
 24116         -
 24117         -        /* zero out remaining entries */ 
 24118         -        for( n = pNode->nRegion; n <= iRegion; n++ )
 24119         -          pNode->apRegion[n] = NULL;
 24120         -
 24121         -        /* Return this region (maybe zero) */
 24122         -        *pp = pNode->apRegion[iRegion];
 24123         -      } else {
 24124         -        rc = SQLITE_NOMEM;
 24125         -      }
 24126         -
 24127         -      /* Allow other processes to resize the shared memory */
 24128         -      os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
 24129         -      
 24130         -    } else {
 24131         -      /* Region has been mapped previously */
 24132         -      *pp = pNode->apRegion[iRegion];
 24133         -    }
 24134         -
 24135         -    sqlite3_mutex_leave(pNode->mutex);
 24136         -  } 
 24137         -
 24138         -  ERR_TRACE(rc, ("os2ShmMap: %s iRgn = %d, szRgn = %d, bExt = %d : %d\n", 
 24139         -                 pFile->zFullPathCp, iRegion, szRegion, bExtend, rc))
 24140         -          
 24141         -  return rc;
 24142         -}
 24143         -
 24144         -/*
 24145         -** Close a connection to shared-memory.  Delete the underlying
 24146         -** storage if deleteFlag is true.
 24147         -**
 24148         -** If there is no shared memory associated with the connection then this
 24149         -** routine is a harmless no-op.
 24150         -*/
 24151         -static int os2ShmUnmap(
 24152         -  sqlite3_file *id,               /* The underlying database file */
 24153         -  int deleteFlag                  /* Delete shared-memory if true */
 24154         -){
 24155         -  os2File *pFile = (os2File*)id;
 24156         -  os2ShmLink *pLink = pFile->pShmLink;
 24157         -  
 24158         -  if( pLink ) {
 24159         -    int nRef = -1;
 24160         -    os2ShmLink **ppLink;
 24161         -    os2ShmNode *pNode = pLink->pShmNode;
 24162         -
 24163         -    sqlite3_mutex_enter(pNode->mutex);
 24164         -    
 24165         -    for( ppLink = &pNode->pFirst;
 24166         -         *ppLink && *ppLink != pLink;
 24167         -         ppLink = &(*ppLink)->pNext )   ;
 24168         -         
 24169         -    assert(*ppLink);
 24170         -
 24171         -    if( *ppLink ) {
 24172         -      *ppLink = pLink->pNext;
 24173         -      nRef = --pNode->nRef;
 24174         -    } else {
 24175         -      ERR_TRACE(1, ("os2ShmUnmap: link not found ! %s\n", 
 24176         -                    pNode->shmBaseName))
 24177         -    }
 24178         -    
 24179         -    pFile->pShmLink = NULL;
 24180         -    sqlite3_free(pLink);
 24181         -
 24182         -    sqlite3_mutex_leave(pNode->mutex);
 24183         -    
 24184         -    if( nRef == 0 )
 24185         -      os2PurgeShmNodes( deleteFlag );
 24186         -  }
 24187         -
 24188         -  return SQLITE_OK;
 24189         -}
 24190         -
 24191         -/*
 24192         -** Change the lock state for a shared-memory segment.
 24193         -**
 24194         -** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
 24195         -** different here than in posix.  In xShmLock(), one can go from unlocked
 24196         -** to shared and back or from unlocked to exclusive and back.  But one may
 24197         -** not go from shared to exclusive or from exclusive to shared.
 24198         -*/
 24199         -static int os2ShmLock(
 24200         -  sqlite3_file *id,          /* Database file holding the shared memory */
 24201         -  int ofst,                  /* First lock to acquire or release */
 24202         -  int n,                     /* Number of locks to acquire or release */
 24203         -  int flags                  /* What to do with the lock */
 24204         -){
 24205         -  u32 mask;                             /* Mask of locks to take or release */
 24206         -  int rc = SQLITE_OK;                   /* Result code */
 24207         -  os2File *pFile = (os2File*)id;
 24208         -  os2ShmLink *p = pFile->pShmLink;      /* The shared memory being locked */
 24209         -  os2ShmLink *pX;                       /* For looping over all siblings */
 24210         -  os2ShmNode *pShmNode = p->pShmNode;   /* Our node */
 24211         -  
 24212         -  assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
 24213         -  assert( n>=1 );
 24214         -  assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
 24215         -       || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
 24216         -       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
 24217         -       || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
 24218         -  assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
 24219         -
 24220         -  mask = (u32)((1U<<(ofst+n)) - (1U<<ofst));
 24221         -  assert( n>1 || mask==(1<<ofst) );
 24222         -
 24223         -
 24224         -  sqlite3_mutex_enter(pShmNode->mutex);
 24225         -
 24226         -  if( flags & SQLITE_SHM_UNLOCK ){
 24227         -    u32 allMask = 0; /* Mask of locks held by siblings */
 24228         -
 24229         -    /* See if any siblings hold this same lock */
 24230         -    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 24231         -      if( pX==p ) continue;
 24232         -      assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
 24233         -      allMask |= pX->sharedMask;
 24234         -    }
 24235         -
 24236         -    /* Unlock the system-level locks */
 24237         -    if( (mask & allMask)==0 ){
 24238         -      rc = os2ShmSystemLock(pShmNode, _SHM_UNLCK, ofst+OS2_SHM_BASE, n);
 24239         -    }else{
 24240         -      rc = SQLITE_OK;
 24241         -    }
 24242         -
 24243         -    /* Undo the local locks */
 24244         -    if( rc==SQLITE_OK ){
 24245         -      p->exclMask &= ~mask;
 24246         -      p->sharedMask &= ~mask;
 24247         -    } 
 24248         -  }else if( flags & SQLITE_SHM_SHARED ){
 24249         -    u32 allShared = 0;  /* Union of locks held by connections other than "p" */
 24250         -
 24251         -    /* Find out which shared locks are already held by sibling connections.
 24252         -    ** If any sibling already holds an exclusive lock, go ahead and return
 24253         -    ** SQLITE_BUSY.
 24254         -    */
 24255         -    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 24256         -      if( (pX->exclMask & mask)!=0 ){
 24257         -        rc = SQLITE_BUSY;
 24258         -        break;
 24259         -      }
 24260         -      allShared |= pX->sharedMask;
 24261         -    }
 24262         -
 24263         -    /* Get shared locks at the system level, if necessary */
 24264         -    if( rc==SQLITE_OK ){
 24265         -      if( (allShared & mask)==0 ){
 24266         -        rc = os2ShmSystemLock(pShmNode, _SHM_RDLCK, ofst+OS2_SHM_BASE, n);
 24267         -      }else{
 24268         -        rc = SQLITE_OK;
 24269         -      }
 24270         -    }
 24271         -
 24272         -    /* Get the local shared locks */
 24273         -    if( rc==SQLITE_OK ){
 24274         -      p->sharedMask |= mask;
 24275         -    }
 24276         -  }else{
 24277         -    /* Make sure no sibling connections hold locks that will block this
 24278         -    ** lock.  If any do, return SQLITE_BUSY right away.
 24279         -    */
 24280         -    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
 24281         -      if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
 24282         -        rc = SQLITE_BUSY;
 24283         -        break;
 24284         -      }
 24285         -    }
 24286         -  
 24287         -    /* Get the exclusive locks at the system level.  Then if successful
 24288         -    ** also mark the local connection as being locked.
 24289         -    */
 24290         -    if( rc==SQLITE_OK ){
 24291         -      rc = os2ShmSystemLock(pShmNode, _SHM_WRLCK, ofst+OS2_SHM_BASE, n);
 24292         -      if( rc==SQLITE_OK ){
 24293         -        assert( (p->sharedMask & mask)==0 );
 24294         -        p->exclMask |= mask;
 24295         -      }
 24296         -    }
 24297         -  }
 24298         -
 24299         -  sqlite3_mutex_leave(pShmNode->mutex);
 24300         -  
 24301         -  OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
 24302         -           p->id, (int)GetCurrentProcessId(), p->sharedMask, p->exclMask,
 24303         -           rc ? "failed" : "ok"));
 24304         -
 24305         -  ERR_TRACE(rc, ("os2ShmLock: ofst = %d, n = %d, flags = 0x%x -> %d \n", 
 24306         -                 ofst, n, flags, rc))
 24307         -                  
 24308         -  return rc; 
 24309         -}
 24310         -
 24311         -/*
 24312         -** Implement a memory barrier or memory fence on shared memory.
 24313         -**
 24314         -** All loads and stores begun before the barrier must complete before
 24315         -** any load or store begun after the barrier.
 24316         -*/
 24317         -static void os2ShmBarrier(
 24318         -  sqlite3_file *id                /* Database file holding the shared memory */
 24319         -){
 24320         -  UNUSED_PARAMETER(id);
 24321         -  os2ShmEnterMutex();
 24322         -  os2ShmLeaveMutex();
 24323         -}
 24324         -
 24325         -#else
 24326         -# define os2ShmMap     0
 24327         -# define os2ShmLock    0
 24328         -# define os2ShmBarrier 0
 24329         -# define os2ShmUnmap   0
 24330         -#endif /* #ifndef SQLITE_OMIT_WAL */
 24331         -
 24332         -
 24333         -/*
 24334         -** This vector defines all the methods that can operate on an
 24335         -** sqlite3_file for os2.
 24336         -*/
 24337         -static const sqlite3_io_methods os2IoMethod = {
 24338         -  2,                              /* iVersion */
 24339         -  os2Close,                       /* xClose */
 24340         -  os2Read,                        /* xRead */
 24341         -  os2Write,                       /* xWrite */
 24342         -  os2Truncate,                    /* xTruncate */
 24343         -  os2Sync,                        /* xSync */
 24344         -  os2FileSize,                    /* xFileSize */
 24345         -  os2Lock,                        /* xLock */
 24346         -  os2Unlock,                      /* xUnlock */
 24347         -  os2CheckReservedLock,           /* xCheckReservedLock */
 24348         -  os2FileControl,                 /* xFileControl */
 24349         -  os2SectorSize,                  /* xSectorSize */
 24350         -  os2DeviceCharacteristics,       /* xDeviceCharacteristics */
 24351         -  os2ShmMap,                      /* xShmMap */
 24352         -  os2ShmLock,                     /* xShmLock */
 24353         -  os2ShmBarrier,                  /* xShmBarrier */
 24354         -  os2ShmUnmap                     /* xShmUnmap */
 24355         -};
 24356         -
 24357         -
 24358         -/***************************************************************************
 24359         -** Here ends the I/O methods that form the sqlite3_io_methods object.
 24360         -**
 24361         -** The next block of code implements the VFS methods.
 24362         -****************************************************************************/
 24363         -
 24364         -/*
 24365         -** Create a temporary file name in zBuf.  zBuf must be big enough to
 24366         -** hold at pVfs->mxPathname characters.
 24367         -*/
 24368         -static int getTempname(int nBuf, char *zBuf ){
 24369         -  static const char zChars[] =
 24370         -    "abcdefghijklmnopqrstuvwxyz"
 24371         -    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 24372         -    "0123456789";
 24373         -  int i, j;
 24374         -  PSZ zTempPathCp;      
 24375         -  char zTempPath[CCHMAXPATH];
 24376         -  ULONG ulDriveNum, ulDriveMap;
 24377         -  
 24378         -  /* It's odd to simulate an io-error here, but really this is just
 24379         -  ** using the io-error infrastructure to test that SQLite handles this
 24380         -  ** function failing. 
 24381         -  */
 24382         -  SimulateIOError( return SQLITE_IOERR );
 24383         -
 24384         -  if( sqlite3_temp_directory ) {
 24385         -    sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", sqlite3_temp_directory);
 24386         -  } else if( DosScanEnv( (PSZ)"TEMP",   &zTempPathCp ) == NO_ERROR ||
 24387         -             DosScanEnv( (PSZ)"TMP",    &zTempPathCp ) == NO_ERROR ||
 24388         -             DosScanEnv( (PSZ)"TMPDIR", &zTempPathCp ) == NO_ERROR ) {
 24389         -    char *zTempPathUTF = convertCpPathToUtf8( (char *)zTempPathCp );
 24390         -    sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", zTempPathUTF);
 24391         -    free( zTempPathUTF );
 24392         -  } else if( DosQueryCurrentDisk( &ulDriveNum, &ulDriveMap ) == NO_ERROR ) {
 24393         -    zTempPath[0] = (char)('A' + ulDriveNum - 1);
 24394         -    zTempPath[1] = ':'; 
 24395         -    zTempPath[2] = '\0'; 
 24396         -  } else {
 24397         -    zTempPath[0] = '\0'; 
 24398         -  }
 24399         -  
 24400         -  /* Strip off a trailing slashes or backslashes, otherwise we would get *
 24401         -   * multiple (back)slashes which causes DosOpen() to fail.              *
 24402         -   * Trailing spaces are not allowed, either.                            */
 24403         -  j = sqlite3Strlen30(zTempPath);
 24404         -  while( j > 0 && ( zTempPath[j-1] == '\\' || zTempPath[j-1] == '/' || 
 24405         -                    zTempPath[j-1] == ' ' ) ){
 24406         -    j--;
 24407         -  }
 24408         -  zTempPath[j] = '\0';
 24409         -  
 24410         -  /* We use 20 bytes to randomize the name */
 24411         -  sqlite3_snprintf(nBuf-22, zBuf,
 24412         -                   "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
 24413         -  j = sqlite3Strlen30(zBuf);
 24414         -  sqlite3_randomness( 20, &zBuf[j] );
 24415         -  for( i = 0; i < 20; i++, j++ ){
 24416         -    zBuf[j] = zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
 24417         -  }
 24418         -  zBuf[j] = 0;
 24419         -
 24420         -  OSTRACE(( "TEMP FILENAME: %s\n", zBuf ));
 24421         -  return SQLITE_OK;
 24422         -}
 24423         -
 24424         -
 24425         -/*
 24426         -** Turn a relative pathname into a full pathname.  Write the full
 24427         -** pathname into zFull[].  zFull[] will be at least pVfs->mxPathname
 24428         -** bytes in size.
 24429         -*/
 24430         -static int os2FullPathname(
 24431         -  sqlite3_vfs *pVfs,          /* Pointer to vfs object */
 24432         -  const char *zRelative,      /* Possibly relative input path */
 24433         -  int nFull,                  /* Size of output buffer in bytes */
 24434         -  char *zFull                 /* Output buffer */
 24435         -){
 24436         -  char *zRelativeCp = convertUtf8PathToCp( zRelative );
 24437         -  char zFullCp[CCHMAXPATH] = "\0";
 24438         -  char *zFullUTF;
 24439         -  APIRET rc = DosQueryPathInfo( (PSZ)zRelativeCp, FIL_QUERYFULLNAME, 
 24440         -                                zFullCp, CCHMAXPATH );
 24441         -  free( zRelativeCp );
 24442         -  zFullUTF = convertCpPathToUtf8( zFullCp );
 24443         -  sqlite3_snprintf( nFull, zFull, zFullUTF );
 24444         -  free( zFullUTF );
 24445         -  return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
 24446         -}
 24447         -
 24448         -
 24449         -/*
 24450         -** Open a file.
 24451         -*/
 24452         -static int os2Open(
 24453         -  sqlite3_vfs *pVfs,            /* Not used */
 24454         -  const char *zName,            /* Name of the file (UTF-8) */
 24455         -  sqlite3_file *id,             /* Write the SQLite file handle here */
 24456         -  int flags,                    /* Open mode flags */
 24457         -  int *pOutFlags                /* Status return flags */
 24458         -){
 24459         -  HFILE h;
 24460         -  ULONG ulOpenFlags = 0;
 24461         -  ULONG ulOpenMode = 0;
 24462         -  ULONG ulAction = 0;
 24463         -  ULONG rc;
 24464         -  os2File *pFile = (os2File*)id;
 24465         -  const char *zUtf8Name = zName;
 24466         -  char *zNameCp;
 24467         -  char  zTmpname[CCHMAXPATH];
 24468         -
 24469         -  int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
 24470         -  int isCreate     = (flags & SQLITE_OPEN_CREATE);
 24471         -  int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
 24472         -#ifndef NDEBUG
 24473         -  int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
 24474         -  int isReadonly   = (flags & SQLITE_OPEN_READONLY);
 24475         -  int eType        = (flags & 0xFFFFFF00);
 24476         -  int isOpenJournal = (isCreate && (
 24477         -        eType==SQLITE_OPEN_MASTER_JOURNAL 
 24478         -     || eType==SQLITE_OPEN_MAIN_JOURNAL 
 24479         -     || eType==SQLITE_OPEN_WAL
 24480         -  ));
 24481         -#endif
 24482         -
 24483         -  UNUSED_PARAMETER(pVfs);
 24484         -  assert( id!=0 );
 24485         -
 24486         -  /* Check the following statements are true: 
 24487         -  **
 24488         -  **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
 24489         -  **   (b) if CREATE is set, then READWRITE must also be set, and
 24490         -  **   (c) if EXCLUSIVE is set, then CREATE must also be set.
 24491         -  **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
 24492         -  */
 24493         -  assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
 24494         -  assert(isCreate==0 || isReadWrite);
 24495         -  assert(isExclusive==0 || isCreate);
 24496         -  assert(isDelete==0 || isCreate);
 24497         -
 24498         -  /* The main DB, main journal, WAL file and master journal are never 
 24499         -  ** automatically deleted. Nor are they ever temporary files.  */
 24500         -  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
 24501         -  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
 24502         -  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
 24503         -  assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
 24504         -
 24505         -  /* Assert that the upper layer has set one of the "file-type" flags. */
 24506         -  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
 24507         -       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
 24508         -       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
 24509         -       || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
 24510         -  );
 24511         -
 24512         -  memset( pFile, 0, sizeof(*pFile) );
 24513         -  pFile->h = (HFILE)-1;
 24514         -
 24515         -  /* If the second argument to this function is NULL, generate a 
 24516         -  ** temporary file name to use 
 24517         -  */
 24518         -  if( !zUtf8Name ){
 24519         -    assert(isDelete && !isOpenJournal);
 24520         -    rc = getTempname(CCHMAXPATH, zTmpname);
 24521         -    if( rc!=SQLITE_OK ){
 24522         -      return rc;
 24523         -    }
 24524         -    zUtf8Name = zTmpname;
 24525         -  }
 24526         -
 24527         -  if( isReadWrite ){
 24528         -    ulOpenMode |= OPEN_ACCESS_READWRITE;
 24529         -  }else{
 24530         -    ulOpenMode |= OPEN_ACCESS_READONLY;
 24531         -  }
 24532         -
 24533         -  /* Open in random access mode for possibly better speed.  Allow full
 24534         -  ** sharing because file locks will provide exclusive access when needed.
 24535         -  ** The handle should not be inherited by child processes and we don't 
 24536         -  ** want popups from the critical error handler.
 24537         -  */
 24538         -  ulOpenMode |= OPEN_FLAGS_RANDOM | OPEN_SHARE_DENYNONE | 
 24539         -                OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR;
 24540         -
 24541         -  /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is 
 24542         -  ** created. SQLite doesn't use it to indicate "exclusive access" 
 24543         -  ** as it is usually understood.
 24544         -  */
 24545         -  if( isExclusive ){
 24546         -    /* Creates a new file, only if it does not already exist. */
 24547         -    /* If the file exists, it fails. */
 24548         -    ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_FAIL_IF_EXISTS;
 24549         -  }else if( isCreate ){
 24550         -    /* Open existing file, or create if it doesn't exist */
 24551         -    ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
 24552         -  }else{
 24553         -    /* Opens a file, only if it exists. */
 24554         -    ulOpenFlags |= OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
 24555         -  }
 24556         -
 24557         -  zNameCp = convertUtf8PathToCp( zUtf8Name );
 24558         -  rc = DosOpen( (PSZ)zNameCp,
 24559         -                &h,
 24560         -                &ulAction,
 24561         -                0L,
 24562         -                FILE_NORMAL,
 24563         -                ulOpenFlags,
 24564         -                ulOpenMode,
 24565         -                (PEAOP2)NULL );
 24566         -  free( zNameCp );
 24567         -
 24568         -  if( rc != NO_ERROR ){
 24569         -    OSTRACE(( "OPEN Invalid handle rc=%d: zName=%s, ulAction=%#lx, ulFlags=%#lx, ulMode=%#lx\n",
 24570         -              rc, zUtf8Name, ulAction, ulOpenFlags, ulOpenMode ));
 24571         -
 24572         -    if( isReadWrite ){
 24573         -      return os2Open( pVfs, zName, id,
 24574         -                      ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
 24575         -                      pOutFlags );
 24576         -    }else{
 24577         -      return SQLITE_CANTOPEN;
 24578         -    }
 24579         -  }
 24580         -
 24581         -  if( pOutFlags ){
 24582         -    *pOutFlags = isReadWrite ? SQLITE_OPEN_READWRITE : SQLITE_OPEN_READONLY;
 24583         -  }
 24584         -
 24585         -  os2FullPathname( pVfs, zUtf8Name, sizeof( zTmpname ), zTmpname );
 24586         -  pFile->zFullPathCp = convertUtf8PathToCp( zTmpname );
 24587         -  pFile->pMethod = &os2IoMethod;
 24588         -  pFile->flags = flags;
 24589         -  pFile->h = h;
 24590         -
 24591         -  OpenCounter(+1);
 24592         -  OSTRACE(( "OPEN %d pOutFlags=%d\n", pFile->h, pOutFlags ));
 24593         -  return SQLITE_OK;
 24594         -}
 24595         -
 24596         -/*
 24597         -** Delete the named file.
 24598         -*/
 24599         -static int os2Delete(
 24600         -  sqlite3_vfs *pVfs,                     /* Not used on os2 */
 24601         -  const char *zFilename,                 /* Name of file to delete */
 24602         -  int syncDir                            /* Not used on os2 */
 24603         -){
 24604         -  APIRET rc;
 24605         -  char *zFilenameCp;
 24606         -  SimulateIOError( return SQLITE_IOERR_DELETE );
 24607         -  zFilenameCp = convertUtf8PathToCp( zFilename );
 24608         -  rc = DosDelete( (PSZ)zFilenameCp );
 24609         -  free( zFilenameCp );
 24610         -  OSTRACE(( "DELETE \"%s\"\n", zFilename ));
 24611         -  return (rc == NO_ERROR ||
 24612         -          rc == ERROR_FILE_NOT_FOUND ||
 24613         -          rc == ERROR_PATH_NOT_FOUND ) ? SQLITE_OK : SQLITE_IOERR_DELETE;
 24614         -}
 24615         -
 24616         -/*
 24617         -** Check the existance and status of a file.
 24618         -*/
 24619         -static int os2Access(
 24620         -  sqlite3_vfs *pVfs,        /* Not used on os2 */
 24621         -  const char *zFilename,    /* Name of file to check */
 24622         -  int flags,                /* Type of test to make on this file */
 24623         -  int *pOut                 /* Write results here */
 24624         -){
 24625         -  APIRET rc;
 24626         -  FILESTATUS3 fsts3ConfigInfo;
 24627         -  char *zFilenameCp;
 24628         -
 24629         -  UNUSED_PARAMETER(pVfs);
 24630         -  SimulateIOError( return SQLITE_IOERR_ACCESS; );
 24631         -  
 24632         -  zFilenameCp = convertUtf8PathToCp( zFilename );
 24633         -  rc = DosQueryPathInfo( (PSZ)zFilenameCp, FIL_STANDARD,
 24634         -                         &fsts3ConfigInfo, sizeof(FILESTATUS3) );
 24635         -  free( zFilenameCp );
 24636         -  OSTRACE(( "ACCESS fsts3ConfigInfo.attrFile=%d flags=%d rc=%d\n",
 24637         -            fsts3ConfigInfo.attrFile, flags, rc ));
 24638         -
 24639         -  switch( flags ){
 24640         -    case SQLITE_ACCESS_EXISTS:
 24641         -      /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
 24642         -      ** as if it does not exist.
 24643         -      */
 24644         -      if( fsts3ConfigInfo.cbFile == 0 ) 
 24645         -        rc = ERROR_FILE_NOT_FOUND;
 24646         -      break;
 24647         -    case SQLITE_ACCESS_READ:
 24648         -      break;
 24649         -    case SQLITE_ACCESS_READWRITE:
 24650         -      if( fsts3ConfigInfo.attrFile & FILE_READONLY )
 24651         -        rc = ERROR_ACCESS_DENIED;
 24652         -      break;
 24653         -    default:
 24654         -      rc = ERROR_FILE_NOT_FOUND;
 24655         -      assert( !"Invalid flags argument" );
 24656         -  }
 24657         -
 24658         -  *pOut = (rc == NO_ERROR);
 24659         -  OSTRACE(( "ACCESS %s flags %d: rc=%d\n", zFilename, flags, *pOut ));
 24660         -
 24661         -  return SQLITE_OK;
 24662         -}
 24663         -
 24664         -
 24665         -#ifndef SQLITE_OMIT_LOAD_EXTENSION
 24666         -/*
 24667         -** Interfaces for opening a shared library, finding entry points
 24668         -** within the shared library, and closing the shared library.
 24669         -*/
 24670         -/*
 24671         -** Interfaces for opening a shared library, finding entry points
 24672         -** within the shared library, and closing the shared library.
 24673         -*/
 24674         -static void *os2DlOpen(sqlite3_vfs *pVfs, const char *zFilename){
 24675         -  HMODULE hmod;
 24676         -  APIRET rc;
 24677         -  char *zFilenameCp = convertUtf8PathToCp(zFilename);
 24678         -  rc = DosLoadModule(NULL, 0, (PSZ)zFilenameCp, &hmod);
 24679         -  free(zFilenameCp);
 24680         -  return rc != NO_ERROR ? 0 : (void*)hmod;
 24681         -}
 24682         -/*
 24683         -** A no-op since the error code is returned on the DosLoadModule call.
 24684         -** os2Dlopen returns zero if DosLoadModule is not successful.
 24685         -*/
 24686         -static void os2DlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
 24687         -/* no-op */
 24688         -}
 24689         -static void (*os2DlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
 24690         -  PFN pfn;
 24691         -  APIRET rc;
 24692         -  rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)zSymbol, &pfn);
 24693         -  if( rc != NO_ERROR ){
 24694         -    /* if the symbol itself was not found, search again for the same
 24695         -     * symbol with an extra underscore, that might be needed depending
 24696         -     * on the calling convention */
 24697         -    char _zSymbol[256] = "_";
 24698         -    strncat(_zSymbol, zSymbol, 254);
 24699         -    rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)_zSymbol, &pfn);
 24700         -  }
 24701         -  return rc != NO_ERROR ? 0 : (void(*)(void))pfn;
 24702         -}
 24703         -static void os2DlClose(sqlite3_vfs *pVfs, void *pHandle){
 24704         -  DosFreeModule((HMODULE)pHandle);
 24705         -}
 24706         -#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
 24707         -  #define os2DlOpen 0
 24708         -  #define os2DlError 0
 24709         -  #define os2DlSym 0
 24710         -  #define os2DlClose 0
 24711         -#endif
 24712         -
 24713         -
 24714         -/*
 24715         -** Write up to nBuf bytes of randomness into zBuf.
 24716         -*/
 24717         -static int os2Randomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf ){
 24718         -  int n = 0;
 24719         -#if defined(SQLITE_TEST)
 24720         -  n = nBuf;
 24721         -  memset(zBuf, 0, nBuf);
 24722         -#else
 24723         -  int i;                           
 24724         -  PPIB ppib;
 24725         -  PTIB ptib;
 24726         -  DATETIME dt; 
 24727         -  static unsigned c = 0;
 24728         -  /* Ordered by variation probability */
 24729         -  static ULONG svIdx[6] = { QSV_MS_COUNT, QSV_TIME_LOW,
 24730         -                            QSV_MAXPRMEM, QSV_MAXSHMEM,
 24731         -                            QSV_TOTAVAILMEM, QSV_TOTRESMEM };
 24732         -
 24733         -  /* 8 bytes; timezone and weekday don't increase the randomness much */
 24734         -  if( (int)sizeof(dt)-3 <= nBuf - n ){
 24735         -    c += 0x0100;
 24736         -    DosGetDateTime(&dt);
 24737         -    dt.year = (USHORT)((dt.year - 1900) | c);
 24738         -    memcpy(&zBuf[n], &dt, sizeof(dt)-3);
 24739         -    n += sizeof(dt)-3;
 24740         -  }
 24741         -
 24742         -  /* 4 bytes; PIDs and TIDs are 16 bit internally, so combine them */
 24743         -  if( (int)sizeof(ULONG) <= nBuf - n ){
 24744         -    DosGetInfoBlocks(&ptib, &ppib);
 24745         -    *(PULONG)&zBuf[n] = MAKELONG(ppib->pib_ulpid,
 24746         -                                 ptib->tib_ptib2->tib2_ultid);
 24747         -    n += sizeof(ULONG);
 24748         -  }
 24749         -
 24750         -  /* Up to 6 * 4 bytes; variables depend on the system state */
 24751         -  for( i = 0; i < 6 && (int)sizeof(ULONG) <= nBuf - n; i++ ){
 24752         -    DosQuerySysInfo(svIdx[i], svIdx[i], 
 24753         -                    (PULONG)&zBuf[n], sizeof(ULONG));
 24754         -    n += sizeof(ULONG);
 24755         -  } 
 24756         -#endif
 24757         -
 24758         -  return n;
 24759         -}
 24760         -
 24761         -/*
 24762         -** Sleep for a little while.  Return the amount of time slept.
 24763         -** The argument is the number of microseconds we want to sleep.
 24764         -** The return value is the number of microseconds of sleep actually
 24765         -** requested from the underlying operating system, a number which
 24766         -** might be greater than or equal to the argument, but not less
 24767         -** than the argument.
 24768         -*/
 24769         -static int os2Sleep( sqlite3_vfs *pVfs, int microsec ){
 24770         -  DosSleep( (microsec/1000) );
 24771         -  return microsec;
 24772         -}
 24773         -
 24774         -/*
 24775         -** The following variable, if set to a non-zero value, becomes the result
 24776         -** returned from sqlite3OsCurrentTime().  This is used for testing.
 24777         -*/
 24778         -#ifdef SQLITE_TEST
 24779         -SQLITE_API int sqlite3_current_time = 0;
 24780         -#endif
 24781         -
 24782         -/*
 24783         -** Find the current time (in Universal Coordinated Time).  Write into *piNow
 24784         -** the current time and date as a Julian Day number times 86_400_000.  In
 24785         -** other words, write into *piNow the number of milliseconds since the Julian
 24786         -** epoch of noon in Greenwich on November 24, 4714 B.C according to the
 24787         -** proleptic Gregorian calendar.
 24788         -**
 24789         -** On success, return 0.  Return 1 if the time and date cannot be found.
 24790         -*/
 24791         -static int os2CurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
 24792         -#ifdef SQLITE_TEST
 24793         -  static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
 24794         -#endif
 24795         -  int year, month, datepart, timepart;
 24796         - 
 24797         -  DATETIME dt;
 24798         -  DosGetDateTime( &dt );
 24799         -
 24800         -  year = dt.year;
 24801         -  month = dt.month;
 24802         -
 24803         -  /* Calculations from http://www.astro.keele.ac.uk/~rno/Astronomy/hjd.html
 24804         -  ** http://www.astro.keele.ac.uk/~rno/Astronomy/hjd-0.1.c
 24805         -  ** Calculate the Julian days
 24806         -  */
 24807         -  datepart = (int)dt.day - 32076 +
 24808         -    1461*(year + 4800 + (month - 14)/12)/4 +
 24809         -    367*(month - 2 - (month - 14)/12*12)/12 -
 24810         -    3*((year + 4900 + (month - 14)/12)/100)/4;
 24811         -
 24812         -  /* Time in milliseconds, hours to noon added */
 24813         -  timepart = 12*3600*1000 + dt.hundredths*10 + dt.seconds*1000 +
 24814         -    ((int)dt.minutes + dt.timezone)*60*1000 + dt.hours*3600*1000;
 24815         -
 24816         -  *piNow = (sqlite3_int64)datepart*86400*1000 + timepart;
 24817         -   
 24818         -#ifdef SQLITE_TEST
 24819         -  if( sqlite3_current_time ){
 24820         -    *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
 24821         -  }
 24822         -#endif
 24823         -
 24824         -  UNUSED_PARAMETER(pVfs);
 24825         -  return 0;
 24826         -}
 24827         -
 24828         -/*
 24829         -** Find the current time (in Universal Coordinated Time).  Write the
 24830         -** current time and date as a Julian Day number into *prNow and
 24831         -** return 0.  Return 1 if the time and date cannot be found.
 24832         -*/
 24833         -static int os2CurrentTime( sqlite3_vfs *pVfs, double *prNow ){
 24834         -  int rc;
 24835         -  sqlite3_int64 i;
 24836         -  rc = os2CurrentTimeInt64(pVfs, &i);
 24837         -  if( !rc ){
 24838         -    *prNow = i/86400000.0;
 24839         -  }
 24840         -  return rc;
 24841         -}
 24842         -
 24843         -/*
 24844         -** The idea is that this function works like a combination of
 24845         -** GetLastError() and FormatMessage() on windows (or errno and
 24846         -** strerror_r() on unix). After an error is returned by an OS
 24847         -** function, SQLite calls this function with zBuf pointing to
 24848         -** a buffer of nBuf bytes. The OS layer should populate the
 24849         -** buffer with a nul-terminated UTF-8 encoded error message
 24850         -** describing the last IO error to have occurred within the calling
 24851         -** thread.
 24852         -**
 24853         -** If the error message is too large for the supplied buffer,
 24854         -** it should be truncated. The return value of xGetLastError
 24855         -** is zero if the error message fits in the buffer, or non-zero
 24856         -** otherwise (if the message was truncated). If non-zero is returned,
 24857         -** then it is not necessary to include the nul-terminator character
 24858         -** in the output buffer.
 24859         -**
 24860         -** Not supplying an error message will have no adverse effect
 24861         -** on SQLite. It is fine to have an implementation that never
 24862         -** returns an error message:
 24863         -**
 24864         -**   int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
 24865         -**     assert(zBuf[0]=='\0');
 24866         -**     return 0;
 24867         -**   }
 24868         -**
 24869         -** However if an error message is supplied, it will be incorporated
 24870         -** by sqlite into the error message available to the user using
 24871         -** sqlite3_errmsg(), possibly making IO errors easier to debug.
 24872         -*/
 24873         -static int os2GetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
 24874         -  assert(zBuf[0]=='\0');
 24875         -  return 0;
 24876         -}
 24877         -
 24878         -/*
 24879         -** Initialize and deinitialize the operating system interface.
 24880         -*/
 24881         -SQLITE_API int sqlite3_os_init(void){
 24882         -  static sqlite3_vfs os2Vfs = {
 24883         -    3,                 /* iVersion */
 24884         -    sizeof(os2File),   /* szOsFile */
 24885         -    CCHMAXPATH,        /* mxPathname */
 24886         -    0,                 /* pNext */
 24887         -    "os2",             /* zName */
 24888         -    0,                 /* pAppData */
 24889         -
 24890         -    os2Open,           /* xOpen */
 24891         -    os2Delete,         /* xDelete */
 24892         -    os2Access,         /* xAccess */
 24893         -    os2FullPathname,   /* xFullPathname */
 24894         -    os2DlOpen,         /* xDlOpen */
 24895         -    os2DlError,        /* xDlError */
 24896         -    os2DlSym,          /* xDlSym */
 24897         -    os2DlClose,        /* xDlClose */
 24898         -    os2Randomness,     /* xRandomness */
 24899         -    os2Sleep,          /* xSleep */
 24900         -    os2CurrentTime,    /* xCurrentTime */
 24901         -    os2GetLastError,   /* xGetLastError */
 24902         -    os2CurrentTimeInt64, /* xCurrentTimeInt64 */
 24903         -    0,                 /* xSetSystemCall */
 24904         -    0,                 /* xGetSystemCall */
 24905         -    0                  /* xNextSystemCall */
 24906         -  };
 24907         -  sqlite3_vfs_register(&os2Vfs, 1);
 24908         -  initUconvObjects();
 24909         -/*  sqlite3OSTrace = 1; */
 24910         -  return SQLITE_OK;
 24911         -}
 24912         -SQLITE_API int sqlite3_os_end(void){
 24913         -  freeUconvObjects();
 24914         -  return SQLITE_OK;
 24915         -}
 24916         -
 24917         -#endif /* SQLITE_OS_OS2 */
 24918         -
 24919         -/************** End of os_os2.c **********************************************/
 24920  22516   /************** Begin file os_unix.c *****************************************/
 24921  22517   /*
 24922  22518   ** 2004 May 22
 24923  22519   **
 24924  22520   ** The author disclaims copyright to this source code.  In place of
 24925  22521   ** a legal notice, here is a blessing:
 24926  22522   **
................................................................................
 59052  56648   }
 59053  56649   
 59054  56650   /*
 59055  56651   ** Release all resources associated with an sqlite3_backup* handle.
 59056  56652   */
 59057  56653   SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
 59058  56654     sqlite3_backup **pp;                 /* Ptr to head of pagers backup list */
 59059         -  MUTEX_LOGIC( sqlite3_mutex *mutex; ) /* Mutex to protect source database */
        56655  +  sqlite3 *pSrcDb;                     /* Source database connection */
 59060  56656     int rc;                              /* Value to return */
 59061  56657   
 59062  56658     /* Enter the mutexes */
 59063  56659     if( p==0 ) return SQLITE_OK;
 59064         -  sqlite3_mutex_enter(p->pSrcDb->mutex);
        56660  +  pSrcDb = p->pSrcDb;
        56661  +  sqlite3_mutex_enter(pSrcDb->mutex);
 59065  56662     sqlite3BtreeEnter(p->pSrc);
 59066         -  MUTEX_LOGIC( mutex = p->pSrcDb->mutex; )
 59067  56663     if( p->pDestDb ){
 59068  56664       sqlite3_mutex_enter(p->pDestDb->mutex);
 59069  56665     }
 59070  56666   
 59071  56667     /* Detach this backup from the source pager. */
 59072  56668     if( p->pDestDb ){
 59073  56669       p->pSrc->nBackup--;
................................................................................
 59085  56681   
 59086  56682     /* Set the error code of the destination database handle. */
 59087  56683     rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
 59088  56684     sqlite3Error(p->pDestDb, rc, 0);
 59089  56685   
 59090  56686     /* Exit the mutexes and free the backup context structure. */
 59091  56687     if( p->pDestDb ){
 59092         -    sqlite3_mutex_leave(p->pDestDb->mutex);
        56688  +    sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
 59093  56689     }
 59094  56690     sqlite3BtreeLeave(p->pSrc);
 59095  56691     if( p->pDestDb ){
 59096  56692       /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
 59097  56693       ** call to sqlite3_backup_init() and is destroyed by a call to
 59098  56694       ** sqlite3_backup_finish(). */
 59099  56695       sqlite3_free(p);
 59100  56696     }
 59101         -  sqlite3_mutex_leave(mutex);
        56697  +  sqlite3LeaveMutexAndCloseZombie(pSrcDb);
 59102  56698     return rc;
 59103  56699   }
 59104  56700   
 59105  56701   /*
 59106  56702   ** Return the number of pages still to be backed up as of the most recent
 59107  56703   ** call to sqlite3_backup_step().
 59108  56704   */
................................................................................
 62858  60454   ** Delete an entire VDBE.
 62859  60455   */
 62860  60456   SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
 62861  60457     sqlite3 *db;
 62862  60458   
 62863  60459     if( NEVER(p==0) ) return;
 62864  60460     db = p->db;
        60461  +  assert( sqlite3_mutex_held(db->mutex) );
 62865  60462     if( p->pPrev ){
 62866  60463       p->pPrev->pNext = p->pNext;
 62867  60464     }else{
 62868  60465       assert( db->pVdbe==p );
 62869  60466       db->pVdbe = p->pNext;
 62870  60467     }
 62871  60468     if( p->pNext ){
................................................................................
 63697  61294     if( pStmt==0 ){
 63698  61295       /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
 63699  61296       ** pointer is a harmless no-op. */
 63700  61297       rc = SQLITE_OK;
 63701  61298     }else{
 63702  61299       Vdbe *v = (Vdbe*)pStmt;
 63703  61300       sqlite3 *db = v->db;
 63704         -#if SQLITE_THREADSAFE
 63705         -    sqlite3_mutex *mutex;
 63706         -#endif
 63707  61301       if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
 63708         -#if SQLITE_THREADSAFE
 63709         -    mutex = v->db->mutex;
 63710         -#endif
 63711         -    sqlite3_mutex_enter(mutex);
        61302  +    sqlite3_mutex_enter(db->mutex);
 63712  61303       rc = sqlite3VdbeFinalize(v);
 63713  61304       rc = sqlite3ApiExit(db, rc);
 63714         -    sqlite3_mutex_leave(mutex);
        61305  +    sqlite3LeaveMutexAndCloseZombie(db);
 63715  61306     }
 63716  61307     return rc;
 63717  61308   }
 63718  61309   
 63719  61310   /*
 63720  61311   ** Terminate the current execution of an SQL statement and reset it
 63721  61312   ** back to its starting state so that it can be reused. A success code from
................................................................................
 70015  67606     u.bl.pC = p->apCsr[pOp->p1];
 70016  67607     assert( u.bl.pC->isSorter==0 );
 70017  67608     assert( u.bl.pC->isTable || pOp->opcode!=OP_RowData );
 70018  67609     assert( u.bl.pC->isIndex || pOp->opcode==OP_RowData );
 70019  67610     assert( u.bl.pC!=0 );
 70020  67611     assert( u.bl.pC->nullRow==0 );
 70021  67612     assert( u.bl.pC->pseudoTableReg==0 );
 70022         -  assert( !u.bl.pC->isSorter );
 70023  67613     assert( u.bl.pC->pCursor!=0 );
 70024  67614     u.bl.pCrsr = u.bl.pC->pCursor;
 70025  67615     assert( sqlite3BtreeCursorIsValid(u.bl.pCrsr) );
 70026  67616   
 70027  67617     /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
 70028  67618     ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
 70029  67619     ** the cursor.  Hence the following sqlite3VdbeCursorMoveto() call is always
................................................................................
 88017  85607   ** "NULL".  Otherwise, the argument is enclosed in single quotes with
 88018  85608   ** single-quote escapes.
 88019  85609   */
 88020  85610   static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
 88021  85611     assert( argc==1 );
 88022  85612     UNUSED_PARAMETER(argc);
 88023  85613     switch( sqlite3_value_type(argv[0]) ){
 88024         -    case SQLITE_INTEGER:
 88025  85614       case SQLITE_FLOAT: {
        85615  +      double r1, r2;
        85616  +      char zBuf[50];
        85617  +      r1 = sqlite3_value_double(argv[0]);
        85618  +      sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
        85619  +      sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8);
        85620  +      if( r1!=r2 ){
        85621  +        sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1);
        85622  +      }
        85623  +      sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
        85624  +      break;
        85625  +    }
        85626  +    case SQLITE_INTEGER: {
 88026  85627         sqlite3_result_value(context, argv[0]);
 88027  85628         break;
 88028  85629       }
 88029  85630       case SQLITE_BLOB: {
 88030  85631         char *zText = 0;
 88031  85632         char const *zBlob = sqlite3_value_blob(argv[0]);
 88032  85633         int nBlob = sqlite3_value_bytes(argv[0]);
................................................................................
114345 111946       }
114346 111947     }
114347 111948     sqlite3BtreeLeaveAll(db);
114348 111949   #else
114349 111950     UNUSED_PARAMETER(db);
114350 111951   #endif
114351 111952   }
       111953  +
       111954  +/*
       111955  +** Return TRUE if database connection db has unfinalized prepared
       111956  +** statements or unfinished sqlite3_backup objects.  
       111957  +*/
       111958  +static int connectionIsBusy(sqlite3 *db){
       111959  +  int j;
       111960  +  assert( sqlite3_mutex_held(db->mutex) );
       111961  +  if( db->pVdbe ) return 1;
       111962  +  for(j=0; j<db->nDb; j++){
       111963  +    Btree *pBt = db->aDb[j].pBt;
       111964  +    if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1;
       111965  +  }
       111966  +  return 0;
       111967  +}
114352 111968   
114353 111969   /*
114354 111970   ** Close an existing SQLite database
114355 111971   */
114356         -SQLITE_API int sqlite3_close(sqlite3 *db){
114357         -  HashElem *i;                    /* Hash table iterator */
114358         -  int j;
114359         -
       111972  +static int sqlite3Close(sqlite3 *db, int forceZombie){
114360 111973     if( !db ){
114361 111974       return SQLITE_OK;
114362 111975     }
114363 111976     if( !sqlite3SafetyCheckSickOrOk(db) ){
114364 111977       return SQLITE_MISUSE_BKPT;
114365 111978     }
114366 111979     sqlite3_mutex_enter(db->mutex);
................................................................................
114373 111986     ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
114374 111987     ** call will do so. We need to do this before the check for active
114375 111988     ** SQL statements below, as the v-table implementation may be storing
114376 111989     ** some prepared statements internally.
114377 111990     */
114378 111991     sqlite3VtabRollback(db);
114379 111992   
114380         -  /* If there are any outstanding VMs, return SQLITE_BUSY. */
114381         -  if( db->pVdbe ){
114382         -    sqlite3Error(db, SQLITE_BUSY, 
114383         -        "unable to close due to unfinalised statements");
       111993  +  /* Legacy behavior (sqlite3_close() behavior) is to return
       111994  +  ** SQLITE_BUSY if the connection can not be closed immediately.
       111995  +  */
       111996  +  if( !forceZombie && connectionIsBusy(db) ){
       111997  +    sqlite3Error(db, SQLITE_BUSY, "unable to close due to unfinalized "
       111998  +       "statements or unfinished backups");
114384 111999       sqlite3_mutex_leave(db->mutex);
114385 112000       return SQLITE_BUSY;
114386 112001     }
114387         -  assert( sqlite3SafetyCheckSickOrOk(db) );
114388         -
114389         -  for(j=0; j<db->nDb; j++){
114390         -    Btree *pBt = db->aDb[j].pBt;
114391         -    if( pBt && sqlite3BtreeIsInBackup(pBt) ){
114392         -      sqlite3Error(db, SQLITE_BUSY, 
114393         -          "unable to close due to unfinished backup operation");
114394         -      sqlite3_mutex_leave(db->mutex);
114395         -      return SQLITE_BUSY;
114396         -    }
114397         -  }
       112002  +
       112003  +  /* Convert the connection into a zombie and then close it.
       112004  +  */
       112005  +  db->magic = SQLITE_MAGIC_ZOMBIE;
       112006  +  sqlite3LeaveMutexAndCloseZombie(db);
       112007  +  return SQLITE_OK;
       112008  +}
       112009  +
       112010  +/*
       112011  +** Two variations on the public interface for closing a database
       112012  +** connection. The sqlite3_close() version returns SQLITE_BUSY and
       112013  +** leaves the connection option if there are unfinalized prepared
       112014  +** statements or unfinished sqlite3_backups.  The sqlite3_close_v2()
       112015  +** version forces the connection to become a zombie if there are
       112016  +** unclosed resources, and arranges for deallocation when the last
       112017  +** prepare statement or sqlite3_backup closes.
       112018  +*/
       112019  +SQLITE_API int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
       112020  +SQLITE_API int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
       112021  +
       112022  +
       112023  +/*
       112024  +** Close the mutex on database connection db.
       112025  +**
       112026  +** Furthermore, if database connection db is a zombie (meaning that there
       112027  +** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
       112028  +** every sqlite3_stmt has now been finalized and every sqlite3_backup has
       112029  +** finished, then free all resources.
       112030  +*/
       112031  +SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
       112032  +  HashElem *i;                    /* Hash table iterator */
       112033  +  int j;
       112034  +
       112035  +  /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
       112036  +  ** or if the connection has not yet been closed by sqlite3_close_v2(),
       112037  +  ** then just leave the mutex and return.
       112038  +  */
       112039  +  if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
       112040  +    sqlite3_mutex_leave(db->mutex);
       112041  +    return;
       112042  +  }
       112043  +
       112044  +  /* If we reach this point, it means that the database connection has
       112045  +  ** closed all sqlite3_stmt and sqlite3_backup objects and has been
       112046  +  ** pased to sqlite3_close (meaning that it is a zombie).  Therefore,
       112047  +  ** go ahead and free all resources.
       112048  +  */
114398 112049   
114399 112050     /* Free any outstanding Savepoint structures. */
114400 112051     sqlite3CloseSavepoints(db);
114401 112052   
114402 112053     /* Close all database connections */
114403 112054     for(j=0; j<db->nDb; j++){
114404 112055       struct Db *pDb = &db->aDb[j];
................................................................................
114479 112130     db->magic = SQLITE_MAGIC_CLOSED;
114480 112131     sqlite3_mutex_free(db->mutex);
114481 112132     assert( db->lookaside.nOut==0 );  /* Fails on a lookaside memory leak */
114482 112133     if( db->lookaside.bMalloced ){
114483 112134       sqlite3_free(db->lookaside.pStart);
114484 112135     }
114485 112136     sqlite3_free(db);
114486         -  return SQLITE_OK;
114487 112137   }
114488 112138   
114489 112139   /*
114490 112140   ** Rollback all database files.  If tripCode is not SQLITE_OK, then
114491 112141   ** any open cursors are invalidated ("tripped" - as in "tripping a circuit
114492 112142   ** breaker") and made to return tripCode if there are any further
114493 112143   ** attempts to use that cursor.
................................................................................
115646 113296             zModeType = "cache";
115647 113297           }
115648 113298           if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
115649 113299             static struct OpenMode aOpenMode[] = {
115650 113300               { "ro",  SQLITE_OPEN_READONLY },
115651 113301               { "rw",  SQLITE_OPEN_READWRITE }, 
115652 113302               { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
115653         -            { "memory",
115654         -                    SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE 
115655         -                       | SQLITE_OPEN_MEMORY },
       113303  +            { "memory", SQLITE_OPEN_MEMORY },
115656 113304               { 0, 0 }
115657 113305             };
115658 113306   
115659 113307             mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE
115660 113308                      | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY;
115661 113309             aMode = aOpenMode;
115662 113310             limit = mask & flags;
................................................................................
125934 123582   
125935 123583   /* #include <tcl.h> */
125936 123584   /* #include <string.h> */
125937 123585   
125938 123586   /*
125939 123587   ** Implementation of a special SQL scalar function for testing tokenizers 
125940 123588   ** designed to be used in concert with the Tcl testing framework. This
125941         -** function must be called with two arguments:
       123589  +** function must be called with two or more arguments:
125942 123590   **
125943         -**   SELECT <function-name>(<key-name>, <input-string>);
125944         -**   SELECT <function-name>(<key-name>, <pointer>);
       123591  +**   SELECT <function-name>(<key-name>, ..., <input-string>);
125945 123592   **
125946 123593   ** where <function-name> is the name passed as the second argument
125947 123594   ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
125948 123595   ** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
125949 123596   **
125950 123597   ** The return value is a string that may be interpreted as a Tcl
125951 123598   ** list. For each token in the <input-string>, three elements are
................................................................................
125974 123621     const char *zErr = 0;
125975 123622   
125976 123623     const char *zName;
125977 123624     int nName;
125978 123625     const char *zInput;
125979 123626     int nInput;
125980 123627   
125981         -  const char *zArg = 0;
       123628  +  const char *azArg[64];
125982 123629   
125983 123630     const char *zToken;
125984 123631     int nToken;
125985 123632     int iStart;
125986 123633     int iEnd;
125987 123634     int iPos;
       123635  +  int i;
125988 123636   
125989 123637     Tcl_Obj *pRet;
125990 123638   
125991         -  assert( argc==2 || argc==3 );
       123639  +  if( argc<2 ){
       123640  +    sqlite3_result_error(context, "insufficient arguments", -1);
       123641  +    return;
       123642  +  }
125992 123643   
125993 123644     nName = sqlite3_value_bytes(argv[0]);
125994 123645     zName = (const char *)sqlite3_value_text(argv[0]);
125995 123646     nInput = sqlite3_value_bytes(argv[argc-1]);
125996 123647     zInput = (const char *)sqlite3_value_text(argv[argc-1]);
125997 123648   
125998         -  if( argc==3 ){
125999         -    zArg = (const char *)sqlite3_value_text(argv[1]);
126000         -  }
126001         -
126002 123649     pHash = (Fts3Hash *)sqlite3_user_data(context);
126003 123650     p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
126004 123651   
126005 123652     if( !p ){
126006 123653       char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
126007 123654       sqlite3_result_error(context, zErr, -1);
126008 123655       sqlite3_free(zErr);
126009 123656       return;
126010 123657     }
126011 123658   
126012 123659     pRet = Tcl_NewObj();
126013 123660     Tcl_IncrRefCount(pRet);
126014 123661   
126015         -  if( SQLITE_OK!=p->xCreate(zArg ? 1 : 0, &zArg, &pTokenizer) ){
       123662  +  for(i=1; i<argc-1; i++){
       123663  +    azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
       123664  +  }
       123665  +
       123666  +  if( SQLITE_OK!=p->xCreate(argc-2, azArg, &pTokenizer) ){
126016 123667       zErr = "error in xCreate()";
126017 123668       goto finish;
126018 123669     }
126019 123670     pTokenizer->pModule = p;
126020 123671     if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){
126021 123672       zErr = "error in xOpen()";
126022 123673       goto finish;
................................................................................
126192 123843       rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0);
126193 123844     }
126194 123845     if( SQLITE_OK==rc ){
126195 123846       rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0);
126196 123847     }
126197 123848   #ifdef SQLITE_TEST
126198 123849     if( SQLITE_OK==rc ){
126199         -    rc = sqlite3_create_function(db, zTest, 2, any, p, testFunc, 0, 0);
126200         -  }
126201         -  if( SQLITE_OK==rc ){
126202         -    rc = sqlite3_create_function(db, zTest, 3, any, p, testFunc, 0, 0);
       123850  +    rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
126203 123851     }
126204 123852     if( SQLITE_OK==rc ){
126205 123853       rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
126206 123854     }
126207 123855   #endif
126208 123856   
126209 123857   #ifdef SQLITE_TEST
................................................................................
126586 124234   **
126587 124235   **   fts3SegReaderNext()
126588 124236   **   fts3SegReaderFirstDocid()
126589 124237   **   fts3SegReaderNextDocid()
126590 124238   */
126591 124239   struct Fts3SegReader {
126592 124240     int iIdx;                       /* Index within level, or 0x7FFFFFFF for PT */
126593         -  int bLookup;                    /* True for a lookup only */
       124241  +  u8 bLookup;                     /* True for a lookup only */
       124242  +  u8 rootOnly;                    /* True for a root-only reader */
126594 124243   
126595 124244     sqlite3_int64 iStartBlock;      /* Rowid of first leaf block to traverse */
126596 124245     sqlite3_int64 iLeafEndBlock;    /* Rowid of final leaf block to traverse */
126597 124246     sqlite3_int64 iEndBlock;        /* Rowid of final block in segment (or 0) */
126598 124247     sqlite3_int64 iCurrentBlock;    /* Current leaf block (or 0) */
126599 124248   
126600 124249     char *aNode;                    /* Pointer to node data (or NULL) */
................................................................................
126620 124269     */
126621 124270     char *pOffsetList;
126622 124271     int nOffsetList;                /* For descending pending seg-readers only */
126623 124272     sqlite3_int64 iDocid;
126624 124273   };
126625 124274   
126626 124275   #define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
126627         -#define fts3SegReaderIsRootOnly(p) ((p)->aNode==(char *)&(p)[1])
       124276  +#define fts3SegReaderIsRootOnly(p) ((p)->rootOnly!=0)
126628 124277   
126629 124278   /*
126630 124279   ** An instance of this structure is used to create a segment b-tree in the
126631 124280   ** database. The internal details of this type are only accessed by the
126632 124281   ** following functions:
126633 124282   **
126634 124283   **   fts3SegWriterAdd()
................................................................................
128031 125680   
128032 125681     pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra);
128033 125682     if( !pReader ){
128034 125683       return SQLITE_NOMEM;
128035 125684     }
128036 125685     memset(pReader, 0, sizeof(Fts3SegReader));
128037 125686     pReader->iIdx = iAge;
128038         -  pReader->bLookup = bLookup;
       125687  +  pReader->bLookup = bLookup!=0;
128039 125688     pReader->iStartBlock = iStartLeaf;
128040 125689     pReader->iLeafEndBlock = iEndLeaf;
128041 125690     pReader->iEndBlock = iEndBlock;
128042 125691   
128043 125692     if( nExtra ){
128044 125693       /* The entire segment is stored in the root node. */
128045 125694       pReader->aNode = (char *)&pReader[1];
       125695  +    pReader->rootOnly = 1;
128046 125696       pReader->nNode = nRoot;
128047 125697       memcpy(pReader->aNode, zRoot, nRoot);
128048 125698       memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
128049 125699     }else{
128050 125700       pReader->iCurrentBlock = iStartLeaf-1;
128051 125701     }
128052 125702     *ppReader = pReader;
................................................................................
133436 131086   
133437 131087   typedef struct unicode_tokenizer unicode_tokenizer;
133438 131088   typedef struct unicode_cursor unicode_cursor;
133439 131089   
133440 131090   struct unicode_tokenizer {
133441 131091     sqlite3_tokenizer base;
133442 131092     int bRemoveDiacritic;
       131093  +  int nException;
       131094  +  int *aiException;
133443 131095   };
133444 131096   
133445 131097   struct unicode_cursor {
133446 131098     sqlite3_tokenizer_cursor base;
133447 131099     const unsigned char *aInput;    /* Input text being tokenized */
133448 131100     int nInput;                     /* Size of aInput[] in bytes */
133449 131101     int iOff;                       /* Current offset within aInput[] */
133450 131102     int iToken;                     /* Index of next token to be returned */
133451 131103     char *zToken;                   /* storage for current token */
133452 131104     int nAlloc;                     /* space allocated at zToken */
133453 131105   };
133454 131106   
       131107  +
       131108  +/*
       131109  +** Destroy a tokenizer allocated by unicodeCreate().
       131110  +*/
       131111  +static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
       131112  +  if( pTokenizer ){
       131113  +    unicode_tokenizer *p = (unicode_tokenizer *)pTokenizer;
       131114  +    sqlite3_free(p->aiException);
       131115  +    sqlite3_free(p);
       131116  +  }
       131117  +  return SQLITE_OK;
       131118  +}
       131119  +
       131120  +/*
       131121  +** As part of a tokenchars= or separators= option, the CREATE VIRTUAL TABLE
       131122  +** statement has specified that the tokenizer for this table shall consider
       131123  +** all characters in string zIn/nIn to be separators (if bAlnum==0) or
       131124  +** token characters (if bAlnum==1).
       131125  +**
       131126  +** For each codepoint in the zIn/nIn string, this function checks if the
       131127  +** sqlite3FtsUnicodeIsalnum() function already returns the desired result.
       131128  +** If so, no action is taken. Otherwise, the codepoint is added to the 
       131129  +** unicode_tokenizer.aiException[] array. For the purposes of tokenization,
       131130  +** the return value of sqlite3FtsUnicodeIsalnum() is inverted for all
       131131  +** codepoints in the aiException[] array.
       131132  +**
       131133  +** If a standalone diacritic mark (one that sqlite3FtsUnicodeIsdiacritic()
       131134  +** identifies as a diacritic) occurs in the zIn/nIn string it is ignored.
       131135  +** It is not possible to change the behaviour of the tokenizer with respect
       131136  +** to these codepoints.
       131137  +*/
       131138  +static int unicodeAddExceptions(
       131139  +  unicode_tokenizer *p,           /* Tokenizer to add exceptions to */
       131140  +  int bAlnum,                     /* Replace Isalnum() return value with this */
       131141  +  const char *zIn,                /* Array of characters to make exceptions */
       131142  +  int nIn                         /* Length of z in bytes */
       131143  +){
       131144  +  const unsigned char *z = (const unsigned char *)zIn;
       131145  +  const unsigned char *zTerm = &z[nIn];
       131146  +  int iCode;
       131147  +  int nEntry = 0;
       131148  +
       131149  +  assert( bAlnum==0 || bAlnum==1 );
       131150  +
       131151  +  while( z<zTerm ){
       131152  +    READ_UTF8(z, zTerm, iCode);
       131153  +    assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
       131154  +    if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum 
       131155  +     && sqlite3FtsUnicodeIsdiacritic(iCode)==0 
       131156  +    ){
       131157  +      nEntry++;
       131158  +    }
       131159  +  }
       131160  +
       131161  +  if( nEntry ){
       131162  +    int *aNew;                    /* New aiException[] array */
       131163  +    int nNew;                     /* Number of valid entries in array aNew[] */
       131164  +
       131165  +    aNew = sqlite3_realloc(p->aiException, (p->nException+nEntry)*sizeof(int));
       131166  +    if( aNew==0 ) return SQLITE_NOMEM;
       131167  +    nNew = p->nException;
       131168  +
       131169  +    z = (const unsigned char *)zIn;
       131170  +    while( z<zTerm ){
       131171  +      READ_UTF8(z, zTerm, iCode);
       131172  +      if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum 
       131173  +       && sqlite3FtsUnicodeIsdiacritic(iCode)==0
       131174  +      ){
       131175  +        int i, j;
       131176  +        for(i=0; i<nNew && aNew[i]<iCode; i++);
       131177  +        for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
       131178  +        aNew[i] = iCode;
       131179  +        nNew++;
       131180  +      }
       131181  +    }
       131182  +    p->aiException = aNew;
       131183  +    p->nException = nNew;
       131184  +  }
       131185  +
       131186  +  return SQLITE_OK;
       131187  +}
       131188  +
       131189  +/*
       131190  +** Return true if the p->aiException[] array contains the value iCode.
       131191  +*/
       131192  +static int unicodeIsException(unicode_tokenizer *p, int iCode){
       131193  +  if( p->nException>0 ){
       131194  +    int *a = p->aiException;
       131195  +    int iLo = 0;
       131196  +    int iHi = p->nException-1;
       131197  +
       131198  +    while( iHi>=iLo ){
       131199  +      int iTest = (iHi + iLo) / 2;
       131200  +      if( iCode==a[iTest] ){
       131201  +        return 1;
       131202  +      }else if( iCode>a[iTest] ){
       131203  +        iLo = iTest+1;
       131204  +      }else{
       131205  +        iHi = iTest-1;
       131206  +      }
       131207  +    }
       131208  +  }
       131209  +
       131210  +  return 0;
       131211  +}
       131212  +
       131213  +/*
       131214  +** Return true if, for the purposes of tokenization, codepoint iCode is
       131215  +** considered a token character (not a separator).
       131216  +*/
       131217  +static int unicodeIsAlnum(unicode_tokenizer *p, int iCode){
       131218  +  assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
       131219  +  return sqlite3FtsUnicodeIsalnum(iCode) ^ unicodeIsException(p, iCode);
       131220  +}
       131221  +
133455 131222   /*
133456 131223   ** Create a new tokenizer instance.
133457 131224   */
133458 131225   static int unicodeCreate(
133459 131226     int nArg,                       /* Size of array argv[] */
133460 131227     const char * const *azArg,      /* Tokenizer creation arguments */
133461 131228     sqlite3_tokenizer **pp          /* OUT: New tokenizer handle */
133462 131229   ){
133463 131230     unicode_tokenizer *pNew;        /* New tokenizer object */
133464 131231     int i;
       131232  +  int rc = SQLITE_OK;
       131233  +
133465 131234     pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer));
133466         -  if( pNew==NULL ){
133467         -    return SQLITE_NOMEM;
133468         -  }
       131235  +  if( pNew==NULL ) return SQLITE_NOMEM;
133469 131236     memset(pNew, 0, sizeof(unicode_tokenizer));
133470 131237     pNew->bRemoveDiacritic = 1;
133471 131238   
133472         -  for(i=0; i<nArg; i++){
       131239  +  for(i=0; rc==SQLITE_OK && i<nArg; i++){
133473 131240       const char *z = azArg[i];
133474 131241       int n = strlen(z);
133475 131242   
133476 131243       if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
133477 131244         pNew->bRemoveDiacritic = 1;
133478 131245       }
133479 131246       else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
133480 131247         pNew->bRemoveDiacritic = 0;
133481 131248       }
       131249  +    else if( n>=11 && memcmp("tokenchars=", z, 11)==0 ){
       131250  +      rc = unicodeAddExceptions(pNew, 1, &z[11], n-11);
       131251  +    }
       131252  +    else if( n>=11 && memcmp("separators=", z, 11)==0 ){
       131253  +      rc = unicodeAddExceptions(pNew, 0, &z[11], n-11);
       131254  +    }
133482 131255       else{
133483 131256         /* Unrecognized argument */
133484         -      return SQLITE_ERROR;
       131257  +      rc  = SQLITE_ERROR;
133485 131258       }
133486 131259     }
133487 131260   
133488         -  *pp = &pNew->base;
133489         -  return SQLITE_OK;
133490         -}
133491         -
133492         -/*
133493         -** Destroy a tokenizer allocated by unicodeCreate().
133494         -*/
133495         -static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
133496         -  sqlite3_free(pTokenizer);
133497         -  return SQLITE_OK;
       131261  +  if( rc!=SQLITE_OK ){
       131262  +    unicodeDestroy((sqlite3_tokenizer *)pNew);
       131263  +    pNew = 0;
       131264  +  }
       131265  +  *pp = (sqlite3_tokenizer *)pNew;
       131266  +  return rc;
133498 131267   }
133499 131268   
133500 131269   /*
133501 131270   ** Prepare to begin tokenizing a particular string.  The input
133502 131271   ** string to be tokenized is pInput[0..nBytes-1].  A cursor
133503 131272   ** used to incrementally tokenize this string is returned in 
133504 131273   ** *ppCursor.
................................................................................
133543 131312   }
133544 131313   
133545 131314   /*
133546 131315   ** Extract the next token from a tokenization cursor.  The cursor must
133547 131316   ** have been opened by a prior call to simpleOpen().
133548 131317   */
133549 131318   static int unicodeNext(
133550         -  sqlite3_tokenizer_cursor *p,    /* Cursor returned by simpleOpen */
       131319  +  sqlite3_tokenizer_cursor *pC,   /* Cursor returned by simpleOpen */
133551 131320     const char **paToken,           /* OUT: Token text */
133552 131321     int *pnToken,                   /* OUT: Number of bytes at *paToken */
133553 131322     int *piStart,                   /* OUT: Starting offset of token */
133554 131323     int *piEnd,                     /* OUT: Ending offset of token */
133555 131324     int *piPos                      /* OUT: Position integer of token */
133556 131325   ){
133557         -  unicode_cursor *pCsr = (unicode_cursor *)p;
       131326  +  unicode_cursor *pCsr = (unicode_cursor *)pC;
       131327  +  unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
133558 131328     int iCode;
133559 131329     char *zOut;
133560 131330     const unsigned char *z = &pCsr->aInput[pCsr->iOff];
133561 131331     const unsigned char *zStart = z;
133562 131332     const unsigned char *zEnd;
133563 131333     const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
133564 131334   
133565 131335     /* Scan past any delimiter characters before the start of the next token.
133566 131336     ** Return SQLITE_DONE early if this takes us all the way to the end of 
133567 131337     ** the input.  */
133568 131338     while( z<zTerm ){
133569 131339       READ_UTF8(z, zTerm, iCode);
133570         -    if( sqlite3FtsUnicodeIsalnum(iCode) ) break;
       131340  +    if( unicodeIsAlnum(p, iCode) ) break;
133571 131341       zStart = z;
133572 131342     }
133573 131343     if( zStart>=zTerm ) return SQLITE_DONE;
133574 131344   
133575 131345     zOut = pCsr->zToken;
133576 131346     do {
133577 131347       int iOut;
................................................................................
133583 131353         zOut = &zNew[zOut - pCsr->zToken];
133584 131354         pCsr->zToken = zNew;
133585 131355         pCsr->nAlloc += 64;
133586 131356       }
133587 131357   
133588 131358       /* Write the folded case of the last character read to the output */
133589 131359       zEnd = z;
133590         -    iOut = sqlite3FtsUnicodeFold(iCode, 
133591         -        ((unicode_tokenizer *)pCsr->base.pTokenizer)->bRemoveDiacritic
133592         -    );
       131360  +    iOut = sqlite3FtsUnicodeFold(iCode, p->bRemoveDiacritic);
133593 131361       if( iOut ){
133594 131362         WRITE_UTF8(zOut, iOut);
133595 131363       }
133596 131364   
133597 131365       /* If the cursor is not at EOF, read the next character */
133598 131366       if( z>=zTerm ) break;
133599 131367       READ_UTF8(z, zTerm, iCode);
133600         -  }while( sqlite3FtsUnicodeIsalnum(iCode) 
       131368  +  }while( unicodeIsAlnum(p, iCode) 
133601 131369          || sqlite3FtsUnicodeIsdiacritic(iCode)
133602 131370     );
133603 131371   
133604 131372     /* Set the output variables and return. */
133605 131373     pCsr->iOff = (z - pCsr->aInput);
133606 131374     *paToken = pCsr->zToken;
133607 131375     *pnToken = zOut - pCsr->zToken;
................................................................................
133777 131545           iLo = iTest+1;
133778 131546         }else{
133779 131547           iHi = iTest-1;
133780 131548         }
133781 131549       }
133782 131550       assert( aEntry[0]<key );
133783 131551       assert( key>=aEntry[iRes] );
133784         -    return (c >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
       131552  +    return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
133785 131553     }
133786 131554     return 1;
133787 131555   }
133788 131556   
133789 131557   
133790 131558   /*
133791 131559   ** If the argument is a codepoint corresponding to a lowercase letter
................................................................................
138057 135825       iEnd = ubrk_next(pCsr->pIter);
138058 135826       if( iEnd==UBRK_DONE ){
138059 135827         return SQLITE_DONE;
138060 135828       }
138061 135829   
138062 135830       while( iStart<iEnd ){
138063 135831         int iWhite = iStart;
138064         -      U8_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
       135832  +      U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
138065 135833         if( u_isspace(c) ){
138066 135834           iStart = iWhite;
138067 135835         }else{
138068 135836           break;
138069 135837         }
138070 135838       }
138071 135839       assert(iStart<=iEnd);