feat: [CODE-2531]: replace os.IsNotExist(err) with error.Is(err, fs.ErrNotExist) (#3036)

* replace os.IsNotExist(err) with error.Is(err, fs.ErrNotExist)
pull/3597/head
Marko Gaćeša 2024-11-22 12:17:56 +00:00 committed by Harness
parent f1eb44a30a
commit ad8f74ce71
7 changed files with 27 additions and 18 deletions

View File

@ -16,8 +16,10 @@ package blob
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"io" "io"
"io/fs"
"os" "os"
"path" "path"
@ -45,7 +47,7 @@ func (c FileSystemStore) Upload(ctx context.Context,
fileDiskPath := fmt.Sprintf(fileDiskPathFmt, c.basePath, filePath) fileDiskPath := fmt.Sprintf(fileDiskPathFmt, c.basePath, filePath)
dir, _ := path.Split(fileDiskPath) dir, _ := path.Split(fileDiskPath)
if _, err := os.Stat(dir); os.IsNotExist(err) { if _, err := os.Stat(dir); errors.Is(err, fs.ErrNotExist) {
err = os.MkdirAll(dir, os.ModeDir|os.ModePerm) err = os.MkdirAll(dir, os.ModeDir|os.ModePerm)
if err != nil { if err != nil {
return fmt.Errorf("failed to create parent directory for the file: %w", err) return fmt.Errorf("failed to create parent directory for the file: %w", err)
@ -87,7 +89,7 @@ func (c *FileSystemStore) Download(_ context.Context, filePath string) (io.ReadC
fileDiskPath := fmt.Sprintf(fileDiskPathFmt, c.basePath, filePath) fileDiskPath := fmt.Sprintf(fileDiskPathFmt, c.basePath, filePath)
file, err := os.Open(fileDiskPath) file, err := os.Open(fileDiskPath)
if os.IsNotExist(err) { if errors.Is(err, fs.ErrNotExist) {
return nil, ErrNotFound return nil, ErrNotFound
} }
if err != nil { if err != nil {

View File

@ -18,6 +18,7 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"io/fs"
"os" "os"
"time" "time"
@ -328,7 +329,7 @@ func catFileBatchCheckAllObjects(
// --batch-all-objects reports objects in the current repository and in all alternate directories. // --batch-all-objects reports objects in the current repository and in all alternate directories.
// We want to report objects in the current repository only. // We want to report objects in the current repository only.
if err := os.Rename(gitObjDir+oldFilename, gitObjDir+newFilename); err != nil && !os.IsNotExist(err) { if err := os.Rename(gitObjDir+oldFilename, gitObjDir+newFilename); err != nil && !errors.Is(err, fs.ErrNotExist) {
return nil, fmt.Errorf("failed to rename %s to %s: %w", oldFilename, newFilename, err) return nil, fmt.Errorf("failed to rename %s to %s: %w", oldFilename, newFilename, err)
} }
@ -354,7 +355,7 @@ func catFileBatchCheckAllObjects(
return nil, fmt.Errorf("failed to parse output of cat-file batch check all objects: %w", err) return nil, fmt.Errorf("failed to parse output of cat-file batch check all objects: %w", err)
} }
if err := os.Rename(gitObjDir+newFilename, gitObjDir+oldFilename); err != nil && !os.IsNotExist(err) { if err := os.Rename(gitObjDir+newFilename, gitObjDir+oldFilename); err != nil && !errors.Is(err, fs.ErrNotExist) {
return nil, fmt.Errorf("failed to rename %s to %s: %w", newFilename, oldFilename, err) return nil, fmt.Errorf("failed to rename %s to %s: %w", newFilename, oldFilename, err)
} }

View File

@ -18,6 +18,7 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/fs"
"os" "os"
"path" "path"
"runtime/debug" "runtime/debug"
@ -219,7 +220,7 @@ func (s *Service) DeleteRepository(ctx context.Context, params *DeleteRepository
} }
repoPath := getFullPathForRepo(s.reposRoot, params.RepoUID) repoPath := getFullPathForRepo(s.reposRoot, params.RepoUID)
if _, err := os.Stat(repoPath); err != nil && os.IsNotExist(err) { if _, err := os.Stat(repoPath); err != nil && errors.Is(err, fs.ErrNotExist) {
return errors.NotFound("repository path not found") return errors.NotFound("repository path not found")
} else if err != nil { } else if err != nil {
return fmt.Errorf("failed to check the status of the repository %v: %w", repoPath, err) return fmt.Errorf("failed to check the status of the repository %v: %w", repoPath, err)
@ -233,7 +234,7 @@ func (s *Service) DeleteRepositoryBestEffort(ctx context.Context, repoUID string
tempPath := path.Join(s.reposGraveyard, repoUID) tempPath := path.Join(s.reposGraveyard, repoUID)
// delete should not fail if repoGraveyard dir does not exist. // delete should not fail if repoGraveyard dir does not exist.
if _, err := os.Stat(s.reposGraveyard); os.IsNotExist(err) { if _, err := os.Stat(s.reposGraveyard); errors.Is(err, fs.ErrNotExist) {
if errdir := os.MkdirAll(s.reposGraveyard, fileMode700); errdir != nil { if errdir := os.MkdirAll(s.reposGraveyard, fileMode700); errdir != nil {
return fmt.Errorf("clean up dir '%s' doesn't exist and can't be created: %w", s.reposGraveyard, errdir) return fmt.Errorf("clean up dir '%s' doesn't exist and can't be created: %w", s.reposGraveyard, errdir)
} }
@ -241,7 +242,7 @@ func (s *Service) DeleteRepositoryBestEffort(ctx context.Context, repoUID string
// move current dir to a temp dir (prevent partial deletion) // move current dir to a temp dir (prevent partial deletion)
err := os.Rename(repoPath, tempPath) err := os.Rename(repoPath, tempPath)
if os.IsNotExist(err) { if errors.Is(err, fs.ErrNotExist) {
return nil // repository directory doesn't exist - nothing to do return nil // repository directory doesn't exist - nothing to do
} }
if err != nil { if err != nil {
@ -267,11 +268,11 @@ func (s *Service) SyncRepository(
// create repo if requested // create repo if requested
_, err := os.Stat(repoPath) _, err := os.Stat(repoPath)
if err != nil && !os.IsNotExist(err) { if err != nil && !errors.Is(err, fs.ErrNotExist) {
return nil, errors.Internal(err, "failed to create repository") return nil, errors.Internal(err, "failed to create repository")
} }
if os.IsNotExist(err) { if errors.Is(err, fs.ErrNotExist) {
if !params.CreateIfNotExists { if !params.CreateIfNotExists {
return nil, errors.NotFound("repository not found") return nil, errors.NotFound("repository not found")
} }
@ -410,7 +411,7 @@ func (s *Service) createRepositoryInternal(
) error { ) error {
log := log.Ctx(ctx) log := log.Ctx(ctx)
repoPath := getFullPathForRepo(s.reposRoot, base.RepoUID) repoPath := getFullPathForRepo(s.reposRoot, base.RepoUID)
if _, err := os.Stat(repoPath); !os.IsNotExist(err) { if _, err := os.Stat(repoPath); !errors.Is(err, fs.ErrNotExist) {
return errors.Conflict("repository already exists at path %q", repoPath) return errors.Conflict("repository already exists at path %q", repoPath)
} }

View File

@ -15,6 +15,7 @@
package git package git
import ( import (
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
@ -57,7 +58,7 @@ func New(
// create a temp dir for deleted repositories // create a temp dir for deleted repositories
// this dir should get cleaned up peridocally if it's not empty // this dir should get cleaned up peridocally if it's not empty
reposGraveyard := filepath.Join(config.Root, ReposGraveyardSubdirName) reposGraveyard := filepath.Join(config.Root, ReposGraveyardSubdirName)
if _, errdir := os.Stat(reposGraveyard); os.IsNotExist(errdir) { if _, errdir := os.Stat(reposGraveyard); errors.Is(errdir, fs.ErrNotExist) {
if errdir = os.MkdirAll(reposGraveyard, fileMode700); errdir != nil { if errdir = os.MkdirAll(reposGraveyard, fileMode700); errdir != nil {
return nil, errdir return nil, errdir
} }

View File

@ -15,7 +15,9 @@
package tempdir package tempdir
import ( import (
"errors"
"fmt" "fmt"
"io/fs"
"os" "os"
) )
@ -35,7 +37,7 @@ func CreateTemporaryPath(reposTempPath, prefix string) (string, error) {
// RemoveTemporaryPath removes the temporary path. // RemoveTemporaryPath removes the temporary path.
func RemoveTemporaryPath(basePath string) error { func RemoveTemporaryPath(basePath string) error {
if _, err := os.Stat(basePath); !os.IsNotExist(err) { if _, err := os.Stat(basePath); !errors.Is(err, fs.ErrNotExist) {
return os.RemoveAll(basePath) return os.RemoveAll(basePath)
} }
return nil return nil

View File

@ -23,6 +23,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/fs"
"os" "os"
"path" "path"
"time" "time"
@ -181,7 +182,7 @@ func (d *driver) Reader(ctx context.Context, path string, offset int64) (io.Read
file, err := os.OpenFile(d.fullPath(path), os.O_RDONLY, 0o644) file, err := os.OpenFile(d.fullPath(path), os.O_RDONLY, 0o644)
log.Ctx(ctx).Info().Msgf("Opening file %s %s", d.fullPath(path), d.rootDirectory) log.Ctx(ctx).Info().Msgf("Opening file %s %s", d.fullPath(path), d.rootDirectory)
if err != nil { if err != nil {
if os.IsNotExist(err) { if errors.Is(err, fs.ErrNotExist) {
return nil, storagedriver.PathNotFoundError{Path: path} return nil, storagedriver.PathNotFoundError{Path: path}
} }
@ -239,7 +240,7 @@ func (d *driver) Stat(_ context.Context, subPath string) (storagedriver.FileInfo
fi, err := os.Stat(fullPath) fi, err := os.Stat(fullPath)
if err != nil { if err != nil {
if os.IsNotExist(err) { if errors.Is(err, fs.ErrNotExist) {
return nil, storagedriver.PathNotFoundError{Path: subPath} return nil, storagedriver.PathNotFoundError{Path: subPath}
} }
@ -259,7 +260,7 @@ func (d *driver) List(_ context.Context, subPath string) ([]string, error) {
dir, err := os.Open(fullPath) dir, err := os.Open(fullPath)
if err != nil { if err != nil {
if os.IsNotExist(err) { if errors.Is(err, fs.ErrNotExist) {
return nil, storagedriver.PathNotFoundError{Path: subPath} return nil, storagedriver.PathNotFoundError{Path: subPath}
} }
return nil, err return nil, err
@ -286,7 +287,7 @@ func (d *driver) Move(_ context.Context, sourcePath string, destPath string) err
source := d.fullPath(sourcePath) source := d.fullPath(sourcePath)
dest := d.fullPath(destPath) dest := d.fullPath(destPath)
if _, err := os.Stat(source); os.IsNotExist(err) { if _, err := os.Stat(source); errors.Is(err, fs.ErrNotExist) {
return storagedriver.PathNotFoundError{Path: sourcePath} return storagedriver.PathNotFoundError{Path: sourcePath}
} }
@ -303,7 +304,7 @@ func (d *driver) Delete(_ context.Context, subPath string) error {
fullPath := d.fullPath(subPath) fullPath := d.fullPath(subPath)
_, err := os.Stat(fullPath) _, err := os.Stat(fullPath)
if err != nil && !os.IsNotExist(err) { if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err return err
} else if err != nil { } else if err != nil {
return storagedriver.PathNotFoundError{Path: subPath} return storagedriver.PathNotFoundError{Path: subPath}

View File

@ -22,6 +22,7 @@ import (
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"io" "io"
"io/fs"
"net" "net"
"os" "os"
"path/filepath" "path/filepath"
@ -359,7 +360,7 @@ func createKeyIfNotExists(path string) error {
// if the path already exists there's nothing we have to do // if the path already exists there's nothing we have to do
return nil return nil
} }
if !os.IsNotExist(err) { if !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("failed to check for for existence of key: %w", err) return fmt.Errorf("failed to check for for existence of key: %w", err)
} }