Various fixes and test case updates so that veryquick.test passes again.

FossilOrigin-Name: f352ef57e39a9443b4bdf47fc2c9b01006fe3c10
diff --git a/manifest b/manifest
index 19a6faf..fb3e8f6 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Increase\sthe\sversion\snumber\sto\s3.8.4
-D 2014-02-11T16:24:34.448
+C Various\sfixes\sand\stest\scase\supdates\sso\sthat\sveryquick.test\spasses\sagain.
+D 2014-02-12T15:05:05.674
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in 2ef13430cd359f7b361bb863504e227b25cc7f81
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -205,7 +205,7 @@
 F src/os.c 1b147e4cf7cc39e618115c14a086aed44bc91ace
 F src/os.h 4a46270a64e9193af4a0aaa3bc2c66dc07c29b3f
 F src/os_common.h 92815ed65f805560b66166e3583470ff94478f04
-F src/os_unix.c 18f7f95dc6bcb9cf4d4a238d8e2de96611bc2ae5
+F src/os_unix.c c53e2683d8cc23d08f28dbecf0e3c2bcf1d1acca
 F src/os_win.c d4284f003445054a26689f1264b1b9bf7261bd1b
 F src/pager.c 0ffa313a30ed6d061d9c6601b7b175cc50a1cab7
 F src/pager.h ffd5607f7b3e4590b415b007a4382f693334d428
@@ -230,7 +230,7 @@
 F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e
 F src/tclsqlite.c 649d373f2a3cfbefe8e935a8dec83686616f9a85
 F src/test1.c 2401eee14a4309a7cfe2aeb2f30ad517a1d9c299
-F src/test2.c 7355101c085304b90024f2261e056cdff13c6c35
+F src/test2.c 0f8bd858c127fd5236dd965d534622422fbe0b2f
 F src/test3.c 1c0e5d6f080b8e33c1ce8b3078e7013fdbcd560c
 F src/test4.c 9b32d22f5f150abe23c1830e2057c4037c45b3df
 F src/test5.c 41e6e732f14a54c7b47f753e364700760f6521b0
@@ -336,9 +336,9 @@
 F test/autovacuum_ioerr2.test 8a367b224183ad801e0e24dcb7d1501f45f244b4
 F test/avtrans.test 0252654f4295ddda3b2cce0e894812259e655a85
 F test/backcompat.test 5f8ad58b3eaebc78cd2c66c65476a42e6f32b2ad
-F test/backup.test c9cdd23a495864b9edf75a9fa66f5cb7e10fcf62
+F test/backup.test 3b072335eadafa32ab940fceed5852fb48c30655
 F test/backup2.test 34986ef926ea522911a51dfdb2f8e99b7b75ebcf
-F test/backup4.test 2a2e4a64388090b152de753fd9e123f28f6a3bd4
+F test/backup4.test 139a465f76d43259123d6e09281d080fccc59f8a
 F test/backup_ioerr.test 4c3c7147cee85b024ecf6e150e090c32fdbb5135
 F test/backup_malloc.test 7162d604ec2b4683c4b3799a48657fb8b5e2d450
 F test/badutf.test d5360fc31f643d37a973ab0d8b4fb85799c3169f
@@ -368,7 +368,7 @@
 F test/capi3b.test efb2b9cfd127efa84433cd7a2d72ce0454ae0dc4
 F test/capi3c.test a21869e4d50d5dbb7e566e328fc0bc7c2efa6a32
 F test/capi3d.test 6d0fc0a86d73f42dd19a7d8b7761ab9bc02277d0
-F test/capi3e.test ad90088b18b0367125ff2d4b5400153fd2f99aab
+F test/capi3e.test 1ecb23b79a6f2a50ba336142e795d6e677d1d2d4
 F test/cast.test 4c275cbdc8202d6f9c54a3596701719868ac7dc3
 F test/check.test 5831ddb6f2c687782eaf2e1a07b6e17f24c4f763
 F test/close.test 340bd24cc58b16c6bc01967402755027c37eb815
@@ -447,7 +447,7 @@
 F test/e_select.test 52692ff3849541e828ad4661fe3773a9b8711763
 F test/e_select2.test aceb80ab927d46fba5ce7586ebabf23e2bb0604f
 F test/e_update.test 312cb8f5ccfe41515a6bb092f8ea562a9bd54d52
-F test/e_uri.test a2c92d80093a7efdcfbb11093651cbea87097b6b
+F test/e_uri.test bfa56d624d693a81bb29ac2a9a999e35c2c8d9bf
 F test/e_vacuum.test 5bfbdc21b65c0abf24398d0ba31dc88d93ca77a9
 F test/enc.test e54531cd6bf941ee6760be041dff19a104c7acea
 F test/enc2.test 83437a79ba1545a55fb549309175c683fb334473
@@ -457,7 +457,7 @@
 F test/errmsg.test f31592a594b44ee121371d25ddd5d63497bb3401
 F test/eval.test bc269c365ba877554948441e91ad5373f9f91be3
 F test/exclusive.test c7ebbc756eacf544c108b15eed64d7d4e5f86b75
