2016-05-03 04:15:43 +00:00
|
|
|
// Directory utilities. This library contains functions for locating configuration directories, for
|
|
|
|
// testing if a command with a given name can be found in the PATH, and various other path-related
|
|
|
|
// issues.
|
2016-05-18 22:30:21 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2019-10-13 22:50:48 +00:00
|
|
|
#include "path.h"
|
|
|
|
|
2016-05-03 04:15:43 +00:00
|
|
|
#include <errno.h>
|
2006-10-19 11:50:23 +00:00
|
|
|
#include <sys/stat.h>
|
2021-05-09 01:17:54 +00:00
|
|
|
#if defined(__linux__)
|
|
|
|
#include <sys/statfs.h>
|
|
|
|
#endif
|
2006-10-19 11:50:23 +00:00
|
|
|
#include <unistd.h>
|
2019-10-13 22:50:48 +00:00
|
|
|
|
2019-05-05 10:09:25 +00:00
|
|
|
#include <cstring>
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <string>
|
2022-08-21 06:14:48 +00:00
|
|
|
#include <utility>
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <vector>
|
2006-10-19 11:50:23 +00:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "env.h"
|
|
|
|
#include "expand.h"
|
2016-05-03 04:15:43 +00:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2019-05-27 22:56:53 +00:00
|
|
|
#include "flog.h"
|
2020-07-29 23:37:23 +00:00
|
|
|
#include "wcstringutil.h"
|
2016-05-03 04:15:43 +00:00
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2006-10-19 11:50:23 +00:00
|
|
|
|
2022-04-23 22:17:14 +00:00
|
|
|
// PREFIX is defined at build time.
|
2022-08-21 21:43:28 +00:00
|
|
|
static const wcstring_list_t kDefaultPath({L"/bin", L"/usr/bin", PREFIX L"/bin"});
|
2017-08-05 22:08:39 +00:00
|
|
|
|
2022-08-21 21:42:59 +00:00
|
|
|
static get_path_result_t path_get_path_core(const wcstring &cmd, const wcstring_list_t &pathsv) {
|
2022-04-23 22:17:14 +00:00
|
|
|
const get_path_result_t noent_res{ENOENT, wcstring{}};
|
|
|
|
get_path_result_t result{};
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2022-04-23 22:17:14 +00:00
|
|
|
/// 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 = wcs2string(path);
|
2016-05-04 22:19:47 +00:00
|
|
|
struct stat buff;
|
2022-04-23 22:17:14 +00:00
|
|
|
if (access(narrow.c_str(), X_OK) != 0 || stat(narrow.c_str(), &buff) != 0) {
|
|
|
|
return errno;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2022-04-23 22:17:14 +00:00
|
|
|
return S_ISREG(buff.st_mode) ? 0 : EACCES;
|
|
|
|
};
|
|
|
|
|
2022-09-14 16:18:08 +00:00
|
|
|
if (cmd.empty()) {
|
|
|
|
return noent_res;
|
|
|
|
}
|
|
|
|
|
2022-04-23 22:17:14 +00:00
|
|
|
// Commands cannot contain NUL byte.
|
|
|
|
if (cmd.find(L'\0') != wcstring::npos) {
|
|
|
|
return noent_res;
|
2016-05-04 22:19:47 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2022-04-23 22:17:14 +00:00
|
|
|
// 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) {
|
2022-08-15 18:01:17 +00:00
|
|
|
int merr = test_path(cmd);
|
|
|
|
return get_path_result_t{merr, cmd};
|
2016-05-04 22:19:47 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2022-04-23 22:17:14 +00:00
|
|
|
get_path_result_t best = noent_res;
|
|
|
|
wcstring proposed_path;
|
|
|
|
for (const auto &next_path : pathsv) {
|
2017-03-17 03:59:04 +00:00
|
|
|
if (next_path.empty()) continue;
|
2022-04-23 22:17:14 +00:00
|
|
|
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) {
|
2022-03-31 13:10:45 +00:00
|
|
|
// 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.
|
2022-04-23 22:17:14 +00:00
|
|
|
if (waccess(wdirname(proposed_path), X_OK) == 0) {
|
|
|
|
best = get_path_result_t{merr, std::move(proposed_path)};
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-23 22:17:14 +00:00
|
|
|
return best;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2022-04-23 22:17:14 +00:00
|
|
|
maybe_t<wcstring> path_get_path(const wcstring &cmd, const environment_t &vars) {
|
|
|
|
auto result = path_try_get_path(cmd, vars);
|
|
|
|
if (result.err != 0) {
|
|
|
|
return none();
|
|
|
|
}
|
|
|
|
wcstring path = std::move(result.path);
|
|
|
|
return path;
|
2006-10-19 11:50:23 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 22:17:14 +00:00
|
|
|
get_path_result_t path_try_get_path(const wcstring &cmd, const environment_t &vars) {
|
|
|
|
auto pathvar = vars.get(L"PATH");
|
2022-08-21 21:42:59 +00:00
|
|
|
return path_get_path_core(cmd, pathvar ? pathvar->as_list() : kDefaultPath);
|
2012-02-08 06:44:10 +00:00
|
|
|
}
|
|
|
|
|
2021-10-31 10:51:16 +00:00
|
|
|
static bool path_is_executable(const std::string &path) {
|
2020-12-15 17:15:59 +00:00
|
|
|
if (access(path.c_str(), X_OK)) return false;
|
|
|
|
struct stat buff;
|
|
|
|
if (stat(path.c_str(), &buff) == -1) {
|
|
|
|
if (errno != EACCES) wperror(L" stat");
|
|
|
|
return false;
|
|
|
|
}
|
2021-08-17 23:23:02 +00:00
|
|
|
return S_ISREG(buff.st_mode);
|
2020-12-15 17:15:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-19 04:39:00 +00:00
|
|
|
/// \return whether the given path is on a remote filesystem.
|
|
|
|
static dir_remoteness_t path_remoteness(const wcstring &path) {
|
2021-05-09 01:17:54 +00:00
|
|
|
std::string narrow = wcs2string(path);
|
|
|
|
#if defined(__linux__)
|
|
|
|
struct statfs buf {};
|
|
|
|
if (statfs(narrow.c_str(), &buf) < 0) {
|
2021-12-19 04:39:00 +00:00
|
|
|
return dir_remoteness_t::unknown;
|
2021-05-09 01:17:54 +00:00
|
|
|
}
|
|
|
|
// 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
|
2021-12-19 04:39:00 +00:00
|
|
|
return dir_remoteness_t::remote;
|
2021-05-09 01:17:54 +00:00
|
|
|
default:
|
|
|
|
// Other FSes are assumed local.
|
2021-12-19 04:39:00 +00:00
|
|
|
return dir_remoteness_t::local;
|
2021-05-09 01:17:54 +00:00
|
|
|
}
|
|
|
|
#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 {};
|
2021-12-19 04:39:00 +00:00
|
|
|
if (statvfs(narrow.c_str(), &buf) < 0) return dir_remoteness_t::unknown;
|
2022-03-14 16:55:20 +00:00
|
|
|
return (buf.f_flag & ST_LOCAL) ? dir_remoteness_t::local : dir_remoteness_t::remote;
|
2021-05-09 01:17:54 +00:00
|
|
|
#elif defined(MNT_LOCAL)
|
|
|
|
struct statfs buf {};
|
2021-12-19 04:39:00 +00:00
|
|
|
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;
|
2021-05-09 01:17:54 +00:00
|
|
|
#else
|
2021-12-19 04:39:00 +00:00
|
|
|
return dir_remoteness_t::unknown;
|
2021-05-09 01:17:54 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-09-25 02:26:46 +00:00
|
|
|
wcstring_list_t path_get_paths(const wcstring &cmd, const environment_t &vars) {
|
2020-01-19 13:44:30 +00:00
|
|
|
FLOGF(path, L"path_get_paths('%ls')", cmd.c_str());
|
2017-06-23 22:42:38 +00:00
|
|
|
wcstring_list_t paths;
|
|
|
|
|
|
|
|
// If the command has a slash, it must be an absolute or relative path and thus we don't bother
|
|
|
|
// looking for matching commands in the PATH var.
|
|
|
|
if (cmd.find(L'/') != wcstring::npos) {
|
2020-12-15 17:15:59 +00:00
|
|
|
std::string narrow = wcs2string(cmd);
|
|
|
|
if (path_is_executable(narrow)) paths.push_back(cmd);
|
2017-06-23 22:42:38 +00:00
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
2018-09-25 02:26:46 +00:00
|
|
|
auto path_var = vars.get(L"PATH");
|
2020-12-15 14:26:38 +00:00
|
|
|
if (!path_var) return paths;
|
|
|
|
|
|
|
|
const wcstring_list_t &pathsv = path_var->as_list();
|
2017-06-23 22:42:38 +00:00
|
|
|
for (auto path : pathsv) {
|
|
|
|
if (path.empty()) continue;
|
|
|
|
append_path_component(path, cmd);
|
2020-12-15 17:15:59 +00:00
|
|
|
std::string narrow = wcs2string(path);
|
|
|
|
if (path_is_executable(narrow)) paths.push_back(path);
|
2017-06-23 22:42:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
2021-03-27 17:28:03 +00:00
|
|
|
wcstring_list_t path_apply_cdpath(const wcstring &dir, const wcstring &wd,
|
2018-09-16 11:05:17 +00:00
|
|
|
const environment_t &env_vars) {
|
2012-02-19 02:54:36 +00:00
|
|
|
wcstring_list_t paths;
|
2016-05-03 04:15:43 +00:00
|
|
|
if (dir.at(0) == L'/') {
|
|
|
|
// Absolute path.
|
2012-02-19 02:54:36 +00:00
|
|
|
paths.push_back(dir);
|
2016-05-03 04:15:43 +00:00
|
|
|
} else if (string_prefixes_string(L"./", dir) || string_prefixes_string(L"../", dir) ||
|
|
|
|
dir == L"." || dir == L"..") {
|
|
|
|
// Path is relative to the working directory.
|
2018-11-18 02:02:28 +00:00
|
|
|
paths.push_back(path_normalize_for_cd(wd, dir));
|
2016-05-03 04:15:43 +00:00
|
|
|
} else {
|
|
|
|
// Respect CDPATH.
|
2018-09-30 22:20:59 +00:00
|
|
|
wcstring_list_t cdpathsv;
|
|
|
|
if (auto cdpaths = env_vars.get(L"CDPATH")) {
|
|
|
|
cdpathsv = cdpaths->as_list();
|
|
|
|
}
|
2019-03-26 09:11:36 +00:00
|
|
|
// Always append $PWD
|
|
|
|
cdpathsv.push_back(L".");
|
2018-09-30 22:20:59 +00:00
|
|
|
for (wcstring next_path : cdpathsv) {
|
2017-03-17 03:59:04 +00:00
|
|
|
if (next_path.empty()) next_path = L".";
|
2019-10-17 19:15:43 +00:00
|
|
|
if (next_path == L".") {
|
2017-03-17 03:59:04 +00:00
|
|
|
// next_path is just '.', and we have a working directory, so use the wd instead.
|
|
|
|
next_path = wd;
|
2012-05-06 21:53:19 +00:00
|
|
|
}
|
2019-05-22 19:46:53 +00:00
|
|
|
|
2019-10-17 19:15:43 +00:00
|
|
|
// We want to return an absolute path (see issue 6220)
|
|
|
|
if (string_prefixes_string(L"./", next_path)) {
|
2019-05-22 19:46:53 +00:00
|
|
|
next_path = next_path.replace(0, 2, wd);
|
2019-10-17 19:15:43 +00:00
|
|
|
} else if (string_prefixes_string(L"../", next_path) || next_path == L"..") {
|
|
|
|
next_path = next_path.insert(0, wd);
|
2019-05-22 19:46:53 +00:00
|
|
|
}
|
|
|
|
|
2018-09-22 04:52:47 +00:00
|
|
|
expand_tilde(next_path, env_vars);
|
2017-03-17 03:59:04 +00:00
|
|
|
if (next_path.empty()) continue;
|
2006-10-19 11:50:23 +00:00
|
|
|
|
2019-04-04 21:16:34 +00:00
|
|
|
wcstring whole_path = std::move(next_path);
|
2012-05-09 09:33:42 +00:00
|
|
|
append_path_component(whole_path, dir);
|
2012-02-19 02:54:36 +00:00
|
|
|
paths.push_back(whole_path);
|
|
|
|
}
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2021-03-27 17:28:03 +00:00
|
|
|
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);
|
|
|
|
|
2021-10-31 10:51:16 +00:00
|
|
|
for (const wcstring &a_dir : paths) {
|
2012-11-19 00:30:30 +00:00
|
|
|
struct stat buf;
|
2021-10-31 10:51:16 +00:00
|
|
|
if (wstat(a_dir, &buf) == 0) {
|
2016-05-03 04:15:43 +00:00
|
|
|
if (S_ISDIR(buf.st_mode)) {
|
2021-10-31 10:51:16 +00:00
|
|
|
return a_dir;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2018-09-16 11:05:17 +00:00
|
|
|
err = ENOTDIR;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2018-09-16 11:05:17 +00:00
|
|
|
errno = err;
|
|
|
|
return none();
|
2006-10-19 11:50:23 +00:00
|
|
|
}
|
|
|
|
|
2018-09-16 11:05:17 +00:00
|
|
|
maybe_t<wcstring> path_as_implicit_cd(const wcstring &path, const wcstring &wd,
|
|
|
|
const environment_t &vars) {
|
2012-06-02 21:04:25 +00:00
|
|
|
wcstring exp_path = path;
|
2018-09-22 04:52:47 +00:00
|
|
|
expand_tilde(exp_path, vars);
|
2016-05-03 04:15:43 +00:00
|
|
|
if (string_prefixes_string(L"/", exp_path) || string_prefixes_string(L"./", exp_path) ||
|
|
|
|
string_prefixes_string(L"../", exp_path) || string_suffixes_string(L"/", exp_path) ||
|
|
|
|
exp_path == L"..") {
|
2018-09-16 11:05:17 +00:00
|
|
|
// These paths can be implicit cd, so see if you cd to the path. Note that a single period
|
|
|
|
// cannot (that's used for sourcing files anyways).
|
|
|
|
return path_get_cdpath(exp_path, wd, vars);
|
2012-06-02 21:04:25 +00:00
|
|
|
}
|
2018-09-16 11:05:17 +00:00
|
|
|
return none();
|
2012-06-02 21:04:25 +00:00
|
|
|
}
|
2012-02-08 19:48:51 +00:00
|
|
|
|
2016-05-03 04:15:43 +00:00
|
|
|
// If the given path looks like it's relative to the working directory, then prepend that working
|
|
|
|
// directory. This operates on unescaped paths only (so a ~ means a literal ~).
|
|
|
|
wcstring path_apply_working_directory(const wcstring &path, const wcstring &working_directory) {
|
|
|
|
if (path.empty() || working_directory.empty()) return path;
|
|
|
|
|
|
|
|
// We're going to make sure that if we want to prepend the wd, that the string has no leading
|
|
|
|
// "/".
|
2016-10-29 02:15:05 +00:00
|
|
|
bool prepend_wd = path.at(0) != L'/' && path.at(0) != HOME_DIRECTORY;
|
2016-05-03 04:15:43 +00:00
|
|
|
if (!prepend_wd) {
|
|
|
|
// No need to prepend the wd, so just return the path we were given.
|
2016-02-06 22:39:47 +00:00
|
|
|
return path;
|
2016-05-04 22:19:47 +00:00
|
|
|
}
|
2016-05-03 04:15:43 +00:00
|
|
|
|
2016-05-04 22:19:47 +00:00
|
|
|
// Remove up to one "./".
|
|
|
|
wcstring path_component = path;
|
|
|
|
if (string_prefixes_string(L"./", path_component)) {
|
|
|
|
path_component.erase(0, 2);
|
|
|
|
}
|
2016-05-03 04:15:43 +00:00
|
|
|
|
2016-05-04 22:19:47 +00:00
|
|
|
// Removing leading /s.
|
|
|
|
while (string_prefixes_string(L"/", path_component)) {
|
|
|
|
path_component.erase(0, 1);
|
2016-02-06 22:39:47 +00:00
|
|
|
}
|
2016-05-04 22:19:47 +00:00
|
|
|
|
|
|
|
// Construct and return a new path.
|
|
|
|
wcstring new_path = working_directory;
|
|
|
|
append_path_component(new_path, path_component);
|
|
|
|
return new_path;
|
2016-02-06 22:39:47 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 00:35:48 +00:00
|
|
|
/// We separate this from path_create() for two reasons. First it's only caused if there is a
|
|
|
|
/// problem, and thus is not central to the behavior of that function. Second, we only want to issue
|
|
|
|
/// the message once. If the current shell starts a new fish shell (e.g., by running `fish -c` from
|
|
|
|
/// a function) we don't want that subshell to issue the same warnings.
|
|
|
|
static void maybe_issue_path_warning(const wcstring &which_dir, const wcstring &custom_error_msg,
|
|
|
|
bool using_xdg, const wcstring &xdg_var, const wcstring &path,
|
2019-05-02 00:31:22 +00:00
|
|
|
int saved_errno, env_stack_t &vars) {
|
2016-11-27 00:35:48 +00:00
|
|
|
wcstring warning_var_name = L"_FISH_WARNED_" + which_dir;
|
2019-05-02 00:47:50 +00:00
|
|
|
if (vars.get(warning_var_name, ENV_GLOBAL | ENV_EXPORT)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-09-14 07:36:26 +00:00
|
|
|
vars.set_one(warning_var_name, ENV_GLOBAL | ENV_EXPORT, L"1");
|
2016-11-27 00:35:48 +00:00
|
|
|
|
2019-05-27 22:56:53 +00:00
|
|
|
FLOG(error, custom_error_msg.c_str());
|
2016-11-27 00:35:48 +00:00
|
|
|
if (path.empty()) {
|
2020-02-20 16:30:14 +00:00
|
|
|
FLOGF(warning_path, _(L"Unable to locate the %ls directory."), which_dir.c_str());
|
2020-03-26 19:45:40 +00:00
|
|
|
FLOGF(warning_path,
|
|
|
|
_(L"Please set the %ls or HOME environment variable before starting fish."),
|
2019-06-04 03:30:48 +00:00
|
|
|
xdg_var.c_str());
|
2016-11-27 00:35:48 +00:00
|
|
|
} else {
|
|
|
|
const wchar_t *env_var = using_xdg ? xdg_var.c_str() : L"HOME";
|
2020-02-20 16:30:14 +00:00
|
|
|
FLOGF(warning_path, _(L"Unable to locate %ls directory derived from $%ls: '%ls'."),
|
2019-06-04 03:30:48 +00:00
|
|
|
which_dir.c_str(), env_var, path.c_str());
|
2020-02-20 16:30:14 +00:00
|
|
|
FLOGF(warning_path, _(L"The error was '%s'."), std::strerror(saved_errno));
|
2020-03-26 19:45:40 +00:00
|
|
|
FLOGF(warning_path, _(L"Please set $%ls to a directory where you have write access."),
|
|
|
|
env_var);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2017-08-12 14:54:26 +00:00
|
|
|
ignore_result(write(STDERR_FILENO, "\n", 1));
|
2012-12-03 10:25:08 +00:00
|
|
|
}
|
2012-02-06 00:42:24 +00:00
|
|
|
|
2020-01-28 18:30:51 +00:00
|
|
|
/// 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;
|
|
|
|
}
|
|
|
|
|
2021-09-28 19:50:27 +00:00
|
|
|
namespace {
|
2019-05-02 00:31:22 +00:00
|
|
|
/// The following type wraps up a user's "base" directories, corresponding (conceptually if not
|
|
|
|
/// actually) to XDG spec.
|
|
|
|
struct base_directory_t {
|
2021-12-11 18:48:23 +00:00
|
|
|
wcstring path{}; /// the path where we attempted to create the directory.
|
2021-12-19 04:39:00 +00:00
|
|
|
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.
|
2021-05-08 23:40:53 +00:00
|
|
|
bool success() const { return err == 0; }
|
2021-12-09 08:28:30 +00:00
|
|
|
bool used_xdg{false}; /// whether an XDG variable was used in resolving the directory.
|
2019-05-02 00:31:22 +00:00
|
|
|
};
|
2021-09-28 19:50:27 +00:00
|
|
|
} // namespace
|
2019-05-02 00:31:22 +00:00
|
|
|
|
|
|
|
/// 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) {
|
2016-11-27 00:35:48 +00:00
|
|
|
// 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.
|
2018-09-25 02:26:46 +00:00
|
|
|
const auto &vars = env_stack_t::globals();
|
2019-05-02 00:31:22 +00:00
|
|
|
base_directory_t result{};
|
2018-09-25 02:26:46 +00:00
|
|
|
const auto xdg_dir = vars.get(xdg_var, ENV_GLOBAL | ENV_EXPORT);
|
2016-11-27 00:35:48 +00:00
|
|
|
if (!xdg_dir.missing_or_empty()) {
|
2019-05-02 00:31:22 +00:00
|
|
|
result.path = xdg_dir->as_string() + L"/fish";
|
|
|
|
result.used_xdg = true;
|
2016-05-03 04:15:43 +00:00
|
|
|
} else {
|
2018-09-25 02:26:46 +00:00
|
|
|
const auto home = vars.get(L"HOME", ENV_GLOBAL | ENV_EXPORT);
|
2016-11-27 00:35:48 +00:00
|
|
|
if (!home.missing_or_empty()) {
|
2019-05-02 00:31:22 +00:00
|
|
|
result.path = home->as_string() + non_xdg_homepath;
|
2015-09-17 04:31:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 00:31:22 +00:00
|
|
|
errno = 0;
|
2021-05-08 23:40:53 +00:00
|
|
|
if (result.path.empty()) {
|
|
|
|
result.err = ENOENT;
|
|
|
|
} else if (create_directory(result.path) < 0) {
|
|
|
|
result.err = errno;
|
|
|
|
} else {
|
|
|
|
result.err = 0;
|
2021-05-09 01:17:54 +00:00
|
|
|
// Need to append a trailing slash to check the contents of the directory, not its parent.
|
2021-12-19 04:39:00 +00:00
|
|
|
result.remoteness = path_remoteness(result.path + L'/');
|
2021-05-08 23:40:53 +00:00
|
|
|
}
|
2019-05-02 00:31:22 +00:00
|
|
|
return result;
|
|
|
|
}
|
2016-11-27 00:35:48 +00:00
|
|
|
|
2019-05-02 00:31:22 +00:00
|
|
|
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;
|
2015-09-17 04:31:08 +00:00
|
|
|
}
|
|
|
|
|
2019-05-02 00:31:22 +00:00
|
|
|
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;
|
|
|
|
}
|
2016-11-27 00:35:48 +00:00
|
|
|
|
2021-05-09 01:17:54 +00:00
|
|
|
void path_emit_config_directory_messages(env_stack_t &vars) {
|
2019-05-02 00:31:22 +00:00
|
|
|
const auto &data = get_data_directory();
|
2021-05-08 23:40:53 +00:00
|
|
|
if (!data.success()) {
|
2021-11-02 10:09:38 +00:00
|
|
|
maybe_issue_path_warning(L"data", _(L"can not save history"), data.used_xdg,
|
2019-05-02 00:31:22 +00:00
|
|
|
L"XDG_DATA_HOME", data.path, data.err, vars);
|
2016-11-27 00:35:48 +00:00
|
|
|
}
|
2021-12-19 04:39:00 +00:00
|
|
|
if (data.remoteness == dir_remoteness_t::remote) {
|
2021-05-09 01:17:54 +00:00
|
|
|
FLOG(path, "data path appears to be on a network volume");
|
|
|
|
}
|
2016-11-27 00:35:48 +00:00
|
|
|
|
2019-05-02 00:31:22 +00:00
|
|
|
const auto &config = get_config_directory();
|
2021-05-08 23:40:53 +00:00
|
|
|
if (!config.success()) {
|
2021-11-02 10:09:38 +00:00
|
|
|
maybe_issue_path_warning(L"config", _(L"can not save universal variables or functions"),
|
2019-05-02 00:31:22 +00:00
|
|
|
config.used_xdg, L"XDG_CONFIG_HOME", config.path, config.err,
|
|
|
|
vars);
|
|
|
|
}
|
2021-12-19 04:39:00 +00:00
|
|
|
if (config.remoteness == dir_remoteness_t::remote) {
|
2021-05-09 01:17:54 +00:00
|
|
|
FLOG(path, "config path appears to be on a network volume");
|
|
|
|
}
|
2006-10-19 11:50:23 +00:00
|
|
|
}
|
|
|
|
|
2019-05-02 00:31:22 +00:00
|
|
|
bool path_get_config(wcstring &path) {
|
|
|
|
const auto &dir = get_config_directory();
|
2021-05-08 23:40:53 +00:00
|
|
|
path = dir.success() ? dir.path : L"";
|
|
|
|
return dir.success();
|
2019-05-02 00:31:22 +00:00
|
|
|
}
|
2016-11-27 00:35:48 +00:00
|
|
|
|
2019-05-02 00:31:22 +00:00
|
|
|
bool path_get_data(wcstring &path) {
|
|
|
|
const auto &dir = get_data_directory();
|
2021-05-08 23:40:53 +00:00
|
|
|
path = dir.success() ? dir.path : L"";
|
|
|
|
return dir.success();
|
2015-09-17 04:31:08 +00:00
|
|
|
}
|
|
|
|
|
2021-12-19 04:39:00 +00:00
|
|
|
dir_remoteness_t path_get_data_remoteness() { return get_data_directory().remoteness; }
|
2021-05-09 21:47:10 +00:00
|
|
|
|
2021-12-19 04:39:00 +00:00
|
|
|
dir_remoteness_t path_get_config_remoteness() { return get_config_directory().remoteness; }
|
2021-05-10 22:10:52 +00:00
|
|
|
|
2016-05-03 04:15:43 +00:00
|
|
|
void path_make_canonical(wcstring &path) {
|
|
|
|
// Ignore trailing slashes, unless it's the first character.
|
2013-08-28 01:26:22 +00:00
|
|
|
size_t len = path.size();
|
2016-05-03 04:15:43 +00:00
|
|
|
while (len > 1 && path.at(len - 1) == L'/') len--;
|
2013-08-28 01:26:22 +00:00
|
|
|
|
2016-05-03 04:15:43 +00:00
|
|
|
// Turn runs of slashes into a single slash.
|
2013-08-28 01:26:22 +00:00
|
|
|
size_t trailing = 0;
|
|
|
|
bool prev_was_slash = false;
|
2016-05-03 04:15:43 +00:00
|
|
|
for (size_t leading = 0; leading < len; leading++) {
|
2013-08-28 01:26:22 +00:00
|
|
|
wchar_t c = path.at(leading);
|
|
|
|
bool is_slash = (c == '/');
|
2016-05-03 04:15:43 +00:00
|
|
|
if (!prev_was_slash || !is_slash) {
|
|
|
|
// This is either the first slash in a run, or not a slash at all.
|
2013-08-28 01:26:22 +00:00
|
|
|
path.at(trailing++) = c;
|
|
|
|
}
|
|
|
|
prev_was_slash = is_slash;
|
2013-10-26 22:27:39 +00:00
|
|
|
}
|
2013-08-28 01:26:22 +00:00
|
|
|
assert(trailing <= len);
|
2016-05-03 04:15:43 +00:00
|
|
|
if (trailing < len) path.resize(trailing);
|
2013-08-28 01:26:22 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 04:15:43 +00:00
|
|
|
bool paths_are_equivalent(const wcstring &p1, const wcstring &p2) {
|
|
|
|
if (p1 == p2) return true;
|
2013-10-26 22:27:39 +00:00
|
|
|
|
2013-08-28 01:26:22 +00:00
|
|
|
size_t len1 = p1.size(), len2 = p2.size();
|
2013-10-26 22:27:39 +00:00
|
|
|
|
2016-05-03 04:15:43 +00:00
|
|
|
// Ignore trailing slashes after the first character.
|
2013-08-28 01:26:22 +00:00
|
|
|
while (len1 > 1 && p1.at(len1 - 1) == L'/') len1--;
|
|
|
|
while (len2 > 1 && p2.at(len2 - 1) == L'/') len2--;
|
2013-10-26 22:27:39 +00:00
|
|
|
|
2013-08-28 01:26:22 +00:00
|
|
|
// Start walking
|
|
|
|
size_t idx1 = 0, idx2 = 0;
|
2016-05-03 04:15:43 +00:00
|
|
|
while (idx1 < len1 && idx2 < len2) {
|
2013-08-28 01:26:22 +00:00
|
|
|
wchar_t c1 = p1.at(idx1), c2 = p2.at(idx2);
|
2013-10-26 22:27:39 +00:00
|
|
|
|
2016-05-03 04:15:43 +00:00
|
|
|
// If the characters are different, the strings are not equivalent.
|
|
|
|
if (c1 != c2) break;
|
2013-10-26 22:27:39 +00:00
|
|
|
|
2013-08-28 01:26:22 +00:00
|
|
|
idx1++;
|
|
|
|
idx2++;
|
2013-10-26 22:27:39 +00:00
|
|
|
|
2016-05-03 04:15:43 +00:00
|
|
|
// 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.
|
2013-08-28 01:26:22 +00:00
|
|
|
while (c1 == L'/' && idx1 < len1 && p1.at(idx1) == L'/') idx1++;
|
|
|
|
while (c2 == L'/' && idx2 < len2 && p2.at(idx2) == L'/') idx2++;
|
2012-02-08 05:23:12 +00:00
|
|
|
}
|
2013-10-26 22:27:39 +00:00
|
|
|
|
2016-05-03 04:15:43 +00:00
|
|
|
// We matched if we consumed all of the characters in both strings.
|
2013-08-28 01:26:22 +00:00
|
|
|
return idx1 == len1 && idx2 == len2;
|
2007-05-10 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 04:15:43 +00:00
|
|
|
bool path_is_valid(const wcstring &path, const wcstring &working_directory) {
|
2012-02-19 02:54:36 +00:00
|
|
|
bool path_is_valid;
|
2016-05-03 04:15:43 +00:00
|
|
|
// Some special paths are always valid.
|
|
|
|
if (path.empty()) {
|
2012-02-19 02:54:36 +00:00
|
|
|
path_is_valid = false;
|
2016-05-03 04:15:43 +00:00
|
|
|
} else if (path == L"." || path == L"./") {
|
2012-02-19 02:54:36 +00:00
|
|
|
path_is_valid = true;
|
2016-05-03 04:15:43 +00:00
|
|
|
} else if (path == L".." || path == L"../") {
|
|
|
|
path_is_valid = (!working_directory.empty() && working_directory != L"/");
|
|
|
|
} else if (path.at(0) != '/') {
|
|
|
|
// Prepend the working directory. Note that we know path is not empty here.
|
2012-02-19 02:54:36 +00:00
|
|
|
wcstring tmp = working_directory;
|
|
|
|
tmp.append(path);
|
2012-07-21 05:11:05 +00:00
|
|
|
path_is_valid = (0 == waccess(tmp, F_OK));
|
2016-05-03 04:15:43 +00:00
|
|
|
} else {
|
|
|
|
// Simple check.
|
2012-07-21 05:11:05 +00:00
|
|
|
path_is_valid = (0 == waccess(path, F_OK));
|
2012-02-19 02:54:36 +00:00
|
|
|
}
|
|
|
|
return path_is_valid;
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:15:43 +00:00
|
|
|
bool paths_are_same_file(const wcstring &path1, const wcstring &path2) {
|
|
|
|
if (paths_are_equivalent(path1, path2)) return true;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-02-19 05:56:30 +00:00
|
|
|
struct stat s1, s2;
|
2016-05-03 04:15:43 +00:00
|
|
|
if (wstat(path1, &s1) == 0 && wstat(path2, &s2) == 0) {
|
2012-02-19 05:56:30 +00:00
|
|
|
return s1.st_ino == s2.st_ino && s1.st_dev == s2.st_dev;
|
|
|
|
}
|
2016-10-29 00:42:50 +00:00
|
|
|
|
|
|
|
return false;
|
2012-02-19 05:56:30 +00:00
|
|
|
}
|
2020-01-15 21:16:43 +00:00
|
|
|
|
|
|
|
void append_path_component(wcstring &path, const wcstring &component) {
|
|
|
|
if (path.empty() || component.empty()) {
|
|
|
|
path.append(component);
|
|
|
|
} else {
|
|
|
|
size_t path_len = path.size();
|
|
|
|
bool path_slash = path.at(path_len - 1) == L'/';
|
|
|
|
bool comp_slash = component.at(0) == L'/';
|
|
|
|
if (!path_slash && !comp_slash) {
|
|
|
|
// Need a slash
|
|
|
|
path.push_back(L'/');
|
|
|
|
} else if (path_slash && comp_slash) {
|
|
|
|
// Too many slashes.
|
|
|
|
path.erase(path_len - 1, 1);
|
|
|
|
}
|
|
|
|
path.append(component);
|
|
|
|
}
|
|
|
|
}
|