Skip to content

Commit a741256

Browse files
committed
system: remove all unwraps from system.rs
Signed-off-by: Leonard Göhrs <[email protected]>
1 parent fed9e2c commit a741256

File tree

2 files changed

+73
-40
lines changed

2 files changed

+73
-40
lines changed

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ async fn init(screenshooter: ScreenShooter) -> Result<(Ui, WatchedTasksBuilder)>
114114

115115
// Expose information about the system provided by the kernel via the
116116
// broker framework.
117-
let system = System::new(&mut bb, hardware_generation);
117+
let system = System::new(&mut bb, hardware_generation)?;
118118

119119
// Make sure the ADC and power switching threads of the tacd are not
120120
// stalled for too long by providing watchdog events to systemd

src/system.rs

Lines changed: 72 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
// with this program; if not, write to the Free Software Foundation, Inc.,
1616
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717

18-
use anyhow::{bail, Result};
18+
use std::ffi::OsStr;
19+
20+
use anyhow::{anyhow, bail, Result};
1921
use async_std::sync::Arc;
2022
use nix::sys::utsname::uname;
2123
use serde::{Deserialize, Serialize};
@@ -24,6 +26,8 @@ use crate::broker::{BrokerBuilder, Topic};
2426

2527
#[cfg(feature = "demo_mode")]
2628
mod read_dt_props {
29+
use anyhow::{anyhow, Result};
30+
2731
const DEMO_DATA_STR: &[(&str, &str)] = &[
2832
(
2933
"compatible",
@@ -53,14 +57,22 @@ mod read_dt_props {
5357
),
5458
];
5559

56-
pub fn read_dt_property(path: &str) -> String {
57-
let (_, content) = DEMO_DATA_STR.iter().find(|(p, _)| *p == path).unwrap();
60+
pub fn read_dt_property(path: &str) -> Result<String> {
61+
let (_, content) = DEMO_DATA_STR
62+
.iter()
63+
.find(|(p, _)| *p == path)
64+
.ok_or_else(|| anyhow!("could not find devicetree property {path}"))?;
5865

59-
content.to_string()
66+
Ok(content.to_string())
6067
}
6168

62-
pub fn read_dt_property_u32(path: &str) -> u32 {
63-
DEMO_DATA_NUM.iter().find(|(p, _)| *p == path).unwrap().1
69+
pub fn read_dt_property_u32(path: &str) -> Result<u32> {
70+
let (_, content) = DEMO_DATA_NUM
71+
.iter()
72+
.find(|(p, _)| *p == path)
73+
.ok_or_else(|| anyhow!("could not find devicetree property {path}"))?;
74+
75+
Ok(*content)
6476
}
6577
}
6678

@@ -69,17 +81,26 @@ mod read_dt_props {
6981
use std::fs::read;
7082
use std::str::from_utf8;
7183

84+
use anyhow::{anyhow, Result};
85+
7286
const DT_BASE: &str = "/sys/firmware/devicetree/base/";
7387

74-
pub fn read_dt_property(path: &str) -> String {
75-
let bytes = read([DT_BASE, path].join("/")).unwrap();
76-
from_utf8(bytes.strip_suffix(&[0]).unwrap())
77-
.unwrap()
78-
.to_string()
88+
pub fn read_dt_property(path: &str) -> Result<String> {
89+
let path = [DT_BASE, path].join("/");
90+
let bytes = read(&path)?;
91+
let stripped_bytes = bytes
92+
.strip_suffix(&[0])
93+
.ok_or_else(|| anyhow!("Devicetree property {path} did not contain a value"))?;
94+
let stripped = from_utf8(stripped_bytes)?;
95+
96+
Ok(stripped.to_string())
7997
}
8098

81-
pub fn read_dt_property_u32(path: &str) -> u32 {
82-
read_dt_property(path).parse().unwrap()
99+
pub fn read_dt_property_u32(path: &str) -> Result<u32> {
100+
let raw = read_dt_property(path)?;
101+
let value = raw.parse()?;
102+
103+
Ok(value)
83104
}
84105
}
85106

@@ -95,16 +116,25 @@ pub struct Uname {
95116
}
96117

97118
impl Uname {
98-
fn get() -> Self {
99-
let uts = uname().unwrap();
100-
101-
Self {
102-
sysname: uts.sysname().to_str().unwrap().to_string(),
103-
nodename: uts.nodename().to_str().unwrap().to_string(),
104-
release: uts.release().to_str().unwrap().to_string(),
105-
version: uts.version().to_str().unwrap().to_string(),
106-
machine: uts.machine().to_str().unwrap().to_string(),
119+
fn get() -> Result<Self> {
120+
let uts = uname()?;
121+
122+
fn to_string(val: &OsStr, name: &str) -> Result<String> {
123+
let res = val
124+
.to_str()
125+
.ok_or_else(|| anyhow!("uname entry {name} can not be converted to utf-8"))?
126+
.to_string();
127+
128+
Ok(res)
107129
}
130+
131+
Ok(Self {
132+
sysname: to_string(uts.sysname(), "sysname")?,
133+
nodename: to_string(uts.nodename(), "nodename")?,
134+
release: to_string(uts.release(), "release")?,
135+
version: to_string(uts.version(), "version")?,
136+
machine: to_string(uts.machine(), "machine")?,
137+
})
108138
}
109139
}
110140

@@ -118,31 +148,31 @@ pub struct Barebox {
118148
}
119149

120150
impl Barebox {
121-
fn get() -> Self {
151+
fn get() -> Result<Self> {
122152
// Get info from devicetree chosen
123-
Self {
124-
version: read_dt_property("chosen/barebox-version"),
153+
Ok(Self {
154+
version: read_dt_property("chosen/barebox-version")?,
125155
baseboard_release: {
126156
let template =
127-
read_dt_property("chosen/baseboard-factory-data/pcba-hardware-release");
128-
let changeset = read_dt_property_u32("chosen/baseboard-factory-data/modification");
129-
157+
read_dt_property("chosen/baseboard-factory-data/pcba-hardware-release")?;
158+
let changeset = read_dt_property_u32("chosen/baseboard-factory-data/modification")?;
130159
template.replace("-C??", &format!("-C{changeset:02}"))
131160
},
132161
powerboard_release: {
133162
let template =
134-
read_dt_property("chosen/powerboard-factory-data/pcba-hardware-release");
135-
let changeset = read_dt_property_u32("chosen/powerboard-factory-data/modification");
163+
read_dt_property("chosen/powerboard-factory-data/pcba-hardware-release")?;
164+
let changeset =
165+
read_dt_property_u32("chosen/powerboard-factory-data/modification")?;
136166

137167
template.replace("-C??", &format!("-C{changeset:02}"))
138168
},
139169
baseboard_timestamp: {
140-
read_dt_property_u32("chosen/baseboard-factory-data/factory-timestamp")
170+
read_dt_property_u32("chosen/baseboard-factory-data/factory-timestamp")?
141171
},
142172
powerboard_timestamp: {
143-
read_dt_property_u32("chosen/powerboard-factory-data/factory-timestamp")
173+
read_dt_property_u32("chosen/powerboard-factory-data/factory-timestamp")?
144174
},
145-
}
175+
})
146176
}
147177
}
148178

@@ -155,7 +185,7 @@ pub enum HardwareGeneration {
155185

156186
impl HardwareGeneration {
157187
pub fn get() -> Result<Self> {
158-
let compatible = read_dt_property("compatible");
188+
let compatible = read_dt_property("compatible")?;
159189

160190
// The compatible property consists of strings separated by NUL bytes.
161191
// We are interested in the first of these strings.
@@ -182,17 +212,20 @@ pub struct System {
182212
}
183213

184214
impl System {
185-
pub fn new(bb: &mut BrokerBuilder, hardware_generation: HardwareGeneration) -> Self {
215+
pub fn new(bb: &mut BrokerBuilder, hardware_generation: HardwareGeneration) -> Result<Self> {
186216
let version = env!("VERSION_STRING").to_string();
187217

188-
Self {
189-
uname: bb.topic_ro("/v1/tac/info/uname", Some(Arc::new(Uname::get()))),
190-
barebox: bb.topic_ro("/v1/tac/info/bootloader", Some(Arc::new(Barebox::get()))),
218+
let uname = Uname::get()?;
219+
let barebox = Barebox::get()?;
220+
221+
Ok(Self {
222+
uname: bb.topic_ro("/v1/tac/info/uname", Some(Arc::new(uname))),
223+
barebox: bb.topic_ro("/v1/tac/info/bootloader", Some(Arc::new(barebox))),
191224
tacd_version: bb.topic_ro("/v1/tac/info/tacd/version", Some(version)),
192225
hardware_generation: bb.topic_ro(
193226
"/v1/tac/info/hardware_generation",
194227
Some(hardware_generation),
195228
),
196-
}
229+
})
197230
}
198231
}

0 commit comments

Comments
 (0)