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:
zeripath 2020-11-28 02:42:08 +00:00 committed by GitHub
parent 5b75f17043
commit 742e21aeba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 384 additions and 94 deletions

View file

@ -23,7 +23,6 @@ import (
"gitea.com/macaron/session" "gitea.com/macaron/session"
archiver "github.com/mholt/archiver/v3" archiver "github.com/mholt/archiver/v3"
"github.com/unknwon/com"
"github.com/urfave/cli" "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) 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) log.Info("Packing data directory...%s", setting.AppDataPath)
var excludes []string var excludes []string
@ -349,9 +352,15 @@ func runDump(ctx *cli.Context) error {
// yet or not. // yet or not.
if ctx.IsSet("skip-log") && ctx.Bool("skip-log") { if ctx.IsSet("skip-log") && ctx.Bool("skip-log") {
log.Info("Skip dumping log files") log.Info("Skip dumping log files")
} else if com.IsExist(setting.LogRootPath) { } else {
if err := addRecursive(w, "log", setting.LogRootPath, verbose); err != nil { isExist, err := util.IsExist(setting.LogRootPath)
fatal("Failed to include log: %v", err) 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)
}
} }
} }

View file

@ -16,11 +16,11 @@ import (
"code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers" "code.gitea.io/gitea/routers"
"code.gitea.io/gitea/routers/routes" "code.gitea.io/gitea/routers/routes"
context2 "github.com/gorilla/context" context2 "github.com/gorilla/context"
"github.com/unknwon/com"
"github.com/urfave/cli" "github.com/urfave/cli"
"golang.org/x/crypto/acme/autocert" "golang.org/x/crypto/acme/autocert"
ini "gopkg.in/ini.v1" ini "gopkg.in/ini.v1"
@ -188,7 +188,11 @@ func setPort(port string) error {
default: default:
// Save LOCAL_ROOT_URL if port changed // Save LOCAL_ROOT_URL if port changed
cfg := ini.Empty() 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. // Keeps custom settings if there is already something.
if err := cfg.Append(setting.CustomConf); err != nil { if err := cfg.Append(setting.CustomConf); err != nil {
return fmt.Errorf("Failed to load custom conf '%s': %v", setting.CustomConf, err) return fmt.Errorf("Failed to load custom conf '%s': %v", setting.CustomConf, err)

View file

@ -12,8 +12,8 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"github.com/unknwon/com"
"github.com/urfave/cli" "github.com/urfave/cli"
ini "gopkg.in/ini.v1" ini "gopkg.in/ini.v1"
) )
@ -97,7 +97,11 @@ func runEnvironmentToIni(c *cli.Context) error {
setting.SetCustomPathAndConf(providedCustom, providedConf, providedWorkPath) setting.SetCustomPathAndConf(providedCustom, providedConf, providedWorkPath)
cfg := ini.Empty() 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 { if err := cfg.Append(setting.CustomConf); err != nil {
log.Fatal("Failed to load custom conf '%s': %v", setting.CustomConf, err) 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 { if len(destination) == 0 {
destination = setting.CustomConf destination = setting.CustomConf
} }
err := cfg.SaveTo(destination) err = cfg.SaveTo(destination)
if err != nil { if err != nil {
return err return err
} }

View file

@ -325,7 +325,7 @@ func TestAPIRepoMigrate(t *testing.T) {
if resp.Code == http.StatusUnprocessableEntity { if resp.Code == http.StatusUnprocessableEntity {
respJSON := map[string]string{} respJSON := map[string]string{}
DecodeJSON(t, resp, &respJSON) 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") t.Log("test hit github rate limitation")
} }
} else { } else {

View file

@ -74,7 +74,11 @@ func loadRepoConfig() {
log.Fatal("Failed to get %s files: %v", t, err) log.Fatal("Failed to get %s files: %v", t, err)
} }
customPath := path.Join(setting.CustomPath, "options", t) 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) customFiles, err := com.StatDir(customPath)
if err != nil { if err != nil {
log.Fatal("Failed to get custom %s files: %v", t, err) 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, OwnerID: u.ID,
LowerName: strings.ToLower(repoName), 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. // 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} 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 ErrRepoFilesAlreadyExist{u.Name, name}
} }
return nil return nil
@ -1110,7 +1123,11 @@ func GetRepoInitFile(tp, name string) ([]byte, error) {
// Use custom file when available. // Use custom file when available.
customPath := path.Join(setting.CustomPath, relPath) 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) return ioutil.ReadFile(customPath)
} }
@ -1156,7 +1173,12 @@ func CreateRepository(ctx DBContext, doer, u *User, repo *Repository, overwriteO
} }
repoPath := RepoPath(u.Name, repo.Name) 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) log.Error("Files already exist in %s and we are not going to adopt or delete.", repoPath)
return ErrRepoFilesAlreadyExist{ return ErrRepoFilesAlreadyExist{
Uname: u.Name, 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. // Rename remote wiki repository to new path and delete local copy.
wikiPath := WikiPath(oldOwner.Name, repo.Name) 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 { if err = os.Rename(wikiPath, WikiPath(newOwner.Name, repo.Name)); err != nil {
return fmt.Errorf("rename repository wiki: %v", err) return fmt.Errorf("rename repository wiki: %v", err)
} }
@ -1451,7 +1478,12 @@ func ChangeRepositoryName(doer *User, repo *Repository, newRepoName string) (err
} }
wikiPath := repo.WikiPath() 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 { if err = os.Rename(wikiPath, WikiPath(repo.Owner.Name, newRepoName)); err != nil {
return fmt.Errorf("rename repository wiki: %v", err) 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... // Create/Remove git-daemon-export-ok for git-daemon...
daemonExportFile := path.Join(repo.RepoPath(), `git-daemon-export-ok`) 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 { if err = util.Remove(daemonExportFile); err != nil {
log.Error("Failed to remove %s: %v", daemonExportFile, err) 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 { if f, err := os.Create(daemonExportFile); err != nil {
log.Error("Failed to create %s: %v", daemonExportFile, err) log.Error("Failed to create %s: %v", daemonExportFile, err)
} else { } else {

View file

@ -736,11 +736,18 @@ func rewriteAllPublicKeys(e Engine) error {
} }
}() }()
if setting.SSH.AuthorizedKeysBackup && com.IsExist(fPath) { if setting.SSH.AuthorizedKeysBackup {
bakPath := fmt.Sprintf("%s_%d.gitea_bak", fPath, time.Now().Unix()) isExist, err := util.IsExist(fPath)
if err = com.Copy(fPath, bakPath); err != nil { if err != nil {
log.Error("Unable to check if %s exists. Error: %v", fPath, err)
return 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 { 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") 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) f, err := os.Open(fPath)
if err != nil { if err != nil {
return err return err
@ -1206,11 +1218,18 @@ func rewriteAllPrincipalKeys(e Engine) error {
os.Remove(tmpPath) os.Remove(tmpPath)
}() }()
if setting.SSH.AuthorizedPrincipalsBackup && com.IsExist(fPath) { if setting.SSH.AuthorizedPrincipalsBackup {
bakPath := fmt.Sprintf("%s_%d.gitea_bak", fPath, time.Now().Unix()) isExist, err := util.IsExist(fPath)
if err = com.Copy(fPath, bakPath); err != nil { if err != nil {
log.Error("Unable to check if %s exists. Error: %v", fPath, err)
return 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 { 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) 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) f, err := os.Open(fPath)
if err != nil { if err != nil {
return err return err

View file

@ -12,11 +12,11 @@ import (
"os" "os"
"path" "path"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
gouuid "github.com/google/uuid" gouuid "github.com/google/uuid"
"github.com/unknwon/com"
) )
// ____ ___ .__ .___ ___________.___.__ // ____ ___ .__ .___ ___________.___.__
@ -126,7 +126,11 @@ func DeleteUploads(uploads ...*Upload) (err error) {
for _, upload := range uploads { for _, upload := range uploads {
localPath := upload.LocalPath() 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 continue
} }

View file

@ -9,7 +9,8 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/unknwon/com" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"
) )
// WikiCloneLink returns clone URLs of repository wiki. // WikiCloneLink returns clone URLs of repository wiki.
@ -29,5 +30,9 @@ func (repo *Repository) WikiPath() string {
// HasWiki returns true if repository has wiki. // HasWiki returns true if repository has wiki.
func (repo *Repository) HasWiki() bool { 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
} }

View file

@ -10,13 +10,14 @@ import (
"strings" "strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers/utils" "code.gitea.io/gitea/routers/utils"
"gitea.com/macaron/binding" "gitea.com/macaron/binding"
"gitea.com/macaron/macaron" "gitea.com/macaron/macaron"
"github.com/unknwon/com"
) )
// _______________________________________ _________.______________________ _______________.___. // _______________________________________ _________.______________________ _______________.___.
@ -107,8 +108,15 @@ func ParseRemoteAddr(remoteAddr, authUsername, authPassword string, user *models
} }
} else if !user.CanImportLocal() { } else if !user.CanImportLocal() {
return "", models.ErrInvalidCloneAddr{IsPermissionDenied: true} return "", models.ErrInvalidCloneAddr{IsPermissionDenied: true}
} else if !com.IsDir(remoteAddr) { } else {
return "", models.ErrInvalidCloneAddr{IsInvalidPath: true} 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 return remoteAddr, nil

View file

@ -13,7 +13,6 @@ import (
"strings" "strings"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
"github.com/unknwon/com"
) )
// hookNames is a list of Git server hooks' name that are supported. // hookNames is a list of Git server hooks' name that are supported.
@ -129,7 +128,12 @@ const (
func SetUpdateHook(repoPath, content string) (err error) { func SetUpdateHook(repoPath, content string) (err error) {
log("Setting update hook: %s", repoPath) log("Setting update hook: %s", repoPath)
hookPath := path.Join(repoPath, HookPathUpdate) 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) err = util.Remove(hookPath)
} else { } else {
err = os.MkdirAll(path.Dir(hookPath), os.ModePerm) err = os.MkdirAll(path.Dir(hookPath), os.ModePerm)

View file

@ -11,7 +11,9 @@ import (
"io/ioutil" "io/ioutil"
"path" "path"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"github.com/unknwon/com" "github.com/unknwon/com"
) )
@ -32,7 +34,11 @@ func Dir(name string) ([]string, error) {
customDir := path.Join(setting.CustomPath, "options", name) 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) files, err := com.StatDir(customDir, true)
if err != nil { if err != nil {
@ -44,7 +50,11 @@ func Dir(name string) ([]string, error) {
staticDir := path.Join(setting.StaticRootPath, "options", name) 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) files, err := com.StatDir(staticDir, true)
if err != nil { if err != nil {
@ -86,13 +96,21 @@ func Labels(name string) ([]byte, error) {
func fileFromDir(name string) ([]byte, error) { func fileFromDir(name string) ([]byte, error) {
customPath := path.Join(setting.CustomPath, "options", name) 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) return ioutil.ReadFile(customPath)
} }
staticPath := path.Join(setting.StaticRootPath, "options", name) 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) return ioutil.ReadFile(staticPath)
} }

View file

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

View file

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

View file

@ -13,8 +13,8 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"
"github.com/unknwon/com"
"xorm.io/builder" "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()) return models.ErrCancelledf("during gathering missing repo records before checking %s", repo.FullName())
default: 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) repos = append(repos, repo)
} }
return nil return nil

View file

@ -13,8 +13,6 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
"github.com/unknwon/com"
) )
// CreateRepository creates a repository for the user/organization. // 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) 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. // repo already exists - We have two or three options.
// 1. We fail stating that the directory exists // 1. We fail stating that the directory exists
// 2. We create the db repository to go with this data and adopt the git repo // 2. We create the db repository to go with this data and adopt the git repo

View file

@ -19,7 +19,6 @@ import (
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
"github.com/huandu/xstrings" "github.com/huandu/xstrings"
"github.com/unknwon/com"
) )
type transformer struct { type transformer struct {
@ -252,7 +251,12 @@ func GenerateRepository(ctx models.DBContext, doer, owner *models.User, template
} }
repoPath := generateRepo.RepoPath() 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{ return nil, models.ErrRepoFilesAlreadyExist{
Uname: generateRepo.OwnerName, Uname: generateRepo.OwnerName,
Name: generateRepo.Name, Name: generateRepo.Name,

View file

@ -15,7 +15,6 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
"github.com/unknwon/com"
"xorm.io/builder" "xorm.io/builder"
) )
@ -112,15 +111,27 @@ func CheckDelegateHooks(repoPath string) ([]string, error) {
newHookPath := filepath.Join(hookDir, hookName+".d", "gitea") newHookPath := filepath.Join(hookDir, hookName+".d", "gitea")
cont := false 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)) results = append(results, fmt.Sprintf("old hook file %s does not exist", oldHookPath))
cont = true 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")) results = append(results, fmt.Sprintf("hooks directory %s does not exist", oldHookPath+".d"))
cont = true 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)) results = append(results, fmt.Sprintf("new hook file %s does not exist", newHookPath))
cont = true cont = true
} }

View file

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

View file

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

View file

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

View file

@ -22,6 +22,7 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"github.com/gliderlabs/ssh" "github.com/gliderlabs/ssh"
"github.com/unknwon/com" "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") 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) filePath := filepath.Dir(keyPath)
if err := os.MkdirAll(filePath, os.ModePerm); err != nil { 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) log.Trace("New private key is generated: %s", keyPath)
} }
err := srv.SetOption(ssh.HostKeyFile(keyPath)) err = srv.SetOption(ssh.HostKeyFile(keyPath))
if err != nil { if err != nil {
log.Error("Failed to set Host Key. %s", err) log.Error("Failed to set Host Key. %s", err)
} }

