public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pve-devel@lists.proxmox.com, pbs-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH proxmox 2/2] proxmox-compression: add 'tar_directory'
Date: Tue, 31 May 2022 13:17:21 +0200	[thread overview]
Message-ID: <20220531111726.2972022-3-d.csapak@proxmox.com> (raw)
In-Reply-To: <20220531111726.2972022-1-d.csapak@proxmox.com>

similar to 'zip_directory', this is intended to tar a local directory,
e.g. when we're in a restore vm.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 proxmox-compression/Cargo.toml |   1 +
 proxmox-compression/src/tar.rs | 116 +++++++++++++++++++++++++++++++++
 2 files changed, 117 insertions(+)

diff --git a/proxmox-compression/Cargo.toml b/proxmox-compression/Cargo.toml
index c5ad3fd..0618465 100644
--- a/proxmox-compression/Cargo.toml
+++ b/proxmox-compression/Cargo.toml
@@ -15,6 +15,7 @@ crc32fast = "1"
 endian_trait = { version = "0.6" }
 flate2 = "1.0"
 futures = "0.3"
+libc = "0.2"
 tokio = { version = "1.6", features = [ "fs", "io-util"] }
 walkdir = "2"
 tar = "0.4"
diff --git a/proxmox-compression/src/tar.rs b/proxmox-compression/src/tar.rs
index 7489e43..5aa4167 100644
--- a/proxmox-compression/src/tar.rs
+++ b/proxmox-compression/src/tar.rs
@@ -1,9 +1,12 @@
 //! tar helper
+use std::collections::HashMap;
 use std::io;
 use std::os::unix::ffi::OsStrExt;
 use std::path::{Component, Path, PathBuf};
 use std::str;
 
+use anyhow::Error;
+
 use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
 
 use tar::{EntryType, Header};
@@ -162,3 +165,116 @@ where
     }
     Ok(())
 }
