diff --git a/src/uucore/src/lib/features/safe_traversal.rs b/src/uucore/src/lib/features/safe_traversal.rs index 8ae51f563..c6dc2f2c6 100644 --- a/src/uucore/src/lib/features/safe_traversal.rs +++ b/src/uucore/src/lib/features/safe_traversal.rs @@ -133,7 +133,6 @@ pub struct DirFd { impl DirFd { /// Open a directory and return a file descriptor pub fn open(path: &Path) -> io::Result { - let path_str = path.to_string_lossy(); let path_cstr = CString::new(path.as_os_str().as_bytes()) .map_err(|_| SafeTraversalError::PathContainsNull)?; @@ -146,7 +145,7 @@ impl DirFd { if fd < 0 { Err(SafeTraversalError::OpenFailed { - path: path_str.to_string(), + path: path.to_string_lossy().into_owned(), source: io::Error::last_os_error(), } .into()) @@ -157,7 +156,6 @@ impl DirFd { /// Open a subdirectory relative to this directory pub fn open_subdir(&self, name: &OsStr) -> io::Result { - let name_str = name.to_string_lossy(); let name_cstr = CString::new(name.as_bytes()).map_err(|_| SafeTraversalError::PathContainsNull)?; @@ -171,7 +169,7 @@ impl DirFd { if fd < 0 { Err(SafeTraversalError::OpenFailed { - path: name_str.to_string(), + path: name.to_string_lossy().into_owned(), source: io::Error::last_os_error(), } .into()) @@ -182,7 +180,6 @@ impl DirFd { /// Get raw stat data for a file relative to this directory pub fn stat_at(&self, name: &OsStr, follow_symlinks: bool) -> io::Result { - let name_str = name.to_string_lossy(); let name_cstr = CString::new(name.as_bytes()).map_err(|_| SafeTraversalError::PathContainsNull)?; @@ -197,7 +194,7 @@ impl DirFd { if ret < 0 { Err(SafeTraversalError::StatFailed { - path: name_str.to_string(), + path: name.to_string_lossy().into_owned(), source: io::Error::last_os_error(), } .into()) @@ -264,7 +261,6 @@ impl DirFd { /// Remove a file or empty directory relative to this directory pub fn unlink_at(&self, name: &OsStr, is_dir: bool) -> io::Result<()> { - let name_str = name.to_string_lossy(); let name_cstr = CString::new(name.as_bytes()).map_err(|_| SafeTraversalError::PathContainsNull)?; let flags = if is_dir { libc::AT_REMOVEDIR } else { 0 }; @@ -273,7 +269,7 @@ impl DirFd { if ret < 0 { Err(SafeTraversalError::UnlinkFailed { - path: name_str.to_string(), + path: name.to_string_lossy().into_owned(), source: io::Error::last_os_error(), } .into())