diff options
author | Adrian Kummerlaender | 2015-10-17 00:02:42 +0200 |
---|---|---|
committer | Adrian Kummerlaender | 2015-10-17 00:02:42 +0200 |
commit | bc38a776d5b12beb84e7b8d3a852f9515e0b8330 (patch) | |
tree | da2049569f5918958f14bbad9b600c964f56d88e | |
parent | 5c7c4edfe45be83211d5c6217824dd4b9ec562d2 (diff) | |
download | change-bc38a776d5b12beb84e7b8d3a852f9515e0b8330.tar change-bc38a776d5b12beb84e7b8d3a852f9515e0b8330.tar.gz change-bc38a776d5b12beb84e7b8d3a852f9515e0b8330.tar.bz2 change-bc38a776d5b12beb84e7b8d3a852f9515e0b8330.tar.lz change-bc38a776d5b12beb84e7b8d3a852f9515e0b8330.tar.xz change-bc38a776d5b12beb84e7b8d3a852f9515e0b8330.tar.zst change-bc38a776d5b12beb84e7b8d3a852f9515e0b8330.zip |
Implement basic logging of write, rename, rmdir, unlink
-rw-r--r-- | change_log.cc | 75 |
1 files 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 <unistd.h> #include <dlfcn.h> +#include <fcntl.h> #include <sys/stat.h> #include <cstring> #include <iostream> +#include <utility> #include <functional> +#include <unordered_set> + +static std::unordered_set<std::string> tracked_files; template <class Result, typename... Arguments> std::function<Result(Arguments...)> get_real_function( @@ -22,6 +27,10 @@ std::function<Result(Arguments...)> get_real_function( return std::function<Result(Arguments...)>(real_function); } +void exit(int status) { + get_real_function<void, int>("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<ssize_t, int, void*, size_t>("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<ssize_t, int, const void*, size_t>("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<int, const char*, const char*>("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<int, const char*>("rmdir"); + + std::cerr << "removed directory '" << path << "'" << std::endl; + + return real_rmdir(path); +} + +int unlink(const char* path) { + static auto real_unlink = get_real_function<int, const char*>("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<int, int, const char*, int>("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); +} |