-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathdevice_id.rs
More file actions
69 lines (61 loc) · 2.08 KB
/
device_id.rs
File metadata and controls
69 lines (61 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/// Unique identifier for a device or filesystem.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct DeviceId(Inner);
#[cfg(unix)]
type Inner = u64;
#[cfg(not(unix))]
type Inner = ();
/// Retrieve the [`DeviceId`] from filesystem metadata.
#[cfg(unix)]
pub(crate) fn get_device_id(stats: &std::fs::Metadata) -> DeviceId {
use std::os::unix::fs::MetadataExt;
DeviceId(stats.dev())
}
/// Retrieve the [`DeviceId`] from filesystem metadata.
///
/// On unsupported platforms, all entries share the same [`DeviceId`],
/// effectively disabling cross-device detection.
#[cfg(not(unix))]
pub(crate) fn get_device_id(_stats: &std::fs::Metadata) -> DeviceId {
DeviceId(())
}
#[cfg(test)]
#[cfg(unix)]
mod tests {
use super::get_device_id;
use std::fs::symlink_metadata;
#[test]
fn same_filesystem_returns_equal_ids() {
let root_stats = symlink_metadata("/").expect("stat /");
let root_stats2 = symlink_metadata("/").expect("stat / again");
assert_eq!(
get_device_id(&root_stats),
get_device_id(&root_stats2),
"same path should yield the same DeviceId",
);
}
/// `/proc` is a virtual filesystem mounted separately from `/` on Linux.
#[test]
#[cfg(target_os = "linux")]
fn different_filesystem_returns_different_ids() {
let root_stats = symlink_metadata("/").expect("stat /");
let proc_stats = symlink_metadata("/proc").expect("stat /proc");
assert_ne!(
get_device_id(&root_stats),
get_device_id(&proc_stats),
"/ and /proc should be on different devices",
);
}
/// `/dev` is a separate filesystem (`devfs`) from `/` on macOS.
#[test]
#[cfg(target_os = "macos")]
fn different_filesystem_returns_different_ids() {
let root_stats = symlink_metadata("/").expect("stat /");
let dev_stats = symlink_metadata("/dev").expect("stat /dev");
assert_ne!(
get_device_id(&root_stats),
get_device_id(&dev_stats),
"/ and /dev should be on different devices",
);
}
}