From bc38a776d5b12beb84e7b8d3a852f9515e0b8330 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 17 Oct 2015 00:02:42 +0200 Subject: Implement basic logging of write, rename, rmdir, unlink --- change_log.cc | 75 +++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 18 deletions(-) diff --git a/change_log.cc b/change_log.cc index d4c3dc4..03c4343 100644 --- a/change_log.cc +++ b/change_log.cc @@ -4,11 +4,16 @@ #include #include +#include #include #include #include +#include #include +#include + +static std::unordered_set tracked_files; template std::function get_real_function( @@ -22,6 +27,10 @@ std::function get_real_function( return std::function(real_function); } +void exit(int status) { + get_real_function("exit")(status); +} + std::string get_file_name(int fd) { char proc_link[20]; char file_name[256]; @@ -45,32 +54,62 @@ bool is_regular_file(int fd) { return S_ISREG(fd_stat.st_mode); } -ssize_t read(int fd, void *buffer, size_t count) { - static auto real_read = get_real_function("read"); - - const ssize_t result = real_read(fd, buffer, count); - - if ( is_regular_file(fd) ) { - std::cerr << "read size " - << count - << " of " - << get_file_name(fd) - << std::endl; - } +bool is_tracked_file(const std::string& file_name) { + return tracked_files.find(file_name) != tracked_files.end(); +} - return result; +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 ( is_regular_file(fd) ) { - std::cerr << "write size " - << count - << " to " - << get_file_name(fd) - << std::endl; + const std::string file_name{ get_file_name(fd) }; + + if ( !is_tracked_file(file_name) ) { + track_file(file_name); + + std::cerr << "wrote to '" << file_name << "'" << std::endl; + } } return real_write(fd, buffer, count); } + +int rename(const char* old_path, const char* new_path) { + static auto real_rename = get_real_function("rename"); + + std::cerr << "renamed '" << old_path << "' to '" << new_path << "'" << std::endl; + + return real_rename(old_path, new_path); +} + +int rmdir(const char* path) { + static auto real_rmdir = get_real_function("rmdir"); + + std::cerr << "removed directory '" << path << "'" << std::endl; + + return real_rmdir(path); +} + +int unlink(const char* path) { + static auto real_unlink = get_real_function("unlink"); + + std::cerr << "removed '" << path << "'" << std::endl; + + 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 ) { + std::cerr << "removed '" << path << "'" << std::endl; + } else { + std::cerr << "removed '" << get_file_name(dirfd) << path << "'" << std::endl; + } + + return real_unlinkat(dirfd, path, flags); +} -- cgit v1.2.3