fix most linter errors

This commit is contained in:
Michael Eischer 2025-02-02 22:45:41 +01:00
parent 3e0737a8bd
commit 9fc5066fc4
8 changed files with 57 additions and 42 deletions

View file

@ -61,7 +61,10 @@ func TestUnixSocket(t *testing.T) {
if err != nil { if err != nil {
return err return err
} }
resp.Body.Close() err = resp.Body.Close()
if err != nil {
return err
}
if resp.StatusCode != test.StatusCode { if resp.StatusCode != test.StatusCode {
return fmt.Errorf("expected %d from server, instead got %d (path %s)", test.StatusCode, resp.StatusCode, test.Path) return fmt.Errorf("expected %d from server, instead got %d (path %s)", test.StatusCode, resp.StatusCode, test.Path)
} }

View file

@ -22,7 +22,7 @@ import (
type restServerApp struct { type restServerApp struct {
CmdRoot *cobra.Command CmdRoot *cobra.Command
Server restserver.Server Server restserver.Server
CpuProfile string CPUProfile string
listenerAddressMu sync.Mutex listenerAddressMu sync.Mutex
listenerAddress net.Addr // set after startup listenerAddress net.Addr // set after startup
@ -36,7 +36,7 @@ func newRestServerApp() *restServerApp {
Short: "Run a REST server for use with restic", Short: "Run a REST server for use with restic",
SilenceErrors: true, SilenceErrors: true,
SilenceUsage: true, SilenceUsage: true,
Args: func(cmd *cobra.Command, args []string) error { Args: func(_ *cobra.Command, args []string) error {
if len(args) != 0 { if len(args) != 0 {
return fmt.Errorf("rest-server expects no arguments - unknown argument: %s", args[0]) return fmt.Errorf("rest-server expects no arguments - unknown argument: %s", args[0])
} }
@ -52,7 +52,7 @@ func newRestServerApp() *restServerApp {
rv.CmdRoot.RunE = rv.runRoot rv.CmdRoot.RunE = rv.runRoot
flags := rv.CmdRoot.Flags() flags := rv.CmdRoot.Flags()
flags.StringVar(&rv.CpuProfile, "cpu-profile", rv.CpuProfile, "write CPU profile to file") flags.StringVar(&rv.CPUProfile, "cpu-profile", rv.CPUProfile, "write CPU profile to file")
flags.BoolVar(&rv.Server.Debug, "debug", rv.Server.Debug, "output debug messages") flags.BoolVar(&rv.Server.Debug, "debug", rv.Server.Debug, "output debug messages")
flags.StringVar(&rv.Server.Listen, "listen", rv.Server.Listen, "listen address") flags.StringVar(&rv.Server.Listen, "listen", rv.Server.Listen, "listen address")
flags.StringVar(&rv.Server.Log, "log", rv.Server.Log, "write HTTP requests in the combined log format to the specified `filename` (use \"-\" for logging to stdout)") flags.StringVar(&rv.Server.Log, "log", rv.Server.Log, "write HTTP requests in the combined log format to the specified `filename` (use \"-\" for logging to stdout)")
@ -103,17 +103,19 @@ func (app *restServerApp) ListenerAddress() net.Addr {
return app.listenerAddress return app.listenerAddress
} }
func (app *restServerApp) runRoot(cmd *cobra.Command, args []string) error { func (app *restServerApp) runRoot(_ *cobra.Command, _ []string) error {
log.SetFlags(0) log.SetFlags(0)
log.Printf("Data directory: %s", app.Server.Path) log.Printf("Data directory: %s", app.Server.Path)
if app.CpuProfile != "" { if app.CPUProfile != "" {
f, err := os.Create(app.CpuProfile) f, err := os.Create(app.CPUProfile)
if err != nil { if err != nil {
return err return err
} }
defer f.Close() defer func() {
_ = f.Close()
}()
if err := pprof.StartCPUProfile(f); err != nil { if err := pprof.StartCPUProfile(f); err != nil {
return err return err

View file

@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -94,7 +93,7 @@ func TestTLSSettings(t *testing.T) {
} }
func TestGetHandler(t *testing.T) { func TestGetHandler(t *testing.T) {
dir, err := ioutil.TempDir("", "rest-server-test") dir, err := os.MkdirTemp("", "rest-server-test")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -120,7 +119,7 @@ func TestGetHandler(t *testing.T) {
} }
// With NoAuth = false and custom .htpasswd // With NoAuth = false and custom .htpasswd
htpFile, err := ioutil.TempFile(dir, "custom") htpFile, err := os.CreateTemp(dir, "custom")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -137,7 +136,7 @@ func TestGetHandler(t *testing.T) {
// Create .htpasswd // Create .htpasswd
htpasswd := filepath.Join(dir, ".htpasswd") htpasswd := filepath.Join(dir, ".htpasswd")
err = ioutil.WriteFile(htpasswd, []byte(""), 0644) err = os.WriteFile(htpasswd, []byte(""), 0644)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -262,7 +261,10 @@ func TestHttpListen(t *testing.T) {
if err != nil { if err != nil {
return err return err
} }
resp.Body.Close() err = resp.Body.Close()
if err != nil {
return err
}
if resp.StatusCode != test.StatusCode { if resp.StatusCode != test.StatusCode {
return fmt.Errorf("expected %d from server, instead got %d (path %s)", test.StatusCode, resp.StatusCode, test.Path) return fmt.Errorf("expected %d from server, instead got %d (path %s)", test.StatusCode, resp.StatusCode, test.Path)
} }

View file

@ -158,7 +158,8 @@ func join(base string, names ...string) (string, error) {
// splitURLPath splits the URL path into a folderPath of the subrepo, and // splitURLPath splits the URL path into a folderPath of the subrepo, and
// a remainder that can be passed to repo.Handler. // a remainder that can be passed to repo.Handler.
// Example: /foo/bar/locks/0123... will be split into: // Example: /foo/bar/locks/0123... will be split into:
// ["foo", "bar"] and "/locks/0123..." //
// ["foo", "bar"] and "/locks/0123..."
func splitURLPath(urlPath string, maxDepth int) (folderPath []string, remainder string) { func splitURLPath(urlPath string, maxDepth int) (folderPath []string, remainder string) {
if !strings.HasPrefix(urlPath, "/") { if !strings.HasPrefix(urlPath, "/") {
// Really should start with "/" // Really should start with "/"

View file

@ -6,7 +6,6 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
@ -165,7 +164,7 @@ func createOverwriteDeleteSeq(t testing.TB, path string, data string) []TestRequ
return req return req
} }
func createTestHandler(t *testing.T, conf Server) (http.Handler, string, string, string, func()) { func createTestHandler(t *testing.T, conf *Server) (http.Handler, string, string, string, func()) {
buf := make([]byte, 32) buf := make([]byte, 32)
_, err := io.ReadFull(rand.Reader, buf) _, err := io.ReadFull(rand.Reader, buf)
if err != nil { if err != nil {
@ -176,7 +175,7 @@ func createTestHandler(t *testing.T, conf Server) (http.Handler, string, string,
fileID := hex.EncodeToString(dataHash[:]) fileID := hex.EncodeToString(dataHash[:])
// setup the server with a local backend in a temporary directory // setup the server with a local backend in a temporary directory
tempdir, err := ioutil.TempDir("", "rest-server-test-") tempdir, err := os.MkdirTemp("", "rest-server-test-")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -190,7 +189,7 @@ func createTestHandler(t *testing.T, conf Server) (http.Handler, string, string,
} }
conf.Path = tempdir conf.Path = tempdir
mux, err := NewHandler(&conf) mux, err := NewHandler(conf)
if err != nil { if err != nil {
t.Fatalf("error from NewHandler: %v", err) t.Fatalf("error from NewHandler: %v", err)
} }
@ -199,7 +198,7 @@ func createTestHandler(t *testing.T, conf Server) (http.Handler, string, string,
// TestResticAppendOnlyHandler runs tests on the restic handler code, especially in append-only mode. // TestResticAppendOnlyHandler runs tests on the restic handler code, especially in append-only mode.
func TestResticAppendOnlyHandler(t *testing.T) { func TestResticAppendOnlyHandler(t *testing.T) {
mux, data, fileID, _, cleanup := createTestHandler(t, Server{ mux, data, fileID, _, cleanup := createTestHandler(t, &Server{
AppendOnly: true, AppendOnly: true,
NoAuth: true, NoAuth: true,
Debug: true, Debug: true,
@ -300,7 +299,7 @@ func createIdempotentDeleteSeq(t testing.TB, path string, data string) []TestReq
// TestResticHandler runs tests on the restic handler code, especially in append-only mode. // TestResticHandler runs tests on the restic handler code, especially in append-only mode.
func TestResticHandler(t *testing.T) { func TestResticHandler(t *testing.T) {
mux, data, fileID, _, cleanup := createTestHandler(t, Server{ mux, data, fileID, _, cleanup := createTestHandler(t, &Server{
NoAuth: true, NoAuth: true,
Debug: true, Debug: true,
PanicOnError: true, PanicOnError: true,
@ -331,7 +330,7 @@ func TestResticHandler(t *testing.T) {
// TestResticErrorHandler runs tests on the restic handler error handling. // TestResticErrorHandler runs tests on the restic handler error handling.
func TestResticErrorHandler(t *testing.T) { func TestResticErrorHandler(t *testing.T) {
mux, _, _, tempdir, cleanup := createTestHandler(t, Server{ mux, _, _, tempdir, cleanup := createTestHandler(t, &Server{
AppendOnly: true, AppendOnly: true,
NoAuth: true, NoAuth: true,
Debug: true, Debug: true,
@ -380,7 +379,7 @@ func TestResticErrorHandler(t *testing.T) {
} }
func TestEmptyList(t *testing.T) { func TestEmptyList(t *testing.T) {
mux, _, _, _, cleanup := createTestHandler(t, Server{ mux, _, _, _, cleanup := createTestHandler(t, &Server{
AppendOnly: true, AppendOnly: true,
NoAuth: true, NoAuth: true,
Debug: true, Debug: true,
@ -404,7 +403,7 @@ func TestEmptyList(t *testing.T) {
} }
func TestListWithUnexpectedFiles(t *testing.T) { func TestListWithUnexpectedFiles(t *testing.T) {
mux, _, _, tempdir, cleanup := createTestHandler(t, Server{ mux, _, _, tempdir, cleanup := createTestHandler(t, &Server{
AppendOnly: true, AppendOnly: true,
NoAuth: true, NoAuth: true,
Debug: true, Debug: true,
@ -510,7 +509,7 @@ func newDelayedErrorReader(err error) *delayErrorReader {
} }
} }
func (d *delayErrorReader) Read(p []byte) (int, error) { func (d *delayErrorReader) Read(_ []byte) (int, error) {
d.firstReadOnce.Do(func() { d.firstReadOnce.Do(func() {
// close the channel to signal that the first read has happened // close the channel to signal that the first read has happened
close(d.FirstRead) close(d.FirstRead)
@ -522,7 +521,7 @@ func (d *delayErrorReader) Read(p []byte) (int, error) {
// TestAbortedRequest runs tests with concurrent upload requests for the same file. // TestAbortedRequest runs tests with concurrent upload requests for the same file.
func TestAbortedRequest(t *testing.T) { func TestAbortedRequest(t *testing.T) {
// the race condition doesn't happen for append-only repositories // the race condition doesn't happen for append-only repositories
mux, _, _, _, cleanup := createTestHandler(t, Server{ mux, _, _, _, cleanup := createTestHandler(t, &Server{
NoAuth: true, NoAuth: true,
Debug: true, Debug: true,
PanicOnError: true, PanicOnError: true,

View file

@ -1,7 +1,6 @@
package restserver package restserver
import ( import (
"io/ioutil"
"os" "os"
"testing" "testing"
) )
@ -12,7 +11,7 @@ func TestValidate(t *testing.T) {
rawPwd := "test" rawPwd := "test"
wrongPwd := "wrong" wrongPwd := "wrong"
tmpfile, err := ioutil.TempFile("", "rest-validate-") tmpfile, err := os.CreateTemp("", "rest-validate-")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -113,7 +113,7 @@ func tallySize(path string) (int64, error) {
path = "." path = "."
} }
var size int64 var size int64
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error { err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
} }

View file

@ -6,7 +6,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"math/rand" "math/rand"
"net/http" "net/http"
@ -251,7 +250,7 @@ func (h *Handler) wrapFileWriter(r *http.Request, w io.Writer) (io.Writer, int,
} }
// checkConfig checks whether a configuration exists. // checkConfig checks whether a configuration exists.
func (h *Handler) checkConfig(w http.ResponseWriter, r *http.Request) { func (h *Handler) checkConfig(w http.ResponseWriter, _ *http.Request) {
if h.opt.Debug { if h.opt.Debug {
log.Println("checkConfig()") log.Println("checkConfig()")
} }
@ -267,13 +266,13 @@ func (h *Handler) checkConfig(w http.ResponseWriter, r *http.Request) {
} }
// getConfig allows for a config to be retrieved. // getConfig allows for a config to be retrieved.
func (h *Handler) getConfig(w http.ResponseWriter, r *http.Request) { func (h *Handler) getConfig(w http.ResponseWriter, _ *http.Request) {
if h.opt.Debug { if h.opt.Debug {
log.Println("getConfig()") log.Println("getConfig()")
} }
cfg := h.getSubPath("config") cfg := h.getSubPath("config")
bytes, err := ioutil.ReadFile(cfg) bytes, err := os.ReadFile(cfg)
if err != nil { if err != nil {
h.fileAccessError(w, err) h.fileAccessError(w, err)
return return
@ -314,7 +313,7 @@ func (h *Handler) saveConfig(w http.ResponseWriter, r *http.Request) {
} }
// deleteConfig removes a config. // deleteConfig removes a config.
func (h *Handler) deleteConfig(w http.ResponseWriter, r *http.Request) { func (h *Handler) deleteConfig(w http.ResponseWriter, _ *http.Request) {
if h.opt.Debug { if h.opt.Debug {
log.Println("deleteConfig()") log.Println("deleteConfig()")
} }
@ -369,7 +368,7 @@ func (h *Handler) listBlobsV1(w http.ResponseWriter, r *http.Request) {
} }
path := h.getSubPath(objectType) path := h.getSubPath(objectType)
items, err := ioutil.ReadDir(path) items, err := os.ReadDir(path)
if err != nil { if err != nil {
h.fileAccessError(w, err) h.fileAccessError(w, err)
return return
@ -383,8 +382,8 @@ func (h *Handler) listBlobsV1(w http.ResponseWriter, r *http.Request) {
continue continue
} }
subpath := filepath.Join(path, i.Name()) subpath := filepath.Join(path, i.Name())
var subitems []os.FileInfo var subitems []os.DirEntry
subitems, err = ioutil.ReadDir(subpath) subitems, err = os.ReadDir(subpath)
if err != nil { if err != nil {
h.fileAccessError(w, err) h.fileAccessError(w, err)
return return
@ -428,7 +427,7 @@ func (h *Handler) listBlobsV2(w http.ResponseWriter, r *http.Request) {
} }
path := h.getSubPath(objectType) path := h.getSubPath(objectType)
items, err := ioutil.ReadDir(path) items, err := os.ReadDir(path)
if err != nil { if err != nil {
h.fileAccessError(w, err) h.fileAccessError(w, err)
return return
@ -442,17 +441,27 @@ func (h *Handler) listBlobsV2(w http.ResponseWriter, r *http.Request) {
continue continue
} }
subpath := filepath.Join(path, i.Name()) subpath := filepath.Join(path, i.Name())
var subitems []os.FileInfo var subitems []os.DirEntry
subitems, err = ioutil.ReadDir(subpath) subitems, err = os.ReadDir(subpath)
if err != nil { if err != nil {
h.fileAccessError(w, err) h.fileAccessError(w, err)
return return
} }
for _, f := range subitems { for _, f := range subitems {
blobs = append(blobs, Blob{Name: f.Name(), Size: f.Size()}) fi, err := f.Info()
if err != nil {
h.fileAccessError(w, err)
return
}
blobs = append(blobs, Blob{Name: f.Name(), Size: fi.Size()})
} }
} else { } else {
blobs = append(blobs, Blob{Name: i.Name(), Size: i.Size()}) fi, err := i.Info()
if err != nil {
h.fileAccessError(w, err)
return
}
blobs = append(blobs, Blob{Name: i.Name(), Size: fi.Size()})
} }
} }
@ -652,7 +661,7 @@ func (h *Handler) saveBlob(w http.ResponseWriter, r *http.Request) {
h.sendMetric(objectType, BlobWrite, uint64(written)) h.sendMetric(objectType, BlobWrite, uint64(written))
} }
// tempFile implements a custom version of ioutil.TempFile which allows modifying the file permissions // tempFile implements a custom version of os.CreateTemp which allows modifying the file permissions
func tempFile(fn string, perm os.FileMode) (f *os.File, err error) { func tempFile(fn string, perm os.FileMode) (f *os.File, err error) {
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
name := fn + strconv.FormatInt(rand.Int63(), 10) name := fn + strconv.FormatInt(rand.Int63(), 10)