verify.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2014 Jean-Marc Martins
  3. # Copyright © 2012-2017 Guillaume Ayoub
  4. # Copyright © 2017-2021 Unrud <unrud@outlook.com>
  5. # Copyright © 2024-2024 Peter Bieringer <pb@bieringer.de>
  6. #
  7. # This library is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  19. from typing import Iterator, Optional, Set
  20. from radicale import pathutils, storage, types
  21. from radicale.log import logger
  22. from radicale.storage.multifilesystem.base import StorageBase
  23. from radicale.storage.multifilesystem.discover import StoragePartDiscover
  24. class StoragePartVerify(StoragePartDiscover, StorageBase):
  25. def verify(self) -> bool:
  26. item_errors = collection_errors = 0
  27. @types.contextmanager
  28. def exception_cm(sane_path: str, href: Optional[str]
  29. ) -> Iterator[None]:
  30. nonlocal item_errors, collection_errors
  31. try:
  32. yield
  33. except Exception as e:
  34. if href is not None:
  35. item_errors += 1
  36. name = "item %r in %r" % (href, sane_path)
  37. else:
  38. collection_errors += 1
  39. name = "collection %r" % sane_path
  40. logger.error("Invalid %s: %s", name, e, exc_info=True)
  41. remaining_sane_paths = [""]
  42. while remaining_sane_paths:
  43. sane_path = remaining_sane_paths.pop(0)
  44. path = pathutils.unstrip_path(sane_path, True)
  45. logger.info("Verifying path %r", sane_path)
  46. count = 0
  47. is_collection = True
  48. with exception_cm(sane_path, None):
  49. saved_item_errors = item_errors
  50. collection: Optional[storage.BaseCollection] = None
  51. uids: Set[str] = set()
  52. has_child_collections = False
  53. for item in self.discover(path, "1", exception_cm):
  54. if not collection:
  55. assert isinstance(item, storage.BaseCollection)
  56. collection = item
  57. collection.get_meta()
  58. if not collection.tag:
  59. is_collection = False
  60. logger.info("Skip !collection %r", sane_path)
  61. continue
  62. if isinstance(item, storage.BaseCollection):
  63. has_child_collections = True
  64. remaining_sane_paths.append(item.path)
  65. elif item.uid in uids:
  66. logger.error("Invalid item %r in %r: UID conflict %r",
  67. item.href, sane_path, item.uid)
  68. else:
  69. uids.add(item.uid)
  70. count += 1
  71. logger.debug("Verified in %r item %r",
  72. sane_path, item.href)
  73. assert collection
  74. if item_errors == saved_item_errors:
  75. if is_collection:
  76. collection.sync()
  77. if has_child_collections and collection.tag:
  78. logger.error("Invalid collection %r: %r must not have "
  79. "child collections", sane_path,
  80. collection.tag)
  81. if is_collection:
  82. logger.info("Verified collect %r (items: %d)", sane_path, count)
  83. return item_errors == 0 and collection_errors == 0