Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use libc's definitions where possible #26

Merged
merged 1 commit into from Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -20,7 +20,7 @@ default = ["unsupported"]
unsupported = []

[dependencies]
libc = "0.2"
libc = "0.2.44"

[dev-dependencies]
tempfile = "3"
61 changes: 8 additions & 53 deletions src/sys/bsd.rs
Expand Up @@ -7,68 +7,21 @@ use std::os::unix::ffi::{OsStrExt, OsStringExt};
use std::os::unix::io::RawFd;
use std::path::Path;

use libc::{c_char, c_int, c_void, size_t, ssize_t, EPERM};
use libc::{c_int, c_void, size_t, EPERM};
use util::{allocate_loop, path_to_c};

use libc::{extattr_delete_fd, extattr_get_fd, extattr_list_fd, extattr_set_fd,
extattr_delete_link, extattr_get_link, extattr_list_link, extattr_set_link,
EXTATTR_NAMESPACE_USER, EXTATTR_NAMESPACE_SYSTEM
};

const EXTATTR_NAMESPACE_USER_STRING: &'static str = "user";
const EXTATTR_NAMESPACE_SYSTEM_STRING: &'static str = "system";
const EXTATTR_NAMESPACE_NAMES: [&'static str; 3] = [
"empty",
EXTATTR_NAMESPACE_USER_STRING,
EXTATTR_NAMESPACE_SYSTEM_STRING,
];
const EXTATTR_NAMESPACE_USER: c_int = 1;
const EXTATTR_NAMESPACE_SYSTEM: c_int = 2;

extern "C" {
pub fn extattr_list_fd(
fd: c_int,
attrnamespace: c_int,
data: *mut c_void,
nbytes: size_t,
) -> ssize_t;
pub fn extattr_get_fd(
fd: c_int,
attrnamespace: c_int,
attrname: *const c_char,
data: *mut c_void,
nbytes: size_t,
) -> ssize_t;
pub fn extattr_delete_fd(fd: c_int, attrname: c_int, attrname: *const c_char) -> c_int;
pub fn extattr_set_fd(
fd: c_int,
attrname: c_int,
attrname: *const c_char,
data: *const c_void,
nbytes: size_t,
) -> ssize_t;

pub fn extattr_list_link(
path: *const c_char,
attrnamespace: c_int,
data: *mut c_void,
nbytes: size_t,
) -> ssize_t;
pub fn extattr_get_link(
path: *const c_char,
attrnamespace: c_int,
attrname: *const c_char,
data: *mut c_void,
nbytes: size_t,
) -> ssize_t;
pub fn extattr_delete_link(
path: *const c_char,
attrname: c_int,
attrname: *const c_char,
) -> c_int;
pub fn extattr_set_link(
path: *const c_char,
attrname: c_int,
attrname: *const c_char,
data: *const c_void,
nbytes: size_t,
) -> ssize_t;
}

/// An iterator over a set of extended attributes names.
pub struct XAttrs {
Expand Down Expand Up @@ -141,6 +94,8 @@ impl Iterator for XAttrs {
}
}

