From 8f64bf635fb021861288222ae5c741cd8fb22f03 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Tue, 7 Aug 2018 00:22:19 -0700 Subject: Add the hash_tree_info class in Command Add hash_tree_info to represent the hash tree computation arguments in the transfer commands 'compute_hash_tree'. Also add its parsing code in the Command class. Bug: 25170618 Test: unit tests pass Change-Id: Ie8607968377968e8fb3e58d1af0b8ca315e145be --- updater/commands.cpp | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'updater/commands.cpp') diff --git a/updater/commands.cpp b/updater/commands.cpp index 15a787c51..4a90ea873 100644 --- a/updater/commands.cpp +++ b/updater/commands.cpp @@ -31,6 +31,14 @@ using namespace std::string_literals; bool Command::abort_allowed_ = false; +Command::Command(Type type, size_t index, std::string cmdline, HashTreeInfo hash_tree_info) + : type_(type), + index_(index), + cmdline_(std::move(cmdline)), + hash_tree_info_(std::move(hash_tree_info)) { + CHECK(type == Type::COMPUTE_HASH_TREE); +} + Command::Type Command::ParseType(const std::string& type_str) { if (type_str == "abort") { if (!abort_allowed_) { @@ -177,7 +185,6 @@ Command Command::Parse(const std::string& line, size_t index, std::string* err) SourceInfo source_info; StashInfo stash_info; - // TODO(xunchang) add the parse code of compute_hash_tree if (op == Type::ZERO || op == Type::NEW || op == Type::ERASE) { // zero/new/erase if (pos + 1 != tokens.size()) { @@ -255,6 +262,39 @@ Command Command::Parse(const std::string& line, size_t index, std::string* err) tokens.size() - pos); return {}; } + } else if (op == Type::COMPUTE_HASH_TREE) { + // + if (pos + 5 != tokens.size()) { + *err = android::base::StringPrintf("invalid number of args: %zu (expected 5)", + tokens.size() - pos); + return {}; + } + + // Expects the hash_tree data to be contiguous. + RangeSet hash_tree_ranges = RangeSet::Parse(tokens[pos++]); + if (!hash_tree_ranges || hash_tree_ranges.size() != 1) { + *err = "invalid hash tree ranges in: " + line; + return {}; + } + + RangeSet source_ranges = RangeSet::Parse(tokens[pos++]); + if (!source_ranges) { + *err = "invalid source ranges in: " + line; + return {}; + } + + std::string hash_algorithm = tokens[pos++]; + std::string salt_hex = tokens[pos++]; + std::string root_hash = tokens[pos++]; + if (hash_algorithm.empty() || salt_hex.empty() || root_hash.empty()) { + *err = "invalid hash tree arguments in " + line; + return {}; + } + + HashTreeInfo hash_tree_info(std::move(hash_tree_ranges), std::move(source_ranges), + std::move(hash_algorithm), std::move(salt_hex), + std::move(root_hash)); + return Command(op, index, line, std::move(hash_tree_info)); } else { *err = "invalid op"; return {}; -- cgit v1.2.3