# core/commands/rm.py import fnmatch import posixpath from core.connection import BuzzerError from core.commands.ls import get_file_tree def _delete_recursive(conn, nodes): """Löscht Knoten Bottom-Up (erst Dateien/Unterordner, dann den Ordner selbst)""" for node in nodes: if node["type"] == 'D': if "children" in node and node["children"]: _delete_recursive(conn, node["children"]) _try_rm(conn, node["path"], is_dir=True) elif node["type"] == 'F': _try_rm(conn, node["path"], is_dir=False) def _try_rm(conn, path, is_dir=False): icon = "📁" if is_dir else "📄" try: conn.rm(path) print(f" 🗑️ {icon} Gelöscht: {path}") except BuzzerError as e: print(f" ❌ Fehler bei {path}: {e}") def execute(conn, path: str, recursive: bool = False): """Löscht eine Datei, ein Verzeichnis oder löst Wildcards (*) auf.""" # 1. Wildcard-Behandlung (z.B. /lfs/a/* oder *.wav) if '*' in path or '?' in path: dirname, pattern = posixpath.split(path) if not dirname: dirname = "/" print(f"Suche nach Dateien passend zu '{pattern}' in '{dirname}'...") tree = get_file_tree(conn, target_path=dirname, recursive=False) # Fehler beim Verzeichnis-Lesen abfangen if len(tree) == 1 and tree[0].get("type") == "E": print(f"❌ Verzeichnis '{dirname}' nicht gefunden.") return # Filtern mit fnmatch (funktioniert wie in der Linux-Shell) matches = [node for node in tree if node.get("type") == "F" and fnmatch.fnmatch(node["name"], pattern)] if not matches: print(f"Keine passenden Dateien für '{path}' gefunden.") return for match in matches: _try_rm(conn, match["path"], is_dir=False) return # Fertig mit Wildcard-Löschen # 2. Rekursives Löschen (-r) if recursive: try: conn.rm_recursive(path) print(f"🗑️ '{path}' rekursiv gelöscht.") except BuzzerError as e: print(f"❌ Fehler beim rekursiven Löschen von '{path}': {e}") return # 3. Standard-Löschen (Einzeldatei oder leeres Verzeichnis) try: conn.rm(path) print(f"🗑️ '{path}' erfolgreich gelöscht.") except BuzzerError as e: print(f"❌ Fehler beim Löschen von '{path}': {e}")