Write to temp file and do atomic rename in C++ DocumentAPI tests
Should avoid racing with concurrent reads by Java DocumentAPI tests.
@@ -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()) {
+ 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;
}
