Skip to content

Commit

Permalink
Merge pull request agola-io#325 from sgotti/replace_os_errors_check_f…
Browse files Browse the repository at this point in the history
…unctions_with_error_is

*: replace os errors check functions with errors.Is
  • Loading branch information
sgotti authored Mar 2, 2022
2 parents 6b286d7 + a47834d commit 0c8d973
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions internal/git-save/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ func copyFile(src, dest string) error {

func fileExists(path string) (bool, error) {
_, err := os.Stat(path)
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, os.ErrNotExist) {
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
Expand Down
10 changes: 5 additions & 5 deletions internal/objectstorage/posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (s *PosixStorage) Stat(p string) (*ObjectInfo, error) {

fi, err := os.Stat(fspath)
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, errors.WithStack(err)
Expand All @@ -80,7 +80,7 @@ func (s *PosixStorage) ReadObject(p string) (ReadSeekCloser, error) {
}

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 f, errors.WithStack(err)
Expand Down Expand Up @@ -113,7 +113,7 @@ func (s *PosixStorage) DeleteObject(p string) error {
}

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 errors.WithStack(err)
Expand Down Expand Up @@ -178,10 +178,10 @@ func (s *PosixStorage) List(prefix, startWith, delimiter string, doneCh <-chan s
go func(objectCh chan<- ObjectInfo) {
defer close(objectCh)
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)
}
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
return nil
}
p := ep
Expand Down
14 changes: 7 additions & 7 deletions internal/objectstorage/posixflat.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func (s *PosixFlatStorage) Stat(p string) (*ObjectInfo, error) {

fi, err := os.Stat(fspath)
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, errors.WithStack(err)
Expand All @@ -254,7 +254,7 @@ func (s *PosixFlatStorage) ReadObject(p string) (ReadSeekCloser, error) {
}

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 f, errors.WithStack(err)
Expand Down Expand Up @@ -287,7 +287,7 @@ func (s *PosixFlatStorage) DeleteObject(p string) error {
}

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 errors.WithStack(err)
Expand Down Expand Up @@ -353,10 +353,10 @@ func (s *PosixFlatStorage) List(prefix, startWith, delimiter string, doneCh <-ch
var prevp string
defer close(objectCh)
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)
}
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
return nil
}
p := ep
Expand Down Expand Up @@ -389,10 +389,10 @@ func (s *PosixFlatStorage) List(prefix, startWith, delimiter string, doneCh <-ch
// just be listed
hasFile := true
_, err = os.Stat(ep + ".f")
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, os.ErrNotExist) {
return errors.WithStack(err)
}
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
hasFile = false
}
if info.IsDir() && !hasFile {
Expand Down
4 changes: 2 additions & 2 deletions internal/services/executor/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
f, err := os.Open(logPath)
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
http.Error(w, "", http.StatusNotFound)
} else {
http.Error(w, "", http.StatusInternalServerError)
Expand Down Expand Up @@ -220,7 +220,7 @@ func (h *archivesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-cache")

if err := h.readArchive(taskID, step, w); err != nil {
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
http.Error(w, "", http.StatusNotFound)
} else {
http.Error(w, "", http.StatusInternalServerError)
Expand Down
2 changes: 1 addition & 1 deletion internal/services/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,7 @@ func (e *Executor) handleTasks(ctx context.Context, c <-chan *types.ExecutorTask

func (e *Executor) getExecutorID() (string, error) {
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 string(id), nil
Expand Down
12 changes: 6 additions & 6 deletions internal/services/gitserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ func repoPathIsValid(reposDir, repoPath string) (bool, error) {

path := repoPath
_, err = os.Stat(path)
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, os.ErrNotExist) {
return false, errors.WithStack(err)
}
if !os.IsNotExist(err) {
if !errors.Is(err, os.ErrNotExist) {
// if it exists assume it's valid
return true, nil
}
Expand All @@ -69,14 +69,14 @@ func repoPathIsValid(reposDir, repoPath string) (bool, error) {
}

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

func repoExists(repoAbsPath string) (bool, error) {
_, err := os.Stat(repoAbsPath)
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, os.ErrNotExist) {
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) {
Expand Down
8 changes: 4 additions & 4 deletions internal/toolbox/unarchive/unarchive.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Unarchive(source io.Reader, destDir string, overwrite, removeDestDir bool)
}
// don't follow destdir if it's a symlink
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")
}
if fi != nil && !fi.IsDir() {
Expand Down Expand Up @@ -92,7 +92,7 @@ func untarNext(tr *tar.Reader, destDir string, overwrite bool) error {
switch hdr.Typeflag {
case tar.TypeDir:
fi, err := os.Lstat(destPath)
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, os.ErrNotExist) {
return errors.WithStack(err)
}
if fi != nil && !fi.IsDir() {
Expand All @@ -103,7 +103,7 @@ func untarNext(tr *tar.Reader, destDir string, overwrite bool) error {
return mkdir(destPath, hdr.FileInfo().Mode())
case tar.TypeReg, tar.TypeRegA, tar.TypeChar, tar.TypeBlock, tar.TypeFifo:
fi, err := os.Lstat(destPath)
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, os.ErrNotExist) {
return errors.WithStack(err)
}
if fi != nil && !fi.Mode().IsRegular() {
Expand Down Expand Up @@ -135,7 +135,7 @@ func untarNext(tr *tar.Reader, destDir string, overwrite bool) error {

func fileExists(name string) bool {
_, err := os.Lstat(name)
return !os.IsNotExist(err)
return !errors.Is(err, os.ErrNotExist)
}

func mkdir(dirPath string, mode os.FileMode) error {
Expand Down

0 comments on commit 0c8d973

Please sign in to comment.