17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate ** 2001 September 16 37c478bd9Sstevel@tonic-gate ** 47c478bd9Sstevel@tonic-gate ** The author disclaims copyright to this source code. In place of 57c478bd9Sstevel@tonic-gate ** a legal notice, here is a blessing: 67c478bd9Sstevel@tonic-gate ** 77c478bd9Sstevel@tonic-gate ** May you do good and not evil. 87c478bd9Sstevel@tonic-gate ** May you find forgiveness for yourself and forgive others. 97c478bd9Sstevel@tonic-gate ** May you share freely, never taking more than you give. 107c478bd9Sstevel@tonic-gate ** 117c478bd9Sstevel@tonic-gate ****************************************************************************** 127c478bd9Sstevel@tonic-gate ** 137c478bd9Sstevel@tonic-gate ** This header file (together with is companion C source-code file 147c478bd9Sstevel@tonic-gate ** "os.c") attempt to abstract the underlying operating system so that 157c478bd9Sstevel@tonic-gate ** the SQLite library will work on both POSIX and windows systems. 167c478bd9Sstevel@tonic-gate */ 177c478bd9Sstevel@tonic-gate #ifndef _SQLITE_OS_H_ 187c478bd9Sstevel@tonic-gate #define _SQLITE_OS_H_ 197c478bd9Sstevel@tonic-gate 207c478bd9Sstevel@tonic-gate /* 217c478bd9Sstevel@tonic-gate ** Helpful hint: To get this to compile on HP/UX, add -D_INCLUDE_POSIX_SOURCE 227c478bd9Sstevel@tonic-gate ** to the compiler command line. 237c478bd9Sstevel@tonic-gate */ 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate /* 267c478bd9Sstevel@tonic-gate ** These #defines should enable >2GB file support on Posix if the 277c478bd9Sstevel@tonic-gate ** underlying operating system supports it. If the OS lacks 287c478bd9Sstevel@tonic-gate ** large file support, or if the OS is windows, these should be no-ops. 297c478bd9Sstevel@tonic-gate ** 307c478bd9Sstevel@tonic-gate ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch 317c478bd9Sstevel@tonic-gate ** on the compiler command line. This is necessary if you are compiling 327c478bd9Sstevel@tonic-gate ** on a recent machine (ex: RedHat 7.2) but you want your code to work 337c478bd9Sstevel@tonic-gate ** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2 347c478bd9Sstevel@tonic-gate ** without this option, LFS is enable. But LFS does not exist in the kernel 357c478bd9Sstevel@tonic-gate ** in RedHat 6.0, so the code won't work. Hence, for maximum binary 367c478bd9Sstevel@tonic-gate ** portability you should omit LFS. 377c478bd9Sstevel@tonic-gate ** 387c478bd9Sstevel@tonic-gate ** Similar is true for MacOS. LFS is only supported on MacOS 9 and later. 397c478bd9Sstevel@tonic-gate */ 407c478bd9Sstevel@tonic-gate #ifndef SQLITE_DISABLE_LFS 417c478bd9Sstevel@tonic-gate # define _LARGE_FILE 1 427c478bd9Sstevel@tonic-gate # ifndef _FILE_OFFSET_BITS 437c478bd9Sstevel@tonic-gate # define _FILE_OFFSET_BITS 64 447c478bd9Sstevel@tonic-gate # endif 457c478bd9Sstevel@tonic-gate # define _LARGEFILE_SOURCE 1 467c478bd9Sstevel@tonic-gate #endif 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate /* 497c478bd9Sstevel@tonic-gate ** Temporary files are named starting with this prefix followed by 16 random 507c478bd9Sstevel@tonic-gate ** alphanumeric characters, and no file extension. They are stored in the 517c478bd9Sstevel@tonic-gate ** OS's standard temporary file directory, and are deleted prior to exit. 527c478bd9Sstevel@tonic-gate ** If sqlite is being embedded in another program, you may wish to change the 537c478bd9Sstevel@tonic-gate ** prefix to reflect your program's name, so that if your program exits 547c478bd9Sstevel@tonic-gate ** prematurely, old temporary files can be easily identified. This can be done 557c478bd9Sstevel@tonic-gate ** using -DTEMP_FILE_PREFIX=myprefix_ on the compiler command line. 567c478bd9Sstevel@tonic-gate */ 577c478bd9Sstevel@tonic-gate #ifndef TEMP_FILE_PREFIX 587c478bd9Sstevel@tonic-gate # define TEMP_FILE_PREFIX "sqlite_" 597c478bd9Sstevel@tonic-gate #endif 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate /* 627c478bd9Sstevel@tonic-gate ** Figure out if we are dealing with Unix, Windows or MacOS. 637c478bd9Sstevel@tonic-gate ** 647c478bd9Sstevel@tonic-gate ** N.B. MacOS means Mac Classic (or Carbon). Treat Darwin (OS X) as Unix. 657c478bd9Sstevel@tonic-gate ** The MacOS build is designed to use CodeWarrior (tested with v8) 667c478bd9Sstevel@tonic-gate */ 677c478bd9Sstevel@tonic-gate #ifndef OS_UNIX 687c478bd9Sstevel@tonic-gate # ifndef OS_WIN 697c478bd9Sstevel@tonic-gate # ifndef OS_MAC 707c478bd9Sstevel@tonic-gate # if defined(__MACOS__) 717c478bd9Sstevel@tonic-gate # define OS_MAC 1 727c478bd9Sstevel@tonic-gate # define OS_WIN 0 737c478bd9Sstevel@tonic-gate # define OS_UNIX 0 747c478bd9Sstevel@tonic-gate # elif defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__) 757c478bd9Sstevel@tonic-gate # define OS_MAC 0 767c478bd9Sstevel@tonic-gate # define OS_WIN 1 777c478bd9Sstevel@tonic-gate # define OS_UNIX 0 787c478bd9Sstevel@tonic-gate # else 797c478bd9Sstevel@tonic-gate # define OS_MAC 0 807c478bd9Sstevel@tonic-gate # define OS_WIN 0 817c478bd9Sstevel@tonic-gate # define OS_UNIX 1 827c478bd9Sstevel@tonic-gate # endif 837c478bd9Sstevel@tonic-gate # else 847c478bd9Sstevel@tonic-gate # define OS_WIN 0 857c478bd9Sstevel@tonic-gate # define OS_UNIX 0 867c478bd9Sstevel@tonic-gate # endif 877c478bd9Sstevel@tonic-gate # else 887c478bd9Sstevel@tonic-gate # define OS_MAC 0 897c478bd9Sstevel@tonic-gate # define OS_UNIX 0 907c478bd9Sstevel@tonic-gate # endif 917c478bd9Sstevel@tonic-gate #else 927c478bd9Sstevel@tonic-gate # define OS_MAC 0 937c478bd9Sstevel@tonic-gate # ifndef OS_WIN 947c478bd9Sstevel@tonic-gate # define OS_WIN 0 957c478bd9Sstevel@tonic-gate # endif 967c478bd9Sstevel@tonic-gate #endif 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate /* 997c478bd9Sstevel@tonic-gate ** A handle for an open file is stored in an OsFile object. 1007c478bd9Sstevel@tonic-gate */ 1017c478bd9Sstevel@tonic-gate #if OS_UNIX 1027c478bd9Sstevel@tonic-gate # include <sys/types.h> 1037c478bd9Sstevel@tonic-gate # include <sys/stat.h> 1047c478bd9Sstevel@tonic-gate # include <fcntl.h> 1057c478bd9Sstevel@tonic-gate # include <unistd.h> 1067c478bd9Sstevel@tonic-gate typedef struct OsFile OsFile; 1077c478bd9Sstevel@tonic-gate struct OsFile { 1087c478bd9Sstevel@tonic-gate struct openCnt *pOpen; /* Info about all open fd's on this inode */ 1097c478bd9Sstevel@tonic-gate struct lockInfo *pLock; /* Info about locks on this inode */ 1107c478bd9Sstevel@tonic-gate int fd; /* The file descriptor */ 1117c478bd9Sstevel@tonic-gate int locked; /* True if this instance holds the lock */ 1127c478bd9Sstevel@tonic-gate int dirfd; /* File descriptor for the directory */ 1137c478bd9Sstevel@tonic-gate }; 1147c478bd9Sstevel@tonic-gate # define SQLITE_TEMPNAME_SIZE 200 1157c478bd9Sstevel@tonic-gate # if defined(HAVE_USLEEP) && HAVE_USLEEP 1167c478bd9Sstevel@tonic-gate # define SQLITE_MIN_SLEEP_MS 1 1177c478bd9Sstevel@tonic-gate # else 1187c478bd9Sstevel@tonic-gate # define SQLITE_MIN_SLEEP_MS 1000 1197c478bd9Sstevel@tonic-gate # endif 1207c478bd9Sstevel@tonic-gate #endif 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate #if OS_WIN 1237c478bd9Sstevel@tonic-gate #include <windows.h> 1247c478bd9Sstevel@tonic-gate #include <winbase.h> 1257c478bd9Sstevel@tonic-gate typedef struct OsFile OsFile; 1267c478bd9Sstevel@tonic-gate struct OsFile { 1277c478bd9Sstevel@tonic-gate HANDLE h; /* Handle for accessing the file */ 1287c478bd9Sstevel@tonic-gate int locked; /* 0: unlocked, <0: write lock, >0: read lock */ 1297c478bd9Sstevel@tonic-gate }; 1307c478bd9Sstevel@tonic-gate # if defined(_MSC_VER) || defined(__BORLANDC__) 1317c478bd9Sstevel@tonic-gate typedef __int64 off_t; 1327c478bd9Sstevel@tonic-gate # else 1337c478bd9Sstevel@tonic-gate # if !defined(_CYGWIN_TYPES_H) 1347c478bd9Sstevel@tonic-gate typedef long long off_t; 1357c478bd9Sstevel@tonic-gate # if defined(__MINGW32__) 1367c478bd9Sstevel@tonic-gate # define _OFF_T_ 1377c478bd9Sstevel@tonic-gate # endif 1387c478bd9Sstevel@tonic-gate # endif 1397c478bd9Sstevel@tonic-gate # endif 1407c478bd9Sstevel@tonic-gate # define SQLITE_TEMPNAME_SIZE (MAX_PATH+50) 1417c478bd9Sstevel@tonic-gate # define SQLITE_MIN_SLEEP_MS 1 1427c478bd9Sstevel@tonic-gate #endif 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate #if OS_MAC 1457c478bd9Sstevel@tonic-gate # include <unistd.h> 1467c478bd9Sstevel@tonic-gate # include <Files.h> 1477c478bd9Sstevel@tonic-gate typedef struct OsFile OsFile; 1487c478bd9Sstevel@tonic-gate struct OsFile { 1497c478bd9Sstevel@tonic-gate SInt16 refNum; /* Data fork/file reference number */ 1507c478bd9Sstevel@tonic-gate SInt16 refNumRF; /* Resource fork reference number (for locking) */ 1517c478bd9Sstevel@tonic-gate int locked; /* 0: unlocked, <0: write lock, >0: read lock */ 1527c478bd9Sstevel@tonic-gate int delOnClose; /* True if file is to be deleted on close */ 1537c478bd9Sstevel@tonic-gate char *pathToDel; /* Name of file to delete on close */ 1547c478bd9Sstevel@tonic-gate }; 1557c478bd9Sstevel@tonic-gate # ifdef _LARGE_FILE 1567c478bd9Sstevel@tonic-gate typedef SInt64 off_t; 1577c478bd9Sstevel@tonic-gate # else 1587c478bd9Sstevel@tonic-gate typedef SInt32 off_t; 1597c478bd9Sstevel@tonic-gate # endif 1607c478bd9Sstevel@tonic-gate # define SQLITE_TEMPNAME_SIZE _MAX_PATH 1617c478bd9Sstevel@tonic-gate # define SQLITE_MIN_SLEEP_MS 17 1627c478bd9Sstevel@tonic-gate #endif 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate int sqliteOsDelete(const char*); 1657c478bd9Sstevel@tonic-gate int sqliteOsFileExists(const char*); 1667c478bd9Sstevel@tonic-gate int sqliteOsFileRename(const char*, const char*); 1677c478bd9Sstevel@tonic-gate int sqliteOsOpenReadWrite(const char*, OsFile*, int*); 1687c478bd9Sstevel@tonic-gate int sqliteOsOpenExclusive(const char*, OsFile*, int); 1697c478bd9Sstevel@tonic-gate int sqliteOsOpenReadOnly(const char*, OsFile*); 1707c478bd9Sstevel@tonic-gate int sqliteOsOpenDirectory(const char*, OsFile*); 1717c478bd9Sstevel@tonic-gate int sqliteOsTempFileName(char*); 1727c478bd9Sstevel@tonic-gate int sqliteOsClose(OsFile*); 1737c478bd9Sstevel@tonic-gate int sqliteOsRead(OsFile*, void*, int amt); 1747c478bd9Sstevel@tonic-gate int sqliteOsWrite(OsFile*, const void*, int amt); 1757c478bd9Sstevel@tonic-gate int sqliteOsSeek(OsFile*, off_t offset); 1767c478bd9Sstevel@tonic-gate int sqliteOsSync(OsFile*); 1777c478bd9Sstevel@tonic-gate int sqliteOsTruncate(OsFile*, off_t size); 1787c478bd9Sstevel@tonic-gate int sqliteOsFileSize(OsFile*, off_t *pSize); 1797c478bd9Sstevel@tonic-gate int sqliteOsReadLock(OsFile*); 1807c478bd9Sstevel@tonic-gate int sqliteOsWriteLock(OsFile*); 1817c478bd9Sstevel@tonic-gate int sqliteOsUnlock(OsFile*); 1827c478bd9Sstevel@tonic-gate int sqliteOsRandomSeed(char*); 1837c478bd9Sstevel@tonic-gate int sqliteOsSleep(int ms); 1847c478bd9Sstevel@tonic-gate int sqliteOsCurrentTime(double*); 1857c478bd9Sstevel@tonic-gate void sqliteOsEnterMutex(void); 1867c478bd9Sstevel@tonic-gate void sqliteOsLeaveMutex(void); 1877c478bd9Sstevel@tonic-gate char *sqliteOsFullPathname(const char*); 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate #endif /* _SQLITE_OS_H_ */ 192