Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugins: Set constrains for plugin filenames. #2201

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod paths;
use std::{
fs::{self, read_to_string},
io,
path::PathBuf,
path::{Path, PathBuf},
};

use stypes::{InvalidPluginInfo, PluginMetadata};
Expand Down Expand Up @@ -146,33 +146,58 @@ enum PluginValidationState {
},
}

/// Loads the files inside plugin directory and validate their content return the state
/// Extract plugins binary filename and metadata filename from plugins directory
/// path by conventions.
/// The current conventions state the plugin filename and metadata must match
/// the directory name of the plugin itself and will be considered as plugin name.
///
/// * `plugins_dir`: The path of the plugin directory
///
/// # Returns:
///
/// A tuple contains the filename of the plugin binary `*.wasm` file and its
/// metadata `*.toml` file when plugins directory is valid.
fn extract_plugin_filenames(plugins_dir: &Path) -> Result<(String, String), InitError> {
let dir_name = plugins_dir
.file_name()
.and_then(|name| name.to_str())
.ok_or_else(|| {
InitError::Other(format!(
"Extracting plugins files from its directory failed. Plugin directory: {}",
plugins_dir.display()
))
})?;

let plugin_file = format!("{dir_name}.wasm");
let metadata_file = format!("{dir_name}.toml");

Ok((plugin_file, metadata_file))
}

/// Loads plugin files inside its directory and validate their content returning the state
/// of the plugin.
fn validate_plugin_files(dir: &PathBuf) -> Result<PluginValidationState, InitError> {
///
/// * `plugin_dir`: Path for the plugin directory.
fn validate_plugin_files(plugin_dir: &PathBuf) -> Result<PluginValidationState, InitError> {
use PluginValidationState as Re;

let (plugin_filename, metadata_filename) = extract_plugin_filenames(plugin_dir)?;

let mut wasm_file = None;
let mut metadata_file = None;
for file in fs::read_dir(dir)?
for file in fs::read_dir(plugin_dir)?
.filter_map(|e| e.ok().map(|e| e.path()))
.filter(|e| e.is_file())
{
match file.extension().map(|ext| ext.to_str()) {
Some(Some("wasm")) => {
if wasm_file.replace(file).is_some() {
let err_msg = format!("Multiple wasm files found in {}", dir.display());

return Ok(Re::Invalid { err_msg });
}
match file.file_name().and_then(|name| name.to_str()) {
Some(name) if name == plugin_filename => {
wasm_file = Some(file);
}
Some(Some("toml")) => {
if metadata_file.replace(file).is_some() {
let err_msg = format!("Multiple metadata files found in {}", dir.display());

return Ok(Re::Invalid { err_msg });
}
Some(name) if name == metadata_filename => {
metadata_file = Some(file);
}
_invalid => {
log::warn!(
_ignored => {
log::info!(
"File ignored while loading parser plugin. Path {}",
file.display()
);
Expand All @@ -186,7 +211,10 @@ fn validate_plugin_files(dir: &PathBuf) -> Result<PluginValidationState, InitErr
metadata: metadata_file,
},
None => {
let err_msg = format!("No *.wasm file found in {}", dir.display());
let err_msg = format!(
"File {plugin_filename} is not found in {}",
plugin_dir.display()
);

Re::Invalid { err_msg }
}
Expand Down
6 changes: 5 additions & 1 deletion application/client/src/app/service/actions/folder.plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ export class Action extends Base {
session
.initialize()
.observe(
new Factory.File().type(files[0].type).file(files[0].filename).asPlugin().get(),
new Factory.File()
.type(files[0].type)
.file(files[0].filename)
.asParserPlugin()
.get(),
);
}

Expand Down
1 change: 1 addition & 0 deletions cli/chipmunk-cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion plugin_examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ The build process will generate a WASM file named after your plugin. To integrat
Create a directory within the appropriate plugin type folder (for example, `<HOME>/.chipmunk/plugins/parser/` for parser plugins or `<HOME>/.chipmunk/plugins/bytesource/` for byte-source plugins) using the plugin name.

2. **Copy Artifacts:**
Place the compiled WASM file in the directory. You may also include an optional TOML file containing plugin metadata (such as the plugin name and description).
Place the compiled WASM file inside the plugin directory. Optionally, you can include a TOML file to provide metadata such as the plugin’s name and description.
Ensure that both the `.wasm` binary and the `.toml` metadata file (if present) have names that match the plugin directory name.

---

Expand Down
Loading