public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: "Fabian Grünbichler" <f.gruenbichler@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox-backup 1/3] debug: recover: allow ignoring missing/corrupt chunks
Date: Mon, 23 May 2022 16:11:33 +0200	[thread overview]
Message-ID: <20220523141135.921321-1-f.gruenbichler@proxmox.com> (raw)

replacing them with chunks of zero bytes.

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
---
 src/bin/proxmox_backup_debug/recover.rs | 98 +++++++++++++++++++++----
 1 file changed, 84 insertions(+), 14 deletions(-)

diff --git a/src/bin/proxmox_backup_debug/recover.rs b/src/bin/proxmox_backup_debug/recover.rs
index b118a71c..9c4aed3d 100644
--- a/src/bin/proxmox_backup_debug/recover.rs
+++ b/src/bin/proxmox_backup_debug/recover.rs
@@ -38,7 +38,19 @@ use pbs_tools::crypt_config::CryptConfig;
                 type: Boolean,
                 optional: true,
                 default: false,
-            }
+            },
+            "ignore-missing-chunks": {
+                description: "If a chunk is missing, warn and write 0-bytes instead to attempt partial recovery.",
+                type: Boolean,
+                optional: true,
+                default: false,
+            },
+            "ignore-corrupt-chunks": {
+                description: "If a chunk is corrupt, warn and write 0-bytes instead to attempt partial recovery.",
+                type: Boolean,
+                optional: true,
+                default: false,
+            },
         }
     }
 )]
@@ -49,6 +61,8 @@ fn recover_index(
     chunks: String,
     keyfile: Option<String>,
     skip_crc: bool,
+    ignore_missing_chunks: bool,
+    ignore_corrupt_chunks: bool,
     _param: Value,
 ) -> Result<(), Error> {
     let file_path = Path::new(&file);
@@ -89,22 +103,78 @@ fn recover_index(
         let digest_str = hex::encode(chunk_digest);
         let digest_prefix = &digest_str[0..4];
         let chunk_path = chunks_path.join(digest_prefix).join(digest_str);
-        let mut chunk_file = std::fs::File::open(&chunk_path)
-            .map_err(|e| format_err!("could not open chunk file - {}", e))?;
 
-        data.clear();
-        chunk_file.read_to_end(&mut data)?;
-        let chunk_blob = DataBlob::from_raw(data.clone())?;
+        let create_zero_chunk = |msg: String| -> Result<(DataBlob, Option<&[u8; 32]>), Error> {
+            let info = index
+                .chunk_info(pos)
+                .ok_or_else(|| format_err!("Couldn't read chunk info from index at {pos}"))?;
+            let size = info.size();
 
-        if !skip_crc {
-            chunk_blob.verify_crc()?;
-        }
+            eprintln!("WARN: chunk {:?} {}", chunk_path, msg);
+            eprintln!("WARN: replacing output file {:?} with '\\0'", info.range,);
+
+            Ok((
+                DataBlob::encode(&vec![0; size as usize], crypt_conf_opt.as_ref(), true)?,
+                None,
+            ))
+        };
+
+        let (chunk_blob, chunk_digest) = match std::fs::File::open(&chunk_path) {
+            Ok(mut chunk_file) => {
+                data.clear();
+                chunk_file.read_to_end(&mut data)?;
+
+                // first chance for corrupt chunk - handling magic fails
+                DataBlob::from_raw(data.clone())
+                    .map(|blob| (blob, Some(chunk_digest)))
+                    .or_else(|err| {
+                        if ignore_corrupt_chunks {
+                            create_zero_chunk(format!("is corrupt - {err}"))
+                        } else {
+                            bail!("{err}");
+                        }
+                    })?
+            }
+            Err(err) => {
+                if ignore_missing_chunks && err.kind() == std::io::ErrorKind::NotFound {
+                    create_zero_chunk(format!("is missing"))?
+                } else {
+                    bail!("could not open chunk file - {}", err);
+                }
+            }
+        };
+
+        // second chance - we need CRC to detect truncated chunks!
+        let crc_res = if skip_crc {
+            Ok(())
+        } else {
+            chunk_blob.verify_crc()
+        };
+
+        let (chunk_blob, chunk_digest) = if let Err(crc_err) = crc_res {
+            if ignore_corrupt_chunks {
+                create_zero_chunk(format!("is corrupt - {crc_err}"))?
+            } else {
+                bail!("Error at chunk {:?} - {crc_err}", chunk_path);
+            }
+        } else {
+            (chunk_blob, chunk_digest)
+        };
+
+        // third chance - decoding might fail (digest, compression, encryption)
+        let decoded = chunk_blob
+            .decode(crypt_conf_opt.as_ref(), chunk_digest)
+            .or_else(|err| {
+                if ignore_corrupt_chunks {
+                    create_zero_chunk(format!("fails to decode - {err}"))?
+                        .0
+                        .decode(crypt_conf_opt.as_ref(), None)
+                } else {
+                    bail!("Failed to decode chunk {:?} = {}", chunk_path, err);
+                }
+            })?;
 
-        output_file.write_all(
-            chunk_blob
-                .decode(crypt_conf_opt.as_ref(), Some(chunk_digest))?
-                .as_slice(),
-        )?;
+        output_file.write_all(decoded.as_slice())?;
     }
 
     Ok(())
-- 
2.30.2





             reply	other threads:[~2022-05-23 14:12 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-23 14:11 Fabian Grünbichler [this message]
2022-05-23 14:11 ` [pbs-devel] [PATCH proxmox-backup 2/3] debug: move outfile_or_stdout to module for reuse Fabian Grünbichler
2022-05-23 14:11 ` [pbs-devel] [PATCH proxmox-backup 3/3] debug: recover: allow overriding output-path Fabian Grünbichler
2022-05-24  9:16 ` [pbs-devel] [PATCH proxmox-backup 1/3] debug: recover: allow ignoring missing/corrupt chunks Hannes Laimer
2022-05-24  9:47 ` [pbs-devel] applied-series: " Thomas Lamprecht

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=20220523141135.921321-1-f.gruenbichler@proxmox.com \
    --to=f.gruenbichler@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