Remove tokio-tar. (#7631)

This commit is contained in:
Michał Wawrzyniec Urbańczyk 2023-08-22 23:13:10 +02:00 committed by GitHub
parent 2385f5b357
commit d7e5c3f362
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 66 deletions

44
Cargo.lock generated
View File

@ -3484,7 +3484,7 @@ checksum = "4e884668cd0c7480504233e951174ddc3b382f7c2666e3b7310b5c4e7b0c37f9"
dependencies = [ dependencies = [
"cfg-if 1.0.0", "cfg-if 1.0.0",
"libc", "libc",
"redox_syscall 0.2.16", "redox_syscall",
"windows-sys", "windows-sys",
] ]
@ -4295,7 +4295,6 @@ dependencies = [
"tempfile", "tempfile",
"tokio", "tokio",
"tokio-stream", "tokio-stream",
"tokio-tar",
"tokio-util", "tokio-util",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
@ -5350,7 +5349,7 @@ checksum = "ba1ef8814b5c993410bb3adfad7a5ed269563e4a2f90c41f5d85be7fb47133bf"
dependencies = [ dependencies = [
"cfg-if 1.0.0", "cfg-if 1.0.0",
"libc", "libc",
"redox_syscall 0.2.16", "redox_syscall",
"smallvec 1.10.0", "smallvec 1.10.0",
"windows-sys", "windows-sys",
] ]
@ -5876,15 +5875,6 @@ dependencies = [
"bitflags 1.3.2", "bitflags 1.3.2",
] ]
[[package]]
name = "redox_syscall"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29"
dependencies = [
"bitflags 1.3.2",
]
[[package]] [[package]]
name = "redox_users" name = "redox_users"
version = "0.4.3" version = "0.4.3"
@ -5892,7 +5882,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b"
dependencies = [ dependencies = [
"getrandom 0.2.8", "getrandom 0.2.8",
"redox_syscall 0.2.16", "redox_syscall",
"thiserror", "thiserror",
] ]
@ -6664,7 +6654,7 @@ checksum = "4b55807c0344e1e6c04d7c965f5289c39a8d94ae23ed5c0b57aabac549f871c6"
dependencies = [ dependencies = [
"filetime", "filetime",
"libc", "libc",
"xattr 0.2.3", "xattr",
] ]
[[package]] [[package]]
@ -6676,7 +6666,7 @@ dependencies = [
"cfg-if 1.0.0", "cfg-if 1.0.0",
"fastrand", "fastrand",
"libc", "libc",
"redox_syscall 0.2.16", "redox_syscall",
"remove_dir_all", "remove_dir_all",
"winapi", "winapi",
] ]
@ -6901,21 +6891,6 @@ dependencies = [
"tokio", "tokio",
] ]
[[package]]
name = "tokio-tar"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d5714c010ca3e5c27114c1cdeb9d14641ace49874aa5626d7149e47aedace75"
dependencies = [
"filetime",
"futures-core",
"libc",
"redox_syscall 0.3.5",
"tokio",
"tokio-stream",
"xattr 1.0.1",
]
[[package]] [[package]]
name = "tokio-tungstenite" name = "tokio-tungstenite"
version = "0.17.2" version = "0.17.2"
@ -7811,15 +7786,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "xattr"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4686009f71ff3e5c4dbcf1a282d0a44db3f021ba69350cd42086b3e5f1c6985"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "xi-rope" name = "xi-rope"
version = "0.3.0" version = "0.3.0"

View File

@ -58,7 +58,7 @@ quote = { workspace = true }
rand = "0.8.4" rand = "0.8.4"
regex = { workspace = true } regex = { workspace = true }
reqwest = { version = "0.11.5", default-features = false, features = [ reqwest = { version = "0.11.5", default-features = false, features = [
"stream" "stream",
] } ] }
semver = { workspace = true } semver = { workspace = true }
serde = { version = "1.0.130", features = ["derive"] } serde = { version = "1.0.130", features = ["derive"] }
@ -71,7 +71,6 @@ symlink = "0.1.0"
syn = { workspace = true } syn = { workspace = true }
sysinfo = "0.26.2" sysinfo = "0.26.2"
tar = "0.4.37" tar = "0.4.37"
tokio-tar = "0.3.1"
tempfile = "3.2.0" tempfile = "3.2.0"
tokio = { workspace = true } tokio = { workspace = true }
tokio-stream = { workspace = true } tokio-stream = { workspace = true }

View File

