aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Maennich <maennich@google.com>2021-12-06 22:37:19 +0000
committerMatthias Maennich <maennich@google.com>2021-12-08 13:10:11 +0000
commit28d23c975ec11679945b4a72a085a08dc176321c (patch)
treedde2ecaaf5473ab147f07d9121dcd732a1e20d2d
parentbb8c8328fe50f4f9bcd444c69e27bff5fd71d0f6 (diff)
downloadinterceptor-28d23c975ec11679945b4a72a085a08dc176321c.tar.gz
interceptor: tidy up default_make_relative
This removes some outstanding code smells identified in aosp/1898997. Some correctness fixes, some cleanups. Bug: 207620569 Signed-off-by: Matthias Maennich <maennich@google.com> Change-Id: Ic31b2ac1ef18b2a93b99dc2e303f7c6ccd66d1d6
-rw-r--r--interceptor.cc32
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()};