#ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include "actual_function.h" #include "utility/io.h" #include "utility/logger.h" #include "tracking/change_tracker.h" static std::unique_ptr fd_guard; static std::unique_ptr logger; static std::unique_ptr tracker; void track_write(const int fd) { if ( fd != *fd_guard && utility::is_regular_file(fd) ) { const std::string file_name{ utility::get_file_name(fd) }; if ( !tracker->is_tracked(file_name) ) { tracker->track(file_name); } } } void init() __attribute__ ((constructor)); void init() { if ( getenv("CHANGE_LOG_TARGET") != NULL ) { fd_guard = std::make_unique( getenv("CHANGE_LOG_TARGET") ); logger = std::make_unique(*fd_guard); } else { logger = std::make_unique(STDERR_FILENO); } if ( getenv("CHANGE_LOG_DIFF_CMD") != NULL ) { tracker = std::make_unique( logger.get(), getenv("CHANGE_LOG_DIFF_CMD") ); } else { tracker = std::make_unique(logger.get()); } } ssize_t write(int fd, const void* buffer, size_t count) { track_write(fd); return actual::write(fd, buffer, count); } ssize_t writev(int fd, const iovec* iov, int iovcnt) { track_write(fd); return actual::writev(fd, iov, iovcnt); } int rename(const char* old_path, const char* new_path) { if ( !tracker->is_tracked(old_path) ) { tracker->track(old_path); } logger->append("renamed '", old_path, "' to '", new_path, "'"); return actual::rename(old_path, new_path); } int rmdir(const char* path) { logger->append("removed directory '", path, "'"); return actual::rmdir(path); } int unlink(const char* path) { if ( utility::is_regular_file(path) ) { logger->append("remove '", path, "'"); } return actual::unlink(path); } int unlinkat(int dirfd, const char* path, int flags) { if ( dirfd == AT_FDCWD ) { logger->append("removed '", path, "'"); } else { logger->append("removed '", utility::get_file_name(dirfd), path, "'"); } return actual::unlinkat(dirfd, path, flags); } void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset) { if ( prot & PROT_WRITE ) { track_write(fd); } return actual::mmap(addr, length, prot, flags, fd, offset); }