blob: 828bbfd6288c30a5c0b0aa52e5fbcb2ce4ec14e9 [file] [log] [blame]
drhbbd42a62004-05-22 17:41:58 +00001/*
2** 2004 May 22
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11******************************************************************************
12**
13** This file contains code that is specific to Unix systems.
14*/
drhbbd42a62004-05-22 17:41:58 +000015#include "sqliteInt.h"
drheb206252004-10-01 02:00:31 +000016#include "os.h"
17#if OS_UNIX /* This file is used on unix only */
drh66560ad2006-01-06 14:32:19 +000018
drhbfe66312006-10-03 17:40:40 +000019/* #define SQLITE_ENABLE_LOCKING_STYLE 0 */
20
drh9cbe6352005-11-29 03:13:21 +000021/*
22** These #defines should enable >2GB file support on Posix if the
23** underlying operating system supports it. If the OS lacks
drhf1a221e2006-01-15 17:27:17 +000024** large file support, these should be no-ops.
drh9cbe6352005-11-29 03:13:21 +000025**
26** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
27** on the compiler command line. This is necessary if you are compiling
28** on a recent machine (ex: RedHat 7.2) but you want your code to work
29** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2
30** without this option, LFS is enable. But LFS does not exist in the kernel
31** in RedHat 6.0, so the code won't work. Hence, for maximum binary
32** portability you should omit LFS.
drh9cbe6352005-11-29 03:13:21 +000033*/
34#ifndef SQLITE_DISABLE_LFS
35# define _LARGE_FILE 1
36# ifndef _FILE_OFFSET_BITS
37# define _FILE_OFFSET_BITS 64
38# endif
39# define _LARGEFILE_SOURCE 1
40#endif
drhbbd42a62004-05-22 17:41:58 +000041
drh9cbe6352005-11-29 03:13:21 +000042/*
43** standard include files.
44*/
45#include <sys/types.h>
46#include <sys/stat.h>
47#include <fcntl.h>
48#include <unistd.h>
drhbbd42a62004-05-22 17:41:58 +000049#include <time.h>
drh19e2d372005-08-29 23:00:03 +000050#include <sys/time.h>
drhbbd42a62004-05-22 17:41:58 +000051#include <errno.h>
drhbfe66312006-10-03 17:40:40 +000052#ifdef SQLITE_ENABLE_LOCKING_STYLE
53#include <sys/ioctl.h>
54#include <sys/param.h>
55#include <sys/mount.h>
56#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drh9cbe6352005-11-29 03:13:21 +000057
58/*
drhf1a221e2006-01-15 17:27:17 +000059** If we are to be thread-safe, include the pthreads header and define
60** the SQLITE_UNIX_THREADS macro.
drh9cbe6352005-11-29 03:13:21 +000061*/
drh2c547df2007-04-01 18:46:19 +000062#ifndef THREADSAFE
63# define THREADSAFE 1
64#endif
65#if THREADSAFE
drh9cbe6352005-11-29 03:13:21 +000066# include <pthread.h>
67# define SQLITE_UNIX_THREADS 1
68#endif
69
70/*
71** Default permissions when creating a new file
72*/
73#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
74# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
75#endif
76
danielk1977b4b47412007-08-17 15:53:36 +000077/*
78** Maximum supported path-length.
79*/
80#define MAX_PATHNAME 512
drh9cbe6352005-11-29 03:13:21 +000081
82
83/*
danielk1977ad94b582007-08-20 06:44:22 +000084** The unixFile structure is subclass of sqlite3_file specific for the unix
drh054889e2005-11-30 03:20:31 +000085** protability layer.
drh9cbe6352005-11-29 03:13:21 +000086*/
drh054889e2005-11-30 03:20:31 +000087typedef struct unixFile unixFile;
88struct unixFile {
danielk197762079062007-08-15 17:08:46 +000089 sqlite3_io_methods const *pMethod; /* Always the first entry */
danielk1977967a4a12007-08-20 14:23:44 +000090#ifdef SQLITE_TEST
91 /* In test mode, increase the size of this structure a bit so that
92 ** it is larger than the struct CrashFile defined in test6.c.
93 */
94 char aPadding[32];
95#endif
drh9cbe6352005-11-29 03:13:21 +000096 struct openCnt *pOpen; /* Info about all open fd's on this inode */
97 struct lockInfo *pLock; /* Info about locks on this inode */
drhbfe66312006-10-03 17:40:40 +000098#ifdef SQLITE_ENABLE_LOCKING_STYLE
99 void *lockingContext; /* Locking style specific state */
100#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drh9cbe6352005-11-29 03:13:21 +0000101 int h; /* The file descriptor */
102 unsigned char locktype; /* The type of lock held on this fd */
103 unsigned char isOpen; /* True if needs to be closed */
drh9cbe6352005-11-29 03:13:21 +0000104 int dirfd; /* File descriptor for the directory */
105#ifdef SQLITE_UNIX_THREADS
danielk1977ad94b582007-08-20 06:44:22 +0000106 pthread_t tid; /* The thread that "owns" this unixFile */
drh9cbe6352005-11-29 03:13:21 +0000107#endif
108};
109
drh0ccebe72005-06-07 22:22:50 +0000110/*
drh198bf392006-01-06 21:52:49 +0000111** Include code that is common to all os_*.c files
112*/
113#include "os_common.h"
114
115/*
drh0ccebe72005-06-07 22:22:50 +0000116** Do not include any of the File I/O interface procedures if the
drhf1a221e2006-01-15 17:27:17 +0000117** SQLITE_OMIT_DISKIO macro is defined (indicating that the database
drh0ccebe72005-06-07 22:22:50 +0000118** will be in-memory only)
119*/
120#ifndef SQLITE_OMIT_DISKIO
121
drh0ccebe72005-06-07 22:22:50 +0000122/*
123** Define various macros that are missing from some systems.
124*/
drhbbd42a62004-05-22 17:41:58 +0000125#ifndef O_LARGEFILE
126# define O_LARGEFILE 0
127#endif
128#ifdef SQLITE_DISABLE_LFS
129# undef O_LARGEFILE
130# define O_LARGEFILE 0
131#endif
132#ifndef O_NOFOLLOW
133# define O_NOFOLLOW 0
134#endif
135#ifndef O_BINARY
136# define O_BINARY 0
137#endif
138
139/*
140** The DJGPP compiler environment looks mostly like Unix, but it
141** lacks the fcntl() system call. So redefine fcntl() to be something
142** that always succeeds. This means that locking does not occur under
danielk197726c5d792005-11-25 09:01:23 +0000143** DJGPP. But it's DOS - what did you expect?
drhbbd42a62004-05-22 17:41:58 +0000144*/
145#ifdef __DJGPP__
146# define fcntl(A,B,C) 0
147#endif
148
149/*
drh2b4b5962005-06-15 17:47:55 +0000150** The threadid macro resolves to the thread-id or to 0. Used for
151** testing and debugging only.
152*/
153#ifdef SQLITE_UNIX_THREADS
154#define threadid pthread_self()
155#else
156#define threadid 0
157#endif
158
159/*
danielk1977ad94b582007-08-20 06:44:22 +0000160** Set or check the unixFile.tid field. This field is set when an unixFile
161** is first opened. All subsequent uses of the unixFile verify that the
162** same thread is operating on the unixFile. Some operating systems do
drh2b4b5962005-06-15 17:47:55 +0000163** not allow locks to be overridden by other threads and that restriction
164** means that sqlite3* database handles cannot be moved from one thread
165** to another. This logic makes sure a user does not try to do that
166** by mistake.
drhf1a221e2006-01-15 17:27:17 +0000167**
danielk1977ad94b582007-08-20 06:44:22 +0000168** Version 3.3.1 (2006-01-15): unixFile can be moved from one thread to
drhf1a221e2006-01-15 17:27:17 +0000169** another as long as we are running on a system that supports threads
170** overriding each others locks (which now the most common behavior)
danielk1977ad94b582007-08-20 06:44:22 +0000171** or if no locks are held. But the unixFile.pLock field needs to be
drhf1a221e2006-01-15 17:27:17 +0000172** recomputed because its key includes the thread-id. See the
173** transferOwnership() function below for additional information
drh2b4b5962005-06-15 17:47:55 +0000174*/
drh029b44b2006-01-15 00:13:15 +0000175#if defined(SQLITE_UNIX_THREADS)
drh9cbe6352005-11-29 03:13:21 +0000176# define SET_THREADID(X) (X)->tid = pthread_self()
drh029b44b2006-01-15 00:13:15 +0000177# define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
178 !pthread_equal((X)->tid, pthread_self()))
drh2b4b5962005-06-15 17:47:55 +0000179#else
180# define SET_THREADID(X)
181# define CHECK_THREADID(X) 0
danielk197713adf8a2004-06-03 16:08:41 +0000182#endif
183
drhbbd42a62004-05-22 17:41:58 +0000184/*
185** Here is the dirt on POSIX advisory locks: ANSI STD 1003.1 (1996)
186** section 6.5.2.2 lines 483 through 490 specify that when a process
187** sets or clears a lock, that operation overrides any prior locks set
188** by the same process. It does not explicitly say so, but this implies
189** that it overrides locks set by the same process using a different
190** file descriptor. Consider this test case:
191**
192** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
193** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
194**
195** Suppose ./file1 and ./file2 are really the same file (because
196** one is a hard or symbolic link to the other) then if you set
197** an exclusive lock on fd1, then try to get an exclusive lock
198** on fd2, it works. I would have expected the second lock to
199** fail since there was already a lock on the file due to fd1.
200** But not so. Since both locks came from the same process, the
201** second overrides the first, even though they were on different
202** file descriptors opened on different file names.
203**
204** Bummer. If you ask me, this is broken. Badly broken. It means
205** that we cannot use POSIX locks to synchronize file access among
206** competing threads of the same process. POSIX locks will work fine
207** to synchronize access for threads in separate processes, but not
208** threads within the same process.
209**
210** To work around the problem, SQLite has to manage file locks internally
211** on its own. Whenever a new database is opened, we have to find the
212** specific inode of the database file (the inode is determined by the
213** st_dev and st_ino fields of the stat structure that fstat() fills in)
214** and check for locks already existing on that inode. When locks are
215** created or removed, we have to look at our own internal record of the
216** locks to see if another thread has previously set a lock on that same
217** inode.
218**
danielk1977ad94b582007-08-20 06:44:22 +0000219** The sqlite3_file structure for POSIX is no longer just an integer file
drhbbd42a62004-05-22 17:41:58 +0000220** descriptor. It is now a structure that holds the integer file
221** descriptor and a pointer to a structure that describes the internal
222** locks on the corresponding inode. There is one locking structure
danielk1977ad94b582007-08-20 06:44:22 +0000223** per inode, so if the same inode is opened twice, both unixFile structures
drhbbd42a62004-05-22 17:41:58 +0000224** point to the same locking structure. The locking structure keeps
225** a reference count (so we will know when to delete it) and a "cnt"
226** field that tells us its internal lock status. cnt==0 means the
227** file is unlocked. cnt==-1 means the file has an exclusive lock.
228** cnt>0 means there are cnt shared locks on the file.
229**
230** Any attempt to lock or unlock a file first checks the locking
231** structure. The fcntl() system call is only invoked to set a
232** POSIX lock if the internal lock structure transitions between
233** a locked and an unlocked state.
234**
235** 2004-Jan-11:
236** More recent discoveries about POSIX advisory locks. (The more
237** I discover, the more I realize the a POSIX advisory locks are
238** an abomination.)
239**
240** If you close a file descriptor that points to a file that has locks,
241** all locks on that file that are owned by the current process are
danielk1977ad94b582007-08-20 06:44:22 +0000242** released. To work around this problem, each unixFile structure contains
drhbbd42a62004-05-22 17:41:58 +0000243** a pointer to an openCnt structure. There is one openCnt structure
danielk1977ad94b582007-08-20 06:44:22 +0000244** per open inode, which means that multiple unixFile can point to a single
245** openCnt. When an attempt is made to close an unixFile, if there are
246** other unixFile open on the same inode that are holding locks, the call
drhbbd42a62004-05-22 17:41:58 +0000247** to close() the file descriptor is deferred until all of the locks clear.
248** The openCnt structure keeps a list of file descriptors that need to
249** be closed and that list is walked (and cleared) when the last lock
250** clears.
251**
252** First, under Linux threads, because each thread has a separate
253** process ID, lock operations in one thread do not override locks
254** to the same file in other threads. Linux threads behave like
255** separate processes in this respect. But, if you close a file
256** descriptor in linux threads, all locks are cleared, even locks
257** on other threads and even though the other threads have different
258** process IDs. Linux threads is inconsistent in this respect.
259** (I'm beginning to think that linux threads is an abomination too.)
260** The consequence of this all is that the hash table for the lockInfo
261** structure has to include the process id as part of its key because
262** locks in different threads are treated as distinct. But the
263** openCnt structure should not include the process id in its
264** key because close() clears lock on all threads, not just the current
265** thread. Were it not for this goofiness in linux threads, we could
266** combine the lockInfo and openCnt structures into a single structure.
drh5fdae772004-06-29 03:29:00 +0000267**
268** 2004-Jun-28:
269** On some versions of linux, threads can override each others locks.
270** On others not. Sometimes you can change the behavior on the same
271** system by setting the LD_ASSUME_KERNEL environment variable. The
272** POSIX standard is silent as to which behavior is correct, as far
273** as I can tell, so other versions of unix might show the same
274** inconsistency. There is no little doubt in my mind that posix
275** advisory locks and linux threads are profoundly broken.
276**
277** To work around the inconsistencies, we have to test at runtime
278** whether or not threads can override each others locks. This test
279** is run once, the first time any lock is attempted. A static
280** variable is set to record the results of this test for future
281** use.
drhbbd42a62004-05-22 17:41:58 +0000282*/
283
284/*
285** An instance of the following structure serves as the key used
drh5fdae772004-06-29 03:29:00 +0000286** to locate a particular lockInfo structure given its inode.
287**
288** If threads cannot override each others locks, then we set the
289** lockKey.tid field to the thread ID. If threads can override
drhf1a221e2006-01-15 17:27:17 +0000290** each others locks then tid is always set to zero. tid is omitted
291** if we compile without threading support.
drhbbd42a62004-05-22 17:41:58 +0000292*/
293struct lockKey {
drh5fdae772004-06-29 03:29:00 +0000294 dev_t dev; /* Device number */
295 ino_t ino; /* Inode number */
296#ifdef SQLITE_UNIX_THREADS
drhd9cb6ac2005-10-20 07:28:17 +0000297 pthread_t tid; /* Thread ID or zero if threads can override each other */
drh5fdae772004-06-29 03:29:00 +0000298#endif
drhbbd42a62004-05-22 17:41:58 +0000299};
300
301/*
302** An instance of the following structure is allocated for each open
303** inode on each thread with a different process ID. (Threads have
304** different process IDs on linux, but not on most other unixes.)
305**
danielk1977ad94b582007-08-20 06:44:22 +0000306** A single inode can have multiple file descriptors, so each unixFile
drhbbd42a62004-05-22 17:41:58 +0000307** structure contains a pointer to an instance of this object and this
danielk1977ad94b582007-08-20 06:44:22 +0000308** object keeps a count of the number of unixFile pointing to it.
drhbbd42a62004-05-22 17:41:58 +0000309*/
310struct lockInfo {
311 struct lockKey key; /* The lookup key */
drh2ac3ee92004-06-07 16:27:46 +0000312 int cnt; /* Number of SHARED locks held */
danielk19779a1d0ab2004-06-01 14:09:28 +0000313 int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
drhbbd42a62004-05-22 17:41:58 +0000314 int nRef; /* Number of pointers to this structure */
315};
316
317/*
318** An instance of the following structure serves as the key used
319** to locate a particular openCnt structure given its inode. This
drh5fdae772004-06-29 03:29:00 +0000320** is the same as the lockKey except that the thread ID is omitted.
drhbbd42a62004-05-22 17:41:58 +0000321*/
322struct openKey {
323 dev_t dev; /* Device number */
324 ino_t ino; /* Inode number */
325};
326
327/*
328** An instance of the following structure is allocated for each open
329** inode. This structure keeps track of the number of locks on that
330** inode. If a close is attempted against an inode that is holding
331** locks, the close is deferred until all locks clear by adding the
332** file descriptor to be closed to the pending list.
333*/
334struct openCnt {
335 struct openKey key; /* The lookup key */
336 int nRef; /* Number of pointers to this structure */
337 int nLock; /* Number of outstanding locks */
338 int nPending; /* Number of pending close() operations */
339 int *aPending; /* Malloced space holding fd's awaiting a close() */
340};
341
342/*
drhf1a221e2006-01-15 17:27:17 +0000343** These hash tables map inodes and file descriptors (really, lockKey and
344** openKey structures) into lockInfo and openCnt structures. Access to
345** these hash tables must be protected by a mutex.
drhbbd42a62004-05-22 17:41:58 +0000346*/
drh17435752007-08-16 04:30:38 +0000347static Hash lockHash = {SQLITE_HASH_BINARY, 0, 0, 0, 0, 0};
348static Hash openHash = {SQLITE_HASH_BINARY, 0, 0, 0, 0, 0};
drh5fdae772004-06-29 03:29:00 +0000349
drhbfe66312006-10-03 17:40:40 +0000350#ifdef SQLITE_ENABLE_LOCKING_STYLE
351/*
352** The locking styles are associated with the different file locking
353** capabilities supported by different file systems.
354**
355** POSIX locking style fully supports shared and exclusive byte-range locks
356** ADP locking only supports exclusive byte-range locks
357** FLOCK only supports a single file-global exclusive lock
358** DOTLOCK isn't a true locking style, it refers to the use of a special
359** file named the same as the database file with a '.lock' extension, this
360** can be used on file systems that do not offer any reliable file locking
361** NO locking means that no locking will be attempted, this is only used for
362** read-only file systems currently
363** UNSUPPORTED means that no locking will be attempted, this is only used for
364** file systems that are known to be unsupported
365*/
366typedef enum {
drhfd131da2007-08-07 17:13:03 +0000367 posixLockingStyle = 0, /* standard posix-advisory locks */
368 afpLockingStyle, /* use afp locks */
369 flockLockingStyle, /* use flock() */
370 dotlockLockingStyle, /* use <file>.lock files */
371 noLockingStyle, /* useful for read-only file system */
372 unsupportedLockingStyle /* indicates unsupported file system */
drhbfe66312006-10-03 17:40:40 +0000373} sqlite3LockingStyle;
374#endif /* SQLITE_ENABLE_LOCKING_STYLE */
375
danielk1977ad94b582007-08-20 06:44:22 +0000376/*
377** Helper functions to obtain and relinquish the global mutex.
378*/
danielk1977b4b47412007-08-17 15:53:36 +0000379static void enterMutex(){
380 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_GLOBAL));
381}
382static void leaveMutex(){
383 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_GLOBAL));
384}
385
drh5fdae772004-06-29 03:29:00 +0000386#ifdef SQLITE_UNIX_THREADS
387/*
388** This variable records whether or not threads can override each others
389** locks.
390**
391** 0: No. Threads cannot override each others locks.
392** 1: Yes. Threads can override each others locks.
393** -1: We don't know yet.
drhf1a221e2006-01-15 17:27:17 +0000394**
drh5062d3a2006-01-31 23:03:35 +0000395** On some systems, we know at compile-time if threads can override each
396** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro
397** will be set appropriately. On other systems, we have to check at
398** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is
399** undefined.
400**
drhf1a221e2006-01-15 17:27:17 +0000401** This variable normally has file scope only. But during testing, we make
402** it a global so that the test code can change its value in order to verify
403** that the right stuff happens in either case.
drh5fdae772004-06-29 03:29:00 +0000404*/
drh5062d3a2006-01-31 23:03:35 +0000405#ifndef SQLITE_THREAD_OVERRIDE_LOCK
406# define SQLITE_THREAD_OVERRIDE_LOCK -1
407#endif
drh029b44b2006-01-15 00:13:15 +0000408#ifdef SQLITE_TEST
drh5062d3a2006-01-31 23:03:35 +0000409int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh029b44b2006-01-15 00:13:15 +0000410#else
drh5062d3a2006-01-31 23:03:35 +0000411static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh029b44b2006-01-15 00:13:15 +0000412#endif
drh5fdae772004-06-29 03:29:00 +0000413
414/*
415** This structure holds information passed into individual test
416** threads by the testThreadLockingBehavior() routine.
417*/
418struct threadTestData {
419 int fd; /* File to be locked */
420 struct flock lock; /* The locking operation */
421 int result; /* Result of the locking operation */
422};
423
drh2b4b5962005-06-15 17:47:55 +0000424#ifdef SQLITE_LOCK_TRACE
425/*
426** Print out information about all locking operations.
427**
428** This routine is used for troubleshooting locks on multithreaded
429** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
430** command-line option on the compiler. This code is normally
drhf1a221e2006-01-15 17:27:17 +0000431** turned off.
drh2b4b5962005-06-15 17:47:55 +0000432*/
433static int lockTrace(int fd, int op, struct flock *p){
434 char *zOpName, *zType;
435 int s;
436 int savedErrno;
437 if( op==F_GETLK ){
438 zOpName = "GETLK";
439 }else if( op==F_SETLK ){
440 zOpName = "SETLK";
441 }else{
442 s = fcntl(fd, op, p);
443 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
444 return s;
445 }
446 if( p->l_type==F_RDLCK ){
447 zType = "RDLCK";
448 }else if( p->l_type==F_WRLCK ){
449 zType = "WRLCK";
450 }else if( p->l_type==F_UNLCK ){
451 zType = "UNLCK";
452 }else{
453 assert( 0 );
454 }
455 assert( p->l_whence==SEEK_SET );
456 s = fcntl(fd, op, p);
457 savedErrno = errno;
458 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
459 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
460 (int)p->l_pid, s);
drhe2396a12007-03-29 20:19:58 +0000461 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
drh2b4b5962005-06-15 17:47:55 +0000462 struct flock l2;
463 l2 = *p;
464 fcntl(fd, F_GETLK, &l2);
465 if( l2.l_type==F_RDLCK ){
466 zType = "RDLCK";
467 }else if( l2.l_type==F_WRLCK ){
468 zType = "WRLCK";
469 }else if( l2.l_type==F_UNLCK ){
470 zType = "UNLCK";
471 }else{
472 assert( 0 );
473 }
474 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
475 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
476 }
477 errno = savedErrno;
478 return s;
479}
480#define fcntl lockTrace
481#endif /* SQLITE_LOCK_TRACE */
482
drh5fdae772004-06-29 03:29:00 +0000483/*
484** The testThreadLockingBehavior() routine launches two separate
485** threads on this routine. This routine attempts to lock a file
486** descriptor then returns. The success or failure of that attempt
487** allows the testThreadLockingBehavior() procedure to determine
488** whether or not threads can override each others locks.
489*/
490static void *threadLockingTest(void *pArg){
491 struct threadTestData *pData = (struct threadTestData*)pArg;
492 pData->result = fcntl(pData->fd, F_SETLK, &pData->lock);
493 return pArg;
494}
495
496/*
497** This procedure attempts to determine whether or not threads
498** can override each others locks then sets the
499** threadsOverrideEachOthersLocks variable appropriately.
500*/
danielk19774d5238f2006-01-27 06:32:00 +0000501static void testThreadLockingBehavior(int fd_orig){
drh5fdae772004-06-29 03:29:00 +0000502 int fd;
503 struct threadTestData d[2];
504 pthread_t t[2];
505
506 fd = dup(fd_orig);
507 if( fd<0 ) return;
508 memset(d, 0, sizeof(d));
509 d[0].fd = fd;
510 d[0].lock.l_type = F_RDLCK;
511 d[0].lock.l_len = 1;
512 d[0].lock.l_start = 0;
513 d[0].lock.l_whence = SEEK_SET;
514 d[1] = d[0];
515 d[1].lock.l_type = F_WRLCK;
516 pthread_create(&t[0], 0, threadLockingTest, &d[0]);
517 pthread_create(&t[1], 0, threadLockingTest, &d[1]);
518 pthread_join(t[0], 0);
519 pthread_join(t[1], 0);
520 close(fd);
521 threadsOverrideEachOthersLocks = d[0].result==0 && d[1].result==0;
522}
523#endif /* SQLITE_UNIX_THREADS */
524
drhbbd42a62004-05-22 17:41:58 +0000525/*
526** Release a lockInfo structure previously allocated by findLockInfo().
527*/
528static void releaseLockInfo(struct lockInfo *pLock){
drhbfe66312006-10-03 17:40:40 +0000529 if (pLock == NULL)
530 return;
drhbbd42a62004-05-22 17:41:58 +0000531 pLock->nRef--;
532 if( pLock->nRef==0 ){
533 sqlite3HashInsert(&lockHash, &pLock->key, sizeof(pLock->key), 0);
drh17435752007-08-16 04:30:38 +0000534 sqlite3_free(pLock);
drhbbd42a62004-05-22 17:41:58 +0000535 }
536}
537
538/*
539** Release a openCnt structure previously allocated by findLockInfo().
540*/
541static void releaseOpenCnt(struct openCnt *pOpen){
drhbfe66312006-10-03 17:40:40 +0000542 if (pOpen == NULL)
543 return;
drhbbd42a62004-05-22 17:41:58 +0000544 pOpen->nRef--;
545 if( pOpen->nRef==0 ){
546 sqlite3HashInsert(&openHash, &pOpen->key, sizeof(pOpen->key), 0);
drh64b1bea2006-01-15 02:30:57 +0000547 free(pOpen->aPending);
drh17435752007-08-16 04:30:38 +0000548 sqlite3_free(pOpen);
drhbbd42a62004-05-22 17:41:58 +0000549 }
550}
551
drhbfe66312006-10-03 17:40:40 +0000552#ifdef SQLITE_ENABLE_LOCKING_STYLE
553/*
554** Tests a byte-range locking query to see if byte range locks are
555** supported, if not we fall back to dotlockLockingStyle.
556*/
danielk1977ad94b582007-08-20 06:44:22 +0000557static sqlite3LockingStyle sqlite3TestLockingStyle(
558 const char *filePath,
559 int fd
560){
drhbfe66312006-10-03 17:40:40 +0000561 /* test byte-range lock using fcntl */
562 struct flock lockInfo;
563
564 lockInfo.l_len = 1;
565 lockInfo.l_start = 0;
566 lockInfo.l_whence = SEEK_SET;
567 lockInfo.l_type = F_RDLCK;
568
danielk1977ad94b582007-08-20 06:44:22 +0000569 if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
drhbfe66312006-10-03 17:40:40 +0000570 return posixLockingStyle;
571 }
572
573 /* testing for flock can give false positives. So if if the above test
574 ** fails, then we fall back to using dot-lock style locking.
575 */
576 return dotlockLockingStyle;
577}
578
579/*
580** Examines the f_fstypename entry in the statfs structure as returned by
581** stat() for the file system hosting the database file, assigns the
582** appropriate locking style based on it's value. These values and
583** assignments are based on Darwin/OSX behavior and have not been tested on
584** other systems.
585*/
danielk1977ad94b582007-08-20 06:44:22 +0000586static sqlite3LockingStyle sqlite3DetectLockingStyle(
587 const char *filePath,
588 int fd
589){
drhbfe66312006-10-03 17:40:40 +0000590
591#ifdef SQLITE_FIXED_LOCKING_STYLE
592 return (sqlite3LockingStyle)SQLITE_FIXED_LOCKING_STYLE;
593#else
594 struct statfs fsInfo;
595
596 if (statfs(filePath, &fsInfo) == -1)
597 return sqlite3TestLockingStyle(filePath, fd);
598
599 if (fsInfo.f_flags & MNT_RDONLY)
600 return noLockingStyle;
601
602 if( (!strcmp(fsInfo.f_fstypename, "hfs")) ||
603 (!strcmp(fsInfo.f_fstypename, "ufs")) )
drhfd131da2007-08-07 17:13:03 +0000604 return posixLockingStyle;
drhbfe66312006-10-03 17:40:40 +0000605
606 if(!strcmp(fsInfo.f_fstypename, "afpfs"))
607 return afpLockingStyle;
608
609 if(!strcmp(fsInfo.f_fstypename, "nfs"))
610 return sqlite3TestLockingStyle(filePath, fd);
611
612 if(!strcmp(fsInfo.f_fstypename, "smbfs"))
613 return flockLockingStyle;
614
615 if(!strcmp(fsInfo.f_fstypename, "msdos"))
616 return dotlockLockingStyle;
617
618 if(!strcmp(fsInfo.f_fstypename, "webdav"))
619 return unsupportedLockingStyle;
620
621 return sqlite3TestLockingStyle(filePath, fd);
drh3b62b2f2007-06-08 18:27:03 +0000622#endif /* SQLITE_FIXED_LOCKING_STYLE */
drhbfe66312006-10-03 17:40:40 +0000623}
624
625#endif /* SQLITE_ENABLE_LOCKING_STYLE */
626
drhbbd42a62004-05-22 17:41:58 +0000627/*
628** Given a file descriptor, locate lockInfo and openCnt structures that
drh029b44b2006-01-15 00:13:15 +0000629** describes that file descriptor. Create new ones if necessary. The
630** return values might be uninitialized if an error occurs.
drhbbd42a62004-05-22 17:41:58 +0000631**
632** Return the number of errors.
633*/
drh38f82712004-06-18 17:10:16 +0000634static int findLockInfo(
drhbbd42a62004-05-22 17:41:58 +0000635 int fd, /* The file descriptor used in the key */
636 struct lockInfo **ppLock, /* Return the lockInfo structure here */
drh5fdae772004-06-29 03:29:00 +0000637 struct openCnt **ppOpen /* Return the openCnt structure here */
drhbbd42a62004-05-22 17:41:58 +0000638){
639 int rc;
640 struct lockKey key1;
641 struct openKey key2;
642 struct stat statbuf;
643 struct lockInfo *pLock;
644 struct openCnt *pOpen;
645 rc = fstat(fd, &statbuf);
646 if( rc!=0 ) return 1;
danielk1977441b09a2006-01-05 13:48:29 +0000647
drhbbd42a62004-05-22 17:41:58 +0000648 memset(&key1, 0, sizeof(key1));
649 key1.dev = statbuf.st_dev;
650 key1.ino = statbuf.st_ino;
drh5fdae772004-06-29 03:29:00 +0000651#ifdef SQLITE_UNIX_THREADS
652 if( threadsOverrideEachOthersLocks<0 ){
653 testThreadLockingBehavior(fd);
654 }
655 key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
656#endif
drhbbd42a62004-05-22 17:41:58 +0000657 memset(&key2, 0, sizeof(key2));
658 key2.dev = statbuf.st_dev;
659 key2.ino = statbuf.st_ino;
660 pLock = (struct lockInfo*)sqlite3HashFind(&lockHash, &key1, sizeof(key1));
661 if( pLock==0 ){
662 struct lockInfo *pOld;
drh17435752007-08-16 04:30:38 +0000663 pLock = sqlite3_malloc( sizeof(*pLock) );
danielk1977441b09a2006-01-05 13:48:29 +0000664 if( pLock==0 ){
665 rc = 1;
666 goto exit_findlockinfo;
667 }
drhbbd42a62004-05-22 17:41:58 +0000668 pLock->key = key1;
669 pLock->nRef = 1;
670 pLock->cnt = 0;
danielk19779a1d0ab2004-06-01 14:09:28 +0000671 pLock->locktype = 0;
drhbbd42a62004-05-22 17:41:58 +0000672 pOld = sqlite3HashInsert(&lockHash, &pLock->key, sizeof(key1), pLock);
673 if( pOld!=0 ){
674 assert( pOld==pLock );
drh17435752007-08-16 04:30:38 +0000675 sqlite3_free(pLock);
danielk1977441b09a2006-01-05 13:48:29 +0000676 rc = 1;
677 goto exit_findlockinfo;
drhbbd42a62004-05-22 17:41:58 +0000678 }
679 }else{
680 pLock->nRef++;
681 }
682 *ppLock = pLock;
drh029b44b2006-01-15 00:13:15 +0000683 if( ppOpen!=0 ){
684 pOpen = (struct openCnt*)sqlite3HashFind(&openHash, &key2, sizeof(key2));
drhbbd42a62004-05-22 17:41:58 +0000685 if( pOpen==0 ){
drh029b44b2006-01-15 00:13:15 +0000686 struct openCnt *pOld;
drh17435752007-08-16 04:30:38 +0000687 pOpen = sqlite3_malloc( sizeof(*pOpen) );
drh029b44b2006-01-15 00:13:15 +0000688 if( pOpen==0 ){
689 releaseLockInfo(pLock);
690 rc = 1;
691 goto exit_findlockinfo;
692 }
693 pOpen->key = key2;
694 pOpen->nRef = 1;
695 pOpen->nLock = 0;
696 pOpen->nPending = 0;
697 pOpen->aPending = 0;
698 pOld = sqlite3HashInsert(&openHash, &pOpen->key, sizeof(key2), pOpen);
699 if( pOld!=0 ){
700 assert( pOld==pOpen );
drh17435752007-08-16 04:30:38 +0000701 sqlite3_free(pOpen);
drh029b44b2006-01-15 00:13:15 +0000702 releaseLockInfo(pLock);
703 rc = 1;
704 goto exit_findlockinfo;
705 }
706 }else{
707 pOpen->nRef++;
drhbbd42a62004-05-22 17:41:58 +0000708 }
drh029b44b2006-01-15 00:13:15 +0000709 *ppOpen = pOpen;
drhbbd42a62004-05-22 17:41:58 +0000710 }
danielk1977441b09a2006-01-05 13:48:29 +0000711
712exit_findlockinfo:
danielk1977441b09a2006-01-05 13:48:29 +0000713 return rc;
drhbbd42a62004-05-22 17:41:58 +0000714}
715
drh64b1bea2006-01-15 02:30:57 +0000716#ifdef SQLITE_DEBUG
717/*
718** Helper function for printing out trace information from debugging
719** binaries. This returns the string represetation of the supplied
720** integer lock-type.
721*/
722static const char *locktypeName(int locktype){
723 switch( locktype ){
724 case NO_LOCK: return "NONE";
725 case SHARED_LOCK: return "SHARED";
726 case RESERVED_LOCK: return "RESERVED";
727 case PENDING_LOCK: return "PENDING";
728 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
729 }
730 return "ERROR";
731}
732#endif
733
drhbbd42a62004-05-22 17:41:58 +0000734/*
drh029b44b2006-01-15 00:13:15 +0000735** If we are currently in a different thread than the thread that the
736** unixFile argument belongs to, then transfer ownership of the unixFile
737** over to the current thread.
738**
739** A unixFile is only owned by a thread on systems where one thread is
740** unable to override locks created by a different thread. RedHat9 is
741** an example of such a system.
742**
743** Ownership transfer is only allowed if the unixFile is currently unlocked.
744** If the unixFile is locked and an ownership is wrong, then return
drhf1a221e2006-01-15 17:27:17 +0000745** SQLITE_MISUSE. SQLITE_OK is returned if everything works.
drh029b44b2006-01-15 00:13:15 +0000746*/
747#ifdef SQLITE_UNIX_THREADS
748static int transferOwnership(unixFile *pFile){
drh64b1bea2006-01-15 02:30:57 +0000749 int rc;
drh029b44b2006-01-15 00:13:15 +0000750 pthread_t hSelf;
751 if( threadsOverrideEachOthersLocks ){
752 /* Ownership transfers not needed on this system */
753 return SQLITE_OK;
754 }
755 hSelf = pthread_self();
756 if( pthread_equal(pFile->tid, hSelf) ){
757 /* We are still in the same thread */
drh4f0c5872007-03-26 22:05:01 +0000758 OSTRACE1("No-transfer, same thread\n");
drh029b44b2006-01-15 00:13:15 +0000759 return SQLITE_OK;
760 }
761 if( pFile->locktype!=NO_LOCK ){
762 /* We cannot change ownership while we are holding a lock! */
763 return SQLITE_MISUSE;
764 }
drh4f0c5872007-03-26 22:05:01 +0000765 OSTRACE4("Transfer ownership of %d from %d to %d\n",
766 pFile->h, pFile->tid, hSelf);
drh029b44b2006-01-15 00:13:15 +0000767 pFile->tid = hSelf;
drhbfe66312006-10-03 17:40:40 +0000768 if (pFile->pLock != NULL) {
769 releaseLockInfo(pFile->pLock);
770 rc = findLockInfo(pFile->h, &pFile->pLock, 0);
drh4f0c5872007-03-26 22:05:01 +0000771 OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +0000772 locktypeName(pFile->locktype),
773 locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
774 return rc;
775 } else {
776 return SQLITE_OK;
777 }
drh029b44b2006-01-15 00:13:15 +0000778}
779#else
drhf1a221e2006-01-15 17:27:17 +0000780 /* On single-threaded builds, ownership transfer is a no-op */
drh029b44b2006-01-15 00:13:15 +0000781# define transferOwnership(X) SQLITE_OK
782#endif
783
784/*
drhb912b282006-03-23 22:42:20 +0000785** Seek to the offset in id->offset then read cnt bytes into pBuf.
786** Return the number of bytes actually read. Update the offset.
787*/
danielk197762079062007-08-15 17:08:46 +0000788static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
drhb912b282006-03-23 22:42:20 +0000789 int got;
drh8ebf6702007-02-06 11:11:08 +0000790 i64 newOffset;
drh15d00c42007-02-27 02:01:14 +0000791 TIMER_START;
drh8350a212007-03-22 15:22:06 +0000792#if defined(USE_PREAD)
danielk197762079062007-08-15 17:08:46 +0000793 got = pread(id->h, pBuf, cnt, offset);
drhbb5f18d2007-04-06 18:23:17 +0000794 SimulateIOError( got = -1 );
drh8350a212007-03-22 15:22:06 +0000795#elif defined(USE_PREAD64)
danielk197762079062007-08-15 17:08:46 +0000796 got = pread64(id->h, pBuf, cnt, offset);
drhbb5f18d2007-04-06 18:23:17 +0000797 SimulateIOError( got = -1 );
drhb912b282006-03-23 22:42:20 +0000798#else
danielk197762079062007-08-15 17:08:46 +0000799 newOffset = lseek(id->h, offset, SEEK_SET);
drhbb5f18d2007-04-06 18:23:17 +0000800 SimulateIOError( newOffset-- );
danielk197762079062007-08-15 17:08:46 +0000801 if( newOffset!=offset ){
drh8ebf6702007-02-06 11:11:08 +0000802 return -1;
803 }
drhb912b282006-03-23 22:42:20 +0000804 got = read(id->h, pBuf, cnt);
805#endif
drh15d00c42007-02-27 02:01:14 +0000806 TIMER_END;
danielk1977967a4a12007-08-20 14:23:44 +0000807 OSTRACE5("READ %-3d %5d %7lld %d\n", id->h, got, offset, TIMER_ELAPSED);
drhb912b282006-03-23 22:42:20 +0000808 return got;
809}
810
811/*
drhbbd42a62004-05-22 17:41:58 +0000812** Read data from a file into a buffer. Return SQLITE_OK if all
813** bytes were read successfully and SQLITE_IOERR if anything goes
814** wrong.
815*/
danielk197762079062007-08-15 17:08:46 +0000816static int unixRead(
817 sqlite3_file *id,
818 void *pBuf,
819 int amt,
820 sqlite3_int64 offset
821){
drhbbd42a62004-05-22 17:41:58 +0000822 int got;
drh9cbe6352005-11-29 03:13:21 +0000823 assert( id );
danielk197762079062007-08-15 17:08:46 +0000824 got = seekAndRead((unixFile*)id, offset, pBuf, amt);
drhbbd42a62004-05-22 17:41:58 +0000825 if( got==amt ){
826 return SQLITE_OK;
drh4ac285a2006-09-15 07:28:50 +0000827 }else if( got<0 ){
828 return SQLITE_IOERR_READ;
drhbbd42a62004-05-22 17:41:58 +0000829 }else{
drhbafda092007-01-03 23:36:22 +0000830 memset(&((char*)pBuf)[got], 0, amt-got);
drh4ac285a2006-09-15 07:28:50 +0000831 return SQLITE_IOERR_SHORT_READ;
drhbbd42a62004-05-22 17:41:58 +0000832 }
833}
834
835/*
drhb912b282006-03-23 22:42:20 +0000836** Seek to the offset in id->offset then read cnt bytes into pBuf.
837** Return the number of bytes actually read. Update the offset.
838*/
danielk197762079062007-08-15 17:08:46 +0000839static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
drhb912b282006-03-23 22:42:20 +0000840 int got;
drh8ebf6702007-02-06 11:11:08 +0000841 i64 newOffset;
drh15d00c42007-02-27 02:01:14 +0000842 TIMER_START;
drh8350a212007-03-22 15:22:06 +0000843#if defined(USE_PREAD)
danielk197762079062007-08-15 17:08:46 +0000844 got = pwrite(id->h, pBuf, cnt, offset);
drh8350a212007-03-22 15:22:06 +0000845#elif defined(USE_PREAD64)
danielk197762079062007-08-15 17:08:46 +0000846 got = pwrite64(id->h, pBuf, cnt, offset);
drhb912b282006-03-23 22:42:20 +0000847#else
danielk197762079062007-08-15 17:08:46 +0000848 newOffset = lseek(id->h, offset, SEEK_SET);
849 if( newOffset!=offset ){
drh8ebf6702007-02-06 11:11:08 +0000850 return -1;
851 }
drhb912b282006-03-23 22:42:20 +0000852 got = write(id->h, pBuf, cnt);
853#endif
drh15d00c42007-02-27 02:01:14 +0000854 TIMER_END;
danielk197762079062007-08-15 17:08:46 +0000855 OSTRACE5("WRITE %-3d %5d %7lld %d\n", id->h, got, offset, TIMER_ELAPSED);
drhb912b282006-03-23 22:42:20 +0000856 return got;
857}
858
859
860/*
drhbbd42a62004-05-22 17:41:58 +0000861** Write data from a buffer into a file. Return SQLITE_OK on success
862** or some other error code on failure.
863*/
danielk197762079062007-08-15 17:08:46 +0000864static int unixWrite(
865 sqlite3_file *id,
866 const void *pBuf,
867 int amt,
868 sqlite3_int64 offset
869){
drhbbd42a62004-05-22 17:41:58 +0000870 int wrote = 0;
drh9cbe6352005-11-29 03:13:21 +0000871 assert( id );
drh4c7f9412005-02-03 00:29:47 +0000872 assert( amt>0 );
danielk197762079062007-08-15 17:08:46 +0000873 while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){
drhbbd42a62004-05-22 17:41:58 +0000874 amt -= wrote;
danielk197762079062007-08-15 17:08:46 +0000875 offset += wrote;
drhbbd42a62004-05-22 17:41:58 +0000876 pBuf = &((char*)pBuf)[wrote];
877 }
drh59685932006-09-14 13:47:11 +0000878 SimulateIOError(( wrote=(-1), amt=1 ));
879 SimulateDiskfullError(( wrote=0, amt=1 ));
drhbbd42a62004-05-22 17:41:58 +0000880 if( amt>0 ){
drh59685932006-09-14 13:47:11 +0000881 if( wrote<0 ){
drh4ac285a2006-09-15 07:28:50 +0000882 return SQLITE_IOERR_WRITE;
drh59685932006-09-14 13:47:11 +0000883 }else{
884 return SQLITE_FULL;
885 }
drhbbd42a62004-05-22 17:41:58 +0000886 }
887 return SQLITE_OK;
888}
889
drhb851b2c2005-03-10 14:11:12 +0000890#ifdef SQLITE_TEST
891/*
892** Count the number of fullsyncs and normal syncs. This is used to test
893** that syncs and fullsyncs are occuring at the right times.
894*/
895int sqlite3_sync_count = 0;
896int sqlite3_fullsync_count = 0;
897#endif
898
drhf2f23912005-10-05 10:29:36 +0000899/*
900** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.
901** Otherwise use fsync() in its place.
902*/
903#ifndef HAVE_FDATASYNC
904# define fdatasync fsync
905#endif
906
drhac530b12006-02-11 01:25:50 +0000907/*
908** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
909** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
910** only available on Mac OS X. But that could change.
911*/
912#ifdef F_FULLFSYNC
913# define HAVE_FULLFSYNC 1
914#else
915# define HAVE_FULLFSYNC 0
916#endif
917
drhb851b2c2005-03-10 14:11:12 +0000918
drhbbd42a62004-05-22 17:41:58 +0000919/*
drhdd809b02004-07-17 21:44:57 +0000920** The fsync() system call does not work as advertised on many
921** unix systems. The following procedure is an attempt to make
922** it work better.
drh1398ad32005-01-19 23:24:50 +0000923**
924** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
925** for testing when we want to run through the test suite quickly.
926** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
927** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
928** or power failure will likely corrupt the database file.
drhdd809b02004-07-17 21:44:57 +0000929*/
drheb796a72005-09-08 12:38:41 +0000930static int full_fsync(int fd, int fullSync, int dataOnly){
drhdd809b02004-07-17 21:44:57 +0000931 int rc;
drhb851b2c2005-03-10 14:11:12 +0000932
933 /* Record the number of times that we do a normal fsync() and
934 ** FULLSYNC. This is used during testing to verify that this procedure
935 ** gets called with the correct arguments.
936 */
937#ifdef SQLITE_TEST
938 if( fullSync ) sqlite3_fullsync_count++;
939 sqlite3_sync_count++;
940#endif
941
942 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
943 ** no-op
944 */
945#ifdef SQLITE_NO_SYNC
946 rc = SQLITE_OK;
947#else
948
drhac530b12006-02-11 01:25:50 +0000949#if HAVE_FULLFSYNC
drhb851b2c2005-03-10 14:11:12 +0000950 if( fullSync ){
drhf30cc942005-03-11 17:52:34 +0000951 rc = fcntl(fd, F_FULLFSYNC, 0);
aswiftae0943b2007-01-31 23:37:07 +0000952 }else{
953 rc = 1;
954 }
955 /* If the FULLFSYNC failed, fall back to attempting an fsync().
956 * It shouldn't be possible for fullfsync to fail on the local
957 * file system (on OSX), so failure indicates that FULLFSYNC
958 * isn't supported for this file system. So, attempt an fsync
959 * and (for now) ignore the overhead of a superfluous fcntl call.
960 * It'd be better to detect fullfsync support once and avoid
961 * the fcntl call every time sync is called.
962 */
963 if( rc ) rc = fsync(fd);
964
965#else
drheb796a72005-09-08 12:38:41 +0000966 if( dataOnly ){
967 rc = fdatasync(fd);
drhf2f23912005-10-05 10:29:36 +0000968 }else{
drheb796a72005-09-08 12:38:41 +0000969 rc = fsync(fd);
970 }
aswiftae0943b2007-01-31 23:37:07 +0000971#endif /* HAVE_FULLFSYNC */
drhb851b2c2005-03-10 14:11:12 +0000972#endif /* defined(SQLITE_NO_SYNC) */
973
drhdd809b02004-07-17 21:44:57 +0000974 return rc;
975}
976
977/*
drhbbd42a62004-05-22 17:41:58 +0000978** Make sure all writes to a particular file are committed to disk.
979**
drheb796a72005-09-08 12:38:41 +0000980** If dataOnly==0 then both the file itself and its metadata (file
981** size, access time, etc) are synced. If dataOnly!=0 then only the
982** file data is synced.
983**
drhbbd42a62004-05-22 17:41:58 +0000984** Under Unix, also make sure that the directory entry for the file
985** has been created by fsync-ing the directory that contains the file.
986** If we do not do this and we encounter a power failure, the directory
987** entry for the journal might not exist after we reboot. The next
988** SQLite to access the file will not know that the journal exists (because
989** the directory entry for the journal was never created) and the transaction
990** will not roll back - possibly leading to database corruption.
991*/
danielk197790949c22007-08-17 16:50:38 +0000992static int unixSync(sqlite3_file *id, int flags){
drh59685932006-09-14 13:47:11 +0000993 int rc;
drh054889e2005-11-30 03:20:31 +0000994 unixFile *pFile = (unixFile*)id;
danielk197790949c22007-08-17 16:50:38 +0000995
danielk1977f036aef2007-08-20 05:36:51 +0000996 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
997 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
998
999 /* Check that one of SQLITE_SYNC_NORMAL, FULL or BARRIER was passed */
1000 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
1001 || (flags&0x0F)==SQLITE_SYNC_FULL
1002 || (flags&0x0F)==SQLITE_SYNC_BARRIER
1003 );
danielk197790949c22007-08-17 16:50:38 +00001004
drh054889e2005-11-30 03:20:31 +00001005 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001006 OSTRACE2("SYNC %-3d\n", pFile->h);
danielk197790949c22007-08-17 16:50:38 +00001007 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
drh59685932006-09-14 13:47:11 +00001008 SimulateIOError( rc=1 );
1009 if( rc ){
drh4ac285a2006-09-15 07:28:50 +00001010 return SQLITE_IOERR_FSYNC;
drhbbd42a62004-05-22 17:41:58 +00001011 }
drh054889e2005-11-30 03:20:31 +00001012 if( pFile->dirfd>=0 ){
drh4f0c5872007-03-26 22:05:01 +00001013 OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
danielk197790949c22007-08-17 16:50:38 +00001014 HAVE_FULLFSYNC, isFullsync);
danielk1977d7c03f72005-11-25 10:38:22 +00001015#ifndef SQLITE_DISABLE_DIRSYNC
drhac530b12006-02-11 01:25:50 +00001016 /* The directory sync is only attempted if full_fsync is
1017 ** turned off or unavailable. If a full_fsync occurred above,
1018 ** then the directory sync is superfluous.
1019 */
danielk197790949c22007-08-17 16:50:38 +00001020 if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
drhac530b12006-02-11 01:25:50 +00001021 /*
1022 ** We have received multiple reports of fsync() returning
drh86631a52006-02-09 23:05:51 +00001023 ** errors when applied to directories on certain file systems.
1024 ** A failed directory sync is not a big deal. So it seems
1025 ** better to ignore the error. Ticket #1657
1026 */
1027 /* return SQLITE_IOERR; */
danielk19770964b232005-11-25 08:47:57 +00001028 }
danielk1977d7c03f72005-11-25 10:38:22 +00001029#endif
drh054889e2005-11-30 03:20:31 +00001030 close(pFile->dirfd); /* Only need to sync once, so close the directory */
1031 pFile->dirfd = -1; /* when we are done. */
drha2854222004-06-17 19:04:17 +00001032 }
drha2854222004-06-17 19:04:17 +00001033 return SQLITE_OK;
drhbbd42a62004-05-22 17:41:58 +00001034}
1035
1036/*
1037** Truncate an open file to a specified size
1038*/
danielk197762079062007-08-15 17:08:46 +00001039static int unixTruncate(sqlite3_file *id, i64 nByte){
drh59685932006-09-14 13:47:11 +00001040 int rc;
drh9cbe6352005-11-29 03:13:21 +00001041 assert( id );
drh63fff5f2007-06-19 10:50:38 +00001042 rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);
drh59685932006-09-14 13:47:11 +00001043 SimulateIOError( rc=1 );
1044 if( rc ){
drh4ac285a2006-09-15 07:28:50 +00001045 return SQLITE_IOERR_TRUNCATE;
drh59685932006-09-14 13:47:11 +00001046 }else{
1047 return SQLITE_OK;
1048 }
drhbbd42a62004-05-22 17:41:58 +00001049}
1050
1051/*
1052** Determine the current size of a file in bytes
1053*/
danielk197762079062007-08-15 17:08:46 +00001054static int unixFileSize(sqlite3_file *id, i64 *pSize){
drh59685932006-09-14 13:47:11 +00001055 int rc;
drhbbd42a62004-05-22 17:41:58 +00001056 struct stat buf;
drh9cbe6352005-11-29 03:13:21 +00001057 assert( id );
drh59685932006-09-14 13:47:11 +00001058 rc = fstat(((unixFile*)id)->h, &buf);
1059 SimulateIOError( rc=1 );
1060 if( rc!=0 ){
drh4ac285a2006-09-15 07:28:50 +00001061 return SQLITE_IOERR_FSTAT;
drhbbd42a62004-05-22 17:41:58 +00001062 }
1063 *pSize = buf.st_size;
1064 return SQLITE_OK;
1065}
1066
danielk19779a1d0ab2004-06-01 14:09:28 +00001067/*
danielk197713adf8a2004-06-03 16:08:41 +00001068** This routine checks if there is a RESERVED lock held on the specified
1069** file by this or any other process. If such a lock is held, return
drh2ac3ee92004-06-07 16:27:46 +00001070** non-zero. If the file is unlocked or holds only SHARED locks, then
1071** return zero.
danielk197713adf8a2004-06-03 16:08:41 +00001072*/
danielk197762079062007-08-15 17:08:46 +00001073static int unixCheckReservedLock(sqlite3_file *id){
danielk197713adf8a2004-06-03 16:08:41 +00001074 int r = 0;
drh054889e2005-11-30 03:20:31 +00001075 unixFile *pFile = (unixFile*)id;
danielk197713adf8a2004-06-03 16:08:41 +00001076
drh054889e2005-11-30 03:20:31 +00001077 assert( pFile );
danielk1977b4b47412007-08-17 15:53:36 +00001078 enterMutex(); /* Because pFile->pLock is shared across threads */
danielk197713adf8a2004-06-03 16:08:41 +00001079
1080 /* Check if a thread in this process holds such a lock */
drh054889e2005-11-30 03:20:31 +00001081 if( pFile->pLock->locktype>SHARED_LOCK ){
danielk197713adf8a2004-06-03 16:08:41 +00001082 r = 1;
1083 }
1084
drh2ac3ee92004-06-07 16:27:46 +00001085 /* Otherwise see if some other process holds it.
danielk197713adf8a2004-06-03 16:08:41 +00001086 */
1087 if( !r ){
1088 struct flock lock;
1089 lock.l_whence = SEEK_SET;
drh2ac3ee92004-06-07 16:27:46 +00001090 lock.l_start = RESERVED_BYTE;
1091 lock.l_len = 1;
1092 lock.l_type = F_WRLCK;
drh054889e2005-11-30 03:20:31 +00001093 fcntl(pFile->h, F_GETLK, &lock);
danielk197713adf8a2004-06-03 16:08:41 +00001094 if( lock.l_type!=F_UNLCK ){
1095 r = 1;
1096 }
1097 }
1098
danielk1977b4b47412007-08-17 15:53:36 +00001099 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001100 OSTRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
danielk197713adf8a2004-06-03 16:08:41 +00001101
1102 return r;
1103}
1104
1105/*
danielk19779a1d0ab2004-06-01 14:09:28 +00001106** Lock the file with the lock specified by parameter locktype - one
1107** of the following:
1108**
drh2ac3ee92004-06-07 16:27:46 +00001109** (1) SHARED_LOCK
1110** (2) RESERVED_LOCK
1111** (3) PENDING_LOCK
1112** (4) EXCLUSIVE_LOCK
1113**
drhb3e04342004-06-08 00:47:47 +00001114** Sometimes when requesting one lock state, additional lock states
1115** are inserted in between. The locking might fail on one of the later
1116** transitions leaving the lock state different from what it started but
1117** still short of its goal. The following chart shows the allowed
1118** transitions and the inserted intermediate states:
1119**
1120** UNLOCKED -> SHARED
1121** SHARED -> RESERVED
1122** SHARED -> (PENDING) -> EXCLUSIVE
1123** RESERVED -> (PENDING) -> EXCLUSIVE
1124** PENDING -> EXCLUSIVE
drh2ac3ee92004-06-07 16:27:46 +00001125**
drha6abd042004-06-09 17:37:22 +00001126** This routine will only increase a lock. Use the sqlite3OsUnlock()
1127** routine to lower a locking level.
danielk19779a1d0ab2004-06-01 14:09:28 +00001128*/
danielk197762079062007-08-15 17:08:46 +00001129static int unixLock(sqlite3_file *id, int locktype){
danielk1977f42f25c2004-06-25 07:21:28 +00001130 /* The following describes the implementation of the various locks and
1131 ** lock transitions in terms of the POSIX advisory shared and exclusive
1132 ** lock primitives (called read-locks and write-locks below, to avoid
1133 ** confusion with SQLite lock names). The algorithms are complicated
1134 ** slightly in order to be compatible with windows systems simultaneously
1135 ** accessing the same database file, in case that is ever required.
1136 **
1137 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
1138 ** byte', each single bytes at well known offsets, and the 'shared byte
1139 ** range', a range of 510 bytes at a well known offset.
1140 **
1141 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
1142 ** byte'. If this is successful, a random byte from the 'shared byte
1143 ** range' is read-locked and the lock on the 'pending byte' released.
1144 **
danielk197790ba3bd2004-06-25 08:32:25 +00001145 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
1146 ** A RESERVED lock is implemented by grabbing a write-lock on the
1147 ** 'reserved byte'.
danielk1977f42f25c2004-06-25 07:21:28 +00001148 **
1149 ** A process may only obtain a PENDING lock after it has obtained a
danielk197790ba3bd2004-06-25 08:32:25 +00001150 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
1151 ** on the 'pending byte'. This ensures that no new SHARED locks can be
1152 ** obtained, but existing SHARED locks are allowed to persist. A process
1153 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
1154 ** This property is used by the algorithm for rolling back a journal file
1155 ** after a crash.
danielk1977f42f25c2004-06-25 07:21:28 +00001156 **
danielk197790ba3bd2004-06-25 08:32:25 +00001157 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
1158 ** implemented by obtaining a write-lock on the entire 'shared byte
1159 ** range'. Since all other locks require a read-lock on one of the bytes
1160 ** within this range, this ensures that no other locks are held on the
1161 ** database.
danielk1977f42f25c2004-06-25 07:21:28 +00001162 **
1163 ** The reason a single byte cannot be used instead of the 'shared byte
1164 ** range' is that some versions of windows do not support read-locks. By
1165 ** locking a random byte from a range, concurrent SHARED locks may exist
1166 ** even if the locking primitive used is always a write-lock.
1167 */
danielk19779a1d0ab2004-06-01 14:09:28 +00001168 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001169 unixFile *pFile = (unixFile*)id;
1170 struct lockInfo *pLock = pFile->pLock;
danielk19779a1d0ab2004-06-01 14:09:28 +00001171 struct flock lock;
1172 int s;
1173
drh054889e2005-11-30 03:20:31 +00001174 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001175 OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001176 locktypeName(locktype), locktypeName(pFile->locktype),
1177 locktypeName(pLock->locktype), pLock->cnt , getpid());
danielk19779a1d0ab2004-06-01 14:09:28 +00001178
1179 /* If there is already a lock of this type or more restrictive on the
danielk1977ad94b582007-08-20 06:44:22 +00001180 ** unixFile, do nothing. Don't use the end_lock: exit path, as
danielk1977b4b47412007-08-17 15:53:36 +00001181 ** enterMutex() hasn't been called yet.
danielk19779a1d0ab2004-06-01 14:09:28 +00001182 */
drh054889e2005-11-30 03:20:31 +00001183 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00001184 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001185 locktypeName(locktype));
danielk19779a1d0ab2004-06-01 14:09:28 +00001186 return SQLITE_OK;
1187 }
1188
drhb3e04342004-06-08 00:47:47 +00001189 /* Make sure the locking sequence is correct
drh2ac3ee92004-06-07 16:27:46 +00001190 */
drh054889e2005-11-30 03:20:31 +00001191 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
drhb3e04342004-06-08 00:47:47 +00001192 assert( locktype!=PENDING_LOCK );
drh054889e2005-11-30 03:20:31 +00001193 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
drh2ac3ee92004-06-07 16:27:46 +00001194
drh054889e2005-11-30 03:20:31 +00001195 /* This mutex is needed because pFile->pLock is shared across threads
drhb3e04342004-06-08 00:47:47 +00001196 */
danielk1977b4b47412007-08-17 15:53:36 +00001197 enterMutex();
danielk19779a1d0ab2004-06-01 14:09:28 +00001198
drh029b44b2006-01-15 00:13:15 +00001199 /* Make sure the current thread owns the pFile.
1200 */
1201 rc = transferOwnership(pFile);
1202 if( rc!=SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00001203 leaveMutex();
drh029b44b2006-01-15 00:13:15 +00001204 return rc;
1205 }
drh64b1bea2006-01-15 02:30:57 +00001206 pLock = pFile->pLock;
drh029b44b2006-01-15 00:13:15 +00001207
danielk1977ad94b582007-08-20 06:44:22 +00001208 /* If some thread using this PID has a lock via a different unixFile*
danielk19779a1d0ab2004-06-01 14:09:28 +00001209 ** handle that precludes the requested lock, return BUSY.
1210 */
drh054889e2005-11-30 03:20:31 +00001211 if( (pFile->locktype!=pLock->locktype &&
drh2ac3ee92004-06-07 16:27:46 +00001212 (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
danielk19779a1d0ab2004-06-01 14:09:28 +00001213 ){
1214 rc = SQLITE_BUSY;
1215 goto end_lock;
1216 }
1217
1218 /* If a SHARED lock is requested, and some thread using this PID already
1219 ** has a SHARED or RESERVED lock, then increment reference counts and
1220 ** return SQLITE_OK.
1221 */
1222 if( locktype==SHARED_LOCK &&
1223 (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
1224 assert( locktype==SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001225 assert( pFile->locktype==0 );
danielk1977ecb2a962004-06-02 06:30:16 +00001226 assert( pLock->cnt>0 );
drh054889e2005-11-30 03:20:31 +00001227 pFile->locktype = SHARED_LOCK;
danielk19779a1d0ab2004-06-01 14:09:28 +00001228 pLock->cnt++;
drh054889e2005-11-30 03:20:31 +00001229 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001230 goto end_lock;
1231 }
1232
danielk197713adf8a2004-06-03 16:08:41 +00001233 lock.l_len = 1L;
drh2b4b5962005-06-15 17:47:55 +00001234
danielk19779a1d0ab2004-06-01 14:09:28 +00001235 lock.l_whence = SEEK_SET;
1236
drh3cde3bb2004-06-12 02:17:14 +00001237 /* A PENDING lock is needed before acquiring a SHARED lock and before
1238 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1239 ** be released.
danielk19779a1d0ab2004-06-01 14:09:28 +00001240 */
drh3cde3bb2004-06-12 02:17:14 +00001241 if( locktype==SHARED_LOCK
drh054889e2005-11-30 03:20:31 +00001242 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh3cde3bb2004-06-12 02:17:14 +00001243 ){
danielk1977489468c2004-06-28 08:25:47 +00001244 lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
drh2ac3ee92004-06-07 16:27:46 +00001245 lock.l_start = PENDING_BYTE;
drh054889e2005-11-30 03:20:31 +00001246 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001247 if( s==(-1) ){
danielk19779a1d0ab2004-06-01 14:09:28 +00001248 rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
1249 goto end_lock;
1250 }
drh3cde3bb2004-06-12 02:17:14 +00001251 }
1252
1253
1254 /* If control gets to this point, then actually go ahead and make
1255 ** operating system calls for the specified lock.
1256 */
1257 if( locktype==SHARED_LOCK ){
1258 assert( pLock->cnt==0 );
1259 assert( pLock->locktype==0 );
danielk19779a1d0ab2004-06-01 14:09:28 +00001260
drh2ac3ee92004-06-07 16:27:46 +00001261 /* Now get the read-lock */
1262 lock.l_start = SHARED_FIRST;
1263 lock.l_len = SHARED_SIZE;
drh054889e2005-11-30 03:20:31 +00001264 s = fcntl(pFile->h, F_SETLK, &lock);
drh2ac3ee92004-06-07 16:27:46 +00001265
1266 /* Drop the temporary PENDING lock */
1267 lock.l_start = PENDING_BYTE;
1268 lock.l_len = 1L;
danielk19779a1d0ab2004-06-01 14:09:28 +00001269 lock.l_type = F_UNLCK;
drh054889e2005-11-30 03:20:31 +00001270 if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
drh4ac285a2006-09-15 07:28:50 +00001271 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
drh2b4b5962005-06-15 17:47:55 +00001272 goto end_lock;
1273 }
drhe2396a12007-03-29 20:19:58 +00001274 if( s==(-1) ){
drhbbd42a62004-05-22 17:41:58 +00001275 rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
1276 }else{
drh054889e2005-11-30 03:20:31 +00001277 pFile->locktype = SHARED_LOCK;
1278 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001279 pLock->cnt = 1;
drhbbd42a62004-05-22 17:41:58 +00001280 }
drh3cde3bb2004-06-12 02:17:14 +00001281 }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
1282 /* We are trying for an exclusive lock but another thread in this
1283 ** same process is still holding a shared lock. */
1284 rc = SQLITE_BUSY;
drhbbd42a62004-05-22 17:41:58 +00001285 }else{
drh3cde3bb2004-06-12 02:17:14 +00001286 /* The request was for a RESERVED or EXCLUSIVE lock. It is
danielk19779a1d0ab2004-06-01 14:09:28 +00001287 ** assumed that there is a SHARED or greater lock on the file
1288 ** already.
1289 */
drh054889e2005-11-30 03:20:31 +00001290 assert( 0!=pFile->locktype );
danielk19779a1d0ab2004-06-01 14:09:28 +00001291 lock.l_type = F_WRLCK;
1292 switch( locktype ){
1293 case RESERVED_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001294 lock.l_start = RESERVED_BYTE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001295 break;
danielk19779a1d0ab2004-06-01 14:09:28 +00001296 case EXCLUSIVE_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001297 lock.l_start = SHARED_FIRST;
1298 lock.l_len = SHARED_SIZE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001299 break;
1300 default:
1301 assert(0);
1302 }
drh054889e2005-11-30 03:20:31 +00001303 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001304 if( s==(-1) ){
danielk19779a1d0ab2004-06-01 14:09:28 +00001305 rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
1306 }
drhbbd42a62004-05-22 17:41:58 +00001307 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001308
danielk1977ecb2a962004-06-02 06:30:16 +00001309 if( rc==SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001310 pFile->locktype = locktype;
danielk1977ecb2a962004-06-02 06:30:16 +00001311 pLock->locktype = locktype;
drh3cde3bb2004-06-12 02:17:14 +00001312 }else if( locktype==EXCLUSIVE_LOCK ){
drh054889e2005-11-30 03:20:31 +00001313 pFile->locktype = PENDING_LOCK;
drh3cde3bb2004-06-12 02:17:14 +00001314 pLock->locktype = PENDING_LOCK;
danielk1977ecb2a962004-06-02 06:30:16 +00001315 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001316
1317end_lock:
danielk1977b4b47412007-08-17 15:53:36 +00001318 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001319 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
danielk19772b444852004-06-29 07:45:33 +00001320 rc==SQLITE_OK ? "ok" : "failed");
drhbbd42a62004-05-22 17:41:58 +00001321 return rc;
1322}
1323
1324/*
drh054889e2005-11-30 03:20:31 +00001325** Lower the locking level on file descriptor pFile to locktype. locktype
drha6abd042004-06-09 17:37:22 +00001326** must be either NO_LOCK or SHARED_LOCK.
1327**
1328** If the locking level of the file descriptor is already at or below
1329** the requested locking level, this routine is a no-op.
drhbbd42a62004-05-22 17:41:58 +00001330*/
danielk197762079062007-08-15 17:08:46 +00001331static int unixUnlock(sqlite3_file *id, int locktype){
drha6abd042004-06-09 17:37:22 +00001332 struct lockInfo *pLock;
1333 struct flock lock;
drh9c105bb2004-10-02 20:38:28 +00001334 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001335 unixFile *pFile = (unixFile*)id;
drha6abd042004-06-09 17:37:22 +00001336
drh054889e2005-11-30 03:20:31 +00001337 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001338 OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
drh054889e2005-11-30 03:20:31 +00001339 pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
drha6abd042004-06-09 17:37:22 +00001340
1341 assert( locktype<=SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001342 if( pFile->locktype<=locktype ){
drha6abd042004-06-09 17:37:22 +00001343 return SQLITE_OK;
1344 }
drhf1a221e2006-01-15 17:27:17 +00001345 if( CHECK_THREADID(pFile) ){
1346 return SQLITE_MISUSE;
1347 }
danielk1977b4b47412007-08-17 15:53:36 +00001348 enterMutex();
drh054889e2005-11-30 03:20:31 +00001349 pLock = pFile->pLock;
drha6abd042004-06-09 17:37:22 +00001350 assert( pLock->cnt!=0 );
drh054889e2005-11-30 03:20:31 +00001351 if( pFile->locktype>SHARED_LOCK ){
1352 assert( pLock->locktype==pFile->locktype );
drh9c105bb2004-10-02 20:38:28 +00001353 if( locktype==SHARED_LOCK ){
1354 lock.l_type = F_RDLCK;
1355 lock.l_whence = SEEK_SET;
1356 lock.l_start = SHARED_FIRST;
1357 lock.l_len = SHARED_SIZE;
drhe2396a12007-03-29 20:19:58 +00001358 if( fcntl(pFile->h, F_SETLK, &lock)==(-1) ){
drh9c105bb2004-10-02 20:38:28 +00001359 /* This should never happen */
drh4ac285a2006-09-15 07:28:50 +00001360 rc = SQLITE_IOERR_RDLOCK;
drh9c105bb2004-10-02 20:38:28 +00001361 }
1362 }
drhbbd42a62004-05-22 17:41:58 +00001363 lock.l_type = F_UNLCK;
1364 lock.l_whence = SEEK_SET;
drha6abd042004-06-09 17:37:22 +00001365 lock.l_start = PENDING_BYTE;
1366 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
drhe2396a12007-03-29 20:19:58 +00001367 if( fcntl(pFile->h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001368 pLock->locktype = SHARED_LOCK;
1369 }else{
drh4ac285a2006-09-15 07:28:50 +00001370 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
drh2b4b5962005-06-15 17:47:55 +00001371 }
drhbbd42a62004-05-22 17:41:58 +00001372 }
drha6abd042004-06-09 17:37:22 +00001373 if( locktype==NO_LOCK ){
1374 struct openCnt *pOpen;
danielk1977ecb2a962004-06-02 06:30:16 +00001375
drha6abd042004-06-09 17:37:22 +00001376 /* Decrement the shared lock counter. Release the lock using an
1377 ** OS call only when all threads in this same process have released
1378 ** the lock.
1379 */
1380 pLock->cnt--;
1381 if( pLock->cnt==0 ){
1382 lock.l_type = F_UNLCK;
1383 lock.l_whence = SEEK_SET;
1384 lock.l_start = lock.l_len = 0L;
drhe2396a12007-03-29 20:19:58 +00001385 if( fcntl(pFile->h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001386 pLock->locktype = NO_LOCK;
1387 }else{
drh4ac285a2006-09-15 07:28:50 +00001388 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
drh2b4b5962005-06-15 17:47:55 +00001389 }
drha6abd042004-06-09 17:37:22 +00001390 }
1391
drhbbd42a62004-05-22 17:41:58 +00001392 /* Decrement the count of locks against this same file. When the
1393 ** count reaches zero, close any other file descriptors whose close
1394 ** was deferred because of outstanding locks.
1395 */
drh054889e2005-11-30 03:20:31 +00001396 pOpen = pFile->pOpen;
drhbbd42a62004-05-22 17:41:58 +00001397 pOpen->nLock--;
1398 assert( pOpen->nLock>=0 );
1399 if( pOpen->nLock==0 && pOpen->nPending>0 ){
1400 int i;
1401 for(i=0; i<pOpen->nPending; i++){
1402 close(pOpen->aPending[i]);
1403 }
drh64b1bea2006-01-15 02:30:57 +00001404 free(pOpen->aPending);
drhbbd42a62004-05-22 17:41:58 +00001405 pOpen->nPending = 0;
1406 pOpen->aPending = 0;
1407 }
1408 }
danielk1977b4b47412007-08-17 15:53:36 +00001409 leaveMutex();
drh054889e2005-11-30 03:20:31 +00001410 pFile->locktype = locktype;
drh9c105bb2004-10-02 20:38:28 +00001411 return rc;
drhbbd42a62004-05-22 17:41:58 +00001412}
1413
1414/*
danielk1977e3026632004-06-22 11:29:02 +00001415** Close a file.
1416*/
danielk197762079062007-08-15 17:08:46 +00001417static int unixClose(sqlite3_file *id){
1418 unixFile *pFile = (unixFile *)id;
1419 if( !pFile ) return SQLITE_OK;
1420 unixUnlock(id, NO_LOCK);
1421 if( pFile->dirfd>=0 ) close(pFile->dirfd);
1422 pFile->dirfd = -1;
danielk1977b4b47412007-08-17 15:53:36 +00001423 enterMutex();
danielk1977441b09a2006-01-05 13:48:29 +00001424
danielk197762079062007-08-15 17:08:46 +00001425 if( pFile->pOpen->nLock ){
danielk1977e3026632004-06-22 11:29:02 +00001426 /* If there are outstanding locks, do not actually close the file just
1427 ** yet because that would clear those locks. Instead, add the file
1428 ** descriptor to pOpen->aPending. It will be automatically closed when
1429 ** the last lock is cleared.
1430 */
1431 int *aNew;
danielk197762079062007-08-15 17:08:46 +00001432 struct openCnt *pOpen = pFile->pOpen;
drh64b1bea2006-01-15 02:30:57 +00001433 aNew = realloc( pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
danielk1977e3026632004-06-22 11:29:02 +00001434 if( aNew==0 ){
1435 /* If a malloc fails, just leak the file descriptor */
1436 }else{
1437 pOpen->aPending = aNew;
danielk197762079062007-08-15 17:08:46 +00001438 pOpen->aPending[pOpen->nPending] = pFile->h;
drhad81e872005-08-21 21:45:01 +00001439 pOpen->nPending++;
danielk1977e3026632004-06-22 11:29:02 +00001440 }
1441 }else{
1442 /* There are no outstanding locks so we can close the file immediately */
danielk197762079062007-08-15 17:08:46 +00001443 close(pFile->h);
danielk1977e3026632004-06-22 11:29:02 +00001444 }
danielk197762079062007-08-15 17:08:46 +00001445 releaseLockInfo(pFile->pLock);
1446 releaseOpenCnt(pFile->pOpen);
danielk1977441b09a2006-01-05 13:48:29 +00001447
danielk1977b4b47412007-08-17 15:53:36 +00001448 leaveMutex();
danielk197762079062007-08-15 17:08:46 +00001449 pFile->isOpen = 0;
1450 OSTRACE2("CLOSE %-3d\n", pFile->h);
danielk1977e3026632004-06-22 11:29:02 +00001451 OpenCounter(-1);
danielk1977b4b47412007-08-17 15:53:36 +00001452 memset(pFile, 0, sizeof(unixFile));
drh02afc862006-01-20 18:10:57 +00001453 return SQLITE_OK;
danielk1977e3026632004-06-22 11:29:02 +00001454}
1455
drhbfe66312006-10-03 17:40:40 +00001456
1457#ifdef SQLITE_ENABLE_LOCKING_STYLE
1458#pragma mark AFP Support
1459
1460/*
1461 ** The afpLockingContext structure contains all afp lock specific state
1462 */
1463typedef struct afpLockingContext afpLockingContext;
1464struct afpLockingContext {
1465 unsigned long long sharedLockByte;
1466 char *filePath;
1467};
1468
1469struct ByteRangeLockPB2
1470{
1471 unsigned long long offset; /* offset to first byte to lock */
1472 unsigned long long length; /* nbr of bytes to lock */
1473 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
1474 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
1475 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
1476 int fd; /* file desc to assoc this lock with */
1477};
1478
drhfd131da2007-08-07 17:13:03 +00001479#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
drhbfe66312006-10-03 17:40:40 +00001480
danielk1977ad94b582007-08-20 06:44:22 +00001481/*
1482** Return 0 on success, 1 on failure. To match the behavior of the
1483** normal posix file locking (used in unixLock for example), we should
1484** provide 'richer' return codes - specifically to differentiate between
1485** 'file busy' and 'file system error' results.
1486*/
1487static int _AFPFSSetLock(
1488 const char *path,
1489 int fd,
1490 unsigned long long offset,
1491 unsigned long long length,
1492 int setLockFlag
1493){
drhfd131da2007-08-07 17:13:03 +00001494 struct ByteRangeLockPB2 pb;
drhbfe66312006-10-03 17:40:40 +00001495 int err;
1496
1497 pb.unLockFlag = setLockFlag ? 0 : 1;
1498 pb.startEndFlag = 0;
1499 pb.offset = offset;
1500 pb.length = length;
1501 pb.fd = fd;
drh4f0c5872007-03-26 22:05:01 +00001502 OSTRACE5("AFPLOCK setting lock %s for %d in range %llx:%llx\n",
drhbfe66312006-10-03 17:40:40 +00001503 (setLockFlag?"ON":"OFF"), fd, offset, length);
1504 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
1505 if ( err==-1 ) {
drh4f0c5872007-03-26 22:05:01 +00001506 OSTRACE4("AFPLOCK failed to fsctl() '%s' %d %s\n", path, errno,
drhbfe66312006-10-03 17:40:40 +00001507 strerror(errno));
drh3b62b2f2007-06-08 18:27:03 +00001508 return 1; /* error */
drhbfe66312006-10-03 17:40:40 +00001509 } else {
1510 return 0;
1511 }
1512}
1513
1514/*
1515 ** This routine checks if there is a RESERVED lock held on the specified
1516 ** file by this or any other process. If such a lock is held, return
1517 ** non-zero. If the file is unlocked or holds only SHARED locks, then
1518 ** return zero.
1519 */
danielk1977ad94b582007-08-20 06:44:22 +00001520static int afpUnixCheckReservedLock(sqlite3_file *id){
drhbfe66312006-10-03 17:40:40 +00001521 int r = 0;
1522 unixFile *pFile = (unixFile*)id;
1523
1524 assert( pFile );
1525 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
1526
1527 /* Check if a thread in this process holds such a lock */
1528 if( pFile->locktype>SHARED_LOCK ){
1529 r = 1;
1530 }
1531
1532 /* Otherwise see if some other process holds it.
1533 */
1534 if ( !r ) {
drh3b62b2f2007-06-08 18:27:03 +00001535 /* lock the byte */
drhbfe66312006-10-03 17:40:40 +00001536 int failed = _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1,1);
1537 if (failed) {
1538 /* if we failed to get the lock then someone else must have it */
1539 r = 1;
1540 } else {
1541 /* if we succeeded in taking the reserved lock, unlock it to restore
1542 ** the original state */
1543 _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1, 0);
1544 }
1545 }
drh4f0c5872007-03-26 22:05:01 +00001546 OSTRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
drhbfe66312006-10-03 17:40:40 +00001547
1548 return r;
1549}
1550
1551/* AFP-style locking following the behavior of unixLock, see the unixLock
1552** function comments for details of lock management. */
danielk1977ad94b582007-08-20 06:44:22 +00001553static int afpUnixLock(sqlite3_file *id, int locktype)
drhbfe66312006-10-03 17:40:40 +00001554{
1555 int rc = SQLITE_OK;
1556 unixFile *pFile = (unixFile*)id;
1557 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
1558 int gotPendingLock = 0;
1559
1560 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001561 OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00001562 locktypeName(locktype), locktypeName(pFile->locktype), getpid());
1563 /* If there is already a lock of this type or more restrictive on the
danielk1977ad94b582007-08-20 06:44:22 +00001564 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
danielk1977b4b47412007-08-17 15:53:36 +00001565 ** enterMutex() hasn't been called yet.
drhbfe66312006-10-03 17:40:40 +00001566 */
1567 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00001568 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00001569 locktypeName(locktype));
1570 return SQLITE_OK;
1571 }
1572
1573 /* Make sure the locking sequence is correct
1574 */
1575 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
1576 assert( locktype!=PENDING_LOCK );
1577 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
1578
1579 /* This mutex is needed because pFile->pLock is shared across threads
1580 */
danielk1977b4b47412007-08-17 15:53:36 +00001581 enterMutex();
drhbfe66312006-10-03 17:40:40 +00001582
1583 /* Make sure the current thread owns the pFile.
1584 */
1585 rc = transferOwnership(pFile);
1586 if( rc!=SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00001587 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00001588 return rc;
1589 }
1590
1591 /* A PENDING lock is needed before acquiring a SHARED lock and before
1592 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1593 ** be released.
1594 */
1595 if( locktype==SHARED_LOCK
1596 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
1597 ){
1598 int failed = _AFPFSSetLock(context->filePath, pFile->h,
1599 PENDING_BYTE, 1, 1);
1600 if (failed) {
1601 rc = SQLITE_BUSY;
1602 goto afp_end_lock;
1603 }
1604 }
1605
1606 /* If control gets to this point, then actually go ahead and make
1607 ** operating system calls for the specified lock.
1608 */
1609 if( locktype==SHARED_LOCK ){
1610 int lk, failed;
1611 int tries = 0;
1612
1613 /* Now get the read-lock */
1614 /* note that the quality of the randomness doesn't matter that much */
1615 lk = random();
1616 context->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
1617 failed = _AFPFSSetLock(context->filePath, pFile->h,
1618 SHARED_FIRST+context->sharedLockByte, 1, 1);
1619
1620 /* Drop the temporary PENDING lock */
1621 if (_AFPFSSetLock(context->filePath, pFile->h, PENDING_BYTE, 1, 0)) {
1622 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1623 goto afp_end_lock;
1624 }
1625
1626 if( failed ){
1627 rc = SQLITE_BUSY;
1628 } else {
1629 pFile->locktype = SHARED_LOCK;
1630 }
1631 }else{
1632 /* The request was for a RESERVED or EXCLUSIVE lock. It is
1633 ** assumed that there is a SHARED or greater lock on the file
1634 ** already.
1635 */
1636 int failed = 0;
1637 assert( 0!=pFile->locktype );
1638 if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
1639 /* Acquire a RESERVED lock */
1640 failed = _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1,1);
1641 }
1642 if (!failed && locktype == EXCLUSIVE_LOCK) {
1643 /* Acquire an EXCLUSIVE lock */
1644
1645 /* Remove the shared lock before trying the range. we'll need to
1646 ** reestablish the shared lock if we can't get the afpUnixUnlock
1647 */
1648 if (!_AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST +
1649 context->sharedLockByte, 1, 0)) {
1650 /* now attemmpt to get the exclusive lock range */
1651 failed = _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST,
1652 SHARED_SIZE, 1);
1653 if (failed && _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST +
1654 context->sharedLockByte, 1, 1)) {
1655 rc = SQLITE_IOERR_RDLOCK; /* this should never happen */
1656 }
1657 } else {
1658 /* */
1659 rc = SQLITE_IOERR_UNLOCK; /* this should never happen */
1660 }
1661 }
1662 if( failed && rc == SQLITE_OK){
1663 rc = SQLITE_BUSY;
1664 }
1665 }
1666
1667 if( rc==SQLITE_OK ){
1668 pFile->locktype = locktype;
1669 }else if( locktype==EXCLUSIVE_LOCK ){
1670 pFile->locktype = PENDING_LOCK;
1671 }
1672
1673afp_end_lock:
danielk1977b4b47412007-08-17 15:53:36 +00001674 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001675 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
drhbfe66312006-10-03 17:40:40 +00001676 rc==SQLITE_OK ? "ok" : "failed");
1677 return rc;
1678}
1679
1680/*
1681 ** Lower the locking level on file descriptor pFile to locktype. locktype
1682 ** must be either NO_LOCK or SHARED_LOCK.
1683 **
1684 ** If the locking level of the file descriptor is already at or below
1685 ** the requested locking level, this routine is a no-op.
1686 */
danielk1977ad94b582007-08-20 06:44:22 +00001687static int afpUnixUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001688 struct flock lock;
1689 int rc = SQLITE_OK;
1690 unixFile *pFile = (unixFile*)id;
1691 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
1692
1693 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001694 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
drhbfe66312006-10-03 17:40:40 +00001695 pFile->locktype, getpid());
1696
1697 assert( locktype<=SHARED_LOCK );
1698 if( pFile->locktype<=locktype ){
1699 return SQLITE_OK;
1700 }
1701 if( CHECK_THREADID(pFile) ){
1702 return SQLITE_MISUSE;
1703 }
danielk1977b4b47412007-08-17 15:53:36 +00001704 enterMutex();
drhbfe66312006-10-03 17:40:40 +00001705 if( pFile->locktype>SHARED_LOCK ){
1706 if( locktype==SHARED_LOCK ){
1707 int failed = 0;
1708
1709 /* unlock the exclusive range - then re-establish the shared lock */
1710 if (pFile->locktype==EXCLUSIVE_LOCK) {
1711 failed = _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST,
1712 SHARED_SIZE, 0);
1713 if (!failed) {
1714 /* successfully removed the exclusive lock */
1715 if (_AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST+
1716 context->sharedLockByte, 1, 1)) {
1717 /* failed to re-establish our shared lock */
1718 rc = SQLITE_IOERR_RDLOCK; /* This should never happen */
1719 }
1720 } else {
1721 /* This should never happen - failed to unlock the exclusive range */
1722 rc = SQLITE_IOERR_UNLOCK;
1723 }
1724 }
1725 }
1726 if (rc == SQLITE_OK && pFile->locktype>=PENDING_LOCK) {
1727 if (_AFPFSSetLock(context->filePath, pFile->h, PENDING_BYTE, 1, 0)){
1728 /* failed to release the pending lock */
1729 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1730 }
1731 }
1732 if (rc == SQLITE_OK && pFile->locktype>=RESERVED_LOCK) {
1733 if (_AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1, 0)) {
1734 /* failed to release the reserved lock */
1735 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1736 }
1737 }
1738 }
1739 if( locktype==NO_LOCK ){
1740 int failed = _AFPFSSetLock(context->filePath, pFile->h,
1741 SHARED_FIRST + context->sharedLockByte, 1, 0);
1742 if (failed) {
1743 rc = SQLITE_IOERR_UNLOCK; /* This should never happen */
1744 }
1745 }
1746 if (rc == SQLITE_OK)
1747 pFile->locktype = locktype;
danielk1977b4b47412007-08-17 15:53:36 +00001748 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00001749 return rc;
1750}
1751
1752/*
1753 ** Close a file & cleanup AFP specific locking context
1754 */
danielk1977ad94b582007-08-20 06:44:22 +00001755static int afpUnixClose(sqlite3_file *id) {
1756 unixFile *pFile = (unixFile*)pId;
1757
1758 if( !pFile ) return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00001759 afpUnixUnlock(*pId, NO_LOCK);
1760 /* free the AFP locking structure */
danielk1977ad94b582007-08-20 06:44:22 +00001761 if (pFile->lockingContext != NULL) {
1762 if (((afpLockingContext *)pFile->lockingContext)->filePath != NULL)
1763 sqlite3_free(((afpLockingContext*)pFile->lockingContext)->filePath);
1764 sqlite3_free(pFile->lockingContext);
drhbfe66312006-10-03 17:40:40 +00001765 }
danielk1977ad94b582007-08-20 06:44:22 +00001766
1767 if( pFile->dirfd>=0 ) close(pFile->dirfd);
1768 pFile->dirfd = -1;
1769 close(pFile->h);
1770 pFile->isOpen = 0;
1771 OSTRACE2("CLOSE %-3d\n", pFile->h);
drhbfe66312006-10-03 17:40:40 +00001772 OpenCounter(-1);
drhbfe66312006-10-03 17:40:40 +00001773 return SQLITE_OK;
1774}
1775
1776
1777#pragma mark flock() style locking
1778
1779/*
1780 ** The flockLockingContext is not used
1781 */
1782typedef void flockLockingContext;
1783
danielk1977ad94b582007-08-20 06:44:22 +00001784static int flockUnixCheckReservedLock(sqlite3_file *id) {
drhbfe66312006-10-03 17:40:40 +00001785 unixFile *pFile = (unixFile*)id;
1786
1787 if (pFile->locktype == RESERVED_LOCK) {
drh3b62b2f2007-06-08 18:27:03 +00001788 return 1; /* already have a reserved lock */
drhbfe66312006-10-03 17:40:40 +00001789 } else {
drh3b62b2f2007-06-08 18:27:03 +00001790 /* attempt to get the lock */
drhbfe66312006-10-03 17:40:40 +00001791 int rc = flock(pFile->h, LOCK_EX | LOCK_NB);
1792 if (!rc) {
drh3b62b2f2007-06-08 18:27:03 +00001793 /* got the lock, unlock it */
drhbfe66312006-10-03 17:40:40 +00001794 flock(pFile->h, LOCK_UN);
drh3b62b2f2007-06-08 18:27:03 +00001795 return 0; /* no one has it reserved */
drhbfe66312006-10-03 17:40:40 +00001796 }
drh3b62b2f2007-06-08 18:27:03 +00001797 return 1; /* someone else might have it reserved */
drhbfe66312006-10-03 17:40:40 +00001798 }
1799}
1800
danielk1977ad94b582007-08-20 06:44:22 +00001801static int flockUnixLock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001802 unixFile *pFile = (unixFile*)id;
1803
drh3b62b2f2007-06-08 18:27:03 +00001804 /* if we already have a lock, it is exclusive.
1805 ** Just adjust level and punt on outta here. */
drhbfe66312006-10-03 17:40:40 +00001806 if (pFile->locktype > NO_LOCK) {
1807 pFile->locktype = locktype;
1808 return SQLITE_OK;
1809 }
1810
drh3b62b2f2007-06-08 18:27:03 +00001811 /* grab an exclusive lock */
drhbfe66312006-10-03 17:40:40 +00001812 int rc = flock(pFile->h, LOCK_EX | LOCK_NB);
1813 if (rc) {
drh3b62b2f2007-06-08 18:27:03 +00001814 /* didn't get, must be busy */
drhbfe66312006-10-03 17:40:40 +00001815 return SQLITE_BUSY;
1816 } else {
drh3b62b2f2007-06-08 18:27:03 +00001817 /* got it, set the type and return ok */
drhbfe66312006-10-03 17:40:40 +00001818 pFile->locktype = locktype;
1819 return SQLITE_OK;
1820 }
1821}
1822
danielk1977ad94b582007-08-20 06:44:22 +00001823static int flockUnixUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001824 unixFile *pFile = (unixFile*)id;
1825
1826 assert( locktype<=SHARED_LOCK );
1827
drh3b62b2f2007-06-08 18:27:03 +00001828 /* no-op if possible */
drhbfe66312006-10-03 17:40:40 +00001829 if( pFile->locktype==locktype ){
1830 return SQLITE_OK;
1831 }
1832
drh3b62b2f2007-06-08 18:27:03 +00001833 /* shared can just be set because we always have an exclusive */
drhbfe66312006-10-03 17:40:40 +00001834 if (locktype==SHARED_LOCK) {
1835 pFile->locktype = locktype;
1836 return SQLITE_OK;
1837 }
1838
drh3b62b2f2007-06-08 18:27:03 +00001839 /* no, really, unlock. */
drhbfe66312006-10-03 17:40:40 +00001840 int rc = flock(pFile->h, LOCK_UN);
1841 if (rc)
1842 return SQLITE_IOERR_UNLOCK;
1843 else {
1844 pFile->locktype = NO_LOCK;
1845 return SQLITE_OK;
1846 }
1847}
1848
1849/*
1850 ** Close a file.
1851 */
danielk1977ad94b582007-08-20 06:44:22 +00001852static int flockUnixClose(sqlite3_file *pId) {
1853 unixFile *pFile = (unixFile*)*pId;
drhbfe66312006-10-03 17:40:40 +00001854
danielk1977ad94b582007-08-20 06:44:22 +00001855 if( !pFile ) return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00001856 flockUnixUnlock(*pId, NO_LOCK);
1857
danielk1977ad94b582007-08-20 06:44:22 +00001858 if( pFile->dirfd>=0 ) close(pFile->dirfd);
1859 pFile->dirfd = -1;
danielk1977b4b47412007-08-17 15:53:36 +00001860 enterMutex();
drhbfe66312006-10-03 17:40:40 +00001861
danielk1977ad94b582007-08-20 06:44:22 +00001862 close(pFile->h);
danielk1977b4b47412007-08-17 15:53:36 +00001863 leaveMutex();
danielk1977ad94b582007-08-20 06:44:22 +00001864 pFile->isOpen = 0;
1865 OSTRACE2("CLOSE %-3d\n", pFile->h);
drhbfe66312006-10-03 17:40:40 +00001866 OpenCounter(-1);
drhbfe66312006-10-03 17:40:40 +00001867 return SQLITE_OK;
1868}
1869
1870#pragma mark Old-School .lock file based locking
1871
1872/*
1873 ** The dotlockLockingContext structure contains all dotlock (.lock) lock
1874 ** specific state
1875 */
1876typedef struct dotlockLockingContext dotlockLockingContext;
1877struct dotlockLockingContext {
1878 char *lockPath;
1879};
1880
1881
danielk1977ad94b582007-08-20 06:44:22 +00001882static int dotlockUnixCheckReservedLock(sqlite3_file *id) {
drhbfe66312006-10-03 17:40:40 +00001883 unixFile *pFile = (unixFile*)id;
1884 dotlockLockingContext *context =
1885 (dotlockLockingContext *) pFile->lockingContext;
1886
1887 if (pFile->locktype == RESERVED_LOCK) {
drh3b62b2f2007-06-08 18:27:03 +00001888 return 1; /* already have a reserved lock */
drhbfe66312006-10-03 17:40:40 +00001889 } else {
1890 struct stat statBuf;
1891 if (lstat(context->lockPath,&statBuf) == 0)
drh3b62b2f2007-06-08 18:27:03 +00001892 /* file exists, someone else has the lock */
drhbfe66312006-10-03 17:40:40 +00001893 return 1;
1894 else
drh3b62b2f2007-06-08 18:27:03 +00001895 /* file does not exist, we could have it if we want it */
drhbfe66312006-10-03 17:40:40 +00001896 return 0;
1897 }
1898}
1899
danielk1977ad94b582007-08-20 06:44:22 +00001900static int dotlockUnixLock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001901 unixFile *pFile = (unixFile*)id;
1902 dotlockLockingContext *context =
1903 (dotlockLockingContext *) pFile->lockingContext;
1904
drh3b62b2f2007-06-08 18:27:03 +00001905 /* if we already have a lock, it is exclusive.
1906 ** Just adjust level and punt on outta here. */
drhbfe66312006-10-03 17:40:40 +00001907 if (pFile->locktype > NO_LOCK) {
1908 pFile->locktype = locktype;
1909
1910 /* Always update the timestamp on the old file */
1911 utimes(context->lockPath,NULL);
1912 return SQLITE_OK;
1913 }
1914
drh3b62b2f2007-06-08 18:27:03 +00001915 /* check to see if lock file already exists */
drhbfe66312006-10-03 17:40:40 +00001916 struct stat statBuf;
1917 if (lstat(context->lockPath,&statBuf) == 0){
drh3b62b2f2007-06-08 18:27:03 +00001918 return SQLITE_BUSY; /* it does, busy */
drhbfe66312006-10-03 17:40:40 +00001919 }
1920
drh3b62b2f2007-06-08 18:27:03 +00001921 /* grab an exclusive lock */
drhbfe66312006-10-03 17:40:40 +00001922 int fd = open(context->lockPath,O_RDONLY|O_CREAT|O_EXCL,0600);
1923 if (fd < 0) {
drh3b62b2f2007-06-08 18:27:03 +00001924 /* failed to open/create the file, someone else may have stolen the lock */
drhbfe66312006-10-03 17:40:40 +00001925 return SQLITE_BUSY;
1926 }
1927 close(fd);
1928
drh3b62b2f2007-06-08 18:27:03 +00001929 /* got it, set the type and return ok */
drhbfe66312006-10-03 17:40:40 +00001930 pFile->locktype = locktype;
1931 return SQLITE_OK;
1932}
1933
danielk1977ad94b582007-08-20 06:44:22 +00001934static int dotlockUnixUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001935 unixFile *pFile = (unixFile*)id;
1936 dotlockLockingContext *context =
1937 (dotlockLockingContext *) pFile->lockingContext;
1938
1939 assert( locktype<=SHARED_LOCK );
1940
drh3b62b2f2007-06-08 18:27:03 +00001941 /* no-op if possible */
drhbfe66312006-10-03 17:40:40 +00001942 if( pFile->locktype==locktype ){
1943 return SQLITE_OK;
1944 }
1945
drh3b62b2f2007-06-08 18:27:03 +00001946 /* shared can just be set because we always have an exclusive */
drhbfe66312006-10-03 17:40:40 +00001947 if (locktype==SHARED_LOCK) {
1948 pFile->locktype = locktype;
1949 return SQLITE_OK;
1950 }
1951
drh3b62b2f2007-06-08 18:27:03 +00001952 /* no, really, unlock. */
drhbfe66312006-10-03 17:40:40 +00001953 unlink(context->lockPath);
1954 pFile->locktype = NO_LOCK;
1955 return SQLITE_OK;
1956}
1957
1958/*
1959 ** Close a file.
1960 */
danielk1977ad94b582007-08-20 06:44:22 +00001961static int dotlockUnixClose(sqlite3_file *id) {
1962 unixFile *pFile = (unixFile*)id;
drhbfe66312006-10-03 17:40:40 +00001963
danielk1977ad94b582007-08-20 06:44:22 +00001964 if( !pFile ) return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00001965 dotlockUnixUnlock(*pId, NO_LOCK);
1966 /* free the dotlock locking structure */
danielk1977ad94b582007-08-20 06:44:22 +00001967 if (pFile->lockingContext != NULL) {
1968 if (((dotlockLockingContext *)pFile->lockingContext)->lockPath != NULL)
drh17435752007-08-16 04:30:38 +00001969 sqlite3_free( ( (dotlockLockingContext *)
danielk1977ad94b582007-08-20 06:44:22 +00001970 pFile->lockingContext)->lockPath);
1971 sqlite3_free(pFile->lockingContext);
drhbfe66312006-10-03 17:40:40 +00001972 }
1973
danielk1977ad94b582007-08-20 06:44:22 +00001974 if( pFile->dirfd>=0 ) close(pFile->dirfd);
1975 pFile->dirfd = -1;
danielk1977b4b47412007-08-17 15:53:36 +00001976 enterMutex();
drhbfe66312006-10-03 17:40:40 +00001977
danielk1977ad94b582007-08-20 06:44:22 +00001978 close(pFile->h);
drhbfe66312006-10-03 17:40:40 +00001979
danielk1977b4b47412007-08-17 15:53:36 +00001980 leaveMutex();
danielk1977ad94b582007-08-20 06:44:22 +00001981 pFile->isOpen = 0;
1982 OSTRACE2("CLOSE %-3d\n", pFile->h);
drhbfe66312006-10-03 17:40:40 +00001983 OpenCounter(-1);
drhbfe66312006-10-03 17:40:40 +00001984 return SQLITE_OK;
1985}
1986
1987
1988#pragma mark No locking
1989
1990/*
1991 ** The nolockLockingContext is void
1992 */
1993typedef void nolockLockingContext;
1994
danielk1977ad94b582007-08-20 06:44:22 +00001995static int nolockUnixCheckReservedLock(sqlite3_file *id) {
drhbfe66312006-10-03 17:40:40 +00001996 return 0;
1997}
1998
danielk1977ad94b582007-08-20 06:44:22 +00001999static int nolockUnixLock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002000 return SQLITE_OK;
2001}
2002
danielk1977ad94b582007-08-20 06:44:22 +00002003static int nolockUnixUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002004 return SQLITE_OK;
2005}
2006
2007/*
2008 ** Close a file.
2009 */
danielk1977ad94b582007-08-20 06:44:22 +00002010static int nolockUnixClose(sqlite3_file *id) {
2011 unixFile *pFile = (unixFile*)id;
drhbfe66312006-10-03 17:40:40 +00002012
danielk1977ad94b582007-08-20 06:44:22 +00002013 if( !pFile ) return SQLITE_OK;
2014 if( pFile->dirfd>=0 ) close(pFile->dirfd);
2015 pFile->dirfd = -1;
danielk1977b4b47412007-08-17 15:53:36 +00002016 enterMutex();
drhbfe66312006-10-03 17:40:40 +00002017
danielk1977ad94b582007-08-20 06:44:22 +00002018 close(pFile->h);
drhbfe66312006-10-03 17:40:40 +00002019
danielk1977b4b47412007-08-17 15:53:36 +00002020 leaveMutex();
danielk1977ad94b582007-08-20 06:44:22 +00002021 pFile->isOpen = 0;
2022 OSTRACE2("CLOSE %-3d\n", pFile->h);
drhbfe66312006-10-03 17:40:40 +00002023 OpenCounter(-1);
drhbfe66312006-10-03 17:40:40 +00002024 return SQLITE_OK;
2025}
2026
2027#endif /* SQLITE_ENABLE_LOCKING_STYLE */
2028
danielk1977ad94b582007-08-20 06:44:22 +00002029
danielk1977e3026632004-06-22 11:29:02 +00002030/*
danielk1977ad94b582007-08-20 06:44:22 +00002031** TODO: xBreakLock() for this vfs.
drh18839212005-11-26 03:43:23 +00002032*/
danielk1977ad94b582007-08-20 06:44:22 +00002033static int unixBreakLock(sqlite3_file *id){
2034 assert(!"TODO: unixBreakLock()");
2035 return 0;
drh9cbe6352005-11-29 03:13:21 +00002036}
2037
2038/*
danielk1977ad94b582007-08-20 06:44:22 +00002039** Return an integer that indices the type of lock currently held
2040** by this handle. (Used for testing and analysis only.)
drh9cbe6352005-11-29 03:13:21 +00002041*/
danielk1977ad94b582007-08-20 06:44:22 +00002042static int unixLockState(sqlite3_file *id){
2043 return ((unixFile*)id)->locktype;
drh9cbe6352005-11-29 03:13:21 +00002044}
2045
drh9c06c952005-11-26 00:25:00 +00002046/*
danielk1977a3d4c882007-03-23 10:08:38 +00002047** Return the sector size in bytes of the underlying block device for
2048** the specified file. This is almost always 512 bytes, but may be
2049** larger for some devices.
2050**
2051** SQLite code assumes this function cannot fail. It also assumes that
2052** if two files are created in the same file-system directory (i.e.
2053** a database and it's journal file) that the sector size will be the
2054** same for both.
2055*/
danielk197762079062007-08-15 17:08:46 +00002056static int unixSectorSize(sqlite3_file *id){
drh3ceeb752007-03-29 18:19:52 +00002057 return SQLITE_DEFAULT_SECTOR_SIZE;
danielk1977a3d4c882007-03-23 10:08:38 +00002058}
2059
danielk197790949c22007-08-17 16:50:38 +00002060/*
2061** Return the device characteristics for the file. This is always 0.
2062*/
danielk197762079062007-08-15 17:08:46 +00002063static int unixDeviceCharacteristics(sqlite3_file *id){
2064 return 0;
2065}
2066
danielk1977a3d4c882007-03-23 10:08:38 +00002067/*
danielk1977ad94b582007-08-20 06:44:22 +00002068** This vector defines all the methods that can operate on an sqlite3_file
drh054889e2005-11-30 03:20:31 +00002069** for unix.
drh9c06c952005-11-26 00:25:00 +00002070*/
danielk197762079062007-08-15 17:08:46 +00002071static const sqlite3_io_methods sqlite3UnixIoMethod = {
2072 1, /* iVersion */
drh9c06c952005-11-26 00:25:00 +00002073 unixClose,
2074 unixRead,
2075 unixWrite,
drh9c06c952005-11-26 00:25:00 +00002076 unixTruncate,
drh054889e2005-11-30 03:20:31 +00002077 unixSync,
drh054889e2005-11-30 03:20:31 +00002078 unixFileSize,
2079 unixLock,
2080 unixUnlock,
drh054889e2005-11-30 03:20:31 +00002081 unixCheckReservedLock,
danielk197762079062007-08-15 17:08:46 +00002082 unixBreakLock,
danielk197790949c22007-08-17 16:50:38 +00002083 unixLockState,
danielk1977a3d4c882007-03-23 10:08:38 +00002084 unixSectorSize,
danielk197762079062007-08-15 17:08:46 +00002085 unixDeviceCharacteristics
drh9c06c952005-11-26 00:25:00 +00002086};
2087
drhbfe66312006-10-03 17:40:40 +00002088#ifdef SQLITE_ENABLE_LOCKING_STYLE
drh054889e2005-11-30 03:20:31 +00002089/*
danielk1977ad94b582007-08-20 06:44:22 +00002090** This vector defines all the methods that can operate on an sqlite3_file
2091** for unix with AFP style file locking.
2092*/
2093static const sqlite3_io_methods sqlite3AFPLockingUnixIoMethod = {
2094 1, /* iVersion */
2095 unixClose,
drhbfe66312006-10-03 17:40:40 +00002096 unixRead,
2097 unixWrite,
drhbfe66312006-10-03 17:40:40 +00002098 unixTruncate,
2099 unixSync,
danielk1977ad94b582007-08-20 06:44:22 +00002100 unixFileSize,
2101 afpUnixLock,
2102 afpUnixUnlock,
2103 afpUnixCheckReservedLock,
2104 unixBreakLock,
2105 unixLockState,
2106 unixSectorSize,
2107 unixDeviceCharacteristics
2108};
2109
2110/*
2111** This vector defines all the methods that can operate on an sqlite3_file
2112** for unix with flock() style file locking.
2113*/
2114static const sqlite3_io_methods sqlite3FlockLockingUnixIoMethod = {
2115 1, /* iVersion */
2116 flockUnixClose,
2117 unixRead,
2118 unixWrite,
2119 unixTruncate,
2120 unixSync,
2121 unixFileSize,
2122 flockUnixLock,
2123 flockUnixUnlock,
2124 flockUnixCheckReservedLock,
2125 unixBreakLock,
2126 unixLockState,
2127 unixSectorSize,
2128 unixDeviceCharacteristics
2129};
2130
2131/*
2132** This vector defines all the methods that can operate on an sqlite3_file
2133** for unix with dotlock style file locking.
2134*/
2135static const sqlite3_io_methods sqlite3DotlockLockingUnixIoMethod = {
2136 1, /* iVersion */
2137 dotlockUnixClose,
2138 unixRead,
2139 unixWrite,
2140 unixTruncate,
2141 unixSync,
2142 unixFileSize,
2143 dotlockUnixLock,
2144 dotlockUnixUnlock,
2145 dotlockUnixCheckReservedLock,
2146 unixBreakLock,
2147 unixLockState,
2148 unixSectorSize,
2149 unixDeviceCharacteristics
2150};
2151
2152/*
2153** This vector defines all the methods that can operate on an sqlite3_file
2154** for unix with dotlock style file locking.
2155*/
2156static const sqlite3_io_methods sqlite3NolockLockingUnixIoMethod = {
2157 1, /* iVersion */
2158 nolockUnixClose,
2159 unixRead,
2160 unixWrite,
2161 unixTruncate,
2162 unixSync,
drhbfe66312006-10-03 17:40:40 +00002163 unixFileSize,
2164 nolockUnixLock,
2165 nolockUnixUnlock,
drhbfe66312006-10-03 17:40:40 +00002166 nolockUnixCheckReservedLock,
danielk1977ad94b582007-08-20 06:44:22 +00002167 unixBreakLock,
2168 unixLockState,
danielk1977a3d4c882007-03-23 10:08:38 +00002169 unixSectorSize,
danielk1977ad94b582007-08-20 06:44:22 +00002170 unixDeviceCharacteristics
drhbfe66312006-10-03 17:40:40 +00002171};
2172
2173#endif /* SQLITE_ENABLE_LOCKING_STYLE */
2174
2175/*
2176** Allocate memory for a new unixFile and initialize that unixFile.
2177** Write a pointer to the new unixFile into *pId.
2178** If we run out of memory, close the file and return an error.
drh054889e2005-11-30 03:20:31 +00002179*/
drhbfe66312006-10-03 17:40:40 +00002180#ifdef SQLITE_ENABLE_LOCKING_STYLE
2181/*
danielk1977ad94b582007-08-20 06:44:22 +00002182** When locking extensions are enabled, the filepath and locking style
2183** are needed to determine the unixFile pMethod to use for locking operations.
2184** The locking-style specific lockingContext data structure is created
2185** and assigned here also.
2186*/
2187static int fillInUnixFile(
drhbfe66312006-10-03 17:40:40 +00002188 int h, /* Open file descriptor of file being opened */
danielk1977ad94b582007-08-20 06:44:22 +00002189 int dirfd, /* Directory file descriptor */
2190 sqlite3_file *pId, /* Write completed initialization here */
drhbfe66312006-10-03 17:40:40 +00002191 const char *zFilename, /* Name of the file being opened */
drhbfe66312006-10-03 17:40:40 +00002192){
aswift108bc322006-10-11 17:19:46 +00002193 sqlite3LockingStyle lockingStyle;
danielk1977ad94b582007-08-20 06:44:22 +00002194 unixFile *pNew = (unixFile *)pId;
drhbfe66312006-10-03 17:40:40 +00002195 int rc;
2196
danielk1977ad94b582007-08-20 06:44:22 +00002197 memset(pNew, 0, sizeof(unixFile));
aswift448aa6f2006-11-11 01:31:58 +00002198 lockingStyle = sqlite3DetectLockingStyle(zFilename, h);
drhbfe66312006-10-03 17:40:40 +00002199 if ( lockingStyle == posixLockingStyle ) {
danielk1977b4b47412007-08-17 15:53:36 +00002200 enterMutex();
danielk1977ad94b582007-08-20 06:44:22 +00002201 rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
danielk1977b4b47412007-08-17 15:53:36 +00002202 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00002203 if( rc ){
2204 close(h);
2205 unlink(zFilename);
2206 return SQLITE_NOMEM;
2207 }
2208 } else {
drh3b62b2f2007-06-08 18:27:03 +00002209 /* pLock and pOpen are only used for posix advisory locking */
danielk1977ad94b582007-08-20 06:44:22 +00002210 pNew->pLock = NULL;
2211 pNew->pOpen = NULL;
drhbfe66312006-10-03 17:40:40 +00002212 }
danielk1977ad94b582007-08-20 06:44:22 +00002213 pNew->dirfd = -1;
2214 pNew->h = h;
2215 SET_THREADID(pNew);
drh17435752007-08-16 04:30:38 +00002216 pNew = sqlite3_malloc( sizeof(unixFile) );
drh054889e2005-11-30 03:20:31 +00002217 if( pNew==0 ){
drhbfe66312006-10-03 17:40:40 +00002218 close(h);
danielk1977b4b47412007-08-17 15:53:36 +00002219 enterMutex();
danielk1977ad94b582007-08-20 06:44:22 +00002220 releaseLockInfo(pNew->pLock);
2221 releaseOpenCnt(pNew->pOpen);
danielk1977b4b47412007-08-17 15:53:36 +00002222 leaveMutex();
drh054889e2005-11-30 03:20:31 +00002223 return SQLITE_NOMEM;
2224 }else{
aswift108bc322006-10-11 17:19:46 +00002225 switch(lockingStyle) {
drh5bb3eb92007-05-04 13:15:55 +00002226 case afpLockingStyle: {
drhbfe66312006-10-03 17:40:40 +00002227 /* afp locking uses the file path so it needs to be included in
2228 ** the afpLockingContext */
drh5bb3eb92007-05-04 13:15:55 +00002229 int nFilename;
drhbfe66312006-10-03 17:40:40 +00002230 pNew->pMethod = &sqlite3AFPLockingUnixIoMethod;
2231 pNew->lockingContext =
drh17435752007-08-16 04:30:38 +00002232 sqlite3_malloc(sizeof(afpLockingContext));
drh5bb3eb92007-05-04 13:15:55 +00002233 nFilename = strlen(zFilename)+1;
drhbfe66312006-10-03 17:40:40 +00002234 ((afpLockingContext *)pNew->lockingContext)->filePath =
drh17435752007-08-16 04:30:38 +00002235 sqlite3_malloc(nFilename);
drh5bb3eb92007-05-04 13:15:55 +00002236 memcpy(((afpLockingContext *)pNew->lockingContext)->filePath,
2237 zFilename, nFilename);
drhbfe66312006-10-03 17:40:40 +00002238 srandomdev();
2239 break;
drh5bb3eb92007-05-04 13:15:55 +00002240 }
drhbfe66312006-10-03 17:40:40 +00002241 case flockLockingStyle:
2242 /* flock locking doesn't need additional lockingContext information */
2243 pNew->pMethod = &sqlite3FlockLockingUnixIoMethod;
2244 break;
drh5bb3eb92007-05-04 13:15:55 +00002245 case dotlockLockingStyle: {
drhbfe66312006-10-03 17:40:40 +00002246 /* dotlock locking uses the file path so it needs to be included in
2247 ** the dotlockLockingContext */
drh5bb3eb92007-05-04 13:15:55 +00002248 int nFilename;
drhbfe66312006-10-03 17:40:40 +00002249 pNew->pMethod = &sqlite3DotlockLockingUnixIoMethod;
drh17435752007-08-16 04:30:38 +00002250 pNew->lockingContext = sqlite3_malloc(
drhbfe66312006-10-03 17:40:40 +00002251 sizeof(dotlockLockingContext));
drh5bb3eb92007-05-04 13:15:55 +00002252 nFilename = strlen(zFilename) + 6;
drhbfe66312006-10-03 17:40:40 +00002253 ((dotlockLockingContext *)pNew->lockingContext)->lockPath =
drh17435752007-08-16 04:30:38 +00002254 sqlite3_malloc( nFilename );
drh5bb3eb92007-05-04 13:15:55 +00002255 sqlite3_snprintf(nFilename,
2256 ((dotlockLockingContext *)pNew->lockingContext)->lockPath,
drhbfe66312006-10-03 17:40:40 +00002257 "%s.lock", zFilename);
2258 break;
drh5bb3eb92007-05-04 13:15:55 +00002259 }
drhbfe66312006-10-03 17:40:40 +00002260 case posixLockingStyle:
2261 /* posix locking doesn't need additional lockingContext information */
2262 pNew->pMethod = &sqlite3UnixIoMethod;
2263 break;
2264 case noLockingStyle:
2265 case unsupportedLockingStyle:
2266 default:
2267 pNew->pMethod = &sqlite3NolockLockingUnixIoMethod;
2268 }
drhbfe66312006-10-03 17:40:40 +00002269 OpenCounter(+1);
2270 return SQLITE_OK;
2271 }
2272}
2273#else /* SQLITE_ENABLE_LOCKING_STYLE */
danielk1977b4b47412007-08-17 15:53:36 +00002274static int fillInUnixFile(
drhbfe66312006-10-03 17:40:40 +00002275 int h, /* Open file descriptor on file being opened */
danielk1977fee2d252007-08-18 10:59:19 +00002276 int dirfd,
danielk1977b4b47412007-08-17 15:53:36 +00002277 sqlite3_file *pId, /* Write to the unixFile structure here */
2278 const char *zFilename /* Name of the file being opened */
drhbfe66312006-10-03 17:40:40 +00002279){
danielk1977b4b47412007-08-17 15:53:36 +00002280 unixFile *pNew = (unixFile *)pId;
drhbfe66312006-10-03 17:40:40 +00002281 int rc;
2282
drhe78669b2007-06-29 12:04:26 +00002283#ifdef FD_CLOEXEC
2284 fcntl(h, F_SETFD, fcntl(h, F_GETFD, 0) | FD_CLOEXEC);
2285#endif
danielk1977b4b47412007-08-17 15:53:36 +00002286
2287 enterMutex();
2288 rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
2289 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00002290 if( rc ){
2291 close(h);
2292 return SQLITE_NOMEM;
2293 }
danielk1977b4b47412007-08-17 15:53:36 +00002294
drh4f0c5872007-03-26 22:05:01 +00002295 OSTRACE3("OPEN %-3d %s\n", h, zFilename);
danielk1977b4b47412007-08-17 15:53:36 +00002296 pNew->dirfd = -1;
2297 pNew->h = h;
danielk1977fee2d252007-08-18 10:59:19 +00002298 pNew->dirfd = dirfd;
danielk1977b4b47412007-08-17 15:53:36 +00002299 SET_THREADID(pNew);
2300
2301 pNew->pMethod = &sqlite3UnixIoMethod;
2302 OpenCounter(+1);
2303 return SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00002304}
drhbfe66312006-10-03 17:40:40 +00002305#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drh9c06c952005-11-26 00:25:00 +00002306
drh0ccebe72005-06-07 22:22:50 +00002307#endif /* SQLITE_OMIT_DISKIO */
2308/***************************************************************************
2309** Everything above deals with file I/O. Everything that follows deals
2310** with other miscellanous aspects of the operating system interface
2311****************************************************************************/
2312
danielk1977ad94b582007-08-20 06:44:22 +00002313/*
2314** Open a file descriptor to the directory containing file zFilename.
2315** If successful, *pFd is set to the opened file descriptor and
2316** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
2317** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
2318** value.
2319**
2320** If SQLITE_OK is returned, the caller is responsible for closing
2321** the file descriptor *pFd using close().
2322*/
danielk1977fee2d252007-08-18 10:59:19 +00002323static int openDirectory(const char *zFilename, int *pFd){
2324 char *zDirname;
2325 int ii;
2326 int fd;
2327
2328 zDirname = (char *)sqlite3_malloc(MAX_PATHNAME);
2329 if( !zDirname ){
2330 return SQLITE_NOMEM;
2331 }
2332 strncpy(zDirname, zFilename, MAX_PATHNAME);
2333 zDirname[MAX_PATHNAME-1] = '\0';
2334 for(ii=strlen(zDirname); ii>=0 && zDirname[ii]!='/'; ii--);
2335 if( ii>0 ){
2336 zDirname[ii] = '\0';
2337 fd = open(zDirname, O_RDONLY|O_BINARY, 0);
2338 if( fd>0 ){
2339#ifdef FD_CLOEXEC
2340 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
2341#endif
2342 OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname);
2343 }
2344 }
2345 sqlite3_free(zDirname);
2346 *pFd = fd;
2347 return (fd>0?SQLITE_OK:SQLITE_CANTOPEN);
2348}
2349
danielk1977b4b47412007-08-17 15:53:36 +00002350/*
danielk1977ad94b582007-08-20 06:44:22 +00002351** Open the file zPath.
2352**
danielk1977b4b47412007-08-17 15:53:36 +00002353** Previously, the SQLite OS layer used three functions in place of this
2354** one:
2355**
2356** sqlite3OsOpenReadWrite();
2357** sqlite3OsOpenReadOnly();
2358** sqlite3OsOpenExclusive();
2359**
2360** These calls correspond to the following combinations of flags:
2361**
2362** ReadWrite() -> (READWRITE | CREATE)
2363** ReadOnly() -> (READONLY)
2364** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
2365**
2366** The old OpenExclusive() accepted a boolean argument - "delFlag". If
2367** true, the file was configured to be automatically deleted when the
2368** file handle closed. To achieve the same effect using this new
2369** interface, add the DELETEONCLOSE flag to those specified above for
2370** OpenExclusive().
2371*/
2372static int unixOpen(
2373 void *pNotUsed,
2374 const char *zPath,
2375 sqlite3_file *pFile,
2376 int flags,
2377 int *pOutFlags
2378){
danielk1977fee2d252007-08-18 10:59:19 +00002379 int fd = 0; /* File descriptor returned by open() */
2380 int dirfd = -1; /* Directory file descriptor */
2381 int oflags = 0; /* Flags to pass to open() */
2382 int eType = flags&0xFFFFFF00; /* Type of file to open */
danielk1977b4b47412007-08-17 15:53:36 +00002383
2384 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
2385 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
2386 int isCreate = (flags & SQLITE_OPEN_CREATE);
2387 int isReadonly = (flags & SQLITE_OPEN_READONLY);
2388 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
2389
danielk1977fee2d252007-08-18 10:59:19 +00002390 /* If creating a master or main-file journal, this function will open
2391 ** a file-descriptor on the directory too. The first time unixSync()
2392 ** is called the directory file descriptor will be fsync()ed and close()d.
2393 */
2394 int isOpenDirectory = (isCreate &&
2395 (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL)
2396 );
2397
2398 /* Check the following statements are true:
2399 **
2400 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
2401 ** (b) if CREATE is set, then READWRITE must also be set, and
2402 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
2403 */
danielk1977b4b47412007-08-17 15:53:36 +00002404 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
danielk1977b4b47412007-08-17 15:53:36 +00002405 assert(isCreate==0 || isReadWrite);
danielk1977b4b47412007-08-17 15:53:36 +00002406 assert(isExclusive==0 || isCreate);
2407
danielk1977fee2d252007-08-18 10:59:19 +00002408 /* Assert that the upper layer has set one of the "file-type" flags. */
2409 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
2410 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
2411 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
2412 );
2413
danielk1977b4b47412007-08-17 15:53:36 +00002414 if( isReadonly ) oflags |= O_RDONLY;
2415 if( isReadWrite ) oflags |= O_RDWR;
2416 if( isCreate ) oflags |= O_CREAT;
2417 if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW);
2418 oflags |= (O_LARGEFILE|O_BINARY);
2419
2420 memset(pFile, 0, sizeof(unixFile));
2421 fd = open(zPath, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
2422 if( fd<0 && isReadWrite && !isExclusive ){
2423 /* Failed to open the file for read/write access. Try read-only. */
2424 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
2425 flags |= SQLITE_OPEN_READONLY;
2426 return unixOpen(pNotUsed, zPath, pFile, flags, pOutFlags);
2427 }
2428 if( fd<0 ){
2429 return SQLITE_CANTOPEN;
2430 }
2431 if( isDelete ){
2432 unlink(zPath);
2433 }
2434 if( pOutFlags ){
2435 *pOutFlags = flags;
2436 }
2437
2438 assert(fd!=0);
danielk1977fee2d252007-08-18 10:59:19 +00002439 if( isOpenDirectory ){
2440 int rc = openDirectory(zPath, &dirfd);
2441 if( rc!=SQLITE_OK ){
2442 close(fd);
2443 return rc;
2444 }
2445 }
2446 return fillInUnixFile(fd, dirfd, pFile, zPath);
danielk1977b4b47412007-08-17 15:53:36 +00002447}
2448
2449/*
danielk1977fee2d252007-08-18 10:59:19 +00002450** Delete the file at zPath. If the dirSync argument is true, fsync()
2451** the directory after deleting the file.
danielk1977b4b47412007-08-17 15:53:36 +00002452*/
danielk1977fee2d252007-08-18 10:59:19 +00002453static int unixDelete(void *pNotUsed, const char *zPath, int dirSync){
2454 int rc = SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00002455 SimulateIOError(return SQLITE_IOERR_DELETE);
2456 unlink(zPath);
danielk1977fee2d252007-08-18 10:59:19 +00002457 if( dirSync ){
2458 int fd;
2459 rc = openDirectory(zPath, &fd);
2460 if( rc==SQLITE_OK ){
2461 if( fsync(fd) ){
2462 rc = SQLITE_IOERR_DIR_FSYNC;
2463 }
2464 close(fd);
2465 }
2466 }
2467 return rc;
danielk1977b4b47412007-08-17 15:53:36 +00002468}
2469
danielk197790949c22007-08-17 16:50:38 +00002470/*
2471** Test the existance of or access permissions of file zPath. The
2472** test performed depends on the value of flags:
2473**
2474** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
2475** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
2476** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
2477**
2478** Otherwise return 0.
2479*/
danielk1977b4b47412007-08-17 15:53:36 +00002480static int unixAccess(void *pNotUsed, const char *zPath, int flags){
2481 int amode;
2482 switch( flags ){
2483 case SQLITE_ACCESS_EXISTS:
2484 amode = F_OK;
2485 break;
2486 case SQLITE_ACCESS_READWRITE:
2487 amode = W_OK|R_OK;
2488 break;
2489 case SQLITE_ACCESS_READONLY:
2490 amode = R_OK;
2491 break;
2492
2493 default:
2494 assert(!"Invalid flags argument");
2495 }
2496 return (access(zPath, amode)==0);
2497}
2498
2499/*
2500** Create a temporary file name in zBuf. zBuf must be big enough to
2501** hold at least MAX_PATHNAME characters.
2502*/
2503static int unixGetTempName(void *pNotUsed, char *zBuf){
2504 static const char *azDirs[] = {
2505 0,
2506 "/var/tmp",
2507 "/usr/tmp",
2508 "/tmp",
2509 ".",
2510 };
2511 static const unsigned char zChars[] =
2512 "abcdefghijklmnopqrstuvwxyz"
2513 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2514 "0123456789";
2515 int i, j;
2516 struct stat buf;
2517 const char *zDir = ".";
2518 azDirs[0] = sqlite3_temp_directory;
2519 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
2520 if( azDirs[i]==0 ) continue;
2521 if( stat(azDirs[i], &buf) ) continue;
2522 if( !S_ISDIR(buf.st_mode) ) continue;
2523 if( access(azDirs[i], 07) ) continue;
2524 zDir = azDirs[i];
2525 break;
2526 }
2527 do{
2528 sqlite3_snprintf(MAX_PATHNAME-17, zBuf, "%s/"TEMP_FILE_PREFIX, zDir);
2529 j = strlen(zBuf);
2530 sqlite3Randomness(15, &zBuf[j]);
2531 for(i=0; i<15; i++, j++){
2532 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
2533 }
2534 zBuf[j] = 0;
2535 }while( access(zBuf,0)==0 );
2536 return SQLITE_OK;
2537}
2538
2539
2540/*
2541** Turn a relative pathname into a full pathname. The relative path
2542** is stored as a nul-terminated string in the buffer pointed to by
2543** zPath.
2544**
2545** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
2546** (in this case, MAX_PATHNAME bytes). The full-path is written to
2547** this buffer before returning.
2548*/
2549static int unixFullPathname(void *pNotUsed, const char *zPath, char *zOut){
2550 zOut[MAX_PATHNAME-1] = '\0';
2551 if( zPath[0]=='/' ){
2552 strncpy(zOut, zPath, MAX_PATHNAME-1);
2553 }else{
2554 int nCwd;
2555 if( getcwd(zOut, MAX_PATHNAME-1)==0 ){
2556 return SQLITE_ERROR;
2557 }
2558 nCwd = strlen(zOut);
2559 zOut[nCwd] = '/';
2560 strncpy(&zOut[nCwd+1], zPath, MAX_PATHNAME-1-nCwd-1);
2561 }
2562 return SQLITE_OK;
2563
2564#if 0
2565 /*
2566 ** Remove "/./" path elements and convert "/A/./" path elements
2567 ** to just "/".
2568 */
2569 if( zFull ){
2570 int i, j;
2571 for(i=j=0; zFull[i]; i++){
2572 if( zFull[i]=='/' ){
2573 if( zFull[i+1]=='/' ) continue;
2574 if( zFull[i+1]=='.' && zFull[i+2]=='/' ){
2575 i += 1;
2576 continue;
2577 }
2578 if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){
2579 while( j>0 && zFull[j-1]!='/' ){ j--; }
2580 i += 3;
2581 continue;
2582 }
2583 }
2584 zFull[j++] = zFull[i];
2585 }
2586 zFull[j] = 0;
2587 }
2588#endif
2589}
2590
drh0ccebe72005-06-07 22:22:50 +00002591
drh761df872006-12-21 01:29:22 +00002592#ifndef SQLITE_OMIT_LOAD_EXTENSION
2593/*
2594** Interfaces for opening a shared library, finding entry points
2595** within the shared library, and closing the shared library.
2596*/
2597#include <dlfcn.h>
danielk1977b4b47412007-08-17 15:53:36 +00002598static void *unixDlOpen(void *pNotUsed, const char *zFilename){
drh761df872006-12-21 01:29:22 +00002599 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
2600}
danielk1977b4b47412007-08-17 15:53:36 +00002601static void unixDlError(void *pNotUsed, int nBuf, char *zBufOut){
2602 char *zErr;
2603 enterMutex();
2604 zErr = dlerror();
2605 if( zErr ){
2606 strncpy(zBufOut, zErr, nBuf-1);
2607 zBufOut[nBuf-1] = '\0';
2608 }else if(nBuf>0) {
2609 zBufOut[0] = '\0';
2610 }
2611 leaveMutex();
2612}
2613void *unixDlSym(void *pHandle, const char *zSymbol){
drh761df872006-12-21 01:29:22 +00002614 return dlsym(pHandle, zSymbol);
2615}
danielk1977b4b47412007-08-17 15:53:36 +00002616void unixDlClose(void *pHandle){
2617 dlclose(pHandle);
drh761df872006-12-21 01:29:22 +00002618}
danielk1977b4b47412007-08-17 15:53:36 +00002619#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
2620 #define unixDlOpen 0
2621 #define unixDlError 0
2622 #define unixDlSym 0
2623 #define unixDlClose 0
2624#endif
2625
2626/*
danielk197790949c22007-08-17 16:50:38 +00002627** Write nBuf bytes of random data to the supplied buffer zBuf.
drhbbd42a62004-05-22 17:41:58 +00002628*/
danielk1977b4b47412007-08-17 15:53:36 +00002629static int unixRandomness(void *pNotUsed, int nBuf, char *zBuf){
danielk197790949c22007-08-17 16:50:38 +00002630
2631 assert(nBuf>=(sizeof(time_t)+sizeof(int)));
2632
drhbbd42a62004-05-22 17:41:58 +00002633 /* We have to initialize zBuf to prevent valgrind from reporting
2634 ** errors. The reports issued by valgrind are incorrect - we would
2635 ** prefer that the randomness be increased by making use of the
2636 ** uninitialized space in zBuf - but valgrind errors tend to worry
2637 ** some users. Rather than argue, it seems easier just to initialize
2638 ** the whole array and silence valgrind, even if that means less randomness
2639 ** in the random seed.
2640 **
2641 ** When testing, initializing zBuf[] to zero is all we do. That means
drhf1a221e2006-01-15 17:27:17 +00002642 ** that we always use the same random number sequence. This makes the
drhbbd42a62004-05-22 17:41:58 +00002643 ** tests repeatable.
2644 */
danielk1977b4b47412007-08-17 15:53:36 +00002645 memset(zBuf, 0, nBuf);
drhbbd42a62004-05-22 17:41:58 +00002646#if !defined(SQLITE_TEST)
2647 {
drh842b8642005-01-21 17:53:17 +00002648 int pid, fd;
2649 fd = open("/dev/urandom", O_RDONLY);
2650 if( fd<0 ){
drh07397232006-01-06 14:46:46 +00002651 time_t t;
2652 time(&t);
danielk197790949c22007-08-17 16:50:38 +00002653 memcpy(zBuf, &t, sizeof(t));
2654 pid = getpid();
2655 memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
drh842b8642005-01-21 17:53:17 +00002656 }else{
danielk1977b4b47412007-08-17 15:53:36 +00002657 read(fd, zBuf, nBuf);
drh842b8642005-01-21 17:53:17 +00002658 close(fd);
2659 }
drhbbd42a62004-05-22 17:41:58 +00002660 }
2661#endif
2662 return SQLITE_OK;
2663}
2664
danielk1977b4b47412007-08-17 15:53:36 +00002665
drhbbd42a62004-05-22 17:41:58 +00002666/*
2667** Sleep for a little while. Return the amount of time slept.
danielk1977b4b47412007-08-17 15:53:36 +00002668** The argument is the number of microseconds we want to sleep.
drhbbd42a62004-05-22 17:41:58 +00002669*/
danielk1977b4b47412007-08-17 15:53:36 +00002670static int unixSleep(void *pNotUsed, int microseconds){
drhbbd42a62004-05-22 17:41:58 +00002671#if defined(HAVE_USLEEP) && HAVE_USLEEP
danielk1977b4b47412007-08-17 15:53:36 +00002672 usleep(microseconds);
2673 return microseconds;
drhbbd42a62004-05-22 17:41:58 +00002674#else
danielk1977b4b47412007-08-17 15:53:36 +00002675 int seconds = (microseconds+999999)/1000000;
2676 sleep(seconds);
2677 return seconds;
drha3fad6f2006-01-18 14:06:37 +00002678#endif
drh88f474a2006-01-02 20:00:12 +00002679}
2680
2681/*
drhbbd42a62004-05-22 17:41:58 +00002682** The following variable, if set to a non-zero value, becomes the result
drh66560ad2006-01-06 14:32:19 +00002683** returned from sqlite3OsCurrentTime(). This is used for testing.
drhbbd42a62004-05-22 17:41:58 +00002684*/
2685#ifdef SQLITE_TEST
2686int sqlite3_current_time = 0;
2687#endif
2688
2689/*
2690** Find the current time (in Universal Coordinated Time). Write the
2691** current time and date as a Julian Day number into *prNow and
2692** return 0. Return 1 if the time and date cannot be found.
2693*/
danielk1977b4b47412007-08-17 15:53:36 +00002694static int unixCurrentTime(void *pNotUsed, double *prNow){
drh19e2d372005-08-29 23:00:03 +00002695#ifdef NO_GETTOD
drhbbd42a62004-05-22 17:41:58 +00002696 time_t t;
2697 time(&t);
2698 *prNow = t/86400.0 + 2440587.5;
drh19e2d372005-08-29 23:00:03 +00002699#else
2700 struct timeval sNow;
drhbdcc2762007-04-02 18:06:57 +00002701 gettimeofday(&sNow, 0);
drh19e2d372005-08-29 23:00:03 +00002702 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
2703#endif
drhbbd42a62004-05-22 17:41:58 +00002704#ifdef SQLITE_TEST
2705 if( sqlite3_current_time ){
2706 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
2707 }
2708#endif
2709 return 0;
2710}
danielk1977b4b47412007-08-17 15:53:36 +00002711
2712
2713sqlite3_vfs sqlite3DefaultVfs = {
2714 1, /* iVersion */
2715 sizeof(unixFile), /* szOsFile */
2716 MAX_PATHNAME, /* mxPathname */
2717 0, /* nRef */
2718 0, /* vfsMutex */
2719 0, /* pNext */
2720 0, /* pPrev */
2721 "unix", /* zName */
2722 0, /* pAppData */
2723
2724 unixOpen, /* xOpen */
2725 unixDelete, /* xDelete */
2726 unixAccess, /* xAccess */
2727 unixGetTempName, /* xGetTempName */
2728 unixFullPathname, /* xFullPathname */
2729 unixDlOpen, /* xDlOpen */
2730 unixDlError, /* xDlError */
2731 unixDlSym, /* xDlSym */
2732 unixDlClose, /* xDlClose */
2733 unixRandomness, /* xRandomness */
2734 unixSleep, /* xSleep */
2735 unixCurrentTime /* xCurrentTime */
2736};
drhdce8bdb2007-08-16 13:01:44 +00002737
drhbbd42a62004-05-22 17:41:58 +00002738#endif /* OS_UNIX */