Skip to content

Commit b5e1a76

Browse files
authored
fcntl adding few apple extensions (#2155)
1 parent ad60784 commit b5e1a76

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

changelog/2155.added.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Added `F_GETPATH_NOFIRMLINK` and `F_BARRIERFSYNC` FcntlFlags entry
2+
on Apple for `::nix::fcntl`.

src/fcntl.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,8 @@ pub enum FcntlArg<'a> {
504504
F_GET_SEALS,
505505
#[cfg(any(target_os = "macos", target_os = "ios"))]
506506
F_FULLFSYNC,
507+
#[cfg(any(target_os = "macos", target_os = "ios"))]
508+
F_BARRIERFSYNC,
507509
#[cfg(any(target_os = "linux", target_os = "android"))]
508510
F_GETPIPE_SZ,
509511
#[cfg(any(target_os = "linux", target_os = "android"))]
@@ -512,6 +514,8 @@ pub enum FcntlArg<'a> {
512514
F_GETPATH(&'a mut PathBuf),
513515
#[cfg(all(target_os = "freebsd", target_arch = "x86_64"))]
514516
F_KINFO(&'a mut PathBuf),
517+
#[cfg(any(target_os = "macos", target_os = "ios"))]
518+
F_GETPATH_NOFIRMLINK(&'a mut PathBuf),
515519
// TODO: Rest of flags
516520
}
517521

@@ -568,6 +572,8 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
568572
F_GET_SEALS => libc::fcntl(fd, libc::F_GET_SEALS),
569573
#[cfg(any(target_os = "macos", target_os = "ios"))]
570574
F_FULLFSYNC => libc::fcntl(fd, libc::F_FULLFSYNC),
575+
#[cfg(any(target_os = "macos", target_os = "ios"))]
576+
F_BARRIERFSYNC => libc::fcntl(fd, libc::F_BARRIERFSYNC),
571577
#[cfg(any(target_os = "linux", target_os = "android"))]
572578
F_GETPIPE_SZ => libc::fcntl(fd, libc::F_GETPIPE_SZ),
573579
#[cfg(any(target_os = "linux", target_os = "android"))]
@@ -593,6 +599,15 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
593599
*path = PathBuf::from(OsString::from(optr.to_str().unwrap()));
594600
return Ok(ok_res)
595601
},
602+
#[cfg(any(target_os = "macos", target_os = "ios"))]
603+
F_GETPATH_NOFIRMLINK(path) => {
604+
let mut buffer = vec![0; libc::PATH_MAX as usize];
605+
let res = libc::fcntl(fd, libc::F_GETPATH_NOFIRMLINK, buffer.as_mut_ptr());
606+
let ok_res = Errno::result(res)?;
607+
let optr = CStr::from_bytes_until_nul(&buffer).unwrap();
608+
*path = PathBuf::from(OsString::from(optr.to_str().unwrap()));
609+
return Ok(ok_res)
610+
},
596611
}
597612
};
598613

test/test_fcntl.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,38 @@ fn test_f_get_path() {
585585
);
586586
}
587587

588+
#[cfg(any(target_os = "macos", target_os = "ios"))]
589+
#[test]
590+
fn test_f_get_path_nofirmlink() {
591+
use nix::fcntl::*;
592+
use std::{os::unix::io::AsRawFd, path::PathBuf};
593+
594+
let tmp = NamedTempFile::new().unwrap();
595+
let fd = tmp.as_raw_fd();
596+
let mut path = PathBuf::new();
597+
let res = fcntl(fd, FcntlArg::F_GETPATH_NOFIRMLINK(&mut path))
598+
.expect("get path failed");
599+
let mut tmpstr = String::from("/System/Volumes/Data");
600+
tmpstr.push_str(
601+
&tmp.path()
602+
.canonicalize()
603+
.unwrap()
604+
.into_os_string()
605+
.into_string()
606+
.unwrap(),
607+
);
608+
assert_ne!(res, -1);
609+
assert_eq!(
610+
path.as_path()
611+
.canonicalize()
612+
.unwrap()
613+
.into_os_string()
614+
.into_string()
615+
.unwrap(),
616+
tmpstr
617+
);
618+
}
619+
588620
#[cfg(all(target_os = "freebsd", target_arch = "x86_64"))]
589621
#[test]
590622
fn test_f_kinfo() {

0 commit comments

Comments
 (0)