mirror of
https://github.com/fish-shell/fish-shell.git
synced 2025-02-21 05:17:08 +08:00
drop unused path functions
This commit is contained in:
parent
d8feb0bcd1
commit
118dfe776a
9
fish-rust/src/env/environment.rs
vendored
9
fish-rust/src/env/environment.rs
vendored
@ -544,10 +544,6 @@ fn setup_path() {
|
||||
/// This is a simple key->value map and not e.g. cut into paths.
|
||||
pub static INHERITED_VARS: OnceCell<HashMap<WString, WString>> = OnceCell::new();
|
||||
|
||||
extern "C" {
|
||||
fn env_cpp_init();
|
||||
}
|
||||
|
||||
pub fn env_init(paths: Option<&ConfigPaths>, do_uvars: bool, default_paths: bool) {
|
||||
let vars = &PRINCIPAL_STACK;
|
||||
|
||||
@ -614,11 +610,6 @@ pub fn env_init(paths: Option<&ConfigPaths>, do_uvars: bool, default_paths: bool
|
||||
}
|
||||
}
|
||||
|
||||
// See the env_cpp_init() comment for this.
|
||||
unsafe {
|
||||
env_cpp_init();
|
||||
}
|
||||
|
||||
// Set $USER, $HOME and $EUID
|
||||
// This involves going to passwd and stuff.
|
||||
vars.set_one(L!("EUID"), EnvMode::GLOBAL, geteuid().to_wstring());
|
||||
|
14
src/env.cpp
14
src/env.cpp
@ -110,20 +110,6 @@ std::vector<wcstring> null_environment_t::get_names(env_mode_flags_t flags) cons
|
||||
|
||||
bool env_stack_t::is_principal() const { return impl_->is_principal(); }
|
||||
|
||||
extern "C" {
|
||||
void env_cpp_init() {
|
||||
// Temporary for the Rust port.
|
||||
// path_get_config and path_get_data both inspect the environment stack to determine
|
||||
// config paths, and then remember those paths in a static variable. This can lead to
|
||||
// a deadlock if these functions are called while already holding the environment lock.
|
||||
// Call them immediately to trigger the caching.
|
||||
// This can be removed once path_get_config and path_get_data are removed.
|
||||
wcstring dir;
|
||||
path_get_config(dir);
|
||||
path_get_data(dir);
|
||||
}
|
||||
}
|
||||
|
||||
static std::map<wcstring, wcstring> inheriteds;
|
||||
|
||||
void set_inheriteds_ffi() {
|
||||
|
293
src/path.cpp
293
src/path.cpp
@ -5,309 +5,16 @@
|
||||
|
||||
#include "path.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
#if defined(__linux__)
|
||||
#include <sys/statfs.h>
|
||||
#endif
|
||||
#include <unistd.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "common.h"
|
||||
#include "env.h"
|
||||
#include "expand.h"
|
||||
#include "fallback.h" // IWYU pragma: keep
|
||||
#include "flog.h"
|
||||
#include "wcstringutil.h"
|
||||
#include "wutil.h" // IWYU pragma: keep
|
||||
|
||||
// PREFIX is defined at build time.
|
||||
static const std::vector<wcstring> kDefaultPath({L"/bin", L"/usr/bin", PREFIX L"/bin"});
|
||||
|
||||
static get_path_result_t path_get_path_core(const wcstring &cmd,
|
||||
const std::vector<wcstring> &pathsv) {
|
||||
const get_path_result_t noent_res{ENOENT, wcstring{}};
|
||||
get_path_result_t result{};
|
||||
|
||||
/// Test if the given path can be executed.
|
||||
/// \return 0 on success, an errno value on failure.
|
||||
auto test_path = [](const wcstring &path) -> int {
|
||||
std::string narrow = wcs2zstring(path);
|
||||
struct stat buff;
|
||||
if (access(narrow.c_str(), X_OK) != 0 || stat(narrow.c_str(), &buff) != 0) {
|
||||
return errno;
|
||||
}
|
||||
return S_ISREG(buff.st_mode) ? 0 : EACCES;
|
||||
};
|
||||
|
||||
if (cmd.empty()) {
|
||||
return noent_res;
|
||||
}
|
||||
|
||||
// Commands cannot contain NUL byte.
|
||||
if (cmd.find(L'\0') != wcstring::npos) {
|
||||
return noent_res;
|
||||
}
|
||||
|
||||
// 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};
|
||||
}
|
||||
|
||||
get_path_result_t best = noent_res;
|
||||
wcstring proposed_path;
|
||||
for (const auto &next_path : pathsv) {
|
||||
if (next_path.empty()) continue;
|
||||
proposed_path = next_path;
|
||||
append_path_component(proposed_path, cmd);
|
||||
int merr = test_path(proposed_path);
|
||||
if (merr == 0) {
|
||||
// We found one.
|
||||
best = get_path_result_t{merr, std::move(proposed_path)};
|
||||
break;
|
||||
} else if (merr != ENOENT && best.err == ENOENT) {
|
||||
// Keep the first *interesting* error and path around.
|
||||
// ENOENT isn't interesting because not having a file is the normal case.
|
||||
// Ignore if the parent directory is already inaccessible.
|
||||
if (waccess(wdirname(proposed_path), X_OK) == 0) {
|
||||
best = get_path_result_t{merr, std::move(proposed_path)};
|
||||
}
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
get_path_result_t path_try_get_path(const wcstring &cmd, const environment_t &vars) {
|
||||
auto pathvar = vars.get(L"PATH");
|
||||
return path_get_path_core(cmd, pathvar ? pathvar->as_list() : kDefaultPath);
|
||||
}
|
||||
|
||||
/// \return whether the given path is on a remote filesystem.
|
||||
static dir_remoteness_t path_remoteness(const wcstring &path) {
|
||||
std::string narrow = wcs2zstring(path);
|
||||
#if defined(__linux__)
|
||||
struct statfs buf {};
|
||||
if (statfs(narrow.c_str(), &buf) < 0) {
|
||||
return dir_remoteness_t::unknown;
|
||||
}
|
||||
// Linux has constants for these like NFS_SUPER_MAGIC, SMB_SUPER_MAGIC, CIFS_MAGIC_NUMBER but
|
||||
// these are in varying headers. Simply hard code them.
|
||||
// NOTE: The cast is necessary for 32-bit systems because of the 4-byte CIFS_MAGIC_NUMBER
|
||||
switch (static_cast<unsigned int>(buf.f_type)) {
|
||||
case 0x6969: // NFS_SUPER_MAGIC
|
||||
case 0x517B: // SMB_SUPER_MAGIC
|
||||
case 0xFE534D42U: // SMB2_MAGIC_NUMBER - not in the manpage
|
||||
case 0xFF534D42U: // CIFS_MAGIC_NUMBER
|
||||
return dir_remoteness_t::remote;
|
||||
default:
|
||||
// Other FSes are assumed local.
|
||||
return dir_remoteness_t::local;
|
||||
}
|
||||
#elif defined(ST_LOCAL)
|
||||
// ST_LOCAL is a flag to statvfs, which is itself standardized.
|
||||
// In practice the only system to use this path is NetBSD.
|
||||
struct statvfs buf {};
|
||||
if (statvfs(narrow.c_str(), &buf) < 0) return dir_remoteness_t::unknown;
|
||||
return (buf.f_flag & ST_LOCAL) ? dir_remoteness_t::local : dir_remoteness_t::remote;
|
||||
#elif defined(MNT_LOCAL)
|
||||
struct statfs buf {};
|
||||
if (statfs(narrow.c_str(), &buf) < 0) return dir_remoteness_t::unknown;
|
||||
return (buf.f_flags & MNT_LOCAL) ? dir_remoteness_t::local : dir_remoteness_t::remote;
|
||||
#else
|
||||
return dir_remoteness_t::unknown;
|
||||
#endif
|
||||
}
|
||||
|
||||
std::vector<wcstring> path_apply_cdpath(const wcstring &dir, const wcstring &wd,
|
||||
// todo!("houd be environment_t")
|
||||
const env_stack_t &env_vars) {
|
||||
std::vector<wcstring> paths;
|
||||
if (dir.at(0) == L'/') {
|
||||
// Absolute path.
|
||||
paths.push_back(dir);
|
||||
} else if (string_prefixes_string(L"./", dir) || string_prefixes_string(L"../", dir) ||
|
||||
dir == L"." || dir == L"..") {
|
||||
// Path is relative to the working directory.
|
||||
paths.push_back(path_normalize_for_cd(wd, dir));
|
||||
} else {
|
||||
// Respect CDPATH.
|
||||
std::vector<wcstring> cdpathsv;
|
||||
if (auto cdpaths = env_vars.get(L"CDPATH")) {
|
||||
cdpathsv = cdpaths->as_list();
|
||||
}
|
||||
// Always append $PWD
|
||||
cdpathsv.push_back(L".");
|
||||
for (const wcstring &path : cdpathsv) {
|
||||
wcstring abspath;
|
||||
// We want to return an absolute path (see issue 6220)
|
||||
if (path.empty() || (path.front() != L'/' && path.front() != L'~')) {
|
||||
abspath = wd;
|
||||
abspath += L'/';
|
||||
}
|
||||
abspath += path;
|
||||
|
||||
expand_tilde(abspath, env_vars);
|
||||
if (abspath.empty()) continue;
|
||||
abspath = normalize_path(abspath);
|
||||
|
||||
wcstring whole_path = std::move(abspath);
|
||||
append_path_component(whole_path, dir);
|
||||
paths.push_back(whole_path);
|
||||
}
|
||||
}
|
||||
|
||||
return paths;
|
||||
}
|
||||
|
||||
maybe_t<wcstring> path_get_cdpath(const wcstring &dir, const wcstring &wd,
|
||||
// todo!("should be environment_t")
|
||||
const env_stack_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 &a_dir : paths) {
|
||||
struct stat buf;
|
||||
if (wstat(a_dir, &buf) == 0) {
|
||||
if (S_ISDIR(buf.st_mode)) {
|
||||
return a_dir;
|
||||
}
|
||||
err = ENOTDIR;
|
||||
}
|
||||
}
|
||||
|
||||
errno = err;
|
||||
return none();
|
||||
}
|
||||
|
||||
/// Make sure the specified directory exists. If needed, try to create it and any currently not
|
||||
/// existing parent directories, like mkdir -p,.
|
||||
///
|
||||
/// \return 0 if, at the time of function return the directory exists, -1 otherwise.
|
||||
static int create_directory(const wcstring &d) {
|
||||
bool ok = false;
|
||||
struct stat buf;
|
||||
int stat_res = 0;
|
||||
|
||||
while ((stat_res = wstat(d, &buf)) != 0) {
|
||||
if (errno != EAGAIN) break;
|
||||
}
|
||||
|
||||
if (stat_res == 0) {
|
||||
if (S_ISDIR(buf.st_mode)) ok = true;
|
||||
} else if (errno == ENOENT) {
|
||||
wcstring dir = wdirname(d);
|
||||
if (!create_directory(dir) && !wmkdir(d, 0700)) ok = true;
|
||||
}
|
||||
|
||||
return ok ? 0 : -1;
|
||||
}
|
||||
|
||||
namespace {
|
||||
/// The following type wraps up a user's "base" directories, corresponding (conceptually if not
|
||||
/// actually) to XDG spec.
|
||||
struct base_directory_t {
|
||||
wcstring path{}; /// the path where we attempted to create the directory.
|
||||
dir_remoteness_t remoteness{dir_remoteness_t::unknown}; // whether the dir is remote
|
||||
int err{0}; /// the error code if creating the directory failed, or 0 on success.
|
||||
bool success() const { return err == 0; }
|
||||
bool used_xdg{false}; /// whether an XDG variable was used in resolving the directory.
|
||||
};
|
||||
} // namespace
|
||||
|
||||
/// Attempt to get a base directory, creating it if necessary. If a variable named \p xdg_var is
|
||||
/// set, use that directory; otherwise use the path \p non_xdg_homepath rooted in $HOME. \return the
|
||||
/// result; see the base_directory_t fields.
|
||||
static base_directory_t make_base_directory(const wcstring &xdg_var,
|
||||
const wchar_t *non_xdg_homepath) {
|
||||
// The vars we fetch must be exported. Allowing them to be universal doesn't make sense and
|
||||
// allowing that creates a lock inversion that deadlocks the shell since we're called before
|
||||
// uvars are available.
|
||||
const auto &vars = env_stack_t::globals();
|
||||
base_directory_t result{};
|
||||
const auto xdg_dir = vars.get_unless_empty(xdg_var, ENV_GLOBAL | ENV_EXPORT);
|
||||
if (xdg_dir) {
|
||||
result.path = xdg_dir->as_string() + L"/fish";
|
||||
result.used_xdg = true;
|
||||
} else {
|
||||
const auto home = vars.get_unless_empty(L"HOME", ENV_GLOBAL | ENV_EXPORT);
|
||||
if (home) {
|
||||
result.path = home->as_string() + non_xdg_homepath;
|
||||
}
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
if (result.path.empty()) {
|
||||
result.err = ENOENT;
|
||||
} else if (create_directory(result.path) < 0) {
|
||||
result.err = errno;
|
||||
} else {
|
||||
result.err = 0;
|
||||
// Need to append a trailing slash to check the contents of the directory, not its parent.
|
||||
result.remoteness = path_remoteness(result.path + L'/');
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static const base_directory_t &get_data_directory() {
|
||||
static base_directory_t s_dir = make_base_directory(L"XDG_DATA_HOME", L"/.local/share/fish");
|
||||
return s_dir;
|
||||
}
|
||||
|
||||
static const base_directory_t &get_config_directory() {
|
||||
static base_directory_t s_dir = make_base_directory(L"XDG_CONFIG_HOME", L"/.config/fish");
|
||||
return s_dir;
|
||||
}
|
||||
|
||||
bool path_get_config(wcstring &path) {
|
||||
const auto &dir = get_config_directory();
|
||||
path = dir.success() ? dir.path : L"";
|
||||
return dir.success();
|
||||
}
|
||||
|
||||
bool path_get_data(wcstring &path) {
|
||||
const auto &dir = get_data_directory();
|
||||
path = dir.success() ? dir.path : L"";
|
||||
return dir.success();
|
||||
}
|
||||
|
||||
bool paths_are_equivalent(const wcstring &p1, const wcstring &p2) {
|
||||
if (p1 == p2) return true;
|
||||
|
||||
size_t len1 = p1.size(), len2 = p2.size();
|
||||
|
||||
// Ignore trailing slashes after the first character.
|
||||
while (len1 > 1 && p1.at(len1 - 1) == L'/') len1--;
|
||||
while (len2 > 1 && p2.at(len2 - 1) == L'/') len2--;
|
||||
|
||||
// Start walking
|
||||
size_t idx1 = 0, idx2 = 0;
|
||||
while (idx1 < len1 && idx2 < len2) {
|
||||
wchar_t c1 = p1.at(idx1), c2 = p2.at(idx2);
|
||||
|
||||
// If the characters are different, the strings are not equivalent.
|
||||
if (c1 != c2) break;
|
||||
|
||||
idx1++;
|
||||
idx2++;
|
||||
|
||||
// If the character was a slash, walk forwards until we hit the end of the string, or a
|
||||
// non-slash. Note the first condition is invariant within the loop.
|
||||
while (c1 == L'/' && idx1 < len1 && p1.at(idx1) == L'/') idx1++;
|
||||
while (c2 == L'/' && idx2 < len2 && p2.at(idx2) == L'/') idx2++;
|
||||
}
|
||||
|
||||
// We matched if we consumed all of the characters in both strings.
|
||||
return idx1 == len1 && idx2 == len2;
|
||||
}
|
||||
|
||||
void append_path_component(wcstring &path, const wcstring &component) {
|
||||
if (path.empty() || component.empty()) {
|
||||
path.append(component);
|
||||
|
61
src/path.h
61
src/path.h
@ -7,69 +7,8 @@
|
||||
#include <string>
|
||||
|
||||
#include "common.h"
|
||||
#include "env.h"
|
||||
#include "maybe.h"
|
||||
#include "parser.h"
|
||||
#include "wutil.h"
|
||||
|
||||
/// Returns the user configuration directory for fish. If the directory or one of its parents
|
||||
/// doesn't exist, they are first created.
|
||||
///
|
||||
/// \param path The directory as an out param
|
||||
/// \return whether the directory was returned successfully
|
||||
bool path_get_config(wcstring &path);
|
||||
|
||||
/// Returns the user data directory for fish. If the directory or one of its parents doesn't exist,
|
||||
/// they are first created.
|
||||
///
|
||||
/// Volatile files presumed to be local to the machine, such as the fish_history and all the
|
||||
/// generated_completions, will be stored in this directory.
|
||||
///
|
||||
/// \param path The directory as an out param
|
||||
/// \return whether the directory was returned successfully
|
||||
bool path_get_data(wcstring &path);
|
||||
|
||||
enum class dir_remoteness_t {
|
||||
unknown, // directory status is unknown
|
||||
local, // directory is known local
|
||||
remote, // directory is known remote
|
||||
};
|
||||
|
||||
/// Finds the path of an executable named \p cmd, by looking in $PATH taken from \p vars.
|
||||
/// On success, err will be 0 and the path is returned.
|
||||
/// On failure, we return the "best path" with err set appropriately.
|
||||
/// For example, if we find a non-executable file, we will return its path and EACCESS.
|
||||
/// If no candidate path is found, path will be empty and err will be set to ENOENT.
|
||||
/// Possible err values are taken from access().
|
||||
struct get_path_result_t {
|
||||
int err;
|
||||
wcstring path;
|
||||
};
|
||||
get_path_result_t path_try_get_path(const wcstring &cmd, const environment_t &vars);
|
||||
|
||||
/// Returns the full path of the specified directory, using the CDPATH variable as a list of base
|
||||
/// directories for relative paths.
|
||||
///
|
||||
/// If no valid path is found, false is returned and errno is set to ENOTDIR if at least one such
|
||||
/// path was found, but it did not point to a directory, or ENOENT if no file of the specified
|
||||
/// name was found.
|
||||
///
|
||||
/// \param dir The name of the directory.
|
||||
/// \param wd The working directory. The working directory must end with a slash.
|
||||
/// \param vars The environment variables to use (for the CDPATH variable)
|
||||
/// \return the command, or none() if it could not be found.
|
||||
maybe_t<wcstring> path_get_cdpath(const wcstring &dir, const wcstring &wd,
|
||||
// todo!("should be environment_t")
|
||||
const env_stack_t &vars);
|
||||
|
||||
/// Returns the given directory with all CDPATH components applied.
|
||||
std::vector<wcstring> path_apply_cdpath(const wcstring &dir, const wcstring &wd,
|
||||
const env_stack_t &env_vars);
|
||||
|
||||
/// Check if two paths are equivalent, which means to ignore runs of multiple slashes (or trailing
|
||||
/// slashes).
|
||||
bool paths_are_equivalent(const wcstring &p1, const wcstring &p2);
|
||||
|
||||
/// Appends a path component, with a / if necessary.
|
||||
void append_path_component(wcstring &path, const wcstring &component);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user