• 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 
ParseCommandType(const std::string & firstCmd)65 CommandType Command::ParseCommandType(const std::string &firstCmd)
66 {
67     if (firstCmd == "abort") {
68         return CommandType::ABORT;
69     } else if (firstCmd == "bsdiff") {
70         return CommandType::BSDIFF;
71     } else if (firstCmd == "erase") {
72         return CommandType::ERASE;
73     } else if (firstCmd == "free") {
74         return CommandType::FREE;
75     } else if (firstCmd == "pkgdiff") {
76         return CommandType::IMGDIFF;
77     } else if (firstCmd == "move") {
78         return CommandType::MOVE;
79     } else if (firstCmd == "new") {
80         return CommandType::NEW;
81     } else if (firstCmd == "stash") {
82         return CommandType::STASH;
83     } else if (firstCmd == "zero") {
84         return CommandType::ZERO;
85     }
86     return CommandType::LAST;
87 }
88 }
89