@@ -926,7 +926,7 @@ impl DirEntry {
926
926
miri
927
927
) ) ]
928
928
pub fn metadata ( & self ) -> io:: Result < FileAttr > {
929
- lstat ( & self . path ( ) )
929
+ run_path_with_cstr ( & self . path ( ) , & lstat )
930
930
}
931
931
932
932
#[ cfg( any(
@@ -1653,7 +1653,7 @@ impl fmt::Debug for File {
1653
1653
fn get_path ( fd : c_int ) -> Option < PathBuf > {
1654
1654
let mut p = PathBuf :: from ( "/proc/self/fd" ) ;
1655
1655
p. push ( & fd. to_string ( ) ) ;
1656
- readlink ( & p) . ok ( )
1656
+ run_path_with_cstr ( & p, & readlink ) . ok ( )
1657
1657
}
1658
1658
1659
1659
#[ cfg( any( target_vendor = "apple" , target_os = "netbsd" ) ) ]
@@ -1826,127 +1826,106 @@ pub fn readdir(path: &Path) -> io::Result<ReadDir> {
1826
1826
}
1827
1827
}
1828
1828
1829
- pub fn unlink ( p : & Path ) -> io:: Result < ( ) > {
1830
- run_path_with_cstr ( p , & |p| cvt ( unsafe { libc:: unlink ( p. as_ptr ( ) ) } ) . map ( |_| ( ) ) )
1829
+ pub fn unlink ( p : & CStr ) -> io:: Result < ( ) > {
1830
+ cvt ( unsafe { libc:: unlink ( p. as_ptr ( ) ) } ) . map ( |_| ( ) )
1831
1831
}
1832
1832
1833
- pub fn rename ( old : & Path , new : & Path ) -> io:: Result < ( ) > {
1834
- run_path_with_cstr ( old, & |old| {
1835
- run_path_with_cstr ( new, & |new| {
1836
- cvt ( unsafe { libc:: rename ( old. as_ptr ( ) , new. as_ptr ( ) ) } ) . map ( |_| ( ) )
1837
- } )
1838
- } )
1833
+ pub fn rename ( old : & CStr , new : & CStr ) -> io:: Result < ( ) > {
1834
+ cvt ( unsafe { libc:: rename ( old. as_ptr ( ) , new. as_ptr ( ) ) } ) . map ( |_| ( ) )
1839
1835
}
1840
1836
1841
- pub fn set_perm ( p : & Path , perm : FilePermissions ) -> io:: Result < ( ) > {
1842
- run_path_with_cstr ( p , & |p| cvt_r ( || unsafe { libc:: chmod ( p. as_ptr ( ) , perm. mode ) } ) . map ( |_| ( ) ) )
1837
+ pub fn set_perm ( p : & CStr , perm : FilePermissions ) -> io:: Result < ( ) > {
1838
+ cvt_r ( || unsafe { libc:: chmod ( p. as_ptr ( ) , perm. mode ) } ) . map ( |_| ( ) )
1843
1839
}
1844
1840
1845
- pub fn rmdir ( p : & Path ) -> io:: Result < ( ) > {
1846
- run_path_with_cstr ( p , & |p| cvt ( unsafe { libc:: rmdir ( p. as_ptr ( ) ) } ) . map ( |_| ( ) ) )
1841
+ pub fn rmdir ( p : & CStr ) -> io:: Result < ( ) > {
1842
+ cvt ( unsafe { libc:: rmdir ( p. as_ptr ( ) ) } ) . map ( |_| ( ) )
1847
1843
}
1848
1844
1849
- pub fn readlink ( p : & Path ) -> io:: Result < PathBuf > {
1850
- run_path_with_cstr ( p, & |c_path| {
1851
- let p = c_path. as_ptr ( ) ;
1852
-
1853
- let mut buf = Vec :: with_capacity ( 256 ) ;
1845
+ pub fn readlink ( c_path : & CStr ) -> io:: Result < PathBuf > {
1846
+ let p = c_path. as_ptr ( ) ;
1854
1847
1855
- loop {
1856
- let buf_read =
1857
- cvt ( unsafe { libc:: readlink ( p, buf. as_mut_ptr ( ) as * mut _ , buf. capacity ( ) ) } ) ?
1858
- as usize ;
1848
+ let mut buf = Vec :: with_capacity ( 256 ) ;
1859
1849
1860
- unsafe {
1861
- buf . set_len ( buf_read) ;
1862
- }
1850
+ loop {
1851
+ let buf_read =
1852
+ cvt ( unsafe { libc :: readlink ( p , buf . as_mut_ptr ( ) as * mut _ , buf . capacity ( ) ) } ) ? as usize ;
1863
1853
1864
- if buf_read != buf. capacity ( ) {
1865
- buf. shrink_to_fit ( ) ;
1854
+ unsafe {
1855
+ buf. set_len ( buf_read) ;
1856
+ }
1866
1857
1867
- return Ok ( PathBuf :: from ( OsString :: from_vec ( buf) ) ) ;
1868
- }
1858
+ if buf_read != buf. capacity ( ) {
1859
+ buf . shrink_to_fit ( ) ;
1869
1860
1870
- // Trigger the internal buffer resizing logic of `Vec` by requiring
1871
- // more space than the current capacity. The length is guaranteed to be
1872
- // the same as the capacity due to the if statement above.
1873
- buf. reserve ( 1 ) ;
1861
+ return Ok ( PathBuf :: from ( OsString :: from_vec ( buf) ) ) ;
1874
1862
}
1875
- } )
1863
+
1864
+ // Trigger the internal buffer resizing logic of `Vec` by requiring
1865
+ // more space than the current capacity. The length is guaranteed to be
1866
+ // the same as the capacity due to the if statement above.
1867
+ buf. reserve ( 1 ) ;
1868
+ }
1876
1869
}
1877
1870
1878
- pub fn symlink ( original : & Path , link : & Path ) -> io:: Result < ( ) > {
1879
- run_path_with_cstr ( original, & |original| {
1880
- run_path_with_cstr ( link, & |link| {
1881
- cvt ( unsafe { libc:: symlink ( original. as_ptr ( ) , link. as_ptr ( ) ) } ) . map ( |_| ( ) )
1882
- } )
1883
- } )
1871
+ pub fn symlink ( original : & CStr , link : & CStr ) -> io:: Result < ( ) > {
1872
+ cvt ( unsafe { libc:: symlink ( original. as_ptr ( ) , link. as_ptr ( ) ) } ) . map ( |_| ( ) )
1884
1873
}
1885
1874
1886
- pub fn link ( original : & Path , link : & Path ) -> io:: Result < ( ) > {
1887
- run_path_with_cstr ( original, & |original| {
1888
- run_path_with_cstr ( link, & |link| {
1889
- cfg_if:: cfg_if! {
1890
- if #[ cfg( any( target_os = "vxworks" , target_os = "redox" , target_os = "android" , target_os = "espidf" , target_os = "horizon" , target_os = "vita" , target_env = "nto70" ) ) ] {
1891
- // VxWorks, Redox and ESP-IDF lack `linkat`, so use `link` instead. POSIX leaves
1892
- // it implementation-defined whether `link` follows symlinks, so rely on the
1893
- // `symlink_hard_link` test in library/std/src/fs/tests.rs to check the behavior.
1894
- // Android has `linkat` on newer versions, but we happen to know `link`
1895
- // always has the correct behavior, so it's here as well.
1896
- cvt( unsafe { libc:: link( original. as_ptr( ) , link. as_ptr( ) ) } ) ?;
1897
- } else {
1898
- // Where we can, use `linkat` instead of `link`; see the comment above
1899
- // this one for details on why.
1900
- cvt( unsafe { libc:: linkat( libc:: AT_FDCWD , original. as_ptr( ) , libc:: AT_FDCWD , link. as_ptr( ) , 0 ) } ) ?;
1901
- }
1902
- }
1903
- Ok ( ( ) )
1904
- } )
1905
- } )
1875
+ pub fn link ( original : & CStr , link : & CStr ) -> io:: Result < ( ) > {
1876
+ cfg_if:: cfg_if! {
1877
+ if #[ cfg( any( target_os = "vxworks" , target_os = "redox" , target_os = "android" , target_os = "espidf" , target_os = "horizon" , target_os = "vita" , target_env = "nto70" ) ) ] {
1878
+ // VxWorks, Redox and ESP-IDF lack `linkat`, so use `link` instead. POSIX leaves
1879
+ // it implementation-defined whether `link` follows symlinks, so rely on the
1880
+ // `symlink_hard_link` test in library/std/src/fs/tests.rs to check the behavior.
1881
+ // Android has `linkat` on newer versions, but we happen to know `link`
1882
+ // always has the correct behavior, so it's here as well.
1883
+ cvt( unsafe { libc:: link( original. as_ptr( ) , link. as_ptr( ) ) } ) ?;
1884
+ } else {
1885
+ // Where we can, use `linkat` instead of `link`; see the comment above
1886
+ // this one for details on why.
1887
+ cvt( unsafe { libc:: linkat( libc:: AT_FDCWD , original. as_ptr( ) , libc:: AT_FDCWD , link. as_ptr( ) , 0 ) } ) ?;
1888
+ }
1889
+ }
1890
+ Ok ( ( ) )
1906
1891
}
1907
1892
1908
- pub fn stat ( p : & Path ) -> io:: Result < FileAttr > {
1909
- run_path_with_cstr ( p, & |p| {
1910
- cfg_has_statx ! {
1911
- if let Some ( ret) = unsafe { try_statx(
1912
- libc:: AT_FDCWD ,
1913
- p. as_ptr( ) ,
1914
- libc:: AT_STATX_SYNC_AS_STAT ,
1915
- libc:: STATX_BASIC_STATS | libc:: STATX_BTIME ,
1916
- ) } {
1917
- return ret;
1918
- }
1893
+ pub fn stat ( p : & CStr ) -> io:: Result < FileAttr > {
1894
+ cfg_has_statx ! {
1895
+ if let Some ( ret) = unsafe { try_statx(
1896
+ libc:: AT_FDCWD ,
1897
+ p. as_ptr( ) ,
1898
+ libc:: AT_STATX_SYNC_AS_STAT ,
1899
+ libc:: STATX_BASIC_STATS | libc:: STATX_BTIME ,
1900
+ ) } {
1901
+ return ret;
1919
1902
}
1903
+ }
1920
1904
1921
- let mut stat: stat64 = unsafe { mem:: zeroed ( ) } ;
1922
- cvt ( unsafe { stat64 ( p. as_ptr ( ) , & mut stat) } ) ?;
1923
- Ok ( FileAttr :: from_stat64 ( stat) )
1924
- } )
1905
+ let mut stat: stat64 = unsafe { mem:: zeroed ( ) } ;
1906
+ cvt ( unsafe { stat64 ( p. as_ptr ( ) , & mut stat) } ) ?;
1907
+ Ok ( FileAttr :: from_stat64 ( stat) )
1925
1908
}
1926
1909
1927
- pub fn lstat ( p : & Path ) -> io:: Result < FileAttr > {
1928
- run_path_with_cstr ( p, & |p| {
1929
- cfg_has_statx ! {
1930
- if let Some ( ret) = unsafe { try_statx(
1931
- libc:: AT_FDCWD ,
1932
- p. as_ptr( ) ,
1933
- libc:: AT_SYMLINK_NOFOLLOW | libc:: AT_STATX_SYNC_AS_STAT ,
1934
- libc:: STATX_BASIC_STATS | libc:: STATX_BTIME ,
1935
- ) } {
1936
- return ret;
1937
- }
1910
+ pub fn lstat ( p : & CStr ) -> io:: Result < FileAttr > {
1911
+ cfg_has_statx ! {
1912
+ if let Some ( ret) = unsafe { try_statx(
1913
+ libc:: AT_FDCWD ,
1914
+ p. as_ptr( ) ,
1915
+ libc:: AT_SYMLINK_NOFOLLOW | libc:: AT_STATX_SYNC_AS_STAT ,
1916
+ libc:: STATX_BASIC_STATS | libc:: STATX_BTIME ,
1917
+ ) } {
1918
+ return ret;
1938
1919
}
1920
+ }
1939
1921
1940
- let mut stat: stat64 = unsafe { mem:: zeroed ( ) } ;
1941
- cvt ( unsafe { lstat64 ( p. as_ptr ( ) , & mut stat) } ) ?;
1942
- Ok ( FileAttr :: from_stat64 ( stat) )
1943
- } )
1922
+ let mut stat: stat64 = unsafe { mem:: zeroed ( ) } ;
1923
+ cvt ( unsafe { lstat64 ( p. as_ptr ( ) , & mut stat) } ) ?;
1924
+ Ok ( FileAttr :: from_stat64 ( stat) )
1944
1925
}
1945
1926
1946
- pub fn canonicalize ( p : & Path ) -> io:: Result < PathBuf > {
1947
- let r = run_path_with_cstr ( p, & |path| unsafe {
1948
- Ok ( libc:: realpath ( path. as_ptr ( ) , ptr:: null_mut ( ) ) )
1949
- } ) ?;
1927
+ pub fn canonicalize ( path : & CStr ) -> io:: Result < PathBuf > {
1928
+ let r = unsafe { libc:: realpath ( path. as_ptr ( ) , ptr:: null_mut ( ) ) } ;
1950
1929
if r. is_null ( ) {
1951
1930
return Err ( io:: Error :: last_os_error ( ) ) ;
1952
1931
}
@@ -2202,8 +2181,7 @@ mod remove_dir_impl {
2202
2181
use crate :: io;
2203
2182
use crate :: os:: unix:: io:: { AsRawFd , FromRawFd , IntoRawFd } ;
2204
2183
use crate :: os:: unix:: prelude:: { OwnedFd , RawFd } ;
2205
- use crate :: path:: { Path , PathBuf } ;
2206
- use crate :: sys:: common:: small_c_string:: run_path_with_cstr;
2184
+ use crate :: path:: PathBuf ;
2207
2185
use crate :: sys:: { cvt, cvt_r} ;
2208
2186
use crate :: sys_common:: ignore_notfound;
2209
2187
@@ -2324,19 +2302,19 @@ mod remove_dir_impl {
2324
2302
Ok ( ( ) )
2325
2303
}
2326
2304
2327
- fn remove_dir_all_modern ( p : & Path ) -> io:: Result < ( ) > {
2305
+ fn remove_dir_all_modern ( p : & CStr ) -> io:: Result < ( ) > {
2328
2306
// We cannot just call remove_dir_all_recursive() here because that would not delete a passed
2329
2307
// symlink. No need to worry about races, because remove_dir_all_recursive() does not recurse
2330
2308
// into symlinks.
2331
2309
let attr = lstat ( p) ?;
2332
2310
if attr. file_type ( ) . is_symlink ( ) {
2333
- crate :: fs :: remove_file ( p)
2311
+ super :: unlink ( p)
2334
2312
} else {
2335
- run_path_with_cstr ( p , & |p| remove_dir_all_recursive ( None , & p) )
2313
+ remove_dir_all_recursive ( None , & p)
2336
2314
}
2337
2315
}
2338
2316
2339
- pub fn remove_dir_all ( p : & Path ) -> io:: Result < ( ) > {
2317
+ pub fn remove_dir_all ( p : & CStr ) -> io:: Result < ( ) > {
2340
2318
remove_dir_all_modern ( p)
2341
2319
}
2342
2320
}
0 commit comments