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 "std_filesystem.h"
17
18 #if defined(__PLATFORM_OHOS__)
19 #undef HAS_FILESYSTEM
20 #else
21 #if defined(__has_include)
22 #if __has_include(<filesystem>)
23 #include <filesystem>
24 #define HAS_FILESYSTEM
25 #endif
26 #endif // defined(__has_include)
27 #endif
28
29 #if !defined(HAS_FILESYSTEM)
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #endif
33
34 #include <cstdint>
35
36 #include <base/containers/string.h>
37 #include <base/containers/string_view.h>
38 #include <base/containers/unique_ptr.h>
39 #include <base/containers/vector.h>
40 #include <base/namespace.h>
41 #include <core/io/intf_directory.h>
42 #include <core/io/intf_file.h>
43 #include <core/log.h>
44 #include <core/namespace.h>
45
46 #include "io/path_tools.h"
47 #include "std_directory.h"
48 #include "std_file.h"
49
50 CORE_BEGIN_NAMESPACE()
51 using BASE_NS::make_unique;
52 using BASE_NS::string;
53 using BASE_NS::string_view;
54 using BASE_NS::vector;
55
56 namespace {
57 #if defined(HAS_FILESYSTEM)
U8Path(string_view str)58 std::filesystem::path U8Path(string_view str)
59 {
60 return std::filesystem::u8path(str.begin().ptr(), str.end().ptr());
61 }
62 #endif
63
64 } // namespace
65
ValidatePath(const string_view pathIn) const66 string StdFilesystem::ValidatePath(const string_view pathIn) const
67 {
68 auto path = NormalizePath(pathIn);
69 if (!path.empty()) {
70 if (!basePath_.empty()) {
71 // If basePath_ is set we are in a sandbox. so all paths are relative to basePath_ (after normalization)
72 path = basePath_ + path;
73 }
74 // path must be absolute.
75 if (path[0] != '/') {
76 CORE_LOG_V("Corrupted path in StdFilesystem::ValidatePath. not absolute");
77 return "";
78 }
79 #ifdef _WIN32
80 // path must have drive letter, otherwise it is NOT absolute. ie. must conform to "/C:/" style
81 if ((path.length() < 4) || (path[2] != ':') || (path[3] != '/')) { // 4: size limit; 2 3: index of ':' '/'
82 CORE_LOG_V("Corrupted path in StdFilesystem::ValidatePath. missing drive letter, or incorrect root");
83 return "";
84 }
85 // remove the '/' slash, which is not used in windows.
86 return string(path.substr(1));
87 #endif
88 }
89 return path;
90 }
91
OpenFile(const string_view pathIn,const IFile::Mode mode)92 IFile::Ptr StdFilesystem::OpenFile(const string_view pathIn, const IFile::Mode mode)
93 {
94 auto path = ValidatePath(pathIn);
95 if (!path.empty()) {
96 return StdFile::Open(path, mode);
97 }
98 return {};
99 }
100
CreateFile(const string_view pathIn)101 IFile::Ptr StdFilesystem::CreateFile(const string_view pathIn)
102 {
103 auto path = ValidatePath(pathIn);
104 if (!path.empty()) {
105 return StdFile::Create(path, IFile::Mode::READ_WRITE);
106 }
107
108 return {};
109 }
110
DeleteFile(const string_view pathIn)111 bool StdFilesystem::DeleteFile(const string_view pathIn)
112 {
113 auto path = ValidatePath(pathIn);
114 if (path.empty()) {
115 return false;
116 }
117 #if defined(HAS_FILESYSTEM)
118 std::error_code ec;
119 return std::filesystem::remove(U8Path(path), ec) && !ec;
120 #else
121 return std::remove(path.c_str()) == 0;
122 #endif
123 }
124
FileExists(const string_view pathIn) const125 bool StdFilesystem::FileExists(const string_view pathIn) const
126 {
127 auto path = ValidatePath(pathIn);
128 if (path.empty()) {
129 return false;
130 }
131 return StdFile::FileExists(path);
132 }
133
OpenDirectory(const string_view pathIn)134 IDirectory::Ptr StdFilesystem::OpenDirectory(const string_view pathIn)
135 {
136 auto path = ValidatePath(pathIn);
137 if (!path.empty()) {
138 return IDirectory::Ptr { StdDirectory::Open(path).release() };
139 }
140
141 return {};
142 }
143
CreateDirectory(const string_view pathIn)144 IDirectory::Ptr StdFilesystem::CreateDirectory(const string_view pathIn)
145 {
146 auto path = ValidatePath(pathIn);
147 if (!path.empty()) {
148 return IDirectory::Ptr { StdDirectory::Create(path).release() };
149 }
150
151 return {};
152 }
153
DeleteDirectory(const string_view pathIn)154 bool StdFilesystem::DeleteDirectory(const string_view pathIn)
155 {
156 auto path = ValidatePath(pathIn);
157 if (path.empty()) {
158 return false;
159 }
160 #if defined(HAS_FILESYSTEM)
161 std::error_code ec;
162 return std::filesystem::remove(U8Path(path), ec) && !ec;
163 #else
164 return rmdir(string(path).c_str()) == 0;
165 #endif
166 }
167
DirectoryExists(const string_view pathIn) const168 bool StdFilesystem::DirectoryExists(const string_view pathIn) const
169 {
170 auto path = ValidatePath(pathIn);
171 if (path.empty()) {
172 return false;
173 }
174 return StdDirectory::DirectoryExists(path);
175 }
176
Rename(const string_view fromPath,const string_view toPath)177 bool StdFilesystem::Rename(const string_view fromPath, const string_view toPath)
178 {
179 auto pathFrom = ValidatePath(fromPath);
180 auto pathTo = ValidatePath(toPath);
181 if (pathFrom.empty() || pathTo.empty()) {
182 return false;
183 }
184
185 #if defined(HAS_FILESYSTEM)
186 std::error_code ec;
187 std::filesystem::rename(U8Path(pathFrom), U8Path(pathTo), ec);
188 return !ec;
189 #else
190 return std::rename(pathFrom.c_str(), pathTo.c_str()) == 0;
191 #endif
192 }
193
GetUriPaths(const string_view) const194 vector<string> StdFilesystem::GetUriPaths(const string_view) const
195 {
196 return {};
197 }
198
StdFilesystem(string_view basePath)199 StdFilesystem::StdFilesystem(string_view basePath) : basePath_(basePath)
200 {
201 // remove the extraneous slash
202 if (basePath_.back() == '/') {
203 basePath_.resize(basePath_.size() - 1);
204 }
205 }
206
207 CORE_END_NAMESPACE()
208
209 // the rest is here, due to shlwapi leaking windows CreateFile macro, and breaking build.
210 #if !defined(HAS_FILESYSTEM)
211 #include <climits>
212 #define CORE_MAX_PATH PATH_MAX
213 #endif
214
CORE_BEGIN_NAMESPACE()215 CORE_BEGIN_NAMESPACE()
216 IDirectory::Entry StdFilesystem::GetEntry(const string_view uriIn)
217 {
218 auto uri = ValidatePath(uriIn);
219 if (!uri.empty()) {
220 #if defined(HAS_FILESYSTEM)
221 std::error_code ec;
222 auto canonicalPath = std::filesystem::canonical(U8Path(uri), ec);
223 if (ec) {
224 return {};
225 }
226 auto status = std::filesystem::status(canonicalPath, ec);
227 if (ec) {
228 return {};
229 }
230 auto time = std::filesystem::last_write_time(canonicalPath, ec);
231 if (ec) {
232 return {};
233 }
234
235 auto asString = canonicalPath.u8string();
236 if (std::filesystem::is_directory(status)) {
237 return { IDirectory::Entry::DIRECTORY, string { asString.data(), asString.size() },
238 static_cast<uint64_t>(time.time_since_epoch().count()) };
239 }
240 if (std::filesystem::is_regular_file(status)) {
241 return { IDirectory::Entry::FILE, string { asString.data(), asString.size() },
242 static_cast<uint64_t>(time.time_since_epoch().count()) };
243 }
244 #else
245 auto path = string(uri);
246 char canonicalPath[CORE_MAX_PATH] = { 0 };
247
248 if (realpath(path.c_str(), canonicalPath) == nullptr) {
249 return {};
250 }
251 struct stat ds {};
252 if (stat(canonicalPath, &ds) != 0) {
253 return {};
254 }
255
256 if ((ds.st_mode & S_IFDIR)) {
257 return { IDirectory::Entry::DIRECTORY, canonicalPath, static_cast<uint64_t>(ds.st_mtime) };
258 }
259 if ((ds.st_mode & S_IFREG)) {
260 return { IDirectory::Entry::FILE, canonicalPath, static_cast<uint64_t>(ds.st_mtime) };
261 }
262 #endif
263 }
264 return {};
265 }
266
267 CORE_END_NAMESPACE()
268