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 |
| 42 | # define _FILE_OFFSET_BITS 64 |
| 43 | # define _LARGEFILE_SOURCE 1 |
| 44 | #endif |
drh | 829e802 | 2002-11-06 14:08:11 +0000 | [diff] [blame] | 45 | |
| 46 | /* |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 47 | ** Temporary files are named starting with this prefix followed by 16 random |
| 48 | ** alphanumeric characters, and no file extension. They are stored in the |
| 49 | ** OS's standard temporary file directory, and are deleted prior to exit. |
| 50 | ** If sqlite is being embedded in another program, you may wish to change the |
| 51 | ** prefix to reflect your program's name, so that if your program exits |
| 52 | ** prematurely, old temporary files can be easily identified. This can be done |
| 53 | ** using -DTEMP_FILE_PREFIX=myprefix_ on the compiler command line. |
| 54 | */ |
| 55 | #ifndef TEMP_FILE_PREFIX |
| 56 | # define TEMP_FILE_PREFIX "sqlite_" |
| 57 | #endif |
| 58 | |
| 59 | /* |
| 60 | ** Figure out if we are dealing with Unix, Windows or MacOS. |
| 61 | ** |
| 62 | ** N.B. MacOS means Mac Classic (or Carbon). Treat Darwin (OS X) as Unix. |
| 63 | ** The MacOS build is designed to use CodeWarrior (tested with v8) |
drh | 829e802 | 2002-11-06 14:08:11 +0000 | [diff] [blame] | 64 | */ |
drh | 27a3220 | 2002-03-20 00:00:29 +0000 | [diff] [blame] | 65 | #ifndef OS_UNIX |
| 66 | # ifndef OS_WIN |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 67 | # ifndef OS_MAC |
| 68 | # if defined(__MACOS__) |
| 69 | # define OS_MAC 1 |
| 70 | # define OS_WIN 0 |
| 71 | # define OS_UNIX 0 |
| 72 | # elif defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__) |
| 73 | # define OS_MAC 0 |
| 74 | # define OS_WIN 1 |
| 75 | # define OS_UNIX 0 |
| 76 | # else |
| 77 | # define OS_MAC 0 |
| 78 | # define OS_WIN 0 |
| 79 | # define OS_UNIX 1 |
| 80 | # endif |
drh | 27a3220 | 2002-03-20 00:00:29 +0000 | [diff] [blame] | 81 | # else |
| 82 | # define OS_WIN 0 |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 83 | # define OS_UNIX 0 |
drh | 27a3220 | 2002-03-20 00:00:29 +0000 | [diff] [blame] | 84 | # endif |
| 85 | # else |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 86 | # define OS_MAC 0 |
drh | 27a3220 | 2002-03-20 00:00:29 +0000 | [diff] [blame] | 87 | # define OS_UNIX 0 |
| 88 | # endif |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 89 | #else |
| 90 | # define OS_MAC 0 |
drh | 27a3220 | 2002-03-20 00:00:29 +0000 | [diff] [blame] | 91 | # define OS_WIN 0 |
drh | 1ab4300 | 2002-01-14 09:28:19 +0000 | [diff] [blame] | 92 | #endif |
| 93 | |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 94 | /* |
| 95 | ** A handle for an open file is stored in an OsFile object. |
| 96 | */ |
| 97 | #if OS_UNIX |
drh | 20e9ab1 | 2002-11-06 00:59:44 +0000 | [diff] [blame] | 98 | # include <sys/types.h> |
| 99 | # include <sys/stat.h> |
| 100 | # include <fcntl.h> |
| 101 | # include <unistd.h> |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 102 | typedef struct OsFile OsFile; |
| 103 | struct OsFile { |
| 104 | struct lockInfo *pLock; /* Information about locks on this inode */ |
| 105 | int fd; /* The file descriptor */ |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 106 | int locked; /* True if this user holds the lock */ |
drh | a76c82e | 2003-07-27 18:59:42 +0000 | [diff] [blame] | 107 | int dirfd; /* File descriptor for the directory */ |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 108 | }; |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 109 | # define SQLITE_TEMPNAME_SIZE 200 |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 110 | # if defined(HAVE_USLEEP) && HAVE_USLEEP |
| 111 | # define SQLITE_MIN_SLEEP_MS 1 |
| 112 | # else |
| 113 | # define SQLITE_MIN_SLEEP_MS 1000 |
| 114 | # endif |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 115 | #endif |
| 116 | |
| 117 | #if OS_WIN |
drh | 254cba2 | 2001-09-20 01:44:42 +0000 | [diff] [blame] | 118 | #include <windows.h> |
| 119 | #include <winbase.h> |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 120 | typedef struct OsFile OsFile; |
| 121 | struct OsFile { |
drh | d1efac5 | 2002-08-14 12:56:54 +0000 | [diff] [blame] | 122 | HANDLE h; /* Handle for accessing the file */ |
| 123 | int locked; /* 0: unlocked, <0: write lock, >0: read lock */ |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 124 | }; |
drh | 7cdbd32 | 2002-11-20 11:07:59 +0000 | [diff] [blame] | 125 | # if defined(_MSC_VER) || defined(__BORLANDC__) |
drh | 829e802 | 2002-11-06 14:08:11 +0000 | [diff] [blame] | 126 | typedef __int64 off_t; |
| 127 | # else |
| 128 | typedef long long off_t; |
| 129 | # endif |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 130 | # define SQLITE_TEMPNAME_SIZE (MAX_PATH+50) |
drh | 254cba2 | 2001-09-20 01:44:42 +0000 | [diff] [blame] | 131 | # define SQLITE_MIN_SLEEP_MS 1 |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 132 | #endif |
| 133 | |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 134 | #if OS_MAC |
| 135 | # include <unistd.h> |
| 136 | # include <Files.h> |
| 137 | typedef struct OsFile OsFile; |
| 138 | struct OsFile { |
| 139 | SInt16 refNum; /* Data fork/file reference number */ |
| 140 | SInt16 refNumRF; /* Resource fork reference number (for locking) */ |
| 141 | int locked; /* 0: unlocked, <0: write lock, >0: read lock */ |
| 142 | int delOnClose; /* True if file is to be deleted on close */ |
| 143 | char *pathToDel; /* Name of file to delete on close */ |
| 144 | }; |
| 145 | # ifdef _LARGE_FILE |
| 146 | typedef SInt64 off_t; |
| 147 | # else |
| 148 | typedef SInt32 off_t; |
| 149 | # endif |
| 150 | # define SQLITE_TEMPNAME_SIZE _MAX_PATH |
| 151 | # define SQLITE_MIN_SLEEP_MS 17 |
| 152 | #endif |
| 153 | |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 154 | int sqliteOsDelete(const char*); |
| 155 | int sqliteOsFileExists(const char*); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 156 | int sqliteOsFileRename(const char*, const char*); |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 157 | int sqliteOsOpenReadWrite(const char*, OsFile*, int*); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 158 | int sqliteOsOpenExclusive(const char*, OsFile*, int); |
drh | 474d3d6 | 2001-09-19 13:58:43 +0000 | [diff] [blame] | 159 | int sqliteOsOpenReadOnly(const char*, OsFile*); |
drh | a76c82e | 2003-07-27 18:59:42 +0000 | [diff] [blame] | 160 | int sqliteOsOpenDirectory(const char*, OsFile*); |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 161 | int sqliteOsTempFileName(char*); |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 162 | int sqliteOsClose(OsFile*); |
| 163 | int sqliteOsRead(OsFile*, void*, int amt); |
| 164 | int sqliteOsWrite(OsFile*, const void*, int amt); |
drh | 28be87c | 2002-11-05 23:03:02 +0000 | [diff] [blame] | 165 | int sqliteOsSeek(OsFile*, off_t offset); |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 166 | int sqliteOsSync(OsFile*); |
drh | 28be87c | 2002-11-05 23:03:02 +0000 | [diff] [blame] | 167 | int sqliteOsTruncate(OsFile*, off_t size); |
| 168 | int sqliteOsFileSize(OsFile*, off_t *pSize); |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 169 | int sqliteOsReadLock(OsFile*); |
| 170 | int sqliteOsWriteLock(OsFile*); |
| 171 | int sqliteOsUnlock(OsFile*); |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 172 | int sqliteOsRandomSeed(char*); |
| 173 | int sqliteOsSleep(int ms); |
drh | 771d8c3 | 2003-08-09 21:32:28 +0000 | [diff] [blame^] | 174 | int sqliteOsCurrentTime(double*); |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 175 | void sqliteOsEnterMutex(void); |
| 176 | void sqliteOsLeaveMutex(void); |
drh | 3e7a609 | 2002-12-07 21:45:14 +0000 | [diff] [blame] | 177 | char *sqliteOsFullPathname(const char*); |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 178 | |
| 179 | |
| 180 | |
| 181 | #endif /* _SQLITE_OS_H_ */ |