drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2001 September 16 |
| 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 header file (together with is companion C source-code file |
| 14 | ** "os.c") attempt to abstract the underlying operating system so that |
| 15 | ** the SQLite library will work on both POSIX and windows systems. |
| 16 | */ |
| 17 | #ifndef _SQLITE_OS_H_ |
| 18 | #define _SQLITE_OS_H_ |
| 19 | |
drh | 829e802 | 2002-11-06 14:08:11 +0000 | [diff] [blame] | 20 | /* |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 21 | ** Helpful hint: To get this to compile on HP/UX, add -D_INCLUDE_POSIX_SOURCE |
| 22 | ** to the compiler command line. |
| 23 | */ |
| 24 | |
| 25 | /* |
drh | 829e802 | 2002-11-06 14:08:11 +0000 | [diff] [blame] | 26 | ** These #defines should enable >2GB file support on Posix if the |
| 27 | ** underlying operating system supports it. If the OS lacks |
| 28 | ** large file support, or if the OS is windows, these should be no-ops. |
drh | 8766c34 | 2002-11-09 00:33:15 +0000 | [diff] [blame] | 29 | ** |
| 30 | ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch |
| 31 | ** on the compiler command line. This is necessary if you are compiling |
| 32 | ** on a recent machine (ex: RedHat 7.2) but you want your code to work |
| 33 | ** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2 |
| 34 | ** without this option, LFS is enable. But LFS does not exist in the kernel |
| 35 | ** in RedHat 6.0, so the code won't work. Hence, for maximum binary |
| 36 | ** portability you should omit LFS. |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 37 | ** |
| 38 | ** Similar is true for MacOS. LFS is only supported on MacOS 9 and later. |
drh | 829e802 | 2002-11-06 14:08:11 +0000 | [diff] [blame] | 39 | */ |
drh | 8766c34 | 2002-11-09 00:33:15 +0000 | [diff] [blame] | 40 | #ifndef SQLITE_DISABLE_LFS |
| 41 | # define _LARGE_FILE 1 |
drh | ab9426e | 2004-02-11 16:38:06 +0000 | [diff] [blame] | 42 | # ifndef _FILE_OFFSET_BITS |
| 43 | # define _FILE_OFFSET_BITS 64 |
| 44 | # endif |
drh | 8766c34 | 2002-11-09 00:33:15 +0000 | [diff] [blame] | 45 | # define _LARGEFILE_SOURCE 1 |
| 46 | #endif |
drh | 829e802 | 2002-11-06 14:08:11 +0000 | [diff] [blame] | 47 | |
| 48 | /* |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 49 | ** Temporary files are named starting with this prefix followed by 16 random |
| 50 | ** alphanumeric characters, and no file extension. They are stored in the |
| 51 | ** OS's standard temporary file directory, and are deleted prior to exit. |
| 52 | ** If sqlite is being embedded in another program, you may wish to change the |
| 53 | ** prefix to reflect your program's name, so that if your program exits |
| 54 | ** prematurely, old temporary files can be easily identified. This can be done |
| 55 | ** using -DTEMP_FILE_PREFIX=myprefix_ on the compiler command line. |
| 56 | */ |
| 57 | #ifndef TEMP_FILE_PREFIX |
| 58 | # define TEMP_FILE_PREFIX "sqlite_" |
| 59 | #endif |
| 60 | |
| 61 | /* |
| 62 | ** Figure out if we are dealing with Unix, Windows or MacOS. |
| 63 | ** |
| 64 | ** N.B. MacOS means Mac Classic (or Carbon). Treat Darwin (OS X) as Unix. |
| 65 | ** The MacOS build is designed to use CodeWarrior (tested with v8) |
drh | 829e802 | 2002-11-06 14:08:11 +0000 | [diff] [blame] | 66 | */ |
drh | 27a3220 | 2002-03-20 00:00:29 +0000 | [diff] [blame] | 67 | #ifndef OS_UNIX |
| 68 | # ifndef OS_WIN |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 69 | # ifndef OS_MAC |
| 70 | # if defined(__MACOS__) |
| 71 | # define OS_MAC 1 |
| 72 | # define OS_WIN 0 |
| 73 | # define OS_UNIX 0 |
| 74 | # elif defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__) |
| 75 | # define OS_MAC 0 |
| 76 | # define OS_WIN 1 |
| 77 | # define OS_UNIX 0 |
| 78 | # else |
| 79 | # define OS_MAC 0 |
| 80 | # define OS_WIN 0 |
| 81 | # define OS_UNIX 1 |
| 82 | # endif |
drh | 27a3220 | 2002-03-20 00:00:29 +0000 | [diff] [blame] | 83 | # else |
| 84 | # define OS_WIN 0 |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 85 | # define OS_UNIX 0 |
drh | 27a3220 | 2002-03-20 00:00:29 +0000 | [diff] [blame] | 86 | # endif |
| 87 | # else |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 88 | # define OS_MAC 0 |
drh | 27a3220 | 2002-03-20 00:00:29 +0000 | [diff] [blame] | 89 | # define OS_UNIX 0 |
| 90 | # endif |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 91 | #else |
| 92 | # define OS_MAC 0 |
drh | e5e3760 | 2003-08-16 13:10:51 +0000 | [diff] [blame] | 93 | # ifndef OS_WIN |
| 94 | # define OS_WIN 0 |
| 95 | # endif |
drh | 1ab4300 | 2002-01-14 09:28:19 +0000 | [diff] [blame] | 96 | #endif |
| 97 | |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 98 | /* |
| 99 | ** A handle for an open file is stored in an OsFile object. |
| 100 | */ |
| 101 | #if OS_UNIX |
drh | 20e9ab1 | 2002-11-06 00:59:44 +0000 | [diff] [blame] | 102 | # include <sys/types.h> |
| 103 | # include <sys/stat.h> |
| 104 | # include <fcntl.h> |
| 105 | # include <unistd.h> |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 106 | typedef struct OsFile OsFile; |
| 107 | struct OsFile { |
drh | 31e95bc | 2004-01-12 00:39:05 +0000 | [diff] [blame] | 108 | struct openCnt *pOpen; /* Info about all open fd's on this inode */ |
| 109 | struct lockInfo *pLock; /* Info about locks on this inode */ |
| 110 | int fd; /* The file descriptor */ |
| 111 | int locked; /* True if this instance holds the lock */ |
| 112 | int dirfd; /* File descriptor for the directory */ |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 113 | }; |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 114 | # define SQLITE_TEMPNAME_SIZE 200 |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 115 | # if defined(HAVE_USLEEP) && HAVE_USLEEP |
| 116 | # define SQLITE_MIN_SLEEP_MS 1 |
| 117 | # else |
| 118 | # define SQLITE_MIN_SLEEP_MS 1000 |
| 119 | # endif |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 120 | #endif |
| 121 | |
| 122 | #if OS_WIN |
drh | 254cba2 | 2001-09-20 01:44:42 +0000 | [diff] [blame] | 123 | #include <windows.h> |
| 124 | #include <winbase.h> |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 125 | typedef struct OsFile OsFile; |
| 126 | struct OsFile { |
drh | d1efac5 | 2002-08-14 12:56:54 +0000 | [diff] [blame] | 127 | HANDLE h; /* Handle for accessing the file */ |
| 128 | int locked; /* 0: unlocked, <0: write lock, >0: read lock */ |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 129 | }; |
drh | 7cdbd32 | 2002-11-20 11:07:59 +0000 | [diff] [blame] | 130 | # if defined(_MSC_VER) || defined(__BORLANDC__) |
drh | 829e802 | 2002-11-06 14:08:11 +0000 | [diff] [blame] | 131 | typedef __int64 off_t; |
| 132 | # else |
drh | e5e3760 | 2003-08-16 13:10:51 +0000 | [diff] [blame] | 133 | # if !defined(_CYGWIN_TYPES_H) |
| 134 | typedef long long off_t; |
dougcurrie | ae53418 | 2003-12-24 01:41:19 +0000 | [diff] [blame] | 135 | # if defined(__MINGW32__) |
| 136 | # define _OFF_T_ |
| 137 | # endif |
drh | e5e3760 | 2003-08-16 13:10:51 +0000 | [diff] [blame] | 138 | # endif |
drh | 829e802 | 2002-11-06 14:08:11 +0000 | [diff] [blame] | 139 | # endif |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 140 | # define SQLITE_TEMPNAME_SIZE (MAX_PATH+50) |
drh | 254cba2 | 2001-09-20 01:44:42 +0000 | [diff] [blame] | 141 | # define SQLITE_MIN_SLEEP_MS 1 |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 142 | #endif |
| 143 | |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 144 | #if OS_MAC |
| 145 | # include <unistd.h> |
| 146 | # include <Files.h> |
| 147 | typedef struct OsFile OsFile; |
| 148 | struct OsFile { |
| 149 | SInt16 refNum; /* Data fork/file reference number */ |
| 150 | SInt16 refNumRF; /* Resource fork reference number (for locking) */ |
| 151 | int locked; /* 0: unlocked, <0: write lock, >0: read lock */ |
| 152 | int delOnClose; /* True if file is to be deleted on close */ |
| 153 | char *pathToDel; /* Name of file to delete on close */ |
| 154 | }; |
| 155 | # ifdef _LARGE_FILE |
| 156 | typedef SInt64 off_t; |
| 157 | # else |
| 158 | typedef SInt32 off_t; |
| 159 | # endif |
| 160 | # define SQLITE_TEMPNAME_SIZE _MAX_PATH |
| 161 | # define SQLITE_MIN_SLEEP_MS 17 |
| 162 | #endif |
| 163 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame^] | 164 | /* |
| 165 | ** Macros to determine whether the machine is big or little endian, |
| 166 | ** evaluated at runtime. |
| 167 | */ |
| 168 | static const int sqlite3_one = 1; |
| 169 | #define SQLITE3_BIGENDIAN (*(char *)(&sqlite3_one)==0) |
| 170 | #define SQLITE3_LITTLEENDIAN (*(char *)(&sqlite3_one)==1) |
| 171 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 172 | int sqlite3OsDelete(const char*); |
| 173 | int sqlite3OsFileExists(const char*); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 174 | int sqliteOsFileRename(const char*, const char*); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 175 | int sqlite3OsOpenReadWrite(const char*, OsFile*, int*); |
| 176 | int sqlite3OsOpenExclusive(const char*, OsFile*, int); |
| 177 | int sqlite3OsOpenReadOnly(const char*, OsFile*); |
| 178 | int sqlite3OsOpenDirectory(const char*, OsFile*); |
| 179 | int sqlite3OsTempFileName(char*); |
| 180 | int sqlite3OsClose(OsFile*); |
| 181 | int sqlite3OsRead(OsFile*, void*, int amt); |
| 182 | int sqlite3OsWrite(OsFile*, const void*, int amt); |
| 183 | int sqlite3OsSeek(OsFile*, off_t offset); |
| 184 | int sqlite3OsSync(OsFile*); |
| 185 | int sqlite3OsTruncate(OsFile*, off_t size); |
| 186 | int sqlite3OsFileSize(OsFile*, off_t *pSize); |
| 187 | int sqlite3OsReadLock(OsFile*); |
| 188 | int sqlite3OsWriteLock(OsFile*); |
| 189 | int sqlite3OsUnlock(OsFile*); |
| 190 | int sqlite3OsRandomSeed(char*); |
| 191 | int sqlite3OsSleep(int ms); |
| 192 | int sqlite3OsCurrentTime(double*); |
| 193 | void sqlite3OsEnterMutex(void); |
| 194 | void sqlite3OsLeaveMutex(void); |
| 195 | char *sqlite3OsFullPathname(const char*); |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 196 | |
| 197 | |
| 198 | |
| 199 | #endif /* _SQLITE_OS_H_ */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 200 | |
| 201 | |
| 202 | |