blob: 73f067bbcf8247ee404d523f3711f810ef539c3b [file] [log] [blame]
drh054889e2005-11-30 03:20:31 +00001/*
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*/
drh3f459022006-01-07 16:06:07 +000016#define _SQLITE_OS_C_ 1
drh054889e2005-11-30 03:20:31 +000017#include "sqliteInt.h"
18#include "os.h"
drhbd08af42007-04-05 21:58:33 +000019#undef _SQLITE_OS_C_
drh054889e2005-11-30 03:20:31 +000020
21/*
22** The following routines are convenience wrappers around methods
danielk197762079062007-08-15 17:08:46 +000023** of the sqlite3_file object. This is mostly just syntactic sugar. All
drh054889e2005-11-30 03:20:31 +000024** of this would be completely automatic if SQLite were coded using
25** C++ instead of plain old C.
26*/
danielk1977b4b47412007-08-17 15:53:36 +000027int sqlite3OsClose(sqlite3_file *pId){
28 if( !pId->pMethods ) return SQLITE_OK;
29 return pId->pMethods->xClose(pId);
drh054889e2005-11-30 03:20:31 +000030}
danielk197762079062007-08-15 17:08:46 +000031int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
32 return id->pMethods->xRead(id, pBuf, amt, offset);
drh054889e2005-11-30 03:20:31 +000033}
danielk197762079062007-08-15 17:08:46 +000034int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
35 return id->pMethods->xWrite(id, pBuf, amt, offset);
drh054889e2005-11-30 03:20:31 +000036}
danielk197762079062007-08-15 17:08:46 +000037int sqlite3OsTruncate(sqlite3_file *id, i64 size){
38 return id->pMethods->xTruncate(id, size);
drh054889e2005-11-30 03:20:31 +000039}
danielk197790949c22007-08-17 16:50:38 +000040int sqlite3OsSync(sqlite3_file *id, int flags){
41 return id->pMethods->xSync(id, flags);
drh054889e2005-11-30 03:20:31 +000042}
danielk197762079062007-08-15 17:08:46 +000043int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
44 return id->pMethods->xFileSize(id, pSize);
drh054889e2005-11-30 03:20:31 +000045}
danielk197762079062007-08-15 17:08:46 +000046int sqlite3OsLock(sqlite3_file *id, int lockType){
47 return id->pMethods->xLock(id, lockType);
drh054889e2005-11-30 03:20:31 +000048}
danielk197762079062007-08-15 17:08:46 +000049int sqlite3OsUnlock(sqlite3_file *id, int lockType){
50 return id->pMethods->xUnlock(id, lockType);
drh054889e2005-11-30 03:20:31 +000051}
danielk197762079062007-08-15 17:08:46 +000052int sqlite3OsBreakLock(sqlite3_file *id){
53 return id->pMethods->xBreakLock(id);
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}
danielk197762079062007-08-15 17:08:46 +000058int sqlite3OsSectorSize(sqlite3_file *id){
59 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
danielk1977967a4a12007-08-20 14:23:44 +000060 return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
danielk1977b4721172007-03-19 05:54:48 +000061}
danielk197762079062007-08-15 17:08:46 +000062int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
63 return id->pMethods->xDeviceCharacteristics(id);
64}
drh3f459022006-01-07 16:06:07 +000065
drh87cc3b32007-05-08 21:45:27 +000066#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
67 /* These methods are currently only used for testing and debugging. */
danielk197762079062007-08-15 17:08:46 +000068 int sqlite3OsFileHandle(sqlite3_file *id){
69 /* return id->pMethods->xFileHandle(id); */
70 return 0;
drh87cc3b32007-05-08 21:45:27 +000071 }
danielk197762079062007-08-15 17:08:46 +000072 int sqlite3OsLockState(sqlite3_file *id){
danielk197790949c22007-08-17 16:50:38 +000073 return id->pMethods->xLockState(id);
drh87cc3b32007-05-08 21:45:27 +000074 }
75#endif
76
danielk1977b4b47412007-08-17 15:53:36 +000077int sqlite3OsOpen(
78 sqlite3_vfs *pVfs,
79 const char *zPath,
80 sqlite3_file *pFile,
81 int flags,
82 int *pFlagsOut
83){
danielk1977967a4a12007-08-20 14:23:44 +000084#if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_DISKIO)
85 return sqlite3CrashFileOpen(pVfs, zPath, pFile, flags, pFlagsOut);
86#endif
danielk1977b4b47412007-08-17 15:53:36 +000087 return pVfs->xOpen(pVfs->pAppData, zPath, pFile, flags, pFlagsOut);
drh3f459022006-01-07 16:06:07 +000088}
danielk1977fee2d252007-08-18 10:59:19 +000089int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
90 return pVfs->xDelete(pVfs->pAppData, zPath, dirSync);
danielk1977b4b47412007-08-17 15:53:36 +000091}
92int sqlite3OsAccess(sqlite3_vfs *pVfs, const char *zPath, int flags){
93 return pVfs->xAccess(pVfs->pAppData, zPath, flags);
94}
95int sqlite3OsGetTempName(sqlite3_vfs *pVfs, char *zBufOut){
96 return pVfs->xGetTempName(pVfs->pAppData, zBufOut);
97}
98int sqlite3OsFullPathname(sqlite3_vfs *pVfs, const char *zPath, char *zPathOut){
99 return pVfs->xFullPathname(pVfs->pAppData, zPath, zPathOut);
100}
101void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
102 return pVfs->xDlOpen(pVfs->pAppData, zPath);
103}
104void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
105 pVfs->xDlError(pVfs->pAppData, nByte, zBufOut);
106}
107void *sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){
108 return pVfs->xDlSym(pHandle, zSymbol);
109}
110void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
111 pVfs->xDlClose(pHandle);
112}
113int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
114 return pVfs->xRandomness(pVfs->pAppData, nByte, zBufOut);
115}
116int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
117 return pVfs->xSleep(pVfs->pAppData, nMicro);
118}
119int sqlite3OsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
120 return pVfs->xCurrentTime(pVfs->pAppData, pTimeOut);
121}
122
123int sqlite3OsOpenMalloc(
124 sqlite3_vfs *pVfs,
125 const char *zFile,
126 sqlite3_file **ppFile,
danielk1977967a4a12007-08-20 14:23:44 +0000127 int flags,
128 int *pOutFlags
danielk1977b4b47412007-08-17 15:53:36 +0000129){
130 int rc = SQLITE_NOMEM;
131 sqlite3_file *pFile;
132 pFile = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile);
133 if( pFile ){
danielk1977967a4a12007-08-20 14:23:44 +0000134 rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
danielk1977b4b47412007-08-17 15:53:36 +0000135 if( rc!=SQLITE_OK ){
136 sqlite3_free(pFile);
137 }else{
138 *ppFile = pFile;
139 }
140 }
141 return rc;
142}
143int sqlite3OsCloseFree(sqlite3_file *pFile){
144 int rc = SQLITE_OK;
145 if( pFile ){
146 rc = sqlite3OsClose(pFile);
147 sqlite3_free(pFile);
148 }
149 return rc;
150}
151
152/*
153** Default vfs implementation. Defined by the various os_X.c implementations.
154*/
155extern sqlite3_vfs sqlite3DefaultVfs;
156
157sqlite3_vfs *sqlite3_find_vfs(const char *zVfs){
158 return &sqlite3DefaultVfs;
159}
160