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 
22 namespace Updater {
Init(const std::string & cmdLine)23 bool 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 
~Command()34 Command::~Command()
35 {
36     fd_.reset();
37 }
38 
GetCommandType() const39 CommandType Command::GetCommandType() const
40 {
41     return type_;
42 }
43 
GetCommandHead() const44 std::string Command::GetCommandHead() const
45 {
46     return cmdhead_;
47 }
48 
GetArgumentByPos(size_t pos) const49 std::string Command::GetArgumentByPos(size_t pos) const
50 {
51     if (pos >= tokens_.size()) {
52         return "";
53     }
54     return tokens_[pos];
55 }
56 
GetCommandLine() const57 std::string Command::GetCommandLine() const
58 {
59     return cmdLine_;
60 }
61 
SetFileDescriptor(int fd)62 void Command::SetFileDescriptor(int fd)
63 {
64     fd_ = std::make_unique<int>(fd);
65 }
66 
GetFileDescriptor() const67 int Command::GetFileDescriptor() const
68 {
69     return *fd_;
70 }
71 
GetTransferParams() const72 TransferParams* Command::GetTransferParams() const
73 {
74     return transferParams_;
75 }
76 
ParseCommandType(const std::string & firstCmd)77 CommandType 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