majorly de-indent code structure in secure_join by introducing cleanup function, also majorly reducing unsafety in several places

This commit is contained in:
holger krekel
2019-09-05 01:26:45 +02:00
parent b2fafeff19
commit 4bbab876ae
5 changed files with 572 additions and 641 deletions

View File

@@ -854,7 +854,7 @@ pub fn unarchive(context: &Context, chat_id: u32) -> Result<(), Error> {
/// However, this does not imply, the message really reached the recipient -
/// sending may be delayed eg. due to network problems. However, from your
/// view, you're done with the message. Sooner or later it will find its way.
pub unsafe fn send_msg<'a>(
pub fn send_msg<'a>(
context: &'a Context,
chat_id: u32,
msg: &mut Message<'a>,
@@ -872,7 +872,7 @@ pub unsafe fn send_msg<'a>(
}
ensure!(
job_send_msg(context, msg.id) != 0,
unsafe { job_send_msg(context, msg.id) } != 0,
"Failed to initiate send job"
);
@@ -1394,7 +1394,7 @@ pub unsafe fn add_contact_to_chat(context: &Context, chat_id: u32, contact_id: u
// TODO should return bool /rtn
#[allow(non_snake_case)]
pub unsafe fn add_contact_to_chat_ex(
pub fn add_contact_to_chat_ex(
context: &Context,
chat_id: u32,
contact_id: u32,
@@ -1407,7 +1407,7 @@ pub unsafe fn add_contact_to_chat_ex(
if contact.is_err() || chat_id <= DC_CHAT_ID_LAST_SPECIAL {
return 0;
}
let mut msg = dc_msg_new_untyped(context);
let mut msg = unsafe { dc_msg_new_untyped(context) };
reset_gossiped_timestamp(context, chat_id);
let contact = contact.unwrap();

View File

@@ -33,7 +33,7 @@ macro_rules! progress {
// connect
pub unsafe fn configure(context: &Context) {
if 0 != dc_has_ongoing(context) {
if dc_has_ongoing(context) {
warn!(
context,
0, "There is already another ongoing process running.",
@@ -43,6 +43,7 @@ pub unsafe fn configure(context: &Context) {
job_kill_action(context, Action::ConfigureImap);
job_add(context, Action::ConfigureImap, 0, Params::new(), 0);
}
/// Check if the context is already configured.
pub fn dc_is_configured(context: &Context) -> libc::c_int {
if context
@@ -56,17 +57,6 @@ pub fn dc_is_configured(context: &Context) -> libc::c_int {
0
}
}
/// Check if there is an ongoing process.
unsafe fn dc_has_ongoing(context: &Context) -> libc::c_int {
let s_a = context.running_state.clone();
let s = s_a.read().unwrap();
if s.ongoing_running || !s.shall_stop_ongoing {
1
} else {
0
}
}
/*******************************************************************************
* Configure JOB
@@ -80,7 +70,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &Context, _job: &Job) {
let mut ongoing_allocated_here = false;
let mut param_autoconfig: Option<dc_loginparam_t> = None;
if !(0 == dc_alloc_ongoing(context)) {
if dc_alloc_ongoing(context) {
ongoing_allocated_here = true;
if !context.sql.is_open() {
error!(context, 0, "Cannot configure, database not opened.",);
@@ -591,7 +581,45 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: &Context, _job: &Job) {
progress!(context, (if success { 1000 } else { 0 }));
}
/* File Structure like in C: */
/*******************************************************************************
* Ongoing process allocation/free/check
******************************************************************************/
pub fn dc_alloc_ongoing(context: &Context) -> bool {
if dc_has_ongoing(context) {
warn!(
context,
0, "There is already another ongoing process running.",
);
false
} else {
let s_a = context.running_state.clone();
let mut s = s_a.write().unwrap();
s.ongoing_running = true;
s.shall_stop_ongoing = false;
true
}
}
pub fn dc_free_ongoing(context: &Context) {
let s_a = context.running_state.clone();
let mut s = s_a.write().unwrap();
s.ongoing_running = false;
s.shall_stop_ongoing = true;
}
fn dc_has_ongoing(context: &Context) -> bool {
let s_a = context.running_state.clone();
let s = s_a.read().unwrap();
s.ongoing_running || !s.shall_stop_ongoing
}
/*******************************************************************************
* Connect to configured account
@@ -624,35 +652,6 @@ pub fn dc_connect_to_configured_imap(context: &Context, imap: &Imap) -> libc::c_
* Configure a Context
******************************************************************************/
/// Request an ongoing process to start.
/// Returns 0=process started, 1=not started, there is running another process
pub unsafe fn dc_alloc_ongoing(context: &Context) -> libc::c_int {
if 0 != dc_has_ongoing(context) {
warn!(
context,
0, "There is already another ongoing process running.",
);
return 0;
}
let s_a = context.running_state.clone();
let mut s = s_a.write().unwrap();
s.ongoing_running = true;
s.shall_stop_ongoing = false;
1
}
/// Frees the process allocated with dc_alloc_ongoing() - independently of dc_shall_stop_ongoing.
/// If dc_alloc_ongoing() fails, this function MUST NOT be called.
pub unsafe fn dc_free_ongoing(context: &Context) {
let s_a = context.running_state.clone();
let mut s = s_a.write().unwrap();
s.ongoing_running = false;
s.shall_stop_ongoing = true;
}
/// Signal an ongoing process to stop.
pub fn dc_stop_ongoing_process(context: &Context) {
let s_a = context.running_state.clone();

View File

@@ -103,7 +103,7 @@ pub unsafe fn dc_imex_has_backup(
pub unsafe fn dc_initiate_key_transfer(context: &Context) -> *mut libc::c_char {
let mut setup_file_name: *mut libc::c_char = ptr::null_mut();
let mut msg: Message;
if dc_alloc_ongoing(context) == 0 {
if !dc_alloc_ongoing(context) {
return std::ptr::null_mut();
}
let setup_code = dc_create_setup_code(context);
@@ -502,11 +502,9 @@ pub unsafe fn dc_normalize_setup_code(
pub unsafe fn dc_job_do_DC_JOB_IMEX_IMAP(context: &Context, job: &Job) {
let mut ok_to_continue = true;
let mut success: libc::c_int = 0;
let mut ongoing_allocated_here: libc::c_int = 0;
let what: libc::c_int;
if !(0 == dc_alloc_ongoing(context)) {
ongoing_allocated_here = 1;
if dc_alloc_ongoing(context) {
what = job.param.get_int(Param::Cmd).unwrap_or_default();
let param1_s = job.param.get(Param::Arg).unwrap_or_default();
let param1 = CString::yolo(param1_s);
@@ -564,9 +562,6 @@ pub unsafe fn dc_job_do_DC_JOB_IMEX_IMAP(context: &Context, job: &Job) {
}
}
}
}
if 0 != ongoing_allocated_here {
dc_free_ongoing(context);
}
context.call_cb(

File diff suppressed because it is too large Load Diff

View File

@@ -220,7 +220,7 @@ pub fn send_locations_to_chat(context: &Context, chat_id: u32, seconds: i64) {
msg.text =
Some(context.stock_system_msg(StockMessage::MsgLocationEnabled, "", "", 0));
msg.param.set_int(Param::Cmd, 8);
unsafe { chat::send_msg(context, chat_id, &mut msg).unwrap() };
chat::send_msg(context, chat_id, &mut msg).unwrap();
} else if 0 == seconds && is_sending_locations_before {
let stock_str =
context.stock_system_msg(StockMessage::MsgLocationDisabled, "", "", 0);
@@ -607,7 +607,7 @@ pub fn job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context: &Context, _job: &Job) {
msg.hidden = true;
msg.param.set_int(Param::Cmd, 9);
// TODO: handle cleanup on error
unsafe { chat::send_msg(context, chat_id as u32, &mut msg).unwrap() };
chat::send_msg(context, chat_id as u32, &mut msg).unwrap();
}
Ok(())
},