procfs_core/
devices.rs

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use std::io::BufRead;

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

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

/// Device entries under `/proc/devices`
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct Devices {
    /// Character devices
    pub char_devices: Vec<CharDeviceEntry>,
    /// Block devices, which can be empty if the kernel doesn't support block devices (without `CONFIG_BLOCK`)
    pub block_devices: Vec<BlockDeviceEntry>,
}

/// A charcter device entry under `/proc/devices`
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct CharDeviceEntry {
    /// Device major number
    pub major: u32,
    /// Device name
    pub name: String,
}

/// A block device entry under `/proc/devices`
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct BlockDeviceEntry {
    /// Device major number
    pub major: i32,
    /// Device name
    pub name: String,
}

impl super::FromBufRead for Devices {
    fn from_buf_read<R: BufRead>(r: R) -> ProcResult<Self> {
        enum State {
            Char,
            Block,
        }
        let mut state = State::Char; // Always start with char devices
        let mut devices = Devices {
            char_devices: vec![],
            block_devices: vec![],
        };

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

            if line.is_empty() {
                continue;
            } else if line.starts_with("Character devices:") {
                state = State::Char;
                continue;
            } else if line.starts_with("Block devices:") {
                state = State::Block;
                continue;
            }

            let mut s = line.split_whitespace();

            match state {
                State::Char => {
                    let major = expect!(u32::from_str(expect!(s.next())));
                    let name = expect!(s.next()).to_string();

                    let char_device_entry = CharDeviceEntry { major, name };

                    devices.char_devices.push(char_device_entry);
                }
                State::Block => {
                    let major = expect!(i32::from_str(expect!(s.next())));
                    let name = expect!(s.next()).to_string();

                    let block_device_entry = BlockDeviceEntry { major, name };

                    devices.block_devices.push(block_device_entry);
                }
            }
        }

        Ok(devices)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_devices() {
        use crate::FromBufRead;
        use std::io::Cursor;

        let s = "Character devices:
  1 mem
  4 /dev/vc/0
  4 tty
  4 ttyS
  5 /dev/tty
  5 /dev/console
  5 /dev/ptmx
  7 vcs
 10 misc
 13 input
 29 fb
 90 mtd
136 pts
180 usb
188 ttyUSB
189 usb_device

Block devices:
  7 loop
  8 sd
 65 sd
 71 sd
128 sd
135 sd
254 device-mapper
259 blkext
";

        let cursor = Cursor::new(s);
        let devices = Devices::from_buf_read(cursor).unwrap();
        let (chrs, blks) = (devices.char_devices, devices.block_devices);

        assert_eq!(chrs.len(), 16);

        assert_eq!(chrs[1].major, 4);
        assert_eq!(chrs[1].name, "/dev/vc/0");

        assert_eq!(chrs[8].major, 10);
        assert_eq!(chrs[8].name, "misc");

        assert_eq!(chrs[15].major, 189);
        assert_eq!(chrs[15].name, "usb_device");

        assert_eq!(blks.len(), 8);

        assert_eq!(blks[0].major, 7);
        assert_eq!(blks[0].name, "loop");

        assert_eq!(blks[7].major, 259);
        assert_eq!(blks[7].name, "blkext");
    }

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

        let s = "Character devices:
  1 mem
  4 /dev/vc/0
  4 tty
  4 ttyS
  5 /dev/tty
  5 /dev/console
  5 /dev/ptmx
  7 vcs
 10 misc
 13 input
 29 fb
 90 mtd
136 pts
180 usb
188 ttyUSB
189 usb_device
";

        let cursor = Cursor::new(s);
        let devices = Devices::from_buf_read(cursor).unwrap();
        let (chrs, blks) = (devices.char_devices, devices.block_devices);

        assert_eq!(chrs.len(), 16);

        assert_eq!(chrs[1].major, 4);
        assert_eq!(chrs[1].name, "/dev/vc/0");

        assert_eq!(chrs[8].major, 10);
        assert_eq!(chrs[8].name, "misc");

        assert_eq!(chrs[15].major, 189);
        assert_eq!(chrs[15].name, "usb_device");

        assert_eq!(blks.len(), 0);
    }
}