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 "packages_info.h"
16 #include <cstring>
17 #include <iostream>
18 #include "log.h"
19 #include "pkg_manager.h"
20 #include "pkg_manager_impl.h"
21 #include "pkg_pkgfile.h"
22 #include "pkg_stream.h"
23 #include "pkg_upgradefile.h"
24 #include "pkg_utils.h"
25
26 using namespace std;
27 using namespace Hpackage;
28
29 namespace Updater {
SplitString(const string & str,const string & pattern)30 vector<string> SplitString(const string &str, const string &pattern)
31 {
32 vector<string> ret;
33 if (pattern.empty()) {
34 return ret;
35 }
36 size_t start = 0;
37 size_t index = str.find_first_of(pattern, 0);
38 while (index != str.npos) {
39 if (start != index) {
40 string tmpStr = str.substr(start, index - start);
41 tmpStr.erase(0, tmpStr.find_first_not_of(" "));
42 tmpStr.erase(tmpStr.find_last_not_of(" ") + 1);
43 tmpStr.erase(tmpStr.find_last_not_of("\r") + 1);
44 ret.push_back(tmpStr);
45 }
46 start = index + 1;
47 index = str.find_first_of(pattern, start);
48 }
49
50 if (!str.substr(start).empty()) {
51 string tmpStr = str.substr(start);
52 tmpStr.erase(0, tmpStr.find_first_not_of(" "));
53 tmpStr.erase(tmpStr.find_last_not_of(" ") + 1);
54 tmpStr.erase(tmpStr.find_last_not_of("\r") + 1);
55 ret.push_back(tmpStr);
56 }
57 return ret;
58 }
59
60 static PackagesInfoPtr g_packagesInfoInstance = nullptr;
GetPackagesInfoInstance()61 PackagesInfoPtr PackagesInfo::GetPackagesInfoInstance()
62 {
63 if (g_packagesInfoInstance == nullptr) {
64 g_packagesInfoInstance = new PackagesInfo();
65 }
66 return g_packagesInfoInstance;
67 }
68
ReleasePackagesInfoInstance(PackagesInfoPtr pkginfomanager)69 void PackagesInfo::ReleasePackagesInfoInstance(PackagesInfoPtr pkginfomanager)
70 {
71 delete pkginfomanager;
72 g_packagesInfoInstance = nullptr;
73 }
74
GetOTAVersion(Hpackage::PkgManager::PkgManagerPtr manager,const std::string & versionList,const std::string & versionPath)75 std::vector<std::string> PackagesInfo::GetOTAVersion(Hpackage::PkgManager::PkgManagerPtr manager,
76 const std::string &versionList, const std::string &versionPath)
77 {
78 vector<string> tmpVersionList;
79 if (manager == nullptr) {
80 PKG_LOGE("manager is null");
81 return tmpVersionList;
82 }
83
84 PkgManager::StreamPtr outStream = nullptr;
85 const string pattern = "\n";
86 const FileInfo *info = manager->GetFileInfo(versionList);
87 if (info == nullptr) {
88 PKG_LOGE("Can not get file info %s", versionList.c_str());
89 return tmpVersionList;
90 }
91 int32_t ret = manager->CreatePkgStream(outStream, versionList, info->unpackedSize,
92 PkgStream::PkgStreamType_MemoryMap);
93 if (outStream == nullptr || ret == -1) {
94 PKG_LOGE("Create stream fail %s", versionList.c_str());
95 return tmpVersionList;
96 }
97 ret = manager->ExtractFile(versionList, outStream);
98 if (ret != PKG_SUCCESS) {
99 PKG_LOGE("ExtractFile versionList fail ");
100 manager->ClosePkgStream(outStream);
101 return tmpVersionList;
102 }
103 PkgBuffer data = {};
104 ret = outStream->GetBuffer(data);
105 if (ret != PKG_SUCCESS) {
106 PKG_LOGE("ExtractFile fail %s", versionList.c_str());
107 manager->ClosePkgStream(outStream);
108 return tmpVersionList;
109 }
110 std::string str(reinterpret_cast<char*>(data.buffer), data.length);
111 tmpVersionList = SplitString(str, pattern);
112 manager->ClosePkgStream(outStream);
113 return tmpVersionList;
114 }
115
GetBoardID(Hpackage::PkgManager::PkgManagerPtr manager,const std::string & boardIdName,const std::string & boardListPath)116 std::vector<std::string> PackagesInfo::GetBoardID(Hpackage::PkgManager::PkgManagerPtr manager,
117 const std::string &boardIdName, const std::string &boardListPath)
118 {
119 vector<string> boardlist;
120 if (manager == nullptr) {
121 PKG_LOGE("manager is null");
122 return boardlist;
123 }
124 (void)boardListPath;
125 PkgManager::StreamPtr outStream = nullptr;
126 const FileInfo *info = manager->GetFileInfo(boardIdName);
127 if (info == nullptr) {
128 PKG_LOGE("Can not get file info %s", boardIdName.c_str());
129 return boardlist;
130 }
131 int32_t ret = manager->CreatePkgStream(outStream, boardIdName, info->unpackedSize,
132 PkgStream::PkgStreamType_MemoryMap);
133 if (outStream == nullptr || ret != PKG_SUCCESS) {
134 PKG_LOGE("Create stream fail %s", boardIdName.c_str());
135 return boardlist;
136 }
137 ret = manager->ExtractFile(boardIdName, outStream);
138 if (ret != PKG_SUCCESS) {
139 PKG_LOGE("ExtractFile error, ret = %d", ret);
140 return boardlist;
141 }
142 PkgBuffer data = {};
143 ret = outStream->GetBuffer(data);
144 if (ret != PKG_SUCCESS) {
145 PKG_LOGE("ExtractFile fail %s", boardIdName.c_str());
146 manager->ClosePkgStream(outStream);
147 return boardlist;
148 }
149 std::string str(reinterpret_cast<char*>(data.buffer), data.length);
150 boardlist = SplitString(str, "\n");
151 manager->ClosePkgStream(outStream);
152 return boardlist;
153 }
154
IsAllowRollback()155 bool PackagesInfo::IsAllowRollback()
156 {
157 return false;
158 }
159 } // namespace Updater
160