• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 #ifdef _WIN32
19 #include <direct.h>
20 #else
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #endif
24 
25 #include <core/namespace.h>
26 
27 #include "io/path_tools.h"
28 #include "std_directory.h"
29 #include "std_file.h"
30 #include "util/string_util.h"
31 
32 CORE_BEGIN_NAMESPACE()
33 using BASE_NS::make_unique;
34 using BASE_NS::string;
35 using BASE_NS::string_view;
36 using BASE_NS::vector;
37 
ValidatePath(const string_view pathIn) const38 string StdFilesystem::ValidatePath(const string_view pathIn) const
39 {
40     auto path = NormalizePath(pathIn);
41     if (!path.empty()) {
42         if (!basePath_.empty()) {
43             // If basePath_ is set we are in a sandbox. so all paths are relative to basePath_ (after normalization)
44             path = basePath_ + path;
45         }
46         // path must be absolute.
47         if (path[0] != '/') {
48             CORE_LOG_V("Corrupted path in StdFilesystem::ValidatePath. not absolute");
49             return "";
50         }
51 #ifdef _WIN32
52         // path must have drive letter, otherwise it is NOT absolute. ie. must conform to "/C:/" style
53         if ((path.length() < 4) || (path[2] != ':') || (path[3] != '/')) { // 4: size limit; 2 3: index of ':' '/'
54             CORE_LOG_V("Corrupted path in StdFilesystem::ValidatePath. missing drive letter, or incorrect root");
55             return "";
56         }
57         // remove the '/' slash, which is not used in windows.
58         return string(path.substr(1));
59 #endif
60     }
61     return path;
62 }
63 
OpenFile(const string_view pathIn)64 IFile::Ptr StdFilesystem::OpenFile(const string_view pathIn)
65 {
66     auto path = ValidatePath(pathIn);
67     if (!path.empty()) {
68         auto file = make_unique<StdFile>();
69         if (file->Open(path, IFile::Mode::READ_ONLY)) {
70             return IFile::Ptr { file.release() };
71         }
72     }
73     return IFile::Ptr();
74 }
75 
CreateFile(const string_view pathIn)76 IFile::Ptr StdFilesystem::CreateFile(const string_view pathIn)
77 {
78     auto path = ValidatePath(pathIn);
79     if (!path.empty()) {
80         auto file = make_unique<StdFile>();
81         if (file->Create(path, IFile::Mode::READ_WRITE)) {
82             return IFile::Ptr { file.release() };
83         }
84     }
85 
86     return IFile::Ptr();
87 }
88 
DeleteFile(const string_view pathIn)89 bool StdFilesystem::DeleteFile(const string_view pathIn)
90 {
91     auto path = ValidatePath(pathIn);
92     if (path.empty()) {
93         return false;
94     }
95     return remove(path.c_str()) == 0;
96 }
97 
OpenDirectory(const string_view pathIn)98 IDirectory::Ptr StdFilesystem::OpenDirectory(const string_view pathIn)
99 {
100     auto path = ValidatePath(pathIn);
101     if (!path.empty()) {
102         auto directory = make_unique<StdDirectory>();
103         if (directory->Open(path)) {
104             return IDirectory::Ptr { directory.release() };
105         }
106     }
107 
108     return IDirectory::Ptr();
109 }
110 
CreateDirectory(const string_view pathIn)111 IDirectory::Ptr StdFilesystem::CreateDirectory(const string_view pathIn)
112 {
113     auto path = ValidatePath(pathIn);
114     if (!path.empty()) {
115 #ifdef _WIN32
116         int result = _mkdir(string(path).c_str());
117 #else
118         int result = mkdir(string(path).c_str(), S_IRWXU | S_IRWXO);
119 #endif
120         if (result == 0) {
121             // Directory creation successful.
122             return OpenDirectory(path);
123         }
124     }
125 
126     return IDirectory::Ptr();
127 }
128 
DeleteDirectory(const string_view pathIn)129 bool StdFilesystem::DeleteDirectory(const string_view pathIn)
130 {
131     auto path = ValidatePath(pathIn);
132     if (path.empty()) {
133         return false;
134     }
135 #ifdef _WIN32
136     return _rmdir(string(path).c_str()) == 0;
137 #else
138     return rmdir(string(path).c_str()) == 0;
139 #endif
140 }
141 
Rename(const string_view fromPath,const string_view toPath)142 bool StdFilesystem::Rename(const string_view fromPath, const string_view toPath)
143 {
144     auto pathFrom = ValidatePath(fromPath);
145     auto pathTo = ValidatePath(toPath);
146     if (pathFrom.empty() || pathTo.empty()) {
147         return false;
148     }
149 
150     return rename(pathFrom.c_str(), pathTo.c_str()) == 0;
151 }
152 
GetUriPaths(const string_view) const153 vector<string> StdFilesystem::GetUriPaths(const string_view) const
154 {
155     return {};
156 }
157 
StdFilesystem(string_view basePath)158 StdFilesystem::StdFilesystem(string_view basePath) : basePath_(basePath)
159 {
160     // remove the extraneous slash
161     if (basePath_.back() == '/') {
162         basePath_.resize(basePath_.size() - 1);
163     }
164 }
165 
166 CORE_END_NAMESPACE()
167 
168 // the rest is here, due to shlwapi leaking windows CreateFile macro, and breaking build.
169 #if defined(_WIN32)
170 #include <shlwapi.h>
171 #pragma comment(lib, "Shlwapi.lib")
172 #define CORE_MAX_PATH MAX_PATH
173 #else
174 #include <climits>
175 #define CORE_MAX_PATH PATH_MAX
176 #endif
177 
CORE_BEGIN_NAMESPACE()178 CORE_BEGIN_NAMESPACE()
179 IDirectory::Entry StdFilesystem::GetEntry(const string_view uriIn)
180 {
181     auto uri = ValidatePath(uriIn);
182     if (!uri.empty()) {
183         struct stat ds;
184         auto path = string(uri);
185         char canonicalPath[CORE_MAX_PATH] = { 0 };
186 
187 #if defined(_WIN32)
188         if (!PathCanonicalize(canonicalPath, path.c_str())) {
189             return {};
190         }
191 #else
192         if (realpath(path.c_str(), canonicalPath) == NULL) {
193             return {};
194         }
195 #endif
196 
197         if (stat(canonicalPath, &ds) != 0) {
198             return {};
199         }
200 
201         if ((ds.st_mode & S_IFDIR)) {
202             return { IDirectory::Entry::DIRECTORY, canonicalPath, static_cast<uint64_t>(ds.st_mtime) };
203         }
204         if ((ds.st_mode & S_IFREG)) {
205             return { IDirectory::Entry::FILE, canonicalPath, static_cast<uint64_t>(ds.st_mtime) };
206         }
207     }
208     return {};
209 }
210 
211 CORE_END_NAMESPACE()