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:
parent
6e7c9c61d9
commit
80d794223c
6 changed files with 44 additions and 53 deletions
|
@ -11,6 +11,7 @@
|
|||
#include <list>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
|
@ -211,6 +212,9 @@ template <class... Ts> struct overloaded : Ts... {
|
|||
// explicit deduction guide (not needed as of C++20)
|
||||
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::string &name);
|
||||
|
||||
namespace base64 {
|
||||
|
||||
using byte = std::uint8_t;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()) {
|
||||
|
|
|
@ -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 =
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -17,7 +17,11 @@
|
|||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -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<std::string> 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;
|
||||
}
|
||||
|
||||
} // namespace utils
|
||||
} // namespace villas
|
||||
|
|
Loading…
Add table
Reference in a new issue