Skip to content

Commit 58a3952

Browse files
authored
Fix package upload temp path (#34196)
Fix #34195 The temp dir should be created when it is used.
1 parent f6474cf commit 58a3952

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

modules/setting/packages.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ import (
1313
// Package registry settings
1414
var (
1515
Packages = struct {
16-
Storage *Storage
17-
Enabled bool
18-
ChunkedUploadPath string
16+
Storage *Storage
17+
Enabled bool
1918

2019
LimitTotalOwnerCount int64
2120
LimitTotalOwnerSize int64
@@ -65,13 +64,6 @@ func loadPackagesFrom(rootCfg ConfigProvider) (err error) {
6564
return err
6665
}
6766

68-
if HasInstallLock(rootCfg) {
69-
Packages.ChunkedUploadPath, err = AppDataTempDir("package-upload").MkdirAllSub("")
70-
if err != nil {
71-
return fmt.Errorf("unable to create chunked upload directory: %w", err)
72-
}
73-
}
74-
7567
Packages.LimitTotalOwnerSize = mustBytes(sec, "LIMIT_TOTAL_OWNER_SIZE")
7668
Packages.LimitSizeAlpine = mustBytes(sec, "LIMIT_SIZE_ALPINE")
7769
Packages.LimitSizeArch = mustBytes(sec, "LIMIT_SIZE_ARCH")

services/packages/container/blob_uploader.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
packages_model "code.gitea.io/gitea/models/packages"
1313
packages_module "code.gitea.io/gitea/modules/packages"
1414
"code.gitea.io/gitea/modules/setting"
15-
"code.gitea.io/gitea/modules/util"
15+
"code.gitea.io/gitea/modules/tempdir"
1616
)
1717

1818
var (
@@ -30,8 +30,12 @@ type BlobUploader struct {
3030
reading bool
3131
}
3232

33-
func buildFilePath(id string) string {
34-
return util.FilePathJoinAbs(setting.Packages.ChunkedUploadPath, id)
33+
func uploadPathTempDir() *tempdir.TempDir {
34+
return setting.AppDataTempDir("package-upload")
35+
}
36+
37+
func buildFilePath(uploadPath *tempdir.TempDir, id string) string {
38+
return uploadPath.JoinPath(id)
3539
}
3640

3741
// NewBlobUploader creates a new blob uploader for the given id
@@ -48,7 +52,12 @@ func NewBlobUploader(ctx context.Context, id string) (*BlobUploader, error) {
4852
}
4953
}
5054

51-
f, err := os.OpenFile(buildFilePath(model.ID), os.O_RDWR|os.O_CREATE, 0o666)
55+
uploadPath := uploadPathTempDir()
56+
_, err = uploadPath.MkdirAllSub("")
57+
if err != nil {
58+
return nil, err
59+
}
60+
f, err := os.OpenFile(buildFilePath(uploadPath, model.ID), os.O_RDWR|os.O_CREATE, 0o666)
5261
if err != nil {
5362
return nil, err
5463
}
@@ -118,13 +127,13 @@ func (u *BlobUploader) Read(p []byte) (int, error) {
118127
return u.file.Read(p)
119128
}
120129

121-
// Remove deletes the data and the model of a blob upload
130+
// RemoveBlobUploadByID Remove deletes the data and the model of a blob upload
122131
func RemoveBlobUploadByID(ctx context.Context, id string) error {
123132
if err := packages_model.DeleteBlobUploadByID(ctx, id); err != nil {
124133
return err
125134
}
126135

127-
err := os.Remove(buildFilePath(id))
136+
err := os.Remove(buildFilePath(uploadPathTempDir(), id))
128137
if err != nil && !os.IsNotExist(err) {
129138
return err
130139
}

0 commit comments

Comments
 (0)