annotate sqlite/os_unix.c @ 1530:bdb22d1e6302 trunk

[svn] - don't peg the CPU
author nenolod
date Mon, 07 Aug 2006 23:27:43 -0700
parents b6b61becdf4e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1434
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
2 ** 2004 May 22
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
3 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
4 ** The author disclaims copyright to this source code. In place of
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
5 ** a legal notice, here is a blessing:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
6 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
7 ** May you do good and not evil.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
8 ** May you find forgiveness for yourself and forgive others.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
9 ** May you share freely, never taking more than you give.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
10 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
11 ******************************************************************************
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
12 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
13 ** This file contains code that is specific to Unix systems.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
14 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
15 #include "sqliteInt.h"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
16 #include "os.h"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
17 #if OS_UNIX /* This file is used on unix only */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
18
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
19 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
20 ** These #defines should enable >2GB file support on Posix if the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
21 ** underlying operating system supports it. If the OS lacks
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
22 ** large file support, these should be no-ops.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
23 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
24 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
25 ** on the compiler command line. This is necessary if you are compiling
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
26 ** on a recent machine (ex: RedHat 7.2) but you want your code to work
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
27 ** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
28 ** without this option, LFS is enable. But LFS does not exist in the kernel
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
29 ** in RedHat 6.0, so the code won't work. Hence, for maximum binary
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
30 ** portability you should omit LFS.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
31 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
32 #ifndef SQLITE_DISABLE_LFS
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
33 # define _LARGE_FILE 1
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
34 # ifndef _FILE_OFFSET_BITS
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
35 # define _FILE_OFFSET_BITS 64
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
36 # endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
37 # define _LARGEFILE_SOURCE 1
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
38 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
39
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
40 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
41 ** standard include files.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
42 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
43 #include <sys/types.h>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
44 #include <sys/stat.h>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
45 #include <fcntl.h>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
46 #include <unistd.h>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
47 #include <time.h>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
48 #include <sys/time.h>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
49 #include <errno.h>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
50
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
51 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
52 ** If we are to be thread-safe, include the pthreads header and define
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
53 ** the SQLITE_UNIX_THREADS macro.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
54 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
55 #if defined(THREADSAFE) && THREADSAFE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
56 # include <pthread.h>
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
57 # define SQLITE_UNIX_THREADS 1
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
58 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
59
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
60 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
61 ** Default permissions when creating a new file
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
62 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
63 #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
64 # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
65 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
66
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
67
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
68
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
69 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
70 ** The unixFile structure is subclass of OsFile specific for the unix
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
71 ** protability layer.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
72 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
73 typedef struct unixFile unixFile;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
74 struct unixFile {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
75 IoMethod const *pMethod; /* Always the first entry */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
76 struct openCnt *pOpen; /* Info about all open fd's on this inode */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
77 struct lockInfo *pLock; /* Info about locks on this inode */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
78 int h; /* The file descriptor */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
79 unsigned char locktype; /* The type of lock held on this fd */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
80 unsigned char isOpen; /* True if needs to be closed */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
81 unsigned char fullSync; /* Use F_FULLSYNC if available */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
82 int dirfd; /* File descriptor for the directory */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
83 i64 offset; /* Seek offset */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
84 #ifdef SQLITE_UNIX_THREADS
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
85 pthread_t tid; /* The thread that "owns" this OsFile */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
86 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
87 };
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
88
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
89 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
90 ** Provide the ability to override some OS-layer functions during
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
91 ** testing. This is used to simulate OS crashes to verify that
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
92 ** commits are atomic even in the event of an OS crash.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
93 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
94 #ifdef SQLITE_CRASH_TEST
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
95 extern int sqlite3CrashTestEnable;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
96 extern int sqlite3CrashOpenReadWrite(const char*, OsFile**, int*);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
97 extern int sqlite3CrashOpenExclusive(const char*, OsFile**, int);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
98 extern int sqlite3CrashOpenReadOnly(const char*, OsFile**, int);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
99 # define CRASH_TEST_OVERRIDE(X,A,B,C) \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
100 if(sqlite3CrashTestEnable){ return X(A,B,C); }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
101 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
102 # define CRASH_TEST_OVERRIDE(X,A,B,C) /* no-op */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
103 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
104
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
105
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
106 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
107 ** Include code that is common to all os_*.c files
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
108 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
109 #include "os_common.h"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
110
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
111 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
112 ** Do not include any of the File I/O interface procedures if the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
113 ** SQLITE_OMIT_DISKIO macro is defined (indicating that the database
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
114 ** will be in-memory only)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
115 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
116 #ifndef SQLITE_OMIT_DISKIO
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
117
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
118
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
119 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
120 ** Define various macros that are missing from some systems.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
121 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
122 #ifndef O_LARGEFILE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
123 # define O_LARGEFILE 0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
124 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
125 #ifdef SQLITE_DISABLE_LFS
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
126 # undef O_LARGEFILE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
127 # define O_LARGEFILE 0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
128 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
129 #ifndef O_NOFOLLOW
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
130 # define O_NOFOLLOW 0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
131 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
132 #ifndef O_BINARY
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
133 # define O_BINARY 0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
134 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
135
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
136 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
137 ** The DJGPP compiler environment looks mostly like Unix, but it
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
138 ** lacks the fcntl() system call. So redefine fcntl() to be something
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
139 ** that always succeeds. This means that locking does not occur under
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
140 ** DJGPP. But it's DOS - what did you expect?
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
141 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
142 #ifdef __DJGPP__
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
143 # define fcntl(A,B,C) 0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
144 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
145
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
146 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
147 ** The threadid macro resolves to the thread-id or to 0. Used for
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
148 ** testing and debugging only.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
149 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
150 #ifdef SQLITE_UNIX_THREADS
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
151 #define threadid pthread_self()
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
152 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
153 #define threadid 0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
154 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
155
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
156 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
157 ** Set or check the OsFile.tid field. This field is set when an OsFile
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
158 ** is first opened. All subsequent uses of the OsFile verify that the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
159 ** same thread is operating on the OsFile. Some operating systems do
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
160 ** not allow locks to be overridden by other threads and that restriction
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
161 ** means that sqlite3* database handles cannot be moved from one thread
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
162 ** to another. This logic makes sure a user does not try to do that
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
163 ** by mistake.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
164 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
165 ** Version 3.3.1 (2006-01-15): OsFiles can be moved from one thread to
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
166 ** another as long as we are running on a system that supports threads
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
167 ** overriding each others locks (which now the most common behavior)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
168 ** or if no locks are held. But the OsFile.pLock field needs to be
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
169 ** recomputed because its key includes the thread-id. See the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
170 ** transferOwnership() function below for additional information
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
171 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
172 #if defined(SQLITE_UNIX_THREADS)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
173 # define SET_THREADID(X) (X)->tid = pthread_self()
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
174 # define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
175 !pthread_equal((X)->tid, pthread_self()))
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
176 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
177 # define SET_THREADID(X)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
178 # define CHECK_THREADID(X) 0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
179 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
180
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
181 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
182 ** Here is the dirt on POSIX advisory locks: ANSI STD 1003.1 (1996)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
183 ** section 6.5.2.2 lines 483 through 490 specify that when a process
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
184 ** sets or clears a lock, that operation overrides any prior locks set
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
185 ** by the same process. It does not explicitly say so, but this implies
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
186 ** that it overrides locks set by the same process using a different
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
187 ** file descriptor. Consider this test case:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
188 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
189 ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
190 ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
191 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
192 ** Suppose ./file1 and ./file2 are really the same file (because
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
193 ** one is a hard or symbolic link to the other) then if you set
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
194 ** an exclusive lock on fd1, then try to get an exclusive lock
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
195 ** on fd2, it works. I would have expected the second lock to
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
196 ** fail since there was already a lock on the file due to fd1.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
197 ** But not so. Since both locks came from the same process, the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
198 ** second overrides the first, even though they were on different
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
199 ** file descriptors opened on different file names.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
200 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
201 ** Bummer. If you ask me, this is broken. Badly broken. It means
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
202 ** that we cannot use POSIX locks to synchronize file access among
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
203 ** competing threads of the same process. POSIX locks will work fine
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
204 ** to synchronize access for threads in separate processes, but not
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
205 ** threads within the same process.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
206 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
207 ** To work around the problem, SQLite has to manage file locks internally
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
208 ** on its own. Whenever a new database is opened, we have to find the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
209 ** specific inode of the database file (the inode is determined by the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
210 ** st_dev and st_ino fields of the stat structure that fstat() fills in)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
211 ** and check for locks already existing on that inode. When locks are
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
212 ** created or removed, we have to look at our own internal record of the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
213 ** locks to see if another thread has previously set a lock on that same
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
214 ** inode.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
215 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
216 ** The OsFile structure for POSIX is no longer just an integer file
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
217 ** descriptor. It is now a structure that holds the integer file
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
218 ** descriptor and a pointer to a structure that describes the internal
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
219 ** locks on the corresponding inode. There is one locking structure
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
220 ** per inode, so if the same inode is opened twice, both OsFile structures
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
221 ** point to the same locking structure. The locking structure keeps
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
222 ** a reference count (so we will know when to delete it) and a "cnt"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
223 ** field that tells us its internal lock status. cnt==0 means the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
224 ** file is unlocked. cnt==-1 means the file has an exclusive lock.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
225 ** cnt>0 means there are cnt shared locks on the file.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
226 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
227 ** Any attempt to lock or unlock a file first checks the locking
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
228 ** structure. The fcntl() system call is only invoked to set a
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
229 ** POSIX lock if the internal lock structure transitions between
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
230 ** a locked and an unlocked state.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
231 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
232 ** 2004-Jan-11:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
233 ** More recent discoveries about POSIX advisory locks. (The more
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
234 ** I discover, the more I realize the a POSIX advisory locks are
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
235 ** an abomination.)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
236 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
237 ** If you close a file descriptor that points to a file that has locks,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
238 ** all locks on that file that are owned by the current process are
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
239 ** released. To work around this problem, each OsFile structure contains
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
240 ** a pointer to an openCnt structure. There is one openCnt structure
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
241 ** per open inode, which means that multiple OsFiles can point to a single
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
242 ** openCnt. When an attempt is made to close an OsFile, if there are
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
243 ** other OsFiles open on the same inode that are holding locks, the call
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
244 ** to close() the file descriptor is deferred until all of the locks clear.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
245 ** The openCnt structure keeps a list of file descriptors that need to
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
246 ** be closed and that list is walked (and cleared) when the last lock
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
247 ** clears.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
248 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
249 ** First, under Linux threads, because each thread has a separate
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
250 ** process ID, lock operations in one thread do not override locks
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
251 ** to the same file in other threads. Linux threads behave like
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
252 ** separate processes in this respect. But, if you close a file
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
253 ** descriptor in linux threads, all locks are cleared, even locks
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
254 ** on other threads and even though the other threads have different
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
255 ** process IDs. Linux threads is inconsistent in this respect.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
256 ** (I'm beginning to think that linux threads is an abomination too.)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
257 ** The consequence of this all is that the hash table for the lockInfo
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
258 ** structure has to include the process id as part of its key because
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
259 ** locks in different threads are treated as distinct. But the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
260 ** openCnt structure should not include the process id in its
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
261 ** key because close() clears lock on all threads, not just the current
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
262 ** thread. Were it not for this goofiness in linux threads, we could
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
263 ** combine the lockInfo and openCnt structures into a single structure.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
264 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
265 ** 2004-Jun-28:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
266 ** On some versions of linux, threads can override each others locks.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
267 ** On others not. Sometimes you can change the behavior on the same
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
268 ** system by setting the LD_ASSUME_KERNEL environment variable. The
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
269 ** POSIX standard is silent as to which behavior is correct, as far
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
270 ** as I can tell, so other versions of unix might show the same
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
271 ** inconsistency. There is no little doubt in my mind that posix
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
272 ** advisory locks and linux threads are profoundly broken.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
273 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
274 ** To work around the inconsistencies, we have to test at runtime
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
275 ** whether or not threads can override each others locks. This test
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
276 ** is run once, the first time any lock is attempted. A static
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
277 ** variable is set to record the results of this test for future
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
278 ** use.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
279 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
280
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
281 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
282 ** An instance of the following structure serves as the key used
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
283 ** to locate a particular lockInfo structure given its inode.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
284 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
285 ** If threads cannot override each others locks, then we set the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
286 ** lockKey.tid field to the thread ID. If threads can override
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
287 ** each others locks then tid is always set to zero. tid is omitted
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
288 ** if we compile without threading support.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
289 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
290 struct lockKey {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
291 dev_t dev; /* Device number */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
292 ino_t ino; /* Inode number */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
293 #ifdef SQLITE_UNIX_THREADS
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
294 pthread_t tid; /* Thread ID or zero if threads can override each other */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
295 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
296 };
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
297
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
298 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
299 ** An instance of the following structure is allocated for each open
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
300 ** inode on each thread with a different process ID. (Threads have
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
301 ** different process IDs on linux, but not on most other unixes.)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
302 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
303 ** A single inode can have multiple file descriptors, so each OsFile
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
304 ** structure contains a pointer to an instance of this object and this
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
305 ** object keeps a count of the number of OsFiles pointing to it.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
306 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
307 struct lockInfo {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
308 struct lockKey key; /* The lookup key */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
309 int cnt; /* Number of SHARED locks held */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
310 int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
311 int nRef; /* Number of pointers to this structure */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
312 };
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
313
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
314 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
315 ** An instance of the following structure serves as the key used
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
316 ** to locate a particular openCnt structure given its inode. This
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
317 ** is the same as the lockKey except that the thread ID is omitted.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
318 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
319 struct openKey {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
320 dev_t dev; /* Device number */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
321 ino_t ino; /* Inode number */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
322 };
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
323
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
324 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
325 ** An instance of the following structure is allocated for each open
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
326 ** inode. This structure keeps track of the number of locks on that
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
327 ** inode. If a close is attempted against an inode that is holding
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
328 ** locks, the close is deferred until all locks clear by adding the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
329 ** file descriptor to be closed to the pending list.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
330 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
331 struct openCnt {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
332 struct openKey key; /* The lookup key */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
333 int nRef; /* Number of pointers to this structure */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
334 int nLock; /* Number of outstanding locks */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
335 int nPending; /* Number of pending close() operations */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
336 int *aPending; /* Malloced space holding fd's awaiting a close() */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
337 };
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
338
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
339 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
340 ** These hash tables map inodes and file descriptors (really, lockKey and
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
341 ** openKey structures) into lockInfo and openCnt structures. Access to
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
342 ** these hash tables must be protected by a mutex.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
343 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
344 static Hash lockHash = {SQLITE_HASH_BINARY, 0, 0, 0,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
345 sqlite3ThreadSafeMalloc, sqlite3ThreadSafeFree, 0, 0};
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
346 static Hash openHash = {SQLITE_HASH_BINARY, 0, 0, 0,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
347 sqlite3ThreadSafeMalloc, sqlite3ThreadSafeFree, 0, 0};
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
348
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
349 #ifdef SQLITE_UNIX_THREADS
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
350 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
351 ** This variable records whether or not threads can override each others
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
352 ** locks.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
353 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
354 ** 0: No. Threads cannot override each others locks.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
355 ** 1: Yes. Threads can override each others locks.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
356 ** -1: We don't know yet.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
357 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
358 ** On some systems, we know at compile-time if threads can override each
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
359 ** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
360 ** will be set appropriately. On other systems, we have to check at
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
361 ** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
362 ** undefined.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
363 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
364 ** This variable normally has file scope only. But during testing, we make
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
365 ** it a global so that the test code can change its value in order to verify
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
366 ** that the right stuff happens in either case.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
367 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
368 #ifndef SQLITE_THREAD_OVERRIDE_LOCK
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
369 # define SQLITE_THREAD_OVERRIDE_LOCK -1
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
370 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
371 #ifdef SQLITE_TEST
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
372 int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
373 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
374 static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
375 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
376
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
377 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
378 ** This structure holds information passed into individual test
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
379 ** threads by the testThreadLockingBehavior() routine.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
380 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
381 struct threadTestData {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
382 int fd; /* File to be locked */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
383 struct flock lock; /* The locking operation */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
384 int result; /* Result of the locking operation */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
385 };
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
386
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
387 #ifdef SQLITE_LOCK_TRACE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
388 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
389 ** Print out information about all locking operations.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
390 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
391 ** This routine is used for troubleshooting locks on multithreaded
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
392 ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
393 ** command-line option on the compiler. This code is normally
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
394 ** turned off.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
395 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
396 static int lockTrace(int fd, int op, struct flock *p){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
397 char *zOpName, *zType;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
398 int s;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
399 int savedErrno;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
400 if( op==F_GETLK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
401 zOpName = "GETLK";
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
402 }else if( op==F_SETLK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
403 zOpName = "SETLK";
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
404 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
405 s = fcntl(fd, op, p);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
406 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
407 return s;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
408 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
409 if( p->l_type==F_RDLCK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
410 zType = "RDLCK";
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
411 }else if( p->l_type==F_WRLCK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
412 zType = "WRLCK";
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
413 }else if( p->l_type==F_UNLCK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
414 zType = "UNLCK";
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
415 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
416 assert( 0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
417 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
418 assert( p->l_whence==SEEK_SET );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
419 s = fcntl(fd, op, p);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
420 savedErrno = errno;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
421 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
422 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
423 (int)p->l_pid, s);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
424 if( s && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
425 struct flock l2;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
426 l2 = *p;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
427 fcntl(fd, F_GETLK, &l2);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
428 if( l2.l_type==F_RDLCK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
429 zType = "RDLCK";
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
430 }else if( l2.l_type==F_WRLCK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
431 zType = "WRLCK";
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
432 }else if( l2.l_type==F_UNLCK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
433 zType = "UNLCK";
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
434 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
435 assert( 0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
436 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
437 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
438 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
439 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
440 errno = savedErrno;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
441 return s;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
442 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
443 #define fcntl lockTrace
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
444 #endif /* SQLITE_LOCK_TRACE */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
445
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
446 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
447 ** The testThreadLockingBehavior() routine launches two separate
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
448 ** threads on this routine. This routine attempts to lock a file
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
449 ** descriptor then returns. The success or failure of that attempt
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
450 ** allows the testThreadLockingBehavior() procedure to determine
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
451 ** whether or not threads can override each others locks.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
452 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
453 static void *threadLockingTest(void *pArg){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
454 struct threadTestData *pData = (struct threadTestData*)pArg;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
455 pData->result = fcntl(pData->fd, F_SETLK, &pData->lock);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
456 return pArg;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
457 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
458
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
459 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
460 ** This procedure attempts to determine whether or not threads
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
461 ** can override each others locks then sets the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
462 ** threadsOverrideEachOthersLocks variable appropriately.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
463 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
464 static void testThreadLockingBehavior(int fd_orig){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
465 int fd;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
466 struct threadTestData d[2];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
467 pthread_t t[2];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
468
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
469 fd = dup(fd_orig);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
470 if( fd<0 ) return;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
471 memset(d, 0, sizeof(d));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
472 d[0].fd = fd;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
473 d[0].lock.l_type = F_RDLCK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
474 d[0].lock.l_len = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
475 d[0].lock.l_start = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
476 d[0].lock.l_whence = SEEK_SET;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
477 d[1] = d[0];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
478 d[1].lock.l_type = F_WRLCK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
479 pthread_create(&t[0], 0, threadLockingTest, &d[0]);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
480 pthread_create(&t[1], 0, threadLockingTest, &d[1]);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
481 pthread_join(t[0], 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
482 pthread_join(t[1], 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
483 close(fd);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
484 threadsOverrideEachOthersLocks = d[0].result==0 && d[1].result==0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
485 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
486 #endif /* SQLITE_UNIX_THREADS */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
487
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
488 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
489 ** Release a lockInfo structure previously allocated by findLockInfo().
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
490 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
491 static void releaseLockInfo(struct lockInfo *pLock){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
492 assert( sqlite3OsInMutex(1) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
493 pLock->nRef--;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
494 if( pLock->nRef==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
495 sqlite3HashInsert(&lockHash, &pLock->key, sizeof(pLock->key), 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
496 sqlite3ThreadSafeFree(pLock);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
497 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
498 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
499
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
500 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
501 ** Release a openCnt structure previously allocated by findLockInfo().
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
502 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
503 static void releaseOpenCnt(struct openCnt *pOpen){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
504 assert( sqlite3OsInMutex(1) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
505 pOpen->nRef--;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
506 if( pOpen->nRef==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
507 sqlite3HashInsert(&openHash, &pOpen->key, sizeof(pOpen->key), 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
508 free(pOpen->aPending);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
509 sqlite3ThreadSafeFree(pOpen);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
510 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
511 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
512
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
513 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
514 ** Given a file descriptor, locate lockInfo and openCnt structures that
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
515 ** describes that file descriptor. Create new ones if necessary. The
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
516 ** return values might be uninitialized if an error occurs.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
517 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
518 ** Return the number of errors.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
519 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
520 static int findLockInfo(
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
521 int fd, /* The file descriptor used in the key */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
522 struct lockInfo **ppLock, /* Return the lockInfo structure here */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
523 struct openCnt **ppOpen /* Return the openCnt structure here */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
524 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
525 int rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
526 struct lockKey key1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
527 struct openKey key2;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
528 struct stat statbuf;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
529 struct lockInfo *pLock;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
530 struct openCnt *pOpen;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
531 rc = fstat(fd, &statbuf);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
532 if( rc!=0 ) return 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
533
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
534 assert( sqlite3OsInMutex(1) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
535 memset(&key1, 0, sizeof(key1));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
536 key1.dev = statbuf.st_dev;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
537 key1.ino = statbuf.st_ino;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
538 #ifdef SQLITE_UNIX_THREADS
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
539 if( threadsOverrideEachOthersLocks<0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
540 testThreadLockingBehavior(fd);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
541 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
542 key1.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
543 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
544 memset(&key2, 0, sizeof(key2));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
545 key2.dev = statbuf.st_dev;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
546 key2.ino = statbuf.st_ino;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
547 pLock = (struct lockInfo*)sqlite3HashFind(&lockHash, &key1, sizeof(key1));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
548 if( pLock==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
549 struct lockInfo *pOld;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
550 pLock = sqlite3ThreadSafeMalloc( sizeof(*pLock) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
551 if( pLock==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
552 rc = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
553 goto exit_findlockinfo;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
554 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
555 pLock->key = key1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
556 pLock->nRef = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
557 pLock->cnt = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
558 pLock->locktype = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
559 pOld = sqlite3HashInsert(&lockHash, &pLock->key, sizeof(key1), pLock);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
560 if( pOld!=0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
561 assert( pOld==pLock );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
562 sqlite3ThreadSafeFree(pLock);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
563 rc = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
564 goto exit_findlockinfo;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
565 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
566 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
567 pLock->nRef++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
568 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
569 *ppLock = pLock;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
570 if( ppOpen!=0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
571 pOpen = (struct openCnt*)sqlite3HashFind(&openHash, &key2, sizeof(key2));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
572 if( pOpen==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
573 struct openCnt *pOld;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
574 pOpen = sqlite3ThreadSafeMalloc( sizeof(*pOpen) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
575 if( pOpen==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
576 releaseLockInfo(pLock);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
577 rc = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
578 goto exit_findlockinfo;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
579 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
580 pOpen->key = key2;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
581 pOpen->nRef = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
582 pOpen->nLock = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
583 pOpen->nPending = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
584 pOpen->aPending = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
585 pOld = sqlite3HashInsert(&openHash, &pOpen->key, sizeof(key2), pOpen);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
586 if( pOld!=0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
587 assert( pOld==pOpen );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
588 sqlite3ThreadSafeFree(pOpen);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
589 releaseLockInfo(pLock);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
590 rc = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
591 goto exit_findlockinfo;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
592 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
593 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
594 pOpen->nRef++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
595 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
596 *ppOpen = pOpen;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
597 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
598
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
599 exit_findlockinfo:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
600 return rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
601 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
602
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
603 #ifdef SQLITE_DEBUG
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
604 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
605 ** Helper function for printing out trace information from debugging
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
606 ** binaries. This returns the string represetation of the supplied
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
607 ** integer lock-type.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
608 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
609 static const char *locktypeName(int locktype){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
610 switch( locktype ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
611 case NO_LOCK: return "NONE";
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
612 case SHARED_LOCK: return "SHARED";
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
613 case RESERVED_LOCK: return "RESERVED";
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
614 case PENDING_LOCK: return "PENDING";
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
615 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
616 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
617 return "ERROR";
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
618 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
619 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
620
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
621 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
622 ** If we are currently in a different thread than the thread that the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
623 ** unixFile argument belongs to, then transfer ownership of the unixFile
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
624 ** over to the current thread.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
625 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
626 ** A unixFile is only owned by a thread on systems where one thread is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
627 ** unable to override locks created by a different thread. RedHat9 is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
628 ** an example of such a system.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
629 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
630 ** Ownership transfer is only allowed if the unixFile is currently unlocked.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
631 ** If the unixFile is locked and an ownership is wrong, then return
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
632 ** SQLITE_MISUSE. SQLITE_OK is returned if everything works.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
633 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
634 #ifdef SQLITE_UNIX_THREADS
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
635 static int transferOwnership(unixFile *pFile){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
636 int rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
637 pthread_t hSelf;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
638 if( threadsOverrideEachOthersLocks ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
639 /* Ownership transfers not needed on this system */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
640 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
641 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
642 hSelf = pthread_self();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
643 if( pthread_equal(pFile->tid, hSelf) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
644 /* We are still in the same thread */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
645 TRACE1("No-transfer, same thread\n");
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
646 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
647 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
648 if( pFile->locktype!=NO_LOCK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
649 /* We cannot change ownership while we are holding a lock! */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
650 return SQLITE_MISUSE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
651 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
652 TRACE4("Transfer ownership of %d from %d to %d\n", pFile->h,pFile->tid,hSelf);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
653 pFile->tid = hSelf;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
654 releaseLockInfo(pFile->pLock);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
655 rc = findLockInfo(pFile->h, &pFile->pLock, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
656 TRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
657 locktypeName(pFile->locktype),
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
658 locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
659 return rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
660 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
661 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
662 /* On single-threaded builds, ownership transfer is a no-op */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
663 # define transferOwnership(X) SQLITE_OK
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
664 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
665
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
666 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
667 ** Delete the named file
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
668 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
669 int sqlite3UnixDelete(const char *zFilename){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
670 unlink(zFilename);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
671 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
672 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
673
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
674 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
675 ** Return TRUE if the named file exists.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
676 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
677 int sqlite3UnixFileExists(const char *zFilename){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
678 return access(zFilename, 0)==0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
679 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
680
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
681 /* Forward declaration */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
682 static int allocateUnixFile(unixFile *pInit, OsFile **pId);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
683
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
684 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
685 ** Attempt to open a file for both reading and writing. If that
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
686 ** fails, try opening it read-only. If the file does not exist,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
687 ** try to create it.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
688 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
689 ** On success, a handle for the open file is written to *id
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
690 ** and *pReadonly is set to 0 if the file was opened for reading and
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
691 ** writing or 1 if the file was opened read-only. The function returns
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
692 ** SQLITE_OK.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
693 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
694 ** On failure, the function returns SQLITE_CANTOPEN and leaves
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
695 ** *id and *pReadonly unchanged.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
696 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
697 int sqlite3UnixOpenReadWrite(
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
698 const char *zFilename,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
699 OsFile **pId,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
700 int *pReadonly
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
701 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
702 int rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
703 unixFile f;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
704
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
705 CRASH_TEST_OVERRIDE(sqlite3CrashOpenReadWrite, zFilename, pId, pReadonly);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
706 assert( 0==*pId );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
707 f.h = open(zFilename, O_RDWR|O_CREAT|O_LARGEFILE|O_BINARY,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
708 SQLITE_DEFAULT_FILE_PERMISSIONS);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
709 if( f.h<0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
710 #ifdef EISDIR
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
711 if( errno==EISDIR ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
712 return SQLITE_CANTOPEN;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
713 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
714 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
715 f.h = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
716 if( f.h<0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
717 return SQLITE_CANTOPEN;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
718 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
719 *pReadonly = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
720 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
721 *pReadonly = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
722 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
723 sqlite3OsEnterMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
724 rc = findLockInfo(f.h, &f.pLock, &f.pOpen);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
725 sqlite3OsLeaveMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
726 if( rc ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
727 close(f.h);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
728 return SQLITE_NOMEM;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
729 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
730 TRACE3("OPEN %-3d %s\n", f.h, zFilename);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
731 return allocateUnixFile(&f, pId);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
732 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
733
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
734
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
735 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
736 ** Attempt to open a new file for exclusive access by this process.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
737 ** The file will be opened for both reading and writing. To avoid
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
738 ** a potential security problem, we do not allow the file to have
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
739 ** previously existed. Nor do we allow the file to be a symbolic
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
740 ** link.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
741 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
742 ** If delFlag is true, then make arrangements to automatically delete
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
743 ** the file when it is closed.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
744 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
745 ** On success, write the file handle into *id and return SQLITE_OK.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
746 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
747 ** On failure, return SQLITE_CANTOPEN.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
748 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
749 int sqlite3UnixOpenExclusive(const char *zFilename, OsFile **pId, int delFlag){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
750 int rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
751 unixFile f;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
752
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
753 CRASH_TEST_OVERRIDE(sqlite3CrashOpenExclusive, zFilename, pId, delFlag);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
754 assert( 0==*pId );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
755 f.h = open(zFilename,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
756 O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_LARGEFILE|O_BINARY,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
757 SQLITE_DEFAULT_FILE_PERMISSIONS);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
758 if( f.h<0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
759 return SQLITE_CANTOPEN;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
760 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
761 sqlite3OsEnterMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
762 rc = findLockInfo(f.h, &f.pLock, &f.pOpen);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
763 sqlite3OsLeaveMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
764 if( rc ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
765 close(f.h);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
766 unlink(zFilename);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
767 return SQLITE_NOMEM;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
768 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
769 if( delFlag ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
770 unlink(zFilename);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
771 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
772 TRACE3("OPEN-EX %-3d %s\n", f.h, zFilename);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
773 return allocateUnixFile(&f, pId);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
774 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
775
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
776 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
777 ** Attempt to open a new file for read-only access.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
778 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
779 ** On success, write the file handle into *id and return SQLITE_OK.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
780 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
781 ** On failure, return SQLITE_CANTOPEN.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
782 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
783 int sqlite3UnixOpenReadOnly(const char *zFilename, OsFile **pId){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
784 int rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
785 unixFile f;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
786
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
787 CRASH_TEST_OVERRIDE(sqlite3CrashOpenReadOnly, zFilename, pId, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
788 assert( 0==*pId );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
789 f.h = open(zFilename, O_RDONLY|O_LARGEFILE|O_BINARY);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
790 if( f.h<0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
791 return SQLITE_CANTOPEN;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
792 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
793 sqlite3OsEnterMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
794 rc = findLockInfo(f.h, &f.pLock, &f.pOpen);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
795 sqlite3OsLeaveMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
796 if( rc ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
797 close(f.h);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
798 return SQLITE_NOMEM;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
799 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
800 TRACE3("OPEN-RO %-3d %s\n", f.h, zFilename);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
801 return allocateUnixFile(&f, pId);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
802 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
803
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
804 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
805 ** Attempt to open a file descriptor for the directory that contains a
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
806 ** file. This file descriptor can be used to fsync() the directory
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
807 ** in order to make sure the creation of a new file is actually written
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
808 ** to disk.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
809 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
810 ** This routine is only meaningful for Unix. It is a no-op under
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
811 ** windows since windows does not support hard links.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
812 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
813 ** On success, a handle for a previously open file at *id is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
814 ** updated with the new directory file descriptor and SQLITE_OK is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
815 ** returned.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
816 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
817 ** On failure, the function returns SQLITE_CANTOPEN and leaves
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
818 ** *id unchanged.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
819 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
820 static int unixOpenDirectory(
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
821 OsFile *id,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
822 const char *zDirname
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
823 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
824 unixFile *pFile = (unixFile*)id;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
825 if( pFile==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
826 /* Do not open the directory if the corresponding file is not already
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
827 ** open. */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
828 return SQLITE_CANTOPEN;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
829 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
830 SET_THREADID(pFile);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
831 assert( pFile->dirfd<0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
832 pFile->dirfd = open(zDirname, O_RDONLY|O_BINARY, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
833 if( pFile->dirfd<0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
834 return SQLITE_CANTOPEN;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
835 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
836 TRACE3("OPENDIR %-3d %s\n", pFile->dirfd, zDirname);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
837 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
838 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
839
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
840 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
841 ** If the following global variable points to a string which is the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
842 ** name of a directory, then that directory will be used to store
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
843 ** temporary files.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
844 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
845 ** See also the "PRAGMA temp_store_directory" SQL command.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
846 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
847 char *sqlite3_temp_directory = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
848
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
849 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
850 ** Create a temporary file name in zBuf. zBuf must be big enough to
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
851 ** hold at least SQLITE_TEMPNAME_SIZE characters.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
852 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
853 int sqlite3UnixTempFileName(char *zBuf){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
854 static const char *azDirs[] = {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
855 0,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
856 "/var/tmp",
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
857 "/usr/tmp",
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
858 "/tmp",
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
859 ".",
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
860 };
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
861 static const unsigned char zChars[] =
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
862 "abcdefghijklmnopqrstuvwxyz"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
863 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
864 "0123456789";
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
865 int i, j;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
866 struct stat buf;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
867 const char *zDir = ".";
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
868 azDirs[0] = sqlite3_temp_directory;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
869 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
870 if( azDirs[i]==0 ) continue;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
871 if( stat(azDirs[i], &buf) ) continue;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
872 if( !S_ISDIR(buf.st_mode) ) continue;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
873 if( access(azDirs[i], 07) ) continue;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
874 zDir = azDirs[i];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
875 break;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
876 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
877 do{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
878 sprintf(zBuf, "%s/"TEMP_FILE_PREFIX, zDir);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
879 j = strlen(zBuf);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
880 sqlite3Randomness(15, &zBuf[j]);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
881 for(i=0; i<15; i++, j++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
882 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
883 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
884 zBuf[j] = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
885 }while( access(zBuf,0)==0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
886 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
887 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
888
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
889 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
890 ** Check that a given pathname is a directory and is writable
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
891 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
892 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
893 int sqlite3UnixIsDirWritable(char *zBuf){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
894 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
895 struct stat buf;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
896 if( zBuf==0 ) return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
897 if( zBuf[0]==0 ) return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
898 if( stat(zBuf, &buf) ) return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
899 if( !S_ISDIR(buf.st_mode) ) return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
900 if( access(zBuf, 07) ) return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
901 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
902 return 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
903 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
904
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
905 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
906 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
907 ** Return the number of bytes actually read. Update the offset.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
908 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
909 static int seekAndRead(unixFile *id, void *pBuf, int cnt){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
910 int got;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
911 #ifdef USE_PREAD
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
912 got = pread(id->h, pBuf, cnt, id->offset);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
913 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
914 lseek(id->h, id->offset, SEEK_SET);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
915 got = read(id->h, pBuf, cnt);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
916 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
917 if( got>0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
918 id->offset += got;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
919 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
920 return got;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
921 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
922
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
923 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
924 ** Read data from a file into a buffer. Return SQLITE_OK if all
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
925 ** bytes were read successfully and SQLITE_IOERR if anything goes
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
926 ** wrong.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
927 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
928 static int unixRead(OsFile *id, void *pBuf, int amt){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
929 int got;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
930 assert( id );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
931 SimulateIOError(SQLITE_IOERR);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
932 TIMER_START;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
933 got = seekAndRead((unixFile*)id, pBuf, amt);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
934 TIMER_END;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
935 TRACE5("READ %-3d %5d %7d %d\n", ((unixFile*)id)->h, got,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
936 last_page, TIMER_ELAPSED);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
937 SEEK(0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
938 /* if( got<0 ) got = 0; */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
939 if( got==amt ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
940 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
941 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
942 return SQLITE_IOERR;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
943 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
944 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
945
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
946 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
947 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
948 ** Return the number of bytes actually read. Update the offset.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
949 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
950 static int seekAndWrite(unixFile *id, const void *pBuf, int cnt){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
951 int got;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
952 #ifdef USE_PREAD
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
953 got = pwrite(id->h, pBuf, cnt, id->offset);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
954 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
955 lseek(id->h, id->offset, SEEK_SET);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
956 got = write(id->h, pBuf, cnt);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
957 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
958 if( got>0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
959 id->offset += got;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
960 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
961 return got;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
962 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
963
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
964
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
965 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
966 ** Write data from a buffer into a file. Return SQLITE_OK on success
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
967 ** or some other error code on failure.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
968 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
969 static int unixWrite(OsFile *id, const void *pBuf, int amt){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
970 int wrote = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
971 assert( id );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
972 assert( amt>0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
973 SimulateIOError(SQLITE_IOERR);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
974 SimulateDiskfullError;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
975 TIMER_START;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
976 while( amt>0 && (wrote = seekAndWrite((unixFile*)id, pBuf, amt))>0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
977 amt -= wrote;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
978 pBuf = &((char*)pBuf)[wrote];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
979 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
980 TIMER_END;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
981 TRACE5("WRITE %-3d %5d %7d %d\n", ((unixFile*)id)->h, wrote,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
982 last_page, TIMER_ELAPSED);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
983 SEEK(0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
984 if( amt>0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
985 return SQLITE_FULL;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
986 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
987 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
988 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
989
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
990 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
991 ** Move the read/write pointer in a file.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
992 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
993 static int unixSeek(OsFile *id, i64 offset){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
994 assert( id );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
995 SEEK(offset/1024 + 1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
996 #ifdef SQLITE_TEST
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
997 if( offset ) SimulateDiskfullError
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
998 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
999 ((unixFile*)id)->offset = offset;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1000 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1001 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1002
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1003 #ifdef SQLITE_TEST
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1004 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1005 ** Count the number of fullsyncs and normal syncs. This is used to test
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1006 ** that syncs and fullsyncs are occuring at the right times.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1007 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1008 int sqlite3_sync_count = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1009 int sqlite3_fullsync_count = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1010 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1011
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1012 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1013 ** Use the fdatasync() API only if the HAVE_FDATASYNC macro is defined.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1014 ** Otherwise use fsync() in its place.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1015 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1016 #ifndef HAVE_FDATASYNC
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1017 # define fdatasync fsync
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1018 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1019
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1020 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1021 ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1022 ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1023 ** only available on Mac OS X. But that could change.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1024 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1025 #ifdef F_FULLFSYNC
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1026 # define HAVE_FULLFSYNC 1
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1027 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1028 # define HAVE_FULLFSYNC 0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1029 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1030
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1031
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1032 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1033 ** The fsync() system call does not work as advertised on many
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1034 ** unix systems. The following procedure is an attempt to make
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1035 ** it work better.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1036 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1037 ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1038 ** for testing when we want to run through the test suite quickly.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1039 ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1040 ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1041 ** or power failure will likely corrupt the database file.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1042 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1043 static int full_fsync(int fd, int fullSync, int dataOnly){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1044 int rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1045
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1046 /* Record the number of times that we do a normal fsync() and
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1047 ** FULLSYNC. This is used during testing to verify that this procedure
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1048 ** gets called with the correct arguments.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1049 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1050 #ifdef SQLITE_TEST
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1051 if( fullSync ) sqlite3_fullsync_count++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1052 sqlite3_sync_count++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1053 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1054
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1055 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1056 ** no-op
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1057 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1058 #ifdef SQLITE_NO_SYNC
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1059 rc = SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1060 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1061
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1062 #if HAVE_FULLFSYNC
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1063 if( fullSync ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1064 rc = fcntl(fd, F_FULLFSYNC, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1065 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1066 rc = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1067 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1068 /* If the FULLSYNC failed, try to do a normal fsync() */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1069 if( rc ) rc = fsync(fd);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1070
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1071 #else /* if !defined(F_FULLSYNC) */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1072 if( dataOnly ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1073 rc = fdatasync(fd);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1074 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1075 rc = fsync(fd);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1076 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1077 #endif /* defined(F_FULLFSYNC) */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1078 #endif /* defined(SQLITE_NO_SYNC) */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1079
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1080 return rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1081 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1082
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1083 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1084 ** Make sure all writes to a particular file are committed to disk.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1085 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1086 ** If dataOnly==0 then both the file itself and its metadata (file
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1087 ** size, access time, etc) are synced. If dataOnly!=0 then only the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1088 ** file data is synced.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1089 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1090 ** Under Unix, also make sure that the directory entry for the file
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1091 ** has been created by fsync-ing the directory that contains the file.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1092 ** If we do not do this and we encounter a power failure, the directory
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1093 ** entry for the journal might not exist after we reboot. The next
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1094 ** SQLite to access the file will not know that the journal exists (because
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1095 ** the directory entry for the journal was never created) and the transaction
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1096 ** will not roll back - possibly leading to database corruption.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1097 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1098 static int unixSync(OsFile *id, int dataOnly){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1099 unixFile *pFile = (unixFile*)id;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1100 assert( pFile );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1101 SimulateIOError(SQLITE_IOERR);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1102 TRACE2("SYNC %-3d\n", pFile->h);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1103 if( full_fsync(pFile->h, pFile->fullSync, dataOnly) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1104 return SQLITE_IOERR;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1105 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1106 if( pFile->dirfd>=0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1107 TRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1108 HAVE_FULLFSYNC, pFile->fullSync);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1109 #ifndef SQLITE_DISABLE_DIRSYNC
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1110 /* The directory sync is only attempted if full_fsync is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1111 ** turned off or unavailable. If a full_fsync occurred above,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1112 ** then the directory sync is superfluous.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1113 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1114 if( (!HAVE_FULLFSYNC || !pFile->fullSync) && full_fsync(pFile->dirfd,0,0) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1115 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1116 ** We have received multiple reports of fsync() returning
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1117 ** errors when applied to directories on certain file systems.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1118 ** A failed directory sync is not a big deal. So it seems
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1119 ** better to ignore the error. Ticket #1657
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1120 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1121 /* return SQLITE_IOERR; */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1122 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1123 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1124 close(pFile->dirfd); /* Only need to sync once, so close the directory */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1125 pFile->dirfd = -1; /* when we are done. */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1126 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1127 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1128 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1129
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1130 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1131 ** Sync the directory zDirname. This is a no-op on operating systems other
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1132 ** than UNIX.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1133 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1134 ** This is used to make sure the master journal file has truely been deleted
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1135 ** before making changes to individual journals on a multi-database commit.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1136 ** The F_FULLFSYNC option is not needed here.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1137 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1138 int sqlite3UnixSyncDirectory(const char *zDirname){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1139 #ifdef SQLITE_DISABLE_DIRSYNC
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1140 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1141 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1142 int fd;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1143 int r;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1144 SimulateIOError(SQLITE_IOERR);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1145 fd = open(zDirname, O_RDONLY|O_BINARY, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1146 TRACE3("DIRSYNC %-3d (%s)\n", fd, zDirname);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1147 if( fd<0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1148 return SQLITE_CANTOPEN;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1149 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1150 r = fsync(fd);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1151 close(fd);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1152 return ((r==0)?SQLITE_OK:SQLITE_IOERR);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1153 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1154 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1155
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1156 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1157 ** Truncate an open file to a specified size
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1158 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1159 static int unixTruncate(OsFile *id, i64 nByte){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1160 assert( id );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1161 SimulateIOError(SQLITE_IOERR);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1162 return ftruncate(((unixFile*)id)->h, nByte)==0 ? SQLITE_OK : SQLITE_IOERR;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1163 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1164
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1165 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1166 ** Determine the current size of a file in bytes
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1167 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1168 static int unixFileSize(OsFile *id, i64 *pSize){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1169 struct stat buf;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1170 assert( id );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1171 SimulateIOError(SQLITE_IOERR);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1172 if( fstat(((unixFile*)id)->h, &buf)!=0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1173 return SQLITE_IOERR;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1174 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1175 *pSize = buf.st_size;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1176 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1177 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1178
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1179 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1180 ** This routine checks if there is a RESERVED lock held on the specified
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1181 ** file by this or any other process. If such a lock is held, return
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1182 ** non-zero. If the file is unlocked or holds only SHARED locks, then
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1183 ** return zero.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1184 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1185 static int unixCheckReservedLock(OsFile *id){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1186 int r = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1187 unixFile *pFile = (unixFile*)id;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1188
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1189 assert( pFile );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1190 sqlite3OsEnterMutex(); /* Because pFile->pLock is shared across threads */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1191
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1192 /* Check if a thread in this process holds such a lock */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1193 if( pFile->pLock->locktype>SHARED_LOCK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1194 r = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1195 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1196
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1197 /* Otherwise see if some other process holds it.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1198 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1199 if( !r ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1200 struct flock lock;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1201 lock.l_whence = SEEK_SET;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1202 lock.l_start = RESERVED_BYTE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1203 lock.l_len = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1204 lock.l_type = F_WRLCK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1205 fcntl(pFile->h, F_GETLK, &lock);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1206 if( lock.l_type!=F_UNLCK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1207 r = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1208 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1209 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1210
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1211 sqlite3OsLeaveMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1212 TRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1213
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1214 return r;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1215 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1216
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1217 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1218 ** Lock the file with the lock specified by parameter locktype - one
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1219 ** of the following:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1220 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1221 ** (1) SHARED_LOCK
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1222 ** (2) RESERVED_LOCK
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1223 ** (3) PENDING_LOCK
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1224 ** (4) EXCLUSIVE_LOCK
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1225 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1226 ** Sometimes when requesting one lock state, additional lock states
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1227 ** are inserted in between. The locking might fail on one of the later
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1228 ** transitions leaving the lock state different from what it started but
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1229 ** still short of its goal. The following chart shows the allowed
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1230 ** transitions and the inserted intermediate states:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1231 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1232 ** UNLOCKED -> SHARED
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1233 ** SHARED -> RESERVED
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1234 ** SHARED -> (PENDING) -> EXCLUSIVE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1235 ** RESERVED -> (PENDING) -> EXCLUSIVE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1236 ** PENDING -> EXCLUSIVE
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1237 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1238 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1239 ** routine to lower a locking level.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1240 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1241 static int unixLock(OsFile *id, int locktype){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1242 /* The following describes the implementation of the various locks and
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1243 ** lock transitions in terms of the POSIX advisory shared and exclusive
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1244 ** lock primitives (called read-locks and write-locks below, to avoid
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1245 ** confusion with SQLite lock names). The algorithms are complicated
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1246 ** slightly in order to be compatible with windows systems simultaneously
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1247 ** accessing the same database file, in case that is ever required.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1248 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1249 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1250 ** byte', each single bytes at well known offsets, and the 'shared byte
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1251 ** range', a range of 510 bytes at a well known offset.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1252 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1253 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1254 ** byte'. If this is successful, a random byte from the 'shared byte
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1255 ** range' is read-locked and the lock on the 'pending byte' released.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1256 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1257 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1258 ** A RESERVED lock is implemented by grabbing a write-lock on the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1259 ** 'reserved byte'.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1260 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1261 ** A process may only obtain a PENDING lock after it has obtained a
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1262 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1263 ** on the 'pending byte'. This ensures that no new SHARED locks can be
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1264 ** obtained, but existing SHARED locks are allowed to persist. A process
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1265 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1266 ** This property is used by the algorithm for rolling back a journal file
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1267 ** after a crash.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1268 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1269 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1270 ** implemented by obtaining a write-lock on the entire 'shared byte
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1271 ** range'. Since all other locks require a read-lock on one of the bytes
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1272 ** within this range, this ensures that no other locks are held on the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1273 ** database.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1274 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1275 ** The reason a single byte cannot be used instead of the 'shared byte
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1276 ** range' is that some versions of windows do not support read-locks. By
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1277 ** locking a random byte from a range, concurrent SHARED locks may exist
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1278 ** even if the locking primitive used is always a write-lock.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1279 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1280 int rc = SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1281 unixFile *pFile = (unixFile*)id;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1282 struct lockInfo *pLock = pFile->pLock;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1283 struct flock lock;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1284 int s;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1285
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1286 assert( pFile );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1287 TRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1288 locktypeName(locktype), locktypeName(pFile->locktype),
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1289 locktypeName(pLock->locktype), pLock->cnt , getpid());
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1290
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1291 /* If there is already a lock of this type or more restrictive on the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1292 ** OsFile, do nothing. Don't use the end_lock: exit path, as
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1293 ** sqlite3OsEnterMutex() hasn't been called yet.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1294 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1295 if( pFile->locktype>=locktype ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1296 TRACE3("LOCK %d %s ok (already held)\n", pFile->h,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1297 locktypeName(locktype));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1298 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1299 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1300
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1301 /* Make sure the locking sequence is correct
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1302 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1303 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1304 assert( locktype!=PENDING_LOCK );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1305 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1306
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1307 /* This mutex is needed because pFile->pLock is shared across threads
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1308 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1309 sqlite3OsEnterMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1310
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1311 /* Make sure the current thread owns the pFile.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1312 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1313 rc = transferOwnership(pFile);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1314 if( rc!=SQLITE_OK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1315 sqlite3OsLeaveMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1316 return rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1317 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1318 pLock = pFile->pLock;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1319
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1320 /* If some thread using this PID has a lock via a different OsFile*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1321 ** handle that precludes the requested lock, return BUSY.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1322 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1323 if( (pFile->locktype!=pLock->locktype &&
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1324 (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1325 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1326 rc = SQLITE_BUSY;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1327 goto end_lock;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1328 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1329
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1330 /* If a SHARED lock is requested, and some thread using this PID already
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1331 ** has a SHARED or RESERVED lock, then increment reference counts and
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1332 ** return SQLITE_OK.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1333 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1334 if( locktype==SHARED_LOCK &&
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1335 (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1336 assert( locktype==SHARED_LOCK );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1337 assert( pFile->locktype==0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1338 assert( pLock->cnt>0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1339 pFile->locktype = SHARED_LOCK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1340 pLock->cnt++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1341 pFile->pOpen->nLock++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1342 goto end_lock;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1343 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1344
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1345 lock.l_len = 1L;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1346
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1347 lock.l_whence = SEEK_SET;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1348
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1349 /* A PENDING lock is needed before acquiring a SHARED lock and before
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1350 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1351 ** be released.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1352 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1353 if( locktype==SHARED_LOCK
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1354 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1355 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1356 lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1357 lock.l_start = PENDING_BYTE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1358 s = fcntl(pFile->h, F_SETLK, &lock);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1359 if( s ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1360 rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1361 goto end_lock;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1362 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1363 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1364
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1365
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1366 /* If control gets to this point, then actually go ahead and make
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1367 ** operating system calls for the specified lock.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1368 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1369 if( locktype==SHARED_LOCK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1370 assert( pLock->cnt==0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1371 assert( pLock->locktype==0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1372
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1373 /* Now get the read-lock */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1374 lock.l_start = SHARED_FIRST;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1375 lock.l_len = SHARED_SIZE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1376 s = fcntl(pFile->h, F_SETLK, &lock);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1377
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1378 /* Drop the temporary PENDING lock */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1379 lock.l_start = PENDING_BYTE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1380 lock.l_len = 1L;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1381 lock.l_type = F_UNLCK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1382 if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1383 rc = SQLITE_IOERR; /* This should never happen */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1384 goto end_lock;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1385 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1386 if( s ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1387 rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1388 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1389 pFile->locktype = SHARED_LOCK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1390 pFile->pOpen->nLock++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1391 pLock->cnt = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1392 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1393 }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1394 /* We are trying for an exclusive lock but another thread in this
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1395 ** same process is still holding a shared lock. */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1396 rc = SQLITE_BUSY;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1397 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1398 /* The request was for a RESERVED or EXCLUSIVE lock. It is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1399 ** assumed that there is a SHARED or greater lock on the file
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1400 ** already.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1401 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1402 assert( 0!=pFile->locktype );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1403 lock.l_type = F_WRLCK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1404 switch( locktype ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1405 case RESERVED_LOCK:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1406 lock.l_start = RESERVED_BYTE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1407 break;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1408 case EXCLUSIVE_LOCK:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1409 lock.l_start = SHARED_FIRST;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1410 lock.l_len = SHARED_SIZE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1411 break;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1412 default:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1413 assert(0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1414 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1415 s = fcntl(pFile->h, F_SETLK, &lock);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1416 if( s ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1417 rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1418 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1419 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1420
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1421 if( rc==SQLITE_OK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1422 pFile->locktype = locktype;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1423 pLock->locktype = locktype;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1424 }else if( locktype==EXCLUSIVE_LOCK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1425 pFile->locktype = PENDING_LOCK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1426 pLock->locktype = PENDING_LOCK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1427 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1428
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1429 end_lock:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1430 sqlite3OsLeaveMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1431 TRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1432 rc==SQLITE_OK ? "ok" : "failed");
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1433 return rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1434 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1435
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1436 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1437 ** Lower the locking level on file descriptor pFile to locktype. locktype
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1438 ** must be either NO_LOCK or SHARED_LOCK.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1439 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1440 ** If the locking level of the file descriptor is already at or below
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1441 ** the requested locking level, this routine is a no-op.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1442 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1443 static int unixUnlock(OsFile *id, int locktype){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1444 struct lockInfo *pLock;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1445 struct flock lock;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1446 int rc = SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1447 unixFile *pFile = (unixFile*)id;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1448
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1449 assert( pFile );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1450 TRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1451 pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1452
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1453 assert( locktype<=SHARED_LOCK );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1454 if( pFile->locktype<=locktype ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1455 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1456 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1457 if( CHECK_THREADID(pFile) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1458 return SQLITE_MISUSE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1459 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1460 sqlite3OsEnterMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1461 pLock = pFile->pLock;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1462 assert( pLock->cnt!=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1463 if( pFile->locktype>SHARED_LOCK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1464 assert( pLock->locktype==pFile->locktype );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1465 if( locktype==SHARED_LOCK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1466 lock.l_type = F_RDLCK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1467 lock.l_whence = SEEK_SET;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1468 lock.l_start = SHARED_FIRST;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1469 lock.l_len = SHARED_SIZE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1470 if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1471 /* This should never happen */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1472 rc = SQLITE_IOERR;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1473 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1474 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1475 lock.l_type = F_UNLCK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1476 lock.l_whence = SEEK_SET;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1477 lock.l_start = PENDING_BYTE;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1478 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1479 if( fcntl(pFile->h, F_SETLK, &lock)==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1480 pLock->locktype = SHARED_LOCK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1481 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1482 rc = SQLITE_IOERR; /* This should never happen */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1483 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1484 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1485 if( locktype==NO_LOCK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1486 struct openCnt *pOpen;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1487
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1488 /* Decrement the shared lock counter. Release the lock using an
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1489 ** OS call only when all threads in this same process have released
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1490 ** the lock.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1491 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1492 pLock->cnt--;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1493 if( pLock->cnt==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1494 lock.l_type = F_UNLCK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1495 lock.l_whence = SEEK_SET;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1496 lock.l_start = lock.l_len = 0L;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1497 if( fcntl(pFile->h, F_SETLK, &lock)==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1498 pLock->locktype = NO_LOCK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1499 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1500 rc = SQLITE_IOERR; /* This should never happen */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1501 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1502 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1503
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1504 /* Decrement the count of locks against this same file. When the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1505 ** count reaches zero, close any other file descriptors whose close
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1506 ** was deferred because of outstanding locks.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1507 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1508 pOpen = pFile->pOpen;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1509 pOpen->nLock--;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1510 assert( pOpen->nLock>=0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1511 if( pOpen->nLock==0 && pOpen->nPending>0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1512 int i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1513 for(i=0; i<pOpen->nPending; i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1514 close(pOpen->aPending[i]);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1515 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1516 free(pOpen->aPending);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1517 pOpen->nPending = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1518 pOpen->aPending = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1519 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1520 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1521 sqlite3OsLeaveMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1522 pFile->locktype = locktype;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1523 return rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1524 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1525
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1526 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1527 ** Close a file.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1528 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1529 static int unixClose(OsFile **pId){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1530 unixFile *id = (unixFile*)*pId;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1531
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1532 if( !id ) return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1533 unixUnlock(*pId, NO_LOCK);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1534 if( id->dirfd>=0 ) close(id->dirfd);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1535 id->dirfd = -1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1536 sqlite3OsEnterMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1537
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1538 if( id->pOpen->nLock ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1539 /* If there are outstanding locks, do not actually close the file just
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1540 ** yet because that would clear those locks. Instead, add the file
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1541 ** descriptor to pOpen->aPending. It will be automatically closed when
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1542 ** the last lock is cleared.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1543 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1544 int *aNew;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1545 struct openCnt *pOpen = id->pOpen;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1546 aNew = realloc( pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1547 if( aNew==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1548 /* If a malloc fails, just leak the file descriptor */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1549 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1550 pOpen->aPending = aNew;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1551 pOpen->aPending[pOpen->nPending] = id->h;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1552 pOpen->nPending++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1553 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1554 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1555 /* There are no outstanding locks so we can close the file immediately */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1556 close(id->h);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1557 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1558 releaseLockInfo(id->pLock);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1559 releaseOpenCnt(id->pOpen);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1560
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1561 sqlite3OsLeaveMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1562 id->isOpen = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1563 TRACE2("CLOSE %-3d\n", id->h);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1564 OpenCounter(-1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1565 sqlite3ThreadSafeFree(id);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1566 *pId = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1567 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1568 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1569
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1570 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1571 ** Turn a relative pathname into a full pathname. Return a pointer
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1572 ** to the full pathname stored in space obtained from sqliteMalloc().
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1573 ** The calling function is responsible for freeing this space once it
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1574 ** is no longer needed.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1575 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1576 char *sqlite3UnixFullPathname(const char *zRelative){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1577 char *zFull = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1578 if( zRelative[0]=='/' ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1579 sqlite3SetString(&zFull, zRelative, (char*)0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1580 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1581 char *zBuf = sqliteMalloc(5000);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1582 if( zBuf==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1583 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1584 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1585 zBuf[0] = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1586 sqlite3SetString(&zFull, getcwd(zBuf, 5000), "/", zRelative,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1587 (char*)0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1588 sqliteFree(zBuf);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1589 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1590
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1591 #if 0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1592 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1593 ** Remove "/./" path elements and convert "/A/./" path elements
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1594 ** to just "/".
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1595 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1596 if( zFull ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1597 int i, j;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1598 for(i=j=0; zFull[i]; i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1599 if( zFull[i]=='/' ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1600 if( zFull[i+1]=='/' ) continue;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1601 if( zFull[i+1]=='.' && zFull[i+2]=='/' ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1602 i += 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1603 continue;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1604 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1605 if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1606 while( j>0 && zFull[j-1]!='/' ){ j--; }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1607 i += 3;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1608 continue;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1609 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1610 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1611 zFull[j++] = zFull[i];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1612 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1613 zFull[j] = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1614 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1615 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1616
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1617 return zFull;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1618 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1619
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1620 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1621 ** Change the value of the fullsync flag in the given file descriptor.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1622 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1623 static void unixSetFullSync(OsFile *id, int v){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1624 ((unixFile*)id)->fullSync = v;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1625 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1626
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1627 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1628 ** Return the underlying file handle for an OsFile
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1629 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1630 static int unixFileHandle(OsFile *id){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1631 return ((unixFile*)id)->h;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1632 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1633
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1634 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1635 ** Return an integer that indices the type of lock currently held
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1636 ** by this handle. (Used for testing and analysis only.)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1637 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1638 static int unixLockState(OsFile *id){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1639 return ((unixFile*)id)->locktype;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1640 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1641
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1642 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1643 ** This vector defines all the methods that can operate on an OsFile
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1644 ** for unix.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1645 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1646 static const IoMethod sqlite3UnixIoMethod = {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1647 unixClose,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1648 unixOpenDirectory,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1649 unixRead,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1650 unixWrite,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1651 unixSeek,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1652 unixTruncate,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1653 unixSync,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1654 unixSetFullSync,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1655 unixFileHandle,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1656 unixFileSize,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1657 unixLock,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1658 unixUnlock,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1659 unixLockState,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1660 unixCheckReservedLock,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1661 };
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1662
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1663 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1664 ** Allocate memory for a unixFile. Initialize the new unixFile
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1665 ** to the value given in pInit and return a pointer to the new
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1666 ** OsFile. If we run out of memory, close the file and return NULL.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1667 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1668 static int allocateUnixFile(unixFile *pInit, OsFile **pId){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1669 unixFile *pNew;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1670 pInit->dirfd = -1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1671 pInit->fullSync = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1672 pInit->locktype = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1673 pInit->offset = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1674 SET_THREADID(pInit);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1675 pNew = sqlite3ThreadSafeMalloc( sizeof(unixFile) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1676 if( pNew==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1677 close(pInit->h);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1678 sqlite3OsEnterMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1679 releaseLockInfo(pInit->pLock);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1680 releaseOpenCnt(pInit->pOpen);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1681 sqlite3OsLeaveMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1682 *pId = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1683 return SQLITE_NOMEM;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1684 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1685 *pNew = *pInit;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1686 pNew->pMethod = &sqlite3UnixIoMethod;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1687 *pId = (OsFile*)pNew;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1688 OpenCounter(+1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1689 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1690 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1691 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1692
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1693
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1694 #endif /* SQLITE_OMIT_DISKIO */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1695 /***************************************************************************
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1696 ** Everything above deals with file I/O. Everything that follows deals
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1697 ** with other miscellanous aspects of the operating system interface
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1698 ****************************************************************************/
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1699
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1700
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1701 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1702 ** Get information to seed the random number generator. The seed
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1703 ** is written into the buffer zBuf[256]. The calling function must
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1704 ** supply a sufficiently large buffer.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1705 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1706 int sqlite3UnixRandomSeed(char *zBuf){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1707 /* We have to initialize zBuf to prevent valgrind from reporting
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1708 ** errors. The reports issued by valgrind are incorrect - we would
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1709 ** prefer that the randomness be increased by making use of the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1710 ** uninitialized space in zBuf - but valgrind errors tend to worry
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1711 ** some users. Rather than argue, it seems easier just to initialize
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1712 ** the whole array and silence valgrind, even if that means less randomness
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1713 ** in the random seed.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1714 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1715 ** When testing, initializing zBuf[] to zero is all we do. That means
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1716 ** that we always use the same random number sequence. This makes the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1717 ** tests repeatable.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1718 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1719 memset(zBuf, 0, 256);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1720 #if !defined(SQLITE_TEST)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1721 {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1722 int pid, fd;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1723 fd = open("/dev/urandom", O_RDONLY);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1724 if( fd<0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1725 time_t t;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1726 time(&t);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1727 memcpy(zBuf, &t, sizeof(t));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1728 pid = getpid();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1729 memcpy(&zBuf[sizeof(time_t)], &pid, sizeof(pid));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1730 }else{
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1731 read(fd, zBuf, 256);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1732 close(fd);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1733 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1734 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1735 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1736 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1737 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1738
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1739 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1740 ** Sleep for a little while. Return the amount of time slept.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1741 ** The argument is the number of milliseconds we want to sleep.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1742 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1743 int sqlite3UnixSleep(int ms){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1744 #if defined(HAVE_USLEEP) && HAVE_USLEEP
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1745 usleep(ms*1000);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1746 return ms;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1747 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1748 sleep((ms+999)/1000);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1749 return 1000*((ms+999)/1000);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1750 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1751 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1752
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1753 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1754 ** Static variables used for thread synchronization.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1755 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1756 ** inMutex the nesting depth of the recursive mutex. The thread
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1757 ** holding mutexMain can read this variable at any time.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1758 ** But is must hold mutexAux to change this variable. Other
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1759 ** threads must hold mutexAux to read the variable and can
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1760 ** never write.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1761 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1762 ** mutexOwner The thread id of the thread holding mutexMain. Same
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1763 ** access rules as for inMutex.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1764 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1765 ** mutexOwnerValid True if the value in mutexOwner is valid. The same
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1766 ** access rules apply as for inMutex.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1767 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1768 ** mutexMain The main mutex. Hold this mutex in order to get exclusive
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1769 ** access to SQLite data structures.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1770 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1771 ** mutexAux An auxiliary mutex needed to access variables defined above.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1772 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1773 ** Mutexes are always acquired in this order: mutexMain mutexAux. It
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1774 ** is not necessary to acquire mutexMain in order to get mutexAux - just
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1775 ** do not attempt to acquire them in the reverse order: mutexAux mutexMain.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1776 ** Either get the mutexes with mutexMain first or get mutexAux only.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1777 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1778 ** When running on a platform where the three variables inMutex, mutexOwner,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1779 ** and mutexOwnerValid can be set atomically, the mutexAux is not required.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1780 ** On many systems, all three are 32-bit integers and writing to a 32-bit
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1781 ** integer is atomic. I think. But there are no guarantees. So it seems
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1782 ** safer to protect them using mutexAux.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1783 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1784 static int inMutex = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1785 #ifdef SQLITE_UNIX_THREADS
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1786 static pthread_t mutexOwner; /* Thread holding mutexMain */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1787 static int mutexOwnerValid = 0; /* True if mutexOwner is valid */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1788 static pthread_mutex_t mutexMain = PTHREAD_MUTEX_INITIALIZER; /* The mutex */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1789 static pthread_mutex_t mutexAux = PTHREAD_MUTEX_INITIALIZER; /* Aux mutex */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1790 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1791
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1792 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1793 ** The following pair of routine implement mutual exclusion for
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1794 ** multi-threaded processes. Only a single thread is allowed to
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1795 ** executed code that is surrounded by EnterMutex() and LeaveMutex().
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1796 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1797 ** SQLite uses only a single Mutex. There is not much critical
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1798 ** code and what little there is executes quickly and without blocking.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1799 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1800 ** As of version 3.3.2, this mutex must be recursive.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1801 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1802 void sqlite3UnixEnterMutex(){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1803 #ifdef SQLITE_UNIX_THREADS
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1804 pthread_mutex_lock(&mutexAux);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1805 if( !mutexOwnerValid || !pthread_equal(mutexOwner, pthread_self()) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1806 pthread_mutex_unlock(&mutexAux);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1807 pthread_mutex_lock(&mutexMain);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1808 assert( inMutex==0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1809 assert( !mutexOwnerValid );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1810 pthread_mutex_lock(&mutexAux);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1811 mutexOwner = pthread_self();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1812 mutexOwnerValid = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1813 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1814 inMutex++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1815 pthread_mutex_unlock(&mutexAux);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1816 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1817 inMutex++;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1818 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1819 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1820 void sqlite3UnixLeaveMutex(){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1821 assert( inMutex>0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1822 #ifdef SQLITE_UNIX_THREADS
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1823 pthread_mutex_lock(&mutexAux);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1824 inMutex--;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1825 assert( pthread_equal(mutexOwner, pthread_self()) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1826 if( inMutex==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1827 assert( mutexOwnerValid );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1828 mutexOwnerValid = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1829 pthread_mutex_unlock(&mutexMain);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1830 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1831 pthread_mutex_unlock(&mutexAux);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1832 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1833 inMutex--;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1834 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1835 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1836
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1837 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1838 ** Return TRUE if the mutex is currently held.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1839 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1840 ** If the thisThrd parameter is true, return true only if the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1841 ** calling thread holds the mutex. If the parameter is false, return
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1842 ** true if any thread holds the mutex.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1843 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1844 int sqlite3UnixInMutex(int thisThrd){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1845 #ifdef SQLITE_UNIX_THREADS
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1846 int rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1847 pthread_mutex_lock(&mutexAux);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1848 rc = inMutex>0 && (thisThrd==0 || pthread_equal(mutexOwner,pthread_self()));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1849 pthread_mutex_unlock(&mutexAux);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1850 return rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1851 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1852 return inMutex>0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1853 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1854 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1855
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1856 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1857 ** Remember the number of thread-specific-data blocks allocated.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1858 ** Use this to verify that we are not leaking thread-specific-data.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1859 ** Ticket #1601
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1860 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1861 #ifdef SQLITE_TEST
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1862 int sqlite3_tsd_count = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1863 # ifdef SQLITE_UNIX_THREADS
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1864 static pthread_mutex_t tsd_counter_mutex = PTHREAD_MUTEX_INITIALIZER;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1865 # define TSD_COUNTER(N) \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1866 pthread_mutex_lock(&tsd_counter_mutex); \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1867 sqlite3_tsd_count += N; \
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1868 pthread_mutex_unlock(&tsd_counter_mutex);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1869 # else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1870 # define TSD_COUNTER(N) sqlite3_tsd_count += N
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1871 # endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1872 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1873 # define TSD_COUNTER(N) /* no-op */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1874 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1875
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1876 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1877 ** If called with allocateFlag>0, then return a pointer to thread
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1878 ** specific data for the current thread. Allocate and zero the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1879 ** thread-specific data if it does not already exist.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1880 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1881 ** If called with allocateFlag==0, then check the current thread
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1882 ** specific data. Return it if it exists. If it does not exist,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1883 ** then return NULL.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1884 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1885 ** If called with allocateFlag<0, check to see if the thread specific
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1886 ** data is allocated and is all zero. If it is then deallocate it.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1887 ** Return a pointer to the thread specific data or NULL if it is
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1888 ** unallocated or gets deallocated.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1889 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1890 ThreadData *sqlite3UnixThreadSpecificData(int allocateFlag){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1891 static const ThreadData zeroData = {0}; /* Initializer to silence warnings
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1892 ** from broken compilers */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1893 #ifdef SQLITE_UNIX_THREADS
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1894 static pthread_key_t key;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1895 static int keyInit = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1896 ThreadData *pTsd;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1897
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1898 if( !keyInit ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1899 sqlite3OsEnterMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1900 if( !keyInit ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1901 int rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1902 rc = pthread_key_create(&key, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1903 if( rc ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1904 sqlite3OsLeaveMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1905 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1906 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1907 keyInit = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1908 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1909 sqlite3OsLeaveMutex();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1910 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1911
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1912 pTsd = pthread_getspecific(key);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1913 if( allocateFlag>0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1914 if( pTsd==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1915 if( !sqlite3TestMallocFail() ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1916 pTsd = sqlite3OsMalloc(sizeof(zeroData));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1917 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1918 #ifdef SQLITE_MEMDEBUG
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1919 sqlite3_isFail = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1920 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1921 if( pTsd ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1922 *pTsd = zeroData;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1923 pthread_setspecific(key, pTsd);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1924 TSD_COUNTER(+1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1925 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1926 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1927 }else if( pTsd!=0 && allocateFlag<0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1928 && memcmp(pTsd, &zeroData, sizeof(ThreadData))==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1929 sqlite3OsFree(pTsd);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1930 pthread_setspecific(key, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1931 TSD_COUNTER(-1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1932 pTsd = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1933 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1934 return pTsd;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1935 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1936 static ThreadData *pTsd = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1937 if( allocateFlag>0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1938 if( pTsd==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1939 if( !sqlite3TestMallocFail() ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1940 pTsd = sqlite3OsMalloc( sizeof(zeroData) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1941 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1942 #ifdef SQLITE_MEMDEBUG
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1943 sqlite3_isFail = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1944 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1945 if( pTsd ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1946 *pTsd = zeroData;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1947 TSD_COUNTER(+1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1948 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1949 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1950 }else if( pTsd!=0 && allocateFlag<0
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1951 && memcmp(pTsd, &zeroData, sizeof(ThreadData))==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1952 sqlite3OsFree(pTsd);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1953 TSD_COUNTER(-1);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1954 pTsd = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1955 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1956 return pTsd;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1957 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1958 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1959
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1960 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1961 ** The following variable, if set to a non-zero value, becomes the result
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1962 ** returned from sqlite3OsCurrentTime(). This is used for testing.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1963 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1964 #ifdef SQLITE_TEST
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1965 int sqlite3_current_time = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1966 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1967
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1968 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1969 ** Find the current time (in Universal Coordinated Time). Write the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1970 ** current time and date as a Julian Day number into *prNow and
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1971 ** return 0. Return 1 if the time and date cannot be found.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1972 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1973 int sqlite3UnixCurrentTime(double *prNow){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1974 #ifdef NO_GETTOD
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1975 time_t t;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1976 time(&t);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1977 *prNow = t/86400.0 + 2440587.5;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1978 #else
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1979 struct timeval sNow;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1980 struct timezone sTz; /* Not used */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1981 gettimeofday(&sNow, &sTz);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1982 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1983 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1984 #ifdef SQLITE_TEST
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1985 if( sqlite3_current_time ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1986 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1987 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1988 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1989 return 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1990 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1991
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1992 #endif /* OS_UNIX */