file.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # file.py -- Safe access to git files
  2. # Copyright (C) 2010 Google, Inc.
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # of the License or (at your option) a later version of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. """Safe access to git files."""
  19. import errno
  20. import io
  21. import os
  22. import sys
  23. import tempfile
  24. def ensure_dir_exists(dirname):
  25. """Ensure a directory exists, creating if necessary."""
  26. try:
  27. os.makedirs(dirname)
  28. except OSError as e:
  29. if e.errno != errno.EEXIST:
  30. raise
  31. def _fancy_rename(oldname, newname):
  32. """Rename file with temporary backup file to rollback if rename fails"""
  33. if not os.path.exists(newname):
  34. try:
  35. os.rename(oldname, newname)
  36. except OSError:
  37. raise
  38. return
  39. # destination file exists
  40. try:
  41. (fd, tmpfile) = tempfile.mkstemp(".tmp", prefix=oldname+".", dir=".")
  42. os.close(fd)
  43. os.remove(tmpfile)
  44. except OSError:
  45. # either file could not be created (e.g. permission problem)
  46. # or could not be deleted (e.g. rude virus scanner)
  47. raise
  48. try:
  49. os.rename(newname, tmpfile)
  50. except OSError:
  51. raise # no rename occurred
  52. try:
  53. os.rename(oldname, newname)
  54. except OSError:
  55. os.rename(tmpfile, newname)
  56. raise
  57. os.remove(tmpfile)
  58. def GitFile(filename, mode='rb', bufsize=-1):
  59. """Create a file object that obeys the git file locking protocol.
  60. :return: a builtin file object or a _GitFile object
  61. :note: See _GitFile for a description of the file locking protocol.
  62. Only read-only and write-only (binary) modes are supported; r+, w+, and a
  63. are not. To read and write from the same file, you can take advantage of
  64. the fact that opening a file for write does not actually open the file you
  65. request.
  66. """
  67. if 'a' in mode:
  68. raise IOError('append mode not supported for Git files')
  69. if '+' in mode:
  70. raise IOError('read/write mode not supported for Git files')
  71. if 'b' not in mode:
  72. raise IOError('text mode not supported for Git files')
  73. if 'w' in mode:
  74. return _GitFile(filename, mode, bufsize)
  75. else:
  76. return io.open(filename, mode, bufsize)
  77. class _GitFile(object):
  78. """File that follows the git locking protocol for writes.
  79. All writes to a file foo will be written into foo.lock in the same
  80. directory, and the lockfile will be renamed to overwrite the original file
  81. on close.
  82. :note: You *must* call close() or abort() on a _GitFile for the lock to be
  83. released. Typically this will happen in a finally block.
  84. """
  85. PROXY_PROPERTIES = set(['closed', 'encoding', 'errors', 'mode', 'name',
  86. 'newlines', 'softspace'])
  87. PROXY_METHODS = ('__iter__', 'flush', 'fileno', 'isatty', 'read',
  88. 'readline', 'readlines', 'seek', 'tell',
  89. 'truncate', 'write', 'writelines')
  90. def __init__(self, filename, mode, bufsize):
  91. self._filename = filename
  92. self._lockfilename = '%s.lock' % self._filename
  93. fd = os.open(self._lockfilename,
  94. os.O_RDWR | os.O_CREAT | os.O_EXCL | getattr(os, "O_BINARY", 0))
  95. self._file = os.fdopen(fd, mode, bufsize)
  96. self._closed = False
  97. for method in self.PROXY_METHODS:
  98. setattr(self, method, getattr(self._file, method))
  99. def abort(self):
  100. """Close and discard the lockfile without overwriting the target.
  101. If the file is already closed, this is a no-op.
  102. """
  103. if self._closed:
  104. return
  105. self._file.close()
  106. try:
  107. os.remove(self._lockfilename)
  108. self._closed = True
  109. except OSError as e:
  110. # The file may have been removed already, which is ok.
  111. if e.errno != errno.ENOENT:
  112. raise
  113. self._closed = True
  114. def close(self):
  115. """Close this file, saving the lockfile over the original.
  116. :note: If this method fails, it will attempt to delete the lockfile.
  117. However, it is not guaranteed to do so (e.g. if a filesystem becomes
  118. suddenly read-only), which will prevent future writes to this file
  119. until the lockfile is removed manually.
  120. :raises OSError: if the original file could not be overwritten. The lock
  121. file is still closed, so further attempts to write to the same file
  122. object will raise ValueError.
  123. """
  124. if self._closed:
  125. return
  126. self._file.close()
  127. try:
  128. try:
  129. os.rename(self._lockfilename, self._filename)
  130. except OSError as e:
  131. if sys.platform == 'win32' and e.errno == errno.EEXIST:
  132. # Windows versions prior to Vista don't support atomic renames
  133. _fancy_rename(self._lockfilename, self._filename)
  134. else:
  135. raise
  136. finally:
  137. self.abort()
  138. def __enter__(self):
  139. return self
  140. def __exit__(self, exc_type, exc_val, exc_tb):
  141. self.close()
  142. def __getattr__(self, name):
  143. """Proxy property calls to the underlying file."""
  144. if name in self.PROXY_PROPERTIES:
  145. return getattr(self._file, name)
  146. raise AttributeError(name)