+
+pub async fn tar_directory<W>(target: W, source: &Path) -> Result<(), Error>
+where
+    W: AsyncWrite + Unpin + Send,
+{
+    use std::os::unix::fs::{FileTypeExt, MetadataExt};
+    use walkdir::WalkDir;
+
+    let base_path = source.parent().unwrap_or_else(|| Path::new("/"));
+    let mut encoder = Builder::new(target);
+    let mut hardlinks: HashMap<u64, HashMap<u64, PathBuf>> = HashMap::new(); // dev -> inode -> first path
+
+    for entry in WalkDir::new(&source).into_iter() {
+        match entry {
+            Ok(entry) => {
+                let entry_path = entry.path().to_owned();
+                let encoder = &mut encoder;
+                let hardlinks = &mut hardlinks;
+
+                if let Err(err) = async move {
+                    let entry_path_no_base = entry.path().strip_prefix(base_path)?;
+                    let metadata = entry.metadata()?;
+                    let mut header = Header::new_gnu();
+                    header.set_mode(metadata.mode());
+                    header.set_mtime(metadata.mtime() as u64);
+                    header.set_uid(metadata.uid() as u64);
+                    header.set_gid(metadata.gid() as u64);
+                    header.set_size(0);
+                    let dev = metadata.dev();
+
+                    let file_type = entry.file_type();
+
+                    if file_type.is_file() {
+                        if metadata.nlink() > 1 {
+                            let ino = metadata.ino();
+                            if let Some(map) = hardlinks.get_mut(&dev) {
+                                if let Some(target) = map.get(&ino) {
+                                    header.set_entry_type(tar::EntryType::Link);
+                                    encoder
+                                        .add_link(&mut header, entry_path_no_base, target)
+                                        .await?;
+                                    return Ok(());
+                                } else {
+                                    map.insert(ino, entry_path_no_base.to_path_buf());
+                                }
+                            } else {
+                                let mut map = HashMap::new();
+                                map.insert(ino, entry_path_no_base.to_path_buf());
+                                hardlinks.insert(dev, map);
+                            }
+                        }
+                        let file = tokio::fs::File::open(entry.path()).await?;
+                        header.set_size(metadata.size());
+                        header.set_cksum();
+                        encoder
+                            .add_entry(&mut header, entry_path_no_base, file)
+                            .await?;
+                    } else if file_type.is_dir() {
+                        header.set_entry_type(EntryType::Directory);
+                        header.set_cksum();
+                        encoder
+                            .add_entry(&mut header, entry_path_no_base, tokio::io::empty())
+                            .await?;
+                    } else if file_type.is_symlink() {
+                        let target = std::fs::read_link(entry.path())?;
+                        header.set_entry_type(EntryType::Symlink);
+                        encoder
+                            .add_link(&mut header, entry_path_no_base, target)
+                            .await?;
+                    } else if file_type.is_block_device() {
+                        header.set_entry_type(EntryType::Block);
+                        header.set_device_major(unsafe { libc::major(dev) })?;
+                        header.set_device_minor(unsafe { libc::minor(dev) })?;
+                        header.set_cksum();
+                        encoder
+                            .add_entry(&mut header, entry_path_no_base, tokio::io::empty())
+                            .await?;
+                    } else if file_type.is_char_device() {
+                        header.set_entry_type(EntryType::Char);
+                        header.set_device_major(unsafe { libc::major(dev) })?;
+                        header.set_device_minor(unsafe { libc::minor(dev) })?;
+                        header.set_cksum();
+                        encoder
+                            .add_entry(&mut header, entry_path_no_base, tokio::io::empty())
+                            .await?;
+                    } else if file_type.is_fifo() {
+                        header.set_entry_type(EntryType::Fifo);
+                        header.set_device_major(0)?;
+                        header.set_device_minor(0)?;
+                        header.set_cksum();
+                        encoder
+                            .add_entry(&mut header, entry_path_no_base, tokio::io::empty())
+                            .await?;
+                    }
+                    // ignore other file_types
+                    Ok::<_, Error>(())
+                }
+                .await
+                {
+                    eprintln!(
+                        "zip: error encoding file or directory '{}': {}",
+                        entry_path.display(),
+                        err
+                    );
+                }
+            }
+            Err(err) => {
+                eprintln!("zip: error reading directory entry: {}", err);
+            }
+        }
+    }
+    Ok(())
+}
-- 
2.30.2





  parent reply	other threads:[~2022-05-31 11:18 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-31 11:17 [pve-devel] [PATCH proxmox/backup/common/storage/wt] add tar.zst download in pve Dominik Csapak
2022-05-31 11:17 ` [pve-devel] [PATCH proxmox 1/2] proxmox-compression: make ZstdEncoder stream a bit more generic Dominik Csapak
2022-07-05 11:47   ` [pve-devel] applied-both: " Wolfgang Bumiller
2022-05-31 11:17 ` Dominik Csapak [this message]
2022-05-31 11:17 ` [pve-devel] [PATCH proxmox-backup 1/2] restore-daemon: add 'format' parameter to the 'extract' handler Dominik Csapak
2022-07-05 11:39   ` [pve-devel] [pbs-devel] " Wolfgang Bumiller
2022-05-31 11:17 ` [pve-devel] [PATCH proxmox-backup 2/2] file-restore: add 'tar' option to 'extract' command Dominik Csapak
2022-07-05 11:43   ` [pve-devel] [pbs-devel] " Wolfgang Bumiller
2022-05-31 11:17 ` [pve-devel] [PATCH common 1/1] PBSClient: add 'tar' parameter to file_restore_extract Dominik Csapak
2022-05-31 11:17 ` [pve-devel] [PATCH storage 1/1] api/filerestore: add 'tar' parameter to 'download' api Dominik Csapak
2022-05-31 11:17 ` [pve-devel] [PATCH widget-toolkit 1/1] window/FileBrowser: enable tar button by default Dominik Csapak
2022-07-01 12:12 ` [pve-devel] [PATCH proxmox/backup/common/storage/wt] add tar.zst download in pve Dominik Csapak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220531111726.2972022-3-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal