blob: ea38ec5cf92362c5e18475a0b756f7827f0131f0 [file] [log] [blame]
drhbbd42a62004-05-22 17:41:58 +00001/*
2** 2004 May 22
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11******************************************************************************
12**
13** This file contains code that is specific to Unix systems.
danielk1977822a5162008-05-16 04:51:54 +000014**
danielk1977cd3b3c82008-09-22 11:46:32 +000015** $Id: os_unix.c,v 1.202 2008/09/22 11:46:33 danielk1977 Exp $
drhbbd42a62004-05-22 17:41:58 +000016*/
drhbbd42a62004-05-22 17:41:58 +000017#include "sqliteInt.h"
danielk197729bafea2008-06-26 10:41:19 +000018#if SQLITE_OS_UNIX /* This file is used on unix only */
drh66560ad2006-01-06 14:32:19 +000019
danielk1977e339d652008-06-28 11:23:00 +000020/*
21** If SQLITE_ENABLE_LOCKING_STYLE is defined, then several different
22** locking implementations are provided:
23**
24** * POSIX locking (the default),
25** * No locking,
26** * Dot-file locking,
27** * flock() locking,
28** * AFP locking (OSX only).
29*/
drhbfe66312006-10-03 17:40:40 +000030/* #define SQLITE_ENABLE_LOCKING_STYLE 0 */
31
drh9cbe6352005-11-29 03:13:21 +000032/*
33** These #defines should enable >2GB file support on Posix if the
34** underlying operating system supports it. If the OS lacks
drhf1a221e2006-01-15 17:27:17 +000035** large file support, these should be no-ops.
drh9cbe6352005-11-29 03:13:21 +000036**
37** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
38** on the compiler command line. This is necessary if you are compiling
39** on a recent machine (ex: RedHat 7.2) but you want your code to work
40** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2
41** without this option, LFS is enable. But LFS does not exist in the kernel
42** in RedHat 6.0, so the code won't work. Hence, for maximum binary
43** portability you should omit LFS.
drh9cbe6352005-11-29 03:13:21 +000044*/
45#ifndef SQLITE_DISABLE_LFS
46# define _LARGE_FILE 1
47# ifndef _FILE_OFFSET_BITS
48# define _FILE_OFFSET_BITS 64
49# endif
50# define _LARGEFILE_SOURCE 1
51#endif
drhbbd42a62004-05-22 17:41:58 +000052
drh9cbe6352005-11-29 03:13:21 +000053/*
54** standard include files.
55*/
56#include <sys/types.h>
57#include <sys/stat.h>
58#include <fcntl.h>
59#include <unistd.h>
drhbbd42a62004-05-22 17:41:58 +000060#include <time.h>
drh19e2d372005-08-29 23:00:03 +000061#include <sys/time.h>
drhbbd42a62004-05-22 17:41:58 +000062#include <errno.h>
danielk1977e339d652008-06-28 11:23:00 +000063
drhbfe66312006-10-03 17:40:40 +000064#ifdef SQLITE_ENABLE_LOCKING_STYLE
65#include <sys/ioctl.h>
66#include <sys/param.h>
67#include <sys/mount.h>
68#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drh9cbe6352005-11-29 03:13:21 +000069
70/*
drhf1a221e2006-01-15 17:27:17 +000071** If we are to be thread-safe, include the pthreads header and define
72** the SQLITE_UNIX_THREADS macro.
drh9cbe6352005-11-29 03:13:21 +000073*/
drhd677b3d2007-08-20 22:48:41 +000074#if SQLITE_THREADSAFE
drh9cbe6352005-11-29 03:13:21 +000075# include <pthread.h>
76# define SQLITE_UNIX_THREADS 1
77#endif
78
79/*
80** Default permissions when creating a new file
81*/
82#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
83# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
84#endif
85
danielk1977b4b47412007-08-17 15:53:36 +000086/*
87** Maximum supported path-length.
88*/
89#define MAX_PATHNAME 512
drh9cbe6352005-11-29 03:13:21 +000090
91
92/*
danielk1977ad94b582007-08-20 06:44:22 +000093** The unixFile structure is subclass of sqlite3_file specific for the unix
drh054889e2005-11-30 03:20:31 +000094** protability layer.
drh9cbe6352005-11-29 03:13:21 +000095*/
drh054889e2005-11-30 03:20:31 +000096typedef struct unixFile unixFile;
97struct unixFile {
danielk197762079062007-08-15 17:08:46 +000098 sqlite3_io_methods const *pMethod; /* Always the first entry */
danielk1977967a4a12007-08-20 14:23:44 +000099#ifdef SQLITE_TEST
100 /* In test mode, increase the size of this structure a bit so that
101 ** it is larger than the struct CrashFile defined in test6.c.
102 */
103 char aPadding[32];
104#endif
drh9cbe6352005-11-29 03:13:21 +0000105 struct openCnt *pOpen; /* Info about all open fd's on this inode */
106 struct lockInfo *pLock; /* Info about locks on this inode */
drhbfe66312006-10-03 17:40:40 +0000107#ifdef SQLITE_ENABLE_LOCKING_STYLE
108 void *lockingContext; /* Locking style specific state */
danielk1977e339d652008-06-28 11:23:00 +0000109#endif
drh9cbe6352005-11-29 03:13:21 +0000110 int h; /* The file descriptor */
111 unsigned char locktype; /* The type of lock held on this fd */
drh9cbe6352005-11-29 03:13:21 +0000112 int dirfd; /* File descriptor for the directory */
drhd677b3d2007-08-20 22:48:41 +0000113#if SQLITE_THREADSAFE
danielk1977ad94b582007-08-20 06:44:22 +0000114 pthread_t tid; /* The thread that "owns" this unixFile */
drh9cbe6352005-11-29 03:13:21 +0000115#endif
aswift5b1a2562008-08-22 00:22:35 +0000116 int lastErrno; /* The unix errno from the last I/O error */
drh9cbe6352005-11-29 03:13:21 +0000117};
118
drh0ccebe72005-06-07 22:22:50 +0000119/*
drh198bf392006-01-06 21:52:49 +0000120** Include code that is common to all os_*.c files
121*/
122#include "os_common.h"
123
124/*
drh0ccebe72005-06-07 22:22:50 +0000125** Define various macros that are missing from some systems.
126*/
drhbbd42a62004-05-22 17:41:58 +0000127#ifndef O_LARGEFILE
128# define O_LARGEFILE 0
129#endif
130#ifdef SQLITE_DISABLE_LFS
131# undef O_LARGEFILE
132# define O_LARGEFILE 0
133#endif
134#ifndef O_NOFOLLOW
135# define O_NOFOLLOW 0
136#endif
137#ifndef O_BINARY
138# define O_BINARY 0
139#endif
140
141/*
142** The DJGPP compiler environment looks mostly like Unix, but it
143** lacks the fcntl() system call. So redefine fcntl() to be something
144** that always succeeds. This means that locking does not occur under
drh85b623f2007-12-13 21:54:09 +0000145** DJGPP. But it is DOS - what did you expect?
drhbbd42a62004-05-22 17:41:58 +0000146*/
147#ifdef __DJGPP__
148# define fcntl(A,B,C) 0
149#endif
150
151/*
drh2b4b5962005-06-15 17:47:55 +0000152** The threadid macro resolves to the thread-id or to 0. Used for
153** testing and debugging only.
154*/
drhd677b3d2007-08-20 22:48:41 +0000155#if SQLITE_THREADSAFE
drh2b4b5962005-06-15 17:47:55 +0000156#define threadid pthread_self()
157#else
158#define threadid 0
159#endif
160
161/*
danielk1977ad94b582007-08-20 06:44:22 +0000162** Set or check the unixFile.tid field. This field is set when an unixFile
163** is first opened. All subsequent uses of the unixFile verify that the
164** same thread is operating on the unixFile. Some operating systems do
drh2b4b5962005-06-15 17:47:55 +0000165** not allow locks to be overridden by other threads and that restriction
166** means that sqlite3* database handles cannot be moved from one thread
167** to another. This logic makes sure a user does not try to do that
168** by mistake.
drhf1a221e2006-01-15 17:27:17 +0000169**
danielk1977ad94b582007-08-20 06:44:22 +0000170** Version 3.3.1 (2006-01-15): unixFile can be moved from one thread to
drhf1a221e2006-01-15 17:27:17 +0000171** another as long as we are running on a system that supports threads
172** overriding each others locks (which now the most common behavior)
danielk1977ad94b582007-08-20 06:44:22 +0000173** or if no locks are held. But the unixFile.pLock field needs to be
drhf1a221e2006-01-15 17:27:17 +0000174** recomputed because its key includes the thread-id. See the
175** transferOwnership() function below for additional information
drh2b4b5962005-06-15 17:47:55 +0000176*/
drhd677b3d2007-08-20 22:48:41 +0000177#if SQLITE_THREADSAFE
drh9cbe6352005-11-29 03:13:21 +0000178# define SET_THREADID(X) (X)->tid = pthread_self()
drh029b44b2006-01-15 00:13:15 +0000179# define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
180 !pthread_equal((X)->tid, pthread_self()))
drh2b4b5962005-06-15 17:47:55 +0000181#else
182# define SET_THREADID(X)
183# define CHECK_THREADID(X) 0
danielk197713adf8a2004-06-03 16:08:41 +0000184#endif
185
drhbbd42a62004-05-22 17:41:58 +0000186/*
187** Here is the dirt on POSIX advisory locks: ANSI STD 1003.1 (1996)
188** section 6.5.2.2 lines 483 through 490 specify that when a process
189** sets or clears a lock, that operation overrides any prior locks set
190** by the same process. It does not explicitly say so, but this implies
191** that it overrides locks set by the same process using a different
192** file descriptor. Consider this test case:
drhbbd42a62004-05-22 17:41:58 +0000193** 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 */
drhd677b3d2007-08-20 22:48:41 +0000296#if SQLITE_THREADSAFE
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 */
drhda0e7682008-07-30 15:27:54 +0000315 struct lockInfo *pNext, *pPrev; /* List of all lockInfo objects */
drhbbd42a62004-05-22 17:41:58 +0000316};
317
318/*
319** An instance of the following structure serves as the key used
320** to locate a particular openCnt structure given its inode. This
drh5fdae772004-06-29 03:29:00 +0000321** is the same as the lockKey except that the thread ID is omitted.
drhbbd42a62004-05-22 17:41:58 +0000322*/
323struct openKey {
324 dev_t dev; /* Device number */
325 ino_t ino; /* Inode number */
326};
327
328/*
329** An instance of the following structure is allocated for each open
330** inode. This structure keeps track of the number of locks on that
331** inode. If a close is attempted against an inode that is holding
332** locks, the close is deferred until all locks clear by adding the
333** file descriptor to be closed to the pending list.
334*/
335struct openCnt {
336 struct openKey key; /* The lookup key */
337 int nRef; /* Number of pointers to this structure */
338 int nLock; /* Number of outstanding locks */
339 int nPending; /* Number of pending close() operations */
340 int *aPending; /* Malloced space holding fd's awaiting a close() */
drhda0e7682008-07-30 15:27:54 +0000341 struct openCnt *pNext, *pPrev; /* List of all openCnt objects */
drhbbd42a62004-05-22 17:41:58 +0000342};
343
drhda0e7682008-07-30 15:27:54 +0000344/*
345** List of all lockInfo and openCnt objects. This used to be a hash
346** table. But the number of objects is rarely more than a dozen and
347** never exceeds a few thousand. And lookup is not on a critical
348** path oo a simple linked list will suffice.
drhbbd42a62004-05-22 17:41:58 +0000349*/
drhda0e7682008-07-30 15:27:54 +0000350static struct lockInfo *lockList = 0;
351static struct openCnt *openList = 0;
drh5fdae772004-06-29 03:29:00 +0000352
drhbfe66312006-10-03 17:40:40 +0000353/*
354** The locking styles are associated with the different file locking
355** capabilities supported by different file systems.
356**
357** POSIX locking style fully supports shared and exclusive byte-range locks
danielk1977e339d652008-06-28 11:23:00 +0000358** AFP locking only supports exclusive byte-range locks
drhbfe66312006-10-03 17:40:40 +0000359** FLOCK only supports a single file-global exclusive lock
360** DOTLOCK isn't a true locking style, it refers to the use of a special
361** file named the same as the database file with a '.lock' extension, this
362** can be used on file systems that do not offer any reliable file locking
363** NO locking means that no locking will be attempted, this is only used for
364** read-only file systems currently
365** UNSUPPORTED means that no locking will be attempted, this is only used for
366** file systems that are known to be unsupported
367*/
danielk1977e339d652008-06-28 11:23:00 +0000368#define LOCKING_STYLE_POSIX 1
drhda0e7682008-07-30 15:27:54 +0000369#define LOCKING_STYLE_NONE 2
danielk1977e339d652008-06-28 11:23:00 +0000370#define LOCKING_STYLE_DOTFILE 3
drhda0e7682008-07-30 15:27:54 +0000371#define LOCKING_STYLE_FLOCK 4
danielk1977e339d652008-06-28 11:23:00 +0000372#define LOCKING_STYLE_AFP 5
drhbfe66312006-10-03 17:40:40 +0000373
danielk1977ad94b582007-08-20 06:44:22 +0000374/*
aswift5b1a2562008-08-22 00:22:35 +0000375** Only set the lastErrno if the error code is a real error and not
376** a normal expected return code of SQLITE_BUSY or SQLITE_OK
377*/
378#define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY))
379
380/*
danielk1977ad94b582007-08-20 06:44:22 +0000381** Helper functions to obtain and relinquish the global mutex.
382*/
danielk1977b4b47412007-08-17 15:53:36 +0000383static void enterMutex(){
danielk197759f8c082008-06-18 17:09:10 +0000384 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
danielk1977b4b47412007-08-17 15:53:36 +0000385}
386static void leaveMutex(){
danielk197759f8c082008-06-18 17:09:10 +0000387 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
danielk1977b4b47412007-08-17 15:53:36 +0000388}
389
drhd677b3d2007-08-20 22:48:41 +0000390#if SQLITE_THREADSAFE
drh5fdae772004-06-29 03:29:00 +0000391/*
392** This variable records whether or not threads can override each others
393** locks.
394**
395** 0: No. Threads cannot override each others locks.
396** 1: Yes. Threads can override each others locks.
397** -1: We don't know yet.
drhf1a221e2006-01-15 17:27:17 +0000398**
drh5062d3a2006-01-31 23:03:35 +0000399** On some systems, we know at compile-time if threads can override each
400** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro
401** will be set appropriately. On other systems, we have to check at
402** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is
403** undefined.
404**
drhf1a221e2006-01-15 17:27:17 +0000405** This variable normally has file scope only. But during testing, we make
406** it a global so that the test code can change its value in order to verify
407** that the right stuff happens in either case.
drh5fdae772004-06-29 03:29:00 +0000408*/
drh5062d3a2006-01-31 23:03:35 +0000409#ifndef SQLITE_THREAD_OVERRIDE_LOCK
410# define SQLITE_THREAD_OVERRIDE_LOCK -1
411#endif
drh029b44b2006-01-15 00:13:15 +0000412#ifdef SQLITE_TEST
drh5062d3a2006-01-31 23:03:35 +0000413int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh029b44b2006-01-15 00:13:15 +0000414#else
drh5062d3a2006-01-31 23:03:35 +0000415static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
drh029b44b2006-01-15 00:13:15 +0000416#endif
drh5fdae772004-06-29 03:29:00 +0000417
418/*
419** This structure holds information passed into individual test
420** threads by the testThreadLockingBehavior() routine.
421*/
422struct threadTestData {
423 int fd; /* File to be locked */
424 struct flock lock; /* The locking operation */
425 int result; /* Result of the locking operation */
426};
427
drh2b4b5962005-06-15 17:47:55 +0000428#ifdef SQLITE_LOCK_TRACE
429/*
430** Print out information about all locking operations.
431**
432** This routine is used for troubleshooting locks on multithreaded
433** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
434** command-line option on the compiler. This code is normally
drhf1a221e2006-01-15 17:27:17 +0000435** turned off.
drh2b4b5962005-06-15 17:47:55 +0000436*/
437static int lockTrace(int fd, int op, struct flock *p){
438 char *zOpName, *zType;
439 int s;
440 int savedErrno;
441 if( op==F_GETLK ){
442 zOpName = "GETLK";
443 }else if( op==F_SETLK ){
444 zOpName = "SETLK";
445 }else{
446 s = fcntl(fd, op, p);
447 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
448 return s;
449 }
450 if( p->l_type==F_RDLCK ){
451 zType = "RDLCK";
452 }else if( p->l_type==F_WRLCK ){
453 zType = "WRLCK";
454 }else if( p->l_type==F_UNLCK ){
455 zType = "UNLCK";
456 }else{
457 assert( 0 );
458 }
459 assert( p->l_whence==SEEK_SET );
460 s = fcntl(fd, op, p);
461 savedErrno = errno;
462 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
463 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
464 (int)p->l_pid, s);
drhe2396a12007-03-29 20:19:58 +0000465 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
drh2b4b5962005-06-15 17:47:55 +0000466 struct flock l2;
467 l2 = *p;
468 fcntl(fd, F_GETLK, &l2);
469 if( l2.l_type==F_RDLCK ){
470 zType = "RDLCK";
471 }else if( l2.l_type==F_WRLCK ){
472 zType = "WRLCK";
473 }else if( l2.l_type==F_UNLCK ){
474 zType = "UNLCK";
475 }else{
476 assert( 0 );
477 }
478 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
479 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
480 }
481 errno = savedErrno;
482 return s;
483}
484#define fcntl lockTrace
485#endif /* SQLITE_LOCK_TRACE */
486
drh5fdae772004-06-29 03:29:00 +0000487/*
488** The testThreadLockingBehavior() routine launches two separate
489** threads on this routine. This routine attempts to lock a file
490** descriptor then returns. The success or failure of that attempt
491** allows the testThreadLockingBehavior() procedure to determine
492** whether or not threads can override each others locks.
493*/
494static void *threadLockingTest(void *pArg){
495 struct threadTestData *pData = (struct threadTestData*)pArg;
496 pData->result = fcntl(pData->fd, F_SETLK, &pData->lock);
497 return pArg;
498}
499
500/*
501** This procedure attempts to determine whether or not threads
502** can override each others locks then sets the
503** threadsOverrideEachOthersLocks variable appropriately.
504*/
danielk19774d5238f2006-01-27 06:32:00 +0000505static void testThreadLockingBehavior(int fd_orig){
drh5fdae772004-06-29 03:29:00 +0000506 int fd;
507 struct threadTestData d[2];
508 pthread_t t[2];
509
510 fd = dup(fd_orig);
511 if( fd<0 ) return;
512 memset(d, 0, sizeof(d));
513 d[0].fd = fd;
514 d[0].lock.l_type = F_RDLCK;
515 d[0].lock.l_len = 1;
516 d[0].lock.l_start = 0;
517 d[0].lock.l_whence = SEEK_SET;
518 d[1] = d[0];
519 d[1].lock.l_type = F_WRLCK;
520 pthread_create(&t[0], 0, threadLockingTest, &d[0]);
521 pthread_create(&t[1], 0, threadLockingTest, &d[1]);
522 pthread_join(t[0], 0);
523 pthread_join(t[1], 0);
524 close(fd);
525 threadsOverrideEachOthersLocks = d[0].result==0 && d[1].result==0;
526}
drhd677b3d2007-08-20 22:48:41 +0000527#endif /* SQLITE_THREADSAFE */
drh5fdae772004-06-29 03:29:00 +0000528
drhbbd42a62004-05-22 17:41:58 +0000529/*
530** Release a lockInfo structure previously allocated by findLockInfo().
531*/
532static void releaseLockInfo(struct lockInfo *pLock){
danielk1977e339d652008-06-28 11:23:00 +0000533 if( pLock ){
534 pLock->nRef--;
535 if( pLock->nRef==0 ){
drhda0e7682008-07-30 15:27:54 +0000536 if( pLock->pPrev ){
537 assert( pLock->pPrev->pNext==pLock );
538 pLock->pPrev->pNext = pLock->pNext;
539 }else{
540 assert( lockList==pLock );
541 lockList = pLock->pNext;
542 }
543 if( pLock->pNext ){
544 assert( pLock->pNext->pPrev==pLock );
545 pLock->pNext->pPrev = pLock->pPrev;
546 }
danielk1977e339d652008-06-28 11:23:00 +0000547 sqlite3_free(pLock);
548 }
drhbbd42a62004-05-22 17:41:58 +0000549 }
550}
551
552/*
553** Release a openCnt structure previously allocated by findLockInfo().
554*/
555static void releaseOpenCnt(struct openCnt *pOpen){
danielk1977e339d652008-06-28 11:23:00 +0000556 if( pOpen ){
557 pOpen->nRef--;
558 if( pOpen->nRef==0 ){
drhda0e7682008-07-30 15:27:54 +0000559 if( pOpen->pPrev ){
560 assert( pOpen->pPrev->pNext==pOpen );
561 pOpen->pPrev->pNext = pOpen->pNext;
562 }else{
563 assert( openList==pOpen );
564 openList = pOpen->pNext;
565 }
566 if( pOpen->pNext ){
567 assert( pOpen->pNext->pPrev==pOpen );
568 pOpen->pNext->pPrev = pOpen->pPrev;
569 }
570 sqlite3_free(pOpen->aPending);
danielk1977e339d652008-06-28 11:23:00 +0000571 sqlite3_free(pOpen);
572 }
drhbbd42a62004-05-22 17:41:58 +0000573 }
574}
575
drh93a960a2008-07-10 00:32:42 +0000576#ifdef SQLITE_ENABLE_LOCKING_STYLE
drhbfe66312006-10-03 17:40:40 +0000577/*
578** Tests a byte-range locking query to see if byte range locks are
579** supported, if not we fall back to dotlockLockingStyle.
580*/
danielk1977e339d652008-06-28 11:23:00 +0000581static int testLockingStyle(int fd){
drhbfe66312006-10-03 17:40:40 +0000582 struct flock lockInfo;
danielk1977e339d652008-06-28 11:23:00 +0000583
584 /* Test byte-range lock using fcntl(). If the call succeeds,
585 ** assume that the file-system supports POSIX style locks.
586 */
drhbfe66312006-10-03 17:40:40 +0000587 lockInfo.l_len = 1;
588 lockInfo.l_start = 0;
589 lockInfo.l_whence = SEEK_SET;
590 lockInfo.l_type = F_RDLCK;
danielk1977ad94b582007-08-20 06:44:22 +0000591 if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
danielk1977e339d652008-06-28 11:23:00 +0000592 return LOCKING_STYLE_POSIX;
593 }
drhbfe66312006-10-03 17:40:40 +0000594
danielk1977e339d652008-06-28 11:23:00 +0000595 /* Testing for flock() can give false positives. So if if the above
596 ** test fails, then we fall back to using dot-file style locking.
drhbfe66312006-10-03 17:40:40 +0000597 */
danielk1977e339d652008-06-28 11:23:00 +0000598 return LOCKING_STYLE_DOTFILE;
drhbfe66312006-10-03 17:40:40 +0000599}
drh93a960a2008-07-10 00:32:42 +0000600#endif
drhbfe66312006-10-03 17:40:40 +0000601
602/*
danielk1977e339d652008-06-28 11:23:00 +0000603** If SQLITE_ENABLE_LOCKING_STYLE is defined, this function Examines the
604** f_fstypename entry in the statfs structure as returned by stat() for
605** the file system hosting the database file and selects the appropriate
606** locking style based on its value. These values and assignments are
607** based on Darwin/OSX behavior and have not been thoroughly tested on
drhbfe66312006-10-03 17:40:40 +0000608** other systems.
danielk1977e339d652008-06-28 11:23:00 +0000609**
610** If SQLITE_ENABLE_LOCKING_STYLE is not defined, this function always
611** returns LOCKING_STYLE_POSIX.
drhbfe66312006-10-03 17:40:40 +0000612*/
danielk1977e339d652008-06-28 11:23:00 +0000613static int detectLockingStyle(
614 sqlite3_vfs *pVfs,
danielk1977ad94b582007-08-20 06:44:22 +0000615 const char *filePath,
616 int fd
617){
danielk1977e339d652008-06-28 11:23:00 +0000618#ifdef SQLITE_ENABLE_LOCKING_STYLE
619 struct Mapping {
620 const char *zFilesystem;
621 int eLockingStyle;
622 } aMap[] = {
623 { "hfs", LOCKING_STYLE_POSIX },
624 { "ufs", LOCKING_STYLE_POSIX },
625 { "afpfs", LOCKING_STYLE_AFP },
aswift5b1a2562008-08-22 00:22:35 +0000626#ifdef SQLITE_ENABLE_AFP_LOCKING_SMB
627 { "smbfs", LOCKING_STYLE_AFP },
628#else
danielk1977e339d652008-06-28 11:23:00 +0000629 { "smbfs", LOCKING_STYLE_FLOCK },
aswift5b1a2562008-08-22 00:22:35 +0000630#endif
danielk1977e339d652008-06-28 11:23:00 +0000631 { "msdos", LOCKING_STYLE_DOTFILE },
632 { "webdav", LOCKING_STYLE_NONE },
633 { 0, 0 }
634 };
635 int i;
drhbfe66312006-10-03 17:40:40 +0000636 struct statfs fsInfo;
637
danielk1977e339d652008-06-28 11:23:00 +0000638 if( !filePath ){
639 return LOCKING_STYLE_NONE;
drh339eb0b2008-03-07 15:34:11 +0000640 }
danielk1977e339d652008-06-28 11:23:00 +0000641 if( pVfs->pAppData ){
aswiftf54b1b32008-08-22 18:41:37 +0000642 return SQLITE_PTR_TO_INT(pVfs->pAppData);
drh339eb0b2008-03-07 15:34:11 +0000643 }
drhbfe66312006-10-03 17:40:40 +0000644
danielk1977e339d652008-06-28 11:23:00 +0000645 if( statfs(filePath, &fsInfo) != -1 ){
646 if( fsInfo.f_flags & MNT_RDONLY ){
647 return LOCKING_STYLE_NONE;
648 }
649 for(i=0; aMap[i].zFilesystem; i++){
650 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
651 return aMap[i].eLockingStyle;
652 }
653 }
654 }
655
656 /* Default case. Handles, amongst others, "nfs". */
657 return testLockingStyle(fd);
658#endif
659 return LOCKING_STYLE_POSIX;
660}
drhbfe66312006-10-03 17:40:40 +0000661
drhbbd42a62004-05-22 17:41:58 +0000662/*
663** Given a file descriptor, locate lockInfo and openCnt structures that
drh029b44b2006-01-15 00:13:15 +0000664** describes that file descriptor. Create new ones if necessary. The
665** return values might be uninitialized if an error occurs.
drhbbd42a62004-05-22 17:41:58 +0000666**
drh65594042008-05-05 16:56:34 +0000667** Return an appropriate error code.
drhbbd42a62004-05-22 17:41:58 +0000668*/
drh38f82712004-06-18 17:10:16 +0000669static int findLockInfo(
drhbbd42a62004-05-22 17:41:58 +0000670 int fd, /* The file descriptor used in the key */
671 struct lockInfo **ppLock, /* Return the lockInfo structure here */
drh5fdae772004-06-29 03:29:00 +0000672 struct openCnt **ppOpen /* Return the openCnt structure here */
drhbbd42a62004-05-22 17:41:58 +0000673){
674 int rc;
675 struct lockKey key1;
676 struct openKey key2;
677 struct stat statbuf;
678 struct lockInfo *pLock;
679 struct openCnt *pOpen;
680 rc = fstat(fd, &statbuf);
drh65594042008-05-05 16:56:34 +0000681 if( rc!=0 ){
682#ifdef EOVERFLOW
683 if( errno==EOVERFLOW ) return SQLITE_NOLFS;
684#endif
685 return SQLITE_IOERR;
686 }
danielk1977441b09a2006-01-05 13:48:29 +0000687
drh54626242008-07-30 17:28:04 +0000688 /* On OS X on an msdos filesystem, the inode number is reported
689 ** incorrectly for zero-size files. See ticket #3260. To work
690 ** around this problem (we consider it a bug in OS X, not SQLite)
691 ** we always increase the file size to 1 by writing a single byte
692 ** prior to accessing the inode number. The one byte written is
693 ** an ASCII 'S' character which also happens to be the first byte
694 ** in the header of every SQLite database. In this way, if there
695 ** is a race condition such that another thread has already populated
696 ** the first page of the database, no damage is done.
697 */
698 if( statbuf.st_size==0 ){
699 write(fd, "S", 1);
700 rc = fstat(fd, &statbuf);
701 if( rc!=0 ){
702 return SQLITE_IOERR;
703 }
704 }
705
drhbbd42a62004-05-22 17:41:58 +0000706 memset(&key1, 0, sizeof(key1));
707 key1.dev = statbuf.st_dev;
708 key1.ino = statbuf.st_ino;
drhd677b3d2007-08-20 22:48:41 +0000709#if SQLITE_THREADSAFE
drh5fdae772004-06-29 03:29:00 +0000710 if( threadsOverrideEachOthersLocks<0 ){
711 testThreadLockingBehavior(fd);
712 }
713 key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
714#endif
drhbbd42a62004-05-22 17:41:58 +0000715 memset(&key2, 0, sizeof(key2));
716 key2.dev = statbuf.st_dev;
717 key2.ino = statbuf.st_ino;
drhda0e7682008-07-30 15:27:54 +0000718 pLock = lockList;
719 while( pLock && memcmp(&key1, &pLock->key, sizeof(key1)) ){
720 pLock = pLock->pNext;
721 }
drhbbd42a62004-05-22 17:41:58 +0000722 if( pLock==0 ){
drh17435752007-08-16 04:30:38 +0000723 pLock = sqlite3_malloc( sizeof(*pLock) );
danielk1977441b09a2006-01-05 13:48:29 +0000724 if( pLock==0 ){
drh65594042008-05-05 16:56:34 +0000725 rc = SQLITE_NOMEM;
danielk1977441b09a2006-01-05 13:48:29 +0000726 goto exit_findlockinfo;
727 }
drhbbd42a62004-05-22 17:41:58 +0000728 pLock->key = key1;
729 pLock->nRef = 1;
730 pLock->cnt = 0;
danielk19779a1d0ab2004-06-01 14:09:28 +0000731 pLock->locktype = 0;
drhda0e7682008-07-30 15:27:54 +0000732 pLock->pNext = lockList;
733 pLock->pPrev = 0;
734 if( lockList ) lockList->pPrev = pLock;
735 lockList = pLock;
drhbbd42a62004-05-22 17:41:58 +0000736 }else{
737 pLock->nRef++;
738 }
739 *ppLock = pLock;
drh029b44b2006-01-15 00:13:15 +0000740 if( ppOpen!=0 ){
drhda0e7682008-07-30 15:27:54 +0000741 pOpen = openList;
742 while( pOpen && memcmp(&key2, &pOpen->key, sizeof(key2)) ){
743 pOpen = pOpen->pNext;
744 }
drhbbd42a62004-05-22 17:41:58 +0000745 if( pOpen==0 ){
drh17435752007-08-16 04:30:38 +0000746 pOpen = sqlite3_malloc( sizeof(*pOpen) );
drh029b44b2006-01-15 00:13:15 +0000747 if( pOpen==0 ){
748 releaseLockInfo(pLock);
drh65594042008-05-05 16:56:34 +0000749 rc = SQLITE_NOMEM;
drh029b44b2006-01-15 00:13:15 +0000750 goto exit_findlockinfo;
751 }
752 pOpen->key = key2;
753 pOpen->nRef = 1;
754 pOpen->nLock = 0;
755 pOpen->nPending = 0;
756 pOpen->aPending = 0;
drhda0e7682008-07-30 15:27:54 +0000757 pOpen->pNext = openList;
758 pOpen->pPrev = 0;
759 if( openList ) openList->pPrev = pOpen;
760 openList = pOpen;
drh029b44b2006-01-15 00:13:15 +0000761 }else{
762 pOpen->nRef++;
drhbbd42a62004-05-22 17:41:58 +0000763 }
drh029b44b2006-01-15 00:13:15 +0000764 *ppOpen = pOpen;
drhbbd42a62004-05-22 17:41:58 +0000765 }
danielk1977441b09a2006-01-05 13:48:29 +0000766
767exit_findlockinfo:
danielk1977441b09a2006-01-05 13:48:29 +0000768 return rc;
drhbbd42a62004-05-22 17:41:58 +0000769}
770
drh64b1bea2006-01-15 02:30:57 +0000771#ifdef SQLITE_DEBUG
772/*
773** Helper function for printing out trace information from debugging
774** binaries. This returns the string represetation of the supplied
775** integer lock-type.
776*/
777static const char *locktypeName(int locktype){
778 switch( locktype ){
779 case NO_LOCK: return "NONE";
780 case SHARED_LOCK: return "SHARED";
781 case RESERVED_LOCK: return "RESERVED";
782 case PENDING_LOCK: return "PENDING";
783 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
784 }
785 return "ERROR";
786}
787#endif
788
drhbbd42a62004-05-22 17:41:58 +0000789/*
drh029b44b2006-01-15 00:13:15 +0000790** If we are currently in a different thread than the thread that the
791** unixFile argument belongs to, then transfer ownership of the unixFile
792** over to the current thread.
793**
794** A unixFile is only owned by a thread on systems where one thread is
795** unable to override locks created by a different thread. RedHat9 is
796** an example of such a system.
797**
798** Ownership transfer is only allowed if the unixFile is currently unlocked.
799** If the unixFile is locked and an ownership is wrong, then return
drhf1a221e2006-01-15 17:27:17 +0000800** SQLITE_MISUSE. SQLITE_OK is returned if everything works.
drh029b44b2006-01-15 00:13:15 +0000801*/
drhd677b3d2007-08-20 22:48:41 +0000802#if SQLITE_THREADSAFE
drh029b44b2006-01-15 00:13:15 +0000803static int transferOwnership(unixFile *pFile){
drh64b1bea2006-01-15 02:30:57 +0000804 int rc;
drh029b44b2006-01-15 00:13:15 +0000805 pthread_t hSelf;
806 if( threadsOverrideEachOthersLocks ){
807 /* Ownership transfers not needed on this system */
808 return SQLITE_OK;
809 }
810 hSelf = pthread_self();
811 if( pthread_equal(pFile->tid, hSelf) ){
812 /* We are still in the same thread */
drh4f0c5872007-03-26 22:05:01 +0000813 OSTRACE1("No-transfer, same thread\n");
drh029b44b2006-01-15 00:13:15 +0000814 return SQLITE_OK;
815 }
816 if( pFile->locktype!=NO_LOCK ){
817 /* We cannot change ownership while we are holding a lock! */
818 return SQLITE_MISUSE;
819 }
drh4f0c5872007-03-26 22:05:01 +0000820 OSTRACE4("Transfer ownership of %d from %d to %d\n",
821 pFile->h, pFile->tid, hSelf);
drh029b44b2006-01-15 00:13:15 +0000822 pFile->tid = hSelf;
drhbfe66312006-10-03 17:40:40 +0000823 if (pFile->pLock != NULL) {
824 releaseLockInfo(pFile->pLock);
825 rc = findLockInfo(pFile->h, &pFile->pLock, 0);
drh4f0c5872007-03-26 22:05:01 +0000826 OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +0000827 locktypeName(pFile->locktype),
828 locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
829 return rc;
830 } else {
831 return SQLITE_OK;
832 }
drh029b44b2006-01-15 00:13:15 +0000833}
834#else
drhf1a221e2006-01-15 17:27:17 +0000835 /* On single-threaded builds, ownership transfer is a no-op */
drh029b44b2006-01-15 00:13:15 +0000836# define transferOwnership(X) SQLITE_OK
837#endif
838
839/*
danielk19772a6bdf62007-08-20 16:07:00 +0000840** Seek to the offset passed as the second argument, then read cnt
841** bytes into pBuf. Return the number of bytes actually read.
drh9e0ebbf2007-10-23 15:59:18 +0000842**
843** NB: If you define USE_PREAD or USE_PREAD64, then it might also
844** be necessary to define _XOPEN_SOURCE to be 500. This varies from
845** one system to another. Since SQLite does not define USE_PREAD
846** any any form by default, we will not attempt to define _XOPEN_SOURCE.
847** See tickets #2741 and #2681.
drhb912b282006-03-23 22:42:20 +0000848*/
danielk197762079062007-08-15 17:08:46 +0000849static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
drhb912b282006-03-23 22:42:20 +0000850 int got;
drh8ebf6702007-02-06 11:11:08 +0000851 i64 newOffset;
drh15d00c42007-02-27 02:01:14 +0000852 TIMER_START;
drh8350a212007-03-22 15:22:06 +0000853#if defined(USE_PREAD)
danielk197762079062007-08-15 17:08:46 +0000854 got = pread(id->h, pBuf, cnt, offset);
drhbb5f18d2007-04-06 18:23:17 +0000855 SimulateIOError( got = -1 );
drh8350a212007-03-22 15:22:06 +0000856#elif defined(USE_PREAD64)
danielk197762079062007-08-15 17:08:46 +0000857 got = pread64(id->h, pBuf, cnt, offset);
drhbb5f18d2007-04-06 18:23:17 +0000858 SimulateIOError( got = -1 );
drhb912b282006-03-23 22:42:20 +0000859#else
danielk197762079062007-08-15 17:08:46 +0000860 newOffset = lseek(id->h, offset, SEEK_SET);
drhbb5f18d2007-04-06 18:23:17 +0000861 SimulateIOError( newOffset-- );
danielk197762079062007-08-15 17:08:46 +0000862 if( newOffset!=offset ){
drh8ebf6702007-02-06 11:11:08 +0000863 return -1;
864 }
drhb912b282006-03-23 22:42:20 +0000865 got = read(id->h, pBuf, cnt);
866#endif
drh15d00c42007-02-27 02:01:14 +0000867 TIMER_END;
shane9bcbdad2008-05-29 20:22:37 +0000868 OSTRACE5("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
drhb912b282006-03-23 22:42:20 +0000869 return got;
870}
871
872/*
drhbbd42a62004-05-22 17:41:58 +0000873** Read data from a file into a buffer. Return SQLITE_OK if all
874** bytes were read successfully and SQLITE_IOERR if anything goes
875** wrong.
876*/
danielk197762079062007-08-15 17:08:46 +0000877static int unixRead(
878 sqlite3_file *id,
879 void *pBuf,
880 int amt,
881 sqlite3_int64 offset
882){
drhbbd42a62004-05-22 17:41:58 +0000883 int got;
drh9cbe6352005-11-29 03:13:21 +0000884 assert( id );
danielk197762079062007-08-15 17:08:46 +0000885 got = seekAndRead((unixFile*)id, offset, pBuf, amt);
drhbbd42a62004-05-22 17:41:58 +0000886 if( got==amt ){
887 return SQLITE_OK;
drh4ac285a2006-09-15 07:28:50 +0000888 }else if( got<0 ){
889 return SQLITE_IOERR_READ;
drhbbd42a62004-05-22 17:41:58 +0000890 }else{
drhbafda092007-01-03 23:36:22 +0000891 memset(&((char*)pBuf)[got], 0, amt-got);
drh4ac285a2006-09-15 07:28:50 +0000892 return SQLITE_IOERR_SHORT_READ;
drhbbd42a62004-05-22 17:41:58 +0000893 }
894}
895
896/*
drhb912b282006-03-23 22:42:20 +0000897** Seek to the offset in id->offset then read cnt bytes into pBuf.
898** Return the number of bytes actually read. Update the offset.
899*/
danielk197762079062007-08-15 17:08:46 +0000900static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
drhb912b282006-03-23 22:42:20 +0000901 int got;
drh8ebf6702007-02-06 11:11:08 +0000902 i64 newOffset;
drh15d00c42007-02-27 02:01:14 +0000903 TIMER_START;
drh8350a212007-03-22 15:22:06 +0000904#if defined(USE_PREAD)
danielk197762079062007-08-15 17:08:46 +0000905 got = pwrite(id->h, pBuf, cnt, offset);
drh8350a212007-03-22 15:22:06 +0000906#elif defined(USE_PREAD64)
danielk197762079062007-08-15 17:08:46 +0000907 got = pwrite64(id->h, pBuf, cnt, offset);
drhb912b282006-03-23 22:42:20 +0000908#else
danielk197762079062007-08-15 17:08:46 +0000909 newOffset = lseek(id->h, offset, SEEK_SET);
910 if( newOffset!=offset ){
drh8ebf6702007-02-06 11:11:08 +0000911 return -1;
912 }
drhb912b282006-03-23 22:42:20 +0000913 got = write(id->h, pBuf, cnt);
914#endif
drh15d00c42007-02-27 02:01:14 +0000915 TIMER_END;
shane9bcbdad2008-05-29 20:22:37 +0000916 OSTRACE5("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
drhb912b282006-03-23 22:42:20 +0000917 return got;
918}
919
920
921/*
drhbbd42a62004-05-22 17:41:58 +0000922** Write data from a buffer into a file. Return SQLITE_OK on success
923** or some other error code on failure.
924*/
danielk197762079062007-08-15 17:08:46 +0000925static int unixWrite(
926 sqlite3_file *id,
927 const void *pBuf,
928 int amt,
929 sqlite3_int64 offset
930){
drhbbd42a62004-05-22 17:41:58 +0000931 int wrote = 0;
drh9cbe6352005-11-29 03:13:21 +0000932 assert( id );
drh4c7f9412005-02-03 00:29:47 +0000933 assert( amt>0 );
danielk197762079062007-08-15 17:08:46 +0000934 while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){
drhbbd42a62004-05-22 17:41:58 +0000935 amt -= wrote;
danielk197762079062007-08-15 17:08:46 +0000936 offset += wrote;
drhbbd42a62004-05-22 17:41:58 +0000937 pBuf = &((char*)pBuf)[wrote];
938 }
drh59685932006-09-14 13:47:11 +0000939 SimulateIOError(( wrote=(-1), amt=1 ));
940 SimulateDiskfullError(( wrote=0, amt=1 ));
drhbbd42a62004-05-22 17:41:58 +0000941 if( amt>0 ){
drh59685932006-09-14 13:47:11 +0000942 if( wrote<0 ){
drh4ac285a2006-09-15 07:28:50 +0000943 return SQLITE_IOERR_WRITE;
drh59685932006-09-14 13:47:11 +0000944 }else{
945 return SQLITE_FULL;
946 }
drhbbd42a62004-05-22 17:41:58 +0000947 }
948 return SQLITE_OK;
949}
950
drhb851b2c2005-03-10 14:11:12 +0000951#ifdef SQLITE_TEST
952/*
953** Count the number of fullsyncs and normal syncs. This is used to test
954** that syncs and fullsyncs are occuring at the right times.
955*/
956int sqlite3_sync_count = 0;
957int sqlite3_fullsync_count = 0;
958#endif
959
drhf2f23912005-10-05 10:29:36 +0000960/*
961** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.
962** Otherwise use fsync() in its place.
963*/
964#ifndef HAVE_FDATASYNC
965# define fdatasync fsync
966#endif
967
drhac530b12006-02-11 01:25:50 +0000968/*
969** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
970** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
971** only available on Mac OS X. But that could change.
972*/
973#ifdef F_FULLFSYNC
974# define HAVE_FULLFSYNC 1
975#else
976# define HAVE_FULLFSYNC 0
977#endif
978
drhb851b2c2005-03-10 14:11:12 +0000979
drhbbd42a62004-05-22 17:41:58 +0000980/*
drhdd809b02004-07-17 21:44:57 +0000981** The fsync() system call does not work as advertised on many
982** unix systems. The following procedure is an attempt to make
983** it work better.
drh1398ad32005-01-19 23:24:50 +0000984**
985** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
986** for testing when we want to run through the test suite quickly.
987** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
988** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
989** or power failure will likely corrupt the database file.
drhdd809b02004-07-17 21:44:57 +0000990*/
drheb796a72005-09-08 12:38:41 +0000991static int full_fsync(int fd, int fullSync, int dataOnly){
drhdd809b02004-07-17 21:44:57 +0000992 int rc;
drhb851b2c2005-03-10 14:11:12 +0000993
994 /* Record the number of times that we do a normal fsync() and
995 ** FULLSYNC. This is used during testing to verify that this procedure
996 ** gets called with the correct arguments.
997 */
998#ifdef SQLITE_TEST
999 if( fullSync ) sqlite3_fullsync_count++;
1000 sqlite3_sync_count++;
1001#endif
1002
1003 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
1004 ** no-op
1005 */
1006#ifdef SQLITE_NO_SYNC
1007 rc = SQLITE_OK;
1008#else
1009
drhac530b12006-02-11 01:25:50 +00001010#if HAVE_FULLFSYNC
drhb851b2c2005-03-10 14:11:12 +00001011 if( fullSync ){
drhf30cc942005-03-11 17:52:34 +00001012 rc = fcntl(fd, F_FULLFSYNC, 0);
aswiftae0943b2007-01-31 23:37:07 +00001013 }else{
1014 rc = 1;
1015 }
1016 /* If the FULLFSYNC failed, fall back to attempting an fsync().
1017 * It shouldn't be possible for fullfsync to fail on the local
1018 * file system (on OSX), so failure indicates that FULLFSYNC
1019 * isn't supported for this file system. So, attempt an fsync
1020 * and (for now) ignore the overhead of a superfluous fcntl call.
1021 * It'd be better to detect fullfsync support once and avoid
1022 * the fcntl call every time sync is called.
1023 */
1024 if( rc ) rc = fsync(fd);
1025
1026#else
drheb796a72005-09-08 12:38:41 +00001027 if( dataOnly ){
1028 rc = fdatasync(fd);
drhf2f23912005-10-05 10:29:36 +00001029 }else{
drheb796a72005-09-08 12:38:41 +00001030 rc = fsync(fd);
1031 }
aswiftae0943b2007-01-31 23:37:07 +00001032#endif /* HAVE_FULLFSYNC */
drhb851b2c2005-03-10 14:11:12 +00001033#endif /* defined(SQLITE_NO_SYNC) */
1034
drhdd809b02004-07-17 21:44:57 +00001035 return rc;
1036}
1037
1038/*
drhbbd42a62004-05-22 17:41:58 +00001039** Make sure all writes to a particular file are committed to disk.
1040**
drheb796a72005-09-08 12:38:41 +00001041** If dataOnly==0 then both the file itself and its metadata (file
1042** size, access time, etc) are synced. If dataOnly!=0 then only the
1043** file data is synced.
1044**
drhbbd42a62004-05-22 17:41:58 +00001045** Under Unix, also make sure that the directory entry for the file
1046** has been created by fsync-ing the directory that contains the file.
1047** If we do not do this and we encounter a power failure, the directory
1048** entry for the journal might not exist after we reboot. The next
1049** SQLite to access the file will not know that the journal exists (because
1050** the directory entry for the journal was never created) and the transaction
1051** will not roll back - possibly leading to database corruption.
1052*/
danielk197790949c22007-08-17 16:50:38 +00001053static int unixSync(sqlite3_file *id, int flags){
drh59685932006-09-14 13:47:11 +00001054 int rc;
drh054889e2005-11-30 03:20:31 +00001055 unixFile *pFile = (unixFile*)id;
danielk197790949c22007-08-17 16:50:38 +00001056
danielk1977f036aef2007-08-20 05:36:51 +00001057 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
1058 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
1059
danielk1977c16d4632007-08-30 14:49:58 +00001060 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
danielk1977f036aef2007-08-20 05:36:51 +00001061 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
1062 || (flags&0x0F)==SQLITE_SYNC_FULL
danielk1977f036aef2007-08-20 05:36:51 +00001063 );
danielk197790949c22007-08-17 16:50:38 +00001064
danielk1977cd3b3c82008-09-22 11:46:32 +00001065 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
1066 ** line is to test that doing so does not cause any problems.
1067 */
1068 SimulateDiskfullError( return SQLITE_FULL );
1069
drh054889e2005-11-30 03:20:31 +00001070 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001071 OSTRACE2("SYNC %-3d\n", pFile->h);
danielk197790949c22007-08-17 16:50:38 +00001072 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
drh59685932006-09-14 13:47:11 +00001073 SimulateIOError( rc=1 );
1074 if( rc ){
drh4ac285a2006-09-15 07:28:50 +00001075 return SQLITE_IOERR_FSYNC;
drhbbd42a62004-05-22 17:41:58 +00001076 }
drh054889e2005-11-30 03:20:31 +00001077 if( pFile->dirfd>=0 ){
drh4f0c5872007-03-26 22:05:01 +00001078 OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
danielk197790949c22007-08-17 16:50:38 +00001079 HAVE_FULLFSYNC, isFullsync);
danielk1977d7c03f72005-11-25 10:38:22 +00001080#ifndef SQLITE_DISABLE_DIRSYNC
drhac530b12006-02-11 01:25:50 +00001081 /* The directory sync is only attempted if full_fsync is
1082 ** turned off or unavailable. If a full_fsync occurred above,
1083 ** then the directory sync is superfluous.
1084 */
danielk197790949c22007-08-17 16:50:38 +00001085 if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
drhac530b12006-02-11 01:25:50 +00001086 /*
1087 ** We have received multiple reports of fsync() returning
drh86631a52006-02-09 23:05:51 +00001088 ** errors when applied to directories on certain file systems.
1089 ** A failed directory sync is not a big deal. So it seems
1090 ** better to ignore the error. Ticket #1657
1091 */
1092 /* return SQLITE_IOERR; */
danielk19770964b232005-11-25 08:47:57 +00001093 }
danielk1977d7c03f72005-11-25 10:38:22 +00001094#endif
drh054889e2005-11-30 03:20:31 +00001095 close(pFile->dirfd); /* Only need to sync once, so close the directory */
1096 pFile->dirfd = -1; /* when we are done. */
drha2854222004-06-17 19:04:17 +00001097 }
drha2854222004-06-17 19:04:17 +00001098 return SQLITE_OK;
drhbbd42a62004-05-22 17:41:58 +00001099}
1100
1101/*
1102** Truncate an open file to a specified size
1103*/
danielk197762079062007-08-15 17:08:46 +00001104static int unixTruncate(sqlite3_file *id, i64 nByte){
drh59685932006-09-14 13:47:11 +00001105 int rc;
drh9cbe6352005-11-29 03:13:21 +00001106 assert( id );
drh93aed5a2008-01-16 17:46:38 +00001107 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
drh63fff5f2007-06-19 10:50:38 +00001108 rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);
drh59685932006-09-14 13:47:11 +00001109 if( rc ){
drh4ac285a2006-09-15 07:28:50 +00001110 return SQLITE_IOERR_TRUNCATE;
drh59685932006-09-14 13:47:11 +00001111 }else{
1112 return SQLITE_OK;
1113 }
drhbbd42a62004-05-22 17:41:58 +00001114}
1115
1116/*
1117** Determine the current size of a file in bytes
1118*/
danielk197762079062007-08-15 17:08:46 +00001119static int unixFileSize(sqlite3_file *id, i64 *pSize){
drh59685932006-09-14 13:47:11 +00001120 int rc;
drhbbd42a62004-05-22 17:41:58 +00001121 struct stat buf;
drh9cbe6352005-11-29 03:13:21 +00001122 assert( id );
drh59685932006-09-14 13:47:11 +00001123 rc = fstat(((unixFile*)id)->h, &buf);
1124 SimulateIOError( rc=1 );
1125 if( rc!=0 ){
drh4ac285a2006-09-15 07:28:50 +00001126 return SQLITE_IOERR_FSTAT;
drhbbd42a62004-05-22 17:41:58 +00001127 }
1128 *pSize = buf.st_size;
drh54626242008-07-30 17:28:04 +00001129
1130 /* When opening a zero-size database, the findLockInfo() procedure
1131 ** writes a single byte into that file in order to work around a bug
1132 ** in the OS-X msdos filesystem. In order to avoid problems with upper
1133 ** layers, we need to report this file size as zero even though it is
1134 ** really 1. Ticket #3260.
1135 */
1136 if( *pSize==1 ) *pSize = 0;
1137
1138
drhbbd42a62004-05-22 17:41:58 +00001139 return SQLITE_OK;
1140}
1141
danielk19779a1d0ab2004-06-01 14:09:28 +00001142/*
aswift5b1a2562008-08-22 00:22:35 +00001143** This routine translates a standard POSIX errno code into something
1144** useful to the clients of the sqlite3 functions. Specifically, it is
1145** intended to translate a variety of "try again" errors into SQLITE_BUSY
1146** and a variety of "please close the file descriptor NOW" errors into
1147** SQLITE_IOERR
1148**
1149** Errors during initialization of locks, or file system support for locks,
1150** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
1151*/
1152static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
1153 switch (posixError) {
1154 case 0:
1155 return SQLITE_OK;
1156
1157 case EAGAIN:
1158 case ETIMEDOUT:
1159 case EBUSY:
1160 case EINTR:
1161 case ENOLCK:
1162 /* random NFS retry error, unless during file system support
1163 * introspection, in which it actually means what it says */
1164 return SQLITE_BUSY;
1165
1166 case EACCES:
1167 /* EACCES is like EAGAIN during locking operations, but not any other time*/
1168 if( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
1169 (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
1170 (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
1171 (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
1172 return SQLITE_BUSY;
1173 }
1174 /* else fall through */
1175 case EPERM:
1176 return SQLITE_PERM;
1177
1178 case EDEADLK:
1179 return SQLITE_IOERR_BLOCKED;
1180
drhf489c452008-08-22 00:47:53 +00001181#if EOPNOTSUPP!=ENOTSUP
aswift5b1a2562008-08-22 00:22:35 +00001182 case EOPNOTSUPP:
1183 /* something went terribly awry, unless during file system support
1184 * introspection, in which it actually means what it says */
drhf489c452008-08-22 00:47:53 +00001185#endif
danielk19775ad6a882008-09-15 04:20:31 +00001186#ifdef ENOTSUP
aswift5b1a2562008-08-22 00:22:35 +00001187 case ENOTSUP:
1188 /* invalid fd, unless during file system support introspection, in which
1189 * it actually means what it says */
danielk19775ad6a882008-09-15 04:20:31 +00001190#endif
aswift5b1a2562008-08-22 00:22:35 +00001191 case EIO:
1192 case EBADF:
1193 case EINVAL:
1194 case ENOTCONN:
1195 case ENODEV:
1196 case ENXIO:
1197 case ENOENT:
1198 case ESTALE:
1199 case ENOSYS:
1200 /* these should force the client to close the file and reconnect */
1201
1202 default:
1203 return sqliteIOErr;
1204 }
1205}
1206
1207/*
danielk197713adf8a2004-06-03 16:08:41 +00001208** This routine checks if there is a RESERVED lock held on the specified
aswift5b1a2562008-08-22 00:22:35 +00001209** file by this or any other process. If such a lock is held, set *pResOut
1210** to a non-zero value otherwise *pResOut is set to zero. The return value
1211** is set to SQLITE_OK unless an I/O error occurs during lock checking.
danielk197713adf8a2004-06-03 16:08:41 +00001212*/
danielk1977861f7452008-06-05 11:39:11 +00001213static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00001214 int rc = SQLITE_OK;
1215 int reserved = 0;
drh054889e2005-11-30 03:20:31 +00001216 unixFile *pFile = (unixFile*)id;
danielk197713adf8a2004-06-03 16:08:41 +00001217
danielk1977861f7452008-06-05 11:39:11 +00001218 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
1219
drh054889e2005-11-30 03:20:31 +00001220 assert( pFile );
danielk1977b4b47412007-08-17 15:53:36 +00001221 enterMutex(); /* Because pFile->pLock is shared across threads */
danielk197713adf8a2004-06-03 16:08:41 +00001222
1223 /* Check if a thread in this process holds such a lock */
drh054889e2005-11-30 03:20:31 +00001224 if( pFile->pLock->locktype>SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001225 reserved = 1;
danielk197713adf8a2004-06-03 16:08:41 +00001226 }
1227
drh2ac3ee92004-06-07 16:27:46 +00001228 /* Otherwise see if some other process holds it.
danielk197713adf8a2004-06-03 16:08:41 +00001229 */
aswift5b1a2562008-08-22 00:22:35 +00001230 if( !reserved ){
danielk197713adf8a2004-06-03 16:08:41 +00001231 struct flock lock;
1232 lock.l_whence = SEEK_SET;
drh2ac3ee92004-06-07 16:27:46 +00001233 lock.l_start = RESERVED_BYTE;
1234 lock.l_len = 1;
1235 lock.l_type = F_WRLCK;
aswift5b1a2562008-08-22 00:22:35 +00001236 if (-1 == fcntl(pFile->h, F_GETLK, &lock)) {
1237 int tErrno = errno;
1238 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
1239 pFile->lastErrno = tErrno;
1240 } else if( lock.l_type!=F_UNLCK ){
1241 reserved = 1;
danielk197713adf8a2004-06-03 16:08:41 +00001242 }
1243 }
1244
danielk1977b4b47412007-08-17 15:53:36 +00001245 leaveMutex();
aswift5b1a2562008-08-22 00:22:35 +00001246 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
danielk197713adf8a2004-06-03 16:08:41 +00001247
aswift5b1a2562008-08-22 00:22:35 +00001248 *pResOut = reserved;
1249 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001250}
1251
1252/*
danielk19779a1d0ab2004-06-01 14:09:28 +00001253** Lock the file with the lock specified by parameter locktype - one
1254** of the following:
1255**
drh2ac3ee92004-06-07 16:27:46 +00001256** (1) SHARED_LOCK
1257** (2) RESERVED_LOCK
1258** (3) PENDING_LOCK
1259** (4) EXCLUSIVE_LOCK
1260**
drhb3e04342004-06-08 00:47:47 +00001261** Sometimes when requesting one lock state, additional lock states
1262** are inserted in between. The locking might fail on one of the later
1263** transitions leaving the lock state different from what it started but
1264** still short of its goal. The following chart shows the allowed
1265** transitions and the inserted intermediate states:
1266**
1267** UNLOCKED -> SHARED
1268** SHARED -> RESERVED
1269** SHARED -> (PENDING) -> EXCLUSIVE
1270** RESERVED -> (PENDING) -> EXCLUSIVE
1271** PENDING -> EXCLUSIVE
drh2ac3ee92004-06-07 16:27:46 +00001272**
drha6abd042004-06-09 17:37:22 +00001273** This routine will only increase a lock. Use the sqlite3OsUnlock()
1274** routine to lower a locking level.
danielk19779a1d0ab2004-06-01 14:09:28 +00001275*/
danielk197762079062007-08-15 17:08:46 +00001276static int unixLock(sqlite3_file *id, int locktype){
danielk1977f42f25c2004-06-25 07:21:28 +00001277 /* The following describes the implementation of the various locks and
1278 ** lock transitions in terms of the POSIX advisory shared and exclusive
1279 ** lock primitives (called read-locks and write-locks below, to avoid
1280 ** confusion with SQLite lock names). The algorithms are complicated
1281 ** slightly in order to be compatible with windows systems simultaneously
1282 ** accessing the same database file, in case that is ever required.
1283 **
1284 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
1285 ** byte', each single bytes at well known offsets, and the 'shared byte
1286 ** range', a range of 510 bytes at a well known offset.
1287 **
1288 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
1289 ** byte'. If this is successful, a random byte from the 'shared byte
1290 ** range' is read-locked and the lock on the 'pending byte' released.
1291 **
danielk197790ba3bd2004-06-25 08:32:25 +00001292 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
1293 ** A RESERVED lock is implemented by grabbing a write-lock on the
1294 ** 'reserved byte'.
danielk1977f42f25c2004-06-25 07:21:28 +00001295 **
1296 ** A process may only obtain a PENDING lock after it has obtained a
danielk197790ba3bd2004-06-25 08:32:25 +00001297 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
1298 ** on the 'pending byte'. This ensures that no new SHARED locks can be
1299 ** obtained, but existing SHARED locks are allowed to persist. A process
1300 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
1301 ** This property is used by the algorithm for rolling back a journal file
1302 ** after a crash.
danielk1977f42f25c2004-06-25 07:21:28 +00001303 **
danielk197790ba3bd2004-06-25 08:32:25 +00001304 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
1305 ** implemented by obtaining a write-lock on the entire 'shared byte
1306 ** range'. Since all other locks require a read-lock on one of the bytes
1307 ** within this range, this ensures that no other locks are held on the
1308 ** database.
danielk1977f42f25c2004-06-25 07:21:28 +00001309 **
1310 ** The reason a single byte cannot be used instead of the 'shared byte
1311 ** range' is that some versions of windows do not support read-locks. By
1312 ** locking a random byte from a range, concurrent SHARED locks may exist
1313 ** even if the locking primitive used is always a write-lock.
1314 */
danielk19779a1d0ab2004-06-01 14:09:28 +00001315 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001316 unixFile *pFile = (unixFile*)id;
1317 struct lockInfo *pLock = pFile->pLock;
danielk19779a1d0ab2004-06-01 14:09:28 +00001318 struct flock lock;
1319 int s;
1320
drh054889e2005-11-30 03:20:31 +00001321 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001322 OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001323 locktypeName(locktype), locktypeName(pFile->locktype),
1324 locktypeName(pLock->locktype), pLock->cnt , getpid());
danielk19779a1d0ab2004-06-01 14:09:28 +00001325
1326 /* If there is already a lock of this type or more restrictive on the
danielk1977ad94b582007-08-20 06:44:22 +00001327 ** unixFile, do nothing. Don't use the end_lock: exit path, as
danielk1977b4b47412007-08-17 15:53:36 +00001328 ** enterMutex() hasn't been called yet.
danielk19779a1d0ab2004-06-01 14:09:28 +00001329 */
drh054889e2005-11-30 03:20:31 +00001330 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00001331 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drh054889e2005-11-30 03:20:31 +00001332 locktypeName(locktype));
danielk19779a1d0ab2004-06-01 14:09:28 +00001333 return SQLITE_OK;
1334 }
1335
drhb3e04342004-06-08 00:47:47 +00001336 /* Make sure the locking sequence is correct
drh2ac3ee92004-06-07 16:27:46 +00001337 */
drh054889e2005-11-30 03:20:31 +00001338 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
drhb3e04342004-06-08 00:47:47 +00001339 assert( locktype!=PENDING_LOCK );
drh054889e2005-11-30 03:20:31 +00001340 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
drh2ac3ee92004-06-07 16:27:46 +00001341
drh054889e2005-11-30 03:20:31 +00001342 /* This mutex is needed because pFile->pLock is shared across threads
drhb3e04342004-06-08 00:47:47 +00001343 */
danielk1977b4b47412007-08-17 15:53:36 +00001344 enterMutex();
danielk19779a1d0ab2004-06-01 14:09:28 +00001345
drh029b44b2006-01-15 00:13:15 +00001346 /* Make sure the current thread owns the pFile.
1347 */
1348 rc = transferOwnership(pFile);
1349 if( rc!=SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00001350 leaveMutex();
drh029b44b2006-01-15 00:13:15 +00001351 return rc;
1352 }
drh64b1bea2006-01-15 02:30:57 +00001353 pLock = pFile->pLock;
drh029b44b2006-01-15 00:13:15 +00001354
danielk1977ad94b582007-08-20 06:44:22 +00001355 /* If some thread using this PID has a lock via a different unixFile*
danielk19779a1d0ab2004-06-01 14:09:28 +00001356 ** handle that precludes the requested lock, return BUSY.
1357 */
drh054889e2005-11-30 03:20:31 +00001358 if( (pFile->locktype!=pLock->locktype &&
drh2ac3ee92004-06-07 16:27:46 +00001359 (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
danielk19779a1d0ab2004-06-01 14:09:28 +00001360 ){
1361 rc = SQLITE_BUSY;
1362 goto end_lock;
1363 }
1364
1365 /* If a SHARED lock is requested, and some thread using this PID already
1366 ** has a SHARED or RESERVED lock, then increment reference counts and
1367 ** return SQLITE_OK.
1368 */
1369 if( locktype==SHARED_LOCK &&
1370 (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
1371 assert( locktype==SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001372 assert( pFile->locktype==0 );
danielk1977ecb2a962004-06-02 06:30:16 +00001373 assert( pLock->cnt>0 );
drh054889e2005-11-30 03:20:31 +00001374 pFile->locktype = SHARED_LOCK;
danielk19779a1d0ab2004-06-01 14:09:28 +00001375 pLock->cnt++;
drh054889e2005-11-30 03:20:31 +00001376 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001377 goto end_lock;
1378 }
1379
danielk197713adf8a2004-06-03 16:08:41 +00001380 lock.l_len = 1L;
drh2b4b5962005-06-15 17:47:55 +00001381
danielk19779a1d0ab2004-06-01 14:09:28 +00001382 lock.l_whence = SEEK_SET;
1383
drh3cde3bb2004-06-12 02:17:14 +00001384 /* A PENDING lock is needed before acquiring a SHARED lock and before
1385 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1386 ** be released.
danielk19779a1d0ab2004-06-01 14:09:28 +00001387 */
drh3cde3bb2004-06-12 02:17:14 +00001388 if( locktype==SHARED_LOCK
drh054889e2005-11-30 03:20:31 +00001389 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh3cde3bb2004-06-12 02:17:14 +00001390 ){
danielk1977489468c2004-06-28 08:25:47 +00001391 lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
drh2ac3ee92004-06-07 16:27:46 +00001392 lock.l_start = PENDING_BYTE;
drh054889e2005-11-30 03:20:31 +00001393 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001394 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001395 int tErrno = errno;
1396 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1397 if( IS_LOCK_ERROR(rc) ){
1398 pFile->lastErrno = tErrno;
1399 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001400 goto end_lock;
1401 }
drh3cde3bb2004-06-12 02:17:14 +00001402 }
1403
1404
1405 /* If control gets to this point, then actually go ahead and make
1406 ** operating system calls for the specified lock.
1407 */
1408 if( locktype==SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001409 int tErrno = 0;
drh3cde3bb2004-06-12 02:17:14 +00001410 assert( pLock->cnt==0 );
1411 assert( pLock->locktype==0 );
danielk19779a1d0ab2004-06-01 14:09:28 +00001412
drh2ac3ee92004-06-07 16:27:46 +00001413 /* Now get the read-lock */
1414 lock.l_start = SHARED_FIRST;
1415 lock.l_len = SHARED_SIZE;
aswift5b1a2562008-08-22 00:22:35 +00001416 if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){
1417 tErrno = errno;
1418 }
drh2ac3ee92004-06-07 16:27:46 +00001419 /* Drop the temporary PENDING lock */
1420 lock.l_start = PENDING_BYTE;
1421 lock.l_len = 1L;
danielk19779a1d0ab2004-06-01 14:09:28 +00001422 lock.l_type = F_UNLCK;
drh054889e2005-11-30 03:20:31 +00001423 if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
aswift5b1a2562008-08-22 00:22:35 +00001424 if( s != -1 ){
1425 /* This could happen with a network mount */
1426 tErrno = errno;
1427 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1428 if( IS_LOCK_ERROR(rc) ){
1429 pFile->lastErrno = tErrno;
1430 }
1431 goto end_lock;
1432 }
drh2b4b5962005-06-15 17:47:55 +00001433 }
drhe2396a12007-03-29 20:19:58 +00001434 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001435 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1436 if( IS_LOCK_ERROR(rc) ){
1437 pFile->lastErrno = tErrno;
1438 }
drhbbd42a62004-05-22 17:41:58 +00001439 }else{
drh054889e2005-11-30 03:20:31 +00001440 pFile->locktype = SHARED_LOCK;
1441 pFile->pOpen->nLock++;
danielk19779a1d0ab2004-06-01 14:09:28 +00001442 pLock->cnt = 1;
drhbbd42a62004-05-22 17:41:58 +00001443 }
drh3cde3bb2004-06-12 02:17:14 +00001444 }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
1445 /* We are trying for an exclusive lock but another thread in this
1446 ** same process is still holding a shared lock. */
1447 rc = SQLITE_BUSY;
drhbbd42a62004-05-22 17:41:58 +00001448 }else{
drh3cde3bb2004-06-12 02:17:14 +00001449 /* The request was for a RESERVED or EXCLUSIVE lock. It is
danielk19779a1d0ab2004-06-01 14:09:28 +00001450 ** assumed that there is a SHARED or greater lock on the file
1451 ** already.
1452 */
drh054889e2005-11-30 03:20:31 +00001453 assert( 0!=pFile->locktype );
danielk19779a1d0ab2004-06-01 14:09:28 +00001454 lock.l_type = F_WRLCK;
1455 switch( locktype ){
1456 case RESERVED_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001457 lock.l_start = RESERVED_BYTE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001458 break;
danielk19779a1d0ab2004-06-01 14:09:28 +00001459 case EXCLUSIVE_LOCK:
drh2ac3ee92004-06-07 16:27:46 +00001460 lock.l_start = SHARED_FIRST;
1461 lock.l_len = SHARED_SIZE;
danielk19779a1d0ab2004-06-01 14:09:28 +00001462 break;
1463 default:
1464 assert(0);
1465 }
drh054889e2005-11-30 03:20:31 +00001466 s = fcntl(pFile->h, F_SETLK, &lock);
drhe2396a12007-03-29 20:19:58 +00001467 if( s==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001468 int tErrno = errno;
1469 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1470 if( IS_LOCK_ERROR(rc) ){
1471 pFile->lastErrno = tErrno;
1472 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001473 }
drhbbd42a62004-05-22 17:41:58 +00001474 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001475
danielk1977ecb2a962004-06-02 06:30:16 +00001476 if( rc==SQLITE_OK ){
drh054889e2005-11-30 03:20:31 +00001477 pFile->locktype = locktype;
danielk1977ecb2a962004-06-02 06:30:16 +00001478 pLock->locktype = locktype;
drh3cde3bb2004-06-12 02:17:14 +00001479 }else if( locktype==EXCLUSIVE_LOCK ){
drh054889e2005-11-30 03:20:31 +00001480 pFile->locktype = PENDING_LOCK;
drh3cde3bb2004-06-12 02:17:14 +00001481 pLock->locktype = PENDING_LOCK;
danielk1977ecb2a962004-06-02 06:30:16 +00001482 }
danielk19779a1d0ab2004-06-01 14:09:28 +00001483
1484end_lock:
danielk1977b4b47412007-08-17 15:53:36 +00001485 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001486 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
danielk19772b444852004-06-29 07:45:33 +00001487 rc==SQLITE_OK ? "ok" : "failed");
drhbbd42a62004-05-22 17:41:58 +00001488 return rc;
1489}
1490
1491/*
drh054889e2005-11-30 03:20:31 +00001492** Lower the locking level on file descriptor pFile to locktype. locktype
drha6abd042004-06-09 17:37:22 +00001493** must be either NO_LOCK or SHARED_LOCK.
1494**
1495** If the locking level of the file descriptor is already at or below
1496** the requested locking level, this routine is a no-op.
drhbbd42a62004-05-22 17:41:58 +00001497*/
danielk197762079062007-08-15 17:08:46 +00001498static int unixUnlock(sqlite3_file *id, int locktype){
drha6abd042004-06-09 17:37:22 +00001499 struct lockInfo *pLock;
1500 struct flock lock;
drh9c105bb2004-10-02 20:38:28 +00001501 int rc = SQLITE_OK;
drh054889e2005-11-30 03:20:31 +00001502 unixFile *pFile = (unixFile*)id;
drh1aa5af12008-03-07 19:51:14 +00001503 int h;
drha6abd042004-06-09 17:37:22 +00001504
drh054889e2005-11-30 03:20:31 +00001505 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001506 OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
drh054889e2005-11-30 03:20:31 +00001507 pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
drha6abd042004-06-09 17:37:22 +00001508
1509 assert( locktype<=SHARED_LOCK );
drh054889e2005-11-30 03:20:31 +00001510 if( pFile->locktype<=locktype ){
drha6abd042004-06-09 17:37:22 +00001511 return SQLITE_OK;
1512 }
drhf1a221e2006-01-15 17:27:17 +00001513 if( CHECK_THREADID(pFile) ){
1514 return SQLITE_MISUSE;
1515 }
danielk1977b4b47412007-08-17 15:53:36 +00001516 enterMutex();
drh1aa5af12008-03-07 19:51:14 +00001517 h = pFile->h;
drh054889e2005-11-30 03:20:31 +00001518 pLock = pFile->pLock;
drha6abd042004-06-09 17:37:22 +00001519 assert( pLock->cnt!=0 );
drh054889e2005-11-30 03:20:31 +00001520 if( pFile->locktype>SHARED_LOCK ){
1521 assert( pLock->locktype==pFile->locktype );
drh1aa5af12008-03-07 19:51:14 +00001522 SimulateIOErrorBenign(1);
1523 SimulateIOError( h=(-1) )
1524 SimulateIOErrorBenign(0);
drh9c105bb2004-10-02 20:38:28 +00001525 if( locktype==SHARED_LOCK ){
1526 lock.l_type = F_RDLCK;
1527 lock.l_whence = SEEK_SET;
1528 lock.l_start = SHARED_FIRST;
1529 lock.l_len = SHARED_SIZE;
drh1aa5af12008-03-07 19:51:14 +00001530 if( fcntl(h, F_SETLK, &lock)==(-1) ){
aswift5b1a2562008-08-22 00:22:35 +00001531 int tErrno = errno;
1532 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
1533 if( IS_LOCK_ERROR(rc) ){
1534 pFile->lastErrno = tErrno;
1535 }
1536 goto end_unlock;
drh9c105bb2004-10-02 20:38:28 +00001537 }
1538 }
drhbbd42a62004-05-22 17:41:58 +00001539 lock.l_type = F_UNLCK;
1540 lock.l_whence = SEEK_SET;
drha6abd042004-06-09 17:37:22 +00001541 lock.l_start = PENDING_BYTE;
1542 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
drh1aa5af12008-03-07 19:51:14 +00001543 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001544 pLock->locktype = SHARED_LOCK;
1545 }else{
aswift5b1a2562008-08-22 00:22:35 +00001546 int tErrno = errno;
1547 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
1548 if( IS_LOCK_ERROR(rc) ){
1549 pFile->lastErrno = tErrno;
1550 }
1551 goto end_unlock;
drh2b4b5962005-06-15 17:47:55 +00001552 }
drhbbd42a62004-05-22 17:41:58 +00001553 }
drha6abd042004-06-09 17:37:22 +00001554 if( locktype==NO_LOCK ){
1555 struct openCnt *pOpen;
danielk1977ecb2a962004-06-02 06:30:16 +00001556
drha6abd042004-06-09 17:37:22 +00001557 /* Decrement the shared lock counter. Release the lock using an
1558 ** OS call only when all threads in this same process have released
1559 ** the lock.
1560 */
1561 pLock->cnt--;
1562 if( pLock->cnt==0 ){
1563 lock.l_type = F_UNLCK;
1564 lock.l_whence = SEEK_SET;
1565 lock.l_start = lock.l_len = 0L;
drh1aa5af12008-03-07 19:51:14 +00001566 SimulateIOErrorBenign(1);
1567 SimulateIOError( h=(-1) )
1568 SimulateIOErrorBenign(0);
1569 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
drh2b4b5962005-06-15 17:47:55 +00001570 pLock->locktype = NO_LOCK;
1571 }else{
aswift5b1a2562008-08-22 00:22:35 +00001572 int tErrno = errno;
danielk19775ad6a882008-09-15 04:20:31 +00001573 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
aswift5b1a2562008-08-22 00:22:35 +00001574 if( IS_LOCK_ERROR(rc) ){
1575 pFile->lastErrno = tErrno;
1576 }
drh1aa5af12008-03-07 19:51:14 +00001577 pLock->cnt = 1;
aswift5b1a2562008-08-22 00:22:35 +00001578 goto end_unlock;
drh2b4b5962005-06-15 17:47:55 +00001579 }
drha6abd042004-06-09 17:37:22 +00001580 }
1581
drhbbd42a62004-05-22 17:41:58 +00001582 /* Decrement the count of locks against this same file. When the
1583 ** count reaches zero, close any other file descriptors whose close
1584 ** was deferred because of outstanding locks.
1585 */
drh1aa5af12008-03-07 19:51:14 +00001586 if( rc==SQLITE_OK ){
1587 pOpen = pFile->pOpen;
1588 pOpen->nLock--;
1589 assert( pOpen->nLock>=0 );
1590 if( pOpen->nLock==0 && pOpen->nPending>0 ){
1591 int i;
1592 for(i=0; i<pOpen->nPending; i++){
1593 close(pOpen->aPending[i]);
1594 }
drhda0e7682008-07-30 15:27:54 +00001595 sqlite3_free(pOpen->aPending);
drh1aa5af12008-03-07 19:51:14 +00001596 pOpen->nPending = 0;
1597 pOpen->aPending = 0;
drhbbd42a62004-05-22 17:41:58 +00001598 }
drhbbd42a62004-05-22 17:41:58 +00001599 }
1600 }
aswift5b1a2562008-08-22 00:22:35 +00001601
1602end_unlock:
danielk1977b4b47412007-08-17 15:53:36 +00001603 leaveMutex();
drh1aa5af12008-03-07 19:51:14 +00001604 if( rc==SQLITE_OK ) pFile->locktype = locktype;
drh9c105bb2004-10-02 20:38:28 +00001605 return rc;
drhbbd42a62004-05-22 17:41:58 +00001606}
1607
1608/*
danielk1977e339d652008-06-28 11:23:00 +00001609** This function performs the parts of the "close file" operation
1610** common to all locking schemes. It closes the directory and file
1611** handles, if they are valid, and sets all fields of the unixFile
1612** structure to 0.
1613*/
1614static int closeUnixFile(sqlite3_file *id){
1615 unixFile *pFile = (unixFile*)id;
1616 if( pFile ){
1617 if( pFile->dirfd>=0 ){
1618 close(pFile->dirfd);
1619 }
1620 if( pFile->h>=0 ){
1621 close(pFile->h);
1622 }
1623 OSTRACE2("CLOSE %-3d\n", pFile->h);
1624 OpenCounter(-1);
1625 memset(pFile, 0, sizeof(unixFile));
1626 }
1627 return SQLITE_OK;
1628}
1629
1630/*
danielk1977e3026632004-06-22 11:29:02 +00001631** Close a file.
1632*/
danielk197762079062007-08-15 17:08:46 +00001633static int unixClose(sqlite3_file *id){
danielk1977e339d652008-06-28 11:23:00 +00001634 if( id ){
1635 unixFile *pFile = (unixFile *)id;
1636 unixUnlock(id, NO_LOCK);
1637 enterMutex();
danielk19776cb427f2008-06-30 10:16:04 +00001638 if( pFile->pOpen && pFile->pOpen->nLock ){
danielk1977e339d652008-06-28 11:23:00 +00001639 /* If there are outstanding locks, do not actually close the file just
1640 ** yet because that would clear those locks. Instead, add the file
1641 ** descriptor to pOpen->aPending. It will be automatically closed when
1642 ** the last lock is cleared.
1643 */
1644 int *aNew;
1645 struct openCnt *pOpen = pFile->pOpen;
drhda0e7682008-07-30 15:27:54 +00001646 aNew = sqlite3_realloc(pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
danielk1977e339d652008-06-28 11:23:00 +00001647 if( aNew==0 ){
1648 /* If a malloc fails, just leak the file descriptor */
1649 }else{
1650 pOpen->aPending = aNew;
1651 pOpen->aPending[pOpen->nPending] = pFile->h;
1652 pOpen->nPending++;
1653 pFile->h = -1;
1654 }
danielk1977e3026632004-06-22 11:29:02 +00001655 }
danielk1977e339d652008-06-28 11:23:00 +00001656 releaseLockInfo(pFile->pLock);
1657 releaseOpenCnt(pFile->pOpen);
1658 closeUnixFile(id);
1659 leaveMutex();
danielk1977e3026632004-06-22 11:29:02 +00001660 }
drh02afc862006-01-20 18:10:57 +00001661 return SQLITE_OK;
danielk1977e3026632004-06-22 11:29:02 +00001662}
1663
drhbfe66312006-10-03 17:40:40 +00001664
1665#ifdef SQLITE_ENABLE_LOCKING_STYLE
1666#pragma mark AFP Support
1667
1668/*
1669 ** The afpLockingContext structure contains all afp lock specific state
1670 */
1671typedef struct afpLockingContext afpLockingContext;
1672struct afpLockingContext {
drh1aa5af12008-03-07 19:51:14 +00001673 unsigned long long sharedLockByte;
drh308aa322008-03-07 20:14:38 +00001674 const char *filePath;
drhbfe66312006-10-03 17:40:40 +00001675};
1676
1677struct ByteRangeLockPB2
1678{
1679 unsigned long long offset; /* offset to first byte to lock */
1680 unsigned long long length; /* nbr of bytes to lock */
1681 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
1682 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
1683 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
1684 int fd; /* file desc to assoc this lock with */
1685};
1686
drhfd131da2007-08-07 17:13:03 +00001687#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
drhbfe66312006-10-03 17:40:40 +00001688
danielk1977ad94b582007-08-20 06:44:22 +00001689/*
aswift5b1a2562008-08-22 00:22:35 +00001690 ** Return SQLITE_OK on success, SQLITE_BUSY on failure.
1691 */
danielk1977ad94b582007-08-20 06:44:22 +00001692static int _AFPFSSetLock(
1693 const char *path,
aswift5b1a2562008-08-22 00:22:35 +00001694 unixFile *pFile,
danielk1977ad94b582007-08-20 06:44:22 +00001695 unsigned long long offset,
1696 unsigned long long length,
1697 int setLockFlag
1698){
drhfd131da2007-08-07 17:13:03 +00001699 struct ByteRangeLockPB2 pb;
drhbfe66312006-10-03 17:40:40 +00001700 int err;
1701
1702 pb.unLockFlag = setLockFlag ? 0 : 1;
1703 pb.startEndFlag = 0;
1704 pb.offset = offset;
1705 pb.length = length;
aswift5b1a2562008-08-22 00:22:35 +00001706 pb.fd = pFile->h;
drh4f0c5872007-03-26 22:05:01 +00001707 OSTRACE5("AFPLOCK setting lock %s for %d in range %llx:%llx\n",
aswift5b1a2562008-08-22 00:22:35 +00001708 (setLockFlag?"ON":"OFF"), pFile->h, offset, length);
drhbfe66312006-10-03 17:40:40 +00001709 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
1710 if ( err==-1 ) {
aswift5b1a2562008-08-22 00:22:35 +00001711 int rc;
1712 int tErrno = errno;
1713 OSTRACE4("AFPLOCK failed to fsctl() '%s' %d %s\n", path, tErrno, strerror(tErrno));
1714 rc = sqliteErrorFromPosixError(tErrno, setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); /* error */
1715 if( IS_LOCK_ERROR(rc) ){
1716 pFile->lastErrno = tErrno;
1717 }
1718 return rc;
drhbfe66312006-10-03 17:40:40 +00001719 } else {
aswift5b1a2562008-08-22 00:22:35 +00001720 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00001721 }
1722}
1723
aswift5b1a2562008-08-22 00:22:35 +00001724/* AFP-style reserved lock checking following the behavior of
1725** unixCheckReservedLock, see the unixCheckReservedLock function comments */
danielk1977e339d652008-06-28 11:23:00 +00001726static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00001727 int rc = SQLITE_OK;
1728 int reserved = 0;
drhbfe66312006-10-03 17:40:40 +00001729 unixFile *pFile = (unixFile*)id;
1730
aswift5b1a2562008-08-22 00:22:35 +00001731 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
1732
1733 assert( pFile );
drhbfe66312006-10-03 17:40:40 +00001734 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
1735
1736 /* Check if a thread in this process holds such a lock */
1737 if( pFile->locktype>SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001738 reserved = 1;
drhbfe66312006-10-03 17:40:40 +00001739 }
1740
1741 /* Otherwise see if some other process holds it.
1742 */
aswift5b1a2562008-08-22 00:22:35 +00001743 if( !reserved ){
1744 /* lock the RESERVED byte */
1745 int lrc = _AFPFSSetLock(context->filePath, pFile, RESERVED_BYTE, 1,1);
1746 if( SQLITE_OK==lrc ){
drhbfe66312006-10-03 17:40:40 +00001747 /* if we succeeded in taking the reserved lock, unlock it to restore
1748 ** the original state */
aswift5b1a2562008-08-22 00:22:35 +00001749 lrc = _AFPFSSetLock(context->filePath, pFile, RESERVED_BYTE, 1, 0);
1750 } else {
1751 /* if we failed to get the lock then someone else must have it */
1752 reserved = 1;
1753 }
1754 if( IS_LOCK_ERROR(lrc) ){
1755 rc=lrc;
drhbfe66312006-10-03 17:40:40 +00001756 }
1757 }
drhbfe66312006-10-03 17:40:40 +00001758
aswift5b1a2562008-08-22 00:22:35 +00001759 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
1760
1761 *pResOut = reserved;
1762 return rc;
drhbfe66312006-10-03 17:40:40 +00001763}
1764
1765/* AFP-style locking following the behavior of unixLock, see the unixLock
1766** function comments for details of lock management. */
danielk1977e339d652008-06-28 11:23:00 +00001767static int afpLock(sqlite3_file *id, int locktype){
drhbfe66312006-10-03 17:40:40 +00001768 int rc = SQLITE_OK;
1769 unixFile *pFile = (unixFile*)id;
1770 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00001771
1772 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001773 OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h,
drh339eb0b2008-03-07 15:34:11 +00001774 locktypeName(locktype), locktypeName(pFile->locktype), getpid());
1775
drhbfe66312006-10-03 17:40:40 +00001776 /* If there is already a lock of this type or more restrictive on the
drh339eb0b2008-03-07 15:34:11 +00001777 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
1778 ** enterMutex() hasn't been called yet.
1779 */
drhbfe66312006-10-03 17:40:40 +00001780 if( pFile->locktype>=locktype ){
drh4f0c5872007-03-26 22:05:01 +00001781 OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
drhbfe66312006-10-03 17:40:40 +00001782 locktypeName(locktype));
1783 return SQLITE_OK;
1784 }
1785
1786 /* Make sure the locking sequence is correct
drh339eb0b2008-03-07 15:34:11 +00001787 */
drhbfe66312006-10-03 17:40:40 +00001788 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
1789 assert( locktype!=PENDING_LOCK );
1790 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
1791
1792 /* This mutex is needed because pFile->pLock is shared across threads
drh339eb0b2008-03-07 15:34:11 +00001793 */
danielk1977b4b47412007-08-17 15:53:36 +00001794 enterMutex();
drhbfe66312006-10-03 17:40:40 +00001795
1796 /* Make sure the current thread owns the pFile.
drh339eb0b2008-03-07 15:34:11 +00001797 */
drhbfe66312006-10-03 17:40:40 +00001798 rc = transferOwnership(pFile);
1799 if( rc!=SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00001800 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00001801 return rc;
1802 }
1803
1804 /* A PENDING lock is needed before acquiring a SHARED lock and before
drh339eb0b2008-03-07 15:34:11 +00001805 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
1806 ** be released.
1807 */
drhbfe66312006-10-03 17:40:40 +00001808 if( locktype==SHARED_LOCK
1809 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
drh339eb0b2008-03-07 15:34:11 +00001810 ){
1811 int failed;
aswift5b1a2562008-08-22 00:22:35 +00001812 failed = _AFPFSSetLock(context->filePath, pFile, PENDING_BYTE, 1, 1);
drhbfe66312006-10-03 17:40:40 +00001813 if (failed) {
aswift5b1a2562008-08-22 00:22:35 +00001814 rc = failed;
drhbfe66312006-10-03 17:40:40 +00001815 goto afp_end_lock;
1816 }
1817 }
1818
1819 /* If control gets to this point, then actually go ahead and make
drh339eb0b2008-03-07 15:34:11 +00001820 ** operating system calls for the specified lock.
1821 */
drhbfe66312006-10-03 17:40:40 +00001822 if( locktype==SHARED_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001823 int lk, lrc1, lrc2, lrc1Errno;
drhbfe66312006-10-03 17:40:40 +00001824
aswift5b1a2562008-08-22 00:22:35 +00001825 /* Now get the read-lock SHARED_LOCK */
drhbfe66312006-10-03 17:40:40 +00001826 /* note that the quality of the randomness doesn't matter that much */
1827 lk = random();
1828 context->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
aswift5b1a2562008-08-22 00:22:35 +00001829 lrc1 = _AFPFSSetLock(context->filePath, pFile,
1830 SHARED_FIRST+context->sharedLockByte, 1, 1);
1831 if( IS_LOCK_ERROR(lrc1) ){
1832 lrc1Errno = pFile->lastErrno;
drhbfe66312006-10-03 17:40:40 +00001833 }
aswift5b1a2562008-08-22 00:22:35 +00001834 /* Drop the temporary PENDING lock */
1835 lrc2 = _AFPFSSetLock(context->filePath, pFile, PENDING_BYTE, 1, 0);
drhbfe66312006-10-03 17:40:40 +00001836
aswift5b1a2562008-08-22 00:22:35 +00001837 if( IS_LOCK_ERROR(lrc1) ) {
1838 pFile->lastErrno = lrc1Errno;
1839 rc = lrc1;
1840 goto afp_end_lock;
1841 } else if( IS_LOCK_ERROR(lrc2) ){
1842 rc = lrc2;
1843 goto afp_end_lock;
1844 } else if( lrc1 != SQLITE_OK ) {
1845 rc = lrc1;
drhbfe66312006-10-03 17:40:40 +00001846 } else {
1847 pFile->locktype = SHARED_LOCK;
1848 }
1849 }else{
1850 /* The request was for a RESERVED or EXCLUSIVE lock. It is
1851 ** assumed that there is a SHARED or greater lock on the file
1852 ** already.
1853 */
1854 int failed = 0;
1855 assert( 0!=pFile->locktype );
1856 if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
1857 /* Acquire a RESERVED lock */
aswift5b1a2562008-08-22 00:22:35 +00001858 failed = _AFPFSSetLock(context->filePath, pFile, RESERVED_BYTE, 1,1);
drhbfe66312006-10-03 17:40:40 +00001859 }
1860 if (!failed && locktype == EXCLUSIVE_LOCK) {
1861 /* Acquire an EXCLUSIVE lock */
1862
1863 /* Remove the shared lock before trying the range. we'll need to
danielk1977e339d652008-06-28 11:23:00 +00001864 ** reestablish the shared lock if we can't get the afpUnlock
drhbfe66312006-10-03 17:40:40 +00001865 */
aswift5b1a2562008-08-22 00:22:35 +00001866 if (!(failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST +
1867 context->sharedLockByte, 1, 0))) {
drhbfe66312006-10-03 17:40:40 +00001868 /* now attemmpt to get the exclusive lock range */
aswift5b1a2562008-08-22 00:22:35 +00001869 failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST,
drhbfe66312006-10-03 17:40:40 +00001870 SHARED_SIZE, 1);
aswift5b1a2562008-08-22 00:22:35 +00001871 if (failed && (failed = _AFPFSSetLock(context->filePath, pFile,
1872 SHARED_FIRST + context->sharedLockByte, 1, 1))) {
1873 rc = failed;
drhbfe66312006-10-03 17:40:40 +00001874 }
1875 } else {
aswift5b1a2562008-08-22 00:22:35 +00001876 rc = failed;
drhbfe66312006-10-03 17:40:40 +00001877 }
1878 }
aswift5b1a2562008-08-22 00:22:35 +00001879 if( failed ){
1880 rc = failed;
drhbfe66312006-10-03 17:40:40 +00001881 }
1882 }
1883
1884 if( rc==SQLITE_OK ){
1885 pFile->locktype = locktype;
1886 }else if( locktype==EXCLUSIVE_LOCK ){
1887 pFile->locktype = PENDING_LOCK;
1888 }
1889
1890afp_end_lock:
drh339eb0b2008-03-07 15:34:11 +00001891 leaveMutex();
drh4f0c5872007-03-26 22:05:01 +00001892 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
drhbfe66312006-10-03 17:40:40 +00001893 rc==SQLITE_OK ? "ok" : "failed");
1894 return rc;
1895}
1896
1897/*
drh339eb0b2008-03-07 15:34:11 +00001898** Lower the locking level on file descriptor pFile to locktype. locktype
1899** must be either NO_LOCK or SHARED_LOCK.
1900**
1901** If the locking level of the file descriptor is already at or below
1902** the requested locking level, this routine is a no-op.
1903*/
danielk1977e339d652008-06-28 11:23:00 +00001904static int afpUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00001905 int rc = SQLITE_OK;
1906 unixFile *pFile = (unixFile*)id;
1907 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
1908
1909 assert( pFile );
drh4f0c5872007-03-26 22:05:01 +00001910 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
drhbfe66312006-10-03 17:40:40 +00001911 pFile->locktype, getpid());
aswift5b1a2562008-08-22 00:22:35 +00001912
drhbfe66312006-10-03 17:40:40 +00001913 assert( locktype<=SHARED_LOCK );
1914 if( pFile->locktype<=locktype ){
1915 return SQLITE_OK;
1916 }
1917 if( CHECK_THREADID(pFile) ){
1918 return SQLITE_MISUSE;
1919 }
danielk1977b4b47412007-08-17 15:53:36 +00001920 enterMutex();
aswift5b1a2562008-08-22 00:22:35 +00001921 int failed = SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00001922 if( pFile->locktype>SHARED_LOCK ){
1923 if( locktype==SHARED_LOCK ){
drhbfe66312006-10-03 17:40:40 +00001924
1925 /* unlock the exclusive range - then re-establish the shared lock */
1926 if (pFile->locktype==EXCLUSIVE_LOCK) {
aswift5b1a2562008-08-22 00:22:35 +00001927 failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST,
drhbfe66312006-10-03 17:40:40 +00001928 SHARED_SIZE, 0);
1929 if (!failed) {
1930 /* successfully removed the exclusive lock */
aswift5b1a2562008-08-22 00:22:35 +00001931 if ((failed = _AFPFSSetLock(context->filePath, pFile, SHARED_FIRST+
1932 context->sharedLockByte, 1, 1))) {
drhbfe66312006-10-03 17:40:40 +00001933 /* failed to re-establish our shared lock */
aswift5b1a2562008-08-22 00:22:35 +00001934 rc = failed;
drhbfe66312006-10-03 17:40:40 +00001935 }
1936 } else {
aswift5b1a2562008-08-22 00:22:35 +00001937 rc = failed;
drhbfe66312006-10-03 17:40:40 +00001938 }
1939 }
1940 }
1941 if (rc == SQLITE_OK && pFile->locktype>=PENDING_LOCK) {
aswift5b1a2562008-08-22 00:22:35 +00001942 if ((failed = _AFPFSSetLock(context->filePath, pFile,
1943 PENDING_BYTE, 1, 0))){
drhbfe66312006-10-03 17:40:40 +00001944 /* failed to release the pending lock */
aswift5b1a2562008-08-22 00:22:35 +00001945 rc = failed;
drhbfe66312006-10-03 17:40:40 +00001946 }
1947 }
1948 if (rc == SQLITE_OK && pFile->locktype>=RESERVED_LOCK) {
aswift5b1a2562008-08-22 00:22:35 +00001949 if ((failed = _AFPFSSetLock(context->filePath, pFile,
1950 RESERVED_BYTE, 1, 0))) {
drhbfe66312006-10-03 17:40:40 +00001951 /* failed to release the reserved lock */
aswift5b1a2562008-08-22 00:22:35 +00001952 rc = failed;
drhbfe66312006-10-03 17:40:40 +00001953 }
1954 }
1955 }
1956 if( locktype==NO_LOCK ){
aswift5b1a2562008-08-22 00:22:35 +00001957 int failed = _AFPFSSetLock(context->filePath, pFile,
drhbfe66312006-10-03 17:40:40 +00001958 SHARED_FIRST + context->sharedLockByte, 1, 0);
1959 if (failed) {
aswift5b1a2562008-08-22 00:22:35 +00001960 rc = failed;
drhbfe66312006-10-03 17:40:40 +00001961 }
1962 }
1963 if (rc == SQLITE_OK)
1964 pFile->locktype = locktype;
danielk1977b4b47412007-08-17 15:53:36 +00001965 leaveMutex();
drhbfe66312006-10-03 17:40:40 +00001966 return rc;
1967}
1968
1969/*
drh339eb0b2008-03-07 15:34:11 +00001970** Close a file & cleanup AFP specific locking context
1971*/
danielk1977e339d652008-06-28 11:23:00 +00001972static int afpClose(sqlite3_file *id) {
1973 if( id ){
1974 unixFile *pFile = (unixFile*)id;
1975 afpUnlock(id, NO_LOCK);
1976 sqlite3_free(pFile->lockingContext);
1977 }
1978 return closeUnixFile(id);
drhbfe66312006-10-03 17:40:40 +00001979}
1980
1981
1982#pragma mark flock() style locking
1983
1984/*
drh339eb0b2008-03-07 15:34:11 +00001985** The flockLockingContext is not used
1986*/
drhbfe66312006-10-03 17:40:40 +00001987typedef void flockLockingContext;
1988
aswift5b1a2562008-08-22 00:22:35 +00001989/* flock-style reserved lock checking following the behavior of
1990 ** unixCheckReservedLock, see the unixCheckReservedLock function comments */
danielk1977e339d652008-06-28 11:23:00 +00001991static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
aswift5b1a2562008-08-22 00:22:35 +00001992 int rc = SQLITE_OK;
1993 int reserved = 0;
drhbfe66312006-10-03 17:40:40 +00001994 unixFile *pFile = (unixFile*)id;
1995
aswift5b1a2562008-08-22 00:22:35 +00001996 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
1997
1998 assert( pFile );
1999
2000 /* Check if a thread in this process holds such a lock */
2001 if( pFile->locktype>SHARED_LOCK ){
2002 reserved = 1;
2003 }
2004
2005 /* Otherwise see if some other process holds it. */
2006 if( !reserved ){
drh3b62b2f2007-06-08 18:27:03 +00002007 /* attempt to get the lock */
aswift5b1a2562008-08-22 00:22:35 +00002008 int lrc = flock(pFile->h, LOCK_EX | LOCK_NB);
2009 if( !lrc ){
drh3b62b2f2007-06-08 18:27:03 +00002010 /* got the lock, unlock it */
aswift5b1a2562008-08-22 00:22:35 +00002011 lrc = flock(pFile->h, LOCK_UN);
2012 if ( lrc ) {
2013 int tErrno = errno;
2014 /* unlock failed with an error */
2015 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2016 if( IS_LOCK_ERROR(lrc) ){
2017 pFile->lastErrno = tErrno;
2018 rc = lrc;
2019 }
2020 }
2021 } else {
2022 int tErrno = errno;
2023 reserved = 1;
2024 /* someone else might have it reserved */
2025 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2026 if( IS_LOCK_ERROR(lrc) ){
2027 pFile->lastErrno = tErrno;
2028 rc = lrc;
2029 }
drhbfe66312006-10-03 17:40:40 +00002030 }
drhbfe66312006-10-03 17:40:40 +00002031 }
aswift5b1a2562008-08-22 00:22:35 +00002032 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
danielk1977861f7452008-06-05 11:39:11 +00002033
aswift5b1a2562008-08-22 00:22:35 +00002034 *pResOut = reserved;
2035 return rc;
drhbfe66312006-10-03 17:40:40 +00002036}
2037
danielk1977e339d652008-06-28 11:23:00 +00002038static int flockLock(sqlite3_file *id, int locktype) {
aswift5b1a2562008-08-22 00:22:35 +00002039 int rc = SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002040 unixFile *pFile = (unixFile*)id;
aswift5b1a2562008-08-22 00:22:35 +00002041
2042 assert( pFile );
2043
drh3b62b2f2007-06-08 18:27:03 +00002044 /* if we already have a lock, it is exclusive.
2045 ** Just adjust level and punt on outta here. */
drhbfe66312006-10-03 17:40:40 +00002046 if (pFile->locktype > NO_LOCK) {
2047 pFile->locktype = locktype;
2048 return SQLITE_OK;
2049 }
2050
drh3b62b2f2007-06-08 18:27:03 +00002051 /* grab an exclusive lock */
aswift5b1a2562008-08-22 00:22:35 +00002052
2053 if (flock(pFile->h, LOCK_EX | LOCK_NB)) {
2054 int tErrno = errno;
drh3b62b2f2007-06-08 18:27:03 +00002055 /* didn't get, must be busy */
aswift5b1a2562008-08-22 00:22:35 +00002056 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2057 if( IS_LOCK_ERROR(rc) ){
2058 pFile->lastErrno = tErrno;
2059 }
drhbfe66312006-10-03 17:40:40 +00002060 } else {
drh3b62b2f2007-06-08 18:27:03 +00002061 /* got it, set the type and return ok */
drhbfe66312006-10-03 17:40:40 +00002062 pFile->locktype = locktype;
drhbfe66312006-10-03 17:40:40 +00002063 }
aswift5b1a2562008-08-22 00:22:35 +00002064 OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
2065 rc==SQLITE_OK ? "ok" : "failed");
2066 return rc;
drhbfe66312006-10-03 17:40:40 +00002067}
2068
danielk1977e339d652008-06-28 11:23:00 +00002069static int flockUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002070 unixFile *pFile = (unixFile*)id;
2071
aswift5b1a2562008-08-22 00:22:35 +00002072 assert( pFile );
2073 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
2074 pFile->locktype, getpid());
drhbfe66312006-10-03 17:40:40 +00002075 assert( locktype<=SHARED_LOCK );
2076
drh3b62b2f2007-06-08 18:27:03 +00002077 /* no-op if possible */
drhbfe66312006-10-03 17:40:40 +00002078 if( pFile->locktype==locktype ){
2079 return SQLITE_OK;
2080 }
2081
drh3b62b2f2007-06-08 18:27:03 +00002082 /* shared can just be set because we always have an exclusive */
drhbfe66312006-10-03 17:40:40 +00002083 if (locktype==SHARED_LOCK) {
2084 pFile->locktype = locktype;
2085 return SQLITE_OK;
2086 }
2087
drh3b62b2f2007-06-08 18:27:03 +00002088 /* no, really, unlock. */
drhbfe66312006-10-03 17:40:40 +00002089 int rc = flock(pFile->h, LOCK_UN);
aswift5b1a2562008-08-22 00:22:35 +00002090 if (rc) {
2091 int r, tErrno = errno;
2092 r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2093 if( IS_LOCK_ERROR(r) ){
2094 pFile->lastErrno = tErrno;
2095 }
2096 return r;
2097 } else {
drhbfe66312006-10-03 17:40:40 +00002098 pFile->locktype = NO_LOCK;
2099 return SQLITE_OK;
2100 }
2101}
2102
2103/*
drh339eb0b2008-03-07 15:34:11 +00002104** Close a file.
2105*/
danielk1977e339d652008-06-28 11:23:00 +00002106static int flockClose(sqlite3_file *id) {
2107 if( id ){
2108 flockUnlock(id, NO_LOCK);
2109 }
2110 return closeUnixFile(id);
drhbfe66312006-10-03 17:40:40 +00002111}
2112
2113#pragma mark Old-School .lock file based locking
2114
aswift5b1a2562008-08-22 00:22:35 +00002115/* Dotlock-style reserved lock checking following the behavior of
2116** unixCheckReservedLock, see the unixCheckReservedLock function comments */
danielk1977e339d652008-06-28 11:23:00 +00002117static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
aswift5b1a2562008-08-22 00:22:35 +00002118 int rc = SQLITE_OK;
2119 int reserved = 0;
drhbfe66312006-10-03 17:40:40 +00002120 unixFile *pFile = (unixFile*)id;
drh339eb0b2008-03-07 15:34:11 +00002121
aswift5b1a2562008-08-22 00:22:35 +00002122 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2123
2124 assert( pFile );
2125
2126 /* Check if a thread in this process holds such a lock */
2127 if( pFile->locktype>SHARED_LOCK ){
2128 reserved = 1;
2129 }
2130
2131 /* Otherwise see if some other process holds it. */
2132 if( !reserved ){
2133 char *zLockFile = (char *)pFile->lockingContext;
drhbfe66312006-10-03 17:40:40 +00002134 struct stat statBuf;
aswift5b1a2562008-08-22 00:22:35 +00002135
2136 if( lstat(zLockFile, &statBuf)==0 ){
2137 /* file exists, someone else has the lock */
2138 reserved = 1;
2139 }else{
drh3b62b2f2007-06-08 18:27:03 +00002140 /* file does not exist, we could have it if we want it */
aswift5b1a2562008-08-22 00:22:35 +00002141 int tErrno = errno;
2142 if( ENOENT != tErrno ){
2143 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
2144 pFile->lastErrno = tErrno;
2145 }
drh339eb0b2008-03-07 15:34:11 +00002146 }
drhbfe66312006-10-03 17:40:40 +00002147 }
aswift5b1a2562008-08-22 00:22:35 +00002148 OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved);
danielk1977861f7452008-06-05 11:39:11 +00002149
aswift5b1a2562008-08-22 00:22:35 +00002150 *pResOut = reserved;
2151 return rc;
drhbfe66312006-10-03 17:40:40 +00002152}
2153
danielk1977e339d652008-06-28 11:23:00 +00002154static int dotlockLock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002155 unixFile *pFile = (unixFile*)id;
drh339eb0b2008-03-07 15:34:11 +00002156 int fd;
danielk1977e339d652008-06-28 11:23:00 +00002157 char *zLockFile = (char *)pFile->lockingContext;
aswift5b1a2562008-08-22 00:22:35 +00002158 int rc=SQLITE_OK;
drh339eb0b2008-03-07 15:34:11 +00002159
drh3b62b2f2007-06-08 18:27:03 +00002160 /* if we already have a lock, it is exclusive.
2161 ** Just adjust level and punt on outta here. */
drhbfe66312006-10-03 17:40:40 +00002162 if (pFile->locktype > NO_LOCK) {
2163 pFile->locktype = locktype;
2164
2165 /* Always update the timestamp on the old file */
danielk1977e339d652008-06-28 11:23:00 +00002166 utimes(zLockFile, NULL);
aswift5b1a2562008-08-22 00:22:35 +00002167 rc = SQLITE_OK;
2168 goto dotlock_end_lock;
drhbfe66312006-10-03 17:40:40 +00002169 }
2170
drh3b62b2f2007-06-08 18:27:03 +00002171 /* check to see if lock file already exists */
drhbfe66312006-10-03 17:40:40 +00002172 struct stat statBuf;
danielk1977e339d652008-06-28 11:23:00 +00002173 if (lstat(zLockFile,&statBuf) == 0){
aswift5b1a2562008-08-22 00:22:35 +00002174 rc = SQLITE_BUSY; /* it does, busy */
2175 goto dotlock_end_lock;
drhbfe66312006-10-03 17:40:40 +00002176 }
2177
drh3b62b2f2007-06-08 18:27:03 +00002178 /* grab an exclusive lock */
danielk1977e339d652008-06-28 11:23:00 +00002179 fd = open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600);
drh339eb0b2008-03-07 15:34:11 +00002180 if( fd<0 ){
drh3b62b2f2007-06-08 18:27:03 +00002181 /* failed to open/create the file, someone else may have stolen the lock */
aswift5b1a2562008-08-22 00:22:35 +00002182 int tErrno = errno;
2183 if( EEXIST == tErrno ){
2184 rc = SQLITE_BUSY;
2185 } else {
2186 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2187 if( IS_LOCK_ERROR(rc) ){
2188 pFile->lastErrno = tErrno;
2189 }
2190 }
2191 goto dotlock_end_lock;
2192 }
drhbfe66312006-10-03 17:40:40 +00002193 close(fd);
2194
drh3b62b2f2007-06-08 18:27:03 +00002195 /* got it, set the type and return ok */
drhbfe66312006-10-03 17:40:40 +00002196 pFile->locktype = locktype;
aswift5b1a2562008-08-22 00:22:35 +00002197
2198 dotlock_end_lock:
2199 return rc;
drhbfe66312006-10-03 17:40:40 +00002200}
2201
danielk1977e339d652008-06-28 11:23:00 +00002202static int dotlockUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002203 unixFile *pFile = (unixFile*)id;
danielk1977e339d652008-06-28 11:23:00 +00002204 char *zLockFile = (char *)pFile->lockingContext;
drh339eb0b2008-03-07 15:34:11 +00002205
aswift5b1a2562008-08-22 00:22:35 +00002206 assert( pFile );
2207 OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
2208 pFile->locktype, getpid());
drhbfe66312006-10-03 17:40:40 +00002209 assert( locktype<=SHARED_LOCK );
2210
drh3b62b2f2007-06-08 18:27:03 +00002211 /* no-op if possible */
drhbfe66312006-10-03 17:40:40 +00002212 if( pFile->locktype==locktype ){
2213 return SQLITE_OK;
2214 }
2215
drh3b62b2f2007-06-08 18:27:03 +00002216 /* shared can just be set because we always have an exclusive */
drhbfe66312006-10-03 17:40:40 +00002217 if (locktype==SHARED_LOCK) {
2218 pFile->locktype = locktype;
2219 return SQLITE_OK;
2220 }
2221
drh3b62b2f2007-06-08 18:27:03 +00002222 /* no, really, unlock. */
aswift5b1a2562008-08-22 00:22:35 +00002223 if (unlink(zLockFile) ) {
2224 int rc, tErrno = errno;
2225 if( ENOENT != tErrno ){
2226 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2227 }
2228 if( IS_LOCK_ERROR(rc) ){
2229 pFile->lastErrno = tErrno;
2230 }
2231 return rc;
2232 }
drhbfe66312006-10-03 17:40:40 +00002233 pFile->locktype = NO_LOCK;
2234 return SQLITE_OK;
2235}
2236
2237/*
2238 ** Close a file.
2239 */
danielk1977e339d652008-06-28 11:23:00 +00002240static int dotlockClose(sqlite3_file *id) {
2241 if( id ){
2242 unixFile *pFile = (unixFile*)id;
2243 dotlockUnlock(id, NO_LOCK);
2244 sqlite3_free(pFile->lockingContext);
2245 }
2246 return closeUnixFile(id);
drhbfe66312006-10-03 17:40:40 +00002247}
2248
2249
drhda0e7682008-07-30 15:27:54 +00002250#endif /* SQLITE_ENABLE_LOCKING_STYLE */
drhbfe66312006-10-03 17:40:40 +00002251
2252/*
drh339eb0b2008-03-07 15:34:11 +00002253** The nolockLockingContext is void
2254*/
drhbfe66312006-10-03 17:40:40 +00002255typedef void nolockLockingContext;
2256
danielk1977e339d652008-06-28 11:23:00 +00002257static int nolockCheckReservedLock(sqlite3_file *id, int *pResOut) {
danielk1977861f7452008-06-05 11:39:11 +00002258 *pResOut = 0;
2259 return SQLITE_OK;
drhbfe66312006-10-03 17:40:40 +00002260}
2261
danielk1977e339d652008-06-28 11:23:00 +00002262static int nolockLock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002263 return SQLITE_OK;
2264}
2265
danielk1977e339d652008-06-28 11:23:00 +00002266static int nolockUnlock(sqlite3_file *id, int locktype) {
drhbfe66312006-10-03 17:40:40 +00002267 return SQLITE_OK;
2268}
2269
2270/*
drh339eb0b2008-03-07 15:34:11 +00002271** Close a file.
2272*/
danielk1977e339d652008-06-28 11:23:00 +00002273static int nolockClose(sqlite3_file *id) {
2274 return closeUnixFile(id);
drhbfe66312006-10-03 17:40:40 +00002275}
2276
danielk1977ad94b582007-08-20 06:44:22 +00002277
danielk1977e3026632004-06-22 11:29:02 +00002278/*
drh9e33c2c2007-08-31 18:34:59 +00002279** Information and control of an open file handle.
drh18839212005-11-26 03:43:23 +00002280*/
drhcc6bb3e2007-08-31 16:11:35 +00002281static int unixFileControl(sqlite3_file *id, int op, void *pArg){
drh9e33c2c2007-08-31 18:34:59 +00002282 switch( op ){
2283 case SQLITE_FCNTL_LOCKSTATE: {
2284 *(int*)pArg = ((unixFile*)id)->locktype;
2285 return SQLITE_OK;
2286 }
2287 }
drhcc6bb3e2007-08-31 16:11:35 +00002288 return SQLITE_ERROR;
drh9cbe6352005-11-29 03:13:21 +00002289}
2290
2291/*
danielk1977a3d4c882007-03-23 10:08:38 +00002292** Return the sector size in bytes of the underlying block device for
2293** the specified file. This is almost always 512 bytes, but may be
2294** larger for some devices.
2295**
2296** SQLite code assumes this function cannot fail. It also assumes that
2297** if two files are created in the same file-system directory (i.e.
drh85b623f2007-12-13 21:54:09 +00002298** a database and its journal file) that the sector size will be the
danielk1977a3d4c882007-03-23 10:08:38 +00002299** same for both.
2300*/
danielk197762079062007-08-15 17:08:46 +00002301static int unixSectorSize(sqlite3_file *id){
drh3ceeb752007-03-29 18:19:52 +00002302 return SQLITE_DEFAULT_SECTOR_SIZE;
danielk1977a3d4c882007-03-23 10:08:38 +00002303}
2304
danielk197790949c22007-08-17 16:50:38 +00002305/*
2306** Return the device characteristics for the file. This is always 0.
2307*/
danielk197762079062007-08-15 17:08:46 +00002308static int unixDeviceCharacteristics(sqlite3_file *id){
2309 return 0;
2310}
2311
danielk1977a3d4c882007-03-23 10:08:38 +00002312/*
danielk1977e339d652008-06-28 11:23:00 +00002313** Initialize the contents of the unixFile structure pointed to by pId.
2314**
danielk1977ad94b582007-08-20 06:44:22 +00002315** When locking extensions are enabled, the filepath and locking style
2316** are needed to determine the unixFile pMethod to use for locking operations.
2317** The locking-style specific lockingContext data structure is created
2318** and assigned here also.
2319*/
2320static int fillInUnixFile(
danielk1977e339d652008-06-28 11:23:00 +00002321 sqlite3_vfs *pVfs, /* Pointer to vfs object */
drhbfe66312006-10-03 17:40:40 +00002322 int h, /* Open file descriptor of file being opened */
danielk1977ad94b582007-08-20 06:44:22 +00002323 int dirfd, /* Directory file descriptor */
drh218c5082008-03-07 00:27:10 +00002324 sqlite3_file *pId, /* Write to the unixFile structure here */
drhda0e7682008-07-30 15:27:54 +00002325 const char *zFilename, /* Name of the file being opened */
2326 int noLock /* Omit locking if true */
drhbfe66312006-10-03 17:40:40 +00002327){
drhda0e7682008-07-30 15:27:54 +00002328 int eLockingStyle;
2329 unixFile *pNew = (unixFile *)pId;
2330 int rc = SQLITE_OK;
2331
danielk1977e339d652008-06-28 11:23:00 +00002332 /* Macro to define the static contents of an sqlite3_io_methods
2333 ** structure for a unix backend file. Different locking methods
2334 ** require different functions for the xClose, xLock, xUnlock and
2335 ** xCheckReservedLock methods.
2336 */
2337 #define IOMETHODS(xClose, xLock, xUnlock, xCheckReservedLock) { \
2338 1, /* iVersion */ \
2339 xClose, /* xClose */ \
2340 unixRead, /* xRead */ \
2341 unixWrite, /* xWrite */ \
2342 unixTruncate, /* xTruncate */ \
2343 unixSync, /* xSync */ \
2344 unixFileSize, /* xFileSize */ \
2345 xLock, /* xLock */ \
2346 xUnlock, /* xUnlock */ \
2347 xCheckReservedLock, /* xCheckReservedLock */ \
2348 unixFileControl, /* xFileControl */ \
2349 unixSectorSize, /* xSectorSize */ \
2350 unixDeviceCharacteristics /* xDeviceCapabilities */ \
2351 }
2352 static sqlite3_io_methods aIoMethod[] = {
2353 IOMETHODS(unixClose, unixLock, unixUnlock, unixCheckReservedLock)
danielk1977e339d652008-06-28 11:23:00 +00002354 ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
drhda0e7682008-07-30 15:27:54 +00002355#ifdef SQLITE_ENABLE_LOCKING_STYLE
2356 ,IOMETHODS(dotlockClose, dotlockLock, dotlockUnlock,dotlockCheckReservedLock)
2357 ,IOMETHODS(flockClose, flockLock, flockUnlock, flockCheckReservedLock)
danielk1977e339d652008-06-28 11:23:00 +00002358 ,IOMETHODS(afpClose, afpLock, afpUnlock, afpCheckReservedLock)
drh218c5082008-03-07 00:27:10 +00002359#endif
danielk1977e339d652008-06-28 11:23:00 +00002360 };
drhda0e7682008-07-30 15:27:54 +00002361 /* The order of the IOMETHODS macros above is important. It must be the
2362 ** same order as the LOCKING_STYLE numbers
2363 */
2364 assert(LOCKING_STYLE_POSIX==1);
2365 assert(LOCKING_STYLE_NONE==2);
2366 assert(LOCKING_STYLE_DOTFILE==3);
2367 assert(LOCKING_STYLE_FLOCK==4);
2368 assert(LOCKING_STYLE_AFP==5);
drh218c5082008-03-07 00:27:10 +00002369
danielk197717b90b52008-06-06 11:11:25 +00002370 assert( pNew->pLock==NULL );
2371 assert( pNew->pOpen==NULL );
drh218c5082008-03-07 00:27:10 +00002372
2373 OSTRACE3("OPEN %-3d %s\n", h, zFilename);
danielk1977ad94b582007-08-20 06:44:22 +00002374 pNew->h = h;
drh218c5082008-03-07 00:27:10 +00002375 pNew->dirfd = dirfd;
danielk1977ad94b582007-08-20 06:44:22 +00002376 SET_THREADID(pNew);
drh339eb0b2008-03-07 15:34:11 +00002377
drhda0e7682008-07-30 15:27:54 +00002378 if( noLock ){
2379 eLockingStyle = LOCKING_STYLE_NONE;
2380 }else{
2381 eLockingStyle = detectLockingStyle(pVfs, zFilename, h);
2382 }
danielk1977e339d652008-06-28 11:23:00 +00002383
2384 switch( eLockingStyle ){
2385
2386 case LOCKING_STYLE_POSIX: {
2387 enterMutex();
2388 rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
2389 leaveMutex();
drh218c5082008-03-07 00:27:10 +00002390 break;
drhbfe66312006-10-03 17:40:40 +00002391 }
danielk1977e339d652008-06-28 11:23:00 +00002392
2393#ifdef SQLITE_ENABLE_LOCKING_STYLE
2394 case LOCKING_STYLE_AFP: {
2395 /* AFP locking uses the file path so it needs to be included in
2396 ** the afpLockingContext.
2397 */
2398 afpLockingContext *pCtx;
2399 pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
2400 if( pCtx==0 ){
2401 rc = SQLITE_NOMEM;
2402 }else{
2403 /* NB: zFilename exists and remains valid until the file is closed
2404 ** according to requirement F11141. So we do not need to make a
2405 ** copy of the filename. */
2406 pCtx->filePath = zFilename;
2407 srandomdev();
2408 }
drh218c5082008-03-07 00:27:10 +00002409 break;
danielk1977e339d652008-06-28 11:23:00 +00002410 }
2411
2412 case LOCKING_STYLE_DOTFILE: {
2413 /* Dotfile locking uses the file path so it needs to be included in
2414 ** the dotlockLockingContext
2415 */
2416 char *zLockFile;
drh218c5082008-03-07 00:27:10 +00002417 int nFilename;
danielk1977e339d652008-06-28 11:23:00 +00002418 nFilename = strlen(zFilename) + 6;
2419 zLockFile = (char *)sqlite3_malloc(nFilename);
2420 if( zLockFile==0 ){
2421 rc = SQLITE_NOMEM;
2422 }else{
2423 sqlite3_snprintf(nFilename, zLockFile, "%s.lock", zFilename);
drh339eb0b2008-03-07 15:34:11 +00002424 }
danielk1977e339d652008-06-28 11:23:00 +00002425 pNew->lockingContext = zLockFile;
drh218c5082008-03-07 00:27:10 +00002426 break;
2427 }
danielk1977e339d652008-06-28 11:23:00 +00002428
2429 case LOCKING_STYLE_FLOCK:
2430 case LOCKING_STYLE_NONE:
drh218c5082008-03-07 00:27:10 +00002431 break;
drhe78669b2007-06-29 12:04:26 +00002432#endif
danielk1977e339d652008-06-28 11:23:00 +00002433 }
aswift5b1a2562008-08-22 00:22:35 +00002434
2435 pNew->lastErrno = 0;
danielk1977e339d652008-06-28 11:23:00 +00002436 if( rc!=SQLITE_OK ){
danielk19777c055b92007-10-30 17:28:51 +00002437 if( dirfd>=0 ) close(dirfd);
drhbfe66312006-10-03 17:40:40 +00002438 close(h);
danielk1977e339d652008-06-28 11:23:00 +00002439 }else{
danielk19776cb427f2008-06-30 10:16:04 +00002440 pNew->pMethod = &aIoMethod[eLockingStyle-1];
danielk1977e339d652008-06-28 11:23:00 +00002441 OpenCounter(+1);
drhbfe66312006-10-03 17:40:40 +00002442 }
danielk1977e339d652008-06-28 11:23:00 +00002443 return rc;
drh054889e2005-11-30 03:20:31 +00002444}
drh9c06c952005-11-26 00:25:00 +00002445
danielk1977ad94b582007-08-20 06:44:22 +00002446/*
2447** Open a file descriptor to the directory containing file zFilename.
2448** If successful, *pFd is set to the opened file descriptor and
2449** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
2450** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
2451** value.
2452**
2453** If SQLITE_OK is returned, the caller is responsible for closing
2454** the file descriptor *pFd using close().
2455*/
danielk1977fee2d252007-08-18 10:59:19 +00002456static int openDirectory(const char *zFilename, int *pFd){
danielk1977fee2d252007-08-18 10:59:19 +00002457 int ii;
drh777b17a2007-09-20 10:02:54 +00002458 int fd = -1;
drhf3a65f72007-08-22 20:18:21 +00002459 char zDirname[MAX_PATHNAME+1];
danielk1977fee2d252007-08-18 10:59:19 +00002460
drh153c62c2007-08-24 03:51:33 +00002461 sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
danielk1977fee2d252007-08-18 10:59:19 +00002462 for(ii=strlen(zDirname); ii>=0 && zDirname[ii]!='/'; ii--);
2463 if( ii>0 ){
2464 zDirname[ii] = '\0';
2465 fd = open(zDirname, O_RDONLY|O_BINARY, 0);
drh777b17a2007-09-20 10:02:54 +00002466 if( fd>=0 ){
danielk1977fee2d252007-08-18 10:59:19 +00002467#ifdef FD_CLOEXEC
2468 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
2469#endif
2470 OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname);
2471 }
2472 }
danielk1977fee2d252007-08-18 10:59:19 +00002473 *pFd = fd;
drh777b17a2007-09-20 10:02:54 +00002474 return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN);
danielk1977fee2d252007-08-18 10:59:19 +00002475}
2476
danielk1977b4b47412007-08-17 15:53:36 +00002477/*
danielk197717b90b52008-06-06 11:11:25 +00002478** Create a temporary file name in zBuf. zBuf must be allocated
2479** by the calling process and must be big enough to hold at least
2480** pVfs->mxPathname bytes.
2481*/
2482static int getTempname(int nBuf, char *zBuf){
2483 static const char *azDirs[] = {
2484 0,
2485 "/var/tmp",
2486 "/usr/tmp",
2487 "/tmp",
2488 ".",
2489 };
2490 static const unsigned char zChars[] =
2491 "abcdefghijklmnopqrstuvwxyz"
2492 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2493 "0123456789";
2494 int i, j;
2495 struct stat buf;
2496 const char *zDir = ".";
2497
2498 /* It's odd to simulate an io-error here, but really this is just
2499 ** using the io-error infrastructure to test that SQLite handles this
2500 ** function failing.
2501 */
2502 SimulateIOError( return SQLITE_IOERR );
2503
2504 azDirs[0] = sqlite3_temp_directory;
2505 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
2506 if( azDirs[i]==0 ) continue;
2507 if( stat(azDirs[i], &buf) ) continue;
2508 if( !S_ISDIR(buf.st_mode) ) continue;
2509 if( access(azDirs[i], 07) ) continue;
2510 zDir = azDirs[i];
2511 break;
2512 }
2513
2514 /* Check that the output buffer is large enough for the temporary file
2515 ** name. If it is not, return SQLITE_ERROR.
2516 */
2517 if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= nBuf ){
2518 return SQLITE_ERROR;
2519 }
2520
2521 do{
2522 sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
2523 j = strlen(zBuf);
2524 sqlite3_randomness(15, &zBuf[j]);
2525 for(i=0; i<15; i++, j++){
2526 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
2527 }
2528 zBuf[j] = 0;
2529 }while( access(zBuf,0)==0 );
2530 return SQLITE_OK;
2531}
2532
2533
2534/*
danielk1977ad94b582007-08-20 06:44:22 +00002535** Open the file zPath.
2536**
danielk1977b4b47412007-08-17 15:53:36 +00002537** Previously, the SQLite OS layer used three functions in place of this
2538** one:
2539**
2540** sqlite3OsOpenReadWrite();
2541** sqlite3OsOpenReadOnly();
2542** sqlite3OsOpenExclusive();
2543**
2544** These calls correspond to the following combinations of flags:
2545**
2546** ReadWrite() -> (READWRITE | CREATE)
2547** ReadOnly() -> (READONLY)
2548** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
2549**
2550** The old OpenExclusive() accepted a boolean argument - "delFlag". If
2551** true, the file was configured to be automatically deleted when the
2552** file handle closed. To achieve the same effect using this new
2553** interface, add the DELETEONCLOSE flag to those specified above for
2554** OpenExclusive().
2555*/
2556static int unixOpen(
drh153c62c2007-08-24 03:51:33 +00002557 sqlite3_vfs *pVfs,
danielk1977b4b47412007-08-17 15:53:36 +00002558 const char *zPath,
2559 sqlite3_file *pFile,
2560 int flags,
2561 int *pOutFlags
2562){
danielk1977fee2d252007-08-18 10:59:19 +00002563 int fd = 0; /* File descriptor returned by open() */
2564 int dirfd = -1; /* Directory file descriptor */
2565 int oflags = 0; /* Flags to pass to open() */
2566 int eType = flags&0xFFFFFF00; /* Type of file to open */
drhda0e7682008-07-30 15:27:54 +00002567 int noLock; /* True to omit locking primitives */
danielk1977b4b47412007-08-17 15:53:36 +00002568
2569 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
2570 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
2571 int isCreate = (flags & SQLITE_OPEN_CREATE);
2572 int isReadonly = (flags & SQLITE_OPEN_READONLY);
2573 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
2574
danielk1977fee2d252007-08-18 10:59:19 +00002575 /* If creating a master or main-file journal, this function will open
2576 ** a file-descriptor on the directory too. The first time unixSync()
2577 ** is called the directory file descriptor will be fsync()ed and close()d.
2578 */
2579 int isOpenDirectory = (isCreate &&
2580 (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL)
2581 );
2582
danielk197717b90b52008-06-06 11:11:25 +00002583 /* If argument zPath is a NULL pointer, this function is required to open
2584 ** a temporary file. Use this buffer to store the file name in.
2585 */
2586 char zTmpname[MAX_PATHNAME+1];
2587 const char *zName = zPath;
2588
danielk1977fee2d252007-08-18 10:59:19 +00002589 /* Check the following statements are true:
2590 **
2591 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
2592 ** (b) if CREATE is set, then READWRITE must also be set, and
2593 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
drh33f4e022007-09-03 15:19:34 +00002594 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
danielk1977fee2d252007-08-18 10:59:19 +00002595 */
danielk1977b4b47412007-08-17 15:53:36 +00002596 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
danielk1977b4b47412007-08-17 15:53:36 +00002597 assert(isCreate==0 || isReadWrite);
danielk1977b4b47412007-08-17 15:53:36 +00002598 assert(isExclusive==0 || isCreate);
drh33f4e022007-09-03 15:19:34 +00002599 assert(isDelete==0 || isCreate);
2600
drh33f4e022007-09-03 15:19:34 +00002601 /* The main DB, main journal, and master journal are never automatically
2602 ** deleted
2603 */
2604 assert( eType!=SQLITE_OPEN_MAIN_DB || !isDelete );
2605 assert( eType!=SQLITE_OPEN_MAIN_JOURNAL || !isDelete );
2606 assert( eType!=SQLITE_OPEN_MASTER_JOURNAL || !isDelete );
danielk1977b4b47412007-08-17 15:53:36 +00002607
danielk1977fee2d252007-08-18 10:59:19 +00002608 /* Assert that the upper layer has set one of the "file-type" flags. */
2609 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
2610 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
2611 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
drh33f4e022007-09-03 15:19:34 +00002612 || eType==SQLITE_OPEN_TRANSIENT_DB
danielk1977fee2d252007-08-18 10:59:19 +00002613 );
2614
danielk1977e339d652008-06-28 11:23:00 +00002615 memset(pFile, 0, sizeof(unixFile));
2616
danielk197717b90b52008-06-06 11:11:25 +00002617 if( !zName ){
2618 int rc;
2619 assert(isDelete && !isOpenDirectory);
2620 rc = getTempname(MAX_PATHNAME+1, zTmpname);
2621 if( rc!=SQLITE_OK ){
2622 return rc;
2623 }
2624 zName = zTmpname;
2625 }
2626
danielk1977b4b47412007-08-17 15:53:36 +00002627 if( isReadonly ) oflags |= O_RDONLY;
2628 if( isReadWrite ) oflags |= O_RDWR;
2629 if( isCreate ) oflags |= O_CREAT;
2630 if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW);
2631 oflags |= (O_LARGEFILE|O_BINARY);
2632
danielk197717b90b52008-06-06 11:11:25 +00002633 fd = open(zName, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
danielk19772f2d8c72007-08-30 16:13:33 +00002634 if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
danielk1977b4b47412007-08-17 15:53:36 +00002635 /* Failed to open the file for read/write access. Try read-only. */
2636 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
2637 flags |= SQLITE_OPEN_READONLY;
drh153c62c2007-08-24 03:51:33 +00002638 return unixOpen(pVfs, zPath, pFile, flags, pOutFlags);
danielk1977b4b47412007-08-17 15:53:36 +00002639 }
2640 if( fd<0 ){
2641 return SQLITE_CANTOPEN;
2642 }
2643 if( isDelete ){
danielk197717b90b52008-06-06 11:11:25 +00002644 unlink(zName);
danielk1977b4b47412007-08-17 15:53:36 +00002645 }
2646 if( pOutFlags ){
2647 *pOutFlags = flags;
2648 }
2649
2650 assert(fd!=0);
danielk1977fee2d252007-08-18 10:59:19 +00002651 if( isOpenDirectory ){
2652 int rc = openDirectory(zPath, &dirfd);
2653 if( rc!=SQLITE_OK ){
2654 close(fd);
2655 return rc;
2656 }
2657 }
danielk1977e339d652008-06-28 11:23:00 +00002658
2659#ifdef FD_CLOEXEC
2660 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
2661#endif
2662
drhda0e7682008-07-30 15:27:54 +00002663 noLock = eType!=SQLITE_OPEN_MAIN_DB;
2664 return fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock);
danielk1977b4b47412007-08-17 15:53:36 +00002665}
2666
2667/*
danielk1977fee2d252007-08-18 10:59:19 +00002668** Delete the file at zPath. If the dirSync argument is true, fsync()
2669** the directory after deleting the file.
danielk1977b4b47412007-08-17 15:53:36 +00002670*/
drh153c62c2007-08-24 03:51:33 +00002671static int unixDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
danielk1977fee2d252007-08-18 10:59:19 +00002672 int rc = SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00002673 SimulateIOError(return SQLITE_IOERR_DELETE);
2674 unlink(zPath);
danielk1977fee2d252007-08-18 10:59:19 +00002675 if( dirSync ){
2676 int fd;
2677 rc = openDirectory(zPath, &fd);
2678 if( rc==SQLITE_OK ){
2679 if( fsync(fd) ){
2680 rc = SQLITE_IOERR_DIR_FSYNC;
2681 }
2682 close(fd);
2683 }
2684 }
2685 return rc;
danielk1977b4b47412007-08-17 15:53:36 +00002686}
2687
danielk197790949c22007-08-17 16:50:38 +00002688/*
2689** Test the existance of or access permissions of file zPath. The
2690** test performed depends on the value of flags:
2691**
2692** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
2693** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
2694** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
2695**
2696** Otherwise return 0.
2697*/
danielk1977861f7452008-06-05 11:39:11 +00002698static int unixAccess(
2699 sqlite3_vfs *pVfs,
2700 const char *zPath,
2701 int flags,
2702 int *pResOut
2703){
rse25c0d1a2007-09-20 08:38:14 +00002704 int amode = 0;
danielk1977861f7452008-06-05 11:39:11 +00002705 SimulateIOError( return SQLITE_IOERR_ACCESS; );
danielk1977b4b47412007-08-17 15:53:36 +00002706 switch( flags ){
2707 case SQLITE_ACCESS_EXISTS:
2708 amode = F_OK;
2709 break;
2710 case SQLITE_ACCESS_READWRITE:
2711 amode = W_OK|R_OK;
2712 break;
drh50d3f902007-08-27 21:10:36 +00002713 case SQLITE_ACCESS_READ:
danielk1977b4b47412007-08-17 15:53:36 +00002714 amode = R_OK;
2715 break;
2716
2717 default:
2718 assert(!"Invalid flags argument");
2719 }
danielk1977861f7452008-06-05 11:39:11 +00002720 *pResOut = (access(zPath, amode)==0);
2721 return SQLITE_OK;
danielk1977b4b47412007-08-17 15:53:36 +00002722}
2723
danielk1977b4b47412007-08-17 15:53:36 +00002724
2725/*
2726** Turn a relative pathname into a full pathname. The relative path
2727** is stored as a nul-terminated string in the buffer pointed to by
2728** zPath.
2729**
2730** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
2731** (in this case, MAX_PATHNAME bytes). The full-path is written to
2732** this buffer before returning.
2733*/
danielk1977adfb9b02007-09-17 07:02:56 +00002734static int unixFullPathname(
2735 sqlite3_vfs *pVfs, /* Pointer to vfs object */
2736 const char *zPath, /* Possibly relative input path */
2737 int nOut, /* Size of output buffer in bytes */
2738 char *zOut /* Output buffer */
2739){
danielk1977843e65f2007-09-01 16:16:15 +00002740
2741 /* It's odd to simulate an io-error here, but really this is just
2742 ** using the io-error infrastructure to test that SQLite handles this
2743 ** function failing. This function could fail if, for example, the
2744 ** current working directly has been unlinked.
2745 */
2746 SimulateIOError( return SQLITE_ERROR );
2747
drh153c62c2007-08-24 03:51:33 +00002748 assert( pVfs->mxPathname==MAX_PATHNAME );
drh3c7f2dc2007-12-06 13:26:20 +00002749 zOut[nOut-1] = '\0';
danielk1977b4b47412007-08-17 15:53:36 +00002750 if( zPath[0]=='/' ){
drh3c7f2dc2007-12-06 13:26:20 +00002751 sqlite3_snprintf(nOut, zOut, "%s", zPath);
danielk1977b4b47412007-08-17 15:53:36 +00002752 }else{
2753 int nCwd;
drh3c7f2dc2007-12-06 13:26:20 +00002754 if( getcwd(zOut, nOut-1)==0 ){
drh70c01452007-09-03 17:42:17 +00002755 return SQLITE_CANTOPEN;
danielk1977b4b47412007-08-17 15:53:36 +00002756 }
2757 nCwd = strlen(zOut);
drh3c7f2dc2007-12-06 13:26:20 +00002758 sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
danielk1977b4b47412007-08-17 15:53:36 +00002759 }
2760 return SQLITE_OK;
2761
2762#if 0
2763 /*
2764 ** Remove "/./" path elements and convert "/A/./" path elements
2765 ** to just "/".
2766 */
2767 if( zFull ){
2768 int i, j;
2769 for(i=j=0; zFull[i]; i++){
2770 if( zFull[i]=='/' ){
2771 if( zFull[i+1]=='/' ) continue;
2772 if( zFull[i+1]=='.' && zFull[i+2]=='/' ){
2773 i += 1;
2774 continue;
2775 }
2776 if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){
2777 while( j>0 && zFull[j-1]!='/' ){ j--; }
2778 i += 3;
2779 continue;
2780 }
2781 }
2782 zFull[j++] = zFull[i];
2783 }
2784 zFull[j] = 0;
2785 }
2786#endif
2787}
2788
drh0ccebe72005-06-07 22:22:50 +00002789
drh761df872006-12-21 01:29:22 +00002790#ifndef SQLITE_OMIT_LOAD_EXTENSION
2791/*
2792** Interfaces for opening a shared library, finding entry points
2793** within the shared library, and closing the shared library.
2794*/
2795#include <dlfcn.h>
drh153c62c2007-08-24 03:51:33 +00002796static void *unixDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
drh761df872006-12-21 01:29:22 +00002797 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
2798}
danielk197795c8a542007-09-01 06:51:27 +00002799
2800/*
2801** SQLite calls this function immediately after a call to unixDlSym() or
2802** unixDlOpen() fails (returns a null pointer). If a more detailed error
2803** message is available, it is written to zBufOut. If no error message
2804** is available, zBufOut is left unmodified and SQLite uses a default
2805** error message.
2806*/
drh153c62c2007-08-24 03:51:33 +00002807static void unixDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
danielk1977b4b47412007-08-17 15:53:36 +00002808 char *zErr;
2809 enterMutex();
2810 zErr = dlerror();
2811 if( zErr ){
drh153c62c2007-08-24 03:51:33 +00002812 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
danielk1977b4b47412007-08-17 15:53:36 +00002813 }
2814 leaveMutex();
2815}
drh46c99e02007-08-27 23:26:59 +00002816static void *unixDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){
drh761df872006-12-21 01:29:22 +00002817 return dlsym(pHandle, zSymbol);
2818}
drh46c99e02007-08-27 23:26:59 +00002819static void unixDlClose(sqlite3_vfs *pVfs, void *pHandle){
danielk1977b4b47412007-08-17 15:53:36 +00002820 dlclose(pHandle);
drh761df872006-12-21 01:29:22 +00002821}
danielk1977b4b47412007-08-17 15:53:36 +00002822#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
2823 #define unixDlOpen 0
2824 #define unixDlError 0
2825 #define unixDlSym 0
2826 #define unixDlClose 0
2827#endif
2828
2829/*
danielk197790949c22007-08-17 16:50:38 +00002830** Write nBuf bytes of random data to the supplied buffer zBuf.
drhbbd42a62004-05-22 17:41:58 +00002831*/
drh153c62c2007-08-24 03:51:33 +00002832static int unixRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
danielk197790949c22007-08-17 16:50:38 +00002833
2834 assert(nBuf>=(sizeof(time_t)+sizeof(int)));
2835
drhbbd42a62004-05-22 17:41:58 +00002836 /* We have to initialize zBuf to prevent valgrind from reporting
2837 ** errors. The reports issued by valgrind are incorrect - we would
2838 ** prefer that the randomness be increased by making use of the
2839 ** uninitialized space in zBuf - but valgrind errors tend to worry
2840 ** some users. Rather than argue, it seems easier just to initialize
2841 ** the whole array and silence valgrind, even if that means less randomness
2842 ** in the random seed.
2843 **
2844 ** When testing, initializing zBuf[] to zero is all we do. That means
drhf1a221e2006-01-15 17:27:17 +00002845 ** that we always use the same random number sequence. This makes the
drhbbd42a62004-05-22 17:41:58 +00002846 ** tests repeatable.
2847 */
danielk1977b4b47412007-08-17 15:53:36 +00002848 memset(zBuf, 0, nBuf);
drhbbd42a62004-05-22 17:41:58 +00002849#if !defined(SQLITE_TEST)
2850 {
drh842b8642005-01-21 17:53:17 +00002851 int pid, fd;
2852 fd = open("/dev/urandom", O_RDONLY);
2853 if( fd<0 ){
drh07397232006-01-06 14:46:46 +00002854 time_t t;
2855 time(&t);
danielk197790949c22007-08-17 16:50:38 +00002856 memcpy(zBuf, &t, sizeof(t));
2857 pid = getpid();
2858 memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
drh842b8642005-01-21 17:53:17 +00002859 }else{
danielk1977b4b47412007-08-17 15:53:36 +00002860 read(fd, zBuf, nBuf);
drh842b8642005-01-21 17:53:17 +00002861 close(fd);
2862 }
drhbbd42a62004-05-22 17:41:58 +00002863 }
2864#endif
2865 return SQLITE_OK;
2866}
2867
danielk1977b4b47412007-08-17 15:53:36 +00002868
drhbbd42a62004-05-22 17:41:58 +00002869/*
2870** Sleep for a little while. Return the amount of time slept.
danielk1977b4b47412007-08-17 15:53:36 +00002871** The argument is the number of microseconds we want to sleep.
drh4a50aac2007-08-23 02:47:53 +00002872** The return value is the number of microseconds of sleep actually
2873** requested from the underlying operating system, a number which
2874** might be greater than or equal to the argument, but not less
2875** than the argument.
drhbbd42a62004-05-22 17:41:58 +00002876*/
drh153c62c2007-08-24 03:51:33 +00002877static int unixSleep(sqlite3_vfs *pVfs, int microseconds){
drhbbd42a62004-05-22 17:41:58 +00002878#if defined(HAVE_USLEEP) && HAVE_USLEEP
danielk1977b4b47412007-08-17 15:53:36 +00002879 usleep(microseconds);
2880 return microseconds;
drhbbd42a62004-05-22 17:41:58 +00002881#else
danielk1977b4b47412007-08-17 15:53:36 +00002882 int seconds = (microseconds+999999)/1000000;
2883 sleep(seconds);
drh4a50aac2007-08-23 02:47:53 +00002884 return seconds*1000000;
drha3fad6f2006-01-18 14:06:37 +00002885#endif
drh88f474a2006-01-02 20:00:12 +00002886}
2887
2888/*
drhbbd42a62004-05-22 17:41:58 +00002889** The following variable, if set to a non-zero value, becomes the result
drh66560ad2006-01-06 14:32:19 +00002890** returned from sqlite3OsCurrentTime(). This is used for testing.
drhbbd42a62004-05-22 17:41:58 +00002891*/
2892#ifdef SQLITE_TEST
2893int sqlite3_current_time = 0;
2894#endif
2895
2896/*
2897** Find the current time (in Universal Coordinated Time). Write the
2898** current time and date as a Julian Day number into *prNow and
2899** return 0. Return 1 if the time and date cannot be found.
2900*/
drh153c62c2007-08-24 03:51:33 +00002901static int unixCurrentTime(sqlite3_vfs *pVfs, double *prNow){
drh19e2d372005-08-29 23:00:03 +00002902#ifdef NO_GETTOD
drhbbd42a62004-05-22 17:41:58 +00002903 time_t t;
2904 time(&t);
2905 *prNow = t/86400.0 + 2440587.5;
drh19e2d372005-08-29 23:00:03 +00002906#else
2907 struct timeval sNow;
drhbdcc2762007-04-02 18:06:57 +00002908 gettimeofday(&sNow, 0);
drh19e2d372005-08-29 23:00:03 +00002909 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
2910#endif
drhbbd42a62004-05-22 17:41:58 +00002911#ifdef SQLITE_TEST
2912 if( sqlite3_current_time ){
2913 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
2914 }
2915#endif
2916 return 0;
2917}
danielk1977b4b47412007-08-17 15:53:36 +00002918
danielk1977bcb97fe2008-06-06 15:49:29 +00002919static int unixGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
2920 return 0;
2921}
2922
drh153c62c2007-08-24 03:51:33 +00002923/*
danielk1977e339d652008-06-28 11:23:00 +00002924** Initialize the operating system interface.
drh153c62c2007-08-24 03:51:33 +00002925*/
danielk1977c0fa4c52008-06-25 17:19:00 +00002926int sqlite3_os_init(void){
danielk1977e339d652008-06-28 11:23:00 +00002927 /* Macro to define the static contents of an sqlite3_vfs structure for
2928 ** the unix backend. The two parameters are the values to use for
2929 ** the sqlite3_vfs.zName and sqlite3_vfs.pAppData fields, respectively.
2930 **
2931 */
2932 #define UNIXVFS(zVfsName, pVfsAppData) { \
2933 1, /* iVersion */ \
2934 sizeof(unixFile), /* szOsFile */ \
2935 MAX_PATHNAME, /* mxPathname */ \
2936 0, /* pNext */ \
2937 zVfsName, /* zName */ \
2938 (void *)pVfsAppData, /* pAppData */ \
2939 unixOpen, /* xOpen */ \
2940 unixDelete, /* xDelete */ \
2941 unixAccess, /* xAccess */ \
2942 unixFullPathname, /* xFullPathname */ \
2943 unixDlOpen, /* xDlOpen */ \
2944 unixDlError, /* xDlError */ \
2945 unixDlSym, /* xDlSym */ \
2946 unixDlClose, /* xDlClose */ \
2947 unixRandomness, /* xRandomness */ \
2948 unixSleep, /* xSleep */ \
2949 unixCurrentTime, /* xCurrentTime */ \
2950 unixGetLastError /* xGetLastError */ \
2951 }
2952
2953 static sqlite3_vfs unixVfs = UNIXVFS("unix", 0);
2954#ifdef SQLITE_ENABLE_LOCKING_STYLE
danielk1977e339d652008-06-28 11:23:00 +00002955 int i;
2956 static sqlite3_vfs aVfs[] = {
2957 UNIXVFS("unix-posix", LOCKING_STYLE_POSIX),
2958 UNIXVFS("unix-afp", LOCKING_STYLE_AFP),
2959 UNIXVFS("unix-flock", LOCKING_STYLE_FLOCK),
2960 UNIXVFS("unix-dotfile", LOCKING_STYLE_DOTFILE),
2961 UNIXVFS("unix-none", LOCKING_STYLE_NONE)
drh153c62c2007-08-24 03:51:33 +00002962 };
danielk1977e339d652008-06-28 11:23:00 +00002963 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
2964 sqlite3_vfs_register(&aVfs[i], 0);
2965 }
2966#endif
danielk1977c0fa4c52008-06-25 17:19:00 +00002967 sqlite3_vfs_register(&unixVfs, 1);
2968 return SQLITE_OK;
drh153c62c2007-08-24 03:51:33 +00002969}
danielk1977e339d652008-06-28 11:23:00 +00002970
2971/*
2972** Shutdown the operating system interface. This is a no-op for unix.
2973*/
danielk1977c0fa4c52008-06-25 17:19:00 +00002974int sqlite3_os_end(void){
2975 return SQLITE_OK;
2976}
drhdce8bdb2007-08-16 13:01:44 +00002977
danielk197729bafea2008-06-26 10:41:19 +00002978#endif /* SQLITE_OS_UNIX */