drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2005 November 29 |
| 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 OS interface code that is common to all |
| 14 | ** architectures. |
| 15 | */ |
drh | 3f45902 | 2006-01-07 16:06:07 +0000 | [diff] [blame] | 16 | #define _SQLITE_OS_C_ 1 |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 17 | #include "sqliteInt.h" |
drh | bd08af4 | 2007-04-05 21:58:33 +0000 | [diff] [blame] | 18 | #undef _SQLITE_OS_C_ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 19 | |
| 20 | /* |
| 21 | ** The following routines are convenience wrappers around methods |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 22 | ** of the sqlite3_file object. This is mostly just syntactic sugar. All |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 23 | ** of this would be completely automatic if SQLite were coded using |
| 24 | ** C++ instead of plain old C. |
| 25 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 26 | int sqlite3OsClose(sqlite3_file *pId){ |
danielk1977 | c7b6017 | 2007-08-22 11:22:03 +0000 | [diff] [blame] | 27 | int rc = SQLITE_OK; |
| 28 | if( pId->pMethods ){ |
| 29 | rc = pId->pMethods->xClose(pId); |
| 30 | pId->pMethods = 0; |
| 31 | } |
| 32 | return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 33 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 34 | int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){ |
| 35 | return id->pMethods->xRead(id, pBuf, amt, offset); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 36 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 37 | int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){ |
| 38 | return id->pMethods->xWrite(id, pBuf, amt, offset); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 39 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 40 | int sqlite3OsTruncate(sqlite3_file *id, i64 size){ |
| 41 | return id->pMethods->xTruncate(id, size); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 42 | } |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 43 | int sqlite3OsSync(sqlite3_file *id, int flags){ |
| 44 | return id->pMethods->xSync(id, flags); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 45 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 46 | int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){ |
| 47 | return id->pMethods->xFileSize(id, pSize); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 48 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 49 | int sqlite3OsLock(sqlite3_file *id, int lockType){ |
| 50 | return id->pMethods->xLock(id, lockType); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 51 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 52 | int sqlite3OsUnlock(sqlite3_file *id, int lockType){ |
| 53 | return id->pMethods->xUnlock(id, lockType); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 54 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 55 | int sqlite3OsCheckReservedLock(sqlite3_file *id){ |
| 56 | return id->pMethods->xCheckReservedLock(id); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 57 | } |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame^] | 58 | int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){ |
| 59 | return id->pMethods->xFileControl(id,op,pArg); |
| 60 | } |
danielk1977 | 2ca0f86 | 2007-08-23 08:06:44 +0000 | [diff] [blame] | 61 | |
| 62 | #ifdef SQLITE_TEST |
| 63 | /* The following two variables are used to override the values returned |
| 64 | ** by the xSectorSize() and xDeviceCharacteristics() vfs methods for |
| 65 | ** testing purposes. They are usually set by a test command implemented |
| 66 | ** in test6.c. |
| 67 | */ |
| 68 | int sqlite3_test_sector_size = 0; |
| 69 | int sqlite3_test_device_characteristics = 0; |
| 70 | int sqlite3OsDeviceCharacteristics(sqlite3_file *id){ |
| 71 | int dc = id->pMethods->xDeviceCharacteristics(id); |
| 72 | return dc | sqlite3_test_device_characteristics; |
| 73 | } |
| 74 | int sqlite3OsSectorSize(sqlite3_file *id){ |
| 75 | if( sqlite3_test_sector_size==0 ){ |
| 76 | int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize; |
| 77 | return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE); |
| 78 | } |
| 79 | return sqlite3_test_sector_size; |
| 80 | } |
| 81 | #else |
| 82 | int sqlite3OsSectorSize(sqlite3_file *id){ |
| 83 | int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize; |
| 84 | return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE); |
| 85 | } |
| 86 | int sqlite3OsDeviceCharacteristics(sqlite3_file *id){ |
| 87 | return id->pMethods->xDeviceCharacteristics(id); |
| 88 | } |
| 89 | #endif |
drh | 3f45902 | 2006-01-07 16:06:07 +0000 | [diff] [blame] | 90 | |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 91 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
| 92 | /* These methods are currently only used for testing and debugging. */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 93 | int sqlite3OsFileHandle(sqlite3_file *id){ |
| 94 | /* return id->pMethods->xFileHandle(id); */ |
| 95 | return 0; |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 96 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 97 | int sqlite3OsLockState(sqlite3_file *id){ |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 98 | return id->pMethods->xLockState(id); |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 99 | } |
| 100 | #endif |
| 101 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 102 | /* |
| 103 | ** The next group of routines are convenience wrappers around the |
| 104 | ** VFS methods. |
| 105 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 106 | int sqlite3OsOpen( |
| 107 | sqlite3_vfs *pVfs, |
| 108 | const char *zPath, |
| 109 | sqlite3_file *pFile, |
| 110 | int flags, |
| 111 | int *pFlagsOut |
| 112 | ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 113 | return pVfs->xOpen(pVfs, zPath, pFile, flags, pFlagsOut); |
drh | 3f45902 | 2006-01-07 16:06:07 +0000 | [diff] [blame] | 114 | } |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 115 | int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 116 | return pVfs->xDelete(pVfs, zPath, dirSync); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 117 | } |
| 118 | int sqlite3OsAccess(sqlite3_vfs *pVfs, const char *zPath, int flags){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 119 | return pVfs->xAccess(pVfs, zPath, flags); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 120 | } |
| 121 | int sqlite3OsGetTempName(sqlite3_vfs *pVfs, char *zBufOut){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 122 | return pVfs->xGetTempName(pVfs, zBufOut); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 123 | } |
| 124 | int sqlite3OsFullPathname(sqlite3_vfs *pVfs, const char *zPath, char *zPathOut){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 125 | return pVfs->xFullPathname(pVfs, zPath, zPathOut); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 126 | } |
| 127 | void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 128 | return pVfs->xDlOpen(pVfs, zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 129 | } |
| 130 | void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 131 | pVfs->xDlError(pVfs, nByte, zBufOut); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 132 | } |
| 133 | void *sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 134 | return pVfs->xDlSym(pVfs, pHandle, zSymbol); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 135 | } |
| 136 | void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 137 | pVfs->xDlClose(pVfs, pHandle); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 138 | } |
| 139 | int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 140 | return pVfs->xRandomness(pVfs, nByte, zBufOut); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 141 | } |
| 142 | int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 143 | return pVfs->xSleep(pVfs, nMicro); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 144 | } |
| 145 | int sqlite3OsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 146 | return pVfs->xCurrentTime(pVfs, pTimeOut); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | int sqlite3OsOpenMalloc( |
| 150 | sqlite3_vfs *pVfs, |
| 151 | const char *zFile, |
| 152 | sqlite3_file **ppFile, |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 153 | int flags, |
| 154 | int *pOutFlags |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 155 | ){ |
| 156 | int rc = SQLITE_NOMEM; |
| 157 | sqlite3_file *pFile; |
| 158 | pFile = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile); |
| 159 | if( pFile ){ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 160 | rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 161 | if( rc!=SQLITE_OK ){ |
| 162 | sqlite3_free(pFile); |
| 163 | }else{ |
| 164 | *ppFile = pFile; |
| 165 | } |
| 166 | } |
| 167 | return rc; |
| 168 | } |
| 169 | int sqlite3OsCloseFree(sqlite3_file *pFile){ |
| 170 | int rc = SQLITE_OK; |
| 171 | if( pFile ){ |
| 172 | rc = sqlite3OsClose(pFile); |
| 173 | sqlite3_free(pFile); |
| 174 | } |
| 175 | return rc; |
| 176 | } |
| 177 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 178 | /* |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 179 | ** The list of all registered VFS implementations. This list is |
| 180 | ** initialized to the single VFS returned by sqlite3OsDefaultVfs() |
| 181 | ** upon the first call to sqlite3_vfs_find(). |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 182 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 183 | static sqlite3_vfs *vfsList = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 184 | |
| 185 | /* |
| 186 | ** Locate a VFS by name. If no name is given, simply return the |
| 187 | ** first VFS on the list. |
| 188 | */ |
| 189 | sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){ |
| 190 | sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); |
| 191 | sqlite3_vfs *pVfs; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 192 | static int isInit = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 193 | sqlite3_mutex_enter(mutex); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 194 | if( !isInit ){ |
| 195 | vfsList = sqlite3OsDefaultVfs(); |
| 196 | isInit = 1; |
| 197 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 198 | for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){ |
| 199 | if( zVfs==0 ) break; |
| 200 | if( strcmp(zVfs, pVfs->zName)==0 ) break; |
| 201 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 202 | sqlite3_mutex_leave(mutex); |
| 203 | return pVfs; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 204 | } |
| 205 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 206 | /* |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 207 | ** Unlink a VFS from the linked list |
| 208 | */ |
| 209 | static void vfsUnlink(sqlite3_vfs *pVfs){ |
| 210 | assert( sqlite3_mutex_held(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)) ); |
| 211 | if( vfsList==pVfs ){ |
| 212 | vfsList = pVfs->pNext; |
| 213 | }else{ |
| 214 | sqlite3_vfs *p = vfsList; |
| 215 | while( p->pNext && p->pNext!=pVfs ){ |
| 216 | p = p->pNext; |
| 217 | } |
| 218 | if( p->pNext==pVfs ){ |
| 219 | p->pNext = pVfs->pNext; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | ** Register a VFS with the system. It is harmless to register the same |
| 226 | ** VFS multiple times. The new VFS becomes the default if makeDflt is |
| 227 | ** true. |
| 228 | */ |
| 229 | int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){ |
| 230 | sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); |
| 231 | sqlite3_mutex_enter(mutex); |
| 232 | vfsUnlink(pVfs); |
| 233 | if( makeDflt || vfsList==0 ){ |
| 234 | pVfs->pNext = vfsList; |
| 235 | vfsList = pVfs; |
| 236 | }else{ |
| 237 | pVfs->pNext = vfsList->pNext; |
| 238 | pVfs->pNext = pVfs; |
| 239 | } |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 240 | assert(vfsList); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 241 | sqlite3_mutex_leave(mutex); |
| 242 | return SQLITE_OK; |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | ** Unregister a VFS so that it is no longer accessible. |
| 247 | */ |
| 248 | int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){ |
| 249 | sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); |
| 250 | sqlite3_mutex_enter(mutex); |
| 251 | vfsUnlink(pVfs); |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 252 | assert(vfsList); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 253 | sqlite3_mutex_leave(mutex); |
| 254 | return SQLITE_OK; |
| 255 | } |