Tar archives as streams (Effect's encoding/Tar).
decode streams entry bodies from the archive input and encode streams them
back into archive bytes; neither holds a whole body, which is what these channels
add to the blocking tarfile. The decoder reads v7, USTAR, PAX extended headers,
and GNU long names and base-256 numbers. The encoder writes USTAR, adding PAX
extended headers for fields that do not fit or for custom metadata. Neither touches
the file system or compresses.
Paths and link targets are returned as written in the archive, so they can be
absolute or contain .., and a symlink entry can redirect a later entry outside
the target directory. Validate both before writing entries to disk.
Types#
| Type | Definition | Description |
|---|---|---|
EntryType | type EntryType = Literal['File', 'Directory', 'Symlink', 'Hardlink', 'CharDevice', 'BlockDevice', 'Fifo', 'Other'] |
|
TarErrorKind | type TarErrorKind = Literal['InvalidHeader', 'Truncated', 'SizeMismatch', 'Unsupported', 'BodyConsumed'] |
Classes#
Functions#
is_tar_error#
is_tar_error(value) -> TypeIs[TarError]| Parameter | Type | Description |
|---|---|---|
value | object |
Returns TypeIs[TarError]
def is_tar_error(value: object) -> TypeIs[TarError]:
return isinstance(value, TarError)file#
file(path, content, *, mode=None, uid=None, gid=None, uname=None, gname=None, mtime=None, pax=None) -> EntryA regular file from in-memory content; a str is encoded as UTF-8. Unset
fields default to mode 0o644, uid and gid 0, empty names, an mtime at the
epoch and no extra PAX records, which makes archives reproducible.
| Parameter | Type | Description |
|---|---|---|
path | str | |
content | str | bytes | |
mode | int | None = None | |
uid | int | None = None | |
gid | int | None = None | |
uname | str | None = None | |
gname | str | None = None | |
mtime | datetime | FarDate | None = None | |
pax | Mapping[str, str] | None = None |
Returns Entry
def file(
path: str,
content: str | bytes,
*,
mode: int | None = None,
uid: int | None = None,
gid: int | None = None,
uname: str | None = None,
gname: str | None = None,
mtime: datetime | FarDate | None = None,
pax: Mapping[str, str] | None = None,
) -> Entry:
"""A regular file from in-memory content; a ``str`` is encoded as UTF-8. Unset
fields default to mode ``0o644``, uid and gid 0, empty names, an mtime at the
epoch and no extra PAX records, which makes archives reproducible."""
data = text_encode(content) if isinstance(content, str) else content
header = _header(
path,
"File",
len(data),
None,
mode=mode,
uid=uid,
gid=gid,
uname=uname,
gname=gname,
mtime=mtime,
pax=pax,
)
return Entry(header, _Content(data) if data else empty)file_stream#
file_stream[E = Never, R = Never](path, size, body, *, mode=None, uid=None, gid=None, uname=None, gname=None, mtime=None, pax=None) -> Entry[E, R]A regular file streamed from body, which runs when the entry is encoded.
size is its exact byte length: encode fails with "SizeMismatch"
otherwise.
| Parameter | Type | Description |
|---|---|---|
path | str | |
size | int | |
body | Stream[bytes, E, R] | |
mode | int | None = None | |
uid | int | None = None | |
gid | int | None = None | |
uname | str | None = None | |
gname | str | None = None | |
mtime | datetime | FarDate | None = None | |
pax | Mapping[str, str] | None = None |
Returns Entry[E, R]
def file_stream[E = Never, R = Never](
path: str,
size: int,
body: Stream[bytes, E, R],
*,
mode: int | None = None,
uid: int | None = None,
gid: int | None = None,
uname: str | None = None,
gname: str | None = None,
mtime: datetime | FarDate | None = None,
pax: Mapping[str, str] | None = None,
) -> Entry[E, R]:
"""A regular file streamed from ``body``, which runs when the entry is encoded.
``size`` is its exact byte length: ``encode`` fails with ``"SizeMismatch"``
otherwise."""
require_stream(body, "body")
header = _header(
path,
"File",
size,
None,
mode=mode,
uid=uid,
gid=gid,
uname=uname,
gname=gname,
mtime=mtime,
pax=pax,
)
return Entry(header, body)directory#
directory(path, *, mode=None, uid=None, gid=None, uname=None, gname=None, mtime=None, pax=None) -> EntryA directory (mode 0o755 by default). A trailing / is added to a
non-empty path; an empty one stays empty, so that encode rejects it.
| Parameter | Type | Description |
|---|---|---|
path | str | |
mode | int | None = None | |
uid | int | None = None | |
gid | int | None = None | |
uname | str | None = None | |
gname | str | None = None | |
mtime | datetime | FarDate | None = None | |
pax | Mapping[str, str] | None = None |
Returns Entry
def directory(
path: str,
*,
mode: int | None = None,
uid: int | None = None,
gid: int | None = None,
uname: str | None = None,
gname: str | None = None,
mtime: datetime | FarDate | None = None,
pax: Mapping[str, str] | None = None,
) -> Entry:
"""A directory (mode ``0o755`` by default). A trailing ``/`` is added to a
non-empty ``path``; an empty one stays empty, so that ``encode`` rejects it."""
named = path if path == "" or path.endswith("/") else f"{path}/"
header = _header(
named,
"Directory",
0,
None,
mode=mode,
uid=uid,
gid=gid,
uname=uname,
gname=gname,
mtime=mtime,
pax=pax,
)
return Entry(header, empty)symlink#
symlink(path, target, *, mode=None, uid=None, gid=None, uname=None, gname=None, mtime=None, pax=None) -> EntryA symbolic link to target (mode 0o777 by default).
| Parameter | Type | Description |
|---|---|---|
path | str | |
target | str | |
mode | int | None = None | |
uid | int | None = None | |
gid | int | None = None | |
uname | str | None = None | |
gname | str | None = None | |
mtime | datetime | FarDate | None = None | |
pax | Mapping[str, str] | None = None |
Returns Entry
def symlink(
path: str,
target: str,
*,
mode: int | None = None,
uid: int | None = None,
gid: int | None = None,
uname: str | None = None,
gname: str | None = None,
mtime: datetime | FarDate | None = None,
pax: Mapping[str, str] | None = None,
) -> Entry:
"""A symbolic link to ``target`` (mode ``0o777`` by default)."""
header = _header(
path,
"Symlink",
0,
target,
mode=mode,
uid=uid,
gid=gid,
uname=uname,
gname=gname,
mtime=mtime,
pax=pax,
)
return Entry(header, empty)hardlink#
hardlink(path, target, *, mode=None, uid=None, gid=None, uname=None, gname=None, mtime=None, pax=None) -> EntryA hard link to target, the archive path of an entry written earlier.
| Parameter | Type | Description |
|---|---|---|
path | str | |
target | str | |
mode | int | None = None | |
uid | int | None = None | |
gid | int | None = None | |
uname | str | None = None | |
gname | str | None = None | |
mtime | datetime | FarDate | None = None | |
pax | Mapping[str, str] | None = None |
Returns Entry
def hardlink(
path: str,
target: str,
*,
mode: int | None = None,
uid: int | None = None,
gid: int | None = None,
uname: str | None = None,
gname: str | None = None,
mtime: datetime | FarDate | None = None,
pax: Mapping[str, str] | None = None,
) -> Entry:
"""A hard link to ``target``, the archive path of an entry written earlier."""
header = _header(
path,
"Hardlink",
0,
target,
mode=mode,
uid=uid,
gid=gid,
uname=uname,
gname=gname,
mtime=mtime,
pax=pax,
)
return Entry(header, empty)decode#
decode[IE = Never, D = None]() -> Channel[tuple[Entry[TarError | IE], ...], TarError | IE, D, tuple[bytes, ...], IE, D]A channel decoding archive bytes into entries, one entry per output chunk,
each body streaming header.size bytes straight from the input. Pulling the
next entry skips what is left of the current body. A PAX global header's records
apply to every entry after it, except path, linkpath and size.
Read each body before pulling the next entry and while the entry stream runs,
for example in map_effect: a later read fails with "BodyConsumed", so
operators that pull ahead cannot sit between decode and the reads. A body
reads its bytes once: running it again continues where it stopped.
Directories, links, devices and FIFOs have no body and a size of 0. Names
that are not UTF-8 get replacement characters. PAX headers and GNU long names
past 1 MiB fail with "InvalidHeader", and GNU sparse entries with
"Unsupported". The channel ends only when its input ends: input after the
end-of-archive marker is read and discarded, so that the upstream's done value
can be passed on.
An mtime datetime cannot hold (outside years 1 to 9999) fails with
"InvalidHeader".
Returns Channel[tuple[Entry[TarError | IE], ...], TarError | IE, D, tuple[bytes, ...], IE, D]
def decode[IE = Never, D = None]() -> Channel[
tuple[Entry[TarError | IE], ...], TarError | IE, D, tuple[bytes, ...], IE, D
]:
"""A channel decoding archive bytes into entries, one entry per output chunk,
each body streaming ``header.size`` bytes straight from the input. Pulling the
next entry skips what is left of the current body. A PAX global header's records
apply to every entry after it, except ``path``, ``linkpath`` and ``size``.
Read each body before pulling the next entry and while the entry stream runs,
for example in ``map_effect``: a later read fails with ``"BodyConsumed"``, so
operators that pull ahead cannot sit between ``decode`` and the reads. A body
reads its bytes once: running it again continues where it stopped.
Directories, links, devices and FIFOs have no body and a ``size`` of 0. Names
that are not UTF-8 get replacement characters. PAX headers and GNU long names
past 1 MiB fail with ``"InvalidHeader"``, and GNU sparse entries with
``"Unsupported"``. The channel ends only when its input ends: input after the
end-of-archive marker is read and discarded, so that the upstream's done value
can be passed on.
An mtime ``datetime`` cannot hold (outside years 1 to 9999) fails with
``"InvalidHeader"``."""
return decode_channel(Decoder, Entry, TarError)encode#
encode[E = Never, R = Never, IE = Never, D = None]() -> Channel[tuple[bytes, ...], TarError | IE | E, D, tuple[Entry[E, R], ...], IE, D, R]A channel encoding entries into archive bytes: a USTAR header per entry, a
PAX header only for what USTAR cannot hold (a long or non-ASCII path, link target
or name, a size of 8 GiB or more, a uid or gid above 2097151, an mtime outside
USTAR's range, or custom pax records), and two zero blocks at the end.
Output is written in chunks of about 64 KiB, and large body chunks pass through uncopied, so bodies stream without being held in memory.
A body with any byte count but header.size fails with "SizeMismatch".
header.pax holds custom metadata: the keys of header fields and
GNU.sparse.* fail with "InvalidHeader", as does a File path ending in
/, a non-zero size on an entry other than File and Other, or a device
number above 2097151. mtime is rounded down to whole seconds, and a naive
datetime fails with "InvalidHeader". typeflag is only written for
Other entries.
Entries that differ in their bodies' errors or services do not infer one E and
R: annotate the stream, as Stream[Entry[E1 | E2, R1 | R2]].
Returns Channel[tuple[bytes, ...], TarError | IE | E, D, tuple[Entry[E, R], ...], IE, D, R]
def encode[E = Never, R = Never, IE = Never, D = None]() -> Channel[
tuple[bytes, ...], TarError | IE | E, D, tuple[Entry[E, R], ...], IE, D, R
]:
"""A channel encoding entries into archive bytes: a USTAR header per entry, a
PAX header only for what USTAR cannot hold (a long or non-ASCII path, link target
or name, a size of 8 GiB or more, a uid or gid above ``2097151``, an mtime outside
USTAR's range, or custom ``pax`` records), and two zero blocks at the end.
Output is written in chunks of about 64 KiB, and large body chunks pass through
uncopied, so bodies stream without being held in memory.
A body with any byte count but ``header.size`` fails with ``"SizeMismatch"``.
``header.pax`` holds custom metadata: the keys of header fields and
``GNU.sparse.*`` fail with ``"InvalidHeader"``, as does a ``File`` path ending in
``/``, a non-zero size on an entry other than ``File`` and ``Other``, or a device
number above ``2097151``. ``mtime`` is rounded down to whole seconds, and a naive
``datetime`` fails with ``"InvalidHeader"``. ``typeflag`` is only written for
``Other`` entries.
Entries that differ in their bodies' errors or services do not infer one ``E`` and
``R``: annotate the stream, as ``Stream[Entry[E1 | E2, R1 | R2]]``."""
def transform(upstream: AnyPull, scope: CloseableScope) -> AnyPull:
return _Succeed(_Suspend(_EncodePull(upstream, scope).step))
channel: Any = from_transform(
lambda upstream, scope: _Suspend(lambda: transform(upstream, scope))
)
return channel