@ -1,9 +1,7 @@
use crate::prelude::*; use crate::prelude::*;
use async_compression::tokio::bufread::GzipDecoder; use flate2::read::GzDecoder;
use tokio::io::AsyncRead; use std::fs::File;
use tokio::io::BufReader;
use tokio_tar::Archive as Tar;
@ -18,19 +16,52 @@ pub struct Archive {
/// The path that the `file` originated from. This is stored for error reporting. /// The path that the `file` originated from. This is stored for error reporting.
path: Box<Path>, path: Box<Path>,
#[derivative(Debug = "ignore")] #[derivative(Debug = "ignore")]
file: Tar<Box<dyn AsyncRead + Unpin + Send>>, file: tar::Archive<GzDecoder<File>>,
} }
impl Archive { impl Archive {
/// Open a gzip-compressed tar archive. /// Open a gzip-compressed tar archive.
#[context("Failed to open archive: {}", path.as_ref().display())]
pub async fn open_tar_gz(path: impl AsRef<Path>) -> Result<Self> { pub async fn open_tar_gz(path: impl AsRef<Path>) -> Result<Self> {
let file = crate::fs::tokio::open(&path).await?; let file = crate::fs::tokio::open(&path).await?;
let file = BufReader::new(file); let file = file
let file = GzipDecoder::new(file); .try_into_std()
let file: Box<dyn AsyncRead + Unpin + Send> = Box::new(file); .map_err(|_| anyhow!("Failed to convert tokio::fs::File to std::fs::File"))?;
let file = Tar::new(file); let tar_stream = flate2::read::GzDecoder::new(file);
let archive = tar::Archive::new(tar_stream);
let path = path.as_ref().to_owned().into_boxed_path(); let path = path.as_ref().to_owned().into_boxed_path();
Ok(Self { path, file }) Ok(Self { path, file: archive })
}
/// Synchronous version of [`extract_files`].
#[context("Failed to extract files from archive {}", self.path.display())]
pub fn extract_files_sync(
mut self,
mut filter: impl FnMut(&Path) -> Option<PathBuf>,
) -> Result {
let entries = self.file.entries()?;
for entry in entries {
let mut entry = entry?;
let path_in_archive = entry.path()?;
if let Some(output_path) = filter(&path_in_archive) {
let entry_type = entry.header().entry_type();
let make_message = |prefix, path: Cow<Path>| {
format!(
"{} {:?} entry: {} => {}",
prefix,
entry_type,
path.display(),
output_path.display()
)
};
trace!("{}", make_message("Extracting", path_in_archive));
entry.unpack(&output_path).with_context(|| {
make_message("Failed to extract", entry.path().unwrap_or_default())
})?;
}
}
Ok(())
} }
/// The given function will be called with the path of each file within the archive. For each /// The given function will be called with the path of each file within the archive. For each
@ -39,21 +70,9 @@ impl Archive {
/// IMPORTANT: If the function uses its input path to generate an output path, care must be /// IMPORTANT: If the function uses its input path to generate an output path, care must be
/// taken that the output path is not in an unexpected location, especially if coming from an /// taken that the output path is not in an unexpected location, especially if coming from an
/// untrusted archive. /// untrusted archive.
#[context("Failed to extract files from archive: {}", self.path.display())] pub async fn extract_files(self, filter: impl FnMut(&Path) -> Option<PathBuf>) -> Result {
pub async fn extract_files( let job = move || self.extract_files_sync(filter);
mut self, tokio::task::block_in_place(job)
mut filter: impl FnMut(&Path) -> Option<PathBuf>,
) -> Result {
let mut entries = self.file.entries()?;
while let Some(entry) = entries.next().await {
let mut entry = entry?;
let path_in_archive = entry.path()?;
if let Some(output_path) = filter(&path_in_archive) {
trace!("Extracting {}", output_path.display());
entry.unpack(&output_path).await?;
}
}
Ok(())
} }
/// Extract all files from the specified subtree in the archive, placing them in the specified /// Extract all files from the specified subtree in the archive, placing them in the specified
@ -63,6 +82,13 @@ impl Archive {
prefix: impl AsRef<Path>, prefix: impl AsRef<Path>,
output: impl AsRef<Path>, output: impl AsRef<Path>,
) -> Result { ) -> Result {
let path = self.path.clone();
debug!(
"Extracting subtree '{}' from archive {} to {}",
prefix.as_ref().display(),
self.path.display(),
output.as_ref().display()
);
self.extract_files(|path_in_archive| { self.extract_files(|path_in_archive| {
path_in_archive path_in_archive
.strip_prefix(&prefix) .strip_prefix(&prefix)
@ -70,5 +96,13 @@ impl Archive {
.map(|relative_path| output.as_ref().join(relative_path)) .map(|relative_path| output.as_ref().join(relative_path))
}) })
.await .await
.with_context(|| {
format!(
"Failed to extract subtree '{}' from archive {} to {}",
prefix.as_ref().display(),
path.display(),
output.as_ref().display()
)
})
} }
} }

View File

@ -86,3 +86,9 @@ pub async fn read<P: AsRef<Path>>(path: P) -> Result<Vec<u8>> {
pub async fn read_to_string(path: impl AsRef<Path>) -> Result<String> { pub async fn read_to_string(path: impl AsRef<Path>) -> Result<String> {
tokio::fs::read_to_string(&path).await.anyhow_err() tokio::fs::read_to_string(&path).await.anyhow_err()
} }
/// See [`tokio::fs::set_permissions`].
#[context("Failed to set permissions {:?} for file: {}", permissions, path.as_ref().display())]
pub async fn set_permissions(path: impl AsRef<Path>, permissions: std::fs::Permissions) -> Result {
tokio::fs::set_permissions(&path, permissions.clone()).await.anyhow_err()
}

View File

@ -31,6 +31,7 @@ pub use ttf::Weight;
pub use ttf::Width; pub use ttf::Width;
// ================= // =================
// === Constants === // === Constants ===
// ================= // =================