Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
8ef482b
uapi16: support Linux only
brauner Nov 24, 2026
5604184
ci: let the tests use user namespaces
brauner Nov 24, 2026
3131493
uapi16: leave IDs a manifest can't have out of from_metadata()
brauner Nov 25, 2026
8ff2ff0
uapi16: let a failed write end the Writer
brauner Nov 26, 2026
a015482
uapi16: make Decoder, Step and Encoder private
brauner Nov 26, 2026
30ca12b
uapi16: add FileObject::retain_features()
brauner Nov 26, 2026
579a101
uapi16: add update_zeros() to Sha256Context and DataCheck
brauner Nov 26, 2026
f7bd320
uapi16: add PinnedDirectory::open_data()
brauner Nov 26, 2026
1c43402
uapi16: add the errors of the manifest builder
brauner Nov 23, 2026
a5d3cfa
uapi16: read one entry of a tree from a descriptor
brauner Nov 23, 2026
5efb336
uapi16: read what the optional features record of a file
brauner Nov 23, 2026
d231098
uapi16: add scratch files for the manifest builder
brauner Nov 23, 2026
4f09d4e
uapi16: keep the hard links of a tree in a table on disk
brauner Nov 23, 2026
8551978
uapi16: sort the file objects of the manifest builder
brauner Nov 23, 2026
90a4d69
uapi16: walk the trees of the manifest builder
brauner Nov 23, 2026
e36d73e
uapi16: add the manifest builder
brauner Nov 23, 2026
780b5e8
uapi16: keep the stated features of a file object in a bitmap
brauner Nov 29, 2026
ffe435f
uapi16: make the features from one table
brauner Nov 29, 2026
04d68aa
uapi16: add the C API
brauner Nov 24, 2026
665fa84
uapi16: document the manifest builder
brauner Nov 24, 2026
f2be807
TODO: add a list of work for later releases
brauner Nov 28, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ updates:
- "typenum"
ignore:
# Stay on 0.2 like pathrs, errno, getrandom and sha2's cpufeatures,
# so that the tests never build two versions of libc.
# so that no build has two versions of libc.
- dependency-name: "libc"
update-types:
- "version-update:semver-major"
Expand Down
386 changes: 368 additions & 18 deletions .github/workflows/ci.yml

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ rust-version = "1.85"
license = "LGPL-2.1-or-later"

[workspace.dependencies]
# Looks up the names of users and groups, which rustix has no call for.
# Stay on 0.2 like pathrs, errno, getrandom and sha2's cpufeatures, so
# that the tests never build two versions of libc.
# that no build has two versions of libc.
libc = "0.2.191"
# Resolves the names of the files next to a manifest below their
# directory, for PinnedDirectory. It builds on Linux only.
Expand Down
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# uapi16

This repository holds uapi16, a Rust crate that reads, checks and
writes [UAPI.16 file manifests][spec]. The crate is in src/uapi16, and
its README describes what it does.
writes [UAPI.16 file manifests][spec], and its C library, libuapi16. The
crate is in src/uapi16, and its README describes both.

## C library

From the top of the repository, these build the C library and install
it into /usr:

src/uapi16/build.sh
src/uapi16/install.sh --prefix=/usr

## Submitting changes

Expand Down
138 changes: 138 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# TODO

Work that is planned for a later release, and ideas that are not decided
yet.

## Manifest builder

- A flag that leaves out every mount point a walk doesn't cross. Without
`CROSS_MOUNTS`, `generate_at()` and `generate()` record a directory
that is a mount point as an empty directory with the attributes of the
mounted root, and a file or a device node that is a mount point as the
mount shows it. The flag leaves out all of them, directories and files
alike. It could be `AddFlags::DROP_LEAF_MOUNTS` and
`UAPI16_ADD_DROP_LEAF_MOUNTS`, or have a name such as
`ONE_FILE_SYSTEM`. What it means together with `CROSS_MOUNTS` is still
open, and so is whether it also leaves out an automount point that
isn't triggered, which a walk records as an empty directory.

