summaryrefslogtreecommitdiffstats
path: root/models/repo.go
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2020-11-28 02:42:08 +0000
committerGitHub <noreply@github.com>2020-11-27 21:42:08 -0500
commit742e21aeba5c02935269a2a3681f4486019ce542 (patch)
treee1572ab13c33dec1238321170a90d42851ae4ca2 /models/repo.go
parent5b75f17043bc2a6d0e753ae5c9c6759adad5aaac (diff)
downloadgitea-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/repo.go')
-rw-r--r--models/repo.go55
1 files changed, 46 insertions, 9 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 {