Reduce number of arguments of export_key_to_asc_file

Previously, this function accepted both key id (integer) and is_default
(boolean). If `is_default` flag was set, value `id` parameter wasn't
used. This commit compresses two argument into single `id: Option<i64>`.
This commit is contained in:
Dmitry Bogatov
2019-09-13 16:43:04 +00:00
committed by holger krekel
parent 5d0481f7a2
commit 178b216e48

View File

@@ -961,22 +961,23 @@ unsafe fn export_self_keys(context: &Context, dir: *const libc::c_char) -> bool
let public_key = Key::from_slice(&public_key_blob, KeyType::Public); let public_key = Key::from_slice(&public_key_blob, KeyType::Public);
let private_key_blob: Vec<u8> = row.get(2)?; let private_key_blob: Vec<u8> = row.get(2)?;
let private_key = Key::from_slice(&private_key_blob, KeyType::Private); let private_key = Key::from_slice(&private_key_blob, KeyType::Private);
let is_default = row.get(3)?; let is_default: i32 = row.get(3)?;
Ok((id, public_key, private_key, is_default)) Ok((id, public_key, private_key, is_default))
}, },
|keys| { |keys| {
for key_pair in keys { for key_pair in keys {
let (id, public_key, private_key, is_default) = key_pair?; let (id, public_key, private_key, is_default) = key_pair?;
let id = Some(id).filter(|_| is_default != 0);
if let Some(key) = public_key { if let Some(key) = public_key {
if export_key_to_asc_file(context, dir, id, &key, is_default) { if export_key_to_asc_file(context, dir, id, &key) {
export_errors += 1; export_errors += 1;
} }
} else { } else {
export_errors += 1; export_errors += 1;
} }
if let Some(key) = private_key { if let Some(key) = private_key {
if export_key_to_asc_file(context, dir, id, &key, is_default) { if export_key_to_asc_file(context, dir, id, &key) {
export_errors += 1; export_errors += 1;
} }
} else { } else {
@@ -998,34 +999,26 @@ unsafe fn export_self_keys(context: &Context, dir: *const libc::c_char) -> bool
unsafe fn export_key_to_asc_file( unsafe fn export_key_to_asc_file(
context: &Context, context: &Context,
dir: *const libc::c_char, dir: *const libc::c_char,
id: libc::c_int, id: Option<i64>,
key: &Key, key: &Key,
is_default: libc::c_int,
) -> bool { ) -> bool {
let mut success = false; let mut success = false;
let dir = as_path(dir); let file_name = {
let kind = if key.is_public() { "public" } else { "private" };
let file_name = if 0 != is_default { let dir = as_str(dir);
let name = format!( if let Some(id) = id {
"{}-key-default.asc", format!("{}/{}-key-{}.asc", dir, kind, id)
if key.is_public() { "public" } else { "private" },
);
dir.join(name)
} else { } else {
let name = format!( format!("{}/{}-key-default.asc", dir, kind)
"{}-key-{}.asc", }
if key.is_public() { "public" } else { "private" },
id
);
dir.join(name)
}; };
info!(context, "Exporting key {}", file_name.display()); info!(context, "Exporting key {}", &file_name);
dc_delete_file(context, &file_name); dc_delete_file(context, &file_name);
if !key.write_asc_to_file(&file_name, context) { if !key.write_asc_to_file(&file_name, context) {
error!(context, "Cannot write key to {}", file_name.display()); error!(context, "Cannot write key to {}", file_name);
} else { } else {
context.call_cb(Event::ImexFileWritten(file_name.clone())); context.call_cb(Event::ImexFileWritten(file_name.into()));
success = true; success = true;
} }