Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for /proc/partitions parsing #286

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions procfs-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ pub use locks::*;
mod mounts;
pub use mounts::*;

mod partitions;
pub use partitions::*;

mod meminfo;
pub use meminfo::*;

Expand Down
87 changes: 87 additions & 0 deletions procfs-core/src/partitions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use std::io::BufRead;

use super::ProcResult;
use std::str::FromStr;

#[cfg(feature = "serde1")]
use serde::{Deserialize, Serialize};

/// A partition entry under `/proc/partitions`
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
#[allow(non_snake_case)]
pub struct PartitionEntry {
/// Device major number
pub major: u16,
/// Device minor number
pub minor: u16,
/// Number of 1024 byte blocks
pub blocks: u64,
/// Device name
pub name: String
}

impl super::FromBufRead for Vec<PartitionEntry> {
fn from_buf_read<R: BufRead>(r: R) -> ProcResult<Self> {
let mut vec = Vec::new();

for line in r.lines().skip(2) {
let line = expect!(line);

let mut s = line.split_whitespace();

let major = expect!(u16::from_str(expect!(s.next())));
let minor = expect!(u16::from_str(expect!(s.next())));
let blocks = expect!(u64::from_str(expect!(s.next())));
let name = expect!(s.next()).to_string();

let partition_entry = PartitionEntry {
major,
minor,
blocks,
name
};

vec.push(partition_entry);
}

Ok(vec)
}
}


#[test]
fn test_partitions() {
use crate::FromBufRead;
use std::io::Cursor;

let s = "major minor #blocks name

259 0 1000204632 nvme0n1
259 1 1048576 nvme0n1p1
259 2 1048576 nvme0n1p2
259 3 104857600 nvme0n1p3
259 4 893248512 nvme0n1p4
253 0 104841216 dm-0
252 0 8388608 zram0
253 1 893232128 dm-1
8 0 3953664 sda
8 1 2097152 sda1
8 2 1855488 sda2
253 2 1853440 dm-2
";

let cursor = Cursor::new(s);
let partitions = Vec::<PartitionEntry>::from_buf_read(cursor).unwrap();
assert_eq!(partitions.len(), 12);

assert_eq!(partitions[3].major, 259);
assert_eq!(partitions[3].minor, 3);
assert_eq!(partitions[3].blocks, 104857600);
assert_eq!(partitions[3].name, "nvme0n1p3");

assert_eq!(partitions[11].major, 253);
assert_eq!(partitions[11].minor, 2);
assert_eq!(partitions[11].blocks, 1853440);
assert_eq!(partitions[11].name, "dm-2");
}
7 changes: 7 additions & 0 deletions procfs/examples/partitions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// List partitions listed in /proc/partitions

fn main() {
for part_entry in procfs::partitions().unwrap() {
println!("{part_entry:?}");
}
}
9 changes: 9 additions & 0 deletions procfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,15 @@ pub fn mounts() -> ProcResult<Vec<MountEntry>> {
Vec::<MountEntry>::current()
}

impl Current for Vec<PartitionEntry> {
const PATH: &'static str = "/proc/partitions";
}

/// Get a list of partitions from `/proc/partitions`
pub fn partitions() -> ProcResult<Vec<PartitionEntry>> {
Vec::<PartitionEntry>::current()
}

impl Current for Locks {
const PATH: &'static str = "/proc/locks";
}
Expand Down
2 changes: 1 addition & 1 deletion support.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ This is an approximate list of all the files under the `/proc` mount, and an ind
* [x] `/proc/net/udp`
* [x] `/proc/net/unix`
* [ ] `/proc/net/netfilter/nfnetlink_queue`
* [ ] `/proc/partitions`
* [x] `/proc/partitions`
* [ ] `/proc/pci`
* [x] `/proc/pressure`
* [x] `/proc/pressure/cpu`
Expand Down