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/transfer_manager.h"
16 #include <fcntl.h>
17 #include <sstream>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include "applypatch/command_function.h"
21 #include "log/log.h"
22 #include "updater/updater_const.h"
23 #include "utils.h"
24 #include "applypatch/update_progress.h"
25 #include "thread_pool.h"
26
27 namespace Updater {
28 using namespace Updater::Utils;
29
TransferManager()30 TransferManager::TransferManager()
31 {
32 transferParams_ = std::make_unique<TransferParams>();
33 transferParams_->writerThreadInfo = std::make_unique<WriterThreadInfo>();
34 }
35
CommandsExecute(int fd,Command & cmd)36 bool TransferManager::CommandsExecute(int fd, Command &cmd)
37 {
38 cmd.SetFileDescriptor(fd);
39 CommandFunction* cf = CommandFunctionFactory::GetInstance().GetCommandFunction(cmd.GetCommandHead());
40 if (cf == nullptr) {
41 LOG(ERROR) << "Failed to get cmd exec";
42 return false;
43 }
44 CommandResult ret = cf->Execute(cmd);
45 if (!CheckResult(ret, cmd.GetCommandLine(), cmd.GetCommandType())) {
46 return false;
47 }
48 return true;
49 }
50
CommandsParser(int fd,const std::vector<std::string> & context)51 bool TransferManager::CommandsParser(int fd, const std::vector<std::string> &context)
52 {
53 if (context.size() < 1) {
54 LOG(ERROR) << "too small context in transfer file";
55 return false;
56 }
57 if (transferParams_ == nullptr) {
58 LOG(ERROR) << "transferParams_ is nullptr";
59 return false;
60 }
61
62 std::vector<std::string>::const_iterator ct = context.begin();
63 transferParams_->version = Utils::String2Int<size_t>(*ct++, Utils::N_DEC);
64 transferParams_->blockCount = Utils::String2Int<size_t>(*ct++, Utils::N_DEC);
65 transferParams_->maxEntries = Utils::String2Int<size_t>(*ct++, Utils::N_DEC);
66 transferParams_->maxBlocks = Utils::String2Int<size_t>(*ct++, Utils::N_DEC);
67 size_t totalSize = transferParams_->blockCount;
68 std::string retryCmd = "";
69 if (transferParams_->env != nullptr && transferParams_->env->IsRetry()) {
70 retryCmd = ReloadForRetry();
71 }
72 size_t initBlock = 0;
73 for (; ct != context.end(); ct++) {
74 std::unique_ptr<Command> cmd = std::make_unique<Command>(transferParams_.get());
75 if (cmd == nullptr) {
76 LOG(ERROR) << "Failed to parse command line.";
77 return false;
78 }
79 if (!cmd->Init(*ct) || transferParams_->env == nullptr) {
80 continue;
81 }
82 if (!retryCmd.empty() && transferParams_->env->IsRetry()) {
83 if (*ct == retryCmd) {
84 retryCmd.clear();
85 }
86 if (cmd->GetCommandType() != CommandType::NEW) {
87 LOG(INFO) << "Retry: Command " << *ct << " passed";
88 continue;
89 }
90 }
91 if (!CommandsExecute(fd, *cmd)) {
92 LOG(ERROR) << "Running command : " << cmd->GetCommandLine() << " fail";
93 return false;
94 }
95 if (initBlock == 0) {
96 initBlock = transferParams_->written;
97 }
98 if (totalSize != 0 && (transferParams_->written - initBlock) > 0) {
99 UpdateProgress(initBlock, totalSize);
100 }
101 }
102 if (fabs(Uscript::GetScriptProportion() - 1.0f) < 1e-6) {
103 FillUpdateProgress();
104 }
105 return true;
106 }
107
UpdateProgress(size_t & initBlock,size_t totalSize)108 void TransferManager::UpdateProgress(size_t &initBlock, size_t totalSize)
109 {
110 float p = static_cast<float>(transferParams_->written - initBlock) / totalSize\
111 * Uscript::GetScriptProportion();
112 SetUpdateProgress(p);
113 initBlock = transferParams_->written;
114 }
115
RegisterForRetry(const std::string & cmd)116 bool TransferManager::RegisterForRetry(const std::string &cmd)
117 {
118 std::string path = transferParams_->retryFile;
119 int fd = open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
120 if (fd == -1) {
121 LOG(ERROR) << "Failed to create";
122 return false;
123 }
124 bool ret = Utils::WriteStringToFile(fd, cmd);
125 if (ret == false) {
126 LOG(ERROR) << "Write retry flag error";
127 }
128 fsync(fd);
129 close(fd);
130 return ret;
131 }
132
ReloadForRetry() const133 std::string TransferManager::ReloadForRetry() const
134 {
135 std::string path = transferParams_->retryFile;
136 int fd = open(path.c_str(), O_RDONLY);
137 if (fd < 0) {
138 LOG(ERROR) << "Failed to open";
139 return "";
140 }
141 (void)lseek(fd, 0, SEEK_SET);
142 std::string cmd = "";
143 if (!Utils::ReadFileToString(fd, cmd)) {
144 LOG(ERROR) << "Error to read retry flag";
145 }
146 close(fd);
147 return cmd;
148 }
149
CheckResult(const CommandResult result,const std::string & cmd,const CommandType & type)150 bool TransferManager::CheckResult(const CommandResult result, const std::string &cmd, const CommandType &type)
151 {
152 switch (result) {
153 case SUCCESS:
154 if (type != CommandType::NEW) {
155 RegisterForRetry(cmd);
156 }
157 break;
158 case NEED_RETRY:
159 LOG(INFO) << "Running command need retry!";
160 if (transferParams_->env != nullptr) {
161 transferParams_->env->PostMessage("retry_update", IO_FAILED_REBOOT);
162 }
163 return false;
164 case FAILED:
165 default:
166 LOG(ERROR) << "Running command failed";
167 return false;
168 }
169 return true;
170 }
171 } // namespace Updater
172