gitea源码

archiver.go 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "context"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "code.gitea.io/gitea/models/db"
  11. "code.gitea.io/gitea/modules/git"
  12. "code.gitea.io/gitea/modules/timeutil"
  13. "code.gitea.io/gitea/modules/util"
  14. "xorm.io/builder"
  15. )
  16. // ArchiverStatus represents repo archive status
  17. type ArchiverStatus int
  18. // enumerate all repo archive statuses
  19. const (
  20. ArchiverGenerating = iota // the archiver is generating
  21. ArchiverReady // it's ready
  22. )
  23. // RepoArchiver represents all archivers
  24. type RepoArchiver struct { //revive:disable-line:exported
  25. ID int64 `xorm:"pk autoincr"`
  26. RepoID int64 `xorm:"index unique(s)"`
  27. Type git.ArchiveType `xorm:"unique(s)"`
  28. Status ArchiverStatus
  29. CommitID string `xorm:"VARCHAR(64) unique(s)"`
  30. CreatedUnix timeutil.TimeStamp `xorm:"INDEX NOT NULL created"`
  31. }
  32. func init() {
  33. db.RegisterModel(new(RepoArchiver))
  34. }
  35. // RelativePath returns the archive path relative to the archive storage root.
  36. func (archiver *RepoArchiver) RelativePath() string {
  37. return fmt.Sprintf("%d/%s/%s.%s", archiver.RepoID, archiver.CommitID[:2], archiver.CommitID, archiver.Type.String())
  38. }
  39. // repoArchiverForRelativePath takes a relativePath created from (archiver *RepoArchiver) RelativePath() and creates a shell repoArchiver struct representing it
  40. func repoArchiverForRelativePath(relativePath string) (*RepoArchiver, error) {
  41. parts := strings.SplitN(relativePath, "/", 3)
  42. if len(parts) != 3 {
  43. return nil, util.NewInvalidArgumentErrorf("invalid storage path: must have 3 parts")
  44. }
  45. repoID, err := strconv.ParseInt(parts[0], 10, 64)
  46. if err != nil {
  47. return nil, util.NewInvalidArgumentErrorf("invalid storage path: invalid repo id")
  48. }
  49. commitID, archiveType := git.SplitArchiveNameType(parts[2])
  50. if archiveType == git.ArchiveUnknown {
  51. return nil, util.NewInvalidArgumentErrorf("invalid storage path: invalid archive type")
  52. }
  53. return &RepoArchiver{RepoID: repoID, CommitID: commitID, Type: archiveType}, nil
  54. }
  55. // GetRepoArchiver get an archiver
  56. func GetRepoArchiver(ctx context.Context, repoID int64, tp git.ArchiveType, commitID string) (*RepoArchiver, error) {
  57. var archiver RepoArchiver
  58. has, err := db.GetEngine(ctx).Where("repo_id=?", repoID).And("`type`=?", tp).And("commit_id=?", commitID).Get(&archiver)
  59. if err != nil {
  60. return nil, err
  61. }
  62. if has {
  63. return &archiver, nil
  64. }
  65. return nil, nil
  66. }
  67. // ExistsRepoArchiverWithStoragePath checks if there is a RepoArchiver for a given storage path
  68. func ExistsRepoArchiverWithStoragePath(ctx context.Context, storagePath string) (bool, error) {
  69. // We need to invert the path provided func (archiver *RepoArchiver) RelativePath() above
  70. archiver, err := repoArchiverForRelativePath(storagePath)
  71. if err != nil {
  72. return false, err
  73. }
  74. return db.GetEngine(ctx).Exist(archiver)
  75. }
  76. // UpdateRepoArchiverStatus updates archiver's status
  77. func UpdateRepoArchiverStatus(ctx context.Context, archiver *RepoArchiver) error {
  78. _, err := db.GetEngine(ctx).ID(archiver.ID).Cols("status").Update(archiver)
  79. return err
  80. }
  81. // DeleteAllRepoArchives deletes all repo archives records
  82. func DeleteAllRepoArchives(ctx context.Context) error {
  83. // 1=1 to enforce delete all data, otherwise it will delete nothing
  84. _, err := db.GetEngine(ctx).Where("1=1").Delete(new(RepoArchiver))
  85. return err
  86. }
  87. // FindRepoArchiversOption represents an archiver options
  88. type FindRepoArchiversOption struct {
  89. db.ListOptions
  90. OlderThan time.Duration
  91. }
  92. func (opts FindRepoArchiversOption) ToConds() builder.Cond {
  93. cond := builder.NewCond()
  94. if opts.OlderThan > 0 {
  95. cond = cond.And(builder.Lt{"created_unix": time.Now().Add(-opts.OlderThan).Unix()})
  96. }
  97. return cond
  98. }
  99. func (opts FindRepoArchiversOption) ToOrders() string {
  100. return "created_unix ASC"
  101. }
  102. // SetArchiveRepoState sets if a repo is archived
  103. func SetArchiveRepoState(ctx context.Context, repo *Repository, isArchived bool) (err error) {
  104. repo.IsArchived = isArchived
  105. if isArchived {
  106. repo.ArchivedUnix = timeutil.TimeStampNow()
  107. } else {
  108. repo.ArchivedUnix = timeutil.TimeStamp(0)
  109. }
  110. _, err = db.GetEngine(ctx).ID(repo.ID).Cols("is_archived", "archived_unix").NoAutoTime().Update(repo)
  111. return err
  112. }