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.
|
2006-10-19 11:50:23 +00:00
|
|
|
#ifndef FISH_PATH_H
|
|
|
|
#define FISH_PATH_H
|
|
|
|
|
2022-08-21 06:14:48 +00:00
|
|
|
#include <string>
|
2016-04-21 06:00:54 +00:00
|
|
|
|
2015-07-25 15:14:25 +00:00
|
|
|
#include "common.h"
|
2022-08-21 06:14:48 +00:00
|
|
|
#include "maybe.h"
|
2012-07-21 05:11:05 +00:00
|
|
|
|
2016-05-03 04:15:43 +00:00
|
|
|
/// 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
|
2012-02-06 00:42:24 +00:00
|
|
|
bool path_get_config(wcstring &path);
|
2006-10-19 11:50:23 +00:00
|
|
|
|
2016-05-03 04:15:43 +00:00
|
|
|
/// 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
|
2015-09-17 04:31:08 +00:00
|
|
|
bool path_get_data(wcstring &path);
|
|
|
|
|
2021-12-19 04:39:00 +00:00
|
|
|
enum class dir_remoteness_t {
|
|
|
|
unknown, // directory status is unknown
|
|
|
|
local, // directory is known local
|
|
|
|
remote, // directory is known remote
|
|
|
|
};
|
2021-05-09 21:47:10 +00:00
|
|
|
|
2021-12-19 04:39:00 +00:00
|
|
|
/// \return the remoteness of the fish data directory.
|
|
|
|
/// This will be remote for filesystems like NFS, SMB, etc.
|
|
|
|
dir_remoteness_t path_get_data_remoteness();
|
|
|
|
|
|
|
|
/// Like path_get_data_remoteness but for the config directory.
|
|
|
|
dir_remoteness_t path_get_config_remoteness();
|
2021-05-10 22:10:52 +00:00
|
|
|
|
2022-08-21 06:14:48 +00:00
|
|
|
class env_stack_t;
|
2019-05-02 00:31:22 +00:00
|
|
|
/// Emit any errors if config directories are missing.
|
|
|
|
/// Use the given environment stack to ensure this only occurs once.
|
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
|
|
|
|
2022-08-21 06:14:48 +00:00
|
|
|
class environment_t;
|
2022-04-23 22:17:14 +00:00
|
|
|
/// Finds the path of an executable named \p cmd, by looking in $PATH taken from \p vars.
|
|
|
|
/// \returns the path if found, none if not.
|
|
|
|
maybe_t<wcstring> path_get_path(const wcstring &cmd, const environment_t &vars);
|
|
|
|
|
|
|
|
/// 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);
|
2012-06-02 21:04:25 +00:00
|
|
|
|
2017-06-23 22:42:38 +00:00
|
|
|
/// Return all the paths that match the given command.
|
2018-09-25 02:26:46 +00:00
|
|
|
wcstring_list_t path_get_paths(const wcstring &cmd, const environment_t &vars);
|
2017-06-23 22:42:38 +00:00
|
|
|
|
2016-05-03 04:15:43 +00:00
|
|
|
/// Returns the full path of the specified directory, using the CDPATH variable as a list of base
|
2018-09-16 11:05:17 +00:00
|
|
|
/// directories for relative paths.
|
2016-05-03 04:15:43 +00:00
|
|
|
///
|
2018-09-16 11:05:17 +00:00
|
|
|
/// If no valid path is found, false is returned and errno is set to ENOTDIR if at least one such
|
2021-09-05 00:11:21 +00:00
|
|
|
/// path was found, but it did not point to a directory, or ENOENT if no file of the specified
|
|
|
|
/// name was found.
|
2016-05-03 04:15:43 +00:00
|
|
|
///
|
|
|
|
/// \param dir The name of the directory.
|
2019-10-17 19:15:43 +00:00
|
|
|
/// \param wd The working directory. The working directory must end with a slash.
|
2018-09-16 11:05:17 +00:00
|
|
|
/// \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,
|
|
|
|
const environment_t &vars);
|
2012-02-08 19:48:51 +00:00
|
|
|
|
2021-03-27 17:28:03 +00:00
|
|
|
/// Returns the given directory with all CDPATH components applied.
|
|
|
|
wcstring_list_t path_apply_cdpath(const wcstring &dir, const wcstring &wd,
|
|
|
|
const environment_t &env_vars);
|
|
|
|
|
2018-09-16 11:05:17 +00:00
|
|
|
/// Returns the path resolved as an implicit cd command, or none() if none. This requires it to
|
|
|
|
/// start with one of the allowed prefixes (., .., ~) and resolve to a directory.
|
|
|
|
maybe_t<wcstring> path_as_implicit_cd(const wcstring &path, const wcstring &wd,
|
|
|
|
const environment_t &vars);
|
2006-10-19 11:50:23 +00:00
|
|
|
|
2016-05-03 04:15:43 +00:00
|
|
|
/// Remove double slashes and trailing slashes from a path, e.g. transform foo//bar/ into foo/bar.
|
|
|
|
/// The string is modified in-place.
|
2012-11-19 00:30:30 +00:00
|
|
|
void path_make_canonical(wcstring &path);
|
2007-05-10 19:11:28 +00:00
|
|
|
|
2016-05-03 04:15:43 +00:00
|
|
|
/// Check if two paths are equivalent, which means to ignore runs of multiple slashes (or trailing
|
|
|
|
/// slashes).
|
2013-08-28 01:26:22 +00:00
|
|
|
bool paths_are_equivalent(const wcstring &p1, const wcstring &p2);
|
|
|
|
|
2012-02-19 02:54:36 +00:00
|
|
|
bool path_is_valid(const wcstring &path, const wcstring &working_directory);
|
|
|
|
|
2016-05-03 04:15:43 +00:00
|
|
|
/// Returns whether the two paths refer to the same file.
|
2012-02-19 05:56:30 +00:00
|
|
|
bool paths_are_same_file(const wcstring &path1, const wcstring &path2);
|
|
|
|
|
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 ~).
|
2016-02-06 22:39:47 +00:00
|
|
|
wcstring path_apply_working_directory(const wcstring &path, const wcstring &working_directory);
|
|
|
|
|
2020-01-15 21:16:43 +00:00
|
|
|
/// Appends a path component, with a / if necessary.
|
|
|
|
void append_path_component(wcstring &path, const wcstring &component);
|
|
|
|
|
2006-10-19 11:50:23 +00:00
|
|
|
#endif
|