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>
tokarchuk/v1.17
zeripath 4 years ago committed by GitHub
parent 5b75f17043
commit 742e21aeba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      cmd/dump.go
  2. 8
      cmd/web.go
  3. 10
      contrib/environment-to-ini/environment-to-ini.go
  4. 2
      integrations/api_repo_test.go
  5. 55
      models/repo.go
  6. 32
      models/ssh_key.go
  7. 8
      models/upload.go
  8. 9
      models/wiki.go
  9. 12
      modules/auth/repo_form.go
  10. 8
      modules/git/hook.go
  11. 26
      modules/options/dynamic.go
  12. 15
      modules/options/static.go
  13. 15
      modules/repository/adopt.go
  14. 8
      modules/repository/check.go
  15. 9
      modules/repository/create.go
  16. 8
      modules/repository/generate.go
  17. 19
      modules/repository/hooks.go
  18. 14
      modules/repository/init.go
  19. 8
      modules/setting/lfs.go
  20. 24
      modules/setting/setting.go
  21. 9
      modules/ssh/ssh.go
  22. 13
      modules/templates/dynamic.go
  23. 15
      modules/templates/static.go
  24. 39
      modules/util/path.go
  25. 13
      routers/admin/repos.go
  26. 24
      routers/api/v1/admin/adopt.go
  27. 7
      routers/install.go
  28. 14
      routers/user/setting/adopt.go
  29. 25
      services/archiver/archiver.go

@ -23,7 +23,6 @@ import (
"gitea.com/macaron/session"
archiver "github.com/mholt/archiver/v3"
"github.com/unknwon/com"
"github.com/urfave/cli"
)
@ -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
@ -349,11 +352,17 @@ 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) {
} 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)
}
}
}
if fileName != "-" {
if err = w.Close(); err != nil {

@ -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"
@ -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)

@ -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"
)
@ -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)
}
@ -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
}

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

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

@ -736,12 +736,19 @@ func rewriteAllPublicKeys(e Engine) error {
}
}()
if setting.SSH.AuthorizedKeysBackup && com.IsExist(fPath) {
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 {
return err
@ -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,12 +1218,19 @@ func rewriteAllPrincipalKeys(e Engine) error {
os.Remove(tmpPath)
}()
if setting.SSH.AuthorizedPrincipalsBackup && com.IsExist(fPath) {
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 {
return err
@ -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

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

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

@ -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"
)
// _______________________________________ _________.______________________ _______________.___.
@ -107,9 +108,16 @@ func ParseRemoteAddr(remoteAddr, authUsername, authPassword string, user *models
}
} else if !user.CanImportLocal() {
return "", models.ErrInvalidCloneAddr{IsPermissionDenied: true}
} else if !com.IsDir(remoteAddr) {
} 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
}

@ -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.
@ -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)

