Skip to content

Commit

Permalink
Merge branch 'refactor/remove-mprintf'
Browse files Browse the repository at this point in the history
  • Loading branch information
hpk42 committed Sep 17, 2019
2 parents 8c2c3f8 + 2b038a3 commit b680319
Show file tree
Hide file tree
Showing 16 changed files with 552 additions and 897 deletions.
448 changes: 223 additions & 225 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ authors = ["dignifiedquire <[email protected]>"]
edition = "2018"
license = "MPL"

[build-dependencies]
cc = "1.0.35"
pkg-config = "0.3"

[dependencies]
deltachat_derive = { path = "./deltachat_derive" }
libc = "0.2.51"
Expand Down
38 changes: 0 additions & 38 deletions build.rs

This file was deleted.

10 changes: 3 additions & 7 deletions examples/repl/cmdline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,18 +510,14 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
}
"export-setup" => {
let setup_code = dc_create_setup_code(context);
let file_name: *mut libc::c_char = dc_mprintf(
b"%s/autocrypt-setup-message.html\x00" as *const u8 as *const libc::c_char,
context.get_blobdir(),
);
let file_name = context.get_blobdir().join("autocrypt-setup-message.html");
let file_content = dc_render_setup_file(context, &setup_code)?;
std::fs::write(as_str(file_name), file_content)?;
std::fs::write(&file_name, file_content)?;
println!(
"Setup message written to: {}\nSetup code: {}",
as_str(file_name),
file_name.display(),
&setup_code,
);
free(file_name as *mut libc::c_void);
}
"poke" => {
ensure!(0 != poke_spec(context, arg1_c), "Poke failed");
Expand Down
52 changes: 0 additions & 52 deletions misc.c

This file was deleted.

1 change: 0 additions & 1 deletion misc.h

This file was deleted.

128 changes: 47 additions & 81 deletions src/dc_imex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ 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) {
return std::ptr::null_mut();
Expand All @@ -115,8 +114,7 @@ pub unsafe fn dc_initiate_key_transfer(context: &Context) -> *mut libc::c_char {
.unwrap()
.shall_stop_ongoing
{
if let Ok(setup_file_content) = dc_render_setup_file(context, &setup_code) {
let setup_file_content_c = CString::yolo(setup_file_content.as_str());
if let Ok(ref setup_file_content) = dc_render_setup_file(context, &setup_code) {
/* encrypting may also take a while ... */
if !context
.running_state
Expand All @@ -125,23 +123,14 @@ pub unsafe fn dc_initiate_key_transfer(context: &Context) -> *mut libc::c_char {
.unwrap()
.shall_stop_ongoing
{
setup_file_name = dc_get_fine_pathNfilename(
context,
b"$BLOBDIR\x00" as *const u8 as *const libc::c_char,
b"autocrypt-setup-message.html\x00" as *const u8 as *const libc::c_char,
);
if !(setup_file_name.is_null()
|| 0 == dc_write_file(
context,
setup_file_name,
setup_file_content_c.as_ptr() as *const libc::c_void,
setup_file_content_c.as_bytes().len(),
))
{
let setup_file_name =
dc_get_fine_path_filename(context, "$BLOBDIR", "autocrypt-setup-message.html");
if dc_write_file(context, &setup_file_name, setup_file_content.as_bytes()) {
if let Ok(chat_id) = chat::create_by_contact_id(context, 1) {
msg = dc_msg_new_untyped();
msg.type_0 = Viewtype::File;
msg.param.set(Param::File, as_str(setup_file_name));
msg.param
.set(Param::File, setup_file_name.to_string_lossy());

msg.param
.set(Param::MimeType, "application/autocrypt-setup");
Expand Down Expand Up @@ -182,7 +171,6 @@ pub unsafe fn dc_initiate_key_transfer(context: &Context) -> *mut libc::c_char {
}
}
}
free(setup_file_name as *mut libc::c_void);
dc_free_ongoing(context);

setup_code.strdup()
Expand Down Expand Up @@ -639,7 +627,7 @@ unsafe fn import_backup(context: &Context, backup_to_import: *const libc::c_char
}

let pathNfilename = context.get_blobdir().join(file_name);
if dc_write_file_safe(context, &pathNfilename, &file_blob) {
if dc_write_file(context, &pathNfilename, &file_blob) {
continue;
}
error!(
Expand Down Expand Up @@ -686,13 +674,8 @@ unsafe fn export_backup(context: &Context, dir: *const libc::c_char) -> bool {
let res = chrono::NaiveDateTime::from_timestamp(now as i64, 0)
.format("delta-chat-%Y-%m-%d.bak")
.to_string();
let buffer = CString::yolo(res);
let dest_pathNfilename = dc_get_fine_pathNfilename(context, dir, buffer.as_ptr());
if dest_pathNfilename.is_null() {
error!(context, "Cannot get backup file name.",);

return success;
}
let dest_path_filename = dc_get_fine_path_filename(context, as_path(dir), res);

sql::housekeeping(context);

Expand All @@ -703,15 +686,15 @@ unsafe fn export_backup(context: &Context, dir: *const libc::c_char) -> bool {
context,
"Backup \"{}\" to \"{}\".",
context.get_dbfile().display(),
as_str(dest_pathNfilename),
dest_path_filename.display(),
);
if dc_copy_file(context, context.get_dbfile(), as_path(dest_pathNfilename)) {
if dc_copy_file(context, context.get_dbfile(), &dest_path_filename) {
context.sql.open(&context, &context.get_dbfile(), 0);
closed = false;
/* add all files as blobs to the database copy (this does not require the source to be locked, neigher the destination as it is used only here) */
/*for logging only*/
let sql = Sql::new();
if sql.open(context, as_path(dest_pathNfilename), 0) {
if sql.open(context, &dest_path_filename, 0) {
if !sql.table_exists("backup_blobs") {
if sql::execute(
context,
Expand Down Expand Up @@ -820,9 +803,7 @@ unsafe fn export_backup(context: &Context, dir: *const libc::c_char) -> bool {
.set_config_int(context, "backup_time", now as i32)
.is_ok()
{
context.call_cb(Event::ImexFileWritten(
as_path(dest_pathNfilename).to_path_buf(),
));
context.call_cb(Event::ImexFileWritten(dest_path_filename.clone()));
success = true;
}
}
Expand All @@ -840,9 +821,8 @@ unsafe fn export_backup(context: &Context, dir: *const libc::c_char) -> bool {
context.sql.open(&context, &context.get_dbfile(), 0);
}
if 0 != delete_dest_file {
dc_delete_file(context, as_path(dest_pathNfilename));
dc_delete_file(context, &dest_path_filename);
}
free(dest_pathNfilename as *mut libc::c_void);

success
}
Expand All @@ -859,10 +839,8 @@ unsafe fn import_self_keys(context: &Context, dir_name: *const libc::c_char) ->
(currently, the last imported key is the standard key unless it contains the string "legacy" in its name) */
let mut imported_cnt: libc::c_int = 0;
let mut suffix: *mut libc::c_char = ptr::null_mut();
let mut path_plus_name: *mut libc::c_char = ptr::null_mut();
let mut set_default: libc::c_int;
let mut buf: *mut libc::c_char = ptr::null_mut();
let mut buf_bytes: libc::size_t = 0;
// a pointer inside buf, MUST NOT be free()'d
let mut private_key: *const libc::c_char;
let mut buf2: *mut libc::c_char = ptr::null_mut();
Expand All @@ -885,25 +863,21 @@ unsafe fn import_self_keys(context: &Context, dir_name: *const libc::c_char) ->
{
continue;
}
free(path_plus_name as *mut libc::c_void);
path_plus_name = dc_mprintf(
b"%s/%s\x00" as *const u8 as *const libc::c_char,
dir_name,
name_c.as_ptr(),
);
info!(context, "Checking: {}", as_str(path_plus_name));
free(buf as *mut libc::c_void);
let path_plus_name = dir.join(entry.file_name());
info!(context, "Checking: {}", path_plus_name.display());

free(buf.cast());
buf = ptr::null_mut();
if 0 == dc_read_file(
context,
path_plus_name,
&mut buf as *mut *mut libc::c_char as *mut *mut libc::c_void,
&mut buf_bytes,
) || buf_bytes < 50
{

if let Some(buf_r) = dc_read_file_safe(context, &path_plus_name) {
buf = buf_r.as_ptr() as *mut _;
std::mem::forget(buf_r);
} else {
continue;
}
};

private_key = buf;

free(buf2 as *mut libc::c_void);
buf2 = dc_strdup(buf);
if dc_split_armored_data(
Expand Down Expand Up @@ -936,7 +910,7 @@ unsafe fn import_self_keys(context: &Context, dir_name: *const libc::c_char) ->
info!(
context,
"Treating \"{}\" as a legacy private key.",
as_str(path_plus_name),
path_plus_name.display(),
);
set_default = 0i32
}
Expand All @@ -962,7 +936,6 @@ unsafe fn import_self_keys(context: &Context, dir_name: *const libc::c_char) ->
}

free(suffix as *mut libc::c_void);
free(path_plus_name as *mut libc::c_void);
free(buf as *mut libc::c_void);
free(buf2 as *mut libc::c_void);

Expand Down Expand Up @@ -1025,38 +998,31 @@ unsafe fn export_key_to_asc_file(
is_default: libc::c_int,
) -> bool {
let mut success = false;
let file_name;
if 0 != is_default {
file_name = dc_mprintf(
b"%s/%s-key-default.asc\x00" as *const u8 as *const libc::c_char,
dir,
if key.is_public() {
b"public\x00" as *const u8 as *const libc::c_char
} else {
b"private\x00" as *const u8 as *const libc::c_char
},
)
let dir = as_path(dir);

let file_name = if 0 != is_default {
let name = format!(
"{}-key-default.asc",
if key.is_public() { "public" } else { "private" },
);
dir.join(name)
} else {
file_name = dc_mprintf(
b"%s/%s-key-%i.asc\x00" as *const u8 as *const libc::c_char,
dir,
if key.is_public() {
b"public\x00" as *const u8 as *const libc::c_char
} else {
b"private\x00" as *const u8 as *const libc::c_char
},
id,
)
}
info!(context, "Exporting key {}", as_str(file_name),);
dc_delete_file(context, as_path(file_name));
if !key.write_asc_to_file(as_path(file_name), context) {
error!(context, "Cannot write key to {}", as_str(file_name),);
let name = format!(
"{}-key-{}.asc",
if key.is_public() { "public" } else { "private" },
id
);
dir.join(name)
};
info!(context, "Exporting key {}", file_name.display());
dc_delete_file(context, &file_name);

if !key.write_asc_to_file(&file_name, context) {
error!(context, "Cannot write key to {}", file_name.display());
} else {
context.call_cb(Event::ImexFileWritten(as_path(file_name).to_path_buf()));
context.call_cb(Event::ImexFileWritten(file_name.clone()));
success = true;
}
free(file_name as *mut libc::c_void);

success
}
Expand Down
Loading

0 comments on commit b680319

Please sign in to comment.