fix: preserve mapped native paths and retain probe ownership guards
This commit is contained in:
1 parent
dd1fcea7e2
commit
46c3e72364
7 files changed
+365
-36
No files matched your search
@@ -18,6 +18,7 @@ from uuid import uuid4
|
||||
from .management import (Capability, Catalog, CatalogEntry, NativeOutcome,
|
||||
engineering_identity, validate_name, _write_json)
|
||||
from .scoped_manifest import hash_file
|
||||
from .network_path import absolute_preserving_drive
|
||||
|
||||
_retained_workers = []
|
||||
_BINARY_PROOF = {
|
||||
@@ -55,6 +56,12 @@ class NativeManagementAdapter:
|
||||
self.home = Path(mentor_home) if mentor_home is not None else discover_sdd_home()
|
||||
self.worker_factory = worker_factory
|
||||
self.timeout_seconds = timeout_seconds
|
||||
self.mapped_drive = ''
|
||||
self.mapped_remote_root = ''
|
||||
|
||||
def bind_library_mapping(self, drive, remote_root):
|
||||
self.mapped_drive = drive or ''
|
||||
self.mapped_remote_root = remote_root or ''
|
||||
|
||||
def capabilities(self):
|
||||
reason = ''
|
||||
@@ -74,14 +81,36 @@ class NativeManagementAdapter:
|
||||
}
|
||||
|
||||
def _worker(self, job, evidence):
|
||||
from .desktop_worker import DesktopWorker
|
||||
evidence = Path(evidence)
|
||||
evidence.mkdir(parents=True, exist_ok=False)
|
||||
request_path = evidence/'request.json'
|
||||
desktop = 'CbbManagement_' + uuid4().hex[:12]
|
||||
timeout = float(job.get('timeout_seconds', self.timeout_seconds))
|
||||
job = {**job, 'desktop': desktop, 'mentor_home': str(self.home or ''),
|
||||
'mapped_drive': job.get('mapped_drive', self.mapped_drive),
|
||||
'mapped_remote_root': job.get('mapped_remote_root', self.mapped_remote_root),
|
||||
'timeout_seconds': timeout, 'supervisor_pid': os.getpid()}
|
||||
if job['operation'] == 'probe_create_add':
|
||||
# Prepare the fixed NEW fixture before a native owner can start.
|
||||
# The parent owns its sidecar for the entire create/add invocation.
|
||||
from .library_lock import LibraryLock
|
||||
library = absolute_preserving_drive(evidence)/'fixture/mentor_lib.lmc'
|
||||
if Path(job['library']).resolve() != library.resolve() or not job.get('probe_token'):
|
||||
raise ValueError('隔离创建目标必须为本次工作目录内的新库。')
|
||||
library.parent.mkdir(exist_ok=False)
|
||||
_write_json(evidence/'probe-prepared.json', {'library': str(library), 'token': job['probe_token'],
|
||||
'supervisor_pid': os.getpid()})
|
||||
owner_safe = True
|
||||
with LibraryLock(library, safe_to_release=lambda: owner_safe):
|
||||
owner_safe = False
|
||||
result = self._launch_worker(job, evidence)
|
||||
owner_safe = result.get('safe_to_continue') is True
|
||||
return result
|
||||
return self._launch_worker(job, evidence)
|
||||
|
||||
def _launch_worker(self, job, evidence):
|
||||
from .desktop_worker import DesktopWorker
|
||||
request_path = evidence/'request.json'
|
||||
desktop, timeout = job['desktop'], job['timeout_seconds']
|
||||
_write_json(request_path, job)
|
||||
command = ([sys.executable] if getattr(sys, 'frozen', False)
|
||||
else [sys.executable, '-m', 'cbb_refresh_verify'])
|
||||
@@ -106,7 +135,7 @@ class NativeManagementAdapter:
|
||||
worker.close()
|
||||
|
||||
def catalog(self, library, evidence):
|
||||
result = self._worker({'operation': 'catalog', 'library': str(library)}, evidence)
|
||||
result = self._worker({'operation': 'catalog', 'library': str(absolute_preserving_drive(library))}, evidence)
|
||||
if not result.get('success') or not result.get('safe_to_continue'):
|
||||
raise RuntimeError(result.get('detail', '原生目录复读未确认,禁止文件夹回退。'))
|
||||
return Catalog(Path(result['library']), int(result['flow']),
|
||||
@@ -119,8 +148,9 @@ class NativeManagementAdapter:
|
||||
removal_input(request.name, request.partition)
|
||||
if request.partition != 'Logical-Physical':
|
||||
return NativeOutcome(False, True, '仅已验证 Logical-Physical 原生移除。')
|
||||
result = self._worker({'operation': 'remove', 'library': str(request.library),
|
||||
result = self._worker({'operation': 'remove', 'library': str(absolute_preserving_drive(request.library)),
|
||||
'name': request.name, 'partition': request.partition,
|
||||
'mapped_drive': request.mapped_drive, 'mapped_remote_root': request.mapped_remote_root,
|
||||
'timeout_seconds': request.timeout_seconds}, evidence)
|
||||
return NativeOutcome(bool(result.get('success')), bool(result.get('safe_to_continue')), result.get('detail', ''))
|
||||
|
||||
@@ -135,22 +165,36 @@ class NativeManagementAdapter:
|
||||
|
||||
|
||||
def prepare_probe_library(job, root):
|
||||
"""Diagnostic create is restricted to a new fixed descendant of this job."""
|
||||
expected = Path(root).resolve()/'fixture/mentor_lib.lmc'
|
||||
if Path(job['library']).resolve() != expected or expected.parent.exists():
|
||||
raise ValueError('诊断只能创建当前任务下全新独占 fixture/mentor_lib.lmc。')
|
||||
expected.parent.mkdir()
|
||||
"""Validate a NEW fixed fixture already guarded by its supervisor."""
|
||||
from .library_lock import blocker_path, checkpoint_record
|
||||
expected = absolute_preserving_drive(root)/'fixture/mentor_lib.lmc'
|
||||
try:
|
||||
prepared = json.loads((Path(root)/'probe-prepared.json').read_text(encoding='utf-8'))
|
||||
guard = checkpoint_record(blocker_path(expected))
|
||||
allowed = {'.mentor_lib.cbb-upload.lock', '.mentor_lib.cbb-upload.blocked.json'}
|
||||
valid = (Path(job['library']).resolve() == expected.resolve() and not expected.exists()
|
||||
and expected.parent.is_dir() and {p.name for p in expected.parent.iterdir()} == allowed
|
||||
and prepared.get('token') == job.get('probe_token') and bool(job.get('probe_token'))
|
||||
and Path(prepared['library']).resolve() == expected.resolve()
|
||||
and prepared.get('supervisor_pid') == job.get('supervisor_pid')
|
||||
and guard is not None and guard['pid'] == job.get('supervisor_pid')
|
||||
and Path(guard['library']).resolve() == expected.resolve()
|
||||
and guard['status'] == 'OPERATION_IN_PROGRESS')
|
||||
except (OSError, ValueError, KeyError):
|
||||
valid = False
|
||||
if not valid:
|
||||
raise ValueError('诊断只能创建当前监督进程已持锁的全新 fixture/mentor_lib.lmc。')
|
||||
return expected
|
||||
|
||||
|
||||
def validate_probe_readd(job, root):
|
||||
expected = Path(root).parent.resolve()/'native-add/fixture/mentor_lib.lmc'
|
||||
expected = absolute_preserving_drive(root).parent/'native-add/fixture/mentor_lib.lmc'
|
||||
try:
|
||||
owner = json.loads((Path(root).parent/'probe-owner.json').read_text(encoding='utf-8'))
|
||||
except (OSError, ValueError) as error:
|
||||
raise ValueError('没有本次原生创建的隔离库所有权记录。') from error
|
||||
if (Path(job['library']).resolve() != expected or not expected.is_file()
|
||||
or owner.get('library') != str(expected) or not job.get('probe_token')
|
||||
if (Path(job['library']).resolve() != expected.resolve() or not expected.is_file()
|
||||
or not owner.get('library') or Path(owner['library']).resolve() != expected.resolve() or not job.get('probe_token')
|
||||
or owner.get('token') != job['probe_token']):
|
||||
raise ValueError('重新导入诊断仅允许本次原生新建的固定隔离库。')
|
||||
return expected
|
||||
@@ -288,18 +332,19 @@ def run_isolated_probe(source, root, name, *, mentor_home=None, replacement_sour
|
||||
from .scoped_manifest import ManifestScope
|
||||
from .scoped_manifest import copy_verified, build_manifest
|
||||
import pythoncom, win32con, win32event
|
||||
root = Path(root).resolve()
|
||||
root = absolute_preserving_drive(root)
|
||||
if root.exists():
|
||||
raise ValueError('诊断目录必须是全新目录,不能复用现有中心库或旧 fixture。')
|
||||
source, _, top = source_project(source)
|
||||
validate_name(name)
|
||||
if root.is_relative_to(source.parent) or source.parent.is_relative_to(root):
|
||||
if root.resolve().is_relative_to(source.parent) or source.parent.is_relative_to(root.resolve()):
|
||||
raise ValueError('诊断目录不能与来源工程相互包含。')
|
||||
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
|
||||
acquired = False
|
||||
token = uuid4().hex
|
||||
record = {'operation': 'isolated_add_probe', 'source': str(source), 'name': name,
|
||||
'production_capability_enabled': False}
|
||||
owner_safe = True
|
||||
from .com_runtime import initialize_com, uninitialize_com
|
||||
initialized = initialize_com()
|
||||
try:
|
||||
@@ -314,13 +359,20 @@ def run_isolated_probe(source, root, name, *, mentor_home=None, replacement_sour
|
||||
if not adapter.capabilities()['remove'].supported:
|
||||
raise RuntimeError('诊断要求已识别的 EE7.9.5 原生安装。')
|
||||
library = root/'native-add/fixture/mentor_lib.lmc'
|
||||
def read_catalog(evidence):
|
||||
nonlocal owner_safe
|
||||
owner_safe = False
|
||||
catalog = adapter.catalog(library, evidence)
|
||||
owner_safe = catalog.safe_to_continue
|
||||
return catalog
|
||||
native = adapter._worker({'operation': 'probe_create_add', 'library': str(library),
|
||||
'source': str(staged), 'name': name, 'top_level_block': top,
|
||||
'probe_token': token}, root/'native-add')
|
||||
record['native'] = native
|
||||
record['source_unchanged'] = build_manifest(source.parent) == manifest
|
||||
if native.get('success') and native.get('safe_to_continue'):
|
||||
catalog = adapter.catalog(library, root/'reopen')
|
||||
with LibraryLock(library, safe_to_release=lambda: owner_safe):
|
||||
catalog = read_catalog(root/'reopen')
|
||||
record['catalog'] = asdict(catalog)
|
||||
matches = [entry for entry in catalog.entries if entry.name == name and entry.partition == 'Logical-Physical']
|
||||
record['identity_matches'] = len(matches) == 1 and matches[0].engineering_identity == identity
|
||||
@@ -330,35 +382,39 @@ def run_isolated_probe(source, root, name, *, mentor_home=None, replacement_sour
|
||||
_write_json(root/'probe-result.json', record)
|
||||
if record['accepted'] and replacement_source is not None:
|
||||
replacement, _, replacement_top = source_project(replacement_source)
|
||||
if replacement.is_relative_to(root) or root.is_relative_to(replacement.parent):
|
||||
if replacement.is_relative_to(root.resolve()) or root.resolve().is_relative_to(replacement.parent):
|
||||
raise ValueError('更新诊断来源必须位于隔离库与证据目录之外。')
|
||||
replacement_manifest = copy_verified(replacement.parent, root/'source-replacement')
|
||||
staged_replacement = root/'source-replacement'/replacement.name
|
||||
replacement_identity = engineering_identity(staged_replacement)
|
||||
record['update'] = {'stage': 'backup', 'accepted': False, 'source': str(replacement)}
|
||||
record['accepted'] = False
|
||||
with LibraryLock(library):
|
||||
with LibraryLock(library, safe_to_release=lambda: owner_safe):
|
||||
copy_verified(library.parent, root/'library-before-update',
|
||||
scope=ManifestScope.library(name, library.name))
|
||||
request = ManagementRequest('remove', library, name, evidence_dir=root)
|
||||
owner_safe = False
|
||||
removal = adapter.remove(request, root/'native-remove')
|
||||
owner_safe = removal.safe_to_continue
|
||||
record['update']['removal'] = asdict(removal)
|
||||
_write_json(root/'probe-result.json', record)
|
||||
if not removal.success or not removal.safe_to_continue:
|
||||
return record
|
||||
missing = adapter.catalog(library, root/'after-remove')
|
||||
missing = read_catalog(root/'after-remove')
|
||||
if any(e.name == name for e in missing.entries):
|
||||
record['update']['error'] = '原生移除后条目仍存在'
|
||||
_write_json(root/'probe-result.json', record)
|
||||
return record
|
||||
record['update']['stage'] = 'removed'
|
||||
_write_json(root/'probe-result.json', record)
|
||||
owner_safe = False
|
||||
readd = adapter._worker({'operation': 'probe_readd', 'library': str(library),
|
||||
'source': str(staged_replacement), 'name': name, 'top_level_block': replacement_top,
|
||||
'probe_token': token}, root/'native-readd')
|
||||
owner_safe = readd.get('safe_to_continue') is True
|
||||
record['update']['readd'] = readd
|
||||
if readd.get('success') and readd.get('safe_to_continue'):
|
||||
updated = adapter.catalog(library, root/'reopen-update')
|
||||
updated = read_catalog(root/'reopen-update')
|
||||
record['update']['catalog'] = asdict(updated)
|
||||
entries = [e for e in updated.entries if e.name == name and e.partition == 'Logical-Physical']
|
||||
record['update']['accepted'] = (len(entries) == 1
|
||||
@@ -483,7 +539,7 @@ def _catalog_com(job, root):
|
||||
else:
|
||||
if job['operation'] == 'probe_readd':
|
||||
validate_probe_readd(job, root)
|
||||
library = app.OpenLibrary(str(Path(job['library']).resolve()))
|
||||
library = app.OpenLibrary(str(absolute_preserving_drive(job['library'])))
|
||||
if refused:
|
||||
raise RuntimeError('目录读取需要升级中心库或选择流程;已拒绝,目标未获准自动升级。')
|
||||
if library is None or Path(str(library.FullName)).resolve() != Path(job['library']).resolve():
|
||||
@@ -541,6 +597,9 @@ def management_worker_main(request_path):
|
||||
_, desktop = _current_desktop()
|
||||
if desktop != job['desktop'] or not desktop.startswith('CbbManagement_'):
|
||||
raise RuntimeError('原生管理操作仅允许在本次私有桌面执行。')
|
||||
if job.get('mapped_drive') and job.get('mapped_remote_root'):
|
||||
from .network_path import ensure_mapped_drive_visible
|
||||
ensure_mapped_drive_visible(job['mapped_drive'], job['mapped_remote_root'])
|
||||
if job['operation'] in ('catalog', 'probe_create_add', 'probe_readd'):
|
||||
result = _catalog_native(job, root)
|
||||
elif job['operation'] == 'remove':
|
||||
|
||||
Reference in new issue
Block a user