Skip to content
This repository has been archived by the owner on Jun 7, 2022. It is now read-only.

Commit

Permalink
Merge pull request #176 from darrenldl/dev
Browse files Browse the repository at this point in the history
Added update mode, fixed incosistency in use of null and N/A in output, fixed help message for --pv
  • Loading branch information
darrenldl committed Apr 21, 2019
2 parents 4ed2af2 + c60f5f7 commit 69f086a
Show file tree
Hide file tree
Showing 30 changed files with 1,644 additions and 588 deletions.
342 changes: 182 additions & 160 deletions BLKAR_SPECS.md

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# Changelog

## 7.0.0

- Updated `--pv` help message to state the default in JSON mode

- Added `update` mode for updating metadata

- Updated output text of following modes to use null instead of "N/A" for missing fields in JSON mode for consistency

- Encode and show mode

- Bumped major version as this may break backward compatibility

- Updated JSON code to output all numbers without quotes for consistency

- Previously, version numbers were quoted

- Previously, there may have been inconsistency as well

- Bumped major version as this may break backward compatibility

## 6.0.1

- Minor fixes for rescue and decode mode help messages
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "blkar"
version = "6.0.1"
version = "7.0.0"
authors = ["Darren Ldl <darrenldldev@gmail.com>"]
edition = "2018"
build = "build.rs"
Expand Down
27 changes: 15 additions & 12 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,35 @@ fn real_main() -> i32 {
.version(env!("CARGO_PKG_VERSION"))
.author("Darren Ldl <darrenldldev@gmail.com>")
.about("Archive with forward error correction and sector level recoverability")
.subcommand(cli_encode::sub_command())
.subcommand(cli_calc::sub_command())
.subcommand(cli_check::sub_command())
.subcommand(cli_decode::sub_command())
.subcommand(cli_encode::sub_command())
.subcommand(cli_repair::sub_command())
.subcommand(cli_rescue::sub_command())
.subcommand(cli_show::sub_command())
.subcommand(cli_repair::sub_command())
.subcommand(cli_check::sub_command())
.subcommand(cli_sort::sub_command())
.subcommand(cli_calc::sub_command())
.subcommand(cli_update::sub_command())
.get_matches();

if let Some(matches) = matches.subcommand_matches("encode") {
cli_encode::encode(matches)
if let Some(matches) = matches.subcommand_matches("calc") {
cli_calc::calc(matches)
} else if let Some(matches) = matches.subcommand_matches("check") {
cli_check::check(matches)
} else if let Some(matches) = matches.subcommand_matches("decode") {
cli_decode::decode(matches)
} else if let Some(matches) = matches.subcommand_matches("encode") {
cli_encode::encode(matches)
} else if let Some(matches) = matches.subcommand_matches("repair") {
cli_repair::repair(matches)
} else if let Some(matches) = matches.subcommand_matches("rescue") {
cli_rescue::rescue(matches)
} else if let Some(matches) = matches.subcommand_matches("show") {
cli_show::show(matches)
} else if let Some(matches) = matches.subcommand_matches("repair") {
cli_repair::repair(matches)
} else if let Some(matches) = matches.subcommand_matches("check") {
cli_check::check(matches)
} else if let Some(matches) = matches.subcommand_matches("sort") {
cli_sort::sort(matches)
} else if let Some(matches) = matches.subcommand_matches("calc") {
cli_calc::calc(matches)
} else if let Some(matches) = matches.subcommand_matches("update") {
cli_update::update(matches)
} else {
exit_with_msg!(ok json_printer::JSONPrinter::new(false, output_channel::OutputChannel::Stdout)
=> "Invoke with -h or --help for help message\n");
Expand Down
4 changes: 2 additions & 2 deletions src/block_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use crate::integer_utils::IntegerUtils;
use std::collections::HashMap;

use crate::sbx_specs::{
ver_to_block_size, ver_to_usize, ver_uses_rs, SBX_LARGEST_BLOCK_SIZE,
SBX_MAX_BURST_ERR_RESISTANCE, SBX_SCAN_BLOCK_SIZE,
ver_to_block_size, ver_uses_rs, SBX_LARGEST_BLOCK_SIZE, SBX_MAX_BURST_ERR_RESISTANCE,
SBX_SCAN_BLOCK_SIZE,
};

use crate::progress_report::*;
Expand Down
3 changes: 0 additions & 3 deletions src/check_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@ use crate::sbx_specs::Version;
use crate::sbx_block;
use crate::sbx_specs::{ver_to_block_size, ver_to_usize, SBX_LARGEST_BLOCK_SIZE};

use crate::block_utils;
use crate::time_utils;

use crate::block_utils::RefBlockChoice;
use crate::misc_utils::{PositionOrLength, RangeEnd};

use crate::cli_utils::report_ref_block_info;

pub struct Param {
ref_block_choice: RefBlockChoice,
ref_block_from_pos: Option<u64>,
Expand Down
2 changes: 1 addition & 1 deletion src/cli_encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub fn encode<'a>(matches: &ArgMatches<'a>) -> i32 {
};

let in_file_mod_time_str = match in_file_mod_time {
None => "N/A".to_string(),
None => null_if_json_else_NA!(json_printer).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),
Expand Down
147 changes: 147 additions & 0 deletions src/cli_update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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),
}
}
6 changes: 3 additions & 3 deletions src/cli_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ pub fn pr_verbosity_level_arg<'a, 'b>() -> Arg<'a, 'b> {
.takes_value(true)
.help(
"Progress report verbosity level, one of :
0 (show nothing)
1 (only show progress stats when done)
(default) 2 (show both progress bar and progress stats)
(default in JSON mode) 0 (show nothing)
1 (only show progress stats when done)
(default in non-JSON mode) 2 (show both progress bar and progress stats)
This only affects progress text printing",
)
}
Expand Down
28 changes: 16 additions & 12 deletions src/decode_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ use crate::misc_utils::{PositionOrLength, RangeEnd};
use crate::multihash;
use crate::multihash::*;

