From 3681247dc1e38c75012e77c93cc72346f1a87f82 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Sun, 25 Dec 2016 19:58:52 +0100 Subject: [PATCH] refactor(file_util): Move block mode helpers --- include/utils/file.hpp | 2 -- include/utils/io.hpp | 3 +++ src/utils/file.cpp | 26 +------------------------- src/utils/io.cpp | 17 ++++++++++++++++- 4 files changed, 20 insertions(+), 28 deletions(-) diff --git a/include/utils/file.hpp b/include/utils/file.hpp index 6bd695ac..bfe30de4 100644 --- a/include/utils/file.hpp +++ b/include/utils/file.hpp @@ -37,8 +37,6 @@ class file_descriptor { namespace file_util { bool exists(const string& filename); string get_contents(const string& filename); - void set_block(int fd); - void set_nonblock(int fd); bool is_fifo(string filename); template diff --git a/include/utils/io.hpp b/include/utils/io.hpp index 220966b9..ead53f71 100644 --- a/include/utils/io.hpp +++ b/include/utils/io.hpp @@ -21,6 +21,9 @@ namespace io_util { bool poll_write(int fd, int timeout_ms = 1); bool interrupt_read(int write_fd); + + void set_block(int fd); + void set_nonblock(int fd); } POLYBAR_NS_END diff --git a/src/utils/file.cpp b/src/utils/file.cpp index 17307763..99549a21 100644 --- a/src/utils/file.cpp +++ b/src/utils/file.cpp @@ -1,12 +1,10 @@ #include -#include #include +#include #include #include "errors.hpp" -#include "utils/factory.hpp" #include "utils/file.hpp" -#include "utils/scope.hpp" POLYBAR_NS @@ -92,28 +90,6 @@ namespace file_util { } } - /** - * Puts the given file descriptor into blocking mode - */ - void set_block(int fd) { - int flags = fcntl(fd, F_GETFL, 0); - flags &= ~O_NONBLOCK; - if (fcntl(fd, F_SETFL, flags) == -1) { - throw system_error("Failed to unset O_NONBLOCK"); - } - } - - /** - * Puts the given file descriptor into non-blocking mode - */ - void set_nonblock(int fd) { - int flags = fcntl(fd, F_GETFL, 0); - flags |= O_NONBLOCK; - if (fcntl(fd, F_SETFL, flags) == -1) { - throw system_error("Failed to set O_NONBLOCK"); - } - } - /** * Checks if the given file is a named pipe */ diff --git a/src/utils/io.cpp b/src/utils/io.cpp index 654103c7..c1483586 100644 --- a/src/utils/io.cpp +++ b/src/utils/io.cpp @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -119,6 +118,22 @@ namespace io_util { size_t bytes = ::write(write_fd, end, 1); return bytes > 0; } + + void set_block(int fd) { + int flags = fcntl(fd, F_GETFL, 0); + flags &= ~O_NONBLOCK; + if (fcntl(fd, F_SETFL, flags) == -1) { + throw system_error("Failed to unset O_NONBLOCK"); + } + } + + void set_nonblock(int fd) { + int flags = fcntl(fd, F_GETFL, 0); + flags |= O_NONBLOCK; + if (fcntl(fd, F_SETFL, flags) == -1) { + throw system_error("Failed to set O_NONBLOCK"); + } + } } POLYBAR_NS_END