blob: 066ae6e58982ad188f385d2c9a04d001308db01c [file] [log] [blame]
drhbbd42a62004-05-22 17:41:58 +00001/*
2** 2004 May 22
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11******************************************************************************
12**
13** This file contains code that is specific to Unix systems.
14*/
drhbbd42a62004-05-22 17:41:58 +000015#include "sqliteInt.h"
drheb206252004-10-01 02:00:31 +000016#if OS_UNIX /* This file is used on unix only */
drh66560ad2006-01-06 14:32:19 +000017
drhbfe66312006-10-03 17:40:40 +000018/* #define SQLITE_ENABLE_LOCKING_STYLE 0 */
19
drh9cbe6352005-11-29 03:13:21 +000020/*
21** These #defines should enable >2GB file support on Posix if the
22** underlying operating system supports it. If the OS lacks
drhf1a221e2006-01-15 17:27:17 +000023** large file support, these should be no-ops.
drh9cbe6352005-11-29 03:13:21 +000024**
25** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
26** on the compiler command line. This is necessary if you are compiling
27** on a recent machine (ex: RedHat 7.2) but you want your code to work
28** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2
29** without this option, LFS is enable. But LFS does not exist in the kernel
30** in RedHat 6.0, so the code won't work. Hence, for maximum binary
31** portability you should omit LFS.
drh9cbe6352005-11-29 03:13:21 +000032*/
33#ifndef SQLITE_DISABLE_LFS
34# define _LARGE_FILE 1
35# ifndef _FILE_OFFSET_BITS
36# define _FILE_OFFSET_BITS 64
37# endif
38# define _LARGEFILE_SOURCE 1
39#endif
drhbbd42a62004-05-22 17:41:58 +000040
drh9cbe6352005-11-29 03:13:21 +000041/*
42** standard include files.
43*/
44#include <sys/types.h>
45#include <sys/stat.h>
46#include <fcntl.h>
47#include <unistd.h>
drhbbd42a62004-05-22 17:41:58 +000048#include <time.h>
drh19e2d372005-08-29 23:00:03 +000049#include <sys/time.h>
drhbbd42a62004-05-22 17:41:58 +000050#include <errno.h>
drhbfe66312006-10-03 17:40:40 +000051#ifdef SQLITE_ENABLE_LOCKING_STYLE
52#include <sys/ioctl.h>
53#include <sys/param.h>
54#include <sys/mount.h>
55#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drh9cbe6352005-11-29 03:13:21 +000056
57/*
drhf1a221e2006-01-15 17:27:17 +000058** If we are to be thread-safe, include the pthreads header and define
59** the SQLITE_UNIX_THREADS macro.
drh9cbe6352005-11-29 03:13:21 +000060*/
drhd677b3d2007-08-20 22:48:41 +000061#if SQLITE_THREADSAFE
drh9cbe6352005-11-29 03:13:21 +000062# include <pthread.h>
63# define SQLITE_UNIX_THREADS 1
64#endif
65
66/*
67** Default permissions when creating a new file
68*/
69#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
70# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
71#endif
72
danielk1977b4b47412007-08-17 15:53:36 +000073/*
74** Maximum supported path-length.
75*/
76#define MAX_PATHNAME 512
drh9cbe6352005-11-29 03:13:21 +000077
78
79/*
danielk1977ad94b582007-08-20 06:44:22 +000080** The unixFile structure is subclass of sqlite3_file specific for the unix
drh054889e2005-11-30 03:20:31 +000081** protability layer.
drh9cbe6352005-11-29 03:13:21 +000082*/
drh054889e2005-11-30 03:20:31 +000083typedef struct unixFile unixFile;
84struct unixFile {
danielk197762079062007-08-15 17:08:46 +000085 sqlite3_io_methods const *pMethod; /* Always the first entry */
danielk1977967a4a12007-08-20 14:23:44 +000086#ifdef SQLITE_TEST
87 /* In test mode, increase the size of this structure a bit so that
88 ** it is larger than the struct CrashFile defined in test6.c.
89 */
90 char aPadding[32];
91#endif
drh9cbe6352005-11-29 03:13:21 +000092 struct openCnt *pOpen; /* Info about all open fd's on this inode */
93 struct lockInfo *pLock; /* Info about locks on this inode */
drhbfe66312006-10-03 17:40:40 +000094#ifdef SQLITE_ENABLE_LOCKING_STYLE
95 void *lockingContext; /* Locking style specific state */
96#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drh9cbe6352005-11-29 03:13:21 +000097 int h; /* The file descriptor */
98 unsigned char locktype; /* The type of lock held on this fd */
99 unsigned char isOpen; /* True if needs to be closed */
drh9cbe6352005-11-29 03:13:21 +0000100 int dirfd; /* File descriptor for the directory */
drhd677b3d2007-08-20 22:48:41 +0000101#if SQLITE_THREADSAFE
danielk1977ad94b582007-08-20 06:44:22 +0000102 pthread_t tid; /* The thread that "owns" this unixFile */
drh9cbe6352005-11-29 03:13:21 +0000103#endif
104};
105
drh0ccebe72005-06-07 22:22:50 +0000106/*
drh198bf392006-01-06 21:52:49 +0000107** Include code that is common to all os_*.c files
108*/
109#include "os_common.h"
110
111/*
drh0ccebe72005-06-07 22:22:50 +0000112** Do not include any of the File I/O interface procedures if the
drhf1a221e2006-01-15 17:27:17 +0000113** SQLITE_OMIT_DISKIO macro is defined (indicating that the database
drh0ccebe72005-06-07 22:22:50 +0000114** will be in-memory only)
115*/
116#ifndef SQLITE_OMIT_DISKIO
117
drh0ccebe72005-06-07 22:22:50 +0000118/*
119** Define various macros that are missing from some systems.
120*/
drhbbd42a62004-05-22 17:41:58 +0000121#ifndef O_LARGEFILE
122# define O_LARGEFILE 0
123#endif
124#ifdef SQLITE_DISABLE_LFS
125# undef O_LARGEFILE
126# define O_LARGEFILE 0
127#endif
128#ifndef O_NOFOLLOW
129# define O_NOFOLLOW 0
130#endif
131#ifndef O_BINARY
132# define O_BINARY 0
133#endif
134
135/*
136** The DJGPP compiler environment looks mostly like Unix, but it
137** lacks the fcntl() system call. So redefine fcntl() to be something
138** that always succeeds. This means that locking does not occur under
danielk197726c5d792005-11-25 09:01:23 +0000139** DJGPP. But it's DOS - what did you expect?
drhbbd42a62004-05-22 17:41:58 +0000140*/
141#ifdef __DJGPP__
142# define fcntl(A,B,C) 0
143#endif
144
145/*
drh2b4b5962005-06-15 17:47:55 +0000146** The threadid macro resolves to the thread-id or to 0. Used for
147** testing and debugging only.
148*/
drhd677b3d2007-08-20 22:48:41 +0000149#if SQLITE_THREADSAFE
drh2b4b5962005-06-15 17:47:55 +0000150#define threadid pthread_self()
151#else
152#define threadid 0
153#endif
154
155/*
danielk1977ad94b582007-08-20 06:44:22 +0000156** Set or check the unixFile.tid field. This field is set when an unixFile
157** is first opened. All subsequent uses of the unixFile verify that the
158** same thread is operating on the unixFile. Some operating systems do
drh2b4b5962005-06-15 17:47:55 +0000159** not allow locks to be overridden by other threads and that restriction
160** means that sqlite3* database handles cannot be moved from one thread
161** to another. This logic makes sure a user does not try to do that
162** by mistake.
drhf1a221e2006-01-15 17:27:17 +0000163**
danielk1977ad94b582007-08-20 06:44:22 +0000164** Version 3.3.1 (2006-01-15): unixFile can be moved from one thread to
drhf1a221e2006-01-15 17:27:17 +0000165** another as long as we are running on a system that supports threads
166** overriding each others locks (which now the most common behavior)
danielk1977ad94b582007-08-20 06:44:22 +0000167** or if no locks are held. But the unixFile.pLock field needs to be
drhf1a221e2006-01-15 17:27:17 +0000168** recomputed because its key includes the thread-id. See the
169** transferOwnership() function below for additional information
drh2b4b5962005-06-15 17:47:55 +0000170*/
drhd677b3d2007-08-20 22:48:41 +0000171#if SQLITE_THREADSAFE
drh9cbe6352005-11-29 03:13:21 +0000172# define SET_THREADID(X) (X)->tid = pthread_self()
drh029b44b2006-01-15 00:13:15 +0000173# define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
174 !pthread_equal((X)->tid, pthread_self()))
drh2b4b5962005-06-15 17:47:55 +0000175#else
176# define SET_THREADID(X)
177# define CHECK_THREADID(X) 0
danielk197713adf8a2004-06-03 16:08:41 +0000178#endif
179
drhbbd42a62004-05-22 17:41:58 +0000180/*
181** Here is the dirt on POSIX advisory locks: ANSI STD 1003.1 (1996)
182** section 6.5.2.2 lines 483 through 490 specify that when a process
183** sets or clears a lock, that operation overrides any prior locks set
184** by the same process. It does not explicitly say so, but this implies
185** that it overrides locks set by the same process using a different
186** file descriptor. Consider this test case:
187**
188** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
189** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
190**
191** Suppose ./file1 and ./file2 are really the same file (because
192** one is a hard or symbolic link to the other) then if you set
193** an exclusive lock on fd1, then try to get an exclusive lock
194** on fd2, it works. I would have expected the second lock to
195** fail since there was already a lock on the file due to fd1.
196** But not so. Since both locks came from the same process, the
197** second overrides the first, even though they were on different
198** file descriptors opened on different file names.
199**
200** Bummer. If you ask me, this is broken. Badly broken. It means
201** that we cannot use POSIX locks to synchronize file access among
202** competing threads of the same process. POSIX locks will work fine
203** to synchronize access for threads in separate processes, but not
204** threads within the same process.
205**
206** To work around the problem, SQLite has to manage file locks internally
207** on its own. Whenever a new database is opened, we have to find the
208** specific inode of the database file (the inode is determined by the
209** st_dev and st_ino fields of the stat structure that fstat() fills in)
210** and check for locks already existing on that inode. When locks are
211** created or removed, we have to look at our own internal record of the
212** locks to see if another thread has previously set a lock on that same
213** inode.
214**
danielk1977ad94b582007-08-20 06:44:22 +0000215** The sqlite3_file structure for POSIX is no longer just an integer file
drhbbd42a62004-05-22 17:41:58 +0000216** descriptor. It is now a structure that holds the integer file
217** descriptor and a pointer to a structure that describes the internal
218** locks on the corresponding inode. There is one locking structure
danielk1977ad94b582007-08-20 06:44:22 +0000219** per inode, so if the same inode is opened twice, both unixFile structures
drhbbd42a62004-05-22 17:41:58 +0000220** point to the same locking structure. The locking structure keeps
221** a reference count (so we will know when to delete it) and a "cnt"
222** field that tells us its internal lock status. cnt==0 means the
223** file is unlocked. cnt==-1 means the file has an exclusive lock.
224** cnt>0 means there are cnt shared locks on the file.
225**
226** Any attempt to lock or unlock a file first checks the locking
227** structure. The fcntl() system call is only invoked to set a
228** POSIX lock if the internal lock structure transitions between
229** a locked and an unlocked state.
230**
231** 2004-Jan-11:
232** More recent discoveries about POSIX advisory locks. (The more
233** I discover, the more I realize the a POSIX advisory locks are
234** an abomination.)
235**
236** If you close a file descriptor that points to a file that has locks,
237** all locks on that file that are owned by the current process are
danielk1977ad94b582007-08-20 06:44:22 +0000238** released. To work around this problem, each unixFile structure contains
drhbbd42a62004-05-22 17:41:58 +0000239** a pointer to an openCnt structure. There is one openCnt structure
danielk1977ad94b582007-08-20 06:44:22 +0000240** per open inode, which means that multiple unixFile can point to a single
241** openCnt. When an attempt is made to close an unixFile, if there are
242** other unixFile open on the same inode that are holding locks, the call
drhbbd42a62004-05-22 17:41:58 +0000243** to close() the file descriptor is deferred until all of the locks clear.
244** The openCnt structure keeps a list of file descriptors that need to
245** be closed and that list is walked (and cleared) when the last lock
246** clears.
247**
248** First, under Linux threads, because each thread has a separate
249** process ID, lock operations in one thread do not override locks
250** to the same file in other threads. Linux threads behave like
251** separate processes in this respect. But, if you close a file
252** descriptor in linux threads, all locks are cleared, even locks
253** on other threads and even though the other threads have different
254** process IDs. Linux threads is inconsistent in this respect.
255** (I'm beginning to think that linux threads is an abomination too.)
256** The consequence of this all is that the hash table for the lockInfo
257** structure has to include the process id as part of its key because
258** locks in different threads are treated as distinct. But the
259** openCnt structure should not include the process id in its
260** key because close() clears lock on all threads, not just the current
261** thread. Were it not for this goofiness in linux threads, we could
262** combine the lockInfo and openCnt structures into a single structure.
drh5fdae772004-06-29 03:29:00 +0000263**
264** 2004-Jun-28:
265** On some versions of linux, threads can override each others locks.
266** On others not. Sometimes you can change the behavior on the same
267** system by setting the LD_ASSUME_KERNEL environment variable. The
268** POSIX standard is silent as to which behavior is correct, as far
269** as I can tell, so other versions of unix might show the same
270** inconsistency. There is no little doubt in my mind that posix
271** advisory locks and linux threads are profoundly broken.
272**
273** To work around the inconsistencies, we have to test at runtime
274** whether or not threads can override each others locks. This test
275** is run once, the first time any lock is attempted. A static
276** variable is set to record the results of this test for future
277** use.
drhbbd42a62004-05-22 17:41:58 +0000278*/
279
280/*
281** An instance of the following structure serves as the key used
drh5fdae772004-06-29 03:29:00 +0000282** to locate a particular lockInfo structure given its inode.
283**
284** If threads cannot override each others locks, then we set the
285** lockKey.tid field to the thread ID. If threads can override
drhf1a221e2006-01-15 17:27:17 +0000286** each others locks then tid is always set to zero. tid is omitted
287** if we compile without threading support.
drhbbd42a62004-05-22 17:41:58 +0000288*/
289struct lockKey {
drh5fdae772004-06-29 03:29:00 +0000290 dev_t dev; /* Device number */
291 ino_t ino; /* Inode number */
drhd677b3d2007-08-20 22:48:41 +0000292#if SQLITE_THREADSAFE
drhd9cb6ac2005-10-20 07:28:17 +0000293 pthread_t tid; /* Thread ID or zero if threads can override each other */
drh5fdae772004-06-29 03:29:00 +0000294#endif
drhbbd42a62004-05-22 17:41:58 +0000295};
296
297/*
298** An instance of the following structure is allocated for each open
299** inode on each thread with a different process ID. (Threads have
300** different process IDs on linux, but not on most other unixes.)
301**
danielk1977ad94b582007-08-20 06:44:22 +0000302** A single inode can have multiple file descriptors, so each unixFile
drhbbd42a62004-05-22 17:41:58 +0000303** structure contains a pointer to an instance of this object and this
danielk1977ad94b582007-08-20 06:44:22 +0000304** object keeps a count of the number of unixFile pointing to it.
drhbbd42a62004-05-22 17:41:58 +0000305*/
306struct lockInfo {
307 struct lockKey key; /* The lookup key */
drh2ac3ee92004-06-07 16:27:46 +0000308 int cnt; /* Number of SHARED locks held */
danielk19779a1d0ab2004-06-01 14:09:28 +0000309 int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
drhbbd42a62004-05-22 17:41:58 +0000310 int nRef; /* Number of pointers to this structure */
311};
312
313/*
314** An instance of the following structure serves as the key used
315** to locate a particular openCnt structure given its inode. This
drh5fdae772004-06-29 03:29:00 +0000316** is the same as the lockKey except that the thread ID is omitted.
drhbbd42a62004-05-22 17:41:58 +0000317*/
318struct openKey {
319 dev_t dev; /* Device number */
320 ino_t ino; /* Inode number */
321};
322
323/*
324** An instance of the following structure is allocated for each open
325** inode. This structure keeps track of the number of locks on that
326** inode. If a close is attempted against an inode that is holding
327** locks, the close is deferred until all locks clear by adding the
328** file descriptor to be closed to the pending list.
329*/
330struct openCnt {
331 struct openKey key; /* The lookup key */
332 int nRef; /* Number of pointers to this structure */
333 int nLock; /* Number of outstanding locks */
334 int nPending; /* Number of pending close() operations */
335 int *aPending; /* Malloced space holding fd's awaiting a close() */
336};
337
338/*
drhf1a221e2006-01-15 17:27:17 +0000339** These hash tables map inodes and file descriptors (really, lockKey and
340** openKey structures) into lockInfo and openCnt structures. Access to
341** these hash tables must be protected by a mutex.
drhbbd42a62004-05-22 17:41:58 +0000342*/
drh17435752007-08-16 04:30:38 +0000343static Hash lockHash = {SQLITE_HASH_BINARY, 0, 0, 0, 0, 0};
344static Hash openHash = {SQLITE_HASH_BINARY, 0, 0, 0, 0, 0};
drh5fdae772004-06-29 03:29:00 +0000345
drhbfe66312006-10-03 17:40:40 +0000346#ifdef SQLITE_ENABLE_LOCKING_STYLE
347/*
348** The locking styles are associated with the different file locking
349** capabilities supported by different file systems.
350**
351** POSIX locking style fully supports shared and exclusive byte-range locks
352** ADP locking only supports exclusive byte-range locks
353** FLOCK only supports a single file-global exclusive lock
354** DOTLOCK isn't a true locking style, it refers to the use of a special
355** file named the same as the database file with a '.lock' extension, this
356** can be used on file systems that do not offer any reliable file locking
357** NO locking means that no locking will be attempted, this is only used for
358** read-only file systems currently
359** UNSUPPORTED means that no locking will be attempted, this is only used for
360** file systems that are known to be unsupported
361*/
362typedef enum {
drhfd131da2007-08-07 17:13:03 +0000363 posixLockingStyle = 0, /* standard posix-advisory locks */
364 afpLockingStyle, /* use afp locks */
365 flockLockingStyle, /* use flock() */
366 dotlockLockingStyle, /* use <file>.lock files */
367 noLockingStyle, /* useful for read-only file system */
368 unsupportedLockingStyle /* indicates unsupported file system */
drhbfe66312006-10-03 17:40:40 +0000369} sqlite3LockingStyle;
370#endif /* SQLITE_ENABLE_LOCKING_STYLE */
371
danielk1977ad94b582007-08-20 06:44:22 +0000372/*
373** Helper functions to obtain and relinquish the global mutex.
374*/
danielk1977b4b47412007-08-17 15:53:36 +0000375static void enterMutex(){
drh51fc3472007-08-21 13:51:23 +0000376 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
danielk1977b4b47412007-08-17 15:53:36 +0000377}
378static void leaveMutex(){
drh51fc3472007-08-21 13:51:23 +0000379 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
danielk1977b4b47412007-08-17 15:53:36 +0000380}
381
drhd677b3d2007-08-20 22:48:41 +0000382#if SQLITE_THREADSAFE
drh5fdae772004-06-29 03:29:00 +0000383/*
384** This variable records whether or not threads can override each others
385** locks.
386**
387** 0: No. Threads cannot override each others locks.
388** 1: Yes. Threads can override each others locks.
389** -1: We don't know yet.
drhf1a221e2006-01-15 17:27:17 +0000390**
drh5062d3a2006-01-31 23:03:35 +0000391** On some systems, we know at compile-time if threads can override each
392** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro
393** will be set appropriately. On other systems, we have to check at
394** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is
395** undefined.
396**
drhf1a221e2006-01-15 17:27:17 +0000397** This variable normally has file scope only. But during testing, we make
398** it a global so that the test code can change its value in order to verify
399** that the right stuff happens in either case.
drh5fdae772004-06-29 03:29:00 +0000400*/
drh5062d3a2006-01-31 23:03:35 +0000401#ifndef SQLITE_THREAD_OVERRIDE_LOCK
402# define SQLITE_THREAD_OVERRIDE_LOCK -1
403#endif
drh029b44b2006-01-15 00:13:15 +0000404#ifdef SQLITE_TEST
drh5062d3a2006-01-31 23:03:35 +0000405int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh029b44b2006-01-15 00:13:15 +0000406#else
drh5062d3a2006-01-31 23:03:35 +0000407static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh029b44b2006-01-15 00:13:15 +0000408#endif
drh5fdae772004-06-29 03:29:00 +0000409
410/*
411** This structure holds information passed into individual test
412** threads by the testThreadLockingBehavior() routine.
413*/
414struct threadTestData {
415 int fd; /* File to be locked */
416 struct flock lock; /* The locking operation */
417 int result; /* Result of the locking operation */
418};
419
drh2b4b5962005-06-15 17:47:55 +0000420#ifdef SQLITE_LOCK_TRACE
421/*
422** Print out information about all locking operations.
423**
424** This routine is used for troubleshooting locks on multithreaded
425** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
426** command-line option on the compiler. This code is normally
drhf1a221e2006-01-15 17:27:17 +0000427** turned off.
drh2b4b5962005-06-15 17:47:55 +0000428*/
429static int lockTrace(int fd, int op, struct flock *p){
430 char *zOpName, *zType;
431 int s;
432 int savedErrno;
433 if( op==F_GETLK ){
434 zOpName = "GETLK";
435 }else if( op==F_SETLK ){
436 zOpName = "SETLK";
437 }else{
438 s = fcntl(fd, op, p);
439 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
440 return s;
441 }
442 if( p->l_type==F_RDLCK ){
443 zType = "RDLCK";
444 }else if( p->l_type==F_WRLCK ){
445 zType = "WRLCK";
446 }else if( p->l_type==F_UNLCK ){
447 zType = "UNLCK";
448 }else{
449 assert( 0 );
450 }
451 assert( p->l_whence==SEEK_SET );
452 s = fcntl(fd, op, p);
453 savedErrno = errno;
454 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
455 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
456 (int)p->l_pid, s);
drhe2396a12007-03-29 20:19:58 +0000457 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
drh2b4b5962005-06-15 17:47:55 +0000458 struct flock l2;
459 l2 = *p;
460 fcntl(fd, F_GETLK, &l2);
461 if( l2.l_type==F_RDLCK ){
462 zType = "RDLCK";
463 }else if( l2.l_type==F_WRLCK ){
464 zType = "WRLCK";
465 }else if( l2.l_type==F_UNLCK ){
466 zType = "UNLCK";
467 }else{
468 assert( 0 );
469 }
470 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
471 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
472 }
473 errno = savedErrno;
474 return s;
475}
476#define fcntl lockTrace
477#endif /* SQLITE_LOCK_TRACE */
478
drh5fdae772004-06-29 03:29:00 +0000479/*
480** The testThreadLockingBehavior() routine launches two separate
481** threads on this routine. This routine attempts to lock a file
482** descriptor then returns. The success or failure of that attempt
483** allows the testThreadLockingBehavior() procedure to determine
484** whether or not threads can override each others locks.
485*/
486static void *threadLockingTest(void *pArg){
487 struct threadTestData *pData = (struct threadTestData*)pArg;
488 pData->result = fcntl(pData->fd, F_SETLK, &pData->lock);
489 return pArg;
490}
491
492/*
493** This procedure attempts to determine whether or not threads
494** can override each others locks then sets the
495** threadsOverrideEachOthersLocks variable appropriately.
496*/
danielk19774d5238f2006-01-27 06:32:00 +0000497static void testThreadLockingBehavior(int fd_orig){
drh5fdae772004-06-29 03:29:00 +0000498 int fd;
499 struct threadTestData d[2];
500 pthread_t t[2];
501
502 fd = dup(fd_orig);
503 if( fd<0 ) return;
504 memset(d, 0, sizeof(d));
505 d[0].fd = fd;
506 d[0].lock.l_type = F_RDLCK;
507 d[0].lock.l_len = 1;
508 d[0].lock.l_start = 0;
509 d[0].lock.l_whence = SEEK_SET;
510 d[1] = d[0];
511 d[1].lock.l_type = F_WRLCK;
512 pthread_create(&t[0], 0, threadLockingTest, &d[0]);
513 pthread_create(&t[1], 0, threadLockingTest, &d[1]);
514 pthread_join(t[0], 0);
515 pthread_join(t[1], 0);
516 close(fd);
517 threadsOverrideEachOthersLocks = d[0].result==0 && d[1].result==0;
518}
drhd677b3d2007-08-20 22:48:41 +0000519#endif /* SQLITE_THREADSAFE */
drh5fdae772004-06-29 03:29:00 +0000520
drhbbd42a62004-05-22 17:41:58 +0000521/*
522** Release a lockInfo structure previously allocated by findLockInfo().
523*/
524static void releaseLockInfo(struct lockInfo *pLock){
drhbfe66312006-10-03 17:40:40 +0000525 if (pLock == NULL)
526 return;
drhbbd42a62004-05-22 17:41:58 +0000527 pLock->nRef--;
528 if( pLock->nRef==0 ){
529 sqlite3HashInsert(&lockHash, &pLock->key, sizeof(pLock->key), 0);
drh17435752007-08-16 04:30:38 +0000530 sqlite3_free(pLock);
drhbbd42a62004-05-22 17:41:58 +0000531 }
532}
533
534/*
535** Release a openCnt structure previously allocated by findLockInfo().
536*/
537static void releaseOpenCnt(struct openCnt *pOpen){
drhbfe66312006-10-03 17:40:40 +0000538 if (pOpen == NULL)
539 return;
drhbbd42a62004-05-22 17:41:58 +0000540 pOpen->nRef--;
541 if( pOpen->nRef==0 ){
542 sqlite3HashInsert(&openHash, &pOpen->key, sizeof(pOpen->key), 0);
drh64b1bea2006-01-15 02:30:57 +0000543 free(pOpen->aPending);
drh17435752007-08-16 04:30:38 +0000544 sqlite3_free(pOpen);
drhbbd42a62004-05-22 17:41:58 +0000545 }
546}
547
drhbfe66312006-10-03 17:40:40 +0000548#ifdef SQLITE_ENABLE_LOCKING_STYLE
549/*
550** Tests a byte-range locking query to see if byte range locks are
551** supported, if not we fall back to dotlockLockingStyle.
552*/
danielk1977ad94b582007-08-20 06:44:22 +0000553static sqlite3LockingStyle sqlite3TestLockingStyle(
554 const char *filePath,
555 int fd
556){
drhbfe66312006-10-03 17:40:40 +0000557 /* test byte-range lock using fcntl */
558 struct flock lockInfo;
559
560 lockInfo.l_len = 1;
561 lockInfo.l_start = 0;
562 lockInfo.l_whence = SEEK_SET;
563 lockInfo.l_type = F_RDLCK;
564
danielk1977ad94b582007-08-20 06:44:22 +0000565 if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
drhbfe66312006-10-03 17:40:40 +0000566 return posixLockingStyle;
567 }
568
569 /* testing for flock can give false positives. So if if the above test
570 ** fails, then we fall back to using dot-lock style locking.
571 */
572 return dotlockLockingStyle;
573}
574
575/*
576** Examines the f_fstypename entry in the statfs structure as returned by
577** stat() for the file system hosting the database file, assigns the
578** appropriate locking style based on it's value. These values and
579** assignments are based on Darwin/OSX behavior and have not been tested on
580** other systems.
581*/
danielk1977ad94b582007-08-20 06:44:22 +0000582static sqlite3LockingStyle sqlite3DetectLockingStyle(
583 const char *filePath,
584 int fd
585){
drhbfe66312006-10-03 17:40:40 +0000586
587#ifdef SQLITE_FIXED_LOCKING_STYLE
588 return (sqlite3LockingStyle)SQLITE_FIXED_LOCKING_STYLE;
589#else
590 struct statfs fsInfo;
591
592 if (statfs(filePath, &fsInfo) == -1)
593 return sqlite3TestLockingStyle(filePath, fd);
594
595 if (fsInfo.f_flags & MNT_RDONLY)
596 return noLockingStyle;
597
598 if( (!strcmp(fsInfo.f_fstypename, "hfs")) ||
599 (!strcmp(fsInfo.f_fstypename, "ufs")) )
drhfd131da2007-08-07 17:13:03 +0000600 return posixLockingStyle;
drhbfe66312006-10-03 17:40:40 +0000601
602 if(!strcmp(fsInfo.f_fstypename, "afpfs"))
603 return afpLockingStyle;
604
605 if(!strcmp(fsInfo.f_fstypename, "nfs"))
606 return sqlite3TestLockingStyle(filePath, fd);
607
608 if(!strcmp(fsInfo.f_fstypename, "smbfs"))
609 return flockLockingStyle;
610
611 if(!strcmp(fsInfo.f_fstypename, "msdos"))
612 return dotlockLockingStyle;
613
614 if(!strcmp(fsInfo.f_fstypename, "webdav"))
615 return unsupportedLockingStyle;
616
617 return sqlite3TestLockingStyle(filePath, fd);
drh3b62b2f2007-06-08 18:27:03 +0000618#endif /* SQLITE_FIXED_LOCKING_STYLE */
drhbfe66312006-10-03 17:40:40 +0000619}
620
621#endif /* SQLITE_ENABLE_LOCKING_STYLE */
622
drhbbd42a62004-05-22 17:41:58 +0000623/*
624** Given a file descriptor, locate lockInfo and openCnt structures that
drh029b44b2006-01-15 00:13:15 +0000625** describes that file descriptor. Create new ones if necessary. The
626** return values might be uninitialized if an error occurs.
drhbbd42a62004-05-22 17:41:58 +0000627**
628** Return the number of errors.
629*/
drh38f82712004-06-18 17:10:16 +0000630static int findLockInfo(
drhbbd42a62004-05-22 17:41:58 +0000631 int fd, /* The file descriptor used in the key */
632 struct lockInfo **ppLock, /* Return the lockInfo structure here */
drh5fdae772004-06-29 03:29:00 +0000633 struct openCnt **ppOpen /* Return the openCnt structure here */
drhbbd42a62004-05-22 17:41:58 +0000634){
635 int rc;
636 struct lockKey key1;
637 struct openKey key2;
638 struct stat statbuf;
639 struct lockInfo *pLock;
640 struct openCnt *pOpen;
641 rc = fstat(fd, &statbuf);
642 if( rc!=0 ) return 1;
danielk1977441b09a2006-01-05 13:48:29 +0000643
drhbbd42a62004-05-22 17:41:58 +0000644 memset(&key1, 0, sizeof(key1));
645 key1.dev = statbuf.st_dev;
646 key1.ino = statbuf.st_ino;
drhd677b3d2007-08-20 22:48:41 +0000647#if SQLITE_THREADSAFE
drh5fdae772004-06-29 03:29:00 +0000648 if( threadsOverrideEachOthersLocks<0 ){
649 testThreadLockingBehavior(fd);
650 }
651 key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
652#endif
drhbbd42a62004-05-22 17:41:58 +0000653 memset(&key2, 0, sizeof(key2));
654 key2.dev = statbuf.st_dev;
655 key2.ino = statbuf.st_ino;
656 pLock = (struct lockInfo*)sqlite3HashFind(&lockHash, &key1, sizeof(key1));
657 if( pLock==0 ){
658 struct lockInfo *pOld;
drh17435752007-08-16 04:30:38 +0000659 pLock = sqlite3_malloc( sizeof(*pLock) );
danielk1977441b09a2006-01-05 13:48:29 +0000660 if( pLock==0 ){
661 rc = 1;
662 goto exit_findlockinfo;
663 }
drhbbd42a62004-05-22 17:41:58 +0000664 pLock->key = key1;
665 pLock->nRef = 1;
666 pLock->cnt = 0;
danielk19779a1d0ab2004-06-01 14:09:28 +0000667 pLock->locktype = 0;
drhbbd42a62004-05-22 17:41:58 +0000668 pOld = sqlite3HashInsert(&lockHash, &pLock->key, sizeof(key1), pLock);
669 if( pOld!=0 ){
670 assert( pOld==pLock );
drh17435752007-08-16 04:30:38 +0000671 sqlite3_free(pLock);
danielk1977441b09a2006-01-05 13:48:29 +0000672 rc = 1;
673 goto exit_findlockinfo;
drhbbd42a62004-05-22 17:41:58 +0000674 }
675 }else{
676 pLock->nRef++;
677 }
678 *ppLock = pLock;
drh029b44b2006-01-15 00:13:15 +0000679 if( ppOpen!=0 ){
680 pOpen = (struct openCnt*)sqlite3HashFind(&openHash, &key2, sizeof(key2));
drhbbd42a62004-05-22 17:41:58 +0000681 if( pOpen==0 ){
drh029b44b2006-01-15 00:13:15 +0000682 struct openCnt *pOld;
drh17435752007-08-16 04:30:38 +0000683 pOpen = sqlite3_malloc( sizeof(*pOpen) );
drh029b44b2006-01-15 00:13:15 +0000684 if( pOpen==0 ){
685 releaseLockInfo(pLock);
686 rc = 1;
687 goto exit_findlockinfo;
688 }
689 pOpen->key = key2;
690 pOpen->nRef = 1;
691 pOpen->nLock = 0;
692 pOpen->nPending = 0;
693 pOpen->aPending = 0;
694 pOld = sqlite3HashInsert(&openHash, &pOpen->key, sizeof(key2), pOpen);
695 if( pOld!=0 ){
696 assert( pOld==pOpen );
drh17435752007-08-16 04:30:38 +0000697 sqlite3_free(pOpen);
drh029b44b2006-01-15 00:13:15 +0000698 releaseLockInfo(pLock);
699 rc = 1;
700 goto exit_findlockinfo;
701 }
702 }else{
703 pOpen->nRef++;
drhbbd42a62004-05-22 17:41:58 +0000704 }
drh029b44b2006-01-15 00:13:15 +0000705 *ppOpen = pOpen;
drhbbd42a62004-05-22 17:41:58 +0000706 }
danielk1977441b09a2006-01-05 13:48:29 +0000707
708exit_findlockinfo:
danielk1977441b09a2006-01-05 13:48:29 +0000709 return rc;
drhbbd42a62004-05-22 17:41:58 +0000710}
711
drh64b1bea2006-01-15 02:30:57 +0000712#ifdef SQLITE_DEBUG
713/*
714** Helper function for printing out trace information from debugging
715** binaries. This returns the string represetation of the supplied
716** integer lock-type.
717*/
718static const char *locktypeName(int locktype){
719 switch( locktype ){
720 case NO_LOCK: return "NONE";
721 case SHARED_LOCK: return "SHARED";
722 case RESERVED_LOCK: return "RESERVED";
723 case PENDING_LOCK: return "PENDING";
724 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
725 }
726 return "ERROR";
727}
728#endif
729
drhbbd42a62004-05-22 17:41:58 +0000730/*
drh029b44b2006-01-15 00:13:15 +0000731** If we are currently in a different thread than the thread that the
732** unixFile argument belongs to, then transfer ownership of the unixFile
733** over to the current thread.
734**
735** A unixFile is only owned by a thread on systems where one thread is
736** unable to override locks created by a different thread. RedHat9 is
737** an example of such a system.
738**
739** Ownership transfer is only allowed if the unixFile is currently unlocked.
740** If the unixFile is locked and an ownership is wrong, then return
drhf1a221e2006-01-15 17:27:17 +0000741** SQLITE_MISUSE. SQLITE_OK is returned if everything works.
drh029b44b2006-01-15 00:13:15 +0000742*/
drhd677b3d2007-08-20 22:48:41 +0000743#if SQLITE_THREADSAFE
drh029b44b2006-01-15 00:13:15 +0000744static int transferOwnership(unixFile *pFile){
drh64b1bea2006-01-15 02:30:57 +0000745 int rc;
drh029b44b2006-01-15 00:13:15 +0000746 pthread_t hSelf;
747 if( threadsOverrideEachOthersLocks ){
748 /* Ownership transfers not needed on this system */
749 return SQLITE_OK;
750 }
751 hSelf = pthread_self();
752 if( pthread_equal(pFile->tid, hSelf) ){
753 /* We are still in the same thread */
drh4f0c5872007-03-26 22:05:01 +0000754 OSTRACE1("No-transfer, same thread\n");
drh029b44b2006-01-15 00:13:15 +0000755 return SQLITE_OK;
756 }
757 if( pFile->locktype!=NO_LOCK ){
758 /* We cannot change ownership while we are holding a lock! */
759 return SQLITE_MISUSE;
760 }
drh4f0c5872007-03-26 22:05:01 +0000761 OSTRACE4("Transfer ownership of %d from %d to %d\n",
762 pFile->h, pFile->tid, hSelf);
drh029b44b2006-01-15 00:13:15 +0000763 pFile->tid = hSelf;
drhbfe66312006-10-03 17:40:40 +0000764 if (pFile->pLock != NULL) {
765 releaseLockInfo(pFile->pLock);
766 rc = findLockInfo(pFile->h, &pFile->pLock, 0);
drh4f0c5872007-03-26 22:05:01 +0000767 OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +0000768 locktypeName(pFile->locktype),
769 locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
770 return rc;
771 } else {
772 return SQLITE_OK;
773 }
drh029b44b2006-01-15 00:13:15 +0000774}
775#else
drhf1a221e2006-01-15 17:27:17 +0000776 /* On single-threaded builds, ownership transfer is a no-op */
drh029b44b2006-01-15 00:13:15 +0000777# define transferOwnership(X) SQLITE_OK
778#endif
779
780/*
danielk19772a6bdf62007-08-20 16:07:00 +0000781** Seek to the offset passed as the second argument, then read cnt
782** bytes into pBuf. Return the number of bytes actually read.
drhb912b282006-03-23 22:42:20 +0000783*/
danielk197762079062007-08-15 17:08:46 +0000784static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
drhb912b282006-03-23 22:42:20 +0000785 int got;
drh8ebf6702007-02-06 11:11:08 +0000786 i64 newOffset;
drh15d00c42007-02-27 02:01:14 +0000787 TIMER_START;
drh8350a212007-03-22 15:22:06 +0000788#if defined(USE_PREAD)
danielk197762079062007-08-15 17:08:46 +0000789 got = pread(id->h, pBuf, cnt, offset);
drhbb5f18d2007-04-06 18:23:17 +0000790 SimulateIOError( got = -1 );
drh8350a212007-03-22 15:22:06 +0000791#elif defined(USE_PREAD64)
danielk197762079062007-08-15 17:08:46 +0000792 got = pread64(id->h, pBuf, cnt, offset);
drhbb5f18d2007-04-06 18:23:17 +0000793 SimulateIOError( got = -1 );
drhb912b282006-03-23 22:42:20 +0000794#else
danielk197762079062007-08-15 17:08:46 +0000795 newOffset = lseek(id->h, offset, SEEK_SET);
drhbb5f18d2007-04-06 18:23:17 +0000796 SimulateIOError( newOffset-- );
danielk197762079062007-08-15 17:08:46 +0000797 if( newOffset!=offset ){
drh8ebf6702007-02-06 11:11:08 +0000798 return -1;
799 }
drhb912b282006-03-23 22:42:20 +0000800 got = read(id->h, pBuf, cnt);
801#endif
drh15d00c42007-02-27 02:01:14 +0000802 TIMER_END;
danielk1977967a4a12007-08-20 14:23:44 +0000803 OSTRACE5("READ %-3d %5d %7lld %d\n", id->h, got, offset, TIMER_ELAPSED);
drhb912b282006-03-23 22:42:20 +0000804 return got;
805}
806
807/*
drhbbd42a62004-05-22 17:41:58 +0000808** Read data from a file into a buffer. Return SQLITE_OK if all
809** bytes were read successfully and SQLITE_IOERR if anything goes
810** wrong.
811*/
danielk197762079062007-08-15 17:08:46 +0000812static int unixRead(
813 sqlite3_file *id,
814 void *pBuf,
815 int amt,
816 sqlite3_int64 offset
817){
drhbbd42a62004-05-22 17:41:58 +0000818 int got;
drh9cbe6352005-11-29 03:13:21 +0000819 assert( id );
danielk197762079062007-08-15 17:08:46 +0000820 got = seekAndRead((unixFile*)id, offset, pBuf, amt);
drhbbd42a62004-05-22 17:41:58 +0000821 if( got==amt ){
822 return SQLITE_OK;
drh4ac285a2006-09-15 07:28:50 +0000823 }else if( got<0 ){
824 return SQLITE_IOERR_READ;
drhbbd42a62004-05-22 17:41:58 +0000825 }else{
drhbafda092007-01-03 23:36:22 +0000826 memset(&((char*)pBuf)[got], 0, amt-got);
drh4ac285a2006-09-15 07:28:50 +0000827 return SQLITE_IOERR_SHORT_READ;
drhbbd42a62004-05-22 17:41:58 +0000828 }
829}
830
831/*
drhb912b282006-03-23 22:42:20 +0000832** Seek to the offset in id->offset then read cnt bytes into pBuf.
833** Return the number of bytes actually read. Update the offset.
834*/
danielk197762079062007-08-15 17:08:46 +0000835static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
drhb912b282006-03-23 22:42:20 +0000836 int got;
drh8ebf6702007-02-06 11:11:08 +0000837 i64 newOffset;
drh15d00c42007-02-27 02:01:14 +0000838 TIMER_START;
drh8350a212007-03-22 15:22:06 +0000839#if defined(USE_PREAD)
danielk197762079062007-08-15 17:08:46 +0000840 got = pwrite(id->h, pBuf, cnt, offset);
drh8350a212007-03-22 15:22:06 +0000841#elif defined(USE_PREAD64)
danielk197762079062007-08-15 17:08:46 +0000842 got = pwrite64(id->h, pBuf, cnt, offset);
drhb912b282006-03-23 22:42:20 +0000843#else
danielk197762079062007-08-15 17:08:46 +0000844 newOffset = lseek(id->h, offset, SEEK_SET);
845 if( newOffset!=offset ){
drh8ebf6702007-02-06 11:11:08 +0000846 return -1;
847 }
drhb912b282006-03-23 22:42:20 +0000848 got = write(id->h, pBuf, cnt);
849#endif
drh15d00c42007-02-27 02:01:14 +0000850 TIMER_END;
danielk197762079062007-08-15 17:08:46 +0000851 OSTRACE5("WRITE %-3d %5d %7lld %d\n", id->h, got, offset, TIMER_ELAPSED);
drhb912b282006-03-23 22:42:20 +0000852 return got;
853}
854
855
856/*
drhbbd42a62004-05-22 17:41:58 +0000857** Write data from a buffer into a file. Return SQLITE_OK on success
858** or some other error code on failure.
859*/
danielk197762079062007-08-15 17:08:46 +0000860static int unixWrite(
861 sqlite3_file *id,
862 const void *pBuf,
863 int amt,
864 sqlite3_int64 offset
865){
drhbbd42a62004-05-22 17:41:58 +0000866 int wrote = 0;
drh9cbe6352005-11-29 03:13:21 +0000867 assert( id );
drh4c7f9412005-02-03 00:29:47 +0000868 assert( amt>0 );
danielk197762079062007-08-15 17:08:46 +0000869 while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){
drhbbd42a62004-05-22 17:41:58 +0000870 amt -= wrote;
danielk197762079062007-08-15 17:08:46 +0000871 offset += wrote;
drhbbd42a62004-05-22 17:41:58 +0000872 pBuf = &((char*)pBuf)[wrote];
873 }
drh59685932006-09-14 13:47:11 +0000874 SimulateIOError(( wrote=(-1), amt=1 ));
875 SimulateDiskfullError(( wrote=0, amt=1 ));
drhbbd42a62004-05-22 17:41:58 +0000876 if( amt>0 ){
drh59685932006-09-14 13:47:11 +0000877 if( wrote<0 ){
drh4ac285a2006-09-15 07:28:50 +0000878 return SQLITE_IOERR_WRITE;
drh59685932006-09-14 13:47:11 +0000879 }else{
880 return SQLITE_FULL;
881 }
drhbbd42a62004-05-22 17:41:58 +0000882 }
883 return SQLITE_OK;
884}
885
drhb851b2c2005-03-10 14:11:12 +0000886#ifdef SQLITE_TEST
887/*
888** Count the number of fullsyncs and normal syncs. This is used to test
889** that syncs and fullsyncs are occuring at the right times.
890*/
891int sqlite3_sync_count = 0;
892int sqlite3_fullsync_count = 0;
893#endif
894
drhf2f23912005-10-05 10:29:36 +0000895/*
896** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.
897** Otherwise use fsync() in its place.
898*/
899#ifndef HAVE_FDATASYNC
900# define fdatasync fsync
901#endif
902
drhac530b12006-02-11 01:25:50 +0000903/*
904** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
905** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
906** only available on Mac OS X. But that could change.
907*/
908#ifdef F_FULLFSYNC
909# define HAVE_FULLFSYNC 1
910#else
911# define HAVE_FULLFSYNC 0
912#endif
913
drhb851b2c2005-03-10 14:11:12 +0000914
drhbbd42a62004-05-22 17:41:58 +0000915/*
drhdd809b02004-07-17 21:44:57 +0000916** The fsync() system call does not work as advertised on many
917** unix systems. The following procedure is an attempt to make
918** it work better.
drh1398ad32005-01-19 23:24:50 +0000919**
920** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
921** for testing when we want to run through the test suite quickly.
922** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
923** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
924** or power failure will likely corrupt the database file.
drhdd809b02004-07-17 21:44:57 +0000925*/
drheb796a72005-09-08 12:38:41 +0000926static int full_fsync(int fd, int fullSync, int dataOnly){
drhdd809b02004-07-17 21:44:57 +0000927 int rc;
drhb851b2c2005-03-10 14:11:12 +0000928
929 /* Record the number of times that we do a normal fsync() and
930 ** FULLSYNC. This is used during testing to verify that this procedure
931 ** gets called with the correct arguments.
932 */
933#ifdef SQLITE_TEST
934 if( fullSync ) sqlite3_fullsync_count++;
935 sqlite3_sync_count++;
936#endif
937
938 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
939 ** no-op
940 */
941#ifdef SQLITE_NO_SYNC
942 rc = SQLITE_OK;
943#else
944
drhac530b12006-02-11 01:25:50 +0000945#if HAVE_FULLFSYNC
drhb851b2c2005-03-10 14:11:12 +0000946 if( fullSync ){
drhf30cc942005-03-11 17:52:34 +0000947 rc = fcntl(fd, F_FULLFSYNC, 0);
aswiftae0943b2007-01-31 23:37:07 +0000948 }else{
949 rc = 1;
950 }
951 /* If the FULLFSYNC failed, fall back to attempting an fsync().
952 * It shouldn't be possible for fullfsync to fail on the local
953 * file system (on OSX), so failure indicates that FULLFSYNC
954 * isn't supported for this file system. So, attempt an fsync
955 * and (for now) ignore the overhead of a superfluous fcntl call.
956 * It'd be better to detect fullfsync support once and avoid
957 * the fcntl call every time sync is called.
958 */
959 if( rc ) rc = fsync(fd);
960
961#else
drheb796a72005-09-08 12:38:41 +0000962 if( dataOnly ){
963 rc = fdatasync(fd);
drhf2f23912005-10-05 10:29:36 +0000964 }else{
drheb796a72005-09-08 12:38:41 +0000965 rc = fsync(fd);
966 }
aswiftae0943b2007-01-31 23:37:07 +0000967#endif /* HAVE_FULLFSYNC */
drhb851b2c2005-03-10 14:11:12 +0000968#endif /* defined(SQLITE_NO_SYNC) */
969
drhdd809b02004-07-17 21:44:57 +0000970 return rc;
971}
972
973/*
drhbbd42a62004-05-22 17:41:58 +0000974** Make sure all writes to a particular file are committed to disk.
975**
drheb796a72005-09-08 12:38:41 +0000976** If dataOnly==0 then both the file itself and its metadata (file
977** size, access time, etc) are synced. If dataOnly!=0 then only the
978** file data is synced.
979**
drhbbd42a62004-05-22 17:41:58 +0000980** Under Unix, also make sure that the directory entry for the file
981** has been created by fsync-ing the directory that contains the file.
982** If we do not do this and we encounter a power failure, the directory
983** entry for the journal might not exist after we reboot. The next
984** SQLite to access the file will not know that the journal exists (because
985** the directory entry for the journal was never created) and the transaction
986** will not roll back - possibly leading to database corruption.
987*/
danielk197790949c22007-08-17 16:50:38 +0000988static int unixSync(sqlite3_file *id, int flags){
drh59685932006-09-14 13:47:11 +0000989 int rc;
drh054889e2005-11-30 03:20:31 +0000990 unixFile *pFile = (unixFile*)id;
danielk197790949c22007-08-17 16:50:38 +0000991
danielk1977f036aef2007-08-20 05:36:51 +0000992 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
993 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
994
995 /* Check that one of SQLITE_SYNC_NORMAL, FULL or BARRIER was passed */
996 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
997 || (flags&0x0F)==SQLITE_SYNC_FULL
998 || (flags&0x0F)==SQLITE_SYNC_BARRIER
999 );
danielk197790949c22007-08-17 16:50:38 +00001000
drh054889e2005-11-30 03:20:31 +00001001 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001002 OSTRACE2("SYNC %-3d\n", pFile->h);
danielk197790949c22007-08-17 16:50:38 +00001003 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
drh59685932006-09-14 13:47:11 +00001004 SimulateIOError( rc=1 );
1005 if( rc ){
drh4ac285a2006-09-15 07:28:50 +00001006 return SQLITE_IOERR_FSYNC;
drhbbd42a62004-05-22 17:41:58 +00001007 }
drh054889e2005-11-30 03:20:31 +00001008 if( pFile->dirfd>=0 ){
drh4f0c5872007-03-26 22:05:01 +00001009 OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
danielk197790949c22007-08-17 16:50:38 +00001010 HAVE_FULLFSYNC, isFullsync);
danielk1977d7c03f72005-11-25 10:38:22 +00001011#ifndef SQLITE_DISABLE_DIRSYNC
drhac530b12006-02-11 01:25:50 +00001012 /* The directory sync is only attempted if full_fsync is
1013 ** turned off or unavailable. If a full_fsync occurred above,
1014 ** then the directory sync is superfluous.
1015 */
danielk197790949c22007-08-17 16:50:38 +00001016 if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
drhac530b12006-02-11 01:25:50 +00001017 /*
1018 ** We have received multiple reports of fsync() returning
drh86631a52006-02-09 23:05:51 +00001019 ** errors when applied to directories on certain file systems.
1020 ** A failed directory sync is not a big deal. So it seems
1021 ** better to ignore the error. Ticket #1657
1022 */
1023 /* return SQLITE_IOERR; */
danielk19770964b232005-11-25 08:47:57 +00001024 }
danielk1977d7c03f72005-11-25 10:38:22 +00001025#endif
drh054889e2005-11-30 03:20:31 +00001026 close(pFile->dirfd); /* Only need to sync once, so close the directory */
1027 pFile->dirfd = -1; /* when we are done. */
drha2854222004-06-17 19:04:17 +00001028 }
drha2854222004-06-17 19:04:17 +00001029 return SQLITE_OK;
drhbbd42a62004-05-22 17:41:58 +00001030}
1031
1032/*
1033** Truncate an open file to a specified size
1034*/
danielk197762079062007-08-15 17:08:46 +00001035static int unixTruncate(sqlite3_file *id, i64 nByte){
drh59685932006-09-14 13:47:11 +00001036 int rc;
drh9cbe6352005-11-29 03:13:21 +00001037 assert( id );
drh63fff5f2007-06-19 10:50:38 +00001038 rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);
drh59685932006-09-14 13:47:11 +00001039 SimulateIOError( rc=1 );
1040 if( rc ){
drh4ac285a2006-09-15 07:28:50 +00001041 return SQLITE_IOERR_TRUNCATE;
drh59685932006-09-14 13:47:11 +00001042 }else{
1043 return SQLITE_OK;
1044 }
drhbbd42a62004-05-22 17:41:58 +00001045}
1046
1047/*
1048** Determine the current size of a file in bytes
1049*/
danielk197762079062007-08-15 17:08:46 +00001050static int unixFileSize(sqlite3_file *id, i64 *pSize){
drh59685932006-09-14 13:47:11 +00001051 int rc;
drhbbd42a62004-05-22 17:41:58 +00001052 struct stat buf;
drh9cbe6352005-11-29 03:13:21 +00001053 assert( id );
drh59685932006-09-14 13:47:11 +00001054 rc = fstat(((unixFile*)id)->h, &buf);
1055 SimulateIOError( rc=1 );
1056 if( rc!=0 ){
drh4ac285a2006-09-15 07:28:50 +00001057 return SQLITE_IOERR_FSTAT;
drhbbd42a62004-05-22 17:41:58 +00001058 }
1059 *pSize = buf.st_size;
1060 return SQLITE_OK;
1061}
1062
danielk19779a1d0ab2004-06-01 14:09:28 +00001063/*
danielk197713adf8a2004-06-03 16:08:41 +00001064** This routine checks if there is a RESERVED lock held on the specified
1065** file by this or any other process. If such a lock is held, return
drh2ac3ee92004-06-07 16:27:46 +00001066** non-zero. If the file is unlocked or holds only SHARED locks, then
1067** return zero.
danielk197713adf8a2004-06-03 16:08:41 +00001068*/
danielk197762079062007-08-15 17:08:46 +00001069static int unixCheckReservedLock(sqlite3_file *id){
danielk197713adf8a2004-06-03 16:08:41 +00001070 int r = 0;
drh054889e2005-11-30 03:20:31 +00001071 unixFile *pFile = (unixFile*)id;
danielk197713adf8a2004-06-03 16:08:41 +00001072
drh054889e2005-11-30 03:20:31 +00001073 assert( pFile );
danielk1977b4b47412007-08-17 15:53:36 +00001074 enterMutex(); /* Because pFile->pLock is shared across threads */
danielk197713adf8a2004-06-03 16:08:41 +00001075
1076 /* Check if a thread in this process holds such a lock */
drh054889e2005-11-30 03:20:31 +00001077 if( pFile->pLock->locktype>SHARED_LOCK ){
danielk197713adf8a2004-06-03 16:08:41 +00001078 r = 1;
1079 }
1080
drh2ac3ee92004-06-07 16:27:46 +00001081 /* Otherwise see if some other process holds it.
danielk197713adf8a2004-06-03 16:08:41 +00001082 */
1083 if( !r ){
1084 struct flock lock;
1085 lock.l_whence = SEEK_SET;
drh2ac3ee92004-06-07 16:27:46 +00001086 lock.l_start = RESERVED_BYTE;
1087 lock.l_len = 1;
1088 lock.l_type = F_WRLCK;
drh054889e2005-11-30 03:20:31 +00001089 fcntl(pFile->h, F_GETLK, &lock);
danielk197713adf8a2004-06-03 16:08:41 +00001090 if( lock.l_type!=F_UNLCK ){
1091 r = 1;
1092 }
1093 }
1094
danielk1977b4b47412007-08-17 15:53:36 +00001095 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001096 OSTRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
danielk197713adf8a2004-06-03 16:08:41 +00001097
1098 return r;
1099}
1100
1101/*
danielk19779a1d0ab2004-06-01 14:09:28 +00001102** Lock the file with the lock specified by parameter locktype - one
1103** of the following:
1104**
drh2ac3ee92004-06-07 16:27:46 +00001105** (1) SHARED_LOCK
1106** (2) RESERVED_LOCK
1107** (3) PENDING_LOCK
1108** (4) EXCLUSIVE_LOCK
1109**
drhb3e04342004-06-08 00:47:47 +00001110** Sometimes when requesting one lock state, additional lock states
1111** are inserted in between. The locking might fail on one of the later
1112** transitions leaving the lock state different from what it started but
1113** still short of its goal. The following chart shows the allowed
1114** transitions and the inserted intermediate states:
1115**
1116** UNLOCKED -> SHARED
1117** SHARED -> RESERVED
1118** SHARED -> (PENDING) -> EXCLUSIVE
1119** RESERVED -> (PENDING) -> EXCLUSIVE
1120** PENDING -> EXCLUSIVE
drh2ac3ee92004-06-07 16:27:46 +00001121**
drha6abd042004-06-09 17:37:22 +00001122** This routine will only increase a lock. Use the sqlite3OsUnlock()
1123** routine to lower a locking level.
danielk19779a1d0ab2004-06-01 14:09:28 +00001124*/
danielk197762079062007-08-15 17:08:46 +00001125static int unixLock(sqlite3_file *id, int locktype){
danielk1977f42f25c2004-06-25 07:21:28 +00001126 /* The following describes the implementation of the various locks and
1127 ** lock transitions in terms of the POSIX advisory shared and exclusive
1128 ** lock primitives (called read-locks and write-locks below, to avoid
1129 ** confusion with SQLite lock names). The algorithms are complicated
1130 ** slightly in order to be compatible with windows systems simultaneously
1131 ** accessing the same database file, in case that is ever required.
1132 **
1133 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
1134 ** byte', each single bytes at well known offsets, and the 'shared byte
1135 ** range', a range of 510 bytes at a well known offset.
1136 **
1137 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
1138 ** byte'. If this is successful, a random byte from the 'shared byte
1139 ** range' is read-locked and the lock on the 'pending byte' released.
1140 **
danielk197790ba3bd2004-06-25 08:32:25 +00001141 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
1142 ** A RESERVED lock is implemented by grabbing a write-lock on the
1143 ** 'reserved byte'.
danielk1977f42f25c2004-06-25 07:21:28 +00001144 **
1145 ** A process may only obtain a PENDING lock after it has obtained a
danielk197790ba3bd2004-06-25 08:32:25 +00001146 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
1147 ** on the 'pending byte'. This ensures that no new SHARED locks can be
1148 ** obtained, but existing SHARED locks are allowed to persist. A process
1149 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
1150 ** This property is used by the algorithm for rolling back a journal file
1151 ** after a crash.
danielk1977f42f25c2004-06-25 07:21:28 +00001152 **
danielk197790ba3bd2004-06-25 08:32:25 +00001153 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
1154 ** implemented by obtaining a write-lock on the entire 'shared byte
1155 ** range'. Since all other locks require a read-lock on one of the bytes
1156 ** within this range, this ensures that no other locks are held on the
1157 ** database.
danielk1977f42f25c2004-06-25 07:21:28 +00001158 **
1159 ** The reason a single byte cannot be used instead of the 'shared byte
1160 ** range' is that some versions of windows do not support read-locks. By
1161 ** locking a random byte from a range, concurrent SHARED locks may exist
1162 ** even if the locking primitive used is always a write-lock.
1163 */
danielk19779a1d0ab2004-06-01 14:09:28 +00001164 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001165 unixFile *pFile = (unixFile*)id;
1166 struct lockInfo *pLock = pFile->pLock;
danielk19779a1d0ab2004-06-01 14:09:28 +00001167 struct flock lock;
1168 int s;
1169
drh054889e2005-11-30 03:20:31 +00001170 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001171 OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001172 locktypeName(locktype), locktypeName(pFile->locktype),
1173 locktypeName(pLock->locktype), pLock->cnt , getpid());
danielk19779a1d0ab2004-06-01 14:09:28 +00001174
1175 /* If there is already a lock of this type or more restrictive on the
danielk1977ad94b582007-08-20 06:44:22 +00001176 ** unixFile, do nothing. Don't use the end_lock: exit path, as
danielk1977b4b47412007-08-17 15:53:36 +00001177 ** enterMutex() hasn't been called yet.
danielk19779a1d0ab2004-06-01 14:09:28 +00001178 */
drh054889e2005-11-30 03:20:31 +00001179 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00001180 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001181 locktypeName(locktype));
danielk19779a1d0ab2004-06-01 14:09:28 +00001182 return SQLITE_OK;
1183 }
1184
drhb3e04342004-06-08 00:47:47 +00001185 /* Make sure the locking sequence is correct
drh2ac3ee92004-06-07 16:27:46 +00001186 */
drh054889e2005-11-30 03:20:31 +00001187 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
drhb3e04342004-06-08 00:47:47 +00001188 assert( locktype!=PENDING_LOCK );
drh054889e2005-11-30 03:20:31 +00001189 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
drh2ac3ee92004-06-07 16:27:46 +00001190
drh054889e2005-11-30 03:20:31 +00001191 /* This mutex is needed because pFile->pLock is shared across threads
drhb3e04342004-06-08 00:47:47 +00001192 */
danielk1977b4b47412007-08-17 15:53:36 +00001193 enterMutex();
danielk19779a1d0ab2004-06-01 14:09:28 +00001194
drh029b44b2006-01-15 00:13:15 +00001195 /* Make sure the current thread owns the pFile.
1196 */
1197 rc = transferOwnership(pFile);
1198 if( rc!=SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00001199 leaveMutex();
drh029b44b2006-01-15 00:13:15 +00001200 return rc;
1201 }
drh64b1bea2006-01-15 02:30:57 +00001202 pLock = pFile->pLock;
drh029b44b2006-01-15 00:13:15 +00001203
danielk1977ad94b582007-08-20 06:44:22 +00001204 /* If some thread using this PID has a lock via a different unixFile*
danielk19779a1d0ab2004-06-01 14:09:28 +00001205 ** handle that precludes the requested lock, return BUSY.
1206 */
drh054889e2005-11-30 03:20:31 +00001207 if( (pFile->locktype!=pLock->locktype &&
drh2ac3ee92004-06-07 16:27:46 +00001208 (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
danielk19779a1d0ab2004-06-01 14:09:28 +00001209 ){
1210 rc = SQLITE_BUSY;
1211 goto end_lock;
1212 }
1213
1214 /* If a SHARED lock is requested, and some thread using this PID already
1215 ** has a SHARED or RESERVED lock, then increment reference counts and
1216 ** return SQLITE_OK.
1217 */
1218 if( locktype==SHARED_LOCK &&
1219 (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
1220 assert( locktype==SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001221 assert( pFile->locktype==0 );
danielk1977ecb2a962004-06-02 06:30:16 +00001222 assert( pLock->cnt>0 );
drh054889e2005-11-30 03:20:31 +00001223 pFile->locktype = SHARED_LOCK;
danielk19779a1d0ab2004-06-01 14:09:28 +00001224 pLock->cnt++;
drh054889e2005-11-30 03:20:31 +00001225 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001226 goto end_lock;
1227 }
1228
danielk197713adf8a2004-06-03 16:08:41 +00001229 lock.l_len = 1L;
drh2b4b5962005-06-15 17:47:55 +00001230
danielk19779a1d0ab2004-06-01 14:09:28 +00001231 lock.l_whence = SEEK_SET;
1232
drh3cde3bb2004-06-12 02:17:14 +00001233 /* A PENDING lock is needed before acquiring a SHARED lock and before
1234 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1235 ** be released.
danielk19779a1d0ab2004-06-01 14:09:28 +00001236 */
drh3cde3bb2004-06-12 02:17:14 +00001237 if( locktype==SHARED_LOCK
drh054889e2005-11-30 03:20:31 +00001238 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh3cde3bb2004-06-12 02:17:14 +00001239 ){
danielk1977489468c2004-06-28 08:25:47 +00001240 lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
drh2ac3ee92004-06-07 16:27:46 +00001241 lock.l_start = PENDING_BYTE;
drh054889e2005-11-30 03:20:31 +00001242 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001243 if( s==(-1) ){
danielk19779a1d0ab2004-06-01 14:09:28 +00001244 rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
1245 goto end_lock;
1246 }
drh3cde3bb2004-06-12 02:17:14 +00001247 }
1248
1249
1250 /* If control gets to this point, then actually go ahead and make
1251 ** operating system calls for the specified lock.
1252 */
1253 if( locktype==SHARED_LOCK ){
1254 assert( pLock->cnt==0 );
1255 assert( pLock->locktype==0 );
danielk19779a1d0ab2004-06-01 14:09:28 +00001256
drh2ac3ee92004-06-07 16:27:46 +00001257 /* Now get the read-lock */
1258 lock.l_start = SHARED_FIRST;
1259 lock.l_len = SHARED_SIZE;
drh054889e2005-11-30 03:20:31 +00001260 s = fcntl(pFile->h, F_SETLK, &lock);
drh2ac3ee92004-06-07 16:27:46 +00001261
1262 /* Drop the temporary PENDING lock */
1263 lock.l_start = PENDING_BYTE;
1264 lock.l_len = 1L;
danielk19779a1d0ab2004-06-01 14:09:28 +00001265 lock.l_type = F_UNLCK;
drh054889e2005-11-30 03:20:31 +00001266 if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
drh4ac285a2006-09-15 07:28:50 +00001267 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
drh2b4b5962005-06-15 17:47:55 +00001268 goto end_lock;
1269 }
drhe2396a12007-03-29 20:19:58 +00001270 if( s==(-1) ){
drhbbd42a62004-05-22 17:41:58 +00001271 rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
1272 }else{
drh054889e2005-11-30 03:20:31 +00001273 pFile->locktype = SHARED_LOCK;
1274 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001275 pLock->cnt = 1;
drhbbd42a62004-05-22 17:41:58 +00001276 }
drh3cde3bb2004-06-12 02:17:14 +00001277 }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
1278 /* We are trying for an exclusive lock but another thread in this
1279 ** same process is still holding a shared lock. */
1280 rc = SQLITE_BUSY;
drhbbd42a62004-05-22 17:41:58 +00001281 }else{
drh3cde3bb2004-06-12 02:17:14 +00001282 /* The request was for a RESERVED or EXCLUSIVE lock. It is
danielk19779a1d0ab2004-06-01 14:09:28 +00001283 ** assumed that there is a SHARED or greater lock on the file
1284 ** already.
1285 */
drh054889e2005-11-30 03:20:31 +00001286 assert( 0!=pFile->locktype );
danielk19779a1d0ab2004-06-01 14:09:28 +00001287 lock.l_type = F_WRLCK;
1288 switch( locktype ){
1289 case RESERVED_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001290 lock.l_start = RESERVED_BYTE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001291 break;
danielk19779a1d0ab2004-06-01 14:09:28 +00001292 case EXCLUSIVE_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001293 lock.l_start = SHARED_FIRST;
1294 lock.l_len = SHARED_SIZE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001295 break;
1296 default:
1297 assert(0);
1298 }
drh054889e2005-11-30 03:20:31 +00001299 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001300 if( s==(-1) ){
danielk19779a1d0ab2004-06-01 14:09:28 +00001301 rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
1302 }
drhbbd42a62004-05-22 17:41:58 +00001303 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001304
danielk1977ecb2a962004-06-02 06:30:16 +00001305 if( rc==SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001306 pFile->locktype = locktype;
danielk1977ecb2a962004-06-02 06:30:16 +00001307 pLock->locktype = locktype;
drh3cde3bb2004-06-12 02:17:14 +00001308 }else if( locktype==EXCLUSIVE_LOCK ){
drh054889e2005-11-30 03:20:31 +00001309 pFile->locktype = PENDING_LOCK;
drh3cde3bb2004-06-12 02:17:14 +00001310 pLock->locktype = PENDING_LOCK;
danielk1977ecb2a962004-06-02 06:30:16 +00001311 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001312
1313end_lock:
danielk1977b4b47412007-08-17 15:53:36 +00001314 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001315 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
danielk19772b444852004-06-29 07:45:33 +00001316 rc==SQLITE_OK ? "ok" : "failed");
drhbbd42a62004-05-22 17:41:58 +00001317 return rc;
1318}
1319
1320/*
drh054889e2005-11-30 03:20:31 +00001321** Lower the locking level on file descriptor pFile to locktype. locktype
drha6abd042004-06-09 17:37:22 +00001322** must be either NO_LOCK or SHARED_LOCK.
1323**
1324** If the locking level of the file descriptor is already at or below
1325** the requested locking level, this routine is a no-op.
drhbbd42a62004-05-22 17:41:58 +00001326*/
danielk197762079062007-08-15 17:08:46 +00001327static int unixUnlock(sqlite3_file *id, int locktype){
drha6abd042004-06-09 17:37:22 +00001328 struct lockInfo *pLock;
1329 struct flock lock;
drh9c105bb2004-10-02 20:38:28 +00001330 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001331 unixFile *pFile = (unixFile*)id;
drha6abd042004-06-09 17:37:22 +00001332
drh054889e2005-11-30 03:20:31 +00001333 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001334 OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
drh054889e2005-11-30 03:20:31 +00001335 pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
drha6abd042004-06-09 17:37:22 +00001336
1337 assert( locktype<=SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001338 if( pFile->locktype<=locktype ){
drha6abd042004-06-09 17:37:22 +00001339 return SQLITE_OK;
1340 }
drhf1a221e2006-01-15 17:27:17 +00001341 if( CHECK_THREADID(pFile) ){
1342 return SQLITE_MISUSE;
1343 }
danielk1977b4b47412007-08-17 15:53:36 +00001344 enterMutex();
drh054889e2005-11-30 03:20:31 +00001345 pLock = pFile->pLock;
drha6abd042004-06-09 17:37:22 +00001346 assert( pLock->cnt!=0 );
drh054889e2005-11-30 03:20:31 +00001347 if( pFile->locktype>SHARED_LOCK ){
1348 assert( pLock->locktype==pFile->locktype );
drh9c105bb2004-10-02 20:38:28 +00001349 if( locktype==SHARED_LOCK ){
1350 lock.l_type = F_RDLCK;
1351 lock.l_whence = SEEK_SET;
1352 lock.l_start = SHARED_FIRST;
1353 lock.l_len = SHARED_SIZE;
drhe2396a12007-03-29 20:19:58 +00001354 if( fcntl(pFile->h, F_SETLK, &lock)==(-1) ){
drh9c105bb2004-10-02 20:38:28 +00001355 /* This should never happen */
drh4ac285a2006-09-15 07:28:50 +00001356 rc = SQLITE_IOERR_RDLOCK;
drh9c105bb2004-10-02 20:38:28 +00001357 }
1358 }
drhbbd42a62004-05-22 17:41:58 +00001359 lock.l_type = F_UNLCK;
1360 lock.l_whence = SEEK_SET;
drha6abd042004-06-09 17:37:22 +00001361 lock.l_start = PENDING_BYTE;
1362 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
drhe2396a12007-03-29 20:19:58 +00001363 if( fcntl(pFile->h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001364 pLock->locktype = SHARED_LOCK;
1365 }else{
drh4ac285a2006-09-15 07:28:50 +00001366 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
drh2b4b5962005-06-15 17:47:55 +00001367 }
drhbbd42a62004-05-22 17:41:58 +00001368 }
drha6abd042004-06-09 17:37:22 +00001369 if( locktype==NO_LOCK ){
1370 struct openCnt *pOpen;
danielk1977ecb2a962004-06-02 06:30:16 +00001371
drha6abd042004-06-09 17:37:22 +00001372 /* Decrement the shared lock counter. Release the lock using an
1373 ** OS call only when all threads in this same process have released
1374 ** the lock.
1375 */
1376 pLock->cnt--;
1377 if( pLock->cnt==0 ){
1378 lock.l_type = F_UNLCK;
1379 lock.l_whence = SEEK_SET;
1380 lock.l_start = lock.l_len = 0L;
drhe2396a12007-03-29 20:19:58 +00001381 if( fcntl(pFile->h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001382 pLock->locktype = NO_LOCK;
1383 }else{
drh4ac285a2006-09-15 07:28:50 +00001384 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
drh2b4b5962005-06-15 17:47:55 +00001385 }
drha6abd042004-06-09 17:37:22 +00001386 }
1387
drhbbd42a62004-05-22 17:41:58 +00001388 /* Decrement the count of locks against this same file. When the
1389 ** count reaches zero, close any other file descriptors whose close
1390 ** was deferred because of outstanding locks.
1391 */
drh054889e2005-11-30 03:20:31 +00001392 pOpen = pFile->pOpen;
drhbbd42a62004-05-22 17:41:58 +00001393 pOpen->nLock--;
1394 assert( pOpen->nLock>=0 );
1395 if( pOpen->nLock==0 && pOpen->nPending>0 ){
1396 int i;
1397 for(i=0; i<pOpen->nPending; i++){
1398 close(pOpen->aPending[i]);
1399 }
drh64b1bea2006-01-15 02:30:57 +00001400 free(pOpen->aPending);
drhbbd42a62004-05-22 17:41:58 +00001401 pOpen->nPending = 0;
1402 pOpen->aPending = 0;
1403 }
1404 }
danielk1977b4b47412007-08-17 15:53:36 +00001405 leaveMutex();
drh054889e2005-11-30 03:20:31 +00001406 pFile->locktype = locktype;
drh9c105bb2004-10-02 20:38:28 +00001407 return rc;
drhbbd42a62004-05-22 17:41:58 +00001408}
1409
1410/*
danielk1977e3026632004-06-22 11:29:02 +00001411** Close a file.
1412*/
danielk197762079062007-08-15 17:08:46 +00001413static int unixClose(sqlite3_file *id){
1414 unixFile *pFile = (unixFile *)id;
1415 if( !pFile ) return SQLITE_OK;
1416 unixUnlock(id, NO_LOCK);
1417 if( pFile->dirfd>=0 ) close(pFile->dirfd);
1418 pFile->dirfd = -1;
danielk1977b4b47412007-08-17 15:53:36 +00001419 enterMutex();
danielk1977441b09a2006-01-05 13:48:29 +00001420
danielk197762079062007-08-15 17:08:46 +00001421 if( pFile->pOpen->nLock ){
danielk1977e3026632004-06-22 11:29:02 +00001422 /* If there are outstanding locks, do not actually close the file just
1423 ** yet because that would clear those locks. Instead, add the file
1424 ** descriptor to pOpen->aPending. It will be automatically closed when
1425 ** the last lock is cleared.
1426 */
1427 int *aNew;
danielk197762079062007-08-15 17:08:46 +00001428 struct openCnt *pOpen = pFile->pOpen;
drh64b1bea2006-01-15 02:30:57 +00001429 aNew = realloc( pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
danielk1977e3026632004-06-22 11:29:02 +00001430 if( aNew==0 ){
1431 /* If a malloc fails, just leak the file descriptor */
1432 }else{
1433 pOpen->aPending = aNew;
danielk197762079062007-08-15 17:08:46 +00001434 pOpen->aPending[pOpen->nPending] = pFile->h;
drhad81e872005-08-21 21:45:01 +00001435 pOpen->nPending++;
danielk1977e3026632004-06-22 11:29:02 +00001436 }
1437 }else{
1438 /* There are no outstanding locks so we can close the file immediately */
danielk197762079062007-08-15 17:08:46 +00001439 close(pFile->h);
danielk1977e3026632004-06-22 11:29:02 +00001440 }
danielk197762079062007-08-15 17:08:46 +00001441 releaseLockInfo(pFile->pLock);
1442 releaseOpenCnt(pFile->pOpen);
danielk1977441b09a2006-01-05 13:48:29 +00001443
danielk1977b4b47412007-08-17 15:53:36 +00001444 leaveMutex();
danielk197762079062007-08-15 17:08:46 +00001445 pFile->isOpen = 0;
1446 OSTRACE2("CLOSE %-3d\n", pFile->h);
danielk1977e3026632004-06-22 11:29:02 +00001447 OpenCounter(-1);
danielk1977b4b47412007-08-17 15:53:36 +00001448 memset(pFile, 0, sizeof(unixFile));
drh02afc862006-01-20 18:10:57 +00001449 return SQLITE_OK;
danielk1977e3026632004-06-22 11:29:02 +00001450}
1451
drhbfe66312006-10-03 17:40:40 +00001452
1453#ifdef SQLITE_ENABLE_LOCKING_STYLE
1454#pragma mark AFP Support
1455
1456/*
1457 ** The afpLockingContext structure contains all afp lock specific state
1458 */
1459typedef struct afpLockingContext afpLockingContext;
1460struct afpLockingContext {
1461 unsigned long long sharedLockByte;
1462 char *filePath;
1463};
1464
1465struct ByteRangeLockPB2
1466{
1467 unsigned long long offset; /* offset to first byte to lock */
1468 unsigned long long length; /* nbr of bytes to lock */
1469 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
1470 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
1471 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
1472 int fd; /* file desc to assoc this lock with */
1473};
1474
drhfd131da2007-08-07 17:13:03 +00001475#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
drhbfe66312006-10-03 17:40:40 +00001476
danielk1977ad94b582007-08-20 06:44:22 +00001477/*
1478** Return 0 on success, 1 on failure. To match the behavior of the
1479** normal posix file locking (used in unixLock for example), we should
1480** provide 'richer' return codes - specifically to differentiate between
1481** 'file busy' and 'file system error' results.
1482*/
1483static int _AFPFSSetLock(
1484 const char *path,
1485 int fd,
1486 unsigned long long offset,
1487 unsigned long long length,
1488 int setLockFlag
1489){
drhfd131da2007-08-07 17:13:03 +00001490 struct ByteRangeLockPB2 pb;
drhbfe66312006-10-03 17:40:40 +00001491 int err;
1492
1493 pb.unLockFlag = setLockFlag ? 0 : 1;
1494 pb.startEndFlag = 0;
1495 pb.offset = offset;
1496 pb.length = length;
1497 pb.fd = fd;
drh4f0c5872007-03-26 22:05:01 +00001498 OSTRACE5("AFPLOCK setting lock %s for %d in range %llx:%llx\n",
drhbfe66312006-10-03 17:40:40 +00001499 (setLockFlag?"ON":"OFF"), fd, offset, length);
1500 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
1501 if ( err==-1 ) {
drh4f0c5872007-03-26 22:05:01 +00001502 OSTRACE4("AFPLOCK failed to fsctl() '%s' %d %s\n", path, errno,
drhbfe66312006-10-03 17:40:40 +00001503 strerror(errno));
drh3b62b2f2007-06-08 18:27:03 +00001504 return 1; /* error */
drhbfe66312006-10-03 17:40:40 +00001505 } else {
1506 return 0;
1507 }
1508}
1509
1510/*
1511 ** This routine checks if there is a RESERVED lock held on the specified
1512 ** file by this or any other process. If such a lock is held, return
1513 ** non-zero. If the file is unlocked or holds only SHARED locks, then
1514 ** return zero.
1515 */
danielk1977ad94b582007-08-20 06:44:22 +00001516static int afpUnixCheckReservedLock(sqlite3_file *id){
drhbfe66312006-10-03 17:40:40 +00001517 int r = 0;
1518 unixFile *pFile = (unixFile*)id;
1519
1520 assert( pFile );
1521 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
1522
1523 /* Check if a thread in this process holds such a lock */
1524 if( pFile->locktype>SHARED_LOCK ){
1525 r = 1;
1526 }
1527
1528 /* Otherwise see if some other process holds it.
1529 */
1530 if ( !r ) {
drh3b62b2f2007-06-08 18:27:03 +00001531 /* lock the byte */
drhbfe66312006-10-03 17:40:40 +00001532 int failed = _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1,1);
1533 if (failed) {
1534 /* if we failed to get the lock then someone else must have it */
1535 r = 1;
1536 } else {
1537 /* if we succeeded in taking the reserved lock, unlock it to restore
1538 ** the original state */
1539 _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1, 0);
1540 }
1541 }
drh4f0c5872007-03-26 22:05:01 +00001542 OSTRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
drhbfe66312006-10-03 17:40:40 +00001543
1544 return r;
1545}
1546
1547/* AFP-style locking following the behavior of unixLock, see the unixLock
1548** function comments for details of lock management. */
danielk1977ad94b582007-08-20 06:44:22 +00001549static int afpUnixLock(sqlite3_file *id, int locktype)
drhbfe66312006-10-03 17:40:40 +00001550{
1551 int rc = SQLITE_OK;
1552 unixFile *pFile = (unixFile*)id;
1553 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
1554 int gotPendingLock = 0;
1555
1556 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001557 OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00001558 locktypeName(locktype), locktypeName(pFile->locktype), getpid());
1559 /* If there is already a lock of this type or more restrictive on the
danielk1977ad94b582007-08-20 06:44:22 +00001560 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
danielk1977b4b47412007-08-17 15:53:36 +00001561 ** enterMutex() hasn't been called yet.
drhbfe66312006-10-03 17:40:40 +00001562 */
1563 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00001564 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00001565 locktypeName(locktype));
1566 return SQLITE_OK;
1567 }
1568
1569 /* Make sure the locking sequence is correct
1570 */
1571 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
1572 assert( locktype!=PENDING_LOCK );
1573 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
1574
1575 /* This mutex is needed because pFile->pLock is shared across threads
1576 */
danielk1977b4b47412007-08-17 15:53:36 +00001577 enterMutex();
drhbfe66312006-10-03 17:40:40 +00001578
1579 /* Make sure the current thread owns the pFile.
1580 */
1581 rc = transferOwnership(pFile);
1582 if( rc!=SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00001583 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00001584 return rc;
1585 }
1586
1587 /* A PENDING lock is needed before acquiring a SHARED lock and before
1588 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1589 ** be released.
1590 */
1591 if( locktype==SHARED_LOCK
1592 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
1593 ){
1594 int failed = _AFPFSSetLock(context->filePath, pFile->h,
1595 PENDING_BYTE, 1, 1);
1596 if (failed) {
1597 rc = SQLITE_BUSY;
1598 goto afp_end_lock;
1599 }
1600 }
1601
1602 /* If control gets to this point, then actually go ahead and make
1603 ** operating system calls for the specified lock.
1604 */
1605 if( locktype==SHARED_LOCK ){
1606 int lk, failed;
1607 int tries = 0;
1608
1609 /* Now get the read-lock */
1610 /* note that the quality of the randomness doesn't matter that much */
1611 lk = random();
1612 context->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
1613 failed = _AFPFSSetLock(context->filePath, pFile->h,
1614 SHARED_FIRST+context->sharedLockByte, 1, 1);
1615
1616 /* Drop the temporary PENDING lock */
1617 if (_AFPFSSetLock(context->filePath, pFile->h, PENDING_BYTE, 1, 0)) {
1618 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1619 goto afp_end_lock;
1620 }
1621
1622 if( failed ){
1623 rc = SQLITE_BUSY;
1624 } else {
1625 pFile->locktype = SHARED_LOCK;
1626 }
1627 }else{
1628 /* The request was for a RESERVED or EXCLUSIVE lock. It is
1629 ** assumed that there is a SHARED or greater lock on the file
1630 ** already.
1631 */
1632 int failed = 0;
1633 assert( 0!=pFile->locktype );
1634 if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
1635 /* Acquire a RESERVED lock */
1636 failed = _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1,1);
1637 }
1638 if (!failed && locktype == EXCLUSIVE_LOCK) {
1639 /* Acquire an EXCLUSIVE lock */
1640
1641 /* Remove the shared lock before trying the range. we'll need to
1642 ** reestablish the shared lock if we can't get the afpUnixUnlock
1643 */
1644 if (!_AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST +
1645 context->sharedLockByte, 1, 0)) {
1646 /* now attemmpt to get the exclusive lock range */
1647 failed = _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST,
1648 SHARED_SIZE, 1);
1649 if (failed && _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST +
1650 context->sharedLockByte, 1, 1)) {
1651 rc = SQLITE_IOERR_RDLOCK; /* this should never happen */
1652 }
1653 } else {
1654 /* */
1655 rc = SQLITE_IOERR_UNLOCK; /* this should never happen */
1656 }
1657 }
1658 if( failed && rc == SQLITE_OK){
1659 rc = SQLITE_BUSY;
1660 }
1661 }
1662
1663 if( rc==SQLITE_OK ){
1664 pFile->locktype = locktype;
1665 }else if( locktype==EXCLUSIVE_LOCK ){
1666 pFile->locktype = PENDING_LOCK;
1667 }
1668
1669afp_end_lock:
danielk1977b4b47412007-08-17 15:53:36 +00001670 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001671 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
drhbfe66312006-10-03 17:40:40 +00001672 rc==SQLITE_OK ? "ok" : "failed");
1673 return rc;
1674}
1675
1676/*
1677 ** Lower the locking level on file descriptor pFile to locktype. locktype
1678 ** must be either NO_LOCK or SHARED_LOCK.
1679 **
1680 ** If the locking level of the file descriptor is already at or below
1681 ** the requested locking level, this routine is a no-op.
1682 */
danielk1977ad94b582007-08-20 06:44:22 +00001683static int afpUnixUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001684 struct flock lock;
1685 int rc = SQLITE_OK;
1686 unixFile *pFile = (unixFile*)id;
1687 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
1688
1689 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001690 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
drhbfe66312006-10-03 17:40:40 +00001691 pFile->locktype, getpid());
1692
1693 assert( locktype<=SHARED_LOCK );
1694 if( pFile->locktype<=locktype ){
1695 return SQLITE_OK;
1696 }
1697 if( CHECK_THREADID(pFile) ){
1698 return SQLITE_MISUSE;
1699 }
danielk1977b4b47412007-08-17 15:53:36 +00001700 enterMutex();
drhbfe66312006-10-03 17:40:40 +00001701 if( pFile->locktype>SHARED_LOCK ){
1702 if( locktype==SHARED_LOCK ){
1703 int failed = 0;
1704
1705 /* unlock the exclusive range - then re-establish the shared lock */
1706 if (pFile->locktype==EXCLUSIVE_LOCK) {
1707 failed = _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST,
1708 SHARED_SIZE, 0);
1709 if (!failed) {
1710 /* successfully removed the exclusive lock */
1711 if (_AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST+
1712 context->sharedLockByte, 1, 1)) {
1713 /* failed to re-establish our shared lock */
1714 rc = SQLITE_IOERR_RDLOCK; /* This should never happen */
1715 }
1716 } else {
1717 /* This should never happen - failed to unlock the exclusive range */
1718 rc = SQLITE_IOERR_UNLOCK;
1719 }
1720 }
1721 }
1722 if (rc == SQLITE_OK && pFile->locktype>=PENDING_LOCK) {
1723 if (_AFPFSSetLock(context->filePath, pFile->h, PENDING_BYTE, 1, 0)){
1724 /* failed to release the pending lock */
1725 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1726 }
1727 }
1728 if (rc == SQLITE_OK && pFile->locktype>=RESERVED_LOCK) {
1729 if (_AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1, 0)) {
1730 /* failed to release the reserved lock */
1731 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1732 }
1733 }
1734 }
1735 if( locktype==NO_LOCK ){
1736 int failed = _AFPFSSetLock(context->filePath, pFile->h,
1737 SHARED_FIRST + context->sharedLockByte, 1, 0);
1738 if (failed) {
1739 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1740 }
1741 }
1742 if (rc == SQLITE_OK)
1743 pFile->locktype = locktype;
danielk1977b4b47412007-08-17 15:53:36 +00001744 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00001745 return rc;
1746}
1747
1748/*
1749 ** Close a file & cleanup AFP specific locking context
1750 */
danielk1977ad94b582007-08-20 06:44:22 +00001751static int afpUnixClose(sqlite3_file *id) {
1752 unixFile *pFile = (unixFile*)pId;
1753
1754 if( !pFile ) return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00001755 afpUnixUnlock(*pId, NO_LOCK);
1756 /* free the AFP locking structure */
danielk1977ad94b582007-08-20 06:44:22 +00001757 if (pFile->lockingContext != NULL) {
1758 if (((afpLockingContext *)pFile->lockingContext)->filePath != NULL)
1759 sqlite3_free(((afpLockingContext*)pFile->lockingContext)->filePath);
1760 sqlite3_free(pFile->lockingContext);
drhbfe66312006-10-03 17:40:40 +00001761 }
danielk1977ad94b582007-08-20 06:44:22 +00001762
1763 if( pFile->dirfd>=0 ) close(pFile->dirfd);
1764 pFile->dirfd = -1;
1765 close(pFile->h);
1766 pFile->isOpen = 0;
1767 OSTRACE2("CLOSE %-3d\n", pFile->h);
drhbfe66312006-10-03 17:40:40 +00001768 OpenCounter(-1);
drhbfe66312006-10-03 17:40:40 +00001769 return SQLITE_OK;
1770}
1771
1772
1773#pragma mark flock() style locking
1774
1775/*
1776 ** The flockLockingContext is not used
1777 */
1778typedef void flockLockingContext;
1779
danielk1977ad94b582007-08-20 06:44:22 +00001780static int flockUnixCheckReservedLock(sqlite3_file *id) {
drhbfe66312006-10-03 17:40:40 +00001781 unixFile *pFile = (unixFile*)id;
1782
1783 if (pFile->locktype == RESERVED_LOCK) {
drh3b62b2f2007-06-08 18:27:03 +00001784 return 1; /* already have a reserved lock */
drhbfe66312006-10-03 17:40:40 +00001785 } else {
drh3b62b2f2007-06-08 18:27:03 +00001786 /* attempt to get the lock */
drhbfe66312006-10-03 17:40:40 +00001787 int rc = flock(pFile->h, LOCK_EX | LOCK_NB);
1788 if (!rc) {
drh3b62b2f2007-06-08 18:27:03 +00001789 /* got the lock, unlock it */
drhbfe66312006-10-03 17:40:40 +00001790 flock(pFile->h, LOCK_UN);
drh3b62b2f2007-06-08 18:27:03 +00001791 return 0; /* no one has it reserved */
drhbfe66312006-10-03 17:40:40 +00001792 }
drh3b62b2f2007-06-08 18:27:03 +00001793 return 1; /* someone else might have it reserved */
drhbfe66312006-10-03 17:40:40 +00001794 }
1795}
1796
danielk1977ad94b582007-08-20 06:44:22 +00001797static int flockUnixLock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001798 unixFile *pFile = (unixFile*)id;
1799
drh3b62b2f2007-06-08 18:27:03 +00001800 /* if we already have a lock, it is exclusive.
1801 ** Just adjust level and punt on outta here. */
drhbfe66312006-10-03 17:40:40 +00001802 if (pFile->locktype > NO_LOCK) {
1803 pFile->locktype = locktype;
1804 return SQLITE_OK;
1805 }
1806
drh3b62b2f2007-06-08 18:27:03 +00001807 /* grab an exclusive lock */
drhbfe66312006-10-03 17:40:40 +00001808 int rc = flock(pFile->h, LOCK_EX | LOCK_NB);
1809 if (rc) {
drh3b62b2f2007-06-08 18:27:03 +00001810 /* didn't get, must be busy */
drhbfe66312006-10-03 17:40:40 +00001811 return SQLITE_BUSY;
1812 } else {
drh3b62b2f2007-06-08 18:27:03 +00001813 /* got it, set the type and return ok */
drhbfe66312006-10-03 17:40:40 +00001814 pFile->locktype = locktype;
1815 return SQLITE_OK;
1816 }
1817}
1818
danielk1977ad94b582007-08-20 06:44:22 +00001819static int flockUnixUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001820 unixFile *pFile = (unixFile*)id;
1821
1822 assert( locktype<=SHARED_LOCK );
1823
drh3b62b2f2007-06-08 18:27:03 +00001824 /* no-op if possible */
drhbfe66312006-10-03 17:40:40 +00001825 if( pFile->locktype==locktype ){
1826 return SQLITE_OK;
1827 }
1828
drh3b62b2f2007-06-08 18:27:03 +00001829 /* shared can just be set because we always have an exclusive */
drhbfe66312006-10-03 17:40:40 +00001830 if (locktype==SHARED_LOCK) {
1831 pFile->locktype = locktype;
1832 return SQLITE_OK;
1833 }
1834
drh3b62b2f2007-06-08 18:27:03 +00001835 /* no, really, unlock. */
drhbfe66312006-10-03 17:40:40 +00001836 int rc = flock(pFile->h, LOCK_UN);
1837 if (rc)
1838 return SQLITE_IOERR_UNLOCK;
1839 else {
1840 pFile->locktype = NO_LOCK;
1841 return SQLITE_OK;
1842 }
1843}
1844
1845/*
1846 ** Close a file.
1847 */
danielk1977ad94b582007-08-20 06:44:22 +00001848static int flockUnixClose(sqlite3_file *pId) {
1849 unixFile *pFile = (unixFile*)*pId;
drhbfe66312006-10-03 17:40:40 +00001850
danielk1977ad94b582007-08-20 06:44:22 +00001851 if( !pFile ) return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00001852 flockUnixUnlock(*pId, NO_LOCK);
1853
danielk1977ad94b582007-08-20 06:44:22 +00001854 if( pFile->dirfd>=0 ) close(pFile->dirfd);
1855 pFile->dirfd = -1;
danielk1977b4b47412007-08-17 15:53:36 +00001856 enterMutex();
drhbfe66312006-10-03 17:40:40 +00001857
danielk1977ad94b582007-08-20 06:44:22 +00001858 close(pFile->h);
danielk1977b4b47412007-08-17 15:53:36 +00001859 leaveMutex();
danielk1977ad94b582007-08-20 06:44:22 +00001860 pFile->isOpen = 0;
1861 OSTRACE2("CLOSE %-3d\n", pFile->h);
drhbfe66312006-10-03 17:40:40 +00001862 OpenCounter(-1);
drhbfe66312006-10-03 17:40:40 +00001863 return SQLITE_OK;
1864}
1865
1866#pragma mark Old-School .lock file based locking
1867
1868/*
1869 ** The dotlockLockingContext structure contains all dotlock (.lock) lock
1870 ** specific state
1871 */
1872typedef struct dotlockLockingContext dotlockLockingContext;
1873struct dotlockLockingContext {
1874 char *lockPath;
1875};
1876
1877
danielk1977ad94b582007-08-20 06:44:22 +00001878static int dotlockUnixCheckReservedLock(sqlite3_file *id) {
drhbfe66312006-10-03 17:40:40 +00001879 unixFile *pFile = (unixFile*)id;
1880 dotlockLockingContext *context =
1881 (dotlockLockingContext *) pFile->lockingContext;
1882
1883 if (pFile->locktype == RESERVED_LOCK) {
drh3b62b2f2007-06-08 18:27:03 +00001884 return 1; /* already have a reserved lock */
drhbfe66312006-10-03 17:40:40 +00001885 } else {
1886 struct stat statBuf;
1887 if (lstat(context->lockPath,&statBuf) == 0)
drh3b62b2f2007-06-08 18:27:03 +00001888 /* file exists, someone else has the lock */
drhbfe66312006-10-03 17:40:40 +00001889 return 1;
1890 else
drh3b62b2f2007-06-08 18:27:03 +00001891 /* file does not exist, we could have it if we want it */
drhbfe66312006-10-03 17:40:40 +00001892 return 0;
1893 }
1894}
1895
danielk1977ad94b582007-08-20 06:44:22 +00001896static int dotlockUnixLock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001897 unixFile *pFile = (unixFile*)id;
1898 dotlockLockingContext *context =
1899 (dotlockLockingContext *) pFile->lockingContext;
1900
drh3b62b2f2007-06-08 18:27:03 +00001901 /* if we already have a lock, it is exclusive.
1902 ** Just adjust level and punt on outta here. */
drhbfe66312006-10-03 17:40:40 +00001903 if (pFile->locktype > NO_LOCK) {
1904 pFile->locktype = locktype;
1905
1906 /* Always update the timestamp on the old file */
1907 utimes(context->lockPath,NULL);
1908 return SQLITE_OK;
1909 }
1910
drh3b62b2f2007-06-08 18:27:03 +00001911 /* check to see if lock file already exists */
drhbfe66312006-10-03 17:40:40 +00001912 struct stat statBuf;
1913 if (lstat(context->lockPath,&statBuf) == 0){
drh3b62b2f2007-06-08 18:27:03 +00001914 return SQLITE_BUSY; /* it does, busy */
drhbfe66312006-10-03 17:40:40 +00001915 }
1916
drh3b62b2f2007-06-08 18:27:03 +00001917 /* grab an exclusive lock */
drhbfe66312006-10-03 17:40:40 +00001918 int fd = open(context->lockPath,O_RDONLY|O_CREAT|O_EXCL,0600);
1919 if (fd < 0) {
drh3b62b2f2007-06-08 18:27:03 +00001920 /* failed to open/create the file, someone else may have stolen the lock */
drhbfe66312006-10-03 17:40:40 +00001921 return SQLITE_BUSY;
1922 }
1923 close(fd);
1924
drh3b62b2f2007-06-08 18:27:03 +00001925 /* got it, set the type and return ok */
drhbfe66312006-10-03 17:40:40 +00001926 pFile->locktype = locktype;
1927 return SQLITE_OK;
1928}
1929
danielk1977ad94b582007-08-20 06:44:22 +00001930static int dotlockUnixUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001931 unixFile *pFile = (unixFile*)id;
1932 dotlockLockingContext *context =
1933 (dotlockLockingContext *) pFile->lockingContext;
1934
1935 assert( locktype<=SHARED_LOCK );
1936
drh3b62b2f2007-06-08 18:27:03 +00001937 /* no-op if possible */
drhbfe66312006-10-03 17:40:40 +00001938 if( pFile->locktype==locktype ){
1939 return SQLITE_OK;
1940 }
1941
drh3b62b2f2007-06-08 18:27:03 +00001942 /* shared can just be set because we always have an exclusive */
drhbfe66312006-10-03 17:40:40 +00001943 if (locktype==SHARED_LOCK) {
1944 pFile->locktype = locktype;
1945 return SQLITE_OK;
1946 }
1947
drh3b62b2f2007-06-08 18:27:03 +00001948 /* no, really, unlock. */
drhbfe66312006-10-03 17:40:40 +00001949 unlink(context->lockPath);
1950 pFile->locktype = NO_LOCK;
1951 return SQLITE_OK;
1952}
1953
1954/*
1955 ** Close a file.
1956 */
danielk1977ad94b582007-08-20 06:44:22 +00001957static int dotlockUnixClose(sqlite3_file *id) {
1958 unixFile *pFile = (unixFile*)id;
drhbfe66312006-10-03 17:40:40 +00001959
danielk1977ad94b582007-08-20 06:44:22 +00001960 if( !pFile ) return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00001961 dotlockUnixUnlock(*pId, NO_LOCK);
1962 /* free the dotlock locking structure */
danielk1977ad94b582007-08-20 06:44:22 +00001963 if (pFile->lockingContext != NULL) {
1964 if (((dotlockLockingContext *)pFile->lockingContext)->lockPath != NULL)
drh17435752007-08-16 04:30:38 +00001965 sqlite3_free( ( (dotlockLockingContext *)
danielk1977ad94b582007-08-20 06:44:22 +00001966 pFile->lockingContext)->lockPath);
1967 sqlite3_free(pFile->lockingContext);
drhbfe66312006-10-03 17:40:40 +00001968 }
1969
danielk1977ad94b582007-08-20 06:44:22 +00001970 if( pFile->dirfd>=0 ) close(pFile->dirfd);
1971 pFile->dirfd = -1;
danielk1977b4b47412007-08-17 15:53:36 +00001972 enterMutex();
drhbfe66312006-10-03 17:40:40 +00001973
danielk1977ad94b582007-08-20 06:44:22 +00001974 close(pFile->h);
drhbfe66312006-10-03 17:40:40 +00001975
danielk1977b4b47412007-08-17 15:53:36 +00001976 leaveMutex();
danielk1977ad94b582007-08-20 06:44:22 +00001977 pFile->isOpen = 0;
1978 OSTRACE2("CLOSE %-3d\n", pFile->h);
drhbfe66312006-10-03 17:40:40 +00001979 OpenCounter(-1);
drhbfe66312006-10-03 17:40:40 +00001980 return SQLITE_OK;
1981}
1982
1983
1984#pragma mark No locking
1985
1986/*
1987 ** The nolockLockingContext is void
1988 */
1989typedef void nolockLockingContext;
1990
danielk1977ad94b582007-08-20 06:44:22 +00001991static int nolockUnixCheckReservedLock(sqlite3_file *id) {
drhbfe66312006-10-03 17:40:40 +00001992 return 0;
1993}
1994
danielk1977ad94b582007-08-20 06:44:22 +00001995static int nolockUnixLock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001996 return SQLITE_OK;
1997}
1998
danielk1977ad94b582007-08-20 06:44:22 +00001999static int nolockUnixUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002000 return SQLITE_OK;
2001}
2002
2003/*
2004 ** Close a file.
2005 */
danielk1977ad94b582007-08-20 06:44:22 +00002006static int nolockUnixClose(sqlite3_file *id) {
2007 unixFile *pFile = (unixFile*)id;
drhbfe66312006-10-03 17:40:40 +00002008
danielk1977ad94b582007-08-20 06:44:22 +00002009 if( !pFile ) return SQLITE_OK;
2010 if( pFile->dirfd>=0 ) close(pFile->dirfd);
2011 pFile->dirfd = -1;
danielk1977b4b47412007-08-17 15:53:36 +00002012 enterMutex();
drhbfe66312006-10-03 17:40:40 +00002013
danielk1977ad94b582007-08-20 06:44:22 +00002014 close(pFile->h);
drhbfe66312006-10-03 17:40:40 +00002015
danielk1977b4b47412007-08-17 15:53:36 +00002016 leaveMutex();
danielk1977ad94b582007-08-20 06:44:22 +00002017 pFile->isOpen = 0;
2018 OSTRACE2("CLOSE %-3d\n", pFile->h);
drhbfe66312006-10-03 17:40:40 +00002019 OpenCounter(-1);
drhbfe66312006-10-03 17:40:40 +00002020 return SQLITE_OK;
2021}
2022
2023#endif /* SQLITE_ENABLE_LOCKING_STYLE */
2024
danielk1977ad94b582007-08-20 06:44:22 +00002025
danielk1977e3026632004-06-22 11:29:02 +00002026/*
danielk1977ad94b582007-08-20 06:44:22 +00002027** TODO: xBreakLock() for this vfs.
drh18839212005-11-26 03:43:23 +00002028*/
danielk1977ad94b582007-08-20 06:44:22 +00002029static int unixBreakLock(sqlite3_file *id){
2030 assert(!"TODO: unixBreakLock()");
2031 return 0;
drh9cbe6352005-11-29 03:13:21 +00002032}
2033
2034/*
danielk1977ad94b582007-08-20 06:44:22 +00002035** Return an integer that indices the type of lock currently held
2036** by this handle. (Used for testing and analysis only.)
drh9cbe6352005-11-29 03:13:21 +00002037*/
danielk1977ad94b582007-08-20 06:44:22 +00002038static int unixLockState(sqlite3_file *id){
2039 return ((unixFile*)id)->locktype;
drh9cbe6352005-11-29 03:13:21 +00002040}
2041
drh9c06c952005-11-26 00:25:00 +00002042/*
danielk1977a3d4c882007-03-23 10:08:38 +00002043** Return the sector size in bytes of the underlying block device for
2044** the specified file. This is almost always 512 bytes, but may be
2045** larger for some devices.
2046**
2047** SQLite code assumes this function cannot fail. It also assumes that
2048** if two files are created in the same file-system directory (i.e.
2049** a database and it's journal file) that the sector size will be the
2050** same for both.
2051*/
danielk197762079062007-08-15 17:08:46 +00002052static int unixSectorSize(sqlite3_file *id){
drh3ceeb752007-03-29 18:19:52 +00002053 return SQLITE_DEFAULT_SECTOR_SIZE;
danielk1977a3d4c882007-03-23 10:08:38 +00002054}
2055
danielk197790949c22007-08-17 16:50:38 +00002056/*
2057** Return the device characteristics for the file. This is always 0.
2058*/
danielk197762079062007-08-15 17:08:46 +00002059static int unixDeviceCharacteristics(sqlite3_file *id){
2060 return 0;
2061}
2062
danielk1977a3d4c882007-03-23 10:08:38 +00002063/*
danielk1977ad94b582007-08-20 06:44:22 +00002064** This vector defines all the methods that can operate on an sqlite3_file
drh054889e2005-11-30 03:20:31 +00002065** for unix.
drh9c06c952005-11-26 00:25:00 +00002066*/
danielk197762079062007-08-15 17:08:46 +00002067static const sqlite3_io_methods sqlite3UnixIoMethod = {
2068 1, /* iVersion */
drh9c06c952005-11-26 00:25:00 +00002069 unixClose,
2070 unixRead,
2071 unixWrite,
drh9c06c952005-11-26 00:25:00 +00002072 unixTruncate,
drh054889e2005-11-30 03:20:31 +00002073 unixSync,
drh054889e2005-11-30 03:20:31 +00002074 unixFileSize,
2075 unixLock,
2076 unixUnlock,
drh054889e2005-11-30 03:20:31 +00002077 unixCheckReservedLock,
danielk197762079062007-08-15 17:08:46 +00002078 unixBreakLock,
danielk197790949c22007-08-17 16:50:38 +00002079 unixLockState,
danielk1977a3d4c882007-03-23 10:08:38 +00002080 unixSectorSize,
danielk197762079062007-08-15 17:08:46 +00002081 unixDeviceCharacteristics
drh9c06c952005-11-26 00:25:00 +00002082};
2083
drhbfe66312006-10-03 17:40:40 +00002084#ifdef SQLITE_ENABLE_LOCKING_STYLE
drh054889e2005-11-30 03:20:31 +00002085/*
danielk1977ad94b582007-08-20 06:44:22 +00002086** This vector defines all the methods that can operate on an sqlite3_file
2087** for unix with AFP style file locking.
2088*/
2089static const sqlite3_io_methods sqlite3AFPLockingUnixIoMethod = {
2090 1, /* iVersion */
2091 unixClose,
drhbfe66312006-10-03 17:40:40 +00002092 unixRead,
2093 unixWrite,
drhbfe66312006-10-03 17:40:40 +00002094 unixTruncate,
2095 unixSync,
danielk1977ad94b582007-08-20 06:44:22 +00002096 unixFileSize,
2097 afpUnixLock,
2098 afpUnixUnlock,
2099 afpUnixCheckReservedLock,
2100 unixBreakLock,
2101 unixLockState,
2102 unixSectorSize,
2103 unixDeviceCharacteristics
2104};
2105
2106/*
2107** This vector defines all the methods that can operate on an sqlite3_file
2108** for unix with flock() style file locking.
2109*/
2110static const sqlite3_io_methods sqlite3FlockLockingUnixIoMethod = {
2111 1, /* iVersion */
2112 flockUnixClose,
2113 unixRead,
2114 unixWrite,
2115 unixTruncate,
2116 unixSync,
2117 unixFileSize,
2118 flockUnixLock,
2119 flockUnixUnlock,
2120 flockUnixCheckReservedLock,
2121 unixBreakLock,
2122 unixLockState,
2123 unixSectorSize,
2124 unixDeviceCharacteristics
2125};
2126
2127/*
2128** This vector defines all the methods that can operate on an sqlite3_file
2129** for unix with dotlock style file locking.
2130*/
2131static const sqlite3_io_methods sqlite3DotlockLockingUnixIoMethod = {
2132 1, /* iVersion */
2133 dotlockUnixClose,
2134 unixRead,
2135 unixWrite,
2136 unixTruncate,
2137 unixSync,
2138 unixFileSize,
2139 dotlockUnixLock,
2140 dotlockUnixUnlock,
2141 dotlockUnixCheckReservedLock,
2142 unixBreakLock,
2143 unixLockState,
2144 unixSectorSize,
2145 unixDeviceCharacteristics
2146};
2147
2148/*
2149** This vector defines all the methods that can operate on an sqlite3_file
2150** for unix with dotlock style file locking.
2151*/
2152static const sqlite3_io_methods sqlite3NolockLockingUnixIoMethod = {
2153 1, /* iVersion */
2154 nolockUnixClose,
2155 unixRead,
2156 unixWrite,
2157 unixTruncate,
2158 unixSync,
drhbfe66312006-10-03 17:40:40 +00002159 unixFileSize,
2160 nolockUnixLock,
2161 nolockUnixUnlock,
drhbfe66312006-10-03 17:40:40 +00002162 nolockUnixCheckReservedLock,
danielk1977ad94b582007-08-20 06:44:22 +00002163 unixBreakLock,
2164 unixLockState,
danielk1977a3d4c882007-03-23 10:08:38 +00002165 unixSectorSize,
danielk1977ad94b582007-08-20 06:44:22 +00002166 unixDeviceCharacteristics
drhbfe66312006-10-03 17:40:40 +00002167};
2168
2169#endif /* SQLITE_ENABLE_LOCKING_STYLE */
2170
2171/*
2172** Allocate memory for a new unixFile and initialize that unixFile.
2173** Write a pointer to the new unixFile into *pId.
2174** If we run out of memory, close the file and return an error.
drh054889e2005-11-30 03:20:31 +00002175*/
drhbfe66312006-10-03 17:40:40 +00002176#ifdef SQLITE_ENABLE_LOCKING_STYLE
2177/*
danielk1977ad94b582007-08-20 06:44:22 +00002178** When locking extensions are enabled, the filepath and locking style
2179** are needed to determine the unixFile pMethod to use for locking operations.
2180** The locking-style specific lockingContext data structure is created
2181** and assigned here also.
2182*/
2183static int fillInUnixFile(
drhbfe66312006-10-03 17:40:40 +00002184 int h, /* Open file descriptor of file being opened */
danielk1977ad94b582007-08-20 06:44:22 +00002185 int dirfd, /* Directory file descriptor */
2186 sqlite3_file *pId, /* Write completed initialization here */
drhbfe66312006-10-03 17:40:40 +00002187 const char *zFilename, /* Name of the file being opened */
drhbfe66312006-10-03 17:40:40 +00002188){
aswift108bc322006-10-11 17:19:46 +00002189 sqlite3LockingStyle lockingStyle;
danielk1977ad94b582007-08-20 06:44:22 +00002190 unixFile *pNew = (unixFile *)pId;
drhbfe66312006-10-03 17:40:40 +00002191 int rc;
2192
danielk1977ad94b582007-08-20 06:44:22 +00002193 memset(pNew, 0, sizeof(unixFile));
aswift448aa6f2006-11-11 01:31:58 +00002194 lockingStyle = sqlite3DetectLockingStyle(zFilename, h);
drhbfe66312006-10-03 17:40:40 +00002195 if ( lockingStyle == posixLockingStyle ) {
danielk1977b4b47412007-08-17 15:53:36 +00002196 enterMutex();
danielk1977ad94b582007-08-20 06:44:22 +00002197 rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
danielk1977b4b47412007-08-17 15:53:36 +00002198 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00002199 if( rc ){
2200 close(h);
2201 unlink(zFilename);
2202 return SQLITE_NOMEM;
2203 }
2204 } else {
drh3b62b2f2007-06-08 18:27:03 +00002205 /* pLock and pOpen are only used for posix advisory locking */
danielk1977ad94b582007-08-20 06:44:22 +00002206 pNew->pLock = NULL;
2207 pNew->pOpen = NULL;
drhbfe66312006-10-03 17:40:40 +00002208 }
danielk1977ad94b582007-08-20 06:44:22 +00002209 pNew->dirfd = -1;
2210 pNew->h = h;
2211 SET_THREADID(pNew);
drh17435752007-08-16 04:30:38 +00002212 pNew = sqlite3_malloc( sizeof(unixFile) );
drh054889e2005-11-30 03:20:31 +00002213 if( pNew==0 ){
drhbfe66312006-10-03 17:40:40 +00002214 close(h);
danielk1977b4b47412007-08-17 15:53:36 +00002215 enterMutex();
danielk1977ad94b582007-08-20 06:44:22 +00002216 releaseLockInfo(pNew->pLock);
2217 releaseOpenCnt(pNew->pOpen);
danielk1977b4b47412007-08-17 15:53:36 +00002218 leaveMutex();
drh054889e2005-11-30 03:20:31 +00002219 return SQLITE_NOMEM;
2220 }else{
aswift108bc322006-10-11 17:19:46 +00002221 switch(lockingStyle) {
drh5bb3eb92007-05-04 13:15:55 +00002222 case afpLockingStyle: {
drhbfe66312006-10-03 17:40:40 +00002223 /* afp locking uses the file path so it needs to be included in
2224 ** the afpLockingContext */
drh5bb3eb92007-05-04 13:15:55 +00002225 int nFilename;
drhbfe66312006-10-03 17:40:40 +00002226 pNew->pMethod = &sqlite3AFPLockingUnixIoMethod;
2227 pNew->lockingContext =
drh17435752007-08-16 04:30:38 +00002228 sqlite3_malloc(sizeof(afpLockingContext));
drh5bb3eb92007-05-04 13:15:55 +00002229 nFilename = strlen(zFilename)+1;
drhbfe66312006-10-03 17:40:40 +00002230 ((afpLockingContext *)pNew->lockingContext)->filePath =
drh17435752007-08-16 04:30:38 +00002231 sqlite3_malloc(nFilename);
drh5bb3eb92007-05-04 13:15:55 +00002232 memcpy(((afpLockingContext *)pNew->lockingContext)->filePath,
2233 zFilename, nFilename);
drhbfe66312006-10-03 17:40:40 +00002234 srandomdev();
2235 break;
drh5bb3eb92007-05-04 13:15:55 +00002236 }
drhbfe66312006-10-03 17:40:40 +00002237 case flockLockingStyle:
2238 /* flock locking doesn't need additional lockingContext information */
2239 pNew->pMethod = &sqlite3FlockLockingUnixIoMethod;
2240 break;
drh5bb3eb92007-05-04 13:15:55 +00002241 case dotlockLockingStyle: {
drhbfe66312006-10-03 17:40:40 +00002242 /* dotlock locking uses the file path so it needs to be included in
2243 ** the dotlockLockingContext */
drh5bb3eb92007-05-04 13:15:55 +00002244 int nFilename;
drhbfe66312006-10-03 17:40:40 +00002245 pNew->pMethod = &sqlite3DotlockLockingUnixIoMethod;
drh17435752007-08-16 04:30:38 +00002246 pNew->lockingContext = sqlite3_malloc(
drhbfe66312006-10-03 17:40:40 +00002247 sizeof(dotlockLockingContext));
drh5bb3eb92007-05-04 13:15:55 +00002248 nFilename = strlen(zFilename) + 6;
drhbfe66312006-10-03 17:40:40 +00002249 ((dotlockLockingContext *)pNew->lockingContext)->lockPath =
drh17435752007-08-16 04:30:38 +00002250 sqlite3_malloc( nFilename );
drh5bb3eb92007-05-04 13:15:55 +00002251 sqlite3_snprintf(nFilename,
2252 ((dotlockLockingContext *)pNew->lockingContext)->lockPath,
drhbfe66312006-10-03 17:40:40 +00002253 "%s.lock", zFilename);
2254 break;
drh5bb3eb92007-05-04 13:15:55 +00002255 }
drhbfe66312006-10-03 17:40:40 +00002256 case posixLockingStyle:
2257 /* posix locking doesn't need additional lockingContext information */
2258 pNew->pMethod = &sqlite3UnixIoMethod;
2259 break;
2260 case noLockingStyle:
2261 case unsupportedLockingStyle:
2262 default:
2263 pNew->pMethod = &sqlite3NolockLockingUnixIoMethod;
2264 }
drhbfe66312006-10-03 17:40:40 +00002265 OpenCounter(+1);
2266 return SQLITE_OK;
2267 }
2268}
2269#else /* SQLITE_ENABLE_LOCKING_STYLE */
danielk1977b4b47412007-08-17 15:53:36 +00002270static int fillInUnixFile(
drhbfe66312006-10-03 17:40:40 +00002271 int h, /* Open file descriptor on file being opened */
danielk1977fee2d252007-08-18 10:59:19 +00002272 int dirfd,
danielk1977b4b47412007-08-17 15:53:36 +00002273 sqlite3_file *pId, /* Write to the unixFile structure here */
2274 const char *zFilename /* Name of the file being opened */
drhbfe66312006-10-03 17:40:40 +00002275){
danielk1977b4b47412007-08-17 15:53:36 +00002276 unixFile *pNew = (unixFile *)pId;
drhbfe66312006-10-03 17:40:40 +00002277 int rc;
2278
drhe78669b2007-06-29 12:04:26 +00002279#ifdef FD_CLOEXEC
2280 fcntl(h, F_SETFD, fcntl(h, F_GETFD, 0) | FD_CLOEXEC);
2281#endif
danielk1977b4b47412007-08-17 15:53:36 +00002282
2283 enterMutex();
2284 rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
2285 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00002286 if( rc ){
2287 close(h);
2288 return SQLITE_NOMEM;
2289 }
danielk1977b4b47412007-08-17 15:53:36 +00002290
drh4f0c5872007-03-26 22:05:01 +00002291 OSTRACE3("OPEN %-3d %s\n", h, zFilename);
danielk1977b4b47412007-08-17 15:53:36 +00002292 pNew->dirfd = -1;
2293 pNew->h = h;
danielk1977fee2d252007-08-18 10:59:19 +00002294 pNew->dirfd = dirfd;
danielk1977b4b47412007-08-17 15:53:36 +00002295 SET_THREADID(pNew);
2296
2297 pNew->pMethod = &sqlite3UnixIoMethod;
2298 OpenCounter(+1);
2299 return SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00002300}
drhbfe66312006-10-03 17:40:40 +00002301#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drh9c06c952005-11-26 00:25:00 +00002302
drh0ccebe72005-06-07 22:22:50 +00002303#endif /* SQLITE_OMIT_DISKIO */
2304/***************************************************************************
2305** Everything above deals with file I/O. Everything that follows deals
2306** with other miscellanous aspects of the operating system interface
2307****************************************************************************/
2308
danielk1977ad94b582007-08-20 06:44:22 +00002309/*
2310** Open a file descriptor to the directory containing file zFilename.
2311** If successful, *pFd is set to the opened file descriptor and
2312** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
2313** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
2314** value.
2315**
2316** If SQLITE_OK is returned, the caller is responsible for closing
2317** the file descriptor *pFd using close().
2318*/
danielk1977fee2d252007-08-18 10:59:19 +00002319static int openDirectory(const char *zFilename, int *pFd){
2320 char *zDirname;
2321 int ii;
2322 int fd;
2323
2324 zDirname = (char *)sqlite3_malloc(MAX_PATHNAME);
2325 if( !zDirname ){
2326 return SQLITE_NOMEM;
2327 }
2328 strncpy(zDirname, zFilename, MAX_PATHNAME);
2329 zDirname[MAX_PATHNAME-1] = '\0';
2330 for(ii=strlen(zDirname); ii>=0 && zDirname[ii]!='/'; ii--);
2331 if( ii>0 ){
2332 zDirname[ii] = '\0';
2333 fd = open(zDirname, O_RDONLY|O_BINARY, 0);
2334 if( fd>0 ){
2335#ifdef FD_CLOEXEC
2336 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
2337#endif
2338 OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname);
2339 }
2340 }
2341 sqlite3_free(zDirname);
2342 *pFd = fd;
2343 return (fd>0?SQLITE_OK:SQLITE_CANTOPEN);
2344}
2345
danielk1977b4b47412007-08-17 15:53:36 +00002346/*
danielk1977ad94b582007-08-20 06:44:22 +00002347** Open the file zPath.
2348**
danielk1977b4b47412007-08-17 15:53:36 +00002349** Previously, the SQLite OS layer used three functions in place of this
2350** one:
2351**
2352** sqlite3OsOpenReadWrite();
2353** sqlite3OsOpenReadOnly();
2354** sqlite3OsOpenExclusive();
2355**
2356** These calls correspond to the following combinations of flags:
2357**
2358** ReadWrite() -> (READWRITE | CREATE)
2359** ReadOnly() -> (READONLY)
2360** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
2361**
2362** The old OpenExclusive() accepted a boolean argument - "delFlag". If
2363** true, the file was configured to be automatically deleted when the
2364** file handle closed. To achieve the same effect using this new
2365** interface, add the DELETEONCLOSE flag to those specified above for
2366** OpenExclusive().
2367*/
2368static int unixOpen(
2369 void *pNotUsed,
2370 const char *zPath,
2371 sqlite3_file *pFile,
2372 int flags,
2373 int *pOutFlags
2374){
danielk1977fee2d252007-08-18 10:59:19 +00002375 int fd = 0; /* File descriptor returned by open() */
2376 int dirfd = -1; /* Directory file descriptor */
2377 int oflags = 0; /* Flags to pass to open() */
2378 int eType = flags&0xFFFFFF00; /* Type of file to open */
danielk1977b4b47412007-08-17 15:53:36 +00002379
2380 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
2381 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
2382 int isCreate = (flags & SQLITE_OPEN_CREATE);
2383 int isReadonly = (flags & SQLITE_OPEN_READONLY);
2384 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
2385
danielk1977fee2d252007-08-18 10:59:19 +00002386 /* If creating a master or main-file journal, this function will open
2387 ** a file-descriptor on the directory too. The first time unixSync()
2388 ** is called the directory file descriptor will be fsync()ed and close()d.
2389 */
2390 int isOpenDirectory = (isCreate &&
2391 (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL)
2392 );
2393
2394 /* Check the following statements are true:
2395 **
2396 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
2397 ** (b) if CREATE is set, then READWRITE must also be set, and
2398 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
2399 */
danielk1977b4b47412007-08-17 15:53:36 +00002400 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
danielk1977b4b47412007-08-17 15:53:36 +00002401 assert(isCreate==0 || isReadWrite);
danielk1977b4b47412007-08-17 15:53:36 +00002402 assert(isExclusive==0 || isCreate);
2403
danielk1977fee2d252007-08-18 10:59:19 +00002404 /* Assert that the upper layer has set one of the "file-type" flags. */
2405 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
2406 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
2407 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
2408 );
2409
danielk1977b4b47412007-08-17 15:53:36 +00002410 if( isReadonly ) oflags |= O_RDONLY;
2411 if( isReadWrite ) oflags |= O_RDWR;
2412 if( isCreate ) oflags |= O_CREAT;
2413 if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW);
2414 oflags |= (O_LARGEFILE|O_BINARY);
2415
2416 memset(pFile, 0, sizeof(unixFile));
2417 fd = open(zPath, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
2418 if( fd<0 && isReadWrite && !isExclusive ){
2419 /* Failed to open the file for read/write access. Try read-only. */
2420 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
2421 flags |= SQLITE_OPEN_READONLY;
2422 return unixOpen(pNotUsed, zPath, pFile, flags, pOutFlags);
2423 }
2424 if( fd<0 ){
2425 return SQLITE_CANTOPEN;
2426 }
2427 if( isDelete ){
2428 unlink(zPath);
2429 }
2430 if( pOutFlags ){
2431 *pOutFlags = flags;
2432 }
2433
2434 assert(fd!=0);
danielk1977fee2d252007-08-18 10:59:19 +00002435 if( isOpenDirectory ){
2436 int rc = openDirectory(zPath, &dirfd);
2437 if( rc!=SQLITE_OK ){
2438 close(fd);
2439 return rc;
2440 }
2441 }
2442 return fillInUnixFile(fd, dirfd, pFile, zPath);
danielk1977b4b47412007-08-17 15:53:36 +00002443}
2444
2445/*
danielk1977fee2d252007-08-18 10:59:19 +00002446** Delete the file at zPath. If the dirSync argument is true, fsync()
2447** the directory after deleting the file.
danielk1977b4b47412007-08-17 15:53:36 +00002448*/
danielk1977fee2d252007-08-18 10:59:19 +00002449static int unixDelete(void *pNotUsed, const char *zPath, int dirSync){
2450 int rc = SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00002451 SimulateIOError(return SQLITE_IOERR_DELETE);
2452 unlink(zPath);
danielk1977fee2d252007-08-18 10:59:19 +00002453 if( dirSync ){
2454 int fd;
2455 rc = openDirectory(zPath, &fd);
2456 if( rc==SQLITE_OK ){
2457 if( fsync(fd) ){
2458 rc = SQLITE_IOERR_DIR_FSYNC;
2459 }
2460 close(fd);
2461 }
2462 }
2463 return rc;
danielk1977b4b47412007-08-17 15:53:36 +00002464}
2465
danielk197790949c22007-08-17 16:50:38 +00002466/*
2467** Test the existance of or access permissions of file zPath. The
2468** test performed depends on the value of flags:
2469**
2470** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
2471** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
2472** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
2473**
2474** Otherwise return 0.
2475*/
danielk1977b4b47412007-08-17 15:53:36 +00002476static int unixAccess(void *pNotUsed, const char *zPath, int flags){
2477 int amode;
2478 switch( flags ){
2479 case SQLITE_ACCESS_EXISTS:
2480 amode = F_OK;
2481 break;
2482 case SQLITE_ACCESS_READWRITE:
2483 amode = W_OK|R_OK;
2484 break;
2485 case SQLITE_ACCESS_READONLY:
2486 amode = R_OK;
2487 break;
2488
2489 default:
2490 assert(!"Invalid flags argument");
2491 }
2492 return (access(zPath, amode)==0);
2493}
2494
2495/*
2496** Create a temporary file name in zBuf. zBuf must be big enough to
2497** hold at least MAX_PATHNAME characters.
2498*/
2499static int unixGetTempName(void *pNotUsed, char *zBuf){
2500 static const char *azDirs[] = {
2501 0,
2502 "/var/tmp",
2503 "/usr/tmp",
2504 "/tmp",
2505 ".",
2506 };
2507 static const unsigned char zChars[] =
2508 "abcdefghijklmnopqrstuvwxyz"
2509 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2510 "0123456789";
2511 int i, j;
2512 struct stat buf;
2513 const char *zDir = ".";
2514 azDirs[0] = sqlite3_temp_directory;
2515 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
2516 if( azDirs[i]==0 ) continue;
2517 if( stat(azDirs[i], &buf) ) continue;
2518 if( !S_ISDIR(buf.st_mode) ) continue;
2519 if( access(azDirs[i], 07) ) continue;
2520 zDir = azDirs[i];
2521 break;
2522 }
2523 do{
2524 sqlite3_snprintf(MAX_PATHNAME-17, zBuf, "%s/"TEMP_FILE_PREFIX, zDir);
2525 j = strlen(zBuf);
2526 sqlite3Randomness(15, &zBuf[j]);
2527 for(i=0; i<15; i++, j++){
2528 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
2529 }
2530 zBuf[j] = 0;
2531 }while( access(zBuf,0)==0 );
2532 return SQLITE_OK;
2533}
2534
2535
2536/*
2537** Turn a relative pathname into a full pathname. The relative path
2538** is stored as a nul-terminated string in the buffer pointed to by
2539** zPath.
2540**
2541** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
2542** (in this case, MAX_PATHNAME bytes). The full-path is written to
2543** this buffer before returning.
2544*/
2545static int unixFullPathname(void *pNotUsed, const char *zPath, char *zOut){
2546 zOut[MAX_PATHNAME-1] = '\0';
2547 if( zPath[0]=='/' ){
2548 strncpy(zOut, zPath, MAX_PATHNAME-1);
2549 }else{
2550 int nCwd;
2551 if( getcwd(zOut, MAX_PATHNAME-1)==0 ){
2552 return SQLITE_ERROR;
2553 }
2554 nCwd = strlen(zOut);
2555 zOut[nCwd] = '/';
2556 strncpy(&zOut[nCwd+1], zPath, MAX_PATHNAME-1-nCwd-1);
2557 }
2558 return SQLITE_OK;
2559
2560#if 0
2561 /*
2562 ** Remove "/./" path elements and convert "/A/./" path elements
2563 ** to just "/".
2564 */
2565 if( zFull ){
2566 int i, j;
2567 for(i=j=0; zFull[i]; i++){
2568 if( zFull[i]=='/' ){
2569 if( zFull[i+1]=='/' ) continue;
2570 if( zFull[i+1]=='.' && zFull[i+2]=='/' ){
2571 i += 1;
2572 continue;
2573 }
2574 if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){
2575 while( j>0 && zFull[j-1]!='/' ){ j--; }
2576 i += 3;
2577 continue;
2578 }
2579 }
2580 zFull[j++] = zFull[i];
2581 }
2582 zFull[j] = 0;
2583 }
2584#endif
2585}
2586
drh0ccebe72005-06-07 22:22:50 +00002587
drh761df872006-12-21 01:29:22 +00002588#ifndef SQLITE_OMIT_LOAD_EXTENSION
2589/*
2590** Interfaces for opening a shared library, finding entry points
2591** within the shared library, and closing the shared library.
2592*/
2593#include <dlfcn.h>
danielk1977b4b47412007-08-17 15:53:36 +00002594static void *unixDlOpen(void *pNotUsed, const char *zFilename){
drh761df872006-12-21 01:29:22 +00002595 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
2596}
danielk1977b4b47412007-08-17 15:53:36 +00002597static void unixDlError(void *pNotUsed, int nBuf, char *zBufOut){
2598 char *zErr;
2599 enterMutex();
2600 zErr = dlerror();
2601 if( zErr ){
2602 strncpy(zBufOut, zErr, nBuf-1);
2603 zBufOut[nBuf-1] = '\0';
2604 }else if(nBuf>0) {
2605 zBufOut[0] = '\0';
2606 }
2607 leaveMutex();
2608}
2609void *unixDlSym(void *pHandle, const char *zSymbol){
drh761df872006-12-21 01:29:22 +00002610 return dlsym(pHandle, zSymbol);
2611}
danielk1977b4b47412007-08-17 15:53:36 +00002612void unixDlClose(void *pHandle){
2613 dlclose(pHandle);
drh761df872006-12-21 01:29:22 +00002614}
danielk1977b4b47412007-08-17 15:53:36 +00002615#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
2616 #define unixDlOpen 0
2617 #define unixDlError 0
2618 #define unixDlSym 0
2619 #define unixDlClose 0
2620#endif
2621
2622/*
danielk197790949c22007-08-17 16:50:38 +00002623** Write nBuf bytes of random data to the supplied buffer zBuf.
drhbbd42a62004-05-22 17:41:58 +00002624*/
danielk1977b4b47412007-08-17 15:53:36 +00002625static int unixRandomness(void *pNotUsed, int nBuf, char *zBuf){
danielk197790949c22007-08-17 16:50:38 +00002626
2627 assert(nBuf>=(sizeof(time_t)+sizeof(int)));
2628
drhbbd42a62004-05-22 17:41:58 +00002629 /* We have to initialize zBuf to prevent valgrind from reporting
2630 ** errors. The reports issued by valgrind are incorrect - we would
2631 ** prefer that the randomness be increased by making use of the
2632 ** uninitialized space in zBuf - but valgrind errors tend to worry
2633 ** some users. Rather than argue, it seems easier just to initialize
2634 ** the whole array and silence valgrind, even if that means less randomness
2635 ** in the random seed.
2636 **
2637 ** When testing, initializing zBuf[] to zero is all we do. That means
drhf1a221e2006-01-15 17:27:17 +00002638 ** that we always use the same random number sequence. This makes the
drhbbd42a62004-05-22 17:41:58 +00002639 ** tests repeatable.
2640 */
danielk1977b4b47412007-08-17 15:53:36 +00002641 memset(zBuf, 0, nBuf);
drhbbd42a62004-05-22 17:41:58 +00002642#if !defined(SQLITE_TEST)
2643 {
drh842b8642005-01-21 17:53:17 +00002644 int pid, fd;
2645 fd = open("/dev/urandom", O_RDONLY);
2646 if( fd<0 ){
drh07397232006-01-06 14:46:46 +00002647 time_t t;
2648 time(&t);
danielk197790949c22007-08-17 16:50:38 +00002649 memcpy(zBuf, &t, sizeof(t));
2650 pid = getpid();
2651 memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
drh842b8642005-01-21 17:53:17 +00002652 }else{
danielk1977b4b47412007-08-17 15:53:36 +00002653 read(fd, zBuf, nBuf);
drh842b8642005-01-21 17:53:17 +00002654 close(fd);
2655 }
drhbbd42a62004-05-22 17:41:58 +00002656 }
2657#endif
2658 return SQLITE_OK;
2659}
2660
danielk1977b4b47412007-08-17 15:53:36 +00002661
drhbbd42a62004-05-22 17:41:58 +00002662/*
2663** Sleep for a little while. Return the amount of time slept.
danielk1977b4b47412007-08-17 15:53:36 +00002664** The argument is the number of microseconds we want to sleep.
drhbbd42a62004-05-22 17:41:58 +00002665*/
danielk1977b4b47412007-08-17 15:53:36 +00002666static int unixSleep(void *pNotUsed, int microseconds){
drhbbd42a62004-05-22 17:41:58 +00002667#if defined(HAVE_USLEEP) && HAVE_USLEEP
danielk1977b4b47412007-08-17 15:53:36 +00002668 usleep(microseconds);
2669 return microseconds;
drhbbd42a62004-05-22 17:41:58 +00002670#else
danielk1977b4b47412007-08-17 15:53:36 +00002671 int seconds = (microseconds+999999)/1000000;
2672 sleep(seconds);
2673 return seconds;
drha3fad6f2006-01-18 14:06:37 +00002674#endif
drh88f474a2006-01-02 20:00:12 +00002675}
2676
2677/*
drhbbd42a62004-05-22 17:41:58 +00002678** The following variable, if set to a non-zero value, becomes the result
drh66560ad2006-01-06 14:32:19 +00002679** returned from sqlite3OsCurrentTime(). This is used for testing.
drhbbd42a62004-05-22 17:41:58 +00002680*/
2681#ifdef SQLITE_TEST
2682int sqlite3_current_time = 0;
2683#endif
2684
2685/*
2686** Find the current time (in Universal Coordinated Time). Write the
2687** current time and date as a Julian Day number into *prNow and
2688** return 0. Return 1 if the time and date cannot be found.
2689*/
danielk1977b4b47412007-08-17 15:53:36 +00002690static int unixCurrentTime(void *pNotUsed, double *prNow){
drh19e2d372005-08-29 23:00:03 +00002691#ifdef NO_GETTOD
drhbbd42a62004-05-22 17:41:58 +00002692 time_t t;
2693 time(&t);
2694 *prNow = t/86400.0 + 2440587.5;
drh19e2d372005-08-29 23:00:03 +00002695#else
2696 struct timeval sNow;
drhbdcc2762007-04-02 18:06:57 +00002697 gettimeofday(&sNow, 0);
drh19e2d372005-08-29 23:00:03 +00002698 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
2699#endif
drhbbd42a62004-05-22 17:41:58 +00002700#ifdef SQLITE_TEST
2701 if( sqlite3_current_time ){
2702 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
2703 }
2704#endif
2705 return 0;
2706}
danielk1977b4b47412007-08-17 15:53:36 +00002707
2708
2709sqlite3_vfs sqlite3DefaultVfs = {
2710 1, /* iVersion */
2711 sizeof(unixFile), /* szOsFile */
2712 MAX_PATHNAME, /* mxPathname */
2713 0, /* nRef */
2714 0, /* vfsMutex */
2715 0, /* pNext */
danielk1977b4b47412007-08-17 15:53:36 +00002716 "unix", /* zName */
2717 0, /* pAppData */
2718
2719 unixOpen, /* xOpen */
2720 unixDelete, /* xDelete */
2721 unixAccess, /* xAccess */
2722 unixGetTempName, /* xGetTempName */
2723 unixFullPathname, /* xFullPathname */
2724 unixDlOpen, /* xDlOpen */
2725 unixDlError, /* xDlError */
2726 unixDlSym, /* xDlSym */
2727 unixDlClose, /* xDlClose */
2728 unixRandomness, /* xRandomness */
2729 unixSleep, /* xSleep */
2730 unixCurrentTime /* xCurrentTime */
2731};
drhdce8bdb2007-08-16 13:01:44 +00002732
drhbbd42a62004-05-22 17:41:58 +00002733#endif /* OS_UNIX */