Fix checksum hex padding
This commit is contained in:
28
db.go
28
db.go
@@ -1324,6 +1324,13 @@ func (db *DB) Restore(ctx context.Context, opt RestoreOptions) error {
|
||||
if err := db.restoreSnapshot(ctx, r, pos.Generation, pos.Index, tmpPath); err != nil {
|
||||
return fmt.Errorf("cannot restore snapshot: %w", err)
|
||||
}
|
||||
|
||||
chksum, err := checksumFile(tmpPath)
|
||||
if err != nil {
|
||||
log.Printf("%s(%s): cannot checksum snapshot: %s", db.path, r.Name(), err)
|
||||
} else {
|
||||
log.Printf("%s(%s): restored snapshot checksum: %016x", db.path, r.Name(), chksum)
|
||||
}
|
||||
}
|
||||
|
||||
// Restore each WAL file until we reach our maximum index.
|
||||
@@ -1352,6 +1359,20 @@ func (db *DB) Restore(ctx context.Context, opt RestoreOptions) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func checksumFile(filename string) (uint64, error) {
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
h := crc64.New(crc64.MakeTable(crc64.ISO))
|
||||
if _, err := io.Copy(h, f); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return h.Sum64(), nil
|
||||
}
|
||||
|
||||
func (db *DB) restoreTarget(ctx context.Context, opt RestoreOptions, logger *log.Logger) (Replica, string, error) {
|
||||
var target struct {
|
||||
replica Replica
|
||||
@@ -1460,6 +1481,13 @@ func (db *DB) restoreWAL(ctx context.Context, r Replica, generation string, inde
|
||||
return err
|
||||
}
|
||||
|
||||
chksum, err := checksumFile(dbPath + "-wal")
|
||||
if err != nil {
|
||||
log.Printf("%s(%s): cannot checksum wal: %s", db.path, r.Name(), err)
|
||||
} else {
|
||||
log.Printf("%s(%s): restored wal checksum: %016x", db.path, r.Name(), chksum)
|
||||
}
|
||||
|
||||
// Open SQLite database and force a truncating checkpoint.
|
||||
d, err := sql.Open("sqlite3", dbPath)
|
||||
if err != nil {
|
||||
|
||||
@@ -98,7 +98,7 @@ func (p Pos) String() string {
|
||||
if p.IsZero() {
|
||||
return "<>"
|
||||
}
|
||||
return fmt.Sprintf("<%s,%d,%d>", p.Generation, p.Index, p.Offset)
|
||||
return fmt.Sprintf("<%s,%08x,%d>", p.Generation, p.Index, p.Offset)
|
||||
}
|
||||
|
||||
// IsZero returns true if p is the zero value.
|
||||
|
||||
@@ -996,7 +996,7 @@ func ValidateReplica(ctx context.Context, r Replica) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot compute checksum: %w", err)
|
||||
}
|
||||
log.Printf("%s(%s): primary checksum computed: %08x @ %s", db.Path(), r.Name(), chksum0, pos)
|
||||
log.Printf("%s(%s): primary checksum computed: %016x @ %s", db.Path(), r.Name(), chksum0, pos)
|
||||
|
||||
// Wait until replica catches up to position.
|
||||
log.Printf("%s(%s): waiting for replica", db.Path(), r.Name())
|
||||
@@ -1039,7 +1039,7 @@ func ValidateReplica(ctx context.Context, r Replica) error {
|
||||
}
|
||||
chksum1 := h.Sum64()
|
||||
|
||||
log.Printf("%s(%s): replica checksum computed: %08x", db.Path(), r.Name(), chksum1)
|
||||
log.Printf("%s(%s): replica checksum computed: %016x", db.Path(), r.Name(), chksum1)
|
||||
|
||||
// Validate checksums match.
|
||||
if chksum0 != chksum1 {
|
||||
|
||||
Reference in New Issue
Block a user