summaryrefslogtreecommitdiffstats
path: root/documentapi
diff options
context:
space:
mode:
authorTor Brede Vekterli <vekterli@oath.com>2018-04-06 13:28:33 +0000
committerTor Brede Vekterli <vekterli@oath.com>2018-04-06 13:28:33 +0000
commit91b873df42717710b48fd614a589e179d8bd4ab7 (patch)
tree06e2eabe4f79a8c60097ffa6cbebf58447a180b7 /documentapi
parent67e98ccb86f0fa4838a4b2112fd7c0c654972cab (diff)
Write to temp file and do atomic rename in C++ DocumentAPI tests
Should avoid racing with concurrent reads by Java DocumentAPI tests.
Diffstat (limited to 'documentapi')
-rw-r--r--documentapi/src/tests/messages/testbase.cpp10
1 files changed, 7 insertions, 3 deletions
diff --git a/documentapi/src/tests/messages/testbase.cpp b/documentapi/src/tests/messages/testbase.cpp
index 02c180853d4..ffa3bf67744 100644
--- a/documentapi/src/tests/messages/testbase.cpp
+++ b/documentapi/src/tests/messages/testbase.cpp
@@ -175,14 +175,18 @@ TestBase::dump(const mbus::Blob& blob) const
bool
TestBase::writeFile(const string &filename, const mbus::Blob& blob) const
{
- int file = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
+ std::string tmp_filename = filename + ".tmp";
+ int file = open(tmp_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (file == -1) {
return false;
}
- if (write(file, blob.data(), blob.size()) != (ssize_t)blob.size()) {
- throw vespalib::Exception("write failed");
+ if (write(file, blob.data(), blob.size()) != static_cast<ssize_t>(blob.size())) {
+ throw vespalib::Exception("write failed");
}
close(file);
+ if (rename(tmp_filename.c_str(), filename.c_str()) != 0) {
+ throw vespalib::Exception("rename failed");
+ }
return true;
}