pathutils.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. else:
  65. import fcntl
  66. if sys.platform == "linux":
  67. import ctypes
  68. RENAME_EXCHANGE: int = 2
  69. renameat2 = None
  70. try:
  71. renameat2 = ctypes.CDLL(None, use_errno=True).renameat2
  72. except AttributeError:
  73. pass
  74. else:
  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. if sys.platform == "darwin":
  81. # Definition missing in PyPy
  82. F_FULLFSYNC: int = getattr(fcntl, "F_FULLFSYNC", 51)
  83. class RwLock:
  84. """A readers-Writer lock that locks a file."""
  85. _path: str
  86. _readers: int
  87. _writer: bool
  88. _lock: threading.Lock
  89. def __init__(self, path: str) -> None:
  90. self._path = path
  91. self._readers = 0
  92. self._writer = False
  93. self._lock = threading.Lock()
  94. @property
  95. def locked(self) -> str:
  96. with self._lock:
  97. if self._readers > 0:
  98. return "r"
  99. if self._writer:
  100. return "w"
  101. return ""
  102. @types.contextmanager
  103. def acquire(self, mode: str) -> Iterator[None]:
  104. if mode not in "rw":
  105. raise ValueError("Invalid mode: %r" % mode)
  106. with open(self._path, "w+") as lock_file:
  107. if sys.platform == "win32":
  108. handle = msvcrt.get_osfhandle(lock_file.fileno())
  109. flags = LOCKFILE_EXCLUSIVE_LOCK if mode == "w" else 0
  110. overlapped = Overlapped()
  111. try:
  112. if not lock_file_ex(handle, flags, 0, 1, 0, overlapped):
  113. raise ctypes.WinError()
  114. except OSError as e:
  115. raise RuntimeError("Locking the storage failed: %s" % e
  116. ) from e
  117. else:
  118. _cmd = fcntl.LOCK_EX if mode == "w" else fcntl.LOCK_SH
  119. try:
  120. fcntl.flock(lock_file.fileno(), _cmd)
  121. except OSError as e:
  122. raise RuntimeError("Locking the storage failed: %s" % e
  123. ) from e
  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 sys.platform == "linux" and 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. return
  162. errno_ = ctypes.get_errno()
  163. # Fallback if RENAME_EXCHANGE not supported by filesystem
  164. if errno_ != errno.EINVAL:
  165. raise OSError(errno_, os.strerror(errno_))
  166. finally:
  167. os.close(dst_dir_fd)
  168. finally:
  169. os.close(src_dir_fd)
  170. with TemporaryDirectory(prefix=".Radicale.tmp-", dir=src_dir
  171. ) as tmp_dir:
  172. os.rename(dst, os.path.join(tmp_dir, "interim"))
  173. os.rename(src, dst)
  174. os.rename(os.path.join(tmp_dir, "interim"), src)
  175. def fsync(fd: int) -> None:
  176. if sys.platform == "darwin":
  177. try:
  178. fcntl.fcntl(fd, F_FULLFSYNC)
  179. return
  180. except OSError as e:
  181. # Fallback if F_FULLFSYNC not supported by filesystem
  182. if e.errno != errno.EINVAL:
  183. raise
  184. os.fsync(fd)
  185. def strip_path(path: str) -> str:
  186. assert sanitize_path(path) == path
  187. return path.strip("/")
  188. def unstrip_path(stripped_path: str, trailing_slash: bool = False) -> str:
  189. assert strip_path(sanitize_path(stripped_path)) == stripped_path
  190. assert stripped_path or trailing_slash
  191. path = "/%s" % stripped_path
  192. if trailing_slash and not path.endswith("/"):
  193. path += "/"
  194. return path
  195. def sanitize_path(path: str) -> str:
  196. """Make path absolute with leading slash to prevent access to other data.
  197. Preserve potential trailing slash.
  198. """
  199. trailing_slash = "/" if path.endswith("/") else ""
  200. path = posixpath.normpath(path)
  201. new_path = "/"
  202. for part in path.split("/"):
  203. if not is_safe_path_component(part):
  204. continue
  205. new_path = posixpath.join(new_path, part)
  206. trailing_slash = "" if new_path.endswith("/") else trailing_slash
  207. return new_path + trailing_slash
  208. def is_safe_path_component(path: str) -> bool:
  209. """Check if path is a single component of a path.
  210. Check that the path is safe to join too.
  211. """
  212. return bool(path) and "/" not in path and path not in (".", "..")
  213. def is_safe_filesystem_path_component(path: str) -> bool:
  214. """Check if path is a single component of a local and posix filesystem
  215. path.
  216. Check that the path is safe to join too.
  217. """
  218. return (
  219. bool(path) and not os.path.splitdrive(path)[0] and
  220. (sys.platform != "win32" or ":" not in path) and # Block NTFS-ADS
  221. not os.path.split(path)[0] and path not in (os.curdir, os.pardir) and
  222. not path.startswith(".") and not path.endswith("~") and
  223. is_safe_path_component(path))
  224. def path_to_filesystem(root: str, sane_path: str) -> str:
  225. """Convert `sane_path` to a local filesystem path relative to `root`.
  226. `root` must be a secure filesystem path, it will be prepend to the path.
  227. `sane_path` must be a sanitized path without leading or trailing ``/``.
  228. Conversion of `sane_path` is done in a secure manner,
  229. or raises ``ValueError``.
  230. """
  231. assert sane_path == strip_path(sanitize_path(sane_path))
  232. safe_path = root
  233. parts = sane_path.split("/") if sane_path else []
  234. for part in parts:
  235. if not is_safe_filesystem_path_component(part):
  236. raise UnsafePathError(part)
  237. safe_path_parent = safe_path
  238. safe_path = os.path.join(safe_path, part)
  239. # Check for conflicting files (e.g. case-insensitive file systems
  240. # or short names on Windows file systems)
  241. if (os.path.lexists(safe_path) and
  242. part not in (e.name for e in os.scandir(safe_path_parent))):
  243. raise CollidingPathError(part)
  244. return safe_path
  245. class UnsafePathError(ValueError):
  246. def __init__(self, path: str) -> None:
  247. super().__init__("Can't translate name safely to filesystem: %r" %
  248. path)
  249. class CollidingPathError(ValueError):
  250. def __init__(self, path: str) -> None:
  251. super().__init__("File name collision: %r" % path)
  252. def name_from_path(path: str, collection: "storage.BaseCollection") -> str:
  253. """Return Radicale item name from ``path``."""
  254. assert sanitize_path(path) == path
  255. start = unstrip_path(collection.path, True)
  256. if not (path + "/").startswith(start):
  257. raise ValueError("%r doesn't start with %r" % (path, start))
  258. name = path[len(start):]
  259. if name and not is_safe_path_component(name):
  260. raise ValueError("%r is not a component in collection %r" %
  261. (name, collection.path))
  262. return name