|
| 1 | +mod mount_point; |
| 2 | + |
1 | 3 | use crate::{ |
2 | 4 | args::Fraction, |
3 | 5 | data_tree::{DataTree, DataTreeReflection}, |
4 | 6 | fs_tree_builder::FsTreeBuilder, |
5 | 7 | get_size::GetSize, |
6 | 8 | json_data::{BinaryVersion, JsonData, SchemaVersion, UnitAndTree}, |
7 | | - mount_point::find_mountpoint, |
8 | 9 | os_string_display::OsStringDisplay, |
9 | 10 | reporter::ParallelReporter, |
10 | 11 | runtime_error::RuntimeError, |
11 | 12 | size, |
12 | 13 | status_board::GLOBAL_STATUS_BOARD, |
13 | 14 | visualizer::{BarAlignment, ColumnWidthDistribution, Direction, Visualizer}, |
14 | 15 | }; |
| 16 | +use mount_point::find_mount_point; |
15 | 17 | use serde::Serialize; |
16 | 18 | use std::{fs, io::stdout, iter::once, num::NonZeroUsize, path::PathBuf}; |
17 | | -use sysinfo::{DiskKind, Disks}; |
| 19 | +use sysinfo::{Disk, DiskKind, Disks}; |
18 | 20 |
|
19 | 21 | /// The sub program of the main application. |
20 | 22 | pub struct Sub<Size, SizeGetter, Report> |
@@ -74,24 +76,11 @@ where |
74 | 76 | // If one of the files is on HDD, set thread number to 1 |
75 | 77 | let disks = Disks::new_with_refreshed_list(); |
76 | 78 |
|
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()) { |
91 | 80 | rayon::ThreadPoolBuilder::new() |
92 | 81 | .num_threads(1) |
93 | 82 | .build_global() |
94 | | - .unwrap(); |
| 83 | + .unwrap_or_else(|_| eprintln!("warning: This program is suboptimal with HDD")); |
95 | 84 | } |
96 | 85 |
|
97 | 86 | let mut iter = files |
@@ -174,3 +163,24 @@ where |
174 | 163 | Ok(()) |
175 | 164 | } |
176 | 165 | } |
| 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 | +} |
0 commit comments