monotone

monotone Mtn Source Tree

Root/sqlite/os.h

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** Figure out if we are dealing with Unix, Windows, or some other
22** operating system.
23*/
24#if defined(OS_OTHER)
25# if OS_OTHER==1
26# undef OS_UNIX
27# define OS_UNIX 0
28# undef OS_WIN
29# define OS_WIN 0
30# undef OS_OS2
31# define OS_OS2 0
32# else
33# undef OS_OTHER
34# endif
35#endif
36#if !defined(OS_UNIX) && !defined(OS_OTHER)
37# define OS_OTHER 0
38# ifndef OS_WIN
39# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
40# define OS_WIN 1
41# define OS_UNIX 0
42# define OS_OS2 0
43# elif defined(_EMX_) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__)
44# define OS_WIN 0
45# define OS_UNIX 0
46# define OS_OS2 1
47# else
48# define OS_WIN 0
49# define OS_UNIX 1
50# define OS_OS2 0
51# endif
52# else
53# define OS_UNIX 0
54# define OS_OS2 0
55# endif
56#else
57# ifndef OS_WIN
58# define OS_WIN 0
59# endif
60#endif
61
62
63/*
64** Define the maximum size of a temporary filename
65*/
66#if OS_WIN
67# include <windows.h>
68# define SQLITE_TEMPNAME_SIZE (MAX_PATH+50)
69#elif OS_OS2
70# define INCL_DOSDATETIME
71# define INCL_DOSFILEMGR
72# define INCL_DOSERRORS
73# define INCL_DOSMISC
74# define INCL_DOSPROCESS
75# include <os2.h>
76# define SQLITE_TEMPNAME_SIZE (CCHMAXPATHCOMP)
77#else
78# define SQLITE_TEMPNAME_SIZE 200
79#endif
80
81/* If the SET_FULLSYNC macro is not defined above, then make it
82** a no-op
83*/
84#ifndef SET_FULLSYNC
85# define SET_FULLSYNC(x,y)
86#endif
87
88/*
89** The default size of a disk sector
90*/
91#ifndef SQLITE_DEFAULT_SECTOR_SIZE
92# define SQLITE_DEFAULT_SECTOR_SIZE 512
93#endif
94
95/*
96** Temporary files are named starting with this prefix followed by 16 random
97** alphanumeric characters, and no file extension. They are stored in the
98** OS's standard temporary file directory, and are deleted prior to exit.
99** If sqlite is being embedded in another program, you may wish to change the
100** prefix to reflect your program's name, so that if your program exits
101** prematurely, old temporary files can be easily identified. This can be done
102** using -DTEMP_FILE_PREFIX=myprefix_ on the compiler command line.
103**
104** 2006-10-31: The default prefix used to be "sqlite_". But then
105** Mcafee started using SQLite in their anti-virus product and it
106** started putting files with the "sqlite" name in the c:/temp folder.
107** This annoyed many windows users. Those users would then do a
108** Google search for "sqlite", find the telephone numbers of the
109** developers and call to wake them up at night and complain.
110** For this reason, the default name prefix is changed to be "sqlite"
111** spelled backwards. So the temp files are still identified, but
112** anybody smart enough to figure out the code is also likely smart
113** enough to know that calling the developer will not help get rid
114** of the file.
115*/
116#ifndef TEMP_FILE_PREFIX
117# define TEMP_FILE_PREFIX "etilqs_"
118#endif
119
120/*
121** Define the interfaces for Unix, Windows, and OS/2.
122*/
123#if OS_UNIX
124#define sqlite3OsOpenReadWrite sqlite3UnixOpenReadWrite
125#define sqlite3OsOpenExclusive sqlite3UnixOpenExclusive
126#define sqlite3OsOpenReadOnly sqlite3UnixOpenReadOnly
127#define sqlite3OsDelete sqlite3UnixDelete
128#define sqlite3OsFileExists sqlite3UnixFileExists
129#define sqlite3OsFullPathname sqlite3UnixFullPathname
130#define sqlite3OsIsDirWritable sqlite3UnixIsDirWritable
131#define sqlite3OsSyncDirectory sqlite3UnixSyncDirectory
132#define sqlite3OsTempFileName sqlite3UnixTempFileName
133#define sqlite3OsRandomSeed sqlite3UnixRandomSeed
134#define sqlite3OsSleep sqlite3UnixSleep
135#define sqlite3OsCurrentTime sqlite3UnixCurrentTime
136#define sqlite3OsEnterMutex sqlite3UnixEnterMutex
137#define sqlite3OsLeaveMutex sqlite3UnixLeaveMutex
138#define sqlite3OsInMutex sqlite3UnixInMutex
139#define sqlite3OsThreadSpecificData sqlite3UnixThreadSpecificData
140#define sqlite3OsMalloc sqlite3GenericMalloc
141#define sqlite3OsRealloc sqlite3GenericRealloc
142#define sqlite3OsFree sqlite3GenericFree
143#define sqlite3OsAllocationSize sqlite3GenericAllocationSize
144#define sqlite3OsDlopen sqlite3UnixDlopen
145#define sqlite3OsDlsym sqlite3UnixDlsym
146#define sqlite3OsDlclose sqlite3UnixDlclose
147#endif
148#if OS_WIN
149#define sqlite3OsOpenReadWrite sqlite3WinOpenReadWrite
150#define sqlite3OsOpenExclusive sqlite3WinOpenExclusive
151#define sqlite3OsOpenReadOnly sqlite3WinOpenReadOnly
152#define sqlite3OsDelete sqlite3WinDelete
153#define sqlite3OsFileExists sqlite3WinFileExists
154#define sqlite3OsFullPathname sqlite3WinFullPathname
155#define sqlite3OsIsDirWritable sqlite3WinIsDirWritable
156#define sqlite3OsSyncDirectory sqlite3WinSyncDirectory
157#define sqlite3OsTempFileName sqlite3WinTempFileName
158#define sqlite3OsRandomSeed sqlite3WinRandomSeed
159#define sqlite3OsSleep sqlite3WinSleep
160#define sqlite3OsCurrentTime sqlite3WinCurrentTime
161#define sqlite3OsEnterMutex sqlite3WinEnterMutex
162#define sqlite3OsLeaveMutex sqlite3WinLeaveMutex
163#define sqlite3OsInMutex sqlite3WinInMutex
164#define sqlite3OsThreadSpecificData sqlite3WinThreadSpecificData
165#define sqlite3OsMalloc sqlite3GenericMalloc
166#define sqlite3OsRealloc sqlite3GenericRealloc
167#define sqlite3OsFree sqlite3GenericFree
168#define sqlite3OsAllocationSize sqlite3GenericAllocationSize
169#define sqlite3OsDlopen sqlite3WinDlopen
170#define sqlite3OsDlsym sqlite3WinDlsym
171#define sqlite3OsDlclose sqlite3WinDlclose
172#endif
173#if OS_OS2
174#define sqlite3OsOpenReadWrite sqlite3Os2OpenReadWrite
175#define sqlite3OsOpenExclusive sqlite3Os2OpenExclusive
176#define sqlite3OsOpenReadOnly sqlite3Os2OpenReadOnly
177#define sqlite3OsDelete sqlite3Os2Delete
178#define sqlite3OsFileExists sqlite3Os2FileExists
179#define sqlite3OsFullPathname sqlite3Os2FullPathname
180#define sqlite3OsIsDirWritable sqlite3Os2IsDirWritable
181#define sqlite3OsSyncDirectory sqlite3Os2SyncDirectory
182#define sqlite3OsTempFileName sqlite3Os2TempFileName
183#define sqlite3OsRandomSeed sqlite3Os2RandomSeed
184#define sqlite3OsSleep sqlite3Os2Sleep
185#define sqlite3OsCurrentTime sqlite3Os2CurrentTime
186#define sqlite3OsEnterMutex sqlite3Os2EnterMutex
187#define sqlite3OsLeaveMutex sqlite3Os2LeaveMutex
188#define sqlite3OsInMutex sqlite3Os2InMutex
189#define sqlite3OsThreadSpecificData sqlite3Os2ThreadSpecificData
190#define sqlite3OsMalloc sqlite3GenericMalloc
191#define sqlite3OsRealloc sqlite3GenericRealloc
192#define sqlite3OsFree sqlite3GenericFree
193#define sqlite3OsAllocationSize sqlite3GenericAllocationSize
194#define sqlite3OsDlopen sqlite3Os2Dlopen
195#define sqlite3OsDlsym sqlite3Os2Dlsym
196#define sqlite3OsDlclose sqlite3Os2Dlclose
197#endif
198
199
200
201
202/*
203** If using an alternative OS interface, then we must have an "os_other.h"
204** header file available for that interface. Presumably the "os_other.h"
205** header file contains #defines similar to those above.
206*/
207#if OS_OTHER
208# include "os_other.h"
209#endif
210
211
212
213/*
214** Forward declarations
215*/
216typedef struct OsFile OsFile;
217typedef struct IoMethod IoMethod;
218
219/*
220** An instance of the following structure contains pointers to all
221** methods on an OsFile object.
222*/
223struct IoMethod {
224 int (*xClose)(OsFile**);
225 int (*xOpenDirectory)(OsFile*, const char*);
226 int (*xRead)(OsFile*, void*, int amt);
227 int (*xWrite)(OsFile*, const void*, int amt);
228 int (*xSeek)(OsFile*, i64 offset);
229 int (*xTruncate)(OsFile*, i64 size);
230 int (*xSync)(OsFile*, int);
231 void (*xSetFullSync)(OsFile *id, int setting);
232 int (*xFileHandle)(OsFile *id);
233 int (*xFileSize)(OsFile*, i64 *pSize);
234 int (*xLock)(OsFile*, int);
235 int (*xUnlock)(OsFile*, int);
236 int (*xLockState)(OsFile *id);
237 int (*xCheckReservedLock)(OsFile *id);
238 int (*xSectorSize)(OsFile *id);
239};
240
241/*
242** The OsFile object describes an open disk file in an OS-dependent way.
243** The version of OsFile defined here is a generic version. Each OS
244** implementation defines its own subclass of this structure that contains
245** additional information needed to handle file I/O. But the pMethod
246** entry (pointing to the virtual function table) always occurs first
247** so that we can always find the appropriate methods.
248*/
249struct OsFile {
250 IoMethod const *pMethod;
251};
252
253/*
254** The following values may be passed as the second argument to
255** sqlite3OsLock(). The various locks exhibit the following semantics:
256**
257** SHARED: Any number of processes may hold a SHARED lock simultaneously.
258** RESERVED: A single process may hold a RESERVED lock on a file at
259** any time. Other processes may hold and obtain new SHARED locks.
260** PENDING: A single process may hold a PENDING lock on a file at
261** any one time. Existing SHARED locks may persist, but no new
262** SHARED locks may be obtained by other processes.
263** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
264**
265** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
266** process that requests an EXCLUSIVE lock may actually obtain a PENDING
267** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
268** sqlite3OsLock().
269*/
270#define NO_LOCK 0
271#define SHARED_LOCK 1
272#define RESERVED_LOCK 2
273#define PENDING_LOCK 3
274#define EXCLUSIVE_LOCK 4
275
276/*
277** File Locking Notes: (Mostly about windows but also some info for Unix)
278**
279** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
280** those functions are not available. So we use only LockFile() and
281** UnlockFile().
282**
283** LockFile() prevents not just writing but also reading by other processes.
284** A SHARED_LOCK is obtained by locking a single randomly-chosen
285** byte out of a specific range of bytes. The lock byte is obtained at
286** random so two separate readers can probably access the file at the
287** same time, unless they are unlucky and choose the same lock byte.
288** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
289** There can only be one writer. A RESERVED_LOCK is obtained by locking
290** a single byte of the file that is designated as the reserved lock byte.
291** A PENDING_LOCK is obtained by locking a designated byte different from
292** the RESERVED_LOCK byte.
293**
294** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
295** which means we can use reader/writer locks. When reader/writer locks
296** are used, the lock is placed on the same range of bytes that is used
297** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
298** will support two or more Win95 readers or two or more WinNT readers.
299** But a single Win95 reader will lock out all WinNT readers and a single
300** WinNT reader will lock out all other Win95 readers.
301**
302** The following #defines specify the range of bytes used for locking.
303** SHARED_SIZE is the number of bytes available in the pool from which
304** a random byte is selected for a shared lock. The pool of bytes for
305** shared locks begins at SHARED_FIRST.
306**
307** These #defines are available in sqlite_aux.h so that adaptors for
308** connecting SQLite to other operating systems can use the same byte
309** ranges for locking. In particular, the same locking strategy and
310** byte ranges are used for Unix. This leaves open the possiblity of having
311** clients on win95, winNT, and unix all talking to the same shared file
312** and all locking correctly. To do so would require that samba (or whatever
313** tool is being used for file sharing) implements locks correctly between
314** windows and unix. I'm guessing that isn't likely to happen, but by
315** using the same locking range we are at least open to the possibility.
316**
317** Locking in windows is manditory. For this reason, we cannot store
318** actual data in the bytes used for locking. The pager never allocates
319** the pages involved in locking therefore. SHARED_SIZE is selected so
320** that all locks will fit on a single page even at the minimum page size.
321** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
322** is set high so that we don't have to allocate an unused page except
323** for very large databases. But one should test the page skipping logic
324** by setting PENDING_BYTE low and running the entire regression suite.
325**
326** Changing the value of PENDING_BYTE results in a subtly incompatible
327** file format. Depending on how it is changed, you might not notice
328** the incompatibility right away, even running a full regression test.
329** The default location of PENDING_BYTE is the first byte past the
330** 1GB boundary.
331**
332*/
333#ifndef SQLITE_TEST
334#define PENDING_BYTE 0x40000000 /* First byte past the 1GB boundary */
335#else
336extern unsigned int sqlite3_pending_byte;
337#define PENDING_BYTE sqlite3_pending_byte
338#endif
339
340#define RESERVED_BYTE (PENDING_BYTE+1)
341#define SHARED_FIRST (PENDING_BYTE+2)
342#define SHARED_SIZE 510
343
344/*
345** Prototypes for operating system interface routines.
346*/
347int sqlite3OsClose(OsFile**);
348int sqlite3OsOpenDirectory(OsFile*, const char*);
349int sqlite3OsRead(OsFile*, void*, int amt);
350int sqlite3OsWrite(OsFile*, const void*, int amt);
351int sqlite3OsSeek(OsFile*, i64 offset);
352int sqlite3OsTruncate(OsFile*, i64 size);
353int sqlite3OsSync(OsFile*, int);
354void sqlite3OsSetFullSync(OsFile *id, int setting);
355int sqlite3OsFileSize(OsFile*, i64 *pSize);
356int sqlite3OsLock(OsFile*, int);
357int sqlite3OsUnlock(OsFile*, int);
358int sqlite3OsCheckReservedLock(OsFile *id);
359int sqlite3OsOpenReadWrite(const char*, OsFile**, int*);
360int sqlite3OsOpenExclusive(const char*, OsFile**, int);
361int sqlite3OsOpenReadOnly(const char*, OsFile**);
362int sqlite3OsDelete(const char*);
363int sqlite3OsFileExists(const char*);
364char *sqlite3OsFullPathname(const char*);
365int sqlite3OsIsDirWritable(char*);
366int sqlite3OsSyncDirectory(const char*);
367int sqlite3OsSectorSize(OsFile *id);
368int sqlite3OsTempFileName(char*);
369int sqlite3OsRandomSeed(char*);
370int sqlite3OsSleep(int ms);
371int sqlite3OsCurrentTime(double*);
372void sqlite3OsEnterMutex(void);
373void sqlite3OsLeaveMutex(void);
374int sqlite3OsInMutex(int);
375ThreadData *sqlite3OsThreadSpecificData(int);
376void *sqlite3OsMalloc(int);
377void *sqlite3OsRealloc(void *, int);
378void sqlite3OsFree(void *);
379int sqlite3OsAllocationSize(void *);
380void *sqlite3OsDlopen(const char*);
381void *sqlite3OsDlsym(void*, const char*);
382int sqlite3OsDlclose(void*);
383
384#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
385 int sqlite3OsFileHandle(OsFile *id);
386 int sqlite3OsLockState(OsFile *id);
387#endif
388
389/*
390** If the SQLITE_ENABLE_REDEF_IO macro is defined, then the OS-layer
391** interface routines are not called directly but are invoked using
392** pointers to functions. This allows the implementation of various
393** OS-layer interface routines to be modified at run-time. There are
394** obscure but legitimate reasons for wanting to do this. But for
395** most users, a direct call to the underlying interface is preferable
396** so the the redefinable I/O interface is turned off by default.
397*/
398#ifdef SQLITE_ENABLE_REDEF_IO
399
400/*
401** When redefinable I/O is enabled, a single global instance of the
402** following structure holds pointers to the routines that SQLite
403** uses to talk with the underlying operating system. Modify this
404** structure (before using any SQLite API!) to accomodate perculiar
405** operating system interfaces or behaviors.
406*/
407struct sqlite3OsVtbl {
408 int (*xOpenReadWrite)(const char*, OsFile**, int*);
409 int (*xOpenExclusive)(const char*, OsFile**, int);
410 int (*xOpenReadOnly)(const char*, OsFile**);
411
412 int (*xDelete)(const char*);
413 int (*xFileExists)(const char*);
414 char *(*xFullPathname)(const char*);
415 int (*xIsDirWritable)(char*);
416 int (*xSyncDirectory)(const char*);
417 int (*xTempFileName)(char*);
418
419 int (*xRandomSeed)(char*);
420 int (*xSleep)(int ms);
421 int (*xCurrentTime)(double*);
422
423 void (*xEnterMutex)(void);
424 void (*xLeaveMutex)(void);
425 int (*xInMutex)(int);
426 ThreadData *(*xThreadSpecificData)(int);
427
428 void *(*xMalloc)(int);
429 void *(*xRealloc)(void *, int);
430 void (*xFree)(void *);
431 int (*xAllocationSize)(void *);
432
433 void *(*xDlopen)(const char*);
434 void *(*xDlsym)(void*, const char*);
435 int (*xDlclose)(void*);
436};
437
438/* Macro used to comment out routines that do not exists when there is
439** no disk I/O or extension loading
440*/
441#ifdef SQLITE_OMIT_DISKIO
442# define IF_DISKIO(X) 0
443#else
444# define IF_DISKIO(X) X
445#endif
446#ifdef SQLITE_OMIT_LOAD_EXTENSION
447# define IF_DLOPEN(X) 0
448#else
449# define IF_DLOPEN(X) X
450#endif
451
452
453#if defined(_SQLITE_OS_C_) || defined(SQLITE_AMALGAMATION)
454 /*
455 ** The os.c file implements the global virtual function table.
456 ** We have to put this file here because the initializers
457 ** (ex: sqlite3OsRandomSeed) are macros that are about to be
458 ** redefined.
459 */
460 struct sqlite3OsVtbl sqlite3Os = {
461 IF_DISKIO( sqlite3OsOpenReadWrite ),
462 IF_DISKIO( sqlite3OsOpenExclusive ),
463 IF_DISKIO( sqlite3OsOpenReadOnly ),
464 IF_DISKIO( sqlite3OsDelete ),
465 IF_DISKIO( sqlite3OsFileExists ),
466 IF_DISKIO( sqlite3OsFullPathname ),
467 IF_DISKIO( sqlite3OsIsDirWritable ),
468 IF_DISKIO( sqlite3OsSyncDirectory ),
469 IF_DISKIO( sqlite3OsTempFileName ),
470 sqlite3OsRandomSeed,
471 sqlite3OsSleep,
472 sqlite3OsCurrentTime,
473 sqlite3OsEnterMutex,
474 sqlite3OsLeaveMutex,
475 sqlite3OsInMutex,
476 sqlite3OsThreadSpecificData,
477 sqlite3OsMalloc,
478 sqlite3OsRealloc,
479 sqlite3OsFree,
480 sqlite3OsAllocationSize,
481 IF_DLOPEN( sqlite3OsDlopen ),
482 IF_DLOPEN( sqlite3OsDlsym ),
483 IF_DLOPEN( sqlite3OsDlclose ),
484 };
485#else
486 /*
487 ** Files other than os.c just reference the global virtual function table.
488 */
489 extern struct sqlite3OsVtbl sqlite3Os;
490#endif /* _SQLITE_OS_C_ */
491
492
493/* This additional API routine is available with redefinable I/O */
494struct sqlite3OsVtbl *sqlite3_os_switch(void);
495
496
497/*
498** Redefine the OS interface to go through the virtual function table
499** rather than calling routines directly.
500*/
501#undef sqlite3OsOpenReadWrite
502#undef sqlite3OsOpenExclusive
503#undef sqlite3OsOpenReadOnly
504#undef sqlite3OsDelete
505#undef sqlite3OsFileExists
506#undef sqlite3OsFullPathname
507#undef sqlite3OsIsDirWritable
508#undef sqlite3OsSyncDirectory
509#undef sqlite3OsTempFileName
510#undef sqlite3OsRandomSeed
511#undef sqlite3OsSleep
512#undef sqlite3OsCurrentTime
513#undef sqlite3OsEnterMutex
514#undef sqlite3OsLeaveMutex
515#undef sqlite3OsInMutex
516#undef sqlite3OsThreadSpecificData
517#undef sqlite3OsMalloc
518#undef sqlite3OsRealloc
519#undef sqlite3OsFree
520#undef sqlite3OsAllocationSize
521#define sqlite3OsOpenReadWrite sqlite3Os.xOpenReadWrite
522#define sqlite3OsOpenExclusive sqlite3Os.xOpenExclusive
523#define sqlite3OsOpenReadOnly sqlite3Os.xOpenReadOnly
524#define sqlite3OsDelete sqlite3Os.xDelete
525#define sqlite3OsFileExists sqlite3Os.xFileExists
526#define sqlite3OsFullPathname sqlite3Os.xFullPathname
527#define sqlite3OsIsDirWritable sqlite3Os.xIsDirWritable
528#define sqlite3OsSyncDirectory sqlite3Os.xSyncDirectory
529#define sqlite3OsTempFileName sqlite3Os.xTempFileName
530#define sqlite3OsRandomSeed sqlite3Os.xRandomSeed
531#define sqlite3OsSleep sqlite3Os.xSleep
532#define sqlite3OsCurrentTime sqlite3Os.xCurrentTime
533#define sqlite3OsEnterMutex sqlite3Os.xEnterMutex
534#define sqlite3OsLeaveMutex sqlite3Os.xLeaveMutex
535#define sqlite3OsInMutex sqlite3Os.xInMutex
536#define sqlite3OsThreadSpecificData sqlite3Os.xThreadSpecificData
537#define sqlite3OsMalloc sqlite3Os.xMalloc
538#define sqlite3OsRealloc sqlite3Os.xRealloc
539#define sqlite3OsFree sqlite3Os.xFree
540#define sqlite3OsAllocationSize sqlite3Os.xAllocationSize
541
542#endif /* SQLITE_ENABLE_REDEF_IO */
543
544#endif /* _SQLITE_OS_H_ */

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status