1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  *
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12 
13  * * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
15  * either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations
18  * under the License.
19  */
20 
21 #ifndef CORE__IO__OHOS_FILE_H
22 #define CORE__IO__OHOS_FILE_H
23 
24 #include <atomic>
25 #include <fstream>
26 #include <iostream>
27 #include <memory>
28 #include <regex>
29 
30 #include <base/containers/refcnt_ptr.h>
31 #include <base/containers/string_view.h>
32 #include <base/containers/type_traits.h>
33 #include <base/containers/unordered_map.h>
34 #include <base/containers/vector.h>
35 #include <core/io/intf_directory.h>
36 #include <core/io/intf_file.h>
37 #include <core/namespace.h>
38 
39 #include "base/global/resource_management/interfaces/inner_api/include/resource_manager.h"
40 
41 CORE_BEGIN_NAMESPACE()
42 struct OhosDirImpl {
OhosDirImplOhosDirImpl43     OhosDirImpl(const BASE_NS::string_view path, std::vector<std::string> fileList) : path_(path), fileList_(fileList)
44     {}
45 
46     BASE_NS::string path_;
47     std::vector<std::string> fileList_;
48 };
49 
50 struct PlatformHapInfo {
PlatformHapInfoPlatformHapInfo51     PlatformHapInfo(const BASE_NS::string_view hapPath, const BASE_NS::string_view bundleName,
52         const BASE_NS::string_view moduleName) : hapPath(hapPath), bundleName(bundleName), moduleName(moduleName) {}
53     BASE_NS::string hapPath = " ";
54     BASE_NS::string bundleName = " ";
55     BASE_NS::string moduleName = " ";
56 };
57 
58 class OhosResMgr {
59 public:
OhosResMgr(const PlatformHapInfo & hapInfo)60     explicit OhosResMgr(const PlatformHapInfo& hapInfo) : hapInfo_(hapInfo) {}
61     std::shared_ptr<OHOS::Global::Resource::ResourceManager> GetResMgr() const;
62     void UpdateResManager(const PlatformHapInfo& hapInfo);
Ref()63     void Ref()
64     {
65         refCount_.fetch_add(1, std::memory_order_relaxed);
66     }
Unref()67     void Unref()
68     {
69         if (refCount_.fetch_sub(1, std::memory_order_release) == 1) {
70             std::atomic_thread_fence(std::memory_order_acquire);
71             delete this;
72         }
73     }
74 
75 private:
76     std::atomic_int32_t refCount_ { 0 };
77     PlatformHapInfo hapInfo_;
78     std::shared_ptr<OHOS::Global::Resource::ResourceManager> resourceManager_  { nullptr };
79     BASE_NS::unordered_map<BASE_NS::string,
80         std::shared_ptr<OHOS::Global::Resource::ResourceManager>> resourceManagers_;
81 };
82 
83 class OhosFileDirectory final : public IDirectory {
84 public:
85     explicit OhosFileDirectory(BASE_NS::refcnt_ptr<OhosResMgr> resMgr);
86     ~OhosFileDirectory() override;
87 
88     bool Open(BASE_NS::string_view path);
89     void Close() override;
90     BASE_NS::vector<Entry> GetEntries() const override;
91     // new add
92     IDirectory::Entry GetEntry(BASE_NS::string_view uriIn) const;
93     bool IsFile(BASE_NS::string_view path) const;
94     bool IsDir(BASE_NS::string_view path, std::vector<std::string>& fileList) const;
95 
96 protected:
Destroy()97     void Destroy() override
98     {
99         delete this;
100     }
101 
102 private:
103     BASE_NS::unique_ptr<OhosDirImpl> dir_;
104     BASE_NS::refcnt_ptr<OhosResMgr> dirResMgr_;
105 };
106 
107 class OhosFileStorage {
108 public:
OhosFileStorage(std::unique_ptr<uint8_t[]> buffer)109     explicit OhosFileStorage(std::unique_ptr<uint8_t[]> buffer) : buffer_(std::move(buffer)) {}
110     ~OhosFileStorage() = default;
111 
GetStorage()112     const std::unique_ptr<uint8_t[]>& GetStorage() const
113     {
114         return buffer_;
115     }
116 
Size()117     size_t Size() const
118     {
119         return size_;
120     }
121 
SetBuffer(std::unique_ptr<uint8_t[]> buffer,size_t size)122     void SetBuffer(std::unique_ptr<uint8_t[]> buffer, size_t size)
123     {
124         buffer_ = BASE_NS::move(buffer);
125         size_ = size;
126     }
127 
128 private:
129     std::unique_ptr<uint8_t[]> buffer_;
130     size_t size_ { 0 };
131 };
132 
133 class OhosFile final : public IFile {
134 public:
135     explicit OhosFile(BASE_NS::refcnt_ptr<OhosResMgr> resMgr);
136     ~OhosFile() override = default;
137 
138     Mode GetMode() const override;
139     // Open an existing file, fails if the file does not exist.
140     std::shared_ptr<OhosFileStorage> Open(BASE_NS::string_view path);
141     // Close file.
142     void Close() override;
143     uint64_t Read(void* buffer, uint64_t count) override;
144     uint64_t Write(const void* buffer, uint64_t count) override;
145     uint64_t GetLength() const override;
146     bool Seek(uint64_t offset) override;
147     uint64_t GetPosition() const override;
148     bool OpenRawFile(BASE_NS::string_view src, size_t& len, std::unique_ptr<uint8_t[]>& dest);
149     void UpdateStorage(std::shared_ptr<OhosFileStorage> buffer);
150 
151 protected:
Destroy()152     void Destroy() override
153     {
154         delete this;
155     }
156 
157 private:
158     bool GetResourceId(const std::string& uri, uint32_t& resId) const;
159     bool GetResourceId(const std::string& uri, std::string& path) const;
160     bool GetResourceName(const std::string& uri, std::string& resName) const;
161 
162     uint64_t index_ { 0 };
163     std::shared_ptr<OhosFileStorage> buffer_;
164     BASE_NS::refcnt_ptr<OhosResMgr> fileResMgr_;
165 };
166 CORE_END_NAMESPACE()
167 #endif
168