blob: a88756f8c1ddcb1a4bd0064c7a083117ae25a8ab [file] [log] [blame]
drhe3c41372001-09-17 20:25:58 +00001/*
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
drh829e8022002-11-06 14:08:11 +000020/*
drh5cf590c2003-04-24 01:45:04 +000021** Helpful hint: To get this to compile on HP/UX, add -D_INCLUDE_POSIX_SOURCE
22** to the compiler command line.
23*/
24
25/*
drh829e8022002-11-06 14:08:11 +000026** 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.
drh8766c342002-11-09 00:33:15 +000029**
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.
drh820f3812003-01-08 13:02:52 +000037**
38** Similar is true for MacOS. LFS is only supported on MacOS 9 and later.
drh829e8022002-11-06 14:08:11 +000039*/
drh8766c342002-11-09 00:33:15 +000040#ifndef SQLITE_DISABLE_LFS
41# define _LARGE_FILE 1
drhab9426e2004-02-11 16:38:06 +000042# ifndef _FILE_OFFSET_BITS
43# define _FILE_OFFSET_BITS 64
44# endif
drh8766c342002-11-09 00:33:15 +000045# define _LARGEFILE_SOURCE 1
46#endif
drh829e8022002-11-06 14:08:11 +000047
48/*
drh820f3812003-01-08 13:02:52 +000049** 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)
drh829e8022002-11-06 14:08:11 +000066*/
drh27a32202002-03-20 00:00:29 +000067#ifndef OS_UNIX
68# ifndef OS_WIN
drh820f3812003-01-08 13:02:52 +000069# 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
drh27a32202002-03-20 00:00:29 +000083# else
84# define OS_WIN 0
drh820f3812003-01-08 13:02:52 +000085# define OS_UNIX 0
drh27a32202002-03-20 00:00:29 +000086# endif
87# else
drh820f3812003-01-08 13:02:52 +000088# define OS_MAC 0
drh27a32202002-03-20 00:00:29 +000089# define OS_UNIX 0
90# endif
drh820f3812003-01-08 13:02:52 +000091#else
92# define OS_MAC 0
drhe5e37602003-08-16 13:10:51 +000093# ifndef OS_WIN
94# define OS_WIN 0
95# endif
drh1ab43002002-01-14 09:28:19 +000096#endif
97
drhe3c41372001-09-17 20:25:58 +000098/*
99** A handle for an open file is stored in an OsFile object.
100*/
101#if OS_UNIX
drh20e9ab12002-11-06 00:59:44 +0000102# include <sys/types.h>
103# include <sys/stat.h>
104# include <fcntl.h>
105# include <unistd.h>
drhad75e982001-10-09 04:19:46 +0000106 typedef struct OsFile OsFile;
107 struct OsFile {
drh31e95bc2004-01-12 00:39:05 +0000108 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 */
drhad75e982001-10-09 04:19:46 +0000113 };
drhe3c41372001-09-17 20:25:58 +0000114# define SQLITE_TEMPNAME_SIZE 200
drh8cfbf082001-09-19 13:22:39 +0000115# if defined(HAVE_USLEEP) && HAVE_USLEEP
116# define SQLITE_MIN_SLEEP_MS 1
117# else
118# define SQLITE_MIN_SLEEP_MS 1000
119# endif
drhe3c41372001-09-17 20:25:58 +0000120#endif
121
122#if OS_WIN
drh254cba22001-09-20 01:44:42 +0000123#include <windows.h>
124#include <winbase.h>
drha7fcb052001-12-14 15:09:55 +0000125 typedef struct OsFile OsFile;
126 struct OsFile {
drhd1efac52002-08-14 12:56:54 +0000127 HANDLE h; /* Handle for accessing the file */
128 int locked; /* 0: unlocked, <0: write lock, >0: read lock */
drha7fcb052001-12-14 15:09:55 +0000129 };
drh7cdbd322002-11-20 11:07:59 +0000130# if defined(_MSC_VER) || defined(__BORLANDC__)
drh829e8022002-11-06 14:08:11 +0000131 typedef __int64 off_t;
132# else
drhe5e37602003-08-16 13:10:51 +0000133# if !defined(_CYGWIN_TYPES_H)
134 typedef long long off_t;
dougcurrieae534182003-12-24 01:41:19 +0000135# if defined(__MINGW32__)
136# define _OFF_T_
137# endif
drhe5e37602003-08-16 13:10:51 +0000138# endif
drh829e8022002-11-06 14:08:11 +0000139# endif
drh8cfbf082001-09-19 13:22:39 +0000140# define SQLITE_TEMPNAME_SIZE (MAX_PATH+50)
drh254cba22001-09-20 01:44:42 +0000141# define SQLITE_MIN_SLEEP_MS 1
drhe3c41372001-09-17 20:25:58 +0000142#endif
143
drh820f3812003-01-08 13:02:52 +0000144#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
danielk19776622cce2004-05-20 11:00:52 +0000164/*
165** Macros to determine whether the machine is big or little endian,
166** evaluated at runtime.
167*/
168static const int sqlite3_one = 1;
169#define SQLITE3_BIGENDIAN (*(char *)(&sqlite3_one)==0)
170#define SQLITE3_LITTLEENDIAN (*(char *)(&sqlite3_one)==1)
171
danielk19774adee202004-05-08 08:23:19 +0000172int sqlite3OsDelete(const char*);
173int sqlite3OsFileExists(const char*);
drh001bbcb2003-03-19 03:14:00 +0000174int sqliteOsFileRename(const char*, const char*);
danielk19774adee202004-05-08 08:23:19 +0000175int sqlite3OsOpenReadWrite(const char*, OsFile*, int*);
176int sqlite3OsOpenExclusive(const char*, OsFile*, int);
177int sqlite3OsOpenReadOnly(const char*, OsFile*);
178int sqlite3OsOpenDirectory(const char*, OsFile*);
179int sqlite3OsTempFileName(char*);
180int sqlite3OsClose(OsFile*);
181int sqlite3OsRead(OsFile*, void*, int amt);
182int sqlite3OsWrite(OsFile*, const void*, int amt);
183int sqlite3OsSeek(OsFile*, off_t offset);
184int sqlite3OsSync(OsFile*);
185int sqlite3OsTruncate(OsFile*, off_t size);
186int sqlite3OsFileSize(OsFile*, off_t *pSize);
187int sqlite3OsReadLock(OsFile*);
188int sqlite3OsWriteLock(OsFile*);
189int sqlite3OsUnlock(OsFile*);
190int sqlite3OsRandomSeed(char*);
191int sqlite3OsSleep(int ms);
192int sqlite3OsCurrentTime(double*);
193void sqlite3OsEnterMutex(void);
194void sqlite3OsLeaveMutex(void);
195char *sqlite3OsFullPathname(const char*);
drhe3c41372001-09-17 20:25:58 +0000196
197
198
199#endif /* _SQLITE_OS_H_ */
danielk19774adee202004-05-08 08:23:19 +0000200
201
202