blob: e46abb46ace91a32252b074016d4a51f4109cd8b [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
drhe5e37602003-08-16 13:10:51 +000091# ifndef OS_WIN
92# define OS_WIN 0
93# endif
drh1ab43002002-01-14 09:28:19 +000094#endif
95
drhe3c41372001-09-17 20:25:58 +000096/*
97** A handle for an open file is stored in an OsFile object.
98*/
99#if OS_UNIX
drh20e9ab12002-11-06 00:59:44 +0000100# include <sys/types.h>
101# include <sys/stat.h>
102# include <fcntl.h>
103# include <unistd.h>
drhad75e982001-10-09 04:19:46 +0000104 typedef struct OsFile OsFile;
105 struct OsFile {
106 struct lockInfo *pLock; /* Information about locks on this inode */
107 int fd; /* The file descriptor */
drha7fcb052001-12-14 15:09:55 +0000108 int locked; /* True if this user holds the lock */
drha76c82e2003-07-27 18:59:42 +0000109 int dirfd; /* File descriptor for the directory */
drhad75e982001-10-09 04:19:46 +0000110 };
drhe3c41372001-09-17 20:25:58 +0000111# define SQLITE_TEMPNAME_SIZE 200
drh8cfbf082001-09-19 13:22:39 +0000112# if defined(HAVE_USLEEP) && HAVE_USLEEP
113# define SQLITE_MIN_SLEEP_MS 1
114# else
115# define SQLITE_MIN_SLEEP_MS 1000
116# endif
drhe3c41372001-09-17 20:25:58 +0000117#endif
118
119#if OS_WIN
drhe5e37602003-08-16 13:10:51 +0000120# if defined(__CYGWIN__)
121# define __CYGWIN_USE_BIG_TYPES__
122# endif
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;
135# endif
drh829e8022002-11-06 14:08:11 +0000136# endif
drh8cfbf082001-09-19 13:22:39 +0000137# define SQLITE_TEMPNAME_SIZE (MAX_PATH+50)
drh254cba22001-09-20 01:44:42 +0000138# define SQLITE_MIN_SLEEP_MS 1
drhe3c41372001-09-17 20:25:58 +0000139#endif
140
drh820f3812003-01-08 13:02:52 +0000141#if OS_MAC
142# include <unistd.h>
143# include <Files.h>
144 typedef struct OsFile OsFile;
145 struct OsFile {
146 SInt16 refNum; /* Data fork/file reference number */
147 SInt16 refNumRF; /* Resource fork reference number (for locking) */
148 int locked; /* 0: unlocked, <0: write lock, >0: read lock */
149 int delOnClose; /* True if file is to be deleted on close */
150 char *pathToDel; /* Name of file to delete on close */
151 };
152# ifdef _LARGE_FILE
153 typedef SInt64 off_t;
154# else
155 typedef SInt32 off_t;
156# endif
157# define SQLITE_TEMPNAME_SIZE _MAX_PATH
158# define SQLITE_MIN_SLEEP_MS 17
159#endif
160
drh8cfbf082001-09-19 13:22:39 +0000161int sqliteOsDelete(const char*);
162int sqliteOsFileExists(const char*);
drh001bbcb2003-03-19 03:14:00 +0000163int sqliteOsFileRename(const char*, const char*);
drh8cfbf082001-09-19 13:22:39 +0000164int sqliteOsOpenReadWrite(const char*, OsFile*, int*);
drhfa86c412002-02-02 15:01:15 +0000165int sqliteOsOpenExclusive(const char*, OsFile*, int);
drh474d3d62001-09-19 13:58:43 +0000166int sqliteOsOpenReadOnly(const char*, OsFile*);
drha76c82e2003-07-27 18:59:42 +0000167int sqliteOsOpenDirectory(const char*, OsFile*);
drhe3c41372001-09-17 20:25:58 +0000168int sqliteOsTempFileName(char*);
drha7fcb052001-12-14 15:09:55 +0000169int sqliteOsClose(OsFile*);
170int sqliteOsRead(OsFile*, void*, int amt);
171int sqliteOsWrite(OsFile*, const void*, int amt);
drh28be87c2002-11-05 23:03:02 +0000172int sqliteOsSeek(OsFile*, off_t offset);
drha7fcb052001-12-14 15:09:55 +0000173int sqliteOsSync(OsFile*);
drh28be87c2002-11-05 23:03:02 +0000174int sqliteOsTruncate(OsFile*, off_t size);
175int sqliteOsFileSize(OsFile*, off_t *pSize);
drha7fcb052001-12-14 15:09:55 +0000176int sqliteOsReadLock(OsFile*);
177int sqliteOsWriteLock(OsFile*);
178int sqliteOsUnlock(OsFile*);
drh8cfbf082001-09-19 13:22:39 +0000179int sqliteOsRandomSeed(char*);
180int sqliteOsSleep(int ms);
drh771d8c32003-08-09 21:32:28 +0000181int sqliteOsCurrentTime(double*);
drhb8ca3072001-12-05 00:21:20 +0000182void sqliteOsEnterMutex(void);
183void sqliteOsLeaveMutex(void);
drh3e7a6092002-12-07 21:45:14 +0000184char *sqliteOsFullPathname(const char*);
drhe3c41372001-09-17 20:25:58 +0000185
186
187
188#endif /* _SQLITE_OS_H_ */