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