Resultify dc_write_file and related functions

This commit is contained in:
Alexander Krotov
2019-11-30 00:11:21 +01:00
committed by holger krekel
parent 4e6d0c9c69
commit 02e281e465
3 changed files with 32 additions and 28 deletions

View File

@@ -523,9 +523,13 @@ pub(crate) fn dc_create_folder(context: &Context, path: impl AsRef<std::path::Pa
} }
/// Write a the given content to provied file path. /// Write a the given content to provied file path.
pub(crate) fn dc_write_file(context: &Context, path: impl AsRef<Path>, buf: &[u8]) -> bool { pub(crate) fn dc_write_file(
context: &Context,
path: impl AsRef<Path>,
buf: &[u8],
) -> Result<(), std::io::Error> {
let path_abs = dc_get_abs_path(context, &path); 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!( warn!(
context, context,
"Cannot write {} bytes to \"{}\": {}", "Cannot write {} bytes to \"{}\": {}",
@@ -533,10 +537,8 @@ pub(crate) fn dc_write_file(context: &Context, path: impl AsRef<Path>, buf: &[u8
path.as_ref().display(), path.as_ref().display(),
err err
); );
false err
} else { })
true
}
} }
pub fn dc_read_file<P: AsRef<std::path::Path>>( pub fn dc_read_file<P: AsRef<std::path::Path>>(
@@ -1323,7 +1325,7 @@ mod tests {
dc_delete_file(context, "$BLOBDIR/foobar.dadada"); dc_delete_file(context, "$BLOBDIR/foobar.dadada");
dc_delete_file(context, "$BLOBDIR/foobar-folder"); 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/foobar",));
assert!(!dc_file_exist(context, "$BLOBDIR/foobarx")); assert!(!dc_file_exist(context, "$BLOBDIR/foobarx"));
assert_eq!(dc_get_filebytes(context, "$BLOBDIR/foobar"), 7); assert_eq!(dc_get_filebytes(context, "$BLOBDIR/foobar"), 7);
@@ -1355,7 +1357,7 @@ mod tests {
assert!(!dc_delete_file(context, "$BLOBDIR/foobar-folder")); assert!(!dc_delete_file(context, "$BLOBDIR/foobar-folder"));
let fn0 = "$BLOBDIR/data.data"; 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_delete_file(context, &fn0));
assert!(!dc_file_exist(context, &fn0)); assert!(!dc_file_exist(context, &fn0));

View File

@@ -476,14 +476,15 @@ fn import_backup(context: &Context, backup_to_import: impl AsRef<Path>) -> Resul
} }
let path_filename = context.get_blobdir().join(file_name); 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; continue;
} }
bail!(
"Storage full? Cannot write file {} with {} bytes.",
path_filename.display(),
file_blob.len(),
);
} }
Ok(()) Ok(())
}, },
@@ -686,14 +687,14 @@ fn export_self_keys(context: &Context, dir: impl AsRef<Path>) -> Result<()> {
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); 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) { if export_key_to_asc_file(context, &dir, id, &key).is_err() {
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) { if export_key_to_asc_file(context, &dir, id, &key).is_err() {
export_errors += 1; export_errors += 1;
} }
} else { } else {
@@ -717,8 +718,7 @@ fn export_key_to_asc_file(
dir: impl AsRef<Path>, dir: impl AsRef<Path>,
id: Option<i64>, id: Option<i64>,
key: &Key, key: &Key,
) -> bool { ) -> std::io::Result<()> {
let mut success = false;
let file_name = { let file_name = {
let kind = if key.is_public() { "public" } else { "private" }; let kind = if key.is_public() { "public" } else { "private" };
let id = id.map_or("default".into(), |i| i.to_string()); 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()); info!(context, "Exporting key {}", file_name.display());
dc_delete_file(context, &file_name); 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()); error!(context, "Cannot write key to {}", file_name.display());
} else { } else {
context.call_cb(Event::ImexFileWritten(file_name)); context.call_cb(Event::ImexFileWritten(file_name));
success = true;
} }
res
success
} }
#[cfg(test)] #[cfg(test)]
@@ -799,7 +798,7 @@ mod tests {
let base64 = include_str!("../test-data/key/public.asc"); let base64 = include_str!("../test-data/key/public.asc");
let key = Key::from_base64(base64, KeyType::Public).unwrap(); let key = Key::from_base64(base64, KeyType::Public).unwrap();
let blobdir = "$BLOBDIR"; 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 blobdir = context.ctx.get_blobdir().to_str().unwrap();
let filename = format!("{}/public-key-default.asc", blobdir); let filename = format!("{}/public-key-default.asc", blobdir);
let bytes = std::fs::read(&filename).unwrap(); let bytes = std::fs::read(&filename).unwrap();

View File

@@ -217,15 +217,18 @@ impl Key {
.expect("failed to serialize key") .expect("failed to serialize key")
} }
pub fn write_asc_to_file(&self, file: impl AsRef<Path>, context: &Context) -> bool { pub fn write_asc_to_file(
&self,
file: impl AsRef<Path>,
context: &Context,
) -> std::io::Result<()> {
let file_content = self.to_asc(None).into_bytes(); let file_content = self.to_asc(None).into_bytes();
if dc_write_file(context, &file, &file_content) { let res = dc_write_file(context, &file, &file_content);
true if res.is_err() {
} else {
error!(context, "Cannot write key to {}", file.as_ref().display()); error!(context, "Cannot write key to {}", file.as_ref().display());
false
} }
res
} }
pub fn fingerprint(&self) -> String { pub fn fingerprint(&self) -> String {