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 | /* |
| 21 | ** These #defines should enable >2GB file support on Posix if the |
| 22 | ** underlying operating system supports it. If the OS lacks |
| 23 | ** large file support, or if the OS is windows, these should be no-ops. |
| 24 | */ |
| 25 | #define _LARGE_FILE 1 |
| 26 | #define _FILE_OFFSET_BITS 64 |
| 27 | #define _LARGEFILE_SOURCE 1 |
| 28 | |
| 29 | /* |
| 30 | ** Figure out if we are dealing with Unix or Windows. |
| 31 | */ |
drh | 27a3220 | 2002-03-20 00:00:29 +0000 | [diff] [blame] | 32 | #ifndef OS_UNIX |
| 33 | # ifndef OS_WIN |
| 34 | # if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__) |
| 35 | # define OS_WIN 1 |
| 36 | # define OS_UNIX 0 |
| 37 | # else |
| 38 | # define OS_WIN 0 |
| 39 | # define OS_UNIX 1 |
| 40 | # endif |
| 41 | # else |
| 42 | # define OS_UNIX 0 |
| 43 | # endif |
| 44 | #endif |
| 45 | #ifndef OS_WIN |
| 46 | # define OS_WIN 0 |
drh | 1ab4300 | 2002-01-14 09:28:19 +0000 | [diff] [blame] | 47 | #endif |
| 48 | |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 49 | /* |
| 50 | ** A handle for an open file is stored in an OsFile object. |
| 51 | */ |
| 52 | #if OS_UNIX |
drh | 20e9ab1 | 2002-11-06 00:59:44 +0000 | [diff] [blame] | 53 | # include <sys/types.h> |
| 54 | # include <sys/stat.h> |
| 55 | # include <fcntl.h> |
| 56 | # include <unistd.h> |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 57 | typedef struct OsFile OsFile; |
| 58 | struct OsFile { |
| 59 | struct lockInfo *pLock; /* Information about locks on this inode */ |
| 60 | int fd; /* The file descriptor */ |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 61 | int locked; /* True if this user holds the lock */ |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 62 | }; |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 63 | # define SQLITE_TEMPNAME_SIZE 200 |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 64 | # if defined(HAVE_USLEEP) && HAVE_USLEEP |
| 65 | # define SQLITE_MIN_SLEEP_MS 1 |
| 66 | # else |
| 67 | # define SQLITE_MIN_SLEEP_MS 1000 |
| 68 | # endif |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 69 | #endif |
| 70 | |
| 71 | #if OS_WIN |
drh | 254cba2 | 2001-09-20 01:44:42 +0000 | [diff] [blame] | 72 | #include <windows.h> |
| 73 | #include <winbase.h> |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 74 | typedef struct OsFile OsFile; |
| 75 | struct OsFile { |
drh | d1efac5 | 2002-08-14 12:56:54 +0000 | [diff] [blame] | 76 | HANDLE h; /* Handle for accessing the file */ |
| 77 | int locked; /* 0: unlocked, <0: write lock, >0: read lock */ |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 78 | }; |
drh | 829e802 | 2002-11-06 14:08:11 +0000 | [diff] [blame^] | 79 | # ifdef _MSC_VER |
| 80 | typedef __int64 off_t; |
| 81 | # else |
| 82 | typedef long long off_t; |
| 83 | # endif |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 84 | # define SQLITE_TEMPNAME_SIZE (MAX_PATH+50) |
drh | 254cba2 | 2001-09-20 01:44:42 +0000 | [diff] [blame] | 85 | # define SQLITE_MIN_SLEEP_MS 1 |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 86 | #endif |
| 87 | |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 88 | int sqliteOsDelete(const char*); |
| 89 | int sqliteOsFileExists(const char*); |
| 90 | int sqliteOsOpenReadWrite(const char*, OsFile*, int*); |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 91 | int sqliteOsOpenExclusive(const char*, OsFile*, int); |
drh | 474d3d6 | 2001-09-19 13:58:43 +0000 | [diff] [blame] | 92 | int sqliteOsOpenReadOnly(const char*, OsFile*); |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 93 | int sqliteOsTempFileName(char*); |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 94 | int sqliteOsClose(OsFile*); |
| 95 | int sqliteOsRead(OsFile*, void*, int amt); |
| 96 | int sqliteOsWrite(OsFile*, const void*, int amt); |
drh | 28be87c | 2002-11-05 23:03:02 +0000 | [diff] [blame] | 97 | int sqliteOsSeek(OsFile*, off_t offset); |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 98 | int sqliteOsSync(OsFile*); |
drh | 28be87c | 2002-11-05 23:03:02 +0000 | [diff] [blame] | 99 | int sqliteOsTruncate(OsFile*, off_t size); |
| 100 | int sqliteOsFileSize(OsFile*, off_t *pSize); |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 101 | int sqliteOsReadLock(OsFile*); |
| 102 | int sqliteOsWriteLock(OsFile*); |
| 103 | int sqliteOsUnlock(OsFile*); |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 104 | int sqliteOsRandomSeed(char*); |
| 105 | int sqliteOsSleep(int ms); |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 106 | void sqliteOsEnterMutex(void); |
| 107 | void sqliteOsLeaveMutex(void); |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 108 | |
| 109 | |
| 110 | |
| 111 | #endif /* _SQLITE_OS_H_ */ |