Skip to content

Commit 7b0f3e6

Browse files
committed
uniform all temporary directories
1 parent d70af38 commit 7b0f3e6

File tree

25 files changed

+82
-66
lines changed

25 files changed

+82
-66
lines changed

build/generate-gitignores.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"path/filepath"
1616
"strings"
1717

18+
"code.gitea.io/gitea/modules/setting"
1819
"code.gitea.io/gitea/modules/util"
1920
)
2021

@@ -32,7 +33,7 @@ func main() {
3233
flag.StringVar(&githubApiToken, "token", "", "github api token")
3334
flag.Parse()
3435

35-
file, err := os.CreateTemp(os.TempDir(), prefix)
36+
file, err := os.CreateTemp(setting.TempDir(), prefix)
3637
if err != nil {
3738
log.Fatalf("Failed to create temp file. %s", err)
3839
}

build/generate-licenses.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"code.gitea.io/gitea/build/license"
2424
"code.gitea.io/gitea/modules/json"
25+
"code.gitea.io/gitea/modules/setting"
2526
"code.gitea.io/gitea/modules/util"
2627
)
2728

@@ -39,7 +40,7 @@ func main() {
3940
flag.StringVar(&githubApiToken, "token", "", "github api token")
4041
flag.Parse()
4142

42-
file, err := os.CreateTemp(os.TempDir(), prefix)
43+
file, err := os.CreateTemp(setting.TempDir(), prefix)
4344
if err != nil {
4445
log.Fatalf("Failed to create temp file. %s", err)
4546
}

cmd/dump.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var CmdDump = &cli.Command{
4949
&cli.StringFlag{
5050
Name: "tempdir",
5151
Aliases: []string{"t"},
52-
Value: os.TempDir(),
52+
Value: setting.TempDir(),
5353
Usage: "Temporary dir path",
5454
},
5555
&cli.StringFlag{

models/migrations/base/tests.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func MainTest(m *testing.M) {
141141
setting.CustomConf = giteaConf
142142
}
143143

144-
tmpDataPath, err := os.MkdirTemp("", "data")
144+
tmpDataPath, err := os.MkdirTemp(setting.TempDir(), "data")
145145
if err != nil {
146146
fmt.Printf("Unable to create temporary data path %v\n", err)
147147
os.Exit(1)

models/unittest/testdb.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ func MainTest(m *testing.M, testOpts ...*TestOptions) {
124124
setting.SSH.Domain = "try.gitea.io"
125125
setting.Database.Type = "sqlite3"
126126
setting.Repository.DefaultBranch = "master" // many test code still assume that default branch is called "master"
127-
repoRootPath, err := os.MkdirTemp(os.TempDir(), "repos")
127+
repoRootPath, err := os.MkdirTemp(setting.TempDir(), "repos")
128128
if err != nil {
129129
fatalTestError("TempDir: %v\n", err)
130130
}
131131
setting.RepoRootPath = repoRootPath
132-
appDataPath, err := os.MkdirTemp(os.TempDir(), "appdata")
132+
appDataPath, err := os.MkdirTemp(setting.TempDir(), "appdata")
133133
if err != nil {
134134
fatalTestError("TempDir: %v\n", err)
135135
}

modules/git/blame.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os"
1313

1414
"code.gitea.io/gitea/modules/log"
15+
"code.gitea.io/gitea/modules/setting"
1516
"code.gitea.io/gitea/modules/util"
1617
)
1718

@@ -195,7 +196,7 @@ func tryCreateBlameIgnoreRevsFile(commit *Commit) *string {
195196
}
196197
defer r.Close()
197198

198-
f, err := os.CreateTemp("", "gitea_git-blame-ignore-revs")
199+
f, err := os.CreateTemp(setting.TempDir(), "gitea_git-blame-ignore-revs")
199200
if err != nil {
200201
return nil
201202
}

modules/git/repo_index.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (repo *Repository) readTreeToIndex(id ObjectID, indexFilename ...string) er
5151

5252
// ReadTreeToTemporaryIndex reads a treeish to a temporary index file
5353
func (repo *Repository) ReadTreeToTemporaryIndex(treeish string) (filename, tmpDir string, cancel context.CancelFunc, err error) {
54-
tmpDir, err = os.MkdirTemp("", "index")
54+
tmpDir, err = os.MkdirTemp(os.TempDir(), "index")
5555
if err != nil {
5656
return filename, tmpDir, cancel, err
5757
}

modules/markup/external/external.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (p *Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.
8989

9090
if p.IsInputFile {
9191
// write to temp file
92-
f, err := os.CreateTemp("", "gitea_input")
92+
f, err := os.CreateTemp(setting.TempDir(), "gitea_input")
9393
if err != nil {
9494
return fmt.Errorf("%s create temp file when rendering %s failed: %w", p.Name(), p.Command, err)
9595
}

modules/repository/temp.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,44 @@ package repository
66
import (
77
"fmt"
88
"os"
9-
"path"
109
"path/filepath"
1110

1211
"code.gitea.io/gitea/modules/log"
1312
"code.gitea.io/gitea/modules/setting"
1413
"code.gitea.io/gitea/modules/util"
1514
)
1615

17-
// LocalCopyPath returns the local repository temporary copy path.
18-
func LocalCopyPath() string {
19-
if filepath.IsAbs(setting.Repository.Local.LocalCopyPath) {
20-
return setting.Repository.Local.LocalCopyPath
16+
// localCopyPath returns the local repository temporary copy path.
17+
func localCopyPath() string {
18+
return filepath.Join(setting.TempDir(), "local-repo")
19+
}
20+
21+
func CleanUpTemporaryPaths() {
22+
if err := util.RemoveAll(localCopyPath()); err != nil {
23+
log.Error("Unable to remove local repository temporary copy path: %s (%v)", localCopyPath(), err)
2124
}
22-
return path.Join(setting.AppDataPath, setting.Repository.Local.LocalCopyPath)
2325
}
2426

2527
// CreateTemporaryPath creates a temporary path
26-
func CreateTemporaryPath(prefix string) (string, error) {
27-
if err := os.MkdirAll(LocalCopyPath(), os.ModePerm); err != nil {
28-
log.Error("Unable to create localcopypath directory: %s (%v)", LocalCopyPath(), err)
29-
return "", fmt.Errorf("Failed to create localcopypath directory %s: %w", LocalCopyPath(), err)
28+
func CreateTemporaryPath(prefix string) (string, func(), error) {
29+
if err := os.MkdirAll(localCopyPath(), os.ModePerm); err != nil {
30+
log.Error("Unable to create localcopypath directory: %s (%v)", localCopyPath(), err)
31+
return "", func() {}, fmt.Errorf("failed to create localcopypath directory %s: %w", localCopyPath(), err)
3032
}
31-
basePath, err := os.MkdirTemp(LocalCopyPath(), prefix+".git")
33+
basePath, err := os.MkdirTemp(localCopyPath(), prefix+".git")
3234
if err != nil {
3335
log.Error("Unable to create temporary directory: %s-*.git (%v)", prefix, err)
34-
return "", fmt.Errorf("Failed to create dir %s-*.git: %w", prefix, err)
36+
return "", func() {}, fmt.Errorf("failed to create dir %s-*.git: %w", prefix, err)
3537
}
36-
return basePath, nil
38+
return basePath, func() {
39+
if err := removeTemporaryPath(basePath); err != nil {
40+
log.Error("Unable to remove temporary directory: %s (%v)", basePath, err)
41+
}
42+
}, nil
3743
}
3844

39-
// RemoveTemporaryPath removes the temporary path
40-
func RemoveTemporaryPath(basePath string) error {
45+
// removeTemporaryPath removes the temporary path
46+
func removeTemporaryPath(basePath string) error {
4147
if _, err := os.Stat(basePath); !os.IsNotExist(err) {
4248
return util.RemoveAll(basePath)
4349
}

modules/setting/global.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
package setting
55

6+
import "os"
7+
68
// Global settings
79
var (
810
// RunUser is the OS user that Gitea is running as. ini:"RUN_USER"
@@ -16,3 +18,8 @@ var (
1618
// AppName is the Application name, used in the page title. ini: "APP_NAME"
1719
AppName string
1820
)
21+
22+
// TempDir returns the OS temp directory
23+
func TempDir() string {
24+
return os.TempDir()
25+
}

0 commit comments

Comments
 (0)