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 #include "ohos_filesystem.h"
17
18 #include <algorithm>
19 #include <cstring>
20
21 #include <base/containers/iterator.h>
22 #include <base/containers/string.h>
23 #include <base/containers/string_view.h>
24 #include <base/containers/type_traits.h>
25 #include <base/containers/unique_ptr.h>
26 #include <base/containers/vector.h>
27 #include <base/namespace.h>
28 #include <core/io/intf_directory.h>
29 #include <core/io/intf_file.h>
30 #include <core/log.h>
31 #include <core/namespace.h>
32
33 #include "io/file_manager.h"
34 #include "io/path_tools.h"
35 #include "io/proxy_directory.h"
36
37 CORE_BEGIN_NAMESPACE()
38 using BASE_NS::move;
39 using BASE_NS::string;
40 using BASE_NS::string_view;
41 using BASE_NS::vector;
ValidatePath(const string_view pathIn) const42 string OhosFilesystem::ValidatePath(const string_view pathIn) const
43 {
44 auto path = NormalizePath(pathIn);
45 if (!path.empty() && (path.back() == '/')) {
46 // remove suffix -> '/'
47 size_t len = path.length();
48 path.resize(len - 1U);
49 }
50 // remove the '/' slash, which is not used in ResourceMgr
51 if (!path.empty() && path.front() == '/') {
52 path.erase(path.cbegin());
53 }
54 return path;
55 }
56
OpenFile(const BASE_NS::string_view path,const IFile::Mode mode)57 IFile::Ptr OhosFilesystem::OpenFile(const BASE_NS::string_view path, const IFile::Mode mode)
58 {
59 if (mode == IFile::Mode::READ_ONLY) {
60 if (auto const pos = ohosFiles_.find(path); pos != ohosFiles_.end()) {
61 auto storage = pos->second.lock();
62 if (storage) {
63 auto file = BASE_NS::make_unique<OhosFile>(resManager_);
64 file->UpdateStorage(storage);
65 return IFile::Ptr { file.release() };
66 }
67 }
68 if (!path.empty()) {
69 auto file = BASE_NS::make_unique<OhosFile>(resManager_);
70 auto storage = file->Open(path.data());
71 ohosFiles_[path] = std::move(storage);
72 return IFile::Ptr { file.release() };
73 }
74 }
75 return IFile::Ptr();
76 }
77
OhosFilesystem(const BASE_NS::string_view hapPath,const BASE_NS::string_view bundleName,const BASE_NS::string_view moduleName,std::shared_ptr<OHOS::Global::Resource::ResourceManager> resourceManager)78 OhosFilesystem::OhosFilesystem(
79 const BASE_NS::string_view hapPath, const BASE_NS::string_view bundleName, const BASE_NS::string_view moduleName,
80 std::shared_ptr<OHOS::Global::Resource::ResourceManager> resourceManager)
81 : hapInfo_({hapPath, bundleName, moduleName, resourceManager})
82 {
83 resManager_ = BASE_NS::refcnt_ptr<OhosResMgr>(new OhosResMgr(hapInfo_));
84 resManager_->UpdateResManager(hapInfo_);
85 }
86
GetEntry(BASE_NS::string_view path) const87 IDirectory::Entry OhosFilesystem::GetEntry(BASE_NS::string_view path) const
88 {
89 if (!path.empty()) {
90 auto directory = BASE_NS::make_unique<OhosFileDirectory>(resManager_);
91 return directory->GetEntry(path.data());
92 }
93 return {};
94 }
95
GetEntry(BASE_NS::string_view path)96 IDirectory::Entry OhosFilesystem::GetEntry(BASE_NS::string_view path)
97 {
98 const auto& ofs = *this;
99 return ofs.GetEntry(path);
100 }
101
CreateFile(BASE_NS::string_view path)102 IFile::Ptr OhosFilesystem::CreateFile(BASE_NS::string_view path)
103 {
104 return IFile::Ptr();
105 }
106
DeleteFile(BASE_NS::string_view path)107 bool OhosFilesystem::DeleteFile(BASE_NS::string_view path)
108 {
109 // read only filesystem. can not delete files.
110 return false;
111 }
112
FileExists(const string_view path) const113 bool OhosFilesystem::FileExists(const string_view path) const
114 {
115 return GetEntry(path).type == IDirectory::Entry::Type::FILE;
116 }
117
OpenDirectory(BASE_NS::string_view pathIn)118 IDirectory::Ptr OhosFilesystem::OpenDirectory(BASE_NS::string_view pathIn)
119 {
120 auto path = ValidatePath(pathIn);
121 auto directory = BASE_NS::make_unique<OhosFileDirectory>(resManager_);
122 if (directory->Open(path.data())) {
123 return IDirectory::Ptr { directory.release() };
124 }
125 return IDirectory::Ptr();
126 }
127
CreateDirectory(BASE_NS::string_view path)128 IDirectory::Ptr OhosFilesystem::CreateDirectory(BASE_NS::string_view path)
129 {
130 return IDirectory::Ptr();
131 }
132
DeleteDirectory(BASE_NS::string_view path)133 bool OhosFilesystem::DeleteDirectory(BASE_NS::string_view path)
134 {
135 // read only filesystem. can not delete files.
136 return false;
137 }
138
DirectoryExists(const string_view path) const139 bool OhosFilesystem::DirectoryExists(const string_view path) const
140 {
141 return GetEntry(path).type == IDirectory::Entry::Type::DIRECTORY;
142 }
143
Rename(BASE_NS::string_view fromPath,BASE_NS::string_view toPath)144 bool OhosFilesystem::Rename(BASE_NS::string_view fromPath, BASE_NS::string_view toPath)
145 {
146 // read only filesystem. can not rename files.
147 return false;
148 }
149
GetUriPaths(BASE_NS::string_view uri) const150 BASE_NS::vector<BASE_NS::string> OhosFilesystem::GetUriPaths(BASE_NS::string_view uri) const
151 {
152 return {};
153 }
154 CORE_END_NAMESPACE()
155