use core::ffi::{c_char, c_int, c_void};
use crate::fs::unix::XattrMode;
use crate::path::with_cstr;
use crate::sys::{cvt, cvt_long, errno};
use crate::{BorrowedHandle, Result};
const XATTR_NOFOLLOW: c_int = 0x1;
const XATTR_CREATE: c_int = 0x2;
const XATTR_REPLACE: c_int = 0x4;
unsafe extern "C" {
fn getxattr(
path: *const c_char,
name: *const c_char,
value: *mut c_void,
size: usize,
position: u32,
options: c_int,
) -> isize;
fn fgetxattr(
fd: c_int,
name: *const c_char,
value: *mut c_void,
size: usize,
position: u32,
options: c_int,
) -> isize;
fn setxattr(
path: *const c_char,
name: *const c_char,
value: *const c_void,
size: usize,
position: u32,
options: c_int,
) -> c_int;
fn fsetxattr(
fd: c_int,
name: *const c_char,
value: *const c_void,
size: usize,
position: u32,
options: c_int,
) -> c_int;
fn removexattr(path: *const c_char, name: *const c_char, options: c_int) -> c_int;
fn fremovexattr(fd: c_int, name: *const c_char, options: c_int) -> c_int;
fn listxattr(path: *const c_char, names: *mut c_char, size: usize, options: c_int) -> isize;
fn flistxattr(fd: c_int, names: *mut c_char, size: usize, options: c_int) -> isize;
}
fn out(buf: &mut [u8]) -> *mut u8 {
if buf.is_empty() {
core::ptr::null_mut()
} else {
buf.as_mut_ptr()
}
}
fn follow_option(follow: bool) -> c_int {
if follow { 0 } else { XATTR_NOFOLLOW }
}
fn mode_option(mode: XattrMode) -> c_int {
match mode {
XattrMode::Any => 0,
XattrMode::Create => XATTR_CREATE,
XattrMode::Replace => XATTR_REPLACE,
}
}
fn value_len(ret: isize) -> Result<Option<usize>> {
match cvt_long(ret as i64) {
Ok(len) => Ok(Some(len as usize)),
Err(error) if error.raw_os_error() == errno::ENOATTR => Ok(None),
Err(error) => Err(error),
}
}
pub(crate) fn get(path: &[u8], name: &[u8], buf: &mut [u8], follow: bool) -> Result<Option<usize>> {
with_cstr(path, |path| {
with_cstr(name, |name| {
value_len(unsafe {
getxattr(
path.as_ptr(),
name.as_ptr(),
out(buf).cast(),
buf.len(),
0,
follow_option(follow),
)
})
})
})
}
pub(crate) fn fget(
handle: BorrowedHandle<'_>,
name: &[u8],
buf: &mut [u8],
) -> Result<Option<usize>> {
with_cstr(name, |name| {
value_len(unsafe {
fgetxattr(
handle.as_raw(),
name.as_ptr(),
out(buf).cast(),
buf.len(),
0,
0,
)
})
})
}
pub(crate) fn set(
path: &[u8],
name: &[u8],
value: &[u8],
mode: XattrMode,
follow: bool,
) -> Result<()> {
let options = mode_option(mode) | follow_option(follow);
with_cstr(path, |path| {
with_cstr(name, |name| {
cvt(unsafe {
setxattr(
path.as_ptr(),
name.as_ptr(),
value.as_ptr().cast(),
value.len(),
0,
options,
)
})?;
Ok(())
})
})
}
pub(crate) fn fset(
handle: BorrowedHandle<'_>,
name: &[u8],
value: &[u8],
mode: XattrMode,
) -> Result<()> {
with_cstr(name, |name| {
cvt(unsafe {
fsetxattr(
handle.as_raw(),
name.as_ptr(),
value.as_ptr().cast(),
value.len(),
0,
mode_option(mode),
)
})?;
Ok(())
})
}
pub(crate) fn remove(path: &[u8], name: &[u8], follow: bool) -> Result<()> {
with_cstr(path, |path| {
with_cstr(name, |name| {
cvt(unsafe { removexattr(path.as_ptr(), name.as_ptr(), follow_option(follow)) })?;
Ok(())
})
})
}
pub(crate) fn fremove(handle: BorrowedHandle<'_>, name: &[u8]) -> Result<()> {
with_cstr(name, |name| {
cvt(unsafe { fremovexattr(handle.as_raw(), name.as_ptr(), 0) })?;
Ok(())
})
}
pub(crate) fn list(path: &[u8], buf: &mut [u8], follow: bool) -> Result<usize> {
with_cstr(path, |path| {
let len = unsafe {
listxattr(
path.as_ptr(),
out(buf).cast(),
buf.len(),
follow_option(follow),
)
};
Ok(cvt_long(len as i64)? as usize)
})
}
pub(crate) fn flist(handle: BorrowedHandle<'_>, buf: &mut [u8]) -> Result<usize> {
let len = unsafe { flistxattr(handle.as_raw(), out(buf).cast(), buf.len(), 0) };
Ok(cvt_long(len as i64)? as usize)
}