@@ -26,6 +26,7 @@ func (h *hbshVFS) OpenFilename(name *vfs.Filename, flags vfs.OpenFlag) (file vfs
2626 } else {
2727 file , flags , err = h .Open (name .String (), flags )
2828 }
29+
2930 // Encrypt everything except super journals and memory files.
3031 if err != nil || flags & (vfs .OPEN_SUPER_JOURNAL | vfs .OPEN_MEMORY ) != 0 {
3132 return file , flags , err
@@ -47,7 +48,12 @@ func (h *hbshVFS) OpenFilename(name *vfs.Filename, flags vfs.OpenFlag) (file vfs
4748 }
4849 hbsh = h .hbsh .HBSH (key )
4950 }
50- return & hbshFile {File : file , hbsh : hbsh , reset : h .hbsh }, flags , err
51+
52+ // Main datatabases may have their key specified later, as a PRAGMA.
53+ if hbsh != nil || flags & vfs .OPEN_MAIN_DB != 0 {
54+ return & hbshFile {File : file , hbsh : hbsh , reset : h .hbsh }, flags , nil
55+ }
56+ return nil , flags , sqlite3 .CANTOPEN
5157}
5258
5359const (
@@ -87,9 +93,11 @@ func (h *hbshFile) Pragma(name string, value string) (string, error) {
8793
8894func (h * hbshFile ) ReadAt (p []byte , off int64 ) (n int , err error ) {
8995 if h .hbsh == nil {
96+ // Only OPEN_MAIN_DB can have a missing key.
9097 if off == 0 && len (p ) == 100 {
91- // SQLite is trying to read the header of a database.
92- // Pretend the file is empty so the key can be specified later.
98+ // SQLite is trying to read the header of a database file.
99+ // Pretend the file is empty so the key may specified later,
100+ // as a PRAGMA.
93101 return 0 , io .EOF
94102 }
95103 return 0 , sqlite3 .CANTOPEN
@@ -140,7 +148,7 @@ func (h *hbshFile) WriteAt(p []byte, off int64) (n int, err error) {
140148 if err != io .EOF {
141149 return n , err
142150 }
143- // Writing past the EOF:
151+ // Writing past the EOF.
144152 // We're either appending an entirely new block,
145153 // or the final block was only partially written.
146154 // A partially written block can't be decrypted,
0 commit comments