[pbs-devel] [PATCH proxmox-backup 1/1] pxar/extract: if possible create files sparesly

Dominik Csapak d.csapak at proxmox.com
Mon Dec 14 14:41:47 CET 2020


instead of filling them with zeroes

this fixes an issue where we could not restore a container with large
sparse files in the backup (e.g. a 10GiB sparse file in a container
with a 8GiB disk)

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 src/pxar/extract.rs | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)

diff --git a/src/pxar/extract.rs b/src/pxar/extract.rs
index ed238a2c..dd084ead 100644
--- a/src/pxar/extract.rs
+++ b/src/pxar/extract.rs
@@ -18,7 +18,10 @@ use pxar::format::Device;
 use pxar::Metadata;
 
 use proxmox::c_result;
-use proxmox::tools::fs::{create_path, CreateOptions};
+use proxmox::tools::{
+    fs::{create_path, CreateOptions},
+    io::{sparse_copy, sparse_copy_async}
+};
 
 use crate::pxar::dir_stack::PxarDirStack;
 use crate::pxar::metadata;
@@ -392,6 +395,11 @@ impl Extractor {
             )
         };
 
+        let copy_sparse = match nix::unistd::ftruncate(file.as_raw_fd(), size as i64) {
+            Ok(_) => true,
+            Err(_) => false,
+        };
+
         metadata::apply_initial_flags(
             self.feature_flags,
             metadata,
@@ -399,8 +407,12 @@ impl Extractor {
             &mut self.on_error,
         )?;
 
-        let extracted = io::copy(&mut *contents, &mut file)
-            .map_err(|err| format_err!("failed to copy file contents: {}", err))?;
+        let extracted = if copy_sparse {
+            sparse_copy(&mut *contents, &mut file)
+        } else {
+            io::copy(&mut *contents, &mut file)
+        }.map_err(|err| format_err!("failed to copy file contents: {}", err))?;
+
         if size != extracted {
             bail!("extracted {} bytes of a file of {} bytes", extracted, size);
         }
@@ -434,6 +446,11 @@ impl Extractor {
             )
         });
 
+        let copy_sparse = match nix::unistd::ftruncate(file.as_raw_fd(), size as i64) {
+            Ok(_) => true,
+            Err(_) => false,
+        };
+
         metadata::apply_initial_flags(
             self.feature_flags,
             metadata,
@@ -441,9 +458,12 @@ impl Extractor {
             &mut self.on_error,
         )?;
 
-        let extracted = tokio::io::copy(&mut *contents, &mut file)
-            .await
-            .map_err(|err| format_err!("failed to copy file contents: {}", err))?;
+        let extracted = if copy_sparse {
+            sparse_copy_async(&mut *contents, &mut file).await
+        } else {
+            tokio::io::copy(&mut *contents, &mut file).await
+        }.map_err(|err| format_err!("failed to copy file contents: {}", err))?;
+
         if size != extracted {
             bail!("extracted {} bytes of a file of {} bytes", extracted, size);
         }
-- 
2.20.1






More information about the pbs-devel mailing list