Replace flags argument to Sql.open with single bool

Previously, "flags" argument of Sql.open was of type libc::c_int, but
only one bit was used: whether to open database read-only. This commit
makes it explicit by changing type to bool and renaming argument.
This commit is contained in:
Dmitry Bogatov
2019-10-12 05:04:17 +00:00
committed by Floris Bruynooghe
parent 8479c8afbf
commit a4257b619a
3 changed files with 10 additions and 12 deletions

View File

@@ -150,7 +150,7 @@ impl Context {
}; };
ensure!( ensure!(
ctx.sql.open(&ctx, &ctx.dbfile, 0), ctx.sql.open(&ctx, &ctx.dbfile, false),
"Failed opening sqlite database" "Failed opening sqlite database"
); );

View File

@@ -87,7 +87,7 @@ pub fn has_backup(context: &Context, dir_name: impl AsRef<Path>) -> Result<Strin
let name = name.to_string_lossy(); let name = name.to_string_lossy();
if name.starts_with("delta-chat") && name.ends_with(".bak") { if name.starts_with("delta-chat") && name.ends_with(".bak") {
let sql = Sql::new(); let sql = Sql::new();
if sql.open(context, &path, 0x1) { if sql.open(context, &path, true) {
let curr_backup_time = let curr_backup_time =
sql.get_raw_config_int(context, "backup_time") sql.get_raw_config_int(context, "backup_time")
.unwrap_or_default() as u64; .unwrap_or_default() as u64;
@@ -413,7 +413,7 @@ fn import_backup(context: &Context, backup_to_import: impl AsRef<Path>) -> Resul
/* error already logged */ /* error already logged */
/* re-open copied database file */ /* re-open copied database file */
ensure!( ensure!(
context.sql.open(&context, &context.get_dbfile(), 0), context.sql.open(&context, &context.get_dbfile(), false),
"could not re-open db" "could not re-open db"
); );
@@ -496,7 +496,7 @@ fn export_backup(context: &Context, dir: impl AsRef<Path>) -> Result<()> {
dest_path_filename.display(), dest_path_filename.display(),
); );
let copied = dc_copy_file(context, context.get_dbfile(), &dest_path_filename); let copied = dc_copy_file(context, context.get_dbfile(), &dest_path_filename);
context.sql.open(&context, &context.get_dbfile(), 0); context.sql.open(&context, &context.get_dbfile(), false);
if !copied { if !copied {
let s = dest_path_filename.to_string_lossy().to_string(); let s = dest_path_filename.to_string_lossy().to_string();
bail!( bail!(
@@ -526,7 +526,7 @@ fn add_files_to_export(context: &Context, dest_path_filename: &PathBuf) -> Resul
// the source to be locked, neigher the destination as it is used only here) // the source to be locked, neigher the destination as it is used only here)
let sql = Sql::new(); let sql = Sql::new();
ensure!( ensure!(
sql.open(context, &dest_path_filename, 0), sql.open(context, &dest_path_filename, false),
"could not open db" "could not open db"
); );
if !sql.table_exists("backup_blobs") { if !sql.table_exists("backup_blobs") {

View File

@@ -11,8 +11,6 @@ use crate::error::{Error, Result};
use crate::param::*; use crate::param::*;
use crate::peerstate::*; use crate::peerstate::*;
const DC_OPEN_READONLY: usize = 0x01;
/// A wrapper around the underlying Sqlite3 object. /// A wrapper around the underlying Sqlite3 object.
#[derive(DebugStub)] #[derive(DebugStub)]
pub struct Sql { pub struct Sql {
@@ -42,8 +40,8 @@ impl Sql {
} }
// return true on success, false on failure // return true on success, false on failure
pub fn open(&self, context: &Context, dbfile: &std::path::Path, flags: libc::c_int) -> bool { pub fn open(&self, context: &Context, dbfile: &std::path::Path, readonly: bool) -> bool {
match open(context, self, dbfile, flags) { match open(context, self, dbfile, readonly) {
Ok(_) => true, Ok(_) => true,
Err(Error::SqlAlreadyOpen) => false, Err(Error::SqlAlreadyOpen) => false,
Err(_) => { Err(_) => {
@@ -320,7 +318,7 @@ fn open(
context: &Context, context: &Context,
sql: &Sql, sql: &Sql,
dbfile: impl AsRef<std::path::Path>, dbfile: impl AsRef<std::path::Path>,
flags: libc::c_int, readonly: bool,
) -> Result<()> { ) -> Result<()> {
if sql.is_open() { if sql.is_open() {
error!( error!(
@@ -332,7 +330,7 @@ fn open(
} }
let mut open_flags = OpenFlags::SQLITE_OPEN_NO_MUTEX; let mut open_flags = OpenFlags::SQLITE_OPEN_NO_MUTEX;
if 0 != (flags & DC_OPEN_READONLY as i32) { if readonly {
open_flags.insert(OpenFlags::SQLITE_OPEN_READ_ONLY); open_flags.insert(OpenFlags::SQLITE_OPEN_READ_ONLY);
} else { } else {
open_flags.insert(OpenFlags::SQLITE_OPEN_READ_WRITE); open_flags.insert(OpenFlags::SQLITE_OPEN_READ_WRITE);
@@ -351,7 +349,7 @@ fn open(
*sql.pool.write().unwrap() = Some(pool); *sql.pool.write().unwrap() = Some(pool);
} }
if 0 == flags & DC_OPEN_READONLY as i32 { if !readonly {
let mut exists_before_update = 0; let mut exists_before_update = 0;
let mut dbversion_before_update = 0; let mut dbversion_before_update = 0;
/* Init tables to dbversion=0 */ /* Init tables to dbversion=0 */