Files
HackBrowserData/internal/sqlitex/sqlite.go
T
Roger fed6c722e2 refactor: open SQLite in one place and stream records (#688)
* refactor: stream records through sqlitex and the collector

A row that failed to scan was logged at debug and dropped, so it never counted toward partial; it now reaches Collector.Fail.
Every SQLite open goes through sqlitex with options instead of a bool, and extractors add records as they read them.

* refactor: read SQLite rows with plain loops

EachRow hid each extractor's loop and its skip rule behind two callbacks; every extractor now opens, queries, and walks
its own rows, so the control flow reads top to bottom.

* refactor(browser): drop the memoizing key closures

Firefox derives its key once before the login loop, and Yandex loads its data key at the first row, so the state sits
in plain variables where the loop uses it instead of inside a closure.

* fix(safari): report unreadable localStorage origins

A broken origin, database or row was only logged at debug level, so it never counted toward a partial result.
2026-11-09 17:25:36 +08:00

55 lines
1.6 KiB
Go

// Package sqlitex opens the copied SQLite databases that extractors read.
package sqlitex
import (
"database/sql"
"errors"
"fmt"
"net/url"
"os"
_ "modernc.org/sqlite" // registers the "sqlite" driver for database/sql
)
// Option changes how Open opens a database.
type Option func(*settings)
type settings struct {
parameters url.Values
pragmas []string
}
// ReadOnly opens the file without write access.
func ReadOnly(s *settings) { s.parameters.Set("mode", "ro") }
// Immutable promises SQLite the file never changes, so it takes no lock and reads no journal.
func Immutable(s *settings) { s.parameters.Set("immutable", "1") }
// JournalOff stops SQLite journaling the copy, the mode the Firefox and Safari extractors have always read in.
func JournalOff(s *settings) { s.pragmas = append(s.pragmas, "PRAGMA journal_mode=off") }
// Open checks the file first, so a missing database is an error rather than a new empty one.
func Open(path string, options ...Option) (*sql.DB, error) {
if _, err := os.Stat(path); err != nil {
return nil, fmt.Errorf("database file: %w", err)
}
opened := settings{parameters: url.Values{}}
for _, option := range options {
option(&opened)
}
dataSource := path
if len(opened.parameters) > 0 {
dataSource = "file:" + path + "?" + opened.parameters.Encode()
}
database, err := sql.Open("sqlite", dataSource)
if err != nil {
return nil, err
}
for _, pragma := range opened.pragmas {
if _, err := database.Exec(pragma); err != nil {
return nil, errors.Join(fmt.Errorf("%s: %w", pragma, err), database.Close())
}
}
return database, nil
}