Skip to content

Commit 0ceed30

Browse files
committed
Fix change requests
1 parent ca5b7a7 commit 0ceed30

File tree

5 files changed

+68
-55
lines changed

5 files changed

+68
-55
lines changed

src/app/sub.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1+
mod mount_point;
2+
13
use crate::{
24
args::Fraction,
35
data_tree::{DataTree, DataTreeReflection},
46
fs_tree_builder::FsTreeBuilder,
57
get_size::GetSize,
68
json_data::{BinaryVersion, JsonData, SchemaVersion, UnitAndTree},
7-
mount_point::find_mountpoint,
89
os_string_display::OsStringDisplay,
910
reporter::ParallelReporter,
1011
runtime_error::RuntimeError,
1112
size,
1213
status_board::GLOBAL_STATUS_BOARD,
1314
visualizer::{BarAlignment, ColumnWidthDistribution, Direction, Visualizer},
1415
};
16+
use mount_point::find_mount_point;
1517
use serde::Serialize;
1618
use std::{fs, io::stdout, iter::once, num::NonZeroUsize, path::PathBuf};
17-
use sysinfo::{DiskKind, Disks};
19+
use sysinfo::{Disk, DiskKind, Disks};
1820

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

77-
if files
78-
.iter()
79-
.filter_map(|file| fs::canonicalize(file).ok())
80-
.any(|path| {
81-
let mount_points: Vec<_> = disks.iter().map(|disk| disk.mount_point()).collect();
82-
if let Some(mount_point) = find_mountpoint(&path, &mount_points) {
83-
disks.iter().any(|disk| {
84-
matches!(disk.kind(), DiskKind::HDD) && disk.mount_point() == mount_point
85-
})
86-
} else {
87-
false
88-
}
89-
})
90-
{
79+
if detect_hdd_in_files(&disks, &files, |disk| disk.kind()) {
9180
rayon::ThreadPoolBuilder::new()
9281
.num_threads(1)
9382
.build_global()
94-
.unwrap();
83+
.unwrap_or_else(|_| eprintln!("warning: This program is suboptimal with HDD"));
9584
}
9685

9786
let mut iter = files
@@ -174,3 +163,24 @@ where
174163
Ok(())
175164
}
176165
}
166+
167+
fn detect_hdd_in_files(
168+
disks: &[Disk],
169+
files: &[PathBuf],
170+
get_disk_kind: impl Fn(&Disk) -> DiskKind,
171+
) -> bool {
172+
files
173+
.iter()
174+
.filter_map(|file| fs::canonicalize(file).ok())
175+
.any(|path| {
176+
if let Some(mount_point) =
177+
find_mount_point(&path, disks.iter().map(|disk| disk.mount_point()))
178+
{
179+
disks.iter().any(|disk| {
180+
get_disk_kind(disk) == DiskKind::HDD && disk.mount_point() == mount_point
181+
})
182+
} else {
183+
false
184+
}
185+
})
186+
}

src/app/sub/mount_point.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use std::{ffi::OsStr, path::Path};
2+
3+
pub(super) fn find_mount_point<'a>(
4+
path: &Path,
5+
mount_points: impl IntoIterator<Item = &'a Path>,
6+
) -> Option<&'a Path> {
7+
mount_points
8+
.into_iter()
9+
.filter(|mnt| path.starts_with(&*mnt.to_string_lossy()))
10+
.max_by_key(|mnt| AsRef::<OsStr>::as_ref(mnt).len()) // Mount points can be nested in each other
11+
}
12+
13+
#[cfg(test)]
14+
mod tests {
15+
use super::find_mount_point;
16+
use std::path::Path;
17+
18+
#[test]
19+
fn test_mountpoint() {
20+
let mount_points = [
21+
Path::new("/"),
22+
Path::new("/home"),
23+
Path::new("/mnt/data"),
24+
Path::new("/mnt/data/repo"),
25+
Path::new("/mnt/repo"),
26+
];
27+
28+
for (path, mount_point) in &[
29+
("/etc/fstab", "/"),
30+
("/home/user", "/home"),
31+
("/mnt/data/repo/test", "/mnt/data/repo"),
32+
("/mnt/data/test/test", "/mnt/data/"),
33+
("/mnt/repo/test/test", "/mnt/repo/"),
34+
] {
35+
assert_eq!(
36+
find_mount_point(Path::new(path), mount_points).unwrap(),
37+
Path::new(mount_point)
38+
);
39+
}
40+
}
41+
}

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ pub mod data_tree;
3434
pub mod fs_tree_builder;
3535
pub mod get_size;
3636
pub mod json_data;
37-
pub mod mount_point;
3837
pub mod os_string_display;
3938
pub mod reporter;
4039
pub mod size;

src/mount_point.rs

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/find_mountpoint.rs

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)