ds@1952: /* ds@1952: ** 2004 May 22 ds@1952: ** ds@1952: ** The author disclaims copyright to this source code. In place of ds@1952: ** a legal notice, here is a blessing: ds@1952: ** ds@1952: ** May you do good and not evil. ds@1952: ** May you find forgiveness for yourself and forgive others. ds@1952: ** May you share freely, never taking more than you give. ds@1952: ** ds@1952: ****************************************************************************** ds@1952: ** ds@1952: ** This file contains code that is specific to windows. ds@1952: */ ds@1952: #include "sqliteInt.h" ds@1952: #if OS_WIN /* This file is used for windows only */ ds@1952: ds@1952: ds@1952: /* ds@1952: ** A Note About Memory Allocation: ds@1952: ** ds@1952: ** This driver uses malloc()/free() directly rather than going through ds@1952: ** the SQLite-wrappers sqlite3_malloc()/sqlite3_free(). Those wrappers ds@1952: ** are designed for use on embedded systems where memory is scarce and ds@1952: ** malloc failures happen frequently. Win32 does not typically run on ds@1952: ** embedded systems, and when it does the developers normally have bigger ds@1952: ** problems to worry about than running out of memory. So there is not ds@1952: ** a compelling need to use the wrappers. ds@1952: ** ds@1952: ** But there is a good reason to not use the wrappers. If we use the ds@1952: ** wrappers then we will get simulated malloc() failures within this ds@1952: ** driver. And that causes all kinds of problems for our tests. We ds@1952: ** could enhance SQLite to deal with simulated malloc failures within ds@1952: ** the OS driver, but the code to deal with those failure would not ds@1952: ** be exercised on Linux (which does not need to malloc() in the driver) ds@1952: ** and so we would have difficulty writing coverage tests for that ds@1952: ** code. Better to leave the code out, we think. ds@1952: ** ds@1952: ** The point of this discussion is as follows: When creating a new ds@1952: ** OS layer for an embedded system, if you use this file as an example, ds@1952: ** avoid the use of malloc()/free(). Those routines work ok on windows ds@1952: ** desktops but not so well in embedded systems. ds@1952: */ ds@1952: ds@1952: #include ds@1952: ds@1952: #ifdef __CYGWIN__ ds@1952: # include ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** Macros used to determine whether or not to use threads. ds@1952: */ ds@1952: #if defined(THREADSAFE) && THREADSAFE ds@1952: # define SQLITE_W32_THREADS 1 ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** Include code that is common to all os_*.c files ds@1952: */ ds@1952: #include "os_common.h" ds@1952: ds@1952: /* ds@1952: ** Determine if we are dealing with WindowsCE - which has a much ds@1952: ** reduced API. ds@1952: */ ds@1952: #if defined(_WIN32_WCE) ds@1952: # define OS_WINCE 1 ds@1952: # define AreFileApisANSI() 1 ds@1952: #else ds@1952: # define OS_WINCE 0 ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** WinCE lacks native support for file locking so we have to fake it ds@1952: ** with some code of our own. ds@1952: */ ds@1952: #if OS_WINCE ds@1952: typedef struct winceLock { ds@1952: int nReaders; /* Number of reader locks obtained */ ds@1952: BOOL bPending; /* Indicates a pending lock has been obtained */ ds@1952: BOOL bReserved; /* Indicates a reserved lock has been obtained */ ds@1952: BOOL bExclusive; /* Indicates an exclusive lock has been obtained */ ds@1952: } winceLock; ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** The winFile structure is a subclass of sqlite3_file* specific to the win32 ds@1952: ** portability layer. ds@1952: */ ds@1952: typedef struct winFile winFile; ds@1952: struct winFile { ds@1952: const sqlite3_io_methods *pMethod;/* Must be first */ ds@1952: HANDLE h; /* Handle for accessing the file */ ds@1952: unsigned char locktype; /* Type of lock currently held on this file */ ds@1952: short sharedLockByte; /* Randomly chosen byte used as a shared lock */ ds@1952: #if OS_WINCE ds@1952: WCHAR *zDeleteOnClose; /* Name of file to delete when closing */ ds@1952: HANDLE hMutex; /* Mutex used to control access to shared lock */ ds@1952: HANDLE hShared; /* Shared memory segment used for locking */ ds@1952: winceLock local; /* Locks obtained by this instance of winFile */ ds@1952: winceLock *shared; /* Global shared lock memory for the file */ ds@1952: #endif ds@1952: }; ds@1952: ds@1952: ds@1952: /* ds@1952: ** The following variable is (normally) set once and never changes ds@1952: ** thereafter. It records whether the operating system is Win95 ds@1952: ** or WinNT. ds@1952: ** ds@1952: ** 0: Operating system unknown. ds@1952: ** 1: Operating system is Win95. ds@1952: ** 2: Operating system is WinNT. ds@1952: ** ds@1952: ** In order to facilitate testing on a WinNT system, the test fixture ds@1952: ** can manually set this value to 1 to emulate Win98 behavior. ds@1952: */ ds@1952: #ifdef SQLITE_TEST ds@1952: int sqlite3_os_type = 0; ds@1952: #else ds@1952: static int sqlite3_os_type = 0; ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP, ds@1952: ** or WinCE. Return false (zero) for Win95, Win98, or WinME. ds@1952: ** ds@1952: ** Here is an interesting observation: Win95, Win98, and WinME lack ds@1952: ** the LockFileEx() API. But we can still statically link against that ds@1952: ** API as long as we don't call it win running Win95/98/ME. A call to ds@1952: ** this routine is used to determine if the host is Win95/98/ME or ds@1952: ** WinNT/2K/XP so that we will know whether or not we can safely call ds@1952: ** the LockFileEx() API. ds@1952: */ ds@1952: #if OS_WINCE ds@1952: # define isNT() (1) ds@1952: #else ds@1952: static int isNT(void){ ds@1952: if( sqlite3_os_type==0 ){ ds@1952: OSVERSIONINFO sInfo; ds@1952: sInfo.dwOSVersionInfoSize = sizeof(sInfo); ds@1952: GetVersionEx(&sInfo); ds@1952: sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1; ds@1952: } ds@1952: return sqlite3_os_type==2; ds@1952: } ds@1952: #endif /* OS_WINCE */ ds@1952: ds@1952: /* ds@1952: ** Convert a UTF-8 string to microsoft unicode (UTF-16?). ds@1952: ** ds@1952: ** Space to hold the returned string is obtained from malloc. ds@1952: */ ds@1952: static WCHAR *utf8ToUnicode(const char *zFilename){ ds@1952: int nChar; ds@1952: WCHAR *zWideFilename; ds@1952: ds@1952: nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0); ds@1952: zWideFilename = malloc( nChar*sizeof(zWideFilename[0]) ); ds@1952: if( zWideFilename==0 ){ ds@1952: return 0; ds@1952: } ds@1952: nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, nChar); ds@1952: if( nChar==0 ){ ds@1952: free(zWideFilename); ds@1952: zWideFilename = 0; ds@1952: } ds@1952: return zWideFilename; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Convert microsoft unicode to UTF-8. Space to hold the returned string is ds@1952: ** obtained from malloc(). ds@1952: */ ds@1952: static char *unicodeToUtf8(const WCHAR *zWideFilename){ ds@1952: int nByte; ds@1952: char *zFilename; ds@1952: ds@1952: nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0); ds@1952: zFilename = malloc( nByte ); ds@1952: if( zFilename==0 ){ ds@1952: return 0; ds@1952: } ds@1952: nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte, ds@1952: 0, 0); ds@1952: if( nByte == 0 ){ ds@1952: free(zFilename); ds@1952: zFilename = 0; ds@1952: } ds@1952: return zFilename; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Convert an ansi string to microsoft unicode, based on the ds@1952: ** current codepage settings for file apis. ds@1952: ** ds@1952: ** Space to hold the returned string is obtained ds@1952: ** from malloc. ds@1952: */ ds@1952: static WCHAR *mbcsToUnicode(const char *zFilename){ ds@1952: int nByte; ds@1952: WCHAR *zMbcsFilename; ds@1952: int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP; ds@1952: ds@1952: nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, NULL,0)*sizeof(WCHAR); ds@1952: zMbcsFilename = malloc( nByte*sizeof(zMbcsFilename[0]) ); ds@1952: if( zMbcsFilename==0 ){ ds@1952: return 0; ds@1952: } ds@1952: nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename, nByte); ds@1952: if( nByte==0 ){ ds@1952: free(zMbcsFilename); ds@1952: zMbcsFilename = 0; ds@1952: } ds@1952: return zMbcsFilename; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Convert microsoft unicode to multibyte character string, based on the ds@1952: ** user's Ansi codepage. ds@1952: ** ds@1952: ** Space to hold the returned string is obtained from ds@1952: ** malloc(). ds@1952: */ ds@1952: static char *unicodeToMbcs(const WCHAR *zWideFilename){ ds@1952: int nByte; ds@1952: char *zFilename; ds@1952: int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP; ds@1952: ds@1952: nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0); ds@1952: zFilename = malloc( nByte ); ds@1952: if( zFilename==0 ){ ds@1952: return 0; ds@1952: } ds@1952: nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename, nByte, ds@1952: 0, 0); ds@1952: if( nByte == 0 ){ ds@1952: free(zFilename); ds@1952: zFilename = 0; ds@1952: } ds@1952: return zFilename; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Convert multibyte character string to UTF-8. Space to hold the ds@1952: ** returned string is obtained from malloc(). ds@1952: */ ds@1952: static char *mbcsToUtf8(const char *zFilename){ ds@1952: char *zFilenameUtf8; ds@1952: WCHAR *zTmpWide; ds@1952: ds@1952: zTmpWide = mbcsToUnicode(zFilename); ds@1952: if( zTmpWide==0 ){ ds@1952: return 0; ds@1952: } ds@1952: zFilenameUtf8 = unicodeToUtf8(zTmpWide); ds@1952: free(zTmpWide); ds@1952: return zFilenameUtf8; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Convert UTF-8 to multibyte character string. Space to hold the ds@1952: ** returned string is obtained from malloc(). ds@1952: */ ds@1952: static char *utf8ToMbcs(const char *zFilename){ ds@1952: char *zFilenameMbcs; ds@1952: WCHAR *zTmpWide; ds@1952: ds@1952: zTmpWide = utf8ToUnicode(zFilename); ds@1952: if( zTmpWide==0 ){ ds@1952: return 0; ds@1952: } ds@1952: zFilenameMbcs = unicodeToMbcs(zTmpWide); ds@1952: free(zTmpWide); ds@1952: return zFilenameMbcs; ds@1952: } ds@1952: ds@1952: #if OS_WINCE ds@1952: /************************************************************************* ds@1952: ** This section contains code for WinCE only. ds@1952: */ ds@1952: /* ds@1952: ** WindowsCE does not have a localtime() function. So create a ds@1952: ** substitute. ds@1952: */ ds@1952: #include ds@1952: struct tm *__cdecl localtime(const time_t *t) ds@1952: { ds@1952: static struct tm y; ds@1952: FILETIME uTm, lTm; ds@1952: SYSTEMTIME pTm; ds@1952: sqlite3_int64 t64; ds@1952: t64 = *t; ds@1952: t64 = (t64 + 11644473600)*10000000; ds@1952: uTm.dwLowDateTime = t64 & 0xFFFFFFFF; ds@1952: uTm.dwHighDateTime= t64 >> 32; ds@1952: FileTimeToLocalFileTime(&uTm,&lTm); ds@1952: FileTimeToSystemTime(&lTm,&pTm); ds@1952: y.tm_year = pTm.wYear - 1900; ds@1952: y.tm_mon = pTm.wMonth - 1; ds@1952: y.tm_wday = pTm.wDayOfWeek; ds@1952: y.tm_mday = pTm.wDay; ds@1952: y.tm_hour = pTm.wHour; ds@1952: y.tm_min = pTm.wMinute; ds@1952: y.tm_sec = pTm.wSecond; ds@1952: return &y; ds@1952: } ds@1952: ds@1952: /* This will never be called, but defined to make the code compile */ ds@1952: #define GetTempPathA(a,b) ds@1952: ds@1952: #define LockFile(a,b,c,d,e) winceLockFile(&a, b, c, d, e) ds@1952: #define UnlockFile(a,b,c,d,e) winceUnlockFile(&a, b, c, d, e) ds@1952: #define LockFileEx(a,b,c,d,e,f) winceLockFileEx(&a, b, c, d, e, f) ds@1952: ds@1952: #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-offsetof(winFile,h)] ds@1952: ds@1952: /* ds@1952: ** Acquire a lock on the handle h ds@1952: */ ds@1952: static void winceMutexAcquire(HANDLE h){ ds@1952: DWORD dwErr; ds@1952: do { ds@1952: dwErr = WaitForSingleObject(h, INFINITE); ds@1952: } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED); ds@1952: } ds@1952: /* ds@1952: ** Release a lock acquired by winceMutexAcquire() ds@1952: */ ds@1952: #define winceMutexRelease(h) ReleaseMutex(h) ds@1952: ds@1952: /* ds@1952: ** Create the mutex and shared memory used for locking in the file ds@1952: ** descriptor pFile ds@1952: */ ds@1952: static BOOL winceCreateLock(const char *zFilename, winFile *pFile){ ds@1952: WCHAR *zTok; ds@1952: WCHAR *zName = utf8ToUnicode(zFilename); ds@1952: BOOL bInit = TRUE; ds@1952: ds@1952: /* Initialize the local lockdata */ ds@1952: ZeroMemory(&pFile->local, sizeof(pFile->local)); ds@1952: ds@1952: /* Replace the backslashes from the filename and lowercase it ds@1952: ** to derive a mutex name. */ ds@1952: zTok = CharLowerW(zName); ds@1952: for (;*zTok;zTok++){ ds@1952: if (*zTok == '\\') *zTok = '_'; ds@1952: } ds@1952: ds@1952: /* Create/open the named mutex */ ds@1952: pFile->hMutex = CreateMutexW(NULL, FALSE, zName); ds@1952: if (!pFile->hMutex){ ds@1952: free(zName); ds@1952: return FALSE; ds@1952: } ds@1952: ds@1952: /* Acquire the mutex before continuing */ ds@1952: winceMutexAcquire(pFile->hMutex); ds@1952: ds@1952: /* Since the names of named mutexes, semaphores, file mappings etc are ds@1952: ** case-sensitive, take advantage of that by uppercasing the mutex name ds@1952: ** and using that as the shared filemapping name. ds@1952: */ ds@1952: CharUpperW(zName); ds@1952: pFile->hShared = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, ds@1952: PAGE_READWRITE, 0, sizeof(winceLock), ds@1952: zName); ds@1952: ds@1952: /* Set a flag that indicates we're the first to create the memory so it ds@1952: ** must be zero-initialized */ ds@1952: if (GetLastError() == ERROR_ALREADY_EXISTS){ ds@1952: bInit = FALSE; ds@1952: } ds@1952: ds@1952: free(zName); ds@1952: ds@1952: /* If we succeeded in making the shared memory handle, map it. */ ds@1952: if (pFile->hShared){ ds@1952: pFile->shared = (winceLock*)MapViewOfFile(pFile->hShared, ds@1952: FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock)); ds@1952: /* If mapping failed, close the shared memory handle and erase it */ ds@1952: if (!pFile->shared){ ds@1952: CloseHandle(pFile->hShared); ds@1952: pFile->hShared = NULL; ds@1952: } ds@1952: } ds@1952: ds@1952: /* If shared memory could not be created, then close the mutex and fail */ ds@1952: if (pFile->hShared == NULL){ ds@1952: winceMutexRelease(pFile->hMutex); ds@1952: CloseHandle(pFile->hMutex); ds@1952: pFile->hMutex = NULL; ds@1952: return FALSE; ds@1952: } ds@1952: ds@1952: /* Initialize the shared memory if we're supposed to */ ds@1952: if (bInit) { ds@1952: ZeroMemory(pFile->shared, sizeof(winceLock)); ds@1952: } ds@1952: ds@1952: winceMutexRelease(pFile->hMutex); ds@1952: return TRUE; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Destroy the part of winFile that deals with wince locks ds@1952: */ ds@1952: static void winceDestroyLock(winFile *pFile){ ds@1952: if (pFile->hMutex){ ds@1952: /* Acquire the mutex */ ds@1952: winceMutexAcquire(pFile->hMutex); ds@1952: ds@1952: /* The following blocks should probably assert in debug mode, but they ds@1952: are to cleanup in case any locks remained open */ ds@1952: if (pFile->local.nReaders){ ds@1952: pFile->shared->nReaders --; ds@1952: } ds@1952: if (pFile->local.bReserved){ ds@1952: pFile->shared->bReserved = FALSE; ds@1952: } ds@1952: if (pFile->local.bPending){ ds@1952: pFile->shared->bPending = FALSE; ds@1952: } ds@1952: if (pFile->local.bExclusive){ ds@1952: pFile->shared->bExclusive = FALSE; ds@1952: } ds@1952: ds@1952: /* De-reference and close our copy of the shared memory handle */ ds@1952: UnmapViewOfFile(pFile->shared); ds@1952: CloseHandle(pFile->hShared); ds@1952: ds@1952: if( pFile->zDeleteOnClose ){ ds@1952: DeleteFileW(pFile->zDeleteOnClose); ds@1952: free(pFile->zDeleteOnClose); ds@1952: pFile->zDeleteOnClose = 0; ds@1952: } ds@1952: ds@1952: /* Done with the mutex */ ds@1952: winceMutexRelease(pFile->hMutex); ds@1952: CloseHandle(pFile->hMutex); ds@1952: pFile->hMutex = NULL; ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** An implementation of the LockFile() API of windows for wince ds@1952: */ ds@1952: static BOOL winceLockFile( ds@1952: HANDLE *phFile, ds@1952: DWORD dwFileOffsetLow, ds@1952: DWORD dwFileOffsetHigh, ds@1952: DWORD nNumberOfBytesToLockLow, ds@1952: DWORD nNumberOfBytesToLockHigh ds@1952: ){ ds@1952: winFile *pFile = HANDLE_TO_WINFILE(phFile); ds@1952: BOOL bReturn = FALSE; ds@1952: ds@1952: if (!pFile->hMutex) return TRUE; ds@1952: winceMutexAcquire(pFile->hMutex); ds@1952: ds@1952: /* Wanting an exclusive lock? */ ds@1952: if (dwFileOffsetLow == SHARED_FIRST ds@1952: && nNumberOfBytesToLockLow == SHARED_SIZE){ ds@1952: if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){ ds@1952: pFile->shared->bExclusive = TRUE; ds@1952: pFile->local.bExclusive = TRUE; ds@1952: bReturn = TRUE; ds@1952: } ds@1952: } ds@1952: ds@1952: /* Want a read-only lock? */ ds@1952: else if ((dwFileOffsetLow >= SHARED_FIRST && ds@1952: dwFileOffsetLow < SHARED_FIRST + SHARED_SIZE) && ds@1952: nNumberOfBytesToLockLow == 1){ ds@1952: if (pFile->shared->bExclusive == 0){ ds@1952: pFile->local.nReaders ++; ds@1952: if (pFile->local.nReaders == 1){ ds@1952: pFile->shared->nReaders ++; ds@1952: } ds@1952: bReturn = TRUE; ds@1952: } ds@1952: } ds@1952: ds@1952: /* Want a pending lock? */ ds@1952: else if (dwFileOffsetLow == PENDING_BYTE && nNumberOfBytesToLockLow == 1){ ds@1952: /* If no pending lock has been acquired, then acquire it */ ds@1952: if (pFile->shared->bPending == 0) { ds@1952: pFile->shared->bPending = TRUE; ds@1952: pFile->local.bPending = TRUE; ds@1952: bReturn = TRUE; ds@1952: } ds@1952: } ds@1952: /* Want a reserved lock? */ ds@1952: else if (dwFileOffsetLow == RESERVED_BYTE && nNumberOfBytesToLockLow == 1){ ds@1952: if (pFile->shared->bReserved == 0) { ds@1952: pFile->shared->bReserved = TRUE; ds@1952: pFile->local.bReserved = TRUE; ds@1952: bReturn = TRUE; ds@1952: } ds@1952: } ds@1952: ds@1952: winceMutexRelease(pFile->hMutex); ds@1952: return bReturn; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** An implementation of the UnlockFile API of windows for wince ds@1952: */ ds@1952: static BOOL winceUnlockFile( ds@1952: HANDLE *phFile, ds@1952: DWORD dwFileOffsetLow, ds@1952: DWORD dwFileOffsetHigh, ds@1952: DWORD nNumberOfBytesToUnlockLow, ds@1952: DWORD nNumberOfBytesToUnlockHigh ds@1952: ){ ds@1952: winFile *pFile = HANDLE_TO_WINFILE(phFile); ds@1952: BOOL bReturn = FALSE; ds@1952: ds@1952: if (!pFile->hMutex) return TRUE; ds@1952: winceMutexAcquire(pFile->hMutex); ds@1952: ds@1952: /* Releasing a reader lock or an exclusive lock */ ds@1952: if (dwFileOffsetLow >= SHARED_FIRST && ds@1952: dwFileOffsetLow < SHARED_FIRST + SHARED_SIZE){ ds@1952: /* Did we have an exclusive lock? */ ds@1952: if (pFile->local.bExclusive){ ds@1952: pFile->local.bExclusive = FALSE; ds@1952: pFile->shared->bExclusive = FALSE; ds@1952: bReturn = TRUE; ds@1952: } ds@1952: ds@1952: /* Did we just have a reader lock? */ ds@1952: else if (pFile->local.nReaders){ ds@1952: pFile->local.nReaders --; ds@1952: if (pFile->local.nReaders == 0) ds@1952: { ds@1952: pFile->shared->nReaders --; ds@1952: } ds@1952: bReturn = TRUE; ds@1952: } ds@1952: } ds@1952: ds@1952: /* Releasing a pending lock */ ds@1952: else if (dwFileOffsetLow == PENDING_BYTE && nNumberOfBytesToUnlockLow == 1){ ds@1952: if (pFile->local.bPending){ ds@1952: pFile->local.bPending = FALSE; ds@1952: pFile->shared->bPending = FALSE; ds@1952: bReturn = TRUE; ds@1952: } ds@1952: } ds@1952: /* Releasing a reserved lock */ ds@1952: else if (dwFileOffsetLow == RESERVED_BYTE && nNumberOfBytesToUnlockLow == 1){ ds@1952: if (pFile->local.bReserved) { ds@1952: pFile->local.bReserved = FALSE; ds@1952: pFile->shared->bReserved = FALSE; ds@1952: bReturn = TRUE; ds@1952: } ds@1952: } ds@1952: ds@1952: winceMutexRelease(pFile->hMutex); ds@1952: return bReturn; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** An implementation of the LockFileEx() API of windows for wince ds@1952: */ ds@1952: static BOOL winceLockFileEx( ds@1952: HANDLE *phFile, ds@1952: DWORD dwFlags, ds@1952: DWORD dwReserved, ds@1952: DWORD nNumberOfBytesToLockLow, ds@1952: DWORD nNumberOfBytesToLockHigh, ds@1952: LPOVERLAPPED lpOverlapped ds@1952: ){ ds@1952: /* If the caller wants a shared read lock, forward this call ds@1952: ** to winceLockFile */ ds@1952: if (lpOverlapped->Offset == SHARED_FIRST && ds@1952: dwFlags == 1 && ds@1952: nNumberOfBytesToLockLow == SHARED_SIZE){ ds@1952: return winceLockFile(phFile, SHARED_FIRST, 0, 1, 0); ds@1952: } ds@1952: return FALSE; ds@1952: } ds@1952: /* ds@1952: ** End of the special code for wince ds@1952: *****************************************************************************/ ds@1952: #endif /* OS_WINCE */ ds@1952: ds@1952: /***************************************************************************** ds@1952: ** The next group of routines implement the I/O methods specified ds@1952: ** by the sqlite3_io_methods object. ds@1952: ******************************************************************************/ ds@1952: ds@1952: /* ds@1952: ** Close a file. ds@1952: ** ds@1952: ** It is reported that an attempt to close a handle might sometimes ds@1952: ** fail. This is a very unreasonable result, but windows is notorious ds@1952: ** for being unreasonable so I do not doubt that it might happen. If ds@1952: ** the close fails, we pause for 100 milliseconds and try again. As ds@1952: ** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before ds@1952: ** giving up and returning an error. ds@1952: */ ds@1952: #define MX_CLOSE_ATTEMPT 3 ds@1952: static int winClose(sqlite3_file *id){ ds@1952: int rc, cnt = 0; ds@1952: winFile *pFile = (winFile*)id; ds@1952: OSTRACE2("CLOSE %d\n", pFile->h); ds@1952: do{ ds@1952: rc = CloseHandle(pFile->h); ds@1952: }while( rc==0 && cnt++ < MX_CLOSE_ATTEMPT && (Sleep(100), 1) ); ds@1952: #if OS_WINCE ds@1952: winceDestroyLock(pFile); ds@1952: #endif ds@1952: OpenCounter(-1); ds@1952: return rc ? SQLITE_OK : SQLITE_IOERR; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Some microsoft compilers lack this definition. ds@1952: */ ds@1952: #ifndef INVALID_SET_FILE_POINTER ds@1952: # define INVALID_SET_FILE_POINTER ((DWORD)-1) ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** Read data from a file into a buffer. Return SQLITE_OK if all ds@1952: ** bytes were read successfully and SQLITE_IOERR if anything goes ds@1952: ** wrong. ds@1952: */ ds@1952: static int winRead( ds@1952: sqlite3_file *id, /* File to read from */ ds@1952: void *pBuf, /* Write content into this buffer */ ds@1952: int amt, /* Number of bytes to read */ ds@1952: sqlite3_int64 offset /* Begin reading at this offset */ ds@1952: ){ ds@1952: LONG upperBits = (offset>>32) & 0x7fffffff; ds@1952: LONG lowerBits = offset & 0xffffffff; ds@1952: DWORD rc; ds@1952: DWORD got; ds@1952: winFile *pFile = (winFile*)id; ds@1952: assert( id!=0 ); ds@1952: SimulateIOError(return SQLITE_IOERR_READ); ds@1952: OSTRACE3("READ %d lock=%d\n", pFile->h, pFile->locktype); ds@1952: rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN); ds@1952: if( rc==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR ){ ds@1952: return SQLITE_FULL; ds@1952: } ds@1952: if( !ReadFile(pFile->h, pBuf, amt, &got, 0) ){ ds@1952: return SQLITE_IOERR_READ; ds@1952: } ds@1952: if( got==(DWORD)amt ){ ds@1952: return SQLITE_OK; ds@1952: }else{ ds@1952: memset(&((char*)pBuf)[got], 0, amt-got); ds@1952: return SQLITE_IOERR_SHORT_READ; ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Write data from a buffer into a file. Return SQLITE_OK on success ds@1952: ** or some other error code on failure. ds@1952: */ ds@1952: static int winWrite( ds@1952: sqlite3_file *id, /* File to write into */ ds@1952: const void *pBuf, /* The bytes to be written */ ds@1952: int amt, /* Number of bytes to write */ ds@1952: sqlite3_int64 offset /* Offset into the file to begin writing at */ ds@1952: ){ ds@1952: LONG upperBits = (offset>>32) & 0x7fffffff; ds@1952: LONG lowerBits = offset & 0xffffffff; ds@1952: DWORD rc; ds@1952: DWORD wrote; ds@1952: winFile *pFile = (winFile*)id; ds@1952: assert( id!=0 ); ds@1952: SimulateIOError(return SQLITE_IOERR_WRITE); ds@1952: SimulateDiskfullError(return SQLITE_FULL); ds@1952: OSTRACE3("WRITE %d lock=%d\n", pFile->h, pFile->locktype); ds@1952: rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN); ds@1952: if( rc==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR ){ ds@1952: return SQLITE_FULL; ds@1952: } ds@1952: assert( amt>0 ); ds@1952: while( ds@1952: amt>0 ds@1952: && (rc = WriteFile(pFile->h, pBuf, amt, &wrote, 0))!=0 ds@1952: && wrote>0 ds@1952: ){ ds@1952: amt -= wrote; ds@1952: pBuf = &((char*)pBuf)[wrote]; ds@1952: } ds@1952: if( !rc || amt>(int)wrote ){ ds@1952: return SQLITE_FULL; ds@1952: } ds@1952: return SQLITE_OK; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Truncate an open file to a specified size ds@1952: */ ds@1952: static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){ ds@1952: LONG upperBits = (nByte>>32) & 0x7fffffff; ds@1952: LONG lowerBits = nByte & 0xffffffff; ds@1952: winFile *pFile = (winFile*)id; ds@1952: OSTRACE3("TRUNCATE %d %lld\n", pFile->h, nByte); ds@1952: SimulateIOError(return SQLITE_IOERR_TRUNCATE); ds@1952: SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN); ds@1952: SetEndOfFile(pFile->h); ds@1952: return SQLITE_OK; ds@1952: } ds@1952: ds@1952: #ifdef SQLITE_TEST ds@1952: /* ds@1952: ** Count the number of fullsyncs and normal syncs. This is used to test ds@1952: ** that syncs and fullsyncs are occuring at the right times. ds@1952: */ ds@1952: int sqlite3_sync_count = 0; ds@1952: int sqlite3_fullsync_count = 0; ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** Make sure all writes to a particular file are committed to disk. ds@1952: */ ds@1952: static int winSync(sqlite3_file *id, int flags){ ds@1952: winFile *pFile = (winFile*)id; ds@1952: OSTRACE3("SYNC %d lock=%d\n", pFile->h, pFile->locktype); ds@1952: #ifdef SQLITE_TEST ds@1952: if( flags & SQLITE_SYNC_FULL ){ ds@1952: sqlite3_fullsync_count++; ds@1952: } ds@1952: sqlite3_sync_count++; ds@1952: #endif ds@1952: if( FlushFileBuffers(pFile->h) ){ ds@1952: return SQLITE_OK; ds@1952: }else{ ds@1952: return SQLITE_IOERR; ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Determine the current size of a file in bytes ds@1952: */ ds@1952: static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){ ds@1952: winFile *pFile = (winFile*)id; ds@1952: DWORD upperBits, lowerBits; ds@1952: SimulateIOError(return SQLITE_IOERR_FSTAT); ds@1952: lowerBits = GetFileSize(pFile->h, &upperBits); ds@1952: *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits; ds@1952: return SQLITE_OK; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems. ds@1952: */ ds@1952: #ifndef LOCKFILE_FAIL_IMMEDIATELY ds@1952: # define LOCKFILE_FAIL_IMMEDIATELY 1 ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** Acquire a reader lock. ds@1952: ** Different API routines are called depending on whether or not this ds@1952: ** is Win95 or WinNT. ds@1952: */ ds@1952: static int getReadLock(winFile *pFile){ ds@1952: int res; ds@1952: if( isNT() ){ ds@1952: OVERLAPPED ovlp; ds@1952: ovlp.Offset = SHARED_FIRST; ds@1952: ovlp.OffsetHigh = 0; ds@1952: ovlp.hEvent = 0; ds@1952: res = LockFileEx(pFile->h, LOCKFILE_FAIL_IMMEDIATELY, ds@1952: 0, SHARED_SIZE, 0, &ovlp); ds@1952: }else{ ds@1952: int lk; ds@1952: sqlite3Randomness(sizeof(lk), &lk); ds@1952: pFile->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1); ds@1952: res = LockFile(pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0); ds@1952: } ds@1952: return res; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Undo a readlock ds@1952: */ ds@1952: static int unlockReadLock(winFile *pFile){ ds@1952: int res; ds@1952: if( isNT() ){ ds@1952: res = UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0); ds@1952: }else{ ds@1952: res = UnlockFile(pFile->h, SHARED_FIRST + pFile->sharedLockByte, 0, 1, 0); ds@1952: } ds@1952: return res; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Lock the file with the lock specified by parameter locktype - one ds@1952: ** of the following: ds@1952: ** ds@1952: ** (1) SHARED_LOCK ds@1952: ** (2) RESERVED_LOCK ds@1952: ** (3) PENDING_LOCK ds@1952: ** (4) EXCLUSIVE_LOCK ds@1952: ** ds@1952: ** Sometimes when requesting one lock state, additional lock states ds@1952: ** are inserted in between. The locking might fail on one of the later ds@1952: ** transitions leaving the lock state different from what it started but ds@1952: ** still short of its goal. The following chart shows the allowed ds@1952: ** transitions and the inserted intermediate states: ds@1952: ** ds@1952: ** UNLOCKED -> SHARED ds@1952: ** SHARED -> RESERVED ds@1952: ** SHARED -> (PENDING) -> EXCLUSIVE ds@1952: ** RESERVED -> (PENDING) -> EXCLUSIVE ds@1952: ** PENDING -> EXCLUSIVE ds@1952: ** ds@1952: ** This routine will only increase a lock. The winUnlock() routine ds@1952: ** erases all locks at once and returns us immediately to locking level 0. ds@1952: ** It is not possible to lower the locking level one step at a time. You ds@1952: ** must go straight to locking level 0. ds@1952: */ ds@1952: static int winLock(sqlite3_file *id, int locktype){ ds@1952: int rc = SQLITE_OK; /* Return code from subroutines */ ds@1952: int res = 1; /* Result of a windows lock call */ ds@1952: int newLocktype; /* Set pFile->locktype to this value before exiting */ ds@1952: int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */ ds@1952: winFile *pFile = (winFile*)id; ds@1952: ds@1952: assert( pFile!=0 ); ds@1952: OSTRACE5("LOCK %d %d was %d(%d)\n", ds@1952: pFile->h, locktype, pFile->locktype, pFile->sharedLockByte); ds@1952: ds@1952: /* If there is already a lock of this type or more restrictive on the ds@1952: ** OsFile, do nothing. Don't use the end_lock: exit path, as ds@1952: ** sqlite3OsEnterMutex() hasn't been called yet. ds@1952: */ ds@1952: if( pFile->locktype>=locktype ){ ds@1952: return SQLITE_OK; ds@1952: } ds@1952: ds@1952: /* Make sure the locking sequence is correct ds@1952: */ ds@1952: assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK ); ds@1952: assert( locktype!=PENDING_LOCK ); ds@1952: assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK ); ds@1952: ds@1952: /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or ds@1952: ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of ds@1952: ** the PENDING_LOCK byte is temporary. ds@1952: */ ds@1952: newLocktype = pFile->locktype; ds@1952: if( pFile->locktype==NO_LOCK ds@1952: || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK) ds@1952: ){ ds@1952: int cnt = 3; ds@1952: while( cnt-->0 && (res = LockFile(pFile->h, PENDING_BYTE, 0, 1, 0))==0 ){ ds@1952: /* Try 3 times to get the pending lock. The pending lock might be ds@1952: ** held by another reader process who will release it momentarily. ds@1952: */ ds@1952: OSTRACE2("could not get a PENDING lock. cnt=%d\n", cnt); ds@1952: Sleep(1); ds@1952: } ds@1952: gotPendingLock = res; ds@1952: } ds@1952: ds@1952: /* Acquire a shared lock ds@1952: */ ds@1952: if( locktype==SHARED_LOCK && res ){ ds@1952: assert( pFile->locktype==NO_LOCK ); ds@1952: res = getReadLock(pFile); ds@1952: if( res ){ ds@1952: newLocktype = SHARED_LOCK; ds@1952: } ds@1952: } ds@1952: ds@1952: /* Acquire a RESERVED lock ds@1952: */ ds@1952: if( locktype==RESERVED_LOCK && res ){ ds@1952: assert( pFile->locktype==SHARED_LOCK ); ds@1952: res = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0); ds@1952: if( res ){ ds@1952: newLocktype = RESERVED_LOCK; ds@1952: } ds@1952: } ds@1952: ds@1952: /* Acquire a PENDING lock ds@1952: */ ds@1952: if( locktype==EXCLUSIVE_LOCK && res ){ ds@1952: newLocktype = PENDING_LOCK; ds@1952: gotPendingLock = 0; ds@1952: } ds@1952: ds@1952: /* Acquire an EXCLUSIVE lock ds@1952: */ ds@1952: if( locktype==EXCLUSIVE_LOCK && res ){ ds@1952: assert( pFile->locktype>=SHARED_LOCK ); ds@1952: res = unlockReadLock(pFile); ds@1952: OSTRACE2("unreadlock = %d\n", res); ds@1952: res = LockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0); ds@1952: if( res ){ ds@1952: newLocktype = EXCLUSIVE_LOCK; ds@1952: }else{ ds@1952: OSTRACE2("error-code = %d\n", GetLastError()); ds@1952: getReadLock(pFile); ds@1952: } ds@1952: } ds@1952: ds@1952: /* If we are holding a PENDING lock that ought to be released, then ds@1952: ** release it now. ds@1952: */ ds@1952: if( gotPendingLock && locktype==SHARED_LOCK ){ ds@1952: UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0); ds@1952: } ds@1952: ds@1952: /* Update the state of the lock has held in the file descriptor then ds@1952: ** return the appropriate result code. ds@1952: */ ds@1952: if( res ){ ds@1952: rc = SQLITE_OK; ds@1952: }else{ ds@1952: OSTRACE4("LOCK FAILED %d trying for %d but got %d\n", pFile->h, ds@1952: locktype, newLocktype); ds@1952: rc = SQLITE_BUSY; ds@1952: } ds@1952: pFile->locktype = newLocktype; ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** This routine checks if there is a RESERVED lock held on the specified ds@1952: ** file by this or any other process. If such a lock is held, return ds@1952: ** non-zero, otherwise zero. ds@1952: */ ds@1952: static int winCheckReservedLock(sqlite3_file *id){ ds@1952: int rc; ds@1952: winFile *pFile = (winFile*)id; ds@1952: assert( pFile!=0 ); ds@1952: if( pFile->locktype>=RESERVED_LOCK ){ ds@1952: rc = 1; ds@1952: OSTRACE3("TEST WR-LOCK %d %d (local)\n", pFile->h, rc); ds@1952: }else{ ds@1952: rc = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0); ds@1952: if( rc ){ ds@1952: UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0); ds@1952: } ds@1952: rc = !rc; ds@1952: OSTRACE3("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc); ds@1952: } ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Lower the locking level on file descriptor id to locktype. locktype ds@1952: ** must be either NO_LOCK or SHARED_LOCK. ds@1952: ** ds@1952: ** If the locking level of the file descriptor is already at or below ds@1952: ** the requested locking level, this routine is a no-op. ds@1952: ** ds@1952: ** It is not possible for this routine to fail if the second argument ds@1952: ** is NO_LOCK. If the second argument is SHARED_LOCK then this routine ds@1952: ** might return SQLITE_IOERR; ds@1952: */ ds@1952: static int winUnlock(sqlite3_file *id, int locktype){ ds@1952: int type; ds@1952: winFile *pFile = (winFile*)id; ds@1952: int rc = SQLITE_OK; ds@1952: assert( pFile!=0 ); ds@1952: assert( locktype<=SHARED_LOCK ); ds@1952: OSTRACE5("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype, ds@1952: pFile->locktype, pFile->sharedLockByte); ds@1952: type = pFile->locktype; ds@1952: if( type>=EXCLUSIVE_LOCK ){ ds@1952: UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0); ds@1952: if( locktype==SHARED_LOCK && !getReadLock(pFile) ){ ds@1952: /* This should never happen. We should always be able to ds@1952: ** reacquire the read lock */ ds@1952: rc = SQLITE_IOERR_UNLOCK; ds@1952: } ds@1952: } ds@1952: if( type>=RESERVED_LOCK ){ ds@1952: UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0); ds@1952: } ds@1952: if( locktype==NO_LOCK && type>=SHARED_LOCK ){ ds@1952: unlockReadLock(pFile); ds@1952: } ds@1952: if( type>=PENDING_LOCK ){ ds@1952: UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0); ds@1952: } ds@1952: pFile->locktype = locktype; ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Control and query of the open file handle. ds@1952: */ ds@1952: static int winFileControl(sqlite3_file *id, int op, void *pArg){ ds@1952: switch( op ){ ds@1952: case SQLITE_FCNTL_LOCKSTATE: { ds@1952: *(int*)pArg = ((winFile*)id)->locktype; ds@1952: return SQLITE_OK; ds@1952: } ds@1952: } ds@1952: return SQLITE_ERROR; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return the sector size in bytes of the underlying block device for ds@1952: ** the specified file. This is almost always 512 bytes, but may be ds@1952: ** larger for some devices. ds@1952: ** ds@1952: ** SQLite code assumes this function cannot fail. It also assumes that ds@1952: ** if two files are created in the same file-system directory (i.e. ds@1952: ** a database and it's journal file) that the sector size will be the ds@1952: ** same for both. ds@1952: */ ds@1952: static int winSectorSize(sqlite3_file *id){ ds@1952: return SQLITE_DEFAULT_SECTOR_SIZE; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return a vector of device characteristics. ds@1952: */ ds@1952: static int winDeviceCharacteristics(sqlite3_file *id){ ds@1952: return 0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** This vector defines all the methods that can operate on an ds@1952: ** sqlite3_file for win32. ds@1952: */ ds@1952: static const sqlite3_io_methods winIoMethod = { ds@1952: 1, /* iVersion */ ds@1952: winClose, ds@1952: winRead, ds@1952: winWrite, ds@1952: winTruncate, ds@1952: winSync, ds@1952: winFileSize, ds@1952: winLock, ds@1952: winUnlock, ds@1952: winCheckReservedLock, ds@1952: winFileControl, ds@1952: winSectorSize, ds@1952: winDeviceCharacteristics ds@1952: }; ds@1952: ds@1952: /*************************************************************************** ds@1952: ** Here ends the I/O methods that form the sqlite3_io_methods object. ds@1952: ** ds@1952: ** The next block of code implements the VFS methods. ds@1952: ****************************************************************************/ ds@1952: ds@1952: /* ds@1952: ** Convert a UTF-8 filename into whatever form the underlying ds@1952: ** operating system wants filenames in. Space to hold the result ds@1952: ** is obtained from malloc and must be freed by the calling ds@1952: ** function. ds@1952: */ ds@1952: static void *convertUtf8Filename(const char *zFilename){ ds@1952: void *zConverted = 0; ds@1952: if( isNT() ){ ds@1952: zConverted = utf8ToUnicode(zFilename); ds@1952: }else{ ds@1952: zConverted = utf8ToMbcs(zFilename); ds@1952: } ds@1952: /* caller will handle out of memory */ ds@1952: return zConverted; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Open a file. ds@1952: */ ds@1952: static int winOpen( ds@1952: sqlite3_vfs *pVfs, /* Not used */ ds@1952: const char *zName, /* Name of the file (UTF-8) */ ds@1952: sqlite3_file *id, /* Write the SQLite file handle here */ ds@1952: int flags, /* Open mode flags */ ds@1952: int *pOutFlags /* Status return flags */ ds@1952: ){ ds@1952: HANDLE h; ds@1952: DWORD dwDesiredAccess; ds@1952: DWORD dwShareMode; ds@1952: DWORD dwCreationDisposition; ds@1952: DWORD dwFlagsAndAttributes = 0; ds@1952: winFile *pFile = (winFile*)id; ds@1952: void *zConverted = convertUtf8Filename(zName); ds@1952: if( zConverted==0 ){ ds@1952: return SQLITE_NOMEM; ds@1952: } ds@1952: ds@1952: if( flags & SQLITE_OPEN_READWRITE ){ ds@1952: dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; ds@1952: }else{ ds@1952: dwDesiredAccess = GENERIC_READ; ds@1952: } ds@1952: if( flags & SQLITE_OPEN_CREATE ){ ds@1952: dwCreationDisposition = OPEN_ALWAYS; ds@1952: }else{ ds@1952: dwCreationDisposition = OPEN_EXISTING; ds@1952: } ds@1952: if( flags & SQLITE_OPEN_MAIN_DB ){ ds@1952: dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; ds@1952: }else{ ds@1952: dwShareMode = 0; ds@1952: } ds@1952: if( flags & (SQLITE_OPEN_TEMP_DB | SQLITE_OPEN_TEMP_JOURNAL ds@1952: | SQLITE_OPEN_SUBJOURNAL) ){ ds@1952: dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY ds@1952: | FILE_ATTRIBUTE_HIDDEN ds@1952: | FILE_FLAG_DELETE_ON_CLOSE; ds@1952: }else{ ds@1952: dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL; ds@1952: } ds@1952: if( flags & (SQLITE_OPEN_MAIN_DB | SQLITE_OPEN_TEMP_DB) ){ ds@1952: dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS; ds@1952: }else{ ds@1952: dwFlagsAndAttributes |= FILE_FLAG_SEQUENTIAL_SCAN; ds@1952: } ds@1952: if( isNT() ){ ds@1952: h = CreateFileW((WCHAR*)zConverted, ds@1952: dwDesiredAccess, ds@1952: dwShareMode, ds@1952: NULL, ds@1952: dwCreationDisposition, ds@1952: dwFlagsAndAttributes, ds@1952: NULL ds@1952: ); ds@1952: }else{ ds@1952: #if OS_WINCE ds@1952: return SQLITE_NOMEM; ds@1952: #else ds@1952: h = CreateFileA((char*)zConverted, ds@1952: dwDesiredAccess, ds@1952: dwShareMode, ds@1952: NULL, ds@1952: dwCreationDisposition, ds@1952: dwFlagsAndAttributes, ds@1952: NULL ds@1952: ); ds@1952: #endif ds@1952: } ds@1952: if( h==INVALID_HANDLE_VALUE ){ ds@1952: free(zConverted); ds@1952: if( flags & SQLITE_OPEN_READWRITE ){ ds@1952: return winOpen(0, zName, id, ds@1952: ((flags|SQLITE_OPEN_READONLY)&~SQLITE_OPEN_READWRITE), pOutFlags); ds@1952: }else{ ds@1952: return SQLITE_CANTOPEN; ds@1952: } ds@1952: } ds@1952: if( pOutFlags ){ ds@1952: if( flags & SQLITE_OPEN_READWRITE ){ ds@1952: *pOutFlags = SQLITE_OPEN_READWRITE; ds@1952: }else{ ds@1952: *pOutFlags = SQLITE_OPEN_READONLY; ds@1952: } ds@1952: } ds@1952: memset(pFile, 0, sizeof(*pFile)); ds@1952: pFile->pMethod = &winIoMethod; ds@1952: pFile->h = h; ds@1952: #if OS_WINCE ds@1952: if( (flags & (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB)) == ds@1952: (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB) ds@1952: && !winceCreateLock(zFilename, pFile) ds@1952: ){ ds@1952: CloseHandle(h); ds@1952: free(zConverted); ds@1952: return SQLITE_CANTOPEN; ds@1952: } ds@1952: if( dwFlagsAndAttributes & FILE_FLAG_DELETE_ON_CLOSE ){ ds@1952: pFile->zDeleteOnClose = zConverted; ds@1952: }else ds@1952: #endif ds@1952: { ds@1952: free(zConverted); ds@1952: } ds@1952: OpenCounter(+1); ds@1952: return SQLITE_OK; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Delete the named file. ds@1952: ** ds@1952: ** Note that windows does not allow a file to be deleted if some other ds@1952: ** process has it open. Sometimes a virus scanner or indexing program ds@1952: ** will open a journal file shortly after it is created in order to do ds@1952: ** whatever it is it does. While this other process is holding the ds@1952: ** file open, we will be unable to delete it. To work around this ds@1952: ** problem, we delay 100 milliseconds and try to delete again. Up ds@1952: ** to MX_DELETION_ATTEMPTs deletion attempts are run before giving ds@1952: ** up and returning an error. ds@1952: */ ds@1952: #define MX_DELETION_ATTEMPTS 3 ds@1952: static int winDelete( ds@1952: sqlite3_vfs *pVfs, /* Not used on win32 */ ds@1952: const char *zFilename, /* Name of file to delete */ ds@1952: int syncDir /* Not used on win32 */ ds@1952: ){ ds@1952: int cnt = 0; ds@1952: int rc; ds@1952: void *zConverted = convertUtf8Filename(zFilename); ds@1952: if( zConverted==0 ){ ds@1952: return SQLITE_NOMEM; ds@1952: } ds@1952: SimulateIOError(return SQLITE_IOERR_DELETE); ds@1952: if( isNT() ){ ds@1952: do{ ds@1952: rc = DeleteFileW(zConverted); ds@1952: }while( rc==0 && GetFileAttributesW(zConverted)!=0xffffffff ds@1952: && cnt++ < MX_DELETION_ATTEMPTS && (Sleep(100), 1) ); ds@1952: }else{ ds@1952: #if OS_WINCE ds@1952: return SQLITE_NOMEM; ds@1952: #else ds@1952: do{ ds@1952: rc = DeleteFileA(zConverted); ds@1952: }while( rc==0 && GetFileAttributesA(zConverted)!=0xffffffff ds@1952: && cnt++ < MX_DELETION_ATTEMPTS && (Sleep(100), 1) ); ds@1952: #endif ds@1952: } ds@1952: free(zConverted); ds@1952: OSTRACE2("DELETE \"%s\"\n", zFilename); ds@1952: return rc!=0 ? SQLITE_OK : SQLITE_IOERR; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Check the existance and status of a file. ds@1952: */ ds@1952: static int winAccess( ds@1952: sqlite3_vfs *pVfs, /* Not used on win32 */ ds@1952: const char *zFilename, /* Name of file to check */ ds@1952: int flags /* Type of test to make on this file */ ds@1952: ){ ds@1952: DWORD attr; ds@1952: int rc; ds@1952: void *zConverted = convertUtf8Filename(zFilename); ds@1952: if( zConverted==0 ){ ds@1952: return SQLITE_NOMEM; ds@1952: } ds@1952: if( isNT() ){ ds@1952: attr = GetFileAttributesW((WCHAR*)zConverted); ds@1952: }else{ ds@1952: #if OS_WINCE ds@1952: return SQLITE_NOMEM; ds@1952: #else ds@1952: attr = GetFileAttributesA((char*)zConverted); ds@1952: #endif ds@1952: } ds@1952: free(zConverted); ds@1952: switch( flags ){ ds@1952: case SQLITE_ACCESS_READ: ds@1952: case SQLITE_ACCESS_EXISTS: ds@1952: rc = attr!=0xffffffff; ds@1952: break; ds@1952: case SQLITE_ACCESS_READWRITE: ds@1952: rc = (attr & FILE_ATTRIBUTE_READONLY)==0; ds@1952: break; ds@1952: default: ds@1952: assert(!"Invalid flags argument"); ds@1952: } ds@1952: return rc; ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** Create a temporary file name in zBuf. zBuf must be big enough to ds@1952: ** hold at pVfs->mxPathname characters. ds@1952: */ ds@1952: static int winGetTempname(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ ds@1952: static char zChars[] = ds@1952: "abcdefghijklmnopqrstuvwxyz" ds@1952: "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ds@1952: "0123456789"; ds@1952: int i, j; ds@1952: char zTempPath[MAX_PATH+1]; ds@1952: if( sqlite3_temp_directory ){ ds@1952: sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", sqlite3_temp_directory); ds@1952: }else if( isNT() ){ ds@1952: char *zMulti; ds@1952: WCHAR zWidePath[MAX_PATH]; ds@1952: GetTempPathW(MAX_PATH-30, zWidePath); ds@1952: zMulti = unicodeToUtf8(zWidePath); ds@1952: if( zMulti ){ ds@1952: sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zMulti); ds@1952: free(zMulti); ds@1952: }else{ ds@1952: return SQLITE_NOMEM; ds@1952: } ds@1952: }else{ ds@1952: char *zUtf8; ds@1952: char zMbcsPath[MAX_PATH]; ds@1952: GetTempPathA(MAX_PATH-30, zMbcsPath); ds@1952: zUtf8 = mbcsToUtf8(zMbcsPath); ds@1952: if( zUtf8 ){ ds@1952: sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8); ds@1952: free(zUtf8); ds@1952: }else{ ds@1952: return SQLITE_NOMEM; ds@1952: } ds@1952: } ds@1952: for(i=strlen(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){} ds@1952: zTempPath[i] = 0; ds@1952: sqlite3_snprintf(pVfs->mxPathname-30, zBuf, ds@1952: "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath); ds@1952: j = strlen(zBuf); ds@1952: sqlite3Randomness(20, &zBuf[j]); ds@1952: for(i=0; i<20; i++, j++){ ds@1952: zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; ds@1952: } ds@1952: zBuf[j] = 0; ds@1952: OSTRACE2("TEMP FILENAME: %s\n", zBuf); ds@1952: return SQLITE_OK; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Turn a relative pathname into a full pathname. Write the full ds@1952: ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname ds@1952: ** bytes in size. ds@1952: */ ds@1952: static int winFullPathname( ds@1952: sqlite3_vfs *pVfs, /* Pointer to vfs object */ ds@1952: const char *zRelative, /* Possibly relative input path */ ds@1952: int nFull, /* Size of output buffer in bytes */ ds@1952: char *zFull /* Output buffer */ ds@1952: ){ ds@1952: ds@1952: #if defined(__CYGWIN__) ds@1952: cygwin_conv_to_full_win32_path(zRelative, zFull); ds@1952: return SQLITE_OK; ds@1952: #endif ds@1952: ds@1952: #if OS_WINCE ds@1952: /* WinCE has no concept of a relative pathname, or so I am told. */ ds@1952: sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zRelative); ds@1952: #endif ds@1952: ds@1952: #if !OS_WINCE && !defined(__CYGWIN__) ds@1952: int nByte; ds@1952: void *zConverted; ds@1952: char *zOut; ds@1952: zConverted = convertUtf8Filename(zRelative); ds@1952: if( isNT() ){ ds@1952: WCHAR *zTemp; ds@1952: nByte = GetFullPathNameW((WCHAR*)zConverted, 0, 0, 0) + 3; ds@1952: zTemp = malloc( nByte*sizeof(zTemp[0]) ); ds@1952: if( zTemp==0 ){ ds@1952: free(zConverted); ds@1952: return SQLITE_NOMEM; ds@1952: } ds@1952: GetFullPathNameW((WCHAR*)zConverted, nByte, zTemp, 0); ds@1952: free(zConverted); ds@1952: zOut = unicodeToUtf8(zTemp); ds@1952: free(zTemp); ds@1952: }else{ ds@1952: char *zTemp; ds@1952: nByte = GetFullPathNameA((char*)zConverted, 0, 0, 0) + 3; ds@1952: zTemp = malloc( nByte*sizeof(zTemp[0]) ); ds@1952: if( zTemp==0 ){ ds@1952: free(zConverted); ds@1952: return SQLITE_NOMEM; ds@1952: } ds@1952: GetFullPathNameA((char*)zConverted, nByte, zTemp, 0); ds@1952: free(zConverted); ds@1952: zOut = mbcsToUtf8(zTemp); ds@1952: free(zTemp); ds@1952: } ds@1952: if( zOut ){ ds@1952: sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zOut); ds@1952: free(zOut); ds@1952: return SQLITE_OK; ds@1952: }else{ ds@1952: return SQLITE_NOMEM; ds@1952: } ds@1952: #endif ds@1952: } ds@1952: ds@1952: #ifndef SQLITE_OMIT_LOAD_EXTENSION ds@1952: /* ds@1952: ** Interfaces for opening a shared library, finding entry points ds@1952: ** within the shared library, and closing the shared library. ds@1952: */ ds@1952: /* ds@1952: ** Interfaces for opening a shared library, finding entry points ds@1952: ** within the shared library, and closing the shared library. ds@1952: */ ds@1952: static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){ ds@1952: HANDLE h; ds@1952: void *zConverted = convertUtf8Filename(zFilename); ds@1952: if( zConverted==0 ){ ds@1952: return 0; ds@1952: } ds@1952: if( isNT() ){ ds@1952: h = LoadLibraryW((WCHAR*)zConverted); ds@1952: }else{ ds@1952: #if OS_WINCE ds@1952: return 0; ds@1952: #else ds@1952: h = LoadLibraryA((char*)zConverted); ds@1952: #endif ds@1952: } ds@1952: free(zConverted); ds@1952: return (void*)h; ds@1952: } ds@1952: static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){ ds@1952: FormatMessage( ds@1952: FORMAT_MESSAGE_FROM_SYSTEM, ds@1952: NULL, ds@1952: GetLastError(), ds@1952: 0, ds@1952: zBufOut, ds@1952: nBuf-1, ds@1952: 0 ds@1952: ); ds@1952: } ds@1952: void *winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){ ds@1952: #if OS_WINCE ds@1952: /* The GetProcAddressA() routine is only available on wince. */ ds@1952: return GetProcAddressA((HANDLE)pHandle, zSymbol); ds@1952: #else ds@1952: /* All other windows platforms expect GetProcAddress() to take ds@1952: ** an Ansi string regardless of the _UNICODE setting */ ds@1952: return GetProcAddress((HANDLE)pHandle, zSymbol); ds@1952: #endif ds@1952: } ds@1952: void winDlClose(sqlite3_vfs *pVfs, void *pHandle){ ds@1952: FreeLibrary((HANDLE)pHandle); ds@1952: } ds@1952: #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ ds@1952: #define winDlOpen 0 ds@1952: #define winDlError 0 ds@1952: #define winDlSym 0 ds@1952: #define winDlClose 0 ds@1952: #endif ds@1952: ds@1952: ds@1952: /* ds@1952: ** Write up to nBuf bytes of randomness into zBuf. ds@1952: */ ds@1952: static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ ds@1952: int n = 0; ds@1952: if( sizeof(SYSTEMTIME)<=nBuf-n ){ ds@1952: SYSTEMTIME x; ds@1952: GetSystemTime(&x); ds@1952: memcpy(&zBuf[n], &x, sizeof(x)); ds@1952: n += sizeof(x); ds@1952: } ds@1952: if( sizeof(DWORD)<=nBuf-n ){ ds@1952: DWORD pid = GetCurrentProcessId(); ds@1952: memcpy(&zBuf[n], &pid, sizeof(pid)); ds@1952: n += sizeof(pid); ds@1952: } ds@1952: if( sizeof(DWORD)<=nBuf-n ){ ds@1952: DWORD cnt = GetTickCount(); ds@1952: memcpy(&zBuf[n], &cnt, sizeof(cnt)); ds@1952: n += sizeof(cnt); ds@1952: } ds@1952: if( sizeof(LARGE_INTEGER)<=nBuf-n ){ ds@1952: LARGE_INTEGER i; ds@1952: QueryPerformanceCounter(&i); ds@1952: memcpy(&zBuf[n], &i, sizeof(i)); ds@1952: n += sizeof(i); ds@1952: } ds@1952: return n; ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** Sleep for a little while. Return the amount of time slept. ds@1952: */ ds@1952: static int winSleep(sqlite3_vfs *pVfs, int microsec){ ds@1952: Sleep((microsec+999)/1000); ds@1952: return ((microsec+999)/1000)*1000; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** The following variable, if set to a non-zero value, becomes the result ds@1952: ** returned from sqlite3OsCurrentTime(). This is used for testing. ds@1952: */ ds@1952: #ifdef SQLITE_TEST ds@1952: int sqlite3_current_time = 0; ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** Find the current time (in Universal Coordinated Time). Write the ds@1952: ** current time and date as a Julian Day number into *prNow and ds@1952: ** return 0. Return 1 if the time and date cannot be found. ds@1952: */ ds@1952: int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){ ds@1952: FILETIME ft; ds@1952: /* FILETIME structure is a 64-bit value representing the number of ds@1952: 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5). ds@1952: */ ds@1952: double now; ds@1952: #if OS_WINCE ds@1952: SYSTEMTIME time; ds@1952: GetSystemTime(&time); ds@1952: SystemTimeToFileTime(&time,&ft); ds@1952: #else ds@1952: GetSystemTimeAsFileTime( &ft ); ds@1952: #endif ds@1952: now = ((double)ft.dwHighDateTime) * 4294967296.0; ds@1952: *prNow = (now + ft.dwLowDateTime)/864000000000.0 + 2305813.5; ds@1952: #ifdef SQLITE_TEST ds@1952: if( sqlite3_current_time ){ ds@1952: *prNow = sqlite3_current_time/86400.0 + 2440587.5; ds@1952: } ds@1952: #endif ds@1952: return 0; ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** Return a pointer to the sqlite3DefaultVfs structure. We use ds@1952: ** a function rather than give the structure global scope because ds@1952: ** some compilers (MSVC) do not allow forward declarations of ds@1952: ** initialized structures. ds@1952: */ ds@1952: sqlite3_vfs *sqlite3OsDefaultVfs(void){ ds@1952: static sqlite3_vfs winVfs = { ds@1952: 1, /* iVersion */ ds@1952: sizeof(winFile), /* szOsFile */ ds@1952: MAX_PATH, /* mxPathname */ ds@1952: 0, /* pNext */ ds@1952: "win32", /* zName */ ds@1952: 0, /* pAppData */ ds@1952: ds@1952: winOpen, /* xOpen */ ds@1952: winDelete, /* xDelete */ ds@1952: winAccess, /* xAccess */ ds@1952: winGetTempname, /* xGetTempName */ ds@1952: winFullPathname, /* xFullPathname */ ds@1952: winDlOpen, /* xDlOpen */ ds@1952: winDlError, /* xDlError */ ds@1952: winDlSym, /* xDlSym */ ds@1952: winDlClose, /* xDlClose */ ds@1952: winRandomness, /* xRandomness */ ds@1952: winSleep, /* xSleep */ ds@1952: winCurrentTime /* xCurrentTime */ ds@1952: }; ds@1952: ds@1952: return &winVfs; ds@1952: } ds@1952: ds@1952: #endif /* OS_WIN */