fix: address review 414 write consent and native ownership boundaries
This commit is contained in:
1 parent
1572e2d8ca
commit
fee4a67ff1
19 files changed
+1008
-64
No files matched your search
@@ -28,32 +28,44 @@ def _paths(root):
|
||||
return list(_scan(root))
|
||||
|
||||
|
||||
def _idle_check(library, project, pcb):
|
||||
from .mentor_adapter import _process_details
|
||||
def _idle_check(library, project, pcb, *, confirmation=None, evidence=None, block=''):
|
||||
from .owner_probe import query_owners
|
||||
from .icdb_ownership import database_directories, unreleased_addresses
|
||||
from contextlib import ExitStack
|
||||
import win32con, win32file
|
||||
directories = database_directories(library, project, pcb)
|
||||
scope = (Path(project).parent, *directories)
|
||||
for name in ('ExpeditionPCB.exe', 'LibraryManager.exe', 'viewdraw.exe',
|
||||
'iCDBProjectBackup.exe', 'ReuseBlockImporter.exe'):
|
||||
for pid, executable, command, parent in _process_details(name):
|
||||
match = _native_scope_match(command, roots=scope, files=(library, project, pcb))
|
||||
def owners():
|
||||
unknown=[]
|
||||
names=('ExpeditionPCB.exe','LibraryManager.exe','viewdraw.exe','iCDBProjectBackup.exe','ReuseBlockImporter.exe')
|
||||
for row in query_owners(names):
|
||||
match=_native_scope_match(row['command'],roots=scope,files=(library,project,pcb))
|
||||
if match is True:
|
||||
raise ValueError(f'TARGET_NATIVE_OWNER_ACTIVE: 目标工程或共享库有原生会话,PID={pid}')
|
||||
# Pathless clients do not identify this target. The address and
|
||||
# exclusive file probes below still reject actual target occupancy.
|
||||
addresses = unreleased_addresses(library, project, pcb)
|
||||
if addresses:
|
||||
raise ValueError(f'TARGET_DATABASE_ADDRESS_UNCONFIRMED: 地址记录需原生确认;未据此判定正在占用:{addresses}')
|
||||
with ExitStack() as stack:
|
||||
paths = [Path(library), Path(pcb).parent/'Layout/LayoutDB.lyt',
|
||||
*(directory/'icdb.dat' for directory in directories)]
|
||||
for path in dict.fromkeys(paths):
|
||||
if path.is_file():
|
||||
handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None,
|
||||
win32con.OPEN_EXISTING, 0, None)
|
||||
stack.callback(handle.Close)
|
||||
raise ValueError(f'TARGET_NATIVE_OWNER_ACTIVE: 目标工程或共享库有原生会话,PID={row["pid"]}')
|
||||
if match is None:unknown.append(row)
|
||||
return unknown
|
||||
def resources():
|
||||
addresses=unreleased_addresses(library,project,pcb)
|
||||
if addresses:
|
||||
raise ValueError(f'TARGET_DATABASE_ADDRESS_UNCONFIRMED: 地址记录需原生确认;未据此判定正在占用:{addresses}')
|
||||
with ExitStack() as stack:
|
||||
paths=[Path(library),Path(pcb).parent/'Layout/LayoutDB.lyt',
|
||||
*(directory/'icdb.dat' for directory in directories)]
|
||||
for path in dict.fromkeys(paths):
|
||||
if path.is_file():
|
||||
handle=win32file.CreateFile(str(path),win32con.GENERIC_READ,0,None,
|
||||
win32con.OPEN_EXISTING,0,None)
|
||||
stack.callback(handle.Close)
|
||||
unknown=owners()
|
||||
resources() # Never hold exclusive probe handles while waiting for a user.
|
||||
if unknown:
|
||||
if confirmation is None:
|
||||
raise ValueError('TARGET_NATIVE_OWNER_UNCONFIRMED: 原生程序的工程归属未知,写入前需要确认。')
|
||||
confirmation.confirm(library,unknown,evidence=evidence,block=block)
|
||||
unknown=owners()
|
||||
if unknown and (confirmation is None or not confirmation.confirmed(library,unknown)):
|
||||
raise ValueError('TARGET_NATIVE_OWNER_UNCONFIRMED: 占用确认后出现新的未知进程,请重新检查。')
|
||||
resources()
|
||||
|
||||
|
||||
def _native_scope_match(command, *, roots=(), files=()):
|
||||
@@ -142,17 +154,21 @@ def _align_library(project, pcb, library):
|
||||
ensure_project_central_library(path, library)
|
||||
|
||||
|
||||
def recover_from_backup(request, source, *, progress=None, verifier=None, idle_check=None, version_prompt=None):
|
||||
def recover_from_backup(request, source, *, progress=None, verifier=None, idle_check=None, version_prompt=None,stop=None):
|
||||
"""Validate in copies before changing Work; preserve all recovery evidence."""
|
||||
import pythoncom, win32con, win32event
|
||||
from .network_path import absolute_preserving_drive
|
||||
from .verify_runner import run_verify, read_library_flag_if_available
|
||||
verifier = verifier or run_verify
|
||||
idle_check = idle_check or _idle_check
|
||||
source = Path(source).resolve()
|
||||
library = absolute_preserving_drive(request.lmc_file)
|
||||
library_root = library.parent.resolve()
|
||||
root = Path(request.evidence_dir).resolve() / ('backup-restore-' + uuid4().hex[:12])
|
||||
if idle_check is None:
|
||||
from .write_admission import WriteAdmission
|
||||
admission=WriteAdmission(prompt=version_prompt,stop=stop)
|
||||
idle_check=lambda library,project,pcb:_idle_check(library,project,pcb,
|
||||
confirmation=admission,evidence=root,block=request.block_name)
|
||||
# Packaged desktop apps can redirect AppData to a very deep MSIX path.
|
||||
# Keep native EE7.9 files in a genuinely short, user-owned directory.
|
||||
validation_base = Path.home() / '.cbb-restore'
|
||||
@@ -203,9 +219,9 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c
|
||||
raise ValueError('请选择目标中心库之外的独立备份工程。')
|
||||
project, pcb = preflight(replace(request, lmc_file=library))
|
||||
source_pcb = _source_check(source, request.block_name, project)
|
||||
root.mkdir(parents=True, exist_ok=False)
|
||||
idle_check(library, project, pcb)
|
||||
idle_check(library, source, source_pcb)
|
||||
root.mkdir(parents=True, exist_ok=False)
|
||||
validation_base.mkdir(parents=True, exist_ok=True)
|
||||
library_size = sum(info.st_size for info in _scan(library_root).values() if stat.S_ISREG(info.st_mode))
|
||||
source_size = sum(info.st_size for info in _scan(source.parent).values() if stat.S_ISREG(info.st_mode))
|
||||
@@ -242,6 +258,7 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c
|
||||
emit('validate_source', '正在副本中打开备份工程并执行原生校验…')
|
||||
release_confirmed = False
|
||||
choice_options = {'version_prompt': version_prompt} if version_prompt is not None else {}
|
||||
if stop is not None:choice_options['stop']=stop
|
||||
source_result = verifier(validation, progress=progress, flag_reader=read_library_flag_if_available, **choice_options)
|
||||
release_confirmed = source_result.safe_to_continue
|
||||
record['source_validation'] = asdict(source_result)
|
||||
@@ -284,6 +301,9 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c
|
||||
final = replace(final, stderr=(final.stderr + '\n原 Work 已改名保留;应用未创建完整库备份,请查看恢复记录并使用手动备份。').strip())
|
||||
return save(final)
|
||||
except Exception as error:
|
||||
from .write_admission import WriteAdmissionDeclined
|
||||
if isinstance(error,WriteAdmissionDeclined) and error.decision=='cancel' and not installed:
|
||||
return save(VerifyResult('CANCELLED',stderr=str(error),safe_to_continue=release_confirmed))
|
||||
return save(VerifyResult('FAIL' if installed else 'BLOCKED', stderr=str(error),
|
||||
safe_to_continue=release_confirmed))
|
||||
finally:
|
||||
|
||||
Reference in new issue
Block a user