From c45cd46319db705d8337286a109ad7c03c29705f Mon Sep 17 00:00:00 2001 From: TheAssassin Date: Tue, 19 Nov 2019 11:28:21 +0100 Subject: [PATCH] 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. --- include/linuxdeploy/util/misc.h | 31 +++++++++++++++++++++++++++++++ src/core/copyright/copyright.cpp | 32 ++------------------------------ 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/include/linuxdeploy/util/misc.h b/include/linuxdeploy/util/misc.h index 6b38eb2..1db862c 100644 --- a/include/linuxdeploy/util/misc.h +++ b/include/linuxdeploy/util/misc.h @@ -1,5 +1,6 @@ #pragma once +// system headers #include #include #include @@ -8,6 +9,9 @@ #include #include +// libraries +#include + 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 {}; + }; } } } diff --git a/src/core/copyright/copyright.cpp b/src/core/copyright/copyright.cpp index e9481c9..1bf5596 100644 --- a/src/core/copyright/copyright.cpp +++ b/src/core/copyright/copyright.cpp @@ -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::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(); }