HardwareState: HWMon data reader
Ported HWMon driver from
cla-sysrepo@19163e50ab062a8b5b75f754b30c22fdbd62b03a.
In cla-sysrepo, these drivers provided a property-based API, i.e., you
asked for a specific property (i.e., a filename) and the contents of the
file was returned.
In velia, we do not need this one-by-one property access. We provide all
attributes with a single call.
Change-Id: Ife783f21d4552e3b8bc69436d2f8fa53eb6745b7
diff --git a/tests/fs-helpers/FileInjector.cpp b/tests/fs-helpers/FileInjector.cpp
new file mode 100644
index 0000000..a3a80f2
--- /dev/null
+++ b/tests/fs-helpers/FileInjector.cpp
@@ -0,0 +1,26 @@
+#include <fstream>
+#include "FileInjector.h"
+
+/** @short Creates a file with specific permissions and content */
+FileInjector::FileInjector(const std::filesystem::path& path, const std::filesystem::perms permissions, const std::string& content)
+ : path(path)
+{
+ auto fileStream = std::ofstream(path, std::ios_base::out | std::ios_base::trunc);
+ if (!fileStream.is_open()) {
+ throw std::invalid_argument("FileInjector could not open file " + std::string(path) + " for writing");
+ }
+ fileStream << content;
+ std::filesystem::permissions(path, permissions);
+}
+
+/** @short Removes file associated with this FileInjector instance (if exists) */
+FileInjector::~FileInjector() noexcept(false)
+{
+ std::filesystem::remove(path);
+}
+
+/** @short Sets file permissions */
+void FileInjector::setPermissions(const std::filesystem::perms permissions)
+{
+ std::filesystem::permissions(path, permissions);
+}