Skip to content

Commit 2ec6eb3

Browse files
client: Add a with_broken_item_cleanup API
Make it easy to do the migration code path without forcing every API to write a bunch of code
1 parent 0c58687 commit 2ec6eb3

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

client/src/keyring.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,21 @@ pub enum Keyring {
2525
}
2626

2727
impl Keyring {
28+
/// Create a new instance of the Keyring that automatically removes the
29+
/// broken items from the file backend keyring.
30+
///
31+
/// This method will probably be removed in future versions if the
32+
/// misbehaviour is tracked and fixed.
33+
pub async fn with_broken_item_cleanup() -> Result<Self> {
34+
Self::new_inner(true).await
35+
}
36+
2837
/// Create a new instance of the Keyring.
2938
pub async fn new() -> Result<Self> {
39+
Self::new_inner(false).await
40+
}
41+
42+
async fn new_inner(auto_delete_broken_items: bool) -> Result<Self> {
3043
let is_sandboxed = ashpd::is_sandboxed().await;
3144
if is_sandboxed {
3245
#[cfg(feature = "tracing")]
@@ -41,7 +54,15 @@ impl Keyring {
4154
"org.freedesktop.portal.Secrets is not available, falling back to the Secret Service backend"
4255
);
4356
}
44-
Err(e) => return Err(crate::Error::File(e)),
57+
Err(e) => {
58+
if matches!(e, file::Error::IncorrectSecret) && auto_delete_broken_items {
59+
let keyring = unsafe { file::Keyring::load_default_unchecked().await? };
60+
let deleted_items = keyring.delete_broken_items().await?;
61+
debug_assert!(deleted_items > 0);
62+
return Ok(Self::File(Arc::new(keyring)));
63+
}
64+
return Err(crate::Error::File(e));
65+
}
4566
};
4667
} else {
4768
#[cfg(feature = "tracing")]

0 commit comments

Comments
 (0)