mirror of
https://github.com/go-gitea/gitea
synced 2025-07-23 18:58:38 +00:00
Uniform all temporary directories and allow customizing temp path (#32352)
This PR uniform all temporary directory usage so that it will be easier to manage. Relate to #31792 - [x] Added a new setting to allow users to configure the global temporary directory. - [x] Move all temporary files and directories to be placed under os.Temp()/gitea. - [x] `setting.Repository.Local.LocalCopyPath` now will be `setting.TempPath/local-repo` and the customized path is removed. ```diff -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;[repository.local] -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; -;; Path for local repository copy. Defaults to TEMP_PATH + `local-repo`, this is deprecated and cannot be changed -;LOCAL_COPY_PATH = local-repo ``` - [x] `setting.Repository.Upload.TempPath` now will be `settting.TempPath/uploads` and the customized path is removed. ```diff ;[repository.upload] -;; -;; Path for uploads. Defaults to TEMP_PATH + `uploads` -;TEMP_PATH = uploads ``` - [x] `setting.Packages.ChunkedUploadPath` now will be `settting.TempPath/package-upload` and the customized path is removed. ```diff ;[packages] -;; -;; Path for chunked uploads. Defaults it's `package-upload` under `TEMP_PATH` unless it's an absolute path. -;CHUNKED_UPLOAD_PATH = package-upload ``` - [x] `setting.SSH.KeyTestPath` now will be `settting.TempPath/ssh_key_test` and the customized path is removed. ```diff [server] -;; -;; Directory to create temporary files in when testing public keys using ssh-keygen, -;; default is the system temporary directory. -;SSH_KEY_TEST_PATH = ``` TODO: - [ ] setting.PprofDataPath haven't been changed because it may need to be kept until somebody read it but temp path may be clean up any time. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
@@ -355,23 +355,19 @@ func checkConflicts(ctx context.Context, pr *issues_model.PullRequest, gitRepo *
|
||||
}
|
||||
|
||||
// 3b. Create a plain patch from head to base
|
||||
tmpPatchFile, err := os.CreateTemp("", "patch")
|
||||
tmpPatchFile, cleanup, err := setting.AppDataTempDir("git-repo-content").CreateTempFileRandom("patch")
|
||||
if err != nil {
|
||||
log.Error("Unable to create temporary patch file! Error: %v", err)
|
||||
return false, fmt.Errorf("unable to create temporary patch file! Error: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
_ = util.Remove(tmpPatchFile.Name())
|
||||
}()
|
||||
defer cleanup()
|
||||
|
||||
if err := gitRepo.GetDiffBinary(pr.MergeBase+"...tracking", tmpPatchFile); err != nil {
|
||||
tmpPatchFile.Close()
|
||||
log.Error("Unable to get patch file from %s to %s in %s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
|
||||
return false, fmt.Errorf("unable to get patch file from %s to %s in %s Error: %w", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
|
||||
}
|
||||
stat, err := tmpPatchFile.Stat()
|
||||
if err != nil {
|
||||
tmpPatchFile.Close()
|
||||
return false, fmt.Errorf("unable to stat patch file: %w", err)
|
||||
}
|
||||
patchPath := tmpPatchFile.Name()
|
||||
|
@@ -74,11 +74,13 @@ func createTemporaryRepoForPR(ctx context.Context, pr *issues_model.PullRequest)
|
||||
}
|
||||
|
||||
// Clone base repo.
|
||||
tmpBasePath, err := repo_module.CreateTemporaryPath("pull")
|
||||
tmpBasePath, cleanup, err := repo_module.CreateTemporaryPath("pull")
|
||||
if err != nil {
|
||||
log.Error("CreateTemporaryPath[%-v]: %v", pr, err)
|
||||
return nil, nil, err
|
||||
}
|
||||
cancel = cleanup
|
||||
|
||||
prCtx = &prContext{
|
||||
Context: ctx,
|
||||
tmpBasePath: tmpBasePath,
|
||||
@@ -86,11 +88,6 @@ func createTemporaryRepoForPR(ctx context.Context, pr *issues_model.PullRequest)
|
||||
outbuf: &strings.Builder{},
|
||||
errbuf: &strings.Builder{},
|
||||
}
|
||||
cancel = func() {
|
||||
if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
|
||||
log.Error("Error whilst removing removing temporary repo for %-v: %v", pr, err)
|
||||
}
|
||||
}
|
||||
|
||||
baseRepoPath := pr.BaseRepo.RepoPath()
|
||||
headRepoPath := pr.HeadRepo.RepoPath()
|
||||
|
@@ -29,7 +29,6 @@ import (
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/templates/vars"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
)
|
||||
|
||||
// CreateRepoOptions contains the create repository options
|
||||
@@ -150,15 +149,11 @@ func initRepository(ctx context.Context, u *user_model.User, repo *repo_model.Re
|
||||
|
||||
// Initialize repository according to user's choice.
|
||||
if opts.AutoInit {
|
||||
tmpDir, err := os.MkdirTemp(os.TempDir(), "gitea-"+repo.Name)
|
||||
tmpDir, cleanup, err := setting.AppDataTempDir("git-repo-content").MkdirTempRandom("repos-" + repo.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to create temp dir for repository %s: %w", repo.FullName(), err)
|
||||
return fmt.Errorf("failed to create temp dir for repository %s: %w", repo.FullName(), err)
|
||||
}
|
||||
defer func() {
|
||||
if err := util.RemoveAll(tmpDir); err != nil {
|
||||
log.Warn("Unable to remove temporary directory: %s: Error: %v", tmpDir, err)
|
||||
}
|
||||
}()
|
||||
defer cleanup()
|
||||
|
||||
if err = prepareRepoCommit(ctx, repo, tmpDir, opts); err != nil {
|
||||
return fmt.Errorf("prepareRepoCommit: %w", err)
|
||||
|
@@ -30,23 +30,24 @@ type TemporaryUploadRepository struct {
|
||||
repo *repo_model.Repository
|
||||
gitRepo *git.Repository
|
||||
basePath string
|
||||
cleanup func()
|
||||
}
|
||||
|
||||
// NewTemporaryUploadRepository creates a new temporary upload repository
|
||||
func NewTemporaryUploadRepository(repo *repo_model.Repository) (*TemporaryUploadRepository, error) {
|
||||
basePath, err := repo_module.CreateTemporaryPath("upload")
|
||||
basePath, cleanup, err := repo_module.CreateTemporaryPath("upload")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t := &TemporaryUploadRepository{repo: repo, basePath: basePath}
|
||||
t := &TemporaryUploadRepository{repo: repo, basePath: basePath, cleanup: cleanup}
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// Close the repository cleaning up all files
|
||||
func (t *TemporaryUploadRepository) Close() {
|
||||
defer t.gitRepo.Close()
|
||||
if err := repo_module.RemoveTemporaryPath(t.basePath); err != nil {
|
||||
log.Error("Failed to remove temporary path %s: %v", t.basePath, err)
|
||||
if t.cleanup != nil {
|
||||
t.cleanup()
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -21,6 +21,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/gitrepo"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
repo_module "code.gitea.io/gitea/modules/repository"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
|
||||
"github.com/gobwas/glob"
|
||||
@@ -255,16 +256,11 @@ func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *r
|
||||
}
|
||||
|
||||
func generateGitContent(ctx context.Context, repo, templateRepo, generateRepo *repo_model.Repository) (err error) {
|
||||
tmpDir, err := os.MkdirTemp(os.TempDir(), "gitea-"+repo.Name)
|
||||
tmpDir, cleanup, err := setting.AppDataTempDir("git-repo-content").MkdirTempRandom("gitea-" + repo.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to create temp dir for repository %s: %w", repo.FullName(), err)
|
||||
return fmt.Errorf("failed to create temp dir for repository %s: %w", repo.FullName(), err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := util.RemoveAll(tmpDir); err != nil {
|
||||
log.Error("RemoveAll: %v", err)
|
||||
}
|
||||
}()
|
||||
defer cleanup()
|
||||
|
||||
if err = generateRepoCommit(ctx, repo, templateRepo, generateRepo, tmpDir); err != nil {
|
||||
return fmt.Errorf("generateRepoCommit: %w", err)
|
||||
|
@@ -13,7 +13,6 @@ import (
|
||||
issues_model "code.gitea.io/gitea/models/issues"
|
||||
"code.gitea.io/gitea/models/organization"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
system_model "code.gitea.io/gitea/models/system"
|
||||
"code.gitea.io/gitea/models/unit"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/graceful"
|
||||
@@ -102,8 +101,6 @@ func Init(ctx context.Context) error {
|
||||
if err := repo_module.LoadRepoConfig(); err != nil {
|
||||
return err
|
||||
}
|
||||
system_model.RemoveAllWithNotice(ctx, "Clean up temporary repository uploads", setting.Repository.Upload.TempPath)
|
||||
system_model.RemoveAllWithNotice(ctx, "Clean up temporary repositories", repo_module.LocalCopyPath())
|
||||
if err := initPushQueue(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@@ -102,15 +102,11 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
|
||||
|
||||
hasDefaultBranch := gitrepo.IsBranchExist(ctx, repo.WikiStorageRepo(), repo.DefaultWikiBranch)
|
||||
|
||||
basePath, err := repo_module.CreateTemporaryPath("update-wiki")
|
||||
basePath, cleanup, err := repo_module.CreateTemporaryPath("update-wiki")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if err := repo_module.RemoveTemporaryPath(basePath); err != nil {
|
||||
log.Error("Merge: RemoveTemporaryPath: %s", err)
|
||||
}
|
||||
}()
|
||||
defer cleanup()
|
||||
|
||||
cloneOpts := git.CloneRepoOptions{
|
||||
Bare: true,
|
||||
@@ -264,15 +260,11 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
|
||||
return fmt.Errorf("InitWiki: %w", err)
|
||||
}
|
||||
|
||||
basePath, err := repo_module.CreateTemporaryPath("update-wiki")
|
||||
basePath, cleanup, err := repo_module.CreateTemporaryPath("update-wiki")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if err := repo_module.RemoveTemporaryPath(basePath); err != nil {
|
||||
log.Error("Merge: RemoveTemporaryPath: %s", err)
|
||||
}
|
||||
}()
|
||||
defer cleanup()
|
||||
|
||||
if err := git.Clone(ctx, repo.WikiPath(), basePath, git.CloneRepoOptions{
|
||||
Bare: true,
|
||||
|
Reference in New Issue
Block a user