2001-09-18 00:25:58 +04:00
|
|
|
/*
|
2004-05-22 21:41:58 +04:00
|
|
|
** 2004 May 22
|
2001-09-18 00:25:58 +04:00
|
|
|
**
|
|
|
|
** 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.
|
|
|
|
**
|
|
|
|
******************************************************************************
|
|
|
|
**
|
2004-05-22 21:41:58 +04:00
|
|
|
** This file contains code that is specific to Unix systems.
|
2008-05-16 08:51:54 +04:00
|
|
|
**
|
2008-09-15 08:20:31 +04:00
|
|
|
** $Id: os_unix.c,v 1.201 2008/09/15 04:20:32 danielk1977 Exp $
|
2001-09-18 00:25:58 +04:00
|
|
|
*/
|
|
|
|
#include "sqliteInt.h"
|
2008-06-26 14:41:19 +04:00
|
|
|
#if SQLITE_OS_UNIX /* This file is used on unix only */
|
2006-01-06 17:32:19 +03:00
|
|
|
|
2008-06-28 15:23:00 +04:00
|
|
|
/*
|
|
|
|
** If SQLITE_ENABLE_LOCKING_STYLE is defined, then several different
|
|
|
|
** locking implementations are provided:
|
|
|
|
**
|
|
|
|
** * POSIX locking (the default),
|
|
|
|
** * No locking,
|
|
|
|
** * Dot-file locking,
|
|
|
|
** * flock() locking,
|
|
|
|
** * AFP locking (OSX only).
|
|
|
|
*/
|
2006-10-03 21:40:40 +04:00
|
|
|
/* #define SQLITE_ENABLE_LOCKING_STYLE 0 */
|
|
|
|
|
2005-11-29 06:13:21 +03:00
|
|
|
/*
|
|
|
|
** These #defines should enable >2GB file support on Posix if the
|
|
|
|
** underlying operating system supports it. If the OS lacks
|
2006-01-15 20:27:17 +03:00
|
|
|
** large file support, these should be no-ops.
|
2005-11-29 06:13:21 +03:00
|
|
|
**
|
|
|
|
** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
|
|
|
|
** on the compiler command line. This is necessary if you are compiling
|
|
|
|
** on a recent machine (ex: RedHat 7.2) but you want your code to work
|
|
|
|
** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2
|
|
|
|
** without this option, LFS is enable. But LFS does not exist in the kernel
|
|
|
|
** in RedHat 6.0, so the code won't work. Hence, for maximum binary
|
|
|
|
** portability you should omit LFS.
|
|
|
|
*/
|
|
|
|
#ifndef SQLITE_DISABLE_LFS
|
|
|
|
# define _LARGE_FILE 1
|
|
|
|
# ifndef _FILE_OFFSET_BITS
|
|
|
|
# define _FILE_OFFSET_BITS 64
|
|
|
|
# endif
|
|
|
|
# define _LARGEFILE_SOURCE 1
|
|
|
|
#endif
|
2001-09-18 00:25:58 +04:00
|
|
|
|
2005-11-29 06:13:21 +03:00
|
|
|
/*
|
|
|
|
** standard include files.
|
|
|
|
*/
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2004-05-22 21:41:58 +04:00
|
|
|
#include <time.h>
|
2005-08-30 03:00:03 +04:00
|
|
|
#include <sys/time.h>
|
2004-05-22 21:41:58 +04:00
|
|
|
#include <errno.h>
|
2008-06-28 15:23:00 +04:00
|
|
|
|
2006-10-03 21:40:40 +04:00
|
|
|
#ifdef SQLITE_ENABLE_LOCKING_STYLE
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#endif /* SQLITE_ENABLE_LOCKING_STYLE */
|
2005-11-29 06:13:21 +03:00
|
|
|
|
|
|
|
/*
|
2006-01-15 20:27:17 +03:00
|
|
|
** If we are to be thread-safe, include the pthreads header and define
|
|
|
|
** the SQLITE_UNIX_THREADS macro.
|
2005-11-29 06:13:21 +03:00
|
|
|
*/
|
2007-08-21 02:48:41 +04:00
|
|
|
#if SQLITE_THREADSAFE
|
2005-11-29 06:13:21 +03:00
|
|
|
# include <pthread.h>
|
|
|
|
# define SQLITE_UNIX_THREADS 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Default permissions when creating a new file
|
|
|
|
*/
|
|
|
|
#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
|
|
|
|
# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
|
|
|
|
#endif
|
|
|
|
|
2007-08-17 19:53:36 +04:00
|
|
|
/*
|
|
|
|
** Maximum supported path-length.
|
|
|
|
*/
|
|
|
|
#define MAX_PATHNAME 512
|
2005-11-29 06:13:21 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2007-08-20 10:44:22 +04:00
|
|
|
** The unixFile structure is subclass of sqlite3_file specific for the unix
|
2005-11-30 06:20:31 +03:00
|
|
|
** protability layer.
|
2005-11-29 06:13:21 +03:00
|
|
|
*/
|
2005-11-30 06:20:31 +03:00
|
|
|
typedef struct unixFile unixFile;
|
|
|
|
struct unixFile {
|
2007-08-15 21:08:46 +04:00
|
|
|
sqlite3_io_methods const *pMethod; /* Always the first entry */
|
2007-08-20 18:23:44 +04:00
|
|
|
#ifdef SQLITE_TEST
|
|
|
|
/* In test mode, increase the size of this structure a bit so that
|
|
|
|
** it is larger than the struct CrashFile defined in test6.c.
|
|
|
|
*/
|
|
|
|
char aPadding[32];
|
|
|
|
#endif
|
2005-11-29 06:13:21 +03:00
|
|
|
struct openCnt *pOpen; /* Info about all open fd's on this inode */
|
|
|
|
struct lockInfo *pLock; /* Info about locks on this inode */
|
2006-10-03 21:40:40 +04:00
|
|
|
#ifdef SQLITE_ENABLE_LOCKING_STYLE
|
|
|
|
void *lockingContext; /* Locking style specific state */
|
2008-06-28 15:23:00 +04:00
|
|
|
#endif
|
2005-11-29 06:13:21 +03:00
|
|
|
int h; /* The file descriptor */
|
|
|
|
unsigned char locktype; /* The type of lock held on this fd */
|
|
|
|
int dirfd; /* File descriptor for the directory */
|
2007-08-21 02:48:41 +04:00
|
|
|
#if SQLITE_THREADSAFE
|
2007-08-20 10:44:22 +04:00
|
|
|
pthread_t tid; /* The thread that "owns" this unixFile */
|
2005-11-29 06:13:21 +03:00
|
|
|
#endif
|
2008-08-22 04:22:35 +04:00
|
|
|
int lastErrno; /* The unix errno from the last I/O error */
|
2005-11-29 06:13:21 +03:00
|
|
|
};
|
|
|
|
|
2006-01-07 00:52:49 +03:00
|
|
|
/*
|
|
|
|
** Include code that is common to all os_*.c files
|
|
|
|
*/
|
|
|
|
#include "os_common.h"
|
|
|
|
|
2005-06-08 02:22:50 +04:00
|
|
|
/*
|
|
|
|
** Define various macros that are missing from some systems.
|
|
|
|
*/
|
2004-05-22 21:41:58 +04:00
|
|
|
#ifndef O_LARGEFILE
|
|
|
|
# define O_LARGEFILE 0
|
2001-09-18 00:25:58 +04:00
|
|
|
#endif
|
2004-05-22 21:41:58 +04:00
|
|
|
#ifdef SQLITE_DISABLE_LFS
|
|
|
|
# undef O_LARGEFILE
|
|
|
|
# define O_LARGEFILE 0
|
|
|
|
#endif
|
|
|
|
#ifndef O_NOFOLLOW
|
|
|
|
# define O_NOFOLLOW 0
|
|
|
|
#endif
|
|
|
|
#ifndef O_BINARY
|
|
|
|
# define O_BINARY 0
|
2003-01-08 16:02:52 +03:00
|
|
|
#endif
|
|
|
|
|
2003-04-17 06:54:13 +04:00
|
|
|
/*
|
|
|
|
** 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
|
2007-12-14 00:54:09 +03:00
|
|
|
** DJGPP. But it is DOS - what did you expect?
|
2003-04-17 06:54:13 +04:00
|
|
|
*/
|
|
|
|
#ifdef __DJGPP__
|
|
|
|
# define fcntl(A,B,C) 0
|
|
|
|
#endif
|
|
|
|
|
2002-03-05 04:11:12 +03:00
|
|
|
/*
|
2005-06-15 21:47:55 +04:00
|
|
|
** The threadid macro resolves to the thread-id or to 0. Used for
|
|
|
|
** testing and debugging only.
|
2002-03-05 04:11:12 +03:00
|
|
|
*/
|
2007-08-21 02:48:41 +04:00
|
|
|
#if SQLITE_THREADSAFE
|
2005-06-15 21:47:55 +04:00
|
|
|
#define threadid pthread_self()
|
|
|
|
#else
|
|
|
|
#define threadid 0
|
|
|
|
#endif
|
2002-03-05 04:11:12 +03:00
|
|
|
|
2005-06-15 21:47:55 +04:00
|
|
|
/*
|
2007-08-20 10:44:22 +04:00
|
|
|
** Set or check the unixFile.tid field. This field is set when an unixFile
|
|
|
|
** is first opened. All subsequent uses of the unixFile verify that the
|
|
|
|
** same thread is operating on the unixFile. Some operating systems do
|
2005-06-15 21:47:55 +04:00
|
|
|
** not allow locks to be overridden by other threads and that restriction
|
|
|
|
** means that sqlite3* database handles cannot be moved from one thread
|
|
|
|
** to another. This logic makes sure a user does not try to do that
|
|
|
|
** by mistake.
|
2006-01-15 20:27:17 +03:00
|
|
|
**
|
2007-08-20 10:44:22 +04:00
|
|
|
** Version 3.3.1 (2006-01-15): unixFile can be moved from one thread to
|
2006-01-15 20:27:17 +03:00
|
|
|
** another as long as we are running on a system that supports threads
|
|
|
|
** overriding each others locks (which now the most common behavior)
|
2007-08-20 10:44:22 +04:00
|
|
|
** or if no locks are held. But the unixFile.pLock field needs to be
|
2006-01-15 20:27:17 +03:00
|
|
|
** recomputed because its key includes the thread-id. See the
|
|
|
|
** transferOwnership() function below for additional information
|
2005-06-15 21:47:55 +04:00
|
|
|
*/
|
2007-08-21 02:48:41 +04:00
|
|
|
#if SQLITE_THREADSAFE
|
2005-11-29 06:13:21 +03:00
|
|
|
# define SET_THREADID(X) (X)->tid = pthread_self()
|
2006-01-15 03:13:15 +03:00
|
|
|
# define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
|
|
|
|
!pthread_equal((X)->tid, pthread_self()))
|
2005-06-15 21:47:55 +04:00
|
|
|
#else
|
|
|
|
# define SET_THREADID(X)
|
|
|
|
# define CHECK_THREADID(X) 0
|
2004-06-03 20:08:41 +04:00
|
|
|
#endif
|
|
|
|
|
2001-10-09 16:39:24 +04:00
|
|
|
/*
|
|
|
|
** 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 fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
|
|
|
|
**
|
2003-06-28 20:20:22 +04:00
|
|
|
** Suppose ./file1 and ./file2 are really the same file (because
|
2001-10-09 16:39:24 +04:00
|
|
|
** 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
|
2001-10-15 04:44:35 +04:00
|
|
|
** st_dev and st_ino fields of the stat structure that fstat() fills in)
|
2001-10-09 16:39:24 +04:00
|
|
|
** 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.
|
|
|
|
**
|
2007-08-20 10:44:22 +04:00
|
|
|
** The sqlite3_file structure for POSIX is no longer just an integer file
|
2001-10-09 16:39:24 +04:00
|
|
|
** 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
|
2007-08-20 10:44:22 +04:00
|
|
|
** per inode, so if the same inode is opened twice, both unixFile structures
|
2001-10-09 16:39:24 +04:00
|
|
|
** 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
|
2002-10-20 22:19:45 +04:00
|
|
|
** structure. The fcntl() system call is only invoked to set a
|
2001-10-09 16:39:24 +04:00
|
|
|
** POSIX lock if the internal lock structure transitions between
|
|
|
|
** a locked and an unlocked state.
|
2004-01-12 03:39:05 +03:00
|
|
|
**
|
|
|
|
** 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
|
2007-08-20 10:44:22 +04:00
|
|
|
** released. To work around this problem, each unixFile structure contains
|
2004-01-12 03:39:05 +03:00
|
|
|
** a pointer to an openCnt structure. There is one openCnt structure
|
2007-08-20 10:44:22 +04:00
|
|
|
** per open inode, which means that multiple unixFile can point to a single
|
|
|
|
** openCnt. When an attempt is made to close an unixFile, if there are
|
|
|
|
** other unixFile open on the same inode that are holding locks, the call
|
2004-01-12 03:39:05 +03:00
|
|
|
** 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.
|
2004-06-29 07:29:00 +04:00
|
|
|
**
|
|
|
|
** 2004-Jun-28:
|
|
|
|
** On some versions of linux, threads can override each others locks.
|
|
|
|
** On others not. Sometimes you can change the behavior on the same
|
|
|
|
** system by setting the LD_ASSUME_KERNEL environment variable. The
|
|
|
|
** POSIX standard is silent as to which behavior is correct, as far
|
|
|
|
** as I can tell, so other versions of unix might show the same
|
|
|
|
** inconsistency. There is no little doubt in my mind that posix
|
|
|
|
** advisory locks and linux threads are profoundly broken.
|
|
|
|
**
|
|
|
|
** To work around the inconsistencies, we have to test at runtime
|
|
|
|
** whether or not threads can override each others locks. This test
|
|
|
|
** is run once, the first time any lock is attempted. A static
|
|
|
|
** variable is set to record the results of this test for future
|
|
|
|
** use.
|
2001-10-09 16:39:24 +04:00
|
|
|
*/
|
|
|
|
|
2001-10-09 08:19:46 +04:00
|
|
|
/*
|
|
|
|
** An instance of the following structure serves as the key used
|
2004-06-29 07:29:00 +04:00
|
|
|
** to locate a particular lockInfo structure given its inode.
|
|
|
|
**
|
|
|
|
** If threads cannot override each others locks, then we set the
|
|
|
|
** lockKey.tid field to the thread ID. If threads can override
|
2006-01-15 20:27:17 +03:00
|
|
|
** each others locks then tid is always set to zero. tid is omitted
|
|
|
|
** if we compile without threading support.
|
2001-10-09 08:19:46 +04:00
|
|
|
*/
|
2003-12-19 11:40:22 +03:00
|
|
|
struct lockKey {
|
2004-06-29 07:29:00 +04:00
|
|
|
dev_t dev; /* Device number */
|
|
|
|
ino_t ino; /* Inode number */
|
2007-08-21 02:48:41 +04:00
|
|
|
#if SQLITE_THREADSAFE
|
2005-10-20 11:28:17 +04:00
|
|
|
pthread_t tid; /* Thread ID or zero if threads can override each other */
|
2004-06-29 07:29:00 +04:00
|
|
|
#endif
|
2001-10-09 08:19:46 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2004-01-12 03:39:05 +03:00
|
|
|
** 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.)
|
|
|
|
**
|
2007-08-20 10:44:22 +04:00
|
|
|
** A single inode can have multiple file descriptors, so each unixFile
|
2001-10-09 16:39:24 +04:00
|
|
|
** structure contains a pointer to an instance of this object and this
|
2007-08-20 10:44:22 +04:00
|
|
|
** object keeps a count of the number of unixFile pointing to it.
|
2001-10-09 08:19:46 +04:00
|
|
|
*/
|
|
|
|
struct lockInfo {
|
2003-12-19 11:40:22 +03:00
|
|
|
struct lockKey key; /* The lookup key */
|
2004-06-07 20:27:46 +04:00
|
|
|
int cnt; /* Number of SHARED locks held */
|
2004-06-01 18:09:28 +04:00
|
|
|
int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
|
2004-01-12 03:39:05 +03:00
|
|
|
int nRef; /* Number of pointers to this structure */
|
2008-07-30 19:27:54 +04:00
|
|
|
struct lockInfo *pNext, *pPrev; /* List of all lockInfo objects */
|
2004-01-12 03:39:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** An instance of the following structure serves as the key used
|
|
|
|
** to locate a particular openCnt structure given its inode. This
|
2004-06-29 07:29:00 +04:00
|
|
|
** is the same as the lockKey except that the thread ID is omitted.
|
2004-01-12 03:39:05 +03:00
|
|
|
*/
|
|
|
|
struct openKey {
|
|
|
|
dev_t dev; /* Device number */
|
|
|
|
ino_t ino; /* Inode number */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** 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 {
|
|
|
|
struct openKey key; /* The lookup key */
|
2001-10-09 08:19:46 +04:00
|
|
|
int nRef; /* Number of pointers to this structure */
|
2004-01-12 03:39:05 +03:00
|
|
|
int nLock; /* Number of outstanding locks */
|
|
|
|
int nPending; /* Number of pending close() operations */
|
|
|
|
int *aPending; /* Malloced space holding fd's awaiting a close() */
|
2008-07-30 19:27:54 +04:00
|
|
|
struct openCnt *pNext, *pPrev; /* List of all openCnt objects */
|
2001-10-09 08:19:46 +04:00
|
|
|
};
|
|
|
|
|
2008-07-30 19:27:54 +04:00
|
|
|
/*
|
|
|
|
** List of all lockInfo and openCnt objects. This used to be a hash
|
|
|
|
** table. But the number of objects is rarely more than a dozen and
|
|
|
|
** never exceeds a few thousand. And lookup is not on a critical
|
|
|
|
** path oo a simple linked list will suffice.
|
2001-10-09 08:19:46 +04:00
|
|
|
*/
|
2008-07-30 19:27:54 +04:00
|
|
|
static struct lockInfo *lockList = 0;
|
|
|
|
static struct openCnt *openList = 0;
|
2004-06-29 07:29:00 +04:00
|
|
|
|
2006-10-03 21:40:40 +04:00
|
|
|
/*
|
|
|
|
** The locking styles are associated with the different file locking
|
|
|
|
** capabilities supported by different file systems.
|
|
|
|
**
|
|
|
|
** POSIX locking style fully supports shared and exclusive byte-range locks
|
2008-06-28 15:23:00 +04:00
|
|
|
** AFP locking only supports exclusive byte-range locks
|
2006-10-03 21:40:40 +04:00
|
|
|
** FLOCK only supports a single file-global exclusive lock
|
|
|
|
** DOTLOCK isn't a true locking style, it refers to the use of a special
|
|
|
|
** file named the same as the database file with a '.lock' extension, this
|
|
|
|
** can be used on file systems that do not offer any reliable file locking
|
|
|
|
** NO locking means that no locking will be attempted, this is only used for
|
|
|
|
** read-only file systems currently
|
|
|
|
** UNSUPPORTED means that no locking will be attempted, this is only used for
|
|
|
|
** file systems that are known to be unsupported
|
|
|
|
*/
|
2008-06-28 15:23:00 +04:00
|
|
|
#define LOCKING_STYLE_POSIX 1
|
2008-07-30 19:27:54 +04:00
|
|
|
#define LOCKING_STYLE_NONE 2
|
2008-06-28 15:23:00 +04:00
|
|
|
#define LOCKING_STYLE_DOTFILE 3
|
2008-07-30 19:27:54 +04:00
|
|
|
#define LOCKING_STYLE_FLOCK 4
|
2008-06-28 15:23:00 +04:00
|
|
|
#define LOCKING_STYLE_AFP 5
|
2006-10-03 21:40:40 +04:00
|
|
|
|
2008-08-22 04:22:35 +04:00
|
|
|
/*
|
|
|
|
** Only set the lastErrno if the error code is a real error and not
|
|
|
|
** a normal expected return code of SQLITE_BUSY or SQLITE_OK
|
|
|
|
*/
|
|
|
|
#define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY))
|
|
|
|
|
2007-08-20 10:44:22 +04:00
|
|
|
/*
|
|
|
|
** Helper functions to obtain and relinquish the global mutex.
|
|
|
|
*/
|
2007-08-17 19:53:36 +04:00
|
|
|
static void enterMutex(){
|
2008-06-18 21:09:10 +04:00
|
|
|
sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
|
2007-08-17 19:53:36 +04:00
|
|
|
}
|
|
|
|
static void leaveMutex(){
|
2008-06-18 21:09:10 +04:00
|
|
|
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
|
2007-08-17 19:53:36 +04:00
|
|
|
}
|
|
|
|
|
2007-08-21 02:48:41 +04:00
|
|
|
#if SQLITE_THREADSAFE
|
2004-06-29 07:29:00 +04:00
|
|
|
/*
|
|
|
|
** This variable records whether or not threads can override each others
|
|
|
|
** locks.
|
|
|
|
**
|
|
|
|
** 0: No. Threads cannot override each others locks.
|
|
|
|
** 1: Yes. Threads can override each others locks.
|
|
|
|
** -1: We don't know yet.
|
2006-01-15 20:27:17 +03:00
|
|
|
**
|
2006-02-01 02:03:35 +03:00
|
|
|
** On some systems, we know at compile-time if threads can override each
|
|
|
|
** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro
|
|
|
|
** will be set appropriately. On other systems, we have to check at
|
|
|
|
** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is
|
|
|
|
** undefined.
|
|
|
|
**
|
2006-01-15 20:27:17 +03:00
|
|
|
** This variable normally has file scope only. But during testing, we make
|
|
|
|
** it a global so that the test code can change its value in order to verify
|
|
|
|
** that the right stuff happens in either case.
|
2004-06-29 07:29:00 +04:00
|
|
|
*/
|
2006-02-01 02:03:35 +03:00
|
|
|
#ifndef SQLITE_THREAD_OVERRIDE_LOCK
|
|
|
|
# define SQLITE_THREAD_OVERRIDE_LOCK -1
|
|
|
|
#endif
|
2006-01-15 03:13:15 +03:00
|
|
|
#ifdef SQLITE_TEST
|
2006-02-01 02:03:35 +03:00
|
|
|
int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
|
2006-01-15 03:13:15 +03:00
|
|
|
#else
|
2006-02-01 02:03:35 +03:00
|
|
|
static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
|
2006-01-15 03:13:15 +03:00
|
|
|
#endif
|
2004-06-29 07:29:00 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** This structure holds information passed into individual test
|
|
|
|
** threads by the testThreadLockingBehavior() routine.
|
|
|
|
*/
|
|
|
|
struct threadTestData {
|
|
|
|
int fd; /* File to be locked */
|
|
|
|
struct flock lock; /* The locking operation */
|
|
|
|
int result; /* Result of the locking operation */
|
|
|
|
};
|
|
|
|
|
2005-06-15 21:47:55 +04:00
|
|
|
#ifdef SQLITE_LOCK_TRACE
|
|
|
|
/*
|
|
|
|
** Print out information about all locking operations.
|
|
|
|
**
|
|
|
|
** This routine is used for troubleshooting locks on multithreaded
|
|
|
|
** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
|
|
|
|
** command-line option on the compiler. This code is normally
|
2006-01-15 20:27:17 +03:00
|
|
|
** turned off.
|
2005-06-15 21:47:55 +04:00
|
|
|
*/
|
|
|
|
static int lockTrace(int fd, int op, struct flock *p){
|
|
|
|
char *zOpName, *zType;
|
|
|
|
int s;
|
|
|
|
int savedErrno;
|
|
|
|
if( op==F_GETLK ){
|
|
|
|
zOpName = "GETLK";
|
|
|
|
}else if( op==F_SETLK ){
|
|
|
|
zOpName = "SETLK";
|
|
|
|
}else{
|
|
|
|
s = fcntl(fd, op, p);
|
|
|
|
sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
if( p->l_type==F_RDLCK ){
|
|
|
|
zType = "RDLCK";
|
|
|
|
}else if( p->l_type==F_WRLCK ){
|
|
|
|
zType = "WRLCK";
|
|
|
|
}else if( p->l_type==F_UNLCK ){
|
|
|
|
zType = "UNLCK";
|
|
|
|
}else{
|
|
|
|
assert( 0 );
|
|
|
|
}
|
|
|
|
assert( p->l_whence==SEEK_SET );
|
|
|
|
s = fcntl(fd, op, p);
|
|
|
|
savedErrno = errno;
|
|
|
|
sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
|
|
|
|
threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
|
|
|
|
(int)p->l_pid, s);
|
2007-03-30 00:19:58 +04:00
|
|
|
if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
|
2005-06-15 21:47:55 +04:00
|
|
|
struct flock l2;
|
|
|
|
l2 = *p;
|
|
|
|
fcntl(fd, F_GETLK, &l2);
|
|
|
|
if( l2.l_type==F_RDLCK ){
|
|
|
|
zType = "RDLCK";
|
|
|
|
}else if( l2.l_type==F_WRLCK ){
|
|
|
|
zType = "WRLCK";
|
|
|
|
}else if( l2.l_type==F_UNLCK ){
|
|
|
|
zType = "UNLCK";
|
|
|
|
}else{
|
|
|
|
assert( 0 );
|
|
|
|
}
|
|
|
|
sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
|
|
|
|
zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
|
|
|
|
}
|
|
|
|
errno = savedErrno;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
#define fcntl lockTrace
|
|
|
|
#endif /* SQLITE_LOCK_TRACE */
|
|
|
|
|
2004-06-29 07:29:00 +04:00
|
|
|
/*
|
|
|
|
** The testThreadLockingBehavior() routine launches two separate
|
|
|
|
** threads on this routine. This routine attempts to lock a file
|
|
|
|
** descriptor then returns. The success or failure of that attempt
|
|
|
|
** allows the testThreadLockingBehavior() procedure to determine
|
|
|
|
** whether or not threads can override each others locks.
|
|
|
|
*/
|
|
|
|
static void *threadLockingTest(void *pArg){
|
|
|
|
struct threadTestData *pData = (struct threadTestData*)pArg;
|
|
|
|
pData->result = fcntl(pData->fd, F_SETLK, &pData->lock);
|
|
|
|
return pArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This procedure attempts to determine whether or not threads
|
|
|
|
** can override each others locks then sets the
|
|
|
|
** threadsOverrideEachOthersLocks variable appropriately.
|
|
|
|
*/
|
2006-01-27 09:32:00 +03:00
|
|
|
static void testThreadLockingBehavior(int fd_orig){
|
2004-06-29 07:29:00 +04:00
|
|
|
int fd;
|
|
|
|
struct threadTestData d[2];
|
|
|
|
pthread_t t[2];
|
|
|
|
|
|
|
|
fd = dup(fd_orig);
|
|
|
|
if( fd<0 ) return;
|
|
|
|
memset(d, 0, sizeof(d));
|
|
|
|
d[0].fd = fd;
|
|
|
|
d[0].lock.l_type = F_RDLCK;
|
|
|
|
d[0].lock.l_len = 1;
|
|
|
|
d[0].lock.l_start = 0;
|
|
|
|
d[0].lock.l_whence = SEEK_SET;
|
|
|
|
d[1] = d[0];
|
|
|
|
d[1].lock.l_type = F_WRLCK;
|
|
|
|
pthread_create(&t[0], 0, threadLockingTest, &d[0]);
|
|
|
|
pthread_create(&t[1], 0, threadLockingTest, &d[1]);
|
|
|
|
pthread_join(t[0], 0);
|
|
|
|
pthread_join(t[1], 0);
|
|
|
|
close(fd);
|
|
|
|
threadsOverrideEachOthersLocks = d[0].result==0 && d[1].result==0;
|
|
|
|
}
|
2007-08-21 02:48:41 +04:00
|
|
|
#endif /* SQLITE_THREADSAFE */
|
2004-06-29 07:29:00 +04:00
|
|
|
|
2004-01-12 03:39:05 +03:00
|
|
|
/*
|
|
|
|
** Release a lockInfo structure previously allocated by findLockInfo().
|
|
|
|
*/
|
|
|
|
static void releaseLockInfo(struct lockInfo *pLock){
|
2008-06-28 15:23:00 +04:00
|
|
|
if( pLock ){
|
|
|
|
pLock->nRef--;
|
|
|
|
if( pLock->nRef==0 ){
|
2008-07-30 19:27:54 +04:00
|
|
|
if( pLock->pPrev ){
|
|
|
|
assert( pLock->pPrev->pNext==pLock );
|
|
|
|
pLock->pPrev->pNext = pLock->pNext;
|
|
|
|
}else{
|
|
|
|
assert( lockList==pLock );
|
|
|
|
lockList = pLock->pNext;
|
|
|
|
}
|
|
|
|
if( pLock->pNext ){
|
|
|
|
assert( pLock->pNext->pPrev==pLock );
|
|
|
|
pLock->pNext->pPrev = pLock->pPrev;
|
|
|
|
}
|
2008-06-28 15:23:00 +04:00
|
|
|
sqlite3_free(pLock);
|
|
|
|
}
|
2004-01-12 03:39:05 +03:00
|
|
|
}
|
|
|
|
}
|
2001-10-09 08:19:46 +04:00
|
|
|
|
|
|
|
/*
|
2004-01-12 03:39:05 +03:00
|
|
|
** Release a openCnt structure previously allocated by findLockInfo().
|
|
|
|
*/
|
|
|
|
static void releaseOpenCnt(struct openCnt *pOpen){
|
2008-06-28 15:23:00 +04:00
|
|
|
if( pOpen ){
|
|
|
|
pOpen->nRef--;
|
|
|
|
if( pOpen->nRef==0 ){
|
2008-07-30 19:27:54 +04:00
|
|
|
if( pOpen->pPrev ){
|
|
|
|
assert( pOpen->pPrev->pNext==pOpen );
|
|
|
|
pOpen->pPrev->pNext = pOpen->pNext;
|
|
|
|
}else{
|
|
|
|
assert( openList==pOpen );
|
|
|
|
openList = pOpen->pNext;
|
|
|
|
}
|
|
|
|
if( pOpen->pNext ){
|
|
|
|
assert( pOpen->pNext->pPrev==pOpen );
|
|
|
|
pOpen->pNext->pPrev = pOpen->pPrev;
|
|
|
|
}
|
|
|
|
sqlite3_free(pOpen->aPending);
|
2008-06-28 15:23:00 +04:00
|
|
|
sqlite3_free(pOpen);
|
|
|
|
}
|
2004-01-12 03:39:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-10 04:32:42 +04:00
|
|
|
#ifdef SQLITE_ENABLE_LOCKING_STYLE
|
2006-10-03 21:40:40 +04:00
|
|
|
/*
|
|
|
|
** Tests a byte-range locking query to see if byte range locks are
|
|
|
|
** supported, if not we fall back to dotlockLockingStyle.
|
|
|
|
*/
|
2008-06-28 15:23:00 +04:00
|
|
|
static int testLockingStyle(int fd){
|
2006-10-03 21:40:40 +04:00
|
|
|
struct flock lockInfo;
|
2008-06-28 15:23:00 +04:00
|
|
|
|
|
|
|
/* Test byte-range lock using fcntl(). If the call succeeds,
|
|
|
|
** assume that the file-system supports POSIX style locks.
|
|
|
|
*/
|
2006-10-03 21:40:40 +04:00
|
|
|
lockInfo.l_len = 1;
|
|
|
|
lockInfo.l_start = 0;
|
|
|
|
lockInfo.l_whence = SEEK_SET;
|
|
|
|
lockInfo.l_type = F_RDLCK;
|
2007-08-20 10:44:22 +04:00
|
|
|
if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
|
2008-06-28 15:23:00 +04:00
|
|
|
return LOCKING_STYLE_POSIX;
|
|
|
|
}
|
2006-10-03 21:40:40 +04:00
|
|
|
|
2008-06-28 15:23:00 +04:00
|
|
|
/* Testing for flock() can give false positives. So if if the above
|
|
|
|
** test fails, then we fall back to using dot-file style locking.
|
2006-10-03 21:40:40 +04:00
|
|
|
*/
|
2008-06-28 15:23:00 +04:00
|
|
|
return LOCKING_STYLE_DOTFILE;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
2008-07-10 04:32:42 +04:00
|
|
|
#endif
|
2006-10-03 21:40:40 +04:00
|
|
|
|
|
|
|
/*
|
2008-06-28 15:23:00 +04:00
|
|
|
** If SQLITE_ENABLE_LOCKING_STYLE is defined, this function Examines the
|
|
|
|
** f_fstypename entry in the statfs structure as returned by stat() for
|
|
|
|
** the file system hosting the database file and selects the appropriate
|
|
|
|
** locking style based on its value. These values and assignments are
|
|
|
|
** based on Darwin/OSX behavior and have not been thoroughly tested on
|
2006-10-03 21:40:40 +04:00
|
|
|
** other systems.
|
2008-06-28 15:23:00 +04:00
|
|
|
**
|
|
|
|
** If SQLITE_ENABLE_LOCKING_STYLE is not defined, this function always
|
|
|
|
** returns LOCKING_STYLE_POSIX.
|
2006-10-03 21:40:40 +04:00
|
|
|
*/
|
2008-06-28 15:23:00 +04:00
|
|
|
static int detectLockingStyle(
|
|
|
|
sqlite3_vfs *pVfs,
|
2007-08-20 10:44:22 +04:00
|
|
|
const char *filePath,
|
|
|
|
int fd
|
|
|
|
){
|
2008-06-28 15:23:00 +04:00
|
|
|
#ifdef SQLITE_ENABLE_LOCKING_STYLE
|
|
|
|
struct Mapping {
|
|
|
|
const char *zFilesystem;
|
|
|
|
int eLockingStyle;
|
|
|
|
} aMap[] = {
|
|
|
|
{ "hfs", LOCKING_STYLE_POSIX },
|
|
|
|
{ "ufs", LOCKING_STYLE_POSIX },
|
|
|
|
{ "afpfs", LOCKING_STYLE_AFP },
|
2008-08-22 04:22:35 +04:00
|
|
|
#ifdef SQLITE_ENABLE_AFP_LOCKING_SMB
|
|
|
|
{ "smbfs", LOCKING_STYLE_AFP },
|
|
|
|
#else
|
2008-06-28 15:23:00 +04:00
|
|
|
{ "smbfs", LOCKING_STYLE_FLOCK },
|
2008-08-22 04:22:35 +04:00
|
|
|
#endif
|
2008-06-28 15:23:00 +04:00
|
|
|
{ "msdos", LOCKING_STYLE_DOTFILE },
|
|
|
|
{ "webdav", LOCKING_STYLE_NONE },
|
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
int i;
|
2006-10-03 21:40:40 +04:00
|
|
|
struct statfs fsInfo;
|
|
|
|
|
2008-06-28 15:23:00 +04:00
|
|
|
if( !filePath ){
|
|
|
|
return LOCKING_STYLE_NONE;
|
2008-03-07 18:34:11 +03:00
|
|
|
}
|
2008-06-28 15:23:00 +04:00
|
|
|
if( pVfs->pAppData ){
|
2008-08-22 22:41:37 +04:00
|
|
|
return SQLITE_PTR_TO_INT(pVfs->pAppData);
|
2008-03-07 18:34:11 +03:00
|
|
|
}
|
2008-06-28 15:23:00 +04:00
|
|
|
|
|
|
|
if( statfs(filePath, &fsInfo) != -1 ){
|
|
|
|
if( fsInfo.f_flags & MNT_RDONLY ){
|
|
|
|
return LOCKING_STYLE_NONE;
|
|
|
|
}
|
|
|
|
for(i=0; aMap[i].zFilesystem; i++){
|
|
|
|
if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
|
|
|
|
return aMap[i].eLockingStyle;
|
|
|
|
}
|
|
|
|
}
|
2008-03-07 18:34:11 +03:00
|
|
|
}
|
2006-10-03 21:40:40 +04:00
|
|
|
|
2008-06-28 15:23:00 +04:00
|
|
|
/* Default case. Handles, amongst others, "nfs". */
|
|
|
|
return testLockingStyle(fd);
|
|
|
|
#endif
|
|
|
|
return LOCKING_STYLE_POSIX;
|
|
|
|
}
|
2006-10-03 21:40:40 +04:00
|
|
|
|
2004-01-12 03:39:05 +03:00
|
|
|
/*
|
|
|
|
** Given a file descriptor, locate lockInfo and openCnt structures that
|
2006-01-15 03:13:15 +03:00
|
|
|
** describes that file descriptor. Create new ones if necessary. The
|
|
|
|
** return values might be uninitialized if an error occurs.
|
2004-01-12 03:39:05 +03:00
|
|
|
**
|
2008-05-05 20:56:34 +04:00
|
|
|
** Return an appropriate error code.
|
2001-10-09 08:19:46 +04:00
|
|
|
*/
|
2004-06-18 21:10:16 +04:00
|
|
|
static int findLockInfo(
|
2004-01-12 03:39:05 +03:00
|
|
|
int fd, /* The file descriptor used in the key */
|
|
|
|
struct lockInfo **ppLock, /* Return the lockInfo structure here */
|
2004-06-29 07:29:00 +04:00
|
|
|
struct openCnt **ppOpen /* Return the openCnt structure here */
|
2004-01-12 03:39:05 +03:00
|
|
|
){
|
2001-10-09 08:19:46 +04:00
|
|
|
int rc;
|
2004-01-12 03:39:05 +03:00
|
|
|
struct lockKey key1;
|
|
|
|
struct openKey key2;
|
2001-10-09 08:19:46 +04:00
|
|
|
struct stat statbuf;
|
2004-01-12 03:39:05 +03:00
|
|
|
struct lockInfo *pLock;
|
|
|
|
struct openCnt *pOpen;
|
2001-10-09 08:19:46 +04:00
|
|
|
rc = fstat(fd, &statbuf);
|
2008-05-05 20:56:34 +04:00
|
|
|
if( rc!=0 ){
|
|
|
|
#ifdef EOVERFLOW
|
|
|
|
if( errno==EOVERFLOW ) return SQLITE_NOLFS;
|
|
|
|
#endif
|
|
|
|
return SQLITE_IOERR;
|
|
|
|
}
|
2006-01-05 16:48:29 +03:00
|
|
|
|
2008-07-30 21:28:04 +04:00
|
|
|
/* On OS X on an msdos filesystem, the inode number is reported
|
|
|
|
** incorrectly for zero-size files. See ticket #3260. To work
|
|
|
|
** around this problem (we consider it a bug in OS X, not SQLite)
|
|
|
|
** we always increase the file size to 1 by writing a single byte
|
|
|
|
** prior to accessing the inode number. The one byte written is
|
|
|
|
** an ASCII 'S' character which also happens to be the first byte
|
|
|
|
** in the header of every SQLite database. In this way, if there
|
|
|
|
** is a race condition such that another thread has already populated
|
|
|
|
** the first page of the database, no damage is done.
|
|
|
|
*/
|
|
|
|
if( statbuf.st_size==0 ){
|
|
|
|
write(fd, "S", 1);
|
|
|
|
rc = fstat(fd, &statbuf);
|
|
|
|
if( rc!=0 ){
|
|
|
|
return SQLITE_IOERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-12 03:39:05 +03:00
|
|
|
memset(&key1, 0, sizeof(key1));
|
|
|
|
key1.dev = statbuf.st_dev;
|
|
|
|
key1.ino = statbuf.st_ino;
|
2007-08-21 02:48:41 +04:00
|
|
|
#if SQLITE_THREADSAFE
|
2004-06-29 07:29:00 +04:00
|
|
|
if( threadsOverrideEachOthersLocks<0 ){
|
|
|
|
testThreadLockingBehavior(fd);
|
|
|
|
}
|
|
|
|
key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
|
|
|
|
#endif
|
2004-01-12 03:39:05 +03:00
|
|
|
memset(&key2, 0, sizeof(key2));
|
|
|
|
key2.dev = statbuf.st_dev;
|
|
|
|
key2.ino = statbuf.st_ino;
|
2008-07-30 19:27:54 +04:00
|
|
|
pLock = lockList;
|
|
|
|
while( pLock && memcmp(&key1, &pLock->key, sizeof(key1)) ){
|
|
|
|
pLock = pLock->pNext;
|
|
|
|
}
|
2004-01-12 03:39:05 +03:00
|
|
|
if( pLock==0 ){
|
2007-08-16 08:30:38 +04:00
|
|
|
pLock = sqlite3_malloc( sizeof(*pLock) );
|
2006-01-05 16:48:29 +03:00
|
|
|
if( pLock==0 ){
|
2008-05-05 20:56:34 +04:00
|
|
|
rc = SQLITE_NOMEM;
|
2006-01-05 16:48:29 +03:00
|
|
|
goto exit_findlockinfo;
|
|
|
|
}
|
2004-01-12 03:39:05 +03:00
|
|
|
pLock->key = key1;
|
|
|
|
pLock->nRef = 1;
|
|
|
|
pLock->cnt = 0;
|
2004-06-01 18:09:28 +04:00
|
|
|
pLock->locktype = 0;
|
2008-07-30 19:27:54 +04:00
|
|
|
pLock->pNext = lockList;
|
|
|
|
pLock->pPrev = 0;
|
|
|
|
if( lockList ) lockList->pPrev = pLock;
|
|
|
|
lockList = pLock;
|
2001-10-09 08:19:46 +04:00
|
|
|
}else{
|
2004-01-12 03:39:05 +03:00
|
|
|
pLock->nRef++;
|
2001-10-09 08:19:46 +04:00
|
|
|
}
|
2004-01-12 03:39:05 +03:00
|
|
|
*ppLock = pLock;
|
2006-01-15 03:13:15 +03:00
|
|
|
if( ppOpen!=0 ){
|
2008-07-30 19:27:54 +04:00
|
|
|
pOpen = openList;
|
|
|
|
while( pOpen && memcmp(&key2, &pOpen->key, sizeof(key2)) ){
|
|
|
|
pOpen = pOpen->pNext;
|
|
|
|
}
|
2004-01-12 03:39:05 +03:00
|
|
|
if( pOpen==0 ){
|
2007-08-16 08:30:38 +04:00
|
|
|
pOpen = sqlite3_malloc( sizeof(*pOpen) );
|
2006-01-15 03:13:15 +03:00
|
|
|
if( pOpen==0 ){
|
|
|
|
releaseLockInfo(pLock);
|
2008-05-05 20:56:34 +04:00
|
|
|
rc = SQLITE_NOMEM;
|
2006-01-15 03:13:15 +03:00
|
|
|
goto exit_findlockinfo;
|
|
|
|
}
|
|
|
|
pOpen->key = key2;
|
|
|
|
pOpen->nRef = 1;
|
|
|
|
pOpen->nLock = 0;
|
|
|
|
pOpen->nPending = 0;
|
|
|
|
pOpen->aPending = 0;
|
2008-07-30 19:27:54 +04:00
|
|
|
pOpen->pNext = openList;
|
|
|
|
pOpen->pPrev = 0;
|
|
|
|
if( openList ) openList->pPrev = pOpen;
|
|
|
|
openList = pOpen;
|
2006-01-15 03:13:15 +03:00
|
|
|
}else{
|
|
|
|
pOpen->nRef++;
|
2004-01-12 03:39:05 +03:00
|
|
|
}
|
2006-01-15 03:13:15 +03:00
|
|
|
*ppOpen = pOpen;
|
2001-10-09 08:19:46 +04:00
|
|
|
}
|
2006-01-05 16:48:29 +03:00
|
|
|
|
|
|
|
exit_findlockinfo:
|
|
|
|
return rc;
|
2001-10-09 08:19:46 +04:00
|
|
|
}
|
2004-01-12 03:39:05 +03:00
|
|
|
|
2006-01-15 05:30:57 +03:00
|
|
|
#ifdef SQLITE_DEBUG
|
|
|
|
/*
|
|
|
|
** Helper function for printing out trace information from debugging
|
|
|
|
** binaries. This returns the string represetation of the supplied
|
|
|
|
** integer lock-type.
|
|
|
|
*/
|
|
|
|
static const char *locktypeName(int locktype){
|
|
|
|
switch( locktype ){
|
|
|
|
case NO_LOCK: return "NONE";
|
|
|
|
case SHARED_LOCK: return "SHARED";
|
|
|
|
case RESERVED_LOCK: return "RESERVED";
|
|
|
|
case PENDING_LOCK: return "PENDING";
|
|
|
|
case EXCLUSIVE_LOCK: return "EXCLUSIVE";
|
|
|
|
}
|
|
|
|
return "ERROR";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-01-15 03:13:15 +03:00
|
|
|
/*
|
|
|
|
** If we are currently in a different thread than the thread that the
|
|
|
|
** unixFile argument belongs to, then transfer ownership of the unixFile
|
|
|
|
** over to the current thread.
|
|
|
|
**
|
|
|
|
** A unixFile is only owned by a thread on systems where one thread is
|
|
|
|
** unable to override locks created by a different thread. RedHat9 is
|
|
|
|
** an example of such a system.
|
|
|
|
**
|
|
|
|
** Ownership transfer is only allowed if the unixFile is currently unlocked.
|
|
|
|
** If the unixFile is locked and an ownership is wrong, then return
|
2006-01-15 20:27:17 +03:00
|
|
|
** SQLITE_MISUSE. SQLITE_OK is returned if everything works.
|
2006-01-15 03:13:15 +03:00
|
|
|
*/
|
2007-08-21 02:48:41 +04:00
|
|
|
#if SQLITE_THREADSAFE
|
2006-01-15 03:13:15 +03:00
|
|
|
static int transferOwnership(unixFile *pFile){
|
2006-01-15 05:30:57 +03:00
|
|
|
int rc;
|
2006-01-15 03:13:15 +03:00
|
|
|
pthread_t hSelf;
|
|
|
|
if( threadsOverrideEachOthersLocks ){
|
|
|
|
/* Ownership transfers not needed on this system */
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
hSelf = pthread_self();
|
|
|
|
if( pthread_equal(pFile->tid, hSelf) ){
|
|
|
|
/* We are still in the same thread */
|
2007-03-27 02:05:01 +04:00
|
|
|
OSTRACE1("No-transfer, same thread\n");
|
2006-01-15 03:13:15 +03:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
if( pFile->locktype!=NO_LOCK ){
|
|
|
|
/* We cannot change ownership while we are holding a lock! */
|
|
|
|
return SQLITE_MISUSE;
|
|
|
|
}
|
2007-03-27 02:05:01 +04:00
|
|
|
OSTRACE4("Transfer ownership of %d from %d to %d\n",
|
|
|
|
pFile->h, pFile->tid, hSelf);
|
2006-01-15 03:13:15 +03:00
|
|
|
pFile->tid = hSelf;
|
2006-10-03 21:40:40 +04:00
|
|
|
if (pFile->pLock != NULL) {
|
|
|
|
releaseLockInfo(pFile->pLock);
|
|
|
|
rc = findLockInfo(pFile->h, &pFile->pLock, 0);
|
2007-03-27 02:05:01 +04:00
|
|
|
OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
|
2006-10-03 21:40:40 +04:00
|
|
|
locktypeName(pFile->locktype),
|
|
|
|
locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
|
|
|
|
return rc;
|
|
|
|
} else {
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
2006-01-15 03:13:15 +03:00
|
|
|
}
|
|
|
|
#else
|
2006-01-15 20:27:17 +03:00
|
|
|
/* On single-threaded builds, ownership transfer is a no-op */
|
2006-01-15 03:13:15 +03:00
|
|
|
# define transferOwnership(X) SQLITE_OK
|
|
|
|
#endif
|
|
|
|
|
2006-03-24 01:42:20 +03:00
|
|
|
/*
|
2007-08-20 20:07:00 +04:00
|
|
|
** Seek to the offset passed as the second argument, then read cnt
|
|
|
|
** bytes into pBuf. Return the number of bytes actually read.
|
2007-10-23 19:59:18 +04:00
|
|
|
**
|
|
|
|
** NB: If you define USE_PREAD or USE_PREAD64, then it might also
|
|
|
|
** be necessary to define _XOPEN_SOURCE to be 500. This varies from
|
|
|
|
** one system to another. Since SQLite does not define USE_PREAD
|
|
|
|
** any any form by default, we will not attempt to define _XOPEN_SOURCE.
|
|
|
|
** See tickets #2741 and #2681.
|
2006-03-24 01:42:20 +03:00
|
|
|
*/
|
2007-08-15 21:08:46 +04:00
|
|
|
static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
|
2006-03-24 01:42:20 +03:00
|
|
|
int got;
|
2007-02-06 14:11:08 +03:00
|
|
|
i64 newOffset;
|
2007-02-27 05:01:14 +03:00
|
|
|
TIMER_START;
|
2007-03-22 18:22:06 +03:00
|
|
|
#if defined(USE_PREAD)
|
2007-08-15 21:08:46 +04:00
|
|
|
got = pread(id->h, pBuf, cnt, offset);
|
2007-04-06 22:23:17 +04:00
|
|
|
SimulateIOError( got = -1 );
|
2007-03-22 18:22:06 +03:00
|
|
|
#elif defined(USE_PREAD64)
|
2007-08-15 21:08:46 +04:00
|
|
|
got = pread64(id->h, pBuf, cnt, offset);
|
2007-04-06 22:23:17 +04:00
|
|
|
SimulateIOError( got = -1 );
|
2006-03-24 01:42:20 +03:00
|
|
|
#else
|
2007-08-15 21:08:46 +04:00
|
|
|
newOffset = lseek(id->h, offset, SEEK_SET);
|
2007-04-06 22:23:17 +04:00
|
|
|
SimulateIOError( newOffset-- );
|
2007-08-15 21:08:46 +04:00
|
|
|
if( newOffset!=offset ){
|
2007-02-06 14:11:08 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2006-03-24 01:42:20 +03:00
|
|
|
got = read(id->h, pBuf, cnt);
|
|
|
|
#endif
|
2007-02-27 05:01:14 +03:00
|
|
|
TIMER_END;
|
2008-05-30 00:22:37 +04:00
|
|
|
OSTRACE5("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
|
2006-03-24 01:42:20 +03:00
|
|
|
return got;
|
|
|
|
}
|
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
2001-11-22 03:01:27 +03:00
|
|
|
** Read data from a file into a buffer. Return SQLITE_OK if all
|
|
|
|
** bytes were read successfully and SQLITE_IOERR if anything goes
|
|
|
|
** wrong.
|
2001-09-18 00:25:58 +04:00
|
|
|
*/
|
2007-08-15 21:08:46 +04:00
|
|
|
static int unixRead(
|
|
|
|
sqlite3_file *id,
|
|
|
|
void *pBuf,
|
|
|
|
int amt,
|
|
|
|
sqlite3_int64 offset
|
|
|
|
){
|
2001-09-19 17:22:39 +04:00
|
|
|
int got;
|
2005-11-29 06:13:21 +03:00
|
|
|
assert( id );
|
2007-08-15 21:08:46 +04:00
|
|
|
got = seekAndRead((unixFile*)id, offset, pBuf, amt);
|
2002-09-05 20:08:27 +04:00
|
|
|
if( got==amt ){
|
|
|
|
return SQLITE_OK;
|
2006-09-15 11:28:50 +04:00
|
|
|
}else if( got<0 ){
|
|
|
|
return SQLITE_IOERR_READ;
|
2002-09-05 20:08:27 +04:00
|
|
|
}else{
|
2007-01-04 02:36:22 +03:00
|
|
|
memset(&((char*)pBuf)[got], 0, amt-got);
|
2006-09-15 11:28:50 +04:00
|
|
|
return SQLITE_IOERR_SHORT_READ;
|
2002-09-05 20:08:27 +04:00
|
|
|
}
|
2001-09-19 17:22:39 +04:00
|
|
|
}
|
2001-09-18 00:25:58 +04:00
|
|
|
|
2006-03-24 01:42:20 +03:00
|
|
|
/*
|
|
|
|
** Seek to the offset in id->offset then read cnt bytes into pBuf.
|
|
|
|
** Return the number of bytes actually read. Update the offset.
|
|
|
|
*/
|
2007-08-15 21:08:46 +04:00
|
|
|
static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
|
2006-03-24 01:42:20 +03:00
|
|
|
int got;
|
2007-02-06 14:11:08 +03:00
|
|
|
i64 newOffset;
|
2007-02-27 05:01:14 +03:00
|
|
|
TIMER_START;
|
2007-03-22 18:22:06 +03:00
|
|
|
#if defined(USE_PREAD)
|
2007-08-15 21:08:46 +04:00
|
|
|
got = pwrite(id->h, pBuf, cnt, offset);
|
2007-03-22 18:22:06 +03:00
|
|
|
#elif defined(USE_PREAD64)
|
2007-08-15 21:08:46 +04:00
|
|
|
got = pwrite64(id->h, pBuf, cnt, offset);
|
2006-03-24 01:42:20 +03:00
|
|
|
#else
|
2007-08-15 21:08:46 +04:00
|
|
|
newOffset = lseek(id->h, offset, SEEK_SET);
|
|
|
|
if( newOffset!=offset ){
|
2007-02-06 14:11:08 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2006-03-24 01:42:20 +03:00
|
|
|
got = write(id->h, pBuf, cnt);
|
|
|
|
#endif
|
2007-02-27 05:01:14 +03:00
|
|
|
TIMER_END;
|
2008-05-30 00:22:37 +04:00
|
|
|
OSTRACE5("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
|
2006-03-24 01:42:20 +03:00
|
|
|
return got;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
2001-09-19 17:22:39 +04:00
|
|
|
** Write data from a buffer into a file. Return SQLITE_OK on success
|
|
|
|
** or some other error code on failure.
|
2001-09-18 00:25:58 +04:00
|
|
|
*/
|
2007-08-15 21:08:46 +04:00
|
|
|
static int unixWrite(
|
|
|
|
sqlite3_file *id,
|
|
|
|
const void *pBuf,
|
|
|
|
int amt,
|
|
|
|
sqlite3_int64 offset
|
|
|
|
){
|
2002-09-05 20:08:27 +04:00
|
|
|
int wrote = 0;
|
2005-11-29 06:13:21 +03:00
|
|
|
assert( id );
|
2005-02-03 03:29:47 +03:00
|
|
|
assert( amt>0 );
|
2007-08-15 21:08:46 +04:00
|
|
|
while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){
|
2002-09-05 20:08:27 +04:00
|
|
|
amt -= wrote;
|
2007-08-15 21:08:46 +04:00
|
|
|
offset += wrote;
|
2002-09-05 20:08:27 +04:00
|
|
|
pBuf = &((char*)pBuf)[wrote];
|
|
|
|
}
|
2006-09-14 17:47:11 +04:00
|
|
|
SimulateIOError(( wrote=(-1), amt=1 ));
|
|
|
|
SimulateDiskfullError(( wrote=0, amt=1 ));
|
2002-09-05 20:08:27 +04:00
|
|
|
if( amt>0 ){
|
2006-09-14 17:47:11 +04:00
|
|
|
if( wrote<0 ){
|
2006-09-15 11:28:50 +04:00
|
|
|
return SQLITE_IOERR_WRITE;
|
2006-09-14 17:47:11 +04:00
|
|
|
}else{
|
|
|
|
return SQLITE_FULL;
|
|
|
|
}
|
2002-09-05 20:08:27 +04:00
|
|
|
}
|
2001-09-19 17:22:39 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2005-03-10 17:11:12 +03:00
|
|
|
#ifdef SQLITE_TEST
|
|
|
|
/*
|
|
|
|
** Count the number of fullsyncs and normal syncs. This is used to test
|
|
|
|
** that syncs and fullsyncs are occuring at the right times.
|
|
|
|
*/
|
|
|
|
int sqlite3_sync_count = 0;
|
|
|
|
int sqlite3_fullsync_count = 0;
|
|
|
|
#endif
|
|
|
|
|
2005-10-05 14:29:36 +04:00
|
|
|
/*
|
|
|
|
** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.
|
|
|
|
** Otherwise use fsync() in its place.
|
|
|
|
*/
|
|
|
|
#ifndef HAVE_FDATASYNC
|
|
|
|
# define fdatasync fsync
|
|
|
|
#endif
|
|
|
|
|
2006-02-11 04:25:50 +03:00
|
|
|
/*
|
|
|
|
** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
|
|
|
|
** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
|
|
|
|
** only available on Mac OS X. But that could change.
|
|
|
|
*/
|
|
|
|
#ifdef F_FULLFSYNC
|
|
|
|
# define HAVE_FULLFSYNC 1
|
|
|
|
#else
|
|
|
|
# define HAVE_FULLFSYNC 0
|
|
|
|
#endif
|
|
|
|
|
2005-03-10 17:11:12 +03:00
|
|
|
|
2004-07-18 01:44:57 +04:00
|
|
|
/*
|
|
|
|
** The fsync() system call does not work as advertised on many
|
|
|
|
** unix systems. The following procedure is an attempt to make
|
|
|
|
** it work better.
|
2005-01-20 02:24:50 +03:00
|
|
|
**
|
|
|
|
** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
|
|
|
|
** for testing when we want to run through the test suite quickly.
|
|
|
|
** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
|
|
|
|
** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
|
|
|
|
** or power failure will likely corrupt the database file.
|
2004-07-18 01:44:57 +04:00
|
|
|
*/
|
2005-09-08 16:38:41 +04:00
|
|
|
static int full_fsync(int fd, int fullSync, int dataOnly){
|
2005-03-10 17:11:12 +03:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Record the number of times that we do a normal fsync() and
|
|
|
|
** FULLSYNC. This is used during testing to verify that this procedure
|
|
|
|
** gets called with the correct arguments.
|
|
|
|
*/
|
|
|
|
#ifdef SQLITE_TEST
|
|
|
|
if( fullSync ) sqlite3_fullsync_count++;
|
|
|
|
sqlite3_sync_count++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
|
|
|
|
** no-op
|
|
|
|
*/
|
2005-01-20 02:24:50 +03:00
|
|
|
#ifdef SQLITE_NO_SYNC
|
2005-03-10 17:11:12 +03:00
|
|
|
rc = SQLITE_OK;
|
2005-01-20 02:24:50 +03:00
|
|
|
#else
|
2005-03-10 17:11:12 +03:00
|
|
|
|
2006-02-11 04:25:50 +03:00
|
|
|
#if HAVE_FULLFSYNC
|
2005-03-10 17:11:12 +03:00
|
|
|
if( fullSync ){
|
2005-03-11 20:52:34 +03:00
|
|
|
rc = fcntl(fd, F_FULLFSYNC, 0);
|
2007-02-01 02:37:07 +03:00
|
|
|
}else{
|
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
/* If the FULLFSYNC failed, fall back to attempting an fsync().
|
|
|
|
* It shouldn't be possible for fullfsync to fail on the local
|
|
|
|
* file system (on OSX), so failure indicates that FULLFSYNC
|
|
|
|
* isn't supported for this file system. So, attempt an fsync
|
|
|
|
* and (for now) ignore the overhead of a superfluous fcntl call.
|
|
|
|
* It'd be better to detect fullfsync support once and avoid
|
|
|
|
* the fcntl call every time sync is called.
|
|
|
|
*/
|
|
|
|
if( rc ) rc = fsync(fd);
|
|
|
|
|
|
|
|
#else
|
2005-09-08 16:38:41 +04:00
|
|
|
if( dataOnly ){
|
|
|
|
rc = fdatasync(fd);
|
2005-10-05 14:29:36 +04:00
|
|
|
}else{
|
2005-09-08 16:38:41 +04:00
|
|
|
rc = fsync(fd);
|
|
|
|
}
|
2007-02-01 02:37:07 +03:00
|
|
|
#endif /* HAVE_FULLFSYNC */
|
2005-03-10 17:11:12 +03:00
|
|
|
#endif /* defined(SQLITE_NO_SYNC) */
|
|
|
|
|
2004-07-18 01:44:57 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
|
|
|
** Make sure all writes to a particular file are committed to disk.
|
2003-07-27 22:59:42 +04:00
|
|
|
**
|
2005-09-08 16:38:41 +04:00
|
|
|
** If dataOnly==0 then both the file itself and its metadata (file
|
|
|
|
** size, access time, etc) are synced. If dataOnly!=0 then only the
|
|
|
|
** file data is synced.
|
|
|
|
**
|
2003-07-27 22:59:42 +04:00
|
|
|
** 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.
|
2001-09-18 00:25:58 +04:00
|
|
|
*/
|
2007-08-17 20:50:38 +04:00
|
|
|
static int unixSync(sqlite3_file *id, int flags){
|
2006-09-14 17:47:11 +04:00
|
|
|
int rc;
|
2005-11-30 06:20:31 +03:00
|
|
|
unixFile *pFile = (unixFile*)id;
|
2007-08-17 20:50:38 +04:00
|
|
|
|
2007-08-20 09:36:51 +04:00
|
|
|
int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
|
|
|
|
int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
|
|
|
|
|
2007-08-30 18:49:58 +04:00
|
|
|
/* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
|
2007-08-20 09:36:51 +04:00
|
|
|
assert((flags&0x0F)==SQLITE_SYNC_NORMAL
|
|
|
|
|| (flags&0x0F)==SQLITE_SYNC_FULL
|
|
|
|
);
|
2007-08-17 20:50:38 +04:00
|
|
|
|
2005-11-30 06:20:31 +03:00
|
|
|
assert( pFile );
|
2007-03-27 02:05:01 +04:00
|
|
|
OSTRACE2("SYNC %-3d\n", pFile->h);
|
2007-08-17 20:50:38 +04:00
|
|
|
rc = full_fsync(pFile->h, isFullsync, isDataOnly);
|
2006-09-14 17:47:11 +04:00
|
|
|
SimulateIOError( rc=1 );
|
|
|
|
if( rc ){
|
2006-09-15 11:28:50 +04:00
|
|
|
return SQLITE_IOERR_FSYNC;
|
2002-09-05 20:08:27 +04:00
|
|
|
}
|
2005-11-30 06:20:31 +03:00
|
|
|
if( pFile->dirfd>=0 ){
|
2007-03-27 02:05:01 +04:00
|
|
|
OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
|
2007-08-17 20:50:38 +04:00
|
|
|
HAVE_FULLFSYNC, isFullsync);
|
2005-11-25 13:38:22 +03:00
|
|
|
#ifndef SQLITE_DISABLE_DIRSYNC
|
2006-02-11 04:25:50 +03:00
|
|
|
/* The directory sync is only attempted if full_fsync is
|
|
|
|
** turned off or unavailable. If a full_fsync occurred above,
|
|
|
|
** then the directory sync is superfluous.
|
|
|
|
*/
|
2007-08-17 20:50:38 +04:00
|
|
|
if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
|
2006-02-11 04:25:50 +03:00
|
|
|
/*
|
|
|
|
** We have received multiple reports of fsync() returning
|
2006-02-10 02:05:51 +03:00
|
|
|
** errors when applied to directories on certain file systems.
|
|
|
|
** A failed directory sync is not a big deal. So it seems
|
|
|
|
** better to ignore the error. Ticket #1657
|
|
|
|
*/
|
|
|
|
/* return SQLITE_IOERR; */
|
2005-11-25 11:47:57 +03:00
|
|
|
}
|
2005-11-25 13:38:22 +03:00
|
|
|
#endif
|
2005-11-30 06:20:31 +03:00
|
|
|
close(pFile->dirfd); /* Only need to sync once, so close the directory */
|
|
|
|
pFile->dirfd = -1; /* when we are done. */
|
2004-06-17 23:04:17 +04:00
|
|
|
}
|
|
|
|
return SQLITE_OK;
|
2001-09-19 17:22:39 +04:00
|
|
|
}
|
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
|
|
|
** Truncate an open file to a specified size
|
|
|
|
*/
|
2007-08-15 21:08:46 +04:00
|
|
|
static int unixTruncate(sqlite3_file *id, i64 nByte){
|
2006-09-14 17:47:11 +04:00
|
|
|
int rc;
|
2005-11-29 06:13:21 +03:00
|
|
|
assert( id );
|
2008-01-16 20:46:38 +03:00
|
|
|
SimulateIOError( return SQLITE_IOERR_TRUNCATE );
|
2007-06-19 14:50:38 +04:00
|
|
|
rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);
|
2006-09-14 17:47:11 +04:00
|
|
|
if( rc ){
|
2006-09-15 11:28:50 +04:00
|
|
|
return SQLITE_IOERR_TRUNCATE;
|
2006-09-14 17:47:11 +04:00
|
|
|
}else{
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
2001-09-19 17:22:39 +04:00
|
|
|
}
|
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
|
|
|
** Determine the current size of a file in bytes
|
|
|
|
*/
|
2007-08-15 21:08:46 +04:00
|
|
|
static int unixFileSize(sqlite3_file *id, i64 *pSize){
|
2006-09-14 17:47:11 +04:00
|
|
|
int rc;
|
2001-09-19 17:22:39 +04:00
|
|
|
struct stat buf;
|
2005-11-29 06:13:21 +03:00
|
|
|
assert( id );
|
2006-09-14 17:47:11 +04:00
|
|
|
rc = fstat(((unixFile*)id)->h, &buf);
|
|
|
|
SimulateIOError( rc=1 );
|
|
|
|
if( rc!=0 ){
|
2006-09-15 11:28:50 +04:00
|
|
|
return SQLITE_IOERR_FSTAT;
|
2001-09-19 17:22:39 +04:00
|
|
|
}
|
|
|
|
*pSize = buf.st_size;
|
2008-07-30 21:28:04 +04:00
|
|
|
|
|
|
|
/* When opening a zero-size database, the findLockInfo() procedure
|
|
|
|
** writes a single byte into that file in order to work around a bug
|
|
|
|
** in the OS-X msdos filesystem. In order to avoid problems with upper
|
|
|
|
** layers, we need to report this file size as zero even though it is
|
|
|
|
** really 1. Ticket #3260.
|
|
|
|
*/
|
|
|
|
if( *pSize==1 ) *pSize = 0;
|
|
|
|
|
|
|
|
|
2001-09-19 17:22:39 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2008-08-22 04:22:35 +04:00
|
|
|
/*
|
|
|
|
** This routine translates a standard POSIX errno code into something
|
|
|
|
** useful to the clients of the sqlite3 functions. Specifically, it is
|
|
|
|
** intended to translate a variety of "try again" errors into SQLITE_BUSY
|
|
|
|
** and a variety of "please close the file descriptor NOW" errors into
|
|
|
|
** SQLITE_IOERR
|
|
|
|
**
|
|
|
|
** Errors during initialization of locks, or file system support for locks,
|
|
|
|
** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
|
|
|
|
*/
|
|
|
|
static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
|
|
|
|
switch (posixError) {
|
|
|
|
case 0:
|
|
|
|
return SQLITE_OK;
|
|
|
|
|
|
|
|
case EAGAIN:
|
|
|
|
case ETIMEDOUT:
|
|
|
|
case EBUSY:
|
|
|
|
case EINTR:
|
|
|
|
case ENOLCK:
|
|
|
|
/* random NFS retry error, unless during file system support
|
|
|
|
* introspection, in which it actually means what it says */
|
|
|
|
return SQLITE_BUSY;
|
|
|
|
|
|
|
|
case EACCES:
|
|
|
|
/* EACCES is like EAGAIN during locking operations, but not any other time*/
|
|
|
|
if( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
|
|
|
|
(sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
|
|
|
|
(sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
|
|
|
|
(sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
|
|
|
|
return SQLITE_BUSY;
|
|
|
|
}
|
|
|
|
/* else fall through */
|
|
|
|
case EPERM:
|
|
|
|
return SQLITE_PERM;
|
|
|
|
|
|
|
|
case EDEADLK:
|
|
|
|
return SQLITE_IOERR_BLOCKED;
|
|
|
|
|
2008-08-22 04:47:53 +04:00
|
|
|
#if EOPNOTSUPP!=ENOTSUP
|
2008-08-22 04:22:35 +04:00
|
|
|
case EOPNOTSUPP:
|
|
|
|
/* something went terribly awry, unless during file system support
|
|
|
|
* introspection, in which it actually means what it says */
|
2008-08-22 04:47:53 +04:00
|
|
|
#endif
|
2008-09-15 08:20:31 +04:00
|
|
|
#ifdef ENOTSUP
|
2008-08-22 04:22:35 +04:00
|
|
|
case ENOTSUP:
|
|
|
|
/* invalid fd, unless during file system support introspection, in which
|
|
|
|
* it actually means what it says */
|
2008-09-15 08:20:31 +04:00
|
|
|
#endif
|
2008-08-22 04:22:35 +04:00
|
|
|
case EIO:
|
|
|
|
case EBADF:
|
|
|
|
case EINVAL:
|
|
|
|
case ENOTCONN:
|
|
|
|
case ENODEV:
|
|
|
|
case ENXIO:
|
|
|
|
case ENOENT:
|
|
|
|
case ESTALE:
|
|
|
|
case ENOSYS:
|
|
|
|
/* these should force the client to close the file and reconnect */
|
|
|
|
|
|
|
|
default:
|
|
|
|
return sqliteIOErr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-03 20:08:41 +04:00
|
|
|
/*
|
|
|
|
** This routine checks if there is a RESERVED lock held on the specified
|
2008-08-22 04:22:35 +04:00
|
|
|
** file by this or any other process. If such a lock is held, set *pResOut
|
|
|
|
** to a non-zero value otherwise *pResOut is set to zero. The return value
|
|
|
|
** is set to SQLITE_OK unless an I/O error occurs during lock checking.
|
2004-06-03 20:08:41 +04:00
|
|
|
*/
|
2008-06-05 15:39:11 +04:00
|
|
|
static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
|
2008-08-22 04:22:35 +04:00
|
|
|
int rc = SQLITE_OK;
|
|
|
|
int reserved = 0;
|
2005-11-30 06:20:31 +03:00
|
|
|
unixFile *pFile = (unixFile*)id;
|
2004-06-03 20:08:41 +04:00
|
|
|
|
2008-06-05 15:39:11 +04:00
|
|
|
SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
|
|
|
|
|
2005-11-30 06:20:31 +03:00
|
|
|
assert( pFile );
|
2007-08-17 19:53:36 +04:00
|
|
|
enterMutex(); /* Because pFile->pLock is shared across threads */
|
2004-06-03 20:08:41 +04:00
|
|
|
|
|
|
|
/* Check if a thread in this process holds such a lock */
|
2005-11-30 06:20:31 +03:00
|
|
|
if( pFile->pLock->locktype>SHARED_LOCK ){
|
2008-08-22 04:22:35 +04:00
|
|
|
reserved = 1;
|
2004-06-03 20:08:41 +04:00
|
|
|
}
|
|
|
|
|
2004-06-07 20:27:46 +04:00
|
|
|
/* Otherwise see if some other process holds it.
|
2004-06-03 20:08:41 +04:00
|
|
|
*/
|
2008-08-22 04:22:35 +04:00
|
|
|
if( !reserved ){
|
2004-06-03 20:08:41 +04:00
|
|
|
struct flock lock;
|
|
|
|
lock.l_whence = SEEK_SET;
|
2004-06-07 20:27:46 +04:00
|
|
|
lock.l_start = RESERVED_BYTE;
|
|
|
|
lock.l_len = 1;
|
|
|
|
lock.l_type = F_WRLCK;
|
2008-08-22 04:22:35 +04:00
|
|
|
if (-1 == fcntl(pFile->h, F_GETLK, &lock)) {
|
|
|
|
int tErrno = errno;
|
|
|
|
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
|
|
|
|
pFile->lastErrno = tErrno;
|
|
|
|
} else if( lock.l_type!=F_UNLCK ){
|
|
|
|
reserved = 1;
|
2004-06-03 20:08:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-17 19:53:36 +04:00
|
|
|
leaveMutex();
|
2008-08-22 04:22:35 +04:00
|
|
|
OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
|
2004-06-03 20:08:41 +04:00
|
|
|
|
2008-08-22 04:22:35 +04:00
|
|
|
*pResOut = reserved;
|
|
|
|
return rc;
|
2004-06-03 20:08:41 +04:00
|
|
|
}
|
|
|
|
|
2004-06-01 18:09:28 +04:00
|
|
|
/*
|
|
|
|
** Lock the file with the lock specified by parameter locktype - one
|
|
|
|
** of the following:
|
|
|
|
**
|
2004-06-07 20:27:46 +04:00
|
|
|
** (1) SHARED_LOCK
|
|
|
|
** (2) RESERVED_LOCK
|
|
|
|
** (3) PENDING_LOCK
|
|
|
|
** (4) EXCLUSIVE_LOCK
|
|
|
|
**
|
2004-06-08 04:47:47 +04:00
|
|
|
** Sometimes when requesting one lock state, additional lock states
|
|
|
|
** are inserted in between. The locking might fail on one of the later
|
|
|
|
** transitions leaving the lock state different from what it started but
|
|
|
|
** still short of its goal. The following chart shows the allowed
|
|
|
|
** transitions and the inserted intermediate states:
|
|
|
|
**
|
|
|
|
** UNLOCKED -> SHARED
|
|
|
|
** SHARED -> RESERVED
|
|
|
|
** SHARED -> (PENDING) -> EXCLUSIVE
|
|
|
|
** RESERVED -> (PENDING) -> EXCLUSIVE
|
|
|
|
** PENDING -> EXCLUSIVE
|
2004-06-07 20:27:46 +04:00
|
|
|
**
|
2004-06-09 21:37:22 +04:00
|
|
|
** This routine will only increase a lock. Use the sqlite3OsUnlock()
|
|
|
|
** routine to lower a locking level.
|
2004-06-01 18:09:28 +04:00
|
|
|
*/
|
2007-08-15 21:08:46 +04:00
|
|
|
static int unixLock(sqlite3_file *id, int locktype){
|
2004-06-25 11:21:28 +04:00
|
|
|
/* The following describes the implementation of the various locks and
|
|
|
|
** lock transitions in terms of the POSIX advisory shared and exclusive
|
|
|
|
** lock primitives (called read-locks and write-locks below, to avoid
|
|
|
|
** confusion with SQLite lock names). The algorithms are complicated
|
|
|
|
** slightly in order to be compatible with windows systems simultaneously
|
|
|
|
** accessing the same database file, in case that is ever required.
|
|
|
|
**
|
|
|
|
** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
|
|
|
|
** byte', each single bytes at well known offsets, and the 'shared byte
|
|
|
|
** range', a range of 510 bytes at a well known offset.
|
|
|
|
**
|
|
|
|
** To obtain a SHARED lock, a read-lock is obtained on the 'pending
|
|
|
|
** byte'. If this is successful, a random byte from the 'shared byte
|
|
|
|
** range' is read-locked and the lock on the 'pending byte' released.
|
|
|
|
**
|
2004-06-25 12:32:25 +04:00
|
|
|
** A process may only obtain a RESERVED lock after it has a SHARED lock.
|
|
|
|
** A RESERVED lock is implemented by grabbing a write-lock on the
|
|
|
|
** 'reserved byte'.
|
2004-06-25 11:21:28 +04:00
|
|
|
**
|
|
|
|
** A process may only obtain a PENDING lock after it has obtained a
|
2004-06-25 12:32:25 +04:00
|
|
|
** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
|
|
|
|
** on the 'pending byte'. This ensures that no new SHARED locks can be
|
|
|
|
** obtained, but existing SHARED locks are allowed to persist. A process
|
|
|
|
** does not have to obtain a RESERVED lock on the way to a PENDING lock.
|
|
|
|
** This property is used by the algorithm for rolling back a journal file
|
|
|
|
** after a crash.
|
2004-06-25 11:21:28 +04:00
|
|
|
**
|
2004-06-25 12:32:25 +04:00
|
|
|
** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
|
|
|
|
** implemented by obtaining a write-lock on the entire 'shared byte
|
|
|
|
** range'. Since all other locks require a read-lock on one of the bytes
|
|
|
|
** within this range, this ensures that no other locks are held on the
|
|
|
|
** database.
|
2004-06-25 11:21:28 +04:00
|
|
|
**
|
|
|
|
** The reason a single byte cannot be used instead of the 'shared byte
|
|
|
|
** range' is that some versions of windows do not support read-locks. By
|
|
|
|
** locking a random byte from a range, concurrent SHARED locks may exist
|
|
|
|
** even if the locking primitive used is always a write-lock.
|
|
|
|
*/
|
2004-06-01 18:09:28 +04:00
|
|
|
int rc = SQLITE_OK;
|
2005-11-30 06:20:31 +03:00
|
|
|
unixFile *pFile = (unixFile*)id;
|
|
|
|
struct lockInfo *pLock = pFile->pLock;
|
2004-06-01 18:09:28 +04:00
|
|
|
struct flock lock;
|
|
|
|
int s;
|
|
|
|
|
2005-11-30 06:20:31 +03:00
|
|
|
assert( pFile );
|
2007-03-27 02:05:01 +04:00
|
|
|
OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h,
|
2005-11-30 06:20:31 +03:00
|
|
|
locktypeName(locktype), locktypeName(pFile->locktype),
|
|
|
|
locktypeName(pLock->locktype), pLock->cnt , getpid());
|
2004-06-01 18:09:28 +04:00
|
|
|
|
|
|
|
/* If there is already a lock of this type or more restrictive on the
|
2007-08-20 10:44:22 +04:00
|
|
|
** unixFile, do nothing. Don't use the end_lock: exit path, as
|
2007-08-17 19:53:36 +04:00
|
|
|
** enterMutex() hasn't been called yet.
|
2004-06-01 18:09:28 +04:00
|
|
|
*/
|
2005-11-30 06:20:31 +03:00
|
|
|
if( pFile->locktype>=locktype ){
|
2007-03-27 02:05:01 +04:00
|
|
|
OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
|
2005-11-30 06:20:31 +03:00
|
|
|
locktypeName(locktype));
|
2004-06-01 18:09:28 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2004-06-08 04:47:47 +04:00
|
|
|
/* Make sure the locking sequence is correct
|
2004-06-07 20:27:46 +04:00
|
|
|
*/
|
2005-11-30 06:20:31 +03:00
|
|
|
assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
|
2004-06-08 04:47:47 +04:00
|
|
|
assert( locktype!=PENDING_LOCK );
|
2005-11-30 06:20:31 +03:00
|
|
|
assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
|
2004-06-07 20:27:46 +04:00
|
|
|
|
2005-11-30 06:20:31 +03:00
|
|
|
/* This mutex is needed because pFile->pLock is shared across threads
|
2004-06-08 04:47:47 +04:00
|
|
|
*/
|
2007-08-17 19:53:36 +04:00
|
|
|
enterMutex();
|
2004-06-01 18:09:28 +04:00
|
|
|
|
2006-01-15 03:13:15 +03:00
|
|
|
/* Make sure the current thread owns the pFile.
|
|
|
|
*/
|
|
|
|
rc = transferOwnership(pFile);
|
|
|
|
if( rc!=SQLITE_OK ){
|
2007-08-17 19:53:36 +04:00
|
|
|
leaveMutex();
|
2006-01-15 03:13:15 +03:00
|
|
|
return rc;
|
|
|
|
}
|
2006-01-15 05:30:57 +03:00
|
|
|
pLock = pFile->pLock;
|
2006-01-15 03:13:15 +03:00
|
|
|
|
2007-08-20 10:44:22 +04:00
|
|
|
/* If some thread using this PID has a lock via a different unixFile*
|
2004-06-01 18:09:28 +04:00
|
|
|
** handle that precludes the requested lock, return BUSY.
|
|
|
|
*/
|
2005-11-30 06:20:31 +03:00
|
|
|
if( (pFile->locktype!=pLock->locktype &&
|
2004-06-07 20:27:46 +04:00
|
|
|
(pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
|
2004-06-01 18:09:28 +04:00
|
|
|
){
|
|
|
|
rc = SQLITE_BUSY;
|
|
|
|
goto end_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If a SHARED lock is requested, and some thread using this PID already
|
|
|
|
** has a SHARED or RESERVED lock, then increment reference counts and
|
|
|
|
** return SQLITE_OK.
|
|
|
|
*/
|
|
|
|
if( locktype==SHARED_LOCK &&
|
|
|
|
(pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
|
|
|
|
assert( locktype==SHARED_LOCK );
|
2005-11-30 06:20:31 +03:00
|
|
|
assert( pFile->locktype==0 );
|
2004-06-02 10:30:16 +04:00
|
|
|
assert( pLock->cnt>0 );
|
2005-11-30 06:20:31 +03:00
|
|
|
pFile->locktype = SHARED_LOCK;
|
2004-06-01 18:09:28 +04:00
|
|
|
pLock->cnt++;
|
2005-11-30 06:20:31 +03:00
|
|
|
pFile->pOpen->nLock++;
|
2004-06-01 18:09:28 +04:00
|
|
|
goto end_lock;
|
|
|
|
}
|
|
|
|
|
2004-06-03 20:08:41 +04:00
|
|
|
lock.l_len = 1L;
|
2005-06-15 21:47:55 +04:00
|
|
|
|
2004-06-01 18:09:28 +04:00
|
|
|
lock.l_whence = SEEK_SET;
|
|
|
|
|
2004-06-12 06:17:14 +04:00
|
|
|
/* A PENDING lock is needed before acquiring a SHARED lock and before
|
|
|
|
** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
|
|
|
|
** be released.
|
2004-06-01 18:09:28 +04:00
|
|
|
*/
|
2004-06-12 06:17:14 +04:00
|
|
|
if( locktype==SHARED_LOCK
|
2005-11-30 06:20:31 +03:00
|
|
|
|| (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
|
2004-06-12 06:17:14 +04:00
|
|
|
){
|
2004-06-28 12:25:47 +04:00
|
|
|
lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
|
2004-06-07 20:27:46 +04:00
|
|
|
lock.l_start = PENDING_BYTE;
|
2005-11-30 06:20:31 +03:00
|
|
|
s = fcntl(pFile->h, F_SETLK, &lock);
|
2007-03-30 00:19:58 +04:00
|
|
|
if( s==(-1) ){
|
2008-08-22 04:22:35 +04:00
|
|
|
int tErrno = errno;
|
|
|
|
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
|
|
|
|
if( IS_LOCK_ERROR(rc) ){
|
|
|
|
pFile->lastErrno = tErrno;
|
|
|
|
}
|
2004-06-01 18:09:28 +04:00
|
|
|
goto end_lock;
|
|
|
|
}
|
2004-06-12 06:17:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* If control gets to this point, then actually go ahead and make
|
|
|
|
** operating system calls for the specified lock.
|
|
|
|
*/
|
|
|
|
if( locktype==SHARED_LOCK ){
|
2008-08-22 04:22:35 +04:00
|
|
|
int tErrno = 0;
|
2004-06-12 06:17:14 +04:00
|
|
|
assert( pLock->cnt==0 );
|
|
|
|
assert( pLock->locktype==0 );
|
2004-06-01 18:09:28 +04:00
|
|
|
|
2004-06-07 20:27:46 +04:00
|
|
|
/* Now get the read-lock */
|
|
|
|
lock.l_start = SHARED_FIRST;
|
|
|
|
lock.l_len = SHARED_SIZE;
|
2008-08-22 04:22:35 +04:00
|
|
|
if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){
|
|
|
|
tErrno = errno;
|
|
|
|
}
|
2004-06-07 20:27:46 +04:00
|
|
|
/* Drop the temporary PENDING lock */
|
|
|
|
lock.l_start = PENDING_BYTE;
|
|
|
|
lock.l_len = 1L;
|
2004-06-01 18:09:28 +04:00
|
|
|
lock.l_type = F_UNLCK;
|
2005-11-30 06:20:31 +03:00
|
|
|
if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
|
2008-08-22 04:22:35 +04:00
|
|
|
if( s != -1 ){
|
|
|
|
/* This could happen with a network mount */
|
|
|
|
tErrno = errno;
|
|
|
|
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
|
|
|
|
if( IS_LOCK_ERROR(rc) ){
|
|
|
|
pFile->lastErrno = tErrno;
|
|
|
|
}
|
|
|
|
goto end_lock;
|
|
|
|
}
|
2005-06-15 21:47:55 +04:00
|
|
|
}
|
2007-03-30 00:19:58 +04:00
|
|
|
if( s==(-1) ){
|
2008-08-22 04:22:35 +04:00
|
|
|
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
|
|
|
|
if( IS_LOCK_ERROR(rc) ){
|
|
|
|
pFile->lastErrno = tErrno;
|
|
|
|
}
|
2001-12-14 18:09:55 +03:00
|
|
|
}else{
|
2005-11-30 06:20:31 +03:00
|
|
|
pFile->locktype = SHARED_LOCK;
|
|
|
|
pFile->pOpen->nLock++;
|
2004-06-01 18:09:28 +04:00
|
|
|
pLock->cnt = 1;
|
2001-12-06 16:24:14 +03:00
|
|
|
}
|
2004-06-12 06:17:14 +04:00
|
|
|
}else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
|
|
|
|
/* We are trying for an exclusive lock but another thread in this
|
|
|
|
** same process is still holding a shared lock. */
|
|
|
|
rc = SQLITE_BUSY;
|
2001-12-14 18:09:55 +03:00
|
|
|
}else{
|
2004-06-12 06:17:14 +04:00
|
|
|
/* The request was for a RESERVED or EXCLUSIVE lock. It is
|
2004-06-01 18:09:28 +04:00
|
|
|
** assumed that there is a SHARED or greater lock on the file
|
|
|
|
** already.
|
|
|
|
*/
|
2005-11-30 06:20:31 +03:00
|
|
|
assert( 0!=pFile->locktype );
|
2004-06-01 18:09:28 +04:00
|
|
|
lock.l_type = F_WRLCK;
|
|
|
|
switch( locktype ){
|
|
|
|
case RESERVED_LOCK:
|
2004-06-07 20:27:46 +04:00
|
|
|
lock.l_start = RESERVED_BYTE;
|
2004-06-01 18:09:28 +04:00
|
|
|
break;
|
|
|
|
case EXCLUSIVE_LOCK:
|
2004-06-07 20:27:46 +04:00
|
|
|
lock.l_start = SHARED_FIRST;
|
|
|
|
lock.l_len = SHARED_SIZE;
|
2004-06-01 18:09:28 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2005-11-30 06:20:31 +03:00
|
|
|
s = fcntl(pFile->h, F_SETLK, &lock);
|
2007-03-30 00:19:58 +04:00
|
|
|
if( s==(-1) ){
|
2008-08-22 04:22:35 +04:00
|
|
|
int tErrno = errno;
|
|
|
|
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
|
|
|
|
if( IS_LOCK_ERROR(rc) ){
|
|
|
|
pFile->lastErrno = tErrno;
|
|
|
|
}
|
2004-06-01 18:09:28 +04:00
|
|
|
}
|
2001-10-09 08:19:46 +04:00
|
|
|
}
|
2004-06-01 18:09:28 +04:00
|
|
|
|
2004-06-02 10:30:16 +04:00
|
|
|
if( rc==SQLITE_OK ){
|
2005-11-30 06:20:31 +03:00
|
|
|
pFile->locktype = locktype;
|
2004-06-02 10:30:16 +04:00
|
|
|
pLock->locktype = locktype;
|
2004-06-12 06:17:14 +04:00
|
|
|
}else if( locktype==EXCLUSIVE_LOCK ){
|
2005-11-30 06:20:31 +03:00
|
|
|
pFile->locktype = PENDING_LOCK;
|
2004-06-12 06:17:14 +04:00
|
|
|
pLock->locktype = PENDING_LOCK;
|
2004-06-02 10:30:16 +04:00
|
|
|
}
|
2004-06-01 18:09:28 +04:00
|
|
|
|
|
|
|
end_lock:
|
2007-08-17 19:53:36 +04:00
|
|
|
leaveMutex();
|
2007-03-27 02:05:01 +04:00
|
|
|
OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
|
2004-06-29 11:45:33 +04:00
|
|
|
rc==SQLITE_OK ? "ok" : "failed");
|
2001-10-09 08:19:46 +04:00
|
|
|
return rc;
|
2001-09-19 17:22:39 +04:00
|
|
|
}
|
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
2005-11-30 06:20:31 +03:00
|
|
|
** Lower the locking level on file descriptor pFile to locktype. locktype
|
2004-06-09 21:37:22 +04:00
|
|
|
** must be either NO_LOCK or SHARED_LOCK.
|
|
|
|
**
|
|
|
|
** If the locking level of the file descriptor is already at or below
|
|
|
|
** the requested locking level, this routine is a no-op.
|
2001-09-18 00:25:58 +04:00
|
|
|
*/
|
2007-08-15 21:08:46 +04:00
|
|
|
static int unixUnlock(sqlite3_file *id, int locktype){
|
2004-06-09 21:37:22 +04:00
|
|
|
struct lockInfo *pLock;
|
|
|
|
struct flock lock;
|
2004-10-03 00:38:28 +04:00
|
|
|
int rc = SQLITE_OK;
|
2005-11-30 06:20:31 +03:00
|
|
|
unixFile *pFile = (unixFile*)id;
|
2008-03-07 22:51:14 +03:00
|
|
|
int h;
|
2004-06-09 21:37:22 +04:00
|
|
|
|
2005-11-30 06:20:31 +03:00
|
|
|
assert( pFile );
|
2007-03-27 02:05:01 +04:00
|
|
|
OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
|
2005-11-30 06:20:31 +03:00
|
|
|
pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
|
2004-06-09 21:37:22 +04:00
|
|
|
|
|
|
|
assert( locktype<=SHARED_LOCK );
|
2005-11-30 06:20:31 +03:00
|
|
|
if( pFile->locktype<=locktype ){
|
2004-06-09 21:37:22 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
2006-01-15 20:27:17 +03:00
|
|
|
if( CHECK_THREADID(pFile) ){
|
|
|
|
return SQLITE_MISUSE;
|
|
|
|
}
|
2007-08-17 19:53:36 +04:00
|
|
|
enterMutex();
|
2008-03-07 22:51:14 +03:00
|
|
|
h = pFile->h;
|
2005-11-30 06:20:31 +03:00
|
|
|
pLock = pFile->pLock;
|
2004-06-09 21:37:22 +04:00
|
|
|
assert( pLock->cnt!=0 );
|
2005-11-30 06:20:31 +03:00
|
|
|
if( pFile->locktype>SHARED_LOCK ){
|
|
|
|
assert( pLock->locktype==pFile->locktype );
|
2008-03-07 22:51:14 +03:00
|
|
|
SimulateIOErrorBenign(1);
|
|
|
|
SimulateIOError( h=(-1) )
|
|
|
|
SimulateIOErrorBenign(0);
|
2004-10-03 00:38:28 +04:00
|
|
|
if( locktype==SHARED_LOCK ){
|
|
|
|
lock.l_type = F_RDLCK;
|
|
|
|
lock.l_whence = SEEK_SET;
|
|
|
|
lock.l_start = SHARED_FIRST;
|
|
|
|
lock.l_len = SHARED_SIZE;
|
2008-03-07 22:51:14 +03:00
|
|
|
if( fcntl(h, F_SETLK, &lock)==(-1) ){
|
2008-08-22 04:22:35 +04:00
|
|
|
int tErrno = errno;
|
|
|
|
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
|
|
|
|
if( IS_LOCK_ERROR(rc) ){
|
|
|
|
pFile->lastErrno = tErrno;
|
|
|
|
}
|
|
|
|
goto end_unlock;
|
2004-10-03 00:38:28 +04:00
|
|
|
}
|
|
|
|
}
|
2001-10-09 08:19:46 +04:00
|
|
|
lock.l_type = F_UNLCK;
|
|
|
|
lock.l_whence = SEEK_SET;
|
2004-06-09 21:37:22 +04:00
|
|
|
lock.l_start = PENDING_BYTE;
|
|
|
|
lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
|
2008-03-07 22:51:14 +03:00
|
|
|
if( fcntl(h, F_SETLK, &lock)!=(-1) ){
|
2005-06-15 21:47:55 +04:00
|
|
|
pLock->locktype = SHARED_LOCK;
|
|
|
|
}else{
|
2008-08-22 04:22:35 +04:00
|
|
|
int tErrno = errno;
|
|
|
|
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
|
|
|
|
if( IS_LOCK_ERROR(rc) ){
|
|
|
|
pFile->lastErrno = tErrno;
|
|
|
|
}
|
|
|
|
goto end_unlock;
|
2005-06-15 21:47:55 +04:00
|
|
|
}
|
2001-10-09 08:19:46 +04:00
|
|
|
}
|
2004-06-09 21:37:22 +04:00
|
|
|
if( locktype==NO_LOCK ){
|
|
|
|
struct openCnt *pOpen;
|
|
|
|
|
|
|
|
/* Decrement the shared lock counter. Release the lock using an
|
|
|
|
** OS call only when all threads in this same process have released
|
|
|
|
** the lock.
|
|
|
|
*/
|
|
|
|
pLock->cnt--;
|
|
|
|
if( pLock->cnt==0 ){
|
|
|
|
lock.l_type = F_UNLCK;
|
|
|
|
lock.l_whence = SEEK_SET;
|
|
|
|
lock.l_start = lock.l_len = 0L;
|
2008-03-07 22:51:14 +03:00
|
|
|
SimulateIOErrorBenign(1);
|
|
|
|
SimulateIOError( h=(-1) )
|
|
|
|
SimulateIOErrorBenign(0);
|
|
|
|
if( fcntl(h, F_SETLK, &lock)!=(-1) ){
|
2005-06-15 21:47:55 +04:00
|
|
|
pLock->locktype = NO_LOCK;
|
|
|
|
}else{
|
2008-08-22 04:22:35 +04:00
|
|
|
int tErrno = errno;
|
2008-09-15 08:20:31 +04:00
|
|
|
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
|
2008-08-22 04:22:35 +04:00
|
|
|
if( IS_LOCK_ERROR(rc) ){
|
|
|
|
pFile->lastErrno = tErrno;
|
|
|
|
}
|
2008-03-07 22:51:14 +03:00
|
|
|
pLock->cnt = 1;
|
2008-08-22 04:22:35 +04:00
|
|
|
goto end_unlock;
|
2005-06-15 21:47:55 +04:00
|
|
|
}
|
2004-06-09 21:37:22 +04:00
|
|
|
}
|
2004-06-02 10:30:16 +04:00
|
|
|
|
2004-01-12 03:39:05 +03:00
|
|
|
/* 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.
|
|
|
|
*/
|
2008-03-07 22:51:14 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
pOpen = pFile->pOpen;
|
|
|
|
pOpen->nLock--;
|
|
|
|
assert( pOpen->nLock>=0 );
|
|
|
|
if( pOpen->nLock==0 && pOpen->nPending>0 ){
|
|
|
|
int i;
|
|
|
|
for(i=0; i<pOpen->nPending; i++){
|
|
|
|
close(pOpen->aPending[i]);
|
|
|
|
}
|
2008-07-30 19:27:54 +04:00
|
|
|
sqlite3_free(pOpen->aPending);
|
2008-03-07 22:51:14 +03:00
|
|
|
pOpen->nPending = 0;
|
|
|
|
pOpen->aPending = 0;
|
2004-01-12 03:39:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-08-22 04:22:35 +04:00
|
|
|
|
|
|
|
end_unlock:
|
2007-08-17 19:53:36 +04:00
|
|
|
leaveMutex();
|
2008-03-07 22:51:14 +03:00
|
|
|
if( rc==SQLITE_OK ) pFile->locktype = locktype;
|
2004-10-03 00:38:28 +04:00
|
|
|
return rc;
|
2001-09-19 17:22:39 +04:00
|
|
|
}
|
|
|
|
|
2008-06-28 15:23:00 +04:00
|
|
|
/*
|
|
|
|
** This function performs the parts of the "close file" operation
|
|
|
|
** common to all locking schemes. It closes the directory and file
|
|
|
|
** handles, if they are valid, and sets all fields of the unixFile
|
|
|
|
** structure to 0.
|
|
|
|
*/
|
|
|
|
static int closeUnixFile(sqlite3_file *id){
|
|
|
|
unixFile *pFile = (unixFile*)id;
|
|
|
|
if( pFile ){
|
|
|
|
if( pFile->dirfd>=0 ){
|
|
|
|
close(pFile->dirfd);
|
|
|
|
}
|
|
|
|
if( pFile->h>=0 ){
|
|
|
|
close(pFile->h);
|
|
|
|
}
|
|
|
|
OSTRACE2("CLOSE %-3d\n", pFile->h);
|
|
|
|
OpenCounter(-1);
|
|
|
|
memset(pFile, 0, sizeof(unixFile));
|
|
|
|
}
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2004-06-22 15:29:02 +04:00
|
|
|
/*
|
|
|
|
** Close a file.
|
|
|
|
*/
|
2007-08-15 21:08:46 +04:00
|
|
|
static int unixClose(sqlite3_file *id){
|
2008-06-28 15:23:00 +04:00
|
|
|
if( id ){
|
|
|
|
unixFile *pFile = (unixFile *)id;
|
|
|
|
unixUnlock(id, NO_LOCK);
|
|
|
|
enterMutex();
|
2008-06-30 14:16:04 +04:00
|
|
|
if( pFile->pOpen && pFile->pOpen->nLock ){
|
2008-06-28 15:23:00 +04:00
|
|
|
/* 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;
|
|
|
|
struct openCnt *pOpen = pFile->pOpen;
|
2008-07-30 19:27:54 +04:00
|
|
|
aNew = sqlite3_realloc(pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
|
2008-06-28 15:23:00 +04:00
|
|
|
if( aNew==0 ){
|
|
|
|
/* If a malloc fails, just leak the file descriptor */
|
|
|
|
}else{
|
|
|
|
pOpen->aPending = aNew;
|
|
|
|
pOpen->aPending[pOpen->nPending] = pFile->h;
|
|
|
|
pOpen->nPending++;
|
|
|
|
pFile->h = -1;
|
|
|
|
}
|
2004-06-22 15:29:02 +04:00
|
|
|
}
|
2008-06-28 15:23:00 +04:00
|
|
|
releaseLockInfo(pFile->pLock);
|
|
|
|
releaseOpenCnt(pFile->pOpen);
|
|
|
|
closeUnixFile(id);
|
|
|
|
leaveMutex();
|
2004-06-22 15:29:02 +04:00
|
|
|
}
|
2006-01-20 21:10:57 +03:00
|
|
|
return SQLITE_OK;
|
2004-06-22 15:29:02 +04:00
|
|
|
}
|
|
|
|
|
2006-10-03 21:40:40 +04:00
|
|
|
|
|
|
|
#ifdef SQLITE_ENABLE_LOCKING_STYLE
|
|
|
|
#pragma mark AFP Support
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The afpLockingContext structure contains all afp lock specific state
|
|
|
|
*/
|
|
|
|
typedef struct afpLockingContext afpLockingContext;
|
|
|
|
struct afpLockingContext {
|
2008-03-07 22:51:14 +03:00
|
|
|
unsigned long long sharedLockByte;
|
2008-03-07 23:14:38 +03:00
|
|
|
const char *filePath;
|
2006-10-03 21:40:40 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ByteRangeLockPB2
|
|
|
|
{
|
|
|
|
unsigned long long offset; /* offset to first byte to lock */
|
|
|
|
unsigned long long length; /* nbr of bytes to lock */
|
|
|
|
unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
|
|
|
|
unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
|
|
|
|
unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
|
|
|
|
int fd; /* file desc to assoc this lock with */
|
|
|
|
};
|
|
|
|
|
2007-08-07 21:13:03 +04:00
|
|
|
#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
|
2006-10-03 21:40:40 +04:00
|
|
|
|
2007-08-20 10:44:22 +04:00
|
|
|
/*
|
2008-08-22 04:22:35 +04:00
|
|
|
** Return SQLITE_OK on success, SQLITE_BUSY on failure.
|
|
|
|
*/
|
2007-08-20 10:44:22 +04:00
|
|
|
static int _AFPFSSetLock(
|
|
|
|
const char *path,
|
2008-08-22 04:22:35 +04:00
|
|
|
unixFile *pFile,
|
2007-08-20 10:44:22 +04:00
|
|
|
unsigned long long offset,
|
|
|
|
unsigned long long length,
|
|
|
|
int setLockFlag
|
|
|
|
){
|
2007-08-07 21:13:03 +04:00
|
|
|
struct ByteRangeLockPB2 pb;
|
2006-10-03 21:40:40 +04:00
|
|
|
int err;
|
|
|
|
|
|
|
|
pb.unLockFlag = setLockFlag ? 0 : 1;
|
|
|
|
pb.startEndFlag = 0;
|
|
|
|
pb.offset = offset;
|
|
|
|
pb.length = length;
|
2008-08-22 04:22:35 +04:00
|
|
|
pb.fd = pFile->h;
|
2007-03-27 02:05:01 +04:00
|
|
|
OSTRACE5("AFPLOCK setting lock %s for %d in range %llx:%llx\n",
|
2008-08-22 04:22:35 +04:00
|
|
|
(setLockFlag?"ON":"OFF"), pFile->h, offset, length);
|
2006-10-03 21:40:40 +04:00
|
|
|
err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
|
|
|
|
if ( err==-1 ) {
|
2008-08-22 04:22:35 +04:00
|
|
|
int rc;
|
|
|
|
int tErrno = errno;
|
|
|
|
OSTRACE4("AFPLOCK failed to fsctl() '%s' %d %s\n", path, tErrno, strerror(tErrno));
|
|
|
|
rc = sqliteErrorFromPosixError(tErrno, setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); /* error */
|
|
|
|
if( IS_LOCK_ERROR(rc) ){
|
|
|
|
pFile->lastErrno = tErrno;
|
|
|
|
}
|
|
|
|
return rc;
|
2006-10-03 21:40:40 +04:00
|
|
|
} else {
|
2008-08-22 04:22:35 +04:00
|
|
|
return SQLITE_OK;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-22 04:22:35 +04:00
|
|
|
/* AFP-style reserved lock checking following the behavior of
|
|
|
|
** unixCheckReservedLock, see the unixCheckReservedLock function comments */
|
2008-06-28 15:23:00 +04:00
|
|
|
static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
|
2008-08-22 04:22:35 +04:00
|
|
|
int rc = SQLITE_OK;
|
|
|
|
int reserved = 0;
|
2006-10-03 21:40:40 +04:00
|
|
|
unixFile *pFile = (unixFile*)id;
|
|
|
|
|
2008-08-22 04:22:35 +04:00
|
|
|
SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
|
|
|
|
|
|
|
|
assert( pFile );
|
2006-10-03 21:40:40 +04:00
|
|
|
afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
|
|
|
|
|
|
|
|
/* Check if a thread in this process holds such a lock */
|
|
|
|
if( pFile->locktype>SHARED_LOCK ){
|
2008-08-22 04:22:35 +04:00
|
|
|
reserved = 1;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise see if some other process holds it.
|
|
|
|
*/
|
2008-08-22 04:22:35 +04:00
|
|
|
if( !reserved ){
|
|
|
|
/* lock the RESERVED byte */
|
|
|
|
int lrc = _AFPFSSetLock(context->filePath, pFile, RESERVED_BYTE, 1,1);
|
|
|
|
if( SQLITE_OK==lrc ){
|
2006-10-03 21:40:40 +04:00
|
|
|
/* if we succeeded in taking the reserved lock, unlock it to restore
|
|
|
|
** the original state */
|
2008-08-22 04:22:35 +04:00
|
|
|
lrc = _AFPFSSetLock(context->filePath, pFile, RESERVED_BYTE, 1, 0);
|
|
|
|
} else {
|
|
|
|
/* if we failed to get the lock then someone else must have it */
|
|
|
|
reserved = 1;
|
|
|
|
}
|
|
|
|
if( IS_LOCK_ERROR(lrc) ){
|
|
|
|
rc=lrc;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-22 04:22:35 +04:00
|
|
|
OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
|
|
|
|
|
|
|
|
*pResOut = reserved;
|
|
|
|
return rc;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* AFP-style locking following the behavior of unixLock, see the unixLock
|
|
|
|
** function comments for details of lock management. */
|
2008-06-28 15:23:00 +04:00
|
|
|
static int afpLock(sqlite3_file *id, int locktype){
|
2006-10-03 21:40:40 +04:00
|
|
|
int rc = SQLITE_OK;
|
|
|
|
unixFile *pFile = (unixFile*)id;
|
|
|
|
afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
|
|
|
|
|
|
|
|
assert( pFile );
|
2007-03-27 02:05:01 +04:00
|
|
|
OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h,
|
2008-03-07 18:34:11 +03:00
|
|
|
locktypeName(locktype), locktypeName(pFile->locktype), getpid());
|
|
|
|
|
2006-10-03 21:40:40 +04:00
|
|
|
/* If there is already a lock of this type or more restrictive on the
|
2008-03-07 18:34:11 +03:00
|
|
|
** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
|
|
|
|
** enterMutex() hasn't been called yet.
|
|
|
|
*/
|
2006-10-03 21:40:40 +04:00
|
|
|
if( pFile->locktype>=locktype ){
|
2007-03-27 02:05:01 +04:00
|
|
|
OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
|
2006-10-03 21:40:40 +04:00
|
|
|
locktypeName(locktype));
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure the locking sequence is correct
|
2008-03-07 18:34:11 +03:00
|
|
|
*/
|
2006-10-03 21:40:40 +04:00
|
|
|
assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
|
|
|
|
assert( locktype!=PENDING_LOCK );
|
|
|
|
assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
|
|
|
|
|
|
|
|
/* This mutex is needed because pFile->pLock is shared across threads
|
2008-03-07 18:34:11 +03:00
|
|
|
*/
|
2007-08-17 19:53:36 +04:00
|
|
|
enterMutex();
|
2006-10-03 21:40:40 +04:00
|
|
|
|
|
|
|
/* Make sure the current thread owns the pFile.
|
2008-03-07 18:34:11 +03:00
|
|
|
*/
|
2006-10-03 21:40:40 +04:00
|
|
|
rc = transferOwnership(pFile);
|
|
|
|
if( rc!=SQLITE_OK ){
|
2007-08-17 19:53:36 +04:00
|
|
|
leaveMutex();
|
2006-10-03 21:40:40 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* A PENDING lock is needed before acquiring a SHARED lock and before
|
2008-03-07 18:34:11 +03:00
|
|
|
** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
|
|
|
|
** be released.
|
|
|
|
*/
|
2006-10-03 21:40:40 +04:00
|
|
|
if( locktype==SHARED_LOCK
|
|
|
|
|| (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
|
2008-03-07 18:34:11 +03:00
|
|
|
){
|
|
|
|
int failed;
|
2008-08-22 04:22:35 +04:00
|
|
|
failed = _AFPFSSetLock(context->filePath, pFile, PENDING_BYTE, 1, 1);
|
2006-10-03 21:40:40 +04:00
|
|
|
if (failed) {
|
2008-08-22 04:22:35 +04:00
|
|
|
rc = failed;
|
2006-10-03 21:40:40 +04:00
|
|
|
goto afp_end_lock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If control gets to this point, then actually go ahead and make
|
2008-03-07 18:34:11 +03:00
|
|
|
** operating system calls for the specified lock.
|
|
|
|
*/
|
2006-10-03 21:40:40 +04:00
|
|
|
if( locktype==SHARED_LOCK ){
|
2008-08-22 04:22:35 +04:00
|
|
|
int lk, lrc1, lrc2, lrc1Errno;
|
2006-10-03 21:40:40 +04:00
|
|
|
|
2008-08-22 04:22:35 +04:00
|
|
|
/* Now get the read-lock SHARED_LOCK */
|
2006-10-03 21:40:40 +04:00
|
|
|
/* note that the quality of the randomness doesn't matter that much */
|
|
|
|
lk = random();
|
|
|
|
context->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
|
2008-08-22 04:22:35 +04:00
|
|
|
lrc1 = _AFPFSSetLock(context->filePath, pFile,
|
|
|
|
SHARED_FIRST+context->sharedLockByte, 1, 1);
|
|
|
|
if( IS_LOCK_ERROR(lrc1) ){
|
|
|
|
lrc1Errno = pFile->lastErrno;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
2008-08-22 04:22:35 +04:00
|
|
|
/* Drop the temporary PENDING lock */
|
|
|
|
lrc2 = _AFPFSSetLock(context->filePath, pFile, PENDING_BYTE, 1, 0);
|
2006-10-03 21:40:40 +04:00
|
|
|
|
2008-08-22 04:22:35 +04:00
|
|
|
if( IS_LOCK_ERROR(lrc1) ) {
|
|
|
|
pFile->lastErrno = lrc1Errno;
|
|
|
|
rc = lrc1;
|
|
|
|
goto afp_end_lock;
|
|
|
|
} else if( IS_LOCK_ERROR(lrc2) ){
|
|
|
|
rc = lrc2;
|
|
|
|
goto afp_end_lock;
|
|
|
|
} else if( lrc1 != SQLITE_OK ) {
|
|
|
|
rc = lrc1;
|
2006-10-03 21:40:40 +04:00
|
|
|
} else {
|
|
|
|
pFile->locktype = SHARED_LOCK;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
/* The request was for a RESERVED or EXCLUSIVE lock. It is
|
|
|
|
** assumed that there is a SHARED or greater lock on the file
|
|
|
|
** already.
|
|
|
|
*/
|
|
|
|
int failed = 0;
|
|
|
|
assert( 0!=pFile->locktype );
|
|
|
|
if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
|
|
|
|
/* Acquire a RESERVED lock */
|
2008-08-22 04:22:35 +04:00
|
|
|
failed = _AFPFSSetLock(context->filePath, pFile, RESERVED_BYTE, 1,1);
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
if (!failed && locktype == EXCLUSIVE_LOCK) {
|
|
|
|
/* Acquire an EXCLUSIVE lock */
|
|
|
|
|
|
|
|
/* Remove the shared lock before trying the range. we'll need to
|
2008-06-28 15:23:00 +04:00
|
|
|
** reestablish the shared lock if we can't get the afpUnlock
|
2006-10-03 21:40:40 +04:00
|
|
|
*/
|
2008-08-22 04:22:35 +04:00
|
|
|
if (!(failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST +
|
|
|
|
context->sharedLockByte, 1, 0))) {
|
2006-10-03 21:40:40 +04:00
|
|
|
/* now attemmpt to get the exclusive lock range */
|
2008-08-22 04:22:35 +04:00
|
|
|
failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST,
|
2006-10-03 21:40:40 +04:00
|
|
|
SHARED_SIZE, 1);
|
2008-08-22 04:22:35 +04:00
|
|
|
if (failed && (failed = _AFPFSSetLock(context->filePath, pFile,
|
|
|
|
SHARED_FIRST + context->sharedLockByte, 1, 1))) {
|
|
|
|
rc = failed;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
} else {
|
2008-08-22 04:22:35 +04:00
|
|
|
rc = failed;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
}
|
2008-08-22 04:22:35 +04:00
|
|
|
if( failed ){
|
|
|
|
rc = failed;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
pFile->locktype = locktype;
|
|
|
|
}else if( locktype==EXCLUSIVE_LOCK ){
|
|
|
|
pFile->locktype = PENDING_LOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
afp_end_lock:
|
2008-03-07 18:34:11 +03:00
|
|
|
leaveMutex();
|
2007-03-27 02:05:01 +04:00
|
|
|
OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
|
2006-10-03 21:40:40 +04:00
|
|
|
rc==SQLITE_OK ? "ok" : "failed");
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-03-07 18:34:11 +03:00
|
|
|
** Lower the locking level on file descriptor pFile to locktype. locktype
|
|
|
|
** must be either NO_LOCK or SHARED_LOCK.
|
|
|
|
**
|
|
|
|
** If the locking level of the file descriptor is already at or below
|
|
|
|
** the requested locking level, this routine is a no-op.
|
|
|
|
*/
|
2008-06-28 15:23:00 +04:00
|
|
|
static int afpUnlock(sqlite3_file *id, int locktype) {
|
2006-10-03 21:40:40 +04:00
|
|
|
int rc = SQLITE_OK;
|
|
|
|
unixFile *pFile = (unixFile*)id;
|
|
|
|
afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
|
|
|
|
|
|
|
|
assert( pFile );
|
2007-03-27 02:05:01 +04:00
|
|
|
OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
|
2006-10-03 21:40:40 +04:00
|
|
|
pFile->locktype, getpid());
|
2008-08-22 04:22:35 +04:00
|
|
|
|
2006-10-03 21:40:40 +04:00
|
|
|
assert( locktype<=SHARED_LOCK );
|
|
|
|
if( pFile->locktype<=locktype ){
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
if( CHECK_THREADID(pFile) ){
|
|
|
|
return SQLITE_MISUSE;
|
|
|
|
}
|
2007-08-17 19:53:36 +04:00
|
|
|
enterMutex();
|
2008-08-22 04:22:35 +04:00
|
|
|
int failed = SQLITE_OK;
|
2006-10-03 21:40:40 +04:00
|
|
|
if( pFile->locktype>SHARED_LOCK ){
|
|
|
|
if( locktype==SHARED_LOCK ){
|
|
|
|
|
|
|
|
/* unlock the exclusive range - then re-establish the shared lock */
|
|
|
|
if (pFile->locktype==EXCLUSIVE_LOCK) {
|
2008-08-22 04:22:35 +04:00
|
|
|
failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST,
|
2006-10-03 21:40:40 +04:00
|
|
|
SHARED_SIZE, 0);
|
|
|
|
if (!failed) {
|
|
|
|
/* successfully removed the exclusive lock */
|
2008-08-22 04:22:35 +04:00
|
|
|
if ((failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST+
|
|
|
|
context->sharedLockByte, 1, 1))) {
|
2006-10-03 21:40:40 +04:00
|
|
|
/* failed to re-establish our shared lock */
|
2008-08-22 04:22:35 +04:00
|
|
|
rc = failed;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
} else {
|
2008-08-22 04:22:35 +04:00
|
|
|
rc = failed;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rc == SQLITE_OK && pFile->locktype>=PENDING_LOCK) {
|
2008-08-22 04:22:35 +04:00
|
|
|
if ((failed = _AFPFSSetLock(context->filePath, pFile,
|
|
|
|
PENDING_BYTE, 1, 0))){
|
2006-10-03 21:40:40 +04:00
|
|
|
/* failed to release the pending lock */
|
2008-08-22 04:22:35 +04:00
|
|
|
rc = failed;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rc == SQLITE_OK && pFile->locktype>=RESERVED_LOCK) {
|
2008-08-22 04:22:35 +04:00
|
|
|
if ((failed = _AFPFSSetLock(context->filePath, pFile,
|
|
|
|
RESERVED_BYTE, 1, 0))) {
|
2006-10-03 21:40:40 +04:00
|
|
|
/* failed to release the reserved lock */
|
2008-08-22 04:22:35 +04:00
|
|
|
rc = failed;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( locktype==NO_LOCK ){
|
2008-08-22 04:22:35 +04:00
|
|
|
int failed = _AFPFSSetLock(context->filePath, pFile,
|
2006-10-03 21:40:40 +04:00
|
|
|
SHARED_FIRST + context->sharedLockByte, 1, 0);
|
|
|
|
if (failed) {
|
2008-08-22 04:22:35 +04:00
|
|
|
rc = failed;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rc == SQLITE_OK)
|
|
|
|
pFile->locktype = locktype;
|
2007-08-17 19:53:36 +04:00
|
|
|
leaveMutex();
|
2006-10-03 21:40:40 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-03-07 18:34:11 +03:00
|
|
|
** Close a file & cleanup AFP specific locking context
|
|
|
|
*/
|
2008-06-28 15:23:00 +04:00
|
|
|
static int afpClose(sqlite3_file *id) {
|
|
|
|
if( id ){
|
|
|
|
unixFile *pFile = (unixFile*)id;
|
|
|
|
afpUnlock(id, NO_LOCK);
|
|
|
|
sqlite3_free(pFile->lockingContext);
|
|
|
|
}
|
|
|
|
return closeUnixFile(id);
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark flock() style locking
|
|
|
|
|
|
|
|
/*
|
2008-03-07 18:34:11 +03:00
|
|
|
** The flockLockingContext is not used
|
|
|
|
*/
|
2006-10-03 21:40:40 +04:00
|
|
|
typedef void flockLockingContext;
|
|
|
|
|
2008-08-22 04:22:35 +04:00
|
|
|
/* flock-style reserved lock checking following the behavior of
|
|
|
|
** unixCheckReservedLock, see the unixCheckReservedLock function comments */
|
2008-06-28 15:23:00 +04:00
|
|
|
static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
|
2008-08-22 04:22:35 +04:00
|
|
|
int rc = SQLITE_OK;
|
|
|
|
int reserved = 0;
|
2006-10-03 21:40:40 +04:00
|
|
|
unixFile *pFile = (unixFile*)id;
|
|
|
|
|
2008-08-22 04:22:35 +04:00
|
|
|
SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
|
|
|
|
|
|
|
|
assert( pFile );
|
|
|
|
|
|
|
|
/* Check if a thread in this process holds such a lock */
|
|
|
|
if( pFile->locktype>SHARED_LOCK ){
|
|
|
|
reserved = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise see if some other process holds it. */
|
|
|
|
if( !reserved ){
|
2007-06-08 22:27:03 +04:00
|
|
|
/* attempt to get the lock */
|
2008-08-22 04:22:35 +04:00
|
|
|
int lrc = flock(pFile->h, LOCK_EX | LOCK_NB);
|
|
|
|
if( !lrc ){
|
2007-06-08 22:27:03 +04:00
|
|
|
/* got the lock, unlock it */
|
2008-08-22 04:22:35 +04:00
|
|
|
lrc = flock(pFile->h, LOCK_UN);
|
|
|
|
if ( lrc ) {
|
|
|
|
int tErrno = errno;
|
|
|
|
/* unlock failed with an error */
|
|
|
|
lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
|
|
|
|
if( IS_LOCK_ERROR(lrc) ){
|
|
|
|
pFile->lastErrno = tErrno;
|
|
|
|
rc = lrc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int tErrno = errno;
|
|
|
|
reserved = 1;
|
|
|
|
/* someone else might have it reserved */
|
|
|
|
lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
|
|
|
|
if( IS_LOCK_ERROR(lrc) ){
|
|
|
|
pFile->lastErrno = tErrno;
|
|
|
|
rc = lrc;
|
|
|
|
}
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
}
|
2008-08-22 04:22:35 +04:00
|
|
|
OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
|
2008-06-05 15:39:11 +04:00
|
|
|
|
2008-08-22 04:22:35 +04:00
|
|
|
*pResOut = reserved;
|
|
|
|
return rc;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
|
2008-06-28 15:23:00 +04:00
|
|
|
static int flockLock(sqlite3_file *id, int locktype) {
|
2008-08-22 04:22:35 +04:00
|
|
|
int rc = SQLITE_OK;
|
2006-10-03 21:40:40 +04:00
|
|
|
unixFile *pFile = (unixFile*)id;
|
2008-08-22 04:22:35 +04:00
|
|
|
|
|
|
|
assert( pFile );
|
|
|
|
|
2007-06-08 22:27:03 +04:00
|
|
|
/* if we already have a lock, it is exclusive.
|
|
|
|
** Just adjust level and punt on outta here. */
|
2006-10-03 21:40:40 +04:00
|
|
|
if (pFile->locktype > NO_LOCK) {
|
|
|
|
pFile->locktype = locktype;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2007-06-08 22:27:03 +04:00
|
|
|
/* grab an exclusive lock */
|
2008-08-22 04:22:35 +04:00
|
|
|
|
|
|
|
if (flock(pFile->h, LOCK_EX | LOCK_NB)) {
|
|
|
|
int tErrno = errno;
|
2007-06-08 22:27:03 +04:00
|
|
|
/* didn't get, must be busy */
|
2008-08-22 04:22:35 +04:00
|
|
|
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
|
|
|
|
if( IS_LOCK_ERROR(rc) ){
|
|
|
|
pFile->lastErrno = tErrno;
|
|
|
|
}
|
2006-10-03 21:40:40 +04:00
|
|
|
} else {
|
2007-06-08 22:27:03 +04:00
|
|
|
/* got it, set the type and return ok */
|
2006-10-03 21:40:40 +04:00
|
|
|
pFile->locktype = locktype;
|
|
|
|
}
|
2008-08-22 04:22:35 +04:00
|
|
|
OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
|
|
|
|
rc==SQLITE_OK ? "ok" : "failed");
|
|
|
|
return rc;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
|
2008-06-28 15:23:00 +04:00
|
|
|
static int flockUnlock(sqlite3_file *id, int locktype) {
|
2006-10-03 21:40:40 +04:00
|
|
|
unixFile *pFile = (unixFile*)id;
|
|
|
|
|
2008-08-22 04:22:35 +04:00
|
|
|
assert( pFile );
|
|
|
|
OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
|
|
|
|
pFile->locktype, getpid());
|
2006-10-03 21:40:40 +04:00
|
|
|
assert( locktype<=SHARED_LOCK );
|
|
|
|
|
2007-06-08 22:27:03 +04:00
|
|
|
/* no-op if possible */
|
2006-10-03 21:40:40 +04:00
|
|
|
if( pFile->locktype==locktype ){
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2007-06-08 22:27:03 +04:00
|
|
|
/* shared can just be set because we always have an exclusive */
|
2006-10-03 21:40:40 +04:00
|
|
|
if (locktype==SHARED_LOCK) {
|
|
|
|
pFile->locktype = locktype;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2007-06-08 22:27:03 +04:00
|
|
|
/* no, really, unlock. */
|
2006-10-03 21:40:40 +04:00
|
|
|
int rc = flock(pFile->h, LOCK_UN);
|
2008-08-22 04:22:35 +04:00
|
|
|
if (rc) {
|
|
|
|
int r, tErrno = errno;
|
|
|
|
r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
|
|
|
|
if( IS_LOCK_ERROR(r) ){
|
|
|
|
pFile->lastErrno = tErrno;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
} else {
|
2006-10-03 21:40:40 +04:00
|
|
|
pFile->locktype = NO_LOCK;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-03-07 18:34:11 +03:00
|
|
|
** Close a file.
|
|
|
|
*/
|
2008-06-28 15:23:00 +04:00
|
|
|
static int flockClose(sqlite3_file *id) {
|
|
|
|
if( id ){
|
|
|
|
flockUnlock(id, NO_LOCK);
|
|
|
|
}
|
|
|
|
return closeUnixFile(id);
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark Old-School .lock file based locking
|
|
|
|
|
2008-08-22 04:22:35 +04:00
|
|
|
/* Dotlock-style reserved lock checking following the behavior of
|
|
|
|
** unixCheckReservedLock, see the unixCheckReservedLock function comments */
|
2008-06-28 15:23:00 +04:00
|
|
|
static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
|
2008-08-22 04:22:35 +04:00
|
|
|
int rc = SQLITE_OK;
|
|
|
|
int reserved = 0;
|
2006-10-03 21:40:40 +04:00
|
|
|
unixFile *pFile = (unixFile*)id;
|
2008-03-07 18:34:11 +03:00
|
|
|
|
2008-08-22 04:22:35 +04:00
|
|
|
SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
|
|
|
|
|
|
|
|
assert( pFile );
|
|
|
|
|
|
|
|
/* Check if a thread in this process holds such a lock */
|
|
|
|
if( pFile->locktype>SHARED_LOCK ){
|
|
|
|
reserved = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise see if some other process holds it. */
|
|
|
|
if( !reserved ){
|
|
|
|
char *zLockFile = (char *)pFile->lockingContext;
|
2006-10-03 21:40:40 +04:00
|
|
|
struct stat statBuf;
|
2008-08-22 04:22:35 +04:00
|
|
|
|
|
|
|
if( lstat(zLockFile, &statBuf)==0 ){
|
|
|
|
/* file exists, someone else has the lock */
|
|
|
|
reserved = 1;
|
|
|
|
}else{
|
2007-06-08 22:27:03 +04:00
|
|
|
/* file does not exist, we could have it if we want it */
|
2008-08-22 04:22:35 +04:00
|
|
|
int tErrno = errno;
|
|
|
|
if( ENOENT != tErrno ){
|
|
|
|
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
|
|
|
|
pFile->lastErrno = tErrno;
|
|
|
|
}
|
2008-03-07 18:34:11 +03:00
|
|
|
}
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
2008-08-22 04:22:35 +04:00
|
|
|
OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
|
2008-06-05 15:39:11 +04:00
|
|
|
|
2008-08-22 04:22:35 +04:00
|
|
|
*pResOut = reserved;
|
|
|
|
return rc;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
|
2008-06-28 15:23:00 +04:00
|
|
|
static int dotlockLock(sqlite3_file *id, int locktype) {
|
2006-10-03 21:40:40 +04:00
|
|
|
unixFile *pFile = (unixFile*)id;
|
2008-03-07 18:34:11 +03:00
|
|
|
int fd;
|
2008-06-28 15:23:00 +04:00
|
|
|
char *zLockFile = (char *)pFile->lockingContext;
|
2008-08-22 04:22:35 +04:00
|
|
|
int rc=SQLITE_OK;
|
2008-03-07 18:34:11 +03:00
|
|
|
|
2007-06-08 22:27:03 +04:00
|
|
|
/* if we already have a lock, it is exclusive.
|
|
|
|
** Just adjust level and punt on outta here. */
|
2006-10-03 21:40:40 +04:00
|
|
|
if (pFile->locktype > NO_LOCK) {
|
|
|
|
pFile->locktype = locktype;
|
|
|
|
|
|
|
|
/* Always update the timestamp on the old file */
|
2008-06-28 15:23:00 +04:00
|
|
|
utimes(zLockFile, NULL);
|
2008-08-22 04:22:35 +04:00
|
|
|
rc = SQLITE_OK;
|
|
|
|
goto dotlock_end_lock;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
|
2007-06-08 22:27:03 +04:00
|
|
|
/* check to see if lock file already exists */
|
2006-10-03 21:40:40 +04:00
|
|
|
struct stat statBuf;
|
2008-06-28 15:23:00 +04:00
|
|
|
if (lstat(zLockFile,&statBuf) == 0){
|
2008-08-22 04:22:35 +04:00
|
|
|
rc = SQLITE_BUSY; /* it does, busy */
|
|
|
|
goto dotlock_end_lock;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
|
2007-06-08 22:27:03 +04:00
|
|
|
/* grab an exclusive lock */
|
2008-06-28 15:23:00 +04:00
|
|
|
fd = open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600);
|
2008-03-07 18:34:11 +03:00
|
|
|
if( fd<0 ){
|
2007-06-08 22:27:03 +04:00
|
|
|
/* failed to open/create the file, someone else may have stolen the lock */
|
2008-08-22 04:22:35 +04:00
|
|
|
int tErrno = errno;
|
|
|
|
if( EEXIST == tErrno ){
|
|
|
|
rc = SQLITE_BUSY;
|
|
|
|
} else {
|
|
|
|
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
|
|
|
|
if( IS_LOCK_ERROR(rc) ){
|
|
|
|
pFile->lastErrno = tErrno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto dotlock_end_lock;
|
|
|
|
}
|
2006-10-03 21:40:40 +04:00
|
|
|
close(fd);
|
|
|
|
|
2007-06-08 22:27:03 +04:00
|
|
|
/* got it, set the type and return ok */
|
2006-10-03 21:40:40 +04:00
|
|
|
pFile->locktype = locktype;
|
2008-08-22 04:22:35 +04:00
|
|
|
|
|
|
|
dotlock_end_lock:
|
|
|
|
return rc;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
|
2008-06-28 15:23:00 +04:00
|
|
|
static int dotlockUnlock(sqlite3_file *id, int locktype) {
|
2006-10-03 21:40:40 +04:00
|
|
|
unixFile *pFile = (unixFile*)id;
|
2008-06-28 15:23:00 +04:00
|
|
|
char *zLockFile = (char *)pFile->lockingContext;
|
2008-03-07 18:34:11 +03:00
|
|
|
|
2008-08-22 04:22:35 +04:00
|
|
|
assert( pFile );
|
|
|
|
OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
|
|
|
|
pFile->locktype, getpid());
|
2006-10-03 21:40:40 +04:00
|
|
|
assert( locktype<=SHARED_LOCK );
|
|
|
|
|
2007-06-08 22:27:03 +04:00
|
|
|
/* no-op if possible */
|
2006-10-03 21:40:40 +04:00
|
|
|
if( pFile->locktype==locktype ){
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2007-06-08 22:27:03 +04:00
|
|
|
/* shared can just be set because we always have an exclusive */
|
2006-10-03 21:40:40 +04:00
|
|
|
if (locktype==SHARED_LOCK) {
|
|
|
|
pFile->locktype = locktype;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2007-06-08 22:27:03 +04:00
|
|
|
/* no, really, unlock. */
|
2008-08-22 04:22:35 +04:00
|
|
|
if (unlink(zLockFile) ) {
|
|
|
|
int rc, tErrno = errno;
|
|
|
|
if( ENOENT != tErrno ){
|
|
|
|
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
|
|
|
|
}
|
|
|
|
if( IS_LOCK_ERROR(rc) ){
|
|
|
|
pFile->lastErrno = tErrno;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
2006-10-03 21:40:40 +04:00
|
|
|
pFile->locktype = NO_LOCK;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Close a file.
|
|
|
|
*/
|
2008-06-28 15:23:00 +04:00
|
|
|
static int dotlockClose(sqlite3_file *id) {
|
|
|
|
if( id ){
|
|
|
|
unixFile *pFile = (unixFile*)id;
|
|
|
|
dotlockUnlock(id, NO_LOCK);
|
|
|
|
sqlite3_free(pFile->lockingContext);
|
|
|
|
}
|
|
|
|
return closeUnixFile(id);
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-30 19:27:54 +04:00
|
|
|
#endif /* SQLITE_ENABLE_LOCKING_STYLE */
|
2006-10-03 21:40:40 +04:00
|
|
|
|
|
|
|
/*
|
2008-03-07 18:34:11 +03:00
|
|
|
** The nolockLockingContext is void
|
|
|
|
*/
|
2006-10-03 21:40:40 +04:00
|
|
|
typedef void nolockLockingContext;
|
|
|
|
|
2008-06-28 15:23:00 +04:00
|
|
|
static int nolockCheckReservedLock(sqlite3_file *id, int *pResOut) {
|
2008-06-05 15:39:11 +04:00
|
|
|
*pResOut = 0;
|
|
|
|
return SQLITE_OK;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
|
2008-06-28 15:23:00 +04:00
|
|
|
static int nolockLock(sqlite3_file *id, int locktype) {
|
2006-10-03 21:40:40 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2008-06-28 15:23:00 +04:00
|
|
|
static int nolockUnlock(sqlite3_file *id, int locktype) {
|
2006-10-03 21:40:40 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-03-07 18:34:11 +03:00
|
|
|
** Close a file.
|
|
|
|
*/
|
2008-06-28 15:23:00 +04:00
|
|
|
static int nolockClose(sqlite3_file *id) {
|
|
|
|
return closeUnixFile(id);
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
|
|
|
|
2007-08-20 10:44:22 +04:00
|
|
|
|
2005-11-26 06:43:23 +03:00
|
|
|
/*
|
2007-08-31 22:34:59 +04:00
|
|
|
** Information and control of an open file handle.
|
2005-11-29 06:13:21 +03:00
|
|
|
*/
|
2007-08-31 20:11:35 +04:00
|
|
|
static int unixFileControl(sqlite3_file *id, int op, void *pArg){
|
2007-08-31 22:34:59 +04:00
|
|
|
switch( op ){
|
|
|
|
case SQLITE_FCNTL_LOCKSTATE: {
|
|
|
|
*(int*)pArg = ((unixFile*)id)->locktype;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
}
|
2007-08-31 20:11:35 +04:00
|
|
|
return SQLITE_ERROR;
|
2005-11-29 06:13:21 +03:00
|
|
|
}
|
|
|
|
|
2007-03-23 13:08:38 +03:00
|
|
|
/*
|
|
|
|
** Return the sector size in bytes of the underlying block device for
|
|
|
|
** the specified file. This is almost always 512 bytes, but may be
|
|
|
|
** larger for some devices.
|
|
|
|
**
|
|
|
|
** SQLite code assumes this function cannot fail. It also assumes that
|
|
|
|
** if two files are created in the same file-system directory (i.e.
|
2007-12-14 00:54:09 +03:00
|
|
|
** a database and its journal file) that the sector size will be the
|
2007-03-23 13:08:38 +03:00
|
|
|
** same for both.
|
|
|
|
*/
|
2007-08-15 21:08:46 +04:00
|
|
|
static int unixSectorSize(sqlite3_file *id){
|
2007-03-29 22:19:52 +04:00
|
|
|
return SQLITE_DEFAULT_SECTOR_SIZE;
|
2007-03-23 13:08:38 +03:00
|
|
|
}
|
|
|
|
|
2007-08-17 20:50:38 +04:00
|
|
|
/*
|
|
|
|
** Return the device characteristics for the file. This is always 0.
|
|
|
|
*/
|
2007-08-15 21:08:46 +04:00
|
|
|
static int unixDeviceCharacteristics(sqlite3_file *id){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-26 03:25:00 +03:00
|
|
|
/*
|
2008-06-28 15:23:00 +04:00
|
|
|
** Initialize the contents of the unixFile structure pointed to by pId.
|
|
|
|
**
|
2007-08-20 10:44:22 +04:00
|
|
|
** When locking extensions are enabled, the filepath and locking style
|
|
|
|
** are needed to determine the unixFile pMethod to use for locking operations.
|
|
|
|
** The locking-style specific lockingContext data structure is created
|
|
|
|
** and assigned here also.
|
|
|
|
*/
|
|
|
|
static int fillInUnixFile(
|
2008-06-28 15:23:00 +04:00
|
|
|
sqlite3_vfs *pVfs, /* Pointer to vfs object */
|
2006-10-03 21:40:40 +04:00
|
|
|
int h, /* Open file descriptor of file being opened */
|
2007-08-20 10:44:22 +04:00
|
|
|
int dirfd, /* Directory file descriptor */
|
2008-03-07 03:27:10 +03:00
|
|
|
sqlite3_file *pId, /* Write to the unixFile structure here */
|
2008-07-30 19:27:54 +04:00
|
|
|
const char *zFilename, /* Name of the file being opened */
|
|
|
|
int noLock /* Omit locking if true */
|
2006-10-03 21:40:40 +04:00
|
|
|
){
|
2008-07-30 19:27:54 +04:00
|
|
|
int eLockingStyle;
|
|
|
|
unixFile *pNew = (unixFile *)pId;
|
|
|
|
int rc = SQLITE_OK;
|
|
|
|
|
2008-06-28 15:23:00 +04:00
|
|
|
/* Macro to define the static contents of an sqlite3_io_methods
|
|
|
|
** structure for a unix backend file. Different locking methods
|
|
|
|
** require different functions for the xClose, xLock, xUnlock and
|
|
|
|
** xCheckReservedLock methods.
|
|
|
|
*/
|
|
|
|
#define IOMETHODS(xClose, xLock, xUnlock, xCheckReservedLock) { \
|
|
|
|
1, /* iVersion */ \
|
|
|
|
xClose, /* xClose */ \
|
|
|
|
unixRead, /* xRead */ \
|
|
|
|
unixWrite, /* xWrite */ \
|
|
|
|
unixTruncate, /* xTruncate */ \
|
|
|
|
unixSync, /* xSync */ \
|
|
|
|
unixFileSize, /* xFileSize */ \
|
|
|
|
xLock, /* xLock */ \
|
|
|
|
xUnlock, /* xUnlock */ \
|
|
|
|
xCheckReservedLock, /* xCheckReservedLock */ \
|
|
|
|
unixFileControl, /* xFileControl */ \
|
|
|
|
unixSectorSize, /* xSectorSize */ \
|
|
|
|
unixDeviceCharacteristics /* xDeviceCapabilities */ \
|
|
|
|
}
|
|
|
|
static sqlite3_io_methods aIoMethod[] = {
|
|
|
|
IOMETHODS(unixClose, unixLock, unixUnlock, unixCheckReservedLock)
|
2008-07-30 19:27:54 +04:00
|
|
|
,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
|
2008-06-28 15:23:00 +04:00
|
|
|
#ifdef SQLITE_ENABLE_LOCKING_STYLE
|
|
|
|
,IOMETHODS(dotlockClose, dotlockLock, dotlockUnlock,dotlockCheckReservedLock)
|
2008-07-30 19:27:54 +04:00
|
|
|
,IOMETHODS(flockClose, flockLock, flockUnlock, flockCheckReservedLock)
|
2008-06-28 15:23:00 +04:00
|
|
|
,IOMETHODS(afpClose, afpLock, afpUnlock, afpCheckReservedLock)
|
2008-03-07 03:27:10 +03:00
|
|
|
#endif
|
2008-06-28 15:23:00 +04:00
|
|
|
};
|
2008-07-30 19:27:54 +04:00
|
|
|
/* The order of the IOMETHODS macros above is important. It must be the
|
|
|
|
** same order as the LOCKING_STYLE numbers
|
|
|
|
*/
|
|
|
|
assert(LOCKING_STYLE_POSIX==1);
|
|
|
|
assert(LOCKING_STYLE_NONE==2);
|
|
|
|
assert(LOCKING_STYLE_DOTFILE==3);
|
|
|
|
assert(LOCKING_STYLE_FLOCK==4);
|
|
|
|
assert(LOCKING_STYLE_AFP==5);
|
2008-03-07 03:27:10 +03:00
|
|
|
|
2008-06-06 15:11:25 +04:00
|
|
|
assert( pNew->pLock==NULL );
|
|
|
|
assert( pNew->pOpen==NULL );
|
2008-03-07 03:27:10 +03:00
|
|
|
|
|
|
|
OSTRACE3("OPEN %-3d %s\n", h, zFilename);
|
2007-08-20 10:44:22 +04:00
|
|
|
pNew->h = h;
|
2008-03-07 03:27:10 +03:00
|
|
|
pNew->dirfd = dirfd;
|
2007-08-20 10:44:22 +04:00
|
|
|
SET_THREADID(pNew);
|
2008-03-07 18:34:11 +03:00
|
|
|
|
2008-07-30 19:27:54 +04:00
|
|
|
if( noLock ){
|
|
|
|
eLockingStyle = LOCKING_STYLE_NONE;
|
|
|
|
}else{
|
|
|
|
eLockingStyle = detectLockingStyle(pVfs, zFilename, h);
|
|
|
|
}
|
2008-06-28 15:23:00 +04:00
|
|
|
|
|
|
|
switch( eLockingStyle ){
|
|
|
|
|
|
|
|
case LOCKING_STYLE_POSIX: {
|
|
|
|
enterMutex();
|
|
|
|
rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
|
|
|
|
leaveMutex();
|
2008-03-07 03:27:10 +03:00
|
|
|
break;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
2008-06-28 15:23:00 +04:00
|
|
|
|
|
|
|
#ifdef SQLITE_ENABLE_LOCKING_STYLE
|
|
|
|
case LOCKING_STYLE_AFP: {
|
|
|
|
/* AFP locking uses the file path so it needs to be included in
|
|
|
|
** the afpLockingContext.
|
|
|
|
*/
|
|
|
|
afpLockingContext *pCtx;
|
|
|
|
pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
|
|
|
|
if( pCtx==0 ){
|
|
|
|
rc = SQLITE_NOMEM;
|
|
|
|
}else{
|
|
|
|
/* NB: zFilename exists and remains valid until the file is closed
|
|
|
|
** according to requirement F11141. So we do not need to make a
|
|
|
|
** copy of the filename. */
|
|
|
|
pCtx->filePath = zFilename;
|
|
|
|
srandomdev();
|
|
|
|
}
|
2008-03-07 03:27:10 +03:00
|
|
|
break;
|
2008-06-28 15:23:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
case LOCKING_STYLE_DOTFILE: {
|
|
|
|
/* Dotfile locking uses the file path so it needs to be included in
|
|
|
|
** the dotlockLockingContext
|
|
|
|
*/
|
|
|
|
char *zLockFile;
|
2008-03-07 03:27:10 +03:00
|
|
|
int nFilename;
|
2008-06-28 15:23:00 +04:00
|
|
|
nFilename = strlen(zFilename) + 6;
|
|
|
|
zLockFile = (char *)sqlite3_malloc(nFilename);
|
|
|
|
if( zLockFile==0 ){
|
|
|
|
rc = SQLITE_NOMEM;
|
|
|
|
}else{
|
|
|
|
sqlite3_snprintf(nFilename, zLockFile, "%s.lock", zFilename);
|
2008-03-07 18:34:11 +03:00
|
|
|
}
|
2008-06-28 15:23:00 +04:00
|
|
|
pNew->lockingContext = zLockFile;
|
2008-03-07 03:27:10 +03:00
|
|
|
break;
|
|
|
|
}
|
2005-11-30 06:20:31 +03:00
|
|
|
|
2008-06-28 15:23:00 +04:00
|
|
|
case LOCKING_STYLE_FLOCK:
|
|
|
|
case LOCKING_STYLE_NONE:
|
|
|
|
break;
|
2007-06-29 16:04:26 +04:00
|
|
|
#endif
|
2008-06-28 15:23:00 +04:00
|
|
|
}
|
2008-08-22 04:22:35 +04:00
|
|
|
|
|
|
|
pNew->lastErrno = 0;
|
2008-06-28 15:23:00 +04:00
|
|
|
if( rc!=SQLITE_OK ){
|
2007-10-30 20:28:51 +03:00
|
|
|
if( dirfd>=0 ) close(dirfd);
|
2006-10-03 21:40:40 +04:00
|
|
|
close(h);
|
2008-06-28 15:23:00 +04:00
|
|
|
}else{
|
2008-06-30 14:16:04 +04:00
|
|
|
pNew->pMethod = &aIoMethod[eLockingStyle-1];
|
2008-06-28 15:23:00 +04:00
|
|
|
OpenCounter(+1);
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
2008-06-28 15:23:00 +04:00
|
|
|
return rc;
|
2006-10-03 21:40:40 +04:00
|
|
|
}
|
2005-11-26 03:25:00 +03:00
|
|
|
|
2007-08-20 10:44:22 +04:00
|
|
|
/*
|
|
|
|
** Open a file descriptor to the directory containing file zFilename.
|
|
|
|
** If successful, *pFd is set to the opened file descriptor and
|
|
|
|
** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
|
|
|
|
** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
|
|
|
|
** value.
|
|
|
|
**
|
|
|
|
** If SQLITE_OK is returned, the caller is responsible for closing
|
|
|
|
** the file descriptor *pFd using close().
|
|
|
|
*/
|
2007-08-18 14:59:19 +04:00
|
|
|
static int openDirectory(const char *zFilename, int *pFd){
|
|
|
|
int ii;
|
2007-09-20 14:02:54 +04:00
|
|
|
int fd = -1;
|
2007-08-23 00:18:21 +04:00
|
|
|
char zDirname[MAX_PATHNAME+1];
|
2007-08-18 14:59:19 +04:00
|
|
|
|
2007-08-24 07:51:33 +04:00
|
|
|
sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
|
2007-08-18 14:59:19 +04:00
|
|
|
for(ii=strlen(zDirname); ii>=0 && zDirname[ii]!='/'; ii--);
|
|
|
|
if( ii>0 ){
|
|
|
|
zDirname[ii] = '\0';
|
|
|
|
fd = open(zDirname, O_RDONLY|O_BINARY, 0);
|
2007-09-20 14:02:54 +04:00
|
|
|
if( fd>=0 ){
|
2007-08-18 14:59:19 +04:00
|
|
|
#ifdef FD_CLOEXEC
|
|
|
|
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
|
|
|
|
#endif
|
|
|
|
OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*pFd = fd;
|
2007-09-20 14:02:54 +04:00
|
|
|
return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN);
|
2007-08-18 14:59:19 +04:00
|
|
|
}
|
|
|
|
|
2008-06-06 15:11:25 +04:00
|
|
|
/*
|
|
|
|
** Create a temporary file name in zBuf. zBuf must be allocated
|
|
|
|
** by the calling process and must be big enough to hold at least
|
|
|
|
** pVfs->mxPathname bytes.
|
|
|
|
*/
|
|
|
|
static int getTempname(int nBuf, char *zBuf){
|
|
|
|
static const char *azDirs[] = {
|
|
|
|
0,
|
|
|
|
"/var/tmp",
|
|
|
|
"/usr/tmp",
|
|
|
|
"/tmp",
|
|
|
|
".",
|
|
|
|
};
|
|
|
|
static const unsigned char zChars[] =
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"0123456789";
|
|
|
|
int i, j;
|
|
|
|
struct stat buf;
|
|
|
|
const char *zDir = ".";
|
|
|
|
|
|
|
|
/* It's odd to simulate an io-error here, but really this is just
|
|
|
|
** using the io-error infrastructure to test that SQLite handles this
|
|
|
|
** function failing.
|
|
|
|
*/
|
|
|
|
SimulateIOError( return SQLITE_IOERR );
|
|
|
|
|
|
|
|
azDirs[0] = sqlite3_temp_directory;
|
|
|
|
for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
|
|
|
|
if( azDirs[i]==0 ) continue;
|
|
|
|
if( stat(azDirs[i], &buf) ) continue;
|
|
|
|
if( !S_ISDIR(buf.st_mode) ) continue;
|
|
|
|
if( access(azDirs[i], 07) ) continue;
|
|
|
|
zDir = azDirs[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that the output buffer is large enough for the temporary file
|
|
|
|
** name. If it is not, return SQLITE_ERROR.
|
|
|
|
*/
|
|
|
|
if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= nBuf ){
|
|
|
|
return SQLITE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
do{
|
|
|
|
sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
|
|
|
|
j = strlen(zBuf);
|
|
|
|
sqlite3_randomness(15, &zBuf[j]);
|
|
|
|
for(i=0; i<15; i++, j++){
|
|
|
|
zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
|
|
|
|
}
|
|
|
|
zBuf[j] = 0;
|
|
|
|
}while( access(zBuf,0)==0 );
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-17 19:53:36 +04:00
|
|
|
/*
|
2007-08-20 10:44:22 +04:00
|
|
|
** Open the file zPath.
|
|
|
|
**
|
2007-08-17 19:53:36 +04:00
|
|
|
** Previously, the SQLite OS layer used three functions in place of this
|
|
|
|
** one:
|
|
|
|
**
|
|
|
|
** sqlite3OsOpenReadWrite();
|
|
|
|
** sqlite3OsOpenReadOnly();
|
|
|
|
** sqlite3OsOpenExclusive();
|
|
|
|
**
|
|
|
|
** These calls correspond to the following combinations of flags:
|
|
|
|
**
|
|
|
|
** ReadWrite() -> (READWRITE | CREATE)
|
|
|
|
** ReadOnly() -> (READONLY)
|
|
|
|
** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
|
|
|
|
**
|
|
|
|
** The old OpenExclusive() accepted a boolean argument - "delFlag". If
|
|
|
|
** true, the file was configured to be automatically deleted when the
|
|
|
|
** file handle closed. To achieve the same effect using this new
|
|
|
|
** interface, add the DELETEONCLOSE flag to those specified above for
|
|
|
|
** OpenExclusive().
|
|
|
|
*/
|
|
|
|
static int unixOpen(
|
2007-08-24 07:51:33 +04:00
|
|
|
sqlite3_vfs *pVfs,
|
2007-08-17 19:53:36 +04:00
|
|
|
const char *zPath,
|
|
|
|
sqlite3_file *pFile,
|
|
|
|
int flags,
|
|
|
|
int *pOutFlags
|
|
|
|
){
|
2007-08-18 14:59:19 +04:00
|
|
|
int fd = 0; /* File descriptor returned by open() */
|
|
|
|
int dirfd = -1; /* Directory file descriptor */
|
|
|
|
int oflags = 0; /* Flags to pass to open() */
|
|
|
|
int eType = flags&0xFFFFFF00; /* Type of file to open */
|
2008-07-30 19:27:54 +04:00
|
|
|
int noLock; /* True to omit locking primitives */
|
2007-08-17 19:53:36 +04:00
|
|
|
|
|
|
|
int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
|
|
|
|
int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
|
|
|
|
int isCreate = (flags & SQLITE_OPEN_CREATE);
|
|
|
|
int isReadonly = (flags & SQLITE_OPEN_READONLY);
|
|
|
|
int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
|
|
|
|
|
2007-08-18 14:59:19 +04:00
|
|
|
/* If creating a master or main-file journal, this function will open
|
|
|
|
** a file-descriptor on the directory too. The first time unixSync()
|
|
|
|
** is called the directory file descriptor will be fsync()ed and close()d.
|
|
|
|
*/
|
|
|
|
int isOpenDirectory = (isCreate &&
|
|
|
|
(eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL)
|
|
|
|
);
|
2007-08-17 19:53:36 +04:00
|
|
|
|
2008-06-06 15:11:25 +04:00
|
|
|
/* If argument zPath is a NULL pointer, this function is required to open
|
|
|
|
** a temporary file. Use this buffer to store the file name in.
|
|
|
|
*/
|
|
|
|
char zTmpname[MAX_PATHNAME+1];
|
|
|
|
const char *zName = zPath;
|
|
|
|
|
2007-08-18 14:59:19 +04:00
|
|
|
/* Check the following statements are true:
|
|
|
|
**
|
|
|
|
** (a) Exactly one of the READWRITE and READONLY flags must be set, and
|
|
|
|
** (b) if CREATE is set, then READWRITE must also be set, and
|
|
|
|
** (c) if EXCLUSIVE is set, then CREATE must also be set.
|
2007-09-03 19:19:34 +04:00
|
|
|
** (d) if DELETEONCLOSE is set, then CREATE must also be set.
|
2007-08-18 14:59:19 +04:00
|
|
|
*/
|
|
|
|
assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
|
2007-08-17 19:53:36 +04:00
|
|
|
assert(isCreate==0 || isReadWrite);
|
|
|
|
assert(isExclusive==0 || isCreate);
|
2007-09-03 19:19:34 +04:00
|
|
|
assert(isDelete==0 || isCreate);
|
|
|
|
|
|
|
|
/* The main DB, main journal, and master journal are never automatically
|
|
|
|
** deleted
|
|
|
|
*/
|
|
|
|
assert( eType!=SQLITE_OPEN_MAIN_DB || !isDelete );
|
|
|
|
assert( eType!=SQLITE_OPEN_MAIN_JOURNAL || !isDelete );
|
|
|
|
assert( eType!=SQLITE_OPEN_MASTER_JOURNAL || !isDelete );
|
2007-08-17 19:53:36 +04:00
|
|
|
|
2007-08-18 14:59:19 +04:00
|
|
|
/* Assert that the upper layer has set one of the "file-type" flags. */
|
|
|
|
assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
|
|
|
|
|| eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
|
|
|
|
|| eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
|
2007-09-03 19:19:34 +04:00
|
|
|
|| eType==SQLITE_OPEN_TRANSIENT_DB
|
2007-08-18 14:59:19 +04:00
|
|
|
);
|
|
|
|
|
2008-06-28 15:23:00 +04:00
|
|
|
memset(pFile, 0, sizeof(unixFile));
|
|
|
|
|
2008-06-06 15:11:25 +04:00
|
|
|
if( !zName ){
|
|
|
|
int rc;
|
|
|
|
assert(isDelete && !isOpenDirectory);
|
|
|
|
rc = getTempname(MAX_PATHNAME+1, zTmpname);
|
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
zName = zTmpname;
|
|
|
|
}
|
|
|
|
|
2007-08-17 19:53:36 +04:00
|
|
|
if( isReadonly ) oflags |= O_RDONLY;
|
|
|
|
if( isReadWrite ) oflags |= O_RDWR;
|
|
|
|
if( isCreate ) oflags |= O_CREAT;
|
|
|
|
if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW);
|
|
|
|
oflags |= (O_LARGEFILE|O_BINARY);
|
|
|
|
|
2008-06-06 15:11:25 +04:00
|
|
|
fd = open(zName, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
|
2007-08-30 20:13:33 +04:00
|
|
|
if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
|
2007-08-17 19:53:36 +04:00
|
|
|
/* Failed to open the file for read/write access. Try read-only. */
|
|
|
|
flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
|
|
|
|
flags |= SQLITE_OPEN_READONLY;
|
2007-08-24 07:51:33 +04:00
|
|
|
return unixOpen(pVfs, zPath, pFile, flags, pOutFlags);
|
2007-08-17 19:53:36 +04:00
|
|
|
}
|
|
|
|
if( fd<0 ){
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
}
|
|
|
|
if( isDelete ){
|
2008-06-06 15:11:25 +04:00
|
|
|
unlink(zName);
|
2007-08-17 19:53:36 +04:00
|
|
|
}
|
|
|
|
if( pOutFlags ){
|
|
|
|
*pOutFlags = flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(fd!=0);
|
2007-08-18 14:59:19 +04:00
|
|
|
if( isOpenDirectory ){
|
|
|
|
int rc = openDirectory(zPath, &dirfd);
|
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
close(fd);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
2008-06-28 15:23:00 +04:00
|
|
|
|
|
|
|
#ifdef FD_CLOEXEC
|
|
|
|
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
|
|
|
|
#endif
|
|
|
|
|
2008-07-30 19:27:54 +04:00
|
|
|
noLock = eType!=SQLITE_OPEN_MAIN_DB;
|
|
|
|
return fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock);
|
2007-08-17 19:53:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-08-18 14:59:19 +04:00
|
|
|
** Delete the file at zPath. If the dirSync argument is true, fsync()
|
|
|
|
** the directory after deleting the file.
|
2007-08-17 19:53:36 +04:00
|
|
|
*/
|
2007-08-24 07:51:33 +04:00
|
|
|
static int unixDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
|
2007-08-18 14:59:19 +04:00
|
|
|
int rc = SQLITE_OK;
|
2007-08-17 19:53:36 +04:00
|
|
|
SimulateIOError(return SQLITE_IOERR_DELETE);
|
|
|
|
unlink(zPath);
|
2007-08-18 14:59:19 +04:00
|
|
|
if( dirSync ){
|
|
|
|
int fd;
|
|
|
|
rc = openDirectory(zPath, &fd);
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
if( fsync(fd) ){
|
|
|
|
rc = SQLITE_IOERR_DIR_FSYNC;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc;
|
2007-08-17 19:53:36 +04:00
|
|
|
}
|
|
|
|
|
2007-08-17 20:50:38 +04:00
|
|
|
/*
|
|
|
|
** Test the existance of or access permissions of file zPath. The
|
|
|
|
** test performed depends on the value of flags:
|
|
|
|
**
|
|
|
|
** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
|
|
|
|
** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
|
|
|
|
** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
|
|
|
|
**
|
|
|
|
** Otherwise return 0.
|
|
|
|
*/
|
2008-06-05 15:39:11 +04:00
|
|
|
static int unixAccess(
|
|
|
|
sqlite3_vfs *pVfs,
|
|
|
|
const char *zPath,
|
|
|
|
int flags,
|
|
|
|
int *pResOut
|
|
|
|
){
|
2007-09-20 12:38:14 +04:00
|
|
|
int amode = 0;
|
2008-06-05 15:39:11 +04:00
|
|
|
SimulateIOError( return SQLITE_IOERR_ACCESS; );
|
2007-08-17 19:53:36 +04:00
|
|
|
switch( flags ){
|
|
|
|
case SQLITE_ACCESS_EXISTS:
|
|
|
|
amode = F_OK;
|
|
|
|
break;
|
|
|
|
case SQLITE_ACCESS_READWRITE:
|
|
|
|
amode = W_OK|R_OK;
|
|
|
|
break;
|
2007-08-28 01:10:36 +04:00
|
|
|
case SQLITE_ACCESS_READ:
|
2007-08-17 19:53:36 +04:00
|
|
|
amode = R_OK;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(!"Invalid flags argument");
|
|
|
|
}
|
2008-06-05 15:39:11 +04:00
|
|
|
*pResOut = (access(zPath, amode)==0);
|
|
|
|
return SQLITE_OK;
|
2007-08-17 19:53:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Turn a relative pathname into a full pathname. The relative path
|
|
|
|
** is stored as a nul-terminated string in the buffer pointed to by
|
|
|
|
** zPath.
|
|
|
|
**
|
|
|
|
** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
|
|
|
|
** (in this case, MAX_PATHNAME bytes). The full-path is written to
|
|
|
|
** this buffer before returning.
|
|
|
|
*/
|
2007-09-17 11:02:56 +04:00
|
|
|
static int unixFullPathname(
|
|
|
|
sqlite3_vfs *pVfs, /* Pointer to vfs object */
|
|
|
|
const char *zPath, /* Possibly relative input path */
|
|
|
|
int nOut, /* Size of output buffer in bytes */
|
|
|
|
char *zOut /* Output buffer */
|
|
|
|
){
|
2007-09-01 20:16:15 +04:00
|
|
|
|
|
|
|
/* It's odd to simulate an io-error here, but really this is just
|
|
|
|
** using the io-error infrastructure to test that SQLite handles this
|
|
|
|
** function failing. This function could fail if, for example, the
|
|
|
|
** current working directly has been unlinked.
|
|
|
|
*/
|
|
|
|
SimulateIOError( return SQLITE_ERROR );
|
|
|
|
|
2007-08-24 07:51:33 +04:00
|
|
|
assert( pVfs->mxPathname==MAX_PATHNAME );
|
2007-12-06 16:26:20 +03:00
|
|
|
zOut[nOut-1] = '\0';
|
2007-08-17 19:53:36 +04:00
|
|
|
if( zPath[0]=='/' ){
|
2007-12-06 16:26:20 +03:00
|
|
|
sqlite3_snprintf(nOut, zOut, "%s", zPath);
|
2007-08-17 19:53:36 +04:00
|
|
|
}else{
|
|
|
|
int nCwd;
|
2007-12-06 16:26:20 +03:00
|
|
|
if( getcwd(zOut, nOut-1)==0 ){
|
2007-09-03 21:42:17 +04:00
|
|
|
return SQLITE_CANTOPEN;
|
2007-08-17 19:53:36 +04:00
|
|
|
}
|
|
|
|
nCwd = strlen(zOut);
|
2007-12-06 16:26:20 +03:00
|
|
|
sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
|
2007-08-17 19:53:36 +04:00
|
|
|
}
|
|
|
|
return SQLITE_OK;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
** Remove "/./" path elements and convert "/A/./" path elements
|
|
|
|
** to just "/".
|
|
|
|
*/
|
|
|
|
if( zFull ){
|
|
|
|
int i, j;
|
|
|
|
for(i=j=0; zFull[i]; i++){
|
|
|
|
if( zFull[i]=='/' ){
|
|
|
|
if( zFull[i+1]=='/' ) continue;
|
|
|
|
if( zFull[i+1]=='.' && zFull[i+2]=='/' ){
|
|
|
|
i += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){
|
|
|
|
while( j>0 && zFull[j-1]!='/' ){ j--; }
|
|
|
|
i += 3;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
zFull[j++] = zFull[i];
|
|
|
|
}
|
|
|
|
zFull[j] = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-06-08 02:22:50 +04:00
|
|
|
|
2006-12-21 04:29:22 +03:00
|
|
|
#ifndef SQLITE_OMIT_LOAD_EXTENSION
|
|
|
|
/*
|
|
|
|
** Interfaces for opening a shared library, finding entry points
|
|
|
|
** within the shared library, and closing the shared library.
|
|
|
|
*/
|
|
|
|
#include <dlfcn.h>
|
2007-08-24 07:51:33 +04:00
|
|
|
static void *unixDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
|
2006-12-21 04:29:22 +03:00
|
|
|
return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
|
|
|
|
}
|
2007-09-01 10:51:27 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** SQLite calls this function immediately after a call to unixDlSym() or
|
|
|
|
** unixDlOpen() fails (returns a null pointer). If a more detailed error
|
|
|
|
** message is available, it is written to zBufOut. If no error message
|
|
|
|
** is available, zBufOut is left unmodified and SQLite uses a default
|
|
|
|
** error message.
|
|
|
|
*/
|
2007-08-24 07:51:33 +04:00
|
|
|
static void unixDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
|
2007-08-17 19:53:36 +04:00
|
|
|
char *zErr;
|
|
|
|
enterMutex();
|
|
|
|
zErr = dlerror();
|
|
|
|
if( zErr ){
|
2007-08-24 07:51:33 +04:00
|
|
|
sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
|
2007-08-17 19:53:36 +04:00
|
|
|
}
|
|
|
|
leaveMutex();
|
|
|
|
}
|
2007-08-28 03:26:59 +04:00
|
|
|
static void *unixDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){
|
2006-12-21 04:29:22 +03:00
|
|
|
return dlsym(pHandle, zSymbol);
|
|
|
|
}
|
2007-08-28 03:26:59 +04:00
|
|
|
static void unixDlClose(sqlite3_vfs *pVfs, void *pHandle){
|
2007-08-17 19:53:36 +04:00
|
|
|
dlclose(pHandle);
|
2006-12-21 04:29:22 +03:00
|
|
|
}
|
2007-08-17 19:53:36 +04:00
|
|
|
#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
|
|
|
|
#define unixDlOpen 0
|
|
|
|
#define unixDlError 0
|
|
|
|
#define unixDlSym 0
|
|
|
|
#define unixDlClose 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
2007-08-17 20:50:38 +04:00
|
|
|
** Write nBuf bytes of random data to the supplied buffer zBuf.
|
2001-09-18 00:25:58 +04:00
|
|
|
*/
|
2007-08-24 07:51:33 +04:00
|
|
|
static int unixRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
|
2007-08-17 20:50:38 +04:00
|
|
|
|
|
|
|
assert(nBuf>=(sizeof(time_t)+sizeof(int)));
|
|
|
|
|
2003-12-31 16:21:18 +03:00
|
|
|
/* 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
|
2006-01-15 20:27:17 +03:00
|
|
|
** that we always use the same random number sequence. This makes the
|
2003-12-31 16:21:18 +03:00
|
|
|
** tests repeatable.
|
2002-07-07 20:52:46 +04:00
|
|
|
*/
|
2007-08-17 19:53:36 +04:00
|
|
|
memset(zBuf, 0, nBuf);
|
2004-05-22 21:41:58 +04:00
|
|
|
#if !defined(SQLITE_TEST)
|
2003-12-31 19:00:19 +03:00
|
|
|
{
|
2005-01-21 20:53:17 +03:00
|
|
|
int pid, fd;
|
|
|
|
fd = open("/dev/urandom", O_RDONLY);
|
|
|
|
if( fd<0 ){
|
2006-01-06 17:46:46 +03:00
|
|
|
time_t t;
|
|
|
|
time(&t);
|
2007-08-17 20:50:38 +04:00
|
|
|
memcpy(zBuf, &t, sizeof(t));
|
|
|
|
pid = getpid();
|
|
|
|
memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
|
2005-01-21 20:53:17 +03:00
|
|
|
}else{
|
2007-08-17 19:53:36 +04:00
|
|
|
read(fd, zBuf, nBuf);
|
2005-01-21 20:53:17 +03:00
|
|
|
close(fd);
|
|
|
|
}
|
2003-12-31 19:00:19 +03:00
|
|
|
}
|
2001-09-23 23:46:51 +04:00
|
|
|
#endif
|
2001-09-19 17:22:39 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2007-08-17 19:53:36 +04:00
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
2001-09-19 17:22:39 +04:00
|
|
|
** Sleep for a little while. Return the amount of time slept.
|
2007-08-17 19:53:36 +04:00
|
|
|
** The argument is the number of microseconds we want to sleep.
|
2007-08-23 06:47:53 +04:00
|
|
|
** The return value is the number of microseconds of sleep actually
|
|
|
|
** requested from the underlying operating system, a number which
|
|
|
|
** might be greater than or equal to the argument, but not less
|
|
|
|
** than the argument.
|
2001-09-18 00:25:58 +04:00
|
|
|
*/
|
2007-08-24 07:51:33 +04:00
|
|
|
static int unixSleep(sqlite3_vfs *pVfs, int microseconds){
|
2001-09-19 17:22:39 +04:00
|
|
|
#if defined(HAVE_USLEEP) && HAVE_USLEEP
|
2007-08-17 19:53:36 +04:00
|
|
|
usleep(microseconds);
|
|
|
|
return microseconds;
|
2006-02-10 07:33:12 +03:00
|
|
|
#else
|
2007-08-17 19:53:36 +04:00
|
|
|
int seconds = (microseconds+999999)/1000000;
|
|
|
|
sleep(seconds);
|
2007-08-23 06:47:53 +04:00
|
|
|
return seconds*1000000;
|
2006-01-18 17:06:37 +03:00
|
|
|
#endif
|
2006-01-02 23:00:12 +03:00
|
|
|
}
|
|
|
|
|
2004-01-06 03:44:24 +03:00
|
|
|
/*
|
2004-03-13 17:00:36 +03:00
|
|
|
** The following variable, if set to a non-zero value, becomes the result
|
2006-01-06 17:32:19 +03:00
|
|
|
** returned from sqlite3OsCurrentTime(). This is used for testing.
|
2004-01-06 03:44:24 +03:00
|
|
|
*/
|
|
|
|
#ifdef SQLITE_TEST
|
2004-05-10 14:34:51 +04:00
|
|
|
int sqlite3_current_time = 0;
|
2004-01-06 03:44:24 +03:00
|
|
|
#endif
|
|
|
|
|
2003-08-10 01:32:28 +04:00
|
|
|
/*
|
|
|
|
** 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.
|
|
|
|
*/
|
2007-08-24 07:51:33 +04:00
|
|
|
static int unixCurrentTime(sqlite3_vfs *pVfs, double *prNow){
|
2005-08-30 03:00:03 +04:00
|
|
|
#ifdef NO_GETTOD
|
2003-08-10 01:32:28 +04:00
|
|
|
time_t t;
|
|
|
|
time(&t);
|
|
|
|
*prNow = t/86400.0 + 2440587.5;
|
2005-08-30 03:00:03 +04:00
|
|
|
#else
|
|
|
|
struct timeval sNow;
|
2007-04-02 22:06:57 +04:00
|
|
|
gettimeofday(&sNow, 0);
|
2005-08-30 03:00:03 +04:00
|
|
|
*prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
|
|
|
|
#endif
|
2004-01-06 03:44:24 +03:00
|
|
|
#ifdef SQLITE_TEST
|
2004-05-10 14:34:51 +04:00
|
|
|
if( sqlite3_current_time ){
|
|
|
|
*prNow = sqlite3_current_time/86400.0 + 2440587.5;
|
2004-01-06 03:44:24 +03:00
|
|
|
}
|
2003-08-10 20:16:22 +04:00
|
|
|
#endif
|
|
|
|
return 0;
|
2003-08-10 01:32:28 +04:00
|
|
|
}
|
2007-08-17 19:53:36 +04:00
|
|
|
|
2008-06-06 19:49:29 +04:00
|
|
|
static int unixGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-08-24 07:51:33 +04:00
|
|
|
/*
|
2008-06-28 15:23:00 +04:00
|
|
|
** Initialize the operating system interface.
|
2007-08-24 07:51:33 +04:00
|
|
|
*/
|
2008-06-25 21:19:00 +04:00
|
|
|
int sqlite3_os_init(void){
|
2008-06-28 15:23:00 +04:00
|
|
|
/* Macro to define the static contents of an sqlite3_vfs structure for
|
|
|
|
** the unix backend. The two parameters are the values to use for
|
|
|
|
** the sqlite3_vfs.zName and sqlite3_vfs.pAppData fields, respectively.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
#define UNIXVFS(zVfsName, pVfsAppData) { \
|
|
|
|
1, /* iVersion */ \
|
|
|
|
sizeof(unixFile), /* szOsFile */ \
|
|
|
|
MAX_PATHNAME, /* mxPathname */ \
|
|
|
|
0, /* pNext */ \
|
|
|
|
zVfsName, /* zName */ \
|
|
|
|
(void *)pVfsAppData, /* pAppData */ \
|
|
|
|
unixOpen, /* xOpen */ \
|
|
|
|
unixDelete, /* xDelete */ \
|
|
|
|
unixAccess, /* xAccess */ \
|
|
|
|
unixFullPathname, /* xFullPathname */ \
|
|
|
|
unixDlOpen, /* xDlOpen */ \
|
|
|
|
unixDlError, /* xDlError */ \
|
|
|
|
unixDlSym, /* xDlSym */ \
|
|
|
|
unixDlClose, /* xDlClose */ \
|
|
|
|
unixRandomness, /* xRandomness */ \
|
|
|
|
unixSleep, /* xSleep */ \
|
|
|
|
unixCurrentTime, /* xCurrentTime */ \
|
|
|
|
unixGetLastError /* xGetLastError */ \
|
|
|
|
}
|
|
|
|
|
|
|
|
static sqlite3_vfs unixVfs = UNIXVFS("unix", 0);
|
|
|
|
#ifdef SQLITE_ENABLE_LOCKING_STYLE
|
|
|
|
int i;
|
|
|
|
static sqlite3_vfs aVfs[] = {
|
|
|
|
UNIXVFS("unix-posix", LOCKING_STYLE_POSIX),
|
|
|
|
UNIXVFS("unix-afp", LOCKING_STYLE_AFP),
|
|
|
|
UNIXVFS("unix-flock", LOCKING_STYLE_FLOCK),
|
|
|
|
UNIXVFS("unix-dotfile", LOCKING_STYLE_DOTFILE),
|
|
|
|
UNIXVFS("unix-none", LOCKING_STYLE_NONE)
|
2007-08-24 07:51:33 +04:00
|
|
|
};
|
2008-06-28 15:23:00 +04:00
|
|
|
for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
|
|
|
|
sqlite3_vfs_register(&aVfs[i], 0);
|
|
|
|
}
|
|
|
|
#endif
|
2008-06-25 21:19:00 +04:00
|
|
|
sqlite3_vfs_register(&unixVfs, 1);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
2008-06-28 15:23:00 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Shutdown the operating system interface. This is a no-op for unix.
|
|
|
|
*/
|
2008-06-25 21:19:00 +04:00
|
|
|
int sqlite3_os_end(void){
|
|
|
|
return SQLITE_OK;
|
2007-08-24 07:51:33 +04:00
|
|
|
}
|
2007-08-16 17:01:44 +04:00
|
|
|
|
2008-06-26 14:41:19 +04:00
|
|
|
#endif /* SQLITE_OS_UNIX */
|