Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
davissp14 committed Jun 23, 2024
1 parent 2048d1b commit 4eb76da
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
32 changes: 13 additions & 19 deletions internal/flypg/collation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package flypg

import (
"context"
"crypto/md5"
"crypto/sha256"
"database/sql"
"encoding/hex"
"fmt"
"log"
"os"
Expand All @@ -15,7 +16,7 @@ import (

const collationVersionFile = "/data/.collationVersion"

func collationSumChanged(sum [16]byte) (bool, error) {
func collationHashChanged(versionHash string) (bool, error) {
// Short-circuit if there's no collation file.
_, err := os.Stat(collationVersionFile)
switch {
Expand All @@ -25,36 +26,29 @@ func collationSumChanged(sum [16]byte) (bool, error) {
return false, fmt.Errorf("failed to stat collation lock file: %w", err)
}

// Read the collation lock file.
body, err := os.ReadFile(collationVersionFile)
// Read the collation version file.
oldVersionHash, err := os.ReadFile(collationVersionFile)
if err != nil {
return false, fmt.Errorf("failed to read collation lock file: %w", err)
}

// Compare the md5sum of the ldd version with version in the collation version file.
return !slices.Equal(sum[:], body), nil
return versionHash == string(oldVersionHash), nil
}

func collationVersionFileExists() bool {
_, err := os.Stat(collationVersionFile)
return !os.IsNotExist(err)
}

func calculateLocaleVersionSum() ([16]byte, error) {
// md5sum the ldd version so we can verify if the system has changed.
func calculateLocaleVersionHash() (string, error) {
output, err := utils.RunCommand("locale --version", "postgres")
if err != nil {
return [16]byte{}, fmt.Errorf("failed to capture ldd version: %w", err)
return "", fmt.Errorf("failed to capture ldd version: %w", err)
}

// Calculate the md5sum of the ldd version output
return md5.Sum(output), nil
hash := sha256.Sum256(output)
return hex.EncodeToString(hash[:]), nil
}

func writeCollationVersionFile(sum [16]byte) error {
func writeCollationVersionFile(versionHash string) error {
// Write the collation lock file.
if err := os.WriteFile(collationVersionFile, sum[:], 0600); err != nil {
return fmt.Errorf("failed to write collation lock file: %w", err)
if err := os.WriteFile(collationVersionFile, []byte(versionHash), 0600); err != nil {
return fmt.Errorf("failed to write collation version file: %w", err)
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions internal/flypg/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,13 +481,13 @@ func setDirOwnership() error {

func (n *Node) evaluateCollationIntegrity(ctx context.Context, conn *pgx.Conn) error {
// Calculate the sum of the locale version.
sum, err := calculateLocaleVersionSum()
versionHash, err := calculateLocaleVersionHash()
if err != nil {
return fmt.Errorf("failed to calculate collation sum: %w", err)
}

// Check to see if the version has changed
changed, err := collationSumChanged(sum)
changed, err := collationHashChanged(versionHash)
if err != nil {
return fmt.Errorf("failed to check collation version file: %s", err)
}
Expand Down Expand Up @@ -558,7 +558,7 @@ func (n *Node) evaluateCollationIntegrity(ctx context.Context, conn *pgx.Conn) e
// No collation issues found, we can safely update the version file.
// This will prevent the system from re-evaluating the collation integrity on every boot.
log.Printf("[INFO] No collation mismatches detected. Updating collation version file.\n")
if err := writeCollationVersionFile(sum); err != nil {
if err := writeCollationVersionFile(versionHash); err != nil {
return fmt.Errorf("failed to write collation lock: %s", err)
}

Expand Down

0 comments on commit 4eb76da

Please sign in to comment.