Skip to content

Commit 98dcd5e

Browse files
committed
Auto merge of #26941 - fhartwig:osx-file-debug, r=alexcrichton
This makes `Debug` for `File` show the file path and access mode of the file on OS X, just like on Linux. I'd be happy about any feedback how to make this code better. In particular, I'm not sure how to handle the buffer passed to `fnctl`. This way works, but it feels a bit cumbersome. `fcntl` unfortunately doesn't return the length of the path.
2 parents 072d07c + f200ad8 commit 98dcd5e

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/liblibc/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -4978,6 +4978,8 @@ pub mod consts {
49784978
pub const F_GETFL : c_int = 3;
49794979
pub const F_SETFL : c_int = 4;
49804980

4981+
pub const O_ACCMODE : c_int = 3;
4982+
49814983
pub const SIGTRAP : c_int = 5;
49824984
pub const SIG_IGN: size_t = 1;
49834985

@@ -5130,6 +5132,7 @@ pub mod consts {
51305132
pub const O_DSYNC : c_int = 4194304;
51315133
pub const O_SYNC : c_int = 128;
51325134
pub const O_NONBLOCK : c_int = 4;
5135+
pub const F_GETPATH : c_int = 50;
51335136
pub const F_FULLFSYNC : c_int = 51;
51345137

51355138
pub const MAP_COPY : c_int = 0x0002;
@@ -5151,6 +5154,8 @@ pub mod consts {
51515154
pub const SO_DONTTRUNC: c_int = 0x2000;
51525155
pub const SO_WANTMORE: c_int = 0x4000;
51535156
pub const SO_WANTOOBFLAG: c_int = 0x8000;
5157+
5158+
pub const PATH_MAX: c_int = 1024;
51545159
}
51555160
pub mod sysconf {
51565161
use types::os::arch::c95::c_int;

src/libstd/sys/unix/fs.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -370,13 +370,25 @@ impl fmt::Debug for File {
370370
readlink(&p).ok()
371371
}
372372

373-
#[cfg(not(target_os = "linux"))]
373+
#[cfg(target_os = "macos")]
374+
fn get_path(fd: c_int) -> Option<PathBuf> {
375+
let mut buf = vec![0;libc::PATH_MAX as usize];
376+
let n = unsafe { libc::fcntl(fd, libc::F_GETPATH, buf.as_ptr()) };
377+
if n == -1 {
378+
return None;
379+
}
380+
let l = buf.iter().position(|&c| c == 0).unwrap();
381+
buf.truncate(l as usize);
382+
Some(PathBuf::from(OsString::from_vec(buf)))
383+
}
384+
385+
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
374386
fn get_path(_fd: c_int) -> Option<PathBuf> {
375387
// FIXME(#24570): implement this for other Unix platforms
376388
None
377389
}
378390

379-
#[cfg(target_os = "linux")]
391+
#[cfg(any(target_os = "linux", target_os = "macos"))]
380392
fn get_mode(fd: c_int) -> Option<(bool, bool)> {
381393
let mode = unsafe { libc::fcntl(fd, libc::F_GETFL) };
382394
if mode == -1 {
@@ -390,7 +402,7 @@ impl fmt::Debug for File {
390402
}
391403
}
392404

393-
#[cfg(not(target_os = "linux"))]
405+
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
394406
fn get_mode(_fd: c_int) -> Option<(bool, bool)> {
395407
// FIXME(#24570): implement this for other Unix platforms
396408
None

0 commit comments

Comments
 (0)