meta-google: dhcp-done: Adding status report
Previously dhcp-done only sends status code, this one provides the
capability to send status code + status message for better
troubleshooting.
Provide a way to let other process upgrade the status.
Tested: Unit test passed.
Change-Id: I9c689f90502a32b586c41e3491ad47ebc78fcc38
Signed-off-by: Yuxiao Zhang <yuxiaozhang@google.com>
diff --git a/subprojects/dhcp-done/file-io.cpp b/subprojects/dhcp-done/file-io.cpp
new file mode 100644
index 0000000..321cc6d
--- /dev/null
+++ b/subprojects/dhcp-done/file-io.cpp
@@ -0,0 +1,39 @@
+#include "file-io.hpp"
+
+#include <fcntl.h>
+#include <sys/file.h>
+#include <unistd.h>
+
+#include <stdplus/fd/atomic.hpp>
+#include <stdplus/fd/create.hpp>
+#include <stdplus/fd/fmt.hpp>
+#include <stdplus/fd/managed.hpp>
+#include <stdplus/fd/ops.hpp>
+#include <stdplus/print.hpp>
+
+#include <cerrno>
+#include <fstream>
+#include <iostream>
+#include <string>
+
+using ::stdplus::fd::ManagedFd;
+using std::literals::string_view_literals::operator""sv;
+
+// Function to read contents from a file (with locking)
+std::string fileRead(const fs::path& filename)
+{
+ // Open the file in read mode
+ ManagedFd fd = stdplus::fd::open(std::string(filename).c_str(),
+ stdplus::fd::OpenAccess::ReadOnly);
+ return stdplus::fd::readAll<std::string>(fd);
+}
+
+// Function to write contents to a file atomically
+void fileWrite(const fs::path& filename, const std::string& data)
+{
+ stdplus::fd::AtomicWriter writer(filename, 0644);
+ stdplus::fd::FormatBuffer out(writer);
+ out.appends(data);
+ out.flush();
+ writer.commit();
+}