diff --git a/deltachat-repl/src/cmdline.rs b/deltachat-repl/src/cmdline.rs index ae676143d..0c51e3f31 100644 --- a/deltachat-repl/src/cmdline.rs +++ b/deltachat-repl/src/cmdline.rs @@ -92,7 +92,7 @@ async fn reset_tables(context: &Context, bits: i32) { context.emit_msgs_changed_without_ids(); } -async fn poke_eml_file(context: &Context, filename: impl AsRef) -> Result<()> { +async fn poke_eml_file(context: &Context, filename: &Path) -> Result<()> { let data = read_file(context, filename).await?; if let Err(err) = receive_imf(context, &data, false).await { @@ -126,7 +126,7 @@ async fn poke_spec(context: &Context, spec: Option<&str>) -> bool { real_spec = rs.unwrap(); } if let Some(suffix) = get_filesuffix_lc(&real_spec) { - if suffix == "eml" && poke_eml_file(context, &real_spec).await.is_ok() { + if suffix == "eml" && poke_eml_file(context, Path::new(&real_spec)).await.is_ok() { read_cnt += 1 } } else { @@ -140,7 +140,10 @@ async fn poke_spec(context: &Context, spec: Option<&str>) -> bool { if name.ends_with(".eml") { let path_plus_name = format!("{}/{}", &real_spec, name); println!("Import: {path_plus_name}"); - if poke_eml_file(context, path_plus_name).await.is_ok() { + if poke_eml_file(context, Path::new(&path_plus_name)) + .await + .is_ok() + { read_cnt += 1 } } @@ -1278,7 +1281,7 @@ pub async fn cmdline(context: Context, line: &str, chat_id: &mut ChatId) -> Resu "fileinfo" => { ensure!(!arg1.is_empty(), "Argument missing."); - if let Ok(buf) = read_file(&context, &arg1).await { + if let Ok(buf) = read_file(&context, Path::new(arg1)).await { let (width, height) = get_filemeta(&buf)?; println!("width={width}, height={height}"); } else { diff --git a/src/blob.rs b/src/blob.rs index 64fde4520..ce08d5028 100644 --- a/src/blob.rs +++ b/src/blob.rs @@ -1,7 +1,6 @@ //! # Blob directory management. use core::cmp::max; -use std::ffi::OsStr; use std::io::{Cursor, Seek}; use std::iter::FusedIterator; use std::mem; @@ -151,10 +150,10 @@ impl<'a> BlobObject<'a> { let rel_path = path .strip_prefix(context.get_blobdir()) .with_context(|| format!("wrong blobdir: {}", path.display()))?; - if !BlobObject::is_acceptible_blob_name(rel_path) { + let name = rel_path.to_str().context("wrong name")?; + if !BlobObject::is_acceptible_blob_name(name) { return Err(format_err!("bad blob name: {}", rel_path.display())); } - let name = rel_path.to_str().context("wrong name")?; BlobObject::from_name(context, name) } @@ -216,19 +215,17 @@ impl<'a> BlobObject<'a> { /// /// This is slightly less strict than stanitise_name, presumably /// someone already created a file with such a name so we just - /// ensure it's not actually a path in disguise is actually utf-8. - fn is_acceptible_blob_name(name: impl AsRef) -> bool { - let uname = match name.as_ref().to_str() { - Some(name) => name, - None => return false, - }; - if uname.find('/').is_some() { + /// ensure it's not actually a path in disguise. + /// + /// Acceptible blob name always have to be valid utf-8. + fn is_acceptible_blob_name(name: &str) -> bool { + if name.find('/').is_some() { return false; } - if uname.find('\\').is_some() { + if name.find('\\').is_some() { return false; } - if uname.find('\0').is_some() { + if name.find('\0').is_some() { return false; } true diff --git a/src/blob/blob_tests.rs b/src/blob/blob_tests.rs index 6b48dc67e..3d8eb9f6b 100644 --- a/src/blob/blob_tests.rs +++ b/src/blob/blob_tests.rs @@ -120,7 +120,7 @@ async fn test_create_from_name_long() { fn test_is_blob_name() { assert!(BlobObject::is_acceptible_blob_name("foo")); assert!(BlobObject::is_acceptible_blob_name("foo.txt")); - assert!(BlobObject::is_acceptible_blob_name("f".repeat(128))); + assert!(BlobObject::is_acceptible_blob_name(&"f".repeat(128))); assert!(!BlobObject::is_acceptible_blob_name("foo/bar")); assert!(!BlobObject::is_acceptible_blob_name("foo\\bar")); assert!(!BlobObject::is_acceptible_blob_name("foo\x00bar")); diff --git a/src/imex.rs b/src/imex.rs index 04f908d82..a6b3e4890 100644 --- a/src/imex.rs +++ b/src/imex.rs @@ -209,7 +209,7 @@ async fn imex_inner( .await .context("Cannot create private key or private key not available")?; - create_folder(context, &path).await?; + create_folder(context, path).await?; } match what { @@ -600,7 +600,7 @@ where /// Imports secret key from a file. async fn import_secret_key(context: &Context, path: &Path, set_default: bool) -> Result<()> { - let buf = read_file(context, &path).await?; + let buf = read_file(context, path).await?; let armored = std::string::String::from_utf8_lossy(&buf); set_self_key(context, &armored, set_default).await?; Ok(()) diff --git a/src/message.rs b/src/message.rs index 8dfcd84fa..963128ad4 100644 --- a/src/message.rs +++ b/src/message.rs @@ -973,7 +973,7 @@ impl Message { } if let Some(filename) = self.get_file(context) { - if let Ok(ref buf) = read_file(context, filename).await { + if let Ok(ref buf) = read_file(context, &filename).await { if let Ok((typ, headers, _)) = split_armored_data(buf) { if typ == pgp::armor::BlockType::Message { return headers.get(crate::pgp::HEADER_SETUPCODE).cloned(); diff --git a/src/test_utils.rs b/src/test_utils.rs index 657493edf..6bf1107e5 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -400,11 +400,11 @@ impl TestContext { /// Sets a name for this [`TestContext`] if one isn't yet set. /// /// This will show up in events logged in the test output. - pub fn set_name(&self, name: impl Into) { + pub fn set_name(&self, name: &str) { let mut context_names = CONTEXT_NAMES.write().unwrap(); context_names .entry(self.ctx.get_id()) - .or_insert_with(|| name.into()); + .or_insert_with(|| name.to_string()); } /// Returns the name of this [`TestContext`]. diff --git a/src/tools.rs b/src/tools.rs index 7ba3ae676..25f953720 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -348,14 +348,13 @@ pub(crate) fn get_abs_path(context: &Context, path: &Path) -> PathBuf { } } -pub(crate) async fn get_filebytes(context: &Context, path: impl AsRef) -> Result { - let path_abs = get_abs_path(context, path.as_ref()); +pub(crate) async fn get_filebytes(context: &Context, path: &Path) -> Result { + let path_abs = get_abs_path(context, path); let meta = fs::metadata(&path_abs).await?; Ok(meta.len()) } -pub(crate) async fn delete_file(context: &Context, path: impl AsRef) -> Result<()> { - let path = path.as_ref(); +pub(crate) async fn delete_file(context: &Context, path: &Path) -> Result<()> { let path_abs = get_abs_path(context, path); if !path_abs.exists() { bail!("path {} does not exist", path_abs.display()); @@ -443,11 +442,8 @@ impl AsRef for TempPathGuard { } } -pub(crate) async fn create_folder( - context: &Context, - path: impl AsRef, -) -> Result<(), io::Error> { - let path_abs = get_abs_path(context, path.as_ref()); +pub(crate) async fn create_folder(context: &Context, path: &Path) -> Result<(), io::Error> { + let path_abs = get_abs_path(context, path); if !path_abs.exists() { match fs::create_dir_all(path_abs).await { Ok(_) => Ok(()), @@ -455,7 +451,7 @@ pub(crate) async fn create_folder( warn!( context, "Cannot create directory \"{}\": {}", - path.as_ref().display(), + path.display(), err ); Err(err) @@ -469,16 +465,16 @@ pub(crate) async fn create_folder( /// Write a the given content to provided file path. pub(crate) async fn write_file( context: &Context, - path: impl AsRef, + path: &Path, buf: &[u8], ) -> Result<(), io::Error> { - let path_abs = get_abs_path(context, path.as_ref()); + let path_abs = get_abs_path(context, path); fs::write(&path_abs, buf).await.map_err(|err| { warn!( context, "Cannot write {} bytes to \"{}\": {}", buf.len(), - path.as_ref().display(), + path.display(), err ); err @@ -486,8 +482,8 @@ pub(crate) async fn write_file( } /// Reads the file and returns its context as a byte vector. -pub async fn read_file(context: &Context, path: impl AsRef) -> Result> { - let path_abs = get_abs_path(context, path.as_ref()); +pub async fn read_file(context: &Context, path: &Path) -> Result> { + let path_abs = get_abs_path(context, path); match fs::read(&path_abs).await { Ok(bytes) => Ok(bytes), @@ -495,7 +491,7 @@ pub async fn read_file(context: &Context, path: impl AsRef) -> Result) -> Result) -> Result { - let path_abs = get_abs_path(context, path.as_ref()); +pub async fn open_file(context: &Context, path: &Path) -> Result { + let path_abs = get_abs_path(context, path); match fs::File::open(&path_abs).await { Ok(bytes) => Ok(bytes), @@ -512,7 +508,7 @@ pub async fn open_file(context: &Context, path: impl AsRef) -> Result