oscall 0.1.1

Low-level cross-platform interface to operating system services: files, memory mapping, networking and more, with no libc on Linux
Documentation
//! `fs::unix`: extended attributes, on macOS.
//!
//! The functions of `<sys/xattr.h>` take two arguments Linux's do not: a
//! position in the value, which only the resource fork of a file uses, and
//! options, where "do not follow a symbolic link" is said instead of in the
//! name of the function.

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;
}

/// Where the system is to write: nowhere for an empty buffer, which is how
/// it is asked for a length. (A buffer of no bytes that is somewhere is
/// one that is too short: `ERANGE`, measured.)
fn out(buf: &mut [u8]) -> *mut u8 {
    if buf.is_empty() {
        core::ptr::null_mut()
    } else {
        buf.as_mut_ptr()
    }
}

/// The option of a call on a path.
fn follow_option(follow: bool) -> c_int {
    if follow { 0 } else { XATTR_NOFOLLOW }
}

/// The `XATTR_*` option of `mode`.
fn mode_option(mode: XattrMode) -> c_int {
    match mode {
        XattrMode::Any => 0,
        XattrMode::Create => XATTR_CREATE,
        XattrMode::Replace => XATTR_REPLACE,
    }
}

/// The length of a value read, or `None` when the system says there is no
/// such attribute.
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| {
            // SAFETY: both strings are NUL-terminated, and the system
            // writes at most `buf.len()` bytes to `buf` (none when it is
            // empty: it then only says how long the value is).
            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| {
        // SAFETY: as for `get`.
        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| {
            // SAFETY: both strings are NUL-terminated, and the system
            // reads `value.len()` bytes of `value`.
            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| {
        // SAFETY: as for `set`.
        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| {
            // SAFETY: both strings are NUL-terminated.
            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| {
        // SAFETY: the name is a NUL-terminated string.
        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| {
        // SAFETY: the path is a NUL-terminated string, and the system
        // writes at most `buf.len()` bytes to `buf`.
        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> {
    // SAFETY: the system writes at most `buf.len()` bytes to `buf`.
    let len = unsafe { flistxattr(handle.as_raw(), out(buf).cast(), buf.len(), 0) };
    Ok(cvt_long(len as i64)? as usize)
}