pycdlib.inplaceeditor

In-place editing of files on an existing ISO.

InPlaceEditor changes an ISO file on disk directly, rather than mastering a new ISO the way the pycdlib.pycdlib.PyCdlib methods do. The two styles of editing cannot safely be mixed on one object, so in-place editing lives on its own class with its own small set of operations.

class InPlaceEditor(filename: str, mode: str = 'rb+')

Context manager for editing files in place on an existing ISO.

Open the ISO, modify one or more files in place, and close. The new content is written to the original ISO file – there is no separate output file the way write_fp produces.

modify_file(
fp: BinaryIO,
length: int,
iso_path: str | None = None,
rr_path: str | None = None,
joliet_path: str | None = None,
udf_path: str | None = None,
) → None

Replace the bytes of an existing file on the ISO with new content.

Constraints:
  • The path must identify an existing file, not a directory, and exactly one of iso_path, rr_path, joliet_path, or udf_path must be given to identify it. The file is looked up through that one view; every other view of the same file (ISO9660, Rock Ridge, Joliet, UDF) is updated as well, since they all share the underlying data.

  • The new content cannot occupy more extents than the old content. Within that ceiling the new length is free: a file currently sized to N extents may be replaced with any content from 0 bytes up through N*logical_block_size bytes. Shrinking past an extent boundary zeroes the orphaned extents inside the file’s original on-disk allocation.

Parameters:
  • fp – A file-like object containing the new contents.

  • length – The length of the new contents.

  • iso_path – The ISO9660 absolute path identifying the file.

  • rr_path – The Rock Ridge absolute path identifying the file.

  • joliet_path – The Joliet absolute path identifying the file.

  • udf_path – The UDF absolute path identifying the file.

Returns:

Nothing.

rm_file(
iso_path: str | None = None,
rr_path: str | None = None,
joliet_path: str | None = None,
udf_path: str | None = None,
) → None

Remove a file from the ISO in place.

The file’s directory record is removed from every tree the file is linked into (ISO9660 and Joliet) and the affected parent extents are rewritten on disk. The file’s data extent stays as orphaned bytes inside the volume – the volume’s on-disk size doesn’t shrink.

Constraints:
  • The path must identify an existing file, not a directory, and exactly one of iso_path, rr_path, joliet_path, or udf_path must be given to identify it. The file is looked up through that one view; every other view of the same file is removed as well.

  • The file must not be the El Torito boot catalog or a boot image.

  • The ISO must not be a UDF ISO (UDF in-place rm is not supported yet).

Parameters:
  • iso_path – The ISO9660 absolute path identifying the file.

  • rr_path – The Rock Ridge absolute path identifying the file.

  • joliet_path – The Joliet absolute path identifying the file.

  • udf_path – The UDF absolute path identifying the file.

Returns:

Nothing.

add_fp(
fp: BinaryIO,
length: int,
iso_path: str,
rr_name: str | None = None,
joliet_path: str | None = None,
file_mode: int | None = None,
) → None

Add a new file to the ISO in place, reading the content from a file-like object.

The new file’s data extent is appended to the end of the ISO (the underlying file on disk grows by the rounded-up content length). The directory record is inserted into the parent’s existing extent on disk.

Constraints:
  • The ISO must not be a UDF or El Torito ISO.

  • rr_name must be provided on a Rock Ridge ISO and omitted otherwise. It can be at most about 130 characters; longer names cannot be added in place.

  • The target’s parent directory must already exist on the ISO.

  • The parent’s directory extent must have enough slack to hold one more record; if not, this raises and you must use pycdlib.pycdlib.PyCdlib.add_fp() + write_fp instead.

  • The caller is responsible for keeping fp open for the lifetime of this editor (until the with-block exits).

Parameters:
  • fp – A file-like object containing the new file’s content.

  • length – The length of the new content.

  • iso_path – The ISO9660 absolute path identifying where to insert the file.

  • rr_name – Rock Ridge name; required on Rock Ridge ISOs and rejected on non-Rock-Ridge ISOs.

  • joliet_path – The Joliet absolute path; if provided, a Joliet directory record is also inserted.

  • file_mode – File-mode bits (Rock Ridge only).

Returns:

Nothing.

add_file(
filename: str,
iso_path: str,
rr_name: str | None = None,
joliet_path: str | None = None,
file_mode: int | None = None,
) → None

Add a new file to the ISO in place, reading the content from a file on the local filesystem. PyCdlib is responsible for opening and closing filename.

The constraints listed for add_fp() apply here as well.

Parameters:
  • filename – The local filename whose content becomes the new file’s content on the ISO.

  • iso_path – The ISO9660 absolute path identifying where to insert the file.

  • rr_name – Rock Ridge name; required on Rock Ridge ISOs and rejected on non-Rock-Ridge ISOs.

  • joliet_path – The Joliet absolute path; if provided, a Joliet directory record is also inserted.

  • file_mode – File-mode bits (Rock Ridge only).

Returns:

Nothing.