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.

storage.go 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. package doctor
  5. import (
  6. repo_model "code.gitea.io/gitea/models/repo"
  7. "code.gitea.io/gitea/modules/log"
  8. "code.gitea.io/gitea/modules/storage"
  9. )
  10. func checkAttachmentStorageFiles(logger log.Logger, autofix bool) error {
  11. var total, garbageNum int
  12. var deletePaths []string
  13. if err := storage.Attachments.IterateObjects(func(p string, obj storage.Object) error {
  14. defer obj.Close()
  15. total++
  16. stat, err := obj.Stat()
  17. if err != nil {
  18. return err
  19. }
  20. exist, err := repo_model.ExistAttachmentsByUUID(stat.Name())
  21. if err != nil {
  22. return err
  23. }
  24. if !exist {
  25. garbageNum++
  26. if autofix {
  27. deletePaths = append(deletePaths, p)
  28. }
  29. }
  30. return nil
  31. }); err != nil {
  32. logger.Error("storage.Attachments.IterateObjects failed: %v", err)
  33. return err
  34. }
  35. if garbageNum > 0 {
  36. if autofix {
  37. var deletedNum int
  38. for _, p := range deletePaths {
  39. if err := storage.Attachments.Delete(p); err != nil {
  40. log.Error("Delete attachment %s failed: %v", p, err)
  41. } else {
  42. deletedNum++
  43. }
  44. }
  45. logger.Info("%d missed information attachment detected, %d deleted.", garbageNum, deletedNum)
  46. } else {
  47. logger.Warn("Checked %d attachment, %d missed information.", total, garbageNum)
  48. }
  49. }
  50. return nil
  51. }
  52. func checkStorageFiles(logger log.Logger, autofix bool) error {
  53. if err := storage.Init(); err != nil {
  54. logger.Error("storage.Init failed: %v", err)
  55. return err
  56. }
  57. return checkAttachmentStorageFiles(logger, autofix)
  58. }
  59. func init() {
  60. Register(&Check{
  61. Title: "Check if there is garbage storage files",
  62. Name: "storages",
  63. IsDefault: false,
  64. Run: checkStorageFiles,
  65. AbortIfFailed: false,
  66. SkipDatabaseInitialization: false,
  67. Priority: 1,
  68. })
  69. }