Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: deprecate ioutil as it's already deprecated in Go 1.16 (#2370)

Danny Xu9239233b96cb
diff --git a/plugin/db/mysql/restore.go b/plugin/db/mysql/restore.go
index 926ce990f6..8c760f4f03 100644
--- a/plugin/db/mysql/restore.go
+++ b/plugin/db/mysql/restore.go
@@ -16,7 +16,6 @@ import (
"fmt"
"io"
"io/fs"
- "io/ioutil"
"math"
"os"
"os/exec"
@@ -101,7 +100,7 @@ type binlogFileMeta struct {
FirstEventTs int64 `json:"first_event_ts"`
}
-// ReplayBinlog replays the binlog for `originDatabase` from `startBinlogInfo.Position` to `targetTs`, read binlog from `driver.binlogDir``.
+// ReplayBinlog replays the binlog for `originDatabase` from `startBinlogInfo.Position` to `targetTs`, read binlog from `driver.binlogDir“.
func (driver *Driver) replayBinlog(ctx context.Context, originalDatabase, targetDatabase string, startBinlogInfo api.BinlogInfo, targetTs int64) error {
return driver.replayBinlogReadFromDir(ctx, originalDatabase, targetDatabase, startBinlogInfo, targetTs, driver.binlogDir)
}
@@ -250,7 +249,7 @@ func GetBinlogReplayList(startBinlogInfo api.BinlogInfo, binlogDir string) ([]st
return nil, errors.Wrapf(err, "cannot parse the start binlog file name %q", startBinlogInfo.FileName)
}
- binlogFiles, err := ioutil.ReadDir(binlogDir)
+ binlogFiles, err := os.ReadDir(binlogDir)
if err != nil {
return nil, errors.Wrapf(err, "cannot read binlog directory %s", binlogDir)
}
@@ -260,7 +259,11 @@ func GetBinlogReplayList(startBinlogInfo api.BinlogInfo, binlogDir string) ([]st
if strings.HasSuffix(f.Name(), binlogMetaSuffix) {
continue
}
- binlogFile, err := newBinlogFile(f.Name(), f.Size())
+ fileInfo, err := f.Info()
+ if err != nil {
+ return nil, errors.Wrapf(err, "cannot get file info %s", f.Name())
+ }
+ binlogFile, err := newBinlogFile(f.Name(), fileInfo.Size())
if err != nil {
return nil, err
}
@@ -475,7 +478,7 @@ func databaseExists(ctx context.Context, conn *sql.Conn, database string) (bool,
// GetSortedLocalBinlogFiles returns a sorted BinlogFile list in the given binlog dir.
func (driver *Driver) GetSortedLocalBinlogFiles() ([]BinlogFile, error) {
- binlogFilesInfoLocal, err := ioutil.ReadDir(driver.binlogDir)
+ binlogFilesInfoLocal, err := os.ReadDir(driver.binlogDir)
if err != nil {
return nil, err
}
@@ -485,7 +488,11 @@ func (driver *Driver) GetSortedLocalBinlogFiles() ([]BinlogFile, error) {
if strings.HasSuffix(fileInfo.Name(), binlogMetaSuffix) {
continue
}
- binlogFile, err := newBinlogFile(fileInfo.Name(), fileInfo.Size())
+ fi, err := fileInfo.Info()
+ if err != nil {
+ return nil, errors.Wrapf(err, "cannot get file info %s", fileInfo.Name())
+ }
+ binlogFile, err := newBinlogFile(fileInfo.Name(), fi.Size())
if err != nil {
return nil, err
}
diff --git a/plugin/db/sqlite/sqlite.go b/plugin/db/sqlite/sqlite.go
index 8d721ce03c..5c84ae5476 100644
--- a/plugin/db/sqlite/sqlite.go
+++ b/plugin/db/sqlite/sqlite.go
@@ -4,7 +4,7 @@ import (
"context"
"database/sql"
"fmt"
- "io/ioutil"
+ "os"
"path"
"strings"
@@ -94,7 +94,7 @@ func (driver *Driver) getVersion(ctx context.Context) (string, error) {
}
func (driver *Driver) getDatabases() ([]string, error) {
- files, err := ioutil.ReadDir(driver.dir)
+ files, err := os.ReadDir(driver.dir)
if err != nil {
return nil, errors.Wrapf(err, "failed to read directory %q", driver.dir)
}
diff --git a/server/backup_runner.go b/server/backup_runner.go
index 5395c729b4..f69f00948b 100644
--- a/server/backup_runner.go
+++ b/server/backup_runner.go
@@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
- "io/ioutil"
"math"
"net/http"
"os"
@@ -150,7 +149,7 @@ func (r *BackupRunner) getMaxRetentionPeriodTsForMySQLInstance(ctx context.Conte
// TODO(dragonly): Remove metadata as well.
func (r *BackupRunner) purgeBinlogFiles(instanceID, retentionPeriodTs int) error {
binlogDir := getBinlogAbsDir(r.server.profile.DataDir, instanceID)
- binlogFileInfoList, err := ioutil.ReadDir(binlogDir)
+ binlogFileInfoList, err := os.ReadDir(binlogDir)
if err != nil {
return errors.Wrapf(err, "failed to read backup directory %q", binlogDir)
}
@@ -161,7 +160,12 @@ func (r *BackupRunner) purgeBinlogFiles(instanceID, retentionPeriodTs int) error
// We use modification time of local binlog files which is later than the modification time of that on the MySQL server,
// which in turn is later than the last event timestamp of the binlog file.
// This is not accurate and gives about 10 minutes (backup runner interval) more retention time to the binlog files, which is acceptable.
- expireTime := binlogFileInfo.ModTime().Add(time.Duration(retentionPeriodTs) * time.Second)
+ fileInfo, err := binlogFileInfo.Info()
+ if err != nil {
+ log.Warn("Failed to get file info.", zap.String("path", binlogFileInfo.Name()), zap.Error(err))
+ continue
+ }
+ expireTime := fileInfo.ModTime().Add(time.Duration(retentionPeriodTs) * time.Second)
if time.Now().After(expireTime) {
binlogFilePath := path.Join(binlogDir, binlogFileInfo.Name())
if err := os.Remove(binlogFilePath); err != nil {
diff --git a/tests/mysql_test.go b/tests/mysql_test.go
index 1e5d9133b2..6dc51a7e77 100644
--- a/tests/mysql_test.go
+++ b/tests/mysql_test.go
@@ -3,7 +3,6 @@ package tests
import (
"context"
"fmt"
- "io/ioutil"
"math/rand"
"os"
"path/filepath"
@@ -178,7 +177,7 @@ func TestFetchBinlogFiles(t *testing.T) {
a.NoError(err)
t.Log("Download binlog files in empty dir")
- binlogFilesBefore, err := ioutil.ReadDir(binlogDir)
+ binlogFilesBefore, err := os.ReadDir(binlogDir)
a.NoError(err)
for _, file := range binlogFilesBefore {
path := filepath.Join(binlogDir, file.Name())
@@ -198,7 +197,7 @@ func TestFetchBinlogFiles(t *testing.T) {
t.Log("Truncate or delete downloaded files and re-download")
rand.Seed(time.Now().Unix())
// Fetch and randomly truncate/delete some binlog files.t.Log("Clean up binlog dir")
- binlogFiles, err := ioutil.ReadDir(binlogDir)
+ binlogFiles, err := os.ReadDir(binlogDir)
a.NoError(err)
for _, file := range binlogFiles {
path := filepath.Join(binlogDir, file.Name())