Update iso-prototype.py

This commit is contained in:
Kroese
2026-07-28 13:10:41 +02:00
committed by GitHub
parent 48cc11c393
commit 5b0f19df20
+42 -17
View File
@@ -399,40 +399,65 @@ def ensure_parent_directories(iso, logical: PurePosixPath) -> None:
iso.add_directory(**namespace_kwargs(iso, current, is_dir=True))
def remove_logical(iso, logical: PurePosixPath) -> bool:
found = find_existing_record(iso, logical)
if not found:
return False
# rm_file() removes the file and all of its linked namespace records even
# when only one valid path is supplied. Passing ISO9660, Joliet, and UDF
# paths together is fragile because their spellings need not map exactly
# to the same internal record on imported Microsoft media.
def remove_found_record(iso, found: dict[str, str]) -> None:
# rm_file() removes the inode and all linked namespace records when called
# through any one path that actually exists.
for namespace in ("udf_path", "joliet_path", "iso_path"):
candidate = found.get(namespace)
if candidate is None:
continue
iso.rm_file(**{namespace: candidate})
return True
return
return False
raise PrototypeError("No usable namespace path was found for removal")
def add_file_all_namespaces(iso, source: Path, logical: PurePosixPath) -> None:
ensure_parent_directories(iso, logical)
iso.add_file(str(source), **namespace_kwargs(iso, logical))
def remove_logical(iso, logical: PurePosixPath) -> bool:
found = find_existing_record(iso, logical)
if not found:
return False
remove_found_record(iso, found)
return True
def add_file_all_namespaces(
iso,
source: Path,
logical: PurePosixPath,
paths: dict[str, str] | None = None,
) -> None:
if paths is None:
ensure_parent_directories(iso, logical)
paths = namespace_kwargs(iso, logical)
if not paths:
raise PrototypeError(f"No namespace paths available for {logical}")
iso.add_file(str(source), **paths)
def sync_file(iso, tree: Path, logical: PurePosixPath) -> None:
source = tree / str(logical).lstrip("/")
if not source.is_file():
raise PrototypeError(f"Replacement does not exist: {source}")
removed = remove_logical(iso, logical)
if not removed:
# Preserve the exact namespaces and spellings imported from the source ISO.
# Some Microsoft media expose a file through UDF/Joliet but not through the
# ISO9660 tree. Reconstructing all three paths can therefore reference a
# parent directory that does not exist and makes PyCdlib raise
# "Could not find path".
found = find_existing_record(iso, logical)
if not found:
raise PrototypeError(
f"Cannot map existing file {logical} in ISO9660, Joliet, or UDF; refusing replacement"
)
add_file_all_namespaces(iso, source, logical)
print(" Existing namespace paths:")
for namespace, path in found.items():
print(f" {namespace}: {path}")
remove_found_record(iso, found)
add_file_all_namespaces(iso, source, logical, found)
def sync_tree(iso, tree: Path, logical_root: PurePosixPath) -> None: