diff options
author | Matthias Maennich <maennich@google.com> | 2021-11-21 15:27:27 +0000 |
---|---|---|
committer | Matthias Maennich <maennich@google.com> | 2021-11-22 17:35:22 +0000 |
commit | beb9109a195483e8cdcd2d3f6acdea35e38d87c5 (patch) | |
tree | 32656689ea2ad477c9e53956525132b2ecd9f362 | |
parent | 39c66e24d1e52dc13a0384762622428c6a6aa286 (diff) | |
download | build-tools-beb9109a195483e8cdcd2d3f6acdea35e38d87c5.tar.gz |
interceptor: record root dir in log
That is required to reconstruct the full working dir again later.
The root dir is either deducted from the environment ($ROOT_DIR) or is
cwd() of the interceptor process otherwise.
Bug: 205731786
Signed-off-by: Matthias Maennich <maennich@google.com>
Change-Id: I12d826d7459a83864ecd024d85c801f1011aee87
-rw-r--r-- | interceptor/log.proto | 3 | ||||
-rw-r--r-- | interceptor/main.cc | 21 |
2 files changed, 16 insertions, 8 deletions
diff --git a/interceptor/log.proto b/interceptor/log.proto index fc9e31d..e339878 100644 --- a/interceptor/log.proto +++ b/interceptor/log.proto @@ -35,5 +35,6 @@ message Message { // The entirety of a the final log should be stored as just this Log. message Log { - repeated Command commands = 1; + string root_dir = 1; // ${ROOT_DIR} or cwd() if unset + repeated Command commands = 2; } diff --git a/interceptor/main.cc b/interceptor/main.cc index e9184e6..e46300c 100644 --- a/interceptor/main.cc +++ b/interceptor/main.cc @@ -87,20 +87,26 @@ static void setup_interceptor_library_path() { setenv("LD_PRELOAD", interceptor_library.c_str(), 1); } -static void setup_root_dir() { +static fs::path setup_root_dir() { const auto root_dir = getenv("ROOT_DIR"); + fs::path result; if (root_dir != nullptr) - setenv(ENV_root_dir, root_dir, 1); + result = root_dir; else - setenv(ENV_root_dir, fs::current_path().c_str(), 1); + result = fs::current_path(); + + setenv(ENV_root_dir, result.c_str(), 1); + + return result; } class CommandLog { const decltype(Options::command_log) command_log_file_; + const fs::path root_dir_; public: - CommandLog(decltype(command_log_file_) command_log_file) - : command_log_file_(std::move(command_log_file)) { + CommandLog(decltype(command_log_file_) command_log_file, const fs::path& root_dir) + : command_log_file_(std::move(command_log_file)), root_dir_(root_dir) { if (command_log_file_) { setenv(ENV_command_log, command_log_file_->c_str(), 1); std::ofstream command_log(command_log_file_->c_str(), std::ios_base::trunc); @@ -116,6 +122,7 @@ class CommandLog { // compact the log by re-reading the individual log::Message's to combine // them to a log::Log interceptor::log::Log log; + log.set_root_dir(root_dir_); { std::ifstream command_log(command_log_file_->c_str(), std::ios_base::binary); @@ -138,9 +145,9 @@ int main(int argc, char* argv[]) { const auto& options = parse_args(argc, argv); setup_interceptor_library_path(); - setup_root_dir(); + const auto root_dir = setup_root_dir(); - CommandLog command_log(options.command_log); + CommandLog command_log(options.command_log, root_dir); // TODO: cleanly to google::protobuf::ShutdownProtobufLibrary(); |