Skip to content

Commit d3656d5

Browse files
committed
feat(api): make RecordHardlinks fallible
1 parent 603f995 commit d3656d5

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

src/fs_tree_builder.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,11 @@ where
9898
let is_dir = stats.is_dir();
9999
let size = size_getter.get_size(&stats);
100100
reporter.report(Event::ReceiveData(size));
101-
hardlinks_recorder.record_hardlinks(RecordHardlinksArgument::new(
102-
path, &stats, size, reporter,
103-
));
101+
hardlinks_recorder
102+
.record_hardlinks(RecordHardlinksArgument::new(
103+
path, &stats, size, reporter,
104+
))
105+
.ok(); // ignore the error for now
104106
(is_dir, size)
105107
}
106108
};

src/hardlink/aware.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{
2-
DeduplicateSharedSize, HardlinkList, LinkPathList, RecordHardlinks, RecordHardlinksArgument,
2+
hardlink_list, DeduplicateSharedSize, HardlinkList, LinkPathList, RecordHardlinks,
3+
RecordHardlinksArgument,
34
};
45
use crate::{
56
data_tree::DataTree,
@@ -8,7 +9,7 @@ use crate::{
89
reporter::{event::HardlinkDetection, Event, Reporter},
910
size,
1011
};
11-
use derive_more::{AsMut, AsRef, From, Into};
12+
use derive_more::{AsMut, AsRef, Display, Error, From, Into};
1213
use pipe_trait::Pipe;
1314
use smart_default::SmartDefault;
1415
use std::{convert::Infallible, fmt::Debug, os::unix::fs::MetadataExt, path::Path};
@@ -37,12 +38,26 @@ impl<Size> Aware<Size> {
3738
}
3839
}
3940

41+
/// Error that occurs when [`Aware::record_hardlinks`] fails.
42+
#[derive(Debug, Display, Error)]
43+
#[non_exhaustive]
44+
pub enum ReportHardlinksError<Size> {
45+
/// Fail to add an entry to the record.
46+
#[display("Fail to add an entry to record: {_0}")]
47+
AddToRecord(hardlink_list::AddError<Size>),
48+
}
49+
4050
impl<Size, Report> RecordHardlinks<Size, Report> for Aware<Size>
4151
where
4252
Size: size::Size + Eq + Debug,
4353
Report: Reporter<Size> + ?Sized,
4454
{
45-
fn record_hardlinks(&self, argument: RecordHardlinksArgument<Size, Report>) {
55+
type Error = ReportHardlinksError<Size>;
56+
57+
fn record_hardlinks(
58+
&self,
59+
argument: RecordHardlinksArgument<Size, Report>,
60+
) -> Result<(), Self::Error> {
4661
let RecordHardlinksArgument {
4762
path,
4863
stats,
@@ -51,12 +66,12 @@ where
5166
} = argument;
5267

5368
if stats.is_dir() {
54-
return;
69+
return Ok(());
5570
}
5671

5772
let links = stats.nlink();
5873
if links <= 1 {
59-
return;
74+
return Ok(());
6075
}
6176

6277
reporter.report(Event::DetectHardlink(HardlinkDetection {
@@ -67,7 +82,9 @@ where
6782
}));
6883

6984
let ino = InodeNumber::get(stats);
70-
self.record.add(ino, size, path).unwrap(); // TODO: propagate the error
85+
self.record
86+
.add(ino, size, path)
87+
.map_err(ReportHardlinksError::AddToRecord)
7188
}
7289
}
7390

src/hardlink/ignorant.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@ pub use Ignorant as HardlinkIgnorant;
1212

1313
/// Do nothing to detect nor record any hardlink.
1414
impl<Size, Reporter> RecordHardlinks<Size, Reporter> for Ignorant {
15+
/// Doing nothing cannot fail.
16+
type Error = Infallible;
17+
1518
/// Do nothing.
16-
fn record_hardlinks(&self, _: RecordHardlinksArgument<Size, Reporter>) {}
19+
fn record_hardlinks(
20+
&self,
21+
_: RecordHardlinksArgument<Size, Reporter>,
22+
) -> Result<(), Self::Error> {
23+
Ok(())
24+
}
1725
}
1826

1927
/// Do nothing to deduplicate the sizes of hardlinks.

src/hardlink/record.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ impl<'a, Size, Report: ?Sized> Argument<'a, Size, Report> {
2929

3030
/// Ability to detect and record hardlinks.
3131
pub trait RecordHardlinks<Size, Reporter: ?Sized> {
32+
/// Error when [`RecordHardlinks::record_hardlinks`] fails.
33+
type Error;
3234
/// Perform hardlinks detection and recording.
33-
fn record_hardlinks(&self, argument: Argument<Size, Reporter>);
35+
fn record_hardlinks(&self, argument: Argument<Size, Reporter>) -> Result<(), Self::Error>;
3436
}
3537

3638
/// Do detect and record hardlinks.

0 commit comments

Comments
 (0)