termux-packages/packages/fish/src-path.cpp.patch

60 lines
2.6 KiB
Diff

This patch fixes inconsistent behaviour of fish when executing absolute binaries in absolute path.
When executing command as /bin/<command>, fish first checks whether <command> is present in /bin/ or not. If it is found, then it will execute the command. But as we are using termux-exec, fish will actually try to execute <command> in @TERMUX_PREFIX@/bin/.
An example of this:-
/bin/ls:
1) Fish will first try find whether ls binary exists in /bin and whether user has permissions to execute it
2) If both the above are true, fish will try to execute /bin/ls.
3) But since we are using termux-exec, the path will be remapped to @TERMUX_PREFIX@/bin/ls
4) This is a *highly* inconsistent behaviour.
This can cause weird bugs when a script tries to execute with hardcoded paths, it might get errors about command not found even if it exists, because fish is checking it at the wrong place.
This patch has been tested to fix the bug
diff --git a/src/path.cpp b/src/path.cpp
index d5a649e0e..ad4e84278 100644
--- a/src/path.cpp
+++ b/src/path.cpp
@@ -30,7 +30,7 @@
#include "wutil.h" // IWYU pragma: keep
// PREFIX is defined at build time.
-static const wcstring_list_t kDefaultPath({L"/bin", L"/usr/bin", PREFIX L"/bin"});
+static const wcstring_list_t kDefaultPath({PREFIX L"/bin"});
static get_path_result_t path_get_path_core(const wcstring &cmd, const wcstring_list_t &pathsv) {
const get_path_result_t noent_res{ENOENT, wcstring{}};
@@ -52,11 +52,19 @@ static get_path_result_t path_get_path_c
return noent_res;
}
+ wcstring _cmd;
+ if (string_prefixes_string(L"/bin/", cmd)) {
+ _cmd = L"@TERMUX_PREFIX@" + cmd;
+ } else if (string_prefixes_string(L"/usr/bin/", cmd)) {
+ _cmd = L"@TERMUX_BASE_DIR@" + cmd;
+ } else {
+ _cmd = cmd;
+ }
// If the command has a slash, it must be an absolute or relative path and thus we don't bother
// looking for a matching command.
- if (cmd.find(L'/') != wcstring::npos) {
- int merr = test_path(cmd);
- return get_path_result_t{merr, cmd};
+ if (_cmd.find(L'/') != wcstring::npos) {
+ int merr = test_path(_cmd);
+ return get_path_result_t{merr, _cmd};
}
get_path_result_t best = noent_res;
@@ -66,7 +74,7 @@ static get_path_result_t path_get_path_core(const wcstring &cmd, const wcstring_
for (const auto &next_path : pathsv) {
if (next_path.empty()) continue;
proposed_path = next_path;
- append_path_component(proposed_path, cmd);
+ append_path_component(proposed_path, _cmd);
int merr = test_path(proposed_path);
if (merr == 0) {
// We found one.