mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
uucore::fsext: Replace some unsafe calls
GetLastError() and libc::stat() were unnecessary as libstd offered equivalents. LPWSTR2String() was technically unsafe if passed a slice without zeroes, but it's a private function and was probably always called correctly in practice.
This commit is contained in:
parent
e9d63519dd
commit
9d5133157a
1 changed files with 22 additions and 26 deletions
|
@ -30,12 +30,8 @@ use std::ffi::OsString;
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
use std::os::windows::ffi::OsStrExt;
|
use std::os::windows::ffi::OsStrExt;
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
use std::os::windows::ffi::OsStringExt;
|
|
||||||
#[cfg(windows)]
|
|
||||||
use winapi::shared::minwindef::DWORD;
|
use winapi::shared::minwindef::DWORD;
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
use winapi::um::errhandlingapi::GetLastError;
|
|
||||||
#[cfg(windows)]
|
|
||||||
use winapi::um::fileapi::GetDiskFreeSpaceW;
|
use winapi::um::fileapi::GetDiskFreeSpaceW;
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
use winapi::um::fileapi::{
|
use winapi::um::fileapi::{
|
||||||
|
@ -62,10 +58,8 @@ macro_rules! String2LPWSTR {
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
fn LPWSTR2String(buf: &[u16]) -> String {
|
fn LPWSTR2String(buf: &[u16]) -> String {
|
||||||
let len = unsafe { libc::wcslen(buf.as_ptr()) };
|
let len = buf.iter().position(|&n| n == 0).unwrap();
|
||||||
OsString::from_wide(&buf[..len as usize])
|
String::from_utf16(&buf[..len]).unwrap()
|
||||||
.into_string()
|
|
||||||
.unwrap()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
use self::time::Timespec;
|
use self::time::Timespec;
|
||||||
|
@ -77,7 +71,6 @@ use std::borrow::Cow;
|
||||||
use std::convert::{AsRef, From};
|
use std::convert::{AsRef, From};
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
#[cfg(unix)]
|
|
||||||
use std::io::Error as IOError;
|
use std::io::Error as IOError;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
@ -157,16 +150,14 @@ impl MountInfo {
|
||||||
fn set_missing_fields(&mut self) {
|
fn set_missing_fields(&mut self) {
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
{
|
{
|
||||||
|
use std::os::unix::fs::MetadataExt;
|
||||||
// We want to keep the dev_id on Windows
|
// We want to keep the dev_id on Windows
|
||||||
// but set dev_id
|
// but set dev_id
|
||||||
let path = CString::new(self.mount_dir.clone()).unwrap();
|
if let Ok(stat) = std::fs::metadata(&self.mount_dir) {
|
||||||
unsafe {
|
// Why do we cast this to i32?
|
||||||
let mut stat = mem::zeroed();
|
self.dev_id = (stat.dev() as i32).to_string()
|
||||||
if libc::stat(path.as_ptr(), &mut stat) == 0 {
|
} else {
|
||||||
self.dev_id = (stat.st_dev as i32).to_string();
|
self.dev_id = "".to_string();
|
||||||
} else {
|
|
||||||
self.dev_id = "".to_string();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// set MountInfo::dummy
|
// set MountInfo::dummy
|
||||||
|
@ -445,9 +436,11 @@ pub fn read_fs_list() -> Vec<MountInfo> {
|
||||||
FindFirstVolumeW(volume_name_buf.as_mut_ptr(), volume_name_buf.len() as DWORD)
|
FindFirstVolumeW(volume_name_buf.as_mut_ptr(), volume_name_buf.len() as DWORD)
|
||||||
};
|
};
|
||||||
if INVALID_HANDLE_VALUE == find_handle {
|
if INVALID_HANDLE_VALUE == find_handle {
|
||||||
crash!(EXIT_ERR, "FindFirstVolumeW failed: {}", unsafe {
|
crash!(
|
||||||
GetLastError()
|
EXIT_ERR,
|
||||||
});
|
"FindFirstVolumeW failed: {}",
|
||||||
|
IOError::last_os_error()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
let mut mounts = Vec::<MountInfo>::new();
|
let mut mounts = Vec::<MountInfo>::new();
|
||||||
loop {
|
loop {
|
||||||
|
@ -466,8 +459,9 @@ pub fn read_fs_list() -> Vec<MountInfo> {
|
||||||
volume_name_buf.len() as DWORD,
|
volume_name_buf.len() as DWORD,
|
||||||
)
|
)
|
||||||
} {
|
} {
|
||||||
let err = unsafe { GetLastError() };
|
let err = IOError::last_os_error();
|
||||||
if err != winapi::shared::winerror::ERROR_NO_MORE_FILES {
|
if err.raw_os_error() != Some(winapi::shared::winerror::ERROR_NO_MORE_FILES as i32)
|
||||||
|
{
|
||||||
crash!(EXIT_ERR, "FindNextVolumeW failed: {}", err);
|
crash!(EXIT_ERR, "FindNextVolumeW failed: {}", err);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -527,7 +521,7 @@ impl FsUsage {
|
||||||
crash!(
|
crash!(
|
||||||
EXIT_ERR,
|
EXIT_ERR,
|
||||||
"GetVolumePathNamesForVolumeNameW failed: {}",
|
"GetVolumePathNamesForVolumeNameW failed: {}",
|
||||||
unsafe { GetLastError() }
|
IOError::last_os_error()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -547,9 +541,11 @@ impl FsUsage {
|
||||||
};
|
};
|
||||||
if 0 == success {
|
if 0 == success {
|
||||||
// Fails in case of CD for example
|
// Fails in case of CD for example
|
||||||
//crash!(EXIT_ERR, "GetDiskFreeSpaceW failed: {}", unsafe {
|
// crash!(
|
||||||
//GetLastError()
|
// EXIT_ERR,
|
||||||
//});
|
// "GetDiskFreeSpaceW failed: {}",
|
||||||
|
// IOError::last_os_error()
|
||||||
|
// );
|
||||||
}
|
}
|
||||||
|
|
||||||
let bytes_per_cluster = sectors_per_cluster as u64 * bytes_per_sector as u64;
|
let bytes_per_cluster = sectors_per_cluster as u64 * bytes_per_sector as u64;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue