drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 1 | /* |
| 2 | ** 2004 May 22 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ****************************************************************************** |
| 12 | ** |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 13 | ** This file contains code that is specific to Windows. |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 14 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 15 | #include "sqliteInt.h" |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 16 | #if SQLITE_OS_WIN /* This file is used for Windows only */ |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 17 | |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 18 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 19 | ** Include code that is common to all os_*.c files |
| 20 | */ |
| 21 | #include "os_common.h" |
| 22 | |
| 23 | /* |
mistachkin | 8bc5262 | 2013-11-25 09:36:07 | [diff] [blame] | 24 | ** Include the header file for the Windows VFS. |
| 25 | */ |
| 26 | #include "os_win.h" |
| 27 | |
| 28 | /* |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 29 | ** Compiling and using WAL mode requires several APIs that are only |
| 30 | ** available in Windows platforms based on the NT kernel. |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 31 | */ |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 32 | #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL) |
mistachkin | 4496a23 | 2013-08-26 23:18:57 | [diff] [blame] | 33 | # error "WAL mode requires support from the Windows NT kernel, compile\ |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 34 | with SQLITE_OMIT_WAL." |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 35 | #endif |
| 36 | |
mistachkin | 7376782 | 2014-11-04 19:37:22 | [diff] [blame] | 37 | #if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE>0 |
| 38 | # error "Memory mapped files require support from the Windows NT kernel,\ |
| 39 | compile with SQLITE_MAX_MMAP_SIZE=0." |
| 40 | #endif |
| 41 | |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 42 | /* |
mistachkin | f1dacbf | 2012-10-07 00:52:22 | [diff] [blame] | 43 | ** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions |
| 44 | ** based on the sub-platform)? |
| 45 | */ |
mistachkin | 28159a5 | 2013-08-31 17:01:56 | [diff] [blame] | 46 | #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI) |
mistachkin | f1dacbf | 2012-10-07 00:52:22 | [diff] [blame] | 47 | # define SQLITE_WIN32_HAS_ANSI |
| 48 | #endif |
| 49 | |
| 50 | /* |
| 51 | ** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions |
| 52 | ** based on the sub-platform)? |
| 53 | */ |
mistachkin | 28159a5 | 2013-08-31 17:01:56 | [diff] [blame] | 54 | #if (SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT) && \ |
| 55 | !defined(SQLITE_WIN32_NO_WIDE) |
mistachkin | f1dacbf | 2012-10-07 00:52:22 | [diff] [blame] | 56 | # define SQLITE_WIN32_HAS_WIDE |
| 57 | #endif |
| 58 | |
| 59 | /* |
mistachkin | 28159a5 | 2013-08-31 17:01:56 | [diff] [blame] | 60 | ** Make sure at least one set of Win32 APIs is available. |
| 61 | */ |
| 62 | #if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE) |
| 63 | # error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\ |
| 64 | must be defined." |
| 65 | #endif |
| 66 | |
| 67 | /* |
mistachkin | 0f7e08e | 2013-11-27 03:01:34 | [diff] [blame] | 68 | ** Define the required Windows SDK version constants if they are not |
| 69 | ** already available. |
| 70 | */ |
| 71 | #ifndef NTDDI_WIN8 |
| 72 | # define NTDDI_WIN8 0x06020000 |
| 73 | #endif |
| 74 | |
| 75 | #ifndef NTDDI_WINBLUE |
| 76 | # define NTDDI_WINBLUE 0x06030000 |
| 77 | #endif |
| 78 | |
mistachkin | d5be6f0 | 2016-01-27 07:28:33 | [diff] [blame] | 79 | #ifndef NTDDI_WINTHRESHOLD |
| 80 | # define NTDDI_WINTHRESHOLD 0x06040000 |
| 81 | #endif |
| 82 | |
mistachkin | 0f7e08e | 2013-11-27 03:01:34 | [diff] [blame] | 83 | /* |
drh | c96c7e3 | 2014-08-11 17:40:30 | [diff] [blame] | 84 | ** Check to see if the GetVersionEx[AW] functions are deprecated on the |
drh | e5e20d3 | 2014-08-11 17:41:53 | [diff] [blame] | 85 | ** target system. GetVersionEx was first deprecated in Win8.1. |
mistachkin | 0f7e08e | 2013-11-27 03:01:34 | [diff] [blame] | 86 | */ |
| 87 | #ifndef SQLITE_WIN32_GETVERSIONEX |
| 88 | # if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE |
drh | c96c7e3 | 2014-08-11 17:40:30 | [diff] [blame] | 89 | # define SQLITE_WIN32_GETVERSIONEX 0 /* GetVersionEx() is deprecated */ |
mistachkin | 0f7e08e | 2013-11-27 03:01:34 | [diff] [blame] | 90 | # else |
drh | c96c7e3 | 2014-08-11 17:40:30 | [diff] [blame] | 91 | # define SQLITE_WIN32_GETVERSIONEX 1 /* GetVersionEx() is current */ |
mistachkin | 0f7e08e | 2013-11-27 03:01:34 | [diff] [blame] | 92 | # endif |
| 93 | #endif |
| 94 | |
| 95 | /* |
mistachkin | d5be6f0 | 2016-01-27 07:28:33 | [diff] [blame] | 96 | ** Check to see if the CreateFileMappingA function is supported on the |
| 97 | ** target system. It is unavailable when using "mincore.lib" on Win10. |
| 98 | ** When compiling for Windows 10, always assume "mincore.lib" is in use. |
| 99 | */ |
| 100 | #ifndef SQLITE_WIN32_CREATEFILEMAPPINGA |
| 101 | # if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINTHRESHOLD |
| 102 | # define SQLITE_WIN32_CREATEFILEMAPPINGA 0 |
| 103 | # else |
| 104 | # define SQLITE_WIN32_CREATEFILEMAPPINGA 1 |
| 105 | # endif |
| 106 | #endif |
| 107 | |
| 108 | /* |
mistachkin | 9858bf2 | 2013-11-09 23:55:18 | [diff] [blame] | 109 | ** This constant should already be defined (in the "WinDef.h" SDK file). |
| 110 | */ |
| 111 | #ifndef MAX_PATH |
| 112 | # define MAX_PATH (260) |
| 113 | #endif |
| 114 | |
| 115 | /* |
mistachkin | 31706a2 | 2013-08-26 20:45:50 | [diff] [blame] | 116 | ** Maximum pathname length (in chars) for Win32. This should normally be |
| 117 | ** MAX_PATH. |
| 118 | */ |
| 119 | #ifndef SQLITE_WIN32_MAX_PATH_CHARS |
| 120 | # define SQLITE_WIN32_MAX_PATH_CHARS (MAX_PATH) |
| 121 | #endif |
| 122 | |
| 123 | /* |
mistachkin | 9858bf2 | 2013-11-09 23:55:18 | [diff] [blame] | 124 | ** This constant should already be defined (in the "WinNT.h" SDK file). |
| 125 | */ |
| 126 | #ifndef UNICODE_STRING_MAX_CHARS |
| 127 | # define UNICODE_STRING_MAX_CHARS (32767) |
| 128 | #endif |
| 129 | |
| 130 | /* |
mistachkin | 4496a23 | 2013-08-26 23:18:57 | [diff] [blame] | 131 | ** Maximum pathname length (in chars) for WinNT. This should normally be |
mistachkin | dc7b055 | 2013-11-09 21:11:36 | [diff] [blame] | 132 | ** UNICODE_STRING_MAX_CHARS. |
mistachkin | 4496a23 | 2013-08-26 23:18:57 | [diff] [blame] | 133 | */ |
| 134 | #ifndef SQLITE_WINNT_MAX_PATH_CHARS |
mistachkin | dc7b055 | 2013-11-09 21:11:36 | [diff] [blame] | 135 | # define SQLITE_WINNT_MAX_PATH_CHARS (UNICODE_STRING_MAX_CHARS) |
mistachkin | 4496a23 | 2013-08-26 23:18:57 | [diff] [blame] | 136 | #endif |
| 137 | |
| 138 | /* |
mistachkin | e49d98f | 2013-08-24 23:55:01 | [diff] [blame] | 139 | ** Maximum pathname length (in bytes) for Win32. The MAX_PATH macro is in |
mistachkin | 0804649 | 2013-11-10 00:03:11 | [diff] [blame] | 140 | ** characters, so we allocate 4 bytes per character assuming worst-case of |
mistachkin | 4496a23 | 2013-08-26 23:18:57 | [diff] [blame] | 141 | ** 4-bytes-per-character for UTF8. |
mistachkin | e49d98f | 2013-08-24 23:55:01 | [diff] [blame] | 142 | */ |
mistachkin | 31706a2 | 2013-08-26 20:45:50 | [diff] [blame] | 143 | #ifndef SQLITE_WIN32_MAX_PATH_BYTES |
mistachkin | 4496a23 | 2013-08-26 23:18:57 | [diff] [blame] | 144 | # define SQLITE_WIN32_MAX_PATH_BYTES (SQLITE_WIN32_MAX_PATH_CHARS*4) |
| 145 | #endif |
| 146 | |
| 147 | /* |
| 148 | ** Maximum pathname length (in bytes) for WinNT. This should normally be |
mistachkin | dc7b055 | 2013-11-09 21:11:36 | [diff] [blame] | 149 | ** UNICODE_STRING_MAX_CHARS * sizeof(WCHAR). |
mistachkin | 4496a23 | 2013-08-26 23:18:57 | [diff] [blame] | 150 | */ |
| 151 | #ifndef SQLITE_WINNT_MAX_PATH_BYTES |
| 152 | # define SQLITE_WINNT_MAX_PATH_BYTES \ |
| 153 | (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS) |
mistachkin | e49d98f | 2013-08-24 23:55:01 | [diff] [blame] | 154 | #endif |
| 155 | |
| 156 | /* |
mistachkin | 31706a2 | 2013-08-26 20:45:50 | [diff] [blame] | 157 | ** Maximum error message length (in chars) for WinRT. |
mistachkin | e49d98f | 2013-08-24 23:55:01 | [diff] [blame] | 158 | */ |
mistachkin | 31706a2 | 2013-08-26 20:45:50 | [diff] [blame] | 159 | #ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS |
| 160 | # define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024) |
mistachkin | e49d98f | 2013-08-24 23:55:01 | [diff] [blame] | 161 | #endif |
| 162 | |
| 163 | /* |
mistachkin | 533fb6d | 2013-08-28 02:26:48 | [diff] [blame] | 164 | ** Returns non-zero if the character should be treated as a directory |
| 165 | ** separator. |
| 166 | */ |
| 167 | #ifndef winIsDirSep |
| 168 | # define winIsDirSep(a) (((a) == '/') || ((a) == '\\')) |
| 169 | #endif |
| 170 | |
| 171 | /* |
mistachkin | 36ca535 | 2013-09-12 01:47:57 | [diff] [blame] | 172 | ** This macro is used when a local variable is set to a value that is |
| 173 | ** [sometimes] not used by the code (e.g. via conditional compilation). |
| 174 | */ |
| 175 | #ifndef UNUSED_VARIABLE_VALUE |
mistachkin | 91d1249 | 2014-08-11 17:38:38 | [diff] [blame] | 176 | # define UNUSED_VARIABLE_VALUE(x) (void)(x) |
mistachkin | 36ca535 | 2013-09-12 01:47:57 | [diff] [blame] | 177 | #endif |
| 178 | |
| 179 | /* |
mistachkin | 14eca4e | 2013-11-07 22:11:55 | [diff] [blame] | 180 | ** Returns the character that should be used as the directory separator. |
mistachkin | 533fb6d | 2013-08-28 02:26:48 | [diff] [blame] | 181 | */ |
mistachkin | 14eca4e | 2013-11-07 22:11:55 | [diff] [blame] | 182 | #ifndef winGetDirSep |
| 183 | # define winGetDirSep() '\\' |
mistachkin | 533fb6d | 2013-08-28 02:26:48 | [diff] [blame] | 184 | #endif |
| 185 | |
| 186 | /* |
mistachkin | f1dacbf | 2012-10-07 00:52:22 | [diff] [blame] | 187 | ** Do we need to manually define the Win32 file mapping APIs for use with WAL |
mistachkin | 7376782 | 2014-11-04 19:37:22 | [diff] [blame] | 188 | ** mode or memory mapped files (e.g. these APIs are available in the Windows |
| 189 | ** CE SDK; however, they are not present in the header file)? |
mistachkin | f1dacbf | 2012-10-07 00:52:22 | [diff] [blame] | 190 | */ |
mistachkin | 7376782 | 2014-11-04 19:37:22 | [diff] [blame] | 191 | #if SQLITE_WIN32_FILEMAPPING_API && \ |
| 192 | (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) |
mistachkin | f1dacbf | 2012-10-07 00:52:22 | [diff] [blame] | 193 | /* |
| 194 | ** Two of the file mapping APIs are different under WinRT. Figure out which |
| 195 | ** set we need. |
| 196 | */ |
| 197 | #if SQLITE_OS_WINRT |
mistachkin | 31d7211 | 2012-10-08 14:36:42 | [diff] [blame] | 198 | WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \ |
| 199 | LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR); |
mistachkin | f1dacbf | 2012-10-07 00:52:22 | [diff] [blame] | 200 | |
mistachkin | 31d7211 | 2012-10-08 14:36:42 | [diff] [blame] | 201 | WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T); |
mistachkin | f1dacbf | 2012-10-07 00:52:22 | [diff] [blame] | 202 | #else |
| 203 | #if defined(SQLITE_WIN32_HAS_ANSI) |
mistachkin | 31d7211 | 2012-10-08 14:36:42 | [diff] [blame] | 204 | WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \ |
| 205 | DWORD, DWORD, DWORD, LPCSTR); |
mistachkin | f1dacbf | 2012-10-07 00:52:22 | [diff] [blame] | 206 | #endif /* defined(SQLITE_WIN32_HAS_ANSI) */ |
| 207 | |
| 208 | #if defined(SQLITE_WIN32_HAS_WIDE) |
mistachkin | 31d7211 | 2012-10-08 14:36:42 | [diff] [blame] | 209 | WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \ |
| 210 | DWORD, DWORD, DWORD, LPCWSTR); |
mistachkin | f1dacbf | 2012-10-07 00:52:22 | [diff] [blame] | 211 | #endif /* defined(SQLITE_WIN32_HAS_WIDE) */ |
| 212 | |
mistachkin | 31d7211 | 2012-10-08 14:36:42 | [diff] [blame] | 213 | WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T); |
mistachkin | f1dacbf | 2012-10-07 00:52:22 | [diff] [blame] | 214 | #endif /* SQLITE_OS_WINRT */ |
| 215 | |
| 216 | /* |
mistachkin | ccb4371 | 2015-03-26 23:36:35 | [diff] [blame] | 217 | ** These file mapping APIs are common to both Win32 and WinRT. |
mistachkin | f1dacbf | 2012-10-07 00:52:22 | [diff] [blame] | 218 | */ |
mistachkin | ccb4371 | 2015-03-26 23:36:35 | [diff] [blame] | 219 | |
| 220 | WINBASEAPI BOOL WINAPI FlushViewOfFile(LPCVOID, SIZE_T); |
mistachkin | 31d7211 | 2012-10-08 14:36:42 | [diff] [blame] | 221 | WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID); |
mistachkin | 7376782 | 2014-11-04 19:37:22 | [diff] [blame] | 222 | #endif /* SQLITE_WIN32_FILEMAPPING_API */ |
mistachkin | f1dacbf | 2012-10-07 00:52:22 | [diff] [blame] | 223 | |
| 224 | /* |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 225 | ** Some Microsoft compilers lack this definition. |
shane | 171fa29 | 2008-09-01 22:15:18 | [diff] [blame] | 226 | */ |
| 227 | #ifndef INVALID_FILE_ATTRIBUTES |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 228 | # define INVALID_FILE_ATTRIBUTES ((DWORD)-1) |
shane | 171fa29 | 2008-09-01 22:15:18 | [diff] [blame] | 229 | #endif |
| 230 | |
mistachkin | 5483f77 | 2012-03-07 20:11:47 | [diff] [blame] | 231 | #ifndef FILE_FLAG_MASK |
| 232 | # define FILE_FLAG_MASK (0xFF3C0000) |
| 233 | #endif |
| 234 | |
| 235 | #ifndef FILE_ATTRIBUTE_MASK |
| 236 | # define FILE_ATTRIBUTE_MASK (0x0003FFF7) |
| 237 | #endif |
| 238 | |
mistachkin | 6f92833 | 2012-08-17 11:47:32 | [diff] [blame] | 239 | #ifndef SQLITE_OMIT_WAL |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 240 | /* Forward references to structures used for WAL */ |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 241 | typedef struct winShm winShm; /* A connection to shared-memory */ |
| 242 | typedef struct winShmNode winShmNode; /* A region of shared-memory */ |
mistachkin | 6f92833 | 2012-08-17 11:47:32 | [diff] [blame] | 243 | #endif |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 244 | |
drh | cc78fea | 2006-01-06 16:17:05 | [diff] [blame] | 245 | /* |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 246 | ** WinCE lacks native support for file locking so we have to fake it |
| 247 | ** with some code of our own. |
| 248 | */ |
danielk1977 | 29bafea | 2008-06-26 10:41:19 | [diff] [blame] | 249 | #if SQLITE_OS_WINCE |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 250 | typedef struct winceLock { |
| 251 | int nReaders; /* Number of reader locks obtained */ |
| 252 | BOOL bPending; /* Indicates a pending lock has been obtained */ |
| 253 | BOOL bReserved; /* Indicates a reserved lock has been obtained */ |
| 254 | BOOL bExclusive; /* Indicates an exclusive lock has been obtained */ |
| 255 | } winceLock; |
| 256 | #endif |
| 257 | |
| 258 | /* |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 259 | ** The winFile structure is a subclass of sqlite3_file* specific to the win32 |
drh | 054889e | 2005-11-30 03:20:31 | [diff] [blame] | 260 | ** portability layer. |
drh | 9cbe635 | 2005-11-29 03:13:21 | [diff] [blame] | 261 | */ |
drh | 054889e | 2005-11-30 03:20:31 | [diff] [blame] | 262 | typedef struct winFile winFile; |
| 263 | struct winFile { |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 264 | const sqlite3_io_methods *pMethod; /*** Must be first ***/ |
| 265 | sqlite3_vfs *pVfs; /* The VFS used to open this file */ |
drh | 9cbe635 | 2005-11-29 03:13:21 | [diff] [blame] | 266 | HANDLE h; /* Handle for accessing the file */ |
drh | f0b190d | 2011-07-26 16:03:07 | [diff] [blame] | 267 | u8 locktype; /* Type of lock currently held on this file */ |
drh | 9cbe635 | 2005-11-29 03:13:21 | [diff] [blame] | 268 | short sharedLockByte; /* Randomly chosen byte used as a shared lock */ |
drh | f12b3f6 | 2011-12-21 14:42:29 | [diff] [blame] | 269 | u8 ctrlFlags; /* Flags. See WINFILE_* below */ |
shane | 9db299f | 2009-01-30 05:59:10 | [diff] [blame] | 270 | DWORD lastErrno; /* The Windows errno from the last I/O error */ |
mistachkin | 6f92833 | 2012-08-17 11:47:32 | [diff] [blame] | 271 | #ifndef SQLITE_OMIT_WAL |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 272 | winShm *pShm; /* Instance of shared memory on this file */ |
mistachkin | 6f92833 | 2012-08-17 11:47:32 | [diff] [blame] | 273 | #endif |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 274 | const char *zPath; /* Full pathname of this file */ |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 275 | int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */ |
danielk1977 | 29bafea | 2008-06-26 10:41:19 | [diff] [blame] | 276 | #if SQLITE_OS_WINCE |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 277 | LPWSTR zDeleteOnClose; /* Name of file to delete when closing */ |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 278 | HANDLE hMutex; /* Mutex used to control access to shared lock */ |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 279 | HANDLE hShared; /* Shared memory segment used for locking */ |
| 280 | winceLock local; /* Locks obtained by this instance of winFile */ |
| 281 | winceLock *shared; /* Global shared lock memory for the file */ |
drh | cc78fea | 2006-01-06 16:17:05 | [diff] [blame] | 282 | #endif |
mistachkin | 5824e05 | 2013-04-15 20:08:27 | [diff] [blame] | 283 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 9b4c59f | 2013-04-15 17:03:42 | [diff] [blame] | 284 | int nFetchOut; /* Number of outstanding xFetch references */ |
| 285 | HANDLE hMap; /* Handle for accessing memory mapping */ |
| 286 | void *pMapRegion; /* Area memory mapped */ |
drh | 48ea97e | 2018-11-24 16:07:21 | [diff] [blame] | 287 | sqlite3_int64 mmapSize; /* Size of mapped region */ |
drh | 9b4c59f | 2013-04-15 17:03:42 | [diff] [blame] | 288 | sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */ |
mistachkin | 5824e05 | 2013-04-15 20:08:27 | [diff] [blame] | 289 | #endif |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 290 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
dan | 4628888 | 2025-01-30 15:26:16 | [diff] [blame] | 291 | DWORD iBusyTimeout; /* Wait this many millisec on locks */ |
dan | 2d87894 | 2025-02-10 20:46:14 | [diff] [blame] | 292 | int bBlockOnConnect; |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 293 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 | [diff] [blame] | 294 | }; |
| 295 | |
dan | d50eb9c | 2024-12-10 19:00:07 | [diff] [blame] | 296 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 297 | # define winFileBusyTimeout(pDbFd) pDbFd->iBusyTimeout |
| 298 | #else |
| 299 | # define winFileBusyTimeout(pDbFd) 0 |
| 300 | #endif |
| 301 | |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 302 | /* |
mistachkin | 1e75483 | 2016-07-08 21:14:37 | [diff] [blame] | 303 | ** The winVfsAppData structure is used for the pAppData member for all of the |
| 304 | ** Win32 VFS variants. |
| 305 | */ |
| 306 | typedef struct winVfsAppData winVfsAppData; |
| 307 | struct winVfsAppData { |
| 308 | const sqlite3_io_methods *pMethod; /* The file I/O methods to use. */ |
| 309 | void *pAppData; /* The extra pAppData, if any. */ |
| 310 | BOOL bNoLock; /* Non-zero if locking is disabled. */ |
| 311 | }; |
| 312 | |
| 313 | /* |
drh | f12b3f6 | 2011-12-21 14:42:29 | [diff] [blame] | 314 | ** Allowed values for winFile.ctrlFlags |
| 315 | */ |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 316 | #define WINFILE_RDONLY 0x02 /* Connection is read only */ |
drh | f12b3f6 | 2011-12-21 14:42:29 | [diff] [blame] | 317 | #define WINFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */ |
drh | cb15f35 | 2011-12-23 01:04:17 | [diff] [blame] | 318 | #define WINFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */ |
drh | f12b3f6 | 2011-12-21 14:42:29 | [diff] [blame] | 319 | |
| 320 | /* |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 321 | * The size of the buffer used by sqlite3_win32_write_debug(). |
| 322 | */ |
| 323 | #ifndef SQLITE_WIN32_DBG_BUF_SIZE |
mistachkin | 16afb9e | 2012-03-14 23:08:59 | [diff] [blame] | 324 | # define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD))) |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 325 | #endif |
| 326 | |
| 327 | /* |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 328 | * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the |
| 329 | * various Win32 API heap functions instead of our own. |
| 330 | */ |
| 331 | #ifdef SQLITE_WIN32_MALLOC |
mistachkin | 40c471d | 2012-03-15 03:40:59 | [diff] [blame] | 332 | |
| 333 | /* |
| 334 | * If this is non-zero, an isolated heap will be created by the native Win32 |
| 335 | * allocator subsystem; otherwise, the default process heap will be used. This |
| 336 | * setting has no effect when compiling for WinRT. By default, this is enabled |
| 337 | * and an isolated heap will be created to store all allocated data. |
| 338 | * |
| 339 | ****************************************************************************** |
| 340 | * WARNING: It is important to note that when this setting is non-zero and the |
| 341 | * winMemShutdown function is called (e.g. by the sqlite3_shutdown |
| 342 | * function), all data that was allocated using the isolated heap will |
| 343 | * be freed immediately and any attempt to access any of that freed |
| 344 | * data will almost certainly result in an immediate access violation. |
| 345 | ****************************************************************************** |
| 346 | */ |
| 347 | #ifndef SQLITE_WIN32_HEAP_CREATE |
mistachkin | 5e6710a | 2017-02-20 19:13:37 | [diff] [blame] | 348 | # define SQLITE_WIN32_HEAP_CREATE (TRUE) |
| 349 | #endif |
| 350 | |
| 351 | /* |
| 352 | * This is the maximum possible initial size of the Win32-specific heap, in |
| 353 | * bytes. |
| 354 | */ |
| 355 | #ifndef SQLITE_WIN32_HEAP_MAX_INIT_SIZE |
| 356 | # define SQLITE_WIN32_HEAP_MAX_INIT_SIZE (4294967295U) |
| 357 | #endif |
| 358 | |
| 359 | /* |
| 360 | * This is the extra space for the initial size of the Win32-specific heap, |
| 361 | * in bytes. This value may be zero. |
| 362 | */ |
| 363 | #ifndef SQLITE_WIN32_HEAP_INIT_EXTRA |
| 364 | # define SQLITE_WIN32_HEAP_INIT_EXTRA (4194304) |
| 365 | #endif |
| 366 | |
| 367 | /* |
| 368 | * Calculate the maximum legal cache size, in pages, based on the maximum |
| 369 | * possible initial heap size and the default page size, setting aside the |
| 370 | * needed extra space. |
| 371 | */ |
| 372 | #ifndef SQLITE_WIN32_MAX_CACHE_SIZE |
| 373 | # define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \ |
| 374 | (SQLITE_WIN32_HEAP_INIT_EXTRA)) / \ |
| 375 | (SQLITE_DEFAULT_PAGE_SIZE)) |
mistachkin | 40c471d | 2012-03-15 03:40:59 | [diff] [blame] | 376 | #endif |
| 377 | |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 378 | /* |
mistachkin | eb2329b | 2016-03-24 20:36:47 | [diff] [blame] | 379 | * This is cache size used in the calculation of the initial size of the |
| 380 | * Win32-specific heap. It cannot be negative. |
| 381 | */ |
| 382 | #ifndef SQLITE_WIN32_CACHE_SIZE |
| 383 | # if SQLITE_DEFAULT_CACHE_SIZE>=0 |
mistachkin | 5e6710a | 2017-02-20 19:13:37 | [diff] [blame] | 384 | # define SQLITE_WIN32_CACHE_SIZE (SQLITE_DEFAULT_CACHE_SIZE) |
mistachkin | eb2329b | 2016-03-24 20:36:47 | [diff] [blame] | 385 | # else |
mistachkin | 5e6710a | 2017-02-20 19:13:37 | [diff] [blame] | 386 | # define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE)) |
mistachkin | eb2329b | 2016-03-24 20:36:47 | [diff] [blame] | 387 | # endif |
| 388 | #endif |
| 389 | |
| 390 | /* |
mistachkin | 5e6710a | 2017-02-20 19:13:37 | [diff] [blame] | 391 | * Make sure that the calculated cache size, in pages, cannot cause the |
| 392 | * initial size of the Win32-specific heap to exceed the maximum amount |
| 393 | * of memory that can be specified in the call to HeapCreate. |
| 394 | */ |
mistachkin | 4689956 | 2017-02-20 23:32:04 | [diff] [blame] | 395 | #if SQLITE_WIN32_CACHE_SIZE>SQLITE_WIN32_MAX_CACHE_SIZE |
mistachkin | 5e6710a | 2017-02-20 19:13:37 | [diff] [blame] | 396 | # undef SQLITE_WIN32_CACHE_SIZE |
| 397 | # define SQLITE_WIN32_CACHE_SIZE (2000) |
| 398 | #endif |
| 399 | |
| 400 | /* |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 401 | * The initial size of the Win32-specific heap. This value may be zero. |
| 402 | */ |
| 403 | #ifndef SQLITE_WIN32_HEAP_INIT_SIZE |
mistachkin | 5e6710a | 2017-02-20 19:13:37 | [diff] [blame] | 404 | # define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_WIN32_CACHE_SIZE) * \ |
| 405 | (SQLITE_DEFAULT_PAGE_SIZE) + \ |
| 406 | (SQLITE_WIN32_HEAP_INIT_EXTRA)) |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 407 | #endif |
| 408 | |
| 409 | /* |
| 410 | * The maximum size of the Win32-specific heap. This value may be zero. |
| 411 | */ |
| 412 | #ifndef SQLITE_WIN32_HEAP_MAX_SIZE |
mistachkin | 5e6710a | 2017-02-20 19:13:37 | [diff] [blame] | 413 | # define SQLITE_WIN32_HEAP_MAX_SIZE (0) |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 414 | #endif |
| 415 | |
| 416 | /* |
mistachkin | 155892c | 2011-08-26 01:32:24 | [diff] [blame] | 417 | * The extra flags to use in calls to the Win32 heap APIs. This value may be |
| 418 | * zero for the default behavior. |
| 419 | */ |
| 420 | #ifndef SQLITE_WIN32_HEAP_FLAGS |
mistachkin | 5e6710a | 2017-02-20 19:13:37 | [diff] [blame] | 421 | # define SQLITE_WIN32_HEAP_FLAGS (0) |
mistachkin | 155892c | 2011-08-26 01:32:24 | [diff] [blame] | 422 | #endif |
| 423 | |
drh | 68f7a9e | 2013-07-31 19:55:25 | [diff] [blame] | 424 | |
mistachkin | 155892c | 2011-08-26 01:32:24 | [diff] [blame] | 425 | /* |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 426 | ** The winMemData structure stores information required by the Win32-specific |
| 427 | ** sqlite3_mem_methods implementation. |
| 428 | */ |
| 429 | typedef struct winMemData winMemData; |
| 430 | struct winMemData { |
mistachkin | 7da32b5 | 2011-08-26 01:45:50 | [diff] [blame] | 431 | #ifndef NDEBUG |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 432 | u32 magic1; /* Magic number to detect structure corruption. */ |
mistachkin | 7da32b5 | 2011-08-26 01:45:50 | [diff] [blame] | 433 | #endif |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 434 | HANDLE hHeap; /* The handle to our heap. */ |
| 435 | BOOL bOwned; /* Do we own the heap (i.e. destroy it on shutdown)? */ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 436 | #ifndef NDEBUG |
| 437 | u32 magic2; /* Magic number to detect structure corruption. */ |
| 438 | #endif |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 439 | }; |
| 440 | |
mistachkin | 7da32b5 | 2011-08-26 01:45:50 | [diff] [blame] | 441 | #ifndef NDEBUG |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 442 | #define WINMEM_MAGIC1 0x42b2830b |
| 443 | #define WINMEM_MAGIC2 0xbd4d7cf4 |
mistachkin | 7da32b5 | 2011-08-26 01:45:50 | [diff] [blame] | 444 | #endif |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 445 | |
mistachkin | 7da32b5 | 2011-08-26 01:45:50 | [diff] [blame] | 446 | static struct winMemData win_mem_data = { |
| 447 | #ifndef NDEBUG |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 448 | WINMEM_MAGIC1, |
mistachkin | 7da32b5 | 2011-08-26 01:45:50 | [diff] [blame] | 449 | #endif |
| 450 | NULL, FALSE |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 451 | #ifndef NDEBUG |
| 452 | ,WINMEM_MAGIC2 |
| 453 | #endif |
mistachkin | 7da32b5 | 2011-08-26 01:45:50 | [diff] [blame] | 454 | }; |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 455 | |
mistachkin | 7da32b5 | 2011-08-26 01:45:50 | [diff] [blame] | 456 | #ifndef NDEBUG |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 457 | #define winMemAssertMagic1() assert( win_mem_data.magic1==WINMEM_MAGIC1 ) |
| 458 | #define winMemAssertMagic2() assert( win_mem_data.magic2==WINMEM_MAGIC2 ) |
| 459 | #define winMemAssertMagic() winMemAssertMagic1(); winMemAssertMagic2(); |
mistachkin | 7da32b5 | 2011-08-26 01:45:50 | [diff] [blame] | 460 | #else |
| 461 | #define winMemAssertMagic() |
| 462 | #endif |
| 463 | |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 464 | #define winMemGetDataPtr() &win_mem_data |
| 465 | #define winMemGetHeap() win_mem_data.hHeap |
| 466 | #define winMemGetOwned() win_mem_data.bOwned |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 467 | |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 468 | static void *winMemMalloc(int nBytes); |
| 469 | static void winMemFree(void *pPrior); |
| 470 | static void *winMemRealloc(void *pPrior, int nBytes); |
| 471 | static int winMemSize(void *p); |
| 472 | static int winMemRoundup(int n); |
| 473 | static int winMemInit(void *pAppData); |
| 474 | static void winMemShutdown(void *pAppData); |
| 475 | |
| 476 | const sqlite3_mem_methods *sqlite3MemGetWin32(void); |
| 477 | #endif /* SQLITE_WIN32_MALLOC */ |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 478 | |
shane | 50daafc | 2009-03-05 05:54:55 | [diff] [blame] | 479 | /* |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 480 | ** The following variable is (normally) set once and never changes |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 | [diff] [blame] | 481 | ** thereafter. It records whether the operating system is Win9x |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 482 | ** or WinNT. |
| 483 | ** |
| 484 | ** 0: Operating system unknown. |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 | [diff] [blame] | 485 | ** 1: Operating system is Win9x. |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 486 | ** 2: Operating system is WinNT. |
| 487 | ** |
| 488 | ** In order to facilitate testing on a WinNT system, the test fixture |
| 489 | ** can manually set this value to 1 to emulate Win98 behavior. |
| 490 | */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 491 | #ifdef SQLITE_TEST |
mistachkin | ce64d61 | 2014-08-14 18:31:56 | [diff] [blame] | 492 | LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0; |
mistachkin | 16d511a | 2014-08-02 20:44:13 | [diff] [blame] | 493 | #else |
mistachkin | ce64d61 | 2014-08-14 18:31:56 | [diff] [blame] | 494 | static LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0; |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 495 | #endif |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 496 | |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 497 | #ifndef SYSCALL |
| 498 | # define SYSCALL sqlite3_syscall_ptr |
| 499 | #endif |
| 500 | |
drh | 7acec68 | 2012-03-01 21:19:39 | [diff] [blame] | 501 | /* |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 502 | ** This function is not available on Windows CE or WinRT. |
| 503 | */ |
| 504 | |
| 505 | #if SQLITE_OS_WINCE || SQLITE_OS_WINRT |
| 506 | # define osAreFileApisANSI() 1 |
| 507 | #endif |
| 508 | |
| 509 | /* |
drh | 7acec68 | 2012-03-01 21:19:39 | [diff] [blame] | 510 | ** Many system calls are accessed through pointer-to-functions so that |
| 511 | ** they may be overridden at runtime to facilitate fault injection during |
| 512 | ** testing and sandboxing. The following array holds the names and pointers |
| 513 | ** to all overrideable system calls. |
| 514 | */ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 515 | static struct win_syscall { |
mistachkin | 48864df | 2013-03-21 21:20:32 | [diff] [blame] | 516 | const char *zName; /* Name of the system call */ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 517 | sqlite3_syscall_ptr pCurrent; /* Current value of the system call */ |
| 518 | sqlite3_syscall_ptr pDefault; /* Default value */ |
| 519 | } aSyscall[] = { |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 520 | #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 521 | { "AreFileApisANSI", (SYSCALL)AreFileApisANSI, 0 }, |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 522 | #else |
| 523 | { "AreFileApisANSI", (SYSCALL)0, 0 }, |
| 524 | #endif |
| 525 | |
mistachkin | 0df898e | 2012-03-14 20:17:34 | [diff] [blame] | 526 | #ifndef osAreFileApisANSI |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 527 | #define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent) |
mistachkin | 0df898e | 2012-03-14 20:17:34 | [diff] [blame] | 528 | #endif |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 529 | |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 530 | #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE) |
| 531 | { "CharLowerW", (SYSCALL)CharLowerW, 0 }, |
| 532 | #else |
| 533 | { "CharLowerW", (SYSCALL)0, 0 }, |
| 534 | #endif |
| 535 | |
| 536 | #define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent) |
| 537 | |
| 538 | #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE) |
| 539 | { "CharUpperW", (SYSCALL)CharUpperW, 0 }, |
| 540 | #else |
| 541 | { "CharUpperW", (SYSCALL)0, 0 }, |
| 542 | #endif |
| 543 | |
| 544 | #define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent) |
| 545 | |
| 546 | { "CloseHandle", (SYSCALL)CloseHandle, 0 }, |
| 547 | |
| 548 | #define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent) |
| 549 | |
| 550 | #if defined(SQLITE_WIN32_HAS_ANSI) |
| 551 | { "CreateFileA", (SYSCALL)CreateFileA, 0 }, |
| 552 | #else |
| 553 | { "CreateFileA", (SYSCALL)0, 0 }, |
| 554 | #endif |
| 555 | |
| 556 | #define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \ |
| 557 | LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent) |
| 558 | |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 559 | #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 560 | { "CreateFileW", (SYSCALL)CreateFileW, 0 }, |
| 561 | #else |
| 562 | { "CreateFileW", (SYSCALL)0, 0 }, |
| 563 | #endif |
| 564 | |
| 565 | #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \ |
| 566 | LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent) |
| 567 | |
mistachkin | d5be6f0 | 2016-01-27 07:28:33 | [diff] [blame] | 568 | #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \ |
| 569 | (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) && \ |
| 570 | SQLITE_WIN32_CREATEFILEMAPPINGA |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 571 | { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 }, |
| 572 | #else |
| 573 | { "CreateFileMappingA", (SYSCALL)0, 0 }, |
| 574 | #endif |
| 575 | |
| 576 | #define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \ |
| 577 | DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent) |
| 578 | |
mistachkin | 1700b1c | 2012-08-14 01:45:12 | [diff] [blame] | 579 | #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \ |
mistachkin | 7376782 | 2014-11-04 19:37:22 | [diff] [blame] | 580 | (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 581 | { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 }, |
| 582 | #else |
| 583 | { "CreateFileMappingW", (SYSCALL)0, 0 }, |
| 584 | #endif |
| 585 | |
| 586 | #define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \ |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 587 | DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 588 | |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 589 | #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 590 | { "CreateMutexW", (SYSCALL)CreateMutexW, 0 }, |
| 591 | #else |
| 592 | { "CreateMutexW", (SYSCALL)0, 0 }, |
| 593 | #endif |
| 594 | |
| 595 | #define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \ |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 596 | LPCWSTR))aSyscall[8].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 597 | |
| 598 | #if defined(SQLITE_WIN32_HAS_ANSI) |
| 599 | { "DeleteFileA", (SYSCALL)DeleteFileA, 0 }, |
| 600 | #else |
| 601 | { "DeleteFileA", (SYSCALL)0, 0 }, |
| 602 | #endif |
| 603 | |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 604 | #define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 605 | |
| 606 | #if defined(SQLITE_WIN32_HAS_WIDE) |
| 607 | { "DeleteFileW", (SYSCALL)DeleteFileW, 0 }, |
| 608 | #else |
| 609 | { "DeleteFileW", (SYSCALL)0, 0 }, |
| 610 | #endif |
| 611 | |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 612 | #define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 613 | |
| 614 | #if SQLITE_OS_WINCE |
| 615 | { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 }, |
| 616 | #else |
| 617 | { "FileTimeToLocalFileTime", (SYSCALL)0, 0 }, |
| 618 | #endif |
| 619 | |
stephan | 7b9407a | 2025-03-07 06:54:04 | [diff] [blame] | 620 | #define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(const FILETIME*, \ |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 621 | LPFILETIME))aSyscall[11].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 622 | |
| 623 | #if SQLITE_OS_WINCE |
| 624 | { "FileTimeToSystemTime", (SYSCALL)FileTimeToSystemTime, 0 }, |
| 625 | #else |
| 626 | { "FileTimeToSystemTime", (SYSCALL)0, 0 }, |
| 627 | #endif |
| 628 | |
stephan | 7b9407a | 2025-03-07 06:54:04 | [diff] [blame] | 629 | #define osFileTimeToSystemTime ((BOOL(WINAPI*)(const FILETIME*, \ |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 630 | LPSYSTEMTIME))aSyscall[12].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 631 | |
| 632 | { "FlushFileBuffers", (SYSCALL)FlushFileBuffers, 0 }, |
| 633 | |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 634 | #define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 635 | |
| 636 | #if defined(SQLITE_WIN32_HAS_ANSI) |
| 637 | { "FormatMessageA", (SYSCALL)FormatMessageA, 0 }, |
| 638 | #else |
| 639 | { "FormatMessageA", (SYSCALL)0, 0 }, |
| 640 | #endif |
| 641 | |
| 642 | #define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \ |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 643 | DWORD,va_list*))aSyscall[14].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 644 | |
| 645 | #if defined(SQLITE_WIN32_HAS_WIDE) |
| 646 | { "FormatMessageW", (SYSCALL)FormatMessageW, 0 }, |
| 647 | #else |
| 648 | { "FormatMessageW", (SYSCALL)0, 0 }, |
| 649 | #endif |
| 650 | |
| 651 | #define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \ |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 652 | DWORD,va_list*))aSyscall[15].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 653 | |
mistachkin | 08c1c31 | 2012-10-06 03:48:25 | [diff] [blame] | 654 | #if !defined(SQLITE_OMIT_LOAD_EXTENSION) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 655 | { "FreeLibrary", (SYSCALL)FreeLibrary, 0 }, |
mistachkin | 08c1c31 | 2012-10-06 03:48:25 | [diff] [blame] | 656 | #else |
| 657 | { "FreeLibrary", (SYSCALL)0, 0 }, |
| 658 | #endif |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 659 | |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 660 | #define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 661 | |
| 662 | { "GetCurrentProcessId", (SYSCALL)GetCurrentProcessId, 0 }, |
| 663 | |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 664 | #define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 665 | |
| 666 | #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI) |
| 667 | { "GetDiskFreeSpaceA", (SYSCALL)GetDiskFreeSpaceA, 0 }, |
| 668 | #else |
| 669 | { "GetDiskFreeSpaceA", (SYSCALL)0, 0 }, |
| 670 | #endif |
| 671 | |
| 672 | #define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \ |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 673 | LPDWORD))aSyscall[18].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 674 | |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 675 | #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 676 | { "GetDiskFreeSpaceW", (SYSCALL)GetDiskFreeSpaceW, 0 }, |
| 677 | #else |
| 678 | { "GetDiskFreeSpaceW", (SYSCALL)0, 0 }, |
| 679 | #endif |
| 680 | |
| 681 | #define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \ |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 682 | LPDWORD))aSyscall[19].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 683 | |
| 684 | #if defined(SQLITE_WIN32_HAS_ANSI) |
| 685 | { "GetFileAttributesA", (SYSCALL)GetFileAttributesA, 0 }, |
| 686 | #else |
| 687 | { "GetFileAttributesA", (SYSCALL)0, 0 }, |
| 688 | #endif |
| 689 | |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 690 | #define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 691 | |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 692 | #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 693 | { "GetFileAttributesW", (SYSCALL)GetFileAttributesW, 0 }, |
| 694 | #else |
| 695 | { "GetFileAttributesW", (SYSCALL)0, 0 }, |
| 696 | #endif |
| 697 | |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 698 | #define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 699 | |
| 700 | #if defined(SQLITE_WIN32_HAS_WIDE) |
| 701 | { "GetFileAttributesExW", (SYSCALL)GetFileAttributesExW, 0 }, |
| 702 | #else |
| 703 | { "GetFileAttributesExW", (SYSCALL)0, 0 }, |
| 704 | #endif |
| 705 | |
| 706 | #define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \ |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 707 | LPVOID))aSyscall[22].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 708 | |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 709 | #if !SQLITE_OS_WINRT |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 710 | { "GetFileSize", (SYSCALL)GetFileSize, 0 }, |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 711 | #else |
| 712 | { "GetFileSize", (SYSCALL)0, 0 }, |
drh | 24560d1 | 2012-03-01 22:44:56 | [diff] [blame] | 713 | #endif |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 714 | |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 715 | #define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 716 | |
| 717 | #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI) |
| 718 | { "GetFullPathNameA", (SYSCALL)GetFullPathNameA, 0 }, |
| 719 | #else |
| 720 | { "GetFullPathNameA", (SYSCALL)0, 0 }, |
| 721 | #endif |
| 722 | |
| 723 | #define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \ |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 724 | LPSTR*))aSyscall[24].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 725 | |
mistachkin | c548465 | 2012-03-05 22:52:33 | [diff] [blame] | 726 | #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 727 | { "GetFullPathNameW", (SYSCALL)GetFullPathNameW, 0 }, |
| 728 | #else |
| 729 | { "GetFullPathNameW", (SYSCALL)0, 0 }, |
| 730 | #endif |
| 731 | |
| 732 | #define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \ |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 733 | LPWSTR*))aSyscall[25].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 734 | |
dan | b1ad83a | 2024-12-11 15:30:58 | [diff] [blame] | 735 | /* |
| 736 | ** For GetLastError(), MSDN says: |
| 737 | ** |
| 738 | ** Minimum supported client: Windows XP [desktop apps | UWP apps] |
| 739 | ** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps] |
| 740 | */ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 741 | { "GetLastError", (SYSCALL)GetLastError, 0 }, |
| 742 | |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 743 | #define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 744 | |
mistachkin | 08c1c31 | 2012-10-06 03:48:25 | [diff] [blame] | 745 | #if !defined(SQLITE_OMIT_LOAD_EXTENSION) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 746 | #if SQLITE_OS_WINCE |
| 747 | /* The GetProcAddressA() routine is only available on Windows CE. */ |
| 748 | { "GetProcAddressA", (SYSCALL)GetProcAddressA, 0 }, |
| 749 | #else |
| 750 | /* All other Windows platforms expect GetProcAddress() to take |
| 751 | ** an ANSI string regardless of the _UNICODE setting */ |
| 752 | { "GetProcAddressA", (SYSCALL)GetProcAddress, 0 }, |
| 753 | #endif |
mistachkin | 08c1c31 | 2012-10-06 03:48:25 | [diff] [blame] | 754 | #else |
| 755 | { "GetProcAddressA", (SYSCALL)0, 0 }, |
| 756 | #endif |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 757 | |
| 758 | #define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \ |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 759 | LPCSTR))aSyscall[27].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 760 | |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 761 | #if !SQLITE_OS_WINRT |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 762 | { "GetSystemInfo", (SYSCALL)GetSystemInfo, 0 }, |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 763 | #else |
| 764 | { "GetSystemInfo", (SYSCALL)0, 0 }, |
| 765 | #endif |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 766 | |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 767 | #define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 768 | |
| 769 | { "GetSystemTime", (SYSCALL)GetSystemTime, 0 }, |
| 770 | |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 771 | #define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 772 | |
| 773 | #if !SQLITE_OS_WINCE |
| 774 | { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 }, |
| 775 | #else |
| 776 | { "GetSystemTimeAsFileTime", (SYSCALL)0, 0 }, |
| 777 | #endif |
| 778 | |
| 779 | #define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \ |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 780 | LPFILETIME))aSyscall[30].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 781 | |
| 782 | #if defined(SQLITE_WIN32_HAS_ANSI) |
| 783 | { "GetTempPathA", (SYSCALL)GetTempPathA, 0 }, |
| 784 | #else |
| 785 | { "GetTempPathA", (SYSCALL)0, 0 }, |
| 786 | #endif |
| 787 | |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 788 | #define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 789 | |
mistachkin | c548465 | 2012-03-05 22:52:33 | [diff] [blame] | 790 | #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 791 | { "GetTempPathW", (SYSCALL)GetTempPathW, 0 }, |
| 792 | #else |
| 793 | { "GetTempPathW", (SYSCALL)0, 0 }, |
| 794 | #endif |
| 795 | |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 796 | #define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 797 | |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 798 | #if !SQLITE_OS_WINRT |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 799 | { "GetTickCount", (SYSCALL)GetTickCount, 0 }, |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 800 | #else |
| 801 | { "GetTickCount", (SYSCALL)0, 0 }, |
| 802 | #endif |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 803 | |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 804 | #define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 805 | |
mistachkin | d5be6f0 | 2016-01-27 07:28:33 | [diff] [blame] | 806 | #if defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_GETVERSIONEX |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 807 | { "GetVersionExA", (SYSCALL)GetVersionExA, 0 }, |
| 808 | #else |
| 809 | { "GetVersionExA", (SYSCALL)0, 0 }, |
| 810 | #endif |
| 811 | |
| 812 | #define osGetVersionExA ((BOOL(WINAPI*)( \ |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 813 | LPOSVERSIONINFOA))aSyscall[34].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 814 | |
mistachkin | 0f7e08e | 2013-11-27 03:01:34 | [diff] [blame] | 815 | #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \ |
mistachkin | d5be6f0 | 2016-01-27 07:28:33 | [diff] [blame] | 816 | SQLITE_WIN32_GETVERSIONEX |
mistachkin | b8af6a2 | 2013-09-13 23:27:39 | [diff] [blame] | 817 | { "GetVersionExW", (SYSCALL)GetVersionExW, 0 }, |
| 818 | #else |
| 819 | { "GetVersionExW", (SYSCALL)0, 0 }, |
| 820 | #endif |
| 821 | |
| 822 | #define osGetVersionExW ((BOOL(WINAPI*)( \ |
| 823 | LPOSVERSIONINFOW))aSyscall[35].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 824 | |
| 825 | { "HeapAlloc", (SYSCALL)HeapAlloc, 0 }, |
| 826 | |
| 827 | #define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \ |
mistachkin | b8af6a2 | 2013-09-13 23:27:39 | [diff] [blame] | 828 | SIZE_T))aSyscall[36].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 829 | |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 830 | #if !SQLITE_OS_WINRT |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 831 | { "HeapCreate", (SYSCALL)HeapCreate, 0 }, |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 832 | #else |
| 833 | { "HeapCreate", (SYSCALL)0, 0 }, |
| 834 | #endif |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 835 | |
| 836 | #define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \ |
mistachkin | b8af6a2 | 2013-09-13 23:27:39 | [diff] [blame] | 837 | SIZE_T))aSyscall[37].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 838 | |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 839 | #if !SQLITE_OS_WINRT |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 840 | { "HeapDestroy", (SYSCALL)HeapDestroy, 0 }, |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 841 | #else |
| 842 | { "HeapDestroy", (SYSCALL)0, 0 }, |
| 843 | #endif |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 844 | |
mistachkin | b8af6a2 | 2013-09-13 23:27:39 | [diff] [blame] | 845 | #define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[38].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 846 | |
| 847 | { "HeapFree", (SYSCALL)HeapFree, 0 }, |
| 848 | |
mistachkin | b8af6a2 | 2013-09-13 23:27:39 | [diff] [blame] | 849 | #define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[39].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 850 | |
| 851 | { "HeapReAlloc", (SYSCALL)HeapReAlloc, 0 }, |
| 852 | |
| 853 | #define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \ |
mistachkin | b8af6a2 | 2013-09-13 23:27:39 | [diff] [blame] | 854 | SIZE_T))aSyscall[40].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 855 | |
| 856 | { "HeapSize", (SYSCALL)HeapSize, 0 }, |
| 857 | |
| 858 | #define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \ |
mistachkin | b8af6a2 | 2013-09-13 23:27:39 | [diff] [blame] | 859 | LPCVOID))aSyscall[41].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 860 | |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 861 | #if !SQLITE_OS_WINRT |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 862 | { "HeapValidate", (SYSCALL)HeapValidate, 0 }, |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 863 | #else |
| 864 | { "HeapValidate", (SYSCALL)0, 0 }, |
| 865 | #endif |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 866 | |
| 867 | #define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \ |
mistachkin | b8af6a2 | 2013-09-13 23:27:39 | [diff] [blame] | 868 | LPCVOID))aSyscall[42].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 869 | |
mistachkin | 5134a81 | 2013-11-08 19:51:12 | [diff] [blame] | 870 | #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 871 | { "HeapCompact", (SYSCALL)HeapCompact, 0 }, |
| 872 | #else |
| 873 | { "HeapCompact", (SYSCALL)0, 0 }, |
| 874 | #endif |
| 875 | |
| 876 | #define osHeapCompact ((UINT(WINAPI*)(HANDLE,DWORD))aSyscall[43].pCurrent) |
| 877 | |
mistachkin | 08c1c31 | 2012-10-06 03:48:25 | [diff] [blame] | 878 | #if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 879 | { "LoadLibraryA", (SYSCALL)LoadLibraryA, 0 }, |
| 880 | #else |
| 881 | { "LoadLibraryA", (SYSCALL)0, 0 }, |
| 882 | #endif |
| 883 | |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 884 | #define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[44].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 885 | |
mistachkin | 08c1c31 | 2012-10-06 03:48:25 | [diff] [blame] | 886 | #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \ |
| 887 | !defined(SQLITE_OMIT_LOAD_EXTENSION) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 888 | { "LoadLibraryW", (SYSCALL)LoadLibraryW, 0 }, |
| 889 | #else |
| 890 | { "LoadLibraryW", (SYSCALL)0, 0 }, |
| 891 | #endif |
| 892 | |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 893 | #define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[45].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 894 | |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 895 | #if !SQLITE_OS_WINRT |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 896 | { "LocalFree", (SYSCALL)LocalFree, 0 }, |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 897 | #else |
| 898 | { "LocalFree", (SYSCALL)0, 0 }, |
| 899 | #endif |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 900 | |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 901 | #define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[46].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 902 | |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 903 | #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 904 | { "LockFile", (SYSCALL)LockFile, 0 }, |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 905 | #else |
| 906 | { "LockFile", (SYSCALL)0, 0 }, |
| 907 | #endif |
| 908 | |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 909 | #if !defined(osLockFile) && defined(SQLITE_WIN32_HAS_ANSI) |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 910 | #define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 911 | DWORD))aSyscall[47].pCurrent) |
mistachkin | 0df898e | 2012-03-14 20:17:34 | [diff] [blame] | 912 | #endif |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 913 | |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 914 | #if !SQLITE_OS_WINCE |
| 915 | { "LockFileEx", (SYSCALL)LockFileEx, 0 }, |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 916 | #else |
| 917 | { "LockFileEx", (SYSCALL)0, 0 }, |
| 918 | #endif |
| 919 | |
mistachkin | 0df898e | 2012-03-14 20:17:34 | [diff] [blame] | 920 | #ifndef osLockFileEx |
mistachkin | 5cfbeac | 2012-03-13 01:30:20 | [diff] [blame] | 921 | #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 922 | LPOVERLAPPED))aSyscall[48].pCurrent) |
mistachkin | 0df898e | 2012-03-14 20:17:34 | [diff] [blame] | 923 | #endif |
mistachkin | 5cfbeac | 2012-03-13 01:30:20 | [diff] [blame] | 924 | |
mistachkin | 7376782 | 2014-11-04 19:37:22 | [diff] [blame] | 925 | #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \ |
| 926 | (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 927 | { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 }, |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 928 | #else |
| 929 | { "MapViewOfFile", (SYSCALL)0, 0 }, |
| 930 | #endif |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 931 | |
| 932 | #define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 933 | SIZE_T))aSyscall[49].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 934 | |
| 935 | { "MultiByteToWideChar", (SYSCALL)MultiByteToWideChar, 0 }, |
| 936 | |
| 937 | #define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 938 | int))aSyscall[50].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 939 | |
| 940 | { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 }, |
| 941 | |
| 942 | #define osQueryPerformanceCounter ((BOOL(WINAPI*)( \ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 943 | LARGE_INTEGER*))aSyscall[51].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 944 | |
| 945 | { "ReadFile", (SYSCALL)ReadFile, 0 }, |
| 946 | |
| 947 | #define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 948 | LPOVERLAPPED))aSyscall[52].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 949 | |
| 950 | { "SetEndOfFile", (SYSCALL)SetEndOfFile, 0 }, |
| 951 | |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 952 | #define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[53].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 953 | |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 954 | #if !SQLITE_OS_WINRT |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 955 | { "SetFilePointer", (SYSCALL)SetFilePointer, 0 }, |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 956 | #else |
| 957 | { "SetFilePointer", (SYSCALL)0, 0 }, |
drh | 8045df0 | 2012-03-01 22:06:30 | [diff] [blame] | 958 | #endif |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 959 | |
| 960 | #define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 961 | DWORD))aSyscall[54].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 962 | |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 963 | #if !SQLITE_OS_WINRT |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 964 | { "Sleep", (SYSCALL)Sleep, 0 }, |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 965 | #else |
| 966 | { "Sleep", (SYSCALL)0, 0 }, |
drh | 7acec68 | 2012-03-01 21:19:39 | [diff] [blame] | 967 | #endif |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 968 | |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 969 | #define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[55].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 970 | |
| 971 | { "SystemTimeToFileTime", (SYSCALL)SystemTimeToFileTime, 0 }, |
| 972 | |
stephan | 7b9407a | 2025-03-07 06:54:04 | [diff] [blame] | 973 | #define osSystemTimeToFileTime ((BOOL(WINAPI*)(const SYSTEMTIME*, \ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 974 | LPFILETIME))aSyscall[56].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 975 | |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 976 | #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 977 | { "UnlockFile", (SYSCALL)UnlockFile, 0 }, |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 978 | #else |
| 979 | { "UnlockFile", (SYSCALL)0, 0 }, |
| 980 | #endif |
| 981 | |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 982 | #if !defined(osUnlockFile) && defined(SQLITE_WIN32_HAS_ANSI) |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 983 | #define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 984 | DWORD))aSyscall[57].pCurrent) |
mistachkin | 0df898e | 2012-03-14 20:17:34 | [diff] [blame] | 985 | #endif |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 986 | |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 987 | #if !SQLITE_OS_WINCE |
| 988 | { "UnlockFileEx", (SYSCALL)UnlockFileEx, 0 }, |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 989 | #else |
| 990 | { "UnlockFileEx", (SYSCALL)0, 0 }, |
| 991 | #endif |
| 992 | |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 993 | #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 994 | LPOVERLAPPED))aSyscall[58].pCurrent) |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 995 | |
mistachkin | 7376782 | 2014-11-04 19:37:22 | [diff] [blame] | 996 | #if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 997 | { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 }, |
mistachkin | 1700b1c | 2012-08-14 01:45:12 | [diff] [blame] | 998 | #else |
| 999 | { "UnmapViewOfFile", (SYSCALL)0, 0 }, |
| 1000 | #endif |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1001 | |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1002 | #define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[59].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1003 | |
| 1004 | { "WideCharToMultiByte", (SYSCALL)WideCharToMultiByte, 0 }, |
| 1005 | |
| 1006 | #define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1007 | LPCSTR,LPBOOL))aSyscall[60].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1008 | |
| 1009 | { "WriteFile", (SYSCALL)WriteFile, 0 }, |
| 1010 | |
| 1011 | #define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1012 | LPOVERLAPPED))aSyscall[61].pCurrent) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1013 | |
mistachkin | 401e915 | 2012-06-04 05:18:32 | [diff] [blame] | 1014 | #if SQLITE_OS_WINRT |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 1015 | { "CreateEventExW", (SYSCALL)CreateEventExW, 0 }, |
mistachkin | 75b70a2 | 2012-03-02 13:47:16 | [diff] [blame] | 1016 | #else |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 1017 | { "CreateEventExW", (SYSCALL)0, 0 }, |
mistachkin | 75b70a2 | 2012-03-02 13:47:16 | [diff] [blame] | 1018 | #endif |
| 1019 | |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 1020 | #define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1021 | DWORD,DWORD))aSyscall[62].pCurrent) |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 1022 | |
dan | b1ad83a | 2024-12-11 15:30:58 | [diff] [blame] | 1023 | /* |
| 1024 | ** For WaitForSingleObject(), MSDN says: |
| 1025 | ** |
| 1026 | ** Minimum supported client: Windows XP [desktop apps | UWP apps] |
| 1027 | ** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps] |
| 1028 | */ |
mistachkin | 75b70a2 | 2012-03-02 13:47:16 | [diff] [blame] | 1029 | { "WaitForSingleObject", (SYSCALL)WaitForSingleObject, 0 }, |
mistachkin | 75b70a2 | 2012-03-02 13:47:16 | [diff] [blame] | 1030 | |
| 1031 | #define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1032 | DWORD))aSyscall[63].pCurrent) |
mistachkin | 75b70a2 | 2012-03-02 13:47:16 | [diff] [blame] | 1033 | |
mistachkin | 92c2e0d | 2014-10-16 18:34:50 | [diff] [blame] | 1034 | #if !SQLITE_OS_WINCE |
mistachkin | 75b70a2 | 2012-03-02 13:47:16 | [diff] [blame] | 1035 | { "WaitForSingleObjectEx", (SYSCALL)WaitForSingleObjectEx, 0 }, |
mistachkin | 92c2e0d | 2014-10-16 18:34:50 | [diff] [blame] | 1036 | #else |
| 1037 | { "WaitForSingleObjectEx", (SYSCALL)0, 0 }, |
| 1038 | #endif |
mistachkin | 75b70a2 | 2012-03-02 13:47:16 | [diff] [blame] | 1039 | |
mistachkin | 5cfbeac | 2012-03-13 01:30:20 | [diff] [blame] | 1040 | #define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1041 | BOOL))aSyscall[64].pCurrent) |
mistachkin | 5cfbeac | 2012-03-13 01:30:20 | [diff] [blame] | 1042 | |
mistachkin | a0aa13b | 2012-08-13 22:05:22 | [diff] [blame] | 1043 | #if SQLITE_OS_WINRT |
mistachkin | 75b70a2 | 2012-03-02 13:47:16 | [diff] [blame] | 1044 | { "SetFilePointerEx", (SYSCALL)SetFilePointerEx, 0 }, |
mistachkin | 75b70a2 | 2012-03-02 13:47:16 | [diff] [blame] | 1045 | #else |
| 1046 | { "SetFilePointerEx", (SYSCALL)0, 0 }, |
| 1047 | #endif |
| 1048 | |
mistachkin | 5cfbeac | 2012-03-13 01:30:20 | [diff] [blame] | 1049 | #define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1050 | PLARGE_INTEGER,DWORD))aSyscall[65].pCurrent) |
mistachkin | 5cfbeac | 2012-03-13 01:30:20 | [diff] [blame] | 1051 | |
mistachkin | 401e915 | 2012-06-04 05:18:32 | [diff] [blame] | 1052 | #if SQLITE_OS_WINRT |
mistachkin | 75b70a2 | 2012-03-02 13:47:16 | [diff] [blame] | 1053 | { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 }, |
mistachkin | 75b70a2 | 2012-03-02 13:47:16 | [diff] [blame] | 1054 | #else |
| 1055 | { "GetFileInformationByHandleEx", (SYSCALL)0, 0 }, |
| 1056 | #endif |
| 1057 | |
mistachkin | 5cfbeac | 2012-03-13 01:30:20 | [diff] [blame] | 1058 | #define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1059 | FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent) |
mistachkin | 5cfbeac | 2012-03-13 01:30:20 | [diff] [blame] | 1060 | |
mistachkin | 7376782 | 2014-11-04 19:37:22 | [diff] [blame] | 1061 | #if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) |
mistachkin | 1e6eea9 | 2012-05-31 22:12:26 | [diff] [blame] | 1062 | { "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 }, |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 1063 | #else |
mistachkin | 1e6eea9 | 2012-05-31 22:12:26 | [diff] [blame] | 1064 | { "MapViewOfFileFromApp", (SYSCALL)0, 0 }, |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 1065 | #endif |
mistachkin | 287a48d | 2012-03-03 13:15:25 | [diff] [blame] | 1066 | |
mistachkin | 1e6eea9 | 2012-05-31 22:12:26 | [diff] [blame] | 1067 | #define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1068 | SIZE_T))aSyscall[67].pCurrent) |
mistachkin | 287a48d | 2012-03-03 13:15:25 | [diff] [blame] | 1069 | |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 1070 | #if SQLITE_OS_WINRT |
| 1071 | { "CreateFile2", (SYSCALL)CreateFile2, 0 }, |
mistachkin | 5483f77 | 2012-03-07 20:11:47 | [diff] [blame] | 1072 | #else |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 1073 | { "CreateFile2", (SYSCALL)0, 0 }, |
mistachkin | 5483f77 | 2012-03-07 20:11:47 | [diff] [blame] | 1074 | #endif |
| 1075 | |
| 1076 | #define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1077 | LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[68].pCurrent) |
mistachkin | 5483f77 | 2012-03-07 20:11:47 | [diff] [blame] | 1078 | |
mistachkin | 08c1c31 | 2012-10-06 03:48:25 | [diff] [blame] | 1079 | #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION) |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 1080 | { "LoadPackagedLibrary", (SYSCALL)LoadPackagedLibrary, 0 }, |
mistachkin | 5483f77 | 2012-03-07 20:11:47 | [diff] [blame] | 1081 | #else |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 1082 | { "LoadPackagedLibrary", (SYSCALL)0, 0 }, |
mistachkin | 5483f77 | 2012-03-07 20:11:47 | [diff] [blame] | 1083 | #endif |
| 1084 | |
| 1085 | #define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1086 | DWORD))aSyscall[69].pCurrent) |
mistachkin | 5483f77 | 2012-03-07 20:11:47 | [diff] [blame] | 1087 | |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 1088 | #if SQLITE_OS_WINRT |
| 1089 | { "GetTickCount64", (SYSCALL)GetTickCount64, 0 }, |
| 1090 | #else |
| 1091 | { "GetTickCount64", (SYSCALL)0, 0 }, |
| 1092 | #endif |
| 1093 | |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1094 | #define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[70].pCurrent) |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 1095 | |
| 1096 | #if SQLITE_OS_WINRT |
| 1097 | { "GetNativeSystemInfo", (SYSCALL)GetNativeSystemInfo, 0 }, |
| 1098 | #else |
| 1099 | { "GetNativeSystemInfo", (SYSCALL)0, 0 }, |
| 1100 | #endif |
| 1101 | |
| 1102 | #define osGetNativeSystemInfo ((VOID(WINAPI*)( \ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1103 | LPSYSTEM_INFO))aSyscall[71].pCurrent) |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 1104 | |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 1105 | #if defined(SQLITE_WIN32_HAS_ANSI) |
| 1106 | { "OutputDebugStringA", (SYSCALL)OutputDebugStringA, 0 }, |
| 1107 | #else |
| 1108 | { "OutputDebugStringA", (SYSCALL)0, 0 }, |
| 1109 | #endif |
| 1110 | |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1111 | #define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[72].pCurrent) |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 1112 | |
| 1113 | #if defined(SQLITE_WIN32_HAS_WIDE) |
| 1114 | { "OutputDebugStringW", (SYSCALL)OutputDebugStringW, 0 }, |
| 1115 | #else |
| 1116 | { "OutputDebugStringW", (SYSCALL)0, 0 }, |
| 1117 | #endif |
| 1118 | |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1119 | #define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[73].pCurrent) |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 1120 | |
mistachkin | 0df898e | 2012-03-14 20:17:34 | [diff] [blame] | 1121 | { "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 }, |
| 1122 | |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1123 | #define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent) |
mistachkin | 0df898e | 2012-03-14 20:17:34 | [diff] [blame] | 1124 | |
mistachkin | 7376782 | 2014-11-04 19:37:22 | [diff] [blame] | 1125 | #if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) |
mistachkin | 1e6eea9 | 2012-05-31 22:12:26 | [diff] [blame] | 1126 | { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 }, |
| 1127 | #else |
| 1128 | { "CreateFileMappingFromApp", (SYSCALL)0, 0 }, |
| 1129 | #endif |
| 1130 | |
| 1131 | #define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1132 | LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent) |
mistachkin | 1e6eea9 | 2012-05-31 22:12:26 | [diff] [blame] | 1133 | |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 1134 | /* |
| 1135 | ** NOTE: On some sub-platforms, the InterlockedCompareExchange "function" |
| 1136 | ** is really just a macro that uses a compiler intrinsic (e.g. x64). |
drh | 2abe6a2 | 2014-08-06 11:57:54 | [diff] [blame] | 1137 | ** So do not try to make this is into a redefinable interface. |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 1138 | */ |
mistachkin | 91d1249 | 2014-08-11 17:38:38 | [diff] [blame] | 1139 | #if defined(InterlockedCompareExchange) |
| 1140 | { "InterlockedCompareExchange", (SYSCALL)0, 0 }, |
| 1141 | |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 1142 | #define osInterlockedCompareExchange InterlockedCompareExchange |
mistachkin | 91d1249 | 2014-08-11 17:38:38 | [diff] [blame] | 1143 | #else |
mistachkin | 202cb64 | 2014-07-31 18:54:01 | [diff] [blame] | 1144 | { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 }, |
| 1145 | |
mistachkin | ce64d61 | 2014-08-14 18:31:56 | [diff] [blame] | 1146 | #define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG \ |
| 1147 | SQLITE_WIN32_VOLATILE*, LONG,LONG))aSyscall[76].pCurrent) |
mistachkin | 91d1249 | 2014-08-11 17:38:38 | [diff] [blame] | 1148 | #endif /* defined(InterlockedCompareExchange) */ |
mistachkin | 202cb64 | 2014-07-31 18:54:01 | [diff] [blame] | 1149 | |
mistachkin | e45e0fb | 2015-01-21 00:48:46 | [diff] [blame] | 1150 | #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID |
| 1151 | { "UuidCreate", (SYSCALL)UuidCreate, 0 }, |
| 1152 | #else |
| 1153 | { "UuidCreate", (SYSCALL)0, 0 }, |
| 1154 | #endif |
| 1155 | |
| 1156 | #define osUuidCreate ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[77].pCurrent) |
| 1157 | |
| 1158 | #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID |
| 1159 | { "UuidCreateSequential", (SYSCALL)UuidCreateSequential, 0 }, |
| 1160 | #else |
| 1161 | { "UuidCreateSequential", (SYSCALL)0, 0 }, |
| 1162 | #endif |
| 1163 | |
| 1164 | #define osUuidCreateSequential \ |
| 1165 | ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[78].pCurrent) |
| 1166 | |
mistachkin | ccb4371 | 2015-03-26 23:36:35 | [diff] [blame] | 1167 | #if !defined(SQLITE_NO_SYNC) && SQLITE_MAX_MMAP_SIZE>0 |
| 1168 | { "FlushViewOfFile", (SYSCALL)FlushViewOfFile, 0 }, |
| 1169 | #else |
| 1170 | { "FlushViewOfFile", (SYSCALL)0, 0 }, |
| 1171 | #endif |
| 1172 | |
| 1173 | #define osFlushViewOfFile \ |
| 1174 | ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent) |
| 1175 | |
dan | b1ad83a | 2024-12-11 15:30:58 | [diff] [blame] | 1176 | /* |
| 1177 | ** If SQLITE_ENABLE_SETLK_TIMEOUT is defined, we require CreateEvent() |
| 1178 | ** to implement blocking locks with timeouts. MSDN says: |
| 1179 | ** |
| 1180 | ** Minimum supported client: Windows XP [desktop apps | UWP apps] |
| 1181 | ** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps] |
| 1182 | */ |
| 1183 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 1184 | { "CreateEvent", (SYSCALL)CreateEvent, 0 }, |
| 1185 | #else |
| 1186 | { "CreateEvent", (SYSCALL)0, 0 }, |
| 1187 | #endif |
| 1188 | |
| 1189 | #define osCreateEvent ( \ |
| 1190 | (HANDLE(WINAPI*) (LPSECURITY_ATTRIBUTES,BOOL,BOOL,LPCSTR)) \ |
| 1191 | aSyscall[80].pCurrent \ |
| 1192 | ) |
| 1193 | |
dan | ce50282 | 2024-12-24 14:44:38 | [diff] [blame] | 1194 | /* |
| 1195 | ** If SQLITE_ENABLE_SETLK_TIMEOUT is defined, we require CancelIo() |
| 1196 | ** for the case where a timeout expires and a lock request must be |
| 1197 | ** cancelled. |
| 1198 | ** |
| 1199 | ** Minimum supported client: Windows XP [desktop apps | UWP apps] |
| 1200 | ** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps] |
| 1201 | */ |
| 1202 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 1203 | { "CancelIo", (SYSCALL)CancelIo, 0 }, |
| 1204 | #else |
| 1205 | { "CancelIo", (SYSCALL)0, 0 }, |
| 1206 | #endif |
| 1207 | |
| 1208 | #define osCancelIo ((BOOL(WINAPI*)(HANDLE))aSyscall[81].pCurrent) |
| 1209 | |
stephan | 7b9407a | 2025-03-07 06:54:04 | [diff] [blame] | 1210 | #if defined(SQLITE_WIN32_HAS_WIDE) && defined(_WIN32) |
| 1211 | { "GetModuleHandleW", (SYSCALL)GetModuleHandleW, 0 }, |
| 1212 | #else |
| 1213 | { "GetModuleHandleW", (SYSCALL)0, 0 }, |
| 1214 | #endif |
| 1215 | |
| 1216 | #define osGetModuleHandleW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[82].pCurrent) |
| 1217 | |
| 1218 | #ifndef _WIN32 |
| 1219 | { "getenv", (SYSCALL)getenv, 0 }, |
| 1220 | #else |
| 1221 | { "getenv", (SYSCALL)0, 0 }, |
| 1222 | #endif |
| 1223 | |
| 1224 | #define osGetenv ((const char *(*)(const char *))aSyscall[83].pCurrent) |
| 1225 | |
| 1226 | #ifndef _WIN32 |
| 1227 | { "getcwd", (SYSCALL)getcwd, 0 }, |
| 1228 | #else |
| 1229 | { "getcwd", (SYSCALL)0, 0 }, |
| 1230 | #endif |
| 1231 | |
| 1232 | #define osGetcwd ((char*(*)(char*,size_t))aSyscall[84].pCurrent) |
| 1233 | |
| 1234 | #ifndef _WIN32 |
| 1235 | { "readlink", (SYSCALL)readlink, 0 }, |
| 1236 | #else |
| 1237 | { "readlink", (SYSCALL)0, 0 }, |
| 1238 | #endif |
| 1239 | |
| 1240 | #define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[85].pCurrent) |
| 1241 | |
| 1242 | #ifndef _WIN32 |
| 1243 | { "lstat", (SYSCALL)lstat, 0 }, |
| 1244 | #else |
| 1245 | { "lstat", (SYSCALL)0, 0 }, |
| 1246 | #endif |
| 1247 | |
| 1248 | #define osLstat ((int(*)(const char*,struct stat*))aSyscall[86].pCurrent) |
| 1249 | |
| 1250 | #ifndef _WIN32 |
| 1251 | { "__errno", (SYSCALL)__errno, 0 }, |
| 1252 | #else |
| 1253 | { "__errno", (SYSCALL)0, 0 }, |
| 1254 | #endif |
| 1255 | |
| 1256 | #define osErrno (*((int*(*)(void))aSyscall[87].pCurrent)()) |
| 1257 | |
| 1258 | #ifndef _WIN32 |
| 1259 | { "cygwin_conv_path", (SYSCALL)cygwin_conv_path, 0 }, |
| 1260 | #else |
| 1261 | { "cygwin_conv_path", (SYSCALL)0, 0 }, |
| 1262 | #endif |
| 1263 | |
| 1264 | #define osCygwin_conv_path ((size_t(*)(unsigned int, \ |
| 1265 | const void *, void *, size_t))aSyscall[88].pCurrent) |
| 1266 | |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1267 | }; /* End of the overrideable system calls */ |
| 1268 | |
| 1269 | /* |
| 1270 | ** This is the xSetSystemCall() method of sqlite3_vfs for all of the |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1271 | ** "win32" VFSes. Return SQLITE_OK upon successfully updating the |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1272 | ** system call pointer, or SQLITE_NOTFOUND if there is no configurable |
| 1273 | ** system call named zName. |
| 1274 | */ |
| 1275 | static int winSetSystemCall( |
| 1276 | sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */ |
| 1277 | const char *zName, /* Name of system call to override */ |
| 1278 | sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */ |
| 1279 | ){ |
| 1280 | unsigned int i; |
| 1281 | int rc = SQLITE_NOTFOUND; |
| 1282 | |
| 1283 | UNUSED_PARAMETER(pNotUsed); |
| 1284 | if( zName==0 ){ |
| 1285 | /* If no zName is given, restore all system calls to their default |
| 1286 | ** settings and return NULL |
| 1287 | */ |
| 1288 | rc = SQLITE_OK; |
| 1289 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 1290 | if( aSyscall[i].pDefault ){ |
| 1291 | aSyscall[i].pCurrent = aSyscall[i].pDefault; |
| 1292 | } |
| 1293 | } |
| 1294 | }else{ |
| 1295 | /* If zName is specified, operate on only the one system call |
| 1296 | ** specified. |
| 1297 | */ |
| 1298 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 1299 | if( strcmp(zName, aSyscall[i].zName)==0 ){ |
| 1300 | if( aSyscall[i].pDefault==0 ){ |
| 1301 | aSyscall[i].pDefault = aSyscall[i].pCurrent; |
| 1302 | } |
| 1303 | rc = SQLITE_OK; |
| 1304 | if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault; |
| 1305 | aSyscall[i].pCurrent = pNewFunc; |
| 1306 | break; |
| 1307 | } |
| 1308 | } |
| 1309 | } |
| 1310 | return rc; |
| 1311 | } |
| 1312 | |
| 1313 | /* |
| 1314 | ** Return the value of a system call. Return NULL if zName is not a |
| 1315 | ** recognized system call name. NULL is also returned if the system call |
| 1316 | ** is currently undefined. |
| 1317 | */ |
| 1318 | static sqlite3_syscall_ptr winGetSystemCall( |
| 1319 | sqlite3_vfs *pNotUsed, |
| 1320 | const char *zName |
| 1321 | ){ |
| 1322 | unsigned int i; |
| 1323 | |
| 1324 | UNUSED_PARAMETER(pNotUsed); |
| 1325 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 1326 | if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent; |
| 1327 | } |
| 1328 | return 0; |
| 1329 | } |
| 1330 | |
| 1331 | /* |
| 1332 | ** Return the name of the first system call after zName. If zName==NULL |
| 1333 | ** then return the name of the first system call. Return NULL if zName |
| 1334 | ** is the last system call or if zName is not the name of a valid |
| 1335 | ** system call. |
| 1336 | */ |
| 1337 | static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){ |
| 1338 | int i = -1; |
| 1339 | |
| 1340 | UNUSED_PARAMETER(p); |
| 1341 | if( zName ){ |
| 1342 | for(i=0; i<ArraySize(aSyscall)-1; i++){ |
| 1343 | if( strcmp(zName, aSyscall[i].zName)==0 ) break; |
| 1344 | } |
| 1345 | } |
| 1346 | for(i++; i<ArraySize(aSyscall); i++){ |
| 1347 | if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName; |
| 1348 | } |
| 1349 | return 0; |
| 1350 | } |
| 1351 | |
mistachkin | 7f9d179 | 2013-11-08 20:10:57 | [diff] [blame] | 1352 | #ifdef SQLITE_WIN32_MALLOC |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1353 | /* |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1354 | ** If a Win32 native heap has been configured, this function will attempt to |
| 1355 | ** compact it. Upon success, SQLITE_OK will be returned. Upon failure, one |
| 1356 | ** of SQLITE_NOMEM, SQLITE_ERROR, or SQLITE_NOTFOUND will be returned. The |
| 1357 | ** "pnLargest" argument, if non-zero, will be used to return the size of the |
| 1358 | ** largest committed free block in the heap, in bytes. |
| 1359 | */ |
| 1360 | int sqlite3_win32_compact_heap(LPUINT pnLargest){ |
| 1361 | int rc = SQLITE_OK; |
| 1362 | UINT nLargest = 0; |
| 1363 | HANDLE hHeap; |
| 1364 | |
| 1365 | winMemAssertMagic(); |
| 1366 | hHeap = winMemGetHeap(); |
| 1367 | assert( hHeap!=0 ); |
| 1368 | assert( hHeap!=INVALID_HANDLE_VALUE ); |
| 1369 | #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE) |
| 1370 | assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) ); |
| 1371 | #endif |
mistachkin | 5134a81 | 2013-11-08 19:51:12 | [diff] [blame] | 1372 | #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1373 | if( (nLargest=osHeapCompact(hHeap, SQLITE_WIN32_HEAP_FLAGS))==0 ){ |
| 1374 | DWORD lastErrno = osGetLastError(); |
| 1375 | if( lastErrno==NO_ERROR ){ |
| 1376 | sqlite3_log(SQLITE_NOMEM, "failed to HeapCompact (no space), heap=%p", |
| 1377 | (void*)hHeap); |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 1378 | rc = SQLITE_NOMEM_BKPT; |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1379 | }else{ |
mistachkin | 17bc3ff | 2013-11-08 18:37:02 | [diff] [blame] | 1380 | sqlite3_log(SQLITE_ERROR, "failed to HeapCompact (%lu), heap=%p", |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1381 | osGetLastError(), (void*)hHeap); |
| 1382 | rc = SQLITE_ERROR; |
| 1383 | } |
| 1384 | } |
| 1385 | #else |
mistachkin | 17bc3ff | 2013-11-08 18:37:02 | [diff] [blame] | 1386 | sqlite3_log(SQLITE_NOTFOUND, "failed to HeapCompact, heap=%p", |
| 1387 | (void*)hHeap); |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1388 | rc = SQLITE_NOTFOUND; |
| 1389 | #endif |
| 1390 | if( pnLargest ) *pnLargest = nLargest; |
| 1391 | return rc; |
| 1392 | } |
| 1393 | |
| 1394 | /* |
| 1395 | ** If a Win32 native heap has been configured, this function will attempt to |
| 1396 | ** destroy and recreate it. If the Win32 native heap is not isolated and/or |
| 1397 | ** the sqlite3_memory_used() function does not return zero, SQLITE_BUSY will |
| 1398 | ** be returned and no changes will be made to the Win32 native heap. |
| 1399 | */ |
| 1400 | int sqlite3_win32_reset_heap(){ |
| 1401 | int rc; |
drh | 067b92b | 2020-06-19 15:24:12 | [diff] [blame] | 1402 | MUTEX_LOGIC( sqlite3_mutex *pMainMtx; ) /* The main static mutex */ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1403 | MUTEX_LOGIC( sqlite3_mutex *pMem; ) /* The memsys static mutex */ |
drh | 067b92b | 2020-06-19 15:24:12 | [diff] [blame] | 1404 | MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); ) |
drh | 97a7e5e | 2016-04-26 18:58:54 | [diff] [blame] | 1405 | MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); ) |
drh | 067b92b | 2020-06-19 15:24:12 | [diff] [blame] | 1406 | sqlite3_mutex_enter(pMainMtx); |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1407 | sqlite3_mutex_enter(pMem); |
mistachkin | 7f9d179 | 2013-11-08 20:10:57 | [diff] [blame] | 1408 | winMemAssertMagic(); |
| 1409 | if( winMemGetHeap()!=NULL && winMemGetOwned() && sqlite3_memory_used()==0 ){ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1410 | /* |
| 1411 | ** At this point, there should be no outstanding memory allocations on |
drh | 067b92b | 2020-06-19 15:24:12 | [diff] [blame] | 1412 | ** the heap. Also, since both the main and memsys locks are currently |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1413 | ** being held by us, no other function (i.e. from another thread) should |
| 1414 | ** be able to even access the heap. Attempt to destroy and recreate our |
| 1415 | ** isolated Win32 native heap now. |
| 1416 | */ |
mistachkin | 7f9d179 | 2013-11-08 20:10:57 | [diff] [blame] | 1417 | assert( winMemGetHeap()!=NULL ); |
| 1418 | assert( winMemGetOwned() ); |
| 1419 | assert( sqlite3_memory_used()==0 ); |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1420 | winMemShutdown(winMemGetDataPtr()); |
mistachkin | 7f9d179 | 2013-11-08 20:10:57 | [diff] [blame] | 1421 | assert( winMemGetHeap()==NULL ); |
| 1422 | assert( !winMemGetOwned() ); |
| 1423 | assert( sqlite3_memory_used()==0 ); |
mistachkin | 2eb0966 | 2013-11-08 18:52:45 | [diff] [blame] | 1424 | rc = winMemInit(winMemGetDataPtr()); |
mistachkin | 7f9d179 | 2013-11-08 20:10:57 | [diff] [blame] | 1425 | assert( rc!=SQLITE_OK || winMemGetHeap()!=NULL ); |
| 1426 | assert( rc!=SQLITE_OK || winMemGetOwned() ); |
| 1427 | assert( rc!=SQLITE_OK || sqlite3_memory_used()==0 ); |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1428 | }else{ |
| 1429 | /* |
| 1430 | ** The Win32 native heap cannot be modified because it may be in use. |
| 1431 | */ |
| 1432 | rc = SQLITE_BUSY; |
| 1433 | } |
| 1434 | sqlite3_mutex_leave(pMem); |
drh | 067b92b | 2020-06-19 15:24:12 | [diff] [blame] | 1435 | sqlite3_mutex_leave(pMainMtx); |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1436 | return rc; |
| 1437 | } |
mistachkin | 7f9d179 | 2013-11-08 20:10:57 | [diff] [blame] | 1438 | #endif /* SQLITE_WIN32_MALLOC */ |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1439 | |
stephan | 7b9407a | 2025-03-07 06:54:04 | [diff] [blame] | 1440 | #ifdef _WIN32 |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 1441 | /* |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 1442 | ** This function outputs the specified (ANSI) string to the Win32 debugger |
| 1443 | ** (if available). |
| 1444 | */ |
| 1445 | |
mistachkin | 7e87eae | 2013-02-12 09:46:48 | [diff] [blame] | 1446 | void sqlite3_win32_write_debug(const char *zBuf, int nBuf){ |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 1447 | char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE]; |
mistachkin | 16afb9e | 2012-03-14 23:08:59 | [diff] [blame] | 1448 | int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */ |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 1449 | if( nMin<-1 ) nMin = -1; /* all negative values become -1. */ |
mistachkin | 5ff7240 | 2012-03-13 03:38:22 | [diff] [blame] | 1450 | assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE ); |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1451 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1452 | if( !zBuf ){ |
| 1453 | (void)SQLITE_MISUSE_BKPT; |
| 1454 | return; |
| 1455 | } |
| 1456 | #endif |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 1457 | #if defined(SQLITE_WIN32_HAS_ANSI) |
| 1458 | if( nMin>0 ){ |
| 1459 | memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE); |
| 1460 | memcpy(zDbgBuf, zBuf, nMin); |
mistachkin | ca04d8a | 2012-03-19 23:28:35 | [diff] [blame] | 1461 | osOutputDebugStringA(zDbgBuf); |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 1462 | }else{ |
mistachkin | ca04d8a | 2012-03-19 23:28:35 | [diff] [blame] | 1463 | osOutputDebugStringA(zBuf); |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 1464 | } |
| 1465 | #elif defined(SQLITE_WIN32_HAS_WIDE) |
| 1466 | memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE); |
| 1467 | if ( osMultiByteToWideChar( |
| 1468 | osAreFileApisANSI() ? CP_ACP : CP_OEMCP, 0, zBuf, |
mistachkin | 0df898e | 2012-03-14 20:17:34 | [diff] [blame] | 1469 | nMin, (LPWSTR)zDbgBuf, SQLITE_WIN32_DBG_BUF_SIZE/sizeof(WCHAR))<=0 ){ |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 1470 | return; |
| 1471 | } |
mistachkin | 0df898e | 2012-03-14 20:17:34 | [diff] [blame] | 1472 | osOutputDebugStringW((LPCWSTR)zDbgBuf); |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 1473 | #else |
| 1474 | if( nMin>0 ){ |
| 1475 | memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE); |
| 1476 | memcpy(zDbgBuf, zBuf, nMin); |
| 1477 | fprintf(stderr, "%s", zDbgBuf); |
| 1478 | }else{ |
| 1479 | fprintf(stderr, "%s", zBuf); |
| 1480 | } |
| 1481 | #endif |
| 1482 | } |
stephan | 7b9407a | 2025-03-07 06:54:04 | [diff] [blame] | 1483 | #endif /* _WIN32 */ |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 1484 | |
| 1485 | /* |
| 1486 | ** The following routine suspends the current thread for at least ms |
| 1487 | ** milliseconds. This is equivalent to the Win32 Sleep() interface. |
drh | 7acec68 | 2012-03-01 21:19:39 | [diff] [blame] | 1488 | */ |
| 1489 | #if SQLITE_OS_WINRT |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 1490 | static HANDLE sleepObj = NULL; |
drh | 7acec68 | 2012-03-01 21:19:39 | [diff] [blame] | 1491 | #endif |
| 1492 | |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 1493 | void sqlite3_win32_sleep(DWORD milliseconds){ |
| 1494 | #if SQLITE_OS_WINRT |
| 1495 | if ( sleepObj==NULL ){ |
| 1496 | sleepObj = osCreateEventExW(NULL, NULL, CREATE_EVENT_MANUAL_RESET, |
| 1497 | SYNCHRONIZE); |
| 1498 | } |
| 1499 | assert( sleepObj!=NULL ); |
| 1500 | osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE); |
| 1501 | #else |
| 1502 | osSleep(milliseconds); |
| 1503 | #endif |
| 1504 | } |
| 1505 | |
mistachkin | 92c2e0d | 2014-10-16 18:34:50 | [diff] [blame] | 1506 | #if SQLITE_MAX_WORKER_THREADS>0 && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \ |
| 1507 | SQLITE_THREADSAFE>0 |
mistachkin | b1ac2bc | 2014-07-29 16:37:53 | [diff] [blame] | 1508 | DWORD sqlite3Win32Wait(HANDLE hObject){ |
| 1509 | DWORD rc; |
| 1510 | while( (rc = osWaitForSingleObjectEx(hObject, INFINITE, |
| 1511 | TRUE))==WAIT_IO_COMPLETION ){} |
| 1512 | return rc; |
| 1513 | } |
mistachkin | 30c633a | 2014-09-05 05:58:37 | [diff] [blame] | 1514 | #endif |
mistachkin | b1ac2bc | 2014-07-29 16:37:53 | [diff] [blame] | 1515 | |
drh | 7acec68 | 2012-03-01 21:19:39 | [diff] [blame] | 1516 | /* |
drh | cc78fea | 2006-01-06 16:17:05 | [diff] [blame] | 1517 | ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP, |
| 1518 | ** or WinCE. Return false (zero) for Win95, Win98, or WinME. |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 1519 | ** |
| 1520 | ** Here is an interesting observation: Win95, Win98, and WinME lack |
| 1521 | ** the LockFileEx() API. But we can still statically link against that |
shane | 50daafc | 2009-03-05 05:54:55 | [diff] [blame] | 1522 | ** API as long as we don't call it when running Win95/98/ME. A call to |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 1523 | ** this routine is used to determine if the host is Win95/98/ME or |
| 1524 | ** WinNT/2K/XP so that we will know whether or not we can safely call |
| 1525 | ** the LockFileEx() API. |
| 1526 | */ |
mistachkin | b8af6a2 | 2013-09-13 23:27:39 | [diff] [blame] | 1527 | |
mistachkin | d5be6f0 | 2016-01-27 07:28:33 | [diff] [blame] | 1528 | #if !SQLITE_WIN32_GETVERSIONEX |
mistachkin | 0f7e08e | 2013-11-27 03:01:34 | [diff] [blame] | 1529 | # define osIsNT() (1) |
| 1530 | #elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI) |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 1531 | # define osIsNT() (1) |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 1532 | #elif !defined(SQLITE_WIN32_HAS_WIDE) |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 1533 | # define osIsNT() (0) |
drh | cc78fea | 2006-01-06 16:17:05 | [diff] [blame] | 1534 | #else |
mistachkin | 202cb64 | 2014-07-31 18:54:01 | [diff] [blame] | 1535 | # define osIsNT() ((sqlite3_os_type==2) || sqlite3_win32_is_nt()) |
mistachkin | d691865 | 2014-07-29 05:49:02 | [diff] [blame] | 1536 | #endif |
| 1537 | |
| 1538 | /* |
| 1539 | ** This function determines if the machine is running a version of Windows |
| 1540 | ** based on the NT kernel. |
| 1541 | */ |
| 1542 | int sqlite3_win32_is_nt(void){ |
mistachkin | cf4200a | 2014-08-21 19:11:17 | [diff] [blame] | 1543 | #if SQLITE_OS_WINRT |
| 1544 | /* |
| 1545 | ** NOTE: The WinRT sub-platform is always assumed to be based on the NT |
| 1546 | ** kernel. |
| 1547 | */ |
| 1548 | return 1; |
mistachkin | d5be6f0 | 2016-01-27 07:28:33 | [diff] [blame] | 1549 | #elif SQLITE_WIN32_GETVERSIONEX |
mistachkin | 202cb64 | 2014-07-31 18:54:01 | [diff] [blame] | 1550 | if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){ |
mistachkin | 31753c8 | 2014-08-22 19:12:16 | [diff] [blame] | 1551 | #if defined(SQLITE_WIN32_HAS_ANSI) |
mistachkin | d691865 | 2014-07-29 05:49:02 | [diff] [blame] | 1552 | OSVERSIONINFOA sInfo; |
| 1553 | sInfo.dwOSVersionInfoSize = sizeof(sInfo); |
| 1554 | osGetVersionExA(&sInfo); |
mistachkin | 202cb64 | 2014-07-31 18:54:01 | [diff] [blame] | 1555 | osInterlockedCompareExchange(&sqlite3_os_type, |
| 1556 | (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0); |
mistachkin | 31753c8 | 2014-08-22 19:12:16 | [diff] [blame] | 1557 | #elif defined(SQLITE_WIN32_HAS_WIDE) |
| 1558 | OSVERSIONINFOW sInfo; |
| 1559 | sInfo.dwOSVersionInfoSize = sizeof(sInfo); |
| 1560 | osGetVersionExW(&sInfo); |
| 1561 | osInterlockedCompareExchange(&sqlite3_os_type, |
| 1562 | (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0); |
mistachkin | 4eb4fef | 2014-08-12 16:13:37 | [diff] [blame] | 1563 | #endif |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 1564 | } |
mistachkin | 202cb64 | 2014-07-31 18:54:01 | [diff] [blame] | 1565 | return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2; |
mistachkin | f0740a9 | 2014-08-11 17:51:23 | [diff] [blame] | 1566 | #elif SQLITE_TEST |
dan | 093c213 | 2024-12-04 18:27:13 | [diff] [blame] | 1567 | return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2 |
| 1568 | || osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 |
| 1569 | ; |
mistachkin | 91d1249 | 2014-08-11 17:38:38 | [diff] [blame] | 1570 | #else |
mistachkin | cf4200a | 2014-08-21 19:11:17 | [diff] [blame] | 1571 | /* |
| 1572 | ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are |
| 1573 | ** deprecated are always assumed to be based on the NT kernel. |
| 1574 | */ |
mistachkin | 91d1249 | 2014-08-11 17:38:38 | [diff] [blame] | 1575 | return 1; |
| 1576 | #endif |
mistachkin | d691865 | 2014-07-29 05:49:02 | [diff] [blame] | 1577 | } |
drh | cc78fea | 2006-01-06 16:17:05 | [diff] [blame] | 1578 | |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 1579 | #ifdef SQLITE_WIN32_MALLOC |
| 1580 | /* |
| 1581 | ** Allocate nBytes of memory. |
| 1582 | */ |
| 1583 | static void *winMemMalloc(int nBytes){ |
| 1584 | HANDLE hHeap; |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1585 | void *p; |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 1586 | |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1587 | winMemAssertMagic(); |
| 1588 | hHeap = winMemGetHeap(); |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 1589 | assert( hHeap!=0 ); |
| 1590 | assert( hHeap!=INVALID_HANDLE_VALUE ); |
mistachkin | e8c9a18 | 2012-03-14 20:20:37 | [diff] [blame] | 1591 | #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE) |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1592 | assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) ); |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 1593 | #endif |
| 1594 | assert( nBytes>=0 ); |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1595 | p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes); |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1596 | if( !p ){ |
mistachkin | a9cb5be | 2013-09-12 02:09:05 | [diff] [blame] | 1597 | sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%lu), heap=%p", |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1598 | nBytes, osGetLastError(), (void*)hHeap); |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1599 | } |
| 1600 | return p; |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | /* |
| 1604 | ** Free memory. |
| 1605 | */ |
| 1606 | static void winMemFree(void *pPrior){ |
| 1607 | HANDLE hHeap; |
| 1608 | |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1609 | winMemAssertMagic(); |
| 1610 | hHeap = winMemGetHeap(); |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 1611 | assert( hHeap!=0 ); |
| 1612 | assert( hHeap!=INVALID_HANDLE_VALUE ); |
mistachkin | e8c9a18 | 2012-03-14 20:20:37 | [diff] [blame] | 1613 | #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE) |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1614 | assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ); |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 1615 | #endif |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1616 | if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1617 | if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){ |
mistachkin | a9cb5be | 2013-09-12 02:09:05 | [diff] [blame] | 1618 | sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%lu), heap=%p", |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1619 | pPrior, osGetLastError(), (void*)hHeap); |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1620 | } |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | /* |
| 1624 | ** Change the size of an existing memory allocation |
| 1625 | */ |
| 1626 | static void *winMemRealloc(void *pPrior, int nBytes){ |
| 1627 | HANDLE hHeap; |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1628 | void *p; |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 1629 | |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1630 | winMemAssertMagic(); |
| 1631 | hHeap = winMemGetHeap(); |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 1632 | assert( hHeap!=0 ); |
| 1633 | assert( hHeap!=INVALID_HANDLE_VALUE ); |
mistachkin | e8c9a18 | 2012-03-14 20:20:37 | [diff] [blame] | 1634 | #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE) |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1635 | assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ); |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 1636 | #endif |
| 1637 | assert( nBytes>=0 ); |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1638 | if( !pPrior ){ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1639 | p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes); |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1640 | }else{ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1641 | p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes); |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1642 | } |
| 1643 | if( !p ){ |
mistachkin | a9cb5be | 2013-09-12 02:09:05 | [diff] [blame] | 1644 | sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%lu), heap=%p", |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1645 | pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(), |
| 1646 | (void*)hHeap); |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1647 | } |
| 1648 | return p; |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | /* |
| 1652 | ** Return the size of an outstanding allocation, in bytes. |
| 1653 | */ |
| 1654 | static int winMemSize(void *p){ |
| 1655 | HANDLE hHeap; |
| 1656 | SIZE_T n; |
| 1657 | |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1658 | winMemAssertMagic(); |
| 1659 | hHeap = winMemGetHeap(); |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 1660 | assert( hHeap!=0 ); |
| 1661 | assert( hHeap!=INVALID_HANDLE_VALUE ); |
mistachkin | e8c9a18 | 2012-03-14 20:20:37 | [diff] [blame] | 1662 | #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE) |
mistachkin | 055f165 | 2013-11-11 01:42:10 | [diff] [blame] | 1663 | assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, p) ); |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 1664 | #endif |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1665 | if( !p ) return 0; |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1666 | n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p); |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1667 | if( n==(SIZE_T)-1 ){ |
mistachkin | a9cb5be | 2013-09-12 02:09:05 | [diff] [blame] | 1668 | sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%lu), heap=%p", |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1669 | p, osGetLastError(), (void*)hHeap); |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1670 | return 0; |
| 1671 | } |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 1672 | return (int)n; |
| 1673 | } |
| 1674 | |
| 1675 | /* |
| 1676 | ** Round up a request size to the next valid allocation size. |
| 1677 | */ |
| 1678 | static int winMemRoundup(int n){ |
| 1679 | return n; |
| 1680 | } |
| 1681 | |
| 1682 | /* |
| 1683 | ** Initialize this module. |
| 1684 | */ |
| 1685 | static int winMemInit(void *pAppData){ |
| 1686 | winMemData *pWinMemData = (winMemData *)pAppData; |
| 1687 | |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1688 | if( !pWinMemData ) return SQLITE_ERROR; |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1689 | assert( pWinMemData->magic1==WINMEM_MAGIC1 ); |
| 1690 | assert( pWinMemData->magic2==WINMEM_MAGIC2 ); |
mistachkin | 0df898e | 2012-03-14 20:17:34 | [diff] [blame] | 1691 | |
mistachkin | 40c471d | 2012-03-15 03:40:59 | [diff] [blame] | 1692 | #if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1693 | if( !pWinMemData->hHeap ){ |
mistachkin | ac1f104 | 2013-11-23 00:27:29 | [diff] [blame] | 1694 | DWORD dwInitialSize = SQLITE_WIN32_HEAP_INIT_SIZE; |
| 1695 | DWORD dwMaximumSize = (DWORD)sqlite3GlobalConfig.nHeap; |
| 1696 | if( dwMaximumSize==0 ){ |
| 1697 | dwMaximumSize = SQLITE_WIN32_HEAP_MAX_SIZE; |
| 1698 | }else if( dwInitialSize>dwMaximumSize ){ |
| 1699 | dwInitialSize = dwMaximumSize; |
| 1700 | } |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1701 | pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS, |
mistachkin | ac1f104 | 2013-11-23 00:27:29 | [diff] [blame] | 1702 | dwInitialSize, dwMaximumSize); |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1703 | if( !pWinMemData->hHeap ){ |
| 1704 | sqlite3_log(SQLITE_NOMEM, |
mistachkin | ac1f104 | 2013-11-23 00:27:29 | [diff] [blame] | 1705 | "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu", |
| 1706 | osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize, |
| 1707 | dwMaximumSize); |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 1708 | return SQLITE_NOMEM_BKPT; |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 1709 | } |
| 1710 | pWinMemData->bOwned = TRUE; |
mistachkin | 0df898e | 2012-03-14 20:17:34 | [diff] [blame] | 1711 | assert( pWinMemData->bOwned ); |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 1712 | } |
mistachkin | 0df898e | 2012-03-14 20:17:34 | [diff] [blame] | 1713 | #else |
| 1714 | pWinMemData->hHeap = osGetProcessHeap(); |
| 1715 | if( !pWinMemData->hHeap ){ |
| 1716 | sqlite3_log(SQLITE_NOMEM, |
mistachkin | a9cb5be | 2013-09-12 02:09:05 | [diff] [blame] | 1717 | "failed to GetProcessHeap (%lu)", osGetLastError()); |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 1718 | return SQLITE_NOMEM_BKPT; |
mistachkin | 0df898e | 2012-03-14 20:17:34 | [diff] [blame] | 1719 | } |
| 1720 | pWinMemData->bOwned = FALSE; |
| 1721 | assert( !pWinMemData->bOwned ); |
| 1722 | #endif |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 1723 | assert( pWinMemData->hHeap!=0 ); |
| 1724 | assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE ); |
mistachkin | e8c9a18 | 2012-03-14 20:20:37 | [diff] [blame] | 1725 | #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1726 | assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) ); |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 1727 | #endif |
| 1728 | return SQLITE_OK; |
| 1729 | } |
| 1730 | |
| 1731 | /* |
| 1732 | ** Deinitialize this module. |
| 1733 | */ |
| 1734 | static void winMemShutdown(void *pAppData){ |
| 1735 | winMemData *pWinMemData = (winMemData *)pAppData; |
| 1736 | |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1737 | if( !pWinMemData ) return; |
mistachkin | e8f9105 | 2013-11-08 18:13:48 | [diff] [blame] | 1738 | assert( pWinMemData->magic1==WINMEM_MAGIC1 ); |
| 1739 | assert( pWinMemData->magic2==WINMEM_MAGIC2 ); |
| 1740 | |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1741 | if( pWinMemData->hHeap ){ |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 1742 | assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE ); |
mistachkin | e8c9a18 | 2012-03-14 20:20:37 | [diff] [blame] | 1743 | #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE) |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1744 | assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) ); |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 1745 | #endif |
mistachkin | 468690e | 2011-08-24 17:42:22 | [diff] [blame] | 1746 | if( pWinMemData->bOwned ){ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1747 | if( !osHeapDestroy(pWinMemData->hHeap) ){ |
mistachkin | a9cb5be | 2013-09-12 02:09:05 | [diff] [blame] | 1748 | sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%lu), heap=%p", |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1749 | osGetLastError(), (void*)pWinMemData->hHeap); |
mistachkin | 1b186a9 | 2011-08-24 16:13:57 | [diff] [blame] | 1750 | } |
| 1751 | pWinMemData->bOwned = FALSE; |
| 1752 | } |
| 1753 | pWinMemData->hHeap = NULL; |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | /* |
| 1758 | ** Populate the low-level memory allocation function pointers in |
| 1759 | ** sqlite3GlobalConfig.m with pointers to the routines in this file. The |
| 1760 | ** arguments specify the block of memory to manage. |
| 1761 | ** |
| 1762 | ** This routine is only called by sqlite3_config(), and therefore |
| 1763 | ** is not required to be threadsafe (it is not). |
| 1764 | */ |
| 1765 | const sqlite3_mem_methods *sqlite3MemGetWin32(void){ |
| 1766 | static const sqlite3_mem_methods winMemMethods = { |
| 1767 | winMemMalloc, |
| 1768 | winMemFree, |
| 1769 | winMemRealloc, |
| 1770 | winMemSize, |
| 1771 | winMemRoundup, |
| 1772 | winMemInit, |
| 1773 | winMemShutdown, |
| 1774 | &win_mem_data |
| 1775 | }; |
| 1776 | return &winMemMethods; |
| 1777 | } |
| 1778 | |
| 1779 | void sqlite3MemSetDefault(void){ |
| 1780 | sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32()); |
| 1781 | } |
| 1782 | #endif /* SQLITE_WIN32_MALLOC */ |
| 1783 | |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 1784 | #ifdef _WIN32 |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 1785 | /* |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1786 | ** Convert a UTF-8 string to Microsoft Unicode. |
drh | 584c094 | 2006-12-21 03:20:40 | [diff] [blame] | 1787 | ** |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1788 | ** Space to hold the returned string is obtained from sqlite3_malloc(). |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 1789 | */ |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1790 | static LPWSTR winUtf8ToUnicode(const char *zText){ |
drh | e3dd8bb | 2006-02-27 23:44:35 | [diff] [blame] | 1791 | int nChar; |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1792 | LPWSTR zWideText; |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 1793 | |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1794 | nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0); |
mistachkin | 6ca514b | 2011-12-14 00:37:45 | [diff] [blame] | 1795 | if( nChar==0 ){ |
| 1796 | return 0; |
| 1797 | } |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1798 | zWideText = sqlite3MallocZero( nChar*sizeof(WCHAR) ); |
| 1799 | if( zWideText==0 ){ |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 1800 | return 0; |
| 1801 | } |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1802 | nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText, |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1803 | nChar); |
drh | e3dd8bb | 2006-02-27 23:44:35 | [diff] [blame] | 1804 | if( nChar==0 ){ |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1805 | sqlite3_free(zWideText); |
| 1806 | zWideText = 0; |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 1807 | } |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1808 | return zWideText; |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 1809 | } |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 1810 | #endif /* _WIN32 */ |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 1811 | |
| 1812 | /* |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1813 | ** Convert a Microsoft Unicode string to UTF-8. |
| 1814 | ** |
| 1815 | ** Space to hold the returned string is obtained from sqlite3_malloc(). |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 1816 | */ |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1817 | static char *winUnicodeToUtf8(LPCWSTR zWideText){ |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 1818 | int nByte; |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1819 | char *zText; |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 1820 | |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1821 | nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0); |
mistachkin | 6ca514b | 2011-12-14 00:37:45 | [diff] [blame] | 1822 | if( nByte == 0 ){ |
| 1823 | return 0; |
| 1824 | } |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1825 | zText = sqlite3MallocZero( nByte ); |
| 1826 | if( zText==0 ){ |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 1827 | return 0; |
| 1828 | } |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1829 | nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte, |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1830 | 0, 0); |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 1831 | if( nByte == 0 ){ |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1832 | sqlite3_free(zText); |
| 1833 | zText = 0; |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 1834 | } |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1835 | return zText; |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 1836 | } |
| 1837 | |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1838 | /* |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1839 | ** Convert an ANSI string to Microsoft Unicode, using the ANSI or OEM |
| 1840 | ** code page. |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 1841 | ** |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1842 | ** Space to hold the returned string is obtained from sqlite3_malloc(). |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1843 | */ |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1844 | static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){ |
stephan | 7b9407a | 2025-03-07 06:54:04 | [diff] [blame] | 1845 | int nWideChar; |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1846 | LPWSTR zMbcsText; |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1847 | int codepage = useAnsi ? CP_ACP : CP_OEMCP; |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1848 | |
stephan | 7b9407a | 2025-03-07 06:54:04 | [diff] [blame] | 1849 | nWideChar = osMultiByteToWideChar(codepage, 0, zText, -1, NULL, |
| 1850 | 0); |
| 1851 | if( nWideChar==0 ){ |
mistachkin | 6ca514b | 2011-12-14 00:37:45 | [diff] [blame] | 1852 | return 0; |
| 1853 | } |
stephan | 7b9407a | 2025-03-07 06:54:04 | [diff] [blame] | 1854 | zMbcsText = sqlite3MallocZero( nWideChar*sizeof(WCHAR) ); |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1855 | if( zMbcsText==0 ){ |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1856 | return 0; |
| 1857 | } |
stephan | 7b9407a | 2025-03-07 06:54:04 | [diff] [blame] | 1858 | nWideChar = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText, |
| 1859 | nWideChar); |
| 1860 | if( nWideChar==0 ){ |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1861 | sqlite3_free(zMbcsText); |
| 1862 | zMbcsText = 0; |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1863 | } |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1864 | return zMbcsText; |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1865 | } |
| 1866 | |
stephan | 7b9407a | 2025-03-07 06:54:04 | [diff] [blame] | 1867 | #ifdef _WIN32 |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1868 | /* |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1869 | ** Convert a Microsoft Unicode string to a multi-byte character string, |
| 1870 | ** using the ANSI or OEM code page. |
drh | 584c094 | 2006-12-21 03:20:40 | [diff] [blame] | 1871 | ** |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1872 | ** Space to hold the returned string is obtained from sqlite3_malloc(). |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1873 | */ |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1874 | static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){ |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1875 | int nByte; |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1876 | char *zText; |
| 1877 | int codepage = useAnsi ? CP_ACP : CP_OEMCP; |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1878 | |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1879 | nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0); |
mistachkin | 6ca514b | 2011-12-14 00:37:45 | [diff] [blame] | 1880 | if( nByte == 0 ){ |
| 1881 | return 0; |
| 1882 | } |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1883 | zText = sqlite3MallocZero( nByte ); |
| 1884 | if( zText==0 ){ |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1885 | return 0; |
| 1886 | } |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1887 | nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText, |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1888 | nByte, 0, 0); |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1889 | if( nByte == 0 ){ |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1890 | sqlite3_free(zText); |
| 1891 | zText = 0; |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1892 | } |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1893 | return zText; |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1894 | } |
stephan | 7b9407a | 2025-03-07 06:54:04 | [diff] [blame] | 1895 | #endif /* _WIN32 */ |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1896 | |
| 1897 | /* |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1898 | ** Convert a multi-byte character string to UTF-8. |
| 1899 | ** |
| 1900 | ** Space to hold the returned string is obtained from sqlite3_malloc(). |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1901 | */ |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1902 | static char *winMbcsToUtf8(const char *zText, int useAnsi){ |
| 1903 | char *zTextUtf8; |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1904 | LPWSTR zTmpWide; |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1905 | |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1906 | zTmpWide = winMbcsToUnicode(zText, useAnsi); |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1907 | if( zTmpWide==0 ){ |
| 1908 | return 0; |
| 1909 | } |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1910 | zTextUtf8 = winUnicodeToUtf8(zTmpWide); |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 1911 | sqlite3_free(zTmpWide); |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1912 | return zTextUtf8; |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1913 | } |
| 1914 | |
stephan | 7b9407a | 2025-03-07 06:54:04 | [diff] [blame] | 1915 | #ifdef _WIN32 |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1916 | /* |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1917 | ** Convert a UTF-8 string to a multi-byte character string. |
| 1918 | ** |
| 1919 | ** Space to hold the returned string is obtained from sqlite3_malloc(). |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1920 | */ |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1921 | static char *winUtf8ToMbcs(const char *zText, int useAnsi){ |
| 1922 | char *zTextMbcs; |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 1923 | LPWSTR zTmpWide; |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1924 | |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1925 | zTmpWide = winUtf8ToUnicode(zText); |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1926 | if( zTmpWide==0 ){ |
| 1927 | return 0; |
| 1928 | } |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1929 | zTextMbcs = winUnicodeToMbcs(zTmpWide, useAnsi); |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 1930 | sqlite3_free(zTmpWide); |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1931 | return zTextMbcs; |
| 1932 | } |
| 1933 | |
| 1934 | /* |
| 1935 | ** This is a public wrapper for the winUtf8ToUnicode() function. |
| 1936 | */ |
| 1937 | LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText){ |
| 1938 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1939 | if( !zText ){ |
| 1940 | (void)SQLITE_MISUSE_BKPT; |
| 1941 | return 0; |
| 1942 | } |
| 1943 | #endif |
| 1944 | #ifndef SQLITE_OMIT_AUTOINIT |
| 1945 | if( sqlite3_initialize() ) return 0; |
| 1946 | #endif |
| 1947 | return winUtf8ToUnicode(zText); |
| 1948 | } |
| 1949 | |
| 1950 | /* |
| 1951 | ** This is a public wrapper for the winUnicodeToUtf8() function. |
| 1952 | */ |
| 1953 | char *sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText){ |
| 1954 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1955 | if( !zWideText ){ |
| 1956 | (void)SQLITE_MISUSE_BKPT; |
| 1957 | return 0; |
| 1958 | } |
| 1959 | #endif |
| 1960 | #ifndef SQLITE_OMIT_AUTOINIT |
| 1961 | if( sqlite3_initialize() ) return 0; |
| 1962 | #endif |
| 1963 | return winUnicodeToUtf8(zWideText); |
| 1964 | } |
stephan | 7b9407a | 2025-03-07 06:54:04 | [diff] [blame] | 1965 | #endif /* _WIN32 */ |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1966 | |
| 1967 | /* |
| 1968 | ** This is a public wrapper for the winMbcsToUtf8() function. |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1969 | */ |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1970 | char *sqlite3_win32_mbcs_to_utf8(const char *zText){ |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1971 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1972 | if( !zText ){ |
| 1973 | (void)SQLITE_MISUSE_BKPT; |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 1974 | return 0; |
| 1975 | } |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1976 | #endif |
| 1977 | #ifndef SQLITE_OMIT_AUTOINIT |
| 1978 | if( sqlite3_initialize() ) return 0; |
| 1979 | #endif |
| 1980 | return winMbcsToUtf8(zText, osAreFileApisANSI()); |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1981 | } |
| 1982 | |
stephan | 7b9407a | 2025-03-07 06:54:04 | [diff] [blame] | 1983 | #ifdef _WIN32 |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1984 | /* |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1985 | ** This is a public wrapper for the winMbcsToUtf8() function. |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1986 | */ |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1987 | char *sqlite3_win32_mbcs_to_utf8_v2(const char *zText, int useAnsi){ |
| 1988 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 1989 | if( !zText ){ |
| 1990 | (void)SQLITE_MISUSE_BKPT; |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1991 | return 0; |
| 1992 | } |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 1993 | #endif |
| 1994 | #ifndef SQLITE_OMIT_AUTOINIT |
| 1995 | if( sqlite3_initialize() ) return 0; |
| 1996 | #endif |
| 1997 | return winMbcsToUtf8(zText, useAnsi); |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 1998 | } |
| 1999 | |
| 2000 | /* |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 2001 | ** This is a public wrapper for the winUtf8ToMbcs() function. |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 2002 | */ |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 2003 | char *sqlite3_win32_utf8_to_mbcs(const char *zText){ |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 2004 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 2005 | if( !zText ){ |
| 2006 | (void)SQLITE_MISUSE_BKPT; |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 2007 | return 0; |
| 2008 | } |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 2009 | #endif |
| 2010 | #ifndef SQLITE_OMIT_AUTOINIT |
| 2011 | if( sqlite3_initialize() ) return 0; |
| 2012 | #endif |
| 2013 | return winUtf8ToMbcs(zText, osAreFileApisANSI()); |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 2014 | } |
| 2015 | |
| 2016 | /* |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 2017 | ** This is a public wrapper for the winUtf8ToMbcs() function. |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 2018 | */ |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 2019 | char *sqlite3_win32_utf8_to_mbcs_v2(const char *zText, int useAnsi){ |
| 2020 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 2021 | if( !zText ){ |
| 2022 | (void)SQLITE_MISUSE_BKPT; |
mistachkin | 5daed67 | 2016-04-03 22:44:16 | [diff] [blame] | 2023 | return 0; |
| 2024 | } |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 2025 | #endif |
| 2026 | #ifndef SQLITE_OMIT_AUTOINIT |
| 2027 | if( sqlite3_initialize() ) return 0; |
| 2028 | #endif |
| 2029 | return winUtf8ToMbcs(zText, useAnsi); |
drh | 371de5a | 2006-10-30 13:37:22 | [diff] [blame] | 2030 | } |
| 2031 | |
mistachkin | 6154068 | 2012-08-21 23:33:45 | [diff] [blame] | 2032 | /* |
mistachkin | 07430a8 | 2018-05-02 03:01:50 | [diff] [blame] | 2033 | ** This function is the same as sqlite3_win32_set_directory (below); however, |
| 2034 | ** it accepts a UTF-8 string. |
mistachkin | 6154068 | 2012-08-21 23:33:45 | [diff] [blame] | 2035 | */ |
mistachkin | 07430a8 | 2018-05-02 03:01:50 | [diff] [blame] | 2036 | int sqlite3_win32_set_directory8( |
mistachkin | 95d5ae1 | 2018-04-27 22:42:37 | [diff] [blame] | 2037 | unsigned long type, /* Identifier for directory being set or reset */ |
mistachkin | 07430a8 | 2018-05-02 03:01:50 | [diff] [blame] | 2038 | const char *zValue /* New value for directory being set or reset */ |
mistachkin | 95d5ae1 | 2018-04-27 22:42:37 | [diff] [blame] | 2039 | ){ |
mistachkin | 6154068 | 2012-08-21 23:33:45 | [diff] [blame] | 2040 | char **ppDirectory = 0; |
drh | 6e440ab | 2022-09-05 18:17:03 | [diff] [blame] | 2041 | int rc; |
mistachkin | 6154068 | 2012-08-21 23:33:45 | [diff] [blame] | 2042 | #ifndef SQLITE_OMIT_AUTOINIT |
drh | 6e440ab | 2022-09-05 18:17:03 | [diff] [blame] | 2043 | rc = sqlite3_initialize(); |
mistachkin | 6154068 | 2012-08-21 23:33:45 | [diff] [blame] | 2044 | if( rc ) return rc; |
| 2045 | #endif |
drh | 18a3a48 | 2022-09-02 00:36:16 | [diff] [blame] | 2046 | sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); |
mistachkin | 6154068 | 2012-08-21 23:33:45 | [diff] [blame] | 2047 | if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){ |
| 2048 | ppDirectory = &sqlite3_data_directory; |
| 2049 | }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){ |
| 2050 | ppDirectory = &sqlite3_temp_directory; |
| 2051 | } |
| 2052 | assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE |
| 2053 | || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE |
| 2054 | ); |
mistachkin | 484dbef | 2012-08-22 00:18:27 | [diff] [blame] | 2055 | assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) ); |
mistachkin | 6154068 | 2012-08-21 23:33:45 | [diff] [blame] | 2056 | if( ppDirectory ){ |
mistachkin | 07430a8 | 2018-05-02 03:01:50 | [diff] [blame] | 2057 | char *zCopy = 0; |
| 2058 | if( zValue && zValue[0] ){ |
| 2059 | zCopy = sqlite3_mprintf("%s", zValue); |
| 2060 | if ( zCopy==0 ){ |
drh | 18a3a48 | 2022-09-02 00:36:16 | [diff] [blame] | 2061 | rc = SQLITE_NOMEM_BKPT; |
| 2062 | goto set_directory8_done; |
mistachkin | 6154068 | 2012-08-21 23:33:45 | [diff] [blame] | 2063 | } |
| 2064 | } |
| 2065 | sqlite3_free(*ppDirectory); |
mistachkin | 07430a8 | 2018-05-02 03:01:50 | [diff] [blame] | 2066 | *ppDirectory = zCopy; |
drh | 18a3a48 | 2022-09-02 00:36:16 | [diff] [blame] | 2067 | rc = SQLITE_OK; |
| 2068 | }else{ |
| 2069 | rc = SQLITE_ERROR; |
mistachkin | 6154068 | 2012-08-21 23:33:45 | [diff] [blame] | 2070 | } |
drh | 18a3a48 | 2022-09-02 00:36:16 | [diff] [blame] | 2071 | set_directory8_done: |
| 2072 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); |
| 2073 | return rc; |
mistachkin | 6154068 | 2012-08-21 23:33:45 | [diff] [blame] | 2074 | } |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2075 | |
| 2076 | /* |
mistachkin | 07430a8 | 2018-05-02 03:01:50 | [diff] [blame] | 2077 | ** This function is the same as sqlite3_win32_set_directory (below); however, |
| 2078 | ** it accepts a UTF-16 string. |
| 2079 | */ |
| 2080 | int sqlite3_win32_set_directory16( |
| 2081 | unsigned long type, /* Identifier for directory being set or reset */ |
| 2082 | const void *zValue /* New value for directory being set or reset */ |
| 2083 | ){ |
| 2084 | int rc; |
| 2085 | char *zUtf8 = 0; |
| 2086 | if( zValue ){ |
| 2087 | zUtf8 = sqlite3_win32_unicode_to_utf8(zValue); |
| 2088 | if( zUtf8==0 ) return SQLITE_NOMEM_BKPT; |
| 2089 | } |
| 2090 | rc = sqlite3_win32_set_directory8(type, zUtf8); |
| 2091 | if( zUtf8 ) sqlite3_free(zUtf8); |
| 2092 | return rc; |
| 2093 | } |
| 2094 | |
| 2095 | /* |
| 2096 | ** This function sets the data directory or the temporary directory based on |
| 2097 | ** the provided arguments. The type argument must be 1 in order to set the |
| 2098 | ** data directory or 2 in order to set the temporary directory. The zValue |
| 2099 | ** argument is the name of the directory to use. The return value will be |
| 2100 | ** SQLITE_OK if successful. |
| 2101 | */ |
| 2102 | int sqlite3_win32_set_directory( |
| 2103 | unsigned long type, /* Identifier for directory being set or reset */ |
| 2104 | void *zValue /* New value for directory being set or reset */ |
| 2105 | ){ |
| 2106 | return sqlite3_win32_set_directory16(type, zValue); |
| 2107 | } |
stephan | 7b9407a | 2025-03-07 06:54:04 | [diff] [blame] | 2108 | #endif /* _WIN32 */ |
mistachkin | 07430a8 | 2018-05-02 03:01:50 | [diff] [blame] | 2109 | |
| 2110 | /* |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 2111 | ** The return value of winGetLastErrorMsg |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2112 | ** is zero if the error message fits in the buffer, or non-zero |
| 2113 | ** otherwise (if the message was truncated). |
| 2114 | */ |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 2115 | static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){ |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2116 | /* FormatMessage returns 0 on failure. Otherwise it |
| 2117 | ** returns the number of TCHARs written to the output |
| 2118 | ** buffer, excluding the terminating null char. |
| 2119 | */ |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2120 | DWORD dwLen = 0; |
| 2121 | char *zOut = 0; |
| 2122 | |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 2123 | if( osIsNT() ){ |
mistachkin | 62d1924 | 2012-03-02 23:53:54 | [diff] [blame] | 2124 | #if SQLITE_OS_WINRT |
mistachkin | 31706a2 | 2013-08-26 20:45:50 | [diff] [blame] | 2125 | WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1]; |
mistachkin | 62d1924 | 2012-03-02 23:53:54 | [diff] [blame] | 2126 | dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | |
| 2127 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 2128 | NULL, |
| 2129 | lastErrno, |
| 2130 | 0, |
| 2131 | zTempWide, |
mistachkin | 31706a2 | 2013-08-26 20:45:50 | [diff] [blame] | 2132 | SQLITE_WIN32_MAX_ERRMSG_CHARS, |
mistachkin | 62d1924 | 2012-03-02 23:53:54 | [diff] [blame] | 2133 | 0); |
| 2134 | #else |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2135 | LPWSTR zTempWide = NULL; |
| 2136 | dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 2137 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 2138 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 2139 | NULL, |
| 2140 | lastErrno, |
| 2141 | 0, |
| 2142 | (LPWSTR) &zTempWide, |
| 2143 | 0, |
| 2144 | 0); |
mistachkin | 62d1924 | 2012-03-02 23:53:54 | [diff] [blame] | 2145 | #endif |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2146 | if( dwLen > 0 ){ |
| 2147 | /* allocate a buffer and convert to UTF8 */ |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 | [diff] [blame] | 2148 | sqlite3BeginBenignMalloc(); |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 2149 | zOut = winUnicodeToUtf8(zTempWide); |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 | [diff] [blame] | 2150 | sqlite3EndBenignMalloc(); |
mistachkin | 62d1924 | 2012-03-02 23:53:54 | [diff] [blame] | 2151 | #if !SQLITE_OS_WINRT |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2152 | /* free the system buffer allocated by FormatMessage */ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2153 | osLocalFree(zTempWide); |
mistachkin | 62d1924 | 2012-03-02 23:53:54 | [diff] [blame] | 2154 | #endif |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2155 | } |
drh | d52ee72 | 2012-03-02 00:00:47 | [diff] [blame] | 2156 | } |
| 2157 | #ifdef SQLITE_WIN32_HAS_ANSI |
| 2158 | else{ |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2159 | char *zTemp = NULL; |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2160 | dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 2161 | FORMAT_MESSAGE_FROM_SYSTEM | |
| 2162 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 2163 | NULL, |
| 2164 | lastErrno, |
| 2165 | 0, |
| 2166 | (LPSTR) &zTemp, |
| 2167 | 0, |
| 2168 | 0); |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2169 | if( dwLen > 0 ){ |
| 2170 | /* allocate a buffer and convert to UTF8 */ |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 | [diff] [blame] | 2171 | sqlite3BeginBenignMalloc(); |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 2172 | zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI()); |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 | [diff] [blame] | 2173 | sqlite3EndBenignMalloc(); |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2174 | /* free the system buffer allocated by FormatMessage */ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2175 | osLocalFree(zTemp); |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2176 | } |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2177 | } |
drh | d52ee72 | 2012-03-02 00:00:47 | [diff] [blame] | 2178 | #endif |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2179 | if( 0 == dwLen ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 2180 | sqlite3_snprintf(nBuf, zBuf, "OsError 0x%lx (%lu)", lastErrno, lastErrno); |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2181 | }else{ |
| 2182 | /* copy a maximum of nBuf chars to output buffer */ |
| 2183 | sqlite3_snprintf(nBuf, zBuf, "%s", zOut); |
| 2184 | /* free the UTF8 buffer */ |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 2185 | sqlite3_free(zOut); |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2186 | } |
| 2187 | return 0; |
| 2188 | } |
| 2189 | |
| 2190 | /* |
| 2191 | ** |
| 2192 | ** This function - winLogErrorAtLine() - is only ever called via the macro |
| 2193 | ** winLogError(). |
| 2194 | ** |
| 2195 | ** This routine is invoked after an error occurs in an OS function. |
| 2196 | ** It logs a message using sqlite3_log() containing the current value of |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 2197 | ** error code and, if possible, the human-readable equivalent from |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2198 | ** FormatMessage. |
| 2199 | ** |
| 2200 | ** The first argument passed to the macro should be the error code that |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 2201 | ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2202 | ** The two subsequent arguments should be the name of the OS function that |
mistachkin | d557843 | 2012-08-25 10:01:29 | [diff] [blame] | 2203 | ** failed and the associated file-system path, if any. |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2204 | */ |
mistachkin | 2aef997 | 2011-11-10 20:21:20 | [diff] [blame] | 2205 | #define winLogError(a,b,c,d) winLogErrorAtLine(a,b,c,d,__LINE__) |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2206 | static int winLogErrorAtLine( |
| 2207 | int errcode, /* SQLite error code */ |
mistachkin | 2aef997 | 2011-11-10 20:21:20 | [diff] [blame] | 2208 | DWORD lastErrno, /* Win32 last error */ |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2209 | const char *zFunc, /* Name of OS function that failed */ |
| 2210 | const char *zPath, /* File path associated with error */ |
| 2211 | int iLine /* Source line number where error occurred */ |
| 2212 | ){ |
| 2213 | char zMsg[500]; /* Human readable error text */ |
drh | 5f2dfdb | 2011-04-13 23:42:53 | [diff] [blame] | 2214 | int i; /* Loop counter */ |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2215 | |
| 2216 | zMsg[0] = 0; |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 2217 | winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg); |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2218 | assert( errcode!=SQLITE_OK ); |
| 2219 | if( zPath==0 ) zPath = ""; |
drh | 5f2dfdb | 2011-04-13 23:42:53 | [diff] [blame] | 2220 | for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){} |
| 2221 | zMsg[i] = 0; |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2222 | sqlite3_log(errcode, |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 2223 | "os_win.c:%d: (%lu) %s(%s) - %s", |
mistachkin | 2aef997 | 2011-11-10 20:21:20 | [diff] [blame] | 2224 | iLine, lastErrno, zFunc, zPath, zMsg |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2225 | ); |
| 2226 | |
| 2227 | return errcode; |
| 2228 | } |
| 2229 | |
drh | 5d9ef0a | 2011-07-11 18:17:56 | [diff] [blame] | 2230 | /* |
drh | 52564d7 | 2011-07-12 11:04:18 | [diff] [blame] | 2231 | ** The number of times that a ReadFile(), WriteFile(), and DeleteFile() |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 2232 | ** will be retried following a locking error - probably caused by |
drh | 52564d7 | 2011-07-12 11:04:18 | [diff] [blame] | 2233 | ** antivirus software. Also the initial delay before the first retry. |
| 2234 | ** The delay increases linearly with each retry. |
drh | 5d9ef0a | 2011-07-11 18:17:56 | [diff] [blame] | 2235 | */ |
| 2236 | #ifndef SQLITE_WIN32_IOERR_RETRY |
drh | 52564d7 | 2011-07-12 11:04:18 | [diff] [blame] | 2237 | # define SQLITE_WIN32_IOERR_RETRY 10 |
| 2238 | #endif |
| 2239 | #ifndef SQLITE_WIN32_IOERR_RETRY_DELAY |
| 2240 | # define SQLITE_WIN32_IOERR_RETRY_DELAY 25 |
drh | 5d9ef0a | 2011-07-11 18:17:56 | [diff] [blame] | 2241 | #endif |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 2242 | static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY; |
| 2243 | static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY; |
drh | 5d9ef0a | 2011-07-11 18:17:56 | [diff] [blame] | 2244 | |
| 2245 | /* |
mistachkin | 491451d | 2014-02-18 05:18:36 | [diff] [blame] | 2246 | ** The "winIoerrCanRetry1" macro is used to determine if a particular I/O |
| 2247 | ** error code obtained via GetLastError() is eligible to be retried. It |
| 2248 | ** must accept the error code DWORD as its only argument and should return |
| 2249 | ** non-zero if the error code is transient in nature and the operation |
| 2250 | ** responsible for generating the original error might succeed upon being |
| 2251 | ** retried. The argument to this macro should be a variable. |
| 2252 | ** |
| 2253 | ** Additionally, a macro named "winIoerrCanRetry2" may be defined. If it |
| 2254 | ** is defined, it will be consulted only when the macro "winIoerrCanRetry1" |
| 2255 | ** returns zero. The "winIoerrCanRetry2" macro is completely optional and |
| 2256 | ** may be used to include additional error codes in the set that should |
| 2257 | ** result in the failing I/O operation being retried by the caller. If |
| 2258 | ** defined, the "winIoerrCanRetry2" macro must exhibit external semantics |
| 2259 | ** identical to those of the "winIoerrCanRetry1" macro. |
| 2260 | */ |
| 2261 | #if !defined(winIoerrCanRetry1) |
| 2262 | #define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED) || \ |
| 2263 | ((a)==ERROR_SHARING_VIOLATION) || \ |
| 2264 | ((a)==ERROR_LOCK_VIOLATION) || \ |
| 2265 | ((a)==ERROR_DEV_NOT_EXIST) || \ |
| 2266 | ((a)==ERROR_NETNAME_DELETED) || \ |
| 2267 | ((a)==ERROR_SEM_TIMEOUT) || \ |
| 2268 | ((a)==ERROR_NETWORK_UNREACHABLE)) |
| 2269 | #endif |
| 2270 | |
| 2271 | /* |
drh | 5d9ef0a | 2011-07-11 18:17:56 | [diff] [blame] | 2272 | ** If a ReadFile() or WriteFile() error occurs, invoke this routine |
| 2273 | ** to see if it should be retried. Return TRUE to retry. Return FALSE |
| 2274 | ** to give up with an error. |
| 2275 | */ |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 2276 | static int winRetryIoerr(int *pnRetry, DWORD *pError){ |
mistachkin | d1ef9b6 | 2011-11-21 00:54:37 | [diff] [blame] | 2277 | DWORD e = osGetLastError(); |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 2278 | if( *pnRetry>=winIoerrRetry ){ |
mistachkin | d1ef9b6 | 2011-11-21 00:54:37 | [diff] [blame] | 2279 | if( pError ){ |
| 2280 | *pError = e; |
| 2281 | } |
drh | 5d9ef0a | 2011-07-11 18:17:56 | [diff] [blame] | 2282 | return 0; |
| 2283 | } |
mistachkin | 491451d | 2014-02-18 05:18:36 | [diff] [blame] | 2284 | if( winIoerrCanRetry1(e) ){ |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 2285 | sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry)); |
drh | 5d9ef0a | 2011-07-11 18:17:56 | [diff] [blame] | 2286 | ++*pnRetry; |
| 2287 | return 1; |
| 2288 | } |
mistachkin | 491451d | 2014-02-18 05:18:36 | [diff] [blame] | 2289 | #if defined(winIoerrCanRetry2) |
| 2290 | else if( winIoerrCanRetry2(e) ){ |
| 2291 | sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry)); |
| 2292 | ++*pnRetry; |
| 2293 | return 1; |
| 2294 | } |
| 2295 | #endif |
mistachkin | d1ef9b6 | 2011-11-21 00:54:37 | [diff] [blame] | 2296 | if( pError ){ |
| 2297 | *pError = e; |
| 2298 | } |
drh | 5d9ef0a | 2011-07-11 18:17:56 | [diff] [blame] | 2299 | return 0; |
| 2300 | } |
| 2301 | |
drh | a32ad84 | 2011-07-12 13:51:05 | [diff] [blame] | 2302 | /* |
| 2303 | ** Log a I/O error retry episode. |
| 2304 | */ |
drh | 21aa6a1 | 2015-03-26 15:27:32 | [diff] [blame] | 2305 | static void winLogIoerr(int nRetry, int lineno){ |
drh | a32ad84 | 2011-07-12 13:51:05 | [diff] [blame] | 2306 | if( nRetry ){ |
drh | 8237f6d | 2015-03-31 17:32:06 | [diff] [blame] | 2307 | sqlite3_log(SQLITE_NOTICE, |
drh | 21aa6a1 | 2015-03-26 15:27:32 | [diff] [blame] | 2308 | "delayed %dms for lock/sharing conflict at line %d", |
| 2309 | winIoerrRetryDelay*nRetry*(nRetry+1)/2, lineno |
drh | a32ad84 | 2011-07-12 13:51:05 | [diff] [blame] | 2310 | ); |
| 2311 | } |
| 2312 | } |
| 2313 | |
mistachkin | 6cc16fc | 2016-01-23 01:54:15 | [diff] [blame] | 2314 | /* |
mistachkin | 8366ddf | 2016-04-12 16:11:52 | [diff] [blame] | 2315 | ** This #if does not rely on the SQLITE_OS_WINCE define because the |
| 2316 | ** corresponding section in "date.c" cannot use it. |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2317 | */ |
mistachkin | 0cedb96 | 2016-04-11 22:45:45 | [diff] [blame] | 2318 | #if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \ |
mistachkin | 0e188e1 | 2016-04-11 22:10:26 | [diff] [blame] | 2319 | (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API) |
mistachkin | 8366ddf | 2016-04-12 16:11:52 | [diff] [blame] | 2320 | /* |
| 2321 | ** The MSVC CRT on Windows CE may not have a localtime() function. |
| 2322 | ** So define a substitute. |
| 2323 | */ |
mistachkin | 0cedb96 | 2016-04-11 22:45:45 | [diff] [blame] | 2324 | # include <time.h> |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2325 | struct tm *__cdecl localtime(const time_t *t) |
| 2326 | { |
| 2327 | static struct tm y; |
| 2328 | FILETIME uTm, lTm; |
| 2329 | SYSTEMTIME pTm; |
drh | c51250a | 2007-09-20 14:39:23 | [diff] [blame] | 2330 | sqlite3_int64 t64; |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2331 | t64 = *t; |
| 2332 | t64 = (t64 + 11644473600)*10000000; |
shane | 11bb41f | 2009-09-10 20:23:30 | [diff] [blame] | 2333 | uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF); |
| 2334 | uTm.dwHighDateTime= (DWORD)(t64 >> 32); |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2335 | osFileTimeToLocalFileTime(&uTm,&lTm); |
| 2336 | osFileTimeToSystemTime(&lTm,&pTm); |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2337 | y.tm_year = pTm.wYear - 1900; |
| 2338 | y.tm_mon = pTm.wMonth - 1; |
| 2339 | y.tm_wday = pTm.wDayOfWeek; |
| 2340 | y.tm_mday = pTm.wDay; |
| 2341 | y.tm_hour = pTm.wHour; |
| 2342 | y.tm_min = pTm.wMinute; |
| 2343 | y.tm_sec = pTm.wSecond; |
| 2344 | return &y; |
| 2345 | } |
mistachkin | 2bfe1df | 2013-02-07 05:12:25 | [diff] [blame] | 2346 | #endif |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2347 | |
mistachkin | 6cc16fc | 2016-01-23 01:54:15 | [diff] [blame] | 2348 | #if SQLITE_OS_WINCE |
| 2349 | /************************************************************************* |
| 2350 | ** This section contains code for WinCE only. |
| 2351 | */ |
shane | 11bb41f | 2009-09-10 20:23:30 | [diff] [blame] | 2352 | #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)] |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2353 | |
| 2354 | /* |
| 2355 | ** Acquire a lock on the handle h |
| 2356 | */ |
| 2357 | static void winceMutexAcquire(HANDLE h){ |
| 2358 | DWORD dwErr; |
| 2359 | do { |
mistachkin | 75b70a2 | 2012-03-02 13:47:16 | [diff] [blame] | 2360 | dwErr = osWaitForSingleObject(h, INFINITE); |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2361 | } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED); |
| 2362 | } |
| 2363 | /* |
| 2364 | ** Release a lock acquired by winceMutexAcquire() |
| 2365 | */ |
| 2366 | #define winceMutexRelease(h) ReleaseMutex(h) |
| 2367 | |
| 2368 | /* |
| 2369 | ** Create the mutex and shared memory used for locking in the file |
| 2370 | ** descriptor pFile |
| 2371 | */ |
mistachkin | 7e87eae | 2013-02-12 09:46:48 | [diff] [blame] | 2372 | static int winceCreateLock(const char *zFilename, winFile *pFile){ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2373 | LPWSTR zTok; |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 2374 | LPWSTR zName; |
mistachkin | 7e87eae | 2013-02-12 09:46:48 | [diff] [blame] | 2375 | DWORD lastErrno; |
| 2376 | BOOL bLogged = FALSE; |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2377 | BOOL bInit = TRUE; |
| 2378 | |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 2379 | zName = winUtf8ToUnicode(zFilename); |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 2380 | if( zName==0 ){ |
| 2381 | /* out of memory */ |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 2382 | return SQLITE_IOERR_NOMEM_BKPT; |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 2383 | } |
| 2384 | |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2385 | /* Initialize the local lockdata */ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2386 | memset(&pFile->local, 0, sizeof(pFile->local)); |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2387 | |
| 2388 | /* Replace the backslashes from the filename and lowercase it |
| 2389 | ** to derive a mutex name. */ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2390 | zTok = osCharLowerW(zName); |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2391 | for (;*zTok;zTok++){ |
| 2392 | if (*zTok == '\\') *zTok = '_'; |
| 2393 | } |
| 2394 | |
| 2395 | /* Create/open the named mutex */ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2396 | pFile->hMutex = osCreateMutexW(NULL, FALSE, zName); |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2397 | if (!pFile->hMutex){ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2398 | pFile->lastErrno = osGetLastError(); |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 2399 | sqlite3_free(zName); |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 2400 | return winLogError(SQLITE_IOERR, pFile->lastErrno, |
| 2401 | "winceCreateLock1", zFilename); |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2402 | } |
| 2403 | |
| 2404 | /* Acquire the mutex before continuing */ |
| 2405 | winceMutexAcquire(pFile->hMutex); |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 2406 | |
| 2407 | /* Since the names of named mutexes, semaphores, file mappings etc are |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2408 | ** case-sensitive, take advantage of that by uppercasing the mutex name |
| 2409 | ** and using that as the shared filemapping name. |
| 2410 | */ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2411 | osCharUpperW(zName); |
| 2412 | pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL, |
| 2413 | PAGE_READWRITE, 0, sizeof(winceLock), |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 2414 | zName); |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2415 | |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 2416 | /* Set a flag that indicates we're the first to create the memory so it |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2417 | ** must be zero-initialized */ |
mistachkin | 7e87eae | 2013-02-12 09:46:48 | [diff] [blame] | 2418 | lastErrno = osGetLastError(); |
| 2419 | if (lastErrno == ERROR_ALREADY_EXISTS){ |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2420 | bInit = FALSE; |
| 2421 | } |
| 2422 | |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 2423 | sqlite3_free(zName); |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2424 | |
| 2425 | /* If we succeeded in making the shared memory handle, map it. */ |
mistachkin | 7e87eae | 2013-02-12 09:46:48 | [diff] [blame] | 2426 | if( pFile->hShared ){ |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 2427 | pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared, |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2428 | FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock)); |
| 2429 | /* If mapping failed, close the shared memory handle and erase it */ |
mistachkin | 7e87eae | 2013-02-12 09:46:48 | [diff] [blame] | 2430 | if( !pFile->shared ){ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2431 | pFile->lastErrno = osGetLastError(); |
mistachkin | 7e87eae | 2013-02-12 09:46:48 | [diff] [blame] | 2432 | winLogError(SQLITE_IOERR, pFile->lastErrno, |
| 2433 | "winceCreateLock2", zFilename); |
| 2434 | bLogged = TRUE; |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2435 | osCloseHandle(pFile->hShared); |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2436 | pFile->hShared = NULL; |
| 2437 | } |
| 2438 | } |
| 2439 | |
| 2440 | /* If shared memory could not be created, then close the mutex and fail */ |
mistachkin | 7e87eae | 2013-02-12 09:46:48 | [diff] [blame] | 2441 | if( pFile->hShared==NULL ){ |
| 2442 | if( !bLogged ){ |
| 2443 | pFile->lastErrno = lastErrno; |
| 2444 | winLogError(SQLITE_IOERR, pFile->lastErrno, |
| 2445 | "winceCreateLock3", zFilename); |
| 2446 | bLogged = TRUE; |
| 2447 | } |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2448 | winceMutexRelease(pFile->hMutex); |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2449 | osCloseHandle(pFile->hMutex); |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2450 | pFile->hMutex = NULL; |
mistachkin | 7e87eae | 2013-02-12 09:46:48 | [diff] [blame] | 2451 | return SQLITE_IOERR; |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2452 | } |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 2453 | |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2454 | /* Initialize the shared memory if we're supposed to */ |
mistachkin | 7e87eae | 2013-02-12 09:46:48 | [diff] [blame] | 2455 | if( bInit ){ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2456 | memset(pFile->shared, 0, sizeof(winceLock)); |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2457 | } |
| 2458 | |
| 2459 | winceMutexRelease(pFile->hMutex); |
mistachkin | 7e87eae | 2013-02-12 09:46:48 | [diff] [blame] | 2460 | return SQLITE_OK; |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2461 | } |
| 2462 | |
| 2463 | /* |
| 2464 | ** Destroy the part of winFile that deals with wince locks |
| 2465 | */ |
| 2466 | static void winceDestroyLock(winFile *pFile){ |
| 2467 | if (pFile->hMutex){ |
| 2468 | /* Acquire the mutex */ |
| 2469 | winceMutexAcquire(pFile->hMutex); |
| 2470 | |
| 2471 | /* The following blocks should probably assert in debug mode, but they |
| 2472 | are to cleanup in case any locks remained open */ |
| 2473 | if (pFile->local.nReaders){ |
| 2474 | pFile->shared->nReaders --; |
| 2475 | } |
| 2476 | if (pFile->local.bReserved){ |
| 2477 | pFile->shared->bReserved = FALSE; |
| 2478 | } |
| 2479 | if (pFile->local.bPending){ |
| 2480 | pFile->shared->bPending = FALSE; |
| 2481 | } |
| 2482 | if (pFile->local.bExclusive){ |
| 2483 | pFile->shared->bExclusive = FALSE; |
| 2484 | } |
| 2485 | |
| 2486 | /* De-reference and close our copy of the shared memory handle */ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2487 | osUnmapViewOfFile(pFile->shared); |
| 2488 | osCloseHandle(pFile->hShared); |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2489 | |
| 2490 | /* Done with the mutex */ |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 2491 | winceMutexRelease(pFile->hMutex); |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2492 | osCloseHandle(pFile->hMutex); |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2493 | pFile->hMutex = NULL; |
| 2494 | } |
| 2495 | } |
| 2496 | |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 2497 | /* |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2498 | ** An implementation of the LockFile() API of Windows for CE |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2499 | */ |
| 2500 | static BOOL winceLockFile( |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 2501 | LPHANDLE phFile, |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2502 | DWORD dwFileOffsetLow, |
| 2503 | DWORD dwFileOffsetHigh, |
| 2504 | DWORD nNumberOfBytesToLockLow, |
| 2505 | DWORD nNumberOfBytesToLockHigh |
| 2506 | ){ |
| 2507 | winFile *pFile = HANDLE_TO_WINFILE(phFile); |
| 2508 | BOOL bReturn = FALSE; |
| 2509 | |
shane | 11bb41f | 2009-09-10 20:23:30 | [diff] [blame] | 2510 | UNUSED_PARAMETER(dwFileOffsetHigh); |
| 2511 | UNUSED_PARAMETER(nNumberOfBytesToLockHigh); |
| 2512 | |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2513 | if (!pFile->hMutex) return TRUE; |
| 2514 | winceMutexAcquire(pFile->hMutex); |
| 2515 | |
| 2516 | /* Wanting an exclusive lock? */ |
shane | 11bb41f | 2009-09-10 20:23:30 | [diff] [blame] | 2517 | if (dwFileOffsetLow == (DWORD)SHARED_FIRST |
| 2518 | && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){ |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2519 | if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){ |
| 2520 | pFile->shared->bExclusive = TRUE; |
| 2521 | pFile->local.bExclusive = TRUE; |
| 2522 | bReturn = TRUE; |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | /* Want a read-only lock? */ |
shane | 11bb41f | 2009-09-10 20:23:30 | [diff] [blame] | 2527 | else if (dwFileOffsetLow == (DWORD)SHARED_FIRST && |
shane | 338ea3c | 2009-08-05 04:08:29 | [diff] [blame] | 2528 | nNumberOfBytesToLockLow == 1){ |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2529 | if (pFile->shared->bExclusive == 0){ |
| 2530 | pFile->local.nReaders ++; |
| 2531 | if (pFile->local.nReaders == 1){ |
| 2532 | pFile->shared->nReaders ++; |
| 2533 | } |
| 2534 | bReturn = TRUE; |
| 2535 | } |
| 2536 | } |
| 2537 | |
| 2538 | /* Want a pending lock? */ |
drh | 1488052 | 2013-03-01 23:24:04 | [diff] [blame] | 2539 | else if (dwFileOffsetLow == (DWORD)PENDING_BYTE |
| 2540 | && nNumberOfBytesToLockLow == 1){ |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2541 | /* If no pending lock has been acquired, then acquire it */ |
| 2542 | if (pFile->shared->bPending == 0) { |
| 2543 | pFile->shared->bPending = TRUE; |
| 2544 | pFile->local.bPending = TRUE; |
| 2545 | bReturn = TRUE; |
| 2546 | } |
| 2547 | } |
shane | 338ea3c | 2009-08-05 04:08:29 | [diff] [blame] | 2548 | |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2549 | /* Want a reserved lock? */ |
drh | 1488052 | 2013-03-01 23:24:04 | [diff] [blame] | 2550 | else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE |
| 2551 | && nNumberOfBytesToLockLow == 1){ |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2552 | if (pFile->shared->bReserved == 0) { |
| 2553 | pFile->shared->bReserved = TRUE; |
| 2554 | pFile->local.bReserved = TRUE; |
| 2555 | bReturn = TRUE; |
| 2556 | } |
| 2557 | } |
| 2558 | |
| 2559 | winceMutexRelease(pFile->hMutex); |
| 2560 | return bReturn; |
| 2561 | } |
| 2562 | |
| 2563 | /* |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2564 | ** An implementation of the UnlockFile API of Windows for CE |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2565 | */ |
| 2566 | static BOOL winceUnlockFile( |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 2567 | LPHANDLE phFile, |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2568 | DWORD dwFileOffsetLow, |
| 2569 | DWORD dwFileOffsetHigh, |
| 2570 | DWORD nNumberOfBytesToUnlockLow, |
| 2571 | DWORD nNumberOfBytesToUnlockHigh |
| 2572 | ){ |
| 2573 | winFile *pFile = HANDLE_TO_WINFILE(phFile); |
| 2574 | BOOL bReturn = FALSE; |
| 2575 | |
shane | 11bb41f | 2009-09-10 20:23:30 | [diff] [blame] | 2576 | UNUSED_PARAMETER(dwFileOffsetHigh); |
| 2577 | UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh); |
| 2578 | |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2579 | if (!pFile->hMutex) return TRUE; |
| 2580 | winceMutexAcquire(pFile->hMutex); |
| 2581 | |
| 2582 | /* Releasing a reader lock or an exclusive lock */ |
shane | 11bb41f | 2009-09-10 20:23:30 | [diff] [blame] | 2583 | if (dwFileOffsetLow == (DWORD)SHARED_FIRST){ |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2584 | /* Did we have an exclusive lock? */ |
| 2585 | if (pFile->local.bExclusive){ |
shane | 11bb41f | 2009-09-10 20:23:30 | [diff] [blame] | 2586 | assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE); |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2587 | pFile->local.bExclusive = FALSE; |
| 2588 | pFile->shared->bExclusive = FALSE; |
| 2589 | bReturn = TRUE; |
| 2590 | } |
| 2591 | |
| 2592 | /* Did we just have a reader lock? */ |
| 2593 | else if (pFile->local.nReaders){ |
drh | 1488052 | 2013-03-01 23:24:04 | [diff] [blame] | 2594 | assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE |
| 2595 | || nNumberOfBytesToUnlockLow == 1); |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2596 | pFile->local.nReaders --; |
| 2597 | if (pFile->local.nReaders == 0) |
| 2598 | { |
| 2599 | pFile->shared->nReaders --; |
| 2600 | } |
| 2601 | bReturn = TRUE; |
| 2602 | } |
| 2603 | } |
| 2604 | |
| 2605 | /* Releasing a pending lock */ |
drh | 1488052 | 2013-03-01 23:24:04 | [diff] [blame] | 2606 | else if (dwFileOffsetLow == (DWORD)PENDING_BYTE |
| 2607 | && nNumberOfBytesToUnlockLow == 1){ |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2608 | if (pFile->local.bPending){ |
| 2609 | pFile->local.bPending = FALSE; |
| 2610 | pFile->shared->bPending = FALSE; |
| 2611 | bReturn = TRUE; |
| 2612 | } |
| 2613 | } |
| 2614 | /* Releasing a reserved lock */ |
drh | 1488052 | 2013-03-01 23:24:04 | [diff] [blame] | 2615 | else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE |
| 2616 | && nNumberOfBytesToUnlockLow == 1){ |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2617 | if (pFile->local.bReserved) { |
| 2618 | pFile->local.bReserved = FALSE; |
| 2619 | pFile->shared->bReserved = FALSE; |
| 2620 | bReturn = TRUE; |
| 2621 | } |
| 2622 | } |
| 2623 | |
| 2624 | winceMutexRelease(pFile->hMutex); |
| 2625 | return bReturn; |
| 2626 | } |
drh | 72aead8 | 2006-01-23 15:54:25 | [diff] [blame] | 2627 | /* |
| 2628 | ** End of the special code for wince |
| 2629 | *****************************************************************************/ |
danielk1977 | 29bafea | 2008-06-26 10:41:19 | [diff] [blame] | 2630 | #endif /* SQLITE_OS_WINCE */ |
drh | c092998 | 2005-09-05 19:08:29 | [diff] [blame] | 2631 | |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 2632 | /* |
| 2633 | ** Lock a file region. |
| 2634 | */ |
| 2635 | static BOOL winLockFile( |
| 2636 | LPHANDLE phFile, |
| 2637 | DWORD flags, |
| 2638 | DWORD offsetLow, |
| 2639 | DWORD offsetHigh, |
| 2640 | DWORD numBytesLow, |
| 2641 | DWORD numBytesHigh |
| 2642 | ){ |
| 2643 | #if SQLITE_OS_WINCE |
| 2644 | /* |
| 2645 | ** NOTE: Windows CE is handled differently here due its lack of the Win32 |
| 2646 | ** API LockFile. |
| 2647 | */ |
| 2648 | return winceLockFile(phFile, offsetLow, offsetHigh, |
| 2649 | numBytesLow, numBytesHigh); |
| 2650 | #else |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 2651 | if( osIsNT() ){ |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 2652 | OVERLAPPED ovlp; |
| 2653 | memset(&ovlp, 0, sizeof(OVERLAPPED)); |
| 2654 | ovlp.Offset = offsetLow; |
| 2655 | ovlp.OffsetHigh = offsetHigh; |
| 2656 | return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp); |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 2657 | #ifdef SQLITE_WIN32_HAS_ANSI |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 2658 | }else{ |
| 2659 | return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow, |
| 2660 | numBytesHigh); |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 2661 | #endif |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 2662 | } |
| 2663 | #endif |
| 2664 | } |
| 2665 | |
| 2666 | /* |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 2667 | ** Lock a region of nByte bytes starting at offset offset of file hFile. |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 2668 | ** Take an EXCLUSIVE lock if parameter bExclusive is true, or a SHARED lock |
| 2669 | ** otherwise. If nMs is greater than zero and the lock cannot be obtained |
| 2670 | ** immediately, block for that many ms before giving up. |
| 2671 | ** |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 2672 | ** This function returns SQLITE_OK if the lock is obtained successfully. If |
| 2673 | ** some other process holds the lock, SQLITE_BUSY is returned if nMs==0, or |
| 2674 | ** SQLITE_BUSY_TIMEOUT otherwise. Or, if an error occurs, SQLITE_IOERR. |
| 2675 | */ |
dan | d50eb9c | 2024-12-10 19:00:07 | [diff] [blame] | 2676 | static int winHandleLockTimeout( |
dan | 1743c5f | 2024-11-25 16:54:10 | [diff] [blame] | 2677 | HANDLE hFile, |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 2678 | DWORD offset, |
| 2679 | DWORD nByte, |
| 2680 | int bExcl, |
dan | 4628888 | 2025-01-30 15:26:16 | [diff] [blame] | 2681 | DWORD nMs |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 2682 | ){ |
| 2683 | DWORD flags = LOCKFILE_FAIL_IMMEDIATELY | (bExcl?LOCKFILE_EXCLUSIVE_LOCK:0); |
| 2684 | int rc = SQLITE_OK; |
| 2685 | BOOL ret; |
| 2686 | |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 2687 | if( !osIsNT() ){ |
dan | 1743c5f | 2024-11-25 16:54:10 | [diff] [blame] | 2688 | ret = winLockFile(&hFile, flags, offset, 0, nByte, 0); |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 2689 | }else{ |
| 2690 | OVERLAPPED ovlp; |
| 2691 | memset(&ovlp, 0, sizeof(OVERLAPPED)); |
| 2692 | ovlp.Offset = offset; |
dan | 1743c5f | 2024-11-25 16:54:10 | [diff] [blame] | 2693 | |
dan | ce50282 | 2024-12-24 14:44:38 | [diff] [blame] | 2694 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
dan | 4628888 | 2025-01-30 15:26:16 | [diff] [blame] | 2695 | if( nMs!=0 ){ |
dan | 1743c5f | 2024-11-25 16:54:10 | [diff] [blame] | 2696 | flags &= ~LOCKFILE_FAIL_IMMEDIATELY; |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 2697 | } |
dan | ce50282 | 2024-12-24 14:44:38 | [diff] [blame] | 2698 | ovlp.hEvent = osCreateEvent(NULL, TRUE, FALSE, NULL); |
| 2699 | if( ovlp.hEvent==NULL ){ |
| 2700 | return SQLITE_IOERR_LOCK; |
| 2701 | } |
| 2702 | #endif |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 2703 | |
dan | 1743c5f | 2024-11-25 16:54:10 | [diff] [blame] | 2704 | ret = osLockFileEx(hFile, flags, 0, nByte, 0, &ovlp); |
dan | f6d2674 | 2024-11-25 18:47:12 | [diff] [blame] | 2705 | |
dan | ce50282 | 2024-12-24 14:44:38 | [diff] [blame] | 2706 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 2707 | /* If SQLITE_ENABLE_SETLK_TIMEOUT is defined, then the file-handle was |
| 2708 | ** opened with FILE_FLAG_OVERHEAD specified. In this case, the call to |
| 2709 | ** LockFileEx() may fail because the request is still pending. This can |
dan | 4628888 | 2025-01-30 15:26:16 | [diff] [blame] | 2710 | ** happen even if LOCKFILE_FAIL_IMMEDIATELY was specified. |
| 2711 | ** |
| 2712 | ** If nMs is 0, then LOCKFILE_FAIL_IMMEDIATELY was set in the flags |
| 2713 | ** passed to LockFileEx(). In this case, if the operation is pending, |
| 2714 | ** block indefinitely until it is finished. |
| 2715 | ** |
| 2716 | ** Otherwise, wait for up to nMs ms for the operation to finish. nMs |
| 2717 | ** may be set to INFINITE. |
| 2718 | */ |
dan | ce50282 | 2024-12-24 14:44:38 | [diff] [blame] | 2719 | if( !ret && GetLastError()==ERROR_IO_PENDING ){ |
dan | 4628888 | 2025-01-30 15:26:16 | [diff] [blame] | 2720 | DWORD nDelay = (nMs==0 ? INFINITE : nMs); |
dan | ce50282 | 2024-12-24 14:44:38 | [diff] [blame] | 2721 | DWORD res = osWaitForSingleObject(ovlp.hEvent, nDelay); |
dan | 1743c5f | 2024-11-25 16:54:10 | [diff] [blame] | 2722 | if( res==WAIT_OBJECT_0 ){ |
dan | 1743c5f | 2024-11-25 16:54:10 | [diff] [blame] | 2723 | ret = TRUE; |
dan | ce50282 | 2024-12-24 14:44:38 | [diff] [blame] | 2724 | }else if( res==WAIT_TIMEOUT ){ |
dan | 69ce758 | 2025-06-02 18:37:32 | [diff] [blame] | 2725 | #if SQLITE_ENABLE_SETLK_TIMEOUT==1 |
dan | ce50282 | 2024-12-24 14:44:38 | [diff] [blame] | 2726 | rc = SQLITE_BUSY_TIMEOUT; |
dan | 69ce758 | 2025-06-02 18:37:32 | [diff] [blame] | 2727 | #else |
| 2728 | rc = SQLITE_BUSY; |
| 2729 | #endif |
dan | 1743c5f | 2024-11-25 16:54:10 | [diff] [blame] | 2730 | }else{ |
dan | ce50282 | 2024-12-24 14:44:38 | [diff] [blame] | 2731 | /* Some other error has occurred */ |
| 2732 | rc = SQLITE_IOERR_LOCK; |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 2733 | } |
dan | 362d51a | 2024-12-26 16:10:15 | [diff] [blame] | 2734 | |
| 2735 | /* If it is still pending, cancel the LockFileEx() call. */ |
dan | ce50282 | 2024-12-24 14:44:38 | [diff] [blame] | 2736 | osCancelIo(hFile); |
dan | 1743c5f | 2024-11-25 16:54:10 | [diff] [blame] | 2737 | } |
| 2738 | |
dan | ce50282 | 2024-12-24 14:44:38 | [diff] [blame] | 2739 | osCloseHandle(ovlp.hEvent); |
| 2740 | #endif |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 2741 | } |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 2742 | |
| 2743 | if( rc==SQLITE_OK && !ret ){ |
| 2744 | rc = SQLITE_BUSY; |
| 2745 | } |
| 2746 | return rc; |
| 2747 | } |
| 2748 | |
| 2749 | /* |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 2750 | ** Unlock a file region. |
| 2751 | */ |
| 2752 | static BOOL winUnlockFile( |
| 2753 | LPHANDLE phFile, |
| 2754 | DWORD offsetLow, |
| 2755 | DWORD offsetHigh, |
| 2756 | DWORD numBytesLow, |
| 2757 | DWORD numBytesHigh |
| 2758 | ){ |
| 2759 | #if SQLITE_OS_WINCE |
| 2760 | /* |
| 2761 | ** NOTE: Windows CE is handled differently here due its lack of the Win32 |
| 2762 | ** API UnlockFile. |
| 2763 | */ |
| 2764 | return winceUnlockFile(phFile, offsetLow, offsetHigh, |
| 2765 | numBytesLow, numBytesHigh); |
| 2766 | #else |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 2767 | if( osIsNT() ){ |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 2768 | OVERLAPPED ovlp; |
| 2769 | memset(&ovlp, 0, sizeof(OVERLAPPED)); |
| 2770 | ovlp.Offset = offsetLow; |
| 2771 | ovlp.OffsetHigh = offsetHigh; |
| 2772 | return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp); |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 2773 | #ifdef SQLITE_WIN32_HAS_ANSI |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 2774 | }else{ |
| 2775 | return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow, |
| 2776 | numBytesHigh); |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 2777 | #endif |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 2778 | } |
| 2779 | #endif |
| 2780 | } |
| 2781 | |
dan | d50eb9c | 2024-12-10 19:00:07 | [diff] [blame] | 2782 | /* |
| 2783 | ** Remove an nByte lock starting at offset iOff from HANDLE h. |
| 2784 | */ |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 2785 | static int winHandleUnlock(HANDLE h, int iOff, int nByte){ |
| 2786 | BOOL ret = winUnlockFile(&h, iOff, 0, nByte, 0); |
| 2787 | return (ret ? SQLITE_OK : SQLITE_IOERR_UNLOCK); |
| 2788 | } |
| 2789 | |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 2790 | /***************************************************************************** |
| 2791 | ** The next group of routines implement the I/O methods specified |
| 2792 | ** by the sqlite3_io_methods object. |
| 2793 | ******************************************************************************/ |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 2794 | |
| 2795 | /* |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2796 | ** Some Microsoft compilers lack this definition. |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 2797 | */ |
| 2798 | #ifndef INVALID_SET_FILE_POINTER |
| 2799 | # define INVALID_SET_FILE_POINTER ((DWORD)-1) |
| 2800 | #endif |
| 2801 | |
| 2802 | /* |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 2803 | ** Seek the file handle h to offset nByte of the file. |
| 2804 | ** |
| 2805 | ** If successful, return SQLITE_OK. Or, if an error occurs, return an SQLite |
| 2806 | ** error code. |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 2807 | */ |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 2808 | static int winHandleSeek(HANDLE h, sqlite3_int64 iOffset){ |
| 2809 | int rc = SQLITE_OK; /* Return value */ |
| 2810 | |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 2811 | #if !SQLITE_OS_WINRT |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 2812 | LONG upperBits; /* Most sig. 32 bits of new offset */ |
| 2813 | LONG lowerBits; /* Least sig. 32 bits of new offset */ |
| 2814 | DWORD dwRet; /* Value returned by SetFilePointer() */ |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 2815 | |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 2816 | upperBits = (LONG)((iOffset>>32) & 0x7fffffff); |
| 2817 | lowerBits = (LONG)(iOffset & 0xffffffff); |
| 2818 | |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 2819 | dwRet = osSetFilePointer(h, lowerBits, &upperBits, FILE_BEGIN); |
| 2820 | |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 2821 | /* API oddity: If successful, SetFilePointer() returns a dword |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 2822 | ** containing the lower 32-bits of the new file-offset. Or, if it fails, |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 2823 | ** it returns INVALID_SET_FILE_POINTER. However according to MSDN, |
| 2824 | ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine |
| 2825 | ** whether an error has actually occurred, it is also necessary to call |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 2826 | ** GetLastError(). */ |
| 2827 | if( dwRet==INVALID_SET_FILE_POINTER ){ |
| 2828 | DWORD lastErrno = osGetLastError(); |
| 2829 | if( lastErrno!=NO_ERROR ){ |
| 2830 | rc = SQLITE_IOERR_SEEK; |
| 2831 | } |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 2832 | } |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 2833 | #else |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 2834 | /* This implementation works for WinRT. */ |
mistachkin | 75b70a2 | 2012-03-02 13:47:16 | [diff] [blame] | 2835 | LARGE_INTEGER x; /* The new offset */ |
| 2836 | BOOL bRet; /* Value returned by SetFilePointerEx() */ |
| 2837 | |
drh | 8045df0 | 2012-03-01 22:06:30 | [diff] [blame] | 2838 | x.QuadPart = iOffset; |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 2839 | bRet = osSetFilePointerEx(h, x, 0, FILE_BEGIN); |
mistachkin | 75b70a2 | 2012-03-02 13:47:16 | [diff] [blame] | 2840 | |
| 2841 | if(!bRet){ |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 2842 | rc = SQLITE_IOERR_SEEK; |
mistachkin | 75b70a2 | 2012-03-02 13:47:16 | [diff] [blame] | 2843 | } |
drh | 8045df0 | 2012-03-01 22:06:30 | [diff] [blame] | 2844 | #endif |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 2845 | |
| 2846 | OSTRACE(("SEEK file=%p, offset=%lld rc=%s\n", h, iOffset, sqlite3ErrName(rc))); |
| 2847 | return rc; |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 2848 | } |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 2849 | |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 2850 | /* |
| 2851 | ** Move the current position of the file handle passed as the first |
| 2852 | ** argument to offset iOffset within the file. If successful, return 0. |
| 2853 | ** Otherwise, set pFile->lastErrno and return non-zero. |
| 2854 | */ |
| 2855 | static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){ |
| 2856 | int rc; |
| 2857 | |
| 2858 | rc = winHandleSeek(pFile->h, iOffset); |
| 2859 | if( rc!=SQLITE_OK ){ |
| 2860 | pFile->lastErrno = osGetLastError(); |
| 2861 | winLogError(rc, pFile->lastErrno, "winSeekFile", pFile->zPath); |
| 2862 | } |
| 2863 | return rc; |
| 2864 | } |
| 2865 | |
| 2866 | |
mistachkin | 5824e05 | 2013-04-15 20:08:27 | [diff] [blame] | 2867 | #if SQLITE_MAX_MMAP_SIZE>0 |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 2868 | /* Forward references to VFS helper methods used for memory mapped files */ |
| 2869 | static int winMapfile(winFile*, sqlite3_int64); |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 2870 | static int winUnmapfile(winFile*); |
mistachkin | 5824e05 | 2013-04-15 20:08:27 | [diff] [blame] | 2871 | #endif |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 2872 | |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 2873 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 2874 | ** Close a file. |
drh | 59e63a6 | 2006-06-04 23:31:48 | [diff] [blame] | 2875 | ** |
| 2876 | ** It is reported that an attempt to close a handle might sometimes |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2877 | ** fail. This is a very unreasonable result, but Windows is notorious |
drh | 59e63a6 | 2006-06-04 23:31:48 | [diff] [blame] | 2878 | ** for being unreasonable so I do not doubt that it might happen. If |
| 2879 | ** the close fails, we pause for 100 milliseconds and try again. As |
| 2880 | ** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before |
| 2881 | ** giving up and returning an error. |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 2882 | */ |
drh | 59e63a6 | 2006-06-04 23:31:48 | [diff] [blame] | 2883 | #define MX_CLOSE_ATTEMPT 3 |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 2884 | static int winClose(sqlite3_file *id){ |
| 2885 | int rc, cnt = 0; |
| 2886 | winFile *pFile = (winFile*)id; |
shane | 50daafc | 2009-03-05 05:54:55 | [diff] [blame] | 2887 | |
| 2888 | assert( id!=0 ); |
mistachkin | 6f92833 | 2012-08-17 11:47:32 | [diff] [blame] | 2889 | #ifndef SQLITE_OMIT_WAL |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 2890 | assert( pFile->pShm==0 ); |
mistachkin | 6f92833 | 2012-08-17 11:47:32 | [diff] [blame] | 2891 | #endif |
mistachkin | 9ce59a9 | 2013-02-13 22:54:03 | [diff] [blame] | 2892 | assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE ); |
mistachkin | ad1e55e | 2015-03-27 18:20:25 | [diff] [blame] | 2893 | OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p\n", |
| 2894 | osGetCurrentProcessId(), pFile, pFile->h)); |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 2895 | |
mistachkin | 5824e05 | 2013-04-15 20:08:27 | [diff] [blame] | 2896 | #if SQLITE_MAX_MMAP_SIZE>0 |
mistachkin | c216566 | 2013-10-16 09:49:10 | [diff] [blame] | 2897 | winUnmapfile(pFile); |
mistachkin | 5824e05 | 2013-04-15 20:08:27 | [diff] [blame] | 2898 | #endif |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 2899 | |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 2900 | do{ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2901 | rc = osCloseHandle(pFile->h); |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 2902 | /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */ |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 2903 | }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) ); |
danielk1977 | 29bafea | 2008-06-26 10:41:19 | [diff] [blame] | 2904 | #if SQLITE_OS_WINCE |
drh | d641d51 | 2008-02-20 00:00:00 | [diff] [blame] | 2905 | #define WINCE_DELETION_ATTEMPTS 3 |
mistachkin | 1e75483 | 2016-07-08 21:14:37 | [diff] [blame] | 2906 | { |
| 2907 | winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData; |
| 2908 | if( pAppData==NULL || !pAppData->bNoLock ){ |
| 2909 | winceDestroyLock(pFile); |
| 2910 | } |
| 2911 | } |
drh | 7229ed4 | 2007-10-08 12:29:17 | [diff] [blame] | 2912 | if( pFile->zDeleteOnClose ){ |
drh | d641d51 | 2008-02-20 00:00:00 | [diff] [blame] | 2913 | int cnt = 0; |
| 2914 | while( |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2915 | osDeleteFileW(pFile->zDeleteOnClose)==0 |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 2916 | && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff |
drh | d641d51 | 2008-02-20 00:00:00 | [diff] [blame] | 2917 | && cnt++ < WINCE_DELETION_ATTEMPTS |
| 2918 | ){ |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 2919 | sqlite3_win32_sleep(100); /* Wait a little before trying again */ |
drh | d641d51 | 2008-02-20 00:00:00 | [diff] [blame] | 2920 | } |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 2921 | sqlite3_free(pFile->zDeleteOnClose); |
drh | 7229ed4 | 2007-10-08 12:29:17 | [diff] [blame] | 2922 | } |
drh | cc78fea | 2006-01-06 16:17:05 | [diff] [blame] | 2923 | #endif |
mistachkin | 05340e3 | 2012-03-30 12:27:55 | [diff] [blame] | 2924 | if( rc ){ |
| 2925 | pFile->h = NULL; |
| 2926 | } |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 2927 | OpenCounter(-1); |
mistachkin | ad1e55e | 2015-03-27 18:20:25 | [diff] [blame] | 2928 | OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p, rc=%s\n", |
| 2929 | osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed")); |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 2930 | return rc ? SQLITE_OK |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 2931 | : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(), |
mistachkin | 2aef997 | 2011-11-10 20:21:20 | [diff] [blame] | 2932 | "winClose", pFile->zPath); |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 2933 | } |
| 2934 | |
| 2935 | /* |
| 2936 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 2937 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 2938 | ** wrong. |
| 2939 | */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 2940 | static int winRead( |
| 2941 | sqlite3_file *id, /* File to read from */ |
| 2942 | void *pBuf, /* Write content into this buffer */ |
| 2943 | int amt, /* Number of bytes to read */ |
| 2944 | sqlite3_int64 offset /* Begin reading at this offset */ |
| 2945 | ){ |
mistachkin | d9d812f | 2014-12-20 22:21:49 | [diff] [blame] | 2946 | #if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED) |
mistachkin | 05340e3 | 2012-03-30 12:27:55 | [diff] [blame] | 2947 | OVERLAPPED overlapped; /* The offset for ReadFile. */ |
mistachkin | 00fa55d | 2012-03-30 16:44:33 | [diff] [blame] | 2948 | #endif |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 2949 | winFile *pFile = (winFile*)id; /* file handle */ |
| 2950 | DWORD nRead; /* Number of bytes actually read from file */ |
drh | 5d9ef0a | 2011-07-11 18:17:56 | [diff] [blame] | 2951 | int nRetry = 0; /* Number of retrys */ |
shane | 50daafc | 2009-03-05 05:54:55 | [diff] [blame] | 2952 | |
drh | 9cbe635 | 2005-11-29 03:13:21 | [diff] [blame] | 2953 | assert( id!=0 ); |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 2954 | assert( amt>0 ); |
drh | 6cf9d8d | 2013-05-09 18:12:40 | [diff] [blame] | 2955 | assert( offset>=0 ); |
drh | 9cce710 | 2007-01-09 17:18:19 | [diff] [blame] | 2956 | SimulateIOError(return SQLITE_IOERR_READ); |
mistachkin | ad1e55e | 2015-03-27 18:20:25 | [diff] [blame] | 2957 | OSTRACE(("READ pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, " |
| 2958 | "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile, |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 2959 | pFile->h, pBuf, amt, offset, pFile->locktype)); |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 2960 | |
drh | 9b4c59f | 2013-04-15 17:03:42 | [diff] [blame] | 2961 | #if SQLITE_MAX_MMAP_SIZE>0 |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2962 | /* Deal with as much of this read request as possible by transferring |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 2963 | ** data from the memory mapping using memcpy(). */ |
| 2964 | if( offset<pFile->mmapSize ){ |
| 2965 | if( offset+amt <= pFile->mmapSize ){ |
| 2966 | memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt); |
mistachkin | ad1e55e | 2015-03-27 18:20:25 | [diff] [blame] | 2967 | OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n", |
| 2968 | osGetCurrentProcessId(), pFile, pFile->h)); |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 2969 | return SQLITE_OK; |
| 2970 | }else{ |
| 2971 | int nCopy = (int)(pFile->mmapSize - offset); |
| 2972 | memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy); |
| 2973 | pBuf = &((u8 *)pBuf)[nCopy]; |
| 2974 | amt -= nCopy; |
| 2975 | offset += nCopy; |
| 2976 | } |
| 2977 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 | [diff] [blame] | 2978 | #endif |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 2979 | |
mistachkin | d9d812f | 2014-12-20 22:21:49 | [diff] [blame] | 2980 | #if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED) |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 2981 | if( winSeekFile(pFile, offset) ){ |
mistachkin | ad1e55e | 2015-03-27 18:20:25 | [diff] [blame] | 2982 | OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n", |
| 2983 | osGetCurrentProcessId(), pFile, pFile->h)); |
mistachkin | 00fa55d | 2012-03-30 16:44:33 | [diff] [blame] | 2984 | return SQLITE_FULL; |
| 2985 | } |
| 2986 | while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){ |
| 2987 | #else |
mistachkin | 05340e3 | 2012-03-30 12:27:55 | [diff] [blame] | 2988 | memset(&overlapped, 0, sizeof(OVERLAPPED)); |
| 2989 | overlapped.Offset = (LONG)(offset & 0xffffffff); |
| 2990 | overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff); |
| 2991 | while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) && |
| 2992 | osGetLastError()!=ERROR_HANDLE_EOF ){ |
mistachkin | 00fa55d | 2012-03-30 16:44:33 | [diff] [blame] | 2993 | #endif |
mistachkin | d1ef9b6 | 2011-11-21 00:54:37 | [diff] [blame] | 2994 | DWORD lastErrno; |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 2995 | if( winRetryIoerr(&nRetry, &lastErrno) ) continue; |
mistachkin | d1ef9b6 | 2011-11-21 00:54:37 | [diff] [blame] | 2996 | pFile->lastErrno = lastErrno; |
mistachkin | ad1e55e | 2015-03-27 18:20:25 | [diff] [blame] | 2997 | OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_READ\n", |
| 2998 | osGetCurrentProcessId(), pFile, pFile->h)); |
mistachkin | 2aef997 | 2011-11-10 20:21:20 | [diff] [blame] | 2999 | return winLogError(SQLITE_IOERR_READ, pFile->lastErrno, |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 3000 | "winRead", pFile->zPath); |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3001 | } |
drh | 21aa6a1 | 2015-03-26 15:27:32 | [diff] [blame] | 3002 | winLogIoerr(nRetry, __LINE__); |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3003 | if( nRead<(DWORD)amt ){ |
drh | 4c17c3f | 2008-11-07 00:06:18 | [diff] [blame] | 3004 | /* Unread parts of the buffer must be zero-filled */ |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3005 | memset(&((char*)pBuf)[nRead], 0, amt-nRead); |
mistachkin | ad1e55e | 2015-03-27 18:20:25 | [diff] [blame] | 3006 | OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_SHORT_READ\n", |
| 3007 | osGetCurrentProcessId(), pFile, pFile->h)); |
drh | 551b773 | 2006-11-06 21:20:25 | [diff] [blame] | 3008 | return SQLITE_IOERR_SHORT_READ; |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3009 | } |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3010 | |
mistachkin | ad1e55e | 2015-03-27 18:20:25 | [diff] [blame] | 3011 | OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n", |
| 3012 | osGetCurrentProcessId(), pFile, pFile->h)); |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3013 | return SQLITE_OK; |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3014 | } |
| 3015 | |
| 3016 | /* |
| 3017 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 3018 | ** or some other error code on failure. |
| 3019 | */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 3020 | static int winWrite( |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3021 | sqlite3_file *id, /* File to write into */ |
| 3022 | const void *pBuf, /* The bytes to be written */ |
| 3023 | int amt, /* Number of bytes to write */ |
| 3024 | sqlite3_int64 offset /* Offset into the file to begin writing at */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 3025 | ){ |
mistachkin | 48864df | 2013-03-21 21:20:32 | [diff] [blame] | 3026 | int rc = 0; /* True if error has occurred, else false */ |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3027 | winFile *pFile = (winFile*)id; /* File handle */ |
drh | a32ad84 | 2011-07-12 13:51:05 | [diff] [blame] | 3028 | int nRetry = 0; /* Number of retries */ |
shane | 50daafc | 2009-03-05 05:54:55 | [diff] [blame] | 3029 | |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3030 | assert( amt>0 ); |
| 3031 | assert( pFile ); |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 3032 | SimulateIOError(return SQLITE_IOERR_WRITE); |
drh | 5968593 | 2006-09-14 13:47:11 | [diff] [blame] | 3033 | SimulateDiskfullError(return SQLITE_FULL); |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3034 | |
mistachkin | ad1e55e | 2015-03-27 18:20:25 | [diff] [blame] | 3035 | OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, " |
| 3036 | "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile, |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3037 | pFile->h, pBuf, amt, offset, pFile->locktype)); |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3038 | |
mistachkin | c88cd13 | 2015-11-17 21:42:32 | [diff] [blame] | 3039 | #if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0 |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3040 | /* Deal with as much of this write request as possible by transferring |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 3041 | ** data from the memory mapping using memcpy(). */ |
| 3042 | if( offset<pFile->mmapSize ){ |
| 3043 | if( offset+amt <= pFile->mmapSize ){ |
| 3044 | memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt); |
mistachkin | ad1e55e | 2015-03-27 18:20:25 | [diff] [blame] | 3045 | OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n", |
| 3046 | osGetCurrentProcessId(), pFile, pFile->h)); |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 3047 | return SQLITE_OK; |
| 3048 | }else{ |
| 3049 | int nCopy = (int)(pFile->mmapSize - offset); |
| 3050 | memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy); |
| 3051 | pBuf = &((u8 *)pBuf)[nCopy]; |
| 3052 | amt -= nCopy; |
| 3053 | offset += nCopy; |
| 3054 | } |
| 3055 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 | [diff] [blame] | 3056 | #endif |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3057 | |
mistachkin | d9d812f | 2014-12-20 22:21:49 | [diff] [blame] | 3058 | #if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED) |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 3059 | rc = winSeekFile(pFile, offset); |
mistachkin | 00fa55d | 2012-03-30 16:44:33 | [diff] [blame] | 3060 | if( rc==0 ){ |
| 3061 | #else |
mistachkin | 05340e3 | 2012-03-30 12:27:55 | [diff] [blame] | 3062 | { |
mistachkin | 00fa55d | 2012-03-30 16:44:33 | [diff] [blame] | 3063 | #endif |
mistachkin | d9d812f | 2014-12-20 22:21:49 | [diff] [blame] | 3064 | #if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED) |
mistachkin | 05340e3 | 2012-03-30 12:27:55 | [diff] [blame] | 3065 | OVERLAPPED overlapped; /* The offset for WriteFile. */ |
mistachkin | 00fa55d | 2012-03-30 16:44:33 | [diff] [blame] | 3066 | #endif |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3067 | u8 *aRem = (u8 *)pBuf; /* Data yet to be written */ |
| 3068 | int nRem = amt; /* Number of bytes yet to be written */ |
| 3069 | DWORD nWrite; /* Bytes written by each WriteFile() call */ |
mistachkin | d1ef9b6 | 2011-11-21 00:54:37 | [diff] [blame] | 3070 | DWORD lastErrno = NO_ERROR; /* Value returned by GetLastError() */ |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3071 | |
mistachkin | d9d812f | 2014-12-20 22:21:49 | [diff] [blame] | 3072 | #if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED) |
mistachkin | 05340e3 | 2012-03-30 12:27:55 | [diff] [blame] | 3073 | memset(&overlapped, 0, sizeof(OVERLAPPED)); |
| 3074 | overlapped.Offset = (LONG)(offset & 0xffffffff); |
| 3075 | overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff); |
mistachkin | 00fa55d | 2012-03-30 16:44:33 | [diff] [blame] | 3076 | #endif |
mistachkin | 05340e3 | 2012-03-30 12:27:55 | [diff] [blame] | 3077 | |
drh | 5d9ef0a | 2011-07-11 18:17:56 | [diff] [blame] | 3078 | while( nRem>0 ){ |
mistachkin | d9d812f | 2014-12-20 22:21:49 | [diff] [blame] | 3079 | #if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED) |
mistachkin | 00fa55d | 2012-03-30 16:44:33 | [diff] [blame] | 3080 | if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){ |
| 3081 | #else |
mistachkin | 05340e3 | 2012-03-30 12:27:55 | [diff] [blame] | 3082 | if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){ |
mistachkin | 00fa55d | 2012-03-30 16:44:33 | [diff] [blame] | 3083 | #endif |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 3084 | if( winRetryIoerr(&nRetry, &lastErrno) ) continue; |
drh | 5d9ef0a | 2011-07-11 18:17:56 | [diff] [blame] | 3085 | break; |
| 3086 | } |
mistachkin | e1b461b | 2012-10-18 09:39:16 | [diff] [blame] | 3087 | assert( nWrite==0 || nWrite<=(DWORD)nRem ); |
| 3088 | if( nWrite==0 || nWrite>(DWORD)nRem ){ |
mistachkin | 05340e3 | 2012-03-30 12:27:55 | [diff] [blame] | 3089 | lastErrno = osGetLastError(); |
| 3090 | break; |
| 3091 | } |
mistachkin | d9d812f | 2014-12-20 22:21:49 | [diff] [blame] | 3092 | #if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED) |
mistachkin | 05340e3 | 2012-03-30 12:27:55 | [diff] [blame] | 3093 | offset += nWrite; |
| 3094 | overlapped.Offset = (LONG)(offset & 0xffffffff); |
| 3095 | overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff); |
mistachkin | 00fa55d | 2012-03-30 16:44:33 | [diff] [blame] | 3096 | #endif |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3097 | aRem += nWrite; |
| 3098 | nRem -= nWrite; |
| 3099 | } |
| 3100 | if( nRem>0 ){ |
mistachkin | d1ef9b6 | 2011-11-21 00:54:37 | [diff] [blame] | 3101 | pFile->lastErrno = lastErrno; |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3102 | rc = 1; |
shaneh | 133ce56 | 2010-07-06 20:33:47 | [diff] [blame] | 3103 | } |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 3104 | } |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3105 | |
| 3106 | if( rc ){ |
shaneh | eeb5d8a | 2011-06-20 20:48:09 | [diff] [blame] | 3107 | if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL ) |
| 3108 | || ( pFile->lastErrno==ERROR_DISK_FULL )){ |
mistachkin | ad1e55e | 2015-03-27 18:20:25 | [diff] [blame] | 3109 | OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n", |
| 3110 | osGetCurrentProcessId(), pFile, pFile->h)); |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 3111 | return winLogError(SQLITE_FULL, pFile->lastErrno, |
| 3112 | "winWrite1", pFile->zPath); |
shaneh | 133ce56 | 2010-07-06 20:33:47 | [diff] [blame] | 3113 | } |
mistachkin | ad1e55e | 2015-03-27 18:20:25 | [diff] [blame] | 3114 | OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_WRITE\n", |
| 3115 | osGetCurrentProcessId(), pFile, pFile->h)); |
mistachkin | 2aef997 | 2011-11-10 20:21:20 | [diff] [blame] | 3116 | return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno, |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 3117 | "winWrite2", pFile->zPath); |
drh | a32ad84 | 2011-07-12 13:51:05 | [diff] [blame] | 3118 | }else{ |
drh | 21aa6a1 | 2015-03-26 15:27:32 | [diff] [blame] | 3119 | winLogIoerr(nRetry, __LINE__); |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3120 | } |
mistachkin | ad1e55e | 2015-03-27 18:20:25 | [diff] [blame] | 3121 | OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n", |
| 3122 | osGetCurrentProcessId(), pFile, pFile->h)); |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3123 | return SQLITE_OK; |
| 3124 | } |
| 3125 | |
| 3126 | /* |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 3127 | ** Truncate the file opened by handle h to nByte bytes in size. |
| 3128 | */ |
| 3129 | static int winHandleTruncate(HANDLE h, sqlite3_int64 nByte){ |
| 3130 | int rc = SQLITE_OK; /* Return code */ |
| 3131 | rc = winHandleSeek(h, nByte); |
| 3132 | if( rc==SQLITE_OK ){ |
| 3133 | if( 0==osSetEndOfFile(h) ){ |
| 3134 | rc = SQLITE_IOERR_TRUNCATE; |
| 3135 | } |
| 3136 | } |
| 3137 | return rc; |
| 3138 | } |
| 3139 | |
| 3140 | /* |
| 3141 | ** Determine the size in bytes of the file opened by the handle passed as |
| 3142 | ** the first argument. |
| 3143 | */ |
| 3144 | static int winHandleSize(HANDLE h, sqlite3_int64 *pnByte){ |
| 3145 | int rc = SQLITE_OK; |
| 3146 | |
| 3147 | #if SQLITE_OS_WINRT |
| 3148 | FILE_STANDARD_INFO info; |
| 3149 | BOOL b; |
| 3150 | b = osGetFileInformationByHandleEx(h, FileStandardInfo, &info, sizeof(info)); |
| 3151 | if( b ){ |
| 3152 | *pnByte = info.EndOfFile.QuadPart; |
| 3153 | }else{ |
| 3154 | rc = SQLITE_IOERR_FSTAT; |
| 3155 | } |
| 3156 | #else |
| 3157 | DWORD upperBits = 0; |
| 3158 | DWORD lowerBits = 0; |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 3159 | |
dan | ce50282 | 2024-12-24 14:44:38 | [diff] [blame] | 3160 | assert( pnByte ); |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 3161 | lowerBits = osGetFileSize(h, &upperBits); |
| 3162 | *pnByte = (((sqlite3_int64)upperBits)<<32) + lowerBits; |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 3163 | if( lowerBits==INVALID_FILE_SIZE && osGetLastError()!=NO_ERROR ){ |
| 3164 | rc = SQLITE_IOERR_FSTAT; |
| 3165 | } |
| 3166 | #endif |
| 3167 | |
| 3168 | return rc; |
| 3169 | } |
| 3170 | |
| 3171 | /* |
dan | ce50282 | 2024-12-24 14:44:38 | [diff] [blame] | 3172 | ** Close the handle passed as the only argument. |
| 3173 | */ |
| 3174 | static void winHandleClose(HANDLE h){ |
| 3175 | if( h!=INVALID_HANDLE_VALUE ){ |
| 3176 | osCloseHandle(h); |
| 3177 | } |
| 3178 | } |
| 3179 | |
| 3180 | /* |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 3181 | ** Truncate an open file to a specified size |
drh | bbdc2b9 | 2005-09-19 12:53:18 | [diff] [blame] | 3182 | */ |
drh | c51250a | 2007-09-20 14:39:23 | [diff] [blame] | 3183 | static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){ |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3184 | winFile *pFile = (winFile*)id; /* File handle object */ |
| 3185 | int rc = SQLITE_OK; /* Return code for this function */ |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 3186 | DWORD lastErrno; |
mistachkin | 1e8487d | 2018-07-22 06:25:35 | [diff] [blame] | 3187 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 3188 | sqlite3_int64 oldMmapSize; |
drh | eafb9a0 | 2018-11-24 17:46:07 | [diff] [blame] | 3189 | if( pFile->nFetchOut>0 ){ |
| 3190 | /* File truncation is a no-op if there are outstanding memory mapped |
| 3191 | ** pages. This is because truncating the file means temporarily unmapping |
| 3192 | ** the file, and that might delete memory out from under existing cursors. |
| 3193 | ** |
| 3194 | ** This can result in incremental vacuum not truncating the file, |
| 3195 | ** if there is an active read cursor when the incremental vacuum occurs. |
| 3196 | ** No real harm comes of this - the database file is not corrupted, |
| 3197 | ** though some folks might complain that the file is bigger than it |
| 3198 | ** needs to be. |
| 3199 | ** |
| 3200 | ** The only feasible work-around is to defer the truncation until after |
| 3201 | ** all references to memory-mapped content are closed. That is doable, |
| 3202 | ** but involves adding a few branches in the common write code path which |
| 3203 | ** could slow down normal operations slightly. Hence, we have decided for |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3204 | ** now to simply make transactions a no-op if there are pending reads. We |
drh | eafb9a0 | 2018-11-24 17:46:07 | [diff] [blame] | 3205 | ** can maybe revisit this decision in the future. |
| 3206 | */ |
| 3207 | return SQLITE_OK; |
| 3208 | } |
mistachkin | 1e8487d | 2018-07-22 06:25:35 | [diff] [blame] | 3209 | #endif |
shane | 50daafc | 2009-03-05 05:54:55 | [diff] [blame] | 3210 | |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3211 | assert( pFile ); |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 3212 | SimulateIOError(return SQLITE_IOERR_TRUNCATE); |
mistachkin | ad1e55e | 2015-03-27 18:20:25 | [diff] [blame] | 3213 | OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n", |
| 3214 | osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype)); |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3215 | |
| 3216 | /* If the user has configured a chunk-size for this file, truncate the |
| 3217 | ** file so that it consists of an integer number of chunks (i.e. the |
| 3218 | ** actual file size after the operation may be larger than the requested |
| 3219 | ** size). |
| 3220 | */ |
mistachkin | d589a54 | 2011-08-30 01:23:34 | [diff] [blame] | 3221 | if( pFile->szChunk>0 ){ |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3222 | nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; |
| 3223 | } |
| 3224 | |
mistachkin | 1e8487d | 2018-07-22 06:25:35 | [diff] [blame] | 3225 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 3226 | if( pFile->pMapRegion ){ |
| 3227 | oldMmapSize = pFile->mmapSize; |
| 3228 | }else{ |
| 3229 | oldMmapSize = 0; |
| 3230 | } |
| 3231 | winUnmapfile(pFile); |
| 3232 | #endif |
| 3233 | |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3234 | /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */ |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 3235 | if( winSeekFile(pFile, nByte) ){ |
mistachkin | 2aef997 | 2011-11-10 20:21:20 | [diff] [blame] | 3236 | rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno, |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 3237 | "winTruncate1", pFile->zPath); |
| 3238 | }else if( 0==osSetEndOfFile(pFile->h) && |
| 3239 | ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){ |
| 3240 | pFile->lastErrno = lastErrno; |
mistachkin | 2aef997 | 2011-11-10 20:21:20 | [diff] [blame] | 3241 | rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno, |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 3242 | "winTruncate2", pFile->zPath); |
shane | a3465f2 | 2008-10-12 02:27:38 | [diff] [blame] | 3243 | } |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3244 | |
drh | 9b4c59f | 2013-04-15 17:03:42 | [diff] [blame] | 3245 | #if SQLITE_MAX_MMAP_SIZE>0 |
mistachkin | 1e8487d | 2018-07-22 06:25:35 | [diff] [blame] | 3246 | if( rc==SQLITE_OK && oldMmapSize>0 ){ |
| 3247 | if( oldMmapSize>nByte ){ |
| 3248 | winMapfile(pFile, -1); |
| 3249 | }else{ |
| 3250 | winMapfile(pFile, oldMmapSize); |
| 3251 | } |
shane | a3465f2 | 2008-10-12 02:27:38 | [diff] [blame] | 3252 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 | [diff] [blame] | 3253 | #endif |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3254 | |
mistachkin | ad1e55e | 2015-03-27 18:20:25 | [diff] [blame] | 3255 | OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, rc=%s\n", |
| 3256 | osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc))); |
shaneh | 04882a9 | 2010-05-11 02:49:39 | [diff] [blame] | 3257 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3258 | } |
| 3259 | |
drh | dec6fae | 2007-09-03 17:02:50 | [diff] [blame] | 3260 | #ifdef SQLITE_TEST |
| 3261 | /* |
| 3262 | ** Count the number of fullsyncs and normal syncs. This is used to test |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 3263 | ** that syncs and fullsyncs are occurring at the right times. |
drh | dec6fae | 2007-09-03 17:02:50 | [diff] [blame] | 3264 | */ |
| 3265 | int sqlite3_sync_count = 0; |
| 3266 | int sqlite3_fullsync_count = 0; |
| 3267 | #endif |
| 3268 | |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3269 | /* |
| 3270 | ** Make sure all writes to a particular file are committed to disk. |
| 3271 | */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 3272 | static int winSync(sqlite3_file *id, int flags){ |
mistachkin | c377f31 | 2011-09-16 20:43:44 | [diff] [blame] | 3273 | #ifndef SQLITE_NO_SYNC |
| 3274 | /* |
| 3275 | ** Used only when SQLITE_NO_SYNC is not defined. |
| 3276 | */ |
shaneh | 9dd6e08 | 2011-04-15 20:18:40 | [diff] [blame] | 3277 | BOOL rc; |
mistachkin | c377f31 | 2011-09-16 20:43:44 | [diff] [blame] | 3278 | #endif |
| 3279 | #if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \ |
mistachkin | fb383e9 | 2015-04-16 03:24:38 | [diff] [blame] | 3280 | defined(SQLITE_HAVE_OS_TRACE) |
mistachkin | c377f31 | 2011-09-16 20:43:44 | [diff] [blame] | 3281 | /* |
| 3282 | ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or |
| 3283 | ** OSTRACE() macros. |
| 3284 | */ |
| 3285 | winFile *pFile = (winFile*)id; |
shane | 18e526c | 2008-12-10 22:30:24 | [diff] [blame] | 3286 | #else |
| 3287 | UNUSED_PARAMETER(id); |
| 3288 | #endif |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 3289 | |
| 3290 | assert( pFile ); |
| 3291 | /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
| 3292 | assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 3293 | || (flags&0x0F)==SQLITE_SYNC_FULL |
| 3294 | ); |
| 3295 | |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 3296 | /* Unix cannot, but some systems may return SQLITE_FULL from here. This |
| 3297 | ** line is to test that doing so does not cause any problems. |
| 3298 | */ |
| 3299 | SimulateDiskfullError( return SQLITE_FULL ); |
shaneh | 9dd6e08 | 2011-04-15 20:18:40 | [diff] [blame] | 3300 | |
mistachkin | ad1e55e | 2015-03-27 18:20:25 | [diff] [blame] | 3301 | OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, flags=%x, lock=%d\n", |
| 3302 | osGetCurrentProcessId(), pFile, pFile->h, flags, |
| 3303 | pFile->locktype)); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3304 | |
shaneh | 9dd6e08 | 2011-04-15 20:18:40 | [diff] [blame] | 3305 | #ifndef SQLITE_TEST |
| 3306 | UNUSED_PARAMETER(flags); |
| 3307 | #else |
| 3308 | if( (flags&0x0F)==SQLITE_SYNC_FULL ){ |
| 3309 | sqlite3_fullsync_count++; |
| 3310 | } |
| 3311 | sqlite3_sync_count++; |
| 3312 | #endif |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 3313 | |
shane | 84ca383 | 2008-11-13 18:20:43 | [diff] [blame] | 3314 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
| 3315 | ** no-op |
| 3316 | */ |
| 3317 | #ifdef SQLITE_NO_SYNC |
mistachkin | ad1e55e | 2015-03-27 18:20:25 | [diff] [blame] | 3318 | OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n", |
| 3319 | osGetCurrentProcessId(), pFile, pFile->h)); |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 3320 | return SQLITE_OK; |
shane | 84ca383 | 2008-11-13 18:20:43 | [diff] [blame] | 3321 | #else |
mistachkin | ccb4371 | 2015-03-26 23:36:35 | [diff] [blame] | 3322 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 3323 | if( pFile->pMapRegion ){ |
| 3324 | if( osFlushViewOfFile(pFile->pMapRegion, 0) ){ |
| 3325 | OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, " |
| 3326 | "rc=SQLITE_OK\n", osGetCurrentProcessId(), |
| 3327 | pFile, pFile->pMapRegion)); |
| 3328 | }else{ |
| 3329 | pFile->lastErrno = osGetLastError(); |
| 3330 | OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, " |
| 3331 | "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), |
| 3332 | pFile, pFile->pMapRegion)); |
| 3333 | return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno, |
| 3334 | "winSync1", pFile->zPath); |
| 3335 | } |
| 3336 | } |
| 3337 | #endif |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 3338 | rc = osFlushFileBuffers(pFile->h); |
shaneh | 9dd6e08 | 2011-04-15 20:18:40 | [diff] [blame] | 3339 | SimulateIOError( rc=FALSE ); |
| 3340 | if( rc ){ |
mistachkin | ad1e55e | 2015-03-27 18:20:25 | [diff] [blame] | 3341 | OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n", |
| 3342 | osGetCurrentProcessId(), pFile, pFile->h)); |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3343 | return SQLITE_OK; |
| 3344 | }else{ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 3345 | pFile->lastErrno = osGetLastError(); |
mistachkin | ad1e55e | 2015-03-27 18:20:25 | [diff] [blame] | 3346 | OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_FSYNC\n", |
| 3347 | osGetCurrentProcessId(), pFile, pFile->h)); |
mistachkin | 2aef997 | 2011-11-10 20:21:20 | [diff] [blame] | 3348 | return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno, |
mistachkin | ccb4371 | 2015-03-26 23:36:35 | [diff] [blame] | 3349 | "winSync2", pFile->zPath); |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3350 | } |
shane | 84ca383 | 2008-11-13 18:20:43 | [diff] [blame] | 3351 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3352 | } |
| 3353 | |
| 3354 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3355 | ** Determine the current size of a file in bytes |
| 3356 | */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 3357 | static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){ |
| 3358 | winFile *pFile = (winFile*)id; |
drh | 24560d1 | 2012-03-01 22:44:56 | [diff] [blame] | 3359 | int rc = SQLITE_OK; |
shane | 50daafc | 2009-03-05 05:54:55 | [diff] [blame] | 3360 | |
| 3361 | assert( id!=0 ); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3362 | assert( pSize!=0 ); |
drh | 9cce710 | 2007-01-09 17:18:19 | [diff] [blame] | 3363 | SimulateIOError(return SQLITE_IOERR_FSTAT); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3364 | OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize)); |
| 3365 | |
drh | 24560d1 | 2012-03-01 22:44:56 | [diff] [blame] | 3366 | #if SQLITE_OS_WINRT |
shane | 9db299f | 2009-01-30 05:59:10 | [diff] [blame] | 3367 | { |
drh | 24560d1 | 2012-03-01 22:44:56 | [diff] [blame] | 3368 | FILE_STANDARD_INFO info; |
mistachkin | 75b70a2 | 2012-03-02 13:47:16 | [diff] [blame] | 3369 | if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo, |
drh | 24560d1 | 2012-03-01 22:44:56 | [diff] [blame] | 3370 | &info, sizeof(info)) ){ |
| 3371 | *pSize = info.EndOfFile.QuadPart; |
| 3372 | }else{ |
mistachkin | 75b70a2 | 2012-03-02 13:47:16 | [diff] [blame] | 3373 | pFile->lastErrno = osGetLastError(); |
drh | 24560d1 | 2012-03-01 22:44:56 | [diff] [blame] | 3374 | rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno, |
| 3375 | "winFileSize", pFile->zPath); |
| 3376 | } |
shane | 9db299f | 2009-01-30 05:59:10 | [diff] [blame] | 3377 | } |
drh | 24560d1 | 2012-03-01 22:44:56 | [diff] [blame] | 3378 | #else |
| 3379 | { |
| 3380 | DWORD upperBits; |
| 3381 | DWORD lowerBits; |
| 3382 | DWORD lastErrno; |
| 3383 | |
| 3384 | lowerBits = osGetFileSize(pFile->h, &upperBits); |
| 3385 | *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits; |
| 3386 | if( (lowerBits == INVALID_FILE_SIZE) |
| 3387 | && ((lastErrno = osGetLastError())!=NO_ERROR) ){ |
| 3388 | pFile->lastErrno = lastErrno; |
| 3389 | rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno, |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 3390 | "winFileSize", pFile->zPath); |
drh | 24560d1 | 2012-03-01 22:44:56 | [diff] [blame] | 3391 | } |
| 3392 | } |
| 3393 | #endif |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3394 | OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n", |
| 3395 | pFile->h, pSize, *pSize, sqlite3ErrName(rc))); |
drh | 24560d1 | 2012-03-01 22:44:56 | [diff] [blame] | 3396 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3397 | } |
| 3398 | |
| 3399 | /* |
drh | 602bbd3 | 2006-01-06 20:22:29 | [diff] [blame] | 3400 | ** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems. |
| 3401 | */ |
| 3402 | #ifndef LOCKFILE_FAIL_IMMEDIATELY |
| 3403 | # define LOCKFILE_FAIL_IMMEDIATELY 1 |
| 3404 | #endif |
| 3405 | |
mistachkin | 2a5cfb3 | 2012-03-02 22:38:49 | [diff] [blame] | 3406 | #ifndef LOCKFILE_EXCLUSIVE_LOCK |
| 3407 | # define LOCKFILE_EXCLUSIVE_LOCK 2 |
| 3408 | #endif |
| 3409 | |
| 3410 | /* |
| 3411 | ** Historically, SQLite has used both the LockFile and LockFileEx functions. |
| 3412 | ** When the LockFile function was used, it was always expected to fail |
| 3413 | ** immediately if the lock could not be obtained. Also, it always expected to |
| 3414 | ** obtain an exclusive lock. These flags are used with the LockFileEx function |
| 3415 | ** and reflect those expectations; therefore, they should not be changed. |
| 3416 | */ |
| 3417 | #ifndef SQLITE_LOCKFILE_FLAGS |
| 3418 | # define SQLITE_LOCKFILE_FLAGS (LOCKFILE_FAIL_IMMEDIATELY | \ |
| 3419 | LOCKFILE_EXCLUSIVE_LOCK) |
| 3420 | #endif |
| 3421 | |
| 3422 | /* |
| 3423 | ** Currently, SQLite never calls the LockFileEx function without wanting the |
| 3424 | ** call to fail immediately if the lock cannot be obtained. |
| 3425 | */ |
| 3426 | #ifndef SQLITE_LOCKFILEEX_FLAGS |
| 3427 | # define SQLITE_LOCKFILEEX_FLAGS (LOCKFILE_FAIL_IMMEDIATELY) |
| 3428 | #endif |
| 3429 | |
drh | 602bbd3 | 2006-01-06 20:22:29 | [diff] [blame] | 3430 | /* |
drh | 9c105bb | 2004-10-02 20:38:28 | [diff] [blame] | 3431 | ** Acquire a reader lock. |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3432 | ** Different API routines are called depending on whether or not this |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 | [diff] [blame] | 3433 | ** is Win9x or WinNT. |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3434 | */ |
dan | 2d87894 | 2025-02-10 20:46:14 | [diff] [blame] | 3435 | static int winGetReadLock(winFile *pFile, int bBlock){ |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3436 | int res; |
dan | 2d87894 | 2025-02-10 20:46:14 | [diff] [blame] | 3437 | DWORD mask = ~(bBlock ? LOCKFILE_FAIL_IMMEDIATELY : 0); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3438 | OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype)); |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 3439 | if( osIsNT() ){ |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 3440 | #if SQLITE_OS_WINCE |
| 3441 | /* |
| 3442 | ** NOTE: Windows CE is handled differently here due its lack of the Win32 |
| 3443 | ** API LockFileEx. |
| 3444 | */ |
| 3445 | res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0); |
| 3446 | #else |
dan | 2d87894 | 2025-02-10 20:46:14 | [diff] [blame] | 3447 | res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS&mask, SHARED_FIRST, 0, |
mistachkin | 2a5cfb3 | 2012-03-02 22:38:49 | [diff] [blame] | 3448 | SHARED_SIZE, 0); |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 3449 | #endif |
drh | d52ee72 | 2012-03-02 00:00:47 | [diff] [blame] | 3450 | } |
| 3451 | #ifdef SQLITE_WIN32_HAS_ANSI |
| 3452 | else{ |
drh | 9c105bb | 2004-10-02 20:38:28 | [diff] [blame] | 3453 | int lk; |
drh | 2fa1868 | 2008-03-19 14:15:34 | [diff] [blame] | 3454 | sqlite3_randomness(sizeof(lk), &lk); |
drh | 1bd10f8 | 2008-12-10 21:19:56 | [diff] [blame] | 3455 | pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1)); |
dan | 2d87894 | 2025-02-10 20:46:14 | [diff] [blame] | 3456 | res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS&mask, |
mistachkin | 2a5cfb3 | 2012-03-02 22:38:49 | [diff] [blame] | 3457 | SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0); |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3458 | } |
drh | d52ee72 | 2012-03-02 00:00:47 | [diff] [blame] | 3459 | #endif |
shane | 9db299f | 2009-01-30 05:59:10 | [diff] [blame] | 3460 | if( res == 0 ){ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 3461 | pFile->lastErrno = osGetLastError(); |
drh | 50990db | 2011-04-13 20:26:13 | [diff] [blame] | 3462 | /* No need to log a failure to lock */ |
shane | 9db299f | 2009-01-30 05:59:10 | [diff] [blame] | 3463 | } |
mistachkin | ff0bc8f | 2014-05-28 03:23:55 | [diff] [blame] | 3464 | OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res)); |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3465 | return res; |
| 3466 | } |
| 3467 | |
| 3468 | /* |
| 3469 | ** Undo a readlock |
| 3470 | */ |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 3471 | static int winUnlockReadLock(winFile *pFile){ |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3472 | int res; |
mistachkin | d1ef9b6 | 2011-11-21 00:54:37 | [diff] [blame] | 3473 | DWORD lastErrno; |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3474 | OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype)); |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 3475 | if( osIsNT() ){ |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 3476 | res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0); |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3477 | } |
drh | d52ee72 | 2012-03-02 00:00:47 | [diff] [blame] | 3478 | #ifdef SQLITE_WIN32_HAS_ANSI |
| 3479 | else{ |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 3480 | res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0); |
drh | d52ee72 | 2012-03-02 00:00:47 | [diff] [blame] | 3481 | } |
| 3482 | #endif |
mistachkin | d1ef9b6 | 2011-11-21 00:54:37 | [diff] [blame] | 3483 | if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){ |
| 3484 | pFile->lastErrno = lastErrno; |
mistachkin | 2aef997 | 2011-11-10 20:21:20 | [diff] [blame] | 3485 | winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno, |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 3486 | "winUnlockReadLock", pFile->zPath); |
shane | 9db299f | 2009-01-30 05:59:10 | [diff] [blame] | 3487 | } |
mistachkin | ff0bc8f | 2014-05-28 03:23:55 | [diff] [blame] | 3488 | OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res)); |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3489 | return res; |
| 3490 | } |
| 3491 | |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 | [diff] [blame] | 3492 | /* |
drh | b3e0434 | 2004-06-08 00:47:47 | [diff] [blame] | 3493 | ** Lock the file with the lock specified by parameter locktype - one |
| 3494 | ** of the following: |
| 3495 | ** |
| 3496 | ** (1) SHARED_LOCK |
| 3497 | ** (2) RESERVED_LOCK |
| 3498 | ** (3) PENDING_LOCK |
| 3499 | ** (4) EXCLUSIVE_LOCK |
| 3500 | ** |
| 3501 | ** Sometimes when requesting one lock state, additional lock states |
| 3502 | ** are inserted in between. The locking might fail on one of the later |
| 3503 | ** transitions leaving the lock state different from what it started but |
| 3504 | ** still short of its goal. The following chart shows the allowed |
| 3505 | ** transitions and the inserted intermediate states: |
| 3506 | ** |
| 3507 | ** UNLOCKED -> SHARED |
| 3508 | ** SHARED -> RESERVED |
| 3509 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 3510 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 3511 | ** PENDING -> EXCLUSIVE |
| 3512 | ** |
drh | 9c06c95 | 2005-11-26 00:25:00 | [diff] [blame] | 3513 | ** This routine will only increase a lock. The winUnlock() routine |
drh | b3e0434 | 2004-06-08 00:47:47 | [diff] [blame] | 3514 | ** erases all locks at once and returns us immediately to locking level 0. |
| 3515 | ** It is not possible to lower the locking level one step at a time. You |
| 3516 | ** must go straight to locking level 0. |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3517 | */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 3518 | static int winLock(sqlite3_file *id, int locktype){ |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3519 | int rc = SQLITE_OK; /* Return code from subroutines */ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 3520 | int res = 1; /* Result of a Windows lock call */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 3521 | int newLocktype; /* Set pFile->locktype to this value before exiting */ |
drh | e54ca3f | 2004-06-07 01:52:14 | [diff] [blame] | 3522 | int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */ |
drh | 054889e | 2005-11-30 03:20:31 | [diff] [blame] | 3523 | winFile *pFile = (winFile*)id; |
mistachkin | d1ef9b6 | 2011-11-21 00:54:37 | [diff] [blame] | 3524 | DWORD lastErrno = NO_ERROR; |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3525 | |
shane | 50daafc | 2009-03-05 05:54:55 | [diff] [blame] | 3526 | assert( id!=0 ); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3527 | OSTRACE(("LOCK file=%p, oldLock=%d(%d), newLock=%d\n", |
| 3528 | pFile->h, pFile->locktype, pFile->sharedLockByte, locktype)); |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3529 | |
| 3530 | /* If there is already a lock of this type or more restrictive on the |
| 3531 | ** OsFile, do nothing. Don't use the end_lock: exit path, as |
| 3532 | ** sqlite3OsEnterMutex() hasn't been called yet. |
| 3533 | */ |
drh | 054889e | 2005-11-30 03:20:31 | [diff] [blame] | 3534 | if( pFile->locktype>=locktype ){ |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3535 | OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h)); |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3536 | return SQLITE_OK; |
| 3537 | } |
| 3538 | |
drh | 275fe3a | 2015-05-28 00:54:35 | [diff] [blame] | 3539 | /* Do not allow any kind of write-lock on a read-only database |
| 3540 | */ |
| 3541 | if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){ |
| 3542 | return SQLITE_IOERR_LOCK; |
| 3543 | } |
| 3544 | |
drh | b3e0434 | 2004-06-08 00:47:47 | [diff] [blame] | 3545 | /* Make sure the locking sequence is correct |
| 3546 | */ |
drh | 054889e | 2005-11-30 03:20:31 | [diff] [blame] | 3547 | assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK ); |
drh | b3e0434 | 2004-06-08 00:47:47 | [diff] [blame] | 3548 | assert( locktype!=PENDING_LOCK ); |
drh | 054889e | 2005-11-30 03:20:31 | [diff] [blame] | 3549 | assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK ); |
drh | b3e0434 | 2004-06-08 00:47:47 | [diff] [blame] | 3550 | |
dan | 8bbce21 | 2025-02-12 17:21:24 | [diff] [blame] | 3551 | /* Lock the PENDING_LOCK byte if we need to acquire an EXCLUSIVE lock or |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3552 | ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of |
| 3553 | ** the PENDING_LOCK byte is temporary. |
| 3554 | */ |
drh | 054889e | 2005-11-30 03:20:31 | [diff] [blame] | 3555 | newLocktype = pFile->locktype; |
dan | 8bbce21 | 2025-02-12 17:21:24 | [diff] [blame] | 3556 | if( locktype==SHARED_LOCK |
| 3557 | || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK) |
drh | e54ca3f | 2004-06-07 01:52:14 | [diff] [blame] | 3558 | ){ |
drh | b3e0434 | 2004-06-08 00:47:47 | [diff] [blame] | 3559 | int cnt = 3; |
dan | 8bbce21 | 2025-02-12 17:21:24 | [diff] [blame] | 3560 | |
| 3561 | /* Flags for the LockFileEx() call. This should be an exclusive lock if |
| 3562 | ** this call is to obtain EXCLUSIVE, or a shared lock if this call is to |
| 3563 | ** obtain SHARED. */ |
| 3564 | int flags = LOCKFILE_FAIL_IMMEDIATELY; |
| 3565 | if( locktype==EXCLUSIVE_LOCK ){ |
| 3566 | flags |= LOCKFILE_EXCLUSIVE_LOCK; |
| 3567 | } |
| 3568 | while( cnt>0 ){ |
drh | d6ca4b9 | 2011-11-09 18:07:34 | [diff] [blame] | 3569 | /* Try 3 times to get the pending lock. This is needed to work |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 3570 | ** around problems caused by indexing and/or anti-virus software on |
| 3571 | ** Windows systems. |
dan | 8bbce21 | 2025-02-12 17:21:24 | [diff] [blame] | 3572 | ** |
drh | d6ca4b9 | 2011-11-09 18:07:34 | [diff] [blame] | 3573 | ** If you are using this code as a model for alternative VFSes, do not |
dan | 8bbce21 | 2025-02-12 17:21:24 | [diff] [blame] | 3574 | ** copy this retry logic. It is a hack intended for Windows only. */ |
| 3575 | res = winLockFile(&pFile->h, flags, PENDING_BYTE, 0, 1, 0); |
| 3576 | if( res ) break; |
| 3577 | |
mistachkin | 8e86b6a | 2014-05-28 03:27:42 | [diff] [blame] | 3578 | lastErrno = osGetLastError(); |
dan | 8bbce21 | 2025-02-12 17:21:24 | [diff] [blame] | 3579 | OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n", |
| 3580 | pFile->h, cnt, res |
| 3581 | )); |
| 3582 | |
mistachkin | 8e86b6a | 2014-05-28 03:27:42 | [diff] [blame] | 3583 | if( lastErrno==ERROR_INVALID_HANDLE ){ |
| 3584 | pFile->lastErrno = lastErrno; |
| 3585 | rc = SQLITE_IOERR_LOCK; |
dan | 8bbce21 | 2025-02-12 17:21:24 | [diff] [blame] | 3586 | OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n", |
| 3587 | pFile->h, cnt, sqlite3ErrName(rc) |
| 3588 | )); |
mistachkin | 8e86b6a | 2014-05-28 03:27:42 | [diff] [blame] | 3589 | return rc; |
| 3590 | } |
dan | 8bbce21 | 2025-02-12 17:21:24 | [diff] [blame] | 3591 | |
| 3592 | cnt--; |
| 3593 | if( cnt>0 ) sqlite3_win32_sleep(1); |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3594 | } |
drh | e54ca3f | 2004-06-07 01:52:14 | [diff] [blame] | 3595 | gotPendingLock = res; |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3596 | } |
| 3597 | |
| 3598 | /* Acquire a shared lock |
| 3599 | */ |
drh | b3e0434 | 2004-06-08 00:47:47 | [diff] [blame] | 3600 | if( locktype==SHARED_LOCK && res ){ |
drh | 054889e | 2005-11-30 03:20:31 | [diff] [blame] | 3601 | assert( pFile->locktype==NO_LOCK ); |
dan | f037440 | 2025-02-24 21:27:16 | [diff] [blame] | 3602 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
dan | 2d87894 | 2025-02-10 20:46:14 | [diff] [blame] | 3603 | res = winGetReadLock(pFile, pFile->bBlockOnConnect); |
dan | f037440 | 2025-02-24 21:27:16 | [diff] [blame] | 3604 | #else |
| 3605 | res = winGetReadLock(pFile, 0); |
| 3606 | #endif |
drh | e54ca3f | 2004-06-07 01:52:14 | [diff] [blame] | 3607 | if( res ){ |
| 3608 | newLocktype = SHARED_LOCK; |
shane | 9db299f | 2009-01-30 05:59:10 | [diff] [blame] | 3609 | }else{ |
mistachkin | d1ef9b6 | 2011-11-21 00:54:37 | [diff] [blame] | 3610 | lastErrno = osGetLastError(); |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3611 | } |
| 3612 | } |
| 3613 | |
| 3614 | /* Acquire a RESERVED lock |
| 3615 | */ |
drh | b3e0434 | 2004-06-08 00:47:47 | [diff] [blame] | 3616 | if( locktype==RESERVED_LOCK && res ){ |
drh | 054889e | 2005-11-30 03:20:31 | [diff] [blame] | 3617 | assert( pFile->locktype==SHARED_LOCK ); |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 3618 | res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0); |
drh | e54ca3f | 2004-06-07 01:52:14 | [diff] [blame] | 3619 | if( res ){ |
| 3620 | newLocktype = RESERVED_LOCK; |
shane | 9db299f | 2009-01-30 05:59:10 | [diff] [blame] | 3621 | }else{ |
mistachkin | d1ef9b6 | 2011-11-21 00:54:37 | [diff] [blame] | 3622 | lastErrno = osGetLastError(); |
drh | e54ca3f | 2004-06-07 01:52:14 | [diff] [blame] | 3623 | } |
| 3624 | } |
| 3625 | |
| 3626 | /* Acquire a PENDING lock |
| 3627 | */ |
drh | b3e0434 | 2004-06-08 00:47:47 | [diff] [blame] | 3628 | if( locktype==EXCLUSIVE_LOCK && res ){ |
drh | e54ca3f | 2004-06-07 01:52:14 | [diff] [blame] | 3629 | newLocktype = PENDING_LOCK; |
| 3630 | gotPendingLock = 0; |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3631 | } |
| 3632 | |
| 3633 | /* Acquire an EXCLUSIVE lock |
| 3634 | */ |
drh | e54ca3f | 2004-06-07 01:52:14 | [diff] [blame] | 3635 | if( locktype==EXCLUSIVE_LOCK && res ){ |
drh | 054889e | 2005-11-30 03:20:31 | [diff] [blame] | 3636 | assert( pFile->locktype>=SHARED_LOCK ); |
drh | 56a4107 | 2023-06-16 14:39:21 | [diff] [blame] | 3637 | (void)winUnlockReadLock(pFile); |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 3638 | res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0, |
mistachkin | 2a5cfb3 | 2012-03-02 22:38:49 | [diff] [blame] | 3639 | SHARED_SIZE, 0); |
drh | e54ca3f | 2004-06-07 01:52:14 | [diff] [blame] | 3640 | if( res ){ |
| 3641 | newLocktype = EXCLUSIVE_LOCK; |
| 3642 | }else{ |
mistachkin | d1ef9b6 | 2011-11-21 00:54:37 | [diff] [blame] | 3643 | lastErrno = osGetLastError(); |
dan | 2d87894 | 2025-02-10 20:46:14 | [diff] [blame] | 3644 | winGetReadLock(pFile, 0); |
drh | e54ca3f | 2004-06-07 01:52:14 | [diff] [blame] | 3645 | } |
| 3646 | } |
| 3647 | |
| 3648 | /* If we are holding a PENDING lock that ought to be released, then |
| 3649 | ** release it now. |
| 3650 | */ |
drh | b3e0434 | 2004-06-08 00:47:47 | [diff] [blame] | 3651 | if( gotPendingLock && locktype==SHARED_LOCK ){ |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 3652 | winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0); |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3653 | } |
| 3654 | |
| 3655 | /* Update the state of the lock has held in the file descriptor then |
| 3656 | ** return the appropriate result code. |
| 3657 | */ |
| 3658 | if( res ){ |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3659 | rc = SQLITE_OK; |
| 3660 | }else{ |
mistachkin | d1ef9b6 | 2011-11-21 00:54:37 | [diff] [blame] | 3661 | pFile->lastErrno = lastErrno; |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3662 | rc = SQLITE_BUSY; |
mistachkin | 533fb6d | 2013-08-28 02:26:48 | [diff] [blame] | 3663 | OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n", |
| 3664 | pFile->h, locktype, newLocktype)); |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3665 | } |
drh | 1bd10f8 | 2008-12-10 21:19:56 | [diff] [blame] | 3666 | pFile->locktype = (u8)newLocktype; |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3667 | OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n", |
| 3668 | pFile->h, pFile->locktype, sqlite3ErrName(rc))); |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3669 | return rc; |
| 3670 | } |
| 3671 | |
| 3672 | /* |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3673 | ** This routine checks if there is a RESERVED lock held on the specified |
| 3674 | ** file by this or any other process. If such a lock is held, return |
| 3675 | ** non-zero, otherwise zero. |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3676 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 | [diff] [blame] | 3677 | static int winCheckReservedLock(sqlite3_file *id, int *pResOut){ |
mistachkin | ff0bc8f | 2014-05-28 03:23:55 | [diff] [blame] | 3678 | int res; |
drh | 054889e | 2005-11-30 03:20:31 | [diff] [blame] | 3679 | winFile *pFile = (winFile*)id; |
shane | 50daafc | 2009-03-05 05:54:55 | [diff] [blame] | 3680 | |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 3681 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3682 | OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut)); |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 3683 | |
shane | 50daafc | 2009-03-05 05:54:55 | [diff] [blame] | 3684 | assert( id!=0 ); |
drh | 054889e | 2005-11-30 03:20:31 | [diff] [blame] | 3685 | if( pFile->locktype>=RESERVED_LOCK ){ |
mistachkin | ff0bc8f | 2014-05-28 03:23:55 | [diff] [blame] | 3686 | res = 1; |
| 3687 | OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res)); |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3688 | }else{ |
drh | 62aaa6c | 2015-11-21 17:27:42 | [diff] [blame] | 3689 | res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0); |
mistachkin | ff0bc8f | 2014-05-28 03:23:55 | [diff] [blame] | 3690 | if( res ){ |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 3691 | winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0); |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3692 | } |
mistachkin | ff0bc8f | 2014-05-28 03:23:55 | [diff] [blame] | 3693 | res = !res; |
| 3694 | OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res)); |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3695 | } |
mistachkin | ff0bc8f | 2014-05-28 03:23:55 | [diff] [blame] | 3696 | *pResOut = res; |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3697 | OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n", |
| 3698 | pFile->h, pResOut, *pResOut)); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 | [diff] [blame] | 3699 | return SQLITE_OK; |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3700 | } |
| 3701 | |
| 3702 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 | [diff] [blame] | 3703 | ** Lower the locking level on file descriptor id to locktype. locktype |
| 3704 | ** must be either NO_LOCK or SHARED_LOCK. |
| 3705 | ** |
| 3706 | ** If the locking level of the file descriptor is already at or below |
| 3707 | ** the requested locking level, this routine is a no-op. |
| 3708 | ** |
drh | 9c105bb | 2004-10-02 20:38:28 | [diff] [blame] | 3709 | ** It is not possible for this routine to fail if the second argument |
| 3710 | ** is NO_LOCK. If the second argument is SHARED_LOCK then this routine |
| 3711 | ** might return SQLITE_IOERR; |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3712 | */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 3713 | static int winUnlock(sqlite3_file *id, int locktype){ |
drh | 9c105bb | 2004-10-02 20:38:28 | [diff] [blame] | 3714 | int type; |
drh | 054889e | 2005-11-30 03:20:31 | [diff] [blame] | 3715 | winFile *pFile = (winFile*)id; |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 3716 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 | [diff] [blame] | 3717 | assert( pFile!=0 ); |
drh | a6abd04 | 2004-06-09 17:37:22 | [diff] [blame] | 3718 | assert( locktype<=SHARED_LOCK ); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3719 | OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n", |
| 3720 | pFile->h, pFile->locktype, pFile->sharedLockByte, locktype)); |
drh | 054889e | 2005-11-30 03:20:31 | [diff] [blame] | 3721 | type = pFile->locktype; |
drh | e54ca3f | 2004-06-07 01:52:14 | [diff] [blame] | 3722 | if( type>=EXCLUSIVE_LOCK ){ |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 3723 | winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0); |
dan | 41f2980 | 2025-02-12 08:07:10 | [diff] [blame] | 3724 | if( locktype==SHARED_LOCK && !winGetReadLock(pFile, 0) ){ |
drh | 9c105bb | 2004-10-02 20:38:28 | [diff] [blame] | 3725 | /* This should never happen. We should always be able to |
| 3726 | ** reacquire the read lock */ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 3727 | rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(), |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 3728 | "winUnlock", pFile->zPath); |
drh | 9c105bb | 2004-10-02 20:38:28 | [diff] [blame] | 3729 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3730 | } |
drh | e54ca3f | 2004-06-07 01:52:14 | [diff] [blame] | 3731 | if( type>=RESERVED_LOCK ){ |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 3732 | winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0); |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3733 | } |
drh | 9c105bb | 2004-10-02 20:38:28 | [diff] [blame] | 3734 | if( locktype==NO_LOCK && type>=SHARED_LOCK ){ |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 3735 | winUnlockReadLock(pFile); |
drh | 51c6d96 | 2004-06-06 00:42:25 | [diff] [blame] | 3736 | } |
drh | b3e0434 | 2004-06-08 00:47:47 | [diff] [blame] | 3737 | if( type>=PENDING_LOCK ){ |
mistachkin | a749486 | 2012-04-18 05:57:38 | [diff] [blame] | 3738 | winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0); |
drh | b3e0434 | 2004-06-08 00:47:47 | [diff] [blame] | 3739 | } |
drh | 1bd10f8 | 2008-12-10 21:19:56 | [diff] [blame] | 3740 | pFile->locktype = (u8)locktype; |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3741 | OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n", |
| 3742 | pFile->h, pFile->locktype, sqlite3ErrName(rc))); |
drh | 9c105bb | 2004-10-02 20:38:28 | [diff] [blame] | 3743 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3744 | } |
| 3745 | |
mistachkin | 1e75483 | 2016-07-08 21:14:37 | [diff] [blame] | 3746 | /****************************************************************************** |
| 3747 | ****************************** No-op Locking ********************************** |
| 3748 | ** |
| 3749 | ** Of the various locking implementations available, this is by far the |
| 3750 | ** simplest: locking is ignored. No attempt is made to lock the database |
| 3751 | ** file for reading or writing. |
| 3752 | ** |
| 3753 | ** This locking mode is appropriate for use on read-only databases |
| 3754 | ** (ex: databases that are burned into CD-ROM, for example.) It can |
| 3755 | ** also be used if the application employs some external mechanism to |
| 3756 | ** prevent simultaneous access of the same database by two or more |
| 3757 | ** database connections. But there is a serious risk of database |
| 3758 | ** corruption if this locking mode is used in situations where multiple |
| 3759 | ** database connections are accessing the same database file at the same |
| 3760 | ** time and one or more of those connections are writing. |
| 3761 | */ |
| 3762 | |
| 3763 | static int winNolockLock(sqlite3_file *id, int locktype){ |
| 3764 | UNUSED_PARAMETER(id); |
| 3765 | UNUSED_PARAMETER(locktype); |
| 3766 | return SQLITE_OK; |
| 3767 | } |
| 3768 | |
| 3769 | static int winNolockCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 3770 | UNUSED_PARAMETER(id); |
| 3771 | UNUSED_PARAMETER(pResOut); |
| 3772 | return SQLITE_OK; |
| 3773 | } |
| 3774 | |
| 3775 | static int winNolockUnlock(sqlite3_file *id, int locktype){ |
| 3776 | UNUSED_PARAMETER(id); |
| 3777 | UNUSED_PARAMETER(locktype); |
| 3778 | return SQLITE_OK; |
| 3779 | } |
| 3780 | |
| 3781 | /******************* End of the no-op lock implementation ********************* |
| 3782 | ******************************************************************************/ |
| 3783 | |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 3784 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 3785 | ** If *pArg is initially negative then this is a query. Set *pArg to |
drh | f12b3f6 | 2011-12-21 14:42:29 | [diff] [blame] | 3786 | ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set. |
| 3787 | ** |
| 3788 | ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags. |
| 3789 | */ |
| 3790 | static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){ |
| 3791 | if( *pArg<0 ){ |
| 3792 | *pArg = (pFile->ctrlFlags & mask)!=0; |
| 3793 | }else if( (*pArg)==0 ){ |
| 3794 | pFile->ctrlFlags &= ~mask; |
| 3795 | }else{ |
| 3796 | pFile->ctrlFlags |= mask; |
| 3797 | } |
| 3798 | } |
| 3799 | |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 3800 | /* Forward references to VFS helper methods used for temporary files */ |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 3801 | static int winGetTempname(sqlite3_vfs *, char **); |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 3802 | static int winIsDir(const void *); |
mistachkin | a8e41ec | 2020-05-15 01:18:07 | [diff] [blame] | 3803 | static BOOL winIsLongPathPrefix(const char *); |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 3804 | static BOOL winIsDriveLetterAndColon(const char *); |
drh | 696b33e | 2012-12-06 19:01:42 | [diff] [blame] | 3805 | |
drh | f12b3f6 | 2011-12-21 14:42:29 | [diff] [blame] | 3806 | /* |
drh | 9e33c2c | 2007-08-31 18:34:59 | [diff] [blame] | 3807 | ** Control and query of the open file handle. |
drh | 0ccebe7 | 2005-06-07 22:22:50 | [diff] [blame] | 3808 | */ |
drh | 9e33c2c | 2007-08-31 18:34:59 | [diff] [blame] | 3809 | static int winFileControl(sqlite3_file *id, int op, void *pArg){ |
drh | f0b190d | 2011-07-26 16:03:07 | [diff] [blame] | 3810 | winFile *pFile = (winFile*)id; |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3811 | OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg)); |
drh | 9e33c2c | 2007-08-31 18:34:59 | [diff] [blame] | 3812 | switch( op ){ |
| 3813 | case SQLITE_FCNTL_LOCKSTATE: { |
drh | f0b190d | 2011-07-26 16:03:07 | [diff] [blame] | 3814 | *(int*)pArg = pFile->locktype; |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3815 | OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); |
drh | 9e33c2c | 2007-08-31 18:34:59 | [diff] [blame] | 3816 | return SQLITE_OK; |
| 3817 | } |
mistachkin | b7b9106 | 2016-03-15 19:10:39 | [diff] [blame] | 3818 | case SQLITE_FCNTL_LAST_ERRNO: { |
drh | f0b190d | 2011-07-26 16:03:07 | [diff] [blame] | 3819 | *(int*)pArg = (int)pFile->lastErrno; |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3820 | OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); |
shane | 9db299f | 2009-01-30 05:59:10 | [diff] [blame] | 3821 | return SQLITE_OK; |
| 3822 | } |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3823 | case SQLITE_FCNTL_CHUNK_SIZE: { |
drh | f0b190d | 2011-07-26 16:03:07 | [diff] [blame] | 3824 | pFile->szChunk = *(int *)pArg; |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3825 | OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); |
dan | 502019c | 2010-07-28 14:26:17 | [diff] [blame] | 3826 | return SQLITE_OK; |
| 3827 | } |
drh | f5d6e47 | 2010-05-19 19:39:26 | [diff] [blame] | 3828 | case SQLITE_FCNTL_SIZE_HINT: { |
mistachkin | d589a54 | 2011-08-30 01:23:34 | [diff] [blame] | 3829 | if( pFile->szChunk>0 ){ |
mistachkin | d589a54 | 2011-08-30 01:23:34 | [diff] [blame] | 3830 | sqlite3_int64 oldSz; |
| 3831 | int rc = winFileSize(id, &oldSz); |
| 3832 | if( rc==SQLITE_OK ){ |
| 3833 | sqlite3_int64 newSz = *(sqlite3_int64*)pArg; |
| 3834 | if( newSz>oldSz ){ |
| 3835 | SimulateIOErrorBenign(1); |
| 3836 | rc = winTruncate(id, newSz); |
| 3837 | SimulateIOErrorBenign(0); |
| 3838 | } |
mistachkin | 4458bc8 | 2011-08-25 01:16:42 | [diff] [blame] | 3839 | } |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3840 | OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc))); |
mistachkin | d589a54 | 2011-08-30 01:23:34 | [diff] [blame] | 3841 | return rc; |
mistachkin | 4458bc8 | 2011-08-25 01:16:42 | [diff] [blame] | 3842 | } |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3843 | OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); |
mistachkin | d589a54 | 2011-08-30 01:23:34 | [diff] [blame] | 3844 | return SQLITE_OK; |
drh | f5d6e47 | 2010-05-19 19:39:26 | [diff] [blame] | 3845 | } |
drh | f0b190d | 2011-07-26 16:03:07 | [diff] [blame] | 3846 | case SQLITE_FCNTL_PERSIST_WAL: { |
drh | f12b3f6 | 2011-12-21 14:42:29 | [diff] [blame] | 3847 | winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3848 | OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); |
drh | f12b3f6 | 2011-12-21 14:42:29 | [diff] [blame] | 3849 | return SQLITE_OK; |
| 3850 | } |
drh | cb15f35 | 2011-12-23 01:04:17 | [diff] [blame] | 3851 | case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { |
| 3852 | winModeBit(pFile, WINFILE_PSOW, (int*)pArg); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3853 | OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); |
drh | f0b190d | 2011-07-26 16:03:07 | [diff] [blame] | 3854 | return SQLITE_OK; |
| 3855 | } |
drh | de60fc2 | 2011-12-14 17:53:36 | [diff] [blame] | 3856 | case SQLITE_FCNTL_VFSNAME: { |
mistachkin | 4013875 | 2013-12-09 21:48:49 | [diff] [blame] | 3857 | *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3858 | OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); |
drh | de60fc2 | 2011-12-14 17:53:36 | [diff] [blame] | 3859 | return SQLITE_OK; |
| 3860 | } |
drh | d0cdf01 | 2011-07-13 16:03:46 | [diff] [blame] | 3861 | case SQLITE_FCNTL_WIN32_AV_RETRY: { |
| 3862 | int *a = (int*)pArg; |
| 3863 | if( a[0]>0 ){ |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 3864 | winIoerrRetry = a[0]; |
drh | d0cdf01 | 2011-07-13 16:03:46 | [diff] [blame] | 3865 | }else{ |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 3866 | a[0] = winIoerrRetry; |
drh | d0cdf01 | 2011-07-13 16:03:46 | [diff] [blame] | 3867 | } |
| 3868 | if( a[1]>0 ){ |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 3869 | winIoerrRetryDelay = a[1]; |
drh | d0cdf01 | 2011-07-13 16:03:46 | [diff] [blame] | 3870 | }else{ |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 3871 | a[1] = winIoerrRetryDelay; |
drh | d0cdf01 | 2011-07-13 16:03:46 | [diff] [blame] | 3872 | } |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3873 | OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); |
drh | d0cdf01 | 2011-07-13 16:03:46 | [diff] [blame] | 3874 | return SQLITE_OK; |
| 3875 | } |
mistachkin | 1b361ff | 2016-05-03 19:36:54 | [diff] [blame] | 3876 | case SQLITE_FCNTL_WIN32_GET_HANDLE: { |
| 3877 | LPHANDLE phFile = (LPHANDLE)pArg; |
| 3878 | *phFile = pFile->h; |
| 3879 | OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); |
| 3880 | return SQLITE_OK; |
| 3881 | } |
mistachkin | 6b98d67 | 2014-05-30 16:42:35 | [diff] [blame] | 3882 | #ifdef SQLITE_TEST |
| 3883 | case SQLITE_FCNTL_WIN32_SET_HANDLE: { |
| 3884 | LPHANDLE phFile = (LPHANDLE)pArg; |
| 3885 | HANDLE hOldFile = pFile->h; |
| 3886 | pFile->h = *phFile; |
| 3887 | *phFile = hOldFile; |
| 3888 | OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n", |
| 3889 | hOldFile, pFile->h)); |
| 3890 | return SQLITE_OK; |
| 3891 | } |
| 3892 | #endif |
drh | 1b37bc0 | 2024-11-13 14:58:35 | [diff] [blame] | 3893 | case SQLITE_FCNTL_NULL_IO: { |
| 3894 | (void)osCloseHandle(pFile->h); |
| 3895 | pFile->h = NULL; |
| 3896 | return SQLITE_OK; |
| 3897 | } |
drh | 696b33e | 2012-12-06 19:01:42 | [diff] [blame] | 3898 | case SQLITE_FCNTL_TEMPFILENAME: { |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 3899 | char *zTFile = 0; |
| 3900 | int rc = winGetTempname(pFile->pVfs, &zTFile); |
| 3901 | if( rc==SQLITE_OK ){ |
drh | 696b33e | 2012-12-06 19:01:42 | [diff] [blame] | 3902 | *(char**)pArg = zTFile; |
| 3903 | } |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 3904 | OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc))); |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 3905 | return rc; |
drh | 696b33e | 2012-12-06 19:01:42 | [diff] [blame] | 3906 | } |
mistachkin | 5824e05 | 2013-04-15 20:08:27 | [diff] [blame] | 3907 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 9b4c59f | 2013-04-15 17:03:42 | [diff] [blame] | 3908 | case SQLITE_FCNTL_MMAP_SIZE: { |
drh | 34f7490 | 2013-04-03 13:09:18 | [diff] [blame] | 3909 | i64 newLimit = *(i64*)pArg; |
drh | 34e258c | 2013-05-23 01:40:53 | [diff] [blame] | 3910 | int rc = SQLITE_OK; |
drh | 9b4c59f | 2013-04-15 17:03:42 | [diff] [blame] | 3911 | if( newLimit>sqlite3GlobalConfig.mxMmap ){ |
| 3912 | newLimit = sqlite3GlobalConfig.mxMmap; |
| 3913 | } |
mistachkin | e35395a | 2017-08-07 19:06:54 | [diff] [blame] | 3914 | |
| 3915 | /* The value of newLimit may be eventually cast to (SIZE_T) and passed |
| 3916 | ** to MapViewOfFile(). Restrict its value to 2GB if (SIZE_T) is not at |
| 3917 | ** least a 64-bit type. */ |
| 3918 | if( newLimit>0 && sizeof(SIZE_T)<8 ){ |
| 3919 | newLimit = (newLimit & 0x7FFFFFFF); |
| 3920 | } |
| 3921 | |
drh | 9b4c59f | 2013-04-15 17:03:42 | [diff] [blame] | 3922 | *(i64*)pArg = pFile->mmapSizeMax; |
drh | 34e258c | 2013-05-23 01:40:53 | [diff] [blame] | 3923 | if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){ |
| 3924 | pFile->mmapSizeMax = newLimit; |
| 3925 | if( pFile->mmapSize>0 ){ |
mistachkin | c216566 | 2013-10-16 09:49:10 | [diff] [blame] | 3926 | winUnmapfile(pFile); |
drh | 34e258c | 2013-05-23 01:40:53 | [diff] [blame] | 3927 | rc = winMapfile(pFile, -1); |
| 3928 | } |
| 3929 | } |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 3930 | OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc))); |
drh | 34e258c | 2013-05-23 01:40:53 | [diff] [blame] | 3931 | return rc; |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 3932 | } |
mistachkin | 5824e05 | 2013-04-15 20:08:27 | [diff] [blame] | 3933 | #endif |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 3934 | |
| 3935 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 3936 | case SQLITE_FCNTL_LOCK_TIMEOUT: { |
| 3937 | int iOld = pFile->iBusyTimeout; |
dan | 4628888 | 2025-01-30 15:26:16 | [diff] [blame] | 3938 | int iNew = *(int*)pArg; |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 3939 | #if SQLITE_ENABLE_SETLK_TIMEOUT==1 |
dan | 4628888 | 2025-01-30 15:26:16 | [diff] [blame] | 3940 | pFile->iBusyTimeout = (iNew < 0) ? INFINITE : (DWORD)iNew; |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 3941 | #elif SQLITE_ENABLE_SETLK_TIMEOUT==2 |
dan | 4628888 | 2025-01-30 15:26:16 | [diff] [blame] | 3942 | pFile->iBusyTimeout = (DWORD)(!!iNew); |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 3943 | #else |
| 3944 | # error "SQLITE_ENABLE_SETLK_TIMEOUT must be set to 1 or 2" |
| 3945 | #endif |
| 3946 | *(int*)pArg = iOld; |
| 3947 | return SQLITE_OK; |
| 3948 | } |
dan | 2d87894 | 2025-02-10 20:46:14 | [diff] [blame] | 3949 | case SQLITE_FCNTL_BLOCK_ON_CONNECT: { |
| 3950 | int iNew = *(int*)pArg; |
| 3951 | pFile->bBlockOnConnect = iNew; |
| 3952 | return SQLITE_OK; |
| 3953 | } |
| 3954 | #endif /* SQLITE_ENABLE_SETLK_TIMEOUT */ |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 3955 | |
drh | 9e33c2c | 2007-08-31 18:34:59 | [diff] [blame] | 3956 | } |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 3957 | OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h)); |
drh | 0b52b7d | 2011-01-26 19:46:22 | [diff] [blame] | 3958 | return SQLITE_NOTFOUND; |
drh | 9cbe635 | 2005-11-29 03:13:21 | [diff] [blame] | 3959 | } |
| 3960 | |
| 3961 | /* |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 | [diff] [blame] | 3962 | ** Return the sector size in bytes of the underlying block device for |
| 3963 | ** the specified file. This is almost always 512 bytes, but may be |
| 3964 | ** larger for some devices. |
| 3965 | ** |
| 3966 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 3967 | ** if two files are created in the same file-system directory (i.e. |
drh | 85b623f | 2007-12-13 21:54:09 | [diff] [blame] | 3968 | ** a database and its journal file) that the sector size will be the |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 | [diff] [blame] | 3969 | ** same for both. |
| 3970 | */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 3971 | static int winSectorSize(sqlite3_file *id){ |
drh | 8942d41 | 2012-01-02 18:20:14 | [diff] [blame] | 3972 | (void)id; |
| 3973 | return SQLITE_DEFAULT_SECTOR_SIZE; |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 | [diff] [blame] | 3974 | } |
| 3975 | |
| 3976 | /* |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 3977 | ** Return a vector of device characteristics. |
drh | 9c06c95 | 2005-11-26 00:25:00 | [diff] [blame] | 3978 | */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 3979 | static int winDeviceCharacteristics(sqlite3_file *id){ |
drh | f12b3f6 | 2011-12-21 14:42:29 | [diff] [blame] | 3980 | winFile *p = (winFile*)id; |
drh | 96501c8 | 2024-10-22 18:26:03 | [diff] [blame] | 3981 | return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN | SQLITE_IOCAP_SUBPAGE_READ | |
drh | cb15f35 | 2011-12-23 01:04:17 | [diff] [blame] | 3982 | ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0); |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 3983 | } |
| 3984 | |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 3985 | /* |
shaneh | 420398c | 2010-09-03 04:29:30 | [diff] [blame] | 3986 | ** Windows will only let you create file view mappings |
| 3987 | ** on allocation size granularity boundaries. |
| 3988 | ** During sqlite3_os_init() we do a GetSystemInfo() |
| 3989 | ** to get the granularity size. |
| 3990 | */ |
mistachkin | 5dfd3d9 | 2014-02-05 11:05:47 | [diff] [blame] | 3991 | static SYSTEM_INFO winSysInfo; |
shaneh | 420398c | 2010-09-03 04:29:30 | [diff] [blame] | 3992 | |
mistachkin | 6e8752d | 2013-05-16 12:41:49 | [diff] [blame] | 3993 | #ifndef SQLITE_OMIT_WAL |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 3994 | |
| 3995 | /* |
| 3996 | ** Helper functions to obtain and relinquish the global mutex. The |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 3997 | ** global mutex is used to protect the winLockInfo objects used by |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 3998 | ** this file, all of which may be shared by multiple threads. |
| 3999 | ** |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 4000 | ** Function winShmMutexHeld() is used to assert() that the global mutex |
| 4001 | ** is held when required. This function is only used as part of assert() |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4002 | ** statements. e.g. |
| 4003 | ** |
| 4004 | ** winShmEnterMutex() |
| 4005 | ** assert( winShmMutexHeld() ); |
shaneh | d5a7240 | 2010-07-20 20:23:38 | [diff] [blame] | 4006 | ** winShmLeaveMutex() |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4007 | */ |
mistachkin | 435666e | 2018-02-05 20:42:50 | [diff] [blame] | 4008 | static sqlite3_mutex *winBigLock = 0; |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4009 | static void winShmEnterMutex(void){ |
mistachkin | 435666e | 2018-02-05 20:42:50 | [diff] [blame] | 4010 | sqlite3_mutex_enter(winBigLock); |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4011 | } |
| 4012 | static void winShmLeaveMutex(void){ |
mistachkin | 435666e | 2018-02-05 20:42:50 | [diff] [blame] | 4013 | sqlite3_mutex_leave(winBigLock); |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4014 | } |
mistachkin | 866b53e | 2014-01-14 10:17:02 | [diff] [blame] | 4015 | #ifndef NDEBUG |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4016 | static int winShmMutexHeld(void) { |
mistachkin | 435666e | 2018-02-05 20:42:50 | [diff] [blame] | 4017 | return sqlite3_mutex_held(winBigLock); |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4018 | } |
| 4019 | #endif |
| 4020 | |
| 4021 | /* |
| 4022 | ** Object used to represent a single file opened and mmapped to provide |
| 4023 | ** shared memory. When multiple threads all reference the same |
| 4024 | ** log-summary, each thread has its own winFile object, but they all |
| 4025 | ** point to a single instance of this object. In other words, each |
| 4026 | ** log-summary is opened only once per process. |
| 4027 | ** |
| 4028 | ** winShmMutexHeld() must be true when creating or destroying |
| 4029 | ** this object or while reading or writing the following fields: |
| 4030 | ** |
| 4031 | ** nRef |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 4032 | ** pNext |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4033 | ** |
| 4034 | ** The following fields are read-only after the object is created: |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 4035 | ** |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4036 | ** zFilename |
| 4037 | ** |
| 4038 | ** Either winShmNode.mutex must be held or winShmNode.nRef==0 and |
| 4039 | ** winShmMutexHeld() is true when reading or writing any other field |
| 4040 | ** in this structure. |
| 4041 | ** |
dan | d50eb9c | 2024-12-10 19:00:07 | [diff] [blame] | 4042 | ** File-handle hSharedShm is used to (a) take the DMS lock, (b) truncate |
| 4043 | ** the *-shm file if the DMS-locking protocol demands it, and (c) map |
| 4044 | ** regions of the *-shm file into memory using MapViewOfFile() or |
| 4045 | ** similar. Other locks are taken by individual clients using the |
| 4046 | ** winShm.hShm handles. |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4047 | */ |
| 4048 | struct winShmNode { |
| 4049 | sqlite3_mutex *mutex; /* Mutex to access this object */ |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4050 | char *zFilename; /* Name of the file */ |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4051 | HANDLE hSharedShm; /* File handle open on zFilename */ |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4052 | |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4053 | int isUnlocked; /* DMS lock has not yet been obtained */ |
| 4054 | int isReadonly; /* True if read-only */ |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4055 | int szRegion; /* Size of shared-memory regions */ |
| 4056 | int nRegion; /* Size of array apRegion */ |
mistachkin | 4ff8431 | 2017-11-09 16:30:55 | [diff] [blame] | 4057 | |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4058 | struct ShmRegion { |
| 4059 | HANDLE hMap; /* File handle from CreateFileMapping */ |
| 4060 | void *pMap; |
| 4061 | } *aRegion; |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4062 | DWORD lastErrno; /* The Windows errno from the last I/O error */ |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4063 | |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4064 | int nRef; /* Number of winShm objects pointing to this */ |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4065 | winShmNode *pNext; /* Next in list of all winShmNode objects */ |
mistachkin | fb383e9 | 2015-04-16 03:24:38 | [diff] [blame] | 4066 | #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4067 | u8 nextShmId; /* Next available winShm.id value */ |
| 4068 | #endif |
| 4069 | }; |
| 4070 | |
| 4071 | /* |
| 4072 | ** A global array of all winShmNode objects. |
| 4073 | ** |
| 4074 | ** The winShmMutexHeld() must be true while reading or writing this list. |
| 4075 | */ |
| 4076 | static winShmNode *winShmNodeList = 0; |
| 4077 | |
| 4078 | /* |
| 4079 | ** Structure used internally by this VFS to record the state of an |
dan | d50eb9c | 2024-12-10 19:00:07 | [diff] [blame] | 4080 | ** open shared memory connection. There is one such structure for each |
| 4081 | ** winFile open on a wal mode database. |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4082 | */ |
| 4083 | struct winShm { |
| 4084 | winShmNode *pShmNode; /* The underlying winShmNode object */ |
shaneh | 1f3e27b | 2010-07-12 20:46:33 | [diff] [blame] | 4085 | u16 sharedMask; /* Mask of shared locks held */ |
| 4086 | u16 exclMask; /* Mask of exclusive locks held */ |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4087 | HANDLE hShm; /* File-handle on *-shm file. For locking. */ |
| 4088 | int bReadonly; /* True if hShm is opened read-only */ |
mistachkin | fb383e9 | 2015-04-16 03:24:38 | [diff] [blame] | 4089 | #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4090 | u8 id; /* Id of this connection with its winShmNode */ |
| 4091 | #endif |
| 4092 | }; |
| 4093 | |
| 4094 | /* |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4095 | ** Constants used for locking |
| 4096 | */ |
drh | bd9676c | 2010-06-23 17:58:38 | [diff] [blame] | 4097 | #define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
drh | 20e1f08 | 2010-05-31 16:10:12 | [diff] [blame] | 4098 | #define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4099 | |
drh | 05cb5b2 | 2010-06-03 18:02:48 | [diff] [blame] | 4100 | /* Forward references to VFS methods */ |
| 4101 | static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*); |
| 4102 | static int winDelete(sqlite3_vfs *,const char*,int); |
| 4103 | |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4104 | /* |
| 4105 | ** Purge the winShmNodeList list of all entries with winShmNode.nRef==0. |
| 4106 | ** |
| 4107 | ** This is not a VFS shared-memory method; it is a utility function called |
| 4108 | ** by VFS shared-memory methods. |
| 4109 | */ |
drh | 05cb5b2 | 2010-06-03 18:02:48 | [diff] [blame] | 4110 | static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){ |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4111 | winShmNode **pp; |
| 4112 | winShmNode *p; |
| 4113 | assert( winShmMutexHeld() ); |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 4114 | OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n", |
| 4115 | osGetCurrentProcessId(), deleteFlag)); |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4116 | pp = &winShmNodeList; |
| 4117 | while( (p = *pp)!=0 ){ |
| 4118 | if( p->nRef==0 ){ |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4119 | int i; |
drh | 0353ed2 | 2013-10-16 11:31:51 | [diff] [blame] | 4120 | if( p->mutex ){ sqlite3_mutex_free(p->mutex); } |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4121 | for(i=0; i<p->nRegion; i++){ |
mistachkin | 36ca535 | 2013-09-12 01:47:57 | [diff] [blame] | 4122 | BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap); |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 4123 | OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n", |
| 4124 | osGetCurrentProcessId(), i, bRc ? "ok" : "failed")); |
mistachkin | 36ca535 | 2013-09-12 01:47:57 | [diff] [blame] | 4125 | UNUSED_VARIABLE_VALUE(bRc); |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 4126 | bRc = osCloseHandle(p->aRegion[i].hMap); |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 4127 | OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n", |
| 4128 | osGetCurrentProcessId(), i, bRc ? "ok" : "failed")); |
mistachkin | 36ca535 | 2013-09-12 01:47:57 | [diff] [blame] | 4129 | UNUSED_VARIABLE_VALUE(bRc); |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4130 | } |
dan | ce50282 | 2024-12-24 14:44:38 | [diff] [blame] | 4131 | winHandleClose(p->hSharedShm); |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 4132 | if( deleteFlag ){ |
| 4133 | SimulateIOErrorBenign(1); |
drh | 92c45cf | 2012-01-10 00:24:59 | [diff] [blame] | 4134 | sqlite3BeginBenignMalloc(); |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 4135 | winDelete(pVfs, p->zFilename, 0); |
drh | 92c45cf | 2012-01-10 00:24:59 | [diff] [blame] | 4136 | sqlite3EndBenignMalloc(); |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 4137 | SimulateIOErrorBenign(0); |
| 4138 | } |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4139 | *pp = p->pNext; |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4140 | sqlite3_free(p->aRegion); |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4141 | sqlite3_free(p); |
| 4142 | }else{ |
| 4143 | pp = &p->pNext; |
| 4144 | } |
| 4145 | } |
| 4146 | } |
| 4147 | |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4148 | /* |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4149 | ** The DMS lock has not yet been taken on the shm file associated with |
| 4150 | ** pShmNode. Take the lock. Truncate the *-shm file if required. |
| 4151 | ** Return SQLITE_OK if successful, or an SQLite error code otherwise. |
mistachkin | 4ff8431 | 2017-11-09 16:30:55 | [diff] [blame] | 4152 | */ |
dan | 4628888 | 2025-01-30 15:26:16 | [diff] [blame] | 4153 | static int winLockSharedMemory(winShmNode *pShmNode, DWORD nMs){ |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4154 | HANDLE h = pShmNode->hSharedShm; |
| 4155 | int rc = SQLITE_OK; |
| 4156 | |
dan | d50eb9c | 2024-12-10 19:00:07 | [diff] [blame] | 4157 | assert( sqlite3_mutex_held(pShmNode->mutex) ); |
| 4158 | rc = winHandleLockTimeout(h, WIN_SHM_DMS, 1, 1, 0); |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4159 | if( rc==SQLITE_OK ){ |
| 4160 | /* We have an EXCLUSIVE lock on the DMS byte. This means that this |
| 4161 | ** is the first process to open the file. Truncate it to zero bytes |
| 4162 | ** in this case. */ |
| 4163 | if( pShmNode->isReadonly ){ |
| 4164 | rc = SQLITE_READONLY_CANTINIT; |
| 4165 | }else{ |
| 4166 | rc = winHandleTruncate(h, 0); |
| 4167 | } |
| 4168 | |
| 4169 | /* Release the EXCLUSIVE lock acquired above. */ |
| 4170 | winUnlockFile(&h, WIN_SHM_DMS, 0, 1, 0); |
| 4171 | }else if( (rc & 0xFF)==SQLITE_BUSY ){ |
| 4172 | rc = SQLITE_OK; |
| 4173 | } |
mistachkin | 0e026f4 | 2017-11-09 23:24:29 | [diff] [blame] | 4174 | |
mistachkin | 7b7f224 | 2017-11-09 22:23:50 | [diff] [blame] | 4175 | if( rc==SQLITE_OK ){ |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4176 | /* Take a SHARED lock on the DMS byte. */ |
dan | d50eb9c | 2024-12-10 19:00:07 | [diff] [blame] | 4177 | rc = winHandleLockTimeout(h, WIN_SHM_DMS, 1, 0, nMs); |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4178 | if( rc==SQLITE_OK ){ |
| 4179 | pShmNode->isUnlocked = 0; |
mistachkin | 4ff8431 | 2017-11-09 16:30:55 | [diff] [blame] | 4180 | } |
mistachkin | 4ff8431 | 2017-11-09 16:30:55 | [diff] [blame] | 4181 | } |
| 4182 | |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4183 | return rc; |
| 4184 | } |
mistachkin | 0e026f4 | 2017-11-09 23:24:29 | [diff] [blame] | 4185 | |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4186 | |
| 4187 | /* |
| 4188 | ** Convert a UTF-8 filename into whatever form the underlying |
| 4189 | ** operating system wants filenames in. Space to hold the result |
| 4190 | ** is obtained from malloc and must be freed by the calling |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 4191 | ** function |
| 4192 | ** |
| 4193 | ** On Cygwin, 3 possible input forms are accepted: |
| 4194 | ** - If the filename starts with "<drive>:/" or "<drive>:\", |
| 4195 | ** it is converted to UTF-16 as-is. |
| 4196 | ** - If the filename contains '/', it is assumed to be a |
| 4197 | ** Cygwin absolute path, it is converted to a win32 |
| 4198 | ** absolute path in UTF-16. |
| 4199 | ** - Otherwise it must be a filename only, the win32 filename |
| 4200 | ** is returned in UTF-16. |
| 4201 | ** Note: If the function cygwin_conv_path() fails, only |
| 4202 | ** UTF-8 -> UTF-16 conversion will be done. This can only |
| 4203 | ** happen when the file path >32k, in which case winUtf8ToUnicode() |
| 4204 | ** will fail too. |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4205 | */ |
| 4206 | static void *winConvertFromUtf8Filename(const char *zFilename){ |
| 4207 | void *zConverted = 0; |
| 4208 | if( osIsNT() ){ |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 4209 | #ifdef __CYGWIN__ |
| 4210 | int nChar; |
| 4211 | LPWSTR zWideFilename; |
| 4212 | |
| 4213 | if( osCygwin_conv_path && !(winIsDriveLetterAndColon(zFilename) |
| 4214 | && winIsDirSep(zFilename[2])) ){ |
drh | 4a0b7a3 | 2025-05-19 14:50:36 | [diff] [blame] | 4215 | i64 nByte; |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 4216 | int convertflag = CCP_POSIX_TO_WIN_W; |
| 4217 | if( !strchr(zFilename, '/') ) convertflag |= CCP_RELATIVE; |
drh | 4a0b7a3 | 2025-05-19 14:50:36 | [diff] [blame] | 4218 | nByte = (i64)osCygwin_conv_path(convertflag, |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 4219 | zFilename, 0, 0); |
| 4220 | if( nByte>0 ){ |
drh | 4a0b7a3 | 2025-05-19 14:50:36 | [diff] [blame] | 4221 | zConverted = sqlite3MallocZero(12+(u64)nByte); |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 4222 | if ( zConverted==0 ){ |
| 4223 | return zConverted; |
| 4224 | } |
| 4225 | zWideFilename = zConverted; |
| 4226 | /* Filenames should be prefixed, except when converted |
| 4227 | * full path already starts with "\\?\". */ |
| 4228 | if( osCygwin_conv_path(convertflag, zFilename, |
| 4229 | zWideFilename+4, nByte)==0 ){ |
| 4230 | if( (convertflag&CCP_RELATIVE) ){ |
| 4231 | memmove(zWideFilename, zWideFilename+4, nByte); |
| 4232 | }else if( memcmp(zWideFilename+4, L"\\\\", 4) ){ |
| 4233 | memcpy(zWideFilename, L"\\\\?\\", 8); |
| 4234 | }else if( zWideFilename[6]!='?' ){ |
| 4235 | memmove(zWideFilename+6, zWideFilename+4, nByte); |
| 4236 | memcpy(zWideFilename, L"\\\\?\\UNC", 14); |
| 4237 | }else{ |
| 4238 | memmove(zWideFilename, zWideFilename+4, nByte); |
| 4239 | } |
| 4240 | return zConverted; |
| 4241 | } |
| 4242 | sqlite3_free(zConverted); |
| 4243 | } |
| 4244 | } |
| 4245 | nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0); |
| 4246 | if( nChar==0 ){ |
| 4247 | return 0; |
| 4248 | } |
| 4249 | zWideFilename = sqlite3MallocZero( nChar*sizeof(WCHAR)+12 ); |
| 4250 | if( zWideFilename==0 ){ |
| 4251 | return 0; |
| 4252 | } |
| 4253 | nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, |
| 4254 | zWideFilename, nChar); |
| 4255 | if( nChar==0 ){ |
| 4256 | sqlite3_free(zWideFilename); |
| 4257 | zWideFilename = 0; |
| 4258 | }else if( nChar>MAX_PATH |
| 4259 | && winIsDriveLetterAndColon(zFilename) |
| 4260 | && winIsDirSep(zFilename[2]) ){ |
| 4261 | memmove(zWideFilename+4, zWideFilename, nChar*sizeof(WCHAR)); |
| 4262 | zWideFilename[2] = '\\'; |
| 4263 | memcpy(zWideFilename, L"\\\\?\\", 8); |
| 4264 | }else if( nChar>MAX_PATH |
| 4265 | && winIsDirSep(zFilename[0]) && winIsDirSep(zFilename[1]) |
| 4266 | && zFilename[2] != '?' ){ |
| 4267 | memmove(zWideFilename+6, zWideFilename, nChar*sizeof(WCHAR)); |
| 4268 | memcpy(zWideFilename, L"\\\\?\\UNC", 14); |
| 4269 | } |
| 4270 | zConverted = zWideFilename; |
| 4271 | #else |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4272 | zConverted = winUtf8ToUnicode(zFilename); |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 4273 | #endif /* __CYGWIN__ */ |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4274 | } |
stephan | 7b9407a | 2025-03-07 06:54:04 | [diff] [blame] | 4275 | #if defined(SQLITE_WIN32_HAS_ANSI) && defined(_WIN32) |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4276 | else{ |
| 4277 | zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI()); |
| 4278 | } |
| 4279 | #endif |
| 4280 | /* caller will handle out of memory */ |
| 4281 | return zConverted; |
mistachkin | 4ff8431 | 2017-11-09 16:30:55 | [diff] [blame] | 4282 | } |
| 4283 | |
| 4284 | /* |
dan | 4ab343c | 2024-12-09 16:01:28 | [diff] [blame] | 4285 | ** This function is used to open a handle on a *-shm file. |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4286 | ** |
dan | ce50282 | 2024-12-24 14:44:38 | [diff] [blame] | 4287 | ** If SQLITE_ENABLE_SETLK_TIMEOUT is defined at build time, then the file |
| 4288 | ** is opened with FILE_FLAG_OVERLAPPED specified. If not, it is not. |
dan | 4ab343c | 2024-12-09 16:01:28 | [diff] [blame] | 4289 | */ |
| 4290 | static int winHandleOpen( |
| 4291 | const char *zUtf8, /* File to open */ |
| 4292 | int *pbReadonly, /* IN/OUT: True for readonly handle */ |
| 4293 | HANDLE *ph /* OUT: New HANDLE for file */ |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4294 | ){ |
| 4295 | int rc = SQLITE_OK; |
| 4296 | void *zConverted = 0; |
| 4297 | int bReadonly = *pbReadonly; |
| 4298 | HANDLE h = INVALID_HANDLE_VALUE; |
| 4299 | |
dan | ce50282 | 2024-12-24 14:44:38 | [diff] [blame] | 4300 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 4301 | const DWORD flag_overlapped = FILE_FLAG_OVERLAPPED; |
| 4302 | #else |
| 4303 | const DWORD flag_overlapped = 0; |
| 4304 | #endif |
| 4305 | |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4306 | /* Convert the filename to the system encoding. */ |
| 4307 | zConverted = winConvertFromUtf8Filename(zUtf8); |
| 4308 | if( zConverted==0 ){ |
| 4309 | OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8)); |
| 4310 | rc = SQLITE_IOERR_NOMEM_BKPT; |
| 4311 | goto winopenfile_out; |
mistachkin | 0e026f4 | 2017-11-09 23:24:29 | [diff] [blame] | 4312 | } |
| 4313 | |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4314 | /* Ensure the file we are trying to open is not actually a directory. */ |
| 4315 | if( winIsDir(zConverted) ){ |
| 4316 | OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8)); |
| 4317 | rc = SQLITE_CANTOPEN_ISDIR; |
| 4318 | goto winopenfile_out; |
| 4319 | } |
| 4320 | |
| 4321 | /* TODO: platforms. |
| 4322 | ** TODO: retry-on-ioerr. |
| 4323 | */ |
dan | 4ab343c | 2024-12-09 16:01:28 | [diff] [blame] | 4324 | if( osIsNT() ){ |
dan | 9d59235 | 2024-12-02 20:48:17 | [diff] [blame] | 4325 | #if SQLITE_OS_WINRT |
dan | 9d59235 | 2024-12-02 20:48:17 | [diff] [blame] | 4326 | CREATEFILE2_EXTENDED_PARAMETERS extendedParameters; |
| 4327 | memset(&extendedParameters, 0, sizeof(extendedParameters)); |
| 4328 | extendedParameters.dwSize = sizeof(extendedParameters); |
| 4329 | extendedParameters.dwFileAttributes = FILE_ATTRIBUTE_NORMAL; |
dan | ce50282 | 2024-12-24 14:44:38 | [diff] [blame] | 4330 | extendedParameters.dwFileFlags = flag_overlapped; |
dan | 9d59235 | 2024-12-02 20:48:17 | [diff] [blame] | 4331 | extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS; |
| 4332 | h = osCreateFile2((LPCWSTR)zConverted, |
| 4333 | (GENERIC_READ | (bReadonly ? 0 : GENERIC_WRITE)),/* dwDesiredAccess */ |
| 4334 | FILE_SHARE_READ | FILE_SHARE_WRITE, /* dwShareMode */ |
| 4335 | OPEN_ALWAYS, /* dwCreationDisposition */ |
| 4336 | &extendedParameters |
| 4337 | ); |
dan | 9d59235 | 2024-12-02 20:48:17 | [diff] [blame] | 4338 | #else |
dan | 4ab343c | 2024-12-09 16:01:28 | [diff] [blame] | 4339 | h = osCreateFileW((LPCWSTR)zConverted, /* lpFileName */ |
| 4340 | (GENERIC_READ | (bReadonly ? 0 : GENERIC_WRITE)), /* dwDesiredAccess */ |
| 4341 | FILE_SHARE_READ | FILE_SHARE_WRITE, /* dwShareMode */ |
| 4342 | NULL, /* lpSecurityAttributes */ |
| 4343 | OPEN_ALWAYS, /* dwCreationDisposition */ |
dan | ce50282 | 2024-12-24 14:44:38 | [diff] [blame] | 4344 | FILE_ATTRIBUTE_NORMAL|flag_overlapped, |
dan | 4ab343c | 2024-12-09 16:01:28 | [diff] [blame] | 4345 | NULL |
| 4346 | ); |
dan | 9d59235 | 2024-12-02 20:48:17 | [diff] [blame] | 4347 | #endif |
dan | 4ab343c | 2024-12-09 16:01:28 | [diff] [blame] | 4348 | }else{ |
| 4349 | /* Due to pre-processor directives earlier in this file, |
| 4350 | ** SQLITE_WIN32_HAS_ANSI is always defined if osIsNT() is false. */ |
| 4351 | #ifdef SQLITE_WIN32_HAS_ANSI |
| 4352 | h = osCreateFileA((LPCSTR)zConverted, |
| 4353 | (GENERIC_READ | (bReadonly ? 0 : GENERIC_WRITE)), /* dwDesiredAccess */ |
| 4354 | FILE_SHARE_READ | FILE_SHARE_WRITE, /* dwShareMode */ |
| 4355 | NULL, /* lpSecurityAttributes */ |
| 4356 | OPEN_ALWAYS, /* dwCreationDisposition */ |
dan | ce50282 | 2024-12-24 14:44:38 | [diff] [blame] | 4357 | FILE_ATTRIBUTE_NORMAL|flag_overlapped, |
dan | 4ab343c | 2024-12-09 16:01:28 | [diff] [blame] | 4358 | NULL |
| 4359 | ); |
| 4360 | #endif |
| 4361 | } |
| 4362 | |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4363 | if( h==INVALID_HANDLE_VALUE ){ |
| 4364 | if( bReadonly==0 ){ |
| 4365 | bReadonly = 1; |
dan | 4ab343c | 2024-12-09 16:01:28 | [diff] [blame] | 4366 | rc = winHandleOpen(zUtf8, &bReadonly, &h); |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4367 | }else{ |
| 4368 | rc = SQLITE_CANTOPEN_BKPT; |
| 4369 | } |
| 4370 | } |
| 4371 | |
| 4372 | winopenfile_out: |
| 4373 | sqlite3_free(zConverted); |
| 4374 | *pbReadonly = bReadonly; |
| 4375 | *ph = h; |
| 4376 | return rc; |
mistachkin | 4ff8431 | 2017-11-09 16:30:55 | [diff] [blame] | 4377 | } |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4378 | |
mistachkin | 4ff8431 | 2017-11-09 16:30:55 | [diff] [blame] | 4379 | |
| 4380 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 | [diff] [blame] | 4381 | ** Open the shared-memory area associated with database file pDbFd. |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4382 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 | [diff] [blame] | 4383 | static int winOpenSharedMemory(winFile *pDbFd){ |
mistachkin | 1081371 | 2017-11-09 22:25:58 | [diff] [blame] | 4384 | struct winShm *p; /* The connection to be opened */ |
| 4385 | winShmNode *pShmNode = 0; /* The underlying mmapped file */ |
| 4386 | int rc = SQLITE_OK; /* Result code */ |
mistachkin | 1081371 | 2017-11-09 22:25:58 | [diff] [blame] | 4387 | winShmNode *pNew; /* Newly allocated winShmNode */ |
| 4388 | int nName; /* Size of zName in bytes */ |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4389 | |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4390 | assert( pDbFd->pShm==0 ); /* Not previously opened */ |
| 4391 | |
| 4392 | /* Allocate space for the new sqlite3_shm object. Also speculatively |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4393 | ** allocate space for a new winShmNode and filename. */ |
mistachkin | 7ea11af | 2012-09-13 15:24:29 | [diff] [blame] | 4394 | p = sqlite3MallocZero( sizeof(*p) ); |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 4395 | if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT; |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4396 | nName = sqlite3Strlen30(pDbFd->zPath); |
drh | ef86b94 | 2025-02-17 17:33:14 | [diff] [blame] | 4397 | pNew = sqlite3MallocZero( sizeof(*pShmNode) + (i64)nName + 17 ); |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4398 | if( pNew==0 ){ |
| 4399 | sqlite3_free(p); |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 4400 | return SQLITE_IOERR_NOMEM_BKPT; |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4401 | } |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4402 | pNew->zFilename = (char*)&pNew[1]; |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4403 | pNew->hSharedShm = INVALID_HANDLE_VALUE; |
| 4404 | pNew->isUnlocked = 1; |
drh | d36f660 | 2010-06-25 12:52:47 | [diff] [blame] | 4405 | sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath); |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 4406 | sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename); |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4407 | |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4408 | /* Open a file-handle on the *-shm file for this connection. This file-handle |
dan | 4ab343c | 2024-12-09 16:01:28 | [diff] [blame] | 4409 | ** is only used for locking. The mapping of the *-shm file is created using |
| 4410 | ** the shared file handle in winShmNode.hSharedShm. */ |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4411 | p->bReadonly = sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0); |
dan | 4ab343c | 2024-12-09 16:01:28 | [diff] [blame] | 4412 | rc = winHandleOpen(pNew->zFilename, &p->bReadonly, &p->hShm); |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4413 | |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4414 | /* Look to see if there is an existing winShmNode that can be used. |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4415 | ** If no matching winShmNode currently exists, then create a new one. */ |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4416 | winShmEnterMutex(); |
| 4417 | for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){ |
| 4418 | /* TBD need to come up with better match here. Perhaps |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4419 | ** use FILE_ID_BOTH_DIR_INFO Structure. */ |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4420 | if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break; |
| 4421 | } |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4422 | if( pShmNode==0 ){ |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4423 | pShmNode = pNew; |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4424 | |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4425 | /* Allocate a mutex for this winShmNode object, if one is required. */ |
drh | 97a7e5e | 2016-04-26 18:58:54 | [diff] [blame] | 4426 | if( sqlite3GlobalConfig.bCoreMutex ){ |
| 4427 | pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4428 | if( pShmNode->mutex==0 ) rc = SQLITE_IOERR_NOMEM_BKPT; |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4429 | } |
shaneh | 420398c | 2010-09-03 04:29:30 | [diff] [blame] | 4430 | |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4431 | /* Open a file-handle to use for mappings, and for the DMS lock. */ |
| 4432 | if( rc==SQLITE_OK ){ |
| 4433 | HANDLE h = INVALID_HANDLE_VALUE; |
| 4434 | pShmNode->isReadonly = p->bReadonly; |
dan | 4ab343c | 2024-12-09 16:01:28 | [diff] [blame] | 4435 | rc = winHandleOpen(pNew->zFilename, &pShmNode->isReadonly, &h); |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4436 | pShmNode->hSharedShm = h; |
| 4437 | } |
| 4438 | |
| 4439 | /* If successful, link the new winShmNode into the global list. If an |
| 4440 | ** error occurred, free the object. */ |
| 4441 | if( rc==SQLITE_OK ){ |
| 4442 | pShmNode->pNext = winShmNodeList; |
| 4443 | winShmNodeList = pShmNode; |
| 4444 | pNew = 0; |
mistachkin | 98e2cb8 | 2018-01-17 01:40:57 | [diff] [blame] | 4445 | }else{ |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4446 | sqlite3_mutex_free(pShmNode->mutex); |
| 4447 | if( pShmNode->hSharedShm!=INVALID_HANDLE_VALUE ){ |
| 4448 | osCloseHandle(pShmNode->hSharedShm); |
drh | 97a7e5e | 2016-04-26 18:58:54 | [diff] [blame] | 4449 | } |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4450 | } |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4451 | } |
| 4452 | |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4453 | /* If no error has occurred, link the winShm object to the winShmNode and |
| 4454 | ** the winShm to pDbFd. */ |
| 4455 | if( rc==SQLITE_OK ){ |
| 4456 | p->pShmNode = pShmNode; |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4457 | pShmNode->nRef++; |
mistachkin | fb383e9 | 2015-04-16 03:24:38 | [diff] [blame] | 4458 | #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4459 | p->id = pShmNode->nextShmId++; |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4460 | #endif |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4461 | pDbFd->pShm = p; |
dan | 800bf8f | 2024-12-16 15:13:34 | [diff] [blame] | 4462 | }else if( p ){ |
dan | ce50282 | 2024-12-24 14:44:38 | [diff] [blame] | 4463 | winHandleClose(p->hShm); |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4464 | sqlite3_free(p); |
| 4465 | } |
| 4466 | |
| 4467 | assert( rc!=SQLITE_OK || pShmNode->isUnlocked==0 || pShmNode->nRegion==0 ); |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4468 | winShmLeaveMutex(); |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4469 | sqlite3_free(pNew); |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4470 | return rc; |
| 4471 | } |
| 4472 | |
| 4473 | /* |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 4474 | ** Close a connection to shared-memory. Delete the underlying |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4475 | ** storage if deleteFlag is true. |
| 4476 | */ |
drh | e11fedc | 2010-07-14 00:14:30 | [diff] [blame] | 4477 | static int winShmUnmap( |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4478 | sqlite3_file *fd, /* Database holding shared memory */ |
| 4479 | int deleteFlag /* Delete after closing if true */ |
| 4480 | ){ |
| 4481 | winFile *pDbFd; /* Database holding shared-memory */ |
| 4482 | winShm *p; /* The connection to be closed */ |
| 4483 | winShmNode *pShmNode; /* The underlying shared-memory file */ |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4484 | |
| 4485 | pDbFd = (winFile*)fd; |
| 4486 | p = pDbFd->pShm; |
shaneh | 1f3e27b | 2010-07-12 20:46:33 | [diff] [blame] | 4487 | if( p==0 ) return SQLITE_OK; |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4488 | if( p->hShm!=INVALID_HANDLE_VALUE ){ |
| 4489 | osCloseHandle(p->hShm); |
| 4490 | } |
| 4491 | |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4492 | pShmNode = p->pShmNode; |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4493 | winShmEnterMutex(); |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4494 | |
| 4495 | /* If pShmNode->nRef has reached 0, then close the underlying |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4496 | ** shared-memory file, too. */ |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4497 | assert( pShmNode->nRef>0 ); |
| 4498 | pShmNode->nRef--; |
| 4499 | if( pShmNode->nRef==0 ){ |
drh | 05cb5b2 | 2010-06-03 18:02:48 | [diff] [blame] | 4500 | winShmPurge(pDbFd->pVfs, deleteFlag); |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4501 | } |
| 4502 | winShmLeaveMutex(); |
| 4503 | |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4504 | /* Free the connection p */ |
| 4505 | sqlite3_free(p); |
| 4506 | pDbFd->pShm = 0; |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4507 | return SQLITE_OK; |
| 4508 | } |
| 4509 | |
| 4510 | /* |
shaneh | 1f3e27b | 2010-07-12 20:46:33 | [diff] [blame] | 4511 | ** Change the lock state for a shared-memory segment. |
| 4512 | */ |
| 4513 | static int winShmLock( |
| 4514 | sqlite3_file *fd, /* Database file holding the shared memory */ |
| 4515 | int ofst, /* First lock to acquire or release */ |
| 4516 | int n, /* Number of locks to acquire or release */ |
| 4517 | int flags /* What to do with the lock */ |
| 4518 | ){ |
| 4519 | winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */ |
| 4520 | winShm *p = pDbFd->pShm; /* The shared memory being locked */ |
drh | 56d88aa | 2022-03-22 19:41:55 | [diff] [blame] | 4521 | winShmNode *pShmNode; |
shaneh | 1f3e27b | 2010-07-12 20:46:33 | [diff] [blame] | 4522 | int rc = SQLITE_OK; /* Result code */ |
dan | 362d51a | 2024-12-26 16:10:15 | [diff] [blame] | 4523 | u16 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst)); /* Mask of locks to [un]take */ |
shaneh | 1f3e27b | 2010-07-12 20:46:33 | [diff] [blame] | 4524 | |
drh | 56d88aa | 2022-03-22 19:41:55 | [diff] [blame] | 4525 | if( p==0 ) return SQLITE_IOERR_SHMLOCK; |
| 4526 | pShmNode = p->pShmNode; |
| 4527 | if( NEVER(pShmNode==0) ) return SQLITE_IOERR_SHMLOCK; |
| 4528 | |
shaneh | 1f3e27b | 2010-07-12 20:46:33 | [diff] [blame] | 4529 | assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); |
| 4530 | assert( n>=1 ); |
| 4531 | assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) |
| 4532 | || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) |
| 4533 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) |
| 4534 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 4535 | assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
| 4536 | |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 4537 | /* Check that, if this to be a blocking lock, no locks that occur later |
| 4538 | ** in the following list than the lock being obtained are already held: |
| 4539 | ** |
dan | 420233e | 2025-05-31 15:10:41 | [diff] [blame] | 4540 | ** 1. Recovery lock (ofst==2). |
| 4541 | ** 2. Checkpointer lock (ofst==1). |
| 4542 | ** 3. Write lock (ofst==0). |
| 4543 | ** 4. Read locks (ofst>=3 && ofst<SQLITE_SHM_NLOCK). |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 4544 | ** |
| 4545 | ** In other words, if this is a blocking lock, none of the locks that |
| 4546 | ** occur later in the above list than the lock being obtained may be |
| 4547 | ** held. |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 4548 | */ |
dan | 833dd3d | 2025-02-11 18:29:35 | [diff] [blame] | 4549 | #if defined(SQLITE_ENABLE_SETLK_TIMEOUT) && defined(SQLITE_DEBUG) |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 4550 | { |
| 4551 | u16 lockMask = (p->exclMask|p->sharedMask); |
| 4552 | assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || ( |
dan | 420233e | 2025-05-31 15:10:41 | [diff] [blame] | 4553 | (ofst!=2 || lockMask==0) |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 4554 | && (ofst!=1 || lockMask==0 || lockMask==2) |
| 4555 | && (ofst!=0 || lockMask<3) |
| 4556 | && (ofst<3 || lockMask<(1<<ofst)) |
| 4557 | )); |
shaneh | 1f3e27b | 2010-07-12 20:46:33 | [diff] [blame] | 4558 | } |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 4559 | #endif |
shaneh | 1f3e27b | 2010-07-12 20:46:33 | [diff] [blame] | 4560 | |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 4561 | /* Check if there is any work to do. There are three cases: |
| 4562 | ** |
| 4563 | ** a) An unlock operation where there are locks to unlock, |
| 4564 | ** b) An shared lock where the requested lock is not already held |
| 4565 | ** c) An exclusive lock where the requested lock is not already held |
| 4566 | ** |
| 4567 | ** The SQLite core never requests an exclusive lock that it already holds. |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4568 | ** This is assert()ed immediately below. */ |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 4569 | assert( flags!=(SQLITE_SHM_EXCLUSIVE|SQLITE_SHM_LOCK) |
| 4570 | || 0==(p->exclMask & mask) |
| 4571 | ); |
| 4572 | if( ((flags & SQLITE_SHM_UNLOCK) && ((p->exclMask|p->sharedMask) & mask)) |
| 4573 | || (flags==(SQLITE_SHM_SHARED|SQLITE_SHM_LOCK) && 0==(p->sharedMask & mask)) |
| 4574 | || (flags==(SQLITE_SHM_EXCLUSIVE|SQLITE_SHM_LOCK)) |
| 4575 | ){ |
shaneh | 1f3e27b | 2010-07-12 20:46:33 | [diff] [blame] | 4576 | |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4577 | if( flags & SQLITE_SHM_UNLOCK ){ |
| 4578 | /* Case (a) - unlock. */ |
shaneh | 1f3e27b | 2010-07-12 20:46:33 | [diff] [blame] | 4579 | |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4580 | assert( (p->exclMask & p->sharedMask)==0 ); |
| 4581 | assert( !(flags & SQLITE_SHM_EXCLUSIVE) || (p->exclMask & mask)==mask ); |
| 4582 | assert( !(flags & SQLITE_SHM_SHARED) || (p->sharedMask & mask)==mask ); |
shaneh | 1f3e27b | 2010-07-12 20:46:33 | [diff] [blame] | 4583 | |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4584 | rc = winHandleUnlock(p->hShm, ofst+WIN_SHM_BASE, n); |
shaneh | 1f3e27b | 2010-07-12 20:46:33 | [diff] [blame] | 4585 | |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4586 | /* If successful, also clear the bits in sharedMask/exclMask */ |
shaneh | 1f3e27b | 2010-07-12 20:46:33 | [diff] [blame] | 4587 | if( rc==SQLITE_OK ){ |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4588 | p->exclMask = (p->exclMask & ~mask); |
| 4589 | p->sharedMask = (p->sharedMask & ~mask); |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 4590 | } |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4591 | }else{ |
| 4592 | int bExcl = ((flags & SQLITE_SHM_EXCLUSIVE) ? 1 : 0); |
dan | 4628888 | 2025-01-30 15:26:16 | [diff] [blame] | 4593 | DWORD nMs = winFileBusyTimeout(pDbFd); |
dan | d50eb9c | 2024-12-10 19:00:07 | [diff] [blame] | 4594 | rc = winHandleLockTimeout(p->hShm, ofst+WIN_SHM_BASE, n, bExcl, nMs); |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4595 | if( rc==SQLITE_OK ){ |
| 4596 | if( bExcl ){ |
| 4597 | p->exclMask = (p->exclMask | mask); |
| 4598 | }else{ |
| 4599 | p->sharedMask = (p->sharedMask | mask); |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 4600 | } |
shaneh | 1f3e27b | 2010-07-12 20:46:33 | [diff] [blame] | 4601 | } |
| 4602 | } |
| 4603 | } |
dan | 6bd3faa | 2024-11-22 21:24:08 | [diff] [blame] | 4604 | |
| 4605 | OSTRACE(( |
dan | 362d51a | 2024-12-26 16:10:15 | [diff] [blame] | 4606 | "SHM-LOCK(%d,%d,%d) pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x," |
| 4607 | " rc=%s\n", |
| 4608 | ofst, n, flags, |
| 4609 | osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask, |
| 4610 | sqlite3ErrName(rc)) |
| 4611 | ); |
shaneh | 1f3e27b | 2010-07-12 20:46:33 | [diff] [blame] | 4612 | return rc; |
| 4613 | } |
| 4614 | |
| 4615 | /* |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 4616 | ** Implement a memory barrier or memory fence on shared memory. |
shaneh | 1f3e27b | 2010-07-12 20:46:33 | [diff] [blame] | 4617 | ** |
| 4618 | ** All loads and stores begun before the barrier must complete before |
| 4619 | ** any load or store begun after the barrier. |
| 4620 | */ |
| 4621 | static void winShmBarrier( |
| 4622 | sqlite3_file *fd /* Database holding the shared memory */ |
| 4623 | ){ |
| 4624 | UNUSED_PARAMETER(fd); |
drh | 22c733d | 2015-09-24 12:40:43 | [diff] [blame] | 4625 | sqlite3MemoryBarrier(); /* compiler-defined memory barrier */ |
| 4626 | winShmEnterMutex(); /* Also mutex, for redundancy */ |
shaneh | 1f3e27b | 2010-07-12 20:46:33 | [diff] [blame] | 4627 | winShmLeaveMutex(); |
| 4628 | } |
| 4629 | |
| 4630 | /* |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 4631 | ** This function is called to obtain a pointer to region iRegion of the |
| 4632 | ** shared-memory associated with the database file fd. Shared-memory regions |
| 4633 | ** are numbered starting from zero. Each shared-memory region is szRegion |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4634 | ** bytes in size. |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4635 | ** |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4636 | ** If an error occurs, an error code is returned and *pp is set to NULL. |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4637 | ** |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4638 | ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory |
| 4639 | ** region has not been allocated (by any client, including one running in a |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 4640 | ** separate process), then *pp is set to NULL and SQLITE_OK returned. If |
| 4641 | ** isWrite is non-zero and the requested shared-memory region has not yet |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4642 | ** been allocated, it is allocated by this function. |
| 4643 | ** |
| 4644 | ** If the shared-memory region has already been allocated or is allocated by |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 4645 | ** this call as described above, then it is mapped into this processes |
| 4646 | ** address space (if it is not already), *pp is set to point to the mapped |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4647 | ** memory and SQLITE_OK returned. |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4648 | */ |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4649 | static int winShmMap( |
| 4650 | sqlite3_file *fd, /* Handle open on database file */ |
| 4651 | int iRegion, /* Region to retrieve */ |
| 4652 | int szRegion, /* Size of regions */ |
| 4653 | int isWrite, /* True to extend file if necessary */ |
| 4654 | void volatile **pp /* OUT: Mapped memory */ |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4655 | ){ |
| 4656 | winFile *pDbFd = (winFile*)fd; |
mistachkin | dedc5ea | 2015-01-16 19:35:45 | [diff] [blame] | 4657 | winShm *pShm = pDbFd->pShm; |
dan | da9fe0c | 2010-07-13 18:44:03 | [diff] [blame] | 4658 | winShmNode *pShmNode; |
mistachkin | 4ff8431 | 2017-11-09 16:30:55 | [diff] [blame] | 4659 | DWORD protect = PAGE_READWRITE; |
| 4660 | DWORD flags = FILE_MAP_WRITE | FILE_MAP_READ; |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4661 | int rc = SQLITE_OK; |
| 4662 | |
mistachkin | dedc5ea | 2015-01-16 19:35:45 | [diff] [blame] | 4663 | if( !pShm ){ |
dan | da9fe0c | 2010-07-13 18:44:03 | [diff] [blame] | 4664 | rc = winOpenSharedMemory(pDbFd); |
| 4665 | if( rc!=SQLITE_OK ) return rc; |
mistachkin | dedc5ea | 2015-01-16 19:35:45 | [diff] [blame] | 4666 | pShm = pDbFd->pShm; |
drh | 2cd02a5 | 2019-07-16 18:27:07 | [diff] [blame] | 4667 | assert( pShm!=0 ); |
dan | da9fe0c | 2010-07-13 18:44:03 | [diff] [blame] | 4668 | } |
mistachkin | dedc5ea | 2015-01-16 19:35:45 | [diff] [blame] | 4669 | pShmNode = pShm->pShmNode; |
dan | da9fe0c | 2010-07-13 18:44:03 | [diff] [blame] | 4670 | |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4671 | sqlite3_mutex_enter(pShmNode->mutex); |
mistachkin | 4ff8431 | 2017-11-09 16:30:55 | [diff] [blame] | 4672 | if( pShmNode->isUnlocked ){ |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4673 | /* Take the DMS lock. */ |
| 4674 | assert( pShmNode->nRegion==0 ); |
dan | d50eb9c | 2024-12-10 19:00:07 | [diff] [blame] | 4675 | rc = winLockSharedMemory(pShmNode, winFileBusyTimeout(pDbFd)); |
mistachkin | 4ff8431 | 2017-11-09 16:30:55 | [diff] [blame] | 4676 | if( rc!=SQLITE_OK ) goto shmpage_out; |
mistachkin | 4ff8431 | 2017-11-09 16:30:55 | [diff] [blame] | 4677 | } |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4678 | |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4679 | assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4680 | if( pShmNode->nRegion<=iRegion ){ |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4681 | HANDLE hShared = pShmNode->hSharedShm; |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4682 | struct ShmRegion *apNew; /* New aRegion[] array */ |
| 4683 | int nByte = (iRegion+1)*szRegion; /* Minimum required file size */ |
| 4684 | sqlite3_int64 sz; /* Current size of wal-index file */ |
| 4685 | |
| 4686 | pShmNode->szRegion = szRegion; |
| 4687 | |
| 4688 | /* The requested region is not mapped into this processes address space. |
| 4689 | ** Check to see if it has been allocated (i.e. if the wal-index file is |
| 4690 | ** large enough to contain the requested region). |
| 4691 | */ |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4692 | rc = winHandleSize(hShared, &sz); |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4693 | if( rc!=SQLITE_OK ){ |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4694 | rc = winLogError(rc, osGetLastError(), "winShmMap1", pDbFd->zPath); |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4695 | goto shmpage_out; |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4696 | } |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4697 | |
| 4698 | if( sz<nByte ){ |
| 4699 | /* The requested memory region does not exist. If isWrite is set to |
| 4700 | ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned. |
| 4701 | ** |
| 4702 | ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4703 | ** the requested memory region. */ |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4704 | if( !isWrite ) goto shmpage_out; |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4705 | rc = winHandleTruncate(hShared, nByte); |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4706 | if( rc!=SQLITE_OK ){ |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4707 | rc = winLogError(rc, osGetLastError(), "winShmMap2", pDbFd->zPath); |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4708 | goto shmpage_out; |
| 4709 | } |
| 4710 | } |
| 4711 | |
| 4712 | /* Map the requested memory region into this processes address space. */ |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4713 | apNew = (struct ShmRegion*)sqlite3_realloc64( |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4714 | pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0]) |
| 4715 | ); |
| 4716 | if( !apNew ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 4717 | rc = SQLITE_IOERR_NOMEM_BKPT; |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4718 | goto shmpage_out; |
| 4719 | } |
| 4720 | pShmNode->aRegion = apNew; |
| 4721 | |
mistachkin | 4ff8431 | 2017-11-09 16:30:55 | [diff] [blame] | 4722 | if( pShmNode->isReadonly ){ |
| 4723 | protect = PAGE_READONLY; |
| 4724 | flags = FILE_MAP_READ; |
| 4725 | } |
| 4726 | |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4727 | while( pShmNode->nRegion<=iRegion ){ |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 4728 | HANDLE hMap = NULL; /* file-mapping handle */ |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4729 | void *pMap = 0; /* Mapped memory region */ |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 4730 | |
mistachkin | 1e6eea9 | 2012-05-31 22:12:26 | [diff] [blame] | 4731 | #if SQLITE_OS_WINRT |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4732 | hMap = osCreateFileMappingFromApp(hShared, NULL, protect, nByte, NULL); |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 4733 | #elif defined(SQLITE_WIN32_HAS_WIDE) |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4734 | hMap = osCreateFileMappingW(hShared, NULL, protect, 0, nByte, NULL); |
mistachkin | d5be6f0 | 2016-01-27 07:28:33 | [diff] [blame] | 4735 | #elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4736 | hMap = osCreateFileMappingA(hShared, NULL, protect, 0, nByte, NULL); |
mistachkin | 1e6eea9 | 2012-05-31 22:12:26 | [diff] [blame] | 4737 | #endif |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4738 | |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 4739 | OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n", |
| 4740 | osGetCurrentProcessId(), pShmNode->nRegion, nByte, |
shaneh | 420398c | 2010-09-03 04:29:30 | [diff] [blame] | 4741 | hMap ? "ok" : "failed")); |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4742 | if( hMap ){ |
shaneh | 420398c | 2010-09-03 04:29:30 | [diff] [blame] | 4743 | int iOffset = pShmNode->nRegion*szRegion; |
| 4744 | int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity; |
mistachkin | 287a48d | 2012-03-03 13:15:25 | [diff] [blame] | 4745 | #if SQLITE_OS_WINRT |
mistachkin | 4ff8431 | 2017-11-09 16:30:55 | [diff] [blame] | 4746 | pMap = osMapViewOfFileFromApp(hMap, flags, |
mistachkin | 1e6eea9 | 2012-05-31 22:12:26 | [diff] [blame] | 4747 | iOffset - iOffsetShift, szRegion + iOffsetShift |
mistachkin | 287a48d | 2012-03-03 13:15:25 | [diff] [blame] | 4748 | ); |
| 4749 | #else |
mistachkin | 4ff8431 | 2017-11-09 16:30:55 | [diff] [blame] | 4750 | pMap = osMapViewOfFile(hMap, flags, |
shaneh | 420398c | 2010-09-03 04:29:30 | [diff] [blame] | 4751 | 0, iOffset - iOffsetShift, szRegion + iOffsetShift |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4752 | ); |
mistachkin | 287a48d | 2012-03-03 13:15:25 | [diff] [blame] | 4753 | #endif |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 4754 | OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n", |
| 4755 | osGetCurrentProcessId(), pShmNode->nRegion, iOffset, |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 4756 | szRegion, pMap ? "ok" : "failed")); |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4757 | } |
| 4758 | if( !pMap ){ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 4759 | pShmNode->lastErrno = osGetLastError(); |
mistachkin | 2aef997 | 2011-11-10 20:21:20 | [diff] [blame] | 4760 | rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno, |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 4761 | "winShmMap3", pDbFd->zPath); |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 4762 | if( hMap ) osCloseHandle(hMap); |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4763 | goto shmpage_out; |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4764 | } |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4765 | |
| 4766 | pShmNode->aRegion[pShmNode->nRegion].pMap = pMap; |
| 4767 | pShmNode->aRegion[pShmNode->nRegion].hMap = hMap; |
| 4768 | pShmNode->nRegion++; |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4769 | } |
| 4770 | } |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4771 | |
| 4772 | shmpage_out: |
| 4773 | if( pShmNode->nRegion>iRegion ){ |
shaneh | 420398c | 2010-09-03 04:29:30 | [diff] [blame] | 4774 | int iOffset = iRegion*szRegion; |
| 4775 | int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity; |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4776 | char *p = (char *)pShmNode->aRegion[iRegion].pMap; |
shaneh | 420398c | 2010-09-03 04:29:30 | [diff] [blame] | 4777 | *pp = (void *)&p[iOffsetShift]; |
dan | 9785fc9 | 2010-06-14 16:16:33 | [diff] [blame] | 4778 | }else{ |
| 4779 | *pp = 0; |
| 4780 | } |
dan | a180131 | 2024-11-30 20:00:54 | [diff] [blame] | 4781 | if( pShmNode->isReadonly && rc==SQLITE_OK ){ |
| 4782 | rc = SQLITE_READONLY; |
| 4783 | } |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4784 | sqlite3_mutex_leave(pShmNode->mutex); |
| 4785 | return rc; |
| 4786 | } |
| 4787 | |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4788 | #else |
shaneh | 1f3e27b | 2010-07-12 20:46:33 | [diff] [blame] | 4789 | # define winShmMap 0 |
dan | da9fe0c | 2010-07-13 18:44:03 | [diff] [blame] | 4790 | # define winShmLock 0 |
drh | 286a288 | 2010-05-20 23:51:06 | [diff] [blame] | 4791 | # define winShmBarrier 0 |
drh | e11fedc | 2010-07-14 00:14:30 | [diff] [blame] | 4792 | # define winShmUnmap 0 |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4793 | #endif /* #ifndef SQLITE_OMIT_WAL */ |
shaneh | 1f3e27b | 2010-07-12 20:46:33 | [diff] [blame] | 4794 | |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 4795 | /* |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 4796 | ** Cleans up the mapped region of the specified file, if any. |
| 4797 | */ |
mistachkin | 5824e05 | 2013-04-15 20:08:27 | [diff] [blame] | 4798 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4799 | static int winUnmapfile(winFile *pFile){ |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 4800 | assert( pFile!=0 ); |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 4801 | OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, " |
drh | 48ea97e | 2018-11-24 16:07:21 | [diff] [blame] | 4802 | "mmapSize=%lld, mmapSizeMax=%lld\n", |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 4803 | osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion, |
drh | 48ea97e | 2018-11-24 16:07:21 | [diff] [blame] | 4804 | pFile->mmapSize, pFile->mmapSizeMax)); |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 4805 | if( pFile->pMapRegion ){ |
| 4806 | if( !osUnmapViewOfFile(pFile->pMapRegion) ){ |
| 4807 | pFile->lastErrno = osGetLastError(); |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 4808 | OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, " |
| 4809 | "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile, |
| 4810 | pFile->pMapRegion)); |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4811 | return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno, |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 4812 | "winUnmapfile1", pFile->zPath); |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 4813 | } |
| 4814 | pFile->pMapRegion = 0; |
| 4815 | pFile->mmapSize = 0; |
| 4816 | } |
| 4817 | if( pFile->hMap!=NULL ){ |
| 4818 | if( !osCloseHandle(pFile->hMap) ){ |
| 4819 | pFile->lastErrno = osGetLastError(); |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 4820 | OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n", |
| 4821 | osGetCurrentProcessId(), pFile, pFile->hMap)); |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4822 | return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno, |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 4823 | "winUnmapfile2", pFile->zPath); |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 4824 | } |
| 4825 | pFile->hMap = NULL; |
| 4826 | } |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 4827 | OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n", |
| 4828 | osGetCurrentProcessId(), pFile)); |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 4829 | return SQLITE_OK; |
| 4830 | } |
| 4831 | |
| 4832 | /* |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4833 | ** Memory map or remap the file opened by file-descriptor pFd (if the file |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 4834 | ** is already mapped, the existing mapping is replaced by the new). Or, if |
| 4835 | ** there already exists a mapping for this file, and there are still |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4836 | ** outstanding xFetch() references to it, this function is a no-op. |
| 4837 | ** |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 4838 | ** If parameter nByte is non-negative, then it is the requested size of |
| 4839 | ** the mapping to create. Otherwise, if nByte is less than zero, then the |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4840 | ** requested size is the size of the file on disk. The actual size of the |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 4841 | ** created mapping is either the requested size or the value configured |
drh | 9b4c59f | 2013-04-15 17:03:42 | [diff] [blame] | 4842 | ** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller. |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4843 | ** |
| 4844 | ** SQLITE_OK is returned if no error occurs (even if the mapping is not |
| 4845 | ** recreated as a result of outstanding references) or an SQLite error |
| 4846 | ** code otherwise. |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 4847 | */ |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4848 | static int winMapfile(winFile *pFd, sqlite3_int64 nByte){ |
| 4849 | sqlite3_int64 nMap = nByte; |
| 4850 | int rc; |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 4851 | |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4852 | assert( nMap>=0 || pFd->nFetchOut==0 ); |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 4853 | OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n", |
| 4854 | osGetCurrentProcessId(), pFd, nByte)); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 4855 | |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4856 | if( pFd->nFetchOut>0 ) return SQLITE_OK; |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 4857 | |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4858 | if( nMap<0 ){ |
| 4859 | rc = winFileSize((sqlite3_file*)pFd, &nMap); |
| 4860 | if( rc ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 4861 | OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n", |
| 4862 | osGetCurrentProcessId(), pFd)); |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4863 | return SQLITE_IOERR_FSTAT; |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 4864 | } |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 4865 | } |
drh | 9b4c59f | 2013-04-15 17:03:42 | [diff] [blame] | 4866 | if( nMap>pFd->mmapSizeMax ){ |
| 4867 | nMap = pFd->mmapSizeMax; |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4868 | } |
drh | db56bcb | 2013-04-01 18:15:50 | [diff] [blame] | 4869 | nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1); |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 4870 | |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4871 | if( nMap==0 && pFd->mmapSize>0 ){ |
| 4872 | winUnmapfile(pFd); |
| 4873 | } |
| 4874 | if( nMap!=pFd->mmapSize ){ |
| 4875 | void *pNew = 0; |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 4876 | DWORD protect = PAGE_READONLY; |
| 4877 | DWORD flags = FILE_MAP_READ; |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4878 | |
| 4879 | winUnmapfile(pFd); |
mistachkin | c88cd13 | 2015-11-17 21:42:32 | [diff] [blame] | 4880 | #ifdef SQLITE_MMAP_READWRITE |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4881 | if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){ |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 4882 | protect = PAGE_READWRITE; |
| 4883 | flags |= FILE_MAP_WRITE; |
| 4884 | } |
mistachkin | c88cd13 | 2015-11-17 21:42:32 | [diff] [blame] | 4885 | #endif |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 4886 | #if SQLITE_OS_WINRT |
drh | f9d18e4 | 2013-04-01 17:56:28 | [diff] [blame] | 4887 | pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL); |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 4888 | #elif defined(SQLITE_WIN32_HAS_WIDE) |
drh | f9d18e4 | 2013-04-01 17:56:28 | [diff] [blame] | 4889 | pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect, |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4890 | (DWORD)((nMap>>32) & 0xffffffff), |
| 4891 | (DWORD)(nMap & 0xffffffff), NULL); |
mistachkin | d5be6f0 | 2016-01-27 07:28:33 | [diff] [blame] | 4892 | #elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA |
drh | f9d18e4 | 2013-04-01 17:56:28 | [diff] [blame] | 4893 | pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect, |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4894 | (DWORD)((nMap>>32) & 0xffffffff), |
| 4895 | (DWORD)(nMap & 0xffffffff), NULL); |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 4896 | #endif |
drh | f9d18e4 | 2013-04-01 17:56:28 | [diff] [blame] | 4897 | if( pFd->hMap==NULL ){ |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4898 | pFd->lastErrno = osGetLastError(); |
| 4899 | rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno, |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 4900 | "winMapfile1", pFd->zPath); |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4901 | /* Log the error, but continue normal operation using xRead/xWrite */ |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 4902 | OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n", |
| 4903 | osGetCurrentProcessId(), pFd, sqlite3ErrName(rc))); |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4904 | return SQLITE_OK; |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 4905 | } |
drh | 07fa864 | 2013-04-02 14:37:40 | [diff] [blame] | 4906 | assert( (nMap % winSysInfo.dwPageSize)==0 ); |
mistachkin | a9d79ae | 2013-04-02 20:13:04 | [diff] [blame] | 4907 | assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff ); |
mistachkin | c6fc65c | 2013-07-31 23:28:36 | [diff] [blame] | 4908 | #if SQLITE_OS_WINRT |
| 4909 | pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap); |
| 4910 | #else |
drh | f9d18e4 | 2013-04-01 17:56:28 | [diff] [blame] | 4911 | pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap); |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 4912 | #endif |
| 4913 | if( pNew==NULL ){ |
drh | f9d18e4 | 2013-04-01 17:56:28 | [diff] [blame] | 4914 | osCloseHandle(pFd->hMap); |
| 4915 | pFd->hMap = NULL; |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4916 | pFd->lastErrno = osGetLastError(); |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 4917 | rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno, |
| 4918 | "winMapfile2", pFd->zPath); |
| 4919 | /* Log the error, but continue normal operation using xRead/xWrite */ |
| 4920 | OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n", |
| 4921 | osGetCurrentProcessId(), pFd, sqlite3ErrName(rc))); |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4922 | return SQLITE_OK; |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 4923 | } |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4924 | pFd->pMapRegion = pNew; |
| 4925 | pFd->mmapSize = nMap; |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 4926 | } |
| 4927 | |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 4928 | OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n", |
| 4929 | osGetCurrentProcessId(), pFd)); |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4930 | return SQLITE_OK; |
| 4931 | } |
drh | 9b4c59f | 2013-04-15 17:03:42 | [diff] [blame] | 4932 | #endif /* SQLITE_MAX_MMAP_SIZE>0 */ |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 4933 | |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4934 | /* |
| 4935 | ** If possible, return a pointer to a mapping of file fd starting at offset |
| 4936 | ** iOff. The mapping must be valid for at least nAmt bytes. |
| 4937 | ** |
| 4938 | ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK. |
| 4939 | ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK. |
| 4940 | ** Finally, if an error does occur, return an SQLite error code. The final |
| 4941 | ** value of *pp is undefined in this case. |
| 4942 | ** |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 4943 | ** If this function does return a pointer, the caller must eventually |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 4944 | ** release the reference by calling winUnfetch(). |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4945 | */ |
| 4946 | static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){ |
mistachkin | 5824e05 | 2013-04-15 20:08:27 | [diff] [blame] | 4947 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4948 | winFile *pFd = (winFile*)fd; /* The underlying database file */ |
mistachkin | 5824e05 | 2013-04-15 20:08:27 | [diff] [blame] | 4949 | #endif |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4950 | *pp = 0; |
| 4951 | |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 4952 | OSTRACE(("FETCH pid=%lu, pFile=%p, offset=%lld, amount=%d, pp=%p\n", |
| 4953 | osGetCurrentProcessId(), fd, iOff, nAmt, pp)); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 4954 | |
drh | 9b4c59f | 2013-04-15 17:03:42 | [diff] [blame] | 4955 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 4956 | if( pFd->mmapSizeMax>0 ){ |
dan | bcf3df0 | 2024-01-23 16:09:22 | [diff] [blame] | 4957 | /* Ensure that there is always at least a 256 byte buffer of addressable |
| 4958 | ** memory following the returned page. If the database is corrupt, |
| 4959 | ** SQLite may overread the page slightly (in practice only a few bytes, |
| 4960 | ** but 256 is safe, round, number). */ |
| 4961 | const int nEofBuffer = 256; |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4962 | if( pFd->pMapRegion==0 ){ |
| 4963 | int rc = winMapfile(pFd, -1); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 4964 | if( rc!=SQLITE_OK ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 4965 | OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n", |
| 4966 | osGetCurrentProcessId(), pFd, sqlite3ErrName(rc))); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 4967 | return rc; |
| 4968 | } |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4969 | } |
dan | bcf3df0 | 2024-01-23 16:09:22 | [diff] [blame] | 4970 | if( pFd->mmapSize >= (iOff+nAmt+nEofBuffer) ){ |
drh | 2cd02a5 | 2019-07-16 18:27:07 | [diff] [blame] | 4971 | assert( pFd->pMapRegion!=0 ); |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4972 | *pp = &((u8 *)pFd->pMapRegion)[iOff]; |
| 4973 | pFd->nFetchOut++; |
| 4974 | } |
| 4975 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 | [diff] [blame] | 4976 | #endif |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 4977 | |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 4978 | OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n", |
| 4979 | osGetCurrentProcessId(), fd, pp, *pp)); |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4980 | return SQLITE_OK; |
| 4981 | } |
| 4982 | |
| 4983 | /* |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 4984 | ** If the third argument is non-NULL, then this function releases a |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 4985 | ** reference obtained by an earlier call to winFetch(). The second |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4986 | ** argument passed to this function must be the same as the corresponding |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 4987 | ** argument that was passed to the winFetch() invocation. |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4988 | ** |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 4989 | ** Or, if the third argument is NULL, then this function is being called |
| 4990 | ** to inform the VFS layer that, according to POSIX, any existing mapping |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4991 | ** may now be invalid and should be unmapped. |
| 4992 | */ |
| 4993 | static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){ |
mistachkin | 5824e05 | 2013-04-15 20:08:27 | [diff] [blame] | 4994 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4995 | winFile *pFd = (winFile*)fd; /* The underlying database file */ |
| 4996 | |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 4997 | /* If p==0 (unmap the entire file) then there must be no outstanding |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 4998 | ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference), |
| 4999 | ** then there must be at least one outstanding. */ |
| 5000 | assert( (p==0)==(pFd->nFetchOut==0) ); |
| 5001 | |
| 5002 | /* If p!=0, it must match the iOff value. */ |
| 5003 | assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] ); |
| 5004 | |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 5005 | OSTRACE(("UNFETCH pid=%lu, pFile=%p, offset=%lld, p=%p\n", |
| 5006 | osGetCurrentProcessId(), pFd, iOff, p)); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 5007 | |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 5008 | if( p ){ |
| 5009 | pFd->nFetchOut--; |
| 5010 | }else{ |
drh | a539c8a | 2013-04-01 18:25:48 | [diff] [blame] | 5011 | /* FIXME: If Windows truly always prevents truncating or deleting a |
| 5012 | ** file while a mapping is held, then the following winUnmapfile() call |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 | [diff] [blame] | 5013 | ** is unnecessary can be omitted - potentially improving |
drh | a539c8a | 2013-04-01 18:25:48 | [diff] [blame] | 5014 | ** performance. */ |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 5015 | winUnmapfile(pFd); |
| 5016 | } |
| 5017 | |
| 5018 | assert( pFd->nFetchOut>=0 ); |
mistachkin | 5824e05 | 2013-04-15 20:08:27 | [diff] [blame] | 5019 | #endif |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 5020 | |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 5021 | OSTRACE(("UNFETCH pid=%lu, pFile=%p, rc=SQLITE_OK\n", |
| 5022 | osGetCurrentProcessId(), fd)); |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 5023 | return SQLITE_OK; |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 5024 | } |
| 5025 | |
| 5026 | /* |
shaneh | 1f3e27b | 2010-07-12 20:46:33 | [diff] [blame] | 5027 | ** Here ends the implementation of all sqlite3_file methods. |
| 5028 | ** |
| 5029 | ********************** End sqlite3_file Methods ******************************* |
| 5030 | ******************************************************************************/ |
drh | 8323521 | 2010-05-14 16:34:34 | [diff] [blame] | 5031 | |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5032 | /* |
| 5033 | ** This vector defines all the methods that can operate on an |
| 5034 | ** sqlite3_file for win32. |
| 5035 | */ |
| 5036 | static const sqlite3_io_methods winIoMethod = { |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 5037 | 3, /* iVersion */ |
dan | da9fe0c | 2010-07-13 18:44:03 | [diff] [blame] | 5038 | winClose, /* xClose */ |
| 5039 | winRead, /* xRead */ |
| 5040 | winWrite, /* xWrite */ |
| 5041 | winTruncate, /* xTruncate */ |
| 5042 | winSync, /* xSync */ |
| 5043 | winFileSize, /* xFileSize */ |
| 5044 | winLock, /* xLock */ |
| 5045 | winUnlock, /* xUnlock */ |
| 5046 | winCheckReservedLock, /* xCheckReservedLock */ |
| 5047 | winFileControl, /* xFileControl */ |
| 5048 | winSectorSize, /* xSectorSize */ |
| 5049 | winDeviceCharacteristics, /* xDeviceCharacteristics */ |
| 5050 | winShmMap, /* xShmMap */ |
| 5051 | winShmLock, /* xShmLock */ |
| 5052 | winShmBarrier, /* xShmBarrier */ |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 5053 | winShmUnmap, /* xShmUnmap */ |
drh | 5175b32 | 2013-04-01 17:22:51 | [diff] [blame] | 5054 | winFetch, /* xFetch */ |
| 5055 | winUnfetch /* xUnfetch */ |
drh | 9c06c95 | 2005-11-26 00:25:00 | [diff] [blame] | 5056 | }; |
| 5057 | |
mistachkin | 1e75483 | 2016-07-08 21:14:37 | [diff] [blame] | 5058 | /* |
| 5059 | ** This vector defines all the methods that can operate on an |
| 5060 | ** sqlite3_file for win32 without performing any locking. |
| 5061 | */ |
| 5062 | static const sqlite3_io_methods winIoNolockMethod = { |
| 5063 | 3, /* iVersion */ |
| 5064 | winClose, /* xClose */ |
| 5065 | winRead, /* xRead */ |
| 5066 | winWrite, /* xWrite */ |
| 5067 | winTruncate, /* xTruncate */ |
| 5068 | winSync, /* xSync */ |
| 5069 | winFileSize, /* xFileSize */ |
| 5070 | winNolockLock, /* xLock */ |
| 5071 | winNolockUnlock, /* xUnlock */ |
| 5072 | winNolockCheckReservedLock, /* xCheckReservedLock */ |
| 5073 | winFileControl, /* xFileControl */ |
| 5074 | winSectorSize, /* xSectorSize */ |
| 5075 | winDeviceCharacteristics, /* xDeviceCharacteristics */ |
| 5076 | winShmMap, /* xShmMap */ |
| 5077 | winShmLock, /* xShmLock */ |
| 5078 | winShmBarrier, /* xShmBarrier */ |
| 5079 | winShmUnmap, /* xShmUnmap */ |
| 5080 | winFetch, /* xFetch */ |
| 5081 | winUnfetch /* xUnfetch */ |
| 5082 | }; |
| 5083 | |
| 5084 | static winVfsAppData winAppData = { |
| 5085 | &winIoMethod, /* pMethod */ |
| 5086 | 0, /* pAppData */ |
| 5087 | 0 /* bNoLock */ |
| 5088 | }; |
| 5089 | |
| 5090 | static winVfsAppData winNolockAppData = { |
| 5091 | &winIoNolockMethod, /* pMethod */ |
| 5092 | 0, /* pAppData */ |
| 5093 | 1 /* bNoLock */ |
| 5094 | }; |
| 5095 | |
shaneh | 1f3e27b | 2010-07-12 20:46:33 | [diff] [blame] | 5096 | /**************************************************************************** |
| 5097 | **************************** sqlite3_vfs methods **************************** |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5098 | ** |
shaneh | 1f3e27b | 2010-07-12 20:46:33 | [diff] [blame] | 5099 | ** This division contains the implementation of methods on the |
| 5100 | ** sqlite3_vfs object. |
| 5101 | */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5102 | |
drh | 054889e | 2005-11-30 03:20:31 | [diff] [blame] | 5103 | /* |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5104 | ** This function returns non-zero if the specified UTF-8 string buffer |
mistachkin | 14eca4e | 2013-11-07 22:11:55 | [diff] [blame] | 5105 | ** ends with a directory separator character or one was successfully |
| 5106 | ** added to it. |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 | [diff] [blame] | 5107 | */ |
mistachkin | 14eca4e | 2013-11-07 22:11:55 | [diff] [blame] | 5108 | static int winMakeEndInDirSep(int nBuf, char *zBuf){ |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5109 | if( zBuf ){ |
| 5110 | int nLen = sqlite3Strlen30(zBuf); |
mistachkin | 14eca4e | 2013-11-07 22:11:55 | [diff] [blame] | 5111 | if( nLen>0 ){ |
| 5112 | if( winIsDirSep(zBuf[nLen-1]) ){ |
| 5113 | return 1; |
| 5114 | }else if( nLen+1<nBuf ){ |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 5115 | if( !osGetenv ){ |
| 5116 | zBuf[nLen] = winGetDirSep(); |
| 5117 | }else if( winIsDriveLetterAndColon(zBuf) && winIsDirSep(zBuf[2]) ){ |
| 5118 | zBuf[nLen] = '\\'; |
| 5119 | zBuf[2]='\\'; |
| 5120 | }else{ |
| 5121 | zBuf[nLen] = '/'; |
| 5122 | } |
mistachkin | 14eca4e | 2013-11-07 22:11:55 | [diff] [blame] | 5123 | zBuf[nLen+1] = '\0'; |
| 5124 | return 1; |
| 5125 | } |
| 5126 | } |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5127 | } |
| 5128 | return 0; |
| 5129 | } |
| 5130 | |
| 5131 | /* |
drh | e38b6e0 | 2022-11-07 15:01:05 | [diff] [blame] | 5132 | ** If sqlite3_temp_directory is defined, take the mutex and return true. |
drh | 18a3a48 | 2022-09-02 00:36:16 | [diff] [blame] | 5133 | ** |
drh | e38b6e0 | 2022-11-07 15:01:05 | [diff] [blame] | 5134 | ** If sqlite3_temp_directory is NULL (undefined), omit the mutex and |
| 5135 | ** return false. |
drh | 18a3a48 | 2022-09-02 00:36:16 | [diff] [blame] | 5136 | */ |
| 5137 | static int winTempDirDefined(void){ |
| 5138 | sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); |
| 5139 | if( sqlite3_temp_directory!=0 ) return 1; |
| 5140 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); |
| 5141 | return 0; |
| 5142 | } |
| 5143 | |
| 5144 | /* |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5145 | ** Create a temporary file name and store the resulting pointer into pzBuf. |
| 5146 | ** The pointer returned in pzBuf must be freed via sqlite3_free(). |
| 5147 | */ |
| 5148 | static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){ |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 5149 | static const char zChars[] = |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 | [diff] [blame] | 5150 | "abcdefghijklmnopqrstuvwxyz" |
| 5151 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 5152 | "0123456789"; |
shane | 3582c5a | 2008-07-31 01:34:34 | [diff] [blame] | 5153 | size_t i, j; |
drh | 7309b50 | 2023-08-16 15:10:07 | [diff] [blame] | 5154 | DWORD pid; |
mistachkin | fd4b90b | 2013-11-09 21:10:47 | [diff] [blame] | 5155 | int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX); |
drh | ef86b94 | 2025-02-17 17:33:14 | [diff] [blame] | 5156 | i64 nMax, nBuf, nDir, nLen; |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5157 | char *zBuf; |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 5158 | |
| 5159 | /* It's odd to simulate an io-error here, but really this is just |
| 5160 | ** using the io-error infrastructure to test that SQLite handles this |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 5161 | ** function failing. |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 5162 | */ |
| 5163 | SimulateIOError( return SQLITE_IOERR ); |
| 5164 | |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5165 | /* Allocate a temporary buffer to store the fully qualified file |
| 5166 | ** name for the temporary file. If this fails, we cannot continue. |
| 5167 | */ |
drh | ef86b94 | 2025-02-17 17:33:14 | [diff] [blame] | 5168 | nMax = pVfs->mxPathname; |
| 5169 | nBuf = 2 + (i64)nMax; |
mistachkin | fd4b90b | 2013-11-09 21:10:47 | [diff] [blame] | 5170 | zBuf = sqlite3MallocZero( nBuf ); |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5171 | if( !zBuf ){ |
| 5172 | OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 5173 | return SQLITE_IOERR_NOMEM_BKPT; |
mistachkin | c548465 | 2012-03-05 22:52:33 | [diff] [blame] | 5174 | } |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5175 | |
| 5176 | /* Figure out the effective temporary directory. First, check if one |
| 5177 | ** has been explicitly set by the application; otherwise, use the one |
| 5178 | ** configured by the operating system. |
| 5179 | */ |
mistachkin | fd4b90b | 2013-11-09 21:10:47 | [diff] [blame] | 5180 | nDir = nMax - (nPre + 15); |
| 5181 | assert( nDir>0 ); |
drh | 18a3a48 | 2022-09-02 00:36:16 | [diff] [blame] | 5182 | if( winTempDirDefined() ){ |
mistachkin | fd4b90b | 2013-11-09 21:10:47 | [diff] [blame] | 5183 | int nDirLen = sqlite3Strlen30(sqlite3_temp_directory); |
| 5184 | if( nDirLen>0 ){ |
| 5185 | if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){ |
| 5186 | nDirLen++; |
| 5187 | } |
| 5188 | if( nDirLen>nDir ){ |
drh | 18a3a48 | 2022-09-02 00:36:16 | [diff] [blame] | 5189 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); |
mistachkin | fd4b90b | 2013-11-09 21:10:47 | [diff] [blame] | 5190 | sqlite3_free(zBuf); |
| 5191 | OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n")); |
| 5192 | return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0); |
| 5193 | } |
| 5194 | sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory); |
| 5195 | } |
drh | 18a3a48 | 2022-09-02 00:36:16 | [diff] [blame] | 5196 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR)); |
mistachkin | c548465 | 2012-03-05 22:52:33 | [diff] [blame] | 5197 | } |
drh | 18a3a48 | 2022-09-02 00:36:16 | [diff] [blame] | 5198 | |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 5199 | #if defined(__CYGWIN__) |
| 5200 | else if( osGetenv!=NULL ){ |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 5201 | static const char *azDirs[] = { |
| 5202 | 0, /* getenv("SQLITE_TMPDIR") */ |
| 5203 | 0, /* getenv("TMPDIR") */ |
| 5204 | 0, /* getenv("TMP") */ |
| 5205 | 0, /* getenv("TEMP") */ |
| 5206 | 0, /* getenv("USERPROFILE") */ |
| 5207 | "/var/tmp", |
| 5208 | "/usr/tmp", |
| 5209 | "/tmp", |
mistachkin | 1a88b14 | 2013-08-31 18:06:52 | [diff] [blame] | 5210 | ".", |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 5211 | 0 /* List terminator */ |
| 5212 | }; |
| 5213 | unsigned int i; |
| 5214 | const char *zDir = 0; |
| 5215 | |
stephan | 7b9407a | 2025-03-07 06:54:04 | [diff] [blame] | 5216 | if( !azDirs[0] ) azDirs[0] = osGetenv("SQLITE_TMPDIR"); |
| 5217 | if( !azDirs[1] ) azDirs[1] = osGetenv("TMPDIR"); |
| 5218 | if( !azDirs[2] ) azDirs[2] = osGetenv("TMP"); |
| 5219 | if( !azDirs[3] ) azDirs[3] = osGetenv("TEMP"); |
| 5220 | if( !azDirs[4] ) azDirs[4] = osGetenv("USERPROFILE"); |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 5221 | for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){ |
| 5222 | void *zConverted; |
| 5223 | if( zDir==0 ) continue; |
| 5224 | /* If the path starts with a drive letter followed by the colon |
| 5225 | ** character, assume it is already a native Win32 path; otherwise, |
mistachkin | 14eca4e | 2013-11-07 22:11:55 | [diff] [blame] | 5226 | ** it must be converted to a native Win32 path via the Cygwin API |
| 5227 | ** prior to using it. |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 5228 | */ |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 5229 | { |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 5230 | zConverted = winConvertFromUtf8Filename(zDir); |
| 5231 | if( !zConverted ){ |
mistachkin | c216566 | 2013-10-16 09:49:10 | [diff] [blame] | 5232 | sqlite3_free(zBuf); |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 5233 | OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 5234 | return SQLITE_IOERR_NOMEM_BKPT; |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 5235 | } |
| 5236 | if( winIsDir(zConverted) ){ |
mistachkin | fd4b90b | 2013-11-09 21:10:47 | [diff] [blame] | 5237 | sqlite3_snprintf(nMax, zBuf, "%s", zDir); |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 5238 | sqlite3_free(zConverted); |
| 5239 | break; |
| 5240 | } |
| 5241 | sqlite3_free(zConverted); |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 5242 | } |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 5243 | } |
| 5244 | } |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 5245 | #endif |
| 5246 | |
| 5247 | #if !SQLITE_OS_WINRT && defined(_WIN32) |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 5248 | else if( osIsNT() ){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 | [diff] [blame] | 5249 | char *zMulti; |
mistachkin | fd4b90b | 2013-11-09 21:10:47 | [diff] [blame] | 5250 | LPWSTR zWidePath = sqlite3MallocZero( nMax*sizeof(WCHAR) ); |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5251 | if( !zWidePath ){ |
| 5252 | sqlite3_free(zBuf); |
| 5253 | OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 5254 | return SQLITE_IOERR_NOMEM_BKPT; |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5255 | } |
mistachkin | fd4b90b | 2013-11-09 21:10:47 | [diff] [blame] | 5256 | if( osGetTempPathW(nMax, zWidePath)==0 ){ |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5257 | sqlite3_free(zWidePath); |
| 5258 | sqlite3_free(zBuf); |
mistachkin | 16a2e7a | 2013-07-31 22:27:16 | [diff] [blame] | 5259 | OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n")); |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 5260 | return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(), |
mistachkin | fd4b90b | 2013-11-09 21:10:47 | [diff] [blame] | 5261 | "winGetTempname2", 0); |
mistachkin | 16a2e7a | 2013-07-31 22:27:16 | [diff] [blame] | 5262 | } |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 5263 | zMulti = winUnicodeToUtf8(zWidePath); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 | [diff] [blame] | 5264 | if( zMulti ){ |
mistachkin | fd4b90b | 2013-11-09 21:10:47 | [diff] [blame] | 5265 | sqlite3_snprintf(nMax, zBuf, "%s", zMulti); |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 5266 | sqlite3_free(zMulti); |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5267 | sqlite3_free(zWidePath); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 | [diff] [blame] | 5268 | }else{ |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5269 | sqlite3_free(zWidePath); |
| 5270 | sqlite3_free(zBuf); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 5271 | OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 5272 | return SQLITE_IOERR_NOMEM_BKPT; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 | [diff] [blame] | 5273 | } |
drh | d52ee72 | 2012-03-02 00:00:47 | [diff] [blame] | 5274 | } |
| 5275 | #ifdef SQLITE_WIN32_HAS_ANSI |
| 5276 | else{ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 | [diff] [blame] | 5277 | char *zUtf8; |
mistachkin | fd4b90b | 2013-11-09 21:10:47 | [diff] [blame] | 5278 | char *zMbcsPath = sqlite3MallocZero( nMax ); |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5279 | if( !zMbcsPath ){ |
| 5280 | sqlite3_free(zBuf); |
| 5281 | OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 5282 | return SQLITE_IOERR_NOMEM_BKPT; |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5283 | } |
mistachkin | fd4b90b | 2013-11-09 21:10:47 | [diff] [blame] | 5284 | if( osGetTempPathA(nMax, zMbcsPath)==0 ){ |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5285 | sqlite3_free(zBuf); |
mistachkin | 16a2e7a | 2013-07-31 22:27:16 | [diff] [blame] | 5286 | OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n")); |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 5287 | return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(), |
mistachkin | fd4b90b | 2013-11-09 21:10:47 | [diff] [blame] | 5288 | "winGetTempname3", 0); |
mistachkin | 16a2e7a | 2013-07-31 22:27:16 | [diff] [blame] | 5289 | } |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 5290 | zUtf8 = winMbcsToUtf8(zMbcsPath, osAreFileApisANSI()); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 | [diff] [blame] | 5291 | if( zUtf8 ){ |
mistachkin | fd4b90b | 2013-11-09 21:10:47 | [diff] [blame] | 5292 | sqlite3_snprintf(nMax, zBuf, "%s", zUtf8); |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 5293 | sqlite3_free(zUtf8); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 | [diff] [blame] | 5294 | }else{ |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5295 | sqlite3_free(zBuf); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 5296 | OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 5297 | return SQLITE_IOERR_NOMEM_BKPT; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 | [diff] [blame] | 5298 | } |
| 5299 | } |
mistachkin | c6fc65c | 2013-07-31 23:28:36 | [diff] [blame] | 5300 | #endif /* SQLITE_WIN32_HAS_ANSI */ |
mistachkin | c6fc65c | 2013-07-31 23:28:36 | [diff] [blame] | 5301 | #endif /* !SQLITE_OS_WINRT */ |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 5302 | |
mistachkin | fd4b90b | 2013-11-09 21:10:47 | [diff] [blame] | 5303 | /* |
| 5304 | ** Check to make sure the temporary directory ends with an appropriate |
| 5305 | ** separator. If it does not and there is not enough space left to add |
| 5306 | ** one, fail. |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 5307 | */ |
mistachkin | fd4b90b | 2013-11-09 21:10:47 | [diff] [blame] | 5308 | if( !winMakeEndInDirSep(nDir+1, zBuf) ){ |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5309 | sqlite3_free(zBuf); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 5310 | OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n")); |
mistachkin | fd4b90b | 2013-11-09 21:10:47 | [diff] [blame] | 5311 | return winLogError(SQLITE_ERROR, 0, "winGetTempname4", 0); |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 5312 | } |
| 5313 | |
mistachkin | fd4b90b | 2013-11-09 21:10:47 | [diff] [blame] | 5314 | /* |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 5315 | ** Check that the output buffer is large enough for the temporary file |
mistachkin | fd4b90b | 2013-11-09 21:10:47 | [diff] [blame] | 5316 | ** name in the following format: |
| 5317 | ** |
| 5318 | ** "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0" |
| 5319 | ** |
| 5320 | ** If not, return SQLITE_ERROR. The number 17 is used here in order to |
| 5321 | ** account for the space used by the 15 character random suffix and the |
| 5322 | ** two trailing NUL characters. The final directory separator character |
| 5323 | ** has already added if it was not already present. |
| 5324 | */ |
| 5325 | nLen = sqlite3Strlen30(zBuf); |
| 5326 | if( (nLen + nPre + 17) > nBuf ){ |
| 5327 | sqlite3_free(zBuf); |
| 5328 | OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n")); |
| 5329 | return winLogError(SQLITE_ERROR, 0, "winGetTempname5", 0); |
| 5330 | } |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 5331 | |
mistachkin | fd4b90b | 2013-11-09 21:10:47 | [diff] [blame] | 5332 | sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX); |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 5333 | |
drh | ea67883 | 2008-12-10 19:26:22 | [diff] [blame] | 5334 | j = sqlite3Strlen30(zBuf); |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 5335 | sqlite3_randomness(15, &zBuf[j]); |
drh | 7309b50 | 2023-08-16 15:10:07 | [diff] [blame] | 5336 | pid = osGetCurrentProcessId(); |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 5337 | for(i=0; i<15; i++, j++){ |
drh | 7309b50 | 2023-08-16 15:10:07 | [diff] [blame] | 5338 | zBuf[j] += pid & 0xff; |
| 5339 | pid >>= 8; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 | [diff] [blame] | 5340 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 5341 | } |
| 5342 | zBuf[j] = 0; |
mistachkin | e290919 | 2012-01-11 01:01:02 | [diff] [blame] | 5343 | zBuf[j+1] = 0; |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5344 | *pzBuf = zBuf; |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 5345 | |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 5346 | OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf)); |
| 5347 | return SQLITE_OK; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 | [diff] [blame] | 5348 | } |
| 5349 | |
shane | 820800d | 2008-07-22 05:32:03 | [diff] [blame] | 5350 | /* |
drh | 1e039a2 | 2012-05-07 13:15:20 | [diff] [blame] | 5351 | ** Return TRUE if the named file is really a directory. Return false if |
| 5352 | ** it is something other than a directory, or if there is any kind of memory |
| 5353 | ** allocation failure. |
| 5354 | */ |
mistachkin | 48a55aa | 2012-05-07 17:16:07 | [diff] [blame] | 5355 | static int winIsDir(const void *zConverted){ |
| 5356 | DWORD attr; |
| 5357 | int rc = 0; |
| 5358 | DWORD lastErrno; |
drh | 1e039a2 | 2012-05-07 13:15:20 | [diff] [blame] | 5359 | |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 5360 | if( osIsNT() ){ |
mistachkin | 48a55aa | 2012-05-07 17:16:07 | [diff] [blame] | 5361 | int cnt = 0; |
| 5362 | WIN32_FILE_ATTRIBUTE_DATA sAttrData; |
| 5363 | memset(&sAttrData, 0, sizeof(sAttrData)); |
| 5364 | while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted, |
| 5365 | GetFileExInfoStandard, |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 5366 | &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){} |
mistachkin | 48a55aa | 2012-05-07 17:16:07 | [diff] [blame] | 5367 | if( !rc ){ |
| 5368 | return 0; /* Invalid name? */ |
| 5369 | } |
| 5370 | attr = sAttrData.dwFileAttributes; |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 5371 | #if SQLITE_OS_WINCE==0 && defined(SQLITE_WIN32_HAS_ANSI) |
drh | 1e039a2 | 2012-05-07 13:15:20 | [diff] [blame] | 5372 | }else{ |
mistachkin | 48a55aa | 2012-05-07 17:16:07 | [diff] [blame] | 5373 | attr = osGetFileAttributesA((char*)zConverted); |
| 5374 | #endif |
drh | 1e039a2 | 2012-05-07 13:15:20 | [diff] [blame] | 5375 | } |
mistachkin | 48a55aa | 2012-05-07 17:16:07 | [diff] [blame] | 5376 | return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY); |
drh | 1e039a2 | 2012-05-07 13:15:20 | [diff] [blame] | 5377 | } |
| 5378 | |
drh | 0e97e9a | 2017-09-21 20:43:48 | [diff] [blame] | 5379 | /* forward reference */ |
| 5380 | static int winAccess( |
| 5381 | sqlite3_vfs *pVfs, /* Not used on win32 */ |
| 5382 | const char *zFilename, /* Name of file to check */ |
| 5383 | int flags, /* Type of test to make on this file */ |
| 5384 | int *pResOut /* OUT: Result */ |
| 5385 | ); |
| 5386 | |
shane | 820800d | 2008-07-22 05:32:03 | [diff] [blame] | 5387 | /* |
drh | 517a0e0 | 2025-03-26 14:45:15 | [diff] [blame] | 5388 | ** The Windows version of xAccess() accepts an extra bit in the flags |
| 5389 | ** parameter that prevents an anti-virus retry loop. |
| 5390 | */ |
| 5391 | #define NORETRY 0x4000 |
| 5392 | |
| 5393 | /* |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5394 | ** Open a file. |
| 5395 | */ |
| 5396 | static int winOpen( |
mistachkin | 1e75483 | 2016-07-08 21:14:37 | [diff] [blame] | 5397 | sqlite3_vfs *pVfs, /* Used to get maximum path length and AppData */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5398 | const char *zName, /* Name of the file (UTF-8) */ |
| 5399 | sqlite3_file *id, /* Write the SQLite file handle here */ |
| 5400 | int flags, /* Open mode flags */ |
| 5401 | int *pOutFlags /* Status return flags */ |
| 5402 | ){ |
| 5403 | HANDLE h; |
mistachkin | 9939118 | 2013-11-11 02:46:32 | [diff] [blame] | 5404 | DWORD lastErrno = 0; |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5405 | DWORD dwDesiredAccess; |
| 5406 | DWORD dwShareMode; |
| 5407 | DWORD dwCreationDisposition; |
| 5408 | DWORD dwFlagsAndAttributes = 0; |
shane | d94b055 | 2008-09-30 04:20:07 | [diff] [blame] | 5409 | #if SQLITE_OS_WINCE |
| 5410 | int isTemp = 0; |
| 5411 | #endif |
mistachkin | 1e75483 | 2016-07-08 21:14:37 | [diff] [blame] | 5412 | winVfsAppData *pAppData; |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5413 | winFile *pFile = (winFile*)id; |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5414 | void *zConverted; /* Filename in OS encoding */ |
| 5415 | const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */ |
mistachkin | fda06be | 2011-08-02 00:57:34 | [diff] [blame] | 5416 | int cnt = 0; |
drh | 4a6de7f | 2025-03-26 15:51:05 | [diff] [blame] | 5417 | int isRO = 0; /* file is known to be accessible readonly */ |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5418 | |
| 5419 | /* If argument zPath is a NULL pointer, this function is required to open |
| 5420 | ** a temporary file. Use this buffer to store the file name in. |
| 5421 | */ |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5422 | char *zTmpname = 0; /* For temporary filename, if necessary. */ |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5423 | |
| 5424 | int rc = SQLITE_OK; /* Function Return Code */ |
shaneh | bd2aaf9 | 2010-09-01 02:38:21 | [diff] [blame] | 5425 | #if !defined(NDEBUG) || SQLITE_OS_WINCE |
drh | 9bf5bea | 2024-11-21 01:50:01 | [diff] [blame] | 5426 | int eType = flags&0x0FFF00; /* Type of file to open */ |
shaneh | bd2aaf9 | 2010-09-01 02:38:21 | [diff] [blame] | 5427 | #endif |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5428 | |
| 5429 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 5430 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 5431 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 5432 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 5433 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
| 5434 | |
shaneh | bd2aaf9 | 2010-09-01 02:38:21 | [diff] [blame] | 5435 | #ifndef NDEBUG |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5436 | int isOpenJournal = (isCreate && ( |
drh | ccb2113 | 2020-06-19 11:34:57 | [diff] [blame] | 5437 | eType==SQLITE_OPEN_SUPER_JOURNAL |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 5438 | || eType==SQLITE_OPEN_MAIN_JOURNAL |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5439 | || eType==SQLITE_OPEN_WAL |
| 5440 | )); |
shaneh | bd2aaf9 | 2010-09-01 02:38:21 | [diff] [blame] | 5441 | #endif |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5442 | |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 5443 | OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n", |
| 5444 | zUtf8Name, id, flags, pOutFlags)); |
| 5445 | |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 5446 | /* Check the following statements are true: |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5447 | ** |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 5448 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5449 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 5450 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
| 5451 | ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
| 5452 | */ |
| 5453 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
| 5454 | assert(isCreate==0 || isReadWrite); |
| 5455 | assert(isExclusive==0 || isCreate); |
| 5456 | assert(isDelete==0 || isCreate); |
| 5457 | |
drh | 067b92b | 2020-06-19 15:24:12 | [diff] [blame] | 5458 | /* The main DB, main journal, WAL file and super-journal are never |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5459 | ** automatically deleted. Nor are they ever temporary files. */ |
| 5460 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 5461 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
drh | ccb2113 | 2020-06-19 11:34:57 | [diff] [blame] | 5462 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_SUPER_JOURNAL ); |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5463 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); |
| 5464 | |
| 5465 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 5466 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 5467 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
drh | ccb2113 | 2020-06-19 11:34:57 | [diff] [blame] | 5468 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_SUPER_JOURNAL |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5469 | || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL |
| 5470 | ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 | [diff] [blame] | 5471 | |
mistachkin | 9ce59a9 | 2013-02-13 22:54:03 | [diff] [blame] | 5472 | assert( pFile!=0 ); |
| 5473 | memset(pFile, 0, sizeof(winFile)); |
shaneh | 04882a9 | 2010-05-11 02:49:39 | [diff] [blame] | 5474 | pFile->h = INVALID_HANDLE_VALUE; |
| 5475 | |
mistachkin | af52973 | 2012-08-28 04:20:56 | [diff] [blame] | 5476 | #if SQLITE_OS_WINRT |
mistachkin | 533fb6d | 2013-08-28 02:26:48 | [diff] [blame] | 5477 | if( !zUtf8Name && !sqlite3_temp_directory ){ |
mistachkin | af52973 | 2012-08-28 04:20:56 | [diff] [blame] | 5478 | sqlite3_log(SQLITE_ERROR, |
| 5479 | "sqlite3_temp_directory variable should be set for WinRT"); |
| 5480 | } |
| 5481 | #endif |
| 5482 | |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 5483 | /* If the second argument to this function is NULL, generate a |
| 5484 | ** temporary file name to use |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 | [diff] [blame] | 5485 | */ |
| 5486 | if( !zUtf8Name ){ |
mistachkin | 533fb6d | 2013-08-28 02:26:48 | [diff] [blame] | 5487 | assert( isDelete && !isOpenJournal ); |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5488 | rc = winGetTempname(pVfs, &zTmpname); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 | [diff] [blame] | 5489 | if( rc!=SQLITE_OK ){ |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 5490 | OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc))); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 | [diff] [blame] | 5491 | return rc; |
| 5492 | } |
| 5493 | zUtf8Name = zTmpname; |
| 5494 | } |
| 5495 | |
mistachkin | e290919 | 2012-01-11 01:01:02 | [diff] [blame] | 5496 | /* Database filenames are double-zero terminated if they are not |
| 5497 | ** URIs with parameters. Hence, they can always be passed into |
| 5498 | ** sqlite3_uri_parameter(). |
| 5499 | */ |
| 5500 | assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) || |
mistachkin | 533fb6d | 2013-08-28 02:26:48 | [diff] [blame] | 5501 | zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 ); |
mistachkin | e290919 | 2012-01-11 01:01:02 | [diff] [blame] | 5502 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 | [diff] [blame] | 5503 | /* Convert the filename to the system encoding. */ |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 5504 | zConverted = winConvertFromUtf8Filename(zUtf8Name); |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5505 | if( zConverted==0 ){ |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5506 | sqlite3_free(zTmpname); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 5507 | OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name)); |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 5508 | return SQLITE_IOERR_NOMEM_BKPT; |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5509 | } |
| 5510 | |
mistachkin | 48a55aa | 2012-05-07 17:16:07 | [diff] [blame] | 5511 | if( winIsDir(zConverted) ){ |
| 5512 | sqlite3_free(zConverted); |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5513 | sqlite3_free(zTmpname); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 5514 | OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name)); |
mistachkin | 48a55aa | 2012-05-07 17:16:07 | [diff] [blame] | 5515 | return SQLITE_CANTOPEN_ISDIR; |
| 5516 | } |
| 5517 | |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5518 | if( isReadWrite ){ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5519 | dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; |
| 5520 | }else{ |
| 5521 | dwDesiredAccess = GENERIC_READ; |
| 5522 | } |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5523 | |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 5524 | /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is |
| 5525 | ** created. SQLite doesn't use it to indicate "exclusive access" |
shane | 68d405e | 2009-04-23 19:08:32 | [diff] [blame] | 5526 | ** as it is usually understood. |
| 5527 | */ |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5528 | if( isExclusive ){ |
shane | 68d405e | 2009-04-23 19:08:32 | [diff] [blame] | 5529 | /* Creates a new file, only if it does not already exist. */ |
| 5530 | /* If the file exists, it fails. */ |
| 5531 | dwCreationDisposition = CREATE_NEW; |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5532 | }else if( isCreate ){ |
shane | 68d405e | 2009-04-23 19:08:32 | [diff] [blame] | 5533 | /* Open existing file, or create if it doesn't exist */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5534 | dwCreationDisposition = OPEN_ALWAYS; |
| 5535 | }else{ |
shane | 68d405e | 2009-04-23 19:08:32 | [diff] [blame] | 5536 | /* Opens a file, only if it exists. */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5537 | dwCreationDisposition = OPEN_EXISTING; |
| 5538 | } |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5539 | |
drh | f8c4c3a | 2020-10-15 14:37:27 | [diff] [blame] | 5540 | if( 0==sqlite3_uri_boolean(zName, "exclusive", 0) ){ |
| 5541 | dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; |
| 5542 | }else{ |
| 5543 | dwShareMode = 0; |
| 5544 | } |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5545 | |
| 5546 | if( isDelete ){ |
danielk1977 | 29bafea | 2008-06-26 10:41:19 | [diff] [blame] | 5547 | #if SQLITE_OS_WINCE |
drh | 0cd1ea5 | 2007-10-08 15:06:03 | [diff] [blame] | 5548 | dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN; |
shane | d94b055 | 2008-09-30 04:20:07 | [diff] [blame] | 5549 | isTemp = 1; |
drh | 0cd1ea5 | 2007-10-08 15:06:03 | [diff] [blame] | 5550 | #else |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5551 | dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY |
| 5552 | | FILE_ATTRIBUTE_HIDDEN |
| 5553 | | FILE_FLAG_DELETE_ON_CLOSE; |
drh | 0cd1ea5 | 2007-10-08 15:06:03 | [diff] [blame] | 5554 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5555 | }else{ |
| 5556 | dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL; |
| 5557 | } |
drh | 496936c | 2007-10-08 12:21:10 | [diff] [blame] | 5558 | /* Reports from the internet are that performance is always |
| 5559 | ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */ |
shane | d94b055 | 2008-09-30 04:20:07 | [diff] [blame] | 5560 | #if SQLITE_OS_WINCE |
drh | 496936c | 2007-10-08 12:21:10 | [diff] [blame] | 5561 | dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS; |
shane | d94b055 | 2008-09-30 04:20:07 | [diff] [blame] | 5562 | #endif |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5563 | |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 5564 | if( osIsNT() ){ |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 5565 | #if SQLITE_OS_WINRT |
mistachkin | 5483f77 | 2012-03-07 20:11:47 | [diff] [blame] | 5566 | CREATEFILE2_EXTENDED_PARAMETERS extendedParameters; |
| 5567 | extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS); |
| 5568 | extendedParameters.dwFileAttributes = |
| 5569 | dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK; |
| 5570 | extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK; |
| 5571 | extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS; |
| 5572 | extendedParameters.lpSecurityAttributes = NULL; |
| 5573 | extendedParameters.hTemplateFile = NULL; |
mistachkin | 3314062 | 2017-09-22 16:23:23 | [diff] [blame] | 5574 | do{ |
| 5575 | h = osCreateFile2((LPCWSTR)zConverted, |
| 5576 | dwDesiredAccess, |
| 5577 | dwShareMode, |
| 5578 | dwCreationDisposition, |
| 5579 | &extendedParameters); |
| 5580 | if( h!=INVALID_HANDLE_VALUE ) break; |
| 5581 | if( isReadWrite ){ |
drh | 4a6de7f | 2025-03-26 15:51:05 | [diff] [blame] | 5582 | int rc2; |
mistachkin | 5685257 | 2018-01-17 01:26:05 | [diff] [blame] | 5583 | sqlite3BeginBenignMalloc(); |
drh | 517a0e0 | 2025-03-26 14:45:15 | [diff] [blame] | 5584 | rc2 = winAccess(pVfs, zUtf8Name, SQLITE_ACCESS_READ|NORETRY, &isRO); |
mistachkin | 5685257 | 2018-01-17 01:26:05 | [diff] [blame] | 5585 | sqlite3EndBenignMalloc(); |
mistachkin | 3314062 | 2017-09-22 16:23:23 | [diff] [blame] | 5586 | if( rc2==SQLITE_OK && isRO ) break; |
| 5587 | } |
| 5588 | }while( winRetryIoerr(&cnt, &lastErrno) ); |
mistachkin | 5483f77 | 2012-03-07 20:11:47 | [diff] [blame] | 5589 | #else |
drh | 0e97e9a | 2017-09-21 20:43:48 | [diff] [blame] | 5590 | do{ |
| 5591 | h = osCreateFileW((LPCWSTR)zConverted, |
| 5592 | dwDesiredAccess, |
| 5593 | dwShareMode, NULL, |
| 5594 | dwCreationDisposition, |
| 5595 | dwFlagsAndAttributes, |
| 5596 | NULL); |
| 5597 | if( h!=INVALID_HANDLE_VALUE ) break; |
| 5598 | if( isReadWrite ){ |
drh | 4a6de7f | 2025-03-26 15:51:05 | [diff] [blame] | 5599 | int rc2; |
mistachkin | 5685257 | 2018-01-17 01:26:05 | [diff] [blame] | 5600 | sqlite3BeginBenignMalloc(); |
drh | 517a0e0 | 2025-03-26 14:45:15 | [diff] [blame] | 5601 | rc2 = winAccess(pVfs, zUtf8Name, SQLITE_ACCESS_READ|NORETRY, &isRO); |
mistachkin | 5685257 | 2018-01-17 01:26:05 | [diff] [blame] | 5602 | sqlite3EndBenignMalloc(); |
drh | 0e97e9a | 2017-09-21 20:43:48 | [diff] [blame] | 5603 | if( rc2==SQLITE_OK && isRO ) break; |
| 5604 | } |
| 5605 | }while( winRetryIoerr(&cnt, &lastErrno) ); |
mistachkin | 5483f77 | 2012-03-07 20:11:47 | [diff] [blame] | 5606 | #endif |
drh | d52ee72 | 2012-03-02 00:00:47 | [diff] [blame] | 5607 | } |
| 5608 | #ifdef SQLITE_WIN32_HAS_ANSI |
| 5609 | else{ |
mistachkin | 3314062 | 2017-09-22 16:23:23 | [diff] [blame] | 5610 | do{ |
| 5611 | h = osCreateFileA((LPCSTR)zConverted, |
| 5612 | dwDesiredAccess, |
| 5613 | dwShareMode, NULL, |
| 5614 | dwCreationDisposition, |
| 5615 | dwFlagsAndAttributes, |
| 5616 | NULL); |
| 5617 | if( h!=INVALID_HANDLE_VALUE ) break; |
| 5618 | if( isReadWrite ){ |
drh | 4a6de7f | 2025-03-26 15:51:05 | [diff] [blame] | 5619 | int rc2; |
mistachkin | 5685257 | 2018-01-17 01:26:05 | [diff] [blame] | 5620 | sqlite3BeginBenignMalloc(); |
drh | 517a0e0 | 2025-03-26 14:45:15 | [diff] [blame] | 5621 | rc2 = winAccess(pVfs, zUtf8Name, SQLITE_ACCESS_READ|NORETRY, &isRO); |
mistachkin | 5685257 | 2018-01-17 01:26:05 | [diff] [blame] | 5622 | sqlite3EndBenignMalloc(); |
mistachkin | 3314062 | 2017-09-22 16:23:23 | [diff] [blame] | 5623 | if( rc2==SQLITE_OK && isRO ) break; |
| 5624 | } |
| 5625 | }while( winRetryIoerr(&cnt, &lastErrno) ); |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5626 | } |
drh | d52ee72 | 2012-03-02 00:00:47 | [diff] [blame] | 5627 | #endif |
drh | 21aa6a1 | 2015-03-26 15:27:32 | [diff] [blame] | 5628 | winLogIoerr(cnt, __LINE__); |
mistachkin | fda06be | 2011-08-02 00:57:34 | [diff] [blame] | 5629 | |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 5630 | OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name, |
| 5631 | dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok")); |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5632 | |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5633 | if( h==INVALID_HANDLE_VALUE ){ |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 5634 | sqlite3_free(zConverted); |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 5635 | sqlite3_free(zTmpname); |
drh | 4a6de7f | 2025-03-26 15:51:05 | [diff] [blame] | 5636 | if( isReadWrite && isRO && !isExclusive ){ |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 5637 | return winOpen(pVfs, zName, id, |
drh | 1488052 | 2013-03-01 23:24:04 | [diff] [blame] | 5638 | ((flags|SQLITE_OPEN_READONLY) & |
| 5639 | ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), |
| 5640 | pOutFlags); |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5641 | }else{ |
drh | b40d9ee | 2017-09-21 20:03:17 | [diff] [blame] | 5642 | pFile->lastErrno = lastErrno; |
| 5643 | winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name); |
drh | 9978c97 | 2010-02-23 17:36:32 | [diff] [blame] | 5644 | return SQLITE_CANTOPEN_BKPT; |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5645 | } |
| 5646 | } |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5647 | |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5648 | if( pOutFlags ){ |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5649 | if( isReadWrite ){ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5650 | *pOutFlags = SQLITE_OPEN_READWRITE; |
| 5651 | }else{ |
| 5652 | *pOutFlags = SQLITE_OPEN_READONLY; |
| 5653 | } |
| 5654 | } |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5655 | |
mistachkin | e84d8d3 | 2013-04-29 03:09:10 | [diff] [blame] | 5656 | OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, " |
| 5657 | "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ? |
| 5658 | *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok")); |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5659 | |
mistachkin | 1e75483 | 2016-07-08 21:14:37 | [diff] [blame] | 5660 | pAppData = (winVfsAppData*)pVfs->pAppData; |
| 5661 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 | [diff] [blame] | 5662 | #if SQLITE_OS_WINCE |
mistachkin | 1e75483 | 2016-07-08 21:14:37 | [diff] [blame] | 5663 | { |
| 5664 | if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB |
| 5665 | && ((pAppData==NULL) || !pAppData->bNoLock) |
| 5666 | && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK |
| 5667 | ){ |
| 5668 | osCloseHandle(h); |
| 5669 | sqlite3_free(zConverted); |
| 5670 | sqlite3_free(zTmpname); |
| 5671 | OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc))); |
| 5672 | return rc; |
| 5673 | } |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5674 | } |
drh | fc3afb6 | 2007-10-09 15:36:10 | [diff] [blame] | 5675 | if( isTemp ){ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5676 | pFile->zDeleteOnClose = zConverted; |
| 5677 | }else |
| 5678 | #endif |
| 5679 | { |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 5680 | sqlite3_free(zConverted); |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5681 | } |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5682 | |
mistachkin | c216566 | 2013-10-16 09:49:10 | [diff] [blame] | 5683 | sqlite3_free(zTmpname); |
drh | 0c52f5a | 2020-07-24 09:17:42 | [diff] [blame] | 5684 | id->pMethods = pAppData ? pAppData->pMethod : &winIoMethod; |
mistachkin | 9ce59a9 | 2013-02-13 22:54:03 | [diff] [blame] | 5685 | pFile->pVfs = pVfs; |
| 5686 | pFile->h = h; |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 5687 | if( isReadonly ){ |
| 5688 | pFile->ctrlFlags |= WINFILE_RDONLY; |
| 5689 | } |
dan | 10757ed | 2020-05-19 15:40:07 | [diff] [blame] | 5690 | if( (flags & SQLITE_OPEN_MAIN_DB) |
| 5691 | && sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) |
| 5692 | ){ |
mistachkin | 9ce59a9 | 2013-02-13 22:54:03 | [diff] [blame] | 5693 | pFile->ctrlFlags |= WINFILE_PSOW; |
| 5694 | } |
| 5695 | pFile->lastErrno = NO_ERROR; |
| 5696 | pFile->zPath = zName; |
mistachkin | 5824e05 | 2013-04-15 20:08:27 | [diff] [blame] | 5697 | #if SQLITE_MAX_MMAP_SIZE>0 |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 5698 | pFile->hMap = NULL; |
| 5699 | pFile->pMapRegion = 0; |
| 5700 | pFile->mmapSize = 0; |
dan | ede01a9 | 2013-05-17 12:10:52 | [diff] [blame] | 5701 | pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap; |
mistachkin | 5824e05 | 2013-04-15 20:08:27 | [diff] [blame] | 5702 | #endif |
mistachkin | 9ce59a9 | 2013-02-13 22:54:03 | [diff] [blame] | 5703 | |
drh | af5f040 | 2007-09-03 17:09:03 | [diff] [blame] | 5704 | OpenCounter(+1); |
shaneh | f7b5f85 | 2010-08-24 20:46:53 | [diff] [blame] | 5705 | return rc; |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5706 | } |
| 5707 | |
| 5708 | /* |
| 5709 | ** Delete the named file. |
| 5710 | ** |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 5711 | ** Note that Windows does not allow a file to be deleted if some other |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5712 | ** process has it open. Sometimes a virus scanner or indexing program |
| 5713 | ** will open a journal file shortly after it is created in order to do |
shane | 3582c5a | 2008-07-31 01:34:34 | [diff] [blame] | 5714 | ** whatever it does. While this other process is holding the |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5715 | ** file open, we will be unable to delete it. To work around this |
| 5716 | ** problem, we delay 100 milliseconds and try to delete again. Up |
| 5717 | ** to MX_DELETION_ATTEMPTs deletion attempts are run before giving |
| 5718 | ** up and returning an error. |
| 5719 | */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5720 | static int winDelete( |
| 5721 | sqlite3_vfs *pVfs, /* Not used on win32 */ |
| 5722 | const char *zFilename, /* Name of file to delete */ |
| 5723 | int syncDir /* Not used on win32 */ |
| 5724 | ){ |
| 5725 | int cnt = 0; |
drh | 52564d7 | 2011-07-12 11:04:18 | [diff] [blame] | 5726 | int rc; |
mistachkin | 48a55aa | 2012-05-07 17:16:07 | [diff] [blame] | 5727 | DWORD attr; |
mistachkin | 9939118 | 2013-11-11 02:46:32 | [diff] [blame] | 5728 | DWORD lastErrno = 0; |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 5729 | void *zConverted; |
shane | 18e526c | 2008-12-10 22:30:24 | [diff] [blame] | 5730 | UNUSED_PARAMETER(pVfs); |
| 5731 | UNUSED_PARAMETER(syncDir); |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 5732 | |
| 5733 | SimulateIOError(return SQLITE_IOERR_DELETE); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 5734 | OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir)); |
| 5735 | |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 5736 | zConverted = winConvertFromUtf8Filename(zFilename); |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5737 | if( zConverted==0 ){ |
mistachkin | 533fb6d | 2013-08-28 02:26:48 | [diff] [blame] | 5738 | OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename)); |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 5739 | return SQLITE_IOERR_NOMEM_BKPT; |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5740 | } |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 5741 | if( osIsNT() ){ |
mistachkin | 48a55aa | 2012-05-07 17:16:07 | [diff] [blame] | 5742 | do { |
mistachkin | 287a48d | 2012-03-03 13:15:25 | [diff] [blame] | 5743 | #if SQLITE_OS_WINRT |
mistachkin | fcd2f12 | 2012-05-07 20:28:19 | [diff] [blame] | 5744 | WIN32_FILE_ATTRIBUTE_DATA sAttrData; |
| 5745 | memset(&sAttrData, 0, sizeof(sAttrData)); |
| 5746 | if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard, |
| 5747 | &sAttrData) ){ |
| 5748 | attr = sAttrData.dwFileAttributes; |
| 5749 | }else{ |
mistachkin | 55fbc86 | 2012-11-21 02:10:20 | [diff] [blame] | 5750 | lastErrno = osGetLastError(); |
drh | 1488052 | 2013-03-01 23:24:04 | [diff] [blame] | 5751 | if( lastErrno==ERROR_FILE_NOT_FOUND |
| 5752 | || lastErrno==ERROR_PATH_NOT_FOUND ){ |
mistachkin | 55fbc86 | 2012-11-21 02:10:20 | [diff] [blame] | 5753 | rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */ |
| 5754 | }else{ |
| 5755 | rc = SQLITE_ERROR; |
| 5756 | } |
mistachkin | fcd2f12 | 2012-05-07 20:28:19 | [diff] [blame] | 5757 | break; |
| 5758 | } |
mistachkin | 287a48d | 2012-03-03 13:15:25 | [diff] [blame] | 5759 | #else |
mistachkin | 48a55aa | 2012-05-07 17:16:07 | [diff] [blame] | 5760 | attr = osGetFileAttributesW(zConverted); |
mistachkin | 287a48d | 2012-03-03 13:15:25 | [diff] [blame] | 5761 | #endif |
mistachkin | 48a55aa | 2012-05-07 17:16:07 | [diff] [blame] | 5762 | if ( attr==INVALID_FILE_ATTRIBUTES ){ |
mistachkin | 55fbc86 | 2012-11-21 02:10:20 | [diff] [blame] | 5763 | lastErrno = osGetLastError(); |
drh | 1488052 | 2013-03-01 23:24:04 | [diff] [blame] | 5764 | if( lastErrno==ERROR_FILE_NOT_FOUND |
| 5765 | || lastErrno==ERROR_PATH_NOT_FOUND ){ |
mistachkin | 55fbc86 | 2012-11-21 02:10:20 | [diff] [blame] | 5766 | rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */ |
| 5767 | }else{ |
| 5768 | rc = SQLITE_ERROR; |
| 5769 | } |
mistachkin | 48a55aa | 2012-05-07 17:16:07 | [diff] [blame] | 5770 | break; |
| 5771 | } |
| 5772 | if ( attr&FILE_ATTRIBUTE_DIRECTORY ){ |
| 5773 | rc = SQLITE_ERROR; /* Files only. */ |
| 5774 | break; |
| 5775 | } |
| 5776 | if ( osDeleteFileW(zConverted) ){ |
| 5777 | rc = SQLITE_OK; /* Deleted OK. */ |
| 5778 | break; |
| 5779 | } |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 5780 | if ( !winRetryIoerr(&cnt, &lastErrno) ){ |
mistachkin | 48a55aa | 2012-05-07 17:16:07 | [diff] [blame] | 5781 | rc = SQLITE_ERROR; /* No more retries. */ |
| 5782 | break; |
| 5783 | } |
| 5784 | } while(1); |
drh | d52ee72 | 2012-03-02 00:00:47 | [diff] [blame] | 5785 | } |
| 5786 | #ifdef SQLITE_WIN32_HAS_ANSI |
| 5787 | else{ |
mistachkin | 48a55aa | 2012-05-07 17:16:07 | [diff] [blame] | 5788 | do { |
| 5789 | attr = osGetFileAttributesA(zConverted); |
| 5790 | if ( attr==INVALID_FILE_ATTRIBUTES ){ |
mistachkin | 55fbc86 | 2012-11-21 02:10:20 | [diff] [blame] | 5791 | lastErrno = osGetLastError(); |
drh | 1488052 | 2013-03-01 23:24:04 | [diff] [blame] | 5792 | if( lastErrno==ERROR_FILE_NOT_FOUND |
| 5793 | || lastErrno==ERROR_PATH_NOT_FOUND ){ |
mistachkin | 55fbc86 | 2012-11-21 02:10:20 | [diff] [blame] | 5794 | rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */ |
| 5795 | }else{ |
| 5796 | rc = SQLITE_ERROR; |
| 5797 | } |
mistachkin | 48a55aa | 2012-05-07 17:16:07 | [diff] [blame] | 5798 | break; |
| 5799 | } |
| 5800 | if ( attr&FILE_ATTRIBUTE_DIRECTORY ){ |
| 5801 | rc = SQLITE_ERROR; /* Files only. */ |
| 5802 | break; |
| 5803 | } |
| 5804 | if ( osDeleteFileA(zConverted) ){ |
| 5805 | rc = SQLITE_OK; /* Deleted OK. */ |
| 5806 | break; |
| 5807 | } |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 5808 | if ( !winRetryIoerr(&cnt, &lastErrno) ){ |
mistachkin | 48a55aa | 2012-05-07 17:16:07 | [diff] [blame] | 5809 | rc = SQLITE_ERROR; /* No more retries. */ |
| 5810 | break; |
| 5811 | } |
| 5812 | } while(1); |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5813 | } |
drh | d52ee72 | 2012-03-02 00:00:47 | [diff] [blame] | 5814 | #endif |
drh | 6d405c2 | 2012-11-20 15:06:57 | [diff] [blame] | 5815 | if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){ |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 5816 | rc = winLogError(SQLITE_IOERR_DELETE, lastErrno, "winDelete", zFilename); |
drh | a32ad84 | 2011-07-12 13:51:05 | [diff] [blame] | 5817 | }else{ |
drh | 21aa6a1 | 2015-03-26 15:27:32 | [diff] [blame] | 5818 | winLogIoerr(cnt, __LINE__); |
drh | a32ad84 | 2011-07-12 13:51:05 | [diff] [blame] | 5819 | } |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 5820 | sqlite3_free(zConverted); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 5821 | OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc))); |
drh | 52564d7 | 2011-07-12 11:04:18 | [diff] [blame] | 5822 | return rc; |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5823 | } |
| 5824 | |
| 5825 | /* |
mistachkin | 48864df | 2013-03-21 21:20:32 | [diff] [blame] | 5826 | ** Check the existence and status of a file. |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5827 | */ |
| 5828 | static int winAccess( |
| 5829 | sqlite3_vfs *pVfs, /* Not used on win32 */ |
| 5830 | const char *zFilename, /* Name of file to check */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 | [diff] [blame] | 5831 | int flags, /* Type of test to make on this file */ |
| 5832 | int *pResOut /* OUT: Result */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5833 | ){ |
| 5834 | DWORD attr; |
drh | ea67883 | 2008-12-10 19:26:22 | [diff] [blame] | 5835 | int rc = 0; |
mistachkin | 9939118 | 2013-11-11 02:46:32 | [diff] [blame] | 5836 | DWORD lastErrno = 0; |
shaneh | cce1b68 | 2010-07-07 14:45:40 | [diff] [blame] | 5837 | void *zConverted; |
drh | 517a0e0 | 2025-03-26 14:45:15 | [diff] [blame] | 5838 | int noRetry = 0; /* Do not use winRetryIoerr() */ |
shane | 18e526c | 2008-12-10 22:30:24 | [diff] [blame] | 5839 | UNUSED_PARAMETER(pVfs); |
shaneh | cce1b68 | 2010-07-07 14:45:40 | [diff] [blame] | 5840 | |
drh | 517a0e0 | 2025-03-26 14:45:15 | [diff] [blame] | 5841 | if( (flags & NORETRY)!=0 ){ |
| 5842 | noRetry = 1; |
| 5843 | flags &= ~NORETRY; |
| 5844 | } |
| 5845 | |
shaneh | cce1b68 | 2010-07-07 14:45:40 | [diff] [blame] | 5846 | SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 5847 | OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n", |
| 5848 | zFilename, flags, pResOut)); |
| 5849 | |
mistachkin | cf5dce0 | 2023-04-27 21:31:21 | [diff] [blame] | 5850 | if( zFilename==0 ){ |
| 5851 | *pResOut = 0; |
| 5852 | OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n", |
| 5853 | zFilename, pResOut, *pResOut)); |
| 5854 | return SQLITE_OK; |
| 5855 | } |
| 5856 | |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 5857 | zConverted = winConvertFromUtf8Filename(zFilename); |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5858 | if( zConverted==0 ){ |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 5859 | OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename)); |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 5860 | return SQLITE_IOERR_NOMEM_BKPT; |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5861 | } |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 5862 | if( osIsNT() ){ |
drh | fdf6db1 | 2011-07-22 21:25:57 | [diff] [blame] | 5863 | int cnt = 0; |
drh | 722a7e9 | 2010-07-05 21:00:43 | [diff] [blame] | 5864 | WIN32_FILE_ATTRIBUTE_DATA sAttrData; |
| 5865 | memset(&sAttrData, 0, sizeof(sAttrData)); |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 5866 | while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted, |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 5867 | GetFileExInfoStandard, |
drh | 517a0e0 | 2025-03-26 14:45:15 | [diff] [blame] | 5868 | &sAttrData)) |
| 5869 | && !noRetry |
| 5870 | && winRetryIoerr(&cnt, &lastErrno) |
| 5871 | ){ /* Loop until true */} |
drh | fdf6db1 | 2011-07-22 21:25:57 | [diff] [blame] | 5872 | if( rc ){ |
shaneh | 7ea7259 | 2010-07-07 13:58:11 | [diff] [blame] | 5873 | /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file |
| 5874 | ** as if it does not exist. |
| 5875 | */ |
| 5876 | if( flags==SQLITE_ACCESS_EXISTS |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 5877 | && sAttrData.nFileSizeHigh==0 |
shaneh | 7ea7259 | 2010-07-07 13:58:11 | [diff] [blame] | 5878 | && sAttrData.nFileSizeLow==0 ){ |
| 5879 | attr = INVALID_FILE_ATTRIBUTES; |
| 5880 | }else{ |
| 5881 | attr = sAttrData.dwFileAttributes; |
| 5882 | } |
| 5883 | }else{ |
drh | 21aa6a1 | 2015-03-26 15:27:32 | [diff] [blame] | 5884 | winLogIoerr(cnt, __LINE__); |
drh | 19038f1b7 | 2012-08-31 12:31:18 | [diff] [blame] | 5885 | if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){ |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 5886 | sqlite3_free(zConverted); |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 5887 | return winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess", |
| 5888 | zFilename); |
shaneh | cce1b68 | 2010-07-07 14:45:40 | [diff] [blame] | 5889 | }else{ |
| 5890 | attr = INVALID_FILE_ATTRIBUTES; |
| 5891 | } |
drh | 722a7e9 | 2010-07-05 21:00:43 | [diff] [blame] | 5892 | } |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5893 | } |
drh | d52ee72 | 2012-03-02 00:00:47 | [diff] [blame] | 5894 | #ifdef SQLITE_WIN32_HAS_ANSI |
| 5895 | else{ |
| 5896 | attr = osGetFileAttributesA((char*)zConverted); |
| 5897 | } |
| 5898 | #endif |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 5899 | sqlite3_free(zConverted); |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5900 | switch( flags ){ |
drh | 50d3f90 | 2007-08-27 21:10:36 | [diff] [blame] | 5901 | case SQLITE_ACCESS_READ: |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5902 | case SQLITE_ACCESS_EXISTS: |
shane | 820800d | 2008-07-22 05:32:03 | [diff] [blame] | 5903 | rc = attr!=INVALID_FILE_ATTRIBUTES; |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5904 | break; |
| 5905 | case SQLITE_ACCESS_READWRITE: |
mistachkin | 4e6b49b | 2011-07-28 19:16:41 | [diff] [blame] | 5906 | rc = attr!=INVALID_FILE_ATTRIBUTES && |
| 5907 | (attr & FILE_ATTRIBUTE_READONLY)==0; |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5908 | break; |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5909 | default: |
| 5910 | assert(!"Invalid flags argument"); |
| 5911 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 | [diff] [blame] | 5912 | *pResOut = rc; |
mistachkin | f2c1c99 | 2013-04-28 01:44:43 | [diff] [blame] | 5913 | OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n", |
| 5914 | zFilename, pResOut, *pResOut)); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 | [diff] [blame] | 5915 | return SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 | [diff] [blame] | 5916 | } |
| 5917 | |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 5918 | /* |
mistachkin | a8e41ec | 2020-05-15 01:18:07 | [diff] [blame] | 5919 | ** Returns non-zero if the specified path name starts with the "long path" |
| 5920 | ** prefix. |
| 5921 | */ |
| 5922 | static BOOL winIsLongPathPrefix( |
| 5923 | const char *zPathname |
| 5924 | ){ |
| 5925 | return ( zPathname[0]=='\\' && zPathname[1]=='\\' |
| 5926 | && zPathname[2]=='?' && zPathname[3]=='\\' ); |
| 5927 | } |
| 5928 | |
| 5929 | /* |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 5930 | ** Returns non-zero if the specified path name starts with a drive letter |
| 5931 | ** followed by a colon character. |
| 5932 | */ |
| 5933 | static BOOL winIsDriveLetterAndColon( |
| 5934 | const char *zPathname |
| 5935 | ){ |
| 5936 | return ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' ); |
| 5937 | } |
drh | 054889e | 2005-11-30 03:20:31 | [diff] [blame] | 5938 | |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 5939 | #ifdef _WIN32 |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 5940 | /* |
mistachkin | a112d14 | 2012-03-14 00:44:01 | [diff] [blame] | 5941 | ** Returns non-zero if the specified path name should be used verbatim. If |
| 5942 | ** non-zero is returned from this function, the calling function must simply |
| 5943 | ** use the provided path name verbatim -OR- resolve it into a full path name |
| 5944 | ** using the GetFullPathName Win32 API function (if available). |
| 5945 | */ |
| 5946 | static BOOL winIsVerbatimPathname( |
| 5947 | const char *zPathname |
| 5948 | ){ |
| 5949 | /* |
| 5950 | ** If the path name starts with a forward slash or a backslash, it is either |
| 5951 | ** a legal UNC name, a volume relative path, or an absolute path name in the |
| 5952 | ** "Unix" format on Windows. There is no easy way to differentiate between |
| 5953 | ** the final two cases; therefore, we return the safer return value of TRUE |
| 5954 | ** so that callers of this function will simply use it verbatim. |
| 5955 | */ |
mistachkin | 533fb6d | 2013-08-28 02:26:48 | [diff] [blame] | 5956 | if ( winIsDirSep(zPathname[0]) ){ |
mistachkin | a112d14 | 2012-03-14 00:44:01 | [diff] [blame] | 5957 | return TRUE; |
| 5958 | } |
| 5959 | |
| 5960 | /* |
| 5961 | ** If the path name starts with a letter and a colon it is either a volume |
| 5962 | ** relative path or an absolute path. Callers of this function must not |
| 5963 | ** attempt to treat it as a relative path name (i.e. they should simply use |
| 5964 | ** it verbatim). |
| 5965 | */ |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 5966 | if ( winIsDriveLetterAndColon(zPathname) ){ |
mistachkin | a112d14 | 2012-03-14 00:44:01 | [diff] [blame] | 5967 | return TRUE; |
| 5968 | } |
| 5969 | |
| 5970 | /* |
| 5971 | ** If we get to this point, the path name should almost certainly be a purely |
| 5972 | ** relative one (i.e. not a UNC name, not absolute, and not volume relative). |
| 5973 | */ |
| 5974 | return FALSE; |
| 5975 | } |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 5976 | #endif /* _WIN32 */ |
| 5977 | |
| 5978 | #ifdef __CYGWIN__ |
| 5979 | /* |
| 5980 | ** Simplify a filename into its canonical form |
| 5981 | ** by making the following changes: |
| 5982 | ** |
| 5983 | ** * convert any '/' to '\' (win32) or reverse (Cygwin) |
| 5984 | ** * removing any trailing and duplicate / (except for UNC paths) |
| 5985 | ** * convert /./ into just / |
| 5986 | ** |
| 5987 | ** Changes are made in-place. Return the new name length. |
| 5988 | ** |
| 5989 | ** The original filename is in z[0..]. If the path is shortened, |
| 5990 | ** no-longer used bytes will be written by '\0'. |
| 5991 | */ |
| 5992 | static void winSimplifyName(char *z){ |
| 5993 | int i, j; |
| 5994 | for(i=j=0; z[i]; ++i){ |
| 5995 | if( winIsDirSep(z[i]) ){ |
| 5996 | #if !defined(SQLITE_TEST) |
| 5997 | /* Some test-cases assume that "./foo" and "foo" are different */ |
| 5998 | if( z[i+1]=='.' && winIsDirSep(z[i+2]) ){ |
| 5999 | ++i; |
| 6000 | continue; |
| 6001 | } |
| 6002 | #endif |
| 6003 | if( !z[i+1] || (winIsDirSep(z[i+1]) && (i!=0)) ){ |
| 6004 | continue; |
| 6005 | } |
| 6006 | z[j++] = osGetenv?'/':'\\'; |
| 6007 | }else{ |
| 6008 | z[j++] = z[i]; |
| 6009 | } |
| 6010 | } |
| 6011 | while(j<i) z[j++] = '\0'; |
| 6012 | } |
| 6013 | |
| 6014 | #define SQLITE_MAX_SYMLINKS 100 |
| 6015 | |
| 6016 | static int mkFullPathname( |
| 6017 | const char *zPath, /* Input path */ |
| 6018 | char *zOut, /* Output buffer */ |
| 6019 | int nOut /* Allocated size of buffer zOut */ |
| 6020 | ){ |
| 6021 | int nPath = sqlite3Strlen30(zPath); |
| 6022 | int iOff = 0; |
| 6023 | if( zPath[0]!='/' ){ |
| 6024 | if( osGetcwd(zOut, nOut-2)==0 ){ |
| 6025 | return winLogError(SQLITE_CANTOPEN_BKPT, (DWORD)osErrno, "getcwd", zPath); |
| 6026 | } |
| 6027 | iOff = sqlite3Strlen30(zOut); |
| 6028 | zOut[iOff++] = '/'; |
| 6029 | } |
| 6030 | if( (iOff+nPath+1)>nOut ){ |
| 6031 | /* SQLite assumes that xFullPathname() nul-terminates the output buffer |
| 6032 | ** even if it returns an error. */ |
| 6033 | zOut[iOff] = '\0'; |
| 6034 | return SQLITE_CANTOPEN_BKPT; |
| 6035 | } |
| 6036 | sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath); |
| 6037 | return SQLITE_OK; |
| 6038 | } |
| 6039 | #endif /* __CYGWIN__ */ |
mistachkin | a112d14 | 2012-03-14 00:44:01 | [diff] [blame] | 6040 | |
| 6041 | /* |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6042 | ** Turn a relative pathname into a full pathname. Write the full |
| 6043 | ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname |
| 6044 | ** bytes in size. |
| 6045 | */ |
drh | fee6431 | 2022-09-02 11:12:16 | [diff] [blame] | 6046 | static int winFullPathnameNoMutex( |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 | [diff] [blame] | 6047 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 6048 | const char *zRelative, /* Possibly relative input path */ |
| 6049 | int nFull, /* Size of output buffer in bytes */ |
| 6050 | char *zFull /* Output buffer */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6051 | ){ |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 6052 | #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT |
| 6053 | int nByte; |
mistachkin | 4a58d5f | 2016-06-06 20:36:26 | [diff] [blame] | 6054 | void *zConverted; |
| 6055 | char *zOut; |
| 6056 | #endif |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 6057 | |
mistachkin | a8e41ec | 2020-05-15 01:18:07 | [diff] [blame] | 6058 | /* If this path name begins with "/X:" or "\\?\", where "X" is any |
| 6059 | ** alphabetic character, discard the initial "/" from the pathname. |
drh | 8a5fd26 | 2016-06-06 20:27:15 | [diff] [blame] | 6060 | */ |
mistachkin | a8e41ec | 2020-05-15 01:18:07 | [diff] [blame] | 6061 | if( zRelative[0]=='/' && (winIsDriveLetterAndColon(zRelative+1) |
| 6062 | || winIsLongPathPrefix(zRelative+1)) ){ |
drh | 8a5fd26 | 2016-06-06 20:27:15 | [diff] [blame] | 6063 | zRelative++; |
| 6064 | } |
| 6065 | |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 6066 | SimulateIOError( return SQLITE_ERROR ); |
| 6067 | |
| 6068 | #ifdef __CYGWIN__ |
| 6069 | if( osGetcwd ){ |
| 6070 | zFull[nFull-1] = '\0'; |
| 6071 | if( !winIsDriveLetterAndColon(zRelative) || !winIsDirSep(zRelative[2]) ){ |
| 6072 | int rc = SQLITE_OK; |
| 6073 | int nLink = 1; /* Number of symbolic links followed so far */ |
| 6074 | const char *zIn = zRelative; /* Input path for each iteration of loop */ |
| 6075 | char *zDel = 0; |
| 6076 | struct stat buf; |
| 6077 | |
| 6078 | UNUSED_PARAMETER(pVfs); |
| 6079 | |
| 6080 | do { |
| 6081 | /* Call lstat() on path zIn. Set bLink to true if the path is a symbolic |
| 6082 | ** link, or false otherwise. */ |
| 6083 | int bLink = 0; |
| 6084 | if( osLstat && osReadlink ) { |
| 6085 | if( osLstat(zIn, &buf)!=0 ){ |
| 6086 | int myErrno = osErrno; |
| 6087 | if( myErrno!=ENOENT ){ |
| 6088 | rc = winLogError(SQLITE_CANTOPEN_BKPT, (DWORD)myErrno, "lstat", zIn); |
| 6089 | } |
| 6090 | }else{ |
| 6091 | bLink = ((buf.st_mode & 0170000) == 0120000); |
| 6092 | } |
| 6093 | |
| 6094 | if( bLink ){ |
| 6095 | if( zDel==0 ){ |
| 6096 | zDel = sqlite3MallocZero(nFull); |
| 6097 | if( zDel==0 ) rc = SQLITE_NOMEM; |
| 6098 | }else if( ++nLink>SQLITE_MAX_SYMLINKS ){ |
| 6099 | rc = SQLITE_CANTOPEN_BKPT; |
| 6100 | } |
| 6101 | |
| 6102 | if( rc==SQLITE_OK ){ |
| 6103 | nByte = osReadlink(zIn, zDel, nFull-1); |
| 6104 | if( nByte ==(DWORD)-1 ){ |
| 6105 | rc = winLogError(SQLITE_CANTOPEN_BKPT, (DWORD)osErrno, "readlink", zIn); |
| 6106 | }else{ |
| 6107 | if( zDel[0]!='/' ){ |
| 6108 | int n; |
| 6109 | for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--); |
| 6110 | if( nByte+n+1>nFull ){ |
| 6111 | rc = SQLITE_CANTOPEN_BKPT; |
| 6112 | }else{ |
| 6113 | memmove(&zDel[n], zDel, nByte+1); |
| 6114 | memcpy(zDel, zIn, n); |
| 6115 | nByte += n; |
| 6116 | } |
| 6117 | } |
| 6118 | zDel[nByte] = '\0'; |
| 6119 | } |
| 6120 | } |
| 6121 | |
| 6122 | zIn = zDel; |
| 6123 | } |
| 6124 | } |
| 6125 | |
| 6126 | assert( rc!=SQLITE_OK || zIn!=zFull || zIn[0]=='/' ); |
| 6127 | if( rc==SQLITE_OK && zIn!=zFull ){ |
| 6128 | rc = mkFullPathname(zIn, zFull, nFull); |
| 6129 | } |
| 6130 | if( bLink==0 ) break; |
| 6131 | zIn = zFull; |
| 6132 | }while( rc==SQLITE_OK ); |
| 6133 | |
| 6134 | sqlite3_free(zDel); |
| 6135 | winSimplifyName(zFull); |
| 6136 | return rc; |
| 6137 | } |
| 6138 | } |
| 6139 | #endif /* __CYGWIN__ */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6140 | |
stephan | 7b9407a | 2025-03-07 06:54:04 | [diff] [blame] | 6141 | #if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && defined(_WIN32) |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 6142 | SimulateIOError( return SQLITE_ERROR ); |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6143 | /* WinCE has no concept of a relative pathname, or so I am told. */ |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 6144 | /* WinRT has no way to convert a relative path to an absolute one. */ |
mistachkin | a112d14 | 2012-03-14 00:44:01 | [diff] [blame] | 6145 | if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){ |
| 6146 | /* |
| 6147 | ** NOTE: We are dealing with a relative path name and the data |
| 6148 | ** directory has been set. Therefore, use it as the basis |
| 6149 | ** for converting the relative path name to an absolute |
| 6150 | ** one by prepending the data directory and a backslash. |
| 6151 | */ |
mistachkin | 14eca4e | 2013-11-07 22:11:55 | [diff] [blame] | 6152 | sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s", |
| 6153 | sqlite3_data_directory, winGetDirSep(), zRelative); |
mistachkin | a112d14 | 2012-03-14 00:44:01 | [diff] [blame] | 6154 | }else{ |
| 6155 | sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative); |
| 6156 | } |
drh | e825609 | 2007-10-09 15:20:39 | [diff] [blame] | 6157 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6158 | #endif |
| 6159 | |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 6160 | #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT |
| 6161 | #if defined(_WIN32) |
shaneh | e2ad931 | 2010-07-08 03:13:33 | [diff] [blame] | 6162 | /* It's odd to simulate an io-error here, but really this is just |
| 6163 | ** using the io-error infrastructure to test that SQLite handles this |
| 6164 | ** function failing. This function could fail if, for example, the |
| 6165 | ** current working directory has been unlinked. |
| 6166 | */ |
| 6167 | SimulateIOError( return SQLITE_ERROR ); |
mistachkin | a112d14 | 2012-03-14 00:44:01 | [diff] [blame] | 6168 | if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){ |
| 6169 | /* |
| 6170 | ** NOTE: We are dealing with a relative path name and the data |
| 6171 | ** directory has been set. Therefore, use it as the basis |
| 6172 | ** for converting the relative path name to an absolute |
| 6173 | ** one by prepending the data directory and a backslash. |
| 6174 | */ |
mistachkin | 14eca4e | 2013-11-07 22:11:55 | [diff] [blame] | 6175 | sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s", |
| 6176 | sqlite3_data_directory, winGetDirSep(), zRelative); |
mistachkin | a112d14 | 2012-03-14 00:44:01 | [diff] [blame] | 6177 | return SQLITE_OK; |
| 6178 | } |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 6179 | #endif |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 6180 | zConverted = winConvertFromUtf8Filename(zRelative); |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 6181 | if( zConverted==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 6182 | return SQLITE_IOERR_NOMEM_BKPT; |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 6183 | } |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 6184 | if( osIsNT() ){ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 6185 | LPWSTR zTemp; |
mistachkin | 7ea11af | 2012-09-13 15:24:29 | [diff] [blame] | 6186 | nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0); |
| 6187 | if( nByte==0 ){ |
mistachkin | 7ea11af | 2012-09-13 15:24:29 | [diff] [blame] | 6188 | sqlite3_free(zConverted); |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 6189 | return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(), |
| 6190 | "winFullPathname1", zRelative); |
mistachkin | 7ea11af | 2012-09-13 15:24:29 | [diff] [blame] | 6191 | } |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 6192 | nByte += 3; |
| 6193 | zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) ); |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6194 | if( zTemp==0 ){ |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 6195 | sqlite3_free(zConverted); |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 6196 | return SQLITE_IOERR_NOMEM_BKPT; |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6197 | } |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 6198 | nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0); |
mistachkin | 7ea11af | 2012-09-13 15:24:29 | [diff] [blame] | 6199 | if( nByte==0 ){ |
mistachkin | 7ea11af | 2012-09-13 15:24:29 | [diff] [blame] | 6200 | sqlite3_free(zConverted); |
| 6201 | sqlite3_free(zTemp); |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 6202 | return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(), |
| 6203 | "winFullPathname2", zRelative); |
mistachkin | 7ea11af | 2012-09-13 15:24:29 | [diff] [blame] | 6204 | } |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 6205 | sqlite3_free(zConverted); |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 6206 | zOut = winUnicodeToUtf8(zTemp); |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 6207 | sqlite3_free(zTemp); |
drh | d52ee72 | 2012-03-02 00:00:47 | [diff] [blame] | 6208 | } |
| 6209 | #ifdef SQLITE_WIN32_HAS_ANSI |
| 6210 | else{ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6211 | char *zTemp; |
mistachkin | 7ea11af | 2012-09-13 15:24:29 | [diff] [blame] | 6212 | nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0); |
| 6213 | if( nByte==0 ){ |
mistachkin | 7ea11af | 2012-09-13 15:24:29 | [diff] [blame] | 6214 | sqlite3_free(zConverted); |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 6215 | return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(), |
| 6216 | "winFullPathname3", zRelative); |
mistachkin | 7ea11af | 2012-09-13 15:24:29 | [diff] [blame] | 6217 | } |
drh | ef86b94 | 2025-02-17 17:33:14 | [diff] [blame] | 6218 | zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) + 3*sizeof(zTemp[0]) ); |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6219 | if( zTemp==0 ){ |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 6220 | sqlite3_free(zConverted); |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 6221 | return SQLITE_IOERR_NOMEM_BKPT; |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6222 | } |
drh | ef86b94 | 2025-02-17 17:33:14 | [diff] [blame] | 6223 | nByte = osGetFullPathNameA((char*)zConverted, nByte+3, zTemp, 0); |
mistachkin | 7ea11af | 2012-09-13 15:24:29 | [diff] [blame] | 6224 | if( nByte==0 ){ |
mistachkin | 7ea11af | 2012-09-13 15:24:29 | [diff] [blame] | 6225 | sqlite3_free(zConverted); |
| 6226 | sqlite3_free(zTemp); |
mistachkin | 9f11ef1 | 2013-08-31 02:48:56 | [diff] [blame] | 6227 | return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(), |
| 6228 | "winFullPathname4", zRelative); |
mistachkin | 7ea11af | 2012-09-13 15:24:29 | [diff] [blame] | 6229 | } |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 6230 | sqlite3_free(zConverted); |
mistachkin | 1293120 | 2016-04-04 02:05:46 | [diff] [blame] | 6231 | zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI()); |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 6232 | sqlite3_free(zTemp); |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6233 | } |
drh | d52ee72 | 2012-03-02 00:00:47 | [diff] [blame] | 6234 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6235 | if( zOut ){ |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 6236 | #ifdef __CYGWIN__ |
| 6237 | if( memcmp(zOut, "\\\\?\\", 4) ){ |
| 6238 | sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut); |
| 6239 | }else if( memcmp(zOut+4, "UNC\\", 4) ){ |
| 6240 | sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut+4); |
| 6241 | }else{ |
| 6242 | char *p = zOut+6; |
| 6243 | *p = '\\'; |
| 6244 | if( osGetcwd ){ |
| 6245 | /* On Cygwin, UNC paths use forward slashes */ |
| 6246 | while( *p ){ |
| 6247 | if( *p=='\\' ) *p = '/'; |
| 6248 | ++p; |
| 6249 | } |
| 6250 | } |
| 6251 | sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut+6); |
| 6252 | } |
| 6253 | #else |
mistachkin | a112d14 | 2012-03-14 00:44:01 | [diff] [blame] | 6254 | sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut); |
stephan | 42e5ceb | 2025-03-10 15:15:13 | [diff] [blame] | 6255 | #endif /* __CYGWIN__ */ |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 6256 | sqlite3_free(zOut); |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6257 | return SQLITE_OK; |
| 6258 | }else{ |
mistachkin | fad3039 | 2016-02-13 23:43:46 | [diff] [blame] | 6259 | return SQLITE_IOERR_NOMEM_BKPT; |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6260 | } |
| 6261 | #endif |
| 6262 | } |
drh | fee6431 | 2022-09-02 11:12:16 | [diff] [blame] | 6263 | static int winFullPathname( |
| 6264 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 6265 | const char *zRelative, /* Possibly relative input path */ |
| 6266 | int nFull, /* Size of output buffer in bytes */ |
| 6267 | char *zFull /* Output buffer */ |
| 6268 | ){ |
| 6269 | int rc; |
mistachkin | bdb6368 | 2022-09-09 17:50:29 | [diff] [blame] | 6270 | MUTEX_LOGIC( sqlite3_mutex *pMutex; ) |
| 6271 | MUTEX_LOGIC( pMutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR); ) |
drh | fee6431 | 2022-09-02 11:12:16 | [diff] [blame] | 6272 | sqlite3_mutex_enter(pMutex); |
| 6273 | rc = winFullPathnameNoMutex(pVfs, zRelative, nFull, zFull); |
| 6274 | sqlite3_mutex_leave(pMutex); |
| 6275 | return rc; |
| 6276 | } |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6277 | |
| 6278 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | 761df87 | 2006-12-21 01:29:22 | [diff] [blame] | 6279 | /* |
| 6280 | ** Interfaces for opening a shared library, finding entry points |
| 6281 | ** within the shared library, and closing the shared library. |
| 6282 | */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6283 | static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){ |
drh | 761df87 | 2006-12-21 01:29:22 | [diff] [blame] | 6284 | HANDLE h; |
mistachkin | d95a3d3 | 2013-08-30 21:52:38 | [diff] [blame] | 6285 | void *zConverted = winConvertFromUtf8Filename(zFilename); |
drh | 761df87 | 2006-12-21 01:29:22 | [diff] [blame] | 6286 | UNUSED_PARAMETER(pVfs); |
drh | 761df87 | 2006-12-21 01:29:22 | [diff] [blame] | 6287 | if( zConverted==0 ){ |
mistachkin | 1925a2e | 2014-02-24 21:20:25 | [diff] [blame] | 6288 | OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0)); |
drh | 761df87 | 2006-12-21 01:29:22 | [diff] [blame] | 6289 | return 0; |
| 6290 | } |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 6291 | if( osIsNT() ){ |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 6292 | #if SQLITE_OS_WINRT |
mistachkin | 5483f77 | 2012-03-07 20:11:47 | [diff] [blame] | 6293 | h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0); |
| 6294 | #else |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 6295 | h = osLoadLibraryW((LPCWSTR)zConverted); |
mistachkin | 5483f77 | 2012-03-07 20:11:47 | [diff] [blame] | 6296 | #endif |
drh | 761df87 | 2006-12-21 01:29:22 | [diff] [blame] | 6297 | } |
drh | d52ee72 | 2012-03-02 00:00:47 | [diff] [blame] | 6298 | #ifdef SQLITE_WIN32_HAS_ANSI |
| 6299 | else{ |
| 6300 | h = osLoadLibraryA((char*)zConverted); |
| 6301 | } |
| 6302 | #endif |
mistachkin | 1925a2e | 2014-02-24 21:20:25 | [diff] [blame] | 6303 | OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)h)); |
mistachkin | 5f07538 | 2011-11-11 23:31:04 | [diff] [blame] | 6304 | sqlite3_free(zConverted); |
drh | 761df87 | 2006-12-21 01:29:22 | [diff] [blame] | 6305 | return (void*)h; |
drh | 761df87 | 2006-12-21 01:29:22 | [diff] [blame] | 6306 | } |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6307 | static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){ |
drh | 1bd10f8 | 2008-12-10 21:19:56 | [diff] [blame] | 6308 | UNUSED_PARAMETER(pVfs); |
mistachkin | b324bc7 | 2013-08-28 02:37:29 | [diff] [blame] | 6309 | winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut); |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6310 | } |
drh | 1488052 | 2013-03-01 23:24:04 | [diff] [blame] | 6311 | static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){ |
mistachkin | 151d05c | 2014-02-24 21:42:33 | [diff] [blame] | 6312 | FARPROC proc; |
drh | 1bd10f8 | 2008-12-10 21:19:56 | [diff] [blame] | 6313 | UNUSED_PARAMETER(pVfs); |
mistachkin | 151d05c | 2014-02-24 21:42:33 | [diff] [blame] | 6314 | proc = osGetProcAddressA((HANDLE)pH, zSym); |
| 6315 | OSTRACE(("DLSYM handle=%p, symbol=%s, address=%p\n", |
| 6316 | (void*)pH, zSym, (void*)proc)); |
| 6317 | return (void(*)(void))proc; |
drh | 761df87 | 2006-12-21 01:29:22 | [diff] [blame] | 6318 | } |
drh | 134c4ff | 2011-10-13 14:05:32 | [diff] [blame] | 6319 | static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){ |
drh | 1bd10f8 | 2008-12-10 21:19:56 | [diff] [blame] | 6320 | UNUSED_PARAMETER(pVfs); |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 6321 | osFreeLibrary((HANDLE)pHandle); |
mistachkin | 1925a2e | 2014-02-24 21:20:25 | [diff] [blame] | 6322 | OSTRACE(("DLCLOSE handle=%p\n", (void*)pHandle)); |
drh | 761df87 | 2006-12-21 01:29:22 | [diff] [blame] | 6323 | } |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6324 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 6325 | #define winDlOpen 0 |
| 6326 | #define winDlError 0 |
| 6327 | #define winDlSym 0 |
| 6328 | #define winDlClose 0 |
| 6329 | #endif |
| 6330 | |
drh | 8eb3790 | 2016-02-15 20:41:56 | [diff] [blame] | 6331 | /* State information for the randomness gatherer. */ |
| 6332 | typedef struct EntropyGatherer EntropyGatherer; |
| 6333 | struct EntropyGatherer { |
| 6334 | unsigned char *a; /* Gather entropy into this buffer */ |
| 6335 | int na; /* Size of a[] in bytes */ |
| 6336 | int i; /* XOR next input into a[i] */ |
| 6337 | int nXor; /* Number of XOR operations done */ |
| 6338 | }; |
| 6339 | |
| 6340 | #if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) |
| 6341 | /* Mix sz bytes of entropy into p. */ |
| 6342 | static void xorMemory(EntropyGatherer *p, unsigned char *x, int sz){ |
| 6343 | int j, k; |
| 6344 | for(j=0, k=p->i; j<sz; j++){ |
| 6345 | p->a[k++] ^= x[j]; |
| 6346 | if( k>=p->na ) k = 0; |
| 6347 | } |
| 6348 | p->i = k; |
| 6349 | p->nXor += sz; |
| 6350 | } |
mistachkin | b71aef3 | 2016-02-15 22:37:18 | [diff] [blame] | 6351 | #endif /* !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) */ |
drh | 761df87 | 2006-12-21 01:29:22 | [diff] [blame] | 6352 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 | [diff] [blame] | 6353 | /* |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6354 | ** Write up to nBuf bytes of randomness into zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 6355 | */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6356 | static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ |
drh | 6a412b8 | 2015-04-30 12:31:49 | [diff] [blame] | 6357 | #if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) |
drh | 8eb3790 | 2016-02-15 20:41:56 | [diff] [blame] | 6358 | UNUSED_PARAMETER(pVfs); |
shane | c7b7f1a | 2008-11-19 21:35:46 | [diff] [blame] | 6359 | memset(zBuf, 0, nBuf); |
drh | 8eb3790 | 2016-02-15 20:41:56 | [diff] [blame] | 6360 | return nBuf; |
shane | c7b7f1a | 2008-11-19 21:35:46 | [diff] [blame] | 6361 | #else |
drh | 8eb3790 | 2016-02-15 20:41:56 | [diff] [blame] | 6362 | EntropyGatherer e; |
| 6363 | UNUSED_PARAMETER(pVfs); |
| 6364 | memset(zBuf, 0, nBuf); |
| 6365 | e.a = (unsigned char*)zBuf; |
| 6366 | e.na = nBuf; |
| 6367 | e.nXor = 0; |
| 6368 | e.i = 0; |
| 6369 | { |
drh | d1a7931 | 2007-09-03 13:06:11 | [diff] [blame] | 6370 | SYSTEMTIME x; |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 6371 | osGetSystemTime(&x); |
mistachkin | b71aef3 | 2016-02-15 22:37:18 | [diff] [blame] | 6372 | xorMemory(&e, (unsigned char*)&x, sizeof(SYSTEMTIME)); |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6373 | } |
drh | 8eb3790 | 2016-02-15 20:41:56 | [diff] [blame] | 6374 | { |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 6375 | DWORD pid = osGetCurrentProcessId(); |
mistachkin | b71aef3 | 2016-02-15 22:37:18 | [diff] [blame] | 6376 | xorMemory(&e, (unsigned char*)&pid, sizeof(DWORD)); |
drh | d1a7931 | 2007-09-03 13:06:11 | [diff] [blame] | 6377 | } |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 6378 | #if SQLITE_OS_WINRT |
drh | 8eb3790 | 2016-02-15 20:41:56 | [diff] [blame] | 6379 | { |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 6380 | ULONGLONG cnt = osGetTickCount64(); |
mistachkin | b71aef3 | 2016-02-15 22:37:18 | [diff] [blame] | 6381 | xorMemory(&e, (unsigned char*)&cnt, sizeof(ULONGLONG)); |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 6382 | } |
| 6383 | #else |
drh | 8eb3790 | 2016-02-15 20:41:56 | [diff] [blame] | 6384 | { |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 6385 | DWORD cnt = osGetTickCount(); |
mistachkin | b71aef3 | 2016-02-15 22:37:18 | [diff] [blame] | 6386 | xorMemory(&e, (unsigned char*)&cnt, sizeof(DWORD)); |
drh | d1a7931 | 2007-09-03 13:06:11 | [diff] [blame] | 6387 | } |
mistachkin | b71aef3 | 2016-02-15 22:37:18 | [diff] [blame] | 6388 | #endif /* SQLITE_OS_WINRT */ |
drh | 8eb3790 | 2016-02-15 20:41:56 | [diff] [blame] | 6389 | { |
drh | d1a7931 | 2007-09-03 13:06:11 | [diff] [blame] | 6390 | LARGE_INTEGER i; |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 6391 | osQueryPerformanceCounter(&i); |
mistachkin | b71aef3 | 2016-02-15 22:37:18 | [diff] [blame] | 6392 | xorMemory(&e, (unsigned char*)&i, sizeof(LARGE_INTEGER)); |
drh | d1a7931 | 2007-09-03 13:06:11 | [diff] [blame] | 6393 | } |
mistachkin | e45e0fb | 2015-01-21 00:48:46 | [diff] [blame] | 6394 | #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID |
drh | 8eb3790 | 2016-02-15 20:41:56 | [diff] [blame] | 6395 | { |
mistachkin | e45e0fb | 2015-01-21 00:48:46 | [diff] [blame] | 6396 | UUID id; |
| 6397 | memset(&id, 0, sizeof(UUID)); |
| 6398 | osUuidCreate(&id); |
drh | 8eb3790 | 2016-02-15 20:41:56 | [diff] [blame] | 6399 | xorMemory(&e, (unsigned char*)&id, sizeof(UUID)); |
mistachkin | e45e0fb | 2015-01-21 00:48:46 | [diff] [blame] | 6400 | memset(&id, 0, sizeof(UUID)); |
| 6401 | osUuidCreateSequential(&id); |
drh | 8eb3790 | 2016-02-15 20:41:56 | [diff] [blame] | 6402 | xorMemory(&e, (unsigned char*)&id, sizeof(UUID)); |
mistachkin | e45e0fb | 2015-01-21 00:48:46 | [diff] [blame] | 6403 | } |
mistachkin | b71aef3 | 2016-02-15 22:37:18 | [diff] [blame] | 6404 | #endif /* !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID */ |
drh | 8eb3790 | 2016-02-15 20:41:56 | [diff] [blame] | 6405 | return e.nXor>nBuf ? nBuf : e.nXor; |
mistachkin | 0ead47d | 2016-02-15 22:28:50 | [diff] [blame] | 6406 | #endif /* defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) */ |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 6407 | } |
| 6408 | |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6409 | |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 6410 | /* |
| 6411 | ** Sleep for a little while. Return the amount of time slept. |
| 6412 | */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6413 | static int winSleep(sqlite3_vfs *pVfs, int microsec){ |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 6414 | sqlite3_win32_sleep((microsec+999)/1000); |
drh | 1bd10f8 | 2008-12-10 21:19:56 | [diff] [blame] | 6415 | UNUSED_PARAMETER(pVfs); |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6416 | return ((microsec+999)/1000)*1000; |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 6417 | } |
| 6418 | |
| 6419 | /* |
shaneh | 04882a9 | 2010-05-11 02:49:39 | [diff] [blame] | 6420 | ** The following variable, if set to a non-zero value, is interpreted as |
| 6421 | ** the number of seconds since 1970 and is used to set the result of |
| 6422 | ** sqlite3OsCurrentTime() during testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 6423 | */ |
| 6424 | #ifdef SQLITE_TEST |
shaneh | 04882a9 | 2010-05-11 02:49:39 | [diff] [blame] | 6425 | int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 6426 | #endif |
| 6427 | |
| 6428 | /* |
shaneh | 04882a9 | 2010-05-11 02:49:39 | [diff] [blame] | 6429 | ** Find the current time (in Universal Coordinated Time). Write into *piNow |
| 6430 | ** the current time and date as a Julian Day number times 86_400_000. In |
| 6431 | ** other words, write into *piNow the number of milliseconds since the Julian |
| 6432 | ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 6433 | ** proleptic Gregorian calendar. |
| 6434 | ** |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 6435 | ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date |
drh | 3170225 | 2011-10-12 23:13:43 | [diff] [blame] | 6436 | ** cannot be found. |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 6437 | */ |
shaneh | 04882a9 | 2010-05-11 02:49:39 | [diff] [blame] | 6438 | static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){ |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 6439 | /* FILETIME structure is a 64-bit value representing the number of |
| 6440 | 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5). |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 6441 | */ |
shaneh | 04882a9 | 2010-05-11 02:49:39 | [diff] [blame] | 6442 | FILETIME ft; |
| 6443 | static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000; |
| 6444 | #ifdef SQLITE_TEST |
| 6445 | static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
| 6446 | #endif |
shane | b08a67a | 2009-03-31 03:41:56 | [diff] [blame] | 6447 | /* 2^32 - to avoid use of LL and warnings in gcc */ |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 6448 | static const sqlite3_int64 max32BitValue = |
drh | 1488052 | 2013-03-01 23:24:04 | [diff] [blame] | 6449 | (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 + |
| 6450 | (sqlite3_int64)294967296; |
drh | 5c905d6 | 2009-03-30 12:42:45 | [diff] [blame] | 6451 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 | [diff] [blame] | 6452 | #if SQLITE_OS_WINCE |
drh | cc78fea | 2006-01-06 16:17:05 | [diff] [blame] | 6453 | SYSTEMTIME time; |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 6454 | osGetSystemTime(&time); |
shane | 820800d | 2008-07-22 05:32:03 | [diff] [blame] | 6455 | /* if SystemTimeToFileTime() fails, it returns zero. */ |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 6456 | if (!osSystemTimeToFileTime(&time,&ft)){ |
drh | 3170225 | 2011-10-12 23:13:43 | [diff] [blame] | 6457 | return SQLITE_ERROR; |
shane | 820800d | 2008-07-22 05:32:03 | [diff] [blame] | 6458 | } |
drh | cc78fea | 2006-01-06 16:17:05 | [diff] [blame] | 6459 | #else |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 6460 | osGetSystemTimeAsFileTime( &ft ); |
drh | cc78fea | 2006-01-06 16:17:05 | [diff] [blame] | 6461 | #endif |
shaneh | 04882a9 | 2010-05-11 02:49:39 | [diff] [blame] | 6462 | |
| 6463 | *piNow = winFiletimeEpoch + |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 6464 | ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) + |
drh | b316cb2 | 2010-06-23 15:18:11 | [diff] [blame] | 6465 | (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000; |
shaneh | 04882a9 | 2010-05-11 02:49:39 | [diff] [blame] | 6466 | |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 6467 | #ifdef SQLITE_TEST |
| 6468 | if( sqlite3_current_time ){ |
shaneh | 04882a9 | 2010-05-11 02:49:39 | [diff] [blame] | 6469 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 6470 | } |
| 6471 | #endif |
shaneh | 04882a9 | 2010-05-11 02:49:39 | [diff] [blame] | 6472 | UNUSED_PARAMETER(pVfs); |
drh | 3170225 | 2011-10-12 23:13:43 | [diff] [blame] | 6473 | return SQLITE_OK; |
drh | bbd42a6 | 2004-05-22 17:41:58 | [diff] [blame] | 6474 | } |
| 6475 | |
shane | 820800d | 2008-07-22 05:32:03 | [diff] [blame] | 6476 | /* |
shaneh | 04882a9 | 2010-05-11 02:49:39 | [diff] [blame] | 6477 | ** Find the current time (in Universal Coordinated Time). Write the |
| 6478 | ** current time and date as a Julian Day number into *prNow and |
| 6479 | ** return 0. Return 1 if the time and date cannot be found. |
| 6480 | */ |
drh | 134c4ff | 2011-10-13 14:05:32 | [diff] [blame] | 6481 | static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){ |
shaneh | 04882a9 | 2010-05-11 02:49:39 | [diff] [blame] | 6482 | int rc; |
| 6483 | sqlite3_int64 i; |
| 6484 | rc = winCurrentTimeInt64(pVfs, &i); |
| 6485 | if( !rc ){ |
| 6486 | *prNow = i/86400000.0; |
| 6487 | } |
| 6488 | return rc; |
| 6489 | } |
| 6490 | |
| 6491 | /* |
shane | 820800d | 2008-07-22 05:32:03 | [diff] [blame] | 6492 | ** The idea is that this function works like a combination of |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 6493 | ** GetLastError() and FormatMessage() on Windows (or errno and |
| 6494 | ** strerror_r() on Unix). After an error is returned by an OS |
shane | 820800d | 2008-07-22 05:32:03 | [diff] [blame] | 6495 | ** function, SQLite calls this function with zBuf pointing to |
| 6496 | ** a buffer of nBuf bytes. The OS layer should populate the |
| 6497 | ** buffer with a nul-terminated UTF-8 encoded error message |
shane | be21779 | 2009-03-05 04:20:31 | [diff] [blame] | 6498 | ** describing the last IO error to have occurred within the calling |
shane | 820800d | 2008-07-22 05:32:03 | [diff] [blame] | 6499 | ** thread. |
| 6500 | ** |
| 6501 | ** If the error message is too large for the supplied buffer, |
| 6502 | ** it should be truncated. The return value of xGetLastError |
| 6503 | ** is zero if the error message fits in the buffer, or non-zero |
| 6504 | ** otherwise (if the message was truncated). If non-zero is returned, |
| 6505 | ** then it is not necessary to include the nul-terminator character |
| 6506 | ** in the output buffer. |
| 6507 | ** |
| 6508 | ** Not supplying an error message will have no adverse effect |
| 6509 | ** on SQLite. It is fine to have an implementation that never |
| 6510 | ** returns an error message: |
| 6511 | ** |
| 6512 | ** int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ |
| 6513 | ** assert(zBuf[0]=='\0'); |
| 6514 | ** return 0; |
| 6515 | ** } |
| 6516 | ** |
| 6517 | ** However if an error message is supplied, it will be incorporated |
| 6518 | ** by sqlite into the error message available to the user using |
| 6519 | ** sqlite3_errmsg(), possibly making IO errors easier to debug. |
| 6520 | */ |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 | [diff] [blame] | 6521 | static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ |
drh | 762e32b | 2016-03-17 19:28:19 | [diff] [blame] | 6522 | DWORD e = osGetLastError(); |
drh | 1bd10f8 | 2008-12-10 21:19:56 | [diff] [blame] | 6523 | UNUSED_PARAMETER(pVfs); |
drh | 762e32b | 2016-03-17 19:28:19 | [diff] [blame] | 6524 | if( nBuf>0 ) winGetLastErrorMsg(e, nBuf, zBuf); |
| 6525 | return e; |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 | [diff] [blame] | 6526 | } |
drh | b4bc705 | 2006-01-11 23:40:33 | [diff] [blame] | 6527 | |
| 6528 | /* |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 | [diff] [blame] | 6529 | ** Initialize and deinitialize the operating system interface. |
danielk1977 | 13a68c3 | 2005-12-15 10:11:30 | [diff] [blame] | 6530 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 | [diff] [blame] | 6531 | int sqlite3_os_init(void){ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6532 | static sqlite3_vfs winVfs = { |
mistachkin | 1e75483 | 2016-07-08 21:14:37 | [diff] [blame] | 6533 | 3, /* iVersion */ |
| 6534 | sizeof(winFile), /* szOsFile */ |
mistachkin | 31706a2 | 2013-08-26 20:45:50 | [diff] [blame] | 6535 | SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */ |
mistachkin | 1e75483 | 2016-07-08 21:14:37 | [diff] [blame] | 6536 | 0, /* pNext */ |
| 6537 | "win32", /* zName */ |
| 6538 | &winAppData, /* pAppData */ |
| 6539 | winOpen, /* xOpen */ |
| 6540 | winDelete, /* xDelete */ |
| 6541 | winAccess, /* xAccess */ |
| 6542 | winFullPathname, /* xFullPathname */ |
| 6543 | winDlOpen, /* xDlOpen */ |
| 6544 | winDlError, /* xDlError */ |
| 6545 | winDlSym, /* xDlSym */ |
| 6546 | winDlClose, /* xDlClose */ |
| 6547 | winRandomness, /* xRandomness */ |
| 6548 | winSleep, /* xSleep */ |
| 6549 | winCurrentTime, /* xCurrentTime */ |
| 6550 | winGetLastError, /* xGetLastError */ |
| 6551 | winCurrentTimeInt64, /* xCurrentTimeInt64 */ |
| 6552 | winSetSystemCall, /* xSetSystemCall */ |
| 6553 | winGetSystemCall, /* xGetSystemCall */ |
| 6554 | winNextSystemCall, /* xNextSystemCall */ |
drh | 153c62c | 2007-08-24 03:51:33 | [diff] [blame] | 6555 | }; |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 6556 | #if defined(SQLITE_WIN32_HAS_WIDE) |
| 6557 | static sqlite3_vfs winLongPathVfs = { |
mistachkin | 1e75483 | 2016-07-08 21:14:37 | [diff] [blame] | 6558 | 3, /* iVersion */ |
| 6559 | sizeof(winFile), /* szOsFile */ |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 6560 | SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */ |
mistachkin | 1e75483 | 2016-07-08 21:14:37 | [diff] [blame] | 6561 | 0, /* pNext */ |
| 6562 | "win32-longpath", /* zName */ |
| 6563 | &winAppData, /* pAppData */ |
| 6564 | winOpen, /* xOpen */ |
| 6565 | winDelete, /* xDelete */ |
| 6566 | winAccess, /* xAccess */ |
| 6567 | winFullPathname, /* xFullPathname */ |
| 6568 | winDlOpen, /* xDlOpen */ |
| 6569 | winDlError, /* xDlError */ |
| 6570 | winDlSym, /* xDlSym */ |
| 6571 | winDlClose, /* xDlClose */ |
| 6572 | winRandomness, /* xRandomness */ |
| 6573 | winSleep, /* xSleep */ |
| 6574 | winCurrentTime, /* xCurrentTime */ |
| 6575 | winGetLastError, /* xGetLastError */ |
| 6576 | winCurrentTimeInt64, /* xCurrentTimeInt64 */ |
| 6577 | winSetSystemCall, /* xSetSystemCall */ |
| 6578 | winGetSystemCall, /* xGetSystemCall */ |
| 6579 | winNextSystemCall, /* xNextSystemCall */ |
| 6580 | }; |
| 6581 | #endif |
| 6582 | static sqlite3_vfs winNolockVfs = { |
| 6583 | 3, /* iVersion */ |
| 6584 | sizeof(winFile), /* szOsFile */ |
| 6585 | SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */ |
| 6586 | 0, /* pNext */ |
| 6587 | "win32-none", /* zName */ |
| 6588 | &winNolockAppData, /* pAppData */ |
| 6589 | winOpen, /* xOpen */ |
| 6590 | winDelete, /* xDelete */ |
| 6591 | winAccess, /* xAccess */ |
| 6592 | winFullPathname, /* xFullPathname */ |
| 6593 | winDlOpen, /* xDlOpen */ |
| 6594 | winDlError, /* xDlError */ |
| 6595 | winDlSym, /* xDlSym */ |
| 6596 | winDlClose, /* xDlClose */ |
| 6597 | winRandomness, /* xRandomness */ |
| 6598 | winSleep, /* xSleep */ |
| 6599 | winCurrentTime, /* xCurrentTime */ |
| 6600 | winGetLastError, /* xGetLastError */ |
| 6601 | winCurrentTimeInt64, /* xCurrentTimeInt64 */ |
| 6602 | winSetSystemCall, /* xSetSystemCall */ |
| 6603 | winGetSystemCall, /* xGetSystemCall */ |
| 6604 | winNextSystemCall, /* xNextSystemCall */ |
| 6605 | }; |
| 6606 | #if defined(SQLITE_WIN32_HAS_WIDE) |
| 6607 | static sqlite3_vfs winLongPathNolockVfs = { |
| 6608 | 3, /* iVersion */ |
| 6609 | sizeof(winFile), /* szOsFile */ |
| 6610 | SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */ |
| 6611 | 0, /* pNext */ |
| 6612 | "win32-longpath-none", /* zName */ |
| 6613 | &winNolockAppData, /* pAppData */ |
| 6614 | winOpen, /* xOpen */ |
| 6615 | winDelete, /* xDelete */ |
| 6616 | winAccess, /* xAccess */ |
| 6617 | winFullPathname, /* xFullPathname */ |
| 6618 | winDlOpen, /* xDlOpen */ |
| 6619 | winDlError, /* xDlError */ |
| 6620 | winDlSym, /* xDlSym */ |
| 6621 | winDlClose, /* xDlClose */ |
| 6622 | winRandomness, /* xRandomness */ |
| 6623 | winSleep, /* xSleep */ |
| 6624 | winCurrentTime, /* xCurrentTime */ |
| 6625 | winGetLastError, /* xGetLastError */ |
| 6626 | winCurrentTimeInt64, /* xCurrentTimeInt64 */ |
| 6627 | winSetSystemCall, /* xSetSystemCall */ |
| 6628 | winGetSystemCall, /* xGetSystemCall */ |
| 6629 | winNextSystemCall, /* xNextSystemCall */ |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 6630 | }; |
| 6631 | #endif |
dan | e1ab219 | 2009-08-17 15:16:19 | [diff] [blame] | 6632 | |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 6633 | /* Double-check that the aSyscall[] array has been constructed |
| 6634 | ** correctly. See ticket [bb3a86e890c8e96ab] */ |
stephan | 7b9407a | 2025-03-07 06:54:04 | [diff] [blame] | 6635 | assert( ArraySize(aSyscall)==89 ); |
drh | 7acec68 | 2012-03-01 21:19:39 | [diff] [blame] | 6636 | |
shaneh | 420398c | 2010-09-03 04:29:30 | [diff] [blame] | 6637 | /* get memory map allocation granularity */ |
| 6638 | memset(&winSysInfo, 0, sizeof(SYSTEM_INFO)); |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 6639 | #if SQLITE_OS_WINRT |
| 6640 | osGetNativeSystemInfo(&winSysInfo); |
| 6641 | #else |
mistachkin | 318507b | 2011-11-11 22:08:54 | [diff] [blame] | 6642 | osGetSystemInfo(&winSysInfo); |
mistachkin | df562d5 | 2012-03-13 01:16:57 | [diff] [blame] | 6643 | #endif |
mistachkin | daf9a5a | 2013-03-23 09:56:39 | [diff] [blame] | 6644 | assert( winSysInfo.dwAllocationGranularity>0 ); |
| 6645 | assert( winSysInfo.dwPageSize>0 ); |
shaneh | 420398c | 2010-09-03 04:29:30 | [diff] [blame] | 6646 | |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 | [diff] [blame] | 6647 | sqlite3_vfs_register(&winVfs, 1); |
mistachkin | 3741827 | 2013-08-28 05:49:39 | [diff] [blame] | 6648 | |
| 6649 | #if defined(SQLITE_WIN32_HAS_WIDE) |
| 6650 | sqlite3_vfs_register(&winLongPathVfs, 0); |
| 6651 | #endif |
| 6652 | |
mistachkin | 1e75483 | 2016-07-08 21:14:37 | [diff] [blame] | 6653 | sqlite3_vfs_register(&winNolockVfs, 0); |
| 6654 | |
| 6655 | #if defined(SQLITE_WIN32_HAS_WIDE) |
| 6656 | sqlite3_vfs_register(&winLongPathNolockVfs, 0); |
| 6657 | #endif |
| 6658 | |
mistachkin | bc6b8d7 | 2018-02-05 21:02:47 | [diff] [blame] | 6659 | #ifndef SQLITE_OMIT_WAL |
mistachkin | 435666e | 2018-02-05 20:42:50 | [diff] [blame] | 6660 | winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1); |
mistachkin | bc6b8d7 | 2018-02-05 21:02:47 | [diff] [blame] | 6661 | #endif |
| 6662 | |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 6663 | return SQLITE_OK; |
danielk1977 | 13a68c3 | 2005-12-15 10:11:30 | [diff] [blame] | 6664 | } |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 | [diff] [blame] | 6665 | |
mistachkin | 17835a5 | 2014-08-06 03:06:01 | [diff] [blame] | 6666 | int sqlite3_os_end(void){ |
drh | 7acec68 | 2012-03-01 21:19:39 | [diff] [blame] | 6667 | #if SQLITE_OS_WINRT |
mistachkin | c60941f | 2012-09-13 01:51:02 | [diff] [blame] | 6668 | if( sleepObj!=NULL ){ |
mistachkin | f4f327c | 2012-03-13 03:35:07 | [diff] [blame] | 6669 | osCloseHandle(sleepObj); |
| 6670 | sleepObj = NULL; |
| 6671 | } |
drh | 7acec68 | 2012-03-01 21:19:39 | [diff] [blame] | 6672 | #endif |
mistachkin | bc6b8d7 | 2018-02-05 21:02:47 | [diff] [blame] | 6673 | |
| 6674 | #ifndef SQLITE_OMIT_WAL |
mistachkin | 435666e | 2018-02-05 20:42:50 | [diff] [blame] | 6675 | winBigLock = 0; |
mistachkin | bc6b8d7 | 2018-02-05 21:02:47 | [diff] [blame] | 6676 | #endif |
| 6677 | |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 | [diff] [blame] | 6678 | return SQLITE_OK; |
| 6679 | } |
drh | 40257ff | 2008-06-13 18:24:27 | [diff] [blame] | 6680 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 | [diff] [blame] | 6681 | #endif /* SQLITE_OS_WIN */ |