file.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 io
  22. import os
  23. import sys
  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 IOError("append mode not supported for Git files")
  72. if "+" in mode:
  73. raise IOError("read/write mode not supported for Git files")
  74. if "b" not in mode:
  75. raise IOError("text mode not supported for Git files")
  76. if "w" in mode:
  77. return _GitFile(filename, mode, bufsize, mask)
  78. else:
  79. return io.open(filename, mode, bufsize)
  80. class FileLocked(Exception):
  81. """File is already locked."""
  82. def __init__(self, filename, lockfilename):
  83. self.filename = filename
  84. self.lockfilename = lockfilename
  85. super(FileLocked, self).__init__(filename, lockfilename)
  86. class _GitFile(object):
  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 = set(
  95. [
  96. "closed",
  97. "encoding",
  98. "errors",
  99. "mode",
  100. "name",
  101. "newlines",
  102. "softspace",
  103. ]
  104. )
  105. PROXY_METHODS = (
  106. "__iter__",
  107. "flush",
  108. "fileno",
  109. "isatty",
  110. "read",
  111. "readline",
  112. "readlines",
  113. "seek",
  114. "tell",
  115. "truncate",
  116. "write",
  117. "writelines",
  118. )
  119. def __init__(self, filename, mode, bufsize, mask):
  120. self._filename = filename
  121. if isinstance(self._filename, bytes):
  122. self._lockfilename = self._filename + b".lock"
  123. else:
  124. self._lockfilename = self._filename + ".lock"
  125. try:
  126. fd = os.open(
  127. self._lockfilename,
  128. os.O_RDWR | os.O_CREAT | os.O_EXCL | getattr(os, "O_BINARY", 0),
  129. mask,
  130. )
  131. except FileExistsError:
  132. raise FileLocked(filename, self._lockfilename)
  133. self._file = os.fdopen(fd, mode, bufsize)
  134. self._closed = False
  135. for method in self.PROXY_METHODS:
  136. setattr(self, method, getattr(self._file, method))
  137. def abort(self):
  138. """Close and discard the lockfile without overwriting the target.
  139. If the file is already closed, this is a no-op.
  140. """
  141. if self._closed:
  142. return
  143. self._file.close()
  144. try:
  145. os.remove(self._lockfilename)
  146. self._closed = True
  147. except FileNotFoundError:
  148. # The file may have been removed already, which is ok.
  149. self._closed = True
  150. def close(self):
  151. """Close this file, saving the lockfile over the original.
  152. Note: If this method fails, it will attempt to delete the lockfile.
  153. However, it is not guaranteed to do so (e.g. if a filesystem
  154. becomes suddenly read-only), which will prevent future writes to
  155. this file until the lockfile is removed manually.
  156. Raises:
  157. OSError: if the original file could not be overwritten. The
  158. lock file is still closed, so further attempts to write to the same
  159. file object will raise ValueError.
  160. """
  161. if self._closed:
  162. return
  163. os.fsync(self._file.fileno())
  164. self._file.close()
  165. try:
  166. if getattr(os, "replace", None) is not None:
  167. os.replace(self._lockfilename, self._filename)
  168. else:
  169. if sys.platform != "win32":
  170. os.rename(self._lockfilename, self._filename)
  171. else:
  172. # Windows versions prior to Vista don't support atomic
  173. # renames
  174. _fancy_rename(self._lockfilename, self._filename)
  175. finally:
  176. self.abort()
  177. def __enter__(self):
  178. return self
  179. def __exit__(self, exc_type, exc_val, exc_tb):
  180. self.close()
  181. def __getattr__(self, name):
  182. """Proxy property calls to the underlying file."""
  183. if name in self.PROXY_PROPERTIES:
  184. return getattr(self._file, name)
  185. raise AttributeError(name)