[pbs-devel] [RFC pxar 4/20] fix #3174: metadata: impl fn to calc byte size

Christian Ebner c.ebner at proxmox.com
Fri Sep 22 09:16:05 CEST 2023


Add a helper function to calculate the byte size of pxar Metadata
objects, needed to be able to recalculate offsets when creating archives
with appendix sections.

Signed-off-by: Christian Ebner <c.ebner at proxmox.com>
---
 src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/src/lib.rs b/src/lib.rs
index 210c4b1..ed7ba40 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -144,6 +144,50 @@ impl Metadata {
     pub fn builder_from_stat(stat: &libc::stat) -> MetadataBuilder {
         MetadataBuilder::new(0).fill_from_stat(stat)
     }
+
+    /// Calculate the number of bytes when serialized in pxar archive
+    pub fn calculate_byte_len(&self) -> usize {
+        let mut bytes = mem::size_of::<format::Header>();
+        bytes += mem::size_of_val(&self.stat);
+        for xattr in &self.xattrs {
+            bytes += mem::size_of::<format::Header>();
+            bytes += mem::size_of_val(xattr);
+        }
+        for acl_user in &self.acl.users {
+            bytes += mem::size_of::<format::Header>();
+            bytes += mem::size_of_val(acl_user);
+        }
+        for acl_group in &self.acl.groups {
+            bytes += mem::size_of::<format::Header>();
+            bytes += mem::size_of_val(acl_group);
+        }
+        if let Some(group_obj) = &self.acl.group_obj {
+            bytes += mem::size_of::<format::Header>();
+            bytes += mem::size_of_val(group_obj);
+        }
+        if let Some(default) = &self.acl.default {
+            bytes += mem::size_of::<format::Header>();
+            bytes += mem::size_of_val(default);
+        }
+        for acl_default_user in &self.acl.default_users {
+            bytes += mem::size_of::<format::Header>();
+            bytes += mem::size_of_val(acl_default_user);
+        }
+        for acl_default_group in &self.acl.default_groups {
+            bytes += mem::size_of::<format::Header>();
+            bytes += mem::size_of_val(acl_default_group);
+        }
+        if let Some(fcaps) = &self.fcaps {
+            bytes += mem::size_of::<format::Header>();
+            bytes += mem::size_of_val(fcaps);
+        }
+        if let Some(quota_project_id) = &self.quota_project_id {
+            bytes += mem::size_of::<format::Header>();
+            bytes += mem::size_of_val(quota_project_id);
+        }
+
+        bytes
+    }
 }
 
 impl From<MetadataBuilder> for Metadata {
-- 
2.39.2






More information about the pbs-devel mailing list