-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathbase_impl.h
More file actions
162 lines (134 loc) · 6.49 KB
/
base_impl.h
File metadata and controls
162 lines (134 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// system headers
#include <fcntl.h>
#include <filesystem>
#include <poll.h>
#include <set>
#include <string>
#include <vector>
// library headers
#include <fnmatch.h>
#include <thread>
// local headers
#include "linuxdeploy/log/log.h"
#include "linuxdeploy/util/util.h"
#include "linuxdeploy/subprocess/process.h"
#include "linuxdeploy/plugin/plugin_process_handler.h"
#pragma once
// implementation of PluginBase in a header to solve issues like
// https://bytefreaks.net/programming-2/c/c-undefined-reference-to-templated-class-function
namespace linuxdeploy {
namespace plugin {
namespace base {
using namespace linuxdeploy::log;
template<int API_LEVEL>
class PluginBase<API_LEVEL>::PrivateData {
public:
const std::filesystem::path pluginPath;
std::string name;
int apiLevel;
PLUGIN_TYPE pluginType;
public:
explicit PrivateData(const std::filesystem::path& path) : pluginPath(path) {
if (!std::filesystem::exists(path)) {
throw PluginError("No such file or directory: " + path.string());
}
ldLog() << LD_DEBUG << "Probing plugin" << path.string() << std::endl;
apiLevel = getApiLevelFromExecutable();
pluginType = getPluginTypeFromExecutable();
std::cmatch res;
std::regex_match(path.filename().c_str(), res, PLUGIN_EXPR);
name = res[1].str();
};
private:
subprocess::subprocess_env_map_t getFixedEnvironment() {
auto rv = subprocess::get_environment();
rv.erase("VERBOSE");
return rv;
}
int getApiLevelFromExecutable() {
const auto arg = "--plugin-api-version";
// during plugin detection, we must make sure $VERBOSE is not passed to the AppImage runtime
// otherwise, in combination with $APPIMAGE_EXTRACT_AND_RUN, the runtime will spam the file
// extraction messages, which makes parsing the output very hard
const subprocess::subprocess proc({pluginPath.string(), arg}, getFixedEnvironment());
const auto stdoutOutput = proc.check_output();
if (stdoutOutput.empty()) {
ldLog() << LD_WARNING << "received empty response from plugin" << pluginPath << "while trying to fetch data for" << "--plugin-api-version" << std::endl;
return -1;
}
if (getenv("DEBUG_PLUGIN_DETECTION")) {
ldLog() << LD_DEBUG << "output from plugin:" << stdoutOutput << std::endl;
}
try {
const int parsedApiLevel = std::stoi(stdoutOutput);
return parsedApiLevel;
} catch (const std::exception&) {
return -1;
}
}
PLUGIN_TYPE getPluginTypeFromExecutable() {
// assume input type
auto type = INPUT_TYPE;
// check whether plugin implements --plugin-type
try {
// during plugin detection, we must make sure $VERBOSE is not passed to the AppImage runtime
// otherwise, in combination with $APPIMAGE_EXTRACT_AND_RUN, the runtime will spam the file
// extraction messages, which makes parsing the output very hard
const subprocess::subprocess proc({pluginPath.c_str(), "--plugin-type"}, getFixedEnvironment());
const auto stdoutOutput = proc.check_output();
// the specification requires a single line, but we'll silently accept more than that, too
if (std::count(stdoutOutput.begin(), stdoutOutput.end(), '\n') >= 1) {
auto firstLine = stdoutOutput.substr(0, stdoutOutput.find_first_of('\n'));
if (firstLine == "input")
type = INPUT_TYPE;
else if (firstLine == "output")
type = OUTPUT_TYPE;
}
} catch (const std::logic_error&) {}
return type;
}
};
template<int API_LEVEL>
PluginBase<API_LEVEL>::PluginBase(const std::filesystem::path& path) : IPlugin(path) {
d = new PrivateData(path);
if (d->apiLevel != API_LEVEL) {
std::stringstream msg;
msg << "This class only supports API level " << API_LEVEL << ", not " << d->apiLevel;
throw WrongApiLevelError(msg.str());
}
}
template<int API_LEVEL>
PluginBase<API_LEVEL>::~PluginBase() {
delete d;
}
template<int API_LEVEL>
std::filesystem::path PluginBase<API_LEVEL>::path() const {
return d->pluginPath;
}
template<int API_LEVEL>
PLUGIN_TYPE PluginBase<API_LEVEL>::pluginType() const {
return d->pluginType;
}
template<int API_LEVEL>
std::string PluginBase<API_LEVEL>::pluginTypeString() const {
switch ((int) d->pluginType) {
case INPUT_TYPE:
return "input";
case OUTPUT_TYPE:
return "output";
default:
return "<unknown>";
}
}
template<int API_LEVEL>
int PluginBase<API_LEVEL>::apiLevel() const {
return d->apiLevel;
}
template<int API_LEVEL>
int PluginBase<API_LEVEL>::run(const std::filesystem::path& appDirPath) {
plugin_process_handler handler(d->name, path());
return handler.run(appDirPath);
}
}
}
}