MCPcopy Create free account
hub / github.com/archlinux/alpm / mtree_buffer_to_string

Function mtree_buffer_to_string

alpm-mtree/src/utils.rs:26–38  ·  view source on GitHub ↗

Creates a [`String`] from a byte vector which may be gzip compressed. If `buffer` contains gzip compressed data, it decompressed before converting it into a `String`. Detects whether `buffer` contains gzip compressed data by checking if it is longer than two bytes and whether the first two bytes are the [`GZIP_MAGIC_NUMBER`]. # Errors Returns an error if - `buffer` contains invalid gzip compres

(buffer: Vec<u8>)

Source from the content-addressed store, hash-verified

24/// - `buffer` contains invalid gzip compressed data
25/// - or `buffer` can not be converted to `String`.
26pub fn mtree_buffer_to_string(buffer: Vec<u8>) -> Result<String, Error> {
27 if buffer.len() >= 2 && [buffer[0], buffer[1]] == GZIP_MAGIC_NUMBER {
28 let mut decoder = GzDecoder::new(buffer.as_slice());
29
30 let mut content = String::new();
31 decoder
32 .read_to_string(&mut content)
33 .map_err(Error::InvalidGzip)?;
34 Ok(content)
35 } else {
36 Ok(String::from_utf8(buffer)?)
37 }
38}

Callers 2

derive_from_readerMethod · 0.85

Calls 1

lenMethod · 0.45

Tested by

no test coverage detected