From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id DA982DA99 for ; Tue, 19 Apr 2022 12:29:00 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id CF11826CF0 for ; Tue, 19 Apr 2022 12:28:30 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 0788C26CE7 for ; Tue, 19 Apr 2022 12:28:30 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id D3DE641FD2 for ; Tue, 19 Apr 2022 12:28:29 +0200 (CEST) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Tue, 19 Apr 2022 12:28:27 +0200 Message-Id: <20220419102828.2847367-1-d.csapak@proxmox.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.137 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pbs-devel] [PATCH proxmox-backup 1/2] pbs-client: extract: rewrite create_zip with sequential decoder X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 19 Apr 2022 10:29:00 -0000 instead of an async recursive function. Not only is it less code, recursive futures are not really nice and it should be faster too. Signed-off-by: Dominik Csapak --- pbs-client/src/pxar/extract.rs | 154 ++++++++++++++++----------------- 1 file changed, 74 insertions(+), 80 deletions(-) diff --git a/pbs-client/src/pxar/extract.rs b/pbs-client/src/pxar/extract.rs index 7898e255..90ce88bd 100644 --- a/pbs-client/src/pxar/extract.rs +++ b/pbs-client/src/pxar/extract.rs @@ -7,11 +7,9 @@ use std::io; use std::os::unix::ffi::OsStrExt; use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; use std::path::{Path, PathBuf}; -use std::pin::Pin; use std::sync::{Arc, Mutex}; use anyhow::{bail, format_err, Error}; -use futures::future::Future; use nix::dir::Dir; use nix::fcntl::OFlag; use nix::sys::stat::Mode; @@ -699,7 +697,7 @@ where pub async fn create_zip( output: W, - decoder: Accessor, + accessor: Accessor, path: P, verbose: bool, ) -> Result<(), Error> @@ -708,7 +706,7 @@ where W: tokio::io::AsyncWrite + Unpin + Send + 'static, P: AsRef, { - let root = decoder.open_root().await?; + let root = accessor.open_root().await?; let file = root .lookup(&path) .await? @@ -720,88 +718,84 @@ where components.as_path().to_owned() }; - let mut zipencoder = ZipEncoder::new(output); - let mut decoder = decoder; - recurse_files_zip(&mut zipencoder, &mut decoder, &prefix, file, verbose) - .await - .map_err(|err| { - eprintln!("error during creating of zip: {}", err); - err - })?; + let mut zip = ZipEncoder::new(output); - zipencoder.finish().await.map_err(|err| { - eprintln!("error during finishing of zip: {}", err); - err - }) -} + if let Ok(dir) = file.enter_directory().await { + let entry = dir.lookup_self().await?; + let path = entry.path().strip_prefix(&prefix)?; + if path != Path::new("/") { + let metadata = entry.metadata(); + let entry = ZipEntry::new( + path, + metadata.stat.mtime.secs, + metadata.stat.mode as u16, + false, + ); + zip.add_entry::>(entry, None).await?; + } -fn recurse_files_zip<'a, T, W>( - zip: &'a mut ZipEncoder, - decoder: &'a mut Accessor, - prefix: &'a Path, - file: FileEntry, - verbose: bool, -) -> Pin> + Send + 'a>> -where - T: Clone + pxar::accessor::ReadAt + Unpin + Send + Sync + 'static, - W: tokio::io::AsyncWrite + Unpin + Send + 'static, -{ - Box::pin(async move { - let metadata = file.entry().metadata(); - let path = file.entry().path().strip_prefix(&prefix)?; - - match file.kind() { - EntryKind::File { .. } => { - if verbose { - eprintln!("adding '{}' to zip", path.display()); - } - let entry = ZipEntry::new( - path, - metadata.stat.mtime.secs, - metadata.stat.mode as u16, - true, - ); - zip.add_entry(entry, Some(file.contents().await?)) - .await - .map_err(|err| format_err!("could not send file entry: {}", err))?; - } - EntryKind::Hardlink(_) => { - let realfile = decoder.follow_hardlink(&file).await?; - if verbose { - eprintln!("adding '{}' to zip", path.display()); + let mut decoder = dir.decode_full().await?; + decoder.enable_goodbye_entries(false); + while let Some(entry) = decoder.next().await { + let entry = entry?; + let metadata = entry.metadata(); + let path = entry.path().strip_prefix(&prefix)?; + + match entry.kind() { + EntryKind::File { .. } => { + if verbose { + eprintln!("adding '{}' to zip", path.display()); + } + let entry = ZipEntry::new( + path, + metadata.stat.mtime.secs, + metadata.stat.mode as u16, + true, + ); + zip.add_entry(entry, decoder.contents()) + .await + .map_err(|err| format_err!("could not send file entry: {}", err))?; } - let entry = ZipEntry::new( - path, - metadata.stat.mtime.secs, - metadata.stat.mode as u16, - true, - ); - zip.add_entry(entry, Some(realfile.contents().await?)) - .await - .map_err(|err| format_err!("could not send file entry: {}", err))?; - } - EntryKind::Directory => { - let dir = file.enter_directory().await?; - let mut readdir = dir.read_dir(); - if verbose { - eprintln!("adding '{}' to zip", path.display()); + EntryKind::Hardlink(_) => { + let entry = root + .lookup(&path) + .await? + .ok_or(format_err!("error looking up '{:?}'", path))?; + let realfile = accessor.follow_hardlink(&entry).await?; + let metadata = realfile.entry().metadata(); + if verbose { + eprintln!("adding '{}' to zip", path.display()); + } + let entry = ZipEntry::new( + path, + metadata.stat.mtime.secs, + metadata.stat.mode as u16, + true, + ); + zip.add_entry(entry, decoder.contents()) + .await + .map_err(|err| format_err!("could not send file entry: {}", err))?; } - let entry = ZipEntry::new( - path, - metadata.stat.mtime.secs, - metadata.stat.mode as u16, - false, - ); - zip.add_entry::>(entry, None).await?; - while let Some(entry) = readdir.next().await { - let entry = entry?.decode_entry().await?; - recurse_files_zip(zip, decoder, prefix, entry, verbose).await?; + EntryKind::Directory => { + if verbose { + eprintln!("adding '{}' to zip", path.display()); + } + let entry = ZipEntry::new( + path, + metadata.stat.mtime.secs, + metadata.stat.mode as u16, + false, + ); + zip.add_entry::>(entry, None).await?; } - } - _ => {} // ignore all else - }; + _ => {} // ignore all else + }; + } + } - Ok(()) + zip.finish().await.map_err(|err| { + eprintln!("error during finishing of zip: {}", err); + err }) } -- 2.30.2