verify.py 3.5 KB

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