mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 09: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:
@@ -17,7 +17,7 @@ use std::ptr;
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use deltachat::contact::Contact;
|
use deltachat::contact::Contact;
|
||||||
use deltachat::dc_tools::{as_str, dc_strdup, StrExt};
|
use deltachat::dc_tools::{as_str, dc_strdup, OsStrExt, StrExt};
|
||||||
use deltachat::*;
|
use deltachat::*;
|
||||||
|
|
||||||
// as C lacks a good and portable error handling,
|
// as C lacks a good and portable error handling,
|
||||||
@@ -2013,6 +2013,9 @@ pub unsafe extern "C" fn dc_msg_get_file(msg: *mut dc_msg_t) -> *mut libc::c_cha
|
|||||||
}
|
}
|
||||||
let ffi_msg = &*msg;
|
let ffi_msg = &*msg;
|
||||||
message::dc_msg_get_file(&*ffi_msg.context, &ffi_msg.message)
|
message::dc_msg_get_file(&*ffi_msg.context, &ffi_msg.message)
|
||||||
|
.and_then(|p| p.to_c_string().ok())
|
||||||
|
.map(|cs| dc_strdup(cs.as_ptr()))
|
||||||
|
.unwrap_or_else(|| dc_strdup(ptr::null()))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
|||||||
@@ -269,41 +269,30 @@ pub unsafe fn dc_continue_key_transfer(
|
|||||||
setup_code: *const libc::c_char,
|
setup_code: *const libc::c_char,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
let mut success = false;
|
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 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() {
|
if msg_id <= 9i32 as libc::c_uint || setup_code.is_null() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
let msg = dc_get_msg(context, msg_id);
|
let msg = dc_get_msg(context, msg_id);
|
||||||
if msg.is_err()
|
if msg.is_err() {
|
||||||
|| !dc_msg_is_setupmessage(msg.as_ref().unwrap())
|
error!(context, "Message is no Autocrypt Setup Message.");
|
||||||
|| {
|
return false;
|
||||||
filename = dc_msg_get_file(context, msg.as_ref().unwrap());
|
|
||||||
filename.is_null()
|
|
||||||
}
|
}
|
||||||
|| *filename.offset(0isize) as libc::c_int == 0i32
|
let msg = msg.unwrap();
|
||||||
{
|
if !dc_msg_is_setupmessage(&msg) {
|
||||||
error!(context, "Message is no Autocrypt Setup Message.",);
|
error!(context, "Message is no Autocrypt Setup Message.");
|
||||||
} else if 0
|
return false;
|
||||||
== dc_read_file(
|
}
|
||||||
context,
|
|
||||||
filename,
|
if let Some(filename) = dc_msg_get_file(context, &msg) {
|
||||||
&mut filecontent as *mut *mut libc::c_char as *mut *mut libc::c_void,
|
if let Some(buf) = dc_read_file_safe(context, filename) {
|
||||||
&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);
|
norm_sc = dc_normalize_setup_code(context, setup_code);
|
||||||
if norm_sc.is_null() {
|
if norm_sc.is_null() {
|
||||||
warn!(context, "Cannot normalize Setup Code.",);
|
warn!(context, "Cannot normalize Setup Code.",);
|
||||||
} else {
|
} else {
|
||||||
armored_key = dc_decrypt_setup_file(context, norm_sc, filecontent);
|
armored_key = dc_decrypt_setup_file(context, norm_sc, buf.as_ptr().cast());
|
||||||
if armored_key.is_null() {
|
if armored_key.is_null() {
|
||||||
warn!(context, "Cannot decrypt Autocrypt Setup Message.",);
|
warn!(context, "Cannot decrypt Autocrypt Setup Message.",);
|
||||||
} else if set_self_key(context, armored_key, 1) {
|
} else if set_self_key(context, armored_key, 1) {
|
||||||
@@ -312,11 +301,16 @@ pub unsafe fn dc_continue_key_transfer(
|
|||||||
success = true
|
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(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);
|
free(norm_sc as *mut libc::c_void);
|
||||||
|
|
||||||
success
|
success
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
use std::path::Path;
|
use std::path::{Path, PathBuf};
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
|
|
||||||
use deltachat_derive::{FromSql, ToSql};
|
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 let Some(path) = dc_msg_get_file(context, &msg) {
|
||||||
if !p.is_null() && 0 != *p.offset(0isize) as libc::c_int {
|
let bytes = dc_get_filebytes(context, &path);
|
||||||
ret += &format!(
|
ret += &format!("\nFile: {}, {}, bytes\n", path.display(), bytes);
|
||||||
"\nFile: {}, {}, bytes\n",
|
|
||||||
as_str(p),
|
|
||||||
dc_get_filebytes(context, as_path(p)) as libc::c_int,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
free(p as *mut libc::c_void);
|
|
||||||
|
|
||||||
if msg.type_0 != Viewtype::Text {
|
if msg.type_0 != Viewtype::Text {
|
||||||
ret += "Type: ";
|
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)
|
KNOWN.get(extension).map(|x| *x)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn dc_msg_get_file(context: &Context, msg: &Message) -> *mut libc::c_char {
|
pub unsafe fn dc_msg_get_file(context: &Context, msg: &Message) -> Option<PathBuf> {
|
||||||
let mut file_abs = ptr::null_mut();
|
msg.param
|
||||||
|
.get(Param::File)
|
||||||
if let Some(file_rel) = msg.param.get(Param::File) {
|
.map(|f| dc_get_abs_path_safe(context, f))
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -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 {
|
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
|
// just a pointer inside buf, MUST NOT be free()'d
|
||||||
let mut buf_headerline: *const libc::c_char = ptr::null();
|
let mut buf_headerline: *const libc::c_char = ptr::null();
|
||||||
// just a pointer inside buf, MUST NOT be free()'d
|
// just a pointer inside buf, MUST NOT be free()'d
|
||||||
let mut buf_setupcodebegin: *const libc::c_char = ptr::null();
|
let mut buf_setupcodebegin: *const libc::c_char = ptr::null();
|
||||||
let mut ret: *mut libc::c_char = ptr::null_mut();
|
let mut ret: *mut libc::c_char = ptr::null_mut();
|
||||||
if dc_msg_is_setupmessage(msg) {
|
if dc_msg_is_setupmessage(msg) {
|
||||||
filename = dc_msg_get_file(context, msg);
|
if let Some(filename) = dc_msg_get_file(context, msg) {
|
||||||
if !(filename.is_null() || *filename.offset(0isize) as libc::c_int == 0i32) {
|
if let Some(mut buf) = dc_read_file_safe(context, filename) {
|
||||||
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 dc_split_armored_data(
|
if dc_split_armored_data(
|
||||||
buf,
|
buf.as_mut_ptr().cast(),
|
||||||
&mut buf_headerline,
|
&mut buf_headerline,
|
||||||
&mut buf_setupcodebegin,
|
&mut buf_setupcodebegin,
|
||||||
ptr::null_mut(),
|
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() {
|
if !ret.is_null() {
|
||||||
ret
|
ret
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user