• 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 #ifndef PKG_STREAM_H
16 #define PKG_STREAM_H
17 #ifndef __WIN32
18 #include <sys/mman.h>
19 #endif
20 #include <atomic>
21 #include "pkg_manager.h"
22 #include "pkg_utils.h"
23 
24 namespace Hpackage {
25 class PkgStreamImpl;
26 using PkgStreamPtr = PkgStreamImpl *;
27 class PkgStreamImpl : public PkgStream {
28 public:
PkgStreamImpl(PkgManager::PkgManagerPtr pkgManager,const std::string fileName)29     explicit PkgStreamImpl(PkgManager::PkgManagerPtr pkgManager, const std::string fileName)
30         : fileName_(fileName), refCount_(0), pkgManager_(pkgManager) {}
31 
~PkgStreamImpl()32     virtual ~PkgStreamImpl() {}
33 
Read(const PkgBuffer & data,size_t start,size_t needRead,size_t & readLen)34     int32_t Read(const PkgBuffer &data, size_t start, size_t needRead, size_t &readLen) override
35     {
36         UNUSED(data);
37         UNUSED(start);
38         UNUSED(readLen);
39         UNUSED(needRead);
40         return PKG_SUCCESS;
41     }
42 
GetBuffer(PkgBuffer & buffer)43     int32_t GetBuffer(PkgBuffer &buffer) const override
44     {
45         buffer.length = 0;
46         buffer.buffer = nullptr;
47         return PKG_SUCCESS;
48     }
49 
50     virtual int32_t Write(const PkgBuffer &data, size_t size, size_t start) = 0;
51 
52     virtual int32_t Seek(long int offset, int whence) = 0;
53 
54     virtual int32_t Flush(size_t size) = 0;
55 
56     const std::string GetFileName() const override;
57 
GetStreamType()58     int32_t GetStreamType() const override
59     {
60         return PkgStreamType_Read;
61     };
62 
63     void AddRef();
64 
65     void DelRef();
66 
67     bool IsRef() const;
68 
69     static PkgStreamPtr ConvertPkgStream(PkgManager::StreamPtr stream);
70 
71 protected:
72     void PostDecodeProgress(int type, size_t writeDataLen, const void *context) const;
73     std::string fileName_;
74 
75 private:
76     std::atomic_int refCount_;
77     PkgManager::PkgManagerPtr pkgManager_ = nullptr;
78 };
79 
80 class FileStream : public PkgStreamImpl {
81 public:
FileStream(PkgManager::PkgManagerPtr pkgManager,const std::string fileName,FILE * stream,int32_t streamType)82     FileStream(PkgManager::PkgManagerPtr pkgManager, const std::string fileName, FILE *stream, int32_t streamType)
83         : PkgStreamImpl(pkgManager, fileName), stream_(stream), fileLength_(0), streamType_(streamType) {}
84 
85     ~FileStream() override;
86 
87     int32_t Read(const PkgBuffer &data, size_t start, size_t needRead, size_t &readLen) override;
88 
89     int32_t Write(const PkgBuffer &data, size_t size, size_t start) override;
90 
91     int32_t Seek(long int offset, int whence) override;
92 
93     int32_t Flush(size_t size) override;
94 
95     size_t GetFileLength() override;
96 
GetStreamType()97     int32_t GetStreamType() const override
98     {
99         return streamType_;
100     }
101 
102 private:
103     FILE *stream_;
104     size_t fileLength_;
105     int32_t streamType_;
106 };
107 
108 class MemoryMapStream : public PkgStreamImpl {
109 public:
110     MemoryMapStream(PkgManager::PkgManagerPtr pkgManager, const std::string fileName, const PkgBuffer &buffer,
PkgStreamImpl(pkgManager,fileName)111         int32_t streamType = PkgStreamType_MemoryMap) : PkgStreamImpl(pkgManager, fileName), memMap_(buffer.buffer),
112         memSize_(buffer.length), currOffset_(0), streamType_(streamType) {}
113     ~MemoryMapStream() override;
114 
115     int32_t Read(const PkgBuffer &data, size_t start, size_t needRead, size_t &readLen) override;
116 
117     int32_t Write(const PkgBuffer &data, size_t size, size_t start) override;
118 
119     int32_t Seek(long int offset, int whence) override;
120 
GetStreamType()121     int32_t GetStreamType() const override
122     {
123         return streamType_;
124     }
125 
GetFileLength()126     size_t GetFileLength() override
127     {
128         return memSize_;
129     }
130 
Flush(size_t size)131     int32_t Flush(size_t size) override
132     {
133         if (size != memSize_) {
134             PKG_LOGE("Flush size %zu local size:%zu", size, memSize_);
135         }
136         if (streamType_ == PkgStreamType_MemoryMap) {
137             msync(static_cast<void *>(memMap_), memSize_, MS_ASYNC);
138         }
139         currOffset_ = size;
140         return PKG_SUCCESS;
141     }
142 
GetBuffer(PkgBuffer & buffer)143     int32_t GetBuffer(PkgBuffer &buffer) const override
144     {
145         buffer.buffer = memMap_;
146         buffer.length = memSize_;
147         return PKG_SUCCESS;
148     }
149 
150 private:
151     uint8_t *memMap_;
152     size_t memSize_;
153     size_t currOffset_;
154     int32_t streamType_;
155 };
156 
157 class ProcessorStream : public PkgStreamImpl {
158 public:
ProcessorStream(PkgManager::PkgManagerPtr pkgManager,const std::string fileName,ExtractFileProcessor processor,const void * context)159     ProcessorStream(PkgManager::PkgManagerPtr pkgManager, const std::string fileName,
160         ExtractFileProcessor processor, const void *context)
161         : PkgStreamImpl(pkgManager, fileName), processor_(processor), context_(context) {}
162 
~ProcessorStream()163     ~ProcessorStream() override {}
164 
Read(const PkgBuffer & data,size_t start,size_t needRead,size_t & readLen)165     int32_t Read(const PkgBuffer &data, size_t start, size_t needRead, size_t &readLen) override
166     {
167         UNUSED(data);
168         UNUSED(start);
169         UNUSED(readLen);
170         UNUSED(needRead);
171         return PKG_INVALID_STREAM;
172     }
173 
Write(const PkgBuffer & data,size_t size,size_t start)174     int32_t Write(const PkgBuffer &data, size_t size, size_t start) override
175     {
176         if (processor_ == nullptr) {
177             PKG_LOGE("processor not exist");
178             return PKG_INVALID_STREAM;
179         }
180         int ret = processor_(data, size, start, false, context_);
181         PostDecodeProgress(POST_TYPE_DECODE_PKG, size, nullptr);
182         return ret;
183     }
184 
Seek(long int size,int whence)185     int32_t Seek(long int size, int whence) override
186     {
187         UNUSED(size);
188         UNUSED(whence);
189         return PKG_SUCCESS;
190     }
191 
GetStreamType()192     int32_t GetStreamType() const override
193     {
194         return PkgStreamType_Process;
195     }
196 
GetFileLength()197     size_t GetFileLength() override
198     {
199         return 0;
200     }
201 
Flush(size_t size)202     int32_t Flush(size_t size) override
203     {
204         UNUSED(size);
205         if (processor_ == nullptr) {
206             PKG_LOGE("processor not exist");
207             return PKG_INVALID_STREAM;
208         }
209         PkgBuffer data = {};
210         return processor_(data, 0, 0, true, context_);
211     }
212 
213 private:
214     ExtractFileProcessor processor_ = nullptr;
215     const void *context_;
216 };
217 } // namespace Hpackage
218 #endif // PKG_STREAM_H
219