blob: ed5c98e87bff01c6901df8788766fa80459273ca [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;
drh3ceeb752007-03-29 18:19:52 +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){
84 return pVfs->xOpen(pVfs->pAppData, zPath, pFile, flags, pFlagsOut);
drh3f459022006-01-07 16:06:07 +000085}
danielk1977b4b47412007-08-17 15:53:36 +000086int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath){
87 return pVfs->xDelete(pVfs->pAppData, zPath);
88}
89int sqlite3OsAccess(sqlite3_vfs *pVfs, const char *zPath, int flags){
90 return pVfs->xAccess(pVfs->pAppData, zPath, flags);
91}
92int sqlite3OsGetTempName(sqlite3_vfs *pVfs, char *zBufOut){
93 return pVfs->xGetTempName(pVfs->pAppData, zBufOut);
94}
95int sqlite3OsFullPathname(sqlite3_vfs *pVfs, const char *zPath, char *zPathOut){
96 return pVfs->xFullPathname(pVfs->pAppData, zPath, zPathOut);
97}
98void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
99 return pVfs->xDlOpen(pVfs->pAppData, zPath);
100}
101void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
102 pVfs->xDlError(pVfs->pAppData, nByte, zBufOut);
103}
104void *sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){
105 return pVfs->xDlSym(pHandle, zSymbol);
106}
107void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
108 pVfs->xDlClose(pHandle);
109}
110int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
111 return pVfs->xRandomness(pVfs->pAppData, nByte, zBufOut);
112}
113int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
114 return pVfs->xSleep(pVfs->pAppData, nMicro);
115}
116int sqlite3OsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
117 return pVfs->xCurrentTime(pVfs->pAppData, pTimeOut);
118}
119
120int sqlite3OsOpenMalloc(
121 sqlite3_vfs *pVfs,
122 const char *zFile,
123 sqlite3_file **ppFile,
124 int flags
125){
126 int rc = SQLITE_NOMEM;
127 sqlite3_file *pFile;
128 pFile = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile);
129 if( pFile ){
130 rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, 0);
131 if( rc!=SQLITE_OK ){
132 sqlite3_free(pFile);
133 }else{
134 *ppFile = pFile;
135 }
136 }
137 return rc;
138}
139int sqlite3OsCloseFree(sqlite3_file *pFile){
140 int rc = SQLITE_OK;
141 if( pFile ){
142 rc = sqlite3OsClose(pFile);
143 sqlite3_free(pFile);
144 }
145 return rc;
146}
147
148/*
149** Default vfs implementation. Defined by the various os_X.c implementations.
150*/
151extern sqlite3_vfs sqlite3DefaultVfs;
152
153sqlite3_vfs *sqlite3_find_vfs(const char *zVfs){
154 return &sqlite3DefaultVfs;
155}
156