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 Linux and fullrust.
//!
//! The constants are those of `include/uapi/linux/xattr.h`, which every
//! architecture shares.

use crate::fs::unix::XattrMode;
use crate::path::with_cstr;
use crate::sys::{errno, syscall};
use crate::{BorrowedHandle, Result};

const XATTR_CREATE: usize = 1;
const XATTR_REPLACE: usize = 2;

/// The `XATTR_*` flag of `mode`.
fn flags(mode: XattrMode) -> usize {
    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(result: Result<usize>) -> Result<Option<usize>> {
    match result {
        Ok(len) => Ok(Some(len)),
        Err(error) if error.raw_os_error() == errno::ENODATA => 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| {
            let (path, name) = (path.as_ptr(), name.as_ptr());
            let (value, len) = (buf.as_mut_ptr(), buf.len());
            // SAFETY: both strings are NUL-terminated, and the kernel
            // writes at most `len` bytes to `buf` (none when it is empty:
            // it then only says how long the value is).
            value_len(unsafe {
                if follow {
                    syscall!(GETXATTR, path, name, value, len)
                } else {
                    syscall!(LGETXATTR, path, name, value, len)
                }
            })
        })
    })
}

pub(crate) fn fget(
    handle: BorrowedHandle<'_>,
    name: &[u8],
    buf: &mut [u8],
) -> Result<Option<usize>> {
    with_cstr(name, |name| {
        let (value, len) = (buf.as_mut_ptr(), buf.len());
        // SAFETY: as for `get`.
        value_len(unsafe { syscall!(FGETXATTR, handle.as_raw(), name.as_ptr(), value, len) })
    })
}

pub(crate) fn set(
    path: &[u8],
    name: &[u8],
    value: &[u8],
    mode: XattrMode,
    follow: bool,
) -> Result<()> {
    let flags = flags(mode);
    with_cstr(path, |path| {
        with_cstr(name, |name| {
            let (path, name) = (path.as_ptr(), name.as_ptr());
            let (data, len) = (value.as_ptr(), value.len());
            // SAFETY: both strings are NUL-terminated, and the kernel
            // reads `len` bytes of `value`.
            unsafe {
                if follow {
                    syscall!(SETXATTR, path, name, data, len, flags)
                } else {
                    syscall!(LSETXATTR, path, name, data, len, flags)
                }
            }
            .map(drop)
        })
    })
}

pub(crate) fn fset(
    handle: BorrowedHandle<'_>,
    name: &[u8],
    value: &[u8],
    mode: XattrMode,
) -> Result<()> {
    let flags = flags(mode);
    with_cstr(name, |name| {
        let (data, len) = (value.as_ptr(), value.len());
        // SAFETY: as for `set`.
        unsafe { syscall!(FSETXATTR, handle.as_raw(), name.as_ptr(), data, len, flags) }.map(drop)
    })
}

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.
            unsafe {
                if follow {
                    syscall!(REMOVEXATTR, path.as_ptr(), name.as_ptr())
                } else {
                    syscall!(LREMOVEXATTR, path.as_ptr(), name.as_ptr())
                }
            }
            .map(drop)
        })
    })
}

pub(crate) fn fremove(handle: BorrowedHandle<'_>, name: &[u8]) -> Result<()> {
    with_cstr(name, |name| {
        // SAFETY: the name is a NUL-terminated string.
        unsafe { syscall!(FREMOVEXATTR, handle.as_raw(), name.as_ptr()) }.map(drop)
    })
}

pub(crate) fn list(path: &[u8], buf: &mut [u8], follow: bool) -> Result<usize> {
    with_cstr(path, |path| {
        let (names, len) = (buf.as_mut_ptr(), buf.len());
        // SAFETY: the path is a NUL-terminated string, and the kernel
        // writes at most `len` bytes to `buf`.
        unsafe {
            if follow {
                syscall!(LISTXATTR, path.as_ptr(), names, len)
            } else {
                syscall!(LLISTXATTR, path.as_ptr(), names, len)
            }
        }
    })
}

pub(crate) fn flist(handle: BorrowedHandle<'_>, buf: &mut [u8]) -> Result<usize> {
    // SAFETY: the kernel writes at most `buf.len()` bytes to `buf`.
    unsafe { syscall!(FLISTXATTR, handle.as_raw(), buf.as_mut_ptr(), buf.len()) }
}