1 This is a backport of the following sqlite changes:
3 1. http://sqlite.org/src/ci/9109128cb5
4 2. http://sqlite.org/src/ci/713b1b7dc1
5 3. http://sqlite.org/src/ci/8d1b5c3ac0
6 4. http://sqlite.org/src/ci/6b236069e1
7 5. http://sqlite.org/src/ci/880b51150a
9 which are needed for experiments with using unpatched sqlite.
10 If you hit a merge conflict on this file it is most likely
11 that you've upgraded to version of sqlite that includes those patches.
12 diff --git a/third_party/sqlite/amalgamation/sqlite3.c b/third_party/sqlite/amalgamation/sqlite3.c
13 index 3e794a9..73ff15f 100644
14 --- a/third_party/sqlite/amalgamation/sqlite3.c
15 +++ b/third_party/sqlite/amalgamation/sqlite3.c
16 @@ -24144,7 +24144,6 @@ struct unixFile {
17 sqlite3_io_methods const *pMethod; /* Always the first entry */
18 unixInodeInfo *pInode; /* Info about locks on this inode */
19 int h; /* The file descriptor */
20 - int dirfd; /* File descriptor for the directory */
21 unsigned char eFileLock; /* The type of lock held on this fd */
22 unsigned char ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
23 int lastErrno; /* The unix errno from last I/O error */
24 @@ -24188,6 +24187,7 @@ struct unixFile {
26 #define UNIXFILE_EXCL 0x01 /* Connections from one process only */
27 #define UNIXFILE_RDONLY 0x02 /* Connection is read only */
28 +#define UNIXFILE_DIRSYNC 0x04 /* Directory sync needed */
31 ** Include code that is common to all os_*.c files
32 @@ -24426,6 +24426,9 @@ SQLITE_API int sqlite3_open_file_count = 0;
36 +/* Forward reference */
37 +static int openDirectory(const char*, int*);
40 ** Many system calls are accessed through pointer-to-functions so that
41 ** they may be overridden at runtime to facilitate fault injection during
42 @@ -24522,6 +24525,12 @@ static struct unix_syscall {
44 #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
46 + { "unlink", (sqlite3_syscall_ptr)unlink, 0 },
47 +#define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent)
49 + { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 },
50 +#define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
52 }; /* End of the overrideable system calls */
55 @@ -25876,10 +25885,6 @@ static int unixUnlock(sqlite3_file *id, int eFileLock){
57 static int closeUnixFile(sqlite3_file *id){
58 unixFile *pFile = (unixFile*)id;
59 - if( pFile->dirfd>=0 ){
60 - robust_close(pFile, pFile->dirfd, __LINE__);
64 robust_close(pFile, pFile->h, __LINE__);
66 @@ -25887,7 +25892,7 @@ static int closeUnixFile(sqlite3_file *id){
69 if( pFile->isDelete ){
70 - unlink(pFile->pId->zCanonicalName);
71 + osUnlink(pFile->pId->zCanonicalName);
73 vxworksReleaseFileId(pFile->pId);
75 @@ -26134,7 +26139,7 @@ static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
77 /* To fully unlock the database, delete the lock file */
78 assert( eFileLock==NO_LOCK );
79 - if( unlink(zLockFile) ){
80 + if( osUnlink(zLockFile) ){
83 if( ENOENT != tErrno ){
84 @@ -27371,6 +27376,50 @@ static int full_fsync(int fd, int fullSync, int dataOnly){
88 +** Open a file descriptor to the directory containing file zFilename.
89 +** If successful, *pFd is set to the opened file descriptor and
90 +** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
91 +** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
94 +** The directory file descriptor is used for only one thing - to
95 +** fsync() a directory to make sure file creation and deletion events
96 +** are flushed to disk. Such fsyncs are not needed on newer
97 +** journaling filesystems, but are required on older filesystems.
99 +** This routine can be overridden using the xSetSysCall interface.
100 +** The ability to override this routine was added in support of the
101 +** chromium sandbox. Opening a directory is a security risk (we are
102 +** told) so making it overrideable allows the chromium sandbox to
103 +** replace this routine with a harmless no-op. To make this routine
104 +** a no-op, replace it with a stub that returns SQLITE_OK but leaves
105 +** *pFd set to a negative number.
107 +** If SQLITE_OK is returned, the caller is responsible for closing
108 +** the file descriptor *pFd using close().
110 +static int openDirectory(const char *zFilename, int *pFd){
113 + char zDirname[MAX_PATHNAME+1];
115 + sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
116 + for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
118 + zDirname[ii] = '\0';
119 + fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
122 + osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
124 + OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
128 + return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
132 ** Make sure all writes to a particular file are committed to disk.
134 ** If dataOnly==0 then both the file itself and its metadata (file
135 @@ -27410,28 +27459,21 @@ static int unixSync(sqlite3_file *id, int flags){
136 pFile->lastErrno = errno;
137 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
139 - if( pFile->dirfd>=0 ){
140 - OSTRACE(("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
142 + /* Also fsync the directory containing the file if the DIRSYNC flag
143 + ** is set. This is a one-time occurrance. Many systems (examples: AIX)
144 + ** are unable to fsync a directory, so ignore errors on the fsync.
146 + if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
148 + OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
149 HAVE_FULLFSYNC, isFullsync));
150 -#ifndef SQLITE_DISABLE_DIRSYNC
151 - /* The directory sync is only attempted if full_fsync is
152 - ** turned off or unavailable. If a full_fsync occurred above,
153 - ** then the directory sync is superfluous.
155 - if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
157 - ** We have received multiple reports of fsync() returning
158 - ** errors when applied to directories on certain file systems.
159 - ** A failed directory sync is not a big deal. So it seems
160 - ** better to ignore the error. Ticket #1657
162 - /* pFile->lastErrno = errno; */
163 - /* return SQLITE_IOERR; */
164 + rc = osOpenDirectory(pFile->zPath, &dirfd);
165 + if( rc==SQLITE_OK && dirfd>=0 ){
166 + full_fsync(dirfd, 0, 0);
167 + robust_close(pFile, dirfd, __LINE__);
170 - /* Only need to sync once, so close the directory when we are done */
171 - robust_close(pFile, pFile->dirfd, __LINE__);
173 + pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
177 @@ -28255,7 +28297,7 @@ static int unixShmUnmap(
178 assert( pShmNode->nRef>0 );
180 if( pShmNode->nRef==0 ){
181 - if( deleteFlag && pShmNode->h>=0 ) unlink(pShmNode->zFilename);
182 + if( deleteFlag && pShmNode->h>=0 ) osUnlink(pShmNode->zFilename);
186 @@ -28575,7 +28617,7 @@ void initUnixFile(sqlite3_file* file) {
188 sqlite3_vfs *pVfs, /* Pointer to vfs object */
189 int h, /* Open file descriptor of file being opened */
190 - int dirfd, /* Directory file descriptor */
191 + int syncDir, /* True to sync directory on first sync */
192 sqlite3_file *pId, /* Write to the unixFile structure here */
193 const char *zFilename, /* Name of the file being opened */
194 int noLock, /* Omit locking if true */
195 @@ -28606,7 +28648,6 @@ int fillInUnixFile(
197 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
199 - pNew->dirfd = dirfd;
200 pNew->zPath = zFilename;
201 if( memcmp(pVfs->zName,"unix-excl",10)==0 ){
202 pNew->ctrlFlags = UNIXFILE_EXCL;
203 @@ -28616,6 +28657,9 @@ int fillInUnixFile(
205 pNew->ctrlFlags |= UNIXFILE_RDONLY;
208 + pNew->ctrlFlags |= UNIXFILE_DIRSYNC;
212 pNew->pId = vxworksFindFileId(zFilename);
213 @@ -28742,13 +28786,12 @@ int fillInUnixFile(
215 if( h>=0 ) robust_close(pNew, h, __LINE__);
218 + osUnlink(zFilename);
221 pNew->isDelete = isDelete;
224 - if( dirfd>=0 ) robust_close(pNew, dirfd, __LINE__);
225 if( h>=0 ) robust_close(pNew, h, __LINE__);
227 pNew->pMethod = pLockingStyle;
228 @@ -28758,37 +28801,6 @@ int fillInUnixFile(
232 -** Open a file descriptor to the directory containing file zFilename.
233 -** If successful, *pFd is set to the opened file descriptor and
234 -** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
235 -** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
238 -** If SQLITE_OK is returned, the caller is responsible for closing
239 -** the file descriptor *pFd using close().
241 -static int openDirectory(const char *zFilename, int *pFd){
244 - char zDirname[MAX_PATHNAME+1];
246 - sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
247 - for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
249 - zDirname[ii] = '\0';
250 - fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
253 - osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
255 - OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
259 - return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
263 ** Return the name of a directory in which to put temporary files.
264 ** If no suitable temporary file directory can be found, return NULL.
266 @@ -29083,7 +29095,6 @@ static int unixOpen(
268 unixFile *p = (unixFile *)pFile;
269 int fd = -1; /* File descriptor returned by open() */
270 - int dirfd = -1; /* Directory file descriptor */
271 int openFlags = 0; /* Flags to pass to open() */
272 int eType = flags&0xFFFFFF00; /* Type of file to open */
273 int noLock; /* True to omit locking primitives */
274 @@ -29102,7 +29113,7 @@ static int unixOpen(
275 ** a file-descriptor on the directory too. The first time unixSync()
276 ** is called the directory file descriptor will be fsync()ed and close()d.
278 - int isOpenDirectory = (isCreate && (
279 + int syncDir = (isCreate && (
280 eType==SQLITE_OPEN_MASTER_JOURNAL
281 || eType==SQLITE_OPEN_MAIN_JOURNAL
282 || eType==SQLITE_OPEN_WAL
283 @@ -29149,7 +29160,7 @@ static int unixOpen(
286 /* If zName is NULL, the upper layer is requesting a temp file. */
287 - assert(isDelete && !isOpenDirectory);
288 + assert(isDelete && !syncDir);
289 rc = unixGetTempname(MAX_PATHNAME+1, zTmpname);
292 @@ -29202,7 +29213,7 @@ static int unixOpen(
300 #if SQLITE_ENABLE_LOCKING_STYLE
301 @@ -29211,19 +29222,6 @@ static int unixOpen(
305 - if( isOpenDirectory ){
306 - rc = openDirectory(zPath, &dirfd);
307 - if( rc!=SQLITE_OK ){
308 - /* It is safe to close fd at this point, because it is guaranteed not
309 - ** to be open on a database file. If it were open on a database file,
310 - ** it would not be safe to close as this would release any locks held
311 - ** on the file by this process. */
312 - assert( eType!=SQLITE_OPEN_MAIN_DB );
313 - robust_close(p, fd, __LINE__);
314 - goto open_finished;
319 osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
321 @@ -29235,7 +29233,6 @@ static int unixOpen(
322 struct statfs fsInfo;
323 if( fstatfs(fd, &fsInfo) == -1 ){
324 ((unixFile*)pFile)->lastErrno = errno;
325 - if( dirfd>=0 ) robust_close(p, dirfd, __LINE__);
326 robust_close(p, fd, __LINE__);
327 return SQLITE_IOERR_ACCESS;
329 @@ -29267,9 +29264,6 @@ static int unixOpen(
330 ** not while other file descriptors opened by the same process on
331 ** the same file are working. */
332 p->lastErrno = errno;
334 - robust_close(p, dirfd, __LINE__);
336 robust_close(p, fd, __LINE__);
337 rc = SQLITE_IOERR_ACCESS;
339 @@ -29277,7 +29271,7 @@ static int unixOpen(
340 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
343 - rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock,
344 + rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock,
345 isDelete, isReadonly);
347 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
348 @@ -29295,7 +29289,7 @@ static int unixOpen(
352 - rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock,
353 + rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock,
354 isDelete, isReadonly);
357 @@ -29317,13 +29311,13 @@ static int unixDelete(
359 UNUSED_PARAMETER(NotUsed);
360 SimulateIOError(return SQLITE_IOERR_DELETE);
361 - if( unlink(zPath)==(-1) && errno!=ENOENT ){
362 + if( osUnlink(zPath)==(-1) && errno!=ENOENT ){
363 return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
365 #ifndef SQLITE_DISABLE_DIRSYNC
368 - rc = openDirectory(zPath, &fd);
369 + rc = osOpenDirectory(zPath, &fd);
373 @@ -29895,7 +29889,6 @@ static int proxyCreateUnixFile(
374 int islockfile /* if non zero missing dirs will be created */
380 int openFlags = O_RDWR | O_CREAT;
381 @@ -29960,7 +29953,7 @@ static int proxyCreateUnixFile(
382 pUnused->flags = openFlags;
383 pNew->pUnused = pUnused;
385 - rc = fillInUnixFile(&dummyVfs, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0, 0);
386 + rc = fillInUnixFile(&dummyVfs, fd, 0, (sqlite3_file*)pNew, path, 0, 0, 0);
390 @@ -30074,7 +30067,7 @@ static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
396 robust_close(pFile, fd, __LINE__);
398 fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
399 @@ -30897,7 +30890,7 @@ SQLITE_API int sqlite3_os_init(void){
401 /* Double-check that the aSyscall[] array has been constructed
402 ** correctly. See ticket [bb3a86e890c8e96ab] */
403 - assert( ArraySize(aSyscall)==16 );
404 + assert( ArraySize(aSyscall)==18 );
406 /* Register all VFSes defined in the aVfs[] array */
407 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
408 diff --git a/third_party/sqlite/src/src/os_unix.c b/third_party/sqlite/src/src/os_unix.c
409 index e5b2540..804c588 100644
410 --- a/third_party/sqlite/src/src/os_unix.c
411 +++ b/third_party/sqlite/src/src/os_unix.c
412 @@ -204,7 +204,6 @@ struct unixFile {
413 sqlite3_io_methods const *pMethod; /* Always the first entry */
414 unixInodeInfo *pInode; /* Info about locks on this inode */
415 int h; /* The file descriptor */
416 - int dirfd; /* File descriptor for the directory */
417 unsigned char eFileLock; /* The type of lock held on this fd */
418 unsigned char ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
419 int lastErrno; /* The unix errno from last I/O error */
420 @@ -248,6 +247,7 @@ struct unixFile {
422 #define UNIXFILE_EXCL 0x01 /* Connections from one process only */
423 #define UNIXFILE_RDONLY 0x02 /* Connection is read only */
424 +#define UNIXFILE_DIRSYNC 0x04 /* Directory sync needed */
427 ** Include code that is common to all os_*.c files
428 @@ -281,6 +281,9 @@ struct unixFile {
432 +/* Forward reference */
433 +static int openDirectory(const char*, int*);
436 ** Many system calls are accessed through pointer-to-functions so that
437 ** they may be overridden at runtime to facilitate fault injection during
438 @@ -377,6 +380,12 @@ static struct unix_syscall {
440 #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
442 + { "unlink", (sqlite3_syscall_ptr)unlink, 0 },
443 +#define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent)
445 + { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 },
446 +#define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
448 }; /* End of the overrideable system calls */
451 @@ -1731,10 +1740,6 @@ static int unixUnlock(sqlite3_file *id, int eFileLock){
453 static int closeUnixFile(sqlite3_file *id){
454 unixFile *pFile = (unixFile*)id;
455 - if( pFile->dirfd>=0 ){
456 - robust_close(pFile, pFile->dirfd, __LINE__);
460 robust_close(pFile, pFile->h, __LINE__);
462 @@ -1742,7 +1747,7 @@ static int closeUnixFile(sqlite3_file *id){
465 if( pFile->isDelete ){
466 - unlink(pFile->pId->zCanonicalName);
467 + osUnlink(pFile->pId->zCanonicalName);
469 vxworksReleaseFileId(pFile->pId);
471 @@ -1989,7 +1994,7 @@ static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
473 /* To fully unlock the database, delete the lock file */
474 assert( eFileLock==NO_LOCK );
475 - if( unlink(zLockFile) ){
476 + if( osUnlink(zLockFile) ){
479 if( ENOENT != tErrno ){
480 @@ -3226,6 +3231,50 @@ static int full_fsync(int fd, int fullSync, int dataOnly){
484 +** Open a file descriptor to the directory containing file zFilename.
485 +** If successful, *pFd is set to the opened file descriptor and
486 +** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
487 +** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
490 +** The directory file descriptor is used for only one thing - to
491 +** fsync() a directory to make sure file creation and deletion events
492 +** are flushed to disk. Such fsyncs are not needed on newer
493 +** journaling filesystems, but are required on older filesystems.
495 +** This routine can be overridden using the xSetSysCall interface.
496 +** The ability to override this routine was added in support of the
497 +** chromium sandbox. Opening a directory is a security risk (we are
498 +** told) so making it overrideable allows the chromium sandbox to
499 +** replace this routine with a harmless no-op. To make this routine
500 +** a no-op, replace it with a stub that returns SQLITE_OK but leaves
501 +** *pFd set to a negative number.
503 +** If SQLITE_OK is returned, the caller is responsible for closing
504 +** the file descriptor *pFd using close().
506 +static int openDirectory(const char *zFilename, int *pFd){
509 + char zDirname[MAX_PATHNAME+1];
511 + sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
512 + for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
514 + zDirname[ii] = '\0';
515 + fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
518 + osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
520 + OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
524 + return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
528 ** Make sure all writes to a particular file are committed to disk.
530 ** If dataOnly==0 then both the file itself and its metadata (file
531 @@ -3265,28 +3314,23 @@ static int unixSync(sqlite3_file *id, int flags){
532 pFile->lastErrno = errno;
533 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
535 - if( pFile->dirfd>=0 ){
536 - OSTRACE(("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
538 + /* Also fsync the directory containing the file if the DIRSYNC flag
539 + ** is set. This is a one-time occurrance. Many systems (examples: AIX)
540 + ** are unable to fsync a directory, so ignore errors on the fsync.
542 + if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
544 + OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
545 HAVE_FULLFSYNC, isFullsync));
546 -#ifndef SQLITE_DISABLE_DIRSYNC
547 - /* The directory sync is only attempted if full_fsync is
548 - ** turned off or unavailable. If a full_fsync occurred above,
549 - ** then the directory sync is superfluous.
551 - if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
553 - ** We have received multiple reports of fsync() returning
554 - ** errors when applied to directories on certain file systems.
555 - ** A failed directory sync is not a big deal. So it seems
556 - ** better to ignore the error. Ticket #1657
558 - /* pFile->lastErrno = errno; */
559 - /* return SQLITE_IOERR; */
560 + rc = osOpenDirectory(pFile->zPath, &dirfd);
561 + if( rc==SQLITE_OK && dirfd>=0 ){
562 + full_fsync(dirfd, 0, 0);
563 + robust_close(pFile, dirfd, __LINE__);
564 + }else if( rc==SQLITE_CANTOPEN ){
568 - /* Only need to sync once, so close the directory when we are done */
569 - robust_close(pFile, pFile->dirfd, __LINE__);
571 + pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
575 @@ -4110,7 +4154,7 @@ static int unixShmUnmap(
576 assert( pShmNode->nRef>0 );
578 if( pShmNode->nRef==0 ){
579 - if( deleteFlag && pShmNode->h>=0 ) unlink(pShmNode->zFilename);
580 + if( deleteFlag && pShmNode->h>=0 ) osUnlink(pShmNode->zFilename);
584 @@ -4430,7 +4474,7 @@ void initUnixFile(sqlite3_file* file) {
586 sqlite3_vfs *pVfs, /* Pointer to vfs object */
587 int h, /* Open file descriptor of file being opened */
588 - int dirfd, /* Directory file descriptor */
589 + int syncDir, /* True to sync directory on first sync */
590 sqlite3_file *pId, /* Write to the unixFile structure here */
591 const char *zFilename, /* Name of the file being opened */
592 int noLock, /* Omit locking if true */
593 @@ -4461,7 +4505,6 @@ int fillInUnixFile(
595 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
597 - pNew->dirfd = dirfd;
598 pNew->zPath = zFilename;
599 if( memcmp(pVfs->zName,"unix-excl",10)==0 ){
600 pNew->ctrlFlags = UNIXFILE_EXCL;
601 @@ -4471,6 +4514,9 @@ int fillInUnixFile(
603 pNew->ctrlFlags |= UNIXFILE_RDONLY;
606 + pNew->ctrlFlags |= UNIXFILE_DIRSYNC;
610 pNew->pId = vxworksFindFileId(zFilename);
611 @@ -4597,13 +4643,12 @@ int fillInUnixFile(
613 if( h>=0 ) robust_close(pNew, h, __LINE__);
616 + osUnlink(zFilename);
619 pNew->isDelete = isDelete;
622 - if( dirfd>=0 ) robust_close(pNew, dirfd, __LINE__);
623 if( h>=0 ) robust_close(pNew, h, __LINE__);
625 pNew->pMethod = pLockingStyle;
626 @@ -4613,37 +4658,6 @@ int fillInUnixFile(
630 -** Open a file descriptor to the directory containing file zFilename.
631 -** If successful, *pFd is set to the opened file descriptor and
632 -** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
633 -** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
636 -** If SQLITE_OK is returned, the caller is responsible for closing
637 -** the file descriptor *pFd using close().
639 -static int openDirectory(const char *zFilename, int *pFd){
642 - char zDirname[MAX_PATHNAME+1];
644 - sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
645 - for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
647 - zDirname[ii] = '\0';
648 - fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
651 - osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
653 - OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
657 - return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
661 ** Return the name of a directory in which to put temporary files.
662 ** If no suitable temporary file directory can be found, return NULL.
664 @@ -4938,7 +4952,6 @@ static int unixOpen(
666 unixFile *p = (unixFile *)pFile;
667 int fd = -1; /* File descriptor returned by open() */
668 - int dirfd = -1; /* Directory file descriptor */
669 int openFlags = 0; /* Flags to pass to open() */
670 int eType = flags&0xFFFFFF00; /* Type of file to open */
671 int noLock; /* True to omit locking primitives */
672 @@ -4957,7 +4970,7 @@ static int unixOpen(
673 ** a file-descriptor on the directory too. The first time unixSync()
674 ** is called the directory file descriptor will be fsync()ed and close()d.
676 - int isOpenDirectory = (isCreate && (
677 + int syncDir = (isCreate && (
678 eType==SQLITE_OPEN_MASTER_JOURNAL
679 || eType==SQLITE_OPEN_MAIN_JOURNAL
680 || eType==SQLITE_OPEN_WAL
681 @@ -5004,7 +5017,7 @@ static int unixOpen(
684 /* If zName is NULL, the upper layer is requesting a temp file. */
685 - assert(isDelete && !isOpenDirectory);
686 + assert(isDelete && !syncDir);
687 rc = unixGetTempname(MAX_PATHNAME+1, zTmpname);
690 @@ -5057,7 +5070,7 @@ static int unixOpen(
698 #if SQLITE_ENABLE_LOCKING_STYLE
699 @@ -5066,19 +5079,6 @@ static int unixOpen(
703 - if( isOpenDirectory ){
704 - rc = openDirectory(zPath, &dirfd);
705 - if( rc!=SQLITE_OK ){
706 - /* It is safe to close fd at this point, because it is guaranteed not
707 - ** to be open on a database file. If it were open on a database file,
708 - ** it would not be safe to close as this would release any locks held
709 - ** on the file by this process. */
710 - assert( eType!=SQLITE_OPEN_MAIN_DB );
711 - robust_close(p, fd, __LINE__);
712 - goto open_finished;
717 osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
719 @@ -5090,7 +5090,6 @@ static int unixOpen(
720 struct statfs fsInfo;
721 if( fstatfs(fd, &fsInfo) == -1 ){
722 ((unixFile*)pFile)->lastErrno = errno;
723 - if( dirfd>=0 ) robust_close(p, dirfd, __LINE__);
724 robust_close(p, fd, __LINE__);
725 return SQLITE_IOERR_ACCESS;
727 @@ -5122,9 +5121,6 @@ static int unixOpen(
728 ** not while other file descriptors opened by the same process on
729 ** the same file are working. */
730 p->lastErrno = errno;
732 - robust_close(p, dirfd, __LINE__);
734 robust_close(p, fd, __LINE__);
735 rc = SQLITE_IOERR_ACCESS;
737 @@ -5132,7 +5128,7 @@ static int unixOpen(
738 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
741 - rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock,
742 + rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock,
743 isDelete, isReadonly);
745 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
746 @@ -5150,7 +5146,7 @@ static int unixOpen(
750 - rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock,
751 + rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock,
752 isDelete, isReadonly);
755 @@ -5172,13 +5168,13 @@ static int unixDelete(
757 UNUSED_PARAMETER(NotUsed);
758 SimulateIOError(return SQLITE_IOERR_DELETE);
759 - if( unlink(zPath)==(-1) && errno!=ENOENT ){
760 + if( osUnlink(zPath)==(-1) && errno!=ENOENT ){
761 return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
763 #ifndef SQLITE_DISABLE_DIRSYNC
766 - rc = openDirectory(zPath, &fd);
767 + rc = osOpenDirectory(zPath, &fd);
771 @@ -5189,6 +5185,8 @@ static int unixDelete(
772 rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
774 robust_close(0, fd, __LINE__);
775 + }else if( rc==SQLITE_CANTOPEN ){
780 @@ -5750,7 +5748,6 @@ static int proxyCreateUnixFile(
781 int islockfile /* if non zero missing dirs will be created */
787 int openFlags = O_RDWR | O_CREAT;
788 @@ -5815,7 +5812,7 @@ static int proxyCreateUnixFile(
789 pUnused->flags = openFlags;
790 pNew->pUnused = pUnused;
792 - rc = fillInUnixFile(&dummyVfs, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0, 0);
793 + rc = fillInUnixFile(&dummyVfs, fd, 0, (sqlite3_file*)pNew, path, 0, 0, 0);
797 @@ -5929,7 +5926,7 @@ static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
803 robust_close(pFile, fd, __LINE__);
805 fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
806 @@ -6752,7 +6749,7 @@ int sqlite3_os_init(void){
808 /* Double-check that the aSyscall[] array has been constructed
809 ** correctly. See ticket [bb3a86e890c8e96ab] */
810 - assert( ArraySize(aSyscall)==16 );
811 + assert( ArraySize(aSyscall)==18 );
813 /* Register all VFSes defined in the aVfs[] array */
814 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
815 diff --git a/third_party/sqlite/src/test/syscall.test b/third_party/sqlite/src/test/syscall.test
816 index 4442612..201bd63 100644
817 --- a/third_party/sqlite/src/test/syscall.test
818 +++ b/third_party/sqlite/src/test/syscall.test
819 @@ -59,7 +59,7 @@ do_test 2.1.2 { test_syscall exists nosuchcall } 0
821 open close access getcwd stat fstat ftruncate
822 fcntl read pread write pwrite fchmod fallocate
824 + pread64 pwrite64 unlink openDirectory
826 if {[test_syscall exists $s]} {lappend syscall_list $s}
828 diff --git a/third_party/sqlite/system-sqlite.patch b/third_party/sqlite/system-sqlite.patch
829 index f61f019..31d6b00 100644
830 --- a/third_party/sqlite/system-sqlite.patch
831 +++ b/third_party/sqlite/system-sqlite.patch
833 -This is a backport of http://sqlite.org/src/ci/9109128cb5,
834 -which is needed for experiments with using unpatched sqlite.
835 -If you hit a merge conflict on this file it is most likely
836 -that you've upgraded to version of sqlite that includes this patch.
837 -Index: src/os_unix.c
838 -===================================================================
841 -@@ -4787,11 +4787,11 @@
842 - ** ignored and -1 is returned. The caller will try to open a new file
843 - ** descriptor on the same path, fail, and return an error to SQLite.
845 - ** Even if a subsequent open() call does succeed, the consequences of
846 - ** not searching for a resusable file descriptor are not dire. */
847 -- if( 0==stat(zPath, &sStat) ){
848 -+ if( 0==osStat(zPath, &sStat) ){
849 - unixInodeInfo *pInode;
852 - pInode = inodeList;
853 - while( pInode && (pInode->fileId.dev!=sStat.st_dev
854 -@@ -4863,11 +4863,11 @@
855 - while( nDb>0 && zPath[nDb]!='-' ) nDb--;
856 - if( nDb==0 ) return SQLITE_OK;
857 - memcpy(zDb, zPath, nDb);
860 -- if( 0==stat(zDb, &sStat) ){
861 -+ if( 0==osStat(zDb, &sStat) ){
862 - *pMode = sStat.st_mode & 0777;
864 - rc = SQLITE_IOERR_FSTAT;
866 - }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
867 -@@ -5208,11 +5208,11 @@
868 - assert(!"Invalid flags argument");
870 - *pResOut = (osAccess(zPath, amode)==0);
871 - if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){
873 -- if( 0==stat(zPath, &buf) && buf.st_size==0 ){
874 -+ if( 0==osStat(zPath, &buf) && buf.st_size==0 ){