refactor(imex): remove dc_mprintf

This commit is contained in:
dignifiedquire
2019-09-15 21:28:03 +02:00
parent 72a9ca0aa5
commit d091857cef

View File

@@ -804,7 +804,7 @@ unsafe fn export_backup(context: &Context, dir: *const libc::c_char) -> bool {
.set_config_int(context, "backup_time", now as i32) .set_config_int(context, "backup_time", now as i32)
.is_ok() .is_ok()
{ {
context.call_cb(Event::ImexFileWritten(&dest_path_filename)); context.call_cb(Event::ImexFileWritten(dest_path_filename.clone()));
success = true; success = true;
} }
} }
@@ -840,10 +840,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) */ (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 imported_cnt: libc::c_int = 0;
let mut suffix: *mut libc::c_char = ptr::null_mut(); 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 set_default: libc::c_int;
let mut buf: *mut libc::c_char = ptr::null_mut(); 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 // a pointer inside buf, MUST NOT be free()'d
let mut private_key: *const libc::c_char; let mut private_key: *const libc::c_char;
let mut buf2: *mut libc::c_char = ptr::null_mut(); let mut buf2: *mut libc::c_char = ptr::null_mut();
@@ -866,25 +864,21 @@ unsafe fn import_self_keys(context: &Context, dir_name: *const libc::c_char) ->
{ {
continue; continue;
} }
free(path_plus_name as *mut libc::c_void); let path_plus_name = dir.join(entry.file_name());
path_plus_name = dc_mprintf( info!(context, "Checking: {}", path_plus_name.display());
b"%s/%s\x00" as *const u8 as *const libc::c_char,
dir_name, free(buf.cast());
name_c.as_ptr(),
);
info!(context, "Checking: {}", as_str(path_plus_name));
free(buf as *mut libc::c_void);
buf = ptr::null_mut(); buf = ptr::null_mut();
if 0 == dc_read_file(
context, if let Some(buf_r) = dc_read_file_safe(context, &path_plus_name) {
path_plus_name, buf = buf_r.as_ptr() as *mut _;
&mut buf as *mut *mut libc::c_char as *mut *mut libc::c_void, std::mem::forget(buf_r);
&mut buf_bytes, } else {
) || buf_bytes < 50
{
continue; continue;
} };
private_key = buf; private_key = buf;
free(buf2 as *mut libc::c_void); free(buf2 as *mut libc::c_void);
buf2 = dc_strdup(buf); buf2 = dc_strdup(buf);
if dc_split_armored_data( if dc_split_armored_data(
@@ -917,7 +911,7 @@ unsafe fn import_self_keys(context: &Context, dir_name: *const libc::c_char) ->
info!( info!(
context, context,
"Treating \"{}\" as a legacy private key.", "Treating \"{}\" as a legacy private key.",
as_str(path_plus_name), path_plus_name.display(),
); );
set_default = 0i32 set_default = 0i32
} }
@@ -943,7 +937,6 @@ unsafe fn import_self_keys(context: &Context, dir_name: *const libc::c_char) ->
} }
free(suffix as *mut libc::c_void); free(suffix as *mut libc::c_void);
free(path_plus_name as *mut libc::c_void);
free(buf as *mut libc::c_void); free(buf as *mut libc::c_void);
free(buf2 as *mut libc::c_void); free(buf2 as *mut libc::c_void);
@@ -1006,38 +999,31 @@ unsafe fn export_key_to_asc_file(
is_default: libc::c_int, is_default: libc::c_int,
) -> bool { ) -> bool {
let mut success = false; let mut success = false;
let file_name; let dir = as_path(dir);
if 0 != is_default {
file_name = dc_mprintf( let file_name = if 0 != is_default {
b"%s/%s-key-default.asc\x00" as *const u8 as *const libc::c_char, let name = format!(
dir, "{}-key-default.asc",
if key.is_public() { if key.is_public() { "public" } else { "private" },
b"public\x00" as *const u8 as *const libc::c_char );
} else { dir.join(name)
b"private\x00" as *const u8 as *const libc::c_char
},
)
} else { } else {
file_name = dc_mprintf( let name = format!(
b"%s/%s-key-%i.asc\x00" as *const u8 as *const libc::c_char, "{}-key-{}.asc",
dir, if key.is_public() { "public" } else { "private" },
if key.is_public() { id
b"public\x00" as *const u8 as *const libc::c_char );
} else { dir.join(name)
b"private\x00" as *const u8 as *const libc::c_char };
}, info!(context, "Exporting key {}", file_name.display());
id, dc_delete_file(context, &file_name);
)
} if !key.write_asc_to_file(&file_name, context) {
info!(context, "Exporting key {}", as_str(file_name),); error!(context, "Cannot write key to {}", file_name.display());
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),);
} else { } else {
context.call_cb(Event::ImexFileWritten(as_path(file_name).to_path_buf())); context.call_cb(Event::ImexFileWritten(file_name.clone()));
success = true; success = true;
} }
free(file_name as *mut libc::c_void);
success success
} }