- Add and finish in steps, so that a caller can stop an add between
two of them or keep each call short, as an event loop has to. The
builder works in steps inside already, so this is an addition to the
API in Rust and C: a call that sets up an add or the finish, a step
with a budget, and an end that says how the add ended. For every step
to stay short, more has to work in parts first:
- The doubling of the table of hard links. The step that begins one
writes about 0.9 MiB whatever its budget, every step that copies
writes twice its budget, and the step that ends one truncates the
old file, in 3 to 45 ms for tables of 128 MiB to 2 GiB.
- A merge. One step can read up to 32 MiB into the buffers of its
runs.
- The stages of a step. One that goes on from one stage to the next
can do two or three times its budget.
- Very small file objects of a caller. A budget in bytes lets a step
over them take ten times as long as one over files, and a weight
per record would even that out.

- Read no page of a new table of hard links while it is still a hole.
Today the copy of a doubling reads every new page from its file before
it writes it, so the copy reads three times what it copies. Knowing
which pages are still holes needs no memory that grows with the table:
the copy touches the pages of each half of the new table in order,
except for groups that wrapped around the end of the old table.

- Keep the table of hard links on disk in one file. Today a table on
disk doubles into a second file, and the two take turns, so the table
holds one more descriptor and is copied at every doubling. Two designs
could manage with a single pages file:
- The new table appended past the old one, with the old part punched
out afterwards. FAT can't punch holes, so there the space would
double.
- Linear hashing, which splits one page at a time in place, so there's
no doubling copy at all. It would mean a different table.

- Check and escape the bytes of a name a word at a time. Today the split
of a name into its components, the check of the order of names and the
escape of a record's strings, which serde_json does, go through a name
a byte at a time. Over long names the adds and the finish take up to a
third longer in one build than in another of the same source, by where
these loops land.

- Give the room of a run back on a tmpfs with huge pages. statx() says
2 MiB there, and the sort gives back only whole blocks of that size,
so a run keeps up to 2 MiB of its file until the merge is over: 1256
MiB for the 627 runs of 1.2e8 records, where xfs keeps 2 MiB. The
kernel frees smaller holes there too, so the page size would do as
the block.

- Close the window between the check of a directory for an automount
point and its open by name. A directory that becomes an automount
point in between is triggered, and `generate_at()` or `generate()`
fails with `TreeProblem::Changed`. Where the kernel has `O_EMPTYPATH`,
the handle can be reopened without a lookup, and since Linux 6.18
`openat2()` with `RESOLVE_NO_XDEV` doesn't trigger an automount.

- Signing the manifest a builder writes. systemd-manifest make feeds the
text of every record into its signer as it writes the record, and
appends the signature record at the end.

- A CI job with privileges for what the FAT job doesn't cover: block
devices and device nodes made with mknod, the append-only and
immutable flags, NOCOW, btrfs subvolumes, an mtime after 2554 on
btrfs, automount points, and a file system that keeps a file that was
unlinked while it is open, as NFS does.

- An add of a descriptor that takes the hash of the data of a regular
file from its caller and doesn't read the data. A tool that builds an
image hashes the data while it copies it, with `Sha256Context`, and
adds it with `add_object()`. But then it reads the attributes of the
file itself, extended attributes and flags included, and numbers the
hard links. The new add reads everything but the data from the
descriptor. It could be
`ManifestWriter::add_hashed(entry, name, sha256)` and
`uapi16_manifest_writer_add_hashed()`. The hash is taken as it is,
like the hash of a file object of a caller. Still open: whether the
add also takes how many bytes were hashed and what `statx()` said of
the file before they were read, so that a file that changed while it
was copied fails with `TreeProblem::Changed`, and whether the hasher
comes from the writer, so that it hashes only what the manifest
records.

## C API

- Options for the install script to leave out the static or the shared
library.

## pathrs

