Coverage for src/copytrading_engine/backup/restore/gate.py: 80%
61 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-16 16:55 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-16 16:55 +0000
1"""Fail-closed restore gate shared by bootstrap and the trading lifecycle."""
3from __future__ import annotations
5import os
6import stat
7from pathlib import Path
8from uuid import uuid4
10RESTORE_MANUAL_DISABLED_MARKER = ".restore-manual-disabled"
13def restore_manual_disabled_path(owner_support_directory: Path) -> Path:
14 return owner_support_directory / RESTORE_MANUAL_DISABLED_MARKER
17def restore_manual_disabled(marker_path: Path) -> bool:
18 """Treat every present or unreadable marker path as an active safety gate."""
19 try:
20 os.lstat(marker_path)
21 except FileNotFoundError:
22 return False
23 except OSError:
24 return True
25 return True
28def persist_restore_manual_disabled(marker_path: Path, payload: bytes) -> None:
29 """Atomically persist the gate and sync both its contents and directory entry."""
30 parent = marker_path.parent
31 try:
32 parent_info = os.lstat(parent)
33 except OSError as exc:
34 raise OSError("restore owner directory is unavailable") from exc
35 if not stat.S_ISDIR(parent_info.st_mode) or stat.S_ISLNK(parent_info.st_mode): 35 ↛ 36line 35 didn't jump to line 36 because the condition on line 35 was never true
36 raise OSError("restore owner directory is invalid")
37 temporary = parent / f".{RESTORE_MANUAL_DISABLED_MARKER}.{uuid4().hex}.tmp"
38 directory_flags = os.O_RDONLY | getattr(os, "O_DIRECTORY", 0) | getattr(os, "O_NOFOLLOW", 0)
39 directory_descriptor = os.open(parent, directory_flags)
40 file_descriptor = -1
41 try:
42 file_descriptor = os.open(
43 temporary,
44 os.O_CREAT | os.O_EXCL | os.O_WRONLY | getattr(os, "O_NOFOLLOW", 0),
45 0o600,
46 )
47 remaining = memoryview(payload)
48 while remaining:
49 written = os.write(file_descriptor, remaining)
50 if written <= 0: 50 ↛ 51line 50 didn't jump to line 51 because the condition on line 50 was never true
51 raise OSError("restore gate could not be written")
52 remaining = remaining[written:]
53 os.fsync(file_descriptor)
54 os.close(file_descriptor)
55 file_descriptor = -1
56 os.replace(temporary, marker_path)
57 os.fsync(directory_descriptor)
58 finally:
59 if file_descriptor >= 0: 59 ↛ 60line 59 didn't jump to line 60 because the condition on line 59 was never true
60 os.close(file_descriptor)
61 try:
62 os.unlink(temporary)
63 except FileNotFoundError:
64 pass
65 os.close(directory_descriptor)
68def clear_restore_manual_disabled(marker_path: Path) -> None:
69 """Remove a valid gate and sync the owner directory after a safe transition."""
70 try:
71 info = os.lstat(marker_path)
72 except FileNotFoundError:
73 return
74 if not stat.S_ISREG(info.st_mode) or stat.S_ISLNK(info.st_mode): 74 ↛ 75line 74 didn't jump to line 75 because the condition on line 74 was never true
75 raise OSError("restore gate marker is invalid")
76 flags = os.O_RDONLY | getattr(os, "O_DIRECTORY", 0) | getattr(os, "O_NOFOLLOW", 0)
77 directory_descriptor = os.open(marker_path.parent, flags)
78 try:
79 os.unlink(marker_path)
80 os.fsync(directory_descriptor)
81 finally:
82 os.close(directory_descriptor)