Skip to content

Commit 3c24274

Browse files
committed
changes from review
1 parent 306bb59 commit 3c24274

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

src/fcntl.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ use std::os::raw;
66
use std::os::unix::ffi::OsStringExt;
77
use std::os::unix::io::RawFd;
88
// For splice and copy_file_range
9+
#[cfg(any(
10+
target_os = "netbsd",
11+
target_os = "macos",
12+
target_os = "ios",
13+
target_os = "dragonfly",
14+
))]
15+
use std::path::PathBuf;
916
#[cfg(any(
1017
target_os = "android",
1118
target_os = "freebsd",
@@ -489,8 +496,8 @@ pub enum FcntlArg<'a> {
489496
F_GETPIPE_SZ,
490497
#[cfg(any(target_os = "linux", target_os = "android"))]
491498
F_SETPIPE_SZ(c_int),
492-
#[cfg(any(target_os = "netbsd", target_os = "macos", target_os = "ios"))]
493-
F_GETPATH(Vec<u8>),
499+
#[cfg(any(target_os = "netbsd", target_os = "dragonfly", target_os = "macos", target_os = "ios"))]
500+
F_GETPATH(&'a mut PathBuf),
494501
// TODO: Rest of flags
495502
}
496503

@@ -553,8 +560,17 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
553560
F_SETPIPE_SZ(size) => libc::fcntl(fd, libc::F_SETPIPE_SZ, size),
554561
#[cfg(any(target_os = "netbsd", target_os = "macos", target_os = "ios"))]
555562
F_GETPATH(path) => {
556-
path.resize(libc::PATH_MAX);
557-
libc::fcntl(fd, libc::F_GETPATH, path.as_ptr())
563+
let mut buffer = vec![0; libc::PATH_MAX as usize];
564+
let r = libc::fcntl(fd, libc::F_GETPATH, buffer.as_ptr());
565+
if r < 0 {
566+
return Errno::result(r);
567+
} else {
568+
let len = buffer.iter().position(|b| *b == 0).unwrap();
569+
buffer.truncate(len as usize);
570+
buffer.shrink_to_fit();
571+
*path = PathBuf::from(OsString::from_vec(buffer));
572+
return Errno::result(r);
573+
}
558574
},
559575
}
560576
};

test/test_fcntl.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -562,20 +562,25 @@ mod test_posix_fallocate {
562562
}
563563
}
564564

565-
#[test]
566-
#[cfg(any(target_os = "netbsd", target_os = "macos", target_os = "ios"))]
565+
#[cfg(any(
566+
target_os = "dragonfly",
567+
target_os = "netbsd",
568+
target_os = "macos",
569+
target_os = "ios"
570+
))]
567571
mod test_apple_netbsd {
568572
use nix::fcntl::*;
573+
use std::os::unix::io::AsRawFd;
574+
use std::path::PathBuf;
569575
use tempfile::NamedTempFile;
570576

571577
#[test]
572578
fn test_path() {
573579
let mut tmp = NamedTempFile::new().unwrap();
574580
let fd = tmp.as_raw_fd();
575-
let mut path: Vec<u8> = Vec::new();
576-
let res = fcntl(fd, FcntlArg::F_GETPATH(path)).expect("get path failed");
577-
assert_eq!(
578-
path.len() > 0
579-
);
581+
let mut path = PathBuf::new();
582+
let res =
583+
fcntl(fd, FcntlArg::F_GETPATH(&mut path)).expect("get path failed");
584+
assert_eq!(path, tmp.path());
580585
}
581586
}

0 commit comments

Comments
 (0)