Skip to content

Commit

Permalink
fcntl adding few apple extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
devnexen committed Nov 6, 2023
1 parent ad60784 commit 4147ed9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelog/2155.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added `F_GETPATH_NOFIRMLINK` and `F_BARRIERFSYNC` FcntlFlags entry
on Apple for `::nix::fcntl`.
15 changes: 15 additions & 0 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ pub enum FcntlArg<'a> {
F_GET_SEALS,
#[cfg(any(target_os = "macos", target_os = "ios"))]
F_FULLFSYNC,
#[cfg(any(target_os = "macos", target_os = "ios"))]
F_BARRIERFSYNC,
#[cfg(any(target_os = "linux", target_os = "android"))]
F_GETPIPE_SZ,
#[cfg(any(target_os = "linux", target_os = "android"))]
Expand All @@ -512,6 +514,8 @@ pub enum FcntlArg<'a> {
F_GETPATH(&'a mut PathBuf),
#[cfg(all(target_os = "freebsd", target_arch = "x86_64"))]
F_KINFO(&'a mut PathBuf),
#[cfg(any(target_os = "macos", target_os = "ios"))]
F_GETPATH_NOFIRMLINK(&'a mut PathBuf),
// TODO: Rest of flags
}

Expand Down Expand Up @@ -568,6 +572,8 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
F_GET_SEALS => libc::fcntl(fd, libc::F_GET_SEALS),
#[cfg(any(target_os = "macos", target_os = "ios"))]
F_FULLFSYNC => libc::fcntl(fd, libc::F_FULLFSYNC),
#[cfg(any(target_os = "macos", target_os = "ios"))]
F_BARRIERFSYNC => libc::fcntl(fd, libc::F_BARRIERFSYNC),
#[cfg(any(target_os = "linux", target_os = "android"))]
F_GETPIPE_SZ => libc::fcntl(fd, libc::F_GETPIPE_SZ),
#[cfg(any(target_os = "linux", target_os = "android"))]
Expand All @@ -593,6 +599,15 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
*path = PathBuf::from(OsString::from(optr.to_str().unwrap()));
return Ok(ok_res)
},
#[cfg(any(target_os = "macos", target_os = "ios"))]
F_GETPATH_NOFIRMLINK(path) => {
let mut buffer = vec![0; libc::PATH_MAX as usize];
let res = libc::fcntl(fd, libc::F_GETPATH_NOFIRMLINK, buffer.as_mut_ptr());
let ok_res = Errno::result(res)?;
let optr = CStr::from_bytes_until_nul(&buffer).unwrap();
*path = PathBuf::from(OsString::from(optr.to_str().unwrap()));
return Ok(ok_res)
},
}
};

Expand Down
32 changes: 32 additions & 0 deletions test/test_fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,38 @@ fn test_f_get_path() {
);
}

#[cfg(any(target_os = "macos", target_os = "ios"))]
#[test]
fn test_f_get_path_nofirmlink() {
use nix::fcntl::*;
use std::{os::unix::io::AsRawFd, path::PathBuf};

let tmp = NamedTempFile::new().unwrap();
let fd = tmp.as_raw_fd();
let mut path = PathBuf::new();
let res = fcntl(fd, FcntlArg::F_GETPATH_NOFIRMLINK(&mut path))
.expect("get path failed");
let mut tmpstr = String::from("/System/Volumes/Data");
tmpstr.push_str(
&tmp.path()
.canonicalize()
.unwrap()
.into_os_string()
.into_string()
.unwrap(),
);
assert_ne!(res, -1);
assert_eq!(
path.as_path()
.canonicalize()
.unwrap()
.into_os_string()
.into_string()
.unwrap(),
tmpstr
);
}

#[cfg(all(target_os = "freebsd", target_arch = "x86_64"))]
#[test]
fn test_f_kinfo() {
Expand Down

0 comments on commit 4147ed9

Please sign in to comment.