diff --git a/src/context.rs b/src/context.rs index 98d37af4d..53adbf8e8 100644 --- a/src/context.rs +++ b/src/context.rs @@ -533,7 +533,7 @@ fn get_config_keys_str() -> String { let keys = &CONFIG_KEYS[..].join(" "); let sys_keys = &SYS_CONFIG_KEYS[..].join(" "); - format!("{} {}", keys, sys_keys) + format!(" {} {} ", keys, sys_keys) } pub unsafe fn dc_get_info(context: &Context) -> *mut libc::c_char { diff --git a/src/dc_imex.rs b/src/dc_imex.rs index 2aaa29172..4d4af60f2 100644 --- a/src/dc_imex.rs +++ b/src/dc_imex.rs @@ -964,7 +964,7 @@ unsafe fn export_backup(context: &Context, dir: *const libc::c_char) -> libc::c_ /*for logging only*/ let sql = SQLite::new(); if sql.open(context, as_path(dest_pathNfilename), 0) { - if 0 == dc_sqlite3_table_exists(context, &sql, "backup_blobs") { + if !sql.table_exists("backup_blobs") { if !dc_sqlite3_execute( context, &sql, diff --git a/src/dc_sqlite3.rs b/src/dc_sqlite3.rs index a709a15d1..d19bf06c8 100644 --- a/src/dc_sqlite3.rs +++ b/src/dc_sqlite3.rs @@ -107,19 +107,21 @@ impl SQLite { pub fn table_exists(&self, name: impl AsRef) -> bool { match &*self.connection.read().unwrap() { - Some(conn) => { - conn.pragma(None, "table_info", &format!("{}", name.as_ref()), |row| { - // will only be executed if the info was found - println!("row: {:?}", row.get::<_, String>(0)?); - Ok(()) - }) - .is_ok() - } + Some(conn) => table_exists(conn, name), None => panic!("Querying closed SQLite database"), } } } +fn table_exists(conn: &Connection, name: impl AsRef) -> bool { + conn.pragma(None, "table_info", &format!("{}", name.as_ref()), |row| { + // will only be executed if the info was found + println!("row: {:?}", row.get::<_, String>(0)?); + Ok(()) + }) + .is_ok() +} + // Return 1 -> success // Return 0 -> failure fn dc_sqlite3_open( @@ -161,7 +163,7 @@ fn dc_sqlite3_open( } } - let conn_lock = sql.connection.write().unwrap(); + let conn_lock = sql.connection.read().unwrap(); let conn = conn_lock.as_ref().expect("just opened"); conn.pragma_update(None, "secure_delete", &"on".to_string()) @@ -173,7 +175,7 @@ fn dc_sqlite3_open( let mut exists_before_update = 0; let mut dbversion_before_update = 0; /* Init tables to dbversion=0 */ - if 0 == dc_sqlite3_table_exists(context, sql, "config") { + if !table_exists(conn, "config") { info!( context, 0, @@ -339,12 +341,12 @@ fn dc_sqlite3_open( "CREATE INDEX jobs_index1 ON jobs (desired_timestamp);", params![], ); - if 0 == dc_sqlite3_table_exists(context, sql, "config") - || 0 == dc_sqlite3_table_exists(context, sql, "contacts") - || 0 == dc_sqlite3_table_exists(context, sql, "chats") - || 0 == dc_sqlite3_table_exists(context, sql, "chats_contacts") - || 0 == dc_sqlite3_table_exists(context, sql, "msgs") - || 0 == dc_sqlite3_table_exists(context, sql, "jobs") + if !table_exists(conn, "config") + || !table_exists(conn, "contacts") + || !table_exists(conn, "chats") + || !table_exists(conn, "chats_contacts") + || !table_exists(conn, "msgs") + || !table_exists(conn, "jobs") { error!( context, @@ -965,17 +967,6 @@ pub fn dc_sqlite3_get_config_int( .unwrap_or_else(|| def) } -pub fn dc_sqlite3_table_exists( - _context: &Context, - sql: &SQLite, - name: impl AsRef, -) -> libc::c_int { - match sql.table_exists(name) { - true => 1, - false => 0, - } -} - pub fn dc_sqlite3_set_config_int64( context: &Context, sql: &SQLite,