From f198ce29b132af0fea58af0948253b53ba6ea986 Mon Sep 17 00:00:00 2001 From: Dmitry Bogatov Date: Thu, 12 Sep 2019 01:33:58 +0000 Subject: [PATCH] Drop unsafe version of `dc_get_abs_path` --- src/chat.rs | 2 +- src/config.rs | 2 +- src/dc_mimefactory.rs | 10 ++++------ src/dc_tools.rs | 39 +++++++++------------------------------ src/message.rs | 2 +- 5 files changed, 16 insertions(+), 39 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index 6aa74d6fe..24362dd78 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -183,7 +183,7 @@ impl Chat { pub fn get_profile_image(&self, context: &Context) -> Option { if let Some(image_rel) = self.param.get(Param::ProfileImage) { if !image_rel.is_empty() { - return Some(dc_get_abs_path_safe(context, image_rel)); + return Some(dc_get_abs_path(context, image_rel)); } } else if self.typ == Chattype::Single { let contacts = get_chat_contacts(context, self.id); diff --git a/src/config.rs b/src/config.rs index de85f96bd..e64ca11bb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -73,7 +73,7 @@ impl Context { let value = match key { Config::Selfavatar => { let rel_path = self.sql.get_config(self, key); - rel_path.map(|p| dc_get_abs_path_safe(self, &p).to_str().unwrap().to_string()) + rel_path.map(|p| dc_get_abs_path(self, &p).to_str().unwrap().to_string()) } Config::SysVersion => Some((&*DC_VERSION_STR).clone()), Config::SysMsgsizeMaxRecommended => Some(format!("{}", 24 * 1024 * 1024 / 4 * 3)), diff --git a/src/dc_mimefactory.rs b/src/dc_mimefactory.rs index 2b1abadc7..371c55b33 100644 --- a/src/dc_mimefactory.rs +++ b/src/dc_mimefactory.rs @@ -1289,12 +1289,10 @@ unsafe fn build_body_file( ) as *mut libc::c_void, ); mime_sub = mailmime_new_empty(content, mime_fields); - let abs_path = dc_get_abs_path_safe(context, path_filename) - .as_os_str() - .to_str() - .unwrap() - .strdup(); - mailmime_set_body_file(mime_sub, abs_path); + let abs_path = dc_get_abs_path(context, path_filename) + .to_c_string() + .unwrap(); + mailmime_set_body_file(mime_sub, dc_strdup(abs_path.as_ptr())); if !ret_file_name_as_sent.is_null() { *ret_file_name_as_sent = dc_strdup(filename_to_send) } diff --git a/src/dc_tools.rs b/src/dc_tools.rs index 225c32cbe..bf2009318 100644 --- a/src/dc_tools.rs +++ b/src/dc_tools.rs @@ -792,7 +792,7 @@ pub fn dc_get_filemeta(buf: &[u8]) -> Result<(u32, u32), Error> { /// /// If `path` starts with "$BLOBDIR", replaces it with the blobdir path. /// Otherwise, returns path as is. -pub fn dc_get_abs_path_safe>( +pub fn dc_get_abs_path>( context: &Context, path: P, ) -> std::path::PathBuf { @@ -808,33 +808,12 @@ pub fn dc_get_abs_path_safe>( } } -pub unsafe fn dc_get_abs_path( - context: &Context, - path_filename: impl AsRef, -) -> *mut libc::c_char { - let starts = path_filename.as_ref().starts_with("$BLOBDIR"); - - if starts && !context.has_blobdir() { - return ptr::null_mut(); - } - - let mut path_filename_abs = path_filename.as_ref().strdup(); - if starts && context.has_blobdir() { - dc_str_replace( - &mut path_filename_abs, - b"$BLOBDIR\x00" as *const u8 as *const libc::c_char, - context.get_blobdir(), - ); - } - path_filename_abs -} - pub fn dc_file_exist(context: &Context, path: impl AsRef) -> bool { - dc_get_abs_path_safe(context, &path).exists() + dc_get_abs_path(context, &path).exists() } pub fn dc_get_filebytes(context: &Context, path: impl AsRef) -> uint64_t { - let path_abs = dc_get_abs_path_safe(context, &path); + let path_abs = dc_get_abs_path(context, &path); match fs::metadata(&path_abs) { Ok(meta) => meta.len() as uint64_t, Err(_err) => 0, @@ -842,7 +821,7 @@ pub fn dc_get_filebytes(context: &Context, path: impl AsRef) -> } pub fn dc_delete_file(context: &Context, path: impl AsRef) -> bool { - let path_abs = dc_get_abs_path_safe(context, &path); + let path_abs = dc_get_abs_path(context, &path); if !path_abs.is_file() { warn!( context, @@ -866,8 +845,8 @@ pub fn dc_copy_file( src: impl AsRef, dest: impl AsRef, ) -> bool { - let src_abs = dc_get_abs_path_safe(context, &src); - let dest_abs = dc_get_abs_path_safe(context, &dest); + let src_abs = dc_get_abs_path(context, &src); + let dest_abs = dc_get_abs_path(context, &dest); match fs::copy(&src_abs, &dest_abs) { Ok(_) => true, Err(_) => { @@ -883,7 +862,7 @@ pub fn dc_copy_file( } pub fn dc_create_folder(context: &Context, path: impl AsRef) -> bool { - let path_abs = dc_get_abs_path_safe(context, &path); + let path_abs = dc_get_abs_path(context, &path); if !path_abs.exists() { match fs::create_dir_all(path_abs) { Ok(_) => true, @@ -918,7 +897,7 @@ pub fn dc_write_file_safe>( path: P, buf: &[u8], ) -> bool { - let path_abs = dc_get_abs_path_safe(context, &path); + let path_abs = dc_get_abs_path(context, &path); if let Err(_err) = fs::write(&path_abs, buf) { warn!( context, @@ -953,7 +932,7 @@ pub unsafe fn dc_read_file( } pub fn dc_read_file_safe>(context: &Context, path: P) -> Option> { - let path_abs = dc_get_abs_path_safe(context, &path); + let path_abs = dc_get_abs_path(context, &path); match fs::read(&path_abs) { Ok(bytes) => Some(bytes), Err(_err) => { diff --git a/src/message.rs b/src/message.rs index 26fddb155..4ca6f46e4 100644 --- a/src/message.rs +++ b/src/message.rs @@ -376,7 +376,7 @@ pub fn dc_msg_guess_msgtype_from_suffix(path: &Path) -> Option<(Viewtype, &str)> pub unsafe fn dc_msg_get_file(context: &Context, msg: &Message) -> Option { msg.param .get(Param::File) - .map(|f| dc_get_abs_path_safe(context, f)) + .map(|f| dc_get_abs_path(context, f)) } /**