verify.py 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. logger.info("Disable fsync during storage verification")
  28. self._filesystem_fsync = False
  29. @types.contextmanager
  30. def exception_cm(sane_path: str, href: Optional[str]
  31. ) -> Iterator[None]:
  32. nonlocal item_errors, collection_errors
  33. try:
  34. yield
  35. except Exception as e:
  36. if href is not None:
  37. item_errors += 1
  38. name = "item %r in %r" % (href, sane_path)
  39. else:
  40. collection_errors += 1
  41. name = "collection %r" % sane_path
  42. logger.error("Invalid %s: %s", name, e, exc_info=True)
  43. remaining_sane_paths = [""]
  44. while remaining_sane_paths:
  45. sane_path = remaining_sane_paths.pop(0)
  46. path = pathutils.unstrip_path(sane_path, True)
  47. logger.info("Verifying path %r", sane_path)
  48. count = 0
  49. is_collection = True
  50. with exception_cm(sane_path, None):
  51. saved_item_errors = item_errors
  52. collection: Optional[storage.BaseCollection] = None
  53. uids: Set[str] = set()
  54. has_child_collections = False
  55. for item in self.discover(path, "1", exception_cm):
  56. if not collection:
  57. assert isinstance(item, storage.BaseCollection)
  58. collection = item
  59. collection.get_meta()
  60. if not collection.tag:
  61. is_collection = False
  62. logger.info("Skip !collection %r", sane_path)
  63. continue
  64. if isinstance(item, storage.BaseCollection):
  65. has_child_collections = True
  66. remaining_sane_paths.append(item.path)
  67. elif item.uid in uids:
  68. logger.error("Invalid item %r in %r: UID conflict %r",
  69. item.href, sane_path, item.uid)
  70. else:
  71. uids.add(item.uid)
  72. count += 1
  73. logger.debug("Verified in %r item %r",
  74. sane_path, item.href)
  75. assert collection
  76. if item_errors == saved_item_errors:
  77. if is_collection:
  78. collection.sync()
  79. if has_child_collections and collection.tag:
  80. logger.error("Invalid collection %r: %r must not have "
  81. "child collections", sane_path,
  82. collection.tag)
  83. if is_collection:
  84. logger.info("Verified collect %r (items: %d)", sane_path, count)
  85. return item_errors == 0 and collection_errors == 0