| 1 | #![ allow(dead_code)] | 
| 2 | use std::io; | 
|---|
| 3 | use std::io::Write; | 
|---|
| 4 | use std::slice; | 
|---|
| 5 | use std::str; | 
|---|
| 6 |  | 
|---|
| 7 | use crate::other; | 
|---|
| 8 |  | 
|---|
| 9 | // Keywords for PAX extended header records. | 
|---|
| 10 | pub const PAX_NONE: &str = ""; // Indicates that no PAX key is suitable | 
|---|
| 11 | pub const PAX_PATH: &str = "path"; | 
|---|
| 12 | pub const PAX_LINKPATH: &str = "linkpath"; | 
|---|
| 13 | pub const PAX_SIZE: &str = "size"; | 
|---|
| 14 | pub const PAX_UID: &str = "uid"; | 
|---|
| 15 | pub const PAX_GID: &str = "gid"; | 
|---|
| 16 | pub const PAX_UNAME: &str = "uname"; | 
|---|
| 17 | pub const PAX_GNAME: &str = "gname"; | 
|---|
| 18 | pub const PAX_MTIME: &str = "mtime"; | 
|---|
| 19 | pub const PAX_ATIME: &str = "atime"; | 
|---|
| 20 | pub const PAX_CTIME: &str = "ctime"; // Removed from later revision of PAX spec, but was valid | 
|---|
| 21 | pub const PAX_CHARSET: &str = "charset"; // Currently unused | 
|---|
| 22 | pub const PAX_COMMENT: &str = "comment"; // Currently unused | 
|---|
| 23 |  | 
|---|
| 24 | pub const PAX_SCHILYXATTR: &str = "SCHILY.xattr."; | 
|---|
| 25 |  | 
|---|
| 26 | // Keywords for GNU sparse files in a PAX extended header. | 
|---|
| 27 | pub const PAX_GNUSPARSE: &str = "GNU.sparse."; | 
|---|
| 28 | pub const PAX_GNUSPARSENUMBLOCKS: &str = "GNU.sparse.numblocks"; | 
|---|
| 29 | pub const PAX_GNUSPARSEOFFSET: &str = "GNU.sparse.offset"; | 
|---|
| 30 | pub const PAX_GNUSPARSENUMBYTES: &str = "GNU.sparse.numbytes"; | 
|---|
| 31 | pub const PAX_GNUSPARSEMAP: &str = "GNU.sparse.map"; | 
|---|
| 32 | pub const PAX_GNUSPARSENAME: &str = "GNU.sparse.name"; | 
|---|
| 33 | pub const PAX_GNUSPARSEMAJOR: &str = "GNU.sparse.major"; | 
|---|
| 34 | pub const PAX_GNUSPARSEMINOR: &str = "GNU.sparse.minor"; | 
|---|
| 35 | pub const PAX_GNUSPARSESIZE: &str = "GNU.sparse.size"; | 
|---|
| 36 | pub const PAX_GNUSPARSEREALSIZE: &str = "GNU.sparse.realsize"; | 
|---|
| 37 |  | 
|---|
| 38 | /// An iterator over the pax extensions in an archive entry. | 
|---|
| 39 | /// | 
|---|
| 40 | /// This iterator yields structures which can themselves be parsed into | 
|---|
| 41 | /// key/value pairs. | 
|---|
| 42 | pub struct PaxExtensions<'entry> { | 
|---|
| 43 | data: slice::Split<'entry, u8, fn(&u8) -> bool>, | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 | impl<'entry> PaxExtensions<'entry> { | 
|---|
| 47 | /// Create new pax extensions iterator from the given entry data. | 
|---|
| 48 | pub fn new(a: &'entry [u8]) -> Self { | 
|---|
| 49 | fn is_newline(a: &u8) -> bool { | 
|---|
| 50 | *a == b'\n ' | 
|---|
| 51 | } | 
|---|
| 52 | PaxExtensions { | 
|---|
| 53 | data: a.split(pred:is_newline), | 
|---|
| 54 | } | 
|---|
| 55 | } | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | /// A key/value pair corresponding to a pax extension. | 
|---|
| 59 | pub struct PaxExtension<'entry> { | 
|---|
| 60 | key: &'entry [u8], | 
|---|
| 61 | value: &'entry [u8], | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | pub fn pax_extensions_value(a: &[u8], key: &str) -> Option<u64> { | 
|---|
| 65 | for extension: Result, …> in PaxExtensions::new(a) { | 
|---|
| 66 | let current_extension: PaxExtension<'_> = match extension { | 
|---|
| 67 | Ok(ext: PaxExtension<'_>) => ext, | 
|---|
| 68 | Err(_) => return None, | 
|---|
| 69 | }; | 
|---|
| 70 | if current_extension.key() != Ok(key) { | 
|---|
| 71 | continue; | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | let value: &str = match current_extension.value() { | 
|---|
| 75 | Ok(value: &str) => value, | 
|---|
| 76 | Err(_) => return None, | 
|---|
| 77 | }; | 
|---|
| 78 | let result: u64 = match value.parse::<u64>() { | 
|---|
| 79 | Ok(result: u64) => result, | 
|---|
| 80 | Err(_) => return None, | 
|---|
| 81 | }; | 
|---|
| 82 | return Some(result); | 
|---|
| 83 | } | 
|---|
| 84 | None | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | impl<'entry> Iterator for PaxExtensions<'entry> { | 
|---|
| 88 | type Item = io::Result<PaxExtension<'entry>>; | 
|---|
| 89 |  | 
|---|
| 90 | fn next(&mut self) -> Option<io::Result<PaxExtension<'entry>>> { | 
|---|
| 91 | let line = match self.data.next() { | 
|---|
| 92 | Some(line) if line.is_empty() => return None, | 
|---|
| 93 | Some(line) => line, | 
|---|
| 94 | None => return None, | 
|---|
| 95 | }; | 
|---|
| 96 |  | 
|---|
| 97 | Some( | 
|---|
| 98 | line.iter() | 
|---|
| 99 | .position(|b| *b == b' ') | 
|---|
| 100 | .and_then(|i| { | 
|---|
| 101 | str::from_utf8(&line[..i]) | 
|---|
| 102 | .ok() | 
|---|
| 103 | .and_then(|len| len.parse::<usize>().ok().map(|j| (i + 1, j))) | 
|---|
| 104 | }) | 
|---|
| 105 | .and_then(|(kvstart, reported_len)| { | 
|---|
| 106 | if line.len() + 1 == reported_len { | 
|---|
| 107 | line[kvstart..] | 
|---|
| 108 | .iter() | 
|---|
| 109 | .position(|b| *b == b'=') | 
|---|
| 110 | .map(|equals| (kvstart, equals)) | 
|---|
| 111 | } else { | 
|---|
| 112 | None | 
|---|
| 113 | } | 
|---|
| 114 | }) | 
|---|
| 115 | .map(|(kvstart, equals)| PaxExtension { | 
|---|
| 116 | key: &line[kvstart..kvstart + equals], | 
|---|
| 117 | value: &line[kvstart + equals + 1..], | 
|---|
| 118 | }) | 
|---|
| 119 | .ok_or_else(|| other( "malformed pax extension")), | 
|---|
| 120 | ) | 
|---|
| 121 | } | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | impl<'entry> PaxExtension<'entry> { | 
|---|
| 125 | /// Returns the key for this key/value pair parsed as a string. | 
|---|
| 126 | /// | 
|---|
| 127 | /// May fail if the key isn't actually utf-8. | 
|---|
| 128 | pub fn key(&self) -> Result<&'entry str, str::Utf8Error> { | 
|---|
| 129 | str::from_utf8(self.key) | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | /// Returns the underlying raw bytes for the key of this key/value pair. | 
|---|
| 133 | pub fn key_bytes(&self) -> &'entry [u8] { | 
|---|
| 134 | self.key | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | /// Returns the value for this key/value pair parsed as a string. | 
|---|
| 138 | /// | 
|---|
| 139 | /// May fail if the value isn't actually utf-8. | 
|---|
| 140 | pub fn value(&self) -> Result<&'entry str, str::Utf8Error> { | 
|---|
| 141 | str::from_utf8(self.value) | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | /// Returns the underlying raw bytes for this value of this key/value pair. | 
|---|
| 145 | pub fn value_bytes(&self) -> &'entry [u8] { | 
|---|
| 146 | self.value | 
|---|
| 147 | } | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | /// Extension trait for `Builder` to append PAX extended headers. | 
|---|
| 151 | impl<T: Write> crate::Builder<T> { | 
|---|
| 152 | /// Append PAX extended headers to the archive. | 
|---|
| 153 | /// | 
|---|
| 154 | /// Takes in an iterator over the list of headers to add to convert it into a header set formatted. | 
|---|
| 155 | /// | 
|---|
| 156 | /// Returns io::Error if an error occurs, else it returns () | 
|---|
| 157 | pub fn append_pax_extensions<'key, 'value>( | 
|---|
| 158 | &mut self, | 
|---|
| 159 | headers: impl IntoIterator<Item = (&'key str, &'value [u8])>, | 
|---|
| 160 | ) -> Result<(), io::Error> { | 
|---|
| 161 | // Store the headers formatted before write | 
|---|
| 162 | let mut data: Vec<u8> = Vec::new(); | 
|---|
| 163 |  | 
|---|
| 164 | // For each key in headers, convert into a sized space and add it to data. | 
|---|
| 165 | // This will then be written in the file | 
|---|
| 166 | for (key, value) in headers { | 
|---|
| 167 | let mut len_len = 1; | 
|---|
| 168 | let mut max_len = 10; | 
|---|
| 169 | let rest_len = 3 + key.len() + value.len(); | 
|---|
| 170 | while rest_len + len_len >= max_len { | 
|---|
| 171 | len_len += 1; | 
|---|
| 172 | max_len *= 10; | 
|---|
| 173 | } | 
|---|
| 174 | let len = rest_len + len_len; | 
|---|
| 175 | write!(&mut data, "{}  {} =", len, key)?; | 
|---|
| 176 | data.extend_from_slice(value); | 
|---|
| 177 | data.push( b'\n '); | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | // Ignore the header append if it's empty. | 
|---|
| 181 | if data.is_empty() { | 
|---|
| 182 | return Ok(()); | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | // Create a header of type XHeader, set the size to the length of the | 
|---|
| 186 | // data, set the entry type to XHeader, and set the checksum | 
|---|
| 187 | // then append the header and the data to the archive. | 
|---|
| 188 | let mut header = crate::Header::new_ustar(); | 
|---|
| 189 | let data_as_bytes: &[u8] = &data; | 
|---|
| 190 | header.set_size(data_as_bytes.len() as u64); | 
|---|
| 191 | header.set_entry_type(crate::EntryType::XHeader); | 
|---|
| 192 | header.set_cksum(); | 
|---|
| 193 | self.append(&header, data_as_bytes) | 
|---|
| 194 | } | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|