• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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(const BASE_NS::string_view hapPath, const BASE_NS::string_view bundleName,
79     const BASE_NS::string_view moduleName, std::shared_ptr<OHOS::Global::Resource::ResourceManager> resourceManager)
80     : hapInfo_({ hapPath, bundleName, moduleName, resourceManager })
81 {
82     resManager_ = BASE_NS::refcnt_ptr<OhosResMgr>(new OhosResMgr(hapInfo_));
83     resManager_->UpdateResManager(hapInfo_);
84 }
85 
GetEntry(BASE_NS::string_view path) const86 IDirectory::Entry OhosFilesystem::GetEntry(BASE_NS::string_view path) const
87 {
88     if (!path.empty()) {
89         auto directory = BASE_NS::make_unique<OhosFileDirectory>(resManager_);
90         return directory->GetEntry(path.data());
91     }
92     return {};
93 }
94 
GetEntry(BASE_NS::string_view path)95 IDirectory::Entry OhosFilesystem::GetEntry(BASE_NS::string_view path)
96 {
97     const auto& ofs = *this;
98     return ofs.GetEntry(path);
99 }
100 
CreateFile(BASE_NS::string_view path)101 IFile::Ptr OhosFilesystem::CreateFile(BASE_NS::string_view path)
102 {
103     return IFile::Ptr();
104 }
105 
DeleteFile(BASE_NS::string_view path)106 bool OhosFilesystem::DeleteFile(BASE_NS::string_view path)
107 {
108     // read only filesystem. can not delete files.
109     return false;
110 }
111 
FileExists(const string_view path) const112 bool OhosFilesystem::FileExists(const string_view path) const
113 {
114     return GetEntry(path).type == IDirectory::Entry::Type::FILE;
115 }
116 
OpenDirectory(BASE_NS::string_view pathIn)117 IDirectory::Ptr OhosFilesystem::OpenDirectory(BASE_NS::string_view pathIn)
118 {
119     auto path = ValidatePath(pathIn);
120     auto directory = BASE_NS::make_unique<OhosFileDirectory>(resManager_);
121     if (directory->Open(path.data())) {
122         return IDirectory::Ptr { directory.release() };
123     }
124     return IDirectory::Ptr();
125 }
126 
CreateDirectory(BASE_NS::string_view path)127 IDirectory::Ptr OhosFilesystem::CreateDirectory(BASE_NS::string_view path)
128 {
129     return IDirectory::Ptr();
130 }
131 
DeleteDirectory(BASE_NS::string_view path)132 bool OhosFilesystem::DeleteDirectory(BASE_NS::string_view path)
133 {
134     // read only filesystem. can not delete files.
135     return false;
136 }
137 
DirectoryExists(const string_view path) const138 bool OhosFilesystem::DirectoryExists(const string_view path) const
139 {
140     return GetEntry(path).type == IDirectory::Entry::Type::DIRECTORY;
141 }
142 
Rename(BASE_NS::string_view fromPath,BASE_NS::string_view toPath)143 bool OhosFilesystem::Rename(BASE_NS::string_view fromPath, BASE_NS::string_view toPath)
144 {
145     // read only filesystem. can not rename files.
146     return false;
147 }
148 
GetUriPaths(BASE_NS::string_view uri) const149 BASE_NS::vector<BASE_NS::string> OhosFilesystem::GetUriPaths(BASE_NS::string_view uri) const
150 {
151     return {};
152 }
153 CORE_END_NAMESPACE()
154