refactor: remove connection storage from plugin structs and pass as argument

Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-18 15:52:27 -03:00
parent 2a16edcbe7
commit d96804bdfb
5 changed files with 51 additions and 76 deletions

View File

@@ -60,10 +60,10 @@ pub trait MetaPlugin {
false
}
fn finalize(&mut self) -> Result<()>;
fn finalize(&mut self, conn: &Connection) -> Result<()>;
// Update the meta plugin with new data
fn update(&mut self, data: &[u8]);
fn update(&mut self, data: &[u8], conn: &Connection);
fn meta_name(&mut self) -> String;

View File

@@ -10,7 +10,6 @@ pub struct DigestSha256MetaPlugin {
hasher: Sha256,
meta_name: String,
item_id: Option<i64>,
conn: Option<*mut Connection>,
output_names: std::collections::HashMap<String, String>,
}
@@ -20,7 +19,6 @@ impl DigestSha256MetaPlugin {
hasher: Sha256::new(),
meta_name: "digest_sha256".to_string(),
item_id: None,
conn: None,
output_names: std::collections::HashMap::new(),
}
}
@@ -31,29 +29,24 @@ impl MetaPlugin for DigestSha256MetaPlugin {
true
}
fn initialize(&mut self, conn: &Connection, item_id: i64) -> Result<()> {
fn initialize(&mut self, _conn: &Connection, item_id: i64) -> Result<()> {
self.item_id = Some(item_id);
// Store raw pointer to connection - unsafe but necessary for this design
self.conn = Some(conn as *const _ as *mut Connection);
Ok(())
}
fn finalize(&mut self) -> Result<()> {
if let (Some(conn), Some(item_id)) = (self.conn, self.item_id) {
// Convert raw pointer back to reference (unsafe)
let conn_ref = unsafe { &*conn };
fn finalize(&mut self, conn: &Connection) -> Result<()> {
if let Some(item_id) = self.item_id {
// Finalize the hash
let hash_result = self.hasher.finalize_reset();
let hex_string = format!("{:x}", hash_result);
// Save the hash as metadata using central output handler
let _ = output_metadata(conn_ref, item_id, &self.meta_name, hex_string, &self.output_names);
let _ = output_metadata(conn, item_id, &self.meta_name, hex_string, &self.output_names);
}
Ok(())
}
fn update(&mut self, data: &[u8]) {
fn update(&mut self, data: &[u8], _conn: &Connection) {
self.hasher.update(data);
}
@@ -100,11 +93,11 @@ impl MetaPlugin for ReadTimeMetaPlugin {
true
}
fn finalize(&mut self) -> Result<()> {
fn finalize(&mut self, _conn: &Connection) -> Result<()> {
Ok(())
}
fn update(&mut self, _data: &[u8]) {
fn update(&mut self, _data: &[u8], _conn: &Connection) {
if self.start_time.is_none() {
self.start_time = Some(Instant::now());
}
@@ -137,11 +130,11 @@ impl MetaPlugin for ReadRateMetaPlugin {
true
}
fn finalize(&mut self) -> Result<()> {
fn finalize(&mut self, _conn: &Connection) -> Result<()> {
Ok(())
}
fn update(&mut self, data: &[u8]) {
fn update(&mut self, data: &[u8], _conn: &Connection) {
if self.start_time.is_none() {
self.start_time = Some(Instant::now());
}

View File

@@ -11,7 +11,6 @@ pub struct MagicFileMetaPlugin {
max_buffer_size: usize,
is_saved: bool,
item_id: Option<i64>,
conn: Option<*mut Connection>,
cookie: Option<Cookie>,
output_names: std::collections::HashMap<String, String>,
}
@@ -23,7 +22,6 @@ impl MagicFileMetaPlugin {
max_buffer_size: 4096, // Same as BinaryMetaPlugin
is_saved: false,
item_id: None,
conn: None,
cookie: None,
output_names: std::collections::HashMap::new(),
}
@@ -56,15 +54,12 @@ impl MagicFileMetaPlugin {
}
}
fn save_all_magic_metadata(&mut self) -> Result<()> {
if let (Some(conn), Some(item_id)) = (self.conn, self.item_id) {
// Convert raw pointer back to reference (unsafe)
let conn_ref = unsafe { &*conn };
fn save_all_magic_metadata(&mut self, conn: &Connection) -> Result<()> {
if let Some(item_id) = self.item_id {
// Only save MIME type since that's what's mapped in the config
if let Ok(mime_type) = self.get_magic_result(CookieFlags::MIME_TYPE) {
if !mime_type.is_empty() {
let _ = output_metadata(conn_ref, item_id, "mime_type", mime_type, &self.output_names);
let _ = output_metadata(conn, item_id, "mime_type", mime_type, &self.output_names);
}
}
@@ -79,10 +74,8 @@ impl MetaPlugin for MagicFileMetaPlugin {
true
}
fn initialize(&mut self, conn: &Connection, item_id: i64) -> Result<()> {
fn initialize(&mut self, _conn: &Connection, item_id: i64) -> Result<()> {
self.item_id = Some(item_id);
// Store raw pointer to connection - unsafe but necessary for this design
self.conn = Some(conn as *const _ as *mut Connection);
// Initialize the magic cookie once
let cookie = Cookie::open(Default::default())
@@ -94,17 +87,17 @@ impl MetaPlugin for MagicFileMetaPlugin {
Ok(())
}
fn finalize(&mut self) -> Result<()> {
fn finalize(&mut self, conn: &Connection) -> Result<()> {
// Save all magic metadata if not already saved
if !self.is_saved {
if let Err(e) = self.save_all_magic_metadata() {
if let Err(e) = self.save_all_magic_metadata(conn) {
eprintln!("Warning: Failed to save magic metadata: {}", e);
}
}
Ok(())
}
fn update(&mut self, data: &[u8]) {
fn update(&mut self, data: &[u8], conn: &Connection) {
// Only collect up to max_buffer_size
let remaining_capacity = self.max_buffer_size.saturating_sub(self.buffer.len());
if remaining_capacity > 0 {
@@ -113,7 +106,7 @@ impl MetaPlugin for MagicFileMetaPlugin {
// Check if we've reached our buffer limit and save if so
if self.buffer.len() >= self.max_buffer_size && !self.is_saved {
if let Err(e) = self.save_all_magic_metadata() {
if let Err(e) = self.save_all_magic_metadata(conn) {
eprintln!("Warning: Failed to save magic metadata early: {}", e);
}
}

View File

@@ -16,7 +16,6 @@ pub struct MetaPluginProgram {
process: Option<Child>,
writer: Option<Box<dyn Write>>,
item_id: Option<i64>,
conn: Option<*mut Connection>,
result: Option<String>,
}
@@ -48,7 +47,6 @@ impl MetaPluginProgram {
process: None,
writer: None,
item_id: None,
conn: None,
result: None,
}
}
@@ -63,12 +61,11 @@ impl MetaPlugin for MetaPluginProgram {
false
}
fn initialize(&mut self, conn: &rusqlite::Connection, item_id: i64) -> Result<()> {
fn initialize(&mut self, _conn: &rusqlite::Connection, item_id: i64) -> Result<()> {
debug!("META: Initializing program plugin: {:?}", self);
// Store database connection and item ID for later use
// Store item ID for later use
self.item_id = Some(item_id);
self.conn = Some(conn as *const _ as *mut Connection);
let program = self.program.clone();
let args = self.args.clone();
@@ -94,7 +91,7 @@ impl MetaPlugin for MetaPluginProgram {
Ok(())
}
fn finalize(&mut self) -> Result<()> {
fn finalize(&mut self, conn: &Connection) -> Result<()> {
debug!("META: Finalizing program plugin");
if let Some(process) = self.process.take() {
// Close stdin to signal end of input
@@ -116,16 +113,14 @@ impl MetaPlugin for MetaPluginProgram {
debug!("META: Program output: {}", result);
self.result = Some(result);
// Save the result to database if we have connection and item_id
if let (Some(conn), Some(item_id)) = (self.conn, self.item_id) {
// Convert raw pointer back to reference (unsafe)
let conn_ref = unsafe { &*conn };
// Save the result to database if we have item_id
if let Some(item_id) = self.item_id {
let meta = crate::db::Meta {
id: item_id,
name: self.meta_name.clone(),
value: self.result.clone().unwrap(),
};
let _ = crate::db::store_meta(conn_ref, meta);
let _ = crate::db::store_meta(conn, meta);
}
}
} else {
@@ -139,7 +134,7 @@ impl MetaPlugin for MetaPluginProgram {
Ok(())
}
fn update(&mut self, data: &[u8]) {
fn update(&mut self, data: &[u8], _conn: &Connection) {
if let Some(ref mut writer) = self.writer {
if let Err(e) = writer.write_all(data) {
debug!("META: Failed to write to process stdin: {}", e);

View File

@@ -24,7 +24,6 @@ pub struct BinaryMetaPlugin {
max_buffer_size: usize,
is_saved: bool,
item_id: Option<i64>,
conn: Option<*mut Connection>,
output_names: std::collections::HashMap<String, String>,
}
@@ -36,7 +35,6 @@ impl BinaryMetaPlugin {
max_buffer_size: 4096, // 4KB
is_saved: false,
item_id: None,
conn: None,
output_names: std::collections::HashMap::new(),
}
}
@@ -48,17 +46,15 @@ impl MetaPlugin for BinaryMetaPlugin {
true
}
fn finalize(&mut self) -> Result<()> {
fn finalize(&mut self, conn: &Connection) -> Result<()> {
// Save the binary detection result when finalizing, after all data has been collected
if !self.is_saved {
if let (Some(conn), Some(item_id)) = (self.conn, self.item_id) {
// Convert raw pointer back to reference (unsafe)
let conn_ref = unsafe { &*conn };
if let Some(item_id) = self.item_id {
let is_binary = is_binary(&self.buffer);
let value = if is_binary { "true".to_string() } else { "false".to_string() };
// Save to database immediately using central output handler
let _ = output_metadata(conn_ref, item_id, &self.meta_name, value, &self.output_names);
let _ = output_metadata(conn, item_id, &self.meta_name, value, &self.output_names);
self.is_saved = true;
}
@@ -66,7 +62,7 @@ impl MetaPlugin for BinaryMetaPlugin {
Ok(())
}
fn update(&mut self, data: &[u8]) {
fn update(&mut self, data: &[u8], _conn: &Connection) {
// Only collect up to max_buffer_size
let remaining_capacity = self.max_buffer_size.saturating_sub(self.buffer.len());
if remaining_capacity > 0 {
@@ -79,10 +75,8 @@ impl MetaPlugin for BinaryMetaPlugin {
self.meta_name.clone()
}
fn initialize(&mut self, conn: &Connection, item_id: i64) -> Result<()> {
fn initialize(&mut self, _conn: &Connection, item_id: i64) -> Result<()> {
self.item_id = Some(item_id);
// Store raw pointer to connection - unsafe but necessary for this design
self.conn = Some(conn as *const _ as *mut Connection);
Ok(())
}
@@ -125,12 +119,12 @@ impl MetaPlugin for CwdMetaPlugin {
true
}
fn finalize(&mut self) -> Result<()> {
fn finalize(&mut self, _conn: &Connection) -> Result<()> {
// Since we save during initialize(), return Ok to avoid duplicate saves
Ok(())
}
fn update(&mut self, _data: &[u8]) {
fn update(&mut self, _data: &[u8], _conn: &Connection) {
// No update needed
}
@@ -187,12 +181,12 @@ impl MetaPlugin for UidMetaPlugin {
true
}
fn finalize(&mut self) -> Result<()> {
fn finalize(&mut self, _conn: &Connection) -> Result<()> {
// Since we save during initialize(), return Ok to avoid duplicate saves
Ok(())
}
fn update(&mut self, _data: &[u8]) {
fn update(&mut self, _data: &[u8], _conn: &Connection) {
// No update needed
}
@@ -228,12 +222,12 @@ impl MetaPlugin for UserMetaPlugin {
true
}
fn finalize(&mut self) -> Result<()> {
fn finalize(&mut self, _conn: &Connection) -> Result<()> {
// Since we save during initialize(), return Ok to avoid duplicate saves
Ok(())
}
fn update(&mut self, _data: &[u8]) {
fn update(&mut self, _data: &[u8], _conn: &Connection) {
// No update needed
}
@@ -272,12 +266,12 @@ impl MetaPlugin for GidMetaPlugin {
true
}
fn finalize(&mut self) -> Result<()> {
fn finalize(&mut self, _conn: &Connection) -> Result<()> {
// Since we save during initialize(), return Ok to avoid duplicate saves
Ok(())
}
fn update(&mut self, _data: &[u8]) {
fn update(&mut self, _data: &[u8], _conn: &Connection) {
// No update needed
}
@@ -313,12 +307,12 @@ impl MetaPlugin for GroupMetaPlugin {
true
}
fn finalize(&mut self) -> Result<()> {
fn finalize(&mut self, _conn: &Connection) -> Result<()> {
// Since we save during initialize(), return Ok to avoid duplicate saves
Ok(())
}
fn update(&mut self, _data: &[u8]) {
fn update(&mut self, _data: &[u8], _conn: &Connection) {
// No update needed
}
@@ -357,12 +351,12 @@ impl MetaPlugin for ShellMetaPlugin {
true
}
fn finalize(&mut self) -> Result<()> {
fn finalize(&mut self, _conn: &Connection) -> Result<()> {
// Since we save during initialize(), return Ok to avoid duplicate saves
Ok(())
}
fn update(&mut self, _data: &[u8]) {
fn update(&mut self, _data: &[u8], _conn: &Connection) {
// No update needed
}
@@ -401,12 +395,12 @@ impl MetaPlugin for ShellPidMetaPlugin {
true
}
fn finalize(&mut self) -> Result<()> {
fn finalize(&mut self, _conn: &Connection) -> Result<()> {
// Since we save during initialize(), return Ok to avoid duplicate saves
Ok(())
}
fn update(&mut self, _data: &[u8]) {
fn update(&mut self, _data: &[u8], _conn: &Connection) {
// No update needed
}
@@ -445,12 +439,12 @@ impl MetaPlugin for KeepPidMetaPlugin {
true
}
fn finalize(&mut self) -> Result<()> {
fn finalize(&mut self, _conn: &Connection) -> Result<()> {
// Since we save during initialize(), return Ok to avoid duplicate saves
Ok(())
}
fn update(&mut self, _data: &[u8]) {
fn update(&mut self, _data: &[u8], _conn: &Connection) {
// No update needed
}
@@ -486,12 +480,12 @@ impl MetaPlugin for HostnameMetaPlugin {
true
}
fn finalize(&mut self) -> Result<()> {
fn finalize(&mut self, _conn: &Connection) -> Result<()> {
// Since we save during initialize(), return Ok to avoid duplicate saves
Ok(())
}
fn update(&mut self, _data: &[u8]) {
fn update(&mut self, _data: &[u8], _conn: &Connection) {
// No update needed for hostname
}
@@ -530,12 +524,12 @@ impl MetaPlugin for FullHostnameMetaPlugin {
true
}
fn finalize(&mut self) -> Result<()> {
fn finalize(&mut self, _conn: &Connection) -> Result<()> {
// Since we save during initialize(), return Ok to avoid duplicate saves
Ok(())
}
fn update(&mut self, _data: &[u8]) {
fn update(&mut self, _data: &[u8], _conn: &Connection) {
// No update needed for full hostname
}