xref: /illumos-gate/usr/src/lib/libsqlite/src/os.h (revision a38ddfee9c8c6b6c5a2947ff52fd2338362a4444)
1 
2 #pragma ident	"%Z%%M%	%I%	%E% SMI"
3 
4 /*
5 ** 2001 September 16
6 **
7 ** The author disclaims copyright to this source code.  In place of
8 ** a legal notice, here is a blessing:
9 **
10 **    May you do good and not evil.
11 **    May you find forgiveness for yourself and forgive others.
12 **    May you share freely, never taking more than you give.
13 **
14 ******************************************************************************
15 **
16 ** This header file (together with is companion C source-code file
17 ** "os.c") attempt to abstract the underlying operating system so that
18 ** the SQLite library will work on both POSIX and windows systems.
19 */
20 #ifndef _SQLITE_OS_H_
21 #define _SQLITE_OS_H_
22 
23 /*
24 ** Helpful hint:  To get this to compile on HP/UX, add -D_INCLUDE_POSIX_SOURCE
25 ** to the compiler command line.
26 */
27 
28 /*
29 ** These #defines should enable >2GB file support on Posix if the
30 ** underlying operating system supports it.  If the OS lacks
31 ** large file support, or if the OS is windows, these should be no-ops.
32 **
33 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
34 ** on the compiler command line.  This is necessary if you are compiling
35 ** on a recent machine (ex: RedHat 7.2) but you want your code to work
36 ** on an older machine (ex: RedHat 6.0).  If you compile on RedHat 7.2
37 ** without this option, LFS is enable.  But LFS does not exist in the kernel
38 ** in RedHat 6.0, so the code won't work.  Hence, for maximum binary
39 ** portability you should omit LFS.
40 **
41 ** Similar is true for MacOS.  LFS is only supported on MacOS 9 and later.
42 */
43 #ifndef SQLITE_DISABLE_LFS
44 # define _LARGE_FILE       1
45 # ifndef _FILE_OFFSET_BITS
46 #   define _FILE_OFFSET_BITS 64
47 # endif
48 # define _LARGEFILE_SOURCE 1
49 #endif
50 
51 /*
52 ** Temporary files are named starting with this prefix followed by 16 random
53 ** alphanumeric characters, and no file extension. They are stored in the
54 ** OS's standard temporary file directory, and are deleted prior to exit.
55 ** If sqlite is being embedded in another program, you may wish to change the
56 ** prefix to reflect your program's name, so that if your program exits
57 ** prematurely, old temporary files can be easily identified. This can be done
58 ** using -DTEMP_FILE_PREFIX=myprefix_ on the compiler command line.
59 */
60 #ifndef TEMP_FILE_PREFIX
61 # define TEMP_FILE_PREFIX "sqlite_"
62 #endif
63 
64 /*
65 ** Figure out if we are dealing with Unix, Windows or MacOS.
66 **
67 ** N.B. MacOS means Mac Classic (or Carbon). Treat Darwin (OS X) as Unix.
68 **      The MacOS build is designed to use CodeWarrior (tested with v8)
69 */
70 #ifndef OS_UNIX
71 # ifndef OS_WIN
72 #  ifndef OS_MAC
73 #    if defined(__MACOS__)
74 #      define OS_MAC 1
75 #      define OS_WIN 0
76 #      define OS_UNIX 0
77 #    elif defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
78 #      define OS_MAC 0
79 #      define OS_WIN 1
80 #      define OS_UNIX 0
81 #    else
82 #      define OS_MAC 0
83 #      define OS_WIN 0
84 #      define OS_UNIX 1
85 #    endif
86 #  else
87 #    define OS_WIN 0
88 #    define OS_UNIX 0
89 #  endif
90 # else
91 #  define OS_MAC 0
92 #  define OS_UNIX 0
93 # endif
94 #else
95 # define OS_MAC 0
96 # ifndef OS_WIN
97 #  define OS_WIN 0
98 # endif
99 #endif
100 
101 /*
102 ** A handle for an open file is stored in an OsFile object.
103 */
104 #if OS_UNIX
105 # include <sys/types.h>
106 # include <sys/stat.h>
107 # include <fcntl.h>
108 # include <unistd.h>
109   typedef struct OsFile OsFile;
110   struct OsFile {
111     struct openCnt *pOpen;    /* Info about all open fd's on this inode */
112     struct lockInfo *pLock;   /* Info about locks on this inode */
113     int fd;                   /* The file descriptor */
114     int locked;               /* True if this instance holds the lock */
115     int dirfd;                /* File descriptor for the directory */
116   };
117 # define SQLITE_TEMPNAME_SIZE 200
118 # if defined(HAVE_USLEEP) && HAVE_USLEEP
119 #  define SQLITE_MIN_SLEEP_MS 1
120 # else
121 #  define SQLITE_MIN_SLEEP_MS 1000
122 # endif
123 #endif
124 
125 #if OS_WIN
126 #include <windows.h>
127 #include <winbase.h>
128   typedef struct OsFile OsFile;
129   struct OsFile {
130     HANDLE h;               /* Handle for accessing the file */
131     int locked;             /* 0: unlocked, <0: write lock, >0: read lock */
132   };
133 # if defined(_MSC_VER) || defined(__BORLANDC__)
134     typedef __int64 off_t;
135 # else
136 #  if !defined(_CYGWIN_TYPES_H)
137      typedef long long off_t;
138 #    if defined(__MINGW32__)
139 #      define	_OFF_T_
140 #    endif
141 #  endif
142 # endif
143 # define SQLITE_TEMPNAME_SIZE (MAX_PATH+50)
144 # define SQLITE_MIN_SLEEP_MS 1
145 #endif
146 
147 #if OS_MAC
148 # include <unistd.h>
149 # include <Files.h>
150   typedef struct OsFile OsFile;
151   struct OsFile {
152     SInt16 refNum;           /* Data fork/file reference number */
153     SInt16 refNumRF;         /* Resource fork reference number (for locking) */
154     int locked;              /* 0: unlocked, <0: write lock, >0: read lock */
155     int delOnClose;          /* True if file is to be deleted on close */
156     char *pathToDel;         /* Name of file to delete on close */
157   };
158 # ifdef _LARGE_FILE
159     typedef SInt64 off_t;
160 # else
161     typedef SInt32 off_t;
162 # endif
163 # define SQLITE_TEMPNAME_SIZE _MAX_PATH
164 # define SQLITE_MIN_SLEEP_MS 17
165 #endif
166 
167 int sqliteOsDelete(const char*);
168 int sqliteOsFileExists(const char*);
169 int sqliteOsFileRename(const char*, const char*);
170 int sqliteOsOpenReadWrite(const char*, OsFile*, int*);
171 int sqliteOsOpenExclusive(const char*, OsFile*, int);
172 int sqliteOsOpenReadOnly(const char*, OsFile*);
173 int sqliteOsOpenDirectory(const char*, OsFile*);
174 int sqliteOsTempFileName(char*);
175 int sqliteOsClose(OsFile*);
176 int sqliteOsRead(OsFile*, void*, int amt);
177 int sqliteOsWrite(OsFile*, const void*, int amt);
178 int sqliteOsSeek(OsFile*, off_t offset);
179 int sqliteOsSync(OsFile*);
180 int sqliteOsTruncate(OsFile*, off_t size);
181 int sqliteOsFileSize(OsFile*, off_t *pSize);
182 int sqliteOsReadLock(OsFile*);
183 int sqliteOsWriteLock(OsFile*);
184 int sqliteOsUnlock(OsFile*);
185 int sqliteOsRandomSeed(char*);
186 int sqliteOsSleep(int ms);
187 int sqliteOsCurrentTime(double*);
188 void sqliteOsEnterMutex(void);
189 void sqliteOsLeaveMutex(void);
190 char *sqliteOsFullPathname(const char*);
191 
192 
193 
194 #endif /* _SQLITE_OS_H_ */
195