• 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     PKG_CHECK(CheckState({PKG_FILE_STATE_WORKING}, PKG_FILE_STATE_WORKING),
72         return PKG_INVALID_STATE, "error state curr %d ", state_);
73     auto entry = static_cast<PkgEntryPtr>(node);
74     PKG_CHECK(entry != nullptr, return PKG_INVALID_PARAM, "error get entry %s", pkgStream_->GetFileName().c_str());
75     return entry->Unpack(output);
76 }
77 
FindPkgEntry(const std::string & fileName)78 PkgEntryPtr PkgFile::FindPkgEntry(const std::string &fileName)
79 {
80     PKG_CHECK(CheckState({PKG_FILE_STATE_WORKING}, PKG_FILE_STATE_WORKING), return nullptr,
81         "error state curr %d ", state_);
82     std::multimap<std::string, PkgEntryPtr>::iterator iter = pkgEntryMapFileName_.find(fileName);
83     if (iter != pkgEntryMapFileName_.end()) {
84         return (*iter).second;
85     }
86     return nullptr;
87 }
88 
CheckState(std::vector<uint32_t> states,uint32_t state)89 bool PkgFile::CheckState(std::vector<uint32_t> states, uint32_t state)
90 {
91     bool ret = false;
92     for (auto s : states) {
93         if (state_ == s) {
94             state_ = state;
95             ret = true;
96             break;
97         }
98     }
99     return ret;
100 }
101 
ConvertBufferToString(std::string & fileName,const PkgBuffer & buffer)102 int32_t PkgFile::ConvertBufferToString(std::string &fileName, const PkgBuffer &buffer)
103 {
104     for (uint32_t i = 0; i < buffer.length; ++i) {
105         if (buffer.buffer[i] < 32 || buffer.buffer[i] >= 127) { // should be printable character
106             break;
107         }
108         fileName.push_back(buffer.buffer[i]);
109     }
110     return PKG_SUCCESS;
111 }
112 
ConvertStringToBuffer(const std::string & fileName,const PkgBuffer & buffer,size_t & realLen)113 int32_t PkgFile::ConvertStringToBuffer(const std::string &fileName, const PkgBuffer &buffer, size_t &realLen)
114 {
115     PKG_CHECK(buffer.length >= fileName.size(), return PKG_INVALID_PARAM, "Invalid buffer");
116     for (uint32_t i = 0; i < fileName.size(); ++i) {
117         buffer.buffer[i] = static_cast<uint8_t>(fileName[i]);
118         (realLen)++;
119     }
120     return PKG_SUCCESS;
121 }
122 
Init(PkgManager::FileInfoPtr localFileInfo,const PkgManager::FileInfoPtr fileInfo,PkgStreamPtr inStream)123 int32_t PkgEntry::Init(PkgManager::FileInfoPtr localFileInfo, const PkgManager::FileInfoPtr fileInfo,
124     PkgStreamPtr inStream)
125 {
126     PKG_CHECK(localFileInfo != nullptr && fileInfo != nullptr && inStream != nullptr,
127         return PKG_INVALID_PARAM, "Failed to check input param");
128 
129     fileName_.assign(inStream->GetFileName());
130     localFileInfo->identity.assign(fileInfo->identity);
131     localFileInfo->flags = fileInfo->flags;
132     localFileInfo->digestMethod = fileInfo->digestMethod;
133     localFileInfo->packMethod = fileInfo->packMethod;
134     localFileInfo->modifiedTime = fileInfo->modifiedTime;
135     localFileInfo->packedSize = fileInfo->packedSize;
136     localFileInfo->unpackedSize = fileInfo->unpackedSize;
137 
138     // 填充file信息,默认值使用原始文件长度
139     if (localFileInfo->unpackedSize == 0) {
140         localFileInfo->unpackedSize = inStream->GetFileLength();
141     }
142     if (localFileInfo->packedSize == 0) {
143         localFileInfo->packedSize = inStream->GetFileLength();
144     }
145     PKG_CHECK(localFileInfo->unpackedSize != 0, return PKG_INVALID_PARAM, "Failed to check unpackedSize = 0");
146     if (localFileInfo->modifiedTime == 0) {
147         time(&localFileInfo->modifiedTime);
148     }
149     return PKG_SUCCESS;
150 }
151 
AddSignData(uint8_t digestMethod,size_t currOffset,size_t & signOffset)152 void PkgFile::AddSignData(uint8_t digestMethod, size_t currOffset, size_t &signOffset)
153 {
154     signOffset = currOffset;
155     if (digestMethod == PKG_DIGEST_TYPE_NONE) {
156         return;
157     }
158     std::vector<uint8_t> buffer(SIGN_SHA256_LEN + SIGN_SHA384_LEN, 0);
159     int32_t ret = pkgStream_->Write(buffer, buffer.size(), currOffset);
160     PKG_CHECK(ret == PKG_SUCCESS, return, "Fail write sign for %s", pkgStream_->GetFileName().c_str());
161     pkgStream_->Flush(currOffset + buffer.size());
162     PKG_LOGI("SavePackage success file length: %zu signOffset %zu", pkgStream_->GetFileLength(), signOffset);
163 }
164 } // namespace hpackage
165