mirror of
https://github.com/go-gitea/gitea.git
synced 2025-04-08 17:05:45 +02:00
uniform all temporary directories
This commit is contained in:
parent
d70af38447
commit
7b0f3e6473
@ -15,6 +15,7 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
)
|
||||
|
||||
@ -32,7 +33,7 @@ func main() {
|
||||
flag.StringVar(&githubApiToken, "token", "", "github api token")
|
||||
flag.Parse()
|
||||
|
||||
file, err := os.CreateTemp(os.TempDir(), prefix)
|
||||
file, err := os.CreateTemp(setting.TempDir(), prefix)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create temp file. %s", err)
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/build/license"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
)
|
||||
|
||||
@ -39,7 +40,7 @@ func main() {
|
||||
flag.StringVar(&githubApiToken, "token", "", "github api token")
|
||||
flag.Parse()
|
||||
|
||||
file, err := os.CreateTemp(os.TempDir(), prefix)
|
||||
file, err := os.CreateTemp(setting.TempDir(), prefix)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create temp file. %s", err)
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ var CmdDump = &cli.Command{
|
||||
&cli.StringFlag{
|
||||
Name: "tempdir",
|
||||
Aliases: []string{"t"},
|
||||
Value: os.TempDir(),
|
||||
Value: setting.TempDir(),
|
||||
Usage: "Temporary dir path",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
|
@ -141,7 +141,7 @@ func MainTest(m *testing.M) {
|
||||
setting.CustomConf = giteaConf
|
||||
}
|
||||
|
||||
tmpDataPath, err := os.MkdirTemp("", "data")
|
||||
tmpDataPath, err := os.MkdirTemp(setting.TempDir(), "data")
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to create temporary data path %v\n", err)
|
||||
os.Exit(1)
|
||||
|
@ -124,12 +124,12 @@ func MainTest(m *testing.M, testOpts ...*TestOptions) {
|
||||
setting.SSH.Domain = "try.gitea.io"
|
||||
setting.Database.Type = "sqlite3"
|
||||
setting.Repository.DefaultBranch = "master" // many test code still assume that default branch is called "master"
|
||||
repoRootPath, err := os.MkdirTemp(os.TempDir(), "repos")
|
||||
repoRootPath, err := os.MkdirTemp(setting.TempDir(), "repos")
|
||||
if err != nil {
|
||||
fatalTestError("TempDir: %v\n", err)
|
||||
}
|
||||
setting.RepoRootPath = repoRootPath
|
||||
appDataPath, err := os.MkdirTemp(os.TempDir(), "appdata")
|
||||
appDataPath, err := os.MkdirTemp(setting.TempDir(), "appdata")
|
||||
if err != nil {
|
||||
fatalTestError("TempDir: %v\n", err)
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import (
|
||||
"os"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
)
|
||||
|
||||
@ -195,7 +196,7 @@ func tryCreateBlameIgnoreRevsFile(commit *Commit) *string {
|
||||
}
|
||||
defer r.Close()
|
||||
|
||||
f, err := os.CreateTemp("", "gitea_git-blame-ignore-revs")
|
||||
f, err := os.CreateTemp(setting.TempDir(), "gitea_git-blame-ignore-revs")
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ func (repo *Repository) readTreeToIndex(id ObjectID, indexFilename ...string) er
|
||||
|
||||
// ReadTreeToTemporaryIndex reads a treeish to a temporary index file
|
||||
func (repo *Repository) ReadTreeToTemporaryIndex(treeish string) (filename, tmpDir string, cancel context.CancelFunc, err error) {
|
||||
tmpDir, err = os.MkdirTemp("", "index")
|
||||
tmpDir, err = os.MkdirTemp(os.TempDir(), "index")
|
||||
if err != nil {
|
||||
return filename, tmpDir, cancel, err
|
||||
}
|
||||
|
2
modules/markup/external/external.go
vendored
2
modules/markup/external/external.go
vendored
@ -89,7 +89,7 @@ func (p *Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.
|
||||
|
||||
if p.IsInputFile {
|
||||
// write to temp file
|
||||
f, err := os.CreateTemp("", "gitea_input")
|
||||
f, err := os.CreateTemp(setting.TempDir(), "gitea_input")
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s create temp file when rendering %s failed: %w", p.Name(), p.Command, err)
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ package repository
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
@ -14,30 +13,37 @@ import (
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
)
|
||||
|
||||
// LocalCopyPath returns the local repository temporary copy path.
|
||||
func LocalCopyPath() string {
|
||||
if filepath.IsAbs(setting.Repository.Local.LocalCopyPath) {
|
||||
return setting.Repository.Local.LocalCopyPath
|
||||
// localCopyPath returns the local repository temporary copy path.
|
||||
func localCopyPath() string {
|
||||
return filepath.Join(setting.TempDir(), "local-repo")
|
||||
}
|
||||
|
||||
func CleanUpTemporaryPaths() {
|
||||
if err := util.RemoveAll(localCopyPath()); err != nil {
|
||||
log.Error("Unable to remove local repository temporary copy path: %s (%v)", localCopyPath(), err)
|
||||
}
|
||||
return path.Join(setting.AppDataPath, setting.Repository.Local.LocalCopyPath)
|
||||
}
|
||||
|
||||
// CreateTemporaryPath creates a temporary path
|
||||
func CreateTemporaryPath(prefix string) (string, error) {
|
||||
if err := os.MkdirAll(LocalCopyPath(), os.ModePerm); err != nil {
|
||||
log.Error("Unable to create localcopypath directory: %s (%v)", LocalCopyPath(), err)
|
||||
return "", fmt.Errorf("Failed to create localcopypath directory %s: %w", LocalCopyPath(), err)
|
||||
func CreateTemporaryPath(prefix string) (string, func(), error) {
|
||||
if err := os.MkdirAll(localCopyPath(), os.ModePerm); err != nil {
|
||||
log.Error("Unable to create localcopypath directory: %s (%v)", localCopyPath(), err)
|
||||
return "", func() {}, fmt.Errorf("failed to create localcopypath directory %s: %w", localCopyPath(), err)
|
||||
}
|
||||
basePath, err := os.MkdirTemp(LocalCopyPath(), prefix+".git")
|
||||
basePath, err := os.MkdirTemp(localCopyPath(), prefix+".git")
|
||||
if err != nil {
|
||||
log.Error("Unable to create temporary directory: %s-*.git (%v)", prefix, err)
|
||||
return "", fmt.Errorf("Failed to create dir %s-*.git: %w", prefix, err)
|
||||
return "", func() {}, fmt.Errorf("failed to create dir %s-*.git: %w", prefix, err)
|
||||
}
|
||||
return basePath, nil
|
||||
return basePath, func() {
|
||||
if err := removeTemporaryPath(basePath); err != nil {
|
||||
log.Error("Unable to remove temporary directory: %s (%v)", basePath, err)
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
// RemoveTemporaryPath removes the temporary path
|
||||
func RemoveTemporaryPath(basePath string) error {
|
||||
// removeTemporaryPath removes the temporary path
|
||||
func removeTemporaryPath(basePath string) error {
|
||||
if _, err := os.Stat(basePath); !os.IsNotExist(err) {
|
||||
return util.RemoveAll(basePath)
|
||||
}
|
||||
|
@ -3,6 +3,8 @@
|
||||
|
||||
package setting
|
||||
|
||||
import "os"
|
||||
|
||||
// Global settings
|
||||
var (
|
||||
// RunUser is the OS user that Gitea is running as. ini:"RUN_USER"
|
||||
@ -16,3 +18,8 @@ var (
|
||||
// AppName is the Application name, used in the page title. ini: "APP_NAME"
|
||||
AppName string
|
||||
)
|
||||
|
||||
// TempDir returns the OS temp directory
|
||||
func TempDir() string {
|
||||
return os.TempDir()
|
||||
}
|
||||
|
@ -4,7 +4,6 @@
|
||||
package setting
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@ -124,7 +123,7 @@ func loadSSHFrom(rootCfg ConfigProvider) {
|
||||
if len(serverMACs) > 0 {
|
||||
SSH.ServerMACs = serverMACs
|
||||
}
|
||||
SSH.KeyTestPath = os.TempDir()
|
||||
SSH.KeyTestPath = TempDir()
|
||||
if err = sec.MapTo(&SSH); err != nil {
|
||||
log.Fatal("Failed to map SSH settings: %v", err)
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ func NewLocalStorage(ctx context.Context, config *setting.Storage) (ObjectStorag
|
||||
}
|
||||
|
||||
if config.TemporaryPath == "" {
|
||||
config.TemporaryPath = filepath.Join(config.Path, "tmp")
|
||||
config.TemporaryPath = filepath.Join(setting.TempDir(), filepath.Base(config.Path))
|
||||
}
|
||||
if !filepath.IsAbs(config.TemporaryPath) {
|
||||
return nil, fmt.Errorf("LocalStorageConfig.TemporaryPath should be an absolute path, but not: %q", config.TemporaryPath)
|
||||
|
@ -4,7 +4,6 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
@ -56,6 +55,6 @@ func TestBuildLocalPath(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLocalStorageIterator(t *testing.T) {
|
||||
dir := filepath.Join(os.TempDir(), "TestLocalStorageIteratorTestDir")
|
||||
dir := filepath.Join(t.TempDir(), "TestLocalStorageIteratorTestDir")
|
||||
testStorageIterator(t, setting.LocalStorageType, &setting.Storage{Path: dir})
|
||||
}
|
||||
|
@ -9,6 +9,8 @@ import (
|
||||
"io"
|
||||
"math"
|
||||
"os"
|
||||
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -73,7 +75,7 @@ func (b *FileBackedBuffer) Write(p []byte) (int, error) {
|
||||
n, err = b.file.Write(p)
|
||||
} else {
|
||||
if b.size+int64(len(p)) > b.maxMemorySize {
|
||||
b.file, err = os.CreateTemp("", "gitea-buffer-")
|
||||
b.file, err = os.CreateTemp(setting.TempDir(), "gitea-buffer-")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
@ -11,13 +11,15 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCopyFile(t *testing.T) {
|
||||
testContent := []byte("hello")
|
||||
|
||||
tmpDir := os.TempDir()
|
||||
tmpDir := setting.TempDir()
|
||||
now := time.Now()
|
||||
srcFile := fmt.Sprintf("%s/copy-test-%d-src.txt", tmpDir, now.UnixMicro())
|
||||
dstFile := fmt.Sprintf("%s/copy-test-%d-dst.txt", tmpDir, now.UnixMicro())
|
||||
|
@ -303,7 +303,7 @@ var (
|
||||
|
||||
func dummyInfoRefs(ctx *context.Context) {
|
||||
infoRefsOnce.Do(func() {
|
||||
tmpDir, err := os.MkdirTemp(os.TempDir(), "gitea-info-refs-cache")
|
||||
tmpDir, err := os.MkdirTemp(setting.TempDir(), "gitea-info-refs-cache")
|
||||
if err != nil {
|
||||
log.Error("Failed to create temp dir for git-receive-pack cache: %v", err)
|
||||
return
|
||||
|
@ -109,17 +109,13 @@ func LFSLocks(ctx *context.Context) {
|
||||
}
|
||||
|
||||
// Clone base repo.
|
||||
tmpBasePath, err := repo_module.CreateTemporaryPath("locks")
|
||||
tmpBasePath, cancel, err := repo_module.CreateTemporaryPath("locks")
|
||||
if err != nil {
|
||||
log.Error("Failed to create temporary path: %v", err)
|
||||
ctx.ServerError("LFSLocks", err)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
|
||||
log.Error("LFSLocks: RemoveTemporaryPath: %v", err)
|
||||
}
|
||||
}()
|
||||
defer cancel()
|
||||
|
||||
if err := git.Clone(ctx, ctx.Repo.Repository.RepoPath(), tmpBasePath, git.CloneRepoOptions{
|
||||
Bare: true,
|
||||
|
@ -346,7 +346,7 @@ 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, err := os.CreateTemp(setting.TempDir(), "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)
|
||||
|
@ -73,11 +73,13 @@ func createTemporaryRepoForPR(ctx context.Context, pr *issues_model.PullRequest)
|
||||
}
|
||||
|
||||
// Clone base repo.
|
||||
tmpBasePath, err := repo_module.CreateTemporaryPath("pull")
|
||||
tmpBasePath, cancel, err := repo_module.CreateTemporaryPath("pull")
|
||||
if err != nil {
|
||||
log.Error("CreateTemporaryPath[%-v]: %v", pr, err)
|
||||
return nil, nil, err
|
||||
}
|
||||
defer cancel()
|
||||
|
||||
prCtx = &prContext{
|
||||
Context: ctx,
|
||||
tmpBasePath: tmpBasePath,
|
||||
@ -85,11 +87,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()
|
||||
|
@ -134,6 +134,10 @@ func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir,
|
||||
return nil
|
||||
}
|
||||
|
||||
func createRepoTempDir(repoName string) (string, error) {
|
||||
return os.MkdirTemp(filepath.Join(setting.TempDir(), "repos"), "gitea-"+repoName)
|
||||
}
|
||||
|
||||
// InitRepository initializes README and .gitignore if needed.
|
||||
func initRepository(ctx context.Context, repoPath string, u *user_model.User, repo *repo_model.Repository, opts CreateRepoOptions) (err error) {
|
||||
if err = repo_module.CheckInitRepository(ctx, repo.OwnerName, repo.Name, opts.ObjectFormatName); err != nil {
|
||||
@ -142,9 +146,9 @@ func initRepository(ctx context.Context, repoPath string, u *user_model.User, re
|
||||
|
||||
// Initialize repository according to user's choice.
|
||||
if opts.AutoInit {
|
||||
tmpDir, err := os.MkdirTemp(os.TempDir(), "gitea-"+repo.Name)
|
||||
tmpDir, err := createRepoTempDir(repo.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to create temp dir for repository %s: %w", repo.RepoPath(), err)
|
||||
return fmt.Errorf("failed to create temp dir for repository %s: %w", repo.RepoPath(), err)
|
||||
}
|
||||
defer func() {
|
||||
if err := util.RemoveAll(tmpDir); err != nil {
|
||||
|
@ -30,23 +30,28 @@ type TemporaryUploadRepository struct {
|
||||
repo *repo_model.Repository
|
||||
gitRepo *git.Repository
|
||||
basePath string
|
||||
cancel func()
|
||||
}
|
||||
|
||||
// NewTemporaryUploadRepository creates a new temporary upload repository
|
||||
func NewTemporaryUploadRepository(ctx context.Context, repo *repo_model.Repository) (*TemporaryUploadRepository, error) {
|
||||
basePath, err := repo_module.CreateTemporaryPath("upload")
|
||||
basePath, cancel, err := repo_module.CreateTemporaryPath("upload")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t := &TemporaryUploadRepository{ctx: ctx, repo: repo, basePath: basePath}
|
||||
t := &TemporaryUploadRepository{
|
||||
ctx: ctx, repo: repo,
|
||||
basePath: basePath,
|
||||
cancel: cancel,
|
||||
}
|
||||
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.cancel != nil {
|
||||
t.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -23,6 +23,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"
|
||||
@ -253,7 +254,7 @@ 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, err := os.MkdirTemp(setting.TempDir(), "gitea-"+repo.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to create temp dir for repository %s: %w", repo.RepoPath(), err)
|
||||
}
|
||||
|
@ -107,7 +107,10 @@ func Init(ctx context.Context) error {
|
||||
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())
|
||||
repo_module.CleanUpTemporaryPaths()
|
||||
if err := system_model.CreateNotice(db.DefaultContext, system_model.NoticeRepository, "Clean up temporary repositories"); err != nil {
|
||||
log.Error("CreateRepositoryNotice: %v", err)
|
||||
}
|
||||
if err := initPushQueue(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -102,15 +102,11 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
|
||||
|
||||
hasDefaultBranch := git.IsBranchExist(ctx, repo.WikiPath(), repo.DefaultWikiBranch)
|
||||
|
||||
basePath, err := repo_module.CreateTemporaryPath("update-wiki")
|
||||
basePath, cancel, 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 cancel()
|
||||
|
||||
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, cancel, 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 cancel()
|
||||
|
||||
if err := git.Clone(ctx, repo.WikiPath(), basePath, git.CloneRepoOptions{
|
||||
Bare: true,
|
||||
|
@ -44,7 +44,7 @@ func TestDumpRestore(t *testing.T) {
|
||||
|
||||
reponame := "repo1"
|
||||
|
||||
basePath, err := os.MkdirTemp("", reponame)
|
||||
basePath, err := os.MkdirTemp(setting.TempDir(), reponame)
|
||||
assert.NoError(t, err)
|
||||
defer util.RemoveAll(basePath)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user