@@ -217,13 +217,13 @@ pub enum ReadConsoleError {
217217 BufferTooSmall { available : usize , required : usize } ,
218218}
219219
220- pub fn access ( path : & Path , mode : u8 ) -> bool {
221- let wide = path. as_os_str ( ) . to_wide_with_nul ( ) ;
220+ pub fn access ( path : & Path , mode : u8 ) -> Result < bool , io :: Error > {
221+ let wide = path. as_os_str ( ) . to_wide_with_nul ( ) ? ;
222222 let attr = unsafe { GetFileAttributesW ( wide. as_ptr ( ) ) } ;
223- attr != INVALID_FILE_ATTRIBUTES
223+ Ok ( attr != INVALID_FILE_ATTRIBUTES
224224 && ( mode & 2 == 0
225225 || attr & FILE_ATTRIBUTE_READONLY == 0
226- || attr & windows_sys:: Win32 :: Storage :: FileSystem :: FILE_ATTRIBUTE_DIRECTORY != 0 )
226+ || attr & windows_sys:: Win32 :: Storage :: FileSystem :: FILE_ATTRIBUTE_DIRECTORY != 0 ) )
227227}
228228
229229pub fn remove ( path : & Path ) -> io:: Result < ( ) > {
@@ -882,7 +882,7 @@ pub fn win32_xstat(path: &OsStr, traverse: bool) -> io::Result<StatStruct> {
882882 Ok ( result)
883883}
884884
885- pub fn test_file_type_by_name ( path : & Path , tested_type : TestType ) -> bool {
885+ pub fn test_file_type_by_name ( path : & Path , tested_type : TestType ) -> Result < bool , io :: Error > {
886886 match get_file_information_by_name (
887887 path. as_os_str ( ) ,
888888 FILE_INFO_BY_NAME_CLASS :: FileStatBasicByNameInfo ,
@@ -906,14 +906,14 @@ pub fn test_file_type_by_name(path: &Path, tested_type: TestType) -> bool {
906906 & windows_sys:: Win32 :: Storage :: FileSystem :: FILE_ATTRIBUTE_REPARSE_POINT )
907907 == 0
908908 {
909- return result;
909+ return Ok ( result) ;
910910 }
911911 }
912912 Err ( err) => {
913913 if let Some ( code) = err. raw_os_error ( )
914914 && file_info_error_is_trustworthy ( code as u32 )
915915 {
916- return false ;
916+ return Ok ( false ) ;
917917 }
918918 }
919919 }
@@ -922,7 +922,7 @@ pub fn test_file_type_by_name(path: &Path, tested_type: TestType) -> bool {
922922 if !matches ! ( tested_type, TestType :: RegularFile | TestType :: Directory ) {
923923 flags |= FILE_FLAG_OPEN_REPARSE_POINT ;
924924 }
925- let wide_path = path. as_os_str ( ) . to_wide_with_nul ( ) ;
925+ let wide_path = path. as_os_str ( ) . to_wide_with_nul ( ) ? ;
926926 let handle = unsafe {
927927 CreateFileW (
928928 wide_path. as_ptr ( ) ,
@@ -937,7 +937,7 @@ pub fn test_file_type_by_name(path: &Path, tested_type: TestType) -> bool {
937937 if handle != INVALID_HANDLE_VALUE {
938938 let result = test_file_type_by_handle ( handle, tested_type, false ) ;
939939 unsafe { CloseHandle ( handle) } ;
940- return result;
940+ return Ok ( result) ;
941941 }
942942
943943 match unsafe { GetLastError ( ) } {
@@ -962,10 +962,10 @@ pub fn test_file_type_by_name(path: &Path, tested_type: TestType) -> bool {
962962 _ => { }
963963 }
964964
965- false
965+ Ok ( false )
966966}
967967
968- pub fn test_file_exists_by_name ( path : & Path , follow_links : bool ) -> bool {
968+ pub fn test_file_exists_by_name ( path : & Path , follow_links : bool ) -> Result < bool , io :: Error > {
969969 match get_file_information_by_name (
970970 path. as_os_str ( ) ,
971971 FILE_INFO_BY_NAME_CLASS :: FileStatBasicByNameInfo ,
@@ -976,19 +976,19 @@ pub fn test_file_exists_by_name(path: &Path, follow_links: bool) -> bool {
976976 == 0
977977 || ( !follow_links && is_reparse_tag_name_surrogate ( info. ReparseTag ) )
978978 {
979- return true ;
979+ return Ok ( true ) ;
980980 }
981981 }
982982 Err ( err) => {
983983 if let Some ( code) = err. raw_os_error ( )
984984 && file_info_error_is_trustworthy ( code as u32 )
985985 {
986- return false ;
986+ return ( false ) ;
987987 }
988988 }
989989 }
990990
991- let wide_path = path. as_os_str ( ) . to_wide_with_nul ( ) ;
991+ let wide_path = path. as_os_str ( ) . to_wide_with_nul ( ) ? ;
992992 let mut flags = FILE_FLAG_BACKUP_SEMANTICS ;
993993 if !follow_links {
994994 flags |= FILE_FLAG_OPEN_REPARSE_POINT ;
@@ -1007,13 +1007,13 @@ pub fn test_file_exists_by_name(path: &Path, follow_links: bool) -> bool {
10071007 if handle != INVALID_HANDLE_VALUE {
10081008 if follow_links {
10091009 unsafe { CloseHandle ( handle) } ;
1010- return true ;
1010+ return Ok ( true ) ;
10111011 }
10121012 let is_regular_reparse_point =
10131013 test_file_type_by_handle ( handle, TestType :: RegularReparsePoint , false ) ;
10141014 unsafe { CloseHandle ( handle) } ;
10151015 if !is_regular_reparse_point {
1016- return true ;
1016+ return Ok ( true ) ;
10171017 }
10181018 let handle = unsafe {
10191019 CreateFileW (
@@ -1028,7 +1028,7 @@ pub fn test_file_exists_by_name(path: &Path, follow_links: bool) -> bool {
10281028 } ;
10291029 if handle != INVALID_HANDLE_VALUE {
10301030 unsafe { CloseHandle ( handle) } ;
1031- return true ;
1031+ return Ok ( true ) ;
10321032 }
10331033 }
10341034
@@ -1037,16 +1037,16 @@ pub fn test_file_exists_by_name(path: &Path, follow_links: bool) -> bool {
10371037 | windows_sys:: Win32 :: Foundation :: ERROR_SHARING_VIOLATION
10381038 | windows_sys:: Win32 :: Foundation :: ERROR_CANT_ACCESS_FILE
10391039 | windows_sys:: Win32 :: Foundation :: ERROR_INVALID_PARAMETER => {
1040- return win32_xstat ( path. as_os_str ( ) , follow_links) . is_ok ( ) ;
1040+ return Ok ( win32_xstat ( path. as_os_str ( ) , follow_links) . is_ok ( ) ) ;
10411041 }
10421042 _ => { }
10431043 }
10441044
1045- false
1045+ Ok ( false )
10461046}
10471047
1048- pub fn path_exists_via_open ( path : & Path , follow_links : bool ) -> bool {
1049- let wide_path = path. as_os_str ( ) . to_wide_with_nul ( ) ;
1048+ pub fn path_exists_via_open ( path : & Path , follow_links : bool ) -> Result < bool , io :: Error > {
1049+ let wide_path = path. as_os_str ( ) . to_wide_with_nul ( ) ? ;
10501050 let mut flags = FILE_FLAG_BACKUP_SEMANTICS ;
10511051 if !follow_links {
10521052 flags |= FILE_FLAG_OPEN_REPARSE_POINT ;
@@ -1065,13 +1065,13 @@ pub fn path_exists_via_open(path: &Path, follow_links: bool) -> bool {
10651065 if handle != INVALID_HANDLE_VALUE {
10661066 if follow_links {
10671067 unsafe { CloseHandle ( handle) } ;
1068- return true ;
1068+ return Ok ( true ) ;
10691069 }
10701070 let is_regular_reparse_point =
10711071 test_file_type_by_handle ( handle, TestType :: RegularReparsePoint , false ) ;
10721072 unsafe { CloseHandle ( handle) } ;
10731073 if !is_regular_reparse_point {
1074- return true ;
1074+ return Ok ( true ) ;
10751075 }
10761076 let handle = unsafe {
10771077 CreateFileW (
@@ -1086,10 +1086,10 @@ pub fn path_exists_via_open(path: &Path, follow_links: bool) -> bool {
10861086 } ;
10871087 if handle != INVALID_HANDLE_VALUE {
10881088 unsafe { CloseHandle ( handle) } ;
1089- return true ;
1089+ return Ok ( true ) ;
10901090 }
10911091 }
1092- false
1092+ Ok ( false )
10931093}
10941094
10951095pub fn fd_exists ( fd : crate :: crt_fd:: Borrowed < ' _ > ) -> bool {
@@ -1270,7 +1270,10 @@ pub fn readlink(path: &Path) -> Result<OsString, ReadlinkError> {
12701270 IO_REPARSE_TAG_MOUNT_POINT , IO_REPARSE_TAG_SYMLINK ,
12711271 } ;
12721272
1273- let wide_path = path. as_os_str ( ) . to_wide_with_nul ( ) ;
1273+ let wide_path = path
1274+ . as_os_str ( )
1275+ . to_wide_with_nul ( )
1276+ . map_err ( ReadlinkError :: Io ) ?;
12741277 let handle = unsafe {
12751278 CreateFileW (
12761279 wide_path. as_ptr ( ) ,
@@ -1369,7 +1372,7 @@ pub fn kill(pid: u32, sig: u32) -> io::Result<()> {
13691372pub fn getfinalpathname ( path : & Path ) -> io:: Result < OsString > {
13701373 use windows_sys:: Win32 :: Storage :: FileSystem :: { GetFinalPathNameByHandleW , VOLUME_NAME_DOS } ;
13711374
1372- let wide = path. as_os_str ( ) . to_wide_with_nul ( ) ;
1375+ let wide = path. as_os_str ( ) . to_wide_with_nul ( ) ? ;
13731376 let handle = unsafe {
13741377 CreateFileW (
13751378 wide. as_ptr ( ) ,
@@ -1407,7 +1410,7 @@ pub fn getfinalpathname(path: &Path) -> io::Result<OsString> {
14071410}
14081411
14091412pub fn getfullpathname ( path : & Path ) -> io:: Result < OsString > {
1410- let wide = path. as_os_str ( ) . to_wide_with_nul ( ) ;
1413+ let wide = path. as_os_str ( ) . to_wide_with_nul ( ) ? ;
14111414 let mut buffer = vec ! [ 0u16 ; MAX_PATH as usize ] ;
14121415 let mut ret = unsafe {
14131416 windows_sys:: Win32 :: Storage :: FileSystem :: GetFullPathNameW (
@@ -1435,7 +1438,7 @@ pub fn getfullpathname(path: &Path) -> io::Result<OsString> {
14351438}
14361439
14371440pub fn getvolumepathname ( path : & Path ) -> io:: Result < OsString > {
1438- let wide = path. as_os_str ( ) . to_wide_with_nul ( ) ;
1441+ let wide = path. as_os_str ( ) . to_wide_with_nul ( ) ? ;
14391442 let buflen = core:: cmp:: max ( wide. len ( ) , MAX_PATH as usize ) ;
14401443 let mut buffer = vec ! [ 0u16 ; buflen] ;
14411444 unsafe {
@@ -1452,7 +1455,7 @@ pub fn getvolumepathname(path: &Path) -> io::Result<OsString> {
14521455pub fn getdiskusage ( path : & Path ) -> io:: Result < ( u64 , u64 ) > {
14531456 use windows_sys:: Win32 :: Storage :: FileSystem :: GetDiskFreeSpaceExW ;
14541457
1455- let wide = path. as_os_str ( ) . to_wide_with_nul ( ) ;
1458+ let wide = path. as_os_str ( ) . to_wide_with_nul ( ) ? ;
14561459 let mut free_to_me = 0u64 ;
14571460 let mut total = 0u64 ;
14581461 let mut free = 0u64 ;
@@ -1584,7 +1587,7 @@ pub fn listvolumes() -> io::Result<Vec<OsString>> {
15841587}
15851588
15861589pub fn listmounts ( volume : & Path ) -> io:: Result < Vec < OsString > > {
1587- let wide = volume. as_os_str ( ) . to_wide_with_nul ( ) ;
1590+ let wide = volume. as_os_str ( ) . to_wide_with_nul ( ) ? ;
15881591 let mut buflen: u32 = MAX_PATH + 1 ;
15891592 let mut buffer = vec ! [ 0u16 ; buflen as usize ] ;
15901593
@@ -1676,6 +1679,7 @@ pub fn getppid() -> u32 {
16761679
16771680pub fn path_skip_root ( path : & widestring:: WideCStr ) -> Option < usize > {
16781681 let mut end: * const u16 = core:: ptr:: null ( ) ;
1682+ // SAFETY: `path` is a valid pointer to a nul terminated wide string without interior nuls.
16791683 let hr = unsafe { windows_sys:: Win32 :: UI :: Shell :: PathCchSkipRoot ( path. as_ptr ( ) , & mut end) } ;
16801684 if hr >= 0 {
16811685 assert ! ( !end. is_null( ) ) ;
0 commit comments