From ceb01fb33a039d4f315c9aa9217320be44cefa2d Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sun, 18 Oct 2015 17:19:55 +0200 Subject: Move io and logging functionality into separate namespaces --- change_log.cc | 94 ++++++++++++----------------------------------------------- 1 file changed, 18 insertions(+), 76 deletions(-) (limited to 'change_log.cc') diff --git a/change_log.cc b/change_log.cc index 58ba0db..dc245da 100644 --- a/change_log.cc +++ b/change_log.cc @@ -2,55 +2,16 @@ #define _GNU_SOURCE #endif -#include -#include -#include -#include - -#include - -#include -#include -#include +#include #include #include -#include - -struct FileDescriptorGuard { - FileDescriptorGuard(const std::string& path) { - this->fd = open(path.c_str(), O_WRONLY | O_APPEND); - - if ( !this->fd ) { - this->fd = STDERR_FILENO; - } - } - - ~FileDescriptorGuard() { - close(this->fd); - } - - int fd; -}; - -class LogGuard { - public: - LogGuard(const int target_fd): - buffer(target_fd, std::ios::out), - stream(&this->buffer) { } - - void append(const std::string& msg) { - this->stream << msg << std::endl; - } - - private: - __gnu_cxx::stdio_filebuf buffer; - std::ostream stream; -}; -static std::unordered_set tracked_files; +#include "io.h" +#include "utility.h" -static std::unique_ptr fd_guard; -static std::unique_ptr log; +static std::unique_ptr> tracked_files; +static std::unique_ptr fd_guard; +static std::unique_ptr log; template std::function get_real_function( @@ -66,11 +27,15 @@ std::function get_real_function( void init() __attribute__ ((constructor)); void init() { + tracked_files = std::make_unique>(); + if ( getenv("CHANGE_LOG_TARGET") != NULL ) { - fd_guard = std::make_unique(getenv("CHANGE_LOG_TARGET")); - log = std::make_unique(fd_guard->fd); + fd_guard = std::make_unique( + getenv("CHANGE_LOG_TARGET") + ); + log = std::make_unique(fd_guard->fd); } else { - log = std::make_unique(STDERR_FILENO); + log = std::make_unique(STDERR_FILENO); } } @@ -78,42 +43,19 @@ void exit(int status) { get_real_function("exit")(status); } -std::string get_file_name(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_tracked_file(const std::string& file_name) { - return tracked_files.find(file_name) != tracked_files.end(); + return tracked_files->find(file_name) != tracked_files->end(); } bool track_file(const std::string& file_name) { - return tracked_files.emplace(file_name).second; + return tracked_files->emplace(file_name).second; } ssize_t write(int fd, const void* buffer, size_t count) { static auto real_write = get_real_function("write"); - if ( is_regular_file(fd) ) { - const std::string file_name{ get_file_name(fd) }; + if ( io::is_regular_file(fd) ) { + const std::string file_name{ io::get_file_name(fd) }; if ( !is_tracked_file(file_name) ) { track_file(file_name); @@ -155,7 +97,7 @@ int unlinkat(int dirfd, const char* path, int flags) { if ( dirfd == AT_FDCWD ) { log->append("removed '" + std::string(path) + "'"); } else { - log->append("removed '" + get_file_name(dirfd) + path + "'"); + log->append("removed '" + io::get_file_name(dirfd) + path + "'"); } return real_unlinkat(dirfd, path, flags); -- cgit v1.2.3