41inline void saveBinary(
const fs::path& path,
const void* data, std::size_t
size) {
42 const fs::path temporary = path.string() +
".partial";
43 if (fs::exists(path) || fs::exists(temporary))
44 throw std::runtime_error(
"Refusing to overwrite output: " + path.u8string());
47 std::ofstream
output(temporary, std::ios::binary);
48 output.write(
static_cast<const char*
>(data),
static_cast<std::streamsize
>(
size));
50 if (!
output)
throw std::runtime_error(
"Cannot write output: " + path.u8string());
52 fs::rename(temporary, path, ec);
53 if (ec)
throw std::runtime_error(
"Publish output failed: " + ec.message());
55 std::error_code
error;
56 fs::remove(temporary,
error);
57 if (
error) std::cerr <<
"WARNING: Cannot clean partial output: " <<
error.message() <<
'\n';
void saveBinary(const fs::path &path, const void *data, std::size_t size)
Write bytes to a new path and publish them by same-directory rename.