author | Alberto Bertogli
<albertito@blitiri.com.ar> 2023-08-18 09:45:11 UTC |
committer | Alberto Bertogli
<albertito@blitiri.com.ar> 2023-08-19 11:19:16 UTC |
parent | 05ea437e3a1cd159e9040b4ebbce012f36870e99 |
db.go | +0 | -77 |
go.mod | +0 | -1 |
go.sum | +0 | -2 |
summer.go | +1 | -9 |
test/access.t | +3 | -3 |
test/dryrun_sqlite.t | +0 | -36 |
test/help.t | +3 | -12 |
test/sqlite.t | +0 | -40 |
diff --git a/db.go b/db.go index a186096..a6b3acd 100644 --- a/db.go +++ b/db.go @@ -2,15 +2,11 @@ package main import ( "bytes" - "database/sql" "encoding/binary" "flag" "os" - "path/filepath" "github.com/pkg/xattr" - - _ "github.com/mattn/go-sqlite3" ) var dryRun = flag.Bool("n", false, @@ -64,76 +60,3 @@ func (_ XattrDB) Write(f *os.File, cs ChecksumV1) error { func (_ XattrDB) Close() error { return nil } - -type SqliteDB struct { - root string - db *sql.DB -} - -const createTableV1 = ` - create table if not exists checksums ( - path string primary key, - crc32c integer, - modtimeusec integer - ); -` - -func OpenSqliteDB(dbPath, root string) (*SqliteDB, error) { - db, err := sql.Open("sqlite3", dbPath) - if err != nil { - return nil, err - } - - if _, err := db.Exec(createTableV1); err != nil { - return nil, err - } - - return &SqliteDB{root, db}, nil -} - -func (s *SqliteDB) Has(f *os.File) (bool, error) { - path, err := filepath.Rel(s.root, f.Name()) - if err != nil { - return false, err - } - - q := 0 - err = s.db.QueryRow( - "select count(1) from checksums where path = ?", - path).Scan(&q) - return q == 1, err -} - -func (s *SqliteDB) Read(f *os.File) (ChecksumV1, error) { - cs := ChecksumV1{} - path, err := filepath.Rel(s.root, f.Name()) - if err != nil { - return cs, err - } - - err = s.db.QueryRow( - "select crc32c, modtimeusec from checksums where path = ?", - path).Scan(&cs.CRC32C, &cs.ModTimeUsec) - return cs, err -} - -func (s *SqliteDB) Write(f *os.File, cs ChecksumV1) error { - if *dryRun { - return nil - } - - path, err := filepath.Rel(s.root, f.Name()) - if err != nil { - return err - } - - _, err = s.db.Exec( - "insert or replace into checksums "+ - "(path, crc32c, modtimeusec) values(?, ?, ?)", - path, cs.CRC32C, cs.ModTimeUsec) - return err -} - -func (s *SqliteDB) Close() error { - return s.db.Close() -} diff --git a/go.mod b/go.mod index 127f817..f32836e 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module blitiri.com.ar/go/summer go 1.19 require ( - github.com/mattn/go-sqlite3 v1.14.17 github.com/pkg/xattr v0.4.9 golang.org/x/term v0.11.0 ) diff --git a/go.sum b/go.sum index 8b38e85..6a3310c 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM= -github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/pkg/xattr v0.4.9 h1:5883YPCtkSd8LFbs13nXplj9g9tlrwoJRjgpgMu1/fE= github.com/pkg/xattr v0.4.9/go.mod h1:di8WF84zAKk8jzR1UBTEWh9AUlIZZ7M/JNt8e9B6ktU= golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/summer.go b/summer.go index 0ca24bc..2ed5c81 100644 --- a/summer.go +++ b/summer.go @@ -19,8 +19,7 @@ const usage = `# summer 🌞 🏖 Utility to detect accidental data corruption (e.g. bitrot, storage media problems). Not intended to detect malicious modification. -Checksums are written to/read from each files' extended attributes by default, -or to a separate database file (with the -db flag). +Checksums are written to/read from each file's extended attributes. Usage: @@ -42,7 +41,6 @@ Flags: // Flags. var ( - dbPath = flag.String("db", "", "database to read from/write to") oneFilesystem = flag.Bool("x", false, "don't cross filesystem boundaries") forceTTY = flag.Bool("forcetty", false, "force TTY output") exclude = &RepeatedStringFlag{} @@ -103,12 +101,6 @@ func main() { } options.db = XattrDB{} - if *dbPath != "" { - options.db, err = OpenSqliteDB(*dbPath, root) - if err != nil { - Fatalf("%q: %v", *dbPath, err) - } - } defer options.db.Close() switch op { diff --git a/test/access.t b/test/access.t index f72a125..ac79351 100644 --- a/test/access.t +++ b/test/access.t @@ -8,13 +8,13 @@ interfere. $ touch root/empty $ echo marola > root/hola - $ summer -db=db.sqlite3 generate root/ + $ summer generate root/ 0s: 0 matched, 0 modified, 2 new, 0 corrupted - $ summer -db=db.sqlite3 verify root/ + $ summer verify root/ 0s: 2 matched, 0 modified, 0 new, 0 corrupted $ chmod 0000 root/empty - $ summer -db=db.sqlite3 verify root/ + $ summer verify root/ 0s: 0 matched, 0 modified, 0 new, 0 corrupted open root/empty: permission denied [1] diff --git a/test/dryrun_sqlite.t b/test/dryrun_sqlite.t deleted file mode 100644 index d03f538..0000000 --- a/test/dryrun_sqlite.t +++ /dev/null @@ -1,36 +0,0 @@ -Tests for dry-run mode when using -db. Similar to the basic ones. - - $ alias summer="$TESTDIR/../summer" - -Generate test data. - - $ touch empty - $ echo marola > hola - -Generate and verify. - - $ summer -n -db=db.sqlite3 generate . - 0s: 0 matched, 0 modified, 3 new, 0 corrupted - - $ summer -n -db=db.sqlite3 verify . - 0s: 0 matched, 0 modified, 3 new, 0 corrupted - - $ summer -n -db=db.sqlite3 verify . - 0s: 0 matched, 0 modified, 3 new, 0 corrupted - -Now write data for real, so we can test modification. - - $ summer -db=db.sqlite3 generate . - 0s: 0 matched, 0 modified, 3 new, 0 corrupted - -Check handling of new and updated files. - - $ sleep 0.005 - $ echo trova > nueva - $ touch empty - $ summer -n -db=db.sqlite3 verify . - 0s: 1 matched, 2 modified, 1 new, 0 corrupted - $ summer -n -db=db.sqlite3 update . - 0s: 1 matched, 2 modified, 1 new, 0 corrupted - $ summer -n -db=db.sqlite3 verify . - 0s: 1 matched, 2 modified, 1 new, 0 corrupted diff --git a/test/help.t b/test/help.t index 5f6d217..81431cd 100644 --- a/test/help.t +++ b/test/help.t @@ -10,8 +10,7 @@ No arguments. Utility to detect accidental data corruption (e.g. bitrot, storage media problems). Not intended to detect malicious modification. - Checksums are written to/read from each files' extended attributes by default, - or to a separate database file (with the -db flag). + Checksums are written to/read from each file's extended attributes. Usage: @@ -29,8 +28,6 @@ No arguments. Print software version information. Flags: - -db string - \tdatabase to read from/write to (esc) -exclude value \texclude these paths (can be repeated) (esc) -excludere value @@ -52,8 +49,7 @@ Too few arguments. Utility to detect accidental data corruption (e.g. bitrot, storage media problems). Not intended to detect malicious modification. - Checksums are written to/read from each files' extended attributes by default, - or to a separate database file (with the -db flag). + Checksums are written to/read from each file's extended attributes. Usage: @@ -71,8 +67,6 @@ Too few arguments. Print software version information. Flags: - -db string - \tdatabase to read from/write to (esc) -exclude value \texclude these paths (can be repeated) (esc) -excludere value @@ -94,8 +88,7 @@ No valid path (the argument is given, but it is empty). Utility to detect accidental data corruption (e.g. bitrot, storage media problems). Not intended to detect malicious modification. - Checksums are written to/read from each files' extended attributes by default, - or to a separate database file (with the -db flag). + Checksums are written to/read from each file's extended attributes. Usage: @@ -113,8 +106,6 @@ No valid path (the argument is given, but it is empty). Print software version information. Flags: - -db string - \tdatabase to read from/write to (esc) -exclude value \texclude these paths (can be repeated) (esc) -excludere value diff --git a/test/sqlite.t b/test/sqlite.t deleted file mode 100644 index 1bf30b1..0000000 --- a/test/sqlite.t +++ /dev/null @@ -1,40 +0,0 @@ - -Basic tests but storing in a sqlite3 database. -This is enough to exercise the backend. - - $ alias summer="$TESTDIR/../summer" - $ touch empty - $ echo marola > hola - - $ summer -db=db.sqlite3 generate . - 0s: 0 matched, 0 modified, 3 new, 0 corrupted - $ summer -db=db.sqlite3 verify . - 0s: 2 matched, 1 modified, 0 new, 0 corrupted - $ summer -db=db.sqlite3 update . - 0s: 2 matched, 1 modified, 0 new, 0 corrupted - -Check that the root path doesn't confuse us. - - $ summer -db=db.sqlite3 -v verify $PWD - ".*/db.sqlite3": file modified \(not corrupted\) \(checksum: \w+ -> \w+, mtime: \d+ -> \d+\) (re) - ".*/empty": match \(checksum:0, mtime:\d+\) (re) - ".*/hola": match \(checksum:\w+, mtime:\d+\) (re) - 0s: 2 matched, 1 modified, 0 new, 0 corrupted - -Force a write error to check it is appropriately handled. - - $ summer "-db=file:db.sqlite3?mode=ro" update . - 0s: 0 matched, 1 modified, 0 new, 0 corrupted - attempt to write a readonly database - [1] - -Check errors when we cannot open the database file. - - $ summer -db=/proc/doesnotexist verify . - "/proc/doesnotexist": unable to open database file: no such file or directory - [1] - - $ summer -db=/dev/null verify . - "/dev/null": attempt to write a readonly database - [1] -