Skip to content

Commit e4d3fd2

Browse files
lunnyAbdulrhmnGhanem
authored andcommitted
Fix archive error when rename repo or user (go-gitea#16399)
Use repo id instead of full name to generate archive path
1 parent f7622f0 commit e4d3fd2

File tree

5 files changed

+35
-23
lines changed

5 files changed

+35
-23
lines changed

models/repo.go

-16
Original file line numberDiff line numberDiff line change
@@ -1764,22 +1764,6 @@ func GetPrivateRepositoryCount(u *User) (int64, error) {
17641764
return getPrivateRepositoryCount(x, u)
17651765
}
17661766

1767-
// DeleteRepositoryArchives deletes all repositories' archives.
1768-
func DeleteRepositoryArchives(ctx context.Context) error {
1769-
return x.
1770-
Where("id > 0").
1771-
Iterate(new(Repository),
1772-
func(idx int, bean interface{}) error {
1773-
repo := bean.(*Repository)
1774-
select {
1775-
case <-ctx.Done():
1776-
return ErrCancelledf("before deleting repository archives for %s", repo.FullName())
1777-
default:
1778-
}
1779-
return util.RemoveAll(filepath.Join(repo.RepoPath(), "archives"))
1780-
})
1781-
}
1782-
17831767
// DeleteOldRepositoryArchives deletes old repository archives.
17841768
func DeleteOldRepositoryArchives(ctx context.Context, olderThan time.Duration) error {
17851769
log.Trace("Doing: ArchiveCleanup")

models/repo_archiver.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,7 @@ func (archiver *RepoArchiver) LoadRepo() (*Repository, error) {
5252

5353
// RelativePath returns relative path
5454
func (archiver *RepoArchiver) RelativePath() (string, error) {
55-
repo, err := archiver.LoadRepo()
56-
if err != nil {
57-
return "", err
58-
}
59-
60-
return fmt.Sprintf("%s/%s/%s.%s", repo.FullName(), archiver.CommitID[:2], archiver.CommitID, archiver.Type.String()), nil
55+
return fmt.Sprintf("%d/%s/%s.%s", archiver.RepoID, archiver.CommitID[:2], archiver.CommitID, archiver.Type.String()), nil
6156
}
6257

6358
// GetRepoArchiver get an archiver
@@ -84,3 +79,9 @@ func UpdateRepoArchiverStatus(ctx DBContext, archiver *RepoArchiver) error {
8479
_, err := ctx.e.ID(archiver.ID).Cols("status").Update(archiver)
8580
return err
8681
}
82+
83+
// DeleteAllRepoArchives deletes all repo archives records
84+
func DeleteAllRepoArchives() error {
85+
_, err := x.Where("1=1").Delete(new(RepoArchiver))
86+
return err
87+
}

modules/cron/tasks_extended.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func registerDeleteRepositoryArchives() {
3333
RunAtStart: false,
3434
Schedule: "@annually",
3535
}, func(ctx context.Context, _ *models.User, _ Config) error {
36-
return models.DeleteRepositoryArchives(ctx)
36+
return repo_module.DeleteRepositoryArchives(ctx)
3737
})
3838
}
3939

modules/repository/archive.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package repository
6+
7+
import (
8+
"context"
9+
10+
"code.gitea.io/gitea/models"
11+
"code.gitea.io/gitea/modules/storage"
12+
)
13+
14+
// DeleteRepositoryArchives deletes all repositories' archives.
15+
func DeleteRepositoryArchives(ctx context.Context) error {
16+
if err := models.DeleteAllRepoArchives(); err != nil {
17+
return err
18+
}
19+
return storage.Clean(storage.RepoArchives)
20+
}

modules/storage/storage.go

+7
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ func Copy(dstStorage ObjectStorage, dstPath string, srcStorage ObjectStorage, sr
8888
return dstStorage.Save(dstPath, f, size)
8989
}
9090

91+
// Clean delete all the objects in this storage
92+
func Clean(storage ObjectStorage) error {
93+
return storage.IterateObjects(func(path string, obj Object) error {
94+
return storage.Delete(path)
95+
})
96+
}
97+
9198
// SaveFrom saves data to the ObjectStorage with path p from the callback
9299
func SaveFrom(objStorage ObjectStorage, p string, callback func(w io.Writer) error) error {
93100
pr, pw := io.Pipe()

0 commit comments

Comments
 (0)