src/backup/data_{chunk,blob}.rs: add verify_crc method
This commit is contained in:
		@ -54,6 +54,15 @@ impl DataBlob {
 | 
				
			|||||||
        hasher.finalize()
 | 
					        hasher.finalize()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// verify the CRC32 checksum
 | 
				
			||||||
 | 
					    pub fn verify_crc(&self) -> Result<(), Error> {
 | 
				
			||||||
 | 
					        let expected_crc = self.compute_crc();
 | 
				
			||||||
 | 
					        if expected_crc != self.crc() {
 | 
				
			||||||
 | 
					            bail!("Data blob has wrong CRC checksum.");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        Ok(())
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn encode(
 | 
					    pub fn encode(
 | 
				
			||||||
        data: &[u8],
 | 
					        data: &[u8],
 | 
				
			||||||
        config: Option<&CryptConfig>,
 | 
					        config: Option<&CryptConfig>,
 | 
				
			||||||
 | 
				
			|||||||
@ -61,6 +61,15 @@ impl DataChunk {
 | 
				
			|||||||
        hasher.finalize()
 | 
					        hasher.finalize()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// verify the CRC32 checksum
 | 
				
			||||||
 | 
					    pub fn verify_crc(&self) -> Result<(), Error> {
 | 
				
			||||||
 | 
					        let expected_crc = self.compute_crc();
 | 
				
			||||||
 | 
					        if expected_crc != self.crc() {
 | 
				
			||||||
 | 
					            bail!("Data chunk has wrong CRC checksum.");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        Ok(())
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn encode(
 | 
					    fn encode(
 | 
				
			||||||
        data: &[u8],
 | 
					        data: &[u8],
 | 
				
			||||||
        config: Option<&CryptConfig>,
 | 
					        config: Option<&CryptConfig>,
 | 
				
			||||||
 | 
				
			|||||||
@ -708,6 +708,7 @@ fn download(
 | 
				
			|||||||
        let writer = Vec::with_capacity(1024*1024);
 | 
					        let writer = Vec::with_capacity(1024*1024);
 | 
				
			||||||
        let blob_data = client.download(&path, writer).wait()?;
 | 
					        let blob_data = client.download(&path, writer).wait()?;
 | 
				
			||||||
        let blob = DataBlob::from_raw(blob_data)?;
 | 
					        let blob = DataBlob::from_raw(blob_data)?;
 | 
				
			||||||
 | 
					        blob.verify_crc()?;
 | 
				
			||||||
        let raw_data = blob.decode(crypt_config.as_ref())?; // fixme
 | 
					        let raw_data = blob.decode(crypt_config.as_ref())?; // fixme
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        crate::tools::file_set_contents(target, &raw_data, None)?;
 | 
					        crate::tools::file_set_contents(target, &raw_data, None)?;
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user