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" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"sort" "sort"
"strconv" "strconv"
@ -501,7 +500,7 @@ func readBody(w http.ResponseWriter, r *http.Request) (inputData []byte, ok bool
} }
// Read and close body. // Read and close body.
inputData, err := ioutil.ReadAll(r.Body) inputData, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
http.Error(w, "failed to read body"+err.Error(), http.StatusInternalServerError) http.Error(w, "failed to read body"+err.Error(), http.StatusInternalServerError)
return nil, false return nil, false

View file

@ -2,7 +2,6 @@ package api
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"testing" "testing"
@ -21,7 +20,7 @@ func TestMain(m *testing.M) {
module.Enable() module.Enable()
// tmp dir for data root (db & config) // tmp dir for data root (db & config)
tmpDir, err := ioutil.TempDir("", "portbase-testing-") tmpDir, err := os.MkdirTemp("", "portbase-testing-")
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "failed to create tmp dir: %s\n", err) fmt.Fprintf(os.Stderr, "failed to create tmp dir: %s\n", err)
os.Exit(1) os.Exit(1)

View file

@ -3,7 +3,7 @@ package config
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "os"
"path" "path"
"strings" "strings"
"sync" "sync"
@ -34,7 +34,7 @@ func loadConfig(requireValidConfig bool) error {
} }
// read config file // read config file
data, err := ioutil.ReadFile(configFilePath) data, err := os.ReadFile(configFilePath)
if err != nil { if err != nil {
return err return err
} }
@ -93,7 +93,7 @@ func saveConfig() error {
} }
// write file // write file
return ioutil.WriteFile(configFilePath, data, 0o0600) return os.WriteFile(configFilePath, data, 0o0600)
} }
// JSONToMap parses and flattens a hierarchical json object. // JSONToMap parses and flattens a hierarchical json object.

View file

@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"reflect" "reflect"
@ -22,7 +21,7 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
testDir, err := ioutil.TempDir("", "portbase-database-testing-") testDir, err := os.MkdirTemp("", "portbase-database-testing-")
if err != nil { if err != nil {
panic(err) panic(err)
} }

