pathutils.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2014 Jean-Marc Martins
  3. # Copyright © 2012-2017 Guillaume Ayoub
  4. # Copyright © 2017-2018 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. """
  19. Helper functions for working with the file system.
  20. """
  21. import os
  22. import posixpath
  23. import sys
  24. import threading
  25. from tempfile import TemporaryDirectory
  26. from typing import Iterator, Type, Union
  27. from radicale import storage, types
  28. if sys.platform == "win32":
  29. import ctypes
  30. import ctypes.wintypes
  31. import msvcrt
  32. LOCKFILE_EXCLUSIVE_LOCK: int = 2
  33. ULONG_PTR: Union[Type[ctypes.c_uint32], Type[ctypes.c_uint64]]
  34. if ctypes.sizeof(ctypes.c_void_p) == 4:
  35. ULONG_PTR = ctypes.c_uint32
  36. else:
  37. ULONG_PTR = ctypes.c_uint64
  38. class Overlapped(ctypes.Structure):
  39. _fields_ = [
  40. ("internal", ULONG_PTR),
  41. ("internal_high", ULONG_PTR),
  42. ("offset", ctypes.wintypes.DWORD),
  43. ("offset_high", ctypes.wintypes.DWORD),
  44. ("h_event", ctypes.wintypes.HANDLE)]
  45. kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
  46. lock_file_ex = kernel32.LockFileEx
  47. lock_file_ex.argtypes = [
  48. ctypes.wintypes.HANDLE,
  49. ctypes.wintypes.DWORD,
  50. ctypes.wintypes.DWORD,
  51. ctypes.wintypes.DWORD,
  52. ctypes.wintypes.DWORD,
  53. ctypes.POINTER(Overlapped)]
  54. lock_file_ex.restype = ctypes.wintypes.BOOL
  55. unlock_file_ex = kernel32.UnlockFileEx
  56. unlock_file_ex.argtypes = [
  57. ctypes.wintypes.HANDLE,
  58. ctypes.wintypes.DWORD,
  59. ctypes.wintypes.DWORD,
  60. ctypes.wintypes.DWORD,
  61. ctypes.POINTER(Overlapped)]
  62. unlock_file_ex.restype = ctypes.wintypes.BOOL
  63. elif os.name == "posix":
  64. import fcntl
  65. HAVE_RENAMEAT2: bool = False
  66. if sys.platform == "linux":
  67. import ctypes
  68. RENAME_EXCHANGE: int = 2
  69. try:
  70. renameat2 = ctypes.CDLL("", use_errno=True).renameat2
  71. except AttributeError:
  72. pass
  73. else:
  74. HAVE_RENAMEAT2 = True
  75. renameat2.argtypes = [
  76. ctypes.c_int, ctypes.c_char_p,
  77. ctypes.c_int, ctypes.c_char_p,
  78. ctypes.c_uint]
  79. renameat2.restype = ctypes.c_int
  80. class RwLock:
  81. """A readers-Writer lock that locks a file."""
  82. _path: str
  83. _readers: int
  84. _writer: bool
  85. _lock: threading.Lock
  86. def __init__(self, path: str) -> None:
  87. self._path = path
  88. self._readers = 0
  89. self._writer = False
  90. self._lock = threading.Lock()
  91. @property
  92. def locked(self) -> str:
  93. with self._lock:
  94. if self._readers > 0:
  95. return "r"
  96. if self._writer:
  97. return "w"
  98. return ""
  99. @types.contextmanager
  100. def acquire(self, mode: str) -> Iterator[None]:
  101. if mode not in "rw":
  102. raise ValueError("Invalid mode: %r" % mode)
  103. with open(self._path, "w+") as lock_file:
  104. if sys.platform == "win32":
  105. handle = msvcrt.get_osfhandle(lock_file.fileno())
  106. flags = LOCKFILE_EXCLUSIVE_LOCK if mode == "w" else 0
  107. overlapped = Overlapped()
  108. try:
  109. if not lock_file_ex(handle, flags, 0, 1, 0, overlapped):
  110. raise ctypes.WinError()
  111. except OSError as e:
  112. raise RuntimeError("Locking the storage failed: %s" % e
  113. ) from e
  114. elif os.name == "posix":
  115. _cmd = fcntl.LOCK_EX if mode == "w" else fcntl.LOCK_SH
  116. try:
  117. fcntl.flock(lock_file.fileno(), _cmd)
  118. except OSError as e:
  119. raise RuntimeError("Locking the storage failed: %s" % e
  120. ) from e
  121. else:
  122. raise RuntimeError("Locking the storage failed: "
  123. "Unsupported operating system")
  124. with self._lock:
  125. if self._writer or mode == "w" and self._readers != 0:
  126. raise RuntimeError("Locking the storage failed: "
  127. "Guarantees failed")
  128. if mode == "r":
  129. self._readers += 1
  130. else:
  131. self._writer = True
  132. try:
  133. yield
  134. finally:
  135. with self._lock:
  136. if mode == "r":
  137. self._readers -= 1
  138. self._writer = False
  139. def rename_exchange(src: str, dst: str) -> None:
  140. """Exchange the files or directories `src` and `dst`.
  141. Both `src` and `dst` must exist but may be of different types.
  142. On Linux with renameat2 the operation is atomic.
  143. On other platforms it's not atomic.
  144. """
  145. src_dir, src_base = os.path.split(src)
  146. dst_dir, dst_base = os.path.split(dst)
  147. src_dir = src_dir or os.curdir
  148. dst_dir = dst_dir or os.curdir
  149. if not src_base or not dst_base:
  150. raise ValueError("Invalid arguments: %r -> %r" % (src, dst))
  151. if HAVE_RENAMEAT2:
  152. src_base_bytes = os.fsencode(src_base)
  153. dst_base_bytes = os.fsencode(dst_base)
  154. src_dir_fd = os.open(src_dir, 0)
  155. try:
  156. dst_dir_fd = os.open(dst_dir, 0)
  157. try:
  158. if renameat2(src_dir_fd, src_base_bytes,
  159. dst_dir_fd, dst_base_bytes,
  160. RENAME_EXCHANGE) != 0:
  161. errno = ctypes.get_errno()
  162. raise OSError(errno, os.strerror(errno))
  163. finally:
  164. os.close(dst_dir_fd)
  165. finally:
  166. os.close(src_dir_fd)
  167. else:
  168. with TemporaryDirectory(prefix=".Radicale.tmp-", dir=src_dir
  169. ) as tmp_dir:
  170. os.rename(dst, os.path.join(tmp_dir, "interim"))
  171. os.rename(src, dst)
  172. os.rename(os.path.join(tmp_dir, "interim"), src)
  173. def fsync(fd: int) -> None:
  174. if os.name == "posix" and hasattr(fcntl, "F_FULLFSYNC"):
  175. fcntl.fcntl(fd, fcntl.F_FULLFSYNC)
  176. else:
  177. os.fsync(fd)
  178. def strip_path(path: str) -> str:
  179. assert sanitize_path(path) == path
  180. return path.strip("/")
  181. def unstrip_path(stripped_path: str, trailing_slash: bool = False) -> str:
  182. assert strip_path(sanitize_path(stripped_path)) == stripped_path
  183. assert stripped_path or trailing_slash
  184. path = "/%s" % stripped_path
  185. if trailing_slash and not path.endswith("/"):
  186. path += "/"
  187. return path
  188. def sanitize_path(path: str) -> str:
  189. """Make path absolute with leading slash to prevent access to other data.
  190. Preserve potential trailing slash.
  191. """
  192. trailing_slash = "/" if path.endswith("/") else ""
  193. path = posixpath.normpath(path)
  194. new_path = "/"
  195. for part in path.split("/"):
  196. if not is_safe_path_component(part):
  197. continue
  198. new_path = posixpath.join(new_path, part)
  199. trailing_slash = "" if new_path.endswith("/") else trailing_slash
  200. return new_path + trailing_slash
  201. def is_safe_path_component(path: str) -> bool:
  202. """Check if path is a single component of a path.
  203. Check that the path is safe to join too.
  204. """
  205. return bool(path) and "/" not in path and path not in (".", "..")
  206. def is_safe_filesystem_path_component(path: str) -> bool:
  207. """Check if path is a single component of a local and posix filesystem
  208. path.
  209. Check that the path is safe to join too.
  210. """
  211. return (
  212. bool(path) and not os.path.splitdrive(path)[0] and
  213. not os.path.split(path)[0] and path not in (os.curdir, os.pardir) and
  214. not path.startswith(".") and not path.endswith("~") and
  215. is_safe_path_component(path))
  216. def path_to_filesystem(root: str, sane_path: str) -> str:
  217. """Convert `sane_path` to a local filesystem path relative to `root`.
  218. `root` must be a secure filesystem path, it will be prepend to the path.
  219. `sane_path` must be a sanitized path without leading or trailing ``/``.
  220. Conversion of `sane_path` is done in a secure manner,
  221. or raises ``ValueError``.
  222. """
  223. assert sane_path == strip_path(sanitize_path(sane_path))
  224. safe_path = root
  225. parts = sane_path.split("/") if sane_path else []
  226. for part in parts:
  227. if not is_safe_filesystem_path_component(part):
  228. raise UnsafePathError(part)
  229. safe_path_parent = safe_path
  230. safe_path = os.path.join(safe_path, part)
  231. # Check for conflicting files (e.g. case-insensitive file systems
  232. # or short names on Windows file systems)
  233. if (os.path.lexists(safe_path) and
  234. part not in (e.name for e in os.scandir(safe_path_parent))):
  235. raise CollidingPathError(part)
  236. return safe_path
  237. class UnsafePathError(ValueError):
  238. def __init__(self, path: str) -> None:
  239. super().__init__("Can't translate name safely to filesystem: %r" %
  240. path)
  241. class CollidingPathError(ValueError):
  242. def __init__(self, path: str) -> None:
  243. super().__init__("File name collision: %r" % path)
  244. def name_from_path(path: str, collection: "storage.BaseCollection") -> str:
  245. """Return Radicale item name from ``path``."""
  246. assert sanitize_path(path) == path
  247. start = unstrip_path(collection.path, True)
  248. if not (path + "/").startswith(start):
  249. raise ValueError("%r doesn't start with %r" % (path, start))
  250. name = path[len(start):]
  251. if name and not is_safe_path_component(name):
  252. raise ValueError("%r is not a component in collection %r" %
  253. (name, collection.path))
  254. return name