2001-09-18 00:25:58 +04:00
|
|
|
/*
|
|
|
|
** 2001 September 16
|
|
|
|
**
|
|
|
|
** 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.
|
|
|
|
**
|
|
|
|
******************************************************************************
|
|
|
|
**
|
|
|
|
** This file contains code that is specific to particular operating
|
2001-10-12 21:30:04 +04:00
|
|
|
** systems. The purpose of this file is to provide a uniform abstraction
|
2001-09-18 00:25:58 +04:00
|
|
|
** on which the rest of SQLite can operate.
|
|
|
|
*/
|
|
|
|
#include "sqliteInt.h"
|
|
|
|
#include "os.h"
|
|
|
|
|
|
|
|
#if OS_UNIX
|
2001-09-19 17:58:43 +04:00
|
|
|
# include <unistd.h>
|
2001-09-18 00:25:58 +04:00
|
|
|
# include <fcntl.h>
|
|
|
|
# include <sys/stat.h>
|
|
|
|
# include <time.h>
|
|
|
|
#endif
|
|
|
|
#if OS_WIN
|
|
|
|
# include <winbase.h>
|
|
|
|
#endif
|
|
|
|
|
2002-03-05 04:11:12 +03:00
|
|
|
/*
|
|
|
|
** Macros for performance tracing. Normally turned off
|
|
|
|
*/
|
|
|
|
#if 0
|
|
|
|
static int last_page = 0;
|
|
|
|
#define SEEK(X) last_page=(X)
|
|
|
|
#define TRACE1(X) fprintf(stderr,X)
|
|
|
|
#define TRACE2(X,Y) fprintf(stderr,X,Y)
|
|
|
|
#else
|
|
|
|
#define SEEK(X)
|
|
|
|
#define TRACE1(X)
|
|
|
|
#define TRACE2(X,Y)
|
|
|
|
#endif
|
|
|
|
|
2001-10-09 08:19:46 +04:00
|
|
|
|
|
|
|
#if OS_UNIX
|
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);
|
|
|
|
**
|
|
|
|
** Suppose ./file1 and ./file2 are really be the same file (because
|
|
|
|
** 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
|
|
|
|
** structure. The fcntl() system call is only invoked to set a
|
|
|
|
** POSIX lock if the internal lock structure transitions between
|
|
|
|
** a locked and an unlocked state.
|
|
|
|
*/
|
|
|
|
|
2001-10-09 08:19:46 +04:00
|
|
|
/*
|
|
|
|
** An instance of the following structure serves as the key used
|
|
|
|
** to locate a particular lockInfo structure given its inode.
|
|
|
|
*/
|
|
|
|
struct inodeKey {
|
|
|
|
dev_t dev; /* Device number */
|
|
|
|
ino_t ino; /* Inode number */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** An instance of the following structure is allocated for each inode.
|
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 {
|
|
|
|
struct inodeKey key; /* The lookup key */
|
2001-12-14 18:09:55 +03:00
|
|
|
int cnt; /* 0: unlocked. -1: write lock. 1...: read lock. */
|
2001-10-09 08:19:46 +04:00
|
|
|
int nRef; /* Number of pointers to this structure */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This hash table maps inodes (in the form of inodeKey structures) into
|
|
|
|
** pointers to lockInfo structures.
|
|
|
|
*/
|
|
|
|
static Hash lockHash = { SQLITE_HASH_BINARY, 0, 0, 0, 0, 0 };
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Given a file descriptor, locate a lockInfo structure that describes
|
2001-10-09 16:39:24 +04:00
|
|
|
** that file descriptor. Create a new one if necessary. NULL might
|
|
|
|
** be returned if malloc() fails.
|
2001-10-09 08:19:46 +04:00
|
|
|
*/
|
|
|
|
static struct lockInfo *findLockInfo(int fd){
|
|
|
|
int rc;
|
|
|
|
struct inodeKey key;
|
|
|
|
struct stat statbuf;
|
|
|
|
struct lockInfo *pInfo;
|
|
|
|
rc = fstat(fd, &statbuf);
|
|
|
|
if( rc!=0 ) return 0;
|
2001-11-10 01:41:44 +03:00
|
|
|
memset(&key, 0, sizeof(key));
|
2001-10-09 08:19:46 +04:00
|
|
|
key.dev = statbuf.st_dev;
|
|
|
|
key.ino = statbuf.st_ino;
|
|
|
|
pInfo = (struct lockInfo*)sqliteHashFind(&lockHash, &key, sizeof(key));
|
|
|
|
if( pInfo==0 ){
|
2001-10-22 06:58:08 +04:00
|
|
|
struct lockInfo *pOld;
|
2001-10-09 08:19:46 +04:00
|
|
|
pInfo = sqliteMalloc( sizeof(*pInfo) );
|
2001-10-09 16:39:24 +04:00
|
|
|
if( pInfo==0 ) return 0;
|
2001-10-09 08:19:46 +04:00
|
|
|
pInfo->key = key;
|
|
|
|
pInfo->nRef = 1;
|
|
|
|
pInfo->cnt = 0;
|
2001-10-22 06:58:08 +04:00
|
|
|
pOld = sqliteHashInsert(&lockHash, &pInfo->key, sizeof(key), pInfo);
|
|
|
|
if( pOld!=0 ){
|
|
|
|
assert( pOld==pInfo );
|
|
|
|
sqliteFree(pInfo);
|
|
|
|
pInfo = 0;
|
|
|
|
}
|
2001-10-09 08:19:46 +04:00
|
|
|
}else{
|
|
|
|
pInfo->nRef++;
|
|
|
|
}
|
|
|
|
return pInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Release a lockInfo structure previously allocated by findLockInfo().
|
|
|
|
*/
|
|
|
|
static void releaseLockInfo(struct lockInfo *pInfo){
|
|
|
|
pInfo->nRef--;
|
|
|
|
if( pInfo->nRef==0 ){
|
|
|
|
sqliteHashInsert(&lockHash, &pInfo->key, sizeof(pInfo->key), 0);
|
|
|
|
sqliteFree(pInfo);
|
|
|
|
}
|
|
|
|
}
|
2001-10-09 16:39:24 +04:00
|
|
|
#endif /** POSIX advisory lock work-around **/
|
2001-10-09 08:19:46 +04:00
|
|
|
|
2001-10-12 21:30:04 +04:00
|
|
|
/*
|
|
|
|
** If we compile with the SQLITE_TEST macro set, then the following block
|
|
|
|
** of code will give us the ability to simulate a disk I/O error. This
|
|
|
|
** is used for testing the I/O recovery logic.
|
|
|
|
*/
|
|
|
|
#ifdef SQLITE_TEST
|
|
|
|
int sqlite_io_error_pending = 0;
|
|
|
|
#define SimulateIOError(A) \
|
|
|
|
if( sqlite_io_error_pending ) \
|
|
|
|
if( sqlite_io_error_pending-- == 1 ){ local_ioerr(); return A; }
|
|
|
|
static void local_ioerr(){
|
|
|
|
sqlite_io_error_pending = 0; /* Really just a place to set a breakpoint */
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define SimulateIOError(A)
|
|
|
|
#endif
|
|
|
|
|
2001-10-09 08:19:46 +04:00
|
|
|
|
2001-09-19 17:22:39 +04:00
|
|
|
/*
|
|
|
|
** Delete the named file
|
|
|
|
*/
|
|
|
|
int sqliteOsDelete(const char *zFilename){
|
|
|
|
#if OS_UNIX
|
|
|
|
unlink(zFilename);
|
|
|
|
#endif
|
|
|
|
#if OS_WIN
|
|
|
|
DeleteFile(zFilename);
|
|
|
|
#endif
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return TRUE if the named file exists.
|
|
|
|
*/
|
|
|
|
int sqliteOsFileExists(const char *zFilename){
|
|
|
|
#if OS_UNIX
|
|
|
|
return access(zFilename, 0)==0;
|
|
|
|
#endif
|
|
|
|
#if OS_WIN
|
2002-02-18 15:48:45 +03:00
|
|
|
return GetFileAttributes(zFilename) != 0xffffffff;
|
2001-09-19 17:22:39 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
** *pResulst and *pReadonly unchanged.
|
|
|
|
*/
|
2001-09-19 17:22:39 +04:00
|
|
|
int sqliteOsOpenReadWrite(
|
|
|
|
const char *zFilename,
|
2001-12-14 18:09:55 +03:00
|
|
|
OsFile *id,
|
2001-09-19 17:22:39 +04:00
|
|
|
int *pReadonly
|
|
|
|
){
|
2001-09-18 00:25:58 +04:00
|
|
|
#if OS_UNIX
|
2001-12-14 18:09:55 +03:00
|
|
|
id->fd = open(zFilename, O_RDWR|O_CREAT, 0644);
|
|
|
|
if( id->fd<0 ){
|
|
|
|
id->fd = open(zFilename, O_RDONLY);
|
|
|
|
if( id->fd<0 ){
|
2001-09-18 00:25:58 +04:00
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
}
|
|
|
|
*pReadonly = 1;
|
|
|
|
}else{
|
|
|
|
*pReadonly = 0;
|
|
|
|
}
|
2001-10-09 08:19:46 +04:00
|
|
|
sqliteOsEnterMutex();
|
2001-12-14 18:09:55 +03:00
|
|
|
id->pLock = findLockInfo(id->fd);
|
2001-10-09 08:19:46 +04:00
|
|
|
sqliteOsLeaveMutex();
|
2001-12-14 18:09:55 +03:00
|
|
|
if( id->pLock==0 ){
|
|
|
|
close(id->fd);
|
2001-10-09 08:19:46 +04:00
|
|
|
return SQLITE_NOMEM;
|
|
|
|
}
|
2001-12-14 18:09:55 +03:00
|
|
|
id->locked = 0;
|
2001-09-18 00:25:58 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
|
|
|
#if OS_WIN
|
|
|
|
HANDLE h = CreateFile(zFilename,
|
|
|
|
GENERIC_READ | GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
NULL,
|
|
|
|
OPEN_ALWAYS,
|
|
|
|
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
if( h==INVALID_HANDLE_VALUE ){
|
2001-10-12 21:30:04 +04:00
|
|
|
h = CreateFile(zFilename,
|
2001-09-18 00:25:58 +04:00
|
|
|
GENERIC_READ,
|
|
|
|
FILE_SHARE_READ,
|
|
|
|
NULL,
|
|
|
|
OPEN_ALWAYS,
|
|
|
|
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
if( h==INVALID_HANDLE_VALUE ){
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
}
|
|
|
|
*pReadonly = 1;
|
|
|
|
}else{
|
|
|
|
*pReadonly = 0;
|
|
|
|
}
|
2001-12-14 18:09:55 +03:00
|
|
|
id->h = h;
|
|
|
|
id->locked = 0;
|
2001-09-18 00:25:58 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
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.
|
|
|
|
*/
|
2002-02-02 18:01:15 +03:00
|
|
|
int sqliteOsOpenExclusive(const char *zFilename, OsFile *id, int delFlag){
|
2001-09-18 00:25:58 +04:00
|
|
|
#if OS_UNIX
|
|
|
|
if( access(zFilename, 0)==0 ){
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
}
|
|
|
|
#ifndef O_NOFOLLOW
|
|
|
|
# define O_NOFOLLOW 0
|
|
|
|
#endif
|
2001-12-14 18:09:55 +03:00
|
|
|
id->fd = open(zFilename, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0600);
|
|
|
|
if( id->fd<0 ){
|
2001-09-18 00:25:58 +04:00
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
}
|
2001-10-09 08:19:46 +04:00
|
|
|
sqliteOsEnterMutex();
|
2001-12-14 18:09:55 +03:00
|
|
|
id->pLock = findLockInfo(id->fd);
|
2001-10-09 08:19:46 +04:00
|
|
|
sqliteOsLeaveMutex();
|
2001-12-14 18:09:55 +03:00
|
|
|
if( id->pLock==0 ){
|
|
|
|
close(id->fd);
|
2001-10-22 06:58:08 +04:00
|
|
|
unlink(zFilename);
|
2001-10-09 08:19:46 +04:00
|
|
|
return SQLITE_NOMEM;
|
|
|
|
}
|
2001-12-14 18:09:55 +03:00
|
|
|
id->locked = 0;
|
2002-02-02 18:01:15 +03:00
|
|
|
if( delFlag ){
|
|
|
|
unlink(zFilename);
|
|
|
|
}
|
2001-09-18 00:25:58 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
|
|
|
#if OS_WIN
|
2002-02-02 18:01:15 +03:00
|
|
|
HANDLE h;
|
|
|
|
int fileflags;
|
|
|
|
if( delFlag ){
|
|
|
|
fileflags = FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_RANDOM_ACCESS
|
|
|
|
| FILE_FLAG_DELETE_ON_CLOSE;
|
|
|
|
}else{
|
|
|
|
fileflags = FILE_FLAG_RANDOM_ACCESS;
|
|
|
|
}
|
|
|
|
h = CreateFile(zFilename,
|
2001-09-18 00:25:58 +04:00
|
|
|
GENERIC_READ | GENERIC_WRITE,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
CREATE_ALWAYS,
|
2002-02-02 18:01:15 +03:00
|
|
|
fileflags,
|
2001-09-18 00:25:58 +04:00
|
|
|
NULL
|
|
|
|
);
|
|
|
|
if( h==INVALID_HANDLE_VALUE ){
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
}
|
2001-12-14 18:09:55 +03:00
|
|
|
id->h = h;
|
|
|
|
id->locked = 0;
|
2001-09-18 00:25:58 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2001-12-14 18:09:55 +03:00
|
|
|
int sqliteOsOpenReadOnly(const char *zFilename, OsFile *id){
|
2001-09-19 17:58:43 +04:00
|
|
|
#if OS_UNIX
|
2001-12-14 18:09:55 +03:00
|
|
|
id->fd = open(zFilename, O_RDONLY);
|
|
|
|
if( id->fd<0 ){
|
2001-09-19 17:58:43 +04:00
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
}
|
2001-10-09 08:19:46 +04:00
|
|
|
sqliteOsEnterMutex();
|
2001-12-14 18:09:55 +03:00
|
|
|
id->pLock = findLockInfo(id->fd);
|
2001-10-09 08:19:46 +04:00
|
|
|
sqliteOsLeaveMutex();
|
2001-12-14 18:09:55 +03:00
|
|
|
if( id->pLock==0 ){
|
|
|
|
close(id->fd);
|
2001-10-09 08:19:46 +04:00
|
|
|
return SQLITE_NOMEM;
|
|
|
|
}
|
2001-12-14 18:09:55 +03:00
|
|
|
id->locked = 0;
|
2001-09-19 17:58:43 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
|
|
|
#if OS_WIN
|
|
|
|
HANDLE h = CreateFile(zFilename,
|
|
|
|
GENERIC_READ,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
if( h==INVALID_HANDLE_VALUE ){
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
}
|
2001-12-14 18:09:55 +03:00
|
|
|
id->h = h;
|
|
|
|
id->locked = 0;
|
2001-09-19 17:58:43 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
int sqliteOsTempFileName(char *zBuf){
|
|
|
|
#if OS_UNIX
|
|
|
|
static const char *azDirs[] = {
|
|
|
|
"/var/tmp",
|
|
|
|
"/usr/tmp",
|
|
|
|
"/tmp",
|
2002-08-14 04:10:44 +04:00
|
|
|
".",
|
2001-09-18 00:25:58 +04:00
|
|
|
};
|
|
|
|
static char zChars[] =
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"0123456789";
|
|
|
|
int i, j;
|
|
|
|
struct stat buf;
|
2001-09-19 17:22:39 +04:00
|
|
|
const char *zDir = ".";
|
2001-09-18 00:25:58 +04:00
|
|
|
for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
|
|
|
|
if( stat(azDirs[i], &buf) ) continue;
|
|
|
|
if( !S_ISDIR(buf.st_mode) ) continue;
|
|
|
|
if( access(azDirs[i], 07) ) continue;
|
|
|
|
zDir = azDirs[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
do{
|
|
|
|
sprintf(zBuf, "%s/sqlite_", zDir);
|
|
|
|
j = strlen(zBuf);
|
|
|
|
for(i=0; i<15; i++){
|
2001-10-09 08:19:46 +04:00
|
|
|
int n = sqliteRandomByte() % (sizeof(zChars)-1);
|
2001-09-18 00:25:58 +04:00
|
|
|
zBuf[j++] = zChars[n];
|
|
|
|
}
|
|
|
|
zBuf[j] = 0;
|
|
|
|
}while( access(zBuf,0)==0 );
|
|
|
|
#endif
|
|
|
|
#if OS_WIN
|
2001-09-20 05:44:42 +04:00
|
|
|
static char zChars[] =
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"0123456789";
|
2001-09-18 00:25:58 +04:00
|
|
|
int i, j;
|
|
|
|
char zTempPath[SQLITE_TEMPNAME_SIZE];
|
|
|
|
GetTempPath(SQLITE_TEMPNAME_SIZE-30, zTempPath);
|
2002-05-29 16:44:52 +04:00
|
|
|
for(i=strlen(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){}
|
|
|
|
zTempPath[i] = 0;
|
2001-09-18 00:25:58 +04:00
|
|
|
for(;;){
|
2002-05-29 16:44:52 +04:00
|
|
|
sprintf(zBuf, "%s\\sqlite_", zTempPath);
|
2001-09-18 00:25:58 +04:00
|
|
|
j = strlen(zBuf);
|
|
|
|
for(i=0; i<15; i++){
|
2001-10-09 08:19:46 +04:00
|
|
|
int n = sqliteRandomByte() % sizeof(zChars);
|
2001-09-18 00:25:58 +04:00
|
|
|
zBuf[j++] = zChars[n];
|
|
|
|
}
|
|
|
|
zBuf[j] = 0;
|
2001-09-19 17:22:39 +04:00
|
|
|
if( !sqliteOsFileExists(zBuf) ) break;
|
2001-09-18 00:25:58 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Close a file
|
|
|
|
*/
|
2001-12-14 18:09:55 +03:00
|
|
|
int sqliteOsClose(OsFile *id){
|
2001-09-18 00:25:58 +04:00
|
|
|
#if OS_UNIX
|
2001-12-14 18:09:55 +03:00
|
|
|
close(id->fd);
|
2001-10-09 08:19:46 +04:00
|
|
|
sqliteOsEnterMutex();
|
2001-12-14 18:09:55 +03:00
|
|
|
releaseLockInfo(id->pLock);
|
2001-10-09 08:19:46 +04:00
|
|
|
sqliteOsLeaveMutex();
|
2001-09-18 00:25:58 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
|
|
|
#if OS_WIN
|
2001-12-14 18:09:55 +03:00
|
|
|
CloseHandle(id->h);
|
2001-09-18 00:25:58 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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
|
|
|
*/
|
2001-12-14 18:09:55 +03:00
|
|
|
int sqliteOsRead(OsFile *id, void *pBuf, int amt){
|
2001-09-19 17:22:39 +04:00
|
|
|
#if OS_UNIX
|
|
|
|
int got;
|
2001-10-12 21:30:04 +04:00
|
|
|
SimulateIOError(SQLITE_IOERR);
|
2002-03-05 04:11:12 +03:00
|
|
|
TRACE2("READ %d\n", last_page);
|
2001-12-14 18:09:55 +03:00
|
|
|
got = read(id->fd, pBuf, amt);
|
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
|
|
|
#endif
|
|
|
|
#if OS_WIN
|
2001-09-20 05:44:42 +04:00
|
|
|
DWORD got;
|
2001-10-12 21:30:04 +04:00
|
|
|
SimulateIOError(SQLITE_IOERR);
|
2001-12-14 18:09:55 +03:00
|
|
|
if( !ReadFile(id->h, pBuf, amt, &got, 0) ){
|
2001-09-19 17:22:39 +04:00
|
|
|
got = 0;
|
|
|
|
}
|
2002-09-05 20:08:27 +04:00
|
|
|
if( got==amt ){
|
|
|
|
return SQLITE_OK;
|
|
|
|
}else{
|
|
|
|
return SQLITE_IOERR;
|
|
|
|
}
|
2001-09-19 17:22:39 +04:00
|
|
|
#endif
|
|
|
|
}
|
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
|
|
|
*/
|
2001-12-14 18:09:55 +03:00
|
|
|
int sqliteOsWrite(OsFile *id, const void *pBuf, int amt){
|
2001-09-19 17:22:39 +04:00
|
|
|
#if OS_UNIX
|
2002-09-05 20:08:27 +04:00
|
|
|
int wrote = 0;
|
2001-10-12 21:30:04 +04:00
|
|
|
SimulateIOError(SQLITE_IOERR);
|
2002-03-05 04:11:12 +03:00
|
|
|
TRACE2("WRITE %d\n", last_page);
|
2002-09-05 20:08:27 +04:00
|
|
|
while( amt>0 && (wrote = write(id->fd, pBuf, amt))>0 ){
|
|
|
|
amt -= wrote;
|
|
|
|
pBuf = &((char*)pBuf)[wrote];
|
|
|
|
}
|
|
|
|
if( amt>0 ){
|
|
|
|
return SQLITE_FULL;
|
|
|
|
}
|
2001-09-19 17:22:39 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
|
|
|
#if OS_WIN
|
2002-09-05 20:08:27 +04:00
|
|
|
int rc;
|
2001-09-20 05:44:42 +04:00
|
|
|
DWORD wrote;
|
2001-10-12 21:30:04 +04:00
|
|
|
SimulateIOError(SQLITE_IOERR);
|
2002-09-05 20:08:27 +04:00
|
|
|
while( amt>0 && (rc = WriteFile(id->h, pBuf, amt, &wrote, 0))!=0 && wrote>0 ){
|
|
|
|
amt -= wrote;
|
|
|
|
pBuf = &((char*)pBuf)[wrote];
|
|
|
|
}
|
|
|
|
if( !rc || amt>(int)wrote ){
|
2001-09-19 17:22:39 +04:00
|
|
|
return SQLITE_FULL;
|
|
|
|
}
|
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
|
|
|
** Move the read/write pointer in a file.
|
|
|
|
*/
|
2001-12-14 18:09:55 +03:00
|
|
|
int sqliteOsSeek(OsFile *id, int offset){
|
2002-03-05 04:11:12 +03:00
|
|
|
SEEK(offset/1024 + 1);
|
2001-09-19 17:22:39 +04:00
|
|
|
#if OS_UNIX
|
2001-12-14 18:09:55 +03:00
|
|
|
lseek(id->fd, offset, SEEK_SET);
|
2001-09-19 17:22:39 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
|
|
|
#if OS_WIN
|
2001-12-14 18:09:55 +03:00
|
|
|
SetFilePointer(id->h, offset, 0, FILE_BEGIN);
|
2001-09-19 17:22:39 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
|
|
|
** Make sure all writes to a particular file are committed to disk.
|
|
|
|
*/
|
2001-12-14 18:09:55 +03:00
|
|
|
int sqliteOsSync(OsFile *id){
|
2001-10-12 21:30:04 +04:00
|
|
|
SimulateIOError(SQLITE_IOERR);
|
2002-03-05 04:11:12 +03:00
|
|
|
TRACE1("SYNC\n");
|
2001-09-19 17:22:39 +04:00
|
|
|
#if OS_UNIX
|
2002-09-05 20:08:27 +04:00
|
|
|
if( fsync(id->fd) ){
|
|
|
|
return SQLITE_IOERR;
|
|
|
|
}else{
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
2001-09-19 17:22:39 +04:00
|
|
|
#endif
|
|
|
|
#if OS_WIN
|
2002-09-05 20:08:27 +04:00
|
|
|
if( FlushFileBuffers(id->h) ){
|
|
|
|
return SQLITE_OK;
|
|
|
|
}else{
|
|
|
|
return SQLITE_IOERR;
|
|
|
|
}
|
2001-09-19 17:22:39 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
|
|
|
** Truncate an open file to a specified size
|
|
|
|
*/
|
2001-12-14 18:09:55 +03:00
|
|
|
int sqliteOsTruncate(OsFile *id, int nByte){
|
2001-10-12 21:30:04 +04:00
|
|
|
SimulateIOError(SQLITE_IOERR);
|
2001-09-19 17:22:39 +04:00
|
|
|
#if OS_UNIX
|
2001-12-14 18:09:55 +03:00
|
|
|
return ftruncate(id->fd, nByte)==0 ? SQLITE_OK : SQLITE_IOERR;
|
2001-09-19 17:22:39 +04:00
|
|
|
#endif
|
|
|
|
#if OS_WIN
|
2001-12-14 18:09:55 +03:00
|
|
|
SetFilePointer(id->h, nByte, 0, FILE_BEGIN);
|
|
|
|
SetEndOfFile(id->h);
|
2001-09-19 17:22:39 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
|
|
|
** Determine the current size of a file in bytes
|
|
|
|
*/
|
2001-12-14 18:09:55 +03:00
|
|
|
int sqliteOsFileSize(OsFile *id, int *pSize){
|
2001-09-19 17:22:39 +04:00
|
|
|
#if OS_UNIX
|
|
|
|
struct stat buf;
|
2001-10-12 21:30:04 +04:00
|
|
|
SimulateIOError(SQLITE_IOERR);
|
2001-12-14 18:09:55 +03:00
|
|
|
if( fstat(id->fd, &buf)!=0 ){
|
2001-09-19 17:22:39 +04:00
|
|
|
return SQLITE_IOERR;
|
|
|
|
}
|
|
|
|
*pSize = buf.st_size;
|
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
|
|
|
#if OS_WIN
|
2001-10-12 21:30:04 +04:00
|
|
|
SimulateIOError(SQLITE_IOERR);
|
2001-12-14 18:09:55 +03:00
|
|
|
*pSize = GetFileSize(id->h, 0);
|
2001-09-19 17:22:39 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2002-08-14 16:56:54 +04:00
|
|
|
/*
|
|
|
|
** Windows file locking notes:
|
|
|
|
**
|
|
|
|
** We cannot use LockFileEx() or UnlockFileEx() because those functions
|
|
|
|
** are not available under Win95/98/ME. So we use only LockFile() and
|
|
|
|
** UnlockFile().
|
|
|
|
**
|
|
|
|
** A read lock is obtained by locking a single random byte in the
|
|
|
|
** range of 1 to MX_LOCKBYTE. The lock byte is obtained at random so
|
|
|
|
** two separate readers can probably access the file at the same time,
|
|
|
|
** unless they are unlucky and choose the same lock byte. A write lock
|
|
|
|
** is obtained by locking all bytes in the range of 1 to MX_LOCKBYTE.
|
|
|
|
** There can only be one writer.
|
|
|
|
**
|
|
|
|
** A lock is obtained on byte 0 before acquiring either a read lock or
|
|
|
|
** a write lock. This prevents two processes from attempting to get a
|
|
|
|
** lock at a same time. The semantics of sqliteOsReadLock() require that
|
|
|
|
** if there is already a write lock, that lock is converted into a read
|
|
|
|
** lock atomically. The lock on byte 0 allows us to drop the old write
|
|
|
|
** lock and get the read lock without another process jumping into the
|
|
|
|
** middle and messing us up. The same argument applies to sqliteOsWriteLock().
|
|
|
|
**
|
|
|
|
** There are a finite number of read locks under windows. That number
|
|
|
|
** is determined by the following variable:
|
|
|
|
*/
|
|
|
|
#define MX_LOCKBYTE 10240
|
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
2001-12-14 18:09:55 +03:00
|
|
|
** Change the status of the lock on the file "id" to be a readlock.
|
|
|
|
** If the file was write locked, then this reduces the lock to a read.
|
|
|
|
** If the file was read locked, then this acquires a new read lock.
|
|
|
|
**
|
|
|
|
** Return SQLITE_OK on success and SQLITE_BUSY on failure.
|
2001-09-18 00:25:58 +04:00
|
|
|
*/
|
2001-12-14 18:09:55 +03:00
|
|
|
int sqliteOsReadLock(OsFile *id){
|
2001-09-19 17:22:39 +04:00
|
|
|
#if OS_UNIX
|
|
|
|
int rc;
|
2001-10-09 08:19:46 +04:00
|
|
|
sqliteOsEnterMutex();
|
2001-12-14 18:09:55 +03:00
|
|
|
if( id->pLock->cnt>0 ){
|
|
|
|
if( !id->locked ){
|
|
|
|
id->pLock->cnt++;
|
|
|
|
id->locked = 1;
|
2001-10-09 08:19:46 +04:00
|
|
|
}
|
2001-12-14 18:09:55 +03:00
|
|
|
rc = SQLITE_OK;
|
|
|
|
}else if( id->locked || id->pLock->cnt==0 ){
|
|
|
|
struct flock lock;
|
|
|
|
lock.l_type = F_RDLCK;
|
|
|
|
lock.l_whence = SEEK_SET;
|
|
|
|
lock.l_start = lock.l_len = 0L;
|
|
|
|
if( fcntl(id->fd, F_SETLK, &lock)!=0 ){
|
2001-10-09 08:19:46 +04:00
|
|
|
rc = SQLITE_BUSY;
|
|
|
|
}else{
|
|
|
|
rc = SQLITE_OK;
|
2001-12-14 18:09:55 +03:00
|
|
|
id->pLock->cnt = 1;
|
|
|
|
id->locked = 1;
|
2001-10-09 08:19:46 +04:00
|
|
|
}
|
2001-12-14 18:09:55 +03:00
|
|
|
}else{
|
|
|
|
rc = SQLITE_BUSY;
|
2001-10-08 17:22:32 +04:00
|
|
|
}
|
2001-12-14 18:09:55 +03:00
|
|
|
sqliteOsLeaveMutex();
|
|
|
|
return rc;
|
|
|
|
#endif
|
|
|
|
#if OS_WIN
|
|
|
|
int rc;
|
2002-08-14 16:56:54 +04:00
|
|
|
if( id->locked>0 ){
|
2001-12-14 18:09:55 +03:00
|
|
|
rc = SQLITE_OK;
|
|
|
|
}else{
|
2002-08-14 16:56:54 +04:00
|
|
|
int lk = (sqliteRandomInteger() & 0x7ffffff)%MX_LOCKBYTE + 1;
|
|
|
|
int res;
|
|
|
|
if( (res = LockFile(id->h, 0, 0, 1, 0))!=0 ){
|
|
|
|
UnlockFile(id->h, 1, 0, MX_LOCKBYTE, 0);
|
|
|
|
res = LockFile(id->h, lk, 0, 1, 0);
|
|
|
|
UnlockFile(id->h, 0, 0, 1, 0);
|
|
|
|
}
|
|
|
|
if( res ){
|
|
|
|
id->locked = lk;
|
|
|
|
rc = SQLITE_OK;
|
|
|
|
}else{
|
|
|
|
rc = SQLITE_BUSY;
|
|
|
|
}
|
2001-12-14 18:09:55 +03:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Change the lock status to be an exclusive or write lock. Return
|
|
|
|
** SQLITE_OK on success and SQLITE_BUSY on a failure.
|
|
|
|
*/
|
|
|
|
int sqliteOsWriteLock(OsFile *id){
|
|
|
|
#if OS_UNIX
|
|
|
|
int rc;
|
|
|
|
sqliteOsEnterMutex();
|
|
|
|
if( id->pLock->cnt==0 || (id->pLock->cnt==1 && id->locked==1) ){
|
2001-10-09 08:19:46 +04:00
|
|
|
struct flock lock;
|
2001-12-14 18:09:55 +03:00
|
|
|
lock.l_type = F_WRLCK;
|
2001-10-09 08:19:46 +04:00
|
|
|
lock.l_whence = SEEK_SET;
|
|
|
|
lock.l_start = lock.l_len = 0L;
|
2001-12-14 18:09:55 +03:00
|
|
|
if( fcntl(id->fd, F_SETLK, &lock)!=0 ){
|
|
|
|
rc = SQLITE_BUSY;
|
|
|
|
}else{
|
|
|
|
rc = SQLITE_OK;
|
|
|
|
id->pLock->cnt = -1;
|
|
|
|
id->locked = 1;
|
2001-12-06 16:24:14 +03:00
|
|
|
}
|
2001-12-14 18:09:55 +03:00
|
|
|
}else{
|
|
|
|
rc = SQLITE_BUSY;
|
2001-10-09 08:19:46 +04:00
|
|
|
}
|
2001-12-14 18:09:55 +03:00
|
|
|
sqliteOsLeaveMutex();
|
2001-10-09 08:19:46 +04:00
|
|
|
return rc;
|
2001-09-19 17:22:39 +04:00
|
|
|
#endif
|
|
|
|
#if OS_WIN
|
2001-12-14 18:09:55 +03:00
|
|
|
int rc;
|
2002-08-14 16:56:54 +04:00
|
|
|
if( id->locked<0 ){
|
2001-12-14 18:09:55 +03:00
|
|
|
rc = SQLITE_OK;
|
|
|
|
}else{
|
2002-08-14 16:56:54 +04:00
|
|
|
int res;
|
|
|
|
if( (res = LockFile(id->h, 0, 0, 1, 0))!=0 ){
|
|
|
|
if( id->locked==0 || UnlockFile(id->h, id->locked, 0, 1, 0) ){
|
|
|
|
res = LockFile(id->h, 1, 0, MX_LOCKBYTE, 0);
|
|
|
|
}else{
|
|
|
|
res = 0;
|
|
|
|
}
|
|
|
|
UnlockFile(id->h, 0, 0, 1, 0);
|
|
|
|
}
|
|
|
|
if( res ){
|
|
|
|
id->locked = -1;
|
|
|
|
rc = SQLITE_OK;
|
|
|
|
}else{
|
|
|
|
rc = SQLITE_BUSY;
|
|
|
|
}
|
2001-09-19 17:22:39 +04:00
|
|
|
}
|
2001-12-14 18:09:55 +03:00
|
|
|
return rc;
|
2001-09-19 17:22:39 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
2001-12-14 18:09:55 +03:00
|
|
|
** Unlock the given file descriptor. If the file descriptor was
|
|
|
|
** not previously locked, then this routine is a no-op.
|
2001-09-18 00:25:58 +04:00
|
|
|
*/
|
2001-12-14 18:09:55 +03:00
|
|
|
int sqliteOsUnlock(OsFile *id){
|
2001-09-19 17:22:39 +04:00
|
|
|
#if OS_UNIX
|
|
|
|
int rc;
|
2001-12-14 18:09:55 +03:00
|
|
|
if( !id->locked ) return SQLITE_OK;
|
2001-10-09 08:19:46 +04:00
|
|
|
sqliteOsEnterMutex();
|
2001-12-14 18:09:55 +03:00
|
|
|
assert( id->pLock->cnt!=0 );
|
|
|
|
if( id->pLock->cnt>1 ){
|
|
|
|
id->pLock->cnt--;
|
2001-10-09 08:19:46 +04:00
|
|
|
rc = SQLITE_OK;
|
2001-12-14 18:09:55 +03:00
|
|
|
}else{
|
2001-10-09 08:19:46 +04:00
|
|
|
struct flock lock;
|
|
|
|
lock.l_type = F_UNLCK;
|
|
|
|
lock.l_whence = SEEK_SET;
|
|
|
|
lock.l_start = lock.l_len = 0L;
|
2001-12-14 18:09:55 +03:00
|
|
|
if( fcntl(id->fd, F_SETLK, &lock)!=0 ){
|
|
|
|
rc = SQLITE_BUSY;
|
|
|
|
}else{
|
|
|
|
rc = SQLITE_OK;
|
|
|
|
id->pLock->cnt = 0;
|
|
|
|
}
|
2001-10-09 08:19:46 +04:00
|
|
|
}
|
2001-12-06 16:24:14 +03:00
|
|
|
sqliteOsLeaveMutex();
|
2001-12-14 18:09:55 +03:00
|
|
|
id->locked = 0;
|
2001-10-09 08:19:46 +04:00
|
|
|
return rc;
|
2001-09-19 17:22:39 +04:00
|
|
|
#endif
|
|
|
|
#if OS_WIN
|
2001-12-14 18:09:55 +03:00
|
|
|
int rc;
|
2002-08-14 16:56:54 +04:00
|
|
|
if( id->locked==0 ){
|
2001-12-14 18:09:55 +03:00
|
|
|
rc = SQLITE_OK;
|
2002-08-14 16:56:54 +04:00
|
|
|
}else if( id->locked<0 ){
|
|
|
|
UnlockFile(id->h, 1, 0, MX_LOCKBYTE, 0);
|
2001-12-14 18:09:55 +03:00
|
|
|
rc = SQLITE_OK;
|
|
|
|
id->locked = 0;
|
|
|
|
}else{
|
2002-08-14 16:56:54 +04:00
|
|
|
UnlockFile(id->h, id->locked, 0, 1, 0);
|
|
|
|
rc = SQLITE_OK;
|
|
|
|
id->locked = 0;
|
2001-12-14 18:09:55 +03:00
|
|
|
}
|
|
|
|
return rc;
|
2001-09-19 17:22:39 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2001-09-19 17:22:39 +04:00
|
|
|
int sqliteOsRandomSeed(char *zBuf){
|
2002-07-07 20:52:46 +04:00
|
|
|
#ifdef SQLITE_TEST
|
|
|
|
/* When testing, always use the same random number sequence.
|
|
|
|
** This makes the tests repeatable.
|
|
|
|
*/
|
|
|
|
memset(zBuf, 0, 256);
|
|
|
|
#endif
|
|
|
|
#if OS_UNIX && !defined(SQLITE_TEST)
|
2001-09-19 17:22:39 +04:00
|
|
|
int pid;
|
|
|
|
time((time_t*)zBuf);
|
|
|
|
pid = getpid();
|
2001-09-23 23:46:51 +04:00
|
|
|
memcpy(&zBuf[sizeof(time_t)], &pid, sizeof(pid));
|
2001-09-19 17:22:39 +04:00
|
|
|
#endif
|
2002-07-07 20:52:46 +04:00
|
|
|
#if OS_WIN && !defined(SQLITE_TEST)
|
2001-09-19 17:22:39 +04:00
|
|
|
GetSystemTime((LPSYSTEMTIME)zBuf);
|
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
|
|
|
*/
|
2001-09-19 17:22:39 +04:00
|
|
|
int sqliteOsSleep(int ms){
|
|
|
|
#if OS_UNIX
|
|
|
|
#if defined(HAVE_USLEEP) && HAVE_USLEEP
|
|
|
|
usleep(ms*1000);
|
|
|
|
return ms;
|
|
|
|
#else
|
|
|
|
sleep((ms+999)/1000);
|
|
|
|
return 1000*((ms+999)/1000);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#if OS_WIN
|
|
|
|
Sleep(ms);
|
|
|
|
return ms;
|
|
|
|
#endif
|
|
|
|
}
|
2001-10-09 08:19:46 +04:00
|
|
|
|
2002-01-15 21:39:43 +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.
|
|
|
|
*/
|
|
|
|
#if OS_UNIX && defined(THREADSAFE) && THREADSAFE
|
|
|
|
# include <pthread.h>
|
|
|
|
# define SQLITE_UNIX_THREADS 1
|
|
|
|
#endif
|
|
|
|
#if OS_WIN && defined(THREADSAFE) && THREADSAFE
|
|
|
|
# define SQLITE_W32_THREADS 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Static variables used for thread synchronization
|
|
|
|
*/
|
|
|
|
static int inMutex = 0;
|
|
|
|
#ifdef SQLITE_UNIX_THREADS
|
|
|
|
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
#endif
|
|
|
|
#ifdef SQLITE_W32_THREADS
|
|
|
|
static CRITICAL_SECTION cs;
|
|
|
|
#endif
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
void sqliteOsEnterMutex(){
|
2002-01-15 21:39:43 +03:00
|
|
|
#ifdef SQLITE_UNIX_THREADS
|
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
#endif
|
|
|
|
#ifdef SQLITE_W32_THREADS
|
|
|
|
static int isInit = 0;
|
|
|
|
while( !isInit ){
|
|
|
|
static long lock = 0;
|
|
|
|
if( InterlockedIncrement(&lock)==1 ){
|
|
|
|
InitializeCriticalSection(&cs);
|
|
|
|
isInit = 1;
|
|
|
|
}else{
|
|
|
|
Sleep(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EnterCriticalSection(&cs);
|
|
|
|
#endif
|
2001-10-09 08:19:46 +04:00
|
|
|
assert( !inMutex );
|
|
|
|
inMutex = 1;
|
|
|
|
}
|
|
|
|
void sqliteOsLeaveMutex(){
|
|
|
|
assert( inMutex );
|
|
|
|
inMutex = 0;
|
2002-01-15 21:39:43 +03:00
|
|
|
#ifdef SQLITE_UNIX_THREADS
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
#endif
|
|
|
|
#ifdef SQLITE_W32_THREADS
|
|
|
|
LeaveCriticalSection(&cs);
|
|
|
|
#endif
|
2001-10-09 08:19:46 +04:00
|
|
|
}
|