Skip to content

Commit b88c3a6

Browse files
committed
improvements
1 parent d1bf10a commit b88c3a6

File tree

8 files changed

+47
-20
lines changed

8 files changed

+47
-20
lines changed

custom/conf/app.example.ini

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@ LEVEL = Info
10791079
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
10801080
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
10811081
;;
1082-
;; Path for local repository copy. Defaults to `local-repo` under `TEMP_PATH` except it's an absolute path (content gets deleted on gitea restart)
1082+
;; Path for local repository copy. Defaults to TEMP_PATH + `local-repo`
10831083
;LOCAL_COPY_PATH = local-repo
10841084

10851085
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -1091,7 +1091,7 @@ LEVEL = Info
10911091
;; Whether repository file uploads are enabled. Defaults to `true`
10921092
;ENABLED = true
10931093
;;
1094-
;; Path for uploads. Defaults to `uploads` under `TEMP_PATH` except it's an absolute path (content gets deleted on gitea restart)
1094+
;; Path for uploads. Defaults to TEMP_PATH + `uploads`
10951095
;TEMP_PATH = uploads
10961096
;;
10971097
;; Comma-separated list of allowed file extensions (`.zip`), mime types (`text/plain`) or wildcard type (`image/*`, `audio/*`, `video/*`). Empty value or `*/*` allows all types.
@@ -2592,7 +2592,7 @@ LEVEL = Info
25922592
;; Currently, only `minio` and `azureblob` is supported.
25932593
;SERVE_DIRECT = false
25942594
;;
2595-
;; Path for chunked uploads. Defaults to `package-upload` under `TEMP_PATH` except it's an absolute path.
2595+
;; Path for chunked uploads. Defaults it's `package-upload` under `TEMP_PATH` unless it's an absolute path.
25962596
;CHUNKED_UPLOAD_PATH = package-upload
25972597
;;
25982598
;; Maximum count of package versions a single owner can have (`-1` means no limits)

modules/repository/temp.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414
"code.gitea.io/gitea/modules/util"
1515
)
1616

