diff --git a/common/include/villas/utils.hpp b/common/include/villas/utils.hpp index a65fb6cba..6022ebd39 100644 --- a/common/include/villas/utils.hpp +++ b/common/include/villas/utils.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -211,6 +212,9 @@ template struct overloaded : Ts... { // explicit deduction guide (not needed as of C++20) template overloaded(Ts...) -> overloaded; +void write_to_file(std::string data, const std::filesystem::path file); +std::vector read_names_in_directory(const std::string &name); + namespace base64 { using byte = std::uint8_t; diff --git a/common/lib/CMakeLists.txt b/common/lib/CMakeLists.txt index 3aabe90ae..4c43567f0 100644 --- a/common/lib/CMakeLists.txt +++ b/common/lib/CMakeLists.txt @@ -43,7 +43,6 @@ if(CMAKE_SYSTEM_NAME STREQUAL Linux) kernel/devices/ip_device.cpp kernel/devices/pci_device.cpp kernel/devices/platform_device.cpp - kernel/devices/utils.cpp kernel/vfio_device.cpp kernel/vfio_group.cpp kernel/vfio_container.cpp diff --git a/common/lib/kernel/devices/generic_driver.cpp b/common/lib/kernel/devices/generic_driver.cpp index 9e0de6d98..a79ac23b3 100644 --- a/common/lib/kernel/devices/generic_driver.cpp +++ b/common/lib/kernel/devices/generic_driver.cpp @@ -9,10 +9,10 @@ #include #include -#include +#include using villas::kernel::devices::Device, villas::kernel::devices::GenericDriver; -using villas::kernel::devices::utils::write_to_file; +using villas::utils::write_to_file; void GenericDriver::attach(const Device &device) const { if (device.driver().has_value()) { diff --git a/common/lib/kernel/devices/platform_device.cpp b/common/lib/kernel/devices/platform_device.cpp index d8b128ad3..f0834887b 100644 --- a/common/lib/kernel/devices/platform_device.cpp +++ b/common/lib/kernel/devices/platform_device.cpp @@ -6,12 +6,13 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include #include +#include +#include using villas::kernel::devices::Driver, villas::kernel::devices::GenericDriver; using villas::kernel::devices::PlatformDevice; -using villas::kernel::devices::utils::write_to_file; +using villas::utils::write_to_file; std::optional> PlatformDevice::driver() const { std::filesystem::path driver_symlink = diff --git a/common/lib/kernel/devices/utils.cpp b/common/lib/kernel/devices/utils.cpp deleted file mode 100644 index 16fb5e387..000000000 --- a/common/lib/kernel/devices/utils.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* Utils - * - * Author: Pascal Bauer - * - * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include -#include -#include -#include -#include - -namespace utils = villas::kernel::devices::utils; - -void utils::write_to_file(std::string data, const std::filesystem::path file) { - villas::Log::get("Filewriter")->debug("{} > {}", data, file.u8string()); - std::ofstream outputFile(file.u8string()); - - if (outputFile.is_open()) { - // Write to file - outputFile << data; - outputFile.close(); - } else { - throw std::filesystem::filesystem_error("Cannot open outputfile", - std::error_code()); - } -} - -std::vector -utils::read_names_in_directory(const std::string &name) { - DIR *directory = opendir(name.c_str()); - - struct dirent *dp; - std::vector names; - dp = readdir(directory); - while (dp != NULL) { - auto name = std::string(dp->d_name); - if (name != "." && name != "..") { - names.push_back(name); - } - dp = readdir(directory); - } - closedir(directory); - return names; -} \ No newline at end of file diff --git a/common/lib/utils.cpp b/common/lib/utils.cpp index f495d8121..4735435f8 100644 --- a/common/lib/utils.cpp +++ b/common/lib/utils.cpp @@ -17,7 +17,11 @@ #include #include #include +#include #include +#include +#include +#include #include #include @@ -351,5 +355,36 @@ bool isPrivileged() { return true; } +void write_to_file(std::string data, const std::filesystem::path file) { + villas::Log::get("Filewriter")->debug("{} > {}", data, file.u8string()); + std::ofstream outputFile(file.u8string()); + + if (outputFile.is_open()) { + // Write to file + outputFile << data; + outputFile.close(); + } else { + throw std::filesystem::filesystem_error("Cannot open outputfile", + std::error_code()); + } +} + +std::vector read_names_in_directory(const std::string &name) { + DIR *directory = opendir(name.c_str()); + + struct dirent *dp; + std::vector names; + dp = readdir(directory); + while (dp != NULL) { + auto name = std::string(dp->d_name); + if (name != "." && name != "..") { + names.push_back(name); + } + dp = readdir(directory); + } + closedir(directory); + return names; +} + } // namespace utils } // namespace villas