From 14cfd268d89adb2d6ba26d3786d8303651df2912 Mon Sep 17 00:00:00 2001 From: David Adam Date: Wed, 21 Jun 2023 21:39:45 +0800 Subject: [PATCH] path: drop path_get_paths_ffi f77dc24 provides the pieces to call path_get_paths directly from Rust code. Drop the C++ implementation and its FFI. --- fish-rust/src/builtins/command.rs | 5 ++-- fish-rust/src/builtins/type.rs | 5 ++-- fish-rust/src/ffi.rs | 1 - src/path.cpp | 40 ------------------------------- src/path.h | 6 ----- 5 files changed, 5 insertions(+), 52 deletions(-) diff --git a/fish-rust/src/builtins/command.rs b/fish-rust/src/builtins/command.rs index 1ac1dc407..89ffed8e6 100644 --- a/fish-rust/src/builtins/command.rs +++ b/fish-rust/src/builtins/command.rs @@ -5,9 +5,8 @@ use crate::builtins::shared::{ STATUS_CMD_OK, STATUS_CMD_UNKNOWN, STATUS_INVALID_ARGS, }; use crate::ffi::parser_t; -use crate::ffi::path_get_paths_ffi; +use crate::path::path_get_paths; use crate::wchar::{wstr, WString, L}; -use crate::wchar_ffi::{WCharFromFFI, WCharToFFI}; use crate::wgetopt::{wgetopter_t, wopt, woption, woption_argument_t}; use crate::wutil::sprintf; @@ -75,7 +74,7 @@ pub fn r#command( // TODO: This always gets all paths, and then skips a bunch. // For the common case, we want to get just the one path. // Port this over once path.cpp is. - let paths: Vec = path_get_paths_ffi(&arg.to_ffi(), parser).from_ffi(); + let paths: Vec = path_get_paths(arg, &*parser.get_vars()); for path in paths.iter() { res = true; diff --git a/fish-rust/src/builtins/type.rs b/fish-rust/src/builtins/type.rs index 761ae4e82..2e6553cee 100644 --- a/fish-rust/src/builtins/type.rs +++ b/fish-rust/src/builtins/type.rs @@ -12,8 +12,9 @@ use crate::ffi::{ builtin_exists, colorize_shell, function_get_annotated_definition, function_get_copy_definition_file, function_get_copy_definition_lineno, function_get_definition_file, function_get_definition_lineno, function_get_props_autoload, - function_is_copy, path_get_paths_ffi, + function_is_copy, }; +use crate::path::path_get_paths; use crate::wchar::{wstr, WString, L}; use crate::wchar_ffi::WCharFromFFI; use crate::wchar_ffi::WCharToFFI; @@ -189,7 +190,7 @@ pub fn r#type( } } - let paths: Vec = path_get_paths_ffi(&arg.to_ffi(), parser).from_ffi(); + let paths: Vec = path_get_paths(arg, &*parser.get_vars()); for path in paths.iter() { found += 1; diff --git a/fish-rust/src/ffi.rs b/fish-rust/src/ffi.rs index 55aa83a79..a3491d94a 100644 --- a/fish-rust/src/ffi.rs +++ b/fish-rust/src/ffi.rs @@ -132,7 +132,6 @@ include_cpp! { generate!("function_get_annotated_definition") generate!("function_is_copy") generate!("function_exists") - generate!("path_get_paths_ffi") generate!("rgb_color_t") generate_pod!("color24_t") diff --git a/src/path.cpp b/src/path.cpp index 4a4b8c673..05af907a2 100644 --- a/src/path.cpp +++ b/src/path.cpp @@ -97,16 +97,6 @@ get_path_result_t path_try_get_path(const wcstring &cmd, const environment_t &va return path_get_path_core(cmd, pathvar ? pathvar->as_list() : kDefaultPath); } -static bool path_is_executable(const std::string &path) { - 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; - } - return S_ISREG(buff.st_mode); -} - /// \return whether the given path is on a remote filesystem. static dir_remoteness_t path_remoteness(const wcstring &path) { std::string narrow = wcs2zstring(path); @@ -143,36 +133,6 @@ static dir_remoteness_t path_remoteness(const wcstring &path) { #endif } -std::vector path_get_paths(const wcstring &cmd, const environment_t &vars) { - FLOGF(path, L"path_get_paths('%ls')", cmd.c_str()); - std::vector 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) { - std::string narrow = wcs2zstring(cmd); - if (path_is_executable(narrow)) paths.push_back(cmd); - return paths; - } - - auto path_var = vars.get(L"PATH"); - if (!path_var) return paths; - - const std::vector &pathsv = path_var->as_list(); - for (auto path : pathsv) { - if (path.empty()) continue; - append_path_component(path, cmd); - std::string narrow = wcs2zstring(path); - if (path_is_executable(narrow)) paths.push_back(path); - } - - return paths; -} - -wcstring_list_ffi_t path_get_paths_ffi(const wcstring &cmd, const parser_t &parser) { - return path_get_paths(cmd, parser.vars()); -} - std::vector path_apply_cdpath(const wcstring &dir, const wcstring &wd, const environment_t &env_vars) { std::vector paths; diff --git a/src/path.h b/src/path.h index 0e9fa04f1..91a7504b5 100644 --- a/src/path.h +++ b/src/path.h @@ -63,12 +63,6 @@ struct get_path_result_t { }; get_path_result_t path_try_get_path(const wcstring &cmd, const environment_t &vars); -/// Return all the paths that match the given command. -std::vector path_get_paths(const wcstring &cmd, const environment_t &vars); - -// Needed because of issues with vectors of wstring and environment_t. -wcstring_list_ffi_t path_get_paths_ffi(const wcstring &cmd, const parser_t &parser); - /// Returns the full path of the specified directory, using the CDPATH variable as a list of base /// directories for relative paths. ///