17-
// LocalCopyPath returns the local repository temporary copy path.
18-
func LocalCopyPath() string {
17+
// localCopyPath returns the local repository temporary copy path.
18+
func localCopyPath() string {
1919
if setting.Repository.Local.LocalCopyPath == "" {
2020
return filepath.Join(setting.TempPath, "local-repo")
2121
} else if !filepath.IsAbs(setting.Repository.Local.LocalCopyPath) {
@@ -26,14 +26,14 @@ func LocalCopyPath() string {
2626

2727
// CreateTemporaryPath creates a temporary path
2828
func CreateTemporaryPath(prefix string) (string, context.CancelFunc, 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)
29+
if err := os.MkdirAll(localCopyPath(), os.ModePerm); err != nil {
30+
log.Error("Unable to create localcopypath directory: %s (%v)", localCopyPath(), err)
31+
return "", nil, fmt.Errorf("failed to create localcopypath directory %s: %w", localCopyPath(), err)
3232
}
33-
basePath, err := os.MkdirTemp(LocalCopyPath(), prefix+".git")
33+
basePath, err := os.MkdirTemp(localCopyPath(), prefix+".git")
3434
if err != nil {
3535
log.Error("Unable to create temporary directory: %s-*.git (%v)", prefix, err)
36-
return "", func() {}, fmt.Errorf("failed to create dir %s-*.git: %w", prefix, err)
36+
return "", nil, fmt.Errorf("failed to create dir %s-*.git: %w", prefix, err)
3737
}
3838
return basePath, func() {
3939
if err := util.RemoveAll(basePath); err != nil {

modules/setting/setting.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package setting
77
import (
88
"fmt"
99
"os"
10+
"path/filepath"
1011
"runtime"
1112
"strings"
1213
"time"
@@ -187,12 +188,18 @@ func loadTempDir(rootCfg ConfigProvider) {
187188
rootSec := rootCfg.Section("")
188189
tempPath := rootSec.Key("TEMP_PATH").String()
189190
if tempPath != "" {
191+
if !filepath.IsAbs(tempPath) {
192+
tempPath = filepath.Join(os.TempDir(), tempPath)
193+
}
190194
TempPath = tempPath
191195
}
192-
if TempPath != "" {
193-
if err := os.MkdirAll(TempPath, os.ModePerm); err != nil {
194-
log.Fatal("Failed to create temp directory %s: %v", TempPath, err)
195-
}
196+
// TempPath has been initialized in init function of global.go
197+
if TempPath == "" {
198+
log.Fatal("It's impossible that TEMP_PATH is empty")
199+
}
200+
201+
if err := os.MkdirAll(TempPath, os.ModePerm); err != nil {
202+
log.Fatal("Failed to create temp directory %s: %v", TempPath, err)
196203
}
197204
}
198205

modules/storage/local.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func NewLocalStorage(ctx context.Context, config *setting.Storage) (ObjectStorag
3636
}
3737

3838
if config.TemporaryPath == "" {
39-
config.TemporaryPath = filepath.Join(setting.TempPath, filepath.Base(config.Path))
39+
config.TemporaryPath = filepath.Join(setting.TempPath, "storage", util.SanitizeDirName(config.Path))
4040
}
4141
if !filepath.IsAbs(config.TemporaryPath) {
4242
return nil, fmt.Errorf("LocalStorageConfig.TemporaryPath should be an absolute path, but not: %q", config.TemporaryPath)

modules/util/path.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,11 @@ func IsReadmeFileExtension(name string, ext ...string) (int, bool) {
291291

292292
return 0, false
293293
}
294+
295+
// SanitizeDirName replaces illegal characters in a directory name with "_"
296+
func SanitizeDirName(path string) string {
297+
p := filepath.Clean(path)
298+
re := regexp.MustCompile(`[<>:"/\\|?*]`)
299+
sanitized := re.ReplaceAllString(p, "_")
300+
return sanitized
301+
}

modules/util/path_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,20 @@ func TestListDirRecursively(t *testing.T) {
230230
require.NoError(t, err)
231231
assert.ElementsMatch(t, []string{"d1/f-d1", "d1/s1/f-d1s1"}, res)
232232
}
233+
234+
func TestSanitizeDirName(t *testing.T) {
235+
cases := []struct {
236+
path string
237+
expected string
238+
}{
239+
{"a", "a"},
240+
{"a/b", "a_b"},
241+
{"/a/b", "_a_b"},
242+
{"a/b/c", "a_b_c"},
243+
{"c:\\a\\b", "c__a_b"},
244+
{"c:\\a/b", "c__a_b"},
245+
}
246+
for _, c := range cases {
247+
assert.Equal(t, c.expected, SanitizeDirName(c.path))
248+
}
249+
}

services/repository/repository.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
issues_model "code.gitea.io/gitea/models/issues"
1313
"code.gitea.io/gitea/models/organization"
1414
repo_model "code.gitea.io/gitea/models/repo"
15-
system_model "code.gitea.io/gitea/models/system"
1615
"code.gitea.io/gitea/models/unit"
1716
user_model "code.gitea.io/gitea/models/user"
1817
"code.gitea.io/gitea/modules/graceful"
@@ -101,8 +100,6 @@ func Init(ctx context.Context) error {
101100
if err := repo_module.LoadRepoConfig(); err != nil {
102101
return err
103102
}
104-
system_model.RemoveAllWithNotice(ctx, "Clean up temporary repository uploads", setting.Repository.Upload.TempPath)
105-
system_model.RemoveAllWithNotice(ctx, "Clean up temporary repositories", repo_module.LocalCopyPath())
106103
if err := initPushQueue(); err != nil {
107104
return err
108105
}

tests/test_utils.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"code.gitea.io/gitea/modules/git"
1919
"code.gitea.io/gitea/modules/graceful"
2020
"code.gitea.io/gitea/modules/log"
21-
repo_module "code.gitea.io/gitea/modules/repository"
2221
"code.gitea.io/gitea/modules/setting"
2322
"code.gitea.io/gitea/modules/storage"
2423
"code.gitea.io/gitea/modules/test"
@@ -69,7 +68,6 @@ func InitTest(requireGitea bool) {
6968

7069
unittest.InitSettings()
7170
setting.Repository.DefaultBranch = "master" // many test code still assume that default branch is called "master"
72-
_ = util.RemoveAll(repo_module.LocalCopyPath())
7371

7472
if err := git.InitFull(context.Background()); err != nil {
7573
log.Fatal("git.InitOnceWithSync: %v", err)

0 commit comments

Comments
 (0)