- Dropping pathrs remains an option. When a call of pathrs fails, pathrs
looks up the path of the descriptor of the call in /proc for its
error, and it panics where it can't: where /proc is missing or is not
a procfs, and on a real procfs where no descriptor is left to open
/proc with. Both panics are in pathrs 0.2.6, the latest release, and
gone from the main branch of libpathrs (8e60d5f "procfs: drop panic in
ProcfsBase::into_path", 0ce0614 "*: get rid of silly panics"). Until a
release has that, in both cases `PinnedDirectory` panics for a name it
can't resolve, and it and the builder panic when they reopen a file on
a kernel without `O_EMPTYPATH`. A release of pathrs that returns an
error there instead of panicking takes care of this.

- Without pathrs, `PinnedDirectory` resolves a name with `openat2()`,
`RESOLVE_IN_ROOT` and `RESOLVE_NO_MAGICLINKS`, plus
`RESOLVE_NO_SYMLINKS` unless it follows symbolic links, and tries
again on `EAGAIN`. Where the kernel has no `O_EMPTYPATH`, a file is
reopened through /proc/thread-self/fd and checked to be the inode of
its descriptor once it is open. That fails where /proc is not usable
instead of panicking, but unlike pathrs it opens what the link leads
to before it checks it. The builder's reopen, its only use of pathrs,
changes the same way.

- Still open: where seccomp refuses `openat2()`, a walk of one
component at a time with `O_NOFOLLOW` still works, but
`follow_symlinks(true)` needs a resolver of our own, or fails there.

## Maybe

- Accepting `AT_FDCWD` as the directory descriptor of `create_at()` and
`generate_at()`, in C and as rustix's `CWD` in Rust.
Today it is refused with `EBADF`, like any other negative descriptor.
41 changes: 33 additions & 8 deletions src/uapi16/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,57 @@ repository = "https://github.com/systemd/uapi16"
keywords = ["uapi", "manifest", "filesystem", "systemd", "json-seq"]
categories = ["filesystem", "parser-implementations", "encoding"]
# What the package cargo publish uploads holds next to Cargo.toml and
# Cargo.lock.
include = ["/*.rs", "/refused.jsonl", "/tests/", "/README.md", "/LICENSE"]
# Cargo.lock. The header, the configuration of cbindgen, the version script
# and the two scripts are what the C library is built and installed with.
include = [
"/*.rs",
"/refused.jsonl",
"/tests/",
"/examples/",
"/README.md",
"/LICENSE",
"/build.sh",
"/cbindgen.toml",
"/install.sh",
"/libuapi16.sym",
"/uapi16.h",
]

[lib]
path = "lib.rs"

[features]
# The C API of the crate, which uapi16.h declares. build.sh builds the
# static and the shared library of it, and install.sh installs them.
capi = []

[dependencies]
# getpwuid_r() and getgrgid_r(), which give the names of the owners of the
# files a manifest is built from. The tests take errno values from it.
libc.workspace = true
serde_core.workspace = true
# Without float_roundtrip serde_json refuses some spellings of the largest
# doubles as out of range, such as 1.7976931348623158e308.
serde_json = { workspace = true, features = ["float_roundtrip"] }
sha2.workspace = true

# PinnedDirectory, which only Linux has.
# uapi16 supports Linux only. A build for another system fails with the
# error of lib.rs, and builds neither of the two for it.
[target.'cfg(target_os = "linux")'.dependencies]
# PinnedDirectory.
pathrs.workspace = true

# PinnedDirectory, and the device numbers of FileObject::from_metadata().
[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies]
rustix = { workspace = true, features = ["fs"] }
# open_tree(), which takes the handle of a file of a tree, is in the mount
# module.
rustix = { workspace = true, features = ["fs", "mount"] }

[dev-dependencies]
# The errno values of the errors the tests make.
libc.workspace = true
# The tests look at the order of the keys the writer writes.
serde_json = { workspace = true, features = ["preserve_order"] }

[target.'cfg(target_os = "linux")'.dev-dependencies]
# The tests of the Writer write to a pipe.
rustix = { workspace = true, features = ["pipe"] }

[lints]
workspace = true
93 changes: 83 additions & 10 deletions src/uapi16/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ uapi16 can:
- read a manifest and check every object against the rules of the
specification,
- write a manifest and check it the same way,
- do both without doing any I/O itself, for example in an async runtime,
- copy the data of the files a manifest describes and check its size and
hash.
hash,
- build a manifest from the file objects a tool makes, in any order, for
example while it writes a file system image,
- build the manifest of a tree of files on the host, which it reads
through descriptors.

A manifest is read one record at a time. Memory use does not grow with
the size of the manifest.
the size of the manifest. Building a large manifest takes room on disk
instead of memory.

Two checks are up to the caller: whether a manifest declares the order
of its file objects (RootObject::require_order) and whether an object
Expand All @@ -31,18 +35,87 @@ uapi16 reads JSON with serde_json. If another crate in the same build
turns on serde_json's arbitrary_precision feature, numbers are read
differently, see the crate documentation.

uapi16 supports Linux 5.10 or newer, the minimum baseline of systemd,
and Android. Reading the data of the files from the directory next to
the manifest works on Linux only. uapi16 needs Rust 1.85 or newer. Some
of its dependencies need a newer Rust in later releases, such as sha2
from version 0.11.1 on. With an older Rust, use resolver 3, the default
of edition 2024, so that cargo picks versions that build, or pin such a
dependency, for example with `cargo update -p sha2 --precise 0.11.0`.
uapi16 supports Linux 5.10 or newer, the minimum baseline of systemd. It
does not build for any other system, Android included. uapi16 needs Rust
1.85 or newer. Some of its dependencies need a newer Rust in later
releases, such as sha2 from version 0.11.1 on. With an older Rust, use
resolver 3, the default of edition 2024, so that cargo picks versions
that build, or pin such a dependency, for example with
`cargo update -p sha2 --precise 0.11.0`.

A new minor release, such as 0.2.0, may change the API in ways that
break callers. Releases within a minor version, such as 0.1.1 after
0.1.0, stay compatible.

## Building a manifest

ManifestOptions says what a manifest records, and makes the
ManifestWriter that takes the file objects it lists. generate_at() writes
the manifest of a directory and everything below it to a new file:

```rust
use std::fs::File;
use std::path::Path;
use uapi16::{AddFlags, Features, ManifestOptions};

let esp = File::open("/efi")?;
let out_dir = File::open("/var/lib/manifests")?;
let report = ManifestOptions::new()
.features(Features::DEFAULT | Features::MTIME)
.generate_at(&out_dir, Path::new("esp.manifest"), &esp, AddFlags::NONE)?;
println!("{} files, {} bytes", report.files(), report.bytes());
```

The new file appears once the manifest is complete.
examples/mkmanifest.rs is a program that does this for the directory
and the file it is given:

cargo run --example mkmanifest -- /efi esp.manifest

A tool that builds an image writes the manifest while it does: it gives
the writer the root with add_root() and a file object for every file
with add_object(), in any order, and finish() writes the manifest.

## C library

With the feature capi, the crate is also a C library, libuapi16. It
builds manifests, reads and writes them record by record, and checks the
data of the files they list. uapi16.h declares its functions and says
what each of them does. What holds for all of them is at its top.

examples/ has six programs that use it:

- generate.c writes the manifest of a directory.
- listing.c writes the manifest of a store from the records a tool has
of its files.
- build.c copies a tree into an image and writes the manifest of the
image while it does.
- assemble.c builds an image from a manifest and the files next to it.
- sorted.c writes a manifest record by record, in the order of the
manifest.
- signed.c writes a manifest with a signature object and reads it back.

tests/capi.sh builds and runs them.

build.sh builds the static library with cargo and links the shared one
from it with the C compiler. install.sh installs both with uapi16.h and
uapi16.pc:

./build.sh
./install.sh --prefix=/usr

build.sh works from any directory of the tree. Its default --builddir
is in the target directory of cargo. install.sh looks in target/release
below the current directory, or in the release directory of
CARGO_TARGET_DIR where that is set. So run it from the top of the crate,
or pass --builddir. In the repository the scripts are
src/uapi16/build.sh and src/uapi16/install.sh, and the top is the top of
the repository. Both list their options with --help.

A program that uses the library gets the flags for it from pkg-config:

cc -o program program.c $(pkg-config --cflags --libs uapi16)

## License

LGPL-2.1-or-later, see LICENSE.
Expand Down
Loading