mirror of
https://github.com/chatmail/core.git
synced 2026-05-05 06:16:30 +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:
@@ -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