refactor(file_util): Move block mode helpers

This commit is contained in:
Michael Carlberg 2016-12-25 19:58:52 +01:00
parent 24aba14541
commit 3681247dc1
4 changed files with 20 additions and 28 deletions

View File

@ -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 <typename... Args>

View File

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

View File

@ -1,12 +1,10 @@
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <cstdio>
#include <fstream>
#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
*/

View File

@ -1,6 +1,5 @@
#include <fcntl.h>
#include <poll.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cstdio>
#include <cstdlib>
@ -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