• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     cmdLine_ = std::move(cmdLine);
26     tokens_.clear();
27     tokens_ = Utils::SplitString(cmdLine_, " ");
28     type_ = ParseCommandType(tokens_[H_ZERO_NUMBER]);
29     return true;
30 }
31 
~Command()32 Command::~Command()
33 {
34     fd_.reset();
35 }
36 
GetCommandType() const37 CommandType Command::GetCommandType() const
38 {
39     return type_;
40 }
41 
GetArgumentByPos(size_t pos) const42 std::string Command::GetArgumentByPos(size_t pos) const
43 {
44     if (pos >= tokens_.size()) {
45         return "";
46     }
47     return tokens_[pos];
48 }
49 
GetCommandLine() const50 std::string Command::GetCommandLine() const
51 {
52     return cmdLine_;
53 }
54 
SetFileDescriptor(int fd)55 void Command::SetFileDescriptor(int fd)
56 {
57     fd_ = std::make_unique<int>(fd);
58 }
59 
GetFileDescriptor() const60 int Command::GetFileDescriptor() const
61 {
62     return *fd_;
63 }
64 
GetTransferParams() const65 TransferParams* Command::GetTransferParams() const
66 {
67     return transferParams_;
68 }
69 
ParseCommandType(const std::string & firstCmd)70 CommandType Command::ParseCommandType(const std::string &firstCmd)
71 {
72     if (firstCmd == "abort") {
73         return CommandType::ABORT;
74     } else if (firstCmd == "bsdiff") {
75         return CommandType::BSDIFF;
76     } else if (firstCmd == "erase") {
77         return CommandType::ERASE;
78     } else if (firstCmd == "free") {
79         return CommandType::FREE;
80     } else if (firstCmd == "pkgdiff") {
81         return CommandType::IMGDIFF;
82     } else if (firstCmd == "move") {
83         return CommandType::MOVE;
84     } else if (firstCmd == "new") {
85         return CommandType::NEW;
86     } else if (firstCmd == "stash") {
87         return CommandType::STASH;
88     } else if (firstCmd == "zero") {
89         return CommandType::ZERO;
90     }
91     return CommandType::LAST;
92 }
93 }
94