drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [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 | ** |
| 13 | ** This file contains code that is specific to windows. |
| 14 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 15 | #include "sqliteInt.h" |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 16 | #include "os.h" |
| 17 | #if OS_WIN /* This file is used for windows only */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 18 | |
| 19 | #include <winbase.h> |
| 20 | |
drh | 09bf0e8 | 2005-03-21 00:36:08 +0000 | [diff] [blame] | 21 | #ifdef __CYGWIN__ |
| 22 | # include <sys/cygwin.h> |
| 23 | #endif |
| 24 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 25 | /* |
| 26 | ** Macros used to determine whether or not to use threads. |
| 27 | */ |
| 28 | #if defined(THREADSAFE) && THREADSAFE |
| 29 | # define SQLITE_W32_THREADS 1 |
| 30 | #endif |
| 31 | |
| 32 | /* |
| 33 | ** Include code that is common to all os_*.c files |
| 34 | */ |
| 35 | #include "os_common.h" |
| 36 | |
| 37 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 38 | ** Do not include any of the File I/O interface procedures if the |
| 39 | ** SQLITE_OMIT_DISKIO macro is defined (indicating that there database |
| 40 | ** will be in-memory only) |
| 41 | */ |
| 42 | #ifndef SQLITE_OMIT_DISKIO |
| 43 | |
| 44 | /* |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 45 | ** The following variable is (normally) set once and never changes |
| 46 | ** thereafter. It records whether the operating system is Win95 |
| 47 | ** or WinNT. |
| 48 | ** |
| 49 | ** 0: Operating system unknown. |
| 50 | ** 1: Operating system is Win95. |
| 51 | ** 2: Operating system is WinNT. |
| 52 | ** |
| 53 | ** In order to facilitate testing on a WinNT system, the test fixture |
| 54 | ** can manually set this value to 1 to emulate Win98 behavior. |
| 55 | */ |
| 56 | int sqlite3_os_type = 0; |
| 57 | |
| 58 | /* |
| 59 | ** Return true (non-zero) if we are running under WinNT, Win2K or WinXP. |
| 60 | ** Return false (zero) for Win95, Win98, or WinME. |
| 61 | ** |
| 62 | ** Here is an interesting observation: Win95, Win98, and WinME lack |
| 63 | ** the LockFileEx() API. But we can still statically link against that |
| 64 | ** API as long as we don't call it win running Win95/98/ME. A call to |
| 65 | ** this routine is used to determine if the host is Win95/98/ME or |
| 66 | ** WinNT/2K/XP so that we will know whether or not we can safely call |
| 67 | ** the LockFileEx() API. |
| 68 | */ |
| 69 | static int isNT(void){ |
| 70 | if( sqlite3_os_type==0 ){ |
| 71 | OSVERSIONINFO sInfo; |
| 72 | sInfo.dwOSVersionInfoSize = sizeof(sInfo); |
| 73 | GetVersionEx(&sInfo); |
| 74 | sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1; |
| 75 | } |
| 76 | return sqlite3_os_type==2; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | ** Convert a UTF-8 string to UTF-32. Space to hold the returned string |
| 81 | ** is obtained from sqliteMalloc. |
| 82 | */ |
| 83 | static WCHAR *utf8ToUnicode(const char *zFilename){ |
| 84 | int nByte; |
| 85 | WCHAR *zWideFilename; |
| 86 | |
| 87 | if( !isNT() ){ |
| 88 | return 0; |
| 89 | } |
| 90 | nByte = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0)*sizeof(WCHAR); |
drh | d81bd4e | 2005-09-05 20:06:49 +0000 | [diff] [blame] | 91 | zWideFilename = sqliteMalloc( nByte*sizeof(zWideFilename[0]) ); |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 92 | if( zWideFilename==0 ){ |
| 93 | return 0; |
| 94 | } |
| 95 | nByte = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, nByte); |
| 96 | if( nByte==0 ){ |
| 97 | sqliteFree(zWideFilename); |
| 98 | zWideFilename = 0; |
| 99 | } |
| 100 | return zWideFilename; |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | ** Convert UTF-32 to UTF-8. Space to hold the returned string is |
| 105 | ** obtained from sqliteMalloc(). |
| 106 | */ |
| 107 | static char *unicodeToUtf8(const WCHAR *zWideFilename){ |
| 108 | int nByte; |
| 109 | char *zFilename; |
| 110 | |
| 111 | nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0); |
| 112 | zFilename = sqliteMalloc( nByte ); |
| 113 | if( zFilename==0 ){ |
| 114 | return 0; |
| 115 | } |
| 116 | nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte, |
| 117 | 0, 0); |
| 118 | if( nByte == 0 ){ |
| 119 | sqliteFree(zFilename); |
| 120 | zFilename = 0; |
| 121 | } |
| 122 | return zFilename; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 127 | ** Delete the named file |
| 128 | */ |
| 129 | int sqlite3OsDelete(const char *zFilename){ |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 130 | WCHAR *zWide = utf8ToUnicode(zFilename); |
| 131 | if( zWide ){ |
| 132 | DeleteFileW(zWide); |
| 133 | sqliteFree(zWide); |
| 134 | }else{ |
| 135 | DeleteFileA(zFilename); |
| 136 | } |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 137 | TRACE2("DELETE \"%s\"\n", zFilename); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 138 | return SQLITE_OK; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | ** Return TRUE if the named file exists. |
| 143 | */ |
| 144 | int sqlite3OsFileExists(const char *zFilename){ |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 145 | int exists = 0; |
| 146 | WCHAR *zWide = utf8ToUnicode(zFilename); |
| 147 | if( zWide ){ |
| 148 | exists = GetFileAttributesW(zWide) != 0xffffffff; |
| 149 | sqliteFree(zWide); |
| 150 | }else{ |
| 151 | exists = GetFileAttributesA(zFilename) != 0xffffffff; |
| 152 | } |
| 153 | return exists; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* |
| 157 | ** Attempt to open a file for both reading and writing. If that |
| 158 | ** fails, try opening it read-only. If the file does not exist, |
| 159 | ** try to create it. |
| 160 | ** |
| 161 | ** On success, a handle for the open file is written to *id |
| 162 | ** and *pReadonly is set to 0 if the file was opened for reading and |
| 163 | ** writing or 1 if the file was opened read-only. The function returns |
| 164 | ** SQLITE_OK. |
| 165 | ** |
| 166 | ** On failure, the function returns SQLITE_CANTOPEN and leaves |
| 167 | ** *id and *pReadonly unchanged. |
| 168 | */ |
| 169 | int sqlite3OsOpenReadWrite( |
| 170 | const char *zFilename, |
| 171 | OsFile *id, |
| 172 | int *pReadonly |
| 173 | ){ |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 174 | HANDLE h; |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 175 | WCHAR *zWide = utf8ToUnicode(zFilename); |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 176 | assert( !id->isOpen ); |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 177 | if( zWide ){ |
| 178 | h = CreateFileW(zWide, |
| 179 | GENERIC_READ | GENERIC_WRITE, |
| 180 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 181 | NULL, |
| 182 | OPEN_ALWAYS, |
| 183 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, |
| 184 | NULL |
| 185 | ); |
| 186 | if( h==INVALID_HANDLE_VALUE ){ |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 187 | h = CreateFileW(zWide, |
| 188 | GENERIC_READ, |
| 189 | FILE_SHARE_READ, |
| 190 | NULL, |
| 191 | OPEN_ALWAYS, |
| 192 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, |
| 193 | NULL |
| 194 | ); |
| 195 | if( h==INVALID_HANDLE_VALUE ){ |
| 196 | sqliteFree(zWide); |
| 197 | return SQLITE_CANTOPEN; |
| 198 | } |
| 199 | *pReadonly = 1; |
| 200 | }else{ |
| 201 | *pReadonly = 0; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 202 | } |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 203 | sqliteFree(zWide); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 204 | }else{ |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 205 | h = CreateFileA(zFilename, |
| 206 | GENERIC_READ | GENERIC_WRITE, |
| 207 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 208 | NULL, |
| 209 | OPEN_ALWAYS, |
| 210 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, |
| 211 | NULL |
| 212 | ); |
| 213 | if( h==INVALID_HANDLE_VALUE ){ |
| 214 | h = CreateFileA(zFilename, |
| 215 | GENERIC_READ, |
| 216 | FILE_SHARE_READ, |
| 217 | NULL, |
| 218 | OPEN_ALWAYS, |
| 219 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, |
| 220 | NULL |
| 221 | ); |
| 222 | if( h==INVALID_HANDLE_VALUE ){ |
| 223 | return SQLITE_CANTOPEN; |
| 224 | } |
| 225 | *pReadonly = 1; |
| 226 | }else{ |
| 227 | *pReadonly = 0; |
| 228 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 229 | } |
| 230 | id->h = h; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 231 | id->locktype = NO_LOCK; |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 232 | id->sharedLockByte = 0; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 233 | id->isOpen = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 234 | OpenCounter(+1); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 235 | TRACE3("OPEN R/W %d \"%s\"\n", h, zFilename); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 236 | return SQLITE_OK; |
| 237 | } |
| 238 | |
| 239 | |
| 240 | /* |
| 241 | ** Attempt to open a new file for exclusive access by this process. |
| 242 | ** The file will be opened for both reading and writing. To avoid |
| 243 | ** a potential security problem, we do not allow the file to have |
| 244 | ** previously existed. Nor do we allow the file to be a symbolic |
| 245 | ** link. |
| 246 | ** |
| 247 | ** If delFlag is true, then make arrangements to automatically delete |
| 248 | ** the file when it is closed. |
| 249 | ** |
| 250 | ** On success, write the file handle into *id and return SQLITE_OK. |
| 251 | ** |
| 252 | ** On failure, return SQLITE_CANTOPEN. |
| 253 | */ |
| 254 | int sqlite3OsOpenExclusive(const char *zFilename, OsFile *id, int delFlag){ |
| 255 | HANDLE h; |
| 256 | int fileflags; |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 257 | WCHAR *zWide = utf8ToUnicode(zFilename); |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 258 | assert( !id->isOpen ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 259 | if( delFlag ){ |
| 260 | fileflags = FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_RANDOM_ACCESS |
| 261 | | FILE_FLAG_DELETE_ON_CLOSE; |
| 262 | }else{ |
| 263 | fileflags = FILE_FLAG_RANDOM_ACCESS; |
| 264 | } |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 265 | if( zWide ){ |
| 266 | h = CreateFileW(zWide, |
| 267 | GENERIC_READ | GENERIC_WRITE, |
| 268 | 0, |
| 269 | NULL, |
| 270 | CREATE_ALWAYS, |
| 271 | fileflags, |
| 272 | NULL |
| 273 | ); |
| 274 | sqliteFree(zWide); |
| 275 | }else{ |
| 276 | h = CreateFileA(zFilename, |
| 277 | GENERIC_READ | GENERIC_WRITE, |
| 278 | 0, |
| 279 | NULL, |
| 280 | CREATE_ALWAYS, |
| 281 | fileflags, |
| 282 | NULL |
| 283 | ); |
| 284 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 285 | if( h==INVALID_HANDLE_VALUE ){ |
| 286 | return SQLITE_CANTOPEN; |
| 287 | } |
| 288 | id->h = h; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 289 | id->locktype = NO_LOCK; |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 290 | id->sharedLockByte = 0; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 291 | id->isOpen = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 292 | OpenCounter(+1); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 293 | TRACE3("OPEN EX %d \"%s\"\n", h, zFilename); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 294 | return SQLITE_OK; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | ** Attempt to open a new file for read-only access. |
| 299 | ** |
| 300 | ** On success, write the file handle into *id and return SQLITE_OK. |
| 301 | ** |
| 302 | ** On failure, return SQLITE_CANTOPEN. |
| 303 | */ |
| 304 | int sqlite3OsOpenReadOnly(const char *zFilename, OsFile *id){ |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 305 | HANDLE h; |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 306 | WCHAR *zWide = utf8ToUnicode(zFilename); |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 307 | assert( !id->isOpen ); |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 308 | if( zWide ){ |
| 309 | h = CreateFileW(zWide, |
| 310 | GENERIC_READ, |
| 311 | 0, |
| 312 | NULL, |
| 313 | OPEN_EXISTING, |
| 314 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, |
| 315 | NULL |
| 316 | ); |
| 317 | sqliteFree(zWide); |
| 318 | }else{ |
| 319 | h = CreateFileA(zFilename, |
| 320 | GENERIC_READ, |
| 321 | 0, |
| 322 | NULL, |
| 323 | OPEN_EXISTING, |
| 324 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, |
| 325 | NULL |
| 326 | ); |
| 327 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 328 | if( h==INVALID_HANDLE_VALUE ){ |
| 329 | return SQLITE_CANTOPEN; |
| 330 | } |
| 331 | id->h = h; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 332 | id->locktype = NO_LOCK; |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 333 | id->sharedLockByte = 0; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 334 | id->isOpen = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 335 | OpenCounter(+1); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 336 | TRACE3("OPEN RO %d \"%s\"\n", h, zFilename); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 337 | return SQLITE_OK; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | ** Attempt to open a file descriptor for the directory that contains a |
| 342 | ** file. This file descriptor can be used to fsync() the directory |
| 343 | ** in order to make sure the creation of a new file is actually written |
| 344 | ** to disk. |
| 345 | ** |
| 346 | ** This routine is only meaningful for Unix. It is a no-op under |
| 347 | ** windows since windows does not support hard links. |
| 348 | ** |
| 349 | ** On success, a handle for a previously open file is at *id is |
| 350 | ** updated with the new directory file descriptor and SQLITE_OK is |
| 351 | ** returned. |
| 352 | ** |
| 353 | ** On failure, the function returns SQLITE_CANTOPEN and leaves |
| 354 | ** *id unchanged. |
| 355 | */ |
| 356 | int sqlite3OsOpenDirectory( |
| 357 | const char *zDirname, |
| 358 | OsFile *id |
| 359 | ){ |
| 360 | return SQLITE_OK; |
| 361 | } |
| 362 | |
| 363 | /* |
drh | 3d2efea | 2004-08-28 01:12:56 +0000 | [diff] [blame] | 364 | ** If the following global variable points to a string which is the |
| 365 | ** name of a directory, then that directory will be used to store |
| 366 | ** temporary files. |
| 367 | */ |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 368 | char *sqlite3_temp_directory = 0; |
drh | 3d2efea | 2004-08-28 01:12:56 +0000 | [diff] [blame] | 369 | |
| 370 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 371 | ** Create a temporary file name in zBuf. zBuf must be big enough to |
| 372 | ** hold at least SQLITE_TEMPNAME_SIZE characters. |
| 373 | */ |
| 374 | int sqlite3OsTempFileName(char *zBuf){ |
| 375 | static char zChars[] = |
| 376 | "abcdefghijklmnopqrstuvwxyz" |
| 377 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 378 | "0123456789"; |
| 379 | int i, j; |
| 380 | char zTempPath[SQLITE_TEMPNAME_SIZE]; |
drh | effd02b | 2004-08-29 23:42:13 +0000 | [diff] [blame] | 381 | if( sqlite3_temp_directory ){ |
| 382 | strncpy(zTempPath, sqlite3_temp_directory, SQLITE_TEMPNAME_SIZE-30); |
drh | 3d2efea | 2004-08-28 01:12:56 +0000 | [diff] [blame] | 383 | zTempPath[SQLITE_TEMPNAME_SIZE-30] = 0; |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 384 | }else if( isNT() ){ |
| 385 | char *zMulti; |
| 386 | WCHAR zWidePath[SQLITE_TEMPNAME_SIZE]; |
| 387 | GetTempPathW(SQLITE_TEMPNAME_SIZE-30, zWidePath); |
| 388 | zMulti = unicodeToUtf8(zWidePath); |
| 389 | if( zMulti ){ |
drh | d81bd4e | 2005-09-05 20:06:49 +0000 | [diff] [blame] | 390 | strncpy(zTempPath, zMulti, SQLITE_TEMPNAME_SIZE-30); |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 391 | zTempPath[SQLITE_TEMPNAME_SIZE-30] = 0; |
| 392 | sqliteFree(zMulti); |
| 393 | } |
drh | 3d2efea | 2004-08-28 01:12:56 +0000 | [diff] [blame] | 394 | }else{ |
| 395 | GetTempPathA(SQLITE_TEMPNAME_SIZE-30, zTempPath); |
| 396 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 397 | for(i=strlen(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){} |
| 398 | zTempPath[i] = 0; |
| 399 | for(;;){ |
| 400 | sprintf(zBuf, "%s\\"TEMP_FILE_PREFIX, zTempPath); |
| 401 | j = strlen(zBuf); |
| 402 | sqlite3Randomness(15, &zBuf[j]); |
| 403 | for(i=0; i<15; i++, j++){ |
| 404 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 405 | } |
| 406 | zBuf[j] = 0; |
| 407 | if( !sqlite3OsFileExists(zBuf) ) break; |
| 408 | } |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 409 | TRACE2("TEMP FILENAME: %s\n", zBuf); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 410 | return SQLITE_OK; |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | ** Close a file. |
| 415 | */ |
| 416 | int sqlite3OsClose(OsFile *id){ |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 417 | if( id->isOpen ){ |
| 418 | TRACE2("CLOSE %d\n", id->h); |
| 419 | CloseHandle(id->h); |
| 420 | OpenCounter(-1); |
| 421 | id->isOpen = 0; |
| 422 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 423 | return SQLITE_OK; |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 428 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 429 | ** wrong. |
| 430 | */ |
| 431 | int sqlite3OsRead(OsFile *id, void *pBuf, int amt){ |
| 432 | DWORD got; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 433 | assert( id->isOpen ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 434 | SimulateIOError(SQLITE_IOERR); |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 435 | TRACE3("READ %d lock=%d\n", id->h, id->locktype); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 436 | if( !ReadFile(id->h, pBuf, amt, &got, 0) ){ |
| 437 | got = 0; |
| 438 | } |
| 439 | if( got==(DWORD)amt ){ |
| 440 | return SQLITE_OK; |
| 441 | }else{ |
| 442 | return SQLITE_IOERR; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /* |
| 447 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 448 | ** or some other error code on failure. |
| 449 | */ |
| 450 | int sqlite3OsWrite(OsFile *id, const void *pBuf, int amt){ |
drh | 4c7f941 | 2005-02-03 00:29:47 +0000 | [diff] [blame] | 451 | int rc = 0; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 452 | DWORD wrote; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 453 | assert( id->isOpen ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 454 | SimulateIOError(SQLITE_IOERR); |
dougcurrie | 0924bba | 2004-10-01 18:21:43 +0000 | [diff] [blame] | 455 | SimulateDiskfullError; |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 456 | TRACE3("WRITE %d lock=%d\n", id->h, id->locktype); |
drh | 4c7f941 | 2005-02-03 00:29:47 +0000 | [diff] [blame] | 457 | assert( amt>0 ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 458 | while( amt>0 && (rc = WriteFile(id->h, pBuf, amt, &wrote, 0))!=0 && wrote>0 ){ |
| 459 | amt -= wrote; |
| 460 | pBuf = &((char*)pBuf)[wrote]; |
| 461 | } |
| 462 | if( !rc || amt>(int)wrote ){ |
| 463 | return SQLITE_FULL; |
| 464 | } |
| 465 | return SQLITE_OK; |
| 466 | } |
| 467 | |
| 468 | /* |
| 469 | ** Move the read/write pointer in a file. |
| 470 | */ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 471 | int sqlite3OsSeek(OsFile *id, i64 offset){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 472 | LONG upperBits = offset>>32; |
| 473 | LONG lowerBits = offset & 0xffffffff; |
| 474 | DWORD rc; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 475 | assert( id->isOpen ); |
drh | b4746b9 | 2005-09-09 01:32:06 +0000 | [diff] [blame] | 476 | #ifdef SQLITE_TEST |
| 477 | if( offset ) SimulateDiskfullError |
| 478 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 479 | SEEK(offset/1024 + 1); |
| 480 | rc = SetFilePointer(id->h, lowerBits, &upperBits, FILE_BEGIN); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 481 | TRACE3("SEEK %d %lld\n", id->h, offset); |
drh | e08b814 | 2005-09-09 10:17:33 +0000 | [diff] [blame^] | 482 | if( rc==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR ){ |
| 483 | return SQLITE_FULL; |
| 484 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 485 | return SQLITE_OK; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | ** Make sure all writes to a particular file are committed to disk. |
| 490 | */ |
drh | eb796a7 | 2005-09-08 12:38:41 +0000 | [diff] [blame] | 491 | int sqlite3OsSync(OsFile *id, int dataOnly){ |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 492 | assert( id->isOpen ); |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 493 | TRACE3("SYNC %d lock=%d\n", id->h, id->locktype); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 494 | if( FlushFileBuffers(id->h) ){ |
| 495 | return SQLITE_OK; |
| 496 | }else{ |
| 497 | return SQLITE_IOERR; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | /* |
danielk1977 | 962398d | 2004-06-14 09:35:16 +0000 | [diff] [blame] | 502 | ** Sync the directory zDirname. This is a no-op on operating systems other |
| 503 | ** than UNIX. |
| 504 | */ |
| 505 | int sqlite3OsSyncDirectory(const char *zDirname){ |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 506 | SimulateIOError(SQLITE_IOERR); |
danielk1977 | 962398d | 2004-06-14 09:35:16 +0000 | [diff] [blame] | 507 | return SQLITE_OK; |
| 508 | } |
| 509 | |
| 510 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 511 | ** Truncate an open file to a specified size |
| 512 | */ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 513 | int sqlite3OsTruncate(OsFile *id, i64 nByte){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 514 | LONG upperBits = nByte>>32; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 515 | assert( id->isOpen ); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 516 | TRACE3("TRUNCATE %d %lld\n", id->h, nByte); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 517 | SimulateIOError(SQLITE_IOERR); |
| 518 | SetFilePointer(id->h, nByte, &upperBits, FILE_BEGIN); |
| 519 | SetEndOfFile(id->h); |
| 520 | return SQLITE_OK; |
| 521 | } |
| 522 | |
| 523 | /* |
| 524 | ** Determine the current size of a file in bytes |
| 525 | */ |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 526 | int sqlite3OsFileSize(OsFile *id, i64 *pSize){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 527 | DWORD upperBits, lowerBits; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 528 | assert( id->isOpen ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 529 | SimulateIOError(SQLITE_IOERR); |
| 530 | lowerBits = GetFileSize(id->h, &upperBits); |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 531 | *pSize = (((i64)upperBits)<<32) + lowerBits; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 532 | return SQLITE_OK; |
| 533 | } |
| 534 | |
| 535 | /* |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 536 | ** Acquire a reader lock. |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 537 | ** Different API routines are called depending on whether or not this |
| 538 | ** is Win95 or WinNT. |
| 539 | */ |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 540 | static int getReadLock(OsFile *id){ |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 541 | int res; |
| 542 | if( isNT() ){ |
| 543 | OVERLAPPED ovlp; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 544 | ovlp.Offset = SHARED_FIRST; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 545 | ovlp.OffsetHigh = 0; |
| 546 | ovlp.hEvent = 0; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 547 | res = LockFileEx(id->h, LOCKFILE_FAIL_IMMEDIATELY, 0, SHARED_SIZE,0,&ovlp); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 548 | }else{ |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 549 | int lk; |
| 550 | sqlite3Randomness(sizeof(lk), &lk); |
| 551 | id->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1); |
| 552 | res = LockFile(id->h, SHARED_FIRST+id->sharedLockByte, 0, 1, 0); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 553 | } |
| 554 | return res; |
| 555 | } |
| 556 | |
| 557 | /* |
| 558 | ** Undo a readlock |
| 559 | */ |
| 560 | static int unlockReadLock(OsFile *id){ |
| 561 | int res; |
| 562 | if( isNT() ){ |
| 563 | res = UnlockFile(id->h, SHARED_FIRST, 0, SHARED_SIZE, 0); |
| 564 | }else{ |
| 565 | res = UnlockFile(id->h, SHARED_FIRST + id->sharedLockByte, 0, 1, 0); |
| 566 | } |
| 567 | return res; |
| 568 | } |
| 569 | |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 570 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 571 | /* |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 572 | ** Check that a given pathname is a directory and is writable |
| 573 | ** |
| 574 | */ |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 575 | int sqlite3OsIsDirWritable(char *zDirname){ |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 576 | int fileAttr; |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 577 | WCHAR *zWide; |
| 578 | if( zDirname==0 ) return 0; |
| 579 | if( !isNT() && strlen(zDirname)>MAX_PATH ) return 0; |
| 580 | zWide = utf8ToUnicode(zDirname); |
| 581 | if( zWide ){ |
| 582 | fileAttr = GetFileAttributesW(zWide); |
| 583 | sqliteFree(zWide); |
| 584 | }else{ |
| 585 | fileAttr = GetFileAttributesA(zDirname); |
| 586 | } |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 587 | if( fileAttr == 0xffffffff ) return 0; |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 588 | if( (fileAttr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY ){ |
| 589 | return 0; |
| 590 | } |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 591 | return 1; |
| 592 | } |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 593 | #endif /* SQLITE_OMIT_PAGER_PRAGMAS */ |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 594 | |
| 595 | /* |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 596 | ** Lock the file with the lock specified by parameter locktype - one |
| 597 | ** of the following: |
| 598 | ** |
| 599 | ** (1) SHARED_LOCK |
| 600 | ** (2) RESERVED_LOCK |
| 601 | ** (3) PENDING_LOCK |
| 602 | ** (4) EXCLUSIVE_LOCK |
| 603 | ** |
| 604 | ** Sometimes when requesting one lock state, additional lock states |
| 605 | ** are inserted in between. The locking might fail on one of the later |
| 606 | ** transitions leaving the lock state different from what it started but |
| 607 | ** still short of its goal. The following chart shows the allowed |
| 608 | ** transitions and the inserted intermediate states: |
| 609 | ** |
| 610 | ** UNLOCKED -> SHARED |
| 611 | ** SHARED -> RESERVED |
| 612 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 613 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 614 | ** PENDING -> EXCLUSIVE |
| 615 | ** |
| 616 | ** This routine will only increase a lock. The sqlite3OsUnlock() routine |
| 617 | ** erases all locks at once and returns us immediately to locking level 0. |
| 618 | ** It is not possible to lower the locking level one step at a time. You |
| 619 | ** must go straight to locking level 0. |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 620 | */ |
| 621 | int sqlite3OsLock(OsFile *id, int locktype){ |
| 622 | int rc = SQLITE_OK; /* Return code from subroutines */ |
| 623 | int res = 1; /* Result of a windows lock call */ |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 624 | int newLocktype; /* Set id->locktype to this value before exiting */ |
| 625 | int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */ |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 626 | |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 627 | assert( id->isOpen ); |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 628 | TRACE5("LOCK %d %d was %d(%d)\n", |
| 629 | id->h, locktype, id->locktype, id->sharedLockByte); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 630 | |
| 631 | /* If there is already a lock of this type or more restrictive on the |
| 632 | ** OsFile, do nothing. Don't use the end_lock: exit path, as |
| 633 | ** sqlite3OsEnterMutex() hasn't been called yet. |
| 634 | */ |
| 635 | if( id->locktype>=locktype ){ |
| 636 | return SQLITE_OK; |
| 637 | } |
| 638 | |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 639 | /* Make sure the locking sequence is correct |
| 640 | */ |
| 641 | assert( id->locktype!=NO_LOCK || locktype==SHARED_LOCK ); |
| 642 | assert( locktype!=PENDING_LOCK ); |
| 643 | assert( locktype!=RESERVED_LOCK || id->locktype==SHARED_LOCK ); |
| 644 | |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 645 | /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or |
| 646 | ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of |
| 647 | ** the PENDING_LOCK byte is temporary. |
| 648 | */ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 649 | newLocktype = id->locktype; |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 650 | if( id->locktype==NO_LOCK |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 651 | || (locktype==EXCLUSIVE_LOCK && id->locktype==RESERVED_LOCK) |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 652 | ){ |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 653 | int cnt = 3; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 654 | while( cnt-->0 && (res = LockFile(id->h, PENDING_BYTE, 0, 1, 0))==0 ){ |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 655 | /* Try 3 times to get the pending lock. The pending lock might be |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 656 | ** held by another reader process who will release it momentarily. |
| 657 | */ |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 658 | TRACE2("could not get a PENDING lock. cnt=%d\n", cnt); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 659 | Sleep(1); |
| 660 | } |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 661 | gotPendingLock = res; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /* Acquire a shared lock |
| 665 | */ |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 666 | if( locktype==SHARED_LOCK && res ){ |
| 667 | assert( id->locktype==NO_LOCK ); |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 668 | res = getReadLock(id); |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 669 | if( res ){ |
| 670 | newLocktype = SHARED_LOCK; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
| 674 | /* Acquire a RESERVED lock |
| 675 | */ |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 676 | if( locktype==RESERVED_LOCK && res ){ |
| 677 | assert( id->locktype==SHARED_LOCK ); |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 678 | res = LockFile(id->h, RESERVED_BYTE, 0, 1, 0); |
| 679 | if( res ){ |
| 680 | newLocktype = RESERVED_LOCK; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | /* Acquire a PENDING lock |
| 685 | */ |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 686 | if( locktype==EXCLUSIVE_LOCK && res ){ |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 687 | newLocktype = PENDING_LOCK; |
| 688 | gotPendingLock = 0; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /* Acquire an EXCLUSIVE lock |
| 692 | */ |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 693 | if( locktype==EXCLUSIVE_LOCK && res ){ |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 694 | assert( id->locktype>=SHARED_LOCK ); |
| 695 | res = unlockReadLock(id); |
| 696 | TRACE2("unreadlock = %d\n", res); |
| 697 | res = LockFile(id->h, SHARED_FIRST, 0, SHARED_SIZE, 0); |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 698 | if( res ){ |
| 699 | newLocktype = EXCLUSIVE_LOCK; |
| 700 | }else{ |
| 701 | TRACE2("error-code = %d\n", GetLastError()); |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | /* If we are holding a PENDING lock that ought to be released, then |
| 706 | ** release it now. |
| 707 | */ |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 708 | if( gotPendingLock && locktype==SHARED_LOCK ){ |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 709 | UnlockFile(id->h, PENDING_BYTE, 0, 1, 0); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | /* Update the state of the lock has held in the file descriptor then |
| 713 | ** return the appropriate result code. |
| 714 | */ |
| 715 | if( res ){ |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 716 | rc = SQLITE_OK; |
| 717 | }else{ |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 718 | TRACE4("LOCK FAILED %d trying for %d but got %d\n", id->h, |
| 719 | locktype, newLocktype); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 720 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 721 | } |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 722 | id->locktype = newLocktype; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 723 | return rc; |
| 724 | } |
| 725 | |
| 726 | /* |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 727 | ** This routine checks if there is a RESERVED lock held on the specified |
| 728 | ** file by this or any other process. If such a lock is held, return |
| 729 | ** non-zero, otherwise zero. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 730 | */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 731 | int sqlite3OsCheckReservedLock(OsFile *id){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 732 | int rc; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 733 | assert( id->isOpen ); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 734 | if( id->locktype>=RESERVED_LOCK ){ |
| 735 | rc = 1; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 736 | TRACE3("TEST WR-LOCK %d %d (local)\n", id->h, rc); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 737 | }else{ |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 738 | rc = LockFile(id->h, RESERVED_BYTE, 0, 1, 0); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 739 | if( rc ){ |
| 740 | UnlockFile(id->h, RESERVED_BYTE, 0, 1, 0); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 741 | } |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 742 | rc = !rc; |
| 743 | TRACE3("TEST WR-LOCK %d %d (remote)\n", id->h, rc); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 744 | } |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 745 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 749 | ** Lower the locking level on file descriptor id to locktype. locktype |
| 750 | ** must be either NO_LOCK or SHARED_LOCK. |
| 751 | ** |
| 752 | ** If the locking level of the file descriptor is already at or below |
| 753 | ** the requested locking level, this routine is a no-op. |
| 754 | ** |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 755 | ** It is not possible for this routine to fail if the second argument |
| 756 | ** is NO_LOCK. If the second argument is SHARED_LOCK then this routine |
| 757 | ** might return SQLITE_IOERR; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 758 | */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 759 | int sqlite3OsUnlock(OsFile *id, int locktype){ |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 760 | int type; |
| 761 | int rc = SQLITE_OK; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 762 | assert( id->isOpen ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 763 | assert( locktype<=SHARED_LOCK ); |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 764 | TRACE5("UNLOCK %d to %d was %d(%d)\n", id->h, locktype, |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 765 | id->locktype, id->sharedLockByte); |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 766 | type = id->locktype; |
| 767 | if( type>=EXCLUSIVE_LOCK ){ |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 768 | UnlockFile(id->h, SHARED_FIRST, 0, SHARED_SIZE, 0); |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 769 | if( locktype==SHARED_LOCK && !getReadLock(id) ){ |
| 770 | /* This should never happen. We should always be able to |
| 771 | ** reacquire the read lock */ |
| 772 | rc = SQLITE_IOERR; |
| 773 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 774 | } |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 775 | if( type>=RESERVED_LOCK ){ |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 776 | UnlockFile(id->h, RESERVED_BYTE, 0, 1, 0); |
| 777 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 778 | if( locktype==NO_LOCK && type>=SHARED_LOCK ){ |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 779 | unlockReadLock(id); |
| 780 | } |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 781 | if( type>=PENDING_LOCK ){ |
| 782 | UnlockFile(id->h, PENDING_BYTE, 0, 1, 0); |
| 783 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 784 | id->locktype = locktype; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 785 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 789 | ** Turn a relative pathname into a full pathname. Return a pointer |
| 790 | ** to the full pathname stored in space obtained from sqliteMalloc(). |
| 791 | ** The calling function is responsible for freeing this space once it |
| 792 | ** is no longer needed. |
| 793 | */ |
| 794 | char *sqlite3OsFullPathname(const char *zRelative){ |
| 795 | char *zNotUsed; |
| 796 | char *zFull; |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 797 | WCHAR *zWide; |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 798 | int nByte; |
| 799 | #ifdef __CYGWIN__ |
| 800 | nByte = strlen(zRelative) + MAX_PATH + 1001; |
| 801 | zFull = sqliteMalloc( nByte ); |
| 802 | if( zFull==0 ) return 0; |
| 803 | if( cygwin_conv_to_full_win32_path(zRelative, zFull) ) return 0; |
| 804 | #else |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 805 | zWide = utf8ToUnicode(zRelative); |
| 806 | if( zWide ){ |
| 807 | WCHAR *zTemp, *zNotUsedW; |
| 808 | nByte = GetFullPathNameW(zWide, 0, 0, &zNotUsedW) + 1; |
| 809 | zTemp = sqliteMalloc( nByte*sizeof(zTemp[0]) ); |
| 810 | if( zTemp==0 ) return 0; |
| 811 | GetFullPathNameW(zWide, nByte, zTemp, &zNotUsedW); |
| 812 | sqliteFree(zWide); |
| 813 | zFull = unicodeToUtf8(zTemp); |
| 814 | sqliteFree(zTemp); |
| 815 | }else{ |
| 816 | nByte = GetFullPathNameA(zRelative, 0, 0, &zNotUsed) + 1; |
| 817 | zFull = sqliteMalloc( nByte*sizeof(zFull[0]) ); |
| 818 | if( zFull==0 ) return 0; |
| 819 | GetFullPathNameA(zRelative, nByte, zFull, &zNotUsed); |
| 820 | } |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 821 | #endif |
| 822 | return zFull; |
| 823 | } |
| 824 | |
| 825 | #endif /* SQLITE_OMIT_DISKIO */ |
| 826 | /*************************************************************************** |
| 827 | ** Everything above deals with file I/O. Everything that follows deals |
| 828 | ** with other miscellanous aspects of the operating system interface |
| 829 | ****************************************************************************/ |
| 830 | |
| 831 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 832 | ** Get information to seed the random number generator. The seed |
| 833 | ** is written into the buffer zBuf[256]. The calling function must |
| 834 | ** supply a sufficiently large buffer. |
| 835 | */ |
| 836 | int sqlite3OsRandomSeed(char *zBuf){ |
| 837 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 838 | ** errors. The reports issued by valgrind are incorrect - we would |
| 839 | ** prefer that the randomness be increased by making use of the |
| 840 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 841 | ** some users. Rather than argue, it seems easier just to initialize |
| 842 | ** the whole array and silence valgrind, even if that means less randomness |
| 843 | ** in the random seed. |
| 844 | ** |
| 845 | ** When testing, initializing zBuf[] to zero is all we do. That means |
| 846 | ** that we always use the same random number sequence.* This makes the |
| 847 | ** tests repeatable. |
| 848 | */ |
| 849 | memset(zBuf, 0, 256); |
| 850 | GetSystemTime((LPSYSTEMTIME)zBuf); |
| 851 | return SQLITE_OK; |
| 852 | } |
| 853 | |
| 854 | /* |
| 855 | ** Sleep for a little while. Return the amount of time slept. |
| 856 | */ |
| 857 | int sqlite3OsSleep(int ms){ |
| 858 | Sleep(ms); |
| 859 | return ms; |
| 860 | } |
| 861 | |
| 862 | /* |
| 863 | ** Static variables used for thread synchronization |
| 864 | */ |
| 865 | static int inMutex = 0; |
| 866 | #ifdef SQLITE_W32_THREADS |
| 867 | static CRITICAL_SECTION cs; |
| 868 | #endif |
| 869 | |
| 870 | /* |
| 871 | ** The following pair of routine implement mutual exclusion for |
| 872 | ** multi-threaded processes. Only a single thread is allowed to |
| 873 | ** executed code that is surrounded by EnterMutex() and LeaveMutex(). |
| 874 | ** |
| 875 | ** SQLite uses only a single Mutex. There is not much critical |
| 876 | ** code and what little there is executes quickly and without blocking. |
| 877 | */ |
| 878 | void sqlite3OsEnterMutex(){ |
| 879 | #ifdef SQLITE_W32_THREADS |
| 880 | static int isInit = 0; |
| 881 | while( !isInit ){ |
| 882 | static long lock = 0; |
| 883 | if( InterlockedIncrement(&lock)==1 ){ |
| 884 | InitializeCriticalSection(&cs); |
| 885 | isInit = 1; |
| 886 | }else{ |
| 887 | Sleep(1); |
| 888 | } |
| 889 | } |
| 890 | EnterCriticalSection(&cs); |
| 891 | #endif |
| 892 | assert( !inMutex ); |
| 893 | inMutex = 1; |
| 894 | } |
| 895 | void sqlite3OsLeaveMutex(){ |
| 896 | assert( inMutex ); |
| 897 | inMutex = 0; |
| 898 | #ifdef SQLITE_W32_THREADS |
| 899 | LeaveCriticalSection(&cs); |
| 900 | #endif |
| 901 | } |
| 902 | |
| 903 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 904 | ** The following variable, if set to a non-zero value, becomes the result |
| 905 | ** returned from sqlite3OsCurrentTime(). This is used for testing. |
| 906 | */ |
| 907 | #ifdef SQLITE_TEST |
| 908 | int sqlite3_current_time = 0; |
| 909 | #endif |
| 910 | |
| 911 | /* |
| 912 | ** Find the current time (in Universal Coordinated Time). Write the |
| 913 | ** current time and date as a Julian Day number into *prNow and |
| 914 | ** return 0. Return 1 if the time and date cannot be found. |
| 915 | */ |
| 916 | int sqlite3OsCurrentTime(double *prNow){ |
| 917 | FILETIME ft; |
| 918 | /* FILETIME structure is a 64-bit value representing the number of |
| 919 | 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5). |
| 920 | */ |
| 921 | double now; |
| 922 | GetSystemTimeAsFileTime( &ft ); |
| 923 | now = ((double)ft.dwHighDateTime) * 4294967296.0; |
| 924 | *prNow = (now + ft.dwLowDateTime)/864000000000.0 + 2305813.5; |
| 925 | #ifdef SQLITE_TEST |
| 926 | if( sqlite3_current_time ){ |
| 927 | *prNow = sqlite3_current_time/86400.0 + 2440587.5; |
| 928 | } |
| 929 | #endif |
| 930 | return 0; |
| 931 | } |
| 932 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 933 | #endif /* OS_WIN */ |