This commit fixes several critical issues across the codebase: 1. Memory safety & resource leaks: Added proper cleanup for compression engine processes using RAII patterns 2. Error handling: Replaced unsafe unwrap() calls with proper error propagation using ok_or_else()? 3. Concurrency issues: Improved diff mode thread safety with proper error handling and RAII guards 4. Security concerns: Added validation for item IDs to prevent path traversal vulnerabilities 5. Database design: Wrapped database operations in transactions for atomicity in save/update modes Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
55 lines
1.5 KiB
Rust
55 lines
1.5 KiB
Rust
use anyhow::{Context, Result, anyhow};
|
|
use std::fs;
|
|
use std::path::PathBuf;
|
|
|
|
use crate::db;
|
|
use clap::Command;
|
|
use clap::error::ErrorKind;
|
|
use log::{debug, warn};
|
|
use rusqlite::Connection;
|
|
|
|
pub fn mode_delete(
|
|
cmd: &mut Command,
|
|
_args: &crate::Args,
|
|
ids: &mut Vec<i64>,
|
|
tags: &mut Vec<String>,
|
|
conn: &mut Connection,
|
|
data_path: PathBuf,
|
|
) -> Result<()> {
|
|
if ids.is_empty() {
|
|
cmd.error(
|
|
ErrorKind::InvalidValue,
|
|
"No ID given, you must supply atleast one ID when using --delete",
|
|
)
|
|
.exit();
|
|
} else if !tags.is_empty() {
|
|
cmd.error(
|
|
ErrorKind::InvalidValue,
|
|
"Tags given but not supported, you must supply atleast one ID when using --delete",
|
|
)
|
|
.exit();
|
|
}
|
|
|
|
for item_id in ids.iter() {
|
|
if let Some(item) = db::get_item(conn, *item_id)? {
|
|
debug!("MAIN: Found item {:?}", item);
|
|
db::delete_item(conn, item)?;
|
|
|
|
// Validate that item ID is positive to prevent path traversal issues
|
|
if *item_id <= 0 {
|
|
return Err(anyhow!("Invalid item ID: {}", item_id));
|
|
}
|
|
|
|
let mut item_path = data_path.clone();
|
|
item_path.push(item_id.to_string());
|
|
|
|
fs::remove_file(&item_path)
|
|
.context(anyhow!("Unable to remove item file {:?}", item_path))?;
|
|
} else {
|
|
warn!("Unable to find item {item_id} in database");
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|