diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/auth/repo_form.go | 14 | ||||
-rw-r--r-- | modules/git/hook.go | 8 | ||||
-rw-r--r-- | modules/options/dynamic.go | 26 | ||||
-rw-r--r-- | modules/options/static.go | 15 | ||||
-rw-r--r-- | modules/repository/adopt.go | 15 | ||||
-rw-r--r-- | modules/repository/check.go | 8 | ||||
-rw-r--r-- | modules/repository/create.go | 9 | ||||
-rw-r--r-- | modules/repository/generate.go | 8 | ||||
-rw-r--r-- | modules/repository/hooks.go | 19 | ||||
-rw-r--r-- | modules/repository/init.go | 14 | ||||
-rw-r--r-- | modules/setting/lfs.go | 8 | ||||
-rw-r--r-- | modules/setting/setting.go | 24 | ||||
-rw-r--r-- | modules/ssh/ssh.go | 9 | ||||
-rw-r--r-- | modules/templates/dynamic.go | 13 | ||||
-rw-r--r-- | modules/templates/static.go | 15 | ||||
-rw-r--r-- | modules/util/path.go | 39 |
16 files changed, 202 insertions, 42 deletions
diff --git a/modules/auth/repo_form.go b/modules/auth/repo_form.go index f27812bb1b..2d6f89b6ed 100644 --- a/modules/auth/repo_form.go +++ b/modules/auth/repo_form.go @@ -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 diff --git a/modules/git/hook.go b/modules/git/hook.go index 2de36dbdef..c23fbf8aa1 100644 --- a/modules/git/hook.go +++ b/modules/git/hook.go @@ -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) diff --git a/modules/options/dynamic.go b/modules/options/dynamic.go index 20dde11dc3..060ca12bb0 100644 --- a/modules/options/dynamic.go +++ b/modules/options/dynamic.go @@ -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) } diff --git a/modules/options/static.go b/modules/options/static.go index 39f56f42f4..ff1e6b2332 100644 --- a/modules/options/static.go +++ b/modules/options/static.go @@ -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) } diff --git a/modules/repository/adopt.go b/modules/repository/adopt.go index 22cd6dd91f..0302119630 100644 --- a/modules/repository/adopt.go +++ b/modules/repository/adopt.go @@ -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, diff --git a/modules/repository/check.go b/modules/repository/check.go index 274576c348..d13ddbb0b0 100644 --- a/modules/repository/check.go +++ b/modules/repository/check.go @@ -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 diff --git a/modules/repository/create.go b/modules/repository/create.go index 1408637815..1f7145ee2a 100644 --- a/modules/repository/create.go +++ b/modules/repository/create.go @@ -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 diff --git a/modules/repository/generate.go b/modules/repository/generate.go index 5d1ef72b6c..0142d689d1 100644 --- a/modules/repository/generate.go +++ b/modules/repository/generate.go @@ -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, diff --git a/modules/repository/hooks.go b/modules/repository/hooks.go index faf9c98f8a..aba5db6719 100644 --- a/modules/repository/hooks.go +++ b/modules/repository/hooks.go @@ -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 } diff --git a/modules/repository/init.go b/modules/repository/init.go index 569069ee1f..37dc69a0a8 100644 --- a/modules/repository/init.go +++ b/modules/repository/init.go @@ -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) } diff --git a/modules/setting/lfs.go b/modules/setting/lfs.go index 5af80c2ab1..ab475bbeb4 100644 --- a/modules/setting/lfs.go +++ b/modules/setting/lfs.go @@ -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) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 708dc28233..79e7751905 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -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) diff --git a/modules/ssh/ssh.go b/modules/ssh/ssh.go index 7a449dd41b..761164caa0 100644 --- a/modules/ssh/ssh.go +++ b/modules/ssh/ssh.go @@ -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) } diff --git a/modules/templates/dynamic.go b/modules/templates/dynamic.go index bd1c4d06c5..5eda948034 100644 --- a/modules/templates/dynamic.go +++ b/modules/templates/dynamic.go @@ -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 { diff --git a/modules/templates/static.go b/modules/templates/static.go index a3aff5e567..fd8e79a783 100644 --- a/modules/templates/static.go +++ b/modules/templates/static.go @@ -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 { diff --git a/modules/util/path.go b/modules/util/path.go index 2b198eb6dc..fbcefb83b6 100644 --- a/modules/util/path.go +++ b/modules/util/path.go @@ -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 +} |