You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

archiver.go 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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(40) 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.SilentWrap{Message: fmt.Sprintf("invalid storage path: %s", relativePath), Err: util.ErrInvalidArgument}
  44. }
  45. repoID, err := strconv.ParseInt(parts[0], 10, 64)
  46. if err != nil {
  47. return nil, util.SilentWrap{Message: fmt.Sprintf("invalid storage path: %s", relativePath), Err: util.ErrInvalidArgument}
  48. }
  49. nameExts := strings.SplitN(parts[2], ".", 2)
  50. if len(nameExts) != 2 {
  51. return nil, util.SilentWrap{Message: fmt.Sprintf("invalid storage path: %s", relativePath), Err: util.ErrInvalidArgument}
  52. }
  53. return &RepoArchiver{
  54. RepoID: repoID,
  55. CommitID: parts[1] + nameExts[0],
  56. Type: git.ToArchiveType(nameExts[1]),
  57. }, nil
  58. }
  59. var delRepoArchiver = new(RepoArchiver)
  60. // DeleteRepoArchiver delete archiver
  61. func DeleteRepoArchiver(ctx context.Context, archiver *RepoArchiver) error {
  62. _, err := db.GetEngine(ctx).ID(archiver.ID).Delete(delRepoArchiver)
  63. return err
  64. }
  65. // GetRepoArchiver get an archiver
  66. func GetRepoArchiver(ctx context.Context, repoID int64, tp git.ArchiveType, commitID string) (*RepoArchiver, error) {
  67. var archiver RepoArchiver
  68. has, err := db.GetEngine(ctx).Where("repo_id=?", repoID).And("`type`=?", tp).And("commit_id=?", commitID).Get(&archiver)
  69. if err != nil {
  70. return nil, err
  71. }
  72. if has {
  73. return &archiver, nil
  74. }
  75. return nil, nil
  76. }
  77. // ExistsRepoArchiverWithStoragePath checks if there is a RepoArchiver for a given storage path
  78. func ExistsRepoArchiverWithStoragePath(ctx context.Context, storagePath string) (bool, error) {
  79. // We need to invert the path provided func (archiver *RepoArchiver) RelativePath() above
  80. archiver, err := repoArchiverForRelativePath(storagePath)
  81. if err != nil {
  82. return false, err
  83. }
  84. return db.GetEngine(ctx).Exist(archiver)
  85. }
  86. // AddRepoArchiver adds an archiver
  87. func AddRepoArchiver(ctx context.Context, archiver *RepoArchiver) error {
  88. _, err := db.GetEngine(ctx).Insert(archiver)
  89. return err
  90. }
  91. // UpdateRepoArchiverStatus updates archiver's status
  92. func UpdateRepoArchiverStatus(ctx context.Context, archiver *RepoArchiver) error {
  93. _, err := db.GetEngine(ctx).ID(archiver.ID).Cols("status").Update(archiver)
  94. return err
  95. }
  96. // DeleteAllRepoArchives deletes all repo archives records
  97. func DeleteAllRepoArchives(ctx context.Context) error {
  98. _, err := db.GetEngine(ctx).Where("1=1").Delete(new(RepoArchiver))
  99. return err
  100. }
  101. // FindRepoArchiversOption represents an archiver options
  102. type FindRepoArchiversOption struct {
  103. db.ListOptions
  104. OlderThan time.Duration
  105. }
  106. func (opts FindRepoArchiversOption) toConds() builder.Cond {
  107. cond := builder.NewCond()
  108. if opts.OlderThan > 0 {
  109. cond = cond.And(builder.Lt{"created_unix": time.Now().Add(-opts.OlderThan).Unix()})
  110. }
  111. return cond
  112. }
  113. // FindRepoArchives find repo archivers
  114. func FindRepoArchives(ctx context.Context, opts FindRepoArchiversOption) ([]*RepoArchiver, error) {
  115. archivers := make([]*RepoArchiver, 0, opts.PageSize)
  116. start, limit := opts.GetSkipTake()
  117. err := db.GetEngine(ctx).Where(opts.toConds()).
  118. Asc("created_unix").
  119. Limit(limit, start).
  120. Find(&archivers)
  121. return archivers, err
  122. }
  123. // SetArchiveRepoState sets if a repo is archived
  124. func SetArchiveRepoState(ctx context.Context, repo *Repository, isArchived bool) (err error) {
  125. repo.IsArchived = isArchived
  126. if isArchived {
  127. repo.ArchivedUnix = timeutil.TimeStampNow()
  128. } else {
  129. repo.ArchivedUnix = timeutil.TimeStamp(0)
  130. }
  131. _, err = db.GetEngine(ctx).ID(repo.ID).Cols("is_archived", "archived_unix").NoAutoTime().Update(repo)
  132. return err
  133. }