src/client/backup_reader.rs: factor out download_dynamic_index() helper

This commit is contained in:
Dietmar Maurer 2019-11-08 12:40:10 +01:00
parent 84677c7e8a
commit c3d84a2281
2 changed files with 32 additions and 35 deletions

View File

@ -485,22 +485,9 @@ fn dump_catalog(
true, true,
).await?; ).await?;
let tmpfile = std::fs::OpenOptions::new()
.write(true)
.read(true)
.custom_flags(libc::O_TMPFILE)
.open("/tmp")?;
let manifest = client.download_manifest().await?; let manifest = client.download_manifest().await?;
let tmpfile = client.download(CATALOG_NAME, tmpfile).await?; let index = client.download_dynamic_index(&manifest, CATALOG_NAME).await?;
let index = DynamicIndexReader::new(tmpfile)
.map_err(|err| format_err!("unable to read catalog index - {}", err))?;
// Note: do not use values stored in index (not trusted) - instead, computed them again
let (csum, size) = index.compute_csum();
manifest.verify_file(CATALOG_NAME, &csum, size)?;
let most_used = index.find_most_used_chunks(8); let most_used = index.find_most_used_chunks(8);
@ -1077,14 +1064,8 @@ async fn restore_do(param: Value) -> Result<Value, Error> {
} }
} else if server_archive_name.ends_with(".didx") { } else if server_archive_name.ends_with(".didx") {
let tmpfile = client.download(&server_archive_name, tmpfile).await?;
let index = DynamicIndexReader::new(tmpfile) let index = client.download_dynamic_index(&manifest, &server_archive_name).await?;
.map_err(|err| format_err!("unable to read dynamic index '{}' - {}", archive_name, err))?;
// Note: do not use values stored in index (not trusted) - instead, computed them again
let (csum, size) = index.compute_csum();
manifest.verify_file(&server_archive_name, &csum, size)?;
let most_used = index.find_most_used_chunks(8); let most_used = index.find_most_used_chunks(8);
@ -1753,23 +1734,10 @@ async fn mount_do(param: Value, pipe: Option<RawFd>) -> Result<Value, Error> {
true, true,
).await?; ).await?;
let tmpfile = std::fs::OpenOptions::new()
.write(true)
.read(true)
.custom_flags(libc::O_TMPFILE)
.open("/tmp")?;
let manifest = client.download_manifest().await?; let manifest = client.download_manifest().await?;
if server_archive_name.ends_with(".didx") { if server_archive_name.ends_with(".didx") {
let tmpfile = client.download(&server_archive_name, tmpfile).await?; let index = client.download_dynamic_index(&manifest, &server_archive_name).await?;
let index = DynamicIndexReader::new(tmpfile)
.map_err(|err| format_err!("unable to read dynamic index '{}' - {}", archive_name, err))?;
// Note: do not use values stored in index (not trusted) - instead, computed them again
let (csum, size) = index.compute_csum();
manifest.verify_file(&server_archive_name, &csum, size)?;
let most_used = index.find_most_used_chunks(8); let most_used = index.find_most_used_chunks(8);
let chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config, most_used); let chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config, most_used);
let reader = BufferedDynamicReader::new(index, chunk_reader); let reader = BufferedDynamicReader::new(index, chunk_reader);

View File

@ -1,6 +1,7 @@
use failure::*; use failure::*;
use std::io::Write; use std::io::Write;
use std::sync::Arc; use std::sync::Arc;
use std::os::unix::fs::OpenOptionsExt;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use serde_json::{json, Value}; use serde_json::{json, Value};
@ -133,4 +134,32 @@ impl BackupReader {
BackupManifest::try_from(json) BackupManifest::try_from(json)
} }
/// Download dynamic index file
///
/// This creates a temorary file in /tmp (using O_TMPFILE). The index is verified using
/// the provided manifest.
pub async fn download_dynamic_index(
&self,
manifest: &BackupManifest,
name: &str,
) -> Result<DynamicIndexReader, Error> {
let tmpfile = std::fs::OpenOptions::new()
.write(true)
.read(true)
.custom_flags(libc::O_TMPFILE)
.open("/tmp")?;
let tmpfile = self.download(name, tmpfile).await?;
let index = DynamicIndexReader::new(tmpfile)
.map_err(|err| format_err!("unable to read dynamic index '{}' - {}", name, err))?;
// Note: do not use values stored in index (not trusted) - instead, computed them again
let (csum, size) = index.compute_csum();
manifest.verify_file(name, &csum, size)?;
Ok(index)
}
} }