blkar_lib/
cli_check.rs

1use crate::check_core;
2use crate::check_core::HashAction;
3use crate::check_core::Param;
4use crate::cli_utils::*;
5use crate::json_printer::BracketType;
6use clap::*;
7
8pub fn sub_command<'a, 'b>() -> App<'a, 'b> {
9    SubCommand::with_name("check")
10        .about("Check integrity of SBX blocks in container")
11        .arg(in_file_arg().help("SBX container to check"))
12        .arg(no_meta_arg())
13        .arg(pr_verbosity_level_arg())
14        .arg(Arg::with_name("report_blank").long("report-blank").help(
15            "Completely blank blocks are ignored by default.
16Specify this if you want blkar to report blank blocks as well.",
17        ))
18        .arg(verbose_arg().help("Show reference block info, show individual check results"))
19        .arg(from_byte_arg().help(FROM_BYTE_ARG_HELP_MSG_REF_BLOCK))
20        .arg(to_byte_inc_arg())
21        .arg(to_byte_exc_arg())
22        .arg(force_misalign_arg())
23        .arg(ref_from_byte_arg())
24        .arg(ref_to_byte_inc_arg())
25        .arg(ref_to_byte_exc_arg())
26        .arg(burst_arg().help(
27            "Burst error resistance level used by the container.
28Use this if the level used by the container is above 1000,
29as blkar will only guess up to 1000. Or use this when blkar
30fails to guess correctly. blkar uses this value only if
31--hash is specified.",
32        ))
33        .arg(guess_burst_from_byte_arg())
34        .arg(
35            Arg::with_name("hash")
36                .long("hash")
37                .help(
38                    "Hash stored data after individual block checking. This is done
39only if the reference block is a metadata block and has the hash
40field.",
41                )
42                .conflicts_with("from_pos")
43                .conflicts_with("to_pos_inc")
44                .conflicts_with("to_pos_exc"),
45        )
46        .arg(
47            Arg::with_name("hash_only")
48                .long("hash-only")
49                .help(
50                    "Hash stored data and skip individual block checking. This is
51done only if the reference block is a metadata block and has
52the hash field.",
53                )
54                .conflicts_with("from_pos")
55                .conflicts_with("to_pos_inc")
56                .conflicts_with("to_pos_exc")
57                .conflicts_with("hash"),
58        )
59        .arg(json_arg())
60}
61
62pub fn check<'a>(matches: &ArgMatches<'a>) -> i32 {
63    let json_printer = get_json_printer!(matches);
64
65    json_printer.print_open_bracket(None, BracketType::Curly);
66
67    let pr_verbosity_level = get_pr_verbosity_level!(matches, json_printer);
68
69    let in_file = get_in_file!(matches, json_printer);
70
71    let from_pos = get_from_pos!(matches, json_printer);
72    let to_pos = get_to_pos!(matches, json_printer);
73
74    let ref_from_pos = get_ref_from_pos!(matches, json_printer);
75    let ref_to_pos = get_ref_to_pos!(matches, json_printer);
76
77    let guess_burst_from_pos = get_guess_burst_from_pos!(matches, json_printer);
78
79    let hash_action = if matches.is_present("hash") {
80        HashAction::HashAfterCheck
81    } else if matches.is_present("hash_only") {
82        HashAction::HashOnly
83    } else {
84        HashAction::NoHash
85    };
86
87    let burst = get_burst_opt!(matches, json_printer);
88
89    let param = Param::new(
90        get_ref_block_choice!(matches),
91        ref_from_pos,
92        ref_to_pos,
93        guess_burst_from_pos,
94        matches.is_present("report_blank"),
95        &json_printer,
96        from_pos,
97        to_pos,
98        matches.is_present("force_misalign"),
99        hash_action,
100        burst,
101        in_file,
102        matches.is_present("verbose"),
103        pr_verbosity_level,
104    );
105    match check_core::check_file(&param) {
106        Ok(Some(s)) => exit_with_msg!(ok json_printer => "{}", s),
107        Ok(None) => exit_with_msg!(ok json_printer => ""),
108        Err(e) => exit_with_msg!(op json_printer => "{}", e),
109    }
110}