obu-firmware builds against vanetza-idf from microbu-esp32c5/external, but that tree was gitignored, so a clone of this repository could not build the firmware it ships. It is now committed here as ordinary files in its own folder, microbu-esp32c5/: the colleague's commit cf4b99f plus the V2X2MAP bridge's signature verification (--trust) used on the bench. Nothing is fetched from or pushed to the colleague's repository; this repository and its remotes carry everything. The folder's own .gitignore keeps build output, downloaded components and private key material out, as it did there; the committed file set is identical to that repository's tracked files. The ESP32-C5 is still flashed from obu-firmware/, which only takes vanetza-idf from microbu-esp32c5/, so the two stay separate folders. FLASHING.md says how to take a newer version of the colleague's tree (copy it over the folder, rebuild, test, commit).
63 lines
1.5 KiB
C++
63 lines
1.5 KiB
C++
#include "filesystem.hpp"
|
|
#include <vanetza/common/byte_buffer.hpp>
|
|
#include <fstream>
|
|
#include <stdexcept>
|
|
#include <system_error>
|
|
|
|
namespace vanetza
|
|
{
|
|
namespace pki
|
|
{
|
|
|
|
namespace
|
|
{
|
|
|
|
void write_file(const std::filesystem::path& path, const ByteBuffer& buffer)
|
|
{
|
|
std::ofstream ofs(path, std::ios::binary | std::ios::trunc);
|
|
if (!ofs) {
|
|
throw std::runtime_error("could not open " + path.string() + " for writing");
|
|
}
|
|
ofs.write(reinterpret_cast<const char*>(buffer.data()), buffer.size());
|
|
ofs.flush();
|
|
if (!ofs) {
|
|
throw std::runtime_error("could not write " + path.string());
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void write(const std::filesystem::path& path, const ByteBuffer& buffer)
|
|
{
|
|
// Write to temporary file first and atomically rename it later
|
|
std::filesystem::path tmp = path;
|
|
tmp += ".tmp";
|
|
|
|
try {
|
|
write_file(tmp, buffer);
|
|
std::filesystem::rename(tmp, path);
|
|
} catch (...) {
|
|
// clean up temporary file
|
|
std::error_code ec;
|
|
std::filesystem::remove(tmp, ec);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
ByteBuffer read(const std::filesystem::path& path)
|
|
{
|
|
std::ifstream ifs(path, std::ios::binary);
|
|
if (!ifs) {
|
|
throw std::runtime_error("could not open " + path.string() + " for reading");
|
|
}
|
|
|
|
ByteBuffer buffer { std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>() };
|
|
if (ifs.bad()) {
|
|
throw std::runtime_error("I/O error while reading " + path.string());
|
|
}
|
|
return buffer;
|
|
}
|
|
|
|
} // namespace pki
|
|
} // namespace vanetza
|