1fb299fa2Sopenharmony_ci/* 2fb299fa2Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at 6fb299fa2Sopenharmony_ci * 7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb299fa2Sopenharmony_ci * 9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and 13fb299fa2Sopenharmony_ci * limitations under the License. 14fb299fa2Sopenharmony_ci */ 15fb299fa2Sopenharmony_ci#include "applypatch/command.h" 16fb299fa2Sopenharmony_ci#include <cstdio> 17fb299fa2Sopenharmony_ci#include <vector> 18fb299fa2Sopenharmony_ci#include "applypatch/block_set.h" 19fb299fa2Sopenharmony_ci#include "log/log.h" 20fb299fa2Sopenharmony_ci#include "utils.h" 21fb299fa2Sopenharmony_ci 22fb299fa2Sopenharmony_cinamespace Updater { 23fb299fa2Sopenharmony_cibool Command::Init(const std::string &cmdLine) 24fb299fa2Sopenharmony_ci{ 25fb299fa2Sopenharmony_ci if (cmdLine.empty()) return false; 26fb299fa2Sopenharmony_ci cmdLine_ = std::move(cmdLine); 27fb299fa2Sopenharmony_ci tokens_.clear(); 28fb299fa2Sopenharmony_ci tokens_ = Utils::SplitString(cmdLine_, " "); 29fb299fa2Sopenharmony_ci cmdhead_ = tokens_[H_ZERO_NUMBER]; 30fb299fa2Sopenharmony_ci type_ = ParseCommandType(tokens_[H_ZERO_NUMBER]); 31fb299fa2Sopenharmony_ci return true; 32fb299fa2Sopenharmony_ci} 33fb299fa2Sopenharmony_ci 34fb299fa2Sopenharmony_ciCommand::~Command() 35fb299fa2Sopenharmony_ci{ 36fb299fa2Sopenharmony_ci fd_.reset(); 37fb299fa2Sopenharmony_ci} 38fb299fa2Sopenharmony_ci 39fb299fa2Sopenharmony_ciCommandType Command::GetCommandType() const 40fb299fa2Sopenharmony_ci{ 41fb299fa2Sopenharmony_ci return type_; 42fb299fa2Sopenharmony_ci} 43fb299fa2Sopenharmony_ci 44fb299fa2Sopenharmony_cistd::string Command::GetCommandHead() const 45fb299fa2Sopenharmony_ci{ 46fb299fa2Sopenharmony_ci return cmdhead_; 47fb299fa2Sopenharmony_ci} 48fb299fa2Sopenharmony_ci 49fb299fa2Sopenharmony_cistd::string Command::GetArgumentByPos(size_t pos) const 50fb299fa2Sopenharmony_ci{ 51fb299fa2Sopenharmony_ci if (pos >= tokens_.size()) { 52fb299fa2Sopenharmony_ci return ""; 53fb299fa2Sopenharmony_ci } 54fb299fa2Sopenharmony_ci return tokens_[pos]; 55fb299fa2Sopenharmony_ci} 56fb299fa2Sopenharmony_ci 57fb299fa2Sopenharmony_cistd::string Command::GetCommandLine() const 58fb299fa2Sopenharmony_ci{ 59fb299fa2Sopenharmony_ci return cmdLine_; 60fb299fa2Sopenharmony_ci} 61fb299fa2Sopenharmony_ci 62fb299fa2Sopenharmony_civoid Command::SetFileDescriptor(int fd) 63fb299fa2Sopenharmony_ci{ 64fb299fa2Sopenharmony_ci fd_ = std::make_unique<int>(fd); 65fb299fa2Sopenharmony_ci} 66fb299fa2Sopenharmony_ci 67fb299fa2Sopenharmony_ciint Command::GetFileDescriptor() const 68fb299fa2Sopenharmony_ci{ 69fb299fa2Sopenharmony_ci return *fd_; 70fb299fa2Sopenharmony_ci} 71fb299fa2Sopenharmony_ci 72fb299fa2Sopenharmony_ciTransferParams* Command::GetTransferParams() const 73fb299fa2Sopenharmony_ci{ 74fb299fa2Sopenharmony_ci return transferParams_; 75fb299fa2Sopenharmony_ci} 76fb299fa2Sopenharmony_ci 77fb299fa2Sopenharmony_ciCommandType Command::ParseCommandType(const std::string &firstCmd) 78fb299fa2Sopenharmony_ci{ 79fb299fa2Sopenharmony_ci if (firstCmd == "abort") { 80fb299fa2Sopenharmony_ci return CommandType::ABORT; 81fb299fa2Sopenharmony_ci } else if (firstCmd == "bsdiff") { 82fb299fa2Sopenharmony_ci return CommandType::BSDIFF; 83fb299fa2Sopenharmony_ci } else if (firstCmd == "erase") { 84fb299fa2Sopenharmony_ci return CommandType::ERASE; 85fb299fa2Sopenharmony_ci } else if (firstCmd == "free") { 86fb299fa2Sopenharmony_ci return CommandType::FREE; 87fb299fa2Sopenharmony_ci } else if (firstCmd == "pkgdiff") { 88fb299fa2Sopenharmony_ci return CommandType::IMGDIFF; 89fb299fa2Sopenharmony_ci } else if (firstCmd == "move") { 90fb299fa2Sopenharmony_ci return CommandType::MOVE; 91fb299fa2Sopenharmony_ci } else if (firstCmd == "new") { 92fb299fa2Sopenharmony_ci return CommandType::NEW; 93fb299fa2Sopenharmony_ci } else if (firstCmd == "stash") { 94fb299fa2Sopenharmony_ci return CommandType::STASH; 95fb299fa2Sopenharmony_ci } else if (firstCmd == "zero") { 96fb299fa2Sopenharmony_ci return CommandType::ZERO; 97fb299fa2Sopenharmony_ci } 98fb299fa2Sopenharmony_ci return CommandType::LAST; 99fb299fa2Sopenharmony_ci} 100fb299fa2Sopenharmony_ci} 101