-F test/exclusive2.test 32798111aae78a5deec980eee383213f189df308
+F test/exclusive2.test a3bfa5d21268d2d1f4dd1c094ad9cd583914ca73
 F test/exec.test e949714dc127eaa5ecc7d723efec1ec27118fdd7
 F test/exists.test 8f7b27b61c2fbe5822f0a1f899c715d14e416e30
 F test/expr.test 67c9fd6f8f829e239dc8b0f4a08a73c08b09196d
@@ -602,7 +602,7 @@
 F test/incrblob4.test f26502a5697893e5acea268c910f16478c2f0fab
 F test/incrblob_err.test d2562d2771ebffd4b3af89ef64c140dd44371597
 F test/incrblobfault.test 280474078f6da9e732cd2a215d3d854969014b6e
-F test/incrvacuum.test d2a6ddf5e429720b5fe502766af747915ccf6c32
+F test/incrvacuum.test 0e0f03c3c4956de4f3b1d47fe03ea762bc9aaa83
 F test/incrvacuum2.test 379eeb8740b0ef60c372c439ad4cbea20b34bb9b
 F test/incrvacuum3.test 75256fb1377e7c39ef2de62bfc42bbff67be295a
 F test/incrvacuum_ioerr.test 6ae2f783424e47a0033304808fe27789cf93e635
@@ -625,7 +625,7 @@
 F test/intarray.test 066b7d7ac38d25bf96f87f1b017bfc687551cdd4
 F test/interrupt.test dfe9a67a94b0b2d8f70545ba1a6cca10780d71cc
 F test/intpkey.test 7506090fc08e028712a8bf47e5f54111947e3844
-F test/io.test 3a7abcef18727cc0f2399e04b0e8903eccae50f8
+F test/io.test 148556665d0f5181f510d072f434855bd40be1be
 F test/ioerr.test 2a24bd6ed5a8b062e64bfe1f6cf94fb25e92210d
 F test/ioerr2.test 9d71166f8466eda510f1af6137bdabaa82b5408d
 F test/ioerr3.test d3cec5e1a11ad6d27527d0d38573fbff14c71bdd
@@ -725,7 +725,7 @@
 F test/orderby4.test 4d39bfbaaa3ae64d026ca2ff166353d2edca4ba4
 F test/orderby5.test 0eb82d5890c3f3d0563966560cfdc984ea69e30c
 F test/oserror.test 50417780d0e0d7cd23cf12a8277bb44024765df3
-F test/pager1.test 1acbdb14c5952a72dd43129cabdbf69aaa3ed1fa
+F test/pager1.test c609ea28a3692d8983e945a253ebae25ad385977
 F test/pager2.test 67b8f40ae98112bcdba1f2b2d03ea83266418c71
 F test/pager3.test 3856d9c80839be0668efee1b74811b1b7f7fc95f
 F test/pager4.test a122e9e6925d5b23b31e3dfef8c6a44bbf19590e
@@ -737,7 +737,7 @@
 F test/pcache.test b09104b03160aca0d968d99e8cd2c5b1921a993d
 F test/pcache2.test a83efe2dec0d392f814bfc998def1d1833942025
 F test/percentile.test b98fc868d71eb5619d42a1702e9ab91718cbed54
-F test/permutations.test 40add071ba71aefe1c04f5845308cf46f7de8d04
+F test/permutations.test deb0e98ed7e2849fd896a39e3443b342b7feda2a
 F test/pragma.test e882183ecd21d064cec5c7aaea174fbd36293429
 F test/pragma2.test aea7b3d82c76034a2df2b38a13745172ddc0bc13
 F test/printf.test ec9870c4dce8686a37818e0bf1aba6e6a1863552
@@ -835,7 +835,7 @@
 F test/substr.test 18f57c4ca8a598805c4d64e304c418734d843c1a
 F test/superlock.test 1cde669f68d2dd37d6c9bd35eee1d95491ae3fc2
 F test/sync.test a34cd43e98b7fb84eabbf38f7ed8f7349b3f3d85
-F test/syscall.test a653783d985108c4912cc64d341ffbbb55ad2806
+F test/syscall.test 8e96912cf2e41b572d00c537ac71b9f5bb72477e
 F test/sysfault.test fa776e60bf46bdd3ae69f0b73e46ee3977a58ae6
 F test/table.test 580d23530187026d4502fae74a490f0408cf2cc7
 F test/tableapi.test 2674633fa95d80da917571ebdd759a14d9819126
@@ -844,7 +844,7 @@
 F test/tempdb.test 19d0f66e2e3eeffd68661a11c83ba5e6ace9128c
 F test/temptable.test d2c9b87a54147161bcd1822e30c1d1cd891e5b30
 F test/temptrigger.test 8ec228b0db5d7ebc4ee9b458fc28cb9e7873f5e1
-F test/tester.tcl 9bd04481b8b0ef1f2049ad01f28e175ee9a14f7b
+F test/tester.tcl cc5fc5ecfbbd90f4c100d3aaea65eb5d3b3de988
 F test/thread001.test 9f22fd3525a307ff42a326b6bc7b0465be1745a5
 F test/thread002.test e630504f8a06c00bf8bbe68528774dd96aeb2e58
 F test/thread003.test ee4c9efc3b86a6a2767516a37bd64251272560a7
