1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use crate::update_core;
use crate::update_core::Param;

use crate::cli_utils::*;
use clap::*;

use crate::sbx_block::Metadata;
use crate::sbx_block::MetadataID;

use crate::json_printer::BracketType;

pub fn sub_command<'a, 'b>() -> App<'a, 'b> {
    SubCommand::with_name("update")
        .about("Update SBX container metadata")
        .arg(in_file_arg().help("SBX container to update"))
        .arg(pr_verbosity_level_arg())
        .arg(burst_arg().help(
            "Burst error resistance level used by the container.
Use this if the level used by the container is above 1000,
as blkar will only guess up to 1000. Or use this when blkar
fails to guess correctly.",
        ))
        .arg(
            verbose_arg()
                .help("Show reference block info, and changes made in each metadata block"),
        )
        .arg(
            Arg::with_name("skip_warning")
                .short("y")
                .long("skip-warning")
                .help("Skip warning about in-place updates"),
        )
        .arg(dry_run_arg().help("Only do updates in memory. The container will not be modified."))
        .arg(json_arg().help(
            "Output information in JSON format. Note that blkar does not
guarantee the JSON data to be well-formed if blkar is interrupted.
This also implies --skip-warning, and changes progress report text
(if any) to be in JSON.",
        ))
        .arg(
            Arg::with_name("fnm")
                .value_name("NAME")
                .takes_value(true)
                .long("fnm")
                .help("New file name"),
        )
        .arg(
            Arg::with_name("snm")
                .value_name("NAME")
                .takes_value(true)
                .long("snm")
                .help("New SBX container name"),
        )
        .arg(
            Arg::with_name("no_fnm")
                .long("no-fnm")
                .help("Remove file name")
                .conflicts_with("fnm"),
        )
        .arg(
            Arg::with_name("no_snm")
                .long("no-snm")
                .help("Remove SBX container name")
                .conflicts_with("snm"),
        )
}

pub fn update<'a>(matches: &ArgMatches<'a>) -> i32 {
    let json_printer = get_json_printer!(matches);

    json_printer.print_open_bracket(None, BracketType::Curly);

    let in_file = get_in_file!(matches, json_printer);

    let pr_verbosity_level = get_pr_verbosity_level!(matches, json_printer);

    let burst = get_burst_opt!(matches, json_printer);

    let metas_to_update = {
        let mut res = smallvec![];

        if let Some(x) = matches.value_of("fnm") {
            res.push(Metadata::FNM(x.to_string()))
        }
        if let Some(x) = matches.value_of("snm") {
            res.push(Metadata::SNM(x.to_string()))
        }

        res
    };

    let metas_to_remove = {
        let mut res = smallvec![];

        if matches.is_present("no_fnm") {
            res.push(MetadataID::FNM)
        }
        if matches.is_present("no_snm") {
            res.push(MetadataID::SNM)
        }

        res
    };

    if matches.is_present("dry_run") && !json_printer.json_enabled() {
        print_block!(
            "Note : This is a dry run only, the container is not modified.";
            "";
        );
    }

    if !matches.is_present("skip_warning")
        && !matches.is_present("dry_run")
        && !json_printer.json_enabled()
    {
        print_block!(
            "Warning :";
            "";
            "    Update mode modifies the SBX container in-place.";
            "";
            "    This may cause irreversible damage to the container and prohibit normal";
            "    functioning, depending on your workflow.";
            "";
            "    It is advisable to do a dry run first via supplying the --dry-run flag";
            "    and examine the changes before actually updating the container.";
            "";
        );

        ask_if_wish_to_continue!();
    }

    let param = Param::new(
        in_file,
        matches.is_present("dry_run"),
        metas_to_update,
        metas_to_remove,
        &json_printer,
        matches.is_present("verbose"),
        pr_verbosity_level,
        burst,
    );
    match update_core::update_file(&param) {
        Ok(Some(s)) => exit_with_msg!(ok json_printer => "{}", s),
        Ok(None) => exit_with_msg!(ok json_printer => ""),
        Err(e) => exit_with_msg!(op json_printer => "{}", e),
    }
}