file.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 public 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 typing import ClassVar, Union
  26. def ensure_dir_exists(dirname) -> None:
  27. """Ensure a directory exists, creating if necessary."""
  28. try:
  29. os.makedirs(dirname)
  30. except FileExistsError:
  31. pass
  32. def _fancy_rename(oldname, newname) -> None:
  33. """Rename file with temporary backup file to rollback if rename fails."""
  34. if not os.path.exists(newname):
  35. os.rename(oldname, newname)
  36. return
  37. # Defer the tempfile import since it pulls in a lot of other things.
  38. import tempfile
  39. # destination file exists
  40. (fd, tmpfile) = tempfile.mkstemp(".tmp", prefix=oldname, dir=".")
  41. os.close(fd)
  42. os.remove(tmpfile)
  43. os.rename(newname, tmpfile)
  44. try:
  45. os.rename(oldname, newname)
  46. except OSError:
  47. os.rename(tmpfile, newname)
  48. raise
  49. os.remove(tmpfile)
  50. def GitFile(
  51. filename: Union[str, bytes, os.PathLike], mode="rb", bufsize=-1, mask=0o644
  52. ):
  53. """Create a file object that obeys the git file locking protocol.
  54. Returns: a builtin file object or a _GitFile object
  55. Note: See _GitFile for a description of the file locking protocol.
  56. Only read-only and write-only (binary) modes are supported; r+, w+, and a
  57. are not. To read and write from the same file, you can take advantage of
  58. the fact that opening a file for write does not actually open the file you
  59. request.
  60. The default file mask makes any created files user-writable and
  61. world-readable.
  62. """
  63. if "a" in mode:
  64. raise OSError("append mode not supported for Git files")
  65. if "+" in mode:
  66. raise OSError("read/write mode not supported for Git files")
  67. if "b" not in mode:
  68. raise OSError("text mode not supported for Git files")
  69. if "w" in mode:
  70. return _GitFile(filename, mode, bufsize, mask)
  71. else:
  72. return open(filename, mode, bufsize)
  73. class FileLocked(Exception):
  74. """File is already locked."""
  75. def __init__(self, filename, lockfilename) -> None:
  76. self.filename = filename
  77. self.lockfilename = lockfilename
  78. super().__init__(filename, lockfilename)
  79. class _GitFile:
  80. """File that follows the git locking protocol for writes.
  81. All writes to a file foo will be written into foo.lock in the same
  82. directory, and the lockfile will be renamed to overwrite the original file
  83. on close.
  84. Note: You *must* call close() or abort() on a _GitFile for the lock to be
  85. released. Typically this will happen in a finally block.
  86. """
  87. PROXY_PROPERTIES: ClassVar[set[str]] = {
  88. "closed",
  89. "encoding",
  90. "errors",
  91. "mode",
  92. "name",
  93. "newlines",
  94. "softspace",
  95. }
  96. PROXY_METHODS: ClassVar[set[str]] = {
  97. "__iter__",
  98. "flush",
  99. "fileno",
  100. "isatty",
  101. "read",
  102. "readline",
  103. "readlines",
  104. "seek",
  105. "tell",
  106. "truncate",
  107. "write",
  108. "writelines",
  109. }
  110. def __init__(
  111. self, filename: Union[str, bytes, os.PathLike], mode, bufsize, mask
  112. ) -> None:
  113. # Convert PathLike to str/bytes for our internal use
  114. self._filename: Union[str, bytes] = os.fspath(filename)
  115. if isinstance(self._filename, bytes):
  116. self._lockfilename: Union[str, bytes] = self._filename + b".lock"
  117. else:
  118. self._lockfilename = self._filename + ".lock"
  119. try:
  120. fd = os.open(
  121. self._lockfilename,
  122. os.O_RDWR | os.O_CREAT | os.O_EXCL | getattr(os, "O_BINARY", 0),
  123. mask,
  124. )
  125. except FileExistsError as exc:
  126. raise FileLocked(filename, self._lockfilename) from exc
  127. self._file = os.fdopen(fd, mode, bufsize)
  128. self._closed = False
  129. for method in self.PROXY_METHODS:
  130. setattr(self, method, getattr(self._file, method))
  131. def abort(self) -> None:
  132. """Close and discard the lockfile without overwriting the target.
  133. If the file is already closed, this is a no-op.
  134. """
  135. if self._closed:
  136. return
  137. self._file.close()
  138. try:
  139. os.remove(self._lockfilename)
  140. self._closed = True
  141. except FileNotFoundError:
  142. # The file may have been removed already, which is ok.
  143. self._closed = True
  144. def close(self) -> None:
  145. """Close this file, saving the lockfile over the original.
  146. Note: If this method fails, it will attempt to delete the lockfile.
  147. However, it is not guaranteed to do so (e.g. if a filesystem
  148. becomes suddenly read-only), which will prevent future writes to
  149. this file until the lockfile is removed manually.
  150. Raises:
  151. OSError: if the original file could not be overwritten. The
  152. lock file is still closed, so further attempts to write to the same
  153. file object will raise ValueError.
  154. """
  155. if self._closed:
  156. return
  157. self._file.flush()
  158. os.fsync(self._file.fileno())
  159. self._file.close()
  160. try:
  161. if getattr(os, "replace", None) is not None:
  162. os.replace(self._lockfilename, self._filename)
  163. else:
  164. if sys.platform != "win32":
  165. os.rename(self._lockfilename, self._filename)
  166. else:
  167. # Windows versions prior to Vista don't support atomic
  168. # renames
  169. _fancy_rename(self._lockfilename, self._filename)
  170. finally:
  171. self.abort()
  172. def __del__(self) -> None:
  173. if not getattr(self, "_closed", True):
  174. warnings.warn(f"unclosed {self!r}", ResourceWarning, stacklevel=2)
  175. self.abort()
  176. def __enter__(self):
  177. return self
  178. def __exit__(self, exc_type, exc_val, exc_tb):
  179. if exc_type is not None:
  180. self.abort()
  181. else:
  182. self.close()
  183. def __getattr__(self, name):
  184. """Proxy property calls to the underlying file."""
  185. if name in self.PROXY_PROPERTIES:
  186. return getattr(self._file, name)
  187. raise AttributeError(name)