[pbs-devel] [PATCH v4 proxmox 2/3] compression: Add support for symlinks in zip files

Filip Schauer f.schauer at proxmox.com
Wed Jan 24 11:15:16 CET 2024


Signed-off-by: Filip Schauer <f.schauer at proxmox.com>
---
 proxmox-compression/src/zip.rs | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/proxmox-compression/src/zip.rs b/proxmox-compression/src/zip.rs
index 6229990..aec11a4 100644
--- a/proxmox-compression/src/zip.rs
+++ b/proxmox-compression/src/zip.rs
@@ -17,7 +17,7 @@ use std::time::SystemTime;
 use anyhow::{format_err, Error, Result};
 use endian_trait::Endian;
 use futures::ready;
-use libc::{S_IFDIR, S_IFREG};
+use libc::{S_IFDIR, S_IFLNK, S_IFREG};
 use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt, ReadBuf};
 
 use crc32fast::Hasher;
@@ -75,6 +75,7 @@ fn epoch_to_dos(epoch: i64) -> (u16, u16) {
 pub enum FileType<R> {
     Directory,
     Regular(R),
+    Symlink(OsString),
 }
 
 impl<R: AsyncRead + Unpin + Send> FileType<R> {
@@ -82,6 +83,12 @@ impl<R: AsyncRead + Unpin + Send> FileType<R> {
         match self {
             FileType::Directory => None,
             FileType::Regular(content) => Some(HashWrapper::new(content)),
+            FileType::Symlink(symlink_target) => {
+                let cursor = std::io::Cursor::new(symlink_target.as_bytes());
+                let reader_box = Box::new(cursor) as Box<dyn AsyncRead + Unpin + Send>;
+                let leaked_ref = Box::leak(reader_box);
+                Some(HashWrapper::new(leaked_ref))
+            }
         }
     }
 }
@@ -247,6 +254,7 @@ impl ZipEntry {
         let file_type_attr = match file_type {
             FileType::Regular(_) => S_IFREG,
             FileType::Directory => S_IFDIR,
+            FileType::Symlink(_) => S_IFLNK,
         };
 
         Self {
@@ -699,6 +707,16 @@ where
                     FileType::<&[u8]>::Directory,
                 )
                 .await?;
+        } else if entry.file_type().is_symlink() {
+            let target = std::fs::read_link(entry.path())?;
+            encoder
+                .add_entry(
+                    entry_path_no_base,
+                    mtime,
+                    mode,
+                    FileType::<io::Cursor<&[u8]>>::Symlink(target.into()),
+                )
+                .await?;
         }
     }
 
-- 
2.39.2





More information about the pbs-devel mailing list