diff --git a/examples/repl/cmdline.rs b/examples/repl/cmdline.rs index 88deae915..fddfab0ba 100644 --- a/examples/repl/cmdline.rs +++ b/examples/repl/cmdline.rs @@ -132,7 +132,7 @@ pub unsafe extern "C" fn dc_reset_tables( b"(8) Rest but server config reset.\x00" as *const u8 as *const libc::c_char, ); } - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSGS_CHANGED, 0i32 as uintptr_t, @@ -380,7 +380,7 @@ unsafe extern "C" fn poke_spec( real_spec, ); if read_cnt > 0i32 { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSGS_CHANGED, 0i32 as uintptr_t, @@ -1749,12 +1749,8 @@ pub unsafe extern "C" fn dc_cmdline( } else if strcmp(cmd, b"event\x00" as *const u8 as *const libc::c_char) == 0i32 { if !arg1.is_null() { let mut event = Event::from_u32(atoi(arg1) as u32).unwrap(); - let mut r: uintptr_t = (*context).cb.expect("non-null function pointer")( - context, - event, - 0i32 as uintptr_t, - 0i32 as uintptr_t, - ); + let mut r: uintptr_t = + ((*context).cb)(context, event, 0i32 as uintptr_t, 0i32 as uintptr_t); ret = dc_mprintf( b"Sending event %i, received value %i.\x00" as *const u8 as *const libc::c_char, event as libc::c_int, diff --git a/examples/repl/main.rs b/examples/repl/main.rs index a9feeea04..efdb3edf8 100644 --- a/examples/repl/main.rs +++ b/examples/repl/main.rs @@ -77,7 +77,7 @@ use self::stress::*; ******************************************************************************/ static mut s_do_log_info: libc::c_int = 1i32; -unsafe fn receive_event( +unsafe extern "C" fn receive_event( mut context: *mut dc_context_t, mut event: Event, mut data1: uintptr_t, @@ -396,7 +396,7 @@ unsafe extern "C" fn read_cmd() -> *mut libc::c_char { unsafe fn main_0(mut argc: libc::c_int, mut argv: *mut *mut libc::c_char) -> libc::c_int { let mut cmd: *mut libc::c_char = 0 as *mut libc::c_char; let mut context: *mut dc_context_t = dc_context_new( - Some(receive_event), + receive_event, 0 as *mut libc::c_void, b"CLI\x00" as *const u8 as *const libc::c_char, ); diff --git a/examples/simple.rs b/examples/simple.rs index 862b60a17..fbfb70525 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -16,7 +16,7 @@ use deltachat::dc_job::{ }; use deltachat::dc_lot::*; -fn cb(_ctx: *mut dc_context_t, event: Event, data1: usize, data2: usize) -> usize { +extern "C" fn cb(_ctx: *mut dc_context_t, event: Event, data1: usize, data2: usize) -> usize { println!("[{:?}]", event); match event { @@ -56,7 +56,7 @@ unsafe impl std::marker::Sync for Wrapper {} fn main() { unsafe { - let ctx = dc_context_new(Some(cb), std::ptr::null_mut(), std::ptr::null_mut()); + let ctx = dc_context_new(cb, std::ptr::null_mut(), std::ptr::null_mut()); let info = dc_get_info(ctx); let info_s = CStr::from_ptr(info); println!("info: {}", info_s.to_str().unwrap()); diff --git a/src/dc_chat.rs b/src/dc_chat.rs index 21fc5b029..ad40eacc7 100644 --- a/src/dc_chat.rs +++ b/src/dc_chat.rs @@ -58,7 +58,7 @@ pub unsafe fn dc_create_chat_by_msg_id( dc_msg_unref(msg); dc_chat_unref(chat); if 0 != send_event { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSGS_CHANGED, 0i32 as uintptr_t, @@ -258,7 +258,7 @@ pub unsafe fn dc_create_chat_by_contact_id( dc_scaleup_contact_origin(context, contact_id, 0x800i32); } if 0 != send_event { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSGS_CHANGED, 0i32 as uintptr_t, @@ -442,7 +442,7 @@ pub unsafe fn dc_prepare_msg( } (*msg).state = 18i32; let mut msg_id: uint32_t = prepare_msg_common(context, chat_id, msg); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSGS_CHANGED, (*msg).chat_id as uintptr_t, @@ -767,7 +767,7 @@ unsafe fn prepare_msg_raw( VALUES (?,?,?, ?,?,1);\x00" as *const u8 as *const libc::c_char, ); - sqlite3_bind_int64(stmt, 1, timestamp); + sqlite3_bind_int64(stmt, 1, timestamp as libc::int64_t); sqlite3_bind_int(stmt, 2, DC_CONTACT_ID_SELF as libc::c_int); sqlite3_bind_int(stmt, 3, (*chat).id as libc::c_int); sqlite3_bind_double( @@ -1014,7 +1014,7 @@ pub unsafe fn dc_send_msg( if 0 == dc_job_send_msg(context, (*msg).id) { return 0i32 as uint32_t; } - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSGS_CHANGED, (*msg).chat_id as uintptr_t, @@ -1022,12 +1022,7 @@ pub unsafe fn dc_send_msg( ); if 0 != dc_param_exists((*msg).param, DC_PARAM_SET_LATITUDE as libc::c_int) { - (*context).cb.expect("non-null function pointer")( - context, - Event::LOCATION_CHANGED, - DC_CONTACT_ID_SELF, - 0, - ); + ((*context).cb)(context, Event::LOCATION_CHANGED, DC_CONTACT_ID_SELF, 0); } if 0 == chat_id { @@ -1085,7 +1080,7 @@ pub unsafe fn dc_set_draft( return; } if 0 != set_draft_raw(context, chat_id, msg) { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSGS_CHANGED, chat_id as uintptr_t, @@ -1330,7 +1325,7 @@ pub unsafe fn dc_marknoticed_chat(mut context: *mut dc_context_t, mut chat_id: u ); sqlite3_bind_int(update, 1i32, chat_id as libc::c_int); sqlite3_step(update); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSGS_CHANGED, 0i32 as uintptr_t, @@ -1356,7 +1351,7 @@ pub unsafe fn dc_marknoticed_all_chats(mut context: *mut dc_context_t) { as *const libc::c_char, ); sqlite3_step(update); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSGS_CHANGED, 0i32 as uintptr_t, @@ -1490,7 +1485,7 @@ pub unsafe fn dc_archive_chat( sqlite3_bind_int(stmt_0, 2i32, chat_id as libc::c_int); sqlite3_step(stmt_0); sqlite3_finalize(stmt_0); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSGS_CHANGED, 0i32 as uintptr_t, @@ -1542,7 +1537,7 @@ pub unsafe fn dc_delete_chat(mut context: *mut dc_context_t, mut chat_id: uint32 q3 = 0 as *mut libc::c_char; dc_sqlite3_commit((*context).sql); pending_transaction = 0i32; - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSGS_CHANGED, 0i32 as uintptr_t, @@ -1649,7 +1644,7 @@ pub unsafe fn dc_create_group_chat( dc_msg_unref(draft_msg); free(grpid as *mut libc::c_void); if 0 != chat_id { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSGS_CHANGED, 0i32 as uintptr_t, @@ -1786,14 +1781,14 @@ pub unsafe fn dc_add_contact_to_chat_ex( dc_param_set((*msg).param, 'E' as i32, (*contact).addr); dc_param_set_int((*msg).param, 'F' as i32, flags); (*msg).id = dc_send_msg(context, chat_id, msg); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSGS_CHANGED, chat_id as uintptr_t, (*msg).id as uintptr_t, ); } - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSGS_CHANGED, chat_id as uintptr_t, @@ -1929,7 +1924,7 @@ pub unsafe fn dc_remove_contact_from_chat( dc_param_set_int((*msg).param, 'S' as i32, 5i32); dc_param_set((*msg).param, 'E' as i32, (*contact).addr); (*msg).id = dc_send_msg(context, chat_id, msg); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSGS_CHANGED, chat_id as uintptr_t, @@ -1944,7 +1939,7 @@ pub unsafe fn dc_remove_contact_from_chat( contact_id, ); if !(0 == dc_sqlite3_execute((*context).sql, q3)) { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::CHAT_MODIFIED, chat_id as uintptr_t, @@ -2038,14 +2033,14 @@ pub unsafe fn dc_set_chat_name( dc_param_set_int((*msg).param, 'S' as i32, 2i32); dc_param_set((*msg).param, 'E' as i32, (*chat).name); (*msg).id = dc_send_msg(context, chat_id, msg); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSGS_CHANGED, chat_id as uintptr_t, (*msg).id as uintptr_t, ); } - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::CHAT_MODIFIED, chat_id as uintptr_t, @@ -2119,14 +2114,14 @@ pub unsafe fn dc_set_chat_profile_image( 1i32 as uint32_t, ); (*msg).id = dc_send_msg(context, chat_id, msg); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSGS_CHANGED, chat_id as uintptr_t, (*msg).id as uintptr_t, ); } - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::CHAT_MODIFIED, chat_id as uintptr_t, @@ -2254,7 +2249,7 @@ pub unsafe fn dc_forward_msgs( let mut i = 0u32; let mut icnt = carray_count(created_db_entries); while i < icnt { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSGS_CHANGED, carray_get(created_db_entries, i) as uintptr_t, @@ -2502,7 +2497,7 @@ pub unsafe fn dc_add_device_msg( b"rfc724_mid\x00" as *const u8 as *const libc::c_char, rfc724_mid, ); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSGS_CHANGED, chat_id as uintptr_t, diff --git a/src/dc_configure.rs b/src/dc_configure.rs index f686a9e55..4c3a76729 100644 --- a/src/dc_configure.rs +++ b/src/dc_configure.rs @@ -154,7 +154,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: *mut dc_context_t, _job: b"Configure ...\x00" as *const u8 as *const libc::c_char, ); if !(0 != (*context).shall_stop_ongoing) { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::CONFIGURE_PROGRESS, (if 0i32 < 1i32 { @@ -188,7 +188,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: *mut dc_context_t, _job: if 0 != (*context).shall_stop_ongoing { current_block = 2927484062889439186; } else { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::CONFIGURE_PROGRESS, (if 10i32 < 1i32 { @@ -214,7 +214,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: *mut dc_context_t, _job: if 0 != (*context).shall_stop_ongoing { current_block = 2927484062889439186; } else { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::CONFIGURE_PROGRESS, (if 20i32 < 1i32 { @@ -252,7 +252,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: *mut dc_context_t, _job: (*param).mail_pw = dc_strdup(0 as *const libc::c_char) } if !(0 != (*context).shall_stop_ongoing) { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::CONFIGURE_PROGRESS, (if 200i32 < 1i32 { @@ -296,9 +296,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: *mut dc_context_t, _job: if 0 != (*context).shall_stop_ongoing { current_block = 2927484062889439186; } else { - (*context) - .cb - .expect("non-null function pointer")( + ((*context).cb)( context, Event::CONFIGURE_PROGRESS, (if 300i32 < 1i32 { @@ -337,9 +335,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: *mut dc_context_t, _job: if 0 != (*context).shall_stop_ongoing { current_block = 2927484062889439186; } else { - (*context).cb.expect( - "non-null function pointer", - )( + ((*context).cb)( context, Event::CONFIGURE_PROGRESS, (if 310i32 < 1i32 { @@ -407,9 +403,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: *mut dc_context_t, _job: 2927484062889439186; break; } - (*context).cb.expect( - "non-null function pointer", - )( + ((*context).cb)( context, Event::CONFIGURE_PROGRESS, (if 320i32 + i * 10i32 @@ -456,7 +450,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: *mut dc_context_t, _job: current_block = 2927484062889439186; } else { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::CONFIGURE_PROGRESS, (if 340i32 @@ -517,7 +511,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: *mut dc_context_t, _job: = 2927484062889439186; } else { - (*context).cb.expect("non-null function pointer")(context, + ((*context).cb)(context, Event::CONFIGURE_PROGRESS, (if 350i32 < @@ -583,7 +577,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: *mut dc_context_t, _job: = 2927484062889439186; } else { - (*context).cb.expect("non-null function pointer")(context, + ((*context).cb)(context, Event::CONFIGURE_PROGRESS, (if 500i32 < @@ -809,9 +803,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: *mut dc_context_t, _job: as *const libc::c_char, ); } else if !(0 != (*context).shall_stop_ongoing) { - (*context) - .cb - .expect("non-null function pointer")( + ((*context).cb)( context, Event::CONFIGURE_PROGRESS, (if 600i32 < 1i32 { @@ -858,9 +850,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: *mut dc_context_t, _job: current_block = 2927484062889439186; break; } - (*context) - .cb - .expect("non-null function pointer")( + ((*context).cb)( context, Event::CONFIGURE_PROGRESS, (if 650i32 + username_variation * 30i32 @@ -903,9 +893,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: *mut dc_context_t, _job: current_block = 2927484062889439186; break; } - (*context) - .cb - .expect("non-null function pointer")( + ((*context).cb)( context, Event::CONFIGURE_PROGRESS, (if 660i32 + username_variation * 30i32 @@ -950,9 +938,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: *mut dc_context_t, _job: current_block = 2927484062889439186; break; } - (*context) - .cb - .expect("non-null function pointer")( + ((*context).cb)( context, Event::CONFIGURE_PROGRESS, (if 670i32 + username_variation * 30i32 @@ -991,9 +977,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: *mut dc_context_t, _job: imap_connected_here = 1i32; if !(0 != (*context).shall_stop_ongoing) { - (*context).cb.expect( - "non-null function pointer", - )( + ((*context).cb)( context, Event::CONFIGURE_PROGRESS, (if 800i32 < 1i32 { @@ -1021,7 +1005,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: *mut dc_context_t, _job: current_block = 2927484062889439186; } else { - (*context).cb.expect("non-null function pointer")(context, + ((*context).cb)(context, Event::CONFIGURE_PROGRESS, (if 850i32 < @@ -1073,7 +1057,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: *mut dc_context_t, _job: = 2927484062889439186; } else { - (*context).cb.expect("non-null function pointer")(context, + ((*context).cb)(context, Event::CONFIGURE_PROGRESS, (if 860i32 < @@ -1151,7 +1135,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: *mut dc_context_t, _job: != (*context) .shall_stop_ongoing) { - (*context).cb.expect("non-null function pointer")(context, + ((*context).cb)(context, Event::CONFIGURE_PROGRESS, (if 900i32 < @@ -1205,7 +1189,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: *mut dc_context_t, _job: if !(0 != (*context) .shall_stop_ongoing) { - (*context).cb.expect("non-null function pointer")(context, + ((*context).cb)(context, Event::CONFIGURE_PROGRESS, (if 910i32 < @@ -1243,7 +1227,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: *mut dc_context_t, _job: != (*context).shall_stop_ongoing) { - (*context).cb.expect("non-null function pointer")(context, + ((*context).cb)(context, Event::CONFIGURE_PROGRESS, (if 920i32 < @@ -1278,7 +1262,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: *mut dc_context_t, _job: != (*context).shall_stop_ongoing) { - (*context).cb.expect("non-null function pointer")(context, + ((*context).cb)(context, Event::CONFIGURE_PROGRESS, (if 940i32 < @@ -1332,7 +1316,7 @@ pub unsafe fn dc_job_do_DC_JOB_CONFIGURE_IMAP(context: *mut dc_context_t, _job: dc_free_ongoing(context); } free(mvbox_folder as *mut libc::c_void); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::CONFIGURE_PROGRESS, (if 0 != success { 1000i32 } else { 0i32 }) as uintptr_t, @@ -1886,7 +1870,7 @@ unsafe fn read_autoconf_file( b"Testing %s ...\x00" as *const u8 as *const libc::c_char, url, ); - filecontent = (*context).cb.expect("non-null function pointer")( + filecontent = ((*context).cb)( context, Event::HTTP_GET, url as uintptr_t, diff --git a/src/dc_contact.rs b/src/dc_contact.rs index d85ddebae..f61967833 100644 --- a/src/dc_contact.rs +++ b/src/dc_contact.rs @@ -42,7 +42,7 @@ pub unsafe fn dc_marknoticed_contact(mut context: *mut dc_context_t, mut contact sqlite3_bind_int(stmt, 1i32, contact_id as libc::c_int); sqlite3_step(stmt); sqlite3_finalize(stmt); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSGS_CHANGED, 0i32 as uintptr_t, @@ -144,7 +144,7 @@ pub unsafe fn dc_create_contact( { contact_id = dc_add_or_lookup_contact(context, name, addr, 0x4000000i32, &mut sth_modified); blocked = dc_is_contact_blocked(context, contact_id); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::CONTACTS_CHANGED, (if sth_modified == 2i32 { @@ -211,7 +211,7 @@ pub unsafe fn dc_block_contact( 5249903830285462583 => {} _ => { if 0 != send_event { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::CONTACTS_CHANGED, 0i32 as uintptr_t, @@ -572,7 +572,7 @@ pub unsafe fn dc_add_address_book( } dc_sqlite3_commit((*context).sql); if 0 != modify_cnt { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::CONTACTS_CHANGED, 0i32 as uintptr_t, @@ -929,7 +929,7 @@ pub unsafe fn dc_delete_contact( ); sqlite3_bind_int(stmt, 1i32, contact_id as libc::c_int); if !(sqlite3_step(stmt) != 101i32) { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::CONTACTS_CHANGED, 0i32 as uintptr_t, diff --git a/src/dc_context.rs b/src/dc_context.rs index f5912eb46..ecd85956d 100644 --- a/src/dc_context.rs +++ b/src/dc_context.rs @@ -1,6 +1,6 @@ use libc; -use crate::constants::{Event, VERSION}; +use crate::constants::VERSION; use crate::dc_array::*; use crate::dc_chat::*; use crate::dc_contact::*; @@ -127,10 +127,10 @@ pub unsafe fn dc_context_new( ); (*context).magic = 0x11a11807i32 as uint32_t; (*context).userdata = userdata; - (*context).cb = if cb.is_some() { cb } else { Some(cb_dummy) }; + (*context).cb = cb; (*context).os_name = dc_strdup_keep_null(os_name); (*context).shall_stop_ongoing = 1i32; - // dc_openssl_init(); + dc_pgp_init(); (*context).sql = dc_sqlite3_new(context); (*context).inbox = dc_imap_new( @@ -271,20 +271,6 @@ unsafe fn cb_get_config( let mut context: *mut dc_context_t = (*imap).userData as *mut dc_context_t; return dc_sqlite3_get_config((*context).sql, key, def); } -/* * - * A callback function that is used if no user-defined callback is given to dc_context_new(). - * The callback function simply returns 0 which is safe for every event. - * - * @private @memberof dc_context_t - */ -unsafe fn cb_dummy( - _context: *mut dc_context_t, - _event: Event, - _data1: uintptr_t, - _data2: uintptr_t, -) -> uintptr_t { - 0i32 as uintptr_t -} pub unsafe fn dc_context_unref(mut context: *mut dc_context_t) { if context.is_null() || (*context).magic != 0x11a11807i32 as libc::c_uint { diff --git a/src/dc_imap.rs b/src/dc_imap.rs index dfc086e69..caf40429f 100644 --- a/src/dc_imap.rs +++ b/src/dc_imap.rs @@ -1024,7 +1024,7 @@ unsafe fn peek_body( let mut flag: *mut mailimap_flag = (*flag_fetch).fl_flag; if !flag.is_null() { if (*flag).fl_type == MAILIMAP_FLAG_SEEN as libc::c_int { - *flags = (*flags as libc::c_long | 0x1i64) as uint32_t + *flags = (*flags as libc::c_long | 0x1) as uint32_t } else if (*flag).fl_type == MAILIMAP_FLAG_DELETED as libc::c_int { *deleted = 1i32 } diff --git a/src/dc_imex.rs b/src/dc_imex.rs index f9d69b285..f72850c8e 100644 --- a/src/dc_imex.rs +++ b/src/dc_imex.rs @@ -698,7 +698,7 @@ pub unsafe fn dc_job_do_DC_JOB_IMEX_IMAP(mut context: *mut dc_context_t, mut job 0i32, b"Import/export process started.\x00" as *const u8 as *const libc::c_char, ); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::IMEX_PROGRESS, 10i32 as uintptr_t, @@ -922,7 +922,7 @@ pub unsafe fn dc_job_do_DC_JOB_IMEX_IMAP(mut context: *mut dc_context_t, mut job if 0 != ongoing_allocated_here { dc_free_ongoing(context); } - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::IMEX_PROGRESS, (if 0 != success { 1000i32 } else { 0i32 }) as uintptr_t, @@ -1003,7 +1003,7 @@ unsafe fn import_backup( if permille > 990i32 { permille = 990i32 } - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::IMEX_PROGRESS, permille as uintptr_t, @@ -1215,7 +1215,7 @@ unsafe fn export_backup( if permille > 990i32 { permille = 990i32 } - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::IMEX_PROGRESS, permille as uintptr_t, @@ -1314,7 +1314,7 @@ unsafe fn export_backup( b"backup_time\x00" as *const u8 as *const libc::c_char, now as int32_t, ); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::IMEX_FILE_WRITTEN, dest_pathNfilename as uintptr_t, @@ -1570,7 +1570,7 @@ unsafe fn export_key_to_asc_file( file_name, ); } else { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::IMEX_FILE_WRITTEN, file_name as uintptr_t, diff --git a/src/dc_job.rs b/src/dc_job.rs index ad71ea057..b88ae293e 100644 --- a/src/dc_job.rs +++ b/src/dc_job.rs @@ -1,4 +1,5 @@ use libc; +use rand::{thread_rng, Rng}; use crate::constants::Event; use crate::dc_chat::*; @@ -257,7 +258,9 @@ unsafe fn get_backoff_time_offset(mut c_tries: libc::c_int) -> time_t { // results in ~3 weeks for the last backoff timespan let mut N: time_t = pow(2i32 as libc::c_double, (c_tries - 1i32) as libc::c_double) as time_t; N = N * 60i32 as libc::c_long; - let mut seconds: time_t = rand() as libc::c_long % (N + 1i32 as libc::c_long); + let mut rng = thread_rng(); + let n: libc::c_long = rng.gen(); + let mut seconds: time_t = n % (N + 1i32 as libc::c_long); if seconds < 1i32 as libc::c_long { seconds = 1i32 as time_t } @@ -404,7 +407,7 @@ unsafe extern "C" fn dc_job_do_DC_JOB_SEND(mut context: *mut dc_context_t, mut j } else { 0i32 }; - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSG_DELIVERED, chat_id as uintptr_t, diff --git a/src/dc_key.rs b/src/dc_key.rs index 20be35ea0..89923a3d9 100644 --- a/src/dc_key.rs +++ b/src/dc_key.rs @@ -21,16 +21,10 @@ pub struct dc_key_t { } #[inline] -#[cfg(target_os = "macos")] pub unsafe fn toupper(mut _c: libc::c_int) -> libc::c_int { return __toupper(_c); } -#[inline] -#[cfg(not(target_os = "macos"))] -pub unsafe fn toupper(mut _c: libc::c_int) -> libc::c_int { - return _toupper(_c); -} pub unsafe fn dc_key_new() -> *mut dc_key_t { let mut key: *mut dc_key_t = 0 as *mut dc_key_t; key = calloc(1, ::std::mem::size_of::()) as *mut dc_key_t; @@ -384,7 +378,7 @@ pub unsafe fn dc_render_base64( * Render keys ******************************************************************************/ unsafe fn crc_octets(mut octets: *const libc::c_uchar, mut len: size_t) -> libc::c_long { - let mut crc: libc::c_long = 0xb704cei64; + let mut crc: libc::c_long = 0xb704ce; loop { let fresh0 = len; len = len.wrapping_sub(1); @@ -397,13 +391,13 @@ unsafe fn crc_octets(mut octets: *const libc::c_uchar, mut len: size_t) -> libc: let mut i: libc::c_int = 0i32; while i < 8i32 { crc <<= 1i32; - if 0 != crc & 0x1000000i32 as libc::c_long { - crc ^= 0x1864cfbi64 + if 0 != crc & 0x1000000 as libc::c_long { + crc ^= 0x1864cfb } i += 1 } } - return crc & 0xffffffi64; + return crc & 0xffffff; } /* the result must be freed */ pub unsafe fn dc_key_render_base64( diff --git a/src/dc_location.rs b/src/dc_location.rs index 0c621002a..b3e1a56c1 100644 --- a/src/dc_location.rs +++ b/src/dc_location.rs @@ -102,7 +102,7 @@ pub unsafe fn dc_send_locations_to_chat( ); dc_add_device_msg(context, chat_id, stock_str); } - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::CHAT_MODIFIED, chat_id as uintptr_t, @@ -198,7 +198,7 @@ pub unsafe fn dc_set_location( continue_streaming = 1i32 } if 0 != continue_streaming { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::LOCATION_CHANGED, 1i32 as uintptr_t, @@ -302,7 +302,7 @@ pub unsafe fn dc_delete_all_locations(mut context: *mut dc_context_t) { b"DELETE FROM locations;\x00" as *const u8 as *const libc::c_char, ); sqlite3_step(stmt); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::LOCATION_CHANGED, 0i32 as uintptr_t, @@ -883,7 +883,7 @@ pub unsafe fn dc_job_do_DC_JOB_MAYBE_SEND_LOC_ENDED( 0i32 as uint32_t, ); dc_add_device_msg(context, chat_id, stock_str); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::CHAT_MODIFIED, chat_id as uintptr_t, diff --git a/src/dc_log.rs b/src/dc_log.rs index fc45a9308..426c85272 100644 --- a/src/dc_log.rs +++ b/src/dc_log.rs @@ -48,12 +48,7 @@ unsafe fn log_vprintf( event as libc::c_int, ) } - (*context).cb.expect("non-null function pointer")( - context, - event, - data1 as uintptr_t, - msg as uintptr_t, - ); + ((*context).cb)(context, event, data1 as uintptr_t, msg as uintptr_t); free(msg as *mut libc::c_void); } pub unsafe extern "C" fn dc_log_event_seq( diff --git a/src/dc_mimeparser.rs b/src/dc_mimeparser.rs index f28b4904a..7b1ad3038 100644 --- a/src/dc_mimeparser.rs +++ b/src/dc_mimeparser.rs @@ -1261,8 +1261,10 @@ unsafe fn dc_mimeparser_add_single_part_if_known( (*part).type_0 = 10i32; (*part).int_mimetype = mime_type; (*part).msg = simplified_txt; - (*part).msg_raw = - strndup(decoded_data, decoded_data_bytes as u64); + (*part).msg_raw = strndup( + decoded_data, + decoded_data_bytes as libc::c_ulong, + ); do_add_single_part(mimeparser, part); part = 0 as *mut dc_mimepart_t } else { diff --git a/src/dc_msg.rs b/src/dc_msg.rs index e26505351..8df57178e 100644 --- a/src/dc_msg.rs +++ b/src/dc_msg.rs @@ -653,7 +653,7 @@ pub unsafe fn dc_delete_msgs( } dc_sqlite3_commit((*context).sql); if 0 != msg_cnt { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSGS_CHANGED, 0i32 as uintptr_t, @@ -734,7 +734,7 @@ pub unsafe fn dc_markseen_msgs( dc_sqlite3_commit((*context).sql); transaction_pending = 0i32; if 0 != send_event { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSGS_CHANGED, 0i32 as uintptr_t, @@ -1325,7 +1325,7 @@ pub unsafe fn dc_set_msg_failed( sqlite3_bind_text(stmt, 2i32, (*(*msg).param).packed, -1i32, None); sqlite3_bind_int(stmt, 3i32, msg_id as libc::c_int); sqlite3_step(stmt); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSG_FAILED, (*msg).chat_id as uintptr_t, diff --git a/src/dc_oauth2.rs b/src/dc_oauth2.rs index dbe8cb5df..5b2c97b86 100644 --- a/src/dc_oauth2.rs +++ b/src/dc_oauth2.rs @@ -253,7 +253,7 @@ pub unsafe fn dc_get_oauth2_access_token( b"$REFRESH_TOKEN\x00" as *const u8 as *const libc::c_char, refresh_token, ); - json = (*context).cb.expect("non-null function pointer")( + json = ((*context).cb)( context, Event::HTTP_POST, token_url as uintptr_t, @@ -554,7 +554,7 @@ unsafe fn get_oauth2_addr( b"$ACCESS_TOKEN\x00" as *const u8 as *const libc::c_char, access_token, ); - json = (*context).cb.expect("non-null function pointer")( + json = ((*context).cb)( context, Event::HTTP_GET, userinfo_url as uintptr_t, diff --git a/src/dc_receive_imf.rs b/src/dc_receive_imf.rs index 49af72ed0..529df8ae7 100644 --- a/src/dc_receive_imf.rs +++ b/src/dc_receive_imf.rs @@ -267,7 +267,7 @@ pub unsafe fn dc_receive_imf( } } if 0 != incoming { - state = if 0 != flags as libc::c_long & 0x1i64 { + state = if 0 != flags as libc::c_long & 0x1 { 16i32 } else { 10i32 @@ -456,7 +456,7 @@ pub unsafe fn dc_receive_imf( chat_id, from_id, sent_timestamp, - if 0 != flags as libc::c_long & 0x1i64 { + if 0 != flags as libc::c_long & 0x1 { 0i32 } else { 1i32 @@ -967,7 +967,7 @@ pub unsafe fn dc_receive_imf( dc_contact_unref(contact); } if send_event { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::LOCATION_CHANGED, from_id as uintptr_t, @@ -1008,7 +1008,7 @@ pub unsafe fn dc_receive_imf( let mut icnt_0: size_t = carray_count(created_db_entries) as size_t; i_0 = 0i32 as size_t; while i_0 < icnt_0 { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, create_event_to_send, carray_get(created_db_entries, i_0 as libc::c_uint) as uintptr_t, @@ -1025,7 +1025,7 @@ pub unsafe fn dc_receive_imf( let mut icnt_1: size_t = carray_count(rr_event_to_send) as size_t; i_1 = 0i32 as size_t; while i_1 < icnt_1 { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::MSG_READ, carray_get(rr_event_to_send, i_1 as libc::c_uint) as uintptr_t, @@ -1407,7 +1407,7 @@ unsafe fn create_or_lookup_group( sqlite3_bind_int(stmt, 2i32, chat_id as libc::c_int); sqlite3_step(stmt); sqlite3_finalize(stmt); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::CHAT_MODIFIED, chat_id as uintptr_t, @@ -1510,7 +1510,7 @@ unsafe fn create_or_lookup_group( dc_reset_gossiped_timestamp(context, chat_id); } if 0 != send_EVENT_CHAT_MODIFIED { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::CHAT_MODIFIED, chat_id as uintptr_t, @@ -1646,7 +1646,7 @@ unsafe fn create_or_lookup_adhoc_group( ); i += 1 } - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::CHAT_MODIFIED, chat_id as uintptr_t, diff --git a/src/dc_securejoin.rs b/src/dc_securejoin.rs index 61dc5cd29..9a8525884 100644 --- a/src/dc_securejoin.rs +++ b/src/dc_securejoin.rs @@ -217,7 +217,7 @@ pub unsafe fn dc_join_securejoin( b"Taking protocol shortcut.\x00" as *const u8 as *const libc::c_char, ); (*context).bob_expects = 6i32; - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::SECUREJOIN_JOINER_PROGRESS, chat_id_2_contact_id(context, contact_chat_id) as uintptr_t, @@ -449,7 +449,7 @@ pub unsafe fn dc_handle_securejoin_handshake( 0i32, b"Secure-join requested.\x00" as *const u8 as *const libc::c_char, ); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::SECUREJOIN_INVITER_PROGRESS, contact_id as uintptr_t, @@ -537,7 +537,7 @@ pub unsafe fn dc_handle_securejoin_handshake( b"Fingerprint verified.\x00" as *const u8 as *const libc::c_char, ); own_fingerprint = get_self_fingerprint(context); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::SECUREJOIN_JOINER_PROGRESS, contact_id as uintptr_t, @@ -643,13 +643,13 @@ pub unsafe fn dc_handle_securejoin_handshake( b"Auth verified.\x00" as *const u8 as *const libc::c_char, ); secure_connection_established(context, contact_chat_id); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::CONTACTS_CHANGED, contact_id as uintptr_t, 0i32 as uintptr_t, ); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::SECUREJOIN_INVITER_PROGRESS, contact_id as uintptr_t, @@ -692,7 +692,7 @@ pub unsafe fn dc_handle_securejoin_handshake( 0 as *const libc::c_char, 0 as *const libc::c_char, ); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::SECUREJOIN_INVITER_PROGRESS, contact_id as uintptr_t, @@ -788,7 +788,7 @@ pub unsafe fn dc_handle_securejoin_handshake( current_block = 4378276786830486580; } else { dc_scaleup_contact_origin(context, contact_id, 0x2000000i32); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::CONTACTS_CHANGED, 0i32 as uintptr_t, @@ -858,13 +858,13 @@ pub unsafe fn dc_handle_securejoin_handshake( ); current_block = 4378276786830486580; } else { - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::SECUREJOIN_INVITER_PROGRESS, contact_id as uintptr_t, 800i32 as uintptr_t, ); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::SECUREJOIN_INVITER_PROGRESS, contact_id as uintptr_t, @@ -916,7 +916,7 @@ unsafe fn secure_connection_established( }, ); dc_add_device_msg(context, contact_chat_id, msg); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::CHAT_MODIFIED, contact_chat_id as uintptr_t, @@ -1070,7 +1070,7 @@ pub unsafe fn dc_handle_degrade_event( dc_stock_str_repl_string(context, 37i32, (*peerstate).addr); dc_add_device_msg(context, contact_chat_id, msg); free(msg as *mut libc::c_void); - (*context).cb.expect("non-null function pointer")( + ((*context).cb)( context, Event::CHAT_MODIFIED, contact_chat_id as uintptr_t, diff --git a/src/dc_sqlite3.rs b/src/dc_sqlite3.rs index 8782da06b..0c53c1d01 100644 --- a/src/dc_sqlite3.rs +++ b/src/dc_sqlite3.rs @@ -1222,7 +1222,6 @@ pub unsafe fn dc_sqlite3_rollback(_sql: *mut dc_sqlite3_t) {} /* housekeeping */ pub unsafe fn dc_housekeeping(mut context: *mut dc_context_t) { - let mut keep_files_newer_than: time_t = 0; let mut stmt: *mut sqlite3_stmt = 0 as *mut sqlite3_stmt; let mut dir_handle: *mut DIR = 0 as *mut DIR; let mut dir_entry: *mut dirent = 0 as *mut dirent; @@ -1294,7 +1293,9 @@ pub unsafe fn dc_housekeeping(mut context: *mut dc_context_t) { ); } else { /* avoid deletion of files that are just created to build a message object */ - keep_files_newer_than = time(0 as *mut time_t) - (60i32 * 60i32) as libc::c_long; + let diff = std::time::Duration::from_secs(60 * 60); + let keep_files_newer_than = std::time::SystemTime::now().checked_sub(diff).unwrap(); + loop { dir_entry = readdir(dir_handle); if dir_entry.is_null() { @@ -1336,45 +1337,25 @@ pub unsafe fn dc_housekeeping(mut context: *mut dc_context_t) { (*context).blobdir, name, ); - let mut st: stat = stat { - st_dev: 0, - st_mode: 0, - st_nlink: 0, - st_ino: 0, - st_uid: 0, - st_gid: 0, - st_rdev: 0, - st_atime: 0, - st_atime_nsec: 0, - st_mtime: 0, - st_mtime_nsec: 0, - st_ctime: 0, - st_ctime_nsec: 0, - st_birthtime: 0, - st_birthtime_nsec: 0, - st_size: 0, - st_blocks: 0, - st_blksize: 0, - st_flags: 0, - st_gen: 0, - st_lspare: 0, - st_qspare: [0; 2], - }; - if stat(path, &mut st) == 0i32 { - if st.st_mtime > keep_files_newer_than - || st.st_atime > keep_files_newer_than - || st.st_ctime > keep_files_newer_than - { - dc_log_info( - context, - 0i32, - b"Housekeeping: Keeping new unreferenced file #%i: %s\x00" as *const u8 - as *const libc::c_char, - unreferenced_count, - name, - ); - continue; + + match std::fs::metadata(std::ffi::CStr::from_ptr(path).to_str().unwrap()) { + Ok(stats) => { + if stats.created().unwrap() > keep_files_newer_than + || stats.modified().unwrap() > keep_files_newer_than + || stats.accessed().unwrap() > keep_files_newer_than + { + dc_log_info( + context, + 0i32, + b"Housekeeping: Keeping new unreferenced file #%i: %s\x00" as *const u8 + as *const libc::c_char, + unreferenced_count, + name, + ); + continue; + } } + Err(_) => {} } dc_log_info( context, diff --git a/src/dc_stock.rs b/src/dc_stock.rs index dc080fd30..b93ce0e2a 100644 --- a/src/dc_stock.rs +++ b/src/dc_stock.rs @@ -22,7 +22,7 @@ unsafe fn get_string( ) -> *mut libc::c_char { let mut ret: *mut libc::c_char = 0 as *mut libc::c_char; if !context.is_null() { - ret = (*context).cb.expect("non-null function pointer")( + ret = ((*context).cb)( context, Event::GET_STRING, id as uintptr_t, diff --git a/src/dc_strencode.rs b/src/dc_strencode.rs index 3a878a495..2bee4ebc8 100644 --- a/src/dc_strencode.rs +++ b/src/dc_strencode.rs @@ -428,7 +428,7 @@ pub unsafe fn dc_encode_modified_utf7( if c < 0x80i32 as libc::c_uint { ucs4 = c as libc::c_ulong } else if 0 != utf8total { - ucs4 = ucs4 << 6i32 | c as libc::c_ulong & 0x3fu64; + ucs4 = ucs4 << 6i32 | c as libc::c_ulong & 0x3f; utf8pos = utf8pos.wrapping_add(1); if utf8pos < utf8total { continue; @@ -449,13 +449,13 @@ pub unsafe fn dc_encode_modified_utf7( } utf8total = 0i32 as libc::c_uint; loop { - if ucs4 >= 0x10000u64 { - ucs4 = ucs4.wrapping_sub(0x10000u64); - bitbuf = bitbuf << 16i32 | (ucs4 >> 10i32).wrapping_add(0xd800u64); - ucs4 = (ucs4 & 0x3ffu64).wrapping_add(0xdc00u64); + if ucs4 >= 0x10000 { + ucs4 = ucs4.wrapping_sub(0x10000); + bitbuf = bitbuf << 16 | (ucs4 >> 10).wrapping_add(0xd800); + ucs4 = (ucs4 & 0x3ff).wrapping_add(0xdc00); utf16flag = 1i32 as libc::c_uint } else { - bitbuf = bitbuf << 16i32 | ucs4; + bitbuf = bitbuf << 16 | ucs4; utf16flag = 0i32 as libc::c_uint } bitstogo = bitstogo.wrapping_add(16i32 as libc::c_uint); @@ -570,24 +570,24 @@ pub unsafe fn dc_decode_modified_utf7( } else { bitbuf } & 0xffff; + // convert UTF16 to UCS4 - if utf16 >= 0xd800u64 && utf16 <= 0xdbffu64 { - ucs4 = utf16.wrapping_sub(0xd800u64) << 10i32 + if utf16 >= 0xd800 && utf16 <= 0xdbff { + ucs4 = utf16.wrapping_sub(0xd800) << 10i32 } else { - if utf16 >= 0xdc00u64 && utf16 <= 0xdfffu64 { - ucs4 = ucs4 - .wrapping_add(utf16.wrapping_sub(0xdc00u64).wrapping_add(0x10000u64)) + if utf16 >= 0xdc00 && utf16 <= 0xdfff { + ucs4 = ucs4.wrapping_add(utf16.wrapping_sub(0xdc00).wrapping_add(0x10000)) } else { ucs4 = utf16 } - if ucs4 <= 0x7fu64 { + if ucs4 <= 0x7f { *dst.offset(0isize) = ucs4 as libc::c_char; dst = dst.offset(1isize) - } else if ucs4 <= 0x7ffu64 { + } else if ucs4 <= 0x7ff { *dst.offset(0isize) = (0xc0 | ucs4 >> 6i32) as libc::c_char; *dst.offset(1isize) = (0x80 | ucs4 & 0x3f) as libc::c_char; dst = dst.offset(2isize) - } else if ucs4 <= 0xffffu64 { + } else if ucs4 <= 0xffff { *dst.offset(0isize) = (0xe0 | ucs4 >> 12i32) as libc::c_char; *dst.offset(1isize) = (0x80 | ucs4 >> 6i32 & 0x3f) as libc::c_char; *dst.offset(2isize) = (0x80 | ucs4 & 0x3f) as libc::c_char; diff --git a/src/dc_tools.rs b/src/dc_tools.rs index 3dd13b389..a3efe57bf 100644 --- a/src/dc_tools.rs +++ b/src/dc_tools.rs @@ -1221,7 +1221,7 @@ pub unsafe fn dc_get_filemeta( + ((*buf.offset((pos + 2) as isize) as libc::c_int) << 8i32) + *buf.offset((pos + 3) as isize) as libc::c_int) as libc::c_long; - if (pos + 12) > buf_bytes as i64 { + if (pos + 12) > buf_bytes as libc::c_long { break; } } @@ -1304,128 +1304,38 @@ pub unsafe fn dc_file_exist( mut context: *mut dc_context_t, mut pathNfilename: *const libc::c_char, ) -> libc::c_int { - let mut st: stat = stat { - st_dev: 0, - st_mode: 0, - st_nlink: 0, - st_ino: 0, - st_uid: 0, - st_gid: 0, - st_rdev: 0, - st_atime: 0, - st_atime_nsec: 0, - st_mtime: 0, - st_mtime_nsec: 0, - st_ctime: 0, - st_ctime_nsec: 0, - st_birthtime: 0, - st_birthtime_nsec: 0, - st_size: 0, - st_blocks: 0, - st_blksize: 0, - st_flags: 0, - st_gen: 0, - st_lspare: 0, - st_qspare: [0; 2], + let pathNfilename_abs = dc_get_abs_path(context, pathNfilename); + let exist = { + let p = std::path::Path::new( + std::ffi::CStr::from_ptr(pathNfilename_abs) + .to_str() + .unwrap(), + ); + p.exists() }; - let mut exist: libc::c_int = 0i32; - let mut pathNfilename_abs: *mut libc::c_char = 0 as *mut libc::c_char; - pathNfilename_abs = dc_get_abs_path(context, pathNfilename); - if !pathNfilename_abs.is_null() { - st = stat { - st_dev: 0, - st_mode: 0, - st_nlink: 0, - st_ino: 0, - st_uid: 0, - st_gid: 0, - st_rdev: 0, - st_atime: 0, - st_atime_nsec: 0, - st_mtime: 0, - st_mtime_nsec: 0, - st_ctime: 0, - st_ctime_nsec: 0, - st_birthtime: 0, - st_birthtime_nsec: 0, - st_size: 0, - st_blocks: 0, - st_blksize: 0, - st_flags: 0, - st_gen: 0, - st_lspare: 0, - st_qspare: [0; 2], - }; - if stat(pathNfilename_abs, &mut st) == 0i32 { - exist = 1i32 - } - } + free(pathNfilename_abs as *mut libc::c_void); - return exist; + + exist as libc::c_int } + pub unsafe fn dc_get_filebytes( mut context: *mut dc_context_t, mut pathNfilename: *const libc::c_char, ) -> uint64_t { - let mut st: stat = stat { - st_dev: 0, - st_mode: 0, - st_nlink: 0, - st_ino: 0, - st_uid: 0, - st_gid: 0, - st_rdev: 0, - st_atime: 0, - st_atime_nsec: 0, - st_mtime: 0, - st_mtime_nsec: 0, - st_ctime: 0, - st_ctime_nsec: 0, - st_birthtime: 0, - st_birthtime_nsec: 0, - st_size: 0, - st_blocks: 0, - st_blksize: 0, - st_flags: 0, - st_gen: 0, - st_lspare: 0, - st_qspare: [0; 2], + let pathNfilename_abs = dc_get_abs_path(context, pathNfilename); + + let filebytes = { + let p = std::ffi::CStr::from_ptr(pathNfilename_abs) + .to_str() + .unwrap(); + std::fs::metadata(p).unwrap().len() }; - let mut filebytes: uint64_t = 0i32 as uint64_t; - let mut pathNfilename_abs: *mut libc::c_char = 0 as *mut libc::c_char; - pathNfilename_abs = dc_get_abs_path(context, pathNfilename); - if !pathNfilename_abs.is_null() { - st = stat { - st_dev: 0, - st_mode: 0, - st_nlink: 0, - st_ino: 0, - st_uid: 0, - st_gid: 0, - st_rdev: 0, - st_atime: 0, - st_atime_nsec: 0, - st_mtime: 0, - st_mtime_nsec: 0, - st_ctime: 0, - st_ctime_nsec: 0, - st_birthtime: 0, - st_birthtime_nsec: 0, - st_size: 0, - st_blocks: 0, - st_blksize: 0, - st_flags: 0, - st_gen: 0, - st_lspare: 0, - st_qspare: [0; 2], - }; - if stat(pathNfilename_abs, &mut st) == 0i32 { - filebytes = st.st_size as uint64_t - } - } + free(pathNfilename_abs as *mut libc::c_void); - return filebytes; + filebytes as uint64_t } + pub unsafe fn dc_delete_file( mut context: *mut dc_context_t, mut pathNfilename: *const libc::c_char, @@ -1548,60 +1458,15 @@ pub unsafe fn dc_create_folder( mut context: *mut dc_context_t, mut pathNfilename: *const libc::c_char, ) -> libc::c_int { - let mut st: stat = stat { - st_dev: 0, - st_mode: 0, - st_nlink: 0, - st_ino: 0, - st_uid: 0, - st_gid: 0, - st_rdev: 0, - st_atime: 0, - st_atime_nsec: 0, - st_mtime: 0, - st_mtime_nsec: 0, - st_ctime: 0, - st_ctime_nsec: 0, - st_birthtime: 0, - st_birthtime_nsec: 0, - st_size: 0, - st_blocks: 0, - st_blksize: 0, - st_flags: 0, - st_gen: 0, - st_lspare: 0, - st_qspare: [0; 2], - }; - let mut current_block: u64; - let mut success: libc::c_int = 0i32; - let mut pathNfilename_abs: *mut libc::c_char = 0 as *mut libc::c_char; - pathNfilename_abs = dc_get_abs_path(context, pathNfilename); - if !pathNfilename_abs.is_null() { - st = stat { - st_dev: 0, - st_mode: 0, - st_nlink: 0, - st_ino: 0, - st_uid: 0, - st_gid: 0, - st_rdev: 0, - st_atime: 0, - st_atime_nsec: 0, - st_mtime: 0, - st_mtime_nsec: 0, - st_ctime: 0, - st_ctime_nsec: 0, - st_birthtime: 0, - st_birthtime_nsec: 0, - st_size: 0, - st_blocks: 0, - st_blksize: 0, - st_flags: 0, - st_gen: 0, - st_lspare: 0, - st_qspare: [0; 2], - }; - if stat(pathNfilename_abs, &mut st) == -1i32 { + let mut success = 0; + let pathNfilename_abs = dc_get_abs_path(context, pathNfilename); + { + let p = std::path::Path::new( + std::ffi::CStr::from_ptr(pathNfilename_abs) + .to_str() + .unwrap(), + ); + if !p.exists() { if mkdir(pathNfilename_abs, 0o755i32 as mode_t) != 0i32 { dc_log_warning( context, @@ -1609,21 +1474,18 @@ pub unsafe fn dc_create_folder( b"Cannot create directory \"%s\".\x00" as *const u8 as *const libc::c_char, pathNfilename, ); - current_block = 7696101774396965466; } else { - current_block = 7815301370352969686; + success = 1; } } else { - current_block = 7815301370352969686; - } - match current_block { - 7696101774396965466 => {} - _ => success = 1i32, + success = 1; } } + free(pathNfilename_abs as *mut libc::c_void); - return success; + success } + pub unsafe fn dc_write_file( mut context: *mut dc_context_t, mut pathNfilename: *const libc::c_char, diff --git a/src/types.rs b/src/types.rs index 2ebe11621..a1df7cd7d 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1231,17 +1231,10 @@ pub unsafe fn isspace(mut _c: libc::c_int) -> libc::c_int { } #[inline] -#[cfg(target_os = "macos")] pub unsafe fn tolower(mut _c: libc::c_int) -> libc::c_int { return __tolower(_c); } -#[inline] -#[cfg(not(target_os = "macos"))] -pub unsafe fn tolower(mut _c: libc::c_int) -> libc::c_int { - return _tolower(_c); -} - #[inline] pub unsafe fn carray_count(mut array: *mut carray) -> libc::c_uint { return (*array).len; @@ -1263,7 +1256,7 @@ pub unsafe fn carray_get(mut array: *mut carray, mut indx: libc::c_uint) -> *mut * @return return 0 unless stated otherwise in the event parameter documentation */ pub type dc_callback_t = - Option uintptr_t>; + unsafe extern "C" fn(_: *mut dc_context_t, _: Event, _: uintptr_t, _: uintptr_t) -> uintptr_t; pub const DC_MOVE_STATE_MOVING: libc::c_uint = 3; pub const DC_MOVE_STATE_STAY: libc::c_uint = 2; diff --git a/src/x.rs b/src/x.rs index d5059b08e..bf4918069 100644 --- a/src/x.rs +++ b/src/x.rs @@ -4,12 +4,12 @@ use crate::dc_strbuilder::dc_strbuilder_t; use crate::types::*; pub use libc::{ - atof, atoi, calloc, close, closedir, exit, fclose, fgets, fopen, fread, free, fseek, ftell, - fwrite, gmtime, gmtime_r, localtime, localtime_r, malloc, memcmp, memcpy, memmove, memset, - mkdir, open, opendir, printf, rand, read, readdir, realloc, remove, sleep, snprintf, sprintf, - sscanf, stat, strcasecmp, strcat, strchr, strcmp, strcpy, strcspn, strdup, strlen, strncasecmp, - strncmp, strncpy, strrchr, strspn, strstr, strtol, system, time, tolower as __tolower, - toupper as __toupper, usleep, write, + atoi, calloc, close, closedir, exit, fclose, fgets, fopen, fread, free, fseek, ftell, fwrite, + gmtime, gmtime_r, localtime, localtime_r, malloc, memcmp, memcpy, memmove, memset, mkdir, open, + opendir, printf, read, readdir, realloc, remove, sleep, snprintf, sprintf, sscanf, strcasecmp, + strcat, strchr, strcmp, strcpy, strcspn, strdup, strlen, strncasecmp, strncmp, strncpy, + strrchr, strspn, strstr, strtol, system, time, tolower as __tolower, toupper as __toupper, + usleep, write, }; extern "C" { @@ -543,3 +543,11 @@ pub unsafe extern "C" fn __assert_rtn( ) -> ! { __assert(a, b, c, d) } + +#[cfg(not(target_os = "android"))] +pub use libc::atof; + +#[cfg(target_os = "android")] +pub unsafe fn atof(nptr: *mut libc::c_char) -> libc::c_double { + libc::strtod(nptr, std::ptr::null_mut()) +}