file.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. # file.py -- Safe access to git files
  2. # Copyright (C) 2010 Google, Inc.
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as published by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Safe access to git files."""
  22. import os
  23. import sys
  24. import warnings
  25. from collections.abc import Iterator
  26. from types import TracebackType
  27. from typing import IO, Any, ClassVar, Literal, Optional, Union, overload
  28. def ensure_dir_exists(dirname: Union[str, bytes, os.PathLike]) -> None:
  29. """Ensure a directory exists, creating if necessary."""
  30. try:
  31. os.makedirs(dirname)
  32. except FileExistsError:
  33. pass
  34. def _fancy_rename(oldname: Union[str, bytes], newname: Union[str, bytes]) -> None:
  35. """Rename file with temporary backup file to rollback if rename fails."""
  36. if not os.path.exists(newname):
  37. os.rename(oldname, newname)
  38. return
  39. # Defer the tempfile import since it pulls in a lot of other things.
  40. import tempfile
  41. # destination file exists
  42. (fd, tmpfile) = tempfile.mkstemp(".tmp", prefix=str(oldname), dir=".")
  43. os.close(fd)
  44. os.remove(tmpfile)
  45. os.rename(newname, tmpfile)
  46. try:
  47. os.rename(oldname, newname)
  48. except OSError:
  49. os.rename(tmpfile, newname)
  50. raise
  51. os.remove(tmpfile)
  52. @overload
  53. def GitFile(
  54. filename: Union[str, bytes, os.PathLike],
  55. mode: Literal["wb"],
  56. bufsize: int = -1,
  57. mask: int = 0o644,
  58. ) -> "_GitFile": ...
  59. @overload
  60. def GitFile(
  61. filename: Union[str, bytes, os.PathLike],
  62. mode: Literal["rb"] = "rb",
  63. bufsize: int = -1,
  64. mask: int = 0o644,
  65. ) -> IO[bytes]: ...
  66. @overload
  67. def GitFile(
  68. filename: Union[str, bytes, os.PathLike],
  69. mode: str = "rb",
  70. bufsize: int = -1,
  71. mask: int = 0o644,
  72. ) -> Union[IO[bytes], "_GitFile"]: ...
  73. def GitFile(
  74. filename: Union[str, bytes, os.PathLike],
  75. mode: str = "rb",
  76. bufsize: int = -1,
  77. mask: int = 0o644,
  78. ) -> Union[IO[bytes], "_GitFile"]:
  79. """Create a file object that obeys the git file locking protocol.
  80. Returns: a builtin file object or a _GitFile object
  81. Note: See _GitFile for a description of the file locking protocol.
  82. Only read-only and write-only (binary) modes are supported; r+, w+, and a
  83. are not. To read and write from the same file, you can take advantage of
  84. the fact that opening a file for write does not actually open the file you
  85. request.
  86. The default file mask makes any created files user-writable and
  87. world-readable.
  88. """
  89. if "a" in mode:
  90. raise OSError("append mode not supported for Git files")
  91. if "+" in mode:
  92. raise OSError("read/write mode not supported for Git files")
  93. if "b" not in mode:
  94. raise OSError("text mode not supported for Git files")
  95. if "w" in mode:
  96. return _GitFile(filename, mode, bufsize, mask)
  97. else:
  98. return open(filename, mode, bufsize)
  99. class FileLocked(Exception):
  100. """File is already locked."""
  101. def __init__(
  102. self, filename: Union[str, bytes, os.PathLike], lockfilename: Union[str, bytes]
  103. ) -> None:
  104. """Initialize FileLocked.
  105. Args:
  106. filename: Name of the file that is locked
  107. lockfilename: Name of the lock file
  108. """
  109. self.filename = filename
  110. self.lockfilename = lockfilename
  111. super().__init__(filename, lockfilename)
  112. class _GitFile:
  113. """File that follows the git locking protocol for writes.
  114. All writes to a file foo will be written into foo.lock in the same
  115. directory, and the lockfile will be renamed to overwrite the original file
  116. on close.
  117. Note: You *must* call close() or abort() on a _GitFile for the lock to be
  118. released. Typically this will happen in a finally block.
  119. """
  120. PROXY_PROPERTIES: ClassVar[set[str]] = {
  121. "closed",
  122. "encoding",
  123. "errors",
  124. "mode",
  125. "name",
  126. "newlines",
  127. "softspace",
  128. }
  129. PROXY_METHODS: ClassVar[set[str]] = {
  130. "__iter__",
  131. "flush",
  132. "fileno",
  133. "isatty",
  134. "read",
  135. "readline",
  136. "readlines",
  137. "seek",
  138. "tell",
  139. "truncate",
  140. "write",
  141. "writelines",
  142. }
  143. def __init__(
  144. self,
  145. filename: Union[str, bytes, os.PathLike],
  146. mode: str,
  147. bufsize: int,
  148. mask: int,
  149. ) -> None:
  150. # Convert PathLike to str/bytes for our internal use
  151. self._filename: Union[str, bytes] = os.fspath(filename)
  152. if isinstance(self._filename, bytes):
  153. self._lockfilename: Union[str, bytes] = self._filename + b".lock"
  154. else:
  155. self._lockfilename = self._filename + ".lock"
  156. try:
  157. fd = os.open(
  158. self._lockfilename,
  159. os.O_RDWR | os.O_CREAT | os.O_EXCL | getattr(os, "O_BINARY", 0),
  160. mask,
  161. )
  162. except FileExistsError as exc:
  163. raise FileLocked(filename, self._lockfilename) from exc
  164. self._file = os.fdopen(fd, mode, bufsize)
  165. self._closed = False
  166. for method in self.PROXY_METHODS:
  167. setattr(self, method, getattr(self._file, method))
  168. def __iter__(self) -> Iterator[bytes]:
  169. """Iterate over lines in the file."""
  170. return iter(self._file)
  171. def abort(self) -> None:
  172. """Close and discard the lockfile without overwriting the target.
  173. If the file is already closed, this is a no-op.
  174. """
  175. if self._closed:
  176. return
  177. self._file.close()
  178. try:
  179. os.remove(self._lockfilename)
  180. self._closed = True
  181. except FileNotFoundError:
  182. # The file may have been removed already, which is ok.
  183. self._closed = True
  184. def close(self) -> None:
  185. """Close this file, saving the lockfile over the original.
  186. Note: If this method fails, it will attempt to delete the lockfile.
  187. However, it is not guaranteed to do so (e.g. if a filesystem
  188. becomes suddenly read-only), which will prevent future writes to
  189. this file until the lockfile is removed manually.
  190. Raises:
  191. OSError: if the original file could not be overwritten. The
  192. lock file is still closed, so further attempts to write to the same
  193. file object will raise ValueError.
  194. """
  195. if self._closed:
  196. return
  197. self._file.flush()
  198. os.fsync(self._file.fileno())
  199. self._file.close()
  200. try:
  201. if getattr(os, "replace", None) is not None:
  202. os.replace(self._lockfilename, self._filename)
  203. else:
  204. if sys.platform != "win32":
  205. os.rename(self._lockfilename, self._filename)
  206. else:
  207. # Windows versions prior to Vista don't support atomic
  208. # renames
  209. _fancy_rename(self._lockfilename, self._filename)
  210. finally:
  211. self.abort()
  212. def __del__(self) -> None:
  213. if not getattr(self, "_closed", True):
  214. warnings.warn(f"unclosed {self!r}", ResourceWarning, stacklevel=2)
  215. self.abort()
  216. def __enter__(self) -> "_GitFile":
  217. return self
  218. def __exit__(
  219. self,
  220. exc_type: Optional[type[BaseException]],
  221. exc_val: Optional[BaseException],
  222. exc_tb: Optional[TracebackType],
  223. ) -> None:
  224. if exc_type is not None:
  225. self.abort()
  226. else:
  227. self.close()
  228. def __getattr__(self, name: str) -> Any: # noqa: ANN401
  229. """Proxy property calls to the underlying file."""
  230. if name in self.PROXY_PROPERTIES:
  231. return getattr(self._file, name)
  232. raise AttributeError(name)
  233. def readable(self) -> bool:
  234. """Return whether the file is readable."""
  235. return self._file.readable()
  236. def writable(self) -> bool:
  237. """Return whether the file is writable."""
  238. return self._file.writable()
  239. def seekable(self) -> bool:
  240. """Return whether the file is seekable."""
  241. return self._file.seekable()