From 412b4242c2110c419185a5d05fea38cfc2b009c5 Mon Sep 17 00:00:00 2001 From: Daniel <dhaavi@users.noreply.github.com> Date: Thu, 29 Sep 2022 10:55:01 +0200 Subject: [PATCH] FIx linter errors --- api/endpoints.go | 3 +-- api/main_test.go | 3 +-- config/persistence.go | 6 +++--- database/database_test.go | 3 +-- database/registry.go | 5 ++--- database/storage/badger/badger_test.go | 3 +-- database/storage/bbolt/bbolt_test.go | 3 +-- database/storage/fstree/fstree.go | 5 ++--- formats/dsd/http.go | 5 ++--- metrics/api.go | 3 +-- modules/subsystems/subsystems_test.go | 3 +-- template/module_test.go | 3 +-- updater/fetch.go | 5 ++--- updater/registry_test.go | 3 +-- updater/storage_test.go | 4 ++-- updater/updating.go | 6 +++--- utils/renameio/symlink_test.go | 7 +++---- utils/renameio/tempfile.go | 13 ++++++------- utils/renameio/tempfile_linux_test.go | 5 ++--- utils/renameio/writefile_test.go | 5 ++--- utils/structure_test.go | 3 +-- 21 files changed, 39 insertions(+), 57 deletions(-) diff --git a/api/endpoints.go b/api/endpoints.go index 265df69..37a8b45 100644 --- a/api/endpoints.go +++ b/api/endpoints.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/http" "sort" "strconv" @@ -501,7 +500,7 @@ func readBody(w http.ResponseWriter, r *http.Request) (inputData []byte, ok bool } // Read and close body. - inputData, err := ioutil.ReadAll(r.Body) + inputData, err := io.ReadAll(r.Body) if err != nil { http.Error(w, "failed to read body"+err.Error(), http.StatusInternalServerError) return nil, false diff --git a/api/main_test.go b/api/main_test.go index 078f4b6..4bbc5a0 100644 --- a/api/main_test.go +++ b/api/main_test.go @@ -2,7 +2,6 @@ package api import ( "fmt" - "io/ioutil" "os" "testing" @@ -21,7 +20,7 @@ func TestMain(m *testing.M) { module.Enable() // tmp dir for data root (db & config) - tmpDir, err := ioutil.TempDir("", "portbase-testing-") + tmpDir, err := os.MkdirTemp("", "portbase-testing-") if err != nil { fmt.Fprintf(os.Stderr, "failed to create tmp dir: %s\n", err) os.Exit(1) diff --git a/config/persistence.go b/config/persistence.go index 0b5d40e..c88c83c 100644 --- a/config/persistence.go +++ b/config/persistence.go @@ -3,7 +3,7 @@ package config import ( "encoding/json" "fmt" - "io/ioutil" + "os" "path" "strings" "sync" @@ -34,7 +34,7 @@ func loadConfig(requireValidConfig bool) error { } // read config file - data, err := ioutil.ReadFile(configFilePath) + data, err := os.ReadFile(configFilePath) if err != nil { return err } @@ -93,7 +93,7 @@ func saveConfig() error { } // write file - return ioutil.WriteFile(configFilePath, data, 0o0600) + return os.WriteFile(configFilePath, data, 0o0600) } // JSONToMap parses and flattens a hierarchical json object. diff --git a/database/database_test.go b/database/database_test.go index 33cb2df..bedb4c6 100644 --- a/database/database_test.go +++ b/database/database_test.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "io/ioutil" "log" "os" "reflect" @@ -22,7 +21,7 @@ import ( ) func TestMain(m *testing.M) { - testDir, err := ioutil.TempDir("", "portbase-database-testing-") + testDir, err := os.MkdirTemp("", "portbase-database-testing-") if err != nil { panic(err) } diff --git a/database/registry.go b/database/registry.go index 7c68b59..2694c14 100644 --- a/database/registry.go +++ b/database/registry.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" "os" "path" "regexp" @@ -115,7 +114,7 @@ func loadRegistry() error { // read file filePath := path.Join(rootStructure.Path, registryFileName) - data, err := ioutil.ReadFile(filePath) + data, err := os.ReadFile(filePath) if err != nil { if os.IsNotExist(err) { return nil @@ -150,7 +149,7 @@ func saveRegistry(lock bool) error { // write file // TODO: write atomically (best effort) filePath := path.Join(rootStructure.Path, registryFileName) - return ioutil.WriteFile(filePath, data, 0o0600) + return os.WriteFile(filePath, data, 0o0600) } func registryWriter() { diff --git a/database/storage/badger/badger_test.go b/database/storage/badger/badger_test.go index 9cff49f..40bc8f0 100644 --- a/database/storage/badger/badger_test.go +++ b/database/storage/badger/badger_test.go @@ -2,7 +2,6 @@ package badger import ( "context" - "io/ioutil" "os" "reflect" "sync" @@ -41,7 +40,7 @@ type TestRecord struct { //nolint:maligned func TestBadger(t *testing.T) { t.Parallel() - testDir, err := ioutil.TempDir("", "testing-") + testDir, err := os.MkdirTemp("", "testing-") if err != nil { t.Fatal(err) } diff --git a/database/storage/bbolt/bbolt_test.go b/database/storage/bbolt/bbolt_test.go index 5e9b302..9fdfca8 100644 --- a/database/storage/bbolt/bbolt_test.go +++ b/database/storage/bbolt/bbolt_test.go @@ -2,7 +2,6 @@ package bbolt import ( "context" - "io/ioutil" "os" "reflect" "sync" @@ -43,7 +42,7 @@ type TestRecord struct { //nolint:maligned func TestBBolt(t *testing.T) { t.Parallel() - testDir, err := ioutil.TempDir("", "testing-") + testDir, err := os.MkdirTemp("", "testing-") if err != nil { t.Fatal(err) } diff --git a/database/storage/fstree/fstree.go b/database/storage/fstree/fstree.go index cf6c56c..dfc9e17 100644 --- a/database/storage/fstree/fstree.go +++ b/database/storage/fstree/fstree.go @@ -8,7 +8,6 @@ import ( "context" "errors" "fmt" - "io/ioutil" "os" "path/filepath" "runtime" @@ -88,7 +87,7 @@ func (fst *FSTree) Get(key string) (record.Record, error) { return nil, err } - data, err := ioutil.ReadFile(dstPath) + data, err := os.ReadFile(dstPath) if err != nil { if os.IsNotExist(err) { return nil, storage.ErrNotFound @@ -210,7 +209,7 @@ func (fst *FSTree) queryExecutor(walkRoot string, queryIter *iterator.Iterator, } // read file - data, err := ioutil.ReadFile(path) + data, err := os.ReadFile(path) if err != nil { if os.IsNotExist(err) { return nil diff --git a/formats/dsd/http.go b/formats/dsd/http.go index 8653785..7b2e5c5 100644 --- a/formats/dsd/http.go +++ b/formats/dsd/http.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "mime" "net/http" ) @@ -33,7 +32,7 @@ func LoadFromHTTPResponse(resp *http.Response, t interface{}) (format uint8, err func loadFromHTTP(body io.Reader, mimeType string, t interface{}) (format uint8, err error) { // Read full body. - data, err := ioutil.ReadAll(body) + data, err := io.ReadAll(body) if err != nil { return 0, fmt.Errorf("dsd: failed to read http body: %w", err) } @@ -90,7 +89,7 @@ func DumpToHTTPRequest(r *http.Request, t interface{}, format uint8) error { // Set body. r.Header.Set("Content-Type", mimeType) - r.Body = ioutil.NopCloser(bytes.NewReader(data)) + r.Body = io.NopCloser(bytes.NewReader(data)) return nil } diff --git a/metrics/api.go b/metrics/api.go index 974140e..859e1bb 100644 --- a/metrics/api.go +++ b/metrics/api.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "time" @@ -111,7 +110,7 @@ func writeMetricsTo(ctx context.Context, url string) error { } // Get and return error. - body, _ := ioutil.ReadAll(resp.Body) + body, _ := io.ReadAll(resp.Body) return fmt.Errorf( "got %s while writing metrics to %s: %s", resp.Status, diff --git a/modules/subsystems/subsystems_test.go b/modules/subsystems/subsystems_test.go index ab632e5..e6ba837 100644 --- a/modules/subsystems/subsystems_test.go +++ b/modules/subsystems/subsystems_test.go @@ -1,7 +1,6 @@ package subsystems import ( - "io/ioutil" "os" "testing" "time" @@ -14,7 +13,7 @@ import ( func TestSubsystems(t *testing.T) { //nolint:paralleltest // Too much interference expected. // tmp dir for data root (db & config) - tmpDir, err := ioutil.TempDir("", "portbase-testing-") + tmpDir, err := os.MkdirTemp("", "portbase-testing-") // initialize data dir if err == nil { err = dataroot.Initialize(tmpDir, 0o0755) diff --git a/template/module_test.go b/template/module_test.go index 30c9bb8..8ab51f0 100644 --- a/template/module_test.go +++ b/template/module_test.go @@ -2,7 +2,6 @@ package template import ( "fmt" - "io/ioutil" "os" "testing" @@ -19,7 +18,7 @@ func TestMain(m *testing.M) { module.Enable() // tmp dir for data root (db & config) - tmpDir, err := ioutil.TempDir("", "portbase-testing-") + tmpDir, err := os.MkdirTemp("", "portbase-testing-") if err != nil { fmt.Fprintf(os.Stderr, "failed to create tmp dir: %s\n", err) os.Exit(1) diff --git a/updater/fetch.go b/updater/fetch.go index 16cdbba..753d909 100644 --- a/updater/fetch.go +++ b/updater/fetch.go @@ -7,7 +7,6 @@ import ( "fmt" "hash" "io" - "io/ioutil" "net/http" "net/url" "os" @@ -119,7 +118,7 @@ func (reg *ResourceRegistry) fetchFile(ctx context.Context, client *http.Client, // Write signature file, if we have one and if verification succeeded. if len(sigFileData) > 0 && hasher != nil { sigFilePath := rv.storagePath() + filesig.Extension - err := ioutil.WriteFile(sigFilePath, sigFileData, 0o0644) //nolint:gosec + err := os.WriteFile(sigFilePath, sigFileData, 0o0644) //nolint:gosec if err != nil { switch rv.resource.VerificationOptions.DownloadPolicy { case SignaturePolicyRequire: @@ -212,7 +211,7 @@ func (reg *ResourceRegistry) fetchMissingSig(ctx context.Context, client *http.C } // Write signature file. - err = ioutil.WriteFile(rv.storageSigPath(), sigFileData, 0o0644) //nolint:gosec + err = os.WriteFile(rv.storageSigPath(), sigFileData, 0o0644) //nolint:gosec if err != nil { switch rv.resource.VerificationOptions.DownloadPolicy { case SignaturePolicyRequire: diff --git a/updater/registry_test.go b/updater/registry_test.go index d25ef87..945d3da 100644 --- a/updater/registry_test.go +++ b/updater/registry_test.go @@ -1,7 +1,6 @@ package updater import ( - "io/ioutil" "os" "testing" @@ -12,7 +11,7 @@ var registry *ResourceRegistry func TestMain(m *testing.M) { // setup - tmpDir, err := ioutil.TempDir("", "ci-portmaster-") + tmpDir, err := os.MkdirTemp("", "ci-portmaster-") if err != nil { panic(err) } diff --git a/updater/storage_test.go b/updater/storage_test.go index eafabe4..2e4122f 100644 --- a/updater/storage_test.go +++ b/updater/storage_test.go @@ -13,7 +13,7 @@ func testLoadLatestScope(t *testing.T, basePath, filePath, expectedIdentifier, e } // touch file - err = ioutil.WriteFile(fullPath, []byte{}, 0644) + err = os.WriteFile(fullPath, []byte{}, 0644) if err != nil { t.Fatalf("could not create test file: %s\n", err) return @@ -45,7 +45,7 @@ func TestLoadLatestScope(t *testing.T) { updatesLock.Lock() defer updatesLock.Unlock() - tmpDir, err := ioutil.TempDir("", "testing_") + tmpDir, err := os.MkdirTemp("", "testing_") if err != nil { t.Fatalf("could not create test dir: %s\n", err) return diff --git a/updater/updating.go b/updater/updating.go index 86b7132..116db17 100644 --- a/updater/updating.go +++ b/updater/updating.go @@ -3,8 +3,8 @@ package updater import ( "context" "fmt" - "io/ioutil" "net/http" + "os" "path" "path/filepath" "strings" @@ -143,7 +143,7 @@ func (reg *ResourceRegistry) downloadIndex(ctx context.Context, client *http.Cli } // Index files must be readable by portmaster-staert with user permissions in order to load the index. - err = ioutil.WriteFile( //nolint:gosec + err = os.WriteFile( //nolint:gosec filepath.Join(reg.storageDir.Path, filepath.FromSlash(idx.Path)), indexData, 0o0644, ) @@ -153,7 +153,7 @@ func (reg *ResourceRegistry) downloadIndex(ctx context.Context, client *http.Cli // Write signature file, if we have one. if len(sigFileData) > 0 { - err = ioutil.WriteFile( //nolint:gosec + err = os.WriteFile( //nolint:gosec filepath.Join(reg.storageDir.Path, filepath.FromSlash(idx.Path)+filesig.Extension), sigFileData, 0o0644, ) diff --git a/utils/renameio/symlink_test.go b/utils/renameio/symlink_test.go index e8b25c6..a3a1b48 100644 --- a/utils/renameio/symlink_test.go +++ b/utils/renameio/symlink_test.go @@ -4,7 +4,6 @@ package renameio import ( "bytes" - "io/ioutil" "os" "path/filepath" "testing" @@ -13,7 +12,7 @@ import ( func TestSymlink(t *testing.T) { t.Parallel() - d, err := ioutil.TempDir("", "test-renameio-testsymlink") + d, err := os.MkdirTemp("", "test-renameio-testsymlink") if err != nil { t.Fatal(err) } @@ -22,7 +21,7 @@ func TestSymlink(t *testing.T) { }) want := []byte("Hello World") - if err := ioutil.WriteFile(filepath.Join(d, "hello.txt"), want, 0o0600); err != nil { + if err := os.WriteFile(filepath.Join(d, "hello.txt"), want, 0o0600); err != nil { t.Fatal(err) } @@ -31,7 +30,7 @@ func TestSymlink(t *testing.T) { t.Fatal(err) } - got, err := ioutil.ReadFile(filepath.Join(d, "hi.txt")) + got, err := os.ReadFile(filepath.Join(d, "hi.txt")) if err != nil { t.Fatal(err) } diff --git a/utils/renameio/tempfile.go b/utils/renameio/tempfile.go index 8364d64..270bbc9 100644 --- a/utils/renameio/tempfile.go +++ b/utils/renameio/tempfile.go @@ -1,7 +1,6 @@ package renameio import ( - "io/ioutil" "os" "path/filepath" ) @@ -31,7 +30,7 @@ func tempDir(dir, dest string) string { // the TMPDIR environment variable. tmpdir := os.TempDir() - testsrc, err := ioutil.TempFile(tmpdir, "."+filepath.Base(dest)) + testsrc, err := os.CreateTemp(tmpdir, "."+filepath.Base(dest)) if err != nil { return fallback } @@ -43,7 +42,7 @@ func tempDir(dir, dest string) string { }() _ = testsrc.Close() - testdest, err := ioutil.TempFile(filepath.Dir(dest), "."+filepath.Base(dest)) + testdest, err := os.CreateTemp(filepath.Dir(dest), "."+filepath.Base(dest)) if err != nil { return fallback } @@ -114,7 +113,7 @@ func (t *PendingFile) CloseAtomicallyReplace() error { return nil } -// TempFile wraps ioutil.TempFile for the use case of atomically creating or +// TempFile wraps os.CreateTemp for the use case of atomically creating or // replacing the destination file at path. // // If dir is the empty string, TempDir(filepath.Base(path)) is used. If you are @@ -125,7 +124,7 @@ func (t *PendingFile) CloseAtomicallyReplace() error { // The file's permissions will be 0600 by default. You can change these by // explicitly calling Chmod on the returned PendingFile. func TempFile(dir, path string) (*PendingFile, error) { - f, err := ioutil.TempFile(tempDir(dir, path), "."+filepath.Base(path)) + f, err := os.CreateTemp(tempDir(dir, path), "."+filepath.Base(path)) if err != nil { return nil, err } @@ -142,9 +141,9 @@ func Symlink(oldname, newname string) error { return err } - // We need to use ioutil.TempDir, as we cannot overwrite a ioutil.TempFile, + // We need to use os.MkdirTemp, as we cannot overwrite a os.CreateTemp, // and removing+symlinking creates a TOCTOU race. - d, err := ioutil.TempDir(filepath.Dir(newname), "."+filepath.Base(newname)) + d, err := os.MkdirTemp(filepath.Dir(newname), "."+filepath.Base(newname)) if err != nil { return err } diff --git a/utils/renameio/tempfile_linux_test.go b/utils/renameio/tempfile_linux_test.go index 95375e1..88ce025 100644 --- a/utils/renameio/tempfile_linux_test.go +++ b/utils/renameio/tempfile_linux_test.go @@ -3,7 +3,6 @@ package renameio import ( - "io/ioutil" "os" "path/filepath" "syscall" @@ -23,7 +22,7 @@ func TestTempDir(t *testing.T) { }) } - mount1, err := ioutil.TempDir("", "test-renameio-testtempdir1") + mount1, err := os.MkdirTemp("", "test-renameio-testtempdir1") if err != nil { t.Fatal(err) } @@ -31,7 +30,7 @@ func TestTempDir(t *testing.T) { _ = os.RemoveAll(mount1) }) - mount2, err := ioutil.TempDir("", "test-renameio-testtempdir2") + mount2, err := os.MkdirTemp("", "test-renameio-testtempdir2") if err != nil { t.Fatal(err) } diff --git a/utils/renameio/writefile_test.go b/utils/renameio/writefile_test.go index b7d1b49..eaf302b 100644 --- a/utils/renameio/writefile_test.go +++ b/utils/renameio/writefile_test.go @@ -4,7 +4,6 @@ package renameio import ( "bytes" - "io/ioutil" "os" "path/filepath" "testing" @@ -13,7 +12,7 @@ import ( func TestWriteFile(t *testing.T) { t.Parallel() - d, err := ioutil.TempDir("", "test-renameio-testwritefile") + d, err := os.MkdirTemp("", "test-renameio-testwritefile") if err != nil { t.Fatal(err) } @@ -29,7 +28,7 @@ func TestWriteFile(t *testing.T) { t.Fatal(err) } - gotData, err := ioutil.ReadFile(filename) + gotData, err := os.ReadFile(filename) if err != nil { t.Fatal(err) } diff --git a/utils/structure_test.go b/utils/structure_test.go index 8cbb2ae..0c277af 100644 --- a/utils/structure_test.go +++ b/utils/structure_test.go @@ -4,7 +4,6 @@ package utils import ( "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -23,7 +22,7 @@ func ExampleDirStructure() { // /repo/b/d/f/g/h [707] // /secret [700] - basePath, err := ioutil.TempDir("", "") + basePath, err := os.MkdirTemp("", "") if err != nil { fmt.Println(err) return