From 62b2be7fbedc13ed40ed8f1327dded532b90ad55 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 14 Nov 2015 23:32:35 +0100 Subject: Fix rename tracking and enable logfile creation --- src/change_log.cc | 27 +++++++++++++++++---------- src/io.h | 2 +- 2 files changed, 18 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/change_log.cc b/src/change_log.cc index 180804f..f07296e 100644 --- a/src/change_log.cc +++ b/src/change_log.cc @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -13,7 +14,7 @@ static std::unique_ptr> tracked_files; static std::unique_ptr fd_guard; -static std::unique_ptr log; +static std::unique_ptr logger; template std::function get_real_function( @@ -35,13 +36,15 @@ void init() { fd_guard = std::make_unique( getenv("CHANGE_LOG_TARGET") ); - log = std::make_unique(*fd_guard); + logger = std::make_unique(*fd_guard); } else { - log = std::make_unique(STDERR_FILENO); + logger = std::make_unique(STDERR_FILENO); } } void exit(int status) { + logger->append("exit"); + get_real_function("exit")(status); } @@ -62,7 +65,7 @@ ssize_t write(int fd, const void* buffer, size_t count) { if ( !is_tracked_file(file_name) ) { track_file(file_name); - log->append("wrote to '" + file_name + "'"); + logger->append("wrote to '" + file_name + "'"); } } @@ -72,7 +75,11 @@ ssize_t write(int fd, const void* buffer, size_t 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) + "'"); + if ( !is_tracked_file(old_path) ) { + track_file(old_path); + } + + logger->append("renamed '" + std::string(old_path) + "' to '" + std::string(new_path) + "'"); return real_rename(old_path, new_path); } @@ -80,7 +87,7 @@ int rename(const char* old_path, const char* new_path) { int rmdir(const char* path) { static auto real_rmdir = get_real_function("rmdir"); - log->append("removed directory '" + std::string(path) + "'"); + logger->append("removed directory '" + std::string(path) + "'"); return real_rmdir(path); } @@ -88,7 +95,7 @@ int rmdir(const char* path) { int unlink(const char* path) { static auto real_unlink = get_real_function("unlink"); - log->append("removed '" + std::string(path) + "'"); + logger->append("removed '" + std::string(path) + "'"); return real_unlink(path); } @@ -97,9 +104,9 @@ 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) + "'"); + logger->append("removed '" + std::string(path) + "'"); } else { - log->append("removed '" + io::get_file_name(dirfd) + path + "'"); + logger->append("removed '" + io::get_file_name(dirfd) + path + "'"); } return real_unlinkat(dirfd, path, flags); @@ -114,7 +121,7 @@ void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset) if ( !is_tracked_file(file_name) ) { track_file(file_name); - log->append("mmap '" + file_name + "'"); + logger->append("mmap '" + file_name + "'"); } } diff --git a/src/io.h b/src/io.h index 87778ec..c36a5b0 100644 --- a/src/io.h +++ b/src/io.h @@ -13,7 +13,7 @@ namespace io { class FileDescriptorGuard { public: FileDescriptorGuard(const std::string& path) { - this->fd = open(path.c_str(), O_WRONLY | O_APPEND); + this->fd = open(path.c_str(), O_CREAT | O_WRONLY | O_APPEND); if ( !this->fd ) { this->fd = STDERR_FILENO; -- cgit v1.2.3