1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
use crate::protocol::prelude::*;
#[cfg(test)]
mod listxattr_test;
pub struct ListxattrRequest<'a> {
phantom: PhantomData<&'a ()>,
node_id: NodeId,
size: Option<num::NonZeroU32>,
}
impl ListxattrRequest<'_> {
pub fn node_id(&self) -> NodeId {
self.node_id
}
pub fn size(&self) -> Option<num::NonZeroU32> {
self.size
}
}
impl fmt::Debug for ListxattrRequest<'_> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("ListxattrRequest")
.field("node_id", &self.node_id)
.field("size", &format_args!("{:?}", &self.size))
.finish()
}
}
impl<'a> fuse_io::DecodeRequest<'a> for ListxattrRequest<'a> {
fn decode_request(
mut dec: fuse_io::RequestDecoder<'a>,
) -> Result<Self, Error> {
let header = dec.header();
debug_assert!(header.opcode == fuse_kernel::FUSE_LISTXATTR);
let raw: &fuse_kernel::fuse_getxattr_in = dec.next_sized()?;
Ok(Self {
phantom: PhantomData,
node_id: try_node_id(header.nodeid)?,
size: num::NonZeroU32::new(raw.size),
})
}
}
pub struct ListxattrResponse<'a> {
buf: ListxattrBuf<'a>,
}
impl<'a> ListxattrResponse<'a> {
pub fn without_capacity() -> ListxattrResponse<'a> {
Self {
buf: ListxattrBuf::SizeOnly { size: 0 },
}
}
#[cfg(feature = "std")]
#[cfg_attr(doc, doc(cfg(feature = "std")))]
pub fn with_max_size(max_size: u32) -> ListxattrResponse<'a> {
Self {
buf: ListxattrBuf::Owned {
cap: Vec::new(),
max_size: max_size as usize,
},
}
}
pub fn with_capacity(capacity: &'a mut [u8]) -> ListxattrResponse<'a> {
Self {
buf: ListxattrBuf::Borrowed {
cap: capacity,
size: 0,
},
}
}
pub fn names(&self) -> impl Iterator<Item = &XattrName> {
XattrNamesIter::new(&self.buf)
}
pub fn add_name(&mut self, name: &XattrName) {
self.try_add_name(name).unwrap()
}
pub fn try_add_name(&mut self, name: &XattrName) -> Result<(), XattrError> {
use crate::XATTR_LIST_MAX;
let name = name.as_bytes();
let name_len = name.len() as usize;
let name_buf_len = name_len + 1;
let name_buf = match &mut self.buf {
ListxattrBuf::SizeOnly { size } => {
let new_size = *size as usize + name_buf_len;
if new_size > XATTR_LIST_MAX {
return Err(XattrError::exceeds_list_max(new_size));
}
*size += name_buf_len as u32;
return Ok(());
},
ListxattrBuf::Borrowed {
cap,
size: size_ref,
} => {
let current_size = *size_ref;
let new_size = current_size + name_buf_len;
if new_size > cap.len() {
return Err(XattrError::exceeds_capacity(
new_size,
cap.len(),
));
}
if new_size > XATTR_LIST_MAX {
return Err(XattrError::exceeds_list_max(new_size));
}
let (_, remaining_cap) = cap.split_at_mut(current_size);
let (name_buf, _) = remaining_cap.split_at_mut(name_buf_len);
*size_ref = new_size;
name_buf
},
#[cfg(feature = "std")]
ListxattrBuf::Owned { cap, max_size } => {
let current_size = cap.len();
let new_size = current_size + name_buf_len;
if new_size > XATTR_LIST_MAX {
return Err(XattrError::exceeds_list_max(new_size));
}
if new_size > *max_size {
return Err(XattrError::exceeds_capacity(
new_size, *max_size,
));
}
cap.resize(new_size, 0u8);
let (_, entry_buf) = cap.split_at_mut(current_size);
entry_buf
},
};
debug_assert!(name_buf.len() == name_buf_len);
let (name_no_nul, name_nul) = name_buf.split_at_mut(name_len);
name_no_nul.copy_from_slice(name);
name_nul[0] = 0;
Ok(())
}
}
enum ListxattrBuf<'a> {
SizeOnly {
size: u32,
},
#[cfg(feature = "std")]
Owned {
cap: Vec<u8>,
max_size: usize,
},
Borrowed {
cap: &'a mut [u8],
size: usize,
},
}
impl fmt::Debug for ListxattrResponse<'_> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let mut out = fmt.debug_struct("ListxattrResponse");
let names = DebugClosure(|fmt| {
fmt.debug_list()
.entries(XattrNamesIter::new(&self.buf))
.finish()
});
match &self.buf {
ListxattrBuf::SizeOnly { size } => {
out.field("size", size);
},
#[cfg(feature = "std")]
ListxattrBuf::Owned { .. } => {
out.field("names", &names);
},
ListxattrBuf::Borrowed { .. } => {
out.field("names", &names);
},
}
out.finish()
}
}
struct XattrNamesIter<'a>(&'a [u8]);
impl<'a> XattrNamesIter<'a> {
fn new(buf: &'a ListxattrBuf) -> XattrNamesIter<'a> {
XattrNamesIter(match buf {
ListxattrBuf::SizeOnly { .. } => &[],
#[cfg(feature = "std")]
ListxattrBuf::Owned { cap, .. } => cap.as_ref(),
ListxattrBuf::Borrowed { cap, size } => {
let (bytes, _) = cap.split_at(*size);
bytes
},
})
}
}
impl<'a> core::iter::Iterator for XattrNamesIter<'a> {
type Item = &'a XattrName;
fn next(&mut self) -> Option<&'a XattrName> {
let len = self.0.len();
if len == 0 {
return None;
}
for ii in 0..len {
if self.0[ii] == 0 {
let (name, _) = self.0.split_at(ii);
let (_, next) = self.0.split_at(ii + 1);
self.0 = next;
return Some(XattrName::new_unchecked(name));
}
}
let name = XattrName::new_unchecked(self.0);
self.0 = &[];
Some(name)
}
}
struct DebugListxattrNames<'a>(&'a ListxattrBuf<'a>);
impl fmt::Debug for DebugListxattrNames<'_> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_list()
.entries(XattrNamesIter::new(self.0))
.finish()
}
}
impl fuse_io::EncodeResponse for ListxattrResponse<'_> {
fn encode_response<'a, Chan: fuse_io::Channel>(
&'a self,
enc: fuse_io::ResponseEncoder<Chan>,
) -> Result<(), Chan::Error> {
match &self.buf {
ListxattrBuf::SizeOnly { size } => {
enc.encode_sized(&fuse_kernel::fuse_getxattr_out {
size: *size,
padding: 0,
})
},
#[cfg(feature = "std")]
ListxattrBuf::Owned { cap, .. } => enc.encode_bytes(&cap),
ListxattrBuf::Borrowed { cap, size } => {
let (bytes, _) = cap.split_at(*size);
enc.encode_bytes(bytes)
},
}
}
}