From 68b69dfc92daffbe52b4eb7eb317beb2cc764fb0 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sun, 18 Oct 2015 17:35:11 +0200 Subject: Add cmake build instructions --- src/change_log.cc | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/io.h | 61 ++++++++++++++++++++++++++++++++ src/utility.h | 28 +++++++++++++++ 3 files changed, 193 insertions(+) create mode 100644 src/change_log.cc create mode 100644 src/io.h create mode 100644 src/utility.h (limited to 'src') diff --git a/src/change_log.cc b/src/change_log.cc new file mode 100644 index 0000000..5ca0e8e --- /dev/null +++ b/src/change_log.cc @@ -0,0 +1,104 @@ +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include +#include +#include + +#include "io.h" +#include "utility.h" + +static std::unique_ptr> tracked_files; +static std::unique_ptr fd_guard; +static std::unique_ptr log; + +template +std::function get_real_function( + const std::string& symbol_name) { + + Result (*real_function)(Arguments...) = nullptr; + const void* symbol_address = dlsym(RTLD_NEXT, symbol_name.c_str()); + + std::memcpy(&real_function, &symbol_address, sizeof(symbol_address)); + + return std::function(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); + } else { + log = std::make_unique(STDERR_FILENO); + } +} + +void exit(int status) { + get_real_function("exit")(status); +} + +bool is_tracked_file(const std::string& file_name) { + return tracked_files->find(file_name) != tracked_files->end(); +} + +bool track_file(const std::string& file_name) { + 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 ( 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); + + log->append("wrote to '" + file_name + "'"); + } + } + + return real_write(fd, buffer, count); +} + +int rename(const char* old_path, const char* new_path) { + static auto real_rename = get_real_function("rename"); + + log->append("renamed '" + std::string(old_path) + "' to '" + std::string(new_path) + "'"); + + return real_rename(old_path, new_path); +} + +int rmdir(const char* path) { + static auto real_rmdir = get_real_function("rmdir"); + + log->append("removed directory '" + std::string(path) + "'"); + + return real_rmdir(path); +} + +int unlink(const char* path) { + static auto real_unlink = get_real_function("unlink"); + + log->append("removed '" + std::string(path) + "'"); + + return real_unlink(path); +} + +int unlinkat(int dirfd, const char* path, int flags) { + static auto real_unlinkat = get_real_function("unlinkat"); + + if ( dirfd == AT_FDCWD ) { + log->append("removed '" + std::string(path) + "'"); + } else { + log->append("removed '" + io::get_file_name(dirfd) + path + "'"); + } + + return real_unlinkat(dirfd, path, flags); +} diff --git a/src/io.h b/src/io.h new file mode 100644 index 0000000..87778ec --- /dev/null +++ b/src/io.h @@ -0,0 +1,61 @@ +#ifndef CHANGE_IO_H_ +#define CHANGE_IO_H_ + +#include +#include +#include +#include + +#include + +namespace io { + +class FileDescriptorGuard { + public: + 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); + } + + operator int() { + return this->fd; + } + + private: + int fd; + +}; + +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); +} + +} + +#endif // CHANGE_IO_H_ diff --git a/src/utility.h b/src/utility.h new file mode 100644 index 0000000..27ce8e9 --- /dev/null +++ b/src/utility.h @@ -0,0 +1,28 @@ +#ifndef CHANGE_UTILITY_H_ +#define CHANGE_UTILITY_H_ + +#include + +#include + +namespace utility { + +class Logger { + public: + Logger(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; + +}; + +} + +#endif // CHANGE_UTILITY_H_ -- cgit v1.2.3