2
0

file.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 os
  21. import tempfile
  22. def ensure_dir_exists(dirname):
  23. """Ensure a directory exists, creating if necessary."""
  24. try:
  25. os.makedirs(dirname)
  26. except OSError, e:
  27. if e.errno != errno.EEXIST:
  28. raise
  29. def fancy_rename(oldname, newname):
  30. """Rename file with temporary backup file to rollback if rename fails"""
  31. if not os.path.exists(newname):
  32. try:
  33. os.rename(oldname, newname)
  34. except OSError, e:
  35. raise
  36. return
  37. # destination file exists
  38. try:
  39. (fd, tmpfile) = tempfile.mkstemp(".tmp", prefix=oldname+".", dir=".")
  40. os.close(fd)
  41. os.remove(tmpfile)
  42. except OSError, e:
  43. # either file could not be created (e.g. permission problem)
  44. # or could not be deleted (e.g. rude virus scanner)
  45. raise
  46. try:
  47. os.rename(newname, tmpfile)
  48. except OSError, e:
  49. raise # no rename occurred
  50. try:
  51. os.rename(oldname, newname)
  52. except OSError, e:
  53. os.rename(tmpfile, newname)
  54. raise
  55. os.remove(tmpfile)
  56. def GitFile(filename, mode='rb', bufsize=-1):
  57. """Create a file object that obeys the git file locking protocol.
  58. :return: a builtin file object or a _GitFile object
  59. :note: See _GitFile for a description of the file locking protocol.
  60. Only read-only and write-only (binary) modes are supported; r+, w+, and a
  61. are not. To read and write from the same file, you can take advantage of
  62. the fact that opening a file for write does not actually open the file you
  63. request:
  64. >>> write_file = GitFile('filename', 'wb')
  65. >>> read_file = GitFile('filename', 'rb')
  66. >>> read_file.readlines()
  67. ['contents\n', 'of\n', 'the\n', 'file\n']
  68. >>> write_file.write('foo')
  69. >>> read_file.close()
  70. >>> write_file.close()
  71. >>> new_file = GitFile('filename', 'rb')
  72. 'foo'
  73. >>> new_file.close()
  74. >>> other_file = GitFile('filename', 'wb')
  75. Traceback (most recent call last):
  76. ...
  77. OSError: [Errno 17] File exists: 'filename.lock'
  78. """
  79. if 'a' in mode:
  80. raise IOError('append mode not supported for Git files')
  81. if '+' in mode:
  82. raise IOError('read/write mode not supported for Git files')
  83. if 'b' not in mode:
  84. raise IOError('text mode not supported for Git files')
  85. if 'w' in mode:
  86. return _GitFile(filename, mode, bufsize)
  87. else:
  88. return file(filename, mode, bufsize)
  89. class _GitFile(object):
  90. """File that follows the git locking protocol for writes.
  91. All writes to a file foo will be written into foo.lock in the same
  92. directory, and the lockfile will be renamed to overwrite the original file
  93. on close.
  94. :note: You *must* call close() or abort() on a _GitFile for the lock to be
  95. released. Typically this will happen in a finally block.
  96. """
  97. PROXY_PROPERTIES = set(['closed', 'encoding', 'errors', 'mode', 'name',
  98. 'newlines', 'softspace'])
  99. PROXY_METHODS = ('__iter__', 'flush', 'fileno', 'isatty', 'next', 'read',
  100. 'readline', 'readlines', 'xreadlines', 'seek', 'tell',
  101. 'truncate', 'write', 'writelines')
  102. def __init__(self, filename, mode, bufsize):
  103. self._filename = filename
  104. self._lockfilename = '%s.lock' % self._filename
  105. fd = os.open(self._lockfilename,
  106. os.O_RDWR | os.O_CREAT | os.O_EXCL | getattr(os, "O_BINARY", 0))
  107. self._file = os.fdopen(fd, mode, bufsize)
  108. self._closed = False
  109. for method in self.PROXY_METHODS:
  110. setattr(self, method, getattr(self._file, method))
  111. def abort(self):
  112. """Close and discard the lockfile without overwriting the target.
  113. If the file is already closed, this is a no-op.
  114. """
  115. if self._closed:
  116. return
  117. self._file.close()
  118. try:
  119. os.remove(self._lockfilename)
  120. self._closed = True
  121. except OSError, e:
  122. # The file may have been removed already, which is ok.
  123. if e.errno != errno.ENOENT:
  124. raise
  125. self._closed = True
  126. def close(self):
  127. """Close this file, saving the lockfile over the original.
  128. :note: If this method fails, it will attempt to delete the lockfile.
  129. However, it is not guaranteed to do so (e.g. if a filesystem becomes
  130. suddenly read-only), which will prevent future writes to this file
  131. until the lockfile is removed manually.
  132. :raises OSError: if the original file could not be overwritten. The lock
  133. file is still closed, so further attempts to write to the same file
  134. object will raise ValueError.
  135. """
  136. if self._closed:
  137. return
  138. self._file.close()
  139. try:
  140. try:
  141. os.rename(self._lockfilename, self._filename)
  142. except OSError, e:
  143. # Windows versions prior to Vista don't support atomic renames
  144. if e.errno != errno.EEXIST:
  145. raise
  146. fancy_rename(self._lockfilename, self._filename)
  147. finally:
  148. self.abort()
  149. def __getattr__(self, name):
  150. """Proxy property calls to the underlying file."""
  151. if name in self.PROXY_PROPERTIES:
  152. return getattr(self._file, name)
  153. raise AttributeError(name)