From 3409f5a27f0e34eab672f8af233b28b486b617f6 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Sun, 14 Apr 2024 13:33:52 +0200 Subject: [PATCH] Replace uses of os.IsExist and os.IsNotExist with errors.Is. The former don't properly unwrap errors. --- diskwriter/diskwriter.go | 2 +- group/description.go | 4 ++-- group/group.go | 4 ++-- ice/ice.go | 2 +- rtpconn/webclient.go | 4 ++-- token/stateful.go | 10 +++++----- token/stateful_test.go | 3 ++- webserver/api.go | 5 +++-- webserver/api_test.go | 2 +- webserver/webserver.go | 4 ++-- 10 files changed, 21 insertions(+), 19 deletions(-) diff --git a/diskwriter/diskwriter.go b/diskwriter/diskwriter.go index 02233b4..8e0a240 100644 --- a/diskwriter/diskwriter.go +++ b/diskwriter/diskwriter.go @@ -264,7 +264,7 @@ func openDiskFile(directory, username, extension string) (*os.File, error) { ) if err == nil { return f, nil - } else if !os.IsExist(err) { + } else if !errors.Is(err, os.ErrExist) { return nil, err } } diff --git a/group/description.go b/group/description.go index 53671d6..0d5a080 100644 --- a/group/description.go +++ b/group/description.go @@ -232,7 +232,7 @@ func getDescriptionFile[T any](name string, allowSubgroups bool, get func(string Directory, path.Clean("/"+name)+".json", ) r, err := get(fileName) - if !os.IsNotExist(err) { + if !errors.Is(err, os.ErrNotExist) { return r, fileName, isSubgroup, err } if !allowSubgroups { @@ -348,7 +348,7 @@ func UpdateDescription(name, etag string, desc *Description) error { if err == nil { oldetag = makeETag(old.fileSize, old.modTime) filename = old.FileName - } else if os.IsNotExist(err) { + } else if errors.Is(err, os.ErrNotExist) { old = nil filename = filepath.Join( Directory, path.Clean("/"+name)+".json", diff --git a/group/group.go b/group/group.go index e92a990..2e899f9 100644 --- a/group/group.go +++ b/group/group.go @@ -474,7 +474,7 @@ func add(name string, desc *Description) (*Group, []Client, error) { } else if !descriptionUnchanged(name, g.description) { desc, err = readDescription(name, true) if err != nil { - if !os.IsNotExist(err) { + if !errors.Is(err, os.ErrNotExist) { log.Printf("Reading group %v: %v", name, err) } deleteUnlocked(g) @@ -886,7 +886,7 @@ func GetConfiguration() (*Configuration, error) { filename := filepath.Join(DataDirectory, "config.json") fi, err := os.Stat(filename) if err != nil { - if os.IsNotExist(err) { + if errors.Is(err, os.ErrNotExist) { if !configuration.configuration.Zero() { configuration.configuration = &Configuration{} } diff --git a/ice/ice.go b/ice/ice.go index 9942781..e299b54 100644 --- a/ice/ice.go +++ b/ice/ice.go @@ -95,7 +95,7 @@ func Update() *configuration { found = true file, err := os.Open(ICEFilename) if err != nil { - if !os.IsNotExist(err) { + if !errors.Is(err, os.ErrNotExist) { log.Printf("Open %v: %v", ICEFilename, err) } else { found = false diff --git a/rtpconn/webclient.go b/rtpconn/webclient.go index 5d6aae8..178509c 100644 --- a/rtpconn/webclient.go +++ b/rtpconn/webclient.go @@ -1299,7 +1299,7 @@ func leaveGroup(c *webClient) { func closeDownConn(c *webClient, id string, message string) error { err := delDownConn(c, id) - if err != nil && !os.IsNotExist(err) { + if err != nil && !errors.Is(err, os.ErrNotExist) { log.Printf("Close down connection: %v", err) } err = c.write(clientMessage{ @@ -1413,7 +1413,7 @@ func handleClientMessage(c *webClient, m clientMessage) error { if err != nil { var e, s string var autherr *group.NotAuthorisedError - if os.IsNotExist(err) { + if errors.Is(err, os.ErrNotExist) { s = "group does not exist" } else if errors.Is(err, group.ErrAnonymousNotAuthorised) { s = "please choose a username" diff --git a/token/stateful.go b/token/stateful.go index 45f1813..1d0fb4b 100644 --- a/token/stateful.go +++ b/token/stateful.go @@ -62,7 +62,7 @@ func getStateful(token string) (*Stateful, error) { defer tokens.mu.Unlock() err := tokens.load() if err != nil { - if os.IsNotExist(err) { + if errors.Is(err, os.ErrNotExist) { return nil, nil } return nil, err @@ -109,7 +109,7 @@ func (state *state) load() error { state.modTime = time.Time{} state.fileSize = 0 state.tokens = nil - if os.IsNotExist(err) { + if errors.Is(err, os.ErrNotExist) { return nil } return err @@ -125,7 +125,7 @@ func (state *state) load() error { state.modTime = time.Time{} state.fileSize = 0 state.tokens = nil - if os.IsNotExist(err) { + if errors.Is(err, os.ErrNotExist) { return nil } return err @@ -153,7 +153,7 @@ func (state *state) load() error { state.modTime = time.Time{} state.fileSize = 0 state.tokens = nil - if os.IsNotExist(err) { + if errors.Is(err, os.ErrNotExist) { return nil } return err @@ -262,7 +262,7 @@ func (state *state) extend(group, token string, expires time.Time) (*Stateful, e func (state *state) rewrite() error { if state.tokens == nil || len(state.tokens) == 0 { err := os.Remove(state.filename) - if err == nil || os.IsNotExist(err) { + if err == nil || errors.Is(err, os.ErrNotExist) { return nil } return err diff --git a/token/stateful_test.go b/token/stateful_test.go index 9a88179..c98873c 100644 --- a/token/stateful_test.go +++ b/token/stateful_test.go @@ -2,6 +2,7 @@ package token import ( "encoding/json" + "errors" "io" "os" "path/filepath" @@ -294,7 +295,7 @@ func TestTokenStorage(t *testing.T) { } _, err = os.Stat(s.filename) - if !os.IsNotExist(err) { + if !errors.Is(err, os.ErrNotExist) { t.Errorf("existence check: %v", err) } } diff --git a/webserver/api.go b/webserver/api.go index 0d0ba0e..b497230 100644 --- a/webserver/api.go +++ b/webserver/api.go @@ -5,6 +5,7 @@ import ( "crypto/sha256" "encoding/hex" "encoding/json" + "errors" "fmt" "io" "net/http" @@ -170,7 +171,7 @@ func apiGroupHandler(w http.ResponseWriter, r *http.Request, pth string) { return } else if r.Method == "PUT" { etag, err := group.GetDescriptionTag(g) - if os.IsNotExist(err) { + if errors.Is(err, os.ErrNotExist) { err = nil etag = "" } else if err != nil { @@ -299,7 +300,7 @@ func usersHandler(w http.ResponseWriter, r *http.Request, g, pth string) { return } else if r.Method == "PUT" { etag, err := group.GetUserTag(g, username) - if os.IsNotExist(err) { + if errors.Is(err, os.ErrNotExist) { etag = "" err = nil } else if err != nil { diff --git a/webserver/api_test.go b/webserver/api_test.go index 70ee9fb..a4d18e3 100644 --- a/webserver/api_test.go +++ b/webserver/api_test.go @@ -291,7 +291,7 @@ func TestApi(t *testing.T) { } _, err = group.GetDescription("test") - if !os.IsNotExist(err) { + if !errors.Is(err, os.ErrNotExist) { t.Errorf("Group exists after delete") } } diff --git a/webserver/webserver.go b/webserver/webserver.go index 7e7ace7..a8914ec 100644 --- a/webserver/webserver.go +++ b/webserver/webserver.go @@ -120,7 +120,7 @@ func notFound(w http.ResponseWriter) { var ErrIsDirectory = errors.New("is a directory") func httpError(w http.ResponseWriter, err error) { - if os.IsNotExist(err) { + if errors.Is(err, os.ErrNotExist) { notFound(w) return } @@ -235,7 +235,7 @@ func (fh *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ff, err := fh.root.Open(index) if err != nil { // return 403 if index.html doesn't exist - if os.IsNotExist(err) { + if errors.Is(err, os.ErrNotExist) { http.Error(w, "Forbidden", http.StatusForbidden) return }