Skip to content

Commit 4bfa4ab

Browse files
committed
feat: report deduplication results
1 parent 213382d commit 4bfa4ab

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

src/app.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ impl App {
205205
impl<SizeGetter> HardlinkDeduplicationSystem<true> for SizeGetter
206206
where
207207
SizeGetter: GetSizeUtils,
208-
SizeGetter::Size: Send + Sync + 'static,
208+
SizeGetter::Size: From<u64> + Send + Sync + 'static,
209209
{
210210
type Hook = hook::RecordHardLink<'static, Self::Size>;
211211
fn create_hook(record: &'static hook::RecordHardLinkStorage<Self::Size>) -> Self::Hook {

src/app/sub.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ where
163163
};
164164

165165
print!("{visualizer}"); // visualizer already ends with "\n", println! isn't needed here.
166-
Hook::report_deduplication_results(deduplication_record);
166+
Hook::report_deduplication_results(deduplication_record, bytes_format);
167167
Ok(())
168168
}
169169
}
@@ -180,17 +180,20 @@ pub trait DeduplicateHardlinkSizes<Size: size::Size> {
180180
record: Self::HardlinkRecord,
181181
) -> Self::DeduplicationReport;
182182
/// Handle the report.
183-
fn report_deduplication_results(report: Self::DeduplicationReport);
183+
fn report_deduplication_results(
184+
report: Self::DeduplicationReport,
185+
bytes_format: Size::DisplayFormat,
186+
);
184187
}
185188

186189
#[cfg(unix)]
187190
impl<'a, Size> DeduplicateHardlinkSizes<Size> for hook::RecordHardLink<'a, Size>
188191
where
189192
DataTree<OsStringDisplay, Size>: Send,
190-
Size: size::Size + Sync,
193+
Size: size::Size + From<u64> + Sync,
191194
{
192195
type HardlinkRecord = &'a hook::RecordHardLinkStorage<Size>;
193-
type DeduplicationReport = (); // TODO
196+
type DeduplicationReport = &'a hook::RecordHardLinkStorage<Size>;
194197

195198
fn deduplicate_hardlink_sizes(
196199
data_tree: &mut DataTree<OsStringDisplay, Size>,
@@ -206,8 +209,32 @@ where
206209
.map(|(size, paths)| (*size, paths.iter().map(AsRef::as_ref).collect()))
207210
.collect();
208211
data_tree.par_deduplicate_hardlinks(&hardlink_info);
212+
record
213+
}
214+
215+
fn report_deduplication_results(
216+
report: Self::DeduplicationReport,
217+
bytes_format: Size::DisplayFormat,
218+
) {
219+
let (inodes, links, size): (usize, usize, Size) = report
220+
.iter()
221+
.filter_map(|ref_multi| {
222+
let (size, links) = &*ref_multi;
223+
let links = links.len();
224+
(links > 1).then_some(())?;
225+
Some((*size, links))
226+
})
227+
.fold(
228+
(0, 0, Size::from(0)),
229+
|(inodes, total_links, total_size), (size, links)| {
230+
(inodes + 1, total_links + links, total_size + size)
231+
},
232+
);
233+
if inodes > 0 {
234+
let size = size.display(bytes_format);
235+
println!("Detected {links} hardlinks for {inodes} unique files (total: {size})");
236+
}
209237
}
210-
fn report_deduplication_results((): Self::DeduplicationReport) {} // TODO
211238
}
212239

213240
impl<Size> DeduplicateHardlinkSizes<Size> for hook::DoNothing
@@ -222,5 +249,5 @@ where
222249
_: Self::HardlinkRecord,
223250
) -> Self::DeduplicationReport {
224251
}
225-
fn report_deduplication_results((): Self::DeduplicationReport) {}
252+
fn report_deduplication_results((): Self::DeduplicationReport, _: Size::DisplayFormat) {}
226253
}

0 commit comments

Comments
 (0)