"""Open the official backup UI; version selection and writes stay native.""" from dataclasses import dataclass import json import hashlib import os from pathlib import Path from pathlib import PureWindowsPath import re import stat import subprocess import sys import time from uuid import uuid4 def native_backup_executable(mentor_bin=''): from .mentor_paths import discover_sdd_home home = discover_sdd_home(mentor_bin) executable = home/'iCDB/win32/bin/iCDBProjectBackup.exe' if home else None if executable is None or not executable.is_file(): raise ValueError('未找到 iCDB Project Backup,请检查 Mentor 安装位置。') return executable def native_backup_project(library, block): from .verify_runner import INVALID_NAME_CHARS library = Path(library).absolute() if not block or block in ('.', '..') or any(c in block for c in INVALID_NAME_CHARS): raise ValueError('CBB 名称非法。') if not library.is_file(): raise ValueError('中心库文件不存在。') work = library.parent/'ReusableBlocksLP'/block/'Work' expected = library.parent.resolve()/'ReusableBlocksLP'/block/'Work' if work.resolve() != expected: raise ValueError('工程路径通过链接指向其他位置。') projects = list(work.glob('*.prj')) if len(projects) != 1 or not projects[0].is_file(): raise ValueError('无法唯一确定所选 CBB 的工程 PRJ。') project = projects[0] if (project.lstat().st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT or not project.resolve().is_relative_to(expected)): raise ValueError('工程文件通过链接指向其他位置。') return project def native_backup_target(request, *, mode='versions', target_database=None): """Bind the official tool without requiring an openable/healthy PCB.""" from .backup_recovery import _project_text from .maintenance_entry import maintenance_project_files if mode not in ('versions', 'repair'): raise ValueError('NATIVE_TARGET_MODE_INVALID') project = native_backup_project(request.lmc_file, request.block_name) candidates = (project,) if target_database is not None: pcbs = list((project.parent/'PCB').glob('*.pcb')) if len(pcbs) != 1: raise ValueError('NATIVE_TARGET_UNCONFIRMED: 无法唯一绑定 PCB 工程') candidates = maintenance_project_files(project, pcbs[0]) matches = [] for config in candidates: values = re.findall(r'^KEY iCDBDir "([^"\r\n]+)"\s*$', _project_text(config), re.M) if len(values) != 1 or not values[0] or PureWindowsPath(values[0]).root or PureWindowsPath(values[0]).drive: if config == project or target_database is None: raise ValueError('NATIVE_TARGET_UNCONFIRMED: 主 PRJ 的 iCDBDir 必须唯一且为工程内相对路径,不支持绝对路径或多条记录') continue database = (config.parent/values[0]).resolve() if not database.is_relative_to(project.parent.resolve()): raise ValueError('NATIVE_TARGET_UNCONFIRMED: 数据库不在当前工程内') if target_database is None or database == Path(target_database).resolve(): matches.append((config, database)) if len(matches) != 1: raise ValueError('NATIVE_TARGET_UNCONFIRMED: 诊断数据库未绑定唯一工程 PRJ') return matches[0] @dataclass class NativeBackupSession: process: object job: Path library: Path block: str path_lease: object = None mode: str = 'versions' project: Path | None = None database: Path | None = None session_record_path: Path | None = None operation_id: str | None = None def launch_native_backup(request, *, mode='versions', target_database=None): from .library_lock import recheck_previous_release from .session_record import register_worker_session from .operation_queue import check_active, native_channel, task_stop, commit_worker_start project, database = native_backup_target(request, mode=mode, target_database=target_database) native_backup_executable(request.mentor_bin) root = Path(request.evidence_dir).resolve()/('native-backup-'+uuid4().hex[:12]) library_root = Path(request.lmc_file).resolve().parent if root.is_relative_to(library_root) or library_root.is_relative_to(root): raise ValueError('运行记录目录需位于中心库之外。') root.mkdir(parents=True) job = root/'request.json' recheck_previous_release(request.lmc_file, root/'previous-release', block_name=request.block_name, stop=task_stop(None)) record = {'library': str(request.lmc_file), 'block': request.block_name, 'project': str(project), 'database': str(database), 'mode': mode, 'project_sha256': hashlib.sha256(project.read_bytes()).hexdigest(), 'target_database': str(target_database) if target_database is not None else None, 'mentor_bin': request.mentor_bin, 'automatic_repair': False, 'operation': 'native_backup', 'operation_id': root.name, 'session_record_path': str(root/'session.json'), 'start_gate': str(root/'start-worker.signal'), 'start_gate_protocol': 'event-v1', 'cancel_signal': str(root/'cancel-worker.signal'), 'supervisor_pid': os.getpid(), 'desktop': None} job.write_text(json.dumps(record, ensure_ascii=False), 'utf-8') command = ([sys.executable] if getattr(sys, 'frozen', False) else [sys.executable, '-m', 'cbb_refresh_verify'])+['--native-backup-worker', str(job)] try: check_active() process = subprocess.Popen(command, creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0)) register_worker_session(process, record) with native_channel(): check_active() commit_worker_start(process, record) except BaseException: Path(record['cancel_signal']).touch() # The unadmitted child exits without native launch. raise return NativeBackupSession(process, job, Path(request.lmc_file), request.block_name, mode=mode, project=project, database=database, session_record_path=Path(record['session_record_path']), operation_id=record['operation_id']) class _BackupJobWriter: """Independent Job handle keeps future descendants inside the writer boundary.""" def __init__(self, job): import win32api, win32con current = win32api.GetCurrentProcess() self.job = win32api.DuplicateHandle(current, job, current, 0, False, win32con.DUPLICATE_SAME_ACCESS) def poll(self): import win32job if self.job is None: return 0 if win32job.QueryInformationJobObject(self.job, win32job.JobObjectBasicProcessIdList): return None self.job.Close(); self.job = None return 0 class _NativeBackupProcess: """Track this visible tool and its descendants without killing on close.""" def __init__(self, command, cwd, *, before_resume=None): import win32con, win32job, win32process self.job = win32job.CreateJobObject(None, '') startup = win32process.STARTUPINFO() try: self.handle, thread, self.pid, _ = win32process.CreateProcess( command[0], subprocess.list2cmdline(command), None, None, False, win32con.CREATE_SUSPENDED, None, str(cwd), startup) except Exception: self.job.Close() raise try: win32job.AssignProcessToJobObject(self.job, self.handle) if before_resume is not None: before_resume(self) win32process.ResumeThread(thread) except Exception: # No native code has run while suspended; there can be no DB write. win32process.TerminateProcess(self.handle, 1) self.handle.Close(); self.job.Close() raise finally: thread.Close() def poll(self): import win32process code = win32process.GetExitCodeProcess(self.handle) return None if code == 259 else code def session_pids(self): import win32job return win32job.QueryInformationJobObject(self.job, win32job.JobObjectBasicProcessIdList) def close(self): self.handle.Close(); self.job.Close() def run_native_backup_worker(job, *, admission_state=None): """Publish admission refusal only when this exact worker never entered native work.""" from .desktop_worker import run_native_worker from .management import _write_json from .session_record import IDENTITY_KEYS, process_identity, read_session_record job = Path(job) entered = False request = json.loads(job.read_text('utf-8')) identity = process_identity(os.getpid()) def registered(): try: path = Path(request['session_record_path']) session = read_session_record(path, evidence_root=job.parent) return (session['operation_id'] == request['operation_id'] and session.get('operation') == 'native_backup' and session.get('library') == request['library'] and session.get('block') == request['block'] and request.get('project') in session.get('resource_scope', {}).get('projects', ()) and any(all(process.get(key) == identity[key] for key in IDENTITY_KEYS) for process in session.get('processes', ()) if process.get('role') == 'worker')) except (OSError, ValueError, KeyError, TypeError): return False # Unknown registration never establishes a safe terminal result. def execute(path): nonlocal entered if not registered(): raise ValueError('NATIVE_BACKUP_ADMISSION_UNCONFIRMED: 当前工程与工作进程未登记') entered = True return native_backup_worker_main(path) try: return run_native_worker(execute, job, admission_state=admission_state) except Exception as error: bound = registered() _write_json(job.with_name('result.json'), {'status': 'FAILED', 'detail': str(error), 'operation_id': request.get('operation_id'), 'worker_identity': identity, 'library': request.get('library'), 'block': request.get('block'), 'mode': request.get('mode', 'versions'), 'project': request.get('project'), 'database': request.get('database'), 'worker_entered': entered, 'native_started': None if entered else False, 'safe_to_continue': bound and not entered, 'restore_confirmed': False, 'repair_confirmed': False, 'validation_passed': False, 'automatic_repair': False}) return 1 def native_backup_worker_main(job, *, process_factory=None): from .library_lock import LibraryLock from .management import _write_json job = Path(job) result = {'status': 'FAILED', 'safe_to_continue': True, 'restore_confirmed': False, 'repair_confirmed': False, 'validation_passed': False, 'automatic_repair': False} native = None def write_evidence(name, value): try: _write_json(job.with_name(name), value) except Exception as error: # Evidence storage must never end supervision of a live transaction. result['evidence_error'] = str(error) try: record = json.loads(job.read_text('utf-8')) library, block = Path(record['library']), record['block'] result.update(library=str(library), block=block, operation_id=record.get('operation_id')) from types import SimpleNamespace mode = record.get('mode', 'versions') project, database = native_backup_target(SimpleNamespace(lmc_file=library, block_name=block), mode=mode, target_database=record.get('target_database')) if project != Path(record['project']): raise ValueError('工程身份已变化,未打开原生工具。') if record.get('project_sha256') and hashlib.sha256(project.read_bytes()).hexdigest() != record['project_sha256']: raise ValueError('工程内容身份已变化,未打开原生工具。') if record.get('database') and database != Path(record['database']): raise ValueError('数据库身份已变化,未打开原生工具。') result.update(mode=mode, project=str(project), database=str(database), session_record_path=record.get('session_record_path')) executable = native_backup_executable(record.get('mentor_bin', '')) released = True with LibraryLock(library, checkpoint=bool(record.get('session_record_path')), block_name=block, safe_to_release=lambda: released): from .maintenance_entry import _assert_native_idle_for _assert_native_idle_for(SimpleNamespace(project=project, database=database, library=library)) def admitted(process): from .session_record import update_worker_state from .desktop_worker import register_native_writer if record.get('session_record_path'): update_worker_state(record, 'NATIVE_CALL', identities=(('native', process.pid),), required=True) register_native_writer(_BackupJobWriter(process.job)) command = [str(executable), '-p', str(project)] # User alone invokes Repair in the official UI. native = (process_factory(command, project.parent) if process_factory else _NativeBackupProcess(command, project.parent, before_resume=admitted)) released = False result.update(status='OPEN', native_pid=native.pid, project=str(project), safe_to_continue=False) write_evidence('state.json', result) waiting = False while True: try: pids, exit_code = native.session_pids(), native.poll() except Exception as error: write_evidence('state.json', {**result, 'status': 'RELEASE_UNCONFIRMED', 'detail': str(error)}) time.sleep(.5) continue # Keep the lease while the live session cannot be observed. if not pids and exit_code is not None: break if exit_code is not None and not waiting: waiting = True write_evidence('state.json', {**result, 'status': 'WAITING_RELEASE'}) time.sleep(.25) released = True result.update(status='CLOSED', native_exit_code=exit_code, safe_to_continue=True) result['release'] = {'native_exited': True, 'remaining_pids': [], 'safe_to_continue': True} return 0 if result['native_exit_code'] == 0 else 1 except Exception as error: result.update(status='FAILED', detail=str(error), safe_to_continue=native is None) return 1 finally: write_evidence('result.json', result) if native is not None: native.close() # No kill-on-close: never interrupt a native transaction.