// This could use libc::extattr_string_to_namespace, but it's awkward because
// that requires nul-terminated strings, which Rust's std is loathe to provide.
fn name_to_ns(name: &OsStr) -> io::Result<(c_int, CString)> {
let mut groups = name.as_bytes().splitn(2, |&b| b == b'.').take(2);
let nsname = match groups.next() {
Expand Down
43 changes: 7 additions & 36 deletions src/sys/linux_macos/linux.rs
@@ -1,35 +1,14 @@
use libc::{c_char, c_int, c_void, size_t, ssize_t};

extern "C" {
pub fn flistxattr(fd: c_int, buf: *mut c_char, size: size_t) -> ssize_t;
pub fn fgetxattr(fd: c_int, name: *const c_char, value: *mut c_void, size: size_t) -> ssize_t;
pub fn fremovexattr(fd: c_int, name: *const c_char) -> c_int;

pub fn llistxattr(path: *const c_char, buf: *mut c_char, size: size_t) -> ssize_t;
pub fn lgetxattr(
path: *const c_char,
name: *const c_char,
value: *mut c_void,
size: size_t,
) -> ssize_t;
pub fn lremovexattr(path: *const c_char, name: *const c_char) -> c_int;
}
use libc::{c_char, c_int, c_void, size_t};
pub use libc::{fgetxattr, flistxattr, fremovexattr, lgetxattr, llistxattr,
lremovexattr};

pub unsafe fn fsetxattr(
fd: c_int,
name: *const c_char,
value: *const c_void,
size: size_t,
) -> ssize_t {
extern "C" {
fn fsetxattr(
fd: c_int,
name: *const c_char,
value: *const c_void,
size: size_t,
flags: c_int,
) -> ssize_t;
}
) -> c_int {
use libc::fsetxattr;
fsetxattr(fd, name, value, size, 0)
}

Expand All @@ -38,15 +17,7 @@ pub unsafe fn lsetxattr(
name: *const c_char,
value: *const c_void,
size: size_t,
) -> ssize_t {
extern "C" {
fn lsetxattr(
path: *const c_char,
name: *const c_char,
value: *const c_void,
size: size_t,
flags: c_int,
) -> ssize_t;
}
) -> c_int {
use libc::lsetxattr;
lsetxattr(path, name, value, size, 0)
}
67 changes: 9 additions & 58 deletions src/sys/linux_macos/macos.rs
Expand Up @@ -4,9 +4,7 @@ const XATTR_NOFOLLOW: c_int = 0x0001;

#[inline(always)]
pub unsafe fn fremovexattr(fd: c_int, name: *const c_char) -> c_int {
extern "C" {
fn fremovexattr(fd: c_int, name: *const c_char, options: c_int) -> c_int;
}
use libc::fremovexattr;
fremovexattr(fd, name, 0)
}

Expand All @@ -16,17 +14,8 @@ pub unsafe fn fsetxattr(
name: *const c_char,
value: *const c_void,
size: size_t,
) -> ssize_t {
extern "C" {
fn fsetxattr(
fd: c_int,
name: *const c_char,
value: *const c_void,
size: size_t,
position: uint32_t,
options: c_int,
) -> ssize_t;
}
) -> c_int {
use libc::fsetxattr;
fsetxattr(fd, name, value, size, 0, 0)
}

Expand All @@ -37,32 +26,19 @@ pub unsafe fn fgetxattr(
value: *mut c_void,
size: size_t,
) -> ssize_t {
extern "C" {
fn fgetxattr(
fd: c_int,
name: *const c_char,
value: *mut c_void,
size: size_t,
position: uint32_t,
options: c_int,
) -> ssize_t;
}
use libc::fgetxattr;
fgetxattr(fd, name, value, size, 0, 0)
}

#[inline(always)]
pub unsafe fn flistxattr(fd: c_int, buf: *mut c_char, size: size_t) -> ssize_t {
extern "C" {
fn flistxattr(fd: c_int, buf: *mut c_char, size: size_t, options: c_int) -> ssize_t;
}
use libc::flistxattr;
flistxattr(fd, buf, size, 0)
}

#[inline(always)]
pub unsafe fn lremovexattr(path: *const c_char, name: *const c_char) -> c_int {
extern "C" {
fn removexattr(path: *const c_char, name: *const c_char, options: c_int) -> c_int;
}
use libc::removexattr;
removexattr(path, name, XATTR_NOFOLLOW)
}

Expand All @@ -73,16 +49,7 @@ pub unsafe fn lsetxattr(
value: *const c_void,
size: size_t,
) -> ssize_t {
extern "C" {
fn setxattr(
path: *const c_char,
name: *const c_char,
value: *const c_void,
size: size_t,
position: uint32_t,
options: c_int,
) -> ssize_t;
}
use libc::setxattr;
setxattr(path, name, value, size, 0, XATTR_NOFOLLOW)
}

Expand All @@ -93,28 +60,12 @@ pub unsafe fn lgetxattr(
value: *mut c_void,
size: size_t,
) -> ssize_t {
extern "C" {
fn getxattr(
path: *const c_char,
name: *const c_char,
value: *mut c_void,
size: size_t,
position: uint32_t,
options: c_int,
) -> ssize_t;
}
use libc::getxattr;
getxattr(path, name, value, size, 0, XATTR_NOFOLLOW)
}

#[inline(always)]
pub unsafe fn llistxattr(path: *const c_char, buf: *mut c_char, size: size_t) -> ssize_t {
extern "C" {
fn listxattr(
path: *const c_char,
buf: *mut c_char,
size: size_t,
options: c_int,
) -> ssize_t;
}
use libc::listxattr;
listxattr(path, buf, size, XATTR_NOFOLLOW)
}