Skip to content

Commit

Permalink
search: Be smarter about removing datadir and libdir to calculate a p…
Browse files Browse the repository at this point in the history
…refix

There are two corrections here:
 1) only remove one, either the datadir or the libdir
 2) only remove the directory if the whole directory can be removed, for
    example: the prefix is `/usr/lib/foo/x86_64-linux-gnu`, and the libdir
    is `lib/x86_64-linux-gnu`, we don't want to take off just the
    `x86_64-linux-gnu`, we want to leave the whole thing since we
    couldn't remove the entire libdir.
  • Loading branch information
dcbaker committed Sep 11, 2024
1 parent d3841b5 commit d90ed29
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/cps/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,21 +332,31 @@ namespace cps::search {
}

fs::path p = filename.parent_path();

const auto reducer = [&p](fs::path dir) -> std::optional<fs::path> {
fs::path np = p;

// remove a trailing slash
if (dir.stem() == "") {
dir = dir.parent_path();
}

while (dir.stem() == np.stem()) {
dir = dir.parent_path();
np = np.parent_path();
}

// If our new path has changed and we have consumed the entire
// directory, then return that, otherwise this was not
// successful.
return np != p && dir == dir.root_path() ? std::optional{np} : std::nullopt;
};

if (p.stem() == "cps") {
p = p.parent_path();
}

fs::path dir = platform::datadir();
while (dir.stem() == p.stem()) {
dir = dir.parent_path();
p = p.parent_path();
}
dir = platform::libdir();
while (dir.stem() == p.stem()) {
dir = dir.parent_path();
p = p.parent_path();
}
return p;
return reducer(platform::libdir()).value_or(reducer(platform::datadir()).value_or(p));
}

/// @brief Calculate the required components in the graph
Expand Down

0 comments on commit d90ed29

Please sign in to comment.