@@ -988,7 +988,7 @@
 F test/tkt3935.test e15261fedb9e30a4305a311da614a5d8e693c767
 F test/tkt3992.test f3e7d548ac26f763b47bc0f750da3d03c81071da
 F test/tkt3997.test a335fa41ca3985660a139df7b734a26ef53284bd
-F test/tkt4018.test 7c2c9ba4df489c676a0a7a0e809a1fb9b2185bd1
+F test/tkt4018.test 04115ae33ee39988a076bd78decef7cec820ebe0
 F test/tokenize.test ce430a7aed48fc98301611429595883fdfcab5d7
 F test/tpch01.test 8f4ac52f62f3e9f6bce0889105aecdf0275e331b
 F test/trace.test 4b36a41a3e9c7842151af6da5998f5080cdad9e5
@@ -1019,7 +1019,7 @@
 F test/unixexcl.test a9870e46cc6f8390a494513d4f2bf55b5a8b3e46
 F test/unordered.test ef85ac8f2f3c93ed2b9e811b684de73175fc464c
 F test/update.test 1b6c488a8f993d090b7ee9ad0e234faa161b3aeb
-F test/uri.test 63e03df051620a18f794b4f4adcdefb3c23b6751
+F test/uri.test 6aad91f02090012a03d0545a30c7cea02c70947a
 F test/utf16align.test 54cd35a27c005a9b6e7815d887718780b6a462ae
 F test/vacuum.test ce91c39f7f91a4273bf620efad21086b5aa6ef1d
 F test/vacuum2.test af432e6e3bfc0ea20a80cb86a03c7d9876d38324
@@ -1049,7 +1049,7 @@
 F test/wal.test 40073e54359d43354925b2b700b7eebd5e207285
 F test/wal2.test a8e3963abf6b232cf0b852b09b53665ef34007af
 F test/wal3.test b22eb662bcbc148c5f6d956eaf94b047f7afe9c0
-F test/wal4.test 4744e155cd6299c6bd99d3eab1c82f77db9cdb3c
+F test/wal4.test fc104ae9670221a5326c611789c205889aebe9fa
 F test/wal5.test 8f888b50f66b78821e61ed0e233ded5de378224b
 F test/wal6.test 527581f5527bf9c24394991e2be83000aace5f9e
 F test/wal7.test 2ae8f427d240099cc4b2dfef63cff44e2a68a1bd
@@ -1153,7 +1153,7 @@
 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh d1a6de74685f360ab718efda6265994b99bbea01
 F tool/win/sqlite.vsix 030f3eeaf2cb811a3692ab9c14d021a75ce41fff
-P f5ad1e1bf2828c5da70c1ff944d8212036142e6f
-R 07c665af95e97929880dcb47f3f495f9
-U drh
-Z 585a1fb26982d5587e4e3f114773e866
+P 4d7057c4942464b4302031a1ee9f195700c5e66d 0a8bcbbd4e11a60923736b2be9b1ce83ea2263fb
+R 4a7f56d4353f8f673e4fcc636c77fc4f
+U dan
+Z be45f6e8774a4940e329c99165e0e8a7
diff --git a/manifest.uuid b/manifest.uuid
index a4c42c8..45fc3ac 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-0a8bcbbd4e11a60923736b2be9b1ce83ea2263fb
\ No newline at end of file
+f352ef57e39a9443b4bdf47fc2c9b01006fe3c10
\ No newline at end of file
diff --git a/src/os_unix.c b/src/os_unix.c
index deb9e51..116428e 100644
--- a/src/os_unix.c
+++ b/src/os_unix.c
@@ -203,9 +203,7 @@
   int sectorSize;                     /* Device sector size */
   int deviceCharacteristics;          /* Precomputed device characteristics */
 #endif
-#if SQLITE_ENABLE_LOCKING_STYLE
   int openFlags;                      /* The flags specified at open() */
-#endif
 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
   unsigned fsFlags;                   /* cached details from statfs() */
 #endif
@@ -255,7 +253,8 @@
 #define UNIXFILE_DELETE      0x20     /* Delete on close */
 #define UNIXFILE_URI         0x40     /* Filename might have query parameters */
 #define UNIXFILE_NOLOCK      0x80     /* Do no file locking */
-#define UNIXFILE_WARNED    0x0100     /* verifyDbFile() warnings have been issued */
+#define UNIXFILE_WARNED    0x0100     /* verifyDbFile() has issued warnings */
+#define UNIXFILE_DEFERRED  0x0200     /* File has not yet been opened */
 
 /*
 ** Include code that is common to all os_*.c files
@@ -1357,9 +1356,14 @@
   int reserved = 0;
   unixFile *pFile = (unixFile*)id;
 
+  assert( pFile );
   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
 
-  assert( pFile );
+  if( pFile->ctrlFlags & UNIXFILE_DEFERRED ){
+    *pResOut = 0;
+    return SQLITE_OK;
+  }
+
   unixEnterMutex(); /* Because pFile->pInode is shared across threads */
 
   /* Check if a thread in this process holds such a lock */
@@ -1439,6 +1443,21 @@
   return rc;
 }
 
