mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-16 21:56:40 -05:00
chore: check against errors of io/fs
instead of os
(#6011)
* chore: replace `os.ErrNotExist` with `fs.ErrNotExist` * check against permission error from `io/fs` package
This commit is contained in:
parent
b568a10dd4
commit
787f6b257f
7 changed files with 23 additions and 22 deletions
3
caddy.go
3
caddy.go
|
@ -22,6 +22,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -828,7 +829,7 @@ func InstanceID() (uuid.UUID, error) {
|
||||||
appDataDir := AppDataDir()
|
appDataDir := AppDataDir()
|
||||||
uuidFilePath := filepath.Join(appDataDir, "instance.uuid")
|
uuidFilePath := filepath.Join(appDataDir, "instance.uuid")
|
||||||
uuidFileBytes, err := os.ReadFile(uuidFilePath)
|
uuidFileBytes, err := os.ReadFile(uuidFilePath)
|
||||||
if os.IsNotExist(err) {
|
if errors.Is(err, fs.ErrNotExist) {
|
||||||
uuid, err := uuid.NewRandom()
|
uuid, err := uuid.NewRandom()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return uuid, err
|
return uuid, err
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -232,7 +233,7 @@ const initConfig = `{
|
||||||
func validateTestPrerequisites(t *testing.T) error {
|
func validateTestPrerequisites(t *testing.T) error {
|
||||||
// check certificates are found
|
// check certificates are found
|
||||||
for _, certName := range Default.Certifcates {
|
for _, certName := range Default.Certifcates {
|
||||||
if _, err := os.Stat(getIntegrationDir() + certName); os.IsNotExist(err) {
|
if _, err := os.Stat(getIntegrationDir() + certName); errors.Is(err, fs.ErrNotExist) {
|
||||||
return fmt.Errorf("caddy integration test certificates (%s) not found", certName)
|
return fmt.Errorf("caddy integration test certificates (%s) not found", certName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -190,7 +190,7 @@ func cmdRun(fl Flags) (int, error) {
|
||||||
var config []byte
|
var config []byte
|
||||||
if resumeFlag {
|
if resumeFlag {
|
||||||
config, err = os.ReadFile(caddy.ConfigAutosavePath)
|
config, err = os.ReadFile(caddy.ConfigAutosavePath)
|
||||||
if os.IsNotExist(err) {
|
if errors.Is(err, fs.ErrNotExist) {
|
||||||
// not a bad error; just can't resume if autosave file doesn't exist
|
// not a bad error; just can't resume if autosave file doesn't exist
|
||||||
caddy.Log().Info("no autosave file exists", zap.String("autosave_file", caddy.ConfigAutosavePath))
|
caddy.Log().Info("no autosave file exists", zap.String("autosave_file", caddy.ConfigAutosavePath))
|
||||||
resumeFlag = false
|
resumeFlag = false
|
||||||
|
|
|
@ -21,6 +21,7 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
@ -128,7 +129,7 @@ func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([
|
||||||
cfgAdapter = caddyconfig.GetAdapter("caddyfile")
|
cfgAdapter = caddyconfig.GetAdapter("caddyfile")
|
||||||
if cfgAdapter != nil {
|
if cfgAdapter != nil {
|
||||||
config, err = os.ReadFile("Caddyfile")
|
config, err = os.ReadFile("Caddyfile")
|
||||||
if os.IsNotExist(err) {
|
if errors.Is(err, fs.ErrNotExist) {
|
||||||
// okay, no default Caddyfile; pretend like this never happened
|
// okay, no default Caddyfile; pretend like this never happened
|
||||||
cfgAdapter = nil
|
cfgAdapter = nil
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
|
|
|
@ -19,6 +19,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
@ -92,9 +93,9 @@ func (fsrv *FileServer) serveBrowse(root, dirPath string, w http.ResponseWriter,
|
||||||
// TODO: not entirely sure if path.Clean() is necessary here but seems like a safe plan (i.e. /%2e%2e%2f) - someone could verify this
|
// TODO: not entirely sure if path.Clean() is necessary here but seems like a safe plan (i.e. /%2e%2e%2f) - someone could verify this
|
||||||
listing, err := fsrv.loadDirectoryContents(r.Context(), dir.(fs.ReadDirFile), root, path.Clean(r.URL.EscapedPath()), repl)
|
listing, err := fsrv.loadDirectoryContents(r.Context(), dir.(fs.ReadDirFile), root, path.Clean(r.URL.EscapedPath()), repl)
|
||||||
switch {
|
switch {
|
||||||
case os.IsPermission(err):
|
case errors.Is(err, fs.ErrPermission):
|
||||||
return caddyhttp.Error(http.StatusForbidden, err)
|
return caddyhttp.Error(http.StatusForbidden, err)
|
||||||
case os.IsNotExist(err):
|
case errors.Is(err, fs.ErrNotExist):
|
||||||
return fsrv.notFound(w, r, next)
|
return fsrv.notFound(w, r, next)
|
||||||
case err != nil:
|
case err != nil:
|
||||||
return caddyhttp.Error(http.StatusInternalServerError, err)
|
return caddyhttp.Error(http.StatusInternalServerError, err)
|
||||||
|
|
|
@ -506,10 +506,10 @@ func (fsrv *FileServer) openFile(filename string, w http.ResponseWriter) (fs.Fil
|
||||||
file, err := fsrv.fileSystem.Open(filename)
|
file, err := fsrv.fileSystem.Open(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fsrv.mapDirOpenError(err, filename)
|
err = fsrv.mapDirOpenError(err, filename)
|
||||||
if os.IsNotExist(err) {
|
if errors.Is(err, fs.ErrNotExist) {
|
||||||
fsrv.logger.Debug("file not found", zap.String("filename", filename), zap.Error(err))
|
fsrv.logger.Debug("file not found", zap.String("filename", filename), zap.Error(err))
|
||||||
return nil, caddyhttp.Error(http.StatusNotFound, err)
|
return nil, caddyhttp.Error(http.StatusNotFound, err)
|
||||||
} else if os.IsPermission(err) {
|
} else if errors.Is(err, fs.ErrPermission) {
|
||||||
fsrv.logger.Debug("permission denied", zap.String("filename", filename), zap.Error(err))
|
fsrv.logger.Debug("permission denied", zap.String("filename", filename), zap.Error(err))
|
||||||
return nil, caddyhttp.Error(http.StatusForbidden, err)
|
return nil, caddyhttp.Error(http.StatusForbidden, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,9 @@ package templates
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -30,8 +32,7 @@ import (
|
||||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
type handle struct {
|
type handle struct{}
|
||||||
}
|
|
||||||
|
|
||||||
func (h *handle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h *handle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Header.Get("Accept-Encoding") == "identity" {
|
if r.Header.Get("Accept-Encoding") == "identity" {
|
||||||
|
@ -176,11 +177,10 @@ func TestImport(t *testing.T) {
|
||||||
} else if !templateWasDefined && actual != "" {
|
} else if !templateWasDefined && actual != "" {
|
||||||
// template should be defined, return value should be an empty string
|
// template should be defined, return value should be an empty string
|
||||||
t.Errorf("Test %d: Expected template %s to be define but got %s", i, test.expect, tplContext.tpl.DefinedTemplates())
|
t.Errorf("Test %d: Expected template %s to be define but got %s", i, test.expect, tplContext.tpl.DefinedTemplates())
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if absFilePath != "" {
|
if absFilePath != "" {
|
||||||
if err := os.Remove(absFilePath); err != nil && !os.IsNotExist(err) {
|
if err := os.Remove(absFilePath); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||||
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
|
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -255,21 +255,20 @@ func TestNestedInclude(t *testing.T) {
|
||||||
} else if buf.String() != test.expect {
|
} else if buf.String() != test.expect {
|
||||||
//
|
//
|
||||||
t.Errorf("Test %d: Expected '%s' but got '%s'", i, test.expect, buf.String())
|
t.Errorf("Test %d: Expected '%s' but got '%s'", i, test.expect, buf.String())
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if absFilePath != "" {
|
if absFilePath != "" {
|
||||||
if err := os.Remove(absFilePath); err != nil && !os.IsNotExist(err) {
|
if err := os.Remove(absFilePath); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||||
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
|
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if absFilePath0 != "" {
|
if absFilePath0 != "" {
|
||||||
if err := os.Remove(absFilePath0); err != nil && !os.IsNotExist(err) {
|
if err := os.Remove(absFilePath0); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||||
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
|
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if absFilePath1 != "" {
|
if absFilePath1 != "" {
|
||||||
if err := os.Remove(absFilePath1); err != nil && !os.IsNotExist(err) {
|
if err := os.Remove(absFilePath1); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||||
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
|
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -342,11 +341,10 @@ func TestInclude(t *testing.T) {
|
||||||
t.Errorf("Test %d: Expected error but had none", i)
|
t.Errorf("Test %d: Expected error but had none", i)
|
||||||
} else if actual != test.expect {
|
} else if actual != test.expect {
|
||||||
t.Errorf("Test %d: Expected %s but got %s", i, test.expect, actual)
|
t.Errorf("Test %d: Expected %s but got %s", i, test.expect, actual)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if absFilePath != "" {
|
if absFilePath != "" {
|
||||||
if err := os.Remove(absFilePath); err != nil && !os.IsNotExist(err) {
|
if err := os.Remove(absFilePath); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||||
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
|
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -460,7 +458,7 @@ func TestFileListing(t *testing.T) {
|
||||||
fileNames: nil,
|
fileNames: nil,
|
||||||
inputBase: "doesNotExist",
|
inputBase: "doesNotExist",
|
||||||
shouldErr: true,
|
shouldErr: true,
|
||||||
verifyErr: os.IsNotExist,
|
verifyErr: func(err error) bool { return errors.Is(err, fs.ErrNotExist) },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
// directory and files exist, but path to a file
|
// directory and files exist, but path to a file
|
||||||
|
@ -476,7 +474,7 @@ func TestFileListing(t *testing.T) {
|
||||||
fileNames: nil,
|
fileNames: nil,
|
||||||
inputBase: filepath.Join("..", "..", "..", "..", "..", "etc"),
|
inputBase: filepath.Join("..", "..", "..", "..", "..", "etc"),
|
||||||
shouldErr: true,
|
shouldErr: true,
|
||||||
verifyErr: os.IsNotExist,
|
verifyErr: func(err error) bool { return errors.Is(err, fs.ErrNotExist) },
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
tplContext := getContextOrFail(t)
|
tplContext := getContextOrFail(t)
|
||||||
|
@ -525,7 +523,7 @@ func TestFileListing(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if dirPath != "" {
|
if dirPath != "" {
|
||||||
if err := os.RemoveAll(dirPath); err != nil && !os.IsNotExist(err) {
|
if err := os.RemoveAll(dirPath); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||||
t.Fatalf("Test %d: Expected no error removing temporary test directory, got: %v", i, err)
|
t.Fatalf("Test %d: Expected no error removing temporary test directory, got: %v", i, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -602,7 +600,6 @@ title = "Welcome"
|
||||||
t.Errorf("Test %d: Expected body %s, found %s. Input was SplitFrontMatter(%s)", i, test.body, result.Body, test.input)
|
t.Errorf("Test %d: Expected body %s, found %s. Input was SplitFrontMatter(%s)", i, test.body, result.Body, test.input)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHumanize(t *testing.T) {
|
func TestHumanize(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue