2015-08-25 17:29:23 +00:00
|
|
|
//! Extended attribute support for Darwin and Linux systems.
|
2020-10-10 18:49:46 +00:00
|
|
|
|
2016-03-18 12:19:51 +00:00
|
|
|
#![allow(trivial_casts)] // for ARM
|
2015-08-25 17:29:23 +00:00
|
|
|
|
2020-04-19 04:33:42 +00:00
|
|
|
use std::cmp::Ordering;
|
2015-08-25 17:29:23 +00:00
|
|
|
use std::io;
|
|
|
|
use std::path::Path;
|
|
|
|
|
2020-10-10 18:49:46 +00:00
|
|
|
|
|
|
|
pub const ENABLED: bool =
|
|
|
|
cfg!(feature="git") &&
|
|
|
|
cfg!(any(target_os = "macos", target_os = "linux"));
|
|
|
|
|
2015-08-25 17:29:23 +00:00
|
|
|
|
|
|
|
pub trait FileAttributes {
|
|
|
|
fn attributes(&self) -> io::Result<Vec<Attribute>>;
|
|
|
|
fn symlink_attributes(&self) -> io::Result<Vec<Attribute>>;
|
|
|
|
}
|
|
|
|
|
2015-09-13 23:37:52 +00:00
|
|
|
#[cfg(any(target_os = "macos", target_os = "linux"))]
|
2015-08-25 17:29:23 +00:00
|
|
|
impl FileAttributes for Path {
|
|
|
|
fn attributes(&self) -> io::Result<Vec<Attribute>> {
|
2017-03-31 16:09:50 +00:00
|
|
|
list_attrs(&lister::Lister::new(FollowSymlinks::Yes), self)
|
2015-08-25 17:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn symlink_attributes(&self) -> io::Result<Vec<Attribute>> {
|
2017-03-31 16:09:50 +00:00
|
|
|
list_attrs(&lister::Lister::new(FollowSymlinks::No), self)
|
2015-08-25 17:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-13 23:37:52 +00:00
|
|
|
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
|
|
|
|
impl FileAttributes for Path {
|
|
|
|
fn attributes(&self) -> io::Result<Vec<Attribute>> {
|
2020-10-10 18:49:46 +00:00
|
|
|
Ok(Vec::new())
|
2015-09-13 23:37:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn symlink_attributes(&self) -> io::Result<Vec<Attribute>> {
|
2020-10-10 18:49:46 +00:00
|
|
|
Ok(Vec::new())
|
2015-09-13 23:37:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-10 18:49:46 +00:00
|
|
|
|
2015-08-25 17:29:23 +00:00
|
|
|
/// Attributes which can be passed to `Attribute::list_with_flags`
|
2015-09-13 23:37:52 +00:00
|
|
|
#[cfg(any(target_os = "macos", target_os = "linux"))]
|
2015-08-25 17:29:23 +00:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub enum FollowSymlinks {
|
|
|
|
Yes,
|
2020-10-10 18:49:46 +00:00
|
|
|
No,
|
2015-08-25 17:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Extended attribute
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Attribute {
|
|
|
|
pub name: String,
|
|
|
|
pub size: usize,
|
|
|
|
}
|
|
|
|
|
2020-10-10 18:49:46 +00:00
|
|
|
|
2015-09-13 23:37:52 +00:00
|
|
|
#[cfg(any(target_os = "macos", target_os = "linux"))]
|
2017-03-31 16:09:50 +00:00
|
|
|
pub fn list_attrs(lister: &lister::Lister, path: &Path) -> io::Result<Vec<Attribute>> {
|
2015-12-14 03:13:40 +00:00
|
|
|
use std::ffi::CString;
|
|
|
|
|
2020-10-10 18:49:46 +00:00
|
|
|
let c_path = match path.to_str().and_then(|s| CString::new(s).ok()) {
|
2015-08-25 17:29:23 +00:00
|
|
|
Some(cstring) => cstring,
|
2020-10-10 18:49:46 +00:00
|
|
|
None => {
|
|
|
|
return Err(io::Error::new(io::ErrorKind::Other, "Error: path somehow contained a NUL?"));
|
|
|
|
}
|
2015-08-25 17:29:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let bufsize = lister.listxattr_first(&c_path);
|
2020-04-19 04:33:42 +00:00
|
|
|
match bufsize.cmp(&0) {
|
2020-10-10 18:49:46 +00:00
|
|
|
Ordering::Less => return Err(io::Error::last_os_error()),
|
|
|
|
Ordering::Equal => return Ok(Vec::new()),
|
|
|
|
Ordering::Greater => {},
|
2015-08-25 20:08:25 +00:00
|
|
|
}
|
2015-08-25 17:29:23 +00:00
|
|
|
|
2020-10-10 14:57:40 +00:00
|
|
|
let mut buf = vec![0_u8; bufsize as usize];
|
2020-04-19 04:33:42 +00:00
|
|
|
let err = lister.listxattr_second(&c_path, &mut buf, bufsize);
|
2015-08-25 20:08:25 +00:00
|
|
|
|
2020-04-19 04:33:42 +00:00
|
|
|
match err.cmp(&0) {
|
2020-10-10 18:49:46 +00:00
|
|
|
Ordering::Less => return Err(io::Error::last_os_error()),
|
|
|
|
Ordering::Equal => return Ok(Vec::new()),
|
|
|
|
Ordering::Greater => {},
|
2020-04-19 04:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut names = Vec::new();
|
|
|
|
if err > 0 {
|
|
|
|
// End indices of the attribute names
|
|
|
|
// the buffer contains 0-terminated c-strings
|
|
|
|
let idx = buf.iter().enumerate().filter_map(|(i, v)|
|
|
|
|
if *v == 0 { Some(i) } else { None }
|
|
|
|
);
|
|
|
|
let mut start = 0;
|
|
|
|
|
|
|
|
for end in idx {
|
|
|
|
let c_end = end + 1; // end of the c-string (including 0)
|
|
|
|
let size = lister.getxattr(&c_path, &buf[start..c_end]);
|
|
|
|
|
|
|
|
if size > 0 {
|
|
|
|
names.push(Attribute {
|
|
|
|
name: lister.translate_attribute_name(&buf[start..end]),
|
2020-10-10 18:49:46 +00:00
|
|
|
size: size as usize,
|
2020-04-19 04:33:42 +00:00
|
|
|
});
|
2015-08-25 17:29:23 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 04:33:42 +00:00
|
|
|
start = c_end;
|
2015-08-25 17:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-10 18:49:46 +00:00
|
|
|
|
2015-08-25 20:08:25 +00:00
|
|
|
Ok(names)
|
2015-08-25 17:29:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-10 18:49:46 +00:00
|
|
|
|
2015-08-25 17:29:23 +00:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
mod lister {
|
|
|
|
use super::FollowSymlinks;
|
2020-10-10 18:49:46 +00:00
|
|
|
use libc::{c_int, size_t, ssize_t, c_char, c_void};
|
|
|
|
use std::ffi::CString;
|
2015-08-25 17:29:23 +00:00
|
|
|
use std::ptr;
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
fn listxattr(
|
2020-10-10 18:49:46 +00:00
|
|
|
path: *const c_char,
|
|
|
|
namebuf: *mut c_char,
|
|
|
|
size: size_t,
|
|
|
|
options: c_int,
|
2015-08-25 17:29:23 +00:00
|
|
|
) -> ssize_t;
|
|
|
|
|
|
|
|
fn getxattr(
|
2020-10-10 18:49:46 +00:00
|
|
|
path: *const c_char,
|
|
|
|
name: *const c_char,
|
|
|
|
value: *mut c_void,
|
|
|
|
size: size_t,
|
|
|
|
position: u32,
|
|
|
|
options: c_int,
|
2015-08-25 17:29:23 +00:00
|
|
|
) -> ssize_t;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Lister {
|
|
|
|
c_flags: c_int,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Lister {
|
2020-10-10 12:55:26 +00:00
|
|
|
pub fn new(do_follow: FollowSymlinks) -> Self {
|
2015-08-25 17:29:23 +00:00
|
|
|
let c_flags: c_int = match do_follow {
|
2020-10-10 18:49:46 +00:00
|
|
|
FollowSymlinks::Yes => 0x0001,
|
|
|
|
FollowSymlinks::No => 0x0000,
|
2015-08-25 17:29:23 +00:00
|
|
|
};
|
|
|
|
|
2020-10-10 12:55:26 +00:00
|
|
|
Self { c_flags }
|
2015-08-25 17:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn translate_attribute_name(&self, input: &[u8]) -> String {
|
2020-10-10 18:49:46 +00:00
|
|
|
unsafe { std::str::from_utf8_unchecked(input).into() }
|
2015-08-25 17:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn listxattr_first(&self, c_path: &CString) -> ssize_t {
|
|
|
|
unsafe {
|
2020-10-10 18:49:46 +00:00
|
|
|
listxattr(
|
|
|
|
c_path.as_ptr(),
|
|
|
|
ptr::null_mut(),
|
|
|
|
0,
|
|
|
|
self.c_flags,
|
|
|
|
)
|
2015-08-25 17:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn listxattr_second(&self, c_path: &CString, buf: &mut Vec<u8>, bufsize: ssize_t) -> ssize_t {
|
|
|
|
unsafe {
|
|
|
|
listxattr(
|
|
|
|
c_path.as_ptr(),
|
|
|
|
buf.as_mut_ptr() as *mut c_char,
|
2020-10-10 18:49:46 +00:00
|
|
|
bufsize as size_t,
|
|
|
|
self.c_flags,
|
2015-08-25 17:29:23 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn getxattr(&self, c_path: &CString, buf: &[u8]) -> ssize_t {
|
|
|
|
unsafe {
|
|
|
|
getxattr(
|
|
|
|
c_path.as_ptr(),
|
|
|
|
buf.as_ptr() as *const c_char,
|
2020-10-10 18:49:46 +00:00
|
|
|
ptr::null_mut(),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
self.c_flags,
|
2015-08-25 17:29:23 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-10 18:49:46 +00:00
|
|
|
|
2015-08-25 17:29:23 +00:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
mod lister {
|
|
|
|
use std::ffi::CString;
|
|
|
|
use libc::{size_t, ssize_t, c_char, c_void};
|
|
|
|
use super::FollowSymlinks;
|
|
|
|
use std::ptr;
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
fn listxattr(
|
2020-10-10 18:49:46 +00:00
|
|
|
path: *const c_char,
|
|
|
|
list: *mut c_char,
|
|
|
|
size: size_t,
|
2015-08-25 17:29:23 +00:00
|
|
|
) -> ssize_t;
|
|
|
|
|
|
|
|
fn llistxattr(
|
2020-10-10 18:49:46 +00:00
|
|
|
path: *const c_char,
|
|
|
|
list: *mut c_char,
|
|
|
|
size: size_t,
|
2015-08-25 17:29:23 +00:00
|
|
|
) -> ssize_t;
|
|
|
|
|
|
|
|
fn getxattr(
|
2020-10-10 18:49:46 +00:00
|
|
|
path: *const c_char,
|
|
|
|
name: *const c_char,
|
|
|
|
value: *mut c_void,
|
|
|
|
size: size_t,
|
2015-08-25 17:29:23 +00:00
|
|
|
) -> ssize_t;
|
|
|
|
|
|
|
|
fn lgetxattr(
|
2020-10-10 18:49:46 +00:00
|
|
|
path: *const c_char,
|
|
|
|
name: *const c_char,
|
|
|
|
value: *mut c_void,
|
|
|
|
size: size_t,
|
2015-08-25 17:29:23 +00:00
|
|
|
) -> ssize_t;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Lister {
|
|
|
|
follow_symlinks: FollowSymlinks,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Lister {
|
|
|
|
pub fn new(follow_symlinks: FollowSymlinks) -> Lister {
|
2018-06-19 12:58:03 +00:00
|
|
|
Lister { follow_symlinks }
|
2015-08-25 17:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn translate_attribute_name(&self, input: &[u8]) -> String {
|
|
|
|
String::from_utf8_lossy(input).into_owned()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn listxattr_first(&self, c_path: &CString) -> ssize_t {
|
|
|
|
let listxattr = match self.follow_symlinks {
|
2020-10-10 18:49:46 +00:00
|
|
|
FollowSymlinks::Yes => listxattr,
|
|
|
|
FollowSymlinks::No => llistxattr,
|
2015-08-25 17:29:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
unsafe {
|
2020-10-10 18:49:46 +00:00
|
|
|
listxattr(
|
|
|
|
c_path.as_ptr() as *const _,
|
|
|
|
ptr::null_mut(),
|
|
|
|
0,
|
|
|
|
)
|
2015-08-25 17:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn listxattr_second(&self, c_path: &CString, buf: &mut Vec<u8>, bufsize: ssize_t) -> ssize_t {
|
|
|
|
let listxattr = match self.follow_symlinks {
|
2020-10-10 18:49:46 +00:00
|
|
|
FollowSymlinks::Yes => listxattr,
|
|
|
|
FollowSymlinks::No => llistxattr,
|
2015-08-25 17:29:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
listxattr(
|
2015-12-11 04:50:20 +00:00
|
|
|
c_path.as_ptr() as *const _,
|
2015-08-25 17:29:23 +00:00
|
|
|
buf.as_mut_ptr() as *mut c_char,
|
2020-10-10 18:49:46 +00:00
|
|
|
bufsize as size_t,
|
2015-08-25 17:29:23 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn getxattr(&self, c_path: &CString, buf: &[u8]) -> ssize_t {
|
|
|
|
let getxattr = match self.follow_symlinks {
|
2020-10-10 18:49:46 +00:00
|
|
|
FollowSymlinks::Yes => getxattr,
|
|
|
|
FollowSymlinks::No => lgetxattr,
|
2015-08-25 17:29:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
getxattr(
|
2015-12-11 04:50:20 +00:00
|
|
|
c_path.as_ptr() as *const _,
|
2015-08-25 17:29:23 +00:00
|
|
|
buf.as_ptr() as *const c_char,
|
2020-10-10 18:49:46 +00:00
|
|
|
ptr::null_mut(),
|
|
|
|
0,
|
2015-08-25 17:29:23 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-13 23:37:52 +00:00
|
|
|
}
|