• 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 "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     PKG_CHECK(manager != nullptr, return tmpVersionList, "manager is null");
80 
81     PkgManager::StreamPtr outStream = nullptr;
82     const string pattern = "\n";
83     const FileInfo *info = manager->GetFileInfo(versionList);
84     PKG_CHECK(info != nullptr, return tmpVersionList, "Can not get file info %s", versionList.c_str());
85     int32_t ret = manager->CreatePkgStream(outStream, versionList, info->unpackedSize,
86         PkgStream::PkgStreamType_MemoryMap);
87     PKG_CHECK(outStream, return tmpVersionList, "Create stream fail %s", versionList.c_str());
88     ret = manager->ExtractFile(versionList, outStream);
89     PKG_CHECK(ret == PKG_SUCCESS, manager->ClosePkgStream(outStream);
90         return tmpVersionList, "ExtractFile versionList fail ");
91     PkgBuffer data = {};
92     ret = outStream->GetBuffer(data);
93     PKG_CHECK(ret == PKG_SUCCESS, manager->ClosePkgStream(outStream);
94         return tmpVersionList, "ExtractFile fail %s", versionList.c_str());
95     std::string str(reinterpret_cast<char*>(data.buffer), data.length);
96     tmpVersionList = SplitString(str, pattern);
97     manager->ClosePkgStream(outStream);
98     return tmpVersionList;
99 }
100 
GetBoardID(hpackage::PkgManager::PkgManagerPtr manager,const std::string & boardIdName,const std::string & boardListPath)101 std::vector<std::string> PackagesInfo::GetBoardID(hpackage::PkgManager::PkgManagerPtr manager,
102     const std::string &boardIdName, const std::string &boardListPath)
103 {
104     vector<string> boardlist;
105     PKG_CHECK(manager != nullptr, return boardlist, "manager is null");
106     (void)boardListPath;
107     PkgManager::StreamPtr outStream = nullptr;
108     const FileInfo *info = manager->GetFileInfo(boardIdName);
109     PKG_CHECK(info != nullptr, return boardlist, "Can not get file info %s", boardIdName.c_str());
110     int32_t ret = manager->CreatePkgStream(outStream, boardIdName, info->unpackedSize,
111         PkgStream::PkgStreamType_MemoryMap);
112     PKG_CHECK(outStream, return boardlist, "Create stream fail %s", boardIdName.c_str());
113     ret = manager->ExtractFile(boardIdName, outStream);
114     PkgBuffer data = {};
115     ret = outStream->GetBuffer(data);
116     PKG_CHECK(ret == PKG_SUCCESS, manager->ClosePkgStream(outStream);
117         return boardlist, "ExtractFile fail %s", boardIdName.c_str());
118     std::string str(reinterpret_cast<char*>(data.buffer), data.length);
119     boardlist = SplitString(str, "\n");
120     manager->ClosePkgStream(outStream);
121     return boardlist;
122 }
123 
IsAllowRollback()124 bool PackagesInfo::IsAllowRollback()
125 {
126     return false;
127 }
128 } // namespace updater
129