Catch OSError when renaming and try to remove the destination file first

Otherwise the rename fails on Windows if the destination file already
exists.

Change-Id: Id15cfaa5b9278d7e932a7975ab6e5a878b31a642
This commit is contained in:
Sebastian Schuberth 2016-06-09 11:34:18 +02:00
parent 6e4b760655
commit df1ab0f01a
1 changed files with 7 additions and 1 deletions

View File

@ -110,7 +110,13 @@ class CacheStorage(object):
tfile.flush()
self._os.fsync(tfile.fileno())
tfile.close()
self._os.rename(tfile.name, self.cachefilename)
try:
self._os.rename(tfile.name, self.cachefilename)
except OSError:
# On Windows, if dst already exists, OSError will be raised even if
# it is a file. Remove the file first in that case and try again.
self._os.remove(self.cachefilename)
self._os.rename(tfile.name, self.cachefilename)
self._logger.debug("Cache written out to '%s'" % self.cachefilename)