sync() after write to avoid losing file data on power off.

This commit is contained in:
Robin Davies
2024-09-01 09:53:32 -04:00
parent abb8a1a53c
commit b8c4fafd3a
21 changed files with 102 additions and 34 deletions
+1
View File
@@ -41,6 +41,7 @@ message(STATUS "NMPIPEDAL CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
# Use the sdbus-c++ target in SDBusCpp namespace
add_library(PiPedalCommon STATIC
ofstream_synced.cpp include/ofstream_synced.hpp
ConfigSerializer.cpp include/ConfigSerializer.hpp
WifiRegs.cpp include/WifiRegs.hpp
WifiRegulations.cpp include/WifiRegulations.hpp
+2 -1
View File
@@ -30,6 +30,7 @@
#include <unistd.h>
#include <sys/stat.h>
#include <tuple>
#include "ofstream_synced.hpp"
using namespace pipedal;
@@ -65,7 +66,7 @@ void ServiceConfiguration::Save()
std::filesystem::create_directories(directory);
// make sure the file has correct permissions.
std::ofstream t;
pipedal::ofstream_synced t;
t.open(filename);
t.close();
+2 -1
View File
@@ -21,6 +21,7 @@
#include "WriteTemplateFile.hpp"
#include <fstream>
#include <stdexcept>
#include "ofstream_synced.hpp"
using namespace pipedal;
@@ -37,7 +38,7 @@ void pipedal::WriteTemplateFile(
const std::filesystem::path &inputFile,
const std::filesystem::path &outputFile)
{
std::ofstream out(outputFile);
pipedal::ofstream_synced out(outputFile);
std::ifstream in(inputFile);
if (!in.is_open())
@@ -30,6 +30,7 @@
#include "autoptr_vector.h"
#include <unordered_map>
#include <fstream>
#include "ofstream_synced.hpp"
namespace config_serializer
{
@@ -256,7 +257,7 @@ namespace config_serializer
}
void Save(const std::string &path)
{
std::ofstream f;
pipedal::ofstream_synced f;
f.open(path);
if (!f.is_open())
{
@@ -0,0 +1,26 @@
#pragma once
#include <fstream>
namespace pipedal
{
void FileSystemSync();
class ofstream_synced : public std::ofstream
{
public:
ofstream_synced() {}
explicit ofstream_synced(const std::string &filename, ios_base::openmode mode = ios_base::out)
: std::ofstream(filename, mode)
{
}
explicit ofstream_synced(const char *filename, ios_base::openmode mode = ios_base::out)
: std::ofstream(filename, mode)
{
}
~ofstream_synced();
};
}
+19
View File
@@ -0,0 +1,19 @@
#include "ofstream_synced.hpp"
#include "unistd.h"
using namespace pipedal;
void pipedal::FileSystemSync()
{
::sync();
}
ofstream_synced::~ofstream_synced()
{
if (is_open())
{
close();
::sync();
}
}