discover.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # This file is part of Radicale Server - Calendar 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 contextlib
  19. import os
  20. import posixpath
  21. from radicale import pathutils
  22. from radicale.log import logger
  23. class StorageDiscoverMixin:
  24. def discover(self, path, depth="0", child_context_manager=(
  25. lambda path, href=None: contextlib.ExitStack())):
  26. # Path should already be sanitized
  27. sane_path = pathutils.strip_path(path)
  28. attributes = sane_path.split("/") if sane_path else []
  29. folder = self._get_collection_root_folder()
  30. # Create the root collection
  31. self._makedirs_synced(folder)
  32. try:
  33. filesystem_path = pathutils.path_to_filesystem(folder, sane_path)
  34. except ValueError as e:
  35. # Path is unsafe
  36. logger.debug("Unsafe path %r requested from storage: %s",
  37. sane_path, e, exc_info=True)
  38. return
  39. # Check if the path exists and if it leads to a collection or an item
  40. if not os.path.isdir(filesystem_path):
  41. if attributes and os.path.isfile(filesystem_path):
  42. href = attributes.pop()
  43. else:
  44. return
  45. else:
  46. href = None
  47. sane_path = "/".join(attributes)
  48. collection = self._collection_class(
  49. self, pathutils.unstrip_path(sane_path, True))
  50. if href:
  51. yield collection._get(href)
  52. return
  53. yield collection
  54. if depth == "0":
  55. return
  56. for href in collection._list():
  57. with child_context_manager(sane_path, href):
  58. yield collection._get(href)
  59. for entry in os.scandir(filesystem_path):
  60. if not entry.is_dir():
  61. continue
  62. href = entry.name
  63. if not pathutils.is_safe_filesystem_path_component(href):
  64. if not href.startswith(".Radicale"):
  65. logger.debug("Skipping collection %r in %r",
  66. href, sane_path)
  67. continue
  68. sane_child_path = posixpath.join(sane_path, href)
  69. child_path = pathutils.unstrip_path(sane_child_path, True)
  70. with child_context_manager(sane_child_path):
  71. yield self._collection_class(self, child_path)