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 } ;
@@ -356,7 +359,7 @@ pub struct DirEntry {
356
359
entry : dirent64 ,
357
360
}
358
361
359
- #[ derive( Clone , Debug ) ]
362
+ #[ derive( Clone ) ]
360
363
pub struct OpenOptions {
361
364
// generic
362
365
read : bool ,
@@ -370,7 +373,7 @@ pub struct OpenOptions {
370
373
mode : mode_t ,
371
374
}
372
375
373
- #[ derive( Clone , PartialEq , Eq , Debug ) ]
376
+ #[ derive( Clone , PartialEq , Eq ) ]
374
377
pub struct FilePermissions {
375
378
mode : mode_t ,
376
379
}
@@ -389,7 +392,7 @@ pub struct FileTimes {
389
392
created : Option < SystemTime > ,
390
393
}
391
394
392
- #[ derive( Copy , Clone , Eq , Debug ) ]
395
+ #[ derive( Copy , Clone , Eq ) ]
393
396
pub struct FileType {
394
397
mode : mode_t ,
395
398
}
@@ -406,11 +409,12 @@ impl core::hash::Hash for FileType {
406
409
}
407
410
}
408
411
409
- #[ derive( Debug ) ]
410
412
pub struct DirBuilder {
411
413
mode : mode_t ,
412
414
}
413
415
416
+ struct Mode ( mode_t ) ;
417
+
414
418
cfg_has_statx ! { {
415
419
impl FileAttr {
416
420
fn from_stat64( stat: stat64) -> Self {
@@ -689,12 +693,26 @@ impl FileType {
689
693
}
690
694
}
691
695
696
+ impl fmt:: Debug for FileType {
697
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
698
+ let FileType { mode } = self ;
699
+ f. debug_struct ( "FileType" ) . field ( "mode" , & Mode ( * mode) ) . finish ( )
700
+ }
701
+ }
702
+
692
703
impl FromInner < u32 > for FilePermissions {
693
704
fn from_inner ( mode : u32 ) -> FilePermissions {
694
705
FilePermissions { mode : mode as mode_t }
695
706
}
696
707
}
697
708
709
+ impl fmt:: Debug for FilePermissions {
710
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
711
+ let FilePermissions { mode } = self ;
712
+ f. debug_struct ( "FilePermissions" ) . field ( "mode" , & Mode ( * mode) ) . finish ( )
713
+ }
714
+ }
715
+
698
716
impl fmt:: Debug for ReadDir {
699
717
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
700
718
// This will only be called from std::fs::ReadDir, which will add a "ReadDir()" frame.
@@ -1135,6 +1153,23 @@ impl OpenOptions {
1135
1153
}
1136
1154
}
1137
1155
1156
+ impl fmt:: Debug for OpenOptions {
1157
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1158
+ let OpenOptions { read, write, append, truncate, create, create_new, custom_flags, mode } =
1159
+ self ;
1160
+ f. debug_struct ( "OpenOptions" )
1161
+ . field ( "read" , read)
1162
+ . field ( "write" , write)
1163
+ . field ( "append" , append)
1164
+ . field ( "truncate" , truncate)
1165
+ . field ( "create" , create)
1166
+ . field ( "create_new" , create_new)
1167
+ . field ( "custom_flags" , custom_flags)
1168
+ . field ( "mode" , & Mode ( * mode) )
1169
+ . finish ( )
1170
+ }
1171
+ }
1172
+
1138
1173
impl File {
1139
1174
pub fn open ( path : & Path , opts : & OpenOptions ) -> io:: Result < File > {
1140
1175
run_path_with_cstr ( path, & |path| File :: open_c ( path, opts) )
@@ -1425,6 +1460,13 @@ impl DirBuilder {
1425
1460
}
1426
1461
}
1427
1462
1463
+ impl fmt:: Debug for DirBuilder {
1464
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1465
+ let DirBuilder { mode } = self ;
1466
+ f. debug_struct ( "DirBuilder" ) . field ( "mode" , & Mode ( * mode) ) . finish ( )
1467
+ }
1468
+ }
1469
+
1428
1470
impl AsInner < FileDesc > for File {
1429
1471
#[ inline]
1430
1472
fn as_inner ( & self ) -> & FileDesc {
@@ -1597,6 +1639,67 @@ impl fmt::Debug for File {
1597
1639
}
1598
1640
}
1599
1641
1642
+ // Format in octal, followed by the mode format used in `ls -l`.
1643
+ //
1644
+ // References:
1645
+ // https://pubs.opengroup.org/onlinepubs/009696899/utilities/ls.html
1646
+ // https://www.gnu.org/software/libc/manual/html_node/Testing-File-Type.html
1647
+ // https://www.gnu.org/software/libc/manual/html_node/Permission-Bits.html
1648
+ //
1649
+ // Example:
1650
+ // 0o100664 (-rw-rw-r--)
1651
+ impl fmt:: Debug for Mode {
1652
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1653
+ let Self ( mode) = self ;
1654
+ write ! ( f, "0o{mode:06o}" ) ?;
1655
+
1656
+ let entry_type = match mode & libc:: S_IFMT {
1657
+ libc:: S_IFDIR => 'd' ,
1658
+ libc:: S_IFBLK => 'b' ,
1659
+ libc:: S_IFCHR => 'c' ,
1660
+ libc:: S_IFLNK => 'l' ,
1661
+ libc:: S_IFIFO => 'p' ,
1662
+ libc:: S_IFREG => '-' ,
1663
+ _ => return Ok ( ( ) ) ,
1664
+ } ;
1665
+
1666
+ f. write_str ( " (" ) ?;
1667
+ f. write_char ( entry_type) ?;
1668
+
1669
+ // Owner permissions
1670
+ f. write_char ( if mode & libc:: S_IRUSR != 0 { 'r' } else { '-' } ) ?;
1671
+ f. write_char ( if mode & libc:: S_IWUSR != 0 { 'w' } else { '-' } ) ?;
1672
+ f. write_char ( match ( mode & libc:: S_IXUSR != 0 , mode & libc:: S_ISUID != 0 ) {
1673
+ ( true , true ) => 's' , // executable and setuid
1674
+ ( false , true ) => 'S' , // setuid
1675
+ ( true , false ) => 'x' , // executable
1676
+ ( false , false ) => '-' ,
1677
+ } ) ?;
1678
+
1679
+ // Group permissions
1680
+ f. write_char ( if mode & libc:: S_IRGRP != 0 { 'r' } else { '-' } ) ?;
1681
+ f. write_char ( if mode & libc:: S_IWGRP != 0 { 'w' } else { '-' } ) ?;
1682
+ f. write_char ( match ( mode & libc:: S_IXGRP != 0 , mode & libc:: S_ISGID != 0 ) {
1683
+ ( true , true ) => 's' , // executable and setgid
1684
+ ( false , true ) => 'S' , // setgid
1685
+ ( true , false ) => 'x' , // executable
1686
+ ( false , false ) => '-' ,
1687
+ } ) ?;
1688
+
1689
+ // Other permissions
1690
+ f. write_char ( if mode & libc:: S_IROTH != 0 { 'r' } else { '-' } ) ?;
1691
+ f. write_char ( if mode & libc:: S_IWOTH != 0 { 'w' } else { '-' } ) ?;
1692
+ f. write_char ( match ( entry_type, mode & libc:: S_IXOTH != 0 , mode & libc:: S_ISVTX != 0 ) {
1693
+ ( 'd' , true , true ) => 't' , // searchable and restricted deletion
1694
+ ( 'd' , false , true ) => 'T' , // restricted deletion
1695
+ ( _, true , _) => 'x' , // executable
1696
+ ( _, false , _) => '-' ,
1697
+ } ) ?;
1698
+
1699
+ f. write_char ( ')' )
1700
+ }
1701
+ }
1702
+
1600
1703
pub fn readdir ( path : & Path ) -> io:: Result < ReadDir > {
1601
1704
let ptr = run_path_with_cstr ( path, & |p| unsafe { Ok ( libc:: opendir ( p. as_ptr ( ) ) ) } ) ?;
1602
1705
if ptr. is_null ( ) {
0 commit comments