From e34d1c46cda93a254bbc8d78c2ca548ca619c516 Mon Sep 17 00:00:00 2001 From: Unrud Date: Wed, 3 Aug 2016 14:51:34 +0200 Subject: [PATCH] Move collections into collection-root folder This is required for atomic creation and deletion of the "/" collection. --- radicale/storage.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/radicale/storage.py b/radicale/storage.py index feee9e5..64bf085 100644 --- a/radicale/storage.py +++ b/radicale/storage.py @@ -327,8 +327,7 @@ class Collection(BaseCollection): """Collection stored in several files per calendar.""" def __init__(self, path, principal=False): - folder = os.path.expanduser( - self.configuration.get("storage", "filesystem_folder")) + folder = self._get_collection_root_folder() # path should already be sanitized self.path = sanitize_path(path).strip("/") self.storage_encoding = self.configuration.get("encoding", "stock") @@ -343,6 +342,13 @@ class Collection(BaseCollection): self.owner = None self.is_principal = principal + @classmethod + def _get_collection_root_folder(cls): + filesystem_folder = os.path.expanduser( + cls.configuration.get("storage", "filesystem_folder")) + folder = os.path.join(filesystem_folder, "collection-root") + return folder + @contextmanager def _atomic_write(self, path, mode="w"): with _EncodedAtomicWriter( @@ -370,8 +376,11 @@ class Collection(BaseCollection): attributes.pop() # Try to guess if the path leads to a collection or an item - folder = os.path.expanduser( - cls.configuration.get("storage", "filesystem_folder")) + folder = cls._get_collection_root_folder() + # HACK: Detection of principal collections fails if folder doesn't + # exist. This can be removed, when this method stop returning + # collections that don't exist. + os.makedirs(folder, exist_ok=True) if not os.path.isdir(path_to_filesystem(folder, sane_path)): # path is not a collection if attributes and os.path.isfile(path_to_filesystem(folder, @@ -406,8 +415,7 @@ class Collection(BaseCollection): @classmethod def create_collection(cls, href, collection=None, tag=None): - folder = os.path.expanduser( - cls.configuration.get("storage", "filesystem_folder")) + folder = cls._get_collection_root_folder() path = path_to_filesystem(folder, href) self = cls(href)