View file

@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"gitea.com/macaron/macaron" "gitea.com/macaron/macaron"
"github.com/unknwon/com" "github.com/unknwon/com"
@ -59,7 +60,11 @@ func Mailer() (*texttmpl.Template, *template.Template) {
staticDir := path.Join(setting.StaticRootPath, "templates", "mail") 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) files, err := com.StatDir(staticDir)
if err != nil { if err != nil {
@ -84,7 +89,11 @@ func Mailer() (*texttmpl.Template, *template.Template) {
customDir := path.Join(setting.CustomPath, "templates", "mail") 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) files, err := com.StatDir(customDir)
if err != nil { if err != nil {

View file

@ -18,6 +18,7 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"gitea.com/macaron/macaron" "gitea.com/macaron/macaron"
"github.com/unknwon/com" "github.com/unknwon/com"
@ -77,8 +78,11 @@ func NewTemplateFileSystem() templateFileSystem {
} }
customDir := path.Join(setting.CustomPath, "templates") customDir := path.Join(setting.CustomPath, "templates")
isDir, err := util.IsDir(customDir)
if com.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) files, err := com.StatDir(customDir)
if err != nil { if err != nil {
@ -170,8 +174,11 @@ func Mailer() (*texttmpl.Template, *template.Template) {
} }
customDir := path.Join(setting.CustomPath, "templates", "mail") customDir := path.Join(setting.CustomPath, "templates", "mail")
isDir, err := util.IsDir(customDir)
if com.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) files, err := com.StatDir(customDir)
if err != nil { if err != nil {

View file

@ -31,3 +31,42 @@ func GetDirectorySize(path string) (int64, error) {
}) })
return size, err 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
}

View file

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

View file

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

View file

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

View file

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

View file

@ -21,8 +21,7 @@ import (
"code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"github.com/unknwon/com"
) )
// ArchiveRequest defines the parameters of an archive request, which notably // 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) 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 { if err := os.MkdirAll(r.archivePath, os.ModePerm); err != nil {
ctx.ServerError("Download -> os.MkdirAll(archivePath)", err) ctx.ServerError("Download -> os.MkdirAll(archivePath)", err)
return nil return nil
@ -146,9 +150,6 @@ func DeriveRequestFrom(ctx *context.Context, uri string) *ArchiveRequest {
} }
// Get corresponding commit. // Get corresponding commit.
var (
err error
)
if r.repo.IsBranchExist(r.refName) { if r.repo.IsBranchExist(r.refName) {
r.commit, err = r.repo.GetBranchCommit(r.refName) r.commit, err = r.repo.GetBranchCommit(r.refName)
if err != nil { 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.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 return r
} }
@ -198,7 +203,11 @@ func doArchive(r *ArchiveRequest) {
// race conditions and difficulties in locking. Do one last check that // 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, // the archive we're referring to doesn't already exist. If it does exist,
// then just mark the request as complete and move on. // 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 r.archiveComplete = true
return return
} }