From cce63aca270c51fb3ce9790438e3c23864de0a87 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Wed, 23 Dec 2015 21:43:51 +0100 Subject: Implement file change tracking using `diff` The newly introduced `ChangeTracker` class is now keeping track of all tracked file in addition to spawning and managing a corresponding `diff` instance that enables printing pretty _patch-style_ change summaries to the logging target. This commit introduces `boost-process` and `diff` as dependencies of this library. --- src/change_log.cc | 36 ++++++++++++------------------------ 1 file changed, 12 insertions(+), 24 deletions(-) (limited to 'src/change_log.cc') diff --git a/src/change_log.cc b/src/change_log.cc index a9e485f..e128383 100644 --- a/src/change_log.cc +++ b/src/change_log.cc @@ -4,20 +4,18 @@ #include #include -#include #include "io.h" #include "utility.h" #include "actual_function.h" +#include "change_tracker.h" -static std::unique_ptr> tracked_files; -static std::unique_ptr fd_guard; -static std::unique_ptr logger; +static std::unique_ptr fd_guard; +static std::unique_ptr logger; +static std::unique_ptr tracker; 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") @@ -26,6 +24,8 @@ void init() { } else { logger = std::make_unique(STDERR_FILENO); } + + tracker = std::make_unique(logger.get()); } void exit(int status) { @@ -39,22 +39,12 @@ void exit(int status) { actual_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) { 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); - - logger->append("wrote to '" + file_name + "'"); + if ( !tracker->is_tracked(file_name) ) { + tracker->track(file_name); } } @@ -62,8 +52,8 @@ ssize_t write(int fd, const void* buffer, size_t count) { } int rename(const char* old_path, const char* new_path) { - if ( !is_tracked_file(old_path) ) { - track_file(old_path); + if ( !tracker->is_tracked(old_path) ) { + tracker->track(old_path); } logger->append("renamed '" + std::string(old_path) + "' to '" + std::string(new_path) + "'"); @@ -99,10 +89,8 @@ void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset) if ( ( prot & PROT_WRITE ) && 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); - - logger->append("mmap '" + file_name + "'"); + if ( !tracker->is_tracked(file_name) ) { + tracker->track(file_name); } } -- cgit v1.2.3