Debian Codegraph @

debian/rust-vendor/xattr/src/sys/linux_macos.rs

raw
1 use std::ffi::{OsStr, OsString};
2 use std::io;
3 use std::os::unix::ffi::OsStrExt;
4 use std::os::unix::io::BorrowedFd;
5 use std::path::Path;
6
7 use rustix::fs as rfs;
8
9 #[cfg(not(target_os = "macos"))]
10 pub const ENOATTR: i32 = rustix::io::Errno::NODATA.raw_os_error();
11
12 #[cfg(target_os = "macos")]
13 pub const ENOATTR: i32 = rustix::io::Errno::NOATTR.raw_os_error();
14
15 pub const ERANGE: i32 = rustix::io::Errno::RANGE.raw_os_error();
16
17 /// An iterator over a set of extended attributes names.
18 #[derive(Default, Clone)]
19 pub struct XAttrs {
20 data: Box<[u8]>,
21 offset: usize,
22 }
23
24 // Yes, I could avoid these allocations on linux/macos. However, if we ever want to be freebsd
25 // compatible, we need to be able to prepend the namespace to the extended attribute names.
26 // Furthermore, borrowing makes the API messy.
27 impl Iterator for XAttrs {
28 type Item = OsString;
29 fn next(&mut self) -> Option<OsString> {
30 let data = &self.data[self.offset..];
31 if data.is_empty() {
32 None
33 } else {
34 // always null terminated (unless empty).
35 let end = data.iter().position(|&b| b == 0u8).unwrap();
36 self.offset += end + 1;
37 Some(OsStr::from_bytes(&data[..end]).to_owned())
38 }
39 }
40
41 fn size_hint(&self) -> (usize, Option<usize>) {
42 if self.data.len() == self.offset {
43 (0, Some(0))
44 } else {
45 (1, None)
46 }
47 }
48 }
49
50 /// A macro to abstract away some of the boilerplate when calling `allocate_loop` with rustix
51 /// functions. Unfortunately, we can't write this as a helper function because I need to call
52 /// generic rustix function with two different types.
53 macro_rules! allocate_loop {
54 (|$buf:ident| $($e:tt)*) => {
55 crate::util::allocate_loop(
56 |$buf| Ok($($e)*?.0),
57 || {
58 let $buf: &mut [u8] = &mut [];
59 Ok($($e)*?)
60 },
61 )
62 };
63 }
64
65 pub fn get_fd(fd: BorrowedFd<'_>, name: &OsStr) -> io::Result<Vec<u8>> {
66 allocate_loop!(|buf| rfs::fgetxattr(fd, name, buf))
67 }
68
69 pub fn set_fd(fd: BorrowedFd<'_>, name: &OsStr, value: &[u8]) -> io::Result<()> {
70 rfs::fsetxattr(fd, name, value, rfs::XattrFlags::empty())?;
71 Ok(())
72 }
73
74 pub fn remove_fd(fd: BorrowedFd<'_>, name: &OsStr) -> io::Result<()> {
75 rfs::fremovexattr(fd, name)?;
76 Ok(())
77 }
78
79 pub fn list_fd(fd: BorrowedFd<'_>) -> io::Result<XAttrs> {
80 let vec = allocate_loop!(|buf| rfs::flistxattr(fd, buf))?;
81 Ok(XAttrs {
82 data: vec.into_boxed_slice(),
83 offset: 0,
84 })
85 }
86
87 pub fn get_path(path: &Path, name: &OsStr, deref: bool) -> io::Result<Vec<u8>> {
88 if deref {
89 allocate_loop!(|buf| rfs::getxattr(path, name, buf))
90 } else {
91 allocate_loop!(|buf| rfs::lgetxattr(path, name, buf))
92 }
93 }
94
95 pub fn set_path(path: &Path, name: &OsStr, value: &[u8], deref: bool) -> io::Result<()> {
96 let setxattr_func = if deref { rfs::setxattr } else { rfs::lsetxattr };
97 setxattr_func(path, name, value, rfs::XattrFlags::empty())?;
98 Ok(())
99 }
100
101 pub fn remove_path(path: &Path, name: &OsStr, deref: bool) -> io::Result<()> {
102 if deref {
103 rfs::removexattr(path, name)
104 } else {
105 rfs::lremovexattr(path, name)
106 }?;
107 Ok(())
108 }
109
110 pub fn list_path(path: &Path, deref: bool) -> io::Result<XAttrs> {
111 let vec = if deref {
112 allocate_loop!(|buf| rfs::listxattr(path, buf))
113 } else {
114 allocate_loop!(|buf| rfs::llistxattr(path, buf))
115 }?;
116 Ok(XAttrs {
117 data: vec.into_boxed_slice(),
118 offset: 0,
119 })
120 }