file.py 7.0 KB

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