Repository navigation
UAPI.16: make manifests easier to consume and combine - #253
Conversation
| RECOMMENDED to use the access mode 0755). A tool combining manifests | ||
| MUST add file objects for such implied directories to its output, | ||
| unless all of its inputs are flat listings. | ||
|
|
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
The spec recommends sorting the entries of a directory alphabetically but doesn't say what alphabetical means. Collation depends on the locale and on Unicode tables, so two tools can sort the same tree differently and both claim to sort it alphabetically. Comparing the bytes of the UTF-8 encoding is what strcmp() does and what systemd-manifest writes today, and for valid UTF-8 it's the same as comparing code points (cf. [1]). But that only holds for valid strings, and the spec doesn't say what a valid string is either. A JSON string can carry an escaped lone surrogate like "\uD800", which has no UTF-8 encoding at all. Parsers disagree on what to do with it: systemd's refuses it, others replace it with U+FFFD or keep it as it is. A consumer that repairs strings can turn two different names into the same name before it checks anything. Noncharacters such as U+FFFF are valid UTF-8, but systemd refuses them while other readers accept them, so a manifest one tool writes can fail to load in another. So define strings as valid UTF-8 after their escape sequences have been decoded, without NUL octets and, as in I-JSON (cf. [2]), without noncharacters. Consumers refuse invalid strings instead of repairing them. Strings are ordered by their octets as unsigned values, without Unicode normalization, case folding or collation. Note that this isn't the order the string comparison of JavaScript, Java or .NET gives. They compare UTF-16 code units and sort U+1F600 before U+FF61. Since none of this is alphabetical, call it the code point ordering. The wording for strings is what Lennart suggested in the review (cf. [3]). While at it, define the order of complete names. Comparing complete names as strings puts "a-b" before "a/x", but "a/x" has to come first since the entries of "a" immediately follow its file object. That's what sort(1) on a list of paths gets wrong. Comparing component by component gets it right, and so does treating "/" as lower than every other octet. Also use the same ordering for field names and for the keys of extended attributes, and encode values of extended attributes that contain noncharacters in Base64. [1]: https://www.rfc-editor.org/rfc/rfc3629 [2]: https://www.rfc-editor.org/rfc/rfc7493 [3]: uapi-group#253 (comment) Signed-off-by: Christian Brauner (Amutable) <[email protected]>
d23381d to
bf20046
Compare
The spec recommends sorting the entries of a directory alphabetically but doesn't say what alphabetical means. Collation depends on the locale and on Unicode tables, so two tools can sort the same tree differently and both claim to sort it alphabetically. Comparing the bytes of the UTF-8 encoding is what strcmp() does and what systemd-manifest writes today, and for valid UTF-8 it's the same as comparing code points (cf. [1]). But that only holds for valid strings, and the spec doesn't say what a valid string is either. A JSON string can carry an escaped lone surrogate like "\uD800", which has no UTF-8 encoding at all. Parsers disagree on what to do with it: systemd's refuses it, others replace it with U+FFFD or keep it as it is. A consumer that repairs strings can turn two different names into the same name before it checks anything. Noncharacters such as U+FFFF are valid UTF-8, but systemd refuses them while other readers accept them, so a manifest one tool writes can fail to load in another. So define strings as valid UTF-8 after their escape sequences have been decoded, without NUL octets and, as in I-JSON (cf. [2]), without noncharacters. Consumers refuse invalid strings instead of repairing them. Strings are ordered by their octets as unsigned values, without Unicode normalization, case folding or collation. Note that this isn't the order the string comparison of JavaScript, Java or .NET gives. They compare UTF-16 code units and sort U+1F600 before U+FF61. Since none of this is alphabetical, call it the code point ordering. The wording for strings is what Lennart suggested in the review (cf. [3]). While at it, define the order of complete names. Comparing complete names as strings puts "a-b" before "a/x", but "a/x" has to come first since the entries of "a" immediately follow its file object. That's what sort(1) on a list of paths gets wrong. Comparing component by component gets it right, and so does treating "/" as lower than every other octet. Also use the same ordering for field names and for the keys of extended attributes, and encode values of extended attributes that contain noncharacters in Base64. [1]: https://www.rfc-editor.org/rfc/rfc3629 [2]: https://www.rfc-editor.org/rfc/rfc7493 [3]: uapi-group#253 (comment) Signed-off-by: Christian Brauner (Amutable) <[email protected]>
bf20046 to
7a2ba13
Compare
|
makes a ton of sense to me, some comments |
The spec recommends sorting the entries of a directory alphabetically but doesn't say what alphabetical means. Collation depends on the locale and on Unicode tables, so two tools can sort the same tree differently and both claim to sort it alphabetically. Comparing the bytes of the UTF-8 encoding is what strcmp() does and what systemd-manifest writes today, and for valid UTF-8 it's the same as comparing code points (cf. [1]). But that only holds for valid strings, and the spec doesn't say what a valid string is either. A JSON string can carry an escaped lone surrogate like "\uD800", which has no UTF-8 encoding at all. Parsers disagree on what to do with it: systemd's refuses it, others replace it with U+FFFD or keep it as it is. A consumer that repairs strings can turn two different names into the same name before it checks anything. Noncharacters such as U+FFFF are valid UTF-8, but systemd refuses them while other readers accept them, so a manifest one tool writes can fail to load in another. So define strings as valid UTF-8 after their escape sequences have been decoded, without NUL octets and, as in I-JSON (cf. [2]), without noncharacters. Consumers refuse invalid strings instead of repairing them. Strings are ordered by their octets as unsigned values, without Unicode normalization, case folding or collation. Note that this isn't the order the string comparison of JavaScript, Java or .NET gives. They compare UTF-16 code units and sort U+1F600 before U+FF61. Since none of this is alphabetical, call it the code point ordering. The wording for strings is what Lennart suggested in the review (cf. [3]). While at it, define the order of complete names. Comparing complete names as strings puts "a-b" before "a/x", but "a/x" has to come first since the entries of "a" immediately follow its file object. That's what sort(1) on a list of paths gets wrong. Comparing component by component gets it right, and so does treating "/" as lower than every other octet. Also use the same ordering for field names and for the keys of extended attributes, and encode values of extended attributes that contain noncharacters in Base64. [1]: https://www.rfc-editor.org/rfc/rfc3629 [2]: https://www.rfc-editor.org/rfc/rfc7493 [3]: uapi-group#253 (comment) Signed-off-by: Christian Brauner (Amutable) <[email protected]>
Currently a file object can name a file several directories deep without any file object for the directories above it. Files below a subdirectory have to immediately follow the file object of the subdirectory. A consumer that reads a manifest as a stream checks that with a stack of the directories that are still open, which only needs the names along the current path. But that only works if every directory has a file object. Otherwise a file below a directory that isn't open may be in a directory that was never listed or in one whose entries ended long ago, and telling the two apart means remembering every directory seen so far. A consumer that creates the files also has to make up the attributes of directories nobody listed, and a tool that combines manifests has to make up file objects for them. So require every file object to come after the file object of its directory, or after the root file object if it is at the top level. Together with the rule that the entries of a directory immediately follow it, every manifest is in tree order then. As Lennart pointed out, cpio effectively requires that anyway (cf. [1]). systemd-manifest make already lists every directory. This changes the mapping to SHA256SUMS, whose names can contain slashes. A manifest still maps to a SHA256SUMS file by dropping the dir objects. But going the other way now adds a dir object for every directory, and needs the files of every directory to be listed together. SHA256SUMS files without slashes still map 1:1. [1]: uapi-group#253 (comment) Signed-off-by: Christian Brauner (Amutable) <[email protected]>
The spec doesn't say when two file objects mean the same, so tools that compare manifests compare their JSON text, and the result depends on how a producer happens to write things. The join that systemd-manifest is gaining refuses to join objects that only differ in how they are written, for example "uid":null versus no uid at all. Making the JSON itself reproducible was rejected when the spec was written (cf. [1]), and this doesn't need it. Define equivalence on the level of JSON values instead. The order of fields and the escaping of strings don't matter, and a field set to null is the same as a field that isn't set, which the spec already says elsewhere. Values aren't interpreted any further, so this works just as well for fields a tool doesn't know, which keeps the spec extensible (cf. [2]). That's also what the join does, except that it tells null and unset apart. Comparing values means that "type":"reg" and no type at all, or an uppercase and a lowercase sha256, are not equivalent. The spec already says that sha256 SHALL be written in lowercase. For the former, recommend that generators don't spell out defaults. [1]: uapi-group#213 [2]: uapi-group#253 (comment) Signed-off-by: Christian Brauner (Amutable) <[email protected]>
7a2ba13 to
8104147
Compare
The spec recommends sorting the entries of a directory alphabetically but doesn't say what alphabetical means. Collation depends on the locale and on Unicode tables, so two tools can sort the same tree differently and both claim to sort it alphabetically. Comparing the bytes of the UTF-8 encoding is what strcmp() does and what systemd-manifest writes today, and for valid UTF-8 it's the same as comparing code points (cf. [1]). But that only holds for valid strings, and the spec doesn't say what a valid string is either. A JSON string can carry an escaped lone surrogate like "\uD800", which has no UTF-8 encoding at all. Parsers disagree on what to do with it: systemd's refuses it, others replace it with U+FFFD or keep it as it is. A consumer that repairs strings can turn two different names into the same name before it checks anything. Noncharacters such as U+FFFF are valid UTF-8, but systemd refuses them while other readers accept them, so a manifest one tool writes can fail to load in another. So define strings as valid UTF-8 after their escape sequences have been decoded, without NUL octets and, as in I-JSON (cf. [2]), without noncharacters. Consumers refuse invalid strings instead of repairing them. Strings are ordered by their octets as unsigned values, without Unicode normalization, case folding or collation. Note that this isn't the order the string comparison of JavaScript, Java or .NET gives. They compare UTF-16 code units and sort U+1F600 before U+FF61. Since none of this is alphabetical, call it the code point ordering. The wording for strings is what Lennart suggested in the review (cf. [3]). While at it, define the order of complete names. Comparing complete names as strings puts "a-b" before "a/x", but "a/x" has to come first since the entries of "a" immediately follow its file object. That's what sort(1) on a list of paths gets wrong. Comparing component by component gets it right, and so does treating "/" as lower than every other octet. Also use the same ordering for field names and for the keys of extended attributes, and encode values of extended attributes that contain noncharacters in Base64. [1]: https://www.rfc-editor.org/rfc/rfc3629 [2]: https://www.rfc-editor.org/rfc/rfc7493 [3]: uapi-group#253 (comment) Signed-off-by: Christian Brauner (Amutable) <[email protected]>
Currently a file object can name a file several directories deep without any file object for the directories above it. Files below a subdirectory have to immediately follow the file object of the subdirectory. A consumer that reads a manifest as a stream checks that with a stack of the directories that are still open, which only needs the names along the current path. But that only works if every directory has a file object. Otherwise a file below a directory that isn't open may be in a directory that was never listed or in one whose entries ended long ago, and telling the two apart means remembering every directory seen so far. A consumer that creates the files also has to make up the attributes of directories nobody listed, and a tool that combines manifests has to make up file objects for them. So require every file object to come after the file object of its directory, or after the root file object if it is at the top level. Together with the rule that the entries of a directory immediately follow it, every manifest is in tree order then. As Lennart pointed out, cpio effectively requires that anyway (cf. [1]). systemd-manifest make already lists every directory. This changes the mapping to SHA256SUMS, whose names can contain slashes. A manifest still maps to a SHA256SUMS file by dropping the dir objects. But going the other way now adds a dir object for every directory, and needs the files of every directory to be listed together. SHA256SUMS files without slashes still map 1:1. [1]: uapi-group#253 (comment) Signed-off-by: Christian Brauner (Amutable) <[email protected]>
The entries of a directory may come in any order. That's convenient for producers but leaves consumers guessing. A consumer that wants to merge manifests, or to check that no name is listed twice, has to keep every name of a directory around for a manifest in an unknown order. With the code point ordering it only needs to compare each name with the previous one, so the memory it needs doesn't grow with the manifest. Mandating one ordering was rejected when the spec was written (cf. [1]): some listings are better sorted by version or in the order their entries were added, and GPT manifests list partitions in the order of the partition table. So instead let a manifest declare that it is sorted by code point, through the new "order" field of the root file object, and recommend that manifests sorted that way declare it. A consumer that relies on it checks it while reading, which costs nothing given the above. The field says that the complete names strictly increase. That covers the order within each directory and rules out repeated names. The field describes the manifest and not the root directory. So tools that combine manifests don't compare it between their inputs, and only set it on their output if it holds there. [1]: uapi-group#213 (comment) Signed-off-by: Christian Brauner (Amutable) <[email protected]>
JSON doesn't say what an object with the same key twice means: RFC 8259 calls the behavior of software that receives one unpredictable (cf. [1]). Parsers take the first value, the last one, or refuse. So two consumers can read different values out of the same signed manifest. systemd's reader refuses a repeated field it knows, but takes a repeated one it doesn't. So forbid duplicate keys and have consumers refuse them. A producer built on a JSON library doesn't write them anyway. [1]: https://www.rfc-editor.org/rfc/rfc8259#section-4 Signed-off-by: Christian Brauner (Amutable) <[email protected]>
Names have to be unique and files below a directory have to follow the directory, but nothing says what a consumer does if they don't. Readers differ today. systemd's reader hands both objects to its callers, an image builder may let the later one replace the earlier one, and a verifier may check both against the same file. So the same manifest describes different trees depending on who reads it. Recommend refusing such manifests. Both conditions can be checked while reading: misplaced objects with a stack of the open directories, and repeated names by comparing each name with the previous one if the manifest declares the code point ordering. Signed-off-by: Christian Brauner (Amutable) <[email protected]>
Without limits every reader picks its own. systemd's reader and ufat's both refuse records larger than 4 MiB, other readers will pick other numbers, and a manifest that one of them reads fine fails in another. A limit on names also bounds what a reader that checks the order has to keep around: the names along the current path. So make the 4 MiB the existing readers use the limit for a record, and limit names to 4095 bytes, which is PATH_MAX without the terminating NUL. Components aren't limited on their own, since a 255 character name on FAT or NTFS can take more than 255 bytes in UTF-8. Signed-off-by: Christian Brauner (Amutable) <[email protected]>
Two things about inodeToken are unclear:
(1) systemd-manifest make gives a token to a file whose other names
are outside the tree it walks, so the token is carried by a single
file object. The spec describes tokens as identifying files that
are hardlinked within the tree, so that's arguably not allowed.
Allow it explicitly. A producer that writes as it walks only finds
out at the end that a group has a single member.
(2) The file objects of a group describe one inode, but only the
extended attributes and the flag fields are required to be
identical. Require it for all inode attributes.
Signed-off-by: Christian Brauner (Amutable) <[email protected]>
What a manifest records depends on how it was made. systemd-manifest make takes --field= to pick what it records, and other tools record what the file system they read has to offer. So a field that a file object doesn't carry can mean two things: that the property isn't set, or that the manifest doesn't record it. For hard links that's what makes joining manifests ambiguous, a missing inode token means "not hardlinked" in one input and "don't know" in the other. The same goes for extended attributes and flags, and for comparing manifests in general. So let the root file object list the features the manifest records, in a new features field, as Lennart suggested (cf. [1]). The values are the names make uses for --field=, plus one for each flag. A file object must not carry a field of a feature that isn't listed. For hard links, extended attributes and flags, a listed feature also says what a missing field means: the file isn't hardlinked, has no extended attributes, or doesn't have the flag set. Without the field a manifest records what make records by default, so make's default output doesn't change. Tools that compare or combine manifests compare their features first. If they differ, they either refuse or drop the fields of the features that not all of them record. A manifest made from a SHA256SUMS file only records sha256. [1]: uapi-group#253 (comment) Signed-off-by: Christian Brauner (Amutable) <[email protected]>
The spec doesn't say when two file objects mean the same, so tools that compare manifests compare their JSON text, and the result depends on how a producer happens to write things. The join that systemd-manifest is gaining refuses to join objects that only differ in how they are written, for example "uid":null versus no uid at all. Making the JSON itself reproducible was rejected when the spec was written (cf. [1]), and this doesn't need it. Define equivalence on the level of JSON values instead. The order of fields and the escaping of strings don't matter, and a field set to null is the same as a field that isn't set, which the spec already says elsewhere. Values aren't interpreted any further, so this works just as well for fields a tool doesn't know, which keeps the spec extensible (cf. [2]). That's also what the join does, except that it tells null and unset apart. Comparing values means that "type":"reg" and no type at all, or an uppercase and a lowercase sha256, are not equivalent. The spec already says that sha256 SHALL be written in lowercase. For the former, recommend that generators don't spell out defaults. [1]: uapi-group#213 [2]: uapi-group#253 (comment) Signed-off-by: Christian Brauner (Amutable) <[email protected]>
The note on numbers says that only files of excessive sizes go beyond what a 64-bit floating point number holds exactly, so that it doesn't matter in practice. But mTimeNSec is above 2^53 for every point in time after mid-April 1970. JSON.parse() in JavaScript turns 1777615169123123123 into 1777615169123123200, and jq does the same as soon as it computes anything with the value. So correct the note, say how these numbers are written, and require consumers to process them as unsigned 64-bit integers. Signed-off-by: Christian Brauner (Amutable) <[email protected]>
8104147 to
33a254a
Compare
|
lgtm |
Currently, the entries of a directory may appear in any order. The spec recommends sorting them by code point, by UAPI.10 version or by the time they were added, and a manifest sorted by code point should declare it with the `order` field. That's a problem for tools that create a file system or an archive from a manifest. ufat creates FAT images from manifests and ucpio creates cpio archives from them, and both read the manifest record by record. If the entries of a directory may come in any order, such a builder has to keep the names of every directory that is still open in memory to find a name that is listed twice, or it has to sort the whole manifest first, which takes memory or temporary storage in proportion to the size of the manifest. And if two producers list the same tree in different orders, a builder that writes the entries in the order of the manifest creates two different images of the same tree. When the spec was added I asked to either mandate one ordering or to just say "any order". Lennart preferred "any order", since sorting is hard and different use-cases want different orderings, such as version ordering (cf. [1]). For listings like the ones sysupdate consumes it's also handy to just append new entries at the end (cf. [2]). And a manifest that only describes files that exist already, for example to verify them later, doesn't need a particular order either. systemd-manifest verify checks every file object on its own. So we shouldn't mandate one ordering for every manifest. Instead, let's put the rule on the manifests that file systems and archives are created from. The producer knows whether it generates a manifest for that or for describing files that exist already. Such a manifest must list the entries of every directory in the code point ordering, and must declare it with `order`. The declaration is in the root file object, so a builder knows up front what it is going to get, and it can check the order while reading with one comparison of each name with the previous one. A builder may refuse a manifest that is not in the code point ordering instead of sorting it. Manifests of a GPT partition table are not affected. systemd-repart --manifest-gpt lists the whole image and then the partitions in the order of its partition list, which for a table it created is the order of the partition table. Nothing changes for the tools that write such manifests today but the declaration. systemd-manifest make, and systemd-dissect --uapi16 and systemd-repart --manifest, which use the same code, already sort every directory with strcmp(), whatever their output is used for later. So they can always set `order`, which none of them does yet. While at it, point to the exception where the spec says that files may appear in any order within a directory. [1]: uapi-group#213 (comment) [2]: uapi-group#253 (comment) Signed-off-by: Christian Brauner (Amutable) <[email protected]>
Currently, the entries of a directory may appear in any order. The spec recommends sorting them by code point, by UAPI.10 version or by the time they were added, and a manifest sorted by code point should declare it with the `order` field. That's a problem for tools that create a file system or an archive from a manifest. ufat creates FAT images from manifests and ucpio creates cpio archives from them, and both read the manifest record by record. If the entries of a directory may come in any order, such a builder has to keep the names of every directory that is still open in memory to find a name that is listed twice, or it has to sort the whole manifest first, which takes memory or temporary storage in proportion to the size of the manifest. And if two producers list the same tree in different orders, a builder that writes the entries in the order of the manifest creates two different images of the same tree. When the spec was added I asked to either mandate one ordering or to just say "any order". Lennart preferred "any order", since sorting is hard and different use-cases want different orderings, such as version ordering (cf. [1]). For listings like the ones sysupdate consumes it's also handy to just append new entries at the end (cf. [2]). And a manifest that only describes files that exist already, for example to verify them later, doesn't need a particular order either. systemd-manifest verify checks every file object on its own. So we shouldn't mandate one ordering for every manifest. Instead, let's put the rule on the manifests that file systems and archives are created from, and on the ones whose file objects are placed in a file system or an archive that exists already. The producer knows whether it generates a manifest for that or for describing files that exist already. Such a manifest must list the entries of every directory in the code point ordering, and must declare it with `order`. The declaration is in the root file object, so a builder knows up front what it is going to get, and it can check the order while reading with one comparison of each name with the previous one. A builder, or a tool that adds files to an existing file system or archive, may refuse a manifest that is not in the code point ordering instead of sorting it. For adding files we also need to cover the manifest of the file system or archive that exists already. If that manifest declares an ordering and the new file objects are added to it, it has to stay in that ordering and keep declaring it. So a tool can't just append them at the end and drop `order`. Otherwise a builder may refuse to create the image again from the updated manifest, and the manifest differs from the one of the same image created in one go. Since the new file objects come in the code point ordering too, the tool can merge them in with one pass. Note that this is only about the manifests. Where the new entries end up in the file system or archive is up to the tool. Manifests of a GPT partition table are not affected. systemd-repart --manifest-gpt lists the whole image and then the partitions in the order of its partition list, which for a table it created is the order of the partition table. Nothing changes for the tools that write such manifests today but the declaration. systemd-manifest make, and systemd-dissect --uapi16 and systemd-repart --manifest, which use the same code, already sort every directory with strcmp(), whatever their output is used for later. So they can always set `order`, which none of them does yet. While at it, point to the exception where the spec says that files may appear in any order within a directory, say "where" instead of "even if" in the two places that say that no ordering is mandated, and mention the new requirement where the `order` field is described. [1]: uapi-group#213 (comment) [2]: uapi-group#253 (comment) Signed-off-by: Christian Brauner (Amutable) <[email protected]>
| field names in the ordering of strings (see above). | ||
|
|
||
| A JSON object in a manifest MUST NOT contain the same key twice, and | ||
| consumers MUST refuse manifests that contain such objects. |
There was a problem hiding this comment.
It is very difficult for consumers to actually guarantee this because most JSON parsers do not provide enough knobs to ensure it when deserialising JSON blobs.
I think MUST on the consumer is too strong here.
| For every file object other than the root file object, the manifest | ||
| MUST list the file object of the directory it is located in before it: | ||
| the file object of type `dir` whose name is its name without the last | ||
| component, or the root file object if its name has a single | ||
| component. Together with the requirement that files nested into | ||
| subdirectories immediately follow the file object of the subdirectory | ||
| (see above), this means that the file objects of a manifest are listed | ||
| in tree order: every directory comes before the file objects below it, | ||
| and these immediately follow it. |
There was a problem hiding this comment.
This is really quite onerous for manifest generators that are not being generated from a real filesystem -- for instance, in Quarry we generate manifests from TUF which are a list of filenames.
In order to fulfill this requirement we would need to track which fake directory entries we've emitted and all of that jazz for no real benefit -- if we know that the consumer of the manifest only cares about files why add more complexity to producers?
| Two file objects are equivalent if they carry the same fields with the | ||
| same JSON values. This does not depend on the order of the fields in |
There was a problem hiding this comment.
One issue mtree ran into was that manifests generated from different sources can have different levels of VFS support and so you need to come up with some kind of mechanism for comparing them in the non-trivially-equal case.
The most common problem was that of tar archives -- most tar archive producers do not produce tar archives with sub-second precision and so manifests generated from them will always fail to validate. In go-mtree this was worked around by having separate tar_time and time attributes with special logic for comparing them, but maybe it would be better to have some markers in the root object to define how attribute comparisons should be done if direct comparison is not preferred?
Similarly, not all filesystems support xattrs (and users might want to just say that xattrs not being set is fine) and kernel features can cause xattrs to be auto-applied to files, so just naively comparing the values can be problematic in that case too.
Currently we write manifests by hand. The generator of a host tree and the writer of a volume both build a json::Object per entry, sort its keys and frame it with write_record(). Let's port both of them to the uapi16 crate. They fill in a FileObject per entry and hand it to a uapi16::Writer, which frames the records and checks what it writes the same way the reader checks what it reads. While at it, turn the free functions into methods. generate() becomes HostTree::write_manifest() and volume::write() becomes Volume::write_manifest(). What a caller asks a manifest to record are the features of UAPI.16 now, uapi16::Features, instead of our own Fields. Since uapi-group/specifications#253 the root object of a manifest has two more fields. features lists what the manifest records whenever that isn't the default set, and order declares the order its file objects are in. Both writers sort their objects in code point order already, so they say so with "order": "codepoint". The generator records size, sha256 and mtime and lists them. The manifest of a volume records what FAT has of what it is asked for: sizes, hashes and times. Where FAT has none of what a feature describes, as for symbolic links, device nodes, hard links and extended attributes, the manifest lists the feature and so says that there is none. The Varlink ManifestSink follows. It took systemd-manifest's field names in fields. Now it takes the names of the features of UAPI.16 in features, with "_" in place of "-", the way systemd writes the values of enumerations. So hash is sha256 and userXattr is user_xattr. Names are uapi16::Name now, which follows the spec: a component can be longer than 255 bytes, and a name can't hold a noncharacter. An entry of a volume whose name a manifest can't carry is left out and counted in omitted as before. Note that the generator used to list a directory whose name no manifest can carry, which no reader takes, ours included. It leaves such a directory out now, with everything below it, as systemd-manifest does. systemd-manifest doesn't write features and order yet. So make-fixtures.sh adds both to the fixtures after systemd-manifest wrote them, and tests/fixtures.rs keeps comparing our writers with them byte for byte. That changes the bytes the signature of signed.Uapi16Manifest covers and the signature doesn't match anymore, which the README of the fixtures says. The uapi16 crate gets a test that reads every fixture and writes it back. The errors of the crate are turned into ours in src/fat/manifest/error.rs, with the errno and the detail a Varlink client sees. ManifestProblem gains the problems the crate knows about, and so does the interface description. The reader is still the old one. Signed-off-by: Christian Brauner (Amutable) <[email protected]>
Currently we write manifests by hand. The generator of a host tree and the writer of a volume both build a json::Object per entry, sort its keys and frame it with write_record(). Let's port both of them to the uapi16 crate. They fill in a FileObject per entry and hand it to a uapi16::Writer, which frames the records and checks what it writes the same way the reader checks what it reads. While at it, turn the free functions into methods. generate() becomes HostTree::write_manifest() and volume::write() becomes Volume::write_manifest(). What a caller asks a manifest to record are the features of UAPI.16 now, uapi16::Features, instead of our own Fields. The merged specification gives the root object of a manifest two more fields (cf. [1]). features lists what the manifest records whenever that isn't the default set, and order declares the order its file objects are in. Both writers sort their objects in code point order already, so they say so with "order": "codepoint". The generator records size, sha256 and mtime and lists them. The manifest of a volume records what FAT has of what it is asked for: sizes, hashes and times. Where FAT has none of what a feature describes, as for symbolic links, device nodes, hard links and extended attributes, the manifest lists the feature and so says that there is none. The Varlink ManifestSink follows. It took systemd-manifest's field names in fields. Now it takes the names of the features of UAPI.16 in features, with "_" in place of "-", the way systemd writes the values of enumerations. So hash is sha256 and userXattr is user_xattr. Names are uapi16::Name now, which follows the spec: a component can be longer than 255 bytes, and a name can't hold a noncharacter. An entry of a volume whose name a manifest can't carry is left out and counted in omitted as before. Note that the generator used to list a directory whose name no manifest can carry, which no reader takes, ours included. It leaves such a directory out now, with everything below it, as systemd-manifest does. systemd-manifest doesn't write features and order yet. So make-fixtures.sh adds both to the fixtures after systemd-manifest wrote them, and tests/fixtures.rs keeps comparing our writers with them byte for byte. That changes the bytes the signature of signed.Uapi16Manifest covers and the signature doesn't match anymore, which the README of the fixtures says. The uapi16 crate gets a test that reads every fixture and writes it back. The errors of the crate are turned into ours in src/fat/manifest/error.rs, with the errno and the detail a Varlink client sees. ManifestProblem gains the problems the crate knows about, and so does the interface description. The reader is still the old one. [1]: uapi-group/specifications#253 Signed-off-by: Christian Brauner (Amutable) <[email protected]>
Currently we write manifests by hand. The generator of a host tree and the writer of a volume both build a json::Object per entry, sort its keys and frame it with write_record(). Let's port both of them to the uapi16 crate. They fill in a FileObject per entry and hand it to a uapi16::Writer, which frames the records and checks what it writes the same way the reader checks what it reads. While at it, turn the free functions into methods. generate() becomes HostTree::write_manifest() and volume::write() becomes Volume::write_manifest(). What a caller asks a manifest to record are the features of UAPI.16 now, uapi16::Features, instead of our own Fields. The merged specification gives the root object of a manifest two more fields (cf. [1]). features lists what the manifest records whenever that isn't the default set, and order declares the order its file objects are in. Both writers sort their objects in code point order already, so they say so with "order": "codepoint". The generator records size, sha256 and mtime and lists them. The manifest of a volume records what FAT has of what it is asked for: sizes, hashes and times. Where FAT has none of what a feature describes, as for symbolic links, device nodes, hard links and extended attributes, the manifest lists the feature and so says that there is none. The Varlink ManifestSink follows. It took systemd-manifest's field names in fields. Now it takes the names of the features of UAPI.16 in features, with "_" in place of "-", the way systemd writes the values of enumerations. So hash is sha256 and userXattr is user_xattr. Names are uapi16::Name now, which follows the spec: a component can be longer than 255 bytes, and a name can't hold a noncharacter. An entry of a volume whose name a manifest can't carry is left out and counted in omitted as before. Note that the generator used to list a directory whose name no manifest can carry, which no reader takes, ours included. It leaves such a directory out now, with everything below it, as systemd-manifest does. systemd-manifest doesn't write features and order yet. So make-fixtures.sh adds both to the fixtures after systemd-manifest wrote them, and tests/fixtures.rs keeps comparing our writers with them byte for byte. That changes the bytes the signature of signed.Uapi16Manifest covers and the signature doesn't match anymore, which the README of the fixtures says. The uapi16 crate gets a test that reads every fixture and writes it back. The errors of the crate are turned into ours in src/fat/manifest/error.rs, with the errno and the detail a Varlink client sees. ManifestProblem gains the problems the crate knows about, and so does the interface description. The reader is still the old one. [1]: uapi-group/specifications#253 Signed-off-by: Christian Brauner (Amutable) <[email protected]>
Currently we write manifests by hand. The generator of a host tree and the writer of a volume both build a json::Object per entry, sort its keys and frame it with write_record(). Let's port both of them to the uapi16 crate. They fill in a FileObject per entry and hand it to a uapi16::Writer, which frames the records and runs the checks of the reader on every object it writes. While at it, turn the free functions into methods. generate() becomes HostTree::write_manifest() and volume::write() becomes Volume::write_manifest(). Both take any Write, and both return a WriteReport. What a caller asks a manifest to record are the features of UAPI.16 now, uapi16::Features, instead of our own Fields. The merged specification gives the root object of a manifest two more fields (cf. [1]). features lists what the manifest records whenever that isn't the default set, and order declares the order its file objects are in. Both writers sort their objects in code point order already, so they say so with "order": "codepoint". The generator records size, sha256 and mtime and lists them. The manifest of a volume records what FAT has of what it is asked for: sizes, hashes and times. Where FAT has none of what a feature describes, as for symbolic links, device nodes, hard links and extended attributes, the manifest lists the feature and so says that there is none. The read-only attribute is different. Every entry of a volume has it set or cleared, and the specification has a read-only feature now (cf. [2]): where a manifest lists it, an object without readOnly isn't read-only. So the manifest of a volume always records the attribute, whatever the caller asks for. It lists read-only and writes readOnly where the attribute is set. The writer refuses readOnly without the feature anyway. The writer leaves out what the specification recommends leaving out. So an empty file goes out with a size of 0 and without the hash of no data, and a reader fills the hash in again. The Varlink ManifestSink follows. It took systemd-manifest's field names in fields. Now it takes the names of the features of UAPI.16 in features, with "_" in place of "-", the way systemd writes the values of enumerations. So hash is sha256 and userXattr is user_xattr. Names are uapi16::Name now, which follows the spec: a component can be longer than 255 bytes, and a name can't hold a noncharacter. An entry of a volume whose name a manifest can't carry is left out and counted in omitted as before. Note that the generator used to list a directory whose name no manifest can carry, which no reader takes, ours included. It leaves such a directory out now, with everything below it, as systemd-manifest does. The generator also left out a file whose name no manifest can carry without a word, and a name that isn't UTF-8 or a path longer than a volume takes failed the walk. Now it leaves all of them out and counts the regular files and directories among them the way the writer of a volume does, a directory with everything below it as one, and ufat manifest says how many entries aren't in the manifest, as --write-manifest does. walk_tree() leaves out and counts what it can't name when the generator asks it to. The populator and the planner still fail on it, since they couldn't store it. The long names of a volume are decoded with String::from_utf16_lossy(), so a long name with an unpaired surrogate reads with U+FFFD in its place, and two names that differ only there read as one. Reading a directory now decodes a long name with String::from_utf16(), which refuses exactly such a name, and says how the name came out in a new NameDecoding, Exact or Lossy, which Metadata carries as well. The writer of a volume leaves the lossy names out and counts them before it compares names. The NameDecoding of an entry goes next to the count of its long name slots, which becomes a byte since a set has at most 20 slots, so that the entries the directory cache keeps don't grow. Currently ufat manifest keeps the whole manifest in memory before it prints anything, 150 to 250 bytes per file. The point was to print nothing of a tree that can't be listed to its end. But the trailer covers that already: a manifest without one is incomplete and readers refuse it, and ufat exits with an error. So let's stream the manifest to standard output while the tree is walked. The generator records the size of what it hashed instead of the size that stat() reported before. So the two agree for a file that changes while it is read, and the writer never sees an empty file with the hash of some data, which it would refuse. It reads a file no further than the size the walk found, so a file that keeps growing doesn't keep it busy. Afterwards it compares the size, the modification time and the change time with what the walk found, the way tar does. A file that changed while it was read is counted in a new field of WriteReport, changed, and ufat manifest says how many there were. The exit status stays 0, since 1 says that the manifest has no trailer. Currently, the writer of a volume walks the directory tree by path. It lists a directory with read_dir(path) and reads a file with begin_read(path), and both look up every component of the path again. On a damaged volume that lookup can find another entry than the one the walk is at. Lookups fold names, so of "AAAA" and "aaaa" in one directory it finds the same one twice, and the manifest lists what "aaaa" holds under "AAAA" as well. So let's walk the tree by cluster instead. A directory is listed from the first cluster its entry names, with a crate-private read_dir_at() that does what read_dir() does once it has found the cluster. A file is read from the first cluster and the size of its entry with begin_read_at(). Note that begin_read() walks the chain of a file once before reading to see that it's long enough. begin_read_at() doesn't, and the read that runs past the end of a short chain fails with Corrupt instead. The read of the last cluster of a file looks at the FAT entry of that cluster, which has to end the chain or go on. So a chain that runs into a free, bad or invalid entry right after the file fails as it did before, and the rest of a longer chain isn't read. The writer also keeps the first cluster of every directory it listed in a HashSet, to refuse a directory tree that is cross-linked or that loops. That set grows with the number of directories on the volume. Comparing with the first clusters of the directories that are open would catch a loop only. A cross-link that doesn't loop does end, but a chain of directories that each hold two entries pointing at the next one is listed 2^k times for k levels, and 40 levels take 40 clusters of a small image. And first clusters aren't enough either, since chains can run together further down. 200 empty directories whose chains run on into the chain of a directory of 1000 files list those files 201 times, and 1000 entries of files of 4 MiB that share one chain make us hash 4 GiB. So keep one bit for every cluster of the volume instead, set for every cluster of every directory the walk lists, which Directory::chain() has, and for every cluster of every file it reads for the hash, one per read. Where the walk reaches a cluster whose bit is set already it fails with EUCLEAN and Corrupt. IOW, the walk lists no directory and reads no file twice, and takes time in proportion to the size of the volume. The bits take 16 KiB for a typical ESP and 32 MiB at the most clusters FAT32 has. A directory whose entry points at cluster 0 or past the last cluster of the volume fails with Corrupt as well, and so does a file that isn't empty when it is read for the hash, which is when the manifest records sha256. load_directory() refused a directory whose own chain loops or is longer than a directory can be with a bare EUCLEAN, which a Varlink client sees as io.systemd.System. It names the directory and its first cluster now, so Describe answers Corrupt there too. A damaged directory can also hold two entries of the same name. The writer of the crate would refuse the second one as an InvalidManifest with reason duplicateName, which blames a manifest the caller never sent. The entries of a directory are sorted right there, so compare each name with the one before and fail with EUCLEAN and Corrupt, before the record of the directory goes out. Names that FAT takes for one, like "AAAA" and "aaaa", are two names to a manifest. Since the walk follows the cluster of every entry, it lists each of them with what it holds. Linux writes such directories with some mount options. With iocharset=utf8, for instance, it compares names byte for byte. fsck reports them, which is its job, and the manifest describes what is on the volume. The systemd-manifest the fixtures were made with doesn't write features and order. systemd-manifest writes both since 118717cfe2 and 330fab5e90, which aren't in a release yet. So make-fixtures.sh adds both to the fixtures after systemd-manifest wrote them, and tests/fixtures.rs keeps comparing our writers with them byte for byte. The manifest of a volume lists read-only on top of the features systemd-manifest records, so the comparison adds read-only to the features of systemd's manifest. Adding features and order changes the bytes the signature of signed.Uapi16Manifest covers and the signature doesn't match anymore, which the README of the fixtures says. tests/fixtures.rs also gets a test that reads every fixture with the uapi16 crate and writes it back. It lives there and not in the crate, so that the crate needs no files from outside of it for its tests. Note that the systemd-manifest the fixtures were made with writes the hash of no data next to a size of 0. The writer leaves it out, and so does systemd-manifest since e83f7bce0c, which isn't in a release yet either. A reader that fills it in again marks it as filled in, so the empty file of the special case wouldn't read back as the same object. So make-fixtures.sh also takes that hash out of the file objects of size 0, as the specification recommends. The empty file of the special case is the only one, and the byte comparisons don't use that case. The errors of the crate's writer are turned into ours in src/fat/manifest/error.rs, with the errno and the detail a Varlink client sees. Instead of mirroring uapi16::Problem with a list of its own, ManifestProblem carries the problem itself, and Problem::as_str() gives the name a client sees. So the names on the wire stay what they are. noSize isn't a problem of the crate and stays ours. The interface description gains the problems the crate knows about, and tests/varlink.rs checks that its ManifestProblem has the names of Problem::ALL and noSize. verify/lib.rs builds error.rs for Kani without uapi16, so it stands in for uapi16 with a Problem that has no value. The reader is still the old one. It reports its problems with the variants of uapi16::Problem now, under the names it used before. [1]: uapi-group/specifications#253 [2]: uapi-group/specifications@1eb019d Signed-off-by: Christian Brauner (Amutable) <[email protected]>
I've been writing two consumers of UAPI.16 manifests, ufat which builds
FAT images from them and ucpio which builds cpio archives, and I looked
at the join verb systemd-manifest is gaining in 1. In all three the
same few things the spec leaves open turned into either memory that
grows with the manifest or into consumers that disagree about what a
manifest says. This series closes them. Apart from declaring its
ordering, and its features when --field= changes what it records,
nothing in it requires changes to what systemd-manifest make writes
today.
The common constraint is that a manifest can be huge (think of a
manifest describing the dataset of an LLM, with tens of millions of
files) and can arrive through a pipe. So a consumer wants to process it
one record at a time, keep state bounded by the depth of the tree, and
not read it twice.
Strings and ordering
"Alphabetically" isn't defined. Collation depends on the locale and on
Unicode tables, so two tools can sort the same tree differently and both
claim to sort alphabetically. What a string is isn't defined either. A
JSON string can carry an escaped lone surrogate like "\uD800", which
some parsers refuse, some replace with U+FFFD and some keep. And systemd
refuses noncharacters like U+FFFF that other readers accept.
The first commit takes the wording Lennart suggested: strings are valid
UTF-8 after their escapes are decoded, without NUL and, as in I-JSON
2, without noncharacters. Consumers refuse invalid strings instead of
repairing them. Strings are ordered by their octets as unsigned values,
which is what strcmp() does and what make writes. For valid UTF-8 that
is code point order, so the ordering is now called the code point
ordering. The commit also defines the order of complete names, which
points out a trap: sort(1) over full paths puts "a-b" before "a/x",
which breaks the rule that the entries of a subdirectory immediately
follow it.
The ordering stays optional, as discussed in #213. But a manifest sorted
that way should now declare it with "order": "codepoint" in the root
object. That says that the names strictly increase, and it makes a
few things cheap:
every object to disk. The join in 1 keeps a file and a directory per
object in /var/tmp, about 4 GiB and 2M inodes for 1M objects. A
merge-based prototype of it needs about 5 MiB of memory for the same
input and nothing on disk.
name with the previous one. Without a known order it has to keep all
names of every open directory around.
anything, instead of failing halfway through the archive.
A consumer that relies on the declaration has to check it while reading.
That costs nothing since the check is the same comparison.
Directories
A file object can name a file several directories deep without the
directories above it being listed. Then a consumer can't tell a
directory that was never listed from one whose entries ended a while
ago, unless it remembers every directory it has seen. So, as Lennart
suggested, every file object now has to come after the file object of
its directory, which puts every manifest in tree order.
systemd-manifest make already writes that. A SHA256SUMS file with
slashes in its names now maps to a manifest with dir objects for its
directories.
Repeated names and duplicate keys
Names have to be unique, but nothing says what a consumer does when they
aren't. Today systemd's reader hands both objects to its caller, ufat
lets the later one replace the earlier one, and a verifier might check
both against the same file. IOW, the same signed manifest describes
different trees depending on who reads it. So consumers should now
refuse such manifests, and also files that don't follow the file object
of their directory.
JSON objects with the same key twice are refused outright. RFC 8259
calls the behavior of software that receives one unpredictable, and
parsers disagree about which of the values they hand out.
Limits
systemd's reader and ufat both refuse records larger than 4 MiB. Other
readers would pick other limits, and a manifest that one of them reads
fine fails in another. So the spec now says 4 MiB per record and 4095
bytes per name. That also bounds what an order check has to keep around.
Hard links
make gives a token to a file whose other names are outside the tree it
walks, so groups with a single member exist in practice. They are now
explicitly fine. All members of a group are required to carry the same
inode attributes, not just the same extended attributes and flags.
Features
A manifest records whatever its producer was asked to record, and make
takes --field= for that. So a missing field can mean that a property
isn't set or that it wasn't recorded. For hard links that makes joins
ambiguous: a missing token means "not hardlinked" in one input and
"don't know" in the other. As Lennart suggested, the root object can
now list what the manifest records in "features", using make's field
names plus one value per flag. Fields of features that aren't listed
are invalid, and tools that compare or combine manifests compare the
features first. Without the field a manifest records make's default
fields, so make's default output stays as it is.
Equivalence
Two file objects that mean the same can be written differently:
"type":"reg" or no type at all, "uid":null or no uid, an uppercase or a
lowercase digest. The join in 1 refuses all three as conflicts. Making
the JSON reproducible was rejected in #213 and isn't needed for this.
Instead, the series defines equivalence on the level of JSON values, as
Lennart suggested. Field order and escaping don't matter, null is the
same as not set, and values aren't interpreted any further, so it works
for fields a tool doesn't know as well. Generators should leave out
fields set to their default, and sha256 already has to be lowercase.
Numbers
The note on numbers says that only files of excessive sizes go beyond
2^53. But mTimeNSec is above 2^53 for every point in time after
mid-April 1970. JSON.parse() in JavaScript rounds it right away and jq
rounds it as soon as it computes anything with it. The note now says so,
and consumers have to treat these numbers as unsigned 64-bit integers.