blkar_lib/
cli_sort.rs

1use crate::cli_utils::*;
2use crate::file_utils;
3use crate::json_printer::BracketType;
4use crate::misc_utils;
5use crate::sort_core;
6use crate::sort_core::Param;
7use clap::*;
8
9pub fn sub_command<'a, 'b>() -> App<'a, 'b> {
10    SubCommand::with_name("sort")
11        .about("Sort SBX blocks in container, can also readjust burst error resistance level")
12        .arg(in_file_arg().help("SBX container to sort"))
13        .arg(out_arg().help(
14            "Sorted SBX container (defaults to INFILE.sorted). If OUT is a directory, then the
15container is stored as OUT/INFILE.sorted (only the file part of INFILE is used).
16Ignored if --dry-run is supplied.",
17        ))
18        .arg(force_arg().help("Force overwrite even if OUTFILE exists"))
19        .arg(multi_pass_arg())
20        .arg(multi_pass_no_skip_arg())
21        .arg(pr_verbosity_level_arg())
22        .arg(dry_run_arg().help("Only do sorting in memory, does not output the sorted container."))
23        .arg(ref_from_byte_arg())
24        .arg(ref_to_byte_inc_arg())
25        .arg(ref_to_byte_exc_arg())
26        .arg(guess_burst_from_byte_arg())
27        .arg(from_byte_arg().help(FROM_BYTE_ARG_HELP_MSG_REF_BLOCK))
28        .arg(to_byte_inc_arg())
29        .arg(to_byte_exc_arg())
30        .arg(force_misalign_arg())
31        .arg(burst_arg().help(
32            "Burst error resistance level to use for the output container.
33Defaults to guessing the level (guesses up to 1000) used by the
34original container and uses the result.",
35        ))
36        .arg(Arg::with_name("report_blank").long("report-blank").help(
37            "Failure to sort completely blank blocks are ignored by default.
38Specify this if you want blkar to report said failures as well.",
39        ))
40        .arg(verbose_arg().help("Show reference block info"))
41        .arg(json_arg())
42}
43
44pub fn sort<'a>(matches: &ArgMatches<'a>) -> i32 {
45    let json_printer = get_json_printer!(matches);
46
47    json_printer.print_open_bracket(None, BracketType::Curly);
48
49    let in_file = get_in_file!(matches, json_printer);
50
51    let out = match matches.value_of("out") {
52        None => format!("{}.sorted", in_file),
53        Some(x) => {
54            if file_utils::check_if_file_is_dir(x) {
55                let in_file = file_utils::get_file_name_part_of_path(in_file).unwrap();
56                misc_utils::make_path(&[x, &format!("{}.sorted", in_file)])
57            } else {
58                String::from(x)
59            }
60        }
61    };
62
63    let force = matches.is_present("force");
64    let multi_pass = get_multi_pass!(matches, json_printer);
65    let dry_run = matches.is_present("dry_run");
66
67    let burst = get_burst_opt!(matches, json_printer);
68
69    exit_if_file!(exists &out
70                  => force || multi_pass != None || dry_run
71                  => json_printer
72                  => "File \"{}\" already exists", out);
73
74    let pr_verbosity_level = get_pr_verbosity_level!(matches, json_printer);
75
76    let out: Option<&str> = if dry_run { None } else { Some(&out) };
77
78    let from_pos = get_from_pos!(matches, json_printer);
79    let to_pos = get_to_pos!(matches, json_printer);
80
81    let ref_from_pos = get_ref_from_pos!(matches, json_printer);
82    let ref_to_pos = get_ref_to_pos!(matches, json_printer);
83
84    let guess_burst_from_pos = get_guess_burst_from_pos!(matches, json_printer);
85
86    let param = Param::new(
87        get_ref_block_choice!(matches),
88        ref_from_pos,
89        ref_to_pos,
90        matches.is_present("report_blank"),
91        guess_burst_from_pos,
92        multi_pass,
93        &json_printer,
94        from_pos,
95        to_pos,
96        matches.is_present("force_misalign"),
97        in_file,
98        out,
99        matches.is_present("verbose"),
100        pr_verbosity_level,
101        burst,
102    );
103    match sort_core::sort_file(&param) {
104        Ok(Some(s)) => exit_with_msg!(ok json_printer => "{}", s),
105        Ok(None) => exit_with_msg!(ok json_printer => ""),
106        Err(e) => exit_with_msg!(op json_printer => "{}", e),
107    }
108}