summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Van Berkom <tristan.van.berkom@gmail.com>2018-10-03 14:09:51 +0000
committerTristan Van Berkom <tristan.van.berkom@gmail.com>2018-10-03 14:09:51 +0000
commit3e5ff5a9667917d0200f4e9bbb3e61093fb69368 (patch)
tree479dea089dee42334fa7c75673d54c1bfe9ce673
parent0a1f8e3c41c0decc97a994b2b7da857a03585121 (diff)
parentd9020e43a26a3723427b94b2a73c25126ad10c77 (diff)
downloadbuildstream-3e5ff5a9667917d0200f4e9bbb3e61093fb69368.tar.gz
Merge branch 'tristan/fix-cache-size-race' into 'master'
fix cache size race See merge request BuildStream/buildstream!854
-rw-r--r--buildstream/_artifactcache/cascache.py2
-rw-r--r--buildstream/utils.py10
2 files changed, 8 insertions, 4 deletions
diff --git a/buildstream/_artifactcache/cascache.py b/buildstream/_artifactcache/cascache.py
index 3a3181bfb..3e63608be 100644
--- a/buildstream/_artifactcache/cascache.py
+++ b/buildstream/_artifactcache/cascache.py
@@ -506,7 +506,7 @@ class CASCache(ArtifactCache):
def set_ref(self, ref, tree):
refpath = self._refpath(ref)
os.makedirs(os.path.dirname(refpath), exist_ok=True)
- with utils.save_file_atomic(refpath, 'wb') as f:
+ with utils.save_file_atomic(refpath, 'wb', tempdir=self.tmpdir) as f:
f.write(tree.SerializeToString())
# resolve_ref():
diff --git a/buildstream/utils.py b/buildstream/utils.py
index 1e04a31ed..5c11a363c 100644
--- a/buildstream/utils.py
+++ b/buildstream/utils.py
@@ -502,7 +502,7 @@ def get_bst_version():
@contextmanager
def save_file_atomic(filename, mode='w', *, buffering=-1, encoding=None,
- errors=None, newline=None, closefd=True, opener=None):
+ errors=None, newline=None, closefd=True, opener=None, tempdir=None):
"""Save a file with a temporary name and rename it into place when ready.
This is a context manager which is meant for saving data to files.
@@ -529,8 +529,9 @@ def save_file_atomic(filename, mode='w', *, buffering=-1, encoding=None,
# https://bugs.python.org/issue8604
assert os.path.isabs(filename), "The utils.save_file_atomic() parameter ``filename`` must be an absolute path"
- dirname = os.path.dirname(filename)
- fd, tempname = tempfile.mkstemp(dir=dirname)
+ if tempdir is None:
+ tempdir = os.path.dirname(filename)
+ fd, tempname = tempfile.mkstemp(dir=tempdir)
os.close(fd)
f = open(tempname, mode=mode, buffering=buffering, encoding=encoding,
@@ -562,6 +563,9 @@ def save_file_atomic(filename, mode='w', *, buffering=-1, encoding=None,
#
# Get the disk usage of a given directory in bytes.
#
+# This function assumes that files do not inadvertantly
+# disappear while this function is running.
+#
# Arguments:
# (str) The path whose size to check.
#