mirror of
https://github.com/chatmail/core.git
synced 2026-04-29 11:26:29 +03:00
Make dc_msg_get_file return PathBuf, not char*
Adjust call sites as apporiate: * src/dc_imex.rs(dc_continue_key_transfer): use if-let pattern with dc_read_file_safe() and dc_msg_get_file() * src/message.rs(dc_msg_get_setupcodebegin): ditto * src/message.rs(dc_get_msg_info): simplify code to print information about file inside a message. * src/message.rs(dc_msg_get_file): simplify function using dc_get_abs_path_safe() * deltachat-ffi/src/lib.rs(dc_msg_get_file): convert PathBuf to `char *` to preserve C API
This commit is contained in:
@@ -269,54 +269,48 @@ pub unsafe fn dc_continue_key_transfer(
|
||||
setup_code: *const libc::c_char,
|
||||
) -> bool {
|
||||
let mut success = false;
|
||||
let mut filename: *mut libc::c_char = ptr::null_mut();
|
||||
let mut filecontent: *mut libc::c_char = ptr::null_mut();
|
||||
let mut filebytes: size_t = 0i32 as size_t;
|
||||
let mut armored_key: *mut libc::c_char = ptr::null_mut();
|
||||
let mut norm_sc: *mut libc::c_char = ptr::null_mut();
|
||||
let norm_sc;
|
||||
if msg_id <= 9i32 as libc::c_uint || setup_code.is_null() {
|
||||
return false;
|
||||
}
|
||||
|
||||
let msg = dc_get_msg(context, msg_id);
|
||||
if msg.is_err()
|
||||
|| !dc_msg_is_setupmessage(msg.as_ref().unwrap())
|
||||
|| {
|
||||
filename = dc_msg_get_file(context, msg.as_ref().unwrap());
|
||||
filename.is_null()
|
||||
}
|
||||
|| *filename.offset(0isize) as libc::c_int == 0i32
|
||||
{
|
||||
error!(context, "Message is no Autocrypt Setup Message.",);
|
||||
} else if 0
|
||||
== dc_read_file(
|
||||
context,
|
||||
filename,
|
||||
&mut filecontent as *mut *mut libc::c_char as *mut *mut libc::c_void,
|
||||
&mut filebytes,
|
||||
)
|
||||
|| filecontent.is_null()
|
||||
|| filebytes <= 0
|
||||
{
|
||||
error!(context, "Cannot read Autocrypt Setup Message file.",);
|
||||
} else {
|
||||
norm_sc = dc_normalize_setup_code(context, setup_code);
|
||||
if norm_sc.is_null() {
|
||||
warn!(context, "Cannot normalize Setup Code.",);
|
||||
} else {
|
||||
armored_key = dc_decrypt_setup_file(context, norm_sc, filecontent);
|
||||
if armored_key.is_null() {
|
||||
warn!(context, "Cannot decrypt Autocrypt Setup Message.",);
|
||||
} else if set_self_key(context, armored_key, 1) {
|
||||
/*set default*/
|
||||
/* error already logged */
|
||||
success = true
|
||||
if msg.is_err() {
|
||||
error!(context, "Message is no Autocrypt Setup Message.");
|
||||
return false;
|
||||
}
|
||||
let msg = msg.unwrap();
|
||||
if !dc_msg_is_setupmessage(&msg) {
|
||||
error!(context, "Message is no Autocrypt Setup Message.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if let Some(filename) = dc_msg_get_file(context, &msg) {
|
||||
if let Some(buf) = dc_read_file_safe(context, filename) {
|
||||
norm_sc = dc_normalize_setup_code(context, setup_code);
|
||||
if norm_sc.is_null() {
|
||||
warn!(context, "Cannot normalize Setup Code.",);
|
||||
} else {
|
||||
armored_key = dc_decrypt_setup_file(context, norm_sc, buf.as_ptr().cast());
|
||||
if armored_key.is_null() {
|
||||
warn!(context, "Cannot decrypt Autocrypt Setup Message.",);
|
||||
} else if set_self_key(context, armored_key, 1) {
|
||||
/*set default*/
|
||||
/* error already logged */
|
||||
success = true
|
||||
}
|
||||
}
|
||||
} else {
|
||||
error!(context, "Cannot read Autocrypt Setup Message file.",);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
error!(context, "Message is no Autocrypt Setup Message.");
|
||||
return false;
|
||||
}
|
||||
|
||||
free(armored_key as *mut libc::c_void);
|
||||
free(filecontent as *mut libc::c_void);
|
||||
free(filename as *mut libc::c_void);
|
||||
free(norm_sc as *mut libc::c_void);
|
||||
|
||||
success
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::ffi::CString;
|
||||
use std::path::Path;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::ptr;
|
||||
|
||||
use deltachat_derive::{FromSql, ToSql};
|
||||
@@ -269,15 +269,10 @@ pub unsafe fn dc_get_msg_info(context: &Context, msg_id: u32) -> *mut libc::c_ch
|
||||
_ => {}
|
||||
}
|
||||
|
||||
let p = dc_msg_get_file(context, &msg);
|
||||
if !p.is_null() && 0 != *p.offset(0isize) as libc::c_int {
|
||||
ret += &format!(
|
||||
"\nFile: {}, {}, bytes\n",
|
||||
as_str(p),
|
||||
dc_get_filebytes(context, as_path(p)) as libc::c_int,
|
||||
);
|
||||
if let Some(path) = dc_msg_get_file(context, &msg) {
|
||||
let bytes = dc_get_filebytes(context, &path);
|
||||
ret += &format!("\nFile: {}, {}, bytes\n", path.display(), bytes);
|
||||
}
|
||||
free(p as *mut libc::c_void);
|
||||
|
||||
if msg.type_0 != Viewtype::Text {
|
||||
ret += "Type: ";
|
||||
@@ -378,17 +373,10 @@ pub fn dc_msg_guess_msgtype_from_suffix(path: &Path) -> Option<(Viewtype, &str)>
|
||||
KNOWN.get(extension).map(|x| *x)
|
||||
}
|
||||
|
||||
pub unsafe fn dc_msg_get_file(context: &Context, msg: &Message) -> *mut libc::c_char {
|
||||
let mut file_abs = ptr::null_mut();
|
||||
|
||||
if let Some(file_rel) = msg.param.get(Param::File) {
|
||||
file_abs = dc_get_abs_path(context, file_rel);
|
||||
}
|
||||
if !file_abs.is_null() {
|
||||
file_abs
|
||||
} else {
|
||||
dc_strdup(0 as *const libc::c_char)
|
||||
}
|
||||
pub unsafe fn dc_msg_get_file(context: &Context, msg: &Message) -> Option<PathBuf> {
|
||||
msg.param
|
||||
.get(Param::File)
|
||||
.map(|f| dc_get_abs_path_safe(context, f))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -896,29 +884,16 @@ pub fn dc_msg_is_setupmessage(msg: &Message) -> bool {
|
||||
}
|
||||
|
||||
pub unsafe fn dc_msg_get_setupcodebegin(context: &Context, msg: &Message) -> *mut libc::c_char {
|
||||
let mut filename: *mut libc::c_char = ptr::null_mut();
|
||||
let mut buf: *mut libc::c_char = ptr::null_mut();
|
||||
let mut buf_bytes: size_t = 0i32 as size_t;
|
||||
// just a pointer inside buf, MUST NOT be free()'d
|
||||
let mut buf_headerline: *const libc::c_char = ptr::null();
|
||||
// just a pointer inside buf, MUST NOT be free()'d
|
||||
let mut buf_setupcodebegin: *const libc::c_char = ptr::null();
|
||||
let mut ret: *mut libc::c_char = ptr::null_mut();
|
||||
if dc_msg_is_setupmessage(msg) {
|
||||
filename = dc_msg_get_file(context, msg);
|
||||
if !(filename.is_null() || *filename.offset(0isize) as libc::c_int == 0i32) {
|
||||
if !(0
|
||||
== dc_read_file(
|
||||
context,
|
||||
filename,
|
||||
&mut buf as *mut *mut libc::c_char as *mut *mut libc::c_void,
|
||||
&mut buf_bytes,
|
||||
)
|
||||
|| buf.is_null()
|
||||
|| buf_bytes <= 0)
|
||||
{
|
||||
if let Some(filename) = dc_msg_get_file(context, msg) {
|
||||
if let Some(mut buf) = dc_read_file_safe(context, filename) {
|
||||
if dc_split_armored_data(
|
||||
buf,
|
||||
buf.as_mut_ptr().cast(),
|
||||
&mut buf_headerline,
|
||||
&mut buf_setupcodebegin,
|
||||
ptr::null_mut(),
|
||||
@@ -934,8 +909,6 @@ pub unsafe fn dc_msg_get_setupcodebegin(context: &Context, msg: &Message) -> *mu
|
||||
}
|
||||
}
|
||||
}
|
||||
free(filename as *mut libc::c_void);
|
||||
free(buf as *mut libc::c_void);
|
||||
if !ret.is_null() {
|
||||
ret
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user