Add "database_encrypted" field to Context.get_info()

This commit is contained in:
link2xt
2022-01-22 14:38:18 +00:00
parent 836e26d8d0
commit f21691c122
3 changed files with 22 additions and 0 deletions

View File

@@ -35,6 +35,7 @@
- sql: enable `auto_vacuum=INCREMENTAL` #2931
- Synchronize Seen status across devices #2942
- Add API to set the database password #2956
- Add information about whether the database is encrypted or not to `dc_get_info()` #3000
- Add Webxdc #2826 #2971 #2975 #2977 #2979 #2993 #2998
### Changed

View File

@@ -383,6 +383,13 @@ impl Context {
res.insert("number_of_contacts", contacts.to_string());
res.insert("database_dir", self.get_dbfile().display().to_string());
res.insert("database_version", dbversion.to_string());
res.insert(
"database_encrypted",
self.sql
.is_encrypted()
.await
.map_or_else(|| "closed".to_string(), |b| b.to_string()),
);
res.insert("journal_mode", journal_mode);
res.insert("blobdir", self.get_blobdir().display().to_string());
res.insert("display_name", displayname.unwrap_or_else(|| unset.into()));

View File

@@ -43,6 +43,10 @@ pub struct Sql {
pub(crate) dbfile: PathBuf,
pool: RwLock<Option<r2d2::Pool<r2d2_sqlite::SqliteConnectionManager>>>,
/// None if the database is not open, true if it is open with passphrase and false if it is
/// open without a passphrase.
is_encrypted: RwLock<Option<bool>>,
}
impl Sql {
@@ -50,6 +54,7 @@ impl Sql {
Self {
dbfile,
pool: Default::default(),
is_encrypted: Default::default(),
}
}
@@ -85,6 +90,13 @@ impl Sql {
self.pool.read().await.is_some()
}
/// Returns true if the database is encrypted.
///
/// If database is not open, returns `None`.
pub(crate) async fn is_encrypted(&self) -> Option<bool> {
*self.is_encrypted.read().await
}
/// Closes all underlying Sqlite connections.
async fn close(&self) {
let _ = self.pool.write().await.take();
@@ -286,11 +298,13 @@ impl Sql {
bail!("SQL database is already opened.");
}
let passphrase_nonempty = !passphrase.is_empty();
if let Err(err) = self.try_open(context, &self.dbfile, passphrase).await {
self.close().await;
Err(err)
} else {
info!(context, "Opened database {:?}.", self.dbfile);
*self.is_encrypted.write().await = Some(passphrase_nonempty);
Ok(())
}
}