2016-05-03 12:15:43 +08: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-19 06:30:21 +08:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2016-05-03 12:15:43 +08:00
|
|
|
#include <errno.h>
|
2016-11-27 08:35:48 +08:00
|
|
|
#include <string.h>
|
2006-10-19 19:50:23 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2016-05-03 12:15:43 +08:00
|
|
|
#include <wchar.h>
|
2016-11-27 08:35:48 +08:00
|
|
|
|
2017-07-09 04:14:30 +08:00
|
|
|
#include <memory>
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <string>
|
2017-02-11 10:47:02 +08:00
|
|
|
#include <type_traits>
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <vector>
|
2006-10-19 19:50:23 +08:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "env.h"
|
|
|
|
#include "expand.h"
|
2016-05-03 12:15:43 +08:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
|
|
|
#include "path.h"
|
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2006-10-19 19:50:23 +08:00
|
|
|
|
2016-05-03 12:15:43 +08:00
|
|
|
/// Unexpected error in path_get_path().
|
|
|
|
#define MISSING_COMMAND_ERR_MSG _(L"Error while searching for command '%ls'")
|
2006-10-19 19:50:23 +08:00
|
|
|
|
2016-05-03 12:15:43 +08:00
|
|
|
static bool path_get_path_core(const wcstring &cmd, wcstring *out_path,
|
|
|
|
const env_var_t &bin_path_var) {
|
2012-11-19 08:30:30 +08:00
|
|
|
debug(3, L"path_get_path( '%ls' )", cmd.c_str());
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-06-24 06:42:38 +08: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.
|
2016-05-03 12:15:43 +08:00
|
|
|
if (cmd.find(L'/') != wcstring::npos) {
|
2016-05-05 06:19:47 +08:00
|
|
|
if (waccess(cmd, X_OK) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-05 06:19:47 +08:00
|
|
|
struct stat buff;
|
|
|
|
if (wstat(cmd, &buff)) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
if (S_ISREG(buff.st_mode)) {
|
|
|
|
if (out_path) out_path->assign(cmd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
errno = EACCES;
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-06-24 06:42:38 +08:00
|
|
|
int err = ENOENT;
|
2017-08-10 02:11:58 +08:00
|
|
|
wcstring_list_t pathsv;
|
2016-05-05 06:19:47 +08:00
|
|
|
if (!bin_path_var.missing()) {
|
2017-08-10 02:11:58 +08:00
|
|
|
bin_path_var.to_list(pathsv);
|
2016-05-03 12:15:43 +08:00
|
|
|
} else {
|
2017-05-05 13:42:42 +08:00
|
|
|
// Note that PREFIX is defined in the `Makefile` and is thus defined when this module is
|
|
|
|
// compiled. This ensures we always default to "/bin", "/usr/bin" and the bin dir defined
|
|
|
|
// for the fish programs. Possibly with a duplicate dir if PREFIX is empty, "/", "/usr" or
|
|
|
|
// "/usr/". If the PREFIX duplicates /bin or /usr/bin that is harmless other than a trivial
|
|
|
|
// amount of time testing a path we've already tested.
|
2017-08-10 02:11:58 +08:00
|
|
|
pathsv = wcstring_list_t({L"/bin", L"/usr/bin", PREFIX L"/bin"});
|
2016-05-05 06:19:47 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-03-17 11:59:04 +08:00
|
|
|
for (auto next_path : pathsv) {
|
|
|
|
if (next_path.empty()) continue;
|
|
|
|
append_path_component(next_path, cmd);
|
|
|
|
if (waccess(next_path, X_OK) == 0) {
|
2016-05-05 06:19:47 +08:00
|
|
|
struct stat buff;
|
2017-03-17 11:59:04 +08:00
|
|
|
if (wstat(next_path, &buff) == -1) {
|
2016-05-05 06:19:47 +08:00
|
|
|
if (errno != EACCES) {
|
|
|
|
wperror(L"stat");
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (S_ISREG(buff.st_mode)) {
|
2017-03-17 11:59:04 +08:00
|
|
|
if (out_path) *out_path = std::move(next_path);
|
2016-05-05 06:19:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
err = EACCES;
|
|
|
|
} else {
|
|
|
|
switch (errno) {
|
|
|
|
case ENOENT:
|
|
|
|
case ENAMETOOLONG:
|
|
|
|
case EACCES:
|
|
|
|
case ENOTDIR: {
|
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
default: {
|
2017-03-17 11:59:04 +08:00
|
|
|
debug(1, MISSING_COMMAND_ERR_MSG, next_path.c_str());
|
2016-05-05 06:19:47 +08:00
|
|
|
wperror(L"access");
|
2016-11-02 13:36:30 +08:00
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
errno = err;
|
|
|
|
return false;
|
2006-10-19 19:50:23 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 12:15:43 +08:00
|
|
|
bool path_get_path(const wcstring &cmd, wcstring *out_path, const env_vars_snapshot_t &vars) {
|
2012-07-21 13:11:05 +08:00
|
|
|
return path_get_path_core(cmd, out_path, vars.get(L"PATH"));
|
2012-02-08 14:44:10 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 12:15:43 +08:00
|
|
|
bool path_get_path(const wcstring &cmd, wcstring *out_path) {
|
2017-08-06 09:22:49 +08:00
|
|
|
return path_get_path_core(cmd, out_path, env_get(L"PATH"));
|
2012-07-21 06:01:56 +08:00
|
|
|
}
|
2006-10-19 19:50:23 +08:00
|
|
|
|
2017-06-24 06:42:38 +08:00
|
|
|
wcstring_list_t path_get_paths(const wcstring &cmd) {
|
|
|
|
debug(3, L"path_get_paths('%ls')", cmd.c_str());
|
|
|
|
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) {
|
|
|
|
struct stat buff;
|
|
|
|
if (wstat(cmd, &buff)) return paths;
|
|
|
|
if (!S_ISREG(buff.st_mode)) return paths;
|
|
|
|
if (waccess(cmd, X_OK)) return paths;
|
|
|
|
paths.push_back(cmd);
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
2017-08-06 09:22:49 +08:00
|
|
|
env_var_t path_var = env_get(L"PATH");
|
2017-06-24 06:42:38 +08:00
|
|
|
std::vector<wcstring> pathsv;
|
2017-08-06 09:22:49 +08:00
|
|
|
path_var.to_list(pathsv);
|
2017-06-24 06:42:38 +08:00
|
|
|
for (auto path : pathsv) {
|
|
|
|
if (path.empty()) continue;
|
|
|
|
append_path_component(path, cmd);
|
|
|
|
if (waccess(path, X_OK) == 0) {
|
|
|
|
struct stat buff;
|
|
|
|
if (wstat(path, &buff) == -1) {
|
|
|
|
if (errno != EACCES) wperror(L"stat");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (S_ISREG(buff.st_mode)) paths.push_back(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
2017-08-06 09:22:49 +08:00
|
|
|
bool path_get_cdpath(const env_var_t &dir_var, wcstring *out, const wchar_t *wd,
|
2016-05-03 12:15:43 +08:00
|
|
|
const env_vars_snapshot_t &env_vars) {
|
2012-11-19 08:30:30 +08:00
|
|
|
int err = ENOENT;
|
2017-08-06 09:22:49 +08:00
|
|
|
if (dir_var.missing_or_empty()) return false;
|
|
|
|
wcstring dir = dir_var.as_string();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:15:43 +08:00
|
|
|
if (wd) {
|
2012-02-19 10:54:36 +08:00
|
|
|
size_t len = wcslen(wd);
|
|
|
|
assert(wd[len - 1] == L'/');
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-02-19 10:54:36 +08:00
|
|
|
wcstring_list_t paths;
|
2016-05-03 12:15:43 +08:00
|
|
|
if (dir.at(0) == L'/') {
|
|
|
|
// Absolute path.
|
2012-02-19 10:54:36 +08:00
|
|
|
paths.push_back(dir);
|
2016-05-03 12:15:43 +08:00
|
|
|
} else if (string_prefixes_string(L"./", dir) || string_prefixes_string(L"../", dir) ||
|
|
|
|
dir == L"." || dir == L"..") {
|
|
|
|
// Path is relative to the working directory.
|
2012-02-19 10:54:36 +08:00
|
|
|
wcstring path;
|
2016-05-03 12:15:43 +08:00
|
|
|
if (wd) path.append(wd);
|
2012-02-19 10:54:36 +08:00
|
|
|
path.append(dir);
|
|
|
|
paths.push_back(path);
|
2016-05-03 12:15:43 +08:00
|
|
|
} else {
|
|
|
|
// Respect CDPATH.
|
2017-03-17 11:59:04 +08:00
|
|
|
env_var_t cdpaths = env_vars.get(L"CDPATH");
|
|
|
|
if (cdpaths.missing_or_empty()) cdpaths = L".";
|
|
|
|
|
|
|
|
std::vector<wcstring> cdpathsv;
|
2017-08-06 09:22:49 +08:00
|
|
|
cdpaths.to_list(cdpathsv);
|
2017-03-17 11:59:04 +08:00
|
|
|
for (auto next_path : cdpathsv) {
|
|
|
|
if (next_path.empty()) next_path = L".";
|
|
|
|
if (next_path == L"." && wd != NULL) {
|
|
|
|
// next_path is just '.', and we have a working directory, so use the wd instead.
|
|
|
|
// TODO: if next_path starts with ./ we need to replace the . with the wd.
|
|
|
|
next_path = wd;
|
2012-05-07 05:53:19 +08:00
|
|
|
}
|
2017-03-17 11:59:04 +08:00
|
|
|
expand_tilde(next_path);
|
|
|
|
if (next_path.empty()) continue;
|
2006-10-19 19:50:23 +08:00
|
|
|
|
2017-03-17 11:59:04 +08:00
|
|
|
wcstring whole_path = next_path;
|
2012-05-09 17:33:42 +08:00
|
|
|
append_path_component(whole_path, dir);
|
2012-02-19 10:54:36 +08:00
|
|
|
paths.push_back(whole_path);
|
|
|
|
}
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-07-21 13:11:05 +08:00
|
|
|
bool success = false;
|
2016-05-03 12:15:43 +08:00
|
|
|
for (wcstring_list_t::const_iterator iter = paths.begin(); iter != paths.end(); ++iter) {
|
2012-11-19 08:30:30 +08:00
|
|
|
struct stat buf;
|
|
|
|
const wcstring &dir = *iter;
|
2016-05-03 12:15:43 +08:00
|
|
|
if (wstat(dir, &buf) == 0) {
|
|
|
|
if (S_ISDIR(buf.st_mode)) {
|
2012-07-21 13:11:05 +08:00
|
|
|
success = true;
|
2016-05-03 12:15:43 +08:00
|
|
|
if (out) out->assign(dir);
|
2012-02-19 10:54:36 +08:00
|
|
|
break;
|
2016-05-03 12:15:43 +08:00
|
|
|
} else {
|
2012-11-19 08:30:30 +08:00
|
|
|
err = ENOTDIR;
|
|
|
|
}
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 12:15:43 +08:00
|
|
|
if (!success) errno = err;
|
2012-07-21 13:11:05 +08:00
|
|
|
return success;
|
2006-10-19 19:50:23 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 12:15:43 +08:00
|
|
|
bool path_can_be_implicit_cd(const wcstring &path, wcstring *out_path, const wchar_t *wd,
|
|
|
|
const env_vars_snapshot_t &vars) {
|
2012-06-03 05:04:25 +08:00
|
|
|
wcstring exp_path = path;
|
|
|
|
expand_tilde(exp_path);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-06-03 05:04:25 +08:00
|
|
|
bool result = false;
|
2016-05-03 12:15:43 +08: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"..") {
|
|
|
|
// 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).
|
2012-07-21 13:11:05 +08:00
|
|
|
result = path_get_cdpath(exp_path, out_path, wd, vars);
|
2012-06-03 05:04:25 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2012-02-09 03:48:51 +08:00
|
|
|
|
2016-05-03 12:15:43 +08: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 10:15:05 +08:00
|
|
|
bool prepend_wd = path.at(0) != L'/' && path.at(0) != HOME_DIRECTORY;
|
2016-05-03 12:15:43 +08:00
|
|
|
if (!prepend_wd) {
|
|
|
|
// No need to prepend the wd, so just return the path we were given.
|
2016-02-07 06:39:47 +08:00
|
|
|
return path;
|
2016-05-05 06:19:47 +08:00
|
|
|
}
|
2016-05-03 12:15:43 +08:00
|
|
|
|
2016-05-05 06:19:47 +08:00
|
|
|
// Remove up to one "./".
|
|
|
|
wcstring path_component = path;
|
|
|
|
if (string_prefixes_string(L"./", path_component)) {
|
|
|
|
path_component.erase(0, 2);
|
|
|
|
}
|
2016-05-03 12:15:43 +08:00
|
|
|
|
2016-05-05 06:19:47 +08:00
|
|
|
// Removing leading /s.
|
|
|
|
while (string_prefixes_string(L"/", path_component)) {
|
|
|
|
path_component.erase(0, 1);
|
2016-02-07 06:39:47 +08:00
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
|
|
|
|
// Construct and return a new path.
|
|
|
|
wcstring new_path = working_directory;
|
|
|
|
append_path_component(new_path, path_component);
|
|
|
|
return new_path;
|
2016-02-07 06:39:47 +08:00
|
|
|
}
|
|
|
|
|
2016-11-27 08:35:48 +08: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,
|
|
|
|
int saved_errno) {
|
|
|
|
wcstring warning_var_name = L"_FISH_WARNED_" + which_dir;
|
|
|
|
if (env_exist(warning_var_name.c_str(), ENV_GLOBAL | ENV_EXPORT)) {
|
|
|
|
return;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2016-11-27 08:35:48 +08:00
|
|
|
env_set(warning_var_name, L"1", ENV_GLOBAL | ENV_EXPORT);
|
|
|
|
|
|
|
|
debug(0, custom_error_msg.c_str());
|
|
|
|
if (path.empty()) {
|
|
|
|
debug(0, _(L"Unable to locate the %ls directory."), which_dir.c_str());
|
2017-05-02 12:44:30 +08:00
|
|
|
debug(0, _(L"Please set the %ls or HOME environment variable before starting fish."),
|
2016-11-27 08:35:48 +08:00
|
|
|
xdg_var.c_str());
|
|
|
|
} else {
|
|
|
|
const wchar_t *env_var = using_xdg ? xdg_var.c_str() : L"HOME";
|
|
|
|
debug(0, _(L"Unable to locate %ls directory derived from $%ls: '%ls'."), which_dir.c_str(),
|
|
|
|
env_var, path.c_str());
|
|
|
|
debug(0, _(L"The error was '%s'."), strerror(saved_errno));
|
|
|
|
debug(0, _(L"Please set $%ls to a directory where you have write access."), env_var);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2017-07-26 12:34:22 +08:00
|
|
|
(void)write(STDERR_FILENO, "\n", 1);
|
2012-12-03 18:25:08 +08:00
|
|
|
}
|
2012-02-06 08:42:24 +08:00
|
|
|
|
2016-11-27 08:35:48 +08:00
|
|
|
static void path_create(wcstring &path, const wcstring &xdg_var, const wcstring &which_dir,
|
|
|
|
const wcstring &custom_error_msg) {
|
|
|
|
bool path_done = false;
|
|
|
|
bool using_xdg = false;
|
|
|
|
int saved_errno = 0;
|
|
|
|
|
|
|
|
// 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.
|
2017-08-06 09:22:49 +08:00
|
|
|
const env_var_t xdg_dir = env_get(xdg_var, ENV_GLOBAL | ENV_EXPORT);
|
2016-11-27 08:35:48 +08:00
|
|
|
if (!xdg_dir.missing_or_empty()) {
|
|
|
|
using_xdg = true;
|
2017-08-06 09:22:49 +08:00
|
|
|
path = xdg_dir.as_string() + L"/fish";
|
2016-11-27 08:35:48 +08:00
|
|
|
if (create_directory(path) != -1) {
|
|
|
|
path_done = true;
|
|
|
|
} else {
|
|
|
|
saved_errno = errno;
|
2015-09-17 12:31:08 +08:00
|
|
|
}
|
2016-05-03 12:15:43 +08:00
|
|
|
} else {
|
2017-08-06 09:22:49 +08:00
|
|
|
const env_var_t home = env_get(L"HOME", ENV_GLOBAL | ENV_EXPORT);
|
2016-11-27 08:35:48 +08:00
|
|
|
if (!home.missing_or_empty()) {
|
2017-08-06 09:22:49 +08:00
|
|
|
path = home.as_string() +
|
|
|
|
(which_dir == L"config" ? L"/.config/fish" : L"/.local/share/fish");
|
2016-11-27 08:35:48 +08:00
|
|
|
if (create_directory(path) != -1) {
|
|
|
|
path_done = true;
|
|
|
|
} else {
|
|
|
|
saved_errno = errno;
|
2015-09-17 12:31:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 08:35:48 +08:00
|
|
|
if (!path_done) {
|
|
|
|
maybe_issue_path_warning(which_dir, custom_error_msg, using_xdg, xdg_var, path,
|
|
|
|
saved_errno);
|
|
|
|
path.clear();
|
2015-09-17 12:31:08 +08:00
|
|
|
}
|
2016-11-27 08:35:48 +08:00
|
|
|
|
|
|
|
return;
|
2015-09-17 12:31:08 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 12:15:43 +08:00
|
|
|
/// Cache the config path.
|
|
|
|
bool path_get_config(wcstring &path) {
|
2016-11-27 08:35:48 +08:00
|
|
|
static bool config_path_done = false;
|
|
|
|
static wcstring config_path(L"");
|
|
|
|
|
|
|
|
if (!config_path_done) {
|
|
|
|
path_create(config_path, L"XDG_CONFIG_HOME", L"config",
|
|
|
|
_(L"Your personal settings will not be saved."));
|
|
|
|
config_path_done = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
path = config_path;
|
|
|
|
return !config_path.empty();
|
2006-10-19 19:50:23 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 12:15:43 +08:00
|
|
|
/// Cache the data path.
|
|
|
|
bool path_get_data(wcstring &path) {
|
2016-11-27 08:35:48 +08:00
|
|
|
static bool data_path_done = false;
|
|
|
|
static wcstring data_path(L"");
|
|
|
|
|
|
|
|
if (!data_path_done) {
|
|
|
|
data_path_done = true;
|
|
|
|
path_create(data_path, L"XDG_DATA_HOME", L"data", _(L"Your history will not be saved."));
|
|
|
|
}
|
|
|
|
|
|
|
|
path = data_path;
|
|
|
|
return !data_path.empty();
|
2015-09-17 12:31:08 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 12:15:43 +08:00
|
|
|
void path_make_canonical(wcstring &path) {
|
|
|
|
// Ignore trailing slashes, unless it's the first character.
|
2013-08-28 09:26:22 +08:00
|
|
|
size_t len = path.size();
|
2016-05-03 12:15:43 +08:00
|
|
|
while (len > 1 && path.at(len - 1) == L'/') len--;
|
2013-08-28 09:26:22 +08:00
|
|
|
|
2016-05-03 12:15:43 +08:00
|
|
|
// Turn runs of slashes into a single slash.
|
2013-08-28 09:26:22 +08:00
|
|
|
size_t trailing = 0;
|
|
|
|
bool prev_was_slash = false;
|
2016-05-03 12:15:43 +08:00
|
|
|
for (size_t leading = 0; leading < len; leading++) {
|
2013-08-28 09:26:22 +08:00
|
|
|
wchar_t c = path.at(leading);
|
|
|
|
bool is_slash = (c == '/');
|
2016-05-03 12:15:43 +08: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 09:26:22 +08:00
|
|
|
path.at(trailing++) = c;
|
|
|
|
}
|
|
|
|
prev_was_slash = is_slash;
|
2013-10-27 06:27:39 +08:00
|
|
|
}
|
2013-08-28 09:26:22 +08:00
|
|
|
assert(trailing <= len);
|
2016-05-03 12:15:43 +08:00
|
|
|
if (trailing < len) path.resize(trailing);
|
2013-08-28 09:26:22 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:15:43 +08:00
|
|
|
bool paths_are_equivalent(const wcstring &p1, const wcstring &p2) {
|
|
|
|
if (p1 == p2) return true;
|
2013-10-27 06:27:39 +08:00
|
|
|
|
2013-08-28 09:26:22 +08:00
|
|
|
size_t len1 = p1.size(), len2 = p2.size();
|
2013-10-27 06:27:39 +08:00
|
|
|
|
2016-05-03 12:15:43 +08:00
|
|
|
// Ignore trailing slashes after the first character.
|
2013-08-28 09:26:22 +08:00
|
|
|
while (len1 > 1 && p1.at(len1 - 1) == L'/') len1--;
|
|
|
|
while (len2 > 1 && p2.at(len2 - 1) == L'/') len2--;
|
2013-10-27 06:27:39 +08:00
|
|
|
|
2013-08-28 09:26:22 +08:00
|
|
|
// Start walking
|
|
|
|
size_t idx1 = 0, idx2 = 0;
|
2016-05-03 12:15:43 +08:00
|
|
|
while (idx1 < len1 && idx2 < len2) {
|
2013-08-28 09:26:22 +08:00
|
|
|
wchar_t c1 = p1.at(idx1), c2 = p2.at(idx2);
|
2013-10-27 06:27:39 +08:00
|
|
|
|
2016-05-03 12:15:43 +08:00
|
|
|
// If the characters are different, the strings are not equivalent.
|
|
|
|
if (c1 != c2) break;
|
2013-10-27 06:27:39 +08:00
|
|
|
|
2013-08-28 09:26:22 +08:00
|
|
|
idx1++;
|
|
|
|
idx2++;
|
2013-10-27 06:27:39 +08:00
|
|
|
|
2016-05-03 12:15:43 +08: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 09:26:22 +08:00
|
|
|
while (c1 == L'/' && idx1 < len1 && p1.at(idx1) == L'/') idx1++;
|
|
|
|
while (c2 == L'/' && idx2 < len2 && p2.at(idx2) == L'/') idx2++;
|
2012-02-08 13:23:12 +08:00
|
|
|
}
|
2013-10-27 06:27:39 +08:00
|
|
|
|
2016-05-03 12:15:43 +08:00
|
|
|
// We matched if we consumed all of the characters in both strings.
|
2013-08-28 09:26:22 +08:00
|
|
|
return idx1 == len1 && idx2 == len2;
|
2007-05-11 03:11:28 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 12:15:43 +08:00
|
|
|
bool path_is_valid(const wcstring &path, const wcstring &working_directory) {
|
2012-02-19 10:54:36 +08:00
|
|
|
bool path_is_valid;
|
2016-05-03 12:15:43 +08:00
|
|
|
// Some special paths are always valid.
|
|
|
|
if (path.empty()) {
|
2012-02-19 10:54:36 +08:00
|
|
|
path_is_valid = false;
|
2016-05-03 12:15:43 +08:00
|
|
|
} else if (path == L"." || path == L"./") {
|
2012-02-19 10:54:36 +08:00
|
|
|
path_is_valid = true;
|
2016-05-03 12:15:43 +08: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 10:54:36 +08:00
|
|
|
wcstring tmp = working_directory;
|
|
|
|
tmp.append(path);
|
2012-07-21 13:11:05 +08:00
|
|
|
path_is_valid = (0 == waccess(tmp, F_OK));
|
2016-05-03 12:15:43 +08:00
|
|
|
} else {
|
|
|
|
// Simple check.
|
2012-07-21 13:11:05 +08:00
|
|
|
path_is_valid = (0 == waccess(path, F_OK));
|
2012-02-19 10:54:36 +08:00
|
|
|
}
|
|
|
|
return path_is_valid;
|
|
|
|
}
|
|
|
|
|
2016-05-03 12:15:43 +08:00
|
|
|
bool paths_are_same_file(const wcstring &path1, const wcstring &path2) {
|
|
|
|
if (paths_are_equivalent(path1, path2)) return true;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-02-19 13:56:30 +08:00
|
|
|
struct stat s1, s2;
|
2016-05-03 12:15:43 +08:00
|
|
|
if (wstat(path1, &s1) == 0 && wstat(path2, &s2) == 0) {
|
2012-02-19 13:56:30 +08:00
|
|
|
return s1.st_ino == s2.st_ino && s1.st_dev == s2.st_dev;
|
|
|
|
}
|
2016-10-29 08:42:50 +08:00
|
|
|
|
|
|
|
return false;
|
2012-02-19 13:56:30 +08:00
|
|
|
}
|