Skip to content

Commit

Permalink
Update to sysinfo 0.30
Browse files Browse the repository at this point in the history
  • Loading branch information
zeld committed Jun 8, 2024
1 parent b7c4da9 commit f548c3e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 36 deletions.
41 changes: 30 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ regex = "1"

tar = "0.4"

sysinfo = "0.29"
sysinfo = "0.30"
23 changes: 8 additions & 15 deletions src/disk.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
use std::env::current_dir;
use std::fs;
use std::str;

use sysinfo::{Disk, DiskExt, System, SystemExt};
use sysinfo::{Disk, Disks};

use console::Style;
use indicatif::DecimalBytes;

use log::debug;

// Print disks list as a table
// Requires list of disk to be up to date:
// let mut sys: System = System::new();
// sys.refresh_disks_list();
// sys.refresh_disks();
pub fn print_disks(sys: &System) {
pub fn print_disks() {
println!(
"{0: ^20} | {1: ^30} | {2: ^6} | {3: ^9} | {4: ^10} | {5: ^5} ",
"Name", "Mount point", "Type", "Removable", "Avail.", "Empty"
);
let red = Style::new().red();
let green = Style::new().green();
for disk in sys.disks() {
let disks = Disks::new_with_refreshed_list();
for disk in &disks {
let disk_removable = if disk.is_removable() {
green.apply_to("Yes")
} else {
red.apply_to("No")
};
let file_system_str = str::from_utf8(disk.file_system()).unwrap();
let file_system_str = disk.file_system().to_string_lossy();
let file_system = if file_system_str.eq_ignore_ascii_case("vfat")
|| file_system_str.eq_ignore_ascii_case("fat32")
{
Expand Down Expand Up @@ -59,11 +55,7 @@ pub fn print_disks(sys: &System) {
}

// Available disk space in current directory
// Requires list of disk to be up to date:
// let mut sys: System = System::new();
// sys.refresh_disks_list();
// sys.refresh_disks();
pub fn get_current_dir_available_space(sys: &System) -> Option<u64> {
pub fn get_current_dir_available_space() -> Option<u64> {
let cwd_result = current_dir();
if cwd_result.is_err() {
debug!(
Expand All @@ -76,7 +68,8 @@ pub fn get_current_dir_available_space(sys: &System) -> Option<u64> {
let mut cwd_disk: Option<&Disk> = None;
// Lookup disk whose mount point is parent of cwd
// In case there are multiple candidates, pick up the "nearest" parent of cwd
for disk in sys.disks() {
let disks = Disks::new_with_refreshed_list();
for disk in &disks {
debug!("Disk {:?}", disk);
if cwd.starts_with(disk.mount_point())
&& (cwd_disk.is_none()
Expand Down
11 changes: 2 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ use reqwest::Client;

use indicatif::{DecimalBytes, MultiProgress};

use sysinfo::{System, SystemExt};

mod disk;
mod download;
mod interact;
Expand Down Expand Up @@ -124,10 +122,7 @@ async fn main() -> Result<(), Error> {
}

// Check available disk size
let mut sys: System = System::new();
sys.refresh_disks_list();
sys.refresh_disks();
let disk_space = disk::get_current_dir_available_space(&sys);
let disk_space = disk::get_current_dir_available_space();
if let Some(space) = disk_space {
if space < total_update_size {
interact::warn(&format!("Not enough space on disk to proceed with download. Available disk space in current directory: {}",
Expand Down Expand Up @@ -156,10 +151,8 @@ async fn main() -> Result<(), Error> {
}

// Listing available disks for extraction
sys.refresh_disks_list();
sys.refresh_disks();
// TODO check destination available space.
disk::print_disks(&sys);
disk::print_disks();
let location = interact::prompt("Location where to extract the update files (IMPORTANT: Should be the root of an EMPTY USB device formatted as FAT32)")?;
if !location.is_empty() {
extract_location = Some(location);
Expand Down

0 comments on commit f548c3e

Please sign in to comment.