diff options
-rw-r--r-- | interceptor.cc | 32 |
1 files changed, 9 insertions, 23 deletions
diff --git a/interceptor.cc b/interceptor.cc index 728161c..1f9f93d 100644 --- a/interceptor.cc +++ b/interceptor.cc @@ -126,28 +126,23 @@ static std::optional<std::string> command_getenv(const Command& command, const c static bool default_make_relative(Command* command) { // determine the ROOT_DIR - std::string root_directory; + fs::path root_directory; if (const auto root = command_getenv(*command, kEnvRootDirectory)) { - root_directory = *root; - if (root_directory[root_directory.size() - 1] != '/') { - root_directory += '/'; - } + root_directory = fs::path(*root) / ""; } else { + // there is no ROOT_DIR that we can use to make calls relative return false; } // determine the relative path to ROOT_DIR from the current working dir - std::string relative_root = fs::relative(root_directory); - if (relative_root[relative_root.size() - 1] != '/') { - relative_root += '/'; - } + auto relative_root = fs::relative(root_directory) / ""; if (relative_root == "./") { relative_root.clear(); } // TODO: This is generally bad as this means we can't make anything relative. // This happens if the out dir is outside of the root. - if (relative_root.find(root_directory) != std::string::npos) { + if (relative_root.native().find(root_directory) != std::string::npos) { return false; } @@ -155,9 +150,11 @@ static bool default_make_relative(Command* command) { // replacement functor const auto replace_all = [&](auto& str) { + size_t start = 0; auto pos = std::string::npos; - while ((pos = str.find(root_directory)) != std::string::npos) { - str.replace(pos, root_directory.length(), relative_root); + while ((pos = str.find(root_directory, start)) != std::string::npos) { + str.replace(pos, root_directory.native().length(), relative_root); + start = pos + relative_root.native().length(); } }; @@ -189,17 +186,6 @@ class Analyzer { void Analyzer::set_inputs_outputs(Command* command) const { auto [inputs, outputs] = determine_inputs_outputs(*command); - // TODO: this sanitizing should be done during make_relative - for (auto& input : inputs) { - if (input.rfind("./", 0) == 0) { - input = input.substr(2); - } - } - for (auto& output : outputs) { - if (output.rfind("./", 0) == 0) { - output = output.substr(2); - } - } *command->mutable_inputs() = {inputs.begin(), inputs.end()}; *command->mutable_outputs() = {outputs.begin(), outputs.end()}; |