Skip to content

Commit 4c9219e

Browse files
committed
changes from review
1 parent 306bb59 commit 4c9219e

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
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 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 r;
573+
}
558574
},
559575
}
560576
};

test/test_fcntl.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -563,19 +563,24 @@ mod test_posix_fallocate {
563563
}
564564

565565
#[test]
566-
#[cfg(any(target_os = "netbsd", target_os = "macos", target_os = "ios"))]
566+
#[cfg(any(
567+
target_os = "dragonfly",
568+
target_os = "netbsd",
569+
target_os = "macos",
570+
target_os = "ios"
571+
))]
567572
mod test_apple_netbsd {
568573
use nix::fcntl::*;
574+
use std::path::Path;
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(path)).expect("get path failed");
584+
assert_ne!(path.as_path(), Path::new(""));
580585
}
581586
}

0 commit comments

Comments
 (0)