Skip to content

Commit

Permalink
Fix change requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Integral-Tech committed Nov 25, 2024
1 parent ca5b7a7 commit 0ceed30
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 55 deletions.
44 changes: 27 additions & 17 deletions src/app/sub.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
mod mount_point;

use crate::{
args::Fraction,
data_tree::{DataTree, DataTreeReflection},
fs_tree_builder::FsTreeBuilder,
get_size::GetSize,
json_data::{BinaryVersion, JsonData, SchemaVersion, UnitAndTree},
mount_point::find_mountpoint,
os_string_display::OsStringDisplay,
reporter::ParallelReporter,
runtime_error::RuntimeError,
size,
status_board::GLOBAL_STATUS_BOARD,
visualizer::{BarAlignment, ColumnWidthDistribution, Direction, Visualizer},
};
use mount_point::find_mount_point;
use serde::Serialize;
use std::{fs, io::stdout, iter::once, num::NonZeroUsize, path::PathBuf};
use sysinfo::{DiskKind, Disks};
use sysinfo::{Disk, DiskKind, Disks};

/// The sub program of the main application.
pub struct Sub<Size, SizeGetter, Report>
Expand Down Expand Up @@ -74,24 +76,11 @@ where
// If one of the files is on HDD, set thread number to 1
let disks = Disks::new_with_refreshed_list();

if files
.iter()
.filter_map(|file| fs::canonicalize(file).ok())
.any(|path| {
let mount_points: Vec<_> = disks.iter().map(|disk| disk.mount_point()).collect();
if let Some(mount_point) = find_mountpoint(&path, &mount_points) {
disks.iter().any(|disk| {
matches!(disk.kind(), DiskKind::HDD) && disk.mount_point() == mount_point
})
} else {
false
}
})
{
if detect_hdd_in_files(&disks, &files, |disk| disk.kind()) {
rayon::ThreadPoolBuilder::new()
.num_threads(1)
.build_global()
.unwrap();
.unwrap_or_else(|_| eprintln!("warning: This program is suboptimal with HDD"));
}

let mut iter = files
Expand Down Expand Up @@ -174,3 +163,24 @@ where
Ok(())
}
}

fn detect_hdd_in_files(
disks: &[Disk],
files: &[PathBuf],
get_disk_kind: impl Fn(&Disk) -> DiskKind,
) -> bool {
files
.iter()
.filter_map(|file| fs::canonicalize(file).ok())
.any(|path| {
if let Some(mount_point) =
find_mount_point(&path, disks.iter().map(|disk| disk.mount_point()))
{
disks.iter().any(|disk| {
get_disk_kind(disk) == DiskKind::HDD && disk.mount_point() == mount_point
})
} else {
false
}
})
}
41 changes: 41 additions & 0 deletions src/app/sub/mount_point.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::{ffi::OsStr, path::Path};

pub(super) fn find_mount_point<'a>(
path: &Path,
mount_points: impl IntoIterator<Item = &'a Path>,
) -> Option<&'a Path> {
mount_points
.into_iter()
.filter(|mnt| path.starts_with(&*mnt.to_string_lossy()))
.max_by_key(|mnt| AsRef::<OsStr>::as_ref(mnt).len()) // Mount points can be nested in each other
}

#[cfg(test)]
mod tests {
use super::find_mount_point;
use std::path::Path;

#[test]
fn test_mountpoint() {
let mount_points = [
Path::new("/"),
Path::new("/home"),
Path::new("/mnt/data"),
Path::new("/mnt/data/repo"),
Path::new("/mnt/repo"),
];

for (path, mount_point) in &[
("/etc/fstab", "/"),
("/home/user", "/home"),
("/mnt/data/repo/test", "/mnt/data/repo"),
("/mnt/data/test/test", "/mnt/data/"),
("/mnt/repo/test/test", "/mnt/repo/"),
] {
assert_eq!(
find_mount_point(Path::new(path), mount_points).unwrap(),
Path::new(mount_point)
);
}
}
}
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ pub mod data_tree;
pub mod fs_tree_builder;
pub mod get_size;
pub mod json_data;
pub mod mount_point;
pub mod os_string_display;
pub mod reporter;
pub mod size;
Expand Down
11 changes: 0 additions & 11 deletions src/mount_point.rs

This file was deleted.

26 changes: 0 additions & 26 deletions tests/find_mountpoint.rs

This file was deleted.

0 comments on commit 0ceed30

Please sign in to comment.