tree-wide: rewrite ioutil functions to their replacements
The ioutil package has been deprecated in Go 1.16 [1]. This CL removes
all our own users of that package and rewrites them to use their
replacements in the os package. I initially wanted to do this with a
gofix but because all replacements were signature-compatible I just
did it with a few string replaces and then ran goimports to fix up the
imports.
I intentionally didn't rewrite the patches as that would require a
different process and is IMO of less value.
[1] https://github.com/golang/go/issues/42026
Change-Id: Iac6663a1f1ee49f9b1c6e4b3d97e73f2c3b54a13
Reviewed-on: https://review.monogon.dev/c/monogon/+/449
Reviewed-by: Sergiusz Bazanski <serge@monogon.tech>
diff --git a/metropolis/node/core/cluster/cluster.go b/metropolis/node/core/cluster/cluster.go
index aa293b0..60d7e15 100644
--- a/metropolis/node/core/cluster/cluster.go
+++ b/metropolis/node/core/cluster/cluster.go
@@ -29,7 +29,7 @@
"context"
"errors"
"fmt"
- "io/ioutil"
+ "os"
"sync"
"google.golang.org/protobuf/proto"
@@ -129,7 +129,7 @@
}
func (m *Manager) nodeParamsFWCFG(ctx context.Context) (*apb.NodeParameters, error) {
- bytes, err := ioutil.ReadFile("/sys/firmware/qemu_fw_cfg/by_name/dev.monogon.metropolis/parameters.pb/raw")
+ bytes, err := os.ReadFile("/sys/firmware/qemu_fw_cfg/by_name/dev.monogon.metropolis/parameters.pb/raw")
if err != nil {
return nil, fmt.Errorf("could not read firmware enrolment file: %w", err)
}
diff --git a/metropolis/node/core/consensus/consensus_test.go b/metropolis/node/core/consensus/consensus_test.go
index 105b8eb..16d6f76 100644
--- a/metropolis/node/core/consensus/consensus_test.go
+++ b/metropolis/node/core/consensus/consensus_test.go
@@ -20,7 +20,6 @@
"bytes"
"context"
"crypto/x509"
- "io/ioutil"
"os"
"testing"
"time"
@@ -44,7 +43,7 @@
// Force usage of /tmp as temp directory root, otherwsie TMPDIR from Bazel
// returns a path long enough that socket binds in the localstorage fail
// (as socket names are limited to 108 characters).
- tmp, err := ioutil.TempDir("/tmp", "metropolis-consensus-test")
+ tmp, err := os.MkdirTemp("/tmp", "metropolis-consensus-test")
if err != nil {
t.Fatal(err)
}
diff --git a/metropolis/node/core/curator/curator_test.go b/metropolis/node/core/curator/curator_test.go
index 5ce0077..5529e5d 100644
--- a/metropolis/node/core/curator/curator_test.go
+++ b/metropolis/node/core/curator/curator_test.go
@@ -3,7 +3,6 @@
import (
"context"
"fmt"
- "io/ioutil"
"os"
"testing"
"time"
@@ -79,7 +78,7 @@
// Create ephemeral directory for curator and place it into /tmp.
dir := localstorage.EphemeralCuratorDirectory{}
- tmp, err := ioutil.TempDir("/tmp", "curator-test-*")
+ tmp, err := os.MkdirTemp("/tmp", "curator-test-*")
if err != nil {
t.Fatalf("TempDir: %v", err)
}
diff --git a/metropolis/node/core/curator/listener_test.go b/metropolis/node/core/curator/listener_test.go
index fad7e92..7c1744e 100644
--- a/metropolis/node/core/curator/listener_test.go
+++ b/metropolis/node/core/curator/listener_test.go
@@ -3,7 +3,7 @@
import (
"context"
"errors"
- "io/ioutil"
+ "os"
"testing"
"google.golang.org/grpc/codes"
@@ -29,7 +29,7 @@
// Force usage of /tmp as temp directory root, otherwsie TMPDIR from Bazel
// returns a path long enough that socket binds in the localstorage fail
// (as socket names are limited to 108 characters).
- tmp, err := ioutil.TempDir("/tmp", "curator-test-*")
+ tmp, err := os.MkdirTemp("/tmp", "curator-test-*")
if err != nil {
t.Fatalf("TempDir: %v", err)
}
diff --git a/metropolis/node/core/debug_service.go b/metropolis/node/core/debug_service.go
index 814da9a..48a4c9b 100644
--- a/metropolis/node/core/debug_service.go
+++ b/metropolis/node/core/debug_service.go
@@ -20,7 +20,6 @@
"bufio"
"context"
"fmt"
- "io/ioutil"
"os"
"regexp"
"strings"
@@ -215,7 +214,7 @@
if !safeTracingPropertyNamesRe.MatchString(name) {
return fmt.Errorf("disallowed tracing property name received: \"%v\"", name)
}
- return ioutil.WriteFile("/sys/kernel/tracing/"+name, []byte(value+"\n"), 0)
+ return os.WriteFile("/sys/kernel/tracing/"+name, []byte(value+"\n"), 0)
}
func (s *debugService) Trace(req *apb.TraceRequest, srv apb.NodeDebugService_TraceServer) error {
diff --git a/metropolis/node/core/localstorage/crypt/blockdev.go b/metropolis/node/core/localstorage/crypt/blockdev.go
index 6ea1b49..dde2fd9 100644
--- a/metropolis/node/core/localstorage/crypt/blockdev.go
+++ b/metropolis/node/core/localstorage/crypt/blockdev.go
@@ -19,7 +19,6 @@
import (
"context"
"fmt"
- "io/ioutil"
"os"
"path/filepath"
"strconv"
@@ -51,7 +50,7 @@
// to ESPDevicePath and NodeDataCryptPath respectively. This doesn't fail if it
// doesn't find the partitions, only if something goes catastrophically wrong.
func MakeBlockDevices(ctx context.Context) error {
- blockdevNames, err := ioutil.ReadDir("/sys/class/block")
+ blockdevNames, err := os.ReadDir("/sys/class/block")
if err != nil {
return fmt.Errorf("failed to read sysfs block class: %w", err)
}
diff --git a/metropolis/node/core/localstorage/declarative/placement_local.go b/metropolis/node/core/localstorage/declarative/placement_local.go
index 43921cd..f12f654 100644
--- a/metropolis/node/core/localstorage/declarative/placement_local.go
+++ b/metropolis/node/core/localstorage/declarative/placement_local.go
@@ -18,7 +18,6 @@
import (
"fmt"
- "io/ioutil"
"os"
"sync"
@@ -58,7 +57,7 @@
}
func (f *FSPlacement) Read() ([]byte, error) {
- return ioutil.ReadFile(f.FullPath())
+ return os.ReadFile(f.FullPath())
}
// Write performs an atomic file write, via a temporary file.
@@ -69,7 +68,7 @@
// TODO(q3k): ensure that these do not collide with an existing sibling file, or generate this suffix randomly.
tmp := f.FullPath() + ".__metropolis_tmp"
defer os.Remove(tmp)
- if err := ioutil.WriteFile(tmp, d, mode); err != nil {
+ if err := os.WriteFile(tmp, d, mode); err != nil {
return fmt.Errorf("temporary file write failed: %w", err)
}
diff --git a/metropolis/node/core/localstorage/storage.go b/metropolis/node/core/localstorage/storage.go
index 1e06967..d067438 100644
--- a/metropolis/node/core/localstorage/storage.go
+++ b/metropolis/node/core/localstorage/storage.go
@@ -53,7 +53,7 @@
// Ephemeral data, used by runtime, stored in tmpfs. Things like sockets,
// temporary config files, etc.
Ephemeral EphemeralDirectory `dir:"ephemeral"`
- // FHS-standard /tmp directory, used by ioutil.TempFile.
+ // FHS-standard /tmp directory, used by os.MkdirTemp.
Tmp TmpDirectory `dir:"tmp"`
// FHS-standard /run directory. Used by various services.
Run RunDirectory `dir:"run"`
diff --git a/metropolis/node/core/mounts.go b/metropolis/node/core/mounts.go
index bb98462..a54331d 100644
--- a/metropolis/node/core/mounts.go
+++ b/metropolis/node/core/mounts.go
@@ -18,7 +18,6 @@
import (
"fmt"
- "io/ioutil"
"os"
"strings"
@@ -57,7 +56,7 @@
if err := unix.Mount("tmpfs", "/sys/fs/cgroup", "tmpfs", unix.MS_NOEXEC|unix.MS_NOSUID|unix.MS_NODEV, ""); err != nil {
panic(err)
}
- cgroupsRaw, err := ioutil.ReadFile("/proc/cgroups")
+ cgroupsRaw, err := os.ReadFile("/proc/cgroups")
if err != nil {
panic(err)
}