From 0aceeb3dd77f1ebd2234145d409962a665cc60d7 Mon Sep 17 00:00:00 2001 From: Dieter Verfaillie Date: Sat, 4 Jul 2015 20:32:21 +0200 Subject: scanner: fix cachestore race Using tempfile.mkstemp should prevent the temp file from being cleaned while it might still be used by another process. https://bugzilla.gnome.org/show_bug.cgi?id=751926 --- giscanner/cachestore.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/giscanner/cachestore.py b/giscanner/cachestore.py index d6120706..7b465d85 100644 --- a/giscanner/cachestore.py +++ b/giscanner/cachestore.py @@ -75,11 +75,16 @@ class CacheStore(object): if current_hash == cache_hash: return - versiontmp = version + '.tmp' - self._clean() + + tmp_fd, tmp_filename = tempfile.mkstemp(prefix='g-ir-scanner-cache-version-') try: - fp = open(versiontmp, 'w') + with os.fdopen(tmp_fd, 'w') as tmp_file: + tmp_file.write(current_hash) + + # On Unix, this would just be os.rename() but Windows + # doesn't allow that. + shutil.move(tmp_filename, version) except IOError as e: # Permission denied if e.errno == errno.EACCES: @@ -87,12 +92,6 @@ class CacheStore(object): else: raise - fp.write(current_hash) - fp.close() - # On Unix, this would just be os.rename() but Windows - # doesn't allow that. - shutil.move(versiontmp, version) - def _get_filename(self, filename): # If we couldn't create the directory we're probably # on a read only home directory where we just disable -- cgit v1.2.1