discover.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. import os
  19. import posixpath
  20. from typing import Callable, ContextManager, Iterator, Optional, cast
  21. from radicale import pathutils, types
  22. from radicale.log import logger
  23. from radicale.storage import multifilesystem
  24. from radicale.storage.multifilesystem.base import StorageBase
  25. @types.contextmanager
  26. def _null_child_context_manager(path: str,
  27. href: Optional[str]) -> Iterator[None]:
  28. yield
  29. class StoragePartDiscover(StorageBase):
  30. def discover(
  31. self, path: str, depth: str = "0", child_context_manager: Optional[
  32. Callable[[str, Optional[str]], ContextManager[None]]] = None
  33. ) -> Iterator[types.CollectionOrItem]:
  34. # assert isinstance(self, multifilesystem.Storage)
  35. if child_context_manager is None:
  36. child_context_manager = _null_child_context_manager
  37. # Path should already be sanitized
  38. sane_path = pathutils.strip_path(path)
  39. attributes = sane_path.split("/") if sane_path else []
  40. folder = self._get_collection_root_folder()
  41. # Create the root collection
  42. self._makedirs_synced(folder)
  43. try:
  44. filesystem_path = pathutils.path_to_filesystem(folder, sane_path)
  45. except ValueError as e:
  46. # Path is unsafe
  47. logger.debug("Unsafe path %r requested from storage: %s",
  48. sane_path, e, exc_info=True)
  49. return
  50. # Check if the path exists and if it leads to a collection or an item
  51. href: Optional[str]
  52. if not os.path.isdir(filesystem_path):
  53. if attributes and os.path.isfile(filesystem_path):
  54. href = attributes.pop()
  55. else:
  56. return
  57. else:
  58. href = None
  59. sane_path = "/".join(attributes)
  60. collection = self._collection_class(
  61. cast(multifilesystem.Storage, self),
  62. pathutils.unstrip_path(sane_path, True))
  63. if href:
  64. item = collection._get(href)
  65. if item is not None:
  66. yield item
  67. return
  68. yield collection
  69. if depth == "0":
  70. return
  71. for href in collection._list():
  72. with child_context_manager(sane_path, href):
  73. item = collection._get(href)
  74. if item is not None:
  75. yield item
  76. for entry in os.scandir(filesystem_path):
  77. if not entry.is_dir():
  78. continue
  79. href = entry.name
  80. if not pathutils.is_safe_filesystem_path_component(href):
  81. if not href.startswith(".Radicale"):
  82. logger.debug("Skipping collection %r in %r",
  83. href, sane_path)
  84. continue
  85. sane_child_path = posixpath.join(sane_path, href)
  86. child_path = pathutils.unstrip_path(sane_child_path, True)
  87. with child_context_manager(sane_child_path, None):
  88. yield self._collection_class(
  89. cast(multifilesystem.Storage, self), child_path)