Skip to content

Commit

Permalink
(maint) Merge up 5d792ed to 1.6.x
Browse files Browse the repository at this point in the history
Generated by CI

* commit '5d792ed1ba2b3c3fe6b7e299c1b2927f5a17b4e3':
  (FACT-2054) Facter::Core::Execution.execute expands shell builtins
  • Loading branch information
puppetlabs-jenkins committed Nov 26, 2019
2 parents 3c44bae + 5d792ed commit 0936d52
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
8 changes: 5 additions & 3 deletions execution/inc/leatherman/execution/execution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,17 +252,19 @@ namespace leatherman { namespace execution {
* Searches the given paths for the given executable file.
* @param file The file to search for.
* @param directories The directories to search.
* @param expand default true - expand first command to absolute path if founded in @param directories
* @return Returns the full path or empty if the file could not be found.
*/
std::string which(std::string const& file, std::vector<std::string> const& directories = lth_util::environment::search_paths());
std::string which(std::string const& file, std::vector<std::string> const& directories = lth_util::environment::search_paths(), bool expand = true);

/**
* Expands the executable in the command to the full path.
* Expands the executable in the command to the full path, expand is true, otherwise does not expand
* @param command The command to expand.
* @param directories The directories to search.
* @param expand default true - expand first command to absolute path if founded in @param directories
* @return Returns the expanded command if the executable was found or empty if it was not found..
*/
std::string expand_command(std::string const& command, std::vector<std::string> const& directories = lth_util::environment::search_paths());
std::string expand_command(std::string const& command, std::vector<std::string> const& directories = lth_util::environment::search_paths(), bool expand = true);

/**
* Executes the given program.
Expand Down
4 changes: 2 additions & 2 deletions execution/src/execution.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ namespace leatherman { namespace execution {
LOG_DEBUG("executing command: {1}", command_line.str());
}

string expand_command(string const& command, vector<string> const& directories)
string expand_command(string const& command, vector<string> const& directories, bool expand)
{
string result = command;
boost::trim(result);
Expand Down Expand Up @@ -126,7 +126,7 @@ namespace leatherman { namespace execution {
}
}

file = which(file, directories);
file = which(file, directories, expand);
if (file.empty()) {
return {};
}
Expand Down
28 changes: 27 additions & 1 deletion execution/src/posix/execution.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ using namespace leatherman::util::posix;
using namespace leatherman::execution;
using namespace leatherman::logging;
using namespace leatherman::file_util;
using namespace boost::algorithm;
using namespace boost::filesystem;

// Declare environ for OSX
Expand Down Expand Up @@ -139,8 +140,33 @@ namespace leatherman { namespace execution {
return fs.st_mode & S_IXOTH;
}

string which(string const& file, vector<string> const& directories)
bool is_builtin(string const& file)
{
string data;
string cmd = "type ";
cmd.append(file);
const int buffer_offset = 25;
const int max_buffer = buffer_offset + file.length();
char buffer[max_buffer];
FILE *stream = popen(cmd.c_str(), "r");
if (stream != nullptr) {
rewind(stream);
if (fgets(buffer, max_buffer, stream) != NULL){
data.append(buffer);
}
pclose(stream);
}

return contains(data, "builtin");
}

string which(string const& file, vector<string> const& directories, bool expand)
{
// if it is builtin, just return it
if ( !expand && is_builtin(file) ) {
return file;
}

// If the file is already absolute, return it if it's executable
path p = file;
boost::system::error_code ec;
Expand Down
2 changes: 1 addition & 1 deletion execution/src/windows/execution.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace leatherman { namespace execution {
return isfile;
}

string which(string const& file, vector<string> const& directories)
string which(string const& file, vector<string> const& directories, bool expand)
{
// On Windows, everything has execute permission; Ruby determined
// executability based on extension {com, exe, bat, cmd}. We'll do the
Expand Down
18 changes: 18 additions & 0 deletions execution/tests/posix/execution.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ SCENARIO("expanding command paths with execution::expand_command") {
REQUIRE(expand_command("not_executable", { EXEC_TESTS_DIRECTORY "/fixtures" }) == "");
}
}
GIVEN("set expand to false with a built-in executable") {
THEN("the executable is not expanded to an absolute path") {
boost::format fmt = boost::format("cd %1% && ls") % EXEC_TESTS_DIRECTORY;
std::string fmtStr = boost::str(fmt);
vector<string> lines;
string command = expand_command(fmtStr, lines, false );
REQUIRE( command == "cd " EXEC_TESTS_DIRECTORY " && ls");
}
}
GIVEN("set expand to true with a built-in executable") {
THEN("the executable absolute path is not found") {
boost::format fmt = boost::format("cd %1% && ls") % EXEC_TESTS_DIRECTORY;
std::string fmtStr = boost::str(fmt);
vector<string> lines;
string command = expand_command(fmtStr, lines, true );
REQUIRE( command == "");
}
}
}

SCENARIO("executing commands with execution::execute") {
Expand Down

0 comments on commit 0936d52

Please sign in to comment.