sessions.json/keys.json/settings.json etaient ecrits directement (Path.write_text) : un crash ou une coupure de courant pendant l'ecriture pouvait laisser un fichier tronque, sans aucun recours. - app/storage/atomic_file.py : ecriture .tmp + flush + fsync + os.replace() (atomique y compris sous Windows). - app/storage/json_store.py : load_json_document()/save_json_document(). Chaque sauvegarde conserve la version precedente dans un .bak ; au chargement, un fichier principal corrompu retombe automatiquement sur ce .bak avant de se rabattre sur des valeurs par defaut. Chaque document porte un schema_version, avec une chaine de migrations (vide pour l'instant) pour faire evoluer le format plus tard sans invalider les fichiers existants. - SessionStore et KeyManager utilisent ce module (schema_version=1) ; un fichier legacy sans schema_version reste charge normalement et n'est reecrit qu'a la prochaine sauvegarde naturelle (comportement de "premiere ecriture" inchange par rapport a avant). - settings.json (theme.py) et known_hosts (known_hosts_manager.py, via paramiko.HostKeys.save() redirige vers un .tmp puis os.replace()) beneficient aussi de l'ecriture atomique. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
82 lines
3.1 KiB
Python
82 lines
3.1 KiB
Python
"""Chargement/sauvegarde de documents JSON versionnes (sessions, cles, ...).
|
|
|
|
Chaque document porte un `schema_version`. `migrations` associe une version de
|
|
depart a une fonction qui transforme le document vers la version suivante ; le
|
|
chargement applique la chaine jusqu'a `current_version`, ce qui permet de faire
|
|
evoluer le format plus tard sans invalider les fichiers existants.
|
|
|
|
La sauvegarde est atomique (voir atomic_file) et conserve la version precedente
|
|
dans un `.bak` : si l'ecriture suivante produit un fichier corrompu (crash,
|
|
disque plein), le chargement suivant retombe automatiquement sur cette sauvegarde
|
|
plutot que de perdre les donnees de l'utilisateur.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
from typing import Callable
|
|
|
|
from app.storage.atomic_file import atomic_write_bytes
|
|
|
|
MigrationMap = dict[int, Callable[[dict], dict]]
|
|
|
|
|
|
def _bak_path(path: Path) -> Path:
|
|
return path.with_name(path.name + ".bak")
|
|
|
|
|
|
def _migrate(document: dict, current_version: int, migrations: MigrationMap) -> dict:
|
|
version = int(document.get("schema_version", 0) or 0)
|
|
while version < current_version:
|
|
migrate = migrations.get(version)
|
|
if migrate is None:
|
|
break # pas de chemin de migration connu : le document est garde tel quel
|
|
document = migrate(document)
|
|
version = int(document.get("schema_version", version + 1))
|
|
document["schema_version"] = current_version
|
|
return document
|
|
|
|
|
|
def load_json_document(
|
|
path: Path,
|
|
*,
|
|
default: dict,
|
|
current_version: int,
|
|
migrations: MigrationMap | None = None,
|
|
) -> tuple[dict, str | None]:
|
|
"""Renvoie (document, avertissement). `avertissement` est None si le fichier
|
|
principal a ete lu normalement ; sinon il decrit la recuperation effectuee
|
|
(restauration depuis .bak, ou repli sur les valeurs par defaut)."""
|
|
migrations = migrations or {}
|
|
warning: str | None = None
|
|
|
|
for candidate, is_backup in ((Path(path), False), (_bak_path(Path(path)), True)):
|
|
if not candidate.exists():
|
|
continue
|
|
try:
|
|
raw = json.loads(candidate.read_text(encoding="utf-8"))
|
|
except (OSError, json.JSONDecodeError, UnicodeDecodeError) as exc:
|
|
warning = f"{candidate.name} illisible ({exc})"
|
|
continue
|
|
if not isinstance(raw, dict):
|
|
warning = f"{candidate.name} invalide (contenu inattendu)"
|
|
continue
|
|
if is_backup:
|
|
warning = f"{Path(path).name} illisible, restaure depuis {candidate.name}"
|
|
return _migrate(raw, current_version, migrations), warning
|
|
|
|
if warning is None and not Path(path).exists():
|
|
pass # premier lancement : pas d'avertissement, juste les valeurs par defaut
|
|
return _migrate(dict(default), current_version, migrations), warning
|
|
|
|
|
|
def save_json_document(path: Path, document: dict) -> None:
|
|
path = Path(path)
|
|
if path.exists():
|
|
try:
|
|
atomic_write_bytes(_bak_path(path), path.read_bytes())
|
|
except OSError:
|
|
pass
|
|
payload = json.dumps(document, indent=2, ensure_ascii=False)
|
|
atomic_write_bytes(path, payload.encode("utf-8"))
|