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 "file_entry.h"
17 #include <cstring>
18 #include <fstream>
19 #include <iostream>
20 #include "dirent.h"
21 #include "sys/stat.h"
22 #include "unistd.h"
23 #ifdef _WIN32
24 #include "shlwapi.h"
25 #include "windows.h"
26 #endif
27 #include "resource_data.h"
28
29 namespace OHOS {
30 namespace Global {
31 namespace Restool {
32 #ifdef _WIN32
33 const std::string FileEntry::SEPARATE = "\\";
34 #else
35 const std::string FileEntry::SEPARATE = "/";
36 #endif
37
38 using namespace std;
FileEntry(const string & path)39 FileEntry::FileEntry(const string &path)
40 : filePath_(path), isFile_(false)
41 {
42 }
43
~FileEntry()44 FileEntry::~FileEntry()
45 {
46 }
47
Init()48 bool FileEntry::Init()
49 {
50 string filePath = filePath_.GetPath();
51 if (!Exist(filePath)) {
52 cerr << "Error: '" << filePath << "' not exists." << endl;
53 return false;
54 }
55
56 isFile_ = !IsDirectory(filePath);
57 return true;
58 }
59
GetChilds() const60 const vector<unique_ptr<FileEntry>> FileEntry::GetChilds() const
61 {
62 vector<unique_ptr<FileEntry>> children;
63 string filePath = filePath_.GetPath();
64 #ifdef _WIN32
65 WIN32_FIND_DATA findData;
66 string temp(filePath + "\\*.*");
67 HANDLE handle = FindFirstFile(AdaptLongPath(temp).c_str(), &findData);
68 if (handle == INVALID_HANDLE_VALUE) {
69 return children;
70 }
71
72 do {
73 string filename(findData.cFileName);
74 if (IsIgnore(filename)) {
75 continue;
76 }
77
78 filePath = filePath_.GetPath() + SEPARATE + filename;
79 unique_ptr<FileEntry> f = make_unique<FileEntry>(filePath);
80 f->Init();
81 children.push_back(move(f));
82 } while (FindNextFile(handle, &findData));
83 FindClose(handle);
84 #else
85 DIR *handle = opendir(filePath.c_str());
86 struct dirent *entry;
87 while ((entry = readdir(handle)) != nullptr) {
88 string filename(entry->d_name);
89 if (IsIgnore(filename)) {
90 continue;
91 }
92
93 filePath = filePath_.GetPath() + SEPARATE + filename;
94 unique_ptr<FileEntry> f = make_unique<FileEntry>(filePath);
95 f->Init();
96 children.push_back(move(f));
97 }
98 closedir(handle);
99 #endif
100 return children;
101 }
102
IsFile() const103 bool FileEntry::IsFile() const
104 {
105 return isFile_;
106 }
107
GetFilePath() const108 const FileEntry::FilePath &FileEntry::GetFilePath() const
109 {
110 return filePath_;
111 }
112
Exist(const string & path)113 bool FileEntry::Exist(const string &path)
114 {
115 #ifdef _WIN32
116 return PathFileExists(AdaptLongPath(path).c_str());
117 #else
118 struct stat s;
119 if (stat(path.c_str(), &s) != 0) {
120 return false;
121 }
122 #endif
123 return true;
124 }
125
RemoveAllDir(const string & path)126 bool FileEntry::RemoveAllDir(const string &path)
127 {
128 FileEntry f(path);
129 if (!f.Init()) {
130 return false;
131 }
132
133 if (f.IsFile()) {
134 cerr << "Error: RemoveAllDir '" << path << "' not directory." << endl;
135 return false;
136 }
137 return RemoveAllDirInner(f);
138 }
139
CreateDirs(const string & path)140 bool FileEntry::CreateDirs(const string &path)
141 {
142 return CreateDirsInner(path, 0);
143 }
144
CopyFileInner(const string & src,const string & dst)145 bool FileEntry::CopyFileInner(const string &src, const string &dst)
146 {
147 #ifdef _WIN32
148 if (!CopyFile(AdaptLongPath(src).c_str(), AdaptLongPath(dst).c_str(), false)) {
149 cerr << "Error: copy file fail from '" << src << "' to '" << dst << "'. reason:" << strerror(errno) << endl;
150 return false;
151 }
152 #else
153 ifstream in(src, ios::binary);
154 ofstream out(dst, ios::binary);
155 if (!in || !out) {
156 cerr << "Error: open failed '" << src << "' or '" << dst << "'. reason:" << strerror(errno) << endl;
157 return false;
158 }
159 out << in.rdbuf();
160 #endif
161 return true;
162 }
163
IsDirectory(const string & path)164 bool FileEntry::IsDirectory(const string &path)
165 {
166 #ifdef _WIN32
167 if (!PathIsDirectory(AdaptLongPath(path).c_str())) {
168 return false;
169 }
170 return true;
171 #else
172 struct stat s;
173 stat(path.c_str(), &s);
174 return S_ISDIR(s.st_mode);
175 #endif
176 }
177
RealPath(const string & path)178 string FileEntry::RealPath(const string &path)
179 {
180 #ifdef _WIN32
181 char buffer[MAX_PATH];
182 if (!PathCanonicalize(buffer, path.c_str())) {
183 return "";
184 }
185
186 if (PathIsRelative(buffer)) {
187 char current[MAX_PATH];
188 if (!GetCurrentDirectory(MAX_PATH, current)) {
189 return "";
190 }
191
192 char temp[MAX_PATH];
193 if (!PathCombine(temp, current, buffer)) {
194 return "";
195 }
196 if (!Exist(string(temp))) {
197 return "";
198 }
199 return string(temp);
200 }
201 #else
202 char buffer[PATH_MAX];
203 if (!realpath(path.c_str(), buffer)) {
204 return "";
205 }
206 #endif
207 if (!Exist(string(buffer))) {
208 return "";
209 }
210 return string(buffer);
211 }
212
FilePath(const string & path)213 FileEntry::FilePath::FilePath(const string &path) : filePath_(path)
214 {
215 Format();
216 Init();
217 }
218
~FilePath()219 FileEntry::FilePath::~FilePath()
220 {
221 }
222
Append(const string & path)223 FileEntry::FilePath FileEntry::FilePath::Append(const string &path)
224 {
225 Format();
226 string filePath = filePath_ + SEPARATE + path;
227 return FilePath(filePath);
228 }
229
ReplaceExtension(const string & extension)230 FileEntry::FilePath FileEntry::FilePath::ReplaceExtension(const string &extension)
231 {
232 string filePath;
233 if (!parent_.empty()) {
234 filePath += parent_ + SEPARATE;
235 }
236
237 filePath += filename_.substr(0, filename_.length() - extension_.length()) + extension;
238 return FilePath(filePath);
239 }
240
GetParent()241 FileEntry::FilePath FileEntry::FilePath::GetParent()
242 {
243 return FilePath(parent_);
244 }
245
GetPath() const246 const string &FileEntry::FilePath::GetPath() const
247 {
248 return filePath_;
249 }
250
GetFilename() const251 const string &FileEntry::FilePath::GetFilename() const
252 {
253 return filename_;
254 }
255
GetExtension() const256 const string &FileEntry::FilePath::GetExtension() const
257 {
258 return extension_;
259 }
260
GetSegments() const261 const vector<string> FileEntry::FilePath::GetSegments() const
262 {
263 vector<string> segments;
264 string::size_type offset = 0;
265 string::size_type pos = filePath_.find_first_of(SEPARATE.front(), offset);
266 while (pos != string::npos) {
267 segments.push_back(filePath_.substr(offset, pos - offset));
268 offset = pos + 1;
269 pos = filePath_.find_first_of(SEPARATE.front(), offset);
270 }
271
272 if (offset < filePath_.length()) {
273 segments.push_back(filePath_.substr(offset));
274 }
275 return segments;
276 }
277
278 // below private
IsIgnore(const string & filename) const279 bool FileEntry::IsIgnore(const string &filename) const
280 {
281 if (filename == "." || filename == "..") {
282 return true;
283 }
284 return false;
285 }
286
RemoveAllDirInner(const FileEntry & entry)287 bool FileEntry::RemoveAllDirInner(const FileEntry &entry)
288 {
289 string path = entry.GetFilePath().GetPath();
290 if (entry.IsFile()) {
291 #ifdef _WIN32
292 bool result = remove(AdaptLongPath(path).c_str()) == 0;
293 #else
294 bool result = remove(path.c_str()) == 0;
295 #endif
296 if (!result) {
297 cerr << "Error: remove file '" << path << "' failed, reason: " << strerror(errno) << endl;
298 return false;
299 }
300 return true;
301 }
302
303 for (const auto &iter : entry.GetChilds()) {
304 if (!RemoveAllDirInner(*iter)) {
305 return false;
306 }
307 }
308 #ifdef _WIN32
309 bool result = rmdir(AdaptLongPath(path).c_str()) == 0;
310 #else
311 bool result = rmdir(path.c_str()) == 0;
312 #endif
313 if (!result) {
314 cerr << "Error: remove directory '" << path << "' failed, reason: " << strerror(errno) << endl;
315 return false;
316 }
317 return true;
318 }
319
CreateDirsInner(const string & path,string::size_type offset)320 bool FileEntry::CreateDirsInner(const string &path, string::size_type offset)
321 {
322 string::size_type pos = path.find_first_of(SEPARATE.front(), offset);
323 if (pos == string::npos) {
324 #ifdef _WIN32
325 return CreateDirectory(AdaptLongPath(path).c_str(), nullptr) != 0;
326 #else
327 return mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0;
328 #endif
329 }
330
331 string subPath = path.substr(0, pos + 1);
332 if (!Exist(subPath)) {
333 #ifdef _WIN32
334 if (!CreateDirectory(AdaptLongPath(subPath).c_str(), nullptr)) {
335 #else
336 if (mkdir(subPath.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
337 #endif
338 return false;
339 }
340 }
341 return CreateDirsInner(path, pos + 1);
342 }
343
344 void FileEntry::FilePath::Format()
345 {
346 if (filePath_.back() != SEPARATE.front()) {
347 return;
348 }
349 filePath_.pop_back();
350 }
351
352 void FileEntry::FilePath::Init()
353 {
354 filename_ = filePath_;
355 string::size_type pos = filePath_.find_last_of(SEPARATE.front());
356 if (pos != string::npos) {
357 parent_ = filePath_.substr(0, pos);
358 if (pos + 1 < filePath_.length()) {
359 filename_ = filePath_.substr(pos + 1);
360 }
361 }
362
363 pos = filename_.find_last_of('.');
364 if (pos != string::npos && pos + 1 < filename_.length()) {
365 extension_ = filename_.substr(pos);
366 }
367 }
368
369 string FileEntry::AdaptLongPath(const string &path)
370 {
371 #ifdef _WIN32
372 if (path.size() >= MAX_PATH -12) { //the max file path can not exceed 260 - 12
373 return LONG_PATH_HEAD + path;
374 }
375 #endif
376 return path;
377 }
378 }
379 }
380 }
381