diff --git a/src/uapi16/blocking.rs b/src/uapi16/blocking.rs new file mode 100644 index 0000000..dd26d74 --- /dev/null +++ b/src/uapi16/blocking.rs @@ -0,0 +1,739 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later +//! Reading a manifest from a [`Read`] and writing one to a [`Write`]: a +//! [`Reader`] feeds a [`Decoder`], and a [`Writer`] writes out what an +//! [`Encoder`] puts together. + +use std::io::{self, BufRead, BufReader, Read, Write}; +use std::iter::FusedIterator; + +use crate::decode::ROOT_RECORD; +use crate::error::Error; +use crate::object::{FileObject, RootObject, Signature}; +use crate::reader::{Decoder, Object, Step}; +use crate::writer::Encoder; + +/// How much is read from the input at a time. +pub(crate) const READ_SIZE: usize = 64 * 1024; +/// The most room for the text of a record that a [`Writer`] keeps from one +/// record to the next. A larger record takes more, and gives it back once +/// it went out. +const TEXT_CAPACITY: usize = 64 * 1024; + +/// Reads a manifest record by record. +/// +/// [`Reader::open`] reads the root file object, the first object of +/// every manifest. The reader is then an iterator over the file objects +/// and the signature objects that follow. It ends after the trailer +/// object. Objects of a media type the specification does not have are +/// passed over, as it asks. +/// +/// The reader keeps one record and the name of the last file object in +/// memory. It checks every object against the rules of the specification, +/// and the order of the objects: the file objects in tree order, each +/// right after the object of its directory, then signature objects, then +/// the trailer, and nothing behind it. A file object has no field of a +/// feature the manifest does not record, and the reader fills in what the +/// features say about the fields it does not have, see +/// [`Features`](crate::Features): a +/// flag the manifest records is cleared, a file is not read-only and has +/// no extended attributes. It leaves the `gpt` fields as they are, see +/// [`GptPartition`](crate::GptPartition). +/// +/// A name that comes again right after the entries of the directory it +/// names is an error, a name that comes again later only if the manifest +/// declares an order in `order`. Then every name has to come after the one +/// before it, and none can come twice. A consumer that relies on the order +/// requires the manifest to declare it with +/// [`RootObject::require_order`]. +/// +/// Once the reader returned an error it returns nothing more. An error of +/// the kind [`WouldBlock`](io::ErrorKind::WouldBlock) from the input is +/// the exception: the reader lost nothing, and reads on when it is asked +/// again. +/// +/// A manifest comes from outside and may be anything. No record is larger +/// than [`MAX_RECORD_SIZE`] with its record separator, and reading takes +/// time in proportion to the size of the input. +/// +/// ``` +/// use uapi16::{Object, Reader}; +/// +/// let manifest = concat!( +/// "\x1e{\"mediaType\":\"application/vnd.uapi.16.manifest\"}\n", +/// "\x1e{\"name\":\"a\",\"size\":3}\n", +/// "\x1e{\"name\":\"b\",\"type\":\"dir\"}\n", +/// "\x1e{\"mediaType\":\"application/vnd.uapi.16.trailer\"}\n", +/// ); +/// let mut names = Vec::new(); +/// for object in Reader::open(manifest.as_bytes())? { +/// if let Object::File(object) = object? { +/// names.push(object.name); +/// } +/// } +/// assert_eq!(names, ["a", "b"]); +/// # Ok::<(), uapi16::Error>(()) +/// ``` +/// +/// [`MAX_RECORD_SIZE`]: crate::MAX_RECORD_SIZE +#[derive(Debug)] +pub struct Reader { + input: BufReader, + decoder: Decoder, + root: RootObject, + /// Whether an error ended the reading. + failed: bool, +} + +impl Reader { + /// Starts to read a manifest from `input`, with its root file object. + /// An error of the input fails it, one of the kind + /// [`WouldBlock`](io::ErrorKind::WouldBlock) as well. The input is + /// dropped then, with what was read of it. + pub fn open(input: R) -> Result, Error> { + let mut reader = Reader { + input: BufReader::with_capacity(READ_SIZE, input), + decoder: Decoder::new(), + root: RootObject::default(), + failed: false, + }; + let Step::Root(root) = reader.pull()? else { + unreachable!("a decoder hands out the root file object first"); + }; + reader.root = *root; + Ok(reader) + } + + /// The root file object of the manifest. + pub fn root(&self) -> &RootObject { + &self.root + } + + /// What the manifest is read from. The reader reads ahead of the + /// objects it returned. It has read the input to its end once it + /// returned None after the last of them, which it does when it read + /// the trailer object. + pub fn get_ref(&self) -> &R { + self.input.get_ref() + } + + /// What the manifest is read from, to change it. Reading from it + /// directly takes bytes away from the reader, which reads ahead of the + /// objects it returned. + pub fn get_mut(&mut self) -> &mut R { + self.input.get_mut() + } + + /// The number of the record the object returned last came from. The + /// record of the root file object is number 0. Records that are empty + /// do not count. + pub fn record_number(&self) -> u64 { + self.decoder.record_number() + } + + /// How many bytes at the start of the manifest its signature objects + /// sign: all that comes before the record of the first of them, up to + /// its record separator. None as long as no signature object was read. + /// + /// This crate does not verify signatures. With this number and the + /// [`Signature`] objects a caller can. + pub fn signed_len(&self) -> Option { + self.decoder.signed_len() + } + + /// Feeds the decoder until it takes a step other than [`Step::Input`]. + fn pull(&mut self) -> Result { + loop { + match self.decoder.step()? { + Step::Input => self.fill()?, + step => return Ok(step), + } + } + } + + /// Feeds the decoder what the input has next, or tells it that the + /// input ended. An error of the input is one about the record being + /// read. + fn fill(&mut self) -> Result<(), Error> { + let input = loop { + match self.input.fill_buf() { + Err(e) if e.kind() == io::ErrorKind::Interrupted => {} + result => break result, + } + }; + let input = input.map_err(|e| self.decoder.input_error(e))?; + if input.is_empty() { + self.decoder.end(); + } else { + let taken = self.decoder.feed(input); + self.input.consume(taken); + } + Ok(()) + } +} + +impl Iterator for Reader { + type Item = Result; + + fn next(&mut self) -> Option> { + if self.failed { + return None; + } + match self.pull() { + Ok(Step::Object(object)) => Some(Ok(object)), + Ok(Step::End) => None, + // pull() feeds the decoder what it asks for, and the root file + // object came when the reader was opened. + Ok(Step::Input | Step::Root(_)) => unreachable!("a decoder hands out the root file object once"), + Err(e) => { + // An input that has nothing right now gave the decoder + // nothing, and it reads on from where it was. + self.failed = e.io_error().map(io::Error::kind) != Some(io::ErrorKind::WouldBlock); + Some(Err(e)) + } + } + } +} + +impl FusedIterator for Reader {} + +/// Writes a manifest record by record. +/// +/// [`Writer::begin`] writes the root file object, [`Writer::file`] the +/// file objects, [`Writer::signature`] the signature objects after them +/// and [`Writer::finish`] the trailer object. A manifest without its +/// trailer is one a reader takes as incomplete, so a writer that is +/// dropped before it finished leaves nothing that passes for a manifest. +/// +/// Every object is one record of the JSON text sequence: a record +/// separator, the object with its keys sorted and without whitespace, and +/// a newline, the way `systemd-manifest` writes it. A field that says +/// nothing is left out. So is the inode type where the specification +/// implies it, on the root file object and on regular files. An +/// [`Encoder`] puts each record together, and it goes out in one write, so +/// a file wants a `BufWriter` around it. +/// +/// The root file object says what the manifest records in `features` and +/// which order it lists its file objects in in `order`. The writer checks +/// every object against them as a [`Reader`] does: an object +/// has no fields of features the manifest does not record, it comes right +/// after the object of its directory, and it keeps to the order. It checks +/// what an object is itself as a reader does too: the fields its inode +/// type permits, the value of every field and every string it writes, and +/// the fields against each other. An object with more than one fault is +/// refused for the one a reader finds first. The one fault a reader does +/// not find is an encoding that is not in lower case: a reader lowers its +/// case, and the writer refuses it. No record is larger than +/// [`MAX_RECORD_SIZE`], its record separator included, and no file object +/// has more than [`MAX_CONTENTS`] contents objects. An object the writer +/// refuses is not written, and the writer goes on as if it had not been +/// given it. +/// +/// The writer leaves out what the manifest says without it: where the +/// manifest records them, a flag that is cleared, `readOnly` of false, +/// `gptFlags` of 0 and extended attributes there are none of, and +/// `features` if they are the default ones. It leaves out the hash of no +/// data next to the size 0, as the specification recommends. Without an +/// encoding the specification says that the encoded size and hash should +/// not be there. The writer leaves them out next to the original ones, and +/// writes one that is the only statement of the size or the hash: written +/// as the original size or hash it would come back as another field, and +/// left out it would be lost. The writer also leaves out every field a +/// [`Reader`] filled in, see [`FilledIn`]. So what a reader +/// returns can be written back with the root file object it read. A record +/// close to [`MAX_RECORD_SIZE`] can grow past it on the way back, as the +/// writer writes some values in another form than a manifest may have them +/// in: a UUID with its hyphens, and the value of an extended attribute as +/// text where it can. The writer refuses such a record. +/// +/// ``` +/// use uapi16::{FileObject, Name, Object, Order, Reader, RootObject, Writer}; +/// +/// let mut root = RootObject::default(); +/// root.order = Some(Order::CodePoint); +/// let mut file = FileObject::new(Name::new("a")?); +/// file.size = Some(3); +/// let mut writer = Writer::begin(Vec::new(), &root)?; +/// writer.file(&file)?; +/// let bytes = writer.finish()?; +/// assert_eq!( +/// String::from_utf8_lossy(&bytes), +/// concat!( +/// "\x1e{\"mediaType\":\"application/vnd.uapi.16.manifest\",\"order\":\"codepoint\"}\n", +/// "\x1e{\"name\":\"a\",\"size\":3}\n", +/// "\x1e{\"mediaType\":\"application/vnd.uapi.16.trailer\"}\n", +/// ) +/// ); +/// let mut reader = Reader::open(&bytes[..])?; +/// assert!(matches!(reader.next(), Some(Ok(Object::File(object))) if *object == file)); +/// # Ok::<(), Box>(()) +/// ``` +/// +/// [`FilledIn`]: crate::FilledIn +/// [`MAX_CONTENTS`]: crate::MAX_CONTENTS +/// [`MAX_RECORD_SIZE`]: crate::MAX_RECORD_SIZE +#[derive(Debug)] +pub struct Writer { + out: W, + /// The text of the record that goes out next. The room for it is kept + /// from one record to the next. + text: String, + encoder: Encoder, +} + +impl Writer { + /// Starts a manifest in `out` with the root file object `root`. + pub fn begin(out: W, root: &RootObject) -> Result, Error> { + let mut text = String::new(); + let encoder = Encoder::begin(root, &mut text)?; + let mut writer = Writer { out, text, encoder }; + writer.write_out(ROOT_RECORD)?; + Ok(writer) + } + + /// Writes the file object `file`. + pub fn file(&mut self, file: &FileObject) -> Result<(), Error> { + self.send_record(|encoder, text| encoder.file(file, text)) + } + + /// Writes a signature object. No file object can follow it: it would + /// be one the signature does not cover. + pub fn signature(&mut self, signature: &Signature) -> Result<(), Error> { + self.send_record(|encoder, text| encoder.signature(signature, text)) + } + + /// Ends the manifest with the trailer object, flushes what it was + /// written to and returns it. + pub fn finish(mut self) -> Result { + let record_number = self.encoder.next_record; + self.send_record(|encoder, text| encoder.finish(text))?; + self.out.flush().map_err(|e| Error::from(e).in_record(record_number))?; + Ok(self.out) + } + + /// What the manifest is written to. Every method writes its record to + /// it before it returns, so it has the manifest up to the last record. + /// Before the first signature object, that is what the signature + /// objects sign. + pub fn get_ref(&self) -> &W { + &self.out + } + + /// Writes out the record that `encode` has the encoder append to the + /// text. When the encoder took it back as too large, the text gives + /// back the room it took. + fn send_record( + &mut self, + encode: impl FnOnce(&mut Encoder, &mut String) -> Result<(), Error>, + ) -> Result<(), Error> { + let record_number = self.encoder.next_record; + match encode(&mut self.encoder, &mut self.text) { + Ok(()) => self.write_out(record_number), + Err(e) => { + self.text.shrink_to(TEXT_CAPACITY); + Err(e) + } + } + } + + /// Writes out the text, the record with the number `record_number`, in + /// one write, and empties it for the next record. A record larger than + /// [`TEXT_CAPACITY`] gives back the room it took beyond that. + fn write_out(&mut self, record_number: u64) -> Result<(), Error> { + let written = self.out.write_all(self.text.as_bytes()); + self.text.clear(); + self.text.shrink_to(TEXT_CAPACITY); + written.map_err(|e| Error::from(e).in_record(record_number)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::error::{ErrorKind, Problem}; + use crate::testing::{ + directory, everything, manifest, name, read_whole, read_whole_from, record, recording_all, sequence, + write_whole, Whole, EMPTY, PIECES, ROOT, SIGNATURE, TRAILER, + }; + use crate::{InodeType, Name, Sha256, MAX_RECORD_SIZE, RECORD_SEPARATOR}; + use std::collections::BTreeMap; + + #[test] + fn the_reader_as_an_iterator() { + let bytes = sequence(&[ + ROOT, + " ", + r#"{"name":"a","size":0}"#, + "\n\n", + r#"{"mediaType":"application/x-other"}"#, + r#"{"name":"b","type":"dir"}"#, + SIGNATURE, + TRAILER, + ]); + let mut reader = Reader::open(&bytes[..]).unwrap(); + assert_eq!(reader.root(), &RootObject::default()); + assert_eq!(reader.record_number(), 0); + // Records of whitespace do not count, objects that are passed over + // do. + assert!(matches!(reader.next(), Some(Ok(Object::File(object))) if object.name == "a")); + assert_eq!(reader.record_number(), 1); + assert!(matches!(reader.next(), Some(Ok(Object::File(object))) if object.name == "b")); + assert_eq!(reader.record_number(), 3); + assert_eq!(reader.signed_len(), None); + assert!(matches!(reader.next(), Some(Ok(Object::Signature(signature))) if signature.mechanism == "pkcs7")); + assert_eq!(reader.record_number(), 4); + assert!(reader.next().is_none()); + assert!(reader.next().is_none()); + assert_eq!(reader.record_number(), 4); + // The input is read to its end once None was returned. + assert!(reader.get_ref().is_empty()); + // What is signed ends in front of the first signature object. + let signed = sequence(&[ + ROOT, + " ", + r#"{"name":"a","size":0}"#, + "\n\n", + r#"{"mediaType":"application/x-other"}"#, + r#"{"name":"b","type":"dir"}"#, + ]); + assert_eq!(reader.signed_len(), Some(signed.len() as u64)); + assert_eq!( + &bytes[signed.len()..][..1 + SIGNATURE.len()], + [&[RECORD_SEPARATOR][..], SIGNATURE.as_bytes()].concat() + ); + // After an error there is nothing more. + let bytes = manifest(&[r#"{"name":"a"}"#, r#"{"name":"b","size":"x"}"#, r#"{"name":"c"}"#]); + let mut reader = Reader::open(&bytes[..]).unwrap(); + assert!(matches!(reader.next(), Some(Ok(_)))); + let e = reader.next().unwrap().unwrap_err(); + assert_eq!((e.record_number(), e.name()), (Some(2), Some("b"))); + assert!(reader.next().is_none()); + assert!(reader.next().is_none()); + } + + /// Hands out the bytes of a manifest a few at a time, and says now + /// and then that a signal interrupted the read. + #[derive(Debug)] + struct Trickle<'a> { + bytes: &'a [u8], + step: usize, + calls: usize, + } + + impl Read for Trickle<'_> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + self.calls += 1; + if self.calls % 3 == 0 { + return Err(io::ErrorKind::Interrupted.into()); + } + let n = self.step.min(buf.len()).min(self.bytes.len()); + buf[..n].copy_from_slice(&self.bytes[..n]); + self.bytes = &self.bytes[n..]; + Ok(n) + } + } + + #[test] + fn input_in_pieces() { + let long = format!(r#"{{"name":"long","size":0,"pad":"{}"}}"#, "p".repeat(3 * READ_SIZE)); + let bytes = manifest(&[r#"{"name":"a","size":0}"#, &long, r#"{"name":"é","type":"dir"}"#]); + let whole = read_whole(&bytes).unwrap(); + assert_eq!(whole.objects.len(), 3); + assert_eq!(whole.objects[2].name, Name::new("\u{e9}").unwrap()); + // The pieces end anywhere: inside a record, inside a character, + // right in front of a separator. + for step in PIECES { + let trickle = Trickle { + bytes: &bytes, + step, + calls: 0, + }; + assert_eq!(read_whole_from(trickle).unwrap(), whole, "{} at a time", step); + } + // A NUL byte spoils the record it is in and none before it, and + // where the signed part ends does not depend on the pieces. + let mut bytes = sequence(&[ROOT, r#"{"name":"a"}"#, &long, r#"{"name":"b"}"#, SIGNATURE, SIGNATURE]); + let signed = bytes.len() - 2 * (SIGNATURE.len() + 2); + bytes.push(RECORD_SEPARATOR); + bytes.extend(b"{\"mediaType\":\"application/x-other\0\"}\n"); + bytes.extend(sequence(&[TRAILER])); + for step in [1, 7, READ_SIZE, 5 * READ_SIZE] { + let mut reader = Reader::open(Trickle { + bytes: &bytes, + step, + calls: 0, + }) + .unwrap(); + for name in ["a", "long", "b"] { + assert!(matches!(reader.next(), Some(Ok(Object::File(object))) if object.name == name)); + } + assert_eq!(reader.signed_len(), None); + for _ in 0..2 { + assert!(matches!(reader.next(), Some(Ok(Object::Signature(_))))); + assert_eq!(reader.signed_len(), Some(signed as u64), "{} at a time", step); + } + let e = reader.next().unwrap().unwrap_err(); + assert_eq!(e.kind(), ErrorKind::Manifest(Problem::ContainsNulByte)); + assert_eq!(e.record_number(), Some(6)); + assert!(reader.next().is_none()); + } + } + + /// Fails every read. + #[derive(Debug)] + struct FaultyInput; + + impl Read for FaultyInput { + fn read(&mut self, _buf: &mut [u8]) -> io::Result { + Err(io::Error::from_raw_os_error(libc::EIO)) + } + } + + #[test] + fn a_read_that_fails() { + let e = Reader::open(FaultyInput).unwrap_err(); + assert_eq!(e.kind(), ErrorKind::Io); + assert_eq!(e.io_error().and_then(io::Error::raw_os_error), Some(libc::EIO)); + // Later on too. A record ends where the next one starts, so the + // first one is read whole when the second one begins. + let bytes = sequence(&[ROOT, r#"{"name":"a"}"#]); + let mut reader = Reader::open(bytes.chain(FaultyInput)).unwrap(); + let e = reader.next().unwrap().unwrap_err(); + assert_eq!(e.kind(), ErrorKind::Io); + assert!(reader.next().is_none()); + } + + /// Hands out the bytes of a manifest `step` at a time. With `blocking` + /// it says before every piece that it has nothing yet, as a pipe that + /// does not block does while its writer is slow. + #[derive(Debug)] + struct Hiccups<'a> { + bytes: &'a [u8], + step: usize, + blocking: bool, + /// Whether it said so before the next piece. + said: bool, + } + + impl Read for Hiccups<'_> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + if self.blocking && !self.said { + self.said = true; + return Err(io::ErrorKind::WouldBlock.into()); + } + self.said = false; + let n = self.step.min(buf.len()).min(self.bytes.len()); + buf[..n].copy_from_slice(&self.bytes[..n]); + self.bytes = &self.bytes[n..]; + Ok(n) + } + } + + #[test] + fn reading_on_after_would_block() { + let long = format!(r#"{{"name":"long","size":0,"pad":"{}"}}"#, "p".repeat(3 * READ_SIZE)); + let bytes = sequence(&[ + ROOT, + r#"{"name":"a","size":0}"#, + &long, + r#"{"name":"é","type":"dir"}"#, + SIGNATURE, + TRAILER, + ]); + let whole = read_whole(&bytes).unwrap(); + // The number of the record of every object, and how much is signed + // after it, as a reader says that never sees a WouldBlock. + let mut unblocked = Vec::new(); + let mut reader = Reader::open(&bytes[..]).unwrap(); + while let Some(object) = reader.next() { + object.unwrap(); + unblocked.push((reader.record_number(), reader.signed_len())); + } + let would_block = |e: &Error| e.io_error().map(io::Error::kind) == Some(io::ErrorKind::WouldBlock); + for step in [1, 7, 4096, READ_SIZE + 1] { + let hiccups = Hiccups { + bytes: &bytes, + step, + blocking: false, + said: false, + }; + let mut reader = Reader::open(hiccups).unwrap(); + // From here on the input has nothing every other time it is + // read, in the middle of records as well. + reader.get_mut().blocking = true; + let mut read = Whole { + root: reader.root().clone(), + objects: Vec::new(), + signatures: Vec::new(), + }; + let mut blocked = 0; + let mut numbers = Vec::new(); + let mut last = (reader.record_number(), reader.signed_len()); + while let Some(object) = reader.next() { + let now = (reader.record_number(), reader.signed_len()); + match object { + Ok(Object::File(object)) => read.objects.push(*object), + Ok(Object::Signature(signature)) => read.signatures.push(signature), + // The error is about the record being read, and the + // reader says what it said before it. + Err(e) if would_block(&e) => { + assert_eq!(e.record_number(), Some(last.0 + 1), "{} at a time", step); + assert_eq!(now, last, "{} at a time", step); + blocked += 1; + continue; + } + Err(e) => panic!("{} at a time: {}", step, e), + } + numbers.push(now); + last = now; + } + assert_eq!(read, whole, "{} at a time", step); + assert_eq!(numbers, unblocked, "{} at a time", step); + assert!(blocked > 0, "{} at a time", step); + assert!(reader.next().is_none()); + } + // Reader::open() has no reader to go on with. + let hiccups = Hiccups { + bytes: &bytes, + step: 7, + blocking: true, + said: false, + }; + assert!(would_block(&Reader::open(hiccups).unwrap_err())); + } + + /// No record is larger than MAX_RECORD_SIZE, its record separator + /// included, which is how a reader counts it too. A larger one is not + /// written, and the writer goes on. + #[test] + fn the_largest_record() { + // An object whose record is as large as one with the name `name_of` + // and an extended attribute, plus `pad` bytes. + let padded = |name_of: &str, pad: usize| { + let mut object = FileObject::new(name(name_of)); + object.inode.user_xattrs = Some(Box::new(BTreeMap::from([("p".to_string(), vec![b'x'; pad])]))); + object + }; + // The separator and the newline are not in what record() returns. + let frame = record(&padded("a", 0)).len() + 2; + let (root, _) = recording_all(); + let mut writer = Writer::begin(Vec::new(), &root).unwrap(); + let before = writer.get_ref().len(); + let e = writer.file(&padded("a", MAX_RECORD_SIZE - frame + 1)).unwrap_err(); + assert_eq!( + (e.kind(), e.record_number(), e.name()), + (ErrorKind::Manifest(Problem::RecordTooLarge), Some(1), Some("a")) + ); + assert_eq!( + e.to_string(), + format!("manifest record exceeds {} bytes", MAX_RECORD_SIZE) + ); + assert_eq!(writer.get_ref().len(), before); + assert!(writer.text.capacity() <= TEXT_CAPACITY); + // A directory that is not written is not the directory of what + // comes next. + let mut large = padded("d", MAX_RECORD_SIZE); + large.inode_type = InodeType::Directory; + let e = writer.file(&large).unwrap_err(); + assert_eq!(e.kind(), ErrorKind::Manifest(Problem::RecordTooLarge)); + let e = writer.file(&FileObject::new(name("d/x"))).unwrap_err(); + assert_eq!(e.kind(), ErrorKind::Manifest(Problem::Misplaced)); + // The largest one, which a reader takes. + let largest = padded("a", MAX_RECORD_SIZE - frame); + writer.file(&largest).unwrap(); + assert_eq!(writer.get_ref().len() - before, MAX_RECORD_SIZE); + assert!(writer.text.capacity() <= TEXT_CAPACITY); + let mut signature = Signature::new("pkcs7"); + signature.data = Some(vec![0; MAX_RECORD_SIZE]); + let e = writer.signature(&signature).unwrap_err(); + assert_eq!( + (e.kind(), e.record_number()), + (ErrorKind::Manifest(Problem::RecordTooLarge), Some(2)) + ); + let bytes = writer.finish().unwrap(); + let m = read_whole(&bytes).unwrap(); + assert_eq!(m.objects.len(), 1); + assert_eq!(m.objects[0].inode.user_xattrs, largest.inode.user_xattrs); + // What a reader read, the writer writes back. + assert_eq!(write_whole(&m).unwrap(), bytes); + // The root file object's record is record 0. + let mut root = recording_all().0; + root.inode.user_xattrs = padded("a", MAX_RECORD_SIZE).inode.user_xattrs; + let e = Writer::begin(Vec::new(), &root).unwrap_err(); + assert_eq!( + (e.kind(), e.record_number()), + (ErrorKind::Manifest(Problem::RecordTooLarge), Some(0)) + ); + } + + /// What was written so far is there to be signed: the signature + /// objects sign what comes before the first of them. + #[test] + fn what_was_written_can_be_signed() { + let mut writer = Writer::begin(Vec::new(), &RootObject::default()).unwrap(); + writer.file(&FileObject::new(name("a"))).unwrap(); + let signed = writer.get_ref().len(); + let hash = Sha256::digest(writer.get_ref()); + let mut signature = Signature::new("pkcs7"); + signature.data = Some(hash.as_bytes().to_vec()); + writer.signature(&signature).unwrap(); + let bytes = writer.finish().unwrap(); + let mut reader = Reader::open(&bytes[..]).unwrap(); + let objects: Vec<_> = reader.by_ref().collect::>().unwrap(); + assert_eq!(objects.len(), 2); + assert_eq!(reader.signed_len(), u64::try_from(signed).ok()); + assert_eq!(Sha256::digest(&bytes[..signed]), hash); + } + + /// Notes every write and every flush, and fails the write `fail_at`. + #[derive(Debug, Default)] + struct Calls { + writes: Vec, + flushes: usize, + fail_at: Option, + } + + impl Write for Calls { + fn write(&mut self, data: &[u8]) -> io::Result { + if self.fail_at == Some(self.writes.len()) { + return Err(io::Error::from_raw_os_error(libc::ENOSPC)); + } + self.writes.push(data.len()); + Ok(data.len()) + } + + fn flush(&mut self) -> io::Result<()> { + self.flushes += 1; + Ok(()) + } + } + + #[test] + fn a_record_is_one_write() { + let mut calls = Calls::default(); + let (root, root_text) = recording_all(); + let mut writer = Writer::begin(&mut calls, &root).unwrap(); + writer.file(&directory("a")).unwrap(); + writer.file(&everything()).unwrap(); + let mut empty = FileObject::new(name("empty")); + empty.sha256 = EMPTY.parse().ok(); + writer.file(&empty).unwrap(); + writer.finish().unwrap(); + assert_eq!(calls.writes.len(), 5); + assert_eq!(calls.writes[0], sequence(&[root_text.as_str()]).len()); + assert_eq!(calls.writes[4], sequence(&[TRAILER]).len()); + assert_eq!(calls.flushes, 1); + // An error of what is written to is the writer's, about the record + // it wrote. + for (fail_at, record_number) in [(0, 0), (1, 1)] { + let mut calls = Calls { + fail_at: Some(fail_at), + ..Calls::default() + }; + let e = Writer::begin(&mut calls, &RootObject::default()) + .and_then(|mut writer| writer.file(&FileObject::new(name("a")))) + .unwrap_err(); + assert_eq!((e.kind(), e.record_number()), (ErrorKind::Io, Some(record_number))); + assert_eq!(e.io_error().and_then(io::Error::raw_os_error), Some(libc::ENOSPC)); + } + } +} diff --git a/src/uapi16/lib.rs b/src/uapi16/lib.rs index c49dd48..0eb7fc6 100644 --- a/src/uapi16/lib.rs +++ b/src/uapi16/lib.rs @@ -77,6 +77,7 @@ #![warn(missing_docs)] mod base64; +mod blocking; mod content; mod decode; mod digest; @@ -89,6 +90,7 @@ mod reader; mod sequence; mod writer; +pub use blocking::{Reader, Writer}; pub use content::{ContentCheck, ContentSource, Data, Directory, DirectorySource, OpenError, Reference, StreamSource}; pub use digest::{Digest, Finalize, InvalidDigest, Sha256, Sha256Context}; pub use error::{ContentProblem, Error, ErrorKind, Field, Invalid, Problem, SourceProblem, UnsupportedContent}; @@ -99,8 +101,8 @@ pub use object::{ InodeFlags, InodeType, InvalidFatSerial, InvalidUuid, Location, Mode, RootObject, Signature, UnknownInodeType, Uuid, Validity, ValidityWindow, }; -pub use reader::{Decoder, Object, Reader, Step}; -pub use writer::{Encoder, Writer}; +pub use reader::{Decoder, Object, Step}; +pub use writer::Encoder; /// The media type of a manifest, which its first object carries. pub const MEDIA_TYPE: &str = "application/vnd.uapi.16.manifest"; @@ -135,7 +137,13 @@ pub(crate) const STRING_RULES: json::StringRules = json::StringRules::NO_NONCHAR /// What the tests of this crate build manifests from. #[cfg(test)] pub(crate) mod testing { - use super::{Error, FileObject, Name, Object, Reader, RootObject, Signature, Writer, RECORD_END, RECORD_SEPARATOR}; + use super::blocking::READ_SIZE; + use super::{ + Contents, DosFlags, Error, Features, FileObject, GptPartition, InodeFlags, InodeType, Location, Mode, Name, + Object, Reader, RootObject, Sha256, Signature, Uuid, Validity, Writer, RECORD_END, RECORD_SEPARATOR, + }; + use std::collections::BTreeMap; + use std::num::NonZeroU64; /// The root file object that says nothing but that this is a manifest. pub(crate) const ROOT: &str = r#"{"mediaType":"application/vnd.uapi.16.manifest"}"#; @@ -150,6 +158,10 @@ pub(crate) mod testing { pub(crate) const WORLD: &str = "486ea46224d1bb4fb680f34f7c9ad96a8f24ec88be73ea8e5a6c65260e9cb8a7"; /// The hash of no data, in hex. pub(crate) const EMPTY: &str = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; + /// How many bytes the tests feed a decoder at a time. The pieces end + /// anywhere: inside a record, inside a character, right in front of a + /// separator, and around the size a reader reads at a time. + pub(crate) const PIECES: [usize; 8] = [1, 2, 3, 7, 4096, READ_SIZE - 1, READ_SIZE, READ_SIZE + 1]; /// The JSON text sequence of `records`. pub(crate) fn sequence(records: &[&str]) -> Vec { @@ -225,4 +237,136 @@ pub(crate) mod testing { } writer.finish() } + + pub(crate) fn name(name: &str) -> Name { + Name::new(name).unwrap() + } + + /// The records `writer` wrote after `root`, without the frame of + /// each: the separator in front and the newline behind. + pub(crate) fn written(root: &RootObject, write: impl FnOnce(&mut Writer>)) -> Vec { + let mut writer = Writer::begin(Vec::new(), root).unwrap(); + write(&mut writer); + let text = String::from_utf8(writer.finish().unwrap()).unwrap(); + let separator = char::from(RECORD_SEPARATOR); + assert!(text.starts_with(separator)); + text.split(separator) + .skip(1) + .map(|record| record.strip_suffix(char::from(RECORD_END)).unwrap().to_string()) + .collect() + } + + /// The root file object of a manifest that records every feature, and + /// its record. + pub(crate) fn recording_all() -> (RootObject, String) { + let root = RootObject { + features: Some(Features::ALL), + ..RootObject::default() + }; + let names: Vec = Features::ALL.names().map(|name| format!("\"{}\"", name)).collect(); + let text = format!( + r#"{{"features":[{}],"mediaType":"application/vnd.uapi.16.manifest"}}"#, + names.join(",") + ); + (root, text) + } + + /// A directory that says nothing else about itself. + pub(crate) fn directory(name_of: &str) -> FileObject { + let mut directory = FileObject::new(name(name_of)); + directory.inode_type = InodeType::Directory; + directory + } + + /// The record the writer writes for `object`, in a manifest that + /// records every feature, after the objects of the directories it is + /// in. + pub(crate) fn record(object: &FileObject) -> String { + let (root, root_text) = recording_all(); + let directories: Vec = object + .name + .match_indices('/') + .map(|(end, _)| directory(&object.name[..end])) + .collect(); + let mut records = written(&root, |writer| { + for directory in &directories { + writer.file(directory).unwrap(); + } + writer.file(object).unwrap(); + }); + assert_eq!(records.len(), directories.len() + 3); + assert_eq!(records[0], root_text); + assert_eq!(records.pop().as_deref(), Some(TRAILER)); + records.pop().unwrap() + } + + /// An object with every field there is, and one of its contents + /// objects with every field. + pub(crate) fn everything() -> FileObject { + let uuid = |last: u8| { + let mut bytes = [0x11; Uuid::SIZE]; + bytes[Uuid::SIZE - 1] = last; + Uuid::new(bytes) + }; + let mut encoded = Contents::new(Location::File(name("blob.gz"))); + encoded.encoding = Some("gzip".into()); + encoded.encoded_size = Some(3); + encoded.encoded_sha256 = Some(Sha256::digest(b"gz")); + encoded.original_size = Some(9); + encoded.original_sha256 = Some(Sha256::digest(b"original")); + encoded.offset = 4; + let mut object = FileObject::new(name("a/\u{e9} \"quoted\"\\")); + object.size = Some(5); + object.inode.mode = Mode::new(0o440); + object.inode.uid = Some(1); + object.inode.gid = Some(2); + object.inode.user_name = Some("user".into()); + object.inode.group_name = Some("group".into()); + object.inode.mtime_nsec = Some(u64::MAX); + object.inode_token = NonZeroU64::new(3); + object.inode.user_xattrs = Some(Box::new(BTreeMap::from([ + ("text".to_string(), b"value".to_vec()), + ("empty".to_string(), Vec::new()), + ("binary".to_string(), vec![0xff, 0x00]), + ("nul".to_string(), b"a\0b".to_vec()), + ("noncharacter".to_string(), "a\u{fffe}".as_bytes().to_vec()), + ]))); + // Flags of both kinds, which mix in the order of their names. + object.inode.flags.put(InodeFlags::APPEND_ONLY, Some(true)); + object.dos_flags.put(DosFlags::ARCHIVE, Some(false)); + object.dos_flags.put(DosFlags::HIDDEN, Some(true)); + object.inode.flags.put(InodeFlags::IMMUTABLE, Some(true)); + object.inode.flags.put(InodeFlags::NO_COW, Some(false)); + object.dos_flags.put(DosFlags::SYSTEM, Some(true)); + // The partition is read-only, which gptFlags says as well where the + // manifest records both. + object.gpt = Some(Box::new(GptPartition { + label: Some("label".into()), + uuid: Some(uuid(1)), + type_uuid: Some(uuid(2)), + flags: Some( + GptPartition::FLAG_NO_AUTO | GptPartition::FLAG_GROW_FILE_SYSTEM | GptPartition::FLAG_READ_ONLY, + ), + no_auto: Some(true), + grow_file_system: Some(true), + })); + object.inode.read_only = Some(true); + object.contents = vec![ + encoded, + Contents { + http_headers: vec![ + ("X-Foo".into(), "bar".into()), + ("Authorization".into(), "Bearer x".into()), + ], + ..Contents::new(Location::Url("https://example.com/blob".into())) + }, + Contents::new(Location::Literal(b"hello".to_vec())), + Contents::default(), + ]; + object.sha256 = HELLO.parse().ok(); + object.validity.since_usec = 6; + object.validity.until_usec = Some(7); + object.validity.state = Validity::Revoked; + object + } } diff --git a/src/uapi16/reader.rs b/src/uapi16/reader.rs index bb4b6fa..b1a9d2b 100644 --- a/src/uapi16/reader.rs +++ b/src/uapi16/reader.rs @@ -2,8 +2,7 @@ //! Reading a manifest record by record. use std::fmt; -use std::io::{self, BufRead, BufReader, Read}; -use std::iter::FusedIterator; +use std::io::{self, BufRead}; use json::is_whitespace; @@ -13,8 +12,6 @@ use crate::object::{FileObject, RootObject, Signature}; use crate::sequence::SequenceCheck; use crate::{MAX_RECORD_SIZE, RECORD_END, RECORD_SEPARATOR}; -/// How much is read from the input at a time. -const READ_SIZE: usize = 64 * 1024; /// The most bytes a record has behind its record separator, which counts /// as a part of the record. const MAX_RECORD_TEXT: usize = MAX_RECORD_SIZE - size_of_val(&RECORD_SEPARATOR); @@ -228,6 +225,8 @@ impl fmt::Debug for RecordSplitter { /// An object of a manifest after its root file object, as a [`Reader`] /// and a [`Decoder`] hand it out. +/// +/// [`Reader`]: crate::Reader #[derive(Clone, Debug, PartialEq, Eq)] #[non_exhaustive] pub enum Object { @@ -314,6 +313,9 @@ pub enum Step { /// assert_eq!(names, ["a", "b"]); /// # Ok::<(), uapi16::Error>(()) /// ``` +/// +/// [`Read`]: std::io::Read +/// [`Reader`]: crate::Reader #[derive(Debug)] pub struct Decoder { splitter: RecordSplitter, @@ -392,6 +394,8 @@ impl Decoder { /// How many bytes at the start of the manifest its signature objects /// sign, see [`Reader::signed_len`]. + /// + /// [`Reader::signed_len`]: crate::Reader::signed_len pub fn signed_len(&self) -> Option { self.signed } @@ -474,188 +478,14 @@ impl Decoder { } } -/// Reads a manifest record by record. -/// -/// [`Reader::open`] reads the root file object, the first object of -/// every manifest. The reader is then an iterator over the file objects -/// and the signature objects that follow. It ends after the trailer -/// object. Objects of a media type the specification does not have are -/// passed over, as it asks. -/// -/// The reader keeps one record and the name of the last file object in -/// memory. It checks every object against the rules of the specification, -/// and the order of the objects: the file objects in tree order, each -/// right after the object of its directory, then signature objects, then -/// the trailer, and nothing behind it. A file object has no field of a -/// feature the manifest does not record, and the reader fills in what the -/// features say about the fields it does not have, see -/// [`Features`](crate::Features): a -/// flag the manifest records is cleared, a file is not read-only and has -/// no extended attributes. It leaves the `gpt` fields as they are, see -/// [`GptPartition`](crate::GptPartition). -/// -/// A name that comes again right after the entries of the directory it -/// names is an error, a name that comes again later only if the manifest -/// declares an order in `order`. Then every name has to come after the one -/// before it, and none can come twice. A consumer that relies on the order -/// requires the manifest to declare it with -/// [`RootObject::require_order`]. -/// -/// Once the reader returned an error it returns nothing more. An error of -/// the kind [`WouldBlock`](io::ErrorKind::WouldBlock) from the input is -/// the exception: the reader lost nothing, and reads on when it is asked -/// again. -/// -/// A manifest comes from outside and may be anything. No record is larger -/// than [`MAX_RECORD_SIZE`] with its record separator, and reading takes -/// time in proportion to the size of the input. -/// -/// ``` -/// use uapi16::{Object, Reader}; -/// -/// let manifest = concat!( -/// "\x1e{\"mediaType\":\"application/vnd.uapi.16.manifest\"}\n", -/// "\x1e{\"name\":\"a\",\"size\":3}\n", -/// "\x1e{\"name\":\"b\",\"type\":\"dir\"}\n", -/// "\x1e{\"mediaType\":\"application/vnd.uapi.16.trailer\"}\n", -/// ); -/// let mut names = Vec::new(); -/// for object in Reader::open(manifest.as_bytes())? { -/// if let Object::File(object) = object? { -/// names.push(object.name); -/// } -/// } -/// assert_eq!(names, ["a", "b"]); -/// # Ok::<(), uapi16::Error>(()) -/// ``` -#[derive(Debug)] -pub struct Reader { - input: BufReader, - decoder: Decoder, - root: RootObject, - /// Whether an error ended the reading. - failed: bool, -} - -impl Reader { - /// Starts to read a manifest from `input`, with its root file object. - /// An error of the input fails it, one of the kind - /// [`WouldBlock`](io::ErrorKind::WouldBlock) as well. The input is - /// dropped then, with what was read of it. - pub fn open(input: R) -> Result, Error> { - let mut reader = Reader { - input: BufReader::with_capacity(READ_SIZE, input), - decoder: Decoder::new(), - root: RootObject::default(), - failed: false, - }; - let Step::Root(root) = reader.pull()? else { - unreachable!("a decoder hands out the root file object first"); - }; - reader.root = *root; - Ok(reader) - } - - /// The root file object of the manifest. - pub fn root(&self) -> &RootObject { - &self.root - } - - /// What the manifest is read from. The reader reads ahead of the - /// objects it returned. It has read the input to its end once it - /// returned None after the last of them, which it does when it read - /// the trailer object. - pub fn get_ref(&self) -> &R { - self.input.get_ref() - } - - /// What the manifest is read from, to change it. Reading from it - /// directly takes bytes away from the reader, which reads ahead of the - /// objects it returned. - pub fn get_mut(&mut self) -> &mut R { - self.input.get_mut() - } - - /// The number of the record the object returned last came from. The - /// record of the root file object is number 0. Records that are empty - /// do not count. - pub fn record_number(&self) -> u64 { - self.decoder.record_number() - } - - /// How many bytes at the start of the manifest its signature objects - /// sign: all that comes before the record of the first of them, up to - /// its record separator. None as long as no signature object was read. - /// - /// This crate does not verify signatures. With this number and the - /// [`Signature`] objects a caller can. - pub fn signed_len(&self) -> Option { - self.decoder.signed_len() - } - - /// Feeds the decoder until it takes a step other than [`Step::Input`]. - fn pull(&mut self) -> Result { - loop { - match self.decoder.step()? { - Step::Input => self.fill()?, - step => return Ok(step), - } - } - } - - /// Feeds the decoder what the input has next, or tells it that the - /// input ended. An error of the input is one about the record being - /// read. - fn fill(&mut self) -> Result<(), Error> { - let input = loop { - match self.input.fill_buf() { - Err(e) if e.kind() == io::ErrorKind::Interrupted => {} - result => break result, - } - }; - let input = input.map_err(|e| self.decoder.input_error(e))?; - if input.is_empty() { - self.decoder.end(); - } else { - let taken = self.decoder.feed(input); - self.input.consume(taken); - } - Ok(()) - } -} - -impl Iterator for Reader { - type Item = Result; - - fn next(&mut self) -> Option> { - if self.failed { - return None; - } - match self.pull() { - Ok(Step::Object(object)) => Some(Ok(object)), - Ok(Step::End) => None, - // pull() feeds the decoder what it asks for, and the root file - // object came when the reader was opened. - Ok(Step::Input | Step::Root(_)) => unreachable!("a decoder hands out the root file object once"), - Err(e) => { - // An input that has nothing right now gave the decoder - // nothing, and it reads on from where it was. - self.failed = e.io_error().map(io::Error::kind) != Some(io::ErrorKind::WouldBlock); - Some(Err(e)) - } - } - } -} - -impl FusedIterator for Reader {} - #[cfg(test)] mod tests { use super::*; + use crate::blocking::READ_SIZE; use crate::error::{ErrorKind, Field}; use crate::testing::{manifest, sequence, ROOT, SIGNATURE, TRAILER}; - use crate::testing::{read_whole, read_whole_from, Whole, HELLO}; - use crate::{Digest, DosFlags, Features, FlagStates, InodeFlags, InodeType, Name, Order, RootObject, Sha256}; + use crate::testing::{read_whole, Whole, HELLO, PIECES}; + use crate::{Digest, DosFlags, Features, FlagStates, InodeFlags, InodeType, Order, Reader, RootObject, Sha256}; use std::collections::BTreeMap; /// Reads `bytes` to the end and returns the names of the file objects @@ -1154,156 +984,6 @@ mod tests { } } - #[test] - fn the_reader_as_an_iterator() { - let bytes = sequence(&[ - ROOT, - " ", - r#"{"name":"a","size":0}"#, - "\n\n", - r#"{"mediaType":"application/x-other"}"#, - r#"{"name":"b","type":"dir"}"#, - SIGNATURE, - TRAILER, - ]); - let mut reader = Reader::open(&bytes[..]).unwrap(); - assert_eq!(reader.root(), &RootObject::default()); - assert_eq!(reader.record_number(), 0); - // Records of whitespace do not count, objects that are passed over - // do. - assert!(matches!(reader.next(), Some(Ok(Object::File(object))) if object.name == "a")); - assert_eq!(reader.record_number(), 1); - assert!(matches!(reader.next(), Some(Ok(Object::File(object))) if object.name == "b")); - assert_eq!(reader.record_number(), 3); - assert_eq!(reader.signed_len(), None); - assert!(matches!(reader.next(), Some(Ok(Object::Signature(signature))) if signature.mechanism == "pkcs7")); - assert_eq!(reader.record_number(), 4); - assert!(reader.next().is_none()); - assert!(reader.next().is_none()); - assert_eq!(reader.record_number(), 4); - // The input is read to its end once None was returned. - assert!(reader.get_ref().is_empty()); - // What is signed ends in front of the first signature object. - let signed = sequence(&[ - ROOT, - " ", - r#"{"name":"a","size":0}"#, - "\n\n", - r#"{"mediaType":"application/x-other"}"#, - r#"{"name":"b","type":"dir"}"#, - ]); - assert_eq!(reader.signed_len(), Some(signed.len() as u64)); - assert_eq!( - &bytes[signed.len()..][..1 + SIGNATURE.len()], - [&[RECORD_SEPARATOR][..], SIGNATURE.as_bytes()].concat() - ); - // After an error there is nothing more. - let bytes = manifest(&[r#"{"name":"a"}"#, r#"{"name":"b","size":"x"}"#, r#"{"name":"c"}"#]); - let mut reader = Reader::open(&bytes[..]).unwrap(); - assert!(matches!(reader.next(), Some(Ok(_)))); - let e = reader.next().unwrap().unwrap_err(); - assert_eq!((e.record_number(), e.name()), (Some(2), Some("b"))); - assert!(reader.next().is_none()); - assert!(reader.next().is_none()); - } - - /// Hands out the bytes of a manifest a few at a time, and says now - /// and then that a signal interrupted the read. - #[derive(Debug)] - struct Trickle<'a> { - bytes: &'a [u8], - step: usize, - calls: usize, - } - - impl Read for Trickle<'_> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - self.calls += 1; - if self.calls % 3 == 0 { - return Err(io::ErrorKind::Interrupted.into()); - } - let n = self.step.min(buf.len()).min(self.bytes.len()); - buf[..n].copy_from_slice(&self.bytes[..n]); - self.bytes = &self.bytes[n..]; - Ok(n) - } - } - - #[test] - fn input_in_pieces() { - let long = format!(r#"{{"name":"long","size":0,"pad":"{}"}}"#, "p".repeat(3 * READ_SIZE)); - let bytes = manifest(&[r#"{"name":"a","size":0}"#, &long, r#"{"name":"é","type":"dir"}"#]); - let whole = read_whole(&bytes).unwrap(); - assert_eq!(whole.objects.len(), 3); - assert_eq!(whole.objects[2].name, Name::new("\u{e9}").unwrap()); - // The pieces end anywhere: inside a record, inside a character, - // right in front of a separator. - for step in PIECES { - let trickle = Trickle { - bytes: &bytes, - step, - calls: 0, - }; - assert_eq!(read_whole_from(trickle).unwrap(), whole, "{} at a time", step); - } - // A NUL byte spoils the record it is in and none before it, and - // where the signed part ends does not depend on the pieces. - let mut bytes = sequence(&[ROOT, r#"{"name":"a"}"#, &long, r#"{"name":"b"}"#, SIGNATURE, SIGNATURE]); - let signed = bytes.len() - 2 * (SIGNATURE.len() + 2); - bytes.push(RECORD_SEPARATOR); - bytes.extend(b"{\"mediaType\":\"application/x-other\0\"}\n"); - bytes.extend(sequence(&[TRAILER])); - for step in [1, 7, READ_SIZE, 5 * READ_SIZE] { - let mut reader = Reader::open(Trickle { - bytes: &bytes, - step, - calls: 0, - }) - .unwrap(); - for name in ["a", "long", "b"] { - assert!(matches!(reader.next(), Some(Ok(Object::File(object))) if object.name == name)); - } - assert_eq!(reader.signed_len(), None); - for _ in 0..2 { - assert!(matches!(reader.next(), Some(Ok(Object::Signature(_))))); - assert_eq!(reader.signed_len(), Some(signed as u64), "{} at a time", step); - } - let e = reader.next().unwrap().unwrap_err(); - assert_eq!(e.kind(), ErrorKind::Manifest(Problem::ContainsNulByte)); - assert_eq!(e.record_number(), Some(6)); - assert!(reader.next().is_none()); - } - } - - /// Fails every read. - #[derive(Debug)] - struct FaultyInput; - - impl Read for FaultyInput { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { - Err(io::Error::from_raw_os_error(libc::EIO)) - } - } - - #[test] - fn a_read_that_fails() { - let e = Reader::open(FaultyInput).unwrap_err(); - assert_eq!(e.kind(), ErrorKind::Io); - assert_eq!(e.io_error().and_then(io::Error::raw_os_error), Some(libc::EIO)); - // Later on too. A record ends where the next one starts, so the - // first one is read whole when the second one begins. - let bytes = sequence(&[ROOT, r#"{"name":"a"}"#]); - let mut reader = Reader::open(bytes.chain(FaultyInput)).unwrap(); - let e = reader.next().unwrap().unwrap_err(); - assert_eq!(e.kind(), ErrorKind::Io); - assert!(reader.next().is_none()); - } - - /// How many bytes the tests feed a decoder at a time. The pieces end - /// anywhere: inside a record, inside a character, right in front of a - /// separator, and around the size a reader reads at a time. - const PIECES: [usize; 8] = [1, 2, 3, 7, 4096, READ_SIZE - 1, READ_SIZE, READ_SIZE + 1]; - /// Reads the manifest `bytes` to its end with a decoder that is fed /// `size` bytes at a time. Returns the manifest, the number of the /// record of every object after the root file object, and how much the @@ -1538,103 +1218,4 @@ mod tests { } } } - - /// Hands out the bytes of a manifest `step` at a time. With `blocking` - /// it says before every piece that it has nothing yet, as a pipe that - /// does not block does while its writer is slow. - #[derive(Debug)] - struct Hiccups<'a> { - bytes: &'a [u8], - step: usize, - blocking: bool, - /// Whether it said so before the next piece. - said: bool, - } - - impl Read for Hiccups<'_> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - if self.blocking && !self.said { - self.said = true; - return Err(io::ErrorKind::WouldBlock.into()); - } - self.said = false; - let n = self.step.min(buf.len()).min(self.bytes.len()); - buf[..n].copy_from_slice(&self.bytes[..n]); - self.bytes = &self.bytes[n..]; - Ok(n) - } - } - - #[test] - fn reading_on_after_would_block() { - let long = format!(r#"{{"name":"long","size":0,"pad":"{}"}}"#, "p".repeat(3 * READ_SIZE)); - let bytes = sequence(&[ - ROOT, - r#"{"name":"a","size":0}"#, - &long, - r#"{"name":"é","type":"dir"}"#, - SIGNATURE, - TRAILER, - ]); - let whole = read_whole(&bytes).unwrap(); - // The number of the record of every object, and how much is signed - // after it, as a reader says that never sees a WouldBlock. - let mut unblocked = Vec::new(); - let mut reader = Reader::open(&bytes[..]).unwrap(); - while let Some(object) = reader.next() { - object.unwrap(); - unblocked.push((reader.record_number(), reader.signed_len())); - } - let would_block = |e: &Error| e.io_error().map(io::Error::kind) == Some(io::ErrorKind::WouldBlock); - for step in [1, 7, 4096, READ_SIZE + 1] { - let hiccups = Hiccups { - bytes: &bytes, - step, - blocking: false, - said: false, - }; - let mut reader = Reader::open(hiccups).unwrap(); - // From here on the input has nothing every other time it is - // read, in the middle of records as well. - reader.get_mut().blocking = true; - let mut read = Whole { - root: reader.root().clone(), - objects: Vec::new(), - signatures: Vec::new(), - }; - let mut blocked = 0; - let mut numbers = Vec::new(); - let mut last = (reader.record_number(), reader.signed_len()); - while let Some(object) = reader.next() { - let now = (reader.record_number(), reader.signed_len()); - match object { - Ok(Object::File(object)) => read.objects.push(*object), - Ok(Object::Signature(signature)) => read.signatures.push(signature), - // The error is about the record being read, and the - // reader says what it said before it. - Err(e) if would_block(&e) => { - assert_eq!(e.record_number(), Some(last.0 + 1), "{} at a time", step); - assert_eq!(now, last, "{} at a time", step); - blocked += 1; - continue; - } - Err(e) => panic!("{} at a time: {}", step, e), - } - numbers.push(now); - last = now; - } - assert_eq!(read, whole, "{} at a time", step); - assert_eq!(numbers, unblocked, "{} at a time", step); - assert!(blocked > 0, "{} at a time", step); - assert!(reader.next().is_none()); - } - // Reader::open() has no reader to go on with. - let hiccups = Hiccups { - bytes: &bytes, - step: 7, - blocking: true, - said: false, - }; - assert!(would_block(&Reader::open(hiccups).unwrap_err())); - } } diff --git a/src/uapi16/writer.rs b/src/uapi16/writer.rs index 65b112c..2a57518 100644 --- a/src/uapi16/writer.rs +++ b/src/uapi16/writer.rs @@ -1,9 +1,10 @@ // SPDX-License-Identifier: LGPL-2.1-or-later //! Writing a manifest record by record: an [`Encoder`] puts the records //! together, and a [`Writer`] writes them out. +//! +//! [`Writer`]: crate::Writer use std::fmt; -use std::io::Write; use std::num::NonZeroU64; use json::{ObjectWriter, ValueWriter}; @@ -23,11 +24,6 @@ use crate::{ SIGNATURE_MEDIA_TYPE, STRING_RULES, TRAILER_MEDIA_TYPE, USER_NAMESPACE, }; -/// The most room for the text of a record that a [`Writer`] keeps from one -/// record to the next. A larger record takes more, and gives it back once -/// it went out. -const TEXT_CAPACITY: usize = 64 * 1024; - /// How far an encoder has got in the manifest. #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum Stage { @@ -39,152 +35,6 @@ enum Stage { Done, } -/// Writes a manifest record by record. -/// -/// [`Writer::begin`] writes the root file object, [`Writer::file`] the -/// file objects, [`Writer::signature`] the signature objects after them -/// and [`Writer::finish`] the trailer object. A manifest without its -/// trailer is one a reader takes as incomplete, so a writer that is -/// dropped before it finished leaves nothing that passes for a manifest. -/// -/// Every object is one record of the JSON text sequence: a record -/// separator, the object with its keys sorted and without whitespace, and -/// a newline, the way `systemd-manifest` writes it. A field that says -/// nothing is left out. So is the inode type where the specification -/// implies it, on the root file object and on regular files. An -/// [`Encoder`] puts each record together, and it goes out in one write, so -/// a file wants a `BufWriter` around it. -/// -/// The root file object says what the manifest records in `features` and -/// which order it lists its file objects in in `order`. The writer checks -/// every object against them as a [`Reader`](crate::Reader) does: an object -/// has no fields of features the manifest does not record, it comes right -/// after the object of its directory, and it keeps to the order. It checks -/// what an object is itself as a reader does too: the fields its inode -/// type permits, the value of every field and every string it writes, and -/// the fields against each other. An object with more than one fault is -/// refused for the one a reader finds first. The one fault a reader does -/// not find is an encoding that is not in lower case: a reader lowers its -/// case, and the writer refuses it. No record is larger than -/// [`MAX_RECORD_SIZE`], its record separator included, and no file object -/// has more than [`MAX_CONTENTS`] contents objects. An object the writer -/// refuses is not written, and the writer goes on as if it had not been -/// given it. -/// -/// The writer leaves out what the manifest says without it: where the -/// manifest records them, a flag that is cleared, `readOnly` of false, -/// `gptFlags` of 0 and extended attributes there are none of, and -/// `features` if they are the default ones. It leaves out the hash of no -/// data next to the size 0, as the specification recommends. Without an -/// encoding the specification says that the encoded size and hash should -/// not be there. The writer leaves them out next to the original ones, and -/// writes one that is the only statement of the size or the hash: written -/// as the original size or hash it would come back as another field, and -/// left out it would be lost. The writer also leaves out every field a -/// [`Reader`](crate::Reader) filled in, see [`FilledIn`]. So what a reader -/// returns can be written back with the root file object it read. A record -/// close to [`MAX_RECORD_SIZE`] can grow past it on the way back, as the -/// writer writes some values in another form than a manifest may have them -/// in: a UUID with its hyphens, and the value of an extended attribute as -/// text where it can. The writer refuses such a record. -/// -/// ``` -/// use uapi16::{FileObject, Name, Object, Order, Reader, RootObject, Writer}; -/// -/// let mut root = RootObject::default(); -/// root.order = Some(Order::CodePoint); -/// let mut file = FileObject::new(Name::new("a")?); -/// file.size = Some(3); -/// let mut writer = Writer::begin(Vec::new(), &root)?; -/// writer.file(&file)?; -/// let bytes = writer.finish()?; -/// assert_eq!( -/// String::from_utf8_lossy(&bytes), -/// concat!( -/// "\x1e{\"mediaType\":\"application/vnd.uapi.16.manifest\",\"order\":\"codepoint\"}\n", -/// "\x1e{\"name\":\"a\",\"size\":3}\n", -/// "\x1e{\"mediaType\":\"application/vnd.uapi.16.trailer\"}\n", -/// ) -/// ); -/// let mut reader = Reader::open(&bytes[..])?; -/// assert!(matches!(reader.next(), Some(Ok(Object::File(object))) if *object == file)); -/// # Ok::<(), Box>(()) -/// ``` -#[derive(Debug)] -pub struct Writer { - out: W, - /// The text of the record that goes out next. The room for it is kept - /// from one record to the next. - text: String, - encoder: Encoder, -} - -impl Writer { - /// Starts a manifest in `out` with the root file object `root`. - pub fn begin(out: W, root: &RootObject) -> Result, Error> { - let mut text = String::new(); - let encoder = Encoder::begin(root, &mut text)?; - let mut writer = Writer { out, text, encoder }; - writer.write_out(ROOT_RECORD)?; - Ok(writer) - } - - /// Writes the file object `file`. - pub fn file(&mut self, file: &FileObject) -> Result<(), Error> { - self.send_record(|encoder, text| encoder.file(file, text)) - } - - /// Writes a signature object. No file object can follow it: it would - /// be one the signature does not cover. - pub fn signature(&mut self, signature: &Signature) -> Result<(), Error> { - self.send_record(|encoder, text| encoder.signature(signature, text)) - } - - /// Ends the manifest with the trailer object, flushes what it was - /// written to and returns it. - pub fn finish(mut self) -> Result { - let record_number = self.encoder.next_record; - self.send_record(|encoder, text| encoder.finish(text))?; - self.out.flush().map_err(|e| Error::from(e).in_record(record_number))?; - Ok(self.out) - } - - /// What the manifest is written to. Every method writes its record to - /// it before it returns, so it has the manifest up to the last record. - /// Before the first signature object, that is what the signature - /// objects sign. - pub fn get_ref(&self) -> &W { - &self.out - } - - /// Writes out the record that `encode` has the encoder append to the - /// text. When the encoder took it back as too large, the text gives - /// back the room it took. - fn send_record( - &mut self, - encode: impl FnOnce(&mut Encoder, &mut String) -> Result<(), Error>, - ) -> Result<(), Error> { - let record_number = self.encoder.next_record; - match encode(&mut self.encoder, &mut self.text) { - Ok(()) => self.write_out(record_number), - Err(e) => { - self.text.shrink_to(TEXT_CAPACITY); - Err(e) - } - } - } - - /// Writes out the text, the record with the number `record_number`, in - /// one write, and empties it for the next record. A record larger than - /// [`TEXT_CAPACITY`] gives back the room it took beyond that. - fn write_out(&mut self, record_number: u64) -> Result<(), Error> { - let written = self.out.write_all(self.text.as_bytes()); - self.text.clear(); - self.text.shrink_to(TEXT_CAPACITY); - written.map_err(|e| Error::from(e).in_record(record_number)) - } -} - /// Puts a manifest together record by record, and does no I/O. /// /// [`Encoder::begin`] appends the record of the root file object to a @@ -221,10 +71,13 @@ impl Writer { /// ); /// # Ok::<(), Box>(()) /// ``` +/// +/// [`Write`]: std::io::Write +/// [`Writer`]: crate::Writer #[derive(Debug)] pub struct Encoder { /// The number of the record that is appended next. - next_record: u64, + pub(crate) next_record: u64, stage: Stage, /// The check of the file objects so far, which the next one is checked /// against. @@ -851,72 +704,12 @@ mod tests { use super::*; use crate::error::ErrorKind; use crate::object::{DosFlags, FatSerial, InodeFlags, Location, Mode, Uuid}; - use crate::testing::{read_whole, sequence, write_whole, Whole, EMPTY, HELLO, ROOT, SIGNATURE, TRAILER}; - use crate::{Reader, Sha256}; + use crate::testing::{ + directory, everything, name, read_whole, record, recording_all, sequence, write_whole, written, Whole, EMPTY, + HELLO, ROOT, SIGNATURE, TRAILER, + }; + use crate::{Sha256, Writer}; use std::collections::BTreeMap; - use std::io; - - fn name(name: &str) -> Name { - Name::new(name).unwrap() - } - - /// The records `writer` wrote after `root`, without the frame of - /// each: the separator in front and the newline behind. - fn written(root: &RootObject, write: impl FnOnce(&mut Writer>)) -> Vec { - let mut writer = Writer::begin(Vec::new(), root).unwrap(); - write(&mut writer); - let text = String::from_utf8(writer.finish().unwrap()).unwrap(); - let separator = char::from(RECORD_SEPARATOR); - assert!(text.starts_with(separator)); - text.split(separator) - .skip(1) - .map(|record| record.strip_suffix(char::from(RECORD_END)).unwrap().to_string()) - .collect() - } - - /// The root file object of a manifest that records every feature, and - /// its record. - fn recording_all() -> (RootObject, String) { - let root = RootObject { - features: Some(Features::ALL), - ..RootObject::default() - }; - let names: Vec = Features::ALL.names().map(|name| format!("\"{}\"", name)).collect(); - let text = format!( - r#"{{"features":[{}],"mediaType":"application/vnd.uapi.16.manifest"}}"#, - names.join(",") - ); - (root, text) - } - - /// A directory that says nothing else about itself. - fn directory(name_of: &str) -> FileObject { - let mut directory = FileObject::new(name(name_of)); - directory.inode_type = InodeType::Directory; - directory - } - - /// The record the writer writes for `object`, in a manifest that - /// records every feature, after the objects of the directories it is - /// in. - fn record(object: &FileObject) -> String { - let (root, root_text) = recording_all(); - let directories: Vec = object - .name - .match_indices('/') - .map(|(end, _)| directory(&object.name[..end])) - .collect(); - let mut records = written(&root, |writer| { - for directory in &directories { - writer.file(directory).unwrap(); - } - writer.file(object).unwrap(); - }); - assert_eq!(records.len(), directories.len() + 3); - assert_eq!(records[0], root_text); - assert_eq!(records.pop().as_deref(), Some(TRAILER)); - records.pop().unwrap() - } /// What `systemd-manifest` writes, see the manifests in tests/fixtures: /// the same objects give the same records. @@ -1076,76 +869,6 @@ mod tests { assert_eq!(e.kind(), ErrorKind::Manifest(Problem::UnlistedFeature)); } - /// An object with every field there is, and one of its contents - /// objects with every field. - fn everything() -> FileObject { - let uuid = |last: u8| { - let mut bytes = [0x11; Uuid::SIZE]; - bytes[Uuid::SIZE - 1] = last; - Uuid::new(bytes) - }; - let mut encoded = Contents::new(Location::File(name("blob.gz"))); - encoded.encoding = Some("gzip".into()); - encoded.encoded_size = Some(3); - encoded.encoded_sha256 = Some(Sha256::digest(b"gz")); - encoded.original_size = Some(9); - encoded.original_sha256 = Some(Sha256::digest(b"original")); - encoded.offset = 4; - let mut object = FileObject::new(name("a/\u{e9} \"quoted\"\\")); - object.size = Some(5); - object.inode.mode = Mode::new(0o440); - object.inode.uid = Some(1); - object.inode.gid = Some(2); - object.inode.user_name = Some("user".into()); - object.inode.group_name = Some("group".into()); - object.inode.mtime_nsec = Some(u64::MAX); - object.inode_token = NonZeroU64::new(3); - object.inode.user_xattrs = Some(Box::new(BTreeMap::from([ - ("text".to_string(), b"value".to_vec()), - ("empty".to_string(), Vec::new()), - ("binary".to_string(), vec![0xff, 0x00]), - ("nul".to_string(), b"a\0b".to_vec()), - ("noncharacter".to_string(), "a\u{fffe}".as_bytes().to_vec()), - ]))); - // Flags of both kinds, which mix in the order of their names. - object.inode.flags.put(InodeFlags::APPEND_ONLY, Some(true)); - object.dos_flags.put(DosFlags::ARCHIVE, Some(false)); - object.dos_flags.put(DosFlags::HIDDEN, Some(true)); - object.inode.flags.put(InodeFlags::IMMUTABLE, Some(true)); - object.inode.flags.put(InodeFlags::NO_COW, Some(false)); - object.dos_flags.put(DosFlags::SYSTEM, Some(true)); - // The partition is read-only, which gptFlags says as well where the - // manifest records both. - object.gpt = Some(Box::new(GptPartition { - label: Some("label".into()), - uuid: Some(uuid(1)), - type_uuid: Some(uuid(2)), - flags: Some( - GptPartition::FLAG_NO_AUTO | GptPartition::FLAG_GROW_FILE_SYSTEM | GptPartition::FLAG_READ_ONLY, - ), - no_auto: Some(true), - grow_file_system: Some(true), - })); - object.inode.read_only = Some(true); - object.contents = vec![ - encoded, - Contents { - http_headers: vec![ - ("X-Foo".into(), "bar".into()), - ("Authorization".into(), "Bearer x".into()), - ], - ..Contents::new(Location::Url("https://example.com/blob".into())) - }, - Contents::new(Location::Literal(b"hello".to_vec())), - Contents::default(), - ]; - object.sha256 = HELLO.parse().ok(); - object.validity.since_usec = 6; - object.validity.until_usec = Some(7); - object.validity.state = Validity::Revoked; - object - } - /// Checks that the keys of every object in `value` are in the order of /// their bytes. fn sorted(value: &json::Value) { @@ -2129,70 +1852,6 @@ mod tests { } } - /// No record is larger than MAX_RECORD_SIZE, its record separator - /// included, which is how a reader counts it too. A larger one is not - /// written, and the writer goes on. - #[test] - fn the_largest_record() { - // An object whose record is as large as one with the name `name_of` - // and an extended attribute, plus `pad` bytes. - let padded = |name_of: &str, pad: usize| { - let mut object = FileObject::new(name(name_of)); - object.inode.user_xattrs = Some(Box::new(BTreeMap::from([("p".to_string(), vec![b'x'; pad])]))); - object - }; - // The separator and the newline are not in what record() returns. - let frame = record(&padded("a", 0)).len() + 2; - let (root, _) = recording_all(); - let mut writer = Writer::begin(Vec::new(), &root).unwrap(); - let before = writer.get_ref().len(); - let e = writer.file(&padded("a", MAX_RECORD_SIZE - frame + 1)).unwrap_err(); - assert_eq!( - (e.kind(), e.record_number(), e.name()), - (ErrorKind::Manifest(Problem::RecordTooLarge), Some(1), Some("a")) - ); - assert_eq!( - e.to_string(), - format!("manifest record exceeds {} bytes", MAX_RECORD_SIZE) - ); - assert_eq!(writer.get_ref().len(), before); - assert!(writer.text.capacity() <= TEXT_CAPACITY); - // A directory that is not written is not the directory of what - // comes next. - let mut large = padded("d", MAX_RECORD_SIZE); - large.inode_type = InodeType::Directory; - let e = writer.file(&large).unwrap_err(); - assert_eq!(e.kind(), ErrorKind::Manifest(Problem::RecordTooLarge)); - let e = writer.file(&FileObject::new(name("d/x"))).unwrap_err(); - assert_eq!(e.kind(), ErrorKind::Manifest(Problem::Misplaced)); - // The largest one, which a reader takes. - let largest = padded("a", MAX_RECORD_SIZE - frame); - writer.file(&largest).unwrap(); - assert_eq!(writer.get_ref().len() - before, MAX_RECORD_SIZE); - assert!(writer.text.capacity() <= TEXT_CAPACITY); - let mut signature = Signature::new("pkcs7"); - signature.data = Some(vec![0; MAX_RECORD_SIZE]); - let e = writer.signature(&signature).unwrap_err(); - assert_eq!( - (e.kind(), e.record_number()), - (ErrorKind::Manifest(Problem::RecordTooLarge), Some(2)) - ); - let bytes = writer.finish().unwrap(); - let m = read_whole(&bytes).unwrap(); - assert_eq!(m.objects.len(), 1); - assert_eq!(m.objects[0].inode.user_xattrs, largest.inode.user_xattrs); - // What a reader read, the writer writes back. - assert_eq!(write_whole(&m).unwrap(), bytes); - // The root file object's record is record 0. - let mut root = recording_all().0; - root.inode.user_xattrs = padded("a", MAX_RECORD_SIZE).inode.user_xattrs; - let e = Writer::begin(Vec::new(), &root).unwrap_err(); - assert_eq!( - (e.kind(), e.record_number()), - (ErrorKind::Manifest(Problem::RecordTooLarge), Some(0)) - ); - } - /// An encoder appends what a writer writes, and nothing where it fails. #[test] fn encodes_what_a_writer_writes() { @@ -2236,76 +1895,4 @@ mod tests { } assert_eq!(text.len(), appended); } - - /// What was written so far is there to be signed: the signature - /// objects sign what comes before the first of them. - #[test] - fn what_was_written_can_be_signed() { - let mut writer = Writer::begin(Vec::new(), &RootObject::default()).unwrap(); - writer.file(&FileObject::new(name("a"))).unwrap(); - let signed = writer.get_ref().len(); - let hash = Sha256::digest(writer.get_ref()); - let mut signature = Signature::new("pkcs7"); - signature.data = Some(hash.as_bytes().to_vec()); - writer.signature(&signature).unwrap(); - let bytes = writer.finish().unwrap(); - let mut reader = Reader::open(&bytes[..]).unwrap(); - let objects: Vec<_> = reader.by_ref().collect::>().unwrap(); - assert_eq!(objects.len(), 2); - assert_eq!(reader.signed_len(), u64::try_from(signed).ok()); - assert_eq!(Sha256::digest(&bytes[..signed]), hash); - } - - /// Notes every write and every flush, and fails the write `fail_at`. - #[derive(Debug, Default)] - struct Calls { - writes: Vec, - flushes: usize, - fail_at: Option, - } - - impl Write for Calls { - fn write(&mut self, data: &[u8]) -> io::Result { - if self.fail_at == Some(self.writes.len()) { - return Err(io::Error::from_raw_os_error(libc::ENOSPC)); - } - self.writes.push(data.len()); - Ok(data.len()) - } - - fn flush(&mut self) -> io::Result<()> { - self.flushes += 1; - Ok(()) - } - } - - #[test] - fn a_record_is_one_write() { - let mut calls = Calls::default(); - let (root, root_text) = recording_all(); - let mut writer = Writer::begin(&mut calls, &root).unwrap(); - writer.file(&directory("a")).unwrap(); - writer.file(&everything()).unwrap(); - let mut empty = FileObject::new(name("empty")); - empty.sha256 = EMPTY.parse().ok(); - writer.file(&empty).unwrap(); - writer.finish().unwrap(); - assert_eq!(calls.writes.len(), 5); - assert_eq!(calls.writes[0], sequence(&[root_text.as_str()]).len()); - assert_eq!(calls.writes[4], sequence(&[TRAILER]).len()); - assert_eq!(calls.flushes, 1); - // An error of what is written to is the writer's, about the record - // it wrote. - for (fail_at, record_number) in [(0, 0), (1, 1)] { - let mut calls = Calls { - fail_at: Some(fail_at), - ..Calls::default() - }; - let e = Writer::begin(&mut calls, &RootObject::default()) - .and_then(|mut writer| writer.file(&FileObject::new(name("a")))) - .unwrap_err(); - assert_eq!((e.kind(), e.record_number()), (ErrorKind::Io, Some(record_number))); - assert_eq!(e.io_error().and_then(io::Error::raw_os_error), Some(libc::ENOSPC)); - } - } }