+static int unixOpen(sqlite3_vfs*, const char*, sqlite3_file*, int, int *);
+
+static int unixOpenAndLock(unixFile *pFile){
+  sqlite3_file *id = (sqlite3_file*)pFile;
+  int eOrigLock = pFile->eFileLock;
+  int rc;
+
+  assert( pFile->ctrlFlags & UNIXFILE_DEFERRED );
+  rc = unixOpen(pFile->pVfs, pFile->zPath, id, pFile->openFlags, 0);
+  if( rc==SQLITE_OK && eOrigLock ){
+    rc = id->pMethods->xLock(id, eOrigLock);
+  }
+  return rc;
+}
+
 /*
 ** Lock the file with the lock specified by parameter eFileLock - one
 ** of the following:
@@ -1523,9 +1542,29 @@
     return SQLITE_OK;
   }
 
+  if( pFile->ctrlFlags & UNIXFILE_DEFERRED ){
+    const char *zPath = pFile->zPath;       /* Full-path to database file */
+    int eOrigLock;                          /* Current lock held on pFile */
+
+    assert( pFile->eFileLock==SHARED_LOCK || pFile->eFileLock==NO_LOCK );
+
+    /* If SQLite is requesting a SHARED lock and the database file does
+    ** not exist, return early without opening the file. */ 
+    if( eFileLock==SHARED_LOCK && osAccess(zPath, F_OK) && errno==ENOENT ){
+      pFile->eFileLock = SHARED_LOCK;
+      return SQLITE_OK;
+    }
+
+    /* Or, if the database file has been created or a write lock is 
+    ** requested, open the database file now.  */
+    rc = unixOpenAndLock(pFile);
+    if( rc!=SQLITE_OK ) return rc;
+  }
+  assert( (pFile->ctrlFlags & UNIXFILE_DEFERRED)==0 );
+
   /* Make sure the locking sequence is correct.
   **  (1) We never move from unlocked to anything higher than shared lock.
-  **  (2) SQLite never explicitly requests a pendig lock.
+  **  (2) SQLite never explicitly requests a pending lock.
   **  (3) A shared lock is always held when a reserve lock is requested.
   */
   assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
@@ -1726,6 +1765,7 @@
     return SQLITE_OK;
   }
   unixEnterMutex();
+  if( pFile->ctrlFlags & UNIXFILE_DEFERRED ) goto end_unlock;
   pInode = pFile->pInode;
   assert( pInode->nShared!=0 );
   if( pFile->eFileLock>SHARED_LOCK ){
@@ -1932,19 +1972,23 @@
   unixUnlock(id, NO_LOCK);
   unixEnterMutex();
 
-  /* unixFile.pInode is always valid here. Otherwise, a different close
-  ** routine (e.g. nolockClose()) would be called instead.
+  /* unixFile.pInode may be NULL here if the file was never opened.
   */
-  assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
-  if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
-    /* If there are outstanding locks, do not actually close the file just
-    ** yet because that would clear those locks.  Instead, add the file
-    ** descriptor to pInode->pUnused list.  It will be automatically closed 
-    ** when the last lock is cleared.
-    */
-    setPendingFd(pFile);
+  assert( pFile->pInode==0 
+       || pFile->pInode->nLock>0 
+       || pFile->pInode->bProcessLock==0 
+  );
+  if( pFile->pInode ){
+    if( pFile->pInode->nLock ){
+      /* If there are outstanding locks, do not actually close the file just
+      ** yet because that would clear those locks.  Instead, add the file
+      ** descriptor to pInode->pUnused list.  It will be automatically closed 
+      ** when the last lock is cleared.
+      */
+      setPendingFd(pFile);
+    }
+    releaseInodeInfo(pFile);
   }
-  releaseInodeInfo(pFile);
   rc = closeUnixFile(id);
   unixLeaveMutex();
   return rc;
@@ -3173,6 +3217,20 @@
   );
 #endif
 
+  if( pFile->ctrlFlags & UNIXFILE_DEFERRED ){
+    int rc;
+    if( osAccess(pFile->zPath, F_OK) && errno==ENOENT ){
+      memset(pBuf, 0, amt);
+      rc = SQLITE_IOERR_SHORT_READ;
+    }else{
+      rc = unixOpen(pFile->pVfs, pFile->zPath, id, pFile->openFlags, 0);
+    }
+    if( rc!=SQLITE_OK ){
+      return rc;
+    }
+  }
+  assert( (pFile->ctrlFlags & UNIXFILE_DEFERRED)==0 );
+
 #if SQLITE_MAX_MMAP_SIZE>0
   /* Deal with as much of this read request as possible by transfering
   ** data from the memory mapping using memcpy().  */
@@ -3277,6 +3335,15 @@
   assert( id );
   assert( amt>0 );
 
+  /* SQLite never actually calls xWrite on an empty file before obtaining
+  ** a RESERVED lock on it. So the following condition is never true if 
+  ** the VFS is being used directly by SQLite. But it may be if this module
+  ** is being used in some other way. By the multiplexor VFS, for example. */
+  if( pFile->ctrlFlags & UNIXFILE_DEFERRED ){
+    int rc = unixOpenAndLock(pFile);
+    if( rc!=SQLITE_OK ) return rc;
+  }
+
   /* If this is a database file (not a journal, master-journal or temp
   ** file), the bytes in the locking range should never be read or written. */
 #if 0
