mirror of
https://github.com/chatmail/core.git
synced 2026-04-21 15:36:30 +03:00
Make dc_open arguments rusty
This commit is contained in:
@@ -56,9 +56,16 @@ pub unsafe extern "C" fn dc_open(
|
||||
blobdir: *mut libc::c_char,
|
||||
) -> libc::c_int {
|
||||
assert!(!context.is_null());
|
||||
assert!(!dbfile.is_null());
|
||||
let context = &mut *context;
|
||||
|
||||
context::dc_open(context, dbfile, blobdir)
|
||||
let dbfile_str = dc_tools::as_str(dbfile);
|
||||
let blobdir_str = if blobdir.is_null() {
|
||||
None
|
||||
} else {
|
||||
Some(dc_tools::as_str(blobdir))
|
||||
};
|
||||
context::dc_open(context, dbfile_str, blobdir_str) as libc::c_int
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
|
||||
@@ -503,10 +503,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
|
||||
"open" => {
|
||||
ensure!(!arg1.is_empty(), "Argument <file> missing");
|
||||
dc_close(context);
|
||||
ensure!(
|
||||
0 != dc_open(context, arg1_c, 0 as *const libc::c_char),
|
||||
"Open failed"
|
||||
);
|
||||
ensure!(dc_open(context, arg1, None), "Open failed");
|
||||
}
|
||||
"close" => {
|
||||
dc_close(context);
|
||||
|
||||
@@ -399,12 +399,7 @@ fn main_0(args: Vec<String>) -> Result<(), failure::Error> {
|
||||
unsafe { dc_cmdline_skip_auth() };
|
||||
|
||||
if args.len() == 2 {
|
||||
if 0 == unsafe {
|
||||
let a = to_cstring(&args[1]);
|
||||
let res = dc_open(&mut context, a, 0 as *const _);
|
||||
free(a as *mut _);
|
||||
res
|
||||
} {
|
||||
if unsafe { !dc_open(&mut context, &args[1], None) } {
|
||||
println!("Error: Cannot open {}.", args[0],);
|
||||
}
|
||||
} else if args.len() != 1 {
|
||||
|
||||
@@ -76,11 +76,11 @@ fn main() {
|
||||
});
|
||||
|
||||
let dir = tempdir().unwrap();
|
||||
let dbfile = CString::new(dir.path().join("db.sqlite").to_str().unwrap()).unwrap();
|
||||
let dbfile = dir.path().join("db.sqlite");
|
||||
|
||||
println!("opening database {:?}", dbfile);
|
||||
|
||||
assert_eq!(dc_open(&ctx, dbfile.as_ptr(), std::ptr::null()), 1);
|
||||
assert!(dc_open(&ctx, dbfile.to_str().unwrap(), None));
|
||||
|
||||
println!("configuring");
|
||||
let args = std::env::args().collect::<Vec<String>>();
|
||||
|
||||
@@ -315,32 +315,26 @@ pub unsafe fn dc_get_userdata(context: &mut Context) -> *mut libc::c_void {
|
||||
context.userdata as *mut _
|
||||
}
|
||||
|
||||
pub unsafe fn dc_open(
|
||||
context: &Context,
|
||||
dbfile: *const libc::c_char,
|
||||
blobdir: *const libc::c_char,
|
||||
) -> libc::c_int {
|
||||
let mut success = 0;
|
||||
pub unsafe fn dc_open(context: &Context, dbfile: &str, blobdir: Option<&str>) -> bool {
|
||||
let mut success = false;
|
||||
if 0 != dc_is_open(context) {
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
if !dbfile.is_null() {
|
||||
*context.dbfile.write().unwrap() = dc_strdup(dbfile);
|
||||
if !blobdir.is_null() && 0 != *blobdir.offset(0isize) as libc::c_int {
|
||||
let dir = dc_strdup(blobdir);
|
||||
dc_ensure_no_slash(dir);
|
||||
*context.blobdir.write().unwrap() = dir;
|
||||
} else {
|
||||
let dir = dc_mprintf(b"%s-blobs\x00" as *const u8 as *const libc::c_char, dbfile);
|
||||
dc_create_folder(context, dir);
|
||||
*context.blobdir.write().unwrap() = dir;
|
||||
}
|
||||
// Create/open sqlite database, this may already use the blobdir
|
||||
if context.sql.open(context, as_path(dbfile), 0) {
|
||||
success = 1i32
|
||||
}
|
||||
*context.dbfile.write().unwrap() = to_cstring(dbfile);
|
||||
if blobdir.is_some() && blobdir.unwrap().len() > 0 {
|
||||
let dir = to_cstring(dc_ensure_no_slash_safe(blobdir.unwrap()));
|
||||
*context.blobdir.write().unwrap() = dir;
|
||||
} else {
|
||||
let dir = to_cstring(dbfile.to_string() + "-blobs");
|
||||
dc_create_folder(context, dir);
|
||||
*context.blobdir.write().unwrap() = dir;
|
||||
}
|
||||
if 0 == success {
|
||||
// Create/open sqlite database, this may already use the blobdir
|
||||
let dbfile_path = std::path::Path::new(dbfile);
|
||||
if context.sql.open(context, dbfile_path, 0) {
|
||||
success = true
|
||||
}
|
||||
if !success {
|
||||
dc_close(context);
|
||||
}
|
||||
success
|
||||
|
||||
@@ -462,12 +462,9 @@ mod tests {
|
||||
use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use std::ffi::CStr;
|
||||
use tempfile::{tempdir, TempDir};
|
||||
|
||||
use crate::context::*;
|
||||
use crate::dc_tools::to_cstring;
|
||||
use crate::x::free;
|
||||
|
||||
#[test]
|
||||
fn test_peerstate_save_to_db() {
|
||||
@@ -522,16 +519,13 @@ mod tests {
|
||||
unsafe fn create_test_context() -> TestContext {
|
||||
let mut ctx = dc_context_new(Some(cb), std::ptr::null_mut(), std::ptr::null_mut());
|
||||
let dir = tempdir().unwrap();
|
||||
let dbfile = to_cstring(dir.path().join("db.sqlite").to_str().unwrap());
|
||||
assert_eq!(
|
||||
dc_open(&mut ctx, dbfile, std::ptr::null()),
|
||||
1,
|
||||
let dbfile = dir.path().join("db.sqlite");
|
||||
assert!(
|
||||
dc_open(&mut ctx, dbfile.to_str().unwrap(), None),
|
||||
"Failed to open {}",
|
||||
CStr::from_ptr(dbfile as *const _).to_str().unwrap()
|
||||
dbfile.to_str().unwrap()
|
||||
);
|
||||
|
||||
free(dbfile as *mut _);
|
||||
|
||||
TestContext { ctx: ctx, dir: dir }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,8 +7,6 @@ use tempfile::{tempdir, TempDir};
|
||||
use crate::context::{dc_context_new, dc_open, Context};
|
||||
use crate::types::dc_callback_t;
|
||||
|
||||
use crate::dc_tools::OsStrExt;
|
||||
|
||||
/// A Context and temporary directory.
|
||||
///
|
||||
/// The temporary directory can be used to store the SQLite database,
|
||||
@@ -29,10 +27,8 @@ pub fn test_context(cb: Option<dc_callback_t>) -> TestContext {
|
||||
let mut ctx = dc_context_new(cb, std::ptr::null_mut(), std::ptr::null_mut());
|
||||
let dir = tempdir().unwrap();
|
||||
let dbfile = dir.path().join("db.sqlite");
|
||||
let dbfile_c = dbfile.to_c_string().unwrap();
|
||||
assert_eq!(
|
||||
dc_open(&mut ctx, dbfile_c.as_ptr(), std::ptr::null()),
|
||||
1,
|
||||
assert!(
|
||||
dc_open(&mut ctx, dbfile.to_str().unwrap(), None),
|
||||
"Failed to open {}",
|
||||
dbfile.display(),
|
||||
);
|
||||
|
||||
@@ -817,14 +817,12 @@ struct TestContext {
|
||||
unsafe fn create_test_context() -> TestContext {
|
||||
let mut ctx = dc_context_new(Some(cb), std::ptr::null_mut(), std::ptr::null_mut());
|
||||
let dir = tempdir().unwrap();
|
||||
let dbfile = to_cstring(dir.path().join("db.sqlite").to_str().unwrap());
|
||||
assert_eq!(
|
||||
dc_open(&mut ctx, dbfile, std::ptr::null()),
|
||||
1,
|
||||
let dbfile = dir.path().join("db.sqlite");
|
||||
assert!(
|
||||
dc_open(&mut ctx, dbfile.to_str().unwrap(), None),
|
||||
"Failed to open {}",
|
||||
as_str(dbfile as *const libc::c_char)
|
||||
dbfile.to_str().unwrap()
|
||||
);
|
||||
free(dbfile as *mut _);
|
||||
TestContext { ctx: ctx, dir: dir }
|
||||
}
|
||||
|
||||
@@ -1006,9 +1004,7 @@ fn test_wrong_db() {
|
||||
let dbfile = dir.path().join("db.sqlite");
|
||||
std::fs::write(&dbfile, b"123").unwrap();
|
||||
|
||||
let dbfile_c = to_cstring(dbfile.to_str().unwrap());
|
||||
let res = dc_open(&mut ctx, dbfile_c, std::ptr::null());
|
||||
free(dbfile_c as *mut _);
|
||||
assert_eq!(res, 0);
|
||||
let res = dc_open(&mut ctx, dbfile.to_str().unwrap(), None);
|
||||
assert!(!res);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user