Enable staticcheck, fix issues (#542)

This commit is contained in:
Toni Spets
2023-12-15 22:07:22 +02:00
committed by GitHub
parent 1a96ad4389
commit 6824eb61a8
8 changed files with 29 additions and 35 deletions

View File

@@ -17,4 +17,4 @@ repos:
- "github.com/benbjohnson/litestrem" - "github.com/benbjohnson/litestrem"
- "-w" - "-w"
- id: go-vet-repo-mod - id: go-vet-repo-mod
# - id: go-staticcheck-repo-mod - id: go-staticcheck-repo-mod

View File

@@ -5,7 +5,6 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"log/slog" "log/slog"
"net/url" "net/url"
"os" "os"
@@ -223,7 +222,7 @@ func ReadConfigFile(filename string, expandEnv bool) (_ Config, err error) {
} }
// Read configuration. // Read configuration.
buf, err := ioutil.ReadFile(filename) buf, err := os.ReadFile(filename)
if os.IsNotExist(err) { if os.IsNotExist(err) {
return config, fmt.Errorf("config file not found: %s", filename) return config, fmt.Errorf("config file not found: %s", filename)
} else if err != nil { } else if err != nil {

View File

@@ -1,7 +1,6 @@
package main_test package main_test
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@@ -16,7 +15,7 @@ func TestReadConfigFile(t *testing.T) {
// Ensure global AWS settings are propagated down to replica configurations. // Ensure global AWS settings are propagated down to replica configurations.
t.Run("PropagateGlobalSettings", func(t *testing.T) { t.Run("PropagateGlobalSettings", func(t *testing.T) {
filename := filepath.Join(t.TempDir(), "litestream.yml") filename := filepath.Join(t.TempDir(), "litestream.yml")
if err := ioutil.WriteFile(filename, []byte(` if err := os.WriteFile(filename, []byte(`
access-key-id: XXX access-key-id: XXX
secret-access-key: YYY secret-access-key: YYY
@@ -48,7 +47,7 @@ dbs:
os.Setenv("LITESTREAM_TEST_1872363", "s3://foo/bar") os.Setenv("LITESTREAM_TEST_1872363", "s3://foo/bar")
filename := filepath.Join(t.TempDir(), "litestream.yml") filename := filepath.Join(t.TempDir(), "litestream.yml")
if err := ioutil.WriteFile(filename, []byte(` if err := os.WriteFile(filename, []byte(`
dbs: dbs:
- path: $LITESTREAM_TEST_0129380 - path: $LITESTREAM_TEST_0129380
replicas: replicas:
@@ -75,7 +74,7 @@ dbs:
os.Setenv("LITESTREAM_TEST_9847533", "s3://foo/bar") os.Setenv("LITESTREAM_TEST_9847533", "s3://foo/bar")
filename := filepath.Join(t.TempDir(), "litestream.yml") filename := filepath.Join(t.TempDir(), "litestream.yml")
if err := ioutil.WriteFile(filename, []byte(` if err := os.WriteFile(filename, []byte(`
dbs: dbs:
- path: /path/to/db - path: /path/to/db
replicas: replicas:

20
db.go
View File

@@ -10,7 +10,6 @@ import (
"fmt" "fmt"
"hash/crc64" "hash/crc64"
"io" "io"
"io/ioutil"
"log/slog" "log/slog"
"math" "math"
"math/rand" "math/rand"
@@ -207,7 +206,7 @@ func (db *DB) CurrentShadowWALPath(generation string) (string, error) {
// CurrentShadowWALIndex returns the current WAL index & total size. // CurrentShadowWALIndex returns the current WAL index & total size.
func (db *DB) CurrentShadowWALIndex(generation string) (index int, size int64, err error) { func (db *DB) CurrentShadowWALIndex(generation string) (index int, size int64, err error) {
fis, err := ioutil.ReadDir(filepath.Join(db.GenerationPath(generation), "wal")) des, err := os.ReadDir(filepath.Join(db.GenerationPath(generation), "wal"))
if os.IsNotExist(err) { if os.IsNotExist(err) {
return 0, 0, nil // no wal files written for generation return 0, 0, nil // no wal files written for generation
} else if err != nil { } else if err != nil {
@@ -215,7 +214,12 @@ func (db *DB) CurrentShadowWALIndex(generation string) (index int, size int64, e
} }
// Find highest wal index. // Find highest wal index.
for _, fi := range fis { for _, de := range des {
fi, err := de.Info()
if err != nil {
return 0, 0, err
}
if v, err := ParseWALPath(fi.Name()); err != nil { if v, err := ParseWALPath(fi.Name()); err != nil {
continue // invalid wal filename continue // invalid wal filename
} else if v > index { } else if v > index {
@@ -546,7 +550,7 @@ func (db *DB) cleanGenerations() error {
} }
dir := filepath.Join(db.metaPath, "generations") dir := filepath.Join(db.metaPath, "generations")
fis, err := ioutil.ReadDir(dir) fis, err := os.ReadDir(dir)
if os.IsNotExist(err) { if os.IsNotExist(err) {
return nil return nil
} else if err != nil { } else if err != nil {
@@ -593,7 +597,7 @@ func (db *DB) cleanWAL() error {
// Remove all WAL files for the generation before the lowest index. // Remove all WAL files for the generation before the lowest index.
dir := db.ShadowWALDir(generation) dir := db.ShadowWALDir(generation)
fis, err := ioutil.ReadDir(dir) fis, err := os.ReadDir(dir)
if os.IsNotExist(err) { if os.IsNotExist(err) {
return nil return nil
} else if err != nil { } else if err != nil {
@@ -649,7 +653,7 @@ func (db *DB) releaseReadLock() error {
// CurrentGeneration returns the name of the generation saved to the "generation" // CurrentGeneration returns the name of the generation saved to the "generation"
// file in the meta data directory. Returns empty string if none exists. // file in the meta data directory. Returns empty string if none exists.
func (db *DB) CurrentGeneration() (string, error) { func (db *DB) CurrentGeneration() (string, error) {
buf, err := ioutil.ReadFile(db.GenerationNamePath()) buf, err := os.ReadFile(db.GenerationNamePath())
if os.IsNotExist(err) { if os.IsNotExist(err) {
return "", nil return "", nil
} else if err != nil { } else if err != nil {
@@ -691,7 +695,7 @@ func (db *DB) createGeneration() (string, error) {
if db.fileInfo != nil { if db.fileInfo != nil {
mode = db.fileInfo.Mode() mode = db.fileInfo.Mode()
} }
if err := ioutil.WriteFile(generationNamePath+".tmp", []byte(generation+"\n"), mode); err != nil { if err := os.WriteFile(generationNamePath+".tmp", []byte(generation+"\n"), mode); err != nil {
return "", fmt.Errorf("write generation temp file: %w", err) return "", fmt.Errorf("write generation temp file: %w", err)
} }
uid, gid := internal.Fileinfo(db.fileInfo) uid, gid := internal.Fileinfo(db.fileInfo)
@@ -986,7 +990,7 @@ func (db *DB) initShadowWALFile(filename string) (int64, error) {
} }
if err := internal.MkdirAll(filepath.Dir(filename), db.dirInfo); err != nil { if err := internal.MkdirAll(filepath.Dir(filename), db.dirInfo); err != nil {
return 0, err return 0, err
} else if err := ioutil.WriteFile(filename, hdr, mode); err != nil { } else if err := os.WriteFile(filename, hdr, mode); err != nil {
return 0, err return 0, err
} }
uid, gid := internal.Fileinfo(db.fileInfo) uid, gid := internal.Fileinfo(db.fileInfo)

View File

@@ -3,7 +3,6 @@ package litestream_test
import ( import (
"context" "context"
"database/sql" "database/sql"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@@ -374,9 +373,9 @@ func TestDB_Sync(t *testing.T) {
// Read existing file, update header checksum, and write back only header // Read existing file, update header checksum, and write back only header
// to simulate a header with a mismatched checksum. // to simulate a header with a mismatched checksum.
shadowWALPath := db.ShadowWALPath(pos0.Generation, pos0.Index) shadowWALPath := db.ShadowWALPath(pos0.Generation, pos0.Index)
if buf, err := ioutil.ReadFile(shadowWALPath); err != nil { if buf, err := os.ReadFile(shadowWALPath); err != nil {
t.Fatal(err) t.Fatal(err)
} else if err := ioutil.WriteFile(shadowWALPath, append(buf[:litestream.WALHeaderSize-8], 0, 0, 0, 0, 0, 0, 0, 0), 0600); err != nil { } else if err := os.WriteFile(shadowWALPath, append(buf[:litestream.WALHeaderSize-8], 0, 0, 0, 0, 0, 0, 0, 0), 0600); err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
@@ -112,7 +111,7 @@ func (c *ReplicaClient) Generations(ctx context.Context) ([]string, error) {
return nil, fmt.Errorf("cannot determine generations path: %w", err) return nil, fmt.Errorf("cannot determine generations path: %w", err)
} }
fis, err := ioutil.ReadDir(root) fis, err := os.ReadDir(root)
if os.IsNotExist(err) { if os.IsNotExist(err) {
return nil, nil return nil, nil
} else if err != nil { } else if err != nil {

View File

@@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"hash/crc64" "hash/crc64"
"io" "io"
"io/ioutil"
"log/slog" "log/slog"
"math" "math"
"os" "os"
@@ -380,7 +379,7 @@ func (r *Replica) calcPos(ctx context.Context, generation string) (pos Pos, err
rd = io.NopCloser(drd) rd = io.NopCloser(drd)
} }
n, err := io.Copy(ioutil.Discard, lz4.NewReader(rd)) n, err := io.Copy(io.Discard, lz4.NewReader(rd))
if err != nil { if err != nil {
return pos, err return pos, err
} }
@@ -812,7 +811,7 @@ func (r *Replica) Validate(ctx context.Context) error {
db := r.DB() db := r.DB()
// Restore replica to a temporary directory. // Restore replica to a temporary directory.
tmpdir, err := ioutil.TempDir("", "*-litestream") tmpdir, err := os.MkdirTemp("", "*-litestream")
if err != nil { if err != nil {
return err return err
} }
@@ -1086,7 +1085,7 @@ func (r *Replica) Restore(ctx context.Context, opt RestoreOptions) (err error) {
} }
// Ensure that we found the specific index, if one was specified. // Ensure that we found the specific index, if one was specified.
if opt.Index != math.MaxInt32 && opt.Index != opt.Index { if opt.Index != math.MaxInt32 && opt.Index != maxWALIndex {
return fmt.Errorf("unable to locate index %d in generation %q, highest index was %d", opt.Index, opt.Generation, maxWALIndex) return fmt.Errorf("unable to locate index %d in generation %q, highest index was %d", opt.Index, opt.Generation, maxWALIndex)
} }
@@ -1266,7 +1265,7 @@ func (r *Replica) SnapshotIndexByIndex(ctx context.Context, generation string, i
} }
// Use snapshot if it newer. // Use snapshot if it newer.
if snapshotIndex == -1 || snapshotIndex >= snapshotIndex { if snapshotIndex == -1 || snapshot.Index >= snapshotIndex {
snapshotIndex = snapshot.Index snapshotIndex = snapshot.Index
} }
} }

View File

@@ -4,7 +4,7 @@ import (
"context" "context"
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io"
"math/rand" "math/rand"
"os" "os"
"path" "path"
@@ -12,7 +12,6 @@ import (
"sort" "sort"
"strings" "strings"
"testing" "testing"
"time"
"github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream"
"github.com/benbjohnson/litestream/abs" "github.com/benbjohnson/litestream/abs"
@@ -22,10 +21,6 @@ import (
"github.com/benbjohnson/litestream/sftp" "github.com/benbjohnson/litestream/sftp"
) )
func init() {
rand.Seed(time.Now().UnixNano())
}
var ( var (
// Enables integration tests. // Enables integration tests.
integration = flag.String("integration", "file", "") integration = flag.String("integration", "file", "")
@@ -193,7 +188,7 @@ func TestReplicaClient_WriteSnapshot(t *testing.T) {
if r, err := c.SnapshotReader(context.Background(), "b16ddcf5c697540f", 1000); err != nil { if r, err := c.SnapshotReader(context.Background(), "b16ddcf5c697540f", 1000); err != nil {
t.Fatal(err) t.Fatal(err)
} else if buf, err := ioutil.ReadAll(r); err != nil { } else if buf, err := io.ReadAll(r); err != nil {
t.Fatal(err) t.Fatal(err)
} else if err := r.Close(); err != nil { } else if err := r.Close(); err != nil {
t.Fatal(err) t.Fatal(err)
@@ -224,7 +219,7 @@ func TestReplicaClient_SnapshotReader(t *testing.T) {
} }
defer r.Close() defer r.Close()
if buf, err := ioutil.ReadAll(r); err != nil { if buf, err := io.ReadAll(r); err != nil {
t.Fatal(err) t.Fatal(err)
} else if got, want := string(buf), "foo"; got != want { } else if got, want := string(buf), "foo"; got != want {
t.Fatalf("ReadAll=%v, want %v", got, want) t.Fatalf("ReadAll=%v, want %v", got, want)
@@ -378,7 +373,7 @@ func TestReplicaClient_WriteWALSegment(t *testing.T) {
if r, err := c.WALSegmentReader(context.Background(), litestream.Pos{Generation: "b16ddcf5c697540f", Index: 1000, Offset: 2000}); err != nil { if r, err := c.WALSegmentReader(context.Background(), litestream.Pos{Generation: "b16ddcf5c697540f", Index: 1000, Offset: 2000}); err != nil {
t.Fatal(err) t.Fatal(err)
} else if buf, err := ioutil.ReadAll(r); err != nil { } else if buf, err := io.ReadAll(r); err != nil {
t.Fatal(err) t.Fatal(err)
} else if err := r.Close(); err != nil { } else if err := r.Close(); err != nil {
t.Fatal(err) t.Fatal(err)
@@ -409,7 +404,7 @@ func TestReplicaClient_WALReader(t *testing.T) {
} }
defer r.Close() defer r.Close()
if buf, err := ioutil.ReadAll(r); err != nil { if buf, err := io.ReadAll(r); err != nil {
t.Fatal(err) t.Fatal(err)
} else if got, want := string(buf), "foobar"; got != want { } else if got, want := string(buf), "foobar"; got != want {
t.Fatalf("ReadAll=%v, want %v", got, want) t.Fatalf("ReadAll=%v, want %v", got, want)