diff options
Diffstat (limited to 'src/change_log.cc')
-rw-r--r-- | src/change_log.cc | 36 |
1 files changed, 12 insertions, 24 deletions
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 <algorithm> #include <functional> -#include <unordered_set> #include "io.h" #include "utility.h" #include "actual_function.h" +#include "change_tracker.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> logger; +static std::unique_ptr<io::FileDescriptorGuard> fd_guard; +static std::unique_ptr<utility::Logger> logger; +static std::unique_ptr<utility::ChangeTracker> tracker; 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") @@ -26,6 +24,8 @@ void init() { } else { logger = std::make_unique<utility::Logger>(STDERR_FILENO); } + + tracker = std::make_unique<utility::ChangeTracker>(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); } } |