diff options
author | Tor Brede Vekterli <vekterli@oath.com> | 2018-04-06 13:28:33 +0000 |
---|---|---|
committer | Tor Brede Vekterli <vekterli@oath.com> | 2018-04-06 13:28:33 +0000 |
commit | 91b873df42717710b48fd614a589e179d8bd4ab7 (patch) | |
tree | 06e2eabe4f79a8c60097ffa6cbebf58447a180b7 /documentapi | |
parent | 67e98ccb86f0fa4838a4b2112fd7c0c654972cab (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.cpp | 10 |
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; } |