@@ -3625,10 +3692,19 @@
 ** Determine the current size of a file in bytes
 */
 static int unixFileSize(sqlite3_file *id, i64 *pSize){
+  unixFile *pFile = (unixFile*)id;
   int rc;
   struct stat buf;
   assert( id );
-  rc = osFstat(((unixFile*)id)->h, &buf);
+  if( pFile->ctrlFlags & UNIXFILE_DEFERRED ){
+    rc = osStat(pFile->zPath, &buf);
+    if( rc && errno==ENOENT ){
+      rc = 0;
+      buf.st_size = 0;
+    }
+  }else{
+    rc = osFstat(pFile->h, &buf);
+  }
   SimulateIOError( rc=1 );
   if( rc!=0 ){
     ((unixFile*)id)->lastErrno = errno;
@@ -3644,7 +3720,6 @@
   */
   if( *pSize==1 ) *pSize = 0;
 
-
   return SQLITE_OK;
 }
 
@@ -5176,14 +5251,7 @@
   pNew->h = h;
   pNew->pVfs = pVfs;
   pNew->zPath = zFilename;
-  pNew->ctrlFlags = (u8)ctrlFlags;
-#if SQLITE_MAX_MMAP_SIZE>0
-  pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
-#endif
-  if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
-                           "psow", SQLITE_POWERSAFE_OVERWRITE) ){
-    pNew->ctrlFlags |= UNIXFILE_PSOW;
-  }
+  pNew->ctrlFlags = (unsigned short)ctrlFlags;
   if( strcmp(pVfs->zName,"unix-excl")==0 ){
     pNew->ctrlFlags |= UNIXFILE_EXCL;
   }
@@ -5322,7 +5390,6 @@
     if( h>=0 ) robust_close(pNew, h, __LINE__);
   }else{
     pNew->pMethod = pLockingStyle;
-    OpenCounter(+1);
     verifyDbFile(pNew);
   }
   return rc;
@@ -5579,7 +5646,7 @@
   int eType = flags&0xFFFFFF00;  /* Type of file to open */
   int noLock;                    /* True to omit locking primitives */
   int rc = SQLITE_OK;            /* Function Return Code */
-  int ctrlFlags = 0;             /* UNIXFILE_* flags */
+  int ctrlFlags;                 /* UNIXFILE_* flags */
 
   int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
   int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
@@ -5609,6 +5676,11 @@
   char zTmpname[MAX_PATHNAME+2];
   const char *zName = zPath;
 
+  assert( p->pInode==0 && (p->h==-1 || p->h==0) );
+  p->ctrlFlags &= ~UNIXFILE_DEFERRED;
+  p->eFileLock = NO_LOCK;
+  ctrlFlags = p->ctrlFlags;
+
   /* Check the following statements are true: 
   **
   **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
@@ -5645,7 +5717,6 @@
     sqlite3_randomness(0,0);
   }
 
-  memset(p, 0, sizeof(unixFile));
 
   if( eType==SQLITE_OPEN_MAIN_DB ){
     UnixUnusedFd *pUnused;
@@ -5818,10 +5889,69 @@
 open_finished:
   if( rc!=SQLITE_OK ){
     sqlite3_free(p->pUnused);
+    p->pUnused = 0;
   }
   return rc;
 }
 
+static int unixOpenDeferred(
+  sqlite3_vfs *pVfs,           /* The VFS for which this is the xOpen method */
+  const char *zPath,           /* Pathname of file to be opened */
+  sqlite3_file *pFile,         /* The file descriptor to be filled in */
+  int flags,                   /* Input flags to control the opening */
+  int *pOutFlags               /* Output flags returned to SQLite core */
+){
+  const int mask1 = SQLITE_OPEN_MAIN_DB | SQLITE_OPEN_READWRITE
+                  | SQLITE_OPEN_CREATE;
+  const int mask2 = SQLITE_OPEN_READONLY  | SQLITE_OPEN_DELETEONCLOSE
+                  | SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_AUTOPROXY;
+
+  int rc;                         /* Return code */
+  unixFile *p = (unixFile*)pFile; /* File object to populate */
+  const char *zUri = (flags & UNIXFILE_URI) ? zPath : 0;
+
+  /* Zero the file object */
+  memset(p, 0, sizeof(unixFile));
+  if( sqlite3_uri_boolean(zUri, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
+    p->ctrlFlags |= UNIXFILE_PSOW;
+  }
+#if SQLITE_MAX_MMAP_SIZE>0
+  p->mmapSizeMax = sqlite3GlobalConfig.szMmap;
+#endif
+
+  /* If all the flags in mask1 are set, and all the flags in mask2 are
+  ** clear, the file does not exist but the directory does and is
+  ** writable, then this is a deferred open.  */
+  if( zPath && (flags & (mask1 | mask2))==mask1 ){
+    int posixrc;
+    posixrc = osAccess(zPath, F_OK);
+    if( posixrc && errno==ENOENT ){
+      char zDirname[MAX_PATHNAME+1];
+      int i;
+      for(i=(int)strlen(zPath); i>1 && zPath[i]!='/'; i--);
+      memcpy(zDirname, zPath, i);
+      zDirname[i] = '\0';
+      posixrc = osAccess(zDirname, W_OK);
+      if( posixrc==0 ){
+        p->pMethod = (**(finder_type*)pVfs->pAppData)(0, 0);
+        if( p->pMethod->xLock==unixLock ){
+          p->pVfs = pVfs;
+          p->h = -1;
+          p->ctrlFlags |= UNIXFILE_DEFERRED;
+          p->openFlags = flags;
+          p->zPath = zPath;
+          if( pOutFlags ) *pOutFlags = flags;
+          OpenCounter(+1);
+          return SQLITE_OK;
+        }
+      }
+    }
+  }
+
+  rc = unixOpen(pVfs, zPath, pFile, flags, pOutFlags);
+  OpenCounter( rc==SQLITE_OK );
+  return rc;
+}
 
 /*
 ** Delete the file at zPath. If the dirSync argument is true, fsync()
@@ -7376,7 +7506,7 @@
     0,                    /* pNext */                       \
     VFSNAME,              /* zName */                       \
     (void*)&FINDER,       /* pAppData */                    \
-    unixOpen,             /* xOpen */                       \
+    unixOpenDeferred,     /* xOpen */                       \
     unixDelete,           /* xDelete */                     \
     unixAccess,           /* xAccess */                     \
     unixFullPathname,     /* xFullPathname */               \
diff --git a/src/test2.c b/src/test2.c
index d130e9d..a682932 100644
--- a/src/test2.c
+++ b/src/test2.c
@@ -534,7 +534,10 @@
   }
   offset = n;
   offset *= 1024*1024;
