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
16fb299fa2Sopenharmony_ci#ifndef PKG_MANAGER_H
17fb299fa2Sopenharmony_ci#define PKG_MANAGER_H
18fb299fa2Sopenharmony_ci
19fb299fa2Sopenharmony_ci#include <cstdlib>
20fb299fa2Sopenharmony_ci#include <cstdio>
21fb299fa2Sopenharmony_ci#include <functional>
22fb299fa2Sopenharmony_ci#include <iostream>
23fb299fa2Sopenharmony_ci#include <memory>
24fb299fa2Sopenharmony_ci#include <vector>
25fb299fa2Sopenharmony_ci#include "ring_buffer/ring_buffer.h"
26fb299fa2Sopenharmony_ci#include "package/package.h"
27fb299fa2Sopenharmony_ci#include "pkg_info_utils.h"
28fb299fa2Sopenharmony_ci
29fb299fa2Sopenharmony_cinamespace Hpackage {
30fb299fa2Sopenharmony_ciclass PkgFile;
31fb299fa2Sopenharmony_ciclass PkgStream;
32fb299fa2Sopenharmony_ciclass PkgEntry;
33fb299fa2Sopenharmony_ciusing PkgFilePtr = PkgFile *;
34fb299fa2Sopenharmony_ciusing PkgStreamPtr = PkgStream *;
35fb299fa2Sopenharmony_ciusing PkgEntryPtr = PkgEntry *;
36fb299fa2Sopenharmony_ci
37fb299fa2Sopenharmony_ci/**
38fb299fa2Sopenharmony_ci * Input and output stream definition
39fb299fa2Sopenharmony_ci */
40fb299fa2Sopenharmony_ciclass PkgStream {
41fb299fa2Sopenharmony_cipublic:
42fb299fa2Sopenharmony_ci    enum {
43fb299fa2Sopenharmony_ci        PkgStreamType_Read = 0,     // common file reading
44fb299fa2Sopenharmony_ci        PkgStreamType_Write,        // common file writing (A new file is created and the original content is deleted.)
45fb299fa2Sopenharmony_ci        PkgStreamType_MemoryMap,    // memory mapping
46fb299fa2Sopenharmony_ci        PkgStreamType_Process,      // processing while parsing
47fb299fa2Sopenharmony_ci        PkgStreamType_Buffer,       // buffer
48fb299fa2Sopenharmony_ci        PKgStreamType_FileMap,      // file map to memory
49fb299fa2Sopenharmony_ci        PkgStreamType_FlowData,     // flow data
50fb299fa2Sopenharmony_ci    };
51fb299fa2Sopenharmony_ci
52fb299fa2Sopenharmony_ci    virtual ~PkgStream() = default;
53fb299fa2Sopenharmony_ci
54fb299fa2Sopenharmony_ci    /**
55fb299fa2Sopenharmony_ci     * Read files.
56fb299fa2Sopenharmony_ci     *
57fb299fa2Sopenharmony_ci     * @param buff                  buffer to hold the output file content
58fb299fa2Sopenharmony_ci     * @param start                 start position of reading
59fb299fa2Sopenharmony_ci     * @param needRead              size of the data to read
60fb299fa2Sopenharmony_ci     * @param readLen               length of the read data
61fb299fa2Sopenharmony_ci     * @return                      file reading result
62fb299fa2Sopenharmony_ci     */
63fb299fa2Sopenharmony_ci    virtual int32_t Read(PkgBuffer &data, size_t start, size_t needRead, size_t &readLen) = 0;
64fb299fa2Sopenharmony_ci
65fb299fa2Sopenharmony_ci    /**
66fb299fa2Sopenharmony_ci     * write stream
67fb299fa2Sopenharmony_ci     *
68fb299fa2Sopenharmony_ci     * @param data                  buffer to hold the output file content
69fb299fa2Sopenharmony_ci     * @param size                  size of the data to read
70fb299fa2Sopenharmony_ci     * @param start                 start position of reading
71fb299fa2Sopenharmony_ci     * @return                      file reading result
72fb299fa2Sopenharmony_ci     */
73fb299fa2Sopenharmony_ci    virtual int32_t Write(const PkgBuffer &data, size_t size, size_t start) = 0;
74fb299fa2Sopenharmony_ci
75fb299fa2Sopenharmony_ci    virtual int32_t Flush(size_t size) = 0;
76fb299fa2Sopenharmony_ci
77fb299fa2Sopenharmony_ci    virtual int32_t GetBuffer(PkgBuffer &buffer) const = 0;
78fb299fa2Sopenharmony_ci
79fb299fa2Sopenharmony_ci    virtual size_t GetFileLength() = 0;
80fb299fa2Sopenharmony_ci    virtual const std::string GetFileName() const = 0;
81fb299fa2Sopenharmony_ci    virtual int32_t GetStreamType() const = 0;
82fb299fa2Sopenharmony_ci
83fb299fa2Sopenharmony_ci    virtual void AddRef() = 0;
84fb299fa2Sopenharmony_ci    virtual void DelRef() = 0;
85fb299fa2Sopenharmony_ci    virtual bool IsRef() const = 0;
86fb299fa2Sopenharmony_ci
87fb299fa2Sopenharmony_ci    using ExtractFileProcessor = std::function<int(const PkgBuffer &data, size_t size, size_t start,
88fb299fa2Sopenharmony_ci        bool isFinish, const void *context)>;
89fb299fa2Sopenharmony_ci
90fb299fa2Sopenharmony_ci    int32_t GetBuffer(uint8_t *&buffer, size_t &size)
91fb299fa2Sopenharmony_ci    {
92fb299fa2Sopenharmony_ci        PkgBuffer data = {};
93fb299fa2Sopenharmony_ci        int ret = GetBuffer(data);
94fb299fa2Sopenharmony_ci        buffer = data.buffer;
95fb299fa2Sopenharmony_ci        size = data.length;
96fb299fa2Sopenharmony_ci        return ret;
97fb299fa2Sopenharmony_ci    }
98fb299fa2Sopenharmony_ci
99fb299fa2Sopenharmony_ci    virtual size_t GetReadOffset() const
100fb299fa2Sopenharmony_ci    {
101fb299fa2Sopenharmony_ci        return 0;
102fb299fa2Sopenharmony_ci    }
103fb299fa2Sopenharmony_ci
104fb299fa2Sopenharmony_ci    virtual void Stop()
105fb299fa2Sopenharmony_ci    {
106fb299fa2Sopenharmony_ci        return;
107fb299fa2Sopenharmony_ci    }
108fb299fa2Sopenharmony_ci};
109fb299fa2Sopenharmony_ci
110fb299fa2Sopenharmony_ciclass PkgFile {
111fb299fa2Sopenharmony_cipublic:
112fb299fa2Sopenharmony_ci    enum PkgType {
113fb299fa2Sopenharmony_ci        PKG_TYPE_NONE = PKG_PACK_TYPE_NONE,
114fb299fa2Sopenharmony_ci        PKG_TYPE_UPGRADE = PKG_PACK_TYPE_UPGRADE, // 升级包
115fb299fa2Sopenharmony_ci        PKG_TYPE_ZIP = PKG_PACK_TYPE_ZIP,     // zip压缩包
116fb299fa2Sopenharmony_ci        PKG_TYPE_LZ4 = PKG_PACK_TYPE_LZ4,     // lz4压缩包
117fb299fa2Sopenharmony_ci        PKG_TYPE_GZIP = PKG_PACK_TYPE_GZIP,     // gzip压缩包
118fb299fa2Sopenharmony_ci        PKG_TYPE_MAX
119fb299fa2Sopenharmony_ci    };
120fb299fa2Sopenharmony_ci
121fb299fa2Sopenharmony_ci    using VerifyFunction = std::function<int(const PkgInfoPtr info,
122fb299fa2Sopenharmony_ci        const std::vector<uint8_t> &digest, const std::vector<uint8_t> &signature)>;
123fb299fa2Sopenharmony_ci
124fb299fa2Sopenharmony_cipublic:
125fb299fa2Sopenharmony_ci    virtual ~PkgFile() = default;
126fb299fa2Sopenharmony_ci
127fb299fa2Sopenharmony_ci    virtual int32_t AddEntry(const FileInfoPtr file, const PkgStreamPtr input) = 0;
128fb299fa2Sopenharmony_ci
129fb299fa2Sopenharmony_ci    virtual int32_t SavePackage(size_t &signOffset) = 0;
130fb299fa2Sopenharmony_ci
131fb299fa2Sopenharmony_ci    virtual int32_t ExtractFile(const PkgEntryPtr node, const PkgStreamPtr output) = 0;
132fb299fa2Sopenharmony_ci
133fb299fa2Sopenharmony_ci    virtual int32_t LoadPackage(std::vector<std::string> &fileNames, VerifyFunction verifier = nullptr) = 0;
134fb299fa2Sopenharmony_ci
135fb299fa2Sopenharmony_ci    virtual int32_t ParseComponents(std::vector<std::string> &fileNames) = 0;
136fb299fa2Sopenharmony_ci
137fb299fa2Sopenharmony_ci    virtual PkgEntryPtr FindPkgEntry(const std::string &fileName) = 0;
138fb299fa2Sopenharmony_ci
139fb299fa2Sopenharmony_ci    virtual PkgStreamPtr GetPkgStream() const = 0;
140fb299fa2Sopenharmony_ci
141fb299fa2Sopenharmony_ci    virtual const PkgInfo *GetPkgInfo() const = 0;
142fb299fa2Sopenharmony_ci
143fb299fa2Sopenharmony_ci    virtual PkgType GetPkgType() const = 0;
144fb299fa2Sopenharmony_ci
145fb299fa2Sopenharmony_ci    virtual void ClearPkgStream() = 0;
146fb299fa2Sopenharmony_ci};
147fb299fa2Sopenharmony_ci
148fb299fa2Sopenharmony_ciclass PkgEntry {
149fb299fa2Sopenharmony_cipublic:
150fb299fa2Sopenharmony_ci    PkgEntry(PkgFilePtr pkgFile, uint32_t nodeId) : nodeId_(nodeId), pkgFile_(pkgFile) {}
151fb299fa2Sopenharmony_ci
152fb299fa2Sopenharmony_ci    virtual ~PkgEntry() {}
153fb299fa2Sopenharmony_ci
154fb299fa2Sopenharmony_ci    virtual int32_t Init(const FileInfoPtr fileInfo, PkgStreamPtr inStream) = 0;
155fb299fa2Sopenharmony_ci
156fb299fa2Sopenharmony_ci    virtual int32_t EncodeHeader(PkgStreamPtr inStream, size_t startOffset, size_t &encodeLen) = 0;
157fb299fa2Sopenharmony_ci
158fb299fa2Sopenharmony_ci    virtual int32_t Pack(PkgStreamPtr inStream, size_t startOffset, size_t &encodeLen) = 0;
159fb299fa2Sopenharmony_ci
160fb299fa2Sopenharmony_ci    virtual int32_t DecodeHeader(PkgBuffer &buffer, size_t headerOffset, size_t dataOffset,
161fb299fa2Sopenharmony_ci        size_t &decodeLen) = 0;
162fb299fa2Sopenharmony_ci
163fb299fa2Sopenharmony_ci    virtual int32_t Unpack(PkgStreamPtr outStream) = 0;
164fb299fa2Sopenharmony_ci
165fb299fa2Sopenharmony_ci    virtual const std::string GetFileName() const
166fb299fa2Sopenharmony_ci    {
167fb299fa2Sopenharmony_ci        return fileName_;
168fb299fa2Sopenharmony_ci    };
169fb299fa2Sopenharmony_ci
170fb299fa2Sopenharmony_ci    virtual const FileInfo *GetFileInfo() const = 0;
171fb299fa2Sopenharmony_ci
172fb299fa2Sopenharmony_ci    PkgFilePtr GetPkgFile() const
173fb299fa2Sopenharmony_ci    {
174fb299fa2Sopenharmony_ci        return pkgFile_;
175fb299fa2Sopenharmony_ci    }
176fb299fa2Sopenharmony_ci
177fb299fa2Sopenharmony_ci    uint32_t GetNodeId() const
178fb299fa2Sopenharmony_ci    {
179fb299fa2Sopenharmony_ci        return nodeId_;
180fb299fa2Sopenharmony_ci    }
181fb299fa2Sopenharmony_ci
182fb299fa2Sopenharmony_ci    void AddDataOffset(size_t offset)
183fb299fa2Sopenharmony_ci    {
184fb299fa2Sopenharmony_ci        dataOffset_ += offset;
185fb299fa2Sopenharmony_ci    }
186fb299fa2Sopenharmony_ci
187fb299fa2Sopenharmony_ciprotected:
188fb299fa2Sopenharmony_ci    int32_t Init(FileInfoPtr localFileInfo, const FileInfoPtr fileInfo,
189fb299fa2Sopenharmony_ci        PkgStreamPtr inStream);
190fb299fa2Sopenharmony_ci
191fb299fa2Sopenharmony_ciprotected:
192fb299fa2Sopenharmony_ci    uint32_t nodeId_ {0};
193fb299fa2Sopenharmony_ci    PkgFilePtr pkgFile_ {nullptr};
194fb299fa2Sopenharmony_ci    size_t headerOffset_ {0};
195fb299fa2Sopenharmony_ci    size_t dataOffset_ {0};
196fb299fa2Sopenharmony_ci    std::string fileName_ {};
197fb299fa2Sopenharmony_ci};
198fb299fa2Sopenharmony_ci
199fb299fa2Sopenharmony_ciusing PkgDecodeProgress = std::function<void(int type, size_t writeDataLen, const void *context)>;
200fb299fa2Sopenharmony_ci
201fb299fa2Sopenharmony_ci/**
202fb299fa2Sopenharmony_ci * Get a singleton PkgManager instance.
203fb299fa2Sopenharmony_ci */
204fb299fa2Sopenharmony_ciclass PkgManager {
205fb299fa2Sopenharmony_cipublic:
206fb299fa2Sopenharmony_ci    using PkgManagerPtr = PkgManager *;
207fb299fa2Sopenharmony_ci    using FileInfoPtr = FileInfo *;
208fb299fa2Sopenharmony_ci    using PkgInfoPtr = PkgInfo *;
209fb299fa2Sopenharmony_ci    using StreamPtr = PkgStream *;
210fb299fa2Sopenharmony_ci    using VerifyCallback = std::function<void(int32_t result, uint32_t percent)>;
211fb299fa2Sopenharmony_ci    using PkgFileConstructor = std::function<PkgFilePtr(
212fb299fa2Sopenharmony_ci        PkgManagerPtr manager, PkgStreamPtr stream, PkgManager::PkgInfoPtr header)>;
213fb299fa2Sopenharmony_ci
214fb299fa2Sopenharmony_ci    virtual ~PkgManager() = default;
215fb299fa2Sopenharmony_ci
216fb299fa2Sopenharmony_ci    virtual void RegisterPkgFileCreator(const std::string &fileType, PkgFileConstructor constructor) = 0;
217fb299fa2Sopenharmony_ci
218fb299fa2Sopenharmony_ci    static PkgManagerPtr CreatePackageInstance();
219fb299fa2Sopenharmony_ci    static PkgManagerPtr GetPackageInstance();
220fb299fa2Sopenharmony_ci    static void ReleasePackageInstance(PkgManagerPtr manager);
221fb299fa2Sopenharmony_ci
222fb299fa2Sopenharmony_ci    /**
223fb299fa2Sopenharmony_ci     * Create an update package based on specified parameters.
224fb299fa2Sopenharmony_ci     *
225fb299fa2Sopenharmony_ci     * @param path              path of the update package
226fb299fa2Sopenharmony_ci     * @param header            header, which mainly consists of algorithm information
227fb299fa2Sopenharmony_ci     * @param files             packed file list
228fb299fa2Sopenharmony_ci     * @return                  packaging result, with the package saved as the file specified in path
229fb299fa2Sopenharmony_ci     */
230fb299fa2Sopenharmony_ci    virtual int32_t CreatePackage(const std::string &path, const std::string &keyName, PkgInfoPtr header,
231fb299fa2Sopenharmony_ci        std::vector<std::pair<std::string, ZipFileInfo>> &files) = 0;
232fb299fa2Sopenharmony_ci    virtual int32_t CreatePackage(const std::string &path, const std::string &keyName, PkgInfoPtr header,
233fb299fa2Sopenharmony_ci        std::vector<std::pair<std::string, ComponentInfo>> &files) = 0;
234fb299fa2Sopenharmony_ci    virtual int32_t CreatePackage(const std::string &path, const std::string &keyName, PkgInfoPtr header,
235fb299fa2Sopenharmony_ci        std::vector<std::pair<std::string, Lz4FileInfo>> &files) = 0;
236fb299fa2Sopenharmony_ci
237fb299fa2Sopenharmony_ci    /**
238fb299fa2Sopenharmony_ci     * Verify the signature of the upgrade package.
239fb299fa2Sopenharmony_ci     *
240fb299fa2Sopenharmony_ci     * @param packagePath       file name of the update package
241fb299fa2Sopenharmony_ci     * @param keyPath           file name of the key used for verification
242fb299fa2Sopenharmony_ci     * @param version           version number of the update package to download
243fb299fa2Sopenharmony_ci     * @param digest            digest value
244fb299fa2Sopenharmony_ci     * @param size              digest value size
245fb299fa2Sopenharmony_ci     * @return                  verification result
246fb299fa2Sopenharmony_ci     */
247fb299fa2Sopenharmony_ci    virtual int32_t VerifyPackage(const std::string &packagePath, const std::string &keyPath,
248fb299fa2Sopenharmony_ci        const std::string &version, const PkgBuffer &digest, VerifyCallback cb) = 0;
249fb299fa2Sopenharmony_ci
250fb299fa2Sopenharmony_ci    /**
251fb299fa2Sopenharmony_ci     * Load and parse the update package.
252fb299fa2Sopenharmony_ci     *
253fb299fa2Sopenharmony_ci     * @param packagePath       file name of the update package
254fb299fa2Sopenharmony_ci     * @param fileIds           returned file ID list
255fb299fa2Sopenharmony_ci     * @param middleTofile      file saving mode during intermediate parsing.
256fb299fa2Sopenharmony_ci     * @return                  loading and parsing result
257fb299fa2Sopenharmony_ci     */
258fb299fa2Sopenharmony_ci    virtual int32_t LoadPackage(const std::string &packagePath, const std::string &keyPath,
259fb299fa2Sopenharmony_ci        std::vector<std::string> &fileIds) = 0;
260fb299fa2Sopenharmony_ci
261fb299fa2Sopenharmony_ci    virtual int32_t VerifyAccPackage(const std::string &packagePath, const std::string &keyPath) = 0;
262fb299fa2Sopenharmony_ci
263fb299fa2Sopenharmony_ci    virtual int32_t VerifyOtaPackage(const std::string &devPath, uint64_t offset, size_t size) = 0;
264fb299fa2Sopenharmony_ci
265fb299fa2Sopenharmony_ci    virtual int32_t VerifyOtaPackage(const std::string &packagePath) = 0;
266fb299fa2Sopenharmony_ci
267fb299fa2Sopenharmony_ci    virtual int32_t VerifyBinFile(const std::string &packagePath, const std::string &keyPath,
268fb299fa2Sopenharmony_ci        const std::string &version, const PkgBuffer &digest) = 0;
269fb299fa2Sopenharmony_ci
270fb299fa2Sopenharmony_ci    /**
271fb299fa2Sopenharmony_ci     * Get the information about the update package.
272fb299fa2Sopenharmony_ci     *
273fb299fa2Sopenharmony_ci     * @param packagePath   file name of the update package
274fb299fa2Sopenharmony_ci     * @return              information about the update package
275fb299fa2Sopenharmony_ci     */
276fb299fa2Sopenharmony_ci    virtual const PkgInfo *GetPackageInfo(const std::string &packagePath) = 0;
277fb299fa2Sopenharmony_ci
278fb299fa2Sopenharmony_ci    /**
279fb299fa2Sopenharmony_ci     * Extract files from the update package, parse the files, and verify the hash value.
280fb299fa2Sopenharmony_ci     *
281fb299fa2Sopenharmony_ci     * @param fileId        File ID, which is obtained from the fileIds returned by the LoadPackage function
282fb299fa2Sopenharmony_ci     * @param output        output of the extracted files
283fb299fa2Sopenharmony_ci     * @return              read operation result
284fb299fa2Sopenharmony_ci     */
285fb299fa2Sopenharmony_ci    virtual int32_t ExtractFile(const std::string &fileId, StreamPtr output) = 0;
286fb299fa2Sopenharmony_ci
287fb299fa2Sopenharmony_ci    /**
288fb299fa2Sopenharmony_ci     * Obtain information about the files in the update package.
289fb299fa2Sopenharmony_ci     *
290fb299fa2Sopenharmony_ci     * @param fileId        file ID
291fb299fa2Sopenharmony_ci     * @return              file information
292fb299fa2Sopenharmony_ci     */
293fb299fa2Sopenharmony_ci    virtual const FileInfo *GetFileInfo(const std::string &fileId) = 0;
294fb299fa2Sopenharmony_ci
295fb299fa2Sopenharmony_ci    /**
296fb299fa2Sopenharmony_ci     * Create a a package stream to output.
297fb299fa2Sopenharmony_ci     *
298fb299fa2Sopenharmony_ci     * @param stream        stream for io management
299fb299fa2Sopenharmony_ci     * @param fileName      file name corresponding to the stream
300fb299fa2Sopenharmony_ci     * @param size          file size
301fb299fa2Sopenharmony_ci     * @param type          stream type
302fb299fa2Sopenharmony_ci     * @return              creation result; false if no access permission
303fb299fa2Sopenharmony_ci     */
304fb299fa2Sopenharmony_ci    virtual int32_t CreatePkgStream(StreamPtr &stream, const std::string &fileName, size_t size,
305fb299fa2Sopenharmony_ci        int32_t type) = 0;
306fb299fa2Sopenharmony_ci
307fb299fa2Sopenharmony_ci    /**
308fb299fa2Sopenharmony_ci     * Create a package stream that can be processed while parsing.
309fb299fa2Sopenharmony_ci     *
310fb299fa2Sopenharmony_ci     * @param stream        stream used for io management
311fb299fa2Sopenharmony_ci     * @param fileName      file name corresponding to the stream
312fb299fa2Sopenharmony_ci     * @param processor     content processor
313fb299fa2Sopenharmony_ci     * @param context       context for the processor
314fb299fa2Sopenharmony_ci     * @return              creation result
315fb299fa2Sopenharmony_ci     */
316fb299fa2Sopenharmony_ci    virtual int32_t CreatePkgStream(StreamPtr &stream, const std::string &fileName,
317fb299fa2Sopenharmony_ci        PkgStream::ExtractFileProcessor processor, const void *context) = 0;
318fb299fa2Sopenharmony_ci
319fb299fa2Sopenharmony_ci    /**
320fb299fa2Sopenharmony_ci     * Create a package stream that can be processed while parsing.
321fb299fa2Sopenharmony_ci     *
322fb299fa2Sopenharmony_ci     * @param stream        stream used for io management
323fb299fa2Sopenharmony_ci     * @param fileName      file name corresponding to the stream
324fb299fa2Sopenharmony_ci     * @param buffer        buffer
325fb299fa2Sopenharmony_ci     * @return              creation result
326fb299fa2Sopenharmony_ci     */
327fb299fa2Sopenharmony_ci    virtual int32_t CreatePkgStream(StreamPtr &stream, const std::string &fileName, const PkgBuffer &buffer) = 0;
328fb299fa2Sopenharmony_ci
329fb299fa2Sopenharmony_ci    /**
330fb299fa2Sopenharmony_ci     * Create a package stream that can be processed while parsing.
331fb299fa2Sopenharmony_ci     *
332fb299fa2Sopenharmony_ci     * @param stream        stream used for io management
333fb299fa2Sopenharmony_ci     * @param fileName      file name corresponding to the stream
334fb299fa2Sopenharmony_ci     * @param buffer        ringbuffer
335fb299fa2Sopenharmony_ci     * @return              creation result
336fb299fa2Sopenharmony_ci     */
337fb299fa2Sopenharmony_ci    virtual int32_t CreatePkgStream(StreamPtr &stream, const std::string &fileName,
338fb299fa2Sopenharmony_ci        uint64_t fileLen, Updater::RingBuffer *buffer) = 0;
339fb299fa2Sopenharmony_ci
340fb299fa2Sopenharmony_ci    /**
341fb299fa2Sopenharmony_ci     * Close the stream
342fb299fa2Sopenharmony_ci     *
343fb299fa2Sopenharmony_ci     * @param stream  stream对象
344fb299fa2Sopenharmony_ci     */
345fb299fa2Sopenharmony_ci    virtual void ClosePkgStream(StreamPtr &stream) = 0;
346fb299fa2Sopenharmony_ci
347fb299fa2Sopenharmony_ci    virtual int32_t DecompressBuffer(FileInfoPtr info, const PkgBuffer &buffer, StreamPtr output) const = 0;
348fb299fa2Sopenharmony_ci    virtual int32_t CompressBuffer(FileInfoPtr info, const PkgBuffer &buffer, StreamPtr output) const = 0;
349fb299fa2Sopenharmony_ci
350fb299fa2Sopenharmony_ci    virtual int32_t LoadPackageWithoutUnPack(const std::string &packagePath,
351fb299fa2Sopenharmony_ci        std::vector<std::string> &fileIds) = 0;
352fb299fa2Sopenharmony_ci
353fb299fa2Sopenharmony_ci    virtual int32_t LoadPackageWithStream(const std::string &packagePath, const std::string &keyPath,
354fb299fa2Sopenharmony_ci        std::vector<std::string> &fileIds, uint8_t type, StreamPtr stream) = 0;
355fb299fa2Sopenharmony_ci
356fb299fa2Sopenharmony_ci    virtual int32_t ParsePackage(StreamPtr stream, std::vector<std::string> &fileIds, int32_t type) = 0;
357fb299fa2Sopenharmony_ci
358fb299fa2Sopenharmony_ci    virtual void SetPkgDecodeProgress(PkgDecodeProgress decodeProgress) = 0;
359fb299fa2Sopenharmony_ci
360fb299fa2Sopenharmony_ci    virtual void PostDecodeProgress(int type, size_t writeDataLen, const void *context) = 0;
361fb299fa2Sopenharmony_ci
362fb299fa2Sopenharmony_ci    virtual StreamPtr GetPkgFileStream(const std::string &fileName) = 0;
363fb299fa2Sopenharmony_ci
364fb299fa2Sopenharmony_ci    virtual int32_t ParseComponents(const std::string &packagePath, std::vector<std::string> &fileName) = 0;
365fb299fa2Sopenharmony_ci
366fb299fa2Sopenharmony_ci    /**
367fb299fa2Sopenharmony_ci     * Load and parse the update package.
368fb299fa2Sopenharmony_ci     *
369fb299fa2Sopenharmony_ci     * @param packagePath       file name of the update package
370fb299fa2Sopenharmony_ci     * @param fileIds           returned file ID list
371fb299fa2Sopenharmony_ci     * @param middleTofile      file saving mode during intermediate parsing.
372fb299fa2Sopenharmony_ci     * @return                  loading and parsing result
373fb299fa2Sopenharmony_ci     */
374fb299fa2Sopenharmony_ci    virtual int32_t LoadPackage(const std::string &packagePath,
375fb299fa2Sopenharmony_ci        std::vector<std::string> &fileIds, PkgFile::PkgType type) = 0;
376fb299fa2Sopenharmony_ci};
377fb299fa2Sopenharmony_ci
378fb299fa2Sopenharmony_citemplate <typename FileClassName>
379fb299fa2Sopenharmony_ciPkgFilePtr NewPkgFile(PkgManager::PkgManagerPtr manager, PkgStreamPtr stream, PkgInfoPtr header)
380fb299fa2Sopenharmony_ci{
381fb299fa2Sopenharmony_ci    return new FileClassName (manager, stream, header);
382fb299fa2Sopenharmony_ci}
383fb299fa2Sopenharmony_ci} // namespace Hpackage
384fb299fa2Sopenharmony_ci#endif // PKG_MANAGER_H
385