1
0
Fork 0
mirror of https://github.com/Kozea/Radicale.git synced 2025-06-26 16:45:52 +00:00
Radicale/radicale/storage/multifilesystem/base.py

159 lines
6.6 KiB
Python
Raw Normal View History

2021-12-08 21:45:42 +01:00
# This file is part of Radicale - CalDAV and CardDAV server
2021-07-26 20:56:47 +02:00
# Copyright © 2014 Jean-Marc Martins
# Copyright © 2012-2017 Guillaume Ayoub
2024-06-09 13:57:52 +02:00
# Copyright © 2017-2022 Unrud <unrud@outlook.com>
# Copyright © 2024-2024 Peter Bieringer <pb@bieringer.de>
2021-07-26 20:56:47 +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/>.
import os
import sys
2021-07-26 20:56:47 +02:00
from tempfile import TemporaryDirectory
2021-12-12 20:05:11 +01:00
from typing import IO, AnyStr, ClassVar, Iterator, Optional, Type
2021-07-26 20:56:47 +02:00
from radicale import config, pathutils, storage, types
from radicale.storage import multifilesystem # noqa:F401
class CollectionBase(storage.BaseCollection):
_storage: "multifilesystem.Storage"
_path: str
_encoding: str
_filesystem_path: str
def __init__(self, storage_: "multifilesystem.Storage", path: str,
filesystem_path: Optional[str] = None) -> None:
super().__init__()
self._storage = storage_
folder = storage_._get_collection_root_folder()
# Path should already be sanitized
self._path = pathutils.strip_path(path)
self._encoding = storage_.configuration.get("encoding", "stock")
self._skip_broken_item = storage_.configuration.get("storage", "skip_broken_item")
2021-07-26 20:56:47 +02:00
if filesystem_path is None:
filesystem_path = pathutils.path_to_filesystem(folder, self.path)
self._filesystem_path = filesystem_path
2024-03-02 07:42:39 +01:00
# TODO: better fix for "mypy"
@types.contextmanager # type: ignore
2021-07-26 20:56:47 +02:00
def _atomic_write(self, path: str, mode: str = "w",
newline: Optional[str] = None) -> Iterator[IO[AnyStr]]:
# TODO: Overload with Literal when dropping support for Python < 3.8
parent_dir, name = os.path.split(path)
# Do not use mkstemp because it creates with permissions 0o600
with TemporaryDirectory(
prefix=".Radicale.tmp-", dir=parent_dir) as tmp_dir:
with open(os.path.join(tmp_dir, name), mode, newline=newline,
encoding=None if "b" in mode else self._encoding) as tmp:
yield tmp
tmp.flush()
self._storage._fsync(tmp)
os.replace(os.path.join(tmp_dir, name), path)
self._storage._sync_directory(parent_dir)
class StorageBase(storage.BaseStorage):
2021-12-12 20:05:11 +01:00
_collection_class: ClassVar[Type["multifilesystem.Collection"]]
2021-07-26 20:56:47 +02:00
_filesystem_folder: str
2024-12-10 08:24:12 +01:00
_filesystem_cache_folder: str
2021-07-26 20:56:47 +02:00
_filesystem_fsync: bool
2024-12-03 21:32:57 +01:00
_use_cache_subfolder_for_item: bool
2024-12-10 08:24:12 +01:00
_use_cache_subfolder_for_history: bool
_use_cache_subfolder_for_synctoken: bool
_folder_umask: str
_config_umask: int
2021-07-26 20:56:47 +02:00
def __init__(self, configuration: config.Configuration) -> None:
super().__init__(configuration)
self._filesystem_folder = configuration.get(
"storage", "filesystem_folder")
self._filesystem_fsync = configuration.get(
"storage", "_filesystem_fsync")
2024-12-10 08:24:12 +01:00
self._filesystem_cache_folder = configuration.get(
"storage", "filesystem_cache_folder")
2024-12-03 21:32:57 +01:00
self._use_cache_subfolder_for_item = configuration.get(
"storage", "use_cache_subfolder_for_item")
2024-12-10 08:24:12 +01:00
self._use_cache_subfolder_for_history = configuration.get(
"storage", "use_cache_subfolder_for_history")
self._use_cache_subfolder_for_synctoken = configuration.get(
"storage", "use_cache_subfolder_for_synctoken")
self._folder_umask = configuration.get(
"storage", "folder_umask")
2021-07-26 20:56:47 +02:00
def _get_collection_root_folder(self) -> str:
return os.path.join(self._filesystem_folder, "collection-root")
2024-12-10 08:24:12 +01:00
def _get_collection_cache_folder(self) -> str:
return os.path.join(self._filesystem_cache_folder, "collection-cache")
def _get_collection_cache_subfolder(self, path, folder, subfolder) -> str:
2024-12-03 21:42:50 +01:00
if (self._use_cache_subfolder_for_item is True) and (subfolder == "item"):
2024-12-10 08:24:12 +01:00
path = path.replace(self._get_collection_root_folder(), self._get_collection_cache_folder())
elif (self._use_cache_subfolder_for_history is True) and (subfolder == "history"):
path = path.replace(self._get_collection_root_folder(), self._get_collection_cache_folder())
elif (self._use_cache_subfolder_for_synctoken is True) and (subfolder == "sync-token"):
path = path.replace(self._get_collection_root_folder(), self._get_collection_cache_folder())
2024-12-03 21:32:57 +01:00
return os.path.join(path, folder, subfolder)
2021-07-26 20:56:47 +02:00
def _fsync(self, f: IO[AnyStr]) -> None:
if self._filesystem_fsync:
try:
pathutils.fsync(f.fileno())
except OSError as e:
raise RuntimeError("Fsync'ing file %r failed: %s" %
(f.name, e)) from e
def _sync_directory(self, path: str) -> None:
"""Sync directory to disk.
This only works on POSIX and does nothing on other systems.
"""
if not self._filesystem_fsync:
return
if sys.platform != "win32":
2021-07-26 20:56:47 +02:00
try:
fd = os.open(path, 0)
try:
pathutils.fsync(fd)
finally:
os.close(fd)
except OSError as e:
raise RuntimeError("Fsync'ing directory %r failed: %s" %
(path, e)) from e
def _makedirs_synced(self, filesystem_path: str) -> None:
"""Recursively create a directory and its parents in a sync'ed way.
This method acts silently when the folder already exists.
"""
if os.path.isdir(filesystem_path):
return
parent_filesystem_path = os.path.dirname(filesystem_path)
2024-12-10 08:24:41 +01:00
if sys.platform != "win32" and self._folder_umask:
oldmask = os.umask(self._config_umask)
2021-07-26 20:56:47 +02:00
# Prevent infinite loop
if filesystem_path != parent_filesystem_path:
# Create parent dirs recursively
self._makedirs_synced(parent_filesystem_path)
# Possible race!
os.makedirs(filesystem_path, exist_ok=True)
self._sync_directory(parent_filesystem_path)
2024-12-10 08:24:41 +01:00
if sys.platform != "win32" and self._folder_umask:
os.umask(oldmask)