1fb299fa2Sopenharmony_ci/* 2fb299fa2Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at 6fb299fa2Sopenharmony_ci * 7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb299fa2Sopenharmony_ci * 9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and 13fb299fa2Sopenharmony_ci * limitations under the License. 14fb299fa2Sopenharmony_ci */ 15fb299fa2Sopenharmony_ci#include "packages_info.h" 16fb299fa2Sopenharmony_ci#include <cstring> 17fb299fa2Sopenharmony_ci#include <iostream> 18fb299fa2Sopenharmony_ci#include "log.h" 19fb299fa2Sopenharmony_ci#include "pkg_manager.h" 20fb299fa2Sopenharmony_ci#include "pkg_manager_impl.h" 21fb299fa2Sopenharmony_ci#include "pkg_pkgfile.h" 22fb299fa2Sopenharmony_ci#include "pkg_stream.h" 23fb299fa2Sopenharmony_ci#include "pkg_upgradefile.h" 24fb299fa2Sopenharmony_ci#include "pkg_utils.h" 25fb299fa2Sopenharmony_ci 26fb299fa2Sopenharmony_ciusing namespace std; 27fb299fa2Sopenharmony_ciusing namespace Hpackage; 28fb299fa2Sopenharmony_ci 29fb299fa2Sopenharmony_cinamespace Updater { 30fb299fa2Sopenharmony_civector<string> SplitString(const string &str, const string &pattern) 31fb299fa2Sopenharmony_ci{ 32fb299fa2Sopenharmony_ci vector<string> ret; 33fb299fa2Sopenharmony_ci if (pattern.empty()) { 34fb299fa2Sopenharmony_ci return ret; 35fb299fa2Sopenharmony_ci } 36fb299fa2Sopenharmony_ci size_t start = 0; 37fb299fa2Sopenharmony_ci size_t index = str.find_first_of(pattern, 0); 38fb299fa2Sopenharmony_ci while (index != str.npos) { 39fb299fa2Sopenharmony_ci if (start != index) { 40fb299fa2Sopenharmony_ci string tmpStr = str.substr(start, index - start); 41fb299fa2Sopenharmony_ci tmpStr.erase(0, tmpStr.find_first_not_of(" ")); 42fb299fa2Sopenharmony_ci tmpStr.erase(tmpStr.find_last_not_of(" ") + 1); 43fb299fa2Sopenharmony_ci tmpStr.erase(tmpStr.find_last_not_of("\r") + 1); 44fb299fa2Sopenharmony_ci ret.push_back(tmpStr); 45fb299fa2Sopenharmony_ci } 46fb299fa2Sopenharmony_ci start = index + 1; 47fb299fa2Sopenharmony_ci index = str.find_first_of(pattern, start); 48fb299fa2Sopenharmony_ci } 49fb299fa2Sopenharmony_ci 50fb299fa2Sopenharmony_ci if (!str.substr(start).empty()) { 51fb299fa2Sopenharmony_ci string tmpStr = str.substr(start); 52fb299fa2Sopenharmony_ci tmpStr.erase(0, tmpStr.find_first_not_of(" ")); 53fb299fa2Sopenharmony_ci tmpStr.erase(tmpStr.find_last_not_of(" ") + 1); 54fb299fa2Sopenharmony_ci tmpStr.erase(tmpStr.find_last_not_of("\r") + 1); 55fb299fa2Sopenharmony_ci ret.push_back(tmpStr); 56fb299fa2Sopenharmony_ci } 57fb299fa2Sopenharmony_ci return ret; 58fb299fa2Sopenharmony_ci} 59fb299fa2Sopenharmony_ci 60fb299fa2Sopenharmony_cistatic PackagesInfoPtr g_packagesInfoInstance = nullptr; 61fb299fa2Sopenharmony_ciPackagesInfoPtr PackagesInfo::GetPackagesInfoInstance() 62fb299fa2Sopenharmony_ci{ 63fb299fa2Sopenharmony_ci if (g_packagesInfoInstance == nullptr) { 64fb299fa2Sopenharmony_ci g_packagesInfoInstance = new PackagesInfo(); 65fb299fa2Sopenharmony_ci } 66fb299fa2Sopenharmony_ci return g_packagesInfoInstance; 67fb299fa2Sopenharmony_ci} 68fb299fa2Sopenharmony_ci 69fb299fa2Sopenharmony_civoid PackagesInfo::ReleasePackagesInfoInstance(PackagesInfoPtr pkginfomanager) 70fb299fa2Sopenharmony_ci{ 71fb299fa2Sopenharmony_ci delete pkginfomanager; 72fb299fa2Sopenharmony_ci g_packagesInfoInstance = nullptr; 73fb299fa2Sopenharmony_ci} 74fb299fa2Sopenharmony_ci 75fb299fa2Sopenharmony_cistd::vector<std::string> PackagesInfo::GetOTAVersion(Hpackage::PkgManager::PkgManagerPtr manager, 76fb299fa2Sopenharmony_ci const std::string &versionList, const std::string &versionPath) 77fb299fa2Sopenharmony_ci{ 78fb299fa2Sopenharmony_ci vector<string> tmpVersionList; 79fb299fa2Sopenharmony_ci if (manager == nullptr) { 80fb299fa2Sopenharmony_ci PKG_LOGE("manager is null"); 81fb299fa2Sopenharmony_ci return tmpVersionList; 82fb299fa2Sopenharmony_ci } 83fb299fa2Sopenharmony_ci 84fb299fa2Sopenharmony_ci PkgManager::StreamPtr outStream = nullptr; 85fb299fa2Sopenharmony_ci const string pattern = "\n"; 86fb299fa2Sopenharmony_ci const FileInfo *info = manager->GetFileInfo(versionList); 87fb299fa2Sopenharmony_ci if (info == nullptr) { 88fb299fa2Sopenharmony_ci PKG_LOGE("Can not get file info %s", versionList.c_str()); 89fb299fa2Sopenharmony_ci return tmpVersionList; 90fb299fa2Sopenharmony_ci } 91fb299fa2Sopenharmony_ci int32_t ret = manager->CreatePkgStream(outStream, versionList, info->unpackedSize, 92fb299fa2Sopenharmony_ci PkgStream::PkgStreamType_MemoryMap); 93fb299fa2Sopenharmony_ci if (outStream == nullptr) { 94fb299fa2Sopenharmony_ci PKG_LOGE("Create stream fail %s", versionList.c_str()); 95fb299fa2Sopenharmony_ci return tmpVersionList; 96fb299fa2Sopenharmony_ci } 97fb299fa2Sopenharmony_ci ret = manager->ExtractFile(versionList, outStream); 98fb299fa2Sopenharmony_ci if (ret != PKG_SUCCESS) { 99fb299fa2Sopenharmony_ci PKG_LOGE("ExtractFile versionList fail "); 100fb299fa2Sopenharmony_ci manager->ClosePkgStream(outStream); 101fb299fa2Sopenharmony_ci return tmpVersionList; 102fb299fa2Sopenharmony_ci } 103fb299fa2Sopenharmony_ci PkgBuffer data = {}; 104fb299fa2Sopenharmony_ci ret = outStream->GetBuffer(data); 105fb299fa2Sopenharmony_ci if (ret != PKG_SUCCESS) { 106fb299fa2Sopenharmony_ci PKG_LOGE("ExtractFile fail %s", versionList.c_str()); 107fb299fa2Sopenharmony_ci manager->ClosePkgStream(outStream); 108fb299fa2Sopenharmony_ci return tmpVersionList; 109fb299fa2Sopenharmony_ci } 110fb299fa2Sopenharmony_ci std::string str(reinterpret_cast<char*>(data.buffer), data.length); 111fb299fa2Sopenharmony_ci tmpVersionList = SplitString(str, pattern); 112fb299fa2Sopenharmony_ci manager->ClosePkgStream(outStream); 113fb299fa2Sopenharmony_ci return tmpVersionList; 114fb299fa2Sopenharmony_ci} 115fb299fa2Sopenharmony_ci 116fb299fa2Sopenharmony_cistd::vector<std::string> PackagesInfo::GetBoardID(Hpackage::PkgManager::PkgManagerPtr manager, 117fb299fa2Sopenharmony_ci const std::string &boardIdName, const std::string &boardListPath) 118fb299fa2Sopenharmony_ci{ 119fb299fa2Sopenharmony_ci vector<string> boardlist; 120fb299fa2Sopenharmony_ci if (manager == nullptr) { 121fb299fa2Sopenharmony_ci PKG_LOGE("manager is null"); 122fb299fa2Sopenharmony_ci return boardlist; 123fb299fa2Sopenharmony_ci } 124fb299fa2Sopenharmony_ci (void)boardListPath; 125fb299fa2Sopenharmony_ci PkgManager::StreamPtr outStream = nullptr; 126fb299fa2Sopenharmony_ci const FileInfo *info = manager->GetFileInfo(boardIdName); 127fb299fa2Sopenharmony_ci if (info == nullptr) { 128fb299fa2Sopenharmony_ci PKG_LOGE("Can not get file info %s", boardIdName.c_str()); 129fb299fa2Sopenharmony_ci return boardlist; 130fb299fa2Sopenharmony_ci } 131fb299fa2Sopenharmony_ci int32_t ret = manager->CreatePkgStream(outStream, boardIdName, info->unpackedSize, 132fb299fa2Sopenharmony_ci PkgStream::PkgStreamType_MemoryMap); 133fb299fa2Sopenharmony_ci if (outStream == nullptr) { 134fb299fa2Sopenharmony_ci PKG_LOGE("Create stream fail %s", boardIdName.c_str()); 135fb299fa2Sopenharmony_ci return boardlist; 136fb299fa2Sopenharmony_ci } 137fb299fa2Sopenharmony_ci ret = manager->ExtractFile(boardIdName, outStream); 138fb299fa2Sopenharmony_ci PkgBuffer data = {}; 139fb299fa2Sopenharmony_ci ret = outStream->GetBuffer(data); 140fb299fa2Sopenharmony_ci if (ret != PKG_SUCCESS) { 141fb299fa2Sopenharmony_ci PKG_LOGE("ExtractFile fail %s", boardIdName.c_str()); 142fb299fa2Sopenharmony_ci manager->ClosePkgStream(outStream); 143fb299fa2Sopenharmony_ci return boardlist; 144fb299fa2Sopenharmony_ci } 145fb299fa2Sopenharmony_ci std::string str(reinterpret_cast<char*>(data.buffer), data.length); 146fb299fa2Sopenharmony_ci boardlist = SplitString(str, "\n"); 147fb299fa2Sopenharmony_ci manager->ClosePkgStream(outStream); 148fb299fa2Sopenharmony_ci return boardlist; 149fb299fa2Sopenharmony_ci} 150fb299fa2Sopenharmony_ci 151fb299fa2Sopenharmony_cibool PackagesInfo::IsAllowRollback() 152fb299fa2Sopenharmony_ci{ 153fb299fa2Sopenharmony_ci return false; 154fb299fa2Sopenharmony_ci} 155fb299fa2Sopenharmony_ci} // namespace Updater 156