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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
use crate::cli_utils::*;
use crate::file_utils;
use crate::json_printer::BracketType;
use crate::sbx_specs::{ver_to_block_size, ver_to_data_size, ver_to_usize, ver_uses_rs};
use clap::*;
use std::str::FromStr;

pub fn sub_command<'a, 'b>() -> App<'a, 'b> {
    SubCommand::with_name("calc")
        .about("Calculate and display detailed information given an encoding configuration")
        .arg(
            Arg::with_name("in_file_size")
                .value_name("INFILE-SIZE")
                .required(true)
                .index(1)
                .help("Input file size"),
        )
        .arg(sbx_version_arg())
        .arg(Arg::with_name("no_meta").long("no-meta").help(
            "Skip metadata block in the calculations. Metadata block is
never skipped for version 17, 18, 19.
This means this option has no effect for version 17, 18, 19.",
        ))
        .arg(rs_data_arg())
        .arg(rs_parity_arg())
        .arg(burst_arg())
        .arg(json_arg())
}

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

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

    let (version, data_par_burst) = get_ver_and_data_par_burst_w_defaults!(matches, json_printer);

    let meta_enabled = Some(get_meta_enabled!(matches));

    let in_file_size = match u64::from_str(matches.value_of("in_file_size").unwrap()) {
        Ok(x) => x,
        Err(_) => exit_with_msg!(usr json_printer => "Invalid file size"),
    };

    let out_file_size = file_utils::from_orig_file_size::calc_container_size(
        version,
        meta_enabled,
        data_par_burst,
        in_file_size,
    );

    let total_block_count = file_utils::from_orig_file_size::calc_total_block_count_exc_burst_gaps(
        version,
        meta_enabled,
        data_par_burst,
        in_file_size,
    );

    let meta_block_count =
        file_utils::calc_meta_block_count_exc_burst_gaps(version, meta_enabled, data_par_burst);

    let (data_only_block_count, parity_block_count) =
        file_utils::from_orig_file_size::calc_data_only_and_parity_block_count_exc_burst_gaps(
            version,
            data_par_burst,
            in_file_size,
        );

    json_printer.print_open_bracket(Some("stats"), BracketType::Curly);

    print_if!(not_json => json_printer =>
             "SBX container general info";
             "========================================";);
    if ver_uses_rs(version) {
        print_maybe_json!(
            json_printer,
            "    SBX container version        : {}",
            ver_to_usize(version)
        );
    } else {
        print_maybe_json!(
            json_printer,
            "    SBX container version        : {}",
            ver_to_usize(version)
        );
    }
    print_maybe_json!(
        json_printer,
        "    SBX container block size     : {}",
        ver_to_block_size(version)
    );
    print_maybe_json!(
        json_printer,
        "    SBX container data  size     : {}",
        ver_to_data_size(version)
    );

    print_if!(not_json => json_printer => "";);

    print_if!(not_json => json_printer => "SBX block distribution";);
    print_if!(not_json => json_printer => "========================================";);
    if ver_uses_rs(version) {
        print_maybe_json!(
            json_printer,
            "    Metadata    block count      : {}",
            meta_block_count
        );
        print_maybe_json!(
            json_printer,
            "    Data only   block count      : {}",
            data_only_block_count
        );
        print_maybe_json!(
            json_printer,
            "    Data parity block count      : {}",
            parity_block_count
        );
        print_maybe_json!(
            json_printer,
            "    Total       block count      : {}",
            total_block_count
        );
    } else {
        print_maybe_json!(
            json_printer,
            "    Metadata block count         : {}",
            meta_block_count
        );
        print_maybe_json!(
            json_printer,
            "    Data     block count         : {}",
            data_only_block_count
        );
        print_maybe_json!(
            json_printer,
            "    Total    block count         : {}",
            total_block_count
        );
    }

    print_if!(not_json => json_printer => "";);

    print_if!(not_json => json_printer => "Error correction info";);
    print_if!(not_json => json_printer => "========================================";);
    if ver_uses_rs(version) {
        print_maybe_json!(
            json_printer,
            "    RS data   shard count        : {}",
            data_par_burst.unwrap().0
        );
        print_maybe_json!(
            json_printer,
            "    RS parity shard count        : {}",
            data_par_burst.unwrap().1
        );
        print_maybe_json!(
            json_printer,
            "    Burst error resistance level : {}",
            data_par_burst.unwrap().2
        );
    } else {
        print_maybe_json!(
            json_printer,
            "    RS data   shard count        : {}",
            null_if_json_else!(json_printer, "version does not use RS")
        );
        print_maybe_json!(
            json_printer,
            "    RS parity shard count        : {}",
            null_if_json_else!(json_printer, "version does not use RS")
        );
        print_maybe_json!(
            json_printer,
            "    Burst error resistance level : {}",
            null_if_json_else!(
                json_printer,
                "version does not support burst error resistance"
            )
        );
    }

    print_if!(not_json => json_printer => "";);

    if ver_uses_rs(version) && !json_printer.json_enabled() {
        let (data, par, burst) = data_par_burst.unwrap();

        let block_size = ver_to_block_size(version);

        println!("Error correction parameters interpretation");
        println!("========================================");
        if burst == 0 {
            print_block!(
                "    The container can tolerate {} SBX block corruptions", par;
                "    in each block set.";
                "";
                "    A block set consists of {} blocks ({} bytes).", (data + par), (data + par) * block_size;
                "";
                "    In total, {} blocks({} bytes) may be corrupted in", par, par * block_size;
                "    each block set.";
            );
        } else {
            if burst == 1 {
                print_block!(
                    "    Warning :";
                    "";
                    "        Burst error resistance level of {} may not provide", burst;
                    "        meaningful resistance.";
                    "";
                );
            }

            let block_set_size = data + par;
            let super_block_set_size = (data + par) * burst;

            print_block!("    The container can tolerate {} burst SBX block corruptions in", par;
                         "    each super block set ({} interleaved block sets).", burst;
                         "";
                         "    A block set consists of {} blocks ({} bytes).", block_set_size, block_set_size * block_size;
                         "";
                         "    A super block set consists of {} blocks ({} bytes).", super_block_set_size, super_block_set_size * block_size;
                         "";
                         "    Each burst error may be up to {} blocks ({} bytes) in size.", burst, burst * block_size;
                         "";
                         "    In total, {} sets of {} consecutive blocks ({} bytes) may be", par, burst, burst * block_size;
                         "    corrupted in each super block set.";
                         "";
                         "    The sets of corrupted blocks may be connected as well, so the";
                         "    largest single burst error that can be tolerated in each super";
                         "    block set is {} blocks ({} bytes) in size.", par * burst, par * burst * block_size;
                         "";
                         "    Note that the actual tolerance depends on the behaviour of";
                         "    the file system.";
            );
        }

        println!();
    }

    print_if!(not_json => json_printer => "File and container size";);
    print_if!(not_json => json_printer => "========================================";);
    print_maybe_json!(
        json_printer,
        "    File size                    : {}",
        in_file_size
    );
    print_maybe_json!(
        json_printer,
        "    SBX container size           : {}",
        out_file_size
    );

    json_printer.print_close_bracket();

    exit_with_msg!(ok json_printer => "")
}