Skip to content

Commit af02cc0

Browse files
committed
Improve list() error handling
1 parent 05f44e8 commit af02cc0

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed

src/bin/efivar.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,32 @@ fn create_parser() -> clap::Command {
109109
);
110110
}
111111

112-
fn list_variables(parser_args: clap::ArgMatches) -> ExitCode {
113-
let mut efi_variables: efivar::efivarfs::EfiVariables = Default::default();
112+
fn list_variables(_parser_args: clap::ArgMatches) -> ExitCode {
113+
let mut efivar_fs_variables: efivar::efivarfs::EfiVariables = Default::default();
114114

115-
for v in efi_variables.list() {
116-
println!("{}", v);
115+
match efivar_fs_variables.list() {
116+
Ok(variables) => {
117+
for v in variables {
118+
println!("{}", v);
119+
}
120+
return std::process::ExitCode::from(0);
121+
},
122+
Err(_) => {
123+
let mut efivar_variables: efivar::efivar::EfiVariables = Default::default();
124+
match efivar_variables.list() {
125+
Ok(variables) => {
126+
for v in variables {
127+
println!("{}", v);
128+
}
129+
return std::process::ExitCode::from(0);
130+
},
131+
Err(e) => {
132+
eprintln!("Failed to access EFI variables: {}", e);
133+
return std::process::ExitCode::from(1);
134+
}
135+
}
136+
}
117137
}
118-
return std::process::ExitCode::from(0);
119138
}
120139

121140
fn print_variable(parser_args: clap::ArgMatches, print_mode: efivar::types::PrintMode) -> ExitCode {
@@ -175,6 +194,6 @@ fn main() -> ExitCode {
175194
return export_variable(matches);
176195
} else {
177196
parser.write_help(&mut io::stderr()).ignore();
178-
return std::process::ExitCode::from(22 /* EINVAL */);
197+
return std::process::ExitCode::from(1);
179198
}
180199
}

src/lib/efivar/efivar/efi_variables.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::efivarfs;
2+
use std::io;
23
use std::path::PathBuf;
34

45
const EFIVARS_PATH: &'static str = "/sys/firmware/efi/vars";
@@ -21,7 +22,7 @@ impl EfiVariables {
2122
return self;
2223
}
2324

24-
pub fn list(&self) -> efivarfs::EfiVariablesNameIter {
25+
pub fn list(&self) -> io::Result<efivarfs::EfiVariablesNameIter> {
2526
let mut e: efivarfs::EfiVariables = Default::default();
2627
e.set_path(self.path.clone());
2728
return e.list();

src/lib/efivar/efivarfs/efi_variables.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::types::EfiGuid;
22
use std::fs::{self, ReadDir};
33
use std::path::PathBuf;
4+
use std::io;
45

56
const EFIVARFS_PATH: &'static str = "/sys/firmware/efi/efivars";
67

@@ -82,18 +83,25 @@ impl EfiVariables {
8283
return self;
8384
}
8485

85-
pub fn list(&mut self) -> EfiVariablesNameIter {
86-
if self.path.is_dir() {
87-
let iter = fs::read_dir(&self.path);
88-
return EfiVariablesNameIter {
89-
dir_entry_iter: match iter {
90-
Ok(i) => Some(i),
91-
Err(_) => None,
92-
},
93-
};
94-
}
95-
return EfiVariablesNameIter {
96-
dir_entry_iter: None,
86+
pub fn list(&mut self) -> io::Result<EfiVariablesNameIter> {
87+
return match fs::metadata(self.path.as_path()) {
88+
Ok(m) => {
89+
if m.is_dir() {
90+
let iter = fs::read_dir(&self.path);
91+
return Ok(EfiVariablesNameIter {
92+
dir_entry_iter: match iter {
93+
Ok(i) => Some(i),
94+
Err(_) => None,
95+
},
96+
});
97+
}
98+
/*
99+
* Should return NotADirectory, but Rust doesn't support that, so return NotFound
100+
* instead.
101+
*/
102+
return Err(io::ErrorKind::NotFound.into());
103+
},
104+
Err(e) => Err(e),
97105
};
98106
}
99107
}

0 commit comments

Comments
 (0)