Skip to content

Commit cac069c

Browse files
committed
tmp: save it for later
1 parent 2e74a70 commit cac069c

4 files changed

Lines changed: 130 additions & 1 deletion

File tree

Cargo.lock

Lines changed: 78 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ serde = { version = "^1.0.219", optional = true }
6767
serde_json = { version = "^1.0.140", optional = true }
6868
sysinfo = "^0.35.2"
6969

70+
[target.'cfg(unix)'.dependencies]
71+
dashmap = "^6.1.0"
72+
7073
[dev-dependencies]
7174
build-fs-tree = "^0.7.1"
7275
command-extra = "^1.0.0"

src/hook.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use std::{fs::Metadata, path::Path};
2+
3+
#[cfg(unix)]
4+
use dashmap::DashMap;
5+
#[cfg(unix)]
6+
use std::{os::unix::fs::MetadataExt, path::PathBuf};
7+
8+
/// Hook to run with a [`Path`] and its corresponding [`Metadata`].
9+
pub trait Hook {
10+
fn run_hook(&self, path: &Path, metadata: &Metadata);
11+
}
12+
13+
/// A [hook](Hook) that does nothing.
14+
#[derive(Debug, Clone, Copy)]
15+
pub struct DoNothing;
16+
impl Hook for DoNothing {
17+
fn run_hook(&self, _: &Path, _: &Metadata) {}
18+
}
19+
20+
/// A [hook](Hook) that record files with more than 1 links.
21+
#[cfg(unix)] // Windows does have `MetadataExt::number_of_links` but it requires Nightly
22+
#[derive(Debug, Clone, Copy)]
23+
pub struct RecordHardLink<'a> {
24+
/// Map an inode number to its detected paths.
25+
storage: &'a DashMap<u64, Vec<PathBuf>>, // TODO: benchmark against Mutex<HashMap<u64, Vec<PathBuf>>>
26+
}
27+
28+
#[cfg(unix)]
29+
impl<'a> RecordHardLink<'a> {
30+
/// Create a [hook](Hook) to record files with more than 1 links.
31+
pub fn new(storage: &'a DashMap<u64, Vec<PathBuf>>) -> Self {
32+
RecordHardLink { storage }
33+
}
34+
}
35+
36+
#[cfg(unix)]
37+
impl<'a> Hook for RecordHardLink<'a> {
38+
fn run_hook(&self, path: &Path, metadata: &Metadata) {
39+
if metadata.is_dir() || metadata.nlink() <= 1 {
40+
return;
41+
}
42+
43+
self.storage
44+
.entry(metadata.ino())
45+
.or_default()
46+
.push(path.to_path_buf());
47+
}
48+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub mod bytes_format;
3333
pub mod data_tree;
3434
pub mod fs_tree_builder;
3535
pub mod get_size;
36+
pub mod hook;
3637
pub mod json_data;
3738
pub mod os_string_display;
3839
pub mod reporter;

0 commit comments

Comments
 (0)