FIx linter errors

This commit is contained in:
Daniel 2022-09-29 10:55:01 +02:00
parent 3c697abd5b
commit 412b4242c2
21 changed files with 39 additions and 57 deletions

View file

@ -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

View file

@ -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)

View file

@ -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.

View file

@ -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)
}

View file

@ -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() {

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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

View file

@ -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
}

View file

@ -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,

View file

@ -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)

View file

@ -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)

View file

@ -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:

View file

@ -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)
}

View file

@ -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

View file

@ -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,
)

View file

@ -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)
}

View file

@ -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
}

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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