Debian Codegraph @

src/collection/disks/unix/linux/unmounted.rs

raw symbols SCIP
1 //! Return block devices that aren't currently mounted on Linux.
2
3 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{
4 collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#,
5 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#,
6 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#, BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#},
7 };
8
9 use craterust-analyzer cargo bottom 0.14.9 crate/::collectionrust-analyzer cargo bottom 0.14.9 collection/::disksrust-analyzer cargo bottom 0.14.9 collection/disks/::DiskHarvestrust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest#;
10
11 /// `/proc/partitions` reports sizes in 1024-byte blocks.
12 const PARTITION_BLOCK_SIZErust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/PARTITION_BLOCK_SIZE.: u64 = 1024;
13
14 /// Returns [`DiskHarvest`] entries for block devices that aren't in `mounted`.
15 ///
16 /// These come from `/proc/partitions`, which lists every block device (so even
17 /// devices with no I/O activity are covered) along with its size in terms of blocks.
18 /// Note this also filters out some devices, like `loop*`, `ram*`, `zram*`, etc., as
19 /// these are not "disks".
20 pub(craterust-analyzer cargo bottom 0.14.9 crate/) fn unmounted_disksrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/unmounted_disks().(mounted: &HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<DiskHarvestrust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest#> {
21 const PROC_PARTITIONSrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/PROC_PARTITIONS.: &str = "/proc/partitions";
22
23 let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(file) = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(PROC_PARTITIONSrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/PROC_PARTITIONS.) else {
24 return Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
25 };
26
27 parse_unmounted_disksrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/parse_unmounted_disks().(BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/impl#[`BufReader<R>`]new().(file), mounted)
28 }
29
30 /// Parses `/proc/partitions` into [`DiskHarvest`] entries for block devices not present in `mounted`.
31 fn parse_unmounted_disksrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/parse_unmounted_disks().<R: BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#>(mut reader: R, mounted: &HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<DiskHarvestrust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest#> {
32 let mut disks = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
33 let mut line = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
34
35 while let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(bytes) = reader.read_linerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#read_line().(&mut line) {
36 if bytes rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 {
37 break;
38 }
39
40 // Format: `major minor #blocks name`. The header line and the blank line
41 // after it simply fail to parse here and get skipped.
42 let mut parts = line.split_whitespacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_whitespace().();
43 let blocks = parts.nthrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#nth().(2).and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|b| b.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<u64>().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().());
44 let name = parts.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`SplitWhitespace<'a>`][Iterator]next().();
45
46 if let (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(blocks), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(name)) = (blocks, name)
47 && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().(mounted.containsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S, A>`]contains().(name)
48 || name.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("loop")
49 || name.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("ram")
50 || name.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("zram"))
51 {
52 disks.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(DiskHarvestrust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest# {
53 namerust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest#name.: formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("/dev/{name}"),
54 mount_pointrust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest#mount_point.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().(),
55 free_spacerust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest#free_space.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
56 used_spacerust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest#used_space.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
57 total_spacerust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest#total_space.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(blocks rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().PARTITION_BLOCK_SIZErust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/PARTITION_BLOCK_SIZE.),
58 });
59 }
60
61 line.clearrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]clear().();
62 }
63
64 disks
65 }
66
67 #[cfg(test)]
68 mod testsrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/tests/ {
69 use superrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/::*;
70
71 // Note the header + blank line at the top, exactly like the real file.
72 const SAMPLErust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/tests/SAMPLE.: &str = "\
73 major minor #blocks name
74
75 8 0 500 sda
76 8 1 1000 sda1
77 8 2 4000000 sda2
78 259 0 90000000 nvme0n1
79 259 1 900000 nvme0n1p1
80 7 0 100000 loop0
81 1 0 6000 ram0
82 252 0 1000000 zram0
83 8 16 0 sdb
84 ";
85
86 fn namesrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/tests/names().(disks: &[DiskHarvestrust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest#]) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<&str> {
87 disks.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|d| d.namerust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest#name..as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()
88 }
89
90 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
91 fn skip_non_disk_entriesrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/tests/skip_non_disk_entries().() {
92 let disks = parse_unmounted_disksrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/parse_unmounted_disks().(SAMPLErust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/tests/SAMPLE..as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(), &HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T>`]new().());
93 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
94 namesrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/tests/names().(&disks),
95 vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![
96 "/dev/sda",
97 "/dev/sda1",
98 "/dev/sda2",
99 "/dev/nvme0n1",
100 "/dev/nvme0n1p1",
101 "/dev/sdb",
102 ]
103 );
104 }
105
106 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
107 fn excludes_mounted_devicesrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/tests/excludes_mounted_devices().() {
108 let mounted = HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T>`][`From<[T; N]>`]from().(["sda2".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), "nvme0n1p1".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()]);
109 let disks = parse_unmounted_disksrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/parse_unmounted_disks().(SAMPLErust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/tests/SAMPLE..as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(), &mounted);
110
111 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().namesrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/tests/names().(&disks).containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]contains().(&"/dev/sda2"));
112 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().namesrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/tests/names().(&disks).containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]contains().(&"/dev/nvme0n1p1"));
113
114 // Whole disk + other partitions still appear (kept on purpose).
115 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(namesrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/tests/names().(&disks).containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]contains().(&"/dev/sda"));
116 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(namesrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/tests/names().(&disks).containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]contains().(&"/dev/nvme0n1"));
117 }
118
119 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
120 fn check_disk_harvest_entryrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/tests/check_disk_harvest_entry().() {
121 let disks = parse_unmounted_disksrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/parse_unmounted_disks().(SAMPLErust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/tests/SAMPLE..as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(), &HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T>`]new().());
122 let sda = disks.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]find().(|d| d.namerust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest#name. rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq().==rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq()."/dev/sda").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
123
124 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(sda.total_spacerust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest#total_space., Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(500 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul(). PARTITION_BLOCK_SIZErust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/PARTITION_BLOCK_SIZE.));
125 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(sda.mount_pointrust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest#mount_point., "");
126 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(sda.free_spacerust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest#free_space., Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
127 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(sda.used_spacerust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest#used_space., Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
128 }
129
130 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
131 fn zero_block_entryrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/tests/zero_block_entry().() {
132 let disks = parse_unmounted_disksrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/parse_unmounted_disks().(SAMPLErust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/unmounted/tests/SAMPLE..as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(), &HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T>`]new().());
133 let sdb = disks.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]find().(|d| d.namerust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest#name. rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq().==rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq()."/dev/sdb").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
134 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(sdb.total_spacerust-analyzer cargo bottom 0.14.9 collection/disks/DiskHarvest#total_space., Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(0));
135 }
136 }