]> source.dussan.org Git - gitea.git/commitdiff
Handle and propagate errors when checking if paths are Dirs, Files or Exist (#13186)
authorzeripath <art27@cantab.net>
Sat, 28 Nov 2020 02:42:08 +0000 (02:42 +0000)
committerGitHub <noreply@github.com>
Sat, 28 Nov 2020 02:42:08 +0000 (21:42 -0500)
* 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>
29 files changed:
cmd/dump.go
cmd/web.go
contrib/environment-to-ini/environment-to-ini.go
integrations/api_repo_test.go
models/repo.go
models/ssh_key.go
models/upload.go
models/wiki.go
modules/auth/repo_form.go
modules/git/hook.go
modules/options/dynamic.go
modules/options/static.go
modules/repository/adopt.go
modules/repository/check.go
modules/repository/create.go
modules/repository/generate.go
modules/repository/hooks.go
modules/repository/init.go
modules/setting/lfs.go
modules/setting/setting.go
modules/ssh/ssh.go
modules/templates/dynamic.go
modules/templates/static.go
modules/util/path.go
routers/admin/repos.go
routers/api/v1/admin/adopt.go
routers/install.go
routers/user/setting/adopt.go
services/archiver/archiver.go

index 7ff986f14eac35b69e218932a1bdafea4eb22281..1a2e62576792fe7e82ed91458ec934bbefd4d85a 100644 (file)
@@ -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,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)
+                       }
                }
        }
 
index 47dbc2675e24db33ab05b05a7f1dfc365290c37f..063e41c94647b3eb7e977d592b4596552ebff5b5 100644 (file)
@@ -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)
index e9dc383cb4c273db174ad4dc5a0891df3d767f9b..bfba2c31409088e8819412f6d02f762e94413c59 100644 (file)
@@ -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
        }
index da87db7b1b5ba500642fadcfe726ca33cff486e9..25662cdda34307894bcb67a2546e7c96ad581313 100644 (file)
@@ -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 {
index 2b53ac666cbe7cc218c16b807722499b08d61bdb..7e38bbcdb7ecce0bb8a90a7058ff1a5617545875 100644 (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 {
index 29a4fd293283b230cef19145449f900fbd3db551..f13fc61914a42afb56f5baae359224711560615e 100644 (file)
@@ -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 {
@@ -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,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 {
@@ -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
index 3ad1129c257fe01eb806e86d06e937a423c541cd..de2032fb720c501e860ff15218e37c372ff8b5cb 100644 (file)
@@ -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
                }
 
index 223abf1edc55ff06aec122dbb451f5f70d54b892..beab48d45ada7facc4c2ea6547040943296cfd16 100644 (file)
@@ -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
 }
index f27812bb1b964c5e3b8e64db7843bbaf7063df53..2d6f89b6ed75bcfdb86a7eb0b1fccc3e275e375a 100644 (file)
@@ -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,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
index 2de36dbdef66187f67481ca34b600985a358a054..c23fbf8aa1bb23f39890cd71df6969b61d1806e1 100644 (file)
@@ -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)
index 20dde11dc3e952ed6a92137f0a2d7b3c587c0004..060ca12bb00da74712e38bc64f12d27f4cb96b4e 100644 (file)
@@ -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)
        }
 
index 39f56f42f47867aa0ca61ee085ded6050294483d..ff1e6b2332343fe9f80da090722672d037d62b2e 100644 (file)
@@ -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)
        }
 
index 22cd6dd91f7f1f77e143435224d403a78304263e..0302119630562761b6f78b402c585e3df3545553 100644 (file)
@@ -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,
index 274576c34887eebb389b961d498d64fb48497422..d13ddbb0b017cd173b1de1fac1e18480c7e28090 100644 (file)
@@ -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
index 1408637815d33447e3c5fb17ae91da53a64576d6..1f7145ee2a5834d0b7b8f63f3a7ea3091d7477da 100644 (file)
@@ -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
index 5d1ef72b6c04298cdb2b5406faa34f58143c89f0..0142d689d160f9fd096a88c868258ab610d1332f 100644 (file)
@@ -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,
index faf9c98f8ae83b8d256524828612e60de7a6fb66..aba5db6719b224e89f1388bb95b9acc087a5d92e 100644 (file)
@@ -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
                }
index 569069ee1fcc36a288bd80f4e3c0dc8820d2f685..37dc69a0a8689c1499335a09aaa09949896e76a3 100644 (file)
@@ -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)
        }
 
index 5af80c2ab16f9016e9a9880b0b16ca7c2f35e4ed..ab475bbeb4b314ec37c5faed521832ce414f2f30 100644 (file)
@@ -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)
index 708dc282337db88f23072dd96a344b6e07a3de06..79e7751905b7814145138aad6e6fa921de8c4df3 100644 (file)
@@ -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)
index 7a449dd41b0aad79b91fd772521a834b53113e1f..761164caa0172e35e9ff9175c5d0e2cae14dcbbf 100644 (file)
@@ -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)
        }
index bd1c4d06c586234dd477eefa3362b731ba8596a7..5eda9480348e7469f936cf0704b5008ed57f502b 100644 (file)
@@ -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 {
index a3aff5e567bde44c0d8090be9a103c9270d6f54d..fd8e79a783befa41c0be35f9519b5a880ee6c529 100644 (file)
@@ -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 {
index 2b198eb6dc6e24f38d413892a2379c5a25f34bb0..fbcefb83b6f7256d017b7871b957b22ae1d20a5d 100644 (file)
@@ -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
+}
index 10abaf9547ad18e8524bdbb7695ed562b24ea187..54b6c8e7c6fb09770097313a48c9ade70b24dfb2 100644 (file)
@@ -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{
index 1a7a62a55cf066b71cac76645d230966e084287d..fdef94e6c843c5513cc640f4035c9197b8b20d31 100644 (file)
@@ -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
        }
index 4dd934aa04f9f3b92181919ab21dd2159370becd..88786efb9586bef115fa39f9d0d630e05c76c86f 100644 (file)
@@ -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)
index 6ff07d6daa916d8191114f7e50e0d3210e27b4e1..b2d918784f59ae02256bfebe2ec651591b67ca03 100644 (file)
@@ -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{
index 4dd84524ca93323aef5061381e120301d8d28491..359fc8b627d9164e135544fc7870feaff26ae2fe 100644 (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
        }