os.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
#pragma ident "%Z%%M% %I% %E% SMI"
/*
** 2001 September 16
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
******************************************************************************
**
** This file contains code that is specific to particular operating
** systems. The purpose of this file is to provide a uniform abstraction
** on which the rest of SQLite can operate.
*/
#include "os.h" /* Must be first to enable large file support */
#include "sqliteInt.h"
#if OS_UNIX
# include <time.h>
# include <errno.h>
# include <unistd.h>
# ifndef O_LARGEFILE
# define O_LARGEFILE 0
# endif
# ifdef SQLITE_DISABLE_LFS
# define O_LARGEFILE 0
# endif
# ifndef O_NOFOLLOW
# define O_NOFOLLOW 0
# endif
# ifndef O_BINARY
# define O_BINARY 0
# endif
#endif
#if OS_WIN
# include <winbase.h>
#endif
#if OS_MAC
# include <extras.h>
# include <path2fss.h>
# include <TextUtils.h>
# include <FinderRegistry.h>
# include <Folders.h>
# include <Timer.h>
# include <OSUtils.h>
#endif
/*
** The DJGPP compiler environment looks mostly like Unix, but it
** lacks the fcntl() system call. So redefine fcntl() to be something
** that always succeeds. This means that locking does not occur under
** DJGPP. But its DOS - what did you expect?
*/
#ifdef __DJGPP__
# define fcntl(A,B,C) 0
#endif
/*
** Macros used to determine whether or not to use threads. The
** SQLITE_UNIX_THREADS macro is defined if we are synchronizing for
** Posix threads and SQLITE_W32_THREADS is defined if we are
** synchronizing using Win32 threads.
*/
# include <pthread.h>
# define SQLITE_UNIX_THREADS 1
#endif
# define SQLITE_W32_THREADS 1
#endif
# include <Multiprocessing.h>
# define SQLITE_MACOS_MULTITASKING 1
#endif
/*
** Macros for performance tracing. Normally turned off
*/
#if 0
static int last_page = 0;
__inline__ unsigned long long int hwtime(void){
unsigned long long int x;
__asm__("rdtsc\n\t"
"mov %%edx, %%ecx\n\t"
:"=A" (x));
return x;
}
static unsigned long long int g_start;
static unsigned int elapse;
#else
#define TIMER_START
#define TIMER_END
#define SEEK(X)
#define TRACE1(X)
#define TRACE2(X,Y)
#define TRACE3(X,Y,Z)
#define TRACE4(X,Y,Z,A)
#define TRACE5(X,Y,Z,A,B)
#endif
#if OS_UNIX
/*
** Here is the dirt on POSIX advisory locks: ANSI STD 1003.1 (1996)
** section 6.5.2.2 lines 483 through 490 specify that when a process
** sets or clears a lock, that operation overrides any prior locks set
** by the same process. It does not explicitly say so, but this implies
** that it overrides locks set by the same process using a different
** file descriptor. Consider this test case:
**
** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
**
** Suppose ./file1 and ./file2 are really the same file (because
** one is a hard or symbolic link to the other) then if you set
** an exclusive lock on fd1, then try to get an exclusive lock
** on fd2, it works. I would have expected the second lock to
** fail since there was already a lock on the file due to fd1.
** But not so. Since both locks came from the same process, the
** second overrides the first, even though they were on different
** file descriptors opened on different file names.
**
** Bummer. If you ask me, this is broken. Badly broken. It means
** that we cannot use POSIX locks to synchronize file access among
** competing threads of the same process. POSIX locks will work fine
** to synchronize access for threads in separate processes, but not
** threads within the same process.
**
** To work around the problem, SQLite has to manage file locks internally
** on its own. Whenever a new database is opened, we have to find the
** specific inode of the database file (the inode is determined by the
** st_dev and st_ino fields of the stat structure that fstat() fills in)
** and check for locks already existing on that inode. When locks are
** created or removed, we have to look at our own internal record of the
** locks to see if another thread has previously set a lock on that same
** inode.
**
** The OsFile structure for POSIX is no longer just an integer file
** descriptor. It is now a structure that holds the integer file
** descriptor and a pointer to a structure that describes the internal
** locks on the corresponding inode. There is one locking structure
** per inode, so if the same inode is opened twice, both OsFile structures
** point to the same locking structure. The locking structure keeps
** a reference count (so we will know when to delete it) and a "cnt"
** field that tells us its internal lock status. cnt==0 means the
** file is unlocked. cnt==-1 means the file has an exclusive lock.
** cnt>0 means there are cnt shared locks on the file.
**
** Any attempt to lock or unlock a file first checks the locking
** structure. The fcntl() system call is only invoked to set a
** POSIX lock if the internal lock structure transitions between
** a locked and an unlocked state.
**
** 2004-Jan-11:
** More recent discoveries about POSIX advisory locks. (The more
** I discover, the more I realize the a POSIX advisory locks are
** an abomination.)
**
** If you close a file descriptor that points to a file that has locks,
** all locks on that file that are owned by the current process are
** released. To work around this problem, each OsFile structure contains
** a pointer to an openCnt structure. There is one openCnt structure
** per open inode, which means that multiple OsFiles can point to a single
** openCnt. When an attempt is made to close an OsFile, if there are
** other OsFiles open on the same inode that are holding locks, the call
** to close() the file descriptor is deferred until all of the locks clear.
** The openCnt structure keeps a list of file descriptors that need to
** be closed and that list is walked (and cleared) when the last lock
** clears.
**
** First, under Linux threads, because each thread has a separate
** process ID, lock operations in one thread do not override locks
** to the same file in other threads. Linux threads behave like
** separate processes in this respect. But, if you close a file
** descriptor in linux threads, all locks are cleared, even locks
** on other threads and even though the other threads have different
** process IDs. Linux threads is inconsistent in this respect.
** (I'm beginning to think that linux threads is an abomination too.)
** The consequence of this all is that the hash table for the lockInfo
** structure has to include the process id as part of its key because
** locks in different threads are treated as distinct. But the
** openCnt structure should not include the process id in its
** key because close() clears lock on all threads, not just the current
** thread. Were it not for this goofiness in linux threads, we could
** combine the lockInfo and openCnt structures into a single structure.
*/
/*
** An instance of the following structure serves as the key used
** to locate a particular lockInfo structure given its inode. Note
** that we have to include the process ID as part of the key. On some
** threading implementations (ex: linux), each thread has a separate
** process ID.
*/
struct lockKey {
};
/*
** An instance of the following structure is allocated for each open
** inode on each thread with a different process ID. (Threads have
** different process IDs on linux, but not on most other unixes.)
**
** A single inode can have multiple file descriptors, so each OsFile
** structure contains a pointer to an instance of this object and this
** object keeps a count of the number of OsFiles pointing to it.
*/
struct lockInfo {
int cnt; /* 0: unlocked. -1: write lock. 1...: read lock. */
int nRef; /* Number of pointers to this structure */
};
/*
** An instance of the following structure serves as the key used
** to locate a particular openCnt structure given its inode. This
** is the same as the lockKey except that the process ID is omitted.
*/
struct openKey {
};
/*
** An instance of the following structure is allocated for each open
** inode. This structure keeps track of the number of locks on that
** inode. If a close is attempted against an inode that is holding
** locks, the close is deferred until all locks clear by adding the
** file descriptor to be closed to the pending list.
*/
struct openCnt {
int nRef; /* Number of pointers to this structure */
int nLock; /* Number of outstanding locks */
int nPending; /* Number of pending close() operations */
int *aPending; /* Malloced space holding fd's awaiting a close() */
};
/*
** These hash table maps inodes and process IDs into lockInfo and openCnt
** structures. Access to these hash tables must be protected by a mutex.
*/
/*
** Release a lockInfo structure previously allocated by findLockInfo().
*/
}
}
/*
** Release a openCnt structure previously allocated by findLockInfo().
*/
}
}
/*
** Given a file descriptor, locate lockInfo and openCnt structures that
** describes that file descriptor. Create a new ones if necessary. The
** return values might be unset if an error occurs.
**
** Return the number of errors.
*/
int findLockInfo(
int fd, /* The file descriptor used in the key */
){
int rc;
if( rc!=0 ) return 1;
if( pLock==0 ){
if( pLock==0 ) return 1;
if( pOld!=0 ){
return 1;
}
}else{
}
if( pOpen==0 ){
if( pOpen==0 ){
return 1;
}
if( pOld!=0 ){
return 1;
}
}else{
}
return 0;
}
#endif /** POSIX advisory lock work-around **/
/*
** If we compile with the SQLITE_TEST macro set, then the following block
** of code will give us the ability to simulate a disk I/O error. This
** is used for testing the I/O recovery logic.
*/
#ifdef SQLITE_TEST
int sqlite_io_error_pending = 0;
#define SimulateIOError(A) \
if( sqlite_io_error_pending ) \
static void local_ioerr(){
sqlite_io_error_pending = 0; /* Really just a place to set a breakpoint */
}
#else
#define SimulateIOError(A)
#endif
/*
** When testing, keep a count of the number of open files.
*/
#ifdef SQLITE_TEST
int sqlite_open_file_count = 0;
#define OpenCounter(X) sqlite_open_file_count+=(X)
#else
#define OpenCounter(X)
#endif
/*
** Delete the named file
*/
int sqliteOsDelete(const char *zFilename){
#if OS_UNIX
#endif
#if OS_WIN
#endif
#if OS_MAC
#endif
return SQLITE_OK;
}
/*
** Return TRUE if the named file exists.
*/
int sqliteOsFileExists(const char *zFilename){
#if OS_UNIX
#endif
#if OS_WIN
#endif
#if OS_MAC
#endif
}
#if 0 /* NOT USED */
/*
** Change the name of an existing file.
*/
#if OS_UNIX
return SQLITE_ERROR;
}
return SQLITE_OK;
#endif
#if OS_WIN
return SQLITE_ERROR;
}
return SQLITE_OK;
#endif
#if OS_MAC
/**** FIX ME ***/
return SQLITE_ERROR;
#endif
}
#endif /* NOT USED */
/*
** Attempt to open a file for both reading and writing. If that
** fails, try opening it read-only. If the file does not exist,
** try to create it.
**
** On success, a handle for the open file is written to *id
** and *pReadonly is set to 0 if the file was opened for reading and
** writing or 1 if the file was opened read-only. The function returns
** SQLITE_OK.
**
** On failure, the function returns SQLITE_CANTOPEN and leaves
** *id and *pReadonly unchanged.
*/
const char *zFilename,
int *pReadonly
){
#if OS_UNIX
int rc;
#ifdef EISDIR
return SQLITE_CANTOPEN;
}
#endif
return SQLITE_CANTOPEN;
}
*pReadonly = 1;
}else{
*pReadonly = 0;
}
if( rc ){
return SQLITE_NOMEM;
}
OpenCounter(+1);
return SQLITE_OK;
#endif
#if OS_WIN
NULL,
);
if( h==INVALID_HANDLE_VALUE ){
h = CreateFile(zFilename,
NULL,
);
if( h==INVALID_HANDLE_VALUE ){
return SQLITE_CANTOPEN;
}
*pReadonly = 1;
}else{
*pReadonly = 0;
}
id->h = h;
OpenCounter(+1);
return SQLITE_OK;
#endif
#if OS_MAC
# ifdef _LARGE_FILE
return SQLITE_CANTOPEN;
}
return SQLITE_CANTOPEN;
return SQLITE_CANTOPEN;
else
*pReadonly = 1;
} else
*pReadonly = 0;
} else
*pReadonly = 0;
# else
if( !sqliteOsFileExists(zFilename) ){
return SQLITE_CANTOPEN;
}
return SQLITE_CANTOPEN;
else
*pReadonly = 1;
} else
*pReadonly = 0;
} else
*pReadonly = 0;
# endif
}
id->delOnClose = 0;
OpenCounter(+1);
return SQLITE_OK;
#endif
}
/*
** Attempt to open a new file for exclusive access by this process.
** The file will be opened for both reading and writing. To avoid
** a potential security problem, we do not allow the file to have
** previously existed. Nor do we allow the file to be a symbolic
** link.
**
** If delFlag is true, then make arrangements to automatically delete
** the file when it is closed.
**
** On success, write the file handle into *id and return SQLITE_OK.
**
** On failure, return SQLITE_CANTOPEN.
*/
#if OS_UNIX
int rc;
return SQLITE_CANTOPEN;
}
return SQLITE_CANTOPEN;
}
if( rc ){
return SQLITE_NOMEM;
}
if( delFlag ){
}
OpenCounter(+1);
return SQLITE_OK;
#endif
#if OS_WIN
HANDLE h;
int fileflags;
if( delFlag ){
}else{
}
h = CreateFile(zFilename,
0,
NULL,
);
if( h==INVALID_HANDLE_VALUE ){
return SQLITE_CANTOPEN;
}
id->h = h;
OpenCounter(+1);
return SQLITE_OK;
#endif
#if OS_MAC
# ifdef _LARGE_FILE
return SQLITE_CANTOPEN;
return SQLITE_CANTOPEN;
return SQLITE_CANTOPEN;
# else
return SQLITE_CANTOPEN;
return SQLITE_CANTOPEN;
# endif
if (delFlag)
OpenCounter(+1);
return SQLITE_OK;
#endif
}
/*
** Attempt to open a new file for read-only access.
**
** On success, write the file handle into *id and return SQLITE_OK.
**
** On failure, return SQLITE_CANTOPEN.
*/
#if OS_UNIX
int rc;
return SQLITE_CANTOPEN;
}
if( rc ){
return SQLITE_NOMEM;
}
OpenCounter(+1);
return SQLITE_OK;
#endif
#if OS_WIN
0,
NULL,
);
if( h==INVALID_HANDLE_VALUE ){
return SQLITE_CANTOPEN;
}
id->h = h;
OpenCounter(+1);
return SQLITE_OK;
#endif
#if OS_MAC
# ifdef _LARGE_FILE
return SQLITE_CANTOPEN;
return SQLITE_CANTOPEN;
return SQLITE_CANTOPEN;
# else
return SQLITE_CANTOPEN;
# endif
}
id->delOnClose = 0;
OpenCounter(+1);
return SQLITE_OK;
#endif
}
/*
** Attempt to open a file descriptor for the directory that contains a
** file. This file descriptor can be used to fsync() the directory
** in order to make sure the creation of a new file is actually written
** to disk.
**
** This routine is only meaningful for Unix. It is a no-op under
** windows since windows does not support hard links.
**
** On success, a handle for a previously open file is at *id is
** updated with the new directory file descriptor and SQLITE_OK is
** returned.
**
** On failure, the function returns SQLITE_CANTOPEN and leaves
** *id unchanged.
*/
const char *zDirname,
){
#if OS_UNIX
/* Do not open the directory if the corresponding file is not already
** open. */
return SQLITE_CANTOPEN;
}
return SQLITE_CANTOPEN;
}
#endif
return SQLITE_OK;
}
/*
** If the following global variable points to a string which is the
** name of a directory, then that directory will be used to store
** temporary files.
*/
const char *sqlite_temp_directory = 0;
/*
** Create a temporary file name in zBuf. zBuf must be big enough to
** hold at least SQLITE_TEMPNAME_SIZE characters.
*/
int sqliteOsTempFileName(char *zBuf){
#if OS_UNIX
static const char *azDirs[] = {
0,
"/tmp",
".",
};
static unsigned char zChars[] =
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789";
int i, j;
const char *zDir = ".";
azDirs[0] = sqlite_temp_directory;
if( azDirs[i]==0 ) continue;
break;
}
do{
for(i=0; i<15; i++, j++){
}
zBuf[j] = 0;
#endif
#if OS_WIN
static char zChars[] =
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789";
int i, j;
const char *zDir;
char zTempPath[SQLITE_TEMPNAME_SIZE];
if( sqlite_temp_directory==0 ){
zTempPath[i] = 0;
}else{
}
for(;;){
for(i=0; i<15; i++, j++){
}
zBuf[j] = 0;
if( !sqliteOsFileExists(zBuf) ) break;
}
#endif
#if OS_MAC
static char zChars[] =
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789";
int i, j;
char *zDir;
char zTempPath[SQLITE_TEMPNAME_SIZE];
char zdirName[32];
if( sqlite_temp_directory!=0 ){
do{
zTempPath[i] = ':';
}else{
*zTempPath = 0;
break;
}
}
if( zDir[0]==0 ){
}
for(;;){
for(i=0; i<15; i++, j++){
}
zBuf[j] = 0;
if( !sqliteOsFileExists(zBuf) ) break;
}
#endif
return SQLITE_OK;
}
/*
** Close a file.
*/
#if OS_UNIX
/* If there are outstanding locks, do not actually close the file just
** yet because that would clear those locks. Instead, add the file
** descriptor to pOpen->aPending. It will be automatically closed when
** the last lock is cleared.
*/
int *aNew;
if( aNew==0 ){
/* If a malloc fails, just leak the file descriptor */
}else{
}
}else{
/* There are no outstanding locks so we can close the file immediately */
}
OpenCounter(-1);
return SQLITE_OK;
#endif
#if OS_WIN
CloseHandle(id->h);
OpenCounter(-1);
return SQLITE_OK;
#endif
#if OS_MAC
# ifdef _LARGE_FILE
# else
# endif
if( id->delOnClose ){
}
OpenCounter(-1);
return SQLITE_OK;
#endif
}
/*
** Read data from a file into a buffer. Return SQLITE_OK if all
** bytes were read successfully and SQLITE_IOERR if anything goes
** wrong.
*/
#if OS_UNIX
int got;
SEEK(0);
/* if( got<0 ) got = 0; */
return SQLITE_OK;
}else{
return SQLITE_IOERR;
}
#endif
#if OS_WIN
got = 0;
}
return SQLITE_OK;
}else{
return SQLITE_IOERR;
}
#endif
#if OS_MAC
int got;
# ifdef _LARGE_FILE
# else
# endif
return SQLITE_OK;
}else{
return SQLITE_IOERR;
}
#endif
}
/*
** Write data from a buffer into a file. Return SQLITE_OK on success
** or some other error code on failure.
*/
#if OS_UNIX
int wrote = 0;
}
SEEK(0);
if( amt>0 ){
return SQLITE_FULL;
}
return SQLITE_OK;
#endif
#if OS_WIN
int rc;
}
return SQLITE_FULL;
}
return SQLITE_OK;
#endif
#if OS_MAC
int wrote = 0;
while( amt>0 ){
# ifdef _LARGE_FILE
# else
# endif
break;
}
return SQLITE_FULL;
}
return SQLITE_OK;
#endif
}
/*
*/
#if OS_UNIX
return SQLITE_OK;
#endif
#if OS_WIN
{
/* TRACE3("SEEK rc=0x%x upper=0x%x\n", rc, upperBits); */
}
return SQLITE_OK;
#endif
#if OS_MAC
{
return SQLITE_IOERR;
}
return SQLITE_IOERR;
}
}
# ifdef _LARGE_FILE
# else
# endif
return SQLITE_IOERR;
}else{
return SQLITE_OK;
}
}
#endif
}
/*
** Make sure all writes to a particular file are committed to disk.
**
** Under Unix, also make sure that the directory entry for the file
** has been created by fsync-ing the directory that contains the file.
** If we do not do this and we encounter a power failure, the directory
** entry for the journal might not exist after we reboot. The next
** SQLite to access the file will not know that the journal exists (because
** the directory entry for the journal was never created) and the transaction
** will not roll back - possibly leading to database corruption.
*/
#if OS_UNIX
return SQLITE_IOERR;
}else{
}
return SQLITE_OK;
}
#endif
#if OS_WIN
if( FlushFileBuffers(id->h) ){
return SQLITE_OK;
}else{
return SQLITE_IOERR;
}
#endif
#if OS_MAC
# ifdef _LARGE_FILE
# else
# endif
return SQLITE_IOERR;
}else{
return SQLITE_OK;
}
#endif
}
/*
** Truncate an open file to a specified size
*/
#if OS_UNIX
#endif
#if OS_WIN
{
SetEndOfFile(id->h);
}
return SQLITE_OK;
#endif
#if OS_MAC
# ifdef _LARGE_FILE
# else
# endif
return SQLITE_IOERR;
}else{
return SQLITE_OK;
}
#endif
}
/*
** Determine the current size of a file in bytes
*/
#if OS_UNIX
return SQLITE_IOERR;
}
return SQLITE_OK;
#endif
#if OS_WIN
return SQLITE_OK;
#endif
#if OS_MAC
# ifdef _LARGE_FILE
# else
# endif
return SQLITE_IOERR;
}else{
return SQLITE_OK;
}
#endif
}
#if OS_WIN
/*
** Return true (non-zero) if we are running under WinNT, Win2K or WinXP.
** Return false (zero) for Win95, Win98, or WinME.
**
** Here is an interesting observation: Win95, Win98, and WinME lack
** the LockFileEx() API. But we can still statically link against that
** API as long as we don't call it win running Win95/98/ME. A call to
** this routine is used to determine if the host is Win95/98/ME or
** the LockFileEx() API.
*/
int isNT(void){
static int osType = 0; /* 0=unknown 1=win95 2=winNT */
if( osType==0 ){
}
return osType==2;
}
#endif
/*
** Windows file locking notes: [similar issues apply to MacOS]
**
** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
** those functions are not available. So we use only LockFile() and
** UnlockFile().
**
** LockFile() prevents not just writing but also reading by other processes.
** (This is a design error on the part of Windows, but there is nothing
** we can do about that.) So the region used for locking is at the
** end of the file where it is unlikely to ever interfere with an
** actual read attempt.
**
** A database read lock is obtained by locking a single randomly-chosen
** byte out of a specific range of bytes. The lock byte is obtained at
** random so two separate readers can probably access the file at the
** same time, unless they are unlucky and choose the same lock byte.
** A database write lock is obtained by locking all bytes in the range.
** There can only be one writer.
**
** A lock is obtained on the first byte of the lock range before acquiring
** either a read lock or a write lock. This prevents two processes from
** attempting to get a lock at a same time. The semantics of
** sqliteOsReadLock() require that if there is already a write lock, that
** lock is converted into a read lock atomically. The lock on the first
** byte allows us to drop the old write lock and get the read lock without
** another process jumping into the middle and messing us up. The same
** argument applies to sqliteOsWriteLock().
**
** are used, the lock is placed on the same range of bytes that is used
** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
** will support two or more Win95 readers or two or more WinNT readers.
** But a single Win95 reader will lock out all WinNT readers and a single
** WinNT reader will lock out all other Win95 readers.
**
** Note: On MacOS we use the resource fork for locking.
**
** The following #defines specify the range of bytes used for locking.
** N_LOCKBYTE is the number of bytes available for doing the locking.
** The first byte used to hold the lock while the lock is changing does
** not count toward this number. FIRST_LOCKBYTE is the address of
** the first byte in the range of bytes used for locking.
*/
#define N_LOCKBYTE 10239
#if OS_MAC
#else
#endif
/*
** Change the status of the lock on the file "id" to be a readlock.
** If the file was write locked, then this reduces the lock to a read.
** If the file was read locked, then this acquires a new read lock.
**
** Return SQLITE_OK on success and SQLITE_BUSY on failure. If this
** library was compiled with large file support (LFS) but LFS is not
** available on the host, then an SQLITE_NOLFS is returned.
*/
#if OS_UNIX
int rc;
}
int s;
if( s!=0 ){
}else{
}
}
}else{
rc = SQLITE_BUSY;
}
return rc;
#endif
#if OS_WIN
int rc;
}else{
int lk;
int res;
int cnt = 100;
Sleep(1);
}
if( res ){
if( isNT() ){
ovlp.OffsetHigh = 0;
0, N_LOCKBYTE, 0, &ovlp);
}else{
}
}
if( res ){
}else{
rc = SQLITE_BUSY;
}
}
return rc;
#endif
#if OS_MAC
int rc;
}else{
int lk;
int cnt = 5;
}
}
}else{
rc = SQLITE_BUSY;
}
}
return rc;
#endif
}
/*
** Change the lock status to be an exclusive or write lock. Return
** SQLITE_OK on success and SQLITE_BUSY on a failure. If this
** library was compiled with large file support (LFS) but LFS is not
** available on the host, then an SQLITE_NOLFS is returned.
*/
#if OS_UNIX
int rc;
int s;
if( s!=0 ){
}else{
}
}
}else{
rc = SQLITE_BUSY;
}
return rc;
#endif
#if OS_WIN
int rc;
}else{
int res;
int cnt = 100;
Sleep(1);
}
if( res ){
if( isNT() ){
}else{
}
}
if( res ){
}else{
res = 0;
}
}
if( res ){
}else{
rc = SQLITE_BUSY;
}
}
return rc;
#endif
#if OS_MAC
int rc;
}else{
int cnt = 5;
}
}else{
}
}
}else{
rc = SQLITE_BUSY;
}
}
return rc;
#endif
}
/*
** Unlock the given file descriptor. If the file descriptor was
** not previously locked, then this routine is a no-op. If this
** library was compiled with large file support (LFS) but LFS is not
** available on the host, then an SQLITE_NOLFS is returned.
*/
#if OS_UNIX
int rc;
}else{
int s;
if( s!=0 ){
}else{
}
}
/* Decrement the count of locks against this same file. When the
** count reaches zero, close any other file descriptors whose close
** was deferred because of outstanding locks.
*/
int i;
}
}
}
return rc;
#endif
#if OS_WIN
int rc;
}else{
}
return rc;
#endif
#if OS_MAC
int rc;
}else{
}
return rc;
#endif
}
/*
** Get information to seed the random number generator. The seed
** is written into the buffer zBuf[256]. The calling function must
** supply a sufficiently large buffer.
*/
int sqliteOsRandomSeed(char *zBuf){
/* We have to initialize zBuf to prevent valgrind from reporting
** errors. The reports issued by valgrind are incorrect - we would
** prefer that the randomness be increased by making use of the
** uninitialized space in zBuf - but valgrind errors tend to worry
** some users. Rather than argue, it seems easier just to initialize
** the whole array and silence valgrind, even if that means less randomness
** in the random seed.
**
** When testing, initializing zBuf[] to zero is all we do. That means
** that we always use the same random number sequence.* This makes the
** tests repeatable.
*/
#if OS_UNIX && !defined(SQLITE_TEST)
{
int pid;
}
#endif
#if OS_WIN && !defined(SQLITE_TEST)
#endif
#if OS_MAC
{
int pid;
}
#endif
return SQLITE_OK;
}
/*
** Sleep for a little while. Return the amount of time slept.
*/
int sqliteOsSleep(int ms){
#if OS_UNIX
#if defined(HAVE_USLEEP) && HAVE_USLEEP
return ms;
#else
#endif
#endif
#if OS_WIN
return ms;
#endif
#if OS_MAC
#endif
}
/*
** Static variables used for thread synchronization
*/
static int inMutex = 0;
#ifdef SQLITE_UNIX_THREADS
#endif
#ifdef SQLITE_W32_THREADS
static CRITICAL_SECTION cs;
#endif
#ifdef SQLITE_MACOS_MULTITASKING
static MPCriticalRegionID criticalRegion;
#endif
/*
** The following pair of routine implement mutual exclusion for
** multi-threaded processes. Only a single thread is allowed to
** executed code that is surrounded by EnterMutex() and LeaveMutex().
**
** SQLite uses only a single Mutex. There is not much critical
** code and what little there is executes quickly and without blocking.
*/
void sqliteOsEnterMutex(){
#ifdef SQLITE_UNIX_THREADS
#endif
#ifdef SQLITE_W32_THREADS
static int isInit = 0;
while( !isInit ){
static long lock = 0;
isInit = 1;
}else{
Sleep(1);
}
}
#endif
#ifdef SQLITE_MACOS_MULTITASKING
static volatile int notInit = 1;
if( notInit ){
MPYield();
else{
notInit = 2;
notInit = 0;
}
}
#endif
inMutex = 1;
}
void sqliteOsLeaveMutex(){
inMutex = 0;
#ifdef SQLITE_UNIX_THREADS
#endif
#ifdef SQLITE_W32_THREADS
#endif
#ifdef SQLITE_MACOS_MULTITASKING
#endif
}
/*
** Turn a relative pathname into a full pathname. Return a pointer
** to the full pathname stored in space obtained from sqliteMalloc().
** The calling function is responsible for freeing this space once it
** is no longer needed.
*/
char *sqliteOsFullPathname(const char *zRelative){
#if OS_UNIX
char *zFull = 0;
if( zRelative[0]=='/' ){
}else{
char zBuf[5000];
(char*)0);
}
return zFull;
#endif
#if OS_WIN
char *zNotUsed;
char *zFull;
int nByte;
if( zFull==0 ) return 0;
return zFull;
#endif
#if OS_MAC
char *zFull = 0;
if( zRelative[0]==':' ){
(char*)0);
}else{
}else{
}
}
return zFull;
#endif
}
/*
** The following variable, if set to a non-zero value, becomes the result
** returned from sqliteOsCurrentTime(). This is used for testing.
*/
#ifdef SQLITE_TEST
int sqlite_current_time = 0;
#endif
/*
** Find the current time (in Universal Coordinated Time). Write the
** current time and date as a Julian Day number into *prNow and
** return 0. Return 1 if the time and date cannot be found.
*/
int sqliteOsCurrentTime(double *prNow){
#if OS_UNIX
time_t t;
time(&t);
#endif
#if OS_WIN
/* FILETIME structure is a 64-bit value representing the number of
100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
*/
double now;
#endif
#ifdef SQLITE_TEST
if( sqlite_current_time ){
}
#endif
return 0;
}