pathutils.py 9.6 KB

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