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
use crate::encode_core;
use crate::encode_core::Param;
use crate::sbx_specs::{
    ver_to_block_size, ver_to_data_size, ver_to_usize, ver_uses_rs, Version, SBX_FILE_UID_LEN,
};
use std::str::FromStr;
use std::time::UNIX_EPOCH;

use crate::json_printer::BracketType;

use crate::multihash;

use crate::file_utils;
use crate::misc_utils;
use crate::rand_utils;
use crate::time_utils;

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

pub fn sub_command<'a, 'b>() -> App<'a, 'b> {
    SubCommand::with_name("encode")
        .about("Encode file")
        .arg(
            in_file_arg()
                .help("File to encode. Supply - to use stdin as input. Use ./- for files named -."),
        )
        .arg(out_arg().help(
            "SBX container name (defaults to INFILE.sbx). If OUT is a directory, then the
container is stored as OUT/INFILE.sbx (only the file part of INFILE is used).",
        ))
        .arg(force_arg().help("Force overwrite even if OUT exists"))
        .arg(
            Arg::with_name("hash_type")
                .value_name("HASH-TYPE")
                .long("hash")
                .takes_value(true)
                .help(
                    "Hash function to use, one of (case-insensitive) :
          sha1
(default) sha256
          sha512
          blake2b-512",
                ),
        )
        .arg(Arg::with_name("no_meta").long("no-meta").help(
            "Skip metadata block in the SBX container. Metadata block is
never skipped for version 17, 18, 19.
This means this option has no effect for version 17, 18, 19.",
        ))
        .arg(pr_verbosity_level_arg())
        .arg(sbx_version_arg())
        .arg(only_pick_uid_arg().long("uid").help(
            "Alternative file UID in hex (by default UID is randomly generated).
UID must be exactly 6 bytes (12 hex digits) in length.",
        ))
        .arg(rs_data_arg())
        .arg(rs_parity_arg())
        .arg(from_byte_arg().help(FROM_BYTE_ARG_HELP_MSG_RAW_UNALIGNED))
        .arg(to_byte_inc_arg())
        .arg(to_byte_exc_arg())
        .arg(burst_arg().help(
            "Burst error resistance level. Note that blkar only guesses up to
1000 in repair, show, and sort mode. If you use level above 1000,
then blkar will make an incorrect guess, and you will need to
specify it explicitly in repair and sort mode. Show mode does
not rely on burst level, but provides an option for enabling
automatic guessing.",
        ))
        .arg(
            Arg::with_name("info_only")
                .long("info-only")
                .help("Only display information about encoding then exit"),
        )
        .arg(json_arg())
}

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

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

    // compute uid
    let mut uid: [u8; SBX_FILE_UID_LEN] = [0; SBX_FILE_UID_LEN];
    {
        match matches.value_of("uid") {
            None => {
                rand_utils::fill_random_bytes(&mut uid);
            }
            Some(x) => {
                parse_uid!(uid, x, json_printer);
            }
        }
    }

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

    let in_file = get_in_file!(accept_stdin matches, json_printer);
    let out = match matches.value_of("out") {
        None => {
            if file_utils::check_if_file_is_stdin(in_file) {
                exit_with_msg!(usr json_printer => "Explicit output file name is required when input is stdin");
            } else {
                format!("{}.sbx", in_file)
            }
        }
        Some(x) => {
            if file_utils::check_if_file_is_dir(x) {
                if file_utils::check_if_file_is_stdin(in_file) {
                    exit_with_msg!(usr json_printer => "Explicit output file name is required when input is stdin");
                }

                let in_file = file_utils::get_file_name_part_of_path(in_file);
                misc_utils::make_path(&[x, &format!("{}.sbx", in_file)])
            } else {
                String::from(x)
            }
        }
    };

    let hash_type = match matches.value_of("hash_type") {
        None => multihash::HashType::SHA256,
        Some(x) => match multihash::string_to_hash_type(x) {
            Ok(x) => x,
            Err(_) => exit_with_msg!(usr json_printer => "Invalid hash type"),
        },
    };

    let pr_verbosity_level = get_pr_verbosity_level!(matches, json_printer);

    let meta_enabled = get_meta_enabled!(matches);

    let from_pos = get_from_pos!(matches, json_printer);
    let to_pos = get_to_pos!(matches, json_printer);

    if matches.is_present("info_only") {
        json_printer.print_open_bracket(Some("stats"), BracketType::Curly);

        if file_utils::check_if_file_is_stdin(in_file) {
            exit_with_msg!(usr json_printer => "No information is available for stdin input");
        }

        let in_file_meta = match file_utils::get_file_metadata(in_file) {
            Ok(x) => x,
            Err(_) => exit_with_msg!(usr json_printer => "Failed to get metadata of \"{}\"",
                                     in_file),
        };

        let in_file_size = match file_utils::get_file_size(in_file) {
            Ok(x) => x,
            Err(_) => exit_with_msg!(usr json_printer => "Failed to get file size of \"{}\"",
                                     in_file),
        };

        let in_file_mod_time = match in_file_meta.modified() {
            Ok(t) => match t.duration_since(UNIX_EPOCH) {
                Ok(t) => Some(t.as_secs() as i64),
                Err(_) => None,
            },
            Err(_) => None,
        };

        let in_file_mod_time_str = match in_file_mod_time {
            None => "N/A".to_string(),
            Some(x) => match (
                time_utils::i64_secs_to_date_time_string(x, time_utils::TimeMode::UTC),
                time_utils::i64_secs_to_date_time_string(x, time_utils::TimeMode::Local),
            ) {
                (Some(u), Some(l)) => format!("{} (UTC)  {} (Local)", u, l),
                _ => "Invalid file modification time".to_string(),
            },
        };

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

        if ver_uses_rs(version) {
            print_maybe_json!(json_printer, "File name                    : {}", in_file);
            print_maybe_json!(json_printer, "SBX container name           : {}", out);
            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) => skip_quotes);
            print_maybe_json!(json_printer, "SBX container data  size     : {}", ver_to_data_size(version)  => skip_quotes);
            print_maybe_json!(json_printer, "RS data   shard count        : {}", data_par_burst.unwrap().0  => skip_quotes);
            print_maybe_json!(json_printer, "RS parity shard count        : {}", data_par_burst.unwrap().1  => skip_quotes);
            print_maybe_json!(json_printer, "Burst error resistance level : {}", data_par_burst.unwrap().2  => skip_quotes);
            print_maybe_json!(json_printer, "File size                    : {}", in_file_size               => skip_quotes);
            print_maybe_json!(json_printer, "SBX container size           : {}", out_file_size              => skip_quotes);
            print_maybe_json!(
                json_printer,
                "File modification time       : {}",
                in_file_mod_time_str
            );
        } else {
            print_maybe_json!(json_printer, "File name                : {}", in_file);
            print_maybe_json!(json_printer, "SBX container name       : {}", out);
            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) => skip_quotes);
            print_maybe_json!(json_printer, "SBX container data  size : {}", ver_to_data_size(version)  => skip_quotes);
            print_maybe_json!(json_printer, "File size                : {}", in_file_size               => skip_quotes);
            print_maybe_json!(json_printer, "SBX container size       : {}", out_file_size              => skip_quotes);
            print_maybe_json!(
                json_printer,
                "File modification time   : {}",
                in_file_mod_time_str
            );
        }

        json_printer.print_close_bracket();

        exit_with_msg!(ok json_printer => "")
    } else {
        exit_if_file!(exists &out
                      => matches.is_present("force")
                      => json_printer
                      => "File \"{}\" already exists", out);

        let in_file = if file_utils::check_if_file_is_stdin(in_file) {
            None
        } else {
            Some(in_file)
        };

        let param = Param::new(
            version,
            &uid,
            data_par_burst,
            meta_enabled,
            &json_printer,
            hash_type,
            from_pos,
            to_pos,
            in_file,
            &out,
            pr_verbosity_level,
        );
        match encode_core::encode_file(&param) {
            Ok(s) => exit_with_msg!(ok json_printer => "{}", s),
            Err(e) => exit_with_msg!(op json_printer => "{}", e),
        }
    }
}