pub struct Reader { /* private fields */ }Expand description
An open XISF file.
Implementations§
Source§impl Reader
impl Reader
Sourcepub fn open(path: impl AsRef<Path>) -> Result<Reader, Error>
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.
pub fn header(&self) -> &Header
Sourcepub fn header_text(&self) -> Result<&str, Error>
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.
pub fn bytes(&self) -> &[u8] ⓘ
pub fn path(&self) -> Option<&Path>
Sourcepub fn stored_block(&self, data: &DataRef) -> Result<Cow<'_, [u8]>, Error>
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.
Sourcepub fn block(&self, data: &DataRef) -> Result<Cow<'_, [u8]>, Error>
pub fn block(&self, data: &DataRef) -> Result<Cow<'_, [u8]>, Error>
The usable bytes of a block: decoded, decompressed and unshuffled.
Sourcepub fn verify(&self, data: &DataRef) -> Result<ChecksumStatus, Error>
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.
Sourcepub fn set_verify_checksums(&mut self, verify: bool)
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.
Sourcepub fn set_url_resolver(
&mut self,
resolver: impl Fn(&str) -> Result<Vec<u8>, Error> + Send + Sync + 'static,
)
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 ...
});Sourcepub fn clear_url_resolver(&mut self)
pub fn clear_url_resolver(&mut self)
Remove any installed resolver, so remote blocks are refused again.
Sourcepub fn follow_absolute_paths(&mut self, allow: bool)
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.