105 lines
3.8 KiB
Rust
105 lines
3.8 KiB
Rust
use anyhow::Result;
|
|
use std::io;
|
|
use std::io::Write;
|
|
use rusqlite::Connection;
|
|
use log::debug;
|
|
|
|
pub mod program;
|
|
pub mod digest;
|
|
pub mod system;
|
|
|
|
use crate::meta_plugin::program::MetaPluginProgram;
|
|
use crate::meta_plugin::digest::{DigestSha256MetaPlugin, ReadTimeMetaPlugin, ReadRateMetaPlugin};
|
|
use crate::meta_plugin::system::{CwdMetaPlugin, BinaryMetaPlugin, UidMetaPlugin, UserMetaPlugin, GidMetaPlugin, GroupMetaPlugin, ShellMetaPlugin, ShellPidMetaPlugin, KeepPidMetaPlugin, HostnameMetaPlugin, FullHostnameMetaPlugin};
|
|
|
|
#[derive(Debug, Eq, PartialEq, Clone, strum::EnumIter, strum::Display, strum::EnumString)]
|
|
#[strum(ascii_case_insensitive)]
|
|
pub enum MetaPluginType {
|
|
FileMagic,
|
|
FileMime,
|
|
FileEncoding,
|
|
LineCount,
|
|
WordCount,
|
|
Cwd,
|
|
Binary,
|
|
Uid,
|
|
User,
|
|
Gid,
|
|
Group,
|
|
Shell,
|
|
ShellPid,
|
|
KeepPid,
|
|
DigestSha256,
|
|
DigestMd5,
|
|
ReadTime,
|
|
ReadRate,
|
|
Hostname,
|
|
FullHostname,
|
|
}
|
|
|
|
pub trait MetaPlugin {
|
|
fn is_supported(&self) -> bool {
|
|
true
|
|
}
|
|
|
|
fn is_internal(&self) -> bool {
|
|
false
|
|
}
|
|
|
|
fn create(&self) -> Result<Box<dyn Write>>;
|
|
fn finalize(&mut self) -> io::Result<String>;
|
|
|
|
// Update the meta plugin with new data
|
|
fn update(&mut self, data: &[u8]);
|
|
|
|
fn meta_name(&mut self) -> String;
|
|
|
|
// Get program information for display in status
|
|
fn program_info(&self) -> Option<(&str, Vec<&str>)> {
|
|
None
|
|
}
|
|
|
|
// Initialize with database connection
|
|
fn initialize(&mut self, _conn: &Connection, _item_id: i64) -> Result<()> {
|
|
Ok(())
|
|
}
|
|
|
|
// Save metadata to database
|
|
fn save_meta(&mut self, conn: &Connection, item_id: i64, value: String) -> Result<()> {
|
|
let meta_name = self.meta_name();
|
|
debug!("Saving metadata: item_id={}, name={}, value={}", item_id, meta_name, value);
|
|
let meta = crate::db::Meta {
|
|
id: item_id,
|
|
name: meta_name,
|
|
value,
|
|
};
|
|
crate::db::store_meta(conn, meta)?;
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
pub fn get_meta_plugin(meta_plugin_type: MetaPluginType) -> Box<dyn MetaPlugin> {
|
|
match meta_plugin_type {
|
|
MetaPluginType::FileMagic => Box::new(MetaPluginProgram::new("file", vec!["-bE", "-"], "file_magic".to_string(), true)),
|
|
MetaPluginType::FileMime => Box::new(MetaPluginProgram::new("file", vec!["-b", "--mime-type", "-"], "file_mime".to_string(), true)),
|
|
MetaPluginType::FileEncoding => Box::new(MetaPluginProgram::new("file", vec!["-b", "--mime-encoding", "-"], "file_encoding".to_string(), true)),
|
|
MetaPluginType::LineCount => Box::new(MetaPluginProgram::new("wc", vec!["-l"], "line_count".to_string(), true)),
|
|
MetaPluginType::WordCount => Box::new(MetaPluginProgram::new("wc", vec!["-w"], "word_count".to_string(), true)),
|
|
MetaPluginType::Cwd => Box::new(CwdMetaPlugin::new()),
|
|
MetaPluginType::Binary => Box::new(BinaryMetaPlugin::new()),
|
|
MetaPluginType::Uid => Box::new(UidMetaPlugin::new()),
|
|
MetaPluginType::User => Box::new(UserMetaPlugin::new()),
|
|
MetaPluginType::Gid => Box::new(GidMetaPlugin::new()),
|
|
MetaPluginType::Group => Box::new(GroupMetaPlugin::new()),
|
|
MetaPluginType::Shell => Box::new(ShellMetaPlugin::new()),
|
|
MetaPluginType::ShellPid => Box::new(ShellPidMetaPlugin::new()),
|
|
MetaPluginType::KeepPid => Box::new(KeepPidMetaPlugin::new()),
|
|
MetaPluginType::DigestSha256 => Box::new(DigestSha256MetaPlugin::new()),
|
|
MetaPluginType::DigestMd5 => Box::new(MetaPluginProgram::new("md5sum", vec![], "digest_md5".to_string(), true)),
|
|
MetaPluginType::ReadTime => Box::new(ReadTimeMetaPlugin::new()),
|
|
MetaPluginType::ReadRate => Box::new(ReadRateMetaPlugin::new()),
|
|
MetaPluginType::Hostname => Box::new(HostnameMetaPlugin::new()),
|
|
MetaPluginType::FullHostname => Box::new(FullHostnameMetaPlugin::new()),
|
|
}
|
|
}
|