summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Van Berkom <tristan.van.berkom@gmail.com>2018-10-03 13:44:31 +0000
committerTristan Van Berkom <tristan.van.berkom@gmail.com>2018-10-03 13:44:31 +0000
commiteb92e8e9f51b46d03455e9c67441d4dfec0a5795 (patch)
tree85e042865af5e341ca56eb1ca9185d49a0774032
parent013a8ad47f612b799e51e24181b122f04faf22ff (diff)
parentc02a1ae86bb2dacf0f2b9f1e9254516304b32731 (diff)
downloadbuildstream-eb92e8e9f51b46d03455e9c67441d4dfec0a5795.tar.gz
Merge branch 'tristan/fix-cache-size-race-1.2' into 'bst-1.2'
Tristan/fix cache size race 1.2 See merge request BuildStream/buildstream!855
-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 14932fba2..ffeccc0b3 100644
--- a/buildstream/_artifactcache/cascache.py
+++ b/buildstream/_artifactcache/cascache.py
@@ -418,7 +418,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 efecdc2e5..34c0f898b 100644
--- a/buildstream/utils.py
+++ b/buildstream/utils.py
@@ -496,7 +496,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.
@@ -523,8 +523,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,
@@ -556,6 +557,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.
#