+  sqlite3OsLock(fd, SHARED_LOCK);
+  sqlite3OsLock(fd, EXCLUSIVE_LOCK);
   rc = sqlite3OsWrite(fd, "Hello, World!", 14, offset);
+  sqlite3OsUnlock(fd, NO_LOCK);
   sqlite3OsCloseFree(fd);
   sqlite3_free(zFile);
   if( rc ){
diff --git a/test/backup.test b/test/backup.test
index 444619c..1f3a3de 100644
--- a/test/backup.test
+++ b/test/backup.test
@@ -333,14 +333,14 @@
   set iTab 1
 
   db eval { PRAGMA page_size = 512 }
-  while {[file size test.db] <= $::sqlite_pending_byte} {
+  while {[file_size test.db] <= $::sqlite_pending_byte} {
     db eval "CREATE TABLE t${iTab}(a, b, c)"
     incr iTab
   }
 
   sqlite3 db2 test2.db
   db2 eval { PRAGMA page_size = 4096 }
-  while {[file size test2.db] < $::sqlite_pending_byte} {
+  while {[file_size test2.db] < $::sqlite_pending_byte} {
     db2 eval "CREATE TABLE t${iTab}(a, b, c)"
     incr iTab
   }
diff --git a/test/backup4.test b/test/backup4.test
index 417df80..78bccdb 100644
--- a/test/backup4.test
+++ b/test/backup4.test
@@ -72,7 +72,7 @@
   file size test.db
 } {1024}
 
-do_test 2.4 { file size test.db2 } 0
+do_test 2.4 { file_size test.db2 } 0
 
 db close
 forcedelete test.db
@@ -98,6 +98,6 @@
   file size test.db
 } {1024}
 
-do_test 3.4 { file size test.db2 } 0
+do_test 3.4 { file_size test.db2 } 0
 
 finish_test
diff --git a/test/capi3e.test b/test/capi3e.test
index d7ab8d0..04a9ba6 100644
--- a/test/capi3e.test
+++ b/test/capi3e.test
@@ -70,9 +70,12 @@
     sqlite3_errcode $db2
   } {SQLITE_OK}
   do_test capi3e-1.2.$i {
+    sqlite3_exec $db2 { CREATE TABLE t1(x) }
+  } {0 {}}
+  do_test capi3e-1.3.$i {
     sqlite3_close $db2
   } {SQLITE_OK}
-  do_test capi3e-1.3.$i {
+  do_test capi3e-1.4.$i {
     file isfile $name
   } {1}
 }
diff --git a/test/e_uri.test b/test/e_uri.test
index a8865ca..cff7f45 100644
--- a/test/e_uri.test
+++ b/test/e_uri.test
@@ -64,6 +64,7 @@
   do_test 1.1 {
     forcedelete file:test.db test.db
     set DB [sqlite3_open_v2 file:test.db [concat $flags SQLITE_OPEN_URI] ""]
+    sqlite3_exec $DB {CREATE TABLE t1(x)}
     list [file exists file:test.db] [file exists test.db]
   } {0 1}
   do_test 1.2 {
@@ -71,12 +72,14 @@
     set STMT [sqlite3_prepare $DB "ATTACH 'file:test.db2' AS aux" -1 dummy]
     sqlite3_step $STMT
     sqlite3_finalize $STMT
+    sqlite3_exec $DB {CREATE TABLE aux.t1(x)}
     list [file exists file:test.db2] [file exists test.db2]
   } {0 1}
   sqlite3_close $DB
   do_test 1.3 {
     forcedelete file:test.db test.db
     set DB [sqlite3_open_v2 file:test.db [concat $flags] ""]
+    sqlite3_exec $DB {CREATE TABLE t1(x)}
     list [file exists file:test.db] [file exists test.db]
   } {1 0}
   do_test 1.4 {
@@ -84,6 +87,7 @@
     set STMT [sqlite3_prepare $DB "ATTACH 'file:test.db2' AS aux" -1 dummy]
     sqlite3_step $STMT
     sqlite3_finalize $STMT
+    sqlite3_exec $DB {CREATE TABLE aux.t1(x)}
     list [file exists file:test.db2] [file exists test.db2]
   } {1 0}
   sqlite3_close $DB
