blob: 1fdf92538e500f7578b851e9b5e4f389da2edca2 [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
42# define _FILE_OFFSET_BITS 64
43# define _LARGEFILE_SOURCE 1
44#endif
drh829e8022002-11-06 14:08:11 +000045
46/*
drh820f3812003-01-08 13:02:52 +000047** 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)
drh829e8022002-11-06 14:08:11 +000064*/
drh27a32202002-03-20 00:00:29 +000065#ifndef OS_UNIX
66# ifndef OS_WIN
drh820f3812003-01-08 13:02:52 +000067# 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
drh27a32202002-03-20 00:00:29 +000081# else
82# define OS_WIN 0
drh820f3812003-01-08 13:02:52 +000083# define OS_UNIX 0
drh27a32202002-03-20 00:00:29 +000084# endif
85# else
drh820f3812003-01-08 13:02:52 +000086# define OS_MAC 0
drh27a32202002-03-20 00:00:29 +000087# define OS_UNIX 0
88# endif
drh820f3812003-01-08 13:02:52 +000089#else
90# define OS_MAC 0
drh27a32202002-03-20 00:00:29 +000091# define OS_WIN 0
drh1ab43002002-01-14 09:28:19 +000092#endif
93
drhe3c41372001-09-17 20:25:58 +000094/*
95** A handle for an open file is stored in an OsFile object.
96*/
97#if OS_UNIX
drh20e9ab12002-11-06 00:59:44 +000098# include <sys/types.h>
99# include <sys/stat.h>
100# include <fcntl.h>
101# include <unistd.h>
drhad75e982001-10-09 04:19:46 +0000102 typedef struct OsFile OsFile;
103 struct OsFile {
104 struct lockInfo *pLock; /* Information about locks on this inode */
105 int fd; /* The file descriptor */
drha7fcb052001-12-14 15:09:55 +0000106 int locked; /* True if this user holds the lock */
drha76c82e2003-07-27 18:59:42 +0000107 int dirfd; /* File descriptor for the directory */
drhad75e982001-10-09 04:19:46 +0000108 };
drhe3c41372001-09-17 20:25:58 +0000109# define SQLITE_TEMPNAME_SIZE 200
drh8cfbf082001-09-19 13:22:39 +0000110# if defined(HAVE_USLEEP) && HAVE_USLEEP
111# define SQLITE_MIN_SLEEP_MS 1
112# else
113# define SQLITE_MIN_SLEEP_MS 1000
114# endif
drhe3c41372001-09-17 20:25:58 +0000115#endif
116
117#if OS_WIN
drh254cba22001-09-20 01:44:42 +0000118#include <windows.h>
119#include <winbase.h>
drha7fcb052001-12-14 15:09:55 +0000120 typedef struct OsFile OsFile;
121 struct OsFile {
drhd1efac52002-08-14 12:56:54 +0000122 HANDLE h; /* Handle for accessing the file */
123 int locked; /* 0: unlocked, <0: write lock, >0: read lock */
drha7fcb052001-12-14 15:09:55 +0000124 };
drh7cdbd322002-11-20 11:07:59 +0000125# if defined(_MSC_VER) || defined(__BORLANDC__)
drh829e8022002-11-06 14:08:11 +0000126 typedef __int64 off_t;
127# else
128 typedef long long off_t;
129# endif
drh8cfbf082001-09-19 13:22:39 +0000130# define SQLITE_TEMPNAME_SIZE (MAX_PATH+50)
drh254cba22001-09-20 01:44:42 +0000131# define SQLITE_MIN_SLEEP_MS 1
drhe3c41372001-09-17 20:25:58 +0000132#endif
133
drh820f3812003-01-08 13:02:52 +0000134#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
drh8cfbf082001-09-19 13:22:39 +0000154int sqliteOsDelete(const char*);
155int sqliteOsFileExists(const char*);
drh001bbcb2003-03-19 03:14:00 +0000156int sqliteOsFileRename(const char*, const char*);
drh8cfbf082001-09-19 13:22:39 +0000157int sqliteOsOpenReadWrite(const char*, OsFile*, int*);
drhfa86c412002-02-02 15:01:15 +0000158int sqliteOsOpenExclusive(const char*, OsFile*, int);
drh474d3d62001-09-19 13:58:43 +0000159int sqliteOsOpenReadOnly(const char*, OsFile*);
drha76c82e2003-07-27 18:59:42 +0000160int sqliteOsOpenDirectory(const char*, OsFile*);
drhe3c41372001-09-17 20:25:58 +0000161int sqliteOsTempFileName(char*);
drha7fcb052001-12-14 15:09:55 +0000162int sqliteOsClose(OsFile*);
163int sqliteOsRead(OsFile*, void*, int amt);
164int sqliteOsWrite(OsFile*, const void*, int amt);
drh28be87c2002-11-05 23:03:02 +0000165int sqliteOsSeek(OsFile*, off_t offset);
drha7fcb052001-12-14 15:09:55 +0000166int sqliteOsSync(OsFile*);
drh28be87c2002-11-05 23:03:02 +0000167int sqliteOsTruncate(OsFile*, off_t size);
168int sqliteOsFileSize(OsFile*, off_t *pSize);
drha7fcb052001-12-14 15:09:55 +0000169int sqliteOsReadLock(OsFile*);
170int sqliteOsWriteLock(OsFile*);
171int sqliteOsUnlock(OsFile*);
drh8cfbf082001-09-19 13:22:39 +0000172int sqliteOsRandomSeed(char*);
173int sqliteOsSleep(int ms);
drh771d8c32003-08-09 21:32:28 +0000174int sqliteOsCurrentTime(double*);
drhb8ca3072001-12-05 00:21:20 +0000175void sqliteOsEnterMutex(void);
176void sqliteOsLeaveMutex(void);
drh3e7a6092002-12-07 21:45:14 +0000177char *sqliteOsFullPathname(const char*);
drhe3c41372001-09-17 20:25:58 +0000178
179
180
181#endif /* _SQLITE_OS_H_ */