41namespace fs = std::filesystem;
53[[nodiscard]]
inline bool poll_with_timeout(
int fd,
short events, std::chrono::milliseconds
const& timeout)
59 int poll_result = ::poll(&pfd, 1, timeout.count());
66 else if (poll_result == 0)
88template<
typename Data>
90 , std::chrono::milliseconds
const& timeout
91 , std::span<Data> buf)
93 using Element =
typename std::decay_t<
typename decltype(buf)::value_type>;
100 return ::read(fd, buf.data(), buf.size() *
sizeof(Element));
114template<
typename Data>
116 , std::chrono::milliseconds
const& timeout
117 , std::span<Data> buf)
119 using Element =
typename std::decay_t<
typename decltype(buf)::value_type>;
126 return ::write(fd, buf.data(), buf.size() *
sizeof(Element));
144 fs::path
const& path_file_src,
145 std::chrono::milliseconds timeout,
148 if(
struct stat st; ::stat(path_file_src.c_str(), &st) != 0)
153 else if (not S_ISFIFO(st.st_mode))
156 return ::open(path_file_src.c_str(), oflag);
161 for(
auto start = std::chrono::steady_clock::now();;)
163 if (
int fd = ::open(path_file_src.c_str(), oflag | O_NONBLOCK); fd >= 0)
166 if (!(oflag & O_NONBLOCK))
168 int flags = ::fcntl(fd, F_GETFL);
171 ::fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
177 if (errno == ENXIO and (oflag & O_WRONLY))
180 auto elapsed = std::chrono::steady_clock::now() - start;
181 if (elapsed >= timeout)
187 std::this_thread::sleep_for(std::chrono::milliseconds(10));
204template<
typename Data>
206 , std::chrono::milliseconds
const& timeout
207 , std::span<Data> buf)
210 return_if(fd < 0, fd);
225template<
typename Data>
227 , std::chrono::milliseconds
const& timeout
228 , std::span<Data> buf)
231 return_if(fd < 0, fd);
234 return bytes_written;
245 std::ifstream file_modules(
"/proc/modules");
246 return_if(not file_modules.is_open(), Error(
"E::Could not open modules file"));
249 while ( std::getline(file_modules, line) )
251 return_if(line.contains(str_name),
true);
Enhanced error handling framework built on std::expected.
Simplified macros for common control flow patterns with optional logging.
Linux-specific system operations.
int open_with_timeout(fs::path const &path_file_src, std::chrono::milliseconds timeout, int oflag)
Opens a given file with a timeout.
bool poll_with_timeout(int fd, short events, std::chrono::milliseconds const &timeout)
Waits for a file descriptor to be ready for I/O with a timeout.
ssize_t open_write_with_timeout(fs::path const &path_file_src, std::chrono::milliseconds const &timeout, std::span< Data > buf)
Opens and writes to the given input file.
Value< bool > module_check(std::string_view str_name)
Checks if the linux kernel has a module loaded that matches the input name.
ssize_t write_with_timeout(int fd, std::chrono::milliseconds const &timeout, std::span< Data > buf)
Writes to the file descriptor with a timeout.
ssize_t open_read_with_timeout(fs::path const &path_file_src, std::chrono::milliseconds const &timeout, std::span< Data > buf)
Opens and reads from the given input file.
ssize_t read_with_timeout(int fd, std::chrono::milliseconds const &timeout, std::span< Data > buf)
Reads from the file descriptor with a timeout.
Enhanced expected type with integrated logging capabilities.