mirror of
https://github.com/fish-shell/fish-shell.git
synced 2025-01-19 22:02:45 +08:00
builtin realpath: use absolute path also with -s/--no-symlinks
The old test needs to be changed because $XDG_DATA_HOME can be relative. Fixes #7574
This commit is contained in:
parent
4dae106911
commit
322ceb7ab4
|
@ -12,6 +12,7 @@
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "fallback.h" // IWYU pragma: keep
|
#include "fallback.h" // IWYU pragma: keep
|
||||||
#include "io.h"
|
#include "io.h"
|
||||||
|
#include "wcstringutil.h"
|
||||||
#include "wgetopt.h"
|
#include "wgetopt.h"
|
||||||
#include "wutil.h" // IWYU pragma: keep
|
#include "wutil.h" // IWYU pragma: keep
|
||||||
|
|
||||||
|
@ -80,25 +81,27 @@ maybe_t<int> builtin_realpath(parser_t &parser, io_streams_t &streams, wchar_t *
|
||||||
return STATUS_INVALID_ARGS;
|
return STATUS_INVALID_ARGS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const wchar_t *arg = argv[optind];
|
||||||
|
|
||||||
if (!opts.no_symlinks) {
|
if (!opts.no_symlinks) {
|
||||||
if (auto real_path = wrealpath(argv[optind])) {
|
if (auto real_path = wrealpath(arg)) {
|
||||||
streams.out.append(*real_path);
|
streams.out.append(*real_path);
|
||||||
} else {
|
} else {
|
||||||
if (errno) {
|
if (errno) {
|
||||||
// realpath() just couldn't do it. Report the error and make it clear
|
// realpath() just couldn't do it. Report the error and make it clear
|
||||||
// this is an error from our builtin, not the system's realpath.
|
// this is an error from our builtin, not the system's realpath.
|
||||||
streams.err.append_format(L"builtin %ls: %ls: %s\n", cmd, argv[optind],
|
streams.err.append_format(L"builtin %ls: %ls: %s\n", cmd, arg,
|
||||||
std::strerror(errno));
|
std::strerror(errno));
|
||||||
} else {
|
} else {
|
||||||
// Who knows. Probably a bug in our wrealpath() implementation.
|
// Who knows. Probably a bug in our wrealpath() implementation.
|
||||||
streams.err.append_format(_(L"builtin %ls: Invalid path: %ls\n"), cmd,
|
streams.err.append_format(_(L"builtin %ls: Invalid arg: %ls\n"), cmd, arg);
|
||||||
argv[optind]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return STATUS_CMD_ERROR;
|
return STATUS_CMD_ERROR;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
streams.out.append(normalize_path(argv[optind], /* allow leading double slashes */ false));
|
wcstring absolute_arg = string_prefixes_string(L"/", arg) ? arg : wgetcwd() + L"/" + arg;
|
||||||
|
streams.out.append(normalize_path(absolute_arg, /* allow leading double slashes */ false));
|
||||||
}
|
}
|
||||||
|
|
||||||
streams.out.append(L"\n");
|
streams.out.append(L"\n");
|
||||||
|
|
|
@ -63,8 +63,8 @@ else
|
||||||
end
|
end
|
||||||
|
|
||||||
# With "-s" the symlink is not resolved.
|
# With "-s" the symlink is not resolved.
|
||||||
set -l real_path (builtin realpath -s $XDG_DATA_HOME/fish-symlink)
|
set -l real_path (builtin realpath -s $data_home_realpath/fish-symlink)
|
||||||
set -l expected_real_path "$XDG_DATA_HOME/fish-symlink"
|
set -l expected_real_path "$data_home_realpath/fish-symlink"
|
||||||
if test "$real_path" = "$expected_real_path"
|
if test "$real_path" = "$expected_real_path"
|
||||||
echo "fish-symlink handled correctly"
|
echo "fish-symlink handled correctly"
|
||||||
# CHECK: fish-symlink handled correctly
|
# CHECK: fish-symlink handled correctly
|
||||||
|
@ -72,7 +72,16 @@ else
|
||||||
echo "fish-symlink not handled correctly: $real_path != $expected_real_path" >&2
|
echo "fish-symlink not handled correctly: $real_path != $expected_real_path" >&2
|
||||||
end
|
end
|
||||||
|
|
||||||
test (builtin realpath -s /usr/bin/../) = "/usr"
|
set -l real_path (builtin realpath -s .)
|
||||||
|
set -l expected_real_path (pwd) # Logical working directory.
|
||||||
|
if test "$real_path" = "$expected_real_path"
|
||||||
|
echo "relative path correctly handled"
|
||||||
|
# CHECK: relative path correctly handled
|
||||||
|
else
|
||||||
|
echo "relative path not handled correctly: $real_path != $expected_real_path" >&2
|
||||||
|
end
|
||||||
|
|
||||||
|
test (builtin realpath -s /usr/bin/../) = /usr
|
||||||
or echo builtin realpath -s does not resolve .. or resolves symlink wrong
|
or echo builtin realpath -s does not resolve .. or resolves symlink wrong
|
||||||
|
|
||||||
# A nonexistent file relative to a valid symlink to a directory gets converted.
|
# A nonexistent file relative to a valid symlink to a directory gets converted.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user