blob: 0544e071c0d16ebc1860243aa010330d2c2ba92d [file] [log] [blame]
drhbbd42a62004-05-22 17:41:58 +00001/*
2** 2004 May 22
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11******************************************************************************
12**
13** This file contains code that is specific to Unix systems.
14*/
drhbbd42a62004-05-22 17:41:58 +000015#include "sqliteInt.h"
drheb206252004-10-01 02:00:31 +000016#include "os.h"
17#if OS_UNIX /* This file is used on unix only */
drh66560ad2006-01-06 14:32:19 +000018
drhbfe66312006-10-03 17:40:40 +000019/* #define SQLITE_ENABLE_LOCKING_STYLE 0 */
20
drh9cbe6352005-11-29 03:13:21 +000021/*
22** These #defines should enable >2GB file support on Posix if the
23** underlying operating system supports it. If the OS lacks
drhf1a221e2006-01-15 17:27:17 +000024** large file support, these should be no-ops.
drh9cbe6352005-11-29 03:13:21 +000025**
26** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
27** on the compiler command line. This is necessary if you are compiling
28** on a recent machine (ex: RedHat 7.2) but you want your code to work
29** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2
30** without this option, LFS is enable. But LFS does not exist in the kernel
31** in RedHat 6.0, so the code won't work. Hence, for maximum binary
32** portability you should omit LFS.
drh9cbe6352005-11-29 03:13:21 +000033*/
34#ifndef SQLITE_DISABLE_LFS
35# define _LARGE_FILE 1
36# ifndef _FILE_OFFSET_BITS
37# define _FILE_OFFSET_BITS 64
38# endif
39# define _LARGEFILE_SOURCE 1
40#endif
drhbbd42a62004-05-22 17:41:58 +000041
drh9cbe6352005-11-29 03:13:21 +000042/*
43** standard include files.
44*/
45#include <sys/types.h>
46#include <sys/stat.h>
47#include <fcntl.h>
48#include <unistd.h>
drhbbd42a62004-05-22 17:41:58 +000049#include <time.h>
drh19e2d372005-08-29 23:00:03 +000050#include <sys/time.h>
drhbbd42a62004-05-22 17:41:58 +000051#include <errno.h>
drhbfe66312006-10-03 17:40:40 +000052#ifdef SQLITE_ENABLE_LOCKING_STYLE
53#include <sys/ioctl.h>
54#include <sys/param.h>
55#include <sys/mount.h>
56#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drh9cbe6352005-11-29 03:13:21 +000057
58/*
drhf1a221e2006-01-15 17:27:17 +000059** If we are to be thread-safe, include the pthreads header and define
60** the SQLITE_UNIX_THREADS macro.
drh9cbe6352005-11-29 03:13:21 +000061*/
drhd677b3d2007-08-20 22:48:41 +000062#if SQLITE_THREADSAFE
drh9cbe6352005-11-29 03:13:21 +000063# include <pthread.h>
64# define SQLITE_UNIX_THREADS 1
65#endif
66
67/*
68** Default permissions when creating a new file
69*/
70#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
71# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
72#endif
73
danielk1977b4b47412007-08-17 15:53:36 +000074/*
75** Maximum supported path-length.
76*/
77#define MAX_PATHNAME 512
drh9cbe6352005-11-29 03:13:21 +000078
79
80/*
danielk1977ad94b582007-08-20 06:44:22 +000081** The unixFile structure is subclass of sqlite3_file specific for the unix
drh054889e2005-11-30 03:20:31 +000082** protability layer.
drh9cbe6352005-11-29 03:13:21 +000083*/
drh054889e2005-11-30 03:20:31 +000084typedef struct unixFile unixFile;
85struct unixFile {
danielk197762079062007-08-15 17:08:46 +000086 sqlite3_io_methods const *pMethod; /* Always the first entry */
danielk1977967a4a12007-08-20 14:23:44 +000087#ifdef SQLITE_TEST
88 /* In test mode, increase the size of this structure a bit so that
89 ** it is larger than the struct CrashFile defined in test6.c.
90 */
91 char aPadding[32];
92#endif
drh9cbe6352005-11-29 03:13:21 +000093 struct openCnt *pOpen; /* Info about all open fd's on this inode */
94 struct lockInfo *pLock; /* Info about locks on this inode */
drhbfe66312006-10-03 17:40:40 +000095#ifdef SQLITE_ENABLE_LOCKING_STYLE
96 void *lockingContext; /* Locking style specific state */
97#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drh9cbe6352005-11-29 03:13:21 +000098 int h; /* The file descriptor */
99 unsigned char locktype; /* The type of lock held on this fd */
100 unsigned char isOpen; /* True if needs to be closed */
drh9cbe6352005-11-29 03:13:21 +0000101 int dirfd; /* File descriptor for the directory */
drhd677b3d2007-08-20 22:48:41 +0000102#if SQLITE_THREADSAFE
danielk1977ad94b582007-08-20 06:44:22 +0000103 pthread_t tid; /* The thread that "owns" this unixFile */
drh9cbe6352005-11-29 03:13:21 +0000104#endif
105};
106
drh0ccebe72005-06-07 22:22:50 +0000107/*
drh198bf392006-01-06 21:52:49 +0000108** Include code that is common to all os_*.c files
109*/
110#include "os_common.h"
111
112/*
drh0ccebe72005-06-07 22:22:50 +0000113** Do not include any of the File I/O interface procedures if the
drhf1a221e2006-01-15 17:27:17 +0000114** SQLITE_OMIT_DISKIO macro is defined (indicating that the database
drh0ccebe72005-06-07 22:22:50 +0000115** will be in-memory only)
116*/
117#ifndef SQLITE_OMIT_DISKIO
118
drh0ccebe72005-06-07 22:22:50 +0000119/*
120** Define various macros that are missing from some systems.
121*/
drhbbd42a62004-05-22 17:41:58 +0000122#ifndef O_LARGEFILE
123# define O_LARGEFILE 0
124#endif
125#ifdef SQLITE_DISABLE_LFS
126# undef O_LARGEFILE
127# define O_LARGEFILE 0
128#endif
129#ifndef O_NOFOLLOW
130# define O_NOFOLLOW 0
131#endif
132#ifndef O_BINARY
133# define O_BINARY 0
134#endif
135
136/*
137** The DJGPP compiler environment looks mostly like Unix, but it
138** lacks the fcntl() system call. So redefine fcntl() to be something
139** that always succeeds. This means that locking does not occur under
danielk197726c5d792005-11-25 09:01:23 +0000140** DJGPP. But it's DOS - what did you expect?
drhbbd42a62004-05-22 17:41:58 +0000141*/
142#ifdef __DJGPP__
143# define fcntl(A,B,C) 0
144#endif
145
146/*
drh2b4b5962005-06-15 17:47:55 +0000147** The threadid macro resolves to the thread-id or to 0. Used for
148** testing and debugging only.
149*/
drhd677b3d2007-08-20 22:48:41 +0000150#if SQLITE_THREADSAFE
drh2b4b5962005-06-15 17:47:55 +0000151#define threadid pthread_self()
152#else
153#define threadid 0
154#endif
155
156/*
danielk1977ad94b582007-08-20 06:44:22 +0000157** Set or check the unixFile.tid field. This field is set when an unixFile
158** is first opened. All subsequent uses of the unixFile verify that the
159** same thread is operating on the unixFile. Some operating systems do
drh2b4b5962005-06-15 17:47:55 +0000160** not allow locks to be overridden by other threads and that restriction
161** means that sqlite3* database handles cannot be moved from one thread
162** to another. This logic makes sure a user does not try to do that
163** by mistake.
drhf1a221e2006-01-15 17:27:17 +0000164**
danielk1977ad94b582007-08-20 06:44:22 +0000165** Version 3.3.1 (2006-01-15): unixFile can be moved from one thread to
drhf1a221e2006-01-15 17:27:17 +0000166** another as long as we are running on a system that supports threads
167** overriding each others locks (which now the most common behavior)
danielk1977ad94b582007-08-20 06:44:22 +0000168** or if no locks are held. But the unixFile.pLock field needs to be
drhf1a221e2006-01-15 17:27:17 +0000169** recomputed because its key includes the thread-id. See the
170** transferOwnership() function below for additional information
drh2b4b5962005-06-15 17:47:55 +0000171*/
drhd677b3d2007-08-20 22:48:41 +0000172#if SQLITE_THREADSAFE
drh9cbe6352005-11-29 03:13:21 +0000173# define SET_THREADID(X) (X)->tid = pthread_self()
drh029b44b2006-01-15 00:13:15 +0000174# define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
175 !pthread_equal((X)->tid, pthread_self()))
drh2b4b5962005-06-15 17:47:55 +0000176#else
177# define SET_THREADID(X)
178# define CHECK_THREADID(X) 0
danielk197713adf8a2004-06-03 16:08:41 +0000179#endif
180
drhbbd42a62004-05-22 17:41:58 +0000181/*
182** Here is the dirt on POSIX advisory locks: ANSI STD 1003.1 (1996)
183** section 6.5.2.2 lines 483 through 490 specify that when a process
184** sets or clears a lock, that operation overrides any prior locks set
185** by the same process. It does not explicitly say so, but this implies
186** that it overrides locks set by the same process using a different
187** file descriptor. Consider this test case:
188**
189** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
190** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
191**
192** Suppose ./file1 and ./file2 are really the same file (because
193** one is a hard or symbolic link to the other) then if you set
194** an exclusive lock on fd1, then try to get an exclusive lock
195** on fd2, it works. I would have expected the second lock to
196** fail since there was already a lock on the file due to fd1.
197** But not so. Since both locks came from the same process, the
198** second overrides the first, even though they were on different
199** file descriptors opened on different file names.
200**
201** Bummer. If you ask me, this is broken. Badly broken. It means
202** that we cannot use POSIX locks to synchronize file access among
203** competing threads of the same process. POSIX locks will work fine
204** to synchronize access for threads in separate processes, but not
205** threads within the same process.
206**
207** To work around the problem, SQLite has to manage file locks internally
208** on its own. Whenever a new database is opened, we have to find the
209** specific inode of the database file (the inode is determined by the
210** st_dev and st_ino fields of the stat structure that fstat() fills in)
211** and check for locks already existing on that inode. When locks are
212** created or removed, we have to look at our own internal record of the
213** locks to see if another thread has previously set a lock on that same
214** inode.
215**
danielk1977ad94b582007-08-20 06:44:22 +0000216** The sqlite3_file structure for POSIX is no longer just an integer file
drhbbd42a62004-05-22 17:41:58 +0000217** descriptor. It is now a structure that holds the integer file
218** descriptor and a pointer to a structure that describes the internal
219** locks on the corresponding inode. There is one locking structure
danielk1977ad94b582007-08-20 06:44:22 +0000220** per inode, so if the same inode is opened twice, both unixFile structures
drhbbd42a62004-05-22 17:41:58 +0000221** point to the same locking structure. The locking structure keeps
222** a reference count (so we will know when to delete it) and a "cnt"
223** field that tells us its internal lock status. cnt==0 means the
224** file is unlocked. cnt==-1 means the file has an exclusive lock.
225** cnt>0 means there are cnt shared locks on the file.
226**
227** Any attempt to lock or unlock a file first checks the locking
228** structure. The fcntl() system call is only invoked to set a
229** POSIX lock if the internal lock structure transitions between
230** a locked and an unlocked state.
231**
232** 2004-Jan-11:
233** More recent discoveries about POSIX advisory locks. (The more
234** I discover, the more I realize the a POSIX advisory locks are
235** an abomination.)
236**
237** If you close a file descriptor that points to a file that has locks,
238** all locks on that file that are owned by the current process are
danielk1977ad94b582007-08-20 06:44:22 +0000239** released. To work around this problem, each unixFile structure contains
drhbbd42a62004-05-22 17:41:58 +0000240** a pointer to an openCnt structure. There is one openCnt structure
danielk1977ad94b582007-08-20 06:44:22 +0000241** per open inode, which means that multiple unixFile can point to a single
242** openCnt. When an attempt is made to close an unixFile, if there are
243** other unixFile open on the same inode that are holding locks, the call
drhbbd42a62004-05-22 17:41:58 +0000244** to close() the file descriptor is deferred until all of the locks clear.
245** The openCnt structure keeps a list of file descriptors that need to
246** be closed and that list is walked (and cleared) when the last lock
247** clears.
248**
249** First, under Linux threads, because each thread has a separate
250** process ID, lock operations in one thread do not override locks
251** to the same file in other threads. Linux threads behave like
252** separate processes in this respect. But, if you close a file
253** descriptor in linux threads, all locks are cleared, even locks
254** on other threads and even though the other threads have different
255** process IDs. Linux threads is inconsistent in this respect.
256** (I'm beginning to think that linux threads is an abomination too.)
257** The consequence of this all is that the hash table for the lockInfo
258** structure has to include the process id as part of its key because
259** locks in different threads are treated as distinct. But the
260** openCnt structure should not include the process id in its
261** key because close() clears lock on all threads, not just the current
262** thread. Were it not for this goofiness in linux threads, we could
263** combine the lockInfo and openCnt structures into a single structure.
drh5fdae772004-06-29 03:29:00 +0000264**
265** 2004-Jun-28:
266** On some versions of linux, threads can override each others locks.
267** On others not. Sometimes you can change the behavior on the same
268** system by setting the LD_ASSUME_KERNEL environment variable. The
269** POSIX standard is silent as to which behavior is correct, as far
270** as I can tell, so other versions of unix might show the same
271** inconsistency. There is no little doubt in my mind that posix
272** advisory locks and linux threads are profoundly broken.
273**
274** To work around the inconsistencies, we have to test at runtime
275** whether or not threads can override each others locks. This test
276** is run once, the first time any lock is attempted. A static
277** variable is set to record the results of this test for future
278** use.
drhbbd42a62004-05-22 17:41:58 +0000279*/
280
281/*
282** An instance of the following structure serves as the key used
drh5fdae772004-06-29 03:29:00 +0000283** to locate a particular lockInfo structure given its inode.
284**
285** If threads cannot override each others locks, then we set the
286** lockKey.tid field to the thread ID. If threads can override
drhf1a221e2006-01-15 17:27:17 +0000287** each others locks then tid is always set to zero. tid is omitted
288** if we compile without threading support.
drhbbd42a62004-05-22 17:41:58 +0000289*/
290struct lockKey {
drh5fdae772004-06-29 03:29:00 +0000291 dev_t dev; /* Device number */
292 ino_t ino; /* Inode number */
drhd677b3d2007-08-20 22:48:41 +0000293#if SQLITE_THREADSAFE
drhd9cb6ac2005-10-20 07:28:17 +0000294 pthread_t tid; /* Thread ID or zero if threads can override each other */
drh5fdae772004-06-29 03:29:00 +0000295#endif
drhbbd42a62004-05-22 17:41:58 +0000296};
297
298/*
299** An instance of the following structure is allocated for each open
300** inode on each thread with a different process ID. (Threads have
301** different process IDs on linux, but not on most other unixes.)
302**
danielk1977ad94b582007-08-20 06:44:22 +0000303** A single inode can have multiple file descriptors, so each unixFile
drhbbd42a62004-05-22 17:41:58 +0000304** structure contains a pointer to an instance of this object and this
danielk1977ad94b582007-08-20 06:44:22 +0000305** object keeps a count of the number of unixFile pointing to it.
drhbbd42a62004-05-22 17:41:58 +0000306*/
307struct lockInfo {
308 struct lockKey key; /* The lookup key */
drh2ac3ee92004-06-07 16:27:46 +0000309 int cnt; /* Number of SHARED locks held */
danielk19779a1d0ab2004-06-01 14:09:28 +0000310 int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
drhbbd42a62004-05-22 17:41:58 +0000311 int nRef; /* Number of pointers to this structure */
312};
313
314/*
315** An instance of the following structure serves as the key used
316** to locate a particular openCnt structure given its inode. This
drh5fdae772004-06-29 03:29:00 +0000317** is the same as the lockKey except that the thread ID is omitted.
drhbbd42a62004-05-22 17:41:58 +0000318*/
319struct openKey {
320 dev_t dev; /* Device number */
321 ino_t ino; /* Inode number */
322};
323
324/*
325** An instance of the following structure is allocated for each open
326** inode. This structure keeps track of the number of locks on that
327** inode. If a close is attempted against an inode that is holding
328** locks, the close is deferred until all locks clear by adding the
329** file descriptor to be closed to the pending list.
330*/
331struct openCnt {
332 struct openKey key; /* The lookup key */
333 int nRef; /* Number of pointers to this structure */
334 int nLock; /* Number of outstanding locks */
335 int nPending; /* Number of pending close() operations */
336 int *aPending; /* Malloced space holding fd's awaiting a close() */
337};
338
339/*
drhf1a221e2006-01-15 17:27:17 +0000340** These hash tables map inodes and file descriptors (really, lockKey and
341** openKey structures) into lockInfo and openCnt structures. Access to
342** these hash tables must be protected by a mutex.
drhbbd42a62004-05-22 17:41:58 +0000343*/
drh17435752007-08-16 04:30:38 +0000344static Hash lockHash = {SQLITE_HASH_BINARY, 0, 0, 0, 0, 0};
345static Hash openHash = {SQLITE_HASH_BINARY, 0, 0, 0, 0, 0};
drh5fdae772004-06-29 03:29:00 +0000346
drhbfe66312006-10-03 17:40:40 +0000347#ifdef SQLITE_ENABLE_LOCKING_STYLE
348/*
349** The locking styles are associated with the different file locking
350** capabilities supported by different file systems.
351**
352** POSIX locking style fully supports shared and exclusive byte-range locks
353** ADP locking only supports exclusive byte-range locks
354** FLOCK only supports a single file-global exclusive lock
355** DOTLOCK isn't a true locking style, it refers to the use of a special
356** file named the same as the database file with a '.lock' extension, this
357** can be used on file systems that do not offer any reliable file locking
358** NO locking means that no locking will be attempted, this is only used for
359** read-only file systems currently
360** UNSUPPORTED means that no locking will be attempted, this is only used for
361** file systems that are known to be unsupported
362*/
363typedef enum {
drhfd131da2007-08-07 17:13:03 +0000364 posixLockingStyle = 0, /* standard posix-advisory locks */
365 afpLockingStyle, /* use afp locks */
366 flockLockingStyle, /* use flock() */
367 dotlockLockingStyle, /* use <file>.lock files */
368 noLockingStyle, /* useful for read-only file system */
369 unsupportedLockingStyle /* indicates unsupported file system */
drhbfe66312006-10-03 17:40:40 +0000370} sqlite3LockingStyle;
371#endif /* SQLITE_ENABLE_LOCKING_STYLE */
372
danielk1977ad94b582007-08-20 06:44:22 +0000373/*
374** Helper functions to obtain and relinquish the global mutex.
375*/
danielk1977b4b47412007-08-17 15:53:36 +0000376static void enterMutex(){
377 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_GLOBAL));
378}
379static void leaveMutex(){
380 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_GLOBAL));
381}
382
drhd677b3d2007-08-20 22:48:41 +0000383#if SQLITE_THREADSAFE
drh5fdae772004-06-29 03:29:00 +0000384/*
385** This variable records whether or not threads can override each others
386** locks.
387**
388** 0: No. Threads cannot override each others locks.
389** 1: Yes. Threads can override each others locks.
390** -1: We don't know yet.
drhf1a221e2006-01-15 17:27:17 +0000391**
drh5062d3a2006-01-31 23:03:35 +0000392** On some systems, we know at compile-time if threads can override each
393** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro
394** will be set appropriately. On other systems, we have to check at
395** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is
396** undefined.
397**
drhf1a221e2006-01-15 17:27:17 +0000398** This variable normally has file scope only. But during testing, we make
399** it a global so that the test code can change its value in order to verify
400** that the right stuff happens in either case.
drh5fdae772004-06-29 03:29:00 +0000401*/
drh5062d3a2006-01-31 23:03:35 +0000402#ifndef SQLITE_THREAD_OVERRIDE_LOCK
403# define SQLITE_THREAD_OVERRIDE_LOCK -1
404#endif
drh029b44b2006-01-15 00:13:15 +0000405#ifdef SQLITE_TEST
drh5062d3a2006-01-31 23:03:35 +0000406int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh029b44b2006-01-15 00:13:15 +0000407#else
drh5062d3a2006-01-31 23:03:35 +0000408static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh029b44b2006-01-15 00:13:15 +0000409#endif
drh5fdae772004-06-29 03:29:00 +0000410
411/*
412** This structure holds information passed into individual test
413** threads by the testThreadLockingBehavior() routine.
414*/
415struct threadTestData {
416 int fd; /* File to be locked */
417 struct flock lock; /* The locking operation */
418 int result; /* Result of the locking operation */
419};
420
drh2b4b5962005-06-15 17:47:55 +0000421#ifdef SQLITE_LOCK_TRACE
422/*
423** Print out information about all locking operations.
424**
425** This routine is used for troubleshooting locks on multithreaded
426** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
427** command-line option on the compiler. This code is normally
drhf1a221e2006-01-15 17:27:17 +0000428** turned off.
drh2b4b5962005-06-15 17:47:55 +0000429*/
430static int lockTrace(int fd, int op, struct flock *p){
431 char *zOpName, *zType;
432 int s;
433 int savedErrno;
434 if( op==F_GETLK ){
435 zOpName = "GETLK";
436 }else if( op==F_SETLK ){
437 zOpName = "SETLK";
438 }else{
439 s = fcntl(fd, op, p);
440 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
441 return s;
442 }
443 if( p->l_type==F_RDLCK ){
444 zType = "RDLCK";
445 }else if( p->l_type==F_WRLCK ){
446 zType = "WRLCK";
447 }else if( p->l_type==F_UNLCK ){
448 zType = "UNLCK";
449 }else{
450 assert( 0 );
451 }
452 assert( p->l_whence==SEEK_SET );
453 s = fcntl(fd, op, p);
454 savedErrno = errno;
455 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
456 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
457 (int)p->l_pid, s);
drhe2396a12007-03-29 20:19:58 +0000458 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
drh2b4b5962005-06-15 17:47:55 +0000459 struct flock l2;
460 l2 = *p;
461 fcntl(fd, F_GETLK, &l2);
462 if( l2.l_type==F_RDLCK ){
463 zType = "RDLCK";
464 }else if( l2.l_type==F_WRLCK ){
465 zType = "WRLCK";
466 }else if( l2.l_type==F_UNLCK ){
467 zType = "UNLCK";
468 }else{
469 assert( 0 );
470 }
471 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
472 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
473 }
474 errno = savedErrno;
475 return s;
476}
477#define fcntl lockTrace
478#endif /* SQLITE_LOCK_TRACE */
479
drh5fdae772004-06-29 03:29:00 +0000480/*
481** The testThreadLockingBehavior() routine launches two separate
482** threads on this routine. This routine attempts to lock a file
483** descriptor then returns. The success or failure of that attempt
484** allows the testThreadLockingBehavior() procedure to determine
485** whether or not threads can override each others locks.
486*/
487static void *threadLockingTest(void *pArg){
488 struct threadTestData *pData = (struct threadTestData*)pArg;
489 pData->result = fcntl(pData->fd, F_SETLK, &pData->lock);
490 return pArg;
491}
492
493/*
494** This procedure attempts to determine whether or not threads
495** can override each others locks then sets the
496** threadsOverrideEachOthersLocks variable appropriately.
497*/
danielk19774d5238f2006-01-27 06:32:00 +0000498static void testThreadLockingBehavior(int fd_orig){
drh5fdae772004-06-29 03:29:00 +0000499 int fd;
500 struct threadTestData d[2];
501 pthread_t t[2];
502
503 fd = dup(fd_orig);
504 if( fd<0 ) return;
505 memset(d, 0, sizeof(d));
506 d[0].fd = fd;
507 d[0].lock.l_type = F_RDLCK;
508 d[0].lock.l_len = 1;
509 d[0].lock.l_start = 0;
510 d[0].lock.l_whence = SEEK_SET;
511 d[1] = d[0];
512 d[1].lock.l_type = F_WRLCK;
513 pthread_create(&t[0], 0, threadLockingTest, &d[0]);
514 pthread_create(&t[1], 0, threadLockingTest, &d[1]);
515 pthread_join(t[0], 0);
516 pthread_join(t[1], 0);
517 close(fd);
518 threadsOverrideEachOthersLocks = d[0].result==0 && d[1].result==0;
519}
drhd677b3d2007-08-20 22:48:41 +0000520#endif /* SQLITE_THREADSAFE */
drh5fdae772004-06-29 03:29:00 +0000521
drhbbd42a62004-05-22 17:41:58 +0000522/*
523** Release a lockInfo structure previously allocated by findLockInfo().
524*/
525static void releaseLockInfo(struct lockInfo *pLock){
drhbfe66312006-10-03 17:40:40 +0000526 if (pLock == NULL)
527 return;
drhbbd42a62004-05-22 17:41:58 +0000528 pLock->nRef--;
529 if( pLock->nRef==0 ){
530 sqlite3HashInsert(&lockHash, &pLock->key, sizeof(pLock->key), 0);
drh17435752007-08-16 04:30:38 +0000531 sqlite3_free(pLock);
drhbbd42a62004-05-22 17:41:58 +0000532 }
533}
534
535/*
536** Release a openCnt structure previously allocated by findLockInfo().
537*/
538static void releaseOpenCnt(struct openCnt *pOpen){
drhbfe66312006-10-03 17:40:40 +0000539 if (pOpen == NULL)
540 return;
drhbbd42a62004-05-22 17:41:58 +0000541 pOpen->nRef--;
542 if( pOpen->nRef==0 ){
543 sqlite3HashInsert(&openHash, &pOpen->key, sizeof(pOpen->key), 0);
drh64b1bea2006-01-15 02:30:57 +0000544 free(pOpen->aPending);
drh17435752007-08-16 04:30:38 +0000545 sqlite3_free(pOpen);
drhbbd42a62004-05-22 17:41:58 +0000546 }
547}
548
drhbfe66312006-10-03 17:40:40 +0000549#ifdef SQLITE_ENABLE_LOCKING_STYLE
550/*
551** Tests a byte-range locking query to see if byte range locks are
552** supported, if not we fall back to dotlockLockingStyle.
553*/
danielk1977ad94b582007-08-20 06:44:22 +0000554static sqlite3LockingStyle sqlite3TestLockingStyle(
555 const char *filePath,
556 int fd
557){
drhbfe66312006-10-03 17:40:40 +0000558 /* test byte-range lock using fcntl */
559 struct flock lockInfo;
560
561 lockInfo.l_len = 1;
562 lockInfo.l_start = 0;
563 lockInfo.l_whence = SEEK_SET;
564 lockInfo.l_type = F_RDLCK;
565
danielk1977ad94b582007-08-20 06:44:22 +0000566 if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
drhbfe66312006-10-03 17:40:40 +0000567 return posixLockingStyle;
568 }
569
570 /* testing for flock can give false positives. So if if the above test
571 ** fails, then we fall back to using dot-lock style locking.
572 */
573 return dotlockLockingStyle;
574}
575
576/*
577** Examines the f_fstypename entry in the statfs structure as returned by
578** stat() for the file system hosting the database file, assigns the
579** appropriate locking style based on it's value. These values and
580** assignments are based on Darwin/OSX behavior and have not been tested on
581** other systems.
582*/
danielk1977ad94b582007-08-20 06:44:22 +0000583static sqlite3LockingStyle sqlite3DetectLockingStyle(
584 const char *filePath,
585 int fd
586){
drhbfe66312006-10-03 17:40:40 +0000587
588#ifdef SQLITE_FIXED_LOCKING_STYLE
589 return (sqlite3LockingStyle)SQLITE_FIXED_LOCKING_STYLE;
590#else
591 struct statfs fsInfo;
592
593 if (statfs(filePath, &fsInfo) == -1)
594 return sqlite3TestLockingStyle(filePath, fd);
595
596 if (fsInfo.f_flags & MNT_RDONLY)
597 return noLockingStyle;
598
599 if( (!strcmp(fsInfo.f_fstypename, "hfs")) ||
600 (!strcmp(fsInfo.f_fstypename, "ufs")) )
drhfd131da2007-08-07 17:13:03 +0000601 return posixLockingStyle;
drhbfe66312006-10-03 17:40:40 +0000602
603 if(!strcmp(fsInfo.f_fstypename, "afpfs"))
604 return afpLockingStyle;
605
606 if(!strcmp(fsInfo.f_fstypename, "nfs"))
607 return sqlite3TestLockingStyle(filePath, fd);
608
609 if(!strcmp(fsInfo.f_fstypename, "smbfs"))
610 return flockLockingStyle;
611
612 if(!strcmp(fsInfo.f_fstypename, "msdos"))
613 return dotlockLockingStyle;
614
615 if(!strcmp(fsInfo.f_fstypename, "webdav"))
616 return unsupportedLockingStyle;
617
618 return sqlite3TestLockingStyle(filePath, fd);
drh3b62b2f2007-06-08 18:27:03 +0000619#endif /* SQLITE_FIXED_LOCKING_STYLE */
drhbfe66312006-10-03 17:40:40 +0000620}
621
622#endif /* SQLITE_ENABLE_LOCKING_STYLE */
623
drhbbd42a62004-05-22 17:41:58 +0000624/*
625** Given a file descriptor, locate lockInfo and openCnt structures that
drh029b44b2006-01-15 00:13:15 +0000626** describes that file descriptor. Create new ones if necessary. The
627** return values might be uninitialized if an error occurs.
drhbbd42a62004-05-22 17:41:58 +0000628**
629** Return the number of errors.
630*/
drh38f82712004-06-18 17:10:16 +0000631static int findLockInfo(
drhbbd42a62004-05-22 17:41:58 +0000632 int fd, /* The file descriptor used in the key */
633 struct lockInfo **ppLock, /* Return the lockInfo structure here */
drh5fdae772004-06-29 03:29:00 +0000634 struct openCnt **ppOpen /* Return the openCnt structure here */
drhbbd42a62004-05-22 17:41:58 +0000635){
636 int rc;
637 struct lockKey key1;
638 struct openKey key2;
639 struct stat statbuf;
640 struct lockInfo *pLock;
641 struct openCnt *pOpen;
642 rc = fstat(fd, &statbuf);
643 if( rc!=0 ) return 1;
danielk1977441b09a2006-01-05 13:48:29 +0000644
drhbbd42a62004-05-22 17:41:58 +0000645 memset(&key1, 0, sizeof(key1));
646 key1.dev = statbuf.st_dev;
647 key1.ino = statbuf.st_ino;
drhd677b3d2007-08-20 22:48:41 +0000648#if SQLITE_THREADSAFE
drh5fdae772004-06-29 03:29:00 +0000649 if( threadsOverrideEachOthersLocks<0 ){
650 testThreadLockingBehavior(fd);
651 }
652 key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
653#endif
drhbbd42a62004-05-22 17:41:58 +0000654 memset(&key2, 0, sizeof(key2));
655 key2.dev = statbuf.st_dev;
656 key2.ino = statbuf.st_ino;
657 pLock = (struct lockInfo*)sqlite3HashFind(&lockHash, &key1, sizeof(key1));
658 if( pLock==0 ){
659 struct lockInfo *pOld;
drh17435752007-08-16 04:30:38 +0000660 pLock = sqlite3_malloc( sizeof(*pLock) );
danielk1977441b09a2006-01-05 13:48:29 +0000661 if( pLock==0 ){
662 rc = 1;
663 goto exit_findlockinfo;
664 }
drhbbd42a62004-05-22 17:41:58 +0000665 pLock->key = key1;
666 pLock->nRef = 1;
667 pLock->cnt = 0;
danielk19779a1d0ab2004-06-01 14:09:28 +0000668 pLock->locktype = 0;
drhbbd42a62004-05-22 17:41:58 +0000669 pOld = sqlite3HashInsert(&lockHash, &pLock->key, sizeof(key1), pLock);
670 if( pOld!=0 ){
671 assert( pOld==pLock );
drh17435752007-08-16 04:30:38 +0000672 sqlite3_free(pLock);
danielk1977441b09a2006-01-05 13:48:29 +0000673 rc = 1;
674 goto exit_findlockinfo;
drhbbd42a62004-05-22 17:41:58 +0000675 }
676 }else{
677 pLock->nRef++;
678 }
679 *ppLock = pLock;
drh029b44b2006-01-15 00:13:15 +0000680 if( ppOpen!=0 ){
681 pOpen = (struct openCnt*)sqlite3HashFind(&openHash, &key2, sizeof(key2));
drhbbd42a62004-05-22 17:41:58 +0000682 if( pOpen==0 ){
drh029b44b2006-01-15 00:13:15 +0000683 struct openCnt *pOld;
drh17435752007-08-16 04:30:38 +0000684 pOpen = sqlite3_malloc( sizeof(*pOpen) );
drh029b44b2006-01-15 00:13:15 +0000685 if( pOpen==0 ){
686 releaseLockInfo(pLock);
687 rc = 1;
688 goto exit_findlockinfo;
689 }
690 pOpen->key = key2;
691 pOpen->nRef = 1;
692 pOpen->nLock = 0;
693 pOpen->nPending = 0;
694 pOpen->aPending = 0;
695 pOld = sqlite3HashInsert(&openHash, &pOpen->key, sizeof(key2), pOpen);
696 if( pOld!=0 ){
697 assert( pOld==pOpen );
drh17435752007-08-16 04:30:38 +0000698 sqlite3_free(pOpen);
drh029b44b2006-01-15 00:13:15 +0000699 releaseLockInfo(pLock);
700 rc = 1;
701 goto exit_findlockinfo;
702 }
703 }else{
704 pOpen->nRef++;
drhbbd42a62004-05-22 17:41:58 +0000705 }
drh029b44b2006-01-15 00:13:15 +0000706 *ppOpen = pOpen;
drhbbd42a62004-05-22 17:41:58 +0000707 }
danielk1977441b09a2006-01-05 13:48:29 +0000708
709exit_findlockinfo:
danielk1977441b09a2006-01-05 13:48:29 +0000710 return rc;
drhbbd42a62004-05-22 17:41:58 +0000711}
712
drh64b1bea2006-01-15 02:30:57 +0000713#ifdef SQLITE_DEBUG
714/*
715** Helper function for printing out trace information from debugging
716** binaries. This returns the string represetation of the supplied
717** integer lock-type.
718*/
719static const char *locktypeName(int locktype){
720 switch( locktype ){
721 case NO_LOCK: return "NONE";
722 case SHARED_LOCK: return "SHARED";
723 case RESERVED_LOCK: return "RESERVED";
724 case PENDING_LOCK: return "PENDING";
725 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
726 }
727 return "ERROR";
728}
729#endif
730
drhbbd42a62004-05-22 17:41:58 +0000731/*
drh029b44b2006-01-15 00:13:15 +0000732** If we are currently in a different thread than the thread that the
733** unixFile argument belongs to, then transfer ownership of the unixFile
734** over to the current thread.
735**
736** A unixFile is only owned by a thread on systems where one thread is
737** unable to override locks created by a different thread. RedHat9 is
738** an example of such a system.
739**
740** Ownership transfer is only allowed if the unixFile is currently unlocked.
741** If the unixFile is locked and an ownership is wrong, then return
drhf1a221e2006-01-15 17:27:17 +0000742** SQLITE_MISUSE. SQLITE_OK is returned if everything works.
drh029b44b2006-01-15 00:13:15 +0000743*/
drhd677b3d2007-08-20 22:48:41 +0000744#if SQLITE_THREADSAFE
drh029b44b2006-01-15 00:13:15 +0000745static int transferOwnership(unixFile *pFile){
drh64b1bea2006-01-15 02:30:57 +0000746 int rc;
drh029b44b2006-01-15 00:13:15 +0000747 pthread_t hSelf;
748 if( threadsOverrideEachOthersLocks ){
749 /* Ownership transfers not needed on this system */
750 return SQLITE_OK;
751 }
752 hSelf = pthread_self();
753 if( pthread_equal(pFile->tid, hSelf) ){
754 /* We are still in the same thread */
drh4f0c5872007-03-26 22:05:01 +0000755 OSTRACE1("No-transfer, same thread\n");
drh029b44b2006-01-15 00:13:15 +0000756 return SQLITE_OK;
757 }
758 if( pFile->locktype!=NO_LOCK ){
759 /* We cannot change ownership while we are holding a lock! */
760 return SQLITE_MISUSE;
761 }
drh4f0c5872007-03-26 22:05:01 +0000762 OSTRACE4("Transfer ownership of %d from %d to %d\n",
763 pFile->h, pFile->tid, hSelf);
drh029b44b2006-01-15 00:13:15 +0000764 pFile->tid = hSelf;
drhbfe66312006-10-03 17:40:40 +0000765 if (pFile->pLock != NULL) {
766 releaseLockInfo(pFile->pLock);
767 rc = findLockInfo(pFile->h, &pFile->pLock, 0);
drh4f0c5872007-03-26 22:05:01 +0000768 OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +0000769 locktypeName(pFile->locktype),
770 locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
771 return rc;
772 } else {
773 return SQLITE_OK;
774 }
drh029b44b2006-01-15 00:13:15 +0000775}
776#else
drhf1a221e2006-01-15 17:27:17 +0000777 /* On single-threaded builds, ownership transfer is a no-op */
drh029b44b2006-01-15 00:13:15 +0000778# define transferOwnership(X) SQLITE_OK
779#endif
780
781/*
danielk19772a6bdf62007-08-20 16:07:00 +0000782** Seek to the offset passed as the second argument, then read cnt
783** bytes into pBuf. Return the number of bytes actually read.
drhb912b282006-03-23 22:42:20 +0000784*/
danielk197762079062007-08-15 17:08:46 +0000785static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
drhb912b282006-03-23 22:42:20 +0000786 int got;
drh8ebf6702007-02-06 11:11:08 +0000787 i64 newOffset;
drh15d00c42007-02-27 02:01:14 +0000788 TIMER_START;
drh8350a212007-03-22 15:22:06 +0000789#if defined(USE_PREAD)
danielk197762079062007-08-15 17:08:46 +0000790 got = pread(id->h, pBuf, cnt, offset);
drhbb5f18d2007-04-06 18:23:17 +0000791 SimulateIOError( got = -1 );
drh8350a212007-03-22 15:22:06 +0000792#elif defined(USE_PREAD64)
danielk197762079062007-08-15 17:08:46 +0000793 got = pread64(id->h, pBuf, cnt, offset);
drhbb5f18d2007-04-06 18:23:17 +0000794 SimulateIOError( got = -1 );
drhb912b282006-03-23 22:42:20 +0000795#else
danielk197762079062007-08-15 17:08:46 +0000796 newOffset = lseek(id->h, offset, SEEK_SET);
drhbb5f18d2007-04-06 18:23:17 +0000797 SimulateIOError( newOffset-- );
danielk197762079062007-08-15 17:08:46 +0000798 if( newOffset!=offset ){
drh8ebf6702007-02-06 11:11:08 +0000799 return -1;
800 }
drhb912b282006-03-23 22:42:20 +0000801 got = read(id->h, pBuf, cnt);
802#endif
drh15d00c42007-02-27 02:01:14 +0000803 TIMER_END;
danielk1977967a4a12007-08-20 14:23:44 +0000804 OSTRACE5("READ %-3d %5d %7lld %d\n", id->h, got, offset, TIMER_ELAPSED);
drhb912b282006-03-23 22:42:20 +0000805 return got;
806}
807
808/*
drhbbd42a62004-05-22 17:41:58 +0000809** Read data from a file into a buffer. Return SQLITE_OK if all
810** bytes were read successfully and SQLITE_IOERR if anything goes
811** wrong.
812*/
danielk197762079062007-08-15 17:08:46 +0000813static int unixRead(
814 sqlite3_file *id,
815 void *pBuf,
816 int amt,
817 sqlite3_int64 offset
818){
drhbbd42a62004-05-22 17:41:58 +0000819 int got;
drh9cbe6352005-11-29 03:13:21 +0000820 assert( id );
danielk197762079062007-08-15 17:08:46 +0000821 got = seekAndRead((unixFile*)id, offset, pBuf, amt);
drhbbd42a62004-05-22 17:41:58 +0000822 if( got==amt ){
823 return SQLITE_OK;
drh4ac285a2006-09-15 07:28:50 +0000824 }else if( got<0 ){
825 return SQLITE_IOERR_READ;
drhbbd42a62004-05-22 17:41:58 +0000826 }else{
drhbafda092007-01-03 23:36:22 +0000827 memset(&((char*)pBuf)[got], 0, amt-got);
drh4ac285a2006-09-15 07:28:50 +0000828 return SQLITE_IOERR_SHORT_READ;
drhbbd42a62004-05-22 17:41:58 +0000829 }
830}
831
832/*
drhb912b282006-03-23 22:42:20 +0000833** Seek to the offset in id->offset then read cnt bytes into pBuf.
834** Return the number of bytes actually read. Update the offset.
835*/
danielk197762079062007-08-15 17:08:46 +0000836static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
drhb912b282006-03-23 22:42:20 +0000837 int got;
drh8ebf6702007-02-06 11:11:08 +0000838 i64 newOffset;
drh15d00c42007-02-27 02:01:14 +0000839 TIMER_START;
drh8350a212007-03-22 15:22:06 +0000840#if defined(USE_PREAD)
danielk197762079062007-08-15 17:08:46 +0000841 got = pwrite(id->h, pBuf, cnt, offset);
drh8350a212007-03-22 15:22:06 +0000842#elif defined(USE_PREAD64)
danielk197762079062007-08-15 17:08:46 +0000843 got = pwrite64(id->h, pBuf, cnt, offset);
drhb912b282006-03-23 22:42:20 +0000844#else
danielk197762079062007-08-15 17:08:46 +0000845 newOffset = lseek(id->h, offset, SEEK_SET);
846 if( newOffset!=offset ){
drh8ebf6702007-02-06 11:11:08 +0000847 return -1;
848 }
drhb912b282006-03-23 22:42:20 +0000849 got = write(id->h, pBuf, cnt);
850#endif
drh15d00c42007-02-27 02:01:14 +0000851 TIMER_END;
danielk197762079062007-08-15 17:08:46 +0000852 OSTRACE5("WRITE %-3d %5d %7lld %d\n", id->h, got, offset, TIMER_ELAPSED);
drhb912b282006-03-23 22:42:20 +0000853 return got;
854}
855
856
857/*
drhbbd42a62004-05-22 17:41:58 +0000858** Write data from a buffer into a file. Return SQLITE_OK on success
859** or some other error code on failure.
860*/
danielk197762079062007-08-15 17:08:46 +0000861static int unixWrite(
862 sqlite3_file *id,
863 const void *pBuf,
864 int amt,
865 sqlite3_int64 offset
866){
drhbbd42a62004-05-22 17:41:58 +0000867 int wrote = 0;
drh9cbe6352005-11-29 03:13:21 +0000868 assert( id );
drh4c7f9412005-02-03 00:29:47 +0000869 assert( amt>0 );
danielk197762079062007-08-15 17:08:46 +0000870 while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){
drhbbd42a62004-05-22 17:41:58 +0000871 amt -= wrote;
danielk197762079062007-08-15 17:08:46 +0000872 offset += wrote;
drhbbd42a62004-05-22 17:41:58 +0000873 pBuf = &((char*)pBuf)[wrote];
874 }
drh59685932006-09-14 13:47:11 +0000875 SimulateIOError(( wrote=(-1), amt=1 ));
876 SimulateDiskfullError(( wrote=0, amt=1 ));
drhbbd42a62004-05-22 17:41:58 +0000877 if( amt>0 ){
drh59685932006-09-14 13:47:11 +0000878 if( wrote<0 ){
drh4ac285a2006-09-15 07:28:50 +0000879 return SQLITE_IOERR_WRITE;
drh59685932006-09-14 13:47:11 +0000880 }else{
881 return SQLITE_FULL;
882 }
drhbbd42a62004-05-22 17:41:58 +0000883 }
884 return SQLITE_OK;
885}
886
drhb851b2c2005-03-10 14:11:12 +0000887#ifdef SQLITE_TEST
888/*
889** Count the number of fullsyncs and normal syncs. This is used to test
890** that syncs and fullsyncs are occuring at the right times.
891*/
892int sqlite3_sync_count = 0;
893int sqlite3_fullsync_count = 0;
894#endif
895
drhf2f23912005-10-05 10:29:36 +0000896/*
897** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.
898** Otherwise use fsync() in its place.
899*/
900#ifndef HAVE_FDATASYNC
901# define fdatasync fsync
902#endif
903
drhac530b12006-02-11 01:25:50 +0000904/*
905** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
906** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
907** only available on Mac OS X. But that could change.
908*/
909#ifdef F_FULLFSYNC
910# define HAVE_FULLFSYNC 1
911#else
912# define HAVE_FULLFSYNC 0
913#endif
914
drhb851b2c2005-03-10 14:11:12 +0000915
drhbbd42a62004-05-22 17:41:58 +0000916/*
drhdd809b02004-07-17 21:44:57 +0000917** The fsync() system call does not work as advertised on many
918** unix systems. The following procedure is an attempt to make
919** it work better.
drh1398ad32005-01-19 23:24:50 +0000920**
921** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
922** for testing when we want to run through the test suite quickly.
923** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
924** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
925** or power failure will likely corrupt the database file.
drhdd809b02004-07-17 21:44:57 +0000926*/
drheb796a72005-09-08 12:38:41 +0000927static int full_fsync(int fd, int fullSync, int dataOnly){
drhdd809b02004-07-17 21:44:57 +0000928 int rc;
drhb851b2c2005-03-10 14:11:12 +0000929
930 /* Record the number of times that we do a normal fsync() and
931 ** FULLSYNC. This is used during testing to verify that this procedure
932 ** gets called with the correct arguments.
933 */
934#ifdef SQLITE_TEST
935 if( fullSync ) sqlite3_fullsync_count++;
936 sqlite3_sync_count++;
937#endif
938
939 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
940 ** no-op
941 */
942#ifdef SQLITE_NO_SYNC
943 rc = SQLITE_OK;
944#else
945
drhac530b12006-02-11 01:25:50 +0000946#if HAVE_FULLFSYNC
drhb851b2c2005-03-10 14:11:12 +0000947 if( fullSync ){
drhf30cc942005-03-11 17:52:34 +0000948 rc = fcntl(fd, F_FULLFSYNC, 0);
aswiftae0943b2007-01-31 23:37:07 +0000949 }else{
950 rc = 1;
951 }
952 /* If the FULLFSYNC failed, fall back to attempting an fsync().
953 * It shouldn't be possible for fullfsync to fail on the local
954 * file system (on OSX), so failure indicates that FULLFSYNC
955 * isn't supported for this file system. So, attempt an fsync
956 * and (for now) ignore the overhead of a superfluous fcntl call.
957 * It'd be better to detect fullfsync support once and avoid
958 * the fcntl call every time sync is called.
959 */
960 if( rc ) rc = fsync(fd);
961
962#else
drheb796a72005-09-08 12:38:41 +0000963 if( dataOnly ){
964 rc = fdatasync(fd);
drhf2f23912005-10-05 10:29:36 +0000965 }else{
drheb796a72005-09-08 12:38:41 +0000966 rc = fsync(fd);
967 }
aswiftae0943b2007-01-31 23:37:07 +0000968#endif /* HAVE_FULLFSYNC */
drhb851b2c2005-03-10 14:11:12 +0000969#endif /* defined(SQLITE_NO_SYNC) */
970
drhdd809b02004-07-17 21:44:57 +0000971 return rc;
972}
973
974/*
drhbbd42a62004-05-22 17:41:58 +0000975** Make sure all writes to a particular file are committed to disk.
976**
drheb796a72005-09-08 12:38:41 +0000977** If dataOnly==0 then both the file itself and its metadata (file
978** size, access time, etc) are synced. If dataOnly!=0 then only the
979** file data is synced.
980**
drhbbd42a62004-05-22 17:41:58 +0000981** Under Unix, also make sure that the directory entry for the file
982** has been created by fsync-ing the directory that contains the file.
983** If we do not do this and we encounter a power failure, the directory
984** entry for the journal might not exist after we reboot. The next
985** SQLite to access the file will not know that the journal exists (because
986** the directory entry for the journal was never created) and the transaction
987** will not roll back - possibly leading to database corruption.
988*/
danielk197790949c22007-08-17 16:50:38 +0000989static int unixSync(sqlite3_file *id, int flags){
drh59685932006-09-14 13:47:11 +0000990 int rc;
drh054889e2005-11-30 03:20:31 +0000991 unixFile *pFile = (unixFile*)id;
danielk197790949c22007-08-17 16:50:38 +0000992
danielk1977f036aef2007-08-20 05:36:51 +0000993 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
994 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
995
996 /* Check that one of SQLITE_SYNC_NORMAL, FULL or BARRIER was passed */
997 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
998 || (flags&0x0F)==SQLITE_SYNC_FULL
999 || (flags&0x0F)==SQLITE_SYNC_BARRIER
1000 );
danielk197790949c22007-08-17 16:50:38 +00001001
drh054889e2005-11-30 03:20:31 +00001002 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001003 OSTRACE2("SYNC %-3d\n", pFile->h);
danielk197790949c22007-08-17 16:50:38 +00001004 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
drh59685932006-09-14 13:47:11 +00001005 SimulateIOError( rc=1 );
1006 if( rc ){
drh4ac285a2006-09-15 07:28:50 +00001007 return SQLITE_IOERR_FSYNC;
drhbbd42a62004-05-22 17:41:58 +00001008 }
drh054889e2005-11-30 03:20:31 +00001009 if( pFile->dirfd>=0 ){
drh4f0c5872007-03-26 22:05:01 +00001010 OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
danielk197790949c22007-08-17 16:50:38 +00001011 HAVE_FULLFSYNC, isFullsync);
danielk1977d7c03f72005-11-25 10:38:22 +00001012#ifndef SQLITE_DISABLE_DIRSYNC
drhac530b12006-02-11 01:25:50 +00001013 /* The directory sync is only attempted if full_fsync is
1014 ** turned off or unavailable. If a full_fsync occurred above,
1015 ** then the directory sync is superfluous.
1016 */
danielk197790949c22007-08-17 16:50:38 +00001017 if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
drhac530b12006-02-11 01:25:50 +00001018 /*
1019 ** We have received multiple reports of fsync() returning
drh86631a52006-02-09 23:05:51 +00001020 ** errors when applied to directories on certain file systems.
1021 ** A failed directory sync is not a big deal. So it seems
1022 ** better to ignore the error. Ticket #1657
1023 */
1024 /* return SQLITE_IOERR; */
danielk19770964b232005-11-25 08:47:57 +00001025 }
danielk1977d7c03f72005-11-25 10:38:22 +00001026#endif
drh054889e2005-11-30 03:20:31 +00001027 close(pFile->dirfd); /* Only need to sync once, so close the directory */
1028 pFile->dirfd = -1; /* when we are done. */
drha2854222004-06-17 19:04:17 +00001029 }
drha2854222004-06-17 19:04:17 +00001030 return SQLITE_OK;
drhbbd42a62004-05-22 17:41:58 +00001031}
1032
1033/*
1034** Truncate an open file to a specified size
1035*/
danielk197762079062007-08-15 17:08:46 +00001036static int unixTruncate(sqlite3_file *id, i64 nByte){
drh59685932006-09-14 13:47:11 +00001037 int rc;
drh9cbe6352005-11-29 03:13:21 +00001038 assert( id );
drh63fff5f2007-06-19 10:50:38 +00001039 rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);
drh59685932006-09-14 13:47:11 +00001040 SimulateIOError( rc=1 );
1041 if( rc ){
drh4ac285a2006-09-15 07:28:50 +00001042 return SQLITE_IOERR_TRUNCATE;
drh59685932006-09-14 13:47:11 +00001043 }else{
1044 return SQLITE_OK;
1045 }
drhbbd42a62004-05-22 17:41:58 +00001046}
1047
1048/*
1049** Determine the current size of a file in bytes
1050*/
danielk197762079062007-08-15 17:08:46 +00001051static int unixFileSize(sqlite3_file *id, i64 *pSize){
drh59685932006-09-14 13:47:11 +00001052 int rc;
drhbbd42a62004-05-22 17:41:58 +00001053 struct stat buf;
drh9cbe6352005-11-29 03:13:21 +00001054 assert( id );
drh59685932006-09-14 13:47:11 +00001055 rc = fstat(((unixFile*)id)->h, &buf);
1056 SimulateIOError( rc=1 );
1057 if( rc!=0 ){
drh4ac285a2006-09-15 07:28:50 +00001058 return SQLITE_IOERR_FSTAT;
drhbbd42a62004-05-22 17:41:58 +00001059 }
1060 *pSize = buf.st_size;
1061 return SQLITE_OK;
1062}
1063
danielk19779a1d0ab2004-06-01 14:09:28 +00001064/*
danielk197713adf8a2004-06-03 16:08:41 +00001065** This routine checks if there is a RESERVED lock held on the specified
1066** file by this or any other process. If such a lock is held, return
drh2ac3ee92004-06-07 16:27:46 +00001067** non-zero. If the file is unlocked or holds only SHARED locks, then
1068** return zero.
danielk197713adf8a2004-06-03 16:08:41 +00001069*/
danielk197762079062007-08-15 17:08:46 +00001070static int unixCheckReservedLock(sqlite3_file *id){
danielk197713adf8a2004-06-03 16:08:41 +00001071 int r = 0;
drh054889e2005-11-30 03:20:31 +00001072 unixFile *pFile = (unixFile*)id;
danielk197713adf8a2004-06-03 16:08:41 +00001073
drh054889e2005-11-30 03:20:31 +00001074 assert( pFile );
danielk1977b4b47412007-08-17 15:53:36 +00001075 enterMutex(); /* Because pFile->pLock is shared across threads */
danielk197713adf8a2004-06-03 16:08:41 +00001076
1077 /* Check if a thread in this process holds such a lock */
drh054889e2005-11-30 03:20:31 +00001078 if( pFile->pLock->locktype>SHARED_LOCK ){
danielk197713adf8a2004-06-03 16:08:41 +00001079 r = 1;
1080 }
1081
drh2ac3ee92004-06-07 16:27:46 +00001082 /* Otherwise see if some other process holds it.
danielk197713adf8a2004-06-03 16:08:41 +00001083 */
1084 if( !r ){
1085 struct flock lock;
1086 lock.l_whence = SEEK_SET;
drh2ac3ee92004-06-07 16:27:46 +00001087 lock.l_start = RESERVED_BYTE;
1088 lock.l_len = 1;
1089 lock.l_type = F_WRLCK;
drh054889e2005-11-30 03:20:31 +00001090 fcntl(pFile->h, F_GETLK, &lock);
danielk197713adf8a2004-06-03 16:08:41 +00001091 if( lock.l_type!=F_UNLCK ){
1092 r = 1;
1093 }
1094 }
1095
danielk1977b4b47412007-08-17 15:53:36 +00001096 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001097 OSTRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
danielk197713adf8a2004-06-03 16:08:41 +00001098
1099 return r;
1100}
1101
1102/*
danielk19779a1d0ab2004-06-01 14:09:28 +00001103** Lock the file with the lock specified by parameter locktype - one
1104** of the following:
1105**
drh2ac3ee92004-06-07 16:27:46 +00001106** (1) SHARED_LOCK
1107** (2) RESERVED_LOCK
1108** (3) PENDING_LOCK
1109** (4) EXCLUSIVE_LOCK
1110**
drhb3e04342004-06-08 00:47:47 +00001111** Sometimes when requesting one lock state, additional lock states
1112** are inserted in between. The locking might fail on one of the later
1113** transitions leaving the lock state different from what it started but
1114** still short of its goal. The following chart shows the allowed
1115** transitions and the inserted intermediate states:
1116**
1117** UNLOCKED -> SHARED
1118** SHARED -> RESERVED
1119** SHARED -> (PENDING) -> EXCLUSIVE
1120** RESERVED -> (PENDING) -> EXCLUSIVE
1121** PENDING -> EXCLUSIVE
drh2ac3ee92004-06-07 16:27:46 +00001122**
drha6abd042004-06-09 17:37:22 +00001123** This routine will only increase a lock. Use the sqlite3OsUnlock()
1124** routine to lower a locking level.
danielk19779a1d0ab2004-06-01 14:09:28 +00001125*/
danielk197762079062007-08-15 17:08:46 +00001126static int unixLock(sqlite3_file *id, int locktype){
danielk1977f42f25c2004-06-25 07:21:28 +00001127 /* The following describes the implementation of the various locks and
1128 ** lock transitions in terms of the POSIX advisory shared and exclusive
1129 ** lock primitives (called read-locks and write-locks below, to avoid
1130 ** confusion with SQLite lock names). The algorithms are complicated
1131 ** slightly in order to be compatible with windows systems simultaneously
1132 ** accessing the same database file, in case that is ever required.
1133 **
1134 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
1135 ** byte', each single bytes at well known offsets, and the 'shared byte
1136 ** range', a range of 510 bytes at a well known offset.
1137 **
1138 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
1139 ** byte'. If this is successful, a random byte from the 'shared byte
1140 ** range' is read-locked and the lock on the 'pending byte' released.
1141 **
danielk197790ba3bd2004-06-25 08:32:25 +00001142 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
1143 ** A RESERVED lock is implemented by grabbing a write-lock on the
1144 ** 'reserved byte'.
danielk1977f42f25c2004-06-25 07:21:28 +00001145 **
1146 ** A process may only obtain a PENDING lock after it has obtained a
danielk197790ba3bd2004-06-25 08:32:25 +00001147 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
1148 ** on the 'pending byte'. This ensures that no new SHARED locks can be
1149 ** obtained, but existing SHARED locks are allowed to persist. A process
1150 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
1151 ** This property is used by the algorithm for rolling back a journal file
1152 ** after a crash.
danielk1977f42f25c2004-06-25 07:21:28 +00001153 **
danielk197790ba3bd2004-06-25 08:32:25 +00001154 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
1155 ** implemented by obtaining a write-lock on the entire 'shared byte
1156 ** range'. Since all other locks require a read-lock on one of the bytes
1157 ** within this range, this ensures that no other locks are held on the
1158 ** database.
danielk1977f42f25c2004-06-25 07:21:28 +00001159 **
1160 ** The reason a single byte cannot be used instead of the 'shared byte
1161 ** range' is that some versions of windows do not support read-locks. By
1162 ** locking a random byte from a range, concurrent SHARED locks may exist
1163 ** even if the locking primitive used is always a write-lock.
1164 */
danielk19779a1d0ab2004-06-01 14:09:28 +00001165 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001166 unixFile *pFile = (unixFile*)id;
1167 struct lockInfo *pLock = pFile->pLock;
danielk19779a1d0ab2004-06-01 14:09:28 +00001168 struct flock lock;
1169 int s;
1170
drh054889e2005-11-30 03:20:31 +00001171 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001172 OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001173 locktypeName(locktype), locktypeName(pFile->locktype),
1174 locktypeName(pLock->locktype), pLock->cnt , getpid());
danielk19779a1d0ab2004-06-01 14:09:28 +00001175
1176 /* If there is already a lock of this type or more restrictive on the
danielk1977ad94b582007-08-20 06:44:22 +00001177 ** unixFile, do nothing. Don't use the end_lock: exit path, as
danielk1977b4b47412007-08-17 15:53:36 +00001178 ** enterMutex() hasn't been called yet.
danielk19779a1d0ab2004-06-01 14:09:28 +00001179 */
drh054889e2005-11-30 03:20:31 +00001180 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00001181 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001182 locktypeName(locktype));
danielk19779a1d0ab2004-06-01 14:09:28 +00001183 return SQLITE_OK;
1184 }
1185
drhb3e04342004-06-08 00:47:47 +00001186 /* Make sure the locking sequence is correct
drh2ac3ee92004-06-07 16:27:46 +00001187 */
drh054889e2005-11-30 03:20:31 +00001188 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
drhb3e04342004-06-08 00:47:47 +00001189 assert( locktype!=PENDING_LOCK );
drh054889e2005-11-30 03:20:31 +00001190 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
drh2ac3ee92004-06-07 16:27:46 +00001191
drh054889e2005-11-30 03:20:31 +00001192 /* This mutex is needed because pFile->pLock is shared across threads
drhb3e04342004-06-08 00:47:47 +00001193 */
danielk1977b4b47412007-08-17 15:53:36 +00001194 enterMutex();
danielk19779a1d0ab2004-06-01 14:09:28 +00001195
drh029b44b2006-01-15 00:13:15 +00001196 /* Make sure the current thread owns the pFile.
1197 */
1198 rc = transferOwnership(pFile);
1199 if( rc!=SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00001200 leaveMutex();
drh029b44b2006-01-15 00:13:15 +00001201 return rc;
1202 }
drh64b1bea2006-01-15 02:30:57 +00001203 pLock = pFile->pLock;
drh029b44b2006-01-15 00:13:15 +00001204
danielk1977ad94b582007-08-20 06:44:22 +00001205 /* If some thread using this PID has a lock via a different unixFile*
danielk19779a1d0ab2004-06-01 14:09:28 +00001206 ** handle that precludes the requested lock, return BUSY.
1207 */
drh054889e2005-11-30 03:20:31 +00001208 if( (pFile->locktype!=pLock->locktype &&
drh2ac3ee92004-06-07 16:27:46 +00001209 (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
danielk19779a1d0ab2004-06-01 14:09:28 +00001210 ){
1211 rc = SQLITE_BUSY;
1212 goto end_lock;
1213 }
1214
1215 /* If a SHARED lock is requested, and some thread using this PID already
1216 ** has a SHARED or RESERVED lock, then increment reference counts and
1217 ** return SQLITE_OK.
1218 */
1219 if( locktype==SHARED_LOCK &&
1220 (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
1221 assert( locktype==SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001222 assert( pFile->locktype==0 );
danielk1977ecb2a962004-06-02 06:30:16 +00001223 assert( pLock->cnt>0 );
drh054889e2005-11-30 03:20:31 +00001224 pFile->locktype = SHARED_LOCK;
danielk19779a1d0ab2004-06-01 14:09:28 +00001225 pLock->cnt++;
drh054889e2005-11-30 03:20:31 +00001226 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001227 goto end_lock;
1228 }
1229
danielk197713adf8a2004-06-03 16:08:41 +00001230 lock.l_len = 1L;
drh2b4b5962005-06-15 17:47:55 +00001231
danielk19779a1d0ab2004-06-01 14:09:28 +00001232 lock.l_whence = SEEK_SET;
1233
drh3cde3bb2004-06-12 02:17:14 +00001234 /* A PENDING lock is needed before acquiring a SHARED lock and before
1235 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1236 ** be released.
danielk19779a1d0ab2004-06-01 14:09:28 +00001237 */
drh3cde3bb2004-06-12 02:17:14 +00001238 if( locktype==SHARED_LOCK
drh054889e2005-11-30 03:20:31 +00001239 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh3cde3bb2004-06-12 02:17:14 +00001240 ){
danielk1977489468c2004-06-28 08:25:47 +00001241 lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
drh2ac3ee92004-06-07 16:27:46 +00001242 lock.l_start = PENDING_BYTE;
drh054889e2005-11-30 03:20:31 +00001243 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001244 if( s==(-1) ){
danielk19779a1d0ab2004-06-01 14:09:28 +00001245 rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
1246 goto end_lock;
1247 }
drh3cde3bb2004-06-12 02:17:14 +00001248 }
1249
1250
1251 /* If control gets to this point, then actually go ahead and make
1252 ** operating system calls for the specified lock.
1253 */
1254 if( locktype==SHARED_LOCK ){
1255 assert( pLock->cnt==0 );
1256 assert( pLock->locktype==0 );
danielk19779a1d0ab2004-06-01 14:09:28 +00001257
drh2ac3ee92004-06-07 16:27:46 +00001258 /* Now get the read-lock */
1259 lock.l_start = SHARED_FIRST;
1260 lock.l_len = SHARED_SIZE;
drh054889e2005-11-30 03:20:31 +00001261 s = fcntl(pFile->h, F_SETLK, &lock);
drh2ac3ee92004-06-07 16:27:46 +00001262
1263 /* Drop the temporary PENDING lock */
1264 lock.l_start = PENDING_BYTE;
1265 lock.l_len = 1L;
danielk19779a1d0ab2004-06-01 14:09:28 +00001266 lock.l_type = F_UNLCK;
drh054889e2005-11-30 03:20:31 +00001267 if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
drh4ac285a2006-09-15 07:28:50 +00001268 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
drh2b4b5962005-06-15 17:47:55 +00001269 goto end_lock;
1270 }
drhe2396a12007-03-29 20:19:58 +00001271 if( s==(-1) ){
drhbbd42a62004-05-22 17:41:58 +00001272 rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
1273 }else{
drh054889e2005-11-30 03:20:31 +00001274 pFile->locktype = SHARED_LOCK;
1275 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001276 pLock->cnt = 1;
drhbbd42a62004-05-22 17:41:58 +00001277 }
drh3cde3bb2004-06-12 02:17:14 +00001278 }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
1279 /* We are trying for an exclusive lock but another thread in this
1280 ** same process is still holding a shared lock. */
1281 rc = SQLITE_BUSY;
drhbbd42a62004-05-22 17:41:58 +00001282 }else{
drh3cde3bb2004-06-12 02:17:14 +00001283 /* The request was for a RESERVED or EXCLUSIVE lock. It is
danielk19779a1d0ab2004-06-01 14:09:28 +00001284 ** assumed that there is a SHARED or greater lock on the file
1285 ** already.
1286 */
drh054889e2005-11-30 03:20:31 +00001287 assert( 0!=pFile->locktype );
danielk19779a1d0ab2004-06-01 14:09:28 +00001288 lock.l_type = F_WRLCK;
1289 switch( locktype ){
1290 case RESERVED_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001291 lock.l_start = RESERVED_BYTE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001292 break;
danielk19779a1d0ab2004-06-01 14:09:28 +00001293 case EXCLUSIVE_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001294 lock.l_start = SHARED_FIRST;
1295 lock.l_len = SHARED_SIZE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001296 break;
1297 default:
1298 assert(0);
1299 }
drh054889e2005-11-30 03:20:31 +00001300 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001301 if( s==(-1) ){
danielk19779a1d0ab2004-06-01 14:09:28 +00001302 rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
1303 }
drhbbd42a62004-05-22 17:41:58 +00001304 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001305
danielk1977ecb2a962004-06-02 06:30:16 +00001306 if( rc==SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001307 pFile->locktype = locktype;
danielk1977ecb2a962004-06-02 06:30:16 +00001308 pLock->locktype = locktype;
drh3cde3bb2004-06-12 02:17:14 +00001309 }else if( locktype==EXCLUSIVE_LOCK ){
drh054889e2005-11-30 03:20:31 +00001310 pFile->locktype = PENDING_LOCK;
drh3cde3bb2004-06-12 02:17:14 +00001311 pLock->locktype = PENDING_LOCK;
danielk1977ecb2a962004-06-02 06:30:16 +00001312 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001313
1314end_lock:
danielk1977b4b47412007-08-17 15:53:36 +00001315 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001316 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
danielk19772b444852004-06-29 07:45:33 +00001317 rc==SQLITE_OK ? "ok" : "failed");
drhbbd42a62004-05-22 17:41:58 +00001318 return rc;
1319}
1320
1321/*
drh054889e2005-11-30 03:20:31 +00001322** Lower the locking level on file descriptor pFile to locktype. locktype
drha6abd042004-06-09 17:37:22 +00001323** must be either NO_LOCK or SHARED_LOCK.
1324**
1325** If the locking level of the file descriptor is already at or below
1326** the requested locking level, this routine is a no-op.
drhbbd42a62004-05-22 17:41:58 +00001327*/
danielk197762079062007-08-15 17:08:46 +00001328static int unixUnlock(sqlite3_file *id, int locktype){
drha6abd042004-06-09 17:37:22 +00001329 struct lockInfo *pLock;
1330 struct flock lock;
drh9c105bb2004-10-02 20:38:28 +00001331 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001332 unixFile *pFile = (unixFile*)id;
drha6abd042004-06-09 17:37:22 +00001333
drh054889e2005-11-30 03:20:31 +00001334 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001335 OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
drh054889e2005-11-30 03:20:31 +00001336 pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
drha6abd042004-06-09 17:37:22 +00001337
1338 assert( locktype<=SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001339 if( pFile->locktype<=locktype ){
drha6abd042004-06-09 17:37:22 +00001340 return SQLITE_OK;
1341 }
drhf1a221e2006-01-15 17:27:17 +00001342 if( CHECK_THREADID(pFile) ){
1343 return SQLITE_MISUSE;
1344 }
danielk1977b4b47412007-08-17 15:53:36 +00001345 enterMutex();
drh054889e2005-11-30 03:20:31 +00001346 pLock = pFile->pLock;
drha6abd042004-06-09 17:37:22 +00001347 assert( pLock->cnt!=0 );
drh054889e2005-11-30 03:20:31 +00001348 if( pFile->locktype>SHARED_LOCK ){
1349 assert( pLock->locktype==pFile->locktype );
drh9c105bb2004-10-02 20:38:28 +00001350 if( locktype==SHARED_LOCK ){
1351 lock.l_type = F_RDLCK;
1352 lock.l_whence = SEEK_SET;
1353 lock.l_start = SHARED_FIRST;
1354 lock.l_len = SHARED_SIZE;
drhe2396a12007-03-29 20:19:58 +00001355 if( fcntl(pFile->h, F_SETLK, &lock)==(-1) ){
drh9c105bb2004-10-02 20:38:28 +00001356 /* This should never happen */
drh4ac285a2006-09-15 07:28:50 +00001357 rc = SQLITE_IOERR_RDLOCK;
drh9c105bb2004-10-02 20:38:28 +00001358 }
1359 }
drhbbd42a62004-05-22 17:41:58 +00001360 lock.l_type = F_UNLCK;
1361 lock.l_whence = SEEK_SET;
drha6abd042004-06-09 17:37:22 +00001362 lock.l_start = PENDING_BYTE;
1363 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
drhe2396a12007-03-29 20:19:58 +00001364 if( fcntl(pFile->h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001365 pLock->locktype = SHARED_LOCK;
1366 }else{
drh4ac285a2006-09-15 07:28:50 +00001367 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
drh2b4b5962005-06-15 17:47:55 +00001368 }
drhbbd42a62004-05-22 17:41:58 +00001369 }
drha6abd042004-06-09 17:37:22 +00001370 if( locktype==NO_LOCK ){
1371 struct openCnt *pOpen;
danielk1977ecb2a962004-06-02 06:30:16 +00001372
drha6abd042004-06-09 17:37:22 +00001373 /* Decrement the shared lock counter. Release the lock using an
1374 ** OS call only when all threads in this same process have released
1375 ** the lock.
1376 */
1377 pLock->cnt--;
1378 if( pLock->cnt==0 ){
1379 lock.l_type = F_UNLCK;
1380 lock.l_whence = SEEK_SET;
1381 lock.l_start = lock.l_len = 0L;
drhe2396a12007-03-29 20:19:58 +00001382 if( fcntl(pFile->h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001383 pLock->locktype = NO_LOCK;
1384 }else{
drh4ac285a2006-09-15 07:28:50 +00001385 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
drh2b4b5962005-06-15 17:47:55 +00001386 }
drha6abd042004-06-09 17:37:22 +00001387 }
1388
drhbbd42a62004-05-22 17:41:58 +00001389 /* Decrement the count of locks against this same file. When the
1390 ** count reaches zero, close any other file descriptors whose close
1391 ** was deferred because of outstanding locks.
1392 */
drh054889e2005-11-30 03:20:31 +00001393 pOpen = pFile->pOpen;
drhbbd42a62004-05-22 17:41:58 +00001394 pOpen->nLock--;
1395 assert( pOpen->nLock>=0 );
1396 if( pOpen->nLock==0 && pOpen->nPending>0 ){
1397 int i;
1398 for(i=0; i<pOpen->nPending; i++){
1399 close(pOpen->aPending[i]);
1400 }
drh64b1bea2006-01-15 02:30:57 +00001401 free(pOpen->aPending);
drhbbd42a62004-05-22 17:41:58 +00001402 pOpen->nPending = 0;
1403 pOpen->aPending = 0;
1404 }
1405 }
danielk1977b4b47412007-08-17 15:53:36 +00001406 leaveMutex();
drh054889e2005-11-30 03:20:31 +00001407 pFile->locktype = locktype;
drh9c105bb2004-10-02 20:38:28 +00001408 return rc;
drhbbd42a62004-05-22 17:41:58 +00001409}
1410
1411/*
danielk1977e3026632004-06-22 11:29:02 +00001412** Close a file.
1413*/
danielk197762079062007-08-15 17:08:46 +00001414static int unixClose(sqlite3_file *id){
1415 unixFile *pFile = (unixFile *)id;
1416 if( !pFile ) return SQLITE_OK;
1417 unixUnlock(id, NO_LOCK);
1418 if( pFile->dirfd>=0 ) close(pFile->dirfd);
1419 pFile->dirfd = -1;
danielk1977b4b47412007-08-17 15:53:36 +00001420 enterMutex();
danielk1977441b09a2006-01-05 13:48:29 +00001421
danielk197762079062007-08-15 17:08:46 +00001422 if( pFile->pOpen->nLock ){
danielk1977e3026632004-06-22 11:29:02 +00001423 /* If there are outstanding locks, do not actually close the file just
1424 ** yet because that would clear those locks. Instead, add the file
1425 ** descriptor to pOpen->aPending. It will be automatically closed when
1426 ** the last lock is cleared.
1427 */
1428 int *aNew;
danielk197762079062007-08-15 17:08:46 +00001429 struct openCnt *pOpen = pFile->pOpen;
drh64b1bea2006-01-15 02:30:57 +00001430 aNew = realloc( pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
danielk1977e3026632004-06-22 11:29:02 +00001431 if( aNew==0 ){
1432 /* If a malloc fails, just leak the file descriptor */
1433 }else{
1434 pOpen->aPending = aNew;
danielk197762079062007-08-15 17:08:46 +00001435 pOpen->aPending[pOpen->nPending] = pFile->h;
drhad81e872005-08-21 21:45:01 +00001436 pOpen->nPending++;
danielk1977e3026632004-06-22 11:29:02 +00001437 }
1438 }else{
1439 /* There are no outstanding locks so we can close the file immediately */
danielk197762079062007-08-15 17:08:46 +00001440 close(pFile->h);
danielk1977e3026632004-06-22 11:29:02 +00001441 }
danielk197762079062007-08-15 17:08:46 +00001442 releaseLockInfo(pFile->pLock);
1443 releaseOpenCnt(pFile->pOpen);
danielk1977441b09a2006-01-05 13:48:29 +00001444
danielk1977b4b47412007-08-17 15:53:36 +00001445 leaveMutex();
danielk197762079062007-08-15 17:08:46 +00001446 pFile->isOpen = 0;
1447 OSTRACE2("CLOSE %-3d\n", pFile->h);
danielk1977e3026632004-06-22 11:29:02 +00001448 OpenCounter(-1);
danielk1977b4b47412007-08-17 15:53:36 +00001449 memset(pFile, 0, sizeof(unixFile));
drh02afc862006-01-20 18:10:57 +00001450 return SQLITE_OK;
danielk1977e3026632004-06-22 11:29:02 +00001451}
1452
drhbfe66312006-10-03 17:40:40 +00001453
1454#ifdef SQLITE_ENABLE_LOCKING_STYLE
1455#pragma mark AFP Support
1456
1457/*
1458 ** The afpLockingContext structure contains all afp lock specific state
1459 */
1460typedef struct afpLockingContext afpLockingContext;
1461struct afpLockingContext {
1462 unsigned long long sharedLockByte;
1463 char *filePath;
1464};
1465
1466struct ByteRangeLockPB2
1467{
1468 unsigned long long offset; /* offset to first byte to lock */
1469 unsigned long long length; /* nbr of bytes to lock */
1470 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
1471 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
1472 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
1473 int fd; /* file desc to assoc this lock with */
1474};
1475
drhfd131da2007-08-07 17:13:03 +00001476#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
drhbfe66312006-10-03 17:40:40 +00001477
danielk1977ad94b582007-08-20 06:44:22 +00001478/*
1479** Return 0 on success, 1 on failure. To match the behavior of the
1480** normal posix file locking (used in unixLock for example), we should
1481** provide 'richer' return codes - specifically to differentiate between
1482** 'file busy' and 'file system error' results.
1483*/
1484static int _AFPFSSetLock(
1485 const char *path,
1486 int fd,
1487 unsigned long long offset,
1488 unsigned long long length,
1489 int setLockFlag
1490){
drhfd131da2007-08-07 17:13:03 +00001491 struct ByteRangeLockPB2 pb;
drhbfe66312006-10-03 17:40:40 +00001492 int err;
1493
1494 pb.unLockFlag = setLockFlag ? 0 : 1;
1495 pb.startEndFlag = 0;
1496 pb.offset = offset;
1497 pb.length = length;
1498 pb.fd = fd;
drh4f0c5872007-03-26 22:05:01 +00001499 OSTRACE5("AFPLOCK setting lock %s for %d in range %llx:%llx\n",
drhbfe66312006-10-03 17:40:40 +00001500 (setLockFlag?"ON":"OFF"), fd, offset, length);
1501 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
1502 if ( err==-1 ) {
drh4f0c5872007-03-26 22:05:01 +00001503 OSTRACE4("AFPLOCK failed to fsctl() '%s' %d %s\n", path, errno,
drhbfe66312006-10-03 17:40:40 +00001504 strerror(errno));
drh3b62b2f2007-06-08 18:27:03 +00001505 return 1; /* error */
drhbfe66312006-10-03 17:40:40 +00001506 } else {
1507 return 0;
1508 }
1509}
1510
1511/*
1512 ** This routine checks if there is a RESERVED lock held on the specified
1513 ** file by this or any other process. If such a lock is held, return
1514 ** non-zero. If the file is unlocked or holds only SHARED locks, then
1515 ** return zero.
1516 */
danielk1977ad94b582007-08-20 06:44:22 +00001517static int afpUnixCheckReservedLock(sqlite3_file *id){
drhbfe66312006-10-03 17:40:40 +00001518 int r = 0;
1519 unixFile *pFile = (unixFile*)id;
1520
1521 assert( pFile );
1522 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
1523
1524 /* Check if a thread in this process holds such a lock */
1525 if( pFile->locktype>SHARED_LOCK ){
1526 r = 1;
1527 }
1528
1529 /* Otherwise see if some other process holds it.
1530 */
1531 if ( !r ) {
drh3b62b2f2007-06-08 18:27:03 +00001532 /* lock the byte */
drhbfe66312006-10-03 17:40:40 +00001533 int failed = _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1,1);
1534 if (failed) {
1535 /* if we failed to get the lock then someone else must have it */
1536 r = 1;
1537 } else {
1538 /* if we succeeded in taking the reserved lock, unlock it to restore
1539 ** the original state */
1540 _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1, 0);
1541 }
1542 }
drh4f0c5872007-03-26 22:05:01 +00001543 OSTRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
drhbfe66312006-10-03 17:40:40 +00001544
1545 return r;
1546}
1547
1548/* AFP-style locking following the behavior of unixLock, see the unixLock
1549** function comments for details of lock management. */
danielk1977ad94b582007-08-20 06:44:22 +00001550static int afpUnixLock(sqlite3_file *id, int locktype)
drhbfe66312006-10-03 17:40:40 +00001551{
1552 int rc = SQLITE_OK;
1553 unixFile *pFile = (unixFile*)id;
1554 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
1555 int gotPendingLock = 0;
1556
1557 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001558 OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00001559 locktypeName(locktype), locktypeName(pFile->locktype), getpid());
1560 /* If there is already a lock of this type or more restrictive on the
danielk1977ad94b582007-08-20 06:44:22 +00001561 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
danielk1977b4b47412007-08-17 15:53:36 +00001562 ** enterMutex() hasn't been called yet.
drhbfe66312006-10-03 17:40:40 +00001563 */
1564 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00001565 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00001566 locktypeName(locktype));
1567 return SQLITE_OK;
1568 }
1569
1570 /* Make sure the locking sequence is correct
1571 */
1572 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
1573 assert( locktype!=PENDING_LOCK );
1574 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
1575
1576 /* This mutex is needed because pFile->pLock is shared across threads
1577 */
danielk1977b4b47412007-08-17 15:53:36 +00001578 enterMutex();
drhbfe66312006-10-03 17:40:40 +00001579
1580 /* Make sure the current thread owns the pFile.
1581 */
1582 rc = transferOwnership(pFile);
1583 if( rc!=SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00001584 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00001585 return rc;
1586 }
1587
1588 /* A PENDING lock is needed before acquiring a SHARED lock and before
1589 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1590 ** be released.
1591 */
1592 if( locktype==SHARED_LOCK
1593 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
1594 ){
1595 int failed = _AFPFSSetLock(context->filePath, pFile->h,
1596 PENDING_BYTE, 1, 1);
1597 if (failed) {
1598 rc = SQLITE_BUSY;
1599 goto afp_end_lock;
1600 }
1601 }
1602
1603 /* If control gets to this point, then actually go ahead and make
1604 ** operating system calls for the specified lock.
1605 */
1606 if( locktype==SHARED_LOCK ){
1607 int lk, failed;
1608 int tries = 0;
1609
1610 /* Now get the read-lock */
1611 /* note that the quality of the randomness doesn't matter that much */
1612 lk = random();
1613 context->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
1614 failed = _AFPFSSetLock(context->filePath, pFile->h,
1615 SHARED_FIRST+context->sharedLockByte, 1, 1);
1616
1617 /* Drop the temporary PENDING lock */
1618 if (_AFPFSSetLock(context->filePath, pFile->h, PENDING_BYTE, 1, 0)) {
1619 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1620 goto afp_end_lock;
1621 }
1622
1623 if( failed ){
1624 rc = SQLITE_BUSY;
1625 } else {
1626 pFile->locktype = SHARED_LOCK;
1627 }
1628 }else{
1629 /* The request was for a RESERVED or EXCLUSIVE lock. It is
1630 ** assumed that there is a SHARED or greater lock on the file
1631 ** already.
1632 */
1633 int failed = 0;
1634 assert( 0!=pFile->locktype );
1635 if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
1636 /* Acquire a RESERVED lock */
1637 failed = _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1,1);
1638 }
1639 if (!failed && locktype == EXCLUSIVE_LOCK) {
1640 /* Acquire an EXCLUSIVE lock */
1641
1642 /* Remove the shared lock before trying the range. we'll need to
1643 ** reestablish the shared lock if we can't get the afpUnixUnlock
1644 */
1645 if (!_AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST +
1646 context->sharedLockByte, 1, 0)) {
1647 /* now attemmpt to get the exclusive lock range */
1648 failed = _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST,
1649 SHARED_SIZE, 1);
1650 if (failed && _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST +
1651 context->sharedLockByte, 1, 1)) {
1652 rc = SQLITE_IOERR_RDLOCK; /* this should never happen */
1653 }
1654 } else {
1655 /* */
1656 rc = SQLITE_IOERR_UNLOCK; /* this should never happen */
1657 }
1658 }
1659 if( failed && rc == SQLITE_OK){
1660 rc = SQLITE_BUSY;
1661 }
1662 }
1663
1664 if( rc==SQLITE_OK ){
1665 pFile->locktype = locktype;
1666 }else if( locktype==EXCLUSIVE_LOCK ){
1667 pFile->locktype = PENDING_LOCK;
1668 }
1669
1670afp_end_lock:
danielk1977b4b47412007-08-17 15:53:36 +00001671 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001672 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
drhbfe66312006-10-03 17:40:40 +00001673 rc==SQLITE_OK ? "ok" : "failed");
1674 return rc;
1675}
1676
1677/*
1678 ** Lower the locking level on file descriptor pFile to locktype. locktype
1679 ** must be either NO_LOCK or SHARED_LOCK.
1680 **
1681 ** If the locking level of the file descriptor is already at or below
1682 ** the requested locking level, this routine is a no-op.
1683 */
danielk1977ad94b582007-08-20 06:44:22 +00001684static int afpUnixUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001685 struct flock lock;
1686 int rc = SQLITE_OK;
1687 unixFile *pFile = (unixFile*)id;
1688 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
1689
1690 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001691 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
drhbfe66312006-10-03 17:40:40 +00001692 pFile->locktype, getpid());
1693
1694 assert( locktype<=SHARED_LOCK );
1695 if( pFile->locktype<=locktype ){
1696 return SQLITE_OK;
1697 }
1698 if( CHECK_THREADID(pFile) ){
1699 return SQLITE_MISUSE;
1700 }
danielk1977b4b47412007-08-17 15:53:36 +00001701 enterMutex();
drhbfe66312006-10-03 17:40:40 +00001702 if( pFile->locktype>SHARED_LOCK ){
1703 if( locktype==SHARED_LOCK ){
1704 int failed = 0;
1705
1706 /* unlock the exclusive range - then re-establish the shared lock */
1707 if (pFile->locktype==EXCLUSIVE_LOCK) {
1708 failed = _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST,
1709 SHARED_SIZE, 0);
1710 if (!failed) {
1711 /* successfully removed the exclusive lock */
1712 if (_AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST+
1713 context->sharedLockByte, 1, 1)) {
1714 /* failed to re-establish our shared lock */
1715 rc = SQLITE_IOERR_RDLOCK; /* This should never happen */
1716 }
1717 } else {
1718 /* This should never happen - failed to unlock the exclusive range */
1719 rc = SQLITE_IOERR_UNLOCK;
1720 }
1721 }
1722 }
1723 if (rc == SQLITE_OK && pFile->locktype>=PENDING_LOCK) {
1724 if (_AFPFSSetLock(context->filePath, pFile->h, PENDING_BYTE, 1, 0)){
1725 /* failed to release the pending lock */
1726 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1727 }
1728 }
1729 if (rc == SQLITE_OK && pFile->locktype>=RESERVED_LOCK) {
1730 if (_AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1, 0)) {
1731 /* failed to release the reserved lock */
1732 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1733 }
1734 }
1735 }
1736 if( locktype==NO_LOCK ){
1737 int failed = _AFPFSSetLock(context->filePath, pFile->h,
1738 SHARED_FIRST + context->sharedLockByte, 1, 0);
1739 if (failed) {
1740 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1741 }
1742 }
1743 if (rc == SQLITE_OK)
1744 pFile->locktype = locktype;
danielk1977b4b47412007-08-17 15:53:36 +00001745 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00001746 return rc;
1747}
1748
1749/*
1750 ** Close a file & cleanup AFP specific locking context
1751 */
danielk1977ad94b582007-08-20 06:44:22 +00001752static int afpUnixClose(sqlite3_file *id) {
1753 unixFile *pFile = (unixFile*)pId;
1754
1755 if( !pFile ) return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00001756 afpUnixUnlock(*pId, NO_LOCK);
1757 /* free the AFP locking structure */
danielk1977ad94b582007-08-20 06:44:22 +00001758 if (pFile->lockingContext != NULL) {
1759 if (((afpLockingContext *)pFile->lockingContext)->filePath != NULL)
1760 sqlite3_free(((afpLockingContext*)pFile->lockingContext)->filePath);
1761 sqlite3_free(pFile->lockingContext);
drhbfe66312006-10-03 17:40:40 +00001762 }
danielk1977ad94b582007-08-20 06:44:22 +00001763
1764 if( pFile->dirfd>=0 ) close(pFile->dirfd);
1765 pFile->dirfd = -1;
1766 close(pFile->h);
1767 pFile->isOpen = 0;
1768 OSTRACE2("CLOSE %-3d\n", pFile->h);
drhbfe66312006-10-03 17:40:40 +00001769 OpenCounter(-1);
drhbfe66312006-10-03 17:40:40 +00001770 return SQLITE_OK;
1771}
1772
1773
1774#pragma mark flock() style locking
1775
1776/*
1777 ** The flockLockingContext is not used
1778 */
1779typedef void flockLockingContext;
1780
danielk1977ad94b582007-08-20 06:44:22 +00001781static int flockUnixCheckReservedLock(sqlite3_file *id) {
drhbfe66312006-10-03 17:40:40 +00001782 unixFile *pFile = (unixFile*)id;
1783
1784 if (pFile->locktype == RESERVED_LOCK) {
drh3b62b2f2007-06-08 18:27:03 +00001785 return 1; /* already have a reserved lock */
drhbfe66312006-10-03 17:40:40 +00001786 } else {
drh3b62b2f2007-06-08 18:27:03 +00001787 /* attempt to get the lock */
drhbfe66312006-10-03 17:40:40 +00001788 int rc = flock(pFile->h, LOCK_EX | LOCK_NB);
1789 if (!rc) {
drh3b62b2f2007-06-08 18:27:03 +00001790 /* got the lock, unlock it */
drhbfe66312006-10-03 17:40:40 +00001791 flock(pFile->h, LOCK_UN);
drh3b62b2f2007-06-08 18:27:03 +00001792 return 0; /* no one has it reserved */
drhbfe66312006-10-03 17:40:40 +00001793 }
drh3b62b2f2007-06-08 18:27:03 +00001794 return 1; /* someone else might have it reserved */
drhbfe66312006-10-03 17:40:40 +00001795 }
1796}
1797
danielk1977ad94b582007-08-20 06:44:22 +00001798static int flockUnixLock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001799 unixFile *pFile = (unixFile*)id;
1800
drh3b62b2f2007-06-08 18:27:03 +00001801 /* if we already have a lock, it is exclusive.
1802 ** Just adjust level and punt on outta here. */
drhbfe66312006-10-03 17:40:40 +00001803 if (pFile->locktype > NO_LOCK) {
1804 pFile->locktype = locktype;
1805 return SQLITE_OK;
1806 }
1807
drh3b62b2f2007-06-08 18:27:03 +00001808 /* grab an exclusive lock */
drhbfe66312006-10-03 17:40:40 +00001809 int rc = flock(pFile->h, LOCK_EX | LOCK_NB);
1810 if (rc) {
drh3b62b2f2007-06-08 18:27:03 +00001811 /* didn't get, must be busy */
drhbfe66312006-10-03 17:40:40 +00001812 return SQLITE_BUSY;
1813 } else {
drh3b62b2f2007-06-08 18:27:03 +00001814 /* got it, set the type and return ok */
drhbfe66312006-10-03 17:40:40 +00001815 pFile->locktype = locktype;
1816 return SQLITE_OK;
1817 }
1818}
1819
danielk1977ad94b582007-08-20 06:44:22 +00001820static int flockUnixUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001821 unixFile *pFile = (unixFile*)id;
1822
1823 assert( locktype<=SHARED_LOCK );
1824
drh3b62b2f2007-06-08 18:27:03 +00001825 /* no-op if possible */
drhbfe66312006-10-03 17:40:40 +00001826 if( pFile->locktype==locktype ){
1827 return SQLITE_OK;
1828 }
1829
drh3b62b2f2007-06-08 18:27:03 +00001830 /* shared can just be set because we always have an exclusive */
drhbfe66312006-10-03 17:40:40 +00001831 if (locktype==SHARED_LOCK) {
1832 pFile->locktype = locktype;
1833 return SQLITE_OK;
1834 }
1835
drh3b62b2f2007-06-08 18:27:03 +00001836 /* no, really, unlock. */
drhbfe66312006-10-03 17:40:40 +00001837 int rc = flock(pFile->h, LOCK_UN);
1838 if (rc)
1839 return SQLITE_IOERR_UNLOCK;
1840 else {
1841 pFile->locktype = NO_LOCK;
1842 return SQLITE_OK;
1843 }
1844}
1845
1846/*
1847 ** Close a file.
1848 */
danielk1977ad94b582007-08-20 06:44:22 +00001849static int flockUnixClose(sqlite3_file *pId) {
1850 unixFile *pFile = (unixFile*)*pId;
drhbfe66312006-10-03 17:40:40 +00001851
danielk1977ad94b582007-08-20 06:44:22 +00001852 if( !pFile ) return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00001853 flockUnixUnlock(*pId, NO_LOCK);
1854
danielk1977ad94b582007-08-20 06:44:22 +00001855 if( pFile->dirfd>=0 ) close(pFile->dirfd);
1856 pFile->dirfd = -1;
danielk1977b4b47412007-08-17 15:53:36 +00001857 enterMutex();
drhbfe66312006-10-03 17:40:40 +00001858
danielk1977ad94b582007-08-20 06:44:22 +00001859 close(pFile->h);
danielk1977b4b47412007-08-17 15:53:36 +00001860 leaveMutex();
danielk1977ad94b582007-08-20 06:44:22 +00001861 pFile->isOpen = 0;
1862 OSTRACE2("CLOSE %-3d\n", pFile->h);
drhbfe66312006-10-03 17:40:40 +00001863 OpenCounter(-1);
drhbfe66312006-10-03 17:40:40 +00001864 return SQLITE_OK;
1865}
1866
1867#pragma mark Old-School .lock file based locking
1868
1869/*
1870 ** The dotlockLockingContext structure contains all dotlock (.lock) lock
1871 ** specific state
1872 */
1873typedef struct dotlockLockingContext dotlockLockingContext;
1874struct dotlockLockingContext {
1875 char *lockPath;
1876};
1877
1878
danielk1977ad94b582007-08-20 06:44:22 +00001879static int dotlockUnixCheckReservedLock(sqlite3_file *id) {
drhbfe66312006-10-03 17:40:40 +00001880 unixFile *pFile = (unixFile*)id;
1881 dotlockLockingContext *context =
1882 (dotlockLockingContext *) pFile->lockingContext;
1883
1884 if (pFile->locktype == RESERVED_LOCK) {
drh3b62b2f2007-06-08 18:27:03 +00001885 return 1; /* already have a reserved lock */
drhbfe66312006-10-03 17:40:40 +00001886 } else {
1887 struct stat statBuf;
1888 if (lstat(context->lockPath,&statBuf) == 0)
drh3b62b2f2007-06-08 18:27:03 +00001889 /* file exists, someone else has the lock */
drhbfe66312006-10-03 17:40:40 +00001890 return 1;
1891 else
drh3b62b2f2007-06-08 18:27:03 +00001892 /* file does not exist, we could have it if we want it */
drhbfe66312006-10-03 17:40:40 +00001893 return 0;
1894 }
1895}
1896
danielk1977ad94b582007-08-20 06:44:22 +00001897static int dotlockUnixLock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001898 unixFile *pFile = (unixFile*)id;
1899 dotlockLockingContext *context =
1900 (dotlockLockingContext *) pFile->lockingContext;
1901
drh3b62b2f2007-06-08 18:27:03 +00001902 /* if we already have a lock, it is exclusive.
1903 ** Just adjust level and punt on outta here. */
drhbfe66312006-10-03 17:40:40 +00001904 if (pFile->locktype > NO_LOCK) {
1905 pFile->locktype = locktype;
1906
1907 /* Always update the timestamp on the old file */
1908 utimes(context->lockPath,NULL);
1909 return SQLITE_OK;
1910 }
1911
drh3b62b2f2007-06-08 18:27:03 +00001912 /* check to see if lock file already exists */
drhbfe66312006-10-03 17:40:40 +00001913 struct stat statBuf;
1914 if (lstat(context->lockPath,&statBuf) == 0){
drh3b62b2f2007-06-08 18:27:03 +00001915 return SQLITE_BUSY; /* it does, busy */
drhbfe66312006-10-03 17:40:40 +00001916 }
1917
drh3b62b2f2007-06-08 18:27:03 +00001918 /* grab an exclusive lock */
drhbfe66312006-10-03 17:40:40 +00001919 int fd = open(context->lockPath,O_RDONLY|O_CREAT|O_EXCL,0600);
1920 if (fd < 0) {
drh3b62b2f2007-06-08 18:27:03 +00001921 /* failed to open/create the file, someone else may have stolen the lock */
drhbfe66312006-10-03 17:40:40 +00001922 return SQLITE_BUSY;
1923 }
1924 close(fd);
1925
drh3b62b2f2007-06-08 18:27:03 +00001926 /* got it, set the type and return ok */
drhbfe66312006-10-03 17:40:40 +00001927 pFile->locktype = locktype;
1928 return SQLITE_OK;
1929}
1930
danielk1977ad94b582007-08-20 06:44:22 +00001931static int dotlockUnixUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001932 unixFile *pFile = (unixFile*)id;
1933 dotlockLockingContext *context =
1934 (dotlockLockingContext *) pFile->lockingContext;
1935
1936 assert( locktype<=SHARED_LOCK );
1937
drh3b62b2f2007-06-08 18:27:03 +00001938 /* no-op if possible */
drhbfe66312006-10-03 17:40:40 +00001939 if( pFile->locktype==locktype ){
1940 return SQLITE_OK;
1941 }
1942
drh3b62b2f2007-06-08 18:27:03 +00001943 /* shared can just be set because we always have an exclusive */
drhbfe66312006-10-03 17:40:40 +00001944 if (locktype==SHARED_LOCK) {
1945 pFile->locktype = locktype;
1946 return SQLITE_OK;
1947 }
1948
drh3b62b2f2007-06-08 18:27:03 +00001949 /* no, really, unlock. */
drhbfe66312006-10-03 17:40:40 +00001950 unlink(context->lockPath);
1951 pFile->locktype = NO_LOCK;
1952 return SQLITE_OK;
1953}
1954
1955/*
1956 ** Close a file.
1957 */
danielk1977ad94b582007-08-20 06:44:22 +00001958static int dotlockUnixClose(sqlite3_file *id) {
1959 unixFile *pFile = (unixFile*)id;
drhbfe66312006-10-03 17:40:40 +00001960
danielk1977ad94b582007-08-20 06:44:22 +00001961 if( !pFile ) return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00001962 dotlockUnixUnlock(*pId, NO_LOCK);
1963 /* free the dotlock locking structure */
danielk1977ad94b582007-08-20 06:44:22 +00001964 if (pFile->lockingContext != NULL) {
1965 if (((dotlockLockingContext *)pFile->lockingContext)->lockPath != NULL)
drh17435752007-08-16 04:30:38 +00001966 sqlite3_free( ( (dotlockLockingContext *)
danielk1977ad94b582007-08-20 06:44:22 +00001967 pFile->lockingContext)->lockPath);
1968 sqlite3_free(pFile->lockingContext);
drhbfe66312006-10-03 17:40:40 +00001969 }
1970
danielk1977ad94b582007-08-20 06:44:22 +00001971 if( pFile->dirfd>=0 ) close(pFile->dirfd);
1972 pFile->dirfd = -1;
danielk1977b4b47412007-08-17 15:53:36 +00001973 enterMutex();
drhbfe66312006-10-03 17:40:40 +00001974
danielk1977ad94b582007-08-20 06:44:22 +00001975 close(pFile->h);
drhbfe66312006-10-03 17:40:40 +00001976
danielk1977b4b47412007-08-17 15:53:36 +00001977 leaveMutex();
danielk1977ad94b582007-08-20 06:44:22 +00001978 pFile->isOpen = 0;
1979 OSTRACE2("CLOSE %-3d\n", pFile->h);
drhbfe66312006-10-03 17:40:40 +00001980 OpenCounter(-1);
drhbfe66312006-10-03 17:40:40 +00001981 return SQLITE_OK;
1982}
1983
1984
1985#pragma mark No locking
1986
1987/*
1988 ** The nolockLockingContext is void
1989 */
1990typedef void nolockLockingContext;
1991
danielk1977ad94b582007-08-20 06:44:22 +00001992static int nolockUnixCheckReservedLock(sqlite3_file *id) {
drhbfe66312006-10-03 17:40:40 +00001993 return 0;
1994}
1995
danielk1977ad94b582007-08-20 06:44:22 +00001996static int nolockUnixLock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001997 return SQLITE_OK;
1998}
1999
danielk1977ad94b582007-08-20 06:44:22 +00002000static int nolockUnixUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002001 return SQLITE_OK;
2002}
2003
2004/*
2005 ** Close a file.
2006 */
danielk1977ad94b582007-08-20 06:44:22 +00002007static int nolockUnixClose(sqlite3_file *id) {
2008 unixFile *pFile = (unixFile*)id;
drhbfe66312006-10-03 17:40:40 +00002009
danielk1977ad94b582007-08-20 06:44:22 +00002010 if( !pFile ) return SQLITE_OK;
2011 if( pFile->dirfd>=0 ) close(pFile->dirfd);
2012 pFile->dirfd = -1;
danielk1977b4b47412007-08-17 15:53:36 +00002013 enterMutex();
drhbfe66312006-10-03 17:40:40 +00002014
danielk1977ad94b582007-08-20 06:44:22 +00002015 close(pFile->h);
drhbfe66312006-10-03 17:40:40 +00002016
danielk1977b4b47412007-08-17 15:53:36 +00002017 leaveMutex();
danielk1977ad94b582007-08-20 06:44:22 +00002018 pFile->isOpen = 0;
2019 OSTRACE2("CLOSE %-3d\n", pFile->h);
drhbfe66312006-10-03 17:40:40 +00002020 OpenCounter(-1);
drhbfe66312006-10-03 17:40:40 +00002021 return SQLITE_OK;
2022}
2023
2024#endif /* SQLITE_ENABLE_LOCKING_STYLE */
2025
danielk1977ad94b582007-08-20 06:44:22 +00002026
danielk1977e3026632004-06-22 11:29:02 +00002027/*
danielk1977ad94b582007-08-20 06:44:22 +00002028** TODO: xBreakLock() for this vfs.
drh18839212005-11-26 03:43:23 +00002029*/
danielk1977ad94b582007-08-20 06:44:22 +00002030static int unixBreakLock(sqlite3_file *id){
2031 assert(!"TODO: unixBreakLock()");
2032 return 0;
drh9cbe6352005-11-29 03:13:21 +00002033}
2034
2035/*
danielk1977ad94b582007-08-20 06:44:22 +00002036** Return an integer that indices the type of lock currently held
2037** by this handle. (Used for testing and analysis only.)
drh9cbe6352005-11-29 03:13:21 +00002038*/
danielk1977ad94b582007-08-20 06:44:22 +00002039static int unixLockState(sqlite3_file *id){
2040 return ((unixFile*)id)->locktype;
drh9cbe6352005-11-29 03:13:21 +00002041}
2042
drh9c06c952005-11-26 00:25:00 +00002043/*
danielk1977a3d4c882007-03-23 10:08:38 +00002044** Return the sector size in bytes of the underlying block device for
2045** the specified file. This is almost always 512 bytes, but may be
2046** larger for some devices.
2047**
2048** SQLite code assumes this function cannot fail. It also assumes that
2049** if two files are created in the same file-system directory (i.e.
2050** a database and it's journal file) that the sector size will be the
2051** same for both.
2052*/
danielk197762079062007-08-15 17:08:46 +00002053static int unixSectorSize(sqlite3_file *id){
drh3ceeb752007-03-29 18:19:52 +00002054 return SQLITE_DEFAULT_SECTOR_SIZE;
danielk1977a3d4c882007-03-23 10:08:38 +00002055}
2056
danielk197790949c22007-08-17 16:50:38 +00002057/*
2058** Return the device characteristics for the file. This is always 0.
2059*/
danielk197762079062007-08-15 17:08:46 +00002060static int unixDeviceCharacteristics(sqlite3_file *id){
2061 return 0;
2062}
2063
danielk1977a3d4c882007-03-23 10:08:38 +00002064/*
danielk1977ad94b582007-08-20 06:44:22 +00002065** This vector defines all the methods that can operate on an sqlite3_file
drh054889e2005-11-30 03:20:31 +00002066** for unix.
drh9c06c952005-11-26 00:25:00 +00002067*/
danielk197762079062007-08-15 17:08:46 +00002068static const sqlite3_io_methods sqlite3UnixIoMethod = {
2069 1, /* iVersion */
drh9c06c952005-11-26 00:25:00 +00002070 unixClose,
2071 unixRead,
2072 unixWrite,
drh9c06c952005-11-26 00:25:00 +00002073 unixTruncate,
drh054889e2005-11-30 03:20:31 +00002074 unixSync,
drh054889e2005-11-30 03:20:31 +00002075 unixFileSize,
2076 unixLock,
2077 unixUnlock,
drh054889e2005-11-30 03:20:31 +00002078 unixCheckReservedLock,
danielk197762079062007-08-15 17:08:46 +00002079 unixBreakLock,
danielk197790949c22007-08-17 16:50:38 +00002080 unixLockState,
danielk1977a3d4c882007-03-23 10:08:38 +00002081 unixSectorSize,
danielk197762079062007-08-15 17:08:46 +00002082 unixDeviceCharacteristics
drh9c06c952005-11-26 00:25:00 +00002083};
2084
drhbfe66312006-10-03 17:40:40 +00002085#ifdef SQLITE_ENABLE_LOCKING_STYLE
drh054889e2005-11-30 03:20:31 +00002086/*
danielk1977ad94b582007-08-20 06:44:22 +00002087** This vector defines all the methods that can operate on an sqlite3_file
2088** for unix with AFP style file locking.
2089*/
2090static const sqlite3_io_methods sqlite3AFPLockingUnixIoMethod = {
2091 1, /* iVersion */
2092 unixClose,
drhbfe66312006-10-03 17:40:40 +00002093 unixRead,
2094 unixWrite,
drhbfe66312006-10-03 17:40:40 +00002095 unixTruncate,
2096 unixSync,
danielk1977ad94b582007-08-20 06:44:22 +00002097 unixFileSize,
2098 afpUnixLock,
2099 afpUnixUnlock,
2100 afpUnixCheckReservedLock,
2101 unixBreakLock,
2102 unixLockState,
2103 unixSectorSize,
2104 unixDeviceCharacteristics
2105};
2106
2107/*
2108** This vector defines all the methods that can operate on an sqlite3_file
2109** for unix with flock() style file locking.
2110*/
2111static const sqlite3_io_methods sqlite3FlockLockingUnixIoMethod = {
2112 1, /* iVersion */
2113 flockUnixClose,
2114 unixRead,
2115 unixWrite,
2116 unixTruncate,
2117 unixSync,
2118 unixFileSize,
2119 flockUnixLock,
2120 flockUnixUnlock,
2121 flockUnixCheckReservedLock,
2122 unixBreakLock,
2123 unixLockState,
2124 unixSectorSize,
2125 unixDeviceCharacteristics
2126};
2127
2128/*
2129** This vector defines all the methods that can operate on an sqlite3_file
2130** for unix with dotlock style file locking.
2131*/
2132static const sqlite3_io_methods sqlite3DotlockLockingUnixIoMethod = {
2133 1, /* iVersion */
2134 dotlockUnixClose,
2135 unixRead,
2136 unixWrite,
2137 unixTruncate,
2138 unixSync,
2139 unixFileSize,
2140 dotlockUnixLock,
2141 dotlockUnixUnlock,
2142 dotlockUnixCheckReservedLock,
2143 unixBreakLock,
2144 unixLockState,
2145 unixSectorSize,
2146 unixDeviceCharacteristics
2147};
2148
2149/*
2150** This vector defines all the methods that can operate on an sqlite3_file
2151** for unix with dotlock style file locking.
2152*/
2153static const sqlite3_io_methods sqlite3NolockLockingUnixIoMethod = {
2154 1, /* iVersion */
2155 nolockUnixClose,
2156 unixRead,
2157 unixWrite,
2158 unixTruncate,
2159 unixSync,
drhbfe66312006-10-03 17:40:40 +00002160 unixFileSize,
2161 nolockUnixLock,
2162 nolockUnixUnlock,
drhbfe66312006-10-03 17:40:40 +00002163 nolockUnixCheckReservedLock,
danielk1977ad94b582007-08-20 06:44:22 +00002164 unixBreakLock,
2165 unixLockState,
danielk1977a3d4c882007-03-23 10:08:38 +00002166 unixSectorSize,
danielk1977ad94b582007-08-20 06:44:22 +00002167 unixDeviceCharacteristics
drhbfe66312006-10-03 17:40:40 +00002168};
2169
2170#endif /* SQLITE_ENABLE_LOCKING_STYLE */
2171
2172/*
2173** Allocate memory for a new unixFile and initialize that unixFile.
2174** Write a pointer to the new unixFile into *pId.
2175** If we run out of memory, close the file and return an error.
drh054889e2005-11-30 03:20:31 +00002176*/
drhbfe66312006-10-03 17:40:40 +00002177#ifdef SQLITE_ENABLE_LOCKING_STYLE
2178/*
danielk1977ad94b582007-08-20 06:44:22 +00002179** When locking extensions are enabled, the filepath and locking style
2180** are needed to determine the unixFile pMethod to use for locking operations.
2181** The locking-style specific lockingContext data structure is created
2182** and assigned here also.
2183*/
2184static int fillInUnixFile(
drhbfe66312006-10-03 17:40:40 +00002185 int h, /* Open file descriptor of file being opened */
danielk1977ad94b582007-08-20 06:44:22 +00002186 int dirfd, /* Directory file descriptor */
2187 sqlite3_file *pId, /* Write completed initialization here */
drhbfe66312006-10-03 17:40:40 +00002188 const char *zFilename, /* Name of the file being opened */
drhbfe66312006-10-03 17:40:40 +00002189){
aswift108bc322006-10-11 17:19:46 +00002190 sqlite3LockingStyle lockingStyle;
danielk1977ad94b582007-08-20 06:44:22 +00002191 unixFile *pNew = (unixFile *)pId;
drhbfe66312006-10-03 17:40:40 +00002192 int rc;
2193
danielk1977ad94b582007-08-20 06:44:22 +00002194 memset(pNew, 0, sizeof(unixFile));
aswift448aa6f2006-11-11 01:31:58 +00002195 lockingStyle = sqlite3DetectLockingStyle(zFilename, h);
drhbfe66312006-10-03 17:40:40 +00002196 if ( lockingStyle == posixLockingStyle ) {
danielk1977b4b47412007-08-17 15:53:36 +00002197 enterMutex();
danielk1977ad94b582007-08-20 06:44:22 +00002198 rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
danielk1977b4b47412007-08-17 15:53:36 +00002199 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00002200 if( rc ){
2201 close(h);
2202 unlink(zFilename);
2203 return SQLITE_NOMEM;
2204 }
2205 } else {
drh3b62b2f2007-06-08 18:27:03 +00002206 /* pLock and pOpen are only used for posix advisory locking */
danielk1977ad94b582007-08-20 06:44:22 +00002207 pNew->pLock = NULL;
2208 pNew->pOpen = NULL;
drhbfe66312006-10-03 17:40:40 +00002209 }
danielk1977ad94b582007-08-20 06:44:22 +00002210 pNew->dirfd = -1;
2211 pNew->h = h;
2212 SET_THREADID(pNew);
drh17435752007-08-16 04:30:38 +00002213 pNew = sqlite3_malloc( sizeof(unixFile) );
drh054889e2005-11-30 03:20:31 +00002214 if( pNew==0 ){
drhbfe66312006-10-03 17:40:40 +00002215 close(h);
danielk1977b4b47412007-08-17 15:53:36 +00002216 enterMutex();
danielk1977ad94b582007-08-20 06:44:22 +00002217 releaseLockInfo(pNew->pLock);
2218 releaseOpenCnt(pNew->pOpen);
danielk1977b4b47412007-08-17 15:53:36 +00002219 leaveMutex();
drh054889e2005-11-30 03:20:31 +00002220 return SQLITE_NOMEM;
2221 }else{
aswift108bc322006-10-11 17:19:46 +00002222 switch(lockingStyle) {
drh5bb3eb92007-05-04 13:15:55 +00002223 case afpLockingStyle: {
drhbfe66312006-10-03 17:40:40 +00002224 /* afp locking uses the file path so it needs to be included in
2225 ** the afpLockingContext */
drh5bb3eb92007-05-04 13:15:55 +00002226 int nFilename;
drhbfe66312006-10-03 17:40:40 +00002227 pNew->pMethod = &sqlite3AFPLockingUnixIoMethod;
2228 pNew->lockingContext =
drh17435752007-08-16 04:30:38 +00002229 sqlite3_malloc(sizeof(afpLockingContext));
drh5bb3eb92007-05-04 13:15:55 +00002230 nFilename = strlen(zFilename)+1;
drhbfe66312006-10-03 17:40:40 +00002231 ((afpLockingContext *)pNew->lockingContext)->filePath =
drh17435752007-08-16 04:30:38 +00002232 sqlite3_malloc(nFilename);
drh5bb3eb92007-05-04 13:15:55 +00002233 memcpy(((afpLockingContext *)pNew->lockingContext)->filePath,
2234 zFilename, nFilename);
drhbfe66312006-10-03 17:40:40 +00002235 srandomdev();
2236 break;
drh5bb3eb92007-05-04 13:15:55 +00002237 }
drhbfe66312006-10-03 17:40:40 +00002238 case flockLockingStyle:
2239 /* flock locking doesn't need additional lockingContext information */
2240 pNew->pMethod = &sqlite3FlockLockingUnixIoMethod;
2241 break;
drh5bb3eb92007-05-04 13:15:55 +00002242 case dotlockLockingStyle: {
drhbfe66312006-10-03 17:40:40 +00002243 /* dotlock locking uses the file path so it needs to be included in
2244 ** the dotlockLockingContext */
drh5bb3eb92007-05-04 13:15:55 +00002245 int nFilename;
drhbfe66312006-10-03 17:40:40 +00002246 pNew->pMethod = &sqlite3DotlockLockingUnixIoMethod;
drh17435752007-08-16 04:30:38 +00002247 pNew->lockingContext = sqlite3_malloc(
drhbfe66312006-10-03 17:40:40 +00002248 sizeof(dotlockLockingContext));
drh5bb3eb92007-05-04 13:15:55 +00002249 nFilename = strlen(zFilename) + 6;
drhbfe66312006-10-03 17:40:40 +00002250 ((dotlockLockingContext *)pNew->lockingContext)->lockPath =
drh17435752007-08-16 04:30:38 +00002251 sqlite3_malloc( nFilename );
drh5bb3eb92007-05-04 13:15:55 +00002252 sqlite3_snprintf(nFilename,
2253 ((dotlockLockingContext *)pNew->lockingContext)->lockPath,
drhbfe66312006-10-03 17:40:40 +00002254 "%s.lock", zFilename);
2255 break;
drh5bb3eb92007-05-04 13:15:55 +00002256 }
drhbfe66312006-10-03 17:40:40 +00002257 case posixLockingStyle:
2258 /* posix locking doesn't need additional lockingContext information */
2259 pNew->pMethod = &sqlite3UnixIoMethod;
2260 break;
2261 case noLockingStyle:
2262 case unsupportedLockingStyle:
2263 default:
2264 pNew->pMethod = &sqlite3NolockLockingUnixIoMethod;
2265 }
drhbfe66312006-10-03 17:40:40 +00002266 OpenCounter(+1);
2267 return SQLITE_OK;
2268 }
2269}
2270#else /* SQLITE_ENABLE_LOCKING_STYLE */
danielk1977b4b47412007-08-17 15:53:36 +00002271static int fillInUnixFile(
drhbfe66312006-10-03 17:40:40 +00002272 int h, /* Open file descriptor on file being opened */
danielk1977fee2d252007-08-18 10:59:19 +00002273 int dirfd,
danielk1977b4b47412007-08-17 15:53:36 +00002274 sqlite3_file *pId, /* Write to the unixFile structure here */
2275 const char *zFilename /* Name of the file being opened */
drhbfe66312006-10-03 17:40:40 +00002276){
danielk1977b4b47412007-08-17 15:53:36 +00002277 unixFile *pNew = (unixFile *)pId;
drhbfe66312006-10-03 17:40:40 +00002278 int rc;
2279
drhe78669b2007-06-29 12:04:26 +00002280#ifdef FD_CLOEXEC
2281 fcntl(h, F_SETFD, fcntl(h, F_GETFD, 0) | FD_CLOEXEC);
2282#endif
danielk1977b4b47412007-08-17 15:53:36 +00002283
2284 enterMutex();
2285 rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
2286 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00002287 if( rc ){
2288 close(h);
2289 return SQLITE_NOMEM;
2290 }
danielk1977b4b47412007-08-17 15:53:36 +00002291
drh4f0c5872007-03-26 22:05:01 +00002292 OSTRACE3("OPEN %-3d %s\n", h, zFilename);
danielk1977b4b47412007-08-17 15:53:36 +00002293 pNew->dirfd = -1;
2294 pNew->h = h;
danielk1977fee2d252007-08-18 10:59:19 +00002295 pNew->dirfd = dirfd;
danielk1977b4b47412007-08-17 15:53:36 +00002296 SET_THREADID(pNew);
2297
2298 pNew->pMethod = &sqlite3UnixIoMethod;
2299 OpenCounter(+1);
2300 return SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00002301}
drhbfe66312006-10-03 17:40:40 +00002302#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drh9c06c952005-11-26 00:25:00 +00002303
drh0ccebe72005-06-07 22:22:50 +00002304#endif /* SQLITE_OMIT_DISKIO */
2305/***************************************************************************
2306** Everything above deals with file I/O. Everything that follows deals
2307** with other miscellanous aspects of the operating system interface
2308****************************************************************************/
2309
danielk1977ad94b582007-08-20 06:44:22 +00002310/*
2311** Open a file descriptor to the directory containing file zFilename.
2312** If successful, *pFd is set to the opened file descriptor and
2313** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
2314** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
2315** value.
2316**
2317** If SQLITE_OK is returned, the caller is responsible for closing
2318** the file descriptor *pFd using close().
2319*/
danielk1977fee2d252007-08-18 10:59:19 +00002320static int openDirectory(const char *zFilename, int *pFd){
2321 char *zDirname;
2322 int ii;
2323 int fd;
2324
2325 zDirname = (char *)sqlite3_malloc(MAX_PATHNAME);
2326 if( !zDirname ){
2327 return SQLITE_NOMEM;
2328 }
2329 strncpy(zDirname, zFilename, MAX_PATHNAME);
2330 zDirname[MAX_PATHNAME-1] = '\0';
2331 for(ii=strlen(zDirname); ii>=0 && zDirname[ii]!='/'; ii--);
2332 if( ii>0 ){
2333 zDirname[ii] = '\0';
2334 fd = open(zDirname, O_RDONLY|O_BINARY, 0);
2335 if( fd>0 ){
2336#ifdef FD_CLOEXEC
2337 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
2338#endif
2339 OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname);
2340 }
2341 }
2342 sqlite3_free(zDirname);
2343 *pFd = fd;
2344 return (fd>0?SQLITE_OK:SQLITE_CANTOPEN);
2345}
2346
danielk1977b4b47412007-08-17 15:53:36 +00002347/*
danielk1977ad94b582007-08-20 06:44:22 +00002348** Open the file zPath.
2349**
danielk1977b4b47412007-08-17 15:53:36 +00002350** Previously, the SQLite OS layer used three functions in place of this
2351** one:
2352**
2353** sqlite3OsOpenReadWrite();
2354** sqlite3OsOpenReadOnly();
2355** sqlite3OsOpenExclusive();
2356**
2357** These calls correspond to the following combinations of flags:
2358**
2359** ReadWrite() -> (READWRITE | CREATE)
2360** ReadOnly() -> (READONLY)
2361** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
2362**
2363** The old OpenExclusive() accepted a boolean argument - "delFlag". If
2364** true, the file was configured to be automatically deleted when the
2365** file handle closed. To achieve the same effect using this new
2366** interface, add the DELETEONCLOSE flag to those specified above for
2367** OpenExclusive().
2368*/
2369static int unixOpen(
2370 void *pNotUsed,
2371 const char *zPath,
2372 sqlite3_file *pFile,
2373 int flags,
2374 int *pOutFlags
2375){
danielk1977fee2d252007-08-18 10:59:19 +00002376 int fd = 0; /* File descriptor returned by open() */
2377 int dirfd = -1; /* Directory file descriptor */
2378 int oflags = 0; /* Flags to pass to open() */
2379 int eType = flags&0xFFFFFF00; /* Type of file to open */
danielk1977b4b47412007-08-17 15:53:36 +00002380
2381 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
2382 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
2383 int isCreate = (flags & SQLITE_OPEN_CREATE);
2384 int isReadonly = (flags & SQLITE_OPEN_READONLY);
2385 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
2386
danielk1977fee2d252007-08-18 10:59:19 +00002387 /* If creating a master or main-file journal, this function will open
2388 ** a file-descriptor on the directory too. The first time unixSync()
2389 ** is called the directory file descriptor will be fsync()ed and close()d.
2390 */
2391 int isOpenDirectory = (isCreate &&
2392 (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL)
2393 );
2394
2395 /* Check the following statements are true:
2396 **
2397 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
2398 ** (b) if CREATE is set, then READWRITE must also be set, and
2399 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
2400 */
danielk1977b4b47412007-08-17 15:53:36 +00002401 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
danielk1977b4b47412007-08-17 15:53:36 +00002402 assert(isCreate==0 || isReadWrite);
danielk1977b4b47412007-08-17 15:53:36 +00002403 assert(isExclusive==0 || isCreate);
2404
danielk1977fee2d252007-08-18 10:59:19 +00002405 /* Assert that the upper layer has set one of the "file-type" flags. */
2406 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
2407 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
2408 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
2409 );
2410
danielk1977b4b47412007-08-17 15:53:36 +00002411 if( isReadonly ) oflags |= O_RDONLY;
2412 if( isReadWrite ) oflags |= O_RDWR;
2413 if( isCreate ) oflags |= O_CREAT;
2414 if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW);
2415 oflags |= (O_LARGEFILE|O_BINARY);
2416
2417 memset(pFile, 0, sizeof(unixFile));
2418 fd = open(zPath, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
2419 if( fd<0 && isReadWrite && !isExclusive ){
2420 /* Failed to open the file for read/write access. Try read-only. */
2421 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
2422 flags |= SQLITE_OPEN_READONLY;
2423 return unixOpen(pNotUsed, zPath, pFile, flags, pOutFlags);
2424 }
2425 if( fd<0 ){
2426 return SQLITE_CANTOPEN;
2427 }
2428 if( isDelete ){
2429 unlink(zPath);
2430 }
2431 if( pOutFlags ){
2432 *pOutFlags = flags;
2433 }
2434
2435 assert(fd!=0);
danielk1977fee2d252007-08-18 10:59:19 +00002436 if( isOpenDirectory ){
2437 int rc = openDirectory(zPath, &dirfd);
2438 if( rc!=SQLITE_OK ){
2439 close(fd);
2440 return rc;
2441 }
2442 }
2443 return fillInUnixFile(fd, dirfd, pFile, zPath);
danielk1977b4b47412007-08-17 15:53:36 +00002444}
2445
2446/*
danielk1977fee2d252007-08-18 10:59:19 +00002447** Delete the file at zPath. If the dirSync argument is true, fsync()
2448** the directory after deleting the file.
danielk1977b4b47412007-08-17 15:53:36 +00002449*/
danielk1977fee2d252007-08-18 10:59:19 +00002450static int unixDelete(void *pNotUsed, const char *zPath, int dirSync){
2451 int rc = SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00002452 SimulateIOError(return SQLITE_IOERR_DELETE);
2453 unlink(zPath);
danielk1977fee2d252007-08-18 10:59:19 +00002454 if( dirSync ){
2455 int fd;
2456 rc = openDirectory(zPath, &fd);
2457 if( rc==SQLITE_OK ){
2458 if( fsync(fd) ){
2459 rc = SQLITE_IOERR_DIR_FSYNC;
2460 }
2461 close(fd);
2462 }
2463 }
2464 return rc;
danielk1977b4b47412007-08-17 15:53:36 +00002465}
2466
danielk197790949c22007-08-17 16:50:38 +00002467/*
2468** Test the existance of or access permissions of file zPath. The
2469** test performed depends on the value of flags:
2470**
2471** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
2472** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
2473** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
2474**
2475** Otherwise return 0.
2476*/
danielk1977b4b47412007-08-17 15:53:36 +00002477static int unixAccess(void *pNotUsed, const char *zPath, int flags){
2478 int amode;
2479 switch( flags ){
2480 case SQLITE_ACCESS_EXISTS:
2481 amode = F_OK;
2482 break;
2483 case SQLITE_ACCESS_READWRITE:
2484 amode = W_OK|R_OK;
2485 break;
2486 case SQLITE_ACCESS_READONLY:
2487 amode = R_OK;
2488 break;
2489
2490 default:
2491 assert(!"Invalid flags argument");
2492 }
2493 return (access(zPath, amode)==0);
2494}
2495
2496/*
2497** Create a temporary file name in zBuf. zBuf must be big enough to
2498** hold at least MAX_PATHNAME characters.
2499*/
2500static int unixGetTempName(void *pNotUsed, char *zBuf){
2501 static const char *azDirs[] = {
2502 0,
2503 "/var/tmp",
2504 "/usr/tmp",
2505 "/tmp",
2506 ".",
2507 };
2508 static const unsigned char zChars[] =
2509 "abcdefghijklmnopqrstuvwxyz"
2510 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2511 "0123456789";
2512 int i, j;
2513 struct stat buf;
2514 const char *zDir = ".";
2515 azDirs[0] = sqlite3_temp_directory;
2516 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
2517 if( azDirs[i]==0 ) continue;
2518 if( stat(azDirs[i], &buf) ) continue;
2519 if( !S_ISDIR(buf.st_mode) ) continue;
2520 if( access(azDirs[i], 07) ) continue;
2521 zDir = azDirs[i];
2522 break;
2523 }
2524 do{
2525 sqlite3_snprintf(MAX_PATHNAME-17, zBuf, "%s/"TEMP_FILE_PREFIX, zDir);
2526 j = strlen(zBuf);
2527 sqlite3Randomness(15, &zBuf[j]);
2528 for(i=0; i<15; i++, j++){
2529 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
2530 }
2531 zBuf[j] = 0;
2532 }while( access(zBuf,0)==0 );
2533 return SQLITE_OK;
2534}
2535
2536
2537/*
2538** Turn a relative pathname into a full pathname. The relative path
2539** is stored as a nul-terminated string in the buffer pointed to by
2540** zPath.
2541**
2542** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
2543** (in this case, MAX_PATHNAME bytes). The full-path is written to
2544** this buffer before returning.
2545*/
2546static int unixFullPathname(void *pNotUsed, const char *zPath, char *zOut){
2547 zOut[MAX_PATHNAME-1] = '\0';
2548 if( zPath[0]=='/' ){
2549 strncpy(zOut, zPath, MAX_PATHNAME-1);
2550 }else{
2551 int nCwd;
2552 if( getcwd(zOut, MAX_PATHNAME-1)==0 ){
2553 return SQLITE_ERROR;
2554 }
2555 nCwd = strlen(zOut);
2556 zOut[nCwd] = '/';
2557 strncpy(&zOut[nCwd+1], zPath, MAX_PATHNAME-1-nCwd-1);
2558 }
2559 return SQLITE_OK;
2560
2561#if 0
2562 /*
2563 ** Remove "/./" path elements and convert "/A/./" path elements
2564 ** to just "/".
2565 */
2566 if( zFull ){
2567 int i, j;
2568 for(i=j=0; zFull[i]; i++){
2569 if( zFull[i]=='/' ){
2570 if( zFull[i+1]=='/' ) continue;
2571 if( zFull[i+1]=='.' && zFull[i+2]=='/' ){
2572 i += 1;
2573 continue;
2574 }
2575 if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){
2576 while( j>0 && zFull[j-1]!='/' ){ j--; }
2577 i += 3;
2578 continue;
2579 }
2580 }
2581 zFull[j++] = zFull[i];
2582 }
2583 zFull[j] = 0;
2584 }
2585#endif
2586}
2587
drh0ccebe72005-06-07 22:22:50 +00002588
drh761df872006-12-21 01:29:22 +00002589#ifndef SQLITE_OMIT_LOAD_EXTENSION
2590/*
2591** Interfaces for opening a shared library, finding entry points
2592** within the shared library, and closing the shared library.
2593*/
2594#include <dlfcn.h>
danielk1977b4b47412007-08-17 15:53:36 +00002595static void *unixDlOpen(void *pNotUsed, const char *zFilename){
drh761df872006-12-21 01:29:22 +00002596 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
2597}
danielk1977b4b47412007-08-17 15:53:36 +00002598static void unixDlError(void *pNotUsed, int nBuf, char *zBufOut){
2599 char *zErr;
2600 enterMutex();
2601 zErr = dlerror();
2602 if( zErr ){
2603 strncpy(zBufOut, zErr, nBuf-1);
2604 zBufOut[nBuf-1] = '\0';
2605 }else if(nBuf>0) {
2606 zBufOut[0] = '\0';
2607 }
2608 leaveMutex();
2609}
2610void *unixDlSym(void *pHandle, const char *zSymbol){
drh761df872006-12-21 01:29:22 +00002611 return dlsym(pHandle, zSymbol);
2612}
danielk1977b4b47412007-08-17 15:53:36 +00002613void unixDlClose(void *pHandle){
2614 dlclose(pHandle);
drh761df872006-12-21 01:29:22 +00002615}
danielk1977b4b47412007-08-17 15:53:36 +00002616#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
2617 #define unixDlOpen 0
2618 #define unixDlError 0
2619 #define unixDlSym 0
2620 #define unixDlClose 0
2621#endif
2622
2623/*
danielk197790949c22007-08-17 16:50:38 +00002624** Write nBuf bytes of random data to the supplied buffer zBuf.
drhbbd42a62004-05-22 17:41:58 +00002625*/
danielk1977b4b47412007-08-17 15:53:36 +00002626static int unixRandomness(void *pNotUsed, int nBuf, char *zBuf){
danielk197790949c22007-08-17 16:50:38 +00002627
2628 assert(nBuf>=(sizeof(time_t)+sizeof(int)));
2629
drhbbd42a62004-05-22 17:41:58 +00002630 /* We have to initialize zBuf to prevent valgrind from reporting
2631 ** errors. The reports issued by valgrind are incorrect - we would
2632 ** prefer that the randomness be increased by making use of the
2633 ** uninitialized space in zBuf - but valgrind errors tend to worry
2634 ** some users. Rather than argue, it seems easier just to initialize
2635 ** the whole array and silence valgrind, even if that means less randomness
2636 ** in the random seed.
2637 **
2638 ** When testing, initializing zBuf[] to zero is all we do. That means
drhf1a221e2006-01-15 17:27:17 +00002639 ** that we always use the same random number sequence. This makes the
drhbbd42a62004-05-22 17:41:58 +00002640 ** tests repeatable.
2641 */
danielk1977b4b47412007-08-17 15:53:36 +00002642 memset(zBuf, 0, nBuf);
drhbbd42a62004-05-22 17:41:58 +00002643#if !defined(SQLITE_TEST)
2644 {
drh842b8642005-01-21 17:53:17 +00002645 int pid, fd;
2646 fd = open("/dev/urandom", O_RDONLY);
2647 if( fd<0 ){
drh07397232006-01-06 14:46:46 +00002648 time_t t;
2649 time(&t);
danielk197790949c22007-08-17 16:50:38 +00002650 memcpy(zBuf, &t, sizeof(t));
2651 pid = getpid();
2652 memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
drh842b8642005-01-21 17:53:17 +00002653 }else{
danielk1977b4b47412007-08-17 15:53:36 +00002654 read(fd, zBuf, nBuf);
drh842b8642005-01-21 17:53:17 +00002655 close(fd);
2656 }
drhbbd42a62004-05-22 17:41:58 +00002657 }
2658#endif
2659 return SQLITE_OK;
2660}
2661
danielk1977b4b47412007-08-17 15:53:36 +00002662
drhbbd42a62004-05-22 17:41:58 +00002663/*
2664** Sleep for a little while. Return the amount of time slept.
danielk1977b4b47412007-08-17 15:53:36 +00002665** The argument is the number of microseconds we want to sleep.
drhbbd42a62004-05-22 17:41:58 +00002666*/
danielk1977b4b47412007-08-17 15:53:36 +00002667static int unixSleep(void *pNotUsed, int microseconds){
drhbbd42a62004-05-22 17:41:58 +00002668#if defined(HAVE_USLEEP) && HAVE_USLEEP
danielk1977b4b47412007-08-17 15:53:36 +00002669 usleep(microseconds);
2670 return microseconds;
drhbbd42a62004-05-22 17:41:58 +00002671#else
danielk1977b4b47412007-08-17 15:53:36 +00002672 int seconds = (microseconds+999999)/1000000;
2673 sleep(seconds);
2674 return seconds;
drha3fad6f2006-01-18 14:06:37 +00002675#endif
drh88f474a2006-01-02 20:00:12 +00002676}
2677
2678/*
drhbbd42a62004-05-22 17:41:58 +00002679** The following variable, if set to a non-zero value, becomes the result
drh66560ad2006-01-06 14:32:19 +00002680** returned from sqlite3OsCurrentTime(). This is used for testing.
drhbbd42a62004-05-22 17:41:58 +00002681*/
2682#ifdef SQLITE_TEST
2683int sqlite3_current_time = 0;
2684#endif
2685
2686/*
2687** Find the current time (in Universal Coordinated Time). Write the
2688** current time and date as a Julian Day number into *prNow and
2689** return 0. Return 1 if the time and date cannot be found.
2690*/
danielk1977b4b47412007-08-17 15:53:36 +00002691static int unixCurrentTime(void *pNotUsed, double *prNow){
drh19e2d372005-08-29 23:00:03 +00002692#ifdef NO_GETTOD
drhbbd42a62004-05-22 17:41:58 +00002693 time_t t;
2694 time(&t);
2695 *prNow = t/86400.0 + 2440587.5;
drh19e2d372005-08-29 23:00:03 +00002696#else
2697 struct timeval sNow;
drhbdcc2762007-04-02 18:06:57 +00002698 gettimeofday(&sNow, 0);
drh19e2d372005-08-29 23:00:03 +00002699 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
2700#endif
drhbbd42a62004-05-22 17:41:58 +00002701#ifdef SQLITE_TEST
2702 if( sqlite3_current_time ){
2703 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
2704 }
2705#endif
2706 return 0;
2707}
danielk1977b4b47412007-08-17 15:53:36 +00002708
2709
2710sqlite3_vfs sqlite3DefaultVfs = {
2711 1, /* iVersion */
2712 sizeof(unixFile), /* szOsFile */
2713 MAX_PATHNAME, /* mxPathname */
2714 0, /* nRef */
2715 0, /* vfsMutex */
2716 0, /* pNext */
danielk1977b4b47412007-08-17 15:53:36 +00002717 "unix", /* zName */
2718 0, /* pAppData */
2719
2720 unixOpen, /* xOpen */
2721 unixDelete, /* xDelete */
2722 unixAccess, /* xAccess */
2723 unixGetTempName, /* xGetTempName */
2724 unixFullPathname, /* xFullPathname */
2725 unixDlOpen, /* xDlOpen */
2726 unixDlError, /* xDlError */
2727 unixDlSym, /* xDlSym */
2728 unixDlClose, /* xDlClose */
2729 unixRandomness, /* xRandomness */
2730 unixSleep, /* xSleep */
2731 unixCurrentTime /* xCurrentTime */
2732};
drhdce8bdb2007-08-16 13:01:44 +00002733
drhbbd42a62004-05-22 17:41:58 +00002734#endif /* OS_UNIX */