/ Check-in [45bb84c6]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Test the libraries response to read-only or unreadable database, WAL and wal-index files. If a WAL file cannot be opened in read/write mode, return SQLITE_CANTOPEN to the caller.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 45bb84c6283d803fc29077fdc2d06fa50ec06a59
User & Date: dan 2010-07-14 16:37:18
Original Comment: Test the libaries response to read-only or unreadable database, WAL and wal-index files. If a WAL file cannot be opened in read/write mode, return SQLITE_CANTOPEN to the caller.
Context
2010-07-14
18:10
Fix an assert() failure in wal2.test caused by messing with the contents of shared memory. check-in: 9f452514 user: dan tags: trunk
16:37
Test the libraries response to read-only or unreadable database, WAL and wal-index files. If a WAL file cannot be opened in read/write mode, return SQLITE_CANTOPEN to the caller. check-in: 45bb84c6 user: dan tags: trunk
14:48
On unix, try to create the *-wal and *-shm files with the same permissions as the associated database file. check-in: e5d180ee user: dan tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/wal.c.

  1222   1222     pRet->pDbFd = pDbFd;
  1223   1223     pRet->readLock = -1;
  1224   1224     pRet->zWalName = zWalName;
  1225   1225   
  1226   1226     /* Open file handle on the write-ahead log file. */
  1227   1227     flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
  1228   1228     rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
         1229  +  if( rc==SQLITE_OK && flags&SQLITE_OPEN_READONLY ){
         1230  +    rc = SQLITE_CANTOPEN;
         1231  +  }
  1229   1232   
  1230   1233     if( rc!=SQLITE_OK ){
  1231   1234       walIndexClose(pRet, 0);
  1232   1235       sqlite3OsClose(pRet->pWalFd);
  1233   1236       sqlite3_free(pRet);
  1234   1237     }else{
  1235   1238       *ppWal = pRet;

Changes to test/malloc_common.tcl.

   133    133     }
   134    134   }
   135    135   
   136    136   #-------------------------------------------------------------------------
   137    137   # Procedures to save and restore the current file-system state:
   138    138   #
   139    139   #   faultsim_save
          140  +#   faultsim_restore
   140    141   #   faultsim_save_and_close
   141    142   #   faultsim_restore_and_reopen
   142    143   #   faultsim_delete_and_reopen
   143    144   #
   144    145   proc faultsim_save {} {
   145    146     foreach f [glob -nocomplain sv_test.db*] { file delete -force $f }
   146    147     foreach f [glob -nocomplain test.db*] {
................................................................................
   149    150     }
   150    151   }
   151    152   proc faultsim_save_and_close {} {
   152    153     faultsim_save
   153    154     catch { db close }
   154    155     return ""
   155    156   }
   156         -proc faultsim_restore_and_reopen {{dbfile test.db}} {
   157         -  catch { db close }
          157  +proc faultsim_restore {} {
   158    158     foreach f [glob -nocomplain test.db*] { file delete -force $f }
   159    159     foreach f2 [glob -nocomplain sv_test.db*] {
   160    160       set f [string range $f2 3 end]
   161    161       file copy -force $f2 $f
   162    162     }
          163  +}
          164  +proc faultsim_restore_and_reopen {{dbfile test.db}} {
          165  +  catch { db close }
          166  +  faultsim_restore
   163    167     sqlite3 db $dbfile
   164    168     sqlite3_extended_result_codes db 1
   165    169     sqlite3_db_config_lookaside db 0 0 0
   166    170   }
   167    171   
   168    172   proc faultsim_integrity_check {{db db}} {
   169    173     set ic [$db eval { PRAGMA integrity_check }]

Changes to test/wal2.test.

  1001   1001   #-------------------------------------------------------------------------
  1002   1002   # If a connection is required to create a WAL or SHM file, it creates 
  1003   1003   # the new files with the same file-system permissions as the database 
  1004   1004   # file itself. Test this.
  1005   1005   #
  1006   1006   if {$::tcl_platform(platform) == "unix"} {
  1007   1007     faultsim_delete_and_reopen
  1008         -
  1009   1008     set umask [exec /bin/sh -c umask]
         1009  +
  1010   1010     do_test wal2-12.1 {
  1011   1011       sqlite3 db test.db
  1012   1012       execsql { 
  1013   1013         CREATE TABLE tx(y, z);
  1014   1014         PRAGMA journal_mode = WAL;
  1015   1015       }
  1016   1016       db close
................................................................................
  1041   1041       } [list $effective $effective]
  1042   1042       do_test wal2-12.2.$tn.5 {
  1043   1043         db close
  1044   1044         list [file exists test.db-wal] [file exists test.db-shm]
  1045   1045       } {0 0}
  1046   1046     }
  1047   1047   }
         1048  +
         1049  +#-------------------------------------------------------------------------
         1050  +# Test the libraries response to discovering that one or more of the
         1051  +# database, wal or shm files cannot be opened, or can only be opened
         1052  +# read-only.
         1053  +#
         1054  +if {$::tcl_platform(platform) == "unix"} {
         1055  +  proc perm {} {
         1056  +    set L [list]
         1057  +    foreach f {test.db test.db-wal test.db-shm} {
         1058  +      if {[file exists $f]} {
         1059  +        lappend L [file attr $f -perm]
         1060  +      } else {
         1061  +        lappend L {}
         1062  +      }
         1063  +    }
         1064  +    set L
         1065  +  }
         1066  +
         1067  +  faultsim_delete_and_reopen
         1068  +  execsql {
         1069  +    PRAGMA journal_mode = WAL;
         1070  +    CREATE TABLE t1(a, b);
         1071  +    PRAGMA wal_checkpoint;
         1072  +    INSERT INTO t1 VALUES('3.14', '2.72');
         1073  +  }
         1074  +  do_test wal2-13.1.1 {
         1075  +    list [file exists test.db-shm] [file exists test.db-wal]
         1076  +  } {1 1}
         1077  +  faultsim_save_and_close
         1078  +
         1079  +  foreach {tn db_perm wal_perm shm_perm can_open can_read can_write} {
         1080  +    2   00644   00644   00644   1   1   1
         1081  +    3   00644   00400   00644   1   0   0
         1082  +    4   00644   00644   00400   1   0   0
         1083  +    5   00400   00644   00644   1   1   0
         1084  +
         1085  +    7   00644   00000   00644   1   0   0
         1086  +    8   00644   00644   00000   1   0   0
         1087  +    9   00000   00644   00644   0   0   0
         1088  +  } {
         1089  +    faultsim_restore
         1090  +    do_test wal2-13.$tn.1 {
         1091  +      file attr test.db     -perm $db_perm
         1092  +      file attr test.db-wal -perm $wal_perm
         1093  +      file attr test.db-shm -perm $shm_perm
         1094  +
         1095  +      set     L [file attr test.db -perm]
         1096  +      lappend L [file attr test.db-wal -perm]
         1097  +      lappend L [file attr test.db-shm -perm]
         1098  +    } [list $db_perm $wal_perm $shm_perm]
         1099  +
         1100  +    # If $can_open is true, then it should be possible to open a database
         1101  +    # handle. Otherwise, if $can_open is 0, attempting to open the db
         1102  +    # handle throws an "unable to open database file" exception.
         1103  +    #
         1104  +    set r(1) {0 ok}
         1105  +    set r(0) {1 {unable to open database file}}
         1106  +    do_test wal2-13.$tn.2 {
         1107  +      list [catch {sqlite3 db test.db ; set {} ok} msg] $msg
         1108  +    } $r($can_open)
         1109  +
         1110  +    if {$can_open} {
         1111  +
         1112  +      # If $can_read is true, then the client should be able to read from
         1113  +      # the database file. If $can_read is false, attempting to read should
         1114  +      # throw the "unable to open database file" exception. 
         1115  +      #
         1116  +      set a(0) {1 {unable to open database file}}
         1117  +      set a(1) {0 {3.14 2.72}}
         1118  +      do_test wal2-13.$tn.3 {
         1119  +        catchsql { SELECT * FROM t1 }
         1120  +      } $a($can_read)
         1121  +  
         1122  +      # Now try to write to the db file. If the client can read but not
         1123  +      # write, then it should throw the familiar "unable to open db file"
         1124  +      # exception. If it can read but not write, the exception should
         1125  +      # be "attempt to write a read only database".
         1126  +      #
         1127  +      # If the client can read and write, the operation should succeed.
         1128  +      #
         1129  +      set b(0,0) {1 {unable to open database file}}
         1130  +      set b(1,0) {1 {attempt to write a readonly database}}
         1131  +      set b(1,1) {0 {}}
         1132  +      do_test wal2-13.$tn.4 {
         1133  +        catchsql { INSERT INTO t1 DEFAULT VALUES }
         1134  +      } $b($can_read,$can_write)
         1135  +    }
         1136  +    catch { db close }
         1137  +  }
         1138  +}
  1048   1139   
  1049   1140   finish_test
  1050   1141