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