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