00001 /* 00002 ** 00003 ** The author disclaims copyright to this source code. In place of 00004 ** a legal notice, here is a blessing: 00005 ** 00006 ** May you do good and not evil. 00007 ** May you find forgiveness for yourself and forgive others. 00008 ** May you share freely, never taking more than you give. 00009 ** 00010 ****************************************************************************** 00011 ** 00012 ** This file is a variant of os_unix.c, for Symbian OS / P.I.P.S. 00013 ** based systems. P.I.P.S. provides a fairly decent degree of Unix 00014 ** compatibility, with quirks. 00015 ** 00016 ** os_unix.c might otherwise work on Symbian, but the usual 00017 ** slash/backslash issue is there, and the complicated-looking file 00018 ** locking is unlikely to work. os_symbian.c disables locking 00019 ** altogether. This is fine for applications with an 00020 ** application-specific database and only one instance running, and 00021 ** this actually is what a typical Symbian application/daemon is like, 00022 ** so it's not as bad as its sounds. 00023 ** 00024 ** To further simplify this file, we have also disabled support for 00025 ** SQLITE_THREADSAFE, but luckily a typical Symbian application is 00026 ** single-threaded. 00027 ** 00028 ** Also, we are not supporting directory syncing, as it did not appear 00029 ** to work on Symbian anyway. Do not know how important it is for 00030 ** database integrity. 00031 ** 00032 ** Ensure that you are only compiling in one of os_symbian.c and 00033 ** os_unix.c, not both. We want to keep them somewhat interchangeable 00034 ** to allow for testing the Symbian version on Linux. 00035 ** 00036 */ 00037 #include "sqliteInt.h" 00038 #if SQLITE_OS_UNIX 00039 00040 /* !concept {:name => "Porting SQLite3 to Symbian"} */ 00041 00042 /* 00043 ** standard include files. 00044 */ 00045 #include <sys/types.h> 00046 #include <sys/stat.h> 00047 #include <fcntl.h> 00048 #include <unistd.h> 00049 #include <time.h> 00050 #include <sys/time.h> 00051 #include <errno.h> 00052 00053 //#include "logging.h" 00054 00055 /* 00056 ** If we are to be thread-safe, include the pthreads header and define 00057 ** the SQLITE_UNIX_THREADS macro. 00058 */ 00059 #if SQLITE_THREADSAFE 00060 #error SQLITE_THREADSAFE unsupported 00061 #endif 00062 00063 /* 00064 ** Default permissions when creating a new file 00065 */ 00066 #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS 00067 # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 00068 #endif 00069 00070 /* 00071 ** Maximum supported path-length. In the Symbian case these actually 00072 ** should be UTF-8 encoded, and such that there are at most 256 00073 ** characters, meaning that the number of bytes can be greater 00074 ** (presuming that is okay). But we are assuming ASCII here, and this 00075 ** should be fine for any applications where the user does not get to 00076 ** name the file. 00077 */ 00078 #define MAX_PATHNAME 256 00079 00080 00081 /* 00082 ** The unixFile structure is subclass of sqlite3_file specific for the unix 00083 ** protability layer. 00084 */ 00085 typedef struct unixFile unixFile; 00086 struct unixFile { 00087 sqlite3_io_methods const *pMethod; /* Always the first entry */ 00088 #ifdef SQLITE_TEST 00089 /* In test mode, increase the size of this structure a bit so that 00090 ** it is larger than the struct CrashFile defined in test6.c. 00091 */ 00092 char aPadding[32]; 00093 #endif 00094 int h; /* The file descriptor */ 00095 int lastErrno; /* The unix errno from the last I/O error */ 00096 }; 00097 00098 /* 00099 ** Include code that is common to all os_*.c files 00100 */ 00101 #include "os_common.h" 00102 00103 /* 00104 ** Define various macros that are missing from some systems. 00105 */ 00106 #ifndef O_LARGEFILE 00107 # define O_LARGEFILE 0 00108 #endif 00109 #ifdef SQLITE_DISABLE_LFS 00110 # undef O_LARGEFILE 00111 # define O_LARGEFILE 0 00112 #endif 00113 #ifndef O_NOFOLLOW 00114 # define O_NOFOLLOW 0 00115 #endif 00116 #ifndef O_BINARY 00117 # define O_BINARY 0 00118 #endif 00119 00120 #if defined(__SYMBIAN32__) 00121 #define SEP_CH '\\' 00122 #define SEP_STR "\\" 00123 #else 00124 #define SEP_CH '/' 00125 #define SEP_STR "/" 00126 #endif 00127 00128 /* 00129 ** Seek to the offset passed as the second argument, then read cnt 00130 ** bytes into pBuf. Return the number of bytes actually read. 00131 ** 00132 ** NB: If you define USE_PREAD or USE_PREAD64, then it might also 00133 ** be necessary to define _XOPEN_SOURCE to be 500. This varies from 00134 ** one system to another. Since SQLite does not define USE_PREAD 00135 ** any any form by default, we will not attempt to define _XOPEN_SOURCE. 00136 ** See tickets #2741 and #2681. 00137 */ 00138 static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ 00139 int got; 00140 i64 newOffset; 00141 TIMER_START; 00142 #if defined(USE_PREAD) 00143 got = pread(id->h, pBuf, cnt, offset); 00144 SimulateIOError( got = -1 ); 00145 #elif defined(USE_PREAD64) 00146 got = pread64(id->h, pBuf, cnt, offset); 00147 SimulateIOError( got = -1 ); 00148 #else 00149 newOffset = lseek(id->h, offset, SEEK_SET); 00150 SimulateIOError( newOffset-- ); 00151 if( newOffset!=offset ){ 00152 return -1; 00153 } 00154 got = read(id->h, pBuf, cnt); 00155 #endif 00156 TIMER_END; 00157 OSTRACE5("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED); 00158 return got; 00159 } 00160 00161 /* 00162 ** Read data from a file into a buffer. Return SQLITE_OK if all 00163 ** bytes were read successfully and SQLITE_IOERR if anything goes 00164 ** wrong. 00165 */ 00166 static int unixRead( 00167 sqlite3_file *id, 00168 void *pBuf, 00169 int amt, 00170 sqlite3_int64 offset 00171 ){ 00172 int got; 00173 assert( id ); 00174 got = seekAndRead((unixFile*)id, offset, pBuf, amt); 00175 if( got==amt ){ 00176 return SQLITE_OK; 00177 }else if( got<0 ){ 00178 return SQLITE_IOERR_READ; 00179 }else{ 00180 /* Unread parts of the buffer must be zero-filled */ 00181 memset(&((char*)pBuf)[got], 0, amt-got); 00182 return SQLITE_IOERR_SHORT_READ; 00183 } 00184 } 00185 00186 /* 00187 ** Seek to the offset in id->offset then read cnt bytes into pBuf. 00188 ** Return the number of bytes actually read. Update the offset. 00189 */ 00190 static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ 00191 int got; 00192 i64 newOffset; 00193 TIMER_START; 00194 #if defined(USE_PREAD) 00195 got = pwrite(id->h, pBuf, cnt, offset); 00196 #elif defined(USE_PREAD64) 00197 got = pwrite64(id->h, pBuf, cnt, offset); 00198 #else 00199 newOffset = lseek(id->h, offset, SEEK_SET); 00200 if( newOffset!=offset ){ 00201 return -1; 00202 } 00203 got = write(id->h, pBuf, cnt); 00204 #endif 00205 TIMER_END; 00206 OSTRACE5("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED); 00207 return got; 00208 } 00209 00210 00211 /* 00212 ** Write data from a buffer into a file. Return SQLITE_OK on success 00213 ** or some other error code on failure. 00214 */ 00215 static int unixWrite( 00216 sqlite3_file *id, 00217 const void *pBuf, 00218 int amt, 00219 sqlite3_int64 offset 00220 ){ 00221 int wrote = 0; 00222 assert( id ); 00223 assert( amt>0 ); 00224 while( amt>0 && (wrote = seekAndWrite((unixFile*)id, offset, pBuf, amt))>0 ){ 00225 amt -= wrote; 00226 offset += wrote; 00227 pBuf = &((char*)pBuf)[wrote]; 00228 } 00229 SimulateIOError(( wrote=(-1), amt=1 )); 00230 SimulateDiskfullError(( wrote=0, amt=1 )); 00231 if( amt>0 ){ 00232 if( wrote<0 ){ 00233 return SQLITE_IOERR_WRITE; 00234 }else{ 00235 return SQLITE_FULL; 00236 } 00237 } 00238 return SQLITE_OK; 00239 } 00240 00241 #ifdef SQLITE_TEST 00242 /* 00243 ** Count the number of fullsyncs and normal syncs. This is used to test 00244 ** that syncs and fullsyncs are occuring at the right times. 00245 */ 00246 int sqlite3_sync_count = 0; 00247 int sqlite3_fullsync_count = 0; 00248 #endif 00249 00250 /* 00251 ** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined. 00252 ** Otherwise use fsync() in its place. 00253 */ 00254 #ifndef HAVE_FDATASYNC 00255 # define fdatasync fsync 00256 #endif 00257 00258 /* 00259 ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not 00260 ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently 00261 ** only available on Mac OS X. But that could change. 00262 */ 00263 #ifdef F_FULLFSYNC 00264 # define HAVE_FULLFSYNC 1 00265 #else 00266 # define HAVE_FULLFSYNC 0 00267 #endif 00268 00269 00270 /* 00271 ** The fsync() system call does not work as advertised on many 00272 ** unix systems. The following procedure is an attempt to make 00273 ** it work better. 00274 ** 00275 ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful 00276 ** for testing when we want to run through the test suite quickly. 00277 ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC 00278 ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash 00279 ** or power failure will likely corrupt the database file. 00280 */ 00281 static int full_fsync(int fd, int fullSync, int dataOnly){ 00282 int rc; 00283 00284 /* Record the number of times that we do a normal fsync() and 00285 ** FULLSYNC. This is used during testing to verify that this procedure 00286 ** gets called with the correct arguments. 00287 */ 00288 #ifdef SQLITE_TEST 00289 if( fullSync ) sqlite3_fullsync_count++; 00290 sqlite3_sync_count++; 00291 #endif 00292 00293 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a 00294 ** no-op 00295 */ 00296 #ifdef SQLITE_NO_SYNC 00297 rc = SQLITE_OK; 00298 #else 00299 00300 #if HAVE_FULLFSYNC 00301 if( fullSync ){ 00302 rc = fcntl(fd, F_FULLFSYNC, 0); 00303 }else{ 00304 rc = 1; 00305 } 00306 /* If the FULLFSYNC failed, fall back to attempting an fsync(). 00307 * It shouldn't be possible for fullfsync to fail on the local 00308 * file system (on OSX), so failure indicates that FULLFSYNC 00309 * isn't supported for this file system. So, attempt an fsync 00310 * and (for now) ignore the overhead of a superfluous fcntl call. 00311 * It'd be better to detect fullfsync support once and avoid 00312 * the fcntl call every time sync is called. 00313 */ 00314 if( rc ) rc = fsync(fd); 00315 00316 #else 00317 if( dataOnly ){ 00318 rc = fdatasync(fd); 00319 }else{ 00320 rc = fsync(fd); 00321 } 00322 #endif /* HAVE_FULLFSYNC */ 00323 #endif /* defined(SQLITE_NO_SYNC) */ 00324 00325 return rc; 00326 } 00327 00328 /* 00329 ** Make sure all writes to a particular file are committed to disk. 00330 ** 00331 ** If dataOnly==0 then both the file itself and its metadata (file 00332 ** size, access time, etc) are synced. If dataOnly!=0 then only the 00333 ** file data is synced. 00334 ** 00335 ** Under Unix, also make sure that the directory entry for the file 00336 ** has been created by fsync-ing the directory that contains the file. 00337 ** If we do not do this and we encounter a power failure, the directory 00338 ** entry for the journal might not exist after we reboot. The next 00339 ** SQLite to access the file will not know that the journal exists (because 00340 ** the directory entry for the journal was never created) and the transaction 00341 ** will not roll back - possibly leading to database corruption. 00342 */ 00343 static int unixSync(sqlite3_file *id, int flags){ 00344 int rc; 00345 unixFile *pFile = (unixFile*)id; 00346 00347 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); 00348 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; 00349 00350 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ 00351 assert((flags&0x0F)==SQLITE_SYNC_NORMAL 00352 || (flags&0x0F)==SQLITE_SYNC_FULL 00353 ); 00354 00355 /* Unix cannot, but some systems may return SQLITE_FULL from here. This 00356 ** line is to test that doing so does not cause any problems. 00357 */ 00358 SimulateDiskfullError( return SQLITE_FULL ); 00359 00360 assert( pFile ); 00361 OSTRACE2("SYNC %-3d\n", pFile->h); 00362 rc = full_fsync(pFile->h, isFullsync, isDataOnly); 00363 SimulateIOError( rc=1 ); 00364 if( rc ){ 00365 return SQLITE_IOERR_FSYNC; 00366 } 00367 return SQLITE_OK; 00368 } 00369 00370 /* 00371 ** Truncate an open file to a specified size 00372 */ 00373 static int unixTruncate(sqlite3_file *id, i64 nByte){ 00374 int rc; 00375 assert( id ); 00376 SimulateIOError( return SQLITE_IOERR_TRUNCATE ); 00377 rc = ftruncate(((unixFile*)id)->h, (off_t)nByte); 00378 if( rc ){ 00379 return SQLITE_IOERR_TRUNCATE; 00380 }else{ 00381 return SQLITE_OK; 00382 } 00383 } 00384 00385 /* 00386 ** Determine the current size of a file in bytes 00387 */ 00388 static int unixFileSize(sqlite3_file *id, i64 *pSize){ 00389 int rc; 00390 struct stat buf; 00391 assert( id ); 00392 rc = fstat(((unixFile*)id)->h, &buf); 00393 SimulateIOError( rc=1 ); 00394 if( rc!=0 ){ 00395 return SQLITE_IOERR_FSTAT; 00396 } 00397 *pSize = buf.st_size; 00398 00399 /* When opening a zero-size database, the findLockInfo() procedure 00400 ** writes a single byte into that file in order to work around a bug 00401 ** in the OS-X msdos filesystem. In order to avoid problems with upper 00402 ** layers, we need to report this file size as zero even though it is 00403 ** really 1. Ticket #3260. 00404 */ 00405 if( *pSize==1 ) *pSize = 0; 00406 00407 00408 return SQLITE_OK; 00409 } 00410 00411 /* 00412 ** This function performs the parts of the "close file" operation 00413 ** common to all locking schemes. It closes the directory and file 00414 ** handles, if they are valid, and sets all fields of the unixFile 00415 ** structure to 0. 00416 */ 00417 static int closeUnixFile(sqlite3_file *id){ 00418 unixFile *pFile = (unixFile*)id; 00419 if( pFile ){ 00420 if( pFile->h>=0 ){ 00421 close(pFile->h); 00422 } 00423 OSTRACE2("CLOSE %-3d\n", pFile->h); 00424 OpenCounter(-1); 00425 memset(pFile, 0, sizeof(unixFile)); 00426 } 00427 return SQLITE_OK; 00428 } 00429 00430 /* 00431 ** The nolockLockingContext is void 00432 */ 00433 typedef void nolockLockingContext; 00434 00435 static int nolockCheckReservedLock(sqlite3_file *id, int *pResOut) { 00436 *pResOut = 0; 00437 return SQLITE_OK; 00438 } 00439 00440 static int nolockLock(sqlite3_file *id, int locktype) { 00441 return SQLITE_OK; 00442 } 00443 00444 static int nolockUnlock(sqlite3_file *id, int locktype) { 00445 return SQLITE_OK; 00446 } 00447 00448 /* 00449 ** Close a file. 00450 */ 00451 static int nolockClose(sqlite3_file *id) { 00452 return closeUnixFile(id); 00453 } 00454 00455 00456 /* 00457 ** Information and control of an open file handle. 00458 */ 00459 static int unixFileControl(sqlite3_file *id, int op, void *pArg){ 00460 switch( op ){ 00461 case SQLITE_FCNTL_LOCKSTATE: { 00462 *(int*)pArg = SQLITE_LOCK_NONE; 00463 return SQLITE_OK; 00464 } 00465 } 00466 return SQLITE_ERROR; 00467 } 00468 00469 /* 00470 ** Return the sector size in bytes of the underlying block device for 00471 ** the specified file. This is almost always 512 bytes, but may be 00472 ** larger for some devices. 00473 ** 00474 ** SQLite code assumes this function cannot fail. It also assumes that 00475 ** if two files are created in the same file-system directory (i.e. 00476 ** a database and its journal file) that the sector size will be the 00477 ** same for both. 00478 */ 00479 static int unixSectorSize(sqlite3_file *id){ 00480 return SQLITE_DEFAULT_SECTOR_SIZE; 00481 } 00482 00483 /* 00484 ** Return the device characteristics for the file. This is always 0. 00485 */ 00486 static int unixDeviceCharacteristics(sqlite3_file *id){ 00487 return 0; 00488 } 00489 00490 /* 00491 ** Initialize the contents of the unixFile structure pointed to by pId. 00492 ** 00493 ** When locking extensions are enabled, the filepath and locking style 00494 ** are needed to determine the unixFile pMethod to use for locking operations. 00495 ** The locking-style specific lockingContext data structure is created 00496 ** and assigned here also. 00497 */ 00498 static int fillInUnixFile( 00499 sqlite3_vfs *pVfs, /* Pointer to vfs object */ 00500 int h, /* Open file descriptor of file being opened */ 00501 sqlite3_file *pId, /* Write to the unixFile structure here */ 00502 const char *zFilename /* Name of the file being opened */ 00503 ){ 00504 unixFile *pNew = (unixFile *)pId; 00505 int rc = SQLITE_OK; 00506 00507 /* Macro to define the static contents of an sqlite3_io_methods 00508 ** structure for a unix backend file. Different locking methods 00509 ** require different functions for the xClose, xLock, xUnlock and 00510 ** xCheckReservedLock methods. 00511 */ 00512 #define IOMETHODS(xClose, xLock, xUnlock, xCheckReservedLock) { \ 00513 1, /* iVersion */ \ 00514 xClose, /* xClose */ \ 00515 unixRead, /* xRead */ \ 00516 unixWrite, /* xWrite */ \ 00517 unixTruncate, /* xTruncate */ \ 00518 unixSync, /* xSync */ \ 00519 unixFileSize, /* xFileSize */ \ 00520 xLock, /* xLock */ \ 00521 xUnlock, /* xUnlock */ \ 00522 xCheckReservedLock, /* xCheckReservedLock */ \ 00523 unixFileControl, /* xFileControl */ \ 00524 unixSectorSize, /* xSectorSize */ \ 00525 unixDeviceCharacteristics /* xDeviceCapabilities */ \ 00526 } 00527 static sqlite3_io_methods aIoMethod[] = { 00528 IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock) 00529 }; 00530 00531 OSTRACE3("OPEN %-3d %s\n", h, zFilename); 00532 pNew->h = h; 00533 00534 pNew->lastErrno = 0; 00535 if( rc!=SQLITE_OK ){ 00536 close(h); 00537 }else{ 00538 pNew->pMethod = &aIoMethod[0]; 00539 OpenCounter(+1); 00540 } 00541 return rc; 00542 } 00543 00544 /* 00545 ** Create a temporary file name in zBuf. zBuf must be allocated 00546 ** by the calling process and must be big enough to hold at least 00547 ** pVfs->mxPathname bytes. 00548 */ 00549 static int getTempname(int nBuf, char *zBuf){ 00550 static const char *azDirs[] = { 00551 0, 00552 #ifdef __SYMBIAN32__ 00553 "c:\\system\\temp\\sqlite3", 00554 "c:\\temp", 00555 "c:", 00556 #else 00557 "/var/tmp", 00558 "/usr/tmp", 00559 "/tmp", 00560 #endif 00561 ".", 00562 }; 00563 static const unsigned char zChars[] = 00564 "abcdefghijklmnopqrstuvwxyz" 00565 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 00566 "0123456789"; 00567 int i, j; 00568 struct stat buf; 00569 const char *zDir = "."; 00570 00571 /* It's odd to simulate an io-error here, but really this is just 00572 ** using the io-error infrastructure to test that SQLite handles this 00573 ** function failing. 00574 */ 00575 SimulateIOError( return SQLITE_IOERR ); 00576 00577 azDirs[0] = sqlite3_temp_directory; 00578 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){ 00579 if( azDirs[i]==0 ) continue; 00580 if( stat(azDirs[i], &buf) ) continue; 00581 if( !S_ISDIR(buf.st_mode) ) continue; 00582 if( access(azDirs[i], 07) ) continue; 00583 zDir = azDirs[i]; 00584 break; 00585 } 00586 00587 /* Check that the output buffer is large enough for the temporary file 00588 ** name. If it is not, return SQLITE_ERROR. 00589 */ 00590 if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= nBuf ){ 00591 return SQLITE_ERROR; 00592 } 00593 00594 do{ 00595 sqlite3_snprintf(nBuf-17, zBuf, "%s" SEP_STR SQLITE_TEMP_FILE_PREFIX, zDir); 00596 j = strlen(zBuf); 00597 sqlite3_randomness(15, &zBuf[j]); 00598 for(i=0; i<15; i++, j++){ 00599 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; 00600 } 00601 zBuf[j] = 0; 00602 }while( access(zBuf,0)==0 ); 00603 return SQLITE_OK; 00604 } 00605 00606 00607 /* 00608 ** Open the file zPath. 00609 ** 00610 ** Previously, the SQLite OS layer used three functions in place of this 00611 ** one: 00612 ** 00613 ** sqlite3OsOpenReadWrite(); 00614 ** sqlite3OsOpenReadOnly(); 00615 ** sqlite3OsOpenExclusive(); 00616 ** 00617 ** These calls correspond to the following combinations of flags: 00618 ** 00619 ** ReadWrite() -> (READWRITE | CREATE) 00620 ** ReadOnly() -> (READONLY) 00621 ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) 00622 ** 00623 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If 00624 ** true, the file was configured to be automatically deleted when the 00625 ** file handle closed. To achieve the same effect using this new 00626 ** interface, add the DELETEONCLOSE flag to those specified above for 00627 ** OpenExclusive(). 00628 */ 00629 static int unixOpen( 00630 sqlite3_vfs *pVfs, 00631 const char *zPath, 00632 sqlite3_file *pFile, 00633 int flags, 00634 int *pOutFlags 00635 ){ 00636 int fd = 0; /* File descriptor returned by open() */ 00637 int oflags = 0; /* Flags to pass to open() */ 00638 #ifndef NDEBUG 00639 int eType = flags&0xFFFFFF00; /* Type of file to open */ 00640 #endif 00641 00642 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); 00643 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); 00644 int isCreate = (flags & SQLITE_OPEN_CREATE); 00645 int isReadonly = (flags & SQLITE_OPEN_READONLY); 00646 int isReadWrite = (flags & SQLITE_OPEN_READWRITE); 00647 00648 /* If argument zPath is a NULL pointer, this function is required to open 00649 ** a temporary file. Use this buffer to store the file name in. 00650 */ 00651 char zTmpname[MAX_PATHNAME+1]; 00652 const char *zName = zPath; 00653 00654 /* Check the following statements are true: 00655 ** 00656 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and 00657 ** (b) if CREATE is set, then READWRITE must also be set, and 00658 ** (c) if EXCLUSIVE is set, then CREATE must also be set. 00659 ** (d) if DELETEONCLOSE is set, then CREATE must also be set. 00660 */ 00661 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); 00662 assert(isCreate==0 || isReadWrite); 00663 assert(isExclusive==0 || isCreate); 00664 assert(isDelete==0 || isCreate); 00665 00666 /* The main DB, main journal, and master journal are never automatically 00667 ** deleted 00668 */ 00669 assert( eType!=SQLITE_OPEN_MAIN_DB || !isDelete ); 00670 assert( eType!=SQLITE_OPEN_MAIN_JOURNAL || !isDelete ); 00671 assert( eType!=SQLITE_OPEN_MASTER_JOURNAL || !isDelete ); 00672 00673 /* Assert that the upper layer has set one of the "file-type" flags. */ 00674 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB 00675 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 00676 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL 00677 || eType==SQLITE_OPEN_TRANSIENT_DB 00678 ); 00679 00680 memset(pFile, 0, sizeof(unixFile)); 00681 00682 if( !zName ){ 00683 int rc; 00684 assert(isDelete); 00685 rc = getTempname(MAX_PATHNAME+1, zTmpname); 00686 if( rc!=SQLITE_OK ){ 00687 return rc; 00688 } 00689 zName = zTmpname; 00690 } 00691 00692 if( isReadonly ) oflags |= O_RDONLY; 00693 if( isReadWrite ) oflags |= O_RDWR; 00694 if( isCreate ) oflags |= O_CREAT; 00695 if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW); 00696 oflags |= (O_LARGEFILE|O_BINARY); 00697 00698 fd = open(zName, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS); 00699 if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){ 00700 /* Failed to open the file for read/write access. Try read-only. */ 00701 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); 00702 flags |= SQLITE_OPEN_READONLY; 00703 return unixOpen(pVfs, zPath, pFile, flags, pOutFlags); 00704 } 00705 if( fd<0 ){ 00706 //logt("SQLITE_CANTOPEN in unixOpen"); 00707 return SQLITE_CANTOPEN; 00708 } 00709 if( isDelete ){ 00710 unlink(zName); 00711 } 00712 if( pOutFlags ){ 00713 *pOutFlags = flags; 00714 } 00715 00716 assert(fd!=0); 00717 00718 #ifdef FD_CLOEXEC 00719 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); 00720 #endif 00721 00722 return fillInUnixFile(pVfs, fd, pFile, zPath); 00723 } 00724 00725 /* 00726 ** Delete the file at zPath. If the dirSync argument is true, fsync() 00727 ** the directory after deleting the file. 00728 */ 00729 static int unixDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ 00730 int rc = SQLITE_OK; 00731 SimulateIOError(return SQLITE_IOERR_DELETE); 00732 unlink(zPath); 00733 return rc; 00734 } 00735 00736 /* 00737 ** Test the existance of or access permissions of file zPath. The 00738 ** test performed depends on the value of flags: 00739 ** 00740 ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists 00741 ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. 00742 ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. 00743 ** 00744 ** Otherwise return 0. 00745 */ 00746 static int unixAccess( 00747 sqlite3_vfs *pVfs, 00748 const char *zPath, 00749 int flags, 00750 int *pResOut 00751 ){ 00752 int amode = 0; 00753 SimulateIOError( return SQLITE_IOERR_ACCESS; ); 00754 switch( flags ){ 00755 case SQLITE_ACCESS_EXISTS: 00756 amode = F_OK; 00757 break; 00758 case SQLITE_ACCESS_READWRITE: 00759 amode = W_OK|R_OK; 00760 break; 00761 case SQLITE_ACCESS_READ: 00762 amode = R_OK; 00763 break; 00764 00765 default: 00766 assert(!"Invalid flags argument"); 00767 } 00768 *pResOut = (access(zPath, amode)==0); 00769 return SQLITE_OK; 00770 } 00771 00772 00773 /* 00774 ** Turn a relative pathname into a full pathname. The relative path 00775 ** is stored as a nul-terminated string in the buffer pointed to by 00776 ** zPath. 00777 ** 00778 ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes 00779 ** (in this case, MAX_PATHNAME bytes). The full-path is written to 00780 ** this buffer before returning. 00781 */ 00782 static int unixFullPathname( 00783 sqlite3_vfs *pVfs, /* Pointer to vfs object */ 00784 const char *zPath, /* Possibly relative input path */ 00785 int nOut, /* Size of output buffer in bytes */ 00786 char *zOut /* Output buffer */ 00787 ){ 00788 00789 /* It's odd to simulate an io-error here, but really this is just 00790 ** using the io-error infrastructure to test that SQLite handles this 00791 ** function failing. This function could fail if, for example, the 00792 ** current working directly has been unlinked. 00793 */ 00794 SimulateIOError( return SQLITE_ERROR ); 00795 00796 assert( pVfs->mxPathname==MAX_PATHNAME ); 00797 zOut[nOut-1] = '\0'; 00798 // Must try to check for both with or without drive letter case. 00799 if( (zPath[0] == SEP_CH) || 00800 (zPath[0] && (zPath[1] == ':') && zPath[2] == SEP_CH) ) { 00801 //logf("absolute path '%s'", zPath); 00802 sqlite3_snprintf(nOut, zOut, "%s", zPath); 00803 } else { 00804 int nCwd; 00805 if( getcwd(zOut, nOut-1)==0 ){ //logt("getcwd fail"); 00806 return SQLITE_CANTOPEN; 00807 } 00808 nCwd = strlen(zOut); 00809 sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], SEP_STR "%s", zPath); 00810 } 00811 return SQLITE_OK; 00812 } 00813 00814 00815 #ifndef SQLITE_OMIT_LOAD_EXTENSION 00816 #error !SQLITE_OMIT_LOAD_EXTENSION unsupported 00817 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ 00818 #define unixDlOpen 0 00819 #define unixDlError 0 00820 #define unixDlSym 0 00821 #define unixDlClose 0 00822 #endif 00823 00824 /* 00825 ** Write nBuf bytes of random data to the supplied buffer zBuf. 00826 */ 00827 static int unixRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ 00828 00829 assert(nBuf>=(sizeof(time_t)+sizeof(int))); 00830 00831 /* We have to initialize zBuf to prevent valgrind from reporting 00832 ** errors. The reports issued by valgrind are incorrect - we would 00833 ** prefer that the randomness be increased by making use of the 00834 ** uninitialized space in zBuf - but valgrind errors tend to worry 00835 ** some users. Rather than argue, it seems easier just to initialize 00836 ** the whole array and silence valgrind, even if that means less randomness 00837 ** in the random seed. 00838 ** 00839 ** When testing, initializing zBuf[] to zero is all we do. That means 00840 ** that we always use the same random number sequence. This makes the 00841 ** tests repeatable. 00842 */ 00843 memset(zBuf, 0, nBuf); 00844 #if !defined(SQLITE_TEST) 00845 { 00846 int pid; 00847 time_t t; 00848 time(&t); 00849 memcpy(zBuf, &t, sizeof(t)); 00850 pid = getpid(); 00851 memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid)); 00852 assert( sizeof(t)+sizeof(pid)<=nBuf ); 00853 nBuf = sizeof(t) + sizeof(pid); 00854 } 00855 #endif 00856 return nBuf; 00857 } 00858 00859 00860 /* 00861 ** Sleep for a little while. Return the amount of time slept. 00862 ** The argument is the number of microseconds we want to sleep. 00863 ** The return value is the number of microseconds of sleep actually 00864 ** requested from the underlying operating system, a number which 00865 ** might be greater than or equal to the argument, but not less 00866 ** than the argument. 00867 */ 00868 static int unixSleep(sqlite3_vfs *pVfs, int microseconds){ 00869 #if defined(HAVE_USLEEP) && HAVE_USLEEP 00870 usleep(microseconds); 00871 return microseconds; 00872 #else 00873 int seconds = (microseconds+999999)/1000000; 00874 sleep(seconds); 00875 return seconds*1000000; 00876 #endif 00877 } 00878 00879 /* 00880 ** The following variable, if set to a non-zero value, becomes the result 00881 ** returned from sqlite3OsCurrentTime(). This is used for testing. 00882 */ 00883 #ifdef SQLITE_TEST 00884 int sqlite3_current_time = 0; 00885 #endif 00886 00887 /* 00888 ** Find the current time (in Universal Coordinated Time). Write the 00889 ** current time and date as a Julian Day number into *prNow and 00890 ** return 0. Return 1 if the time and date cannot be found. 00891 */ 00892 static int unixCurrentTime(sqlite3_vfs *pVfs, double *prNow){ 00893 #ifdef NO_GETTOD 00894 time_t t; 00895 time(&t); 00896 *prNow = t/86400.0 + 2440587.5; 00897 #else 00898 struct timeval sNow; 00899 gettimeofday(&sNow, 0); 00900 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0; 00901 #endif 00902 #ifdef SQLITE_TEST 00903 if( sqlite3_current_time ){ 00904 *prNow = sqlite3_current_time/86400.0 + 2440587.5; 00905 } 00906 #endif 00907 return 0; 00908 } 00909 00910 static int unixGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ 00911 return 0; 00912 } 00913 00914 /* 00915 ** Initialize the operating system interface. 00916 */ 00917 int sqlite3_os_init(void){ 00918 /* Macro to define the static contents of an sqlite3_vfs structure for 00919 ** the unix backend. The two parameters are the values to use for 00920 ** the sqlite3_vfs.zName and sqlite3_vfs.pAppData fields, respectively. 00921 ** 00922 */ 00923 #define UNIXVFS(zVfsName, pVfsAppData) { \ 00924 1, /* iVersion */ \ 00925 sizeof(unixFile), /* szOsFile */ \ 00926 MAX_PATHNAME, /* mxPathname */ \ 00927 0, /* pNext */ \ 00928 zVfsName, /* zName */ \ 00929 (void *)pVfsAppData, /* pAppData */ \ 00930 unixOpen, /* xOpen */ \ 00931 unixDelete, /* xDelete */ \ 00932 unixAccess, /* xAccess */ \ 00933 unixFullPathname, /* xFullPathname */ \ 00934 unixDlOpen, /* xDlOpen */ \ 00935 unixDlError, /* xDlError */ \ 00936 unixDlSym, /* xDlSym */ \ 00937 unixDlClose, /* xDlClose */ \ 00938 unixRandomness, /* xRandomness */ \ 00939 unixSleep, /* xSleep */ \ 00940 unixCurrentTime, /* xCurrentTime */ \ 00941 unixGetLastError /* xGetLastError */ \ 00942 } 00943 00944 static sqlite3_vfs unixVfs = UNIXVFS("unix", 0); 00945 sqlite3_vfs_register(&unixVfs, 1); 00946 return SQLITE_OK; 00947 } 00948 00949 /* 00950 ** Shutdown the operating system interface. This is a no-op for unix. 00951 */ 00952 int sqlite3_os_end(void){ 00953 return SQLITE_OK; 00954 } 00955 00956 #endif /* SQLITE_OS_UNIX */
ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:55 2011 by Doxygen 1.6.1