• 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 "pkg_pkgfile.h"
16 #include <ctime>
17 #include <limits>
18 #include <memory>
19 #include "pkg_gzipfile.h"
20 #include "pkg_lz4file.h"
21 #include "pkg_stream.h"
22 #include "pkg_upgradefile.h"
23 #include "pkg_utils.h"
24 #include "pkg_zipfile.h"
25 
26 namespace Hpackage {
~PkgFile()27 PkgFile::~PkgFile()
28 {
29     auto iter = pkgEntryMapId_.begin();
30     while (iter != pkgEntryMapId_.end()) {
31         auto entry = iter->second;
32         delete entry;
33         iter = pkgEntryMapId_.erase(iter);
34     }
35     pkgEntryMapId_.clear();
36     pkgEntryMapFileName_.clear();
37     PkgManager::StreamPtr tmpStream = pkgStream_;
38     pkgManager_->ClosePkgStream(tmpStream);
39 }
40 
AddPkgEntry(const std::string & fileName)41 PkgEntryPtr PkgFile::AddPkgEntry(const std::string &fileName)
42 {
43     uint32_t nodeId = ++nodeId_;
44     PkgEntryPtr entry = nullptr;
45     switch (type_) {
46         case PKG_TYPE_UPGRADE:
47             entry = new UpgradeFileEntry(this, nodeId);
48             break;
49         case PKG_TYPE_ZIP:
50             entry = new ZipFileEntry(this, nodeId);
51             break;
52         case PKG_TYPE_LZ4: {
53             entry = new Lz4FileEntry(this, nodeId);
54             break;
55         }
56         case PKG_TYPE_GZIP: {
57             entry = new GZipFileEntry(this, nodeId);
58             break;
59         }
60         default:
61             return nullptr;
62     }
63     pkgEntryMapId_.insert(std::pair<uint32_t, PkgEntryPtr>(nodeId, entry));
64     pkgEntryMapFileName_.insert(std::pair<std::string, PkgEntryPtr>(fileName, entry));
65     return entry;
66 }
67 
ExtractFile(const PkgEntryPtr node,PkgStreamPtr output)68 int32_t PkgFile::ExtractFile(const PkgEntryPtr node, PkgStreamPtr output)
69 {
70     PKG_LOGI("ExtractFile %s", output->GetFileName().c_str());
71     if (!CheckState({PKG_FILE_STATE_WORKING}, PKG_FILE_STATE_WORKING)) {
72         PKG_LOGE("error state curr %d ", state_);
73         return PKG_INVALID_STATE;
74     }
75     auto entry = static_cast<PkgEntryPtr>(node);
76     if (entry == nullptr) {
77         PKG_LOGE("error get entry %s", pkgStream_->GetFileName().c_str());
78         return PKG_INVALID_PARAM;
79     }
80     return entry->Unpack(output);
81 }
82 
FindPkgEntry(const std::string & fileName)83 PkgEntryPtr PkgFile::FindPkgEntry(const std::string &fileName)
84 {
85     if (!CheckState({PKG_FILE_STATE_WORKING}, PKG_FILE_STATE_WORKING)) {
86         PKG_LOGE("error state curr %d ", state_);
87         return nullptr;
88     }
89     std::multimap<std::string, PkgEntryPtr>::iterator iter = pkgEntryMapFileName_.find(fileName);
90     if (iter != pkgEntryMapFileName_.end()) {
91         return (*iter).second;
92     }
93     return nullptr;
94 }
95 
CheckState(std::vector<uint32_t> states,uint32_t state)96 bool PkgFile::CheckState(std::vector<uint32_t> states, uint32_t state)
97 {
98     bool ret = false;
99     for (auto s : states) {
100         if (state_ == s) {
101             state_ = state;
102             ret = true;
103             break;
104         }
105     }
106     return ret;
107 }
108 
ConvertBufferToString(std::string & fileName,const PkgBuffer & buffer)109 int32_t PkgFile::ConvertBufferToString(std::string &fileName, const PkgBuffer &buffer)
110 {
111     for (uint32_t i = 0; i < buffer.length; ++i) {
112         if (buffer.buffer[i] < 32 || buffer.buffer[i] >= 127) { // 32,127 : should be printable character
113             break;
114         }
115         fileName.push_back(buffer.buffer[i]);
116     }
117     return PKG_SUCCESS;
118 }
119 
ConvertStringToBuffer(const std::string & fileName,const PkgBuffer & buffer,size_t & realLen)120 int32_t PkgFile::ConvertStringToBuffer(const std::string &fileName, const PkgBuffer &buffer, size_t &realLen)
121 {
122     if (buffer.length < fileName.size()) {
123         PKG_LOGE("Invalid buffer");
124         return PKG_INVALID_PARAM;
125     }
126     for (uint32_t i = 0; i < fileName.size(); ++i) {
127         buffer.buffer[i] = static_cast<uint8_t>(fileName[i]);
128         (realLen)++;
129     }
130     return PKG_SUCCESS;
131 }
132 
Init(PkgManager::FileInfoPtr localFileInfo,const PkgManager::FileInfoPtr fileInfo,PkgStreamPtr inStream)133 int32_t PkgEntry::Init(PkgManager::FileInfoPtr localFileInfo, const PkgManager::FileInfoPtr fileInfo,
134     PkgStreamPtr inStream)
135 {
136     if (localFileInfo == nullptr || fileInfo == nullptr || inStream == nullptr) {
137         PKG_LOGE("Failed to check input param");
138         return PKG_INVALID_PARAM;
139     }
140 
141     fileName_.assign(inStream->GetFileName());
142     localFileInfo->identity.assign(fileInfo->identity);
143     localFileInfo->flags = fileInfo->flags;
144     localFileInfo->digestMethod = fileInfo->digestMethod;
145     localFileInfo->packMethod = fileInfo->packMethod;
146     localFileInfo->modifiedTime = fileInfo->modifiedTime;
147     localFileInfo->packedSize = fileInfo->packedSize;
148     localFileInfo->unpackedSize = fileInfo->unpackedSize;
149 
150     // 填充file信息,默认值使用原始文件长度
151     if (localFileInfo->unpackedSize == 0) {
152         localFileInfo->unpackedSize = inStream->GetFileLength();
153     }
154     if (localFileInfo->packedSize == 0) {
155         localFileInfo->packedSize = inStream->GetFileLength();
156     }
157     if (localFileInfo->unpackedSize == 0) {
158         PKG_LOGE("Failed to check unpackedSize = 0");
159         return PKG_INVALID_PARAM;
160     }
161     if (localFileInfo->modifiedTime == 0) {
162         time(&localFileInfo->modifiedTime);
163     }
164     return PKG_SUCCESS;
165 }
166 
AddSignData(uint8_t digestMethod,size_t currOffset,size_t & signOffset)167 void PkgFile::AddSignData(uint8_t digestMethod, size_t currOffset, size_t &signOffset)
168 {
169     signOffset = currOffset;
170     if (digestMethod == PKG_DIGEST_TYPE_NONE) {
171         return;
172     }
173     std::vector<uint8_t> buffer(SIGN_SHA256_LEN + SIGN_SHA384_LEN, 0);
174     int32_t ret = pkgStream_->Write(buffer, buffer.size(), currOffset);
175     if (ret != PKG_SUCCESS) {
176         PKG_LOGE("Fail write sign for %s", pkgStream_->GetFileName().c_str());
177         return;
178     }
179     pkgStream_->Flush(currOffset + buffer.size());
180     PKG_LOGI("SavePackage success file length: %zu signOffset %zu", pkgStream_->GetFileLength(), signOffset);
181 }
182 } // namespace Hpackage
183