View file

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path" "path"
"regexp" "regexp"
@ -115,7 +114,7 @@ func loadRegistry() error {
// read file // read file
filePath := path.Join(rootStructure.Path, registryFileName) filePath := path.Join(rootStructure.Path, registryFileName)
data, err := ioutil.ReadFile(filePath) data, err := os.ReadFile(filePath)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return nil return nil
@ -150,7 +149,7 @@ func saveRegistry(lock bool) error {
// write file // write file
// TODO: write atomically (best effort) // TODO: write atomically (best effort)
filePath := path.Join(rootStructure.Path, registryFileName) filePath := path.Join(rootStructure.Path, registryFileName)
return ioutil.WriteFile(filePath, data, 0o0600) return os.WriteFile(filePath, data, 0o0600)
} }
func registryWriter() { func registryWriter() {

View file

@ -2,7 +2,6 @@ package badger
import ( import (
"context" "context"
"io/ioutil"
"os" "os"
"reflect" "reflect"
"sync" "sync"
@ -41,7 +40,7 @@ type TestRecord struct { //nolint:maligned
func TestBadger(t *testing.T) { func TestBadger(t *testing.T) {
t.Parallel() t.Parallel()
testDir, err := ioutil.TempDir("", "testing-") testDir, err := os.MkdirTemp("", "testing-")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -2,7 +2,6 @@ package bbolt
import ( import (
"context" "context"
"io/ioutil"
"os" "os"
"reflect" "reflect"
"sync" "sync"
@ -43,7 +42,7 @@ type TestRecord struct { //nolint:maligned
func TestBBolt(t *testing.T) { func TestBBolt(t *testing.T) {
t.Parallel() t.Parallel()
testDir, err := ioutil.TempDir("", "testing-") testDir, err := os.MkdirTemp("", "testing-")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -8,7 +8,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -88,7 +87,7 @@ func (fst *FSTree) Get(key string) (record.Record, error) {
return nil, err return nil, err
} }
data, err := ioutil.ReadFile(dstPath) data, err := os.ReadFile(dstPath)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return nil, storage.ErrNotFound return nil, storage.ErrNotFound
@ -210,7 +209,7 @@ func (fst *FSTree) queryExecutor(walkRoot string, queryIter *iterator.Iterator,
} }
// read file // read file
data, err := ioutil.ReadFile(path) data, err := os.ReadFile(path)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return nil return nil

View file

@ -5,7 +5,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"mime" "mime"
"net/http" "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) { func loadFromHTTP(body io.Reader, mimeType string, t interface{}) (format uint8, err error) {
// Read full body. // Read full body.
data, err := ioutil.ReadAll(body) data, err := io.ReadAll(body)
if err != nil { if err != nil {
return 0, fmt.Errorf("dsd: failed to read http body: %w", err) 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. // Set body.
r.Header.Set("Content-Type", mimeType) r.Header.Set("Content-Type", mimeType)
r.Body = ioutil.NopCloser(bytes.NewReader(data)) r.Body = io.NopCloser(bytes.NewReader(data))
return nil return nil
} }

View file

@ -6,7 +6,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"time" "time"
@ -111,7 +110,7 @@ func writeMetricsTo(ctx context.Context, url string) error {
} }
// Get and return error. // Get and return error.
body, _ := ioutil.ReadAll(resp.Body) body, _ := io.ReadAll(resp.Body)
return fmt.Errorf( return fmt.Errorf(
"got %s while writing metrics to %s: %s", "got %s while writing metrics to %s: %s",
resp.Status, resp.Status,

View file

@ -1,7 +1,6 @@
package subsystems package subsystems
import ( import (
"io/ioutil"
"os" "os"
"testing" "testing"
"time" "time"
@ -14,7 +13,7 @@ import (
func TestSubsystems(t *testing.T) { //nolint:paralleltest // Too much interference expected. func TestSubsystems(t *testing.T) { //nolint:paralleltest // Too much interference expected.
// tmp dir for data root (db & config) // tmp dir for data root (db & config)
tmpDir, err := ioutil.TempDir("", "portbase-testing-") tmpDir, err := os.MkdirTemp("", "portbase-testing-")
// initialize data dir // initialize data dir
if err == nil { if err == nil {
err = dataroot.Initialize(tmpDir, 0o0755) err = dataroot.Initialize(tmpDir, 0o0755)

View file

@ -2,7 +2,6 @@ package template
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"testing" "testing"
@ -19,7 +18,7 @@ func TestMain(m *testing.M) {
module.Enable() module.Enable()
// tmp dir for data root (db & config) // tmp dir for data root (db & config)
tmpDir, err := ioutil.TempDir("", "portbase-testing-") tmpDir, err := os.MkdirTemp("", "portbase-testing-")
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "failed to create tmp dir: %s\n", err) fmt.Fprintf(os.Stderr, "failed to create tmp dir: %s\n", err)
os.Exit(1) os.Exit(1)

View file

@ -7,7 +7,6 @@ import (
"fmt" "fmt"
"hash" "hash"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"os" "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. // Write signature file, if we have one and if verification succeeded.
if len(sigFileData) > 0 && hasher != nil { if len(sigFileData) > 0 && hasher != nil {
sigFilePath := rv.storagePath() + filesig.Extension sigFilePath := rv.storagePath() + filesig.Extension
err := ioutil.WriteFile(sigFilePath, sigFileData, 0o0644) //nolint:gosec err := os.WriteFile(sigFilePath, sigFileData, 0o0644) //nolint:gosec
if err != nil { if err != nil {
switch rv.resource.VerificationOptions.DownloadPolicy { switch rv.resource.VerificationOptions.DownloadPolicy {
case SignaturePolicyRequire: case SignaturePolicyRequire:
@ -212,7 +211,7 @@ func (reg *ResourceRegistry) fetchMissingSig(ctx context.Context, client *http.C
} }
// Write signature file. // Write signature file.
err = ioutil.WriteFile(rv.storageSigPath(), sigFileData, 0o0644) //nolint:gosec err = os.WriteFile(rv.storageSigPath(), sigFileData, 0o0644) //nolint:gosec
if err != nil { if err != nil {
switch rv.resource.VerificationOptions.DownloadPolicy { switch rv.resource.VerificationOptions.DownloadPolicy {
case SignaturePolicyRequire: case SignaturePolicyRequire:

View file

@ -1,7 +1,6 @@
package updater package updater
import ( import (
"io/ioutil"
"os" "os"
"testing" "testing"
@ -12,7 +11,7 @@ var registry *ResourceRegistry
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
// setup // setup
tmpDir, err := ioutil.TempDir("", "ci-portmaster-") tmpDir, err := os.MkdirTemp("", "ci-portmaster-")
if err != nil { if err != nil {
panic(err) panic(err)
} }

View file

@ -13,7 +13,7 @@ func testLoadLatestScope(t *testing.T, basePath, filePath, expectedIdentifier, e
} }
// touch file // touch file
err = ioutil.WriteFile(fullPath, []byte{}, 0644) err = os.WriteFile(fullPath, []byte{}, 0644)
if err != nil { if err != nil {
t.Fatalf("could not create test file: %s\n", err) t.Fatalf("could not create test file: %s\n", err)
return return
@ -45,7 +45,7 @@ func TestLoadLatestScope(t *testing.T) {
updatesLock.Lock() updatesLock.Lock()
defer updatesLock.Unlock() defer updatesLock.Unlock()
tmpDir, err := ioutil.TempDir("", "testing_") tmpDir, err := os.MkdirTemp("", "testing_")
if err != nil { if err != nil {
t.Fatalf("could not create test dir: %s\n", err) t.Fatalf("could not create test dir: %s\n", err)
return return

View file

@ -3,8 +3,8 @@ package updater
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"os"
"path" "path"
"path/filepath" "path/filepath"
"strings" "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. // 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)), filepath.Join(reg.storageDir.Path, filepath.FromSlash(idx.Path)),
indexData, 0o0644, indexData, 0o0644,
) )
@ -153,7 +153,7 @@ func (reg *ResourceRegistry) downloadIndex(ctx context.Context, client *http.Cli
// Write signature file, if we have one. // Write signature file, if we have one.
if len(sigFileData) > 0 { 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), filepath.Join(reg.storageDir.Path, filepath.FromSlash(idx.Path)+filesig.Extension),
sigFileData, 0o0644, sigFileData, 0o0644,
) )

View file

@ -4,7 +4,6 @@ package renameio
import ( import (
"bytes" "bytes"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -13,7 +12,7 @@ import (
func TestSymlink(t *testing.T) { func TestSymlink(t *testing.T) {
t.Parallel() t.Parallel()
d, err := ioutil.TempDir("", "test-renameio-testsymlink") d, err := os.MkdirTemp("", "test-renameio-testsymlink")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -22,7 +21,7 @@ func TestSymlink(t *testing.T) {
}) })
want := []byte("Hello World") 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) t.Fatal(err)
} }
@ -31,7 +30,7 @@ func TestSymlink(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
got, err := ioutil.ReadFile(filepath.Join(d, "hi.txt")) got, err := os.ReadFile(filepath.Join(d, "hi.txt"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -1,7 +1,6 @@
package renameio package renameio
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
) )
@ -31,7 +30,7 @@ func tempDir(dir, dest string) string {
// the TMPDIR environment variable. // the TMPDIR environment variable.
tmpdir := os.TempDir() tmpdir := os.TempDir()
testsrc, err := ioutil.TempFile(tmpdir, "."+filepath.Base(dest)) testsrc, err := os.CreateTemp(tmpdir, "."+filepath.Base(dest))
if err != nil { if err != nil {
return fallback return fallback
} }
@ -43,7 +42,7 @@ func tempDir(dir, dest string) string {
}() }()
_ = testsrc.Close() _ = 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 { if err != nil {
return fallback return fallback
} }
@ -114,7 +113,7 @@ func (t *PendingFile) CloseAtomicallyReplace() error {
return nil 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. // replacing the destination file at path.
// //
// If dir is the empty string, TempDir(filepath.Base(path)) is used. If you are // 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 // The file's permissions will be 0600 by default. You can change these by
// explicitly calling Chmod on the returned PendingFile. // explicitly calling Chmod on the returned PendingFile.
func TempFile(dir, path string) (*PendingFile, error) { 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 { if err != nil {
return nil, err return nil, err
} }
@ -142,9 +141,9 @@ func Symlink(oldname, newname string) error {
return err 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. // 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 { if err != nil {
return err return err
} }

View file

@ -3,7 +3,6 @@
package renameio package renameio
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"syscall" "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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -31,7 +30,7 @@ func TestTempDir(t *testing.T) {
_ = os.RemoveAll(mount1) _ = os.RemoveAll(mount1)
}) })
mount2, err := ioutil.TempDir("", "test-renameio-testtempdir2") mount2, err := os.MkdirTemp("", "test-renameio-testtempdir2")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -4,7 +4,6 @@ package renameio
import ( import (
"bytes" "bytes"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -13,7 +12,7 @@ import (
func TestWriteFile(t *testing.T) { func TestWriteFile(t *testing.T) {
t.Parallel() t.Parallel()
d, err := ioutil.TempDir("", "test-renameio-testwritefile") d, err := os.MkdirTemp("", "test-renameio-testwritefile")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -29,7 +28,7 @@ func TestWriteFile(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
gotData, err := ioutil.ReadFile(filename) gotData, err := os.ReadFile(filename)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -4,7 +4,6 @@ package utils
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -23,7 +22,7 @@ func ExampleDirStructure() {
// /repo/b/d/f/g/h [707] // /repo/b/d/f/g/h [707]
// /secret [700] // /secret [700]
basePath, err := ioutil.TempDir("", "") basePath, err := os.MkdirTemp("", "")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return