ncsid: Guarantee MAC override

This allows us to runtime configure the mac address of the link while
still being able to have ncsid write an updated one.

Tested: On hardware and verified that the file gets written with the
MAC updated.

Change-Id: I1081ace0829f686e2e5bb34321433c5eb5b531dd
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/subprojects/ncsid/src/net_config.cpp b/subprojects/ncsid/src/net_config.cpp
index b3fc1f6..3b632c3 100644
--- a/subprojects/ncsid/src/net_config.cpp
+++ b/subprojects/ncsid/src/net_config.cpp
@@ -20,10 +20,13 @@
 #include <unistd.h>
 
 #include <sdbusplus/bus.hpp>
+#include <stdplus/fd/create.hpp>
+#include <stdplus/fd/ops.hpp>
 #include <stdplus/util/string.hpp>
 
 #include <cstdio>
 #include <cstring>
+#include <filesystem>
 #include <utility>
 #include <variant>
 
@@ -135,6 +138,28 @@
 
     try
     {
+        auto netdir = fmt::format("/run/systemd/network/00-bmc-{}.network.d",
+                                  iface_name_);
+        std::filesystem::create_directories(netdir);
+        auto netfile = fmt::format("{}/60-ncsi-mac.conf", netdir);
+        auto fd = stdplus::fd::open(
+            netfile,
+            stdplus::fd::OpenFlags(stdplus::fd::OpenAccess::WriteOnly)
+                .set(stdplus::fd::OpenFlag::Create),
+            0644);
+        auto contents = fmt::format("[Link]\nMACAddress={}\n",
+                                    std::get<std::string>(mac_value));
+        stdplus::fd::writeExact(fd, contents);
+    }
+    catch (const std::exception& ex)
+    {
+        fmt::print(stderr, "Failed to set MAC Addr `{}` writing file: {}\n",
+                   std::get<std::string>(mac_value), ex.what());
+        return -1;
+    }
+
+    try
+    {
         auto reply = bus.call(networkd_call);
     }
     catch (const sdbusplus::exception::SdBusError& ex)