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" |
| 18 | #include "os.h" |
drh | bd08af4 | 2007-04-05 21:58:33 +0000 | [diff] [blame] | 19 | #undef _SQLITE_OS_C_ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 20 | |
| 21 | /* |
| 22 | ** The following routines are convenience wrappers around methods |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 23 | ** of the sqlite3_file object. This is mostly just syntactic sugar. All |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 24 | ** of this would be completely automatic if SQLite were coded using |
| 25 | ** C++ instead of plain old C. |
| 26 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 27 | int sqlite3OsClose(sqlite3_file *pId){ |
| 28 | if( !pId->pMethods ) return SQLITE_OK; |
| 29 | return pId->pMethods->xClose(pId); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 30 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 31 | int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){ |
| 32 | return id->pMethods->xRead(id, pBuf, amt, offset); |
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 sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){ |
| 35 | return id->pMethods->xWrite(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 sqlite3OsTruncate(sqlite3_file *id, i64 size){ |
| 38 | return id->pMethods->xTruncate(id, size); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 39 | } |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame^] | 40 | int sqlite3OsSync(sqlite3_file *id, int flags){ |
| 41 | return id->pMethods->xSync(id, flags); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 42 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 43 | int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){ |
| 44 | return id->pMethods->xFileSize(id, pSize); |
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 sqlite3OsLock(sqlite3_file *id, int lockType){ |
| 47 | return id->pMethods->xLock(id, lockType); |
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 sqlite3OsUnlock(sqlite3_file *id, int lockType){ |
| 50 | return id->pMethods->xUnlock(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 sqlite3OsBreakLock(sqlite3_file *id){ |
| 53 | return id->pMethods->xBreakLock(id); |
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 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 58 | int sqlite3OsSectorSize(sqlite3_file *id){ |
| 59 | int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize; |
drh | 3ceeb75 | 2007-03-29 18:19:52 +0000 | [diff] [blame] | 60 | return xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE; |
danielk1977 | b472117 | 2007-03-19 05:54:48 +0000 | [diff] [blame] | 61 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 62 | int sqlite3OsDeviceCharacteristics(sqlite3_file *id){ |
| 63 | return id->pMethods->xDeviceCharacteristics(id); |
| 64 | } |
drh | 3f45902 | 2006-01-07 16:06:07 +0000 | [diff] [blame] | 65 | |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 66 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
| 67 | /* These methods are currently only used for testing and debugging. */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 68 | int sqlite3OsFileHandle(sqlite3_file *id){ |
| 69 | /* return id->pMethods->xFileHandle(id); */ |
| 70 | return 0; |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 71 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 72 | int sqlite3OsLockState(sqlite3_file *id){ |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame^] | 73 | return id->pMethods->xLockState(id); |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 74 | } |
| 75 | #endif |
| 76 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 77 | int 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); |
drh | 3f45902 | 2006-01-07 16:06:07 +0000 | [diff] [blame] | 85 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 86 | int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath){ |
| 87 | return pVfs->xDelete(pVfs->pAppData, zPath); |
| 88 | } |
| 89 | int sqlite3OsAccess(sqlite3_vfs *pVfs, const char *zPath, int flags){ |
| 90 | return pVfs->xAccess(pVfs->pAppData, zPath, flags); |
| 91 | } |
| 92 | int sqlite3OsGetTempName(sqlite3_vfs *pVfs, char *zBufOut){ |
| 93 | return pVfs->xGetTempName(pVfs->pAppData, zBufOut); |
| 94 | } |
| 95 | int sqlite3OsFullPathname(sqlite3_vfs *pVfs, const char *zPath, char *zPathOut){ |
| 96 | return pVfs->xFullPathname(pVfs->pAppData, zPath, zPathOut); |
| 97 | } |
| 98 | void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){ |
| 99 | return pVfs->xDlOpen(pVfs->pAppData, zPath); |
| 100 | } |
| 101 | void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ |
| 102 | pVfs->xDlError(pVfs->pAppData, nByte, zBufOut); |
| 103 | } |
| 104 | void *sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){ |
| 105 | return pVfs->xDlSym(pHandle, zSymbol); |
| 106 | } |
| 107 | void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){ |
| 108 | pVfs->xDlClose(pHandle); |
| 109 | } |
| 110 | int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ |
| 111 | return pVfs->xRandomness(pVfs->pAppData, nByte, zBufOut); |
| 112 | } |
| 113 | int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){ |
| 114 | return pVfs->xSleep(pVfs->pAppData, nMicro); |
| 115 | } |
| 116 | int sqlite3OsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ |
| 117 | return pVfs->xCurrentTime(pVfs->pAppData, pTimeOut); |
| 118 | } |
| 119 | |
| 120 | int 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 | } |
| 139 | int 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 | */ |
| 151 | extern sqlite3_vfs sqlite3DefaultVfs; |
| 152 | |
| 153 | sqlite3_vfs *sqlite3_find_vfs(const char *zVfs){ |
| 154 | return &sqlite3DefaultVfs; |
| 155 | } |
| 156 | |