[pbs-devel] [PATCH v3 proxmox 1/3] compression: Add a FileType enum to ZipEntry

Filip Schauer f.schauer at proxmox.com
Thu Dec 14 15:48:20 CET 2023


Specify the type of a ZipEntry with a new FileType enum
instead of a boolean is_file.

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

diff --git a/proxmox-compression/src/zip.rs b/proxmox-compression/src/zip.rs
index d2d3fd8..069e8bc 100644
--- a/proxmox-compression/src/zip.rs
+++ b/proxmox-compression/src/zip.rs
@@ -71,6 +71,11 @@ fn epoch_to_dos(epoch: i64) -> (u16, u16) {
     (date, time)
 }
 
+pub enum FileType {
+    Directory,
+    Regular,
+}
+
 #[derive(Endian)]
 #[repr(C, packed)]
 struct Zip64Field {
@@ -202,16 +207,16 @@ pub struct ZipEntry {
     uncompressed_size: u64,
     compressed_size: u64,
     offset: u64,
-    is_file: bool,
+    file_type: FileType,
     is_utf8_filename: bool,
 }
 
 impl ZipEntry {
     /// Creates a new ZipEntry
     ///
-    /// if is_file is false the path will contain an trailing separator,
+    /// if file is a directory the path will contain a trailing separator,
     /// so that the zip file understands that it is a directory
-    pub fn new<P: AsRef<Path>>(path: P, mtime: i64, mode: u16, is_file: bool) -> Self {
+    pub fn new<P: AsRef<Path>>(path: P, mtime: i64, mode: u16, file_type: FileType) -> Self {
         let mut relpath = PathBuf::new();
 
         for comp in path.as_ref().components() {
@@ -220,7 +225,7 @@ impl ZipEntry {
             }
         }
 
-        if !is_file {
+        if matches!(file_type, FileType::Directory) {
             relpath.push(""); // adds trailing slash
         }
 
@@ -235,7 +240,7 @@ impl ZipEntry {
             uncompressed_size: 0,
             compressed_size: 0,
             offset: 0,
-            is_file,
+            file_type,
             is_utf8_filename,
         }
     }
@@ -342,6 +347,11 @@ impl ZipEntry {
             )
         };
 
+        let file_type_attr = match self.file_type {
+            FileType::Regular => 0o100000,
+            FileType::Directory => 0o040000,
+        };
+
         write_struct(
             &mut buf,
             CentralDirectoryFileHeader {
@@ -360,7 +370,8 @@ impl ZipEntry {
                 comment_len: 0,
                 start_disk: 0,
                 internal_flags: 0,
-                external_flags: (self.mode as u32) << 16 | (!self.is_file as u32) << 4,
+                external_flags: (self.mode as u32 | file_type_attr) << 16
+                    | (matches!(self.file_type, FileType::Directory) as u32) << 4,
                 offset,
             },
         )
@@ -449,7 +460,7 @@ where
 ///         "foo.txt",
 ///         0,
 ///         0o100755,
-///         true,
+///         FileType::Regular,
 ///     ), Some(source)).await?;
 ///
 ///     zip.finish().await?;
@@ -503,6 +514,7 @@ impl<W: AsyncWrite + Unpin> ZipEncoder<W> {
 
             entry.crc32 = crc32;
         }
+
         self.byte_count += entry.write_data_descriptor(&mut target).await?;
         self.target = Some(target);
 
@@ -658,10 +670,10 @@ where
 
             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);
+                let ze = ZipEntry::new(entry_path_no_base, mtime, mode, FileType::Regular);
                 Ok(Some((ze, Some(file))))
             } else if entry.file_type().is_dir() {
-                let ze = ZipEntry::new(entry_path_no_base, mtime, mode, false);
+                let ze = ZipEntry::new(entry_path_no_base, mtime, mode, FileType::Directory);
                 let content: Option<tokio::fs::File> = None;
                 Ok(Some((ze, content)))
             } else {
-- 
2.39.2





More information about the pbs-devel mailing list