Skip to content

Commit

Permalink
Bump MSRV to 1.65 (#1108)
Browse files Browse the repository at this point in the history
* Raise MSRV to 1.65

The primary motivation is the option to use let-else for the C-like error handling that low level system APIs usually require.

* Simplify error handling in windows/disk.rs
  • Loading branch information
chklauser authored Oct 22, 2023
1 parent 5f69334 commit 911d230
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 52 deletions.
8 changes: 4 additions & 4 deletions .cirrus.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
task:
name: rust 1.63 on freebsd 13
name: rust 1.65 on freebsd 13
freebsd_instance:
image: freebsd-13-1-release-amd64
setup_script:
- curl https://sh.rustup.rs -sSf --output rustup.sh
- sh rustup.sh -y --profile=minimal --default-toolchain=1.63
- sh rustup.sh -y --profile=minimal --default-toolchain=1.65
- . $HOME/.cargo/env
- rustup --version
- rustup component add clippy
Expand Down Expand Up @@ -37,14 +37,14 @@ task:
- FREEBSD_CI=1 cargo test --lib -j1 -- --ignored

task:
name: rust 1.63 on mac m1
name: rust 1.65 on mac m1
macos_instance:
image: ghcr.io/cirruslabs/macos-monterey-base:latest
setup_script:
- brew update
- brew install curl
- curl https://sh.rustup.rs -sSf --output rustup.sh
- sh rustup.sh -y --profile=minimal --default-toolchain=1.63
- sh rustup.sh -y --profile=minimal --default-toolchain=1.65
- source $HOME/.cargo/env
- rustup --version
- rustup component add clippy
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:
- { os: 'ubuntu-latest', target: 'x86_64-linux-android', cross: true }
- { os: 'ubuntu-latest', target: 'i686-linux-android', cross: true }
toolchain:
- "1.63.0" # minimum supported rust version
- "1.65.0" # minimum supported rust version
- stable
- nightly
steps:
Expand Down Expand Up @@ -139,7 +139,7 @@ jobs:
- macos-latest
- windows-latest
toolchain:
- "1.63.0" # minimum supported rust version
- "1.65.0" # minimum supported rust version
- stable
- nightly
steps:
Expand Down Expand Up @@ -205,7 +205,7 @@ jobs:
strategy:
matrix:
toolchain:
- "1.63.0" # minimum supported rust version
- "1.65.0" # minimum supported rust version
- stable
steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description = "Library to get system information such as processes, CPUs, disks,
repository = "https://github.com/GuillaumeGomez/sysinfo"
license = "MIT"
readme = "README.md"
rust-version = "1.63"
rust-version = "1.65"
exclude = ["/test-unknown"]
categories = ["filesystem", "os", "api-bindings"]
edition = "2018"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ You can still use `sysinfo` on non-supported OSes, it'll simply do nothing and a
empty values. You can check in your program directly if an OS is supported by checking the
[`IS_SUPPORTED`] constant.

The minimum-supported version of `rustc` is **1.63**.
The minimum-supported version of `rustc` is **1.65**.

## Usage

Expand Down
69 changes: 26 additions & 43 deletions src/windows/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ use std::mem::size_of;
use std::os::windows::ffi::OsStringExt;
use std::path::Path;

use windows::core::PCWSTR;
use windows::Win32::Foundation::{
CloseHandle, GetLastError, ERROR_MORE_DATA, ERROR_NO_MORE_FILES, ERROR_UNRECOGNIZED_VOLUME,
HANDLE, MAX_PATH,
};
use windows::core::{Error, HRESULT, PCWSTR};
use windows::Win32::Foundation::{CloseHandle, HANDLE, MAX_PATH};
use windows::Win32::Storage::FileSystem::{
CreateFileW, FindFirstVolumeW, FindNextVolumeW, FindVolumeClose, GetDiskFreeSpaceExW,
GetDriveTypeW, GetVolumeInformationW, GetVolumePathNamesForVolumeNameW, FILE_ACCESS_RIGHTS,
Expand All @@ -36,6 +33,9 @@ fn from_zero_terminated(buf: &[u16]) -> Vec<u16> {
// https://learn.microsoft.com/en-us/windows/win32/fileio/displaying-volume-paths
const VOLUME_NAME_SIZE: usize = MAX_PATH as usize + 1;

const ERROR_NO_MORE_FILES: HRESULT = windows::Win32::Foundation::ERROR_NO_MORE_FILES.to_hresult();
const ERROR_MORE_DATA: HRESULT = windows::Win32::Foundation::ERROR_MORE_DATA.to_hresult();

/// Returns a list of zero-terminated wide strings containing volume GUID paths.
/// Volume GUID paths have the form `\\?\{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}\`.
///
Expand All @@ -44,31 +44,25 @@ pub(crate) fn get_volume_guid_paths() -> Vec<Vec<u16>> {
let mut volume_names = Vec::new();
unsafe {
let mut buf = Box::new([0u16; VOLUME_NAME_SIZE]);
let handle = match FindFirstVolumeW(&mut buf[..]) {
Ok(handle) => handle,
Err(_) => {
sysinfo_debug!("Error: FindFirstVolumeW() = {:?}", GetLastError());
return Vec::new();
}
let Ok(handle) = FindFirstVolumeW(&mut buf[..]) else {
sysinfo_debug!(
"Error: FindFirstVolumeW() = {:?}",
Error::from_win32().code()
);
return Vec::new();
};
volume_names.push(from_zero_terminated(&buf[..]));
loop {
match FindNextVolumeW(handle, &mut buf[..]) {
Ok(_) => (),
Err(_) => {
let find_next_err = GetLastError().expect_err(
"GetLastError should return an error after FindNextVolumeW returned zero.",
);
if find_next_err.code() != ERROR_NO_MORE_FILES.to_hresult() {
sysinfo_debug!("Error: FindNextVolumeW = {}", find_next_err);
}
break;
if FindNextVolumeW(handle, &mut buf[..]).is_err() {
if Error::from_win32().code() != ERROR_NO_MORE_FILES {
sysinfo_debug!("Error: FindNextVolumeW = {}", Error::from_win32().code());
}
break;
}
volume_names.push(from_zero_terminated(&buf[..]));
}
if FindVolumeClose(handle).is_err() {
sysinfo_debug!("Error: FindVolumeClose = {:?}", GetLastError());
sysinfo_debug!("Error: FindVolumeClose = {:?}", Error::from_win32().code());
};
}
volume_names
Expand All @@ -95,18 +89,10 @@ pub(crate) unsafe fn get_volume_path_names_for_volume_name(
Some(path_names_buf.as_mut_slice()),
&mut path_names_output_size,
);
let code = volume_path_names
.map_err(|_| match GetLastError() {
Ok(_) => {
sysinfo_debug!("GetLastError should return an error after GetVolumePathNamesForVolumeNameW returned zero.");
// We return an error in any case that is not `ERROR_MORE_DATA` to stop the loop.
ERROR_UNRECOGNIZED_VOLUME.to_hresult()
}
Err(e) => e.code(),
});
let code = volume_path_names.map_err(|_| Error::from_win32().code());
match code {
Ok(()) => break,
Err(e) if e == ERROR_MORE_DATA.to_hresult() => {
Err(ERROR_MORE_DATA) => {
// We need a bigger buffer. path_names_output_size contains the required buffer size.
path_names_buf = vec![0u16; path_names_output_size as usize];
continue;
Expand Down Expand Up @@ -280,7 +266,10 @@ pub(crate) unsafe fn get_list() -> Vec<Disk> {
)
.is_ok();
if !volume_info_res {
sysinfo_debug!("Error: GetVolumeInformationW = {:?}", GetLastError());
sysinfo_debug!(
"Error: GetVolumeInformationW = {:?}",
Error::from_win32().code()
);
return Vec::new();
}

Expand All @@ -295,17 +284,11 @@ pub(crate) unsafe fn get_list() -> Vec<Disk> {
.copied()
.chain([0])
.collect::<Vec<_>>();
let handle = match HandleWrapper::new(&device_path[..], Default::default()) {
Some(h) => h,
None => {
return Vec::new();
}
let Some(handle) = HandleWrapper::new(&device_path[..], Default::default()) else {
return Vec::new();
};
let (total_space, available_space) = match get_drive_size(&mount_paths[0][..]) {
Some(space) => space,
None => {
return Vec::new();
}
let Some((total_space, available_space)) = get_drive_size(&mount_paths[0][..]) else {
return Vec::new();
};
if total_space == 0 {
sysinfo_debug!("total_space == 0");
Expand Down

0 comments on commit 911d230

Please sign in to comment.