1
1
// miri has some special hacks here that make things unused.
2
2
#![ cfg_attr( miri, allow( unused) ) ]
3
3
4
+ #[ cfg( test) ]
5
+ mod tests;
6
+
4
7
use crate :: os:: unix:: prelude:: * ;
5
8
6
9
use crate :: ffi:: { CStr , OsStr , OsString } ;
7
- use crate :: fmt;
10
+ use crate :: fmt:: { self , Write as _ } ;
8
11
use crate :: io:: { self , BorrowedCursor , Error , IoSlice , IoSliceMut , SeekFrom } ;
9
12
use crate :: mem;
10
13
use crate :: os:: unix:: io:: { AsFd , AsRawFd , BorrowedFd , FromRawFd , IntoRawFd } ;
@@ -354,7 +357,7 @@ pub struct DirEntry {
354
357
entry : dirent64 ,
355
358
}
356
359
357
- #[ derive( Clone , Debug ) ]
360
+ #[ derive( Clone ) ]
358
361
pub struct OpenOptions {
359
362
// generic
360
363
read : bool ,
@@ -368,7 +371,7 @@ pub struct OpenOptions {
368
371
mode : mode_t ,
369
372
}
370
373
371
- #[ derive( Clone , PartialEq , Eq , Debug ) ]
374
+ #[ derive( Clone , PartialEq , Eq ) ]
372
375
pub struct FilePermissions {
373
376
mode : mode_t ,
374
377
}
@@ -381,7 +384,7 @@ pub struct FileTimes {
381
384
created : Option < SystemTime > ,
382
385
}
383
386
384
- #[ derive( Copy , Clone , Eq , Debug ) ]
387
+ #[ derive( Copy , Clone , Eq ) ]
385
388
pub struct FileType {
386
389
mode : mode_t ,
387
390
}
@@ -398,11 +401,12 @@ impl core::hash::Hash for FileType {
398
401
}
399
402
}
400
403
401
- #[ derive( Debug ) ]
402
404
pub struct DirBuilder {
403
405
mode : mode_t ,
404
406
}
405
407
408
+ struct Mode ( mode_t ) ;
409
+
406
410
cfg_has_statx ! { {
407
411
impl FileAttr {
408
412
fn from_stat64( stat: stat64) -> Self {
@@ -673,12 +677,26 @@ impl FileType {
673
677
}
674
678
}
675
679
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
+
676
687
impl FromInner < u32 > for FilePermissions {
677
688
fn from_inner ( mode : u32 ) -> FilePermissions {
678
689
FilePermissions { mode : mode as mode_t }
679
690
}
680
691
}
681
692
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
+
682
700
impl fmt:: Debug for ReadDir {
683
701
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
684
702
// This will only be called from std::fs::ReadDir, which will add a "ReadDir()" frame.
@@ -1116,6 +1134,23 @@ impl OpenOptions {
1116
1134
}
1117
1135
}
1118
1136
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
+
1119
1154
impl File {
1120
1155
pub fn open ( path : & Path , opts : & OpenOptions ) -> io:: Result < File > {
1121
1156
run_path_with_cstr ( path, & |path| File :: open_c ( path, opts) )
@@ -1402,6 +1437,13 @@ impl DirBuilder {
1402
1437
}
1403
1438
}
1404
1439
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
+
1405
1447
impl AsInner < FileDesc > for File {
1406
1448
#[ inline]
1407
1449
fn as_inner ( & self ) -> & FileDesc {
@@ -1574,6 +1616,67 @@ impl fmt::Debug for File {
1574
1616
}
1575
1617
}
1576
1618
1619
+ // Format in octal, followed by the mode format used in `ls -l`.
1620
+ //
1621
+ // References:
1622
+ // https://pubs.opengroup.org/onlinepubs/009696899/utilities/ls.html
1623
+ // https://www.gnu.org/software/libc/manual/html_node/Testing-File-Type.html
1624
+ // https://www.gnu.org/software/libc/manual/html_node/Permission-Bits.html
1625
+ //
1626
+ // Example:
1627
+ // 0o100664 (-rw-rw-r--)
1628
+ impl fmt:: Debug for Mode {
1629
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1630
+ let Self ( mode) = self ;
1631
+ write ! ( f, "0o{mode:06o}" ) ?;
1632
+
1633
+ let entry_type = match mode & libc:: S_IFMT {
1634
+ libc:: S_IFDIR => 'd' ,
1635
+ libc:: S_IFBLK => 'b' ,
1636
+ libc:: S_IFCHR => 'c' ,
1637
+ libc:: S_IFLNK => 'l' ,
1638
+ libc:: S_IFIFO => 'p' ,
1639
+ libc:: S_IFREG => '-' ,
1640
+ _ => return Ok ( ( ) ) ,
1641
+ } ;
1642
+
1643
+ f. write_str ( " (" ) ?;
1644
+ f. write_char ( entry_type) ?;
1645
+
1646
+ // Owner permissions
1647
+ f. write_char ( if mode & libc:: S_IRUSR != 0 { 'r' } else { '-' } ) ?;
1648
+ f. write_char ( if mode & libc:: S_IWUSR != 0 { 'w' } else { '-' } ) ?;
1649
+ f. write_char ( match ( mode & libc:: S_IXUSR != 0 , mode & libc:: S_ISUID != 0 ) {
1650
+ ( true , true ) => 's' , // executable and setuid
1651
+ ( false , true ) => 'S' , // setuid
1652
+ ( true , false ) => 'x' , // executable
1653
+ ( false , false ) => '-' ,
1654
+ } ) ?;
1655
+
1656
+ // Group permissions
1657
+ f. write_char ( if mode & libc:: S_IRGRP != 0 { 'r' } else { '-' } ) ?;
1658
+ f. write_char ( if mode & libc:: S_IWGRP != 0 { 'w' } else { '-' } ) ?;
1659
+ f. write_char ( match ( mode & libc:: S_IXGRP != 0 , mode & libc:: S_ISGID != 0 ) {
1660
+ ( true , true ) => 's' , // executable and setgid
1661
+ ( false , true ) => 'S' , // setgid
1662
+ ( true , false ) => 'x' , // executable
1663
+ ( false , false ) => '-' ,
1664
+ } ) ?;
1665
+
1666
+ // Other permissions
1667
+ f. write_char ( if mode & libc:: S_IROTH != 0 { 'r' } else { '-' } ) ?;
1668
+ f. write_char ( if mode & libc:: S_IWOTH != 0 { 'w' } else { '-' } ) ?;
1669
+ f. write_char ( match ( entry_type, mode & libc:: S_IXOTH != 0 , mode & libc:: S_ISVTX != 0 ) {
1670
+ ( 'd' , true , true ) => 't' , // searchable and restricted deletion
1671
+ ( 'd' , false , true ) => 'T' , // restricted deletion
1672
+ ( _, true , _) => 'x' , // executable
1673
+ ( _, false , _) => '-' ,
1674
+ } ) ?;
1675
+
1676
+ f. write_char ( ')' )
1677
+ }
1678
+ }
1679
+
1577
1680
pub fn readdir ( path : & Path ) -> io:: Result < ReadDir > {
1578
1681
let ptr = run_path_with_cstr ( path, & |p| unsafe { Ok ( libc:: opendir ( p. as_ptr ( ) ) ) } ) ?;
1579
1682
if ptr. is_null ( ) {
0 commit comments