Move which to utils module

Also uses the native split functionality provided by the utils module
instead of some external dependency's.

Preparation for using this method in plugins such as the Qt plugin.
This commit is contained in:
TheAssassin
2019-11-19 11:28:21 +01:00
parent 1bd5453c61
commit c45cd46319
2 changed files with 33 additions and 30 deletions

View File

@@ -1,5 +1,6 @@
#pragma once
// system headers
#include <algorithm>
#include <climits>
#include <cstring>
@@ -8,6 +9,9 @@
#include <unistd.h>
#include <vector>
// libraries
#include <boost/filesystem.hpp>
namespace linuxdeploy {
namespace util {
namespace misc {
@@ -87,6 +91,33 @@ namespace linuxdeploy {
return buf.data();
}
// very simple but for our purposes good enough which like algorithm to find binaries in $PATH
static boost::filesystem::path which(const std::string& name) {
const auto* path = getenv("PATH");
namespace bf = boost::filesystem;
if (path == nullptr)
return "";
for (const auto& binDir : split(path, ':')) {
if (!bf::is_directory(binDir)) {
continue;
}
for (bf::directory_iterator it(binDir); it != bf::directory_iterator{}; ++it) {
const auto binary = it->path();
if (binary.filename() == name) {
// TODO: check if file is executable (skip otherwise)
return binary;
}
}
}
return {};
};
}
}
}

View File

@@ -1,5 +1,6 @@
// local includes
#include "linuxdeploy/core/log.h"
#include "linuxdeploy/util/util.h"
#include "copyright.h"
// specializations
@@ -11,36 +12,7 @@ namespace linuxdeploy {
using namespace log;
std::shared_ptr<ICopyrightFilesManager> ICopyrightFilesManager::getInstance() {
// very simple but for our purposes good enough which like algorithm to find binaries in $PATH
// TODO: move into separate function to be used in the entire code base
// FIXME: remove dependency on subprocess library
static const auto which = [](const std::string& name) -> bf::path {
const auto* path = getenv("PATH");
if (path == nullptr)
return "";
for (const auto& binDir : subprocess::util::split(path, ":")) {
if (!bf::is_directory(binDir)) {
continue;
}
for (bf::directory_iterator it(binDir); it != bf::directory_iterator{}; ++it) {
const auto binary = it->path();
std::cout << binary << std::endl;
if (binary.filename() == name) {
// TODO: check if file is executable (skip otherwise)
return binary;
}
}
}
return {};
};
if (!which("dpkg-query").empty()) {
if (!util::which("dpkg-query").empty()) {
ldLog() << LD_DEBUG << "Using dpkg-query to search for copyright files" << std::endl;
return std::make_shared<DpkgQueryCopyrightFilesManager>();
}