Skip to content

Commit 9bde289

Browse files
committed
changes from review
1 parent 306bb59 commit 9bde289

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/fcntl.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ use std::{
1515
os::unix::io::{AsFd, AsRawFd},
1616
ptr,
1717
};
18+
#[cfg(any(
19+
target_os = "netbsd",
20+
target_os = "macos",
21+
target_os = "ios",
22+
target_os = "dragonfly",
23+
))]
24+
use std::path::PathBuf;
1825

1926
#[cfg(feature = "fs")]
2027
use crate::{sys::stat::Mode, NixPath, Result};
@@ -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,16 @@ 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+
}
558573
},
559574
}
560575
};

test/test_fcntl.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -563,19 +563,20 @@ mod test_posix_fallocate {
563563
}
564564

565565
#[test]
566-
#[cfg(any(target_os = "netbsd", target_os = "macos", target_os = "ios"))]
566+
#[cfg(any(target_os = "dragonfly", target_os = "netbsd", target_os = "macos", target_os = "ios"))]
567567
mod test_apple_netbsd {
568568
use nix::fcntl::*;
569569
use tempfile::NamedTempFile;
570+
use std::path::Path;
570571

571572
#[test]
572573
fn test_path() {
573574
let mut tmp = NamedTempFile::new().unwrap();
574575
let fd = tmp.as_raw_fd();
575-
let mut path: Vec<u8> = Vec::new();
576+
let mut path: PathBuf::new();
576577
let res = fcntl(fd, FcntlArg::F_GETPATH(path)).expect("get path failed");
577-
assert_eq!(
578-
path.len() > 0
578+
assert_ne!(
579+
path.as_path(), Path::new("")
579580
);
580581
}
581582
}

0 commit comments

Comments
 (0)