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

Side by Side Diff: third_party/sqlite/patches/0007-backport-SQLite-os-intercept-changes.patch

Issue 901033002: Import SQLite 3.8.7.4. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Chromium changes to support SQLite 3.8.7.4. 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
(Empty)
1 From 82b8b2c69979f0e1d93577f40597c826d8f8c2ee Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Pawe=C5=82=20Hajdan=20Jr?= <phajdan.jr@chromium.org>
3 Date: Fri, 5 Aug 2011 16:17:40 +0000
4 Subject: [PATCH 07/23] [backport] SQLite os-intercept changes.
5
6 This is a backport of the following SQLite changes to experiment with
7 implementing WebDatabase with an unpatched SQLite:
8
9 1. http://sqlite.org/src/ci/9109128cb5
10 2. http://sqlite.org/src/ci/713b1b7dc1
11 3. http://sqlite.org/src/ci/8d1b5c3ac0
12 4. http://sqlite.org/src/ci/6b236069e1
13 5. http://sqlite.org/src/ci/880b51150a
14
15 BUG=22208
16
17 Original review URLs:
18 http://codereview.chromium.org/7575027
19 http://codereview.chromium.org/7629003
20 [Possibly more]
21 ---
22 third_party/sqlite/src/src/os_unix.c | 187 +++++++++++++++----------------
23 third_party/sqlite/src/test/syscall.test | 2 +-
24 2 files changed, 93 insertions(+), 96 deletions(-)
25
26 diff --git a/third_party/sqlite/src/src/os_unix.c b/third_party/sqlite/src/src/o s_unix.c
27 index 766b52a..77ffd8a 100644
28 --- a/third_party/sqlite/src/src/os_unix.c
29 +++ b/third_party/sqlite/src/src/os_unix.c
30 @@ -204,7 +204,6 @@ struct unixFile {
31 sqlite3_io_methods const *pMethod; /* Always the first entry */
32 unixInodeInfo *pInode; /* Info about locks on this inode */
33 int h; /* The file descriptor */
34 - int dirfd; /* File descriptor for the directory */
35 unsigned char eFileLock; /* The type of lock held on this fd */
36 unsigned char ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
37 int lastErrno; /* The unix errno from last I/O error */
38 @@ -248,6 +247,7 @@ struct unixFile {
39 */
40 #define UNIXFILE_EXCL 0x01 /* Connections from one process only */
41 #define UNIXFILE_RDONLY 0x02 /* Connection is read only */
42 +#define UNIXFILE_DIRSYNC 0x04 /* Directory sync needed */
43
44 /*
45 ** Include code that is common to all os_*.c files
46 @@ -281,6 +281,9 @@ struct unixFile {
47 #define threadid 0
48 #endif
49
50 +/* Forward reference */
51 +static int openDirectory(const char*, int*);
52 +
53 /*
54 ** Many system calls are accessed through pointer-to-functions so that
55 ** they may be overridden at runtime to facilitate fault injection during
56 @@ -377,6 +380,12 @@ static struct unix_syscall {
57 #endif
58 #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
59
60 + { "unlink", (sqlite3_syscall_ptr)unlink, 0 },
61 +#define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent)
62 +
63 + { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 },
64 +#define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
65 +
66 }; /* End of the overrideable system calls */
67
68 /*
69 @@ -1731,10 +1740,6 @@ static int unixUnlock(sqlite3_file *id, int eFileLock){
70 */
71 static int closeUnixFile(sqlite3_file *id){
72 unixFile *pFile = (unixFile*)id;
73 - if( pFile->dirfd>=0 ){
74 - robust_close(pFile, pFile->dirfd, __LINE__);
75 - pFile->dirfd=-1;
76 - }
77 if( pFile->h>=0 ){
78 robust_close(pFile, pFile->h, __LINE__);
79 pFile->h = -1;
80 @@ -1742,7 +1747,7 @@ static int closeUnixFile(sqlite3_file *id){
81 #if OS_VXWORKS
82 if( pFile->pId ){
83 if( pFile->isDelete ){
84 - unlink(pFile->pId->zCanonicalName);
85 + osUnlink(pFile->pId->zCanonicalName);
86 }
87 vxworksReleaseFileId(pFile->pId);
88 pFile->pId = 0;
89 @@ -1989,7 +1994,7 @@ static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
90
91 /* To fully unlock the database, delete the lock file */
92 assert( eFileLock==NO_LOCK );
93 - if( unlink(zLockFile) ){
94 + if( osUnlink(zLockFile) ){
95 int rc = 0;
96 int tErrno = errno;
97 if( ENOENT != tErrno ){
98 @@ -3226,6 +3231,50 @@ static int full_fsync(int fd, int fullSync, int dataOnly) {
99 }
100
101 /*
102 +** Open a file descriptor to the directory containing file zFilename.
103 +** If successful, *pFd is set to the opened file descriptor and
104 +** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
105 +** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
106 +** value.
107 +**
108 +** The directory file descriptor is used for only one thing - to
109 +** fsync() a directory to make sure file creation and deletion events
110 +** are flushed to disk. Such fsyncs are not needed on newer
111 +** journaling filesystems, but are required on older filesystems.
112 +**
113 +** This routine can be overridden using the xSetSysCall interface.
114 +** The ability to override this routine was added in support of the
115 +** chromium sandbox. Opening a directory is a security risk (we are
116 +** told) so making it overrideable allows the chromium sandbox to
117 +** replace this routine with a harmless no-op. To make this routine
118 +** a no-op, replace it with a stub that returns SQLITE_OK but leaves
119 +** *pFd set to a negative number.
120 +**
121 +** If SQLITE_OK is returned, the caller is responsible for closing
122 +** the file descriptor *pFd using close().
123 +*/
124 +static int openDirectory(const char *zFilename, int *pFd){
125 + int ii;
126 + int fd = -1;
127 + char zDirname[MAX_PATHNAME+1];
128 +
129 + sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
130 + for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
131 + if( ii>0 ){
132 + zDirname[ii] = '\0';
133 + fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
134 + if( fd>=0 ){
135 +#ifdef FD_CLOEXEC
136 + osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
137 +#endif
138 + OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
139 + }
140 + }
141 + *pFd = fd;
142 + return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname)) ;
143 +}
144 +
145 +/*
146 ** Make sure all writes to a particular file are committed to disk.
147 **
148 ** If dataOnly==0 then both the file itself and its metadata (file
149 @@ -3265,28 +3314,23 @@ static int unixSync(sqlite3_file *id, int flags){
150 pFile->lastErrno = errno;
151 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
152 }
153 - if( pFile->dirfd>=0 ){
154 - OSTRACE(("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
155 +
156 + /* Also fsync the directory containing the file if the DIRSYNC flag
157 + ** is set. This is a one-time occurrance. Many systems (examples: AIX)
158 + ** are unable to fsync a directory, so ignore errors on the fsync.
159 + */
160 + if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
161 + int dirfd;
162 + OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
163 HAVE_FULLFSYNC, isFullsync));
164 -#ifndef SQLITE_DISABLE_DIRSYNC
165 - /* The directory sync is only attempted if full_fsync is
166 - ** turned off or unavailable. If a full_fsync occurred above,
167 - ** then the directory sync is superfluous.
168 - */
169 - if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){
170 - /*
171 - ** We have received multiple reports of fsync() returning
172 - ** errors when applied to directories on certain file systems.
173 - ** A failed directory sync is not a big deal. So it seems
174 - ** better to ignore the error. Ticket #1657
175 - */
176 - /* pFile->lastErrno = errno; */
177 - /* return SQLITE_IOERR; */
178 + rc = osOpenDirectory(pFile->zPath, &dirfd);
179 + if( rc==SQLITE_OK && dirfd>=0 ){
180 + full_fsync(dirfd, 0, 0);
181 + robust_close(pFile, dirfd, __LINE__);
182 + }else if( rc==SQLITE_CANTOPEN ){
183 + rc = SQLITE_OK;
184 }
185 -#endif
186 - /* Only need to sync once, so close the directory when we are done */
187 - robust_close(pFile, pFile->dirfd, __LINE__);
188 - pFile->dirfd = -1;
189 + pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
190 }
191 return rc;
192 }
193 @@ -4110,7 +4154,7 @@ static int unixShmUnmap(
194 assert( pShmNode->nRef>0 );
195 pShmNode->nRef--;
196 if( pShmNode->nRef==0 ){
197 - if( deleteFlag && pShmNode->h>=0 ) unlink(pShmNode->zFilename);
198 + if( deleteFlag && pShmNode->h>=0 ) osUnlink(pShmNode->zFilename);
199 unixShmPurge(pDbFd);
200 }
201 unixLeaveMutex();
202 @@ -4430,7 +4474,7 @@ void initUnixFile(sqlite3_file* file) {
203 int fillInUnixFile(
204 sqlite3_vfs *pVfs, /* Pointer to vfs object */
205 int h, /* Open file descriptor of file being opened */
206 - int dirfd, /* Directory file descriptor */
207 + int syncDir, /* True to sync directory on first sync */
208 sqlite3_file *pId, /* Write to the unixFile structure here */
209 const char *zFilename, /* Name of the file being opened */
210 int noLock, /* Omit locking if true */
211 @@ -4461,7 +4505,6 @@ int fillInUnixFile(
212
213 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
214 pNew->h = h;
215 - pNew->dirfd = dirfd;
216 pNew->zPath = zFilename;
217 if( strcmp(pVfs->zName,"unix-excl")==0 ){
218 pNew->ctrlFlags = UNIXFILE_EXCL;
219 @@ -4471,6 +4514,9 @@ int fillInUnixFile(
220 if( isReadOnly ){
221 pNew->ctrlFlags |= UNIXFILE_RDONLY;
222 }
223 + if( syncDir ){
224 + pNew->ctrlFlags |= UNIXFILE_DIRSYNC;
225 + }
226
227 #if OS_VXWORKS
228 pNew->pId = vxworksFindFileId(zFilename);
229 @@ -4597,13 +4643,12 @@ int fillInUnixFile(
230 if( rc!=SQLITE_OK ){
231 if( h>=0 ) robust_close(pNew, h, __LINE__);
232 h = -1;
233 - unlink(zFilename);
234 + osUnlink(zFilename);
235 isDelete = 0;
236 }
237 pNew->isDelete = isDelete;
238 #endif
239 if( rc!=SQLITE_OK ){
240 - if( dirfd>=0 ) robust_close(pNew, dirfd, __LINE__);
241 if( h>=0 ) robust_close(pNew, h, __LINE__);
242 }else{
243 pNew->pMethod = pLockingStyle;
244 @@ -4613,37 +4658,6 @@ int fillInUnixFile(
245 }
246
247 /*
248 -** Open a file descriptor to the directory containing file zFilename.
249 -** If successful, *pFd is set to the opened file descriptor and
250 -** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
251 -** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
252 -** value.
253 -**
254 -** If SQLITE_OK is returned, the caller is responsible for closing
255 -** the file descriptor *pFd using close().
256 -*/
257 -static int openDirectory(const char *zFilename, int *pFd){
258 - int ii;
259 - int fd = -1;
260 - char zDirname[MAX_PATHNAME+1];
261 -
262 - sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
263 - for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
264 - if( ii>0 ){
265 - zDirname[ii] = '\0';
266 - fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
267 - if( fd>=0 ){
268 -#ifdef FD_CLOEXEC
269 - osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
270 -#endif
271 - OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
272 - }
273 - }
274 - *pFd = fd;
275 - return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname)) ;
276 -}
277 -
278 -/*
279 ** Return the name of a directory in which to put temporary files.
280 ** If no suitable temporary file directory can be found, return NULL.
281 */
282 @@ -4757,7 +4771,7 @@ static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
283 **
284 ** Even if a subsequent open() call does succeed, the consequences of
285 ** not searching for a resusable file descriptor are not dire. */
286 - if( 0==stat(zPath, &sStat) ){
287 + if( 0==osStat(zPath, &sStat) ){
288 unixInodeInfo *pInode;
289
290 unixEnterMutex();
291 @@ -4827,7 +4841,7 @@ static int findCreateFileMode(
292 memcpy(zDb, zPath, nDb);
293 zDb[nDb] = '\0';
294
295 - if( 0==stat(zDb, &sStat) ){
296 + if( 0==osStat(zDb, &sStat) ){
297 *pMode = sStat.st_mode & 0777;
298 }else{
299 rc = SQLITE_IOERR_FSTAT;
300 @@ -4938,7 +4952,6 @@ static int unixOpen(
301 ){
302 unixFile *p = (unixFile *)pFile;
303 int fd = -1; /* File descriptor returned by open() */
304 - int dirfd = -1; /* Directory file descriptor */
305 int openFlags = 0; /* Flags to pass to open() */
306 int eType = flags&0xFFFFFF00; /* Type of file to open */
307 int noLock; /* True to omit locking primitives */
308 @@ -4957,7 +4970,7 @@ static int unixOpen(
309 ** a file-descriptor on the directory too. The first time unixSync()
310 ** is called the directory file descriptor will be fsync()ed and close()d.
311 */
312 - int isOpenDirectory = (isCreate && (
313 + int syncDir = (isCreate && (
314 eType==SQLITE_OPEN_MASTER_JOURNAL
315 || eType==SQLITE_OPEN_MAIN_JOURNAL
316 || eType==SQLITE_OPEN_WAL
317 @@ -5004,7 +5017,7 @@ static int unixOpen(
318 }
319 }else if( !zName ){
320 /* If zName is NULL, the upper layer is requesting a temp file. */
321 - assert(isDelete && !isOpenDirectory);
322 + assert(isDelete && !syncDir);
323 rc = unixGetTempname(MAX_PATHNAME+1, zTmpname);
324 if( rc!=SQLITE_OK ){
325 return rc;
326 @@ -5057,7 +5070,7 @@ static int unixOpen(
327 #if OS_VXWORKS
328 zPath = zName;
329 #else
330 - unlink(zName);
331 + osUnlink(zName);
332 #endif
333 }
334 #if SQLITE_ENABLE_LOCKING_STYLE
335 @@ -5066,19 +5079,6 @@ static int unixOpen(
336 }
337 #endif
338
339 - if( isOpenDirectory ){
340 - rc = openDirectory(zPath, &dirfd);
341 - if( rc!=SQLITE_OK ){
342 - /* It is safe to close fd at this point, because it is guaranteed not
343 - ** to be open on a database file. If it were open on a database file,
344 - ** it would not be safe to close as this would release any locks held
345 - ** on the file by this process. */
346 - assert( eType!=SQLITE_OPEN_MAIN_DB );
347 - robust_close(p, fd, __LINE__);
348 - goto open_finished;
349 - }
350 - }
351 -
352 #ifdef FD_CLOEXEC
353 osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
354 #endif
355 @@ -5090,7 +5090,6 @@ static int unixOpen(
356 struct statfs fsInfo;
357 if( fstatfs(fd, &fsInfo) == -1 ){
358 ((unixFile*)pFile)->lastErrno = errno;
359 - if( dirfd>=0 ) robust_close(p, dirfd, __LINE__);
360 robust_close(p, fd, __LINE__);
361 return SQLITE_IOERR_ACCESS;
362 }
363 @@ -5122,9 +5121,6 @@ static int unixOpen(
364 ** not while other file descriptors opened by the same process on
365 ** the same file are working. */
366 p->lastErrno = errno;
367 - if( dirfd>=0 ){
368 - robust_close(p, dirfd, __LINE__);
369 - }
370 robust_close(p, fd, __LINE__);
371 rc = SQLITE_IOERR_ACCESS;
372 goto open_finished;
373 @@ -5132,7 +5128,7 @@ static int unixOpen(
374 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
375 }
376 if( useProxy ){
377 - rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock,
378 + rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock,
379 isDelete, isReadonly);
380 if( rc==SQLITE_OK ){
381 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
382 @@ -5150,7 +5146,7 @@ static int unixOpen(
383 }
384 #endif
385
386 - rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock,
387 + rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock,
388 isDelete, isReadonly);
389 open_finished:
390 if( rc!=SQLITE_OK ){
391 @@ -5172,13 +5168,13 @@ static int unixDelete(
392 int rc = SQLITE_OK;
393 UNUSED_PARAMETER(NotUsed);
394 SimulateIOError(return SQLITE_IOERR_DELETE);
395 - if( unlink(zPath)==(-1) && errno!=ENOENT ){
396 + if( osUnlink(zPath)==(-1) && errno!=ENOENT ){
397 return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
398 }
399 #ifndef SQLITE_DISABLE_DIRSYNC
400 if( dirSync ){
401 int fd;
402 - rc = openDirectory(zPath, &fd);
403 + rc = osOpenDirectory(zPath, &fd);
404 if( rc==SQLITE_OK ){
405 #if OS_VXWORKS
406 if( fsync(fd)==-1 )
407 @@ -5189,6 +5185,8 @@ static int unixDelete(
408 rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
409 }
410 robust_close(0, fd, __LINE__);
411 + }else if( rc==SQLITE_CANTOPEN ){
412 + rc = SQLITE_OK;
413 }
414 }
415 #endif
416 @@ -5231,7 +5229,7 @@ static int unixAccess(
417 *pResOut = (osAccess(zPath, amode)==0);
418 if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){
419 struct stat buf;
420 - if( 0==stat(zPath, &buf) && buf.st_size==0 ){
421 + if( 0==osStat(zPath, &buf) && buf.st_size==0 ){
422 *pResOut = 0;
423 }
424 }
425 @@ -5750,7 +5748,6 @@ static int proxyCreateUnixFile(
426 int islockfile /* if non zero missing dirs will be created */
427 ) {
428 int fd = -1;
429 - int dirfd = -1;
430 unixFile *pNew;
431 int rc = SQLITE_OK;
432 int openFlags = O_RDWR | O_CREAT;
433 @@ -5815,7 +5812,7 @@ static int proxyCreateUnixFile(
434 pUnused->flags = openFlags;
435 pNew->pUnused = pUnused;
436
437 - rc = fillInUnixFile(&dummyVfs, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0, 0) ;
438 + rc = fillInUnixFile(&dummyVfs, fd, 0, (sqlite3_file*)pNew, path, 0, 0, 0);
439 if( rc==SQLITE_OK ){
440 *ppFile = pNew;
441 return SQLITE_OK;
442 @@ -5929,7 +5926,7 @@ static int proxyBreakConchLock(unixFile *pFile, uuid_t myH ostID){
443 end_breaklock:
444 if( rc ){
445 if( fd>=0 ){
446 - unlink(tPath);
447 + osUnlink(tPath);
448 robust_close(pFile, fd, __LINE__);
449 }
450 fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
451 @@ -6752,7 +6749,7 @@ int sqlite3_os_init(void){
452
453 /* Double-check that the aSyscall[] array has been constructed
454 ** correctly. See ticket [bb3a86e890c8e96ab] */
455 - assert( ArraySize(aSyscall)==16 );
456 + assert( ArraySize(aSyscall)==18 );
457
458 /* Register all VFSes defined in the aVfs[] array */
459 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
460 diff --git a/third_party/sqlite/src/test/syscall.test b/third_party/sqlite/src/t est/syscall.test
461 index 4442612..201bd63 100644
462 --- a/third_party/sqlite/src/test/syscall.test
463 +++ b/third_party/sqlite/src/test/syscall.test
464 @@ -59,7 +59,7 @@ do_test 2.1.2 { test_syscall exists nosuchcall } 0
465 foreach s {
466 open close access getcwd stat fstat ftruncate
467 fcntl read pread write pwrite fchmod fallocate
468 - pread64 pwrite64
469 + pread64 pwrite64 unlink openDirectory
470 } {
471 if {[test_syscall exists $s]} {lappend syscall_list $s}
472 }
473 --
474 2.2.1
475
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698