1bbd42a6dSdrh /*
2bbd42a6dSdrh ** 2004 May 22
3bbd42a6dSdrh **
4bbd42a6dSdrh ** The author disclaims copyright to this source code. In place of
5bbd42a6dSdrh ** a legal notice, here is a blessing:
6bbd42a6dSdrh **
7bbd42a6dSdrh ** May you do good and not evil.
8bbd42a6dSdrh ** May you find forgiveness for yourself and forgive others.
9bbd42a6dSdrh ** May you share freely, never taking more than you give.
10bbd42a6dSdrh **
11bbd42a6dSdrh ******************************************************************************
12bbd42a6dSdrh **
13318507b7Smistachkin ** This file contains code that is specific to Windows.
14bbd42a6dSdrh */
15bbd42a6dSdrh #include "sqliteInt.h"
16318507b7Smistachkin #if SQLITE_OS_WIN /* This file is used for Windows only */
17bbd42a6dSdrh
18bbd42a6dSdrh /*
19bbd42a6dSdrh ** Include code that is common to all os_*.c files
20bbd42a6dSdrh */
21bbd42a6dSdrh #include "os_common.h"
22bbd42a6dSdrh
23bbd42a6dSdrh /*
248bc52629Smistachkin ** Include the header file for the Windows VFS.
258bc52629Smistachkin */
268bc52629Smistachkin #include "os_win.h"
278bc52629Smistachkin
288bc52629Smistachkin /*
29c60941f8Smistachkin ** Compiling and using WAL mode requires several APIs that are only
30c60941f8Smistachkin ** available in Windows platforms based on the NT kernel.
31f4f327ceSmistachkin */
32c60941f8Smistachkin #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
33c60941f8Smistachkin # error "WAL mode requires support from the Windows NT kernel, compile\
34c60941f8Smistachkin with SQLITE_OMIT_WAL."
35f4f327ceSmistachkin #endif
36f4f327ceSmistachkin
3773767829Smistachkin #if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE>0
3873767829Smistachkin # error "Memory mapped files require support from the Windows NT kernel,\
3973767829Smistachkin compile with SQLITE_MAX_MMAP_SIZE=0."
4073767829Smistachkin #endif
4173767829Smistachkin
42f4f327ceSmistachkin /*
43f1dacbfcSmistachkin ** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
44f1dacbfcSmistachkin ** based on the sub-platform)?
45f1dacbfcSmistachkin */
4628159a5eSmistachkin #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI)
47f1dacbfcSmistachkin # define SQLITE_WIN32_HAS_ANSI
48f1dacbfcSmistachkin #endif
49f1dacbfcSmistachkin
50f1dacbfcSmistachkin /*
51f1dacbfcSmistachkin ** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
52f1dacbfcSmistachkin ** based on the sub-platform)?
53f1dacbfcSmistachkin */
5428159a5eSmistachkin #if (SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT) && \
5528159a5eSmistachkin !defined(SQLITE_WIN32_NO_WIDE)
56f1dacbfcSmistachkin # define SQLITE_WIN32_HAS_WIDE
57f1dacbfcSmistachkin #endif
58f1dacbfcSmistachkin
59f1dacbfcSmistachkin /*
6028159a5eSmistachkin ** Make sure at least one set of Win32 APIs is available.
6128159a5eSmistachkin */
6228159a5eSmistachkin #if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE)
6328159a5eSmistachkin # error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\
6428159a5eSmistachkin must be defined."
6528159a5eSmistachkin #endif
6628159a5eSmistachkin
6728159a5eSmistachkin /*
680f7e08e0Smistachkin ** Define the required Windows SDK version constants if they are not
690f7e08e0Smistachkin ** already available.
700f7e08e0Smistachkin */
710f7e08e0Smistachkin #ifndef NTDDI_WIN8
720f7e08e0Smistachkin # define NTDDI_WIN8 0x06020000
730f7e08e0Smistachkin #endif
740f7e08e0Smistachkin
750f7e08e0Smistachkin #ifndef NTDDI_WINBLUE
760f7e08e0Smistachkin # define NTDDI_WINBLUE 0x06030000
770f7e08e0Smistachkin #endif
780f7e08e0Smistachkin
79d5be6f01Smistachkin #ifndef NTDDI_WINTHRESHOLD
80d5be6f01Smistachkin # define NTDDI_WINTHRESHOLD 0x06040000
81d5be6f01Smistachkin #endif
82d5be6f01Smistachkin
830f7e08e0Smistachkin /*
84c96c7e3cSdrh ** Check to see if the GetVersionEx[AW] functions are deprecated on the
85e5e20d35Sdrh ** target system. GetVersionEx was first deprecated in Win8.1.
860f7e08e0Smistachkin */
870f7e08e0Smistachkin #ifndef SQLITE_WIN32_GETVERSIONEX
880f7e08e0Smistachkin # if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
89c96c7e3cSdrh # define SQLITE_WIN32_GETVERSIONEX 0 /* GetVersionEx() is deprecated */
900f7e08e0Smistachkin # else
91c96c7e3cSdrh # define SQLITE_WIN32_GETVERSIONEX 1 /* GetVersionEx() is current */
920f7e08e0Smistachkin # endif
930f7e08e0Smistachkin #endif
940f7e08e0Smistachkin
950f7e08e0Smistachkin /*
96d5be6f01Smistachkin ** Check to see if the CreateFileMappingA function is supported on the
97d5be6f01Smistachkin ** target system. It is unavailable when using "mincore.lib" on Win10.
98d5be6f01Smistachkin ** When compiling for Windows 10, always assume "mincore.lib" is in use.
99d5be6f01Smistachkin */
100d5be6f01Smistachkin #ifndef SQLITE_WIN32_CREATEFILEMAPPINGA
101d5be6f01Smistachkin # if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINTHRESHOLD
102d5be6f01Smistachkin # define SQLITE_WIN32_CREATEFILEMAPPINGA 0
103d5be6f01Smistachkin # else
104d5be6f01Smistachkin # define SQLITE_WIN32_CREATEFILEMAPPINGA 1
105d5be6f01Smistachkin # endif
106d5be6f01Smistachkin #endif
107d5be6f01Smistachkin
108d5be6f01Smistachkin /*
1099858bf2fSmistachkin ** This constant should already be defined (in the "WinDef.h" SDK file).
1109858bf2fSmistachkin */
1119858bf2fSmistachkin #ifndef MAX_PATH
1129858bf2fSmistachkin # define MAX_PATH (260)
1139858bf2fSmistachkin #endif
1149858bf2fSmistachkin
1159858bf2fSmistachkin /*
11631706a2dSmistachkin ** Maximum pathname length (in chars) for Win32. This should normally be
11731706a2dSmistachkin ** MAX_PATH.
11831706a2dSmistachkin */
11931706a2dSmistachkin #ifndef SQLITE_WIN32_MAX_PATH_CHARS
12031706a2dSmistachkin # define SQLITE_WIN32_MAX_PATH_CHARS (MAX_PATH)
12131706a2dSmistachkin #endif
12231706a2dSmistachkin
12331706a2dSmistachkin /*
1249858bf2fSmistachkin ** This constant should already be defined (in the "WinNT.h" SDK file).
1259858bf2fSmistachkin */
1269858bf2fSmistachkin #ifndef UNICODE_STRING_MAX_CHARS
1279858bf2fSmistachkin # define UNICODE_STRING_MAX_CHARS (32767)
1289858bf2fSmistachkin #endif
1299858bf2fSmistachkin
1309858bf2fSmistachkin /*
1314496a232Smistachkin ** Maximum pathname length (in chars) for WinNT. This should normally be
132dc7b0555Smistachkin ** UNICODE_STRING_MAX_CHARS.
1334496a232Smistachkin */
1344496a232Smistachkin #ifndef SQLITE_WINNT_MAX_PATH_CHARS
135dc7b0555Smistachkin # define SQLITE_WINNT_MAX_PATH_CHARS (UNICODE_STRING_MAX_CHARS)
1364496a232Smistachkin #endif
1374496a232Smistachkin
1384496a232Smistachkin /*
139e49d98fbSmistachkin ** Maximum pathname length (in bytes) for Win32. The MAX_PATH macro is in
14008046494Smistachkin ** characters, so we allocate 4 bytes per character assuming worst-case of
1414496a232Smistachkin ** 4-bytes-per-character for UTF8.
142e49d98fbSmistachkin */
14331706a2dSmistachkin #ifndef SQLITE_WIN32_MAX_PATH_BYTES
1444496a232Smistachkin # define SQLITE_WIN32_MAX_PATH_BYTES (SQLITE_WIN32_MAX_PATH_CHARS*4)
1454496a232Smistachkin #endif
1464496a232Smistachkin
1474496a232Smistachkin /*
1484496a232Smistachkin ** Maximum pathname length (in bytes) for WinNT. This should normally be
149dc7b0555Smistachkin ** UNICODE_STRING_MAX_CHARS * sizeof(WCHAR).
1504496a232Smistachkin */
1514496a232Smistachkin #ifndef SQLITE_WINNT_MAX_PATH_BYTES
1524496a232Smistachkin # define SQLITE_WINNT_MAX_PATH_BYTES \
1534496a232Smistachkin (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
154e49d98fbSmistachkin #endif
155e49d98fbSmistachkin
156e49d98fbSmistachkin /*
15731706a2dSmistachkin ** Maximum error message length (in chars) for WinRT.
158e49d98fbSmistachkin */
15931706a2dSmistachkin #ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS
16031706a2dSmistachkin # define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024)
161e49d98fbSmistachkin #endif
162e49d98fbSmistachkin
163e49d98fbSmistachkin /*
164533fb6deSmistachkin ** Returns non-zero if the character should be treated as a directory
165533fb6deSmistachkin ** separator.
166533fb6deSmistachkin */
167533fb6deSmistachkin #ifndef winIsDirSep
168533fb6deSmistachkin # define winIsDirSep(a) (((a) == '/') || ((a) == '\\'))
169533fb6deSmistachkin #endif
170533fb6deSmistachkin
171533fb6deSmistachkin /*
17236ca5359Smistachkin ** This macro is used when a local variable is set to a value that is
17336ca5359Smistachkin ** [sometimes] not used by the code (e.g. via conditional compilation).
17436ca5359Smistachkin */
17536ca5359Smistachkin #ifndef UNUSED_VARIABLE_VALUE
17636ca5359Smistachkin # define UNUSED_VARIABLE_VALUE(x) (void)(x)
17736ca5359Smistachkin #endif
17836ca5359Smistachkin
17936ca5359Smistachkin /*
18014eca4efSmistachkin ** Returns the character that should be used as the directory separator.
181533fb6deSmistachkin */
18214eca4efSmistachkin #ifndef winGetDirSep
18314eca4efSmistachkin # define winGetDirSep() '\\'
184533fb6deSmistachkin #endif
185533fb6deSmistachkin
186533fb6deSmistachkin /*
187f1dacbfcSmistachkin ** Do we need to manually define the Win32 file mapping APIs for use with WAL
18873767829Smistachkin ** mode or memory mapped files (e.g. these APIs are available in the Windows
18973767829Smistachkin ** CE SDK; however, they are not present in the header file)?
190f1dacbfcSmistachkin */
19173767829Smistachkin #if SQLITE_WIN32_FILEMAPPING_API && \
19273767829Smistachkin (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
193f1dacbfcSmistachkin /*
194f1dacbfcSmistachkin ** Two of the file mapping APIs are different under WinRT. Figure out which
195f1dacbfcSmistachkin ** set we need.
196f1dacbfcSmistachkin */
197f1dacbfcSmistachkin #if SQLITE_OS_WINRT
19831d72112Smistachkin WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \
19931d72112Smistachkin LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR);
200f1dacbfcSmistachkin
20131d72112Smistachkin WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T);
202f1dacbfcSmistachkin #else
203f1dacbfcSmistachkin #if defined(SQLITE_WIN32_HAS_ANSI)
20431d72112Smistachkin WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \
20531d72112Smistachkin DWORD, DWORD, DWORD, LPCSTR);
206f1dacbfcSmistachkin #endif /* defined(SQLITE_WIN32_HAS_ANSI) */
207f1dacbfcSmistachkin
208f1dacbfcSmistachkin #if defined(SQLITE_WIN32_HAS_WIDE)
20931d72112Smistachkin WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \
21031d72112Smistachkin DWORD, DWORD, DWORD, LPCWSTR);
211f1dacbfcSmistachkin #endif /* defined(SQLITE_WIN32_HAS_WIDE) */
212f1dacbfcSmistachkin
21331d72112Smistachkin WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
214f1dacbfcSmistachkin #endif /* SQLITE_OS_WINRT */
215f1dacbfcSmistachkin
216f1dacbfcSmistachkin /*
217ccb43714Smistachkin ** These file mapping APIs are common to both Win32 and WinRT.
218f1dacbfcSmistachkin */
219ccb43714Smistachkin
220ccb43714Smistachkin WINBASEAPI BOOL WINAPI FlushViewOfFile(LPCVOID, SIZE_T);
22131d72112Smistachkin WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
22273767829Smistachkin #endif /* SQLITE_WIN32_FILEMAPPING_API */
223f1dacbfcSmistachkin
224f1dacbfcSmistachkin /*
225318507b7Smistachkin ** Some Microsoft compilers lack this definition.
226171fa295Sshane */
227171fa295Sshane #ifndef INVALID_FILE_ATTRIBUTES
228171fa295Sshane # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
229171fa295Sshane #endif
230171fa295Sshane
2315483f772Smistachkin #ifndef FILE_FLAG_MASK
2325483f772Smistachkin # define FILE_FLAG_MASK (0xFF3C0000)
2335483f772Smistachkin #endif
2345483f772Smistachkin
2355483f772Smistachkin #ifndef FILE_ATTRIBUTE_MASK
2365483f772Smistachkin # define FILE_ATTRIBUTE_MASK (0x0003FFF7)
2375483f772Smistachkin #endif
2385483f772Smistachkin
2396f92833fSmistachkin #ifndef SQLITE_OMIT_WAL
240d95a3d35Smistachkin /* Forward references to structures used for WAL */
24183235214Sdrh typedef struct winShm winShm; /* A connection to shared-memory */
24283235214Sdrh typedef struct winShmNode winShmNode; /* A region of shared-memory */
2436f92833fSmistachkin #endif
24483235214Sdrh
245cc78fea4Sdrh /*
24672aead81Sdrh ** WinCE lacks native support for file locking so we have to fake it
24772aead81Sdrh ** with some code of our own.
24872aead81Sdrh */
24929bafeabSdanielk1977 #if SQLITE_OS_WINCE
25072aead81Sdrh typedef struct winceLock {
25172aead81Sdrh int nReaders; /* Number of reader locks obtained */
25272aead81Sdrh BOOL bPending; /* Indicates a pending lock has been obtained */
25372aead81Sdrh BOOL bReserved; /* Indicates a reserved lock has been obtained */
25472aead81Sdrh BOOL bExclusive; /* Indicates an exclusive lock has been obtained */
25572aead81Sdrh } winceLock;
25672aead81Sdrh #endif
25772aead81Sdrh
25872aead81Sdrh /*
259153c62c4Sdrh ** The winFile structure is a subclass of sqlite3_file* specific to the win32
260054889ecSdrh ** portability layer.
2619cbe6352Sdrh */
262054889ecSdrh typedef struct winFile winFile;
263054889ecSdrh struct winFile {
26483235214Sdrh const sqlite3_io_methods *pMethod; /*** Must be first ***/
26583235214Sdrh sqlite3_vfs *pVfs; /* The VFS used to open this file */
2669cbe6352Sdrh HANDLE h; /* Handle for accessing the file */
267f0b190d9Sdrh u8 locktype; /* Type of lock currently held on this file */
2689cbe6352Sdrh short sharedLockByte; /* Randomly chosen byte used as a shared lock */
269f12b3f60Sdrh u8 ctrlFlags; /* Flags. See WINFILE_* below */
2709db299fbSshane DWORD lastErrno; /* The Windows errno from the last I/O error */
2716f92833fSmistachkin #ifndef SQLITE_OMIT_WAL
27283235214Sdrh winShm *pShm; /* Instance of shared memory on this file */
2736f92833fSmistachkin #endif
27483235214Sdrh const char *zPath; /* Full pathname of this file */
275502019c8Sdan int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
27629bafeabSdanielk1977 #if SQLITE_OS_WINCE
277318507b7Smistachkin LPWSTR zDeleteOnClose; /* Name of file to delete when closing */
27872aead81Sdrh HANDLE hMutex; /* Mutex used to control access to shared lock */
27972aead81Sdrh HANDLE hShared; /* Shared memory segment used for locking */
28072aead81Sdrh winceLock local; /* Locks obtained by this instance of winFile */
28172aead81Sdrh winceLock *shared; /* Global shared lock memory for the file */
282cc78fea4Sdrh #endif
2835824e053Smistachkin #if SQLITE_MAX_MMAP_SIZE>0
2845175b324Sdrh int nFetchOut; /* Number of outstanding xFetch references */
285daf9a5a4Smistachkin HANDLE hMap; /* Handle for accessing memory mapping */
286daf9a5a4Smistachkin void *pMapRegion; /* Area memory mapped */
28748ea97e5Sdrh sqlite3_int64 mmapSize; /* Size of mapped region */
2889b4c59faSdrh sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
2895824e053Smistachkin #endif
2909cbe6352Sdrh };
2919cbe6352Sdrh
2921b186a99Smistachkin /*
2931e75483cSmistachkin ** The winVfsAppData structure is used for the pAppData member for all of the
2941e75483cSmistachkin ** Win32 VFS variants.
2951e75483cSmistachkin */
2961e75483cSmistachkin typedef struct winVfsAppData winVfsAppData;
2971e75483cSmistachkin struct winVfsAppData {
2981e75483cSmistachkin const sqlite3_io_methods *pMethod; /* The file I/O methods to use. */
2991e75483cSmistachkin void *pAppData; /* The extra pAppData, if any. */
3001e75483cSmistachkin BOOL bNoLock; /* Non-zero if locking is disabled. */
3011e75483cSmistachkin };
3021e75483cSmistachkin
3031e75483cSmistachkin /*
304f12b3f60Sdrh ** Allowed values for winFile.ctrlFlags
305f12b3f60Sdrh */
306daf9a5a4Smistachkin #define WINFILE_RDONLY 0x02 /* Connection is read only */
307f12b3f60Sdrh #define WINFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
308cb15f35fSdrh #define WINFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
309f12b3f60Sdrh
310f12b3f60Sdrh /*
311f4f327ceSmistachkin * The size of the buffer used by sqlite3_win32_write_debug().
312f4f327ceSmistachkin */
313f4f327ceSmistachkin #ifndef SQLITE_WIN32_DBG_BUF_SIZE
31416afb9eeSmistachkin # define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
315f4f327ceSmistachkin #endif
316f4f327ceSmistachkin
317f4f327ceSmistachkin /*
3181b186a99Smistachkin * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
3191b186a99Smistachkin * various Win32 API heap functions instead of our own.
3201b186a99Smistachkin */
3211b186a99Smistachkin #ifdef SQLITE_WIN32_MALLOC
32240c471d9Smistachkin
32340c471d9Smistachkin /*
32440c471d9Smistachkin * If this is non-zero, an isolated heap will be created by the native Win32
32540c471d9Smistachkin * allocator subsystem; otherwise, the default process heap will be used. This
32640c471d9Smistachkin * setting has no effect when compiling for WinRT. By default, this is enabled
32740c471d9Smistachkin * and an isolated heap will be created to store all allocated data.
32840c471d9Smistachkin *
32940c471d9Smistachkin ******************************************************************************
33040c471d9Smistachkin * WARNING: It is important to note that when this setting is non-zero and the
33140c471d9Smistachkin * winMemShutdown function is called (e.g. by the sqlite3_shutdown
33240c471d9Smistachkin * function), all data that was allocated using the isolated heap will
33340c471d9Smistachkin * be freed immediately and any attempt to access any of that freed
33440c471d9Smistachkin * data will almost certainly result in an immediate access violation.
33540c471d9Smistachkin ******************************************************************************
33640c471d9Smistachkin */
33740c471d9Smistachkin #ifndef SQLITE_WIN32_HEAP_CREATE
33840c471d9Smistachkin # define SQLITE_WIN32_HEAP_CREATE (TRUE)
33940c471d9Smistachkin #endif
34040c471d9Smistachkin
3411b186a99Smistachkin /*
3425e6710abSmistachkin * This is the maximum possible initial size of the Win32-specific heap, in
3435e6710abSmistachkin * bytes.
3445e6710abSmistachkin */
3455e6710abSmistachkin #ifndef SQLITE_WIN32_HEAP_MAX_INIT_SIZE
3465e6710abSmistachkin # define SQLITE_WIN32_HEAP_MAX_INIT_SIZE (4294967295U)
3475e6710abSmistachkin #endif
3485e6710abSmistachkin
3495e6710abSmistachkin /*
3505e6710abSmistachkin * This is the extra space for the initial size of the Win32-specific heap,
3515e6710abSmistachkin * in bytes. This value may be zero.
3525e6710abSmistachkin */
3535e6710abSmistachkin #ifndef SQLITE_WIN32_HEAP_INIT_EXTRA
3545e6710abSmistachkin # define SQLITE_WIN32_HEAP_INIT_EXTRA (4194304)
3555e6710abSmistachkin #endif
3565e6710abSmistachkin
3575e6710abSmistachkin /*
3585e6710abSmistachkin * Calculate the maximum legal cache size, in pages, based on the maximum
3595e6710abSmistachkin * possible initial heap size and the default page size, setting aside the
3605e6710abSmistachkin * needed extra space.
3615e6710abSmistachkin */
3625e6710abSmistachkin #ifndef SQLITE_WIN32_MAX_CACHE_SIZE
3635e6710abSmistachkin # define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
3645e6710abSmistachkin (SQLITE_WIN32_HEAP_INIT_EXTRA)) / \
3655e6710abSmistachkin (SQLITE_DEFAULT_PAGE_SIZE))
3665e6710abSmistachkin #endif
3675e6710abSmistachkin
3685e6710abSmistachkin /*
369eb2329beSmistachkin * This is cache size used in the calculation of the initial size of the
370eb2329beSmistachkin * Win32-specific heap. It cannot be negative.
371eb2329beSmistachkin */
372eb2329beSmistachkin #ifndef SQLITE_WIN32_CACHE_SIZE
373eb2329beSmistachkin # if SQLITE_DEFAULT_CACHE_SIZE>=0
374eb2329beSmistachkin # define SQLITE_WIN32_CACHE_SIZE (SQLITE_DEFAULT_CACHE_SIZE)
375eb2329beSmistachkin # else
376eb2329beSmistachkin # define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
377eb2329beSmistachkin # endif
378eb2329beSmistachkin #endif
379eb2329beSmistachkin
380eb2329beSmistachkin /*
3815e6710abSmistachkin * Make sure that the calculated cache size, in pages, cannot cause the
3825e6710abSmistachkin * initial size of the Win32-specific heap to exceed the maximum amount
3835e6710abSmistachkin * of memory that can be specified in the call to HeapCreate.
3845e6710abSmistachkin */
3854689956bSmistachkin #if SQLITE_WIN32_CACHE_SIZE>SQLITE_WIN32_MAX_CACHE_SIZE
3865e6710abSmistachkin # undef SQLITE_WIN32_CACHE_SIZE
3875e6710abSmistachkin # define SQLITE_WIN32_CACHE_SIZE (2000)
3885e6710abSmistachkin #endif
3895e6710abSmistachkin
3905e6710abSmistachkin /*
3911b186a99Smistachkin * The initial size of the Win32-specific heap. This value may be zero.
3921b186a99Smistachkin */
3931b186a99Smistachkin #ifndef SQLITE_WIN32_HEAP_INIT_SIZE
394eb2329beSmistachkin # define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_WIN32_CACHE_SIZE) * \
3955e6710abSmistachkin (SQLITE_DEFAULT_PAGE_SIZE) + \
3965e6710abSmistachkin (SQLITE_WIN32_HEAP_INIT_EXTRA))
3971b186a99Smistachkin #endif
3981b186a99Smistachkin
3991b186a99Smistachkin /*
4001b186a99Smistachkin * The maximum size of the Win32-specific heap. This value may be zero.
4011b186a99Smistachkin */
4021b186a99Smistachkin #ifndef SQLITE_WIN32_HEAP_MAX_SIZE
4031b186a99Smistachkin # define SQLITE_WIN32_HEAP_MAX_SIZE (0)
4041b186a99Smistachkin #endif
4051b186a99Smistachkin
4061b186a99Smistachkin /*
407155892ccSmistachkin * The extra flags to use in calls to the Win32 heap APIs. This value may be
408155892ccSmistachkin * zero for the default behavior.
409155892ccSmistachkin */
410155892ccSmistachkin #ifndef SQLITE_WIN32_HEAP_FLAGS
411155892ccSmistachkin # define SQLITE_WIN32_HEAP_FLAGS (0)
412155892ccSmistachkin #endif
413155892ccSmistachkin
41468f7a9e9Sdrh
415155892ccSmistachkin /*
4161b186a99Smistachkin ** The winMemData structure stores information required by the Win32-specific
4171b186a99Smistachkin ** sqlite3_mem_methods implementation.
4181b186a99Smistachkin */
4191b186a99Smistachkin typedef struct winMemData winMemData;
4201b186a99Smistachkin struct winMemData {
4217da32b58Smistachkin #ifndef NDEBUG
422e8f91053Smistachkin u32 magic1; /* Magic number to detect structure corruption. */
4237da32b58Smistachkin #endif
4241b186a99Smistachkin HANDLE hHeap; /* The handle to our heap. */
4251b186a99Smistachkin BOOL bOwned; /* Do we own the heap (i.e. destroy it on shutdown)? */
426e8f91053Smistachkin #ifndef NDEBUG
427e8f91053Smistachkin u32 magic2; /* Magic number to detect structure corruption. */
428e8f91053Smistachkin #endif
4291b186a99Smistachkin };
4301b186a99Smistachkin
4317da32b58Smistachkin #ifndef NDEBUG
432e8f91053Smistachkin #define WINMEM_MAGIC1 0x42b2830b
433e8f91053Smistachkin #define WINMEM_MAGIC2 0xbd4d7cf4
4347da32b58Smistachkin #endif
4351b186a99Smistachkin
4367da32b58Smistachkin static struct winMemData win_mem_data = {
4377da32b58Smistachkin #ifndef NDEBUG
438e8f91053Smistachkin WINMEM_MAGIC1,
4397da32b58Smistachkin #endif
4407da32b58Smistachkin NULL, FALSE
441e8f91053Smistachkin #ifndef NDEBUG
442e8f91053Smistachkin ,WINMEM_MAGIC2
443e8f91053Smistachkin #endif
4447da32b58Smistachkin };
4451b186a99Smistachkin
4467da32b58Smistachkin #ifndef NDEBUG
447e8f91053Smistachkin #define winMemAssertMagic1() assert( win_mem_data.magic1==WINMEM_MAGIC1 )
448e8f91053Smistachkin #define winMemAssertMagic2() assert( win_mem_data.magic2==WINMEM_MAGIC2 )
449e8f91053Smistachkin #define winMemAssertMagic() winMemAssertMagic1(); winMemAssertMagic2();
4507da32b58Smistachkin #else
4517da32b58Smistachkin #define winMemAssertMagic()
4527da32b58Smistachkin #endif
4537da32b58Smistachkin
454e8f91053Smistachkin #define winMemGetDataPtr() &win_mem_data
455468690efSmistachkin #define winMemGetHeap() win_mem_data.hHeap
456e8f91053Smistachkin #define winMemGetOwned() win_mem_data.bOwned
457468690efSmistachkin
4581b186a99Smistachkin static void *winMemMalloc(int nBytes);
4591b186a99Smistachkin static void winMemFree(void *pPrior);
4601b186a99Smistachkin static void *winMemRealloc(void *pPrior, int nBytes);
4611b186a99Smistachkin static int winMemSize(void *p);
4621b186a99Smistachkin static int winMemRoundup(int n);
4631b186a99Smistachkin static int winMemInit(void *pAppData);
4641b186a99Smistachkin static void winMemShutdown(void *pAppData);
4651b186a99Smistachkin
4661b186a99Smistachkin const sqlite3_mem_methods *sqlite3MemGetWin32(void);
4671b186a99Smistachkin #endif /* SQLITE_WIN32_MALLOC */
46850990dbbSdrh
46950daafc7Sshane /*
470c0929987Sdrh ** The following variable is (normally) set once and never changes
4716c3c1a09Smistachkin ** thereafter. It records whether the operating system is Win9x
472c0929987Sdrh ** or WinNT.
473c0929987Sdrh **
474c0929987Sdrh ** 0: Operating system unknown.
4756c3c1a09Smistachkin ** 1: Operating system is Win9x.
476c0929987Sdrh ** 2: Operating system is WinNT.
477c0929987Sdrh **
478c0929987Sdrh ** In order to facilitate testing on a WinNT system, the test fixture
479c0929987Sdrh ** can manually set this value to 1 to emulate Win98 behavior.
480c0929987Sdrh */
481153c62c4Sdrh #ifdef SQLITE_TEST
482ce64d610Smistachkin LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
48316d511a6Smistachkin #else
484ce64d610Smistachkin static LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
485153c62c4Sdrh #endif
486c0929987Sdrh
487318507b7Smistachkin #ifndef SYSCALL
488318507b7Smistachkin # define SYSCALL sqlite3_syscall_ptr
489318507b7Smistachkin #endif
490318507b7Smistachkin
4917acec68aSdrh /*
492df562d55Smistachkin ** This function is not available on Windows CE or WinRT.
493df562d55Smistachkin */
494df562d55Smistachkin
495df562d55Smistachkin #if SQLITE_OS_WINCE || SQLITE_OS_WINRT
496df562d55Smistachkin # define osAreFileApisANSI() 1
497df562d55Smistachkin #endif
498df562d55Smistachkin
499df562d55Smistachkin /*
5007acec68aSdrh ** Many system calls are accessed through pointer-to-functions so that
5017acec68aSdrh ** they may be overridden at runtime to facilitate fault injection during
5027acec68aSdrh ** testing and sandboxing. The following array holds the names and pointers
5037acec68aSdrh ** to all overrideable system calls.
5047acec68aSdrh */
505318507b7Smistachkin static struct win_syscall {
50648864df9Smistachkin const char *zName; /* Name of the system call */
507318507b7Smistachkin sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
508318507b7Smistachkin sqlite3_syscall_ptr pDefault; /* Default value */
509318507b7Smistachkin } aSyscall[] = {
510df562d55Smistachkin #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
511318507b7Smistachkin { "AreFileApisANSI", (SYSCALL)AreFileApisANSI, 0 },
512318507b7Smistachkin #else
513318507b7Smistachkin { "AreFileApisANSI", (SYSCALL)0, 0 },
514318507b7Smistachkin #endif
515318507b7Smistachkin
5160df898e2Smistachkin #ifndef osAreFileApisANSI
517df562d55Smistachkin #define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
5180df898e2Smistachkin #endif
519df562d55Smistachkin
520318507b7Smistachkin #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
521318507b7Smistachkin { "CharLowerW", (SYSCALL)CharLowerW, 0 },
522318507b7Smistachkin #else
523318507b7Smistachkin { "CharLowerW", (SYSCALL)0, 0 },
524318507b7Smistachkin #endif
525318507b7Smistachkin
526318507b7Smistachkin #define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
527318507b7Smistachkin
528318507b7Smistachkin #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
529318507b7Smistachkin { "CharUpperW", (SYSCALL)CharUpperW, 0 },
530318507b7Smistachkin #else
531318507b7Smistachkin { "CharUpperW", (SYSCALL)0, 0 },
532318507b7Smistachkin #endif
533318507b7Smistachkin
534318507b7Smistachkin #define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
535318507b7Smistachkin
536318507b7Smistachkin { "CloseHandle", (SYSCALL)CloseHandle, 0 },
537318507b7Smistachkin
538318507b7Smistachkin #define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
539318507b7Smistachkin
540318507b7Smistachkin #if defined(SQLITE_WIN32_HAS_ANSI)
541318507b7Smistachkin { "CreateFileA", (SYSCALL)CreateFileA, 0 },
542318507b7Smistachkin #else
543318507b7Smistachkin { "CreateFileA", (SYSCALL)0, 0 },
544318507b7Smistachkin #endif
545318507b7Smistachkin
546318507b7Smistachkin #define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
547318507b7Smistachkin LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
548318507b7Smistachkin
549df562d55Smistachkin #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
550318507b7Smistachkin { "CreateFileW", (SYSCALL)CreateFileW, 0 },
551318507b7Smistachkin #else
552318507b7Smistachkin { "CreateFileW", (SYSCALL)0, 0 },
553318507b7Smistachkin #endif
554318507b7Smistachkin
555318507b7Smistachkin #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
556318507b7Smistachkin LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
557318507b7Smistachkin
558d5be6f01Smistachkin #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
559d5be6f01Smistachkin (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) && \
560d5be6f01Smistachkin SQLITE_WIN32_CREATEFILEMAPPINGA
561c60941f8Smistachkin { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 },
562c60941f8Smistachkin #else
563c60941f8Smistachkin { "CreateFileMappingA", (SYSCALL)0, 0 },
564c60941f8Smistachkin #endif
565c60941f8Smistachkin
566c60941f8Smistachkin #define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
567c60941f8Smistachkin DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
568c60941f8Smistachkin
5691700b1c6Smistachkin #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
57073767829Smistachkin (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
571318507b7Smistachkin { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 },
572318507b7Smistachkin #else
573318507b7Smistachkin { "CreateFileMappingW", (SYSCALL)0, 0 },
574318507b7Smistachkin #endif
575318507b7Smistachkin
576318507b7Smistachkin #define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
577c60941f8Smistachkin DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
578318507b7Smistachkin
579df562d55Smistachkin #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
580318507b7Smistachkin { "CreateMutexW", (SYSCALL)CreateMutexW, 0 },
581318507b7Smistachkin #else
582318507b7Smistachkin { "CreateMutexW", (SYSCALL)0, 0 },
583318507b7Smistachkin #endif
584318507b7Smistachkin
585318507b7Smistachkin #define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
586c60941f8Smistachkin LPCWSTR))aSyscall[8].pCurrent)
587318507b7Smistachkin
588318507b7Smistachkin #if defined(SQLITE_WIN32_HAS_ANSI)
589318507b7Smistachkin { "DeleteFileA", (SYSCALL)DeleteFileA, 0 },
590318507b7Smistachkin #else
591318507b7Smistachkin { "DeleteFileA", (SYSCALL)0, 0 },
592318507b7Smistachkin #endif
593318507b7Smistachkin
594c60941f8Smistachkin #define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
595318507b7Smistachkin
596318507b7Smistachkin #if defined(SQLITE_WIN32_HAS_WIDE)
597318507b7Smistachkin { "DeleteFileW", (SYSCALL)DeleteFileW, 0 },
598318507b7Smistachkin #else
599318507b7Smistachkin { "DeleteFileW", (SYSCALL)0, 0 },
600318507b7Smistachkin #endif
601318507b7Smistachkin
602c60941f8Smistachkin #define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
603318507b7Smistachkin
604318507b7Smistachkin #if SQLITE_OS_WINCE
605318507b7Smistachkin { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
606318507b7Smistachkin #else
607318507b7Smistachkin { "FileTimeToLocalFileTime", (SYSCALL)0, 0 },
608318507b7Smistachkin #endif
609318507b7Smistachkin
610318507b7Smistachkin #define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
611c60941f8Smistachkin LPFILETIME))aSyscall[11].pCurrent)
612318507b7Smistachkin
613318507b7Smistachkin #if SQLITE_OS_WINCE
614318507b7Smistachkin { "FileTimeToSystemTime", (SYSCALL)FileTimeToSystemTime, 0 },
615318507b7Smistachkin #else
616318507b7Smistachkin { "FileTimeToSystemTime", (SYSCALL)0, 0 },
617318507b7Smistachkin #endif
618318507b7Smistachkin
619318507b7Smistachkin #define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
620c60941f8Smistachkin LPSYSTEMTIME))aSyscall[12].pCurrent)
621318507b7Smistachkin
622318507b7Smistachkin { "FlushFileBuffers", (SYSCALL)FlushFileBuffers, 0 },
623318507b7Smistachkin
624c60941f8Smistachkin #define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
625318507b7Smistachkin
626318507b7Smistachkin #if defined(SQLITE_WIN32_HAS_ANSI)
627318507b7Smistachkin { "FormatMessageA", (SYSCALL)FormatMessageA, 0 },
628318507b7Smistachkin #else
629318507b7Smistachkin { "FormatMessageA", (SYSCALL)0, 0 },
630318507b7Smistachkin #endif
631318507b7Smistachkin
632318507b7Smistachkin #define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
633c60941f8Smistachkin DWORD,va_list*))aSyscall[14].pCurrent)
634318507b7Smistachkin
635318507b7Smistachkin #if defined(SQLITE_WIN32_HAS_WIDE)
636318507b7Smistachkin { "FormatMessageW", (SYSCALL)FormatMessageW, 0 },
637318507b7Smistachkin #else
638318507b7Smistachkin { "FormatMessageW", (SYSCALL)0, 0 },
639318507b7Smistachkin #endif
640318507b7Smistachkin
641318507b7Smistachkin #define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
642c60941f8Smistachkin DWORD,va_list*))aSyscall[15].pCurrent)
643318507b7Smistachkin
64408c1c315Smistachkin #if !defined(SQLITE_OMIT_LOAD_EXTENSION)
645318507b7Smistachkin { "FreeLibrary", (SYSCALL)FreeLibrary, 0 },
64608c1c315Smistachkin #else
64708c1c315Smistachkin { "FreeLibrary", (SYSCALL)0, 0 },
64808c1c315Smistachkin #endif
649318507b7Smistachkin
650c60941f8Smistachkin #define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
651318507b7Smistachkin
652318507b7Smistachkin { "GetCurrentProcessId", (SYSCALL)GetCurrentProcessId, 0 },
653318507b7Smistachkin
654c60941f8Smistachkin #define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
655318507b7Smistachkin
656318507b7Smistachkin #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
657318507b7Smistachkin { "GetDiskFreeSpaceA", (SYSCALL)GetDiskFreeSpaceA, 0 },
658318507b7Smistachkin #else
659318507b7Smistachkin { "GetDiskFreeSpaceA", (SYSCALL)0, 0 },
660318507b7Smistachkin #endif
661318507b7Smistachkin
662318507b7Smistachkin #define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
663c60941f8Smistachkin LPDWORD))aSyscall[18].pCurrent)
664318507b7Smistachkin
665df562d55Smistachkin #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
666318507b7Smistachkin { "GetDiskFreeSpaceW", (SYSCALL)GetDiskFreeSpaceW, 0 },
667318507b7Smistachkin #else
668318507b7Smistachkin { "GetDiskFreeSpaceW", (SYSCALL)0, 0 },
669318507b7Smistachkin #endif
670318507b7Smistachkin
671318507b7Smistachkin #define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
672c60941f8Smistachkin LPDWORD))aSyscall[19].pCurrent)
673318507b7Smistachkin
674318507b7Smistachkin #if defined(SQLITE_WIN32_HAS_ANSI)
675318507b7Smistachkin { "GetFileAttributesA", (SYSCALL)GetFileAttributesA, 0 },
676318507b7Smistachkin #else
677318507b7Smistachkin { "GetFileAttributesA", (SYSCALL)0, 0 },
678318507b7Smistachkin #endif
679318507b7Smistachkin
680c60941f8Smistachkin #define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
681318507b7Smistachkin
682df562d55Smistachkin #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
683318507b7Smistachkin { "GetFileAttributesW", (SYSCALL)GetFileAttributesW, 0 },
684318507b7Smistachkin #else
685318507b7Smistachkin { "GetFileAttributesW", (SYSCALL)0, 0 },
686318507b7Smistachkin #endif
687318507b7Smistachkin
688c60941f8Smistachkin #define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
689318507b7Smistachkin
690318507b7Smistachkin #if defined(SQLITE_WIN32_HAS_WIDE)
691318507b7Smistachkin { "GetFileAttributesExW", (SYSCALL)GetFileAttributesExW, 0 },
692318507b7Smistachkin #else
693318507b7Smistachkin { "GetFileAttributesExW", (SYSCALL)0, 0 },
694318507b7Smistachkin #endif
695318507b7Smistachkin
696318507b7Smistachkin #define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
697c60941f8Smistachkin LPVOID))aSyscall[22].pCurrent)
698318507b7Smistachkin
699df562d55Smistachkin #if !SQLITE_OS_WINRT
700318507b7Smistachkin { "GetFileSize", (SYSCALL)GetFileSize, 0 },
701df562d55Smistachkin #else
702df562d55Smistachkin { "GetFileSize", (SYSCALL)0, 0 },
70324560d16Sdrh #endif
704318507b7Smistachkin
705c60941f8Smistachkin #define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
706318507b7Smistachkin
707318507b7Smistachkin #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
708318507b7Smistachkin { "GetFullPathNameA", (SYSCALL)GetFullPathNameA, 0 },
709318507b7Smistachkin #else
710318507b7Smistachkin { "GetFullPathNameA", (SYSCALL)0, 0 },
711318507b7Smistachkin #endif
712318507b7Smistachkin
713318507b7Smistachkin #define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
714c60941f8Smistachkin LPSTR*))aSyscall[24].pCurrent)
715318507b7Smistachkin
716c5484654Smistachkin #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
717318507b7Smistachkin { "GetFullPathNameW", (SYSCALL)GetFullPathNameW, 0 },
718318507b7Smistachkin #else
719318507b7Smistachkin { "GetFullPathNameW", (SYSCALL)0, 0 },
720318507b7Smistachkin #endif
721318507b7Smistachkin
722318507b7Smistachkin #define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
723c60941f8Smistachkin LPWSTR*))aSyscall[25].pCurrent)
724318507b7Smistachkin
725318507b7Smistachkin { "GetLastError", (SYSCALL)GetLastError, 0 },
726318507b7Smistachkin
727c60941f8Smistachkin #define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
728318507b7Smistachkin
72908c1c315Smistachkin #if !defined(SQLITE_OMIT_LOAD_EXTENSION)
730318507b7Smistachkin #if SQLITE_OS_WINCE
731318507b7Smistachkin /* The GetProcAddressA() routine is only available on Windows CE. */
732318507b7Smistachkin { "GetProcAddressA", (SYSCALL)GetProcAddressA, 0 },
733318507b7Smistachkin #else
734318507b7Smistachkin /* All other Windows platforms expect GetProcAddress() to take
735318507b7Smistachkin ** an ANSI string regardless of the _UNICODE setting */
736318507b7Smistachkin { "GetProcAddressA", (SYSCALL)GetProcAddress, 0 },
737318507b7Smistachkin #endif
73808c1c315Smistachkin #else
73908c1c315Smistachkin { "GetProcAddressA", (SYSCALL)0, 0 },
74008c1c315Smistachkin #endif
741318507b7Smistachkin
742318507b7Smistachkin #define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
743c60941f8Smistachkin LPCSTR))aSyscall[27].pCurrent)
744318507b7Smistachkin
745df562d55Smistachkin #if !SQLITE_OS_WINRT
746318507b7Smistachkin { "GetSystemInfo", (SYSCALL)GetSystemInfo, 0 },
747df562d55Smistachkin #else
748df562d55Smistachkin { "GetSystemInfo", (SYSCALL)0, 0 },
749df562d55Smistachkin #endif
750318507b7Smistachkin
751c60941f8Smistachkin #define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
752318507b7Smistachkin
753318507b7Smistachkin { "GetSystemTime", (SYSCALL)GetSystemTime, 0 },
754318507b7Smistachkin
755c60941f8Smistachkin #define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
756318507b7Smistachkin
757318507b7Smistachkin #if !SQLITE_OS_WINCE
758318507b7Smistachkin { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
759318507b7Smistachkin #else
760318507b7Smistachkin { "GetSystemTimeAsFileTime", (SYSCALL)0, 0 },
761318507b7Smistachkin #endif
762318507b7Smistachkin
763318507b7Smistachkin #define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
764c60941f8Smistachkin LPFILETIME))aSyscall[30].pCurrent)
765318507b7Smistachkin
766318507b7Smistachkin #if defined(SQLITE_WIN32_HAS_ANSI)
767318507b7Smistachkin { "GetTempPathA", (SYSCALL)GetTempPathA, 0 },
768318507b7Smistachkin #else
769318507b7Smistachkin { "GetTempPathA", (SYSCALL)0, 0 },
770318507b7Smistachkin #endif
771318507b7Smistachkin
772c60941f8Smistachkin #define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
773318507b7Smistachkin
774c5484654Smistachkin #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
775318507b7Smistachkin { "GetTempPathW", (SYSCALL)GetTempPathW, 0 },
776318507b7Smistachkin #else
777318507b7Smistachkin { "GetTempPathW", (SYSCALL)0, 0 },
778318507b7Smistachkin #endif
779318507b7Smistachkin
780c60941f8Smistachkin #define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
781318507b7Smistachkin
782df562d55Smistachkin #if !SQLITE_OS_WINRT
783318507b7Smistachkin { "GetTickCount", (SYSCALL)GetTickCount, 0 },
784df562d55Smistachkin #else
785df562d55Smistachkin { "GetTickCount", (SYSCALL)0, 0 },
786df562d55Smistachkin #endif
787318507b7Smistachkin
788c60941f8Smistachkin #define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
789318507b7Smistachkin
790d5be6f01Smistachkin #if defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_GETVERSIONEX
791318507b7Smistachkin { "GetVersionExA", (SYSCALL)GetVersionExA, 0 },
792318507b7Smistachkin #else
793318507b7Smistachkin { "GetVersionExA", (SYSCALL)0, 0 },
794318507b7Smistachkin #endif
795318507b7Smistachkin
796318507b7Smistachkin #define osGetVersionExA ((BOOL(WINAPI*)( \
797c60941f8Smistachkin LPOSVERSIONINFOA))aSyscall[34].pCurrent)
798318507b7Smistachkin
7990f7e08e0Smistachkin #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
800d5be6f01Smistachkin SQLITE_WIN32_GETVERSIONEX
801b8af6a28Smistachkin { "GetVersionExW", (SYSCALL)GetVersionExW, 0 },
802b8af6a28Smistachkin #else
803b8af6a28Smistachkin { "GetVersionExW", (SYSCALL)0, 0 },
804b8af6a28Smistachkin #endif
805b8af6a28Smistachkin
806b8af6a28Smistachkin #define osGetVersionExW ((BOOL(WINAPI*)( \
807b8af6a28Smistachkin LPOSVERSIONINFOW))aSyscall[35].pCurrent)
808318507b7Smistachkin
809318507b7Smistachkin { "HeapAlloc", (SYSCALL)HeapAlloc, 0 },
810318507b7Smistachkin
811318507b7Smistachkin #define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
812b8af6a28Smistachkin SIZE_T))aSyscall[36].pCurrent)
813318507b7Smistachkin
814df562d55Smistachkin #if !SQLITE_OS_WINRT
815318507b7Smistachkin { "HeapCreate", (SYSCALL)HeapCreate, 0 },
816df562d55Smistachkin #else
817df562d55Smistachkin { "HeapCreate", (SYSCALL)0, 0 },
818df562d55Smistachkin #endif
819318507b7Smistachkin
820318507b7Smistachkin #define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
821b8af6a28Smistachkin SIZE_T))aSyscall[37].pCurrent)
822318507b7Smistachkin
823df562d55Smistachkin #if !SQLITE_OS_WINRT
824318507b7Smistachkin { "HeapDestroy", (SYSCALL)HeapDestroy, 0 },
825df562d55Smistachkin #else
826df562d55Smistachkin { "HeapDestroy", (SYSCALL)0, 0 },
827df562d55Smistachkin #endif
828318507b7Smistachkin
829b8af6a28Smistachkin #define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[38].pCurrent)
830318507b7Smistachkin
831318507b7Smistachkin { "HeapFree", (SYSCALL)HeapFree, 0 },
832318507b7Smistachkin
833b8af6a28Smistachkin #define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[39].pCurrent)
834318507b7Smistachkin
835318507b7Smistachkin { "HeapReAlloc", (SYSCALL)HeapReAlloc, 0 },
836318507b7Smistachkin
837318507b7Smistachkin #define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
838b8af6a28Smistachkin SIZE_T))aSyscall[40].pCurrent)
839318507b7Smistachkin
840318507b7Smistachkin { "HeapSize", (SYSCALL)HeapSize, 0 },
841318507b7Smistachkin
842318507b7Smistachkin #define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
843b8af6a28Smistachkin LPCVOID))aSyscall[41].pCurrent)
844318507b7Smistachkin
845df562d55Smistachkin #if !SQLITE_OS_WINRT
846318507b7Smistachkin { "HeapValidate", (SYSCALL)HeapValidate, 0 },
847df562d55Smistachkin #else
848df562d55Smistachkin { "HeapValidate", (SYSCALL)0, 0 },
849df562d55Smistachkin #endif
850318507b7Smistachkin
851318507b7Smistachkin #define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
852b8af6a28Smistachkin LPCVOID))aSyscall[42].pCurrent)
853318507b7Smistachkin
8545134a817Smistachkin #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
855e8f91053Smistachkin { "HeapCompact", (SYSCALL)HeapCompact, 0 },
856e8f91053Smistachkin #else
857e8f91053Smistachkin { "HeapCompact", (SYSCALL)0, 0 },
858e8f91053Smistachkin #endif
859e8f91053Smistachkin
860e8f91053Smistachkin #define osHeapCompact ((UINT(WINAPI*)(HANDLE,DWORD))aSyscall[43].pCurrent)
861e8f91053Smistachkin
86208c1c315Smistachkin #if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
863318507b7Smistachkin { "LoadLibraryA", (SYSCALL)LoadLibraryA, 0 },
864318507b7Smistachkin #else
865318507b7Smistachkin { "LoadLibraryA", (SYSCALL)0, 0 },
866318507b7Smistachkin #endif
867318507b7Smistachkin
868e8f91053Smistachkin #define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[44].pCurrent)
869318507b7Smistachkin
87008c1c315Smistachkin #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
87108c1c315Smistachkin !defined(SQLITE_OMIT_LOAD_EXTENSION)
872318507b7Smistachkin { "LoadLibraryW", (SYSCALL)LoadLibraryW, 0 },
873318507b7Smistachkin #else
874318507b7Smistachkin { "LoadLibraryW", (SYSCALL)0, 0 },
875318507b7Smistachkin #endif
876318507b7Smistachkin
877e8f91053Smistachkin #define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[45].pCurrent)
878318507b7Smistachkin
879df562d55Smistachkin #if !SQLITE_OS_WINRT
880318507b7Smistachkin { "LocalFree", (SYSCALL)LocalFree, 0 },
881df562d55Smistachkin #else
882df562d55Smistachkin { "LocalFree", (SYSCALL)0, 0 },
883df562d55Smistachkin #endif
884318507b7Smistachkin
885e8f91053Smistachkin #define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[46].pCurrent)
886318507b7Smistachkin
887df562d55Smistachkin #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
888318507b7Smistachkin { "LockFile", (SYSCALL)LockFile, 0 },
889318507b7Smistachkin #else
890318507b7Smistachkin { "LockFile", (SYSCALL)0, 0 },
891318507b7Smistachkin #endif
892318507b7Smistachkin
8930df898e2Smistachkin #ifndef osLockFile
894df562d55Smistachkin #define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
895e8f91053Smistachkin DWORD))aSyscall[47].pCurrent)
8960df898e2Smistachkin #endif
897df562d55Smistachkin
898318507b7Smistachkin #if !SQLITE_OS_WINCE
899318507b7Smistachkin { "LockFileEx", (SYSCALL)LockFileEx, 0 },
900318507b7Smistachkin #else
901318507b7Smistachkin { "LockFileEx", (SYSCALL)0, 0 },
902318507b7Smistachkin #endif
903318507b7Smistachkin
9040df898e2Smistachkin #ifndef osLockFileEx
9055cfbeac0Smistachkin #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
906e8f91053Smistachkin LPOVERLAPPED))aSyscall[48].pCurrent)
9070df898e2Smistachkin #endif
9085cfbeac0Smistachkin
90973767829Smistachkin #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \
91073767829Smistachkin (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
911318507b7Smistachkin { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 },
912df562d55Smistachkin #else
913df562d55Smistachkin { "MapViewOfFile", (SYSCALL)0, 0 },
914df562d55Smistachkin #endif
915318507b7Smistachkin
916318507b7Smistachkin #define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
917e8f91053Smistachkin SIZE_T))aSyscall[49].pCurrent)
918318507b7Smistachkin
919318507b7Smistachkin { "MultiByteToWideChar", (SYSCALL)MultiByteToWideChar, 0 },
920318507b7Smistachkin
921318507b7Smistachkin #define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
922e8f91053Smistachkin int))aSyscall[50].pCurrent)
923318507b7Smistachkin
924318507b7Smistachkin { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
925318507b7Smistachkin
926318507b7Smistachkin #define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
927e8f91053Smistachkin LARGE_INTEGER*))aSyscall[51].pCurrent)
928318507b7Smistachkin
929318507b7Smistachkin { "ReadFile", (SYSCALL)ReadFile, 0 },
930318507b7Smistachkin
931318507b7Smistachkin #define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
932e8f91053Smistachkin LPOVERLAPPED))aSyscall[52].pCurrent)
933318507b7Smistachkin
934318507b7Smistachkin { "SetEndOfFile", (SYSCALL)SetEndOfFile, 0 },
935318507b7Smistachkin
936e8f91053Smistachkin #define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[53].pCurrent)
937318507b7Smistachkin
938df562d55Smistachkin #if !SQLITE_OS_WINRT
939318507b7Smistachkin { "SetFilePointer", (SYSCALL)SetFilePointer, 0 },
940df562d55Smistachkin #else
941df562d55Smistachkin { "SetFilePointer", (SYSCALL)0, 0 },
9428045df0aSdrh #endif
943318507b7Smistachkin
944318507b7Smistachkin #define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
945e8f91053Smistachkin DWORD))aSyscall[54].pCurrent)
946318507b7Smistachkin
947df562d55Smistachkin #if !SQLITE_OS_WINRT
948318507b7Smistachkin { "Sleep", (SYSCALL)Sleep, 0 },
949df562d55Smistachkin #else
950df562d55Smistachkin { "Sleep", (SYSCALL)0, 0 },
9517acec68aSdrh #endif
952318507b7Smistachkin
953e8f91053Smistachkin #define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[55].pCurrent)
954318507b7Smistachkin
955318507b7Smistachkin { "SystemTimeToFileTime", (SYSCALL)SystemTimeToFileTime, 0 },
956318507b7Smistachkin
957318507b7Smistachkin #define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
958e8f91053Smistachkin LPFILETIME))aSyscall[56].pCurrent)
959318507b7Smistachkin
960df562d55Smistachkin #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
961318507b7Smistachkin { "UnlockFile", (SYSCALL)UnlockFile, 0 },
962318507b7Smistachkin #else
963318507b7Smistachkin { "UnlockFile", (SYSCALL)0, 0 },
964318507b7Smistachkin #endif
965318507b7Smistachkin
9660df898e2Smistachkin #ifndef osUnlockFile
967df562d55Smistachkin #define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
968e8f91053Smistachkin DWORD))aSyscall[57].pCurrent)
9690df898e2Smistachkin #endif
970df562d55Smistachkin
971318507b7Smistachkin #if !SQLITE_OS_WINCE
972318507b7Smistachkin { "UnlockFileEx", (SYSCALL)UnlockFileEx, 0 },
973318507b7Smistachkin #else
974318507b7Smistachkin { "UnlockFileEx", (SYSCALL)0, 0 },
975318507b7Smistachkin #endif
976318507b7Smistachkin
977df562d55Smistachkin #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
978e8f91053Smistachkin LPOVERLAPPED))aSyscall[58].pCurrent)
979df562d55Smistachkin
98073767829Smistachkin #if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
981318507b7Smistachkin { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 },
9821700b1c6Smistachkin #else
9831700b1c6Smistachkin { "UnmapViewOfFile", (SYSCALL)0, 0 },
9841700b1c6Smistachkin #endif
985318507b7Smistachkin
986e8f91053Smistachkin #define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[59].pCurrent)
987318507b7Smistachkin
988318507b7Smistachkin { "WideCharToMultiByte", (SYSCALL)WideCharToMultiByte, 0 },
989318507b7Smistachkin
990318507b7Smistachkin #define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
991e8f91053Smistachkin LPCSTR,LPBOOL))aSyscall[60].pCurrent)
992318507b7Smistachkin
993318507b7Smistachkin { "WriteFile", (SYSCALL)WriteFile, 0 },
994318507b7Smistachkin
995318507b7Smistachkin #define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
996e8f91053Smistachkin LPOVERLAPPED))aSyscall[61].pCurrent)
997318507b7Smistachkin
998401e9153Smistachkin #if SQLITE_OS_WINRT
999df562d55Smistachkin { "CreateEventExW", (SYSCALL)CreateEventExW, 0 },
100075b70a20Smistachkin #else
1001df562d55Smistachkin { "CreateEventExW", (SYSCALL)0, 0 },
100275b70a20Smistachkin #endif
100375b70a20Smistachkin
1004df562d55Smistachkin #define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
1005e8f91053Smistachkin DWORD,DWORD))aSyscall[62].pCurrent)
1006df562d55Smistachkin
1007df562d55Smistachkin #if !SQLITE_OS_WINRT
100875b70a20Smistachkin { "WaitForSingleObject", (SYSCALL)WaitForSingleObject, 0 },
1009df562d55Smistachkin #else
1010df562d55Smistachkin { "WaitForSingleObject", (SYSCALL)0, 0 },
1011df562d55Smistachkin #endif
101275b70a20Smistachkin
101375b70a20Smistachkin #define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
1014e8f91053Smistachkin DWORD))aSyscall[63].pCurrent)
101575b70a20Smistachkin
101692c2e0daSmistachkin #if !SQLITE_OS_WINCE
101775b70a20Smistachkin { "WaitForSingleObjectEx", (SYSCALL)WaitForSingleObjectEx, 0 },
101892c2e0daSmistachkin #else
101992c2e0daSmistachkin { "WaitForSingleObjectEx", (SYSCALL)0, 0 },
102092c2e0daSmistachkin #endif
102175b70a20Smistachkin
10225cfbeac0Smistachkin #define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
1023e8f91053Smistachkin BOOL))aSyscall[64].pCurrent)
10245cfbeac0Smistachkin
1025a0aa13b6Smistachkin #if SQLITE_OS_WINRT
102675b70a20Smistachkin { "SetFilePointerEx", (SYSCALL)SetFilePointerEx, 0 },
102775b70a20Smistachkin #else
102875b70a20Smistachkin { "SetFilePointerEx", (SYSCALL)0, 0 },
102975b70a20Smistachkin #endif
103075b70a20Smistachkin
10315cfbeac0Smistachkin #define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
1032e8f91053Smistachkin PLARGE_INTEGER,DWORD))aSyscall[65].pCurrent)
10335cfbeac0Smistachkin
1034401e9153Smistachkin #if SQLITE_OS_WINRT
103575b70a20Smistachkin { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
103675b70a20Smistachkin #else
103775b70a20Smistachkin { "GetFileInformationByHandleEx", (SYSCALL)0, 0 },
103875b70a20Smistachkin #endif
103975b70a20Smistachkin
10405cfbeac0Smistachkin #define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
1041e8f91053Smistachkin FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent)
10425cfbeac0Smistachkin
104373767829Smistachkin #if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
10441e6eea95Smistachkin { "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 },
1045a749486eSmistachkin #else
10461e6eea95Smistachkin { "MapViewOfFileFromApp", (SYSCALL)0, 0 },
1047a749486eSmistachkin #endif
1048287a48d6Smistachkin
10491e6eea95Smistachkin #define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
1050e8f91053Smistachkin SIZE_T))aSyscall[67].pCurrent)
1051287a48d6Smistachkin
1052df562d55Smistachkin #if SQLITE_OS_WINRT
10535483f772Smistachkin { "CreateFile2", (SYSCALL)CreateFile2, 0 },
10545483f772Smistachkin #else
10555483f772Smistachkin { "CreateFile2", (SYSCALL)0, 0 },
10565483f772Smistachkin #endif
10575483f772Smistachkin
10585483f772Smistachkin #define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
1059e8f91053Smistachkin LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[68].pCurrent)
10605483f772Smistachkin
106108c1c315Smistachkin #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION)
10625483f772Smistachkin { "LoadPackagedLibrary", (SYSCALL)LoadPackagedLibrary, 0 },
10635483f772Smistachkin #else
10645483f772Smistachkin { "LoadPackagedLibrary", (SYSCALL)0, 0 },
10655483f772Smistachkin #endif
10665483f772Smistachkin
10675483f772Smistachkin #define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
1068e8f91053Smistachkin DWORD))aSyscall[69].pCurrent)
10695483f772Smistachkin
1070df562d55Smistachkin #if SQLITE_OS_WINRT
1071df562d55Smistachkin { "GetTickCount64", (SYSCALL)GetTickCount64, 0 },
1072df562d55Smistachkin #else
1073df562d55Smistachkin { "GetTickCount64", (SYSCALL)0, 0 },
1074df562d55Smistachkin #endif
1075df562d55Smistachkin
1076e8f91053Smistachkin #define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[70].pCurrent)
1077df562d55Smistachkin
1078df562d55Smistachkin #if SQLITE_OS_WINRT
1079df562d55Smistachkin { "GetNativeSystemInfo", (SYSCALL)GetNativeSystemInfo, 0 },
1080df562d55Smistachkin #else
1081df562d55Smistachkin { "GetNativeSystemInfo", (SYSCALL)0, 0 },
1082df562d55Smistachkin #endif
1083df562d55Smistachkin
1084df562d55Smistachkin #define osGetNativeSystemInfo ((VOID(WINAPI*)( \
1085e8f91053Smistachkin LPSYSTEM_INFO))aSyscall[71].pCurrent)
1086df562d55Smistachkin
1087f4f327ceSmistachkin #if defined(SQLITE_WIN32_HAS_ANSI)
1088f4f327ceSmistachkin { "OutputDebugStringA", (SYSCALL)OutputDebugStringA, 0 },
1089f4f327ceSmistachkin #else
1090f4f327ceSmistachkin { "OutputDebugStringA", (SYSCALL)0, 0 },
1091f4f327ceSmistachkin #endif
1092f4f327ceSmistachkin
1093e8f91053Smistachkin #define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[72].pCurrent)
1094f4f327ceSmistachkin
1095f4f327ceSmistachkin #if defined(SQLITE_WIN32_HAS_WIDE)
1096f4f327ceSmistachkin { "OutputDebugStringW", (SYSCALL)OutputDebugStringW, 0 },
1097f4f327ceSmistachkin #else
1098f4f327ceSmistachkin { "OutputDebugStringW", (SYSCALL)0, 0 },
1099f4f327ceSmistachkin #endif
1100f4f327ceSmistachkin
1101e8f91053Smistachkin #define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[73].pCurrent)
1102f4f327ceSmistachkin
11030df898e2Smistachkin { "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 },
11040df898e2Smistachkin
1105e8f91053Smistachkin #define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent)
11060df898e2Smistachkin
110773767829Smistachkin #if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
11081e6eea95Smistachkin { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
11091e6eea95Smistachkin #else
11101e6eea95Smistachkin { "CreateFileMappingFromApp", (SYSCALL)0, 0 },
11111e6eea95Smistachkin #endif
11121e6eea95Smistachkin
11131e6eea95Smistachkin #define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
1114e8f91053Smistachkin LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
11151e6eea95Smistachkin
111617835a54Smistachkin /*
111717835a54Smistachkin ** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
111817835a54Smistachkin ** is really just a macro that uses a compiler intrinsic (e.g. x64).
11192abe6a28Sdrh ** So do not try to make this is into a redefinable interface.
112017835a54Smistachkin */
112191d12497Smistachkin #if defined(InterlockedCompareExchange)
112291d12497Smistachkin { "InterlockedCompareExchange", (SYSCALL)0, 0 },
112391d12497Smistachkin
112417835a54Smistachkin #define osInterlockedCompareExchange InterlockedCompareExchange
112591d12497Smistachkin #else
1126202cb641Smistachkin { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 },
1127202cb641Smistachkin
1128ce64d610Smistachkin #define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG \
1129ce64d610Smistachkin SQLITE_WIN32_VOLATILE*, LONG,LONG))aSyscall[76].pCurrent)
113091d12497Smistachkin #endif /* defined(InterlockedCompareExchange) */
1131202cb641Smistachkin
1132e45e0fb2Smistachkin #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
1133e45e0fb2Smistachkin { "UuidCreate", (SYSCALL)UuidCreate, 0 },
1134e45e0fb2Smistachkin #else
1135e45e0fb2Smistachkin { "UuidCreate", (SYSCALL)0, 0 },
1136e45e0fb2Smistachkin #endif
1137e45e0fb2Smistachkin
1138e45e0fb2Smistachkin #define osUuidCreate ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[77].pCurrent)
1139e45e0fb2Smistachkin
1140e45e0fb2Smistachkin #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
1141e45e0fb2Smistachkin { "UuidCreateSequential", (SYSCALL)UuidCreateSequential, 0 },
1142e45e0fb2Smistachkin #else
1143e45e0fb2Smistachkin { "UuidCreateSequential", (SYSCALL)0, 0 },
1144e45e0fb2Smistachkin #endif
1145e45e0fb2Smistachkin
1146e45e0fb2Smistachkin #define osUuidCreateSequential \
1147e45e0fb2Smistachkin ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[78].pCurrent)
1148e45e0fb2Smistachkin
1149ccb43714Smistachkin #if !defined(SQLITE_NO_SYNC) && SQLITE_MAX_MMAP_SIZE>0
1150ccb43714Smistachkin { "FlushViewOfFile", (SYSCALL)FlushViewOfFile, 0 },
1151ccb43714Smistachkin #else
1152ccb43714Smistachkin { "FlushViewOfFile", (SYSCALL)0, 0 },
1153ccb43714Smistachkin #endif
1154ccb43714Smistachkin
1155ccb43714Smistachkin #define osFlushViewOfFile \
1156ccb43714Smistachkin ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent)
1157ccb43714Smistachkin
1158318507b7Smistachkin }; /* End of the overrideable system calls */
1159318507b7Smistachkin
1160318507b7Smistachkin /*
1161318507b7Smistachkin ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
1162318507b7Smistachkin ** "win32" VFSes. Return SQLITE_OK opon successfully updating the
1163318507b7Smistachkin ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
1164318507b7Smistachkin ** system call named zName.
1165318507b7Smistachkin */
winSetSystemCall(sqlite3_vfs * pNotUsed,const char * zName,sqlite3_syscall_ptr pNewFunc)1166318507b7Smistachkin static int winSetSystemCall(
1167318507b7Smistachkin sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
1168318507b7Smistachkin const char *zName, /* Name of system call to override */
1169318507b7Smistachkin sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
1170318507b7Smistachkin ){
1171318507b7Smistachkin unsigned int i;
1172318507b7Smistachkin int rc = SQLITE_NOTFOUND;
1173318507b7Smistachkin
1174318507b7Smistachkin UNUSED_PARAMETER(pNotUsed);
1175318507b7Smistachkin if( zName==0 ){
1176318507b7Smistachkin /* If no zName is given, restore all system calls to their default
1177318507b7Smistachkin ** settings and return NULL
1178318507b7Smistachkin */
1179318507b7Smistachkin rc = SQLITE_OK;
1180318507b7Smistachkin for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
1181318507b7Smistachkin if( aSyscall[i].pDefault ){
1182318507b7Smistachkin aSyscall[i].pCurrent = aSyscall[i].pDefault;
1183318507b7Smistachkin }
1184318507b7Smistachkin }
1185318507b7Smistachkin }else{
1186318507b7Smistachkin /* If zName is specified, operate on only the one system call
1187318507b7Smistachkin ** specified.
1188318507b7Smistachkin */
1189318507b7Smistachkin for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
1190318507b7Smistachkin if( strcmp(zName, aSyscall[i].zName)==0 ){
1191318507b7Smistachkin if( aSyscall[i].pDefault==0 ){
1192318507b7Smistachkin aSyscall[i].pDefault = aSyscall[i].pCurrent;
1193318507b7Smistachkin }
1194318507b7Smistachkin rc = SQLITE_OK;
1195318507b7Smistachkin if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
1196318507b7Smistachkin aSyscall[i].pCurrent = pNewFunc;
1197318507b7Smistachkin break;
1198318507b7Smistachkin }
1199318507b7Smistachkin }
1200318507b7Smistachkin }
1201318507b7Smistachkin return rc;
1202318507b7Smistachkin }
1203318507b7Smistachkin
1204318507b7Smistachkin /*
1205318507b7Smistachkin ** Return the value of a system call. Return NULL if zName is not a
1206318507b7Smistachkin ** recognized system call name. NULL is also returned if the system call
1207318507b7Smistachkin ** is currently undefined.
1208318507b7Smistachkin */
winGetSystemCall(sqlite3_vfs * pNotUsed,const char * zName)1209318507b7Smistachkin static sqlite3_syscall_ptr winGetSystemCall(
1210318507b7Smistachkin sqlite3_vfs *pNotUsed,
1211318507b7Smistachkin const char *zName
1212318507b7Smistachkin ){
1213318507b7Smistachkin unsigned int i;
1214318507b7Smistachkin
1215318507b7Smistachkin UNUSED_PARAMETER(pNotUsed);
1216318507b7Smistachkin for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
1217318507b7Smistachkin if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
1218318507b7Smistachkin }
1219318507b7Smistachkin return 0;
1220318507b7Smistachkin }
1221318507b7Smistachkin
1222318507b7Smistachkin /*
1223318507b7Smistachkin ** Return the name of the first system call after zName. If zName==NULL
1224318507b7Smistachkin ** then return the name of the first system call. Return NULL if zName
1225318507b7Smistachkin ** is the last system call or if zName is not the name of a valid
1226318507b7Smistachkin ** system call.
1227318507b7Smistachkin */
winNextSystemCall(sqlite3_vfs * p,const char * zName)1228318507b7Smistachkin static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
1229318507b7Smistachkin int i = -1;
1230318507b7Smistachkin
1231318507b7Smistachkin UNUSED_PARAMETER(p);
1232318507b7Smistachkin if( zName ){
1233318507b7Smistachkin for(i=0; i<ArraySize(aSyscall)-1; i++){
1234318507b7Smistachkin if( strcmp(zName, aSyscall[i].zName)==0 ) break;
1235318507b7Smistachkin }
1236318507b7Smistachkin }
1237318507b7Smistachkin for(i++; i<ArraySize(aSyscall); i++){
1238318507b7Smistachkin if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
1239318507b7Smistachkin }
1240318507b7Smistachkin return 0;
1241318507b7Smistachkin }
1242318507b7Smistachkin
12437f9d1798Smistachkin #ifdef SQLITE_WIN32_MALLOC
1244318507b7Smistachkin /*
1245e8f91053Smistachkin ** If a Win32 native heap has been configured, this function will attempt to
1246e8f91053Smistachkin ** compact it. Upon success, SQLITE_OK will be returned. Upon failure, one
1247e8f91053Smistachkin ** of SQLITE_NOMEM, SQLITE_ERROR, or SQLITE_NOTFOUND will be returned. The
1248e8f91053Smistachkin ** "pnLargest" argument, if non-zero, will be used to return the size of the
1249e8f91053Smistachkin ** largest committed free block in the heap, in bytes.
1250e8f91053Smistachkin */
sqlite3_win32_compact_heap(LPUINT pnLargest)1251e8f91053Smistachkin int sqlite3_win32_compact_heap(LPUINT pnLargest){
1252e8f91053Smistachkin int rc = SQLITE_OK;
1253e8f91053Smistachkin UINT nLargest = 0;
1254e8f91053Smistachkin HANDLE hHeap;
1255e8f91053Smistachkin
1256e8f91053Smistachkin winMemAssertMagic();
1257e8f91053Smistachkin hHeap = winMemGetHeap();
1258e8f91053Smistachkin assert( hHeap!=0 );
1259e8f91053Smistachkin assert( hHeap!=INVALID_HANDLE_VALUE );
1260e8f91053Smistachkin #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
1261e8f91053Smistachkin assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
1262e8f91053Smistachkin #endif
12635134a817Smistachkin #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
1264e8f91053Smistachkin if( (nLargest=osHeapCompact(hHeap, SQLITE_WIN32_HEAP_FLAGS))==0 ){
1265e8f91053Smistachkin DWORD lastErrno = osGetLastError();
1266e8f91053Smistachkin if( lastErrno==NO_ERROR ){
1267e8f91053Smistachkin sqlite3_log(SQLITE_NOMEM, "failed to HeapCompact (no space), heap=%p",
1268e8f91053Smistachkin (void*)hHeap);
1269fad3039cSmistachkin rc = SQLITE_NOMEM_BKPT;
1270e8f91053Smistachkin }else{
127117bc3ff9Smistachkin sqlite3_log(SQLITE_ERROR, "failed to HeapCompact (%lu), heap=%p",
1272e8f91053Smistachkin osGetLastError(), (void*)hHeap);
1273e8f91053Smistachkin rc = SQLITE_ERROR;
1274e8f91053Smistachkin }
1275e8f91053Smistachkin }
1276e8f91053Smistachkin #else
127717bc3ff9Smistachkin sqlite3_log(SQLITE_NOTFOUND, "failed to HeapCompact, heap=%p",
127817bc3ff9Smistachkin (void*)hHeap);
1279e8f91053Smistachkin rc = SQLITE_NOTFOUND;
1280e8f91053Smistachkin #endif
1281e8f91053Smistachkin if( pnLargest ) *pnLargest = nLargest;
1282e8f91053Smistachkin return rc;
1283e8f91053Smistachkin }
1284e8f91053Smistachkin
1285e8f91053Smistachkin /*
1286e8f91053Smistachkin ** If a Win32 native heap has been configured, this function will attempt to
1287e8f91053Smistachkin ** destroy and recreate it. If the Win32 native heap is not isolated and/or
1288e8f91053Smistachkin ** the sqlite3_memory_used() function does not return zero, SQLITE_BUSY will
1289e8f91053Smistachkin ** be returned and no changes will be made to the Win32 native heap.
1290e8f91053Smistachkin */
sqlite3_win32_reset_heap()1291e8f91053Smistachkin int sqlite3_win32_reset_heap(){
1292e8f91053Smistachkin int rc;
1293067b92baSdrh MUTEX_LOGIC( sqlite3_mutex *pMainMtx; ) /* The main static mutex */
1294e8f91053Smistachkin MUTEX_LOGIC( sqlite3_mutex *pMem; ) /* The memsys static mutex */
1295067b92baSdrh MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )
129697a7e5e6Sdrh MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); )
1297067b92baSdrh sqlite3_mutex_enter(pMainMtx);
1298e8f91053Smistachkin sqlite3_mutex_enter(pMem);
12997f9d1798Smistachkin winMemAssertMagic();
13007f9d1798Smistachkin if( winMemGetHeap()!=NULL && winMemGetOwned() && sqlite3_memory_used()==0 ){
1301e8f91053Smistachkin /*
1302e8f91053Smistachkin ** At this point, there should be no outstanding memory allocations on
1303067b92baSdrh ** the heap. Also, since both the main and memsys locks are currently
1304e8f91053Smistachkin ** being held by us, no other function (i.e. from another thread) should
1305e8f91053Smistachkin ** be able to even access the heap. Attempt to destroy and recreate our
1306e8f91053Smistachkin ** isolated Win32 native heap now.
1307e8f91053Smistachkin */
13087f9d1798Smistachkin assert( winMemGetHeap()!=NULL );
13097f9d1798Smistachkin assert( winMemGetOwned() );
13107f9d1798Smistachkin assert( sqlite3_memory_used()==0 );
1311e8f91053Smistachkin winMemShutdown(winMemGetDataPtr());
13127f9d1798Smistachkin assert( winMemGetHeap()==NULL );
13137f9d1798Smistachkin assert( !winMemGetOwned() );
13147f9d1798Smistachkin assert( sqlite3_memory_used()==0 );
13152eb0966eSmistachkin rc = winMemInit(winMemGetDataPtr());
13167f9d1798Smistachkin assert( rc!=SQLITE_OK || winMemGetHeap()!=NULL );
13177f9d1798Smistachkin assert( rc!=SQLITE_OK || winMemGetOwned() );
13187f9d1798Smistachkin assert( rc!=SQLITE_OK || sqlite3_memory_used()==0 );
1319e8f91053Smistachkin }else{
1320e8f91053Smistachkin /*
1321e8f91053Smistachkin ** The Win32 native heap cannot be modified because it may be in use.
1322e8f91053Smistachkin */
1323e8f91053Smistachkin rc = SQLITE_BUSY;
1324e8f91053Smistachkin }
1325e8f91053Smistachkin sqlite3_mutex_leave(pMem);
1326067b92baSdrh sqlite3_mutex_leave(pMainMtx);
1327e8f91053Smistachkin return rc;
1328e8f91053Smistachkin }
13297f9d1798Smistachkin #endif /* SQLITE_WIN32_MALLOC */
1330e8f91053Smistachkin
1331c0929987Sdrh /*
1332f4f327ceSmistachkin ** This function outputs the specified (ANSI) string to the Win32 debugger
1333f4f327ceSmistachkin ** (if available).
13347acec68aSdrh */
1335f4f327ceSmistachkin
sqlite3_win32_write_debug(const char * zBuf,int nBuf)13367e87eae9Smistachkin void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
1337f4f327ceSmistachkin char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
133816afb9eeSmistachkin int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
1339f4f327ceSmistachkin if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
13405ff72401Smistachkin assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
134112931203Smistachkin #ifdef SQLITE_ENABLE_API_ARMOR
134212931203Smistachkin if( !zBuf ){
134312931203Smistachkin (void)SQLITE_MISUSE_BKPT;
134412931203Smistachkin return;
134512931203Smistachkin }
134612931203Smistachkin #endif
1347f4f327ceSmistachkin #if defined(SQLITE_WIN32_HAS_ANSI)
1348f4f327ceSmistachkin if( nMin>0 ){
1349f4f327ceSmistachkin memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
1350f4f327ceSmistachkin memcpy(zDbgBuf, zBuf, nMin);
1351ca04d8a9Smistachkin osOutputDebugStringA(zDbgBuf);
1352f4f327ceSmistachkin }else{
1353ca04d8a9Smistachkin osOutputDebugStringA(zBuf);
13547acec68aSdrh }
1355f4f327ceSmistachkin #elif defined(SQLITE_WIN32_HAS_WIDE)
1356f4f327ceSmistachkin memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
1357f4f327ceSmistachkin if ( osMultiByteToWideChar(
1358f4f327ceSmistachkin osAreFileApisANSI() ? CP_ACP : CP_OEMCP, 0, zBuf,
13590df898e2Smistachkin nMin, (LPWSTR)zDbgBuf, SQLITE_WIN32_DBG_BUF_SIZE/sizeof(WCHAR))<=0 ){
1360f4f327ceSmistachkin return;
1361f4f327ceSmistachkin }
13620df898e2Smistachkin osOutputDebugStringW((LPCWSTR)zDbgBuf);
13637acec68aSdrh #else
1364f4f327ceSmistachkin if( nMin>0 ){
1365f4f327ceSmistachkin memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
1366f4f327ceSmistachkin memcpy(zDbgBuf, zBuf, nMin);
1367f4f327ceSmistachkin fprintf(stderr, "%s", zDbgBuf);
1368f4f327ceSmistachkin }else{
1369f4f327ceSmistachkin fprintf(stderr, "%s", zBuf);
13707acec68aSdrh }
13717acec68aSdrh #endif
1372f4f327ceSmistachkin }
1373f4f327ceSmistachkin
1374f4f327ceSmistachkin /*
1375f4f327ceSmistachkin ** The following routine suspends the current thread for at least ms
1376f4f327ceSmistachkin ** milliseconds. This is equivalent to the Win32 Sleep() interface.
1377f4f327ceSmistachkin */
1378f4f327ceSmistachkin #if SQLITE_OS_WINRT
1379f4f327ceSmistachkin static HANDLE sleepObj = NULL;
1380f4f327ceSmistachkin #endif
1381f4f327ceSmistachkin
sqlite3_win32_sleep(DWORD milliseconds)1382f4f327ceSmistachkin void sqlite3_win32_sleep(DWORD milliseconds){
1383f4f327ceSmistachkin #if SQLITE_OS_WINRT
1384f4f327ceSmistachkin if ( sleepObj==NULL ){
1385f4f327ceSmistachkin sleepObj = osCreateEventExW(NULL, NULL, CREATE_EVENT_MANUAL_RESET,
1386f4f327ceSmistachkin SYNCHRONIZE);
1387f4f327ceSmistachkin }
1388f4f327ceSmistachkin assert( sleepObj!=NULL );
1389f4f327ceSmistachkin osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE);
1390f4f327ceSmistachkin #else
1391f4f327ceSmistachkin osSleep(milliseconds);
1392f4f327ceSmistachkin #endif
1393f4f327ceSmistachkin }
13947acec68aSdrh
139592c2e0daSmistachkin #if SQLITE_MAX_WORKER_THREADS>0 && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
139692c2e0daSmistachkin SQLITE_THREADSAFE>0
sqlite3Win32Wait(HANDLE hObject)1397b1ac2bc8Smistachkin DWORD sqlite3Win32Wait(HANDLE hObject){
1398b1ac2bc8Smistachkin DWORD rc;
1399b1ac2bc8Smistachkin while( (rc = osWaitForSingleObjectEx(hObject, INFINITE,
1400b1ac2bc8Smistachkin TRUE))==WAIT_IO_COMPLETION ){}
1401b1ac2bc8Smistachkin return rc;
1402b1ac2bc8Smistachkin }
140330c633a0Smistachkin #endif
1404b1ac2bc8Smistachkin
14057acec68aSdrh /*
1406cc78fea4Sdrh ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
1407cc78fea4Sdrh ** or WinCE. Return false (zero) for Win95, Win98, or WinME.
1408c0929987Sdrh **
1409c0929987Sdrh ** Here is an interesting observation: Win95, Win98, and WinME lack
1410c0929987Sdrh ** the LockFileEx() API. But we can still statically link against that
141150daafc7Sshane ** API as long as we don't call it when running Win95/98/ME. A call to
1412c0929987Sdrh ** this routine is used to determine if the host is Win95/98/ME or
1413c0929987Sdrh ** WinNT/2K/XP so that we will know whether or not we can safely call
1414c0929987Sdrh ** the LockFileEx() API.
1415c0929987Sdrh */
1416b8af6a28Smistachkin
1417d5be6f01Smistachkin #if !SQLITE_WIN32_GETVERSIONEX
14180f7e08e0Smistachkin # define osIsNT() (1)
14190f7e08e0Smistachkin #elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
1420b324bc79Smistachkin # define osIsNT() (1)
1421c60941f8Smistachkin #elif !defined(SQLITE_WIN32_HAS_WIDE)
1422b324bc79Smistachkin # define osIsNT() (0)
1423cc78fea4Sdrh #else
1424202cb641Smistachkin # define osIsNT() ((sqlite3_os_type==2) || sqlite3_win32_is_nt())
1425d6918657Smistachkin #endif
1426d6918657Smistachkin
1427d6918657Smistachkin /*
1428d6918657Smistachkin ** This function determines if the machine is running a version of Windows
1429d6918657Smistachkin ** based on the NT kernel.
1430d6918657Smistachkin */
sqlite3_win32_is_nt(void)1431d6918657Smistachkin int sqlite3_win32_is_nt(void){
1432cf4200a1Smistachkin #if SQLITE_OS_WINRT
1433cf4200a1Smistachkin /*
1434cf4200a1Smistachkin ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
1435cf4200a1Smistachkin ** kernel.
1436cf4200a1Smistachkin */
1437cf4200a1Smistachkin return 1;
1438d5be6f01Smistachkin #elif SQLITE_WIN32_GETVERSIONEX
1439202cb641Smistachkin if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
144031753c83Smistachkin #if defined(SQLITE_WIN32_HAS_ANSI)
1441318507b7Smistachkin OSVERSIONINFOA sInfo;
1442c0929987Sdrh sInfo.dwOSVersionInfoSize = sizeof(sInfo);
1443318507b7Smistachkin osGetVersionExA(&sInfo);
1444202cb641Smistachkin osInterlockedCompareExchange(&sqlite3_os_type,
1445202cb641Smistachkin (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
144631753c83Smistachkin #elif defined(SQLITE_WIN32_HAS_WIDE)
144731753c83Smistachkin OSVERSIONINFOW sInfo;
144831753c83Smistachkin sInfo.dwOSVersionInfoSize = sizeof(sInfo);
144931753c83Smistachkin osGetVersionExW(&sInfo);
145031753c83Smistachkin osInterlockedCompareExchange(&sqlite3_os_type,
145131753c83Smistachkin (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
14524eb4fefeSmistachkin #endif
1453c0929987Sdrh }
1454202cb641Smistachkin return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
1455f0740a94Smistachkin #elif SQLITE_TEST
1456f0740a94Smistachkin return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
145791d12497Smistachkin #else
1458cf4200a1Smistachkin /*
1459cf4200a1Smistachkin ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
1460cf4200a1Smistachkin ** deprecated are always assumed to be based on the NT kernel.
1461cf4200a1Smistachkin */
146291d12497Smistachkin return 1;
146391d12497Smistachkin #endif
1464c0929987Sdrh }
1465cc78fea4Sdrh
14661b186a99Smistachkin #ifdef SQLITE_WIN32_MALLOC
14671b186a99Smistachkin /*
14681b186a99Smistachkin ** Allocate nBytes of memory.
14691b186a99Smistachkin */
winMemMalloc(int nBytes)14701b186a99Smistachkin static void *winMemMalloc(int nBytes){
14711b186a99Smistachkin HANDLE hHeap;
1472468690efSmistachkin void *p;
14731b186a99Smistachkin
1474468690efSmistachkin winMemAssertMagic();
1475468690efSmistachkin hHeap = winMemGetHeap();
14761b186a99Smistachkin assert( hHeap!=0 );
14771b186a99Smistachkin assert( hHeap!=INVALID_HANDLE_VALUE );
1478e8c9a184Smistachkin #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
1479318507b7Smistachkin assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
14801b186a99Smistachkin #endif
14811b186a99Smistachkin assert( nBytes>=0 );
1482318507b7Smistachkin p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
1483468690efSmistachkin if( !p ){
1484a9cb5be4Smistachkin sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%lu), heap=%p",
1485318507b7Smistachkin nBytes, osGetLastError(), (void*)hHeap);
1486468690efSmistachkin }
1487468690efSmistachkin return p;
14881b186a99Smistachkin }
14891b186a99Smistachkin
14901b186a99Smistachkin /*
14911b186a99Smistachkin ** Free memory.
14921b186a99Smistachkin */
winMemFree(void * pPrior)14931b186a99Smistachkin static void winMemFree(void *pPrior){
14941b186a99Smistachkin HANDLE hHeap;
14951b186a99Smistachkin
1496468690efSmistachkin winMemAssertMagic();
1497468690efSmistachkin hHeap = winMemGetHeap();
14981b186a99Smistachkin assert( hHeap!=0 );
14991b186a99Smistachkin assert( hHeap!=INVALID_HANDLE_VALUE );
1500e8c9a184Smistachkin #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
1501318507b7Smistachkin assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
15021b186a99Smistachkin #endif
15031b186a99Smistachkin if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
1504318507b7Smistachkin if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
1505a9cb5be4Smistachkin sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%lu), heap=%p",
1506318507b7Smistachkin pPrior, osGetLastError(), (void*)hHeap);
1507468690efSmistachkin }
15081b186a99Smistachkin }
15091b186a99Smistachkin
15101b186a99Smistachkin /*
15111b186a99Smistachkin ** Change the size of an existing memory allocation
15121b186a99Smistachkin */
winMemRealloc(void * pPrior,int nBytes)15131b186a99Smistachkin static void *winMemRealloc(void *pPrior, int nBytes){
15141b186a99Smistachkin HANDLE hHeap;
1515468690efSmistachkin void *p;
15161b186a99Smistachkin
1517468690efSmistachkin winMemAssertMagic();
1518468690efSmistachkin hHeap = winMemGetHeap();
15191b186a99Smistachkin assert( hHeap!=0 );
15201b186a99Smistachkin assert( hHeap!=INVALID_HANDLE_VALUE );
1521e8c9a184Smistachkin #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
1522318507b7Smistachkin assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
15231b186a99Smistachkin #endif
15241b186a99Smistachkin assert( nBytes>=0 );
1525468690efSmistachkin if( !pPrior ){
1526318507b7Smistachkin p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
1527468690efSmistachkin }else{
1528318507b7Smistachkin p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
1529468690efSmistachkin }
1530468690efSmistachkin if( !p ){
1531a9cb5be4Smistachkin sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%lu), heap=%p",
1532318507b7Smistachkin pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
1533468690efSmistachkin (void*)hHeap);
1534468690efSmistachkin }
1535468690efSmistachkin return p;
15361b186a99Smistachkin }
15371b186a99Smistachkin
15381b186a99Smistachkin /*
15391b186a99Smistachkin ** Return the size of an outstanding allocation, in bytes.
15401b186a99Smistachkin */
winMemSize(void * p)15411b186a99Smistachkin static int winMemSize(void *p){
15421b186a99Smistachkin HANDLE hHeap;
15431b186a99Smistachkin SIZE_T n;
15441b186a99Smistachkin
1545468690efSmistachkin winMemAssertMagic();
1546468690efSmistachkin hHeap = winMemGetHeap();
15471b186a99Smistachkin assert( hHeap!=0 );
15481b186a99Smistachkin assert( hHeap!=INVALID_HANDLE_VALUE );
1549e8c9a184Smistachkin #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
1550055f1654Smistachkin assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, p) );
15511b186a99Smistachkin #endif
15521b186a99Smistachkin if( !p ) return 0;
1553318507b7Smistachkin n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
1554468690efSmistachkin if( n==(SIZE_T)-1 ){
1555a9cb5be4Smistachkin sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%lu), heap=%p",
1556318507b7Smistachkin p, osGetLastError(), (void*)hHeap);
1557468690efSmistachkin return 0;
1558468690efSmistachkin }
15591b186a99Smistachkin return (int)n;
15601b186a99Smistachkin }
15611b186a99Smistachkin
15621b186a99Smistachkin /*
15631b186a99Smistachkin ** Round up a request size to the next valid allocation size.
15641b186a99Smistachkin */
winMemRoundup(int n)15651b186a99Smistachkin static int winMemRoundup(int n){
15661b186a99Smistachkin return n;
15671b186a99Smistachkin }
15681b186a99Smistachkin
15691b186a99Smistachkin /*
15701b186a99Smistachkin ** Initialize this module.
15711b186a99Smistachkin */
winMemInit(void * pAppData)15721b186a99Smistachkin static int winMemInit(void *pAppData){
15731b186a99Smistachkin winMemData *pWinMemData = (winMemData *)pAppData;
15741b186a99Smistachkin
15751b186a99Smistachkin if( !pWinMemData ) return SQLITE_ERROR;
1576e8f91053Smistachkin assert( pWinMemData->magic1==WINMEM_MAGIC1 );
1577e8f91053Smistachkin assert( pWinMemData->magic2==WINMEM_MAGIC2 );
15780df898e2Smistachkin
157940c471d9Smistachkin #if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE
15801b186a99Smistachkin if( !pWinMemData->hHeap ){
1581ac1f1045Smistachkin DWORD dwInitialSize = SQLITE_WIN32_HEAP_INIT_SIZE;
1582ac1f1045Smistachkin DWORD dwMaximumSize = (DWORD)sqlite3GlobalConfig.nHeap;
1583ac1f1045Smistachkin if( dwMaximumSize==0 ){
1584ac1f1045Smistachkin dwMaximumSize = SQLITE_WIN32_HEAP_MAX_SIZE;
1585ac1f1045Smistachkin }else if( dwInitialSize>dwMaximumSize ){
1586ac1f1045Smistachkin dwInitialSize = dwMaximumSize;
1587ac1f1045Smistachkin }
1588318507b7Smistachkin pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
1589ac1f1045Smistachkin dwInitialSize, dwMaximumSize);
15901b186a99Smistachkin if( !pWinMemData->hHeap ){
1591468690efSmistachkin sqlite3_log(SQLITE_NOMEM,
1592ac1f1045Smistachkin "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu",
1593ac1f1045Smistachkin osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize,
1594ac1f1045Smistachkin dwMaximumSize);
1595fad3039cSmistachkin return SQLITE_NOMEM_BKPT;
15961b186a99Smistachkin }
15971b186a99Smistachkin pWinMemData->bOwned = TRUE;
15980df898e2Smistachkin assert( pWinMemData->bOwned );
15991b186a99Smistachkin }
16000df898e2Smistachkin #else
16010df898e2Smistachkin pWinMemData->hHeap = osGetProcessHeap();
16020df898e2Smistachkin if( !pWinMemData->hHeap ){
16030df898e2Smistachkin sqlite3_log(SQLITE_NOMEM,
1604a9cb5be4Smistachkin "failed to GetProcessHeap (%lu)", osGetLastError());
1605fad3039cSmistachkin return SQLITE_NOMEM_BKPT;
16060df898e2Smistachkin }
16070df898e2Smistachkin pWinMemData->bOwned = FALSE;
16080df898e2Smistachkin assert( !pWinMemData->bOwned );
16090df898e2Smistachkin #endif
16101b186a99Smistachkin assert( pWinMemData->hHeap!=0 );
16111b186a99Smistachkin assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
1612e8c9a184Smistachkin #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
1613318507b7Smistachkin assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
16141b186a99Smistachkin #endif
16151b186a99Smistachkin return SQLITE_OK;
16161b186a99Smistachkin }
16171b186a99Smistachkin
16181b186a99Smistachkin /*
16191b186a99Smistachkin ** Deinitialize this module.
16201b186a99Smistachkin */
winMemShutdown(void * pAppData)16211b186a99Smistachkin static void winMemShutdown(void *pAppData){
16221b186a99Smistachkin winMemData *pWinMemData = (winMemData *)pAppData;
16231b186a99Smistachkin
16241b186a99Smistachkin if( !pWinMemData ) return;
1625e8f91053Smistachkin assert( pWinMemData->magic1==WINMEM_MAGIC1 );
1626e8f91053Smistachkin assert( pWinMemData->magic2==WINMEM_MAGIC2 );
1627e8f91053Smistachkin
16281b186a99Smistachkin if( pWinMemData->hHeap ){
16291b186a99Smistachkin assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
1630e8c9a184Smistachkin #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
1631318507b7Smistachkin assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
16321b186a99Smistachkin #endif
16331b186a99Smistachkin if( pWinMemData->bOwned ){
1634318507b7Smistachkin if( !osHeapDestroy(pWinMemData->hHeap) ){
1635a9cb5be4Smistachkin sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%lu), heap=%p",
1636318507b7Smistachkin osGetLastError(), (void*)pWinMemData->hHeap);
16371b186a99Smistachkin }
16381b186a99Smistachkin pWinMemData->bOwned = FALSE;
16391b186a99Smistachkin }
16401b186a99Smistachkin pWinMemData->hHeap = NULL;
16411b186a99Smistachkin }
16421b186a99Smistachkin }
16431b186a99Smistachkin
16441b186a99Smistachkin /*
16451b186a99Smistachkin ** Populate the low-level memory allocation function pointers in
16461b186a99Smistachkin ** sqlite3GlobalConfig.m with pointers to the routines in this file. The
16471b186a99Smistachkin ** arguments specify the block of memory to manage.
16481b186a99Smistachkin **
16491b186a99Smistachkin ** This routine is only called by sqlite3_config(), and therefore
16501b186a99Smistachkin ** is not required to be threadsafe (it is not).
16511b186a99Smistachkin */
sqlite3MemGetWin32(void)16521b186a99Smistachkin const sqlite3_mem_methods *sqlite3MemGetWin32(void){
16531b186a99Smistachkin static const sqlite3_mem_methods winMemMethods = {
16541b186a99Smistachkin winMemMalloc,
16551b186a99Smistachkin winMemFree,
16561b186a99Smistachkin winMemRealloc,
16571b186a99Smistachkin winMemSize,
16581b186a99Smistachkin winMemRoundup,
16591b186a99Smistachkin winMemInit,
16601b186a99Smistachkin winMemShutdown,
16611b186a99Smistachkin &win_mem_data
16621b186a99Smistachkin };
16631b186a99Smistachkin return &winMemMethods;
16641b186a99Smistachkin }
16651b186a99Smistachkin
sqlite3MemSetDefault(void)16661b186a99Smistachkin void sqlite3MemSetDefault(void){
16671b186a99Smistachkin sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
16681b186a99Smistachkin }
16691b186a99Smistachkin #endif /* SQLITE_WIN32_MALLOC */
16701b186a99Smistachkin
1671c0929987Sdrh /*
167212931203Smistachkin ** Convert a UTF-8 string to Microsoft Unicode.
1673584c094bSdrh **
167412931203Smistachkin ** Space to hold the returned string is obtained from sqlite3_malloc().
1675c0929987Sdrh */
winUtf8ToUnicode(const char * zText)16765daed673Smistachkin static LPWSTR winUtf8ToUnicode(const char *zText){
1677e3dd8bb5Sdrh int nChar;
16785daed673Smistachkin LPWSTR zWideText;
1679c0929987Sdrh
16805daed673Smistachkin nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
16816ca514b0Smistachkin if( nChar==0 ){
16826ca514b0Smistachkin return 0;
16836ca514b0Smistachkin }
16845daed673Smistachkin zWideText = sqlite3MallocZero( nChar*sizeof(WCHAR) );
16855daed673Smistachkin if( zWideText==0 ){
1686c0929987Sdrh return 0;
1687c0929987Sdrh }
16885daed673Smistachkin nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
1689318507b7Smistachkin nChar);
1690e3dd8bb5Sdrh if( nChar==0 ){
16915daed673Smistachkin sqlite3_free(zWideText);
16925daed673Smistachkin zWideText = 0;
1693c0929987Sdrh }
16945daed673Smistachkin return zWideText;
1695c0929987Sdrh }
1696c0929987Sdrh
1697c0929987Sdrh /*
169812931203Smistachkin ** Convert a Microsoft Unicode string to UTF-8.
169912931203Smistachkin **
170012931203Smistachkin ** Space to hold the returned string is obtained from sqlite3_malloc().
1701c0929987Sdrh */
winUnicodeToUtf8(LPCWSTR zWideText)17025daed673Smistachkin static char *winUnicodeToUtf8(LPCWSTR zWideText){
1703c0929987Sdrh int nByte;
17045daed673Smistachkin char *zText;
1705c0929987Sdrh
17065daed673Smistachkin nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
17076ca514b0Smistachkin if( nByte == 0 ){
17086ca514b0Smistachkin return 0;
17096ca514b0Smistachkin }
17105daed673Smistachkin zText = sqlite3MallocZero( nByte );
17115daed673Smistachkin if( zText==0 ){
1712c0929987Sdrh return 0;
1713c0929987Sdrh }
17145daed673Smistachkin nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
1715c0929987Sdrh 0, 0);
1716c0929987Sdrh if( nByte == 0 ){
17175daed673Smistachkin sqlite3_free(zText);
17185daed673Smistachkin zText = 0;
1719c0929987Sdrh }
17205daed673Smistachkin return zText;
1721c0929987Sdrh }
1722c0929987Sdrh
1723371de5adSdrh /*
172412931203Smistachkin ** Convert an ANSI string to Microsoft Unicode, using the ANSI or OEM
172512931203Smistachkin ** code page.
1726584c094bSdrh **
172712931203Smistachkin ** Space to hold the returned string is obtained from sqlite3_malloc().
1728371de5adSdrh */
winMbcsToUnicode(const char * zText,int useAnsi)17295daed673Smistachkin static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){
1730371de5adSdrh int nByte;
173112931203Smistachkin LPWSTR zMbcsText;
17325daed673Smistachkin int codepage = useAnsi ? CP_ACP : CP_OEMCP;
1733371de5adSdrh
17345daed673Smistachkin nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
1735318507b7Smistachkin 0)*sizeof(WCHAR);
17366ca514b0Smistachkin if( nByte==0 ){
17376ca514b0Smistachkin return 0;
17386ca514b0Smistachkin }
173912931203Smistachkin zMbcsText = sqlite3MallocZero( nByte*sizeof(WCHAR) );
174012931203Smistachkin if( zMbcsText==0 ){
1741371de5adSdrh return 0;
1742371de5adSdrh }
174312931203Smistachkin nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
1744318507b7Smistachkin nByte);
1745371de5adSdrh if( nByte==0 ){
174612931203Smistachkin sqlite3_free(zMbcsText);
174712931203Smistachkin zMbcsText = 0;
1748371de5adSdrh }
174912931203Smistachkin return zMbcsText;
1750371de5adSdrh }
1751371de5adSdrh
1752371de5adSdrh /*
175312931203Smistachkin ** Convert a Microsoft Unicode string to a multi-byte character string,
175412931203Smistachkin ** using the ANSI or OEM code page.
1755584c094bSdrh **
175612931203Smistachkin ** Space to hold the returned string is obtained from sqlite3_malloc().
1757371de5adSdrh */
winUnicodeToMbcs(LPCWSTR zWideText,int useAnsi)17585daed673Smistachkin static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){
1759371de5adSdrh int nByte;
17605daed673Smistachkin char *zText;
17615daed673Smistachkin int codepage = useAnsi ? CP_ACP : CP_OEMCP;
1762371de5adSdrh
17635daed673Smistachkin nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
17646ca514b0Smistachkin if( nByte == 0 ){
17656ca514b0Smistachkin return 0;
17666ca514b0Smistachkin }
17675daed673Smistachkin zText = sqlite3MallocZero( nByte );
17685daed673Smistachkin if( zText==0 ){
1769371de5adSdrh return 0;
1770371de5adSdrh }
17715daed673Smistachkin nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
1772318507b7Smistachkin nByte, 0, 0);
1773371de5adSdrh if( nByte == 0 ){
17745daed673Smistachkin sqlite3_free(zText);
17755daed673Smistachkin zText = 0;
1776371de5adSdrh }
17775daed673Smistachkin return zText;
1778371de5adSdrh }
1779371de5adSdrh
1780371de5adSdrh /*
178112931203Smistachkin ** Convert a multi-byte character string to UTF-8.
178212931203Smistachkin **
178312931203Smistachkin ** Space to hold the returned string is obtained from sqlite3_malloc().
1784371de5adSdrh */
winMbcsToUtf8(const char * zText,int useAnsi)178512931203Smistachkin static char *winMbcsToUtf8(const char *zText, int useAnsi){
178612931203Smistachkin char *zTextUtf8;
1787318507b7Smistachkin LPWSTR zTmpWide;
1788371de5adSdrh
178912931203Smistachkin zTmpWide = winMbcsToUnicode(zText, useAnsi);
1790371de5adSdrh if( zTmpWide==0 ){
1791371de5adSdrh return 0;
1792371de5adSdrh }
179312931203Smistachkin zTextUtf8 = winUnicodeToUtf8(zTmpWide);
17945f075388Smistachkin sqlite3_free(zTmpWide);
179512931203Smistachkin return zTextUtf8;
1796371de5adSdrh }
1797371de5adSdrh
1798371de5adSdrh /*
179912931203Smistachkin ** Convert a UTF-8 string to a multi-byte character string.
180012931203Smistachkin **
180112931203Smistachkin ** Space to hold the returned string is obtained from sqlite3_malloc().
1802371de5adSdrh */
winUtf8ToMbcs(const char * zText,int useAnsi)180312931203Smistachkin static char *winUtf8ToMbcs(const char *zText, int useAnsi){
180412931203Smistachkin char *zTextMbcs;
1805318507b7Smistachkin LPWSTR zTmpWide;
1806371de5adSdrh
180712931203Smistachkin zTmpWide = winUtf8ToUnicode(zText);
1808371de5adSdrh if( zTmpWide==0 ){
1809371de5adSdrh return 0;
1810371de5adSdrh }
181112931203Smistachkin zTextMbcs = winUnicodeToMbcs(zTmpWide, useAnsi);
18125f075388Smistachkin sqlite3_free(zTmpWide);
181312931203Smistachkin return zTextMbcs;
181412931203Smistachkin }
181512931203Smistachkin
181612931203Smistachkin /*
181712931203Smistachkin ** This is a public wrapper for the winUtf8ToUnicode() function.
181812931203Smistachkin */
sqlite3_win32_utf8_to_unicode(const char * zText)181912931203Smistachkin LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText){
182012931203Smistachkin #ifdef SQLITE_ENABLE_API_ARMOR
182112931203Smistachkin if( !zText ){
182212931203Smistachkin (void)SQLITE_MISUSE_BKPT;
182312931203Smistachkin return 0;
182412931203Smistachkin }
182512931203Smistachkin #endif
182612931203Smistachkin #ifndef SQLITE_OMIT_AUTOINIT
182712931203Smistachkin if( sqlite3_initialize() ) return 0;
182812931203Smistachkin #endif
182912931203Smistachkin return winUtf8ToUnicode(zText);
183012931203Smistachkin }
183112931203Smistachkin
183212931203Smistachkin /*
183312931203Smistachkin ** This is a public wrapper for the winUnicodeToUtf8() function.
183412931203Smistachkin */
sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText)183512931203Smistachkin char *sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText){
183612931203Smistachkin #ifdef SQLITE_ENABLE_API_ARMOR
183712931203Smistachkin if( !zWideText ){
183812931203Smistachkin (void)SQLITE_MISUSE_BKPT;
183912931203Smistachkin return 0;
184012931203Smistachkin }
184112931203Smistachkin #endif
184212931203Smistachkin #ifndef SQLITE_OMIT_AUTOINIT
184312931203Smistachkin if( sqlite3_initialize() ) return 0;
184412931203Smistachkin #endif
184512931203Smistachkin return winUnicodeToUtf8(zWideText);
184612931203Smistachkin }
184712931203Smistachkin
184812931203Smistachkin /*
184912931203Smistachkin ** This is a public wrapper for the winMbcsToUtf8() function.
1850371de5adSdrh */
sqlite3_win32_mbcs_to_utf8(const char * zText)18515daed673Smistachkin char *sqlite3_win32_mbcs_to_utf8(const char *zText){
185212931203Smistachkin #ifdef SQLITE_ENABLE_API_ARMOR
185312931203Smistachkin if( !zText ){
185412931203Smistachkin (void)SQLITE_MISUSE_BKPT;
1855371de5adSdrh return 0;
1856371de5adSdrh }
185712931203Smistachkin #endif
185812931203Smistachkin #ifndef SQLITE_OMIT_AUTOINIT
185912931203Smistachkin if( sqlite3_initialize() ) return 0;
186012931203Smistachkin #endif
186112931203Smistachkin return winMbcsToUtf8(zText, osAreFileApisANSI());
18625daed673Smistachkin }
18635daed673Smistachkin
18645daed673Smistachkin /*
186512931203Smistachkin ** This is a public wrapper for the winMbcsToUtf8() function.
18665daed673Smistachkin */
sqlite3_win32_mbcs_to_utf8_v2(const char * zText,int useAnsi)186712931203Smistachkin char *sqlite3_win32_mbcs_to_utf8_v2(const char *zText, int useAnsi){
186812931203Smistachkin #ifdef SQLITE_ENABLE_API_ARMOR
186912931203Smistachkin if( !zText ){
187012931203Smistachkin (void)SQLITE_MISUSE_BKPT;
18715daed673Smistachkin return 0;
18725daed673Smistachkin }
187312931203Smistachkin #endif
187412931203Smistachkin #ifndef SQLITE_OMIT_AUTOINIT
187512931203Smistachkin if( sqlite3_initialize() ) return 0;
187612931203Smistachkin #endif
187712931203Smistachkin return winMbcsToUtf8(zText, useAnsi);
18785daed673Smistachkin }
18795daed673Smistachkin
18805daed673Smistachkin /*
188112931203Smistachkin ** This is a public wrapper for the winUtf8ToMbcs() function.
1882371de5adSdrh */
sqlite3_win32_utf8_to_mbcs(const char * zText)18835daed673Smistachkin char *sqlite3_win32_utf8_to_mbcs(const char *zText){
188412931203Smistachkin #ifdef SQLITE_ENABLE_API_ARMOR
188512931203Smistachkin if( !zText ){
188612931203Smistachkin (void)SQLITE_MISUSE_BKPT;
1887371de5adSdrh return 0;
1888371de5adSdrh }
188912931203Smistachkin #endif
189012931203Smistachkin #ifndef SQLITE_OMIT_AUTOINIT
189112931203Smistachkin if( sqlite3_initialize() ) return 0;
189212931203Smistachkin #endif
189312931203Smistachkin return winUtf8ToMbcs(zText, osAreFileApisANSI());
18945daed673Smistachkin }
18955daed673Smistachkin
18965daed673Smistachkin /*
189712931203Smistachkin ** This is a public wrapper for the winUtf8ToMbcs() function.
18985daed673Smistachkin */
sqlite3_win32_utf8_to_mbcs_v2(const char * zText,int useAnsi)189912931203Smistachkin char *sqlite3_win32_utf8_to_mbcs_v2(const char *zText, int useAnsi){
190012931203Smistachkin #ifdef SQLITE_ENABLE_API_ARMOR
190112931203Smistachkin if( !zText ){
190212931203Smistachkin (void)SQLITE_MISUSE_BKPT;
19035daed673Smistachkin return 0;
19045daed673Smistachkin }
190512931203Smistachkin #endif
190612931203Smistachkin #ifndef SQLITE_OMIT_AUTOINIT
190712931203Smistachkin if( sqlite3_initialize() ) return 0;
190812931203Smistachkin #endif
190912931203Smistachkin return winUtf8ToMbcs(zText, useAnsi);
1910371de5adSdrh }
1911371de5adSdrh
191261540688Smistachkin /*
191307430a8cSmistachkin ** This function is the same as sqlite3_win32_set_directory (below); however,
191407430a8cSmistachkin ** it accepts a UTF-8 string.
191561540688Smistachkin */
sqlite3_win32_set_directory8(unsigned long type,const char * zValue)191607430a8cSmistachkin int sqlite3_win32_set_directory8(
191795d5ae19Smistachkin unsigned long type, /* Identifier for directory being set or reset */
191807430a8cSmistachkin const char *zValue /* New value for directory being set or reset */
191995d5ae19Smistachkin ){
192061540688Smistachkin char **ppDirectory = 0;
19216e440ab3Sdrh int rc;
192261540688Smistachkin #ifndef SQLITE_OMIT_AUTOINIT
19236e440ab3Sdrh rc = sqlite3_initialize();
192461540688Smistachkin if( rc ) return rc;
192561540688Smistachkin #endif
192618a3a48dSdrh sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
192761540688Smistachkin if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
192861540688Smistachkin ppDirectory = &sqlite3_data_directory;
192961540688Smistachkin }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){
193061540688Smistachkin ppDirectory = &sqlite3_temp_directory;
193161540688Smistachkin }
193261540688Smistachkin assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE
193361540688Smistachkin || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE
193461540688Smistachkin );
1935484dbef5Smistachkin assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
193661540688Smistachkin if( ppDirectory ){
193707430a8cSmistachkin char *zCopy = 0;
193807430a8cSmistachkin if( zValue && zValue[0] ){
193907430a8cSmistachkin zCopy = sqlite3_mprintf("%s", zValue);
194007430a8cSmistachkin if ( zCopy==0 ){
194118a3a48dSdrh rc = SQLITE_NOMEM_BKPT;
194218a3a48dSdrh goto set_directory8_done;
194361540688Smistachkin }
194461540688Smistachkin }
194561540688Smistachkin sqlite3_free(*ppDirectory);
194607430a8cSmistachkin *ppDirectory = zCopy;
194718a3a48dSdrh rc = SQLITE_OK;
194818a3a48dSdrh }else{
194918a3a48dSdrh rc = SQLITE_ERROR;
195061540688Smistachkin }
195118a3a48dSdrh set_directory8_done:
195218a3a48dSdrh sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
195318a3a48dSdrh return rc;
195461540688Smistachkin }
195550990dbbSdrh
195650990dbbSdrh /*
195707430a8cSmistachkin ** This function is the same as sqlite3_win32_set_directory (below); however,
195807430a8cSmistachkin ** it accepts a UTF-16 string.
195907430a8cSmistachkin */
sqlite3_win32_set_directory16(unsigned long type,const void * zValue)196007430a8cSmistachkin int sqlite3_win32_set_directory16(
196107430a8cSmistachkin unsigned long type, /* Identifier for directory being set or reset */
196207430a8cSmistachkin const void *zValue /* New value for directory being set or reset */
196307430a8cSmistachkin ){
196407430a8cSmistachkin int rc;
196507430a8cSmistachkin char *zUtf8 = 0;
196607430a8cSmistachkin if( zValue ){
196707430a8cSmistachkin zUtf8 = sqlite3_win32_unicode_to_utf8(zValue);
196807430a8cSmistachkin if( zUtf8==0 ) return SQLITE_NOMEM_BKPT;
196907430a8cSmistachkin }
197007430a8cSmistachkin rc = sqlite3_win32_set_directory8(type, zUtf8);
197107430a8cSmistachkin if( zUtf8 ) sqlite3_free(zUtf8);
197207430a8cSmistachkin return rc;
197307430a8cSmistachkin }
197407430a8cSmistachkin
197507430a8cSmistachkin /*
197607430a8cSmistachkin ** This function sets the data directory or the temporary directory based on
197707430a8cSmistachkin ** the provided arguments. The type argument must be 1 in order to set the
197807430a8cSmistachkin ** data directory or 2 in order to set the temporary directory. The zValue
197907430a8cSmistachkin ** argument is the name of the directory to use. The return value will be
198007430a8cSmistachkin ** SQLITE_OK if successful.
198107430a8cSmistachkin */
sqlite3_win32_set_directory(unsigned long type,void * zValue)198207430a8cSmistachkin int sqlite3_win32_set_directory(
198307430a8cSmistachkin unsigned long type, /* Identifier for directory being set or reset */
198407430a8cSmistachkin void *zValue /* New value for directory being set or reset */
198507430a8cSmistachkin ){
198607430a8cSmistachkin return sqlite3_win32_set_directory16(type, zValue);
198707430a8cSmistachkin }
198807430a8cSmistachkin
198907430a8cSmistachkin /*
1990b324bc79Smistachkin ** The return value of winGetLastErrorMsg
199150990dbbSdrh ** is zero if the error message fits in the buffer, or non-zero
199250990dbbSdrh ** otherwise (if the message was truncated).
199350990dbbSdrh */
winGetLastErrorMsg(DWORD lastErrno,int nBuf,char * zBuf)1994b324bc79Smistachkin static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
199550990dbbSdrh /* FormatMessage returns 0 on failure. Otherwise it
199650990dbbSdrh ** returns the number of TCHARs written to the output
199750990dbbSdrh ** buffer, excluding the terminating null char.
199850990dbbSdrh */
199950990dbbSdrh DWORD dwLen = 0;
200050990dbbSdrh char *zOut = 0;
200150990dbbSdrh
2002b324bc79Smistachkin if( osIsNT() ){
200362d1924fSmistachkin #if SQLITE_OS_WINRT
200431706a2dSmistachkin WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1];
200562d1924fSmistachkin dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
200662d1924fSmistachkin FORMAT_MESSAGE_IGNORE_INSERTS,
200762d1924fSmistachkin NULL,
200862d1924fSmistachkin lastErrno,
200962d1924fSmistachkin 0,
201062d1924fSmistachkin zTempWide,
201131706a2dSmistachkin SQLITE_WIN32_MAX_ERRMSG_CHARS,
201262d1924fSmistachkin 0);
201362d1924fSmistachkin #else
2014318507b7Smistachkin LPWSTR zTempWide = NULL;
2015318507b7Smistachkin dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
2016a9664a14Smistachkin FORMAT_MESSAGE_FROM_SYSTEM |
2017a9664a14Smistachkin FORMAT_MESSAGE_IGNORE_INSERTS,
201850990dbbSdrh NULL,
2019a9664a14Smistachkin lastErrno,
202050990dbbSdrh 0,
202150990dbbSdrh (LPWSTR) &zTempWide,
202250990dbbSdrh 0,
202350990dbbSdrh 0);
202462d1924fSmistachkin #endif
202550990dbbSdrh if( dwLen > 0 ){
202650990dbbSdrh /* allocate a buffer and convert to UTF8 */
20276c3c1a09Smistachkin sqlite3BeginBenignMalloc();
2028b324bc79Smistachkin zOut = winUnicodeToUtf8(zTempWide);
20296c3c1a09Smistachkin sqlite3EndBenignMalloc();
203062d1924fSmistachkin #if !SQLITE_OS_WINRT
203150990dbbSdrh /* free the system buffer allocated by FormatMessage */
2032318507b7Smistachkin osLocalFree(zTempWide);
203362d1924fSmistachkin #endif
203450990dbbSdrh }
2035d52ee729Sdrh }
2036d52ee729Sdrh #ifdef SQLITE_WIN32_HAS_ANSI
2037d52ee729Sdrh else{
203850990dbbSdrh char *zTemp = NULL;
2039318507b7Smistachkin dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
2040a9664a14Smistachkin FORMAT_MESSAGE_FROM_SYSTEM |
2041a9664a14Smistachkin FORMAT_MESSAGE_IGNORE_INSERTS,
204250990dbbSdrh NULL,
2043a9664a14Smistachkin lastErrno,
204450990dbbSdrh 0,
204550990dbbSdrh (LPSTR) &zTemp,
204650990dbbSdrh 0,
204750990dbbSdrh 0);
204850990dbbSdrh if( dwLen > 0 ){
204950990dbbSdrh /* allocate a buffer and convert to UTF8 */
20506c3c1a09Smistachkin sqlite3BeginBenignMalloc();
205112931203Smistachkin zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
20526c3c1a09Smistachkin sqlite3EndBenignMalloc();
205350990dbbSdrh /* free the system buffer allocated by FormatMessage */
2054318507b7Smistachkin osLocalFree(zTemp);
205550990dbbSdrh }
205650990dbbSdrh }
2057d52ee729Sdrh #endif
205850990dbbSdrh if( 0 == dwLen ){
2059e84d8d32Smistachkin sqlite3_snprintf(nBuf, zBuf, "OsError 0x%lx (%lu)", lastErrno, lastErrno);
206050990dbbSdrh }else{
206150990dbbSdrh /* copy a maximum of nBuf chars to output buffer */
206250990dbbSdrh sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
206350990dbbSdrh /* free the UTF8 buffer */
20645f075388Smistachkin sqlite3_free(zOut);
206550990dbbSdrh }
206650990dbbSdrh return 0;
206750990dbbSdrh }
206850990dbbSdrh
206950990dbbSdrh /*
207050990dbbSdrh **
207150990dbbSdrh ** This function - winLogErrorAtLine() - is only ever called via the macro
207250990dbbSdrh ** winLogError().
207350990dbbSdrh **
207450990dbbSdrh ** This routine is invoked after an error occurs in an OS function.
207550990dbbSdrh ** It logs a message using sqlite3_log() containing the current value of
207650990dbbSdrh ** error code and, if possible, the human-readable equivalent from
207750990dbbSdrh ** FormatMessage.
207850990dbbSdrh **
207950990dbbSdrh ** The first argument passed to the macro should be the error code that
208050990dbbSdrh ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
208150990dbbSdrh ** The two subsequent arguments should be the name of the OS function that
2082d5578433Smistachkin ** failed and the associated file-system path, if any.
208350990dbbSdrh */
20842aef997cSmistachkin #define winLogError(a,b,c,d) winLogErrorAtLine(a,b,c,d,__LINE__)
winLogErrorAtLine(int errcode,DWORD lastErrno,const char * zFunc,const char * zPath,int iLine)208550990dbbSdrh static int winLogErrorAtLine(
208650990dbbSdrh int errcode, /* SQLite error code */
20872aef997cSmistachkin DWORD lastErrno, /* Win32 last error */
208850990dbbSdrh const char *zFunc, /* Name of OS function that failed */
208950990dbbSdrh const char *zPath, /* File path associated with error */
209050990dbbSdrh int iLine /* Source line number where error occurred */
209150990dbbSdrh ){
209250990dbbSdrh char zMsg[500]; /* Human readable error text */
20935f2dfdbcSdrh int i; /* Loop counter */
209450990dbbSdrh
209550990dbbSdrh zMsg[0] = 0;
2096b324bc79Smistachkin winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
209750990dbbSdrh assert( errcode!=SQLITE_OK );
209850990dbbSdrh if( zPath==0 ) zPath = "";
20995f2dfdbcSdrh for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
21005f2dfdbcSdrh zMsg[i] = 0;
210150990dbbSdrh sqlite3_log(errcode,
2102e84d8d32Smistachkin "os_win.c:%d: (%lu) %s(%s) - %s",
21032aef997cSmistachkin iLine, lastErrno, zFunc, zPath, zMsg
210450990dbbSdrh );
210550990dbbSdrh
210650990dbbSdrh return errcode;
210750990dbbSdrh }
210850990dbbSdrh
21095d9ef0a6Sdrh /*
211052564d70Sdrh ** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
211152564d70Sdrh ** will be retried following a locking error - probably caused by
211252564d70Sdrh ** antivirus software. Also the initial delay before the first retry.
211352564d70Sdrh ** The delay increases linearly with each retry.
21145d9ef0a6Sdrh */
21155d9ef0a6Sdrh #ifndef SQLITE_WIN32_IOERR_RETRY
211652564d70Sdrh # define SQLITE_WIN32_IOERR_RETRY 10
211752564d70Sdrh #endif
211852564d70Sdrh #ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
211952564d70Sdrh # define SQLITE_WIN32_IOERR_RETRY_DELAY 25
21205d9ef0a6Sdrh #endif
2121b324bc79Smistachkin static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
2122b324bc79Smistachkin static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
21235d9ef0a6Sdrh
21245d9ef0a6Sdrh /*
2125491451deSmistachkin ** The "winIoerrCanRetry1" macro is used to determine if a particular I/O
2126491451deSmistachkin ** error code obtained via GetLastError() is eligible to be retried. It
2127491451deSmistachkin ** must accept the error code DWORD as its only argument and should return
2128491451deSmistachkin ** non-zero if the error code is transient in nature and the operation
2129491451deSmistachkin ** responsible for generating the original error might succeed upon being
2130491451deSmistachkin ** retried. The argument to this macro should be a variable.
2131491451deSmistachkin **
2132491451deSmistachkin ** Additionally, a macro named "winIoerrCanRetry2" may be defined. If it
2133491451deSmistachkin ** is defined, it will be consulted only when the macro "winIoerrCanRetry1"
2134491451deSmistachkin ** returns zero. The "winIoerrCanRetry2" macro is completely optional and
2135491451deSmistachkin ** may be used to include additional error codes in the set that should
2136491451deSmistachkin ** result in the failing I/O operation being retried by the caller. If
2137491451deSmistachkin ** defined, the "winIoerrCanRetry2" macro must exhibit external semantics
2138491451deSmistachkin ** identical to those of the "winIoerrCanRetry1" macro.
2139491451deSmistachkin */
2140491451deSmistachkin #if !defined(winIoerrCanRetry1)
2141491451deSmistachkin #define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED) || \
2142491451deSmistachkin ((a)==ERROR_SHARING_VIOLATION) || \
2143491451deSmistachkin ((a)==ERROR_LOCK_VIOLATION) || \
2144491451deSmistachkin ((a)==ERROR_DEV_NOT_EXIST) || \
2145491451deSmistachkin ((a)==ERROR_NETNAME_DELETED) || \
2146491451deSmistachkin ((a)==ERROR_SEM_TIMEOUT) || \
2147491451deSmistachkin ((a)==ERROR_NETWORK_UNREACHABLE))
2148491451deSmistachkin #endif
2149491451deSmistachkin
2150491451deSmistachkin /*
21515d9ef0a6Sdrh ** If a ReadFile() or WriteFile() error occurs, invoke this routine
21525d9ef0a6Sdrh ** to see if it should be retried. Return TRUE to retry. Return FALSE
21535d9ef0a6Sdrh ** to give up with an error.
21545d9ef0a6Sdrh */
winRetryIoerr(int * pnRetry,DWORD * pError)2155b324bc79Smistachkin static int winRetryIoerr(int *pnRetry, DWORD *pError){
2156d1ef9b6dSmistachkin DWORD e = osGetLastError();
2157b324bc79Smistachkin if( *pnRetry>=winIoerrRetry ){
2158d1ef9b6dSmistachkin if( pError ){
2159d1ef9b6dSmistachkin *pError = e;
2160d1ef9b6dSmistachkin }
21615d9ef0a6Sdrh return 0;
21625d9ef0a6Sdrh }
2163491451deSmistachkin if( winIoerrCanRetry1(e) ){
2164b324bc79Smistachkin sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
21655d9ef0a6Sdrh ++*pnRetry;
21665d9ef0a6Sdrh return 1;
21675d9ef0a6Sdrh }
2168491451deSmistachkin #if defined(winIoerrCanRetry2)
2169491451deSmistachkin else if( winIoerrCanRetry2(e) ){
2170491451deSmistachkin sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
2171491451deSmistachkin ++*pnRetry;
2172491451deSmistachkin return 1;
2173491451deSmistachkin }
2174491451deSmistachkin #endif
2175d1ef9b6dSmistachkin if( pError ){
2176d1ef9b6dSmistachkin *pError = e;
2177d1ef9b6dSmistachkin }
21785d9ef0a6Sdrh return 0;
21795d9ef0a6Sdrh }
21805d9ef0a6Sdrh
2181a32ad843Sdrh /*
2182a32ad843Sdrh ** Log a I/O error retry episode.
2183a32ad843Sdrh */
winLogIoerr(int nRetry,int lineno)218421aa6a1aSdrh static void winLogIoerr(int nRetry, int lineno){
2185a32ad843Sdrh if( nRetry ){
21868237f6d1Sdrh sqlite3_log(SQLITE_NOTICE,
218721aa6a1aSdrh "delayed %dms for lock/sharing conflict at line %d",
218821aa6a1aSdrh winIoerrRetryDelay*nRetry*(nRetry+1)/2, lineno
2189a32ad843Sdrh );
2190a32ad843Sdrh }
2191a32ad843Sdrh }
2192a32ad843Sdrh
21936cc16fc0Smistachkin /*
21948366ddf2Smistachkin ** This #if does not rely on the SQLITE_OS_WINCE define because the
21958366ddf2Smistachkin ** corresponding section in "date.c" cannot use it.
219672aead81Sdrh */
21970cedb963Smistachkin #if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
21980e188e1dSmistachkin (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
21998366ddf2Smistachkin /*
22008366ddf2Smistachkin ** The MSVC CRT on Windows CE may not have a localtime() function.
22018366ddf2Smistachkin ** So define a substitute.
22028366ddf2Smistachkin */
22030cedb963Smistachkin # include <time.h>
localtime(const time_t * t)220472aead81Sdrh struct tm *__cdecl localtime(const time_t *t)
220572aead81Sdrh {
220672aead81Sdrh static struct tm y;
220772aead81Sdrh FILETIME uTm, lTm;
220872aead81Sdrh SYSTEMTIME pTm;
2209c51250a5Sdrh sqlite3_int64 t64;
221072aead81Sdrh t64 = *t;
221172aead81Sdrh t64 = (t64 + 11644473600)*10000000;
2212d87873d1Sshane uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
2213d87873d1Sshane uTm.dwHighDateTime= (DWORD)(t64 >> 32);
2214318507b7Smistachkin osFileTimeToLocalFileTime(&uTm,&lTm);
2215318507b7Smistachkin osFileTimeToSystemTime(&lTm,&pTm);
221672aead81Sdrh y.tm_year = pTm.wYear - 1900;
221772aead81Sdrh y.tm_mon = pTm.wMonth - 1;
221872aead81Sdrh y.tm_wday = pTm.wDayOfWeek;
221972aead81Sdrh y.tm_mday = pTm.wDay;
222072aead81Sdrh y.tm_hour = pTm.wHour;
222172aead81Sdrh y.tm_min = pTm.wMinute;
222272aead81Sdrh y.tm_sec = pTm.wSecond;
222372aead81Sdrh return &y;
222472aead81Sdrh }
22252bfe1df0Smistachkin #endif
222672aead81Sdrh
22276cc16fc0Smistachkin #if SQLITE_OS_WINCE
22286cc16fc0Smistachkin /*************************************************************************
22296cc16fc0Smistachkin ** This section contains code for WinCE only.
22306cc16fc0Smistachkin */
2231d87873d1Sshane #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
223272aead81Sdrh
223372aead81Sdrh /*
223472aead81Sdrh ** Acquire a lock on the handle h
223572aead81Sdrh */
winceMutexAcquire(HANDLE h)223672aead81Sdrh static void winceMutexAcquire(HANDLE h){
223772aead81Sdrh DWORD dwErr;
223872aead81Sdrh do {
223975b70a20Smistachkin dwErr = osWaitForSingleObject(h, INFINITE);
224072aead81Sdrh } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
224172aead81Sdrh }
224272aead81Sdrh /*
224372aead81Sdrh ** Release a lock acquired by winceMutexAcquire()
224472aead81Sdrh */
224572aead81Sdrh #define winceMutexRelease(h) ReleaseMutex(h)
224672aead81Sdrh
224772aead81Sdrh /*
224872aead81Sdrh ** Create the mutex and shared memory used for locking in the file
224972aead81Sdrh ** descriptor pFile
225072aead81Sdrh */
winceCreateLock(const char * zFilename,winFile * pFile)22517e87eae9Smistachkin static int winceCreateLock(const char *zFilename, winFile *pFile){
2252318507b7Smistachkin LPWSTR zTok;
22535f075388Smistachkin LPWSTR zName;
22547e87eae9Smistachkin DWORD lastErrno;
22557e87eae9Smistachkin BOOL bLogged = FALSE;
225672aead81Sdrh BOOL bInit = TRUE;
225772aead81Sdrh
2258b324bc79Smistachkin zName = winUtf8ToUnicode(zFilename);
22595f075388Smistachkin if( zName==0 ){
22605f075388Smistachkin /* out of memory */
2261fad3039cSmistachkin return SQLITE_IOERR_NOMEM_BKPT;
22625f075388Smistachkin }
22635f075388Smistachkin
226472aead81Sdrh /* Initialize the local lockdata */
2265318507b7Smistachkin memset(&pFile->local, 0, sizeof(pFile->local));
226672aead81Sdrh
226772aead81Sdrh /* Replace the backslashes from the filename and lowercase it
226872aead81Sdrh ** to derive a mutex name. */
2269318507b7Smistachkin zTok = osCharLowerW(zName);
227072aead81Sdrh for (;*zTok;zTok++){
227172aead81Sdrh if (*zTok == '\\') *zTok = '_';
227272aead81Sdrh }
227372aead81Sdrh
227472aead81Sdrh /* Create/open the named mutex */
2275318507b7Smistachkin pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
227672aead81Sdrh if (!pFile->hMutex){
2277318507b7Smistachkin pFile->lastErrno = osGetLastError();
22785f075388Smistachkin sqlite3_free(zName);
22799f11ef12Smistachkin return winLogError(SQLITE_IOERR, pFile->lastErrno,
22809f11ef12Smistachkin "winceCreateLock1", zFilename);
228172aead81Sdrh }
228272aead81Sdrh
228372aead81Sdrh /* Acquire the mutex before continuing */
228472aead81Sdrh winceMutexAcquire(pFile->hMutex);
228572aead81Sdrh
228672aead81Sdrh /* Since the names of named mutexes, semaphores, file mappings etc are
228772aead81Sdrh ** case-sensitive, take advantage of that by uppercasing the mutex name
228872aead81Sdrh ** and using that as the shared filemapping name.
228972aead81Sdrh */
2290318507b7Smistachkin osCharUpperW(zName);
2291318507b7Smistachkin pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
229272aead81Sdrh PAGE_READWRITE, 0, sizeof(winceLock),
229372aead81Sdrh zName);
229472aead81Sdrh
229572aead81Sdrh /* Set a flag that indicates we're the first to create the memory so it
229672aead81Sdrh ** must be zero-initialized */
22977e87eae9Smistachkin lastErrno = osGetLastError();
22987e87eae9Smistachkin if (lastErrno == ERROR_ALREADY_EXISTS){
229972aead81Sdrh bInit = FALSE;
230072aead81Sdrh }
230172aead81Sdrh
23025f075388Smistachkin sqlite3_free(zName);
230372aead81Sdrh
230472aead81Sdrh /* If we succeeded in making the shared memory handle, map it. */
230572aead81Sdrh if( pFile->hShared ){
2306318507b7Smistachkin pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
230772aead81Sdrh FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
230872aead81Sdrh /* If mapping failed, close the shared memory handle and erase it */
230972aead81Sdrh if( !pFile->shared ){
2310318507b7Smistachkin pFile->lastErrno = osGetLastError();
23117e87eae9Smistachkin winLogError(SQLITE_IOERR, pFile->lastErrno,
23122aef997cSmistachkin "winceCreateLock2", zFilename);
23137e87eae9Smistachkin bLogged = TRUE;
2314318507b7Smistachkin osCloseHandle(pFile->hShared);
231572aead81Sdrh pFile->hShared = NULL;
231672aead81Sdrh }
231772aead81Sdrh }
231872aead81Sdrh
231972aead81Sdrh /* If shared memory could not be created, then close the mutex and fail */
232072aead81Sdrh if( pFile->hShared==NULL ){
23217e87eae9Smistachkin if( !bLogged ){
23227e87eae9Smistachkin pFile->lastErrno = lastErrno;
23237e87eae9Smistachkin winLogError(SQLITE_IOERR, pFile->lastErrno,
23247e87eae9Smistachkin "winceCreateLock3", zFilename);
23257e87eae9Smistachkin bLogged = TRUE;
23267e87eae9Smistachkin }
232772aead81Sdrh winceMutexRelease(pFile->hMutex);
2328318507b7Smistachkin osCloseHandle(pFile->hMutex);
232972aead81Sdrh pFile->hMutex = NULL;
23307e87eae9Smistachkin return SQLITE_IOERR;
233172aead81Sdrh }
233272aead81Sdrh
233372aead81Sdrh /* Initialize the shared memory if we're supposed to */
233472aead81Sdrh if( bInit ){
2335318507b7Smistachkin memset(pFile->shared, 0, sizeof(winceLock));
233672aead81Sdrh }
233772aead81Sdrh
233872aead81Sdrh winceMutexRelease(pFile->hMutex);
23397e87eae9Smistachkin return SQLITE_OK;
234072aead81Sdrh }
234172aead81Sdrh
234272aead81Sdrh /*
234372aead81Sdrh ** Destroy the part of winFile that deals with wince locks
234472aead81Sdrh */
winceDestroyLock(winFile * pFile)234572aead81Sdrh static void winceDestroyLock(winFile *pFile){
234672aead81Sdrh if (pFile->hMutex){
234772aead81Sdrh /* Acquire the mutex */
234872aead81Sdrh winceMutexAcquire(pFile->hMutex);
234972aead81Sdrh
235072aead81Sdrh /* The following blocks should probably assert in debug mode, but they
235172aead81Sdrh are to cleanup in case any locks remained open */
235272aead81Sdrh if (pFile->local.nReaders){
235372aead81Sdrh pFile->shared->nReaders --;
235472aead81Sdrh }
235572aead81Sdrh if (pFile->local.bReserved){
235672aead81Sdrh pFile->shared->bReserved = FALSE;
235772aead81Sdrh }
235872aead81Sdrh if (pFile->local.bPending){
235972aead81Sdrh pFile->shared->bPending = FALSE;
236072aead81Sdrh }
236172aead81Sdrh if (pFile->local.bExclusive){
236272aead81Sdrh pFile->shared->bExclusive = FALSE;
236372aead81Sdrh }
236472aead81Sdrh
236572aead81Sdrh /* De-reference and close our copy of the shared memory handle */
2366318507b7Smistachkin osUnmapViewOfFile(pFile->shared);
2367318507b7Smistachkin osCloseHandle(pFile->hShared);
236872aead81Sdrh
236972aead81Sdrh /* Done with the mutex */
237072aead81Sdrh winceMutexRelease(pFile->hMutex);
2371318507b7Smistachkin osCloseHandle(pFile->hMutex);
237272aead81Sdrh pFile->hMutex = NULL;
237372aead81Sdrh }
237472aead81Sdrh }
237572aead81Sdrh
237672aead81Sdrh /*
2377318507b7Smistachkin ** An implementation of the LockFile() API of Windows for CE
237872aead81Sdrh */
winceLockFile(LPHANDLE phFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh)237972aead81Sdrh static BOOL winceLockFile(
2380a749486eSmistachkin LPHANDLE phFile,
238172aead81Sdrh DWORD dwFileOffsetLow,
238272aead81Sdrh DWORD dwFileOffsetHigh,
238372aead81Sdrh DWORD nNumberOfBytesToLockLow,
238472aead81Sdrh DWORD nNumberOfBytesToLockHigh
238572aead81Sdrh ){
238672aead81Sdrh winFile *pFile = HANDLE_TO_WINFILE(phFile);
238772aead81Sdrh BOOL bReturn = FALSE;
238872aead81Sdrh
2389d87873d1Sshane UNUSED_PARAMETER(dwFileOffsetHigh);
2390d87873d1Sshane UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
2391d87873d1Sshane
239272aead81Sdrh if (!pFile->hMutex) return TRUE;
239372aead81Sdrh winceMutexAcquire(pFile->hMutex);
239472aead81Sdrh
239572aead81Sdrh /* Wanting an exclusive lock? */
239611bb41f8Sshane if (dwFileOffsetLow == (DWORD)SHARED_FIRST
239711bb41f8Sshane && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
239872aead81Sdrh if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
239972aead81Sdrh pFile->shared->bExclusive = TRUE;
240072aead81Sdrh pFile->local.bExclusive = TRUE;
240172aead81Sdrh bReturn = TRUE;
240272aead81Sdrh }
240372aead81Sdrh }
240472aead81Sdrh
240572aead81Sdrh /* Want a read-only lock? */
240611bb41f8Sshane else if (dwFileOffsetLow == (DWORD)SHARED_FIRST &&
240772aead81Sdrh nNumberOfBytesToLockLow == 1){
240872aead81Sdrh if (pFile->shared->bExclusive == 0){
240972aead81Sdrh pFile->local.nReaders ++;
241072aead81Sdrh if (pFile->local.nReaders == 1){
241172aead81Sdrh pFile->shared->nReaders ++;
241272aead81Sdrh }
241372aead81Sdrh bReturn = TRUE;
241472aead81Sdrh }
241572aead81Sdrh }
241672aead81Sdrh
241772aead81Sdrh /* Want a pending lock? */
24181488052dSdrh else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
24191488052dSdrh && nNumberOfBytesToLockLow == 1){
242072aead81Sdrh /* If no pending lock has been acquired, then acquire it */
242172aead81Sdrh if (pFile->shared->bPending == 0) {
242272aead81Sdrh pFile->shared->bPending = TRUE;
242372aead81Sdrh pFile->local.bPending = TRUE;
242472aead81Sdrh bReturn = TRUE;
242572aead81Sdrh }
242672aead81Sdrh }
2427338ea3c1Sshane
242872aead81Sdrh /* Want a reserved lock? */
24291488052dSdrh else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
24301488052dSdrh && nNumberOfBytesToLockLow == 1){
243172aead81Sdrh if (pFile->shared->bReserved == 0) {
243272aead81Sdrh pFile->shared->bReserved = TRUE;
243372aead81Sdrh pFile->local.bReserved = TRUE;
243472aead81Sdrh bReturn = TRUE;
243572aead81Sdrh }
243672aead81Sdrh }
243772aead81Sdrh
243872aead81Sdrh winceMutexRelease(pFile->hMutex);
243972aead81Sdrh return bReturn;
244072aead81Sdrh }
244172aead81Sdrh
244272aead81Sdrh /*
2443318507b7Smistachkin ** An implementation of the UnlockFile API of Windows for CE
244472aead81Sdrh */
winceUnlockFile(LPHANDLE phFile,DWORD dwFileOffsetLow,DWORD dwFileOffsetHigh,DWORD nNumberOfBytesToUnlockLow,DWORD nNumberOfBytesToUnlockHigh)244572aead81Sdrh static BOOL winceUnlockFile(
2446a749486eSmistachkin LPHANDLE phFile,
244772aead81Sdrh DWORD dwFileOffsetLow,
244872aead81Sdrh DWORD dwFileOffsetHigh,
244972aead81Sdrh DWORD nNumberOfBytesToUnlockLow,
245072aead81Sdrh DWORD nNumberOfBytesToUnlockHigh
245172aead81Sdrh ){
245272aead81Sdrh winFile *pFile = HANDLE_TO_WINFILE(phFile);
245372aead81Sdrh BOOL bReturn = FALSE;
245472aead81Sdrh
2455d87873d1Sshane UNUSED_PARAMETER(dwFileOffsetHigh);
2456d87873d1Sshane UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh);
2457d87873d1Sshane
245872aead81Sdrh if (!pFile->hMutex) return TRUE;
245972aead81Sdrh winceMutexAcquire(pFile->hMutex);
246072aead81Sdrh
246172aead81Sdrh /* Releasing a reader lock or an exclusive lock */
246211bb41f8Sshane if (dwFileOffsetLow == (DWORD)SHARED_FIRST){
246372aead81Sdrh /* Did we have an exclusive lock? */
246472aead81Sdrh if (pFile->local.bExclusive){
246511bb41f8Sshane assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE);
246672aead81Sdrh pFile->local.bExclusive = FALSE;
246772aead81Sdrh pFile->shared->bExclusive = FALSE;
246872aead81Sdrh bReturn = TRUE;
246972aead81Sdrh }
247072aead81Sdrh
247172aead81Sdrh /* Did we just have a reader lock? */
247272aead81Sdrh else if (pFile->local.nReaders){
24731488052dSdrh assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE
24741488052dSdrh || nNumberOfBytesToUnlockLow == 1);
247572aead81Sdrh pFile->local.nReaders --;
247672aead81Sdrh if (pFile->local.nReaders == 0)
247772aead81Sdrh {
247872aead81Sdrh pFile->shared->nReaders --;
247972aead81Sdrh }
248072aead81Sdrh bReturn = TRUE;
248172aead81Sdrh }
248272aead81Sdrh }
248372aead81Sdrh
248472aead81Sdrh /* Releasing a pending lock */
24851488052dSdrh else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
24861488052dSdrh && nNumberOfBytesToUnlockLow == 1){
248772aead81Sdrh if (pFile->local.bPending){
248872aead81Sdrh pFile->local.bPending = FALSE;
248972aead81Sdrh pFile->shared->bPending = FALSE;
249072aead81Sdrh bReturn = TRUE;
249172aead81Sdrh }
249272aead81Sdrh }
249372aead81Sdrh /* Releasing a reserved lock */
24941488052dSdrh else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
24951488052dSdrh && nNumberOfBytesToUnlockLow == 1){
249672aead81Sdrh if (pFile->local.bReserved) {
249772aead81Sdrh pFile->local.bReserved = FALSE;
249872aead81Sdrh pFile->shared->bReserved = FALSE;
249972aead81Sdrh bReturn = TRUE;
250072aead81Sdrh }
250172aead81Sdrh }
250272aead81Sdrh
250372aead81Sdrh winceMutexRelease(pFile->hMutex);
250472aead81Sdrh return bReturn;
250572aead81Sdrh }
250672aead81Sdrh /*
250772aead81Sdrh ** End of the special code for wince
250872aead81Sdrh *****************************************************************************/
250929bafeabSdanielk1977 #endif /* SQLITE_OS_WINCE */
2510c0929987Sdrh
2511a749486eSmistachkin /*
2512a749486eSmistachkin ** Lock a file region.
2513a749486eSmistachkin */
winLockFile(LPHANDLE phFile,DWORD flags,DWORD offsetLow,DWORD offsetHigh,DWORD numBytesLow,DWORD numBytesHigh)2514a749486eSmistachkin static BOOL winLockFile(
2515a749486eSmistachkin LPHANDLE phFile,
2516a749486eSmistachkin DWORD flags,
2517a749486eSmistachkin DWORD offsetLow,
2518a749486eSmistachkin DWORD offsetHigh,
2519a749486eSmistachkin DWORD numBytesLow,
2520a749486eSmistachkin DWORD numBytesHigh
2521a749486eSmistachkin ){
2522a749486eSmistachkin #if SQLITE_OS_WINCE
2523a749486eSmistachkin /*
2524a749486eSmistachkin ** NOTE: Windows CE is handled differently here due its lack of the Win32
2525a749486eSmistachkin ** API LockFile.
2526a749486eSmistachkin */
2527a749486eSmistachkin return winceLockFile(phFile, offsetLow, offsetHigh,
2528a749486eSmistachkin numBytesLow, numBytesHigh);
2529a749486eSmistachkin #else
2530b324bc79Smistachkin if( osIsNT() ){
2531a749486eSmistachkin OVERLAPPED ovlp;
2532a749486eSmistachkin memset(&ovlp, 0, sizeof(OVERLAPPED));
2533a749486eSmistachkin ovlp.Offset = offsetLow;
2534a749486eSmistachkin ovlp.OffsetHigh = offsetHigh;
2535a749486eSmistachkin return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
2536a749486eSmistachkin }else{
2537a749486eSmistachkin return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
2538a749486eSmistachkin numBytesHigh);
2539a749486eSmistachkin }
2540a749486eSmistachkin #endif
2541a749486eSmistachkin }
2542a749486eSmistachkin
2543a749486eSmistachkin /*
2544a749486eSmistachkin ** Unlock a file region.
2545a749486eSmistachkin */
winUnlockFile(LPHANDLE phFile,DWORD offsetLow,DWORD offsetHigh,DWORD numBytesLow,DWORD numBytesHigh)2546a749486eSmistachkin static BOOL winUnlockFile(
2547a749486eSmistachkin LPHANDLE phFile,
2548a749486eSmistachkin DWORD offsetLow,
2549a749486eSmistachkin DWORD offsetHigh,
2550a749486eSmistachkin DWORD numBytesLow,
2551a749486eSmistachkin DWORD numBytesHigh
2552a749486eSmistachkin ){
2553a749486eSmistachkin #if SQLITE_OS_WINCE
2554a749486eSmistachkin /*
2555a749486eSmistachkin ** NOTE: Windows CE is handled differently here due its lack of the Win32
2556a749486eSmistachkin ** API UnlockFile.
2557a749486eSmistachkin */
2558a749486eSmistachkin return winceUnlockFile(phFile, offsetLow, offsetHigh,
2559a749486eSmistachkin numBytesLow, numBytesHigh);
2560a749486eSmistachkin #else
2561b324bc79Smistachkin if( osIsNT() ){
2562a749486eSmistachkin OVERLAPPED ovlp;
2563a749486eSmistachkin memset(&ovlp, 0, sizeof(OVERLAPPED));
2564a749486eSmistachkin ovlp.Offset = offsetLow;
2565a749486eSmistachkin ovlp.OffsetHigh = offsetHigh;
2566a749486eSmistachkin return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
2567a749486eSmistachkin }else{
2568a749486eSmistachkin return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
2569a749486eSmistachkin numBytesHigh);
2570a749486eSmistachkin }
2571a749486eSmistachkin #endif
2572a749486eSmistachkin }
2573a749486eSmistachkin
2574153c62c4Sdrh /*****************************************************************************
2575153c62c4Sdrh ** The next group of routines implement the I/O methods specified
2576153c62c4Sdrh ** by the sqlite3_io_methods object.
2577153c62c4Sdrh ******************************************************************************/
2578bbd42a6dSdrh
2579bbd42a6dSdrh /*
2580318507b7Smistachkin ** Some Microsoft compilers lack this definition.
2581502019c8Sdan */
2582502019c8Sdan #ifndef INVALID_SET_FILE_POINTER
2583502019c8Sdan # define INVALID_SET_FILE_POINTER ((DWORD)-1)
2584502019c8Sdan #endif
2585502019c8Sdan
2586502019c8Sdan /*
2587502019c8Sdan ** Move the current position of the file handle passed as the first
2588502019c8Sdan ** argument to offset iOffset within the file. If successful, return 0.
2589502019c8Sdan ** Otherwise, set pFile->lastErrno and return non-zero.
2590502019c8Sdan */
winSeekFile(winFile * pFile,sqlite3_int64 iOffset)2591b324bc79Smistachkin static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
2592df562d55Smistachkin #if !SQLITE_OS_WINRT
2593502019c8Sdan LONG upperBits; /* Most sig. 32 bits of new offset */
2594502019c8Sdan LONG lowerBits; /* Least sig. 32 bits of new offset */
2595502019c8Sdan DWORD dwRet; /* Value returned by SetFilePointer() */
2596d1ef9b6dSmistachkin DWORD lastErrno; /* Value returned by GetLastError() */
2597502019c8Sdan
2598f2c1c99fSmistachkin OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
2599f2c1c99fSmistachkin
2600502019c8Sdan upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
2601502019c8Sdan lowerBits = (LONG)(iOffset & 0xffffffff);
2602502019c8Sdan
2603502019c8Sdan /* API oddity: If successful, SetFilePointer() returns a dword
2604502019c8Sdan ** containing the lower 32-bits of the new file-offset. Or, if it fails,
2605502019c8Sdan ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
2606502019c8Sdan ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
260748864df9Smistachkin ** whether an error has actually occurred, it is also necessary to call
2608502019c8Sdan ** GetLastError().
2609502019c8Sdan */
2610318507b7Smistachkin dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
2611d1ef9b6dSmistachkin
2612d1ef9b6dSmistachkin if( (dwRet==INVALID_SET_FILE_POINTER
2613d1ef9b6dSmistachkin && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
2614d1ef9b6dSmistachkin pFile->lastErrno = lastErrno;
26152aef997cSmistachkin winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
2616b324bc79Smistachkin "winSeekFile", pFile->zPath);
2617f2c1c99fSmistachkin OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
2618502019c8Sdan return 1;
2619502019c8Sdan }
2620502019c8Sdan
2621f2c1c99fSmistachkin OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
2622502019c8Sdan return 0;
2623df562d55Smistachkin #else
26248045df0aSdrh /*
2625df562d55Smistachkin ** Same as above, except that this implementation works for WinRT.
26268045df0aSdrh */
2627df562d55Smistachkin
262875b70a20Smistachkin LARGE_INTEGER x; /* The new offset */
262975b70a20Smistachkin BOOL bRet; /* Value returned by SetFilePointerEx() */
263075b70a20Smistachkin
26318045df0aSdrh x.QuadPart = iOffset;
263275b70a20Smistachkin bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
263375b70a20Smistachkin
263475b70a20Smistachkin if(!bRet){
263575b70a20Smistachkin pFile->lastErrno = osGetLastError();
263675b70a20Smistachkin winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
2637b324bc79Smistachkin "winSeekFile", pFile->zPath);
2638f2c1c99fSmistachkin OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
263975b70a20Smistachkin return 1;
264075b70a20Smistachkin }
264175b70a20Smistachkin
2642f2c1c99fSmistachkin OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
264375b70a20Smistachkin return 0;
26448045df0aSdrh #endif
2645df562d55Smistachkin }
2646502019c8Sdan
26475824e053Smistachkin #if SQLITE_MAX_MMAP_SIZE>0
2648d95a3d35Smistachkin /* Forward references to VFS helper methods used for memory mapped files */
2649d95a3d35Smistachkin static int winMapfile(winFile*, sqlite3_int64);
26505175b324Sdrh static int winUnmapfile(winFile*);
26515824e053Smistachkin #endif
2652daf9a5a4Smistachkin
2653502019c8Sdan /*
2654bbd42a6dSdrh ** Close a file.
265559e63a6bSdrh **
265659e63a6bSdrh ** It is reported that an attempt to close a handle might sometimes
2657318507b7Smistachkin ** fail. This is a very unreasonable result, but Windows is notorious
265859e63a6bSdrh ** for being unreasonable so I do not doubt that it might happen. If
265959e63a6bSdrh ** the close fails, we pause for 100 milliseconds and try again. As
266059e63a6bSdrh ** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
266159e63a6bSdrh ** giving up and returning an error.
2662bbd42a6dSdrh */
266359e63a6bSdrh #define MX_CLOSE_ATTEMPT 3
winClose(sqlite3_file * id)2664153c62c4Sdrh static int winClose(sqlite3_file *id){
2665eb4fa52aSdrh int rc, cnt = 0;
2666153c62c4Sdrh winFile *pFile = (winFile*)id;
266750daafc7Sshane
266850daafc7Sshane assert( id!=0 );
26696f92833fSmistachkin #ifndef SQLITE_OMIT_WAL
267083235214Sdrh assert( pFile->pShm==0 );
26716f92833fSmistachkin #endif
26729ce59a94Smistachkin assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
2673ad1e55e5Smistachkin OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p\n",
2674ad1e55e5Smistachkin osGetCurrentProcessId(), pFile, pFile->h));
2675daf9a5a4Smistachkin
26765824e053Smistachkin #if SQLITE_MAX_MMAP_SIZE>0
2677c216566bSmistachkin winUnmapfile(pFile);
26785824e053Smistachkin #endif
2679daf9a5a4Smistachkin
2680eb4fa52aSdrh do{
2681318507b7Smistachkin rc = osCloseHandle(pFile->h);
2682e2ad9317Sshaneh /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
2683f4f327ceSmistachkin }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
268429bafeabSdanielk1977 #if SQLITE_OS_WINCE
2685d641d511Sdrh #define WINCE_DELETION_ATTEMPTS 3
26861e75483cSmistachkin {
26871e75483cSmistachkin winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
26881e75483cSmistachkin if( pAppData==NULL || !pAppData->bNoLock ){
268972aead81Sdrh winceDestroyLock(pFile);
26901e75483cSmistachkin }
26911e75483cSmistachkin }
26927229ed43Sdrh if( pFile->zDeleteOnClose ){
2693d641d511Sdrh int cnt = 0;
2694d641d511Sdrh while(
2695318507b7Smistachkin osDeleteFileW(pFile->zDeleteOnClose)==0
2696318507b7Smistachkin && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
2697d641d511Sdrh && cnt++ < WINCE_DELETION_ATTEMPTS
2698d641d511Sdrh ){
2699f4f327ceSmistachkin sqlite3_win32_sleep(100); /* Wait a little before trying again */
2700d641d511Sdrh }
27015f075388Smistachkin sqlite3_free(pFile->zDeleteOnClose);
27027229ed43Sdrh }
2703cc78fea4Sdrh #endif
270405340e32Smistachkin if( rc ){
270505340e32Smistachkin pFile->h = NULL;
270605340e32Smistachkin }
2707bbd42a6dSdrh OpenCounter(-1);
2708ad1e55e5Smistachkin OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p, rc=%s\n",
2709ad1e55e5Smistachkin osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
271050990dbbSdrh return rc ? SQLITE_OK
2711318507b7Smistachkin : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
27122aef997cSmistachkin "winClose", pFile->zPath);
2713bbd42a6dSdrh }
2714bbd42a6dSdrh
2715bbd42a6dSdrh /*
2716bbd42a6dSdrh ** Read data from a file into a buffer. Return SQLITE_OK if all
2717bbd42a6dSdrh ** bytes were read successfully and SQLITE_IOERR if anything goes
2718bbd42a6dSdrh ** wrong.
2719bbd42a6dSdrh */
winRead(sqlite3_file * id,void * pBuf,int amt,sqlite3_int64 offset)2720153c62c4Sdrh static int winRead(
2721153c62c4Sdrh sqlite3_file *id, /* File to read from */
2722153c62c4Sdrh void *pBuf, /* Write content into this buffer */
2723153c62c4Sdrh int amt, /* Number of bytes to read */
2724153c62c4Sdrh sqlite3_int64 offset /* Begin reading at this offset */
2725153c62c4Sdrh ){
2726d9d812fcSmistachkin #if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
272705340e32Smistachkin OVERLAPPED overlapped; /* The offset for ReadFile. */
272800fa55d7Smistachkin #endif
2729502019c8Sdan winFile *pFile = (winFile*)id; /* file handle */
2730502019c8Sdan DWORD nRead; /* Number of bytes actually read from file */
27315d9ef0a6Sdrh int nRetry = 0; /* Number of retrys */
273250daafc7Sshane
27339cbe6352Sdrh assert( id!=0 );
2734daf9a5a4Smistachkin assert( amt>0 );
27356cf9d8d6Sdrh assert( offset>=0 );
27369cce7109Sdrh SimulateIOError(return SQLITE_IOERR_READ);
2737ad1e55e5Smistachkin OSTRACE(("READ pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
2738ad1e55e5Smistachkin "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
2739f2c1c99fSmistachkin pFile->h, pBuf, amt, offset, pFile->locktype));
2740502019c8Sdan
27419b4c59faSdrh #if SQLITE_MAX_MMAP_SIZE>0
27425175b324Sdrh /* Deal with as much of this read request as possible by transfering
27435175b324Sdrh ** data from the memory mapping using memcpy(). */
27445175b324Sdrh if( offset<pFile->mmapSize ){
27455175b324Sdrh if( offset+amt <= pFile->mmapSize ){
27465175b324Sdrh memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
2747ad1e55e5Smistachkin OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
2748ad1e55e5Smistachkin osGetCurrentProcessId(), pFile, pFile->h));
27495175b324Sdrh return SQLITE_OK;
27505175b324Sdrh }else{
27515175b324Sdrh int nCopy = (int)(pFile->mmapSize - offset);
27525175b324Sdrh memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
27535175b324Sdrh pBuf = &((u8 *)pBuf)[nCopy];
27545175b324Sdrh amt -= nCopy;
27555175b324Sdrh offset += nCopy;
27565175b324Sdrh }
27575175b324Sdrh }
27586e0b6d52Sdrh #endif
2759502019c8Sdan
2760d9d812fcSmistachkin #if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
2761b324bc79Smistachkin if( winSeekFile(pFile, offset) ){
2762ad1e55e5Smistachkin OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
2763ad1e55e5Smistachkin osGetCurrentProcessId(), pFile, pFile->h));
276400fa55d7Smistachkin return SQLITE_FULL;
276500fa55d7Smistachkin }
276600fa55d7Smistachkin while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
276700fa55d7Smistachkin #else
276805340e32Smistachkin memset(&overlapped, 0, sizeof(OVERLAPPED));
276905340e32Smistachkin overlapped.Offset = (LONG)(offset & 0xffffffff);
277005340e32Smistachkin overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
277105340e32Smistachkin while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
277205340e32Smistachkin osGetLastError()!=ERROR_HANDLE_EOF ){
277300fa55d7Smistachkin #endif
2774d1ef9b6dSmistachkin DWORD lastErrno;
2775b324bc79Smistachkin if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
2776d1ef9b6dSmistachkin pFile->lastErrno = lastErrno;
2777ad1e55e5Smistachkin OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_READ\n",
2778ad1e55e5Smistachkin osGetCurrentProcessId(), pFile, pFile->h));
27792aef997cSmistachkin return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
27802aef997cSmistachkin "winRead", pFile->zPath);
2781bbd42a6dSdrh }
278221aa6a1aSdrh winLogIoerr(nRetry, __LINE__);
2783502019c8Sdan if( nRead<(DWORD)amt ){
27844c17c3fbSdrh /* Unread parts of the buffer must be zero-filled */
2785502019c8Sdan memset(&((char*)pBuf)[nRead], 0, amt-nRead);
2786ad1e55e5Smistachkin OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_SHORT_READ\n",
2787ad1e55e5Smistachkin osGetCurrentProcessId(), pFile, pFile->h));
2788551b7736Sdrh return SQLITE_IOERR_SHORT_READ;
2789bbd42a6dSdrh }
2790502019c8Sdan
2791ad1e55e5Smistachkin OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
2792ad1e55e5Smistachkin osGetCurrentProcessId(), pFile, pFile->h));
2793502019c8Sdan return SQLITE_OK;
2794bbd42a6dSdrh }
2795bbd42a6dSdrh
2796bbd42a6dSdrh /*
2797bbd42a6dSdrh ** Write data from a buffer into a file. Return SQLITE_OK on success
2798bbd42a6dSdrh ** or some other error code on failure.
2799bbd42a6dSdrh */
2800153c62c4Sdrh static int winWrite(
2801153c62c4Sdrh sqlite3_file *id, /* File to write into */
2802153c62c4Sdrh const void *pBuf, /* The bytes to be written */
2803153c62c4Sdrh int amt, /* Number of bytes to write */
2804153c62c4Sdrh sqlite3_int64 offset /* Offset into the file to begin writing at */
2805153c62c4Sdrh ){
280648864df9Smistachkin int rc = 0; /* True if error has occurred, else false */
2807502019c8Sdan winFile *pFile = (winFile*)id; /* File handle */
2808a32ad843Sdrh int nRetry = 0; /* Number of retries */
280950daafc7Sshane
2810502019c8Sdan assert( amt>0 );
2811502019c8Sdan assert( pFile );
2812153c62c4Sdrh SimulateIOError(return SQLITE_IOERR_WRITE);
28135968593bSdrh SimulateDiskfullError(return SQLITE_FULL);
2814502019c8Sdan
2815ad1e55e5Smistachkin OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
2816ad1e55e5Smistachkin "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
2817f2c1c99fSmistachkin pFile->h, pBuf, amt, offset, pFile->locktype));
2818502019c8Sdan
2819c88cd137Smistachkin #if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
28205175b324Sdrh /* Deal with as much of this write request as possible by transfering
28215175b324Sdrh ** data from the memory mapping using memcpy(). */
28225175b324Sdrh if( offset<pFile->mmapSize ){
28235175b324Sdrh if( offset+amt <= pFile->mmapSize ){
28245175b324Sdrh memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
2825ad1e55e5Smistachkin OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
2826ad1e55e5Smistachkin osGetCurrentProcessId(), pFile, pFile->h));
28275175b324Sdrh return SQLITE_OK;
28285175b324Sdrh }else{
28295175b324Sdrh int nCopy = (int)(pFile->mmapSize - offset);
28305175b324Sdrh memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
28315175b324Sdrh pBuf = &((u8 *)pBuf)[nCopy];
28325175b324Sdrh amt -= nCopy;
28335175b324Sdrh offset += nCopy;
28345175b324Sdrh }
28355175b324Sdrh }
28366e0b6d52Sdrh #endif
2837502019c8Sdan
2838d9d812fcSmistachkin #if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
2839b324bc79Smistachkin rc = winSeekFile(pFile, offset);
284000fa55d7Smistachkin if( rc==0 ){
284100fa55d7Smistachkin #else
284205340e32Smistachkin {
284300fa55d7Smistachkin #endif
2844d9d812fcSmistachkin #if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
284505340e32Smistachkin OVERLAPPED overlapped; /* The offset for WriteFile. */
284600fa55d7Smistachkin #endif
2847502019c8Sdan u8 *aRem = (u8 *)pBuf; /* Data yet to be written */
2848502019c8Sdan int nRem = amt; /* Number of bytes yet to be written */
2849502019c8Sdan DWORD nWrite; /* Bytes written by each WriteFile() call */
2850d1ef9b6dSmistachkin DWORD lastErrno = NO_ERROR; /* Value returned by GetLastError() */
2851502019c8Sdan
2852d9d812fcSmistachkin #if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
285305340e32Smistachkin memset(&overlapped, 0, sizeof(OVERLAPPED));
285405340e32Smistachkin overlapped.Offset = (LONG)(offset & 0xffffffff);
285505340e32Smistachkin overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
285600fa55d7Smistachkin #endif
285705340e32Smistachkin
28585d9ef0a6Sdrh while( nRem>0 ){
2859d9d812fcSmistachkin #if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
286000fa55d7Smistachkin if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
286100fa55d7Smistachkin #else
286205340e32Smistachkin if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
286300fa55d7Smistachkin #endif
2864b324bc79Smistachkin if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
28655d9ef0a6Sdrh break;
28665d9ef0a6Sdrh }
2867e1b461bdSmistachkin assert( nWrite==0 || nWrite<=(DWORD)nRem );
2868e1b461bdSmistachkin if( nWrite==0 || nWrite>(DWORD)nRem ){
286905340e32Smistachkin lastErrno = osGetLastError();
287005340e32Smistachkin break;
287105340e32Smistachkin }
2872d9d812fcSmistachkin #if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
287305340e32Smistachkin offset += nWrite;
287405340e32Smistachkin overlapped.Offset = (LONG)(offset & 0xffffffff);
287505340e32Smistachkin overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
287600fa55d7Smistachkin #endif
2877502019c8Sdan aRem += nWrite;
2878502019c8Sdan nRem -= nWrite;
2879133ce560Sshaneh }
2880502019c8Sdan if( nRem>0 ){
2881d1ef9b6dSmistachkin pFile->lastErrno = lastErrno;
2882502019c8Sdan rc = 1;
2883502019c8Sdan }
2884502019c8Sdan }
2885502019c8Sdan
2886502019c8Sdan if( rc ){
2887eeb5d8aaSshaneh if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
2888eeb5d8aaSshaneh || ( pFile->lastErrno==ERROR_DISK_FULL )){
2889ad1e55e5Smistachkin OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
2890ad1e55e5Smistachkin osGetCurrentProcessId(), pFile, pFile->h));
28919f11ef12Smistachkin return winLogError(SQLITE_FULL, pFile->lastErrno,
28929f11ef12Smistachkin "winWrite1", pFile->zPath);
2893133ce560Sshaneh }
2894ad1e55e5Smistachkin OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_WRITE\n",
2895ad1e55e5Smistachkin osGetCurrentProcessId(), pFile, pFile->h));
28962aef997cSmistachkin return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
28979f11ef12Smistachkin "winWrite2", pFile->zPath);
2898a32ad843Sdrh }else{
289921aa6a1aSdrh winLogIoerr(nRetry, __LINE__);
2900bbd42a6dSdrh }
2901ad1e55e5Smistachkin OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
2902ad1e55e5Smistachkin osGetCurrentProcessId(), pFile, pFile->h));
2903bbd42a6dSdrh return SQLITE_OK;
2904bbd42a6dSdrh }
2905bbd42a6dSdrh
2906bbd42a6dSdrh /*
2907153c62c4Sdrh ** Truncate an open file to a specified size
2908bbdc2b94Sdrh */
2909c51250a5Sdrh static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
2910502019c8Sdan winFile *pFile = (winFile*)id; /* File handle object */
2911502019c8Sdan int rc = SQLITE_OK; /* Return code for this function */
2912daf9a5a4Smistachkin DWORD lastErrno;
29131e8487dbSmistachkin #if SQLITE_MAX_MMAP_SIZE>0
29141e8487dbSmistachkin sqlite3_int64 oldMmapSize;
2915eafb9a0aSdrh if( pFile->nFetchOut>0 ){
2916eafb9a0aSdrh /* File truncation is a no-op if there are outstanding memory mapped
2917eafb9a0aSdrh ** pages. This is because truncating the file means temporarily unmapping
2918eafb9a0aSdrh ** the file, and that might delete memory out from under existing cursors.
2919eafb9a0aSdrh **
2920eafb9a0aSdrh ** This can result in incremental vacuum not truncating the file,
2921eafb9a0aSdrh ** if there is an active read cursor when the incremental vacuum occurs.
2922eafb9a0aSdrh ** No real harm comes of this - the database file is not corrupted,
2923eafb9a0aSdrh ** though some folks might complain that the file is bigger than it
2924eafb9a0aSdrh ** needs to be.
2925eafb9a0aSdrh **
2926eafb9a0aSdrh ** The only feasible work-around is to defer the truncation until after
2927eafb9a0aSdrh ** all references to memory-mapped content are closed. That is doable,
2928eafb9a0aSdrh ** but involves adding a few branches in the common write code path which
2929eafb9a0aSdrh ** could slow down normal operations slightly. Hence, we have decided for
2930eafb9a0aSdrh ** now to simply make trancations a no-op if there are pending reads. We
2931eafb9a0aSdrh ** can maybe revisit this decision in the future.
2932eafb9a0aSdrh */
2933eafb9a0aSdrh return SQLITE_OK;
2934eafb9a0aSdrh }
29351e8487dbSmistachkin #endif
293650daafc7Sshane
2937502019c8Sdan assert( pFile );
2938153c62c4Sdrh SimulateIOError(return SQLITE_IOERR_TRUNCATE);
2939ad1e55e5Smistachkin OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n",
2940ad1e55e5Smistachkin osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
2941502019c8Sdan
2942502019c8Sdan /* If the user has configured a chunk-size for this file, truncate the
2943502019c8Sdan ** file so that it consists of an integer number of chunks (i.e. the
2944502019c8Sdan ** actual file size after the operation may be larger than the requested
2945502019c8Sdan ** size).
2946502019c8Sdan */
2947d589a544Smistachkin if( pFile->szChunk>0 ){
2948502019c8Sdan nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
2949502019c8Sdan }
2950502019c8Sdan
29511e8487dbSmistachkin #if SQLITE_MAX_MMAP_SIZE>0
29521e8487dbSmistachkin if( pFile->pMapRegion ){
29531e8487dbSmistachkin oldMmapSize = pFile->mmapSize;
29541e8487dbSmistachkin }else{
29551e8487dbSmistachkin oldMmapSize = 0;
29561e8487dbSmistachkin }
29571e8487dbSmistachkin winUnmapfile(pFile);
29581e8487dbSmistachkin #endif
29591e8487dbSmistachkin
2960502019c8Sdan /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
2961b324bc79Smistachkin if( winSeekFile(pFile, nByte) ){
29622aef997cSmistachkin rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
29632aef997cSmistachkin "winTruncate1", pFile->zPath);
2964daf9a5a4Smistachkin }else if( 0==osSetEndOfFile(pFile->h) &&
2965daf9a5a4Smistachkin ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
2966daf9a5a4Smistachkin pFile->lastErrno = lastErrno;
29672aef997cSmistachkin rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
29682aef997cSmistachkin "winTruncate2", pFile->zPath);
296950daafc7Sshane }
2970502019c8Sdan
29719b4c59faSdrh #if SQLITE_MAX_MMAP_SIZE>0
29721e8487dbSmistachkin if( rc==SQLITE_OK && oldMmapSize>0 ){
29731e8487dbSmistachkin if( oldMmapSize>nByte ){
29741e8487dbSmistachkin winMapfile(pFile, -1);
29751e8487dbSmistachkin }else{
29761e8487dbSmistachkin winMapfile(pFile, oldMmapSize);
29771e8487dbSmistachkin }
2978daf9a5a4Smistachkin }
29796e0b6d52Sdrh #endif
2980502019c8Sdan
2981ad1e55e5Smistachkin OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, rc=%s\n",
2982ad1e55e5Smistachkin osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
298304882a9eSshaneh return rc;
298450daafc7Sshane }
2985bbd42a6dSdrh
2986dec6fae9Sdrh #ifdef SQLITE_TEST
2987dec6fae9Sdrh /*
2988dec6fae9Sdrh ** Count the number of fullsyncs and normal syncs. This is used to test
2989dec6fae9Sdrh ** that syncs and fullsyncs are occuring at the right times.
2990dec6fae9Sdrh */
2991dec6fae9Sdrh int sqlite3_sync_count = 0;
2992dec6fae9Sdrh int sqlite3_fullsync_count = 0;
2993dec6fae9Sdrh #endif
2994dec6fae9Sdrh
2995bbd42a6dSdrh /*
2996bbd42a6dSdrh ** Make sure all writes to a particular file are committed to disk.
2997bbd42a6dSdrh */
2998153c62c4Sdrh static int winSync(sqlite3_file *id, int flags){
2999c377f310Smistachkin #ifndef SQLITE_NO_SYNC
3000c377f310Smistachkin /*
3001c377f310Smistachkin ** Used only when SQLITE_NO_SYNC is not defined.
3002c377f310Smistachkin */
30039dd6e080Sshaneh BOOL rc;
3004c377f310Smistachkin #endif
3005c377f310Smistachkin #if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \
3006fb383e92Smistachkin defined(SQLITE_HAVE_OS_TRACE)
3007c377f310Smistachkin /*
3008c377f310Smistachkin ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
3009c377f310Smistachkin ** OSTRACE() macros.
3010c377f310Smistachkin */
3011c377f310Smistachkin winFile *pFile = (winFile*)id;
301218e526c1Sshane #else
301318e526c1Sshane UNUSED_PARAMETER(id);
301418e526c1Sshane #endif
3015e2ad9317Sshaneh
3016e2ad9317Sshaneh assert( pFile );
3017e2ad9317Sshaneh /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
3018e2ad9317Sshaneh assert((flags&0x0F)==SQLITE_SYNC_NORMAL
3019e2ad9317Sshaneh || (flags&0x0F)==SQLITE_SYNC_FULL
3020e2ad9317Sshaneh );
3021e2ad9317Sshaneh
3022e2ad9317Sshaneh /* Unix cannot, but some systems may return SQLITE_FULL from here. This
3023e2ad9317Sshaneh ** line is to test that doing so does not cause any problems.
3024e2ad9317Sshaneh */
3025e2ad9317Sshaneh SimulateDiskfullError( return SQLITE_FULL );
30269dd6e080Sshaneh
3027ad1e55e5Smistachkin OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, flags=%x, lock=%d\n",
3028ad1e55e5Smistachkin osGetCurrentProcessId(), pFile, pFile->h, flags,
3029ad1e55e5Smistachkin pFile->locktype));
3030f2c1c99fSmistachkin
30319dd6e080Sshaneh #ifndef SQLITE_TEST
30329dd6e080Sshaneh UNUSED_PARAMETER(flags);
30339dd6e080Sshaneh #else
30349dd6e080Sshaneh if( (flags&0x0F)==SQLITE_SYNC_FULL ){
30359dd6e080Sshaneh sqlite3_fullsync_count++;
30369dd6e080Sshaneh }
30379dd6e080Sshaneh sqlite3_sync_count++;
30389dd6e080Sshaneh #endif
3039e2ad9317Sshaneh
304084ca3837Sshane /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
304184ca3837Sshane ** no-op
304284ca3837Sshane */
304384ca3837Sshane #ifdef SQLITE_NO_SYNC
3044ad1e55e5Smistachkin OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
3045ad1e55e5Smistachkin osGetCurrentProcessId(), pFile, pFile->h));
304684ca3837Sshane return SQLITE_OK;
304784ca3837Sshane #else
3048ccb43714Smistachkin #if SQLITE_MAX_MMAP_SIZE>0
3049ccb43714Smistachkin if( pFile->pMapRegion ){
3050ccb43714Smistachkin if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
3051ccb43714Smistachkin OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
3052ccb43714Smistachkin "rc=SQLITE_OK\n", osGetCurrentProcessId(),
3053ccb43714Smistachkin pFile, pFile->pMapRegion));
3054ccb43714Smistachkin }else{
3055ccb43714Smistachkin pFile->lastErrno = osGetLastError();
3056ccb43714Smistachkin OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
3057ccb43714Smistachkin "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(),
3058ccb43714Smistachkin pFile, pFile->pMapRegion));
3059ccb43714Smistachkin return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
3060ccb43714Smistachkin "winSync1", pFile->zPath);
3061ccb43714Smistachkin }
3062ccb43714Smistachkin }
3063ccb43714Smistachkin #endif
3064318507b7Smistachkin rc = osFlushFileBuffers(pFile->h);
30659dd6e080Sshaneh SimulateIOError( rc=FALSE );
30669dd6e080Sshaneh if( rc ){
3067ad1e55e5Smistachkin OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
3068ad1e55e5Smistachkin osGetCurrentProcessId(), pFile, pFile->h));
3069bbd42a6dSdrh return SQLITE_OK;
3070bbd42a6dSdrh }else{
3071318507b7Smistachkin pFile->lastErrno = osGetLastError();
3072ad1e55e5Smistachkin OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_FSYNC\n",
3073ad1e55e5Smistachkin osGetCurrentProcessId(), pFile, pFile->h));
30742aef997cSmistachkin return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
3075ccb43714Smistachkin "winSync2", pFile->zPath);
3076bbd42a6dSdrh }
307784ca3837Sshane #endif
3078bbd42a6dSdrh }
3079bbd42a6dSdrh
3080bbd42a6dSdrh /*
3081bbd42a6dSdrh ** Determine the current size of a file in bytes
3082bbd42a6dSdrh */
3083153c62c4Sdrh static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
3084153c62c4Sdrh winFile *pFile = (winFile*)id;
308524560d16Sdrh int rc = SQLITE_OK;
308650daafc7Sshane
308750daafc7Sshane assert( id!=0 );
3088f2c1c99fSmistachkin assert( pSize!=0 );
30899cce7109Sdrh SimulateIOError(return SQLITE_IOERR_FSTAT);
3090f2c1c99fSmistachkin OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
3091f2c1c99fSmistachkin
309224560d16Sdrh #if SQLITE_OS_WINRT
30939db299fbSshane {
309424560d16Sdrh FILE_STANDARD_INFO info;
309575b70a20Smistachkin if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
309624560d16Sdrh &info, sizeof(info)) ){
309724560d16Sdrh *pSize = info.EndOfFile.QuadPart;
309824560d16Sdrh }else{
309975b70a20Smistachkin pFile->lastErrno = osGetLastError();
310024560d16Sdrh rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
31012aef997cSmistachkin "winFileSize", pFile->zPath);
31029db299fbSshane }
310324560d16Sdrh }
310424560d16Sdrh #else
310524560d16Sdrh {
310624560d16Sdrh DWORD upperBits;
310724560d16Sdrh DWORD lowerBits;
310824560d16Sdrh DWORD lastErrno;
310924560d16Sdrh
311024560d16Sdrh lowerBits = osGetFileSize(pFile->h, &upperBits);
3111153c62c4Sdrh *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
311224560d16Sdrh if( (lowerBits == INVALID_FILE_SIZE)
311324560d16Sdrh && ((lastErrno = osGetLastError())!=NO_ERROR) ){
311424560d16Sdrh pFile->lastErrno = lastErrno;
311524560d16Sdrh rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
311624560d16Sdrh "winFileSize", pFile->zPath);
311724560d16Sdrh }
311824560d16Sdrh }
311924560d16Sdrh #endif
3120f2c1c99fSmistachkin OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n",
3121f2c1c99fSmistachkin pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
312224560d16Sdrh return rc;
3123bbd42a6dSdrh }
3124bbd42a6dSdrh
3125bbd42a6dSdrh /*
3126602bbd32Sdrh ** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
3127602bbd32Sdrh */
3128602bbd32Sdrh #ifndef LOCKFILE_FAIL_IMMEDIATELY
3129602bbd32Sdrh # define LOCKFILE_FAIL_IMMEDIATELY 1
3130602bbd32Sdrh #endif
3131602bbd32Sdrh
31322a5cfb31Smistachkin #ifndef LOCKFILE_EXCLUSIVE_LOCK
31332a5cfb31Smistachkin # define LOCKFILE_EXCLUSIVE_LOCK 2
31342a5cfb31Smistachkin #endif
31352a5cfb31Smistachkin
31362a5cfb31Smistachkin /*
31372a5cfb31Smistachkin ** Historically, SQLite has used both the LockFile and LockFileEx functions.
31382a5cfb31Smistachkin ** When the LockFile function was used, it was always expected to fail
31392a5cfb31Smistachkin ** immediately if the lock could not be obtained. Also, it always expected to
31402a5cfb31Smistachkin ** obtain an exclusive lock. These flags are used with the LockFileEx function
31412a5cfb31Smistachkin ** and reflect those expectations; therefore, they should not be changed.
31422a5cfb31Smistachkin */
31432a5cfb31Smistachkin #ifndef SQLITE_LOCKFILE_FLAGS
31442a5cfb31Smistachkin # define SQLITE_LOCKFILE_FLAGS (LOCKFILE_FAIL_IMMEDIATELY | \
31452a5cfb31Smistachkin LOCKFILE_EXCLUSIVE_LOCK)
31462a5cfb31Smistachkin #endif
31472a5cfb31Smistachkin
31482a5cfb31Smistachkin /*
31492a5cfb31Smistachkin ** Currently, SQLite never calls the LockFileEx function without wanting the
31502a5cfb31Smistachkin ** call to fail immediately if the lock cannot be obtained.
31512a5cfb31Smistachkin */
31522a5cfb31Smistachkin #ifndef SQLITE_LOCKFILEEX_FLAGS
31532a5cfb31Smistachkin # define SQLITE_LOCKFILEEX_FLAGS (LOCKFILE_FAIL_IMMEDIATELY)
31542a5cfb31Smistachkin #endif
31552a5cfb31Smistachkin
3156602bbd32Sdrh /*
31579c105bb9Sdrh ** Acquire a reader lock.
315851c6d963Sdrh ** Different API routines are called depending on whether or not this
31596c3c1a09Smistachkin ** is Win9x or WinNT.
316051c6d963Sdrh */
3161b324bc79Smistachkin static int winGetReadLock(winFile *pFile){
316251c6d963Sdrh int res;
3163f2c1c99fSmistachkin OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
3164b324bc79Smistachkin if( osIsNT() ){
3165a749486eSmistachkin #if SQLITE_OS_WINCE
3166a749486eSmistachkin /*
3167a749486eSmistachkin ** NOTE: Windows CE is handled differently here due its lack of the Win32
3168a749486eSmistachkin ** API LockFileEx.
3169891adeacSshane */
3170a749486eSmistachkin res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
3171a749486eSmistachkin #else
3172a749486eSmistachkin res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
31732a5cfb31Smistachkin SHARED_SIZE, 0);
3174a749486eSmistachkin #endif
3175d52ee729Sdrh }
3176d52ee729Sdrh #ifdef SQLITE_WIN32_HAS_ANSI
3177d52ee729Sdrh else{
31789c105bb9Sdrh int lk;
31792fa1868fSdrh sqlite3_randomness(sizeof(lk), &lk);
31801bd10f8aSdrh pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
3181a749486eSmistachkin res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
31822a5cfb31Smistachkin SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
318351c6d963Sdrh }
3184d52ee729Sdrh #endif
31859db299fbSshane if( res == 0 ){
3186318507b7Smistachkin pFile->lastErrno = osGetLastError();
318750990dbbSdrh /* No need to log a failure to lock */
31889db299fbSshane }
3189ff0bc8f9Smistachkin OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
319051c6d963Sdrh return res;
319151c6d963Sdrh }
319251c6d963Sdrh
319351c6d963Sdrh /*
319451c6d963Sdrh ** Undo a readlock
319551c6d963Sdrh */
3196b324bc79Smistachkin static int winUnlockReadLock(winFile *pFile){
319751c6d963Sdrh int res;
3198d1ef9b6dSmistachkin DWORD lastErrno;
3199f2c1c99fSmistachkin OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
3200b324bc79Smistachkin if( osIsNT() ){
3201a749486eSmistachkin res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
320251c6d963Sdrh }
3203d52ee729Sdrh #ifdef SQLITE_WIN32_HAS_ANSI
3204d52ee729Sdrh else{
3205a749486eSmistachkin res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
3206d52ee729Sdrh }
3207d52ee729Sdrh #endif
3208d1ef9b6dSmistachkin if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
3209d1ef9b6dSmistachkin pFile->lastErrno = lastErrno;
32102aef997cSmistachkin winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
3211b324bc79Smistachkin "winUnlockReadLock", pFile->zPath);
32129db299fbSshane }
3213ff0bc8f9Smistachkin OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
321451c6d963Sdrh return res;
321551c6d963Sdrh }
321651c6d963Sdrh
32179a09a3caStpoindex /*
3218b3e04346Sdrh ** Lock the file with the lock specified by parameter locktype - one
3219b3e04346Sdrh ** of the following:
3220b3e04346Sdrh **
3221b3e04346Sdrh ** (1) SHARED_LOCK
3222b3e04346Sdrh ** (2) RESERVED_LOCK
3223b3e04346Sdrh ** (3) PENDING_LOCK
3224b3e04346Sdrh ** (4) EXCLUSIVE_LOCK
3225b3e04346Sdrh **
3226b3e04346Sdrh ** Sometimes when requesting one lock state, additional lock states
3227b3e04346Sdrh ** are inserted in between. The locking might fail on one of the later
3228b3e04346Sdrh ** transitions leaving the lock state different from what it started but
3229b3e04346Sdrh ** still short of its goal. The following chart shows the allowed
3230b3e04346Sdrh ** transitions and the inserted intermediate states:
3231b3e04346Sdrh **
3232b3e04346Sdrh ** UNLOCKED -> SHARED
3233b3e04346Sdrh ** SHARED -> RESERVED
3234b3e04346Sdrh ** SHARED -> (PENDING) -> EXCLUSIVE
3235b3e04346Sdrh ** RESERVED -> (PENDING) -> EXCLUSIVE
3236b3e04346Sdrh ** PENDING -> EXCLUSIVE
3237b3e04346Sdrh **
32389c06c953Sdrh ** This routine will only increase a lock. The winUnlock() routine
3239b3e04346Sdrh ** erases all locks at once and returns us immediately to locking level 0.
3240b3e04346Sdrh ** It is not possible to lower the locking level one step at a time. You
3241b3e04346Sdrh ** must go straight to locking level 0.
324251c6d963Sdrh */
3243153c62c4Sdrh static int winLock(sqlite3_file *id, int locktype){
324451c6d963Sdrh int rc = SQLITE_OK; /* Return code from subroutines */
3245318507b7Smistachkin int res = 1; /* Result of a Windows lock call */
3246153c62c4Sdrh int newLocktype; /* Set pFile->locktype to this value before exiting */
3247e54ca3feSdrh int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
3248054889ecSdrh winFile *pFile = (winFile*)id;
3249d1ef9b6dSmistachkin DWORD lastErrno = NO_ERROR;
325051c6d963Sdrh
325150daafc7Sshane assert( id!=0 );
3252f2c1c99fSmistachkin OSTRACE(("LOCK file=%p, oldLock=%d(%d), newLock=%d\n",
3253f2c1c99fSmistachkin pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
325451c6d963Sdrh
325551c6d963Sdrh /* If there is already a lock of this type or more restrictive on the
325651c6d963Sdrh ** OsFile, do nothing. Don't use the end_lock: exit path, as
325751c6d963Sdrh ** sqlite3OsEnterMutex() hasn't been called yet.
325851c6d963Sdrh */
3259054889ecSdrh if( pFile->locktype>=locktype ){
3260f2c1c99fSmistachkin OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
32612a02e339Sdanielk1977 return SQLITE_OK;
32622a02e339Sdanielk1977 }
32632a02e339Sdanielk1977
3264275fe3adSdrh /* Do not allow any kind of write-lock on a read-only database
3265275fe3adSdrh */
3266275fe3adSdrh if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
3267275fe3adSdrh return SQLITE_IOERR_LOCK;
3268275fe3adSdrh }
3269275fe3adSdrh
3270b3e04346Sdrh /* Make sure the locking sequence is correct
3271b3e04346Sdrh */
3272054889ecSdrh assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
3273b3e04346Sdrh assert( locktype!=PENDING_LOCK );
3274054889ecSdrh assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
3275b3e04346Sdrh
327651c6d963Sdrh /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
327751c6d963Sdrh ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
327851c6d963Sdrh ** the PENDING_LOCK byte is temporary.
327951c6d963Sdrh */
3280054889ecSdrh newLocktype = pFile->locktype;
3281333f8056Sdrh if( pFile->locktype==NO_LOCK
3282333f8056Sdrh || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
3283e54ca3feSdrh ){
3284b3e04346Sdrh int cnt = 3;
3285a749486eSmistachkin while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
32862a5cfb31Smistachkin PENDING_BYTE, 0, 1, 0))==0 ){
3287d6ca4b9fSdrh /* Try 3 times to get the pending lock. This is needed to work
3288318507b7Smistachkin ** around problems caused by indexing and/or anti-virus software on
3289318507b7Smistachkin ** Windows systems.
3290d6ca4b9fSdrh ** If you are using this code as a model for alternative VFSes, do not
3291318507b7Smistachkin ** copy this retry logic. It is a hack intended for Windows only.
329251c6d963Sdrh */
32938e86b6a3Smistachkin lastErrno = osGetLastError();
3294ff0bc8f9Smistachkin OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
3295ff0bc8f9Smistachkin pFile->h, cnt, res));
32968e86b6a3Smistachkin if( lastErrno==ERROR_INVALID_HANDLE ){
32978e86b6a3Smistachkin pFile->lastErrno = lastErrno;
32988e86b6a3Smistachkin rc = SQLITE_IOERR_LOCK;
32998e86b6a3Smistachkin OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
33008e86b6a3Smistachkin pFile->h, cnt, sqlite3ErrName(rc)));
33018e86b6a3Smistachkin return rc;
33028e86b6a3Smistachkin }
3303f4f327ceSmistachkin if( cnt ) sqlite3_win32_sleep(1);
330451c6d963Sdrh }
3305e54ca3feSdrh gotPendingLock = res;
33069db299fbSshane if( !res ){
3307d1ef9b6dSmistachkin lastErrno = osGetLastError();
33089db299fbSshane }
33092a02e339Sdanielk1977 }
33102a02e339Sdanielk1977
331151c6d963Sdrh /* Acquire a shared lock
3312bbd42a6dSdrh */
3313b3e04346Sdrh if( locktype==SHARED_LOCK && res ){
3314054889ecSdrh assert( pFile->locktype==NO_LOCK );
3315b324bc79Smistachkin res = winGetReadLock(pFile);
3316e54ca3feSdrh if( res ){
3317e54ca3feSdrh newLocktype = SHARED_LOCK;
33189db299fbSshane }else{
3319d1ef9b6dSmistachkin lastErrno = osGetLastError();
3320bbd42a6dSdrh }
3321bbd42a6dSdrh }
3322bbd42a6dSdrh
332351c6d963Sdrh /* Acquire a RESERVED lock
3324bbd42a6dSdrh */
3325b3e04346Sdrh if( locktype==RESERVED_LOCK && res ){
3326054889ecSdrh assert( pFile->locktype==SHARED_LOCK );
3327a749486eSmistachkin res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
3328e54ca3feSdrh if( res ){
3329e54ca3feSdrh newLocktype = RESERVED_LOCK;
33309db299fbSshane }else{
3331d1ef9b6dSmistachkin lastErrno = osGetLastError();
3332e54ca3feSdrh }
3333e54ca3feSdrh }
3334e54ca3feSdrh
3335e54ca3feSdrh /* Acquire a PENDING lock
3336e54ca3feSdrh */
3337b3e04346Sdrh if( locktype==EXCLUSIVE_LOCK && res ){
3338e54ca3feSdrh newLocktype = PENDING_LOCK;
3339e54ca3feSdrh gotPendingLock = 0;
334051c6d963Sdrh }
334151c6d963Sdrh
334251c6d963Sdrh /* Acquire an EXCLUSIVE lock
334351c6d963Sdrh */
3344e54ca3feSdrh if( locktype==EXCLUSIVE_LOCK && res ){
3345054889ecSdrh assert( pFile->locktype>=SHARED_LOCK );
3346b324bc79Smistachkin res = winUnlockReadLock(pFile);
3347a749486eSmistachkin res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
33482a5cfb31Smistachkin SHARED_SIZE, 0);
3349e54ca3feSdrh if( res ){
3350e54ca3feSdrh newLocktype = EXCLUSIVE_LOCK;
3351e54ca3feSdrh }else{
3352d1ef9b6dSmistachkin lastErrno = osGetLastError();
3353b324bc79Smistachkin winGetReadLock(pFile);
3354e54ca3feSdrh }
3355e54ca3feSdrh }
3356e54ca3feSdrh
3357e54ca3feSdrh /* If we are holding a PENDING lock that ought to be released, then
3358e54ca3feSdrh ** release it now.
3359e54ca3feSdrh */
3360b3e04346Sdrh if( gotPendingLock && locktype==SHARED_LOCK ){
3361a749486eSmistachkin winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
3362bbd42a6dSdrh }
336351c6d963Sdrh
336451c6d963Sdrh /* Update the state of the lock has held in the file descriptor then
336551c6d963Sdrh ** return the appropriate result code.
336651c6d963Sdrh */
3367bbd42a6dSdrh if( res ){
3368bbd42a6dSdrh rc = SQLITE_OK;
3369bbd42a6dSdrh }else{
3370d1ef9b6dSmistachkin pFile->lastErrno = lastErrno;
3371bbd42a6dSdrh rc = SQLITE_BUSY;
3372533fb6deSmistachkin OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
3373533fb6deSmistachkin pFile->h, locktype, newLocktype));
3374bbd42a6dSdrh }
33751bd10f8aSdrh pFile->locktype = (u8)newLocktype;
3376f2c1c99fSmistachkin OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
3377f2c1c99fSmistachkin pFile->h, pFile->locktype, sqlite3ErrName(rc)));
3378bbd42a6dSdrh return rc;
3379bbd42a6dSdrh }
3380bbd42a6dSdrh
3381bbd42a6dSdrh /*
338251c6d963Sdrh ** This routine checks if there is a RESERVED lock held on the specified
338351c6d963Sdrh ** file by this or any other process. If such a lock is held, return
338451c6d963Sdrh ** non-zero, otherwise zero.
338551c6d963Sdrh */
3386861f7456Sdanielk1977 static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
3387ff0bc8f9Smistachkin int res;
3388054889ecSdrh winFile *pFile = (winFile*)id;
338950daafc7Sshane
3390e2ad9317Sshaneh SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
3391f2c1c99fSmistachkin OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
3392e2ad9317Sshaneh
339350daafc7Sshane assert( id!=0 );
3394054889ecSdrh if( pFile->locktype>=RESERVED_LOCK ){
3395ff0bc8f9Smistachkin res = 1;
3396ff0bc8f9Smistachkin OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
339751c6d963Sdrh }else{
3398ff0bc8f9Smistachkin res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
3399ff0bc8f9Smistachkin if( res ){
3400a749486eSmistachkin winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
340151c6d963Sdrh }
3402ff0bc8f9Smistachkin res = !res;
3403ff0bc8f9Smistachkin OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
340451c6d963Sdrh }
3405ff0bc8f9Smistachkin *pResOut = res;
3406f2c1c99fSmistachkin OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
3407f2c1c99fSmistachkin pFile->h, pResOut, *pResOut));
3408861f7456Sdanielk1977 return SQLITE_OK;
340951c6d963Sdrh }
341051c6d963Sdrh
341151c6d963Sdrh /*
3412a6abd041Sdrh ** Lower the locking level on file descriptor id to locktype. locktype
3413a6abd041Sdrh ** must be either NO_LOCK or SHARED_LOCK.
3414a6abd041Sdrh **
3415a6abd041Sdrh ** If the locking level of the file descriptor is already at or below
3416a6abd041Sdrh ** the requested locking level, this routine is a no-op.
3417a6abd041Sdrh **
34189c105bb9Sdrh ** It is not possible for this routine to fail if the second argument
34199c105bb9Sdrh ** is NO_LOCK. If the second argument is SHARED_LOCK then this routine
34209c105bb9Sdrh ** might return SQLITE_IOERR;
3421bbd42a6dSdrh */
3422153c62c4Sdrh static int winUnlock(sqlite3_file *id, int locktype){
34239c105bb9Sdrh int type;
3424054889ecSdrh winFile *pFile = (winFile*)id;
3425153c62c4Sdrh int rc = SQLITE_OK;
3426054889ecSdrh assert( pFile!=0 );
3427a6abd041Sdrh assert( locktype<=SHARED_LOCK );
3428f2c1c99fSmistachkin OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
3429f2c1c99fSmistachkin pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
3430054889ecSdrh type = pFile->locktype;
3431e54ca3feSdrh if( type>=EXCLUSIVE_LOCK ){
3432a749486eSmistachkin winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
3433b324bc79Smistachkin if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){
34349c105bb9Sdrh /* This should never happen. We should always be able to
34359c105bb9Sdrh ** reacquire the read lock */
3436318507b7Smistachkin rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
34372aef997cSmistachkin "winUnlock", pFile->zPath);
34389c105bb9Sdrh }
3439bbd42a6dSdrh }
3440e54ca3feSdrh if( type>=RESERVED_LOCK ){
3441a749486eSmistachkin winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
344251c6d963Sdrh }
34439c105bb9Sdrh if( locktype==NO_LOCK && type>=SHARED_LOCK ){
3444b324bc79Smistachkin winUnlockReadLock(pFile);
344551c6d963Sdrh }
3446b3e04346Sdrh if( type>=PENDING_LOCK ){
3447a749486eSmistachkin winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
3448b3e04346Sdrh }
34491bd10f8aSdrh pFile->locktype = (u8)locktype;
3450f2c1c99fSmistachkin OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n",
3451f2c1c99fSmistachkin pFile->h, pFile->locktype, sqlite3ErrName(rc)));
34529c105bb9Sdrh return rc;
3453bbd42a6dSdrh }
3454bbd42a6dSdrh
34551e75483cSmistachkin /******************************************************************************
34561e75483cSmistachkin ****************************** No-op Locking **********************************
34571e75483cSmistachkin **
34581e75483cSmistachkin ** Of the various locking implementations available, this is by far the
34591e75483cSmistachkin ** simplest: locking is ignored. No attempt is made to lock the database
34601e75483cSmistachkin ** file for reading or writing.
34611e75483cSmistachkin **
34621e75483cSmistachkin ** This locking mode is appropriate for use on read-only databases
34631e75483cSmistachkin ** (ex: databases that are burned into CD-ROM, for example.) It can
34641e75483cSmistachkin ** also be used if the application employs some external mechanism to
34651e75483cSmistachkin ** prevent simultaneous access of the same database by two or more
34661e75483cSmistachkin ** database connections. But there is a serious risk of database
34671e75483cSmistachkin ** corruption if this locking mode is used in situations where multiple
34681e75483cSmistachkin ** database connections are accessing the same database file at the same
34691e75483cSmistachkin ** time and one or more of those connections are writing.
34701e75483cSmistachkin */
34711e75483cSmistachkin
34721e75483cSmistachkin static int winNolockLock(sqlite3_file *id, int locktype){
34731e75483cSmistachkin UNUSED_PARAMETER(id);
34741e75483cSmistachkin UNUSED_PARAMETER(locktype);
34751e75483cSmistachkin return SQLITE_OK;
34761e75483cSmistachkin }
34771e75483cSmistachkin
34781e75483cSmistachkin static int winNolockCheckReservedLock(sqlite3_file *id, int *pResOut){
34791e75483cSmistachkin UNUSED_PARAMETER(id);
34801e75483cSmistachkin UNUSED_PARAMETER(pResOut);
34811e75483cSmistachkin return SQLITE_OK;
34821e75483cSmistachkin }
34831e75483cSmistachkin
34841e75483cSmistachkin static int winNolockUnlock(sqlite3_file *id, int locktype){
34851e75483cSmistachkin UNUSED_PARAMETER(id);
34861e75483cSmistachkin UNUSED_PARAMETER(locktype);
34871e75483cSmistachkin return SQLITE_OK;
34881e75483cSmistachkin }
34891e75483cSmistachkin
34901e75483cSmistachkin /******************* End of the no-op lock implementation *********************
34911e75483cSmistachkin ******************************************************************************/
34921e75483cSmistachkin
3493bbd42a6dSdrh /*
349460ec914cSpeter.d.reid ** If *pArg is initially negative then this is a query. Set *pArg to
3495f12b3f60Sdrh ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
3496f12b3f60Sdrh **
3497f12b3f60Sdrh ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
3498f12b3f60Sdrh */
3499f12b3f60Sdrh static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
3500f12b3f60Sdrh if( *pArg<0 ){
3501f12b3f60Sdrh *pArg = (pFile->ctrlFlags & mask)!=0;
3502f12b3f60Sdrh }else if( (*pArg)==0 ){
3503f12b3f60Sdrh pFile->ctrlFlags &= ~mask;
3504f12b3f60Sdrh }else{
3505f12b3f60Sdrh pFile->ctrlFlags |= mask;
3506f12b3f60Sdrh }
3507f12b3f60Sdrh }
3508f12b3f60Sdrh
3509d95a3d35Smistachkin /* Forward references to VFS helper methods used for temporary files */
35103741827eSmistachkin static int winGetTempname(sqlite3_vfs *, char **);
3511d95a3d35Smistachkin static int winIsDir(const void *);
3512a8e41ecaSmistachkin static BOOL winIsLongPathPrefix(const char *);
3513d95a3d35Smistachkin static BOOL winIsDriveLetterAndColon(const char *);
3514696b33e6Sdrh
3515f12b3f60Sdrh /*
35169e33c2c1Sdrh ** Control and query of the open file handle.
35170ccebe7eSdrh */
35189e33c2c1Sdrh static int winFileControl(sqlite3_file *id, int op, void *pArg){
3519f0b190d9Sdrh winFile *pFile = (winFile*)id;
3520f2c1c99fSmistachkin OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
35219e33c2c1Sdrh switch( op ){
35229e33c2c1Sdrh case SQLITE_FCNTL_LOCKSTATE: {
3523f0b190d9Sdrh *(int*)pArg = pFile->locktype;
3524f2c1c99fSmistachkin OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
35259e33c2c1Sdrh return SQLITE_OK;
35269cbe6352Sdrh }
3527b7b91068Smistachkin case SQLITE_FCNTL_LAST_ERRNO: {
3528f0b190d9Sdrh *(int*)pArg = (int)pFile->lastErrno;
3529f2c1c99fSmistachkin OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
35309db299fbSshane return SQLITE_OK;
35319db299fbSshane }
3532502019c8Sdan case SQLITE_FCNTL_CHUNK_SIZE: {
3533f0b190d9Sdrh pFile->szChunk = *(int *)pArg;
3534f2c1c99fSmistachkin OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
3535502019c8Sdan return SQLITE_OK;
3536502019c8Sdan }
3537f5d6e478Sdrh case SQLITE_FCNTL_SIZE_HINT: {
3538d589a544Smistachkin if( pFile->szChunk>0 ){
35394458bc8eSmistachkin sqlite3_int64 oldSz;
35404458bc8eSmistachkin int rc = winFileSize(id, &oldSz);
35414458bc8eSmistachkin if( rc==SQLITE_OK ){
35424458bc8eSmistachkin sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
35434458bc8eSmistachkin if( newSz>oldSz ){
3544e2ad9317Sshaneh SimulateIOErrorBenign(1);
35454458bc8eSmistachkin rc = winTruncate(id, newSz);
3546e2ad9317Sshaneh SimulateIOErrorBenign(0);
35474458bc8eSmistachkin }
35484458bc8eSmistachkin }
3549f2c1c99fSmistachkin OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
35504458bc8eSmistachkin return rc;
3551f5d6e478Sdrh }
3552f2c1c99fSmistachkin OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
3553d589a544Smistachkin return SQLITE_OK;
3554d589a544Smistachkin }
3555f0b190d9Sdrh case SQLITE_FCNTL_PERSIST_WAL: {
3556f12b3f60Sdrh winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
3557f2c1c99fSmistachkin OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
3558f12b3f60Sdrh return SQLITE_OK;
3559f0b190d9Sdrh }
3560cb15f35fSdrh case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
3561cb15f35fSdrh winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
3562f2c1c99fSmistachkin OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
3563f0b190d9Sdrh return SQLITE_OK;
3564f0b190d9Sdrh }
3565de60fc2dSdrh case SQLITE_FCNTL_VFSNAME: {
356640138755Smistachkin *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
3567f2c1c99fSmistachkin OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
3568de60fc2dSdrh return SQLITE_OK;
3569de60fc2dSdrh }
3570d0cdf012Sdrh case SQLITE_FCNTL_WIN32_AV_RETRY: {
3571d0cdf012Sdrh int *a = (int*)pArg;
3572d0cdf012Sdrh if( a[0]>0 ){
3573b324bc79Smistachkin winIoerrRetry = a[0];
3574d0cdf012Sdrh }else{
3575b324bc79Smistachkin a[0] = winIoerrRetry;
3576d0cdf012Sdrh }
3577d0cdf012Sdrh if( a[1]>0 ){
3578b324bc79Smistachkin winIoerrRetryDelay = a[1];
3579d0cdf012Sdrh }else{
3580b324bc79Smistachkin a[1] = winIoerrRetryDelay;
3581d0cdf012Sdrh }
3582f2c1c99fSmistachkin OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
3583d0cdf012Sdrh return SQLITE_OK;
3584d0cdf012Sdrh }
35851b361ff3Smistachkin case SQLITE_FCNTL_WIN32_GET_HANDLE: {
35861b361ff3Smistachkin LPHANDLE phFile = (LPHANDLE)pArg;
35871b361ff3Smistachkin *phFile = pFile->h;
35881b361ff3Smistachkin OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
35891b361ff3Smistachkin return SQLITE_OK;
35901b361ff3Smistachkin }
35916b98d67bSmistachkin #ifdef SQLITE_TEST
35926b98d67bSmistachkin case SQLITE_FCNTL_WIN32_SET_HANDLE: {
35936b98d67bSmistachkin LPHANDLE phFile = (LPHANDLE)pArg;
35946b98d67bSmistachkin HANDLE hOldFile = pFile->h;
35956b98d67bSmistachkin pFile->h = *phFile;
35966b98d67bSmistachkin *phFile = hOldFile;
35976b98d67bSmistachkin OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n",
35986b98d67bSmistachkin hOldFile, pFile->h));
35996b98d67bSmistachkin return SQLITE_OK;
36006b98d67bSmistachkin }
36016b98d67bSmistachkin #endif
3602696b33e6Sdrh case SQLITE_FCNTL_TEMPFILENAME: {
36033741827eSmistachkin char *zTFile = 0;
36043741827eSmistachkin int rc = winGetTempname(pFile->pVfs, &zTFile);
36053741827eSmistachkin if( rc==SQLITE_OK ){
3606696b33e6Sdrh *(char**)pArg = zTFile;
36070b52b7d0Sdrh }
36089f11ef12Smistachkin OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
36093741827eSmistachkin return rc;
3610696b33e6Sdrh }
36115824e053Smistachkin #if SQLITE_MAX_MMAP_SIZE>0
36129b4c59faSdrh case SQLITE_FCNTL_MMAP_SIZE: {
361334f74903Sdrh i64 newLimit = *(i64*)pArg;
361434e258c9Sdrh int rc = SQLITE_OK;
36159b4c59faSdrh if( newLimit>sqlite3GlobalConfig.mxMmap ){
36169b4c59faSdrh newLimit = sqlite3GlobalConfig.mxMmap;
36179b4c59faSdrh }
3618e35395a4Smistachkin
3619e35395a4Smistachkin /* The value of newLimit may be eventually cast to (SIZE_T) and passed
3620e35395a4Smistachkin ** to MapViewOfFile(). Restrict its value to 2GB if (SIZE_T) is not at
3621e35395a4Smistachkin ** least a 64-bit type. */
3622e35395a4Smistachkin if( newLimit>0 && sizeof(SIZE_T)<8 ){
3623e35395a4Smistachkin newLimit = (newLimit & 0x7FFFFFFF);
3624e35395a4Smistachkin }
3625e35395a4Smistachkin
36269b4c59faSdrh *(i64*)pArg = pFile->mmapSizeMax;
362734e258c9Sdrh if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
362834e258c9Sdrh pFile->mmapSizeMax = newLimit;
362934e258c9Sdrh if( pFile->mmapSize>0 ){
3630c216566bSmistachkin winUnmapfile(pFile);
363134e258c9Sdrh rc = winMapfile(pFile, -1);
363234e258c9Sdrh }
363334e258c9Sdrh }
36349f11ef12Smistachkin OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
363534e258c9Sdrh return rc;
36365175b324Sdrh }
36375824e053Smistachkin #endif
36380b52b7d0Sdrh }
3639f2c1c99fSmistachkin OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
36400b52b7d0Sdrh return SQLITE_NOTFOUND;
364118839217Sdrh }
364218839217Sdrh
364318839217Sdrh /*
3644a3d4c887Sdanielk1977 ** Return the sector size in bytes of the underlying block device for
3645a3d4c887Sdanielk1977 ** the specified file. This is almost always 512 bytes, but may be
3646a3d4c887Sdanielk1977 ** larger for some devices.
3647a3d4c887Sdanielk1977 **
3648a3d4c887Sdanielk1977 ** SQLite code assumes this function cannot fail. It also assumes that
3649a3d4c887Sdanielk1977 ** if two files are created in the same file-system directory (i.e.
365085b623f2Sdrh ** a database and its journal file) that the sector size will be the
3651a3d4c887Sdanielk1977 ** same for both.
3652a3d4c887Sdanielk1977 */
3653153c62c4Sdrh static int winSectorSize(sqlite3_file *id){
36548942d412Sdrh (void)id;
36558942d412Sdrh return SQLITE_DEFAULT_SECTOR_SIZE;
3656a3d4c887Sdanielk1977 }
3657a3d4c887Sdanielk1977
3658a3d4c887Sdanielk1977 /*
3659153c62c4Sdrh ** Return a vector of device characteristics.
36609c06c953Sdrh */
3661153c62c4Sdrh static int winDeviceCharacteristics(sqlite3_file *id){
3662f12b3f60Sdrh winFile *p = (winFile*)id;
36638bbaa89dSdrh return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
3664cb15f35fSdrh ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
3665153c62c4Sdrh }
3666153c62c4Sdrh
366783235214Sdrh /*
3668420398ceSshaneh ** Windows will only let you create file view mappings
3669420398ceSshaneh ** on allocation size granularity boundaries.
3670420398ceSshaneh ** During sqlite3_os_init() we do a GetSystemInfo()
3671420398ceSshaneh ** to get the granularity size.
3672420398ceSshaneh */
36735dfd3d90Smistachkin static SYSTEM_INFO winSysInfo;
3674420398ceSshaneh
36756e8752d4Smistachkin #ifndef SQLITE_OMIT_WAL
3676420398ceSshaneh
3677420398ceSshaneh /*
367883235214Sdrh ** Helper functions to obtain and relinquish the global mutex. The
367983235214Sdrh ** global mutex is used to protect the winLockInfo objects used by
368083235214Sdrh ** this file, all of which may be shared by multiple threads.
368183235214Sdrh **
368283235214Sdrh ** Function winShmMutexHeld() is used to assert() that the global mutex
368383235214Sdrh ** is held when required. This function is only used as part of assert()
368483235214Sdrh ** statements. e.g.
368583235214Sdrh **
368683235214Sdrh ** winShmEnterMutex()
368783235214Sdrh ** assert( winShmMutexHeld() );
3688d5a72400Sshaneh ** winShmLeaveMutex()
368983235214Sdrh */
3690435666e2Smistachkin static sqlite3_mutex *winBigLock = 0;
369183235214Sdrh static void winShmEnterMutex(void){
3692435666e2Smistachkin sqlite3_mutex_enter(winBigLock);
369383235214Sdrh }
369483235214Sdrh static void winShmLeaveMutex(void){
3695435666e2Smistachkin sqlite3_mutex_leave(winBigLock);
369683235214Sdrh }
3697866b53ebSmistachkin #ifndef NDEBUG
369883235214Sdrh static int winShmMutexHeld(void) {
3699435666e2Smistachkin return sqlite3_mutex_held(winBigLock);
370083235214Sdrh }
370183235214Sdrh #endif
370283235214Sdrh
370383235214Sdrh /*
370483235214Sdrh ** Object used to represent a single file opened and mmapped to provide
370583235214Sdrh ** shared memory. When multiple threads all reference the same
370683235214Sdrh ** log-summary, each thread has its own winFile object, but they all
370783235214Sdrh ** point to a single instance of this object. In other words, each
370883235214Sdrh ** log-summary is opened only once per process.
370983235214Sdrh **
371083235214Sdrh ** winShmMutexHeld() must be true when creating or destroying
371183235214Sdrh ** this object or while reading or writing the following fields:
371283235214Sdrh **
371383235214Sdrh ** nRef
371483235214Sdrh ** pNext
371583235214Sdrh **
371683235214Sdrh ** The following fields are read-only after the object is created:
371783235214Sdrh **
371883235214Sdrh ** fid
371983235214Sdrh ** zFilename
372083235214Sdrh **
372183235214Sdrh ** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
372283235214Sdrh ** winShmMutexHeld() is true when reading or writing any other field
372383235214Sdrh ** in this structure.
372483235214Sdrh **
372583235214Sdrh */
372683235214Sdrh struct winShmNode {
372783235214Sdrh sqlite3_mutex *mutex; /* Mutex to access this object */
372883235214Sdrh char *zFilename; /* Name of the file */
372983235214Sdrh winFile hFile; /* File handle from winOpen */
37309785fc95Sdan
37319785fc95Sdan int szRegion; /* Size of shared-memory regions */
37329785fc95Sdan int nRegion; /* Size of array apRegion */
37334ff8431fSmistachkin u8 isReadonly; /* True if read-only */
37344ff8431fSmistachkin u8 isUnlocked; /* True if no DMS lock held */
37354ff8431fSmistachkin
37369785fc95Sdan struct ShmRegion {
373783235214Sdrh HANDLE hMap; /* File handle from CreateFileMapping */
37389785fc95Sdan void *pMap;
37399785fc95Sdan } *aRegion;
374083235214Sdrh DWORD lastErrno; /* The Windows errno from the last I/O error */
37419785fc95Sdan
374283235214Sdrh int nRef; /* Number of winShm objects pointing to this */
374383235214Sdrh winShm *pFirst; /* All winShm objects pointing to this */
374483235214Sdrh winShmNode *pNext; /* Next in list of all winShmNode objects */
3745fb383e92Smistachkin #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
374683235214Sdrh u8 nextShmId; /* Next available winShm.id value */
374783235214Sdrh #endif
374883235214Sdrh };
374983235214Sdrh
375083235214Sdrh /*
375183235214Sdrh ** A global array of all winShmNode objects.
375283235214Sdrh **
375383235214Sdrh ** The winShmMutexHeld() must be true while reading or writing this list.
375483235214Sdrh */
375583235214Sdrh static winShmNode *winShmNodeList = 0;
375683235214Sdrh
375783235214Sdrh /*
375883235214Sdrh ** Structure used internally by this VFS to record the state of an
375983235214Sdrh ** open shared memory connection.
376083235214Sdrh **
37611f3e27b2Sshaneh ** The following fields are initialized when this object is created and
37621f3e27b2Sshaneh ** are read-only thereafter:
376383235214Sdrh **
37641f3e27b2Sshaneh ** winShm.pShmNode
37651f3e27b2Sshaneh ** winShm.id
37661f3e27b2Sshaneh **
37671f3e27b2Sshaneh ** All other fields are read/write. The winShm.pShmNode->mutex must be held
37681f3e27b2Sshaneh ** while accessing any read/write fields.
376983235214Sdrh */
377083235214Sdrh struct winShm {
377183235214Sdrh winShmNode *pShmNode; /* The underlying winShmNode object */
377283235214Sdrh winShm *pNext; /* Next winShm with the same winShmNode */
377383235214Sdrh u8 hasMutex; /* True if holding the winShmNode mutex */
37741f3e27b2Sshaneh u16 sharedMask; /* Mask of shared locks held */
37751f3e27b2Sshaneh u16 exclMask; /* Mask of exclusive locks held */
3776fb383e92Smistachkin #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
377783235214Sdrh u8 id; /* Id of this connection with its winShmNode */
377883235214Sdrh #endif
377983235214Sdrh };
378083235214Sdrh
378183235214Sdrh /*
378283235214Sdrh ** Constants used for locking
378383235214Sdrh */
3784bd9676c1Sdrh #define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
378520e1f08eSdrh #define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
378683235214Sdrh
378783235214Sdrh /*
378820e1f08eSdrh ** Apply advisory locks for all n bytes beginning at ofst.
378983235214Sdrh */
379043f58d6aSdrh #define WINSHM_UNLCK 1
379143f58d6aSdrh #define WINSHM_RDLCK 2
379243f58d6aSdrh #define WINSHM_WRLCK 3
379383235214Sdrh static int winShmSystemLock(
379483235214Sdrh winShmNode *pFile, /* Apply locks to this open shared-memory segment */
379543f58d6aSdrh int lockType, /* WINSHM_UNLCK, WINSHM_RDLCK, or WINSHM_WRLCK */
379620e1f08eSdrh int ofst, /* Offset to first byte to be locked/unlocked */
379720e1f08eSdrh int nByte /* Number of bytes to lock or unlock */
379883235214Sdrh ){
379983235214Sdrh int rc = 0; /* Result code form Lock/UnlockFileEx() */
380083235214Sdrh
380183235214Sdrh /* Access to the winShmNode object is serialized by the caller */
380237874b5fSdrh assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) );
380383235214Sdrh
3804f2c1c99fSmistachkin OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
3805f2c1c99fSmistachkin pFile->hFile.h, lockType, ofst, nByte));
3806f2c1c99fSmistachkin
380783235214Sdrh /* Release/Acquire the system-level lock */
380843f58d6aSdrh if( lockType==WINSHM_UNLCK ){
3809a749486eSmistachkin rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
381083235214Sdrh }else{
38112a5cfb31Smistachkin /* Initialize the locking parameters */
38122a5cfb31Smistachkin DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
381343f58d6aSdrh if( lockType == WINSHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
3814a749486eSmistachkin rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
381583235214Sdrh }
3816d5a72400Sshaneh
3817d5a72400Sshaneh if( rc!= 0 ){
3818d5a72400Sshaneh rc = SQLITE_OK;
3819d5a72400Sshaneh }else{
3820318507b7Smistachkin pFile->lastErrno = osGetLastError();
3821d5a72400Sshaneh rc = SQLITE_BUSY;
3822d5a72400Sshaneh }
38231f3e27b2Sshaneh
3824e84d8d32Smistachkin OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
382543f58d6aSdrh pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
3826e84d8d32Smistachkin "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
382783235214Sdrh
382883235214Sdrh return rc;
382983235214Sdrh }
383083235214Sdrh
383105cb5b24Sdrh /* Forward references to VFS methods */
383205cb5b24Sdrh static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
383305cb5b24Sdrh static int winDelete(sqlite3_vfs *,const char*,int);
383405cb5b24Sdrh
383583235214Sdrh /*
383683235214Sdrh ** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
383783235214Sdrh **
383883235214Sdrh ** This is not a VFS shared-memory method; it is a utility function called
383983235214Sdrh ** by VFS shared-memory methods.
384083235214Sdrh */
384105cb5b24Sdrh static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
384283235214Sdrh winShmNode **pp;
384383235214Sdrh winShmNode *p;
384483235214Sdrh assert( winShmMutexHeld() );
3845e84d8d32Smistachkin OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
3846e84d8d32Smistachkin osGetCurrentProcessId(), deleteFlag));
384783235214Sdrh pp = &winShmNodeList;
384883235214Sdrh while( (p = *pp)!=0 ){
384983235214Sdrh if( p->nRef==0 ){
38509785fc95Sdan int i;
38510353ed21Sdrh if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
38529785fc95Sdan for(i=0; i<p->nRegion; i++){
385336ca5359Smistachkin BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
3854e84d8d32Smistachkin OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
3855e84d8d32Smistachkin osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
385636ca5359Smistachkin UNUSED_VARIABLE_VALUE(bRc);
3857318507b7Smistachkin bRc = osCloseHandle(p->aRegion[i].hMap);
3858e84d8d32Smistachkin OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
3859e84d8d32Smistachkin osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
386036ca5359Smistachkin UNUSED_VARIABLE_VALUE(bRc);
386183235214Sdrh }
38624600255aSmistachkin if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
3863e2ad9317Sshaneh SimulateIOErrorBenign(1);
386483235214Sdrh winClose((sqlite3_file *)&p->hFile);
3865e2ad9317Sshaneh SimulateIOErrorBenign(0);
386683235214Sdrh }
3867e2ad9317Sshaneh if( deleteFlag ){
3868e2ad9317Sshaneh SimulateIOErrorBenign(1);
386992c45cf0Sdrh sqlite3BeginBenignMalloc();
3870e2ad9317Sshaneh winDelete(pVfs, p->zFilename, 0);
387192c45cf0Sdrh sqlite3EndBenignMalloc();
3872e2ad9317Sshaneh SimulateIOErrorBenign(0);
3873e2ad9317Sshaneh }
387483235214Sdrh *pp = p->pNext;
38759785fc95Sdan sqlite3_free(p->aRegion);
387683235214Sdrh sqlite3_free(p);
387783235214Sdrh }else{
387883235214Sdrh pp = &p->pNext;
387983235214Sdrh }
388083235214Sdrh }
388183235214Sdrh }
388283235214Sdrh
388383235214Sdrh /*
38844ff8431fSmistachkin ** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
38854ff8431fSmistachkin ** take it now. Return SQLITE_OK if successful, or an SQLite error
38864ff8431fSmistachkin ** code otherwise.
38874ff8431fSmistachkin **
38884ff8431fSmistachkin ** If the DMS cannot be locked because this is a readonly_shm=1
38894ff8431fSmistachkin ** connection and no other process already holds a lock, return
38904ff8431fSmistachkin ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
38914ff8431fSmistachkin */
38928a6fa5d4Smistachkin static int winLockSharedMemory(winShmNode *pShmNode){
38937b7f224cSmistachkin int rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1);
38940e026f40Smistachkin
38957b7f224cSmistachkin if( rc==SQLITE_OK ){
38964ff8431fSmistachkin if( pShmNode->isReadonly ){
38974ff8431fSmistachkin pShmNode->isUnlocked = 1;
38984ff8431fSmistachkin winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
38997b7f224cSmistachkin return SQLITE_READONLY_CANTINIT;
39007b7f224cSmistachkin }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
39017b7f224cSmistachkin winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
39027b7f224cSmistachkin return winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
39034ff8431fSmistachkin "winLockSharedMemory", pShmNode->zFilename);
39044ff8431fSmistachkin }
39054ff8431fSmistachkin }
39064ff8431fSmistachkin
39070e026f40Smistachkin if( rc==SQLITE_OK ){
39084ff8431fSmistachkin winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
39090e026f40Smistachkin }
39100e026f40Smistachkin
39117b7f224cSmistachkin return winShmSystemLock(pShmNode, WINSHM_RDLCK, WIN_SHM_DMS, 1);
39124ff8431fSmistachkin }
39134ff8431fSmistachkin
39144ff8431fSmistachkin /*
3915da9fe0c3Sdan ** Open the shared-memory area associated with database file pDbFd.
391683235214Sdrh **
391783235214Sdrh ** When opening a new shared-memory file, if no other instances of that
391883235214Sdrh ** file are currently open, in this process or in other processes, then
391983235214Sdrh ** the file must be truncated to zero length or have its header cleared.
392083235214Sdrh */
3921da9fe0c3Sdan static int winOpenSharedMemory(winFile *pDbFd){
392283235214Sdrh struct winShm *p; /* The connection to be opened */
39234ff8431fSmistachkin winShmNode *pShmNode = 0; /* The underlying mmapped file */
39244ff8431fSmistachkin int rc = SQLITE_OK; /* Result code */
39254ff8431fSmistachkin winShmNode *pNew; /* Newly allocated winShmNode */
392683235214Sdrh int nName; /* Size of zName in bytes */
392783235214Sdrh
392883235214Sdrh assert( pDbFd->pShm==0 ); /* Not previously opened */
392983235214Sdrh
393083235214Sdrh /* Allocate space for the new sqlite3_shm object. Also speculatively
393183235214Sdrh ** allocate space for a new winShmNode and filename.
393283235214Sdrh */
39337ea11af2Smistachkin p = sqlite3MallocZero( sizeof(*p) );
3934fad3039cSmistachkin if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT;
393583235214Sdrh nName = sqlite3Strlen30(pDbFd->zPath);
39367ea11af2Smistachkin pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
393783235214Sdrh if( pNew==0 ){
393883235214Sdrh sqlite3_free(p);
3939fad3039cSmistachkin return SQLITE_IOERR_NOMEM_BKPT;
394083235214Sdrh }
394183235214Sdrh pNew->zFilename = (char*)&pNew[1];
3942d36f660dSdrh sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
394381cc5163Sdrh sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
394483235214Sdrh
394583235214Sdrh /* Look to see if there is an existing winShmNode that can be used.
394683235214Sdrh ** If no matching winShmNode currently exists, create a new one.
394783235214Sdrh */
394883235214Sdrh winShmEnterMutex();
394983235214Sdrh for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
395083235214Sdrh /* TBD need to come up with better match here. Perhaps
395183235214Sdrh ** use FILE_ID_BOTH_DIR_INFO Structure.
395283235214Sdrh */
395383235214Sdrh if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
395483235214Sdrh }
395583235214Sdrh if( pShmNode ){
395683235214Sdrh sqlite3_free(pNew);
395783235214Sdrh }else{
395898e2cb8bSmistachkin int inFlags = SQLITE_OPEN_WAL;
395998e2cb8bSmistachkin int outFlags = 0;
39605685257bSmistachkin
396183235214Sdrh pShmNode = pNew;
396283235214Sdrh pNew = 0;
396383235214Sdrh ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
396483235214Sdrh pShmNode->pNext = winShmNodeList;
396583235214Sdrh winShmNodeList = pShmNode;
396683235214Sdrh
396797a7e5e6Sdrh if( sqlite3GlobalConfig.bCoreMutex ){
396883235214Sdrh pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
396983235214Sdrh if( pShmNode->mutex==0 ){
3970fad3039cSmistachkin rc = SQLITE_IOERR_NOMEM_BKPT;
397183235214Sdrh goto shm_open_err;
397283235214Sdrh }
397397a7e5e6Sdrh }
3974420398ceSshaneh
397598e2cb8bSmistachkin if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
397698e2cb8bSmistachkin inFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
397798e2cb8bSmistachkin }else{
397898e2cb8bSmistachkin inFlags |= SQLITE_OPEN_READONLY;
39794ff8431fSmistachkin }
398098e2cb8bSmistachkin rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
39814ff8431fSmistachkin (sqlite3_file*)&pShmNode->hFile,
398298e2cb8bSmistachkin inFlags, &outFlags);
398398e2cb8bSmistachkin if( rc!=SQLITE_OK ){
398498e2cb8bSmistachkin rc = winLogError(rc, osGetLastError(), "winOpenShm",
3985bcb416a9Smistachkin pShmNode->zFilename);
398683235214Sdrh goto shm_open_err;
398783235214Sdrh }
398898e2cb8bSmistachkin if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
398983235214Sdrh
39908a6fa5d4Smistachkin rc = winLockSharedMemory(pShmNode);
39914ff8431fSmistachkin if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
399283235214Sdrh }
399383235214Sdrh
399483235214Sdrh /* Make the new connection a child of the winShmNode */
399583235214Sdrh p->pShmNode = pShmNode;
3996fb383e92Smistachkin #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
399783235214Sdrh p->id = pShmNode->nextShmId++;
399883235214Sdrh #endif
399983235214Sdrh pShmNode->nRef++;
400083235214Sdrh pDbFd->pShm = p;
400183235214Sdrh winShmLeaveMutex();
4002d5a72400Sshaneh
4003d5a72400Sshaneh /* The reference count on pShmNode has already been incremented under
4004d5a72400Sshaneh ** the cover of the winShmEnterMutex() mutex and the pointer from the
4005d5a72400Sshaneh ** new (struct winShm) object to the pShmNode has been set. All that is
4006d5a72400Sshaneh ** left to do is to link the new object into the linked list starting
4007d5a72400Sshaneh ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
4008d5a72400Sshaneh ** mutex.
4009d5a72400Sshaneh */
4010d5a72400Sshaneh sqlite3_mutex_enter(pShmNode->mutex);
4011d5a72400Sshaneh p->pNext = pShmNode->pFirst;
4012d5a72400Sshaneh pShmNode->pFirst = p;
4013d5a72400Sshaneh sqlite3_mutex_leave(pShmNode->mutex);
40148a6fa5d4Smistachkin return rc;
401583235214Sdrh
401683235214Sdrh /* Jump here on any error */
401783235214Sdrh shm_open_err:
401843f58d6aSdrh winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
401905cb5b24Sdrh winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
402083235214Sdrh sqlite3_free(p);
402183235214Sdrh sqlite3_free(pNew);
402283235214Sdrh winShmLeaveMutex();
402383235214Sdrh return rc;
402483235214Sdrh }
402583235214Sdrh
402683235214Sdrh /*
402783235214Sdrh ** Close a connection to shared-memory. Delete the underlying
402883235214Sdrh ** storage if deleteFlag is true.
402983235214Sdrh */
4030e11fedc5Sdrh static int winShmUnmap(
403183235214Sdrh sqlite3_file *fd, /* Database holding shared memory */
403283235214Sdrh int deleteFlag /* Delete after closing if true */
403383235214Sdrh ){
403483235214Sdrh winFile *pDbFd; /* Database holding shared-memory */
403583235214Sdrh winShm *p; /* The connection to be closed */
403683235214Sdrh winShmNode *pShmNode; /* The underlying shared-memory file */
403783235214Sdrh winShm **pp; /* For looping over sibling connections */
403883235214Sdrh
403983235214Sdrh pDbFd = (winFile*)fd;
404083235214Sdrh p = pDbFd->pShm;
40411f3e27b2Sshaneh if( p==0 ) return SQLITE_OK;
404283235214Sdrh pShmNode = p->pShmNode;
404383235214Sdrh
404483235214Sdrh /* Remove connection p from the set of connections associated
404583235214Sdrh ** with pShmNode */
404683235214Sdrh sqlite3_mutex_enter(pShmNode->mutex);
404783235214Sdrh for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
404883235214Sdrh *pp = p->pNext;
404983235214Sdrh
405083235214Sdrh /* Free the connection p */
405183235214Sdrh sqlite3_free(p);
405283235214Sdrh pDbFd->pShm = 0;
405383235214Sdrh sqlite3_mutex_leave(pShmNode->mutex);
405483235214Sdrh
405583235214Sdrh /* If pShmNode->nRef has reached 0, then close the underlying
405683235214Sdrh ** shared-memory file, too */
405783235214Sdrh winShmEnterMutex();
405883235214Sdrh assert( pShmNode->nRef>0 );
405983235214Sdrh pShmNode->nRef--;
406083235214Sdrh if( pShmNode->nRef==0 ){
406105cb5b24Sdrh winShmPurge(pDbFd->pVfs, deleteFlag);
406283235214Sdrh }
406383235214Sdrh winShmLeaveMutex();
406483235214Sdrh
406583235214Sdrh return SQLITE_OK;
406683235214Sdrh }
406783235214Sdrh
406883235214Sdrh /*
40691f3e27b2Sshaneh ** Change the lock state for a shared-memory segment.
40701f3e27b2Sshaneh */
40711f3e27b2Sshaneh static int winShmLock(
40721f3e27b2Sshaneh sqlite3_file *fd, /* Database file holding the shared memory */
40731f3e27b2Sshaneh int ofst, /* First lock to acquire or release */
40741f3e27b2Sshaneh int n, /* Number of locks to acquire or release */
40751f3e27b2Sshaneh int flags /* What to do with the lock */
40761f3e27b2Sshaneh ){
40771f3e27b2Sshaneh winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */
40781f3e27b2Sshaneh winShm *p = pDbFd->pShm; /* The shared memory being locked */
40791f3e27b2Sshaneh winShm *pX; /* For looping over all siblings */
408056d88aadSdrh winShmNode *pShmNode;
40811f3e27b2Sshaneh int rc = SQLITE_OK; /* Result code */
40821f3e27b2Sshaneh u16 mask; /* Mask of locks to take or release */
40831f3e27b2Sshaneh
408456d88aadSdrh if( p==0 ) return SQLITE_IOERR_SHMLOCK;
408556d88aadSdrh pShmNode = p->pShmNode;
408656d88aadSdrh if( NEVER(pShmNode==0) ) return SQLITE_IOERR_SHMLOCK;
408756d88aadSdrh
40881f3e27b2Sshaneh assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
40891f3e27b2Sshaneh assert( n>=1 );
40901f3e27b2Sshaneh assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
40911f3e27b2Sshaneh || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
40921f3e27b2Sshaneh || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
40931f3e27b2Sshaneh || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
40941f3e27b2Sshaneh assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
40951f3e27b2Sshaneh
40961f3e27b2Sshaneh mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
40971f3e27b2Sshaneh assert( n>1 || mask==(1<<ofst) );
40981f3e27b2Sshaneh sqlite3_mutex_enter(pShmNode->mutex);
40991f3e27b2Sshaneh if( flags & SQLITE_SHM_UNLOCK ){
41001f3e27b2Sshaneh u16 allMask = 0; /* Mask of locks held by siblings */
41011f3e27b2Sshaneh
41021f3e27b2Sshaneh /* See if any siblings hold this same lock */
41031f3e27b2Sshaneh for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
41041f3e27b2Sshaneh if( pX==p ) continue;
41051f3e27b2Sshaneh assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
41061f3e27b2Sshaneh allMask |= pX->sharedMask;
41071f3e27b2Sshaneh }
41081f3e27b2Sshaneh
41091f3e27b2Sshaneh /* Unlock the system-level locks */
41101f3e27b2Sshaneh if( (mask & allMask)==0 ){
411143f58d6aSdrh rc = winShmSystemLock(pShmNode, WINSHM_UNLCK, ofst+WIN_SHM_BASE, n);
41121f3e27b2Sshaneh }else{
41131f3e27b2Sshaneh rc = SQLITE_OK;
41141f3e27b2Sshaneh }
41151f3e27b2Sshaneh
41161f3e27b2Sshaneh /* Undo the local locks */
41171f3e27b2Sshaneh if( rc==SQLITE_OK ){
41181f3e27b2Sshaneh p->exclMask &= ~mask;
41191f3e27b2Sshaneh p->sharedMask &= ~mask;
41201f3e27b2Sshaneh }
41211f3e27b2Sshaneh }else if( flags & SQLITE_SHM_SHARED ){
41221f3e27b2Sshaneh u16 allShared = 0; /* Union of locks held by connections other than "p" */
41231f3e27b2Sshaneh
41241f3e27b2Sshaneh /* Find out which shared locks are already held by sibling connections.
41251f3e27b2Sshaneh ** If any sibling already holds an exclusive lock, go ahead and return
41261f3e27b2Sshaneh ** SQLITE_BUSY.
41271f3e27b2Sshaneh */
41281f3e27b2Sshaneh for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
41291f3e27b2Sshaneh if( (pX->exclMask & mask)!=0 ){
41301f3e27b2Sshaneh rc = SQLITE_BUSY;
41311f3e27b2Sshaneh break;
41321f3e27b2Sshaneh }
41331f3e27b2Sshaneh allShared |= pX->sharedMask;
41341f3e27b2Sshaneh }
41351f3e27b2Sshaneh
41361f3e27b2Sshaneh /* Get shared locks at the system level, if necessary */
41371f3e27b2Sshaneh if( rc==SQLITE_OK ){
41381f3e27b2Sshaneh if( (allShared & mask)==0 ){
413943f58d6aSdrh rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, ofst+WIN_SHM_BASE, n);
41401f3e27b2Sshaneh }else{
41411f3e27b2Sshaneh rc = SQLITE_OK;
41421f3e27b2Sshaneh }
41431f3e27b2Sshaneh }
41441f3e27b2Sshaneh
41451f3e27b2Sshaneh /* Get the local shared locks */
41461f3e27b2Sshaneh if( rc==SQLITE_OK ){
41471f3e27b2Sshaneh p->sharedMask |= mask;
41481f3e27b2Sshaneh }
41491f3e27b2Sshaneh }else{
41501f3e27b2Sshaneh /* Make sure no sibling connections hold locks that will block this
41511f3e27b2Sshaneh ** lock. If any do, return SQLITE_BUSY right away.
41521f3e27b2Sshaneh */
41531f3e27b2Sshaneh for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
41541f3e27b2Sshaneh if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
41551f3e27b2Sshaneh rc = SQLITE_BUSY;
41561f3e27b2Sshaneh break;
41571f3e27b2Sshaneh }
41581f3e27b2Sshaneh }
41591f3e27b2Sshaneh
41601f3e27b2Sshaneh /* Get the exclusive locks at the system level. Then if successful
41611f3e27b2Sshaneh ** also mark the local connection as being locked.
41621f3e27b2Sshaneh */
41631f3e27b2Sshaneh if( rc==SQLITE_OK ){
416443f58d6aSdrh rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, ofst+WIN_SHM_BASE, n);
41651f3e27b2Sshaneh if( rc==SQLITE_OK ){
41661f3e27b2Sshaneh assert( (p->sharedMask & mask)==0 );
41671f3e27b2Sshaneh p->exclMask |= mask;
41681f3e27b2Sshaneh }
41691f3e27b2Sshaneh }
41701f3e27b2Sshaneh }
41711f3e27b2Sshaneh sqlite3_mutex_leave(pShmNode->mutex);
4172e84d8d32Smistachkin OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
4173e84d8d32Smistachkin osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
4174f2c1c99fSmistachkin sqlite3ErrName(rc)));
41751f3e27b2Sshaneh return rc;
41761f3e27b2Sshaneh }
41771f3e27b2Sshaneh
41781f3e27b2Sshaneh /*
41791f3e27b2Sshaneh ** Implement a memory barrier or memory fence on shared memory.
41801f3e27b2Sshaneh **
41811f3e27b2Sshaneh ** All loads and stores begun before the barrier must complete before
41821f3e27b2Sshaneh ** any load or store begun after the barrier.
41831f3e27b2Sshaneh */
41841f3e27b2Sshaneh static void winShmBarrier(
41851f3e27b2Sshaneh sqlite3_file *fd /* Database holding the shared memory */
41861f3e27b2Sshaneh ){
41871f3e27b2Sshaneh UNUSED_PARAMETER(fd);
418822c733daSdrh sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
418922c733daSdrh winShmEnterMutex(); /* Also mutex, for redundancy */
41901f3e27b2Sshaneh winShmLeaveMutex();
41911f3e27b2Sshaneh }
41921f3e27b2Sshaneh
41931f3e27b2Sshaneh /*
41949785fc95Sdan ** This function is called to obtain a pointer to region iRegion of the
41959785fc95Sdan ** shared-memory associated with the database file fd. Shared-memory regions
41969785fc95Sdan ** are numbered starting from zero. Each shared-memory region is szRegion
41979785fc95Sdan ** bytes in size.
419883235214Sdrh **
41999785fc95Sdan ** If an error occurs, an error code is returned and *pp is set to NULL.
420083235214Sdrh **
42019785fc95Sdan ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
42029785fc95Sdan ** region has not been allocated (by any client, including one running in a
42039785fc95Sdan ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
42049785fc95Sdan ** isWrite is non-zero and the requested shared-memory region has not yet
42059785fc95Sdan ** been allocated, it is allocated by this function.
42069785fc95Sdan **
42079785fc95Sdan ** If the shared-memory region has already been allocated or is allocated by
42089785fc95Sdan ** this call as described above, then it is mapped into this processes
42099785fc95Sdan ** address space (if it is not already), *pp is set to point to the mapped
42109785fc95Sdan ** memory and SQLITE_OK returned.
421183235214Sdrh */
42129785fc95Sdan static int winShmMap(
42139785fc95Sdan sqlite3_file *fd, /* Handle open on database file */
42149785fc95Sdan int iRegion, /* Region to retrieve */
42159785fc95Sdan int szRegion, /* Size of regions */
42169785fc95Sdan int isWrite, /* True to extend file if necessary */
42179785fc95Sdan void volatile **pp /* OUT: Mapped memory */
421883235214Sdrh ){
421983235214Sdrh winFile *pDbFd = (winFile*)fd;
4220dedc5ea0Smistachkin winShm *pShm = pDbFd->pShm;
4221da9fe0c3Sdan winShmNode *pShmNode;
42224ff8431fSmistachkin DWORD protect = PAGE_READWRITE;
42234ff8431fSmistachkin DWORD flags = FILE_MAP_WRITE | FILE_MAP_READ;
422483235214Sdrh int rc = SQLITE_OK;
422583235214Sdrh
4226dedc5ea0Smistachkin if( !pShm ){
4227da9fe0c3Sdan rc = winOpenSharedMemory(pDbFd);
4228da9fe0c3Sdan if( rc!=SQLITE_OK ) return rc;
4229dedc5ea0Smistachkin pShm = pDbFd->pShm;
42302cd02a5dSdrh assert( pShm!=0 );
4231da9fe0c3Sdan }
4232dedc5ea0Smistachkin pShmNode = pShm->pShmNode;
4233da9fe0c3Sdan
423483235214Sdrh sqlite3_mutex_enter(pShmNode->mutex);
42354ff8431fSmistachkin if( pShmNode->isUnlocked ){
42368a6fa5d4Smistachkin rc = winLockSharedMemory(pShmNode);
42374ff8431fSmistachkin if( rc!=SQLITE_OK ) goto shmpage_out;
42384ff8431fSmistachkin pShmNode->isUnlocked = 0;
42394ff8431fSmistachkin }
42409785fc95Sdan assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
42419785fc95Sdan
42429785fc95Sdan if( pShmNode->nRegion<=iRegion ){
42439785fc95Sdan struct ShmRegion *apNew; /* New aRegion[] array */
42449785fc95Sdan int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
42459785fc95Sdan sqlite3_int64 sz; /* Current size of wal-index file */
42469785fc95Sdan
42479785fc95Sdan pShmNode->szRegion = szRegion;
42489785fc95Sdan
42499785fc95Sdan /* The requested region is not mapped into this processes address space.
42509785fc95Sdan ** Check to see if it has been allocated (i.e. if the wal-index file is
42519785fc95Sdan ** large enough to contain the requested region).
425283235214Sdrh */
42539785fc95Sdan rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
42549785fc95Sdan if( rc!=SQLITE_OK ){
4255318507b7Smistachkin rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
42562aef997cSmistachkin "winShmMap1", pDbFd->zPath);
42579785fc95Sdan goto shmpage_out;
425883235214Sdrh }
42599785fc95Sdan
42609785fc95Sdan if( sz<nByte ){
42619785fc95Sdan /* The requested memory region does not exist. If isWrite is set to
42629785fc95Sdan ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
42639785fc95Sdan **
42649785fc95Sdan ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
42659785fc95Sdan ** the requested memory region.
42669785fc95Sdan */
42679785fc95Sdan if( !isWrite ) goto shmpage_out;
42689785fc95Sdan rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
42699785fc95Sdan if( rc!=SQLITE_OK ){
4270318507b7Smistachkin rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
42712aef997cSmistachkin "winShmMap2", pDbFd->zPath);
42729785fc95Sdan goto shmpage_out;
42739785fc95Sdan }
42749785fc95Sdan }
42759785fc95Sdan
42769785fc95Sdan /* Map the requested memory region into this processes address space. */
4277f3cdcdccSdrh apNew = (struct ShmRegion *)sqlite3_realloc64(
42789785fc95Sdan pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
42799785fc95Sdan );
42809785fc95Sdan if( !apNew ){
4281fad3039cSmistachkin rc = SQLITE_IOERR_NOMEM_BKPT;
42829785fc95Sdan goto shmpage_out;
42839785fc95Sdan }
42849785fc95Sdan pShmNode->aRegion = apNew;
42859785fc95Sdan
42864ff8431fSmistachkin if( pShmNode->isReadonly ){
42874ff8431fSmistachkin protect = PAGE_READONLY;
42884ff8431fSmistachkin flags = FILE_MAP_READ;
42894ff8431fSmistachkin }
42904ff8431fSmistachkin
42919785fc95Sdan while( pShmNode->nRegion<=iRegion ){
4292c60941f8Smistachkin HANDLE hMap = NULL; /* file-mapping handle */
42939785fc95Sdan void *pMap = 0; /* Mapped memory region */
42949785fc95Sdan
42951e6eea95Smistachkin #if SQLITE_OS_WINRT
42961e6eea95Smistachkin hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
42974ff8431fSmistachkin NULL, protect, nByte, NULL
42981e6eea95Smistachkin );
4299c60941f8Smistachkin #elif defined(SQLITE_WIN32_HAS_WIDE)
4300df562d55Smistachkin hMap = osCreateFileMappingW(pShmNode->hFile.h,
43014ff8431fSmistachkin NULL, protect, 0, nByte, NULL
43029785fc95Sdan );
4303d5be6f01Smistachkin #elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
4304c60941f8Smistachkin hMap = osCreateFileMappingA(pShmNode->hFile.h,
43054ff8431fSmistachkin NULL, protect, 0, nByte, NULL
4306c60941f8Smistachkin );
43071e6eea95Smistachkin #endif
4308e84d8d32Smistachkin OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
4309e84d8d32Smistachkin osGetCurrentProcessId(), pShmNode->nRegion, nByte,
4310420398ceSshaneh hMap ? "ok" : "failed"));
43119785fc95Sdan if( hMap ){
4312420398ceSshaneh int iOffset = pShmNode->nRegion*szRegion;
4313420398ceSshaneh int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
4314287a48d6Smistachkin #if SQLITE_OS_WINRT
43154ff8431fSmistachkin pMap = osMapViewOfFileFromApp(hMap, flags,
43161e6eea95Smistachkin iOffset - iOffsetShift, szRegion + iOffsetShift
4317287a48d6Smistachkin );
4318287a48d6Smistachkin #else
43194ff8431fSmistachkin pMap = osMapViewOfFile(hMap, flags,
4320420398ceSshaneh 0, iOffset - iOffsetShift, szRegion + iOffsetShift
43219785fc95Sdan );
4322287a48d6Smistachkin #endif
4323e84d8d32Smistachkin OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
4324e84d8d32Smistachkin osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
4325318507b7Smistachkin szRegion, pMap ? "ok" : "failed"));
43269785fc95Sdan }
43279785fc95Sdan if( !pMap ){
4328318507b7Smistachkin pShmNode->lastErrno = osGetLastError();
43292aef997cSmistachkin rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
43302aef997cSmistachkin "winShmMap3", pDbFd->zPath);
4331318507b7Smistachkin if( hMap ) osCloseHandle(hMap);
43329785fc95Sdan goto shmpage_out;
43339785fc95Sdan }
43349785fc95Sdan
43359785fc95Sdan pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
43369785fc95Sdan pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
43379785fc95Sdan pShmNode->nRegion++;
43389785fc95Sdan }
43399785fc95Sdan }
43409785fc95Sdan
43419785fc95Sdan shmpage_out:
43429785fc95Sdan if( pShmNode->nRegion>iRegion ){
4343420398ceSshaneh int iOffset = iRegion*szRegion;
4344420398ceSshaneh int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
43459785fc95Sdan char *p = (char *)pShmNode->aRegion[iRegion].pMap;
4346420398ceSshaneh *pp = (void *)&p[iOffsetShift];
434783235214Sdrh }else{
43489785fc95Sdan *pp = 0;
434983235214Sdrh }
43504ff8431fSmistachkin if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
435183235214Sdrh sqlite3_mutex_leave(pShmNode->mutex);
435283235214Sdrh return rc;
435383235214Sdrh }
435483235214Sdrh
435583235214Sdrh #else
43561f3e27b2Sshaneh # define winShmMap 0
4357da9fe0c3Sdan # define winShmLock 0
4358286a2884Sdrh # define winShmBarrier 0
4359e11fedc5Sdrh # define winShmUnmap 0
436083235214Sdrh #endif /* #ifndef SQLITE_OMIT_WAL */
43611f3e27b2Sshaneh
436283235214Sdrh /*
4363daf9a5a4Smistachkin ** Cleans up the mapped region of the specified file, if any.
4364daf9a5a4Smistachkin */
43655824e053Smistachkin #if SQLITE_MAX_MMAP_SIZE>0
43665175b324Sdrh static int winUnmapfile(winFile *pFile){
4367daf9a5a4Smistachkin assert( pFile!=0 );
4368e84d8d32Smistachkin OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
436948ea97e5Sdrh "mmapSize=%lld, mmapSizeMax=%lld\n",
4370e84d8d32Smistachkin osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
437148ea97e5Sdrh pFile->mmapSize, pFile->mmapSizeMax));
4372daf9a5a4Smistachkin if( pFile->pMapRegion ){
4373daf9a5a4Smistachkin if( !osUnmapViewOfFile(pFile->pMapRegion) ){
4374daf9a5a4Smistachkin pFile->lastErrno = osGetLastError();
4375e84d8d32Smistachkin OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
4376e84d8d32Smistachkin "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
4377e84d8d32Smistachkin pFile->pMapRegion));
43785175b324Sdrh return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
43799f11ef12Smistachkin "winUnmapfile1", pFile->zPath);
4380daf9a5a4Smistachkin }
4381daf9a5a4Smistachkin pFile->pMapRegion = 0;
4382daf9a5a4Smistachkin pFile->mmapSize = 0;
4383daf9a5a4Smistachkin }
4384daf9a5a4Smistachkin if( pFile->hMap!=NULL ){
4385daf9a5a4Smistachkin if( !osCloseHandle(pFile->hMap) ){
4386daf9a5a4Smistachkin pFile->lastErrno = osGetLastError();
4387e84d8d32Smistachkin OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
4388e84d8d32Smistachkin osGetCurrentProcessId(), pFile, pFile->hMap));
43895175b324Sdrh return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
43909f11ef12Smistachkin "winUnmapfile2", pFile->zPath);
4391daf9a5a4Smistachkin }
4392daf9a5a4Smistachkin pFile->hMap = NULL;
4393daf9a5a4Smistachkin }
4394e84d8d32Smistachkin OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
4395e84d8d32Smistachkin osGetCurrentProcessId(), pFile));
4396daf9a5a4Smistachkin return SQLITE_OK;
4397daf9a5a4Smistachkin }
4398daf9a5a4Smistachkin
4399daf9a5a4Smistachkin /*
44005175b324Sdrh ** Memory map or remap the file opened by file-descriptor pFd (if the file
44015175b324Sdrh ** is already mapped, the existing mapping is replaced by the new). Or, if
44025175b324Sdrh ** there already exists a mapping for this file, and there are still
44035175b324Sdrh ** outstanding xFetch() references to it, this function is a no-op.
44045175b324Sdrh **
44055175b324Sdrh ** If parameter nByte is non-negative, then it is the requested size of
44065175b324Sdrh ** the mapping to create. Otherwise, if nByte is less than zero, then the
44075175b324Sdrh ** requested size is the size of the file on disk. The actual size of the
44085175b324Sdrh ** created mapping is either the requested size or the value configured
44099b4c59faSdrh ** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
44105175b324Sdrh **
44115175b324Sdrh ** SQLITE_OK is returned if no error occurs (even if the mapping is not
44125175b324Sdrh ** recreated as a result of outstanding references) or an SQLite error
44135175b324Sdrh ** code otherwise.
4414daf9a5a4Smistachkin */
44155175b324Sdrh static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
44165175b324Sdrh sqlite3_int64 nMap = nByte;
44175175b324Sdrh int rc;
4418daf9a5a4Smistachkin
44195175b324Sdrh assert( nMap>=0 || pFd->nFetchOut==0 );
4420e84d8d32Smistachkin OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
4421e84d8d32Smistachkin osGetCurrentProcessId(), pFd, nByte));
4422f2c1c99fSmistachkin
44235175b324Sdrh if( pFd->nFetchOut>0 ) return SQLITE_OK;
4424daf9a5a4Smistachkin
44255175b324Sdrh if( nMap<0 ){
44265175b324Sdrh rc = winFileSize((sqlite3_file*)pFd, &nMap);
44275175b324Sdrh if( rc ){
4428e84d8d32Smistachkin OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
4429e84d8d32Smistachkin osGetCurrentProcessId(), pFd));
44305175b324Sdrh return SQLITE_IOERR_FSTAT;
4431daf9a5a4Smistachkin }
44322753388eSmistachkin }
44339b4c59faSdrh if( nMap>pFd->mmapSizeMax ){
44349b4c59faSdrh nMap = pFd->mmapSizeMax;
4435daf9a5a4Smistachkin }
4436db56bcbeSdrh nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
4437daf9a5a4Smistachkin
44385175b324Sdrh if( nMap==0 && pFd->mmapSize>0 ){
44395175b324Sdrh winUnmapfile(pFd);
44405175b324Sdrh }
44415175b324Sdrh if( nMap!=pFd->mmapSize ){
44425175b324Sdrh void *pNew = 0;
4443daf9a5a4Smistachkin DWORD protect = PAGE_READONLY;
4444daf9a5a4Smistachkin DWORD flags = FILE_MAP_READ;
44455175b324Sdrh
44465175b324Sdrh winUnmapfile(pFd);
4447c88cd137Smistachkin #ifdef SQLITE_MMAP_READWRITE
44485175b324Sdrh if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
4449daf9a5a4Smistachkin protect = PAGE_READWRITE;
4450daf9a5a4Smistachkin flags |= FILE_MAP_WRITE;
4451daf9a5a4Smistachkin }
4452c88cd137Smistachkin #endif
4453daf9a5a4Smistachkin #if SQLITE_OS_WINRT
4454f9d18e47Sdrh pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
4455daf9a5a4Smistachkin #elif defined(SQLITE_WIN32_HAS_WIDE)
4456f9d18e47Sdrh pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
44575175b324Sdrh (DWORD)((nMap>>32) & 0xffffffff),
44585175b324Sdrh (DWORD)(nMap & 0xffffffff), NULL);
4459d5be6f01Smistachkin #elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
4460f9d18e47Sdrh pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
44615175b324Sdrh (DWORD)((nMap>>32) & 0xffffffff),
44625175b324Sdrh (DWORD)(nMap & 0xffffffff), NULL);
4463daf9a5a4Smistachkin #endif
4464f9d18e47Sdrh if( pFd->hMap==NULL ){
44655175b324Sdrh pFd->lastErrno = osGetLastError();
44665175b324Sdrh rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
44679f11ef12Smistachkin "winMapfile1", pFd->zPath);
44685175b324Sdrh /* Log the error, but continue normal operation using xRead/xWrite */
44699f11ef12Smistachkin OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
44709f11ef12Smistachkin osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
44715175b324Sdrh return SQLITE_OK;
4472daf9a5a4Smistachkin }
447307fa864dSdrh assert( (nMap % winSysInfo.dwPageSize)==0 );
4474a9d79ae9Smistachkin assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff );
4475c6fc65ceSmistachkin #if SQLITE_OS_WINRT
4476c6fc65ceSmistachkin pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
4477c6fc65ceSmistachkin #else
4478f9d18e47Sdrh pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
4479daf9a5a4Smistachkin #endif
4480daf9a5a4Smistachkin if( pNew==NULL ){
4481f9d18e47Sdrh osCloseHandle(pFd->hMap);
4482f9d18e47Sdrh pFd->hMap = NULL;
44835175b324Sdrh pFd->lastErrno = osGetLastError();
44849f11ef12Smistachkin rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
44859f11ef12Smistachkin "winMapfile2", pFd->zPath);
44869f11ef12Smistachkin /* Log the error, but continue normal operation using xRead/xWrite */
44879f11ef12Smistachkin OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
44889f11ef12Smistachkin osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
44895175b324Sdrh return SQLITE_OK;
4490daf9a5a4Smistachkin }
44915175b324Sdrh pFd->pMapRegion = pNew;
44925175b324Sdrh pFd->mmapSize = nMap;
4493daf9a5a4Smistachkin }
4494daf9a5a4Smistachkin
4495e84d8d32Smistachkin OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
4496e84d8d32Smistachkin osGetCurrentProcessId(), pFd));
44975175b324Sdrh return SQLITE_OK;
44985175b324Sdrh }
44999b4c59faSdrh #endif /* SQLITE_MAX_MMAP_SIZE>0 */
4500daf9a5a4Smistachkin
45015175b324Sdrh /*
45025175b324Sdrh ** If possible, return a pointer to a mapping of file fd starting at offset
45035175b324Sdrh ** iOff. The mapping must be valid for at least nAmt bytes.
45045175b324Sdrh **
45055175b324Sdrh ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
45065175b324Sdrh ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
45075175b324Sdrh ** Finally, if an error does occur, return an SQLite error code. The final
45085175b324Sdrh ** value of *pp is undefined in this case.
45095175b324Sdrh **
45105175b324Sdrh ** If this function does return a pointer, the caller must eventually
4511e84d8d32Smistachkin ** release the reference by calling winUnfetch().
45125175b324Sdrh */
45135175b324Sdrh static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
45145824e053Smistachkin #if SQLITE_MAX_MMAP_SIZE>0
45155175b324Sdrh winFile *pFd = (winFile*)fd; /* The underlying database file */
45165824e053Smistachkin #endif
45175175b324Sdrh *pp = 0;
45185175b324Sdrh
4519e84d8d32Smistachkin OSTRACE(("FETCH pid=%lu, pFile=%p, offset=%lld, amount=%d, pp=%p\n",
4520e84d8d32Smistachkin osGetCurrentProcessId(), fd, iOff, nAmt, pp));
4521f2c1c99fSmistachkin
45229b4c59faSdrh #if SQLITE_MAX_MMAP_SIZE>0
45239b4c59faSdrh if( pFd->mmapSizeMax>0 ){
45245175b324Sdrh if( pFd->pMapRegion==0 ){
45255175b324Sdrh int rc = winMapfile(pFd, -1);
4526f2c1c99fSmistachkin if( rc!=SQLITE_OK ){
4527e84d8d32Smistachkin OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n",
4528e84d8d32Smistachkin osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
4529f2c1c99fSmistachkin return rc;
4530f2c1c99fSmistachkin }
45315175b324Sdrh }
45325175b324Sdrh if( pFd->mmapSize >= iOff+nAmt ){
45332cd02a5dSdrh assert( pFd->pMapRegion!=0 );
45345175b324Sdrh *pp = &((u8 *)pFd->pMapRegion)[iOff];
45355175b324Sdrh pFd->nFetchOut++;
45365175b324Sdrh }
45375175b324Sdrh }
45386e0b6d52Sdrh #endif
4539f2c1c99fSmistachkin
4540e84d8d32Smistachkin OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n",
4541e84d8d32Smistachkin osGetCurrentProcessId(), fd, pp, *pp));
45425175b324Sdrh return SQLITE_OK;
45435175b324Sdrh }
45445175b324Sdrh
45455175b324Sdrh /*
45465175b324Sdrh ** If the third argument is non-NULL, then this function releases a
4547e84d8d32Smistachkin ** reference obtained by an earlier call to winFetch(). The second
45485175b324Sdrh ** argument passed to this function must be the same as the corresponding
4549e84d8d32Smistachkin ** argument that was passed to the winFetch() invocation.
45505175b324Sdrh **
45515175b324Sdrh ** Or, if the third argument is NULL, then this function is being called
45525175b324Sdrh ** to inform the VFS layer that, according to POSIX, any existing mapping
45535175b324Sdrh ** may now be invalid and should be unmapped.
45545175b324Sdrh */
45555175b324Sdrh static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
45565824e053Smistachkin #if SQLITE_MAX_MMAP_SIZE>0
45575175b324Sdrh winFile *pFd = (winFile*)fd; /* The underlying database file */
45585175b324Sdrh
45595175b324Sdrh /* If p==0 (unmap the entire file) then there must be no outstanding
45605175b324Sdrh ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
45615175b324Sdrh ** then there must be at least one outstanding. */
45625175b324Sdrh assert( (p==0)==(pFd->nFetchOut==0) );
45635175b324Sdrh
45645175b324Sdrh /* If p!=0, it must match the iOff value. */
45655175b324Sdrh assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
45665175b324Sdrh
4567e84d8d32Smistachkin OSTRACE(("UNFETCH pid=%lu, pFile=%p, offset=%lld, p=%p\n",
4568e84d8d32Smistachkin osGetCurrentProcessId(), pFd, iOff, p));
4569f2c1c99fSmistachkin
45705175b324Sdrh if( p ){
45715175b324Sdrh pFd->nFetchOut--;
45725175b324Sdrh }else{
4573a539c8a4Sdrh /* FIXME: If Windows truly always prevents truncating or deleting a
4574a539c8a4Sdrh ** file while a mapping is held, then the following winUnmapfile() call
457560ec914cSpeter.d.reid ** is unnecessary can be omitted - potentially improving
4576a539c8a4Sdrh ** performance. */
45775175b324Sdrh winUnmapfile(pFd);
45785175b324Sdrh }
45795175b324Sdrh
45805175b324Sdrh assert( pFd->nFetchOut>=0 );
45815824e053Smistachkin #endif
4582f2c1c99fSmistachkin
4583e84d8d32Smistachkin OSTRACE(("UNFETCH pid=%lu, pFile=%p, rc=SQLITE_OK\n",
4584e84d8d32Smistachkin osGetCurrentProcessId(), fd));
45855175b324Sdrh return SQLITE_OK;
4586daf9a5a4Smistachkin }
4587daf9a5a4Smistachkin
4588daf9a5a4Smistachkin /*
45891f3e27b2Sshaneh ** Here ends the implementation of all sqlite3_file methods.
45901f3e27b2Sshaneh **
45911f3e27b2Sshaneh ********************** End sqlite3_file Methods *******************************
45921f3e27b2Sshaneh ******************************************************************************/
459383235214Sdrh
4594153c62c4Sdrh /*
4595153c62c4Sdrh ** This vector defines all the methods that can operate on an
4596153c62c4Sdrh ** sqlite3_file for win32.
4597153c62c4Sdrh */
4598153c62c4Sdrh static const sqlite3_io_methods winIoMethod = {
4599daf9a5a4Smistachkin 3, /* iVersion */
4600da9fe0c3Sdan winClose, /* xClose */
4601da9fe0c3Sdan winRead, /* xRead */
4602da9fe0c3Sdan winWrite, /* xWrite */
4603da9fe0c3Sdan winTruncate, /* xTruncate */
4604da9fe0c3Sdan winSync, /* xSync */
4605da9fe0c3Sdan winFileSize, /* xFileSize */
4606da9fe0c3Sdan winLock, /* xLock */
4607da9fe0c3Sdan winUnlock, /* xUnlock */
4608da9fe0c3Sdan winCheckReservedLock, /* xCheckReservedLock */
4609da9fe0c3Sdan winFileControl, /* xFileControl */
4610da9fe0c3Sdan winSectorSize, /* xSectorSize */
4611da9fe0c3Sdan winDeviceCharacteristics, /* xDeviceCharacteristics */
46126b017cc6Sdrh winShmMap, /* xShmMap */
4613da9fe0c3Sdan winShmLock, /* xShmLock */
4614286a2884Sdrh winShmBarrier, /* xShmBarrier */
4615daf9a5a4Smistachkin winShmUnmap, /* xShmUnmap */
46165175b324Sdrh winFetch, /* xFetch */
46175175b324Sdrh winUnfetch /* xUnfetch */
46189c06c953Sdrh };
46199c06c953Sdrh
46201e75483cSmistachkin /*
46211e75483cSmistachkin ** This vector defines all the methods that can operate on an
46221e75483cSmistachkin ** sqlite3_file for win32 without performing any locking.
46231e75483cSmistachkin */
46241e75483cSmistachkin static const sqlite3_io_methods winIoNolockMethod = {
46251e75483cSmistachkin 3, /* iVersion */
46261e75483cSmistachkin winClose, /* xClose */
46271e75483cSmistachkin winRead, /* xRead */
46281e75483cSmistachkin winWrite, /* xWrite */
46291e75483cSmistachkin winTruncate, /* xTruncate */
46301e75483cSmistachkin winSync, /* xSync */
46311e75483cSmistachkin winFileSize, /* xFileSize */
46321e75483cSmistachkin winNolockLock, /* xLock */
46331e75483cSmistachkin winNolockUnlock, /* xUnlock */
46341e75483cSmistachkin winNolockCheckReservedLock, /* xCheckReservedLock */
46351e75483cSmistachkin winFileControl, /* xFileControl */
46361e75483cSmistachkin winSectorSize, /* xSectorSize */
46371e75483cSmistachkin winDeviceCharacteristics, /* xDeviceCharacteristics */
46381e75483cSmistachkin winShmMap, /* xShmMap */
46391e75483cSmistachkin winShmLock, /* xShmLock */
46401e75483cSmistachkin winShmBarrier, /* xShmBarrier */
46411e75483cSmistachkin winShmUnmap, /* xShmUnmap */
46421e75483cSmistachkin winFetch, /* xFetch */
46431e75483cSmistachkin winUnfetch /* xUnfetch */
46441e75483cSmistachkin };
46451e75483cSmistachkin
46461e75483cSmistachkin static winVfsAppData winAppData = {
46471e75483cSmistachkin &winIoMethod, /* pMethod */
46481e75483cSmistachkin 0, /* pAppData */
46491e75483cSmistachkin 0 /* bNoLock */
46501e75483cSmistachkin };
46511e75483cSmistachkin
46521e75483cSmistachkin static winVfsAppData winNolockAppData = {
46531e75483cSmistachkin &winIoNolockMethod, /* pMethod */
46541e75483cSmistachkin 0, /* pAppData */
46551e75483cSmistachkin 1 /* bNoLock */
46561e75483cSmistachkin };
46571e75483cSmistachkin
46581f3e27b2Sshaneh /****************************************************************************
46591f3e27b2Sshaneh **************************** sqlite3_vfs methods ****************************
4660153c62c4Sdrh **
46611f3e27b2Sshaneh ** This division contains the implementation of methods on the
46621f3e27b2Sshaneh ** sqlite3_vfs object.
46631f3e27b2Sshaneh */
46640ccebe7eSdrh
46655bbb7198Smistachkin #if defined(__CYGWIN__)
4666153c62c4Sdrh /*
4667d95a3d35Smistachkin ** Convert a filename from whatever the underlying operating system
4668d95a3d35Smistachkin ** supports for filenames into UTF-8. Space to hold the result is
4669d95a3d35Smistachkin ** obtained from malloc and must be freed by the calling function.
4670d95a3d35Smistachkin */
4671d95a3d35Smistachkin static char *winConvertToUtf8Filename(const void *zFilename){
4672d95a3d35Smistachkin char *zConverted = 0;
4673d95a3d35Smistachkin if( osIsNT() ){
4674d95a3d35Smistachkin zConverted = winUnicodeToUtf8(zFilename);
4675d95a3d35Smistachkin }
4676d95a3d35Smistachkin #ifdef SQLITE_WIN32_HAS_ANSI
4677d95a3d35Smistachkin else{
467812931203Smistachkin zConverted = winMbcsToUtf8(zFilename, osAreFileApisANSI());
4679d95a3d35Smistachkin }
4680d95a3d35Smistachkin #endif
4681d95a3d35Smistachkin /* caller will handle out of memory */
4682d95a3d35Smistachkin return zConverted;
4683d95a3d35Smistachkin }
468461b7060cSmistachkin #endif
4685d95a3d35Smistachkin
4686153c62c4Sdrh /*
4687153c62c4Sdrh ** Convert a UTF-8 filename into whatever form the underlying
4688153c62c4Sdrh ** operating system wants filenames in. Space to hold the result
4689b11caac3Sdrh ** is obtained from malloc and must be freed by the calling
4690153c62c4Sdrh ** function.
4691153c62c4Sdrh */
4692d95a3d35Smistachkin static void *winConvertFromUtf8Filename(const char *zFilename){
4693153c62c4Sdrh void *zConverted = 0;
4694b324bc79Smistachkin if( osIsNT() ){
4695b324bc79Smistachkin zConverted = winUtf8ToUnicode(zFilename);
4696153c62c4Sdrh }
4697d52ee729Sdrh #ifdef SQLITE_WIN32_HAS_ANSI
4698d52ee729Sdrh else{
469912931203Smistachkin zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
4700d52ee729Sdrh }
4701d52ee729Sdrh #endif
4702153c62c4Sdrh /* caller will handle out of memory */
4703153c62c4Sdrh return zConverted;
4704153c62c4Sdrh }
4705153c62c4Sdrh
4706153c62c4Sdrh /*
47073741827eSmistachkin ** This function returns non-zero if the specified UTF-8 string buffer
470814eca4efSmistachkin ** ends with a directory separator character or one was successfully
470914eca4efSmistachkin ** added to it.
471017b90b53Sdanielk1977 */
471114eca4efSmistachkin static int winMakeEndInDirSep(int nBuf, char *zBuf){
47123741827eSmistachkin if( zBuf ){
47133741827eSmistachkin int nLen = sqlite3Strlen30(zBuf);
471414eca4efSmistachkin if( nLen>0 ){
471514eca4efSmistachkin if( winIsDirSep(zBuf[nLen-1]) ){
471614eca4efSmistachkin return 1;
471714eca4efSmistachkin }else if( nLen+1<nBuf ){
471814eca4efSmistachkin zBuf[nLen] = winGetDirSep();
471914eca4efSmistachkin zBuf[nLen+1] = '\0';
472014eca4efSmistachkin return 1;
472114eca4efSmistachkin }
472214eca4efSmistachkin }
47233741827eSmistachkin }
47243741827eSmistachkin return 0;
47253741827eSmistachkin }
47263741827eSmistachkin
47273741827eSmistachkin /*
4728*e38b6e03Sdrh ** If sqlite3_temp_directory is defined, take the mutex and return true.
472918a3a48dSdrh **
4730*e38b6e03Sdrh ** If sqlite3_temp_directory is NULL (undefined), omit the mutex and
4731*e38b6e03Sdrh ** return false.
473218a3a48dSdrh */
473318a3a48dSdrh static int winTempDirDefined(void){
473418a3a48dSdrh sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
473518a3a48dSdrh if( sqlite3_temp_directory!=0 ) return 1;
473618a3a48dSdrh sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
473718a3a48dSdrh return 0;
473818a3a48dSdrh }
473918a3a48dSdrh
474018a3a48dSdrh /*
47413741827eSmistachkin ** Create a temporary file name and store the resulting pointer into pzBuf.
47423741827eSmistachkin ** The pointer returned in pzBuf must be freed via sqlite3_free().
47433741827eSmistachkin */
47443741827eSmistachkin static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
474517b90b53Sdanielk1977 static char zChars[] =
474617b90b53Sdanielk1977 "abcdefghijklmnopqrstuvwxyz"
474717b90b53Sdanielk1977 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
474817b90b53Sdanielk1977 "0123456789";
47493582c5a9Sshane size_t i, j;
4750fd4b90beSmistachkin int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX);
4751fd4b90beSmistachkin int nMax, nBuf, nDir, nLen;
47523741827eSmistachkin char *zBuf;
4753e2ad9317Sshaneh
4754e2ad9317Sshaneh /* It's odd to simulate an io-error here, but really this is just
4755e2ad9317Sshaneh ** using the io-error infrastructure to test that SQLite handles this
4756e2ad9317Sshaneh ** function failing.
4757e2ad9317Sshaneh */
4758e2ad9317Sshaneh SimulateIOError( return SQLITE_IOERR );
4759e2ad9317Sshaneh
47603741827eSmistachkin /* Allocate a temporary buffer to store the fully qualified file
47613741827eSmistachkin ** name for the temporary file. If this fails, we cannot continue.
47623741827eSmistachkin */
4763fd4b90beSmistachkin nMax = pVfs->mxPathname; nBuf = nMax + 2;
4764fd4b90beSmistachkin zBuf = sqlite3MallocZero( nBuf );
47653741827eSmistachkin if( !zBuf ){
47663741827eSmistachkin OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
4767fad3039cSmistachkin return SQLITE_IOERR_NOMEM_BKPT;
4768c5484654Smistachkin }
47693741827eSmistachkin
47703741827eSmistachkin /* Figure out the effective temporary directory. First, check if one
47713741827eSmistachkin ** has been explicitly set by the application; otherwise, use the one
47723741827eSmistachkin ** configured by the operating system.
47733741827eSmistachkin */
4774fd4b90beSmistachkin nDir = nMax - (nPre + 15);
4775fd4b90beSmistachkin assert( nDir>0 );
477618a3a48dSdrh if( winTempDirDefined() ){
4777fd4b90beSmistachkin int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
4778fd4b90beSmistachkin if( nDirLen>0 ){
4779fd4b90beSmistachkin if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
4780fd4b90beSmistachkin nDirLen++;
4781fd4b90beSmistachkin }
4782fd4b90beSmistachkin if( nDirLen>nDir ){
478318a3a48dSdrh sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
4784fd4b90beSmistachkin sqlite3_free(zBuf);
4785fd4b90beSmistachkin OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
4786fd4b90beSmistachkin return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0);
4787fd4b90beSmistachkin }
4788fd4b90beSmistachkin sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
4789fd4b90beSmistachkin }
479018a3a48dSdrh sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
4791c5484654Smistachkin }
479218a3a48dSdrh
4793d95a3d35Smistachkin #if defined(__CYGWIN__)
4794d95a3d35Smistachkin else{
4795d95a3d35Smistachkin static const char *azDirs[] = {
4796d95a3d35Smistachkin 0, /* getenv("SQLITE_TMPDIR") */
4797d95a3d35Smistachkin 0, /* getenv("TMPDIR") */
4798d95a3d35Smistachkin 0, /* getenv("TMP") */
4799d95a3d35Smistachkin 0, /* getenv("TEMP") */
4800d95a3d35Smistachkin 0, /* getenv("USERPROFILE") */
4801d95a3d35Smistachkin "/var/tmp",
4802d95a3d35Smistachkin "/usr/tmp",
4803d95a3d35Smistachkin "/tmp",
48041a88b14eSmistachkin ".",
4805d95a3d35Smistachkin 0 /* List terminator */
4806d95a3d35Smistachkin };
4807d95a3d35Smistachkin unsigned int i;
4808d95a3d35Smistachkin const char *zDir = 0;
4809d95a3d35Smistachkin
4810d95a3d35Smistachkin if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
4811d95a3d35Smistachkin if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
4812d95a3d35Smistachkin if( !azDirs[2] ) azDirs[2] = getenv("TMP");
4813d95a3d35Smistachkin if( !azDirs[3] ) azDirs[3] = getenv("TEMP");
4814d95a3d35Smistachkin if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE");
4815d95a3d35Smistachkin for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
4816d95a3d35Smistachkin void *zConverted;
4817d95a3d35Smistachkin if( zDir==0 ) continue;
4818d95a3d35Smistachkin /* If the path starts with a drive letter followed by the colon
4819d95a3d35Smistachkin ** character, assume it is already a native Win32 path; otherwise,
482014eca4efSmistachkin ** it must be converted to a native Win32 path via the Cygwin API
482114eca4efSmistachkin ** prior to using it.
4822d95a3d35Smistachkin */
4823d95a3d35Smistachkin if( winIsDriveLetterAndColon(zDir) ){
4824d95a3d35Smistachkin zConverted = winConvertFromUtf8Filename(zDir);
4825d95a3d35Smistachkin if( !zConverted ){
4826c216566bSmistachkin sqlite3_free(zBuf);
4827d95a3d35Smistachkin OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
4828fad3039cSmistachkin return SQLITE_IOERR_NOMEM_BKPT;
4829d95a3d35Smistachkin }
4830d95a3d35Smistachkin if( winIsDir(zConverted) ){
4831fd4b90beSmistachkin sqlite3_snprintf(nMax, zBuf, "%s", zDir);
4832d95a3d35Smistachkin sqlite3_free(zConverted);
4833d95a3d35Smistachkin break;
4834d95a3d35Smistachkin }
4835d95a3d35Smistachkin sqlite3_free(zConverted);
483617b90b53Sdanielk1977 }else{
4837fd4b90beSmistachkin zConverted = sqlite3MallocZero( nMax+1 );
4838d95a3d35Smistachkin if( !zConverted ){
4839c216566bSmistachkin sqlite3_free(zBuf);
4840d95a3d35Smistachkin OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
4841fad3039cSmistachkin return SQLITE_IOERR_NOMEM_BKPT;
4842d95a3d35Smistachkin }
4843d95a3d35Smistachkin if( cygwin_conv_path(
4844d95a3d35Smistachkin osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
4845fd4b90beSmistachkin zConverted, nMax+1)<0 ){
4846d95a3d35Smistachkin sqlite3_free(zConverted);
4847c216566bSmistachkin sqlite3_free(zBuf);
4848d95a3d35Smistachkin OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
48499f11ef12Smistachkin return winLogError(SQLITE_IOERR_CONVPATH, (DWORD)errno,
4850fd4b90beSmistachkin "winGetTempname2", zDir);
4851d95a3d35Smistachkin }
4852d95a3d35Smistachkin if( winIsDir(zConverted) ){
4853d95a3d35Smistachkin /* At this point, we know the candidate directory exists and should
4854d95a3d35Smistachkin ** be used. However, we may need to convert the string containing
4855d95a3d35Smistachkin ** its name into UTF-8 (i.e. if it is UTF-16 right now).
4856d95a3d35Smistachkin */
48575bbb7198Smistachkin char *zUtf8 = winConvertToUtf8Filename(zConverted);
4858d95a3d35Smistachkin if( !zUtf8 ){
4859d95a3d35Smistachkin sqlite3_free(zConverted);
4860c216566bSmistachkin sqlite3_free(zBuf);
4861d95a3d35Smistachkin OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
4862fad3039cSmistachkin return SQLITE_IOERR_NOMEM_BKPT;
4863d95a3d35Smistachkin }
4864fd4b90beSmistachkin sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
4865d95a3d35Smistachkin sqlite3_free(zUtf8);
4866d95a3d35Smistachkin sqlite3_free(zConverted);
4867d95a3d35Smistachkin break;
4868d95a3d35Smistachkin }
4869d95a3d35Smistachkin sqlite3_free(zConverted);
4870d95a3d35Smistachkin }
4871d95a3d35Smistachkin }
4872d95a3d35Smistachkin }
4873d95a3d35Smistachkin #elif !SQLITE_OS_WINRT && !defined(__CYGWIN__)
4874b324bc79Smistachkin else if( osIsNT() ){
487517b90b53Sdanielk1977 char *zMulti;
4876fd4b90beSmistachkin LPWSTR zWidePath = sqlite3MallocZero( nMax*sizeof(WCHAR) );
48773741827eSmistachkin if( !zWidePath ){
48783741827eSmistachkin sqlite3_free(zBuf);
48793741827eSmistachkin OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
4880fad3039cSmistachkin return SQLITE_IOERR_NOMEM_BKPT;
48813741827eSmistachkin }
4882fd4b90beSmistachkin if( osGetTempPathW(nMax, zWidePath)==0 ){
48833741827eSmistachkin sqlite3_free(zWidePath);
48843741827eSmistachkin sqlite3_free(zBuf);
488516a2e7a0Smistachkin OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
48869f11ef12Smistachkin return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
4887fd4b90beSmistachkin "winGetTempname2", 0);
488816a2e7a0Smistachkin }
4889b324bc79Smistachkin zMulti = winUnicodeToUtf8(zWidePath);
489017b90b53Sdanielk1977 if( zMulti ){
4891fd4b90beSmistachkin sqlite3_snprintf(nMax, zBuf, "%s", zMulti);
489217b90b53Sdanielk1977 sqlite3_free(zMulti);
48933741827eSmistachkin sqlite3_free(zWidePath);
489417b90b53Sdanielk1977 }else{
48953741827eSmistachkin sqlite3_free(zWidePath);
48963741827eSmistachkin sqlite3_free(zBuf);
4897f2c1c99fSmistachkin OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
4898fad3039cSmistachkin return SQLITE_IOERR_NOMEM_BKPT;
489917b90b53Sdanielk1977 }
4900d52ee729Sdrh }
4901d52ee729Sdrh #ifdef SQLITE_WIN32_HAS_ANSI
4902d52ee729Sdrh else{
490317b90b53Sdanielk1977 char *zUtf8;
4904fd4b90beSmistachkin char *zMbcsPath = sqlite3MallocZero( nMax );
49053741827eSmistachkin if( !zMbcsPath ){
49063741827eSmistachkin sqlite3_free(zBuf);
49073741827eSmistachkin OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
4908fad3039cSmistachkin return SQLITE_IOERR_NOMEM_BKPT;
49093741827eSmistachkin }
4910fd4b90beSmistachkin if( osGetTempPathA(nMax, zMbcsPath)==0 ){
49113741827eSmistachkin sqlite3_free(zBuf);
491216a2e7a0Smistachkin OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
49139f11ef12Smistachkin return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
4914fd4b90beSmistachkin "winGetTempname3", 0);
491516a2e7a0Smistachkin }
491612931203Smistachkin zUtf8 = winMbcsToUtf8(zMbcsPath, osAreFileApisANSI());
491717b90b53Sdanielk1977 if( zUtf8 ){
4918fd4b90beSmistachkin sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
49195f075388Smistachkin sqlite3_free(zUtf8);
492017b90b53Sdanielk1977 }else{
49213741827eSmistachkin sqlite3_free(zBuf);
4922f2c1c99fSmistachkin OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
4923fad3039cSmistachkin return SQLITE_IOERR_NOMEM_BKPT;
492417b90b53Sdanielk1977 }
492517b90b53Sdanielk1977 }
4926c6fc65ceSmistachkin #endif /* SQLITE_WIN32_HAS_ANSI */
4927c6fc65ceSmistachkin #endif /* !SQLITE_OS_WINRT */
4928e2ad9317Sshaneh
4929fd4b90beSmistachkin /*
4930fd4b90beSmistachkin ** Check to make sure the temporary directory ends with an appropriate
4931fd4b90beSmistachkin ** separator. If it does not and there is not enough space left to add
4932fd4b90beSmistachkin ** one, fail.
4933e2ad9317Sshaneh */
4934fd4b90beSmistachkin if( !winMakeEndInDirSep(nDir+1, zBuf) ){
49353741827eSmistachkin sqlite3_free(zBuf);
4936f2c1c99fSmistachkin OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
4937fd4b90beSmistachkin return winLogError(SQLITE_ERROR, 0, "winGetTempname4", 0);
4938e2ad9317Sshaneh }
4939e2ad9317Sshaneh
4940fd4b90beSmistachkin /*
4941fd4b90beSmistachkin ** Check that the output buffer is large enough for the temporary file
4942fd4b90beSmistachkin ** name in the following format:
4943fd4b90beSmistachkin **
4944fd4b90beSmistachkin ** "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
4945fd4b90beSmistachkin **
4946fd4b90beSmistachkin ** If not, return SQLITE_ERROR. The number 17 is used here in order to
4947fd4b90beSmistachkin ** account for the space used by the 15 character random suffix and the
4948fd4b90beSmistachkin ** two trailing NUL characters. The final directory separator character
4949fd4b90beSmistachkin ** has already added if it was not already present.
4950fd4b90beSmistachkin */
4951fd4b90beSmistachkin nLen = sqlite3Strlen30(zBuf);
4952fd4b90beSmistachkin if( (nLen + nPre + 17) > nBuf ){
4953fd4b90beSmistachkin sqlite3_free(zBuf);
4954fd4b90beSmistachkin OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
4955fd4b90beSmistachkin return winLogError(SQLITE_ERROR, 0, "winGetTempname5", 0);
4956fd4b90beSmistachkin }
4957e2ad9317Sshaneh
4958fd4b90beSmistachkin sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
4959e2ad9317Sshaneh
4960ea678832Sdrh j = sqlite3Strlen30(zBuf);
4961e2ad9317Sshaneh sqlite3_randomness(15, &zBuf[j]);
4962e2ad9317Sshaneh for(i=0; i<15; i++, j++){
496317b90b53Sdanielk1977 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
496417b90b53Sdanielk1977 }
496517b90b53Sdanielk1977 zBuf[j] = 0;
4966e290919aSmistachkin zBuf[j+1] = 0;
49673741827eSmistachkin *pzBuf = zBuf;
4968e2ad9317Sshaneh
4969f2c1c99fSmistachkin OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
497017b90b53Sdanielk1977 return SQLITE_OK;
497117b90b53Sdanielk1977 }
497217b90b53Sdanielk1977
4973820800d0Sshane /*
49741e039a24Sdrh ** Return TRUE if the named file is really a directory. Return false if
49751e039a24Sdrh ** it is something other than a directory, or if there is any kind of memory
49761e039a24Sdrh ** allocation failure.
49771e039a24Sdrh */
497848a55aa9Smistachkin static int winIsDir(const void *zConverted){
497948a55aa9Smistachkin DWORD attr;
498048a55aa9Smistachkin int rc = 0;
498148a55aa9Smistachkin DWORD lastErrno;
49821e039a24Sdrh
4983b324bc79Smistachkin if( osIsNT() ){
498448a55aa9Smistachkin int cnt = 0;
498548a55aa9Smistachkin WIN32_FILE_ATTRIBUTE_DATA sAttrData;
498648a55aa9Smistachkin memset(&sAttrData, 0, sizeof(sAttrData));
498748a55aa9Smistachkin while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
498848a55aa9Smistachkin GetFileExInfoStandard,
4989b324bc79Smistachkin &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
499048a55aa9Smistachkin if( !rc ){
499148a55aa9Smistachkin return 0; /* Invalid name? */
49921e039a24Sdrh }
499348a55aa9Smistachkin attr = sAttrData.dwFileAttributes;
499448a55aa9Smistachkin #if SQLITE_OS_WINCE==0
499548a55aa9Smistachkin }else{
499648a55aa9Smistachkin attr = osGetFileAttributesA((char*)zConverted);
499748a55aa9Smistachkin #endif
499848a55aa9Smistachkin }
499948a55aa9Smistachkin return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
50001e039a24Sdrh }
50011e039a24Sdrh
50020e97e9a4Sdrh /* forward reference */
50030e97e9a4Sdrh static int winAccess(
50040e97e9a4Sdrh sqlite3_vfs *pVfs, /* Not used on win32 */
50050e97e9a4Sdrh const char *zFilename, /* Name of file to check */
50060e97e9a4Sdrh int flags, /* Type of test to make on this file */
50070e97e9a4Sdrh int *pResOut /* OUT: Result */
50080e97e9a4Sdrh );
50090e97e9a4Sdrh
5010820800d0Sshane /*
5011153c62c4Sdrh ** Open a file.
5012153c62c4Sdrh */
5013153c62c4Sdrh static int winOpen(
50141e75483cSmistachkin sqlite3_vfs *pVfs, /* Used to get maximum path length and AppData */
5015153c62c4Sdrh const char *zName, /* Name of the file (UTF-8) */
5016153c62c4Sdrh sqlite3_file *id, /* Write the SQLite file handle here */
5017153c62c4Sdrh int flags, /* Open mode flags */
5018153c62c4Sdrh int *pOutFlags /* Status return flags */
5019153c62c4Sdrh ){
5020153c62c4Sdrh HANDLE h;
502199391180Smistachkin DWORD lastErrno = 0;
5022153c62c4Sdrh DWORD dwDesiredAccess;
5023153c62c4Sdrh DWORD dwShareMode;
5024153c62c4Sdrh DWORD dwCreationDisposition;
5025153c62c4Sdrh DWORD dwFlagsAndAttributes = 0;
5026d94b0556Sshane #if SQLITE_OS_WINCE
5027d94b0556Sshane int isTemp = 0;
5028d94b0556Sshane #endif
50291e75483cSmistachkin winVfsAppData *pAppData;
5030153c62c4Sdrh winFile *pFile = (winFile*)id;
503117b90b53Sdanielk1977 void *zConverted; /* Filename in OS encoding */
503217b90b53Sdanielk1977 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
5033fda06befSmistachkin int cnt = 0;
5034f7b5f855Sshaneh
5035f7b5f855Sshaneh /* If argument zPath is a NULL pointer, this function is required to open
5036f7b5f855Sshaneh ** a temporary file. Use this buffer to store the file name in.
5037f7b5f855Sshaneh */
50383741827eSmistachkin char *zTmpname = 0; /* For temporary filename, if necessary. */
503917b90b53Sdanielk1977
5040f7b5f855Sshaneh int rc = SQLITE_OK; /* Function Return Code */
5041bd2aaf9aSshaneh #if !defined(NDEBUG) || SQLITE_OS_WINCE
5042f7b5f855Sshaneh int eType = flags&0xFFFFFF00; /* Type of file to open */
5043bd2aaf9aSshaneh #endif
5044f7b5f855Sshaneh
5045f7b5f855Sshaneh int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
5046f7b5f855Sshaneh int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
5047f7b5f855Sshaneh int isCreate = (flags & SQLITE_OPEN_CREATE);
5048f7b5f855Sshaneh int isReadonly = (flags & SQLITE_OPEN_READONLY);
5049f7b5f855Sshaneh int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
5050f7b5f855Sshaneh
5051bd2aaf9aSshaneh #ifndef NDEBUG
5052f7b5f855Sshaneh int isOpenJournal = (isCreate && (
5053ccb2113aSdrh eType==SQLITE_OPEN_SUPER_JOURNAL
5054f7b5f855Sshaneh || eType==SQLITE_OPEN_MAIN_JOURNAL
5055f7b5f855Sshaneh || eType==SQLITE_OPEN_WAL
5056f7b5f855Sshaneh ));
5057bd2aaf9aSshaneh #endif
5058f7b5f855Sshaneh
5059f2c1c99fSmistachkin OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
5060f2c1c99fSmistachkin zUtf8Name, id, flags, pOutFlags));
5061f2c1c99fSmistachkin
5062f7b5f855Sshaneh /* Check the following statements are true:
5063f7b5f855Sshaneh **
5064f7b5f855Sshaneh ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
5065f7b5f855Sshaneh ** (b) if CREATE is set, then READWRITE must also be set, and
5066f7b5f855Sshaneh ** (c) if EXCLUSIVE is set, then CREATE must also be set.
5067f7b5f855Sshaneh ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
5068f7b5f855Sshaneh */
5069f7b5f855Sshaneh assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
5070f7b5f855Sshaneh assert(isCreate==0 || isReadWrite);
5071f7b5f855Sshaneh assert(isExclusive==0 || isCreate);
5072f7b5f855Sshaneh assert(isDelete==0 || isCreate);
5073f7b5f855Sshaneh
5074067b92baSdrh /* The main DB, main journal, WAL file and super-journal are never
5075f7b5f855Sshaneh ** automatically deleted. Nor are they ever temporary files. */
5076f7b5f855Sshaneh assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
5077f7b5f855Sshaneh assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
5078ccb2113aSdrh assert( (!isDelete && zName) || eType!=SQLITE_OPEN_SUPER_JOURNAL );
5079f7b5f855Sshaneh assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
5080f7b5f855Sshaneh
5081f7b5f855Sshaneh /* Assert that the upper layer has set one of the "file-type" flags. */
5082f7b5f855Sshaneh assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
5083f7b5f855Sshaneh || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
5084ccb2113aSdrh || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_SUPER_JOURNAL
5085f7b5f855Sshaneh || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
5086f7b5f855Sshaneh );
5087f7b5f855Sshaneh
50889ce59a94Smistachkin assert( pFile!=0 );
50899ce59a94Smistachkin memset(pFile, 0, sizeof(winFile));
509004882a9eSshaneh pFile->h = INVALID_HANDLE_VALUE;
509104882a9eSshaneh
5092af529737Smistachkin #if SQLITE_OS_WINRT
5093533fb6deSmistachkin if( !zUtf8Name && !sqlite3_temp_directory ){
5094af529737Smistachkin sqlite3_log(SQLITE_ERROR,
5095af529737Smistachkin "sqlite3_temp_directory variable should be set for WinRT");
5096af529737Smistachkin }
5097af529737Smistachkin #endif
5098af529737Smistachkin
509917b90b53Sdanielk1977 /* If the second argument to this function is NULL, generate a
510017b90b53Sdanielk1977 ** temporary file name to use
510117b90b53Sdanielk1977 */
510217b90b53Sdanielk1977 if( !zUtf8Name ){
5103f7b5f855Sshaneh assert( isDelete && !isOpenJournal );
51043741827eSmistachkin rc = winGetTempname(pVfs, &zTmpname);
510517b90b53Sdanielk1977 if( rc!=SQLITE_OK ){
5106e84d8d32Smistachkin OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
510717b90b53Sdanielk1977 return rc;
510817b90b53Sdanielk1977 }
510917b90b53Sdanielk1977 zUtf8Name = zTmpname;
511017b90b53Sdanielk1977 }
511117b90b53Sdanielk1977
5112e290919aSmistachkin /* Database filenames are double-zero terminated if they are not
5113e290919aSmistachkin ** URIs with parameters. Hence, they can always be passed into
5114e290919aSmistachkin ** sqlite3_uri_parameter().
5115e290919aSmistachkin */
5116e290919aSmistachkin assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
5117533fb6deSmistachkin zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 );
5118e290919aSmistachkin
511917b90b53Sdanielk1977 /* Convert the filename to the system encoding. */
5120d95a3d35Smistachkin zConverted = winConvertFromUtf8Filename(zUtf8Name);
5121153c62c4Sdrh if( zConverted==0 ){
51223741827eSmistachkin sqlite3_free(zTmpname);
5123f2c1c99fSmistachkin OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
5124fad3039cSmistachkin return SQLITE_IOERR_NOMEM_BKPT;
5125153c62c4Sdrh }
5126153c62c4Sdrh
512748a55aa9Smistachkin if( winIsDir(zConverted) ){
512848a55aa9Smistachkin sqlite3_free(zConverted);
51293741827eSmistachkin sqlite3_free(zTmpname);
5130f2c1c99fSmistachkin OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
513148a55aa9Smistachkin return SQLITE_CANTOPEN_ISDIR;
513248a55aa9Smistachkin }
513348a55aa9Smistachkin
5134f7b5f855Sshaneh if( isReadWrite ){
5135153c62c4Sdrh dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
5136153c62c4Sdrh }else{
5137153c62c4Sdrh dwDesiredAccess = GENERIC_READ;
5138153c62c4Sdrh }
5139f7b5f855Sshaneh
514068d405e2Sshane /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
514168d405e2Sshane ** created. SQLite doesn't use it to indicate "exclusive access"
514268d405e2Sshane ** as it is usually understood.
514368d405e2Sshane */
5144f7b5f855Sshaneh if( isExclusive ){
514568d405e2Sshane /* Creates a new file, only if it does not already exist. */
514668d405e2Sshane /* If the file exists, it fails. */
514768d405e2Sshane dwCreationDisposition = CREATE_NEW;
5148f7b5f855Sshaneh }else if( isCreate ){
514968d405e2Sshane /* Open existing file, or create if it doesn't exist */
5150153c62c4Sdrh dwCreationDisposition = OPEN_ALWAYS;
5151153c62c4Sdrh }else{
515268d405e2Sshane /* Opens a file, only if it exists. */
5153153c62c4Sdrh dwCreationDisposition = OPEN_EXISTING;
5154153c62c4Sdrh }
5155f7b5f855Sshaneh
5156f8c4c3a0Sdrh if( 0==sqlite3_uri_boolean(zName, "exclusive", 0) ){
5157153c62c4Sdrh dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
5158f8c4c3a0Sdrh }else{
5159f8c4c3a0Sdrh dwShareMode = 0;
5160f8c4c3a0Sdrh }
5161f7b5f855Sshaneh
5162f7b5f855Sshaneh if( isDelete ){
516329bafeabSdanielk1977 #if SQLITE_OS_WINCE
51640cd1ea5eSdrh dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
5165d94b0556Sshane isTemp = 1;
51660cd1ea5eSdrh #else
5167153c62c4Sdrh dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
5168153c62c4Sdrh | FILE_ATTRIBUTE_HIDDEN
5169153c62c4Sdrh | FILE_FLAG_DELETE_ON_CLOSE;
51700cd1ea5eSdrh #endif
5171153c62c4Sdrh }else{
5172153c62c4Sdrh dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
5173153c62c4Sdrh }
5174496936c8Sdrh /* Reports from the internet are that performance is always
5175496936c8Sdrh ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */
5176d94b0556Sshane #if SQLITE_OS_WINCE
5177496936c8Sdrh dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
5178d94b0556Sshane #endif
5179f7b5f855Sshaneh
5180b324bc79Smistachkin if( osIsNT() ){
5181df562d55Smistachkin #if SQLITE_OS_WINRT
51825483f772Smistachkin CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
51835483f772Smistachkin extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
51845483f772Smistachkin extendedParameters.dwFileAttributes =
51855483f772Smistachkin dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
51865483f772Smistachkin extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK;
51875483f772Smistachkin extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
51885483f772Smistachkin extendedParameters.lpSecurityAttributes = NULL;
51895483f772Smistachkin extendedParameters.hTemplateFile = NULL;
51903314062bSmistachkin do{
51913314062bSmistachkin h = osCreateFile2((LPCWSTR)zConverted,
51925483f772Smistachkin dwDesiredAccess,
51935483f772Smistachkin dwShareMode,
51945483f772Smistachkin dwCreationDisposition,
51953314062bSmistachkin &extendedParameters);
51963314062bSmistachkin if( h!=INVALID_HANDLE_VALUE ) break;
51973314062bSmistachkin if( isReadWrite ){
51985685257bSmistachkin int rc2, isRO = 0;
51995685257bSmistachkin sqlite3BeginBenignMalloc();
52005685257bSmistachkin rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
52015685257bSmistachkin sqlite3EndBenignMalloc();
52023314062bSmistachkin if( rc2==SQLITE_OK && isRO ) break;
52031e039a24Sdrh }
52043314062bSmistachkin }while( winRetryIoerr(&cnt, &lastErrno) );
52055483f772Smistachkin #else
52060e97e9a4Sdrh do{
52070e97e9a4Sdrh h = osCreateFileW((LPCWSTR)zConverted,
5208153c62c4Sdrh dwDesiredAccess,
5209fda06befSmistachkin dwShareMode, NULL,
5210153c62c4Sdrh dwCreationDisposition,
5211153c62c4Sdrh dwFlagsAndAttributes,
52120e97e9a4Sdrh NULL);
52130e97e9a4Sdrh if( h!=INVALID_HANDLE_VALUE ) break;
52140e97e9a4Sdrh if( isReadWrite ){
52155685257bSmistachkin int rc2, isRO = 0;
52165685257bSmistachkin sqlite3BeginBenignMalloc();
52175685257bSmistachkin rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
52185685257bSmistachkin sqlite3EndBenignMalloc();
52190e97e9a4Sdrh if( rc2==SQLITE_OK && isRO ) break;
522048a55aa9Smistachkin }
52210e97e9a4Sdrh }while( winRetryIoerr(&cnt, &lastErrno) );
52225483f772Smistachkin #endif
5223d52ee729Sdrh }
5224d52ee729Sdrh #ifdef SQLITE_WIN32_HAS_ANSI
5225d52ee729Sdrh else{
52263314062bSmistachkin do{
52273314062bSmistachkin h = osCreateFileA((LPCSTR)zConverted,
5228153c62c4Sdrh dwDesiredAccess,
5229fda06befSmistachkin dwShareMode, NULL,
5230153c62c4Sdrh dwCreationDisposition,
5231153c62c4Sdrh dwFlagsAndAttributes,
52323314062bSmistachkin NULL);
52333314062bSmistachkin if( h!=INVALID_HANDLE_VALUE ) break;
52343314062bSmistachkin if( isReadWrite ){
52355685257bSmistachkin int rc2, isRO = 0;
52365685257bSmistachkin sqlite3BeginBenignMalloc();
52375685257bSmistachkin rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
52385685257bSmistachkin sqlite3EndBenignMalloc();
52393314062bSmistachkin if( rc2==SQLITE_OK && isRO ) break;
5240fcd2f12fSmistachkin }
52413314062bSmistachkin }while( winRetryIoerr(&cnt, &lastErrno) );
5242153c62c4Sdrh }
5243d52ee729Sdrh #endif
524421aa6a1aSdrh winLogIoerr(cnt, __LINE__);
5245fda06befSmistachkin
5246f2c1c99fSmistachkin OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
5247f2c1c99fSmistachkin dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
5248f7b5f855Sshaneh
5249153c62c4Sdrh if( h==INVALID_HANDLE_VALUE ){
52505f075388Smistachkin sqlite3_free(zConverted);
52513741827eSmistachkin sqlite3_free(zTmpname);
52523051dc1cSdrh if( isReadWrite && !isExclusive ){
5253a111577bSdrh return winOpen(pVfs, zName, id,
52541488052dSdrh ((flags|SQLITE_OPEN_READONLY) &
52551488052dSdrh ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
52561488052dSdrh pOutFlags);
5257153c62c4Sdrh }else{
5258b40d9eeaSdrh pFile->lastErrno = lastErrno;
5259b40d9eeaSdrh winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
52609978c97eSdrh return SQLITE_CANTOPEN_BKPT;
5261153c62c4Sdrh }
5262153c62c4Sdrh }
5263f7b5f855Sshaneh
5264153c62c4Sdrh if( pOutFlags ){
5265f7b5f855Sshaneh if( isReadWrite ){
5266153c62c4Sdrh *pOutFlags = SQLITE_OPEN_READWRITE;
5267153c62c4Sdrh }else{
5268153c62c4Sdrh *pOutFlags = SQLITE_OPEN_READONLY;
5269153c62c4Sdrh }
5270153c62c4Sdrh }
5271f7b5f855Sshaneh
5272e84d8d32Smistachkin OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, "
5273e84d8d32Smistachkin "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ?
5274e84d8d32Smistachkin *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
5275f7b5f855Sshaneh
52761e75483cSmistachkin pAppData = (winVfsAppData*)pVfs->pAppData;
52771e75483cSmistachkin
527829bafeabSdanielk1977 #if SQLITE_OS_WINCE
52791e75483cSmistachkin {
5280f7b5f855Sshaneh if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
52811e75483cSmistachkin && ((pAppData==NULL) || !pAppData->bNoLock)
52827e87eae9Smistachkin && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
5283153c62c4Sdrh ){
5284318507b7Smistachkin osCloseHandle(h);
52855f075388Smistachkin sqlite3_free(zConverted);
52863741827eSmistachkin sqlite3_free(zTmpname);
5287f2c1c99fSmistachkin OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
52887e87eae9Smistachkin return rc;
5289153c62c4Sdrh }
52901e75483cSmistachkin }
5291fc3afb6aSdrh if( isTemp ){
5292153c62c4Sdrh pFile->zDeleteOnClose = zConverted;
5293153c62c4Sdrh }else
5294153c62c4Sdrh #endif
5295153c62c4Sdrh {
52965f075388Smistachkin sqlite3_free(zConverted);
5297153c62c4Sdrh }
5298f7b5f855Sshaneh
5299c216566bSmistachkin sqlite3_free(zTmpname);
53000c52f5a2Sdrh id->pMethods = pAppData ? pAppData->pMethod : &winIoMethod;
53019ce59a94Smistachkin pFile->pVfs = pVfs;
53029ce59a94Smistachkin pFile->h = h;
5303daf9a5a4Smistachkin if( isReadonly ){
5304daf9a5a4Smistachkin pFile->ctrlFlags |= WINFILE_RDONLY;
5305daf9a5a4Smistachkin }
530610757ed0Sdan if( (flags & SQLITE_OPEN_MAIN_DB)
530710757ed0Sdan && sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE)
530810757ed0Sdan ){
53099ce59a94Smistachkin pFile->ctrlFlags |= WINFILE_PSOW;
53109ce59a94Smistachkin }
53119ce59a94Smistachkin pFile->lastErrno = NO_ERROR;
53129ce59a94Smistachkin pFile->zPath = zName;
53135824e053Smistachkin #if SQLITE_MAX_MMAP_SIZE>0
5314daf9a5a4Smistachkin pFile->hMap = NULL;
5315daf9a5a4Smistachkin pFile->pMapRegion = 0;
5316daf9a5a4Smistachkin pFile->mmapSize = 0;
5317ede01a97Sdan pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
53185824e053Smistachkin #endif
53199ce59a94Smistachkin
5320af5f0405Sdrh OpenCounter(+1);
5321f7b5f855Sshaneh return rc;
5322153c62c4Sdrh }
5323153c62c4Sdrh
5324153c62c4Sdrh /*
5325153c62c4Sdrh ** Delete the named file.
5326153c62c4Sdrh **
5327318507b7Smistachkin ** Note that Windows does not allow a file to be deleted if some other
5328153c62c4Sdrh ** process has it open. Sometimes a virus scanner or indexing program
5329153c62c4Sdrh ** will open a journal file shortly after it is created in order to do
53303582c5a9Sshane ** whatever it does. While this other process is holding the
5331153c62c4Sdrh ** file open, we will be unable to delete it. To work around this
5332153c62c4Sdrh ** problem, we delay 100 milliseconds and try to delete again. Up
5333153c62c4Sdrh ** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
5334153c62c4Sdrh ** up and returning an error.
5335153c62c4Sdrh */
5336153c62c4Sdrh static int winDelete(
5337153c62c4Sdrh sqlite3_vfs *pVfs, /* Not used on win32 */
5338153c62c4Sdrh const char *zFilename, /* Name of file to delete */
5339153c62c4Sdrh int syncDir /* Not used on win32 */
5340153c62c4Sdrh ){
5341153c62c4Sdrh int cnt = 0;
534252564d70Sdrh int rc;
534348a55aa9Smistachkin DWORD attr;
534499391180Smistachkin DWORD lastErrno = 0;
5345e2ad9317Sshaneh void *zConverted;
534618e526c1Sshane UNUSED_PARAMETER(pVfs);
534718e526c1Sshane UNUSED_PARAMETER(syncDir);
5348e2ad9317Sshaneh
5349e2ad9317Sshaneh SimulateIOError(return SQLITE_IOERR_DELETE);
5350f2c1c99fSmistachkin OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
5351f2c1c99fSmistachkin
5352d95a3d35Smistachkin zConverted = winConvertFromUtf8Filename(zFilename);
5353153c62c4Sdrh if( zConverted==0 ){
5354533fb6deSmistachkin OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
5355fad3039cSmistachkin return SQLITE_IOERR_NOMEM_BKPT;
5356153c62c4Sdrh }
5357b324bc79Smistachkin if( osIsNT() ){
535848a55aa9Smistachkin do {
5359287a48d6Smistachkin #if SQLITE_OS_WINRT
5360287a48d6Smistachkin WIN32_FILE_ATTRIBUTE_DATA sAttrData;
5361287a48d6Smistachkin memset(&sAttrData, 0, sizeof(sAttrData));
5362fcd2f12fSmistachkin if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
5363fcd2f12fSmistachkin &sAttrData) ){
5364fcd2f12fSmistachkin attr = sAttrData.dwFileAttributes;
5365fcd2f12fSmistachkin }else{
536655fbc869Smistachkin lastErrno = osGetLastError();
53671488052dSdrh if( lastErrno==ERROR_FILE_NOT_FOUND
53681488052dSdrh || lastErrno==ERROR_PATH_NOT_FOUND ){
53696d405c2cSdrh rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
537055fbc869Smistachkin }else{
537155fbc869Smistachkin rc = SQLITE_ERROR;
537255fbc869Smistachkin }
5373fcd2f12fSmistachkin break;
5374fcd2f12fSmistachkin }
5375287a48d6Smistachkin #else
537648a55aa9Smistachkin attr = osGetFileAttributesW(zConverted);
5377287a48d6Smistachkin #endif
537848a55aa9Smistachkin if ( attr==INVALID_FILE_ATTRIBUTES ){
537955fbc869Smistachkin lastErrno = osGetLastError();
53801488052dSdrh if( lastErrno==ERROR_FILE_NOT_FOUND
53811488052dSdrh || lastErrno==ERROR_PATH_NOT_FOUND ){
53826d405c2cSdrh rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
538355fbc869Smistachkin }else{
538455fbc869Smistachkin rc = SQLITE_ERROR;
538555fbc869Smistachkin }
538648a55aa9Smistachkin break;
538748a55aa9Smistachkin }
538848a55aa9Smistachkin if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
538948a55aa9Smistachkin rc = SQLITE_ERROR; /* Files only. */
539048a55aa9Smistachkin break;
539148a55aa9Smistachkin }
539248a55aa9Smistachkin if ( osDeleteFileW(zConverted) ){
539348a55aa9Smistachkin rc = SQLITE_OK; /* Deleted OK. */
539448a55aa9Smistachkin break;
539548a55aa9Smistachkin }
5396b324bc79Smistachkin if ( !winRetryIoerr(&cnt, &lastErrno) ){
539748a55aa9Smistachkin rc = SQLITE_ERROR; /* No more retries. */
539848a55aa9Smistachkin break;
539948a55aa9Smistachkin }
540048a55aa9Smistachkin } while(1);
5401d52ee729Sdrh }
5402d52ee729Sdrh #ifdef SQLITE_WIN32_HAS_ANSI
5403d52ee729Sdrh else{
540448a55aa9Smistachkin do {
540548a55aa9Smistachkin attr = osGetFileAttributesA(zConverted);
540648a55aa9Smistachkin if ( attr==INVALID_FILE_ATTRIBUTES ){
540755fbc869Smistachkin lastErrno = osGetLastError();
54081488052dSdrh if( lastErrno==ERROR_FILE_NOT_FOUND
54091488052dSdrh || lastErrno==ERROR_PATH_NOT_FOUND ){
541055fbc869Smistachkin rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
541155fbc869Smistachkin }else{
541255fbc869Smistachkin rc = SQLITE_ERROR;
541355fbc869Smistachkin }
541448a55aa9Smistachkin break;
541548a55aa9Smistachkin }
541648a55aa9Smistachkin if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
541748a55aa9Smistachkin rc = SQLITE_ERROR; /* Files only. */
541848a55aa9Smistachkin break;
541948a55aa9Smistachkin }
542048a55aa9Smistachkin if ( osDeleteFileA(zConverted) ){
542148a55aa9Smistachkin rc = SQLITE_OK; /* Deleted OK. */
542248a55aa9Smistachkin break;
542348a55aa9Smistachkin }
5424b324bc79Smistachkin if ( !winRetryIoerr(&cnt, &lastErrno) ){
542548a55aa9Smistachkin rc = SQLITE_ERROR; /* No more retries. */
542648a55aa9Smistachkin break;
542748a55aa9Smistachkin }
542848a55aa9Smistachkin } while(1);
5429153c62c4Sdrh }
5430d52ee729Sdrh #endif
54316d405c2cSdrh if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
54329f11ef12Smistachkin rc = winLogError(SQLITE_IOERR_DELETE, lastErrno, "winDelete", zFilename);
5433a32ad843Sdrh }else{
543421aa6a1aSdrh winLogIoerr(cnt, __LINE__);
5435a32ad843Sdrh }
54365f075388Smistachkin sqlite3_free(zConverted);
5437f2c1c99fSmistachkin OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
543852564d70Sdrh return rc;
5439153c62c4Sdrh }
5440153c62c4Sdrh
5441153c62c4Sdrh /*
544248864df9Smistachkin ** Check the existence and status of a file.
5443153c62c4Sdrh */
5444153c62c4Sdrh static int winAccess(
5445153c62c4Sdrh sqlite3_vfs *pVfs, /* Not used on win32 */
5446153c62c4Sdrh const char *zFilename, /* Name of file to check */
5447861f7456Sdanielk1977 int flags, /* Type of test to make on this file */
5448861f7456Sdanielk1977 int *pResOut /* OUT: Result */
5449153c62c4Sdrh ){
5450153c62c4Sdrh DWORD attr;
5451ea678832Sdrh int rc = 0;
545299391180Smistachkin DWORD lastErrno = 0;
5453cce1b689Sshaneh void *zConverted;
545418e526c1Sshane UNUSED_PARAMETER(pVfs);
5455cce1b689Sshaneh
5456cce1b689Sshaneh SimulateIOError( return SQLITE_IOERR_ACCESS; );
5457f2c1c99fSmistachkin OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
5458f2c1c99fSmistachkin zFilename, flags, pResOut));
5459f2c1c99fSmistachkin
5460d95a3d35Smistachkin zConverted = winConvertFromUtf8Filename(zFilename);
5461153c62c4Sdrh if( zConverted==0 ){
5462f2c1c99fSmistachkin OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
5463fad3039cSmistachkin return SQLITE_IOERR_NOMEM_BKPT;
5464153c62c4Sdrh }
5465b324bc79Smistachkin if( osIsNT() ){
5466fdf6db12Sdrh int cnt = 0;
5467722a7e9aSdrh WIN32_FILE_ATTRIBUTE_DATA sAttrData;
5468722a7e9aSdrh memset(&sAttrData, 0, sizeof(sAttrData));
5469318507b7Smistachkin while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
54707ea72591Sshaneh GetFileExInfoStandard,
5471b324bc79Smistachkin &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
5472fdf6db12Sdrh if( rc ){
5473722a7e9aSdrh /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
5474722a7e9aSdrh ** as if it does not exist.
5475722a7e9aSdrh */
54767ea72591Sshaneh if( flags==SQLITE_ACCESS_EXISTS
54777ea72591Sshaneh && sAttrData.nFileSizeHigh==0
54787ea72591Sshaneh && sAttrData.nFileSizeLow==0 ){
5479722a7e9aSdrh attr = INVALID_FILE_ATTRIBUTES;
54807ea72591Sshaneh }else{
54817ea72591Sshaneh attr = sAttrData.dwFileAttributes;
54827ea72591Sshaneh }
54837ea72591Sshaneh }else{
548421aa6a1aSdrh winLogIoerr(cnt, __LINE__);
548519038f1bSdrh if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
54865f075388Smistachkin sqlite3_free(zConverted);
54879f11ef12Smistachkin return winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess",
54889f11ef12Smistachkin zFilename);
5489cce1b689Sshaneh }else{
5490cce1b689Sshaneh attr = INVALID_FILE_ATTRIBUTES;
5491cce1b689Sshaneh }
5492722a7e9aSdrh }
5493153c62c4Sdrh }
5494d52ee729Sdrh #ifdef SQLITE_WIN32_HAS_ANSI
5495d52ee729Sdrh else{
5496d52ee729Sdrh attr = osGetFileAttributesA((char*)zConverted);
5497d52ee729Sdrh }
5498d52ee729Sdrh #endif
54995f075388Smistachkin sqlite3_free(zConverted);
5500153c62c4Sdrh switch( flags ){
550150d3f906Sdrh case SQLITE_ACCESS_READ:
5502153c62c4Sdrh case SQLITE_ACCESS_EXISTS:
5503820800d0Sshane rc = attr!=INVALID_FILE_ATTRIBUTES;
5504153c62c4Sdrh break;
5505153c62c4Sdrh case SQLITE_ACCESS_READWRITE:
55064e6b49b4Smistachkin rc = attr!=INVALID_FILE_ATTRIBUTES &&
55074e6b49b4Smistachkin (attr & FILE_ATTRIBUTE_READONLY)==0;
5508153c62c4Sdrh break;
5509153c62c4Sdrh default:
5510153c62c4Sdrh assert(!"Invalid flags argument");
5511153c62c4Sdrh }
5512861f7456Sdanielk1977 *pResOut = rc;
5513f2c1c99fSmistachkin OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
5514f2c1c99fSmistachkin zFilename, pResOut, *pResOut));
5515861f7456Sdanielk1977 return SQLITE_OK;
5516153c62c4Sdrh }
5517153c62c4Sdrh
5518d95a3d35Smistachkin /*
5519a8e41ecaSmistachkin ** Returns non-zero if the specified path name starts with the "long path"
5520a8e41ecaSmistachkin ** prefix.
5521a8e41ecaSmistachkin */
5522a8e41ecaSmistachkin static BOOL winIsLongPathPrefix(
5523a8e41ecaSmistachkin const char *zPathname
5524a8e41ecaSmistachkin ){
5525a8e41ecaSmistachkin return ( zPathname[0]=='\\' && zPathname[1]=='\\'
5526a8e41ecaSmistachkin && zPathname[2]=='?' && zPathname[3]=='\\' );
5527a8e41ecaSmistachkin }
5528a8e41ecaSmistachkin
5529a8e41ecaSmistachkin /*
5530d95a3d35Smistachkin ** Returns non-zero if the specified path name starts with a drive letter
5531d95a3d35Smistachkin ** followed by a colon character.
5532d95a3d35Smistachkin */
5533d95a3d35Smistachkin static BOOL winIsDriveLetterAndColon(
5534d95a3d35Smistachkin const char *zPathname
5535d95a3d35Smistachkin ){
5536d95a3d35Smistachkin return ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' );
5537d95a3d35Smistachkin }
5538153c62c4Sdrh
5539153c62c4Sdrh /*
5540a112d140Smistachkin ** Returns non-zero if the specified path name should be used verbatim. If
5541a112d140Smistachkin ** non-zero is returned from this function, the calling function must simply
5542a112d140Smistachkin ** use the provided path name verbatim -OR- resolve it into a full path name
5543a112d140Smistachkin ** using the GetFullPathName Win32 API function (if available).
5544a112d140Smistachkin */
5545a112d140Smistachkin static BOOL winIsVerbatimPathname(
5546a112d140Smistachkin const char *zPathname
5547a112d140Smistachkin ){
5548a112d140Smistachkin /*
5549a112d140Smistachkin ** If the path name starts with a forward slash or a backslash, it is either
5550a112d140Smistachkin ** a legal UNC name, a volume relative path, or an absolute path name in the
5551a112d140Smistachkin ** "Unix" format on Windows. There is no easy way to differentiate between
5552a112d140Smistachkin ** the final two cases; therefore, we return the safer return value of TRUE
5553a112d140Smistachkin ** so that callers of this function will simply use it verbatim.
5554a112d140Smistachkin */
5555533fb6deSmistachkin if ( winIsDirSep(zPathname[0]) ){
5556a112d140Smistachkin return TRUE;
5557a112d140Smistachkin }
5558a112d140Smistachkin
5559a112d140Smistachkin /*
5560a112d140Smistachkin ** If the path name starts with a letter and a colon it is either a volume
5561a112d140Smistachkin ** relative path or an absolute path. Callers of this function must not
5562a112d140Smistachkin ** attempt to treat it as a relative path name (i.e. they should simply use
5563a112d140Smistachkin ** it verbatim).
5564a112d140Smistachkin */
5565d95a3d35Smistachkin if ( winIsDriveLetterAndColon(zPathname) ){
5566a112d140Smistachkin return TRUE;
5567a112d140Smistachkin }
5568a112d140Smistachkin
5569a112d140Smistachkin /*
5570a112d140Smistachkin ** If we get to this point, the path name should almost certainly be a purely
5571a112d140Smistachkin ** relative one (i.e. not a UNC name, not absolute, and not volume relative).
5572a112d140Smistachkin */
5573a112d140Smistachkin return FALSE;
5574a112d140Smistachkin }
5575a112d140Smistachkin
5576a112d140Smistachkin /*
5577153c62c4Sdrh ** Turn a relative pathname into a full pathname. Write the full
5578153c62c4Sdrh ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
5579153c62c4Sdrh ** bytes in size.
5580153c62c4Sdrh */
5581fee64313Sdrh static int winFullPathnameNoMutex(
5582adfb9b05Sdanielk1977 sqlite3_vfs *pVfs, /* Pointer to vfs object */
5583adfb9b05Sdanielk1977 const char *zRelative, /* Possibly relative input path */
5584adfb9b05Sdanielk1977 int nFull, /* Size of output buffer in bytes */
5585adfb9b05Sdanielk1977 char *zFull /* Output buffer */
5586153c62c4Sdrh ){
55874a58d5fdSmistachkin #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
55884a58d5fdSmistachkin DWORD nByte;
55894a58d5fdSmistachkin void *zConverted;
55904a58d5fdSmistachkin char *zOut;
55914a58d5fdSmistachkin #endif
5592153c62c4Sdrh
5593a8e41ecaSmistachkin /* If this path name begins with "/X:" or "\\?\", where "X" is any
5594a8e41ecaSmistachkin ** alphabetic character, discard the initial "/" from the pathname.
55958a5fd261Sdrh */
5596a8e41ecaSmistachkin if( zRelative[0]=='/' && (winIsDriveLetterAndColon(zRelative+1)
5597a8e41ecaSmistachkin || winIsLongPathPrefix(zRelative+1)) ){
55988a5fd261Sdrh zRelative++;
55998a5fd261Sdrh }
56008a5fd261Sdrh
56014a58d5fdSmistachkin #if defined(__CYGWIN__)
56024a58d5fdSmistachkin SimulateIOError( return SQLITE_ERROR );
56034a58d5fdSmistachkin UNUSED_PARAMETER(nFull);
56044a58d5fdSmistachkin assert( nFull>=pVfs->mxPathname );
5605a112d140Smistachkin if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
5606a112d140Smistachkin /*
5607a112d140Smistachkin ** NOTE: We are dealing with a relative path name and the data
5608a112d140Smistachkin ** directory has been set. Therefore, use it as the basis
5609a112d140Smistachkin ** for converting the relative path name to an absolute
5610a112d140Smistachkin ** one by prepending the data directory and a slash.
5611a112d140Smistachkin */
56123741827eSmistachkin char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
56133741827eSmistachkin if( !zOut ){
5614fad3039cSmistachkin return SQLITE_IOERR_NOMEM_BKPT;
56153741827eSmistachkin }
56165bbb7198Smistachkin if( cygwin_conv_path(
56175bbb7198Smistachkin (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) |
56185bbb7198Smistachkin CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
56193741827eSmistachkin sqlite3_free(zOut);
56209f11ef12Smistachkin return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
56219f11ef12Smistachkin "winFullPathname1", zRelative);
5622a112d140Smistachkin }else{
56235bbb7198Smistachkin char *zUtf8 = winConvertToUtf8Filename(zOut);
56245bbb7198Smistachkin if( !zUtf8 ){
56255bbb7198Smistachkin sqlite3_free(zOut);
5626fad3039cSmistachkin return SQLITE_IOERR_NOMEM_BKPT;
5627aa82eba4Smistachkin }
562814eca4efSmistachkin sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
56295bbb7198Smistachkin sqlite3_data_directory, winGetDirSep(), zUtf8);
56305bbb7198Smistachkin sqlite3_free(zUtf8);
56313741827eSmistachkin sqlite3_free(zOut);
56325bbb7198Smistachkin }
5633a112d140Smistachkin }else{
56345bbb7198Smistachkin char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
56355bbb7198Smistachkin if( !zOut ){
5636fad3039cSmistachkin return SQLITE_IOERR_NOMEM_BKPT;
56375bbb7198Smistachkin }
56385bbb7198Smistachkin if( cygwin_conv_path(
56395bbb7198Smistachkin (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A),
56405bbb7198Smistachkin zRelative, zOut, pVfs->mxPathname+1)<0 ){
56415bbb7198Smistachkin sqlite3_free(zOut);
56429f11ef12Smistachkin return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
56439f11ef12Smistachkin "winFullPathname2", zRelative);
56445bbb7198Smistachkin }else{
56455bbb7198Smistachkin char *zUtf8 = winConvertToUtf8Filename(zOut);
56465bbb7198Smistachkin if( !zUtf8 ){
56475bbb7198Smistachkin sqlite3_free(zOut);
5648fad3039cSmistachkin return SQLITE_IOERR_NOMEM_BKPT;
56495bbb7198Smistachkin }
56505bbb7198Smistachkin sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
56515bbb7198Smistachkin sqlite3_free(zUtf8);
56525bbb7198Smistachkin sqlite3_free(zOut);
5653aa82eba4Smistachkin }
5654a112d140Smistachkin }
5655076f1c0dSdanielk1977 return SQLITE_OK;
5656153c62c4Sdrh #endif
5657153c62c4Sdrh
5658a112d140Smistachkin #if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__)
5659e2ad9317Sshaneh SimulateIOError( return SQLITE_ERROR );
5660153c62c4Sdrh /* WinCE has no concept of a relative pathname, or so I am told. */
5661df562d55Smistachkin /* WinRT has no way to convert a relative path to an absolute one. */
5662a112d140Smistachkin if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
5663a112d140Smistachkin /*
5664a112d140Smistachkin ** NOTE: We are dealing with a relative path name and the data
5665a112d140Smistachkin ** directory has been set. Therefore, use it as the basis
5666a112d140Smistachkin ** for converting the relative path name to an absolute
5667a112d140Smistachkin ** one by prepending the data directory and a backslash.
5668a112d140Smistachkin */
566914eca4efSmistachkin sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
567014eca4efSmistachkin sqlite3_data_directory, winGetDirSep(), zRelative);
5671a112d140Smistachkin }else{
5672a112d140Smistachkin sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
5673a112d140Smistachkin }
5674e825609aSdrh return SQLITE_OK;
5675153c62c4Sdrh #endif
5676153c62c4Sdrh
5677c5484654Smistachkin #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
5678e2ad9317Sshaneh /* It's odd to simulate an io-error here, but really this is just
5679e2ad9317Sshaneh ** using the io-error infrastructure to test that SQLite handles this
5680e2ad9317Sshaneh ** function failing. This function could fail if, for example, the
5681e2ad9317Sshaneh ** current working directory has been unlinked.
5682e2ad9317Sshaneh */
5683e2ad9317Sshaneh SimulateIOError( return SQLITE_ERROR );
5684a112d140Smistachkin if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
5685a112d140Smistachkin /*
5686a112d140Smistachkin ** NOTE: We are dealing with a relative path name and the data
5687a112d140Smistachkin ** directory has been set. Therefore, use it as the basis
5688a112d140Smistachkin ** for converting the relative path name to an absolute
5689a112d140Smistachkin ** one by prepending the data directory and a backslash.
5690a112d140Smistachkin */
569114eca4efSmistachkin sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
569214eca4efSmistachkin sqlite3_data_directory, winGetDirSep(), zRelative);
5693a112d140Smistachkin return SQLITE_OK;
5694a112d140Smistachkin }
5695d95a3d35Smistachkin zConverted = winConvertFromUtf8Filename(zRelative);
56965f075388Smistachkin if( zConverted==0 ){
5697fad3039cSmistachkin return SQLITE_IOERR_NOMEM_BKPT;
56985f075388Smistachkin }
5699b324bc79Smistachkin if( osIsNT() ){
5700318507b7Smistachkin LPWSTR zTemp;
57017ea11af2Smistachkin nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
57027ea11af2Smistachkin if( nByte==0 ){
57037ea11af2Smistachkin sqlite3_free(zConverted);
57049f11ef12Smistachkin return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
57059f11ef12Smistachkin "winFullPathname1", zRelative);
57067ea11af2Smistachkin }
57077ea11af2Smistachkin nByte += 3;
57087ea11af2Smistachkin zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
5709153c62c4Sdrh if( zTemp==0 ){
57105f075388Smistachkin sqlite3_free(zConverted);
5711fad3039cSmistachkin return SQLITE_IOERR_NOMEM_BKPT;
5712153c62c4Sdrh }
57137ea11af2Smistachkin nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
57147ea11af2Smistachkin if( nByte==0 ){
57155f075388Smistachkin sqlite3_free(zConverted);
57167ea11af2Smistachkin sqlite3_free(zTemp);
57179f11ef12Smistachkin return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
57189f11ef12Smistachkin "winFullPathname2", zRelative);
57197ea11af2Smistachkin }
5720b11caac3Sdrh sqlite3_free(zConverted);
5721b324bc79Smistachkin zOut = winUnicodeToUtf8(zTemp);
57225f075388Smistachkin sqlite3_free(zTemp);
5723d52ee729Sdrh }
5724d52ee729Sdrh #ifdef SQLITE_WIN32_HAS_ANSI
5725d52ee729Sdrh else{
5726153c62c4Sdrh char *zTemp;
57277ea11af2Smistachkin nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
57287ea11af2Smistachkin if( nByte==0 ){
57297ea11af2Smistachkin sqlite3_free(zConverted);
57309f11ef12Smistachkin return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
57319f11ef12Smistachkin "winFullPathname3", zRelative);
57327ea11af2Smistachkin }
57337ea11af2Smistachkin nByte += 3;
57347ea11af2Smistachkin zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
5735153c62c4Sdrh if( zTemp==0 ){
57365f075388Smistachkin sqlite3_free(zConverted);
5737fad3039cSmistachkin return SQLITE_IOERR_NOMEM_BKPT;
5738153c62c4Sdrh }
57397ea11af2Smistachkin nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
57407ea11af2Smistachkin if( nByte==0 ){
57417ea11af2Smistachkin sqlite3_free(zConverted);
57427ea11af2Smistachkin sqlite3_free(zTemp);
57439f11ef12Smistachkin return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
57449f11ef12Smistachkin "winFullPathname4", zRelative);
57457ea11af2Smistachkin }
57465f075388Smistachkin sqlite3_free(zConverted);
574712931203Smistachkin zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
57485f075388Smistachkin sqlite3_free(zTemp);
5749153c62c4Sdrh }
5750d52ee729Sdrh #endif
5751153c62c4Sdrh if( zOut ){
5752a112d140Smistachkin sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
57535f075388Smistachkin sqlite3_free(zOut);
5754153c62c4Sdrh return SQLITE_OK;
5755153c62c4Sdrh }else{
5756fad3039cSmistachkin return SQLITE_IOERR_NOMEM_BKPT;
5757153c62c4Sdrh }
5758153c62c4Sdrh #endif
5759153c62c4Sdrh }
5760fee64313Sdrh static int winFullPathname(
5761fee64313Sdrh sqlite3_vfs *pVfs, /* Pointer to vfs object */
5762fee64313Sdrh const char *zRelative, /* Possibly relative input path */
5763fee64313Sdrh int nFull, /* Size of output buffer in bytes */
5764fee64313Sdrh char *zFull /* Output buffer */
5765fee64313Sdrh ){
5766fee64313Sdrh int rc;
5767bdb6368dSmistachkin MUTEX_LOGIC( sqlite3_mutex *pMutex; )
5768bdb6368dSmistachkin MUTEX_LOGIC( pMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR); )
5769fee64313Sdrh sqlite3_mutex_enter(pMutex);
5770fee64313Sdrh rc = winFullPathnameNoMutex(pVfs, zRelative, nFull, zFull);
5771fee64313Sdrh sqlite3_mutex_leave(pMutex);
5772fee64313Sdrh return rc;
5773fee64313Sdrh }
5774153c62c4Sdrh
5775153c62c4Sdrh #ifndef SQLITE_OMIT_LOAD_EXTENSION
5776761df87eSdrh /*
5777761df87eSdrh ** Interfaces for opening a shared library, finding entry points
5778761df87eSdrh ** within the shared library, and closing the shared library.
5779761df87eSdrh */
5780153c62c4Sdrh static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
5781761df87eSdrh HANDLE h;
57825b4627e9Smistachkin #if defined(__CYGWIN__)
57835b4627e9Smistachkin int nFull = pVfs->mxPathname+1;
57845b4627e9Smistachkin char *zFull = sqlite3MallocZero( nFull );
57855b4627e9Smistachkin void *zConverted = 0;
57865b4627e9Smistachkin if( zFull==0 ){
5787edb4ece6Smistachkin OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
5788761df87eSdrh return 0;
5789761df87eSdrh }
57905b4627e9Smistachkin if( winFullPathname(pVfs, zFilename, nFull, zFull)!=SQLITE_OK ){
57915b4627e9Smistachkin sqlite3_free(zFull);
5792edb4ece6Smistachkin OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
57935b4627e9Smistachkin return 0;
57945b4627e9Smistachkin }
57955b4627e9Smistachkin zConverted = winConvertFromUtf8Filename(zFull);
57965b4627e9Smistachkin sqlite3_free(zFull);
57975b4627e9Smistachkin #else
5798d95a3d35Smistachkin void *zConverted = winConvertFromUtf8Filename(zFilename);
5799761df87eSdrh UNUSED_PARAMETER(pVfs);
58005b4627e9Smistachkin #endif
5801761df87eSdrh if( zConverted==0 ){
58021925a2e6Smistachkin OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
5803761df87eSdrh return 0;
5804761df87eSdrh }
5805b324bc79Smistachkin if( osIsNT() ){
5806df562d55Smistachkin #if SQLITE_OS_WINRT
58075483f772Smistachkin h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
58085483f772Smistachkin #else
5809318507b7Smistachkin h = osLoadLibraryW((LPCWSTR)zConverted);
58105483f772Smistachkin #endif
5811761df87eSdrh }
5812d52ee729Sdrh #ifdef SQLITE_WIN32_HAS_ANSI
5813d52ee729Sdrh else{
5814d52ee729Sdrh h = osLoadLibraryA((char*)zConverted);
5815d52ee729Sdrh }
5816d52ee729Sdrh #endif
58171925a2e6Smistachkin OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)h));
58185f075388Smistachkin sqlite3_free(zConverted);
5819761df87eSdrh return (void*)h;
5820761df87eSdrh }
5821153c62c4Sdrh static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
58221bd10f8aSdrh UNUSED_PARAMETER(pVfs);
5823b324bc79Smistachkin winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
5824153c62c4Sdrh }
58251488052dSdrh static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
5826151d05cdSmistachkin FARPROC proc;
58271bd10f8aSdrh UNUSED_PARAMETER(pVfs);
5828151d05cdSmistachkin proc = osGetProcAddressA((HANDLE)pH, zSym);
5829151d05cdSmistachkin OSTRACE(("DLSYM handle=%p, symbol=%s, address=%p\n",
5830151d05cdSmistachkin (void*)pH, zSym, (void*)proc));
5831151d05cdSmistachkin return (void(*)(void))proc;
5832761df87eSdrh }
5833134c4ff6Sdrh static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
58341bd10f8aSdrh UNUSED_PARAMETER(pVfs);
5835318507b7Smistachkin osFreeLibrary((HANDLE)pHandle);
58361925a2e6Smistachkin OSTRACE(("DLCLOSE handle=%p\n", (void*)pHandle));
5837761df87eSdrh }
5838153c62c4Sdrh #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
5839153c62c4Sdrh #define winDlOpen 0
5840153c62c4Sdrh #define winDlError 0
5841153c62c4Sdrh #define winDlSym 0
5842153c62c4Sdrh #define winDlClose 0
5843153c62c4Sdrh #endif
5844153c62c4Sdrh
58458eb3790aSdrh /* State information for the randomness gatherer. */
58468eb3790aSdrh typedef struct EntropyGatherer EntropyGatherer;
58478eb3790aSdrh struct EntropyGatherer {
58488eb3790aSdrh unsigned char *a; /* Gather entropy into this buffer */
58498eb3790aSdrh int na; /* Size of a[] in bytes */
58508eb3790aSdrh int i; /* XOR next input into a[i] */
58518eb3790aSdrh int nXor; /* Number of XOR operations done */
58528eb3790aSdrh };
58538eb3790aSdrh
58548eb3790aSdrh #if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
58558eb3790aSdrh /* Mix sz bytes of entropy into p. */
58568eb3790aSdrh static void xorMemory(EntropyGatherer *p, unsigned char *x, int sz){
58578eb3790aSdrh int j, k;
58588eb3790aSdrh for(j=0, k=p->i; j<sz; j++){
58598eb3790aSdrh p->a[k++] ^= x[j];
58608eb3790aSdrh if( k>=p->na ) k = 0;
58618eb3790aSdrh }
58628eb3790aSdrh p->i = k;
58638eb3790aSdrh p->nXor += sz;
58648eb3790aSdrh }
5865b71aef34Smistachkin #endif /* !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) */
5866761df87eSdrh
58670ccebe7eSdrh /*
5868153c62c4Sdrh ** Write up to nBuf bytes of randomness into zBuf.
5869bbd42a6dSdrh */
5870153c62c4Sdrh static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
58716a412b8bSdrh #if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS)
58728eb3790aSdrh UNUSED_PARAMETER(pVfs);
5873c7b7f1aeSshane memset(zBuf, 0, nBuf);
58748eb3790aSdrh return nBuf;
5875c7b7f1aeSshane #else
58768eb3790aSdrh EntropyGatherer e;
58778eb3790aSdrh UNUSED_PARAMETER(pVfs);
58788eb3790aSdrh memset(zBuf, 0, nBuf);
58798eb3790aSdrh e.a = (unsigned char*)zBuf;
58808eb3790aSdrh e.na = nBuf;
58818eb3790aSdrh e.nXor = 0;
58828eb3790aSdrh e.i = 0;
58838eb3790aSdrh {
5884d1a79312Sdrh SYSTEMTIME x;
5885318507b7Smistachkin osGetSystemTime(&x);
5886b71aef34Smistachkin xorMemory(&e, (unsigned char*)&x, sizeof(SYSTEMTIME));
5887bbd42a6dSdrh }
58888eb3790aSdrh {
5889318507b7Smistachkin DWORD pid = osGetCurrentProcessId();
5890b71aef34Smistachkin xorMemory(&e, (unsigned char*)&pid, sizeof(DWORD));
5891d1a79312Sdrh }
5892df562d55Smistachkin #if SQLITE_OS_WINRT
58938eb3790aSdrh {
5894df562d55Smistachkin ULONGLONG cnt = osGetTickCount64();
5895b71aef34Smistachkin xorMemory(&e, (unsigned char*)&cnt, sizeof(ULONGLONG));
5896df562d55Smistachkin }
5897df562d55Smistachkin #else
58988eb3790aSdrh {
5899318507b7Smistachkin DWORD cnt = osGetTickCount();
5900b71aef34Smistachkin xorMemory(&e, (unsigned char*)&cnt, sizeof(DWORD));
5901d1a79312Sdrh }
5902b71aef34Smistachkin #endif /* SQLITE_OS_WINRT */
59038eb3790aSdrh {
5904d1a79312Sdrh LARGE_INTEGER i;
5905318507b7Smistachkin osQueryPerformanceCounter(&i);
5906b71aef34Smistachkin xorMemory(&e, (unsigned char*)&i, sizeof(LARGE_INTEGER));
5907d1a79312Sdrh }
5908e45e0fb2Smistachkin #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
59098eb3790aSdrh {
5910e45e0fb2Smistachkin UUID id;
5911e45e0fb2Smistachkin memset(&id, 0, sizeof(UUID));
5912e45e0fb2Smistachkin osUuidCreate(&id);
59138eb3790aSdrh xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
5914e45e0fb2Smistachkin memset(&id, 0, sizeof(UUID));
5915e45e0fb2Smistachkin osUuidCreateSequential(&id);
59168eb3790aSdrh xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
5917e45e0fb2Smistachkin }
5918b71aef34Smistachkin #endif /* !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID */
59198eb3790aSdrh return e.nXor>nBuf ? nBuf : e.nXor;
59200ead47d1Smistachkin #endif /* defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) */
5921153c62c4Sdrh }
5922153c62c4Sdrh
5923bbd42a6dSdrh
5924bbd42a6dSdrh /*
5925bbd42a6dSdrh ** Sleep for a little while. Return the amount of time slept.
5926bbd42a6dSdrh */
5927153c62c4Sdrh static int winSleep(sqlite3_vfs *pVfs, int microsec){
5928f4f327ceSmistachkin sqlite3_win32_sleep((microsec+999)/1000);
59291bd10f8aSdrh UNUSED_PARAMETER(pVfs);
5930153c62c4Sdrh return ((microsec+999)/1000)*1000;
5931bbd42a6dSdrh }
5932bbd42a6dSdrh
5933bbd42a6dSdrh /*
593404882a9eSshaneh ** The following variable, if set to a non-zero value, is interpreted as
593504882a9eSshaneh ** the number of seconds since 1970 and is used to set the result of
593604882a9eSshaneh ** sqlite3OsCurrentTime() during testing.
5937bbd42a6dSdrh */
5938bbd42a6dSdrh #ifdef SQLITE_TEST
593904882a9eSshaneh int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
5940bbd42a6dSdrh #endif
5941bbd42a6dSdrh
5942bbd42a6dSdrh /*
594304882a9eSshaneh ** Find the current time (in Universal Coordinated Time). Write into *piNow
594404882a9eSshaneh ** the current time and date as a Julian Day number times 86_400_000. In
594504882a9eSshaneh ** other words, write into *piNow the number of milliseconds since the Julian
594604882a9eSshaneh ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
594704882a9eSshaneh ** proleptic Gregorian calendar.
594804882a9eSshaneh **
59493170225fSdrh ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
59503170225fSdrh ** cannot be found.
5951bbd42a6dSdrh */
595204882a9eSshaneh static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
5953bbd42a6dSdrh /* FILETIME structure is a 64-bit value representing the number of
5954bbd42a6dSdrh 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
5955bbd42a6dSdrh */
595604882a9eSshaneh FILETIME ft;
595704882a9eSshaneh static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
595804882a9eSshaneh #ifdef SQLITE_TEST
595904882a9eSshaneh static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
596004882a9eSshaneh #endif
5961b08a67a7Sshane /* 2^32 - to avoid use of LL and warnings in gcc */
5962b08a67a7Sshane static const sqlite3_int64 max32BitValue =
59631488052dSdrh (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
59641488052dSdrh (sqlite3_int64)294967296;
59655c905d6eSdrh
596629bafeabSdanielk1977 #if SQLITE_OS_WINCE
5967cc78fea4Sdrh SYSTEMTIME time;
5968318507b7Smistachkin osGetSystemTime(&time);
5969820800d0Sshane /* if SystemTimeToFileTime() fails, it returns zero. */
5970318507b7Smistachkin if (!osSystemTimeToFileTime(&time,&ft)){
59713170225fSdrh return SQLITE_ERROR;
5972820800d0Sshane }
5973cc78fea4Sdrh #else
5974318507b7Smistachkin osGetSystemTimeAsFileTime( &ft );
5975cc78fea4Sdrh #endif
597604882a9eSshaneh
597704882a9eSshaneh *piNow = winFiletimeEpoch +
597804882a9eSshaneh ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
5979b316cb22Sdrh (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
598004882a9eSshaneh
5981bbd42a6dSdrh #ifdef SQLITE_TEST
5982bbd42a6dSdrh if( sqlite3_current_time ){
598304882a9eSshaneh *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
5984bbd42a6dSdrh }
5985bbd42a6dSdrh #endif
598604882a9eSshaneh UNUSED_PARAMETER(pVfs);
59873170225fSdrh return SQLITE_OK;
5988bbd42a6dSdrh }
5989bbd42a6dSdrh
5990820800d0Sshane /*
599104882a9eSshaneh ** Find the current time (in Universal Coordinated Time). Write the
599204882a9eSshaneh ** current time and date as a Julian Day number into *prNow and
599304882a9eSshaneh ** return 0. Return 1 if the time and date cannot be found.
599404882a9eSshaneh */
5995134c4ff6Sdrh static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
599604882a9eSshaneh int rc;
599704882a9eSshaneh sqlite3_int64 i;
599804882a9eSshaneh rc = winCurrentTimeInt64(pVfs, &i);
599904882a9eSshaneh if( !rc ){
600004882a9eSshaneh *prNow = i/86400000.0;
600104882a9eSshaneh }
600204882a9eSshaneh return rc;
600304882a9eSshaneh }
600404882a9eSshaneh
600504882a9eSshaneh /*
6006820800d0Sshane ** The idea is that this function works like a combination of
6007318507b7Smistachkin ** GetLastError() and FormatMessage() on Windows (or errno and
6008318507b7Smistachkin ** strerror_r() on Unix). After an error is returned by an OS
6009820800d0Sshane ** function, SQLite calls this function with zBuf pointing to
6010820800d0Sshane ** a buffer of nBuf bytes. The OS layer should populate the
6011820800d0Sshane ** buffer with a nul-terminated UTF-8 encoded error message
6012be217793Sshane ** describing the last IO error to have occurred within the calling
6013820800d0Sshane ** thread.
6014820800d0Sshane **
6015820800d0Sshane ** If the error message is too large for the supplied buffer,
6016820800d0Sshane ** it should be truncated. The return value of xGetLastError
6017820800d0Sshane ** is zero if the error message fits in the buffer, or non-zero
6018820800d0Sshane ** otherwise (if the message was truncated). If non-zero is returned,
6019820800d0Sshane ** then it is not necessary to include the nul-terminator character
6020820800d0Sshane ** in the output buffer.
6021820800d0Sshane **
6022820800d0Sshane ** Not supplying an error message will have no adverse effect
6023820800d0Sshane ** on SQLite. It is fine to have an implementation that never
6024820800d0Sshane ** returns an error message:
6025820800d0Sshane **
6026820800d0Sshane ** int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
6027820800d0Sshane ** assert(zBuf[0]=='\0');
6028820800d0Sshane ** return 0;
6029820800d0Sshane ** }
6030820800d0Sshane **
6031820800d0Sshane ** However if an error message is supplied, it will be incorporated
6032820800d0Sshane ** by sqlite into the error message available to the user using
6033820800d0Sshane ** sqlite3_errmsg(), possibly making IO errors easier to debug.
6034820800d0Sshane */
6035bcb97fe9Sdanielk1977 static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
6036762e32bcSdrh DWORD e = osGetLastError();
60371bd10f8aSdrh UNUSED_PARAMETER(pVfs);
6038762e32bcSdrh if( nBuf>0 ) winGetLastErrorMsg(e, nBuf, zBuf);
6039762e32bcSdrh return e;
6040bcb97fe9Sdanielk1977 }
6041b4bc7057Sdrh
6042b4bc7057Sdrh /*
6043c0fa4c5fSdanielk1977 ** Initialize and deinitialize the operating system interface.
604413a68c3fSdanielk1977 */
6045c0fa4c5fSdanielk1977 int sqlite3_os_init(void){
6046153c62c4Sdrh static sqlite3_vfs winVfs = {
604799ab3b12Sdrh 3, /* iVersion */
6048153c62c4Sdrh sizeof(winFile), /* szOsFile */
604931706a2dSmistachkin SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
6050153c62c4Sdrh 0, /* pNext */
6051153c62c4Sdrh "win32", /* zName */
60521e75483cSmistachkin &winAppData, /* pAppData */
6053153c62c4Sdrh winOpen, /* xOpen */
6054153c62c4Sdrh winDelete, /* xDelete */
6055153c62c4Sdrh winAccess, /* xAccess */
6056153c62c4Sdrh winFullPathname, /* xFullPathname */
6057153c62c4Sdrh winDlOpen, /* xDlOpen */
6058153c62c4Sdrh winDlError, /* xDlError */
6059153c62c4Sdrh winDlSym, /* xDlSym */
6060153c62c4Sdrh winDlClose, /* xDlClose */
6061153c62c4Sdrh winRandomness, /* xRandomness */
6062153c62c4Sdrh winSleep, /* xSleep */
6063bcb97fe9Sdanielk1977 winCurrentTime, /* xCurrentTime */
6064af75c869Sdrh winGetLastError, /* xGetLastError */
606504882a9eSshaneh winCurrentTimeInt64, /* xCurrentTimeInt64 */
6066318507b7Smistachkin winSetSystemCall, /* xSetSystemCall */
6067318507b7Smistachkin winGetSystemCall, /* xGetSystemCall */
6068318507b7Smistachkin winNextSystemCall, /* xNextSystemCall */
6069153c62c4Sdrh };
60703741827eSmistachkin #if defined(SQLITE_WIN32_HAS_WIDE)
60713741827eSmistachkin static sqlite3_vfs winLongPathVfs = {
60723741827eSmistachkin 3, /* iVersion */
60733741827eSmistachkin sizeof(winFile), /* szOsFile */
60743741827eSmistachkin SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
60753741827eSmistachkin 0, /* pNext */
60763741827eSmistachkin "win32-longpath", /* zName */
60771e75483cSmistachkin &winAppData, /* pAppData */
60781e75483cSmistachkin winOpen, /* xOpen */
60791e75483cSmistachkin winDelete, /* xDelete */
60801e75483cSmistachkin winAccess, /* xAccess */
60811e75483cSmistachkin winFullPathname, /* xFullPathname */
60821e75483cSmistachkin winDlOpen, /* xDlOpen */
60831e75483cSmistachkin winDlError, /* xDlError */
60841e75483cSmistachkin winDlSym, /* xDlSym */
60851e75483cSmistachkin winDlClose, /* xDlClose */
60861e75483cSmistachkin winRandomness, /* xRandomness */
60871e75483cSmistachkin winSleep, /* xSleep */
60881e75483cSmistachkin winCurrentTime, /* xCurrentTime */
60891e75483cSmistachkin winGetLastError, /* xGetLastError */
60901e75483cSmistachkin winCurrentTimeInt64, /* xCurrentTimeInt64 */
60911e75483cSmistachkin winSetSystemCall, /* xSetSystemCall */
60921e75483cSmistachkin winGetSystemCall, /* xGetSystemCall */
60931e75483cSmistachkin winNextSystemCall, /* xNextSystemCall */
60941e75483cSmistachkin };
60951e75483cSmistachkin #endif
60961e75483cSmistachkin static sqlite3_vfs winNolockVfs = {
60971e75483cSmistachkin 3, /* iVersion */
60981e75483cSmistachkin sizeof(winFile), /* szOsFile */
60991e75483cSmistachkin SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
61001e75483cSmistachkin 0, /* pNext */
61011e75483cSmistachkin "win32-none", /* zName */
61021e75483cSmistachkin &winNolockAppData, /* pAppData */
61031e75483cSmistachkin winOpen, /* xOpen */
61041e75483cSmistachkin winDelete, /* xDelete */
61051e75483cSmistachkin winAccess, /* xAccess */
61061e75483cSmistachkin winFullPathname, /* xFullPathname */
61071e75483cSmistachkin winDlOpen, /* xDlOpen */
61081e75483cSmistachkin winDlError, /* xDlError */
61091e75483cSmistachkin winDlSym, /* xDlSym */
61101e75483cSmistachkin winDlClose, /* xDlClose */
61111e75483cSmistachkin winRandomness, /* xRandomness */
61121e75483cSmistachkin winSleep, /* xSleep */
61131e75483cSmistachkin winCurrentTime, /* xCurrentTime */
61141e75483cSmistachkin winGetLastError, /* xGetLastError */
61151e75483cSmistachkin winCurrentTimeInt64, /* xCurrentTimeInt64 */
61161e75483cSmistachkin winSetSystemCall, /* xSetSystemCall */
61171e75483cSmistachkin winGetSystemCall, /* xGetSystemCall */
61181e75483cSmistachkin winNextSystemCall, /* xNextSystemCall */
61191e75483cSmistachkin };
61201e75483cSmistachkin #if defined(SQLITE_WIN32_HAS_WIDE)
61211e75483cSmistachkin static sqlite3_vfs winLongPathNolockVfs = {
61221e75483cSmistachkin 3, /* iVersion */
61231e75483cSmistachkin sizeof(winFile), /* szOsFile */
61241e75483cSmistachkin SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
61251e75483cSmistachkin 0, /* pNext */
61261e75483cSmistachkin "win32-longpath-none", /* zName */
61271e75483cSmistachkin &winNolockAppData, /* pAppData */
61283741827eSmistachkin winOpen, /* xOpen */
61293741827eSmistachkin winDelete, /* xDelete */
61303741827eSmistachkin winAccess, /* xAccess */
61313741827eSmistachkin winFullPathname, /* xFullPathname */
61323741827eSmistachkin winDlOpen, /* xDlOpen */
61333741827eSmistachkin winDlError, /* xDlError */
61343741827eSmistachkin winDlSym, /* xDlSym */
61353741827eSmistachkin winDlClose, /* xDlClose */
61363741827eSmistachkin winRandomness, /* xRandomness */
61373741827eSmistachkin winSleep, /* xSleep */
61383741827eSmistachkin winCurrentTime, /* xCurrentTime */
61393741827eSmistachkin winGetLastError, /* xGetLastError */
61403741827eSmistachkin winCurrentTimeInt64, /* xCurrentTimeInt64 */
61413741827eSmistachkin winSetSystemCall, /* xSetSystemCall */
61423741827eSmistachkin winGetSystemCall, /* xGetSystemCall */
61433741827eSmistachkin winNextSystemCall, /* xNextSystemCall */
61443741827eSmistachkin };
61453741827eSmistachkin #endif
6146e1ab2193Sdan
6147318507b7Smistachkin /* Double-check that the aSyscall[] array has been constructed
6148318507b7Smistachkin ** correctly. See ticket [bb3a86e890c8e96ab] */
6149ccb43714Smistachkin assert( ArraySize(aSyscall)==80 );
61507acec68aSdrh
6151420398ceSshaneh /* get memory map allocation granularity */
6152420398ceSshaneh memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
6153df562d55Smistachkin #if SQLITE_OS_WINRT
6154df562d55Smistachkin osGetNativeSystemInfo(&winSysInfo);
6155df562d55Smistachkin #else
6156318507b7Smistachkin osGetSystemInfo(&winSysInfo);
6157df562d55Smistachkin #endif
6158420398ceSshaneh assert( winSysInfo.dwAllocationGranularity>0 );
6159daf9a5a4Smistachkin assert( winSysInfo.dwPageSize>0 );
6160420398ceSshaneh
6161c0fa4c5fSdanielk1977 sqlite3_vfs_register(&winVfs, 1);
61623741827eSmistachkin
61633741827eSmistachkin #if defined(SQLITE_WIN32_HAS_WIDE)
61643741827eSmistachkin sqlite3_vfs_register(&winLongPathVfs, 0);
61653741827eSmistachkin #endif
61663741827eSmistachkin
61671e75483cSmistachkin sqlite3_vfs_register(&winNolockVfs, 0);
61681e75483cSmistachkin
61691e75483cSmistachkin #if defined(SQLITE_WIN32_HAS_WIDE)
61701e75483cSmistachkin sqlite3_vfs_register(&winLongPathNolockVfs, 0);
61711e75483cSmistachkin #endif
61721e75483cSmistachkin
6173bc6b8d73Smistachkin #ifndef SQLITE_OMIT_WAL
6174435666e2Smistachkin winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
6175bc6b8d73Smistachkin #endif
6176bc6b8d73Smistachkin
6177c0fa4c5fSdanielk1977 return SQLITE_OK;
617813a68c3fSdanielk1977 }
61796c3c1a09Smistachkin
6180c0fa4c5fSdanielk1977 int sqlite3_os_end(void){
61817acec68aSdrh #if SQLITE_OS_WINRT
6182f4f327ceSmistachkin if( sleepObj!=NULL ){
618375b70a20Smistachkin osCloseHandle(sleepObj);
6184287a48d6Smistachkin sleepObj = NULL;
6185f4f327ceSmistachkin }
61867acec68aSdrh #endif
6187bc6b8d73Smistachkin
6188bc6b8d73Smistachkin #ifndef SQLITE_OMIT_WAL
6189435666e2Smistachkin winBigLock = 0;
6190bc6b8d73Smistachkin #endif
6191bc6b8d73Smistachkin
6192c0fa4c5fSdanielk1977 return SQLITE_OK;
6193c0fa4c5fSdanielk1977 }
619440257ffdSdrh
619529bafeabSdanielk1977 #endif /* SQLITE_OS_WIN */
6196