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