1 /* 2 ** 2004 May 22 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ****************************************************************************** 12 ** 13 ** This file contains code that is specific to windows. 14 */ 15 #include "sqliteInt.h" 16 #if OS_WIN /* This file is used for windows only */ 17 18 19 /* 20 ** A Note About Memory Allocation: 21 ** 22 ** This driver uses malloc()/free() directly rather than going through 23 ** the SQLite-wrappers sqlite3_malloc()/sqlite3_free(). Those wrappers 24 ** are designed for use on embedded systems where memory is scarce and 25 ** malloc failures happen frequently. Win32 does not typically run on 26 ** embedded systems, and when it does the developers normally have bigger 27 ** problems to worry about than running out of memory. So there is not 28 ** a compelling need to use the wrappers. 29 ** 30 ** But there is a good reason to not use the wrappers. If we use the 31 ** wrappers then we will get simulated malloc() failures within this 32 ** driver. And that causes all kinds of problems for our tests. We 33 ** could enhance SQLite to deal with simulated malloc failures within 34 ** the OS driver, but the code to deal with those failure would not 35 ** be exercised on Linux (which does not need to malloc() in the driver) 36 ** and so we would have difficulty writing coverage tests for that 37 ** code. Better to leave the code out, we think. 38 ** 39 ** The point of this discussion is as follows: When creating a new 40 ** OS layer for an embedded system, if you use this file as an example, 41 ** avoid the use of malloc()/free(). Those routines work ok on windows 42 ** desktops but not so well in embedded systems. 43 */ 44 45 #include <winbase.h> 46 47 #ifdef __CYGWIN__ 48 # include <sys/cygwin.h> 49 #endif 50 51 /* 52 ** Macros used to determine whether or not to use threads. 53 */ 54 #if defined(THREADSAFE) && THREADSAFE 55 # define SQLITE_W32_THREADS 1 56 #endif 57 58 /* 59 ** Include code that is common to all os_*.c files 60 */ 61 #include "os_common.h" 62 63 /* 64 ** Determine if we are dealing with WindowsCE - which has a much 65 ** reduced API. 66 */ 67 #if defined(_WIN32_WCE) 68 # define OS_WINCE 1 69 # define AreFileApisANSI() 1 70 #else 71 # define OS_WINCE 0 72 #endif 73 74 /* 75 ** WinCE lacks native support for file locking so we have to fake it 76 ** with some code of our own. 77 */ 78 #if OS_WINCE 79 typedef struct winceLock { 80 int nReaders; /* Number of reader locks obtained */ 81 BOOL bPending; /* Indicates a pending lock has been obtained */ 82 BOOL bReserved; /* Indicates a reserved lock has been obtained */ 83 BOOL bExclusive; /* Indicates an exclusive lock has been obtained */ 84 } winceLock; 85 #endif 86 87 /* 88 ** The winFile structure is a subclass of sqlite3_file* specific to the win32 89 ** portability layer. 90 */ 91 typedef struct winFile winFile; 92 struct winFile { 93 const sqlite3_io_methods *pMethod;/* Must be first */ 94 HANDLE h; /* Handle for accessing the file */ 95 unsigned char locktype; /* Type of lock currently held on this file */ 96 short sharedLockByte; /* Randomly chosen byte used as a shared lock */ 97 #if OS_WINCE 98 WCHAR *zDeleteOnClose; /* Name of file to delete when closing */ 99 HANDLE hMutex; /* Mutex used to control access to shared lock */ 100 HANDLE hShared; /* Shared memory segment used for locking */ 101 winceLock local; /* Locks obtained by this instance of winFile */ 102 winceLock *shared; /* Global shared lock memory for the file */ 103 #endif 104 }; 105 106 107 /* 108 ** The following variable is (normally) set once and never changes 109 ** thereafter. It records whether the operating system is Win95 110 ** or WinNT. 111 ** 112 ** 0: Operating system unknown. 113 ** 1: Operating system is Win95. 114 ** 2: Operating system is WinNT. 115 ** 116 ** In order to facilitate testing on a WinNT system, the test fixture 117 ** can manually set this value to 1 to emulate Win98 behavior. 118 */ 119 #ifdef SQLITE_TEST 120 int sqlite3_os_type = 0; 121 #else 122 static int sqlite3_os_type = 0; 123 #endif 124 125 /* 126 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP, 127 ** or WinCE. Return false (zero) for Win95, Win98, or WinME. 128 ** 129 ** Here is an interesting observation: Win95, Win98, and WinME lack 130 ** the LockFileEx() API. But we can still statically link against that 131 ** API as long as we don't call it win running Win95/98/ME. A call to 132 ** this routine is used to determine if the host is Win95/98/ME or 133 ** WinNT/2K/XP so that we will know whether or not we can safely call 134 ** the LockFileEx() API. 135 */ 136 #if OS_WINCE 137 # define isNT() (1) 138 #else 139 static int isNT(void){ 140 if( sqlite3_os_type==0 ){ 141 OSVERSIONINFO sInfo; 142 sInfo.dwOSVersionInfoSize = sizeof(sInfo); 143 GetVersionEx(&sInfo); 144 sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1; 145 } 146 return sqlite3_os_type==2; 147 } 148 #endif /* OS_WINCE */ 149 150 /* 151 ** Convert a UTF-8 string to microsoft unicode (UTF-16?). 152 ** 153 ** Space to hold the returned string is obtained from malloc. 154 */ 155 static WCHAR *utf8ToUnicode(const char *zFilename){ 156 int nChar; 157 WCHAR *zWideFilename; 158 159 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0); 160 zWideFilename = malloc( nChar*sizeof(zWideFilename[0]) ); 161 if( zWideFilename==0 ){ 162 return 0; 163 } 164 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, nChar); 165 if( nChar==0 ){ 166 free(zWideFilename); 167 zWideFilename = 0; 168 } 169 return zWideFilename; 170 } 171 172 /* 173 ** Convert microsoft unicode to UTF-8. Space to hold the returned string is 174 ** obtained from malloc(). 175 */ 176 static char *unicodeToUtf8(const WCHAR *zWideFilename){ 177 int nByte; 178 char *zFilename; 179 180 nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0); 181 zFilename = malloc( nByte ); 182 if( zFilename==0 ){ 183 return 0; 184 } 185 nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte, 186 0, 0); 187 if( nByte == 0 ){ 188 free(zFilename); 189 zFilename = 0; 190 } 191 return zFilename; 192 } 193 194 /* 195 ** Convert an ansi string to microsoft unicode, based on the 196 ** current codepage settings for file apis. 197 ** 198 ** Space to hold the returned string is obtained 199 ** from malloc. 200 */ 201 static WCHAR *mbcsToUnicode(const char *zFilename){ 202 int nByte; 203 WCHAR *zMbcsFilename; 204 int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP; 205 206 nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, NULL,0)*sizeof(WCHAR); 207 zMbcsFilename = malloc( nByte*sizeof(zMbcsFilename[0]) ); 208 if( zMbcsFilename==0 ){ 209 return 0; 210 } 211 nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename, nByte); 212 if( nByte==0 ){ 213 free(zMbcsFilename); 214 zMbcsFilename = 0; 215 } 216 return zMbcsFilename; 217 } 218 219 /* 220 ** Convert microsoft unicode to multibyte character string, based on the 221 ** user's Ansi codepage. 222 ** 223 ** Space to hold the returned string is obtained from 224 ** malloc(). 225 */ 226 static char *unicodeToMbcs(const WCHAR *zWideFilename){ 227 int nByte; 228 char *zFilename; 229 int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP; 230 231 nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0); 232 zFilename = malloc( nByte ); 233 if( zFilename==0 ){ 234 return 0; 235 } 236 nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename, nByte, 237 0, 0); 238 if( nByte == 0 ){ 239 free(zFilename); 240 zFilename = 0; 241 } 242 return zFilename; 243 } 244 245 /* 246 ** Convert multibyte character string to UTF-8. Space to hold the 247 ** returned string is obtained from malloc(). 248 */ 249 static char *mbcsToUtf8(const char *zFilename){ 250 char *zFilenameUtf8; 251 WCHAR *zTmpWide; 252 253 zTmpWide = mbcsToUnicode(zFilename); 254 if( zTmpWide==0 ){ 255 return 0; 256 } 257 zFilenameUtf8 = unicodeToUtf8(zTmpWide); 258 free(zTmpWide); 259 return zFilenameUtf8; 260 } 261 262 /* 263 ** Convert UTF-8 to multibyte character string. Space to hold the 264 ** returned string is obtained from malloc(). 265 */ 266 static char *utf8ToMbcs(const char *zFilename){ 267 char *zFilenameMbcs; 268 WCHAR *zTmpWide; 269 270 zTmpWide = utf8ToUnicode(zFilename); 271 if( zTmpWide==0 ){ 272 return 0; 273 } 274 zFilenameMbcs = unicodeToMbcs(zTmpWide); 275 free(zTmpWide); 276 return zFilenameMbcs; 277 } 278 279 #if OS_WINCE 280 /************************************************************************* 281 ** This section contains code for WinCE only. 282 */ 283 /* 284 ** WindowsCE does not have a localtime() function. So create a 285 ** substitute. 286 */ 287 #include <time.h> 288 struct tm *__cdecl localtime(const time_t *t) 289 { 290 static struct tm y; 291 FILETIME uTm, lTm; 292 SYSTEMTIME pTm; 293 sqlite3_int64 t64; 294 t64 = *t; 295 t64 = (t64 + 11644473600)*10000000; 296 uTm.dwLowDateTime = t64 & 0xFFFFFFFF; 297 uTm.dwHighDateTime= t64 >> 32; 298 FileTimeToLocalFileTime(&uTm,&lTm); 299 FileTimeToSystemTime(&lTm,&pTm); 300 y.tm_year = pTm.wYear - 1900; 301 y.tm_mon = pTm.wMonth - 1; 302 y.tm_wday = pTm.wDayOfWeek; 303 y.tm_mday = pTm.wDay; 304 y.tm_hour = pTm.wHour; 305 y.tm_min = pTm.wMinute; 306 y.tm_sec = pTm.wSecond; 307 return &y; 308 } 309 310 /* This will never be called, but defined to make the code compile */ 311 #define GetTempPathA(a,b) 312 313 #define LockFile(a,b,c,d,e) winceLockFile(&a, b, c, d, e) 314 #define UnlockFile(a,b,c,d,e) winceUnlockFile(&a, b, c, d, e) 315 #define LockFileEx(a,b,c,d,e,f) winceLockFileEx(&a, b, c, d, e, f) 316 317 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-offsetof(winFile,h)] 318 319 /* 320 ** Acquire a lock on the handle h 321 */ 322 static void winceMutexAcquire(HANDLE h){ 323 DWORD dwErr; 324 do { 325 dwErr = WaitForSingleObject(h, INFINITE); 326 } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED); 327 } 328 /* 329 ** Release a lock acquired by winceMutexAcquire() 330 */ 331 #define winceMutexRelease(h) ReleaseMutex(h) 332 333 /* 334 ** Create the mutex and shared memory used for locking in the file 335 ** descriptor pFile 336 */ 337 static BOOL winceCreateLock(const char *zFilename, winFile *pFile){ 338 WCHAR *zTok; 339 WCHAR *zName = utf8ToUnicode(zFilename); 340 BOOL bInit = TRUE; 341 342 /* Initialize the local lockdata */ 343 ZeroMemory(&pFile->local, sizeof(pFile->local)); 344 345 /* Replace the backslashes from the filename and lowercase it 346 ** to derive a mutex name. */ 347 zTok = CharLowerW(zName); 348 for (;*zTok;zTok++){ 349 if (*zTok == '\\') *zTok = '_'; 350 } 351 352 /* Create/open the named mutex */ 353 pFile->hMutex = CreateMutexW(NULL, FALSE, zName); 354 if (!pFile->hMutex){ 355 free(zName); 356 return FALSE; 357 } 358 359 /* Acquire the mutex before continuing */ 360 winceMutexAcquire(pFile->hMutex); 361 362 /* Since the names of named mutexes, semaphores, file mappings etc are 363 ** case-sensitive, take advantage of that by uppercasing the mutex name 364 ** and using that as the shared filemapping name. 365 */ 366 CharUpperW(zName); 367 pFile->hShared = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, 368 PAGE_READWRITE, 0, sizeof(winceLock), 369 zName); 370 371 /* Set a flag that indicates we're the first to create the memory so it 372 ** must be zero-initialized */ 373 if (GetLastError() == ERROR_ALREADY_EXISTS){ 374 bInit = FALSE; 375 } 376 377 free(zName); 378 379 /* If we succeeded in making the shared memory handle, map it. */ 380 if (pFile->hShared){ 381 pFile->shared = (winceLock*)MapViewOfFile(pFile->hShared, 382 FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock)); 383 /* If mapping failed, close the shared memory handle and erase it */ 384 if (!pFile->shared){ 385 CloseHandle(pFile->hShared); 386 pFile->hShared = NULL; 387 } 388 } 389 390 /* If shared memory could not be created, then close the mutex and fail */ 391 if (pFile->hShared == NULL){ 392 winceMutexRelease(pFile->hMutex); 393 CloseHandle(pFile->hMutex); 394 pFile->hMutex = NULL; 395 return FALSE; 396 } 397 398 /* Initialize the shared memory if we're supposed to */ 399 if (bInit) { 400 ZeroMemory(pFile->shared, sizeof(winceLock)); 401 } 402 403 winceMutexRelease(pFile->hMutex); 404 return TRUE; 405 } 406 407 /* 408 ** Destroy the part of winFile that deals with wince locks 409 */ 410 static void winceDestroyLock(winFile *pFile){ 411 if (pFile->hMutex){ 412 /* Acquire the mutex */ 413 winceMutexAcquire(pFile->hMutex); 414 415 /* The following blocks should probably assert in debug mode, but they 416 are to cleanup in case any locks remained open */ 417 if (pFile->local.nReaders){ 418 pFile->shared->nReaders --; 419 } 420 if (pFile->local.bReserved){ 421 pFile->shared->bReserved = FALSE; 422 } 423 if (pFile->local.bPending){ 424 pFile->shared->bPending = FALSE; 425 } 426 if (pFile->local.bExclusive){ 427 pFile->shared->bExclusive = FALSE; 428 } 429 430 /* De-reference and close our copy of the shared memory handle */ 431 UnmapViewOfFile(pFile->shared); 432 CloseHandle(pFile->hShared); 433 434 /* Done with the mutex */ 435 winceMutexRelease(pFile->hMutex); 436 CloseHandle(pFile->hMutex); 437 pFile->hMutex = NULL; 438 } 439 } 440 441 /* 442 ** An implementation of the LockFile() API of windows for wince 443 */ 444 static BOOL winceLockFile( 445 HANDLE *phFile, 446 DWORD dwFileOffsetLow, 447 DWORD dwFileOffsetHigh, 448 DWORD nNumberOfBytesToLockLow, 449 DWORD nNumberOfBytesToLockHigh 450 ){ 451 winFile *pFile = HANDLE_TO_WINFILE(phFile); 452 BOOL bReturn = FALSE; 453 454 if (!pFile->hMutex) return TRUE; 455 winceMutexAcquire(pFile->hMutex); 456 457 /* Wanting an exclusive lock? */ 458 if (dwFileOffsetLow == SHARED_FIRST 459 && nNumberOfBytesToLockLow == SHARED_SIZE){ 460 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){ 461 pFile->shared->bExclusive = TRUE; 462 pFile->local.bExclusive = TRUE; 463 bReturn = TRUE; 464 } 465 } 466 467 /* Want a read-only lock? */ 468 else if ((dwFileOffsetLow >= SHARED_FIRST && 469 dwFileOffsetLow < SHARED_FIRST + SHARED_SIZE) && 470 nNumberOfBytesToLockLow == 1){ 471 if (pFile->shared->bExclusive == 0){ 472 pFile->local.nReaders ++; 473 if (pFile->local.nReaders == 1){ 474 pFile->shared->nReaders ++; 475 } 476 bReturn = TRUE; 477 } 478 } 479 480 /* Want a pending lock? */ 481 else if (dwFileOffsetLow == PENDING_BYTE && nNumberOfBytesToLockLow == 1){ 482 /* If no pending lock has been acquired, then acquire it */ 483 if (pFile->shared->bPending == 0) { 484 pFile->shared->bPending = TRUE; 485 pFile->local.bPending = TRUE; 486 bReturn = TRUE; 487 } 488 } 489 /* Want a reserved lock? */ 490 else if (dwFileOffsetLow == RESERVED_BYTE && nNumberOfBytesToLockLow == 1){ 491 if (pFile->shared->bReserved == 0) { 492 pFile->shared->bReserved = TRUE; 493 pFile->local.bReserved = TRUE; 494 bReturn = TRUE; 495 } 496 } 497 498 winceMutexRelease(pFile->hMutex); 499 return bReturn; 500 } 501 502 /* 503 ** An implementation of the UnlockFile API of windows for wince 504 */ 505 static BOOL winceUnlockFile( 506 HANDLE *phFile, 507 DWORD dwFileOffsetLow, 508 DWORD dwFileOffsetHigh, 509 DWORD nNumberOfBytesToUnlockLow, 510 DWORD nNumberOfBytesToUnlockHigh 511 ){ 512 winFile *pFile = HANDLE_TO_WINFILE(phFile); 513 BOOL bReturn = FALSE; 514 515 if (!pFile->hMutex) return TRUE; 516 winceMutexAcquire(pFile->hMutex); 517 518 /* Releasing a reader lock or an exclusive lock */ 519 if (dwFileOffsetLow >= SHARED_FIRST && 520 dwFileOffsetLow < SHARED_FIRST + SHARED_SIZE){ 521 /* Did we have an exclusive lock? */ 522 if (pFile->local.bExclusive){ 523 pFile->local.bExclusive = FALSE; 524 pFile->shared->bExclusive = FALSE; 525 bReturn = TRUE; 526 } 527 528 /* Did we just have a reader lock? */ 529 else if (pFile->local.nReaders){ 530 pFile->local.nReaders --; 531 if (pFile->local.nReaders == 0) 532 { 533 pFile->shared->nReaders --; 534 } 535 bReturn = TRUE; 536 } 537 } 538 539 /* Releasing a pending lock */ 540 else if (dwFileOffsetLow == PENDING_BYTE && nNumberOfBytesToUnlockLow == 1){ 541 if (pFile->local.bPending){ 542 pFile->local.bPending = FALSE; 543 pFile->shared->bPending = FALSE; 544 bReturn = TRUE; 545 } 546 } 547 /* Releasing a reserved lock */ 548 else if (dwFileOffsetLow == RESERVED_BYTE && nNumberOfBytesToUnlockLow == 1){ 549 if (pFile->local.bReserved) { 550 pFile->local.bReserved = FALSE; 551 pFile->shared->bReserved = FALSE; 552 bReturn = TRUE; 553 } 554 } 555 556 winceMutexRelease(pFile->hMutex); 557 return bReturn; 558 } 559 560 /* 561 ** An implementation of the LockFileEx() API of windows for wince 562 */ 563 static BOOL winceLockFileEx( 564 HANDLE *phFile, 565 DWORD dwFlags, 566 DWORD dwReserved, 567 DWORD nNumberOfBytesToLockLow, 568 DWORD nNumberOfBytesToLockHigh, 569 LPOVERLAPPED lpOverlapped 570 ){ 571 /* If the caller wants a shared read lock, forward this call 572 ** to winceLockFile */ 573 if (lpOverlapped->Offset == SHARED_FIRST && 574 dwFlags == 1 && 575 nNumberOfBytesToLockLow == SHARED_SIZE){ 576 return winceLockFile(phFile, SHARED_FIRST, 0, 1, 0); 577 } 578 return FALSE; 579 } 580 /* 581 ** End of the special code for wince 582 *****************************************************************************/ 583 #endif /* OS_WINCE */ 584 585 /***************************************************************************** 586 ** The next group of routines implement the I/O methods specified 587 ** by the sqlite3_io_methods object. 588 ******************************************************************************/ 589 590 /* 591 ** Close a file. 592 ** 593 ** It is reported that an attempt to close a handle might sometimes 594 ** fail. This is a very unreasonable result, but windows is notorious 595 ** for being unreasonable so I do not doubt that it might happen. If 596 ** the close fails, we pause for 100 milliseconds and try again. As 597 ** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before 598 ** giving up and returning an error. 599 */ 600 #define MX_CLOSE_ATTEMPT 3 601 static int winClose(sqlite3_file *id){ 602 int rc, cnt = 0; 603 winFile *pFile = (winFile*)id; 604 OSTRACE2("CLOSE %d\n", pFile->h); 605 do{ 606 rc = CloseHandle(pFile->h); 607 }while( rc==0 && cnt++ < MX_CLOSE_ATTEMPT && (Sleep(100), 1) ); 608 #if OS_WINCE 609 winceDestroyLock(pFile); 610 if( pFile->zDeleteOnClose ){ 611 DeleteFileW(pFile->zDeleteOnClose); 612 free(pFile->zDeleteOnClose); 613 } 614 #endif 615 OpenCounter(-1); 616 return rc ? SQLITE_OK : SQLITE_IOERR; 617 } 618 619 /* 620 ** Some microsoft compilers lack this definition. 621 */ 622 #ifndef INVALID_SET_FILE_POINTER 623 # define INVALID_SET_FILE_POINTER ((DWORD)-1) 624 #endif 625 626 /* 627 ** Read data from a file into a buffer. Return SQLITE_OK if all 628 ** bytes were read successfully and SQLITE_IOERR if anything goes 629 ** wrong. 630 */ 631 static int winRead( 632 sqlite3_file *id, /* File to read from */ 633 void *pBuf, /* Write content into this buffer */ 634 int amt, /* Number of bytes to read */ 635 sqlite3_int64 offset /* Begin reading at this offset */ 636 ){ 637 LONG upperBits = (offset>>32) & 0x7fffffff; 638 LONG lowerBits = offset & 0xffffffff; 639 DWORD rc; 640 DWORD got; 641 winFile *pFile = (winFile*)id; 642 assert( id!=0 ); 643 SimulateIOError(return SQLITE_IOERR_READ); 644 OSTRACE3("READ %d lock=%d\n", pFile->h, pFile->locktype); 645 rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN); 646 if( rc==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR ){ 647 return SQLITE_FULL; 648 } 649 if( !ReadFile(pFile->h, pBuf, amt, &got, 0) ){ 650 return SQLITE_IOERR_READ; 651 } 652 if( got==(DWORD)amt ){ 653 return SQLITE_OK; 654 }else{ 655 memset(&((char*)pBuf)[got], 0, amt-got); 656 return SQLITE_IOERR_SHORT_READ; 657 } 658 } 659 660 /* 661 ** Write data from a buffer into a file. Return SQLITE_OK on success 662 ** or some other error code on failure. 663 */ 664 static int winWrite( 665 sqlite3_file *id, /* File to write into */ 666 const void *pBuf, /* The bytes to be written */ 667 int amt, /* Number of bytes to write */ 668 sqlite3_int64 offset /* Offset into the file to begin writing at */ 669 ){ 670 LONG upperBits = (offset>>32) & 0x7fffffff; 671 LONG lowerBits = offset & 0xffffffff; 672 DWORD rc; 673 DWORD wrote; 674 winFile *pFile = (winFile*)id; 675 assert( id!=0 ); 676 SimulateIOError(return SQLITE_IOERR_WRITE); 677 SimulateDiskfullError(return SQLITE_FULL); 678 OSTRACE3("WRITE %d lock=%d\n", pFile->h, pFile->locktype); 679 rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN); 680 if( rc==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR ){ 681 return SQLITE_FULL; 682 } 683 assert( amt>0 ); 684 while( 685 amt>0 686 && (rc = WriteFile(pFile->h, pBuf, amt, &wrote, 0))!=0 687 && wrote>0 688 ){ 689 amt -= wrote; 690 pBuf = &((char*)pBuf)[wrote]; 691 } 692 if( !rc || amt>(int)wrote ){ 693 return SQLITE_FULL; 694 } 695 return SQLITE_OK; 696 } 697 698 /* 699 ** Truncate an open file to a specified size 700 */ 701 static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){ 702 LONG upperBits = (nByte>>32) & 0x7fffffff; 703 LONG lowerBits = nByte & 0xffffffff; 704 winFile *pFile = (winFile*)id; 705 OSTRACE3("TRUNCATE %d %lld\n", pFile->h, nByte); 706 SimulateIOError(return SQLITE_IOERR_TRUNCATE); 707 SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN); 708 SetEndOfFile(pFile->h); 709 return SQLITE_OK; 710 } 711 712 #ifdef SQLITE_TEST 713 /* 714 ** Count the number of fullsyncs and normal syncs. This is used to test 715 ** that syncs and fullsyncs are occuring at the right times. 716 */ 717 int sqlite3_sync_count = 0; 718 int sqlite3_fullsync_count = 0; 719 #endif 720 721 /* 722 ** Make sure all writes to a particular file are committed to disk. 723 */ 724 static int winSync(sqlite3_file *id, int flags){ 725 winFile *pFile = (winFile*)id; 726 OSTRACE3("SYNC %d lock=%d\n", pFile->h, pFile->locktype); 727 #ifdef SQLITE_TEST 728 if( flags & SQLITE_SYNC_FULL ){ 729 sqlite3_fullsync_count++; 730 } 731 sqlite3_sync_count++; 732 #endif 733 if( FlushFileBuffers(pFile->h) ){ 734 return SQLITE_OK; 735 }else{ 736 return SQLITE_IOERR; 737 } 738 } 739 740 /* 741 ** Determine the current size of a file in bytes 742 */ 743 static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){ 744 winFile *pFile = (winFile*)id; 745 DWORD upperBits, lowerBits; 746 SimulateIOError(return SQLITE_IOERR_FSTAT); 747 lowerBits = GetFileSize(pFile->h, &upperBits); 748 *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits; 749 return SQLITE_OK; 750 } 751 752 /* 753 ** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems. 754 */ 755 #ifndef LOCKFILE_FAIL_IMMEDIATELY 756 # define LOCKFILE_FAIL_IMMEDIATELY 1 757 #endif 758 759 /* 760 ** Acquire a reader lock. 761 ** Different API routines are called depending on whether or not this 762 ** is Win95 or WinNT. 763 */ 764 static int getReadLock(winFile *pFile){ 765 int res; 766 if( isNT() ){ 767 OVERLAPPED ovlp; 768 ovlp.Offset = SHARED_FIRST; 769 ovlp.OffsetHigh = 0; 770 ovlp.hEvent = 0; 771 res = LockFileEx(pFile->h, LOCKFILE_FAIL_IMMEDIATELY, 772 0, SHARED_SIZE, 0, &ovlp); 773 }else{ 774 int lk; 775 sqlite3Randomness(sizeof(lk), &lk); 776 pFile->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1); 777 res = LockFile(pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0); 778 } 779 return res; 780 } 781 782 /* 783 ** Undo a readlock 784 */ 785 static int unlockReadLock(winFile *pFile){ 786 int res; 787 if( isNT() ){ 788 res = UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0); 789 }else{ 790 res = UnlockFile(pFile->h, SHARED_FIRST + pFile->sharedLockByte, 0, 1, 0); 791 } 792 return res; 793 } 794 795 /* 796 ** Lock the file with the lock specified by parameter locktype - one 797 ** of the following: 798 ** 799 ** (1) SHARED_LOCK 800 ** (2) RESERVED_LOCK 801 ** (3) PENDING_LOCK 802 ** (4) EXCLUSIVE_LOCK 803 ** 804 ** Sometimes when requesting one lock state, additional lock states 805 ** are inserted in between. The locking might fail on one of the later 806 ** transitions leaving the lock state different from what it started but 807 ** still short of its goal. The following chart shows the allowed 808 ** transitions and the inserted intermediate states: 809 ** 810 ** UNLOCKED -> SHARED 811 ** SHARED -> RESERVED 812 ** SHARED -> (PENDING) -> EXCLUSIVE 813 ** RESERVED -> (PENDING) -> EXCLUSIVE 814 ** PENDING -> EXCLUSIVE 815 ** 816 ** This routine will only increase a lock. The winUnlock() routine 817 ** erases all locks at once and returns us immediately to locking level 0. 818 ** It is not possible to lower the locking level one step at a time. You 819 ** must go straight to locking level 0. 820 */ 821 static int winLock(sqlite3_file *id, int locktype){ 822 int rc = SQLITE_OK; /* Return code from subroutines */ 823 int res = 1; /* Result of a windows lock call */ 824 int newLocktype; /* Set pFile->locktype to this value before exiting */ 825 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */ 826 winFile *pFile = (winFile*)id; 827 828 assert( pFile!=0 ); 829 OSTRACE5("LOCK %d %d was %d(%d)\n", 830 pFile->h, locktype, pFile->locktype, pFile->sharedLockByte); 831 832 /* If there is already a lock of this type or more restrictive on the 833 ** OsFile, do nothing. Don't use the end_lock: exit path, as 834 ** sqlite3OsEnterMutex() hasn't been called yet. 835 */ 836 if( pFile->locktype>=locktype ){ 837 return SQLITE_OK; 838 } 839 840 /* Make sure the locking sequence is correct 841 */ 842 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK ); 843 assert( locktype!=PENDING_LOCK ); 844 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK ); 845 846 /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or 847 ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of 848 ** the PENDING_LOCK byte is temporary. 849 */ 850 newLocktype = pFile->locktype; 851 if( pFile->locktype==NO_LOCK 852 || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK) 853 ){ 854 int cnt = 3; 855 while( cnt-->0 && (res = LockFile(pFile->h, PENDING_BYTE, 0, 1, 0))==0 ){ 856 /* Try 3 times to get the pending lock. The pending lock might be 857 ** held by another reader process who will release it momentarily. 858 */ 859 OSTRACE2("could not get a PENDING lock. cnt=%d\n", cnt); 860 Sleep(1); 861 } 862 gotPendingLock = res; 863 } 864 865 /* Acquire a shared lock 866 */ 867 if( locktype==SHARED_LOCK && res ){ 868 assert( pFile->locktype==NO_LOCK ); 869 res = getReadLock(pFile); 870 if( res ){ 871 newLocktype = SHARED_LOCK; 872 } 873 } 874 875 /* Acquire a RESERVED lock 876 */ 877 if( locktype==RESERVED_LOCK && res ){ 878 assert( pFile->locktype==SHARED_LOCK ); 879 res = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0); 880 if( res ){ 881 newLocktype = RESERVED_LOCK; 882 } 883 } 884 885 /* Acquire a PENDING lock 886 */ 887 if( locktype==EXCLUSIVE_LOCK && res ){ 888 newLocktype = PENDING_LOCK; 889 gotPendingLock = 0; 890 } 891 892 /* Acquire an EXCLUSIVE lock 893 */ 894 if( locktype==EXCLUSIVE_LOCK && res ){ 895 assert( pFile->locktype>=SHARED_LOCK ); 896 res = unlockReadLock(pFile); 897 OSTRACE2("unreadlock = %d\n", res); 898 res = LockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0); 899 if( res ){ 900 newLocktype = EXCLUSIVE_LOCK; 901 }else{ 902 OSTRACE2("error-code = %d\n", GetLastError()); 903 getReadLock(pFile); 904 } 905 } 906 907 /* If we are holding a PENDING lock that ought to be released, then 908 ** release it now. 909 */ 910 if( gotPendingLock && locktype==SHARED_LOCK ){ 911 UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0); 912 } 913 914 /* Update the state of the lock has held in the file descriptor then 915 ** return the appropriate result code. 916 */ 917 if( res ){ 918 rc = SQLITE_OK; 919 }else{ 920 OSTRACE4("LOCK FAILED %d trying for %d but got %d\n", pFile->h, 921 locktype, newLocktype); 922 rc = SQLITE_BUSY; 923 } 924 pFile->locktype = newLocktype; 925 return rc; 926 } 927 928 /* 929 ** This routine checks if there is a RESERVED lock held on the specified 930 ** file by this or any other process. If such a lock is held, return 931 ** non-zero, otherwise zero. 932 */ 933 static int winCheckReservedLock(sqlite3_file *id){ 934 int rc; 935 winFile *pFile = (winFile*)id; 936 assert( pFile!=0 ); 937 if( pFile->locktype>=RESERVED_LOCK ){ 938 rc = 1; 939 OSTRACE3("TEST WR-LOCK %d %d (local)\n", pFile->h, rc); 940 }else{ 941 rc = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0); 942 if( rc ){ 943 UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0); 944 } 945 rc = !rc; 946 OSTRACE3("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc); 947 } 948 return rc; 949 } 950 951 /* 952 ** Lower the locking level on file descriptor id to locktype. locktype 953 ** must be either NO_LOCK or SHARED_LOCK. 954 ** 955 ** If the locking level of the file descriptor is already at or below 956 ** the requested locking level, this routine is a no-op. 957 ** 958 ** It is not possible for this routine to fail if the second argument 959 ** is NO_LOCK. If the second argument is SHARED_LOCK then this routine 960 ** might return SQLITE_IOERR; 961 */ 962 static int winUnlock(sqlite3_file *id, int locktype){ 963 int type; 964 winFile *pFile = (winFile*)id; 965 int rc = SQLITE_OK; 966 assert( pFile!=0 ); 967 assert( locktype<=SHARED_LOCK ); 968 OSTRACE5("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype, 969 pFile->locktype, pFile->sharedLockByte); 970 type = pFile->locktype; 971 if( type>=EXCLUSIVE_LOCK ){ 972 UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0); 973 if( locktype==SHARED_LOCK && !getReadLock(pFile) ){ 974 /* This should never happen. We should always be able to 975 ** reacquire the read lock */ 976 rc = SQLITE_IOERR_UNLOCK; 977 } 978 } 979 if( type>=RESERVED_LOCK ){ 980 UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0); 981 } 982 if( locktype==NO_LOCK && type>=SHARED_LOCK ){ 983 unlockReadLock(pFile); 984 } 985 if( type>=PENDING_LOCK ){ 986 UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0); 987 } 988 pFile->locktype = locktype; 989 return rc; 990 } 991 992 /* 993 ** Control and query of the open file handle. 994 */ 995 static int winFileControl(sqlite3_file *id, int op, void *pArg){ 996 switch( op ){ 997 case SQLITE_FCNTL_LOCKSTATE: { 998 *(int*)pArg = ((winFile*)id)->locktype; 999 return SQLITE_OK; 1000 } 1001 } 1002 return SQLITE_ERROR; 1003 } 1004 1005 /* 1006 ** Return the sector size in bytes of the underlying block device for 1007 ** the specified file. This is almost always 512 bytes, but may be 1008 ** larger for some devices. 1009 ** 1010 ** SQLite code assumes this function cannot fail. It also assumes that 1011 ** if two files are created in the same file-system directory (i.e. 1012 ** a database and its journal file) that the sector size will be the 1013 ** same for both. 1014 */ 1015 static int winSectorSize(sqlite3_file *id){ 1016 return SQLITE_DEFAULT_SECTOR_SIZE; 1017 } 1018 1019 /* 1020 ** Return a vector of device characteristics. 1021 */ 1022 static int winDeviceCharacteristics(sqlite3_file *id){ 1023 return 0; 1024 } 1025 1026 /* 1027 ** This vector defines all the methods that can operate on an 1028 ** sqlite3_file for win32. 1029 */ 1030 static const sqlite3_io_methods winIoMethod = { 1031 1, /* iVersion */ 1032 winClose, 1033 winRead, 1034 winWrite, 1035 winTruncate, 1036 winSync, 1037 winFileSize, 1038 winLock, 1039 winUnlock, 1040 winCheckReservedLock, 1041 winFileControl, 1042 winSectorSize, 1043 winDeviceCharacteristics 1044 }; 1045 1046 /*************************************************************************** 1047 ** Here ends the I/O methods that form the sqlite3_io_methods object. 1048 ** 1049 ** The next block of code implements the VFS methods. 1050 ****************************************************************************/ 1051 1052 /* 1053 ** Convert a UTF-8 filename into whatever form the underlying 1054 ** operating system wants filenames in. Space to hold the result 1055 ** is obtained from malloc and must be freed by the calling 1056 ** function. 1057 */ 1058 static void *convertUtf8Filename(const char *zFilename){ 1059 void *zConverted = 0; 1060 if( isNT() ){ 1061 zConverted = utf8ToUnicode(zFilename); 1062 }else{ 1063 zConverted = utf8ToMbcs(zFilename); 1064 } 1065 /* caller will handle out of memory */ 1066 return zConverted; 1067 } 1068 1069 /* 1070 ** Open a file. 1071 */ 1072 static int winOpen( 1073 sqlite3_vfs *pVfs, /* Not used */ 1074 const char *zName, /* Name of the file (UTF-8) */ 1075 sqlite3_file *id, /* Write the SQLite file handle here */ 1076 int flags, /* Open mode flags */ 1077 int *pOutFlags /* Status return flags */ 1078 ){ 1079 HANDLE h; 1080 DWORD dwDesiredAccess; 1081 DWORD dwShareMode; 1082 DWORD dwCreationDisposition; 1083 DWORD dwFlagsAndAttributes = 0; 1084 int isTemp; 1085 winFile *pFile = (winFile*)id; 1086 void *zConverted = convertUtf8Filename(zName); 1087 if( zConverted==0 ){ 1088 return SQLITE_NOMEM; 1089 } 1090 1091 if( flags & SQLITE_OPEN_READWRITE ){ 1092 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; 1093 }else{ 1094 dwDesiredAccess = GENERIC_READ; 1095 } 1096 if( flags & SQLITE_OPEN_CREATE ){ 1097 dwCreationDisposition = OPEN_ALWAYS; 1098 }else{ 1099 dwCreationDisposition = OPEN_EXISTING; 1100 } 1101 if( flags & SQLITE_OPEN_MAIN_DB ){ 1102 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; 1103 }else{ 1104 dwShareMode = 0; 1105 } 1106 if( flags & SQLITE_OPEN_DELETEONCLOSE ){ 1107 #if OS_WINCE 1108 dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN; 1109 #else 1110 dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY 1111 | FILE_ATTRIBUTE_HIDDEN 1112 | FILE_FLAG_DELETE_ON_CLOSE; 1113 #endif 1114 isTemp = 1; 1115 }else{ 1116 dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL; 1117 isTemp = 0; 1118 } 1119 /* Reports from the internet are that performance is always 1120 ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */ 1121 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS; 1122 if( isNT() ){ 1123 h = CreateFileW((WCHAR*)zConverted, 1124 dwDesiredAccess, 1125 dwShareMode, 1126 NULL, 1127 dwCreationDisposition, 1128 dwFlagsAndAttributes, 1129 NULL 1130 ); 1131 }else{ 1132 #if OS_WINCE 1133 return SQLITE_NOMEM; 1134 #else 1135 h = CreateFileA((char*)zConverted, 1136 dwDesiredAccess, 1137 dwShareMode, 1138 NULL, 1139 dwCreationDisposition, 1140 dwFlagsAndAttributes, 1141 NULL 1142 ); 1143 #endif 1144 } 1145 if( h==INVALID_HANDLE_VALUE ){ 1146 free(zConverted); 1147 if( flags & SQLITE_OPEN_READWRITE ){ 1148 return winOpen(0, zName, id, 1149 ((flags|SQLITE_OPEN_READONLY)&~SQLITE_OPEN_READWRITE), pOutFlags); 1150 }else{ 1151 return SQLITE_CANTOPEN; 1152 } 1153 } 1154 if( pOutFlags ){ 1155 if( flags & SQLITE_OPEN_READWRITE ){ 1156 *pOutFlags = SQLITE_OPEN_READWRITE; 1157 }else{ 1158 *pOutFlags = SQLITE_OPEN_READONLY; 1159 } 1160 } 1161 memset(pFile, 0, sizeof(*pFile)); 1162 pFile->pMethod = &winIoMethod; 1163 pFile->h = h; 1164 #if OS_WINCE 1165 if( (flags & (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB)) == 1166 (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB) 1167 && !winceCreateLock(zName, pFile) 1168 ){ 1169 CloseHandle(h); 1170 free(zConverted); 1171 return SQLITE_CANTOPEN; 1172 } 1173 if( isTemp ){ 1174 pFile->zDeleteOnClose = zConverted; 1175 }else 1176 #endif 1177 { 1178 free(zConverted); 1179 } 1180 OpenCounter(+1); 1181 return SQLITE_OK; 1182 } 1183 1184 /* 1185 ** Delete the named file. 1186 ** 1187 ** Note that windows does not allow a file to be deleted if some other 1188 ** process has it open. Sometimes a virus scanner or indexing program 1189 ** will open a journal file shortly after it is created in order to do 1190 ** whatever does. While this other process is holding the 1191 ** file open, we will be unable to delete it. To work around this 1192 ** problem, we delay 100 milliseconds and try to delete again. Up 1193 ** to MX_DELETION_ATTEMPTs deletion attempts are run before giving 1194 ** up and returning an error. 1195 */ 1196 #define MX_DELETION_ATTEMPTS 5 1197 static int winDelete( 1198 sqlite3_vfs *pVfs, /* Not used on win32 */ 1199 const char *zFilename, /* Name of file to delete */ 1200 int syncDir /* Not used on win32 */ 1201 ){ 1202 int cnt = 0; 1203 int rc; 1204 void *zConverted = convertUtf8Filename(zFilename); 1205 if( zConverted==0 ){ 1206 return SQLITE_NOMEM; 1207 } 1208 SimulateIOError(return SQLITE_IOERR_DELETE); 1209 if( isNT() ){ 1210 do{ 1211 DeleteFileW(zConverted); 1212 }while( (rc = GetFileAttributesW(zConverted))!=0xffffffff 1213 && cnt++ < MX_DELETION_ATTEMPTS && (Sleep(100), 1) ); 1214 }else{ 1215 #if OS_WINCE 1216 return SQLITE_NOMEM; 1217 #else 1218 do{ 1219 DeleteFileA(zConverted); 1220 }while( (rc = GetFileAttributesA(zConverted))!=0xffffffff 1221 && cnt++ < MX_DELETION_ATTEMPTS && (Sleep(100), 1) ); 1222 #endif 1223 } 1224 free(zConverted); 1225 OSTRACE2("DELETE \"%s\"\n", zFilename); 1226 return rc==0xffffffff ? SQLITE_OK : SQLITE_IOERR_DELETE; 1227 } 1228 1229 /* 1230 ** Check the existance and status of a file. 1231 */ 1232 static int winAccess( 1233 sqlite3_vfs *pVfs, /* Not used on win32 */ 1234 const char *zFilename, /* Name of file to check */ 1235 int flags /* Type of test to make on this file */ 1236 ){ 1237 DWORD attr; 1238 int rc; 1239 void *zConverted = convertUtf8Filename(zFilename); 1240 if( zConverted==0 ){ 1241 return SQLITE_NOMEM; 1242 } 1243 if( isNT() ){ 1244 attr = GetFileAttributesW((WCHAR*)zConverted); 1245 }else{ 1246 #if OS_WINCE 1247 return SQLITE_NOMEM; 1248 #else 1249 attr = GetFileAttributesA((char*)zConverted); 1250 #endif 1251 } 1252 free(zConverted); 1253 switch( flags ){ 1254 case SQLITE_ACCESS_READ: 1255 case SQLITE_ACCESS_EXISTS: 1256 rc = attr!=0xffffffff; 1257 break; 1258 case SQLITE_ACCESS_READWRITE: 1259 rc = (attr & FILE_ATTRIBUTE_READONLY)==0; 1260 break; 1261 default: 1262 assert(!"Invalid flags argument"); 1263 } 1264 return rc; 1265 } 1266 1267 1268 /* 1269 ** Create a temporary file name in zBuf. zBuf must be big enough to 1270 ** hold at pVfs->mxPathname characters. 1271 */ 1272 static int winGetTempname(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ 1273 static char zChars[] = 1274 "abcdefghijklmnopqrstuvwxyz" 1275 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 1276 "0123456789"; 1277 int i, j; 1278 char zTempPath[MAX_PATH+1]; 1279 if( sqlite3_temp_directory ){ 1280 sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", sqlite3_temp_directory); 1281 }else if( isNT() ){ 1282 char *zMulti; 1283 WCHAR zWidePath[MAX_PATH]; 1284 GetTempPathW(MAX_PATH-30, zWidePath); 1285 zMulti = unicodeToUtf8(zWidePath); 1286 if( zMulti ){ 1287 sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zMulti); 1288 free(zMulti); 1289 }else{ 1290 return SQLITE_NOMEM; 1291 } 1292 }else{ 1293 char *zUtf8; 1294 char zMbcsPath[MAX_PATH]; 1295 GetTempPathA(MAX_PATH-30, zMbcsPath); 1296 zUtf8 = mbcsToUtf8(zMbcsPath); 1297 if( zUtf8 ){ 1298 sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8); 1299 free(zUtf8); 1300 }else{ 1301 return SQLITE_NOMEM; 1302 } 1303 } 1304 for(i=strlen(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){} 1305 zTempPath[i] = 0; 1306 sqlite3_snprintf(nBuf-30, zBuf, 1307 "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath); 1308 j = strlen(zBuf); 1309 sqlite3Randomness(20, &zBuf[j]); 1310 for(i=0; i<20; i++, j++){ 1311 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; 1312 } 1313 zBuf[j] = 0; 1314 OSTRACE2("TEMP FILENAME: %s\n", zBuf); 1315 return SQLITE_OK; 1316 } 1317 1318 /* 1319 ** Turn a relative pathname into a full pathname. Write the full 1320 ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname 1321 ** bytes in size. 1322 */ 1323 static int winFullPathname( 1324 sqlite3_vfs *pVfs, /* Pointer to vfs object */ 1325 const char *zRelative, /* Possibly relative input path */ 1326 int nFull, /* Size of output buffer in bytes */ 1327 char *zFull /* Output buffer */ 1328 ){ 1329 1330 #if defined(__CYGWIN__) 1331 cygwin_conv_to_full_win32_path(zRelative, zFull); 1332 return SQLITE_OK; 1333 #endif 1334 1335 #if OS_WINCE 1336 /* WinCE has no concept of a relative pathname, or so I am told. */ 1337 sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zRelative); 1338 return SQLITE_OK; 1339 #endif 1340 1341 #if !OS_WINCE && !defined(__CYGWIN__) 1342 int nByte; 1343 void *zConverted; 1344 char *zOut; 1345 zConverted = convertUtf8Filename(zRelative); 1346 if( isNT() ){ 1347 WCHAR *zTemp; 1348 nByte = GetFullPathNameW((WCHAR*)zConverted, 0, 0, 0) + 3; 1349 zTemp = malloc( nByte*sizeof(zTemp[0]) ); 1350 if( zTemp==0 ){ 1351 free(zConverted); 1352 return SQLITE_NOMEM; 1353 } 1354 GetFullPathNameW((WCHAR*)zConverted, nByte, zTemp, 0); 1355 free(zConverted); 1356 zOut = unicodeToUtf8(zTemp); 1357 free(zTemp); 1358 }else{ 1359 char *zTemp; 1360 nByte = GetFullPathNameA((char*)zConverted, 0, 0, 0) + 3; 1361 zTemp = malloc( nByte*sizeof(zTemp[0]) ); 1362 if( zTemp==0 ){ 1363 free(zConverted); 1364 return SQLITE_NOMEM; 1365 } 1366 GetFullPathNameA((char*)zConverted, nByte, zTemp, 0); 1367 free(zConverted); 1368 zOut = mbcsToUtf8(zTemp); 1369 free(zTemp); 1370 } 1371 if( zOut ){ 1372 sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zOut); 1373 free(zOut); 1374 return SQLITE_OK; 1375 }else{ 1376 return SQLITE_NOMEM; 1377 } 1378 #endif 1379 } 1380 1381 #ifndef SQLITE_OMIT_LOAD_EXTENSION 1382 /* 1383 ** Interfaces for opening a shared library, finding entry points 1384 ** within the shared library, and closing the shared library. 1385 */ 1386 /* 1387 ** Interfaces for opening a shared library, finding entry points 1388 ** within the shared library, and closing the shared library. 1389 */ 1390 static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){ 1391 HANDLE h; 1392 void *zConverted = convertUtf8Filename(zFilename); 1393 if( zConverted==0 ){ 1394 return 0; 1395 } 1396 if( isNT() ){ 1397 h = LoadLibraryW((WCHAR*)zConverted); 1398 }else{ 1399 #if OS_WINCE 1400 return 0; 1401 #else 1402 h = LoadLibraryA((char*)zConverted); 1403 #endif 1404 } 1405 free(zConverted); 1406 return (void*)h; 1407 } 1408 static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){ 1409 #if OS_WINCE 1410 int error = GetLastError(); 1411 if( error>0x7FFFFFF ){ 1412 sqlite3_snprintf(nBuf, zBufOut, "OsError 0x%x", error); 1413 }else{ 1414 sqlite3_snprintf(nBuf, zBufOut, "OsError %d", error); 1415 } 1416 #else 1417 FormatMessageA( 1418 FORMAT_MESSAGE_FROM_SYSTEM, 1419 NULL, 1420 GetLastError(), 1421 0, 1422 zBufOut, 1423 nBuf-1, 1424 0 1425 ); 1426 #endif 1427 } 1428 void *winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){ 1429 #if OS_WINCE 1430 /* The GetProcAddressA() routine is only available on wince. */ 1431 return GetProcAddressA((HANDLE)pHandle, zSymbol); 1432 #else 1433 /* All other windows platforms expect GetProcAddress() to take 1434 ** an Ansi string regardless of the _UNICODE setting */ 1435 return GetProcAddress((HANDLE)pHandle, zSymbol); 1436 #endif 1437 } 1438 void winDlClose(sqlite3_vfs *pVfs, void *pHandle){ 1439 FreeLibrary((HANDLE)pHandle); 1440 } 1441 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ 1442 #define winDlOpen 0 1443 #define winDlError 0 1444 #define winDlSym 0 1445 #define winDlClose 0 1446 #endif 1447 1448 1449 /* 1450 ** Write up to nBuf bytes of randomness into zBuf. 1451 */ 1452 static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ 1453 int n = 0; 1454 if( sizeof(SYSTEMTIME)<=nBuf-n ){ 1455 SYSTEMTIME x; 1456 GetSystemTime(&x); 1457 memcpy(&zBuf[n], &x, sizeof(x)); 1458 n += sizeof(x); 1459 } 1460 if( sizeof(DWORD)<=nBuf-n ){ 1461 DWORD pid = GetCurrentProcessId(); 1462 memcpy(&zBuf[n], &pid, sizeof(pid)); 1463 n += sizeof(pid); 1464 } 1465 if( sizeof(DWORD)<=nBuf-n ){ 1466 DWORD cnt = GetTickCount(); 1467 memcpy(&zBuf[n], &cnt, sizeof(cnt)); 1468 n += sizeof(cnt); 1469 } 1470 if( sizeof(LARGE_INTEGER)<=nBuf-n ){ 1471 LARGE_INTEGER i; 1472 QueryPerformanceCounter(&i); 1473 memcpy(&zBuf[n], &i, sizeof(i)); 1474 n += sizeof(i); 1475 } 1476 return n; 1477 } 1478 1479 1480 /* 1481 ** Sleep for a little while. Return the amount of time slept. 1482 */ 1483 static int winSleep(sqlite3_vfs *pVfs, int microsec){ 1484 Sleep((microsec+999)/1000); 1485 return ((microsec+999)/1000)*1000; 1486 } 1487 1488 /* 1489 ** The following variable, if set to a non-zero value, becomes the result 1490 ** returned from sqlite3OsCurrentTime(). This is used for testing. 1491 */ 1492 #ifdef SQLITE_TEST 1493 int sqlite3_current_time = 0; 1494 #endif 1495 1496 /* 1497 ** Find the current time (in Universal Coordinated Time). Write the 1498 ** current time and date as a Julian Day number into *prNow and 1499 ** return 0. Return 1 if the time and date cannot be found. 1500 */ 1501 int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){ 1502 FILETIME ft; 1503 /* FILETIME structure is a 64-bit value representing the number of 1504 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5). 1505 */ 1506 double now; 1507 #if OS_WINCE 1508 SYSTEMTIME time; 1509 GetSystemTime(&time); 1510 SystemTimeToFileTime(&time,&ft); 1511 #else 1512 GetSystemTimeAsFileTime( &ft ); 1513 #endif 1514 now = ((double)ft.dwHighDateTime) * 4294967296.0; 1515 *prNow = (now + ft.dwLowDateTime)/864000000000.0 + 2305813.5; 1516 #ifdef SQLITE_TEST 1517 if( sqlite3_current_time ){ 1518 *prNow = sqlite3_current_time/86400.0 + 2440587.5; 1519 } 1520 #endif 1521 return 0; 1522 } 1523 1524 1525 /* 1526 ** Return a pointer to the sqlite3DefaultVfs structure. We use 1527 ** a function rather than give the structure global scope because 1528 ** some compilers (MSVC) do not allow forward declarations of 1529 ** initialized structures. 1530 */ 1531 sqlite3_vfs *sqlite3OsDefaultVfs(void){ 1532 static sqlite3_vfs winVfs = { 1533 1, /* iVersion */ 1534 sizeof(winFile), /* szOsFile */ 1535 MAX_PATH, /* mxPathname */ 1536 0, /* pNext */ 1537 "win32", /* zName */ 1538 0, /* pAppData */ 1539 1540 winOpen, /* xOpen */ 1541 winDelete, /* xDelete */ 1542 winAccess, /* xAccess */ 1543 winGetTempname, /* xGetTempName */ 1544 winFullPathname, /* xFullPathname */ 1545 winDlOpen, /* xDlOpen */ 1546 winDlError, /* xDlError */ 1547 winDlSym, /* xDlSym */ 1548 winDlClose, /* xDlClose */ 1549 winRandomness, /* xRandomness */ 1550 winSleep, /* xSleep */ 1551 winCurrentTime /* xCurrentTime */ 1552 }; 1553 1554 return &winVfs; 1555 } 1556 1557 #endif /* OS_WIN */ 1558