"""Private state files shared by the Hermes runtime modules: atomic writes and an exclusive lock.""" from __future__ import annotations from contextlib import contextmanager import json import os import secrets def _write(path, value): path.parent.mkdir(parents=True, exist_ok=True, mode=0o700) temp = path.with_name('.' + path.name + '.' + secrets.token_hex(8)) try: fd = os.open(temp, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600) with os.fdopen(fd, 'w') as f: json.dump(value, f, ensure_ascii=False) f.flush(); os.fsync(f.fileno()) os.replace(temp, path) finally: temp.unlink(missing_ok=True) @contextmanager def _file_lock(path): path.parent.mkdir(parents=True, exist_ok=True, mode=0o700) fd = os.open(path, os.O_RDWR | os.O_CREAT, 0o600) with os.fdopen(fd, 'a+b') as f: if os.name == 'nt': import msvcrt f.seek(0); f.write(b'0'); f.flush(); f.seek(0) msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, 1) else: import fcntl fcntl.flock(f.fileno(), fcntl.LOCK_EX) try: yield finally: if os.name == 'nt': f.seek(0); msvcrt.locking(f.fileno(), msvcrt.LK_UNLCK, 1) else: fcntl.flock(f.fileno(), fcntl.LOCK_UN)