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