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 | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 38 | ** Determine if we are dealing with WindowsCE - which has a much |
| 39 | ** reduced API. |
| 40 | */ |
| 41 | #if defined(_WIN32_WCE) |
| 42 | # define OS_WINCE 1 |
| 43 | #else |
| 44 | # define OS_WINCE 0 |
| 45 | #endif |
| 46 | |
| 47 | /* |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 48 | ** The winFile structure is a subclass of OsFile specific to the win32 |
| 49 | ** portability layer. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 50 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 51 | typedef struct winFile winFile; |
| 52 | struct winFile { |
| 53 | IoMethod const *pMethod;/* Must be first */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 54 | HANDLE h; /* Handle for accessing the file */ |
| 55 | unsigned char locktype; /* Type of lock currently held on this file */ |
| 56 | short sharedLockByte; /* Randomly chosen byte used as a shared lock */ |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 57 | #if OS_WINCE |
drh | 9e9fe6f | 2006-01-06 21:09:01 +0000 | [diff] [blame] | 58 | WCHAR *zDeleteOnClose; /* Name of file to delete when closing */ |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 59 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 63 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 64 | ** Do not include any of the File I/O interface procedures if the |
| 65 | ** SQLITE_OMIT_DISKIO macro is defined (indicating that there database |
| 66 | ** will be in-memory only) |
| 67 | */ |
| 68 | #ifndef SQLITE_OMIT_DISKIO |
| 69 | |
| 70 | /* |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 71 | ** The following variable is (normally) set once and never changes |
| 72 | ** thereafter. It records whether the operating system is Win95 |
| 73 | ** or WinNT. |
| 74 | ** |
| 75 | ** 0: Operating system unknown. |
| 76 | ** 1: Operating system is Win95. |
| 77 | ** 2: Operating system is WinNT. |
| 78 | ** |
| 79 | ** In order to facilitate testing on a WinNT system, the test fixture |
| 80 | ** can manually set this value to 1 to emulate Win98 behavior. |
| 81 | */ |
| 82 | int sqlite3_os_type = 0; |
| 83 | |
| 84 | /* |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 85 | ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP, |
| 86 | ** or WinCE. Return false (zero) for Win95, Win98, or WinME. |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 87 | ** |
| 88 | ** Here is an interesting observation: Win95, Win98, and WinME lack |
| 89 | ** the LockFileEx() API. But we can still statically link against that |
| 90 | ** API as long as we don't call it win running Win95/98/ME. A call to |
| 91 | ** this routine is used to determine if the host is Win95/98/ME or |
| 92 | ** WinNT/2K/XP so that we will know whether or not we can safely call |
| 93 | ** the LockFileEx() API. |
| 94 | */ |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 95 | #if OS_WINCE |
| 96 | # define isNT() (1) |
| 97 | #else |
| 98 | static int isNT(void){ |
| 99 | if( sqlite3_os_type==0 ){ |
| 100 | OSVERSIONINFO sInfo; |
| 101 | sInfo.dwOSVersionInfoSize = sizeof(sInfo); |
| 102 | GetVersionEx(&sInfo); |
| 103 | sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1; |
| 104 | } |
| 105 | return sqlite3_os_type==2; |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 106 | } |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 107 | #endif /* OS_WINCE */ |
| 108 | |
| 109 | #if OS_WINCE |
| 110 | /* |
| 111 | ** WindowsCE does not have a localtime() function. So create a |
| 112 | ** substitute. |
| 113 | */ |
| 114 | #include <time.h> |
| 115 | struct tm *__cdecl localtime(const time_t *t) |
| 116 | { |
| 117 | static struct tm y; |
| 118 | FILETIME uTm, lTm; |
| 119 | SYSTEMTIME pTm; |
| 120 | i64 t64; |
| 121 | t64 = *t; |
| 122 | t64 = (t64 + 11644473600)*10000000; |
| 123 | uTm.dwLowDateTime = t64 & 0xFFFFFFFF; |
| 124 | uTm.dwHighDateTime= t64 >> 32; |
| 125 | FileTimeToLocalFileTime(&uTm,&lTm); |
| 126 | FileTimeToSystemTime(&lTm,&pTm); |
| 127 | y.tm_year = pTm.wYear - 1900; |
| 128 | y.tm_mon = pTm.wMonth - 1; |
| 129 | y.tm_wday = pTm.wDayOfWeek; |
| 130 | y.tm_mday = pTm.wDay; |
| 131 | y.tm_hour = pTm.wHour; |
| 132 | y.tm_min = pTm.wMinute; |
| 133 | y.tm_sec = pTm.wSecond; |
| 134 | return &y; |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 135 | } |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 136 | #endif |
| 137 | |
| 138 | /* |
| 139 | ** Compile with -DSQLITE_OMIT_WIN_LOCKS to disable file locking on |
| 140 | ** windows. If you do this and two or more connections attempt to |
| 141 | ** write the database at the same time, the database file will be |
| 142 | ** corrupted. But some versions of WindowsCE do not support locking, |
| 143 | ** in which case compiling with this option is required just to get |
| 144 | ** it to work at all. |
| 145 | */ |
| 146 | #ifdef SQLITE_OMIT_WIN_LOCKS |
drh | e2a926b | 2006-01-06 20:40:28 +0000 | [diff] [blame] | 147 | # define LockFile(a,b,c,d,e) (1) |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 148 | # define LockFileEx(a,b,c,d,e,f) (1) |
drh | e2a926b | 2006-01-06 20:40:28 +0000 | [diff] [blame] | 149 | # define UnlockFile(a,b,c,d,e) (1) |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 150 | # define UnlockFileEx(a,b,c,d,e) (1) |
| 151 | #endif |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | ** Convert a UTF-8 string to UTF-32. Space to hold the returned string |
| 155 | ** is obtained from sqliteMalloc. |
| 156 | */ |
| 157 | static WCHAR *utf8ToUnicode(const char *zFilename){ |
| 158 | int nByte; |
| 159 | WCHAR *zWideFilename; |
| 160 | |
| 161 | if( !isNT() ){ |
| 162 | return 0; |
| 163 | } |
| 164 | nByte = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0)*sizeof(WCHAR); |
drh | d81bd4e | 2005-09-05 20:06:49 +0000 | [diff] [blame] | 165 | zWideFilename = sqliteMalloc( nByte*sizeof(zWideFilename[0]) ); |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 166 | if( zWideFilename==0 ){ |
| 167 | return 0; |
| 168 | } |
| 169 | nByte = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, nByte); |
| 170 | if( nByte==0 ){ |
| 171 | sqliteFree(zWideFilename); |
| 172 | zWideFilename = 0; |
| 173 | } |
| 174 | return zWideFilename; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | ** Convert UTF-32 to UTF-8. Space to hold the returned string is |
| 179 | ** obtained from sqliteMalloc(). |
| 180 | */ |
| 181 | static char *unicodeToUtf8(const WCHAR *zWideFilename){ |
| 182 | int nByte; |
| 183 | char *zFilename; |
| 184 | |
| 185 | nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0); |
| 186 | zFilename = sqliteMalloc( nByte ); |
| 187 | if( zFilename==0 ){ |
| 188 | return 0; |
| 189 | } |
| 190 | nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte, |
| 191 | 0, 0); |
| 192 | if( nByte == 0 ){ |
| 193 | sqliteFree(zFilename); |
| 194 | zFilename = 0; |
| 195 | } |
| 196 | return zFilename; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 201 | ** Delete the named file |
| 202 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 203 | int sqlite3WinDelete(const char *zFilename){ |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 204 | WCHAR *zWide = utf8ToUnicode(zFilename); |
| 205 | if( zWide ){ |
| 206 | DeleteFileW(zWide); |
| 207 | sqliteFree(zWide); |
| 208 | }else{ |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 209 | #if OS_WINCE |
| 210 | return SQLITE_NOMEM; |
| 211 | #else |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 212 | DeleteFileA(zFilename); |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 213 | #endif |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 214 | } |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 215 | TRACE2("DELETE \"%s\"\n", zFilename); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 216 | return SQLITE_OK; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | ** Return TRUE if the named file exists. |
| 221 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 222 | int sqlite3WinFileExists(const char *zFilename){ |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 223 | int exists = 0; |
| 224 | WCHAR *zWide = utf8ToUnicode(zFilename); |
| 225 | if( zWide ){ |
| 226 | exists = GetFileAttributesW(zWide) != 0xffffffff; |
| 227 | sqliteFree(zWide); |
| 228 | }else{ |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 229 | #if OS_WINCE |
| 230 | return SQLITE_NOMEM; |
| 231 | #else |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 232 | exists = GetFileAttributesA(zFilename) != 0xffffffff; |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 233 | #endif |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 234 | } |
| 235 | return exists; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 236 | } |
| 237 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 238 | /* Forward declaration */ |
| 239 | int allocateWinFile(winFile *pInit, OsFile **pId); |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 240 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 241 | /* |
| 242 | ** Attempt to open a file for both reading and writing. If that |
| 243 | ** fails, try opening it read-only. If the file does not exist, |
| 244 | ** try to create it. |
| 245 | ** |
| 246 | ** On success, a handle for the open file is written to *id |
| 247 | ** and *pReadonly is set to 0 if the file was opened for reading and |
| 248 | ** writing or 1 if the file was opened read-only. The function returns |
| 249 | ** SQLITE_OK. |
| 250 | ** |
| 251 | ** On failure, the function returns SQLITE_CANTOPEN and leaves |
| 252 | ** *id and *pReadonly unchanged. |
| 253 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 254 | int sqlite3WinOpenReadWrite( |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 255 | const char *zFilename, |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 256 | OsFile **pId, |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 257 | int *pReadonly |
| 258 | ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 259 | winFile f; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 260 | HANDLE h; |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 261 | WCHAR *zWide = utf8ToUnicode(zFilename); |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 262 | assert( *pId==0 ); |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 263 | if( zWide ){ |
| 264 | h = CreateFileW(zWide, |
| 265 | GENERIC_READ | GENERIC_WRITE, |
| 266 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 267 | NULL, |
| 268 | OPEN_ALWAYS, |
| 269 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, |
| 270 | NULL |
| 271 | ); |
| 272 | if( h==INVALID_HANDLE_VALUE ){ |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 273 | h = CreateFileW(zWide, |
| 274 | GENERIC_READ, |
| 275 | FILE_SHARE_READ, |
| 276 | NULL, |
| 277 | OPEN_ALWAYS, |
| 278 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, |
| 279 | NULL |
| 280 | ); |
| 281 | if( h==INVALID_HANDLE_VALUE ){ |
| 282 | sqliteFree(zWide); |
| 283 | return SQLITE_CANTOPEN; |
| 284 | } |
| 285 | *pReadonly = 1; |
| 286 | }else{ |
| 287 | *pReadonly = 0; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 288 | } |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 289 | sqliteFree(zWide); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 290 | }else{ |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 291 | #if OS_WINCE |
| 292 | return SQLITE_NOMEM; |
| 293 | #else |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 294 | h = CreateFileA(zFilename, |
| 295 | GENERIC_READ | GENERIC_WRITE, |
| 296 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 297 | NULL, |
| 298 | OPEN_ALWAYS, |
| 299 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, |
| 300 | NULL |
| 301 | ); |
| 302 | if( h==INVALID_HANDLE_VALUE ){ |
| 303 | h = CreateFileA(zFilename, |
| 304 | GENERIC_READ, |
| 305 | FILE_SHARE_READ, |
| 306 | NULL, |
| 307 | OPEN_ALWAYS, |
| 308 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, |
| 309 | NULL |
| 310 | ); |
| 311 | if( h==INVALID_HANDLE_VALUE ){ |
| 312 | return SQLITE_CANTOPEN; |
| 313 | } |
| 314 | *pReadonly = 1; |
| 315 | }else{ |
| 316 | *pReadonly = 0; |
| 317 | } |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 318 | #endif /* OS_WINCE */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 319 | } |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 320 | f.h = h; |
| 321 | f.locktype = NO_LOCK; |
| 322 | f.sharedLockByte = 0; |
drh | 4bddfd2 | 2006-01-07 18:14:48 +0000 | [diff] [blame] | 323 | #if OS_WINCE |
| 324 | f.zDeleteOnClose = 0; |
| 325 | #endif |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 326 | TRACE3("OPEN R/W %d \"%s\"\n", h, zFilename); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 327 | return allocateWinFile(&f, pId); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | |
| 331 | /* |
| 332 | ** Attempt to open a new file for exclusive access by this process. |
| 333 | ** The file will be opened for both reading and writing. To avoid |
| 334 | ** a potential security problem, we do not allow the file to have |
| 335 | ** previously existed. Nor do we allow the file to be a symbolic |
| 336 | ** link. |
| 337 | ** |
| 338 | ** If delFlag is true, then make arrangements to automatically delete |
| 339 | ** the file when it is closed. |
| 340 | ** |
| 341 | ** On success, write the file handle into *id and return SQLITE_OK. |
| 342 | ** |
| 343 | ** On failure, return SQLITE_CANTOPEN. |
| 344 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 345 | int sqlite3WinOpenExclusive(const char *zFilename, OsFile **pId, int delFlag){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 346 | winFile f; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 347 | HANDLE h; |
| 348 | int fileflags; |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 349 | WCHAR *zWide = utf8ToUnicode(zFilename); |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 350 | assert( *pId == 0 ); |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 351 | fileflags = FILE_FLAG_RANDOM_ACCESS; |
| 352 | #if !OS_WINCE |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 353 | if( delFlag ){ |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 354 | fileflags |= FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 355 | } |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 356 | #endif |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 357 | if( zWide ){ |
| 358 | h = CreateFileW(zWide, |
| 359 | GENERIC_READ | GENERIC_WRITE, |
| 360 | 0, |
| 361 | NULL, |
| 362 | CREATE_ALWAYS, |
| 363 | fileflags, |
| 364 | NULL |
| 365 | ); |
| 366 | sqliteFree(zWide); |
| 367 | }else{ |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 368 | #if OS_WINCE |
| 369 | return SQLITE_NOMEM; |
| 370 | #else |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 371 | h = CreateFileA(zFilename, |
| 372 | GENERIC_READ | GENERIC_WRITE, |
| 373 | 0, |
| 374 | NULL, |
| 375 | CREATE_ALWAYS, |
| 376 | fileflags, |
| 377 | NULL |
| 378 | ); |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 379 | #endif /* OS_WINCE */ |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 380 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 381 | if( h==INVALID_HANDLE_VALUE ){ |
| 382 | return SQLITE_CANTOPEN; |
| 383 | } |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 384 | f.h = h; |
| 385 | f.locktype = NO_LOCK; |
| 386 | f.sharedLockByte = 0; |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 387 | #if OS_WINCE |
drh | 9e9fe6f | 2006-01-06 21:09:01 +0000 | [diff] [blame] | 388 | f.zDeleteOnClose = delFlag ? utf8ToUnicode(zFilename) : 0; |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 389 | #endif |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 390 | TRACE3("OPEN EX %d \"%s\"\n", h, zFilename); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 391 | return allocateWinFile(&f, pId); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /* |
| 395 | ** Attempt to open a new file for read-only access. |
| 396 | ** |
| 397 | ** On success, write the file handle into *id and return SQLITE_OK. |
| 398 | ** |
| 399 | ** On failure, return SQLITE_CANTOPEN. |
| 400 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 401 | int sqlite3WinOpenReadOnly(const char *zFilename, OsFile **pId){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 402 | winFile f; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 403 | HANDLE h; |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 404 | WCHAR *zWide = utf8ToUnicode(zFilename); |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 405 | assert( *pId==0 ); |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 406 | if( zWide ){ |
| 407 | h = CreateFileW(zWide, |
| 408 | GENERIC_READ, |
| 409 | 0, |
| 410 | NULL, |
| 411 | OPEN_EXISTING, |
| 412 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, |
| 413 | NULL |
| 414 | ); |
| 415 | sqliteFree(zWide); |
| 416 | }else{ |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 417 | #if OS_WINCE |
| 418 | return SQLITE_NOMEM; |
| 419 | #else |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 420 | h = CreateFileA(zFilename, |
| 421 | GENERIC_READ, |
| 422 | 0, |
| 423 | NULL, |
| 424 | OPEN_EXISTING, |
| 425 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, |
| 426 | NULL |
| 427 | ); |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 428 | #endif |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 429 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 430 | if( h==INVALID_HANDLE_VALUE ){ |
| 431 | return SQLITE_CANTOPEN; |
| 432 | } |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 433 | f.h = h; |
| 434 | f.locktype = NO_LOCK; |
| 435 | f.sharedLockByte = 0; |
drh | 4bddfd2 | 2006-01-07 18:14:48 +0000 | [diff] [blame] | 436 | #if OS_WINCE |
| 437 | f.zDeleteOnClose = 0; |
| 438 | #endif |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 439 | TRACE3("OPEN RO %d \"%s\"\n", h, zFilename); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 440 | return allocateWinFile(&f, pId); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /* |
| 444 | ** Attempt to open a file descriptor for the directory that contains a |
| 445 | ** file. This file descriptor can be used to fsync() the directory |
| 446 | ** in order to make sure the creation of a new file is actually written |
| 447 | ** to disk. |
| 448 | ** |
| 449 | ** This routine is only meaningful for Unix. It is a no-op under |
| 450 | ** windows since windows does not support hard links. |
| 451 | ** |
| 452 | ** On success, a handle for a previously open file is at *id is |
| 453 | ** updated with the new directory file descriptor and SQLITE_OK is |
| 454 | ** returned. |
| 455 | ** |
| 456 | ** On failure, the function returns SQLITE_CANTOPEN and leaves |
| 457 | ** *id unchanged. |
| 458 | */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 459 | static int winOpenDirectory( |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 460 | OsFile *id, |
| 461 | const char *zDirname |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 462 | ){ |
| 463 | return SQLITE_OK; |
| 464 | } |
| 465 | |
| 466 | /* |
drh | 3d2efea | 2004-08-28 01:12:56 +0000 | [diff] [blame] | 467 | ** If the following global variable points to a string which is the |
| 468 | ** name of a directory, then that directory will be used to store |
| 469 | ** temporary files. |
| 470 | */ |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 471 | char *sqlite3_temp_directory = 0; |
drh | 3d2efea | 2004-08-28 01:12:56 +0000 | [diff] [blame] | 472 | |
| 473 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 474 | ** Create a temporary file name in zBuf. zBuf must be big enough to |
| 475 | ** hold at least SQLITE_TEMPNAME_SIZE characters. |
| 476 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 477 | int sqlite3WinTempFileName(char *zBuf){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 478 | static char zChars[] = |
| 479 | "abcdefghijklmnopqrstuvwxyz" |
| 480 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 481 | "0123456789"; |
| 482 | int i, j; |
| 483 | char zTempPath[SQLITE_TEMPNAME_SIZE]; |
drh | effd02b | 2004-08-29 23:42:13 +0000 | [diff] [blame] | 484 | if( sqlite3_temp_directory ){ |
| 485 | strncpy(zTempPath, sqlite3_temp_directory, SQLITE_TEMPNAME_SIZE-30); |
drh | 3d2efea | 2004-08-28 01:12:56 +0000 | [diff] [blame] | 486 | zTempPath[SQLITE_TEMPNAME_SIZE-30] = 0; |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 487 | }else if( isNT() ){ |
| 488 | char *zMulti; |
| 489 | WCHAR zWidePath[SQLITE_TEMPNAME_SIZE]; |
| 490 | GetTempPathW(SQLITE_TEMPNAME_SIZE-30, zWidePath); |
| 491 | zMulti = unicodeToUtf8(zWidePath); |
| 492 | if( zMulti ){ |
drh | d81bd4e | 2005-09-05 20:06:49 +0000 | [diff] [blame] | 493 | strncpy(zTempPath, zMulti, SQLITE_TEMPNAME_SIZE-30); |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 494 | zTempPath[SQLITE_TEMPNAME_SIZE-30] = 0; |
| 495 | sqliteFree(zMulti); |
| 496 | } |
drh | 3d2efea | 2004-08-28 01:12:56 +0000 | [diff] [blame] | 497 | }else{ |
| 498 | GetTempPathA(SQLITE_TEMPNAME_SIZE-30, zTempPath); |
| 499 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 500 | for(i=strlen(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){} |
| 501 | zTempPath[i] = 0; |
| 502 | for(;;){ |
| 503 | sprintf(zBuf, "%s\\"TEMP_FILE_PREFIX, zTempPath); |
| 504 | j = strlen(zBuf); |
| 505 | sqlite3Randomness(15, &zBuf[j]); |
| 506 | for(i=0; i<15; i++, j++){ |
| 507 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 508 | } |
| 509 | zBuf[j] = 0; |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 510 | if( !sqlite3OsFileExists(zBuf) ) break; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 511 | } |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 512 | TRACE2("TEMP FILENAME: %s\n", zBuf); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 513 | return SQLITE_OK; |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | ** Close a file. |
| 518 | */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 519 | static int winClose(OsFile **pId){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 520 | winFile *pFile; |
| 521 | if( pId && (pFile = (winFile*)*pId)!=0 ){ |
| 522 | TRACE2("CLOSE %d\n", pFile->h); |
| 523 | CloseHandle(pFile->h); |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 524 | #if OS_WINCE |
| 525 | if( pFile->zDeleteOnClose ){ |
drh | 9e9fe6f | 2006-01-06 21:09:01 +0000 | [diff] [blame] | 526 | DeleteFileW(pFile->zDeleteOnClose); |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 527 | sqliteFree(pFile->zDeleteOnClose); |
| 528 | } |
| 529 | #endif |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 530 | OpenCounter(-1); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 531 | sqliteFree(pFile); |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 532 | *pId = 0; |
drh | da71ce1 | 2004-06-21 18:14:45 +0000 | [diff] [blame] | 533 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 534 | return SQLITE_OK; |
| 535 | } |
| 536 | |
| 537 | /* |
| 538 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 539 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 540 | ** wrong. |
| 541 | */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 542 | static int winRead(OsFile *id, void *pBuf, int amt){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 543 | DWORD got; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 544 | assert( id!=0 ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 545 | SimulateIOError(SQLITE_IOERR); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 546 | TRACE3("READ %d lock=%d\n", ((winFile*)id)->h, ((winFile*)id)->locktype); |
| 547 | if( !ReadFile(((winFile*)id)->h, pBuf, amt, &got, 0) ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 548 | got = 0; |
| 549 | } |
| 550 | if( got==(DWORD)amt ){ |
| 551 | return SQLITE_OK; |
| 552 | }else{ |
| 553 | return SQLITE_IOERR; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | /* |
| 558 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 559 | ** or some other error code on failure. |
| 560 | */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 561 | static int winWrite(OsFile *id, const void *pBuf, int amt){ |
drh | 4c7f941 | 2005-02-03 00:29:47 +0000 | [diff] [blame] | 562 | int rc = 0; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 563 | DWORD wrote; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 564 | assert( id!=0 ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 565 | SimulateIOError(SQLITE_IOERR); |
dougcurrie | 0924bba | 2004-10-01 18:21:43 +0000 | [diff] [blame] | 566 | SimulateDiskfullError; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 567 | TRACE3("WRITE %d lock=%d\n", ((winFile*)id)->h, ((winFile*)id)->locktype); |
drh | 4c7f941 | 2005-02-03 00:29:47 +0000 | [diff] [blame] | 568 | assert( amt>0 ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 569 | while( amt>0 && (rc = WriteFile(((winFile*)id)->h, pBuf, amt, &wrote, 0))!=0 |
| 570 | && wrote>0 ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 571 | amt -= wrote; |
| 572 | pBuf = &((char*)pBuf)[wrote]; |
| 573 | } |
| 574 | if( !rc || amt>(int)wrote ){ |
| 575 | return SQLITE_FULL; |
| 576 | } |
| 577 | return SQLITE_OK; |
| 578 | } |
| 579 | |
| 580 | /* |
drh | bbdc2b9 | 2005-09-19 12:53:18 +0000 | [diff] [blame] | 581 | ** Some microsoft compilers lack this definition. |
| 582 | */ |
| 583 | #ifndef INVALID_SET_FILE_POINTER |
| 584 | # define INVALID_SET_FILE_POINTER ((DWORD)-1) |
| 585 | #endif |
| 586 | |
| 587 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 588 | ** Move the read/write pointer in a file. |
| 589 | */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 590 | static int winSeek(OsFile *id, i64 offset){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 591 | LONG upperBits = offset>>32; |
| 592 | LONG lowerBits = offset & 0xffffffff; |
| 593 | DWORD rc; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 594 | assert( id!=0 ); |
drh | b4746b9 | 2005-09-09 01:32:06 +0000 | [diff] [blame] | 595 | #ifdef SQLITE_TEST |
| 596 | if( offset ) SimulateDiskfullError |
| 597 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 598 | SEEK(offset/1024 + 1); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 599 | rc = SetFilePointer(((winFile*)id)->h, lowerBits, &upperBits, FILE_BEGIN); |
| 600 | TRACE3("SEEK %d %lld\n", ((winFile*)id)->h, offset); |
drh | e08b814 | 2005-09-09 10:17:33 +0000 | [diff] [blame] | 601 | if( rc==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR ){ |
| 602 | return SQLITE_FULL; |
| 603 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 604 | return SQLITE_OK; |
| 605 | } |
| 606 | |
| 607 | /* |
| 608 | ** Make sure all writes to a particular file are committed to disk. |
| 609 | */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 610 | static int winSync(OsFile *id, int dataOnly){ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 611 | assert( id!=0 ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 612 | TRACE3("SYNC %d lock=%d\n", ((winFile*)id)->h, ((winFile*)id)->locktype); |
| 613 | if( FlushFileBuffers(((winFile*)id)->h) ){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 614 | return SQLITE_OK; |
| 615 | }else{ |
| 616 | return SQLITE_IOERR; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | /* |
danielk1977 | 962398d | 2004-06-14 09:35:16 +0000 | [diff] [blame] | 621 | ** Sync the directory zDirname. This is a no-op on operating systems other |
| 622 | ** than UNIX. |
| 623 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 624 | int sqlite3WinSyncDirectory(const char *zDirname){ |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 625 | SimulateIOError(SQLITE_IOERR); |
danielk1977 | 962398d | 2004-06-14 09:35:16 +0000 | [diff] [blame] | 626 | return SQLITE_OK; |
| 627 | } |
| 628 | |
| 629 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 630 | ** Truncate an open file to a specified size |
| 631 | */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 632 | static int winTruncate(OsFile *id, i64 nByte){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 633 | LONG upperBits = nByte>>32; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 634 | assert( id!=0 ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 635 | TRACE3("TRUNCATE %d %lld\n", ((winFile*)id)->h, nByte); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 636 | SimulateIOError(SQLITE_IOERR); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 637 | SetFilePointer(((winFile*)id)->h, nByte, &upperBits, FILE_BEGIN); |
| 638 | SetEndOfFile(((winFile*)id)->h); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 639 | return SQLITE_OK; |
| 640 | } |
| 641 | |
| 642 | /* |
| 643 | ** Determine the current size of a file in bytes |
| 644 | */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 645 | static int winFileSize(OsFile *id, i64 *pSize){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 646 | DWORD upperBits, lowerBits; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 647 | assert( id!=0 ); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 648 | SimulateIOError(SQLITE_IOERR); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 649 | lowerBits = GetFileSize(((winFile*)id)->h, &upperBits); |
drh | eb20625 | 2004-10-01 02:00:31 +0000 | [diff] [blame] | 650 | *pSize = (((i64)upperBits)<<32) + lowerBits; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 651 | return SQLITE_OK; |
| 652 | } |
| 653 | |
| 654 | /* |
drh | 602bbd3 | 2006-01-06 20:22:29 +0000 | [diff] [blame] | 655 | ** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems. |
| 656 | */ |
| 657 | #ifndef LOCKFILE_FAIL_IMMEDIATELY |
| 658 | # define LOCKFILE_FAIL_IMMEDIATELY 1 |
| 659 | #endif |
| 660 | |
| 661 | /* |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 662 | ** Acquire a reader lock. |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 663 | ** Different API routines are called depending on whether or not this |
| 664 | ** is Win95 or WinNT. |
| 665 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 666 | static int getReadLock(winFile *id){ |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 667 | int res; |
| 668 | if( isNT() ){ |
| 669 | OVERLAPPED ovlp; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 670 | ovlp.Offset = SHARED_FIRST; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 671 | ovlp.OffsetHigh = 0; |
| 672 | ovlp.hEvent = 0; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 673 | res = LockFileEx(id->h, LOCKFILE_FAIL_IMMEDIATELY, 0, SHARED_SIZE,0,&ovlp); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 674 | }else{ |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 675 | int lk; |
| 676 | sqlite3Randomness(sizeof(lk), &lk); |
| 677 | id->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1); |
| 678 | res = LockFile(id->h, SHARED_FIRST+id->sharedLockByte, 0, 1, 0); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 679 | } |
| 680 | return res; |
| 681 | } |
| 682 | |
| 683 | /* |
| 684 | ** Undo a readlock |
| 685 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 686 | static int unlockReadLock(winFile *pFile){ |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 687 | int res; |
| 688 | if( isNT() ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 689 | res = UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 690 | }else{ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 691 | res = UnlockFile(pFile->h, SHARED_FIRST + pFile->sharedLockByte, 0, 1, 0); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 692 | } |
| 693 | return res; |
| 694 | } |
| 695 | |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 696 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 697 | /* |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 698 | ** Check that a given pathname is a directory and is writable |
| 699 | ** |
| 700 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 701 | int sqlite3WinIsDirWritable(char *zDirname){ |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 702 | int fileAttr; |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 703 | WCHAR *zWide; |
| 704 | if( zDirname==0 ) return 0; |
| 705 | if( !isNT() && strlen(zDirname)>MAX_PATH ) return 0; |
| 706 | zWide = utf8ToUnicode(zDirname); |
| 707 | if( zWide ){ |
| 708 | fileAttr = GetFileAttributesW(zWide); |
| 709 | sqliteFree(zWide); |
| 710 | }else{ |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 711 | #if OS_WINCE |
| 712 | return 0; |
| 713 | #else |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 714 | fileAttr = GetFileAttributesA(zDirname); |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 715 | #endif |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 716 | } |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 717 | if( fileAttr == 0xffffffff ) return 0; |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 718 | if( (fileAttr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY ){ |
| 719 | return 0; |
| 720 | } |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 721 | return 1; |
| 722 | } |
drh | 268283b | 2005-01-08 15:44:25 +0000 | [diff] [blame] | 723 | #endif /* SQLITE_OMIT_PAGER_PRAGMAS */ |
tpoindex | 9a09a3c | 2004-12-20 19:01:32 +0000 | [diff] [blame] | 724 | |
| 725 | /* |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 726 | ** Lock the file with the lock specified by parameter locktype - one |
| 727 | ** of the following: |
| 728 | ** |
| 729 | ** (1) SHARED_LOCK |
| 730 | ** (2) RESERVED_LOCK |
| 731 | ** (3) PENDING_LOCK |
| 732 | ** (4) EXCLUSIVE_LOCK |
| 733 | ** |
| 734 | ** Sometimes when requesting one lock state, additional lock states |
| 735 | ** are inserted in between. The locking might fail on one of the later |
| 736 | ** transitions leaving the lock state different from what it started but |
| 737 | ** still short of its goal. The following chart shows the allowed |
| 738 | ** transitions and the inserted intermediate states: |
| 739 | ** |
| 740 | ** UNLOCKED -> SHARED |
| 741 | ** SHARED -> RESERVED |
| 742 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 743 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 744 | ** PENDING -> EXCLUSIVE |
| 745 | ** |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 746 | ** This routine will only increase a lock. The winUnlock() routine |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 747 | ** erases all locks at once and returns us immediately to locking level 0. |
| 748 | ** It is not possible to lower the locking level one step at a time. You |
| 749 | ** must go straight to locking level 0. |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 750 | */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 751 | static int winLock(OsFile *id, int locktype){ |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 752 | int rc = SQLITE_OK; /* Return code from subroutines */ |
| 753 | int res = 1; /* Result of a windows lock call */ |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 754 | int newLocktype; /* Set id->locktype to this value before exiting */ |
| 755 | int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 756 | winFile *pFile = (winFile*)id; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 757 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 758 | assert( pFile!=0 ); |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 759 | TRACE5("LOCK %d %d was %d(%d)\n", |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 760 | pFile->h, locktype, pFile->locktype, pFile->sharedLockByte); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 761 | |
| 762 | /* If there is already a lock of this type or more restrictive on the |
| 763 | ** OsFile, do nothing. Don't use the end_lock: exit path, as |
| 764 | ** sqlite3OsEnterMutex() hasn't been called yet. |
| 765 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 766 | if( pFile->locktype>=locktype ){ |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 767 | return SQLITE_OK; |
| 768 | } |
| 769 | |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 770 | /* Make sure the locking sequence is correct |
| 771 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 772 | assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK ); |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 773 | assert( locktype!=PENDING_LOCK ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 774 | assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK ); |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 775 | |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 776 | /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or |
| 777 | ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of |
| 778 | ** the PENDING_LOCK byte is temporary. |
| 779 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 780 | newLocktype = pFile->locktype; |
| 781 | if( pFile->locktype==NO_LOCK |
| 782 | || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK) |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 783 | ){ |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 784 | int cnt = 3; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 785 | while( cnt-->0 && (res = LockFile(pFile->h, PENDING_BYTE, 0, 1, 0))==0 ){ |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 786 | /* Try 3 times to get the pending lock. The pending lock might be |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 787 | ** held by another reader process who will release it momentarily. |
| 788 | */ |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 789 | TRACE2("could not get a PENDING lock. cnt=%d\n", cnt); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 790 | Sleep(1); |
| 791 | } |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 792 | gotPendingLock = res; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | /* Acquire a shared lock |
| 796 | */ |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 797 | if( locktype==SHARED_LOCK && res ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 798 | assert( pFile->locktype==NO_LOCK ); |
| 799 | res = getReadLock(pFile); |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 800 | if( res ){ |
| 801 | newLocktype = SHARED_LOCK; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 802 | } |
| 803 | } |
| 804 | |
| 805 | /* Acquire a RESERVED lock |
| 806 | */ |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 807 | if( locktype==RESERVED_LOCK && res ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 808 | assert( pFile->locktype==SHARED_LOCK ); |
| 809 | res = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0); |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 810 | if( res ){ |
| 811 | newLocktype = RESERVED_LOCK; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | /* Acquire a PENDING lock |
| 816 | */ |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 817 | if( locktype==EXCLUSIVE_LOCK && res ){ |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 818 | newLocktype = PENDING_LOCK; |
| 819 | gotPendingLock = 0; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | /* Acquire an EXCLUSIVE lock |
| 823 | */ |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 824 | if( locktype==EXCLUSIVE_LOCK && res ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 825 | assert( pFile->locktype>=SHARED_LOCK ); |
| 826 | res = unlockReadLock(pFile); |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 827 | TRACE2("unreadlock = %d\n", res); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 828 | res = LockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0); |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 829 | if( res ){ |
| 830 | newLocktype = EXCLUSIVE_LOCK; |
| 831 | }else{ |
| 832 | TRACE2("error-code = %d\n", GetLastError()); |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | /* If we are holding a PENDING lock that ought to be released, then |
| 837 | ** release it now. |
| 838 | */ |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 839 | if( gotPendingLock && locktype==SHARED_LOCK ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 840 | UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | /* Update the state of the lock has held in the file descriptor then |
| 844 | ** return the appropriate result code. |
| 845 | */ |
| 846 | if( res ){ |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 847 | rc = SQLITE_OK; |
| 848 | }else{ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 849 | TRACE4("LOCK FAILED %d trying for %d but got %d\n", pFile->h, |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 850 | locktype, newLocktype); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 851 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 852 | } |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 853 | pFile->locktype = newLocktype; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 854 | return rc; |
| 855 | } |
| 856 | |
| 857 | /* |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 858 | ** This routine checks if there is a RESERVED lock held on the specified |
| 859 | ** file by this or any other process. If such a lock is held, return |
| 860 | ** non-zero, otherwise zero. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 861 | */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 862 | static int winCheckReservedLock(OsFile *id){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 863 | int rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 864 | winFile *pFile = (winFile*)id; |
| 865 | assert( pFile!=0 ); |
| 866 | if( pFile->locktype>=RESERVED_LOCK ){ |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 867 | rc = 1; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 868 | TRACE3("TEST WR-LOCK %d %d (local)\n", pFile->h, rc); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 869 | }else{ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 870 | rc = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 871 | if( rc ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 872 | UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 873 | } |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 874 | rc = !rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 875 | TRACE3("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 876 | } |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 877 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 881 | ** Lower the locking level on file descriptor id to locktype. locktype |
| 882 | ** must be either NO_LOCK or SHARED_LOCK. |
| 883 | ** |
| 884 | ** If the locking level of the file descriptor is already at or below |
| 885 | ** the requested locking level, this routine is a no-op. |
| 886 | ** |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 887 | ** It is not possible for this routine to fail if the second argument |
| 888 | ** is NO_LOCK. If the second argument is SHARED_LOCK then this routine |
| 889 | ** might return SQLITE_IOERR; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 890 | */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 891 | static int winUnlock(OsFile *id, int locktype){ |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 892 | int type; |
| 893 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 894 | winFile *pFile = (winFile*)id; |
| 895 | assert( pFile!=0 ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 896 | assert( locktype<=SHARED_LOCK ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 897 | TRACE5("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype, |
| 898 | pFile->locktype, pFile->sharedLockByte); |
| 899 | type = pFile->locktype; |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 900 | if( type>=EXCLUSIVE_LOCK ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 901 | UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0); |
| 902 | if( locktype==SHARED_LOCK && !getReadLock(pFile) ){ |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 903 | /* This should never happen. We should always be able to |
| 904 | ** reacquire the read lock */ |
| 905 | rc = SQLITE_IOERR; |
| 906 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 907 | } |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 908 | if( type>=RESERVED_LOCK ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 909 | UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 910 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 911 | if( locktype==NO_LOCK && type>=SHARED_LOCK ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 912 | unlockReadLock(pFile); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 913 | } |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 914 | if( type>=PENDING_LOCK ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 915 | UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0); |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 916 | } |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 917 | pFile->locktype = locktype; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 918 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 922 | ** Turn a relative pathname into a full pathname. Return a pointer |
| 923 | ** to the full pathname stored in space obtained from sqliteMalloc(). |
| 924 | ** The calling function is responsible for freeing this space once it |
| 925 | ** is no longer needed. |
| 926 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 927 | char *sqlite3WinFullPathname(const char *zRelative){ |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 928 | char *zFull; |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 929 | #if defined(__CYGWIN__) |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 930 | int nByte; |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 931 | nByte = strlen(zRelative) + MAX_PATH + 1001; |
| 932 | zFull = sqliteMalloc( nByte ); |
| 933 | if( zFull==0 ) return 0; |
| 934 | if( cygwin_conv_to_full_win32_path(zRelative, zFull) ) return 0; |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 935 | #elif OS_WINCE |
| 936 | /* WinCE has no concept of a relative pathname, or so I am told. */ |
danielk1977 | e725929 | 2006-01-13 06:33:23 +0000 | [diff] [blame] | 937 | zFull = sqliteStrDup(zRelative); |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 938 | #else |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 939 | char *zNotUsed; |
| 940 | WCHAR *zWide; |
| 941 | int nByte; |
drh | c092998 | 2005-09-05 19:08:29 +0000 | [diff] [blame] | 942 | zWide = utf8ToUnicode(zRelative); |
| 943 | if( zWide ){ |
| 944 | WCHAR *zTemp, *zNotUsedW; |
| 945 | nByte = GetFullPathNameW(zWide, 0, 0, &zNotUsedW) + 1; |
| 946 | zTemp = sqliteMalloc( nByte*sizeof(zTemp[0]) ); |
| 947 | if( zTemp==0 ) return 0; |
| 948 | GetFullPathNameW(zWide, nByte, zTemp, &zNotUsedW); |
| 949 | sqliteFree(zWide); |
| 950 | zFull = unicodeToUtf8(zTemp); |
| 951 | sqliteFree(zTemp); |
| 952 | }else{ |
| 953 | nByte = GetFullPathNameA(zRelative, 0, 0, &zNotUsed) + 1; |
| 954 | zFull = sqliteMalloc( nByte*sizeof(zFull[0]) ); |
| 955 | if( zFull==0 ) return 0; |
| 956 | GetFullPathNameA(zRelative, nByte, zFull, &zNotUsed); |
| 957 | } |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 958 | #endif |
| 959 | return zFull; |
| 960 | } |
| 961 | |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 962 | /* |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 963 | ** The fullSync option is meaningless on windows. This is a no-op. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 964 | */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 965 | static void winSetFullSync(OsFile *id, int v){ |
| 966 | return; |
| 967 | } |
| 968 | |
| 969 | /* |
| 970 | ** Return the underlying file handle for an OsFile |
| 971 | */ |
| 972 | static int winFileHandle(OsFile *id){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 973 | return (int)((winFile*)id)->h; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | /* |
| 977 | ** Return an integer that indices the type of lock currently held |
| 978 | ** by this handle. (Used for testing and analysis only.) |
| 979 | */ |
| 980 | static int winLockState(OsFile *id){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 981 | return ((winFile*)id)->locktype; |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 982 | } |
| 983 | |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 984 | /* |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 985 | ** This vector defines all the methods that can operate on an OsFile |
| 986 | ** for win32. |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 987 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 988 | static const IoMethod sqlite3WinIoMethod = { |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 989 | winClose, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 990 | winOpenDirectory, |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 991 | winRead, |
| 992 | winWrite, |
| 993 | winSeek, |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 994 | winTruncate, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 995 | winSync, |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 996 | winSetFullSync, |
| 997 | winFileHandle, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 998 | winFileSize, |
| 999 | winLock, |
| 1000 | winUnlock, |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1001 | winLockState, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1002 | winCheckReservedLock, |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1003 | }; |
| 1004 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1005 | /* |
| 1006 | ** Allocate memory for an OsFile. Initialize the new OsFile |
| 1007 | ** to the value given in pInit and return a pointer to the new |
| 1008 | ** OsFile. If we run out of memory, close the file and return NULL. |
| 1009 | */ |
| 1010 | int allocateWinFile(winFile *pInit, OsFile **pId){ |
| 1011 | winFile *pNew; |
| 1012 | pNew = sqliteMalloc( sizeof(*pNew) ); |
| 1013 | if( pNew==0 ){ |
| 1014 | CloseHandle(pInit->h); |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 1015 | #if OS_WINCE |
| 1016 | sqliteFree(pInit->zDeleteOnClose); |
| 1017 | #endif |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1018 | *pId = 0; |
| 1019 | return SQLITE_NOMEM; |
| 1020 | }else{ |
| 1021 | *pNew = *pInit; |
| 1022 | pNew->pMethod = &sqlite3WinIoMethod; |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1023 | *pId = (OsFile*)pNew; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1024 | return SQLITE_OK; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 1029 | #endif /* SQLITE_OMIT_DISKIO */ |
| 1030 | /*************************************************************************** |
| 1031 | ** Everything above deals with file I/O. Everything that follows deals |
| 1032 | ** with other miscellanous aspects of the operating system interface |
| 1033 | ****************************************************************************/ |
| 1034 | |
| 1035 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1036 | ** Get information to seed the random number generator. The seed |
| 1037 | ** is written into the buffer zBuf[256]. The calling function must |
| 1038 | ** supply a sufficiently large buffer. |
| 1039 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1040 | int sqlite3WinRandomSeed(char *zBuf){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1041 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 1042 | ** errors. The reports issued by valgrind are incorrect - we would |
| 1043 | ** prefer that the randomness be increased by making use of the |
| 1044 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 1045 | ** some users. Rather than argue, it seems easier just to initialize |
| 1046 | ** the whole array and silence valgrind, even if that means less randomness |
| 1047 | ** in the random seed. |
| 1048 | ** |
| 1049 | ** When testing, initializing zBuf[] to zero is all we do. That means |
| 1050 | ** that we always use the same random number sequence.* This makes the |
| 1051 | ** tests repeatable. |
| 1052 | */ |
| 1053 | memset(zBuf, 0, 256); |
| 1054 | GetSystemTime((LPSYSTEMTIME)zBuf); |
| 1055 | return SQLITE_OK; |
| 1056 | } |
| 1057 | |
| 1058 | /* |
| 1059 | ** Sleep for a little while. Return the amount of time slept. |
| 1060 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1061 | int sqlite3WinSleep(int ms){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1062 | Sleep(ms); |
| 1063 | return ms; |
| 1064 | } |
| 1065 | |
| 1066 | /* |
| 1067 | ** Static variables used for thread synchronization |
| 1068 | */ |
| 1069 | static int inMutex = 0; |
| 1070 | #ifdef SQLITE_W32_THREADS |
drh | 332b1fe | 2006-01-18 14:20:17 +0000 | [diff] [blame^] | 1071 | static HANDLE mutexOwner; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1072 | static CRITICAL_SECTION cs; |
| 1073 | #endif |
| 1074 | |
| 1075 | /* |
| 1076 | ** The following pair of routine implement mutual exclusion for |
| 1077 | ** multi-threaded processes. Only a single thread is allowed to |
| 1078 | ** executed code that is surrounded by EnterMutex() and LeaveMutex(). |
| 1079 | ** |
| 1080 | ** SQLite uses only a single Mutex. There is not much critical |
| 1081 | ** code and what little there is executes quickly and without blocking. |
| 1082 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1083 | void sqlite3WinEnterMutex(){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1084 | #ifdef SQLITE_W32_THREADS |
| 1085 | static int isInit = 0; |
| 1086 | while( !isInit ){ |
| 1087 | static long lock = 0; |
| 1088 | if( InterlockedIncrement(&lock)==1 ){ |
| 1089 | InitializeCriticalSection(&cs); |
| 1090 | isInit = 1; |
| 1091 | }else{ |
| 1092 | Sleep(1); |
| 1093 | } |
| 1094 | } |
| 1095 | EnterCriticalSection(&cs); |
drh | 332b1fe | 2006-01-18 14:20:17 +0000 | [diff] [blame^] | 1096 | mutexOwner = GetCurrentThread(); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1097 | #endif |
drh | 332b1fe | 2006-01-18 14:20:17 +0000 | [diff] [blame^] | 1098 | inMutex++; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1099 | } |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1100 | void sqlite3WinLeaveMutex(){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1101 | assert( inMutex ); |
drh | 332b1fe | 2006-01-18 14:20:17 +0000 | [diff] [blame^] | 1102 | inMutex--; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1103 | #ifdef SQLITE_W32_THREADS |
| 1104 | LeaveCriticalSection(&cs); |
| 1105 | #endif |
| 1106 | } |
| 1107 | |
| 1108 | /* |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 1109 | ** Return TRUE if we are currently within the mutex and FALSE if not. |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 1110 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1111 | int sqlite3WinInMutex(){ |
drh | 332b1fe | 2006-01-18 14:20:17 +0000 | [diff] [blame^] | 1112 | #ifdef SQLITE_W32_THREADS |
| 1113 | return inMutex && mutexOwner==GetCurrentThread(); |
| 1114 | #else |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 1115 | return inMutex; |
drh | 332b1fe | 2006-01-18 14:20:17 +0000 | [diff] [blame^] | 1116 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | |
| 1120 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1121 | ** The following variable, if set to a non-zero value, becomes the result |
| 1122 | ** returned from sqlite3OsCurrentTime(). This is used for testing. |
| 1123 | */ |
| 1124 | #ifdef SQLITE_TEST |
| 1125 | int sqlite3_current_time = 0; |
| 1126 | #endif |
| 1127 | |
| 1128 | /* |
| 1129 | ** Find the current time (in Universal Coordinated Time). Write the |
| 1130 | ** current time and date as a Julian Day number into *prNow and |
| 1131 | ** return 0. Return 1 if the time and date cannot be found. |
| 1132 | */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1133 | int sqlite3WinCurrentTime(double *prNow){ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1134 | FILETIME ft; |
| 1135 | /* FILETIME structure is a 64-bit value representing the number of |
| 1136 | 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5). |
| 1137 | */ |
| 1138 | double now; |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 1139 | #if OS_WINCE |
| 1140 | SYSTEMTIME time; |
| 1141 | GetSystemTime(&time); |
| 1142 | SystemTimeToFileTime(&time,&ft); |
| 1143 | #else |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1144 | GetSystemTimeAsFileTime( &ft ); |
drh | cc78fea | 2006-01-06 16:17:05 +0000 | [diff] [blame] | 1145 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1146 | now = ((double)ft.dwHighDateTime) * 4294967296.0; |
| 1147 | *prNow = (now + ft.dwLowDateTime)/864000000000.0 + 2305813.5; |
| 1148 | #ifdef SQLITE_TEST |
| 1149 | if( sqlite3_current_time ){ |
| 1150 | *prNow = sqlite3_current_time/86400.0 + 2440587.5; |
| 1151 | } |
| 1152 | #endif |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1156 | /* |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 1157 | ** Remember the number of thread-specific-data blocks allocated. |
| 1158 | ** Use this to verify that we are not leaking thread-specific-data. |
| 1159 | ** Ticket #1601 |
| 1160 | */ |
| 1161 | #ifdef SQLITE_TEST |
| 1162 | int sqlite3_tsd_count = 0; |
| 1163 | # define TSD_COUNTER_INCR InterlockedIncrement(&sqlite3_tsd_count) |
| 1164 | # define TSD_COUNTER_DECR InterlockedDecrement(&sqlite3_tsd_count) |
| 1165 | #else |
| 1166 | # define TSD_COUNTER_INCR /* no-op */ |
| 1167 | # define TSD_COUNTER_DECR /* no-op */ |
| 1168 | #endif |
| 1169 | |
| 1170 | |
| 1171 | |
| 1172 | /* |
drh | 70ff98a | 2006-01-12 01:25:18 +0000 | [diff] [blame] | 1173 | ** If called with allocateFlag>1, then return a pointer to thread |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1174 | ** specific data for the current thread. Allocate and zero the |
| 1175 | ** thread-specific data if it does not already exist necessary. |
drh | 3fbb0b1 | 2006-01-06 00:36:00 +0000 | [diff] [blame] | 1176 | ** |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1177 | ** If called with allocateFlag==0, then check the current thread |
drh | 70ff98a | 2006-01-12 01:25:18 +0000 | [diff] [blame] | 1178 | ** specific data. Return it if it exists. If it does not exist, |
| 1179 | ** then return NULL. |
| 1180 | ** |
| 1181 | ** If called with allocateFlag<0, check to see if the thread specific |
| 1182 | ** data is allocated and is all zero. If it is then deallocate it. |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1183 | ** Return a pointer to the thread specific data or NULL if it is |
drh | 70ff98a | 2006-01-12 01:25:18 +0000 | [diff] [blame] | 1184 | ** unallocated or gets deallocated. |
danielk1977 | 13a68c3 | 2005-12-15 10:11:30 +0000 | [diff] [blame] | 1185 | */ |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 1186 | ThreadData *sqlite3WinThreadSpecificData(int allocateFlag){ |
drh | 3fbb0b1 | 2006-01-06 00:36:00 +0000 | [diff] [blame] | 1187 | static int key; |
| 1188 | static int keyInit = 0; |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1189 | static const ThreadData zeroData; |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 1190 | ThreadData *pTsd; |
drh | 3fbb0b1 | 2006-01-06 00:36:00 +0000 | [diff] [blame] | 1191 | |
| 1192 | if( !keyInit ){ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1193 | sqlite3OsEnterMutex(); |
drh | 3fbb0b1 | 2006-01-06 00:36:00 +0000 | [diff] [blame] | 1194 | if( !keyInit ){ |
| 1195 | key = TlsAlloc(); |
| 1196 | if( key==0xffffffff ){ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1197 | sqlite3OsLeaveMutex(); |
drh | 3fbb0b1 | 2006-01-06 00:36:00 +0000 | [diff] [blame] | 1198 | return 0; |
| 1199 | } |
| 1200 | keyInit = 1; |
| 1201 | } |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1202 | sqlite3OsLeaveMutex(); |
danielk1977 | 13a68c3 | 2005-12-15 10:11:30 +0000 | [diff] [blame] | 1203 | } |
drh | 3fbb0b1 | 2006-01-06 00:36:00 +0000 | [diff] [blame] | 1204 | pTsd = TlsGetValue(key); |
drh | 70ff98a | 2006-01-12 01:25:18 +0000 | [diff] [blame] | 1205 | if( allocateFlag>0 ){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1206 | if( !pTsd ){ |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 1207 | pTsd = sqlite3OsMalloc( sizeof(zeroData) ); |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1208 | if( pTsd ){ |
| 1209 | *pTsd = zeroData; |
| 1210 | TlsSetValue(key, pTsd); |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 1211 | TSD_COUNTER_INCR; |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1212 | } |
drh | 3fbb0b1 | 2006-01-06 00:36:00 +0000 | [diff] [blame] | 1213 | } |
drh | 70ff98a | 2006-01-12 01:25:18 +0000 | [diff] [blame] | 1214 | }else if( pTsd!=0 && allocateFlag<0 |
danielk1977 | cbb8496 | 2006-01-17 16:10:13 +0000 | [diff] [blame] | 1215 | && memcmp(pTsd, &zeroData, THREADDATASIZE)==0 ){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1216 | sqlite3OsFree(pTsd); |
| 1217 | TlsSetValue(key, 0); |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 1218 | TSD_COUNTER_DECR; |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1219 | pTsd = 0; |
drh | 3fbb0b1 | 2006-01-06 00:36:00 +0000 | [diff] [blame] | 1220 | } |
| 1221 | return pTsd; |
danielk1977 | 13a68c3 | 2005-12-15 10:11:30 +0000 | [diff] [blame] | 1222 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1223 | #endif /* OS_WIN */ |