Skip to content

Commit 75c1dbb

Browse files
authored
Checksum VFS. (#176)
1 parent 64e2500 commit 75c1dbb

File tree

24 files changed

+499
-41
lines changed

24 files changed

+499
-41
lines changed

conn.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -521,10 +521,3 @@ func (c *Conn) stmtsIter(yield func(*Stmt) bool) {
521521
}
522522
}
523523
}
524-
525-
// DriverConn is implemented by the SQLite [database/sql] driver connection.
526-
//
527-
// Deprecated: use [github.com/ncruces/go-sqlite3/driver.Conn] instead.
528-
type DriverConn interface {
529-
Raw() *Conn
530-
}

ext/bloom/testdata/bloom.db

0 Bytes
Binary file not shown.

tests/testdata/utf16be.db

-512 Bytes
Binary file not shown.

tests/testdata/wal.db

0 Bytes
Binary file not shown.

tests/wal_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func TestWAL_readonly(t *testing.T) {
7777

7878
// Select the data using the second (readonly) connection.
7979
var name string
80-
err = db2.QueryRow("SELECT name FROM t").Scan(&name)
80+
err = db2.QueryRow(`SELECT name FROM t`).Scan(&name)
8181
if err != nil {
8282
t.Fatal(err)
8383
}
@@ -95,7 +95,7 @@ func TestWAL_readonly(t *testing.T) {
9595
}
9696

9797
// Select the data using the second (readonly) connection.
98-
err = db2.QueryRow("SELECT name FROM t").Scan(&name)
98+
err = db2.QueryRow(`SELECT name FROM t`).Scan(&name)
9999
if err != nil {
100100
t.Fatal(err)
101101
}

txn.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (c *Conn) Savepoint() Savepoint {
143143
// Names can be reused, but this makes catching bugs more likely.
144144
name = QuoteIdentifier(name + "_" + strconv.Itoa(int(rand.Int31())))
145145

146-
err := c.txnExecInterrupted("SAVEPOINT " + name)
146+
err := c.txnExecInterrupted(`SAVEPOINT ` + name)
147147
if err != nil {
148148
panic(err)
149149
}
@@ -187,7 +187,7 @@ func (s Savepoint) Release(errp *error) {
187187
if s.c.GetAutocommit() { // There is nothing to commit.
188188
return
189189
}
190-
*errp = s.c.Exec("RELEASE " + s.name)
190+
*errp = s.c.Exec(`RELEASE ` + s.name)
191191
if *errp == nil {
192192
return
193193
}
@@ -199,8 +199,7 @@ func (s Savepoint) Release(errp *error) {
199199
return
200200
}
201201
// ROLLBACK and RELEASE even if interrupted.
202-
err := s.c.txnExecInterrupted("ROLLBACK TO " +
203-
s.name + "; RELEASE " + s.name)
202+
err := s.c.txnExecInterrupted(`ROLLBACK TO ` + s.name + `; RELEASE ` + s.name)
204203
if err != nil {
205204
panic(err)
206205
}
@@ -213,7 +212,7 @@ func (s Savepoint) Release(errp *error) {
213212
// https://sqlite.org/lang_transaction.html
214213
func (s Savepoint) Rollback() error {
215214
// ROLLBACK even if interrupted.
216-
return s.c.txnExecInterrupted("ROLLBACK TO " + s.name)
215+
return s.c.txnExecInterrupted(`ROLLBACK TO ` + s.name)
217216
}
218217

219218
func (c *Conn) txnExecInterrupted(sql string) error {

util/vfsutil/wrap.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ func UnwrapFile[T vfs.File](f vfs.File) (_ T, _ bool) {
2222
}
2323
}
2424

25+
// WrapOpenFilename helps wrap [vfs.VFSFilename].
26+
func WrapOpenFilename(f vfs.VFS, name *vfs.Filename, flags vfs.OpenFlag) (file vfs.File, _ vfs.OpenFlag, err error) {
27+
if f, ok := f.(vfs.VFSFilename); ok {
28+
return f.OpenFilename(name, flags)
29+
}
30+
return f.Open(name.String(), flags)
31+
}
32+
2533
// WrapLockState helps wrap [vfs.FileLockState].
2634
func WrapLockState(f vfs.File) vfs.LockLevel {
2735
if f, ok := f.(vfs.FileLockState); ok {

vfs/adiantum/adiantum_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var testDB string
2121

2222
func Test_fileformat(t *testing.T) {
2323
readervfs.Create("test.db", ioutil.NewSizeReaderAt(strings.NewReader(testDB)))
24-
adiantum.Register("radiantum", vfs.Find("reader"), nil)
24+
vfs.Register("radiantum", adiantum.Wrap(vfs.Find("reader"), nil))
2525

2626
db, err := driver.Open("file:test.db?vfs=radiantum")
2727
if err != nil {

vfs/adiantum/api.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,25 @@ import (
4040
)
4141

4242
func init() {
43-
Register("adiantum", vfs.Find(""), nil)
43+
vfs.Register("adiantum", Wrap(vfs.Find(""), nil))
4444
}
4545

46-
// Register registers an encrypting VFS, wrapping a base VFS,
47-
// and possibly using a custom HBSH cipher construction.
46+
// Wrap wraps a base VFS to create an encrypting VFS,
47+
// possibly using a custom HBSH cipher construction.
48+
//
4849
// To use the default Adiantum construction, set cipher to nil.
4950
//
5051
// The default construction uses a 32 byte key/hexkey.
5152
// If a textkey is provided, the default KDF is Argon2id
5253
// with 64 MiB of memory, 3 iterations, and 4 threads.
53-
func Register(name string, base vfs.VFS, cipher HBSHCreator) {
54+
func Wrap(base vfs.VFS, cipher HBSHCreator) vfs.VFS {
5455
if cipher == nil {
5556
cipher = adiantumCreator{}
5657
}
57-
vfs.Register(name, &hbshVFS{
58+
return &hbshVFS{
5859
VFS: base,
5960
init: cipher,
60-
})
61+
}
6162
}
6263

6364
// HBSHCreator creates an [hbsh.HBSH] cipher

vfs/adiantum/example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
)
1818

1919
func ExampleRegister_hpolyc() {
20-
adiantum.Register("hpolyc", vfs.Find(""), hpolycCreator{})
20+
vfs.Register("hpolyc", adiantum.Wrap(vfs.Find(""), hpolycCreator{}))
2121

2222
db, err := sqlite3.Open("file:demo.db?vfs=hpolyc" +
2323
"&textkey=correct+horse+battery+staple")

0 commit comments

Comments
 (0)