blob: 28de37da01f551549116bfb5999f083d596af067 [file] [log] [blame]
drhe9bb50a2007-09-02 17:52:04 +00001 /*
drh054889e2005-11-30 03:20:31 +00002** 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*/
drh3f459022006-01-07 16:06:07 +000016#define _SQLITE_OS_C_ 1
drh054889e2005-11-30 03:20:31 +000017#include "sqliteInt.h"
drhbd08af42007-04-05 21:58:33 +000018#undef _SQLITE_OS_C_
drh054889e2005-11-30 03:20:31 +000019
20/*
21** The following routines are convenience wrappers around methods
danielk197762079062007-08-15 17:08:46 +000022** of the sqlite3_file object. This is mostly just syntactic sugar. All
drh054889e2005-11-30 03:20:31 +000023** of this would be completely automatic if SQLite were coded using
24** C++ instead of plain old C.
25*/
danielk1977b4b47412007-08-17 15:53:36 +000026int sqlite3OsClose(sqlite3_file *pId){
danielk1977c7b60172007-08-22 11:22:03 +000027 int rc = SQLITE_OK;
28 if( pId->pMethods ){
29 rc = pId->pMethods->xClose(pId);
30 pId->pMethods = 0;
31 }
32 return rc;
drh054889e2005-11-30 03:20:31 +000033}
danielk197762079062007-08-15 17:08:46 +000034int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
35 return id->pMethods->xRead(id, pBuf, amt, offset);
drh054889e2005-11-30 03:20:31 +000036}
danielk197762079062007-08-15 17:08:46 +000037int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
38 return id->pMethods->xWrite(id, pBuf, amt, offset);
drh054889e2005-11-30 03:20:31 +000039}
danielk197762079062007-08-15 17:08:46 +000040int sqlite3OsTruncate(sqlite3_file *id, i64 size){
41 return id->pMethods->xTruncate(id, size);
drh054889e2005-11-30 03:20:31 +000042}
danielk197790949c22007-08-17 16:50:38 +000043int sqlite3OsSync(sqlite3_file *id, int flags){
44 return id->pMethods->xSync(id, flags);
drh054889e2005-11-30 03:20:31 +000045}
danielk197762079062007-08-15 17:08:46 +000046int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
47 return id->pMethods->xFileSize(id, pSize);
drh054889e2005-11-30 03:20:31 +000048}
danielk197762079062007-08-15 17:08:46 +000049int sqlite3OsLock(sqlite3_file *id, int lockType){
50 return id->pMethods->xLock(id, lockType);
drh054889e2005-11-30 03:20:31 +000051}
danielk197762079062007-08-15 17:08:46 +000052int sqlite3OsUnlock(sqlite3_file *id, int lockType){
53 return id->pMethods->xUnlock(id, lockType);
drh054889e2005-11-30 03:20:31 +000054}
danielk197762079062007-08-15 17:08:46 +000055int sqlite3OsCheckReservedLock(sqlite3_file *id){
56 return id->pMethods->xCheckReservedLock(id);
drh054889e2005-11-30 03:20:31 +000057}
drhcc6bb3e2007-08-31 16:11:35 +000058int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
59 return id->pMethods->xFileControl(id,op,pArg);
60}
danielk19772ca0f862007-08-23 08:06:44 +000061
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
drh3f459022006-01-07 16:06:07 +000090
drhd677b3d2007-08-20 22:48:41 +000091/*
92** The next group of routines are convenience wrappers around the
93** VFS methods.
94*/
danielk1977b4b47412007-08-17 15:53:36 +000095int sqlite3OsOpen(
96 sqlite3_vfs *pVfs,
97 const char *zPath,
98 sqlite3_file *pFile,
99 int flags,
100 int *pFlagsOut
101){
drh153c62c2007-08-24 03:51:33 +0000102 return pVfs->xOpen(pVfs, zPath, pFile, flags, pFlagsOut);
drh3f459022006-01-07 16:06:07 +0000103}
danielk1977fee2d252007-08-18 10:59:19 +0000104int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
drh153c62c2007-08-24 03:51:33 +0000105 return pVfs->xDelete(pVfs, zPath, dirSync);
danielk1977b4b47412007-08-17 15:53:36 +0000106}
107int sqlite3OsAccess(sqlite3_vfs *pVfs, const char *zPath, int flags){
drh153c62c2007-08-24 03:51:33 +0000108 return pVfs->xAccess(pVfs, zPath, flags);
danielk1977b4b47412007-08-17 15:53:36 +0000109}
danielk1977adfb9b02007-09-17 07:02:56 +0000110int sqlite3OsGetTempname(sqlite3_vfs *pVfs, int nBufOut, char *zBufOut){
111 return pVfs->xGetTempname(pVfs, nBufOut, zBufOut);
danielk1977b4b47412007-08-17 15:53:36 +0000112}
danielk1977adfb9b02007-09-17 07:02:56 +0000113int sqlite3OsFullPathname(
114 sqlite3_vfs *pVfs,
115 const char *zPath,
116 int nPathOut,
117 char *zPathOut
118){
119 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
danielk1977b4b47412007-08-17 15:53:36 +0000120}
121void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
drh153c62c2007-08-24 03:51:33 +0000122 return pVfs->xDlOpen(pVfs, zPath);
danielk1977b4b47412007-08-17 15:53:36 +0000123}
124void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
drh153c62c2007-08-24 03:51:33 +0000125 pVfs->xDlError(pVfs, nByte, zBufOut);
danielk1977b4b47412007-08-17 15:53:36 +0000126}
127void *sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){
drh153c62c2007-08-24 03:51:33 +0000128 return pVfs->xDlSym(pVfs, pHandle, zSymbol);
danielk1977b4b47412007-08-17 15:53:36 +0000129}
130void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
drh153c62c2007-08-24 03:51:33 +0000131 pVfs->xDlClose(pVfs, pHandle);
danielk1977b4b47412007-08-17 15:53:36 +0000132}
133int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
drh153c62c2007-08-24 03:51:33 +0000134 return pVfs->xRandomness(pVfs, nByte, zBufOut);
danielk1977b4b47412007-08-17 15:53:36 +0000135}
136int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
drh153c62c2007-08-24 03:51:33 +0000137 return pVfs->xSleep(pVfs, nMicro);
danielk1977b4b47412007-08-17 15:53:36 +0000138}
139int sqlite3OsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
drh153c62c2007-08-24 03:51:33 +0000140 return pVfs->xCurrentTime(pVfs, pTimeOut);
danielk1977b4b47412007-08-17 15:53:36 +0000141}
142
143int sqlite3OsOpenMalloc(
144 sqlite3_vfs *pVfs,
145 const char *zFile,
146 sqlite3_file **ppFile,
danielk1977967a4a12007-08-20 14:23:44 +0000147 int flags,
148 int *pOutFlags
danielk1977b4b47412007-08-17 15:53:36 +0000149){
150 int rc = SQLITE_NOMEM;
151 sqlite3_file *pFile;
152 pFile = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile);
153 if( pFile ){
danielk1977967a4a12007-08-20 14:23:44 +0000154 rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
danielk1977b4b47412007-08-17 15:53:36 +0000155 if( rc!=SQLITE_OK ){
156 sqlite3_free(pFile);
157 }else{
158 *ppFile = pFile;
159 }
160 }
161 return rc;
162}
163int sqlite3OsCloseFree(sqlite3_file *pFile){
164 int rc = SQLITE_OK;
165 if( pFile ){
166 rc = sqlite3OsClose(pFile);
167 sqlite3_free(pFile);
168 }
169 return rc;
170}
171
drhd677b3d2007-08-20 22:48:41 +0000172/*
drh153c62c2007-08-24 03:51:33 +0000173** The list of all registered VFS implementations. This list is
174** initialized to the single VFS returned by sqlite3OsDefaultVfs()
175** upon the first call to sqlite3_vfs_find().
drhd677b3d2007-08-20 22:48:41 +0000176*/
drh153c62c2007-08-24 03:51:33 +0000177static sqlite3_vfs *vfsList = 0;
drhd677b3d2007-08-20 22:48:41 +0000178
179/*
180** Locate a VFS by name. If no name is given, simply return the
181** first VFS on the list.
182*/
183sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
184 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
185 sqlite3_vfs *pVfs;
drh153c62c2007-08-24 03:51:33 +0000186 static int isInit = 0;
drhd677b3d2007-08-20 22:48:41 +0000187 sqlite3_mutex_enter(mutex);
drh153c62c2007-08-24 03:51:33 +0000188 if( !isInit ){
189 vfsList = sqlite3OsDefaultVfs();
190 isInit = 1;
191 }
drhd677b3d2007-08-20 22:48:41 +0000192 for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
193 if( zVfs==0 ) break;
194 if( strcmp(zVfs, pVfs->zName)==0 ) break;
195 }
drhd677b3d2007-08-20 22:48:41 +0000196 sqlite3_mutex_leave(mutex);
197 return pVfs;
danielk1977b4b47412007-08-17 15:53:36 +0000198}
199
drhd677b3d2007-08-20 22:48:41 +0000200/*
drhd677b3d2007-08-20 22:48:41 +0000201** Unlink a VFS from the linked list
202*/
203static void vfsUnlink(sqlite3_vfs *pVfs){
204 assert( sqlite3_mutex_held(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)) );
205 if( vfsList==pVfs ){
206 vfsList = pVfs->pNext;
207 }else{
208 sqlite3_vfs *p = vfsList;
209 while( p->pNext && p->pNext!=pVfs ){
210 p = p->pNext;
211 }
212 if( p->pNext==pVfs ){
213 p->pNext = pVfs->pNext;
214 }
215 }
216}
217
218/*
219** Register a VFS with the system. It is harmless to register the same
220** VFS multiple times. The new VFS becomes the default if makeDflt is
221** true.
222*/
223int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
224 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
drhe9bb50a2007-09-02 17:52:04 +0000225 sqlite3_vfs_find(0); /* Make sure we are initialized */
drhd677b3d2007-08-20 22:48:41 +0000226 sqlite3_mutex_enter(mutex);
227 vfsUnlink(pVfs);
228 if( makeDflt || vfsList==0 ){
229 pVfs->pNext = vfsList;
230 vfsList = pVfs;
231 }else{
232 pVfs->pNext = vfsList->pNext;
danielk197795c8a542007-09-01 06:51:27 +0000233 vfsList->pNext = pVfs;
drhd677b3d2007-08-20 22:48:41 +0000234 }
danielk1977f1da17a2007-08-21 13:07:46 +0000235 assert(vfsList);
drhd677b3d2007-08-20 22:48:41 +0000236 sqlite3_mutex_leave(mutex);
237 return SQLITE_OK;
238}
239
240/*
241** Unregister a VFS so that it is no longer accessible.
242*/
243int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
244 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
245 sqlite3_mutex_enter(mutex);
246 vfsUnlink(pVfs);
danielk1977f1da17a2007-08-21 13:07:46 +0000247 assert(vfsList);
drhd677b3d2007-08-20 22:48:41 +0000248 sqlite3_mutex_leave(mutex);
249 return SQLITE_OK;
250}