Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(299)

Side by Side Diff: third_party/sqlite/sqlite-src-3080704/src/os_unix.c

Issue 883353008: [sql] Import reference version of SQLite 3.8.7.4. (Closed) Base URL: http://chromium.googlesource.com/chromium/src.git@master
Patch Set: Hold back encoding change which is messing up patch. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 ** 2004 May 22 2 ** 2004 May 22
3 ** 3 **
4 ** The author disclaims copyright to this source code. In place of 4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing: 5 ** a legal notice, here is a blessing:
6 ** 6 **
7 ** May you do good and not evil. 7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others. 8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give. 9 ** May you share freely, never taking more than you give.
10 ** 10 **
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 */ 77 */
78 #ifndef OS_VXWORKS 78 #ifndef OS_VXWORKS
79 # if defined(__RTP__) || defined(_WRS_KERNEL) 79 # if defined(__RTP__) || defined(_WRS_KERNEL)
80 # define OS_VXWORKS 1 80 # define OS_VXWORKS 1
81 # else 81 # else
82 # define OS_VXWORKS 0 82 # define OS_VXWORKS 0
83 # endif 83 # endif
84 #endif 84 #endif
85 85
86 /* 86 /*
87 ** These #defines should enable >2GB file support on Posix if the
88 ** underlying operating system supports it. If the OS lacks
89 ** large file support, these should be no-ops.
90 **
91 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
92 ** on the compiler command line. This is necessary if you are compiling
93 ** on a recent machine (ex: RedHat 7.2) but you want your code to work
94 ** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2
95 ** without this option, LFS is enable. But LFS does not exist in the kernel
96 ** in RedHat 6.0, so the code won't work. Hence, for maximum binary
97 ** portability you should omit LFS.
98 **
99 ** The previous paragraph was written in 2005. (This paragraph is written
100 ** on 2008-11-28.) These days, all Linux kernels support large files, so
101 ** you should probably leave LFS enabled. But some embedded platforms might
102 ** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
103 */
104 #ifndef SQLITE_DISABLE_LFS
105 # define _LARGE_FILE 1
106 # ifndef _FILE_OFFSET_BITS
107 # define _FILE_OFFSET_BITS 64
108 # endif
109 # define _LARGEFILE_SOURCE 1
110 #endif
111
112 /*
113 ** standard include files. 87 ** standard include files.
114 */ 88 */
115 #include <sys/types.h> 89 #include <sys/types.h>
116 #include <sys/stat.h> 90 #include <sys/stat.h>
117 #include <fcntl.h> 91 #include <fcntl.h>
118 #include <unistd.h> 92 #include <unistd.h>
119 #include <time.h> 93 #include <time.h>
120 #include <sys/time.h> 94 #include <sys/time.h>
121 #include <errno.h> 95 #include <errno.h>
122 #ifndef SQLITE_OMIT_WAL 96 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
123 #include <sys/mman.h> 97 # include <sys/mman.h>
124 #endif 98 #endif
125 99
126 #if SQLITE_ENABLE_LOCKING_STYLE 100 #if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS
127 # include <sys/ioctl.h> 101 # include <sys/ioctl.h>
128 # if OS_VXWORKS 102 # if OS_VXWORKS
129 # include <semaphore.h> 103 # include <semaphore.h>
130 # include <limits.h> 104 # include <limits.h>
131 # else 105 # else
132 # include <sys/file.h> 106 # include <sys/file.h>
133 # include <sys/param.h> 107 # include <sys/param.h>
134 # endif 108 # endif
135 #endif /* SQLITE_ENABLE_LOCKING_STYLE */ 109 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
136 110
137 #if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS) 111 #if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
138 # include <sys/mount.h> 112 # include <sys/mount.h>
139 #endif 113 #endif
140 114
115 #ifdef HAVE_UTIME
116 # include <utime.h>
117 #endif
118
141 /* 119 /*
142 ** Allowed values of unixFile.fsFlags 120 ** Allowed values of unixFile.fsFlags
143 */ 121 */
144 #define SQLITE_FSFLAGS_IS_MSDOS 0x1 122 #define SQLITE_FSFLAGS_IS_MSDOS 0x1
145 123
146 /* 124 /*
147 ** If we are to be thread-safe, include the pthreads header and define 125 ** If we are to be thread-safe, include the pthreads header and define
148 ** the SQLITE_UNIX_THREADS macro. 126 ** the SQLITE_UNIX_THREADS macro.
149 */ 127 */
150 #if SQLITE_THREADSAFE 128 #if SQLITE_THREADSAFE
151 # include <pthread.h> 129 # include <pthread.h>
152 # define SQLITE_UNIX_THREADS 1 130 # define SQLITE_UNIX_THREADS 1
153 #endif 131 #endif
154 132
155 /* 133 /*
156 ** Default permissions when creating a new file 134 ** Default permissions when creating a new file
157 */ 135 */
158 #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS 136 #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
159 # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 137 # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
160 #endif 138 #endif
161 139
162 /* 140 /*
163 ** Default permissions when creating auto proxy dir 141 ** Default permissions when creating auto proxy dir
164 */ 142 */
165 #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 143 #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
166 # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755 144 # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
167 #endif 145 #endif
168 146
169 /* 147 /*
170 ** Maximum supported path-length. 148 ** Maximum supported path-length.
171 */ 149 */
172 #define MAX_PATHNAME 512 150 #define MAX_PATHNAME 512
173 151
174 /* 152 /*
(...skipping 20 matching lines...) Expand all
195 UnixUnusedFd *pNext; /* Next unused file descriptor on same file */ 173 UnixUnusedFd *pNext; /* Next unused file descriptor on same file */
196 }; 174 };
197 175
198 /* 176 /*
199 ** The unixFile structure is subclass of sqlite3_file specific to the unix 177 ** The unixFile structure is subclass of sqlite3_file specific to the unix
200 ** VFS implementations. 178 ** VFS implementations.
201 */ 179 */
202 typedef struct unixFile unixFile; 180 typedef struct unixFile unixFile;
203 struct unixFile { 181 struct unixFile {
204 sqlite3_io_methods const *pMethod; /* Always the first entry */ 182 sqlite3_io_methods const *pMethod; /* Always the first entry */
183 sqlite3_vfs *pVfs; /* The VFS that created this unixFile */
205 unixInodeInfo *pInode; /* Info about locks on this inode */ 184 unixInodeInfo *pInode; /* Info about locks on this inode */
206 int h; /* The file descriptor */ 185 int h; /* The file descriptor */
207 unsigned char eFileLock; /* The type of lock held on this fd */ 186 unsigned char eFileLock; /* The type of lock held on this fd */
208 unsigned char ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */ 187 unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
209 int lastErrno; /* The unix errno from last I/O error */ 188 int lastErrno; /* The unix errno from last I/O error */
210 void *lockingContext; /* Locking style specific state */ 189 void *lockingContext; /* Locking style specific state */
211 UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */ 190 UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
212 const char *zPath; /* Name of the file */ 191 const char *zPath; /* Name of the file */
213 unixShm *pShm; /* Shared memory segment information */ 192 unixShm *pShm; /* Shared memory segment information */
214 int szChunk; /* Configured by FCNTL_CHUNK_SIZE */ 193 int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
194 #if SQLITE_MAX_MMAP_SIZE>0
195 int nFetchOut; /* Number of outstanding xFetch refs */
196 sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */
197 sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */
198 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
199 void *pMapRegion; /* Memory mapped region */
200 #endif
201 #ifdef __QNXNTO__
202 int sectorSize; /* Device sector size */
203 int deviceCharacteristics; /* Precomputed device characteristics */
204 #endif
215 #if SQLITE_ENABLE_LOCKING_STYLE 205 #if SQLITE_ENABLE_LOCKING_STYLE
216 int openFlags; /* The flags specified at open() */ 206 int openFlags; /* The flags specified at open() */
217 #endif 207 #endif
218 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) 208 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
219 unsigned fsFlags; /* cached details from statfs() */ 209 unsigned fsFlags; /* cached details from statfs() */
220 #endif 210 #endif
221 #if OS_VXWORKS 211 #if OS_VXWORKS
222 int isDelete; /* Delete on close if true */
223 struct vxworksFileId *pId; /* Unique file ID */ 212 struct vxworksFileId *pId; /* Unique file ID */
224 #endif 213 #endif
225 #ifndef NDEBUG 214 #ifdef SQLITE_DEBUG
226 /* The next group of variables are used to track whether or not the 215 /* The next group of variables are used to track whether or not the
227 ** transaction counter in bytes 24-27 of database files are updated 216 ** transaction counter in bytes 24-27 of database files are updated
228 ** whenever any part of the database changes. An assertion fault will 217 ** whenever any part of the database changes. An assertion fault will
229 ** occur if a file is updated without also updating the transaction 218 ** occur if a file is updated without also updating the transaction
230 ** counter. This test is made to avoid new problems similar to the 219 ** counter. This test is made to avoid new problems similar to the
231 ** one described by ticket #3584. 220 ** one described by ticket #3584.
232 */ 221 */
233 unsigned char transCntrChng; /* True if the transaction counter changed */ 222 unsigned char transCntrChng; /* True if the transaction counter changed */
234 unsigned char dbUpdate; /* True if any part of database file changed */ 223 unsigned char dbUpdate; /* True if any part of database file changed */
235 unsigned char inNormalWrite; /* True if in a normal write operation */ 224 unsigned char inNormalWrite; /* True if in a normal write operation */
225
236 #endif 226 #endif
227
237 #ifdef SQLITE_TEST 228 #ifdef SQLITE_TEST
238 /* In test mode, increase the size of this structure a bit so that 229 /* In test mode, increase the size of this structure a bit so that
239 ** it is larger than the struct CrashFile defined in test6.c. 230 ** it is larger than the struct CrashFile defined in test6.c.
240 */ 231 */
241 char aPadding[32]; 232 char aPadding[32];
242 #endif 233 #endif
243 }; 234 };
244 235
236 /* This variable holds the process id (pid) from when the xRandomness()
237 ** method was called. If xOpen() is called from a different process id,
238 ** indicating that a fork() has occurred, the PRNG will be reset.
239 */
240 static int randomnessPid = 0;
241
245 /* 242 /*
246 ** Allowed values for the unixFile.ctrlFlags bitmask: 243 ** Allowed values for the unixFile.ctrlFlags bitmask:
247 */ 244 */
248 #define UNIXFILE_EXCL 0x01 /* Connections from one process only */ 245 #define UNIXFILE_EXCL 0x01 /* Connections from one process only */
249 #define UNIXFILE_RDONLY 0x02 /* Connection is read only */ 246 #define UNIXFILE_RDONLY 0x02 /* Connection is read only */
250 #define UNIXFILE_DIRSYNC 0x04 /* Directory sync needed */ 247 #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
248 #ifndef SQLITE_DISABLE_DIRSYNC
249 # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */
250 #else
251 # define UNIXFILE_DIRSYNC 0x00
252 #endif
253 #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
254 #define UNIXFILE_DELETE 0x20 /* Delete on close */
255 #define UNIXFILE_URI 0x40 /* Filename might have query parameters */
256 #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */
257 #define UNIXFILE_WARNED 0x0100 /* verifyDbFile() warnings have been issue d */
251 258
252 /* 259 /*
253 ** Include code that is common to all os_*.c files 260 ** Include code that is common to all os_*.c files
254 */ 261 */
255 #include "os_common.h" 262 #include "os_common.h"
256 263
257 /* 264 /*
258 ** Define various macros that are missing from some systems. 265 ** Define various macros that are missing from some systems.
259 */ 266 */
260 #ifndef O_LARGEFILE 267 #ifndef O_LARGEFILE
(...skipping 13 matching lines...) Expand all
274 /* 281 /*
275 ** The threadid macro resolves to the thread-id or to 0. Used for 282 ** The threadid macro resolves to the thread-id or to 0. Used for
276 ** testing and debugging only. 283 ** testing and debugging only.
277 */ 284 */
278 #if SQLITE_THREADSAFE 285 #if SQLITE_THREADSAFE
279 #define threadid pthread_self() 286 #define threadid pthread_self()
280 #else 287 #else
281 #define threadid 0 288 #define threadid 0
282 #endif 289 #endif
283 290
291 /*
292 ** HAVE_MREMAP defaults to true on Linux and false everywhere else.
293 */
294 #if !defined(HAVE_MREMAP)
295 # if defined(__linux__) && defined(_GNU_SOURCE)
296 # define HAVE_MREMAP 1
297 # else
298 # define HAVE_MREMAP 0
299 # endif
300 #endif
301
302 /*
303 ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
304 ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
305 */
306 #ifdef __ANDROID__
307 # define lseek lseek64
308 #endif
309
310 /*
311 ** Different Unix systems declare open() in different ways. Same use
312 ** open(const char*,int,mode_t). Others use open(const char*,int,...).
313 ** The difference is important when using a pointer to the function.
314 **
315 ** The safest way to deal with the problem is to always use this wrapper
316 ** which always has the same well-defined interface.
317 */
318 static int posixOpen(const char *zFile, int flags, int mode){
319 return open(zFile, flags, mode);
320 }
321
322 /*
323 ** On some systems, calls to fchown() will trigger a message in a security
324 ** log if they come from non-root processes. So avoid calling fchown() if
325 ** we are not running as root.
326 */
327 static int posixFchown(int fd, uid_t uid, gid_t gid){
328 #if OS_VXWORKS
329 return 0;
330 #else
331 return geteuid() ? 0 : fchown(fd,uid,gid);
332 #endif
333 }
334
284 /* Forward reference */ 335 /* Forward reference */
285 static int openDirectory(const char*, int*); 336 static int openDirectory(const char*, int*);
337 static int unixGetpagesize(void);
286 338
287 /* 339 /*
288 ** Many system calls are accessed through pointer-to-functions so that 340 ** Many system calls are accessed through pointer-to-functions so that
289 ** they may be overridden at runtime to facilitate fault injection during 341 ** they may be overridden at runtime to facilitate fault injection during
290 ** testing and sandboxing. The following array holds the names and pointers 342 ** testing and sandboxing. The following array holds the names and pointers
291 ** to all overrideable system calls. 343 ** to all overrideable system calls.
292 */ 344 */
293 static struct unix_syscall { 345 static struct unix_syscall {
294 const char *zName; /* Name of the sytem call */ 346 const char *zName; /* Name of the system call */
295 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */ 347 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
296 sqlite3_syscall_ptr pDefault; /* Default value */ 348 sqlite3_syscall_ptr pDefault; /* Default value */
297 } aSyscall[] = { 349 } aSyscall[] = {
298 { "open", (sqlite3_syscall_ptr)open, 0 }, 350 { "open", (sqlite3_syscall_ptr)posixOpen, 0 },
299 #define osOpen ((int(*)(const char*,int,...))aSyscall[0].pCurrent) 351 #define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
300 352
301 { "close", (sqlite3_syscall_ptr)close, 0 }, 353 { "close", (sqlite3_syscall_ptr)close, 0 },
302 #define osClose ((int(*)(int))aSyscall[1].pCurrent) 354 #define osClose ((int(*)(int))aSyscall[1].pCurrent)
303 355
304 { "access", (sqlite3_syscall_ptr)access, 0 }, 356 { "access", (sqlite3_syscall_ptr)access, 0 },
305 #define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent) 357 #define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent)
306 358
307 { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 }, 359 { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 },
308 #define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent) 360 #define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
309 361
(...skipping 16 matching lines...) Expand all
326 378
327 { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 }, 379 { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 },
328 #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent) 380 #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
329 381
330 { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 }, 382 { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 },
331 #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent) 383 #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent)
332 384
333 { "read", (sqlite3_syscall_ptr)read, 0 }, 385 { "read", (sqlite3_syscall_ptr)read, 0 },
334 #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent) 386 #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
335 387
336 #if defined(USE_PREAD) || defined(SQLITE_ENABLE_LOCKING_STYLE) 388 #if defined(USE_PREAD) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
337 { "pread", (sqlite3_syscall_ptr)pread, 0 }, 389 { "pread", (sqlite3_syscall_ptr)pread, 0 },
338 #else 390 #else
339 { "pread", (sqlite3_syscall_ptr)0, 0 }, 391 { "pread", (sqlite3_syscall_ptr)0, 0 },
340 #endif 392 #endif
341 #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent) 393 #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
342 394
343 #if defined(USE_PREAD64) 395 #if defined(USE_PREAD64)
344 { "pread64", (sqlite3_syscall_ptr)pread64, 0 }, 396 { "pread64", (sqlite3_syscall_ptr)pread64, 0 },
345 #else 397 #else
346 { "pread64", (sqlite3_syscall_ptr)0, 0 }, 398 { "pread64", (sqlite3_syscall_ptr)0, 0 },
347 #endif 399 #endif
348 #define osPread64 ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent) 400 #define osPread64 ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)
349 401
350 { "write", (sqlite3_syscall_ptr)write, 0 }, 402 { "write", (sqlite3_syscall_ptr)write, 0 },
351 #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) 403 #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
352 404
353 #if defined(USE_PREAD) || defined(SQLITE_ENABLE_LOCKING_STYLE) 405 #if defined(USE_PREAD) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
354 { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 }, 406 { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 },
355 #else 407 #else
356 { "pwrite", (sqlite3_syscall_ptr)0, 0 }, 408 { "pwrite", (sqlite3_syscall_ptr)0, 0 },
357 #endif 409 #endif
358 #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\ 410 #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\
359 aSyscall[12].pCurrent) 411 aSyscall[12].pCurrent)
360 412
361 #if defined(USE_PREAD64) 413 #if defined(USE_PREAD64)
362 { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 }, 414 { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 },
363 #else 415 #else
364 { "pwrite64", (sqlite3_syscall_ptr)0, 0 }, 416 { "pwrite64", (sqlite3_syscall_ptr)0, 0 },
365 #endif 417 #endif
366 #define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off_t))\ 418 #define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off_t))\
367 aSyscall[13].pCurrent) 419 aSyscall[13].pCurrent)
368 420
369 #if SQLITE_ENABLE_LOCKING_STYLE
370 { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 }, 421 { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 },
371 #else
372 { "fchmod", (sqlite3_syscall_ptr)0, 0 },
373 #endif
374 #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent) 422 #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent)
375 423
376 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE 424 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
377 { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 }, 425 { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 },
378 #else 426 #else
379 { "fallocate", (sqlite3_syscall_ptr)0, 0 }, 427 { "fallocate", (sqlite3_syscall_ptr)0, 0 },
380 #endif 428 #endif
381 #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent) 429 #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
382 430
383 { "unlink", (sqlite3_syscall_ptr)unlink, 0 }, 431 { "unlink", (sqlite3_syscall_ptr)unlink, 0 },
384 #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent) 432 #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent)
385 433
386 { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 }, 434 { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 },
387 #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent) 435 #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
388 436
437 { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 },
438 #define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
439
440 { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 },
441 #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent)
442
443 { "fchown", (sqlite3_syscall_ptr)posixFchown, 0 },
444 #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
445
446 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
447 { "mmap", (sqlite3_syscall_ptr)mmap, 0 },
448 #define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[21].pCurrent)
449
450 { "munmap", (sqlite3_syscall_ptr)munmap, 0 },
451 #define osMunmap ((void*(*)(void*,size_t))aSyscall[22].pCurrent)
452
453 #if HAVE_MREMAP
454 { "mremap", (sqlite3_syscall_ptr)mremap, 0 },
455 #else
456 { "mremap", (sqlite3_syscall_ptr)0, 0 },
457 #endif
458 #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
459 { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 },
460 #define osGetpagesize ((int(*)(void))aSyscall[24].pCurrent)
461
462 #endif
463
389 }; /* End of the overrideable system calls */ 464 }; /* End of the overrideable system calls */
390 465
391 /* 466 /*
392 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the 467 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
393 ** "unix" VFSes. Return SQLITE_OK opon successfully updating the 468 ** "unix" VFSes. Return SQLITE_OK opon successfully updating the
394 ** system call pointer, or SQLITE_NOTFOUND if there is no configurable 469 ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
395 ** system call named zName. 470 ** system call named zName.
396 */ 471 */
397 static int unixSetSystemCall( 472 static int unixSetSystemCall(
398 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */ 473 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
465 if( strcmp(zName, aSyscall[i].zName)==0 ) break; 540 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
466 } 541 }
467 } 542 }
468 for(i++; i<ArraySize(aSyscall); i++){ 543 for(i++; i<ArraySize(aSyscall); i++){
469 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName; 544 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
470 } 545 }
471 return 0; 546 return 0;
472 } 547 }
473 548
474 /* 549 /*
475 ** Retry open() calls that fail due to EINTR 550 ** Do not accept any file descriptor less than this value, in order to avoid
551 ** opening database file using file descriptors that are commonly used for
552 ** standard input, output, and error.
476 */ 553 */
477 static int robust_open(const char *z, int f, int m){ 554 #ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR
478 int rc; 555 # define SQLITE_MINIMUM_FILE_DESCRIPTOR 3
479 do{ rc = osOpen(z,f,m); }while( rc<0 && errno==EINTR ); 556 #endif
480 return rc; 557
558 /*
559 ** Invoke open(). Do so multiple times, until it either succeeds or
560 ** fails for some reason other than EINTR.
561 **
562 ** If the file creation mode "m" is 0 then set it to the default for
563 ** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
564 ** 0644) as modified by the system umask. If m is not 0, then
565 ** make the file creation mode be exactly m ignoring the umask.
566 **
567 ** The m parameter will be non-zero only when creating -wal, -journal,
568 ** and -shm files. We want those files to have *exactly* the same
569 ** permissions as their original database, unadulterated by the umask.
570 ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
571 ** transaction crashes and leaves behind hot journals, then any
572 ** process that is able to write to the database will also be able to
573 ** recover the hot journals.
574 */
575 static int robust_open(const char *z, int f, mode_t m){
576 int fd;
577 mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
578 while(1){
579 #if defined(O_CLOEXEC)
580 fd = osOpen(z,f|O_CLOEXEC,m2);
581 #else
582 fd = osOpen(z,f,m2);
583 #endif
584 if( fd<0 ){
585 if( errno==EINTR ) continue;
586 break;
587 }
588 if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break;
589 osClose(fd);
590 sqlite3_log(SQLITE_WARNING,
591 "attempt to open \"%s\" as file descriptor %d", z, fd);
592 fd = -1;
593 if( osOpen("/dev/null", f, m)<0 ) break;
594 }
595 if( fd>=0 ){
596 if( m!=0 ){
597 struct stat statbuf;
598 if( osFstat(fd, &statbuf)==0
599 && statbuf.st_size==0
600 && (statbuf.st_mode&0777)!=m
601 ){
602 osFchmod(fd, m);
603 }
604 }
605 #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
606 osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
607 #endif
608 }
609 return fd;
481 } 610 }
482 611
483 /* 612 /*
484 ** Helper functions to obtain and relinquish the global mutex. The 613 ** Helper functions to obtain and relinquish the global mutex. The
485 ** global mutex is used to protect the unixInodeInfo and 614 ** global mutex is used to protect the unixInodeInfo and
486 ** vxworksFileId objects used by this file, all of which may be 615 ** vxworksFileId objects used by this file, all of which may be
487 ** shared by multiple threads. 616 ** shared by multiple threads.
488 ** 617 **
489 ** Function unixMutexHeld() is used to assert() that the global mutex 618 ** Function unixMutexHeld() is used to assert() that the global mutex
490 ** is held when required. This function is only used as part of assert() 619 ** is held when required. This function is only used as part of assert()
491 ** statements. e.g. 620 ** statements. e.g.
492 ** 621 **
493 ** unixEnterMutex() 622 ** unixEnterMutex()
494 ** assert( unixMutexHeld() ); 623 ** assert( unixMutexHeld() );
495 ** unixEnterLeave() 624 ** unixEnterLeave()
496 */ 625 */
497 static void unixEnterMutex(void){ 626 static void unixEnterMutex(void){
498 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); 627 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
499 } 628 }
500 static void unixLeaveMutex(void){ 629 static void unixLeaveMutex(void){
501 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); 630 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
502 } 631 }
503 #ifdef SQLITE_DEBUG 632 #ifdef SQLITE_DEBUG
504 static int unixMutexHeld(void) { 633 static int unixMutexHeld(void) {
505 return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); 634 return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
506 } 635 }
507 #endif 636 #endif
508 637
509 638
510 #ifdef SQLITE_DEBUG 639 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
511 /* 640 /*
512 ** Helper function for printing out trace information from debugging 641 ** Helper function for printing out trace information from debugging
513 ** binaries. This returns the string represetation of the supplied 642 ** binaries. This returns the string representation of the supplied
514 ** integer lock-type. 643 ** integer lock-type.
515 */ 644 */
516 static const char *azFileLock(int eFileLock){ 645 static const char *azFileLock(int eFileLock){
517 switch( eFileLock ){ 646 switch( eFileLock ){
518 case NO_LOCK: return "NONE"; 647 case NO_LOCK: return "NONE";
519 case SHARED_LOCK: return "SHARED"; 648 case SHARED_LOCK: return "SHARED";
520 case RESERVED_LOCK: return "RESERVED"; 649 case RESERVED_LOCK: return "RESERVED";
521 case PENDING_LOCK: return "PENDING"; 650 case PENDING_LOCK: return "PENDING";
522 case EXCLUSIVE_LOCK: return "EXCLUSIVE"; 651 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
523 } 652 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
580 } 709 }
581 errno = savedErrno; 710 errno = savedErrno;
582 return s; 711 return s;
583 } 712 }
584 #undef osFcntl 713 #undef osFcntl
585 #define osFcntl lockTrace 714 #define osFcntl lockTrace
586 #endif /* SQLITE_LOCK_TRACE */ 715 #endif /* SQLITE_LOCK_TRACE */
587 716
588 /* 717 /*
589 ** Retry ftruncate() calls that fail due to EINTR 718 ** Retry ftruncate() calls that fail due to EINTR
719 **
720 ** All calls to ftruncate() within this file should be made through this wrapper .
721 ** On the Android platform, bypassing the logic below could lead to a corrupt
722 ** database.
590 */ 723 */
591 static int robust_ftruncate(int h, sqlite3_int64 sz){ 724 static int robust_ftruncate(int h, sqlite3_int64 sz){
592 int rc; 725 int rc;
726 #ifdef __ANDROID__
727 /* On Android, ftruncate() always uses 32-bit offsets, even if
728 ** _FILE_OFFSET_BITS=64 is defined. This means it is unsafe to attempt to
729 ** truncate a file to any size larger than 2GiB. Silently ignore any
730 ** such attempts. */
731 if( sz>(sqlite3_int64)0x7FFFFFFF ){
732 rc = SQLITE_OK;
733 }else
734 #endif
593 do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR ); 735 do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
594 return rc; 736 return rc;
595 } 737 }
596 738
597 /* 739 /*
598 ** This routine translates a standard POSIX errno code into something 740 ** This routine translates a standard POSIX errno code into something
599 ** useful to the clients of the sqlite3 functions. Specifically, it is 741 ** useful to the clients of the sqlite3 functions. Specifically, it is
600 ** intended to translate a variety of "try again" errors into SQLITE_BUSY 742 ** intended to translate a variety of "try again" errors into SQLITE_BUSY
601 ** and a variety of "please close the file descriptor NOW" errors into 743 ** and a variety of "please close the file descriptor NOW" errors into
602 ** SQLITE_IOERR 744 ** SQLITE_IOERR
(...skipping 24 matching lines...) Expand all
627 case EBUSY: 769 case EBUSY:
628 case EINTR: 770 case EINTR:
629 case ENOLCK: 771 case ENOLCK:
630 /* random NFS retry error, unless during file system support 772 /* random NFS retry error, unless during file system support
631 * introspection, in which it actually means what it says */ 773 * introspection, in which it actually means what it says */
632 return SQLITE_BUSY; 774 return SQLITE_BUSY;
633 775
634 case EACCES: 776 case EACCES:
635 /* EACCES is like EAGAIN during locking operations, but not any other time*/ 777 /* EACCES is like EAGAIN during locking operations, but not any other time*/
636 if( (sqliteIOErr == SQLITE_IOERR_LOCK) || 778 if( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
637 » (sqliteIOErr == SQLITE_IOERR_UNLOCK) || 779 (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
638 » (sqliteIOErr == SQLITE_IOERR_RDLOCK) || 780 (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
639 » (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){ 781 (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
640 return SQLITE_BUSY; 782 return SQLITE_BUSY;
641 } 783 }
642 /* else fall through */ 784 /* else fall through */
643 case EPERM: 785 case EPERM:
644 return SQLITE_PERM; 786 return SQLITE_PERM;
645 787
646 /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
647 ** this module never makes such a call. And the code in SQLite itself
648 ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons
649 ** this case is also commented out. If the system does set errno to EDEADLK,
650 ** the default SQLITE_IOERR_XXX code will be returned. */
651 #if 0
652 case EDEADLK:
653 return SQLITE_IOERR_BLOCKED;
654 #endif
655
656 #if EOPNOTSUPP!=ENOTSUP 788 #if EOPNOTSUPP!=ENOTSUP
657 case EOPNOTSUPP: 789 case EOPNOTSUPP:
658 /* something went terribly awry, unless during file system support 790 /* something went terribly awry, unless during file system support
659 * introspection, in which it actually means what it says */ 791 * introspection, in which it actually means what it says */
660 #endif 792 #endif
661 #ifdef ENOTSUP 793 #ifdef ENOTSUP
662 case ENOTSUP: 794 case ENOTSUP:
663 /* invalid fd, unless during file system support introspection, in which 795 /* invalid fd, unless during file system support introspection, in which
664 * it actually means what it says */ 796 * it actually means what it says */
665 #endif 797 #endif
666 case EIO: 798 case EIO:
667 case EBADF: 799 case EBADF:
668 case EINVAL: 800 case EINVAL:
669 case ENOTCONN: 801 case ENOTCONN:
670 case ENODEV: 802 case ENODEV:
671 case ENXIO: 803 case ENXIO:
672 case ENOENT: 804 case ENOENT:
805 #ifdef ESTALE /* ESTALE is not defined on Interix systems */
673 case ESTALE: 806 case ESTALE:
807 #endif
674 case ENOSYS: 808 case ENOSYS:
675 /* these should force the client to close the file and reconnect */ 809 /* these should force the client to close the file and reconnect */
676 810
677 default: 811 default:
678 return sqliteIOErr; 812 return sqliteIOErr;
679 } 813 }
680 } 814 }
681 815
682 816
683
684 /****************************************************************************** 817 /******************************************************************************
685 ****************** Begin Unique File ID Utility Used By VxWorks *************** 818 ****************** Begin Unique File ID Utility Used By VxWorks ***************
686 ** 819 **
687 ** On most versions of unix, we can get a unique ID for a file by concatenating 820 ** On most versions of unix, we can get a unique ID for a file by concatenating
688 ** the device number and the inode number. But this does not work on VxWorks. 821 ** the device number and the inode number. But this does not work on VxWorks.
689 ** On VxWorks, a unique file id must be based on the canonical filename. 822 ** On VxWorks, a unique file id must be based on the canonical filename.
690 ** 823 **
691 ** A pointer to an instance of the following structure can be used as a 824 ** A pointer to an instance of the following structure can be used as a
692 ** unique file ID in VxWorks. Each instance of this structure contains 825 ** unique file ID in VxWorks. Each instance of this structure contains
693 ** a copy of the canonical filename. There is also a reference count. 826 ** a copy of the canonical filename. There is also a reference count.
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
935 struct unixFileId fileId; /* The lookup key */ 1068 struct unixFileId fileId; /* The lookup key */
936 int nShared; /* Number of SHARED locks held */ 1069 int nShared; /* Number of SHARED locks held */
937 unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ 1070 unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
938 unsigned char bProcessLock; /* An exclusive process lock is held */ 1071 unsigned char bProcessLock; /* An exclusive process lock is held */
939 int nRef; /* Number of pointers to this structure */ 1072 int nRef; /* Number of pointers to this structure */
940 unixShmNode *pShmNode; /* Shared memory associated with this inode */ 1073 unixShmNode *pShmNode; /* Shared memory associated with this inode */
941 int nLock; /* Number of outstanding file locks */ 1074 int nLock; /* Number of outstanding file locks */
942 UnixUnusedFd *pUnused; /* Unused file descriptors to close */ 1075 UnixUnusedFd *pUnused; /* Unused file descriptors to close */
943 unixInodeInfo *pNext; /* List of all unixInodeInfo objects */ 1076 unixInodeInfo *pNext; /* List of all unixInodeInfo objects */
944 unixInodeInfo *pPrev; /* .... doubly linked */ 1077 unixInodeInfo *pPrev; /* .... doubly linked */
945 #if defined(SQLITE_ENABLE_LOCKING_STYLE) 1078 #if SQLITE_ENABLE_LOCKING_STYLE
946 unsigned long long sharedByte; /* for AFP simulated shared lock */ 1079 unsigned long long sharedByte; /* for AFP simulated shared lock */
947 #endif 1080 #endif
948 #if OS_VXWORKS 1081 #if OS_VXWORKS
949 sem_t *pSem; /* Named POSIX semaphore */ 1082 sem_t *pSem; /* Named POSIX semaphore */
950 char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */ 1083 char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */
951 #endif 1084 #endif
952 }; 1085 };
953 1086
954 /* 1087 /*
955 ** A lists of all unixInodeInfo objects. 1088 ** A lists of all unixInodeInfo objects.
956 */ 1089 */
957 static unixInodeInfo *inodeList = 0; 1090 static unixInodeInfo *inodeList = 0;
958 1091
959 /* 1092 /*
960 ** 1093 **
961 ** This function - unixLogError_x(), is only ever called via the macro 1094 ** This function - unixLogError_x(), is only ever called via the macro
962 ** unixLogError(). 1095 ** unixLogError().
963 ** 1096 **
964 ** It is invoked after an error occurs in an OS function and errno has been 1097 ** It is invoked after an error occurs in an OS function and errno has been
965 ** set. It logs a message using sqlite3_log() containing the current value of 1098 ** set. It logs a message using sqlite3_log() containing the current value of
966 ** errno and, if possible, the human-readable equivalent from strerror() or 1099 ** errno and, if possible, the human-readable equivalent from strerror() or
967 ** strerror_r(). 1100 ** strerror_r().
968 ** 1101 **
969 ** The first argument passed to the macro should be the error code that 1102 ** The first argument passed to the macro should be the error code that
970 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). 1103 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
971 ** The two subsequent arguments should be the name of the OS function that 1104 ** The two subsequent arguments should be the name of the OS function that
972 ** failed (e.g. "unlink", "open") and the the associated file-system path, 1105 ** failed (e.g. "unlink", "open") and the associated file-system path,
973 ** if any. 1106 ** if any.
974 */ 1107 */
975 #define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__) 1108 #define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__)
976 static int unixLogErrorAtLine( 1109 static int unixLogErrorAtLine(
977 int errcode, /* SQLite error code */ 1110 int errcode, /* SQLite error code */
978 const char *zFunc, /* Name of OS function that failed */ 1111 const char *zFunc, /* Name of OS function that failed */
979 const char *zPath, /* File path associated with error */ 1112 const char *zPath, /* File path associated with error */
980 int iLine /* Source line number where error occurred */ 1113 int iLine /* Source line number where error occurred */
981 ){ 1114 ){
982 char *zErr; /* Message from strerror() or equivalent */ 1115 char *zErr; /* Message from strerror() or equivalent */
983 int iErrno = errno; /* Saved syscall error number */ 1116 int iErrno = errno; /* Saved syscall error number */
984 1117
985 /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use 1118 /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
986 ** the strerror() function to obtain the human-readable error message 1119 ** the strerror() function to obtain the human-readable error message
987 ** equivalent to errno. Otherwise, use strerror_r(). 1120 ** equivalent to errno. Otherwise, use strerror_r().
988 */ 1121 */
989 #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R) 1122 #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
990 char aErr[80]; 1123 char aErr[80];
991 memset(aErr, 0, sizeof(aErr)); 1124 memset(aErr, 0, sizeof(aErr));
992 zErr = aErr; 1125 zErr = aErr;
993 1126
994 /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined, 1127 /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
995 ** assume that the system provides the the GNU version of strerror_r() that 1128 ** assume that the system provides the GNU version of strerror_r() that
996 ** returns a pointer to a buffer containing the error message. That pointer 1129 ** returns a pointer to a buffer containing the error message. That pointer
997 ** may point to aErr[], or it may point to some static storage somewhere. 1130 ** may point to aErr[], or it may point to some static storage somewhere.
998 ** Otherwise, assume that the system provides the POSIX version of 1131 ** Otherwise, assume that the system provides the POSIX version of
999 ** strerror_r(), which always writes an error message into aErr[]. 1132 ** strerror_r(), which always writes an error message into aErr[].
1000 ** 1133 **
1001 ** If the code incorrectly assumes that it is the POSIX version that is 1134 ** If the code incorrectly assumes that it is the POSIX version that is
1002 ** available, the error message will often be an empty string. Not a 1135 ** available, the error message will often be an empty string. Not a
1003 ** huge problem. Incorrectly concluding that the GNU version is available 1136 ** huge problem. Incorrectly concluding that the GNU version is available
1004 ** could lead to a segfault though. 1137 ** could lead to a segfault though.
1005 */ 1138 */
1006 #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU) 1139 #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
1007 zErr = 1140 zErr =
1008 # endif 1141 # endif
1009 strerror_r(iErrno, aErr, sizeof(aErr)-1); 1142 strerror_r(iErrno, aErr, sizeof(aErr)-1);
1010 1143
1011 #elif SQLITE_THREADSAFE 1144 #elif SQLITE_THREADSAFE
1012 /* This is a threadsafe build, but strerror_r() is not available. */ 1145 /* This is a threadsafe build, but strerror_r() is not available. */
1013 zErr = ""; 1146 zErr = "";
1014 #else 1147 #else
1015 /* Non-threadsafe build, use strerror(). */ 1148 /* Non-threadsafe build, use strerror(). */
1016 zErr = strerror(iErrno); 1149 zErr = strerror(iErrno);
1017 #endif 1150 #endif
1018 1151
1019 assert( errcode!=SQLITE_OK );
1020 if( zPath==0 ) zPath = ""; 1152 if( zPath==0 ) zPath = "";
1021 sqlite3_log(errcode, 1153 sqlite3_log(errcode,
1022 "os_unix.c:%d: (%d) %s(%s) - %s", 1154 "os_unix.c:%d: (%d) %s(%s) - %s",
1023 iLine, iErrno, zFunc, zPath, zErr 1155 iLine, iErrno, zFunc, zPath, zErr
1024 ); 1156 );
1025 1157
1026 return errcode; 1158 return errcode;
1027 } 1159 }
1028 1160
1029 /* 1161 /*
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1174 pInode->pPrev = 0; 1306 pInode->pPrev = 0;
1175 if( inodeList ) inodeList->pPrev = pInode; 1307 if( inodeList ) inodeList->pPrev = pInode;
1176 inodeList = pInode; 1308 inodeList = pInode;
1177 }else{ 1309 }else{
1178 pInode->nRef++; 1310 pInode->nRef++;
1179 } 1311 }
1180 *ppInode = pInode; 1312 *ppInode = pInode;
1181 return SQLITE_OK; 1313 return SQLITE_OK;
1182 } 1314 }
1183 1315
1316 /*
1317 ** Return TRUE if pFile has been renamed or unlinked since it was first opened.
1318 */
1319 static int fileHasMoved(unixFile *pFile){
1320 #if OS_VXWORKS
1321 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
1322 #else
1323 struct stat buf;
1324 return pFile->pInode!=0 &&
1325 (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino);
1326 #endif
1327 }
1328
1329
1330 /*
1331 ** Check a unixFile that is a database. Verify the following:
1332 **
1333 ** (1) There is exactly one hard link on the file
1334 ** (2) The file is not a symbolic link
1335 ** (3) The file has not been renamed or unlinked
1336 **
1337 ** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right.
1338 */
1339 static void verifyDbFile(unixFile *pFile){
1340 struct stat buf;
1341 int rc;
1342 if( pFile->ctrlFlags & UNIXFILE_WARNED ){
1343 /* One or more of the following warnings have already been issued. Do not
1344 ** repeat them so as not to clutter the error log */
1345 return;
1346 }
1347 rc = osFstat(pFile->h, &buf);
1348 if( rc!=0 ){
1349 sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
1350 pFile->ctrlFlags |= UNIXFILE_WARNED;
1351 return;
1352 }
1353 if( buf.st_nlink==0 && (pFile->ctrlFlags & UNIXFILE_DELETE)==0 ){
1354 sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
1355 pFile->ctrlFlags |= UNIXFILE_WARNED;
1356 return;
1357 }
1358 if( buf.st_nlink>1 ){
1359 sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
1360 pFile->ctrlFlags |= UNIXFILE_WARNED;
1361 return;
1362 }
1363 if( fileHasMoved(pFile) ){
1364 sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
1365 pFile->ctrlFlags |= UNIXFILE_WARNED;
1366 return;
1367 }
1368 }
1369
1184 1370
1185 /* 1371 /*
1186 ** This routine checks if there is a RESERVED lock held on the specified 1372 ** This routine checks if there is a RESERVED lock held on the specified
1187 ** file by this or any other process. If such a lock is held, set *pResOut 1373 ** file by this or any other process. If such a lock is held, set *pResOut
1188 ** to a non-zero value otherwise *pResOut is set to zero. The return value 1374 ** to a non-zero value otherwise *pResOut is set to zero. The return value
1189 ** is set to SQLITE_OK unless an I/O error occurs during lock checking. 1375 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
1190 */ 1376 */
1191 static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ 1377 static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
1192 int rc = SQLITE_OK; 1378 int rc = SQLITE_OK;
1193 int reserved = 0; 1379 int reserved = 0;
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
1333 ** within this range, this ensures that no other locks are held on the 1519 ** within this range, this ensures that no other locks are held on the
1334 ** database. 1520 ** database.
1335 ** 1521 **
1336 ** The reason a single byte cannot be used instead of the 'shared byte 1522 ** The reason a single byte cannot be used instead of the 'shared byte
1337 ** range' is that some versions of windows do not support read-locks. By 1523 ** range' is that some versions of windows do not support read-locks. By
1338 ** locking a random byte from a range, concurrent SHARED locks may exist 1524 ** locking a random byte from a range, concurrent SHARED locks may exist
1339 ** even if the locking primitive used is always a write-lock. 1525 ** even if the locking primitive used is always a write-lock.
1340 */ 1526 */
1341 int rc = SQLITE_OK; 1527 int rc = SQLITE_OK;
1342 unixFile *pFile = (unixFile*)id; 1528 unixFile *pFile = (unixFile*)id;
1343 unixInodeInfo *pInode = pFile->pInode; 1529 unixInodeInfo *pInode;
1344 struct flock lock; 1530 struct flock lock;
1345 int tErrno = 0; 1531 int tErrno = 0;
1346 1532
1347 assert( pFile ); 1533 assert( pFile );
1348 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, 1534 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
1349 azFileLock(eFileLock), azFileLock(pFile->eFileLock), 1535 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
1350 azFileLock(pInode->eFileLock), pInode->nShared , getpid())); 1536 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared , getpid()));
1351 1537
1352 /* If there is already a lock of this type or more restrictive on the 1538 /* If there is already a lock of this type or more restrictive on the
1353 ** unixFile, do nothing. Don't use the end_lock: exit path, as 1539 ** unixFile, do nothing. Don't use the end_lock: exit path, as
1354 ** unixEnterMutex() hasn't been called yet. 1540 ** unixEnterMutex() hasn't been called yet.
1355 */ 1541 */
1356 if( pFile->eFileLock>=eFileLock ){ 1542 if( pFile->eFileLock>=eFileLock ){
1357 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h, 1543 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
1358 azFileLock(eFileLock))); 1544 azFileLock(eFileLock)));
1359 return SQLITE_OK; 1545 return SQLITE_OK;
1360 } 1546 }
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
1481 if( unixFileLock(pFile, &lock) ){ 1667 if( unixFileLock(pFile, &lock) ){
1482 tErrno = errno; 1668 tErrno = errno;
1483 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); 1669 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1484 if( rc!=SQLITE_BUSY ){ 1670 if( rc!=SQLITE_BUSY ){
1485 pFile->lastErrno = tErrno; 1671 pFile->lastErrno = tErrno;
1486 } 1672 }
1487 } 1673 }
1488 } 1674 }
1489 1675
1490 1676
1491 #ifndef NDEBUG 1677 #ifdef SQLITE_DEBUG
1492 /* Set up the transaction-counter change checking flags when 1678 /* Set up the transaction-counter change checking flags when
1493 ** transitioning from a SHARED to a RESERVED lock. The change 1679 ** transitioning from a SHARED to a RESERVED lock. The change
1494 ** from SHARED to RESERVED marks the beginning of a normal 1680 ** from SHARED to RESERVED marks the beginning of a normal
1495 ** write operation (not a hot journal rollback). 1681 ** write operation (not a hot journal rollback).
1496 */ 1682 */
1497 if( rc==SQLITE_OK 1683 if( rc==SQLITE_OK
1498 && pFile->eFileLock<=SHARED_LOCK 1684 && pFile->eFileLock<=SHARED_LOCK
1499 && eFileLock==RESERVED_LOCK 1685 && eFileLock==RESERVED_LOCK
1500 ){ 1686 ){
1501 pFile->transCntrChng = 0; 1687 pFile->transCntrChng = 0;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1544 ** the byte range is divided into 2 parts and the first part is unlocked then 1730 ** the byte range is divided into 2 parts and the first part is unlocked then
1545 ** set to a read lock, then the other part is simply unlocked. This works 1731 ** set to a read lock, then the other part is simply unlocked. This works
1546 ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to 1732 ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
1547 ** remove the write lock on a region when a read lock is set. 1733 ** remove the write lock on a region when a read lock is set.
1548 */ 1734 */
1549 static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ 1735 static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
1550 unixFile *pFile = (unixFile*)id; 1736 unixFile *pFile = (unixFile*)id;
1551 unixInodeInfo *pInode; 1737 unixInodeInfo *pInode;
1552 struct flock lock; 1738 struct flock lock;
1553 int rc = SQLITE_OK; 1739 int rc = SQLITE_OK;
1554 int h;
1555 1740
1556 assert( pFile ); 1741 assert( pFile );
1557 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock, 1742 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
1558 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, 1743 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
1559 getpid())); 1744 getpid()));
1560 1745
1561 assert( eFileLock<=SHARED_LOCK ); 1746 assert( eFileLock<=SHARED_LOCK );
1562 if( pFile->eFileLock<=eFileLock ){ 1747 if( pFile->eFileLock<=eFileLock ){
1563 return SQLITE_OK; 1748 return SQLITE_OK;
1564 } 1749 }
1565 unixEnterMutex(); 1750 unixEnterMutex();
1566 h = pFile->h;
1567 pInode = pFile->pInode; 1751 pInode = pFile->pInode;
1568 assert( pInode->nShared!=0 ); 1752 assert( pInode->nShared!=0 );
1569 if( pFile->eFileLock>SHARED_LOCK ){ 1753 if( pFile->eFileLock>SHARED_LOCK ){
1570 assert( pInode->eFileLock==pFile->eFileLock ); 1754 assert( pInode->eFileLock==pFile->eFileLock );
1571 SimulateIOErrorBenign(1);
1572 SimulateIOError( h=(-1) )
1573 SimulateIOErrorBenign(0);
1574 1755
1575 #ifndef NDEBUG 1756 #ifdef SQLITE_DEBUG
1576 /* When reducing a lock such that other processes can start 1757 /* When reducing a lock such that other processes can start
1577 ** reading the database file again, make sure that the 1758 ** reading the database file again, make sure that the
1578 ** transaction counter was updated if any part of the database 1759 ** transaction counter was updated if any part of the database
1579 ** file changed. If the transaction counter is not updated, 1760 ** file changed. If the transaction counter is not updated,
1580 ** other connections to the same file might not realize that 1761 ** other connections to the same file might not realize that
1581 ** the file has changed and hence might not know to flush their 1762 ** the file has changed and hence might not know to flush their
1582 ** cache. The use of a stale cache can lead to database corruption. 1763 ** cache. The use of a stale cache can lead to database corruption.
1583 */ 1764 */
1584 #if 0
1585 assert( pFile->inNormalWrite==0
1586 || pFile->dbUpdate==0
1587 || pFile->transCntrChng==1 );
1588 #endif
1589 pFile->inNormalWrite = 0; 1765 pFile->inNormalWrite = 0;
1590 #endif 1766 #endif
1591 1767
1592 /* downgrading to a shared lock on NFS involves clearing the write lock 1768 /* downgrading to a shared lock on NFS involves clearing the write lock
1593 ** before establishing the readlock - to avoid a race condition we downgrade 1769 ** before establishing the readlock - to avoid a race condition we downgrade
1594 ** the lock in 2 blocks, so that part of the range will be covered by a 1770 ** the lock in 2 blocks, so that part of the range will be covered by a
1595 ** write lock until the rest is covered by a read lock: 1771 ** write lock until the rest is covered by a read lock:
1596 ** 1: [WWWWW] 1772 ** 1: [WWWWW]
1597 ** 2: [....W] 1773 ** 2: [....W]
1598 ** 3: [RRRRW] 1774 ** 3: [RRRRW]
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
1680 if( eFileLock==NO_LOCK ){ 1856 if( eFileLock==NO_LOCK ){
1681 /* Decrement the shared lock counter. Release the lock using an 1857 /* Decrement the shared lock counter. Release the lock using an
1682 ** OS call only when all threads in this same process have released 1858 ** OS call only when all threads in this same process have released
1683 ** the lock. 1859 ** the lock.
1684 */ 1860 */
1685 pInode->nShared--; 1861 pInode->nShared--;
1686 if( pInode->nShared==0 ){ 1862 if( pInode->nShared==0 ){
1687 lock.l_type = F_UNLCK; 1863 lock.l_type = F_UNLCK;
1688 lock.l_whence = SEEK_SET; 1864 lock.l_whence = SEEK_SET;
1689 lock.l_start = lock.l_len = 0L; 1865 lock.l_start = lock.l_len = 0L;
1690 SimulateIOErrorBenign(1);
1691 SimulateIOError( h=(-1) )
1692 SimulateIOErrorBenign(0);
1693 if( unixFileLock(pFile, &lock)==0 ){ 1866 if( unixFileLock(pFile, &lock)==0 ){
1694 pInode->eFileLock = NO_LOCK; 1867 pInode->eFileLock = NO_LOCK;
1695 }else{ 1868 }else{
1696 rc = SQLITE_IOERR_UNLOCK; 1869 rc = SQLITE_IOERR_UNLOCK;
1697 » pFile->lastErrno = errno; 1870 pFile->lastErrno = errno;
1698 pInode->eFileLock = NO_LOCK; 1871 pInode->eFileLock = NO_LOCK;
1699 pFile->eFileLock = NO_LOCK; 1872 pFile->eFileLock = NO_LOCK;
1700 } 1873 }
1701 } 1874 }
1702 1875
1703 /* Decrement the count of locks against this same file. When the 1876 /* Decrement the count of locks against this same file. When the
1704 ** count reaches zero, close any other file descriptors whose close 1877 ** count reaches zero, close any other file descriptors whose close
1705 ** was deferred because of outstanding locks. 1878 ** was deferred because of outstanding locks.
1706 */ 1879 */
1707 pInode->nLock--; 1880 pInode->nLock--;
1708 assert( pInode->nLock>=0 ); 1881 assert( pInode->nLock>=0 );
1709 if( pInode->nLock==0 ){ 1882 if( pInode->nLock==0 ){
1710 closePendingFds(pFile); 1883 closePendingFds(pFile);
1711 } 1884 }
1712 } 1885 }
1713 » 1886
1714 end_unlock: 1887 end_unlock:
1715 unixLeaveMutex(); 1888 unixLeaveMutex();
1716 if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; 1889 if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
1717 return rc; 1890 return rc;
1718 } 1891 }
1719 1892
1720 /* 1893 /*
1721 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock 1894 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
1722 ** must be either NO_LOCK or SHARED_LOCK. 1895 ** must be either NO_LOCK or SHARED_LOCK.
1723 ** 1896 **
1724 ** If the locking level of the file descriptor is already at or below 1897 ** If the locking level of the file descriptor is already at or below
1725 ** the requested locking level, this routine is a no-op. 1898 ** the requested locking level, this routine is a no-op.
1726 */ 1899 */
1727 static int unixUnlock(sqlite3_file *id, int eFileLock){ 1900 static int unixUnlock(sqlite3_file *id, int eFileLock){
1901 #if SQLITE_MAX_MMAP_SIZE>0
1902 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
1903 #endif
1728 return posixUnlock(id, eFileLock, 0); 1904 return posixUnlock(id, eFileLock, 0);
1729 } 1905 }
1730 1906
1907 #if SQLITE_MAX_MMAP_SIZE>0
1908 static int unixMapfile(unixFile *pFd, i64 nByte);
1909 static void unixUnmapfile(unixFile *pFd);
1910 #endif
1911
1731 /* 1912 /*
1732 ** This function performs the parts of the "close file" operation 1913 ** This function performs the parts of the "close file" operation
1733 ** common to all locking schemes. It closes the directory and file 1914 ** common to all locking schemes. It closes the directory and file
1734 ** handles, if they are valid, and sets all fields of the unixFile 1915 ** handles, if they are valid, and sets all fields of the unixFile
1735 ** structure to 0. 1916 ** structure to 0.
1736 ** 1917 **
1737 ** It is *not* necessary to hold the mutex when this routine is called, 1918 ** It is *not* necessary to hold the mutex when this routine is called,
1738 ** even on VxWorks. A mutex will be acquired on VxWorks by the 1919 ** even on VxWorks. A mutex will be acquired on VxWorks by the
1739 ** vxworksReleaseFileId() routine. 1920 ** vxworksReleaseFileId() routine.
1740 */ 1921 */
1741 static int closeUnixFile(sqlite3_file *id){ 1922 static int closeUnixFile(sqlite3_file *id){
1742 unixFile *pFile = (unixFile*)id; 1923 unixFile *pFile = (unixFile*)id;
1924 #if SQLITE_MAX_MMAP_SIZE>0
1925 unixUnmapfile(pFile);
1926 #endif
1743 if( pFile->h>=0 ){ 1927 if( pFile->h>=0 ){
1744 robust_close(pFile, pFile->h, __LINE__); 1928 robust_close(pFile, pFile->h, __LINE__);
1745 pFile->h = -1; 1929 pFile->h = -1;
1746 } 1930 }
1747 #if OS_VXWORKS 1931 #if OS_VXWORKS
1748 if( pFile->pId ){ 1932 if( pFile->pId ){
1749 if( pFile->isDelete ){ 1933 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
1750 osUnlink(pFile->pId->zCanonicalName); 1934 osUnlink(pFile->pId->zCanonicalName);
1751 } 1935 }
1752 vxworksReleaseFileId(pFile->pId); 1936 vxworksReleaseFileId(pFile->pId);
1753 pFile->pId = 0; 1937 pFile->pId = 0;
1754 } 1938 }
1755 #endif 1939 #endif
1940 #ifdef SQLITE_UNLINK_AFTER_CLOSE
1941 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
1942 osUnlink(pFile->zPath);
1943 sqlite3_free(*(char**)&pFile->zPath);
1944 pFile->zPath = 0;
1945 }
1946 #endif
1756 OSTRACE(("CLOSE %-3d\n", pFile->h)); 1947 OSTRACE(("CLOSE %-3d\n", pFile->h));
1757 OpenCounter(-1); 1948 OpenCounter(-1);
1758 sqlite3_free(pFile->pUnused); 1949 sqlite3_free(pFile->pUnused);
1759 memset(pFile, 0, sizeof(unixFile)); 1950 memset(pFile, 0, sizeof(unixFile));
1760 return SQLITE_OK; 1951 return SQLITE_OK;
1761 } 1952 }
1762 1953
1763 /* 1954 /*
1764 ** Close a file. 1955 ** Close a file.
1765 */ 1956 */
1766 static int unixClose(sqlite3_file *id){ 1957 static int unixClose(sqlite3_file *id){
1767 int rc = SQLITE_OK; 1958 int rc = SQLITE_OK;
1768 unixFile *pFile = (unixFile *)id; 1959 unixFile *pFile = (unixFile *)id;
1960 verifyDbFile(pFile);
1769 unixUnlock(id, NO_LOCK); 1961 unixUnlock(id, NO_LOCK);
1770 unixEnterMutex(); 1962 unixEnterMutex();
1771 1963
1772 /* unixFile.pInode is always valid here. Otherwise, a different close 1964 /* unixFile.pInode is always valid here. Otherwise, a different close
1773 ** routine (e.g. nolockClose()) would be called instead. 1965 ** routine (e.g. nolockClose()) would be called instead.
1774 */ 1966 */
1775 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 ); 1967 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
1776 if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){ 1968 if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
1777 /* If there are outstanding locks, do not actually close the file just 1969 /* If there are outstanding locks, do not actually close the file just
1778 ** yet because that would clear those locks. Instead, add the file 1970 ** yet because that would clear those locks. Instead, add the file
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1827 static int nolockClose(sqlite3_file *id) { 2019 static int nolockClose(sqlite3_file *id) {
1828 return closeUnixFile(id); 2020 return closeUnixFile(id);
1829 } 2021 }
1830 2022
1831 /******************* End of the no-op lock implementation ********************* 2023 /******************* End of the no-op lock implementation *********************
1832 ******************************************************************************/ 2024 ******************************************************************************/
1833 2025
1834 /****************************************************************************** 2026 /******************************************************************************
1835 ************************* Begin dot-file Locking ****************************** 2027 ************************* Begin dot-file Locking ******************************
1836 ** 2028 **
1837 ** The dotfile locking implementation uses the existance of separate lock 2029 ** The dotfile locking implementation uses the existence of separate lock
1838 ** files in order to control access to the database. This works on just 2030 ** files (really a directory) to control access to the database. This works
1839 ** about every filesystem imaginable. But there are serious downsides: 2031 ** on just about every filesystem imaginable. But there are serious downsides:
1840 ** 2032 **
1841 ** (1) There is zero concurrency. A single reader blocks all other 2033 ** (1) There is zero concurrency. A single reader blocks all other
1842 ** connections from reading or writing the database. 2034 ** connections from reading or writing the database.
1843 ** 2035 **
1844 ** (2) An application crash or power loss can leave stale lock files 2036 ** (2) An application crash or power loss can leave stale lock files
1845 ** sitting around that need to be cleared manually. 2037 ** sitting around that need to be cleared manually.
1846 ** 2038 **
1847 ** Nevertheless, a dotlock is an appropriate locking mode for use if no 2039 ** Nevertheless, a dotlock is an appropriate locking mode for use if no
1848 ** other locking strategy is available. 2040 ** other locking strategy is available.
1849 ** 2041 **
1850 ** Dotfile locking works by creating a file in the same directory as the 2042 ** Dotfile locking works by creating a subdirectory in the same directory as
1851 ** database and with the same name but with a ".lock" extension added. 2043 ** the database and with the same name but with a ".lock" extension added.
1852 ** The existance of a lock file implies an EXCLUSIVE lock. All other lock 2044 ** The existence of a lock directory implies an EXCLUSIVE lock. All other
1853 ** types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. 2045 ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
1854 */ 2046 */
1855 2047
1856 /* 2048 /*
1857 ** The file suffix added to the data base filename in order to create the 2049 ** The file suffix added to the data base filename in order to create the
1858 ** lock file. 2050 ** lock directory.
1859 */ 2051 */
1860 #define DOTLOCK_SUFFIX ".lock" 2052 #define DOTLOCK_SUFFIX ".lock"
1861 2053
1862 /* 2054 /*
1863 ** This routine checks if there is a RESERVED lock held on the specified 2055 ** This routine checks if there is a RESERVED lock held on the specified
1864 ** file by this or any other process. If such a lock is held, set *pResOut 2056 ** file by this or any other process. If such a lock is held, set *pResOut
1865 ** to a non-zero value otherwise *pResOut is set to zero. The return value 2057 ** to a non-zero value otherwise *pResOut is set to zero. The return value
1866 ** is set to SQLITE_OK unless an I/O error occurs during lock checking. 2058 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
1867 ** 2059 **
1868 ** In dotfile locking, either a lock exists or it does not. So in this 2060 ** In dotfile locking, either a lock exists or it does not. So in this
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1915 ** PENDING -> EXCLUSIVE 2107 ** PENDING -> EXCLUSIVE
1916 ** 2108 **
1917 ** This routine will only increase a lock. Use the sqlite3OsUnlock() 2109 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
1918 ** routine to lower a locking level. 2110 ** routine to lower a locking level.
1919 ** 2111 **
1920 ** With dotfile locking, we really only support state (4): EXCLUSIVE. 2112 ** With dotfile locking, we really only support state (4): EXCLUSIVE.
1921 ** But we track the other locking levels internally. 2113 ** But we track the other locking levels internally.
1922 */ 2114 */
1923 static int dotlockLock(sqlite3_file *id, int eFileLock) { 2115 static int dotlockLock(sqlite3_file *id, int eFileLock) {
1924 unixFile *pFile = (unixFile*)id; 2116 unixFile *pFile = (unixFile*)id;
1925 int fd;
1926 char *zLockFile = (char *)pFile->lockingContext; 2117 char *zLockFile = (char *)pFile->lockingContext;
1927 int rc = SQLITE_OK; 2118 int rc = SQLITE_OK;
1928 2119
1929 2120
1930 /* If we have any lock, then the lock file already exists. All we have 2121 /* If we have any lock, then the lock file already exists. All we have
1931 ** to do is adjust our internal record of the lock level. 2122 ** to do is adjust our internal record of the lock level.
1932 */ 2123 */
1933 if( pFile->eFileLock > NO_LOCK ){ 2124 if( pFile->eFileLock > NO_LOCK ){
1934 pFile->eFileLock = eFileLock; 2125 pFile->eFileLock = eFileLock;
1935 #if !OS_VXWORKS
1936 /* Always update the timestamp on the old file */ 2126 /* Always update the timestamp on the old file */
2127 #ifdef HAVE_UTIME
2128 utime(zLockFile, NULL);
2129 #else
1937 utimes(zLockFile, NULL); 2130 utimes(zLockFile, NULL);
1938 #endif 2131 #endif
1939 return SQLITE_OK; 2132 return SQLITE_OK;
1940 } 2133 }
1941 2134
1942 /* grab an exclusive lock */ 2135 /* grab an exclusive lock */
1943 fd = robust_open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600); 2136 rc = osMkdir(zLockFile, 0777);
1944 if( fd<0 ){ 2137 if( rc<0 ){
1945 /* failed to open/create the file, someone else may have stolen the lock */ 2138 /* failed to open/create the lock directory */
1946 int tErrno = errno; 2139 int tErrno = errno;
1947 if( EEXIST == tErrno ){ 2140 if( EEXIST == tErrno ){
1948 rc = SQLITE_BUSY; 2141 rc = SQLITE_BUSY;
1949 } else { 2142 } else {
1950 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); 2143 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
1951 if( IS_LOCK_ERROR(rc) ){ 2144 if( IS_LOCK_ERROR(rc) ){
1952 pFile->lastErrno = tErrno; 2145 pFile->lastErrno = tErrno;
1953 } 2146 }
1954 } 2147 }
1955 return rc; 2148 return rc;
1956 } 2149 }
1957 robust_close(pFile, fd, __LINE__);
1958 2150
1959 /* got it, set the type and return ok */ 2151 /* got it, set the type and return ok */
1960 pFile->eFileLock = eFileLock; 2152 pFile->eFileLock = eFileLock;
1961 return rc; 2153 return rc;
1962 } 2154 }
1963 2155
1964 /* 2156 /*
1965 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock 2157 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
1966 ** must be either NO_LOCK or SHARED_LOCK. 2158 ** must be either NO_LOCK or SHARED_LOCK.
1967 ** 2159 **
1968 ** If the locking level of the file descriptor is already at or below 2160 ** If the locking level of the file descriptor is already at or below
1969 ** the requested locking level, this routine is a no-op. 2161 ** the requested locking level, this routine is a no-op.
1970 ** 2162 **
1971 ** When the locking level reaches NO_LOCK, delete the lock file. 2163 ** When the locking level reaches NO_LOCK, delete the lock file.
1972 */ 2164 */
1973 static int dotlockUnlock(sqlite3_file *id, int eFileLock) { 2165 static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
1974 unixFile *pFile = (unixFile*)id; 2166 unixFile *pFile = (unixFile*)id;
1975 char *zLockFile = (char *)pFile->lockingContext; 2167 char *zLockFile = (char *)pFile->lockingContext;
2168 int rc;
1976 2169
1977 assert( pFile ); 2170 assert( pFile );
1978 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, 2171 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
1979 » pFile->eFileLock, getpid())); 2172 pFile->eFileLock, getpid()));
1980 assert( eFileLock<=SHARED_LOCK ); 2173 assert( eFileLock<=SHARED_LOCK );
1981 2174
1982 /* no-op if possible */ 2175 /* no-op if possible */
1983 if( pFile->eFileLock==eFileLock ){ 2176 if( pFile->eFileLock==eFileLock ){
1984 return SQLITE_OK; 2177 return SQLITE_OK;
1985 } 2178 }
1986 2179
1987 /* To downgrade to shared, simply update our internal notion of the 2180 /* To downgrade to shared, simply update our internal notion of the
1988 ** lock state. No need to mess with the file on disk. 2181 ** lock state. No need to mess with the file on disk.
1989 */ 2182 */
1990 if( eFileLock==SHARED_LOCK ){ 2183 if( eFileLock==SHARED_LOCK ){
1991 pFile->eFileLock = SHARED_LOCK; 2184 pFile->eFileLock = SHARED_LOCK;
1992 return SQLITE_OK; 2185 return SQLITE_OK;
1993 } 2186 }
1994 2187
1995 /* To fully unlock the database, delete the lock file */ 2188 /* To fully unlock the database, delete the lock file */
1996 assert( eFileLock==NO_LOCK ); 2189 assert( eFileLock==NO_LOCK );
1997 if( osUnlink(zLockFile) ){ 2190 rc = osRmdir(zLockFile);
1998 int rc = 0; 2191 if( rc<0 && errno==ENOTDIR ) rc = osUnlink(zLockFile);
2192 if( rc<0 ){
1999 int tErrno = errno; 2193 int tErrno = errno;
2194 rc = 0;
2000 if( ENOENT != tErrno ){ 2195 if( ENOENT != tErrno ){
2001 rc = SQLITE_IOERR_UNLOCK; 2196 rc = SQLITE_IOERR_UNLOCK;
2002 } 2197 }
2003 if( IS_LOCK_ERROR(rc) ){ 2198 if( IS_LOCK_ERROR(rc) ){
2004 pFile->lastErrno = tErrno; 2199 pFile->lastErrno = tErrno;
2005 } 2200 }
2006 return rc; 2201 return rc;
2007 } 2202 }
2008 pFile->eFileLock = NO_LOCK; 2203 pFile->eFileLock = NO_LOCK;
2009 return SQLITE_OK; 2204 return SQLITE_OK;
2010 } 2205 }
2011 2206
2012 /* 2207 /*
2013 ** Close a file. Make sure the lock has been released before closing. 2208 ** Close a file. Make sure the lock has been released before closing.
2014 */ 2209 */
2015 static int dotlockClose(sqlite3_file *id) { 2210 static int dotlockClose(sqlite3_file *id) {
2016 int rc; 2211 int rc = SQLITE_OK;
2017 if( id ){ 2212 if( id ){
2018 unixFile *pFile = (unixFile*)id; 2213 unixFile *pFile = (unixFile*)id;
2019 dotlockUnlock(id, NO_LOCK); 2214 dotlockUnlock(id, NO_LOCK);
2020 sqlite3_free(pFile->lockingContext); 2215 sqlite3_free(pFile->lockingContext);
2216 rc = closeUnixFile(id);
2021 } 2217 }
2022 rc = closeUnixFile(id);
2023 return rc; 2218 return rc;
2024 } 2219 }
2025 /****************** End of the dot-file lock implementation ******************* 2220 /****************** End of the dot-file lock implementation *******************
2026 ******************************************************************************/ 2221 ******************************************************************************/
2027 2222
2028 /****************************************************************************** 2223 /******************************************************************************
2029 ************************** Begin flock Locking ******************************** 2224 ************************** Begin flock Locking ********************************
2030 ** 2225 **
2031 ** Use the flock() system call to do file locking. 2226 ** Use the flock() system call to do file locking.
2032 ** 2227 **
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
2216 }else{ 2411 }else{
2217 pFile->eFileLock = NO_LOCK; 2412 pFile->eFileLock = NO_LOCK;
2218 return SQLITE_OK; 2413 return SQLITE_OK;
2219 } 2414 }
2220 } 2415 }
2221 2416
2222 /* 2417 /*
2223 ** Close a file. 2418 ** Close a file.
2224 */ 2419 */
2225 static int flockClose(sqlite3_file *id) { 2420 static int flockClose(sqlite3_file *id) {
2421 int rc = SQLITE_OK;
2226 if( id ){ 2422 if( id ){
2227 flockUnlock(id, NO_LOCK); 2423 flockUnlock(id, NO_LOCK);
2424 rc = closeUnixFile(id);
2228 } 2425 }
2229 return closeUnixFile(id); 2426 return rc;
2230 } 2427 }
2231 2428
2232 #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ 2429 #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
2233 2430
2234 /******************* End of the flock lock implementation ********************* 2431 /******************* End of the flock lock implementation *********************
2235 ******************************************************************************/ 2432 ******************************************************************************/
2236 2433
2237 /****************************************************************************** 2434 /******************************************************************************
2238 ************************ Begin Named Semaphore Locking ************************ 2435 ************************ Begin Named Semaphore Locking ************************
2239 ** 2436 **
(...skipping 22 matching lines...) Expand all
2262 assert( pFile ); 2459 assert( pFile );
2263 2460
2264 /* Check if a thread in this process holds such a lock */ 2461 /* Check if a thread in this process holds such a lock */
2265 if( pFile->eFileLock>SHARED_LOCK ){ 2462 if( pFile->eFileLock>SHARED_LOCK ){
2266 reserved = 1; 2463 reserved = 1;
2267 } 2464 }
2268 2465
2269 /* Otherwise see if some other process holds it. */ 2466 /* Otherwise see if some other process holds it. */
2270 if( !reserved ){ 2467 if( !reserved ){
2271 sem_t *pSem = pFile->pInode->pSem; 2468 sem_t *pSem = pFile->pInode->pSem;
2272 struct stat statBuf;
2273 2469
2274 if( sem_trywait(pSem)==-1 ){ 2470 if( sem_trywait(pSem)==-1 ){
2275 int tErrno = errno; 2471 int tErrno = errno;
2276 if( EAGAIN != tErrno ){ 2472 if( EAGAIN != tErrno ){
2277 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); 2473 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
2278 pFile->lastErrno = tErrno; 2474 pFile->lastErrno = tErrno;
2279 } else { 2475 } else {
2280 /* someone else has the lock when we are in NO_LOCK */ 2476 /* someone else has the lock when we are in NO_LOCK */
2281 reserved = (pFile->eFileLock < SHARED_LOCK); 2477 reserved = (pFile->eFileLock < SHARED_LOCK);
2282 } 2478 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
2315 ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate 2511 ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate
2316 ** lock states in the sqlite3_file structure, but all locks SHARED or 2512 ** lock states in the sqlite3_file structure, but all locks SHARED or
2317 ** above are really EXCLUSIVE locks and exclude all other processes from 2513 ** above are really EXCLUSIVE locks and exclude all other processes from
2318 ** access the file. 2514 ** access the file.
2319 ** 2515 **
2320 ** This routine will only increase a lock. Use the sqlite3OsUnlock() 2516 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
2321 ** routine to lower a locking level. 2517 ** routine to lower a locking level.
2322 */ 2518 */
2323 static int semLock(sqlite3_file *id, int eFileLock) { 2519 static int semLock(sqlite3_file *id, int eFileLock) {
2324 unixFile *pFile = (unixFile*)id; 2520 unixFile *pFile = (unixFile*)id;
2325 int fd;
2326 sem_t *pSem = pFile->pInode->pSem; 2521 sem_t *pSem = pFile->pInode->pSem;
2327 int rc = SQLITE_OK; 2522 int rc = SQLITE_OK;
2328 2523
2329 /* if we already have a lock, it is exclusive. 2524 /* if we already have a lock, it is exclusive.
2330 ** Just adjust level and punt on outta here. */ 2525 ** Just adjust level and punt on outta here. */
2331 if (pFile->eFileLock > NO_LOCK) { 2526 if (pFile->eFileLock > NO_LOCK) {
2332 pFile->eFileLock = eFileLock; 2527 pFile->eFileLock = eFileLock;
2333 rc = SQLITE_OK; 2528 rc = SQLITE_OK;
2334 goto sem_end_lock; 2529 goto sem_end_lock;
2335 } 2530 }
(...skipping 18 matching lines...) Expand all
2354 ** If the locking level of the file descriptor is already at or below 2549 ** If the locking level of the file descriptor is already at or below
2355 ** the requested locking level, this routine is a no-op. 2550 ** the requested locking level, this routine is a no-op.
2356 */ 2551 */
2357 static int semUnlock(sqlite3_file *id, int eFileLock) { 2552 static int semUnlock(sqlite3_file *id, int eFileLock) {
2358 unixFile *pFile = (unixFile*)id; 2553 unixFile *pFile = (unixFile*)id;
2359 sem_t *pSem = pFile->pInode->pSem; 2554 sem_t *pSem = pFile->pInode->pSem;
2360 2555
2361 assert( pFile ); 2556 assert( pFile );
2362 assert( pSem ); 2557 assert( pSem );
2363 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, 2558 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
2364 » pFile->eFileLock, getpid())); 2559 pFile->eFileLock, getpid()));
2365 assert( eFileLock<=SHARED_LOCK ); 2560 assert( eFileLock<=SHARED_LOCK );
2366 2561
2367 /* no-op if possible */ 2562 /* no-op if possible */
2368 if( pFile->eFileLock==eFileLock ){ 2563 if( pFile->eFileLock==eFileLock ){
2369 return SQLITE_OK; 2564 return SQLITE_OK;
2370 } 2565 }
2371 2566
2372 /* shared can just be set because we always have an exclusive */ 2567 /* shared can just be set because we always have an exclusive */
2373 if (eFileLock==SHARED_LOCK) { 2568 if (eFileLock==SHARED_LOCK) {
2374 pFile->eFileLock = eFileLock; 2569 pFile->eFileLock = eFileLock;
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
2493 /* 2688 /*
2494 ** This routine checks if there is a RESERVED lock held on the specified 2689 ** This routine checks if there is a RESERVED lock held on the specified
2495 ** file by this or any other process. If such a lock is held, set *pResOut 2690 ** file by this or any other process. If such a lock is held, set *pResOut
2496 ** to a non-zero value otherwise *pResOut is set to zero. The return value 2691 ** to a non-zero value otherwise *pResOut is set to zero. The return value
2497 ** is set to SQLITE_OK unless an I/O error occurs during lock checking. 2692 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
2498 */ 2693 */
2499 static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ 2694 static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
2500 int rc = SQLITE_OK; 2695 int rc = SQLITE_OK;
2501 int reserved = 0; 2696 int reserved = 0;
2502 unixFile *pFile = (unixFile*)id; 2697 unixFile *pFile = (unixFile*)id;
2698 afpLockingContext *context;
2503 2699
2504 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); 2700 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2505 2701
2506 assert( pFile ); 2702 assert( pFile );
2507 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; 2703 context = (afpLockingContext *) pFile->lockingContext;
2508 if( context->reserved ){ 2704 if( context->reserved ){
2509 *pResOut = 1; 2705 *pResOut = 1;
2510 return SQLITE_OK; 2706 return SQLITE_OK;
2511 } 2707 }
2512 unixEnterMutex(); /* Because pFile->pInode is shared across threads */ 2708 unixEnterMutex(); /* Because pFile->pInode is shared across threads */
2513 2709
2514 /* Check if a thread in this process holds such a lock */ 2710 /* Check if a thread in this process holds such a lock */
2515 if( pFile->pInode->eFileLock>SHARED_LOCK ){ 2711 if( pFile->pInode->eFileLock>SHARED_LOCK ){
2516 reserved = 1; 2712 reserved = 1;
2517 } 2713 }
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
2637 if (failed) { 2833 if (failed) {
2638 rc = failed; 2834 rc = failed;
2639 goto afp_end_lock; 2835 goto afp_end_lock;
2640 } 2836 }
2641 } 2837 }
2642 2838
2643 /* If control gets to this point, then actually go ahead and make 2839 /* If control gets to this point, then actually go ahead and make
2644 ** operating system calls for the specified lock. 2840 ** operating system calls for the specified lock.
2645 */ 2841 */
2646 if( eFileLock==SHARED_LOCK ){ 2842 if( eFileLock==SHARED_LOCK ){
2647 int lrc1, lrc2, lrc1Errno; 2843 int lrc1, lrc2, lrc1Errno = 0;
2648 long lk, mask; 2844 long lk, mask;
2649 2845
2650 assert( pInode->nShared==0 ); 2846 assert( pInode->nShared==0 );
2651 assert( pInode->eFileLock==0 ); 2847 assert( pInode->eFileLock==0 );
2652 2848
2653 mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff; 2849 mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
2654 /* Now get the read-lock SHARED_LOCK */ 2850 /* Now get the read-lock SHARED_LOCK */
2655 /* note that the quality of the randomness doesn't matter that much */ 2851 /* note that the quality of the randomness doesn't matter that much */
2656 lk = random(); 2852 lk = random();
2657 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1); 2853 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
2768 } 2964 }
2769 unixEnterMutex(); 2965 unixEnterMutex();
2770 pInode = pFile->pInode; 2966 pInode = pFile->pInode;
2771 assert( pInode->nShared!=0 ); 2967 assert( pInode->nShared!=0 );
2772 if( pFile->eFileLock>SHARED_LOCK ){ 2968 if( pFile->eFileLock>SHARED_LOCK ){
2773 assert( pInode->eFileLock==pFile->eFileLock ); 2969 assert( pInode->eFileLock==pFile->eFileLock );
2774 SimulateIOErrorBenign(1); 2970 SimulateIOErrorBenign(1);
2775 SimulateIOError( h=(-1) ) 2971 SimulateIOError( h=(-1) )
2776 SimulateIOErrorBenign(0); 2972 SimulateIOErrorBenign(0);
2777 2973
2778 #ifndef NDEBUG 2974 #ifdef SQLITE_DEBUG
2779 /* When reducing a lock such that other processes can start 2975 /* When reducing a lock such that other processes can start
2780 ** reading the database file again, make sure that the 2976 ** reading the database file again, make sure that the
2781 ** transaction counter was updated if any part of the database 2977 ** transaction counter was updated if any part of the database
2782 ** file changed. If the transaction counter is not updated, 2978 ** file changed. If the transaction counter is not updated,
2783 ** other connections to the same file might not realize that 2979 ** other connections to the same file might not realize that
2784 ** the file has changed and hence might not know to flush their 2980 ** the file has changed and hence might not know to flush their
2785 ** cache. The use of a stale cache can lead to database corruption. 2981 ** cache. The use of a stale cache can lead to database corruption.
2786 */ 2982 */
2787 assert( pFile->inNormalWrite==0 2983 assert( pFile->inNormalWrite==0
2788 || pFile->dbUpdate==0 2984 || pFile->dbUpdate==0
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
2916 ** are gather together into this division. 3112 ** are gather together into this division.
2917 */ 3113 */
2918 3114
2919 /* 3115 /*
2920 ** Seek to the offset passed as the second argument, then read cnt 3116 ** Seek to the offset passed as the second argument, then read cnt
2921 ** bytes into pBuf. Return the number of bytes actually read. 3117 ** bytes into pBuf. Return the number of bytes actually read.
2922 ** 3118 **
2923 ** NB: If you define USE_PREAD or USE_PREAD64, then it might also 3119 ** NB: If you define USE_PREAD or USE_PREAD64, then it might also
2924 ** be necessary to define _XOPEN_SOURCE to be 500. This varies from 3120 ** be necessary to define _XOPEN_SOURCE to be 500. This varies from
2925 ** one system to another. Since SQLite does not define USE_PREAD 3121 ** one system to another. Since SQLite does not define USE_PREAD
2926 ** any any form by default, we will not attempt to define _XOPEN_SOURCE. 3122 ** in any form by default, we will not attempt to define _XOPEN_SOURCE.
2927 ** See tickets #2741 and #2681. 3123 ** See tickets #2741 and #2681.
2928 ** 3124 **
2929 ** To avoid stomping the errno value on a failed read the lastErrno value 3125 ** To avoid stomping the errno value on a failed read the lastErrno value
2930 ** is set before returning. 3126 ** is set before returning.
2931 */ 3127 */
2932 static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ 3128 static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
2933 int got; 3129 int got;
3130 int prior = 0;
2934 #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) 3131 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
2935 i64 newOffset; 3132 i64 newOffset;
2936 #endif 3133 #endif
2937 TIMER_START; 3134 TIMER_START;
3135 assert( cnt==(cnt&0x1ffff) );
3136 assert( id->h>2 );
3137 cnt &= 0x1ffff;
3138 do{
2938 #if defined(USE_PREAD) 3139 #if defined(USE_PREAD)
2939 do{ got = osPread(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR ); 3140 got = osPread(id->h, pBuf, cnt, offset);
2940 SimulateIOError( got = -1 ); 3141 SimulateIOError( got = -1 );
2941 #elif defined(USE_PREAD64) 3142 #elif defined(USE_PREAD64)
2942 do{ got = osPread64(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR); 3143 got = osPread64(id->h, pBuf, cnt, offset);
2943 SimulateIOError( got = -1 ); 3144 SimulateIOError( got = -1 );
2944 #else 3145 #else
2945 newOffset = lseek(id->h, offset, SEEK_SET); 3146 newOffset = lseek(id->h, offset, SEEK_SET);
2946 SimulateIOError( newOffset-- ); 3147 SimulateIOError( newOffset-- );
2947 if( newOffset!=offset ){ 3148 if( newOffset!=offset ){
2948 if( newOffset == -1 ){ 3149 if( newOffset == -1 ){
3150 ((unixFile*)id)->lastErrno = errno;
3151 }else{
3152 ((unixFile*)id)->lastErrno = 0;
3153 }
3154 return -1;
3155 }
3156 got = osRead(id->h, pBuf, cnt);
3157 #endif
3158 if( got==cnt ) break;
3159 if( got<0 ){
3160 if( errno==EINTR ){ got = 1; continue; }
3161 prior = 0;
2949 ((unixFile*)id)->lastErrno = errno; 3162 ((unixFile*)id)->lastErrno = errno;
2950 }else{ 3163 break;
2951 ((unixFile*)id)->lastErrno = 0;» » » 3164 }else if( got>0 ){
3165 cnt -= got;
3166 offset += got;
3167 prior += got;
3168 pBuf = (void*)(got + (char*)pBuf);
2952 } 3169 }
2953 return -1; 3170 }while( got>0 );
2954 }
2955 do{ got = osRead(id->h, pBuf, cnt); }while( got<0 && errno==EINTR );
2956 #endif
2957 TIMER_END; 3171 TIMER_END;
2958 if( got<0 ){ 3172 OSTRACE(("READ %-3d %5d %7lld %llu\n",
2959 ((unixFile*)id)->lastErrno = errno; 3173 id->h, got+prior, offset-prior, TIMER_ELAPSED));
2960 } 3174 return got+prior;
2961 OSTRACE(("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
2962 return got;
2963 } 3175 }
2964 3176
2965 /* 3177 /*
2966 ** Read data from a file into a buffer. Return SQLITE_OK if all 3178 ** Read data from a file into a buffer. Return SQLITE_OK if all
2967 ** bytes were read successfully and SQLITE_IOERR if anything goes 3179 ** bytes were read successfully and SQLITE_IOERR if anything goes
2968 ** wrong. 3180 ** wrong.
2969 */ 3181 */
2970 static int unixRead( 3182 static int unixRead(
2971 sqlite3_file *id, 3183 sqlite3_file *id,
2972 void *pBuf, 3184 void *pBuf,
2973 int amt, 3185 int amt,
2974 sqlite3_int64 offset 3186 sqlite3_int64 offset
2975 ){ 3187 ){
2976 unixFile *pFile = (unixFile *)id; 3188 unixFile *pFile = (unixFile *)id;
2977 int got; 3189 int got;
2978 assert( id ); 3190 assert( id );
3191 assert( offset>=0 );
3192 assert( amt>0 );
2979 3193
2980 /* If this is a database file (not a journal, master-journal or temp 3194 /* If this is a database file (not a journal, master-journal or temp
2981 ** file), the bytes in the locking range should never be read or written. */ 3195 ** file), the bytes in the locking range should never be read or written. */
2982 #if 0 3196 #if 0
2983 assert( pFile->pUnused==0 3197 assert( pFile->pUnused==0
2984 || offset>=PENDING_BYTE+512 3198 || offset>=PENDING_BYTE+512
2985 || offset+amt<=PENDING_BYTE 3199 || offset+amt<=PENDING_BYTE
2986 ); 3200 );
2987 #endif 3201 #endif
2988 3202
3203 #if SQLITE_MAX_MMAP_SIZE>0
3204 /* Deal with as much of this read request as possible by transfering
3205 ** data from the memory mapping using memcpy(). */
3206 if( offset<pFile->mmapSize ){
3207 if( offset+amt <= pFile->mmapSize ){
3208 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
3209 return SQLITE_OK;
3210 }else{
3211 int nCopy = pFile->mmapSize - offset;
3212 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
3213 pBuf = &((u8 *)pBuf)[nCopy];
3214 amt -= nCopy;
3215 offset += nCopy;
3216 }
3217 }
3218 #endif
3219
2989 got = seekAndRead(pFile, offset, pBuf, amt); 3220 got = seekAndRead(pFile, offset, pBuf, amt);
2990 if( got==amt ){ 3221 if( got==amt ){
2991 return SQLITE_OK; 3222 return SQLITE_OK;
2992 }else if( got<0 ){ 3223 }else if( got<0 ){
2993 /* lastErrno set by seekAndRead */ 3224 /* lastErrno set by seekAndRead */
2994 return SQLITE_IOERR_READ; 3225 return SQLITE_IOERR_READ;
2995 }else{ 3226 }else{
2996 pFile->lastErrno = 0; /* not a system error */ 3227 pFile->lastErrno = 0; /* not a system error */
2997 /* Unread parts of the buffer must be zero-filled */ 3228 /* Unread parts of the buffer must be zero-filled */
2998 memset(&((char*)pBuf)[got], 0, amt-got); 3229 memset(&((char*)pBuf)[got], 0, amt-got);
2999 return SQLITE_IOERR_SHORT_READ; 3230 return SQLITE_IOERR_SHORT_READ;
3000 } 3231 }
3001 } 3232 }
3002 3233
3003 /* 3234 /*
3235 ** Attempt to seek the file-descriptor passed as the first argument to
3236 ** absolute offset iOff, then attempt to write nBuf bytes of data from
3237 ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
3238 ** return the actual number of bytes written (which may be less than
3239 ** nBuf).
3240 */
3241 static int seekAndWriteFd(
3242 int fd, /* File descriptor to write to */
3243 i64 iOff, /* File offset to begin writing at */
3244 const void *pBuf, /* Copy data from this buffer to the file */
3245 int nBuf, /* Size of buffer pBuf in bytes */
3246 int *piErrno /* OUT: Error number if error occurs */
3247 ){
3248 int rc = 0; /* Value returned by system call */
3249
3250 assert( nBuf==(nBuf&0x1ffff) );
3251 assert( fd>2 );
3252 nBuf &= 0x1ffff;
3253 TIMER_START;
3254
3255 #if defined(USE_PREAD)
3256 do{ rc = osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
3257 #elif defined(USE_PREAD64)
3258 do{ rc = osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR);
3259 #else
3260 do{
3261 i64 iSeek = lseek(fd, iOff, SEEK_SET);
3262 SimulateIOError( iSeek-- );
3263
3264 if( iSeek!=iOff ){
3265 if( piErrno ) *piErrno = (iSeek==-1 ? errno : 0);
3266 return -1;
3267 }
3268 rc = osWrite(fd, pBuf, nBuf);
3269 }while( rc<0 && errno==EINTR );
3270 #endif
3271
3272 TIMER_END;
3273 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
3274
3275 if( rc<0 && piErrno ) *piErrno = errno;
3276 return rc;
3277 }
3278
3279
3280 /*
3004 ** Seek to the offset in id->offset then read cnt bytes into pBuf. 3281 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
3005 ** Return the number of bytes actually read. Update the offset. 3282 ** Return the number of bytes actually read. Update the offset.
3006 ** 3283 **
3007 ** To avoid stomping the errno value on a failed write the lastErrno value 3284 ** To avoid stomping the errno value on a failed write the lastErrno value
3008 ** is set before returning. 3285 ** is set before returning.
3009 */ 3286 */
3010 static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ 3287 static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
3011 int got; 3288 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
3012 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
3013 i64 newOffset;
3014 #endif
3015 TIMER_START;
3016 #if defined(USE_PREAD)
3017 do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
3018 #elif defined(USE_PREAD64)
3019 do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
3020 #else
3021 newOffset = lseek(id->h, offset, SEEK_SET);
3022 SimulateIOError( newOffset-- );
3023 if( newOffset!=offset ){
3024 if( newOffset == -1 ){
3025 ((unixFile*)id)->lastErrno = errno;
3026 }else{
3027 ((unixFile*)id)->lastErrno = 0;» » »
3028 }
3029 return -1;
3030 }
3031 do{ got = osWrite(id->h, pBuf, cnt); }while( got<0 && errno==EINTR );
3032 #endif
3033 TIMER_END;
3034 if( got<0 ){
3035 ((unixFile*)id)->lastErrno = errno;
3036 }
3037
3038 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
3039 return got;
3040 } 3289 }
3041 3290
3042 3291
3043 /* 3292 /*
3044 ** Write data from a buffer into a file. Return SQLITE_OK on success 3293 ** Write data from a buffer into a file. Return SQLITE_OK on success
3045 ** or some other error code on failure. 3294 ** or some other error code on failure.
3046 */ 3295 */
3047 static int unixWrite( 3296 static int unixWrite(
3048 sqlite3_file *id, 3297 sqlite3_file *id,
3049 const void *pBuf, 3298 const void *pBuf,
3050 int amt, 3299 int amt,
3051 sqlite3_int64 offset 3300 sqlite3_int64 offset
3052 ){ 3301 ){
3053 unixFile *pFile = (unixFile*)id; 3302 unixFile *pFile = (unixFile*)id;
3054 int wrote = 0; 3303 int wrote = 0;
3055 assert( id ); 3304 assert( id );
3056 assert( amt>0 ); 3305 assert( amt>0 );
3057 3306
3058 /* If this is a database file (not a journal, master-journal or temp 3307 /* If this is a database file (not a journal, master-journal or temp
3059 ** file), the bytes in the locking range should never be read or written. */ 3308 ** file), the bytes in the locking range should never be read or written. */
3060 #if 0 3309 #if 0
3061 assert( pFile->pUnused==0 3310 assert( pFile->pUnused==0
3062 || offset>=PENDING_BYTE+512 3311 || offset>=PENDING_BYTE+512
3063 || offset+amt<=PENDING_BYTE 3312 || offset+amt<=PENDING_BYTE
3064 ); 3313 );
3065 #endif 3314 #endif
3066 3315
3067 #ifndef NDEBUG 3316 #ifdef SQLITE_DEBUG
3068 /* If we are doing a normal write to a database file (as opposed to 3317 /* If we are doing a normal write to a database file (as opposed to
3069 ** doing a hot-journal rollback or a write to some file other than a 3318 ** doing a hot-journal rollback or a write to some file other than a
3070 ** normal database file) then record the fact that the database 3319 ** normal database file) then record the fact that the database
3071 ** has changed. If the transaction counter is modified, record that 3320 ** has changed. If the transaction counter is modified, record that
3072 ** fact too. 3321 ** fact too.
3073 */ 3322 */
3074 if( pFile->inNormalWrite ){ 3323 if( pFile->inNormalWrite ){
3075 pFile->dbUpdate = 1; /* The database has been modified */ 3324 pFile->dbUpdate = 1; /* The database has been modified */
3076 if( offset<=24 && offset+amt>=27 ){ 3325 if( offset<=24 && offset+amt>=27 ){
3077 int rc; 3326 int rc;
3078 char oldCntr[4]; 3327 char oldCntr[4];
3079 SimulateIOErrorBenign(1); 3328 SimulateIOErrorBenign(1);
3080 rc = seekAndRead(pFile, 24, oldCntr, 4); 3329 rc = seekAndRead(pFile, 24, oldCntr, 4);
3081 SimulateIOErrorBenign(0); 3330 SimulateIOErrorBenign(0);
3082 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ 3331 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
3083 pFile->transCntrChng = 1; /* The transaction counter has changed */ 3332 pFile->transCntrChng = 1; /* The transaction counter has changed */
3084 } 3333 }
3085 } 3334 }
3086 } 3335 }
3087 #endif 3336 #endif
3088 3337
3338 #if SQLITE_MAX_MMAP_SIZE>0
3339 /* Deal with as much of this write request as possible by transfering
3340 ** data from the memory mapping using memcpy(). */
3341 if( offset<pFile->mmapSize ){
3342 if( offset+amt <= pFile->mmapSize ){
3343 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
3344 return SQLITE_OK;
3345 }else{
3346 int nCopy = pFile->mmapSize - offset;
3347 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
3348 pBuf = &((u8 *)pBuf)[nCopy];
3349 amt -= nCopy;
3350 offset += nCopy;
3351 }
3352 }
3353 #endif
3354
3089 while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){ 3355 while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){
3090 amt -= wrote; 3356 amt -= wrote;
3091 offset += wrote; 3357 offset += wrote;
3092 pBuf = &((char*)pBuf)[wrote]; 3358 pBuf = &((char*)pBuf)[wrote];
3093 } 3359 }
3094 SimulateIOError(( wrote=(-1), amt=1 )); 3360 SimulateIOError(( wrote=(-1), amt=1 ));
3095 SimulateDiskfullError(( wrote=0, amt=1 )); 3361 SimulateDiskfullError(( wrote=0, amt=1 ));
3096 3362
3097 if( amt>0 ){ 3363 if( amt>0 ){
3098 if( wrote<0 ){ 3364 if( wrote<0 && pFile->lastErrno!=ENOSPC ){
3099 /* lastErrno set by seekAndWrite */ 3365 /* lastErrno set by seekAndWrite */
3100 return SQLITE_IOERR_WRITE; 3366 return SQLITE_IOERR_WRITE;
3101 }else{ 3367 }else{
3102 pFile->lastErrno = 0; /* not a system error */ 3368 pFile->lastErrno = 0; /* not a system error */
3103 return SQLITE_FULL; 3369 return SQLITE_FULL;
3104 } 3370 }
3105 } 3371 }
3106 3372
3107 return SQLITE_OK; 3373 return SQLITE_OK;
3108 } 3374 }
3109 3375
3110 #ifdef SQLITE_TEST 3376 #ifdef SQLITE_TEST
3111 /* 3377 /*
3112 ** Count the number of fullsyncs and normal syncs. This is used to test 3378 ** Count the number of fullsyncs and normal syncs. This is used to test
3113 ** that syncs and fullsyncs are occurring at the right times. 3379 ** that syncs and fullsyncs are occurring at the right times.
3114 */ 3380 */
3115 int sqlite3_sync_count = 0; 3381 int sqlite3_sync_count = 0;
3116 int sqlite3_fullsync_count = 0; 3382 int sqlite3_fullsync_count = 0;
3117 #endif 3383 #endif
3118 3384
3119 /* 3385 /*
3120 ** We do not trust systems to provide a working fdatasync(). Some do. 3386 ** We do not trust systems to provide a working fdatasync(). Some do.
3121 ** Others do no. To be safe, we will stick with the (slower) fsync(). 3387 ** Others do no. To be safe, we will stick with the (slightly slower)
3122 ** If you know that your system does support fdatasync() correctly, 3388 ** fsync(). If you know that your system does support fdatasync() correctly,
3123 ** then simply compile with -Dfdatasync=fdatasync 3389 ** then simply compile with -Dfdatasync=fdatasync
3124 */ 3390 */
3125 #if !defined(fdatasync) && !defined(__linux__) 3391 #if !defined(fdatasync)
3126 # define fdatasync fsync 3392 # define fdatasync fsync
3127 #endif 3393 #endif
3128 3394
3129 /* 3395 /*
3130 ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not 3396 ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
3131 ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently 3397 ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
3132 ** only available on Mac OS X. But that could change. 3398 ** only available on Mac OS X. But that could change.
3133 */ 3399 */
3134 #ifdef F_FULLFSYNC 3400 #ifdef F_FULLFSYNC
3135 # define HAVE_FULLFSYNC 1 3401 # define HAVE_FULLFSYNC 1
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
3257 int ii; 3523 int ii;
3258 int fd = -1; 3524 int fd = -1;
3259 char zDirname[MAX_PATHNAME+1]; 3525 char zDirname[MAX_PATHNAME+1];
3260 3526
3261 sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); 3527 sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
3262 for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--); 3528 for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
3263 if( ii>0 ){ 3529 if( ii>0 ){
3264 zDirname[ii] = '\0'; 3530 zDirname[ii] = '\0';
3265 fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0); 3531 fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
3266 if( fd>=0 ){ 3532 if( fd>=0 ){
3267 #ifdef FD_CLOEXEC
3268 osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
3269 #endif
3270 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); 3533 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
3271 } 3534 }
3272 } 3535 }
3273 *pFd = fd; 3536 *pFd = fd;
3274 return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname)); 3537 return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
3275 } 3538 }
3276 3539
3277 /* 3540 /*
3278 ** Make sure all writes to a particular file are committed to disk. 3541 ** Make sure all writes to a particular file are committed to disk.
3279 ** 3542 **
(...skipping 29 matching lines...) Expand all
3309 assert( pFile ); 3572 assert( pFile );
3310 OSTRACE(("SYNC %-3d\n", pFile->h)); 3573 OSTRACE(("SYNC %-3d\n", pFile->h));
3311 rc = full_fsync(pFile->h, isFullsync, isDataOnly); 3574 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
3312 SimulateIOError( rc=1 ); 3575 SimulateIOError( rc=1 );
3313 if( rc ){ 3576 if( rc ){
3314 pFile->lastErrno = errno; 3577 pFile->lastErrno = errno;
3315 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath); 3578 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
3316 } 3579 }
3317 3580
3318 /* Also fsync the directory containing the file if the DIRSYNC flag 3581 /* Also fsync the directory containing the file if the DIRSYNC flag
3319 ** is set. This is a one-time occurrance. Many systems (examples: AIX) 3582 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
3320 ** are unable to fsync a directory, so ignore errors on the fsync. 3583 ** are unable to fsync a directory, so ignore errors on the fsync.
3321 */ 3584 */
3322 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){ 3585 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
3323 int dirfd; 3586 int dirfd;
3324 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath, 3587 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
3325 HAVE_FULLFSYNC, isFullsync)); 3588 HAVE_FULLFSYNC, isFullsync));
3326 rc = osOpenDirectory(pFile->zPath, &dirfd); 3589 rc = osOpenDirectory(pFile->zPath, &dirfd);
3327 if( rc==SQLITE_OK && dirfd>=0 ){ 3590 if( rc==SQLITE_OK && dirfd>=0 ){
3328 full_fsync(dirfd, 0, 0); 3591 full_fsync(dirfd, 0, 0);
3329 robust_close(pFile, dirfd, __LINE__); 3592 robust_close(pFile, dirfd, __LINE__);
(...skipping 12 matching lines...) Expand all
3342 unixFile *pFile = (unixFile *)id; 3605 unixFile *pFile = (unixFile *)id;
3343 int rc; 3606 int rc;
3344 assert( pFile ); 3607 assert( pFile );
3345 SimulateIOError( return SQLITE_IOERR_TRUNCATE ); 3608 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
3346 3609
3347 /* If the user has configured a chunk-size for this file, truncate the 3610 /* If the user has configured a chunk-size for this file, truncate the
3348 ** file so that it consists of an integer number of chunks (i.e. the 3611 ** file so that it consists of an integer number of chunks (i.e. the
3349 ** actual file size after the operation may be larger than the requested 3612 ** actual file size after the operation may be larger than the requested
3350 ** size). 3613 ** size).
3351 */ 3614 */
3352 if( pFile->szChunk ){ 3615 if( pFile->szChunk>0 ){
3353 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; 3616 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
3354 } 3617 }
3355 3618
3356 rc = robust_ftruncate(pFile->h, (off_t)nByte); 3619 rc = robust_ftruncate(pFile->h, nByte);
3357 if( rc ){ 3620 if( rc ){
3358 pFile->lastErrno = errno; 3621 pFile->lastErrno = errno;
3359 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); 3622 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
3360 }else{ 3623 }else{
3361 #ifndef NDEBUG 3624 #ifdef SQLITE_DEBUG
3362 /* If we are doing a normal write to a database file (as opposed to 3625 /* If we are doing a normal write to a database file (as opposed to
3363 ** doing a hot-journal rollback or a write to some file other than a 3626 ** doing a hot-journal rollback or a write to some file other than a
3364 ** normal database file) and we truncate the file to zero length, 3627 ** normal database file) and we truncate the file to zero length,
3365 ** that effectively updates the change counter. This might happen 3628 ** that effectively updates the change counter. This might happen
3366 ** when restoring a database using the backup API from a zero-length 3629 ** when restoring a database using the backup API from a zero-length
3367 ** source. 3630 ** source.
3368 */ 3631 */
3369 if( pFile->inNormalWrite && nByte==0 ){ 3632 if( pFile->inNormalWrite && nByte==0 ){
3370 pFile->transCntrChng = 1; 3633 pFile->transCntrChng = 1;
3371 } 3634 }
3372 #endif 3635 #endif
3373 3636
3637 #if SQLITE_MAX_MMAP_SIZE>0
3638 /* If the file was just truncated to a size smaller than the currently
3639 ** mapped region, reduce the effective mapping size as well. SQLite will
3640 ** use read() and write() to access data beyond this point from now on.
3641 */
3642 if( nByte<pFile->mmapSize ){
3643 pFile->mmapSize = nByte;
3644 }
3645 #endif
3646
3374 return SQLITE_OK; 3647 return SQLITE_OK;
3375 } 3648 }
3376 } 3649 }
3377 3650
3378 /* 3651 /*
3379 ** Determine the current size of a file in bytes 3652 ** Determine the current size of a file in bytes
3380 */ 3653 */
3381 static int unixFileSize(sqlite3_file *id, i64 *pSize){ 3654 static int unixFileSize(sqlite3_file *id, i64 *pSize){
3382 int rc; 3655 int rc;
3383 struct stat buf; 3656 struct stat buf;
(...skipping 21 matching lines...) Expand all
3405 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) 3678 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
3406 /* 3679 /*
3407 ** Handler for proxy-locking file-control verbs. Defined below in the 3680 ** Handler for proxy-locking file-control verbs. Defined below in the
3408 ** proxying locking division. 3681 ** proxying locking division.
3409 */ 3682 */
3410 static int proxyFileControl(sqlite3_file*,int,void*); 3683 static int proxyFileControl(sqlite3_file*,int,void*);
3411 #endif 3684 #endif
3412 3685
3413 /* 3686 /*
3414 ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT 3687 ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
3415 ** file-control operation. 3688 ** file-control operation. Enlarge the database to nBytes in size
3416 ** 3689 ** (rounded up to the next chunk-size). If the database is already
3417 ** If the user has configured a chunk-size for this file, it could be 3690 ** nBytes or larger, this routine is a no-op.
3418 ** that the file needs to be extended at this point. Otherwise, the
3419 ** SQLITE_FCNTL_SIZE_HINT operation is a no-op for Unix.
3420 */ 3691 */
3421 static int fcntlSizeHint(unixFile *pFile, i64 nByte){ 3692 static int fcntlSizeHint(unixFile *pFile, i64 nByte){
3422 if( pFile->szChunk ){ 3693 if( pFile->szChunk>0 ){
3423 i64 nSize; /* Required file size */ 3694 i64 nSize; /* Required file size */
3424 struct stat buf; /* Used to hold return values of fstat() */ 3695 struct stat buf; /* Used to hold return values of fstat() */
3425 3696
3426 if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT; 3697 if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
3427 3698
3428 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk; 3699 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
3429 if( nSize>(i64)buf.st_size ){ 3700 if( nSize>(i64)buf.st_size ){
3430 3701
3431 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE 3702 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
3432 /* The code below is handling the return value of osFallocate() 3703 /* The code below is handling the return value of osFallocate()
(...skipping 21 matching lines...) Expand all
3454 iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1; 3725 iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1;
3455 while( iWrite<nSize ){ 3726 while( iWrite<nSize ){
3456 int nWrite = seekAndWrite(pFile, iWrite, "", 1); 3727 int nWrite = seekAndWrite(pFile, iWrite, "", 1);
3457 if( nWrite!=1 ) return SQLITE_IOERR_WRITE; 3728 if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
3458 iWrite += nBlk; 3729 iWrite += nBlk;
3459 } 3730 }
3460 #endif 3731 #endif
3461 } 3732 }
3462 } 3733 }
3463 3734
3735 #if SQLITE_MAX_MMAP_SIZE>0
3736 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
3737 int rc;
3738 if( pFile->szChunk<=0 ){
3739 if( robust_ftruncate(pFile->h, nByte) ){
3740 pFile->lastErrno = errno;
3741 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
3742 }
3743 }
3744
3745 rc = unixMapfile(pFile, nByte);
3746 return rc;
3747 }
3748 #endif
3749
3464 return SQLITE_OK; 3750 return SQLITE_OK;
3465 } 3751 }
3466 3752
3467 /* 3753 /*
3754 ** If *pArg is initially negative then this is a query. Set *pArg to
3755 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
3756 **
3757 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
3758 */
3759 static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
3760 if( *pArg<0 ){
3761 *pArg = (pFile->ctrlFlags & mask)!=0;
3762 }else if( (*pArg)==0 ){
3763 pFile->ctrlFlags &= ~mask;
3764 }else{
3765 pFile->ctrlFlags |= mask;
3766 }
3767 }
3768
3769 /* Forward declaration */
3770 static int unixGetTempname(int nBuf, char *zBuf);
3771
3772 /*
3468 ** Information and control of an open file handle. 3773 ** Information and control of an open file handle.
3469 */ 3774 */
3470 static int unixFileControl(sqlite3_file *id, int op, void *pArg){ 3775 static int unixFileControl(sqlite3_file *id, int op, void *pArg){
3776 unixFile *pFile = (unixFile*)id;
3471 switch( op ){ 3777 switch( op ){
3472 case SQLITE_FCNTL_LOCKSTATE: { 3778 case SQLITE_FCNTL_LOCKSTATE: {
3473 *(int*)pArg = ((unixFile*)id)->eFileLock; 3779 *(int*)pArg = pFile->eFileLock;
3474 return SQLITE_OK; 3780 return SQLITE_OK;
3475 } 3781 }
3476 case SQLITE_LAST_ERRNO: { 3782 case SQLITE_LAST_ERRNO: {
3477 *(int*)pArg = ((unixFile*)id)->lastErrno; 3783 *(int*)pArg = pFile->lastErrno;
3478 return SQLITE_OK; 3784 return SQLITE_OK;
3479 } 3785 }
3480 case SQLITE_FCNTL_CHUNK_SIZE: { 3786 case SQLITE_FCNTL_CHUNK_SIZE: {
3481 ((unixFile*)id)->szChunk = *(int *)pArg; 3787 pFile->szChunk = *(int *)pArg;
3482 return SQLITE_OK; 3788 return SQLITE_OK;
3483 } 3789 }
3484 case SQLITE_FCNTL_SIZE_HINT: { 3790 case SQLITE_FCNTL_SIZE_HINT: {
3485 return fcntlSizeHint((unixFile *)id, *(i64 *)pArg); 3791 int rc;
3792 SimulateIOErrorBenign(1);
3793 rc = fcntlSizeHint(pFile, *(i64 *)pArg);
3794 SimulateIOErrorBenign(0);
3795 return rc;
3486 } 3796 }
3487 #ifndef NDEBUG 3797 case SQLITE_FCNTL_PERSIST_WAL: {
3798 unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg);
3799 return SQLITE_OK;
3800 }
3801 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
3802 unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg);
3803 return SQLITE_OK;
3804 }
3805 case SQLITE_FCNTL_VFSNAME: {
3806 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
3807 return SQLITE_OK;
3808 }
3809 case SQLITE_FCNTL_TEMPFILENAME: {
3810 char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname );
3811 if( zTFile ){
3812 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
3813 *(char**)pArg = zTFile;
3814 }
3815 return SQLITE_OK;
3816 }
3817 case SQLITE_FCNTL_HAS_MOVED: {
3818 *(int*)pArg = fileHasMoved(pFile);
3819 return SQLITE_OK;
3820 }
3821 #if SQLITE_MAX_MMAP_SIZE>0
3822 case SQLITE_FCNTL_MMAP_SIZE: {
3823 i64 newLimit = *(i64*)pArg;
3824 int rc = SQLITE_OK;
3825 if( newLimit>sqlite3GlobalConfig.mxMmap ){
3826 newLimit = sqlite3GlobalConfig.mxMmap;
3827 }
3828 *(i64*)pArg = pFile->mmapSizeMax;
3829 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
3830 pFile->mmapSizeMax = newLimit;
3831 if( pFile->mmapSize>0 ){
3832 unixUnmapfile(pFile);
3833 rc = unixMapfile(pFile, -1);
3834 }
3835 }
3836 return rc;
3837 }
3838 #endif
3839 #ifdef SQLITE_DEBUG
3488 /* The pager calls this method to signal that it has done 3840 /* The pager calls this method to signal that it has done
3489 ** a rollback and that the database is therefore unchanged and 3841 ** a rollback and that the database is therefore unchanged and
3490 ** it hence it is OK for the transaction change counter to be 3842 ** it hence it is OK for the transaction change counter to be
3491 ** unchanged. 3843 ** unchanged.
3492 */ 3844 */
3493 case SQLITE_FCNTL_DB_UNCHANGED: { 3845 case SQLITE_FCNTL_DB_UNCHANGED: {
3494 ((unixFile*)id)->dbUpdate = 0; 3846 ((unixFile*)id)->dbUpdate = 0;
3495 return SQLITE_OK; 3847 return SQLITE_OK;
3496 } 3848 }
3497 #endif 3849 #endif
3498 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) 3850 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
3499 case SQLITE_SET_LOCKPROXYFILE: 3851 case SQLITE_SET_LOCKPROXYFILE:
3500 case SQLITE_GET_LOCKPROXYFILE: { 3852 case SQLITE_GET_LOCKPROXYFILE: {
3501 return proxyFileControl(id,op,pArg); 3853 return proxyFileControl(id,op,pArg);
3502 } 3854 }
3503 #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ 3855 #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
3504 case SQLITE_FCNTL_SYNC_OMITTED: {
3505 return SQLITE_OK; /* A no-op */
3506 }
3507 } 3856 }
3508 return SQLITE_NOTFOUND; 3857 return SQLITE_NOTFOUND;
3509 } 3858 }
3510 3859
3511 /* 3860 /*
3512 ** Return the sector size in bytes of the underlying block device for 3861 ** Return the sector size in bytes of the underlying block device for
3513 ** the specified file. This is almost always 512 bytes, but may be 3862 ** the specified file. This is almost always 512 bytes, but may be
3514 ** larger for some devices. 3863 ** larger for some devices.
3515 ** 3864 **
3516 ** SQLite code assumes this function cannot fail. It also assumes that 3865 ** SQLite code assumes this function cannot fail. It also assumes that
3517 ** if two files are created in the same file-system directory (i.e. 3866 ** if two files are created in the same file-system directory (i.e.
3518 ** a database and its journal file) that the sector size will be the 3867 ** a database and its journal file) that the sector size will be the
3519 ** same for both. 3868 ** same for both.
3520 */ 3869 */
3870 #ifndef __QNXNTO__
3521 static int unixSectorSize(sqlite3_file *NotUsed){ 3871 static int unixSectorSize(sqlite3_file *NotUsed){
3522 UNUSED_PARAMETER(NotUsed); 3872 UNUSED_PARAMETER(NotUsed);
3523 return SQLITE_DEFAULT_SECTOR_SIZE; 3873 return SQLITE_DEFAULT_SECTOR_SIZE;
3524 } 3874 }
3875 #endif
3525 3876
3526 /* 3877 /*
3527 ** Return the device characteristics for the file. This is always 0 for unix. 3878 ** The following version of unixSectorSize() is optimized for QNX.
3528 */ 3879 */
3529 static int unixDeviceCharacteristics(sqlite3_file *NotUsed){ 3880 #ifdef __QNXNTO__
3530 UNUSED_PARAMETER(NotUsed); 3881 #include <sys/dcmd_blk.h>
3531 return 0; 3882 #include <sys/statvfs.h>
3883 static int unixSectorSize(sqlite3_file *id){
3884 unixFile *pFile = (unixFile*)id;
3885 if( pFile->sectorSize == 0 ){
3886 struct statvfs fsInfo;
3887
3888 /* Set defaults for non-supported filesystems */
3889 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
3890 pFile->deviceCharacteristics = 0;
3891 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
3892 return pFile->sectorSize;
3893 }
3894
3895 if( !strcmp(fsInfo.f_basetype, "tmp") ) {
3896 pFile->sectorSize = fsInfo.f_bsize;
3897 pFile->deviceCharacteristics =
3898 SQLITE_IOCAP_ATOMIC4K | /* All ram filesystem writes are atomic */
3899 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
3900 ** the write succeeds */
3901 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
3902 ** so it is ordered */
3903 0;
3904 }else if( strstr(fsInfo.f_basetype, "etfs") ){
3905 pFile->sectorSize = fsInfo.f_bsize;
3906 pFile->deviceCharacteristics =
3907 /* etfs cluster size writes are atomic */
3908 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
3909 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
3910 ** the write succeeds */
3911 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
3912 ** so it is ordered */
3913 0;
3914 }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){
3915 pFile->sectorSize = fsInfo.f_bsize;
3916 pFile->deviceCharacteristics =
3917 SQLITE_IOCAP_ATOMIC | /* All filesystem writes are atomic */
3918 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
3919 ** the write succeeds */
3920 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
3921 ** so it is ordered */
3922 0;
3923 }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
3924 pFile->sectorSize = fsInfo.f_bsize;
3925 pFile->deviceCharacteristics =
3926 /* full bitset of atomics from max sector size and smaller */
3927 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
3928 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
3929 ** so it is ordered */
3930 0;
3931 }else if( strstr(fsInfo.f_basetype, "dos") ){
3932 pFile->sectorSize = fsInfo.f_bsize;
3933 pFile->deviceCharacteristics =
3934 /* full bitset of atomics from max sector size and smaller */
3935 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
3936 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
3937 ** so it is ordered */
3938 0;
3939 }else{
3940 pFile->deviceCharacteristics =
3941 SQLITE_IOCAP_ATOMIC512 | /* blocks are atomic */
3942 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
3943 ** the write succeeds */
3944 0;
3945 }
3946 }
3947 /* Last chance verification. If the sector size isn't a multiple of 512
3948 ** then it isn't valid.*/
3949 if( pFile->sectorSize % 512 != 0 ){
3950 pFile->deviceCharacteristics = 0;
3951 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
3952 }
3953 return pFile->sectorSize;
3532 } 3954 }
3955 #endif /* __QNXNTO__ */
3956
3957 /*
3958 ** Return the device characteristics for the file.
3959 **
3960 ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
3961 ** However, that choice is controversial since technically the underlying
3962 ** file system does not always provide powersafe overwrites. (In other
3963 ** words, after a power-loss event, parts of the file that were never
3964 ** written might end up being altered.) However, non-PSOW behavior is very,
3965 ** very rare. And asserting PSOW makes a large reduction in the amount
3966 ** of required I/O for journaling, since a lot of padding is eliminated.
3967 ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
3968 ** available to turn it off and URI query parameter available to turn it off.
3969 */
3970 static int unixDeviceCharacteristics(sqlite3_file *id){
3971 unixFile *p = (unixFile*)id;
3972 int rc = 0;
3973 #ifdef __QNXNTO__
3974 if( p->sectorSize==0 ) unixSectorSize(id);
3975 rc = p->deviceCharacteristics;
3976 #endif
3977 if( p->ctrlFlags & UNIXFILE_PSOW ){
3978 rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
3979 }
3980 return rc;
3981 }
3982
3983 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
3984
3985 /*
3986 ** Return the system page size.
3987 **
3988 ** This function should not be called directly by other code in this file.
3989 ** Instead, it should be called via macro osGetpagesize().
3990 */
3991 static int unixGetpagesize(void){
3992 #if defined(_BSD_SOURCE)
3993 return getpagesize();
3994 #else
3995 return (int)sysconf(_SC_PAGESIZE);
3996 #endif
3997 }
3998
3999 #endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */
3533 4000
3534 #ifndef SQLITE_OMIT_WAL 4001 #ifndef SQLITE_OMIT_WAL
3535 4002
3536
3537 /* 4003 /*
3538 ** Object used to represent an shared memory buffer. 4004 ** Object used to represent an shared memory buffer.
3539 ** 4005 **
3540 ** When multiple threads all reference the same wal-index, each thread 4006 ** When multiple threads all reference the same wal-index, each thread
3541 ** has its own unixShm object, but they all point to a single instance 4007 ** has its own unixShm object, but they all point to a single instance
3542 ** of this unixShmNode object. In other words, each wal-index is opened 4008 ** of this unixShmNode object. In other words, each wal-index is opened
3543 ** only once per process. 4009 ** only once per process.
3544 ** 4010 **
3545 ** Each unixShmNode object is connected to a single unixInodeInfo object. 4011 ** Each unixShmNode object is connected to a single unixInodeInfo object.
3546 ** We could coalesce this object into unixInodeInfo, but that would mean 4012 ** We could coalesce this object into unixInodeInfo, but that would mean
(...skipping 15 matching lines...) Expand all
3562 ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and 4028 ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
3563 ** unixMutexHeld() is true when reading or writing any other field 4029 ** unixMutexHeld() is true when reading or writing any other field
3564 ** in this structure. 4030 ** in this structure.
3565 */ 4031 */
3566 struct unixShmNode { 4032 struct unixShmNode {
3567 unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ 4033 unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */
3568 sqlite3_mutex *mutex; /* Mutex to access this object */ 4034 sqlite3_mutex *mutex; /* Mutex to access this object */
3569 char *zFilename; /* Name of the mmapped file */ 4035 char *zFilename; /* Name of the mmapped file */
3570 int h; /* Open file descriptor */ 4036 int h; /* Open file descriptor */
3571 int szRegion; /* Size of shared-memory regions */ 4037 int szRegion; /* Size of shared-memory regions */
3572 int nRegion; /* Size of array apRegion */ 4038 u16 nRegion; /* Size of array apRegion */
4039 u8 isReadonly; /* True if read-only */
3573 char **apRegion; /* Array of mapped shared-memory regions */ 4040 char **apRegion; /* Array of mapped shared-memory regions */
3574 int nRef; /* Number of unixShm objects pointing to this */ 4041 int nRef; /* Number of unixShm objects pointing to this */
3575 unixShm *pFirst; /* All unixShm objects pointing to this */ 4042 unixShm *pFirst; /* All unixShm objects pointing to this */
3576 #ifdef SQLITE_DEBUG 4043 #ifdef SQLITE_DEBUG
3577 u8 exclMask; /* Mask of exclusive locks held */ 4044 u8 exclMask; /* Mask of exclusive locks held */
3578 u8 sharedMask; /* Mask of shared locks held */ 4045 u8 sharedMask; /* Mask of shared locks held */
3579 u8 nextShmId; /* Next available unixShm.id value */ 4046 u8 nextShmId; /* Next available unixShm.id value */
3580 #endif 4047 #endif
3581 }; 4048 };
3582 4049
3583 /* 4050 /*
3584 ** Structure used internally by this VFS to record the state of an 4051 ** Structure used internally by this VFS to record the state of an
3585 ** open shared memory connection. 4052 ** open shared memory connection.
3586 ** 4053 **
3587 ** The following fields are initialized when this object is created and 4054 ** The following fields are initialized when this object is created and
3588 ** are read-only thereafter: 4055 ** are read-only thereafter:
3589 ** 4056 **
3590 ** unixShm.pFile 4057 ** unixShm.pFile
3591 ** unixShm.id 4058 ** unixShm.id
3592 ** 4059 **
3593 ** All other fields are read/write. The unixShm.pFile->mutex must be held 4060 ** All other fields are read/write. The unixShm.pFile->mutex must be held
3594 ** while accessing any read/write fields. 4061 ** while accessing any read/write fields.
3595 */ 4062 */
3596 struct unixShm { 4063 struct unixShm {
3597 unixShmNode *pShmNode; /* The underlying unixShmNode object */ 4064 unixShmNode *pShmNode; /* The underlying unixShmNode object */
3598 unixShm *pNext; /* Next unixShm with the same unixShmNode */ 4065 unixShm *pNext; /* Next unixShm with the same unixShmNode */
3599 u8 hasMutex; /* True if holding the unixShmNode mutex */ 4066 u8 hasMutex; /* True if holding the unixShmNode mutex */
4067 u8 id; /* Id of this connection within its unixShmNode */
3600 u16 sharedMask; /* Mask of shared locks held */ 4068 u16 sharedMask; /* Mask of shared locks held */
3601 u16 exclMask; /* Mask of exclusive locks held */ 4069 u16 exclMask; /* Mask of exclusive locks held */
3602 #ifdef SQLITE_DEBUG
3603 u8 id; /* Id of this connection within its unixShmNode */
3604 #endif
3605 }; 4070 };
3606 4071
3607 /* 4072 /*
3608 ** Constants used for locking 4073 ** Constants used for locking
3609 */ 4074 */
3610 #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ 4075 #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
3611 #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ 4076 #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
3612 4077
3613 /* 4078 /*
3614 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1. 4079 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
(...skipping 28 matching lines...) Expand all
3643 f.l_len = n; 4108 f.l_len = n;
3644 4109
3645 rc = osFcntl(pShmNode->h, F_SETLK, &f); 4110 rc = osFcntl(pShmNode->h, F_SETLK, &f);
3646 rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; 4111 rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
3647 } 4112 }
3648 4113
3649 /* Update the global lock state and do debug tracing */ 4114 /* Update the global lock state and do debug tracing */
3650 #ifdef SQLITE_DEBUG 4115 #ifdef SQLITE_DEBUG
3651 { u16 mask; 4116 { u16 mask;
3652 OSTRACE(("SHM-LOCK ")); 4117 OSTRACE(("SHM-LOCK "));
3653 mask = (1<<(ofst+n)) - (1<<ofst); 4118 mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
3654 if( rc==SQLITE_OK ){ 4119 if( rc==SQLITE_OK ){
3655 if( lockType==F_UNLCK ){ 4120 if( lockType==F_UNLCK ){
3656 OSTRACE(("unlock %d ok", ofst)); 4121 OSTRACE(("unlock %d ok", ofst));
3657 pShmNode->exclMask &= ~mask; 4122 pShmNode->exclMask &= ~mask;
3658 pShmNode->sharedMask &= ~mask; 4123 pShmNode->sharedMask &= ~mask;
3659 }else if( lockType==F_RDLCK ){ 4124 }else if( lockType==F_RDLCK ){
3660 OSTRACE(("read-lock %d ok", ofst)); 4125 OSTRACE(("read-lock %d ok", ofst));
3661 pShmNode->exclMask &= ~mask; 4126 pShmNode->exclMask &= ~mask;
3662 pShmNode->sharedMask |= mask; 4127 pShmNode->sharedMask |= mask;
3663 }else{ 4128 }else{
(...skipping 13 matching lines...) Expand all
3677 } 4142 }
3678 } 4143 }
3679 OSTRACE((" - afterwards %03x,%03x\n", 4144 OSTRACE((" - afterwards %03x,%03x\n",
3680 pShmNode->sharedMask, pShmNode->exclMask)); 4145 pShmNode->sharedMask, pShmNode->exclMask));
3681 } 4146 }
3682 #endif 4147 #endif
3683 4148
3684 return rc; 4149 return rc;
3685 } 4150 }
3686 4151
4152 /*
4153 ** Return the minimum number of 32KB shm regions that should be mapped at
4154 ** a time, assuming that each mapping must be an integer multiple of the
4155 ** current system page-size.
4156 **
4157 ** Usually, this is 1. The exception seems to be systems that are configured
4158 ** to use 64KB pages - in this case each mapping must cover at least two
4159 ** shm regions.
4160 */
4161 static int unixShmRegionPerMap(void){
4162 int shmsz = 32*1024; /* SHM region size */
4163 int pgsz = osGetpagesize(); /* System page size */
4164 assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */
4165 if( pgsz<shmsz ) return 1;
4166 return pgsz/shmsz;
4167 }
3687 4168
3688 /* 4169 /*
3689 ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0. 4170 ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
3690 ** 4171 **
3691 ** This is not a VFS shared-memory method; it is a utility function called 4172 ** This is not a VFS shared-memory method; it is a utility function called
3692 ** by VFS shared-memory methods. 4173 ** by VFS shared-memory methods.
3693 */ 4174 */
3694 static void unixShmPurge(unixFile *pFd){ 4175 static void unixShmPurge(unixFile *pFd){
3695 unixShmNode *p = pFd->pInode->pShmNode; 4176 unixShmNode *p = pFd->pInode->pShmNode;
3696 assert( unixMutexHeld() ); 4177 assert( unixMutexHeld() );
3697 if( p && p->nRef==0 ){ 4178 if( p && p->nRef==0 ){
4179 int nShmPerMap = unixShmRegionPerMap();
3698 int i; 4180 int i;
3699 assert( p->pInode==pFd->pInode ); 4181 assert( p->pInode==pFd->pInode );
3700 if( p->mutex ) sqlite3_mutex_free(p->mutex); 4182 sqlite3_mutex_free(p->mutex);
3701 for(i=0; i<p->nRegion; i++){ 4183 for(i=0; i<p->nRegion; i+=nShmPerMap){
3702 if( p->h>=0 ){ 4184 if( p->h>=0 ){
3703 munmap(p->apRegion[i], p->szRegion); 4185 osMunmap(p->apRegion[i], p->szRegion);
3704 }else{ 4186 }else{
3705 sqlite3_free(p->apRegion[i]); 4187 sqlite3_free(p->apRegion[i]);
3706 } 4188 }
3707 } 4189 }
3708 sqlite3_free(p->apRegion); 4190 sqlite3_free(p->apRegion);
3709 if( p->h>=0 ){ 4191 if( p->h>=0 ){
3710 robust_close(pFd, p->h, __LINE__); 4192 robust_close(pFd, p->h, __LINE__);
3711 p->h = -1; 4193 p->h = -1;
3712 } 4194 }
3713 p->pInode->pShmNode = 0; 4195 p->pInode->pShmNode = 0;
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
3768 ** one if present. Create a new one if necessary. 4250 ** one if present. Create a new one if necessary.
3769 */ 4251 */
3770 unixEnterMutex(); 4252 unixEnterMutex();
3771 pInode = pDbFd->pInode; 4253 pInode = pDbFd->pInode;
3772 pShmNode = pInode->pShmNode; 4254 pShmNode = pInode->pShmNode;
3773 if( pShmNode==0 ){ 4255 if( pShmNode==0 ){
3774 struct stat sStat; /* fstat() info for database file */ 4256 struct stat sStat; /* fstat() info for database file */
3775 4257
3776 /* Call fstat() to figure out the permissions on the database file. If 4258 /* Call fstat() to figure out the permissions on the database file. If
3777 ** a new *-shm file is created, an attempt will be made to create it 4259 ** a new *-shm file is created, an attempt will be made to create it
3778 ** with the same permissions. The actual permissions the file is created 4260 ** with the same permissions.
3779 ** with are subject to the current umask setting.
3780 */ 4261 */
3781 if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){ 4262 if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){
3782 rc = SQLITE_IOERR_FSTAT; 4263 rc = SQLITE_IOERR_FSTAT;
3783 goto shm_open_err; 4264 goto shm_open_err;
3784 } 4265 }
3785 4266
3786 #ifdef SQLITE_SHM_DIRECTORY 4267 #ifdef SQLITE_SHM_DIRECTORY
3787 nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 30; 4268 nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
3788 #else 4269 #else
3789 nShmFilename = 5 + (int)strlen(pDbFd->zPath); 4270 nShmFilename = 6 + (int)strlen(pDbFd->zPath);
3790 #endif 4271 #endif
3791 pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename ); 4272 pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename );
3792 if( pShmNode==0 ){ 4273 if( pShmNode==0 ){
3793 rc = SQLITE_NOMEM; 4274 rc = SQLITE_NOMEM;
3794 goto shm_open_err; 4275 goto shm_open_err;
3795 } 4276 }
3796 memset(pShmNode, 0, sizeof(*pShmNode)); 4277 memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
3797 zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1]; 4278 zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
3798 #ifdef SQLITE_SHM_DIRECTORY 4279 #ifdef SQLITE_SHM_DIRECTORY
3799 sqlite3_snprintf(nShmFilename, zShmFilename, 4280 sqlite3_snprintf(nShmFilename, zShmFilename,
3800 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x", 4281 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
3801 (u32)sStat.st_ino, (u32)sStat.st_dev); 4282 (u32)sStat.st_ino, (u32)sStat.st_dev);
3802 #else 4283 #else
3803 sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath); 4284 sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath);
4285 sqlite3FileSuffix3(pDbFd->zPath, zShmFilename);
3804 #endif 4286 #endif
3805 pShmNode->h = -1; 4287 pShmNode->h = -1;
3806 pDbFd->pInode->pShmNode = pShmNode; 4288 pDbFd->pInode->pShmNode = pShmNode;
3807 pShmNode->pInode = pDbFd->pInode; 4289 pShmNode->pInode = pDbFd->pInode;
3808 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); 4290 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
3809 if( pShmNode->mutex==0 ){ 4291 if( pShmNode->mutex==0 ){
3810 rc = SQLITE_NOMEM; 4292 rc = SQLITE_NOMEM;
3811 goto shm_open_err; 4293 goto shm_open_err;
3812 } 4294 }
3813 4295
3814 if( pInode->bProcessLock==0 ){ 4296 if( pInode->bProcessLock==0 ){
3815 pShmNode->h = robust_open(zShmFilename, O_RDWR|O_CREAT, 4297 int openFlags = O_RDWR | O_CREAT;
3816 (sStat.st_mode & 0777)); 4298 if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
4299 openFlags = O_RDONLY;
4300 pShmNode->isReadonly = 1;
4301 }
4302 pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777));
3817 if( pShmNode->h<0 ){ 4303 if( pShmNode->h<0 ){
3818 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename); 4304 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
3819 goto shm_open_err; 4305 goto shm_open_err;
3820 } 4306 }
4307
4308 /* If this process is running as root, make sure that the SHM file
4309 ** is owned by the same user that owns the original database. Otherwise,
4310 ** the original owner will not be able to connect.
4311 */
4312 osFchown(pShmNode->h, sStat.st_uid, sStat.st_gid);
3821 4313
3822 /* Check to see if another process is holding the dead-man switch. 4314 /* Check to see if another process is holding the dead-man switch.
3823 ** If not, truncate the file to zero length. 4315 ** If not, truncate the file to zero length.
3824 */ 4316 */
3825 rc = SQLITE_OK; 4317 rc = SQLITE_OK;
3826 if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){ 4318 if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
3827 if( robust_ftruncate(pShmNode->h, 0) ){ 4319 if( robust_ftruncate(pShmNode->h, 0) ){
3828 rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename); 4320 rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
3829 } 4321 }
3830 } 4322 }
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
3888 sqlite3_file *fd, /* Handle open on database file */ 4380 sqlite3_file *fd, /* Handle open on database file */
3889 int iRegion, /* Region to retrieve */ 4381 int iRegion, /* Region to retrieve */
3890 int szRegion, /* Size of regions */ 4382 int szRegion, /* Size of regions */
3891 int bExtend, /* True to extend file if necessary */ 4383 int bExtend, /* True to extend file if necessary */
3892 void volatile **pp /* OUT: Mapped memory */ 4384 void volatile **pp /* OUT: Mapped memory */
3893 ){ 4385 ){
3894 unixFile *pDbFd = (unixFile*)fd; 4386 unixFile *pDbFd = (unixFile*)fd;
3895 unixShm *p; 4387 unixShm *p;
3896 unixShmNode *pShmNode; 4388 unixShmNode *pShmNode;
3897 int rc = SQLITE_OK; 4389 int rc = SQLITE_OK;
4390 int nShmPerMap = unixShmRegionPerMap();
4391 int nReqRegion;
3898 4392
3899 /* If the shared-memory file has not yet been opened, open it now. */ 4393 /* If the shared-memory file has not yet been opened, open it now. */
3900 if( pDbFd->pShm==0 ){ 4394 if( pDbFd->pShm==0 ){
3901 rc = unixOpenSharedMemory(pDbFd); 4395 rc = unixOpenSharedMemory(pDbFd);
3902 if( rc!=SQLITE_OK ) return rc; 4396 if( rc!=SQLITE_OK ) return rc;
3903 } 4397 }
3904 4398
3905 p = pDbFd->pShm; 4399 p = pDbFd->pShm;
3906 pShmNode = p->pShmNode; 4400 pShmNode = p->pShmNode;
3907 sqlite3_mutex_enter(pShmNode->mutex); 4401 sqlite3_mutex_enter(pShmNode->mutex);
3908 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); 4402 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
3909 assert( pShmNode->pInode==pDbFd->pInode ); 4403 assert( pShmNode->pInode==pDbFd->pInode );
3910 assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); 4404 assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
3911 assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); 4405 assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
3912 4406
3913 if( pShmNode->nRegion<=iRegion ){ 4407 /* Minimum number of regions required to be mapped. */
4408 nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
4409
4410 if( pShmNode->nRegion<nReqRegion ){
3914 char **apNew; /* New apRegion[] array */ 4411 char **apNew; /* New apRegion[] array */
3915 int nByte = (iRegion+1)*szRegion; /* Minimum required file size */ 4412 int nByte = nReqRegion*szRegion; /* Minimum required file size */
3916 struct stat sStat; /* Used by fstat() */ 4413 struct stat sStat; /* Used by fstat() */
3917 4414
3918 pShmNode->szRegion = szRegion; 4415 pShmNode->szRegion = szRegion;
3919 4416
3920 if( pShmNode->h>=0 ){ 4417 if( pShmNode->h>=0 ){
3921 /* The requested region is not mapped into this processes address space. 4418 /* The requested region is not mapped into this processes address space.
3922 ** Check to see if it has been allocated (i.e. if the wal-index file is 4419 ** Check to see if it has been allocated (i.e. if the wal-index file is
3923 ** large enough to contain the requested region). 4420 ** large enough to contain the requested region).
3924 */ 4421 */
3925 if( osFstat(pShmNode->h, &sStat) ){ 4422 if( osFstat(pShmNode->h, &sStat) ){
3926 rc = SQLITE_IOERR_SHMSIZE; 4423 rc = SQLITE_IOERR_SHMSIZE;
3927 goto shmpage_out; 4424 goto shmpage_out;
3928 } 4425 }
3929 4426
3930 if( sStat.st_size<nByte ){ 4427 if( sStat.st_size<nByte ){
3931 /* The requested memory region does not exist. If bExtend is set to 4428 /* The requested memory region does not exist. If bExtend is set to
3932 ** false, exit early. *pp will be set to NULL and SQLITE_OK returned. 4429 ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
3933 **
3934 ** Alternatively, if bExtend is true, use ftruncate() to allocate
3935 ** the requested memory region.
3936 */ 4430 */
3937 if( !bExtend ) goto shmpage_out; 4431 if( !bExtend ){
3938 if( robust_ftruncate(pShmNode->h, nByte) ){
3939 rc = unixLogError(SQLITE_IOERR_SHMSIZE, "ftruncate",
3940 pShmNode->zFilename);
3941 goto shmpage_out; 4432 goto shmpage_out;
3942 } 4433 }
4434
4435 /* Alternatively, if bExtend is true, extend the file. Do this by
4436 ** writing a single byte to the end of each (OS) page being
4437 ** allocated or extended. Technically, we need only write to the
4438 ** last page in order to extend the file. But writing to all new
4439 ** pages forces the OS to allocate them immediately, which reduces
4440 ** the chances of SIGBUS while accessing the mapped region later on.
4441 */
4442 else{
4443 static const int pgsz = 4096;
4444 int iPg;
4445
4446 /* Write to the last byte of each newly allocated or extended page */
4447 assert( (nByte % pgsz)==0 );
4448 for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
4449 if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, 0)!=1 ){
4450 const char *zFile = pShmNode->zFilename;
4451 rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile);
4452 goto shmpage_out;
4453 }
4454 }
4455 }
3943 } 4456 }
3944 } 4457 }
3945 4458
3946 /* Map the requested memory region into this processes address space. */ 4459 /* Map the requested memory region into this processes address space. */
3947 apNew = (char **)sqlite3_realloc( 4460 apNew = (char **)sqlite3_realloc(
3948 pShmNode->apRegion, (iRegion+1)*sizeof(char *) 4461 pShmNode->apRegion, nReqRegion*sizeof(char *)
3949 ); 4462 );
3950 if( !apNew ){ 4463 if( !apNew ){
3951 rc = SQLITE_IOERR_NOMEM; 4464 rc = SQLITE_IOERR_NOMEM;
3952 goto shmpage_out; 4465 goto shmpage_out;
3953 } 4466 }
3954 pShmNode->apRegion = apNew; 4467 pShmNode->apRegion = apNew;
3955 while(pShmNode->nRegion<=iRegion){ 4468 while( pShmNode->nRegion<nReqRegion ){
4469 int nMap = szRegion*nShmPerMap;
4470 int i;
3956 void *pMem; 4471 void *pMem;
3957 if( pShmNode->h>=0 ){ 4472 if( pShmNode->h>=0 ){
3958 pMem = mmap(0, szRegion, PROT_READ|PROT_WRITE, 4473 pMem = osMmap(0, nMap,
3959 MAP_SHARED, pShmNode->h, pShmNode->nRegion*szRegion 4474 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
4475 MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
3960 ); 4476 );
3961 if( pMem==MAP_FAILED ){ 4477 if( pMem==MAP_FAILED ){
3962 rc = SQLITE_IOERR; 4478 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
3963 goto shmpage_out; 4479 goto shmpage_out;
3964 } 4480 }
3965 }else{ 4481 }else{
3966 pMem = sqlite3_malloc(szRegion); 4482 pMem = sqlite3_malloc(szRegion);
3967 if( pMem==0 ){ 4483 if( pMem==0 ){
3968 rc = SQLITE_NOMEM; 4484 rc = SQLITE_NOMEM;
3969 goto shmpage_out; 4485 goto shmpage_out;
3970 } 4486 }
3971 memset(pMem, 0, szRegion); 4487 memset(pMem, 0, szRegion);
3972 } 4488 }
3973 pShmNode->apRegion[pShmNode->nRegion] = pMem; 4489
3974 pShmNode->nRegion++; 4490 for(i=0; i<nShmPerMap; i++){
4491 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
4492 }
4493 pShmNode->nRegion += nShmPerMap;
3975 } 4494 }
3976 } 4495 }
3977 4496
3978 shmpage_out: 4497 shmpage_out:
3979 if( pShmNode->nRegion>iRegion ){ 4498 if( pShmNode->nRegion>iRegion ){
3980 *pp = pShmNode->apRegion[iRegion]; 4499 *pp = pShmNode->apRegion[iRegion];
3981 }else{ 4500 }else{
3982 *pp = 0; 4501 *pp = 0;
3983 } 4502 }
4503 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
3984 sqlite3_mutex_leave(pShmNode->mutex); 4504 sqlite3_mutex_leave(pShmNode->mutex);
3985 return rc; 4505 return rc;
3986 } 4506 }
3987 4507
3988 /* 4508 /*
3989 ** Change the lock state for a shared-memory segment. 4509 ** Change the lock state for a shared-memory segment.
3990 ** 4510 **
3991 ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little 4511 ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
3992 ** different here than in posix. In xShmLock(), one can go from unlocked 4512 ** different here than in posix. In xShmLock(), one can go from unlocked
3993 ** to shared and back or from unlocked to exclusive and back. But one may 4513 ** to shared and back or from unlocked to exclusive and back. But one may
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
4163 } 4683 }
4164 4684
4165 4685
4166 #else 4686 #else
4167 # define unixShmMap 0 4687 # define unixShmMap 0
4168 # define unixShmLock 0 4688 # define unixShmLock 0
4169 # define unixShmBarrier 0 4689 # define unixShmBarrier 0
4170 # define unixShmUnmap 0 4690 # define unixShmUnmap 0
4171 #endif /* #ifndef SQLITE_OMIT_WAL */ 4691 #endif /* #ifndef SQLITE_OMIT_WAL */
4172 4692
4693 #if SQLITE_MAX_MMAP_SIZE>0
4694 /*
4695 ** If it is currently memory mapped, unmap file pFd.
4696 */
4697 static void unixUnmapfile(unixFile *pFd){
4698 assert( pFd->nFetchOut==0 );
4699 if( pFd->pMapRegion ){
4700 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
4701 pFd->pMapRegion = 0;
4702 pFd->mmapSize = 0;
4703 pFd->mmapSizeActual = 0;
4704 }
4705 }
4706
4707 /*
4708 ** Attempt to set the size of the memory mapping maintained by file
4709 ** descriptor pFd to nNew bytes. Any existing mapping is discarded.
4710 **
4711 ** If successful, this function sets the following variables:
4712 **
4713 ** unixFile.pMapRegion
4714 ** unixFile.mmapSize
4715 ** unixFile.mmapSizeActual
4716 **
4717 ** If unsuccessful, an error message is logged via sqlite3_log() and
4718 ** the three variables above are zeroed. In this case SQLite should
4719 ** continue accessing the database using the xRead() and xWrite()
4720 ** methods.
4721 */
4722 static void unixRemapfile(
4723 unixFile *pFd, /* File descriptor object */
4724 i64 nNew /* Required mapping size */
4725 ){
4726 const char *zErr = "mmap";
4727 int h = pFd->h; /* File descriptor open on db file */
4728 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
4729 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
4730 u8 *pNew = 0; /* Location of new mapping */
4731 int flags = PROT_READ; /* Flags to pass to mmap() */
4732
4733 assert( pFd->nFetchOut==0 );
4734 assert( nNew>pFd->mmapSize );
4735 assert( nNew<=pFd->mmapSizeMax );
4736 assert( nNew>0 );
4737 assert( pFd->mmapSizeActual>=pFd->mmapSize );
4738 assert( MAP_FAILED!=0 );
4739
4740 if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
4741
4742 if( pOrig ){
4743 #if HAVE_MREMAP
4744 i64 nReuse = pFd->mmapSize;
4745 #else
4746 const int szSyspage = osGetpagesize();
4747 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
4748 #endif
4749 u8 *pReq = &pOrig[nReuse];
4750
4751 /* Unmap any pages of the existing mapping that cannot be reused. */
4752 if( nReuse!=nOrig ){
4753 osMunmap(pReq, nOrig-nReuse);
4754 }
4755
4756 #if HAVE_MREMAP
4757 pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE);
4758 zErr = "mremap";
4759 #else
4760 pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
4761 if( pNew!=MAP_FAILED ){
4762 if( pNew!=pReq ){
4763 osMunmap(pNew, nNew - nReuse);
4764 pNew = 0;
4765 }else{
4766 pNew = pOrig;
4767 }
4768 }
4769 #endif
4770
4771 /* The attempt to extend the existing mapping failed. Free it. */
4772 if( pNew==MAP_FAILED || pNew==0 ){
4773 osMunmap(pOrig, nReuse);
4774 }
4775 }
4776
4777 /* If pNew is still NULL, try to create an entirely new mapping. */
4778 if( pNew==0 ){
4779 pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0);
4780 }
4781
4782 if( pNew==MAP_FAILED ){
4783 pNew = 0;
4784 nNew = 0;
4785 unixLogError(SQLITE_OK, zErr, pFd->zPath);
4786
4787 /* If the mmap() above failed, assume that all subsequent mmap() calls
4788 ** will probably fail too. Fall back to using xRead/xWrite exclusively
4789 ** in this case. */
4790 pFd->mmapSizeMax = 0;
4791 }
4792 pFd->pMapRegion = (void *)pNew;
4793 pFd->mmapSize = pFd->mmapSizeActual = nNew;
4794 }
4795
4796 /*
4797 ** Memory map or remap the file opened by file-descriptor pFd (if the file
4798 ** is already mapped, the existing mapping is replaced by the new). Or, if
4799 ** there already exists a mapping for this file, and there are still
4800 ** outstanding xFetch() references to it, this function is a no-op.
4801 **
4802 ** If parameter nByte is non-negative, then it is the requested size of
4803 ** the mapping to create. Otherwise, if nByte is less than zero, then the
4804 ** requested size is the size of the file on disk. The actual size of the
4805 ** created mapping is either the requested size or the value configured
4806 ** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller.
4807 **
4808 ** SQLITE_OK is returned if no error occurs (even if the mapping is not
4809 ** recreated as a result of outstanding references) or an SQLite error
4810 ** code otherwise.
4811 */
4812 static int unixMapfile(unixFile *pFd, i64 nByte){
4813 i64 nMap = nByte;
4814 int rc;
4815
4816 assert( nMap>=0 || pFd->nFetchOut==0 );
4817 if( pFd->nFetchOut>0 ) return SQLITE_OK;
4818
4819 if( nMap<0 ){
4820 struct stat statbuf; /* Low-level file information */
4821 rc = osFstat(pFd->h, &statbuf);
4822 if( rc!=SQLITE_OK ){
4823 return SQLITE_IOERR_FSTAT;
4824 }
4825 nMap = statbuf.st_size;
4826 }
4827 if( nMap>pFd->mmapSizeMax ){
4828 nMap = pFd->mmapSizeMax;
4829 }
4830
4831 if( nMap!=pFd->mmapSize ){
4832 if( nMap>0 ){
4833 unixRemapfile(pFd, nMap);
4834 }else{
4835 unixUnmapfile(pFd);
4836 }
4837 }
4838
4839 return SQLITE_OK;
4840 }
4841 #endif /* SQLITE_MAX_MMAP_SIZE>0 */
4842
4843 /*
4844 ** If possible, return a pointer to a mapping of file fd starting at offset
4845 ** iOff. The mapping must be valid for at least nAmt bytes.
4846 **
4847 ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
4848 ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
4849 ** Finally, if an error does occur, return an SQLite error code. The final
4850 ** value of *pp is undefined in this case.
4851 **
4852 ** If this function does return a pointer, the caller must eventually
4853 ** release the reference by calling unixUnfetch().
4854 */
4855 static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
4856 #if SQLITE_MAX_MMAP_SIZE>0
4857 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
4858 #endif
4859 *pp = 0;
4860
4861 #if SQLITE_MAX_MMAP_SIZE>0
4862 if( pFd->mmapSizeMax>0 ){
4863 if( pFd->pMapRegion==0 ){
4864 int rc = unixMapfile(pFd, -1);
4865 if( rc!=SQLITE_OK ) return rc;
4866 }
4867 if( pFd->mmapSize >= iOff+nAmt ){
4868 *pp = &((u8 *)pFd->pMapRegion)[iOff];
4869 pFd->nFetchOut++;
4870 }
4871 }
4872 #endif
4873 return SQLITE_OK;
4874 }
4875
4876 /*
4877 ** If the third argument is non-NULL, then this function releases a
4878 ** reference obtained by an earlier call to unixFetch(). The second
4879 ** argument passed to this function must be the same as the corresponding
4880 ** argument that was passed to the unixFetch() invocation.
4881 **
4882 ** Or, if the third argument is NULL, then this function is being called
4883 ** to inform the VFS layer that, according to POSIX, any existing mapping
4884 ** may now be invalid and should be unmapped.
4885 */
4886 static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
4887 #if SQLITE_MAX_MMAP_SIZE>0
4888 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
4889 UNUSED_PARAMETER(iOff);
4890
4891 /* If p==0 (unmap the entire file) then there must be no outstanding
4892 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
4893 ** then there must be at least one outstanding. */
4894 assert( (p==0)==(pFd->nFetchOut==0) );
4895
4896 /* If p!=0, it must match the iOff value. */
4897 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
4898
4899 if( p ){
4900 pFd->nFetchOut--;
4901 }else{
4902 unixUnmapfile(pFd);
4903 }
4904
4905 assert( pFd->nFetchOut>=0 );
4906 #else
4907 UNUSED_PARAMETER(fd);
4908 UNUSED_PARAMETER(p);
4909 UNUSED_PARAMETER(iOff);
4910 #endif
4911 return SQLITE_OK;
4912 }
4913
4173 /* 4914 /*
4174 ** Here ends the implementation of all sqlite3_file methods. 4915 ** Here ends the implementation of all sqlite3_file methods.
4175 ** 4916 **
4176 ********************** End sqlite3_file Methods ******************************* 4917 ********************** End sqlite3_file Methods *******************************
4177 ******************************************************************************/ 4918 ******************************************************************************/
4178 4919
4179 /* 4920 /*
4180 ** This division contains definitions of sqlite3_io_methods objects that 4921 ** This division contains definitions of sqlite3_io_methods objects that
4181 ** implement various file locking strategies. It also contains definitions 4922 ** implement various file locking strategies. It also contains definitions
4182 ** of "finder" functions. A finder-function is used to locate the appropriate 4923 ** of "finder" functions. A finder-function is used to locate the appropriate
4183 ** sqlite3_io_methods object for a particular database file. The pAppData 4924 ** sqlite3_io_methods object for a particular database file. The pAppData
4184 ** field of the sqlite3_vfs VFS objects are initialized to be pointers to 4925 ** field of the sqlite3_vfs VFS objects are initialized to be pointers to
4185 ** the correct finder-function for that VFS. 4926 ** the correct finder-function for that VFS.
4186 ** 4927 **
4187 ** Most finder functions return a pointer to a fixed sqlite3_io_methods 4928 ** Most finder functions return a pointer to a fixed sqlite3_io_methods
4188 ** object. The only interesting finder-function is autolockIoFinder, which 4929 ** object. The only interesting finder-function is autolockIoFinder, which
4189 ** looks at the filesystem type and tries to guess the best locking 4930 ** looks at the filesystem type and tries to guess the best locking
4190 ** strategy from that. 4931 ** strategy from that.
4191 ** 4932 **
4192 ** For finder-funtion F, two objects are created: 4933 ** For finder-function F, two objects are created:
4193 ** 4934 **
4194 ** (1) The real finder-function named "FImpt()". 4935 ** (1) The real finder-function named "FImpt()".
4195 ** 4936 **
4196 ** (2) A constant pointer to this function named just "F". 4937 ** (2) A constant pointer to this function named just "F".
4197 ** 4938 **
4198 ** 4939 **
4199 ** A pointer to the F pointer is used as the pAppData value for VFS 4940 ** A pointer to the F pointer is used as the pAppData value for VFS
4200 ** objects. We have to do this instead of letting pAppData point 4941 ** objects. We have to do this instead of letting pAppData point
4201 ** directly at the finder-function since C90 rules prevent a void* 4942 ** directly at the finder-function since C90 rules prevent a void*
4202 ** from be cast into a function pointer. 4943 ** from be cast into a function pointer.
4203 ** 4944 **
4204 ** 4945 **
4205 ** Each instance of this macro generates two objects: 4946 ** Each instance of this macro generates two objects:
4206 ** 4947 **
4207 ** * A constant sqlite3_io_methods object call METHOD that has locking 4948 ** * A constant sqlite3_io_methods object call METHOD that has locking
4208 ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. 4949 ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
4209 ** 4950 **
4210 ** * An I/O method finder function called FINDER that returns a pointer 4951 ** * An I/O method finder function called FINDER that returns a pointer
4211 ** to the METHOD object in the previous bullet. 4952 ** to the METHOD object in the previous bullet.
4212 */ 4953 */
4213 #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK) \ 4954 #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK, SHMMAP) \
4214 static const sqlite3_io_methods METHOD = { \ 4955 static const sqlite3_io_methods METHOD = { \
4215 VERSION, /* iVersion */ \ 4956 VERSION, /* iVersion */ \
4216 CLOSE, /* xClose */ \ 4957 CLOSE, /* xClose */ \
4217 unixRead, /* xRead */ \ 4958 unixRead, /* xRead */ \
4218 unixWrite, /* xWrite */ \ 4959 unixWrite, /* xWrite */ \
4219 unixTruncate, /* xTruncate */ \ 4960 unixTruncate, /* xTruncate */ \
4220 unixSync, /* xSync */ \ 4961 unixSync, /* xSync */ \
4221 unixFileSize, /* xFileSize */ \ 4962 unixFileSize, /* xFileSize */ \
4222 LOCK, /* xLock */ \ 4963 LOCK, /* xLock */ \
4223 UNLOCK, /* xUnlock */ \ 4964 UNLOCK, /* xUnlock */ \
4224 CKLOCK, /* xCheckReservedLock */ \ 4965 CKLOCK, /* xCheckReservedLock */ \
4225 unixFileControl, /* xFileControl */ \ 4966 unixFileControl, /* xFileControl */ \
4226 unixSectorSize, /* xSectorSize */ \ 4967 unixSectorSize, /* xSectorSize */ \
4227 unixDeviceCharacteristics, /* xDeviceCapabilities */ \ 4968 unixDeviceCharacteristics, /* xDeviceCapabilities */ \
4228 unixShmMap, /* xShmMap */ \ 4969 SHMMAP, /* xShmMap */ \
4229 unixShmLock, /* xShmLock */ \ 4970 unixShmLock, /* xShmLock */ \
4230 unixShmBarrier, /* xShmBarrier */ \ 4971 unixShmBarrier, /* xShmBarrier */ \
4231 unixShmUnmap /* xShmUnmap */ \ 4972 unixShmUnmap, /* xShmUnmap */ \
4973 unixFetch, /* xFetch */ \
4974 unixUnfetch, /* xUnfetch */ \
4232 }; \ 4975 }; \
4233 static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \ 4976 static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \
4234 UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \ 4977 UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \
4235 return &METHOD; \ 4978 return &METHOD; \
4236 } \ 4979 } \
4237 static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \ 4980 static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \
4238 = FINDER##Impl; 4981 = FINDER##Impl;
4239 4982
4240 /* 4983 /*
4241 ** Here are all of the sqlite3_io_methods objects for each of the 4984 ** Here are all of the sqlite3_io_methods objects for each of the
4242 ** locking strategies. Functions that return pointers to these methods 4985 ** locking strategies. Functions that return pointers to these methods
4243 ** are also created. 4986 ** are also created.
4244 */ 4987 */
4245 IOMETHODS( 4988 IOMETHODS(
4246 posixIoFinder, /* Finder function name */ 4989 posixIoFinder, /* Finder function name */
4247 posixIoMethods, /* sqlite3_io_methods object name */ 4990 posixIoMethods, /* sqlite3_io_methods object name */
4248 2, /* shared memory is enabled */ 4991 3, /* shared memory and mmap are enabled */
4249 unixClose, /* xClose method */ 4992 unixClose, /* xClose method */
4250 unixLock, /* xLock method */ 4993 unixLock, /* xLock method */
4251 unixUnlock, /* xUnlock method */ 4994 unixUnlock, /* xUnlock method */
4252 unixCheckReservedLock /* xCheckReservedLock method */ 4995 unixCheckReservedLock, /* xCheckReservedLock method */
4996 unixShmMap /* xShmMap method */
4253 ) 4997 )
4254 IOMETHODS( 4998 IOMETHODS(
4255 nolockIoFinder, /* Finder function name */ 4999 nolockIoFinder, /* Finder function name */
4256 nolockIoMethods, /* sqlite3_io_methods object name */ 5000 nolockIoMethods, /* sqlite3_io_methods object name */
4257 1, /* shared memory is disabled */ 5001 3, /* shared memory is disabled */
4258 nolockClose, /* xClose method */ 5002 nolockClose, /* xClose method */
4259 nolockLock, /* xLock method */ 5003 nolockLock, /* xLock method */
4260 nolockUnlock, /* xUnlock method */ 5004 nolockUnlock, /* xUnlock method */
4261 nolockCheckReservedLock /* xCheckReservedLock method */ 5005 nolockCheckReservedLock, /* xCheckReservedLock method */
5006 0 /* xShmMap method */
4262 ) 5007 )
4263 IOMETHODS( 5008 IOMETHODS(
4264 dotlockIoFinder, /* Finder function name */ 5009 dotlockIoFinder, /* Finder function name */
4265 dotlockIoMethods, /* sqlite3_io_methods object name */ 5010 dotlockIoMethods, /* sqlite3_io_methods object name */
4266 1, /* shared memory is disabled */ 5011 1, /* shared memory is disabled */
4267 dotlockClose, /* xClose method */ 5012 dotlockClose, /* xClose method */
4268 dotlockLock, /* xLock method */ 5013 dotlockLock, /* xLock method */
4269 dotlockUnlock, /* xUnlock method */ 5014 dotlockUnlock, /* xUnlock method */
4270 dotlockCheckReservedLock /* xCheckReservedLock method */ 5015 dotlockCheckReservedLock, /* xCheckReservedLock method */
5016 0 /* xShmMap method */
4271 ) 5017 )
4272 5018
4273 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS 5019 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
4274 IOMETHODS( 5020 IOMETHODS(
4275 flockIoFinder, /* Finder function name */ 5021 flockIoFinder, /* Finder function name */
4276 flockIoMethods, /* sqlite3_io_methods object name */ 5022 flockIoMethods, /* sqlite3_io_methods object name */
4277 1, /* shared memory is disabled */ 5023 1, /* shared memory is disabled */
4278 flockClose, /* xClose method */ 5024 flockClose, /* xClose method */
4279 flockLock, /* xLock method */ 5025 flockLock, /* xLock method */
4280 flockUnlock, /* xUnlock method */ 5026 flockUnlock, /* xUnlock method */
4281 flockCheckReservedLock /* xCheckReservedLock method */ 5027 flockCheckReservedLock, /* xCheckReservedLock method */
5028 0 /* xShmMap method */
4282 ) 5029 )
4283 #endif 5030 #endif
4284 5031
4285 #if OS_VXWORKS 5032 #if OS_VXWORKS
4286 IOMETHODS( 5033 IOMETHODS(
4287 semIoFinder, /* Finder function name */ 5034 semIoFinder, /* Finder function name */
4288 semIoMethods, /* sqlite3_io_methods object name */ 5035 semIoMethods, /* sqlite3_io_methods object name */
4289 1, /* shared memory is disabled */ 5036 1, /* shared memory is disabled */
4290 semClose, /* xClose method */ 5037 semClose, /* xClose method */
4291 semLock, /* xLock method */ 5038 semLock, /* xLock method */
4292 semUnlock, /* xUnlock method */ 5039 semUnlock, /* xUnlock method */
4293 semCheckReservedLock /* xCheckReservedLock method */ 5040 semCheckReservedLock, /* xCheckReservedLock method */
5041 0 /* xShmMap method */
4294 ) 5042 )
4295 #endif 5043 #endif
4296 5044
4297 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE 5045 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
4298 IOMETHODS( 5046 IOMETHODS(
4299 afpIoFinder, /* Finder function name */ 5047 afpIoFinder, /* Finder function name */
4300 afpIoMethods, /* sqlite3_io_methods object name */ 5048 afpIoMethods, /* sqlite3_io_methods object name */
4301 1, /* shared memory is disabled */ 5049 1, /* shared memory is disabled */
4302 afpClose, /* xClose method */ 5050 afpClose, /* xClose method */
4303 afpLock, /* xLock method */ 5051 afpLock, /* xLock method */
4304 afpUnlock, /* xUnlock method */ 5052 afpUnlock, /* xUnlock method */
4305 afpCheckReservedLock /* xCheckReservedLock method */ 5053 afpCheckReservedLock, /* xCheckReservedLock method */
5054 0 /* xShmMap method */
4306 ) 5055 )
4307 #endif 5056 #endif
4308 5057
4309 /* 5058 /*
4310 ** The proxy locking method is a "super-method" in the sense that it 5059 ** The proxy locking method is a "super-method" in the sense that it
4311 ** opens secondary file descriptors for the conch and lock files and 5060 ** opens secondary file descriptors for the conch and lock files and
4312 ** it uses proxy, dot-file, AFP, and flock() locking methods on those 5061 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
4313 ** secondary files. For this reason, the division that implements 5062 ** secondary files. For this reason, the division that implements
4314 ** proxy locking is located much further down in the file. But we need 5063 ** proxy locking is located much further down in the file. But we need
4315 ** to go ahead and define the sqlite3_io_methods and finder function 5064 ** to go ahead and define the sqlite3_io_methods and finder function
4316 ** for proxy locking here. So we forward declare the I/O methods. 5065 ** for proxy locking here. So we forward declare the I/O methods.
4317 */ 5066 */
4318 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE 5067 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
4319 static int proxyClose(sqlite3_file*); 5068 static int proxyClose(sqlite3_file*);
4320 static int proxyLock(sqlite3_file*, int); 5069 static int proxyLock(sqlite3_file*, int);
4321 static int proxyUnlock(sqlite3_file*, int); 5070 static int proxyUnlock(sqlite3_file*, int);
4322 static int proxyCheckReservedLock(sqlite3_file*, int*); 5071 static int proxyCheckReservedLock(sqlite3_file*, int*);
4323 IOMETHODS( 5072 IOMETHODS(
4324 proxyIoFinder, /* Finder function name */ 5073 proxyIoFinder, /* Finder function name */
4325 proxyIoMethods, /* sqlite3_io_methods object name */ 5074 proxyIoMethods, /* sqlite3_io_methods object name */
4326 1, /* shared memory is disabled */ 5075 1, /* shared memory is disabled */
4327 proxyClose, /* xClose method */ 5076 proxyClose, /* xClose method */
4328 proxyLock, /* xLock method */ 5077 proxyLock, /* xLock method */
4329 proxyUnlock, /* xUnlock method */ 5078 proxyUnlock, /* xUnlock method */
4330 proxyCheckReservedLock /* xCheckReservedLock method */ 5079 proxyCheckReservedLock, /* xCheckReservedLock method */
5080 0 /* xShmMap method */
4331 ) 5081 )
4332 #endif 5082 #endif
4333 5083
4334 /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ 5084 /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
4335 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE 5085 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
4336 IOMETHODS( 5086 IOMETHODS(
4337 nfsIoFinder, /* Finder function name */ 5087 nfsIoFinder, /* Finder function name */
4338 nfsIoMethods, /* sqlite3_io_methods object name */ 5088 nfsIoMethods, /* sqlite3_io_methods object name */
4339 1, /* shared memory is disabled */ 5089 1, /* shared memory is disabled */
4340 unixClose, /* xClose method */ 5090 unixClose, /* xClose method */
4341 unixLock, /* xLock method */ 5091 unixLock, /* xLock method */
4342 nfsUnlock, /* xUnlock method */ 5092 nfsUnlock, /* xUnlock method */
4343 unixCheckReservedLock /* xCheckReservedLock method */ 5093 unixCheckReservedLock, /* xCheckReservedLock method */
5094 0 /* xShmMap method */
4344 ) 5095 )
4345 #endif 5096 #endif
4346 5097
4347 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE 5098 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
4348 /* 5099 /*
4349 ** This "finder" function attempts to determine the best locking strategy 5100 ** This "finder" function attempts to determine the best locking strategy
4350 ** for the database file "filePath". It then returns the sqlite3_io_methods 5101 ** for the database file "filePath". It then returns the sqlite3_io_methods
4351 ** object that implements that strategy. 5102 ** object that implements that strategy.
4352 ** 5103 **
4353 ** This is for MacOSX only. 5104 ** This is for MacOSX only.
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
4442 }else{ 5193 }else{
4443 return &semIoMethods; 5194 return &semIoMethods;
4444 } 5195 }
4445 } 5196 }
4446 static const sqlite3_io_methods 5197 static const sqlite3_io_methods
4447 *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; 5198 *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
4448 5199
4449 #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */ 5200 #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */
4450 5201
4451 /* 5202 /*
4452 ** An abstract type for a pointer to a IO method finder function: 5203 ** An abstract type for a pointer to an IO method finder function:
4453 */ 5204 */
4454 typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); 5205 typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
4455 5206
4456 5207
4457 /**************************************************************************** 5208 /****************************************************************************
4458 **************************** sqlite3_vfs methods **************************** 5209 **************************** sqlite3_vfs methods ****************************
4459 ** 5210 **
4460 ** This division contains the implementation of methods on the 5211 ** This division contains the implementation of methods on the
4461 ** sqlite3_vfs object. 5212 ** sqlite3_vfs object.
4462 */ 5213 */
4463 5214
4464 /* 5215 /*
4465 ** Initializes a unixFile structure with zeros.
4466 */
4467 void initUnixFile(sqlite3_file* file) {
4468 memset(file, 0, sizeof(unixFile));
4469 }
4470
4471 /*
4472 ** Initialize the contents of the unixFile structure pointed to by pId. 5216 ** Initialize the contents of the unixFile structure pointed to by pId.
4473 */ 5217 */
4474 int fillInUnixFile( 5218 static int fillInUnixFile(
4475 sqlite3_vfs *pVfs, /* Pointer to vfs object */ 5219 sqlite3_vfs *pVfs, /* Pointer to vfs object */
4476 int h, /* Open file descriptor of file being opened */ 5220 int h, /* Open file descriptor of file being opened */
4477 int syncDir, /* True to sync directory on first sync */
4478 sqlite3_file *pId, /* Write to the unixFile structure here */ 5221 sqlite3_file *pId, /* Write to the unixFile structure here */
4479 const char *zFilename, /* Name of the file being opened */ 5222 const char *zFilename, /* Name of the file being opened */
4480 int noLock, /* Omit locking if true */ 5223 int ctrlFlags /* Zero or more UNIXFILE_* values */
4481 int isDelete, /* Delete on close if true */
4482 int isReadOnly /* True if the file is opened read-only */
4483 ){ 5224 ){
4484 const sqlite3_io_methods *pLockingStyle; 5225 const sqlite3_io_methods *pLockingStyle;
4485 unixFile *pNew = (unixFile *)pId; 5226 unixFile *pNew = (unixFile *)pId;
4486 int rc = SQLITE_OK; 5227 int rc = SQLITE_OK;
4487 5228
4488 assert( pNew->pInode==NULL ); 5229 assert( pNew->pInode==NULL );
4489 5230
4490 /* Parameter isDelete is only used on vxworks. Express this explicitly
4491 ** here to prevent compiler warnings about unused parameters.
4492 */
4493 UNUSED_PARAMETER(isDelete);
4494
4495 /* Usually the path zFilename should not be a relative pathname. The 5231 /* Usually the path zFilename should not be a relative pathname. The
4496 ** exception is when opening the proxy "conch" file in builds that 5232 ** exception is when opening the proxy "conch" file in builds that
4497 ** include the special Apple locking styles. 5233 ** include the special Apple locking styles.
4498 */ 5234 */
4499 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE 5235 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
4500 assert( zFilename==0 || zFilename[0]=='/' 5236 assert( zFilename==0 || zFilename[0]=='/'
4501 || pVfs->pAppData==(void*)&autolockIoFinder ); 5237 || pVfs->pAppData==(void*)&autolockIoFinder );
4502 #else 5238 #else
4503 assert( zFilename==0 || zFilename[0]=='/' ); 5239 assert( zFilename==0 || zFilename[0]=='/' );
4504 #endif 5240 #endif
4505 5241
5242 /* No locking occurs in temporary files */
5243 assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
5244
4506 OSTRACE(("OPEN %-3d %s\n", h, zFilename)); 5245 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
4507 pNew->h = h; 5246 pNew->h = h;
5247 pNew->pVfs = pVfs;
4508 pNew->zPath = zFilename; 5248 pNew->zPath = zFilename;
5249 pNew->ctrlFlags = (u8)ctrlFlags;
5250 #if SQLITE_MAX_MMAP_SIZE>0
5251 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
5252 #endif
5253 if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
5254 "psow", SQLITE_POWERSAFE_OVERWRITE) ){
5255 pNew->ctrlFlags |= UNIXFILE_PSOW;
5256 }
4509 if( strcmp(pVfs->zName,"unix-excl")==0 ){ 5257 if( strcmp(pVfs->zName,"unix-excl")==0 ){
4510 pNew->ctrlFlags = UNIXFILE_EXCL; 5258 pNew->ctrlFlags |= UNIXFILE_EXCL;
4511 }else{
4512 pNew->ctrlFlags = 0;
4513 }
4514 if( isReadOnly ){
4515 pNew->ctrlFlags |= UNIXFILE_RDONLY;
4516 }
4517 if( syncDir ){
4518 pNew->ctrlFlags |= UNIXFILE_DIRSYNC;
4519 } 5259 }
4520 5260
4521 #if OS_VXWORKS 5261 #if OS_VXWORKS
4522 pNew->pId = vxworksFindFileId(zFilename); 5262 pNew->pId = vxworksFindFileId(zFilename);
4523 if( pNew->pId==0 ){ 5263 if( pNew->pId==0 ){
4524 noLock = 1; 5264 ctrlFlags |= UNIXFILE_NOLOCK;
4525 rc = SQLITE_NOMEM; 5265 rc = SQLITE_NOMEM;
4526 } 5266 }
4527 #endif 5267 #endif
4528 5268
4529 if( noLock ){ 5269 if( ctrlFlags & UNIXFILE_NOLOCK ){
4530 pLockingStyle = &nolockIoMethods; 5270 pLockingStyle = &nolockIoMethods;
4531 }else{ 5271 }else{
4532 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); 5272 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
4533 #if SQLITE_ENABLE_LOCKING_STYLE 5273 #if SQLITE_ENABLE_LOCKING_STYLE
4534 /* Cache zFilename in the locking context (AFP and dotlock override) for 5274 /* Cache zFilename in the locking context (AFP and dotlock override) for
4535 ** proxyLock activation is possible (remote proxy is based on db name) 5275 ** proxyLock activation is possible (remote proxy is based on db name)
4536 ** zFilename remains valid until file is closed, to support */ 5276 ** zFilename remains valid until file is closed, to support */
4537 pNew->lockingContext = (void*)zFilename; 5277 pNew->lockingContext = (void*)zFilename;
4538 #endif 5278 #endif
4539 } 5279 }
4540 5280
4541 if( pLockingStyle == &posixIoMethods 5281 if( pLockingStyle == &posixIoMethods
4542 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE 5282 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
4543 || pLockingStyle == &nfsIoMethods 5283 || pLockingStyle == &nfsIoMethods
4544 #endif 5284 #endif
4545 ){ 5285 ){
4546 unixEnterMutex(); 5286 unixEnterMutex();
4547 rc = findInodeInfo(pNew, &pNew->pInode); 5287 rc = findInodeInfo(pNew, &pNew->pInode);
4548 if( rc!=SQLITE_OK ){ 5288 if( rc!=SQLITE_OK ){
4549 /* If an error occured in findInodeInfo(), close the file descriptor 5289 /* If an error occurred in findInodeInfo(), close the file descriptor
4550 ** immediately, before releasing the mutex. findInodeInfo() may fail 5290 ** immediately, before releasing the mutex. findInodeInfo() may fail
4551 ** in two scenarios: 5291 ** in two scenarios:
4552 ** 5292 **
4553 ** (a) A call to fstat() failed. 5293 ** (a) A call to fstat() failed.
4554 ** (b) A malloc failed. 5294 ** (b) A malloc failed.
4555 ** 5295 **
4556 ** Scenario (b) may only occur if the process is holding no other 5296 ** Scenario (b) may only occur if the process is holding no other
4557 ** file descriptors open on the same file. If there were other file 5297 ** file descriptors open on the same file. If there were other file
4558 ** descriptors on this file, then no malloc would be required by 5298 ** descriptors on this file, then no malloc would be required by
4559 ** findInodeInfo(). If this is the case, it is quite safe to close 5299 ** findInodeInfo(). If this is the case, it is quite safe to close
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
4597 } 5337 }
4598 } 5338 }
4599 #endif 5339 #endif
4600 5340
4601 else if( pLockingStyle == &dotlockIoMethods ){ 5341 else if( pLockingStyle == &dotlockIoMethods ){
4602 /* Dotfile locking uses the file path so it needs to be included in 5342 /* Dotfile locking uses the file path so it needs to be included in
4603 ** the dotlockLockingContext 5343 ** the dotlockLockingContext
4604 */ 5344 */
4605 char *zLockFile; 5345 char *zLockFile;
4606 int nFilename; 5346 int nFilename;
5347 assert( zFilename!=0 );
4607 nFilename = (int)strlen(zFilename) + 6; 5348 nFilename = (int)strlen(zFilename) + 6;
4608 zLockFile = (char *)sqlite3_malloc(nFilename); 5349 zLockFile = (char *)sqlite3_malloc(nFilename);
4609 if( zLockFile==0 ){ 5350 if( zLockFile==0 ){
4610 rc = SQLITE_NOMEM; 5351 rc = SQLITE_NOMEM;
4611 }else{ 5352 }else{
4612 sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); 5353 sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
4613 } 5354 }
4614 pNew->lockingContext = zLockFile; 5355 pNew->lockingContext = zLockFile;
4615 } 5356 }
4616 5357
(...skipping 20 matching lines...) Expand all
4637 unixLeaveMutex(); 5378 unixLeaveMutex();
4638 } 5379 }
4639 #endif 5380 #endif
4640 5381
4641 pNew->lastErrno = 0; 5382 pNew->lastErrno = 0;
4642 #if OS_VXWORKS 5383 #if OS_VXWORKS
4643 if( rc!=SQLITE_OK ){ 5384 if( rc!=SQLITE_OK ){
4644 if( h>=0 ) robust_close(pNew, h, __LINE__); 5385 if( h>=0 ) robust_close(pNew, h, __LINE__);
4645 h = -1; 5386 h = -1;
4646 osUnlink(zFilename); 5387 osUnlink(zFilename);
4647 isDelete = 0; 5388 pNew->ctrlFlags |= UNIXFILE_DELETE;
4648 } 5389 }
4649 pNew->isDelete = isDelete;
4650 #endif 5390 #endif
4651 if( rc!=SQLITE_OK ){ 5391 if( rc!=SQLITE_OK ){
4652 if( h>=0 ) robust_close(pNew, h, __LINE__); 5392 if( h>=0 ) robust_close(pNew, h, __LINE__);
4653 }else{ 5393 }else{
4654 pNew->pMethod = pLockingStyle; 5394 pNew->pMethod = pLockingStyle;
4655 OpenCounter(+1); 5395 OpenCounter(+1);
5396 verifyDbFile(pNew);
4656 } 5397 }
4657 return rc; 5398 return rc;
4658 } 5399 }
4659 5400
4660 /* 5401 /*
4661 ** Return the name of a directory in which to put temporary files. 5402 ** Return the name of a directory in which to put temporary files.
4662 ** If no suitable temporary file directory can be found, return NULL. 5403 ** If no suitable temporary file directory can be found, return NULL.
4663 */ 5404 */
4664 static const char *unixTempFileDir(void){ 5405 static const char *unixTempFileDir(void){
4665 static const char *azDirs[] = { 5406 static const char *azDirs[] = {
4666 0, 5407 0,
4667 0, 5408 0,
5409 0,
4668 "/var/tmp", 5410 "/var/tmp",
4669 "/usr/tmp", 5411 "/usr/tmp",
4670 "/tmp", 5412 "/tmp",
4671 0 /* List terminator */ 5413 0 /* List terminator */
4672 }; 5414 };
4673 unsigned int i; 5415 unsigned int i;
4674 struct stat buf; 5416 struct stat buf;
4675 const char *zDir = 0; 5417 const char *zDir = 0;
4676 5418
4677 azDirs[0] = sqlite3_temp_directory; 5419 azDirs[0] = sqlite3_temp_directory;
4678 if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); 5420 if( !azDirs[1] ) azDirs[1] = getenv("SQLITE_TMPDIR");
5421 if( !azDirs[2] ) azDirs[2] = getenv("TMPDIR");
4679 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){ 5422 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
4680 if( zDir==0 ) continue; 5423 if( zDir==0 ) continue;
4681 if( osStat(zDir, &buf) ) continue; 5424 if( osStat(zDir, &buf) ) continue;
4682 if( !S_ISDIR(buf.st_mode) ) continue; 5425 if( !S_ISDIR(buf.st_mode) ) continue;
4683 if( osAccess(zDir, 07) ) continue; 5426 if( osAccess(zDir, 07) ) continue;
4684 break; 5427 break;
4685 } 5428 }
4686 return zDir; 5429 return zDir;
4687 } 5430 }
4688 5431
(...skipping 15 matching lines...) Expand all
4704 ** function failing. 5447 ** function failing.
4705 */ 5448 */
4706 SimulateIOError( return SQLITE_IOERR ); 5449 SimulateIOError( return SQLITE_IOERR );
4707 5450
4708 zDir = unixTempFileDir(); 5451 zDir = unixTempFileDir();
4709 if( zDir==0 ) zDir = "."; 5452 if( zDir==0 ) zDir = ".";
4710 5453
4711 /* Check that the output buffer is large enough for the temporary file 5454 /* Check that the output buffer is large enough for the temporary file
4712 ** name. If it is not, return SQLITE_ERROR. 5455 ** name. If it is not, return SQLITE_ERROR.
4713 */ 5456 */
4714 if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){ 5457 if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 18) >= (size_t)nBuf ){
4715 return SQLITE_ERROR; 5458 return SQLITE_ERROR;
4716 } 5459 }
4717 5460
4718 do{ 5461 do{
4719 sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir); 5462 sqlite3_snprintf(nBuf-18, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
4720 j = (int)strlen(zBuf); 5463 j = (int)strlen(zBuf);
4721 sqlite3_randomness(15, &zBuf[j]); 5464 sqlite3_randomness(15, &zBuf[j]);
4722 for(i=0; i<15; i++, j++){ 5465 for(i=0; i<15; i++, j++){
4723 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; 5466 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
4724 } 5467 }
4725 zBuf[j] = 0; 5468 zBuf[j] = 0;
5469 zBuf[j+1] = 0;
4726 }while( osAccess(zBuf,0)==0 ); 5470 }while( osAccess(zBuf,0)==0 );
4727 return SQLITE_OK; 5471 return SQLITE_OK;
4728 } 5472 }
4729 5473
4730 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) 5474 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
4731 /* 5475 /*
4732 ** Routine to transform a unixFile into a proxy-locking unixFile. 5476 ** Routine to transform a unixFile into a proxy-locking unixFile.
4733 ** Implementation in the proxy-lock division, but used by unixOpen() 5477 ** Implementation in the proxy-lock division, but used by unixOpen()
4734 ** if SQLITE_PREFER_PROXY_LOCKING is defined. 5478 ** if SQLITE_PREFER_PROXY_LOCKING is defined.
4735 */ 5479 */
(...skipping 27 matching lines...) Expand all
4763 #if !OS_VXWORKS 5507 #if !OS_VXWORKS
4764 struct stat sStat; /* Results of stat() call */ 5508 struct stat sStat; /* Results of stat() call */
4765 5509
4766 /* A stat() call may fail for various reasons. If this happens, it is 5510 /* A stat() call may fail for various reasons. If this happens, it is
4767 ** almost certain that an open() call on the same path will also fail. 5511 ** almost certain that an open() call on the same path will also fail.
4768 ** For this reason, if an error occurs in the stat() call here, it is 5512 ** For this reason, if an error occurs in the stat() call here, it is
4769 ** ignored and -1 is returned. The caller will try to open a new file 5513 ** ignored and -1 is returned. The caller will try to open a new file
4770 ** descriptor on the same path, fail, and return an error to SQLite. 5514 ** descriptor on the same path, fail, and return an error to SQLite.
4771 ** 5515 **
4772 ** Even if a subsequent open() call does succeed, the consequences of 5516 ** Even if a subsequent open() call does succeed, the consequences of
4773 ** not searching for a resusable file descriptor are not dire. */ 5517 ** not searching for a reusable file descriptor are not dire. */
4774 if( 0==osStat(zPath, &sStat) ){ 5518 if( 0==osStat(zPath, &sStat) ){
4775 unixInodeInfo *pInode; 5519 unixInodeInfo *pInode;
4776 5520
4777 unixEnterMutex(); 5521 unixEnterMutex();
4778 pInode = inodeList; 5522 pInode = inodeList;
4779 while( pInode && (pInode->fileId.dev!=sStat.st_dev 5523 while( pInode && (pInode->fileId.dev!=sStat.st_dev
4780 || pInode->fileId.ino!=sStat.st_ino) ){ 5524 || pInode->fileId.ino!=sStat.st_ino) ){
4781 pInode = pInode->pNext; 5525 pInode = pInode->pNext;
4782 } 5526 }
4783 if( pInode ){ 5527 if( pInode ){
(...skipping 10 matching lines...) Expand all
4794 return pUnused; 5538 return pUnused;
4795 } 5539 }
4796 5540
4797 /* 5541 /*
4798 ** This function is called by unixOpen() to determine the unix permissions 5542 ** This function is called by unixOpen() to determine the unix permissions
4799 ** to create new files with. If no error occurs, then SQLITE_OK is returned 5543 ** to create new files with. If no error occurs, then SQLITE_OK is returned
4800 ** and a value suitable for passing as the third argument to open(2) is 5544 ** and a value suitable for passing as the third argument to open(2) is
4801 ** written to *pMode. If an IO error occurs, an SQLite error code is 5545 ** written to *pMode. If an IO error occurs, an SQLite error code is
4802 ** returned and the value of *pMode is not modified. 5546 ** returned and the value of *pMode is not modified.
4803 ** 5547 **
4804 ** If the file being opened is a temporary file, it is always created with 5548 ** In most cases, this routine sets *pMode to 0, which will become
4805 ** the octal permissions 0600 (read/writable by owner only). If the file 5549 ** an indication to robust_open() to create the file using
4806 ** is a database or master journal file, it is created with the permissions 5550 ** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask.
4807 ** mask SQLITE_DEFAULT_FILE_PERMISSIONS. 5551 ** But if the file being opened is a WAL or regular journal file, then
4808 **
4809 ** Finally, if the file being opened is a WAL or regular journal file, then
4810 ** this function queries the file-system for the permissions on the 5552 ** this function queries the file-system for the permissions on the
4811 ** corresponding database file and sets *pMode to this value. Whenever 5553 ** corresponding database file and sets *pMode to this value. Whenever
4812 ** possible, WAL and journal files are created using the same permissions 5554 ** possible, WAL and journal files are created using the same permissions
4813 ** as the associated database file. 5555 ** as the associated database file.
5556 **
5557 ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
5558 ** original filename is unavailable. But 8_3_NAMES is only used for
5559 ** FAT filesystems and permissions do not matter there, so just use
5560 ** the default permissions.
4814 */ 5561 */
4815 static int findCreateFileMode( 5562 static int findCreateFileMode(
4816 const char *zPath, /* Path of file (possibly) being created */ 5563 const char *zPath, /* Path of file (possibly) being created */
4817 int flags, /* Flags passed as 4th argument to xOpen() */ 5564 int flags, /* Flags passed as 4th argument to xOpen() */
4818 mode_t *pMode /* OUT: Permissions to open file with */ 5565 mode_t *pMode, /* OUT: Permissions to open file with */
5566 uid_t *pUid, /* OUT: uid to set on the file */
5567 gid_t *pGid /* OUT: gid to set on the file */
4819 ){ 5568 ){
4820 int rc = SQLITE_OK; /* Return Code */ 5569 int rc = SQLITE_OK; /* Return Code */
5570 *pMode = 0;
5571 *pUid = 0;
5572 *pGid = 0;
4821 if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ 5573 if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
4822 char zDb[MAX_PATHNAME+1]; /* Database file path */ 5574 char zDb[MAX_PATHNAME+1]; /* Database file path */
4823 int nDb; /* Number of valid bytes in zDb */ 5575 int nDb; /* Number of valid bytes in zDb */
4824 struct stat sStat; /* Output of stat() on database file */ 5576 struct stat sStat; /* Output of stat() on database file */
4825 5577
4826 /* zPath is a path to a WAL or journal file. The following block derives 5578 /* zPath is a path to a WAL or journal file. The following block derives
4827 ** the path to the associated database file from zPath. This block handles 5579 ** the path to the associated database file from zPath. This block handles
4828 ** the following naming conventions: 5580 ** the following naming conventions:
4829 ** 5581 **
4830 ** "<path to db>-journal" 5582 ** "<path to db>-journal"
4831 ** "<path to db>-wal" 5583 ** "<path to db>-wal"
4832 ** "<path to db>-journal-NNNN" 5584 ** "<path to db>-journalNN"
4833 ** "<path to db>-wal-NNNN" 5585 ** "<path to db>-walNN"
4834 ** 5586 **
4835 ** where NNNN is a 4 digit decimal number. The NNNN naming schemes are 5587 ** where NN is a decimal number. The NN naming schemes are
4836 ** used by the test_multiplex.c module. 5588 ** used by the test_multiplex.c module.
4837 */ 5589 */
4838 nDb = sqlite3Strlen30(zPath) - 1; 5590 nDb = sqlite3Strlen30(zPath) - 1;
4839 while( nDb>0 && zPath[nDb]!='l' ) nDb--; 5591 #ifdef SQLITE_ENABLE_8_3_NAMES
4840 nDb -= ((flags & SQLITE_OPEN_WAL) ? 3 : 7); 5592 while( nDb>0 && sqlite3Isalnum(zPath[nDb]) ) nDb--;
5593 if( nDb==0 || zPath[nDb]!='-' ) return SQLITE_OK;
5594 #else
5595 while( zPath[nDb]!='-' ){
5596 assert( nDb>0 );
5597 assert( zPath[nDb]!='\n' );
5598 nDb--;
5599 }
5600 #endif
4841 memcpy(zDb, zPath, nDb); 5601 memcpy(zDb, zPath, nDb);
4842 zDb[nDb] = '\0'; 5602 zDb[nDb] = '\0';
4843 5603
4844 if( 0==osStat(zDb, &sStat) ){ 5604 if( 0==osStat(zDb, &sStat) ){
4845 *pMode = sStat.st_mode & 0777; 5605 *pMode = sStat.st_mode & 0777;
5606 *pUid = sStat.st_uid;
5607 *pGid = sStat.st_gid;
4846 }else{ 5608 }else{
4847 rc = SQLITE_IOERR_FSTAT; 5609 rc = SQLITE_IOERR_FSTAT;
4848 } 5610 }
4849 }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){ 5611 }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
4850 *pMode = 0600; 5612 *pMode = 0600;
4851 }else{
4852 *pMode = SQLITE_DEFAULT_FILE_PERMISSIONS;
4853 } 5613 }
4854 return rc; 5614 return rc;
4855 } 5615 }
4856 5616
4857 /* 5617 /*
4858 ** Initializes a unixFile structure with zeros.
4859 */
4860 void chromium_sqlite3_initialize_unix_sqlite3_file(sqlite3_file* file) {
4861 memset(file, 0, sizeof(unixFile));
4862 }
4863
4864 int chromium_sqlite3_fill_in_unix_sqlite3_file(sqlite3_vfs* vfs,
4865 int fd,
4866 int dirfd,
4867 sqlite3_file* file,
4868 const char* fileName,
4869 int noLock,
4870 int isDelete) {
4871 return fillInUnixFile(vfs, fd, dirfd, file, fileName, noLock, isDelete, 0);
4872 }
4873
4874 /*
4875 ** Search for an unused file descriptor that was opened on the database file.
4876 ** If a suitable file descriptor if found, then it is stored in *fd; otherwise,
4877 ** *fd is not modified.
4878 **
4879 ** If a reusable file descriptor is not found, and a new UnixUnusedFd cannot
4880 ** be allocated, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK is returned.
4881 */
4882 int chromium_sqlite3_get_reusable_file_handle(sqlite3_file* file,
4883 const char* fileName,
4884 int flags,
4885 int* fd) {
4886 unixFile* unixSQLite3File = (unixFile*)file;
4887 int fileType = flags & 0xFFFFFF00;
4888 if (fileType == SQLITE_OPEN_MAIN_DB) {
4889 UnixUnusedFd *unusedFd = findReusableFd(fileName, flags);
4890 if (unusedFd) {
4891 *fd = unusedFd->fd;
4892 } else {
4893 unusedFd = sqlite3_malloc(sizeof(*unusedFd));
4894 if (!unusedFd) {
4895 return SQLITE_NOMEM;
4896 }
4897 }
4898 unixSQLite3File->pUnused = unusedFd;
4899 }
4900 return SQLITE_OK;
4901 }
4902
4903 /*
4904 ** Marks 'fd' as the unused file descriptor for 'pFile'.
4905 */
4906 void chromium_sqlite3_update_reusable_file_handle(sqlite3_file* file,
4907 int fd,
4908 int flags) {
4909 unixFile* unixSQLite3File = (unixFile*)file;
4910 if (unixSQLite3File->pUnused) {
4911 unixSQLite3File->pUnused->fd = fd;
4912 unixSQLite3File->pUnused->flags = flags;
4913 }
4914 }
4915
4916 /*
4917 ** Destroys pFile's field that keeps track of the unused file descriptor.
4918 */
4919 void chromium_sqlite3_destroy_reusable_file_handle(sqlite3_file* file) {
4920 unixFile* unixSQLite3File = (unixFile*)file;
4921 sqlite3_free(unixSQLite3File->pUnused);
4922 }
4923
4924 /*
4925 ** Open the file zPath. 5618 ** Open the file zPath.
4926 ** 5619 **
4927 ** Previously, the SQLite OS layer used three functions in place of this 5620 ** Previously, the SQLite OS layer used three functions in place of this
4928 ** one: 5621 ** one:
4929 ** 5622 **
4930 ** sqlite3OsOpenReadWrite(); 5623 ** sqlite3OsOpenReadWrite();
4931 ** sqlite3OsOpenReadOnly(); 5624 ** sqlite3OsOpenReadOnly();
4932 ** sqlite3OsOpenExclusive(); 5625 ** sqlite3OsOpenExclusive();
4933 ** 5626 **
4934 ** These calls correspond to the following combinations of flags: 5627 ** These calls correspond to the following combinations of flags:
(...skipping 14 matching lines...) Expand all
4949 sqlite3_file *pFile, /* The file descriptor to be filled in */ 5642 sqlite3_file *pFile, /* The file descriptor to be filled in */
4950 int flags, /* Input flags to control the opening */ 5643 int flags, /* Input flags to control the opening */
4951 int *pOutFlags /* Output flags returned to SQLite core */ 5644 int *pOutFlags /* Output flags returned to SQLite core */
4952 ){ 5645 ){
4953 unixFile *p = (unixFile *)pFile; 5646 unixFile *p = (unixFile *)pFile;
4954 int fd = -1; /* File descriptor returned by open() */ 5647 int fd = -1; /* File descriptor returned by open() */
4955 int openFlags = 0; /* Flags to pass to open() */ 5648 int openFlags = 0; /* Flags to pass to open() */
4956 int eType = flags&0xFFFFFF00; /* Type of file to open */ 5649 int eType = flags&0xFFFFFF00; /* Type of file to open */
4957 int noLock; /* True to omit locking primitives */ 5650 int noLock; /* True to omit locking primitives */
4958 int rc = SQLITE_OK; /* Function Return Code */ 5651 int rc = SQLITE_OK; /* Function Return Code */
5652 int ctrlFlags = 0; /* UNIXFILE_* flags */
4959 5653
4960 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); 5654 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
4961 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); 5655 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
4962 int isCreate = (flags & SQLITE_OPEN_CREATE); 5656 int isCreate = (flags & SQLITE_OPEN_CREATE);
4963 int isReadonly = (flags & SQLITE_OPEN_READONLY); 5657 int isReadonly = (flags & SQLITE_OPEN_READONLY);
4964 int isReadWrite = (flags & SQLITE_OPEN_READWRITE); 5658 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
4965 #if SQLITE_ENABLE_LOCKING_STYLE 5659 #if SQLITE_ENABLE_LOCKING_STYLE
4966 int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY); 5660 int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY);
4967 #endif 5661 #endif
5662 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
5663 struct statfs fsInfo;
5664 #endif
4968 5665
4969 /* If creating a master or main-file journal, this function will open 5666 /* If creating a master or main-file journal, this function will open
4970 ** a file-descriptor on the directory too. The first time unixSync() 5667 ** a file-descriptor on the directory too. The first time unixSync()
4971 ** is called the directory file descriptor will be fsync()ed and close()d. 5668 ** is called the directory file descriptor will be fsync()ed and close()d.
4972 */ 5669 */
4973 int syncDir = (isCreate && ( 5670 int syncDir = (isCreate && (
4974 eType==SQLITE_OPEN_MASTER_JOURNAL 5671 eType==SQLITE_OPEN_MASTER_JOURNAL
4975 || eType==SQLITE_OPEN_MAIN_JOURNAL 5672 || eType==SQLITE_OPEN_MAIN_JOURNAL
4976 || eType==SQLITE_OPEN_WAL 5673 || eType==SQLITE_OPEN_WAL
4977 )); 5674 ));
4978 5675
4979 /* If argument zPath is a NULL pointer, this function is required to open 5676 /* If argument zPath is a NULL pointer, this function is required to open
4980 ** a temporary file. Use this buffer to store the file name in. 5677 ** a temporary file. Use this buffer to store the file name in.
4981 */ 5678 */
4982 char zTmpname[MAX_PATHNAME+1]; 5679 char zTmpname[MAX_PATHNAME+2];
4983 const char *zName = zPath; 5680 const char *zName = zPath;
4984 5681
4985 /* Check the following statements are true: 5682 /* Check the following statements are true:
4986 ** 5683 **
4987 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and 5684 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
4988 ** (b) if CREATE is set, then READWRITE must also be set, and 5685 ** (b) if CREATE is set, then READWRITE must also be set, and
4989 ** (c) if EXCLUSIVE is set, then CREATE must also be set. 5686 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
4990 ** (d) if DELETEONCLOSE is set, then CREATE must also be set. 5687 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
4991 */ 5688 */
4992 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); 5689 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
4993 assert(isCreate==0 || isReadWrite); 5690 assert(isCreate==0 || isReadWrite);
4994 assert(isExclusive==0 || isCreate); 5691 assert(isExclusive==0 || isCreate);
4995 assert(isDelete==0 || isCreate); 5692 assert(isDelete==0 || isCreate);
4996 5693
4997 /* The main DB, main journal, WAL file and master journal are never 5694 /* The main DB, main journal, WAL file and master journal are never
4998 ** automatically deleted. Nor are they ever temporary files. */ 5695 ** automatically deleted. Nor are they ever temporary files. */
4999 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); 5696 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
5000 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); 5697 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
5001 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); 5698 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
5002 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); 5699 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
5003 5700
5004 /* Assert that the upper layer has set one of the "file-type" flags. */ 5701 /* Assert that the upper layer has set one of the "file-type" flags. */
5005 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB 5702 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
5006 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 5703 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
5007 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL 5704 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
5008 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL 5705 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
5009 ); 5706 );
5010 5707
5011 chromium_sqlite3_initialize_unix_sqlite3_file(pFile); 5708 /* Detect a pid change and reset the PRNG. There is a race condition
5709 ** here such that two or more threads all trying to open databases at
5710 ** the same instant might all reset the PRNG. But multiple resets
5711 ** are harmless.
5712 */
5713 if( randomnessPid!=getpid() ){
5714 randomnessPid = getpid();
5715 sqlite3_randomness(0,0);
5716 }
5717
5718 memset(p, 0, sizeof(unixFile));
5012 5719
5013 if( eType==SQLITE_OPEN_MAIN_DB ){ 5720 if( eType==SQLITE_OPEN_MAIN_DB ){
5014 rc = chromium_sqlite3_get_reusable_file_handle(pFile, zName, flags, &fd); 5721 UnixUnusedFd *pUnused;
5015 if( rc!=SQLITE_OK ){ 5722 pUnused = findReusableFd(zName, flags);
5016 return rc; 5723 if( pUnused ){
5724 fd = pUnused->fd;
5725 }else{
5726 pUnused = sqlite3_malloc(sizeof(*pUnused));
5727 if( !pUnused ){
5728 return SQLITE_NOMEM;
5729 }
5017 } 5730 }
5731 p->pUnused = pUnused;
5732
5733 /* Database filenames are double-zero terminated if they are not
5734 ** URIs with parameters. Hence, they can always be passed into
5735 ** sqlite3_uri_parameter(). */
5736 assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
5737
5018 }else if( !zName ){ 5738 }else if( !zName ){
5019 /* If zName is NULL, the upper layer is requesting a temp file. */ 5739 /* If zName is NULL, the upper layer is requesting a temp file. */
5020 assert(isDelete && !syncDir); 5740 assert(isDelete && !syncDir);
5021 rc = unixGetTempname(MAX_PATHNAME+1, zTmpname); 5741 rc = unixGetTempname(MAX_PATHNAME+2, zTmpname);
5022 if( rc!=SQLITE_OK ){ 5742 if( rc!=SQLITE_OK ){
5023 return rc; 5743 return rc;
5024 } 5744 }
5025 zName = zTmpname; 5745 zName = zTmpname;
5746
5747 /* Generated temporary filenames are always double-zero terminated
5748 ** for use by sqlite3_uri_parameter(). */
5749 assert( zName[strlen(zName)+1]==0 );
5026 } 5750 }
5027 5751
5028 /* Determine the value of the flags parameter passed to POSIX function 5752 /* Determine the value of the flags parameter passed to POSIX function
5029 ** open(). These must be calculated even if open() is not called, as 5753 ** open(). These must be calculated even if open() is not called, as
5030 ** they may be stored as part of the file handle and used by the 5754 ** they may be stored as part of the file handle and used by the
5031 ** 'conch file' locking functions later on. */ 5755 ** 'conch file' locking functions later on. */
5032 if( isReadonly ) openFlags |= O_RDONLY; 5756 if( isReadonly ) openFlags |= O_RDONLY;
5033 if( isReadWrite ) openFlags |= O_RDWR; 5757 if( isReadWrite ) openFlags |= O_RDWR;
5034 if( isCreate ) openFlags |= O_CREAT; 5758 if( isCreate ) openFlags |= O_CREAT;
5035 if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); 5759 if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
5036 openFlags |= (O_LARGEFILE|O_BINARY); 5760 openFlags |= (O_LARGEFILE|O_BINARY);
5037 5761
5038 if( fd<0 ){ 5762 if( fd<0 ){
5039 mode_t openMode; /* Permissions to create file with */ 5763 mode_t openMode; /* Permissions to create file with */
5040 rc = findCreateFileMode(zName, flags, &openMode); 5764 uid_t uid; /* Userid for the file */
5765 gid_t gid; /* Groupid for the file */
5766 rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
5041 if( rc!=SQLITE_OK ){ 5767 if( rc!=SQLITE_OK ){
5042 assert( !p->pUnused ); 5768 assert( !p->pUnused );
5043 assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL ); 5769 assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
5044 return rc; 5770 return rc;
5045 } 5771 }
5046 fd = robust_open(zName, openFlags, openMode); 5772 fd = robust_open(zName, openFlags, openMode);
5047 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); 5773 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
5048 if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){ 5774 if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
5049 /* Failed to open the file for read/write access. Try read-only. */ 5775 /* Failed to open the file for read/write access. Try read-only. */
5050 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); 5776 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
5051 openFlags &= ~(O_RDWR|O_CREAT); 5777 openFlags &= ~(O_RDWR|O_CREAT);
5052 flags |= SQLITE_OPEN_READONLY; 5778 flags |= SQLITE_OPEN_READONLY;
5053 openFlags |= O_RDONLY; 5779 openFlags |= O_RDONLY;
5054 isReadonly = 1; 5780 isReadonly = 1;
5055 fd = robust_open(zName, openFlags, openMode); 5781 fd = robust_open(zName, openFlags, openMode);
5056 } 5782 }
5057 if( fd<0 ){ 5783 if( fd<0 ){
5058 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName); 5784 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
5059 goto open_finished; 5785 goto open_finished;
5060 } 5786 }
5787
5788 /* If this process is running as root and if creating a new rollback
5789 ** journal or WAL file, set the ownership of the journal or WAL to be
5790 ** the same as the original database.
5791 */
5792 if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
5793 osFchown(fd, uid, gid);
5794 }
5061 } 5795 }
5062 assert( fd>=0 ); 5796 assert( fd>=0 );
5063 if( pOutFlags ){ 5797 if( pOutFlags ){
5064 *pOutFlags = flags; 5798 *pOutFlags = flags;
5065 } 5799 }
5066 5800
5067 chromium_sqlite3_update_reusable_file_handle(pFile, fd, flags); 5801 if( p->pUnused ){
5802 p->pUnused->fd = fd;
5803 p->pUnused->flags = flags;
5804 }
5068 5805
5069 if( isDelete ){ 5806 if( isDelete ){
5070 #if OS_VXWORKS 5807 #if OS_VXWORKS
5071 zPath = zName; 5808 zPath = zName;
5809 #elif defined(SQLITE_UNLINK_AFTER_CLOSE)
5810 zPath = sqlite3_mprintf("%s", zName);
5811 if( zPath==0 ){
5812 robust_close(p, fd, __LINE__);
5813 return SQLITE_NOMEM;
5814 }
5072 #else 5815 #else
5073 osUnlink(zName); 5816 osUnlink(zName);
5074 #endif 5817 #endif
5075 } 5818 }
5076 #if SQLITE_ENABLE_LOCKING_STYLE 5819 #if SQLITE_ENABLE_LOCKING_STYLE
5077 else{ 5820 else{
5078 p->openFlags = openFlags; 5821 p->openFlags = openFlags;
5079 } 5822 }
5080 #endif 5823 #endif
5081 5824
5082 #ifdef FD_CLOEXEC
5083 osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
5084 #endif
5085
5086 noLock = eType!=SQLITE_OPEN_MAIN_DB; 5825 noLock = eType!=SQLITE_OPEN_MAIN_DB;
5087 5826
5088 5827
5089 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE 5828 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
5090 struct statfs fsInfo;
5091 if( fstatfs(fd, &fsInfo) == -1 ){ 5829 if( fstatfs(fd, &fsInfo) == -1 ){
5092 ((unixFile*)pFile)->lastErrno = errno; 5830 ((unixFile*)pFile)->lastErrno = errno;
5093 robust_close(p, fd, __LINE__); 5831 robust_close(p, fd, __LINE__);
5094 return SQLITE_IOERR_ACCESS; 5832 return SQLITE_IOERR_ACCESS;
5095 } 5833 }
5096 if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) { 5834 if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
5097 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; 5835 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
5098 } 5836 }
5099 #endif 5837 #endif
5100 5838
5839 /* Set up appropriate ctrlFlags */
5840 if( isDelete ) ctrlFlags |= UNIXFILE_DELETE;
5841 if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY;
5842 if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK;
5843 if( syncDir ) ctrlFlags |= UNIXFILE_DIRSYNC;
5844 if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
5845
5101 #if SQLITE_ENABLE_LOCKING_STYLE 5846 #if SQLITE_ENABLE_LOCKING_STYLE
5102 #if SQLITE_PREFER_PROXY_LOCKING 5847 #if SQLITE_PREFER_PROXY_LOCKING
5103 isAutoProxy = 1; 5848 isAutoProxy = 1;
5104 #endif 5849 #endif
5105 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){ 5850 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
5106 char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); 5851 char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
5107 int useProxy = 0; 5852 int useProxy = 0;
5108 5853
5109 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means 5854 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
5110 ** never use proxy, NULL means use proxy for non-local files only. */ 5855 ** never use proxy, NULL means use proxy for non-local files only. */
5111 if( envforce!=NULL ){ 5856 if( envforce!=NULL ){
5112 useProxy = atoi(envforce)>0; 5857 useProxy = atoi(envforce)>0;
5113 }else{ 5858 }else{
5114 struct statfs fsInfo;
5115 if( statfs(zPath, &fsInfo) == -1 ){ 5859 if( statfs(zPath, &fsInfo) == -1 ){
5116 /* In theory, the close(fd) call is sub-optimal. If the file opened 5860 /* In theory, the close(fd) call is sub-optimal. If the file opened
5117 ** with fd is a database file, and there are other connections open 5861 ** with fd is a database file, and there are other connections open
5118 ** on that file that are currently holding advisory locks on it, 5862 ** on that file that are currently holding advisory locks on it,
5119 ** then the call to close() will cancel those locks. In practice, 5863 ** then the call to close() will cancel those locks. In practice,
5120 ** we're assuming that statfs() doesn't fail very often. At least 5864 ** we're assuming that statfs() doesn't fail very often. At least
5121 ** not while other file descriptors opened by the same process on 5865 ** not while other file descriptors opened by the same process on
5122 ** the same file are working. */ 5866 ** the same file are working. */
5123 p->lastErrno = errno; 5867 p->lastErrno = errno;
5124 robust_close(p, fd, __LINE__); 5868 robust_close(p, fd, __LINE__);
5125 rc = SQLITE_IOERR_ACCESS; 5869 rc = SQLITE_IOERR_ACCESS;
5126 goto open_finished; 5870 goto open_finished;
5127 } 5871 }
5128 useProxy = !(fsInfo.f_flags&MNT_LOCAL); 5872 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
5129 } 5873 }
5130 if( useProxy ){ 5874 if( useProxy ){
5131 rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock, 5875 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
5132 isDelete, isReadonly);
5133 if( rc==SQLITE_OK ){ 5876 if( rc==SQLITE_OK ){
5134 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); 5877 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
5135 if( rc!=SQLITE_OK ){ 5878 if( rc!=SQLITE_OK ){
5136 /* Use unixClose to clean up the resources added in fillInUnixFile 5879 /* Use unixClose to clean up the resources added in fillInUnixFile
5137 ** and clear all the structure's references. Specifically, 5880 ** and clear all the structure's references. Specifically,
5138 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op 5881 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
5139 */ 5882 */
5140 unixClose(pFile); 5883 unixClose(pFile);
5141 return rc; 5884 return rc;
5142 } 5885 }
5143 } 5886 }
5144 goto open_finished; 5887 goto open_finished;
5145 } 5888 }
5146 } 5889 }
5147 #endif 5890 #endif
5148 5891
5149 rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock, 5892 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
5150 isDelete, isReadonly); 5893
5151 open_finished: 5894 open_finished:
5152 if( rc!=SQLITE_OK ){ 5895 if( rc!=SQLITE_OK ){
5153 chromium_sqlite3_destroy_reusable_file_handle(pFile); 5896 sqlite3_free(p->pUnused);
5154 } 5897 }
5155 return rc; 5898 return rc;
5156 } 5899 }
5157 5900
5158 5901
5159 /* 5902 /*
5160 ** Delete the file at zPath. If the dirSync argument is true, fsync() 5903 ** Delete the file at zPath. If the dirSync argument is true, fsync()
5161 ** the directory after deleting the file. 5904 ** the directory after deleting the file.
5162 */ 5905 */
5163 static int unixDelete( 5906 static int unixDelete(
5164 sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */ 5907 sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */
5165 const char *zPath, /* Name of file to be deleted */ 5908 const char *zPath, /* Name of file to be deleted */
5166 int dirSync /* If true, fsync() directory after deleting file */ 5909 int dirSync /* If true, fsync() directory after deleting file */
5167 ){ 5910 ){
5168 int rc = SQLITE_OK; 5911 int rc = SQLITE_OK;
5169 UNUSED_PARAMETER(NotUsed); 5912 UNUSED_PARAMETER(NotUsed);
5170 SimulateIOError(return SQLITE_IOERR_DELETE); 5913 SimulateIOError(return SQLITE_IOERR_DELETE);
5171 if( osUnlink(zPath)==(-1) && errno!=ENOENT ){ 5914 if( osUnlink(zPath)==(-1) ){
5172 return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath); 5915 if( errno==ENOENT
5916 #if OS_VXWORKS
5917 || osAccess(zPath,0)!=0
5918 #endif
5919 ){
5920 rc = SQLITE_IOERR_DELETE_NOENT;
5921 }else{
5922 rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
5923 }
5924 return rc;
5173 } 5925 }
5174 #ifndef SQLITE_DISABLE_DIRSYNC 5926 #ifndef SQLITE_DISABLE_DIRSYNC
5175 if( dirSync ){ 5927 if( (dirSync & 1)!=0 ){
5176 int fd; 5928 int fd;
5177 rc = osOpenDirectory(zPath, &fd); 5929 rc = osOpenDirectory(zPath, &fd);
5178 if( rc==SQLITE_OK ){ 5930 if( rc==SQLITE_OK ){
5179 #if OS_VXWORKS 5931 #if OS_VXWORKS
5180 if( fsync(fd)==-1 ) 5932 if( fsync(fd)==-1 )
5181 #else 5933 #else
5182 if( fsync(fd) ) 5934 if( fsync(fd) )
5183 #endif 5935 #endif
5184 { 5936 {
5185 rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath); 5937 rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
5186 } 5938 }
5187 robust_close(0, fd, __LINE__); 5939 robust_close(0, fd, __LINE__);
5188 }else if( rc==SQLITE_CANTOPEN ){ 5940 }else if( rc==SQLITE_CANTOPEN ){
5189 rc = SQLITE_OK; 5941 rc = SQLITE_OK;
5190 } 5942 }
5191 } 5943 }
5192 #endif 5944 #endif
5193 return rc; 5945 return rc;
5194 } 5946 }
5195 5947
5196 /* 5948 /*
5197 ** Test the existance of or access permissions of file zPath. The 5949 ** Test the existence of or access permissions of file zPath. The
5198 ** test performed depends on the value of flags: 5950 ** test performed depends on the value of flags:
5199 ** 5951 **
5200 ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists 5952 ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
5201 ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. 5953 ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
5202 ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. 5954 ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
5203 ** 5955 **
5204 ** Otherwise return 0. 5956 ** Otherwise return 0.
5205 */ 5957 */
5206 static int unixAccess( 5958 static int unixAccess(
5207 sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */ 5959 sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
5353 ** uninitialized space in zBuf - but valgrind errors tend to worry 6105 ** uninitialized space in zBuf - but valgrind errors tend to worry
5354 ** some users. Rather than argue, it seems easier just to initialize 6106 ** some users. Rather than argue, it seems easier just to initialize
5355 ** the whole array and silence valgrind, even if that means less randomness 6107 ** the whole array and silence valgrind, even if that means less randomness
5356 ** in the random seed. 6108 ** in the random seed.
5357 ** 6109 **
5358 ** When testing, initializing zBuf[] to zero is all we do. That means 6110 ** When testing, initializing zBuf[] to zero is all we do. That means
5359 ** that we always use the same random number sequence. This makes the 6111 ** that we always use the same random number sequence. This makes the
5360 ** tests repeatable. 6112 ** tests repeatable.
5361 */ 6113 */
5362 memset(zBuf, 0, nBuf); 6114 memset(zBuf, 0, nBuf);
6115 randomnessPid = getpid();
5363 #if !defined(SQLITE_TEST) 6116 #if !defined(SQLITE_TEST)
5364 { 6117 {
5365 int pid, fd; 6118 int fd, got;
5366 fd = robust_open("/dev/urandom", O_RDONLY, 0); 6119 fd = robust_open("/dev/urandom", O_RDONLY, 0);
5367 if( fd<0 ){ 6120 if( fd<0 ){
5368 time_t t; 6121 time_t t;
5369 time(&t); 6122 time(&t);
5370 memcpy(zBuf, &t, sizeof(t)); 6123 memcpy(zBuf, &t, sizeof(t));
5371 pid = getpid(); 6124 memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid));
5372 memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid)); 6125 assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf );
5373 assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf ); 6126 nBuf = sizeof(t) + sizeof(randomnessPid);
5374 nBuf = sizeof(t) + sizeof(pid);
5375 }else{ 6127 }else{
5376 do{ nBuf = osRead(fd, zBuf, nBuf); }while( nBuf<0 && errno==EINTR ); 6128 do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR );
5377 robust_close(0, fd, __LINE__); 6129 robust_close(0, fd, __LINE__);
5378 } 6130 }
5379 } 6131 }
5380 #endif 6132 #endif
5381 return nBuf; 6133 return nBuf;
5382 } 6134 }
5383 6135
5384 6136
5385 /* 6137 /*
5386 ** Sleep for a little while. Return the amount of time slept. 6138 ** Sleep for a little while. Return the amount of time slept.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
5420 int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ 6172 int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
5421 #endif 6173 #endif
5422 6174
5423 /* 6175 /*
5424 ** Find the current time (in Universal Coordinated Time). Write into *piNow 6176 ** Find the current time (in Universal Coordinated Time). Write into *piNow
5425 ** the current time and date as a Julian Day number times 86_400_000. In 6177 ** the current time and date as a Julian Day number times 86_400_000. In
5426 ** other words, write into *piNow the number of milliseconds since the Julian 6178 ** other words, write into *piNow the number of milliseconds since the Julian
5427 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the 6179 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
5428 ** proleptic Gregorian calendar. 6180 ** proleptic Gregorian calendar.
5429 ** 6181 **
5430 ** On success, return 0. Return 1 if the time and date cannot be found. 6182 ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
6183 ** cannot be found.
5431 */ 6184 */
5432 static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ 6185 static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
5433 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; 6186 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
6187 int rc = SQLITE_OK;
5434 #if defined(NO_GETTOD) 6188 #if defined(NO_GETTOD)
5435 time_t t; 6189 time_t t;
5436 time(&t); 6190 time(&t);
5437 *piNow = ((sqlite3_int64)t)*1000 + unixEpoch; 6191 *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
5438 #elif OS_VXWORKS 6192 #elif OS_VXWORKS
5439 struct timespec sNow; 6193 struct timespec sNow;
5440 clock_gettime(CLOCK_REALTIME, &sNow); 6194 clock_gettime(CLOCK_REALTIME, &sNow);
5441 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; 6195 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
5442 #else 6196 #else
5443 struct timeval sNow; 6197 struct timeval sNow;
5444 gettimeofday(&sNow, 0); 6198 if( gettimeofday(&sNow, 0)==0 ){
5445 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; 6199 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
6200 }else{
6201 rc = SQLITE_ERROR;
6202 }
5446 #endif 6203 #endif
5447 6204
5448 #ifdef SQLITE_TEST 6205 #ifdef SQLITE_TEST
5449 if( sqlite3_current_time ){ 6206 if( sqlite3_current_time ){
5450 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; 6207 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
5451 } 6208 }
5452 #endif 6209 #endif
5453 UNUSED_PARAMETER(NotUsed); 6210 UNUSED_PARAMETER(NotUsed);
5454 return 0; 6211 return rc;
5455 } 6212 }
5456 6213
5457 /* 6214 /*
5458 ** Find the current time (in Universal Coordinated Time). Write the 6215 ** Find the current time (in Universal Coordinated Time). Write the
5459 ** current time and date as a Julian Day number into *prNow and 6216 ** current time and date as a Julian Day number into *prNow and
5460 ** return 0. Return 1 if the time and date cannot be found. 6217 ** return 0. Return 1 if the time and date cannot be found.
5461 */ 6218 */
5462 static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ 6219 static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
5463 sqlite3_int64 i; 6220 sqlite3_int64 i = 0;
6221 int rc;
5464 UNUSED_PARAMETER(NotUsed); 6222 UNUSED_PARAMETER(NotUsed);
5465 unixCurrentTimeInt64(0, &i); 6223 rc = unixCurrentTimeInt64(0, &i);
5466 *prNow = i/86400000.0; 6224 *prNow = i/86400000.0;
5467 return 0; 6225 return rc;
5468 } 6226 }
5469 6227
5470 /* 6228 /*
5471 ** We added the xGetLastError() method with the intention of providing 6229 ** We added the xGetLastError() method with the intention of providing
5472 ** better low-level error messages when operating-system problems come up 6230 ** better low-level error messages when operating-system problems come up
5473 ** during SQLite operation. But so far, none of that has been implemented 6231 ** during SQLite operation. But so far, none of that has been implemented
5474 ** in the core. So this routine is never called. For now, it is merely 6232 ** in the core. So this routine is never called. For now, it is merely
5475 ** a place-holder. 6233 ** a place-holder.
5476 */ 6234 */
5477 static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ 6235 static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
(...skipping 25 matching lines...) Expand all
5503 ** database file to coordinate safe, concurrent access by multiple readers 6261 ** database file to coordinate safe, concurrent access by multiple readers
5504 ** and writers [http://sqlite.org/lockingv3.html]. The five file locking 6262 ** and writers [http://sqlite.org/lockingv3.html]. The five file locking
5505 ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented 6263 ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
5506 ** as POSIX read & write locks over fixed set of locations (via fsctl), 6264 ** as POSIX read & write locks over fixed set of locations (via fsctl),
5507 ** on AFP and SMB only exclusive byte-range locks are available via fsctl 6265 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
5508 ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. 6266 ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
5509 ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected 6267 ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
5510 ** address in the shared range is taken for a SHARED lock, the entire 6268 ** address in the shared range is taken for a SHARED lock, the entire
5511 ** shared range is taken for an EXCLUSIVE lock): 6269 ** shared range is taken for an EXCLUSIVE lock):
5512 ** 6270 **
5513 ** PENDING_BYTE 0x40000000» » » 6271 ** PENDING_BYTE 0x40000000
5514 ** RESERVED_BYTE 0x40000001 6272 ** RESERVED_BYTE 0x40000001
5515 ** SHARED_RANGE 0x40000002 -> 0x40000200 6273 ** SHARED_RANGE 0x40000002 -> 0x40000200
5516 ** 6274 **
5517 ** This works well on the local file system, but shows a nearly 100x 6275 ** This works well on the local file system, but shows a nearly 100x
5518 ** slowdown in read performance on AFP because the AFP client disables 6276 ** slowdown in read performance on AFP because the AFP client disables
5519 ** the read cache when byte-range locks are present. Enabling the read 6277 ** the read cache when byte-range locks are present. Enabling the read
5520 ** cache exposes a cache coherency problem that is present on all OS X 6278 ** cache exposes a cache coherency problem that is present on all OS X
5521 ** supported network file systems. NFS and AFP both observe the 6279 ** supported network file systems. NFS and AFP both observe the
5522 ** close-to-open semantics for ensuring cache coherency 6280 ** close-to-open semantics for ensuring cache coherency
5523 ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively 6281 ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
5572 ** 6330 **
5573 ** * proxy file to act as a proxy for the advisory locks normally 6331 ** * proxy file to act as a proxy for the advisory locks normally
5574 ** taken on the database 6332 ** taken on the database
5575 ** 6333 **
5576 ** The conch file - to use a proxy file, sqlite must first "hold the conch" 6334 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
5577 ** by taking an sqlite-style shared lock on the conch file, reading the 6335 ** by taking an sqlite-style shared lock on the conch file, reading the
5578 ** contents and comparing the host's unique host ID (see below) and lock 6336 ** contents and comparing the host's unique host ID (see below) and lock
5579 ** proxy path against the values stored in the conch. The conch file is 6337 ** proxy path against the values stored in the conch. The conch file is
5580 ** stored in the same directory as the database file and the file name 6338 ** stored in the same directory as the database file and the file name
5581 ** is patterned after the database file name as ".<databasename>-conch". 6339 ** is patterned after the database file name as ".<databasename>-conch".
5582 ** If the conch file does not exist, or it's contents do not match the 6340 ** If the conch file does not exist, or its contents do not match the
5583 ** host ID and/or proxy path, then the lock is escalated to an exclusive 6341 ** host ID and/or proxy path, then the lock is escalated to an exclusive
5584 ** lock and the conch file contents is updated with the host ID and proxy 6342 ** lock and the conch file contents is updated with the host ID and proxy
5585 ** path and the lock is downgraded to a shared lock again. If the conch 6343 ** path and the lock is downgraded to a shared lock again. If the conch
5586 ** is held by another process (with a shared lock), the exclusive lock 6344 ** is held by another process (with a shared lock), the exclusive lock
5587 ** will fail and SQLITE_BUSY is returned. 6345 ** will fail and SQLITE_BUSY is returned.
5588 ** 6346 **
5589 ** The proxy file - a single-byte file used for all advisory file locks 6347 ** The proxy file - a single-byte file used for all advisory file locks
5590 ** normally taken on the database file. This allows for safe sharing 6348 ** normally taken on the database file. This allows for safe sharing
5591 ** of the database file for multiple readers and writers on the same 6349 ** of the database file for multiple readers and writers on the same
5592 ** host (the conch ensures that they all use the same local lock file). 6350 ** host (the conch ensures that they all use the same local lock file).
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
5624 ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 6382 ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
5625 ** 6383 **
5626 ** Permissions to use when creating a directory for storing the 6384 ** Permissions to use when creating a directory for storing the
5627 ** lock proxy files, only used when LOCKPROXYDIR is not set. 6385 ** lock proxy files, only used when LOCKPROXYDIR is not set.
5628 ** 6386 **
5629 ** 6387 **
5630 ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, 6388 ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
5631 ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will 6389 ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
5632 ** force proxy locking to be used for every database file opened, and 0 6390 ** force proxy locking to be used for every database file opened, and 0
5633 ** will force automatic proxy locking to be disabled for all database 6391 ** will force automatic proxy locking to be disabled for all database
5634 ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or 6392 ** files (explicitly calling the SQLITE_SET_LOCKPROXYFILE pragma or
5635 ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). 6393 ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
5636 */ 6394 */
5637 6395
5638 /* 6396 /*
5639 ** Proxy locking is only available on MacOSX 6397 ** Proxy locking is only available on MacOSX
5640 */ 6398 */
5641 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE 6399 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
5642 6400
5643 /* 6401 /*
5644 ** The proxyLockingContext has the path and file structures for the remote 6402 ** The proxyLockingContext has the path and file structures for the remote
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
5710 assert(lockPath!=NULL); 6468 assert(lockPath!=NULL);
5711 /* try to create all the intermediate directories */ 6469 /* try to create all the intermediate directories */
5712 len = (int)strlen(lockPath); 6470 len = (int)strlen(lockPath);
5713 buf[0] = lockPath[0]; 6471 buf[0] = lockPath[0];
5714 for( i=1; i<len; i++ ){ 6472 for( i=1; i<len; i++ ){
5715 if( lockPath[i] == '/' && (i - start > 0) ){ 6473 if( lockPath[i] == '/' && (i - start > 0) ){
5716 /* only mkdir if leaf dir != "." or "/" or ".." */ 6474 /* only mkdir if leaf dir != "." or "/" or ".." */
5717 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') 6475 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
5718 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ 6476 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
5719 buf[i]='\0'; 6477 buf[i]='\0';
5720 if( mkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ 6478 if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
5721 int err=errno; 6479 int err=errno;
5722 if( err!=EEXIST ) { 6480 if( err!=EEXIST ) {
5723 OSTRACE(("CREATELOCKPATH FAILED creating %s, " 6481 OSTRACE(("CREATELOCKPATH FAILED creating %s, "
5724 "'%s' proxy lock path=%s pid=%d\n", 6482 "'%s' proxy lock path=%s pid=%d\n",
5725 buf, strerror(err), lockPath, getpid())); 6483 buf, strerror(err), lockPath, getpid()));
5726 return err; 6484 return err;
5727 } 6485 }
5728 } 6486 }
5729 } 6487 }
5730 start=i+1; 6488 start=i+1;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
5764 pUnused = findReusableFd(path, openFlags); 6522 pUnused = findReusableFd(path, openFlags);
5765 if( pUnused ){ 6523 if( pUnused ){
5766 fd = pUnused->fd; 6524 fd = pUnused->fd;
5767 }else{ 6525 }else{
5768 pUnused = sqlite3_malloc(sizeof(*pUnused)); 6526 pUnused = sqlite3_malloc(sizeof(*pUnused));
5769 if( !pUnused ){ 6527 if( !pUnused ){
5770 return SQLITE_NOMEM; 6528 return SQLITE_NOMEM;
5771 } 6529 }
5772 } 6530 }
5773 if( fd<0 ){ 6531 if( fd<0 ){
5774 fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); 6532 fd = robust_open(path, openFlags, 0);
5775 terrno = errno; 6533 terrno = errno;
5776 if( fd<0 && errno==ENOENT && islockfile ){ 6534 if( fd<0 && errno==ENOENT && islockfile ){
5777 if( proxyCreateLockPath(path) == SQLITE_OK ){ 6535 if( proxyCreateLockPath(path) == SQLITE_OK ){
5778 fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); 6536 fd = robust_open(path, openFlags, 0);
5779 } 6537 }
5780 } 6538 }
5781 } 6539 }
5782 if( fd<0 ){ 6540 if( fd<0 ){
5783 openFlags = O_RDONLY; 6541 openFlags = O_RDONLY;
5784 fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); 6542 fd = robust_open(path, openFlags, 0);
5785 terrno = errno; 6543 terrno = errno;
5786 } 6544 }
5787 if( fd<0 ){ 6545 if( fd<0 ){
5788 if( islockfile ){ 6546 if( islockfile ){
5789 return SQLITE_BUSY; 6547 return SQLITE_BUSY;
5790 } 6548 }
5791 switch (terrno) { 6549 switch (terrno) {
5792 case EACCES: 6550 case EACCES:
5793 return SQLITE_PERM; 6551 return SQLITE_PERM;
5794 case EIO: 6552 case EIO:
(...skipping 10 matching lines...) Expand all
5805 } 6563 }
5806 memset(pNew, 0, sizeof(unixFile)); 6564 memset(pNew, 0, sizeof(unixFile));
5807 pNew->openFlags = openFlags; 6565 pNew->openFlags = openFlags;
5808 memset(&dummyVfs, 0, sizeof(dummyVfs)); 6566 memset(&dummyVfs, 0, sizeof(dummyVfs));
5809 dummyVfs.pAppData = (void*)&autolockIoFinder; 6567 dummyVfs.pAppData = (void*)&autolockIoFinder;
5810 dummyVfs.zName = "dummy"; 6568 dummyVfs.zName = "dummy";
5811 pUnused->fd = fd; 6569 pUnused->fd = fd;
5812 pUnused->flags = openFlags; 6570 pUnused->flags = openFlags;
5813 pNew->pUnused = pUnused; 6571 pNew->pUnused = pUnused;
5814 6572
5815 rc = fillInUnixFile(&dummyVfs, fd, 0, (sqlite3_file*)pNew, path, 0, 0, 0); 6573 rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
5816 if( rc==SQLITE_OK ){ 6574 if( rc==SQLITE_OK ){
5817 *ppFile = pNew; 6575 *ppFile = pNew;
5818 return SQLITE_OK; 6576 return SQLITE_OK;
5819 } 6577 }
5820 end_create_proxy: 6578 end_create_proxy:
5821 robust_close(pNew, fd, __LINE__); 6579 robust_close(pNew, fd, __LINE__);
5822 sqlite3_free(pNew); 6580 sqlite3_free(pNew);
5823 sqlite3_free(pUnused); 6581 sqlite3_free(pUnused);
5824 return rc; 6582 return rc;
5825 } 6583 }
(...skipping 19 matching lines...) Expand all
5845 { 6603 {
5846 static const struct timespec timeout = {1, 0}; /* 1 sec timeout */ 6604 static const struct timespec timeout = {1, 0}; /* 1 sec timeout */
5847 if( gethostuuid(pHostID, &timeout) ){ 6605 if( gethostuuid(pHostID, &timeout) ){
5848 int err = errno; 6606 int err = errno;
5849 if( pError ){ 6607 if( pError ){
5850 *pError = err; 6608 *pError = err;
5851 } 6609 }
5852 return SQLITE_IOERR; 6610 return SQLITE_IOERR;
5853 } 6611 }
5854 } 6612 }
6613 #else
6614 UNUSED_PARAMETER(pError);
5855 #endif 6615 #endif
5856 #ifdef SQLITE_TEST 6616 #ifdef SQLITE_TEST
5857 /* simulate multiple hosts by creating unique hostid file paths */ 6617 /* simulate multiple hosts by creating unique hostid file paths */
5858 if( sqlite3_hostid_num != 0){ 6618 if( sqlite3_hostid_num != 0){
5859 pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); 6619 pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
5860 } 6620 }
5861 #endif 6621 #endif
5862 6622
5863 return SQLITE_OK; 6623 return SQLITE_OK;
5864 } 6624 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
5896 sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen); 6656 sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
5897 goto end_breaklock; 6657 goto end_breaklock;
5898 } 6658 }
5899 /* read the conch content */ 6659 /* read the conch content */
5900 readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); 6660 readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
5901 if( readLen<PROXY_PATHINDEX ){ 6661 if( readLen<PROXY_PATHINDEX ){
5902 sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen); 6662 sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
5903 goto end_breaklock; 6663 goto end_breaklock;
5904 } 6664 }
5905 /* write it out to the temporary break file */ 6665 /* write it out to the temporary break file */
5906 fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 6666 fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0);
5907 SQLITE_DEFAULT_FILE_PERMISSIONS);
5908 if( fd<0 ){ 6667 if( fd<0 ){
5909 sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno); 6668 sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
5910 goto end_breaklock; 6669 goto end_breaklock;
5911 } 6670 }
5912 if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){ 6671 if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
5913 sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno); 6672 sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
5914 goto end_breaklock; 6673 goto end_breaklock;
5915 } 6674 }
5916 if( rename(tPath, cPath) ){ 6675 if( rename(tPath, cPath) ){
5917 sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno); 6676 sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
(...skipping 19 matching lines...) Expand all
5937 /* Take the requested lock on the conch file and break a stale lock if the 6696 /* Take the requested lock on the conch file and break a stale lock if the
5938 ** host id matches. 6697 ** host id matches.
5939 */ 6698 */
5940 static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ 6699 static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
5941 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; 6700 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
5942 unixFile *conchFile = pCtx->conchFile; 6701 unixFile *conchFile = pCtx->conchFile;
5943 int rc = SQLITE_OK; 6702 int rc = SQLITE_OK;
5944 int nTries = 0; 6703 int nTries = 0;
5945 struct timespec conchModTime; 6704 struct timespec conchModTime;
5946 6705
6706 memset(&conchModTime, 0, sizeof(conchModTime));
5947 do { 6707 do {
5948 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); 6708 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
5949 nTries ++; 6709 nTries ++;
5950 if( rc==SQLITE_BUSY ){ 6710 if( rc==SQLITE_BUSY ){
5951 /* If the lock failed (busy): 6711 /* If the lock failed (busy):
5952 * 1st try: get the mod time of the conch, wait 0.5s and try again. 6712 * 1st try: get the mod time of the conch, wait 0.5s and try again.
5953 * 2nd try: fail if the mod time changed or host id is different, wait 6713 * 2nd try: fail if the mod time changed or host id is different, wait
5954 * 10 sec and try again 6714 * 10 sec and try again
5955 * 3rd try: break the lock unless the mod time has changed. 6715 * 3rd try: break the lock unless the mod time has changed.
5956 */ 6716 */
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
6168 err, code, strerror(code)); 6928 err, code, strerror(code));
6169 #endif 6929 #endif
6170 } 6930 }
6171 } 6931 }
6172 } 6932 }
6173 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); 6933 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
6174 6934
6175 end_takeconch: 6935 end_takeconch:
6176 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); 6936 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
6177 if( rc==SQLITE_OK && pFile->openFlags ){ 6937 if( rc==SQLITE_OK && pFile->openFlags ){
6938 int fd;
6178 if( pFile->h>=0 ){ 6939 if( pFile->h>=0 ){
6179 robust_close(pFile, pFile->h, __LINE__); 6940 robust_close(pFile, pFile->h, __LINE__);
6180 } 6941 }
6181 pFile->h = -1; 6942 pFile->h = -1;
6182 int fd = robust_open(pCtx->dbPath, pFile->openFlags, 6943 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
6183 SQLITE_DEFAULT_FILE_PERMISSIONS);
6184 OSTRACE(("TRANSPROXY: OPEN %d\n", fd)); 6944 OSTRACE(("TRANSPROXY: OPEN %d\n", fd));
6185 if( fd>=0 ){ 6945 if( fd>=0 ){
6186 pFile->h = fd; 6946 pFile->h = fd;
6187 }else{ 6947 }else{
6188 rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called 6948 rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
6189 during locking */ 6949 during locking */
6190 } 6950 }
6191 } 6951 }
6192 if( rc==SQLITE_OK && !pCtx->lockProxy ){ 6952 if( rc==SQLITE_OK && !pCtx->lockProxy ){
6193 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath; 6953 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
(...skipping 548 matching lines...) Expand 10 before | Expand all | Expand 10 after
6742 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) 7502 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
6743 UNIXVFS("unix-afp", afpIoFinder ), 7503 UNIXVFS("unix-afp", afpIoFinder ),
6744 UNIXVFS("unix-nfs", nfsIoFinder ), 7504 UNIXVFS("unix-nfs", nfsIoFinder ),
6745 UNIXVFS("unix-proxy", proxyIoFinder ), 7505 UNIXVFS("unix-proxy", proxyIoFinder ),
6746 #endif 7506 #endif
6747 }; 7507 };
6748 unsigned int i; /* Loop counter */ 7508 unsigned int i; /* Loop counter */
6749 7509
6750 /* Double-check that the aSyscall[] array has been constructed 7510 /* Double-check that the aSyscall[] array has been constructed
6751 ** correctly. See ticket [bb3a86e890c8e96ab] */ 7511 ** correctly. See ticket [bb3a86e890c8e96ab] */
6752 assert( ArraySize(aSyscall)==18 ); 7512 assert( ArraySize(aSyscall)==25 );
6753 7513
6754 /* Register all VFSes defined in the aVfs[] array */ 7514 /* Register all VFSes defined in the aVfs[] array */
6755 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ 7515 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
6756 sqlite3_vfs_register(&aVfs[i], i==0); 7516 sqlite3_vfs_register(&aVfs[i], i==0);
6757 } 7517 }
6758 return SQLITE_OK; 7518 return SQLITE_OK;
6759 } 7519 }
6760 7520
6761 /* 7521 /*
6762 ** Shutdown the operating system interface. 7522 ** Shutdown the operating system interface.
6763 ** 7523 **
6764 ** Some operating systems might need to do some cleanup in this routine, 7524 ** Some operating systems might need to do some cleanup in this routine,
6765 ** to release dynamically allocated objects. But not on unix. 7525 ** to release dynamically allocated objects. But not on unix.
6766 ** This routine is a no-op for unix. 7526 ** This routine is a no-op for unix.
6767 */ 7527 */
6768 int sqlite3_os_end(void){ 7528 int sqlite3_os_end(void){
6769 return SQLITE_OK; 7529 return SQLITE_OK;
6770 } 7530 }
6771 7531
6772 #endif /* SQLITE_OS_UNIX */ 7532 #endif /* SQLITE_OS_UNIX */
OLDNEW
« no previous file with comments | « third_party/sqlite/sqlite-src-3080704/src/os_setup.h ('k') | third_party/sqlite/sqlite-src-3080704/src/os_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698