|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/test/util/cmd_util.h" |
| 21 | + |
| 22 | +#include <unistd.h> |
| 23 | + |
| 24 | +#include <array> |
| 25 | +#include <cstring> |
| 26 | +#include <format> |
| 27 | +#include <iostream> |
| 28 | +#include <print> |
| 29 | +#include <stdexcept> |
| 30 | + |
| 31 | +#include <sys/wait.h> |
| 32 | + |
| 33 | +namespace iceberg { |
| 34 | + |
| 35 | +Command::Command(std::string program) : program_(std::move(program)) {} |
| 36 | + |
| 37 | +Command& Command::Arg(std::string a) { |
| 38 | + args_.push_back(std::move(a)); |
| 39 | + return *this; |
| 40 | +} |
| 41 | + |
| 42 | +Command& Command::Args(const std::vector<std::string>& as) { |
| 43 | + args_.insert(args_.end(), as.begin(), as.end()); |
| 44 | + return *this; |
| 45 | +} |
| 46 | + |
| 47 | +Command& Command::CurrentDir(const std::filesystem::path& path) { |
| 48 | + cwd_ = path; |
| 49 | + return *this; |
| 50 | +} |
| 51 | + |
| 52 | +Command& Command::Env(const std::string& key, const std::string& val) { |
| 53 | + env_vars_[key] = val; |
| 54 | + return *this; |
| 55 | +} |
| 56 | + |
| 57 | +void Command::RunCommand(const std::string& desc) const { |
| 58 | + std::println("[INFO] Starting to {}, command: {} {}", desc, program_, fmt_args()); |
| 59 | + |
| 60 | + std::cout.flush(); |
| 61 | + std::cerr.flush(); |
| 62 | + |
| 63 | + pid_t pid = fork(); |
| 64 | + |
| 65 | + if (pid == -1) { |
| 66 | + std::println(stderr, "[ERROR] Fork failed: {}", std::strerror(errno)); |
| 67 | + throw std::runtime_error(std::format("Fork failed: {}", std::strerror(errno))); |
| 68 | + } |
| 69 | + |
| 70 | + // --- Child Process --- |
| 71 | + if (pid == 0) { |
| 72 | + if (!cwd_.empty()) { |
| 73 | + std::error_code ec; |
| 74 | + std::filesystem::current_path(cwd_, ec); |
| 75 | + if (ec) { |
| 76 | + std::println(stderr, "Failed to change directory to '{}': {}", cwd_.string(), |
| 77 | + ec.message()); |
| 78 | + _exit(126); // Command invoked cannot execute |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + for (const auto& [k, v] : env_vars_) { |
| 83 | + setenv(k.c_str(), v.c_str(), 1); |
| 84 | + } |
| 85 | + |
| 86 | + std::vector<char*> argv; |
| 87 | + argv.reserve(args_.size() + 2); |
| 88 | + argv.push_back(const_cast<char*>(program_.c_str())); |
| 89 | + |
| 90 | + for (const auto& arg : args_) { |
| 91 | + argv.push_back(const_cast<char*>(arg.c_str())); |
| 92 | + } |
| 93 | + argv.push_back(nullptr); |
| 94 | + |
| 95 | + execvp(program_.c_str(), argv.data()); |
| 96 | + |
| 97 | + std::println(stderr, "execvp failed: {}", std::strerror(errno)); |
| 98 | + _exit(127); |
| 99 | + } |
| 100 | + |
| 101 | + // --- Parent Process --- |
| 102 | + int status = 0; |
| 103 | + if (waitpid(pid, &status, 0) == -1) { |
| 104 | + std::println(stderr, "[ERROR] waitpid failed: {}", std::strerror(errno)); |
| 105 | + throw std::runtime_error(std::format("waitpid failed: {}", std::strerror(errno))); |
| 106 | + } |
| 107 | + |
| 108 | + int exit_code = -1; |
| 109 | + if (WIFEXITED(status)) { |
| 110 | + exit_code = WEXITSTATUS(status); |
| 111 | + } else if (WIFSIGNALED(status)) { |
| 112 | + exit_code = 128 + WTERMSIG(status); |
| 113 | + } |
| 114 | + |
| 115 | + if (exit_code == 0) { |
| 116 | + std::println("[INFO] {} succeed!", desc); |
| 117 | + return; |
| 118 | + } else { |
| 119 | + std::println(stderr, "[ERROR] {} failed. Exit code: {}", desc, exit_code); |
| 120 | + throw std::runtime_error( |
| 121 | + std::format("{} failed with exit code: {}", desc, exit_code)); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +std::string Command::fmt_args() const { |
| 126 | + std::string s; |
| 127 | + for (const auto& a : args_) { |
| 128 | + s += a + " "; |
| 129 | + } |
| 130 | + return s; |
| 131 | +} |
| 132 | + |
| 133 | +} // namespace iceberg |
0 commit comments