INNER CODE UNIT · Python

copy_scoped_regular

i3T4AN/KADATH · kadath/engine.py:80

def copy_scoped_regular(root: Path, item: Path, destination: Path, maximum_bytes: int) -> tuple[int, str]:
    """Copy and hash one regular file without following links or loading it into memory."""
    root = root.absolute(); item = item.absolute(); relative = item.relative_to(root)
    cursor = root
    for part in relative.parts:
        cursor = cursor / part
        if stat.S_ISLNK(cursor.lstat().st_mode): raise ValueError(f"symbolic link is not admissible evidence: {relative}")
    flags = os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0)
    descriptor = os.open(item, flags)
    try:
        metadata = os.fstat(descriptor)
        if not stat.S_ISREG(metadata.st_mode): raise ValueError(f"non-regular evidence file: {relative}")
        if metadata.st_size > maximum_bytes: raise ValueError(f"evidence exceeds remaining {maximum_bytes}-byte quota: {relative}")
        destination.parent.mkdir(parents=True, exist_ok=True)
        hasher = hashlib.sha256(); size = 0
        with os.fdopen(descriptor, "rb", closefd=False) as source, destination.open("wb") as target:
            while True:
                data = source.read(1024 * 1024)

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…