Skip to content

Commit b1c992a

Browse files
committed
Refine plugin factory discovery
1 parent ee07cce commit b1c992a

6 files changed

Lines changed: 119 additions & 51 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ for (auto loader : factory.plugins("org.example.Renderer")) {
141141

142142
Discovery has the following extension and lifetime rules:
143143

144-
- `BundlePluginFactory` provides the common external-metadata layout: every immediate child directory is a bundle, its root `name` selects the platform library name, and its constructor can replace `plugin.json` with another metadata file name.
144+
- `BundlePluginFactory` provides the common external-metadata layout: every immediate child directory is a bundle, its root `name` selects the platform library name, and its constructor can replace `plugin.json` with another relative metadata path such as `metadata/plugin.json`.
145145
- Subclasses can override `scanPluginPaths()` and `resolvePluginPath()` to implement another directory layout or external metadata policy.
146146
- Replacing search paths discards previously discovered filesystem loaders that are not loaded.
147147
- Loaded plugins are not unloaded when search paths change.
@@ -243,7 +243,7 @@ A plugin with compatibility version `C` and current version `V` satisfies a requ
243243

244244
- `Flat` is the default. It puts plugin libraries directly below each search path and reads their embedded metadata.
245245
- `Bundle` gives every plugin a child directory containing its library and a sidecar `plugin.json`. The metadata must contain a root `name` without a platform library prefix or suffix. For example, `"name": "editor"` can resolve to `editor.dll`, `libeditor.dll`, `libeditor.so`, or `libeditor.dylib`.
246-
- `CustomLayout` is reported when the constructor receives a user-provided `PluginFactory`. Its `scanPluginPaths()` and `resolvePluginPath()` overrides can implement recursive packages, another metadata file name, or a separate library subdirectory. `PluginSystem` accepts only filesystem plugins returned by this factory.
246+
- `CustomLayout` is reported when the constructor receives a user-provided `PluginFactory`. Its `scanPluginPaths()` and `resolvePluginPath()` overrides can implement recursive packages, another metadata path, or a separate library subdirectory. `PluginSystem` accepts only filesystem plugins returned by this factory.
247247

248248
In Bundle layout, `name` belongs to `BundlePluginFactory`, while `displayName` belongs to `PluginSystem`. They happen to coexist in the same root metadata object and do not describe the same thing.
249249

‎include/stdcorelib/plugin/pluginfactory.h‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,28 +73,31 @@ namespace stdc::plugin {
7373
/// Finds candidate plugin paths under a registered search directory.
7474
///
7575
/// The default implementation examines each library file directly under \a path and
76-
/// silently ignores files without embedded plugin metadata.
76+
/// silently ignores files without embedded plugin metadata or whose IID differs from
77+
/// \a iid.
7778
///
79+
/// \param iid The IID currently being discovered.
7880
/// \param path The directory registered with \c addPluginPath().
7981
/// \param pluginPaths Receives the candidate paths to resolve.
8082
/// \return Whether the directory was scanned successfully.
8183
/// \warning This is called while the factory is locked. An override must not call any
8284
/// function on this factory.
83-
virtual bool scanPluginPaths(const std::filesystem::path &path,
85+
virtual bool scanPluginPaths(std::string_view iid, const std::filesystem::path &path,
8486
std::vector<std::filesystem::path> *pluginPaths) const;
8587

8688
/// Resolves a candidate into the paths passed to \c PluginLoader::setFilePath().
8789
///
8890
/// The default implementation returns \a path unchanged and clears \a metadataPath, which
8991
/// makes the loader use its embedded metadata.
9092
///
93+
/// \param iid The IID currently being discovered.
9194
/// \param path A candidate returned by \c scanPluginPaths().
9295
/// \param pluginPath Receives the plugin library path.
9396
/// \param metadataPath Receives an optional external metadata JSON path.
9497
/// \return Whether the candidate was resolved successfully.
9598
/// \warning This is called while the factory is locked. An override must not call any
9699
/// function on this factory.
97-
virtual bool resolvePluginPath(const std::filesystem::path &path,
100+
virtual bool resolvePluginPath(std::string_view iid, const std::filesystem::path &path,
98101
std::filesystem::path *pluginPath,
99102
std::optional<std::filesystem::path> *metadataPath) const;
100103

@@ -114,24 +117,25 @@ namespace stdc::plugin {
114117
/// \c editor.dll, \c libeditor.so, or \c libeditor.dylib.
115118
class STDC_PLUGIN_EXPORT BundlePluginFactory : public PluginFactory {
116119
public:
117-
/// Creates a bundle factory with a configurable metadata file name.
120+
/// Creates a bundle factory with a configurable metadata path.
118121
///
119-
/// \param metadataFileName The metadata file name inside each bundle.
122+
/// \param metadataFileName The metadata path relative to each bundle.
120123
///
121-
/// \pre \a metadataFileName is nonempty and contains no directory components.
124+
/// \pre \a metadataFileName is nonempty, relative, and does not contain \c .. components.
122125
explicit BundlePluginFactory(std::filesystem::path metadataFileName = "plugin.json");
123126
~BundlePluginFactory() override;
124127

125128
BundlePluginFactory(BundlePluginFactory &&RHS) noexcept;
126129
BundlePluginFactory &operator=(BundlePluginFactory &&RHS) noexcept;
127130

128-
/// The metadata file name inside each bundle.
131+
/// The metadata path relative to each bundle.
129132
const std::filesystem::path &metadataFileName() const;
130133

131134
protected:
132-
bool scanPluginPaths(const std::filesystem::path &path,
135+
bool scanPluginPaths(std::string_view iid, const std::filesystem::path &path,
133136
std::vector<std::filesystem::path> *pluginPaths) const override;
134-
bool resolvePluginPath(const std::filesystem::path &path, std::filesystem::path *pluginPath,
137+
bool resolvePluginPath(std::string_view iid, const std::filesystem::path &path,
138+
std::filesystem::path *pluginPath,
135139
std::optional<std::filesystem::path> *metadataPath) const override;
136140

137141
/// Resolves the plugin library described by bundle metadata.

‎include/stdcorelib/plugin/pluginloader.h‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ namespace stdc::plugin {
2020
/// \addtogroup plugin
2121
/// @{
2222

23-
class PluginFactory;
24-
2523
/// Reads metadata and manages one plugin.
2624
///
2725
/// Filesystem metadata is read without executing plugin code. The library is not loaded until
@@ -146,8 +144,6 @@ namespace stdc::plugin {
146144
class Impl;
147145
std::unique_ptr<Impl> _impl;
148146

149-
friend class PluginFactory;
150-
151147
STDC_DISABLE_COPY(PluginLoader)
152148
};
153149

‎src/plugin/pluginfactory.cpp‎

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
#include <stdcorelib/stlextra/algorithms.h>
1717
#include <stdcorelib/support/sharedlibrary.h>
1818

19-
#include "pluginloader_p.h"
20-
2119
namespace fs = std::filesystem;
2220

2321
STDC_INSTANTIATE_STATIC_REGISTRY_EXPORT(stdc::plugin::StaticPlugin, STDC_PLUGIN_EXPORT)
@@ -26,8 +24,13 @@ namespace stdc::plugin {
2624

2725
namespace {
2826

29-
bool is_file_name(const fs::path &path) {
30-
return !path.empty() && path != "." && path != ".." && path == path.filename();
27+
bool is_relative_file_path(const fs::path &path) {
28+
if (path.empty() || path.is_absolute() || path.has_root_name() ||
29+
path.has_root_directory() || path.filename().empty() || path.filename() == ".") {
30+
return false;
31+
}
32+
return std::none_of(path.begin(), path.end(),
33+
[](const auto &component) { return component == ".."; });
3134
}
3235

3336
}
@@ -76,14 +79,14 @@ namespace stdc::plugin {
7679
bool scanSucceeded = true;
7780
for (const auto &root : it->second) {
7881
std::vector<std::filesystem::path> candidates;
79-
if (!factory.scanPluginPaths(root, &candidates)) {
82+
if (!factory.scanPluginPaths(iid, root, &candidates)) {
8083
scanSucceeded = false;
8184
continue;
8285
}
8386
for (const auto &candidate : candidates) {
8487
std::filesystem::path pluginPath;
8588
std::optional<std::filesystem::path> metadataPath;
86-
if (!factory.resolvePluginPath(candidate, &pluginPath, &metadataPath)) {
89+
if (!factory.resolvePluginPath(iid, candidate, &pluginPath, &metadataPath)) {
8790
scanSucceeded = false;
8891
continue;
8992
}
@@ -100,10 +103,13 @@ namespace stdc::plugin {
100103
}
101104

102105
auto loader = createLoader();
103-
loader->setFilePath(canonical, metadataPath);
106+
loader->setFilePath(canonical);
104107
if (loader->iid() != iid) {
105108
continue;
106109
}
110+
if (metadataPath) {
111+
loader->setFilePath(canonical, metadataPath);
112+
}
107113

108114
readPluginFiles.insert(std::move(pluginFile));
109115
known.emplace_back(std::move(loader));
@@ -126,7 +132,7 @@ namespace stdc::plugin {
126132

127133
PluginFactory &PluginFactory::operator=(PluginFactory &&RHS) noexcept = default;
128134

129-
bool PluginFactory::scanPluginPaths(const std::filesystem::path &path,
135+
bool PluginFactory::scanPluginPaths(std::string_view iid, const std::filesystem::path &path,
130136
std::vector<std::filesystem::path> *pluginPaths) const {
131137
std::error_code ec;
132138
fs::directory_iterator dir(path, ec);
@@ -138,9 +144,8 @@ namespace stdc::plugin {
138144
while (dir != end) {
139145
const auto candidate = dir->path();
140146
if (SharedLibrary::isLibrary(candidate)) {
141-
std::string text;
142-
std::string errorMessage;
143-
if (PluginLoader::Impl::decodeEmbeddedText(candidate, &text, &errorMessage)) {
147+
const PluginLoader loader(candidate);
148+
if (loader.iid() == iid) {
144149
pluginPaths->push_back(candidate);
145150
}
146151
}
@@ -155,7 +160,7 @@ namespace stdc::plugin {
155160
}
156161

157162
bool
158-
PluginFactory::resolvePluginPath(const std::filesystem::path &path,
163+
PluginFactory::resolvePluginPath(std::string_view, const std::filesystem::path &path,
159164
std::filesystem::path *pluginPath,
160165
std::optional<std::filesystem::path> *metadataPath) const {
161166
*pluginPath = path;
@@ -270,7 +275,7 @@ namespace stdc::plugin {
270275
BundlePluginFactory::BundlePluginFactory(fs::path metadataFileName)
271276
: PluginFactory(std::make_unique<Impl>(std::move(metadataFileName))) {
272277
stdc_impl_t;
273-
assert(is_file_name(impl.metadataFileName));
278+
assert(is_relative_file_path(impl.metadataFileName));
274279
}
275280

276281
BundlePluginFactory::~BundlePluginFactory() = default;
@@ -285,9 +290,9 @@ namespace stdc::plugin {
285290
return impl.metadataFileName;
286291
}
287292

288-
bool BundlePluginFactory::scanPluginPaths(const fs::path &path,
293+
bool BundlePluginFactory::scanPluginPaths(std::string_view, const fs::path &path,
289294
std::vector<fs::path> *pluginPaths) const {
290-
if (!is_file_name(metadataFileName())) {
295+
if (!is_relative_file_path(metadataFileName())) {
291296
return false;
292297
}
293298

@@ -313,9 +318,10 @@ namespace stdc::plugin {
313318
return true;
314319
}
315320

316-
bool BundlePluginFactory::resolvePluginPath(const fs::path &path, fs::path *pluginPath,
321+
bool BundlePluginFactory::resolvePluginPath(std::string_view, const fs::path &path,
322+
fs::path *pluginPath,
317323
std::optional<fs::path> *metadataPath) const {
318-
if (!is_file_name(metadataFileName())) {
324+
if (!is_relative_file_path(metadataFileName())) {
319325
return false;
320326
}
321327

‎tests/auto/test_pluginloader.cpp‎

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,48 +13,68 @@ namespace {
1313

1414
class DefaultPluginFactoryProbe : public stdc::plugin::PluginFactory {
1515
public:
16-
bool scan(const std::filesystem::path &path,
16+
bool scan(std::string_view iid, const std::filesystem::path &path,
1717
std::vector<std::filesystem::path> *pluginPaths) const {
18-
return scanPluginPaths(path, pluginPaths);
18+
return scanPluginPaths(iid, path, pluginPaths);
1919
}
2020

21-
bool resolve(const std::filesystem::path &path, std::filesystem::path *pluginPath,
21+
bool resolve(std::string_view iid, const std::filesystem::path &path,
22+
std::filesystem::path *pluginPath,
2223
std::optional<std::filesystem::path> *metadataPath) const {
23-
return resolvePluginPath(path, pluginPath, metadataPath);
24+
return resolvePluginPath(iid, path, pluginPath, metadataPath);
2425
}
2526
};
2627

2728
class TestPluginFactory : public stdc::plugin::PluginFactory {
2829
protected:
29-
bool scanPluginPaths(const std::filesystem::path &,
30+
bool scanPluginPaths(std::string_view iid, const std::filesystem::path &,
3031
std::vector<std::filesystem::path> *pluginPaths) const override {
32+
scannedIid = iid;
3133
pluginPaths->push_back("candidate");
3234
return true;
3335
}
3436

35-
bool resolvePluginPath(const std::filesystem::path &, std::filesystem::path *pluginPath,
37+
bool resolvePluginPath(std::string_view iid, const std::filesystem::path &,
38+
std::filesystem::path *pluginPath,
3639
std::optional<std::filesystem::path> *metadataPath) const override {
40+
resolvedIid = iid;
3741
*pluginPath = TEST_PLUGINLOADER_PLUGIN_PATH;
3842
*metadataPath = TEST_PLUGINLOADER_METADATA_PATH;
3943
return true;
4044
}
45+
46+
public:
47+
mutable std::string scannedIid;
48+
mutable std::string resolvedIid;
4149
};
4250

4351
class RetryPluginFactory : public TestPluginFactory {
4452
protected:
45-
bool scanPluginPaths(const std::filesystem::path &path,
53+
bool scanPluginPaths(std::string_view iid, const std::filesystem::path &path,
4654
std::vector<std::filesystem::path> *pluginPaths) const override {
4755
if (_firstScan) {
4856
_firstScan = false;
4957
return false;
5058
}
51-
return TestPluginFactory::scanPluginPaths(path, pluginPaths);
59+
return TestPluginFactory::scanPluginPaths(iid, path, pluginPaths);
5260
}
5361

5462
private:
5563
mutable bool _firstScan = true;
5664
};
5765

66+
class InvalidMetadataPluginFactory : public TestPluginFactory {
67+
protected:
68+
bool resolvePluginPath(std::string_view iid, const std::filesystem::path &,
69+
std::filesystem::path *pluginPath,
70+
std::optional<std::filesystem::path> *metadataPath) const override {
71+
resolvedIid = iid;
72+
*pluginPath = TEST_PLUGINLOADER_PLUGIN_PATH;
73+
*metadataPath = TEST_PLUGINLOADER_LIBRARY_PATH;
74+
return true;
75+
}
76+
};
77+
5878
class TemporaryPluginDirectory {
5979
public:
6080
TemporaryPluginDirectory() {
@@ -197,6 +217,8 @@ BOOST_AUTO_TEST_CASE(test_factory_scan_hooks) {
197217
factory.addPluginPath("org.stdcorelib.LoaderTest", root);
198218
const auto plugins = factory.plugins("org.stdcorelib.LoaderTest");
199219
BOOST_REQUIRE_EQUAL(plugins.size(), 1u);
220+
BOOST_CHECK_EQUAL(factory.scannedIid, "org.stdcorelib.LoaderTest");
221+
BOOST_CHECK_EQUAL(factory.resolvedIid, "org.stdcorelib.LoaderTest");
200222
BOOST_CHECK_EQUAL(plugins.front()->filePath(), TEST_PLUGINLOADER_PLUGIN_PATH);
201223

202224
factory.addPluginPath("org.stdcorelib.LoaderTest", root);
@@ -216,6 +238,20 @@ BOOST_AUTO_TEST_CASE(test_factory_retries_failed_scan) {
216238
BOOST_CHECK_EQUAL(factory.plugins("org.stdcorelib.LoaderTest").size(), 1u);
217239
}
218240

241+
BOOST_AUTO_TEST_CASE(test_factory_keeps_matching_plugin_with_invalid_external_metadata) {
242+
const auto root = std::filesystem::path(TEST_PLUGINLOADER_METADATA_PATH).parent_path();
243+
244+
InvalidMetadataPluginFactory factory;
245+
factory.addPluginPath("org.stdcorelib.LoaderTest", root);
246+
const auto plugins = factory.plugins("org.stdcorelib.LoaderTest");
247+
248+
BOOST_REQUIRE_EQUAL(plugins.size(), 1u);
249+
BOOST_CHECK_EQUAL(plugins.front()->state(), stdc::plugin::PluginLoader::Invalid);
250+
BOOST_CHECK(plugins.front()->hasError());
251+
BOOST_CHECK(plugins.front()->iid().empty());
252+
BOOST_CHECK(plugins.front()->metadata().isNull());
253+
}
254+
219255
BOOST_AUTO_TEST_CASE(test_factory_replaces_paths) {
220256
const auto root = std::filesystem::path(TEST_PLUGINLOADER_METADATA_PATH).parent_path();
221257
const std::vector<std::filesystem::path> paths{root};
@@ -256,15 +292,21 @@ BOOST_AUTO_TEST_CASE(test_default_factory_scan) {
256292
DefaultPluginFactoryProbe factory;
257293

258294
std::vector<std::filesystem::path> candidates;
259-
BOOST_REQUIRE(factory.scan(directory.path(), &candidates));
295+
BOOST_REQUIRE(factory.scan("org.stdcorelib.LoaderTest", directory.path(), &candidates));
260296
BOOST_REQUIRE_EQUAL(candidates.size(), 1u);
261-
BOOST_CHECK_EQUAL(candidates.front().filename(),
297+
const auto candidate = candidates.front();
298+
BOOST_CHECK_EQUAL(candidate.filename(),
262299
std::filesystem::path(TEST_PLUGINLOADER_PLUGIN_PATH).filename());
263300

301+
candidates.clear();
302+
BOOST_REQUIRE(factory.scan("org.stdcorelib.Other", directory.path(), &candidates));
303+
BOOST_CHECK(candidates.empty());
304+
264305
std::filesystem::path pluginPath;
265306
std::optional<std::filesystem::path> metadataPath = TEST_PLUGINLOADER_METADATA_PATH;
266-
BOOST_REQUIRE(factory.resolve(candidates.front(), &pluginPath, &metadataPath));
267-
BOOST_CHECK_EQUAL(pluginPath, candidates.front());
307+
BOOST_REQUIRE(
308+
factory.resolve("org.stdcorelib.LoaderTest", candidate, &pluginPath, &metadataPath));
309+
BOOST_CHECK_EQUAL(pluginPath, candidate);
268310
BOOST_CHECK(!metadataPath);
269311

270312
factory.addPluginPath("org.stdcorelib.LoaderTest", directory.path());

0 commit comments

Comments
 (0)