fix failures

This commit is contained in:
senstella
2026-01-18 03:29:42 +09:00
parent ca85a52839
commit 6560ac93f1
6 changed files with 243 additions and 65 deletions

View File

@@ -1,11 +1,12 @@
from __future__ import annotations
import errno
import os
from collections import OrderedDict
from typing import BinaryIO
from .formats import FilePatch, PatchReader
from .proto import Control, SyncOp, SyncOpType
from .proto import TlcContainer, TlcDir, TlcFile, TlcSymlink
from .formats import PatchReader
from .proto import Control, SyncOp, SyncOpType, TlcContainer, TlcFile
from .wire import BLOCK_SIZE
@@ -17,17 +18,37 @@ _MODE_MASK = 0o7777
class FilePool:
def __init__(self, paths: list[str]):
def __init__(self, paths: list[str], max_open: int = 128):
self._paths = list(paths)
self._handles: dict[int, BinaryIO] = {}
self._handles: OrderedDict[int, BinaryIO] = OrderedDict()
self._max_open = max(1, int(max_open))
def _touch(self, index: int, handle: BinaryIO) -> None:
self._handles.pop(index, None)
self._handles[index] = handle
def _evict_if_needed(self) -> None:
while len(self._handles) >= self._max_open:
_, handle = self._handles.popitem(last=False)
handle.close()
def open(self, index: int) -> BinaryIO:
if index < 0 or index >= len(self._paths):
raise PatchApplyError(f"file index out of range: {index}")
handle = self._handles.get(index)
if handle is None:
handle = open(self._paths[index], "rb")
try:
self._evict_if_needed()
handle = open(self._paths[index], "rb")
except OSError as exc:
if exc.errno in (errno.EMFILE, errno.ENFILE):
self.close()
handle = open(self._paths[index], "rb")
else:
raise
self._handles[index] = handle
else:
self._touch(index, handle)
return handle
def size(self, index: int) -> int:
@@ -41,43 +62,86 @@ class FilePool:
self._handles.clear()
def _copy_range(dst: BinaryIO, src: BinaryIO, length: int, buffer_size: int = 32 * 1024) -> None:
def _copy_range(
dst: BinaryIO, src: BinaryIO, length: int, buffer_size: int = 32 * 1024
) -> None:
remaining = length
while remaining > 0:
chunk = src.read(min(buffer_size, remaining))
if not chunk:
raise PatchApplyError("unexpected EOF while copying block range")
break
dst.write(chunk)
remaining -= len(chunk)
def _copy_all(dst: BinaryIO, src: BinaryIO, buffer_size: int = 32 * 1024) -> None:
while True:
chunk = src.read(buffer_size)
if not chunk:
return
dst.write(chunk)
def _compute_num_blocks(file_size: int) -> int:
return (file_size + BLOCK_SIZE - 1) // BLOCK_SIZE
def _normalize_op_type(op_type: SyncOpType | int | None) -> SyncOpType | int:
return op_type if op_type is not None else SyncOpType.BLOCK_RANGE
def _is_full_file_op(op: SyncOp, target_size: int, output_size: int) -> bool:
op_type = _normalize_op_type(op.type)
if op_type != SyncOpType.BLOCK_RANGE:
return False
block_index = 0 if op.block_index is None else op.block_index
if block_index != 0:
return False
if target_size != output_size:
return False
block_span = 0 if op.block_span is None else op.block_span
return block_span == _compute_num_blocks(output_size)
def _apply_file_mode(path: str, file: TlcFile | None) -> None:
if file is None or file.mode is None:
return
mode = int(file.mode) & _MODE_MASK
try:
os.chmod(path, mode)
except (PermissionError, OSError):
pass
def apply_rsync_ops(ops: list[SyncOp], target_pool: FilePool, output: BinaryIO) -> None:
for op in ops:
if op.type == SyncOpType.DATA:
op_type = _normalize_op_type(op.type)
if op_type == SyncOpType.DATA:
output.write(op.data or b"")
continue
if op.type != SyncOpType.BLOCK_RANGE:
raise PatchApplyError(f"unsupported sync op type: {op.type}")
if op_type != SyncOpType.BLOCK_RANGE:
raise PatchApplyError(f"unsupported sync op type: {op_type}")
if op.file_index is None or op.block_index is None or op.block_span is None:
raise PatchApplyError("missing fields in block range op")
if op.block_span <= 0:
raise PatchApplyError("invalid block span in block range op")
file_index = 0 if op.file_index is None else op.file_index
block_index = 0 if op.block_index is None else op.block_index
block_span = 0 if op.block_span is None else op.block_span
file_size = target_pool.size(op.file_index)
last_block_index = op.block_index + op.block_span - 1
file_size = target_pool.size(file_index)
last_block_index = block_index + block_span - 1
last_block_size = BLOCK_SIZE
if BLOCK_SIZE * (last_block_index + 1) > file_size:
last_block_size = file_size % BLOCK_SIZE
op_size = (op.block_span - 1) * BLOCK_SIZE + last_block_size
op_size = (block_span - 1) * BLOCK_SIZE + last_block_size
src = target_pool.open(op.file_index)
src.seek(op.block_index * BLOCK_SIZE)
src = target_pool.open(file_index)
src.seek(block_index * BLOCK_SIZE)
_copy_range(output, src, op_size)
def apply_bsdiff_controls(controls: list[Control], old: BinaryIO, output: BinaryIO) -> None:
def apply_bsdiff_controls(
controls: list[Control], old: BinaryIO, output: BinaryIO
) -> None:
old_offset = 0
for ctrl in controls:
if ctrl.eof:
@@ -108,33 +172,106 @@ def _ensure_parent(path: str) -> None:
os.makedirs(parent, exist_ok=True)
def apply_patch(patch_reader: PatchReader, target_paths: list[str], output_paths: list[str]) -> None:
def apply_patch(
patch_reader: PatchReader, target_paths: list[str], output_paths: list[str]
) -> None:
pool = FilePool(target_paths)
try:
for entry in patch_reader.iter_file_entries():
if entry.sync_header.file_index is None:
raise PatchApplyError("missing file_index in sync header")
out_index = entry.sync_header.file_index
expected_files = len(output_paths)
if patch_reader.source_container is not None:
expected_files = len(patch_reader.source_container.files)
entry_iter = patch_reader.iter_file_entries()
for expected_index in range(expected_files):
try:
entry = next(entry_iter)
except StopIteration:
raise PatchApplyError(
f"corrupted patch: expected {expected_files} file entries, got {expected_index}"
)
header_index = (
0
if entry.sync_header.file_index is None
else int(entry.sync_header.file_index)
)
if header_index != expected_index:
raise PatchApplyError(
f"corrupted patch: expected file index {expected_index}, got {header_index}"
)
out_index = header_index
if out_index < 0 or out_index >= len(output_paths):
raise PatchApplyError(f"output index out of range: {out_index}")
out_path = output_paths[out_index]
_ensure_parent(out_path)
with open(out_path, "wb") as out:
if entry.is_rsync():
if entry.sync_ops is None:
raise PatchApplyError("missing rsync ops")
if entry.is_rsync():
if entry.sync_ops is None:
raise PatchApplyError("missing rsync ops")
if entry.sync_ops:
op = entry.sync_ops[0]
target_index = 0 if op.file_index is None else op.file_index
target_file = None
output_file = None
if patch_reader.target_container and patch_reader.source_container:
if 0 <= target_index < len(patch_reader.target_container.files):
target_file = patch_reader.target_container.files[
target_index
]
if 0 <= out_index < len(patch_reader.source_container.files):
output_file = patch_reader.source_container.files[out_index]
if target_file is not None and output_file is not None:
target_size = (
int(target_file.size) if target_file.size is not None else 0
)
output_size = (
int(output_file.size) if output_file.size is not None else 0
)
if _is_full_file_op(op, target_size, output_size):
src = pool.open(target_index)
src.seek(0)
with open(out_path, "wb") as out:
_copy_all(out, src)
_apply_file_mode(out_path, output_file)
continue
with open(out_path, "wb") as out:
apply_rsync_ops(entry.sync_ops, pool, out)
elif entry.is_bsdiff():
if entry.bsdiff_header is None or entry.bsdiff_controls is None:
raise PatchApplyError("missing bsdiff data")
if entry.bsdiff_header.target_index is None:
raise PatchApplyError("missing target_index in bsdiff header")
old = pool.open(entry.bsdiff_header.target_index)
output_file = None
if patch_reader.source_container and 0 <= out_index < len(
patch_reader.source_container.files
):
output_file = patch_reader.source_container.files[out_index]
_apply_file_mode(out_path, output_file)
continue
if entry.is_bsdiff():
if entry.bsdiff_header is None or entry.bsdiff_controls is None:
raise PatchApplyError("missing bsdiff data")
target_index = (
0
if entry.bsdiff_header.target_index is None
else entry.bsdiff_header.target_index
)
with open(out_path, "wb") as out:
old = pool.open(target_index)
apply_bsdiff_controls(entry.bsdiff_controls, old, out)
else:
raise PatchApplyError("unknown file patch type")
expected_size = None
output_file = None
if patch_reader.source_container and 0 <= out_index < len(
patch_reader.source_container.files
):
output_file = patch_reader.source_container.files[out_index]
if output_file.size is not None:
expected_size = int(output_file.size)
final_size = out.tell()
if expected_size is not None and final_size != expected_size:
raise PatchApplyError(
f"corrupted patch: expected output size {expected_size}, got {final_size}"
)
_apply_file_mode(out_path, output_file)
continue
raise PatchApplyError("unknown file patch type")
finally:
pool.close()