mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Handle and propagate errors when checking if paths are Dirs, Files or Exist (#13186)
* Ensure errors from IsDir propagate * Handle errors when checking IsFile * Handle and propagate errors from IsExist * Update modules/templates/static.go * Update modules/templates/static.go * Return after ctx.ServerError * Apply suggestions from code review * Fix tests The previous merge managed to break repo_form.go Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: techknowlogick <techknowlogick@gitea.io> Co-authored-by: Lauris BH <lauris@nix.lv>
This commit is contained in:
@@ -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
|
||||
|
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user