pathutils.py 10 KB

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