Back to index

php5  5.3.10
os.c
Go to the documentation of this file.
00001 /*
00002 ** 2001 September 16
00003 **
00004 ** The author disclaims copyright to this source code.  In place of
00005 ** a legal notice, here is a blessing:
00006 **
00007 **    May you do good and not evil.
00008 **    May you find forgiveness for yourself and forgive others.
00009 **    May you share freely, never taking more than you give.
00010 **
00011 ******************************************************************************
00012 **
00013 ** This file contains code that is specific to particular operating
00014 ** systems.  The purpose of this file is to provide a uniform abstraction
00015 ** on which the rest of SQLite can operate.
00016 */
00017 #include "os.h"          /* Must be first to enable large file support */
00018 #include "sqliteInt.h"
00019 
00020 #if OS_UNIX
00021 # include <time.h>
00022 # include <errno.h>
00023 # include <unistd.h>
00024 # ifndef O_LARGEFILE
00025 #  define O_LARGEFILE 0
00026 # endif
00027 # ifdef SQLITE_DISABLE_LFS
00028 #  undef O_LARGEFILE
00029 #  define O_LARGEFILE 0
00030 # endif
00031 # ifndef O_NOFOLLOW
00032 #  define O_NOFOLLOW 0
00033 # endif
00034 # ifndef O_BINARY
00035 #  define O_BINARY 0
00036 # endif
00037 #endif
00038 
00039 
00040 #if OS_WIN
00041 # include <winbase.h>
00042 #endif
00043 
00044 #if OS_MAC
00045 # include <extras.h>
00046 # include <path2fss.h>
00047 # include <TextUtils.h>
00048 # include <FinderRegistry.h>
00049 # include <Folders.h>
00050 # include <Timer.h>
00051 # include <OSUtils.h>
00052 #endif
00053 
00054 /*
00055 ** The DJGPP compiler environment looks mostly like Unix, but it
00056 ** lacks the fcntl() system call.  So redefine fcntl() to be something
00057 ** that always succeeds.  This means that locking does not occur under
00058 ** DJGPP.  But its DOS - what did you expect?
00059 */
00060 #ifdef __DJGPP__
00061 # define fcntl(A,B,C) 0
00062 #endif
00063 
00064 /*
00065 ** Macros used to determine whether or not to use threads.  The
00066 ** SQLITE_UNIX_THREADS macro is defined if we are synchronizing for
00067 ** Posix threads and SQLITE_W32_THREADS is defined if we are
00068 ** synchronizing using Win32 threads.
00069 */
00070 #if OS_UNIX && defined(THREADSAFE) && THREADSAFE
00071 # include <pthread.h>
00072 # define SQLITE_UNIX_THREADS 1
00073 #endif
00074 #if OS_WIN && defined(THREADSAFE) && THREADSAFE
00075 # define SQLITE_W32_THREADS 1
00076 #endif
00077 #if OS_MAC && defined(THREADSAFE) && THREADSAFE
00078 # include <Multiprocessing.h>
00079 # define SQLITE_MACOS_MULTITASKING 1
00080 #endif
00081 
00082 /*
00083 ** Macros for performance tracing.  Normally turned off
00084 */
00085 #if 0
00086 static int last_page = 0;
00087 __inline__ unsigned long long int hwtime(void){
00088   unsigned long long int x;
00089   __asm__("rdtsc\n\t"
00090           "mov %%edx, %%ecx\n\t"
00091           :"=A" (x));
00092   return x;
00093 }
00094 static unsigned long long int g_start;
00095 static unsigned int elapse;
00096 #define TIMER_START       g_start=hwtime()
00097 #define TIMER_END         elapse=hwtime()-g_start
00098 #define SEEK(X)           last_page=(X)
00099 #define TRACE1(X)         fprintf(stderr,X)
00100 #define TRACE2(X,Y)       fprintf(stderr,X,Y)
00101 #define TRACE3(X,Y,Z)     fprintf(stderr,X,Y,Z)
00102 #define TRACE4(X,Y,Z,A)   fprintf(stderr,X,Y,Z,A)
00103 #define TRACE5(X,Y,Z,A,B) fprintf(stderr,X,Y,Z,A,B)
00104 #else
00105 #define TIMER_START
00106 #define TIMER_END
00107 #define SEEK(X)
00108 #define TRACE1(X)
00109 #define TRACE2(X,Y)
00110 #define TRACE3(X,Y,Z)
00111 #define TRACE4(X,Y,Z,A)
00112 #define TRACE5(X,Y,Z,A,B)
00113 #endif
00114 
00115 
00116 #if OS_UNIX
00117 /*
00118 ** Here is the dirt on POSIX advisory locks:  ANSI STD 1003.1 (1996)
00119 ** section 6.5.2.2 lines 483 through 490 specify that when a process
00120 ** sets or clears a lock, that operation overrides any prior locks set
00121 ** by the same process.  It does not explicitly say so, but this implies
00122 ** that it overrides locks set by the same process using a different
00123 ** file descriptor.  Consider this test case:
00124 **
00125 **       int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
00126 **       int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
00127 **
00128 ** Suppose ./file1 and ./file2 are really the same file (because
00129 ** one is a hard or symbolic link to the other) then if you set
00130 ** an exclusive lock on fd1, then try to get an exclusive lock
00131 ** on fd2, it works.  I would have expected the second lock to
00132 ** fail since there was already a lock on the file due to fd1.
00133 ** But not so.  Since both locks came from the same process, the
00134 ** second overrides the first, even though they were on different
00135 ** file descriptors opened on different file names.
00136 **
00137 ** Bummer.  If you ask me, this is broken.  Badly broken.  It means
00138 ** that we cannot use POSIX locks to synchronize file access among
00139 ** competing threads of the same process.  POSIX locks will work fine
00140 ** to synchronize access for threads in separate processes, but not
00141 ** threads within the same process.
00142 **
00143 ** To work around the problem, SQLite has to manage file locks internally
00144 ** on its own.  Whenever a new database is opened, we have to find the
00145 ** specific inode of the database file (the inode is determined by the
00146 ** st_dev and st_ino fields of the stat structure that fstat() fills in)
00147 ** and check for locks already existing on that inode.  When locks are
00148 ** created or removed, we have to look at our own internal record of the
00149 ** locks to see if another thread has previously set a lock on that same
00150 ** inode.
00151 **
00152 ** The OsFile structure for POSIX is no longer just an integer file
00153 ** descriptor.  It is now a structure that holds the integer file
00154 ** descriptor and a pointer to a structure that describes the internal
00155 ** locks on the corresponding inode.  There is one locking structure
00156 ** per inode, so if the same inode is opened twice, both OsFile structures
00157 ** point to the same locking structure.  The locking structure keeps
00158 ** a reference count (so we will know when to delete it) and a "cnt"
00159 ** field that tells us its internal lock status.  cnt==0 means the
00160 ** file is unlocked.  cnt==-1 means the file has an exclusive lock.
00161 ** cnt>0 means there are cnt shared locks on the file.
00162 **
00163 ** Any attempt to lock or unlock a file first checks the locking
00164 ** structure.  The fcntl() system call is only invoked to set a 
00165 ** POSIX lock if the internal lock structure transitions between
00166 ** a locked and an unlocked state.
00167 **
00168 ** 2004-Jan-11:
00169 ** More recent discoveries about POSIX advisory locks.  (The more
00170 ** I discover, the more I realize the a POSIX advisory locks are
00171 ** an abomination.)
00172 **
00173 ** If you close a file descriptor that points to a file that has locks,
00174 ** all locks on that file that are owned by the current process are
00175 ** released.  To work around this problem, each OsFile structure contains
00176 ** a pointer to an openCnt structure.  There is one openCnt structure
00177 ** per open inode, which means that multiple OsFiles can point to a single
00178 ** openCnt.  When an attempt is made to close an OsFile, if there are
00179 ** other OsFiles open on the same inode that are holding locks, the call
00180 ** to close() the file descriptor is deferred until all of the locks clear.
00181 ** The openCnt structure keeps a list of file descriptors that need to
00182 ** be closed and that list is walked (and cleared) when the last lock
00183 ** clears.
00184 **
00185 ** First, under Linux threads, because each thread has a separate
00186 ** process ID, lock operations in one thread do not override locks
00187 ** to the same file in other threads.  Linux threads behave like
00188 ** separate processes in this respect.  But, if you close a file
00189 ** descriptor in linux threads, all locks are cleared, even locks
00190 ** on other threads and even though the other threads have different
00191 ** process IDs.  Linux threads is inconsistent in this respect.
00192 ** (I'm beginning to think that linux threads is an abomination too.)
00193 ** The consequence of this all is that the hash table for the lockInfo
00194 ** structure has to include the process id as part of its key because
00195 ** locks in different threads are treated as distinct.  But the 
00196 ** openCnt structure should not include the process id in its
00197 ** key because close() clears lock on all threads, not just the current
00198 ** thread.  Were it not for this goofiness in linux threads, we could
00199 ** combine the lockInfo and openCnt structures into a single structure.
00200 */
00201 
00202 /*
00203 ** An instance of the following structure serves as the key used
00204 ** to locate a particular lockInfo structure given its inode.  Note
00205 ** that we have to include the process ID as part of the key.  On some
00206 ** threading implementations (ex: linux), each thread has a separate
00207 ** process ID.
00208 */
00209 struct lockKey {
00210   dev_t dev;   /* Device number */
00211   ino_t ino;   /* Inode number */
00212   pid_t pid;   /* Process ID */
00213 };
00214 
00215 /*
00216 ** An instance of the following structure is allocated for each open
00217 ** inode on each thread with a different process ID.  (Threads have
00218 ** different process IDs on linux, but not on most other unixes.)
00219 **
00220 ** A single inode can have multiple file descriptors, so each OsFile
00221 ** structure contains a pointer to an instance of this object and this
00222 ** object keeps a count of the number of OsFiles pointing to it.
00223 */
00224 struct lockInfo {
00225   struct lockKey key;  /* The lookup key */
00226   int cnt;             /* 0: unlocked.  -1: write lock.  1...: read lock. */
00227   int nRef;            /* Number of pointers to this structure */
00228 };
00229 
00230 /*
00231 ** An instance of the following structure serves as the key used
00232 ** to locate a particular openCnt structure given its inode.  This
00233 ** is the same as the lockKey except that the process ID is omitted.
00234 */
00235 struct openKey {
00236   dev_t dev;   /* Device number */
00237   ino_t ino;   /* Inode number */
00238 };
00239 
00240 /*
00241 ** An instance of the following structure is allocated for each open
00242 ** inode.  This structure keeps track of the number of locks on that
00243 ** inode.  If a close is attempted against an inode that is holding
00244 ** locks, the close is deferred until all locks clear by adding the
00245 ** file descriptor to be closed to the pending list.
00246 */
00247 struct openCnt {
00248   struct openKey key;   /* The lookup key */
00249   int nRef;             /* Number of pointers to this structure */
00250   int nLock;            /* Number of outstanding locks */
00251   int nPending;         /* Number of pending close() operations */
00252   int *aPending;        /* Malloced space holding fd's awaiting a close() */
00253 };
00254 
00255 /* 
00256 ** These hash table maps inodes and process IDs into lockInfo and openCnt
00257 ** structures.  Access to these hash tables must be protected by a mutex.
00258 */
00259 static Hash lockHash = { SQLITE_HASH_BINARY, 0, 0, 0, 0, 0 };
00260 static Hash openHash = { SQLITE_HASH_BINARY, 0, 0, 0, 0, 0 };
00261 
00262 /*
00263 ** Release a lockInfo structure previously allocated by findLockInfo().
00264 */
00265 static void releaseLockInfo(struct lockInfo *pLock){
00266   pLock->nRef--;
00267   if( pLock->nRef==0 ){
00268     sqliteHashInsert(&lockHash, &pLock->key, sizeof(pLock->key), 0);
00269     sqliteFree(pLock);
00270   }
00271 }
00272 
00273 /*
00274 ** Release a openCnt structure previously allocated by findLockInfo().
00275 */
00276 static void releaseOpenCnt(struct openCnt *pOpen){
00277   pOpen->nRef--;
00278   if( pOpen->nRef==0 ){
00279     sqliteHashInsert(&openHash, &pOpen->key, sizeof(pOpen->key), 0);
00280     sqliteFree(pOpen->aPending);
00281     sqliteFree(pOpen);
00282   }
00283 }
00284 
00285 /*
00286 ** Given a file descriptor, locate lockInfo and openCnt structures that
00287 ** describes that file descriptor.  Create a new ones if necessary.  The
00288 ** return values might be unset if an error occurs.
00289 **
00290 ** Return the number of errors.
00291 */
00292 int findLockInfo(
00293   int fd,                      /* The file descriptor used in the key */
00294   struct lockInfo **ppLock,    /* Return the lockInfo structure here */
00295   struct openCnt **ppOpen   /* Return the openCnt structure here */
00296 ){
00297   int rc;
00298   struct lockKey key1;
00299   struct openKey key2;
00300   struct stat statbuf;
00301   struct lockInfo *pLock;
00302   struct openCnt *pOpen;
00303   rc = fstat(fd, &statbuf);
00304   if( rc!=0 ) return 1;
00305   memset(&key1, 0, sizeof(key1));
00306   key1.dev = statbuf.st_dev;
00307   key1.ino = statbuf.st_ino;
00308   key1.pid = getpid();
00309   memset(&key2, 0, sizeof(key2));
00310   key2.dev = statbuf.st_dev;
00311   key2.ino = statbuf.st_ino;
00312   pLock = (struct lockInfo*)sqliteHashFind(&lockHash, &key1, sizeof(key1));
00313   if( pLock==0 ){
00314     struct lockInfo *pOld;
00315     pLock = sqliteMallocRaw( sizeof(*pLock) );
00316     if( pLock==0 ) return 1;
00317     pLock->key = key1;
00318     pLock->nRef = 1;
00319     pLock->cnt = 0;
00320     pOld = sqliteHashInsert(&lockHash, &pLock->key, sizeof(key1), pLock);
00321     if( pOld!=0 ){
00322       assert( pOld==pLock );
00323       sqliteFree(pLock);
00324       return 1;
00325     }
00326   }else{
00327     pLock->nRef++;
00328   }
00329   *ppLock = pLock;
00330   pOpen = (struct openCnt*)sqliteHashFind(&openHash, &key2, sizeof(key2));
00331   if( pOpen==0 ){
00332     struct openCnt *pOld;
00333     pOpen = sqliteMallocRaw( sizeof(*pOpen) );
00334     if( pOpen==0 ){
00335       releaseLockInfo(pLock);
00336       return 1;
00337     }
00338     pOpen->key = key2;
00339     pOpen->nRef = 1;
00340     pOpen->nLock = 0;
00341     pOpen->nPending = 0;
00342     pOpen->aPending = 0;
00343     pOld = sqliteHashInsert(&openHash, &pOpen->key, sizeof(key2), pOpen);
00344     if( pOld!=0 ){
00345       assert( pOld==pOpen );
00346       sqliteFree(pOpen);
00347       releaseLockInfo(pLock);
00348       return 1;
00349     }
00350   }else{
00351     pOpen->nRef++;
00352   }
00353   *ppOpen = pOpen;
00354   return 0;
00355 }
00356 
00357 #endif  
00359 /*
00360 ** If we compile with the SQLITE_TEST macro set, then the following block
00361 ** of code will give us the ability to simulate a disk I/O error.  This
00362 ** is used for testing the I/O recovery logic.
00363 */
00364 #ifdef SQLITE_TEST
00365 int sqlite_io_error_pending = 0;
00366 #define SimulateIOError(A)  \
00367    if( sqlite_io_error_pending ) \
00368      if( sqlite_io_error_pending-- == 1 ){ local_ioerr(); return A; }
00369 static void local_ioerr(){
00370   sqlite_io_error_pending = 0;  /* Really just a place to set a breakpoint */
00371 }
00372 #else
00373 #define SimulateIOError(A)
00374 #endif
00375 
00376 /*
00377 ** When testing, keep a count of the number of open files.
00378 */
00379 #ifdef SQLITE_TEST
00380 int sqlite_open_file_count = 0;
00381 #define OpenCounter(X)  sqlite_open_file_count+=(X)
00382 #else
00383 #define OpenCounter(X)
00384 #endif
00385 
00386 
00387 /*
00388 ** Delete the named file
00389 */
00390 int sqliteOsDelete(const char *zFilename){
00391 #if OS_UNIX
00392   unlink(zFilename);
00393 #endif
00394 #if OS_WIN
00395   DeleteFile(zFilename);
00396 #endif
00397 #if OS_MAC
00398   unlink(zFilename);
00399 #endif
00400   return SQLITE_OK;
00401 }
00402 
00403 /*
00404 ** Return TRUE if the named file exists.
00405 */
00406 int sqliteOsFileExists(const char *zFilename){
00407 #if OS_UNIX
00408   return access(zFilename, 0)==0;
00409 #endif
00410 #if OS_WIN
00411   return GetFileAttributes(zFilename) != 0xffffffff;
00412 #endif
00413 #if OS_MAC
00414   return access(zFilename, 0)==0;
00415 #endif
00416 }
00417 
00418 
00419 #if 0 /* NOT USED */
00420 /*
00421 ** Change the name of an existing file.
00422 */
00423 int sqliteOsFileRename(const char *zOldName, const char *zNewName){
00424 #if OS_UNIX
00425   if( link(zOldName, zNewName) ){
00426     return SQLITE_ERROR;
00427   }
00428   unlink(zOldName);
00429   return SQLITE_OK;
00430 #endif
00431 #if OS_WIN
00432   if( !MoveFile(zOldName, zNewName) ){
00433     return SQLITE_ERROR;
00434   }
00435   return SQLITE_OK;
00436 #endif
00437 #if OS_MAC
00438   /**** FIX ME ***/
00439   return SQLITE_ERROR;
00440 #endif
00441 }
00442 #endif /* NOT USED */
00443 
00444 /*
00445 ** Attempt to open a file for both reading and writing.  If that
00446 ** fails, try opening it read-only.  If the file does not exist,
00447 ** try to create it.
00448 **
00449 ** On success, a handle for the open file is written to *id
00450 ** and *pReadonly is set to 0 if the file was opened for reading and
00451 ** writing or 1 if the file was opened read-only.  The function returns
00452 ** SQLITE_OK.
00453 **
00454 ** On failure, the function returns SQLITE_CANTOPEN and leaves
00455 ** *id and *pReadonly unchanged.
00456 */
00457 int sqliteOsOpenReadWrite(
00458   const char *zFilename,
00459   OsFile *id,
00460   int *pReadonly
00461 ){
00462 #if OS_UNIX
00463   int rc;
00464   id->dirfd = -1;
00465   id->fd = open(zFilename, O_RDWR|O_CREAT|O_LARGEFILE|O_BINARY, 0644);
00466   if( id->fd<0 ){
00467 #ifdef EISDIR
00468     if( errno==EISDIR ){
00469       return SQLITE_CANTOPEN;
00470     }
00471 #endif
00472     id->fd = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY);
00473     if( id->fd<0 ){
00474       return SQLITE_CANTOPEN; 
00475     }
00476     *pReadonly = 1;
00477   }else{
00478     *pReadonly = 0;
00479   }
00480   sqliteOsEnterMutex();
00481   rc = findLockInfo(id->fd, &id->pLock, &id->pOpen);
00482   sqliteOsLeaveMutex();
00483   if( rc ){
00484     close(id->fd);
00485     return SQLITE_NOMEM;
00486   }
00487   id->locked = 0;
00488   TRACE3("OPEN    %-3d %s\n", id->fd, zFilename);
00489   OpenCounter(+1);
00490   return SQLITE_OK;
00491 #endif
00492 #if OS_WIN
00493   HANDLE h = CreateFile(zFilename,
00494      GENERIC_READ | GENERIC_WRITE,
00495      FILE_SHARE_READ | FILE_SHARE_WRITE,
00496      NULL,
00497      OPEN_ALWAYS,
00498      FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
00499      NULL
00500   );
00501   if( h==INVALID_HANDLE_VALUE ){
00502     h = CreateFile(zFilename,
00503        GENERIC_READ,
00504        FILE_SHARE_READ,
00505        NULL,
00506        OPEN_ALWAYS,
00507        FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
00508        NULL
00509     );
00510     if( h==INVALID_HANDLE_VALUE ){
00511       return SQLITE_CANTOPEN;
00512     }
00513     *pReadonly = 1;
00514   }else{
00515     *pReadonly = 0;
00516   }
00517   id->h = h;
00518   id->locked = 0;
00519   OpenCounter(+1);
00520   return SQLITE_OK;
00521 #endif
00522 #if OS_MAC
00523   FSSpec fsSpec;
00524 # ifdef _LARGE_FILE
00525   HFSUniStr255 dfName;
00526   FSRef fsRef;
00527   if( __path2fss(zFilename, &fsSpec) != noErr ){
00528     if( HCreate(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, 'SQLI', cDocumentFile) != noErr )
00529       return SQLITE_CANTOPEN;
00530   }
00531   if( FSpMakeFSRef(&fsSpec, &fsRef) != noErr )
00532     return SQLITE_CANTOPEN;
00533   FSGetDataForkName(&dfName);
00534   if( FSOpenFork(&fsRef, dfName.length, dfName.unicode,
00535                  fsRdWrShPerm, &(id->refNum)) != noErr ){
00536     if( FSOpenFork(&fsRef, dfName.length, dfName.unicode,
00537                    fsRdWrPerm, &(id->refNum)) != noErr ){
00538       if (FSOpenFork(&fsRef, dfName.length, dfName.unicode,
00539                    fsRdPerm, &(id->refNum)) != noErr )
00540         return SQLITE_CANTOPEN;
00541       else
00542         *pReadonly = 1;
00543     } else
00544       *pReadonly = 0;
00545   } else
00546     *pReadonly = 0;
00547 # else
00548   __path2fss(zFilename, &fsSpec);
00549   if( !sqliteOsFileExists(zFilename) ){
00550     if( HCreate(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, 'SQLI', cDocumentFile) != noErr )
00551       return SQLITE_CANTOPEN;
00552   }
00553   if( HOpenDF(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, fsRdWrShPerm, &(id->refNum)) != noErr ){
00554     if( HOpenDF(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, fsRdWrPerm, &(id->refNum)) != noErr ){
00555       if( HOpenDF(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, fsRdPerm, &(id->refNum)) != noErr )
00556         return SQLITE_CANTOPEN;
00557       else
00558         *pReadonly = 1;
00559     } else
00560       *pReadonly = 0;
00561   } else
00562     *pReadonly = 0;
00563 # endif
00564   if( HOpenRF(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, fsRdWrShPerm, &(id->refNumRF)) != noErr){
00565     id->refNumRF = -1;
00566   }
00567   id->locked = 0;
00568   id->delOnClose = 0;
00569   OpenCounter(+1);
00570   return SQLITE_OK;
00571 #endif
00572 }
00573 
00574 
00575 /*
00576 ** Attempt to open a new file for exclusive access by this process.
00577 ** The file will be opened for both reading and writing.  To avoid
00578 ** a potential security problem, we do not allow the file to have
00579 ** previously existed.  Nor do we allow the file to be a symbolic
00580 ** link.
00581 **
00582 ** If delFlag is true, then make arrangements to automatically delete
00583 ** the file when it is closed.
00584 **
00585 ** On success, write the file handle into *id and return SQLITE_OK.
00586 **
00587 ** On failure, return SQLITE_CANTOPEN.
00588 */
00589 int sqliteOsOpenExclusive(const char *zFilename, OsFile *id, int delFlag){
00590 #if OS_UNIX
00591   int rc;
00592   if( access(zFilename, 0)==0 ){
00593     return SQLITE_CANTOPEN;
00594   }
00595   id->dirfd = -1;
00596   id->fd = open(zFilename,
00597                 O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_LARGEFILE|O_BINARY, 0600);
00598   if( id->fd<0 ){
00599     return SQLITE_CANTOPEN;
00600   }
00601   sqliteOsEnterMutex();
00602   rc = findLockInfo(id->fd, &id->pLock, &id->pOpen);
00603   sqliteOsLeaveMutex();
00604   if( rc ){
00605     close(id->fd);
00606     unlink(zFilename);
00607     return SQLITE_NOMEM;
00608   }
00609   id->locked = 0;
00610   if( delFlag ){
00611     unlink(zFilename);
00612   }
00613   TRACE3("OPEN-EX %-3d %s\n", id->fd, zFilename);
00614   OpenCounter(+1);
00615   return SQLITE_OK;
00616 #endif
00617 #if OS_WIN
00618   HANDLE h;
00619   int fileflags;
00620   if( delFlag ){
00621     fileflags = FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_RANDOM_ACCESS 
00622                      | FILE_FLAG_DELETE_ON_CLOSE;
00623   }else{
00624     fileflags = FILE_FLAG_RANDOM_ACCESS;
00625   }
00626   h = CreateFile(zFilename,
00627      GENERIC_READ | GENERIC_WRITE,
00628      0,
00629      NULL,
00630      CREATE_ALWAYS,
00631      fileflags,
00632      NULL
00633   );
00634   if( h==INVALID_HANDLE_VALUE ){
00635     return SQLITE_CANTOPEN;
00636   }
00637   id->h = h;
00638   id->locked = 0;
00639   OpenCounter(+1);
00640   return SQLITE_OK;
00641 #endif
00642 #if OS_MAC
00643   FSSpec fsSpec;
00644 # ifdef _LARGE_FILE
00645   HFSUniStr255 dfName;
00646   FSRef fsRef;
00647   __path2fss(zFilename, &fsSpec);
00648   if( HCreate(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, 'SQLI', cDocumentFile) != noErr )
00649     return SQLITE_CANTOPEN;
00650   if( FSpMakeFSRef(&fsSpec, &fsRef) != noErr )
00651     return SQLITE_CANTOPEN;
00652   FSGetDataForkName(&dfName);
00653   if( FSOpenFork(&fsRef, dfName.length, dfName.unicode,
00654                  fsRdWrPerm, &(id->refNum)) != noErr )
00655     return SQLITE_CANTOPEN;
00656 # else
00657   __path2fss(zFilename, &fsSpec);
00658   if( HCreate(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, 'SQLI', cDocumentFile) != noErr )
00659     return SQLITE_CANTOPEN;
00660   if( HOpenDF(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, fsRdWrPerm, &(id->refNum)) != noErr )
00661     return SQLITE_CANTOPEN;
00662 # endif
00663   id->refNumRF = -1;
00664   id->locked = 0;
00665   id->delOnClose = delFlag;
00666   if (delFlag)
00667     id->pathToDel = sqliteOsFullPathname(zFilename);
00668   OpenCounter(+1);
00669   return SQLITE_OK;
00670 #endif
00671 }
00672 
00673 /*
00674 ** Attempt to open a new file for read-only access.
00675 **
00676 ** On success, write the file handle into *id and return SQLITE_OK.
00677 **
00678 ** On failure, return SQLITE_CANTOPEN.
00679 */
00680 int sqliteOsOpenReadOnly(const char *zFilename, OsFile *id){
00681 #if OS_UNIX
00682   int rc;
00683   id->dirfd = -1;
00684   id->fd = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY);
00685   if( id->fd<0 ){
00686     return SQLITE_CANTOPEN;
00687   }
00688   sqliteOsEnterMutex();
00689   rc = findLockInfo(id->fd, &id->pLock, &id->pOpen);
00690   sqliteOsLeaveMutex();
00691   if( rc ){
00692     close(id->fd);
00693     return SQLITE_NOMEM;
00694   }
00695   id->locked = 0;
00696   TRACE3("OPEN-RO %-3d %s\n", id->fd, zFilename);
00697   OpenCounter(+1);
00698   return SQLITE_OK;
00699 #endif
00700 #if OS_WIN
00701   HANDLE h = CreateFile(zFilename,
00702      GENERIC_READ,
00703      0,
00704      NULL,
00705      OPEN_EXISTING,
00706      FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
00707      NULL
00708   );
00709   if( h==INVALID_HANDLE_VALUE ){
00710     return SQLITE_CANTOPEN;
00711   }
00712   id->h = h;
00713   id->locked = 0;
00714   OpenCounter(+1);
00715   return SQLITE_OK;
00716 #endif
00717 #if OS_MAC
00718   FSSpec fsSpec;
00719 # ifdef _LARGE_FILE
00720   HFSUniStr255 dfName;
00721   FSRef fsRef;
00722   if( __path2fss(zFilename, &fsSpec) != noErr )
00723     return SQLITE_CANTOPEN;
00724   if( FSpMakeFSRef(&fsSpec, &fsRef) != noErr )
00725     return SQLITE_CANTOPEN;
00726   FSGetDataForkName(&dfName);
00727   if( FSOpenFork(&fsRef, dfName.length, dfName.unicode,
00728                  fsRdPerm, &(id->refNum)) != noErr )
00729     return SQLITE_CANTOPEN;
00730 # else
00731   __path2fss(zFilename, &fsSpec);
00732   if( HOpenDF(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, fsRdPerm, &(id->refNum)) != noErr )
00733     return SQLITE_CANTOPEN;
00734 # endif
00735   if( HOpenRF(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, fsRdWrShPerm, &(id->refNumRF)) != noErr){
00736     id->refNumRF = -1;
00737   }
00738   id->locked = 0;
00739   id->delOnClose = 0;
00740   OpenCounter(+1);
00741   return SQLITE_OK;
00742 #endif
00743 }
00744 
00745 /*
00746 ** Attempt to open a file descriptor for the directory that contains a
00747 ** file.  This file descriptor can be used to fsync() the directory
00748 ** in order to make sure the creation of a new file is actually written
00749 ** to disk.
00750 **
00751 ** This routine is only meaningful for Unix.  It is a no-op under
00752 ** windows since windows does not support hard links.
00753 **
00754 ** On success, a handle for a previously open file is at *id is
00755 ** updated with the new directory file descriptor and SQLITE_OK is
00756 ** returned.
00757 **
00758 ** On failure, the function returns SQLITE_CANTOPEN and leaves
00759 ** *id unchanged.
00760 */
00761 int sqliteOsOpenDirectory(
00762   const char *zDirname,
00763   OsFile *id
00764 ){
00765 #if OS_UNIX
00766   if( id->fd<0 ){
00767     /* Do not open the directory if the corresponding file is not already
00768     ** open. */
00769     return SQLITE_CANTOPEN;
00770   }
00771   assert( id->dirfd<0 );
00772   id->dirfd = open(zDirname, O_RDONLY|O_BINARY, 0644);
00773   if( id->dirfd<0 ){
00774     return SQLITE_CANTOPEN; 
00775   }
00776   TRACE3("OPENDIR %-3d %s\n", id->dirfd, zDirname);
00777 #endif
00778   return SQLITE_OK;
00779 }
00780 
00781 /*
00782 ** If the following global variable points to a string which is the
00783 ** name of a directory, then that directory will be used to store
00784 ** temporary files.
00785 */
00786 const char *sqlite_temp_directory = 0;
00787 
00788 /*
00789 ** Create a temporary file name in zBuf.  zBuf must be big enough to
00790 ** hold at least SQLITE_TEMPNAME_SIZE characters.
00791 */
00792 int sqliteOsTempFileName(char *zBuf){
00793 #if OS_UNIX
00794   static const char *azDirs[] = {
00795      0,
00796      "/var/tmp",
00797      "/usr/tmp",
00798      "/tmp",
00799      ".",
00800   };
00801   static unsigned char zChars[] =
00802     "abcdefghijklmnopqrstuvwxyz"
00803     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
00804     "0123456789";
00805   int i, j;
00806   struct stat buf;
00807   const char *zDir = ".";
00808   azDirs[0] = sqlite_temp_directory;
00809   for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
00810     if( azDirs[i]==0 ) continue;
00811     if( stat(azDirs[i], &buf) ) continue;
00812     if( !S_ISDIR(buf.st_mode) ) continue;
00813     if( access(azDirs[i], 07) ) continue;
00814     zDir = azDirs[i];
00815     break;
00816   }
00817   do{
00818     sprintf(zBuf, "%s/"TEMP_FILE_PREFIX, zDir);
00819     j = strlen(zBuf);
00820     sqliteRandomness(15, &zBuf[j]);
00821     for(i=0; i<15; i++, j++){
00822       zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
00823     }
00824     zBuf[j] = 0;
00825   }while( access(zBuf,0)==0 );
00826 #endif
00827 #if OS_WIN
00828   static char zChars[] =
00829     "abcdefghijklmnopqrstuvwxyz"
00830     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
00831     "0123456789";
00832   int i, j;
00833   const char *zDir;
00834   char zTempPath[SQLITE_TEMPNAME_SIZE];
00835   if( sqlite_temp_directory==0 ){
00836     GetTempPath(SQLITE_TEMPNAME_SIZE-30, zTempPath);
00837     for(i=strlen(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){}
00838     zTempPath[i] = 0;
00839     zDir = zTempPath;
00840   }else{
00841     zDir = sqlite_temp_directory;
00842   }
00843   for(;;){
00844     sprintf(zBuf, "%s\\"TEMP_FILE_PREFIX, zDir);
00845     j = strlen(zBuf);
00846     sqliteRandomness(15, &zBuf[j]);
00847     for(i=0; i<15; i++, j++){
00848       zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
00849     }
00850     zBuf[j] = 0;
00851     if( !sqliteOsFileExists(zBuf) ) break;
00852   }
00853 #endif
00854 #if OS_MAC
00855   static char zChars[] =
00856     "abcdefghijklmnopqrstuvwxyz"
00857     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
00858     "0123456789";
00859   int i, j;
00860   char *zDir;
00861   char zTempPath[SQLITE_TEMPNAME_SIZE];
00862   char zdirName[32];
00863   CInfoPBRec infoRec;
00864   Str31 dirName;
00865   memset(&infoRec, 0, sizeof(infoRec));
00866   memset(zTempPath, 0, SQLITE_TEMPNAME_SIZE);
00867   if( sqlite_temp_directory!=0 ){
00868     zDir = sqlite_temp_directory;
00869   }else if( FindFolder(kOnSystemDisk, kTemporaryFolderType,  kCreateFolder,
00870        &(infoRec.dirInfo.ioVRefNum), &(infoRec.dirInfo.ioDrParID)) == noErr ){
00871     infoRec.dirInfo.ioNamePtr = dirName;
00872     do{
00873       infoRec.dirInfo.ioFDirIndex = -1;
00874       infoRec.dirInfo.ioDrDirID = infoRec.dirInfo.ioDrParID;
00875       if( PBGetCatInfoSync(&infoRec) == noErr ){
00876         CopyPascalStringToC(dirName, zdirName);
00877         i = strlen(zdirName);
00878         memmove(&(zTempPath[i+1]), zTempPath, strlen(zTempPath));
00879         strcpy(zTempPath, zdirName);
00880         zTempPath[i] = ':';
00881       }else{
00882         *zTempPath = 0;
00883         break;
00884       }
00885     } while( infoRec.dirInfo.ioDrDirID != fsRtDirID );
00886     zDir = zTempPath;
00887   }
00888   if( zDir[0]==0 ){
00889     getcwd(zTempPath, SQLITE_TEMPNAME_SIZE-24);
00890     zDir = zTempPath;
00891   }
00892   for(;;){
00893     sprintf(zBuf, "%s"TEMP_FILE_PREFIX, zDir);
00894     j = strlen(zBuf);
00895     sqliteRandomness(15, &zBuf[j]);
00896     for(i=0; i<15; i++, j++){
00897       zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
00898     }
00899     zBuf[j] = 0;
00900     if( !sqliteOsFileExists(zBuf) ) break;
00901   }
00902 #endif
00903   return SQLITE_OK; 
00904 }
00905 
00906 /*
00907 ** Close a file.
00908 */
00909 int sqliteOsClose(OsFile *id){
00910 #if OS_UNIX
00911   sqliteOsUnlock(id);
00912   if( id->dirfd>=0 ) close(id->dirfd);
00913   id->dirfd = -1;
00914   sqliteOsEnterMutex();
00915   if( id->pOpen->nLock ){
00916     /* If there are outstanding locks, do not actually close the file just
00917     ** yet because that would clear those locks.  Instead, add the file
00918     ** descriptor to pOpen->aPending.  It will be automatically closed when
00919     ** the last lock is cleared.
00920     */
00921     int *aNew;
00922     struct openCnt *pOpen = id->pOpen;
00923     pOpen->nPending++;
00924     aNew = sqliteRealloc( pOpen->aPending, pOpen->nPending*sizeof(int) );
00925     if( aNew==0 ){
00926       /* If a malloc fails, just leak the file descriptor */
00927     }else{
00928       pOpen->aPending = aNew;
00929       pOpen->aPending[pOpen->nPending-1] = id->fd;
00930     }
00931   }else{
00932     /* There are no outstanding locks so we can close the file immediately */
00933     close(id->fd);
00934   }
00935   releaseLockInfo(id->pLock);
00936   releaseOpenCnt(id->pOpen);
00937   sqliteOsLeaveMutex();
00938   TRACE2("CLOSE   %-3d\n", id->fd);
00939   OpenCounter(-1);
00940   return SQLITE_OK;
00941 #endif
00942 #if OS_WIN
00943   CloseHandle(id->h);
00944   OpenCounter(-1);
00945   return SQLITE_OK;
00946 #endif
00947 #if OS_MAC
00948   if( id->refNumRF!=-1 )
00949     FSClose(id->refNumRF);
00950 # ifdef _LARGE_FILE
00951   FSCloseFork(id->refNum);
00952 # else
00953   FSClose(id->refNum);
00954 # endif
00955   if( id->delOnClose ){
00956     unlink(id->pathToDel);
00957     sqliteFree(id->pathToDel);
00958   }
00959   OpenCounter(-1);
00960   return SQLITE_OK;
00961 #endif
00962 }
00963 
00964 /*
00965 ** Read data from a file into a buffer.  Return SQLITE_OK if all
00966 ** bytes were read successfully and SQLITE_IOERR if anything goes
00967 ** wrong.
00968 */
00969 int sqliteOsRead(OsFile *id, void *pBuf, int amt){
00970 #if OS_UNIX
00971   int got;
00972   SimulateIOError(SQLITE_IOERR);
00973   TIMER_START;
00974   got = read(id->fd, pBuf, amt);
00975   TIMER_END;
00976   TRACE4("READ    %-3d %7d %d\n", id->fd, last_page, elapse);
00977   SEEK(0);
00978   /* if( got<0 ) got = 0; */
00979   if( got==amt ){
00980     return SQLITE_OK;
00981   }else{
00982     return SQLITE_IOERR;
00983   }
00984 #endif
00985 #if OS_WIN
00986   DWORD got;
00987   SimulateIOError(SQLITE_IOERR);
00988   TRACE2("READ %d\n", last_page);
00989   if( !ReadFile(id->h, pBuf, amt, &got, 0) ){
00990     got = 0;
00991   }
00992   if( got==(DWORD)amt ){
00993     return SQLITE_OK;
00994   }else{
00995     return SQLITE_IOERR;
00996   }
00997 #endif
00998 #if OS_MAC
00999   int got;
01000   SimulateIOError(SQLITE_IOERR);
01001   TRACE2("READ %d\n", last_page);
01002 # ifdef _LARGE_FILE
01003   FSReadFork(id->refNum, fsAtMark, 0, (ByteCount)amt, pBuf, (ByteCount*)&got);
01004 # else
01005   got = amt;
01006   FSRead(id->refNum, &got, pBuf);
01007 # endif
01008   if( got==amt ){
01009     return SQLITE_OK;
01010   }else{
01011     return SQLITE_IOERR;
01012   }
01013 #endif
01014 }
01015 
01016 /*
01017 ** Write data from a buffer into a file.  Return SQLITE_OK on success
01018 ** or some other error code on failure.
01019 */
01020 int sqliteOsWrite(OsFile *id, const void *pBuf, int amt){
01021 #if OS_UNIX
01022   int wrote = 0;
01023   SimulateIOError(SQLITE_IOERR);
01024   TIMER_START;
01025   while( amt>0 && (wrote = write(id->fd, pBuf, amt))>0 ){
01026     amt -= wrote;
01027     pBuf = &((char*)pBuf)[wrote];
01028   }
01029   TIMER_END;
01030   TRACE4("WRITE   %-3d %7d %d\n", id->fd, last_page, elapse);
01031   SEEK(0);
01032   if( amt>0 ){
01033     return SQLITE_FULL;
01034   }
01035   return SQLITE_OK;
01036 #endif
01037 #if OS_WIN
01038   int rc;
01039   DWORD wrote;
01040   SimulateIOError(SQLITE_IOERR);
01041   TRACE2("WRITE %d\n", last_page);
01042   while( amt>0 && (rc = WriteFile(id->h, pBuf, amt, &wrote, 0))!=0 && wrote>0 ){
01043     amt -= wrote;
01044     pBuf = &((char*)pBuf)[wrote];
01045   }
01046   if( !rc || amt>(int)wrote ){
01047     return SQLITE_FULL;
01048   }
01049   return SQLITE_OK;
01050 #endif
01051 #if OS_MAC
01052   OSErr oserr;
01053   int wrote = 0;
01054   SimulateIOError(SQLITE_IOERR);
01055   TRACE2("WRITE %d\n", last_page);
01056   while( amt>0 ){
01057 # ifdef _LARGE_FILE
01058     oserr = FSWriteFork(id->refNum, fsAtMark, 0,
01059                         (ByteCount)amt, pBuf, (ByteCount*)&wrote);
01060 # else
01061     wrote = amt;
01062     oserr = FSWrite(id->refNum, &wrote, pBuf);
01063 # endif
01064     if( wrote == 0 || oserr != noErr)
01065       break;
01066     amt -= wrote;
01067     pBuf = &((char*)pBuf)[wrote];
01068   }
01069   if( oserr != noErr || amt>wrote ){
01070     return SQLITE_FULL;
01071   }
01072   return SQLITE_OK;
01073 #endif
01074 }
01075 
01076 /*
01077 ** Move the read/write pointer in a file.
01078 */
01079 int sqliteOsSeek(OsFile *id, off_t offset){
01080   SEEK(offset/1024 + 1);
01081 #if OS_UNIX
01082   lseek(id->fd, offset, SEEK_SET);
01083   return SQLITE_OK;
01084 #endif
01085 #if OS_WIN
01086   {
01087     LONG upperBits = offset>>32;
01088     LONG lowerBits = offset & 0xffffffff;
01089     DWORD rc;
01090     rc = SetFilePointer(id->h, lowerBits, &upperBits, FILE_BEGIN);
01091     /* TRACE3("SEEK rc=0x%x upper=0x%x\n", rc, upperBits); */
01092   }
01093   return SQLITE_OK;
01094 #endif
01095 #if OS_MAC
01096   {
01097     off_t curSize;
01098     if( sqliteOsFileSize(id, &curSize) != SQLITE_OK ){
01099       return SQLITE_IOERR;
01100     }
01101     if( offset >= curSize ){
01102       if( sqliteOsTruncate(id, offset+1) != SQLITE_OK ){
01103         return SQLITE_IOERR;
01104       }
01105     }
01106 # ifdef _LARGE_FILE
01107     if( FSSetForkPosition(id->refNum, fsFromStart, offset) != noErr ){
01108 # else
01109     if( SetFPos(id->refNum, fsFromStart, offset) != noErr ){
01110 # endif
01111       return SQLITE_IOERR;
01112     }else{
01113       return SQLITE_OK;
01114     }
01115   }
01116 #endif
01117 }
01118 
01119 #ifdef SQLITE_NOSYNC
01120 # define fsync(X) 0
01121 #endif
01122 
01123 /*
01124 ** Make sure all writes to a particular file are committed to disk.
01125 **
01126 ** Under Unix, also make sure that the directory entry for the file
01127 ** has been created by fsync-ing the directory that contains the file.
01128 ** If we do not do this and we encounter a power failure, the directory
01129 ** entry for the journal might not exist after we reboot.  The next
01130 ** SQLite to access the file will not know that the journal exists (because
01131 ** the directory entry for the journal was never created) and the transaction
01132 ** will not roll back - possibly leading to database corruption.
01133 */
01134 int sqliteOsSync(OsFile *id){
01135 #if OS_UNIX
01136   SimulateIOError(SQLITE_IOERR);
01137   TRACE2("SYNC    %-3d\n", id->fd);
01138   if( fsync(id->fd) ){
01139     return SQLITE_IOERR;
01140   }else{
01141     if( id->dirfd>=0 ){
01142       TRACE2("DIRSYNC %-3d\n", id->dirfd);
01143       fsync(id->dirfd);
01144       close(id->dirfd);  /* Only need to sync once, so close the directory */
01145       id->dirfd = -1;    /* when we are done. */
01146     }
01147     return SQLITE_OK;
01148   }
01149 #endif
01150 #if OS_WIN
01151   if( FlushFileBuffers(id->h) ){
01152     return SQLITE_OK;
01153   }else{
01154     return SQLITE_IOERR;
01155   }
01156 #endif
01157 #if OS_MAC
01158 # ifdef _LARGE_FILE
01159   if( FSFlushFork(id->refNum) != noErr ){
01160 # else
01161   ParamBlockRec params;
01162   memset(&params, 0, sizeof(ParamBlockRec));
01163   params.ioParam.ioRefNum = id->refNum;
01164   if( PBFlushFileSync(&params) != noErr ){
01165 # endif
01166     return SQLITE_IOERR;
01167   }else{
01168     return SQLITE_OK;
01169   }
01170 #endif
01171 }
01172 
01173 /*
01174 ** Truncate an open file to a specified size
01175 */
01176 int sqliteOsTruncate(OsFile *id, off_t nByte){
01177   SimulateIOError(SQLITE_IOERR);
01178 #if OS_UNIX
01179   return ftruncate(id->fd, nByte)==0 ? SQLITE_OK : SQLITE_IOERR;
01180 #endif
01181 #if OS_WIN
01182   {
01183     LONG upperBits = nByte>>32;
01184     SetFilePointer(id->h, nByte, &upperBits, FILE_BEGIN);
01185     SetEndOfFile(id->h);
01186   }
01187   return SQLITE_OK;
01188 #endif
01189 #if OS_MAC
01190 # ifdef _LARGE_FILE
01191   if( FSSetForkSize(id->refNum, fsFromStart, nByte) != noErr){
01192 # else
01193   if( SetEOF(id->refNum, nByte) != noErr ){
01194 # endif
01195     return SQLITE_IOERR;
01196   }else{
01197     return SQLITE_OK;
01198   }
01199 #endif
01200 }
01201 
01202 /*
01203 ** Determine the current size of a file in bytes
01204 */
01205 int sqliteOsFileSize(OsFile *id, off_t *pSize){
01206 #if OS_UNIX
01207   struct stat buf;
01208   SimulateIOError(SQLITE_IOERR);
01209   if( fstat(id->fd, &buf)!=0 ){
01210     return SQLITE_IOERR;
01211   }
01212   *pSize = buf.st_size;
01213   return SQLITE_OK;
01214 #endif
01215 #if OS_WIN
01216   DWORD upperBits, lowerBits;
01217   SimulateIOError(SQLITE_IOERR);
01218   lowerBits = GetFileSize(id->h, &upperBits);
01219   *pSize = (((off_t)upperBits)<<32) + lowerBits;
01220   return SQLITE_OK;
01221 #endif
01222 #if OS_MAC
01223 # ifdef _LARGE_FILE
01224   if( FSGetForkSize(id->refNum, pSize) != noErr){
01225 # else
01226   if( GetEOF(id->refNum, pSize) != noErr ){
01227 # endif
01228     return SQLITE_IOERR;
01229   }else{
01230     return SQLITE_OK;
01231   }
01232 #endif
01233 }
01234 
01235 #if OS_WIN
01236 /*
01237 ** Return true (non-zero) if we are running under WinNT, Win2K or WinXP.
01238 ** Return false (zero) for Win95, Win98, or WinME.
01239 **
01240 ** Here is an interesting observation:  Win95, Win98, and WinME lack
01241 ** the LockFileEx() API.  But we can still statically link against that
01242 ** API as long as we don't call it win running Win95/98/ME.  A call to
01243 ** this routine is used to determine if the host is Win95/98/ME or
01244 ** WinNT/2K/XP so that we will know whether or not we can safely call
01245 ** the LockFileEx() API.
01246 */
01247 int isNT(void){
01248   static int osType = 0;   /* 0=unknown 1=win95 2=winNT */
01249   if( osType==0 ){
01250     OSVERSIONINFO sInfo;
01251     sInfo.dwOSVersionInfoSize = sizeof(sInfo);
01252     GetVersionEx(&sInfo);
01253     osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
01254   }
01255   return osType==2;
01256 }
01257 #endif
01258 
01259 /*
01260 ** Windows file locking notes:  [similar issues apply to MacOS]
01261 **
01262 ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
01263 ** those functions are not available.  So we use only LockFile() and
01264 ** UnlockFile().
01265 **
01266 ** LockFile() prevents not just writing but also reading by other processes.
01267 ** (This is a design error on the part of Windows, but there is nothing
01268 ** we can do about that.)  So the region used for locking is at the
01269 ** end of the file where it is unlikely to ever interfere with an
01270 ** actual read attempt.
01271 **
01272 ** A database read lock is obtained by locking a single randomly-chosen 
01273 ** byte out of a specific range of bytes. The lock byte is obtained at 
01274 ** random so two separate readers can probably access the file at the 
01275 ** same time, unless they are unlucky and choose the same lock byte.
01276 ** A database write lock is obtained by locking all bytes in the range.
01277 ** There can only be one writer.
01278 **
01279 ** A lock is obtained on the first byte of the lock range before acquiring
01280 ** either a read lock or a write lock.  This prevents two processes from
01281 ** attempting to get a lock at a same time.  The semantics of 
01282 ** sqliteOsReadLock() require that if there is already a write lock, that
01283 ** lock is converted into a read lock atomically.  The lock on the first
01284 ** byte allows us to drop the old write lock and get the read lock without
01285 ** another process jumping into the middle and messing us up.  The same
01286 ** argument applies to sqliteOsWriteLock().
01287 **
01288 ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
01289 ** which means we can use reader/writer locks.  When reader writer locks
01290 ** are used, the lock is placed on the same range of bytes that is used
01291 ** for probabilistic locking in Win95/98/ME.  Hence, the locking scheme
01292 ** will support two or more Win95 readers or two or more WinNT readers.
01293 ** But a single Win95 reader will lock out all WinNT readers and a single
01294 ** WinNT reader will lock out all other Win95 readers.
01295 **
01296 ** Note: On MacOS we use the resource fork for locking.
01297 **
01298 ** The following #defines specify the range of bytes used for locking.
01299 ** N_LOCKBYTE is the number of bytes available for doing the locking.
01300 ** The first byte used to hold the lock while the lock is changing does
01301 ** not count toward this number.  FIRST_LOCKBYTE is the address of
01302 ** the first byte in the range of bytes used for locking.
01303 */
01304 #define N_LOCKBYTE       10239
01305 #if OS_MAC
01306 # define FIRST_LOCKBYTE   (0x000fffff - N_LOCKBYTE)
01307 #else
01308 # define FIRST_LOCKBYTE   (0xffffffff - N_LOCKBYTE)
01309 #endif
01310 
01311 /*
01312 ** Change the status of the lock on the file "id" to be a readlock.
01313 ** If the file was write locked, then this reduces the lock to a read.
01314 ** If the file was read locked, then this acquires a new read lock.
01315 **
01316 ** Return SQLITE_OK on success and SQLITE_BUSY on failure.  If this
01317 ** library was compiled with large file support (LFS) but LFS is not
01318 ** available on the host, then an SQLITE_NOLFS is returned.
01319 */
01320 int sqliteOsReadLock(OsFile *id){
01321 #if OS_UNIX
01322   int rc;
01323   sqliteOsEnterMutex();
01324   if( id->pLock->cnt>0 ){
01325     if( !id->locked ){
01326       id->pLock->cnt++;
01327       id->locked = 1;
01328       id->pOpen->nLock++;
01329     }
01330     rc = SQLITE_OK;
01331   }else if( id->locked || id->pLock->cnt==0 ){
01332     struct flock lock;
01333     int s;
01334     lock.l_type = F_RDLCK;
01335     lock.l_whence = SEEK_SET;
01336     lock.l_start = lock.l_len = 0L;
01337     s = fcntl(id->fd, F_SETLK, &lock);
01338     if( s!=0 ){
01339       rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
01340     }else{
01341       rc = SQLITE_OK;
01342       if( !id->locked ){
01343         id->pOpen->nLock++;
01344         id->locked = 1;
01345       }
01346       id->pLock->cnt = 1;
01347     }
01348   }else{
01349     rc = SQLITE_BUSY;
01350   }
01351   sqliteOsLeaveMutex();
01352   return rc;
01353 #endif
01354 #if OS_WIN
01355   int rc;
01356   if( id->locked>0 ){
01357     rc = SQLITE_OK;
01358   }else{
01359     int lk;
01360     int res;
01361     int cnt = 100;
01362     sqliteRandomness(sizeof(lk), &lk);
01363     lk = (lk & 0x7fffffff)%N_LOCKBYTE + 1;
01364     while( cnt-->0 && (res = LockFile(id->h, FIRST_LOCKBYTE, 0, 1, 0))==0 ){
01365       Sleep(1);
01366     }
01367     if( res ){
01368       UnlockFile(id->h, FIRST_LOCKBYTE+1, 0, N_LOCKBYTE, 0);
01369       if( isNT() ){
01370         OVERLAPPED ovlp;
01371         ovlp.Offset = FIRST_LOCKBYTE+1;
01372         ovlp.OffsetHigh = 0;
01373         ovlp.hEvent = 0;
01374         res = LockFileEx(id->h, LOCKFILE_FAIL_IMMEDIATELY, 
01375                           0, N_LOCKBYTE, 0, &ovlp);
01376       }else{
01377         res = LockFile(id->h, FIRST_LOCKBYTE+lk, 0, 1, 0);
01378       }
01379       UnlockFile(id->h, FIRST_LOCKBYTE, 0, 1, 0);
01380     }
01381     if( res ){
01382       id->locked = lk;
01383       rc = SQLITE_OK;
01384     }else{
01385       rc = SQLITE_BUSY;
01386     }
01387   }
01388   return rc;
01389 #endif
01390 #if OS_MAC
01391   int rc;
01392   if( id->locked>0 || id->refNumRF == -1 ){
01393     rc = SQLITE_OK;
01394   }else{
01395     int lk;
01396     OSErr res;
01397     int cnt = 5;
01398     ParamBlockRec params;
01399     sqliteRandomness(sizeof(lk), &lk);
01400     lk = (lk & 0x7fffffff)%N_LOCKBYTE + 1;
01401     memset(&params, 0, sizeof(params));
01402     params.ioParam.ioRefNum = id->refNumRF;
01403     params.ioParam.ioPosMode = fsFromStart;
01404     params.ioParam.ioPosOffset = FIRST_LOCKBYTE;
01405     params.ioParam.ioReqCount = 1;
01406     while( cnt-->0 && (res = PBLockRangeSync(&params))!=noErr ){
01407       UInt32 finalTicks;
01408       Delay(1, &finalTicks); /* 1/60 sec */
01409     }
01410     if( res == noErr ){
01411       params.ioParam.ioPosOffset = FIRST_LOCKBYTE+1;
01412       params.ioParam.ioReqCount = N_LOCKBYTE;
01413       PBUnlockRangeSync(&params);
01414       params.ioParam.ioPosOffset = FIRST_LOCKBYTE+lk;
01415       params.ioParam.ioReqCount = 1;
01416       res = PBLockRangeSync(&params);
01417       params.ioParam.ioPosOffset = FIRST_LOCKBYTE;
01418       params.ioParam.ioReqCount = 1;
01419       PBUnlockRangeSync(&params);
01420     }
01421     if( res == noErr ){
01422       id->locked = lk;
01423       rc = SQLITE_OK;
01424     }else{
01425       rc = SQLITE_BUSY;
01426     }
01427   }
01428   return rc;
01429 #endif
01430 }
01431 
01432 /*
01433 ** Change the lock status to be an exclusive or write lock.  Return
01434 ** SQLITE_OK on success and SQLITE_BUSY on a failure.  If this
01435 ** library was compiled with large file support (LFS) but LFS is not
01436 ** available on the host, then an SQLITE_NOLFS is returned.
01437 */
01438 int sqliteOsWriteLock(OsFile *id){
01439 #if OS_UNIX
01440   int rc;
01441   sqliteOsEnterMutex();
01442   if( id->pLock->cnt==0 || (id->pLock->cnt==1 && id->locked==1) ){
01443     struct flock lock;
01444     int s;
01445     lock.l_type = F_WRLCK;
01446     lock.l_whence = SEEK_SET;
01447     lock.l_start = lock.l_len = 0L;
01448     s = fcntl(id->fd, F_SETLK, &lock);
01449     if( s!=0 ){
01450       rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
01451     }else{
01452       rc = SQLITE_OK;
01453       if( !id->locked ){
01454         id->pOpen->nLock++;
01455         id->locked = 1;
01456       }
01457       id->pLock->cnt = -1;
01458     }
01459   }else{
01460     rc = SQLITE_BUSY;
01461   }
01462   sqliteOsLeaveMutex();
01463   return rc;
01464 #endif
01465 #if OS_WIN
01466   int rc;
01467   if( id->locked<0 ){
01468     rc = SQLITE_OK;
01469   }else{
01470     int res;
01471     int cnt = 100;
01472     while( cnt-->0 && (res = LockFile(id->h, FIRST_LOCKBYTE, 0, 1, 0))==0 ){
01473       Sleep(1);
01474     }
01475     if( res ){
01476       if( id->locked>0 ){
01477         if( isNT() ){
01478           UnlockFile(id->h, FIRST_LOCKBYTE+1, 0, N_LOCKBYTE, 0);
01479         }else{
01480           res = UnlockFile(id->h, FIRST_LOCKBYTE + id->locked, 0, 1, 0);
01481         }
01482       }
01483       if( res ){
01484         res = LockFile(id->h, FIRST_LOCKBYTE+1, 0, N_LOCKBYTE, 0);
01485       }else{
01486         res = 0;
01487       }
01488       UnlockFile(id->h, FIRST_LOCKBYTE, 0, 1, 0);
01489     }
01490     if( res ){
01491       id->locked = -1;
01492       rc = SQLITE_OK;
01493     }else{
01494       rc = SQLITE_BUSY;
01495     }
01496   }
01497   return rc;
01498 #endif
01499 #if OS_MAC
01500   int rc;
01501   if( id->locked<0 || id->refNumRF == -1 ){
01502     rc = SQLITE_OK;
01503   }else{
01504     OSErr res;
01505     int cnt = 5;
01506     ParamBlockRec params;
01507     memset(&params, 0, sizeof(params));
01508     params.ioParam.ioRefNum = id->refNumRF;
01509     params.ioParam.ioPosMode = fsFromStart;
01510     params.ioParam.ioPosOffset = FIRST_LOCKBYTE;
01511     params.ioParam.ioReqCount = 1;
01512     while( cnt-->0 && (res = PBLockRangeSync(&params))!=noErr ){
01513       UInt32 finalTicks;
01514       Delay(1, &finalTicks); /* 1/60 sec */
01515     }
01516     if( res == noErr ){
01517       params.ioParam.ioPosOffset = FIRST_LOCKBYTE + id->locked;
01518       params.ioParam.ioReqCount = 1;
01519       if( id->locked==0 
01520             || PBUnlockRangeSync(&params)==noErr ){
01521         params.ioParam.ioPosOffset = FIRST_LOCKBYTE+1;
01522         params.ioParam.ioReqCount = N_LOCKBYTE;
01523         res = PBLockRangeSync(&params);
01524       }else{
01525         res = afpRangeNotLocked;
01526       }
01527       params.ioParam.ioPosOffset = FIRST_LOCKBYTE;
01528       params.ioParam.ioReqCount = 1;
01529       PBUnlockRangeSync(&params);
01530     }
01531     if( res == noErr ){
01532       id->locked = -1;
01533       rc = SQLITE_OK;
01534     }else{
01535       rc = SQLITE_BUSY;
01536     }
01537   }
01538   return rc;
01539 #endif
01540 }
01541 
01542 /*
01543 ** Unlock the given file descriptor.  If the file descriptor was
01544 ** not previously locked, then this routine is a no-op.  If this
01545 ** library was compiled with large file support (LFS) but LFS is not
01546 ** available on the host, then an SQLITE_NOLFS is returned.
01547 */
01548 int sqliteOsUnlock(OsFile *id){
01549 #if OS_UNIX
01550   int rc;
01551   if( !id->locked ) return SQLITE_OK;
01552   sqliteOsEnterMutex();
01553   assert( id->pLock->cnt!=0 );
01554   if( id->pLock->cnt>1 ){
01555     id->pLock->cnt--;
01556     rc = SQLITE_OK;
01557   }else{
01558     struct flock lock;
01559     int s;
01560     lock.l_type = F_UNLCK;
01561     lock.l_whence = SEEK_SET;
01562     lock.l_start = lock.l_len = 0L;
01563     s = fcntl(id->fd, F_SETLK, &lock);
01564     if( s!=0 ){
01565       rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
01566     }else{
01567       rc = SQLITE_OK;
01568       id->pLock->cnt = 0;
01569     }
01570   }
01571   if( rc==SQLITE_OK ){
01572     /* Decrement the count of locks against this same file.  When the
01573     ** count reaches zero, close any other file descriptors whose close
01574     ** was deferred because of outstanding locks.
01575     */
01576     struct openCnt *pOpen = id->pOpen;
01577     pOpen->nLock--;
01578     assert( pOpen->nLock>=0 );
01579     if( pOpen->nLock==0 && pOpen->nPending>0 ){
01580       int i;
01581       for(i=0; i<pOpen->nPending; i++){
01582         close(pOpen->aPending[i]);
01583       }
01584       sqliteFree(pOpen->aPending);
01585       pOpen->nPending = 0;
01586       pOpen->aPending = 0;
01587     }
01588   }
01589   sqliteOsLeaveMutex();
01590   id->locked = 0;
01591   return rc;
01592 #endif
01593 #if OS_WIN
01594   int rc;
01595   if( id->locked==0 ){
01596     rc = SQLITE_OK;
01597   }else if( isNT() || id->locked<0 ){
01598     UnlockFile(id->h, FIRST_LOCKBYTE+1, 0, N_LOCKBYTE, 0);
01599     rc = SQLITE_OK;
01600     id->locked = 0;
01601   }else{
01602     UnlockFile(id->h, FIRST_LOCKBYTE+id->locked, 0, 1, 0);
01603     rc = SQLITE_OK;
01604     id->locked = 0;
01605   }
01606   return rc;
01607 #endif
01608 #if OS_MAC
01609   int rc;
01610   ParamBlockRec params;
01611   memset(&params, 0, sizeof(params));
01612   params.ioParam.ioRefNum = id->refNumRF;
01613   params.ioParam.ioPosMode = fsFromStart;
01614   if( id->locked==0 || id->refNumRF == -1 ){
01615     rc = SQLITE_OK;
01616   }else if( id->locked<0 ){
01617     params.ioParam.ioPosOffset = FIRST_LOCKBYTE+1;
01618     params.ioParam.ioReqCount = N_LOCKBYTE;
01619     PBUnlockRangeSync(&params);
01620     rc = SQLITE_OK;
01621     id->locked = 0;
01622   }else{
01623     params.ioParam.ioPosOffset = FIRST_LOCKBYTE+id->locked;
01624     params.ioParam.ioReqCount = 1;
01625     PBUnlockRangeSync(&params);
01626     rc = SQLITE_OK;
01627     id->locked = 0;
01628   }
01629   return rc;
01630 #endif
01631 }
01632 
01633 /*
01634 ** Get information to seed the random number generator.  The seed
01635 ** is written into the buffer zBuf[256].  The calling function must
01636 ** supply a sufficiently large buffer.
01637 */
01638 int sqliteOsRandomSeed(char *zBuf){
01639   /* We have to initialize zBuf to prevent valgrind from reporting
01640   ** errors.  The reports issued by valgrind are incorrect - we would
01641   ** prefer that the randomness be increased by making use of the
01642   ** uninitialized space in zBuf - but valgrind errors tend to worry
01643   ** some users.  Rather than argue, it seems easier just to initialize
01644   ** the whole array and silence valgrind, even if that means less randomness
01645   ** in the random seed.
01646   **
01647   ** When testing, initializing zBuf[] to zero is all we do.  That means
01648   ** that we always use the same random number sequence.* This makes the
01649   ** tests repeatable.
01650   */
01651   memset(zBuf, 0, 256);
01652 #if OS_UNIX && !defined(SQLITE_TEST)
01653   {
01654     int pid;
01655     time((time_t*)zBuf);
01656     pid = getpid();
01657     memcpy(&zBuf[sizeof(time_t)], &pid, sizeof(pid));
01658   }
01659 #endif
01660 #if OS_WIN && !defined(SQLITE_TEST)
01661   GetSystemTime((LPSYSTEMTIME)zBuf);
01662 #endif
01663 #if OS_MAC
01664   {
01665     int pid;
01666     Microseconds((UnsignedWide*)zBuf);
01667     pid = getpid();
01668     memcpy(&zBuf[sizeof(UnsignedWide)], &pid, sizeof(pid));
01669   }
01670 #endif
01671   return SQLITE_OK;
01672 }
01673 
01674 /*
01675 ** Sleep for a little while.  Return the amount of time slept.
01676 */
01677 int sqliteOsSleep(int ms){
01678 #if OS_UNIX
01679 #if defined(HAVE_USLEEP) && HAVE_USLEEP
01680   usleep(ms*1000);
01681   return ms;
01682 #else
01683   sleep((ms+999)/1000);
01684   return 1000*((ms+999)/1000);
01685 #endif
01686 #endif
01687 #if OS_WIN
01688   Sleep(ms);
01689   return ms;
01690 #endif
01691 #if OS_MAC
01692   UInt32 finalTicks;
01693   UInt32 ticks = (((UInt32)ms+16)*3)/50;  /* 1/60 sec per tick */
01694   Delay(ticks, &finalTicks);
01695   return (int)((ticks*50)/3);
01696 #endif
01697 }
01698 
01699 /*
01700 ** Static variables used for thread synchronization
01701 */
01702 static int inMutex = 0;
01703 #ifdef SQLITE_UNIX_THREADS
01704   static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
01705 #endif
01706 #ifdef SQLITE_W32_THREADS
01707   static CRITICAL_SECTION cs;
01708 #endif
01709 #ifdef SQLITE_MACOS_MULTITASKING
01710   static MPCriticalRegionID criticalRegion;
01711 #endif
01712 
01713 /*
01714 ** The following pair of routine implement mutual exclusion for
01715 ** multi-threaded processes.  Only a single thread is allowed to
01716 ** executed code that is surrounded by EnterMutex() and LeaveMutex().
01717 **
01718 ** SQLite uses only a single Mutex.  There is not much critical
01719 ** code and what little there is executes quickly and without blocking.
01720 */
01721 void sqliteOsEnterMutex(){
01722 #ifdef SQLITE_UNIX_THREADS
01723   pthread_mutex_lock(&mutex);
01724 #endif
01725 #ifdef SQLITE_W32_THREADS
01726   static int isInit = 0;
01727   while( !isInit ){
01728     static long lock = 0;
01729     if( InterlockedIncrement(&lock)==1 ){
01730       InitializeCriticalSection(&cs);
01731       isInit = 1;
01732     }else{
01733       Sleep(1);
01734     }
01735   }
01736   EnterCriticalSection(&cs);
01737 #endif
01738 #ifdef SQLITE_MACOS_MULTITASKING
01739   static volatile int notInit = 1;
01740   if( notInit ){
01741     if( notInit == 2 ) /* as close as you can get to thread safe init */
01742       MPYield();
01743     else{
01744       notInit = 2;
01745       MPCreateCriticalRegion(&criticalRegion);
01746       notInit = 0;
01747     }
01748   }
01749   MPEnterCriticalRegion(criticalRegion, kDurationForever);
01750 #endif
01751   assert( !inMutex );
01752   inMutex = 1;
01753 }
01754 void sqliteOsLeaveMutex(){
01755   assert( inMutex );
01756   inMutex = 0;
01757 #ifdef SQLITE_UNIX_THREADS
01758   pthread_mutex_unlock(&mutex);
01759 #endif
01760 #ifdef SQLITE_W32_THREADS
01761   LeaveCriticalSection(&cs);
01762 #endif
01763 #ifdef SQLITE_MACOS_MULTITASKING
01764   MPExitCriticalRegion(criticalRegion);
01765 #endif
01766 }
01767 
01768 /*
01769 ** Turn a relative pathname into a full pathname.  Return a pointer
01770 ** to the full pathname stored in space obtained from sqliteMalloc().
01771 ** The calling function is responsible for freeing this space once it
01772 ** is no longer needed.
01773 */
01774 char *sqliteOsFullPathname(const char *zRelative){
01775 #if OS_UNIX
01776   char *zFull = 0;
01777   if( zRelative[0]=='/' ){
01778     sqliteSetString(&zFull, zRelative, (char*)0);
01779   }else{
01780     char zBuf[5000];
01781     zBuf[0] = 0;
01782     sqliteSetString(&zFull, getcwd(zBuf, sizeof(zBuf)), "/", zRelative,
01783                     (char*)0);
01784   }
01785   return zFull;
01786 #endif
01787 #if OS_WIN
01788   char *zNotUsed;
01789   char *zFull;
01790   int nByte;
01791   nByte = GetFullPathName(zRelative, 0, 0, &zNotUsed) + 1;
01792   zFull = sqliteMalloc( nByte );
01793   if( zFull==0 ) return 0;
01794   GetFullPathName(zRelative, nByte, zFull, &zNotUsed);
01795   return zFull;
01796 #endif
01797 #if OS_MAC
01798   char *zFull = 0;
01799   if( zRelative[0]==':' ){
01800     char zBuf[_MAX_PATH+1];
01801     sqliteSetString(&zFull, getcwd(zBuf, sizeof(zBuf)), &(zRelative[1]),
01802                     (char*)0);
01803   }else{
01804     if( strchr(zRelative, ':') ){
01805       sqliteSetString(&zFull, zRelative, (char*)0);
01806     }else{
01807     char zBuf[_MAX_PATH+1];
01808       sqliteSetString(&zFull, getcwd(zBuf, sizeof(zBuf)), zRelative, (char*)0);
01809     }
01810   }
01811   return zFull;
01812 #endif
01813 }
01814 
01815 /*
01816 ** The following variable, if set to a non-zero value, becomes the result
01817 ** returned from sqliteOsCurrentTime().  This is used for testing.
01818 */
01819 #ifdef SQLITE_TEST
01820 int sqlite_current_time = 0;
01821 #endif
01822 
01823 /*
01824 ** Find the current time (in Universal Coordinated Time).  Write the
01825 ** current time and date as a Julian Day number into *prNow and
01826 ** return 0.  Return 1 if the time and date cannot be found.
01827 */
01828 int sqliteOsCurrentTime(double *prNow){
01829 #if OS_UNIX
01830   time_t t;
01831   time(&t);
01832   *prNow = t/86400.0 + 2440587.5;
01833 #endif
01834 #if OS_WIN
01835   FILETIME ft;
01836   /* FILETIME structure is a 64-bit value representing the number of 
01837      100-nanosecond intervals since January 1, 1601 (= JD 2305813.5). 
01838   */
01839   double now;
01840   GetSystemTimeAsFileTime( &ft );
01841   now = ((double)ft.dwHighDateTime) * 4294967296.0; 
01842   *prNow = (now + ft.dwLowDateTime)/864000000000.0 + 2305813.5;
01843 #endif
01844 #ifdef SQLITE_TEST
01845   if( sqlite_current_time ){
01846     *prNow = sqlite_current_time/86400.0 + 2440587.5;
01847   }
01848 #endif
01849   return 0;
01850 }