blob: 1b93b792a986a84cfafad28ea77935462d41e61a [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.
danielk1977822a5162008-05-16 04:51:54 +000014**
danielk1977e339d652008-06-28 11:23:00 +000015** $Id: os_unix.c,v 1.191 2008/06/28 11:23:00 danielk1977 Exp $
drhbbd42a62004-05-22 17:41:58 +000016*/
drhbbd42a62004-05-22 17:41:58 +000017#include "sqliteInt.h"
danielk197729bafea2008-06-26 10:41:19 +000018#if SQLITE_OS_UNIX /* This file is used on unix only */
drh66560ad2006-01-06 14:32:19 +000019
danielk1977e339d652008-06-28 11:23:00 +000020/*
21** If SQLITE_ENABLE_LOCKING_STYLE is defined, then several different
22** locking implementations are provided:
23**
24** * POSIX locking (the default),
25** * No locking,
26** * Dot-file locking,
27** * flock() locking,
28** * AFP locking (OSX only).
29*/
drhbfe66312006-10-03 17:40:40 +000030/* #define SQLITE_ENABLE_LOCKING_STYLE 0 */
31
drh9cbe6352005-11-29 03:13:21 +000032/*
33** These #defines should enable >2GB file support on Posix if the
34** underlying operating system supports it. If the OS lacks
drhf1a221e2006-01-15 17:27:17 +000035** large file support, these should be no-ops.
drh9cbe6352005-11-29 03:13:21 +000036**
37** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
38** on the compiler command line. This is necessary if you are compiling
39** on a recent machine (ex: RedHat 7.2) but you want your code to work
40** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2
41** without this option, LFS is enable. But LFS does not exist in the kernel
42** in RedHat 6.0, so the code won't work. Hence, for maximum binary
43** portability you should omit LFS.
drh9cbe6352005-11-29 03:13:21 +000044*/
45#ifndef SQLITE_DISABLE_LFS
46# define _LARGE_FILE 1
47# ifndef _FILE_OFFSET_BITS
48# define _FILE_OFFSET_BITS 64
49# endif
50# define _LARGEFILE_SOURCE 1
51#endif
drhbbd42a62004-05-22 17:41:58 +000052
drh9cbe6352005-11-29 03:13:21 +000053/*
54** standard include files.
55*/
56#include <sys/types.h>
57#include <sys/stat.h>
58#include <fcntl.h>
59#include <unistd.h>
drhbbd42a62004-05-22 17:41:58 +000060#include <time.h>
drh19e2d372005-08-29 23:00:03 +000061#include <sys/time.h>
drhbbd42a62004-05-22 17:41:58 +000062#include <errno.h>
danielk1977e339d652008-06-28 11:23:00 +000063
drhbfe66312006-10-03 17:40:40 +000064#ifdef SQLITE_ENABLE_LOCKING_STYLE
65#include <sys/ioctl.h>
66#include <sys/param.h>
67#include <sys/mount.h>
68#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drh9cbe6352005-11-29 03:13:21 +000069
70/*
drhf1a221e2006-01-15 17:27:17 +000071** If we are to be thread-safe, include the pthreads header and define
72** the SQLITE_UNIX_THREADS macro.
drh9cbe6352005-11-29 03:13:21 +000073*/
drhd677b3d2007-08-20 22:48:41 +000074#if SQLITE_THREADSAFE
drh9cbe6352005-11-29 03:13:21 +000075# include <pthread.h>
76# define SQLITE_UNIX_THREADS 1
77#endif
78
79/*
80** Default permissions when creating a new file
81*/
82#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
83# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
84#endif
85
danielk1977b4b47412007-08-17 15:53:36 +000086/*
87** Maximum supported path-length.
88*/
89#define MAX_PATHNAME 512
drh9cbe6352005-11-29 03:13:21 +000090
91
92/*
danielk1977ad94b582007-08-20 06:44:22 +000093** The unixFile structure is subclass of sqlite3_file specific for the unix
drh054889e2005-11-30 03:20:31 +000094** protability layer.
drh9cbe6352005-11-29 03:13:21 +000095*/
drh054889e2005-11-30 03:20:31 +000096typedef struct unixFile unixFile;
97struct unixFile {
danielk197762079062007-08-15 17:08:46 +000098 sqlite3_io_methods const *pMethod; /* Always the first entry */
danielk1977967a4a12007-08-20 14:23:44 +000099#ifdef SQLITE_TEST
100 /* In test mode, increase the size of this structure a bit so that
101 ** it is larger than the struct CrashFile defined in test6.c.
102 */
103 char aPadding[32];
104#endif
drh9cbe6352005-11-29 03:13:21 +0000105 struct openCnt *pOpen; /* Info about all open fd's on this inode */
106 struct lockInfo *pLock; /* Info about locks on this inode */
drhbfe66312006-10-03 17:40:40 +0000107#ifdef SQLITE_ENABLE_LOCKING_STYLE
108 void *lockingContext; /* Locking style specific state */
danielk1977e339d652008-06-28 11:23:00 +0000109#endif
drh9cbe6352005-11-29 03:13:21 +0000110 int h; /* The file descriptor */
111 unsigned char locktype; /* The type of lock held on this fd */
drh9cbe6352005-11-29 03:13:21 +0000112 int dirfd; /* File descriptor for the directory */
drhd677b3d2007-08-20 22:48:41 +0000113#if SQLITE_THREADSAFE
danielk1977ad94b582007-08-20 06:44:22 +0000114 pthread_t tid; /* The thread that "owns" this unixFile */
drh9cbe6352005-11-29 03:13:21 +0000115#endif
116};
117
drh0ccebe72005-06-07 22:22:50 +0000118/*
drh198bf392006-01-06 21:52:49 +0000119** Include code that is common to all os_*.c files
120*/
121#include "os_common.h"
122
123/*
drh0ccebe72005-06-07 22:22:50 +0000124** Define various macros that are missing from some systems.
125*/
drhbbd42a62004-05-22 17:41:58 +0000126#ifndef O_LARGEFILE
127# define O_LARGEFILE 0
128#endif
129#ifdef SQLITE_DISABLE_LFS
130# undef O_LARGEFILE
131# define O_LARGEFILE 0
132#endif
133#ifndef O_NOFOLLOW
134# define O_NOFOLLOW 0
135#endif
136#ifndef O_BINARY
137# define O_BINARY 0
138#endif
139
140/*
141** The DJGPP compiler environment looks mostly like Unix, but it
142** lacks the fcntl() system call. So redefine fcntl() to be something
143** that always succeeds. This means that locking does not occur under
drh85b623f2007-12-13 21:54:09 +0000144** DJGPP. But it is DOS - what did you expect?
drhbbd42a62004-05-22 17:41:58 +0000145*/
146#ifdef __DJGPP__
147# define fcntl(A,B,C) 0
148#endif
149
150/*
drh2b4b5962005-06-15 17:47:55 +0000151** The threadid macro resolves to the thread-id or to 0. Used for
152** testing and debugging only.
153*/
drhd677b3d2007-08-20 22:48:41 +0000154#if SQLITE_THREADSAFE
drh2b4b5962005-06-15 17:47:55 +0000155#define threadid pthread_self()
156#else
157#define threadid 0
158#endif
159
160/*
danielk1977ad94b582007-08-20 06:44:22 +0000161** Set or check the unixFile.tid field. This field is set when an unixFile
162** is first opened. All subsequent uses of the unixFile verify that the
163** same thread is operating on the unixFile. Some operating systems do
drh2b4b5962005-06-15 17:47:55 +0000164** not allow locks to be overridden by other threads and that restriction
165** means that sqlite3* database handles cannot be moved from one thread
166** to another. This logic makes sure a user does not try to do that
167** by mistake.
drhf1a221e2006-01-15 17:27:17 +0000168**
danielk1977ad94b582007-08-20 06:44:22 +0000169** Version 3.3.1 (2006-01-15): unixFile can be moved from one thread to
drhf1a221e2006-01-15 17:27:17 +0000170** another as long as we are running on a system that supports threads
171** overriding each others locks (which now the most common behavior)
danielk1977ad94b582007-08-20 06:44:22 +0000172** or if no locks are held. But the unixFile.pLock field needs to be
drhf1a221e2006-01-15 17:27:17 +0000173** recomputed because its key includes the thread-id. See the
174** transferOwnership() function below for additional information
drh2b4b5962005-06-15 17:47:55 +0000175*/
drhd677b3d2007-08-20 22:48:41 +0000176#if SQLITE_THREADSAFE
drh9cbe6352005-11-29 03:13:21 +0000177# define SET_THREADID(X) (X)->tid = pthread_self()
drh029b44b2006-01-15 00:13:15 +0000178# define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
179 !pthread_equal((X)->tid, pthread_self()))
drh2b4b5962005-06-15 17:47:55 +0000180#else
181# define SET_THREADID(X)
182# define CHECK_THREADID(X) 0
danielk197713adf8a2004-06-03 16:08:41 +0000183#endif
184
drhbbd42a62004-05-22 17:41:58 +0000185/*
186** Here is the dirt on POSIX advisory locks: ANSI STD 1003.1 (1996)
187** section 6.5.2.2 lines 483 through 490 specify that when a process
188** sets or clears a lock, that operation overrides any prior locks set
189** by the same process. It does not explicitly say so, but this implies
190** that it overrides locks set by the same process using a different
191** file descriptor. Consider this test case:
drhbbd42a62004-05-22 17:41:58 +0000192** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
193**
194** Suppose ./file1 and ./file2 are really the same file (because
195** one is a hard or symbolic link to the other) then if you set
196** an exclusive lock on fd1, then try to get an exclusive lock
197** on fd2, it works. I would have expected the second lock to
198** fail since there was already a lock on the file due to fd1.
199** But not so. Since both locks came from the same process, the
200** second overrides the first, even though they were on different
201** file descriptors opened on different file names.
202**
203** Bummer. If you ask me, this is broken. Badly broken. It means
204** that we cannot use POSIX locks to synchronize file access among
205** competing threads of the same process. POSIX locks will work fine
206** to synchronize access for threads in separate processes, but not
207** threads within the same process.
208**
209** To work around the problem, SQLite has to manage file locks internally
210** on its own. Whenever a new database is opened, we have to find the
211** specific inode of the database file (the inode is determined by the
212** st_dev and st_ino fields of the stat structure that fstat() fills in)
213** and check for locks already existing on that inode. When locks are
214** created or removed, we have to look at our own internal record of the
215** locks to see if another thread has previously set a lock on that same
216** inode.
217**
danielk1977ad94b582007-08-20 06:44:22 +0000218** The sqlite3_file structure for POSIX is no longer just an integer file
drhbbd42a62004-05-22 17:41:58 +0000219** descriptor. It is now a structure that holds the integer file
220** descriptor and a pointer to a structure that describes the internal
221** locks on the corresponding inode. There is one locking structure
danielk1977ad94b582007-08-20 06:44:22 +0000222** per inode, so if the same inode is opened twice, both unixFile structures
drhbbd42a62004-05-22 17:41:58 +0000223** point to the same locking structure. The locking structure keeps
224** a reference count (so we will know when to delete it) and a "cnt"
225** field that tells us its internal lock status. cnt==0 means the
226** file is unlocked. cnt==-1 means the file has an exclusive lock.
227** cnt>0 means there are cnt shared locks on the file.
228**
229** Any attempt to lock or unlock a file first checks the locking
230** structure. The fcntl() system call is only invoked to set a
231** POSIX lock if the internal lock structure transitions between
232** a locked and an unlocked state.
233**
234** 2004-Jan-11:
235** More recent discoveries about POSIX advisory locks. (The more
236** I discover, the more I realize the a POSIX advisory locks are
237** an abomination.)
238**
239** If you close a file descriptor that points to a file that has locks,
240** all locks on that file that are owned by the current process are
danielk1977ad94b582007-08-20 06:44:22 +0000241** released. To work around this problem, each unixFile structure contains
drhbbd42a62004-05-22 17:41:58 +0000242** a pointer to an openCnt structure. There is one openCnt structure
danielk1977ad94b582007-08-20 06:44:22 +0000243** per open inode, which means that multiple unixFile can point to a single
244** openCnt. When an attempt is made to close an unixFile, if there are
245** other unixFile open on the same inode that are holding locks, the call
drhbbd42a62004-05-22 17:41:58 +0000246** to close() the file descriptor is deferred until all of the locks clear.
247** The openCnt structure keeps a list of file descriptors that need to
248** be closed and that list is walked (and cleared) when the last lock
249** clears.
250**
251** First, under Linux threads, because each thread has a separate
252** process ID, lock operations in one thread do not override locks
253** to the same file in other threads. Linux threads behave like
254** separate processes in this respect. But, if you close a file
255** descriptor in linux threads, all locks are cleared, even locks
256** on other threads and even though the other threads have different
257** process IDs. Linux threads is inconsistent in this respect.
258** (I'm beginning to think that linux threads is an abomination too.)
259** The consequence of this all is that the hash table for the lockInfo
260** structure has to include the process id as part of its key because
261** locks in different threads are treated as distinct. But the
262** openCnt structure should not include the process id in its
263** key because close() clears lock on all threads, not just the current
264** thread. Were it not for this goofiness in linux threads, we could
265** combine the lockInfo and openCnt structures into a single structure.
drh5fdae772004-06-29 03:29:00 +0000266**
267** 2004-Jun-28:
268** On some versions of linux, threads can override each others locks.
269** On others not. Sometimes you can change the behavior on the same
270** system by setting the LD_ASSUME_KERNEL environment variable. The
271** POSIX standard is silent as to which behavior is correct, as far
272** as I can tell, so other versions of unix might show the same
273** inconsistency. There is no little doubt in my mind that posix
274** advisory locks and linux threads are profoundly broken.
275**
276** To work around the inconsistencies, we have to test at runtime
277** whether or not threads can override each others locks. This test
278** is run once, the first time any lock is attempted. A static
279** variable is set to record the results of this test for future
280** use.
drhbbd42a62004-05-22 17:41:58 +0000281*/
282
283/*
284** An instance of the following structure serves as the key used
drh5fdae772004-06-29 03:29:00 +0000285** to locate a particular lockInfo structure given its inode.
286**
287** If threads cannot override each others locks, then we set the
288** lockKey.tid field to the thread ID. If threads can override
drhf1a221e2006-01-15 17:27:17 +0000289** each others locks then tid is always set to zero. tid is omitted
290** if we compile without threading support.
drhbbd42a62004-05-22 17:41:58 +0000291*/
292struct lockKey {
drh5fdae772004-06-29 03:29:00 +0000293 dev_t dev; /* Device number */
294 ino_t ino; /* Inode number */
drhd677b3d2007-08-20 22:48:41 +0000295#if SQLITE_THREADSAFE
drhd9cb6ac2005-10-20 07:28:17 +0000296 pthread_t tid; /* Thread ID or zero if threads can override each other */
drh5fdae772004-06-29 03:29:00 +0000297#endif
drhbbd42a62004-05-22 17:41:58 +0000298};
299
300/*
301** An instance of the following structure is allocated for each open
302** inode on each thread with a different process ID. (Threads have
303** different process IDs on linux, but not on most other unixes.)
304**
danielk1977ad94b582007-08-20 06:44:22 +0000305** A single inode can have multiple file descriptors, so each unixFile
drhbbd42a62004-05-22 17:41:58 +0000306** structure contains a pointer to an instance of this object and this
danielk1977ad94b582007-08-20 06:44:22 +0000307** object keeps a count of the number of unixFile pointing to it.
drhbbd42a62004-05-22 17:41:58 +0000308*/
309struct lockInfo {
310 struct lockKey key; /* The lookup key */
drh2ac3ee92004-06-07 16:27:46 +0000311 int cnt; /* Number of SHARED locks held */
danielk19779a1d0ab2004-06-01 14:09:28 +0000312 int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
drhbbd42a62004-05-22 17:41:58 +0000313 int nRef; /* Number of pointers to this structure */
314};
315
316/*
317** An instance of the following structure serves as the key used
318** to locate a particular openCnt structure given its inode. This
drh5fdae772004-06-29 03:29:00 +0000319** is the same as the lockKey except that the thread ID is omitted.
drhbbd42a62004-05-22 17:41:58 +0000320*/
321struct openKey {
322 dev_t dev; /* Device number */
323 ino_t ino; /* Inode number */
324};
325
326/*
327** An instance of the following structure is allocated for each open
328** inode. This structure keeps track of the number of locks on that
329** inode. If a close is attempted against an inode that is holding
330** locks, the close is deferred until all locks clear by adding the
331** file descriptor to be closed to the pending list.
332*/
333struct openCnt {
334 struct openKey key; /* The lookup key */
335 int nRef; /* Number of pointers to this structure */
336 int nLock; /* Number of outstanding locks */
337 int nPending; /* Number of pending close() operations */
338 int *aPending; /* Malloced space holding fd's awaiting a close() */
339};
340
341/*
drhf1a221e2006-01-15 17:27:17 +0000342** These hash tables map inodes and file descriptors (really, lockKey and
343** openKey structures) into lockInfo and openCnt structures. Access to
344** these hash tables must be protected by a mutex.
drhbbd42a62004-05-22 17:41:58 +0000345*/
drh17435752007-08-16 04:30:38 +0000346static Hash lockHash = {SQLITE_HASH_BINARY, 0, 0, 0, 0, 0};
347static Hash openHash = {SQLITE_HASH_BINARY, 0, 0, 0, 0, 0};
drh5fdae772004-06-29 03:29:00 +0000348
drhbfe66312006-10-03 17:40:40 +0000349/*
350** The locking styles are associated with the different file locking
351** capabilities supported by different file systems.
352**
353** POSIX locking style fully supports shared and exclusive byte-range locks
danielk1977e339d652008-06-28 11:23:00 +0000354** AFP locking only supports exclusive byte-range locks
drhbfe66312006-10-03 17:40:40 +0000355** FLOCK only supports a single file-global exclusive lock
356** DOTLOCK isn't a true locking style, it refers to the use of a special
357** file named the same as the database file with a '.lock' extension, this
358** can be used on file systems that do not offer any reliable file locking
359** NO locking means that no locking will be attempted, this is only used for
360** read-only file systems currently
361** UNSUPPORTED means that no locking will be attempted, this is only used for
362** file systems that are known to be unsupported
363*/
danielk1977e339d652008-06-28 11:23:00 +0000364#define LOCKING_STYLE_POSIX 1
365#define LOCKING_STYLE_FLOCK 2
366#define LOCKING_STYLE_DOTFILE 3
367#define LOCKING_STYLE_NONE 4
368#define LOCKING_STYLE_AFP 5
drhbfe66312006-10-03 17:40:40 +0000369
danielk1977ad94b582007-08-20 06:44:22 +0000370/*
371** Helper functions to obtain and relinquish the global mutex.
372*/
danielk1977b4b47412007-08-17 15:53:36 +0000373static void enterMutex(){
danielk197759f8c082008-06-18 17:09:10 +0000374 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
danielk1977b4b47412007-08-17 15:53:36 +0000375}
376static void leaveMutex(){
danielk197759f8c082008-06-18 17:09:10 +0000377 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
danielk1977b4b47412007-08-17 15:53:36 +0000378}
379
drhd677b3d2007-08-20 22:48:41 +0000380#if SQLITE_THREADSAFE
drh5fdae772004-06-29 03:29:00 +0000381/*
382** This variable records whether or not threads can override each others
383** locks.
384**
385** 0: No. Threads cannot override each others locks.
386** 1: Yes. Threads can override each others locks.
387** -1: We don't know yet.
drhf1a221e2006-01-15 17:27:17 +0000388**
drh5062d3a2006-01-31 23:03:35 +0000389** On some systems, we know at compile-time if threads can override each
390** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro
391** will be set appropriately. On other systems, we have to check at
392** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is
393** undefined.
394**
drhf1a221e2006-01-15 17:27:17 +0000395** This variable normally has file scope only. But during testing, we make
396** it a global so that the test code can change its value in order to verify
397** that the right stuff happens in either case.
drh5fdae772004-06-29 03:29:00 +0000398*/
drh5062d3a2006-01-31 23:03:35 +0000399#ifndef SQLITE_THREAD_OVERRIDE_LOCK
400# define SQLITE_THREAD_OVERRIDE_LOCK -1
401#endif
drh029b44b2006-01-15 00:13:15 +0000402#ifdef SQLITE_TEST
drh5062d3a2006-01-31 23:03:35 +0000403int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh029b44b2006-01-15 00:13:15 +0000404#else
drh5062d3a2006-01-31 23:03:35 +0000405static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh029b44b2006-01-15 00:13:15 +0000406#endif
drh5fdae772004-06-29 03:29:00 +0000407
408/*
409** This structure holds information passed into individual test
410** threads by the testThreadLockingBehavior() routine.
411*/
412struct threadTestData {
413 int fd; /* File to be locked */
414 struct flock lock; /* The locking operation */
415 int result; /* Result of the locking operation */
416};
417
drh2b4b5962005-06-15 17:47:55 +0000418#ifdef SQLITE_LOCK_TRACE
419/*
420** Print out information about all locking operations.
421**
422** This routine is used for troubleshooting locks on multithreaded
423** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
424** command-line option on the compiler. This code is normally
drhf1a221e2006-01-15 17:27:17 +0000425** turned off.
drh2b4b5962005-06-15 17:47:55 +0000426*/
427static int lockTrace(int fd, int op, struct flock *p){
428 char *zOpName, *zType;
429 int s;
430 int savedErrno;
431 if( op==F_GETLK ){
432 zOpName = "GETLK";
433 }else if( op==F_SETLK ){
434 zOpName = "SETLK";
435 }else{
436 s = fcntl(fd, op, p);
437 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
438 return s;
439 }
440 if( p->l_type==F_RDLCK ){
441 zType = "RDLCK";
442 }else if( p->l_type==F_WRLCK ){
443 zType = "WRLCK";
444 }else if( p->l_type==F_UNLCK ){
445 zType = "UNLCK";
446 }else{
447 assert( 0 );
448 }
449 assert( p->l_whence==SEEK_SET );
450 s = fcntl(fd, op, p);
451 savedErrno = errno;
452 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
453 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
454 (int)p->l_pid, s);
drhe2396a12007-03-29 20:19:58 +0000455 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
drh2b4b5962005-06-15 17:47:55 +0000456 struct flock l2;
457 l2 = *p;
458 fcntl(fd, F_GETLK, &l2);
459 if( l2.l_type==F_RDLCK ){
460 zType = "RDLCK";
461 }else if( l2.l_type==F_WRLCK ){
462 zType = "WRLCK";
463 }else if( l2.l_type==F_UNLCK ){
464 zType = "UNLCK";
465 }else{
466 assert( 0 );
467 }
468 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
469 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
470 }
471 errno = savedErrno;
472 return s;
473}
474#define fcntl lockTrace
475#endif /* SQLITE_LOCK_TRACE */
476
drh5fdae772004-06-29 03:29:00 +0000477/*
478** The testThreadLockingBehavior() routine launches two separate
479** threads on this routine. This routine attempts to lock a file
480** descriptor then returns. The success or failure of that attempt
481** allows the testThreadLockingBehavior() procedure to determine
482** whether or not threads can override each others locks.
483*/
484static void *threadLockingTest(void *pArg){
485 struct threadTestData *pData = (struct threadTestData*)pArg;
486 pData->result = fcntl(pData->fd, F_SETLK, &pData->lock);
487 return pArg;
488}
489
490/*
491** This procedure attempts to determine whether or not threads
492** can override each others locks then sets the
493** threadsOverrideEachOthersLocks variable appropriately.
494*/
danielk19774d5238f2006-01-27 06:32:00 +0000495static void testThreadLockingBehavior(int fd_orig){
drh5fdae772004-06-29 03:29:00 +0000496 int fd;
497 struct threadTestData d[2];
498 pthread_t t[2];
499
500 fd = dup(fd_orig);
501 if( fd<0 ) return;
502 memset(d, 0, sizeof(d));
503 d[0].fd = fd;
504 d[0].lock.l_type = F_RDLCK;
505 d[0].lock.l_len = 1;
506 d[0].lock.l_start = 0;
507 d[0].lock.l_whence = SEEK_SET;
508 d[1] = d[0];
509 d[1].lock.l_type = F_WRLCK;
510 pthread_create(&t[0], 0, threadLockingTest, &d[0]);
511 pthread_create(&t[1], 0, threadLockingTest, &d[1]);
512 pthread_join(t[0], 0);
513 pthread_join(t[1], 0);
514 close(fd);
515 threadsOverrideEachOthersLocks = d[0].result==0 && d[1].result==0;
516}
drhd677b3d2007-08-20 22:48:41 +0000517#endif /* SQLITE_THREADSAFE */
drh5fdae772004-06-29 03:29:00 +0000518
drhbbd42a62004-05-22 17:41:58 +0000519/*
520** Release a lockInfo structure previously allocated by findLockInfo().
521*/
522static void releaseLockInfo(struct lockInfo *pLock){
danielk1977e339d652008-06-28 11:23:00 +0000523 if( pLock ){
524 pLock->nRef--;
525 if( pLock->nRef==0 ){
526 sqlite3HashInsert(&lockHash, &pLock->key, sizeof(pLock->key), 0);
527 sqlite3_free(pLock);
528 }
drhbbd42a62004-05-22 17:41:58 +0000529 }
530}
531
532/*
533** Release a openCnt structure previously allocated by findLockInfo().
534*/
535static void releaseOpenCnt(struct openCnt *pOpen){
danielk1977e339d652008-06-28 11:23:00 +0000536 if( pOpen ){
537 pOpen->nRef--;
538 if( pOpen->nRef==0 ){
539 sqlite3HashInsert(&openHash, &pOpen->key, sizeof(pOpen->key), 0);
540 free(pOpen->aPending);
541 sqlite3_free(pOpen);
542 }
drhbbd42a62004-05-22 17:41:58 +0000543 }
544}
545
drhbfe66312006-10-03 17:40:40 +0000546/*
547** Tests a byte-range locking query to see if byte range locks are
548** supported, if not we fall back to dotlockLockingStyle.
549*/
danielk1977e339d652008-06-28 11:23:00 +0000550static int testLockingStyle(int fd){
drhbfe66312006-10-03 17:40:40 +0000551 struct flock lockInfo;
danielk1977e339d652008-06-28 11:23:00 +0000552
553 /* Test byte-range lock using fcntl(). If the call succeeds,
554 ** assume that the file-system supports POSIX style locks.
555 */
drhbfe66312006-10-03 17:40:40 +0000556 lockInfo.l_len = 1;
557 lockInfo.l_start = 0;
558 lockInfo.l_whence = SEEK_SET;
559 lockInfo.l_type = F_RDLCK;
danielk1977ad94b582007-08-20 06:44:22 +0000560 if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
danielk1977e339d652008-06-28 11:23:00 +0000561 return LOCKING_STYLE_POSIX;
562 }
drhbfe66312006-10-03 17:40:40 +0000563
danielk1977e339d652008-06-28 11:23:00 +0000564 /* Testing for flock() can give false positives. So if if the above
565 ** test fails, then we fall back to using dot-file style locking.
drhbfe66312006-10-03 17:40:40 +0000566 */
danielk1977e339d652008-06-28 11:23:00 +0000567 return LOCKING_STYLE_DOTFILE;
drhbfe66312006-10-03 17:40:40 +0000568}
569
570/*
danielk1977e339d652008-06-28 11:23:00 +0000571** If SQLITE_ENABLE_LOCKING_STYLE is defined, this function Examines the
572** f_fstypename entry in the statfs structure as returned by stat() for
573** the file system hosting the database file and selects the appropriate
574** locking style based on its value. These values and assignments are
575** based on Darwin/OSX behavior and have not been thoroughly tested on
drhbfe66312006-10-03 17:40:40 +0000576** other systems.
danielk1977e339d652008-06-28 11:23:00 +0000577**
578** If SQLITE_ENABLE_LOCKING_STYLE is not defined, this function always
579** returns LOCKING_STYLE_POSIX.
drhbfe66312006-10-03 17:40:40 +0000580*/
danielk1977e339d652008-06-28 11:23:00 +0000581static int detectLockingStyle(
582 sqlite3_vfs *pVfs,
danielk1977ad94b582007-08-20 06:44:22 +0000583 const char *filePath,
584 int fd
585){
danielk1977e339d652008-06-28 11:23:00 +0000586#ifdef SQLITE_ENABLE_LOCKING_STYLE
587 struct Mapping {
588 const char *zFilesystem;
589 int eLockingStyle;
590 } aMap[] = {
591 { "hfs", LOCKING_STYLE_POSIX },
592 { "ufs", LOCKING_STYLE_POSIX },
593 { "afpfs", LOCKING_STYLE_AFP },
594 { "smbfs", LOCKING_STYLE_FLOCK },
595 { "msdos", LOCKING_STYLE_DOTFILE },
596 { "webdav", LOCKING_STYLE_NONE },
597 { 0, 0 }
598 };
599 int i;
drhbfe66312006-10-03 17:40:40 +0000600 struct statfs fsInfo;
601
danielk1977e339d652008-06-28 11:23:00 +0000602 if( !filePath ){
603 return LOCKING_STYLE_NONE;
drh339eb0b2008-03-07 15:34:11 +0000604 }
danielk1977e339d652008-06-28 11:23:00 +0000605 if( pVfs->pAppData ){
606 return (int)pVfs->pAppData;
drh339eb0b2008-03-07 15:34:11 +0000607 }
drhbfe66312006-10-03 17:40:40 +0000608
danielk1977e339d652008-06-28 11:23:00 +0000609 if( statfs(filePath, &fsInfo) != -1 ){
610 if( fsInfo.f_flags & MNT_RDONLY ){
611 return LOCKING_STYLE_NONE;
612 }
613 for(i=0; aMap[i].zFilesystem; i++){
614 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
615 return aMap[i].eLockingStyle;
616 }
617 }
618 }
619
620 /* Default case. Handles, amongst others, "nfs". */
621 return testLockingStyle(fd);
622#endif
623 return LOCKING_STYLE_POSIX;
624}
drhbfe66312006-10-03 17:40:40 +0000625
drhbbd42a62004-05-22 17:41:58 +0000626/*
627** Given a file descriptor, locate lockInfo and openCnt structures that
drh029b44b2006-01-15 00:13:15 +0000628** describes that file descriptor. Create new ones if necessary. The
629** return values might be uninitialized if an error occurs.
drhbbd42a62004-05-22 17:41:58 +0000630**
drh65594042008-05-05 16:56:34 +0000631** Return an appropriate error code.
drhbbd42a62004-05-22 17:41:58 +0000632*/
drh38f82712004-06-18 17:10:16 +0000633static int findLockInfo(
drhbbd42a62004-05-22 17:41:58 +0000634 int fd, /* The file descriptor used in the key */
635 struct lockInfo **ppLock, /* Return the lockInfo structure here */
drh5fdae772004-06-29 03:29:00 +0000636 struct openCnt **ppOpen /* Return the openCnt structure here */
drhbbd42a62004-05-22 17:41:58 +0000637){
638 int rc;
639 struct lockKey key1;
640 struct openKey key2;
641 struct stat statbuf;
642 struct lockInfo *pLock;
643 struct openCnt *pOpen;
644 rc = fstat(fd, &statbuf);
drh65594042008-05-05 16:56:34 +0000645 if( rc!=0 ){
646#ifdef EOVERFLOW
647 if( errno==EOVERFLOW ) return SQLITE_NOLFS;
648#endif
649 return SQLITE_IOERR;
650 }
danielk1977441b09a2006-01-05 13:48:29 +0000651
drhbbd42a62004-05-22 17:41:58 +0000652 memset(&key1, 0, sizeof(key1));
653 key1.dev = statbuf.st_dev;
654 key1.ino = statbuf.st_ino;
drhd677b3d2007-08-20 22:48:41 +0000655#if SQLITE_THREADSAFE
drh5fdae772004-06-29 03:29:00 +0000656 if( threadsOverrideEachOthersLocks<0 ){
657 testThreadLockingBehavior(fd);
658 }
659 key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
660#endif
drhbbd42a62004-05-22 17:41:58 +0000661 memset(&key2, 0, sizeof(key2));
662 key2.dev = statbuf.st_dev;
663 key2.ino = statbuf.st_ino;
664 pLock = (struct lockInfo*)sqlite3HashFind(&lockHash, &key1, sizeof(key1));
665 if( pLock==0 ){
666 struct lockInfo *pOld;
drh17435752007-08-16 04:30:38 +0000667 pLock = sqlite3_malloc( sizeof(*pLock) );
danielk1977441b09a2006-01-05 13:48:29 +0000668 if( pLock==0 ){
drh65594042008-05-05 16:56:34 +0000669 rc = SQLITE_NOMEM;
danielk1977441b09a2006-01-05 13:48:29 +0000670 goto exit_findlockinfo;
671 }
drhbbd42a62004-05-22 17:41:58 +0000672 pLock->key = key1;
673 pLock->nRef = 1;
674 pLock->cnt = 0;
danielk19779a1d0ab2004-06-01 14:09:28 +0000675 pLock->locktype = 0;
drhbbd42a62004-05-22 17:41:58 +0000676 pOld = sqlite3HashInsert(&lockHash, &pLock->key, sizeof(key1), pLock);
677 if( pOld!=0 ){
678 assert( pOld==pLock );
drh17435752007-08-16 04:30:38 +0000679 sqlite3_free(pLock);
drh65594042008-05-05 16:56:34 +0000680 rc = SQLITE_NOMEM;
danielk1977441b09a2006-01-05 13:48:29 +0000681 goto exit_findlockinfo;
drhbbd42a62004-05-22 17:41:58 +0000682 }
683 }else{
684 pLock->nRef++;
685 }
686 *ppLock = pLock;
drh029b44b2006-01-15 00:13:15 +0000687 if( ppOpen!=0 ){
688 pOpen = (struct openCnt*)sqlite3HashFind(&openHash, &key2, sizeof(key2));
drhbbd42a62004-05-22 17:41:58 +0000689 if( pOpen==0 ){
drh029b44b2006-01-15 00:13:15 +0000690 struct openCnt *pOld;
drh17435752007-08-16 04:30:38 +0000691 pOpen = sqlite3_malloc( sizeof(*pOpen) );
drh029b44b2006-01-15 00:13:15 +0000692 if( pOpen==0 ){
693 releaseLockInfo(pLock);
drh65594042008-05-05 16:56:34 +0000694 rc = SQLITE_NOMEM;
drh029b44b2006-01-15 00:13:15 +0000695 goto exit_findlockinfo;
696 }
697 pOpen->key = key2;
698 pOpen->nRef = 1;
699 pOpen->nLock = 0;
700 pOpen->nPending = 0;
701 pOpen->aPending = 0;
702 pOld = sqlite3HashInsert(&openHash, &pOpen->key, sizeof(key2), pOpen);
703 if( pOld!=0 ){
704 assert( pOld==pOpen );
drh17435752007-08-16 04:30:38 +0000705 sqlite3_free(pOpen);
drh029b44b2006-01-15 00:13:15 +0000706 releaseLockInfo(pLock);
drh65594042008-05-05 16:56:34 +0000707 rc = SQLITE_NOMEM;
drh029b44b2006-01-15 00:13:15 +0000708 goto exit_findlockinfo;
709 }
710 }else{
711 pOpen->nRef++;
drhbbd42a62004-05-22 17:41:58 +0000712 }
drh029b44b2006-01-15 00:13:15 +0000713 *ppOpen = pOpen;
drhbbd42a62004-05-22 17:41:58 +0000714 }
danielk1977441b09a2006-01-05 13:48:29 +0000715
716exit_findlockinfo:
danielk1977441b09a2006-01-05 13:48:29 +0000717 return rc;
drhbbd42a62004-05-22 17:41:58 +0000718}
719
drh64b1bea2006-01-15 02:30:57 +0000720#ifdef SQLITE_DEBUG
721/*
722** Helper function for printing out trace information from debugging
723** binaries. This returns the string represetation of the supplied
724** integer lock-type.
725*/
726static const char *locktypeName(int locktype){
727 switch( locktype ){
728 case NO_LOCK: return "NONE";
729 case SHARED_LOCK: return "SHARED";
730 case RESERVED_LOCK: return "RESERVED";
731 case PENDING_LOCK: return "PENDING";
732 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
733 }
734 return "ERROR";
735}
736#endif
737
drhbbd42a62004-05-22 17:41:58 +0000738/*
drh029b44b2006-01-15 00:13:15 +0000739** If we are currently in a different thread than the thread that the
740** unixFile argument belongs to, then transfer ownership of the unixFile
741** over to the current thread.
742**
743** A unixFile is only owned by a thread on systems where one thread is
744** unable to override locks created by a different thread. RedHat9 is
745** an example of such a system.
746**
747** Ownership transfer is only allowed if the unixFile is currently unlocked.
748** If the unixFile is locked and an ownership is wrong, then return
drhf1a221e2006-01-15 17:27:17 +0000749** SQLITE_MISUSE. SQLITE_OK is returned if everything works.
drh029b44b2006-01-15 00:13:15 +0000750*/
drhd677b3d2007-08-20 22:48:41 +0000751#if SQLITE_THREADSAFE
drh029b44b2006-01-15 00:13:15 +0000752static int transferOwnership(unixFile *pFile){
drh64b1bea2006-01-15 02:30:57 +0000753 int rc;
drh029b44b2006-01-15 00:13:15 +0000754 pthread_t hSelf;
755 if( threadsOverrideEachOthersLocks ){
756 /* Ownership transfers not needed on this system */
757 return SQLITE_OK;
758 }
759 hSelf = pthread_self();
760 if( pthread_equal(pFile->tid, hSelf) ){
761 /* We are still in the same thread */
drh4f0c5872007-03-26 22:05:01 +0000762 OSTRACE1("No-transfer, same thread\n");
drh029b44b2006-01-15 00:13:15 +0000763 return SQLITE_OK;
764 }
765 if( pFile->locktype!=NO_LOCK ){
766 /* We cannot change ownership while we are holding a lock! */
767 return SQLITE_MISUSE;
768 }
drh4f0c5872007-03-26 22:05:01 +0000769 OSTRACE4("Transfer ownership of %d from %d to %d\n",
770 pFile->h, pFile->tid, hSelf);
drh029b44b2006-01-15 00:13:15 +0000771 pFile->tid = hSelf;
drhbfe66312006-10-03 17:40:40 +0000772 if (pFile->pLock != NULL) {
773 releaseLockInfo(pFile->pLock);
774 rc = findLockInfo(pFile->h, &pFile->pLock, 0);
drh4f0c5872007-03-26 22:05:01 +0000775 OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +0000776 locktypeName(pFile->locktype),
777 locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
778 return rc;
779 } else {
780 return SQLITE_OK;
781 }
drh029b44b2006-01-15 00:13:15 +0000782}
783#else
drhf1a221e2006-01-15 17:27:17 +0000784 /* On single-threaded builds, ownership transfer is a no-op */
drh029b44b2006-01-15 00:13:15 +0000785# define transferOwnership(X) SQLITE_OK
786#endif
787
788/*
danielk19772a6bdf62007-08-20 16:07:00 +0000789** Seek to the offset passed as the second argument, then read cnt
790** bytes into pBuf. Return the number of bytes actually read.
drh9e0ebbf2007-10-23 15:59:18 +0000791**
792** NB: If you define USE_PREAD or USE_PREAD64, then it might also
793** be necessary to define _XOPEN_SOURCE to be 500. This varies from
794** one system to another. Since SQLite does not define USE_PREAD
795** any any form by default, we will not attempt to define _XOPEN_SOURCE.
796** See tickets #2741 and #2681.
drhb912b282006-03-23 22:42:20 +0000797*/
danielk197762079062007-08-15 17:08:46 +0000798static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
drhb912b282006-03-23 22:42:20 +0000799 int got;
drh8ebf6702007-02-06 11:11:08 +0000800 i64 newOffset;
drh15d00c42007-02-27 02:01:14 +0000801 TIMER_START;
drh8350a212007-03-22 15:22:06 +0000802#if defined(USE_PREAD)
danielk197762079062007-08-15 17:08:46 +0000803 got = pread(id->h, pBuf, cnt, offset);
drhbb5f18d2007-04-06 18:23:17 +0000804 SimulateIOError( got = -1 );
drh8350a212007-03-22 15:22:06 +0000805#elif defined(USE_PREAD64)
danielk197762079062007-08-15 17:08:46 +0000806 got = pread64(id->h, pBuf, cnt, offset);
drhbb5f18d2007-04-06 18:23:17 +0000807 SimulateIOError( got = -1 );
drhb912b282006-03-23 22:42:20 +0000808#else
danielk197762079062007-08-15 17:08:46 +0000809 newOffset = lseek(id->h, offset, SEEK_SET);
drhbb5f18d2007-04-06 18:23:17 +0000810 SimulateIOError( newOffset-- );
danielk197762079062007-08-15 17:08:46 +0000811 if( newOffset!=offset ){
drh8ebf6702007-02-06 11:11:08 +0000812 return -1;
813 }
drhb912b282006-03-23 22:42:20 +0000814 got = read(id->h, pBuf, cnt);
815#endif
drh15d00c42007-02-27 02:01:14 +0000816 TIMER_END;
shane9bcbdad2008-05-29 20:22:37 +0000817 OSTRACE5("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
drhb912b282006-03-23 22:42:20 +0000818 return got;
819}
820
821/*
drhbbd42a62004-05-22 17:41:58 +0000822** Read data from a file into a buffer. Return SQLITE_OK if all
823** bytes were read successfully and SQLITE_IOERR if anything goes
824** wrong.
825*/
danielk197762079062007-08-15 17:08:46 +0000826static int unixRead(
827 sqlite3_file *id,
828 void *pBuf,
829 int amt,
830 sqlite3_int64 offset
831){
drhbbd42a62004-05-22 17:41:58 +0000832 int got;
drh9cbe6352005-11-29 03:13:21 +0000833 assert( id );
danielk197762079062007-08-15 17:08:46 +0000834 got = seekAndRead((unixFile*)id, offset, pBuf, amt);
drhbbd42a62004-05-22 17:41:58 +0000835 if( got==amt ){
836 return SQLITE_OK;
drh4ac285a2006-09-15 07:28:50 +0000837 }else if( got<0 ){
838 return SQLITE_IOERR_READ;
drhbbd42a62004-05-22 17:41:58 +0000839 }else{
drhbafda092007-01-03 23:36:22 +0000840 memset(&((char*)pBuf)[got], 0, amt-got);
drh4ac285a2006-09-15 07:28:50 +0000841 return SQLITE_IOERR_SHORT_READ;
drhbbd42a62004-05-22 17:41:58 +0000842 }
843}
844
845/*
drhb912b282006-03-23 22:42:20 +0000846** Seek to the offset in id->offset then read cnt bytes into pBuf.
847** Return the number of bytes actually read. Update the offset.
848*/
danielk197762079062007-08-15 17:08:46 +0000849static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
drhb912b282006-03-23 22:42:20 +0000850 int got;
drh8ebf6702007-02-06 11:11:08 +0000851 i64 newOffset;
drh15d00c42007-02-27 02:01:14 +0000852 TIMER_START;
drh8350a212007-03-22 15:22:06 +0000853#if defined(USE_PREAD)
danielk197762079062007-08-15 17:08:46 +0000854 got = pwrite(id->h, pBuf, cnt, offset);
drh8350a212007-03-22 15:22:06 +0000855#elif defined(USE_PREAD64)
danielk197762079062007-08-15 17:08:46 +0000856 got = pwrite64(id->h, pBuf, cnt, offset);
drhb912b282006-03-23 22:42:20 +0000857#else
danielk197762079062007-08-15 17:08:46 +0000858 newOffset = lseek(id->h, offset, SEEK_SET);
859 if( newOffset!=offset ){
drh8ebf6702007-02-06 11:11:08 +0000860 return -1;
861 }
drhb912b282006-03-23 22:42:20 +0000862 got = write(id->h, pBuf, cnt);
863#endif
drh15d00c42007-02-27 02:01:14 +0000864 TIMER_END;
shane9bcbdad2008-05-29 20:22:37 +0000865 OSTRACE5("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
drhb912b282006-03-23 22:42:20 +0000866 return got;
867}
868
869
870/*
drhbbd42a62004-05-22 17:41:58 +0000871** Write data from a buffer into a file. Return SQLITE_OK on success
872** or some other error code on failure.
873*/
danielk197762079062007-08-15 17:08:46 +0000874static int unixWrite(
875 sqlite3_file *id,
876 const void *pBuf,
877 int amt,
878 sqlite3_int64 offset
879){
drhbbd42a62004-05-22 17:41:58 +0000880 int wrote = 0;
drh9cbe6352005-11-29 03:13:21 +0000881 assert( id );
drh4c7f9412005-02-03 00:29:47 +0000882 assert( amt>0 );
danielk197762079062007-08-15 17:08:46 +0000883 while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){
drhbbd42a62004-05-22 17:41:58 +0000884 amt -= wrote;
danielk197762079062007-08-15 17:08:46 +0000885 offset += wrote;
drhbbd42a62004-05-22 17:41:58 +0000886 pBuf = &((char*)pBuf)[wrote];
887 }
drh59685932006-09-14 13:47:11 +0000888 SimulateIOError(( wrote=(-1), amt=1 ));
889 SimulateDiskfullError(( wrote=0, amt=1 ));
drhbbd42a62004-05-22 17:41:58 +0000890 if( amt>0 ){
drh59685932006-09-14 13:47:11 +0000891 if( wrote<0 ){
drh4ac285a2006-09-15 07:28:50 +0000892 return SQLITE_IOERR_WRITE;
drh59685932006-09-14 13:47:11 +0000893 }else{
894 return SQLITE_FULL;
895 }
drhbbd42a62004-05-22 17:41:58 +0000896 }
897 return SQLITE_OK;
898}
899
drhb851b2c2005-03-10 14:11:12 +0000900#ifdef SQLITE_TEST
901/*
902** Count the number of fullsyncs and normal syncs. This is used to test
903** that syncs and fullsyncs are occuring at the right times.
904*/
905int sqlite3_sync_count = 0;
906int sqlite3_fullsync_count = 0;
907#endif
908
drhf2f23912005-10-05 10:29:36 +0000909/*
910** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.
911** Otherwise use fsync() in its place.
912*/
913#ifndef HAVE_FDATASYNC
914# define fdatasync fsync
915#endif
916
drhac530b12006-02-11 01:25:50 +0000917/*
918** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
919** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
920** only available on Mac OS X. But that could change.
921*/
922#ifdef F_FULLFSYNC
923# define HAVE_FULLFSYNC 1
924#else
925# define HAVE_FULLFSYNC 0
926#endif
927
drhb851b2c2005-03-10 14:11:12 +0000928
drhbbd42a62004-05-22 17:41:58 +0000929/*
drhdd809b02004-07-17 21:44:57 +0000930** The fsync() system call does not work as advertised on many
931** unix systems. The following procedure is an attempt to make
932** it work better.
drh1398ad32005-01-19 23:24:50 +0000933**
934** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
935** for testing when we want to run through the test suite quickly.
936** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
937** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
938** or power failure will likely corrupt the database file.
drhdd809b02004-07-17 21:44:57 +0000939*/
drheb796a72005-09-08 12:38:41 +0000940static int full_fsync(int fd, int fullSync, int dataOnly){
drhdd809b02004-07-17 21:44:57 +0000941 int rc;
drhb851b2c2005-03-10 14:11:12 +0000942
943 /* Record the number of times that we do a normal fsync() and
944 ** FULLSYNC. This is used during testing to verify that this procedure
945 ** gets called with the correct arguments.
946 */
947#ifdef SQLITE_TEST
948 if( fullSync ) sqlite3_fullsync_count++;
949 sqlite3_sync_count++;
950#endif
951
952 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
953 ** no-op
954 */
955#ifdef SQLITE_NO_SYNC
956 rc = SQLITE_OK;
957#else
958
drhac530b12006-02-11 01:25:50 +0000959#if HAVE_FULLFSYNC
drhb851b2c2005-03-10 14:11:12 +0000960 if( fullSync ){
drhf30cc942005-03-11 17:52:34 +0000961 rc = fcntl(fd, F_FULLFSYNC, 0);
aswiftae0943b2007-01-31 23:37:07 +0000962 }else{
963 rc = 1;
964 }
965 /* If the FULLFSYNC failed, fall back to attempting an fsync().
966 * It shouldn't be possible for fullfsync to fail on the local
967 * file system (on OSX), so failure indicates that FULLFSYNC
968 * isn't supported for this file system. So, attempt an fsync
969 * and (for now) ignore the overhead of a superfluous fcntl call.
970 * It'd be better to detect fullfsync support once and avoid
971 * the fcntl call every time sync is called.
972 */
973 if( rc ) rc = fsync(fd);
974
975#else
drheb796a72005-09-08 12:38:41 +0000976 if( dataOnly ){
977 rc = fdatasync(fd);
drhf2f23912005-10-05 10:29:36 +0000978 }else{
drheb796a72005-09-08 12:38:41 +0000979 rc = fsync(fd);
980 }
aswiftae0943b2007-01-31 23:37:07 +0000981#endif /* HAVE_FULLFSYNC */
drhb851b2c2005-03-10 14:11:12 +0000982#endif /* defined(SQLITE_NO_SYNC) */
983
drhdd809b02004-07-17 21:44:57 +0000984 return rc;
985}
986
987/*
drhbbd42a62004-05-22 17:41:58 +0000988** Make sure all writes to a particular file are committed to disk.
989**
drheb796a72005-09-08 12:38:41 +0000990** If dataOnly==0 then both the file itself and its metadata (file
991** size, access time, etc) are synced. If dataOnly!=0 then only the
992** file data is synced.
993**
drhbbd42a62004-05-22 17:41:58 +0000994** Under Unix, also make sure that the directory entry for the file
995** has been created by fsync-ing the directory that contains the file.
996** If we do not do this and we encounter a power failure, the directory
997** entry for the journal might not exist after we reboot. The next
998** SQLite to access the file will not know that the journal exists (because
999** the directory entry for the journal was never created) and the transaction
1000** will not roll back - possibly leading to database corruption.
1001*/
danielk197790949c22007-08-17 16:50:38 +00001002static int unixSync(sqlite3_file *id, int flags){
drh59685932006-09-14 13:47:11 +00001003 int rc;
drh054889e2005-11-30 03:20:31 +00001004 unixFile *pFile = (unixFile*)id;
danielk197790949c22007-08-17 16:50:38 +00001005
danielk1977f036aef2007-08-20 05:36:51 +00001006 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
1007 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
1008
danielk1977c16d4632007-08-30 14:49:58 +00001009 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
danielk1977f036aef2007-08-20 05:36:51 +00001010 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
1011 || (flags&0x0F)==SQLITE_SYNC_FULL
danielk1977f036aef2007-08-20 05:36:51 +00001012 );
danielk197790949c22007-08-17 16:50:38 +00001013
drh054889e2005-11-30 03:20:31 +00001014 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001015 OSTRACE2("SYNC %-3d\n", pFile->h);
danielk197790949c22007-08-17 16:50:38 +00001016 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
drh59685932006-09-14 13:47:11 +00001017 SimulateIOError( rc=1 );
1018 if( rc ){
drh4ac285a2006-09-15 07:28:50 +00001019 return SQLITE_IOERR_FSYNC;
drhbbd42a62004-05-22 17:41:58 +00001020 }
drh054889e2005-11-30 03:20:31 +00001021 if( pFile->dirfd>=0 ){
drh4f0c5872007-03-26 22:05:01 +00001022 OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
danielk197790949c22007-08-17 16:50:38 +00001023 HAVE_FULLFSYNC, isFullsync);
danielk1977d7c03f72005-11-25 10:38:22 +00001024#ifndef SQLITE_DISABLE_DIRSYNC
drhac530b12006-02-11 01:25:50 +00001025 /* The directory sync is only attempted if full_fsync is
1026 ** turned off or unavailable. If a full_fsync occurred above,
1027 ** then the directory sync is superfluous.
1028 */
danielk197790949c22007-08-17 16:50:38 +00001029 if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
drhac530b12006-02-11 01:25:50 +00001030 /*
1031 ** We have received multiple reports of fsync() returning
drh86631a52006-02-09 23:05:51 +00001032 ** errors when applied to directories on certain file systems.
1033 ** A failed directory sync is not a big deal. So it seems
1034 ** better to ignore the error. Ticket #1657
1035 */
1036 /* return SQLITE_IOERR; */
danielk19770964b232005-11-25 08:47:57 +00001037 }
danielk1977d7c03f72005-11-25 10:38:22 +00001038#endif
drh054889e2005-11-30 03:20:31 +00001039 close(pFile->dirfd); /* Only need to sync once, so close the directory */
1040 pFile->dirfd = -1; /* when we are done. */
drha2854222004-06-17 19:04:17 +00001041 }
drha2854222004-06-17 19:04:17 +00001042 return SQLITE_OK;
drhbbd42a62004-05-22 17:41:58 +00001043}
1044
1045/*
1046** Truncate an open file to a specified size
1047*/
danielk197762079062007-08-15 17:08:46 +00001048static int unixTruncate(sqlite3_file *id, i64 nByte){
drh59685932006-09-14 13:47:11 +00001049 int rc;
drh9cbe6352005-11-29 03:13:21 +00001050 assert( id );
drh93aed5a2008-01-16 17:46:38 +00001051 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
drh63fff5f2007-06-19 10:50:38 +00001052 rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);
drh59685932006-09-14 13:47:11 +00001053 if( rc ){
drh4ac285a2006-09-15 07:28:50 +00001054 return SQLITE_IOERR_TRUNCATE;
drh59685932006-09-14 13:47:11 +00001055 }else{
1056 return SQLITE_OK;
1057 }
drhbbd42a62004-05-22 17:41:58 +00001058}
1059
1060/*
1061** Determine the current size of a file in bytes
1062*/
danielk197762079062007-08-15 17:08:46 +00001063static int unixFileSize(sqlite3_file *id, i64 *pSize){
drh59685932006-09-14 13:47:11 +00001064 int rc;
drhbbd42a62004-05-22 17:41:58 +00001065 struct stat buf;
drh9cbe6352005-11-29 03:13:21 +00001066 assert( id );
drh59685932006-09-14 13:47:11 +00001067 rc = fstat(((unixFile*)id)->h, &buf);
1068 SimulateIOError( rc=1 );
1069 if( rc!=0 ){
drh4ac285a2006-09-15 07:28:50 +00001070 return SQLITE_IOERR_FSTAT;
drhbbd42a62004-05-22 17:41:58 +00001071 }
1072 *pSize = buf.st_size;
1073 return SQLITE_OK;
1074}
1075
danielk19779a1d0ab2004-06-01 14:09:28 +00001076/*
danielk197713adf8a2004-06-03 16:08:41 +00001077** This routine checks if there is a RESERVED lock held on the specified
1078** file by this or any other process. If such a lock is held, return
drh2ac3ee92004-06-07 16:27:46 +00001079** non-zero. If the file is unlocked or holds only SHARED locks, then
1080** return zero.
danielk197713adf8a2004-06-03 16:08:41 +00001081*/
danielk1977861f7452008-06-05 11:39:11 +00001082static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
danielk197713adf8a2004-06-03 16:08:41 +00001083 int r = 0;
drh054889e2005-11-30 03:20:31 +00001084 unixFile *pFile = (unixFile*)id;
danielk197713adf8a2004-06-03 16:08:41 +00001085
danielk1977861f7452008-06-05 11:39:11 +00001086 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
1087
drh054889e2005-11-30 03:20:31 +00001088 assert( pFile );
danielk1977b4b47412007-08-17 15:53:36 +00001089 enterMutex(); /* Because pFile->pLock is shared across threads */
danielk197713adf8a2004-06-03 16:08:41 +00001090
1091 /* Check if a thread in this process holds such a lock */
drh054889e2005-11-30 03:20:31 +00001092 if( pFile->pLock->locktype>SHARED_LOCK ){
danielk197713adf8a2004-06-03 16:08:41 +00001093 r = 1;
1094 }
1095
drh2ac3ee92004-06-07 16:27:46 +00001096 /* Otherwise see if some other process holds it.
danielk197713adf8a2004-06-03 16:08:41 +00001097 */
1098 if( !r ){
1099 struct flock lock;
1100 lock.l_whence = SEEK_SET;
drh2ac3ee92004-06-07 16:27:46 +00001101 lock.l_start = RESERVED_BYTE;
1102 lock.l_len = 1;
1103 lock.l_type = F_WRLCK;
drh054889e2005-11-30 03:20:31 +00001104 fcntl(pFile->h, F_GETLK, &lock);
danielk197713adf8a2004-06-03 16:08:41 +00001105 if( lock.l_type!=F_UNLCK ){
1106 r = 1;
1107 }
1108 }
1109
danielk1977b4b47412007-08-17 15:53:36 +00001110 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001111 OSTRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
danielk197713adf8a2004-06-03 16:08:41 +00001112
danielk1977861f7452008-06-05 11:39:11 +00001113 *pResOut = r;
1114 return SQLITE_OK;
danielk197713adf8a2004-06-03 16:08:41 +00001115}
1116
1117/*
danielk19779a1d0ab2004-06-01 14:09:28 +00001118** Lock the file with the lock specified by parameter locktype - one
1119** of the following:
1120**
drh2ac3ee92004-06-07 16:27:46 +00001121** (1) SHARED_LOCK
1122** (2) RESERVED_LOCK
1123** (3) PENDING_LOCK
1124** (4) EXCLUSIVE_LOCK
1125**
drhb3e04342004-06-08 00:47:47 +00001126** Sometimes when requesting one lock state, additional lock states
1127** are inserted in between. The locking might fail on one of the later
1128** transitions leaving the lock state different from what it started but
1129** still short of its goal. The following chart shows the allowed
1130** transitions and the inserted intermediate states:
1131**
1132** UNLOCKED -> SHARED
1133** SHARED -> RESERVED
1134** SHARED -> (PENDING) -> EXCLUSIVE
1135** RESERVED -> (PENDING) -> EXCLUSIVE
1136** PENDING -> EXCLUSIVE
drh2ac3ee92004-06-07 16:27:46 +00001137**
drha6abd042004-06-09 17:37:22 +00001138** This routine will only increase a lock. Use the sqlite3OsUnlock()
1139** routine to lower a locking level.
danielk19779a1d0ab2004-06-01 14:09:28 +00001140*/
danielk197762079062007-08-15 17:08:46 +00001141static int unixLock(sqlite3_file *id, int locktype){
danielk1977f42f25c2004-06-25 07:21:28 +00001142 /* The following describes the implementation of the various locks and
1143 ** lock transitions in terms of the POSIX advisory shared and exclusive
1144 ** lock primitives (called read-locks and write-locks below, to avoid
1145 ** confusion with SQLite lock names). The algorithms are complicated
1146 ** slightly in order to be compatible with windows systems simultaneously
1147 ** accessing the same database file, in case that is ever required.
1148 **
1149 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
1150 ** byte', each single bytes at well known offsets, and the 'shared byte
1151 ** range', a range of 510 bytes at a well known offset.
1152 **
1153 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
1154 ** byte'. If this is successful, a random byte from the 'shared byte
1155 ** range' is read-locked and the lock on the 'pending byte' released.
1156 **
danielk197790ba3bd2004-06-25 08:32:25 +00001157 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
1158 ** A RESERVED lock is implemented by grabbing a write-lock on the
1159 ** 'reserved byte'.
danielk1977f42f25c2004-06-25 07:21:28 +00001160 **
1161 ** A process may only obtain a PENDING lock after it has obtained a
danielk197790ba3bd2004-06-25 08:32:25 +00001162 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
1163 ** on the 'pending byte'. This ensures that no new SHARED locks can be
1164 ** obtained, but existing SHARED locks are allowed to persist. A process
1165 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
1166 ** This property is used by the algorithm for rolling back a journal file
1167 ** after a crash.
danielk1977f42f25c2004-06-25 07:21:28 +00001168 **
danielk197790ba3bd2004-06-25 08:32:25 +00001169 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
1170 ** implemented by obtaining a write-lock on the entire 'shared byte
1171 ** range'. Since all other locks require a read-lock on one of the bytes
1172 ** within this range, this ensures that no other locks are held on the
1173 ** database.
danielk1977f42f25c2004-06-25 07:21:28 +00001174 **
1175 ** The reason a single byte cannot be used instead of the 'shared byte
1176 ** range' is that some versions of windows do not support read-locks. By
1177 ** locking a random byte from a range, concurrent SHARED locks may exist
1178 ** even if the locking primitive used is always a write-lock.
1179 */
danielk19779a1d0ab2004-06-01 14:09:28 +00001180 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001181 unixFile *pFile = (unixFile*)id;
1182 struct lockInfo *pLock = pFile->pLock;
danielk19779a1d0ab2004-06-01 14:09:28 +00001183 struct flock lock;
1184 int s;
1185
drh054889e2005-11-30 03:20:31 +00001186 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001187 OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001188 locktypeName(locktype), locktypeName(pFile->locktype),
1189 locktypeName(pLock->locktype), pLock->cnt , getpid());
danielk19779a1d0ab2004-06-01 14:09:28 +00001190
1191 /* If there is already a lock of this type or more restrictive on the
danielk1977ad94b582007-08-20 06:44:22 +00001192 ** unixFile, do nothing. Don't use the end_lock: exit path, as
danielk1977b4b47412007-08-17 15:53:36 +00001193 ** enterMutex() hasn't been called yet.
danielk19779a1d0ab2004-06-01 14:09:28 +00001194 */
drh054889e2005-11-30 03:20:31 +00001195 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00001196 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001197 locktypeName(locktype));
danielk19779a1d0ab2004-06-01 14:09:28 +00001198 return SQLITE_OK;
1199 }
1200
drhb3e04342004-06-08 00:47:47 +00001201 /* Make sure the locking sequence is correct
drh2ac3ee92004-06-07 16:27:46 +00001202 */
drh054889e2005-11-30 03:20:31 +00001203 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
drhb3e04342004-06-08 00:47:47 +00001204 assert( locktype!=PENDING_LOCK );
drh054889e2005-11-30 03:20:31 +00001205 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
drh2ac3ee92004-06-07 16:27:46 +00001206
drh054889e2005-11-30 03:20:31 +00001207 /* This mutex is needed because pFile->pLock is shared across threads
drhb3e04342004-06-08 00:47:47 +00001208 */
danielk1977b4b47412007-08-17 15:53:36 +00001209 enterMutex();
danielk19779a1d0ab2004-06-01 14:09:28 +00001210
drh029b44b2006-01-15 00:13:15 +00001211 /* Make sure the current thread owns the pFile.
1212 */
1213 rc = transferOwnership(pFile);
1214 if( rc!=SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00001215 leaveMutex();
drh029b44b2006-01-15 00:13:15 +00001216 return rc;
1217 }
drh64b1bea2006-01-15 02:30:57 +00001218 pLock = pFile->pLock;
drh029b44b2006-01-15 00:13:15 +00001219
danielk1977ad94b582007-08-20 06:44:22 +00001220 /* If some thread using this PID has a lock via a different unixFile*
danielk19779a1d0ab2004-06-01 14:09:28 +00001221 ** handle that precludes the requested lock, return BUSY.
1222 */
drh054889e2005-11-30 03:20:31 +00001223 if( (pFile->locktype!=pLock->locktype &&
drh2ac3ee92004-06-07 16:27:46 +00001224 (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
danielk19779a1d0ab2004-06-01 14:09:28 +00001225 ){
1226 rc = SQLITE_BUSY;
1227 goto end_lock;
1228 }
1229
1230 /* If a SHARED lock is requested, and some thread using this PID already
1231 ** has a SHARED or RESERVED lock, then increment reference counts and
1232 ** return SQLITE_OK.
1233 */
1234 if( locktype==SHARED_LOCK &&
1235 (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
1236 assert( locktype==SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001237 assert( pFile->locktype==0 );
danielk1977ecb2a962004-06-02 06:30:16 +00001238 assert( pLock->cnt>0 );
drh054889e2005-11-30 03:20:31 +00001239 pFile->locktype = SHARED_LOCK;
danielk19779a1d0ab2004-06-01 14:09:28 +00001240 pLock->cnt++;
drh054889e2005-11-30 03:20:31 +00001241 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001242 goto end_lock;
1243 }
1244
danielk197713adf8a2004-06-03 16:08:41 +00001245 lock.l_len = 1L;
drh2b4b5962005-06-15 17:47:55 +00001246
danielk19779a1d0ab2004-06-01 14:09:28 +00001247 lock.l_whence = SEEK_SET;
1248
drh3cde3bb2004-06-12 02:17:14 +00001249 /* A PENDING lock is needed before acquiring a SHARED lock and before
1250 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1251 ** be released.
danielk19779a1d0ab2004-06-01 14:09:28 +00001252 */
drh3cde3bb2004-06-12 02:17:14 +00001253 if( locktype==SHARED_LOCK
drh054889e2005-11-30 03:20:31 +00001254 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh3cde3bb2004-06-12 02:17:14 +00001255 ){
danielk1977489468c2004-06-28 08:25:47 +00001256 lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
drh2ac3ee92004-06-07 16:27:46 +00001257 lock.l_start = PENDING_BYTE;
drh054889e2005-11-30 03:20:31 +00001258 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001259 if( s==(-1) ){
danielk19779a1d0ab2004-06-01 14:09:28 +00001260 rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
1261 goto end_lock;
1262 }
drh3cde3bb2004-06-12 02:17:14 +00001263 }
1264
1265
1266 /* If control gets to this point, then actually go ahead and make
1267 ** operating system calls for the specified lock.
1268 */
1269 if( locktype==SHARED_LOCK ){
1270 assert( pLock->cnt==0 );
1271 assert( pLock->locktype==0 );
danielk19779a1d0ab2004-06-01 14:09:28 +00001272
drh2ac3ee92004-06-07 16:27:46 +00001273 /* Now get the read-lock */
1274 lock.l_start = SHARED_FIRST;
1275 lock.l_len = SHARED_SIZE;
drh054889e2005-11-30 03:20:31 +00001276 s = fcntl(pFile->h, F_SETLK, &lock);
drh2ac3ee92004-06-07 16:27:46 +00001277
1278 /* Drop the temporary PENDING lock */
1279 lock.l_start = PENDING_BYTE;
1280 lock.l_len = 1L;
danielk19779a1d0ab2004-06-01 14:09:28 +00001281 lock.l_type = F_UNLCK;
drh054889e2005-11-30 03:20:31 +00001282 if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
drh4ac285a2006-09-15 07:28:50 +00001283 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
drh2b4b5962005-06-15 17:47:55 +00001284 goto end_lock;
1285 }
drhe2396a12007-03-29 20:19:58 +00001286 if( s==(-1) ){
drhbbd42a62004-05-22 17:41:58 +00001287 rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
1288 }else{
drh054889e2005-11-30 03:20:31 +00001289 pFile->locktype = SHARED_LOCK;
1290 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001291 pLock->cnt = 1;
drhbbd42a62004-05-22 17:41:58 +00001292 }
drh3cde3bb2004-06-12 02:17:14 +00001293 }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
1294 /* We are trying for an exclusive lock but another thread in this
1295 ** same process is still holding a shared lock. */
1296 rc = SQLITE_BUSY;
drhbbd42a62004-05-22 17:41:58 +00001297 }else{
drh3cde3bb2004-06-12 02:17:14 +00001298 /* The request was for a RESERVED or EXCLUSIVE lock. It is
danielk19779a1d0ab2004-06-01 14:09:28 +00001299 ** assumed that there is a SHARED or greater lock on the file
1300 ** already.
1301 */
drh054889e2005-11-30 03:20:31 +00001302 assert( 0!=pFile->locktype );
danielk19779a1d0ab2004-06-01 14:09:28 +00001303 lock.l_type = F_WRLCK;
1304 switch( locktype ){
1305 case RESERVED_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001306 lock.l_start = RESERVED_BYTE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001307 break;
danielk19779a1d0ab2004-06-01 14:09:28 +00001308 case EXCLUSIVE_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001309 lock.l_start = SHARED_FIRST;
1310 lock.l_len = SHARED_SIZE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001311 break;
1312 default:
1313 assert(0);
1314 }
drh054889e2005-11-30 03:20:31 +00001315 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001316 if( s==(-1) ){
danielk19779a1d0ab2004-06-01 14:09:28 +00001317 rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
1318 }
drhbbd42a62004-05-22 17:41:58 +00001319 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001320
danielk1977ecb2a962004-06-02 06:30:16 +00001321 if( rc==SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001322 pFile->locktype = locktype;
danielk1977ecb2a962004-06-02 06:30:16 +00001323 pLock->locktype = locktype;
drh3cde3bb2004-06-12 02:17:14 +00001324 }else if( locktype==EXCLUSIVE_LOCK ){
drh054889e2005-11-30 03:20:31 +00001325 pFile->locktype = PENDING_LOCK;
drh3cde3bb2004-06-12 02:17:14 +00001326 pLock->locktype = PENDING_LOCK;
danielk1977ecb2a962004-06-02 06:30:16 +00001327 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001328
1329end_lock:
danielk1977b4b47412007-08-17 15:53:36 +00001330 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001331 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
danielk19772b444852004-06-29 07:45:33 +00001332 rc==SQLITE_OK ? "ok" : "failed");
drhbbd42a62004-05-22 17:41:58 +00001333 return rc;
1334}
1335
1336/*
drh054889e2005-11-30 03:20:31 +00001337** Lower the locking level on file descriptor pFile to locktype. locktype
drha6abd042004-06-09 17:37:22 +00001338** must be either NO_LOCK or SHARED_LOCK.
1339**
1340** If the locking level of the file descriptor is already at or below
1341** the requested locking level, this routine is a no-op.
drhbbd42a62004-05-22 17:41:58 +00001342*/
danielk197762079062007-08-15 17:08:46 +00001343static int unixUnlock(sqlite3_file *id, int locktype){
drha6abd042004-06-09 17:37:22 +00001344 struct lockInfo *pLock;
1345 struct flock lock;
drh9c105bb2004-10-02 20:38:28 +00001346 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001347 unixFile *pFile = (unixFile*)id;
drh1aa5af12008-03-07 19:51:14 +00001348 int h;
drha6abd042004-06-09 17:37:22 +00001349
drh054889e2005-11-30 03:20:31 +00001350 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001351 OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
drh054889e2005-11-30 03:20:31 +00001352 pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
drha6abd042004-06-09 17:37:22 +00001353
1354 assert( locktype<=SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001355 if( pFile->locktype<=locktype ){
drha6abd042004-06-09 17:37:22 +00001356 return SQLITE_OK;
1357 }
drhf1a221e2006-01-15 17:27:17 +00001358 if( CHECK_THREADID(pFile) ){
1359 return SQLITE_MISUSE;
1360 }
danielk1977b4b47412007-08-17 15:53:36 +00001361 enterMutex();
drh1aa5af12008-03-07 19:51:14 +00001362 h = pFile->h;
drh054889e2005-11-30 03:20:31 +00001363 pLock = pFile->pLock;
drha6abd042004-06-09 17:37:22 +00001364 assert( pLock->cnt!=0 );
drh054889e2005-11-30 03:20:31 +00001365 if( pFile->locktype>SHARED_LOCK ){
1366 assert( pLock->locktype==pFile->locktype );
drh1aa5af12008-03-07 19:51:14 +00001367 SimulateIOErrorBenign(1);
1368 SimulateIOError( h=(-1) )
1369 SimulateIOErrorBenign(0);
drh9c105bb2004-10-02 20:38:28 +00001370 if( locktype==SHARED_LOCK ){
1371 lock.l_type = F_RDLCK;
1372 lock.l_whence = SEEK_SET;
1373 lock.l_start = SHARED_FIRST;
1374 lock.l_len = SHARED_SIZE;
drh1aa5af12008-03-07 19:51:14 +00001375 if( fcntl(h, F_SETLK, &lock)==(-1) ){
drh4ac285a2006-09-15 07:28:50 +00001376 rc = SQLITE_IOERR_RDLOCK;
drh9c105bb2004-10-02 20:38:28 +00001377 }
1378 }
drhbbd42a62004-05-22 17:41:58 +00001379 lock.l_type = F_UNLCK;
1380 lock.l_whence = SEEK_SET;
drha6abd042004-06-09 17:37:22 +00001381 lock.l_start = PENDING_BYTE;
1382 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
drh1aa5af12008-03-07 19:51:14 +00001383 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001384 pLock->locktype = SHARED_LOCK;
1385 }else{
drh1aa5af12008-03-07 19:51:14 +00001386 rc = SQLITE_IOERR_UNLOCK;
drh2b4b5962005-06-15 17:47:55 +00001387 }
drhbbd42a62004-05-22 17:41:58 +00001388 }
drha6abd042004-06-09 17:37:22 +00001389 if( locktype==NO_LOCK ){
1390 struct openCnt *pOpen;
danielk1977ecb2a962004-06-02 06:30:16 +00001391
drha6abd042004-06-09 17:37:22 +00001392 /* Decrement the shared lock counter. Release the lock using an
1393 ** OS call only when all threads in this same process have released
1394 ** the lock.
1395 */
1396 pLock->cnt--;
1397 if( pLock->cnt==0 ){
1398 lock.l_type = F_UNLCK;
1399 lock.l_whence = SEEK_SET;
1400 lock.l_start = lock.l_len = 0L;
drh1aa5af12008-03-07 19:51:14 +00001401 SimulateIOErrorBenign(1);
1402 SimulateIOError( h=(-1) )
1403 SimulateIOErrorBenign(0);
1404 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001405 pLock->locktype = NO_LOCK;
1406 }else{
drh1aa5af12008-03-07 19:51:14 +00001407 rc = SQLITE_IOERR_UNLOCK;
1408 pLock->cnt = 1;
drh2b4b5962005-06-15 17:47:55 +00001409 }
drha6abd042004-06-09 17:37:22 +00001410 }
1411
drhbbd42a62004-05-22 17:41:58 +00001412 /* Decrement the count of locks against this same file. When the
1413 ** count reaches zero, close any other file descriptors whose close
1414 ** was deferred because of outstanding locks.
1415 */
drh1aa5af12008-03-07 19:51:14 +00001416 if( rc==SQLITE_OK ){
1417 pOpen = pFile->pOpen;
1418 pOpen->nLock--;
1419 assert( pOpen->nLock>=0 );
1420 if( pOpen->nLock==0 && pOpen->nPending>0 ){
1421 int i;
1422 for(i=0; i<pOpen->nPending; i++){
1423 close(pOpen->aPending[i]);
1424 }
1425 free(pOpen->aPending);
1426 pOpen->nPending = 0;
1427 pOpen->aPending = 0;
drhbbd42a62004-05-22 17:41:58 +00001428 }
drhbbd42a62004-05-22 17:41:58 +00001429 }
1430 }
danielk1977b4b47412007-08-17 15:53:36 +00001431 leaveMutex();
drh1aa5af12008-03-07 19:51:14 +00001432 if( rc==SQLITE_OK ) pFile->locktype = locktype;
drh9c105bb2004-10-02 20:38:28 +00001433 return rc;
drhbbd42a62004-05-22 17:41:58 +00001434}
1435
1436/*
danielk1977e339d652008-06-28 11:23:00 +00001437** This function performs the parts of the "close file" operation
1438** common to all locking schemes. It closes the directory and file
1439** handles, if they are valid, and sets all fields of the unixFile
1440** structure to 0.
1441*/
1442static int closeUnixFile(sqlite3_file *id){
1443 unixFile *pFile = (unixFile*)id;
1444 if( pFile ){
1445 if( pFile->dirfd>=0 ){
1446 close(pFile->dirfd);
1447 }
1448 if( pFile->h>=0 ){
1449 close(pFile->h);
1450 }
1451 OSTRACE2("CLOSE %-3d\n", pFile->h);
1452 OpenCounter(-1);
1453 memset(pFile, 0, sizeof(unixFile));
1454 }
1455 return SQLITE_OK;
1456}
1457
1458/*
danielk1977e3026632004-06-22 11:29:02 +00001459** Close a file.
1460*/
danielk197762079062007-08-15 17:08:46 +00001461static int unixClose(sqlite3_file *id){
danielk1977e339d652008-06-28 11:23:00 +00001462 if( id ){
1463 unixFile *pFile = (unixFile *)id;
1464 unixUnlock(id, NO_LOCK);
1465 enterMutex();
1466 if( pFile->pOpen->nLock ){
1467 /* If there are outstanding locks, do not actually close the file just
1468 ** yet because that would clear those locks. Instead, add the file
1469 ** descriptor to pOpen->aPending. It will be automatically closed when
1470 ** the last lock is cleared.
1471 */
1472 int *aNew;
1473 struct openCnt *pOpen = pFile->pOpen;
1474 aNew = realloc( pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
1475 if( aNew==0 ){
1476 /* If a malloc fails, just leak the file descriptor */
1477 }else{
1478 pOpen->aPending = aNew;
1479 pOpen->aPending[pOpen->nPending] = pFile->h;
1480 pOpen->nPending++;
1481 pFile->h = -1;
1482 }
danielk1977e3026632004-06-22 11:29:02 +00001483 }
danielk1977e339d652008-06-28 11:23:00 +00001484 releaseLockInfo(pFile->pLock);
1485 releaseOpenCnt(pFile->pOpen);
1486 closeUnixFile(id);
1487 leaveMutex();
danielk1977e3026632004-06-22 11:29:02 +00001488 }
drh02afc862006-01-20 18:10:57 +00001489 return SQLITE_OK;
danielk1977e3026632004-06-22 11:29:02 +00001490}
1491
drhbfe66312006-10-03 17:40:40 +00001492
1493#ifdef SQLITE_ENABLE_LOCKING_STYLE
1494#pragma mark AFP Support
1495
1496/*
1497 ** The afpLockingContext structure contains all afp lock specific state
1498 */
1499typedef struct afpLockingContext afpLockingContext;
1500struct afpLockingContext {
drh1aa5af12008-03-07 19:51:14 +00001501 unsigned long long sharedLockByte;
drh308aa322008-03-07 20:14:38 +00001502 const char *filePath;
drhbfe66312006-10-03 17:40:40 +00001503};
1504
1505struct ByteRangeLockPB2
1506{
1507 unsigned long long offset; /* offset to first byte to lock */
1508 unsigned long long length; /* nbr of bytes to lock */
1509 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
1510 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
1511 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
1512 int fd; /* file desc to assoc this lock with */
1513};
1514
drhfd131da2007-08-07 17:13:03 +00001515#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
drhbfe66312006-10-03 17:40:40 +00001516
danielk1977ad94b582007-08-20 06:44:22 +00001517/*
1518** Return 0 on success, 1 on failure. To match the behavior of the
1519** normal posix file locking (used in unixLock for example), we should
1520** provide 'richer' return codes - specifically to differentiate between
1521** 'file busy' and 'file system error' results.
1522*/
1523static int _AFPFSSetLock(
1524 const char *path,
1525 int fd,
1526 unsigned long long offset,
1527 unsigned long long length,
1528 int setLockFlag
1529){
drhfd131da2007-08-07 17:13:03 +00001530 struct ByteRangeLockPB2 pb;
drhbfe66312006-10-03 17:40:40 +00001531 int err;
1532
1533 pb.unLockFlag = setLockFlag ? 0 : 1;
1534 pb.startEndFlag = 0;
1535 pb.offset = offset;
1536 pb.length = length;
1537 pb.fd = fd;
drh4f0c5872007-03-26 22:05:01 +00001538 OSTRACE5("AFPLOCK setting lock %s for %d in range %llx:%llx\n",
drhbfe66312006-10-03 17:40:40 +00001539 (setLockFlag?"ON":"OFF"), fd, offset, length);
1540 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
1541 if ( err==-1 ) {
drh4f0c5872007-03-26 22:05:01 +00001542 OSTRACE4("AFPLOCK failed to fsctl() '%s' %d %s\n", path, errno,
drhbfe66312006-10-03 17:40:40 +00001543 strerror(errno));
drh3b62b2f2007-06-08 18:27:03 +00001544 return 1; /* error */
drhbfe66312006-10-03 17:40:40 +00001545 } else {
1546 return 0;
1547 }
1548}
1549
1550/*
1551 ** This routine checks if there is a RESERVED lock held on the specified
1552 ** file by this or any other process. If such a lock is held, return
1553 ** non-zero. If the file is unlocked or holds only SHARED locks, then
1554 ** return zero.
1555 */
danielk1977e339d652008-06-28 11:23:00 +00001556static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
drhbfe66312006-10-03 17:40:40 +00001557 int r = 0;
1558 unixFile *pFile = (unixFile*)id;
1559
1560 assert( pFile );
1561 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
1562
1563 /* Check if a thread in this process holds such a lock */
1564 if( pFile->locktype>SHARED_LOCK ){
1565 r = 1;
1566 }
1567
1568 /* Otherwise see if some other process holds it.
1569 */
1570 if ( !r ) {
drh3b62b2f2007-06-08 18:27:03 +00001571 /* lock the byte */
drhbfe66312006-10-03 17:40:40 +00001572 int failed = _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1,1);
1573 if (failed) {
1574 /* if we failed to get the lock then someone else must have it */
1575 r = 1;
1576 } else {
1577 /* if we succeeded in taking the reserved lock, unlock it to restore
1578 ** the original state */
1579 _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1, 0);
1580 }
1581 }
drh4f0c5872007-03-26 22:05:01 +00001582 OSTRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
drhbfe66312006-10-03 17:40:40 +00001583
danielk1977861f7452008-06-05 11:39:11 +00001584 *pResOut = r;
1585 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00001586}
1587
1588/* AFP-style locking following the behavior of unixLock, see the unixLock
1589** function comments for details of lock management. */
danielk1977e339d652008-06-28 11:23:00 +00001590static int afpLock(sqlite3_file *id, int locktype){
drhbfe66312006-10-03 17:40:40 +00001591 int rc = SQLITE_OK;
1592 unixFile *pFile = (unixFile*)id;
1593 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00001594
1595 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001596 OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h,
drh339eb0b2008-03-07 15:34:11 +00001597 locktypeName(locktype), locktypeName(pFile->locktype), getpid());
1598
drhbfe66312006-10-03 17:40:40 +00001599 /* If there is already a lock of this type or more restrictive on the
drh339eb0b2008-03-07 15:34:11 +00001600 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
1601 ** enterMutex() hasn't been called yet.
1602 */
drhbfe66312006-10-03 17:40:40 +00001603 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00001604 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00001605 locktypeName(locktype));
1606 return SQLITE_OK;
1607 }
1608
1609 /* Make sure the locking sequence is correct
drh339eb0b2008-03-07 15:34:11 +00001610 */
drhbfe66312006-10-03 17:40:40 +00001611 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
1612 assert( locktype!=PENDING_LOCK );
1613 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
1614
1615 /* This mutex is needed because pFile->pLock is shared across threads
drh339eb0b2008-03-07 15:34:11 +00001616 */
danielk1977b4b47412007-08-17 15:53:36 +00001617 enterMutex();
drhbfe66312006-10-03 17:40:40 +00001618
1619 /* Make sure the current thread owns the pFile.
drh339eb0b2008-03-07 15:34:11 +00001620 */
drhbfe66312006-10-03 17:40:40 +00001621 rc = transferOwnership(pFile);
1622 if( rc!=SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00001623 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00001624 return rc;
1625 }
1626
1627 /* A PENDING lock is needed before acquiring a SHARED lock and before
drh339eb0b2008-03-07 15:34:11 +00001628 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1629 ** be released.
1630 */
drhbfe66312006-10-03 17:40:40 +00001631 if( locktype==SHARED_LOCK
1632 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh339eb0b2008-03-07 15:34:11 +00001633 ){
1634 int failed;
1635 failed = _AFPFSSetLock(context->filePath, pFile->h, PENDING_BYTE, 1, 1);
drhbfe66312006-10-03 17:40:40 +00001636 if (failed) {
1637 rc = SQLITE_BUSY;
1638 goto afp_end_lock;
1639 }
1640 }
1641
1642 /* If control gets to this point, then actually go ahead and make
drh339eb0b2008-03-07 15:34:11 +00001643 ** operating system calls for the specified lock.
1644 */
drhbfe66312006-10-03 17:40:40 +00001645 if( locktype==SHARED_LOCK ){
1646 int lk, failed;
drhbfe66312006-10-03 17:40:40 +00001647
1648 /* Now get the read-lock */
1649 /* note that the quality of the randomness doesn't matter that much */
1650 lk = random();
1651 context->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
1652 failed = _AFPFSSetLock(context->filePath, pFile->h,
1653 SHARED_FIRST+context->sharedLockByte, 1, 1);
1654
1655 /* Drop the temporary PENDING lock */
1656 if (_AFPFSSetLock(context->filePath, pFile->h, PENDING_BYTE, 1, 0)) {
1657 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1658 goto afp_end_lock;
1659 }
1660
1661 if( failed ){
1662 rc = SQLITE_BUSY;
1663 } else {
1664 pFile->locktype = SHARED_LOCK;
1665 }
1666 }else{
1667 /* The request was for a RESERVED or EXCLUSIVE lock. It is
1668 ** assumed that there is a SHARED or greater lock on the file
1669 ** already.
1670 */
1671 int failed = 0;
1672 assert( 0!=pFile->locktype );
1673 if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
1674 /* Acquire a RESERVED lock */
1675 failed = _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1,1);
1676 }
1677 if (!failed && locktype == EXCLUSIVE_LOCK) {
1678 /* Acquire an EXCLUSIVE lock */
1679
1680 /* Remove the shared lock before trying the range. we'll need to
danielk1977e339d652008-06-28 11:23:00 +00001681 ** reestablish the shared lock if we can't get the afpUnlock
drhbfe66312006-10-03 17:40:40 +00001682 */
1683 if (!_AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST +
1684 context->sharedLockByte, 1, 0)) {
1685 /* now attemmpt to get the exclusive lock range */
1686 failed = _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST,
1687 SHARED_SIZE, 1);
1688 if (failed && _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST +
1689 context->sharedLockByte, 1, 1)) {
1690 rc = SQLITE_IOERR_RDLOCK; /* this should never happen */
1691 }
1692 } else {
1693 /* */
1694 rc = SQLITE_IOERR_UNLOCK; /* this should never happen */
1695 }
1696 }
1697 if( failed && rc == SQLITE_OK){
1698 rc = SQLITE_BUSY;
1699 }
1700 }
1701
1702 if( rc==SQLITE_OK ){
1703 pFile->locktype = locktype;
1704 }else if( locktype==EXCLUSIVE_LOCK ){
1705 pFile->locktype = PENDING_LOCK;
1706 }
1707
1708afp_end_lock:
drh339eb0b2008-03-07 15:34:11 +00001709 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001710 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
drhbfe66312006-10-03 17:40:40 +00001711 rc==SQLITE_OK ? "ok" : "failed");
1712 return rc;
1713}
1714
1715/*
drh339eb0b2008-03-07 15:34:11 +00001716** Lower the locking level on file descriptor pFile to locktype. locktype
1717** must be either NO_LOCK or SHARED_LOCK.
1718**
1719** If the locking level of the file descriptor is already at or below
1720** the requested locking level, this routine is a no-op.
1721*/
danielk1977e339d652008-06-28 11:23:00 +00001722static int afpUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001723 int rc = SQLITE_OK;
1724 unixFile *pFile = (unixFile*)id;
1725 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
1726
1727 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001728 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
drhbfe66312006-10-03 17:40:40 +00001729 pFile->locktype, getpid());
1730
1731 assert( locktype<=SHARED_LOCK );
1732 if( pFile->locktype<=locktype ){
1733 return SQLITE_OK;
1734 }
1735 if( CHECK_THREADID(pFile) ){
1736 return SQLITE_MISUSE;
1737 }
danielk1977b4b47412007-08-17 15:53:36 +00001738 enterMutex();
drhbfe66312006-10-03 17:40:40 +00001739 if( pFile->locktype>SHARED_LOCK ){
1740 if( locktype==SHARED_LOCK ){
1741 int failed = 0;
1742
1743 /* unlock the exclusive range - then re-establish the shared lock */
1744 if (pFile->locktype==EXCLUSIVE_LOCK) {
1745 failed = _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST,
1746 SHARED_SIZE, 0);
1747 if (!failed) {
1748 /* successfully removed the exclusive lock */
1749 if (_AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST+
1750 context->sharedLockByte, 1, 1)) {
1751 /* failed to re-establish our shared lock */
1752 rc = SQLITE_IOERR_RDLOCK; /* This should never happen */
1753 }
1754 } else {
1755 /* This should never happen - failed to unlock the exclusive range */
1756 rc = SQLITE_IOERR_UNLOCK;
1757 }
1758 }
1759 }
1760 if (rc == SQLITE_OK && pFile->locktype>=PENDING_LOCK) {
1761 if (_AFPFSSetLock(context->filePath, pFile->h, PENDING_BYTE, 1, 0)){
1762 /* failed to release the pending lock */
1763 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1764 }
1765 }
1766 if (rc == SQLITE_OK && pFile->locktype>=RESERVED_LOCK) {
1767 if (_AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1, 0)) {
1768 /* failed to release the reserved lock */
1769 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1770 }
1771 }
1772 }
1773 if( locktype==NO_LOCK ){
1774 int failed = _AFPFSSetLock(context->filePath, pFile->h,
1775 SHARED_FIRST + context->sharedLockByte, 1, 0);
1776 if (failed) {
1777 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1778 }
1779 }
1780 if (rc == SQLITE_OK)
1781 pFile->locktype = locktype;
danielk1977b4b47412007-08-17 15:53:36 +00001782 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00001783 return rc;
1784}
1785
1786/*
drh339eb0b2008-03-07 15:34:11 +00001787** Close a file & cleanup AFP specific locking context
1788*/
danielk1977e339d652008-06-28 11:23:00 +00001789static int afpClose(sqlite3_file *id) {
1790 if( id ){
1791 unixFile *pFile = (unixFile*)id;
1792 afpUnlock(id, NO_LOCK);
1793 sqlite3_free(pFile->lockingContext);
1794 }
1795 return closeUnixFile(id);
drhbfe66312006-10-03 17:40:40 +00001796}
1797
1798
1799#pragma mark flock() style locking
1800
1801/*
drh339eb0b2008-03-07 15:34:11 +00001802** The flockLockingContext is not used
1803*/
drhbfe66312006-10-03 17:40:40 +00001804typedef void flockLockingContext;
1805
danielk1977e339d652008-06-28 11:23:00 +00001806static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
danielk1977861f7452008-06-05 11:39:11 +00001807 int r = 1;
drhbfe66312006-10-03 17:40:40 +00001808 unixFile *pFile = (unixFile*)id;
1809
danielk1977861f7452008-06-05 11:39:11 +00001810 if (pFile->locktype != RESERVED_LOCK) {
drh3b62b2f2007-06-08 18:27:03 +00001811 /* attempt to get the lock */
drhbfe66312006-10-03 17:40:40 +00001812 int rc = flock(pFile->h, LOCK_EX | LOCK_NB);
1813 if (!rc) {
drh3b62b2f2007-06-08 18:27:03 +00001814 /* got the lock, unlock it */
drhbfe66312006-10-03 17:40:40 +00001815 flock(pFile->h, LOCK_UN);
danielk1977861f7452008-06-05 11:39:11 +00001816 r = 0; /* no one has it reserved */
drhbfe66312006-10-03 17:40:40 +00001817 }
drhbfe66312006-10-03 17:40:40 +00001818 }
danielk1977861f7452008-06-05 11:39:11 +00001819
1820 *pResOut = r;
1821 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00001822}
1823
danielk1977e339d652008-06-28 11:23:00 +00001824static int flockLock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001825 unixFile *pFile = (unixFile*)id;
1826
drh3b62b2f2007-06-08 18:27:03 +00001827 /* if we already have a lock, it is exclusive.
1828 ** Just adjust level and punt on outta here. */
drhbfe66312006-10-03 17:40:40 +00001829 if (pFile->locktype > NO_LOCK) {
1830 pFile->locktype = locktype;
1831 return SQLITE_OK;
1832 }
1833
drh3b62b2f2007-06-08 18:27:03 +00001834 /* grab an exclusive lock */
drhbfe66312006-10-03 17:40:40 +00001835 int rc = flock(pFile->h, LOCK_EX | LOCK_NB);
1836 if (rc) {
drh3b62b2f2007-06-08 18:27:03 +00001837 /* didn't get, must be busy */
drhbfe66312006-10-03 17:40:40 +00001838 return SQLITE_BUSY;
1839 } else {
drh3b62b2f2007-06-08 18:27:03 +00001840 /* got it, set the type and return ok */
drhbfe66312006-10-03 17:40:40 +00001841 pFile->locktype = locktype;
1842 return SQLITE_OK;
1843 }
1844}
1845
danielk1977e339d652008-06-28 11:23:00 +00001846static int flockUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001847 unixFile *pFile = (unixFile*)id;
1848
1849 assert( locktype<=SHARED_LOCK );
1850
drh3b62b2f2007-06-08 18:27:03 +00001851 /* no-op if possible */
drhbfe66312006-10-03 17:40:40 +00001852 if( pFile->locktype==locktype ){
1853 return SQLITE_OK;
1854 }
1855
drh3b62b2f2007-06-08 18:27:03 +00001856 /* shared can just be set because we always have an exclusive */
drhbfe66312006-10-03 17:40:40 +00001857 if (locktype==SHARED_LOCK) {
1858 pFile->locktype = locktype;
1859 return SQLITE_OK;
1860 }
1861
drh3b62b2f2007-06-08 18:27:03 +00001862 /* no, really, unlock. */
drhbfe66312006-10-03 17:40:40 +00001863 int rc = flock(pFile->h, LOCK_UN);
1864 if (rc)
1865 return SQLITE_IOERR_UNLOCK;
1866 else {
1867 pFile->locktype = NO_LOCK;
1868 return SQLITE_OK;
1869 }
1870}
1871
1872/*
drh339eb0b2008-03-07 15:34:11 +00001873** Close a file.
1874*/
danielk1977e339d652008-06-28 11:23:00 +00001875static int flockClose(sqlite3_file *id) {
1876 if( id ){
1877 flockUnlock(id, NO_LOCK);
1878 }
1879 return closeUnixFile(id);
drhbfe66312006-10-03 17:40:40 +00001880}
1881
1882#pragma mark Old-School .lock file based locking
1883
danielk1977e339d652008-06-28 11:23:00 +00001884static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
danielk1977861f7452008-06-05 11:39:11 +00001885 int r = 1;
drhbfe66312006-10-03 17:40:40 +00001886 unixFile *pFile = (unixFile*)id;
danielk1977e339d652008-06-28 11:23:00 +00001887 char *zLockFile = (char *)pFile->lockingContext;
drh339eb0b2008-03-07 15:34:11 +00001888
danielk1977861f7452008-06-05 11:39:11 +00001889 if (pFile->locktype != RESERVED_LOCK) {
drhbfe66312006-10-03 17:40:40 +00001890 struct stat statBuf;
danielk1977e339d652008-06-28 11:23:00 +00001891 if (lstat(zLockFile, &statBuf) != 0){
drh3b62b2f2007-06-08 18:27:03 +00001892 /* file does not exist, we could have it if we want it */
danielk1977861f7452008-06-05 11:39:11 +00001893 r = 0;
drh339eb0b2008-03-07 15:34:11 +00001894 }
drhbfe66312006-10-03 17:40:40 +00001895 }
danielk1977861f7452008-06-05 11:39:11 +00001896
1897 *pResOut = r;
1898 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00001899}
1900
danielk1977e339d652008-06-28 11:23:00 +00001901static int dotlockLock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001902 unixFile *pFile = (unixFile*)id;
drh339eb0b2008-03-07 15:34:11 +00001903 int fd;
danielk1977e339d652008-06-28 11:23:00 +00001904 char *zLockFile = (char *)pFile->lockingContext;
drh339eb0b2008-03-07 15:34:11 +00001905
drh3b62b2f2007-06-08 18:27:03 +00001906 /* if we already have a lock, it is exclusive.
1907 ** Just adjust level and punt on outta here. */
drhbfe66312006-10-03 17:40:40 +00001908 if (pFile->locktype > NO_LOCK) {
1909 pFile->locktype = locktype;
1910
1911 /* Always update the timestamp on the old file */
danielk1977e339d652008-06-28 11:23:00 +00001912 utimes(zLockFile, NULL);
drhbfe66312006-10-03 17:40:40 +00001913 return SQLITE_OK;
1914 }
1915
drh3b62b2f2007-06-08 18:27:03 +00001916 /* check to see if lock file already exists */
drhbfe66312006-10-03 17:40:40 +00001917 struct stat statBuf;
danielk1977e339d652008-06-28 11:23:00 +00001918 if (lstat(zLockFile,&statBuf) == 0){
drh3b62b2f2007-06-08 18:27:03 +00001919 return SQLITE_BUSY; /* it does, busy */
drhbfe66312006-10-03 17:40:40 +00001920 }
1921
drh3b62b2f2007-06-08 18:27:03 +00001922 /* grab an exclusive lock */
danielk1977e339d652008-06-28 11:23:00 +00001923 fd = open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600);
drh339eb0b2008-03-07 15:34:11 +00001924 if( fd<0 ){
drh3b62b2f2007-06-08 18:27:03 +00001925 /* failed to open/create the file, someone else may have stolen the lock */
drhbfe66312006-10-03 17:40:40 +00001926 return SQLITE_BUSY;
1927 }
1928 close(fd);
1929
drh3b62b2f2007-06-08 18:27:03 +00001930 /* got it, set the type and return ok */
drhbfe66312006-10-03 17:40:40 +00001931 pFile->locktype = locktype;
1932 return SQLITE_OK;
1933}
1934
danielk1977e339d652008-06-28 11:23:00 +00001935static int dotlockUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001936 unixFile *pFile = (unixFile*)id;
danielk1977e339d652008-06-28 11:23:00 +00001937 char *zLockFile = (char *)pFile->lockingContext;
drh339eb0b2008-03-07 15:34:11 +00001938
drhbfe66312006-10-03 17:40:40 +00001939 assert( locktype<=SHARED_LOCK );
1940
drh3b62b2f2007-06-08 18:27:03 +00001941 /* no-op if possible */
drhbfe66312006-10-03 17:40:40 +00001942 if( pFile->locktype==locktype ){
1943 return SQLITE_OK;
1944 }
1945
drh3b62b2f2007-06-08 18:27:03 +00001946 /* shared can just be set because we always have an exclusive */
drhbfe66312006-10-03 17:40:40 +00001947 if (locktype==SHARED_LOCK) {
1948 pFile->locktype = locktype;
1949 return SQLITE_OK;
1950 }
1951
drh3b62b2f2007-06-08 18:27:03 +00001952 /* no, really, unlock. */
danielk1977e339d652008-06-28 11:23:00 +00001953 unlink(zLockFile);
drhbfe66312006-10-03 17:40:40 +00001954 pFile->locktype = NO_LOCK;
1955 return SQLITE_OK;
1956}
1957
1958/*
1959 ** Close a file.
1960 */
danielk1977e339d652008-06-28 11:23:00 +00001961static int dotlockClose(sqlite3_file *id) {
1962 if( id ){
1963 unixFile *pFile = (unixFile*)id;
1964 dotlockUnlock(id, NO_LOCK);
1965 sqlite3_free(pFile->lockingContext);
1966 }
1967 return closeUnixFile(id);
drhbfe66312006-10-03 17:40:40 +00001968}
1969
1970
1971#pragma mark No locking
1972
1973/*
drh339eb0b2008-03-07 15:34:11 +00001974** The nolockLockingContext is void
1975*/
drhbfe66312006-10-03 17:40:40 +00001976typedef void nolockLockingContext;
1977
danielk1977e339d652008-06-28 11:23:00 +00001978static int nolockCheckReservedLock(sqlite3_file *id, int *pResOut) {
danielk1977861f7452008-06-05 11:39:11 +00001979 *pResOut = 0;
1980 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00001981}
1982
danielk1977e339d652008-06-28 11:23:00 +00001983static int nolockLock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001984 return SQLITE_OK;
1985}
1986
danielk1977e339d652008-06-28 11:23:00 +00001987static int nolockUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001988 return SQLITE_OK;
1989}
1990
1991/*
drh339eb0b2008-03-07 15:34:11 +00001992** Close a file.
1993*/
danielk1977e339d652008-06-28 11:23:00 +00001994static int nolockClose(sqlite3_file *id) {
1995 return closeUnixFile(id);
drhbfe66312006-10-03 17:40:40 +00001996}
1997
1998#endif /* SQLITE_ENABLE_LOCKING_STYLE */
1999
danielk1977ad94b582007-08-20 06:44:22 +00002000
danielk1977e3026632004-06-22 11:29:02 +00002001/*
drh9e33c2c2007-08-31 18:34:59 +00002002** Information and control of an open file handle.
drh18839212005-11-26 03:43:23 +00002003*/
drhcc6bb3e2007-08-31 16:11:35 +00002004static int unixFileControl(sqlite3_file *id, int op, void *pArg){
drh9e33c2c2007-08-31 18:34:59 +00002005 switch( op ){
2006 case SQLITE_FCNTL_LOCKSTATE: {
2007 *(int*)pArg = ((unixFile*)id)->locktype;
2008 return SQLITE_OK;
2009 }
2010 }
drhcc6bb3e2007-08-31 16:11:35 +00002011 return SQLITE_ERROR;
drh9cbe6352005-11-29 03:13:21 +00002012}
2013
2014/*
danielk1977a3d4c882007-03-23 10:08:38 +00002015** Return the sector size in bytes of the underlying block device for
2016** the specified file. This is almost always 512 bytes, but may be
2017** larger for some devices.
2018**
2019** SQLite code assumes this function cannot fail. It also assumes that
2020** if two files are created in the same file-system directory (i.e.
drh85b623f2007-12-13 21:54:09 +00002021** a database and its journal file) that the sector size will be the
danielk1977a3d4c882007-03-23 10:08:38 +00002022** same for both.
2023*/
danielk197762079062007-08-15 17:08:46 +00002024static int unixSectorSize(sqlite3_file *id){
drh3ceeb752007-03-29 18:19:52 +00002025 return SQLITE_DEFAULT_SECTOR_SIZE;
danielk1977a3d4c882007-03-23 10:08:38 +00002026}
2027
danielk197790949c22007-08-17 16:50:38 +00002028/*
2029** Return the device characteristics for the file. This is always 0.
2030*/
danielk197762079062007-08-15 17:08:46 +00002031static int unixDeviceCharacteristics(sqlite3_file *id){
2032 return 0;
2033}
2034
danielk1977a3d4c882007-03-23 10:08:38 +00002035/*
danielk1977e339d652008-06-28 11:23:00 +00002036** Initialize the contents of the unixFile structure pointed to by pId.
2037**
danielk1977ad94b582007-08-20 06:44:22 +00002038** When locking extensions are enabled, the filepath and locking style
2039** are needed to determine the unixFile pMethod to use for locking operations.
2040** The locking-style specific lockingContext data structure is created
2041** and assigned here also.
2042*/
2043static int fillInUnixFile(
danielk1977e339d652008-06-28 11:23:00 +00002044 sqlite3_vfs *pVfs, /* Pointer to vfs object */
drhbfe66312006-10-03 17:40:40 +00002045 int h, /* Open file descriptor of file being opened */
danielk1977ad94b582007-08-20 06:44:22 +00002046 int dirfd, /* Directory file descriptor */
drh218c5082008-03-07 00:27:10 +00002047 sqlite3_file *pId, /* Write to the unixFile structure here */
2048 const char *zFilename /* Name of the file being opened */
drhbfe66312006-10-03 17:40:40 +00002049){
danielk1977e339d652008-06-28 11:23:00 +00002050 /* Macro to define the static contents of an sqlite3_io_methods
2051 ** structure for a unix backend file. Different locking methods
2052 ** require different functions for the xClose, xLock, xUnlock and
2053 ** xCheckReservedLock methods.
2054 */
2055 #define IOMETHODS(xClose, xLock, xUnlock, xCheckReservedLock) { \
2056 1, /* iVersion */ \
2057 xClose, /* xClose */ \
2058 unixRead, /* xRead */ \
2059 unixWrite, /* xWrite */ \
2060 unixTruncate, /* xTruncate */ \
2061 unixSync, /* xSync */ \
2062 unixFileSize, /* xFileSize */ \
2063 xLock, /* xLock */ \
2064 xUnlock, /* xUnlock */ \
2065 xCheckReservedLock, /* xCheckReservedLock */ \
2066 unixFileControl, /* xFileControl */ \
2067 unixSectorSize, /* xSectorSize */ \
2068 unixDeviceCharacteristics /* xDeviceCapabilities */ \
2069 }
2070 static sqlite3_io_methods aIoMethod[] = {
2071 IOMETHODS(unixClose, unixLock, unixUnlock, unixCheckReservedLock)
2072#ifdef SQLITE_ENABLE_LOCKING_STYLE
2073 ,IOMETHODS(flockClose, flockLock, flockUnlock, flockCheckReservedLock)
2074 ,IOMETHODS(dotlockClose, dotlockLock, dotlockUnlock,dotlockCheckReservedLock)
2075 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
2076 ,IOMETHODS(afpClose, afpLock, afpUnlock, afpCheckReservedLock)
drh218c5082008-03-07 00:27:10 +00002077#endif
danielk1977e339d652008-06-28 11:23:00 +00002078 };
2079
2080 int eLockingStyle;
2081 unixFile *pNew = (unixFile *)pId;
2082 int rc = SQLITE_OK;
drh218c5082008-03-07 00:27:10 +00002083
danielk197717b90b52008-06-06 11:11:25 +00002084 assert( pNew->pLock==NULL );
2085 assert( pNew->pOpen==NULL );
drh218c5082008-03-07 00:27:10 +00002086
2087 OSTRACE3("OPEN %-3d %s\n", h, zFilename);
danielk1977ad94b582007-08-20 06:44:22 +00002088 pNew->h = h;
drh218c5082008-03-07 00:27:10 +00002089 pNew->dirfd = dirfd;
danielk1977ad94b582007-08-20 06:44:22 +00002090 SET_THREADID(pNew);
drh339eb0b2008-03-07 15:34:11 +00002091
danielk1977e339d652008-06-28 11:23:00 +00002092 assert(LOCKING_STYLE_POSIX==1);
2093 assert(LOCKING_STYLE_FLOCK==2);
2094 assert(LOCKING_STYLE_DOTFILE==3);
2095 assert(LOCKING_STYLE_NONE==4);
2096 assert(LOCKING_STYLE_AFP==5);
2097 eLockingStyle = detectLockingStyle(pVfs, zFilename, h);
2098 pNew->pMethod = &aIoMethod[eLockingStyle-1];
2099
2100 switch( eLockingStyle ){
2101
2102 case LOCKING_STYLE_POSIX: {
2103 enterMutex();
2104 rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
2105 leaveMutex();
drh218c5082008-03-07 00:27:10 +00002106 break;
drhbfe66312006-10-03 17:40:40 +00002107 }
danielk1977e339d652008-06-28 11:23:00 +00002108
2109#ifdef SQLITE_ENABLE_LOCKING_STYLE
2110 case LOCKING_STYLE_AFP: {
2111 /* AFP locking uses the file path so it needs to be included in
2112 ** the afpLockingContext.
2113 */
2114 afpLockingContext *pCtx;
2115 pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
2116 if( pCtx==0 ){
2117 rc = SQLITE_NOMEM;
2118 }else{
2119 /* NB: zFilename exists and remains valid until the file is closed
2120 ** according to requirement F11141. So we do not need to make a
2121 ** copy of the filename. */
2122 pCtx->filePath = zFilename;
2123 srandomdev();
2124 }
drh218c5082008-03-07 00:27:10 +00002125 break;
danielk1977e339d652008-06-28 11:23:00 +00002126 }
2127
2128 case LOCKING_STYLE_DOTFILE: {
2129 /* Dotfile locking uses the file path so it needs to be included in
2130 ** the dotlockLockingContext
2131 */
2132 char *zLockFile;
drh218c5082008-03-07 00:27:10 +00002133 int nFilename;
danielk1977e339d652008-06-28 11:23:00 +00002134 nFilename = strlen(zFilename) + 6;
2135 zLockFile = (char *)sqlite3_malloc(nFilename);
2136 if( zLockFile==0 ){
2137 rc = SQLITE_NOMEM;
2138 }else{
2139 sqlite3_snprintf(nFilename, zLockFile, "%s.lock", zFilename);
drh339eb0b2008-03-07 15:34:11 +00002140 }
danielk1977e339d652008-06-28 11:23:00 +00002141 pNew->lockingContext = zLockFile;
drh218c5082008-03-07 00:27:10 +00002142 break;
2143 }
danielk1977e339d652008-06-28 11:23:00 +00002144
2145 case LOCKING_STYLE_FLOCK:
2146 case LOCKING_STYLE_NONE:
drh218c5082008-03-07 00:27:10 +00002147 break;
drhe78669b2007-06-29 12:04:26 +00002148#endif
danielk1977e339d652008-06-28 11:23:00 +00002149 }
danielk1977b4b47412007-08-17 15:53:36 +00002150
danielk1977e339d652008-06-28 11:23:00 +00002151 if( rc!=SQLITE_OK ){
danielk19777c055b92007-10-30 17:28:51 +00002152 if( dirfd>=0 ) close(dirfd);
drhbfe66312006-10-03 17:40:40 +00002153 close(h);
danielk1977e339d652008-06-28 11:23:00 +00002154 }else{
2155 OpenCounter(+1);
drhbfe66312006-10-03 17:40:40 +00002156 }
danielk1977e339d652008-06-28 11:23:00 +00002157 return rc;
drh054889e2005-11-30 03:20:31 +00002158}
drh9c06c952005-11-26 00:25:00 +00002159
danielk1977ad94b582007-08-20 06:44:22 +00002160/*
2161** Open a file descriptor to the directory containing file zFilename.
2162** If successful, *pFd is set to the opened file descriptor and
2163** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
2164** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
2165** value.
2166**
2167** If SQLITE_OK is returned, the caller is responsible for closing
2168** the file descriptor *pFd using close().
2169*/
danielk1977fee2d252007-08-18 10:59:19 +00002170static int openDirectory(const char *zFilename, int *pFd){
danielk1977fee2d252007-08-18 10:59:19 +00002171 int ii;
drh777b17a2007-09-20 10:02:54 +00002172 int fd = -1;
drhf3a65f72007-08-22 20:18:21 +00002173 char zDirname[MAX_PATHNAME+1];
danielk1977fee2d252007-08-18 10:59:19 +00002174
drh153c62c2007-08-24 03:51:33 +00002175 sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
danielk1977fee2d252007-08-18 10:59:19 +00002176 for(ii=strlen(zDirname); ii>=0 && zDirname[ii]!='/'; ii--);
2177 if( ii>0 ){
2178 zDirname[ii] = '\0';
2179 fd = open(zDirname, O_RDONLY|O_BINARY, 0);
drh777b17a2007-09-20 10:02:54 +00002180 if( fd>=0 ){
danielk1977fee2d252007-08-18 10:59:19 +00002181#ifdef FD_CLOEXEC
2182 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
2183#endif
2184 OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname);
2185 }
2186 }
danielk1977fee2d252007-08-18 10:59:19 +00002187 *pFd = fd;
drh777b17a2007-09-20 10:02:54 +00002188 return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN);
danielk1977fee2d252007-08-18 10:59:19 +00002189}
2190
danielk1977b4b47412007-08-17 15:53:36 +00002191/*
danielk197717b90b52008-06-06 11:11:25 +00002192** Create a temporary file name in zBuf. zBuf must be allocated
2193** by the calling process and must be big enough to hold at least
2194** pVfs->mxPathname bytes.
2195*/
2196static int getTempname(int nBuf, char *zBuf){
2197 static const char *azDirs[] = {
2198 0,
2199 "/var/tmp",
2200 "/usr/tmp",
2201 "/tmp",
2202 ".",
2203 };
2204 static const unsigned char zChars[] =
2205 "abcdefghijklmnopqrstuvwxyz"
2206 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2207 "0123456789";
2208 int i, j;
2209 struct stat buf;
2210 const char *zDir = ".";
2211
2212 /* It's odd to simulate an io-error here, but really this is just
2213 ** using the io-error infrastructure to test that SQLite handles this
2214 ** function failing.
2215 */
2216 SimulateIOError( return SQLITE_IOERR );
2217
2218 azDirs[0] = sqlite3_temp_directory;
2219 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
2220 if( azDirs[i]==0 ) continue;
2221 if( stat(azDirs[i], &buf) ) continue;
2222 if( !S_ISDIR(buf.st_mode) ) continue;
2223 if( access(azDirs[i], 07) ) continue;
2224 zDir = azDirs[i];
2225 break;
2226 }
2227
2228 /* Check that the output buffer is large enough for the temporary file
2229 ** name. If it is not, return SQLITE_ERROR.
2230 */
2231 if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= nBuf ){
2232 return SQLITE_ERROR;
2233 }
2234
2235 do{
2236 sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
2237 j = strlen(zBuf);
2238 sqlite3_randomness(15, &zBuf[j]);
2239 for(i=0; i<15; i++, j++){
2240 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
2241 }
2242 zBuf[j] = 0;
2243 }while( access(zBuf,0)==0 );
2244 return SQLITE_OK;
2245}
2246
2247
2248/*
danielk1977ad94b582007-08-20 06:44:22 +00002249** Open the file zPath.
2250**
danielk1977b4b47412007-08-17 15:53:36 +00002251** Previously, the SQLite OS layer used three functions in place of this
2252** one:
2253**
2254** sqlite3OsOpenReadWrite();
2255** sqlite3OsOpenReadOnly();
2256** sqlite3OsOpenExclusive();
2257**
2258** These calls correspond to the following combinations of flags:
2259**
2260** ReadWrite() -> (READWRITE | CREATE)
2261** ReadOnly() -> (READONLY)
2262** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
2263**
2264** The old OpenExclusive() accepted a boolean argument - "delFlag". If
2265** true, the file was configured to be automatically deleted when the
2266** file handle closed. To achieve the same effect using this new
2267** interface, add the DELETEONCLOSE flag to those specified above for
2268** OpenExclusive().
2269*/
2270static int unixOpen(
drh153c62c2007-08-24 03:51:33 +00002271 sqlite3_vfs *pVfs,
danielk1977b4b47412007-08-17 15:53:36 +00002272 const char *zPath,
2273 sqlite3_file *pFile,
2274 int flags,
2275 int *pOutFlags
2276){
danielk1977fee2d252007-08-18 10:59:19 +00002277 int fd = 0; /* File descriptor returned by open() */
2278 int dirfd = -1; /* Directory file descriptor */
2279 int oflags = 0; /* Flags to pass to open() */
2280 int eType = flags&0xFFFFFF00; /* Type of file to open */
danielk1977b4b47412007-08-17 15:53:36 +00002281
2282 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
2283 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
2284 int isCreate = (flags & SQLITE_OPEN_CREATE);
2285 int isReadonly = (flags & SQLITE_OPEN_READONLY);
2286 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
2287
danielk1977fee2d252007-08-18 10:59:19 +00002288 /* If creating a master or main-file journal, this function will open
2289 ** a file-descriptor on the directory too. The first time unixSync()
2290 ** is called the directory file descriptor will be fsync()ed and close()d.
2291 */
2292 int isOpenDirectory = (isCreate &&
2293 (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL)
2294 );
2295
danielk197717b90b52008-06-06 11:11:25 +00002296 /* If argument zPath is a NULL pointer, this function is required to open
2297 ** a temporary file. Use this buffer to store the file name in.
2298 */
2299 char zTmpname[MAX_PATHNAME+1];
2300 const char *zName = zPath;
2301
danielk1977fee2d252007-08-18 10:59:19 +00002302 /* Check the following statements are true:
2303 **
2304 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
2305 ** (b) if CREATE is set, then READWRITE must also be set, and
2306 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
drh33f4e022007-09-03 15:19:34 +00002307 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
danielk1977fee2d252007-08-18 10:59:19 +00002308 */
danielk1977b4b47412007-08-17 15:53:36 +00002309 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
danielk1977b4b47412007-08-17 15:53:36 +00002310 assert(isCreate==0 || isReadWrite);
danielk1977b4b47412007-08-17 15:53:36 +00002311 assert(isExclusive==0 || isCreate);
drh33f4e022007-09-03 15:19:34 +00002312 assert(isDelete==0 || isCreate);
2313
drh33f4e022007-09-03 15:19:34 +00002314 /* The main DB, main journal, and master journal are never automatically
2315 ** deleted
2316 */
2317 assert( eType!=SQLITE_OPEN_MAIN_DB || !isDelete );
2318 assert( eType!=SQLITE_OPEN_MAIN_JOURNAL || !isDelete );
2319 assert( eType!=SQLITE_OPEN_MASTER_JOURNAL || !isDelete );
danielk1977b4b47412007-08-17 15:53:36 +00002320
danielk1977fee2d252007-08-18 10:59:19 +00002321 /* Assert that the upper layer has set one of the "file-type" flags. */
2322 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
2323 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
2324 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
drh33f4e022007-09-03 15:19:34 +00002325 || eType==SQLITE_OPEN_TRANSIENT_DB
danielk1977fee2d252007-08-18 10:59:19 +00002326 );
2327
danielk1977e339d652008-06-28 11:23:00 +00002328 memset(pFile, 0, sizeof(unixFile));
2329
danielk197717b90b52008-06-06 11:11:25 +00002330 if( !zName ){
2331 int rc;
2332 assert(isDelete && !isOpenDirectory);
2333 rc = getTempname(MAX_PATHNAME+1, zTmpname);
2334 if( rc!=SQLITE_OK ){
2335 return rc;
2336 }
2337 zName = zTmpname;
2338 }
2339
danielk1977b4b47412007-08-17 15:53:36 +00002340 if( isReadonly ) oflags |= O_RDONLY;
2341 if( isReadWrite ) oflags |= O_RDWR;
2342 if( isCreate ) oflags |= O_CREAT;
2343 if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW);
2344 oflags |= (O_LARGEFILE|O_BINARY);
2345
danielk197717b90b52008-06-06 11:11:25 +00002346 fd = open(zName, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
danielk19772f2d8c72007-08-30 16:13:33 +00002347 if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
danielk1977b4b47412007-08-17 15:53:36 +00002348 /* Failed to open the file for read/write access. Try read-only. */
2349 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
2350 flags |= SQLITE_OPEN_READONLY;
drh153c62c2007-08-24 03:51:33 +00002351 return unixOpen(pVfs, zPath, pFile, flags, pOutFlags);
danielk1977b4b47412007-08-17 15:53:36 +00002352 }
2353 if( fd<0 ){
2354 return SQLITE_CANTOPEN;
2355 }
2356 if( isDelete ){
danielk197717b90b52008-06-06 11:11:25 +00002357 unlink(zName);
danielk1977b4b47412007-08-17 15:53:36 +00002358 }
2359 if( pOutFlags ){
2360 *pOutFlags = flags;
2361 }
2362
2363 assert(fd!=0);
danielk1977fee2d252007-08-18 10:59:19 +00002364 if( isOpenDirectory ){
2365 int rc = openDirectory(zPath, &dirfd);
2366 if( rc!=SQLITE_OK ){
2367 close(fd);
2368 return rc;
2369 }
2370 }
danielk1977e339d652008-06-28 11:23:00 +00002371
2372#ifdef FD_CLOEXEC
2373 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
2374#endif
2375
2376 return fillInUnixFile(pVfs, fd, dirfd, pFile, zPath);
danielk1977b4b47412007-08-17 15:53:36 +00002377}
2378
2379/*
danielk1977fee2d252007-08-18 10:59:19 +00002380** Delete the file at zPath. If the dirSync argument is true, fsync()
2381** the directory after deleting the file.
danielk1977b4b47412007-08-17 15:53:36 +00002382*/
drh153c62c2007-08-24 03:51:33 +00002383static int unixDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
danielk1977fee2d252007-08-18 10:59:19 +00002384 int rc = SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00002385 SimulateIOError(return SQLITE_IOERR_DELETE);
2386 unlink(zPath);
danielk1977fee2d252007-08-18 10:59:19 +00002387 if( dirSync ){
2388 int fd;
2389 rc = openDirectory(zPath, &fd);
2390 if( rc==SQLITE_OK ){
2391 if( fsync(fd) ){
2392 rc = SQLITE_IOERR_DIR_FSYNC;
2393 }
2394 close(fd);
2395 }
2396 }
2397 return rc;
danielk1977b4b47412007-08-17 15:53:36 +00002398}
2399
danielk197790949c22007-08-17 16:50:38 +00002400/*
2401** Test the existance of or access permissions of file zPath. The
2402** test performed depends on the value of flags:
2403**
2404** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
2405** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
2406** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
2407**
2408** Otherwise return 0.
2409*/
danielk1977861f7452008-06-05 11:39:11 +00002410static int unixAccess(
2411 sqlite3_vfs *pVfs,
2412 const char *zPath,
2413 int flags,
2414 int *pResOut
2415){
rse25c0d1a2007-09-20 08:38:14 +00002416 int amode = 0;
danielk1977861f7452008-06-05 11:39:11 +00002417 SimulateIOError( return SQLITE_IOERR_ACCESS; );
danielk1977b4b47412007-08-17 15:53:36 +00002418 switch( flags ){
2419 case SQLITE_ACCESS_EXISTS:
2420 amode = F_OK;
2421 break;
2422 case SQLITE_ACCESS_READWRITE:
2423 amode = W_OK|R_OK;
2424 break;
drh50d3f902007-08-27 21:10:36 +00002425 case SQLITE_ACCESS_READ:
danielk1977b4b47412007-08-17 15:53:36 +00002426 amode = R_OK;
2427 break;
2428
2429 default:
2430 assert(!"Invalid flags argument");
2431 }
danielk1977861f7452008-06-05 11:39:11 +00002432 *pResOut = (access(zPath, amode)==0);
2433 return SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00002434}
2435
danielk1977b4b47412007-08-17 15:53:36 +00002436
2437/*
2438** Turn a relative pathname into a full pathname. The relative path
2439** is stored as a nul-terminated string in the buffer pointed to by
2440** zPath.
2441**
2442** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
2443** (in this case, MAX_PATHNAME bytes). The full-path is written to
2444** this buffer before returning.
2445*/
danielk1977adfb9b02007-09-17 07:02:56 +00002446static int unixFullPathname(
2447 sqlite3_vfs *pVfs, /* Pointer to vfs object */
2448 const char *zPath, /* Possibly relative input path */
2449 int nOut, /* Size of output buffer in bytes */
2450 char *zOut /* Output buffer */
2451){
danielk1977843e65f2007-09-01 16:16:15 +00002452
2453 /* It's odd to simulate an io-error here, but really this is just
2454 ** using the io-error infrastructure to test that SQLite handles this
2455 ** function failing. This function could fail if, for example, the
2456 ** current working directly has been unlinked.
2457 */
2458 SimulateIOError( return SQLITE_ERROR );
2459
drh153c62c2007-08-24 03:51:33 +00002460 assert( pVfs->mxPathname==MAX_PATHNAME );
drh3c7f2dc2007-12-06 13:26:20 +00002461 zOut[nOut-1] = '\0';
danielk1977b4b47412007-08-17 15:53:36 +00002462 if( zPath[0]=='/' ){
drh3c7f2dc2007-12-06 13:26:20 +00002463 sqlite3_snprintf(nOut, zOut, "%s", zPath);
danielk1977b4b47412007-08-17 15:53:36 +00002464 }else{
2465 int nCwd;
drh3c7f2dc2007-12-06 13:26:20 +00002466 if( getcwd(zOut, nOut-1)==0 ){
drh70c01452007-09-03 17:42:17 +00002467 return SQLITE_CANTOPEN;
danielk1977b4b47412007-08-17 15:53:36 +00002468 }
2469 nCwd = strlen(zOut);
drh3c7f2dc2007-12-06 13:26:20 +00002470 sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
danielk1977b4b47412007-08-17 15:53:36 +00002471 }
2472 return SQLITE_OK;
2473
2474#if 0
2475 /*
2476 ** Remove "/./" path elements and convert "/A/./" path elements
2477 ** to just "/".
2478 */
2479 if( zFull ){
2480 int i, j;
2481 for(i=j=0; zFull[i]; i++){
2482 if( zFull[i]=='/' ){
2483 if( zFull[i+1]=='/' ) continue;
2484 if( zFull[i+1]=='.' && zFull[i+2]=='/' ){
2485 i += 1;
2486 continue;
2487 }
2488 if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){
2489 while( j>0 && zFull[j-1]!='/' ){ j--; }
2490 i += 3;
2491 continue;
2492 }
2493 }
2494 zFull[j++] = zFull[i];
2495 }
2496 zFull[j] = 0;
2497 }
2498#endif
2499}
2500
drh0ccebe72005-06-07 22:22:50 +00002501
drh761df872006-12-21 01:29:22 +00002502#ifndef SQLITE_OMIT_LOAD_EXTENSION
2503/*
2504** Interfaces for opening a shared library, finding entry points
2505** within the shared library, and closing the shared library.
2506*/
2507#include <dlfcn.h>
drh153c62c2007-08-24 03:51:33 +00002508static void *unixDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
drh761df872006-12-21 01:29:22 +00002509 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
2510}
danielk197795c8a542007-09-01 06:51:27 +00002511
2512/*
2513** SQLite calls this function immediately after a call to unixDlSym() or
2514** unixDlOpen() fails (returns a null pointer). If a more detailed error
2515** message is available, it is written to zBufOut. If no error message
2516** is available, zBufOut is left unmodified and SQLite uses a default
2517** error message.
2518*/
drh153c62c2007-08-24 03:51:33 +00002519static void unixDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
danielk1977b4b47412007-08-17 15:53:36 +00002520 char *zErr;
2521 enterMutex();
2522 zErr = dlerror();
2523 if( zErr ){
drh153c62c2007-08-24 03:51:33 +00002524 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
danielk1977b4b47412007-08-17 15:53:36 +00002525 }
2526 leaveMutex();
2527}
drh46c99e02007-08-27 23:26:59 +00002528static void *unixDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){
drh761df872006-12-21 01:29:22 +00002529 return dlsym(pHandle, zSymbol);
2530}
drh46c99e02007-08-27 23:26:59 +00002531static void unixDlClose(sqlite3_vfs *pVfs, void *pHandle){
danielk1977b4b47412007-08-17 15:53:36 +00002532 dlclose(pHandle);
drh761df872006-12-21 01:29:22 +00002533}
danielk1977b4b47412007-08-17 15:53:36 +00002534#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
2535 #define unixDlOpen 0
2536 #define unixDlError 0
2537 #define unixDlSym 0
2538 #define unixDlClose 0
2539#endif
2540
2541/*
danielk197790949c22007-08-17 16:50:38 +00002542** Write nBuf bytes of random data to the supplied buffer zBuf.
drhbbd42a62004-05-22 17:41:58 +00002543*/
drh153c62c2007-08-24 03:51:33 +00002544static int unixRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
danielk197790949c22007-08-17 16:50:38 +00002545
2546 assert(nBuf>=(sizeof(time_t)+sizeof(int)));
2547
drhbbd42a62004-05-22 17:41:58 +00002548 /* We have to initialize zBuf to prevent valgrind from reporting
2549 ** errors. The reports issued by valgrind are incorrect - we would
2550 ** prefer that the randomness be increased by making use of the
2551 ** uninitialized space in zBuf - but valgrind errors tend to worry
2552 ** some users. Rather than argue, it seems easier just to initialize
2553 ** the whole array and silence valgrind, even if that means less randomness
2554 ** in the random seed.
2555 **
2556 ** When testing, initializing zBuf[] to zero is all we do. That means
drhf1a221e2006-01-15 17:27:17 +00002557 ** that we always use the same random number sequence. This makes the
drhbbd42a62004-05-22 17:41:58 +00002558 ** tests repeatable.
2559 */
danielk1977b4b47412007-08-17 15:53:36 +00002560 memset(zBuf, 0, nBuf);
drhbbd42a62004-05-22 17:41:58 +00002561#if !defined(SQLITE_TEST)
2562 {
drh842b8642005-01-21 17:53:17 +00002563 int pid, fd;
2564 fd = open("/dev/urandom", O_RDONLY);
2565 if( fd<0 ){
drh07397232006-01-06 14:46:46 +00002566 time_t t;
2567 time(&t);
danielk197790949c22007-08-17 16:50:38 +00002568 memcpy(zBuf, &t, sizeof(t));
2569 pid = getpid();
2570 memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
drh842b8642005-01-21 17:53:17 +00002571 }else{
danielk1977b4b47412007-08-17 15:53:36 +00002572 read(fd, zBuf, nBuf);
drh842b8642005-01-21 17:53:17 +00002573 close(fd);
2574 }
drhbbd42a62004-05-22 17:41:58 +00002575 }
2576#endif
2577 return SQLITE_OK;
2578}
2579
danielk1977b4b47412007-08-17 15:53:36 +00002580
drhbbd42a62004-05-22 17:41:58 +00002581/*
2582** Sleep for a little while. Return the amount of time slept.
danielk1977b4b47412007-08-17 15:53:36 +00002583** The argument is the number of microseconds we want to sleep.
drh4a50aac2007-08-23 02:47:53 +00002584** The return value is the number of microseconds of sleep actually
2585** requested from the underlying operating system, a number which
2586** might be greater than or equal to the argument, but not less
2587** than the argument.
drhbbd42a62004-05-22 17:41:58 +00002588*/
drh153c62c2007-08-24 03:51:33 +00002589static int unixSleep(sqlite3_vfs *pVfs, int microseconds){
drhbbd42a62004-05-22 17:41:58 +00002590#if defined(HAVE_USLEEP) && HAVE_USLEEP
danielk1977b4b47412007-08-17 15:53:36 +00002591 usleep(microseconds);
2592 return microseconds;
drhbbd42a62004-05-22 17:41:58 +00002593#else
danielk1977b4b47412007-08-17 15:53:36 +00002594 int seconds = (microseconds+999999)/1000000;
2595 sleep(seconds);
drh4a50aac2007-08-23 02:47:53 +00002596 return seconds*1000000;
drha3fad6f2006-01-18 14:06:37 +00002597#endif
drh88f474a2006-01-02 20:00:12 +00002598}
2599
2600/*
drhbbd42a62004-05-22 17:41:58 +00002601** The following variable, if set to a non-zero value, becomes the result
drh66560ad2006-01-06 14:32:19 +00002602** returned from sqlite3OsCurrentTime(). This is used for testing.
drhbbd42a62004-05-22 17:41:58 +00002603*/
2604#ifdef SQLITE_TEST
2605int sqlite3_current_time = 0;
2606#endif
2607
2608/*
2609** Find the current time (in Universal Coordinated Time). Write the
2610** current time and date as a Julian Day number into *prNow and
2611** return 0. Return 1 if the time and date cannot be found.
2612*/
drh153c62c2007-08-24 03:51:33 +00002613static int unixCurrentTime(sqlite3_vfs *pVfs, double *prNow){
drh19e2d372005-08-29 23:00:03 +00002614#ifdef NO_GETTOD
drhbbd42a62004-05-22 17:41:58 +00002615 time_t t;
2616 time(&t);
2617 *prNow = t/86400.0 + 2440587.5;
drh19e2d372005-08-29 23:00:03 +00002618#else
2619 struct timeval sNow;
drhbdcc2762007-04-02 18:06:57 +00002620 gettimeofday(&sNow, 0);
drh19e2d372005-08-29 23:00:03 +00002621 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
2622#endif
drhbbd42a62004-05-22 17:41:58 +00002623#ifdef SQLITE_TEST
2624 if( sqlite3_current_time ){
2625 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
2626 }
2627#endif
2628 return 0;
2629}
danielk1977b4b47412007-08-17 15:53:36 +00002630
danielk1977bcb97fe2008-06-06 15:49:29 +00002631static int unixGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
2632 return 0;
2633}
2634
drh153c62c2007-08-24 03:51:33 +00002635/*
danielk1977e339d652008-06-28 11:23:00 +00002636** Initialize the operating system interface.
drh153c62c2007-08-24 03:51:33 +00002637*/
danielk1977c0fa4c52008-06-25 17:19:00 +00002638int sqlite3_os_init(void){
danielk1977e339d652008-06-28 11:23:00 +00002639 /* Macro to define the static contents of an sqlite3_vfs structure for
2640 ** the unix backend. The two parameters are the values to use for
2641 ** the sqlite3_vfs.zName and sqlite3_vfs.pAppData fields, respectively.
2642 **
2643 */
2644 #define UNIXVFS(zVfsName, pVfsAppData) { \
2645 1, /* iVersion */ \
2646 sizeof(unixFile), /* szOsFile */ \
2647 MAX_PATHNAME, /* mxPathname */ \
2648 0, /* pNext */ \
2649 zVfsName, /* zName */ \
2650 (void *)pVfsAppData, /* pAppData */ \
2651 unixOpen, /* xOpen */ \
2652 unixDelete, /* xDelete */ \
2653 unixAccess, /* xAccess */ \
2654 unixFullPathname, /* xFullPathname */ \
2655 unixDlOpen, /* xDlOpen */ \
2656 unixDlError, /* xDlError */ \
2657 unixDlSym, /* xDlSym */ \
2658 unixDlClose, /* xDlClose */ \
2659 unixRandomness, /* xRandomness */ \
2660 unixSleep, /* xSleep */ \
2661 unixCurrentTime, /* xCurrentTime */ \
2662 unixGetLastError /* xGetLastError */ \
2663 }
2664
2665 static sqlite3_vfs unixVfs = UNIXVFS("unix", 0);
2666#ifdef SQLITE_ENABLE_LOCKING_STYLE
2667#if 0
2668 int i;
2669 static sqlite3_vfs aVfs[] = {
2670 UNIXVFS("unix-posix", LOCKING_STYLE_POSIX),
2671 UNIXVFS("unix-afp", LOCKING_STYLE_AFP),
2672 UNIXVFS("unix-flock", LOCKING_STYLE_FLOCK),
2673 UNIXVFS("unix-dotfile", LOCKING_STYLE_DOTFILE),
2674 UNIXVFS("unix-none", LOCKING_STYLE_NONE)
drh153c62c2007-08-24 03:51:33 +00002675 };
danielk1977e339d652008-06-28 11:23:00 +00002676 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
2677 sqlite3_vfs_register(&aVfs[i], 0);
2678 }
2679#endif
2680#endif
danielk1977c0fa4c52008-06-25 17:19:00 +00002681 sqlite3_vfs_register(&unixVfs, 1);
2682 return SQLITE_OK;
drh153c62c2007-08-24 03:51:33 +00002683}
danielk1977e339d652008-06-28 11:23:00 +00002684
2685/*
2686** Shutdown the operating system interface. This is a no-op for unix.
2687*/
danielk1977c0fa4c52008-06-25 17:19:00 +00002688int sqlite3_os_end(void){
2689 return SQLITE_OK;
2690}
drhdce8bdb2007-08-16 13:01:44 +00002691
danielk197729bafea2008-06-26 10:41:19 +00002692#endif /* SQLITE_OS_UNIX */