*: replace os errors check functions with errors.Is

Since we're wrapping also internal errors, if a function returns a
wrapped os pkg error (like os.ErrNotExists) and the caller function uses
the os error check functions (like os.IsNotExist) it won't work since
(like explained in the os pkg comment) it won't unwrap the error.
Fix this by using errors.Is checks.
This commit is contained in:
Simone Gotti 2022-03-02 13:23:32 +01:00
parent 6b286d774d
commit a47834da8a
7 changed files with 27 additions and 27 deletions

View File

@ -51,10 +51,10 @@ func copyFile(src, dest string) error {
func fileExists(path string) (bool, error) { func fileExists(path string) (bool, error) {
_, err := os.Stat(path) _, err := os.Stat(path)
if err != nil && !os.IsNotExist(err) { if err != nil && !errors.Is(err, os.ErrNotExist) {
return false, errors.WithStack(err) return false, errors.WithStack(err)
} }
return !os.IsNotExist(err), nil return !errors.Is(err, os.ErrNotExist), nil
} }
// GitDir returns the git dir relative to the working dir // GitDir returns the git dir relative to the working dir

View File

@ -64,7 +64,7 @@ func (s *PosixStorage) Stat(p string) (*ObjectInfo, error) {
fi, err := os.Stat(fspath) fi, err := os.Stat(fspath)
if err != nil { if err != nil {
if os.IsNotExist(err) { if errors.Is(err, os.ErrNotExist) {
return nil, NewErrNotExist(errors.Errorf("object %q doesn't exist", p)) return nil, NewErrNotExist(errors.Errorf("object %q doesn't exist", p))
} }
return nil, errors.WithStack(err) return nil, errors.WithStack(err)
@ -80,7 +80,7 @@ func (s *PosixStorage) ReadObject(p string) (ReadSeekCloser, error) {
} }
f, err := os.Open(fspath) f, err := os.Open(fspath)
if err != nil && os.IsNotExist(err) { if err != nil && errors.Is(err, os.ErrNotExist) {
return nil, NewErrNotExist(errors.Errorf("object %q doesn't exist", p)) return nil, NewErrNotExist(errors.Errorf("object %q doesn't exist", p))
} }
return f, errors.WithStack(err) return f, errors.WithStack(err)
@ -113,7 +113,7 @@ func (s *PosixStorage) DeleteObject(p string) error {
} }
if err := os.Remove(fspath); err != nil { if err := os.Remove(fspath); err != nil {
if os.IsNotExist(err) { if errors.Is(err, os.ErrNotExist) {
return NewErrNotExist(errors.Errorf("object %q doesn't exist", p)) return NewErrNotExist(errors.Errorf("object %q doesn't exist", p))
} }
return errors.WithStack(err) return errors.WithStack(err)
@ -178,10 +178,10 @@ func (s *PosixStorage) List(prefix, startWith, delimiter string, doneCh <-chan s
go func(objectCh chan<- ObjectInfo) { go func(objectCh chan<- ObjectInfo) {
defer close(objectCh) defer close(objectCh)
err := filepath.Walk(root, func(ep string, info os.FileInfo, err error) error { err := filepath.Walk(root, func(ep string, info os.FileInfo, err error) error {
if err != nil && !os.IsNotExist(err) { if err != nil && !errors.Is(err, os.ErrNotExist) {
return errors.WithStack(err) return errors.WithStack(err)
} }
if os.IsNotExist(err) { if errors.Is(err, os.ErrNotExist) {
return nil return nil
} }
p := ep p := ep

View File

@ -238,7 +238,7 @@ func (s *PosixFlatStorage) Stat(p string) (*ObjectInfo, error) {
fi, err := os.Stat(fspath) fi, err := os.Stat(fspath)
if err != nil { if err != nil {
if os.IsNotExist(err) { if errors.Is(err, os.ErrNotExist) {
return nil, NewErrNotExist(errors.Errorf("object %q doesn't exist", p)) return nil, NewErrNotExist(errors.Errorf("object %q doesn't exist", p))
} }
return nil, errors.WithStack(err) return nil, errors.WithStack(err)
@ -254,7 +254,7 @@ func (s *PosixFlatStorage) ReadObject(p string) (ReadSeekCloser, error) {
} }
f, err := os.Open(fspath) f, err := os.Open(fspath)
if err != nil && os.IsNotExist(err) { if err != nil && errors.Is(err, os.ErrNotExist) {
return nil, NewErrNotExist(errors.Errorf("object %q doesn't exist", p)) return nil, NewErrNotExist(errors.Errorf("object %q doesn't exist", p))
} }
return f, errors.WithStack(err) return f, errors.WithStack(err)
@ -287,7 +287,7 @@ func (s *PosixFlatStorage) DeleteObject(p string) error {
} }
if err := os.Remove(fspath); err != nil { if err := os.Remove(fspath); err != nil {
if os.IsNotExist(err) { if errors.Is(err, os.ErrNotExist) {
return NewErrNotExist(errors.Errorf("object %q doesn't exist", p)) return NewErrNotExist(errors.Errorf("object %q doesn't exist", p))
} }
return errors.WithStack(err) return errors.WithStack(err)
@ -353,10 +353,10 @@ func (s *PosixFlatStorage) List(prefix, startWith, delimiter string, doneCh <-ch
var prevp string var prevp string
defer close(objectCh) defer close(objectCh)
err := filepath.Walk(root, func(ep string, info os.FileInfo, err error) error { err := filepath.Walk(root, func(ep string, info os.FileInfo, err error) error {
if err != nil && !os.IsNotExist(err) { if err != nil && !errors.Is(err, os.ErrNotExist) {
return errors.WithStack(err) return errors.WithStack(err)
} }
if os.IsNotExist(err) { if errors.Is(err, os.ErrNotExist) {
return nil return nil
} }
p := ep p := ep
@ -389,10 +389,10 @@ func (s *PosixFlatStorage) List(prefix, startWith, delimiter string, doneCh <-ch
// just be listed // just be listed
hasFile := true hasFile := true
_, err = os.Stat(ep + ".f") _, err = os.Stat(ep + ".f")
if err != nil && !os.IsNotExist(err) { if err != nil && !errors.Is(err, os.ErrNotExist) {
return errors.WithStack(err) return errors.WithStack(err)
} }
if os.IsNotExist(err) { if errors.Is(err, os.ErrNotExist) {
hasFile = false hasFile = false
} }
if info.IsDir() && !hasFile { if info.IsDir() && !hasFile {

View File

@ -114,7 +114,7 @@ func (h *logsHandler) readTaskLogs(taskID string, setup bool, step int, w http.R
func (h *logsHandler) readLogs(taskID string, setup bool, step int, logPath string, w http.ResponseWriter, follow bool) error { func (h *logsHandler) readLogs(taskID string, setup bool, step int, logPath string, w http.ResponseWriter, follow bool) error {
f, err := os.Open(logPath) f, err := os.Open(logPath)
if err != nil { if err != nil {
if os.IsNotExist(err) { if errors.Is(err, os.ErrNotExist) {
http.Error(w, "", http.StatusNotFound) http.Error(w, "", http.StatusNotFound)
} else { } else {
http.Error(w, "", http.StatusInternalServerError) http.Error(w, "", http.StatusInternalServerError)
@ -220,7 +220,7 @@ func (h *archivesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-cache") w.Header().Set("Cache-Control", "no-cache")
if err := h.readArchive(taskID, step, w); err != nil { if err := h.readArchive(taskID, step, w); err != nil {
if os.IsNotExist(err) { if errors.Is(err, os.ErrNotExist) {
http.Error(w, "", http.StatusNotFound) http.Error(w, "", http.StatusNotFound)
} else { } else {
http.Error(w, "", http.StatusInternalServerError) http.Error(w, "", http.StatusInternalServerError)

View File

@ -1334,7 +1334,7 @@ func (e *Executor) handleTasks(ctx context.Context, c <-chan *types.ExecutorTask
func (e *Executor) getExecutorID() (string, error) { func (e *Executor) getExecutorID() (string, error) {
id, err := ioutil.ReadFile(e.executorIDPath()) id, err := ioutil.ReadFile(e.executorIDPath())
if err != nil && !os.IsNotExist(err) { if err != nil && !errors.Is(err, os.ErrNotExist) {
return "", errors.WithStack(err) return "", errors.WithStack(err)
} }
return string(id), nil return string(id), nil

View File

@ -54,10 +54,10 @@ func repoPathIsValid(reposDir, repoPath string) (bool, error) {
path := repoPath path := repoPath
_, err = os.Stat(path) _, err = os.Stat(path)
if err != nil && !os.IsNotExist(err) { if err != nil && !errors.Is(err, os.ErrNotExist) {
return false, errors.WithStack(err) return false, errors.WithStack(err)
} }
if !os.IsNotExist(err) { if !errors.Is(err, os.ErrNotExist) {
// if it exists assume it's valid // if it exists assume it's valid
return true, nil return true, nil
} }
@ -69,14 +69,14 @@ func repoPathIsValid(reposDir, repoPath string) (bool, error) {
} }
_, err := os.Stat(path) _, err := os.Stat(path)
if err != nil && !os.IsNotExist(err) { if err != nil && !errors.Is(err, os.ErrNotExist) {
return false, errors.WithStack(err) return false, errors.WithStack(err)
} }
// a parent path cannot end with .git // a parent path cannot end with .git
if strings.HasSuffix(path, gitSuffix) { if strings.HasSuffix(path, gitSuffix) {
return false, nil return false, nil
} }
if !os.IsNotExist(err) { if !errors.Is(err, os.ErrNotExist) {
// if a parent exists return not valid // if a parent exists return not valid
return false, nil return false, nil
} }
@ -87,10 +87,10 @@ func repoPathIsValid(reposDir, repoPath string) (bool, error) {
func repoExists(repoAbsPath string) (bool, error) { func repoExists(repoAbsPath string) (bool, error) {
_, err := os.Stat(repoAbsPath) _, err := os.Stat(repoAbsPath)
if err != nil && !os.IsNotExist(err) { if err != nil && !errors.Is(err, os.ErrNotExist) {
return false, errors.WithStack(err) return false, errors.WithStack(err)
} }
return !os.IsNotExist(err), nil return !errors.Is(err, os.ErrNotExist), nil
} }
func repoAbsPath(reposDir, repoPath string) (string, bool, error) { func repoAbsPath(reposDir, repoPath string) (string, bool, error) {

View File

@ -37,7 +37,7 @@ func Unarchive(source io.Reader, destDir string, overwrite, removeDestDir bool)
} }
// don't follow destdir if it's a symlink // don't follow destdir if it's a symlink
fi, err := os.Lstat(destDir) fi, err := os.Lstat(destDir)
if err != nil && !os.IsNotExist(err) { if err != nil && !errors.Is(err, os.ErrNotExist) {
return errors.Wrapf(err, "failed to lstat destination dir") return errors.Wrapf(err, "failed to lstat destination dir")
} }
if fi != nil && !fi.IsDir() { if fi != nil && !fi.IsDir() {
@ -92,7 +92,7 @@ func untarNext(tr *tar.Reader, destDir string, overwrite bool) error {
switch hdr.Typeflag { switch hdr.Typeflag {
case tar.TypeDir: case tar.TypeDir:
fi, err := os.Lstat(destPath) fi, err := os.Lstat(destPath)
if err != nil && !os.IsNotExist(err) { if err != nil && !errors.Is(err, os.ErrNotExist) {
return errors.WithStack(err) return errors.WithStack(err)
} }
if fi != nil && !fi.IsDir() { if fi != nil && !fi.IsDir() {
@ -103,7 +103,7 @@ func untarNext(tr *tar.Reader, destDir string, overwrite bool) error {
return mkdir(destPath, hdr.FileInfo().Mode()) return mkdir(destPath, hdr.FileInfo().Mode())
case tar.TypeReg, tar.TypeRegA, tar.TypeChar, tar.TypeBlock, tar.TypeFifo: case tar.TypeReg, tar.TypeRegA, tar.TypeChar, tar.TypeBlock, tar.TypeFifo:
fi, err := os.Lstat(destPath) fi, err := os.Lstat(destPath)
if err != nil && !os.IsNotExist(err) { if err != nil && !errors.Is(err, os.ErrNotExist) {
return errors.WithStack(err) return errors.WithStack(err)
} }
if fi != nil && !fi.Mode().IsRegular() { if fi != nil && !fi.Mode().IsRegular() {
@ -135,7 +135,7 @@ func untarNext(tr *tar.Reader, destDir string, overwrite bool) error {
func fileExists(name string) bool { func fileExists(name string) bool {
_, err := os.Lstat(name) _, err := os.Lstat(name)
return !os.IsNotExist(err) return !errors.Is(err, os.ErrNotExist)
} }
func mkdir(dirPath string, mode os.FileMode) error { func mkdir(dirPath string, mode os.FileMode) error {