2001-09-18 00:25:58 +04:00
|
|
|
/*
|
2004-05-22 21:41:58 +04:00
|
|
|
** 2004 May 22
|
2001-09-18 00:25:58 +04:00
|
|
|
**
|
|
|
|
** The author disclaims copyright to this source code. In place of
|
|
|
|
** a legal notice, here is a blessing:
|
|
|
|
**
|
|
|
|
** May you do good and not evil.
|
|
|
|
** May you find forgiveness for yourself and forgive others.
|
|
|
|
** May you share freely, never taking more than you give.
|
|
|
|
**
|
|
|
|
******************************************************************************
|
|
|
|
**
|
2004-05-22 21:41:58 +04:00
|
|
|
** This file contains code that is specific to Unix systems.
|
2001-09-18 00:25:58 +04:00
|
|
|
*/
|
|
|
|
#include "sqliteInt.h"
|
2004-10-01 06:00:31 +04:00
|
|
|
#include "os.h"
|
|
|
|
#if OS_UNIX /* This file is used on unix only */
|
2001-09-18 00:25:58 +04:00
|
|
|
|
2003-04-17 06:54:13 +04:00
|
|
|
|
2004-05-22 21:41:58 +04:00
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#ifndef O_LARGEFILE
|
|
|
|
# define O_LARGEFILE 0
|
2001-09-18 00:25:58 +04:00
|
|
|
#endif
|
2004-05-22 21:41:58 +04:00
|
|
|
#ifdef SQLITE_DISABLE_LFS
|
|
|
|
# undef O_LARGEFILE
|
|
|
|
# define O_LARGEFILE 0
|
|
|
|
#endif
|
|
|
|
#ifndef O_NOFOLLOW
|
|
|
|
# define O_NOFOLLOW 0
|
|
|
|
#endif
|
|
|
|
#ifndef O_BINARY
|
|
|
|
# define O_BINARY 0
|
2003-01-08 16:02:52 +03:00
|
|
|
#endif
|
|
|
|
|
2004-10-01 06:00:31 +04:00
|
|
|
|
2003-04-17 06:54:13 +04:00
|
|
|
/*
|
|
|
|
** The DJGPP compiler environment looks mostly like Unix, but it
|
|
|
|
** lacks the fcntl() system call. So redefine fcntl() to be something
|
|
|
|
** that always succeeds. This means that locking does not occur under
|
|
|
|
** DJGPP. But its DOS - what did you expect?
|
|
|
|
*/
|
|
|
|
#ifdef __DJGPP__
|
|
|
|
# define fcntl(A,B,C) 0
|
|
|
|
#endif
|
|
|
|
|
2003-12-19 05:52:05 +03:00
|
|
|
/*
|
|
|
|
** Macros used to determine whether or not to use threads. The
|
|
|
|
** SQLITE_UNIX_THREADS macro is defined if we are synchronizing for
|
|
|
|
** Posix threads and SQLITE_W32_THREADS is defined if we are
|
|
|
|
** synchronizing using Win32 threads.
|
|
|
|
*/
|
2004-05-22 21:41:58 +04:00
|
|
|
#if defined(THREADSAFE) && THREADSAFE
|
2003-12-19 05:52:05 +03:00
|
|
|
# include <pthread.h>
|
|
|
|
# define SQLITE_UNIX_THREADS 1
|
|
|
|
#endif
|
2004-05-22 21:41:58 +04:00
|
|
|
|
2003-12-19 05:52:05 +03:00
|
|
|
|
2002-03-05 04:11:12 +03:00
|
|
|
/*
|
2004-05-22 21:41:58 +04:00
|
|
|
** Include code that is common to all os_*.c files
|
2002-03-05 04:11:12 +03:00
|
|
|
*/
|
2004-05-22 21:41:58 +04:00
|
|
|
#include "os_common.h"
|
2002-03-05 04:11:12 +03:00
|
|
|
|
2004-07-20 18:06:51 +04:00
|
|
|
#if defined(THREADSAFE) && THREADSAFE && defined(__linux__)
|
2004-06-03 20:08:41 +04:00
|
|
|
#define getpid pthread_self
|
|
|
|
#endif
|
|
|
|
|
2001-10-09 16:39:24 +04:00
|
|
|
/*
|
|
|
|
** Here is the dirt on POSIX advisory locks: ANSI STD 1003.1 (1996)
|
|
|
|
** section 6.5.2.2 lines 483 through 490 specify that when a process
|
|
|
|
** sets or clears a lock, that operation overrides any prior locks set
|
|
|
|
** by the same process. It does not explicitly say so, but this implies
|
|
|
|
** that it overrides locks set by the same process using a different
|
|
|
|
** file descriptor. Consider this test case:
|
|
|
|
**
|
|
|
|
** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
|
|
|
|
** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
|
|
|
|
**
|
2003-06-28 20:20:22 +04:00
|
|
|
** Suppose ./file1 and ./file2 are really the same file (because
|
2001-10-09 16:39:24 +04:00
|
|
|
** one is a hard or symbolic link to the other) then if you set
|
|
|
|
** an exclusive lock on fd1, then try to get an exclusive lock
|
|
|
|
** on fd2, it works. I would have expected the second lock to
|
|
|
|
** fail since there was already a lock on the file due to fd1.
|
|
|
|
** But not so. Since both locks came from the same process, the
|
|
|
|
** second overrides the first, even though they were on different
|
|
|
|
** file descriptors opened on different file names.
|
|
|
|
**
|
|
|
|
** Bummer. If you ask me, this is broken. Badly broken. It means
|
|
|
|
** that we cannot use POSIX locks to synchronize file access among
|
|
|
|
** competing threads of the same process. POSIX locks will work fine
|
|
|
|
** to synchronize access for threads in separate processes, but not
|
|
|
|
** threads within the same process.
|
|
|
|
**
|
|
|
|
** To work around the problem, SQLite has to manage file locks internally
|
|
|
|
** on its own. Whenever a new database is opened, we have to find the
|
|
|
|
** specific inode of the database file (the inode is determined by the
|
2001-10-15 04:44:35 +04:00
|
|
|
** st_dev and st_ino fields of the stat structure that fstat() fills in)
|
2001-10-09 16:39:24 +04:00
|
|
|
** and check for locks already existing on that inode. When locks are
|
|
|
|
** created or removed, we have to look at our own internal record of the
|
|
|
|
** locks to see if another thread has previously set a lock on that same
|
|
|
|
** inode.
|
|
|
|
**
|
|
|
|
** The OsFile structure for POSIX is no longer just an integer file
|
|
|
|
** descriptor. It is now a structure that holds the integer file
|
|
|
|
** descriptor and a pointer to a structure that describes the internal
|
|
|
|
** locks on the corresponding inode. There is one locking structure
|
|
|
|
** per inode, so if the same inode is opened twice, both OsFile structures
|
|
|
|
** point to the same locking structure. The locking structure keeps
|
|
|
|
** a reference count (so we will know when to delete it) and a "cnt"
|
|
|
|
** field that tells us its internal lock status. cnt==0 means the
|
|
|
|
** file is unlocked. cnt==-1 means the file has an exclusive lock.
|
|
|
|
** cnt>0 means there are cnt shared locks on the file.
|
|
|
|
**
|
|
|
|
** Any attempt to lock or unlock a file first checks the locking
|
2002-10-20 22:19:45 +04:00
|
|
|
** structure. The fcntl() system call is only invoked to set a
|
2001-10-09 16:39:24 +04:00
|
|
|
** POSIX lock if the internal lock structure transitions between
|
|
|
|
** a locked and an unlocked state.
|
2004-01-12 03:39:05 +03:00
|
|
|
**
|
|
|
|
** 2004-Jan-11:
|
|
|
|
** More recent discoveries about POSIX advisory locks. (The more
|
|
|
|
** I discover, the more I realize the a POSIX advisory locks are
|
|
|
|
** an abomination.)
|
|
|
|
**
|
|
|
|
** If you close a file descriptor that points to a file that has locks,
|
|
|
|
** all locks on that file that are owned by the current process are
|
|
|
|
** released. To work around this problem, each OsFile structure contains
|
|
|
|
** a pointer to an openCnt structure. There is one openCnt structure
|
|
|
|
** per open inode, which means that multiple OsFiles can point to a single
|
|
|
|
** openCnt. When an attempt is made to close an OsFile, if there are
|
|
|
|
** other OsFiles open on the same inode that are holding locks, the call
|
|
|
|
** to close() the file descriptor is deferred until all of the locks clear.
|
|
|
|
** The openCnt structure keeps a list of file descriptors that need to
|
|
|
|
** be closed and that list is walked (and cleared) when the last lock
|
|
|
|
** clears.
|
|
|
|
**
|
|
|
|
** First, under Linux threads, because each thread has a separate
|
|
|
|
** process ID, lock operations in one thread do not override locks
|
|
|
|
** to the same file in other threads. Linux threads behave like
|
|
|
|
** separate processes in this respect. But, if you close a file
|
|
|
|
** descriptor in linux threads, all locks are cleared, even locks
|
|
|
|
** on other threads and even though the other threads have different
|
|
|
|
** process IDs. Linux threads is inconsistent in this respect.
|
|
|
|
** (I'm beginning to think that linux threads is an abomination too.)
|
|
|
|
** The consequence of this all is that the hash table for the lockInfo
|
|
|
|
** structure has to include the process id as part of its key because
|
|
|
|
** locks in different threads are treated as distinct. But the
|
|
|
|
** openCnt structure should not include the process id in its
|
|
|
|
** key because close() clears lock on all threads, not just the current
|
|
|
|
** thread. Were it not for this goofiness in linux threads, we could
|
|
|
|
** combine the lockInfo and openCnt structures into a single structure.
|
2004-06-29 07:29:00 +04:00
|
|
|
**
|
|
|
|
** 2004-Jun-28:
|
|
|
|
** On some versions of linux, threads can override each others locks.
|
|
|
|
** On others not. Sometimes you can change the behavior on the same
|
|
|
|
** system by setting the LD_ASSUME_KERNEL environment variable. The
|
|
|
|
** POSIX standard is silent as to which behavior is correct, as far
|
|
|
|
** as I can tell, so other versions of unix might show the same
|
|
|
|
** inconsistency. There is no little doubt in my mind that posix
|
|
|
|
** advisory locks and linux threads are profoundly broken.
|
|
|
|
**
|
|
|
|
** To work around the inconsistencies, we have to test at runtime
|
|
|
|
** whether or not threads can override each others locks. This test
|
|
|
|
** is run once, the first time any lock is attempted. A static
|
|
|
|
** variable is set to record the results of this test for future
|
|
|
|
** use.
|
2001-10-09 16:39:24 +04:00
|
|
|
*/
|
|
|
|
|
2001-10-09 08:19:46 +04:00
|
|
|
/*
|
|
|
|
** An instance of the following structure serves as the key used
|
2004-06-29 07:29:00 +04:00
|
|
|
** to locate a particular lockInfo structure given its inode.
|
|
|
|
**
|
|
|
|
** If threads cannot override each others locks, then we set the
|
|
|
|
** lockKey.tid field to the thread ID. If threads can override
|
|
|
|
** each others locks then tid is always set to zero. tid is also
|
|
|
|
** set to zero if we compile without threading support.
|
2001-10-09 08:19:46 +04:00
|
|
|
*/
|
2003-12-19 11:40:22 +03:00
|
|
|
struct lockKey {
|
2004-06-29 07:29:00 +04:00
|
|
|
dev_t dev; /* Device number */
|
|
|
|
ino_t ino; /* Inode number */
|
|
|
|
#ifdef SQLITE_UNIX_THREADS
|
|
|
|
pthread_t tid; /* Thread ID or zero if threads cannot override each other */
|
|
|
|
#endif
|
2001-10-09 08:19:46 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2004-01-12 03:39:05 +03:00
|
|
|
** An instance of the following structure is allocated for each open
|
|
|
|
** inode on each thread with a different process ID. (Threads have
|
|
|
|
** different process IDs on linux, but not on most other unixes.)
|
|
|
|
**
|
2001-10-09 16:39:24 +04:00
|
|
|
** A single inode can have multiple file descriptors, so each OsFile
|
|
|
|
** structure contains a pointer to an instance of this object and this
|
|
|
|
** object keeps a count of the number of OsFiles pointing to it.
|
2001-10-09 08:19:46 +04:00
|
|
|
*/
|
|
|
|
struct lockInfo {
|
2003-12-19 11:40:22 +03:00
|
|
|
struct lockKey key; /* The lookup key */
|
2004-06-07 20:27:46 +04:00
|
|
|
int cnt; /* Number of SHARED locks held */
|
2004-06-01 18:09:28 +04:00
|
|
|
int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
|
2004-01-12 03:39:05 +03:00
|
|
|
int nRef; /* Number of pointers to this structure */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** An instance of the following structure serves as the key used
|
|
|
|
** to locate a particular openCnt structure given its inode. This
|
2004-06-29 07:29:00 +04:00
|
|
|
** is the same as the lockKey except that the thread ID is omitted.
|
2004-01-12 03:39:05 +03:00
|
|
|
*/
|
|
|
|
struct openKey {
|
|
|
|
dev_t dev; /* Device number */
|
|
|
|
ino_t ino; /* Inode number */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** An instance of the following structure is allocated for each open
|
|
|
|
** inode. This structure keeps track of the number of locks on that
|
|
|
|
** inode. If a close is attempted against an inode that is holding
|
|
|
|
** locks, the close is deferred until all locks clear by adding the
|
|
|
|
** file descriptor to be closed to the pending list.
|
|
|
|
*/
|
|
|
|
struct openCnt {
|
|
|
|
struct openKey key; /* The lookup key */
|
2001-10-09 08:19:46 +04:00
|
|
|
int nRef; /* Number of pointers to this structure */
|
2004-01-12 03:39:05 +03:00
|
|
|
int nLock; /* Number of outstanding locks */
|
|
|
|
int nPending; /* Number of pending close() operations */
|
|
|
|
int *aPending; /* Malloced space holding fd's awaiting a close() */
|
2001-10-09 08:19:46 +04:00
|
|
|
};
|
|
|
|
|
2002-10-20 22:19:45 +04:00
|
|
|
/*
|
2004-01-12 03:39:05 +03:00
|
|
|
** These hash table maps inodes and process IDs into lockInfo and openCnt
|
|
|
|
** structures. Access to these hash tables must be protected by a mutex.
|
2001-10-09 08:19:46 +04:00
|
|
|
*/
|
|
|
|
static Hash lockHash = { SQLITE_HASH_BINARY, 0, 0, 0, 0, 0 };
|
2004-01-12 03:39:05 +03:00
|
|
|
static Hash openHash = { SQLITE_HASH_BINARY, 0, 0, 0, 0, 0 };
|
|
|
|
|
2004-06-29 07:29:00 +04:00
|
|
|
|
|
|
|
#ifdef SQLITE_UNIX_THREADS
|
|
|
|
/*
|
|
|
|
** This variable records whether or not threads can override each others
|
|
|
|
** locks.
|
|
|
|
**
|
|
|
|
** 0: No. Threads cannot override each others locks.
|
|
|
|
** 1: Yes. Threads can override each others locks.
|
|
|
|
** -1: We don't know yet.
|
|
|
|
*/
|
|
|
|
static int threadsOverrideEachOthersLocks = -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This structure holds information passed into individual test
|
|
|
|
** threads by the testThreadLockingBehavior() routine.
|
|
|
|
*/
|
|
|
|
struct threadTestData {
|
|
|
|
int fd; /* File to be locked */
|
|
|
|
struct flock lock; /* The locking operation */
|
|
|
|
int result; /* Result of the locking operation */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The testThreadLockingBehavior() routine launches two separate
|
|
|
|
** threads on this routine. This routine attempts to lock a file
|
|
|
|
** descriptor then returns. The success or failure of that attempt
|
|
|
|
** allows the testThreadLockingBehavior() procedure to determine
|
|
|
|
** whether or not threads can override each others locks.
|
|
|
|
*/
|
|
|
|
static void *threadLockingTest(void *pArg){
|
|
|
|
struct threadTestData *pData = (struct threadTestData*)pArg;
|
|
|
|
pData->result = fcntl(pData->fd, F_SETLK, &pData->lock);
|
|
|
|
return pArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This procedure attempts to determine whether or not threads
|
|
|
|
** can override each others locks then sets the
|
|
|
|
** threadsOverrideEachOthersLocks variable appropriately.
|
|
|
|
*/
|
|
|
|
static void testThreadLockingBehavior(fd_orig){
|
|
|
|
int fd;
|
|
|
|
struct threadTestData d[2];
|
|
|
|
pthread_t t[2];
|
|
|
|
|
|
|
|
fd = dup(fd_orig);
|
|
|
|
if( fd<0 ) return;
|
|
|
|
memset(d, 0, sizeof(d));
|
|
|
|
d[0].fd = fd;
|
|
|
|
d[0].lock.l_type = F_RDLCK;
|
|
|
|
d[0].lock.l_len = 1;
|
|
|
|
d[0].lock.l_start = 0;
|
|
|
|
d[0].lock.l_whence = SEEK_SET;
|
|
|
|
d[1] = d[0];
|
|
|
|
d[1].lock.l_type = F_WRLCK;
|
|
|
|
pthread_create(&t[0], 0, threadLockingTest, &d[0]);
|
|
|
|
pthread_create(&t[1], 0, threadLockingTest, &d[1]);
|
|
|
|
pthread_join(t[0], 0);
|
|
|
|
pthread_join(t[1], 0);
|
|
|
|
close(fd);
|
|
|
|
threadsOverrideEachOthersLocks = d[0].result==0 && d[1].result==0;
|
|
|
|
}
|
|
|
|
#endif /* SQLITE_UNIX_THREADS */
|
|
|
|
|
2004-01-12 03:39:05 +03:00
|
|
|
/*
|
|
|
|
** Release a lockInfo structure previously allocated by findLockInfo().
|
|
|
|
*/
|
|
|
|
static void releaseLockInfo(struct lockInfo *pLock){
|
|
|
|
pLock->nRef--;
|
|
|
|
if( pLock->nRef==0 ){
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3HashInsert(&lockHash, &pLock->key, sizeof(pLock->key), 0);
|
2004-01-12 03:39:05 +03:00
|
|
|
sqliteFree(pLock);
|
|
|
|
}
|
|
|
|
}
|
2001-10-09 08:19:46 +04:00
|
|
|
|
|
|
|
/*
|
2004-01-12 03:39:05 +03:00
|
|
|
** Release a openCnt structure previously allocated by findLockInfo().
|
|
|
|
*/
|
|
|
|
static void releaseOpenCnt(struct openCnt *pOpen){
|
|
|
|
pOpen->nRef--;
|
|
|
|
if( pOpen->nRef==0 ){
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3HashInsert(&openHash, &pOpen->key, sizeof(pOpen->key), 0);
|
2004-01-12 03:39:05 +03:00
|
|
|
sqliteFree(pOpen->aPending);
|
|
|
|
sqliteFree(pOpen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Given a file descriptor, locate lockInfo and openCnt structures that
|
|
|
|
** describes that file descriptor. Create a new ones if necessary. The
|
|
|
|
** return values might be unset if an error occurs.
|
|
|
|
**
|
|
|
|
** Return the number of errors.
|
2001-10-09 08:19:46 +04:00
|
|
|
*/
|
2004-06-18 21:10:16 +04:00
|
|
|
static int findLockInfo(
|
2004-01-12 03:39:05 +03:00
|
|
|
int fd, /* The file descriptor used in the key */
|
|
|
|
struct lockInfo **ppLock, /* Return the lockInfo structure here */
|
2004-06-29 07:29:00 +04:00
|
|
|
struct openCnt **ppOpen /* Return the openCnt structure here */
|
2004-01-12 03:39:05 +03:00
|
|
|
){
|
2001-10-09 08:19:46 +04:00
|
|
|
int rc;
|
2004-01-12 03:39:05 +03:00
|
|
|
struct lockKey key1;
|
|
|
|
struct openKey key2;
|
2001-10-09 08:19:46 +04:00
|
|
|
struct stat statbuf;
|
2004-01-12 03:39:05 +03:00
|
|
|
struct lockInfo *pLock;
|
|
|
|
struct openCnt *pOpen;
|
2001-10-09 08:19:46 +04:00
|
|
|
rc = fstat(fd, &statbuf);
|
2004-01-12 03:39:05 +03:00
|
|
|
if( rc!=0 ) return 1;
|
|
|
|
memset(&key1, 0, sizeof(key1));
|
|
|
|
key1.dev = statbuf.st_dev;
|
|
|
|
key1.ino = statbuf.st_ino;
|
2004-06-29 07:29:00 +04:00
|
|
|
#ifdef SQLITE_UNIX_THREADS
|
|
|
|
if( threadsOverrideEachOthersLocks<0 ){
|
|
|
|
testThreadLockingBehavior(fd);
|
|
|
|
}
|
|
|
|
key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
|
|
|
|
#endif
|
2004-01-12 03:39:05 +03:00
|
|
|
memset(&key2, 0, sizeof(key2));
|
|
|
|
key2.dev = statbuf.st_dev;
|
|
|
|
key2.ino = statbuf.st_ino;
|
2004-05-08 12:23:19 +04:00
|
|
|
pLock = (struct lockInfo*)sqlite3HashFind(&lockHash, &key1, sizeof(key1));
|
2004-01-12 03:39:05 +03:00
|
|
|
if( pLock==0 ){
|
2001-10-22 06:58:08 +04:00
|
|
|
struct lockInfo *pOld;
|
2004-01-12 03:39:05 +03:00
|
|
|
pLock = sqliteMallocRaw( sizeof(*pLock) );
|
|
|
|
if( pLock==0 ) return 1;
|
|
|
|
pLock->key = key1;
|
|
|
|
pLock->nRef = 1;
|
|
|
|
pLock->cnt = 0;
|
2004-06-01 18:09:28 +04:00
|
|
|
pLock->locktype = 0;
|
2004-05-08 12:23:19 +04:00
|
|
|
pOld = sqlite3HashInsert(&lockHash, &pLock->key, sizeof(key1), pLock);
|
2001-10-22 06:58:08 +04:00
|
|
|
if( pOld!=0 ){
|
2004-01-12 03:39:05 +03:00
|
|
|
assert( pOld==pLock );
|
|
|
|
sqliteFree(pLock);
|
|
|
|
return 1;
|
2001-10-22 06:58:08 +04:00
|
|
|
}
|
2001-10-09 08:19:46 +04:00
|
|
|
}else{
|
2004-01-12 03:39:05 +03:00
|
|
|
pLock->nRef++;
|
2001-10-09 08:19:46 +04:00
|
|
|
}
|
2004-01-12 03:39:05 +03:00
|
|
|
*ppLock = pLock;
|
2004-05-08 12:23:19 +04:00
|
|
|
pOpen = (struct openCnt*)sqlite3HashFind(&openHash, &key2, sizeof(key2));
|
2004-01-12 03:39:05 +03:00
|
|
|
if( pOpen==0 ){
|
|
|
|
struct openCnt *pOld;
|
|
|
|
pOpen = sqliteMallocRaw( sizeof(*pOpen) );
|
|
|
|
if( pOpen==0 ){
|
|
|
|
releaseLockInfo(pLock);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
pOpen->key = key2;
|
|
|
|
pOpen->nRef = 1;
|
|
|
|
pOpen->nLock = 0;
|
|
|
|
pOpen->nPending = 0;
|
|
|
|
pOpen->aPending = 0;
|
2004-05-08 12:23:19 +04:00
|
|
|
pOld = sqlite3HashInsert(&openHash, &pOpen->key, sizeof(key2), pOpen);
|
2004-01-12 03:39:05 +03:00
|
|
|
if( pOld!=0 ){
|
|
|
|
assert( pOld==pOpen );
|
|
|
|
sqliteFree(pOpen);
|
|
|
|
releaseLockInfo(pLock);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
pOpen->nRef++;
|
2001-10-09 08:19:46 +04:00
|
|
|
}
|
2004-01-12 03:39:05 +03:00
|
|
|
*ppOpen = pOpen;
|
|
|
|
return 0;
|
2001-10-09 08:19:46 +04:00
|
|
|
}
|
2004-01-12 03:39:05 +03:00
|
|
|
|
2001-09-19 17:22:39 +04:00
|
|
|
/*
|
|
|
|
** Delete the named file
|
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
int sqlite3OsDelete(const char *zFilename){
|
2001-09-19 17:22:39 +04:00
|
|
|
unlink(zFilename);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return TRUE if the named file exists.
|
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
int sqlite3OsFileExists(const char *zFilename){
|
2003-01-08 16:02:52 +03:00
|
|
|
return access(zFilename, 0)==0;
|
2001-09-19 17:22:39 +04:00
|
|
|
}
|
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
|
|
|
** Attempt to open a file for both reading and writing. If that
|
|
|
|
** fails, try opening it read-only. If the file does not exist,
|
|
|
|
** try to create it.
|
|
|
|
**
|
2001-12-14 18:09:55 +03:00
|
|
|
** On success, a handle for the open file is written to *id
|
2001-09-18 00:25:58 +04:00
|
|
|
** and *pReadonly is set to 0 if the file was opened for reading and
|
|
|
|
** writing or 1 if the file was opened read-only. The function returns
|
|
|
|
** SQLITE_OK.
|
|
|
|
**
|
|
|
|
** On failure, the function returns SQLITE_CANTOPEN and leaves
|
2002-12-01 05:00:57 +03:00
|
|
|
** *id and *pReadonly unchanged.
|
2001-09-18 00:25:58 +04:00
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
int sqlite3OsOpenReadWrite(
|
2001-09-19 17:22:39 +04:00
|
|
|
const char *zFilename,
|
2001-12-14 18:09:55 +03:00
|
|
|
OsFile *id,
|
2001-09-19 17:22:39 +04:00
|
|
|
int *pReadonly
|
|
|
|
){
|
2004-01-12 03:39:05 +03:00
|
|
|
int rc;
|
2004-06-21 22:14:45 +04:00
|
|
|
assert( !id->isOpen );
|
2003-07-27 22:59:42 +04:00
|
|
|
id->dirfd = -1;
|
2004-06-09 21:37:22 +04:00
|
|
|
id->h = open(zFilename, O_RDWR|O_CREAT|O_LARGEFILE|O_BINARY, 0644);
|
|
|
|
if( id->h<0 ){
|
2004-07-20 05:14:13 +04:00
|
|
|
#ifdef EISDIR
|
|
|
|
if( errno==EISDIR ){
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
}
|
|
|
|
#endif
|
2004-06-09 21:37:22 +04:00
|
|
|
id->h = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY);
|
|
|
|
if( id->h<0 ){
|
2002-10-20 22:19:45 +04:00
|
|
|
return SQLITE_CANTOPEN;
|
2001-09-18 00:25:58 +04:00
|
|
|
}
|
|
|
|
*pReadonly = 1;
|
|
|
|
}else{
|
|
|
|
*pReadonly = 0;
|
|
|
|
}
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3OsEnterMutex();
|
2004-06-09 21:37:22 +04:00
|
|
|
rc = findLockInfo(id->h, &id->pLock, &id->pOpen);
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3OsLeaveMutex();
|
2004-01-12 03:39:05 +03:00
|
|
|
if( rc ){
|
2004-06-09 21:37:22 +04:00
|
|
|
close(id->h);
|
2001-10-09 08:19:46 +04:00
|
|
|
return SQLITE_NOMEM;
|
|
|
|
}
|
2004-06-03 20:08:41 +04:00
|
|
|
id->locktype = 0;
|
2004-06-21 22:14:45 +04:00
|
|
|
id->isOpen = 1;
|
2004-06-09 21:37:22 +04:00
|
|
|
TRACE3("OPEN %-3d %s\n", id->h, zFilename);
|
2003-02-17 01:21:32 +03:00
|
|
|
OpenCounter(+1);
|
2001-09-18 00:25:58 +04:00
|
|
|
return SQLITE_OK;
|
2001-09-19 17:22:39 +04:00
|
|
|
}
|
2001-09-18 00:25:58 +04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Attempt to open a new file for exclusive access by this process.
|
|
|
|
** The file will be opened for both reading and writing. To avoid
|
|
|
|
** a potential security problem, we do not allow the file to have
|
|
|
|
** previously existed. Nor do we allow the file to be a symbolic
|
|
|
|
** link.
|
|
|
|
**
|
2002-02-02 18:01:15 +03:00
|
|
|
** If delFlag is true, then make arrangements to automatically delete
|
|
|
|
** the file when it is closed.
|
|
|
|
**
|
2001-12-14 18:09:55 +03:00
|
|
|
** On success, write the file handle into *id and return SQLITE_OK.
|
2001-09-18 00:25:58 +04:00
|
|
|
**
|
|
|
|
** On failure, return SQLITE_CANTOPEN.
|
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
int sqlite3OsOpenExclusive(const char *zFilename, OsFile *id, int delFlag){
|
2004-01-12 03:39:05 +03:00
|
|
|
int rc;
|
2004-06-21 22:14:45 +04:00
|
|
|
assert( !id->isOpen );
|
2001-09-18 00:25:58 +04:00
|
|
|
if( access(zFilename, 0)==0 ){
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
}
|
2003-07-27 22:59:42 +04:00
|
|
|
id->dirfd = -1;
|
2004-06-09 21:37:22 +04:00
|
|
|
id->h = open(zFilename,
|
2003-04-17 06:54:13 +04:00
|
|
|
O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_LARGEFILE|O_BINARY, 0600);
|
2004-06-09 21:37:22 +04:00
|
|
|
if( id->h<0 ){
|
2001-09-18 00:25:58 +04:00
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
}
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3OsEnterMutex();
|
2004-06-09 21:37:22 +04:00
|
|
|
rc = findLockInfo(id->h, &id->pLock, &id->pOpen);
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3OsLeaveMutex();
|
2004-01-12 03:39:05 +03:00
|
|
|
if( rc ){
|
2004-06-09 21:37:22 +04:00
|
|
|
close(id->h);
|
2001-10-22 06:58:08 +04:00
|
|
|
unlink(zFilename);
|
2001-10-09 08:19:46 +04:00
|
|
|
return SQLITE_NOMEM;
|
|
|
|
}
|
2004-06-03 20:08:41 +04:00
|
|
|
id->locktype = 0;
|
2004-06-21 22:14:45 +04:00
|
|
|
id->isOpen = 1;
|
2002-02-02 18:01:15 +03:00
|
|
|
if( delFlag ){
|
|
|
|
unlink(zFilename);
|
|
|
|
}
|
2004-06-09 21:37:22 +04:00
|
|
|
TRACE3("OPEN-EX %-3d %s\n", id->h, zFilename);
|
2003-02-17 01:21:32 +03:00
|
|
|
OpenCounter(+1);
|
2001-09-18 00:25:58 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2001-09-19 17:58:43 +04:00
|
|
|
/*
|
|
|
|
** Attempt to open a new file for read-only access.
|
|
|
|
**
|
2001-12-14 18:09:55 +03:00
|
|
|
** On success, write the file handle into *id and return SQLITE_OK.
|
2001-09-19 17:58:43 +04:00
|
|
|
**
|
|
|
|
** On failure, return SQLITE_CANTOPEN.
|
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
int sqlite3OsOpenReadOnly(const char *zFilename, OsFile *id){
|
2004-01-12 03:39:05 +03:00
|
|
|
int rc;
|
2004-06-21 22:14:45 +04:00
|
|
|
assert( !id->isOpen );
|
2003-07-27 22:59:42 +04:00
|
|
|
id->dirfd = -1;
|
2004-06-09 21:37:22 +04:00
|
|
|
id->h = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY);
|
|
|
|
if( id->h<0 ){
|
2001-09-19 17:58:43 +04:00
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
}
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3OsEnterMutex();
|
2004-06-09 21:37:22 +04:00
|
|
|
rc = findLockInfo(id->h, &id->pLock, &id->pOpen);
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3OsLeaveMutex();
|
2004-01-12 03:39:05 +03:00
|
|
|
if( rc ){
|
2004-06-09 21:37:22 +04:00
|
|
|
close(id->h);
|
2001-10-09 08:19:46 +04:00
|
|
|
return SQLITE_NOMEM;
|
|
|
|
}
|
2004-06-03 20:08:41 +04:00
|
|
|
id->locktype = 0;
|
2004-06-21 22:14:45 +04:00
|
|
|
id->isOpen = 1;
|
2004-06-09 21:37:22 +04:00
|
|
|
TRACE3("OPEN-RO %-3d %s\n", id->h, zFilename);
|
2003-02-17 01:21:32 +03:00
|
|
|
OpenCounter(+1);
|
2001-09-19 17:58:43 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2003-07-27 22:59:42 +04:00
|
|
|
/*
|
|
|
|
** Attempt to open a file descriptor for the directory that contains a
|
|
|
|
** file. This file descriptor can be used to fsync() the directory
|
|
|
|
** in order to make sure the creation of a new file is actually written
|
|
|
|
** to disk.
|
|
|
|
**
|
|
|
|
** This routine is only meaningful for Unix. It is a no-op under
|
|
|
|
** windows since windows does not support hard links.
|
|
|
|
**
|
|
|
|
** On success, a handle for a previously open file is at *id is
|
|
|
|
** updated with the new directory file descriptor and SQLITE_OK is
|
|
|
|
** returned.
|
|
|
|
**
|
|
|
|
** On failure, the function returns SQLITE_CANTOPEN and leaves
|
|
|
|
** *id unchanged.
|
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
int sqlite3OsOpenDirectory(
|
2003-07-27 22:59:42 +04:00
|
|
|
const char *zDirname,
|
|
|
|
OsFile *id
|
|
|
|
){
|
2004-06-21 22:14:45 +04:00
|
|
|
if( !id->isOpen ){
|
2003-07-27 22:59:42 +04:00
|
|
|
/* Do not open the directory if the corresponding file is not already
|
|
|
|
** open. */
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
}
|
|
|
|
assert( id->dirfd<0 );
|
|
|
|
id->dirfd = open(zDirname, O_RDONLY|O_BINARY, 0644);
|
|
|
|
if( id->dirfd<0 ){
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
}
|
|
|
|
TRACE3("OPENDIR %-3d %s\n", id->dirfd, zDirname);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2004-08-14 21:10:10 +04:00
|
|
|
/*
|
|
|
|
** If the following global variable points to a string which is the
|
|
|
|
** name of a directory, then that directory will be used to store
|
|
|
|
** temporary files.
|
|
|
|
*/
|
2004-12-20 22:01:32 +03:00
|
|
|
char *sqlite3_temp_directory = 0;
|
2004-08-14 21:10:10 +04:00
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
|
|
|
** Create a temporary file name in zBuf. zBuf must be big enough to
|
|
|
|
** hold at least SQLITE_TEMPNAME_SIZE characters.
|
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
int sqlite3OsTempFileName(char *zBuf){
|
2001-09-18 00:25:58 +04:00
|
|
|
static const char *azDirs[] = {
|
2004-08-14 21:10:10 +04:00
|
|
|
0,
|
2001-09-18 00:25:58 +04:00
|
|
|
"/var/tmp",
|
|
|
|
"/usr/tmp",
|
|
|
|
"/tmp",
|
2002-08-14 04:10:44 +04:00
|
|
|
".",
|
2001-09-18 00:25:58 +04:00
|
|
|
};
|
2004-10-06 19:41:16 +04:00
|
|
|
static const unsigned char zChars[] =
|
2001-09-18 00:25:58 +04:00
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"0123456789";
|
|
|
|
int i, j;
|
|
|
|
struct stat buf;
|
2001-09-19 17:22:39 +04:00
|
|
|
const char *zDir = ".";
|
2004-08-30 03:42:13 +04:00
|
|
|
azDirs[0] = sqlite3_temp_directory;
|
2001-09-18 00:25:58 +04:00
|
|
|
for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
|
2004-08-14 21:10:10 +04:00
|
|
|
if( azDirs[i]==0 ) continue;
|
2001-09-18 00:25:58 +04:00
|
|
|
if( stat(azDirs[i], &buf) ) continue;
|
|
|
|
if( !S_ISDIR(buf.st_mode) ) continue;
|
|
|
|
if( access(azDirs[i], 07) ) continue;
|
|
|
|
zDir = azDirs[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
do{
|
2003-01-08 16:02:52 +03:00
|
|
|
sprintf(zBuf, "%s/"TEMP_FILE_PREFIX, zDir);
|
2001-09-18 00:25:58 +04:00
|
|
|
j = strlen(zBuf);
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3Randomness(15, &zBuf[j]);
|
2004-02-11 12:46:30 +03:00
|
|
|
for(i=0; i<15; i++, j++){
|
|
|
|
zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
|
2001-09-18 00:25:58 +04:00
|
|
|
}
|
|
|
|
zBuf[j] = 0;
|
|
|
|
}while( access(zBuf,0)==0 );
|
2002-10-20 22:19:45 +04:00
|
|
|
return SQLITE_OK;
|
2001-09-18 00:25:58 +04:00
|
|
|
}
|
|
|
|
|
2005-01-08 18:44:25 +03:00
|
|
|
#ifndef SQLITE_OMIT_PAGER_PRAGMAS
|
2004-12-20 22:01:32 +03:00
|
|
|
/*
|
|
|
|
** Check that a given pathname is a directory and is writable
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
int sqlite3OsIsDirWritable(char *zBuf){
|
|
|
|
struct stat buf;
|
|
|
|
if( zBuf==0 ) return 0;
|
2005-01-08 18:44:25 +03:00
|
|
|
if( zBuf[0]==0 ) return 0;
|
2004-12-20 22:01:32 +03:00
|
|
|
if( stat(zBuf, &buf) ) return 0;
|
|
|
|
if( !S_ISDIR(buf.st_mode) ) return 0;
|
|
|
|
if( access(zBuf, 07) ) return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
2005-01-08 18:44:25 +03:00
|
|
|
#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
|
2004-12-20 22:01:32 +03:00
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
2001-11-22 03:01:27 +03:00
|
|
|
** Read data from a file into a buffer. Return SQLITE_OK if all
|
|
|
|
** bytes were read successfully and SQLITE_IOERR if anything goes
|
|
|
|
** wrong.
|
2001-09-18 00:25:58 +04:00
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
int sqlite3OsRead(OsFile *id, void *pBuf, int amt){
|
2001-09-19 17:22:39 +04:00
|
|
|
int got;
|
2004-06-21 22:14:45 +04:00
|
|
|
assert( id->isOpen );
|
2001-10-12 21:30:04 +04:00
|
|
|
SimulateIOError(SQLITE_IOERR);
|
2003-01-21 05:39:36 +03:00
|
|
|
TIMER_START;
|
2004-06-09 21:37:22 +04:00
|
|
|
got = read(id->h, pBuf, amt);
|
2003-01-21 05:39:36 +03:00
|
|
|
TIMER_END;
|
2004-08-04 18:44:33 +04:00
|
|
|
TRACE4("READ %-3d %7d %d\n", id->h, last_page, TIMER_ELAPSED);
|
2003-01-21 05:39:36 +03:00
|
|
|
SEEK(0);
|
2002-09-05 20:08:27 +04:00
|
|
|
/* if( got<0 ) got = 0; */
|
|
|
|
if( got==amt ){
|
|
|
|
return SQLITE_OK;
|
|
|
|
}else{
|
|
|
|
return SQLITE_IOERR;
|
|
|
|
}
|
2001-09-19 17:22:39 +04:00
|
|
|
}
|
2001-09-18 00:25:58 +04:00
|
|
|
|
|
|
|
/*
|
2001-09-19 17:22:39 +04:00
|
|
|
** Write data from a buffer into a file. Return SQLITE_OK on success
|
|
|
|
** or some other error code on failure.
|
2001-09-18 00:25:58 +04:00
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
int sqlite3OsWrite(OsFile *id, const void *pBuf, int amt){
|
2002-09-05 20:08:27 +04:00
|
|
|
int wrote = 0;
|
2004-06-21 22:14:45 +04:00
|
|
|
assert( id->isOpen );
|
2005-02-03 03:29:47 +03:00
|
|
|
assert( amt>0 );
|
2001-10-12 21:30:04 +04:00
|
|
|
SimulateIOError(SQLITE_IOERR);
|
2004-10-01 18:38:02 +04:00
|
|
|
SimulateDiskfullError;
|
2003-01-21 05:39:36 +03:00
|
|
|
TIMER_START;
|
2004-06-09 21:37:22 +04:00
|
|
|
while( amt>0 && (wrote = write(id->h, pBuf, amt))>0 ){
|
2002-09-05 20:08:27 +04:00
|
|
|
amt -= wrote;
|
|
|
|
pBuf = &((char*)pBuf)[wrote];
|
|
|
|
}
|
2003-01-21 05:39:36 +03:00
|
|
|
TIMER_END;
|
2004-08-04 18:44:33 +04:00
|
|
|
TRACE4("WRITE %-3d %7d %d\n", id->h, last_page, TIMER_ELAPSED);
|
2003-01-21 05:39:36 +03:00
|
|
|
SEEK(0);
|
2002-09-05 20:08:27 +04:00
|
|
|
if( amt>0 ){
|
|
|
|
return SQLITE_FULL;
|
|
|
|
}
|
2001-09-19 17:22:39 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
|
|
|
** Move the read/write pointer in a file.
|
|
|
|
*/
|
2004-10-01 06:00:31 +04:00
|
|
|
int sqlite3OsSeek(OsFile *id, i64 offset){
|
2004-06-21 22:14:45 +04:00
|
|
|
assert( id->isOpen );
|
2002-03-05 04:11:12 +03:00
|
|
|
SEEK(offset/1024 + 1);
|
2004-06-09 21:37:22 +04:00
|
|
|
lseek(id->h, offset, SEEK_SET);
|
2001-09-19 17:22:39 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2004-07-18 01:44:57 +04:00
|
|
|
/*
|
|
|
|
** The fsync() system call does not work as advertised on many
|
|
|
|
** unix systems. The following procedure is an attempt to make
|
|
|
|
** it work better.
|
2005-01-20 02:24:50 +03:00
|
|
|
**
|
|
|
|
** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
|
|
|
|
** for testing when we want to run through the test suite quickly.
|
|
|
|
** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
|
|
|
|
** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
|
|
|
|
** or power failure will likely corrupt the database file.
|
2004-07-18 01:44:57 +04:00
|
|
|
*/
|
|
|
|
static int full_fsync(int fd){
|
2005-01-20 02:24:50 +03:00
|
|
|
#ifdef SQLITE_NO_SYNC
|
|
|
|
return SQLITE_OK;
|
|
|
|
#else
|
2004-07-18 01:44:57 +04:00
|
|
|
int rc;
|
|
|
|
#ifdef F_FULLFSYNC
|
|
|
|
rc = fcntl(fd, F_FULLFSYNC, 0);
|
|
|
|
if( rc ) rc = fsync(fd);
|
|
|
|
#else
|
|
|
|
rc = fsync(fd);
|
|
|
|
#endif
|
|
|
|
return rc;
|
2005-01-20 02:24:50 +03:00
|
|
|
#endif
|
2004-07-18 01:44:57 +04:00
|
|
|
}
|
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
|
|
|
** Make sure all writes to a particular file are committed to disk.
|
2003-07-27 22:59:42 +04:00
|
|
|
**
|
|
|
|
** Under Unix, also make sure that the directory entry for the file
|
|
|
|
** has been created by fsync-ing the directory that contains the file.
|
|
|
|
** If we do not do this and we encounter a power failure, the directory
|
|
|
|
** entry for the journal might not exist after we reboot. The next
|
|
|
|
** SQLite to access the file will not know that the journal exists (because
|
|
|
|
** the directory entry for the journal was never created) and the transaction
|
|
|
|
** will not roll back - possibly leading to database corruption.
|
2001-09-18 00:25:58 +04:00
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
int sqlite3OsSync(OsFile *id){
|
2004-06-21 22:14:45 +04:00
|
|
|
assert( id->isOpen );
|
2001-10-12 21:30:04 +04:00
|
|
|
SimulateIOError(SQLITE_IOERR);
|
2004-06-09 21:37:22 +04:00
|
|
|
TRACE2("SYNC %-3d\n", id->h);
|
2004-07-18 01:44:57 +04:00
|
|
|
if( full_fsync(id->h) ){
|
2002-09-05 20:08:27 +04:00
|
|
|
return SQLITE_IOERR;
|
|
|
|
}
|
2004-06-17 23:04:17 +04:00
|
|
|
if( id->dirfd>=0 ){
|
|
|
|
TRACE2("DIRSYNC %-3d\n", id->dirfd);
|
2004-07-18 01:44:57 +04:00
|
|
|
full_fsync(id->dirfd);
|
2004-06-17 23:04:17 +04:00
|
|
|
close(id->dirfd); /* Only need to sync once, so close the directory */
|
|
|
|
id->dirfd = -1; /* when we are done. */
|
|
|
|
}
|
|
|
|
return SQLITE_OK;
|
2001-09-19 17:22:39 +04:00
|
|
|
}
|
|
|
|
|
2004-06-14 13:35:16 +04:00
|
|
|
/*
|
|
|
|
** Sync the directory zDirname. This is a no-op on operating systems other
|
|
|
|
** than UNIX.
|
|
|
|
*/
|
|
|
|
int sqlite3OsSyncDirectory(const char *zDirname){
|
|
|
|
int fd;
|
|
|
|
int r;
|
2004-06-15 15:40:04 +04:00
|
|
|
SimulateIOError(SQLITE_IOERR);
|
2004-06-14 13:35:16 +04:00
|
|
|
fd = open(zDirname, O_RDONLY|O_BINARY, 0644);
|
2004-06-15 15:40:04 +04:00
|
|
|
TRACE3("DIRSYNC %-3d (%s)\n", fd, zDirname);
|
2004-06-14 13:35:16 +04:00
|
|
|
if( fd<0 ){
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
}
|
|
|
|
r = fsync(fd);
|
|
|
|
close(fd);
|
|
|
|
return ((r==0)?SQLITE_OK:SQLITE_IOERR);
|
|
|
|
}
|
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
|
|
|
** Truncate an open file to a specified size
|
|
|
|
*/
|
2004-10-01 06:00:31 +04:00
|
|
|
int sqlite3OsTruncate(OsFile *id, i64 nByte){
|
2004-06-21 22:14:45 +04:00
|
|
|
assert( id->isOpen );
|
2001-10-12 21:30:04 +04:00
|
|
|
SimulateIOError(SQLITE_IOERR);
|
2004-06-09 21:37:22 +04:00
|
|
|
return ftruncate(id->h, nByte)==0 ? SQLITE_OK : SQLITE_IOERR;
|
2001-09-19 17:22:39 +04:00
|
|
|
}
|
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
|
|
|
** Determine the current size of a file in bytes
|
|
|
|
*/
|
2004-10-01 06:00:31 +04:00
|
|
|
int sqlite3OsFileSize(OsFile *id, i64 *pSize){
|
2001-09-19 17:22:39 +04:00
|
|
|
struct stat buf;
|
2004-06-21 22:14:45 +04:00
|
|
|
assert( id->isOpen );
|
2001-10-12 21:30:04 +04:00
|
|
|
SimulateIOError(SQLITE_IOERR);
|
2004-06-09 21:37:22 +04:00
|
|
|
if( fstat(id->h, &buf)!=0 ){
|
2001-09-19 17:22:39 +04:00
|
|
|
return SQLITE_IOERR;
|
|
|
|
}
|
|
|
|
*pSize = buf.st_size;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2004-06-03 20:08:41 +04:00
|
|
|
/*
|
|
|
|
** This routine checks if there is a RESERVED lock held on the specified
|
|
|
|
** file by this or any other process. If such a lock is held, return
|
2004-06-07 20:27:46 +04:00
|
|
|
** non-zero. If the file is unlocked or holds only SHARED locks, then
|
|
|
|
** return zero.
|
2004-06-03 20:08:41 +04:00
|
|
|
*/
|
2004-06-09 21:37:22 +04:00
|
|
|
int sqlite3OsCheckReservedLock(OsFile *id){
|
2004-06-03 20:08:41 +04:00
|
|
|
int r = 0;
|
|
|
|
|
2004-06-21 22:14:45 +04:00
|
|
|
assert( id->isOpen );
|
2004-06-07 20:27:46 +04:00
|
|
|
sqlite3OsEnterMutex(); /* Needed because id->pLock is shared across threads */
|
2004-06-03 20:08:41 +04:00
|
|
|
|
|
|
|
/* Check if a thread in this process holds such a lock */
|
|
|
|
if( id->pLock->locktype>SHARED_LOCK ){
|
|
|
|
r = 1;
|
|
|
|
}
|
|
|
|
|
2004-06-07 20:27:46 +04:00
|
|
|
/* Otherwise see if some other process holds it.
|
2004-06-03 20:08:41 +04:00
|
|
|
*/
|
|
|
|
if( !r ){
|
|
|
|
struct flock lock;
|
|
|
|
lock.l_whence = SEEK_SET;
|
2004-06-07 20:27:46 +04:00
|
|
|
lock.l_start = RESERVED_BYTE;
|
|
|
|
lock.l_len = 1;
|
|
|
|
lock.l_type = F_WRLCK;
|
2004-06-09 21:37:22 +04:00
|
|
|
fcntl(id->h, F_GETLK, &lock);
|
2004-06-03 20:08:41 +04:00
|
|
|
if( lock.l_type!=F_UNLCK ){
|
|
|
|
r = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlite3OsLeaveMutex();
|
2004-06-09 21:37:22 +04:00
|
|
|
TRACE3("TEST WR-LOCK %d %d\n", id->h, r);
|
2004-06-03 20:08:41 +04:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2004-06-29 11:45:33 +04:00
|
|
|
#ifdef SQLITE_DEBUG
|
|
|
|
/*
|
|
|
|
** Helper function for printing out trace information from debugging
|
|
|
|
** binaries. This returns the string represetation of the supplied
|
|
|
|
** integer lock-type.
|
|
|
|
*/
|
|
|
|
static const char * locktypeName(int locktype){
|
|
|
|
switch( locktype ){
|
|
|
|
case NO_LOCK: return "NONE";
|
|
|
|
case SHARED_LOCK: return "SHARED";
|
|
|
|
case RESERVED_LOCK: return "RESERVED";
|
|
|
|
case PENDING_LOCK: return "PENDING";
|
|
|
|
case EXCLUSIVE_LOCK: return "EXCLUSIVE";
|
|
|
|
}
|
|
|
|
return "ERROR";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-06-01 18:09:28 +04:00
|
|
|
/*
|
|
|
|
** Lock the file with the lock specified by parameter locktype - one
|
|
|
|
** of the following:
|
|
|
|
**
|
2004-06-07 20:27:46 +04:00
|
|
|
** (1) SHARED_LOCK
|
|
|
|
** (2) RESERVED_LOCK
|
|
|
|
** (3) PENDING_LOCK
|
|
|
|
** (4) EXCLUSIVE_LOCK
|
|
|
|
**
|
2004-06-08 04:47:47 +04:00
|
|
|
** Sometimes when requesting one lock state, additional lock states
|
|
|
|
** are inserted in between. The locking might fail on one of the later
|
|
|
|
** transitions leaving the lock state different from what it started but
|
|
|
|
** still short of its goal. The following chart shows the allowed
|
|
|
|
** transitions and the inserted intermediate states:
|
|
|
|
**
|
|
|
|
** UNLOCKED -> SHARED
|
|
|
|
** SHARED -> RESERVED
|
|
|
|
** SHARED -> (PENDING) -> EXCLUSIVE
|
|
|
|
** RESERVED -> (PENDING) -> EXCLUSIVE
|
|
|
|
** PENDING -> EXCLUSIVE
|
2004-06-07 20:27:46 +04:00
|
|
|
**
|
2004-06-09 21:37:22 +04:00
|
|
|
** This routine will only increase a lock. Use the sqlite3OsUnlock()
|
|
|
|
** routine to lower a locking level.
|
2004-06-01 18:09:28 +04:00
|
|
|
*/
|
|
|
|
int sqlite3OsLock(OsFile *id, int locktype){
|
2004-06-25 11:21:28 +04:00
|
|
|
/* The following describes the implementation of the various locks and
|
|
|
|
** lock transitions in terms of the POSIX advisory shared and exclusive
|
|
|
|
** lock primitives (called read-locks and write-locks below, to avoid
|
|
|
|
** confusion with SQLite lock names). The algorithms are complicated
|
|
|
|
** slightly in order to be compatible with windows systems simultaneously
|
|
|
|
** accessing the same database file, in case that is ever required.
|
|
|
|
**
|
|
|
|
** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
|
|
|
|
** byte', each single bytes at well known offsets, and the 'shared byte
|
|
|
|
** range', a range of 510 bytes at a well known offset.
|
|
|
|
**
|
|
|
|
** To obtain a SHARED lock, a read-lock is obtained on the 'pending
|
|
|
|
** byte'. If this is successful, a random byte from the 'shared byte
|
|
|
|
** range' is read-locked and the lock on the 'pending byte' released.
|
|
|
|
**
|
2004-06-25 12:32:25 +04:00
|
|
|
** A process may only obtain a RESERVED lock after it has a SHARED lock.
|
|
|
|
** A RESERVED lock is implemented by grabbing a write-lock on the
|
|
|
|
** 'reserved byte'.
|
2004-06-25 11:21:28 +04:00
|
|
|
**
|
|
|
|
** A process may only obtain a PENDING lock after it has obtained a
|
2004-06-25 12:32:25 +04:00
|
|
|
** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
|
|
|
|
** on the 'pending byte'. This ensures that no new SHARED locks can be
|
|
|
|
** obtained, but existing SHARED locks are allowed to persist. A process
|
|
|
|
** does not have to obtain a RESERVED lock on the way to a PENDING lock.
|
|
|
|
** This property is used by the algorithm for rolling back a journal file
|
|
|
|
** after a crash.
|
2004-06-25 11:21:28 +04:00
|
|
|
**
|
2004-06-25 12:32:25 +04:00
|
|
|
** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
|
|
|
|
** implemented by obtaining a write-lock on the entire 'shared byte
|
|
|
|
** range'. Since all other locks require a read-lock on one of the bytes
|
|
|
|
** within this range, this ensures that no other locks are held on the
|
|
|
|
** database.
|
2004-06-25 11:21:28 +04:00
|
|
|
**
|
|
|
|
** The reason a single byte cannot be used instead of the 'shared byte
|
|
|
|
** range' is that some versions of windows do not support read-locks. By
|
|
|
|
** locking a random byte from a range, concurrent SHARED locks may exist
|
|
|
|
** even if the locking primitive used is always a write-lock.
|
|
|
|
*/
|
2004-06-01 18:09:28 +04:00
|
|
|
int rc = SQLITE_OK;
|
|
|
|
struct lockInfo *pLock = id->pLock;
|
|
|
|
struct flock lock;
|
|
|
|
int s;
|
|
|
|
|
2004-06-21 22:14:45 +04:00
|
|
|
assert( id->isOpen );
|
2004-06-29 11:45:33 +04:00
|
|
|
TRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", id->h, locktypeName(locktype),
|
|
|
|
locktypeName(id->locktype), locktypeName(pLock->locktype), pLock->cnt
|
|
|
|
,getpid() );
|
2004-06-01 18:09:28 +04:00
|
|
|
|
|
|
|
/* If there is already a lock of this type or more restrictive on the
|
|
|
|
** OsFile, do nothing. Don't use the end_lock: exit path, as
|
|
|
|
** sqlite3OsEnterMutex() hasn't been called yet.
|
|
|
|
*/
|
2004-06-03 20:08:41 +04:00
|
|
|
if( id->locktype>=locktype ){
|
2004-06-29 11:45:33 +04:00
|
|
|
TRACE3("LOCK %d %s ok (already held)\n", id->h, locktypeName(locktype));
|
2004-06-01 18:09:28 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2004-06-08 04:47:47 +04:00
|
|
|
/* Make sure the locking sequence is correct
|
2004-06-07 20:27:46 +04:00
|
|
|
*/
|
2004-06-08 04:47:47 +04:00
|
|
|
assert( id->locktype!=NO_LOCK || locktype==SHARED_LOCK );
|
|
|
|
assert( locktype!=PENDING_LOCK );
|
|
|
|
assert( locktype!=RESERVED_LOCK || id->locktype==SHARED_LOCK );
|
2004-06-07 20:27:46 +04:00
|
|
|
|
2004-06-08 04:47:47 +04:00
|
|
|
/* This mutex is needed because id->pLock is shared across threads
|
|
|
|
*/
|
|
|
|
sqlite3OsEnterMutex();
|
2004-06-01 18:09:28 +04:00
|
|
|
|
|
|
|
/* If some thread using this PID has a lock via a different OsFile*
|
|
|
|
** handle that precludes the requested lock, return BUSY.
|
|
|
|
*/
|
2004-06-03 20:08:41 +04:00
|
|
|
if( (id->locktype!=pLock->locktype &&
|
2004-06-07 20:27:46 +04:00
|
|
|
(pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
|
2004-06-01 18:09:28 +04:00
|
|
|
){
|
|
|
|
rc = SQLITE_BUSY;
|
|
|
|
goto end_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If a SHARED lock is requested, and some thread using this PID already
|
|
|
|
** has a SHARED or RESERVED lock, then increment reference counts and
|
|
|
|
** return SQLITE_OK.
|
|
|
|
*/
|
|
|
|
if( locktype==SHARED_LOCK &&
|
|
|
|
(pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
|
|
|
|
assert( locktype==SHARED_LOCK );
|
2004-06-03 20:08:41 +04:00
|
|
|
assert( id->locktype==0 );
|
2004-06-02 10:30:16 +04:00
|
|
|
assert( pLock->cnt>0 );
|
2004-06-03 20:08:41 +04:00
|
|
|
id->locktype = SHARED_LOCK;
|
2004-06-01 18:09:28 +04:00
|
|
|
pLock->cnt++;
|
|
|
|
id->pOpen->nLock++;
|
|
|
|
goto end_lock;
|
|
|
|
}
|
|
|
|
|
2004-06-03 20:08:41 +04:00
|
|
|
lock.l_len = 1L;
|
2004-06-01 18:09:28 +04:00
|
|
|
lock.l_whence = SEEK_SET;
|
|
|
|
|
2004-06-12 06:17:14 +04:00
|
|
|
/* A PENDING lock is needed before acquiring a SHARED lock and before
|
|
|
|
** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
|
|
|
|
** be released.
|
2004-06-01 18:09:28 +04:00
|
|
|
*/
|
2004-06-12 06:17:14 +04:00
|
|
|
if( locktype==SHARED_LOCK
|
|
|
|
|| (locktype==EXCLUSIVE_LOCK && id->locktype<PENDING_LOCK)
|
|
|
|
){
|
2004-06-28 12:25:47 +04:00
|
|
|
lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
|
2004-06-07 20:27:46 +04:00
|
|
|
lock.l_start = PENDING_BYTE;
|
2004-06-09 21:37:22 +04:00
|
|
|
s = fcntl(id->h, F_SETLK, &lock);
|
2004-06-01 18:09:28 +04:00
|
|
|
if( s ){
|
|
|
|
rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
|
|
|
|
goto end_lock;
|
|
|
|
}
|
2004-06-12 06:17:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* If control gets to this point, then actually go ahead and make
|
|
|
|
** operating system calls for the specified lock.
|
|
|
|
*/
|
|
|
|
if( locktype==SHARED_LOCK ){
|
|
|
|
assert( pLock->cnt==0 );
|
|
|
|
assert( pLock->locktype==0 );
|
2004-06-01 18:09:28 +04:00
|
|
|
|
2004-06-07 20:27:46 +04:00
|
|
|
/* Now get the read-lock */
|
|
|
|
lock.l_start = SHARED_FIRST;
|
|
|
|
lock.l_len = SHARED_SIZE;
|
2004-06-09 21:37:22 +04:00
|
|
|
s = fcntl(id->h, F_SETLK, &lock);
|
2004-06-07 20:27:46 +04:00
|
|
|
|
|
|
|
/* Drop the temporary PENDING lock */
|
|
|
|
lock.l_start = PENDING_BYTE;
|
|
|
|
lock.l_len = 1L;
|
2004-06-01 18:09:28 +04:00
|
|
|
lock.l_type = F_UNLCK;
|
2004-06-09 21:37:22 +04:00
|
|
|
fcntl(id->h, F_SETLK, &lock);
|
2004-06-01 18:09:28 +04:00
|
|
|
if( s ){
|
2003-04-26 06:40:45 +04:00
|
|
|
rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
|
2001-12-14 18:09:55 +03:00
|
|
|
}else{
|
2004-06-03 20:08:41 +04:00
|
|
|
id->locktype = SHARED_LOCK;
|
2004-06-02 10:30:16 +04:00
|
|
|
id->pOpen->nLock++;
|
2004-06-01 18:09:28 +04:00
|
|
|
pLock->cnt = 1;
|
2001-12-06 16:24:14 +03:00
|
|
|
}
|
2004-06-12 06:17:14 +04:00
|
|
|
}else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
|
|
|
|
/* We are trying for an exclusive lock but another thread in this
|
|
|
|
** same process is still holding a shared lock. */
|
|
|
|
rc = SQLITE_BUSY;
|
2001-12-14 18:09:55 +03:00
|
|
|
}else{
|
2004-06-12 06:17:14 +04:00
|
|
|
/* The request was for a RESERVED or EXCLUSIVE lock. It is
|
2004-06-01 18:09:28 +04:00
|
|
|
** assumed that there is a SHARED or greater lock on the file
|
|
|
|
** already.
|
|
|
|
*/
|
2004-06-03 20:08:41 +04:00
|
|
|
assert( 0!=id->locktype );
|
2004-06-01 18:09:28 +04:00
|
|
|
lock.l_type = F_WRLCK;
|
|
|
|
switch( locktype ){
|
|
|
|
case RESERVED_LOCK:
|
2004-06-07 20:27:46 +04:00
|
|
|
lock.l_start = RESERVED_BYTE;
|
2004-06-01 18:09:28 +04:00
|
|
|
break;
|
|
|
|
case EXCLUSIVE_LOCK:
|
2004-06-07 20:27:46 +04:00
|
|
|
lock.l_start = SHARED_FIRST;
|
|
|
|
lock.l_len = SHARED_SIZE;
|
2004-06-01 18:09:28 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2004-06-09 21:37:22 +04:00
|
|
|
s = fcntl(id->h, F_SETLK, &lock);
|
2004-06-01 18:09:28 +04:00
|
|
|
if( s ){
|
|
|
|
rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
|
|
|
|
}
|
2001-10-09 08:19:46 +04:00
|
|
|
}
|
2004-06-01 18:09:28 +04:00
|
|
|
|
2004-06-02 10:30:16 +04:00
|
|
|
if( rc==SQLITE_OK ){
|
2004-06-03 20:08:41 +04:00
|
|
|
id->locktype = locktype;
|
2004-06-02 10:30:16 +04:00
|
|
|
pLock->locktype = locktype;
|
2004-06-12 06:17:14 +04:00
|
|
|
}else if( locktype==EXCLUSIVE_LOCK ){
|
|
|
|
id->locktype = PENDING_LOCK;
|
|
|
|
pLock->locktype = PENDING_LOCK;
|
2004-06-02 10:30:16 +04:00
|
|
|
}
|
2004-06-01 18:09:28 +04:00
|
|
|
|
|
|
|
end_lock:
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3OsLeaveMutex();
|
2004-06-29 11:45:33 +04:00
|
|
|
TRACE4("LOCK %d %s %s\n", id->h, locktypeName(locktype),
|
|
|
|
rc==SQLITE_OK ? "ok" : "failed");
|
2001-10-09 08:19:46 +04:00
|
|
|
return rc;
|
2001-09-19 17:22:39 +04:00
|
|
|
}
|
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
2004-06-09 21:37:22 +04:00
|
|
|
** Lower the locking level on file descriptor id to locktype. locktype
|
|
|
|
** must be either NO_LOCK or SHARED_LOCK.
|
|
|
|
**
|
|
|
|
** If the locking level of the file descriptor is already at or below
|
|
|
|
** the requested locking level, this routine is a no-op.
|
|
|
|
**
|
2004-10-03 00:38:28 +04:00
|
|
|
** It is not possible for this routine to fail if the second argument
|
|
|
|
** is NO_LOCK. If the second argument is SHARED_LOCK, this routine
|
|
|
|
** might return SQLITE_IOERR instead of SQLITE_OK.
|
2001-09-18 00:25:58 +04:00
|
|
|
*/
|
2004-06-09 21:37:22 +04:00
|
|
|
int sqlite3OsUnlock(OsFile *id, int locktype){
|
|
|
|
struct lockInfo *pLock;
|
|
|
|
struct flock lock;
|
2004-10-03 00:38:28 +04:00
|
|
|
int rc = SQLITE_OK;
|
2004-06-09 21:37:22 +04:00
|
|
|
|
2004-06-21 22:14:45 +04:00
|
|
|
assert( id->isOpen );
|
2004-06-29 11:45:33 +04:00
|
|
|
TRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", id->h, locktype, id->locktype,
|
|
|
|
id->pLock->locktype, id->pLock->cnt, getpid());
|
2004-06-09 21:37:22 +04:00
|
|
|
|
|
|
|
assert( locktype<=SHARED_LOCK );
|
|
|
|
if( id->locktype<=locktype ){
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3OsEnterMutex();
|
2004-06-09 21:37:22 +04:00
|
|
|
pLock = id->pLock;
|
|
|
|
assert( pLock->cnt!=0 );
|
|
|
|
if( id->locktype>SHARED_LOCK ){
|
|
|
|
assert( pLock->locktype==id->locktype );
|
2004-10-03 00:38:28 +04:00
|
|
|
if( locktype==SHARED_LOCK ){
|
|
|
|
lock.l_type = F_RDLCK;
|
|
|
|
lock.l_whence = SEEK_SET;
|
|
|
|
lock.l_start = SHARED_FIRST;
|
|
|
|
lock.l_len = SHARED_SIZE;
|
|
|
|
if( fcntl(id->h, F_SETLK, &lock)!=0 ){
|
|
|
|
/* This should never happen */
|
|
|
|
rc = SQLITE_IOERR;
|
|
|
|
}
|
|
|
|
}
|
2001-10-09 08:19:46 +04:00
|
|
|
lock.l_type = F_UNLCK;
|
|
|
|
lock.l_whence = SEEK_SET;
|
2004-06-09 21:37:22 +04:00
|
|
|
lock.l_start = PENDING_BYTE;
|
|
|
|
lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
|
|
|
|
fcntl(id->h, F_SETLK, &lock);
|
|
|
|
pLock->locktype = SHARED_LOCK;
|
2001-10-09 08:19:46 +04:00
|
|
|
}
|
2004-06-09 21:37:22 +04:00
|
|
|
if( locktype==NO_LOCK ){
|
|
|
|
struct openCnt *pOpen;
|
|
|
|
|
|
|
|
/* Decrement the shared lock counter. Release the lock using an
|
|
|
|
** OS call only when all threads in this same process have released
|
|
|
|
** the lock.
|
|
|
|
*/
|
|
|
|
pLock->cnt--;
|
|
|
|
if( pLock->cnt==0 ){
|
|
|
|
lock.l_type = F_UNLCK;
|
|
|
|
lock.l_whence = SEEK_SET;
|
|
|
|
lock.l_start = lock.l_len = 0L;
|
|
|
|
fcntl(id->h, F_SETLK, &lock);
|
|
|
|
pLock->locktype = NO_LOCK;
|
|
|
|
}
|
2004-06-02 10:30:16 +04:00
|
|
|
|
2004-01-12 03:39:05 +03:00
|
|
|
/* Decrement the count of locks against this same file. When the
|
|
|
|
** count reaches zero, close any other file descriptors whose close
|
|
|
|
** was deferred because of outstanding locks.
|
|
|
|
*/
|
2004-06-09 21:37:22 +04:00
|
|
|
pOpen = id->pOpen;
|
2004-01-12 03:39:05 +03:00
|
|
|
pOpen->nLock--;
|
|
|
|
assert( pOpen->nLock>=0 );
|
|
|
|
if( pOpen->nLock==0 && pOpen->nPending>0 ){
|
|
|
|
int i;
|
|
|
|
for(i=0; i<pOpen->nPending; i++){
|
|
|
|
close(pOpen->aPending[i]);
|
|
|
|
}
|
|
|
|
sqliteFree(pOpen->aPending);
|
|
|
|
pOpen->nPending = 0;
|
|
|
|
pOpen->aPending = 0;
|
|
|
|
}
|
|
|
|
}
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3OsLeaveMutex();
|
2004-06-09 21:37:22 +04:00
|
|
|
id->locktype = locktype;
|
2004-10-03 00:38:28 +04:00
|
|
|
return rc;
|
2001-09-19 17:22:39 +04:00
|
|
|
}
|
|
|
|
|
2004-06-22 15:29:02 +04:00
|
|
|
/*
|
|
|
|
** Close a file.
|
|
|
|
*/
|
|
|
|
int sqlite3OsClose(OsFile *id){
|
|
|
|
if( !id->isOpen ) return SQLITE_OK;
|
|
|
|
sqlite3OsUnlock(id, NO_LOCK);
|
|
|
|
if( id->dirfd>=0 ) close(id->dirfd);
|
|
|
|
id->dirfd = -1;
|
|
|
|
sqlite3OsEnterMutex();
|
|
|
|
if( id->pOpen->nLock ){
|
|
|
|
/* If there are outstanding locks, do not actually close the file just
|
|
|
|
** yet because that would clear those locks. Instead, add the file
|
|
|
|
** descriptor to pOpen->aPending. It will be automatically closed when
|
|
|
|
** the last lock is cleared.
|
|
|
|
*/
|
|
|
|
int *aNew;
|
|
|
|
struct openCnt *pOpen = id->pOpen;
|
|
|
|
pOpen->nPending++;
|
|
|
|
aNew = sqliteRealloc( pOpen->aPending, pOpen->nPending*sizeof(int) );
|
|
|
|
if( aNew==0 ){
|
|
|
|
/* If a malloc fails, just leak the file descriptor */
|
|
|
|
}else{
|
|
|
|
pOpen->aPending = aNew;
|
|
|
|
pOpen->aPending[pOpen->nPending-1] = id->h;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
/* There are no outstanding locks so we can close the file immediately */
|
|
|
|
close(id->h);
|
|
|
|
}
|
|
|
|
releaseLockInfo(id->pLock);
|
|
|
|
releaseOpenCnt(id->pOpen);
|
|
|
|
sqlite3OsLeaveMutex();
|
|
|
|
id->isOpen = 0;
|
|
|
|
TRACE2("CLOSE %-3d\n", id->h);
|
|
|
|
OpenCounter(-1);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
2002-10-12 17:43:59 +04:00
|
|
|
** Get information to seed the random number generator. The seed
|
|
|
|
** is written into the buffer zBuf[256]. The calling function must
|
|
|
|
** supply a sufficiently large buffer.
|
2001-09-18 00:25:58 +04:00
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
int sqlite3OsRandomSeed(char *zBuf){
|
2003-12-31 16:21:18 +03:00
|
|
|
/* We have to initialize zBuf to prevent valgrind from reporting
|
|
|
|
** errors. The reports issued by valgrind are incorrect - we would
|
|
|
|
** prefer that the randomness be increased by making use of the
|
|
|
|
** uninitialized space in zBuf - but valgrind errors tend to worry
|
|
|
|
** some users. Rather than argue, it seems easier just to initialize
|
|
|
|
** the whole array and silence valgrind, even if that means less randomness
|
|
|
|
** in the random seed.
|
|
|
|
**
|
|
|
|
** When testing, initializing zBuf[] to zero is all we do. That means
|
|
|
|
** that we always use the same random number sequence.* This makes the
|
|
|
|
** tests repeatable.
|
2002-07-07 20:52:46 +04:00
|
|
|
*/
|
|
|
|
memset(zBuf, 0, 256);
|
2004-05-22 21:41:58 +04:00
|
|
|
#if !defined(SQLITE_TEST)
|
2003-12-31 19:00:19 +03:00
|
|
|
{
|
2005-01-21 20:53:17 +03:00
|
|
|
int pid, fd;
|
|
|
|
fd = open("/dev/urandom", O_RDONLY);
|
|
|
|
if( fd<0 ){
|
|
|
|
time((time_t*)zBuf);
|
|
|
|
pid = getpid();
|
|
|
|
memcpy(&zBuf[sizeof(time_t)], &pid, sizeof(pid));
|
|
|
|
}else{
|
|
|
|
read(fd, zBuf, 256);
|
|
|
|
close(fd);
|
|
|
|
}
|
2003-12-31 19:00:19 +03:00
|
|
|
}
|
2001-09-23 23:46:51 +04:00
|
|
|
#endif
|
2001-09-19 17:22:39 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
2001-09-19 17:22:39 +04:00
|
|
|
** Sleep for a little while. Return the amount of time slept.
|
2001-09-18 00:25:58 +04:00
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
int sqlite3OsSleep(int ms){
|
2001-09-19 17:22:39 +04:00
|
|
|
#if defined(HAVE_USLEEP) && HAVE_USLEEP
|
|
|
|
usleep(ms*1000);
|
|
|
|
return ms;
|
|
|
|
#else
|
|
|
|
sleep((ms+999)/1000);
|
|
|
|
return 1000*((ms+999)/1000);
|
|
|
|
#endif
|
|
|
|
}
|
2001-10-09 08:19:46 +04:00
|
|
|
|
2002-01-15 21:39:43 +03:00
|
|
|
/*
|
|
|
|
** Static variables used for thread synchronization
|
|
|
|
*/
|
|
|
|
static int inMutex = 0;
|
2004-05-23 01:30:40 +04:00
|
|
|
#ifdef SQLITE_UNIX_THREADS
|
2004-05-22 21:41:58 +04:00
|
|
|
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
2004-05-23 01:30:40 +04:00
|
|
|
#endif
|
2002-01-15 21:39:43 +03:00
|
|
|
|
2001-10-09 08:19:46 +04:00
|
|
|
/*
|
|
|
|
** The following pair of routine implement mutual exclusion for
|
|
|
|
** multi-threaded processes. Only a single thread is allowed to
|
|
|
|
** executed code that is surrounded by EnterMutex() and LeaveMutex().
|
|
|
|
**
|
|
|
|
** SQLite uses only a single Mutex. There is not much critical
|
|
|
|
** code and what little there is executes quickly and without blocking.
|
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
void sqlite3OsEnterMutex(){
|
2002-01-15 21:39:43 +03:00
|
|
|
#ifdef SQLITE_UNIX_THREADS
|
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
#endif
|
2001-10-09 08:19:46 +04:00
|
|
|
assert( !inMutex );
|
|
|
|
inMutex = 1;
|
|
|
|
}
|
2004-05-08 12:23:19 +04:00
|
|
|
void sqlite3OsLeaveMutex(){
|
2001-10-09 08:19:46 +04:00
|
|
|
assert( inMutex );
|
|
|
|
inMutex = 0;
|
2002-01-15 21:39:43 +03:00
|
|
|
#ifdef SQLITE_UNIX_THREADS
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
#endif
|
2001-10-09 08:19:46 +04:00
|
|
|
}
|
2002-12-08 00:45:14 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Turn a relative pathname into a full pathname. Return a pointer
|
|
|
|
** to the full pathname stored in space obtained from sqliteMalloc().
|
|
|
|
** The calling function is responsible for freeing this space once it
|
|
|
|
** is no longer needed.
|
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
char *sqlite3OsFullPathname(const char *zRelative){
|
2002-12-08 00:45:14 +03:00
|
|
|
char *zFull = 0;
|
|
|
|
if( zRelative[0]=='/' ){
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3SetString(&zFull, zRelative, (char*)0);
|
2002-12-08 00:45:14 +03:00
|
|
|
}else{
|
|
|
|
char zBuf[5000];
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3SetString(&zFull, getcwd(zBuf, sizeof(zBuf)), "/", zRelative,
|
2003-12-07 00:43:55 +03:00
|
|
|
(char*)0);
|
2002-12-08 00:45:14 +03:00
|
|
|
}
|
|
|
|
return zFull;
|
|
|
|
}
|
2003-08-10 01:32:28 +04:00
|
|
|
|
2004-01-06 03:44:24 +03:00
|
|
|
/*
|
2004-03-13 17:00:36 +03:00
|
|
|
** The following variable, if set to a non-zero value, becomes the result
|
2004-05-08 12:23:19 +04:00
|
|
|
** returned from sqlite3OsCurrentTime(). This is used for testing.
|
2004-01-06 03:44:24 +03:00
|
|
|
*/
|
|
|
|
#ifdef SQLITE_TEST
|
2004-05-10 14:34:51 +04:00
|
|
|
int sqlite3_current_time = 0;
|
2004-01-06 03:44:24 +03:00
|
|
|
#endif
|
|
|
|
|
2003-08-10 01:32:28 +04:00
|
|
|
/*
|
|
|
|
** Find the current time (in Universal Coordinated Time). Write the
|
|
|
|
** current time and date as a Julian Day number into *prNow and
|
|
|
|
** return 0. Return 1 if the time and date cannot be found.
|
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
int sqlite3OsCurrentTime(double *prNow){
|
2003-08-10 01:32:28 +04:00
|
|
|
time_t t;
|
|
|
|
time(&t);
|
|
|
|
*prNow = t/86400.0 + 2440587.5;
|
2004-01-06 03:44:24 +03:00
|
|
|
#ifdef SQLITE_TEST
|
2004-05-10 14:34:51 +04:00
|
|
|
if( sqlite3_current_time ){
|
|
|
|
*prNow = sqlite3_current_time/86400.0 + 2440587.5;
|
2004-01-06 03:44:24 +03:00
|
|
|
}
|
2003-08-10 20:16:22 +04:00
|
|
|
#endif
|
|
|
|
return 0;
|
2003-08-10 01:32:28 +04:00
|
|
|
}
|
2004-05-08 12:23:19 +04:00
|
|
|
|
2004-06-29 15:08:19 +04:00
|
|
|
#if 0 /* NOT USED */
|
2004-06-15 04:29:03 +04:00
|
|
|
/*
|
|
|
|
** Find the time that the file was last modified. Write the
|
|
|
|
** modification time and date as a Julian Day number into *prNow and
|
|
|
|
** return SQLITE_OK. Return SQLITE_ERROR if the modification
|
|
|
|
** time cannot be found.
|
|
|
|
*/
|
|
|
|
int sqlite3OsFileModTime(OsFile *id, double *prNow){
|
|
|
|
int rc;
|
|
|
|
struct stat statbuf;
|
|
|
|
if( fstat(id->h, &statbuf)==0 ){
|
|
|
|
*prNow = statbuf.st_mtime/86400.0 + 2440587.5;
|
|
|
|
rc = SQLITE_OK;
|
|
|
|
}else{
|
|
|
|
rc = SQLITE_ERROR;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
2004-06-29 15:08:19 +04:00
|
|
|
#endif /* NOT USED */
|
2004-06-15 04:29:03 +04:00
|
|
|
|
2004-05-22 21:41:58 +04:00
|
|
|
#endif /* OS_UNIX */
|