fix: inspect native ICDB occupancy before bounded repair

This commit is contained in:
阿白叔 committed 2026-11-04 12:26:09 +08:00
1 parent cb1326688b
commit e14a864b37
17 files changed
+617 -153

No files matched your search

+59
View File
@@ -0,0 +1,59 @@
"""Bound WMI enumeration in a read-only helper, never in the Qt thread."""
from pathlib import Path
import json
import re
import subprocess
import sys
import tempfile
def query_owners(names, *, timeout_seconds=10):
names = tuple(names)
if not names or any(not re.fullmatch(r'[\w.-]+\.exe', name, re.I) for name in names):
raise ValueError('OWNER_QUERY_INVALID')
with tempfile.TemporaryDirectory(prefix='cbb-owner-probe-') as directory:
job = Path(directory)/'request.json'
job.write_text(json.dumps({'names': names}), encoding='utf-8')
command = ([sys.executable] if getattr(sys, 'frozen', False) else
[sys.executable, '-m', 'cbb_refresh_verify']) + ['--owner-probe', str(job)]
try:
result = subprocess.run(command, timeout=max(.1, min(10, timeout_seconds)),
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0))
except subprocess.TimeoutExpired as error:
# This helper only enumerates processes; it never launches Mentor
# or acquires a native database session.
raise RuntimeError('OWNER_QUERY_TIMEOUT: 进程查询超时,占用状态无法确认') from error
try:
record = json.loads(job.with_name('result.json').read_text('utf-8'))
rows = record['owners']
if result.returncode != 0 or record['names'] != list(names) or not isinstance(rows, list):
raise ValueError('invalid result')
for row in rows:
if (type(row['pid']) is not int or row['pid'] <= 0 or row['name'].casefold() not in {n.casefold() for n in names}
or not isinstance(row['command'], str)):
raise ValueError('invalid owner')
return rows
except (OSError, ValueError, KeyError, TypeError, AttributeError) as error:
raise RuntimeError('OWNER_QUERY_UNCONFIRMED: 进程查询未返回有效证据') from error
def owner_probe_main(job):
import pythoncom
import win32com.client
job = Path(job)
names = json.loads(job.read_text('utf-8'))['names']
if not names or any(not re.fullmatch(r'[\w.-]+\.exe', name, re.I) for name in names):
return 1
pythoncom.CoInitialize()
try:
service = win32com.client.GetObject('winmgmts:')
query = 'SELECT ProcessId,Name,CommandLine FROM Win32_Process WHERE ' + ' OR '.join(
f"Name='{name}'" for name in names)
rows = [{'pid': int(item.ProcessId), 'name': str(item.Name),
'command': str(item.CommandLine or '')} for item in service.ExecQuery(query)]
service = None
job.with_name('result.json').write_text(json.dumps({'names': names, 'owners': rows}), encoding='utf-8')
return 0
finally:
pythoncom.CoUninitialize()