"""Uploader-compatible exclusive sidecar lease. Metadata never proves ownership.""" from datetime import datetime, timezone from contextlib import contextmanager, ExitStack import json import re import os from pathlib import Path import socket import threading from uuid import uuid4 _guard = threading.RLock() _owners = {} class LibraryBusy(OSError): pass def _key(path): return str(Path(path).resolve()).casefold() def blocker_path(library): library = Path(library).resolve() return library.with_name('.' + library.stem + '.cbb-upload.blocked.json') def checkpoint_record(path): """Recognize only our reserved filename and complete versioned protocol.""" path = Path(path) if not (path.name.endswith('.cbb-upload.blocked.json') or path.name.endswith('.cbb-upload.blocked.pending')): return None try: import stat if path.lstat().st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT: return None record = json.loads(path.read_text(encoding='utf-8')) library = Path(record['library']) expected = '.' + library.stem + '.cbb-upload.blocked' if (record.get('protocol') != 'cbb-refresh-verify-library-lease' or record.get('version') != 1 or not library.is_absolute() or library.suffix.casefold() != '.lmc' or path.name not in (expected+'.json', expected+'.pending') or not re.fullmatch('[0-9a-f]{32}', str(record.get('lease_id', ''))) or not isinstance(record.get('pid'), int) or record['pid'] <= 0 or record.get('status') not in ('OPERATION_IN_PROGRESS', 'OWNER_RELEASE_UNCONFIRMED') or not isinstance(record.get('time'), str)): return None return record except (OSError, ValueError, TypeError, KeyError): return None def has_blocker(library): path = blocker_path(library) if path.with_suffix('.pending').exists(): return True if not path.exists(): return False record = checkpoint_record(path) # Complete snapshots retain every file, including their source lease # checkpoint. Only a valid relocated snapshot record can be ignored. return record is None or _key(record['library']) == _key(library) def _persist_blocker(library, status='OWNER_RELEASE_UNCONFIRMED', *, lease_id): path = blocker_path(library) pending = path.with_suffix('.pending') with pending.open('w', encoding='utf-8') as stream: json.dump({'protocol': 'cbb-refresh-verify-library-lease', 'version': 1, 'lease_id': lease_id, 'library': str(library), 'status': status, 'pid': os.getpid(), 'time': datetime.now(timezone.utc).isoformat()}, stream) stream.flush(); os.fsync(stream.fileno()) pending.replace(path) class LibraryLock: def __init__(self, library, *, safe_to_release=None): library = Path(library).resolve() self.library = library self.path = library.with_name('.' + library.stem + '.cbb-upload.lock') self.key = _key(self.path) self.entered = False self.safe_to_release = safe_to_release self.maintenance_guard = None self.maintenance_mutex = None @classmethod def for_maintenance(cls, library, archive, guard, *, safe_to_release=None): lease = cls(library, safe_to_release=safe_to_release) lease.maintenance_guard = guard lease.maintenance_archive = Path(archive) return lease def __enter__(self): if self.maintenance_guard is not None: return _enter_maintenance(self) import win32con, win32file with _guard: owner = _owners.get(self.key) if owner: if owner['thread'] != threading.get_ident() or owner['retained']: raise LibraryBusy(f'中心库被其他任务占用:{self.path}') owner['depth'] += 1 else: if has_blocker(self.library): raise LibraryBusy(f'中心库存在未解除的会话阻止记录,请先执行释放检查:{blocker_path(self.library)}') try: lease_id = uuid4().hex handle = win32file.CreateFile(str(self.path), win32con.GENERIC_READ | win32con.GENERIC_WRITE, 0, None, win32con.OPEN_ALWAYS, win32con.FILE_ATTRIBUTE_NORMAL, None) except Exception as error: raise LibraryBusy(f'中心库已锁定或无法创建协调锁:{self.path}: {error}') from error try: data = (f'machine={socket.gethostname()}; pid={os.getpid()}; ' f'startedUtc={datetime.now(timezone.utc).isoformat()}; ' f'library={self.library}').encode('utf-8') win32file.SetFilePointer(handle, 0, win32con.FILE_BEGIN) win32file.SetEndOfFile(handle) win32file.WriteFile(handle, data) win32file.FlushFileBuffers(handle) # Closing flushes the last-write timestamp on Windows. Set # it explicitly before complete-copy metadata is captured. import pywintypes win32file.SetFileTime(handle, None, None, pywintypes.Time(datetime.now())) _persist_blocker(self.library, 'OPERATION_IN_PROGRESS', lease_id=lease_id) except Exception: handle.Close() raise _owners[self.key] = {'thread': threading.get_ident(), 'depth': 1, 'handle': handle, 'retained': False, 'lease_id': lease_id} self.entered = True return self def snapshot_bytes(self): data = owned_snapshot(self.path) if data is None: raise LibraryBusy('当前线程不拥有此库协调锁') return data def retain(self): with _guard: if self.entered: _owners[self.key]['retained'] = True def __exit__(self, *_): try: self._exit_owned() finally: if self.maintenance_mutex is not None: import win32event win32event.ReleaseMutex(self.maintenance_mutex) self.maintenance_mutex.Close() self.maintenance_mutex = None def _exit_owned(self): with _guard: if not self.entered: return owner = _owners[self.key] if self.safe_to_release is not None: try: safe = self.safe_to_release() is True except Exception: safe = False if not safe: # A supervisor can time out while its native child still # owns the DB. Keep excluding the uploader until app exit. owner['retained'] = True owner['depth'] -= 1 self.entered = False if owner['depth'] == 0 and not owner['retained']: # This JSON is our durable operation checkpoint, not the # uploader's .lock file. Clear only after proven normal release. marker = blocker_path(self.library) record = checkpoint_record(marker) if marker.exists() and (record is None or record['lease_id'] != owner['lease_id']): owner['retained'] = True raise LibraryBusy('协调检查点被替换或损坏,保留冲突文件及库锁。') try: marker.unlink(missing_ok=True) except OSError: owner['retained'] = True raise owner['handle'].Close() del _owners[self.key] elif owner['retained']: marker = blocker_path(self.library) record = checkpoint_record(marker) if marker.exists() and (record is None or record['lease_id'] != owner['lease_id']): raise LibraryBusy('协调检查点被替换或损坏,保留冲突文件及库锁。') _persist_blocker(self.library, lease_id=owner['lease_id']) def _supervisor_alive(pid): import pywintypes, win32api, win32con, win32event try: handle = win32api.OpenProcess(win32con.SYNCHRONIZE, False, int(pid)) except pywintypes.error as error: if error.winerror == 87: return False raise LibraryBusy('无法确认原监督进程退出') from error try: return win32event.WaitForSingleObject(handle, 0) != win32con.WAIT_OBJECT_0 finally: handle.Close() def _enter_maintenance(lease): """Transfer only a valid known lease, after project admission under lock.""" import win32con, win32event, win32file mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795') acquired = False handle = None existing = None try: acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED) if not acquired: raise LibraryBusy('已有 CBB 操作运行,维护不能接续') with _guard: marker = blocker_path(lease.library) if marker.with_suffix('.pending').exists(): raise LibraryBusy('未完成检查点写入,不能自动接续') old = marker.read_bytes() if marker.exists() else None record = checkpoint_record(marker) if old is not None else None if old is not None and (record is None or _key(record['library']) != _key(lease.library)): raise LibraryBusy('维护拒绝未知、损坏或其他库的检查点') existing = _owners.get(lease.key) if existing: if (not existing['retained'] or existing['depth'] != 0 or record is None or record['pid'] != os.getpid() or record['lease_id'] != existing['lease_id']): raise LibraryBusy('本进程库锁仍在使用或检查点身份不符') handle = existing['handle'] else: if record is not None and _supervisor_alive(record['pid']): raise LibraryBusy('原监督进程仍存在,不能接续其库锁') handle = win32file.CreateFile(str(lease.path), win32con.GENERIC_READ | win32con.GENERIC_WRITE, 0, None, win32con.OPEN_ALWAYS, win32con.FILE_ATTRIBUTE_NORMAL, None) # All target/process/address/exclusive-file checks finish BEFORE # any old checkpoint replacement. Keep the exact sidecar open. with lease.maintenance_guard() as proof: if (marker.read_bytes() if marker.exists() else None) != old: raise LibraryBusy('维护检查期间检查点变化') if old is not None: lease.maintenance_archive.mkdir(parents=True, exist_ok=False) saved = lease.maintenance_archive/'previous-checkpoint.json' with saved.open('xb') as stream: stream.write(old); stream.flush(); os.fsync(stream.fileno()) if saved.read_bytes() != old: raise OSError('原检查点归档校验失败') lease_id = uuid4().hex _persist_blocker(lease.library, 'OPERATION_IN_PROGRESS', lease_id=lease_id) _owners[lease.key] = {'thread': threading.get_ident(), 'depth': 1, 'handle': handle, 'retained': False, 'lease_id': lease_id} lease.maintenance_proof = proof lease.entered = True lease.maintenance_mutex = mutex return lease except Exception: if handle is not None and existing is None: handle.Close() if acquired: win32event.ReleaseMutex(mutex) mutex.Close() raise def owned_snapshot(path): """Read only a lease held by this thread, never reopen an exclusive file.""" import win32con, win32file with _guard: owner = _owners.get(_key(path)) if not owner or owner['thread'] != threading.get_ident() or owner['retained']: return None handle = owner['handle'] position = win32file.SetFilePointer(handle, 0, win32con.FILE_CURRENT) try: win32file.SetFilePointer(handle, 0, win32con.FILE_BEGIN) size = win32file.GetFileSize(handle) return win32file.ReadFile(handle, size)[1] if size else b'' finally: win32file.SetFilePointer(handle, position, win32con.FILE_BEGIN) @contextmanager def _prove_library_released(library): """Hold all native database read handles during explicit lease release.""" import pythoncom, win32com.client, win32con, win32file from .scoped_manifest import scan_tree from .native_management import _retained_workers if any(worker.poll() is None for worker in _retained_workers): raise LibraryBusy('原生管理工作进程尚未返回,不能解除协调阻止。') from .com_runtime import initialize_com, uninitialize_com initialized = initialize_com() try: service = win32com.client.GetObject('winmgmts:') names = ('LibraryManager.exe', 'ExpeditionPCB.exe', 'viewdraw.exe', 'iCDBProjectBackup.exe', 'ReuseBlockImporter.exe', 'CleanWGLib.exe') query = 'SELECT ProcessId, Name FROM Win32_Process WHERE ' + ' OR '.join( f"Name='{name}'" for name in names) owners = [{'pid': int(item.ProcessId), 'name': str(item.Name)} for item in service.ExecQuery(query)] service = None if owners: raise LibraryBusy(f'原生进程仍存在,不能解除协调阻止:{owners}') entries = scan_tree(Path(library).parent) addresses = [str(path) for path in entries if path.name.casefold() == 'saddress.adr'] if addresses: raise LibraryBusy(f'库中仍有数据库地址记录,不能自动解除:{addresses}') native = [path for path in entries if path.is_file() and (path.suffix.casefold() == '.lmc' or path.name.casefold() in ('icdb.dat', 'layoutdb.lyt'))] if Path(library).resolve() not in native: raise LibraryBusy('目标库身份缺失,不能解除协调阻止。') with ExitStack() as stack: for path in native: handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) stack.callback(handle.Close) yield {'native_owners': owners, 'addresses': addresses, 'exclusive_files': [str(path) for path in native]} finally: uninitialize_com(initialized) def release_retained_library(library, evidence_dir): """Explicit user-requested release, after fresh owner/address/access proof. The upload sidecar is never deleted. This acknowledges process release, not the correctness of a partially changed library or its recovery state. """ import win32con, win32event, win32file library = Path(library).resolve(strict=True) evidence = Path(evidence_dir).resolve() if evidence.is_relative_to(library.parent) or library.parent.is_relative_to(evidence): raise ValueError('释放检查证据必须位于目标库之外。') lease = LibraryLock(library) mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795') acquired = False external = None try: acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED) if not acquired: raise LibraryBusy('另一个 CBB 操作仍在运行。') with _guard: owner = _owners.get(lease.key) if owner and (not owner['retained'] or owner['depth']): raise LibraryBusy('本应用操作仍在使用库锁。') if owner is None: try: external = win32file.CreateFile(str(lease.path), win32con.GENERIC_READ | win32con.GENERIC_WRITE, 0, None, win32con.OPEN_ALWAYS, win32con.FILE_ATTRIBUTE_NORMAL, None) except Exception as error: raise LibraryBusy('其他进程仍持有库锁,不能解除。') from error with _prove_library_released(library) as proof: marker = blocker_path(library) checkpoints = (marker, marker.with_suffix('.pending')) if any(path.exists() and checkpoint_record(path) is None for path in checkpoints): raise LibraryBusy('协调检查点格式不明或已损坏,保留原文件供人工审查。') root = evidence/('release-' + uuid4().hex) root.mkdir(parents=True, exist_ok=False) record = {'library': str(library), 'status': 'RELEASE_CONFIRMED', **proof, 'data_recovery_confirmed': False, 'evidence_path': str(root/'release.json')} (root/'release.json').write_text(json.dumps(record, ensure_ascii=False, indent=2), encoding='utf-8') for index, checkpoint in enumerate(checkpoints): if checkpoint.exists(): data = checkpoint.read_bytes() archived = root/f'blocker-before-{index}.json' archived.write_bytes(data) if archived.read_bytes() != data: raise OSError('阻止记录归档校验失败,保持库锁。') checkpoint.unlink() # Checkpoint JSON only; never .lock. if owner: owner['handle'].Close() del _owners[lease.key] return record finally: if external is not None: external.Close() if acquired: win32event.ReleaseMutex(mutex) mutex.Close()