Skip to content

Commit

Permalink
Handle and propagate errors when checking if paths are Dirs, Files or…
Browse files Browse the repository at this point in the history
… 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>
  • Loading branch information
3 people authored Nov 28, 2020
1 parent 5b75f17 commit 742e21a
Show file tree
Hide file tree
Showing 29 changed files with 384 additions and 94 deletions.
19 changes: 14 additions & 5 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"gitea.com/macaron/session"
archiver "github.com/mholt/archiver/v3"
"github.com/unknwon/com"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -306,7 +305,11 @@ func runDump(ctx *cli.Context) error {
log.Info("Custom dir %s doesn't exist, skipped", setting.CustomPath)
}

if com.IsExist(setting.AppDataPath) {
isExist, err := util.IsExist(setting.AppDataPath)
if err != nil {
log.Error("Unable to check if %s exists. Error: %v", setting.AppDataPath, err)
}
if isExist {
log.Info("Packing data directory...%s", setting.AppDataPath)

var excludes []string
Expand Down Expand Up @@ -349,9 +352,15 @@ func runDump(ctx *cli.Context) error {
// yet or not.
if ctx.IsSet("skip-log") && ctx.Bool("skip-log") {
log.Info("Skip dumping log files")
} else if com.IsExist(setting.LogRootPath) {
if err := addRecursive(w, "log", setting.LogRootPath, verbose); err != nil {
fatal("Failed to include log: %v", err)
} else {
isExist, err := util.IsExist(setting.LogRootPath)
if err != nil {
log.Error("Unable to check if %s exists. Error: %v", setting.LogRootPath, err)
}
if isExist {
if err := addRecursive(w, "log", setting.LogRootPath, verbose); err != nil {
fatal("Failed to include log: %v", err)
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers"
"code.gitea.io/gitea/routers/routes"

context2 "github.com/gorilla/context"
"github.com/unknwon/com"
"github.com/urfave/cli"
"golang.org/x/crypto/acme/autocert"
ini "gopkg.in/ini.v1"
Expand Down Expand Up @@ -188,7 +188,11 @@ func setPort(port string) error {
default:
// Save LOCAL_ROOT_URL if port changed
cfg := ini.Empty()
if com.IsFile(setting.CustomConf) {
isFile, err := util.IsFile(setting.CustomConf)
if err != nil {
log.Fatal("Unable to check if %s is a file", err)
}
if isFile {
// Keeps custom settings if there is already something.
if err := cfg.Append(setting.CustomConf); err != nil {
return fmt.Errorf("Failed to load custom conf '%s': %v", setting.CustomConf, err)
Expand Down
10 changes: 7 additions & 3 deletions contrib/environment-to-ini/environment-to-ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"

"github.com/unknwon/com"
"github.com/urfave/cli"
ini "gopkg.in/ini.v1"
)
Expand Down Expand Up @@ -97,7 +97,11 @@ func runEnvironmentToIni(c *cli.Context) error {
setting.SetCustomPathAndConf(providedCustom, providedConf, providedWorkPath)

cfg := ini.Empty()
if com.IsFile(setting.CustomConf) {
isFile, err := util.IsFile(setting.CustomConf)
if err != nil {
log.Fatal("Unable to check if %s is a file. Error: %v", setting.CustomConf, err)
}
if isFile {
if err := cfg.Append(setting.CustomConf); err != nil {
log.Fatal("Failed to load custom conf '%s': %v", setting.CustomConf, err)
}
Expand Down Expand Up @@ -145,7 +149,7 @@ func runEnvironmentToIni(c *cli.Context) error {
if len(destination) == 0 {
destination = setting.CustomConf
}
err := cfg.SaveTo(destination)
err = cfg.SaveTo(destination)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion integrations/api_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func TestAPIRepoMigrate(t *testing.T) {
if resp.Code == http.StatusUnprocessableEntity {
respJSON := map[string]string{}
DecodeJSON(t, resp, &respJSON)
if assert.Equal(t, respJSON["message"], "Remote visit addressed rate limitation.") {
if assert.Equal(t, "Remote visit addressed rate limitation.", respJSON["message"]) {
t.Log("test hit github rate limitation")
}
} else {
Expand Down
55 changes: 46 additions & 9 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
40 changes: 32 additions & 8 deletions models/ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions models/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

// ____ ___ .__ .___ ___________.___.__
Expand Down Expand Up @@ -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
}

Expand Down
9 changes: 7 additions & 2 deletions models/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
14 changes: 11 additions & 3 deletions modules/auth/repo_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import (
"strings"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers/utils"

"gitea.com/macaron/binding"
"gitea.com/macaron/macaron"
"github.com/unknwon/com"
)

// _______________________________________ _________.______________________ _______________.___.
Expand Down Expand Up @@ -107,8 +108,15 @@ func ParseRemoteAddr(remoteAddr, authUsername, authPassword string, user *models
}
} else if !user.CanImportLocal() {
return "", models.ErrInvalidCloneAddr{IsPermissionDenied: true}
} else if !com.IsDir(remoteAddr) {
return "", models.ErrInvalidCloneAddr{IsInvalidPath: true}
} else {
isDir, err := util.IsDir(remoteAddr)
if err != nil {
log.Error("Unable to check if %s is a directory: %v", remoteAddr, err)
return "", err
}
if !isDir {
return "", models.ErrInvalidCloneAddr{IsInvalidPath: true}
}
}

return remoteAddr, nil
Expand Down
8 changes: 6 additions & 2 deletions modules/git/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"strings"

"code.gitea.io/gitea/modules/util"
"github.com/unknwon/com"
)

// hookNames is a list of Git server hooks' name that are supported.
Expand Down Expand Up @@ -129,7 +128,12 @@ const (
func SetUpdateHook(repoPath, content string) (err error) {
log("Setting update hook: %s", repoPath)
hookPath := path.Join(repoPath, HookPathUpdate)
if com.IsExist(hookPath) {
isExist, err := util.IsExist(hookPath)
if err != nil {
log("Unable to check if %s exists. Error: %v", hookPath, err)
return err
}
if isExist {
err = util.Remove(hookPath)
} else {
err = os.MkdirAll(path.Dir(hookPath), os.ModePerm)
Expand Down
Loading

0 comments on commit 742e21a

Please sign in to comment.