1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

Refactor: move device utility functions to villas util

Signed-off-by: Pascal Bauer <pascal.bauer@rwth-aachen.de>
This commit is contained in:
Pascal Bauer 2024-09-23 17:12:16 +02:00 committed by Niklas Eiling
parent 2ffb635a4a
commit c8e9fc13ab
6 changed files with 20 additions and 59 deletions

View file

@ -12,6 +12,7 @@
#include <list>
#include <string>
#include <vector>
#include <filesystem>
#include <cassert>
#include <cstdint>
@ -213,8 +214,7 @@ template <class... Ts> struct overloaded : Ts... {
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
void write_to_file(std::string data, const std::filesystem::path file);
std::vector<std::string>
read_names_in_directory(const std::filesystem::path &directory);
std::vector<std::string> read_names_in_directory(const std::string &name);
namespace base64 {

View file

@ -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

View file

@ -9,10 +9,10 @@
#include <villas/kernel/devices/generic_driver.hpp>
#include <villas/kernel/devices/device.hpp>
#include <villas/kernel/devices/utils.hpp>
#include <villas/utils.hpp>
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()) {

View file

@ -6,12 +6,13 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include <villas/kernel/devices/platform_device.hpp>
#include <villas/kernel/devices/generic_driver.hpp>
#include <villas/kernel/devices/platform_device.hpp>
#include <villas/utils.hpp>
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<std::unique_ptr<Driver>> PlatformDevice::driver() const {
std::filesystem::path driver_symlink =

View file

@ -1,48 +0,0 @@
/* Utils
*
* Author: Pascal Bauer <pascal.bauer@rwth-aachen.de>
*
* SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <pascal.bauer@rwth-aachen.de>
* SPDX-License-Identifier: Apache-2.0
*/
#include <dirent.h>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <villas/kernel/devices/utils.hpp>
#include <villas/log.hpp>
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<std::string>
utils::read_names_in_directory(const std::string &name) {
DIR *directory = opendir(name.c_str());
struct dirent *dp;
std::vector<std::string> 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;
}

View file

@ -360,6 +360,7 @@ void write_to_file(std::string data, const std::filesystem::path file) {
std::ofstream outputFile(file.u8string());
if (outputFile.is_open()) {
// Write to file
outputFile << data;
outputFile.close();
} else {
@ -368,12 +369,20 @@ void write_to_file(std::string data, const std::filesystem::path file) {
}
}
std::vector<std::string>
read_names_in_directory(const std::filesystem::path &directory) {
std::vector<std::string> read_names_in_directory(const std::string &name) {
DIR *directory = opendir(name.c_str());
struct dirent *dp;
std::vector<std::string> names;
for (auto const &dir_entry : std::filesystem::directory_iterator{directory}) {
names.push_back(dir_entry.path().filename());
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;
}