public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Filip Schauer <f.schauer@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH v3 proxmox 2/3] compression: Add support for symlinks in zip files
Date: Thu, 14 Dec 2023 15:48:21 +0100	[thread overview]
Message-ID: <20231214144824.100616-3-f.schauer@proxmox.com> (raw)
In-Reply-To: <20231214144824.100616-1-f.schauer@proxmox.com>

Add support for symlinks to ZipEntry. A symlink is encoded by or-ing its
attributes with S_IFLNK as seen in the kernel in
include/uapi/linux/stat.h

Signed-off-by: Filip Schauer <f.schauer@proxmox.com>
---
 proxmox-compression/src/zip.rs | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)

diff --git a/proxmox-compression/src/zip.rs b/proxmox-compression/src/zip.rs
index 069e8bc..a3b2346 100644
--- a/proxmox-compression/src/zip.rs
+++ b/proxmox-compression/src/zip.rs
@@ -74,6 +74,7 @@ fn epoch_to_dos(epoch: i64) -> (u16, u16) {
 pub enum FileType {
     Directory,
     Regular,
+    Symlink(OsString),
 }
 
 #[derive(Endian)]
@@ -350,6 +351,7 @@ impl ZipEntry {
         let file_type_attr = match self.file_type {
             FileType::Regular => 0o100000,
             FileType::Directory => 0o040000,
+            FileType::Symlink(_) => 0o120000,
         };
 
         write_struct(
@@ -497,22 +499,28 @@ impl<W: AsyncWrite + Unpin> ZipEncoder<W> {
             .ok_or_else(|| format_err!("had no target during add entry"))?;
         entry.offset = self.byte_count.try_into()?;
         self.byte_count += entry.write_local_header(&mut target).await?;
-        if let Some(content) = content {
-            let mut reader = HashWrapper::new(content);
+
+        if content.is_some() || matches!(entry.file_type, FileType::Symlink(_)) {
             let mut enc = DeflateEncoder::with_quality(target, Level::Fastest);
 
-            enc.compress(&mut reader).await?;
+            if let Some(content) = content {
+                let mut reader = HashWrapper::new(content);
+                enc.compress(&mut reader).await?;
+                entry.crc32 = reader.finish().0;
+            } else if let FileType::Symlink(symlink_target) = &entry.file_type {
+                let cursor = std::io::Cursor::new(symlink_target.as_bytes());
+                let mut reader = HashWrapper::new(cursor);
+                enc.compress(&mut reader).await?;
+                entry.crc32 = reader.finish().0;
+            }
+
             let total_in = enc.total_in();
             let total_out = enc.total_out();
             target = enc.into_inner();
 
-            let (crc32, _reader) = reader.finish();
-
             self.byte_count += total_out as usize;
             entry.compressed_size = total_out;
             entry.uncompressed_size = total_in;
-
-            entry.crc32 = crc32;
         }
 
         self.byte_count += entry.write_data_descriptor(&mut target).await?;
@@ -676,6 +684,16 @@ where
                 let ze = ZipEntry::new(entry_path_no_base, mtime, mode, FileType::Directory);
                 let content: Option<tokio::fs::File> = None;
                 Ok(Some((ze, content)))
+            } else if entry.file_type().is_symlink() {
+                let target = std::fs::read_link(entry.path())?;
+                let ze = ZipEntry::new(
+                    entry_path_no_base,
+                    mtime,
+                    mode,
+                    FileType::Symlink(target.into()),
+                );
+                let content: Option<tokio::fs::File> = None;
+                Ok(Some((ze, content)))
             } else {
                 // ignore other file types
                 Ok::<_, Error>(None)
-- 
2.39.2





  parent reply	other threads:[~2023-12-14 14:49 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-14 14:48 [pbs-devel] [PATCH v3 many] fix #4995: Include symlinks in zip file restore Filip Schauer
2023-12-14 14:48 ` [pbs-devel] [PATCH v3 proxmox 1/3] compression: Add a FileType enum to ZipEntry Filip Schauer
2023-12-14 14:48 ` Filip Schauer [this message]
2023-12-20 13:20   ` [pbs-devel] [PATCH v3 proxmox 2/3] compression: Add support for symlinks in zip files Wolfgang Bumiller
2023-12-21 11:37     ` Filip Schauer
2023-12-21 12:03       ` Wolfgang Bumiller
2023-12-21 12:15         ` Filip Schauer
2023-12-21 12:11       ` Wolfgang Bumiller
2024-01-24 10:19         ` Filip Schauer
2023-12-14 14:48 ` [pbs-devel] [PATCH v3 proxmox 3/3] compression: Add unit tests for the ZipEncoder Filip Schauer
2023-12-14 14:48 ` [pbs-devel] [PATCH v3 backup 1/2] pxar: Adopt FileType enum when creating a ZipEntry Filip Schauer
2023-12-14 14:48 ` [pbs-devel] [PATCH v3 backup 2/2] fix #4995: pxar: Include symlinks in zip file creation Filip Schauer

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=20231214144824.100616-3-f.schauer@proxmox.com \
    --to=f.schauer@proxmox.com \
    --cc=pbs-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