Keystore 2.0 legacy_blob.rs: Fix list non existing user.
In when listing the keys for a user an empty list should be returned
instead of a system error if the user did not exists in the legacy
databse.
Test: atest keystore2_test
Change-Id: Ic9d4822dfe002adf2728b7f84e3e122a5bd1db24
diff --git a/keystore2/src/legacy_blob.rs b/keystore2/src/legacy_blob.rs
index 65e6818..a3e440b 100644
--- a/keystore2/src/legacy_blob.rs
+++ b/keystore2/src/legacy_blob.rs
@@ -801,10 +801,18 @@
/// encoded with UID prefix.
fn list_user(&self, user_id: u32) -> Result<Vec<String>> {
let path = self.make_user_path_name(user_id);
- let dir =
- Self::with_retry_interrupted(|| fs::read_dir(path.as_path())).with_context(|| {
- format!("In list_user: Failed to open legacy blob database. {:?}", path)
- })?;
+ let dir = match Self::with_retry_interrupted(|| fs::read_dir(path.as_path())) {
+ Ok(dir) => dir,
+ Err(e) => match e.kind() {
+ ErrorKind::NotFound => return Ok(Default::default()),
+ _ => {
+ return Err(e).context(format!(
+ "In list_user: Failed to open legacy blob database. {:?}",
+ path
+ ))
+ }
+ },
+ };
let mut result: Vec<String> = Vec::new();
for entry in dir {
let file_name = entry.context("In list_user: Trying to access dir entry")?.file_name();
@@ -1352,4 +1360,14 @@
Ok(())
}
+
+ #[test]
+ fn list_non_existing_user() -> Result<()> {
+ let temp_dir = TempDir::new("list_non_existing_user")?;
+ let legacy_blob_loader = LegacyBlobLoader::new(temp_dir.path());
+
+ assert!(legacy_blob_loader.list_user(20)?.is_empty());
+
+ Ok(())
+ }
}