cd first, ask questions later (#7586)

cd: Just try to cd without checking first

Some filesystems are broken and error out on `stat(3)` of existing and
cd-able directories.

So we just try to `fchdir` and report errors later.

Fixes #7577.
This commit is contained in:
Fabian Homborg
2021-03-27 18:28:03 +01:00
committed by GitHub
parent 50398ea9f5
commit df53d1415d
3 changed files with 68 additions and 40 deletions

View File

@@ -126,11 +126,8 @@ wcstring_list_t path_get_paths(const wcstring &cmd, const environment_t &vars) {
return paths;
}
maybe_t<wcstring> path_get_cdpath(const wcstring &dir, const wcstring &wd,
wcstring_list_t path_apply_cdpath(const wcstring &dir, const wcstring &wd,
const environment_t &env_vars) {
int err = ENOENT;
if (dir.empty()) return none();
assert(!wd.empty() && wd.back() == L'/');
wcstring_list_t paths;
if (dir.at(0) == L'/') {
// Absolute path.
@@ -170,6 +167,16 @@ maybe_t<wcstring> path_get_cdpath(const wcstring &dir, const wcstring &wd,
}
}
return paths;
}
maybe_t<wcstring> path_get_cdpath(const wcstring &dir, const wcstring &wd,
const environment_t &env_vars) {
int err = ENOENT;
if (dir.empty()) return none();
assert(!wd.empty() && wd.back() == L'/');
auto paths = path_apply_cdpath(dir, wd, env_vars);
for (const wcstring &dir : paths) {
struct stat buf;
if (wstat(dir, &buf) == 0) {