feat: V8.5 — FAKT-Speicherort-Dialog beim Onboarding (4 Optionen, Pfad-Validierung, strategy-aware doc_store)
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
"""
|
||||
FAKT-Speicherort-Dialog beim Onboarding (Thema 3).
|
||||
|
||||
Stellt die Pfad-Validierung und den 4-Optionen-Dialog für den
|
||||
FAKT-Speicherort bereit. Wird vom Adapter nach der Butler-Mode-Frage
|
||||
aufgerufen.
|
||||
|
||||
Sicherheitsanforderungen:
|
||||
- Pfad muss absolut sein
|
||||
- Keine '..' (Path-Traversal)
|
||||
- System-Verzeichnisse blockiert: /etc, /var, /usr, /boot, /sys, /proc
|
||||
- HOME-relativ wird normalisiert
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from typing import Optional, Dict, Any
|
||||
|
||||
# System-Verzeichnisse, die blockiert werden
|
||||
BLOCKED_PREFIXES = [
|
||||
"/etc",
|
||||
"/var",
|
||||
"/usr",
|
||||
"/boot",
|
||||
"/sys",
|
||||
"/proc",
|
||||
]
|
||||
|
||||
|
||||
def validate_fact_store_path(raw_path: str) -> Dict[str, Any]:
|
||||
"""Validiere einen FAKT-Speicherort-Pfad.
|
||||
|
||||
Args:
|
||||
raw_path: Der vom Benutzer eingegebene Pfad (kann HOME-relativ sein).
|
||||
|
||||
Returns:
|
||||
Dict mit:
|
||||
- valid (bool): True wenn der Pfad gültig ist
|
||||
- error (str | None): Fehlermeldung wenn invalid
|
||||
- normalized_path (str | None): Normalisierter absoluter Pfad
|
||||
"""
|
||||
if not raw_path or not raw_path.strip():
|
||||
return {"valid": False, "error": "Pfad darf nicht leer sein.", "normalized_path": None}
|
||||
|
||||
raw_path = raw_path.strip()
|
||||
|
||||
# Prüfe: keine '..' (Path-Traversal) — VOR der Normalisierung prüfen
|
||||
if ".." in raw_path:
|
||||
return {
|
||||
"valid": False,
|
||||
"error": "Path-Traversal ('..') ist nicht erlaubt.",
|
||||
"normalized_path": None,
|
||||
}
|
||||
|
||||
# Prüfe: muss absolut sein oder mit ~ beginnen
|
||||
if not (raw_path.startswith("/") or raw_path.startswith("~")):
|
||||
return {
|
||||
"valid": False,
|
||||
"error": "Der Pfad muss absolut sein (z.B. /home/bernd/wissenssammlung).",
|
||||
"normalized_path": None,
|
||||
}
|
||||
|
||||
# HOME-relativ normalisieren
|
||||
expanded = os.path.expanduser(raw_path)
|
||||
|
||||
# In absoluten Pfad umwandeln
|
||||
try:
|
||||
abs_path = os.path.abspath(expanded)
|
||||
except (ValueError, OSError) as e:
|
||||
return {"valid": False, "error": f"Ungültiger Pfad: {e}", "normalized_path": None}
|
||||
|
||||
# Prüfe: System-Verzeichnisse blockiert
|
||||
resolved = str(Path(abs_path).resolve())
|
||||
for blocked in BLOCKED_PREFIXES:
|
||||
if resolved == blocked or resolved.startswith(blocked + "/") or resolved.startswith(blocked + os.sep):
|
||||
return {
|
||||
"valid": False,
|
||||
"error": f"System-Verzeichnis '{blocked}' ist blockiert. Bitte wähle einen Pfad unter /home oder /tmp.",
|
||||
"normalized_path": None,
|
||||
}
|
||||
|
||||
return {"valid": True, "error": None, "normalized_path": resolved}
|
||||
|
||||
|
||||
def create_and_verify_fact_store(path: str) -> Dict[str, Any]:
|
||||
"""Erstelle das FAKT-Speicherort-Verzeichnis und prüfe Schreibbarkeit.
|
||||
|
||||
Args:
|
||||
path: Normalisierter absoluter Pfad.
|
||||
|
||||
Returns:
|
||||
Dict mit:
|
||||
- success (bool): True wenn Verzeichnis erstellt und schreibbar
|
||||
- error (str | None): Fehlermeldung bei Misserfolg
|
||||
"""
|
||||
try:
|
||||
os.makedirs(path, exist_ok=True)
|
||||
except OSError as e:
|
||||
return {"success": False, "error": f"Konnte Verzeichnis nicht erstellen: {e}"}
|
||||
|
||||
# Schreibtest
|
||||
try:
|
||||
test_file = os.path.join(path, ".hermes_write_test")
|
||||
with open(test_file, "w") as f:
|
||||
f.write("test")
|
||||
os.remove(test_file)
|
||||
except OSError as e:
|
||||
return {"success": False, "error": f"Schreibtest fehlgeschlagen: {e}"}
|
||||
|
||||
return {"success": True, "error": None}
|
||||
|
||||
|
||||
def verify_existing_fact_store(path: str) -> Dict[str, Any]:
|
||||
"""Prüfe ob ein bestehendes Verzeichnis referenzierbar und schreibbar ist.
|
||||
|
||||
Args:
|
||||
path: Normalisierter absoluter Pfad.
|
||||
|
||||
Returns:
|
||||
Dict mit:
|
||||
- success (bool): True wenn Verzeichnis existiert und schreibbar
|
||||
- error (str | None): Fehlermeldung bei Misserfolg
|
||||
"""
|
||||
if not os.path.isdir(path):
|
||||
return {"success": False, "error": f"Verzeichnis existiert nicht: {path}"}
|
||||
|
||||
# Schreibtest
|
||||
try:
|
||||
test_file = os.path.join(path, ".hermes_write_test")
|
||||
with open(test_file, "w") as f:
|
||||
f.write("test")
|
||||
os.remove(test_file)
|
||||
except OSError as e:
|
||||
return {"success": False, "error": f"Schreibtest fehlgeschlagen: {e}"}
|
||||
|
||||
return {"success": True, "error": None}
|
||||
|
||||
|
||||
def get_default_fact_store_path() -> str:
|
||||
"""Gibt den Standard-FAKT-Speicherort zurück (Option 1).
|
||||
|
||||
Returns:
|
||||
Absoluter Pfad zu .hermes/wissenssammlung im HOME-Verzeichnis.
|
||||
"""
|
||||
return os.path.expanduser("~/.hermes/wissenssammlung")
|
||||
Reference in New Issue
Block a user