2018-06-19 18:02:23 +02:00
|
|
|
// system headers
|
|
|
|
|
#include <set>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
// library headers
|
|
|
|
|
#include <boost/filesystem.hpp>
|
2018-06-19 20:46:53 +02:00
|
|
|
#include <boost/regex.hpp>
|
2018-06-19 18:02:23 +02:00
|
|
|
#include <fnmatch.h>
|
|
|
|
|
#include <subprocess.hpp>
|
|
|
|
|
|
|
|
|
|
// local headers
|
|
|
|
|
#include "linuxdeploy/core/log.h"
|
|
|
|
|
#include "linuxdeploy/plugin/base.h"
|
|
|
|
|
#include "linuxdeploy/plugin/plugin.h"
|
2018-08-03 00:55:48 +02:00
|
|
|
#include "linuxdeploy/util/util.h"
|
2018-06-19 18:02:23 +02:00
|
|
|
#include "plugin_type0.h"
|
|
|
|
|
|
|
|
|
|
using namespace linuxdeploy::core;
|
|
|
|
|
using namespace linuxdeploy::core::log;
|
|
|
|
|
|
|
|
|
|
namespace bf = boost::filesystem;
|
|
|
|
|
|
|
|
|
|
namespace linuxdeploy {
|
|
|
|
|
namespace plugin {
|
|
|
|
|
IPlugin* createPluginInstance(const boost::filesystem::path& path) {
|
|
|
|
|
IPlugin* rv = nullptr;
|
|
|
|
|
|
|
|
|
|
// test whether it's a type 0 plugin
|
|
|
|
|
try {
|
|
|
|
|
rv = new Type0Plugin(path);
|
|
|
|
|
} catch (const WrongApiLevelError& e) {
|
|
|
|
|
ldLog() << LD_DEBUG << e.what() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
2018-06-19 20:46:53 +02:00
|
|
|
|
2018-06-19 22:47:01 +02:00
|
|
|
std::map<std::string, IPlugin*> findPlugins() {
|
|
|
|
|
std::map<std::string, IPlugin*> foundPlugins;
|
2018-06-19 20:46:53 +02:00
|
|
|
|
|
|
|
|
const auto PATH = getenv("PATH");
|
|
|
|
|
|
2018-06-20 01:34:08 +02:00
|
|
|
auto paths = util::split(PATH, ':');
|
|
|
|
|
|
|
|
|
|
auto currentExeDir = bf::path(util::getOwnExecutablePath()).parent_path();
|
|
|
|
|
paths.insert(paths.begin(), currentExeDir.string());
|
|
|
|
|
|
2018-06-20 21:26:48 +02:00
|
|
|
// if shipping as an AppImage, search for plugins in AppImage's location, too
|
|
|
|
|
if (getenv("APPIMAGE") != nullptr) {
|
|
|
|
|
auto appImageDir = bf::path(getenv("APPIMAGE")).parent_path();
|
|
|
|
|
paths.insert(paths.begin(), appImageDir.string());
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 01:34:08 +02:00
|
|
|
for (const auto& dir : paths) {
|
2018-06-19 20:46:53 +02:00
|
|
|
if (!bf::is_directory(dir))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
ldLog() << LD_DEBUG << "Searching for plugins in directory" << dir << std::endl;
|
|
|
|
|
|
|
|
|
|
for (bf::directory_iterator i(dir); i != bf::directory_iterator(); ++i) {
|
|
|
|
|
// file must be executable...
|
|
|
|
|
if (bf::status(*i).permissions() & (bf::owner_exe | bf::group_exe | bf::others_exe)) {
|
|
|
|
|
// ... and filename must match regular expression
|
2018-06-19 22:47:01 +02:00
|
|
|
boost::cmatch res;
|
2018-07-10 15:06:21 +02:00
|
|
|
if (boost::regex_match(i->path().filename().string().c_str(), res, PLUGIN_EXPR)) {
|
2018-06-19 20:46:53 +02:00
|
|
|
try {
|
2018-06-19 22:47:01 +02:00
|
|
|
auto name = res[1].str();
|
2018-06-19 20:46:53 +02:00
|
|
|
auto* plugin = createPluginInstance(*i);
|
2018-06-20 20:42:24 +02:00
|
|
|
if (plugin == nullptr) {
|
|
|
|
|
ldLog() << LD_DEBUG << "Failed to create instance for plugin" << i->path();
|
|
|
|
|
} else {
|
|
|
|
|
ldLog() << LD_DEBUG << "Found plugin '" << LD_NO_SPACE << name << LD_NO_SPACE << "':" << plugin->path() << std::endl;
|
|
|
|
|
foundPlugins[name] = plugin;
|
|
|
|
|
}
|
2018-06-19 20:46:53 +02:00
|
|
|
} catch (const PluginError& e) {
|
2018-06-20 20:42:24 +02:00
|
|
|
ldLog() << LD_WARNING << "Could not load plugin" << i->path() << LD_NO_SPACE << ": " << e.what();
|
2018-06-19 20:46:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return foundPlugins;
|
|
|
|
|
}
|
2018-06-19 18:02:23 +02:00
|
|
|
}
|
|
|
|
|
}
|