152 lines
8.1 KiB
Python
152 lines
8.1 KiB
Python
"""Bound read-only filesystem probes; keep proof handles alive through commit."""
|
|
from contextlib import contextmanager, ExitStack
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
import stat
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import time
|
|
from uuid import uuid4
|
|
|
|
|
|
@contextmanager
|
|
def hold_release_files(library, roots=None, *, timeout_seconds=10, stop=None):
|
|
"""Only this pure probe may be terminated. stdin EOF releases its handles."""
|
|
with tempfile.TemporaryDirectory(prefix='cbb-file-probe-') as folder:
|
|
job = Path(folder)/'request.json'
|
|
token = uuid4().hex
|
|
job.write_text(json.dumps({'token': token, 'library': str(library),
|
|
'roots': [str(p) for p in roots] if roots is not None else None}), 'utf-8')
|
|
command = ([sys.executable] if getattr(sys, 'frozen', False) else
|
|
[sys.executable, '-m', 'cbb_refresh_verify']) + ['--file-probe', str(job)]
|
|
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL,
|
|
stderr=subprocess.DEVNULL, creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0))
|
|
try:
|
|
deadline = time.monotonic()+max(.1, min(10, timeout_seconds))
|
|
result_path = job.with_name('result.json')
|
|
while not result_path.exists():
|
|
if stop is not None and stop.is_set():
|
|
raise InterruptedError('FILE_PROBE_CANCELLED')
|
|
if time.monotonic() >= deadline:
|
|
raise TimeoutError('FILE_PROBE_TIMEOUT: 文件占用检查超时,未证明释放')
|
|
if process.poll() is not None:
|
|
raise OSError('FILE_PROBE_UNCONFIRMED: 文件探测未返回证据')
|
|
time.sleep(.02)
|
|
if stop is not None and stop.is_set():
|
|
raise InterruptedError('FILE_PROBE_CANCELLED')
|
|
record = json.loads(result_path.read_text('utf-8'))
|
|
if record.get('token') != token or record.get('status') != 'HELD' or process.poll() is not None:
|
|
error = OSError('FILE_PROBE_UNCONFIRMED: '+str(record.get('error', '探测身份或句柄失效')))
|
|
error.winerror = record.get('winerror')
|
|
raise error
|
|
# Duplicate the already-open handles before acknowledging the probe.
|
|
# Its exit can never create a gap in the parent's commit protection.
|
|
import win32api, win32con
|
|
with ExitStack() as protection:
|
|
child = win32api.OpenProcess(win32con.PROCESS_DUP_HANDLE, False, process.pid)
|
|
protection.callback(child.Close)
|
|
if len(record['handles']) != len(record['exclusive_files'])+len(record['binding_files']):
|
|
raise OSError('FILE_PROBE_UNCONFIRMED: 证明句柄数量不符')
|
|
for handle in record['handles']:
|
|
owned = win32api.DuplicateHandle(child, handle, win32api.GetCurrentProcess(),
|
|
0, False, win32con.DUPLICATE_SAME_ACCESS)
|
|
protection.callback(owned.Close)
|
|
yield {k: v for k, v in record.items() if k != 'handles'}
|
|
finally:
|
|
process.stdin.close()
|
|
try:
|
|
process.wait(.5)
|
|
except subprocess.TimeoutExpired:
|
|
process.terminate() # This executable only scans and opens read handles.
|
|
process.wait(3)
|
|
|
|
|
|
def _snapshot(library, roots):
|
|
from .scoped_manifest import scan_tree
|
|
entries = {library: library.lstat()}
|
|
for root in roots:
|
|
if root.exists():
|
|
entries.update(scan_tree(root))
|
|
native = sorted(path for path, info in entries.items() if stat.S_ISREG(info.st_mode)
|
|
and (path == library or path.name.casefold() in ('icdb.dat', 'layoutdb.lyt')))
|
|
if library not in native:
|
|
raise ValueError('目标库身份缺失')
|
|
addresses = sorted(path for path in entries if path.name.casefold() == 'saddress.adr')
|
|
targets, signatures = [], {}
|
|
for address in addresses:
|
|
data = address.read_bytes()
|
|
signatures[str(address)] = hashlib.sha256(data).hexdigest()
|
|
database = address.parent.parent
|
|
matches = []
|
|
for project in database.parent.glob('*.prj'):
|
|
if project not in entries or project.stat().st_size > 1024*1024:
|
|
continue
|
|
from .backup_recovery import _project_text
|
|
text = _project_text(project)
|
|
references = re.findall(r'^KEY iCDBDir "([^"\r\n]+)"', text, re.M)
|
|
if len(references) == 1 and (project.parent/references[0]).resolve() == database:
|
|
matches.append(project)
|
|
if len(matches) != 1 or database/'icdb.dat' not in native:
|
|
raise ValueError(f'地址未绑定唯一工程数据库,需人工核对:{address}')
|
|
targets.append({'project': str(matches[0]), 'database': str(database)})
|
|
signatures[str(matches[0])] = hashlib.sha256(matches[0].read_bytes()).hexdigest()
|
|
return {'exclusive_files': [str(p) for p in native], 'addresses': [str(p) for p in addresses],
|
|
'inspection_targets': targets, 'signatures': signatures, 'binding_files': sorted(signatures),
|
|
'resource_scope': {'library': str(library), 'roots': [str(p) for p in roots],
|
|
'projects': sorted(str(p) for p in entries if p.suffix.casefold() == '.prj'),
|
|
'shared_dependencies': sorted(str(p.parent) for p in native
|
|
if p.is_relative_to(library.parent/'SymbolLibs') and p.name.casefold() == 'icdb.dat'),
|
|
'databases': sorted({str(p.parent) for p in native if p.name.casefold() == 'icdb.dat'})}}
|
|
|
|
|
|
def file_probe_main(job):
|
|
"""No native launch, writes, repair, address deletion or process termination."""
|
|
import win32con, win32file
|
|
job = Path(job)
|
|
request = json.loads(job.read_text('utf-8'))
|
|
result = {'token': request['token'], 'status': 'UNCONFIRMED'}
|
|
try:
|
|
def checked_path(value):
|
|
path = Path(value).absolute()
|
|
for part in (path, *path.parents):
|
|
try:
|
|
info = part.lstat()
|
|
except FileNotFoundError:
|
|
continue
|
|
if info.st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT:
|
|
raise ValueError(f'资源范围含重解析点:{part}')
|
|
return path.resolve()
|
|
library = checked_path(request['library'])
|
|
roots = [checked_path(p) for p in request['roots']] if request['roots'] is not None else [library.parent]
|
|
if library.suffix.casefold() != '.lmc' or any(not p.is_relative_to(library.parent) for p in roots):
|
|
raise ValueError('文件探测范围不属于所选库')
|
|
snapshot = _snapshot(library, roots)
|
|
with ExitStack() as stack:
|
|
handles = []
|
|
for path in snapshot['exclusive_files']:
|
|
handle = win32file.CreateFile(path, win32con.GENERIC_READ, 0, None,
|
|
win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None)
|
|
stack.callback(handle.Close)
|
|
handles.append(int(handle))
|
|
for path in snapshot['binding_files']:
|
|
handle = win32file.CreateFile(path, win32con.GENERIC_READ, win32con.FILE_SHARE_READ,
|
|
None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None)
|
|
stack.callback(handle.Close)
|
|
handles.append(int(handle))
|
|
if _snapshot(library, roots) != snapshot:
|
|
raise ValueError('占用检查期间资源集合变化')
|
|
pending = job.with_name('result.pending')
|
|
pending.write_text(json.dumps({**result, **snapshot, 'status': 'HELD', 'handles': handles}), 'utf-8')
|
|
pending.replace(job.with_name('result.json'))
|
|
sys.stdin.buffer.read(1) # Parent keeps stdin open until its commit finishes.
|
|
return 0
|
|
except Exception as error:
|
|
pending = job.with_name('result.pending')
|
|
pending.write_text(json.dumps({**result, 'error': str(error),
|
|
'winerror': getattr(error, 'winerror', None)}), 'utf-8')
|
|
pending.replace(job.with_name('result.json'))
|
|
return 1
|