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.
|
|
|
|
*/
|
2002-11-06 17:08:11 +03:00
|
|
|
#include "os.h" /* Must be first to enable large file support */
|
2001-09-18 00:25:58 +04:00
|
|
|
#include "sqliteInt.h"
|
|
|
|
|
|
|
|
#if OS_UNIX
|
|
|
|
# include <time.h>
|
2002-11-09 03:33:15 +03:00
|
|
|
# include <errno.h>
|
2002-12-08 00:45:14 +03:00
|
|
|
# include <unistd.h>
|
2002-11-06 02:03:02 +03:00
|
|
|
# ifndef O_LARGEFILE
|
|
|
|
# define O_LARGEFILE 0
|
|
|
|
# endif
|
2002-11-09 03:33:15 +03:00
|
|
|
# ifdef SQLITE_DISABLE_LFS
|
|
|
|
# undef O_LARGEFILE
|
|
|
|
# define O_LARGEFILE 0
|
|
|
|
# endif
|
2002-11-06 02:03:02 +03:00
|
|
|
# ifndef O_NOFOLLOW
|
|
|
|
# define O_NOFOLLOW 0
|
|
|
|
# endif
|
2001-09-18 00:25:58 +04:00
|
|
|
#endif
|
2002-11-06 02:03:02 +03:00
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
#if OS_WIN
|
|
|
|
# include <winbase.h>
|
|
|
|
#endif
|
|
|
|
|
2003-01-08 16:02:52 +03:00
|
|
|
#if OS_MAC
|
|
|
|
# include <extras.h>
|
|
|
|
# include <path2fss.h>
|
|
|
|
# include <TextUtils.h>
|
|
|
|
# include <FinderRegistry.h>
|
|
|
|
# include <Folders.h>
|
|
|
|
# include <Timer.h>
|
|
|
|
# include <OSUtils.h>
|
|
|
|
#endif
|
|
|
|
|
2002-03-05 04:11:12 +03:00
|
|
|
/*
|
|
|
|
** Macros for performance tracing. Normally turned off
|
|
|
|
*/
|
2002-12-02 07:25:19 +03:00
|
|
|
#if 0
|
2002-03-05 04:11:12 +03:00
|
|
|
static int last_page = 0;
|
2003-01-21 05:39:36 +03:00
|
|
|
__inline__ unsigned long long int hwtime(void){
|
|
|
|
unsigned long long int x;
|
|
|
|
__asm__("rdtsc\n\t"
|
|
|
|
"mov %%edx, %%ecx\n\t"
|
|
|
|
:"=A" (x));
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
static unsigned long long int g_start;
|
|
|
|
static unsigned int elapse;
|
|
|
|
#define TIMER_START g_start=hwtime()
|
|
|
|
#define TIMER_END elapse=hwtime()-g_start
|
|
|
|
#define SEEK(X) last_page=(X)
|
|
|
|
#define TRACE1(X) fprintf(stderr,X)
|
|
|
|
#define TRACE2(X,Y) fprintf(stderr,X,Y)
|
|
|
|
#define TRACE3(X,Y,Z) fprintf(stderr,X,Y,Z)
|
|
|
|
#define TRACE4(X,Y,Z,A) fprintf(stderr,X,Y,Z,A)
|
|
|
|
#define TRACE5(X,Y,Z,A,B) fprintf(stderr,X,Y,Z,A,B)
|
2002-03-05 04:11:12 +03:00
|
|
|
#else
|
2003-01-21 05:39:36 +03:00
|
|
|
#define TIMER_START
|
|
|
|
#define TIMER_END
|
2002-03-05 04:11:12 +03:00
|
|
|
#define SEEK(X)
|
|
|
|
#define TRACE1(X)
|
|
|
|
#define TRACE2(X,Y)
|
2002-12-01 05:00:57 +03:00
|
|
|
#define TRACE3(X,Y,Z)
|
2003-01-21 05:39:36 +03:00
|
|
|
#define TRACE4(X,Y,Z,A)
|
|
|
|
#define TRACE5(X,Y,Z,A,B)
|
2002-03-05 04:11:12 +03:00
|
|
|
#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
|
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.
|
|
|
|
*/
|
|
|
|
|
2001-10-09 08:19:46 +04:00
|
|
|
/*
|
|
|
|
** An instance of the following structure serves as the key used
|
2002-10-20 22:19:45 +04:00
|
|
|
** to locate a particular lockInfo structure given its inode.
|
2001-10-09 08:19:46 +04:00
|
|
|
*/
|
|
|
|
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 */
|
|
|
|
};
|
|
|
|
|
2002-10-20 22:19:45 +04:00
|
|
|
/*
|
2001-10-09 08:19:46 +04:00
|
|
|
** 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
|
|
|
|
|
2003-02-17 01:21:32 +03:00
|
|
|
/*
|
|
|
|
** When testing, keep a count of the number of open files.
|
|
|
|
*/
|
|
|
|
#ifdef SQLITE_TEST
|
|
|
|
int sqlite_open_file_count = 0;
|
|
|
|
#define OpenCounter(X) sqlite_open_file_count+=(X)
|
|
|
|
#else
|
|
|
|
#define OpenCounter(X)
|
|
|
|
#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);
|
2003-01-08 16:02:52 +03:00
|
|
|
#endif
|
|
|
|
#if OS_MAC
|
|
|
|
unlink(zFilename);
|
2001-09-19 17:22:39 +04:00
|
|
|
#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
|
2003-01-08 16:02:52 +03:00
|
|
|
#if OS_MAC
|
|
|
|
return access(zFilename, 0)==0;
|
|
|
|
#endif
|
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
|
|
|
*/
|
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
|
2002-11-06 02:03:02 +03:00
|
|
|
id->fd = open(zFilename, O_RDWR|O_CREAT|O_LARGEFILE, 0644);
|
2001-12-14 18:09:55 +03:00
|
|
|
if( id->fd<0 ){
|
2002-11-06 02:03:02 +03:00
|
|
|
id->fd = open(zFilename, O_RDONLY|O_LARGEFILE);
|
2001-12-14 18:09:55 +03:00
|
|
|
if( id->fd<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;
|
|
|
|
}
|
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;
|
2002-12-02 07:25:19 +03:00
|
|
|
TRACE3("OPEN %-3d %s\n", id->fd, zFilename);
|
2003-02-17 01:21:32 +03:00
|
|
|
OpenCounter(+1);
|
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;
|
2003-02-17 01:21:32 +03:00
|
|
|
OpenCounter(+1);
|
2001-09-18 00:25:58 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
2003-01-08 16:02:52 +03:00
|
|
|
#if OS_MAC
|
|
|
|
FSSpec fsSpec;
|
|
|
|
# ifdef _LARGE_FILE
|
|
|
|
HFSUniStr255 dfName;
|
|
|
|
FSRef fsRef;
|
|
|
|
if( __path2fss(zFilename, &fsSpec) != noErr ){
|
|
|
|
if( HCreate(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, 'SQLI', cDocumentFile) != noErr )
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
}
|
|
|
|
if( FSpMakeFSRef(&fsSpec, &fsRef) != noErr )
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
FSGetDataForkName(&dfName);
|
|
|
|
if( FSOpenFork(&fsRef, dfName.length, dfName.unicode,
|
|
|
|
fsRdWrShPerm, &(id->refNum)) != noErr ){
|
|
|
|
if( FSOpenFork(&fsRef, dfName.length, dfName.unicode,
|
|
|
|
fsRdWrPerm, &(id->refNum)) != noErr ){
|
|
|
|
if (FSOpenFork(&fsRef, dfName.length, dfName.unicode,
|
|
|
|
fsRdPerm, &(id->refNum)) != noErr )
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
else
|
|
|
|
*pReadonly = 1;
|
|
|
|
} else
|
|
|
|
*pReadonly = 0;
|
|
|
|
} else
|
|
|
|
*pReadonly = 0;
|
|
|
|
# else
|
|
|
|
__path2fss(zFilename, &fsSpec);
|
|
|
|
if( !sqliteOsFileExists(zFilename) ){
|
|
|
|
if( HCreate(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, 'SQLI', cDocumentFile) != noErr )
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
}
|
|
|
|
if( HOpenDF(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, fsRdWrShPerm, &(id->refNum)) != noErr ){
|
|
|
|
if( HOpenDF(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, fsRdWrPerm, &(id->refNum)) != noErr ){
|
|
|
|
if( HOpenDF(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, fsRdPerm, &(id->refNum)) != noErr )
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
else
|
|
|
|
*pReadonly = 1;
|
|
|
|
} else
|
|
|
|
*pReadonly = 0;
|
|
|
|
} else
|
|
|
|
*pReadonly = 0;
|
|
|
|
# endif
|
|
|
|
if( HOpenRF(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, fsRdWrShPerm, &(id->refNumRF)) != noErr){
|
|
|
|
id->refNumRF = -1;
|
|
|
|
}
|
|
|
|
id->locked = 0;
|
|
|
|
id->delOnClose = 0;
|
2003-02-17 01:21:32 +03:00
|
|
|
OpenCounter(+1);
|
2003-01-08 16:02:52 +03: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;
|
|
|
|
}
|
2002-11-06 02:03:02 +03:00
|
|
|
id->fd = open(zFilename, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_LARGEFILE, 0600);
|
2001-12-14 18:09:55 +03:00
|
|
|
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);
|
|
|
|
}
|
2002-12-02 07:25:19 +03:00
|
|
|
TRACE3("OPEN-EX %-3d %s\n", id->fd, zFilename);
|
2003-02-17 01:21:32 +03:00
|
|
|
OpenCounter(+1);
|
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 ){
|
2002-10-20 22:19:45 +04:00
|
|
|
fileflags = FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_RANDOM_ACCESS
|
2002-02-02 18:01:15 +03:00
|
|
|
| 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;
|
2003-02-17 01:21:32 +03:00
|
|
|
OpenCounter(+1);
|
2001-09-18 00:25:58 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
2003-01-08 16:02:52 +03:00
|
|
|
#if OS_MAC
|
|
|
|
FSSpec fsSpec;
|
|
|
|
# ifdef _LARGE_FILE
|
|
|
|
HFSUniStr255 dfName;
|
|
|
|
FSRef fsRef;
|
|
|
|
__path2fss(zFilename, &fsSpec);
|
|
|
|
if( HCreate(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, 'SQLI', cDocumentFile) != noErr )
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
if( FSpMakeFSRef(&fsSpec, &fsRef) != noErr )
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
FSGetDataForkName(&dfName);
|
|
|
|
if( FSOpenFork(&fsRef, dfName.length, dfName.unicode,
|
|
|
|
fsRdWrPerm, &(id->refNum)) != noErr )
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
# else
|
|
|
|
__path2fss(zFilename, &fsSpec);
|
|
|
|
if( HCreate(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, 'SQLI', cDocumentFile) != noErr )
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
if( HOpenDF(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, fsRdWrPerm, &(id->refNum)) != noErr )
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
# endif
|
|
|
|
id->refNumRF = -1;
|
|
|
|
id->locked = 0;
|
|
|
|
id->delOnClose = delFlag;
|
|
|
|
if (delFlag)
|
|
|
|
id->pathToDel = sqliteOsFullPathname(zFilename);
|
2003-02-17 01:21:32 +03:00
|
|
|
OpenCounter(+1);
|
2003-01-08 16:02:52 +03:00
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
2001-09-18 00:25:58 +04:00
|
|
|
}
|
|
|
|
|
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
|
2002-11-06 02:03:02 +03:00
|
|
|
id->fd = open(zFilename, O_RDONLY|O_LARGEFILE);
|
2001-12-14 18:09:55 +03:00
|
|
|
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;
|
2002-12-02 07:25:19 +03:00
|
|
|
TRACE3("OPEN-RO %-3d %s\n", id->fd, zFilename);
|
2003-02-17 01:21:32 +03:00
|
|
|
OpenCounter(+1);
|
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;
|
2003-02-17 01:21:32 +03:00
|
|
|
OpenCounter(+1);
|
2001-09-19 17:58:43 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
2003-01-08 16:02:52 +03:00
|
|
|
#if OS_MAC
|
|
|
|
FSSpec fsSpec;
|
|
|
|
# ifdef _LARGE_FILE
|
|
|
|
HFSUniStr255 dfName;
|
|
|
|
FSRef fsRef;
|
|
|
|
if( __path2fss(zFilename, &fsSpec) != noErr )
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
if( FSpMakeFSRef(&fsSpec, &fsRef) != noErr )
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
FSGetDataForkName(&dfName);
|
|
|
|
if( FSOpenFork(&fsRef, dfName.length, dfName.unicode,
|
|
|
|
fsRdPerm, &(id->refNum)) != noErr )
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
# else
|
|
|
|
__path2fss(zFilename, &fsSpec);
|
|
|
|
if( HOpenDF(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, fsRdPerm, &(id->refNum)) != noErr )
|
|
|
|
return SQLITE_CANTOPEN;
|
|
|
|
# endif
|
|
|
|
if( HOpenRF(fsSpec.vRefNum, fsSpec.parID, fsSpec.name, fsRdWrShPerm, &(id->refNumRF)) != noErr){
|
|
|
|
id->refNumRF = -1;
|
|
|
|
}
|
|
|
|
id->locked = 0;
|
|
|
|
id->delOnClose = 0;
|
2003-02-17 01:21:32 +03:00
|
|
|
OpenCounter(+1);
|
2003-01-08 16:02:52 +03:00
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
2001-09-19 17:58:43 +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.
|
|
|
|
*/
|
|
|
|
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{
|
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);
|
|
|
|
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(;;){
|
2003-01-08 16:02:52 +03:00
|
|
|
sprintf(zBuf, "%s\\"TEMP_FILE_PREFIX, zTempPath);
|
|
|
|
j = strlen(zBuf);
|
|
|
|
for(i=0; i<15; i++){
|
|
|
|
int n = sqliteRandomByte() % sizeof(zChars);
|
|
|
|
zBuf[j++] = zChars[n];
|
|
|
|
}
|
|
|
|
zBuf[j] = 0;
|
|
|
|
if( !sqliteOsFileExists(zBuf) ) break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if OS_MAC
|
|
|
|
static char zChars[] =
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"0123456789";
|
|
|
|
int i, j;
|
|
|
|
char zTempPath[SQLITE_TEMPNAME_SIZE];
|
|
|
|
char zdirName[32];
|
|
|
|
CInfoPBRec infoRec;
|
|
|
|
Str31 dirName;
|
|
|
|
memset(&infoRec, 0, sizeof(infoRec));
|
|
|
|
memset(zTempPath, 0, SQLITE_TEMPNAME_SIZE);
|
|
|
|
if( FindFolder(kOnSystemDisk, kTemporaryFolderType, kCreateFolder,
|
|
|
|
&(infoRec.dirInfo.ioVRefNum), &(infoRec.dirInfo.ioDrParID)) == noErr ){
|
|
|
|
infoRec.dirInfo.ioNamePtr = dirName;
|
|
|
|
do{
|
|
|
|
infoRec.dirInfo.ioFDirIndex = -1;
|
|
|
|
infoRec.dirInfo.ioDrDirID = infoRec.dirInfo.ioDrParID;
|
|
|
|
if( PBGetCatInfoSync(&infoRec) == noErr ){
|
|
|
|
CopyPascalStringToC(dirName, zdirName);
|
|
|
|
i = strlen(zdirName);
|
|
|
|
memmove(&(zTempPath[i+1]), zTempPath, strlen(zTempPath));
|
|
|
|
strcpy(zTempPath, zdirName);
|
|
|
|
zTempPath[i] = ':';
|
|
|
|
}else{
|
|
|
|
*zTempPath = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while( infoRec.dirInfo.ioDrDirID != fsRtDirID );
|
|
|
|
}
|
|
|
|
if( *zTempPath == 0 )
|
|
|
|
getcwd(zTempPath, SQLITE_TEMPNAME_SIZE-24);
|
|
|
|
for(;;){
|
|
|
|
sprintf(zBuf, "%s"TEMP_FILE_PREFIX, 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
|
2002-10-20 22:19:45 +04:00
|
|
|
return SQLITE_OK;
|
2001-09-18 00:25:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** 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();
|
2002-12-02 07:25:19 +03:00
|
|
|
TRACE2("CLOSE %-3d\n", id->fd);
|
2003-02-17 01:21:32 +03:00
|
|
|
OpenCounter(-1);
|
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);
|
2003-02-17 01:21:32 +03:00
|
|
|
OpenCounter(-1);
|
2001-09-18 00:25:58 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
2003-01-08 16:02:52 +03:00
|
|
|
#if OS_MAC
|
|
|
|
if( id->refNumRF!=-1 )
|
|
|
|
FSClose(id->refNumRF);
|
|
|
|
# ifdef _LARGE_FILE
|
|
|
|
FSCloseFork(id->refNum);
|
|
|
|
# else
|
|
|
|
FSClose(id->refNum);
|
|
|
|
# endif
|
|
|
|
if( id->delOnClose ){
|
|
|
|
unlink(id->pathToDel);
|
|
|
|
sqliteFree(id->pathToDel);
|
|
|
|
}
|
2003-02-17 01:21:32 +03:00
|
|
|
OpenCounter(-1);
|
|
|
|
return SQLITE_OK;
|
2003-01-08 16:02:52 +03:00
|
|
|
#endif
|
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
|
|
|
*/
|
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);
|
2003-01-21 05:39:36 +03:00
|
|
|
TIMER_START;
|
2001-12-14 18:09:55 +03:00
|
|
|
got = read(id->fd, pBuf, amt);
|
2003-01-21 05:39:36 +03:00
|
|
|
TIMER_END;
|
|
|
|
TRACE4("READ %-3d %7d %d\n", id->fd, last_page, elapse);
|
|
|
|
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
|
|
|
#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);
|
2002-12-01 05:00:57 +03:00
|
|
|
TRACE2("READ %d\n", last_page);
|
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-10-20 22:19:45 +04:00
|
|
|
if( got==(DWORD)amt ){
|
2002-09-05 20:08:27 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
}else{
|
|
|
|
return SQLITE_IOERR;
|
|
|
|
}
|
2001-09-19 17:22:39 +04:00
|
|
|
#endif
|
2003-01-08 16:02:52 +03:00
|
|
|
#if OS_MAC
|
|
|
|
int got;
|
|
|
|
SimulateIOError(SQLITE_IOERR);
|
|
|
|
TRACE2("READ %d\n", last_page);
|
|
|
|
# ifdef _LARGE_FILE
|
|
|
|
FSReadFork(id->refNum, fsAtMark, 0, (ByteCount)amt, pBuf, (ByteCount*)&got);
|
|
|
|
# else
|
|
|
|
got = amt;
|
|
|
|
FSRead(id->refNum, &got, pBuf);
|
|
|
|
# endif
|
|
|
|
if( got==amt ){
|
|
|
|
return SQLITE_OK;
|
|
|
|
}else{
|
|
|
|
return SQLITE_IOERR;
|
|
|
|
}
|
|
|
|
#endif
|
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
|
|
|
*/
|
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);
|
2003-01-21 05:39:36 +03:00
|
|
|
TIMER_START;
|
2002-09-05 20:08:27 +04:00
|
|
|
while( amt>0 && (wrote = write(id->fd, pBuf, amt))>0 ){
|
|
|
|
amt -= wrote;
|
|
|
|
pBuf = &((char*)pBuf)[wrote];
|
|
|
|
}
|
2003-01-21 05:39:36 +03:00
|
|
|
TIMER_END;
|
|
|
|
TRACE4("WRITE %-3d %7d %d\n", id->fd, last_page, elapse);
|
|
|
|
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;
|
|
|
|
#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-12-01 05:00:57 +03:00
|
|
|
TRACE2("WRITE %d\n", last_page);
|
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
|
2003-01-08 16:02:52 +03:00
|
|
|
#if OS_MAC
|
|
|
|
OSErr oserr;
|
|
|
|
int wrote = 0;
|
|
|
|
SimulateIOError(SQLITE_IOERR);
|
|
|
|
TRACE2("WRITE %d\n", last_page);
|
|
|
|
while( amt>0 ){
|
|
|
|
# ifdef _LARGE_FILE
|
|
|
|
oserr = FSWriteFork(id->refNum, fsAtMark, 0,
|
|
|
|
(ByteCount)amt, pBuf, (ByteCount*)&wrote);
|
|
|
|
# else
|
|
|
|
wrote = amt;
|
|
|
|
oserr = FSWrite(id->refNum, &wrote, pBuf);
|
|
|
|
# endif
|
|
|
|
if( wrote == 0 || oserr != noErr)
|
|
|
|
break;
|
|
|
|
amt -= wrote;
|
|
|
|
pBuf = &((char*)pBuf)[wrote];
|
|
|
|
}
|
|
|
|
if( oserr != noErr || amt>wrote ){
|
|
|
|
return SQLITE_FULL;
|
|
|
|
}
|
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
2001-09-19 17:22:39 +04:00
|
|
|
}
|
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
|
|
|
** Move the read/write pointer in a file.
|
|
|
|
*/
|
2002-11-06 02:03:02 +03:00
|
|
|
int sqliteOsSeek(OsFile *id, off_t 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
|
2002-11-06 17:08:11 +03:00
|
|
|
{
|
|
|
|
LONG upperBits = offset>>32;
|
2002-12-01 05:00:57 +03:00
|
|
|
LONG lowerBits = offset & 0xffffffff;
|
|
|
|
DWORD rc;
|
|
|
|
rc = SetFilePointer(id->h, lowerBits, &upperBits, FILE_BEGIN);
|
2002-12-02 07:25:19 +03:00
|
|
|
/* TRACE3("SEEK rc=0x%x upper=0x%x\n", rc, upperBits); */
|
2002-11-06 17:08:11 +03:00
|
|
|
}
|
2001-09-19 17:22:39 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
2003-01-08 16:02:52 +03:00
|
|
|
#if OS_MAC
|
|
|
|
{
|
|
|
|
off_t curSize;
|
|
|
|
if( sqliteOsFileSize(id, &curSize) != SQLITE_OK ){
|
|
|
|
return SQLITE_IOERR;
|
|
|
|
}
|
|
|
|
if( offset >= curSize ){
|
|
|
|
if( sqliteOsTruncate(id, offset+1) != SQLITE_OK ){
|
|
|
|
return SQLITE_IOERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# ifdef _LARGE_FILE
|
|
|
|
if( FSSetForkPosition(id->refNum, fsFromStart, offset) != noErr ){
|
|
|
|
# else
|
|
|
|
if( SetFPos(id->refNum, fsFromStart, offset) != noErr ){
|
|
|
|
# endif
|
|
|
|
return SQLITE_IOERR;
|
|
|
|
}else{
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2001-09-19 17:22:39 +04:00
|
|
|
}
|
|
|
|
|
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){
|
2003-01-08 16:02:52 +03:00
|
|
|
#if OS_UNIX
|
2001-10-12 21:30:04 +04:00
|
|
|
SimulateIOError(SQLITE_IOERR);
|
2002-12-02 07:25:19 +03:00
|
|
|
TRACE2("SYNC %-3d\n", id->fd);
|
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
|
2003-01-08 16:02:52 +03:00
|
|
|
#if OS_MAC
|
|
|
|
# ifdef _LARGE_FILE
|
|
|
|
if( FSFlushFork(id->refNum) != noErr ){
|
|
|
|
# else
|
|
|
|
ParamBlockRec params;
|
|
|
|
memset(¶ms, 0, sizeof(ParamBlockRec));
|
|
|
|
params.ioParam.ioRefNum = id->refNum;
|
|
|
|
if( PBFlushFileSync(¶ms) != noErr ){
|
|
|
|
# endif
|
|
|
|
return SQLITE_IOERR;
|
|
|
|
}else{
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
#endif
|
2001-09-19 17:22:39 +04:00
|
|
|
}
|
|
|
|
|
2001-09-18 00:25:58 +04:00
|
|
|
/*
|
|
|
|
** Truncate an open file to a specified size
|
|
|
|
*/
|
2002-11-06 02:03:02 +03:00
|
|
|
int sqliteOsTruncate(OsFile *id, off_t 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
|
2002-11-06 17:08:11 +03:00
|
|
|
{
|
|
|
|
LONG upperBits = nByte>>32;
|
|
|
|
SetFilePointer(id->h, nByte, &upperBits, FILE_BEGIN);
|
|
|
|
SetEndOfFile(id->h);
|
|
|
|
}
|
2001-09-19 17:22:39 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
2003-01-08 16:02:52 +03:00
|
|
|
#if OS_MAC
|
|
|
|
# ifdef _LARGE_FILE
|
|
|
|
if( FSSetForkSize(id->refNum, fsFromStart, nByte) != noErr){
|
|
|
|
# else
|
|
|
|
if( SetEOF(id->refNum, nByte) != noErr ){
|
|
|
|
# endif
|
|
|
|
return SQLITE_IOERR;
|
|
|
|
}else{
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
#endif
|
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
|
|
|
|
*/
|
2002-11-06 02:03:02 +03:00
|
|
|
int sqliteOsFileSize(OsFile *id, off_t *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
|
2002-11-06 17:08:11 +03:00
|
|
|
DWORD upperBits, lowerBits;
|
2001-10-12 21:30:04 +04:00
|
|
|
SimulateIOError(SQLITE_IOERR);
|
2002-11-06 17:08:11 +03:00
|
|
|
lowerBits = GetFileSize(id->h, &upperBits);
|
|
|
|
*pSize = (((off_t)upperBits)<<32) + lowerBits;
|
2001-09-19 17:22:39 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
#endif
|
2003-01-08 16:02:52 +03:00
|
|
|
#if OS_MAC
|
|
|
|
# ifdef _LARGE_FILE
|
|
|
|
if( FSGetForkSize(id->refNum, pSize) != noErr){
|
|
|
|
# else
|
|
|
|
if( GetEOF(id->refNum, pSize) != noErr ){
|
|
|
|
# endif
|
|
|
|
return SQLITE_IOERR;
|
|
|
|
}else{
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
#endif
|
2001-09-19 17:22:39 +04:00
|
|
|
}
|
|
|
|
|
2002-10-20 22:19:45 +04:00
|
|
|
#if OS_WIN
|
|
|
|
/*
|
|
|
|
** Return true (non-zero) if we are running under WinNT, Win2K or WinXP.
|
|
|
|
** Return false (zero) for Win95, Win98, or WinME.
|
|
|
|
*/
|
|
|
|
int isNT(void){
|
|
|
|
static osType = 0; /* 0=unknown 1=win95 2=winNT */
|
|
|
|
if( osType==0 ){
|
|
|
|
OSVERSIONINFO sInfo;
|
|
|
|
sInfo.dwOSVersionInfoSize = sizeof(sInfo);
|
|
|
|
GetVersionEx(&sInfo);
|
|
|
|
osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
|
|
|
|
}
|
|
|
|
return osType==2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-08-14 16:56:54 +04:00
|
|
|
/*
|
2003-01-08 16:02:52 +03:00
|
|
|
** Windows file locking notes: [the same/equivalent applies to MacOS]
|
2002-08-14 16:56:54 +04:00
|
|
|
**
|
|
|
|
** We cannot use LockFileEx() or UnlockFileEx() because those functions
|
|
|
|
** are not available under Win95/98/ME. So we use only LockFile() and
|
|
|
|
** UnlockFile().
|
|
|
|
**
|
2002-10-20 22:19:45 +04:00
|
|
|
** LockFile() prevents not just writing but also reading by other processes.
|
|
|
|
** (This is a design error on the part of Windows, but there is nothing
|
|
|
|
** we can do about that.) So the region used for locking is at the
|
|
|
|
** end of the file where it is unlikely to ever interfere with an
|
|
|
|
** actual read attempt.
|
2002-08-14 16:56:54 +04:00
|
|
|
**
|
2002-10-20 22:19:45 +04:00
|
|
|
** A database read lock is obtained by locking a single randomly-chosen
|
|
|
|
** byte out of a specific range of bytes. 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 database write lock is obtained by locking all bytes in the range.
|
|
|
|
** There can only be one writer.
|
2002-08-14 16:56:54 +04:00
|
|
|
**
|
2002-10-20 22:19:45 +04:00
|
|
|
** A lock is obtained on the first byte of the lock range 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 the first
|
|
|
|
** byte 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().
|
2002-10-17 04:38:54 +04:00
|
|
|
**
|
2003-01-08 16:02:52 +03:00
|
|
|
** Note: On MacOS we use the resource fork for locking.
|
|
|
|
**
|
2002-10-20 22:19:45 +04:00
|
|
|
** The following #defines specify the range of bytes used for locking.
|
|
|
|
** N_LOCKBYTE is the number of bytes available for doing the locking.
|
|
|
|
** The first byte used to hold the lock while the lock is changing does
|
|
|
|
** not count toward this number. FIRST_LOCKBYTE is the address of
|
|
|
|
** the first byte in the range of bytes used for locking.
|
2002-08-14 16:56:54 +04:00
|
|
|
*/
|
2002-10-20 22:19:45 +04:00
|
|
|
#define N_LOCKBYTE 10239
|
2003-01-08 16:02:52 +03:00
|
|
|
#if OS_MAC
|
|
|
|
# define FIRST_LOCKBYTE (0x000fffff - N_LOCKBYTE)
|
|
|
|
#else
|
|
|
|
# define FIRST_LOCKBYTE (0xffffffff - N_LOCKBYTE)
|
|
|
|
#endif
|
2002-08-14 16:56:54 +04:00
|
|
|
|
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.
|
|
|
|
**
|
2002-11-09 03:33:15 +03:00
|
|
|
** Return SQLITE_OK on success and SQLITE_BUSY on failure. If this
|
|
|
|
** library was compiled with large file support (LFS) but LFS is not
|
|
|
|
** available on the host, then an SQLITE_NOLFS is returned.
|
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;
|
2002-11-09 03:33:15 +03:00
|
|
|
int s;
|
2001-12-14 18:09:55 +03:00
|
|
|
lock.l_type = F_RDLCK;
|
|
|
|
lock.l_whence = SEEK_SET;
|
|
|
|
lock.l_start = lock.l_len = 0L;
|
2002-11-09 03:33:15 +03:00
|
|
|
s = fcntl(id->fd, F_SETLK, &lock);
|
|
|
|
if( s!=0 ){
|
|
|
|
rc = (s==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
|
2001-10-09 08:19:46 +04:00
|
|
|
}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-10-20 22:19:45 +04:00
|
|
|
int lk = (sqliteRandomInteger() & 0x7ffffff)%N_LOCKBYTE+1;
|
2002-08-14 16:56:54 +04:00
|
|
|
int res;
|
2002-10-20 22:19:45 +04:00
|
|
|
int cnt = 100;
|
|
|
|
int page = isNT() ? 0xffffffff : 0;
|
|
|
|
while( cnt-->0 && (res = LockFile(id->h, FIRST_LOCKBYTE, page, 1, 0))==0 ){
|
|
|
|
Sleep(1);
|
|
|
|
}
|
|
|
|
if( res ){
|
|
|
|
UnlockFile(id->h, FIRST_LOCKBYTE+1, page, N_LOCKBYTE, 0);
|
|
|
|
res = LockFile(id->h, FIRST_LOCKBYTE+lk, page, 1, 0);
|
|
|
|
UnlockFile(id->h, FIRST_LOCKBYTE, page, 1, 0);
|
|
|
|
}
|
2002-08-14 16:56:54 +04:00
|
|
|
if( res ){
|
|
|
|
id->locked = lk;
|
|
|
|
rc = SQLITE_OK;
|
|
|
|
}else{
|
|
|
|
rc = SQLITE_BUSY;
|
|
|
|
}
|
2001-12-14 18:09:55 +03:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
#endif
|
2003-01-08 16:02:52 +03:00
|
|
|
#if OS_MAC
|
|
|
|
int rc;
|
|
|
|
if( id->locked>0 || id->refNumRF == -1 ){
|
|
|
|
rc = SQLITE_OK;
|
|
|
|
}else{
|
|
|
|
int lk = (sqliteRandomInteger() & 0x7ffffff)%N_LOCKBYTE+1;
|
|
|
|
OSErr res;
|
|
|
|
int cnt = 5;
|
|
|
|
ParamBlockRec params;
|
|
|
|
memset(¶ms, 0, sizeof(params));
|
|
|
|
params.ioParam.ioRefNum = id->refNumRF;
|
|
|
|
params.ioParam.ioPosMode = fsFromStart;
|
|
|
|
params.ioParam.ioPosOffset = FIRST_LOCKBYTE;
|
|
|
|
params.ioParam.ioReqCount = 1;
|
|
|
|
while( cnt-->0 && (res = PBLockRangeSync(¶ms))!=noErr ){
|
|
|
|
UInt32 finalTicks;
|
|
|
|
Delay(1, &finalTicks); /* 1/60 sec */
|
|
|
|
}
|
|
|
|
if( res == noErr ){
|
|
|
|
params.ioParam.ioPosOffset = FIRST_LOCKBYTE+1;
|
|
|
|
params.ioParam.ioReqCount = N_LOCKBYTE;
|
|
|
|
PBUnlockRangeSync(¶ms);
|
|
|
|
params.ioParam.ioPosOffset = FIRST_LOCKBYTE+lk;
|
|
|
|
params.ioParam.ioReqCount = 1;
|
|
|
|
res = PBLockRangeSync(¶ms);
|
|
|
|
params.ioParam.ioPosOffset = FIRST_LOCKBYTE;
|
|
|
|
params.ioParam.ioReqCount = 1;
|
|
|
|
PBUnlockRangeSync(¶ms);
|
|
|
|
}
|
|
|
|
if( res == noErr ){
|
|
|
|
id->locked = lk;
|
|
|
|
rc = SQLITE_OK;
|
|
|
|
}else{
|
|
|
|
rc = SQLITE_BUSY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
#endif
|
2001-12-14 18:09:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Change the lock status to be an exclusive or write lock. Return
|
2002-11-09 03:33:15 +03:00
|
|
|
** SQLITE_OK on success and SQLITE_BUSY on a failure. If this
|
|
|
|
** library was compiled with large file support (LFS) but LFS is not
|
|
|
|
** available on the host, then an SQLITE_NOLFS is returned.
|
2001-12-14 18:09:55 +03:00
|
|
|
*/
|
|
|
|
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;
|
2002-11-09 03:33:15 +03:00
|
|
|
int s;
|
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;
|
2002-11-09 03:33:15 +03:00
|
|
|
s = fcntl(id->fd, F_SETLK, &lock);
|
|
|
|
if( s!=0 ){
|
|
|
|
rc = (s==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
|
2001-12-14 18:09:55 +03:00
|
|
|
}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;
|
2002-10-20 22:19:45 +04:00
|
|
|
int cnt = 100;
|
|
|
|
int page = isNT() ? 0xffffffff : 0;
|
|
|
|
while( cnt-->0 && (res = LockFile(id->h, FIRST_LOCKBYTE, page, 1, 0))==0 ){
|
|
|
|
Sleep(1);
|
|
|
|
}
|
|
|
|
if( res ){
|
|
|
|
if( id->locked==0
|
|
|
|
|| UnlockFile(id->h, FIRST_LOCKBYTE + id->locked, page, 1, 0) ){
|
|
|
|
res = LockFile(id->h, FIRST_LOCKBYTE+1, page, N_LOCKBYTE, 0);
|
|
|
|
}else{
|
|
|
|
res = 0;
|
|
|
|
}
|
|
|
|
UnlockFile(id->h, FIRST_LOCKBYTE, page, 1, 0);
|
|
|
|
}
|
|
|
|
if( res ){
|
2002-08-14 16:56:54 +04:00
|
|
|
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
|
2003-01-08 16:02:52 +03:00
|
|
|
#if OS_MAC
|
|
|
|
int rc;
|
|
|
|
if( id->locked<0 || id->refNumRF == -1 ){
|
|
|
|
rc = SQLITE_OK;
|
|
|
|
}else{
|
|
|
|
OSErr res;
|
|
|
|
int cnt = 5;
|
|
|
|
ParamBlockRec params;
|
|
|
|
memset(¶ms, 0, sizeof(params));
|
|
|
|
params.ioParam.ioRefNum = id->refNumRF;
|
|
|
|
params.ioParam.ioPosMode = fsFromStart;
|
|
|
|
params.ioParam.ioPosOffset = FIRST_LOCKBYTE;
|
|
|
|
params.ioParam.ioReqCount = 1;
|
|
|
|
while( cnt-->0 && (res = PBLockRangeSync(¶ms))!=noErr ){
|
|
|
|
UInt32 finalTicks;
|
|
|
|
Delay(1, &finalTicks); /* 1/60 sec */
|
|
|
|
}
|
|
|
|
if( res == noErr ){
|
|
|
|
params.ioParam.ioPosOffset = FIRST_LOCKBYTE + id->locked;
|
|
|
|
params.ioParam.ioReqCount = 1;
|
|
|
|
if( id->locked==0
|
|
|
|
|| PBUnlockRangeSync(¶ms)==noErr ){
|
|
|
|
params.ioParam.ioPosOffset = FIRST_LOCKBYTE+1;
|
|
|
|
params.ioParam.ioReqCount = N_LOCKBYTE;
|
|
|
|
res = PBLockRangeSync(¶ms);
|
|
|
|
}else{
|
|
|
|
res = afpRangeNotLocked;
|
|
|
|
}
|
|
|
|
params.ioParam.ioPosOffset = FIRST_LOCKBYTE;
|
|
|
|
params.ioParam.ioReqCount = 1;
|
|
|
|
PBUnlockRangeSync(¶ms);
|
|
|
|
}
|
|
|
|
if( res == noErr ){
|
|
|
|
id->locked = -1;
|
|
|
|
rc = SQLITE_OK;
|
|
|
|
}else{
|
|
|
|
rc = SQLITE_BUSY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
#endif
|
2001-09-19 17:22:39 +04:00
|
|
|
}
|
|
|
|
|
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
|
2002-11-09 03:33:15 +03:00
|
|
|
** not previously locked, then this routine is a no-op. If this
|
|
|
|
** library was compiled with large file support (LFS) but LFS is not
|
|
|
|
** available on the host, then an SQLITE_NOLFS is returned.
|
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;
|
2002-11-09 03:33:15 +03:00
|
|
|
int s;
|
2001-10-09 08:19:46 +04:00
|
|
|
lock.l_type = F_UNLCK;
|
|
|
|
lock.l_whence = SEEK_SET;
|
|
|
|
lock.l_start = lock.l_len = 0L;
|
2002-11-09 03:33:15 +03:00
|
|
|
s = fcntl(id->fd, F_SETLK, &lock);
|
|
|
|
if( s!=0 ){
|
|
|
|
rc = (s==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
|
2001-12-14 18:09:55 +03:00
|
|
|
}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-10-20 22:19:45 +04:00
|
|
|
int page = isNT() ? 0xffffffff : 0;
|
|
|
|
if( id->locked==0 ){
|
|
|
|
rc = SQLITE_OK;
|
|
|
|
}else if( id->locked<0 ){
|
|
|
|
UnlockFile(id->h, FIRST_LOCKBYTE+1, page, N_LOCKBYTE, 0);
|
|
|
|
rc = SQLITE_OK;
|
|
|
|
id->locked = 0;
|
|
|
|
}else{
|
|
|
|
UnlockFile(id->h, FIRST_LOCKBYTE+id->locked, page, 1, 0);
|
|
|
|
rc = SQLITE_OK;
|
|
|
|
id->locked = 0;
|
2001-12-14 18:09:55 +03:00
|
|
|
}
|
2002-10-20 22:19:45 +04:00
|
|
|
return rc;
|
2001-09-19 17:22:39 +04:00
|
|
|
#endif
|
2003-01-08 16:02:52 +03:00
|
|
|
#if OS_MAC
|
|
|
|
int rc;
|
|
|
|
ParamBlockRec params;
|
|
|
|
memset(¶ms, 0, sizeof(params));
|
|
|
|
params.ioParam.ioRefNum = id->refNumRF;
|
|
|
|
params.ioParam.ioPosMode = fsFromStart;
|
|
|
|
if( id->locked==0 || id->refNumRF == -1 ){
|
|
|
|
rc = SQLITE_OK;
|
|
|
|
}else if( id->locked<0 ){
|
|
|
|
params.ioParam.ioPosOffset = FIRST_LOCKBYTE+1;
|
|
|
|
params.ioParam.ioReqCount = N_LOCKBYTE;
|
|
|
|
PBUnlockRangeSync(¶ms);
|
|
|
|
rc = SQLITE_OK;
|
|
|
|
id->locked = 0;
|
|
|
|
}else{
|
|
|
|
params.ioParam.ioPosOffset = FIRST_LOCKBYTE+id->locked;
|
|
|
|
params.ioParam.ioReqCount = 1;
|
|
|
|
PBUnlockRangeSync(¶ms);
|
|
|
|
rc = SQLITE_OK;
|
|
|
|
id->locked = 0;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
#endif
|
2001-09-19 17:22:39 +04:00
|
|
|
}
|
|
|
|
|
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);
|
2003-01-08 16:02:52 +03:00
|
|
|
#endif
|
|
|
|
#if OS_MAC
|
|
|
|
int pid;
|
|
|
|
Microseconds((UnsignedWide*)zBuf);
|
|
|
|
pid = getpid();
|
|
|
|
memcpy(&zBuf[sizeof(UnsignedWide)], &pid, sizeof(pid));
|
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
|
2003-01-08 16:02:52 +03:00
|
|
|
#if OS_MAC
|
|
|
|
UInt32 finalTicks;
|
|
|
|
UInt32 ticks = (((UInt32)ms+16)*3)/50; /* 1/60 sec per tick */
|
|
|
|
Delay(ticks, &finalTicks);
|
|
|
|
return (int)((ticks*50)/3);
|
|
|
|
#endif
|
2001-09-19 17:22:39 +04:00
|
|
|
}
|
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
|
2003-01-08 16:02:52 +03:00
|
|
|
#if OS_MAC && defined(THREADSAFE) && THREADSAFE
|
|
|
|
# include <Multiprocessing.h>
|
|
|
|
# define SQLITE_MACOS_MULTITASKING 1
|
|
|
|
#endif
|
2002-01-15 21:39:43 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** 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
|
2003-01-08 16:02:52 +03:00
|
|
|
#ifdef SQLITE_MACOS_MULTITASKING
|
|
|
|
static MPCriticalRegionID criticalRegion;
|
|
|
|
#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.
|
|
|
|
*/
|
|
|
|
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);
|
2003-01-08 16:02:52 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SQLITE_MACOS_MULTITASKING
|
|
|
|
static volatile int notInit = 1;
|
|
|
|
if( notInit ){
|
|
|
|
if( notInit == 2 ) /* as close as you can get to thread safe init */
|
|
|
|
MPYield();
|
|
|
|
else{
|
|
|
|
notInit = 2;
|
|
|
|
MPCreateCriticalRegion(&criticalRegion);
|
|
|
|
notInit = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MPEnterCriticalRegion(criticalRegion, kDurationForever);
|
2002-01-15 21:39:43 +03:00
|
|
|
#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
|
2003-01-08 16:02:52 +03:00
|
|
|
#ifdef SQLITE_MACOS_MULTITASKING
|
|
|
|
MPExitCriticalRegion(criticalRegion);
|
|
|
|
#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.
|
|
|
|
*/
|
|
|
|
char *sqliteOsFullPathname(const char *zRelative){
|
|
|
|
#if OS_UNIX
|
|
|
|
char *zFull = 0;
|
|
|
|
if( zRelative[0]=='/' ){
|
|
|
|
sqliteSetString(&zFull, zRelative, 0);
|
|
|
|
}else{
|
|
|
|
char zBuf[5000];
|
|
|
|
sqliteSetString(&zFull, getcwd(zBuf, sizeof(zBuf)), "/", zRelative, 0);
|
|
|
|
}
|
|
|
|
return zFull;
|
|
|
|
#endif
|
|
|
|
#if OS_WIN
|
|
|
|
char *zNotUsed;
|
|
|
|
char *zFull;
|
|
|
|
int nByte;
|
2003-01-08 16:02:52 +03:00
|
|
|
nByte = GetFullPathName(zRelative, 0, 0, &zNotUsed) + 1;
|
2002-12-08 00:45:14 +03:00
|
|
|
zFull = sqliteMalloc( nByte );
|
|
|
|
if( zFull==0 ) return 0;
|
|
|
|
GetFullPathName(zRelative, nByte, zFull, &zNotUsed);
|
|
|
|
return zFull;
|
|
|
|
#endif
|
2003-01-08 16:02:52 +03:00
|
|
|
#if OS_MAC
|
|
|
|
char *zFull = 0;
|
|
|
|
if( zRelative[0]==':' ){
|
|
|
|
char zBuf[_MAX_PATH+1];
|
|
|
|
sqliteSetString(&zFull, getcwd(zBuf, sizeof(zBuf)), &(zRelative[1]), 0);
|
|
|
|
}else{
|
|
|
|
if( strchr(zRelative, ':') ){
|
|
|
|
sqliteSetString(&zFull, zRelative, 0);
|
|
|
|
}else{
|
|
|
|
char zBuf[_MAX_PATH+1];
|
|
|
|
sqliteSetString(&zFull, getcwd(zBuf, sizeof(zBuf)), zRelative, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return zFull;
|
|
|
|
#endif
|
2002-12-08 00:45:14 +03:00
|
|
|
}
|