1 | #![allow (dead_code)] |
2 | use std::io; |
3 | use std::slice; |
4 | use std::str; |
5 | |
6 | use crate::other; |
7 | |
8 | // Keywords for PAX extended header records. |
9 | pub const PAX_NONE: &str = "" ; // Indicates that no PAX key is suitable |
10 | pub const PAX_PATH: &str = "path" ; |
11 | pub const PAX_LINKPATH: &str = "linkpath" ; |
12 | pub const PAX_SIZE: &str = "size" ; |
13 | pub const PAX_UID: &str = "uid" ; |
14 | pub const PAX_GID: &str = "gid" ; |
15 | pub const PAX_UNAME: &str = "uname" ; |
16 | pub const PAX_GNAME: &str = "gname" ; |
17 | pub const PAX_MTIME: &str = "mtime" ; |
18 | pub const PAX_ATIME: &str = "atime" ; |
19 | pub const PAX_CTIME: &str = "ctime" ; // Removed from later revision of PAX spec, but was valid |
20 | pub const PAX_CHARSET: &str = "charset" ; // Currently unused |
21 | pub const PAX_COMMENT: &str = "comment" ; // Currently unused |
22 | |
23 | pub const PAX_SCHILYXATTR: &str = "SCHILY.xattr." ; |
24 | |
25 | // Keywords for GNU sparse files in a PAX extended header. |
26 | pub const PAX_GNUSPARSE: &str = "GNU.sparse." ; |
27 | pub const PAX_GNUSPARSENUMBLOCKS: &str = "GNU.sparse.numblocks" ; |
28 | pub const PAX_GNUSPARSEOFFSET: &str = "GNU.sparse.offset" ; |
29 | pub const PAX_GNUSPARSENUMBYTES: &str = "GNU.sparse.numbytes" ; |
30 | pub const PAX_GNUSPARSEMAP: &str = "GNU.sparse.map" ; |
31 | pub const PAX_GNUSPARSENAME: &str = "GNU.sparse.name" ; |
32 | pub const PAX_GNUSPARSEMAJOR: &str = "GNU.sparse.major" ; |
33 | pub const PAX_GNUSPARSEMINOR: &str = "GNU.sparse.minor" ; |
34 | pub const PAX_GNUSPARSESIZE: &str = "GNU.sparse.size" ; |
35 | pub const PAX_GNUSPARSEREALSIZE: &str = "GNU.sparse.realsize" ; |
36 | |
37 | /// An iterator over the pax extensions in an archive entry. |
38 | /// |
39 | /// This iterator yields structures which can themselves be parsed into |
40 | /// key/value pairs. |
41 | pub struct PaxExtensions<'entry> { |
42 | data: slice::Split<'entry, u8, fn(&u8) -> bool>, |
43 | } |
44 | |
45 | impl<'entry> PaxExtensions<'entry> { |
46 | /// Create new pax extensions iterator from the given entry data. |
47 | pub fn new(a: &'entry [u8]) -> Self { |
48 | fn is_newline(a: &u8) -> bool { |
49 | *a == b' \n' |
50 | } |
51 | PaxExtensions { |
52 | data: a.split(pred:is_newline), |
53 | } |
54 | } |
55 | } |
56 | |
57 | /// A key/value pair corresponding to a pax extension. |
58 | pub struct PaxExtension<'entry> { |
59 | key: &'entry [u8], |
60 | value: &'entry [u8], |
61 | } |
62 | |
63 | pub fn pax_extensions_value(a: &[u8], key: &str) -> Option<u64> { |
64 | for extension: Result, …> in PaxExtensions::new(a) { |
65 | let current_extension: PaxExtension<'_> = match extension { |
66 | Ok(ext: PaxExtension<'_>) => ext, |
67 | Err(_) => return None, |
68 | }; |
69 | if current_extension.key() != Ok(key) { |
70 | continue; |
71 | } |
72 | |
73 | let value: &str = match current_extension.value() { |
74 | Ok(value: &str) => value, |
75 | Err(_) => return None, |
76 | }; |
77 | let result: u64 = match value.parse::<u64>() { |
78 | Ok(result: u64) => result, |
79 | Err(_) => return None, |
80 | }; |
81 | return Some(result); |
82 | } |
83 | None |
84 | } |
85 | |
86 | impl<'entry> Iterator for PaxExtensions<'entry> { |
87 | type Item = io::Result<PaxExtension<'entry>>; |
88 | |
89 | fn next(&mut self) -> Option<io::Result<PaxExtension<'entry>>> { |
90 | let line = match self.data.next() { |
91 | Some(line) if line.is_empty() => return None, |
92 | Some(line) => line, |
93 | None => return None, |
94 | }; |
95 | |
96 | Some( |
97 | line.iter() |
98 | .position(|b| *b == b' ' ) |
99 | .and_then(|i| { |
100 | str::from_utf8(&line[..i]) |
101 | .ok() |
102 | .and_then(|len| len.parse::<usize>().ok().map(|j| (i + 1, j))) |
103 | }) |
104 | .and_then(|(kvstart, reported_len)| { |
105 | if line.len() + 1 == reported_len { |
106 | line[kvstart..] |
107 | .iter() |
108 | .position(|b| *b == b'=' ) |
109 | .map(|equals| (kvstart, equals)) |
110 | } else { |
111 | None |
112 | } |
113 | }) |
114 | .map(|(kvstart, equals)| PaxExtension { |
115 | key: &line[kvstart..kvstart + equals], |
116 | value: &line[kvstart + equals + 1..], |
117 | }) |
118 | .ok_or_else(|| other("malformed pax extension" )), |
119 | ) |
120 | } |
121 | } |
122 | |
123 | impl<'entry> PaxExtension<'entry> { |
124 | /// Returns the key for this key/value pair parsed as a string. |
125 | /// |
126 | /// May fail if the key isn't actually utf-8. |
127 | pub fn key(&self) -> Result<&'entry str, str::Utf8Error> { |
128 | str::from_utf8(self.key) |
129 | } |
130 | |
131 | /// Returns the underlying raw bytes for the key of this key/value pair. |
132 | pub fn key_bytes(&self) -> &'entry [u8] { |
133 | self.key |
134 | } |
135 | |
136 | /// Returns the value for this key/value pair parsed as a string. |
137 | /// |
138 | /// May fail if the value isn't actually utf-8. |
139 | pub fn value(&self) -> Result<&'entry str, str::Utf8Error> { |
140 | str::from_utf8(self.value) |
141 | } |
142 | |
143 | /// Returns the underlying raw bytes for this value of this key/value pair. |
144 | pub fn value_bytes(&self) -> &'entry [u8] { |
145 | self.value |
146 | } |
147 | } |
148 | |