multifilesystem.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2014 Jean-Marc Martins
  5. # Copyright © 2014-2015 Guillaume Ayoub
  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. """
  20. Multi files per calendar filesystem storage backend.
  21. """
  22. import os
  23. import shutil
  24. import time
  25. import sys
  26. from . import filesystem
  27. from .. import ical
  28. from .. import log
  29. from .. import pathutils
  30. class Collection(filesystem.Collection):
  31. """Collection stored in several files per calendar."""
  32. def _create_dirs(self):
  33. if not os.path.exists(self._filesystem_path):
  34. os.makedirs(self._filesystem_path)
  35. @property
  36. def headers(self):
  37. return (
  38. ical.Header("PRODID:-//Radicale//NONSGML Radicale Server//EN"),
  39. ical.Header("VERSION:%s" % self.version))
  40. def write(self):
  41. self._create_dirs()
  42. for component in self.components:
  43. text = ical.serialize(
  44. self.tag, self.headers, [component] + self.timezones)
  45. name = (
  46. component.name if sys.version_info[0] >= 3 else
  47. component.name.encode(filesystem.FILESYSTEM_ENCODING))
  48. if not pathutils.is_safe_filesystem_path_component(name):
  49. log.LOGGER.debug(
  50. "Can't tranlate name safely to filesystem, "
  51. "skipping component: %s", name)
  52. continue
  53. filesystem_path = os.path.join(self._filesystem_path, name)
  54. with filesystem.open(filesystem_path, "w") as fd:
  55. fd.write(text)
  56. def delete(self):
  57. shutil.rmtree(self._filesystem_path)
  58. os.remove(self._props_path)
  59. def remove(self, name):
  60. if not pathutils.is_safe_filesystem_path_component(name):
  61. log.LOGGER.debug(
  62. "Can't tranlate name safely to filesystem, "
  63. "skipping component: %s", name)
  64. return
  65. if name in self.items:
  66. del self.items[name]
  67. filesystem_path = os.path.join(self._filesystem_path, name)
  68. if os.path.exists(filesystem_path):
  69. os.remove(filesystem_path)
  70. @property
  71. def text(self):
  72. components = (
  73. ical.Timezone, ical.Event, ical.Todo, ical.Journal, ical.Card)
  74. items = {}
  75. try:
  76. filenames = os.listdir(self._filesystem_path)
  77. except (OSError, IOError) as e:
  78. log.LOGGER.info(
  79. 'Error while reading collection %r: %r' % (
  80. self._filesystem_path, e))
  81. return ""
  82. for filename in filenames:
  83. path = os.path.join(self._filesystem_path, filename)
  84. try:
  85. with filesystem.open(path) as fd:
  86. items.update(self._parse(fd.read(), components))
  87. except (OSError, IOError) as e:
  88. log.LOGGER.warning(
  89. 'Error while reading item %r: %r' % (path, e))
  90. return ical.serialize(
  91. self.tag, self.headers, sorted(items.values(), key=lambda x: x.name))
  92. @classmethod
  93. def is_node(cls, path):
  94. filesystem_path = pathutils.path_to_filesystem(path, filesystem.FOLDER)
  95. return (
  96. os.path.isdir(filesystem_path) and
  97. not os.path.exists(filesystem_path + ".props"))
  98. @classmethod
  99. def is_leaf(cls, path):
  100. filesystem_path = pathutils.path_to_filesystem(path, filesystem.FOLDER)
  101. return (
  102. os.path.isdir(filesystem_path) and os.path.exists(path + ".props"))
  103. @property
  104. def last_modified(self):
  105. last = max([
  106. os.path.getmtime(os.path.join(self._filesystem_path, filename))
  107. for filename in os.listdir(self._filesystem_path)] or [0])
  108. return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(last))