From 6b9d35fb40ff07f2ff1954cb8b1bfb71de7507c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Rodrigues=20Miguel?= Date: Thu, 28 Oct 2021 10:30:23 -0300 Subject: [PATCH] Avoid reallocating Strings when creating `TarError` --- src/archive.rs | 2 +- src/entry.rs | 16 ++++++++-------- src/error.rs | 7 ++++--- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/archive.rs b/src/archive.rs index 3e98ddce..1bed5124 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -189,7 +189,7 @@ impl Archive { fn _unpack(&mut self, dst: &Path) -> io::Result<()> { if dst.symlink_metadata().is_err() { fs::create_dir_all(&dst) - .map_err(|e| TarError::new(&format!("failed to create `{}`", dst.display()), e))?; + .map_err(|e| TarError::new(format!("failed to create `{}`", dst.display()), e))?; } // Canonicalizing the dst directory will prepend the path with '\\?\' diff --git a/src/entry.rs b/src/entry.rs index 9e4516e2..743d4c42 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -379,7 +379,7 @@ impl<'a> EntryFields<'a> { { let path = self.path().map_err(|e| { TarError::new( - &format!("invalid path in entry header: {}", self.path_lossy()), + format!("invalid path in entry header: {}", self.path_lossy()), e, ) })?; @@ -414,12 +414,12 @@ impl<'a> EntryFields<'a> { }; self.ensure_dir_created(&dst, parent) - .map_err(|e| TarError::new(&format!("failed to create `{}`", parent.display()), e))?; + .map_err(|e| TarError::new(format!("failed to create `{}`", parent.display()), e))?; let canon_target = self.validate_inside_dst(&dst, parent)?; self.unpack(Some(&canon_target), &file_dst) - .map_err(|e| TarError::new(&format!("failed to unpack `{}`", file_dst.display()), e))?; + .map_err(|e| TarError::new(format!("failed to unpack `{}`", file_dst.display()), e))?; Ok(true) } @@ -607,7 +607,7 @@ impl<'a> EntryFields<'a> { .map_err(|e| { let header = self.header.path_bytes(); TarError::new( - &format!( + format!( "failed to unpack `{}` into `{}`", String::from_utf8_lossy(&header), dst.display() @@ -627,7 +627,7 @@ impl<'a> EntryFields<'a> { let mtime = if mtime == 0 { 1 } else { mtime }; let mtime = FileTime::from_unix_time(mtime as i64, 0); filetime::set_file_handle_times(&f, Some(mtime), Some(mtime)).map_err(|e| { - TarError::new(&format!("failed to set mtime for `{}`", dst.display()), e) + TarError::new(format!("failed to set mtime for `{}`", dst.display()), e) })?; } } @@ -647,7 +647,7 @@ impl<'a> EntryFields<'a> { ) -> Result<(), TarError> { _set_perms(dst, f, mode, preserve).map_err(|e| { TarError::new( - &format!( + format!( "failed to set permissions to {:o} \ for `{}`", mode, @@ -735,7 +735,7 @@ impl<'a> EntryFields<'a> { for (key, value) in exts { xattr::set(dst, key, value).map_err(|e| { TarError::new( - &format!( + format!( "failed to set extended \ attributes to {}. \ Xattrs: key={:?}, value={:?}.", @@ -794,7 +794,7 @@ impl<'a> EntryFields<'a> { })?; if !canon_parent.starts_with(&canon_target) { let err = TarError::new( - &format!( + format!( "trying to unpack outside of destination path: {}", canon_target.display() ), diff --git a/src/error.rs b/src/error.rs index 0bbc6cd0..0d3877fa 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,17 +1,18 @@ +use std::borrow::Cow; use std::error; use std::fmt; use std::io::{self, Error}; #[derive(Debug)] pub struct TarError { - desc: String, + desc: Cow<'static, str>, io: io::Error, } impl TarError { - pub fn new(desc: &str, err: Error) -> TarError { + pub fn new(desc: impl Into>, err: Error) -> TarError { TarError { - desc: desc.to_string(), + desc: desc.into(), io: err, } }