base.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2014 Jean-Marc Martins
  3. # Copyright © 2012-2017 Guillaume Ayoub
  4. # Copyright © 2017-2019 Unrud <unrud@outlook.com>
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. import os
  19. from tempfile import TemporaryDirectory
  20. from typing import IO, AnyStr, Iterator, Optional, Type
  21. from radicale import config, pathutils, storage, types
  22. from radicale.storage import multifilesystem # noqa:F401
  23. class CollectionBase(storage.BaseCollection):
  24. _storage: "multifilesystem.Storage"
  25. _path: str
  26. _encoding: str
  27. _filesystem_path: str
  28. def __init__(self, storage_: "multifilesystem.Storage", path: str,
  29. filesystem_path: Optional[str] = None) -> None:
  30. super().__init__()
  31. self._storage = storage_
  32. folder = storage_._get_collection_root_folder()
  33. # Path should already be sanitized
  34. self._path = pathutils.strip_path(path)
  35. self._encoding = storage_.configuration.get("encoding", "stock")
  36. if filesystem_path is None:
  37. filesystem_path = pathutils.path_to_filesystem(folder, self.path)
  38. self._filesystem_path = filesystem_path
  39. @types.contextmanager
  40. def _atomic_write(self, path: str, mode: str = "w",
  41. newline: Optional[str] = None) -> Iterator[IO[AnyStr]]:
  42. # TODO: Overload with Literal when dropping support for Python < 3.8
  43. parent_dir, name = os.path.split(path)
  44. # Do not use mkstemp because it creates with permissions 0o600
  45. with TemporaryDirectory(
  46. prefix=".Radicale.tmp-", dir=parent_dir) as tmp_dir:
  47. with open(os.path.join(tmp_dir, name), mode, newline=newline,
  48. encoding=None if "b" in mode else self._encoding) as tmp:
  49. yield tmp
  50. tmp.flush()
  51. self._storage._fsync(tmp)
  52. os.replace(os.path.join(tmp_dir, name), path)
  53. self._storage._sync_directory(parent_dir)
  54. class StorageBase(storage.BaseStorage):
  55. _collection_class: Type["multifilesystem.Collection"]
  56. _filesystem_folder: str
  57. _filesystem_fsync: bool
  58. def __init__(self, configuration: config.Configuration) -> None:
  59. super().__init__(configuration)
  60. self._filesystem_folder = configuration.get(
  61. "storage", "filesystem_folder")
  62. self._filesystem_fsync = configuration.get(
  63. "storage", "_filesystem_fsync")
  64. def _get_collection_root_folder(self) -> str:
  65. return os.path.join(self._filesystem_folder, "collection-root")
  66. def _fsync(self, f: IO[AnyStr]) -> None:
  67. if self._filesystem_fsync:
  68. try:
  69. pathutils.fsync(f.fileno())
  70. except OSError as e:
  71. raise RuntimeError("Fsync'ing file %r failed: %s" %
  72. (f.name, e)) from e
  73. def _sync_directory(self, path: str) -> None:
  74. """Sync directory to disk.
  75. This only works on POSIX and does nothing on other systems.
  76. """
  77. if not self._filesystem_fsync:
  78. return
  79. if os.name == "posix":
  80. try:
  81. fd = os.open(path, 0)
  82. try:
  83. pathutils.fsync(fd)
  84. finally:
  85. os.close(fd)
  86. except OSError as e:
  87. raise RuntimeError("Fsync'ing directory %r failed: %s" %
  88. (path, e)) from e
  89. def _makedirs_synced(self, filesystem_path: str) -> None:
  90. """Recursively create a directory and its parents in a sync'ed way.
  91. This method acts silently when the folder already exists.
  92. """
  93. if os.path.isdir(filesystem_path):
  94. return
  95. parent_filesystem_path = os.path.dirname(filesystem_path)
  96. # Prevent infinite loop
  97. if filesystem_path != parent_filesystem_path:
  98. # Create parent dirs recursively
  99. self._makedirs_synced(parent_filesystem_path)
  100. # Possible race!
  101. os.makedirs(filesystem_path, exist_ok=True)
  102. self._sync_directory(parent_filesystem_path)