Skip to content

Commit dbdb1e9

Browse files
committed
fix crash when rfkill dir is not present
1 parent 1a5f86d commit dbdb1e9

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

src/rfkill.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,38 @@ use std::fs;
33
use crate::app::AppResult;
44

55
pub fn check() -> AppResult<()> {
6-
let entries = fs::read_dir("/sys/class/rfkill/")?;
6+
if let Ok(entries) = fs::read_dir("/sys/class/rfkill/") {
7+
for entry in entries {
8+
let entry = entry?;
9+
let entry_path = entry.path();
710

8-
for entry in entries {
9-
let entry = entry?;
10-
let entry_path = entry.path();
11+
if let Some(_file_name) = entry_path.file_name() {
12+
let name = fs::read_to_string(entry_path.join("type"))?;
1113

12-
if let Some(_file_name) = entry_path.file_name() {
13-
let name = fs::read_to_string(entry_path.join("type"))?;
14+
if name.trim() == "bluetooth" {
15+
let state_path = entry_path.join("state");
16+
let state = fs::read_to_string(state_path)?.trim().parse::<u8>()?;
1417

15-
if name.trim() == "bluetooth" {
16-
let state_path = entry_path.join("state");
17-
let state = fs::read_to_string(state_path)?.trim().parse::<u8>()?;
18-
19-
// https://www.kernel.org/doc/Documentation/ABI/stable/sysfs-class-rfkill
20-
match state {
21-
0 => {
22-
eprintln!(
23-
r#"
18+
// https://www.kernel.org/doc/Documentation/ABI/stable/sysfs-class-rfkill
19+
match state {
20+
0 => {
21+
eprintln!(
22+
r#"
2423
The bluetooth device is soft blocked
2524
Run the following command to unblock it
2625
$ sudo rfkill unblock bluetooth
2726
"#
28-
);
29-
std::process::exit(1);
30-
}
31-
2 => {
32-
eprintln!("The bluetooth device is hard blocked");
33-
std::process::exit(1);
27+
);
28+
std::process::exit(1);
29+
}
30+
2 => {
31+
eprintln!("The bluetooth device is hard blocked");
32+
std::process::exit(1);
33+
}
34+
_ => {}
3435
}
35-
_ => {}
36+
break;
3637
}
37-
break;
3838
}
3939
}
4040
}

0 commit comments

Comments
 (0)