forked from Stebalien/xattr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsd.rs
322 lines (290 loc) · 8.88 KB
/
bsd.rs
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//! FreeBSD and NetBSD xattr support.
use std::ffi::{CString, OsStr, OsString};
use std::io;
use std::mem;
use std::os::unix::ffi::{OsStrExt, OsStringExt};
use std::os::unix::io::RawFd;
use std::path::Path;
use libc::{c_int, c_void, size_t, EPERM};
use util::{allocate_loop, path_to_c};
use libc::{extattr_delete_fd, extattr_get_fd, extattr_list_fd, extattr_set_fd,
extattr_delete_link, extattr_get_link, extattr_list_link, extattr_set_link,
EXTATTR_NAMESPACE_USER, EXTATTR_NAMESPACE_SYSTEM
};
const EXTATTR_NAMESPACE_USER_STRING: &'static str = "user";
const EXTATTR_NAMESPACE_SYSTEM_STRING: &'static str = "system";
const EXTATTR_NAMESPACE_NAMES: [&'static str; 3] = [
"empty",
EXTATTR_NAMESPACE_USER_STRING,
EXTATTR_NAMESPACE_SYSTEM_STRING,
];
/// An iterator over a set of extended attributes names.
pub struct XAttrs {
user_attrs: Box<[u8]>,
system_attrs: Box<[u8]>,
offset: usize,
}
impl Clone for XAttrs {
fn clone(&self) -> Self {
XAttrs {
user_attrs: Vec::from(&*self.user_attrs).into_boxed_slice(),
system_attrs: Vec::from(&*self.system_attrs).into_boxed_slice(),
offset: self.offset,
}
}
fn clone_from(&mut self, other: &XAttrs) {
self.offset = other.offset;
let mut data = mem::replace(&mut self.user_attrs, Box::new([])).into_vec();
data.extend(other.user_attrs.iter().cloned());
self.user_attrs = data.into_boxed_slice();
data = mem::replace(&mut self.system_attrs, Box::new([])).into_vec();
data.extend(other.system_attrs.iter().cloned());
self.system_attrs = data.into_boxed_slice();
}
}
impl Iterator for XAttrs {
type Item = OsString;
fn next(&mut self) -> Option<OsString> {
if self.user_attrs.is_empty() && self.system_attrs.is_empty() {
return None;
}
if self.offset == self.user_attrs.len() + self.system_attrs.len() {
return None;
}
let data = if self.offset < self.system_attrs.len() {
&self.system_attrs[self.offset..]
} else {
&self.user_attrs[self.offset - self.system_attrs.len()..]
};
let siz = data[0] as usize;
self.offset += siz + 1;
if self.offset < self.system_attrs.len() {
Some(prefix_namespace(
OsStr::from_bytes(&data[1..siz + 1]),
EXTATTR_NAMESPACE_SYSTEM,
))
} else {
Some(prefix_namespace(
OsStr::from_bytes(&data[1..siz + 1]),
EXTATTR_NAMESPACE_USER,
))
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
if self.user_attrs.len() + self.system_attrs.len() == self.offset {
(0, Some(0))
} else {
(1, None)
}
}
}
// This could use libc::extattr_string_to_namespace, but it's awkward because
// that requires nul-terminated strings, which Rust's std is loathe to provide.
fn name_to_ns(name: &OsStr) -> io::Result<(c_int, CString)> {
let mut groups = name.as_bytes().splitn(2, |&b| b == b'.').take(2);
let nsname = match groups.next() {
Some(s) => s,
None => {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"couldn't find namespace",
))
}
};
let propname = match groups.next() {
Some(s) => s,
None => {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"couldn't find attribute",
))
}
};
let ns_int = match EXTATTR_NAMESPACE_NAMES
.iter()
.position(|&s| s.as_bytes() == nsname)
{
Some(i) => i,
None => {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"no matching namespace",
))
}
};
Ok((ns_int as c_int, CString::new(propname)?))
}
fn prefix_namespace(attr: &OsStr, ns: c_int) -> OsString {
let nsname = EXTATTR_NAMESPACE_NAMES[ns as usize];
let mut v = Vec::with_capacity(nsname.as_bytes().len() + attr.as_bytes().len() + 1);
v.extend(nsname.as_bytes());
v.extend(".".as_bytes());
v.extend(attr.as_bytes());
OsString::from_vec(v)
}
pub fn get_fd(fd: RawFd, name: &OsStr) -> io::Result<Vec<u8>> {
let (ns, name) = name_to_ns(name)?;
unsafe {
allocate_loop(|buf, len| {
extattr_get_fd(fd, ns, name.as_ptr(), buf as *mut c_void, len as size_t)
})
}
}
pub fn set_fd(fd: RawFd, name: &OsStr, value: &[u8]) -> io::Result<()> {
let (ns, name) = name_to_ns(name)?;
let ret = unsafe {
extattr_set_fd(
fd,
ns,
name.as_ptr(),
value.as_ptr() as *const c_void,
value.len() as size_t,
)
};
if ret == -1 {
Err(io::Error::last_os_error())
} else {
Ok(())
}
}
pub fn remove_fd(fd: RawFd, name: &OsStr) -> io::Result<()> {
let (ns, name) = name_to_ns(name)?;
let ret = unsafe { extattr_delete_fd(fd, ns, name.as_ptr()) };
if ret != 0 {
Err(io::Error::last_os_error())
} else {
Ok(())
}
}
pub fn list_fd(fd: RawFd) -> io::Result<XAttrs> {
let sysvec = unsafe {
let res = allocate_loop(|buf, len| {
extattr_list_fd(
fd,
EXTATTR_NAMESPACE_SYSTEM,
buf as *mut c_void,
len as size_t,
)
});
// On FreeBSD, system attributes require root privileges to view. However,
// to mimic the behavior of listxattr in linux and osx, we need to query
// them anyway and return empty results if we get EPERM
match res {
Ok(v) => v,
Err(err) => {
if err.raw_os_error() == Some(EPERM) {
Vec::new()
} else {
return Err(err);
}
}
}
};
let uservec = unsafe {
let res = allocate_loop(|buf, len| {
extattr_list_fd(
fd,
EXTATTR_NAMESPACE_USER,
buf as *mut c_void,
len as size_t,
)
});
match res {
Ok(v) => v,
Err(err) => return Err(err),
}
};
Ok(XAttrs {
system_attrs: sysvec.into_boxed_slice(),
user_attrs: uservec.into_boxed_slice(),
offset: 0,
})
}
pub fn get_path(path: &Path, name: &OsStr) -> io::Result<Vec<u8>> {
let (ns, name) = name_to_ns(name)?;
let path = path_to_c(path)?;
unsafe {
allocate_loop(|buf, len| {
extattr_get_link(
path.as_ptr(),
ns,
name.as_ptr(),
buf as *mut c_void,
len as size_t,
)
})
}
}
pub fn set_path(path: &Path, name: &OsStr, value: &[u8]) -> io::Result<()> {
let (ns, name) = name_to_ns(name)?;
let path = path_to_c(path)?;
let ret = unsafe {
extattr_set_link(
path.as_ptr(),
ns,
name.as_ptr(),
value.as_ptr() as *const c_void,
value.len() as size_t,
)
};
if ret == -1 {
Err(io::Error::last_os_error())
} else {
Ok(())
}
}
pub fn remove_path(path: &Path, name: &OsStr) -> io::Result<()> {
let (ns, name) = name_to_ns(name)?;
let path = path_to_c(path)?;
let ret = unsafe { extattr_delete_link(path.as_ptr(), ns, name.as_ptr()) };
if ret != 0 {
Err(io::Error::last_os_error())
} else {
Ok(())
}
}
pub fn list_path(path: &Path) -> io::Result<XAttrs> {
let path = path_to_c(path)?;
let sysvec = unsafe {
let res = allocate_loop(|buf, len| {
extattr_list_link(
path.as_ptr(),
EXTATTR_NAMESPACE_SYSTEM,
buf as *mut c_void,
len as size_t,
)
});
// On FreeBSD, system attributes require root privileges to view. However,
// to mimic the behavior of listxattr in linux and osx, we need to query
// them anyway and return empty results if we get EPERM
match res {
Ok(v) => v,
Err(err) => {
if err.raw_os_error() == Some(EPERM) {
Vec::new()
} else {
return Err(err);
}
}
}
};
let uservec = unsafe {
let res = allocate_loop(|buf, len| {
extattr_list_link(
path.as_ptr(),
EXTATTR_NAMESPACE_USER,
buf as *mut c_void,
len as size_t,
)
});
match res {
Ok(v) => v,
Err(err) => return Err(err),
}
};
Ok(XAttrs {
system_attrs: sysvec.into_boxed_slice(),
user_attrs: uservec.into_boxed_slice(),
offset: 0,
})
}