2013-08-29 16:55:36 +02:00
|
|
|
# This file is part of Radicale Server - Calendar Server
|
2015-02-07 17:26:20 +01:00
|
|
|
# Copyright © 2014 Jean-Marc Martins
|
2016-04-07 19:02:52 +02:00
|
|
|
# Copyright © 2012-2016 Guillaume Ayoub
|
2013-08-29 16:55:36 +02:00
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Radicale. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""
|
2016-04-07 19:02:52 +02:00
|
|
|
Storage backends.
|
|
|
|
|
|
|
|
This module loads the storage backend, according to the storage
|
|
|
|
configuration.
|
|
|
|
|
|
|
|
Default storage uses one folder per collection and one file per collection
|
|
|
|
entry.
|
2013-08-29 16:55:36 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2014-09-15 17:42:33 +02:00
|
|
|
import json
|
2016-04-07 19:02:52 +02:00
|
|
|
import os
|
|
|
|
import posixpath
|
2013-08-29 16:55:36 +02:00
|
|
|
import shutil
|
2013-09-19 14:40:03 +02:00
|
|
|
import sys
|
2016-04-07 19:02:52 +02:00
|
|
|
import time
|
2014-09-15 17:42:33 +02:00
|
|
|
from contextlib import contextmanager
|
2016-03-31 19:57:40 +02:00
|
|
|
|
2016-04-07 19:02:52 +02:00
|
|
|
from . import config, ical, log, pathutils
|
|
|
|
|
|
|
|
|
|
|
|
def _load():
|
|
|
|
"""Load the storage manager chosen in configuration."""
|
|
|
|
storage_type = config.get("storage", "type")
|
|
|
|
if storage_type == "multifilesystem":
|
|
|
|
module = sys.modules[__name__]
|
|
|
|
else:
|
|
|
|
__import__(storage_type)
|
|
|
|
module = sys.modules[storage_type]
|
|
|
|
ical.Collection = module.Collection
|
|
|
|
|
2013-08-29 16:55:36 +02:00
|
|
|
|
2016-04-07 19:02:52 +02:00
|
|
|
FOLDER = os.path.expanduser(config.get("storage", "filesystem_folder"))
|
|
|
|
FILESYSTEM_ENCODING = sys.getfilesystemencoding()
|
2013-08-29 16:55:36 +02:00
|
|
|
|
2016-04-07 19:02:52 +02:00
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def _open(path, mode="r"):
|
|
|
|
"""Open a file at ``path`` with encoding set in the configuration."""
|
|
|
|
abs_path = os.path.join(FOLDER, path.replace("/", os.sep))
|
|
|
|
with open(abs_path, mode, encoding=config.get("encoding", "stock")) as fd:
|
|
|
|
yield fd
|
|
|
|
|
|
|
|
|
|
|
|
class Collection(ical.Collection):
|
2013-08-29 16:55:36 +02:00
|
|
|
"""Collection stored in several files per calendar."""
|
2016-04-07 19:02:52 +02:00
|
|
|
@property
|
|
|
|
def _filesystem_path(self):
|
|
|
|
"""Absolute path of the file at local ``path``."""
|
|
|
|
return pathutils.path_to_filesystem(self.path, FOLDER)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _props_path(self):
|
|
|
|
"""Absolute path of the file storing the collection properties."""
|
|
|
|
return self._filesystem_path + ".props"
|
|
|
|
|
2013-08-29 16:55:36 +02:00
|
|
|
def _create_dirs(self):
|
2016-04-07 19:02:52 +02:00
|
|
|
"""Create folder storing the collection if absent."""
|
2015-12-24 12:37:11 +01:00
|
|
|
if not os.path.exists(self._filesystem_path):
|
|
|
|
os.makedirs(self._filesystem_path)
|
2013-08-29 16:55:36 +02:00
|
|
|
|
2016-04-07 19:02:52 +02:00
|
|
|
def save(self, text):
|
2013-08-29 16:55:36 +02:00
|
|
|
self._create_dirs()
|
2016-04-07 19:02:52 +02:00
|
|
|
item_types = (
|
|
|
|
ical.Timezone, ical.Event, ical.Todo, ical.Journal, ical.Card)
|
|
|
|
for name, component in self._parse(text, item_types).items():
|
2015-12-24 13:32:30 +01:00
|
|
|
if not pathutils.is_safe_filesystem_path_component(name):
|
2016-04-07 19:02:52 +02:00
|
|
|
# TODO: Timezones with slashes can't be saved
|
2015-12-24 13:32:30 +01:00
|
|
|
log.LOGGER.debug(
|
|
|
|
"Can't tranlate name safely to filesystem, "
|
|
|
|
"skipping component: %s", name)
|
|
|
|
continue
|
2016-04-07 19:02:52 +02:00
|
|
|
filename = os.path.join(self._filesystem_path, name)
|
|
|
|
with _open(filename, "w") as fd:
|
|
|
|
fd.write(component.text)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def headers(self):
|
|
|
|
return (
|
|
|
|
ical.Header("PRODID:-//Radicale//NONSGML Radicale Server//EN"),
|
|
|
|
ical.Header("VERSION:%s" % self.version))
|
2013-08-29 16:55:36 +02:00
|
|
|
|
|
|
|
def delete(self):
|
2015-12-24 12:37:11 +01:00
|
|
|
shutil.rmtree(self._filesystem_path)
|
2014-09-18 14:21:10 +02:00
|
|
|
os.remove(self._props_path)
|
2013-08-29 16:55:36 +02:00
|
|
|
|
|
|
|
def remove(self, name):
|
2015-12-24 13:32:30 +01:00
|
|
|
if not pathutils.is_safe_filesystem_path_component(name):
|
|
|
|
log.LOGGER.debug(
|
|
|
|
"Can't tranlate name safely to filesystem, "
|
|
|
|
"skipping component: %s", name)
|
|
|
|
return
|
2016-01-15 01:13:18 +01:00
|
|
|
if name in self.items:
|
|
|
|
del self.items[name]
|
2015-12-24 12:37:11 +01:00
|
|
|
filesystem_path = os.path.join(self._filesystem_path, name)
|
|
|
|
if os.path.exists(filesystem_path):
|
|
|
|
os.remove(filesystem_path)
|
2013-08-29 16:55:36 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def text(self):
|
|
|
|
components = (
|
|
|
|
ical.Timezone, ical.Event, ical.Todo, ical.Journal, ical.Card)
|
2016-01-15 01:09:43 +01:00
|
|
|
items = {}
|
2013-08-29 16:55:36 +02:00
|
|
|
try:
|
2015-12-24 12:37:11 +01:00
|
|
|
filenames = os.listdir(self._filesystem_path)
|
2014-11-30 13:49:11 +01:00
|
|
|
except (OSError, IOError) as e:
|
2015-12-31 12:49:41 +01:00
|
|
|
log.LOGGER.info(
|
|
|
|
'Error while reading collection %r: %r' % (
|
|
|
|
self._filesystem_path, e))
|
2013-08-29 16:55:36 +02:00
|
|
|
return ""
|
2014-11-30 13:49:11 +01:00
|
|
|
|
|
|
|
for filename in filenames:
|
2015-12-24 12:37:11 +01:00
|
|
|
path = os.path.join(self._filesystem_path, filename)
|
2014-11-30 13:49:11 +01:00
|
|
|
try:
|
2016-04-07 19:02:52 +02:00
|
|
|
with _open(path) as fd:
|
2014-11-30 13:49:11 +01:00
|
|
|
items.update(self._parse(fd.read(), components))
|
|
|
|
except (OSError, IOError) as e:
|
2015-12-31 12:49:41 +01:00
|
|
|
log.LOGGER.warning(
|
|
|
|
'Error while reading item %r: %r' % (path, e))
|
2014-11-30 13:49:11 +01:00
|
|
|
|
|
|
|
return ical.serialize(
|
2016-01-15 01:09:43 +01:00
|
|
|
self.tag, self.headers, sorted(items.values(), key=lambda x: x.name))
|
2013-08-29 16:55:36 +02:00
|
|
|
|
2016-04-07 19:02:52 +02:00
|
|
|
@classmethod
|
|
|
|
def children(cls, path):
|
|
|
|
filesystem_path = pathutils.path_to_filesystem(path, FOLDER)
|
|
|
|
_, directories, files = next(os.walk(filesystem_path))
|
|
|
|
for filename in directories + files:
|
|
|
|
# make sure that the local filename can be translated
|
|
|
|
# into an internal path
|
|
|
|
if not pathutils.is_safe_path_component(filename):
|
|
|
|
log.LOGGER.debug("Skipping unsupported filename: %s", filename)
|
|
|
|
continue
|
|
|
|
rel_filename = posixpath.join(path, filename)
|
|
|
|
if cls.is_node(rel_filename) or cls.is_leaf(rel_filename):
|
|
|
|
yield cls(rel_filename)
|
|
|
|
|
2013-08-29 16:55:36 +02:00
|
|
|
@classmethod
|
|
|
|
def is_node(cls, path):
|
2016-04-07 19:02:52 +02:00
|
|
|
filesystem_path = pathutils.path_to_filesystem(path, FOLDER)
|
2015-12-31 12:49:41 +01:00
|
|
|
return (
|
|
|
|
os.path.isdir(filesystem_path) and
|
|
|
|
not os.path.exists(filesystem_path + ".props"))
|
2013-08-29 16:55:36 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def is_leaf(cls, path):
|
2016-04-07 19:02:52 +02:00
|
|
|
filesystem_path = pathutils.path_to_filesystem(path, FOLDER)
|
2015-12-31 12:49:41 +01:00
|
|
|
return (
|
|
|
|
os.path.isdir(filesystem_path) and os.path.exists(path + ".props"))
|
2013-08-29 16:55:36 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def last_modified(self):
|
|
|
|
last = max([
|
2015-12-24 12:37:11 +01:00
|
|
|
os.path.getmtime(os.path.join(self._filesystem_path, filename))
|
|
|
|
for filename in os.listdir(self._filesystem_path)] or [0])
|
2013-08-29 16:55:36 +02:00
|
|
|
return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(last))
|
2014-09-15 17:42:33 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
@contextmanager
|
|
|
|
def props(self):
|
|
|
|
# On enter
|
|
|
|
properties = {}
|
|
|
|
if os.path.exists(self._props_path):
|
|
|
|
with open(self._props_path) as prop_file:
|
|
|
|
properties.update(json.load(prop_file))
|
|
|
|
old_properties = properties.copy()
|
|
|
|
yield properties
|
|
|
|
# On exit
|
2016-04-07 19:02:52 +02:00
|
|
|
self._create_dirs()
|
|
|
|
if old_properties != properties:
|
|
|
|
with open(self._props_path, "w") as prop_file:
|
|
|
|
json.dump(properties, prop_file)
|