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

FossilOrigin-Name: f352ef57e39a9443b4bdf47fc2c9b01006fe3c10
diff --git a/manifest b/manifest
index 7f2b597..fb3e8f6 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Merge\slatest\strunk\schanges.
-D 2014-02-12T14:43:01.128
+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 d75a3d63df53542d74fd8af2f76ef7ea639cfc71
+F src/os_unix.c c53e2683d8cc23d08f28dbecf0e3c2bcf1d1acca
 F src/os_win.c d4284f003445054a26689f1264b1b9bf7261bd1b
 F src/pager.c 0ffa313a30ed6d061d9c6601b7b175cc50a1cab7
 F src/pager.h ffd5607f7b3e4590b415b007a4382f693334d428
@@ -336,9 +336,9 @@
 F test/autovacuum_ioerr2.test 8a367b224183ad801e0e24dcb7d1501f45f244b4
 F test/avtrans.test 0252654f4295ddda3b2cce0e894812259e655a85
 F test/backcompat.test 5f8ad58b3eaebc78cd2c66c65476a42e6f32b2ad
-F test/backup.test cfac575288523288f0acfb6470b7e9cba6c16a52
+F test/backup.test 3b072335eadafa32ab940fceed5852fb48c30655
 F test/backup2.test 34986ef926ea522911a51dfdb2f8e99b7b75ebcf
-F test/backup4.test 4e3a2f9d83cdfeb4efd5dddb0dfbd06116d50957
+F test/backup4.test 139a465f76d43259123d6e09281d080fccc59f8a
 F test/backup_ioerr.test 4c3c7147cee85b024ecf6e150e090c32fdbb5135
 F test/backup_malloc.test 7162d604ec2b4683c4b3799a48657fb8b5e2d450
 F test/badutf.test d5360fc31f643d37a973ab0d8b4fb85799c3169f
@@ -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 64771d6c1cbe270acb788dfdc32ede157a627583
+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
@@ -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 8ef97420b2614f50de6e6d7c758483b8b888f325 0a8bcbbd4e11a60923736b2be9b1ce83ea2263fb
-R 49c6d16c122de920ce19e3016ce96465
+P 4d7057c4942464b4302031a1ee9f195700c5e66d 0a8bcbbd4e11a60923736b2be9b1ce83ea2263fb
+R 4a7f56d4353f8f673e4fcc636c77fc4f
 U dan
-Z b1935a89c2fa8977f31cb64f1fee4e2a
+Z be45f6e8774a4940e329c99165e0e8a7
diff --git a/manifest.uuid b/manifest.uuid
index bf4a1be..45fc3ac 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-4d7057c4942464b4302031a1ee9f195700c5e66d
\ 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 75a567a..116428e 100644
--- a/src/os_unix.c
+++ b/src/os_unix.c
@@ -1445,6 +1445,19 @@
 
 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:
@@ -1544,11 +1557,7 @@
 
     /* Or, if the database file has been created or a write lock is 
     ** requested, open the database file now.  */
-    eOrigLock = pFile->eFileLock;
-    rc = unixOpen(pFile->pVfs, zPath, id, pFile->openFlags, 0);
-    if( rc==SQLITE_OK && eOrigLock ){
-      rc = unixLock(id, eOrigLock);
-    }
+    rc = unixOpenAndLock(pFile);
     if( rc!=SQLITE_OK ) return rc;
   }
   assert( (pFile->ctrlFlags & UNIXFILE_DEFERRED)==0 );
@@ -3326,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
@@ -5234,9 +5252,6 @@
   pNew->pVfs = pVfs;
   pNew->zPath = zFilename;
   pNew->ctrlFlags = (unsigned short)ctrlFlags;
-#if SQLITE_MAX_MMAP_SIZE>0
-  pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
-#endif
   if( strcmp(pVfs->zName,"unix-excl")==0 ){
     pNew->ctrlFlags |= UNIXFILE_EXCL;
   }
@@ -5872,6 +5887,10 @@
   rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
 
 open_finished:
+  if( rc!=SQLITE_OK ){
+    sqlite3_free(p->pUnused);
+    p->pUnused = 0;
+  }
   return rc;
 }
 
@@ -5889,14 +5908,16 @@
 
   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( (flags & UNIXFILE_URI) 
-   && sqlite3_uri_boolean(zPath, "psow", SQLITE_POWERSAFE_OVERWRITE) 
-  ){
+  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
@@ -5913,14 +5934,16 @@
       posixrc = osAccess(zDirname, W_OK);
       if( posixrc==0 ){
         p->pMethod = (**(finder_type*)pVfs->pAppData)(0, 0);
-        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;
+        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;
+        }
       }
     }
   }
diff --git a/test/backup.test b/test/backup.test
index 42d2571..1f3a3de 100644
--- a/test/backup.test
+++ b/test/backup.test
@@ -325,13 +325,6 @@
 }
 }
 
-proc file_size {zFile} {
-  if {[file exists $zFile]} {
-    return [file size $zFile]
-  }
-  return 0
-}
-
 #--------------------------------------------------------------------
 do_test backup-3.$iTest.1 {
   catch { forcedelete test.db }
diff --git a/test/backup4.test b/test/backup4.test
index bdfb213..78bccdb 100644
--- a/test/backup4.test
+++ b/test/backup4.test
@@ -23,13 +23,6 @@
 source $testdir/tester.tcl
 set testprefix backup4
 
-proc file_size {zFile} {
-  if {[file exists $zFile]} {
-    return [file size $zFile]
-  }
-  return 0
-}
-
 #-------------------------------------------------------------------------
 # At one point this test was failing because [db] was using an out of
 # date schema in test case 1.2.
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 ad087c9..9c908d1 100644
--- a/test/permutations.test
+++ b/test/permutations.test
@@ -135,8 +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* \
-    *multiplex* *quota* walbak.test
+  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/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"
   }
 }