1
0
Fork 0
forked from forgejo/forgejo

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>
This commit is contained in:
zeripath 2020-11-28 02:42:08 +00:00 committed by GitHub
parent 5b75f17043
commit 742e21aeba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 384 additions and 94 deletions

View file

@ -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 {