Skip to content

A high-performance Android storage analyzer that bypasses MTP using a native C++ daemon, a Rust TCP bridge, and a React Treemap UI.

Rust

fix: prevent deletion of scan root directory (closes #5)

Vishnu Srivatsava7ad2a0774c15
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index 91b845a..fe998a8 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -14,6 +14,7 @@
use std::io::{Read, Write};
use std::net::TcpStream;
use std::process::Command;
+use std::sync::Mutex;
use std::time::Duration;
// ── Constants ───────────────────────────────────────────────────────────────
@@ -24,6 +25,9 @@ const DEVICE_BIN_PATH: &str = "/data/local/tmp/socketsweep_daemon";
const TCP_CONNECT_TIMEOUT: Duration = Duration::from_secs(5);
const TCP_READ_TIMEOUT: Duration = Duration::from_secs(120); // scans can be slow
+/// Tracks the root path of the last successful scan so we can prevent its deletion.
+static SCAN_ROOT: Mutex<Option<String>> = Mutex::new(None);
+
// ── Resource Resolution ─────────────────────────────────────────────────────
use tauri::Manager;
@@ -191,11 +195,19 @@ fn init_daemon(app: tauri::AppHandle) -> Result<String, String> {
#[tauri::command]
fn run_scan(path: Option<String>) -> Result<String, String> {
- let cmd = match path {
- Some(ref p) if !p.is_empty() => format!("SCAN {p}"),
- _ => "SCAN".to_string(),
+ let effective_root = match path {
+ Some(ref p) if !p.is_empty() => p.clone(),
+ _ => "/sdcard".to_string(), // daemon default
};
- daemon_command(&cmd)
+ let cmd = format!("SCAN {effective_root}");
+ let response = daemon_command(&cmd)?;
+
+ // Store the scan root so delete_item can guard against it.
+ if let Ok(mut root) = SCAN_ROOT.lock() {
+ *root = Some(effective_root);
+ }
+
+ Ok(response)
}
#[tauri::command]
@@ -214,6 +226,14 @@ fn stop_daemon(app: tauri::AppHandle) -> Result<String, String> {
#[tauri::command]
fn delete_item(path: String) -> Result<String, String> {
+ // Prevent deletion of the scan root directory.
+ if let Ok(root) = SCAN_ROOT.lock() {
+ if let Some(ref scan_root) = *root {
+ if path == *scan_root {
+ return Err("Cannot delete the scan root directory.".into());
+ }
+ }
+ }
daemon_command(&format!("DELETE {path}"))
}
diff --git a/src/App.tsx b/src/App.tsx
index a908d5e..e557bbe 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -394,20 +394,22 @@ function FileTreeNode({
{formatBytes(node.size)}
</span>
- <button
- className="btn-nuke opacity-0 group-hover:opacity-100 ml-1 px-2 py-1 rounded-md
- bg-danger-600/20 border border-danger-500/30 text-danger-400
- hover:bg-danger-500/30 hover:text-danger-400 text-[10px] uppercase font-bold tracking-wider flex items-center gap-1
- cursor-pointer"
- onClick={(e) => {
- e.stopPropagation();
- onNuke(node.path, node.name);
- }}
- title={`Delete ${node.name}`}
- >
- <IconNuke />
- <span className="hidden sm:inline">Delete</span>
- </button>
+ {depth > 0 && (
+ <button
+ className="btn-nuke opacity-0 group-hover:opacity-100 ml-1 px-2 py-1 rounded-md
+ bg-danger-600/20 border border-danger-500/30 text-danger-400
+ hover:bg-danger-500/30 hover:text-danger-400 text-[10px] uppercase font-bold tracking-wider flex items-center gap-1
+ cursor-pointer"
+ onClick={(e) => {
+ e.stopPropagation();
+ onNuke(node.path, node.name);
+ }}
+ title={`Delete ${node.name}`}
+ >
+ <IconNuke />
+ <span className="hidden sm:inline">Delete</span>
+ </button>
+ )}
</div>
{isDir && open && node.children && (
@@ -724,6 +726,13 @@ function App() {
const { path, name } = confirmDelete;
setConfirmDelete(null);
+ // Guard: never delete the scan root
+ if (scanData && path === scanData.tree.path) {
+ pushToast("Cannot delete the scan root directory.", "error");
+ addLog(`[DELETE] Blocked: ${path} is the scan root.`);
+ return;
+ }
+
addLog(`[DELETE] Requesting deletion of ${path}...`);
try {
const raw = await invoke<string>("delete_item", { path });