@@ -96,6 +100,7 @@
   do_test 1.5 {
     forcedelete file:test.db test.db
     set DB [sqlite3_open_v2 file:test.db [concat $flags SQLITE_OPEN_URI] ""]
+    sqlite3_exec $DB {CREATE TABLE t1(x)}
     list [file exists file:test.db] [file exists test.db]
   } {0 1}
   do_test 1.6 {
@@ -103,12 +108,14 @@
     set STMT [sqlite3_prepare $DB "ATTACH 'file:test.db2' AS aux" -1 dummy]
     sqlite3_step $STMT
     sqlite3_finalize $STMT
+    sqlite3_exec $DB {CREATE TABLE aux.t1(x)}
     list [file exists file:test.db2] [file exists test.db2]
   } {0 1}
   sqlite3_close $DB
   do_test 1.7 {
     forcedelete file:test.db test.db
     set DB [sqlite3_open_v2 file:test.db [concat $flags] ""]
+    sqlite3_exec $DB {CREATE TABLE t1(x)}
     list [file exists file:test.db] [file exists test.db]
   } {0 1}
   do_test 1.8 {
@@ -116,6 +123,7 @@
     set STMT [sqlite3_prepare $DB "ATTACH 'file:test.db2' AS aux" -1 dummy]
     sqlite3_step $STMT
     sqlite3_finalize $STMT
+    sqlite3_exec $DB {CREATE TABLE aux.t1(x)}
     list [file exists file:test.db2] [file exists test.db2]
   } {0 1}
   sqlite3_close $DB
@@ -314,6 +322,7 @@
 #
 forcedelete test.db
 sqlite3 db test.db
+db eval {CREATE TABLE t1(x)}
 db close
 foreach {tn uri flags error} {
   1   {file:test.db?mode=ro}   ro    {not an error}
@@ -331,7 +340,7 @@
   set f(ro)  [list SQLITE_OPEN_READONLY SQLITE_OPEN_URI]
   set f(rw)  [list SQLITE_OPEN_READWRITE SQLITE_OPEN_URI]
   set f(rwc) [list SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_URI]
-
+breakpoint
   set DB [sqlite3_open_v2 $uri $f($flags) ""]
   set e [sqlite3_errmsg $DB]
   sqlite3_close $DB
diff --git a/test/exclusive2.test b/test/exclusive2.test
index 712363e..1644983 100644
--- a/test/exclusive2.test
+++ b/test/exclusive2.test
@@ -69,18 +69,20 @@
 }
 
 proc readPagerChangeCounter {filename} {
-  set fd [open $filename RDONLY]
-  fconfigure $fd -translation binary -encoding binary
-
-  seek $fd 24
-  foreach {a b c d} [list 0 0 0 0] {}
-  binary scan [read $fd 4] cccc a b c d
-  set  ret [expr ($a&0x000000FF)<<24]
-  incr ret [expr ($b&0x000000FF)<<16]
-  incr ret [expr ($c&0x000000FF)<<8]
-  incr ret [expr ($d&0x000000FF)<<0]
-
-  close $fd
+  if {[file exists $filename]} {
+    set fd [open $filename RDONLY]
+    fconfigure $fd -translation binary -encoding binary
+    seek $fd 24
+    foreach {a b c d} [list 0 0 0 0] {}
+    binary scan [read $fd 4] cccc a b c d
+    set  ret [expr ($a&0x000000FF)<<24]
+    incr ret [expr ($b&0x000000FF)<<16]
+    incr ret [expr ($c&0x000000FF)<<8]
+    incr ret [expr ($d&0x000000FF)<<0]
+    close $fd
+  } else {
+    set ret 0
+  }
   return $ret
 }
 
diff --git a/test/incrvacuum.test b/test/incrvacuum.test
index 91f5c8e..4bae063 100644
--- a/test/incrvacuum.test
+++ b/test/incrvacuum.test
@@ -38,7 +38,7 @@
   # File size is sometimes 1 instead of 0 due to the hack we put in
   # to work around ticket #3260.  Search for comments on #3260 in
   # os_unix.c.
