file restore: rust fmt

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2022-04-06 16:57:07 +02:00
parent aaaa10894d
commit 40ea990c05
4 changed files with 41 additions and 37 deletions

View File

@ -10,7 +10,7 @@ use serde_json::json;
use proxmox_sys::fs::lock_file; use proxmox_sys::fs::lock_file;
use pbs_client::{DEFAULT_VSOCK_PORT, BackupRepository, VsockClient}; use pbs_client::{BackupRepository, VsockClient, DEFAULT_VSOCK_PORT};
use pbs_datastore::backup_info::BackupDir; use pbs_datastore::backup_info::BackupDir;
use pbs_datastore::catalog::ArchiveEntry; use pbs_datastore::catalog::ArchiveEntry;
@ -269,10 +269,8 @@ impl BlockRestoreDriver for QemuBlockDriver {
} }
} }
None => { None => {
let err = format!( let err =
"invalid JSON received from /status call: {}", format!("invalid JSON received from /status call: {}", status);
status
);
extra["error"] = json!(err); extra["error"] = json!(err);
} }
}, },

View File

@ -3,7 +3,7 @@
//! https://www.kernel.org/doc/html/latest/driver-api/early-userspace/buffer-format.html //! https://www.kernel.org/doc/html/latest/driver-api/early-userspace/buffer-format.html
//! This does not provide full support for the format, only what is needed to include files in an //! This does not provide full support for the format, only what is needed to include files in an
//! initramfs intended for a linux kernel. //! initramfs intended for a linux kernel.
use std::ffi::{CString, CStr}; use std::ffi::{CStr, CString};
use anyhow::{bail, Error}; use anyhow::{bail, Error};
use tokio::io::{copy, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}; use tokio::io::{copy, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
@ -40,7 +40,7 @@ pub async fn append_file<W: AsyncWrite + Unpin, R: AsyncRead + Unpin>(
print_cpio_hex(&mut target, 0).await?; // c_check (ignored for newc) print_cpio_hex(&mut target, 0).await?; // c_check (ignored for newc)
target.write_all(name).await?; target.write_all(name).await?;
let header_size = 6 + 8*13 + name.len(); let header_size = 6 + 8 * 13 + name.len();
let mut name_pad = header_size; let mut name_pad = header_size;
while name_pad & 3 != 0 { while name_pad & 3 != 0 {
target.write_u8(0).await?; target.write_u8(0).await?;
@ -70,5 +70,8 @@ pub async fn append_trailer<W: AsyncWrite + Unpin>(target: W) -> Result<(), Erro
} }
async fn print_cpio_hex<W: AsyncWrite + Unpin>(target: &mut W, value: u64) -> Result<(), Error> { async fn print_cpio_hex<W: AsyncWrite + Unpin>(target: &mut W, value: u64) -> Result<(), Error> {
target.write_all(format!("{:08x}", value).as_bytes()).await.map_err(|e| e.into()) target
.write_all(format!("{:08x}", value).as_bytes())
.await
.map_err(|e| e.into())
} }

View File

@ -6,26 +6,17 @@ use std::sync::Arc;
use anyhow::{bail, format_err, Error}; use anyhow::{bail, format_err, Error};
use serde_json::{json, Value}; use serde_json::{json, Value};
use proxmox_sys::fs::{create_path, CreateOptions};
use proxmox_router::cli::{ use proxmox_router::cli::{
complete_file_name, default_table_format_options, complete_file_name, default_table_format_options, format_and_print_result_full,
format_and_print_result_full, get_output_format, get_output_format, run_cli_command, CliCommand, CliCommandMap, CliEnvironment, ColumnConfig,
run_cli_command, OUTPUT_FORMAT,
CliCommand, CliCommandMap, CliEnvironment, ColumnConfig, OUTPUT_FORMAT,
}; };
use proxmox_schema::api; use proxmox_schema::api;
use proxmox_sys::fs::{create_path, CreateOptions};
use pxar::accessor::aio::Accessor; use pxar::accessor::aio::Accessor;
use pxar::decoder::aio::Decoder; use pxar::decoder::aio::Decoder;
use pbs_tools::crypt_config::CryptConfig;
use pbs_api_types::CryptMode; use pbs_api_types::CryptMode;
use pbs_datastore::CATALOG_NAME;
use pbs_datastore::backup_info::BackupDir;
use pbs_datastore::catalog::{ArchiveEntry, CatalogReader, DirEntryAttribute};
use pbs_datastore::dynamic_index::{BufferedDynamicReader, LocalDynamicReadAt};
use pbs_datastore::index::IndexFile;
use pbs_config::key_config::decrypt_key;
use pbs_client::{BackupReader, RemoteChunkReader};
use pbs_client::pxar::{create_zip, extract_sub_dir, extract_sub_dir_seq}; use pbs_client::pxar::{create_zip, extract_sub_dir, extract_sub_dir_seq};
use pbs_client::tools::{ use pbs_client::tools::{
complete_group_or_snapshot, complete_repository, connect, extract_repository_from_value, complete_group_or_snapshot, complete_repository, connect, extract_repository_from_value,
@ -35,14 +26,22 @@ use pbs_client::tools::{
}, },
REPO_URL_SCHEMA, REPO_URL_SCHEMA,
}; };
use pbs_client::{BackupReader, RemoteChunkReader};
use pbs_config::key_config::decrypt_key;
use pbs_datastore::backup_info::BackupDir;
use pbs_datastore::catalog::{ArchiveEntry, CatalogReader, DirEntryAttribute};
use pbs_datastore::dynamic_index::{BufferedDynamicReader, LocalDynamicReadAt};
use pbs_datastore::index::IndexFile;
use pbs_datastore::CATALOG_NAME;
use pbs_tools::crypt_config::CryptConfig;
pub mod block_driver; pub mod block_driver;
pub use block_driver::*; pub use block_driver::*;
pub mod cpio; pub mod cpio;
mod qemu_helper;
mod block_driver_qemu; mod block_driver_qemu;
mod qemu_helper;
enum ExtractPath { enum ExtractPath {
ListArchives, ListArchives,
@ -146,12 +145,7 @@ fn keyfile_path(param: &Value) -> Option<String> {
} }
)] )]
/// List a directory from a backup snapshot. /// List a directory from a backup snapshot.
async fn list( async fn list(snapshot: String, path: String, base64: bool, param: Value) -> Result<(), Error> {
snapshot: String,
path: String,
base64: bool,
param: Value,
) -> Result<(), Error> {
let repo = extract_repository_from_value(&param)?; let repo = extract_repository_from_value(&param)?;
let snapshot: BackupDir = snapshot.parse()?; let snapshot: BackupDir = snapshot.parse()?;
let path = parse_path(path, base64)?; let path = parse_path(path, base64)?;
@ -199,7 +193,11 @@ async fn list(
} else { } else {
None None
}; };
entries.push(ArchiveEntry::new_with_size(path.as_bytes(), attr, Some(file.size))); entries.push(ArchiveEntry::new_with_size(
path.as_bytes(),
attr,
Some(file.size),
));
} }
Ok(entries) Ok(entries)
@ -506,7 +504,10 @@ fn create_run_dir() -> Result<(), Error> {
/// Return User info for the 'backup' user (``getpwnam_r(3)``) /// Return User info for the 'backup' user (``getpwnam_r(3)``)
pub fn backup_user() -> Result<nix::unistd::User, Error> { pub fn backup_user() -> Result<nix::unistd::User, Error> {
nix::unistd::User::from_name(pbs_buildcfg::BACKUP_USER_NAME)? nix::unistd::User::from_name(pbs_buildcfg::BACKUP_USER_NAME)?.ok_or_else(|| {
.ok_or_else(|| format_err!("Unable to lookup '{}' user.", pbs_buildcfg::BACKUP_USER_NAME)) format_err!(
"Unable to lookup '{}' user.",
pbs_buildcfg::BACKUP_USER_NAME
)
})
} }

View File

@ -3,7 +3,7 @@ use std::fs::{File, OpenOptions};
use std::io::prelude::*; use std::io::prelude::*;
use std::os::unix::io::AsRawFd; use std::os::unix::io::AsRawFd;
use std::path::PathBuf; use std::path::PathBuf;
use std::time::{Instant, Duration}; use std::time::{Duration, Instant};
use anyhow::{bail, format_err, Error}; use anyhow::{bail, format_err, Error};
use tokio::time; use tokio::time;
@ -11,14 +11,14 @@ use tokio::time;
use nix::sys::signal::{kill, Signal}; use nix::sys::signal::{kill, Signal};
use nix::unistd::Pid; use nix::unistd::Pid;
use proxmox_sys::fs::{create_path, file_read_string, make_tmp_file, CreateOptions};
use proxmox_sys::fd::fd_change_cloexec; use proxmox_sys::fd::fd_change_cloexec;
use proxmox_sys::fs::{create_path, file_read_string, make_tmp_file, CreateOptions};
use proxmox_sys::logrotate::LogRotate; use proxmox_sys::logrotate::LogRotate;
use pbs_client::{VsockClient, DEFAULT_VSOCK_PORT}; use pbs_client::{VsockClient, DEFAULT_VSOCK_PORT};
use crate::{cpio, backup_user};
use super::SnapRestoreDetails; use super::SnapRestoreDetails;
use crate::{backup_user, cpio};
const PBS_VM_NAME: &str = "pbs-restore-vm"; const PBS_VM_NAME: &str = "pbs-restore-vm";
const MAX_CID_TRIES: u64 = 32; const MAX_CID_TRIES: u64 = 32;
@ -142,7 +142,8 @@ pub async fn start_vm(
validate_img_existance(debug)?; validate_img_existance(debug)?;
let pid; let pid;
let (mut pid_file, pid_path) = make_tmp_file("/tmp/file-restore-qemu.pid.tmp", CreateOptions::new())?; let (mut pid_file, pid_path) =
make_tmp_file("/tmp/file-restore-qemu.pid.tmp", CreateOptions::new())?;
nix::unistd::unlink(&pid_path)?; nix::unistd::unlink(&pid_path)?;
fd_change_cloexec(pid_file.as_raw_fd(), false)?; fd_change_cloexec(pid_file.as_raw_fd(), false)?;
@ -319,7 +320,8 @@ pub async fn start_vm(
} }
return Ok((pid, cid as i32)); return Ok((pid, cid as i32));
} }
if kill(pid_t, None).is_err() { // check if QEMU process exited in between if kill(pid_t, None).is_err() {
// check if QEMU process exited in between
bail!("VM exited before connection could be established"); bail!("VM exited before connection could be established");
} }
if Instant::now().duration_since(start_poll) > Duration::from_secs(25) { if Instant::now().duration_since(start_poll) > Duration::from_secs(25) {