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(AdapateLongPath(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(AdapateLongPath(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(AdapateLongPath(src).c_str(), AdapateLongPath(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(AdapateLongPath(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, AdapateLongPath(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 return string(temp);
197 }
198 #else
199 char buffer[PATH_MAX];
200 if (!realpath(path.c_str(), buffer)) {
201 return "";
202 }
203 #endif
204 return string(buffer);
205 }
206
FilePath(const string & path)207 FileEntry::FilePath::FilePath(const string &path) : filePath_(path)
208 {
209 Format();
210 Init();
211 }
212
~FilePath()213 FileEntry::FilePath::~FilePath()
214 {
215 }
216
Append(const string & path)217 FileEntry::FilePath FileEntry::FilePath::Append(const string &path)
218 {
219 Format();
220 string filePath = filePath_ + SEPARATE + path;
221 return FilePath(filePath);
222 }
223
ReplaceExtension(const string & extension)224 FileEntry::FilePath FileEntry::FilePath::ReplaceExtension(const string &extension)
225 {
226 string filePath;
227 if (!parent_.empty()) {
228 filePath += parent_ + SEPARATE;
229 }
230
231 filePath += filename_.substr(0, filename_.length() - extension_.length()) + extension;
232 return FilePath(filePath);
233 }
234
GetParent()235 FileEntry::FilePath FileEntry::FilePath::GetParent()
236 {
237 return FilePath(parent_);
238 }
239
GetPath() const240 const string &FileEntry::FilePath::GetPath() const
241 {
242 return filePath_;
243 }
244
GetFilename() const245 const string &FileEntry::FilePath::GetFilename() const
246 {
247 return filename_;
248 }
249
GetExtension() const250 const string &FileEntry::FilePath::GetExtension() const
251 {
252 return extension_;
253 }
254
GetSegments() const255 const vector<string> FileEntry::FilePath::GetSegments() const
256 {
257 vector<string> segments;
258 string::size_type offset = 0;
259 string::size_type pos = filePath_.find_first_of(SEPARATE.front(), offset);
260 while (pos != string::npos) {
261 segments.push_back(filePath_.substr(offset, pos - offset));
262 offset = pos + 1;
263 pos = filePath_.find_first_of(SEPARATE.front(), offset);
264 }
265
266 if (offset < filePath_.length()) {
267 segments.push_back(filePath_.substr(offset));
268 }
269 return segments;
270 }
271
272 // below private
IsIgnore(const string & filename) const273 bool FileEntry::IsIgnore(const string &filename) const
274 {
275 if (filename == "." || filename == "..") {
276 return true;
277 }
278 return false;
279 }
280
RemoveAllDirInner(const FileEntry & entry)281 bool FileEntry::RemoveAllDirInner(const FileEntry &entry)
282 {
283 string path = entry.GetFilePath().GetPath();
284 if (entry.IsFile()) {
285 #ifdef _WIN32
286 bool result = remove(AdapateLongPath(path).c_str()) == 0;
287 #else
288 bool result = remove(path.c_str()) == 0;
289 #endif
290 if (!result) {
291 cerr << "Error: remove file '" << path << "' failed, reason: " << strerror(errno) << endl;
292 return false;
293 }
294 return true;
295 }
296
297 for (const auto &iter : entry.GetChilds()) {
298 if (!RemoveAllDirInner(*iter)) {
299 return false;
300 }
301 }
302 #ifdef _WIN32
303 bool result = rmdir(AdapateLongPath(path).c_str()) == 0;
304 #else
305 bool result = rmdir(path.c_str()) == 0;
306 #endif
307 if (!result) {
308 cerr << "Error: remove directory '" << path << "' failed, reason: " << strerror(errno) << endl;
309 return false;
310 }
311 return true;
312 }
313
CreateDirsInner(const string & path,string::size_type offset)314 bool FileEntry::CreateDirsInner(const string &path, string::size_type offset)
315 {
316 string::size_type pos = path.find_first_of(SEPARATE.front(), offset);
317 if (pos == string::npos) {
318 #ifdef _WIN32
319 return CreateDirectory(AdapateLongPath(path).c_str(), nullptr) != 0;
320 #else
321 return mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0;
322 #endif
323 }
324
325 string subPath = path.substr(0, pos + 1);
326 if (!Exist(subPath)) {
327 #ifdef _WIN32
328 if (!CreateDirectory(AdapateLongPath(subPath).c_str(), nullptr)) {
329 #else
330 if (mkdir(subPath.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
331 #endif
332 return false;
333 }
334 }
335 return CreateDirsInner(path, pos + 1);
336 }
337
338 void FileEntry::FilePath::Format()
339 {
340 if (filePath_.back() != SEPARATE.front()) {
341 return;
342 }
343 filePath_.pop_back();
344 }
345
346 void FileEntry::FilePath::Init()
347 {
348 filename_ = filePath_;
349 string::size_type pos = filePath_.find_last_of(SEPARATE.front());
350 if (pos != string::npos) {
351 parent_ = filePath_.substr(0, pos);
352 if (pos + 1 < filePath_.length()) {
353 filename_ = filePath_.substr(pos + 1);
354 }
355 }
356
357 pos = filename_.find_last_of('.');
358 if (pos != string::npos && pos + 1 < filename_.length()) {
359 extension_ = filename_.substr(pos);
360 }
361 }
362
363 #ifdef _WIN32
364 string FileEntry::AdapateLongPath(const string &path)
365 {
366 if (path.size() >= MAX_PATH -12) { //the max file path can not exceed 260 - 12
367 return LONG_PATH_HEAD + path;
368 }
369 return path;
370 }
371 #endif
372 }
373 }
374 }
375