diff options
author | Dieter Verfaillie <dieterv@optionexplicit.be> | 2015-07-04 20:32:21 +0200 |
---|---|---|
committer | Dieter Verfaillie <dieterv@optionexplicit.be> | 2015-07-05 16:12:58 +0200 |
commit | 0aceeb3dd77f1ebd2234145d409962a665cc60d7 (patch) | |
tree | e4e744a0384b333037f372ce71667ec1632de77b | |
parent | d112e8ea4e155312b0b7607101784cad853147c6 (diff) | |
download | gobject-introspection-0aceeb3dd77f1ebd2234145d409962a665cc60d7.tar.gz |
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
-rw-r--r-- | giscanner/cachestore.py | 17 |
1 files 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 |