Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 64c122a

Browse files
committedMar 21, 2024
Show mode_t as octal in std::fs Debug impls
1 parent 6a6cd65 commit 64c122a

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed
 

‎library/std/src/sys/pal/unix/fs.rs

+52-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// miri has some special hacks here that make things unused.
22
#![cfg_attr(miri, allow(unused))]
33

4+
#[cfg(test)]
5+
mod tests;
6+
47
use crate::os::unix::prelude::*;
58

69
use crate::ffi::{CStr, OsStr, OsString};
@@ -354,7 +357,7 @@ pub struct DirEntry {
354357
entry: dirent64,
355358
}
356359

357-
#[derive(Clone, Debug)]
360+
#[derive(Clone)]
358361
pub struct OpenOptions {
359362
// generic
360363
read: bool,
@@ -368,7 +371,7 @@ pub struct OpenOptions {
368371
mode: mode_t,
369372
}
370373

371-
#[derive(Clone, PartialEq, Eq, Debug)]
374+
#[derive(Clone, PartialEq, Eq)]
372375
pub struct FilePermissions {
373376
mode: mode_t,
374377
}
@@ -381,7 +384,7 @@ pub struct FileTimes {
381384
created: Option<SystemTime>,
382385
}
383386

384-
#[derive(Copy, Clone, Eq, Debug)]
387+
#[derive(Copy, Clone, Eq)]
385388
pub struct FileType {
386389
mode: mode_t,
387390
}
@@ -398,11 +401,12 @@ impl core::hash::Hash for FileType {
398401
}
399402
}
400403

401-
#[derive(Debug)]
402404
pub struct DirBuilder {
403405
mode: mode_t,
404406
}
405407

408+
struct Mode(mode_t);
409+
406410
cfg_has_statx! {{
407411
impl FileAttr {
408412
fn from_stat64(stat: stat64) -> Self {
@@ -673,12 +677,26 @@ impl FileType {
673677
}
674678
}
675679

680+
impl fmt::Debug for FileType {
681+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
682+
let FileType { mode } = self;
683+
f.debug_struct("FileType").field("mode", &Mode(*mode)).finish()
684+
}
685+
}
686+
676687
impl FromInner<u32> for FilePermissions {
677688
fn from_inner(mode: u32) -> FilePermissions {
678689
FilePermissions { mode: mode as mode_t }
679690
}
680691
}
681692

693+
impl fmt::Debug for FilePermissions {
694+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
695+
let FilePermissions { mode } = self;
696+
f.debug_struct("FilePermissions").field("mode", &Mode(*mode)).finish()
697+
}
698+
}
699+
682700
impl fmt::Debug for ReadDir {
683701
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
684702
// This will only be called from std::fs::ReadDir, which will add a "ReadDir()" frame.
@@ -1116,6 +1134,23 @@ impl OpenOptions {
11161134
}
11171135
}
11181136

1137+
impl fmt::Debug for OpenOptions {
1138+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1139+
let OpenOptions { read, write, append, truncate, create, create_new, custom_flags, mode } =
1140+
self;
1141+
f.debug_struct("OpenOptions")
1142+
.field("read", read)
1143+
.field("write", write)
1144+
.field("append", append)
1145+
.field("truncate", truncate)
1146+
.field("create", create)
1147+
.field("create_new", create_new)
1148+
.field("custom_flags", custom_flags)
1149+
.field("mode", &Mode(*mode))
1150+
.finish()
1151+
}
1152+
}
1153+
11191154
impl File {
11201155
pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
11211156
run_path_with_cstr(path, &|path| File::open_c(path, opts))
@@ -1402,6 +1437,13 @@ impl DirBuilder {
14021437
}
14031438
}
14041439

1440+
impl fmt::Debug for DirBuilder {
1441+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1442+
let DirBuilder { mode } = self;
1443+
f.debug_struct("DirBuilder").field("mode", &Mode(*mode)).finish()
1444+
}
1445+
}
1446+
14051447
impl AsInner<FileDesc> for File {
14061448
#[inline]
14071449
fn as_inner(&self) -> &FileDesc {
@@ -1574,6 +1616,12 @@ impl fmt::Debug for File {
15741616
}
15751617
}
15761618

1619+
impl fmt::Debug for Mode {
1620+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1621+
write!(f, "0o{:03o}", self.0)
1622+
}
1623+
}
1624+
15771625
pub fn readdir(path: &Path) -> io::Result<ReadDir> {
15781626
let ptr = run_path_with_cstr(path, &|p| unsafe { Ok(libc::opendir(p.as_ptr())) })?;
15791627
if ptr.is_null() {
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use crate::sys::pal::unix::fs::FilePermissions;
2+
use crate::sys_common::FromInner;
3+
4+
#[test]
5+
fn test_debug_permissions() {
6+
for (mode, expected) in [
7+
(0o664, "FilePermissions { mode: 0o664 }"),
8+
(1, "FilePermissions { mode: 0o001 }"),
9+
(0o100777, "FilePermissions { mode: 0o100777 }"),
10+
] {
11+
assert_eq!(format!("{:?}", FilePermissions::from_inner(mode)), expected);
12+
}
13+
}

0 commit comments

Comments
 (0)
Failed to load comments.