storage.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2014 Jean-Marc Martins
  3. # Copyright © 2012-2016 Guillaume Ayoub
  4. #
  5. # This library is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  17. """
  18. Storage backends.
  19. This module loads the storage backend, according to the storage
  20. configuration.
  21. Default storage uses one folder per collection and one file per collection
  22. entry.
  23. """
  24. import json
  25. import os
  26. import posixpath
  27. import shutil
  28. import sys
  29. import time
  30. from contextlib import contextmanager
  31. from . import config, ical, log, pathutils
  32. def _load():
  33. """Load the storage manager chosen in configuration."""
  34. storage_type = config.get("storage", "type")
  35. if storage_type == "multifilesystem":
  36. module = sys.modules[__name__]
  37. else:
  38. __import__(storage_type)
  39. module = sys.modules[storage_type]
  40. ical.Collection = module.Collection
  41. FOLDER = os.path.expanduser(config.get("storage", "filesystem_folder"))
  42. FILESYSTEM_ENCODING = sys.getfilesystemencoding()
  43. @contextmanager
  44. def _open(path, mode="r"):
  45. """Open a file at ``path`` with encoding set in the configuration."""
  46. abs_path = os.path.join(FOLDER, path.replace("/", os.sep))
  47. with open(abs_path, mode, encoding=config.get("encoding", "stock")) as fd:
  48. yield fd
  49. class Collection(ical.Collection):
  50. """Collection stored in several files per calendar."""
  51. @property
  52. def _filesystem_path(self):
  53. """Absolute path of the file at local ``path``."""
  54. return pathutils.path_to_filesystem(self.path, FOLDER)
  55. @property
  56. def _props_path(self):
  57. """Absolute path of the file storing the collection properties."""
  58. return self._filesystem_path + ".props"
  59. def _create_dirs(self):
  60. """Create folder storing the collection if absent."""
  61. if not os.path.exists(self._filesystem_path):
  62. os.makedirs(self._filesystem_path)
  63. def save(self, text):
  64. self._create_dirs()
  65. item_types = (
  66. ical.Timezone, ical.Event, ical.Todo, ical.Journal, ical.Card)
  67. for name, component in self._parse(text, item_types).items():
  68. if not pathutils.is_safe_filesystem_path_component(name):
  69. # TODO: Timezones with slashes can't be saved
  70. log.LOGGER.debug(
  71. "Can't tranlate name safely to filesystem, "
  72. "skipping component: %s", name)
  73. continue
  74. filename = os.path.join(self._filesystem_path, name)
  75. with _open(filename, "w") as fd:
  76. fd.write(component.text)
  77. @property
  78. def headers(self):
  79. return (
  80. ical.Header("PRODID:-//Radicale//NONSGML Radicale Server//EN"),
  81. ical.Header("VERSION:%s" % self.version))
  82. def delete(self):
  83. shutil.rmtree(self._filesystem_path)
  84. os.remove(self._props_path)
  85. def remove(self, name):
  86. if not pathutils.is_safe_filesystem_path_component(name):
  87. log.LOGGER.debug(
  88. "Can't tranlate name safely to filesystem, "
  89. "skipping component: %s", name)
  90. return
  91. if name in self.items:
  92. del self.items[name]
  93. filesystem_path = os.path.join(self._filesystem_path, name)
  94. if os.path.exists(filesystem_path):
  95. os.remove(filesystem_path)
  96. @property
  97. def text(self):
  98. components = (
  99. ical.Timezone, ical.Event, ical.Todo, ical.Journal, ical.Card)
  100. items = {}
  101. try:
  102. filenames = os.listdir(self._filesystem_path)
  103. except (OSError, IOError) as e:
  104. log.LOGGER.info(
  105. 'Error while reading collection %r: %r' % (
  106. self._filesystem_path, e))
  107. return ""
  108. for filename in filenames:
  109. path = os.path.join(self._filesystem_path, filename)
  110. try:
  111. with _open(path) as fd:
  112. items.update(self._parse(fd.read(), components))
  113. except (OSError, IOError) as e:
  114. log.LOGGER.warning(
  115. 'Error while reading item %r: %r' % (path, e))
  116. return ical.serialize(
  117. self.tag, self.headers, sorted(items.values(), key=lambda x: x.name))
  118. @classmethod
  119. def children(cls, path):
  120. filesystem_path = pathutils.path_to_filesystem(path, FOLDER)
  121. _, directories, files = next(os.walk(filesystem_path))
  122. for filename in directories + files:
  123. # make sure that the local filename can be translated
  124. # into an internal path
  125. if not pathutils.is_safe_path_component(filename):
  126. log.LOGGER.debug("Skipping unsupported filename: %s", filename)
  127. continue
  128. rel_filename = posixpath.join(path, filename)
  129. if cls.is_node(rel_filename) or cls.is_leaf(rel_filename):
  130. yield cls(rel_filename)
  131. @classmethod
  132. def is_node(cls, path):
  133. filesystem_path = pathutils.path_to_filesystem(path, FOLDER)
  134. return (
  135. os.path.isdir(filesystem_path) and
  136. not os.path.exists(filesystem_path + ".props"))
  137. @classmethod
  138. def is_leaf(cls, path):
  139. filesystem_path = pathutils.path_to_filesystem(path, FOLDER)
  140. return (
  141. os.path.isdir(filesystem_path) and os.path.exists(path + ".props"))
  142. @property
  143. def last_modified(self):
  144. last = max([
  145. os.path.getmtime(os.path.join(self._filesystem_path, filename))
  146. for filename in os.listdir(self._filesystem_path)] or [0])
  147. return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(last))
  148. @property
  149. @contextmanager
  150. def props(self):
  151. # On enter
  152. properties = {}
  153. if os.path.exists(self._props_path):
  154. with open(self._props_path) as prop_file:
  155. properties.update(json.load(prop_file))
  156. old_properties = properties.copy()
  157. yield properties
  158. # On exit
  159. self._create_dirs()
  160. if old_properties != properties:
  161. with open(self._props_path, "w") as prop_file:
  162. json.dump(properties, prop_file)