diff options
author | Adrian Kummerlaender | 2016-02-17 22:06:36 +0100 |
---|---|---|
committer | Adrian Kummerlaender | 2016-02-17 22:06:36 +0100 |
commit | 50f2ef4ba33b540fa5a8e324996fa639500765f4 (patch) | |
tree | ceb8cbf88a6cd72b6977b3d97eb960694487de56 /src | |
parent | 3294bfe789ff354ca372f7c32275316bf4491ca5 (diff) | |
download | change-50f2ef4ba33b540fa5a8e324996fa639500765f4.tar change-50f2ef4ba33b540fa5a8e324996fa639500765f4.tar.gz change-50f2ef4ba33b540fa5a8e324996fa639500765f4.tar.bz2 change-50f2ef4ba33b540fa5a8e324996fa639500765f4.tar.lz change-50f2ef4ba33b540fa5a8e324996fa639500765f4.tar.xz change-50f2ef4ba33b540fa5a8e324996fa639500765f4.tar.zst change-50f2ef4ba33b540fa5a8e324996fa639500765f4.zip |
Move io utilities into separate compilation unit
Diffstat (limited to 'src')
-rw-r--r-- | src/utility/io.cc | 56 | ||||
-rw-r--r-- | src/utility/io.h | 55 |
2 files changed, 63 insertions, 48 deletions
diff --git a/src/utility/io.cc b/src/utility/io.cc new file mode 100644 index 0000000..3f54c6f --- /dev/null +++ b/src/utility/io.cc @@ -0,0 +1,56 @@ +#include "io.h" + +#include <unistd.h> +#include <dlfcn.h> +#include <fcntl.h> +#include <sys/stat.h> + +namespace utility { + +FileDescriptorGuard::FileDescriptorGuard(const std::string& path) { + this->fd_ = open(path.c_str(), O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); + + if ( !this->fd_ ) { + this->fd_ = STDERR_FILENO; + } +} + +FileDescriptorGuard::~FileDescriptorGuard() { + close(this->fd_); +} + +FileDescriptorGuard::operator int() { + return this->fd_; +} + +std::string get_file_path(int fd) { + char proc_link[20]; + char file_name[256]; + + snprintf(proc_link, sizeof(proc_link), "/proc/self/fd/%d", fd); + const ssize_t name_size = readlink(proc_link, file_name, sizeof(file_name)); + + if ( name_size > 0 ) { + file_name[name_size] = '\0'; + + return std::string(file_name); + } else { + return std::string(); + } +} + +bool is_regular_file(int fd) { + struct stat fd_stat; + fstat(fd, &fd_stat); + + return S_ISREG(fd_stat.st_mode); +} + +bool is_regular_file(const char* path) { + struct stat fd_stat; + lstat(path, &fd_stat); + + return S_ISREG(fd_stat.st_mode); +} + +} diff --git a/src/utility/io.h b/src/utility/io.h index 4c09b30..1015ddb 100644 --- a/src/utility/io.h +++ b/src/utility/io.h @@ -1,67 +1,26 @@ #ifndef CHANGE_SRC_UTILITY_IO_H_ #define CHANGE_SRC_UTILITY_IO_H_ -#include <unistd.h> -#include <dlfcn.h> -#include <fcntl.h> -#include <sys/stat.h> - -#include <cstring> +#include <string> namespace utility { class FileDescriptorGuard { public: - FileDescriptorGuard(const std::string& path) { - this->fd_ = open(path.c_str(), O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); - - if ( !this->fd_ ) { - this->fd_ = STDERR_FILENO; - } - } - - ~FileDescriptorGuard() { - close(this->fd_); - } + FileDescriptorGuard(const std::string& path); + ~FileDescriptorGuard(); - operator int() { - return this->fd_; - } + operator int(); private: int fd_; }; -std::string get_file_path(int fd) { - char proc_link[20]; - char file_name[256]; - - snprintf(proc_link, sizeof(proc_link), "/proc/self/fd/%d", fd); - const ssize_t name_size = readlink(proc_link, file_name, sizeof(file_name)); - - if ( name_size > 0 ) { - file_name[name_size] = '\0'; - - return std::string(file_name); - } else { - return std::string(); - } -} - -bool is_regular_file(int fd) { - struct stat fd_stat; - fstat(fd, &fd_stat); - - return S_ISREG(fd_stat.st_mode); -} - -bool is_regular_file(const char* path) { - struct stat fd_stat; - lstat(path, &fd_stat); +std::string get_file_path(int fd); - return S_ISREG(fd_stat.st_mode); -} +bool is_regular_file(int fd); +bool is_regular_file(const char* path); } |