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.

checkOldArchives.go 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package doctor
  4. import (
  5. "context"
  6. "os"
  7. "path/filepath"
  8. repo_model "code.gitea.io/gitea/models/repo"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/util"
  11. )
  12. func checkOldArchives(ctx context.Context, logger log.Logger, autofix bool) error {
  13. numRepos := 0
  14. numReposUpdated := 0
  15. err := iterateRepositories(ctx, func(repo *repo_model.Repository) error {
  16. if repo.IsEmpty {
  17. return nil
  18. }
  19. p := filepath.Join(repo.RepoPath(), "archives")
  20. isDir, err := util.IsDir(p)
  21. if err != nil {
  22. log.Warn("check if %s is directory failed: %v", p, err)
  23. }
  24. if isDir {
  25. numRepos++
  26. if autofix {
  27. if err := os.RemoveAll(p); err == nil {
  28. numReposUpdated++
  29. } else {
  30. log.Warn("remove %s failed: %v", p, err)
  31. }
  32. }
  33. }
  34. return nil
  35. })
  36. if autofix {
  37. logger.Info("%d / %d old archives in repository deleted", numReposUpdated, numRepos)
  38. } else {
  39. logger.Info("%d old archives in repository need to be deleted", numRepos)
  40. }
  41. return err
  42. }
  43. func init() {
  44. Register(&Check{
  45. Title: "Check old archives",
  46. Name: "check-old-archives",
  47. IsDefault: false,
  48. Run: checkOldArchives,
  49. Priority: 7,
  50. })
  51. }