Skip to content

Commit

Permalink
Remove Disks::refresh_list
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Nov 30, 2024
1 parent 1178cc9 commit b4b139d
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 127 deletions.
11 changes: 1 addition & 10 deletions benches/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,7 @@ fn bench_refresh_disks(b: &mut test::Bencher) {
let mut disks = sysinfo::Disks::new_with_refreshed_list();

b.iter(move || {
disks.refresh();
});
}

#[bench]
fn bench_refresh_disks_list(b: &mut test::Bencher) {
let mut disks = sysinfo::Disks::new_with_refreshed_list();

b.iter(move || {
disks.refresh_list(true);
disks.refresh(true);
});
}

Expand Down
2 changes: 1 addition & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ fn interpret_input(
"help" => print_help(),
"refresh_disks" => {
writeln!(&mut io::stdout(), "Refreshing disk list...");
disks.refresh_list(true);
disks.refresh(true);
writeln!(&mut io::stdout(), "Done.");
}
"refresh_users" => {
Expand Down
16 changes: 1 addition & 15 deletions src/c_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,7 @@ pub extern "C" fn sysinfo_disks_refresh(disks: CDisks) {
let mut disks: Box<Disks> = Box::from_raw(disks as *mut Disks);
{
let disks: &mut Disks = disks.borrow_mut();
disks.refresh();
}
let _ = Box::into_raw(disks);
}
}

/// Equivalent of [`Disks::refresh_list()`][crate::Disks#method.refresh_list].
#[no_mangle]
pub extern "C" fn sysinfo_disks_refresh_list(disks: CDisks) {
assert!(!disks.is_null());
unsafe {
let mut disks: Box<Disks> = Box::from_raw(disks as *mut Disks);
{
let disks: &mut Disks = disks.borrow_mut();
disks.refresh_list(true);
disks.refresh(true);
}
let _ = Box::into_raw(disks);
}
Expand Down
64 changes: 9 additions & 55 deletions src/common/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl Disks {
/// use sysinfo::Disks;
///
/// let mut disks = Disks::new();
/// disks.refresh_list(false);
/// disks.refresh(false);
/// for disk in disks.list() {
/// println!("{disk:?}");
/// }
Expand Down Expand Up @@ -290,7 +290,7 @@ impl Disks {
/// ```
pub fn new_with_refreshed_list_specifics(refreshes: DiskRefreshKind) -> Self {
let mut disks = Self::new();
disks.refresh_list_specifics(false, refreshes);
disks.refresh_specifics(false, refreshes);
disks
}

Expand Down Expand Up @@ -326,69 +326,23 @@ impl Disks {
/// Refreshes the listed disks' information.
///
/// Equivalent to <code>[Disks::refresh_specifics]\([DiskRefreshKind::everything]\())</code>.
pub fn refresh(&mut self) {
self.refresh_specifics(DiskRefreshKind::everything());
pub fn refresh(&mut self, remove_not_listed_disks: bool) {
self.inner
.refresh_specifics(remove_not_listed_disks, DiskRefreshKind::everything());
}

/// Refreshes the listed disks' information according to the given [`DiskRefreshKind`].
///
/// ⚠️ If a disk is added or removed, this method won't take it into account. Use
/// [`Disks::refresh_list`] instead.
///
/// ⚠️ If you didn't call [`Disks::refresh_list`] beforehand, this method will do nothing as
/// the disk list will be empty.
/// Refreshes the disks' information according to the given [`DiskRefreshKind`].
///
/// ```no_run
/// use sysinfo::Disks;
///
/// let mut disks = Disks::new_with_refreshed_list();
/// // We wait some time...?
/// disks.refresh();
/// ```
pub fn refresh_specifics(&mut self, refreshes: DiskRefreshKind) {
self.inner.refresh_specifics(refreshes);
}

/// The disk list will be emptied then completely recomputed.
///
/// Equivalent to <code>[Disks::refresh_list_specifics]\([DiskRefreshKind::everything]\())</code>.
///
/// ```no_run
/// use sysinfo::Disks;
///
/// let mut disks = Disks::new();
/// disks.refresh_list(true);
/// ```
pub fn refresh_list(&mut self, remove_not_listed_disks: bool) {
self.refresh_list_specifics(remove_not_listed_disks, DiskRefreshKind::everything());
}

/// The disk list will be emptied then completely recomputed according to the given
/// [`DiskRefreshKind`].
///
/// ## Linux
///
/// ⚠️ On Linux, the [NFS](https://en.wikipedia.org/wiki/Network_File_System) file
/// systems are ignored and the information of a mounted NFS **cannot** be obtained
/// via [`Disks::refresh_list_specifics`]. This is due to the fact that I/O function
/// `statvfs` used by [`Disks::refresh_list_specifics`] is blocking and
/// [may hang](https://github.com/GuillaumeGomez/sysinfo/pull/876) in some cases,
/// requiring to call `systemctl stop` to terminate the NFS service from the remote
/// server in some cases.
///
/// ```no_run
/// use sysinfo::{Disks, DiskRefreshKind};
///
/// let mut disks = Disks::new();
/// disks.refresh_list_specifics(true, DiskRefreshKind::nothing());
/// disks.refresh(true);
/// ```
pub fn refresh_list_specifics(
&mut self,
remove_not_listed_disks: bool,
refreshes: DiskRefreshKind,
) {
pub fn refresh_specifics(&mut self, remove_not_listed_disks: bool, refreshes: DiskRefreshKind) {
self.inner
.refresh_list_specifics(remove_not_listed_disks, refreshes);
.refresh_specifics(remove_not_listed_disks, refreshes);
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/sysinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ void sysinfo_refresh_process(CSystem system, PID pid);
CDisks sysinfo_disks_init(void);
void sysinfo_disks_destroy(CDisks disks);
void sysinfo_disks_refresh(CDisks disks);
void sysinfo_disks_refresh_list(CDisks disks);

size_t sysinfo_total_memory(CSystem system);
size_t sysinfo_free_memory(CSystem system);
Expand All @@ -57,7 +56,6 @@ size_t sysinfo_process_virtual_memory(CProcess process);
RString sysinfo_process_executable_path(CProcess process);
RString sysinfo_process_root_directory(CProcess process);
RString sysinfo_process_current_directory(CProcess process);
void sysinfo_networks_refresh_list(CNetworks networks);
void sysinfo_networks_refresh(CNetworks networks);
size_t sysinfo_networks_received(CNetworks networks);
size_t sysinfo_networks_transmitted(CNetworks networks);
Expand Down
8 changes: 1 addition & 7 deletions src/unix/apple/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl crate::DisksInner {
}
}

pub(crate) fn refresh_list_specifics(
pub(crate) fn refresh_specifics(
&mut self,
remove_not_listed_disks: bool,
refresh_kind: DiskRefreshKind,
Expand All @@ -192,12 +192,6 @@ impl crate::DisksInner {
}
}

pub(crate) fn refresh_specifics(&mut self, refresh_kind: DiskRefreshKind) {
for disk in self.list_mut() {
disk.refresh_specifics(refresh_kind);
}
}

pub(crate) fn list(&self) -> &[Disk] {
&self.disks
}
Expand Down
13 changes: 3 additions & 10 deletions src/unix/freebsd/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ impl crate::DisksInner {
}
}

pub(crate) fn refresh_list_specifics(
pub(crate) fn refresh_specifics(
&mut self,
remove_not_listed_disks: bool,
refresh_kind: DiskRefreshKind,
) {
unsafe { get_all_list(&mut self.disks, true, remove_not_listed_disks, refresh_kind) }
unsafe { get_all_list(&mut self.disks, remove_not_listed_disks, refresh_kind) }
}

pub(crate) fn list(&self) -> &[Disk] {
Expand All @@ -106,12 +106,6 @@ impl crate::DisksInner {
pub(crate) fn list_mut(&mut self) -> &mut [Disk] {
&mut self.disks
}

pub(crate) fn refresh_specifics(&mut self, refresh_kind: DiskRefreshKind) {
unsafe {
get_all_list(&mut self.disks, false, false, refresh_kind);
}
}
}

trait GetValues {
Expand Down Expand Up @@ -322,7 +316,6 @@ fn get_disks_mapping() -> HashMap<String, String> {

pub unsafe fn get_all_list(
container: &mut Vec<Disk>,
add_new_disks: bool,
remove_not_listed_disks: bool,
refresh_kind: DiskRefreshKind,
) {
Expand Down Expand Up @@ -386,7 +379,7 @@ pub unsafe fn get_all_list(
// I/O usage is updated for all disks at once at the end.
refresh_disk(&mut disk.inner, refresh_kind.without_io_usage());
disk.inner.updated = true;
} else if add_new_disks {
} else {
let dev_mount_point = c_buf_to_utf8_str(&fs_info.f_mntfromname).unwrap_or("");

// USB keys and CDs are removable.
Expand Down
10 changes: 1 addition & 9 deletions src/unix/linux/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl crate::DisksInner {
}
}

pub(crate) fn refresh_list_specifics(
pub(crate) fn refresh_specifics(
&mut self,
remove_not_listed_disks: bool,
refresh_kind: DiskRefreshKind,
Expand All @@ -161,14 +161,6 @@ impl crate::DisksInner {
)
}

pub(crate) fn refresh_specifics(&mut self, refresh_kind: DiskRefreshKind) {
let procfs_disk_stats = disk_stats(&refresh_kind);
for disk in self.list_mut() {
disk.inner
.efficient_refresh(refresh_kind, &procfs_disk_stats, false);
}
}

pub(crate) fn list(&self) -> &[Disk] {
&self.disks
}
Expand Down
8 changes: 0 additions & 8 deletions src/unknown/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ impl DisksInner {
self.disks
}

pub(crate) fn refresh_list_specifics(
&mut self,
_remove_not_listed_disks: bool,
_refreshes: DiskRefreshKind,
) {
// Does nothing.
}

pub(crate) fn refresh_specifics(&mut self, _refreshes: DiskRefreshKind) {
// Does nothing.
}
Expand Down
8 changes: 1 addition & 7 deletions src/windows/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ impl DisksInner {
self.disks
}

pub(crate) fn refresh_list_specifics(
pub(crate) fn refresh_specifics(
&mut self,
remove_not_listed_disks: bool,
refreshes: DiskRefreshKind,
Expand All @@ -242,12 +242,6 @@ impl DisksInner {
}
}

pub(crate) fn refresh_specifics(&mut self, refreshes: DiskRefreshKind) {
for disk in self.list_mut() {
disk.refresh_specifics(refreshes);
}
}

pub(crate) fn list(&self) -> &[Disk] {
&self.disks
}
Expand Down
6 changes: 3 additions & 3 deletions tests/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn test_disks() {

let mut disks = sysinfo::Disks::new();
assert!(disks.list().is_empty());
disks.refresh_list(false);
disks.refresh(false);
assert!(!disks.list().is_empty());
}

Expand Down Expand Up @@ -125,7 +125,7 @@ fn test_disk_refresh_kind() {

// load with minimal `DiskRefreshKind`, then refresh for added detail should also work!
let mut disks = Disks::new_with_refreshed_list_specifics(DiskRefreshKind::nothing());
disks.refresh_specifics(refreshes);
disks.refresh_specifics(true, refreshes);
assertions("incremental", &disks);
}
}
Expand Down Expand Up @@ -168,7 +168,7 @@ fn test_disks_usage() {

// Wait a bit just in case
sleep(std::time::Duration::from_millis(500));
disks.refresh();
disks.refresh(false);

// Depending on the OS and how disks are configured, the disk usage may be the exact same
// across multiple disks. To account for this, collect the disk usages and dedup.
Expand Down

0 comments on commit b4b139d

Please sign in to comment.