diff options
author | Adrian Kummerlaender | 2015-10-18 17:35:11 +0200 |
---|---|---|
committer | Adrian Kummerlaender | 2015-10-18 17:35:11 +0200 |
commit | 68b69dfc92daffbe52b4eb7eb317beb2cc764fb0 (patch) | |
tree | 1748b69328c78c80fbf327911542bec6d75f73f2 /src | |
parent | 6a66bd864b9b95258a198e65849fa693f88032aa (diff) | |
download | change-68b69dfc92daffbe52b4eb7eb317beb2cc764fb0.tar change-68b69dfc92daffbe52b4eb7eb317beb2cc764fb0.tar.gz change-68b69dfc92daffbe52b4eb7eb317beb2cc764fb0.tar.bz2 change-68b69dfc92daffbe52b4eb7eb317beb2cc764fb0.tar.lz change-68b69dfc92daffbe52b4eb7eb317beb2cc764fb0.tar.xz change-68b69dfc92daffbe52b4eb7eb317beb2cc764fb0.tar.zst change-68b69dfc92daffbe52b4eb7eb317beb2cc764fb0.zip |
Add cmake build instructions
Diffstat (limited to 'src')
-rw-r--r-- | src/change_log.cc | 104 | ||||
-rw-r--r-- | src/io.h | 61 | ||||
-rw-r--r-- | src/utility.h | 28 |
3 files changed, 193 insertions, 0 deletions
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 <memory> +#include <functional> +#include <unordered_set> + +#include "io.h" +#include "utility.h" + +static std::unique_ptr<std::unordered_set<std::string>> tracked_files; +static std::unique_ptr<io::FileDescriptorGuard> fd_guard; +static std::unique_ptr<utility::Logger> log; + +template <class Result, typename... Arguments> +std::function<Result(Arguments...)> 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<Result(Arguments...)>(real_function); +} + +void init() __attribute__ ((constructor)); +void init() { + tracked_files = std::make_unique<std::unordered_set<std::string>>(); + + if ( getenv("CHANGE_LOG_TARGET") != NULL ) { + fd_guard = std::make_unique<io::FileDescriptorGuard>( + getenv("CHANGE_LOG_TARGET") + ); + log = std::make_unique<utility::Logger>(*fd_guard); + } else { + log = std::make_unique<utility::Logger>(STDERR_FILENO); + } +} + +void exit(int status) { + get_real_function<void, int>("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<ssize_t, int, const void*, size_t>("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<int, const char*, const char*>("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<int, const char*>("rmdir"); + + log->append("removed directory '" + std::string(path) + "'"); + + return real_rmdir(path); +} + +int unlink(const char* path) { + static auto real_unlink = get_real_function<int, const char*>("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<int, int, const char*, int>("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 <unistd.h> +#include <dlfcn.h> +#include <fcntl.h> +#include <sys/stat.h> + +#include <cstring> + +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 <ext/stdio_filebuf.h> + +#include <iostream> + +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<char> buffer; + std::ostream stream; + +}; + +} + +#endif // CHANGE_UTILITY_H_ |