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 20 /* 21 ** Helpful hint: To get this to compile on HP/UX, add -D_INCLUDE_POSIX_SOURCE 22 ** to the compiler command line. 23 */ 24 25 /* 26 ** 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. 29 ** 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. 37 ** 38 ** Similar is true for MacOS. LFS is only supported on MacOS 9 and later. 39 */ 40 #ifndef SQLITE_DISABLE_LFS 41 # define _LARGE_FILE 1 42 # ifndef _FILE_OFFSET_BITS 43 # define _FILE_OFFSET_BITS 64 44 # endif 45 # define _LARGEFILE_SOURCE 1 46 #endif 47 48 /* 49 ** 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) 66 */ 67 #ifndef OS_UNIX 68 # ifndef OS_WIN 69 # 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 83 # else 84 # define OS_WIN 0 85 # define OS_UNIX 0 86 # endif 87 # else 88 # define OS_MAC 0 89 # define OS_UNIX 0 90 # endif 91 #else 92 # define OS_MAC 0 93 # ifndef OS_WIN 94 # define OS_WIN 0 95 # endif 96 #endif 97 98 /* 99 ** A handle for an open file is stored in an OsFile object. 100 */ 101 #if OS_UNIX 102 # include <sys/types.h> 103 # include <sys/stat.h> 104 # include <fcntl.h> 105 # include <unistd.h> 106 typedef struct OsFile OsFile; 107 struct OsFile { 108 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 */ 113 }; 114 # define SQLITE_TEMPNAME_SIZE 200 115 # if defined(HAVE_USLEEP) && HAVE_USLEEP 116 # define SQLITE_MIN_SLEEP_MS 1 117 # else 118 # define SQLITE_MIN_SLEEP_MS 1000 119 # endif 120 #endif 121 122 #if OS_WIN 123 #include <windows.h> 124 #include <winbase.h> 125 typedef struct OsFile OsFile; 126 struct OsFile { 127 HANDLE h; /* Handle for accessing the file */ 128 int locked; /* 0: unlocked, <0: write lock, >0: read lock */ 129 }; 130 # if defined(_MSC_VER) || defined(__BORLANDC__) 131 typedef __int64 off_t; 132 # else 133 # if !defined(_CYGWIN_TYPES_H) 134 typedef long long off_t; 135 # if defined(__MINGW32__) 136 # define _OFF_T_ 137 # endif 138 # endif 139 # endif 140 # define SQLITE_TEMPNAME_SIZE (MAX_PATH+50) 141 # define SQLITE_MIN_SLEEP_MS 1 142 #endif 143 144 #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 164 int sqliteOsDelete(const char*); 165 int sqliteOsFileExists(const char*); 166 int sqliteOsFileRename(const char*, const char*); 167 int sqliteOsOpenReadWrite(const char*, OsFile*, int*); 168 int sqliteOsOpenExclusive(const char*, OsFile*, int); 169 int sqliteOsOpenReadOnly(const char*, OsFile*); 170 int sqliteOsOpenDirectory(const char*, OsFile*); 171 int sqliteOsTempFileName(char*); 172 int sqliteOsClose(OsFile*); 173 int sqliteOsRead(OsFile*, void*, int amt); 174 int sqliteOsWrite(OsFile*, const void*, int amt); 175 int sqliteOsSeek(OsFile*, off_t offset); 176 int sqliteOsSync(OsFile*); 177 int sqliteOsTruncate(OsFile*, off_t size); 178 int sqliteOsFileSize(OsFile*, off_t *pSize); 179 int sqliteOsReadLock(OsFile*); 180 int sqliteOsWriteLock(OsFile*); 181 int sqliteOsUnlock(OsFile*); 182 int sqliteOsRandomSeed(char*); 183 int sqliteOsSleep(int ms); 184 int sqliteOsCurrentTime(double*); 185 void sqliteOsEnterMutex(void); 186 void sqliteOsLeaveMutex(void); 187 char *sqliteOsFullPathname(const char*); 188 189 190 191 #endif /* _SQLITE_OS_H_ */ 192