-  expr {[file size test.db] > 1}
+  expr {[file_size test.db] > 1}
 } {0}
 do_test incrvacuum-1.2 {
   # This command will create the database.
@@ -700,7 +700,7 @@
   # File size is sometimes 1 instead of 0 due to the hack we put in
   # to work around ticket #3260.  Search for comments on #3260 in
   # os_unix.c.
-  expr {[file size test.db]>1}
+  expr {[file_size test.db]>1}
 } {0}
 do_test incrvacuum-13.2 {
   set ::STMT [sqlite3_prepare $::DB {PRAGMA auto_vacuum = 2} -1 DUMMY]
diff --git a/test/io.test b/test/io.test
index c5086c1..0a080fb 100644
--- a/test/io.test
+++ b/test/io.test
@@ -391,7 +391,7 @@
     }
     # File size might be 1 due to the hack to work around ticket #3260.
     # Search for #3260 in os_unix.c for additional information.
-    expr {[file size test.db]>1}
+    expr {[file_size test.db]>1}
   } {0}
   do_test io-3.2 {
     execsql { CREATE TABLE abc(a, b) }
diff --git a/test/pager1.test b/test/pager1.test
index 005b356..cd8299a 100644
--- a/test/pager1.test
+++ b/test/pager1.test
@@ -1373,7 +1373,7 @@
   list [B step 10000] [B finish]
 } {SQLITE_DONE SQLITE_OK}
 do_test pager1-9.4.2 {
-  list [file size test.db2] [file size test.db]
+  list [file_size test.db2] [file_size test.db]
 } {1024 0}
 db2 close
 
diff --git a/test/permutations.test b/test/permutations.test
index 7f1485f..9c908d1 100644
--- a/test/permutations.test
+++ b/test/permutations.test
@@ -135,7 +135,7 @@
   This test suite is the same as the "quick" tests, except that some files
   that test malloc and IO errors are omitted.
 } -files [
-  test_set $allquicktests -exclude *malloc* *ioerr* *fault*
+  test_set $allquicktests -exclude *malloc* *ioerr* *fault* 
 ]
 
 test_suite "mmap" -prefix "mm-" -description {
diff --git a/test/syscall.test b/test/syscall.test
index 5bf1225..290e8d7 100644
--- a/test/syscall.test
+++ b/test/syscall.test
@@ -236,9 +236,10 @@
 forcedelete test.db test.db2
 
 do_test 8.1 {
+  close [open test.db w]
   sqlite3 db test.db
   file_control_chunksize_test db main 4096
-  file size test.db
+  file_size test.db
 } {0}
 foreach {tn hint size} {
   1  1000    4096 
@@ -249,13 +250,14 @@
 } {
   do_test 8.2.$tn {
     file_control_sizehint_test db main $hint
-    file size test.db
+    file_size test.db
   } $size
 }
 
 do_test 8.3 {
   db close
   forcedelete test.db test.db2
+  close [open test.db w]
   sqlite3 db test.db
   file_control_chunksize_test db main 16
   file size test.db
diff --git a/test/tester.tcl b/test/tester.tcl
index fefd5e8..e032856 100644
--- a/test/tester.tcl
+++ b/test/tester.tcl
@@ -185,6 +185,16 @@
   do_copy_file true $from $to
 }
 
+# If file $zFile exists in the file system, return its size in bytes. 
+# Otherwise, return zero.
+proc file_size {zFile} {
+  if {[file exists $zFile]} {
+    return [file size $zFile]
+  }
+  return 0
+}
+
+
 proc do_copy_file {force from to} {
   set nRetry [getFileRetries]     ;# Maximum number of retries.
   set nDelay [getFileRetryDelay]  ;# Delay in ms before retrying.
diff --git a/test/tkt4018.test b/test/tkt4018.test
index 2bc41d4..b091937 100644
--- a/test/tkt4018.test
+++ b/test/tkt4018.test
@@ -46,6 +46,7 @@
 # connection to test.db 10000 times. If file-descriptors are not being
 # reused, then the process will quickly exceed its maximum number of
 # file descriptors (1024 by default on linux).
+breakpoint
 do_test tkt4018-1.2 {
   for {set i 0} {$i < 10000} {incr i} {
     sqlite3 db2 test.db
diff --git a/test/uri.test b/test/uri.test
index af1ad67..0ff6b93 100644
--- a/test/uri.test
+++ b/test/uri.test
@@ -76,6 +76,7 @@
   forcedelete $file
   do_test 1.$tn.1 { file exists $file } 0
   set DB [sqlite3_open $uri]
+  sqlite3_exec $DB {CREATE TABLE t1(x)}
   do_test 1.$tn.2 { file exists $file } 1
   sqlite3_close $DB
   forcedelete $file
@@ -83,6 +84,7 @@
   do_test 1.$tn.3 { file exists $file } 0
   sqlite3 db xxx.db
   catchsql { ATTACH $uri AS aux }
+  db eval {CREATE TABLE aux.t1(x)}
   do_test 1.$tn.4 { file exists $file } 1
   db close
 }
diff --git a/test/wal4.test b/test/wal4.test
index c7a4381..8e6831e 100644
--- a/test/wal4.test
+++ b/test/wal4.test
@@ -56,8 +56,8 @@
   if { $testrc==0 && [file exists test.db-wal] } { 
     error "Wal file was not deleted"
   }
-  if { [file size test.db]!=0 } { 
-    error "Db file grew to [file size test.db] bytes"
+  if { [file_size test.db]!=0 } { 
+    error "Db file grew to [file_size test.db] bytes"
   }
 }