@ -11,7 +11,9 @@ import (
"io/ioutil"
"path"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"github.com/unknwon/com"
)
@ -32,7 +34,11 @@ func Dir(name string) ([]string, error) {
customDir := path.Join(setting.CustomPath, "options", name)
if com.IsDir(customDir) {
isDir, err := util.IsDir(customDir)
if err != nil {
return []string{}, fmt.Errorf("Unabe to check if custom directory %s is a directory. %v", customDir, err)
}
if isDir {
files, err := com.StatDir(customDir, true)
if err != nil {
@ -44,7 +50,11 @@ func Dir(name string) ([]string, error) {
staticDir := path.Join(setting.StaticRootPath, "options", name)
if com.IsDir(staticDir) {
isDir, err = util.IsDir(staticDir)
if err != nil {
return []string{}, fmt.Errorf("Unabe to check if static directory %s is a directory. %v", staticDir, err)
}
if isDir {
files, err := com.StatDir(staticDir, true)
if err != nil {
@ -86,13 +96,21 @@ func Labels(name string) ([]byte, error) {
func fileFromDir(name string) ([]byte, error) {
customPath := path.Join(setting.CustomPath, "options", name)
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)
}
staticPath := path.Join(setting.StaticRootPath, "options", name)
if com.IsFile(staticPath) {
isFile, err = util.IsFile(staticPath)
if err != nil {
log.Error("Unable to check if %s is a file. Error: %v", staticPath, err)
}
if isFile {
return ioutil.ReadFile(staticPath)
}

@ -11,7 +11,9 @@ import (
"io/ioutil"
"path"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"github.com/unknwon/com"
)
@ -31,8 +33,11 @@ func Dir(name string) ([]string, error) {
)
customDir := path.Join(setting.CustomPath, "options", name)
if com.IsDir(customDir) {
isDir, err := util.IsDir(customDir)
if err != nil {
return []string{}, fmt.Errorf("Failed to check if custom directory %s is a directory. %v", err)
}
if isDir {
files, err := com.StatDir(customDir, true)
if err != nil {
@ -100,7 +105,11 @@ func Labels(name string) ([]byte, error) {
func fileFromDir(name string) ([]byte, error) {
customPath := path.Join(setting.CustomPath, "options", name)
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)
}

@ -16,7 +16,6 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"github.com/gobwas/glob"
"github.com/unknwon/com"
)
// AdoptRepository adopts a repository for the user/organization.
@ -49,7 +48,12 @@ func AdoptRepository(doer, u *models.User, opts models.CreateRepoOptions) (*mode
if err := models.WithTx(func(ctx models.DBContext) error {
repoPath := models.RepoPath(u.Name, repo.Name)
if !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 !isExist {
return models.ErrRepoNotExist{
OwnerName: u.Name,
Name: repo.Name,
@ -91,7 +95,12 @@ func DeleteUnadoptedRepository(doer, u *models.User, repoName string) error {
}
repoPath := models.RepoPath(u.Name, repoName)
if !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 !isExist {
return models.ErrRepoNotExist{
OwnerName: u.Name,
Name: repoName,

@ -13,8 +13,8 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"
"github.com/unknwon/com"
"xorm.io/builder"
)
@ -114,7 +114,11 @@ func gatherMissingRepoRecords(ctx context.Context) ([]*models.Repository, error)
return models.ErrCancelledf("during gathering missing repo records before checking %s", repo.FullName())
default:
}
if !com.IsDir(repo.RepoPath()) {
isDir, err := util.IsDir(repo.RepoPath())
if err != nil {
return fmt.Errorf("Unable to check dir for %s. %w", repo.FullName(), err)
}
if !isDir {
repos = append(repos, repo)
}
return nil

@ -13,8 +13,6 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"github.com/unknwon/com"
)
// CreateRepository creates a repository for the user/organization.
@ -58,7 +56,12 @@ func CreateRepository(doer, u *models.User, opts models.CreateRepoOptions) (*mod
}
repoPath := models.RepoPath(u.Name, repo.Name)
if 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 isExist {
// repo already exists - We have two or three options.
// 1. We fail stating that the directory exists
// 2. We create the db repository to go with this data and adopt the git repo

@ -19,7 +19,6 @@ import (
"code.gitea.io/gitea/modules/util"
"github.com/huandu/xstrings"
"github.com/unknwon/com"
)
type transformer struct {
@ -252,7 +251,12 @@ func GenerateRepository(ctx models.DBContext, doer, owner *models.User, template
}
repoPath := generateRepo.RepoPath()
if com.IsExist(repoPath) {
isExist, err := util.IsExist(repoPath)
if err != nil {
log.Error("Unable to check if %s exists. Error: %v", repoPath, err)
return nil, err
}
if isExist {
return nil, models.ErrRepoFilesAlreadyExist{
Uname: generateRepo.OwnerName,
Name: generateRepo.Name,

@ -15,7 +15,6 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"github.com/unknwon/com"
"xorm.io/builder"
)
@ -112,15 +111,27 @@ func CheckDelegateHooks(repoPath string) ([]string, error) {
newHookPath := filepath.Join(hookDir, hookName+".d", "gitea")
cont := false
if !com.IsExist(oldHookPath) {
isExist, err := util.IsExist(oldHookPath)
if err != nil {
results = append(results, fmt.Sprintf("unable to check if %s exists. Error: %v", oldHookPath, err))
}
if err == nil && !isExist {
results = append(results, fmt.Sprintf("old hook file %s does not exist", oldHookPath))
cont = true
}
if !com.IsExist(oldHookPath + ".d") {
isExist, err = util.IsExist(oldHookPath + ".d")
if err != nil {
results = append(results, fmt.Sprintf("unable to check if %s exists. Error: %v", oldHookPath+".d", err))
}
if err == nil && !isExist {
results = append(results, fmt.Sprintf("hooks directory %s does not exist", oldHookPath+".d"))
cont = true
}
if !com.IsExist(newHookPath) {
isExist, err = util.IsExist(newHookPath)
if err != nil {
results = append(results, fmt.Sprintf("unable to check if %s exists. Error: %v", newHookPath, err))
}
if err == nil && !isExist {
results = append(results, fmt.Sprintf("new hook file %s does not exist", newHookPath))
cont = true
}

@ -175,7 +175,12 @@ func initRepoCommit(tmpPath string, repo *models.Repository, u *models.User, def
func checkInitRepository(owner, name string) (err error) {
// Somehow the directory could exist.
repoPath := models.RepoPath(owner, name)
if 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 isExist {
return models.ErrRepoFilesAlreadyExist{
Uname: owner,
Name: name,
@ -192,7 +197,12 @@ func checkInitRepository(owner, name string) (err error) {
}
func adoptRepository(ctx models.DBContext, repoPath string, u *models.User, repo *models.Repository, opts models.CreateRepoOptions) (err error) {
if !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 !isExist {
return fmt.Errorf("adoptRepository: path does not already exist: %s", repoPath)
}

@ -13,8 +13,8 @@ import (
"code.gitea.io/gitea/modules/generate"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"
"github.com/unknwon/com"
ini "gopkg.in/ini.v1"
)
@ -65,7 +65,11 @@ func newLFSService() {
// Save secret
cfg := ini.Empty()
if com.IsFile(CustomConf) {
isFile, err := util.IsFile(CustomConf)
if err != nil {
log.Error("Unable to check if %s is a file. Error: %v", CustomConf, err)
}
if isFile {
// Keeps custom settings if there is already something.
if err := cfg.Append(CustomConf); err != nil {
log.Error("Failed to load custom conf '%s': %v", CustomConf, err)

@ -25,6 +25,7 @@ import (
"code.gitea.io/gitea/modules/generate"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/user"
"code.gitea.io/gitea/modules/util"
shellquote "github.com/kballard/go-shellquote"
"github.com/unknwon/com"
@ -498,7 +499,11 @@ func NewContext() {
createPIDFile(PIDFile)
}
if com.IsFile(CustomConf) {
isFile, err := util.IsFile(CustomConf)
if err != nil {
log.Error("Unable to check if %s is a file. Error: %v", CustomConf, err)
}
if isFile {
if err := Cfg.Append(CustomConf); err != nil {
log.Fatal("Failed to load custom conf '%s': %v", CustomConf, err)
}
@ -739,7 +744,11 @@ func NewContext() {
return
}
cfg := ini.Empty()
if com.IsFile(CustomConf) {
isFile, err := util.IsFile(CustomConf)
if err != nil {
log.Error("Unable to check if %s is a file. Error: %v", CustomConf, err)
}
if isFile {
if err := cfg.Append(CustomConf); err != nil {
log.Error("failed to load custom conf %s: %v", CustomConf, err)
return
@ -908,7 +917,10 @@ func NewContext() {
UI.SearchRepoDescription = Cfg.Section("ui").Key("SEARCH_REPO_DESCRIPTION").MustBool(true)
UI.UseServiceWorker = Cfg.Section("ui").Key("USE_SERVICE_WORKER").MustBool(true)
HasRobotsTxt = com.IsFile(path.Join(CustomPath, "robots.txt"))
HasRobotsTxt, err = util.IsFile(path.Join(CustomPath, "robots.txt"))
if err != nil {
log.Error("Unable to check if %s is a file. Error: %v", path.Join(CustomPath, "robots.txt"), err)
}
newMarkup()
@ -1005,7 +1017,11 @@ func loadOrGenerateInternalToken(sec *ini.Section) string {
// Save secret
cfgSave := ini.Empty()
if com.IsFile(CustomConf) {
isFile, err := util.IsFile(CustomConf)
if err != nil {
log.Error("Unable to check if %s is a file. Error: %v", CustomConf, err)
}
if isFile {
// Keeps custom settings if there is already something.
if err := cfgSave.Append(CustomConf); err != nil {
log.Error("Failed to load custom conf '%s': %v", CustomConf, err)

@ -22,6 +22,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"github.com/gliderlabs/ssh"
"github.com/unknwon/com"
@ -211,7 +212,11 @@ func Listen(host string, port int, ciphers []string, keyExchanges []string, macs
}
keyPath := filepath.Join(setting.AppDataPath, "ssh/gogs.rsa")
if !com.IsExist(keyPath) {
isExist, err := util.IsExist(keyPath)
if err != nil {
log.Fatal("Unable to check if %s exists. Error: %v", keyPath, err)
}
if !isExist {
filePath := filepath.Dir(keyPath)
if err := os.MkdirAll(filePath, os.ModePerm); err != nil {
@ -225,7 +230,7 @@ func Listen(host string, port int, ciphers []string, keyExchanges []string, macs
log.Trace("New private key is generated: %s", keyPath)
}
err := srv.SetOption(ssh.HostKeyFile(keyPath))
err = srv.SetOption(ssh.HostKeyFile(keyPath))
if err != nil {
log.Error("Failed to set Host Key. %s", err)
}

@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"gitea.com/macaron/macaron"
"github.com/unknwon/com"
@ -59,7 +60,11 @@ func Mailer() (*texttmpl.Template, *template.Template) {
staticDir := path.Join(setting.StaticRootPath, "templates", "mail")
if com.IsDir(staticDir) {
isDir, err := util.IsDir(staticDir)
if err != nil {
log.Warn("Unable to check if templates dir %s is a directory. Error: %v", staticDir, err)
}
if isDir {
files, err := com.StatDir(staticDir)
if err != nil {
@ -84,7 +89,11 @@ func Mailer() (*texttmpl.Template, *template.Template) {
customDir := path.Join(setting.CustomPath, "templates", "mail")
if com.IsDir(customDir) {
isDir, err = util.IsDir(customDir)
if err != nil {
log.Warn("Unable to check if templates dir %s is a directory. Error: %v", customDir, err)
}
if isDir {
files, err := com.StatDir(customDir)
if err != nil {

@ -18,6 +18,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"gitea.com/macaron/macaron"
"github.com/unknwon/com"
@ -77,8 +78,11 @@ func NewTemplateFileSystem() templateFileSystem {
}
customDir := path.Join(setting.CustomPath, "templates")
if com.IsDir(customDir) {
isDir, err := util.IsDir(customDir)
if err != nil {
log.Warn("Unable to check if templates dir %s is a directory. Error: %v", customDir, err)
}
if isDir {
files, err := com.StatDir(customDir)
if err != nil {
@ -170,8 +174,11 @@ func Mailer() (*texttmpl.Template, *template.Template) {
}
customDir := path.Join(setting.CustomPath, "templates", "mail")
if com.IsDir(customDir) {
isDir, err := util.IsDir(customDir)
if err != nil {
log.Warn("Failed to check if custom directory %s is a directory. %v", err)
}
if isDir {
files, err := com.StatDir(customDir)
if err != nil {

@ -31,3 +31,42 @@ func GetDirectorySize(path string) (int64, error) {
})
return size, err
}
// IsDir returns true if given path is a directory,
// or returns false when it's a file or does not exist.
func IsDir(dir string) (bool, error) {
f, err := os.Stat(dir)
if err == nil {
return f.IsDir(), nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// IsFile returns true if given path is a file,
// or returns false when it's a directory or does not exist.
func IsFile(filePath string) (bool, error) {
f, err := os.Stat(filePath)
if err == nil {
return !f.IsDir(), nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// IsExist checks whether a file or directory exists.
// It returns false when the file or directory does not exist.
func IsExist(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil || os.IsExist(err) {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}

@ -13,9 +13,9 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers"
repo_service "code.gitea.io/gitea/services/repository"
"github.com/unknwon/com"
)
const (
@ -120,10 +120,17 @@ func AdoptOrDeleteRepository(ctx *context.Context) {
repoName := dirSplit[1]
// check not a repo
if has, err := models.IsRepositoryExist(ctxUser, repoName); err != nil {
has, err := models.IsRepositoryExist(ctxUser, repoName)
if err != nil {
ctx.ServerError("IsRepositoryExist", err)
return
} else if has || !com.IsDir(models.RepoPath(ctxUser.Name, repoName)) {
}
isDir, err := util.IsDir(models.RepoPath(ctxUser.Name, repoName))
if err != nil {
ctx.ServerError("IsDir", err)
return
}
if has || !isDir {
// Fallthrough to failure mode
} else if action == "adopt" {
if _, err := repository.AdoptRepository(ctx.User, ctxUser, models.CreateRepoOptions{

@ -11,8 +11,8 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers/api/v1/utils"
"github.com/unknwon/com"
)
// ListUnadoptedRepositories lists the unadopted repositories that match the provided names
@ -92,10 +92,17 @@ func AdoptRepository(ctx *context.APIContext) {
}
// check not a repo
if has, err := models.IsRepositoryExist(ctxUser, repoName); err != nil {
has, err := models.IsRepositoryExist(ctxUser, repoName)
if err != nil {
ctx.InternalServerError(err)
return
}
isDir, err := util.IsDir(models.RepoPath(ctxUser.Name, repoName))
if err != nil {
ctx.InternalServerError(err)
return
} else if has || !com.IsDir(models.RepoPath(ctxUser.Name, repoName)) {
}
if has || !isDir {
ctx.NotFound()
return
}
@ -147,10 +154,17 @@ func DeleteUnadoptedRepository(ctx *context.APIContext) {
}
// check not a repo
if has, err := models.IsRepositoryExist(ctxUser, repoName); err != nil {
has, err := models.IsRepositoryExist(ctxUser, repoName)
if err != nil {
ctx.InternalServerError(err)
return
}
isDir, err := util.IsDir(models.RepoPath(ctxUser.Name, repoName))
if err != nil {
ctx.InternalServerError(err)
return
} else if has || !com.IsDir(models.RepoPath(ctxUser.Name, repoName)) {
}
if has || !isDir {
ctx.NotFound()
return
}

@ -20,6 +20,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/user"
"code.gitea.io/gitea/modules/util"
"github.com/unknwon/com"
"gopkg.in/ini.v1"
@ -260,7 +261,11 @@ func InstallPost(ctx *context.Context, form auth.InstallForm) {
// Save settings.
cfg := ini.Empty()
if com.IsFile(setting.CustomConf) {
isFile, err := util.IsFile(setting.CustomConf)
if err != nil {
log.Error("Unable to check if %s is a file. Error: %v", setting.CustomConf, err)
}
if isFile {
// Keeps custom settings if there is already something.
if err = cfg.Append(setting.CustomConf); err != nil {
log.Error("Failed to load custom conf '%s': %v", setting.CustomConf, err)

@ -11,7 +11,7 @@ import (
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
"github.com/unknwon/com"
"code.gitea.io/gitea/modules/util"
)
// AdoptOrDeleteRepository adopts or deletes a repository
@ -30,10 +30,18 @@ func AdoptOrDeleteRepository(ctx *context.Context) {
root := filepath.Join(models.UserPath(ctxUser.LowerName))
// check not a repo
if has, err := models.IsRepositoryExist(ctxUser, dir); err != nil {
has, err := models.IsRepositoryExist(ctxUser, dir)
if err != nil {
ctx.ServerError("IsRepositoryExist", err)
return
} else if has || !com.IsDir(filepath.Join(root, dir+".git")) {
}
isDir, err := util.IsDir(filepath.Join(root, dir+".git"))
if err != nil {
ctx.ServerError("IsDir", err)
return
}
if has || !isDir {
// Fallthrough to failure mode
} else if action == "adopt" && allowAdopt {
if _, err := repository.AdoptRepository(ctxUser, ctxUser, models.CreateRepoOptions{

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

Loading…
Cancel
Save