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 17F586CFA0 for ; Wed, 31 Mar 2021 12:23:02 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id E8E2BDF29 for ; Wed, 31 Mar 2021 12:22:30 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (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 DE898DE79 for ; Wed, 31 Mar 2021 12:22:26 +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 AE6A442942 for ; Wed, 31 Mar 2021 12:22:26 +0200 (CEST) From: Stefan Reiter To: pbs-devel@lists.proxmox.com Date: Wed, 31 Mar 2021 12:22:01 +0200 Message-Id: <20210331102202.14767-20-s.reiter@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210331102202.14767-1-s.reiter@proxmox.com> References: <20210331102202.14767-1-s.reiter@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.019 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [zip.rs] Subject: [pbs-devel] [PATCH v3 proxmox-backup 19/20] tools/zip: add zip_directory helper 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: Wed, 31 Mar 2021 10:23:02 -0000 Encodes an entire local directory into an AsyncWrite recursively. Signed-off-by: Stefan Reiter --- src/tools/zip.rs | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/src/tools/zip.rs b/src/tools/zip.rs index 55f2a24a..d1a98485 100644 --- a/src/tools/zip.rs +++ b/src/tools/zip.rs @@ -10,6 +10,7 @@ use std::io; use std::mem::size_of; use std::os::unix::ffi::OsStrExt; use std::path::{Component, Path, PathBuf}; +use std::time::SystemTime; use anyhow::{Error, Result}; use endian_trait::Endian; @@ -537,3 +538,79 @@ impl ZipEncoder { Ok(()) } } + +/// Zip a local directory and write encoded data to target. "source" has to point to a valid +/// directory, it's name will be the root of the zip file - e.g.: +/// source: +/// /foo/bar +/// zip file: +/// /bar/file1 +/// /bar/dir1 +/// /bar/dir1/file2 +/// ... +/// ...except if "source" is the root directory +pub async fn zip_directory(target: W, source: &Path) -> Result<(), Error> +where + W: AsyncWrite + Unpin + Send, +{ + use walkdir::WalkDir; + use std::os::unix::fs::MetadataExt; + + let base_path = source.parent().unwrap_or_else(|| Path::new("/")); + let mut encoder = ZipEncoder::new(target); + + for entry in WalkDir::new(&source).into_iter() { + match entry { + Ok(entry) => { + let entry_path = entry.path().to_owned(); + let encoder = &mut encoder; + + if let Err(err) = async move { + let entry_path_no_base = entry.path().strip_prefix(base_path)?; + let metadata = entry.metadata()?; + let mtime = match metadata.modified().unwrap_or_else(|_| SystemTime::now()).duration_since(SystemTime::UNIX_EPOCH) { + Ok(dur) => dur.as_secs() as i64, + Err(time_error) => -(time_error.duration().as_secs() as i64) + }; + let mode = metadata.mode() as u16; + + if entry.file_type().is_file() { + let file = tokio::fs::File::open(entry.path()).await?; + let ze = ZipEntry::new( + &entry_path_no_base, + mtime, + mode, + true, + ); + encoder.add_entry(ze, Some(file)).await?; + } else if entry.file_type().is_dir() { + let ze = ZipEntry::new( + &entry_path_no_base, + mtime, + mode, + false, + ); + let content: Option = None; + encoder.add_entry(ze, content).await?; + } + // ignore other file types + let ok: Result<(), Error> = Ok(()); + ok + } + .await + { + eprintln!( + "zip: error encoding file or directory '{}': {}", + entry_path.display(), + err + ); + } + } + Err(err) => { + eprintln!("zip: error reading directory entry: {}", err); + } + } + } + + encoder.finish().await +} -- 2.20.1