Decode and encode tar format files in pure OCaml
README.md

tar #

Reading and writing tar archives, with the I/O left to a backend.

Warning. This is an experimental fork of
ocaml-tar, still in progress. Do not
use it in production; use ocaml-tar
instead.

Container images and root filesystems are shipped as tar archives, and the
host that unpacks one usually trusts what comes out of it. This library parses
and writes tar headers in pure OCaml. It does no I/O itself. Each archive
operation is a value that describes the I/O to perform, and a backend
runs it. The same codec thus works under Eio, on a block device or over a
string in memory.

The Eio backend has three jobs. It extracts an archive and refuses any entry
that would land outside the destination directory. It publishes a directory
tree as an archive, which in the reproducible mode is the same byte for byte
on every machine. And it appends new members to an existing archive in place.
For the reproducible mode, Tar_eio.publish visits directory entries in
byte-sorted order and writes uid 0, gid 0, modification time 0 and empty user
and group names. What is left in the archive is the paths, the permission
bits, the link targets and the file contents.

Libraries #

Library Module Purpose
nox-tar Tar Header parsing and marshalling, and the archive operation type
nox-tar-eio Tar_eio Eio backend: extract, publish, extend and fold over a filesystem
nox-tar-eio.kv Tar_kv Read-only key-value access to a tar on a block device

Installation #

Install with opam:

$ opam install nox-tar nox-tar-eio

The packages may not be in the public opam-repository yet. In that case, add
the overlay first:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install nox-tar nox-tar-eio

Usage #

nox-tar-eio runs the codec over Eio flows and paths.

Tar_eio.extract src dst unpacks the archive src into the directory dst.
An entry whose path is absolute or empty, or has a .. segment, is rejected,
and extraction never writes through a symlink in the path, so a hostile
archive cannot place a file outside dst.

Tar_eio.fold f src acc calls f on the header of each entry with an
accumulator. The callback has to read the entry's data or seek past it, so
that the fold finds the next header. The header fields (file_name,
file_size and the rest) are in Tar.Header.

Tar_eio.publish writes a directory tree as a deterministic archive and moves
it into place atomically. Tar_eio.extend appends members to an existing
archive.

See lib/eio/tar_eio.mli for the full signatures.

Design #

In Tar, an archive operation has type ('a, 'err, 'state) Tar.t. It is a
small tree of Read, Really_read, Seek, Write, Bind and Return
nodes. Return r performs no I/O and yields the result r. Bind (op, k) runs op and
hands its result to k, which returns the next operation. Reading a header
and then choosing how far to seek is therefore a value, and the backend steps
through it. Tar_eio, Tar_kv and the in-memory runner Tar.run_string are
three such backends. The codec itself depends on no I/O library, so it also
runs in a freestanding block-device context.

API #

The .mli files are the reference:

References #

License #

ISC License. See LICENSE.md for details.