Mercurial > hg-stable
changeset 26375:3686fa2b8eee
windows: insert file positioning call between reads and writes
fopen() and fdopen() have a unique-to-Windows requirement that
transitions between read and write operations in files opened
in modes r+, w+, and a+ perform a file positioning call
(fsetpos, fseek, or rewind) in between. While the MSDN docs don't
say what will happen if this is not done, observations reveal
that Python raises an IOError with errno 0. Furthermore, I
/think/ this behavior isn't deterministic. But I can reproduce
it reliably with subsequent patches applied that open revlogs
in a+ mode and perform both reads and writes.
This patch introduces a proxy class for file handles opened
in r+, w+, and a+ mode on Windows. The class intercepts calls
and audits whether a file positioning function has been called
between read and write operations. If not, a dummy, no-op seek
to the current file position is performed. This appears to be
sufficient to "trick" Windows into allowing transitions between
read and writes without raising errors.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Sun, 27 Sep 2015 18:46:53 -0700 |
parents | 048e41e9a6ac |
children | 344a1621674b |
files | mercurial/windows.py |
diffstat | 1 files changed, 71 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/windows.py Sat Sep 26 15:20:32 2015 +0900 +++ b/mercurial/windows.py Sun Sep 27 18:46:53 2015 -0700 @@ -27,6 +27,74 @@ umask = 0o022 +class mixedfilemodewrapper(object): + """Wraps a file handle when it is opened in read/write mode. + + fopen() and fdopen() on Windows have a specific-to-Windows requirement + that files opened with mode r+, w+, or a+ make a call to a file positioning + function when switching between reads and writes. Without this extra call, + Python will raise a not very intuitive "IOError: [Errno 0] Error." + + This class wraps posixfile instances when the file is opened in read/write + mode and automatically adds checks or inserts appropriate file positioning + calls when necessary. + """ + OPNONE = 0 + OPREAD = 1 + OPWRITE = 2 + + def __init__(self, fp): + object.__setattr__(self, '_fp', fp) + object.__setattr__(self, '_lastop', 0) + + def __getattr__(self, name): + return getattr(self._fp, name) + + def __setattr__(self, name, value): + return self._fp.__setattr__(name, value) + + def _noopseek(self): + self._fp.seek(0, os.SEEK_CUR) + + def seek(self, *args, **kwargs): + object.__setattr__(self, '_lastop', self.OPNONE) + return self._fp.seek(*args, **kwargs) + + def write(self, d): + if self._lastop == self.OPREAD: + self._noopseek() + + object.__setattr__(self, '_lastop', self.OPWRITE) + return self._fp.write(d) + + def writelines(self, *args, **kwargs): + if self._lastop == self.OPREAD: + self._noopeseek() + + object.__setattr__(self, '_lastop', self.OPWRITE) + return self._fp.writelines(*args, **kwargs) + + def read(self, *args, **kwargs): + if self._lastop == self.OPWRITE: + self._noopseek() + + object.__setattr__(self, '_lastop', self.OPREAD) + return self._fp.read(*args, **kwargs) + + def readline(self, *args, **kwargs): + if self._lastop == self.OPWRITE: + self._noopseek() + + object.__setattr__(self, '_lastop', self.OPREAD) + return self._fp.readline(*args, **kwargs) + + def readlines(self, *args, **kwargs): + if self._lastop == self.OPWRITE: + self._noopseek() + + object.__setattr__(self, '_lastop', self.OPREAD) + return self._fp.readlines(*args, **kwargs) + def posixfile(name, mode='r', buffering=-1): '''Open a file with even more POSIX-like semantics''' try: @@ -37,6 +105,9 @@ if 'a' in mode: fp.seek(0, os.SEEK_END) + if '+' in mode: + return mixedfilemodewrapper(fp) + return fp except WindowsError as err: # convert to a friendlier exception