Skip to content

squashfs.py

ofrak_components.squashfs

SquashfsFilesystem (GenericBinary, FilesystemRoot) dataclass

Filesystem stored in a squashfs format.

SquashfsPacker (Packer)

Pack files into a compressed squashfs filesystem.

pack(self, resource, config=None) async

Pack the given resource.

Users should not call this method directly; rather, they should run Resource.run or Resource.pack.

Parameters:

Name Type Description Default
resource Resource required
config

Optional config for packing. If an implementation provides a default, this default will always be used when config would otherwise be None. Note that a copy of the default config will be passed, so the default config values cannot be modified persistently by a component run.

None
Source code in ofrak_components/squashfs.py
async def pack(self, resource: Resource, config=None):
    squashfs_view: SquashfsFilesystem = await resource.view_as(SquashfsFilesystem)
    temp_flush_dir = await squashfs_view.flush_to_disk()
    with tempfile.NamedTemporaryFile(suffix=".sqsh", mode="rb") as temp:
        command = ["mksquashfs", temp_flush_dir, temp.name, "-noappend"]
        try:
            subprocess.run(command, check=True, capture_output=True)
        except CalledProcessError as error:
            raise PackerError(format_called_process_error(error))
        new_data = temp.read()
        # Passing in the original range effectively replaces the original data with the new data
        resource.queue_patch(Range(0, await resource.get_data_length()), new_data)

SquashfsUnpacker (Unpacker)

Unpack a SquashFS filesystem.

unpack(self, resource, config=None) async

Unpack the given resource.

Users should not call this method directly; rather, they should run Resource.run or Resource.unpack.

Parameters:

Name Type Description Default
resource Resource

The resource that is being unpacked

required
config

Optional config for unpacking. If an implementation provides a default, this default will always be used when config would otherwise be None. Note that a copy of the default config will be passed, so the default config values cannot be modified persistently by a component run.

None
Source code in ofrak_components/squashfs.py
async def unpack(self, resource: Resource, config=None):
    with tempfile.NamedTemporaryFile() as temp_file:
        resource_data = await resource.get_data()
        temp_file.write(resource_data)
        temp_file.flush()

        with tempfile.TemporaryDirectory() as temp_flush_dir:
            command = [
                "unsquashfs",
                "-no-exit-code",  # Don't return failure status code on warnings
                "-force",  # Overwrite files that already exist
                "-dest",
                temp_flush_dir,
                temp_file.name,
            ]
            await unpack_with_command(command)

            squashfs_view = await resource.view_as(SquashfsFilesystem)
            await squashfs_view.initialize_from_disk(temp_flush_dir)