• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 
16 #ifndef CORE_OS_OHOS_OHOS_FILE_H
17 #define CORE_OS_OHOS_OHOS_FILE_H
18 
19 #include <atomic>
20 #include <fstream>
21 #include <iostream>
22 #include <memory>
23 #include <regex>
24 
25 #include <base/containers/refcnt_ptr.h>
26 #include <base/containers/string_view.h>
27 #include <base/containers/type_traits.h>
28 #include <base/containers/unordered_map.h>
29 #include <base/containers/vector.h>
30 #include <core/io/intf_directory.h>
31 #include <core/io/intf_file.h>
32 #include <core/namespace.h>
33 
34 #include "resource_manager.h"
35 
36 CORE_BEGIN_NAMESPACE()
37 struct OhosDirImpl {
OhosDirImplOhosDirImpl38     OhosDirImpl(const BASE_NS::string_view path, std::vector<std::string> fileList) : path_(path), fileList_(fileList)
39     {}
40 
41     BASE_NS::string path_;
42     std::vector<std::string> fileList_;
43 };
44 
45 struct PlatformHapInfo {
PlatformHapInfoPlatformHapInfo46     PlatformHapInfo(const BASE_NS::string_view hapPath, const BASE_NS::string_view bundleName,
47         const BASE_NS::string_view moduleName, std::shared_ptr<OHOS::Global::Resource::ResourceManager> resManager)
48         : hapPath(hapPath), bundleName(bundleName), moduleName(moduleName), resourceManager(resManager)
49     {}
50     BASE_NS::string hapPath = " ";
51     BASE_NS::string bundleName = " ";
52     BASE_NS::string moduleName = " ";
53     std::shared_ptr<OHOS::Global::Resource::ResourceManager> resourceManager = nullptr;
54 };
55 
56 class OhosResMgr {
57 public:
OhosResMgr(const PlatformHapInfo & hapInfo)58     explicit OhosResMgr(const PlatformHapInfo& hapInfo) : hapInfo_(hapInfo) {}
59     std::shared_ptr<OHOS::Global::Resource::ResourceManager> GetResMgr() const;
60     void UpdateResManager(const PlatformHapInfo& hapInfo);
Ref()61     void Ref()
62     {
63         refcnt_.fetch_add(1, std::memory_order_relaxed);
64     }
Unref()65     void Unref()
66     {
67         if (refcnt_.fetch_sub(1, std::memory_order_release) == 1) {
68             std::atomic_thread_fence(std::memory_order_acquire);
69             delete this;
70         }
71     }
72 
73 private:
74     std::atomic_int32_t refcnt_ { 0 };
75     PlatformHapInfo hapInfo_;
76     std::shared_ptr<OHOS::Global::Resource::ResourceManager> resourceManager_ { nullptr };
77     BASE_NS::unordered_map<BASE_NS::string, std::shared_ptr<OHOS::Global::Resource::ResourceManager>> resourceManagers_;
78 };
79 
80 class OhosFileDirectory final : public IDirectory {
81 public:
82     explicit OhosFileDirectory(BASE_NS::refcnt_ptr<OhosResMgr> resMgr);
83     ~OhosFileDirectory() override;
84 
85     bool Open(BASE_NS::string_view path);
86     void Close() override;
87     BASE_NS::vector<Entry> GetEntries() const override;
88     // new add
89     IDirectory::Entry GetEntry(BASE_NS::string_view uriIn) const;
90     bool IsFile(BASE_NS::string_view path) const;
91     bool IsDir(BASE_NS::string_view path, std::vector<std::string>& fileList) const;
92 
93 protected:
Destroy()94     void Destroy() override
95     {
96         delete this;
97     }
98 
99 private:
100     BASE_NS::unique_ptr<OhosDirImpl> dir_;
101     BASE_NS::refcnt_ptr<OhosResMgr> dirResMgr_;
102 };
103 
104 class OhosFileStorage {
105 public:
OhosFileStorage(std::unique_ptr<uint8_t[]> buffer)106     explicit OhosFileStorage(std::unique_ptr<uint8_t[]> buffer) : buffer_(std::move(buffer)) {}
107     ~OhosFileStorage() = default;
108 
GetStorage()109     const std::unique_ptr<uint8_t[]>& GetStorage() const
110     {
111         return buffer_;
112     }
113 
Size()114     size_t Size() const
115     {
116         return size_;
117     }
118 
SetBuffer(std::unique_ptr<uint8_t[]> buffer,size_t size)119     void SetBuffer(std::unique_ptr<uint8_t[]> buffer, size_t size)
120     {
121         buffer_ = BASE_NS::move(buffer);
122         size_ = size;
123     }
124 
125 private:
126     std::unique_ptr<uint8_t[]> buffer_;
127     size_t size_ { 0 };
128 };
129 
130 class OhosFile final : public IFile {
131 public:
132     explicit OhosFile(BASE_NS::refcnt_ptr<OhosResMgr> resMgr);
133     ~OhosFile() override = default;
134 
135     Mode GetMode() const override;
136     // Open an existing file, fails if the file does not exist.
137     std::shared_ptr<OhosFileStorage> Open(BASE_NS::string_view path);
138     // Close file.
139     void Close() override;
140     uint64_t Read(void* buffer, uint64_t count) override;
141     uint64_t Write(const void* buffer, uint64_t count) override;
142     uint64_t Append(const void* buffer, uint64_t count, uint64_t flushSize) override;
143     uint64_t GetLength() const override;
144     bool Seek(uint64_t offset) override;
145     uint64_t GetPosition() const override;
146     bool OpenRawFile(BASE_NS::string_view src, size_t& len, std::unique_ptr<uint8_t[]>& dest);
147     void UpdateStorage(std::shared_ptr<OhosFileStorage> buffer);
148 
149 protected:
Destroy()150     void Destroy() override
151     {
152         delete this;
153     }
154 
155 private:
156     bool GetResourceId(const std::string& uri, uint32_t& resId) const;
157     bool GetResourceId(const std::string& uri, std::string& path) const;
158     bool GetResourceName(const std::string& uri, std::string& resName) const;
159 
160     uint64_t index_ { 0 };
161     std::shared_ptr<OhosFileStorage> buffer_;
162     BASE_NS::refcnt_ptr<OhosResMgr> fileResMgr_;
163 };
164 CORE_END_NAMESPACE()
165 #endif // CORE_OS_OHOS_OHOS_FILE_H
166