Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(commands): Add --check-index option #1078

Merged
merged 4 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ If you want to contribute your own configuration, please

| Attribute | Description | Default Value | Example Value | Environment Variable |
| ----------------- | --------------------------------------------------------------------------------- | ------------- | ----------------- | ------------------------ |
| check-index | If true, check the index and read pack headers if index information is missing. | false | | RUSTIC_CHECK_INDEX |
| dry-run | If true, performs a dry run without making any changes. | false | | RUSTIC_DRY_RUN |
| log-level | Logging level. Possible values: "off", "error", "warn", "info", "debug", "trace". | "info" | | RUSTIC_LOG_LEVEL |
| log-file | Path to the log file. | No log file | "/log/rustic.log" | RUSTIC_LOG_FILE |
Expand Down
1 change: 1 addition & 0 deletions config/full.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ log-file = "/path/to/rustic.log" # Default: not set
no-progress = false
progress-interval = "100ms"
dry-run = false
check-index = false

# Global env variables: These are set by rustic before calling a subcommand, e.g. rclone or commands
# defined in the repository options.
Expand Down
22 changes: 20 additions & 2 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub(crate) mod tui;
#[cfg(feature = "webdav")]
pub(crate) mod webdav;

use std::fmt::Debug;
use std::fs::File;
use std::path::PathBuf;
use std::str::FromStr;
Expand Down Expand Up @@ -57,8 +58,8 @@ use clap::builder::{
use convert_case::{Case, Casing};
use dialoguer::Password;
use human_panic::setup_panic;
use log::{log, Level};
use rustic_core::{OpenStatus, Repository};
use log::{log, warn, Level};
use rustic_core::{IndexedFull, OpenStatus, Repository};
use simplelog::{CombinedLogger, LevelFilter, TermLogger, TerminalMode, WriteLogger};

pub(super) mod constants {
Expand Down Expand Up @@ -312,6 +313,9 @@ fn get_repository(repo_opts: &AllRepositoryOptions) -> Result<Repository<Progres
fn open_repository(
repo_opts: &AllRepositoryOptions,
) -> Result<Repository<ProgressOptions, OpenStatus>> {
if RUSTIC_APP.config().global.check_index {
warn!("Option check-index is not supported and will be ignored!");
}
let repo = get_repository(repo_opts)?;
match repo.password()? {
// if password is given, directly return the result of find_key_in_backend and don't retry
Expand All @@ -335,6 +339,20 @@ fn open_repository(
Err(anyhow!("incorrect password"))
}

/// helper function to get an opened and inedexed repo
fn open_repository_indexed(
repo_opts: &AllRepositoryOptions,
) -> Result<Repository<ProgressOptions, impl IndexedFull + Debug>> {
let open = open_repository(repo_opts)?;
let check_index = RUSTIC_APP.config().global.check_index;
let repo = if check_index {
open.to_indexed_checked()
} else {
open.to_indexed()
}?;
Ok(repo)
}

#[cfg(test)]
mod tests {
use crate::commands::EntryPoint;
Expand Down
37 changes: 22 additions & 15 deletions src/commands/cat.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! `cat` subcommand

use crate::{commands::open_repository, status_err, Application, RUSTIC_APP};
use crate::{
commands::{open_repository, open_repository_indexed},
status_err, Application, RUSTIC_APP,
};

use abscissa_core::{Command, Runnable, Shutdown};

Expand Down Expand Up @@ -59,20 +62,24 @@ impl Runnable for CatCmd {
impl CatCmd {
fn inner_run(&self) -> Result<()> {
let config = RUSTIC_APP.config();
let repo = open_repository(&config.repository)?;

let data = match &self.cmd {
CatSubCmd::Config => repo.cat_file(FileType::Config, "")?,
CatSubCmd::Index(opt) => repo.cat_file(FileType::Index, &opt.id)?,
CatSubCmd::Snapshot(opt) => repo.cat_file(FileType::Snapshot, &opt.id)?,
// special treatment for 'cat'ing blobs: read the index and use it to locate the blob
CatSubCmd::TreeBlob(opt) => repo.to_indexed()?.cat_blob(BlobType::Tree, &opt.id)?,
CatSubCmd::DataBlob(opt) => repo.to_indexed()?.cat_blob(BlobType::Data, &opt.id)?,
// special treatment for 'cat'ing a tree within a snapshot
CatSubCmd::Tree(opt) => repo
.to_indexed()?
.cat_tree(&opt.snap, |sn| config.snapshot_filter.matches(sn))?,
};
let data =
match &self.cmd {
CatSubCmd::Config => {
open_repository(&config.repository)?.cat_file(FileType::Config, "")?
}
CatSubCmd::Index(opt) => {
open_repository(&config.repository)?.cat_file(FileType::Index, &opt.id)?
}
CatSubCmd::Snapshot(opt) => {
open_repository(&config.repository)?.cat_file(FileType::Snapshot, &opt.id)?
}
CatSubCmd::TreeBlob(opt) => open_repository_indexed(&config.repository)?
.cat_blob(BlobType::Tree, &opt.id)?,
CatSubCmd::DataBlob(opt) => open_repository_indexed(&config.repository)?
.cat_blob(BlobType::Data, &opt.id)?,
CatSubCmd::Tree(opt) => open_repository_indexed(&config.repository)?
.cat_tree(&opt.snap, |sn| config.snapshot_filter.matches(sn))?,
};
println!("{}", String::from_utf8(data.to_vec())?);

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions src/commands/copy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! `copy` subcommand

use crate::{
commands::{get_repository, init::init_password, open_repository},
commands::{get_repository, init::init_password, open_repository, open_repository_indexed},
config::AllRepositoryOptions,
helpers::table_with_titles,
status_err, Application, RUSTIC_APP,
Expand Down Expand Up @@ -56,7 +56,7 @@ impl CopyCmd {
RUSTIC_APP.shutdown(Shutdown::Crash);
}

let repo = open_repository(&config.repository)?.to_indexed()?;
let repo = open_repository_indexed(&config.repository)?;
let mut snapshots = if self.ids.is_empty() {
repo.get_matching_snapshots(|sn| config.snapshot_filter.matches(sn))?
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/diff.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! `diff` subcommand

use crate::{commands::open_repository, status_err, Application, RUSTIC_APP};
use crate::{commands::open_repository_indexed, status_err, Application, RUSTIC_APP};

use abscissa_core::{Command, Runnable, Shutdown};

Expand Down Expand Up @@ -50,7 +50,7 @@ impl Runnable for DiffCmd {
impl DiffCmd {
fn inner_run(&self) -> Result<()> {
let config = RUSTIC_APP.config();
let repo = open_repository(&config.repository)?.to_indexed()?;
let repo = open_repository_indexed(&config.repository)?;

let (id1, path1) = arg_to_snap_path(&self.snap1, "");
let (id2, path2) = arg_to_snap_path(&self.snap2, path1);
Expand Down
5 changes: 3 additions & 2 deletions src/commands/dump.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! `dump` subcommand

use crate::{commands::open_repository, status_err, Application, RUSTIC_APP};
use crate::{commands::open_repository_indexed, status_err, Application, RUSTIC_APP};

use abscissa_core::{Command, Runnable, Shutdown};
use anyhow::Result;
Expand All @@ -25,7 +25,8 @@ impl Runnable for DumpCmd {
impl DumpCmd {
fn inner_run(&self) -> Result<()> {
let config = RUSTIC_APP.config();
let repo = open_repository(&config.repository)?.to_indexed()?;
let repo = open_repository_indexed(&config.repository)?;

let node =
repo.node_from_snapshot_path(&self.snap, |sn| config.snapshot_filter.matches(sn))?;

Expand Down
4 changes: 2 additions & 2 deletions src/commands/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::path::Path;

use crate::{commands::open_repository, status_err, Application, RUSTIC_APP};
use crate::{commands::open_repository_indexed, status_err, Application, RUSTIC_APP};

use abscissa_core::{Command, Runnable, Shutdown};
use anyhow::Result;
Expand Down Expand Up @@ -91,7 +91,7 @@ impl Summary {
impl LsCmd {
fn inner_run(&self) -> Result<()> {
let config = RUSTIC_APP.config();
let repo = open_repository(&config.repository)?.to_indexed()?;
let repo = open_repository_indexed(&config.repository)?;

let node =
repo.node_from_snapshot_path(&self.snap, |sn| config.snapshot_filter.matches(sn))?;
Expand Down
5 changes: 3 additions & 2 deletions src/commands/restore.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! `restore` subcommand

use crate::{
commands::open_repository, helpers::bytes_size_to_string, status_err, Application, RUSTIC_APP,
commands::open_repository_indexed, helpers::bytes_size_to_string, status_err, Application,
RUSTIC_APP,
};

use abscissa_core::{Command, Runnable, Shutdown};
Expand Down Expand Up @@ -52,7 +53,7 @@ impl RestoreCmd {
fn inner_run(&self) -> Result<()> {
let config = RUSTIC_APP.config();
let dry_run = config.global.dry_run;
let repo = open_repository(&config.repository)?.to_indexed()?;
let repo = open_repository_indexed(&config.repository)?;

let node =
repo.node_from_snapshot_path(&self.snap, |sn| config.snapshot_filter.matches(sn))?;
Expand Down
4 changes: 2 additions & 2 deletions src/commands/webdav.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! `webdav` subcommand
use std::{net::ToSocketAddrs, str::FromStr};

use crate::{commands::open_repository, status_err, Application, RusticConfig, RUSTIC_APP};
use crate::{commands::open_repository_indexed, status_err, Application, RusticConfig, RUSTIC_APP};
use abscissa_core::{config::Override, Command, FrameworkError, Runnable, Shutdown};
use anyhow::{anyhow, Result};
use dav_server::{warp::dav_handler, DavHandler};
Expand Down Expand Up @@ -64,7 +64,7 @@ impl Runnable for WebDavCmd {
impl WebDavCmd {
fn inner_run(&self) -> Result<()> {
let config = RUSTIC_APP.config();
let repo = open_repository(&config.repository)?.to_indexed()?;
let repo = open_repository_indexed(&config.repository)?;

let path_template = self
.path_template
Expand Down
5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ pub struct GlobalOptions {
#[merge(strategy = merge::bool::overwrite_false)]
pub dry_run: bool,

/// Check if index matches pack files and read pack headers if neccessary
#[clap(long, global = true, env = "RUSTIC_CHECK_INDEX")]
#[merge(strategy = merge::bool::overwrite_false)]
pub check_index: bool,

/// Use this log level [default: info]
#[clap(long, global = true, env = "RUSTIC_LOG_LEVEL")]
pub log_level: Option<String>,
Expand Down
1 change: 1 addition & 0 deletions tests/show-config-fixtures/empty.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[global]
use-profile = []
dry-run = false
check-index = false
no-progress = false

[global.env]
Expand Down
Loading