go-sqlite3 icon indicating copy to clipboard operation
go-sqlite3 copied to clipboard

WIP Add checksum VFS shim as an opt-in feature

Open mattyhall opened this issue 1 year ago • 0 comments

I am working on a project that would like to turn checksumming on in SQLite, using the checksum VFS shim (see https://www.sqlite.org/cksumvfs.html). This extension isn't in the amalgamation and can either be statically or dynamically linked, but it would be easier if we could toggle it on and off using Go tags like the other features.

The code in this PR is a work in progress (missing documentation, an example, updating the README etc) to see if it is something you are interested in. A user can build with the tag cksumvfs and then use like so:

// We need to set the number of reserved bytes to at least 8 so the checksum can be included
sql.Register("sqlite3_with_chksum", &sqlite3.SQLiteDriver{
	ConnectHook: func(conn *sqlite3.SQLiteConn) error {
		return conn.SetFileControlInt("", sqlite3.SQLITE_FCNTL_RESERVE_BYTES, 8)
	},
})

db, err := sql.Open("sqlite3_with_chksum", "./foo.db")
if err != nil {
	log.Fatal(err)
}
defer db.Close()

// This makes the checksum VFS be the default, wrapping the previous default
sqlite3.InitCksumVFS()

// Corrupted pages will now cause an ErrIoErrData extended error to be returned

Questions

  1. Is this something the project would be interesting in having?
  2. Is it ok to just merge the code into the amalgamation - I took this approach from the user auth stuff? In terms of compile times I think it should be ok - the checksum implementation is only ~900 LOC/~40 small functions (src)
  3. If this isn't wanted is there a way to statically link it into sqlite using this library?

Please let me know what you think - if it's something you'd be happy to accept I'll go and clean this up a bit and add the documentation :)

mattyhall avatar Dec 18 '24 15:12 mattyhall