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