diff options
author | zeripath <art27@cantab.net> | 2020-11-28 02:42:08 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-27 21:42:08 -0500 |
commit | 742e21aeba5c02935269a2a3681f4486019ce542 (patch) | |
tree | e1572ab13c33dec1238321170a90d42851ae4ca2 /models | |
parent | 5b75f17043bc2a6d0e753ae5c9c6759adad5aaac (diff) | |
download | gitea-742e21aeba5c02935269a2a3681f4486019ce542.tar.gz gitea-742e21aeba5c02935269a2a3681f4486019ce542.zip |
Handle and propagate errors when checking if paths are Dirs, Files or Exist (#13186)
* Ensure errors from IsDir propagate
* Handle errors when checking IsFile
* Handle and propagate errors from IsExist
* Update modules/templates/static.go
* Update modules/templates/static.go
* Return after ctx.ServerError
* Apply suggestions from code review
* Fix tests
The previous merge managed to break repo_form.go
Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Co-authored-by: Lauris BH <lauris@nix.lv>
Diffstat (limited to 'models')
-rw-r--r-- | models/repo.go | 55 | ||||
-rw-r--r-- | models/ssh_key.go | 40 | ||||
-rw-r--r-- | models/upload.go | 8 | ||||
-rw-r--r-- | models/wiki.go | 9 |
4 files changed, 91 insertions, 21 deletions
diff --git a/models/repo.go b/models/repo.go index 2b53ac666c..7e38bbcdb7 100644 --- a/models/repo.go +++ b/models/repo.go @@ -74,7 +74,11 @@ func loadRepoConfig() { log.Fatal("Failed to get %s files: %v", t, err) } customPath := path.Join(setting.CustomPath, "options", t) - if com.IsDir(customPath) { + isDir, err := util.IsDir(customPath) + if err != nil { + log.Fatal("Failed to get custom %s files: %v", t, err) + } + if isDir { customFiles, err := com.StatDir(customPath) if err != nil { log.Fatal("Failed to get custom %s files: %v", t, err) @@ -1004,7 +1008,11 @@ func isRepositoryExist(e Engine, u *User, repoName string) (bool, error) { OwnerID: u.ID, LowerName: strings.ToLower(repoName), }) - return has && com.IsDir(RepoPath(u.Name, repoName)), err + if err != nil { + return false, err + } + isDir, err := util.IsDir(RepoPath(u.Name, repoName)) + return has && isDir, err } // IsRepositoryExist returns true if the repository with given name under user has already existed. @@ -1078,7 +1086,12 @@ func CheckCreateRepository(doer, u *User, name string, overwriteOrAdopt bool) er return ErrRepoAlreadyExist{u.Name, name} } - if !overwriteOrAdopt && com.IsExist(RepoPath(u.Name, name)) { + isExist, err := util.IsExist(RepoPath(u.Name, name)) + if err != nil { + log.Error("Unable to check if %s exists. Error: %v", RepoPath(u.Name, name), err) + return err + } + if !overwriteOrAdopt && isExist { return ErrRepoFilesAlreadyExist{u.Name, name} } return nil @@ -1110,7 +1123,11 @@ func GetRepoInitFile(tp, name string) ([]byte, error) { // Use custom file when available. customPath := path.Join(setting.CustomPath, relPath) - if com.IsFile(customPath) { + isFile, err := util.IsFile(customPath) + if err != nil { + log.Error("Unable to check if %s is a file. Error: %v", customPath, err) + } + if isFile { return ioutil.ReadFile(customPath) } @@ -1156,7 +1173,12 @@ func CreateRepository(ctx DBContext, doer, u *User, repo *Repository, overwriteO } repoPath := RepoPath(u.Name, repo.Name) - if !overwriteOrAdopt && com.IsExist(repoPath) { + isExist, err := util.IsExist(repoPath) + if err != nil { + log.Error("Unable to check if %s exists. Error: %v", repoPath, err) + return err + } + if !overwriteOrAdopt && isExist { log.Error("Files already exist in %s and we are not going to adopt or delete.", repoPath) return ErrRepoFilesAlreadyExist{ Uname: u.Name, @@ -1408,7 +1430,12 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error // Rename remote wiki repository to new path and delete local copy. wikiPath := WikiPath(oldOwner.Name, repo.Name) - if com.IsExist(wikiPath) { + isExist, err := util.IsExist(wikiPath) + if err != nil { + log.Error("Unable to check if %s exists. Error: %v", wikiPath, err) + return err + } + if isExist { if err = os.Rename(wikiPath, WikiPath(newOwner.Name, repo.Name)); err != nil { return fmt.Errorf("rename repository wiki: %v", err) } @@ -1451,7 +1478,12 @@ func ChangeRepositoryName(doer *User, repo *Repository, newRepoName string) (err } wikiPath := repo.WikiPath() - if com.IsExist(wikiPath) { + isExist, err := util.IsExist(wikiPath) + if err != nil { + log.Error("Unable to check if %s exists. Error: %v", wikiPath, err) + return err + } + if isExist { if err = os.Rename(wikiPath, WikiPath(repo.Owner.Name, newRepoName)); err != nil { return fmt.Errorf("rename repository wiki: %v", err) } @@ -1528,11 +1560,16 @@ func updateRepository(e Engine, repo *Repository, visibilityChanged bool) (err e // Create/Remove git-daemon-export-ok for git-daemon... daemonExportFile := path.Join(repo.RepoPath(), `git-daemon-export-ok`) - if repo.IsPrivate && com.IsExist(daemonExportFile) { + isExist, err := util.IsExist(daemonExportFile) + if err != nil { + log.Error("Unable to check if %s exists. Error: %v", daemonExportFile, err) + return err + } + if repo.IsPrivate && isExist { if err = util.Remove(daemonExportFile); err != nil { log.Error("Failed to remove %s: %v", daemonExportFile, err) } - } else if !repo.IsPrivate && !com.IsExist(daemonExportFile) { + } else if !repo.IsPrivate && !isExist { if f, err := os.Create(daemonExportFile); err != nil { log.Error("Failed to create %s: %v", daemonExportFile, err) } else { diff --git a/models/ssh_key.go b/models/ssh_key.go index 29a4fd2932..f13fc61914 100644 --- a/models/ssh_key.go +++ b/models/ssh_key.go @@ -736,11 +736,18 @@ func rewriteAllPublicKeys(e Engine) error { } }() - if setting.SSH.AuthorizedKeysBackup && com.IsExist(fPath) { - bakPath := fmt.Sprintf("%s_%d.gitea_bak", fPath, time.Now().Unix()) - if err = com.Copy(fPath, bakPath); err != nil { + if setting.SSH.AuthorizedKeysBackup { + isExist, err := util.IsExist(fPath) + if err != nil { + log.Error("Unable to check if %s exists. Error: %v", fPath, err) return err } + if isExist { + bakPath := fmt.Sprintf("%s_%d.gitea_bak", fPath, time.Now().Unix()) + if err = com.Copy(fPath, bakPath); err != nil { + return err + } + } } if err := regeneratePublicKeys(e, t); err != nil { @@ -765,7 +772,12 @@ func regeneratePublicKeys(e Engine, t io.StringWriter) error { } fPath := filepath.Join(setting.SSH.RootPath, "authorized_keys") - if com.IsExist(fPath) { + isExist, err := util.IsExist(fPath) + if err != nil { + log.Error("Unable to check if %s exists. Error: %v", fPath, err) + return err + } + if isExist { f, err := os.Open(fPath) if err != nil { return err @@ -1206,11 +1218,18 @@ func rewriteAllPrincipalKeys(e Engine) error { os.Remove(tmpPath) }() - if setting.SSH.AuthorizedPrincipalsBackup && com.IsExist(fPath) { - bakPath := fmt.Sprintf("%s_%d.gitea_bak", fPath, time.Now().Unix()) - if err = com.Copy(fPath, bakPath); err != nil { + if setting.SSH.AuthorizedPrincipalsBackup { + isExist, err := util.IsExist(fPath) + if err != nil { + log.Error("Unable to check if %s exists. Error: %v", fPath, err) return err } + if isExist { + bakPath := fmt.Sprintf("%s_%d.gitea_bak", fPath, time.Now().Unix()) + if err = com.Copy(fPath, bakPath); err != nil { + return err + } + } } if err := regeneratePrincipalKeys(e, t); err != nil { @@ -1249,7 +1268,12 @@ func regeneratePrincipalKeys(e Engine, t io.StringWriter) error { } fPath := filepath.Join(setting.SSH.RootPath, authorizedPrincipalsFile) - if com.IsExist(fPath) { + isExist, err := util.IsExist(fPath) + if err != nil { + log.Error("Unable to check if %s exists. Error: %v", fPath, err) + return err + } + if isExist { f, err := os.Open(fPath) if err != nil { return err diff --git a/models/upload.go b/models/upload.go index 3ad1129c25..de2032fb72 100644 --- a/models/upload.go +++ b/models/upload.go @@ -12,11 +12,11 @@ import ( "os" "path" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" gouuid "github.com/google/uuid" - "github.com/unknwon/com" ) // ____ ___ .__ .___ ___________.___.__ @@ -126,7 +126,11 @@ func DeleteUploads(uploads ...*Upload) (err error) { for _, upload := range uploads { localPath := upload.LocalPath() - if !com.IsFile(localPath) { + isFile, err := util.IsFile(localPath) + if err != nil { + log.Error("Unable to check if %s is a file. Error: %v", localPath, err) + } + if !isFile { continue } diff --git a/models/wiki.go b/models/wiki.go index 223abf1edc..beab48d45a 100644 --- a/models/wiki.go +++ b/models/wiki.go @@ -9,7 +9,8 @@ import ( "path/filepath" "strings" - "github.com/unknwon/com" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/util" ) // WikiCloneLink returns clone URLs of repository wiki. @@ -29,5 +30,9 @@ func (repo *Repository) WikiPath() string { // HasWiki returns true if repository has wiki. func (repo *Repository) HasWiki() bool { - return com.IsDir(repo.WikiPath()) + isDir, err := util.IsDir(repo.WikiPath()) + if err != nil { + log.Error("Unable to check if %s is a directory: %v", repo.WikiPath(), err) + } + return isDir } |