diff --git a/src/dc_tools.rs b/src/dc_tools.rs index 4eefe97b6..255e73a2a 100644 --- a/src/dc_tools.rs +++ b/src/dc_tools.rs @@ -523,9 +523,13 @@ pub(crate) fn dc_create_folder(context: &Context, path: impl AsRef, buf: &[u8]) -> bool { +pub(crate) fn dc_write_file( + context: &Context, + path: impl AsRef, + buf: &[u8], +) -> Result<(), std::io::Error> { let path_abs = dc_get_abs_path(context, &path); - if let Err(err) = fs::write(&path_abs, buf) { + fs::write(&path_abs, buf).map_err(|err| { warn!( context, "Cannot write {} bytes to \"{}\": {}", @@ -533,10 +537,8 @@ pub(crate) fn dc_write_file(context: &Context, path: impl AsRef, buf: &[u8 path.as_ref().display(), err ); - false - } else { - true - } + err + }) } pub fn dc_read_file>( @@ -1323,7 +1325,7 @@ mod tests { dc_delete_file(context, "$BLOBDIR/foobar.dadada"); dc_delete_file(context, "$BLOBDIR/foobar-folder"); } - assert!(dc_write_file(context, "$BLOBDIR/foobar", b"content")); + assert!(dc_write_file(context, "$BLOBDIR/foobar", b"content").is_ok()); assert!(dc_file_exist(context, "$BLOBDIR/foobar",)); assert!(!dc_file_exist(context, "$BLOBDIR/foobarx")); assert_eq!(dc_get_filebytes(context, "$BLOBDIR/foobar"), 7); @@ -1355,7 +1357,7 @@ mod tests { assert!(!dc_delete_file(context, "$BLOBDIR/foobar-folder")); let fn0 = "$BLOBDIR/data.data"; - assert!(dc_write_file(context, &fn0, b"content")); + assert!(dc_write_file(context, &fn0, b"content").is_ok()); assert!(dc_delete_file(context, &fn0)); assert!(!dc_file_exist(context, &fn0)); diff --git a/src/imex.rs b/src/imex.rs index c5c3066ad..62e2b8021 100644 --- a/src/imex.rs +++ b/src/imex.rs @@ -476,14 +476,15 @@ fn import_backup(context: &Context, backup_to_import: impl AsRef) -> Resul } let path_filename = context.get_blobdir().join(file_name); - if dc_write_file(context, &path_filename, &file_blob) { + if dc_write_file(context, &path_filename, &file_blob).is_err() { + bail!( + "Storage full? Cannot write file {} with {} bytes.", + path_filename.display(), + file_blob.len(), + ); + } else { continue; } - bail!( - "Storage full? Cannot write file {} with {} bytes.", - path_filename.display(), - file_blob.len(), - ); } Ok(()) }, @@ -686,14 +687,14 @@ fn export_self_keys(context: &Context, dir: impl AsRef) -> Result<()> { 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 !export_key_to_asc_file(context, &dir, id, &key) { + if export_key_to_asc_file(context, &dir, id, &key).is_err() { export_errors += 1; } } else { export_errors += 1; } if let Some(key) = private_key { - if !export_key_to_asc_file(context, &dir, id, &key) { + if export_key_to_asc_file(context, &dir, id, &key).is_err() { export_errors += 1; } } else { @@ -717,8 +718,7 @@ fn export_key_to_asc_file( dir: impl AsRef, id: Option, key: &Key, -) -> bool { - let mut success = false; +) -> std::io::Result<()> { let file_name = { let kind = if key.is_public() { "public" } else { "private" }; let id = id.map_or("default".into(), |i| i.to_string()); @@ -728,14 +728,13 @@ fn export_key_to_asc_file( info!(context, "Exporting key {}", file_name.display()); dc_delete_file(context, &file_name); - if !key.write_asc_to_file(&file_name, context) { + let res = key.write_asc_to_file(&file_name, context); + if res.is_err() { error!(context, "Cannot write key to {}", file_name.display()); } else { context.call_cb(Event::ImexFileWritten(file_name)); - success = true; } - - success + res } #[cfg(test)] @@ -799,7 +798,7 @@ mod tests { let base64 = include_str!("../test-data/key/public.asc"); let key = Key::from_base64(base64, KeyType::Public).unwrap(); let blobdir = "$BLOBDIR"; - assert!(export_key_to_asc_file(&context.ctx, blobdir, None, &key)); + assert!(export_key_to_asc_file(&context.ctx, blobdir, None, &key).is_ok()); let blobdir = context.ctx.get_blobdir().to_str().unwrap(); let filename = format!("{}/public-key-default.asc", blobdir); let bytes = std::fs::read(&filename).unwrap(); diff --git a/src/key.rs b/src/key.rs index 9679f7476..cd851c999 100644 --- a/src/key.rs +++ b/src/key.rs @@ -217,15 +217,18 @@ impl Key { .expect("failed to serialize key") } - pub fn write_asc_to_file(&self, file: impl AsRef, context: &Context) -> bool { + pub fn write_asc_to_file( + &self, + file: impl AsRef, + context: &Context, + ) -> std::io::Result<()> { let file_content = self.to_asc(None).into_bytes(); - if dc_write_file(context, &file, &file_content) { - true - } else { + let res = dc_write_file(context, &file, &file_content); + if res.is_err() { error!(context, "Cannot write key to {}", file.as_ref().display()); - false } + res } pub fn fingerprint(&self) -> String {