Skip to main content

Reader

Struct Reader 

Source
pub struct Reader { /* private fields */ }
Expand description

An open XISF file.

Implementations§

Source§

impl Reader

Source

pub fn open(path: impl AsRef<Path>) -> Result<Reader, Error>

Open and scan a file from disk.

The file is memory-mapped, so an attached block costs nothing until it is read.

§Mapped files and concurrent modification

Because the file stays mapped for as long as the reader lives, another process that truncates it out from under us turns a later block read into SIGBUS, which no amount of checking in this crate can catch: the bytes were valid when they were checked and stopped existing afterwards. This is inherent to memory mapping rather than specific to this crate, and it is the reason Reader::from_bytes exists – a caller reading files that others may be rewriting should read the bytes themselves and hand them over, which costs a copy and removes the hazard entirely.

Source

pub fn from_bytes(bytes: Vec<u8>) -> Result<Reader, Error>

Scan a file already in memory.

Source

pub fn header(&self) -> &Header

Source

pub fn header_text(&self) -> Result<&str, Error>

The header’s XML source, exactly as the file holds it.

The parsed Header is what a reader normally wants, but a tool that shows a file to a person needs the text: the parse is lossy about formatting, attribute order and comments, so re-serialising it would show something the file does not contain.

Source

pub fn bytes(&self) -> &[u8] ⓘ

Source

pub fn path(&self) -> Option<&Path>

Source

pub fn stored_block(&self, data: &DataRef) -> Result<Cow<'_, [u8]>, Error>

The stored bytes of an element’s data block, before decompression.

An attached block is borrowed straight from the mapping; a text-encoded one has to be decoded, so it is owned.

Source

pub fn block(&self, data: &DataRef) -> Result<Cow<'_, [u8]>, Error>

The usable bytes of a block: decoded, decompressed and unshuffled.

Source

pub fn verify(&self, data: &DataRef) -> Result<ChecksumStatus, Error>

Check a block’s stored bytes against its recorded checksum.

The checksum covers the block as stored, so a compressed block can be verified without being decompressed.

Source

pub fn set_verify_checksums(&mut self, verify: bool)

Whether to check a block against its recorded checksum when reading it.

On by default, because the specification requires a decoder to check a digest the file went to the trouble of recording, and because handing back corrupt bytes without a word is the worse failure.

It costs one hash pass over the block: about 15 ms for a 32 MB frame with sha-1 or sha-256, and about 55 ms with sha-512 – roughly twice what the typed read that usually follows it costs, and small against the disk read that must precede it. See docs/PERFORMANCE.md.

Turning it off is for a caller who reads the same block repeatedly, or who has verified the file by other means; Reader::verify then remains available on demand, reporting a mismatch rather than failing.

Blocks with no recorded checksum are unaffected: the spec makes them optional, and their absence is not a failure.

Source

pub fn set_url_resolver( &mut self, resolver: impl Fn(&str) -> Result<Vec<u8>, Error> + Send + Sync + 'static, )

Install a resolver for url(...) blocks.

This library never makes a network request itself, and that is a deliberate design decision rather than an omission.

A url(...) locator is a request, written in a file, for the reader to go and fetch something. Honouring it automatically would mean that Reader::open on a local file could reach out to a host the caller never named – which is a server-side request forgery primitive when the caller is a service, and reaches cloud metadata endpoints and internal addresses that are not otherwise exposed. It would also make timeouts, proxies, redirects and certificate trust this crate’s problem, and pull in an HTTP and TLS stack that the rest of the workspace does without.

Handing the decision to the caller solves all of it at once. The resolver receives the URL exactly as the file wrote it and returns the bytes, so a caller can allow only the schemes and hosts they mean to, apply their own timeouts, or refuse. Without one, a remote block is an Unsupported error that says so.

let mut reader = Reader::open("unit.xisf")?;
reader.set_url_resolver(|url| {
    if !url.starts_with("https://data.example.com/") {
        return Err(xisf_core::Error::new(
            xisf_core::ErrorKind::Unsupported,
            format!("refusing to fetch {url}"),
        ));
    }
    // ... fetch and return the bytes ...
});
Source

pub fn clear_url_resolver(&mut self)

Remove any installed resolver, so remote blocks are refused again.

Source

pub fn follow_absolute_paths(&mut self, allow: bool)

Whether to follow path(...) locators that name an absolute path.

Off by default, and that is a deliberate departure from the letter of the specification, which defines the field as an absolute path. A header is untrusted input: a file that says location="path(/etc/shadow)" is asking a library to read a file the user never named, and returning its bytes as pixel data is an arbitrary-file-read primitive dressed as an image decoder.

Relative locators still resolve beside the referring file, which is what a distributed unit actually needs and what the corpus uses. A caller who genuinely wants absolute paths – a trusted pipeline, say – turns them on and takes that decision knowingly.

Trait Implementations§

Source§

impl Debug for Reader

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.