use crate::cli_utils::report_ref_block_info;

use crate::general_error::Error;
use crate::sbx_specs::Version;

Expand Down Expand Up @@ -159,7 +157,7 @@ impl fmt::Display for Stats {
"Recorded hash {}: {}",
padding,
match *recorded_hash {
None => null_if_json_else!(json_printer, "N/A").to_string(),
None => null_if_json_else_NA!(json_printer).to_string(),
Some(ref h) => format!(
"{} - {}",
hash_type_to_string(h.0),
Expand All @@ -173,7 +171,7 @@ impl fmt::Display for Stats {
"Hash of output file {}: {}",
padding,
match (recorded_hash, computed_hash) {
(&None, &None) => null_if_json_else!(json_printer, "N/A").to_string(),
(&None, &None) => null_if_json_else_NA!(json_printer).to_string(),
(&Some(_), &None) => null_if_json_else!(
json_printer,
"N/A - recorded hash type is not supported by blkar"
Expand Down Expand Up @@ -236,7 +234,7 @@ impl fmt::Display for Stats {
"Recorded hash {}: {}",
padding,
match *recorded_hash {
None => null_if_json_else!(json_printer, "N/A").to_string(),
None => null_if_json_else_NA!(json_printer).to_string(),
Some(ref h) => format!(
"{} - {}",
hash_type_to_string(h.0),
Expand All @@ -250,7 +248,7 @@ impl fmt::Display for Stats {
"Hash of output file {}: {}",
padding,
match (recorded_hash, computed_hash) {
(&None, &None) => null_if_json_else!(json_printer, "N/A").to_string(),
(&None, &None) => null_if_json_else_NA!(json_printer).to_string(),
(&Some(_), &None) => null_if_json_else!(
json_printer,
"N/A - recorded hash type is not supported by blkar"
Expand Down Expand Up @@ -834,7 +832,7 @@ pub fn decode(

let mut hash_ctx = match stored_hash_bytes {
None => None,
Some((ht, _)) => match hash::Ctx::new(ht) {
Some(&(ht, _)) => match hash::Ctx::new(ht) {
Err(()) => None,
Ok(ctx) => Some(ctx),
},
Expand Down Expand Up @@ -1084,7 +1082,10 @@ pub fn decode(

let data_blocks_decoded = stats.lock().unwrap().data_blocks_decoded;

stats.lock().unwrap().recorded_hash = recorded_hash;
stats.lock().unwrap().recorded_hash = match recorded_hash {
Some(h) => Some(h.clone()),
None => None,
};

stats.lock().unwrap().out_file_size = match writer.get_file_size() {
Some(r) => r?,
Expand All @@ -1107,7 +1108,10 @@ fn hash(
let hash_bytes: Option<HashBytes> = if ref_block.is_data() {
None
} else {
ref_block.get_HSH().unwrap()
match ref_block.get_HSH().unwrap() {
Some(h) => Some(h.clone()),
None => None,
}
};

let mut hash_ctx: hash::Ctx = match hash_bytes {
Expand Down Expand Up @@ -1189,7 +1193,7 @@ pub fn decode_file(param: &Param) -> Result<Option<Stats>, Error> {
let out_file_path: Option<String> = match param.out_file {
None => match recorded_file_name {
None => {
return Err(Error::with_message("No original file name was found in SBX container and no output file name/path was provided"));
return Err(Error::with_msg("No original file name was found in SBX container and no output file name/path was provided"));
}
Some(ref x) => Some(file_utils::get_file_name_part_of_path(x)),
},
Expand All @@ -1199,7 +1203,7 @@ pub fn decode_file(param: &Param) -> Result<Option<Stats>, Error> {
} else if file_utils::check_if_file_is_dir(out) {
match recorded_file_name {
None => {
return Err(Error::with_message(&format!("No original file name was found in SBX container and \"{}\" is a directory",
return Err(Error::with_msg(&format!("No original file name was found in SBX container and \"{}\" is a directory",
&out)));
}
Some(x) => Some(misc_utils::make_path(&[out, &x])),
Expand All @@ -1214,7 +1218,7 @@ pub fn decode_file(param: &Param) -> Result<Option<Stats>, Error> {
if let Some(ref out_file_path) = out_file_path {
if !param.force_write && param.multi_pass == None {
if file_utils::check_if_file_exists(out_file_path) {
return Err(Error::with_message(&format!(
return Err(Error::with_msg(&format!(
"File \"{}\" already exists",
out_file_path
)));
Expand Down

0 comments on commit 69f086a

Please sign in to comment.