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

@ -21,8 +21,7 @@ import (
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"github.com/unknwon/com"
"code.gitea.io/gitea/modules/util"
)
// ArchiveRequest defines the parameters of an archive request, which notably
@ -138,7 +137,12 @@ func DeriveRequestFrom(ctx *context.Context, uri string) *ArchiveRequest {
}
r.refName = strings.TrimSuffix(r.uri, r.ext)
if !com.IsDir(r.archivePath) {
isDir, err := util.IsDir(r.archivePath)
if err != nil {
ctx.ServerError("Download -> util.IsDir(archivePath)", err)
return nil
}
if !isDir {
if err := os.MkdirAll(r.archivePath, os.ModePerm); err != nil {
ctx.ServerError("Download -> os.MkdirAll(archivePath)", err)
return nil
@ -146,9 +150,6 @@ func DeriveRequestFrom(ctx *context.Context, uri string) *ArchiveRequest {
}
// Get corresponding commit.
var (
err error
)
if r.repo.IsBranchExist(r.refName) {
r.commit, err = r.repo.GetBranchCommit(r.refName)
if err != nil {
@ -179,7 +180,11 @@ func DeriveRequestFrom(ctx *context.Context, uri string) *ArchiveRequest {
}
r.archivePath = path.Join(r.archivePath, base.ShortSha(r.commit.ID.String())+r.ext)
r.archiveComplete = com.IsFile(r.archivePath)
r.archiveComplete, err = util.IsFile(r.archivePath)
if err != nil {
ctx.ServerError("util.IsFile", err)
return nil
}
return r
}
@ -198,7 +203,11 @@ func doArchive(r *ArchiveRequest) {
// race conditions and difficulties in locking. Do one last check that
// the archive we're referring to doesn't already exist. If it does exist,
// then just mark the request as complete and move on.
if com.IsFile(r.archivePath) {
isFile, err := util.IsFile(r.archivePath)
if err != nil {
log.Error("Unable to check if %s util.IsFile: %v. Will ignore and recreate.", r.archivePath, err)
}
if isFile {
r.archiveComplete = true
return
}