• 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     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     srcFd_.reset();
37     targetFd_.reset();
38 }
39 
GetCommandType() const40 CommandType Command::GetCommandType() const
41 {
42     return type_;
43 }
44 
GetCommandHead() const45 std::string Command::GetCommandHead() const
46 {
47     return cmdhead_;
48 }
49 
GetArgumentByPos(size_t pos) const50 std::string Command::GetArgumentByPos(size_t pos) const
51 {
52     if (pos >= tokens_.size()) {
53         return "";
54     }
55     return tokens_[pos];
56 }
57 
GetCommandLine() const58 std::string Command::GetCommandLine() const
59 {
60     return cmdLine_;
61 }
62 
SetSrcFileDescriptor(int fd)63 void Command::SetSrcFileDescriptor(int fd)
64 {
65     srcFd_ = std::make_unique<int>(fd);
66 }
67 
GetSrcFileDescriptor() const68 int Command::GetSrcFileDescriptor() const
69 {
70     return *srcFd_;
71 }
72 
SetTargetFileDescriptor(int fd)73 void Command::SetTargetFileDescriptor(int fd)
74 {
75     targetFd_ = std::make_unique<int>(fd);
76 }
77 
GetTargetFileDescriptor() const78 int Command::GetTargetFileDescriptor() const
79 {
80     return *targetFd_;
81 }
82 
SetIsStreamCmd(bool isStreamCmd)83 void Command::SetIsStreamCmd(bool isStreamCmd)
84 {
85     isStreamCmd_ = isStreamCmd;
86 }
87 
IsStreamCmd() const88 bool Command::IsStreamCmd() const
89 {
90     return isStreamCmd_;
91 }
92 
GetTransferParams() const93 TransferParams* Command::GetTransferParams() const
94 {
95     return transferParams_;
96 }
97 
ParseCommandType(const std::string & firstCmd)98 CommandType Command::ParseCommandType(const std::string &firstCmd)
99 {
100     if (firstCmd == "abort") {
101         return CommandType::ABORT;
102     } else if (firstCmd == "bsdiff") {
103         return CommandType::BSDIFF;
104     } else if (firstCmd == "erase") {
105         return CommandType::ERASE;
106     } else if (firstCmd == "free") {
107         return CommandType::FREE;
108     } else if (firstCmd == "pkgdiff") {
109         return CommandType::IMGDIFF;
110     } else if (firstCmd == "move") {
111         return CommandType::MOVE;
112     } else if (firstCmd == "new") {
113         return CommandType::NEW;
114     } else if (firstCmd == "stash") {
115         return CommandType::STASH;
116     } else if (firstCmd == "zero") {
117         return CommandType::ZERO;
118     } else if (firstCmd == "copy") {
119         return CommandType::COPY;
120     }
121     return CommandType::LAST;
122 }
123 }
124