add helpers.rs

This commit is contained in:
Alexander Weiss 2022-02-19 09:06:22 +01:00
parent 571312363e
commit f95fb70a1c
3 changed files with 36 additions and 23 deletions

View File

@ -28,7 +28,6 @@ pub(super) fn execute(
) -> Result<()> {
let config = ConfigFile::from_backend_no_id(be)?;
let poly = "37ffea04120bf1";
let poly = u64::from_str_radix(config.chunker_polynomial(), 16)?;
backup_file(opts.sources, &poly, be, key)?;
Ok(())

31
src/commands/helpers.rs Normal file
View File

@ -0,0 +1,31 @@
use std::fs::File;
use std::io::BufReader;
use std::path::PathBuf;
use anyhow::{bail, Result};
use rpassword::{prompt_password_stderr, read_password_with_reader};
use crate::backend::ReadBackend;
use crate::crypto::Key;
use crate::repo::find_key_in_backend;
const MAX_PASSWORD_RETRIES: usize = 5;
pub fn get_key(be: &impl ReadBackend, password_file: Option<PathBuf>) -> Result<Key> {
let key = match password_file {
None => (0..MAX_PASSWORD_RETRIES)
.map(|_| {
let pass = prompt_password_stderr("enter repository password: ")?;
find_key_in_backend(be, &pass, None)
})
.find(Result::is_ok)
.unwrap_or_else(|| bail!("tried too often...aborting!"))?,
Some(file) => {
let mut file = BufReader::new(File::open(file)?);
let pass = read_password_with_reader(Some(&mut file))?;
find_key_in_backend(be, &pass, None)?
}
};
eprintln!("password is correct");
Ok(key)
}

View File

@ -1,22 +1,22 @@
use std::fs;
use std::path::PathBuf;
use anyhow::{bail, Result};
use anyhow::Result;
use clap::{Parser, Subcommand};
use rpassword::{prompt_password_stderr, read_password_with_reader};
use crate::backend::{DecryptBackend, LocalBackend};
use crate::repo;
mod backup;
mod cat;
mod check;
mod diff;
mod helpers;
mod list;
mod ls;
mod restore;
mod snapshots;
use helpers::get_key;
#[derive(Parser)]
#[clap(about, version)]
struct Opts {
@ -59,28 +59,11 @@ enum Command {
Restore(restore::Opts),
}
const MAX_PASSWORD_RETRIES: usize = 5;
pub fn execute() -> Result<()> {
let args = Opts::parse();
let be = LocalBackend::new(&args.repository);
let key = match args.password_file {
None => (0..MAX_PASSWORD_RETRIES)
.map(|_| {
let pass = prompt_password_stderr("enter repository password: ")?;
repo::find_key_in_backend(&be, &pass, None)
})
.find(Result::is_ok)
.unwrap_or_else(|| bail!("tried too often...aborting!"))?,
Some(file) => {
let pass = fs::read_to_string(file)?.replace("\n", "");
repo::find_key_in_backend(&be, &pass, None)?
}
};
eprintln!("password is correct");
let key = get_key(&be, args.password_file)?;
let dbe = DecryptBackend::new(&be, key.clone());
match args.command {