From ad8f74ce716ed35c05623e83552110457760afe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Ga=C4=87e=C5=A1a?= Date: Fri, 22 Nov 2024 12:17:56 +0000 Subject: [PATCH] feat: [CODE-2531]: replace os.IsNotExist(err) with error.Is(err, fs.ErrNotExist) (#3036) * replace os.IsNotExist(err) with error.Is(err, fs.ErrNotExist) --- blob/filesystem.go | 6 ++++-- git/commit.go | 5 +++-- git/repo.go | 13 +++++++------ git/service.go | 3 ++- git/tempdir/file.go | 4 +++- registry/app/driver/filesystem/driver.go | 11 ++++++----- ssh/server.go | 3 ++- 7 files changed, 27 insertions(+), 18 deletions(-) diff --git a/blob/filesystem.go b/blob/filesystem.go index 3ecaed550..0fb4e0638 100644 --- a/blob/filesystem.go +++ b/blob/filesystem.go @@ -16,8 +16,10 @@ package blob import ( "context" + "errors" "fmt" "io" + "io/fs" "os" "path" @@ -45,7 +47,7 @@ func (c FileSystemStore) Upload(ctx context.Context, fileDiskPath := fmt.Sprintf(fileDiskPathFmt, c.basePath, filePath) 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) if err != nil { 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) file, err := os.Open(fileDiskPath) - if os.IsNotExist(err) { + if errors.Is(err, fs.ErrNotExist) { return nil, ErrNotFound } if err != nil { diff --git a/git/commit.go b/git/commit.go index 234a20d25..315ffbe5b 100644 --- a/git/commit.go +++ b/git/commit.go @@ -18,6 +18,7 @@ import ( "bytes" "context" "fmt" + "io/fs" "os" "time" @@ -328,7 +329,7 @@ func catFileBatchCheckAllObjects( // --batch-all-objects reports objects in the current repository and in all alternate directories. // 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) } @@ -354,7 +355,7 @@ func catFileBatchCheckAllObjects( 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) } diff --git a/git/repo.go b/git/repo.go index d89fc7886..1626a5c5b 100644 --- a/git/repo.go +++ b/git/repo.go @@ -18,6 +18,7 @@ import ( "context" "fmt" "io" + "io/fs" "os" "path" "runtime/debug" @@ -219,7 +220,7 @@ func (s *Service) DeleteRepository(ctx context.Context, params *DeleteRepository } 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") } else if err != nil { 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) // 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 { 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) 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 } if err != nil { @@ -267,11 +268,11 @@ func (s *Service) SyncRepository( // create repo if requested _, 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") } - if os.IsNotExist(err) { + if errors.Is(err, fs.ErrNotExist) { if !params.CreateIfNotExists { return nil, errors.NotFound("repository not found") } @@ -410,7 +411,7 @@ func (s *Service) createRepositoryInternal( ) error { log := log.Ctx(ctx) 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) } diff --git a/git/service.go b/git/service.go index 5fdbc7431..1fd695832 100644 --- a/git/service.go +++ b/git/service.go @@ -15,6 +15,7 @@ package git import ( + "io/fs" "os" "path/filepath" @@ -57,7 +58,7 @@ func New( // create a temp dir for deleted repositories // this dir should get cleaned up peridocally if it's not empty 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 { return nil, errdir } diff --git a/git/tempdir/file.go b/git/tempdir/file.go index 50f5831c4..c09fd269f 100644 --- a/git/tempdir/file.go +++ b/git/tempdir/file.go @@ -15,7 +15,9 @@ package tempdir import ( + "errors" "fmt" + "io/fs" "os" ) @@ -35,7 +37,7 @@ func CreateTemporaryPath(reposTempPath, prefix string) (string, error) { // RemoveTemporaryPath removes the temporary path. 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 nil diff --git a/registry/app/driver/filesystem/driver.go b/registry/app/driver/filesystem/driver.go index 38c58d031..a6442db17 100644 --- a/registry/app/driver/filesystem/driver.go +++ b/registry/app/driver/filesystem/driver.go @@ -23,6 +23,7 @@ import ( "errors" "fmt" "io" + "io/fs" "os" "path" "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) log.Ctx(ctx).Info().Msgf("Opening file %s %s", d.fullPath(path), d.rootDirectory) if err != nil { - if os.IsNotExist(err) { + if errors.Is(err, fs.ErrNotExist) { 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) if err != nil { - if os.IsNotExist(err) { + if errors.Is(err, fs.ErrNotExist) { 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) if err != nil { - if os.IsNotExist(err) { + if errors.Is(err, fs.ErrNotExist) { return nil, storagedriver.PathNotFoundError{Path: subPath} } return nil, err @@ -286,7 +287,7 @@ func (d *driver) Move(_ context.Context, sourcePath string, destPath string) err source := d.fullPath(sourcePath) 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} } @@ -303,7 +304,7 @@ func (d *driver) Delete(_ context.Context, subPath string) error { fullPath := d.fullPath(subPath) _, err := os.Stat(fullPath) - if err != nil && !os.IsNotExist(err) { + if err != nil && !errors.Is(err, fs.ErrNotExist) { return err } else if err != nil { return storagedriver.PathNotFoundError{Path: subPath} diff --git a/ssh/server.go b/ssh/server.go index f96205614..178d36567 100644 --- a/ssh/server.go +++ b/ssh/server.go @@ -22,6 +22,7 @@ import ( "encoding/pem" "fmt" "io" + "io/fs" "net" "os" "path/filepath" @@ -359,7 +360,7 @@ func createKeyIfNotExists(path string) error { // if the path already exists there's nothing we have to do 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) }