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 #include "ohos_filesystem.h"
22 
23 #include <algorithm>
24 #include <cstring>
25 
26 #include <base/containers/iterator.h>
27 #include <base/containers/string.h>
28 #include <base/containers/string_view.h>
29 #include <base/containers/type_traits.h>
30 #include <base/containers/unique_ptr.h>
31 #include <base/containers/vector.h>
32 #include <base/namespace.h>
33 #include <core/io/intf_directory.h>
34 #include <core/io/intf_file.h>
35 #include <core/log.h>
36 #include <core/namespace.h>
37 
38 #include "io/file_manager.h"
39 #include "io/path_tools.h"
40 #include "io/proxy_directory.h"
41 
42 CORE_BEGIN_NAMESPACE()
43 using BASE_NS::move;
44 using BASE_NS::string;
45 using BASE_NS::string_view;
46 using BASE_NS::vector;
ValidatePath(const string_view pathIn) const47 string OhosFilesystem::ValidatePath(const string_view pathIn) const
48 {
49     auto path = NormalizePath(pathIn);
50     if (!path.empty()) {
51         // remove suffix -> '/'
52         string temp;
53         if (path.back() == '/') {
54             size_t len = path.length();
55             temp = path.substr(0, len - 1);
56         }
57         // remove the '/' slash, which is not used in ResourceMgr
58         return string(temp.substr(1));
59     }
60     return path;
61 }
62 
OpenFile(const BASE_NS::string_view path)63 IFile::Ptr OhosFilesystem::OpenFile(const BASE_NS::string_view path)
64 {
65     if (auto const pos = ohosFiles_.find(path); pos != ohosFiles_.end()) {
66         auto storage = pos->second.lock();
67         if (storage) {
68             auto file = BASE_NS::make_unique<OhosFile>(resManager_);
69             file->UpdateStorage(storage);
70             return IFile::Ptr { file.release() };
71         }
72     }
73     auto storage = std::make_shared<OhosFileStorage>(nullptr);
74     auto file = BASE_NS::make_unique<OhosFile>(resManager_);
75     if (!path.empty()) {
76         storage = file->Open(path.data());
77     }
78     if (storage == nullptr) {
79         return IFile::Ptr();
80     }
81     ohosFiles_[path] = std::move(storage);
82     return IFile::Ptr { file.release() };
83 }
84 
OhosFilesystem(const BASE_NS::string_view hapPath,const BASE_NS::string_view bundleName,const BASE_NS::string_view moduleName)85 OhosFilesystem::OhosFilesystem(const BASE_NS::string_view hapPath, const BASE_NS::string_view bundleName,
86     const BASE_NS::string_view moduleName) : hapInfo_({ hapPath, bundleName, moduleName })
87 {
88     resManager_ = BASE_NS::refcnt_ptr<OhosResMgr>(new OhosResMgr(hapInfo_));
89     resManager_->UpdateResManager(hapInfo_);
90 }
91 
GetEntry(BASE_NS::string_view path)92 IDirectory::Entry OhosFilesystem::GetEntry(BASE_NS::string_view path)
93 {
94     if (!path.empty()) {
95         auto directory = BASE_NS::make_unique<OhosFileDirectory>(resManager_);
96         return directory->GetEntry(path.data());
97     }
98     return {};
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     return ohosFiles_.erase(path) != 0U;
109 }
110 
OpenDirectory(BASE_NS::string_view pathIn)111 IDirectory::Ptr OhosFilesystem::OpenDirectory(BASE_NS::string_view pathIn)
112 {
113     auto path = ValidatePath(pathIn);
114     auto directory = BASE_NS::make_unique<OhosFileDirectory>(resManager_);
115     if (directory->Open(path.data())) {
116         return IDirectory::Ptr { directory.release() };
117     }
118     return IDirectory::Ptr();
119 }
120 
CreateDirectory(BASE_NS::string_view path)121 IDirectory::Ptr OhosFilesystem::CreateDirectory(BASE_NS::string_view path)
122 {
123     return IDirectory::Ptr();
124 }
125 
DeleteDirectory(BASE_NS::string_view path)126 bool OhosFilesystem::DeleteDirectory(BASE_NS::string_view path)
127 {
128     return false;
129 }
130 
Rename(BASE_NS::string_view fromPath,BASE_NS::string_view toPath)131 bool OhosFilesystem::Rename(BASE_NS::string_view fromPath, BASE_NS::string_view toPath)
132 {
133     return false;
134 }
135 
GetUriPaths(BASE_NS::string_view uri) const136 BASE_NS::vector<BASE_NS::string> OhosFilesystem::GetUriPaths(BASE_NS::string_view uri) const
137 {
138     return {};
139 }
140 CORE_END_NAMESPACE()
141