The package manager¶
The package manager has three parts:
| Part | Where | What it does |
|---|---|---|
| librat | src/lib/rat/ |
reads and checks packages, keeps the database, installs and removes, gets and checks the index, downloads |
rat |
src/user/rat/ |
the command: options, messages, the root check, the progress line |
| the tools | tools/rat/ |
on the host: mkpkg.py makes packages, mkindex.py the index, sign.sh its signature, publish.sh the GitHub release, newkey.sh the signing key; base_files.py writes the database of a new image |
librat uses zlib, mbedTLS and libcurl from their ports. Without them
(make PORTS=) librat and rat are not built.
| File of librat | What it does |
|---|---|
archive.c |
reads and checks a package file |
info.c |
the fields of .RATINFO and of the index, names and versions |
version.c |
the order of versions |
db.c, query.c |
the database and the queries |
install.c, remove.c, txn.c |
the changes and their transactions |
index.c |
reads and checks index.conf |
signature.c |
checks index.conf.sig with the keys in /etc/rat/keys |
download.c |
downloads with libcurl |
repo.c |
update, install by name, the packages that have upgrades, the cache |
The package file¶
A package is a ustar archive in gzip. The first member is .RATINFO, a
settings file:
[package]
name = lua
version = 5.4.9
release = 1
arch = i686
description = The Lua programming language interpreter
licence = MIT
installed_size = 223507
librat refuses GNU and pax headers, hard links, devices, a bad header
checksum, data after the end blocks, and every path or link that breaks
the rules in Make a package.
The folder of each path must also be a folder member of the package, so
librat never makes a folder that the package does not list. A package has
at most 65536 members, and .RATINFO is at most 64 KiB.
The database¶
The database is in /var/lib/rat:
| Path | Contents |
|---|---|
base.files |
every path of the image, one on each line. The build writes it with base_files.py |
installed/<name>/info |
the .RATINFO of the package, with installed (the UTC time) added |
installed/<name>/files |
one line for each member: type, mode, SHA-256 or link target, path |
lock |
an flock lock that an install or a remove holds |
transaction |
the change in progress, if there is one |
stage/ |
the files of a change before they move into place |
index.conf, index.conf.sig |
the last index that rat update accepted, and its signature |
sequence |
the largest index sequence that rat accepted |
cache/ |
downloaded packages; a package leaves it after its install |
A line of files has four fields with tabs between them:
d 0755 - usr/share/doc/lua
f 0755 3c9e... bin/lua
l 0777 lua bin/lua5.4
The lua package has no link; the third line shows the form of one.
A package can share a folder with the base system and with other packages. It cannot share a file or a link.
Versions¶
rat_version_compare orders two versions with the rules of Debian, then
by release. A version is a series of parts: a run of characters that
are not digits, then a run of digits. The parts compare from the left.
Digit runs compare as numbers, so 1.10 is after 1.9 and 1.01 is
the same as 1.1. In the other runs, ~ is before everything, also the
end of the version; the end is before letters, and letters are before
the other characters:
1.0~rc1 < 1.0 < 1.0a < 1.0+ < 1.0.1 < 1.10
Install and remove¶
An install has two steps:
- The prepare step changes nothing outside
/var/lib/rat. It unpacks the package intostage/, with each file namedfand the SHA-256 of its path. It checks each path againstbase.files, the other packages and the disk, and writes the new record intostage/record/. - The transaction makes the folders, moves the files and links from
stage/into place, and moves the record toinstalled/<name>/.
A remove is only a transaction: it removes the files and links, then each folder that is empty and that no other package and not the base system uses (the deepest first), then the record.
Upgrades¶
An install of a package that is installed is an upgrade, in one
transaction. The same version is refused, and an older one needs
RAT_DOWNGRADE (--downgrade). The prepare step checks the paths as
for an install, but a path of the old version is not a conflict. The
transaction then:
- removes the files and links of the old version that the new one does not have, or has as a folder,
- removes the folders of the old version that the new one does not have, when they are empty and nothing else uses them,
- makes the new folders and moves the files and links into place. A rename replaces a file of the old version,
- replaces the record.
A folder of the old version that is a file in the new one is refused: the
folder can have other files in it. record removes the old record and
renames stage/record in one operation, and while stage/record is
there, the operation is not done. A remove that runs again and finds a
folder, or an rmdir that finds a file, counts as done, because an
upgrade can put a folder where a file was.
rat upgrade uses rat_upgradable, which gives each installed package
that the index has in a newer version. Each package is its own
transaction: when one fails, the others that are done stay done. A
package that fails is tried again after the others, while that makes
progress, so a file can move from one package to another.
Settings files¶
A file under etc/ is a settings file. librat compares it with the
SHA-256 of the record before a change:
| Case | Result |
|---|---|
| upgrade, the file is not changed | the new file replaces it |
| upgrade, the file is changed and the new version has the old data | the changed file stays |
| upgrade, the file is changed and the new version has other data | the changed file stays; the new one goes to <path>.ratnew |
| upgrade, the new version does not have the file and it is changed | it stays |
| remove, the file is changed | it stays; its <path>.ratnew is removed |
| install, the file is on the disk and no package owns it | it stays; the new one goes to <path>.ratnew (--force replaces it) |
A file on the disk with the data of the new version is used as it is. A
<path>.ratnew that is a folder stops the change before it starts. The
record has the SHA-256 of the new version, so rat verify reports a
settings file that stayed.
Transactions¶
The transaction file has one operation on each line:
rat-transaction 1
note install lua 5.4.9-1
mkdir 0755 usr/share/doc/lua
move f3a9... bin/lua
record lua
end
librat writes the list to transaction.new, calls fsync, and renames
it to transaction, so the file is complete or not there. Then it does
the operations in order. When they are done, it removes the file and
stage/. Each operation gives the same result when it runs again: a
move whose source is gone is done, a remove of a missing path is
done. So when rat_open for writing finds a transaction file, it runs
all of it again and reports the note. A file without its first line or
its end line is damaged, and rat_open fails.
A prepare step that stops leaves only stage/, and nothing outside
/var/lib/rat changed. The next rat_open for writing removes it.
The host tests stop a change after each operation (rat_test_stop_after)
and check that the next open finishes it.
The repository¶
/etc/rat/repos.conf has the URL of the repository of each
architecture. The repository has index.conf, index.conf.sig and the
package files.
generated = 2026-12-02
sequence = 1796247155
arch = x86_64
[lua]
name = lua
version = 5.4.9
release = 1
arch = x86_64
installed_size = 273935
description = The Lua programming language interpreter
licence = MIT
file = lua-5.4.9-1-x86_64.rat
size = 134892
sha256 = 1f0c...
The keys before the first section are about the index. Each section is a
package, with the fields of its .RATINFO and the name, size and SHA-256
of its file. librat checks every field. The file must be
<name>-<version>-<release>-<arch>.rat, so an index cannot make rat
download or write another path. The arch of the index must be the
architecture of the machine, and the arch of a package the same or
any.
The signature¶
index.conf.sig is an ECDSA P-256 signature over the SHA-256 of
index.conf, in DER, as openssl dgst -sha256 -sign writes it.
signature.c reads each .pem file in /etc/rat/keys with mbedTLS,
uses the P-256 keys, and accepts the signature when one of them verifies
it. rat checks the signature each time it reads the index, also the
saved one.
rat update¶
- Download
index.conf.sig(at most 1 KiB) andindex.conf(at most 8 MiB) intoindex.conf.downloadandindex.conf.sig.download. - Check the signature and parse the index.
- Refuse the index if its
sequenceis smaller than the larger of thesequencefile and the saved index. An attacker who has an old signed index cannot give it again to keep old, broken packages. - Write
sequence, then rename the signature and the index into place.
A failure at any step deletes the downloads and keeps the old index. A
power cut between the renames leaves a signature that does not fit the
index; rat then asks for rat update, and the sequence file still
refuses an older index.
rat install NAME¶
- Read and check the saved index, and find the package.
- Download
<url>/<file>intocache/<file>.part. The download stops when it is larger than thesizein the index. - Check the size and the SHA-256 against the index. A file that does
not match is deleted. A file that matches becomes
cache/<file>. - Install
cache/<file>asrat install FILE.ratdoes, then delete it.
A file that is in the cache already and matches the index is used
without a download. rat clean empties the cache.
download.c lets libcurl use only https and file URLs, and follows a
redirect only to https: GitHub sends release assets from another host.
libcurl checks the server certificate with
/etc/ssl/certs/ca-certificates.crt. A connection that sends no data for
60 seconds stops.
Tests¶
make host-test runs src/lib/rat/test/. It makes packages with its own
tar writer (tarball.c), installs them into a folder on the host, and
checks the refused packages, the queries, verify, conflicts, remove,
the lock and the interrupted changes. version_test.c has a table of
version pairs. upgrade_test.c upgrades and downgrades a package whose
files, links and folders change, stops the upgrade after each operation,
and checks the settings files.
The repository tests sign indexes with the test key
test/keys/test-private.pem (with signer.c) and serve them from a
file:// folder. They check a good update and install, a changed index,
a changed package, an older index, a damaged saved index, a name that is
not in the index, and an upgrade by name. The tests build mbedTLS for the host from the source
that the mbedtls port fetched, in build/host-test/mbedtls, and use the
libcurl of the host.